blob: b96f3e0f3933229788359d528657e73865b3dfca [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
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/thread_info.h>
16#include <asm-generic/uaccess.h>
17
18/*
19 * The fs value determines whether argument validity checking should be
20 * performed or not. If get_fs() == USER_DS, checking is performed, with
21 * get_fs() == KERNEL_DS, checking is bypassed.
22 *
23 * For historical reasons, these macros are grossly misnamed.
24 */
Ralf Baechle875d43e2005-09-03 15:56:16 -070025#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define __UA_LIMIT 0x80000000UL
28
29#define __UA_ADDR ".word"
30#define __UA_LA "la"
31#define __UA_ADDU "addu"
32#define __UA_t0 "$8"
33#define __UA_t1 "$9"
34
Ralf Baechle875d43e2005-09-03 15:56:16 -070035#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ralf Baechle875d43e2005-09-03 15:56:16 -070037#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define __UA_LIMIT (- TASK_SIZE)
40
41#define __UA_ADDR ".dword"
42#define __UA_LA "dla"
43#define __UA_ADDU "daddu"
44#define __UA_t0 "$12"
45#define __UA_t1 "$13"
46
Ralf Baechle875d43e2005-09-03 15:56:16 -070047#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * USER_DS is a bitmask that has the bits set that may not be set in a valid
51 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
52 * the arithmetic we're doing only works if the limit is a power of two, so
53 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
54 * address in this range it's the process's problem, not ours :-)
55 */
56
57#define KERNEL_DS ((mm_segment_t) { 0UL })
58#define USER_DS ((mm_segment_t) { __UA_LIMIT })
59
60#define VERIFY_READ 0
61#define VERIFY_WRITE 1
62
63#define get_ds() (KERNEL_DS)
64#define get_fs() (current_thread_info()->addr_limit)
65#define set_fs(x) (current_thread_info()->addr_limit = (x))
66
67#define segment_eq(a,b) ((a).seg == (b).seg)
68
69
70/*
71 * Is a address valid? This does a straighforward calculation rather
72 * than tests.
73 *
74 * Address valid if:
75 * - "addr" doesn't have any high-bits set
76 * - AND "size" doesn't have any high-bits set
77 * - AND "addr+size" doesn't have any high-bits set
78 * - OR we are in kernel mode.
79 *
80 * __ua_size() is a trick to avoid runtime checking of positive constant
81 * sizes; for those we already know at compile time that the size is ok.
82 */
83#define __ua_size(size) \
84 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
85
86/*
87 * access_ok: - Checks if a user space pointer is valid
88 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
89 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
90 * to write to a block, it is always safe to read from it.
91 * @addr: User space pointer to start of block to check
92 * @size: Size of block to check
93 *
94 * Context: User context only. This function may sleep.
95 *
96 * Checks if a pointer to a block of memory in user space is valid.
97 *
98 * Returns true (nonzero) if the memory block may be valid, false (zero)
99 * if it is definitely invalid.
100 *
101 * Note that, depending on architecture, this function probably just
102 * checks that the pointer is in the user space range - after calling
103 * this function, memory access functions may still return -EFAULT.
104 */
105
106#define __access_mask get_fs().seg
107
108#define __access_ok(addr, size, mask) \
109 (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
110
111#define access_ok(type, addr, size) \
112 likely(__access_ok((unsigned long)(addr), (size),__access_mask))
113
114/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * put_user: - Write a simple value into user space.
116 * @x: Value to copy to user space.
117 * @ptr: Destination address, in user space.
118 *
119 * Context: User context only. This function may sleep.
120 *
121 * This macro copies a single simple value from kernel space to user
122 * space. It supports simple types like char and int, but not larger
123 * data types like structures or arrays.
124 *
125 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
126 * to the result of dereferencing @ptr.
127 *
128 * Returns zero on success, or -EFAULT on error.
129 */
130#define put_user(x,ptr) \
131 __put_user_check((x),(ptr),sizeof(*(ptr)))
132
133/*
134 * get_user: - Get a simple variable from user space.
135 * @x: Variable to store result.
136 * @ptr: Source address, in user space.
137 *
138 * Context: User context only. This function may sleep.
139 *
140 * This macro copies a single simple variable from user space to kernel
141 * space. It supports simple types like char and int, but not larger
142 * data types like structures or arrays.
143 *
144 * @ptr must have pointer-to-simple-variable type, and the result of
145 * dereferencing @ptr must be assignable to @x without a cast.
146 *
147 * Returns zero on success, or -EFAULT on error.
148 * On error, the variable @x is set to zero.
149 */
150#define get_user(x,ptr) \
151 __get_user_check((x),(ptr),sizeof(*(ptr)))
152
153/*
154 * __put_user: - Write a simple value into user space, with less checking.
155 * @x: Value to copy to user space.
156 * @ptr: Destination address, in user space.
157 *
158 * Context: User context only. This function may sleep.
159 *
160 * This macro copies a single simple value from kernel space to user
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 @x must be assignable
165 * to the result of dereferencing @ptr.
166 *
167 * Caller must check the pointer with access_ok() before calling this
168 * function.
169 *
170 * Returns zero on success, or -EFAULT on error.
171 */
172#define __put_user(x,ptr) \
173 __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
174
175/*
176 * __get_user: - Get a simple variable from user space, with less checking.
177 * @x: Variable to store result.
178 * @ptr: Source address, in user space.
179 *
180 * Context: User context only. This function may sleep.
181 *
182 * This macro copies a single simple variable from user space to kernel
183 * space. It supports simple types like char and int, but not larger
184 * data types like structures or arrays.
185 *
186 * @ptr must have pointer-to-simple-variable type, and the result of
187 * dereferencing @ptr must be assignable to @x without a cast.
188 *
189 * Caller must check the pointer with access_ok() before calling this
190 * function.
191 *
192 * Returns zero on success, or -EFAULT on error.
193 * On error, the variable @x is set to zero.
194 */
195#define __get_user(x,ptr) \
196 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
197
198struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000199#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/*
202 * Yuck. We need two variants, one for 64bit operation and one
203 * for 32 bit mode and old iron.
204 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000205#ifdef CONFIG_32BIT
206#define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000208#ifdef CONFIG_64BIT
209#define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
210#endif
211
212extern void __get_user_unknown(void);
213
214#define __get_user_common(val, size, ptr) \
215do { \
216 switch (size) { \
217 case 1: __get_user_asm(val, "lb", ptr); break; \
218 case 2: __get_user_asm(val, "lh", ptr); break; \
219 case 4: __get_user_asm(val, "lw", ptr); break; \
220 case 8: __GET_USER_DW(val, ptr); break; \
221 default: __get_user_unknown(); break; \
222 } \
223} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#define __get_user_nocheck(x,ptr,size) \
226({ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000227 long __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000229 __get_user_common((x), size, ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 __gu_err; \
231})
232
233#define __get_user_check(x,ptr,size) \
234({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000235 long __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900236 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000238 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
239 __get_user_common((x), size, __gu_ptr); \
240 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 __gu_err; \
242})
243
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000244#define __get_user_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000245{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000246 long __gu_tmp; \
247 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 __asm__ __volatile__( \
249 "1: " insn " %1, %3 \n" \
250 "2: \n" \
251 " .section .fixup,\"ax\" \n" \
252 "3: li %0, %4 \n" \
253 " j 2b \n" \
254 " .previous \n" \
255 " .section __ex_table,\"a\" \n" \
256 " "__UA_ADDR "\t1b, 3b \n" \
257 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000258 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000259 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000260 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900261 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/*
265 * Get a long long 64 using 32 bit registers.
266 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000267#define __get_user_asm_ll32(val, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000268{ \
Ralf Baechle32183572006-02-10 01:31:24 +0000269 unsigned long long __gu_tmp; \
270 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000272 "1: lw %1, (%3) \n" \
273 "2: lw %D1, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 " move %0, $0 \n" \
275 "3: .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000276 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 " move %1, $0 \n" \
278 " move %D1, $0 \n" \
279 " j 3b \n" \
280 " .previous \n" \
281 " .section __ex_table,\"a\" \n" \
282 " " __UA_ADDR " 1b, 4b \n" \
283 " " __UA_ADDR " 2b, 4b \n" \
284 " .previous \n" \
Ralf Baechle32183572006-02-10 01:31:24 +0000285 : "=r" (__gu_err), "=&r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000286 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900287 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * Yuck. We need two variants, one for 64bit operation and one
292 * for 32 bit mode and old iron.
293 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000294#ifdef CONFIG_32BIT
Ralf Baechlefe00f942005-03-01 19:22:29 +0000295#define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000297#ifdef CONFIG_64BIT
298#define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301#define __put_user_nocheck(x,ptr,size) \
302({ \
303 __typeof__(*(ptr)) __pu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 long __pu_err = 0; \
305 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 __pu_val = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000308 case 1: __put_user_asm("sb", ptr); break; \
309 case 2: __put_user_asm("sh", ptr); break; \
310 case 4: __put_user_asm("sw", ptr); break; \
311 case 8: __PUT_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 default: __put_user_unknown(); break; \
313 } \
314 __pu_err; \
315})
316
317#define __put_user_check(x,ptr,size) \
318({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000319 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
320 __typeof__(*(ptr)) __pu_val = (x); \
321 long __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000323 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000325 case 1: __put_user_asm("sb", __pu_addr); break; \
326 case 2: __put_user_asm("sh", __pu_addr); break; \
327 case 4: __put_user_asm("sw", __pu_addr); break; \
328 case 8: __PUT_USER_DW(__pu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 default: __put_user_unknown(); break; \
330 } \
331 } \
332 __pu_err; \
333})
334
Ralf Baechlefe00f942005-03-01 19:22:29 +0000335#define __put_user_asm(insn, ptr) \
336{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 __asm__ __volatile__( \
338 "1: " insn " %z2, %3 # __put_user_asm\n" \
339 "2: \n" \
340 " .section .fixup,\"ax\" \n" \
341 "3: li %0, %4 \n" \
342 " j 2b \n" \
343 " .previous \n" \
344 " .section __ex_table,\"a\" \n" \
345 " " __UA_ADDR " 1b, 3b \n" \
346 " .previous \n" \
347 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000348 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Ralf Baechlefe00f942005-03-01 19:22:29 +0000352#define __put_user_asm_ll32(ptr) \
353{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000355 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
356 "2: sw %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 "3: \n" \
358 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000359 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 " j 3b \n" \
361 " .previous \n" \
362 " .section __ex_table,\"a\" \n" \
363 " " __UA_ADDR " 1b, 4b \n" \
364 " " __UA_ADDR " 2b, 4b \n" \
365 " .previous" \
366 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000367 : "0" (0), "r" (__pu_val), "r" (ptr), \
368 "i" (-EFAULT)); \
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371extern void __put_user_unknown(void);
372
373/*
374 * We're generating jump to subroutines which will be outside the range of
375 * jump instructions
376 */
377#ifdef MODULE
378#define __MODULE_JAL(destination) \
379 ".set\tnoat\n\t" \
380 __UA_LA "\t$1, " #destination "\n\t" \
381 "jalr\t$1\n\t" \
382 ".set\tat\n\t"
383#else
384#define __MODULE_JAL(destination) \
385 "jal\t" #destination "\n\t"
386#endif
387
388extern size_t __copy_user(void *__to, const void *__from, size_t __n);
389
390#define __invoke_copy_to_user(to,from,n) \
391({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000392 register void __user *__cu_to_r __asm__ ("$4"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 register const void *__cu_from_r __asm__ ("$5"); \
394 register long __cu_len_r __asm__ ("$6"); \
395 \
396 __cu_to_r = (to); \
397 __cu_from_r = (from); \
398 __cu_len_r = (n); \
399 __asm__ __volatile__( \
400 __MODULE_JAL(__copy_user) \
401 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
402 : \
403 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
404 "memory"); \
405 __cu_len_r; \
406})
407
408/*
409 * __copy_to_user: - Copy a block of data into user space, with less checking.
410 * @to: Destination address, in user space.
411 * @from: Source address, in kernel space.
412 * @n: Number of bytes to copy.
413 *
414 * Context: User context only. This function may sleep.
415 *
416 * Copy data from kernel space to user space. Caller must check
417 * the specified block with access_ok() before calling this function.
418 *
419 * Returns number of bytes that could not be copied.
420 * On success, this will be zero.
421 */
422#define __copy_to_user(to,from,n) \
423({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000424 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 const void *__cu_from; \
426 long __cu_len; \
427 \
428 might_sleep(); \
429 __cu_to = (to); \
430 __cu_from = (from); \
431 __cu_len = (n); \
432 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
433 __cu_len; \
434})
435
436#define __copy_to_user_inatomic __copy_to_user
437#define __copy_from_user_inatomic __copy_from_user
438
439/*
440 * copy_to_user: - Copy a block of data into user space.
441 * @to: Destination address, in user space.
442 * @from: Source address, in kernel space.
443 * @n: Number of bytes to copy.
444 *
445 * Context: User context only. This function may sleep.
446 *
447 * Copy data from kernel space to user space.
448 *
449 * Returns number of bytes that could not be copied.
450 * On success, this will be zero.
451 */
452#define copy_to_user(to,from,n) \
453({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000454 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 const void *__cu_from; \
456 long __cu_len; \
457 \
458 might_sleep(); \
459 __cu_to = (to); \
460 __cu_from = (from); \
461 __cu_len = (n); \
462 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
463 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
464 __cu_len); \
465 __cu_len; \
466})
467
468#define __invoke_copy_from_user(to,from,n) \
469({ \
470 register void *__cu_to_r __asm__ ("$4"); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000471 register const void __user *__cu_from_r __asm__ ("$5"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 register long __cu_len_r __asm__ ("$6"); \
473 \
474 __cu_to_r = (to); \
475 __cu_from_r = (from); \
476 __cu_len_r = (n); \
477 __asm__ __volatile__( \
478 ".set\tnoreorder\n\t" \
479 __MODULE_JAL(__copy_user) \
480 ".set\tnoat\n\t" \
481 __UA_ADDU "\t$1, %1, %2\n\t" \
482 ".set\tat\n\t" \
483 ".set\treorder" \
484 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
485 : \
486 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
487 "memory"); \
488 __cu_len_r; \
489})
490
491/*
492 * __copy_from_user: - Copy a block of data from user space, with less checking. * @to: Destination address, in kernel space.
493 * @from: Source address, in user space.
494 * @n: Number of bytes to copy.
495 *
496 * Context: User context only. This function may sleep.
497 *
498 * Copy data from user space to kernel space. Caller must check
499 * the specified block with access_ok() before calling this function.
500 *
501 * Returns number of bytes that could not be copied.
502 * On success, this will be zero.
503 *
504 * If some data could not be copied, this function will pad the copied
505 * data to the requested size using zero bytes.
506 */
507#define __copy_from_user(to,from,n) \
508({ \
509 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000510 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 long __cu_len; \
512 \
513 might_sleep(); \
514 __cu_to = (to); \
515 __cu_from = (from); \
516 __cu_len = (n); \
517 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
518 __cu_len); \
519 __cu_len; \
520})
521
522/*
523 * copy_from_user: - Copy a block of data from user space.
524 * @to: Destination address, in kernel space.
525 * @from: Source address, in user space.
526 * @n: Number of bytes to copy.
527 *
528 * Context: User context only. This function may sleep.
529 *
530 * Copy data from user space to kernel space.
531 *
532 * Returns number of bytes that could not be copied.
533 * On success, this will be zero.
534 *
535 * If some data could not be copied, this function will pad the copied
536 * data to the requested size using zero bytes.
537 */
538#define copy_from_user(to,from,n) \
539({ \
540 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000541 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 long __cu_len; \
543 \
544 might_sleep(); \
545 __cu_to = (to); \
546 __cu_from = (from); \
547 __cu_len = (n); \
548 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
549 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
550 __cu_len); \
551 __cu_len; \
552})
553
554#define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
555
556#define copy_in_user(to,from,n) \
557({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000558 void __user *__cu_to; \
559 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 long __cu_len; \
561 \
562 might_sleep(); \
563 __cu_to = (to); \
564 __cu_from = (from); \
565 __cu_len = (n); \
566 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
567 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
568 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
569 __cu_len); \
570 __cu_len; \
571})
572
573/*
574 * __clear_user: - Zero a block of memory in user space, with less checking.
575 * @to: Destination address, in user space.
576 * @n: Number of bytes to zero.
577 *
578 * Zero a block of memory in user space. Caller must check
579 * the specified block with access_ok() before calling this function.
580 *
581 * Returns number of bytes that could not be cleared.
582 * On success, this will be zero.
583 */
584static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000585__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 __kernel_size_t res;
588
589 might_sleep();
590 __asm__ __volatile__(
591 "move\t$4, %1\n\t"
592 "move\t$5, $0\n\t"
593 "move\t$6, %2\n\t"
594 __MODULE_JAL(__bzero)
595 "move\t%0, $6"
596 : "=r" (res)
597 : "r" (addr), "r" (size)
598 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
599
600 return res;
601}
602
603#define clear_user(addr,n) \
604({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000605 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long __cl_size = (n); \
607 if (__cl_size && access_ok(VERIFY_WRITE, \
608 ((unsigned long)(__cl_addr)), __cl_size)) \
609 __cl_size = __clear_user(__cl_addr, __cl_size); \
610 __cl_size; \
611})
612
613/*
614 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
615 * @dst: Destination address, in kernel space. This buffer must be at
616 * least @count bytes long.
617 * @src: Source address, in user space.
618 * @count: Maximum number of bytes to copy, including the trailing NUL.
619 *
620 * Copies a NUL-terminated string from userspace to kernel space.
621 * Caller must check the specified block with access_ok() before calling
622 * this function.
623 *
624 * On success, returns the length of the string (not including the trailing
625 * NUL).
626 *
627 * If access to userspace fails, returns -EFAULT (some data may have been
628 * copied).
629 *
630 * If @count is smaller than the length of the string, copies @count bytes
631 * and returns @count.
632 */
633static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000634__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 long res;
637
638 might_sleep();
639 __asm__ __volatile__(
640 "move\t$4, %1\n\t"
641 "move\t$5, %2\n\t"
642 "move\t$6, %3\n\t"
643 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
644 "move\t%0, $2"
645 : "=r" (res)
646 : "r" (__to), "r" (__from), "r" (__len)
647 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
648
649 return res;
650}
651
652/*
653 * strncpy_from_user: - Copy a NUL terminated string from userspace.
654 * @dst: Destination address, in kernel space. This buffer must be at
655 * least @count bytes long.
656 * @src: Source address, in user space.
657 * @count: Maximum number of bytes to copy, including the trailing NUL.
658 *
659 * Copies a NUL-terminated string from userspace to kernel space.
660 *
661 * On success, returns the length of the string (not including the trailing
662 * NUL).
663 *
664 * If access to userspace fails, returns -EFAULT (some data may have been
665 * copied).
666 *
667 * If @count is smaller than the length of the string, copies @count bytes
668 * and returns @count.
669 */
670static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000671strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 long res;
674
675 might_sleep();
676 __asm__ __volatile__(
677 "move\t$4, %1\n\t"
678 "move\t$5, %2\n\t"
679 "move\t$6, %3\n\t"
680 __MODULE_JAL(__strncpy_from_user_asm)
681 "move\t%0, $2"
682 : "=r" (res)
683 : "r" (__to), "r" (__from), "r" (__len)
684 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
685
686 return res;
687}
688
689/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000690static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 long res;
693
694 might_sleep();
695 __asm__ __volatile__(
696 "move\t$4, %1\n\t"
697 __MODULE_JAL(__strlen_user_nocheck_asm)
698 "move\t%0, $2"
699 : "=r" (res)
700 : "r" (s)
701 : "$2", "$4", __UA_t0, "$31");
702
703 return res;
704}
705
706/*
707 * strlen_user: - Get the size of a string in user space.
708 * @str: The string to measure.
709 *
710 * Context: User context only. This function may sleep.
711 *
712 * Get the size of a NUL-terminated string in user space.
713 *
714 * Returns the size of the string INCLUDING the terminating NUL.
715 * On exception, returns 0.
716 *
717 * If there is a limit on the length of a valid string, you may wish to
718 * consider using strnlen_user() instead.
719 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000720static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 long res;
723
724 might_sleep();
725 __asm__ __volatile__(
726 "move\t$4, %1\n\t"
727 __MODULE_JAL(__strlen_user_asm)
728 "move\t%0, $2"
729 : "=r" (res)
730 : "r" (s)
731 : "$2", "$4", __UA_t0, "$31");
732
733 return res;
734}
735
736/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000737static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 long res;
740
741 might_sleep();
742 __asm__ __volatile__(
743 "move\t$4, %1\n\t"
744 "move\t$5, %2\n\t"
745 __MODULE_JAL(__strnlen_user_nocheck_asm)
746 "move\t%0, $2"
747 : "=r" (res)
748 : "r" (s), "r" (n)
749 : "$2", "$4", "$5", __UA_t0, "$31");
750
751 return res;
752}
753
754/*
755 * strlen_user: - Get the size of a string in user space.
756 * @str: The string to measure.
757 *
758 * Context: User context only. This function may sleep.
759 *
760 * Get the size of a NUL-terminated string in user space.
761 *
762 * Returns the size of the string INCLUDING the terminating NUL.
763 * On exception, returns 0.
764 *
765 * If there is a limit on the length of a valid string, you may wish to
766 * consider using strnlen_user() instead.
767 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000768static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 long res;
771
772 might_sleep();
773 __asm__ __volatile__(
774 "move\t$4, %1\n\t"
775 "move\t$5, %2\n\t"
776 __MODULE_JAL(__strnlen_user_asm)
777 "move\t%0, $2"
778 : "=r" (res)
779 : "r" (s), "r" (n)
780 : "$2", "$4", "$5", __UA_t0, "$31");
781
782 return res;
783}
784
785struct exception_table_entry
786{
787 unsigned long insn;
788 unsigned long nextinsn;
789};
790
791extern int fixup_exception(struct pt_regs *regs);
792
793#endif /* _ASM_UACCESS_H */