blob: b46caab453a5aa53d779bf36c2ca94be1e1af9fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01008 * Copyright (C) 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#ifndef _ASM_UACCESS_H
11#define _ASM_UACCESS_H
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
21 *
22 * For historical reasons, these macros are grossly misnamed.
23 */
Ralf Baechle875d43e2005-09-03 15:56:16 -070024#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Sanjay Lal9843b032012-11-21 18:34:03 -080026#ifdef CONFIG_KVM_GUEST
27#define __UA_LIMIT 0x40000000UL
28#else
29#define __UA_LIMIT 0x80000000UL
30#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define __UA_ADDR ".word"
33#define __UA_LA "la"
34#define __UA_ADDU "addu"
35#define __UA_t0 "$8"
36#define __UA_t1 "$9"
37
Ralf Baechle875d43e2005-09-03 15:56:16 -070038#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ralf Baechle875d43e2005-09-03 15:56:16 -070040#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Daney949e51b2010-10-14 11:32:33 -070042extern u64 __ua_limit;
43
44#define __UA_LIMIT __ua_limit
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define __UA_ADDR ".dword"
47#define __UA_LA "dla"
48#define __UA_ADDU "daddu"
49#define __UA_t0 "$12"
50#define __UA_t1 "$13"
51
Ralf Baechle875d43e2005-09-03 15:56:16 -070052#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * USER_DS is a bitmask that has the bits set that may not be set in a valid
56 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
57 * the arithmetic we're doing only works if the limit is a power of two, so
58 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
59 * address in this range it's the process's problem, not ours :-)
60 */
61
Sanjay Lal9843b032012-11-21 18:34:03 -080062#ifdef CONFIG_KVM_GUEST
63#define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
64#define USER_DS ((mm_segment_t) { 0xC0000000UL })
65#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define KERNEL_DS ((mm_segment_t) { 0UL })
67#define USER_DS ((mm_segment_t) { __UA_LIMIT })
Sanjay Lal9843b032012-11-21 18:34:03 -080068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define VERIFY_READ 0
71#define VERIFY_WRITE 1
72
73#define get_ds() (KERNEL_DS)
74#define get_fs() (current_thread_info()->addr_limit)
75#define set_fs(x) (current_thread_info()->addr_limit = (x))
76
Ralf Baechle21a151d2007-10-11 23:46:15 +010077#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79
80/*
81 * Is a address valid? This does a straighforward calculation rather
82 * than tests.
83 *
84 * Address valid if:
85 * - "addr" doesn't have any high-bits set
86 * - AND "size" doesn't have any high-bits set
87 * - AND "addr+size" doesn't have any high-bits set
88 * - OR we are in kernel mode.
89 *
90 * __ua_size() is a trick to avoid runtime checking of positive constant
91 * sizes; for those we already know at compile time that the size is ok.
92 */
93#define __ua_size(size) \
94 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
95
96/*
97 * access_ok: - Checks if a user space pointer is valid
98 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
Ralf Baechle70342282013-01-22 12:59:30 +010099 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
100 * to write to a block, it is always safe to read from it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * @addr: User space pointer to start of block to check
102 * @size: Size of block to check
103 *
Ralf Baechle70342282013-01-22 12:59:30 +0100104 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * Checks if a pointer to a block of memory in user space is valid.
107 *
108 * Returns true (nonzero) if the memory block may be valid, false (zero)
109 * if it is definitely invalid.
110 *
111 * Note that, depending on architecture, this function probably just
112 * checks that the pointer is in the user space range - after calling
113 * this function, memory access functions may still return -EFAULT.
114 */
115
116#define __access_mask get_fs().seg
117
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200118#define __access_ok(addr, size, mask) \
119({ \
120 unsigned long __addr = (unsigned long) (addr); \
121 unsigned long __size = size; \
122 unsigned long __mask = mask; \
123 unsigned long __ok; \
124 \
125 __chk_user_ptr(addr); \
126 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
127 __ua_size(__size))); \
128 __ok == 0; \
Ralf Baechled0aab922009-04-27 15:31:34 +0200129})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define access_ok(type, addr, size) \
Ralf Baechled0aab922009-04-27 15:31:34 +0200132 likely(__access_ok((addr), (size), __access_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * put_user: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100136 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * @ptr: Destination address, in user space.
138 *
Ralf Baechle70342282013-01-22 12:59:30 +0100139 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * This macro copies a single simple value from kernel space to user
142 * space. It supports simple types like char and int, but not larger
143 * data types like structures or arrays.
144 *
145 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
146 * to the result of dereferencing @ptr.
147 *
148 * Returns zero on success, or -EFAULT on error.
149 */
Ralf Baechle70342282013-01-22 12:59:30 +0100150#define put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100151 __put_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * get_user: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100155 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * @ptr: Source address, in user space.
157 *
Ralf Baechle70342282013-01-22 12:59:30 +0100158 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * This macro copies a single simple variable from user space to kernel
161 * space. It supports simple types like char and int, but not larger
162 * data types like structures or arrays.
163 *
164 * @ptr must have pointer-to-simple-variable type, and the result of
165 * dereferencing @ptr must be assignable to @x without a cast.
166 *
167 * Returns zero on success, or -EFAULT on error.
168 * On error, the variable @x is set to zero.
169 */
170#define get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100171 __get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/*
174 * __put_user: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100175 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * @ptr: Destination address, in user space.
177 *
Ralf Baechle70342282013-01-22 12:59:30 +0100178 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
180 * This macro copies a single simple value from kernel space to user
181 * space. It supports simple types like char and int, but not larger
182 * data types like structures or arrays.
183 *
184 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
185 * to the result of dereferencing @ptr.
186 *
187 * Caller must check the pointer with access_ok() before calling this
188 * function.
189 *
190 * Returns zero on success, or -EFAULT on error.
191 */
192#define __put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100193 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * __get_user: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100197 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * @ptr: Source address, in user space.
199 *
Ralf Baechle70342282013-01-22 12:59:30 +0100200 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * This macro copies a single simple variable from user space to kernel
203 * space. It supports simple types like char and int, but not larger
204 * data types like structures or arrays.
205 *
206 * @ptr must have pointer-to-simple-variable type, and the result of
207 * dereferencing @ptr must be assignable to @x without a cast.
208 *
209 * Caller must check the pointer with access_ok() before calling this
210 * function.
211 *
212 * Returns zero on success, or -EFAULT on error.
213 * On error, the variable @x is set to zero.
214 */
215#define __get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100216 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000219#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
222 * Yuck. We need two variants, one for 64bit operation and one
223 * for 32 bit mode and old iron.
224 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000225#ifdef CONFIG_32BIT
226#define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000228#ifdef CONFIG_64BIT
229#define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
230#endif
231
232extern void __get_user_unknown(void);
233
234#define __get_user_common(val, size, ptr) \
235do { \
236 switch (size) { \
237 case 1: __get_user_asm(val, "lb", ptr); break; \
238 case 2: __get_user_asm(val, "lh", ptr); break; \
239 case 4: __get_user_asm(val, "lw", ptr); break; \
240 case 8: __GET_USER_DW(val, ptr); break; \
241 default: __get_user_unknown(); break; \
242 } \
243} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Ralf Baechle21a151d2007-10-11 23:46:15 +0100245#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100247 int __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200249 __chk_user_ptr(ptr); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000250 __get_user_common((x), size, ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 __gu_err; \
252})
253
Ralf Baechle21a151d2007-10-11 23:46:15 +0100254#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100256 int __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900257 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200259 might_fault(); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000260 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
261 __get_user_common((x), size, __gu_ptr); \
262 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 __gu_err; \
264})
265
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000266#define __get_user_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000267{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000268 long __gu_tmp; \
269 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 __asm__ __volatile__( \
271 "1: " insn " %1, %3 \n" \
272 "2: \n" \
273 " .section .fixup,\"ax\" \n" \
274 "3: li %0, %4 \n" \
275 " j 2b \n" \
276 " .previous \n" \
277 " .section __ex_table,\"a\" \n" \
278 " "__UA_ADDR "\t1b, 3b \n" \
279 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000280 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000281 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000282 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900283 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*
287 * Get a long long 64 using 32 bit registers.
288 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000289#define __get_user_asm_ll32(val, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000290{ \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000291 union { \
292 unsigned long long l; \
293 __typeof__(*(addr)) t; \
294 } __gu_tmp; \
Ralf Baechlecd1fb9e2007-02-12 23:12:38 +0000295 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000297 "1: lw %1, (%3) \n" \
298 "2: lw %D1, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 "3: .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000300 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 " move %1, $0 \n" \
302 " move %D1, $0 \n" \
303 " j 3b \n" \
304 " .previous \n" \
305 " .section __ex_table,\"a\" \n" \
306 " " __UA_ADDR " 1b, 4b \n" \
307 " " __UA_ADDR " 2b, 4b \n" \
308 " .previous \n" \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000309 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000310 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000311 \
312 (val) = __gu_tmp.t; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * Yuck. We need two variants, one for 64bit operation and one
317 * for 32 bit mode and old iron.
318 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000319#ifdef CONFIG_32BIT
Ralf Baechlefe00f942005-03-01 19:22:29 +0000320#define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000322#ifdef CONFIG_64BIT
323#define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Ralf Baechle21a151d2007-10-11 23:46:15 +0100326#define __put_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327({ \
328 __typeof__(*(ptr)) __pu_val; \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100329 int __pu_err = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200331 __chk_user_ptr(ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 __pu_val = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000334 case 1: __put_user_asm("sb", ptr); break; \
335 case 2: __put_user_asm("sh", ptr); break; \
336 case 4: __put_user_asm("sw", ptr); break; \
337 case 8: __PUT_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 default: __put_user_unknown(); break; \
339 } \
340 __pu_err; \
341})
342
Ralf Baechle21a151d2007-10-11 23:46:15 +0100343#define __put_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000345 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
346 __typeof__(*(ptr)) __pu_val = (x); \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100347 int __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200349 might_fault(); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000352 case 1: __put_user_asm("sb", __pu_addr); break; \
353 case 2: __put_user_asm("sh", __pu_addr); break; \
354 case 4: __put_user_asm("sw", __pu_addr); break; \
355 case 8: __PUT_USER_DW(__pu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 default: __put_user_unknown(); break; \
357 } \
358 } \
359 __pu_err; \
360})
361
Ralf Baechlefe00f942005-03-01 19:22:29 +0000362#define __put_user_asm(insn, ptr) \
363{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 __asm__ __volatile__( \
365 "1: " insn " %z2, %3 # __put_user_asm\n" \
366 "2: \n" \
367 " .section .fixup,\"ax\" \n" \
368 "3: li %0, %4 \n" \
369 " j 2b \n" \
370 " .previous \n" \
371 " .section __ex_table,\"a\" \n" \
372 " " __UA_ADDR " 1b, 3b \n" \
373 " .previous \n" \
374 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000375 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ralf Baechlefe00f942005-03-01 19:22:29 +0000379#define __put_user_asm_ll32(ptr) \
380{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000382 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
383 "2: sw %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "3: \n" \
385 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000386 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 " j 3b \n" \
388 " .previous \n" \
389 " .section __ex_table,\"a\" \n" \
390 " " __UA_ADDR " 1b, 4b \n" \
391 " " __UA_ADDR " 2b, 4b \n" \
392 " .previous" \
393 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000394 : "0" (0), "r" (__pu_val), "r" (ptr), \
395 "i" (-EFAULT)); \
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398extern void __put_user_unknown(void);
399
400/*
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000401 * put_user_unaligned: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100402 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000403 * @ptr: Destination address, in user space.
404 *
Ralf Baechle70342282013-01-22 12:59:30 +0100405 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000406 *
407 * This macro copies a single simple value from kernel space to user
408 * space. It supports simple types like char and int, but not larger
409 * data types like structures or arrays.
410 *
411 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
412 * to the result of dereferencing @ptr.
413 *
414 * Returns zero on success, or -EFAULT on error.
415 */
416#define put_user_unaligned(x,ptr) \
417 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
418
419/*
420 * get_user_unaligned: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100421 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000422 * @ptr: Source address, in user space.
423 *
Ralf Baechle70342282013-01-22 12:59:30 +0100424 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000425 *
426 * This macro copies a single simple variable from user space to kernel
427 * space. It supports simple types like char and int, but not larger
428 * data types like structures or arrays.
429 *
430 * @ptr must have pointer-to-simple-variable type, and the result of
431 * dereferencing @ptr must be assignable to @x without a cast.
432 *
433 * Returns zero on success, or -EFAULT on error.
434 * On error, the variable @x is set to zero.
435 */
436#define get_user_unaligned(x,ptr) \
437 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
438
439/*
440 * __put_user_unaligned: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100441 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000442 * @ptr: Destination address, in user space.
443 *
Ralf Baechle70342282013-01-22 12:59:30 +0100444 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000445 *
446 * This macro copies a single simple value from kernel space to user
447 * space. It supports simple types like char and int, but not larger
448 * data types like structures or arrays.
449 *
450 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
451 * to the result of dereferencing @ptr.
452 *
453 * Caller must check the pointer with access_ok() before calling this
454 * function.
455 *
456 * Returns zero on success, or -EFAULT on error.
457 */
458#define __put_user_unaligned(x,ptr) \
459 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
460
461/*
462 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100463 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000464 * @ptr: Source address, in user space.
465 *
Ralf Baechle70342282013-01-22 12:59:30 +0100466 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000467 *
468 * This macro copies a single simple variable from user space to kernel
469 * space. It supports simple types like char and int, but not larger
470 * data types like structures or arrays.
471 *
472 * @ptr must have pointer-to-simple-variable type, and the result of
473 * dereferencing @ptr must be assignable to @x without a cast.
474 *
475 * Caller must check the pointer with access_ok() before calling this
476 * function.
477 *
478 * Returns zero on success, or -EFAULT on error.
479 * On error, the variable @x is set to zero.
480 */
481#define __get_user_unaligned(x,ptr) \
482 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
483
484/*
485 * Yuck. We need two variants, one for 64bit operation and one
486 * for 32 bit mode and old iron.
487 */
488#ifdef CONFIG_32BIT
489#define __GET_USER_UNALIGNED_DW(val, ptr) \
490 __get_user_unaligned_asm_ll32(val, ptr)
491#endif
492#ifdef CONFIG_64BIT
493#define __GET_USER_UNALIGNED_DW(val, ptr) \
494 __get_user_unaligned_asm(val, "uld", ptr)
495#endif
496
497extern void __get_user_unaligned_unknown(void);
498
499#define __get_user_unaligned_common(val, size, ptr) \
500do { \
501 switch (size) { \
502 case 1: __get_user_asm(val, "lb", ptr); break; \
503 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
504 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
505 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
506 default: __get_user_unaligned_unknown(); break; \
507 } \
508} while (0)
509
510#define __get_user_unaligned_nocheck(x,ptr,size) \
511({ \
512 int __gu_err; \
513 \
514 __get_user_unaligned_common((x), size, ptr); \
515 __gu_err; \
516})
517
518#define __get_user_unaligned_check(x,ptr,size) \
519({ \
520 int __gu_err = -EFAULT; \
521 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
522 \
523 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
524 __get_user_unaligned_common((x), size, __gu_ptr); \
525 \
526 __gu_err; \
527})
528
529#define __get_user_unaligned_asm(val, insn, addr) \
530{ \
531 long __gu_tmp; \
532 \
533 __asm__ __volatile__( \
534 "1: " insn " %1, %3 \n" \
535 "2: \n" \
536 " .section .fixup,\"ax\" \n" \
537 "3: li %0, %4 \n" \
538 " j 2b \n" \
539 " .previous \n" \
540 " .section __ex_table,\"a\" \n" \
541 " "__UA_ADDR "\t1b, 3b \n" \
542 " "__UA_ADDR "\t1b + 4, 3b \n" \
543 " .previous \n" \
544 : "=r" (__gu_err), "=r" (__gu_tmp) \
545 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
546 \
547 (val) = (__typeof__(*(addr))) __gu_tmp; \
548}
549
550/*
551 * Get a long long 64 using 32 bit registers.
552 */
553#define __get_user_unaligned_asm_ll32(val, addr) \
554{ \
Ralf Baechle70342282013-01-22 12:59:30 +0100555 unsigned long long __gu_tmp; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000556 \
557 __asm__ __volatile__( \
558 "1: ulw %1, (%3) \n" \
559 "2: ulw %D1, 4(%3) \n" \
560 " move %0, $0 \n" \
561 "3: .section .fixup,\"ax\" \n" \
562 "4: li %0, %4 \n" \
563 " move %1, $0 \n" \
564 " move %D1, $0 \n" \
565 " j 3b \n" \
566 " .previous \n" \
567 " .section __ex_table,\"a\" \n" \
568 " " __UA_ADDR " 1b, 4b \n" \
569 " " __UA_ADDR " 1b + 4, 4b \n" \
570 " " __UA_ADDR " 2b, 4b \n" \
571 " " __UA_ADDR " 2b + 4, 4b \n" \
572 " .previous \n" \
573 : "=r" (__gu_err), "=&r" (__gu_tmp) \
574 : "0" (0), "r" (addr), "i" (-EFAULT)); \
575 (val) = (__typeof__(*(addr))) __gu_tmp; \
576}
577
578/*
579 * Yuck. We need two variants, one for 64bit operation and one
580 * for 32 bit mode and old iron.
581 */
582#ifdef CONFIG_32BIT
583#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
584#endif
585#ifdef CONFIG_64BIT
586#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
587#endif
588
589#define __put_user_unaligned_nocheck(x,ptr,size) \
590({ \
591 __typeof__(*(ptr)) __pu_val; \
592 int __pu_err = 0; \
593 \
594 __pu_val = (x); \
595 switch (size) { \
596 case 1: __put_user_asm("sb", ptr); break; \
597 case 2: __put_user_unaligned_asm("ush", ptr); break; \
598 case 4: __put_user_unaligned_asm("usw", ptr); break; \
599 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
600 default: __put_user_unaligned_unknown(); break; \
601 } \
602 __pu_err; \
603})
604
605#define __put_user_unaligned_check(x,ptr,size) \
606({ \
607 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
608 __typeof__(*(ptr)) __pu_val = (x); \
609 int __pu_err = -EFAULT; \
610 \
611 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
612 switch (size) { \
613 case 1: __put_user_asm("sb", __pu_addr); break; \
614 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
615 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
616 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
617 default: __put_user_unaligned_unknown(); break; \
618 } \
619 } \
620 __pu_err; \
621})
622
623#define __put_user_unaligned_asm(insn, ptr) \
624{ \
625 __asm__ __volatile__( \
626 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
627 "2: \n" \
628 " .section .fixup,\"ax\" \n" \
629 "3: li %0, %4 \n" \
630 " j 2b \n" \
631 " .previous \n" \
632 " .section __ex_table,\"a\" \n" \
633 " " __UA_ADDR " 1b, 3b \n" \
634 " .previous \n" \
635 : "=r" (__pu_err) \
636 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
637 "i" (-EFAULT)); \
638}
639
640#define __put_user_unaligned_asm_ll32(ptr) \
641{ \
642 __asm__ __volatile__( \
Ralf Baechle70342282013-01-22 12:59:30 +0100643 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000644 "2: sw %D2, 4(%3) \n" \
645 "3: \n" \
646 " .section .fixup,\"ax\" \n" \
647 "4: li %0, %4 \n" \
648 " j 3b \n" \
649 " .previous \n" \
650 " .section __ex_table,\"a\" \n" \
651 " " __UA_ADDR " 1b, 4b \n" \
652 " " __UA_ADDR " 1b + 4, 4b \n" \
653 " " __UA_ADDR " 2b, 4b \n" \
654 " " __UA_ADDR " 2b + 4, 4b \n" \
655 " .previous" \
656 : "=r" (__pu_err) \
657 : "0" (0), "r" (__pu_val), "r" (ptr), \
658 "i" (-EFAULT)); \
659}
660
661extern void __put_user_unaligned_unknown(void);
662
663/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * We're generating jump to subroutines which will be outside the range of
665 * jump instructions
666 */
667#ifdef MODULE
668#define __MODULE_JAL(destination) \
669 ".set\tnoat\n\t" \
Ralf Baechle70342282013-01-22 12:59:30 +0100670 __UA_LA "\t$1, " #destination "\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 "jalr\t$1\n\t" \
672 ".set\tat\n\t"
673#else
674#define __MODULE_JAL(destination) \
675 "jal\t" #destination "\n\t"
676#endif
677
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100678#ifndef CONFIG_CPU_DADDI_WORKAROUNDS
679#define DADDI_SCRATCH "$0"
680#else
681#define DADDI_SCRATCH "$3"
682#endif
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684extern size_t __copy_user(void *__to, const void *__from, size_t __n);
685
Ralf Baechle21a151d2007-10-11 23:46:15 +0100686#define __invoke_copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100688 register void __user *__cu_to_r __asm__("$4"); \
689 register const void *__cu_from_r __asm__("$5"); \
690 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 \
692 __cu_to_r = (to); \
693 __cu_from_r = (from); \
694 __cu_len_r = (n); \
695 __asm__ __volatile__( \
696 __MODULE_JAL(__copy_user) \
697 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
698 : \
David Daneybb0757e2012-06-06 23:00:31 +0100699 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100700 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 __cu_len_r; \
702})
703
704/*
705 * __copy_to_user: - Copy a block of data into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100706 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100708 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
Ralf Baechle70342282013-01-22 12:59:30 +0100710 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
712 * Copy data from kernel space to user space. Caller must check
713 * the specified block with access_ok() before calling this function.
714 *
715 * Returns number of bytes that could not be copied.
716 * On success, this will be zero.
717 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100718#define __copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000720 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 const void *__cu_from; \
722 long __cu_len; \
723 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 __cu_to = (to); \
725 __cu_from = (from); \
726 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200727 might_fault(); \
Ralf Baechle70342282013-01-22 12:59:30 +0100728 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 __cu_len; \
730})
731
Ralf Baechled0c91ae2007-03-05 15:54:20 +0000732extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
733
Ralf Baechle21a151d2007-10-11 23:46:15 +0100734#define __copy_to_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000735({ \
736 void __user *__cu_to; \
737 const void *__cu_from; \
738 long __cu_len; \
739 \
740 __cu_to = (to); \
741 __cu_from = (from); \
742 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100743 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000744 __cu_len; \
745})
746
Ralf Baechle21a151d2007-10-11 23:46:15 +0100747#define __copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000748({ \
749 void *__cu_to; \
750 const void __user *__cu_from; \
751 long __cu_len; \
752 \
753 __cu_to = (to); \
754 __cu_from = (from); \
755 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100756 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
757 __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000758 __cu_len; \
759})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761/*
762 * copy_to_user: - Copy a block of data into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100763 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100765 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *
Ralf Baechle70342282013-01-22 12:59:30 +0100767 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
769 * Copy data from kernel space to user space.
770 *
771 * Returns number of bytes that could not be copied.
772 * On success, this will be zero.
773 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100774#define copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000776 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 const void *__cu_from; \
778 long __cu_len; \
779 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 __cu_to = (to); \
781 __cu_from = (from); \
782 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200783 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
784 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100786 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200787 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 __cu_len; \
789})
790
Ralf Baechle21a151d2007-10-11 23:46:15 +0100791#define __invoke_copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100793 register void *__cu_to_r __asm__("$4"); \
794 register const void __user *__cu_from_r __asm__("$5"); \
795 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 \
797 __cu_to_r = (to); \
798 __cu_from_r = (from); \
799 __cu_len_r = (n); \
800 __asm__ __volatile__( \
801 ".set\tnoreorder\n\t" \
802 __MODULE_JAL(__copy_user) \
803 ".set\tnoat\n\t" \
804 __UA_ADDU "\t$1, %1, %2\n\t" \
805 ".set\tat\n\t" \
806 ".set\treorder" \
807 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
808 : \
David Daneybb0757e2012-06-06 23:00:31 +0100809 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100810 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 __cu_len_r; \
812})
813
Ralf Baechle21a151d2007-10-11 23:46:15 +0100814#define __invoke_copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000815({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100816 register void *__cu_to_r __asm__("$4"); \
817 register const void __user *__cu_from_r __asm__("$5"); \
818 register long __cu_len_r __asm__("$6"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000819 \
820 __cu_to_r = (to); \
821 __cu_from_r = (from); \
822 __cu_len_r = (n); \
823 __asm__ __volatile__( \
824 ".set\tnoreorder\n\t" \
825 __MODULE_JAL(__copy_user_inatomic) \
826 ".set\tnoat\n\t" \
827 __UA_ADDU "\t$1, %1, %2\n\t" \
828 ".set\tat\n\t" \
829 ".set\treorder" \
830 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
831 : \
David Daneybb0757e2012-06-06 23:00:31 +0100832 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100833 DADDI_SCRATCH, "memory"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000834 __cu_len_r; \
835})
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
Chris Dearman131c1a22007-02-01 19:54:13 +0000838 * __copy_from_user: - Copy a block of data from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100839 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100841 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
Ralf Baechle70342282013-01-22 12:59:30 +0100843 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
845 * Copy data from user space to kernel space. Caller must check
846 * the specified block with access_ok() before calling this function.
847 *
848 * Returns number of bytes that could not be copied.
849 * On success, this will be zero.
850 *
851 * If some data could not be copied, this function will pad the copied
852 * data to the requested size using zero bytes.
853 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100854#define __copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855({ \
856 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000857 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 long __cu_len; \
859 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 __cu_to = (to); \
861 __cu_from = (from); \
862 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200863 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100865 __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 __cu_len; \
867})
868
869/*
870 * copy_from_user: - Copy a block of data from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100871 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100873 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 *
Ralf Baechle70342282013-01-22 12:59:30 +0100875 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
877 * Copy data from user space to kernel space.
878 *
879 * Returns number of bytes that could not be copied.
880 * On success, this will be zero.
881 *
882 * If some data could not be copied, this function will pad the copied
883 * data to the requested size using zero bytes.
884 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100885#define copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886({ \
887 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000888 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 long __cu_len; \
890 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 __cu_to = (to); \
892 __cu_from = (from); \
893 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200894 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
895 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100897 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200898 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 __cu_len; \
900})
901
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200902#define __copy_in_user(to, from, n) \
903({ \
904 void __user *__cu_to; \
905 const void __user *__cu_from; \
906 long __cu_len; \
907 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200908 __cu_to = (to); \
909 __cu_from = (from); \
910 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200911 might_fault(); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200912 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100913 __cu_len); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200914 __cu_len; \
915})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Ralf Baechle21a151d2007-10-11 23:46:15 +0100917#define copy_in_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000919 void __user *__cu_to; \
920 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 long __cu_len; \
922 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 __cu_to = (to); \
924 __cu_from = (from); \
925 __cu_len = (n); \
926 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
Ralf Baechle70342282013-01-22 12:59:30 +0100927 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
Ralf Baechleef41f462009-04-28 14:17:54 +0200928 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100930 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200931 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 __cu_len; \
933})
934
935/*
936 * __clear_user: - Zero a block of memory in user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100937 * @to: Destination address, in user space.
938 * @n: Number of bytes to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
940 * Zero a block of memory in user space. Caller must check
941 * the specified block with access_ok() before calling this function.
942 *
943 * Returns number of bytes that could not be cleared.
944 * On success, this will be zero.
945 */
946static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000947__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 __kernel_size_t res;
950
Ralf Baechleef41f462009-04-28 14:17:54 +0200951 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 __asm__ __volatile__(
953 "move\t$4, %1\n\t"
954 "move\t$5, $0\n\t"
955 "move\t$6, %2\n\t"
956 __MODULE_JAL(__bzero)
957 "move\t%0, $6"
958 : "=r" (res)
959 : "r" (addr), "r" (size)
960 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
961
962 return res;
963}
964
965#define clear_user(addr,n) \
966({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000967 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unsigned long __cl_size = (n); \
969 if (__cl_size && access_ok(VERIFY_WRITE, \
Wu Zhangjin63d38922009-05-21 05:50:01 +0800970 __cl_addr, __cl_size)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 __cl_size = __clear_user(__cl_addr, __cl_size); \
972 __cl_size; \
973})
974
975/*
976 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
977 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +0100978 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * @src: Source address, in user space.
980 * @count: Maximum number of bytes to copy, including the trailing NUL.
981 *
982 * Copies a NUL-terminated string from userspace to kernel space.
983 * Caller must check the specified block with access_ok() before calling
984 * this function.
985 *
986 * On success, returns the length of the string (not including the trailing
987 * NUL).
988 *
989 * If access to userspace fails, returns -EFAULT (some data may have been
990 * copied).
991 *
992 * If @count is smaller than the length of the string, copies @count bytes
993 * and returns @count.
994 */
995static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000996__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 long res;
999
Ralf Baechleef41f462009-04-28 14:17:54 +02001000 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 __asm__ __volatile__(
1002 "move\t$4, %1\n\t"
1003 "move\t$5, %2\n\t"
1004 "move\t$6, %3\n\t"
1005 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1006 "move\t%0, $2"
1007 : "=r" (res)
1008 : "r" (__to), "r" (__from), "r" (__len)
1009 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1010
1011 return res;
1012}
1013
1014/*
1015 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1016 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +01001017 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * @src: Source address, in user space.
1019 * @count: Maximum number of bytes to copy, including the trailing NUL.
1020 *
1021 * Copies a NUL-terminated string from userspace to kernel space.
1022 *
1023 * On success, returns the length of the string (not including the trailing
1024 * NUL).
1025 *
1026 * If access to userspace fails, returns -EFAULT (some data may have been
1027 * copied).
1028 *
1029 * If @count is smaller than the length of the string, copies @count bytes
1030 * and returns @count.
1031 */
1032static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001033strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 long res;
1036
Ralf Baechleef41f462009-04-28 14:17:54 +02001037 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 __asm__ __volatile__(
1039 "move\t$4, %1\n\t"
1040 "move\t$5, %2\n\t"
1041 "move\t$6, %3\n\t"
1042 __MODULE_JAL(__strncpy_from_user_asm)
1043 "move\t%0, $2"
1044 : "=r" (res)
1045 : "r" (__to), "r" (__from), "r" (__len)
1046 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1047
1048 return res;
1049}
1050
1051/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001052static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 long res;
1055
Ralf Baechleef41f462009-04-28 14:17:54 +02001056 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 __asm__ __volatile__(
1058 "move\t$4, %1\n\t"
1059 __MODULE_JAL(__strlen_user_nocheck_asm)
1060 "move\t%0, $2"
1061 : "=r" (res)
1062 : "r" (s)
1063 : "$2", "$4", __UA_t0, "$31");
1064
1065 return res;
1066}
1067
1068/*
1069 * strlen_user: - Get the size of a string in user space.
1070 * @str: The string to measure.
1071 *
Ralf Baechle70342282013-01-22 12:59:30 +01001072 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
1074 * Get the size of a NUL-terminated string in user space.
1075 *
1076 * Returns the size of the string INCLUDING the terminating NUL.
1077 * On exception, returns 0.
1078 *
1079 * If there is a limit on the length of a valid string, you may wish to
1080 * consider using strnlen_user() instead.
1081 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001082static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 long res;
1085
Ralf Baechleef41f462009-04-28 14:17:54 +02001086 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 __asm__ __volatile__(
1088 "move\t$4, %1\n\t"
1089 __MODULE_JAL(__strlen_user_asm)
1090 "move\t%0, $2"
1091 : "=r" (res)
1092 : "r" (s)
1093 : "$2", "$4", __UA_t0, "$31");
1094
1095 return res;
1096}
1097
1098/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001099static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 long res;
1102
Ralf Baechleef41f462009-04-28 14:17:54 +02001103 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 __asm__ __volatile__(
1105 "move\t$4, %1\n\t"
1106 "move\t$5, %2\n\t"
1107 __MODULE_JAL(__strnlen_user_nocheck_asm)
1108 "move\t%0, $2"
1109 : "=r" (res)
1110 : "r" (s), "r" (n)
1111 : "$2", "$4", "$5", __UA_t0, "$31");
1112
1113 return res;
1114}
1115
1116/*
1117 * strlen_user: - Get the size of a string in user space.
1118 * @str: The string to measure.
1119 *
Ralf Baechle70342282013-01-22 12:59:30 +01001120 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 *
1122 * Get the size of a NUL-terminated string in user space.
1123 *
1124 * Returns the size of the string INCLUDING the terminating NUL.
1125 * On exception, returns 0.
1126 *
1127 * If there is a limit on the length of a valid string, you may wish to
1128 * consider using strnlen_user() instead.
1129 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001130static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 long res;
1133
Ralf Baechleef41f462009-04-28 14:17:54 +02001134 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 __asm__ __volatile__(
1136 "move\t$4, %1\n\t"
1137 "move\t$5, %2\n\t"
1138 __MODULE_JAL(__strnlen_user_asm)
1139 "move\t%0, $2"
1140 : "=r" (res)
1141 : "r" (s), "r" (n)
1142 : "$2", "$4", "$5", __UA_t0, "$31");
1143
1144 return res;
1145}
1146
1147struct exception_table_entry
1148{
1149 unsigned long insn;
1150 unsigned long nextinsn;
1151};
1152
1153extern int fixup_exception(struct pt_regs *regs);
1154
1155#endif /* _ASM_UACCESS_H */