blob: b41901db69b87e7623ae11ed4ec3b08194129433 [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 */
205#ifdef __mips64
Ralf Baechlefe00f942005-03-01 19:22:29 +0000206#define __GET_USER_DW(ptr) __get_user_asm("ld", ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#else
Ralf Baechlefe00f942005-03-01 19:22:29 +0000208#define __GET_USER_DW(ptr) __get_user_asm_ll32(ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif
210
211#define __get_user_nocheck(x,ptr,size) \
212({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000213 __typeof(*(ptr)) __gu_val = (__typeof(*(ptr))) 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 long __gu_err = 0; \
215 \
216 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000218 case 1: __get_user_asm("lb", ptr); break; \
219 case 2: __get_user_asm("lh", ptr); break; \
220 case 4: __get_user_asm("lw", ptr); break; \
221 case 8: __GET_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 default: __get_user_unknown(); break; \
223 } \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000224 (x) = (__typeof__(*(ptr))) __gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 __gu_err; \
226})
227
228#define __get_user_check(x,ptr,size) \
229({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000230 const __typeof__(*(ptr)) __user * __gu_addr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 __typeof__(*(ptr)) __gu_val = 0; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000232 long __gu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000234 if (likely(access_ok(VERIFY_READ, __gu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000236 case 1: __get_user_asm("lb", __gu_addr); break; \
237 case 2: __get_user_asm("lh", __gu_addr); break; \
238 case 4: __get_user_asm("lw", __gu_addr); break; \
239 case 8: __GET_USER_DW(__gu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 default: __get_user_unknown(); break; \
241 } \
242 } \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000243 (x) = (__typeof__(*(ptr))) __gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 __gu_err; \
245})
246
Ralf Baechlefe00f942005-03-01 19:22:29 +0000247#define __get_user_asm(insn, addr) \
248{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 __asm__ __volatile__( \
250 "1: " insn " %1, %3 \n" \
251 "2: \n" \
252 " .section .fixup,\"ax\" \n" \
253 "3: li %0, %4 \n" \
254 " j 2b \n" \
255 " .previous \n" \
256 " .section __ex_table,\"a\" \n" \
257 " "__UA_ADDR "\t1b, 3b \n" \
258 " .previous \n" \
259 : "=r" (__gu_err), "=r" (__gu_val) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000260 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/*
264 * Get a long long 64 using 32 bit registers.
265 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000266#define __get_user_asm_ll32(addr) \
267{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000269 "1: lw %1, (%3) \n" \
270 "2: lw %D1, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 " move %0, $0 \n" \
272 "3: .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000273 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 " move %1, $0 \n" \
275 " move %D1, $0 \n" \
276 " j 3b \n" \
277 " .previous \n" \
278 " .section __ex_table,\"a\" \n" \
279 " " __UA_ADDR " 1b, 4b \n" \
280 " " __UA_ADDR " 2b, 4b \n" \
281 " .previous \n" \
282 : "=r" (__gu_err), "=&r" (__gu_val) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000283 : "0" (0), "r" (addr), "i" (-EFAULT)); \
284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286extern void __get_user_unknown(void);
287
288/*
289 * Yuck. We need two variants, one for 64bit operation and one
290 * for 32 bit mode and old iron.
291 */
292#ifdef __mips64
Ralf Baechlefe00f942005-03-01 19:22:29 +0000293#define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#else
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
297
298#define __put_user_nocheck(x,ptr,size) \
299({ \
300 __typeof__(*(ptr)) __pu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 long __pu_err = 0; \
302 \
303 might_sleep(); \
304 __pu_val = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000306 case 1: __put_user_asm("sb", ptr); break; \
307 case 2: __put_user_asm("sh", ptr); break; \
308 case 4: __put_user_asm("sw", ptr); break; \
309 case 8: __PUT_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 default: __put_user_unknown(); break; \
311 } \
312 __pu_err; \
313})
314
315#define __put_user_check(x,ptr,size) \
316({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000317 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
318 __typeof__(*(ptr)) __pu_val = (x); \
319 long __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000321 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000323 case 1: __put_user_asm("sb", __pu_addr); break; \
324 case 2: __put_user_asm("sh", __pu_addr); break; \
325 case 4: __put_user_asm("sw", __pu_addr); break; \
326 case 8: __PUT_USER_DW(__pu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 default: __put_user_unknown(); break; \
328 } \
329 } \
330 __pu_err; \
331})
332
Ralf Baechlefe00f942005-03-01 19:22:29 +0000333#define __put_user_asm(insn, ptr) \
334{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 __asm__ __volatile__( \
336 "1: " insn " %z2, %3 # __put_user_asm\n" \
337 "2: \n" \
338 " .section .fixup,\"ax\" \n" \
339 "3: li %0, %4 \n" \
340 " j 2b \n" \
341 " .previous \n" \
342 " .section __ex_table,\"a\" \n" \
343 " " __UA_ADDR " 1b, 3b \n" \
344 " .previous \n" \
345 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000346 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350#define __put_user_asm_ll32(ptr) \
351{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000353 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
354 "2: sw %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 "3: \n" \
356 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000357 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 " j 3b \n" \
359 " .previous \n" \
360 " .section __ex_table,\"a\" \n" \
361 " " __UA_ADDR " 1b, 4b \n" \
362 " " __UA_ADDR " 2b, 4b \n" \
363 " .previous" \
364 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000365 : "0" (0), "r" (__pu_val), "r" (ptr), \
366 "i" (-EFAULT)); \
367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369extern void __put_user_unknown(void);
370
371/*
372 * We're generating jump to subroutines which will be outside the range of
373 * jump instructions
374 */
375#ifdef MODULE
376#define __MODULE_JAL(destination) \
377 ".set\tnoat\n\t" \
378 __UA_LA "\t$1, " #destination "\n\t" \
379 "jalr\t$1\n\t" \
380 ".set\tat\n\t"
381#else
382#define __MODULE_JAL(destination) \
383 "jal\t" #destination "\n\t"
384#endif
385
386extern size_t __copy_user(void *__to, const void *__from, size_t __n);
387
388#define __invoke_copy_to_user(to,from,n) \
389({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000390 register void __user *__cu_to_r __asm__ ("$4"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 register const void *__cu_from_r __asm__ ("$5"); \
392 register long __cu_len_r __asm__ ("$6"); \
393 \
394 __cu_to_r = (to); \
395 __cu_from_r = (from); \
396 __cu_len_r = (n); \
397 __asm__ __volatile__( \
398 __MODULE_JAL(__copy_user) \
399 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
400 : \
401 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
402 "memory"); \
403 __cu_len_r; \
404})
405
406/*
407 * __copy_to_user: - Copy a block of data into user space, with less checking.
408 * @to: Destination address, in user space.
409 * @from: Source address, in kernel space.
410 * @n: Number of bytes to copy.
411 *
412 * Context: User context only. This function may sleep.
413 *
414 * Copy data from kernel space to user space. Caller must check
415 * the specified block with access_ok() before calling this function.
416 *
417 * Returns number of bytes that could not be copied.
418 * On success, this will be zero.
419 */
420#define __copy_to_user(to,from,n) \
421({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000422 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 const void *__cu_from; \
424 long __cu_len; \
425 \
426 might_sleep(); \
427 __cu_to = (to); \
428 __cu_from = (from); \
429 __cu_len = (n); \
430 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
431 __cu_len; \
432})
433
434#define __copy_to_user_inatomic __copy_to_user
435#define __copy_from_user_inatomic __copy_from_user
436
437/*
438 * copy_to_user: - Copy a block of data into user space.
439 * @to: Destination address, in user space.
440 * @from: Source address, in kernel space.
441 * @n: Number of bytes to copy.
442 *
443 * Context: User context only. This function may sleep.
444 *
445 * Copy data from kernel space to user space.
446 *
447 * Returns number of bytes that could not be copied.
448 * On success, this will be zero.
449 */
450#define copy_to_user(to,from,n) \
451({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000452 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 const void *__cu_from; \
454 long __cu_len; \
455 \
456 might_sleep(); \
457 __cu_to = (to); \
458 __cu_from = (from); \
459 __cu_len = (n); \
460 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
461 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
462 __cu_len); \
463 __cu_len; \
464})
465
466#define __invoke_copy_from_user(to,from,n) \
467({ \
468 register void *__cu_to_r __asm__ ("$4"); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000469 register const void __user *__cu_from_r __asm__ ("$5"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 register long __cu_len_r __asm__ ("$6"); \
471 \
472 __cu_to_r = (to); \
473 __cu_from_r = (from); \
474 __cu_len_r = (n); \
475 __asm__ __volatile__( \
476 ".set\tnoreorder\n\t" \
477 __MODULE_JAL(__copy_user) \
478 ".set\tnoat\n\t" \
479 __UA_ADDU "\t$1, %1, %2\n\t" \
480 ".set\tat\n\t" \
481 ".set\treorder" \
482 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
483 : \
484 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
485 "memory"); \
486 __cu_len_r; \
487})
488
489/*
490 * __copy_from_user: - Copy a block of data from user space, with less checking. * @to: Destination address, in kernel space.
491 * @from: Source address, in user space.
492 * @n: Number of bytes to copy.
493 *
494 * Context: User context only. This function may sleep.
495 *
496 * Copy data from user space to kernel space. Caller must check
497 * the specified block with access_ok() before calling this function.
498 *
499 * Returns number of bytes that could not be copied.
500 * On success, this will be zero.
501 *
502 * If some data could not be copied, this function will pad the copied
503 * data to the requested size using zero bytes.
504 */
505#define __copy_from_user(to,from,n) \
506({ \
507 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000508 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 long __cu_len; \
510 \
511 might_sleep(); \
512 __cu_to = (to); \
513 __cu_from = (from); \
514 __cu_len = (n); \
515 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
516 __cu_len); \
517 __cu_len; \
518})
519
520/*
521 * copy_from_user: - Copy a block of data from user space.
522 * @to: Destination address, in kernel space.
523 * @from: Source address, in user space.
524 * @n: Number of bytes to copy.
525 *
526 * Context: User context only. This function may sleep.
527 *
528 * Copy data from user space to kernel space.
529 *
530 * Returns number of bytes that could not be copied.
531 * On success, this will be zero.
532 *
533 * If some data could not be copied, this function will pad the copied
534 * data to the requested size using zero bytes.
535 */
536#define copy_from_user(to,from,n) \
537({ \
538 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000539 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 long __cu_len; \
541 \
542 might_sleep(); \
543 __cu_to = (to); \
544 __cu_from = (from); \
545 __cu_len = (n); \
546 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
547 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
548 __cu_len); \
549 __cu_len; \
550})
551
552#define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
553
554#define copy_in_user(to,from,n) \
555({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000556 void __user *__cu_to; \
557 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 long __cu_len; \
559 \
560 might_sleep(); \
561 __cu_to = (to); \
562 __cu_from = (from); \
563 __cu_len = (n); \
564 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
565 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
566 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
567 __cu_len); \
568 __cu_len; \
569})
570
571/*
572 * __clear_user: - Zero a block of memory in user space, with less checking.
573 * @to: Destination address, in user space.
574 * @n: Number of bytes to zero.
575 *
576 * Zero a block of memory in user space. Caller must check
577 * the specified block with access_ok() before calling this function.
578 *
579 * Returns number of bytes that could not be cleared.
580 * On success, this will be zero.
581 */
582static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000583__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 __kernel_size_t res;
586
587 might_sleep();
588 __asm__ __volatile__(
589 "move\t$4, %1\n\t"
590 "move\t$5, $0\n\t"
591 "move\t$6, %2\n\t"
592 __MODULE_JAL(__bzero)
593 "move\t%0, $6"
594 : "=r" (res)
595 : "r" (addr), "r" (size)
596 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
597
598 return res;
599}
600
601#define clear_user(addr,n) \
602({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000603 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned long __cl_size = (n); \
605 if (__cl_size && access_ok(VERIFY_WRITE, \
606 ((unsigned long)(__cl_addr)), __cl_size)) \
607 __cl_size = __clear_user(__cl_addr, __cl_size); \
608 __cl_size; \
609})
610
611/*
612 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
613 * @dst: Destination address, in kernel space. This buffer must be at
614 * least @count bytes long.
615 * @src: Source address, in user space.
616 * @count: Maximum number of bytes to copy, including the trailing NUL.
617 *
618 * Copies a NUL-terminated string from userspace to kernel space.
619 * Caller must check the specified block with access_ok() before calling
620 * this function.
621 *
622 * On success, returns the length of the string (not including the trailing
623 * NUL).
624 *
625 * If access to userspace fails, returns -EFAULT (some data may have been
626 * copied).
627 *
628 * If @count is smaller than the length of the string, copies @count bytes
629 * and returns @count.
630 */
631static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000632__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 long res;
635
636 might_sleep();
637 __asm__ __volatile__(
638 "move\t$4, %1\n\t"
639 "move\t$5, %2\n\t"
640 "move\t$6, %3\n\t"
641 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
642 "move\t%0, $2"
643 : "=r" (res)
644 : "r" (__to), "r" (__from), "r" (__len)
645 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
646
647 return res;
648}
649
650/*
651 * strncpy_from_user: - Copy a NUL terminated string from userspace.
652 * @dst: Destination address, in kernel space. This buffer must be at
653 * least @count bytes long.
654 * @src: Source address, in user space.
655 * @count: Maximum number of bytes to copy, including the trailing NUL.
656 *
657 * Copies a NUL-terminated string from userspace to kernel space.
658 *
659 * On success, returns the length of the string (not including the trailing
660 * NUL).
661 *
662 * If access to userspace fails, returns -EFAULT (some data may have been
663 * copied).
664 *
665 * If @count is smaller than the length of the string, copies @count bytes
666 * and returns @count.
667 */
668static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000669strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 long res;
672
673 might_sleep();
674 __asm__ __volatile__(
675 "move\t$4, %1\n\t"
676 "move\t$5, %2\n\t"
677 "move\t$6, %3\n\t"
678 __MODULE_JAL(__strncpy_from_user_asm)
679 "move\t%0, $2"
680 : "=r" (res)
681 : "r" (__to), "r" (__from), "r" (__len)
682 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
683
684 return res;
685}
686
687/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000688static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 long res;
691
692 might_sleep();
693 __asm__ __volatile__(
694 "move\t$4, %1\n\t"
695 __MODULE_JAL(__strlen_user_nocheck_asm)
696 "move\t%0, $2"
697 : "=r" (res)
698 : "r" (s)
699 : "$2", "$4", __UA_t0, "$31");
700
701 return res;
702}
703
704/*
705 * strlen_user: - Get the size of a string in user space.
706 * @str: The string to measure.
707 *
708 * Context: User context only. This function may sleep.
709 *
710 * Get the size of a NUL-terminated string in user space.
711 *
712 * Returns the size of the string INCLUDING the terminating NUL.
713 * On exception, returns 0.
714 *
715 * If there is a limit on the length of a valid string, you may wish to
716 * consider using strnlen_user() instead.
717 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000718static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 long res;
721
722 might_sleep();
723 __asm__ __volatile__(
724 "move\t$4, %1\n\t"
725 __MODULE_JAL(__strlen_user_asm)
726 "move\t%0, $2"
727 : "=r" (res)
728 : "r" (s)
729 : "$2", "$4", __UA_t0, "$31");
730
731 return res;
732}
733
734/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000735static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 long res;
738
739 might_sleep();
740 __asm__ __volatile__(
741 "move\t$4, %1\n\t"
742 "move\t$5, %2\n\t"
743 __MODULE_JAL(__strnlen_user_nocheck_asm)
744 "move\t%0, $2"
745 : "=r" (res)
746 : "r" (s), "r" (n)
747 : "$2", "$4", "$5", __UA_t0, "$31");
748
749 return res;
750}
751
752/*
753 * strlen_user: - Get the size of a string in user space.
754 * @str: The string to measure.
755 *
756 * Context: User context only. This function may sleep.
757 *
758 * Get the size of a NUL-terminated string in user space.
759 *
760 * Returns the size of the string INCLUDING the terminating NUL.
761 * On exception, returns 0.
762 *
763 * If there is a limit on the length of a valid string, you may wish to
764 * consider using strnlen_user() instead.
765 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000766static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 long res;
769
770 might_sleep();
771 __asm__ __volatile__(
772 "move\t$4, %1\n\t"
773 "move\t$5, %2\n\t"
774 __MODULE_JAL(__strnlen_user_asm)
775 "move\t%0, $2"
776 : "=r" (res)
777 : "r" (s), "r" (n)
778 : "$2", "$4", "$5", __UA_t0, "$31");
779
780 return res;
781}
782
783struct exception_table_entry
784{
785 unsigned long insn;
786 unsigned long nextinsn;
787};
788
789extern int fixup_exception(struct pt_regs *regs);
790
791#endif /* _ASM_UACCESS_H */