blob: 32dbfd81f42a48a27c2927634d46a4d30609d7e1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Russell King4baa9922008-08-02 10:55:55 +01003 * arch/arm/include/asm/uaccess.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5#ifndef _ASMARM_UACCESS_H
6#define _ASMARM_UACCESS_H
7
8/*
9 * User space memory access functions
10 */
Russell King87c52572008-11-29 17:35:51 +000011#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/memory.h>
13#include <asm/domain.h>
Ard Biesheuvel15420262022-01-18 13:45:09 +010014#include <asm/unaligned.h>
Catalin Marinas8b592782009-07-24 12:32:57 +010015#include <asm/unified.h>
David Howells9f97da72012-03-28 18:30:01 +010016#include <asm/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Al Viro0f9b38c2016-12-25 03:33:03 -050018#include <asm/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
Russell King3fba7e22015-08-19 11:02:28 +010021 * These two functions allow hooking accesses to userspace to increase
22 * system integrity by ensuring that the kernel can not inadvertantly
23 * perform such accesses (eg, via list poison values) which could then
24 * be exploited for priviledge escalation.
25 */
Masahiro Yamada851140a2019-10-02 11:28:02 +010026static __always_inline unsigned int uaccess_save_and_enable(void)
Russell King3fba7e22015-08-19 11:02:28 +010027{
Russell Kinga5e090a2015-08-19 20:40:41 +010028#ifdef CONFIG_CPU_SW_DOMAIN_PAN
29 unsigned int old_domain = get_domain();
30
31 /* Set the current domain access to permit user accesses */
32 set_domain((old_domain & ~domain_mask(DOMAIN_USER)) |
33 domain_val(DOMAIN_USER, DOMAIN_CLIENT));
34
35 return old_domain;
36#else
Russell King3fba7e22015-08-19 11:02:28 +010037 return 0;
Russell Kinga5e090a2015-08-19 20:40:41 +010038#endif
Russell King3fba7e22015-08-19 11:02:28 +010039}
40
Masahiro Yamada851140a2019-10-02 11:28:02 +010041static __always_inline void uaccess_restore(unsigned int flags)
Russell King3fba7e22015-08-19 11:02:28 +010042{
Russell Kinga5e090a2015-08-19 20:40:41 +010043#ifdef CONFIG_CPU_SW_DOMAIN_PAN
44 /* Restore the user access mask */
45 set_domain(flags);
46#endif
Russell King3fba7e22015-08-19 11:02:28 +010047}
48
49/*
Russell King9641c7c2006-06-21 20:38:17 +010050 * These two are intentionally not defined anywhere - if the kernel
51 * code generates any references to them, that's a bug.
52 */
53extern int __get_user_bad(void);
54extern int __put_user_bad(void);
55
Russell King9641c7c2006-06-21 20:38:17 +010056#ifdef CONFIG_MMU
57
Russell King2350ebe2020-07-22 09:50:23 +010058/*
59 * We use 33-bit arithmetic here. Success returns zero, failure returns
60 * addr_limit. We take advantage that addr_limit will be zero for KERNEL_DS,
61 * so this will always return success in that case.
62 */
Michael S. Tsirkin295bb012015-01-06 15:11:13 +020063#define __range_ok(addr, size) ({ \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080064 unsigned long flag, roksum; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 __chk_user_ptr(addr); \
Stefan Agner32fdb042019-01-24 21:42:54 +010066 __asm__(".syntax unified\n" \
67 "adds %1, %2, %3; sbcscc %1, %1, %0; movcc %0, #0" \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080068 : "=&r" (flag), "=&r" (roksum) \
Arnd Bergmann8ac6f5d2021-08-11 08:30:26 +010069 : "r" (addr), "Ir" (size), "0" (TASK_SIZE) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 : "cc"); \
71 flag; })
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Russell Kingd09fbb32018-07-09 15:22:45 +010074 * This is a type: either unsigned long, if the argument fits into
75 * that type, or otherwise unsigned long long.
76 */
77#define __inttype(x) \
78 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
79
80/*
Julien Thierryafaf6832018-09-11 10:14:50 +010081 * Sanitise a uaccess pointer such that it becomes NULL if addr+size
82 * is above the current addr_limit.
83 */
84#define uaccess_mask_range_ptr(ptr, size) \
85 ((__typeof__(ptr))__uaccess_mask_range_ptr(ptr, size))
86static inline void __user *__uaccess_mask_range_ptr(const void __user *ptr,
87 size_t size)
88{
89 void __user *safe_ptr = (void __user *)ptr;
90 unsigned long tmp;
91
92 asm volatile(
Stefan Agnerfe09d9c2019-03-23 12:10:44 +010093 " .syntax unified\n"
Julien Thierryafaf6832018-09-11 10:14:50 +010094 " sub %1, %3, #1\n"
95 " subs %1, %1, %0\n"
96 " addhs %1, %1, #1\n"
Stefan Agnerfe09d9c2019-03-23 12:10:44 +010097 " subshs %1, %1, %2\n"
Julien Thierryafaf6832018-09-11 10:14:50 +010098 " movlo %0, #0\n"
99 : "+r" (safe_ptr), "=&r" (tmp)
Arnd Bergmann8ac6f5d2021-08-11 08:30:26 +0100100 : "r" (size), "r" (TASK_SIZE)
Julien Thierryafaf6832018-09-11 10:14:50 +0100101 : "cc");
102
103 csdb();
104 return safe_ptr;
105}
106
107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Single-value transfer routines. They automatically use the right
109 * size if we just have the right pointer type. Note that the functions
110 * which read from user space (*get_*) need to take care not to leak
111 * kernel data even if the calling code is buggy and fails to check
112 * the return value. This means zeroing out the destination variable
113 * or buffer on error. Normally this is done out of line by the
114 * fixup code, but there are a few places where it intrudes on the
115 * main code path. When we only write to user space, there is no
116 * problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118extern int __get_user_1(void *);
119extern int __get_user_2(void *);
120extern int __get_user_4(void *);
Victor Kamenskyd9981382014-09-04 06:07:33 +0100121extern int __get_user_32t_8(void *);
Daniel Thompsone38361d2014-07-10 20:58:08 +0100122extern int __get_user_8(void *);
Victor Kamenskyd9981382014-09-04 06:07:33 +0100123extern int __get_user_64t_1(void *);
124extern int __get_user_64t_2(void *);
125extern int __get_user_64t_4(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Russell King84046632012-09-07 18:22:28 +0100127#define __GUP_CLOBBER_1 "lr", "cc"
128#ifdef CONFIG_CPU_USE_DOMAINS
129#define __GUP_CLOBBER_2 "ip", "lr", "cc"
130#else
131#define __GUP_CLOBBER_2 "lr", "cc"
132#endif
133#define __GUP_CLOBBER_4 "lr", "cc"
Victor Kamenskyd9981382014-09-04 06:07:33 +0100134#define __GUP_CLOBBER_32t_8 "lr", "cc"
Daniel Thompsone38361d2014-07-10 20:58:08 +0100135#define __GUP_CLOBBER_8 "lr", "cc"
Russell King84046632012-09-07 18:22:28 +0100136
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200137#define __get_user_x(__r2, __p, __e, __l, __s) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 __asm__ __volatile__ ( \
139 __asmeq("%0", "r0") __asmeq("%1", "r2") \
Russell King84046632012-09-07 18:22:28 +0100140 __asmeq("%3", "r1") \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 "bl __get_user_" #__s \
142 : "=&r" (__e), "=r" (__r2) \
Russell King84046632012-09-07 18:22:28 +0100143 : "0" (__p), "r" (__l) \
144 : __GUP_CLOBBER_##__s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Daniel Thompsone38361d2014-07-10 20:58:08 +0100146/* narrowing a double-word get into a single 32bit word register: */
147#ifdef __ARMEB__
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200148#define __get_user_x_32t(__r2, __p, __e, __l, __s) \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100149 __get_user_x(__r2, __p, __e, __l, 32t_8)
Daniel Thompsone38361d2014-07-10 20:58:08 +0100150#else
Victor Kamenskyd9981382014-09-04 06:07:33 +0100151#define __get_user_x_32t __get_user_x
Daniel Thompsone38361d2014-07-10 20:58:08 +0100152#endif
153
Victor Kamenskyd9981382014-09-04 06:07:33 +0100154/*
155 * storing result into proper least significant word of 64bit target var,
156 * different only for big endian case where 64 bit __r2 lsw is r3:
157 */
158#ifdef __ARMEB__
159#define __get_user_x_64t(__r2, __p, __e, __l, __s) \
160 __asm__ __volatile__ ( \
161 __asmeq("%0", "r0") __asmeq("%1", "r2") \
162 __asmeq("%3", "r1") \
163 "bl __get_user_64t_" #__s \
164 : "=&r" (__e), "=r" (__r2) \
165 : "0" (__p), "r" (__l) \
166 : __GUP_CLOBBER_##__s)
167#else
168#define __get_user_x_64t __get_user_x
169#endif
170
171
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200172#define __get_user_check(x, p) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ({ \
Arnd Bergmann8ac6f5d2021-08-11 08:30:26 +0100174 unsigned long __limit = TASK_SIZE - 1; \
Stefan Agnerdb4667a2018-05-08 22:51:50 +0100175 register typeof(*(p)) __user *__p asm("r0") = (p); \
Russell Kingd09fbb32018-07-09 15:22:45 +0100176 register __inttype(x) __r2 asm("r2"); \
Russell King84046632012-09-07 18:22:28 +0100177 register unsigned long __l asm("r1") = __limit; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 register int __e asm("r0"); \
Russell King3fba7e22015-08-19 11:02:28 +0100179 unsigned int __ua_flags = uaccess_save_and_enable(); \
Lexi Shaodf909df2021-09-23 03:41:25 +0100180 int __tmp_e; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 switch (sizeof(*(__p))) { \
182 case 1: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100183 if (sizeof((x)) >= 8) \
184 __get_user_x_64t(__r2, __p, __e, __l, 1); \
185 else \
186 __get_user_x(__r2, __p, __e, __l, 1); \
Russell King84046632012-09-07 18:22:28 +0100187 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 case 2: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100189 if (sizeof((x)) >= 8) \
190 __get_user_x_64t(__r2, __p, __e, __l, 2); \
191 else \
192 __get_user_x(__r2, __p, __e, __l, 2); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break; \
194 case 4: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100195 if (sizeof((x)) >= 8) \
196 __get_user_x_64t(__r2, __p, __e, __l, 4); \
197 else \
198 __get_user_x(__r2, __p, __e, __l, 4); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break; \
Daniel Thompsone38361d2014-07-10 20:58:08 +0100200 case 8: \
201 if (sizeof((x)) < 8) \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100202 __get_user_x_32t(__r2, __p, __e, __l, 4); \
Daniel Thompsone38361d2014-07-10 20:58:08 +0100203 else \
204 __get_user_x(__r2, __p, __e, __l, 8); \
205 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 default: __e = __get_user_bad(); break; \
207 } \
Lexi Shaodf909df2021-09-23 03:41:25 +0100208 __tmp_e = __e; \
Russell King3fba7e22015-08-19 11:02:28 +0100209 uaccess_restore(__ua_flags); \
Russell Kingd2c5b692005-11-18 14:22:03 +0000210 x = (typeof(*(p))) __r2; \
Lexi Shaodf909df2021-09-23 03:41:25 +0100211 __tmp_e; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 })
213
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200214#define get_user(x, p) \
Will Deaconad729072012-09-07 18:24:10 +0100215 ({ \
216 might_fault(); \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200217 __get_user_check(x, p); \
Will Deaconad729072012-09-07 18:24:10 +0100218 })
219
Russell King9641c7c2006-06-21 20:38:17 +0100220extern int __put_user_1(void *, unsigned int);
221extern int __put_user_2(void *, unsigned int);
222extern int __put_user_4(void *, unsigned int);
223extern int __put_user_8(void *, unsigned long long);
224
Russell King9f73bd82016-02-04 16:54:45 +0000225#define __put_user_check(__pu_val, __ptr, __err, __s) \
Russell King9641c7c2006-06-21 20:38:17 +0100226 ({ \
Arnd Bergmann8ac6f5d2021-08-11 08:30:26 +0100227 unsigned long __limit = TASK_SIZE - 1; \
Russell King9f73bd82016-02-04 16:54:45 +0000228 register typeof(__pu_val) __r2 asm("r2") = __pu_val; \
229 register const void __user *__p asm("r0") = __ptr; \
Russell King84046632012-09-07 18:22:28 +0100230 register unsigned long __l asm("r1") = __limit; \
Russell King9641c7c2006-06-21 20:38:17 +0100231 register int __e asm("r0"); \
Russell King9f73bd82016-02-04 16:54:45 +0000232 __asm__ __volatile__ ( \
233 __asmeq("%0", "r0") __asmeq("%2", "r2") \
234 __asmeq("%3", "r1") \
235 "bl __put_user_" #__s \
236 : "=&r" (__e) \
237 : "0" (__p), "r" (__r2), "r" (__l) \
238 : "ip", "lr", "cc"); \
239 __err = __e; \
Russell King9641c7c2006-06-21 20:38:17 +0100240 })
241
242#else /* CONFIG_MMU */
243
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200244#define __addr_ok(addr) ((void)(addr), 1)
245#define __range_ok(addr, size) ((void)(addr), 0)
Russell King9641c7c2006-06-21 20:38:17 +0100246
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200247#define get_user(x, p) __get_user(x, p)
Russell King9f73bd82016-02-04 16:54:45 +0000248#define __put_user_check __put_user_nocheck
Russell King9641c7c2006-06-21 20:38:17 +0100249
250#endif /* CONFIG_MMU */
251
Linus Torvalds96d4f262019-01-03 18:57:57 -0800252#define access_ok(addr, size) (__range_ok(addr, size) == 0)
Russell King9641c7c2006-06-21 20:38:17 +0100253
Russell Kingb1cd0a12018-07-09 10:28:12 +0100254#ifdef CONFIG_CPU_SPECTRE
255/*
256 * When mitigating Spectre variant 1, it is not worth fixing the non-
257 * verifying accessors, because we need to add verification of the
258 * address space there. Force these to use the standard get_user()
259 * version instead.
260 */
261#define __get_user(x, ptr) get_user(x, ptr)
262#else
263
Russell King9641c7c2006-06-21 20:38:17 +0100264/*
265 * The "__xxx" versions of the user access functions do not verify the
266 * address space - it must have been done previously with a separate
267 * "access_ok()" call.
268 *
269 * The "xxx_error" versions set the third argument to EFAULT if an
270 * error occurs, and leave it unchanged on success. Note that these
271 * versions are void (ie, don't return a value as such).
272 */
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200273#define __get_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274({ \
275 long __gu_err = 0; \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100276 __get_user_err((x), (ptr), __gu_err, TUSER()); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 __gu_err; \
278})
279
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100280#define __get_user_err(x, ptr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281do { \
282 unsigned long __gu_addr = (unsigned long)(ptr); \
283 unsigned long __gu_val; \
Russell King3fba7e22015-08-19 11:02:28 +0100284 unsigned int __ua_flags; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 __chk_user_ptr(ptr); \
Will Deaconad729072012-09-07 18:24:10 +0100286 might_fault(); \
Russell King3fba7e22015-08-19 11:02:28 +0100287 __ua_flags = uaccess_save_and_enable(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 switch (sizeof(*(ptr))) { \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100289 case 1: __get_user_asm_byte(__gu_val, __gu_addr, err, __t); break; \
290 case 2: __get_user_asm_half(__gu_val, __gu_addr, err, __t); break; \
291 case 4: __get_user_asm_word(__gu_val, __gu_addr, err, __t); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 default: (__gu_val) = __get_user_bad(); \
293 } \
Russell King3fba7e22015-08-19 11:02:28 +0100294 uaccess_restore(__ua_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 (x) = (__typeof__(*(ptr)))__gu_val; \
296} while (0)
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Russell Kingb64d1f62015-08-18 23:06:25 +0100299#define __get_user_asm(x, addr, err, instr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 __asm__ __volatile__( \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100301 "1: " instr " %1, [%2], #0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 "2:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100303 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 " .align 2\n" \
305 "3: mov %0, %3\n" \
306 " mov %1, #0\n" \
307 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100308 " .popsection\n" \
309 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 " .align 3\n" \
311 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100312 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 : "+r" (err), "=&r" (x) \
314 : "r" (addr), "i" (-EFAULT) \
315 : "cc")
316
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100317#define __get_user_asm_byte(x, addr, err, __t) \
318 __get_user_asm(x, addr, err, "ldrb" __t)
Russell Kingb64d1f62015-08-18 23:06:25 +0100319
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100320#if __LINUX_ARM_ARCH__ >= 6
321
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100322#define __get_user_asm_half(x, addr, err, __t) \
323 __get_user_asm(x, addr, err, "ldrh" __t)
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100324
325#else
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifndef __ARMEB__
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100328#define __get_user_asm_half(x, __gu_addr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329({ \
330 unsigned long __b1, __b2; \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100331 __get_user_asm_byte(__b1, __gu_addr, err, __t); \
332 __get_user_asm_byte(__b2, __gu_addr + 1, err, __t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 (x) = __b1 | (__b2 << 8); \
334})
335#else
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100336#define __get_user_asm_half(x, __gu_addr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337({ \
338 unsigned long __b1, __b2; \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100339 __get_user_asm_byte(__b1, __gu_addr, err, __t); \
340 __get_user_asm_byte(__b2, __gu_addr + 1, err, __t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 (x) = (__b1 << 8) | __b2; \
342})
343#endif
344
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100345#endif /* __LINUX_ARM_ARCH__ >= 6 */
346
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100347#define __get_user_asm_word(x, addr, err, __t) \
348 __get_user_asm(x, addr, err, "ldr" __t)
Russell King9f73bd82016-02-04 16:54:45 +0000349
350#define __put_user_switch(x, ptr, __err, __fn) \
351 do { \
352 const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
353 __typeof__(*(ptr)) __pu_val = (x); \
354 unsigned int __ua_flags; \
355 might_fault(); \
356 __ua_flags = uaccess_save_and_enable(); \
357 switch (sizeof(*(ptr))) { \
358 case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \
359 case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \
360 case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \
361 case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \
362 default: __err = __put_user_bad(); break; \
363 } \
364 uaccess_restore(__ua_flags); \
365 } while (0)
366
367#define put_user(x, ptr) \
368({ \
369 int __pu_err = 0; \
370 __put_user_switch((x), (ptr), __pu_err, __put_user_check); \
371 __pu_err; \
372})
373
Julien Thierrye3aa6242018-09-11 10:13:39 +0100374#ifdef CONFIG_CPU_SPECTRE
375/*
376 * When mitigating Spectre variant 1.1, all accessors need to include
377 * verification of the address space.
378 */
379#define __put_user(x, ptr) put_user(x, ptr)
380
381#else
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200382#define __put_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383({ \
384 long __pu_err = 0; \
Russell King9f73bd82016-02-04 16:54:45 +0000385 __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 __pu_err; \
387})
388
Russell King9f73bd82016-02-04 16:54:45 +0000389#define __put_user_nocheck(x, __pu_ptr, __err, __size) \
390 do { \
391 unsigned long __pu_addr = (unsigned long)__pu_ptr; \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100392 __put_user_nocheck_##__size(x, __pu_addr, __err, TUSER());\
Russell King9f73bd82016-02-04 16:54:45 +0000393 } while (0)
394
395#define __put_user_nocheck_1 __put_user_asm_byte
396#define __put_user_nocheck_2 __put_user_asm_half
397#define __put_user_nocheck_4 __put_user_asm_word
398#define __put_user_nocheck_8 __put_user_asm_dword
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100400#endif /* !CONFIG_CPU_SPECTRE */
401
Russell Kingb64d1f62015-08-18 23:06:25 +0100402#define __put_user_asm(x, __pu_addr, err, instr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 __asm__ __volatile__( \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100404 "1: " instr " %1, [%2], #0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 "2:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100406 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 " .align 2\n" \
408 "3: mov %0, %3\n" \
409 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100410 " .popsection\n" \
411 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 " .align 3\n" \
413 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100414 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 : "+r" (err) \
416 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
417 : "cc")
418
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100419#define __put_user_asm_byte(x, __pu_addr, err, __t) \
420 __put_user_asm(x, __pu_addr, err, "strb" __t)
Russell Kingb64d1f62015-08-18 23:06:25 +0100421
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100422#if __LINUX_ARM_ARCH__ >= 6
423
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100424#define __put_user_asm_half(x, __pu_addr, err, __t) \
425 __put_user_asm(x, __pu_addr, err, "strh" __t)
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100426
427#else
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#ifndef __ARMEB__
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100430#define __put_user_asm_half(x, __pu_addr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431({ \
Michael S. Tsirkine8b94de2015-01-06 14:37:22 +0200432 unsigned long __temp = (__force unsigned long)(x); \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100433 __put_user_asm_byte(__temp, __pu_addr, err, __t); \
434 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err, __t);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435})
436#else
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100437#define __put_user_asm_half(x, __pu_addr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438({ \
Michael S. Tsirkine8b94de2015-01-06 14:37:22 +0200439 unsigned long __temp = (__force unsigned long)(x); \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100440 __put_user_asm_byte(__temp >> 8, __pu_addr, err, __t); \
441 __put_user_asm_byte(__temp, __pu_addr + 1, err, __t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442})
443#endif
444
Vincent Whitchurch344eb5532018-11-09 10:12:30 +0100445#endif /* __LINUX_ARM_ARCH__ >= 6 */
446
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100447#define __put_user_asm_word(x, __pu_addr, err, __t) \
448 __put_user_asm(x, __pu_addr, err, "str" __t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450#ifndef __ARMEB__
451#define __reg_oper0 "%R2"
452#define __reg_oper1 "%Q2"
453#else
454#define __reg_oper0 "%Q2"
455#define __reg_oper1 "%R2"
456#endif
457
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100458#define __put_user_asm_dword(x, __pu_addr, err, __t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 __asm__ __volatile__( \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100460 ARM( "1: str" __t " " __reg_oper1 ", [%1], #4\n" ) \
461 ARM( "2: str" __t " " __reg_oper0 ", [%1]\n" ) \
462 THUMB( "1: str" __t " " __reg_oper1 ", [%1]\n" ) \
463 THUMB( "2: str" __t " " __reg_oper0 ", [%1, #4]\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 "3:\n" \
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100465 " .pushsection .text.fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 " .align 2\n" \
467 "4: mov %0, %3\n" \
468 " b 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100469 " .popsection\n" \
470 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 " .align 3\n" \
472 " .long 1b, 4b\n" \
473 " .long 2b, 4b\n" \
Russell King42604152010-04-19 10:15:03 +0100474 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 : "+r" (err), "+r" (__pu_addr) \
476 : "r" (x), "i" (-EFAULT) \
477 : "cc")
478
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100479#define HAVE_GET_KERNEL_NOFAULT
480
481#define __get_kernel_nofault(dst, src, type, err_label) \
482do { \
483 const type *__pk_ptr = (src); \
484 unsigned long __src = (unsigned long)(__pk_ptr); \
485 type __val; \
486 int __err = 0; \
487 switch (sizeof(type)) { \
488 case 1: __get_user_asm_byte(__val, __src, __err, ""); break; \
489 case 2: __get_user_asm_half(__val, __src, __err, ""); break; \
490 case 4: __get_user_asm_word(__val, __src, __err, ""); break; \
491 case 8: { \
492 u32 *__v32 = (u32*)&__val; \
493 __get_user_asm_word(__v32[0], __src, __err, ""); \
494 if (__err) \
495 break; \
496 __get_user_asm_word(__v32[1], __src+4, __err, ""); \
497 break; \
498 } \
499 default: __err = __get_user_bad(); break; \
500 } \
Ard Biesheuvel15420262022-01-18 13:45:09 +0100501 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) \
502 put_unaligned(__val, (type *)(dst)); \
503 else \
504 *(type *)(dst) = __val; /* aligned by caller */ \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100505 if (__err) \
506 goto err_label; \
507} while (0)
508
509#define __put_kernel_nofault(dst, src, type, err_label) \
510do { \
511 const type *__pk_ptr = (dst); \
512 unsigned long __dst = (unsigned long)__pk_ptr; \
513 int __err = 0; \
Ard Biesheuvel15420262022-01-18 13:45:09 +0100514 type __val = IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) \
515 ? get_unaligned((type *)(src)) \
516 : *(type *)(src); /* aligned by caller */ \
Arnd Bergmann2df4c9a2021-08-11 08:30:25 +0100517 switch (sizeof(type)) { \
518 case 1: __put_user_asm_byte(__val, __dst, __err, ""); break; \
519 case 2: __put_user_asm_half(__val, __dst, __err, ""); break; \
520 case 4: __put_user_asm_word(__val, __dst, __err, ""); break; \
521 case 8: __put_user_asm_dword(__val, __dst, __err, ""); break; \
522 default: __err = __put_user_bad(); break; \
523 } \
524 if (__err) \
525 goto err_label; \
526} while (0)
Russell King02fcb972006-06-21 14:44:52 +0100527
Russell King9641c7c2006-06-21 20:38:17 +0100528#ifdef CONFIG_MMU
Russell King3fba7e22015-08-19 11:02:28 +0100529extern unsigned long __must_check
530arm_copy_from_user(void *to, const void __user *from, unsigned long n);
531
532static inline unsigned long __must_check
Al Viro4de5b632017-03-21 08:23:33 -0400533raw_copy_from_user(void *to, const void __user *from, unsigned long n)
Russell King3fba7e22015-08-19 11:02:28 +0100534{
Kees Cookdfd45b62016-06-23 15:06:53 -0700535 unsigned int __ua_flags;
536
Kees Cookdfd45b62016-06-23 15:06:53 -0700537 __ua_flags = uaccess_save_and_enable();
Russell King3fba7e22015-08-19 11:02:28 +0100538 n = arm_copy_from_user(to, from, n);
539 uaccess_restore(__ua_flags);
540 return n;
541}
542
543extern unsigned long __must_check
544arm_copy_to_user(void __user *to, const void *from, unsigned long n);
545extern unsigned long __must_check
546__copy_to_user_std(void __user *to, const void *from, unsigned long n);
547
548static inline unsigned long __must_check
Al Viro4de5b632017-03-21 08:23:33 -0400549raw_copy_to_user(void __user *to, const void *from, unsigned long n)
Russell King3fba7e22015-08-19 11:02:28 +0100550{
Russell Kingc014953d2015-12-05 13:42:07 +0000551#ifndef CONFIG_UACCESS_WITH_MEMCPY
Kees Cookdfd45b62016-06-23 15:06:53 -0700552 unsigned int __ua_flags;
Kees Cookdfd45b62016-06-23 15:06:53 -0700553 __ua_flags = uaccess_save_and_enable();
Russell King3fba7e22015-08-19 11:02:28 +0100554 n = arm_copy_to_user(to, from, n);
555 uaccess_restore(__ua_flags);
556 return n;
Russell Kingc014953d2015-12-05 13:42:07 +0000557#else
558 return arm_copy_to_user(to, from, n);
559#endif
Russell King3fba7e22015-08-19 11:02:28 +0100560}
561
562extern unsigned long __must_check
563arm_clear_user(void __user *addr, unsigned long n);
564extern unsigned long __must_check
565__clear_user_std(void __user *addr, unsigned long n);
566
567static inline unsigned long __must_check
568__clear_user(void __user *addr, unsigned long n)
569{
570 unsigned int __ua_flags = uaccess_save_and_enable();
571 n = arm_clear_user(addr, n);
572 uaccess_restore(__ua_flags);
573 return n;
574}
575
Russell King9641c7c2006-06-21 20:38:17 +0100576#else
Al Viro4de5b632017-03-21 08:23:33 -0400577static inline unsigned long
578raw_copy_from_user(void *to, const void __user *from, unsigned long n)
579{
580 memcpy(to, (const void __force *)from, n);
581 return 0;
582}
583static inline unsigned long
584raw_copy_to_user(void __user *to, const void *from, unsigned long n)
585{
586 memcpy((void __force *)to, from, n);
587 return 0;
588}
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200589#define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
Russell King9641c7c2006-06-21 20:38:17 +0100590#endif
Al Viro4de5b632017-03-21 08:23:33 -0400591#define INLINE_COPY_TO_USER
592#define INLINE_COPY_FROM_USER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Russell King99573292006-10-26 10:27:42 +0100594static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Linus Torvalds96d4f262019-01-03 18:57:57 -0800596 if (access_ok(to, n))
Russell King02fcb972006-06-21 14:44:52 +0100597 n = __clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return n;
599}
600
Russell King3fba7e22015-08-19 11:02:28 +0100601/* These are from lib/ code, and use __get_user() and friends */
Will Deacon8c56cc82012-07-06 15:45:39 +0100602extern long strncpy_from_user(char *dest, const char __user *src, long count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Will Deacon8c56cc82012-07-06 15:45:39 +0100604extern __must_check long strnlen_user(const char __user *str, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606#endif /* _ASMARM_UACCESS_H */