blob: c62c20e7b5c669dc29dd7273c7bb771e7dc12f14 [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.
8 */
9#ifndef _ASM_UACCESS_H
10#define _ASM_UACCESS_H
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/thread_info.h>
15#include <asm-generic/uaccess.h>
16
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
26#define __UA_LIMIT 0x80000000UL
27
28#define __UA_ADDR ".word"
29#define __UA_LA "la"
30#define __UA_ADDU "addu"
31#define __UA_t0 "$8"
32#define __UA_t1 "$9"
33
Ralf Baechle875d43e2005-09-03 15:56:16 -070034#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ralf Baechle875d43e2005-09-03 15:56:16 -070036#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define __UA_LIMIT (- TASK_SIZE)
39
40#define __UA_ADDR ".dword"
41#define __UA_LA "dla"
42#define __UA_ADDU "daddu"
43#define __UA_t0 "$12"
44#define __UA_t1 "$13"
45
Ralf Baechle875d43e2005-09-03 15:56:16 -070046#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * USER_DS is a bitmask that has the bits set that may not be set in a valid
50 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
51 * the arithmetic we're doing only works if the limit is a power of two, so
52 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
53 * address in this range it's the process's problem, not ours :-)
54 */
55
56#define KERNEL_DS ((mm_segment_t) { 0UL })
57#define USER_DS ((mm_segment_t) { __UA_LIMIT })
58
59#define VERIFY_READ 0
60#define VERIFY_WRITE 1
61
62#define get_ds() (KERNEL_DS)
63#define get_fs() (current_thread_info()->addr_limit)
64#define set_fs(x) (current_thread_info()->addr_limit = (x))
65
66#define segment_eq(a,b) ((a).seg == (b).seg)
67
68
69/*
70 * Is a address valid? This does a straighforward calculation rather
71 * than tests.
72 *
73 * Address valid if:
74 * - "addr" doesn't have any high-bits set
75 * - AND "size" doesn't have any high-bits set
76 * - AND "addr+size" doesn't have any high-bits set
77 * - OR we are in kernel mode.
78 *
79 * __ua_size() is a trick to avoid runtime checking of positive constant
80 * sizes; for those we already know at compile time that the size is ok.
81 */
82#define __ua_size(size) \
83 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
84
85/*
86 * access_ok: - Checks if a user space pointer is valid
87 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
88 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
89 * to write to a block, it is always safe to read from it.
90 * @addr: User space pointer to start of block to check
91 * @size: Size of block to check
92 *
93 * Context: User context only. This function may sleep.
94 *
95 * Checks if a pointer to a block of memory in user space is valid.
96 *
97 * Returns true (nonzero) if the memory block may be valid, false (zero)
98 * if it is definitely invalid.
99 *
100 * Note that, depending on architecture, this function probably just
101 * checks that the pointer is in the user space range - after calling
102 * this function, memory access functions may still return -EFAULT.
103 */
104
105#define __access_mask get_fs().seg
106
107#define __access_ok(addr, size, mask) \
108 (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
109
110#define access_ok(type, addr, size) \
111 likely(__access_ok((unsigned long)(addr), (size),__access_mask))
112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * put_user: - Write a simple value into user space.
115 * @x: Value to copy to user space.
116 * @ptr: Destination address, in user space.
117 *
118 * Context: User context only. This function may sleep.
119 *
120 * This macro copies a single simple value from kernel space to user
121 * space. It supports simple types like char and int, but not larger
122 * data types like structures or arrays.
123 *
124 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
125 * to the result of dereferencing @ptr.
126 *
127 * Returns zero on success, or -EFAULT on error.
128 */
129#define put_user(x,ptr) \
130 __put_user_check((x),(ptr),sizeof(*(ptr)))
131
132/*
133 * get_user: - Get a simple variable from user space.
134 * @x: Variable to store result.
135 * @ptr: Source address, in user space.
136 *
137 * Context: User context only. This function may sleep.
138 *
139 * This macro copies a single simple variable from user space to kernel
140 * space. It supports simple types like char and int, but not larger
141 * data types like structures or arrays.
142 *
143 * @ptr must have pointer-to-simple-variable type, and the result of
144 * dereferencing @ptr must be assignable to @x without a cast.
145 *
146 * Returns zero on success, or -EFAULT on error.
147 * On error, the variable @x is set to zero.
148 */
149#define get_user(x,ptr) \
150 __get_user_check((x),(ptr),sizeof(*(ptr)))
151
152/*
153 * __put_user: - Write a simple value into user space, with less checking.
154 * @x: Value to copy to user space.
155 * @ptr: Destination address, in user space.
156 *
157 * Context: User context only. This function may sleep.
158 *
159 * This macro copies a single simple value from kernel space to user
160 * space. It supports simple types like char and int, but not larger
161 * data types like structures or arrays.
162 *
163 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
164 * to the result of dereferencing @ptr.
165 *
166 * Caller must check the pointer with access_ok() before calling this
167 * function.
168 *
169 * Returns zero on success, or -EFAULT on error.
170 */
171#define __put_user(x,ptr) \
172 __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
173
174/*
175 * __get_user: - Get a simple variable from user space, with less checking.
176 * @x: Variable to store result.
177 * @ptr: Source address, in user space.
178 *
179 * Context: User context only. This function may sleep.
180 *
181 * This macro copies a single simple variable from user space to kernel
182 * space. It supports simple types like char and int, but not larger
183 * data types like structures or arrays.
184 *
185 * @ptr must have pointer-to-simple-variable type, and the result of
186 * dereferencing @ptr must be assignable to @x without a cast.
187 *
188 * Caller must check the pointer with access_ok() before calling this
189 * function.
190 *
191 * Returns zero on success, or -EFAULT on error.
192 * On error, the variable @x is set to zero.
193 */
194#define __get_user(x,ptr) \
195 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
196
197struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000198#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*
201 * Yuck. We need two variants, one for 64bit operation and one
202 * for 32 bit mode and old iron.
203 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000204#ifdef CONFIG_32BIT
205#define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000207#ifdef CONFIG_64BIT
208#define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
209#endif
210
211extern void __get_user_unknown(void);
212
213#define __get_user_common(val, size, ptr) \
214do { \
215 switch (size) { \
216 case 1: __get_user_asm(val, "lb", ptr); break; \
217 case 2: __get_user_asm(val, "lh", ptr); break; \
218 case 4: __get_user_asm(val, "lw", ptr); break; \
219 case 8: __GET_USER_DW(val, ptr); break; \
220 default: __get_user_unknown(); break; \
221 } \
222} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224#define __get_user_nocheck(x,ptr,size) \
225({ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000226 long __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000228 __get_user_common((x), size, ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 __gu_err; \
230})
231
232#define __get_user_check(x,ptr,size) \
233({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000234 long __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900235 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000237 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
238 __get_user_common((x), size, __gu_ptr); \
239 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 __gu_err; \
241})
242
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000243#define __get_user_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000244{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000245 long __gu_tmp; \
246 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 __asm__ __volatile__( \
248 "1: " insn " %1, %3 \n" \
249 "2: \n" \
250 " .section .fixup,\"ax\" \n" \
251 "3: li %0, %4 \n" \
252 " j 2b \n" \
253 " .previous \n" \
254 " .section __ex_table,\"a\" \n" \
255 " "__UA_ADDR "\t1b, 3b \n" \
256 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000257 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000258 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000259 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900260 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/*
264 * Get a long long 64 using 32 bit registers.
265 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000266#define __get_user_asm_ll32(val, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000267{ \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000268 union { \
269 unsigned long long l; \
270 __typeof__(*(addr)) t; \
271 } __gu_tmp; \
Ralf Baechlecd1fb9e2007-02-12 23:12:38 +0000272 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000274 "1: lw %1, (%3) \n" \
275 "2: lw %D1, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "3: .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000277 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 " move %1, $0 \n" \
279 " move %D1, $0 \n" \
280 " j 3b \n" \
281 " .previous \n" \
282 " .section __ex_table,\"a\" \n" \
283 " " __UA_ADDR " 1b, 4b \n" \
284 " " __UA_ADDR " 2b, 4b \n" \
285 " .previous \n" \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000286 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000287 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000288 \
289 (val) = __gu_tmp.t; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * Yuck. We need two variants, one for 64bit operation and one
294 * for 32 bit mode and old iron.
295 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000296#ifdef CONFIG_32BIT
Ralf Baechlefe00f942005-03-01 19:22:29 +0000297#define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000299#ifdef CONFIG_64BIT
300#define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303#define __put_user_nocheck(x,ptr,size) \
304({ \
305 __typeof__(*(ptr)) __pu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 long __pu_err = 0; \
307 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 __pu_val = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000310 case 1: __put_user_asm("sb", ptr); break; \
311 case 2: __put_user_asm("sh", ptr); break; \
312 case 4: __put_user_asm("sw", ptr); break; \
313 case 8: __PUT_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 default: __put_user_unknown(); break; \
315 } \
316 __pu_err; \
317})
318
319#define __put_user_check(x,ptr,size) \
320({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000321 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
322 __typeof__(*(ptr)) __pu_val = (x); \
323 long __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000325 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000327 case 1: __put_user_asm("sb", __pu_addr); break; \
328 case 2: __put_user_asm("sh", __pu_addr); break; \
329 case 4: __put_user_asm("sw", __pu_addr); break; \
330 case 8: __PUT_USER_DW(__pu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 default: __put_user_unknown(); break; \
332 } \
333 } \
334 __pu_err; \
335})
336
Ralf Baechlefe00f942005-03-01 19:22:29 +0000337#define __put_user_asm(insn, ptr) \
338{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 __asm__ __volatile__( \
340 "1: " insn " %z2, %3 # __put_user_asm\n" \
341 "2: \n" \
342 " .section .fixup,\"ax\" \n" \
343 "3: li %0, %4 \n" \
344 " j 2b \n" \
345 " .previous \n" \
346 " .section __ex_table,\"a\" \n" \
347 " " __UA_ADDR " 1b, 3b \n" \
348 " .previous \n" \
349 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Ralf Baechlefe00f942005-03-01 19:22:29 +0000354#define __put_user_asm_ll32(ptr) \
355{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000357 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
358 "2: sw %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 "3: \n" \
360 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000361 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 " j 3b \n" \
363 " .previous \n" \
364 " .section __ex_table,\"a\" \n" \
365 " " __UA_ADDR " 1b, 4b \n" \
366 " " __UA_ADDR " 2b, 4b \n" \
367 " .previous" \
368 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000369 : "0" (0), "r" (__pu_val), "r" (ptr), \
370 "i" (-EFAULT)); \
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373extern void __put_user_unknown(void);
374
375/*
376 * We're generating jump to subroutines which will be outside the range of
377 * jump instructions
378 */
379#ifdef MODULE
380#define __MODULE_JAL(destination) \
381 ".set\tnoat\n\t" \
382 __UA_LA "\t$1, " #destination "\n\t" \
383 "jalr\t$1\n\t" \
384 ".set\tat\n\t"
385#else
386#define __MODULE_JAL(destination) \
387 "jal\t" #destination "\n\t"
388#endif
389
390extern size_t __copy_user(void *__to, const void *__from, size_t __n);
391
392#define __invoke_copy_to_user(to,from,n) \
393({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000394 register void __user *__cu_to_r __asm__ ("$4"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 register const void *__cu_from_r __asm__ ("$5"); \
396 register long __cu_len_r __asm__ ("$6"); \
397 \
398 __cu_to_r = (to); \
399 __cu_from_r = (from); \
400 __cu_len_r = (n); \
401 __asm__ __volatile__( \
402 __MODULE_JAL(__copy_user) \
403 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
404 : \
405 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
406 "memory"); \
407 __cu_len_r; \
408})
409
410/*
411 * __copy_to_user: - Copy a block of data into user space, with less checking.
412 * @to: Destination address, in user space.
413 * @from: Source address, in kernel space.
414 * @n: Number of bytes to copy.
415 *
416 * Context: User context only. This function may sleep.
417 *
418 * Copy data from kernel space to user space. Caller must check
419 * the specified block with access_ok() before calling this function.
420 *
421 * Returns number of bytes that could not be copied.
422 * On success, this will be zero.
423 */
424#define __copy_to_user(to,from,n) \
425({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000426 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 const void *__cu_from; \
428 long __cu_len; \
429 \
430 might_sleep(); \
431 __cu_to = (to); \
432 __cu_from = (from); \
433 __cu_len = (n); \
434 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
435 __cu_len; \
436})
437
Ralf Baechlee03b5262007-02-19 16:59:24 +0000438#define __copy_to_user_inatomic(to,from,n) \
439({ \
440 void __user *__cu_to; \
441 const void *__cu_from; \
442 long __cu_len; \
443 \
444 __cu_to = (to); \
445 __cu_from = (from); \
446 __cu_len = (n); \
447 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
448 __cu_len; \
449})
450
451#define __copy_from_user_inatomic(to,from,n) \
452({ \
453 void *__cu_to; \
454 const void __user *__cu_from; \
455 long __cu_len; \
456 \
457 __cu_to = (to); \
458 __cu_from = (from); \
459 __cu_len = (n); \
460 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
461 __cu_len); \
462 __cu_len; \
463})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/*
466 * copy_to_user: - Copy a block of data into user space.
467 * @to: Destination address, in user space.
468 * @from: Source address, in kernel space.
469 * @n: Number of bytes to copy.
470 *
471 * Context: User context only. This function may sleep.
472 *
473 * Copy data from kernel space to user space.
474 *
475 * Returns number of bytes that could not be copied.
476 * On success, this will be zero.
477 */
478#define copy_to_user(to,from,n) \
479({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000480 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 const void *__cu_from; \
482 long __cu_len; \
483 \
484 might_sleep(); \
485 __cu_to = (to); \
486 __cu_from = (from); \
487 __cu_len = (n); \
488 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
489 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
490 __cu_len); \
491 __cu_len; \
492})
493
494#define __invoke_copy_from_user(to,from,n) \
495({ \
496 register void *__cu_to_r __asm__ ("$4"); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000497 register const void __user *__cu_from_r __asm__ ("$5"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 register long __cu_len_r __asm__ ("$6"); \
499 \
500 __cu_to_r = (to); \
501 __cu_from_r = (from); \
502 __cu_len_r = (n); \
503 __asm__ __volatile__( \
504 ".set\tnoreorder\n\t" \
505 __MODULE_JAL(__copy_user) \
506 ".set\tnoat\n\t" \
507 __UA_ADDU "\t$1, %1, %2\n\t" \
508 ".set\tat\n\t" \
509 ".set\treorder" \
510 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
511 : \
512 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
513 "memory"); \
514 __cu_len_r; \
515})
516
Ralf Baechlee03b5262007-02-19 16:59:24 +0000517#define __invoke_copy_from_user_inatomic(to,from,n) \
518({ \
519 register void *__cu_to_r __asm__ ("$4"); \
520 register const void __user *__cu_from_r __asm__ ("$5"); \
521 register long __cu_len_r __asm__ ("$6"); \
522 \
523 __cu_to_r = (to); \
524 __cu_from_r = (from); \
525 __cu_len_r = (n); \
526 __asm__ __volatile__( \
527 ".set\tnoreorder\n\t" \
528 __MODULE_JAL(__copy_user_inatomic) \
529 ".set\tnoat\n\t" \
530 __UA_ADDU "\t$1, %1, %2\n\t" \
531 ".set\tat\n\t" \
532 ".set\treorder" \
533 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
534 : \
535 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
536 "memory"); \
537 __cu_len_r; \
538})
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
Chris Dearman131c1a22007-02-01 19:54:13 +0000541 * __copy_from_user: - Copy a block of data from user space, with less checking.
542 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * @from: Source address, in user space.
544 * @n: Number of bytes to copy.
545 *
546 * Context: User context only. This function may sleep.
547 *
548 * Copy data from user space to kernel space. Caller must check
549 * the specified block with access_ok() before calling this function.
550 *
551 * Returns number of bytes that could not be copied.
552 * On success, this will be zero.
553 *
554 * If some data could not be copied, this function will pad the copied
555 * data to the requested size using zero bytes.
556 */
557#define __copy_from_user(to,from,n) \
558({ \
559 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000560 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 long __cu_len; \
562 \
563 might_sleep(); \
564 __cu_to = (to); \
565 __cu_from = (from); \
566 __cu_len = (n); \
567 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
568 __cu_len); \
569 __cu_len; \
570})
571
572/*
573 * copy_from_user: - Copy a block of data from user space.
574 * @to: Destination address, in kernel space.
575 * @from: Source address, in user space.
576 * @n: Number of bytes to copy.
577 *
578 * Context: User context only. This function may sleep.
579 *
580 * Copy data from user space to kernel space.
581 *
582 * Returns number of bytes that could not be copied.
583 * On success, this will be zero.
584 *
585 * If some data could not be copied, this function will pad the copied
586 * data to the requested size using zero bytes.
587 */
588#define copy_from_user(to,from,n) \
589({ \
590 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000591 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 long __cu_len; \
593 \
594 might_sleep(); \
595 __cu_to = (to); \
596 __cu_from = (from); \
597 __cu_len = (n); \
598 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
599 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
600 __cu_len); \
601 __cu_len; \
602})
603
604#define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
605
606#define copy_in_user(to,from,n) \
607({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000608 void __user *__cu_to; \
609 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 long __cu_len; \
611 \
612 might_sleep(); \
613 __cu_to = (to); \
614 __cu_from = (from); \
615 __cu_len = (n); \
616 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
617 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
618 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
619 __cu_len); \
620 __cu_len; \
621})
622
623/*
624 * __clear_user: - Zero a block of memory in user space, with less checking.
625 * @to: Destination address, in user space.
626 * @n: Number of bytes to zero.
627 *
628 * Zero a block of memory in user space. Caller must check
629 * the specified block with access_ok() before calling this function.
630 *
631 * Returns number of bytes that could not be cleared.
632 * On success, this will be zero.
633 */
634static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000635__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 __kernel_size_t res;
638
639 might_sleep();
640 __asm__ __volatile__(
641 "move\t$4, %1\n\t"
642 "move\t$5, $0\n\t"
643 "move\t$6, %2\n\t"
644 __MODULE_JAL(__bzero)
645 "move\t%0, $6"
646 : "=r" (res)
647 : "r" (addr), "r" (size)
648 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
649
650 return res;
651}
652
653#define clear_user(addr,n) \
654({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000655 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned long __cl_size = (n); \
657 if (__cl_size && access_ok(VERIFY_WRITE, \
658 ((unsigned long)(__cl_addr)), __cl_size)) \
659 __cl_size = __clear_user(__cl_addr, __cl_size); \
660 __cl_size; \
661})
662
663/*
664 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
665 * @dst: Destination address, in kernel space. This buffer must be at
666 * least @count bytes long.
667 * @src: Source address, in user space.
668 * @count: Maximum number of bytes to copy, including the trailing NUL.
669 *
670 * Copies a NUL-terminated string from userspace to kernel space.
671 * Caller must check the specified block with access_ok() before calling
672 * this function.
673 *
674 * On success, returns the length of the string (not including the trailing
675 * NUL).
676 *
677 * If access to userspace fails, returns -EFAULT (some data may have been
678 * copied).
679 *
680 * If @count is smaller than the length of the string, copies @count bytes
681 * and returns @count.
682 */
683static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000684__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 long res;
687
688 might_sleep();
689 __asm__ __volatile__(
690 "move\t$4, %1\n\t"
691 "move\t$5, %2\n\t"
692 "move\t$6, %3\n\t"
693 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
694 "move\t%0, $2"
695 : "=r" (res)
696 : "r" (__to), "r" (__from), "r" (__len)
697 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
698
699 return res;
700}
701
702/*
703 * strncpy_from_user: - Copy a NUL terminated string from userspace.
704 * @dst: Destination address, in kernel space. This buffer must be at
705 * least @count bytes long.
706 * @src: Source address, in user space.
707 * @count: Maximum number of bytes to copy, including the trailing NUL.
708 *
709 * Copies a NUL-terminated string from userspace to kernel space.
710 *
711 * On success, returns the length of the string (not including the trailing
712 * NUL).
713 *
714 * If access to userspace fails, returns -EFAULT (some data may have been
715 * copied).
716 *
717 * If @count is smaller than the length of the string, copies @count bytes
718 * and returns @count.
719 */
720static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000721strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 long res;
724
725 might_sleep();
726 __asm__ __volatile__(
727 "move\t$4, %1\n\t"
728 "move\t$5, %2\n\t"
729 "move\t$6, %3\n\t"
730 __MODULE_JAL(__strncpy_from_user_asm)
731 "move\t%0, $2"
732 : "=r" (res)
733 : "r" (__to), "r" (__from), "r" (__len)
734 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
735
736 return res;
737}
738
739/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000740static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 long res;
743
744 might_sleep();
745 __asm__ __volatile__(
746 "move\t$4, %1\n\t"
747 __MODULE_JAL(__strlen_user_nocheck_asm)
748 "move\t%0, $2"
749 : "=r" (res)
750 : "r" (s)
751 : "$2", "$4", __UA_t0, "$31");
752
753 return res;
754}
755
756/*
757 * strlen_user: - Get the size of a string in user space.
758 * @str: The string to measure.
759 *
760 * Context: User context only. This function may sleep.
761 *
762 * Get the size of a NUL-terminated string in user space.
763 *
764 * Returns the size of the string INCLUDING the terminating NUL.
765 * On exception, returns 0.
766 *
767 * If there is a limit on the length of a valid string, you may wish to
768 * consider using strnlen_user() instead.
769 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000770static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 long res;
773
774 might_sleep();
775 __asm__ __volatile__(
776 "move\t$4, %1\n\t"
777 __MODULE_JAL(__strlen_user_asm)
778 "move\t%0, $2"
779 : "=r" (res)
780 : "r" (s)
781 : "$2", "$4", __UA_t0, "$31");
782
783 return res;
784}
785
786/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000787static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 long res;
790
791 might_sleep();
792 __asm__ __volatile__(
793 "move\t$4, %1\n\t"
794 "move\t$5, %2\n\t"
795 __MODULE_JAL(__strnlen_user_nocheck_asm)
796 "move\t%0, $2"
797 : "=r" (res)
798 : "r" (s), "r" (n)
799 : "$2", "$4", "$5", __UA_t0, "$31");
800
801 return res;
802}
803
804/*
805 * strlen_user: - Get the size of a string in user space.
806 * @str: The string to measure.
807 *
808 * Context: User context only. This function may sleep.
809 *
810 * Get the size of a NUL-terminated string in user space.
811 *
812 * Returns the size of the string INCLUDING the terminating NUL.
813 * On exception, returns 0.
814 *
815 * If there is a limit on the length of a valid string, you may wish to
816 * consider using strnlen_user() instead.
817 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000818static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 long res;
821
822 might_sleep();
823 __asm__ __volatile__(
824 "move\t$4, %1\n\t"
825 "move\t$5, %2\n\t"
826 __MODULE_JAL(__strnlen_user_asm)
827 "move\t%0, $2"
828 : "=r" (res)
829 : "r" (s), "r" (n)
830 : "$2", "$4", "$5", __UA_t0, "$31");
831
832 return res;
833}
834
835struct exception_table_entry
836{
837 unsigned long insn;
838 unsigned long nextinsn;
839};
840
841extern int fixup_exception(struct pt_regs *regs);
842
843#endif /* _ASM_UACCESS_H */