blob: 3f1337c342087561483c5d604396a227ce4664ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __i386_UACCESS_H
2#define __i386_UACCESS_H
3
4/*
5 * User space memory access functions
6 */
7#include <linux/config.h>
8#include <linux/errno.h>
9#include <linux/thread_info.h>
10#include <linux/prefetch.h>
11#include <linux/string.h>
12#include <asm/page.h>
13
14#define VERIFY_READ 0
15#define VERIFY_WRITE 1
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 */
24
25#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26
27
28#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
29#define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
30
31#define get_ds() (KERNEL_DS)
32#define get_fs() (current_thread_info()->addr_limit)
33#define set_fs(x) (current_thread_info()->addr_limit = (x))
34
35#define segment_eq(a,b) ((a).seg == (b).seg)
36
37/*
38 * movsl can be slow when source and dest are not both 8-byte aligned
39 */
40#ifdef CONFIG_X86_INTEL_USERCOPY
41extern struct movsl_mask {
42 int mask;
43} ____cacheline_aligned_in_smp movsl_mask;
44#endif
45
46#define __addr_ok(addr) ((unsigned long __force)(addr) < (current_thread_info()->addr_limit.seg))
47
48/*
49 * Test whether a block of memory is a valid user space address.
50 * Returns 0 if the range is valid, nonzero otherwise.
51 *
52 * This is equivalent to the following test:
53 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
54 *
55 * This needs 33-bit arithmetic. We have a carry...
56 */
57#define __range_ok(addr,size) ({ \
58 unsigned long flag,sum; \
59 __chk_user_ptr(addr); \
60 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
61 :"=&r" (flag), "=r" (sum) \
62 :"1" (addr),"g" ((int)(size)),"g" (current_thread_info()->addr_limit.seg)); \
63 flag; })
64
65/**
66 * access_ok: - Checks if a user space pointer is valid
67 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
68 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
69 * to write to a block, it is always safe to read from it.
70 * @addr: User space pointer to start of block to check
71 * @size: Size of block to check
72 *
73 * Context: User context only. This function may sleep.
74 *
75 * Checks if a pointer to a block of memory in user space is valid.
76 *
77 * Returns true (nonzero) if the memory block may be valid, false (zero)
78 * if it is definitely invalid.
79 *
80 * Note that, depending on architecture, this function probably just
81 * checks that the pointer is in the user space range - after calling
82 * this function, memory access functions may still return -EFAULT.
83 */
84#define access_ok(type,addr,size) (likely(__range_ok(addr,size) == 0))
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * The exception table consists of pairs of addresses: the first is the
88 * address of an instruction that is allowed to fault, and the second is
89 * the address at which the program should continue. No registers are
90 * modified, so it is entirely up to the continuation code to figure out
91 * what to do.
92 *
93 * All the routines below use bits of fixup code that are out of line
94 * with the main instruction path. This means when everything is well,
95 * we don't even have to jump over them. Further, they do not intrude
96 * on our cache or tlb entries.
97 */
98
99struct exception_table_entry
100{
101 unsigned long insn, fixup;
102};
103
104extern int fixup_exception(struct pt_regs *regs);
105
106/*
107 * These are the main single-value transfer routines. They automatically
108 * use the right size if we just have the right pointer type.
109 *
110 * This gets kind of ugly. We want to return _two_ values in "get_user()"
111 * and yet we don't want to do any pointers, because that is too much
112 * of a performance impact. Thus we have a few rather ugly macros here,
113 * and hide all the ugliness from the user.
114 *
115 * The "__xxx" versions of the user access functions are versions that
116 * do not verify the address space, that must have been done previously
117 * with a separate "access_ok()" call (this is used when we do multiple
118 * accesses to the same area of user memory).
119 */
120
121extern void __get_user_1(void);
122extern void __get_user_2(void);
123extern void __get_user_4(void);
124
125#define __get_user_x(size,ret,x,ptr) \
126 __asm__ __volatile__("call __get_user_" #size \
127 :"=a" (ret),"=d" (x) \
128 :"0" (ptr))
129
130
131/* Careful: we have to cast the result to the type of the pointer for sign reasons */
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({ int __ret_gu; \
151 unsigned long __val_gu; \
152 __chk_user_ptr(ptr); \
153 switch(sizeof (*(ptr))) { \
154 case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \
155 case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \
156 case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \
157 default: __get_user_x(X,__ret_gu,__val_gu,ptr); break; \
158 } \
159 (x) = (__typeof__(*(ptr)))__val_gu; \
160 __ret_gu; \
161})
162
163extern void __put_user_bad(void);
164
165/*
166 * Strange magic calling convention: pointer in %ecx,
167 * value in %eax(:%edx), return value in %eax, no clobbers.
168 */
169extern void __put_user_1(void);
170extern void __put_user_2(void);
171extern void __put_user_4(void);
172extern void __put_user_8(void);
173
174#define __put_user_1(x, ptr) __asm__ __volatile__("call __put_user_1":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
175#define __put_user_2(x, ptr) __asm__ __volatile__("call __put_user_2":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
176#define __put_user_4(x, ptr) __asm__ __volatile__("call __put_user_4":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
177#define __put_user_8(x, ptr) __asm__ __volatile__("call __put_user_8":"=a" (__ret_pu):"A" ((typeof(*(ptr)))(x)), "c" (ptr))
178#define __put_user_X(x, ptr) __asm__ __volatile__("call __put_user_X":"=a" (__ret_pu):"c" (ptr))
179
180/**
181 * put_user: - Write a simple value into user space.
182 * @x: Value to copy to user space.
183 * @ptr: Destination address, in user space.
184 *
185 * Context: User context only. This function may sleep.
186 *
187 * This macro copies a single simple value from kernel space to user
188 * space. It supports simple types like char and int, but not larger
189 * data types like structures or arrays.
190 *
191 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
192 * to the result of dereferencing @ptr.
193 *
194 * Returns zero on success, or -EFAULT on error.
195 */
196#ifdef CONFIG_X86_WP_WORKS_OK
197
198#define put_user(x,ptr) \
199({ int __ret_pu; \
200 __chk_user_ptr(ptr); \
201 switch(sizeof(*(ptr))) { \
202 case 1: __put_user_1(x, ptr); break; \
203 case 2: __put_user_2(x, ptr); break; \
204 case 4: __put_user_4(x, ptr); break; \
205 case 8: __put_user_8(x, ptr); break; \
206 default:__put_user_X(x, ptr); break; \
207 } \
208 __ret_pu; \
209})
210
211#else
212#define put_user(x,ptr) \
213({ \
214 int __ret_pu; \
215 __typeof__(*(ptr)) __pus_tmp = x; \
216 __ret_pu=0; \
217 if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
218 sizeof(*(ptr))) != 0)) \
219 __ret_pu=-EFAULT; \
220 __ret_pu; \
221 })
222
223
224#endif
225
226/**
227 * __get_user: - Get a simple variable from user space, with less checking.
228 * @x: Variable to store result.
229 * @ptr: Source address, in user space.
230 *
231 * Context: User context only. This function may sleep.
232 *
233 * This macro copies a single simple variable from user space to kernel
234 * space. It supports simple types like char and int, but not larger
235 * data types like structures or arrays.
236 *
237 * @ptr must have pointer-to-simple-variable type, and the result of
238 * dereferencing @ptr must be assignable to @x without a cast.
239 *
240 * Caller must check the pointer with access_ok() before calling this
241 * function.
242 *
243 * Returns zero on success, or -EFAULT on error.
244 * On error, the variable @x is set to zero.
245 */
246#define __get_user(x,ptr) \
247 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
248
249
250/**
251 * __put_user: - Write a simple value into user space, with less checking.
252 * @x: Value to copy to user space.
253 * @ptr: Destination address, in user space.
254 *
255 * Context: User context only. This function may sleep.
256 *
257 * This macro copies a single simple value from kernel space to user
258 * space. It supports simple types like char and int, but not larger
259 * data types like structures or arrays.
260 *
261 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
262 * to the result of dereferencing @ptr.
263 *
264 * Caller must check the pointer with access_ok() before calling this
265 * function.
266 *
267 * Returns zero on success, or -EFAULT on error.
268 */
269#define __put_user(x,ptr) \
270 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
271
272#define __put_user_nocheck(x,ptr,size) \
273({ \
274 long __pu_err; \
275 __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
276 __pu_err; \
277})
278
279
280#define __put_user_u64(x, addr, err) \
281 __asm__ __volatile__( \
282 "1: movl %%eax,0(%2)\n" \
283 "2: movl %%edx,4(%2)\n" \
284 "3:\n" \
285 ".section .fixup,\"ax\"\n" \
286 "4: movl %3,%0\n" \
287 " jmp 3b\n" \
288 ".previous\n" \
289 ".section __ex_table,\"a\"\n" \
290 " .align 4\n" \
291 " .long 1b,4b\n" \
292 " .long 2b,4b\n" \
293 ".previous" \
294 : "=r"(err) \
295 : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
296
297#ifdef CONFIG_X86_WP_WORKS_OK
298
299#define __put_user_size(x,ptr,size,retval,errret) \
300do { \
301 retval = 0; \
302 __chk_user_ptr(ptr); \
303 switch (size) { \
304 case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \
305 case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \
306 case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \
307 case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\
308 default: __put_user_bad(); \
309 } \
310} while (0)
311
312#else
313
314#define __put_user_size(x,ptr,size,retval,errret) \
315do { \
316 __typeof__(*(ptr)) __pus_tmp = x; \
317 retval = 0; \
318 \
319 if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
320 retval = errret; \
321} while (0)
322
323#endif
324struct __large_struct { unsigned long buf[100]; };
325#define __m(x) (*(struct __large_struct __user *)(x))
326
327/*
328 * Tell gcc we read from memory instead of writing: this is because
329 * we do not write to any memory gcc knows about, so there are no
330 * aliasing issues.
331 */
332#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
333 __asm__ __volatile__( \
334 "1: mov"itype" %"rtype"1,%2\n" \
335 "2:\n" \
336 ".section .fixup,\"ax\"\n" \
337 "3: movl %3,%0\n" \
338 " jmp 2b\n" \
339 ".previous\n" \
340 ".section __ex_table,\"a\"\n" \
341 " .align 4\n" \
342 " .long 1b,3b\n" \
343 ".previous" \
344 : "=r"(err) \
345 : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err))
346
347
348#define __get_user_nocheck(x,ptr,size) \
349({ \
350 long __gu_err; \
351 unsigned long __gu_val; \
352 __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
353 (x) = (__typeof__(*(ptr)))__gu_val; \
354 __gu_err; \
355})
356
357extern long __get_user_bad(void);
358
359#define __get_user_size(x,ptr,size,retval,errret) \
360do { \
361 retval = 0; \
362 __chk_user_ptr(ptr); \
363 switch (size) { \
364 case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \
365 case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \
366 case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break; \
367 default: (x) = __get_user_bad(); \
368 } \
369} while (0)
370
371#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
372 __asm__ __volatile__( \
373 "1: mov"itype" %2,%"rtype"1\n" \
374 "2:\n" \
375 ".section .fixup,\"ax\"\n" \
376 "3: movl %3,%0\n" \
377 " xor"itype" %"rtype"1,%"rtype"1\n" \
378 " jmp 2b\n" \
379 ".previous\n" \
380 ".section __ex_table,\"a\"\n" \
381 " .align 4\n" \
382 " .long 1b,3b\n" \
383 ".previous" \
384 : "=r"(err), ltype (x) \
385 : "m"(__m(addr)), "i"(errret), "0"(err))
386
387
388unsigned long __must_check __copy_to_user_ll(void __user *to,
389 const void *from, unsigned long n);
390unsigned long __must_check __copy_from_user_ll(void *to,
391 const void __user *from, unsigned long n);
392
393/*
394 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
395 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
396 * If a store crosses a page boundary and gets a fault, the x86 will not write
397 * anything, so this is accurate.
398 */
399
400/**
401 * __copy_to_user: - Copy a block of data into user space, with less checking.
402 * @to: Destination address, in user space.
403 * @from: Source address, in kernel space.
404 * @n: Number of bytes to copy.
405 *
406 * Context: User context only. This function may sleep.
407 *
408 * Copy data from kernel space to user space. Caller must check
409 * the specified block with access_ok() before calling this function.
410 *
411 * Returns number of bytes that could not be copied.
412 * On success, this will be zero.
413 */
Ingo Molnar652050a2006-01-14 13:21:30 -0800414static __always_inline unsigned long __must_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415__copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
416{
417 if (__builtin_constant_p(n)) {
418 unsigned long ret;
419
420 switch (n) {
421 case 1:
422 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
423 return ret;
424 case 2:
425 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
426 return ret;
427 case 4:
428 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
429 return ret;
430 }
431 }
432 return __copy_to_user_ll(to, from, n);
433}
434
Ingo Molnar652050a2006-01-14 13:21:30 -0800435static __always_inline unsigned long __must_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436__copy_to_user(void __user *to, const void *from, unsigned long n)
437{
438 might_sleep();
439 return __copy_to_user_inatomic(to, from, n);
440}
441
442/**
443 * __copy_from_user: - Copy a block of data from user space, with less checking.
444 * @to: Destination address, in kernel space.
445 * @from: Source address, in user space.
446 * @n: Number of bytes to copy.
447 *
448 * Context: User context only. This function may sleep.
449 *
450 * Copy data from user space to kernel space. Caller must check
451 * the specified block with access_ok() before calling this function.
452 *
453 * Returns number of bytes that could not be copied.
454 * On success, this will be zero.
455 *
456 * If some data could not be copied, this function will pad the copied
457 * data to the requested size using zero bytes.
458 */
Ingo Molnar652050a2006-01-14 13:21:30 -0800459static __always_inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
461{
462 if (__builtin_constant_p(n)) {
463 unsigned long ret;
464
465 switch (n) {
466 case 1:
467 __get_user_size(*(u8 *)to, from, 1, ret, 1);
468 return ret;
469 case 2:
470 __get_user_size(*(u16 *)to, from, 2, ret, 2);
471 return ret;
472 case 4:
473 __get_user_size(*(u32 *)to, from, 4, ret, 4);
474 return ret;
475 }
476 }
477 return __copy_from_user_ll(to, from, n);
478}
479
Ingo Molnar652050a2006-01-14 13:21:30 -0800480static __always_inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481__copy_from_user(void *to, const void __user *from, unsigned long n)
482{
483 might_sleep();
484 return __copy_from_user_inatomic(to, from, n);
485}
486unsigned long __must_check copy_to_user(void __user *to,
487 const void *from, unsigned long n);
488unsigned long __must_check copy_from_user(void *to,
489 const void __user *from, unsigned long n);
490long __must_check strncpy_from_user(char *dst, const char __user *src,
491 long count);
492long __must_check __strncpy_from_user(char *dst,
493 const char __user *src, long count);
494
495/**
496 * strlen_user: - Get the size of a string in user space.
497 * @str: The string to measure.
498 *
499 * Context: User context only. This function may sleep.
500 *
501 * Get the size of a NUL-terminated string in user space.
502 *
503 * Returns the size of the string INCLUDING the terminating NUL.
504 * On exception, returns 0.
505 *
506 * If there is a limit on the length of a valid string, you may wish to
507 * consider using strnlen_user() instead.
508 */
509#define strlen_user(str) strnlen_user(str, ~0UL >> 1)
510
511long strnlen_user(const char __user *str, long n);
512unsigned long __must_check clear_user(void __user *mem, unsigned long len);
513unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
514
515#endif /* __i386_UACCESS_H */