[ALSA] oxygen: generalize handling of DAC volume limits
[linux/fpc-iii.git] / include / asm-x86 / uaccess_32.h
blob8e7595c1f34e69959e42eb98a33b2a64b56ba848
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
4 /*
5 * User space memory access functions
6 */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/prefetch.h>
10 #include <linux/string.h>
11 #include <asm/asm.h>
12 #include <asm/page.h>
14 #define VERIFY_READ 0
15 #define VERIFY_WRITE 1
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.
22 * For historical reasons, these macros are grossly misnamed.
25 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
28 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
29 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
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))
35 #define segment_eq(a, b) ((a).seg == (b).seg)
38 * movsl can be slow when source and dest are not both 8-byte aligned
40 #ifdef CONFIG_X86_INTEL_USERCOPY
41 extern struct movsl_mask {
42 int mask;
43 } ____cacheline_aligned_in_smp movsl_mask;
44 #endif
46 #define __addr_ok(addr) \
47 ((unsigned long __force)(addr) < \
48 (current_thread_info()->addr_limit.seg))
51 * Test whether a block of memory is a valid user space address.
52 * Returns 0 if the range is valid, nonzero otherwise.
54 * This is equivalent to the following test:
55 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
57 * This needs 33-bit arithmetic. We have a carry...
59 #define __range_ok(addr, size) \
60 ({ \
61 unsigned long flag, roksum; \
62 __chk_user_ptr(addr); \
63 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
64 :"=&r" (flag), "=r" (roksum) \
65 :"1" (addr), "g" ((int)(size)), \
66 "rm" (current_thread_info()->addr_limit.seg)); \
67 flag; \
70 /**
71 * access_ok: - Checks if a user space pointer is valid
72 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
73 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
74 * to write to a block, it is always safe to read from it.
75 * @addr: User space pointer to start of block to check
76 * @size: Size of block to check
78 * Context: User context only. This function may sleep.
80 * Checks if a pointer to a block of memory in user space is valid.
82 * Returns true (nonzero) if the memory block may be valid, false (zero)
83 * if it is definitely invalid.
85 * Note that, depending on architecture, this function probably just
86 * checks that the pointer is in the user space range - after calling
87 * this function, memory access functions may still return -EFAULT.
89 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
92 * The exception table consists of pairs of addresses: the first is the
93 * address of an instruction that is allowed to fault, and the second is
94 * the address at which the program should continue. No registers are
95 * modified, so it is entirely up to the continuation code to figure out
96 * what to do.
98 * All the routines below use bits of fixup code that are out of line
99 * with the main instruction path. This means when everything is well,
100 * we don't even have to jump over them. Further, they do not intrude
101 * on our cache or tlb entries.
104 struct exception_table_entry {
105 unsigned long insn, fixup;
108 extern int fixup_exception(struct pt_regs *regs);
111 * These are the main single-value transfer routines. They automatically
112 * use the right size if we just have the right pointer type.
114 * This gets kind of ugly. We want to return _two_ values in "get_user()"
115 * and yet we don't want to do any pointers, because that is too much
116 * of a performance impact. Thus we have a few rather ugly macros here,
117 * and hide all the ugliness from the user.
119 * The "__xxx" versions of the user access functions are versions that
120 * do not verify the address space, that must have been done previously
121 * with a separate "access_ok()" call (this is used when we do multiple
122 * accesses to the same area of user memory).
125 extern void __get_user_1(void);
126 extern void __get_user_2(void);
127 extern void __get_user_4(void);
129 #define __get_user_x(size, ret, x, ptr) \
130 asm volatile("call __get_user_" #size \
131 :"=a" (ret),"=d" (x) \
132 :"0" (ptr))
135 /* Careful: we have to cast the result to the type of the pointer
136 * for sign reasons */
139 * get_user: - Get a simple variable from user space.
140 * @x: Variable to store result.
141 * @ptr: Source address, in user space.
143 * Context: User context only. This function may sleep.
145 * This macro copies a single simple variable from user space to kernel
146 * space. It supports simple types like char and int, but not larger
147 * data types like structures or arrays.
149 * @ptr must have pointer-to-simple-variable type, and the result of
150 * dereferencing @ptr must be assignable to @x without a cast.
152 * Returns zero on success, or -EFAULT on error.
153 * On error, the variable @x is set to zero.
155 #define get_user(x, ptr) \
156 ({ \
157 int __ret_gu; \
158 unsigned long __val_gu; \
159 __chk_user_ptr(ptr); \
160 switch (sizeof(*(ptr))) { \
161 case 1: \
162 __get_user_x(1, __ret_gu, __val_gu, ptr); \
163 break; \
164 case 2: \
165 __get_user_x(2, __ret_gu, __val_gu, ptr); \
166 break; \
167 case 4: \
168 __get_user_x(4, __ret_gu, __val_gu, ptr); \
169 break; \
170 default: \
171 __get_user_x(X, __ret_gu, __val_gu, ptr); \
172 break; \
174 (x) = (__typeof__(*(ptr)))__val_gu; \
175 __ret_gu; \
178 extern void __put_user_bad(void);
181 * Strange magic calling convention: pointer in %ecx,
182 * value in %eax(:%edx), return value in %eax, no clobbers.
184 extern void __put_user_1(void);
185 extern void __put_user_2(void);
186 extern void __put_user_4(void);
187 extern void __put_user_8(void);
189 #define __put_user_1(x, ptr) \
190 asm volatile("call __put_user_1" : "=a" (__ret_pu) \
191 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
193 #define __put_user_2(x, ptr) \
194 asm volatile("call __put_user_2" : "=a" (__ret_pu) \
195 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
197 #define __put_user_4(x, ptr) \
198 asm volatile("call __put_user_4" : "=a" (__ret_pu) \
199 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
201 #define __put_user_8(x, ptr) \
202 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
203 : "A" ((typeof(*(ptr)))(x)), "c" (ptr))
205 #define __put_user_X(x, ptr) \
206 asm volatile("call __put_user_X" : "=a" (__ret_pu) \
207 : "c" (ptr))
210 * put_user: - Write a simple value into user space.
211 * @x: Value to copy to user space.
212 * @ptr: Destination address, in user space.
214 * Context: User context only. This function may sleep.
216 * This macro copies a single simple value from kernel space to user
217 * space. It supports simple types like char and int, but not larger
218 * data types like structures or arrays.
220 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
221 * to the result of dereferencing @ptr.
223 * Returns zero on success, or -EFAULT on error.
225 #ifdef CONFIG_X86_WP_WORKS_OK
227 #define put_user(x, ptr) \
228 ({ \
229 int __ret_pu; \
230 __typeof__(*(ptr)) __pu_val; \
231 __chk_user_ptr(ptr); \
232 __pu_val = x; \
233 switch (sizeof(*(ptr))) { \
234 case 1: \
235 __put_user_1(__pu_val, ptr); \
236 break; \
237 case 2: \
238 __put_user_2(__pu_val, ptr); \
239 break; \
240 case 4: \
241 __put_user_4(__pu_val, ptr); \
242 break; \
243 case 8: \
244 __put_user_8(__pu_val, ptr); \
245 break; \
246 default: \
247 __put_user_X(__pu_val, ptr); \
248 break; \
250 __ret_pu; \
253 #else
254 #define put_user(x, ptr) \
255 ({ \
256 int __ret_pu; \
257 __typeof__(*(ptr))__pus_tmp = x; \
258 __ret_pu = 0; \
259 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
260 sizeof(*(ptr))) != 0)) \
261 __ret_pu = -EFAULT; \
262 __ret_pu; \
266 #endif
269 * __get_user: - Get a simple variable from user space, with less checking.
270 * @x: Variable to store result.
271 * @ptr: Source address, in user space.
273 * Context: User context only. This function may sleep.
275 * This macro copies a single simple variable from user space to kernel
276 * space. It supports simple types like char and int, but not larger
277 * data types like structures or arrays.
279 * @ptr must have pointer-to-simple-variable type, and the result of
280 * dereferencing @ptr must be assignable to @x without a cast.
282 * Caller must check the pointer with access_ok() before calling this
283 * function.
285 * Returns zero on success, or -EFAULT on error.
286 * On error, the variable @x is set to zero.
288 #define __get_user(x, ptr) \
289 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
293 * __put_user: - Write a simple value into user space, with less checking.
294 * @x: Value to copy to user space.
295 * @ptr: Destination address, in user space.
297 * Context: User context only. This function may sleep.
299 * This macro copies a single simple value from kernel space to user
300 * space. It supports simple types like char and int, but not larger
301 * data types like structures or arrays.
303 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
304 * to the result of dereferencing @ptr.
306 * Caller must check the pointer with access_ok() before calling this
307 * function.
309 * Returns zero on success, or -EFAULT on error.
311 #define __put_user(x, ptr) \
312 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
314 #define __put_user_nocheck(x, ptr, size) \
315 ({ \
316 long __pu_err; \
317 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
318 __pu_err; \
322 #define __put_user_u64(x, addr, err) \
323 asm volatile("1: movl %%eax,0(%2)\n" \
324 "2: movl %%edx,4(%2)\n" \
325 "3:\n" \
326 ".section .fixup,\"ax\"\n" \
327 "4: movl %3,%0\n" \
328 " jmp 3b\n" \
329 ".previous\n" \
330 _ASM_EXTABLE(1b, 4b) \
331 _ASM_EXTABLE(2b, 4b) \
332 : "=r" (err) \
333 : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
335 #ifdef CONFIG_X86_WP_WORKS_OK
337 #define __put_user_size(x, ptr, size, retval, errret) \
338 do { \
339 retval = 0; \
340 __chk_user_ptr(ptr); \
341 switch (size) { \
342 case 1: \
343 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
344 break; \
345 case 2: \
346 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
347 break; \
348 case 4: \
349 __put_user_asm(x, ptr, retval, "l", "", "ir", errret); \
350 break; \
351 case 8: \
352 __put_user_u64((__typeof__(*ptr))(x), ptr, retval); \
353 break; \
354 default: \
355 __put_user_bad(); \
357 } while (0)
359 #else
361 #define __put_user_size(x, ptr, size, retval, errret) \
362 do { \
363 __typeof__(*(ptr))__pus_tmp = x; \
364 retval = 0; \
366 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
367 retval = errret; \
368 } while (0)
370 #endif
371 struct __large_struct { unsigned long buf[100]; };
372 #define __m(x) (*(struct __large_struct __user *)(x))
375 * Tell gcc we read from memory instead of writing: this is because
376 * we do not write to any memory gcc knows about, so there are no
377 * aliasing issues.
379 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
380 asm volatile("1: mov"itype" %"rtype"1,%2\n" \
381 "2:\n" \
382 ".section .fixup,\"ax\"\n" \
383 "3: movl %3,%0\n" \
384 " jmp 2b\n" \
385 ".previous\n" \
386 _ASM_EXTABLE(1b, 3b) \
387 : "=r"(err) \
388 : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
391 #define __get_user_nocheck(x, ptr, size) \
392 ({ \
393 long __gu_err; \
394 unsigned long __gu_val; \
395 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
396 (x) = (__typeof__(*(ptr)))__gu_val; \
397 __gu_err; \
400 extern long __get_user_bad(void);
402 #define __get_user_size(x, ptr, size, retval, errret) \
403 do { \
404 retval = 0; \
405 __chk_user_ptr(ptr); \
406 switch (size) { \
407 case 1: \
408 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
409 break; \
410 case 2: \
411 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
412 break; \
413 case 4: \
414 __get_user_asm(x, ptr, retval, "l", "", "=r", errret); \
415 break; \
416 default: \
417 (x) = __get_user_bad(); \
419 } while (0)
421 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
422 asm volatile("1: mov"itype" %2,%"rtype"1\n" \
423 "2:\n" \
424 ".section .fixup,\"ax\"\n" \
425 "3: movl %3,%0\n" \
426 " xor"itype" %"rtype"1,%"rtype"1\n" \
427 " jmp 2b\n" \
428 ".previous\n" \
429 _ASM_EXTABLE(1b, 3b) \
430 : "=r" (err), ltype (x) \
431 : "m" (__m(addr)), "i" (errret), "0" (err))
434 unsigned long __must_check __copy_to_user_ll
435 (void __user *to, const void *from, unsigned long n);
436 unsigned long __must_check __copy_from_user_ll
437 (void *to, const void __user *from, unsigned long n);
438 unsigned long __must_check __copy_from_user_ll_nozero
439 (void *to, const void __user *from, unsigned long n);
440 unsigned long __must_check __copy_from_user_ll_nocache
441 (void *to, const void __user *from, unsigned long n);
442 unsigned long __must_check __copy_from_user_ll_nocache_nozero
443 (void *to, const void __user *from, unsigned long n);
446 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
447 * @to: Destination address, in user space.
448 * @from: Source address, in kernel space.
449 * @n: Number of bytes to copy.
451 * Context: User context only.
453 * Copy data from kernel space to user space. Caller must check
454 * the specified block with access_ok() before calling this function.
455 * The caller should also make sure he pins the user space address
456 * so that the we don't result in page fault and sleep.
458 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
459 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
460 * If a store crosses a page boundary and gets a fault, the x86 will not write
461 * anything, so this is accurate.
464 static __always_inline unsigned long __must_check
465 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
467 if (__builtin_constant_p(n)) {
468 unsigned long ret;
470 switch (n) {
471 case 1:
472 __put_user_size(*(u8 *)from, (u8 __user *)to,
473 1, ret, 1);
474 return ret;
475 case 2:
476 __put_user_size(*(u16 *)from, (u16 __user *)to,
477 2, ret, 2);
478 return ret;
479 case 4:
480 __put_user_size(*(u32 *)from, (u32 __user *)to,
481 4, ret, 4);
482 return ret;
485 return __copy_to_user_ll(to, from, n);
489 * __copy_to_user: - Copy a block of data into user space, with less checking.
490 * @to: Destination address, in user space.
491 * @from: Source address, in kernel space.
492 * @n: Number of bytes to copy.
494 * Context: User context only. This function may sleep.
496 * Copy data from kernel space to user space. Caller must check
497 * the specified block with access_ok() before calling this function.
499 * Returns number of bytes that could not be copied.
500 * On success, this will be zero.
502 static __always_inline unsigned long __must_check
503 __copy_to_user(void __user *to, const void *from, unsigned long n)
505 might_sleep();
506 return __copy_to_user_inatomic(to, from, n);
509 static __always_inline unsigned long
510 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
512 /* Avoid zeroing the tail if the copy fails..
513 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
514 * but as the zeroing behaviour is only significant when n is not
515 * constant, that shouldn't be a problem.
517 if (__builtin_constant_p(n)) {
518 unsigned long ret;
520 switch (n) {
521 case 1:
522 __get_user_size(*(u8 *)to, from, 1, ret, 1);
523 return ret;
524 case 2:
525 __get_user_size(*(u16 *)to, from, 2, ret, 2);
526 return ret;
527 case 4:
528 __get_user_size(*(u32 *)to, from, 4, ret, 4);
529 return ret;
532 return __copy_from_user_ll_nozero(to, from, n);
536 * __copy_from_user: - Copy a block of data from user space, with less checking.
537 * @to: Destination address, in kernel space.
538 * @from: Source address, in user space.
539 * @n: Number of bytes to copy.
541 * Context: User context only. This function may sleep.
543 * Copy data from user space to kernel space. Caller must check
544 * the specified block with access_ok() before calling this function.
546 * Returns number of bytes that could not be copied.
547 * On success, this will be zero.
549 * If some data could not be copied, this function will pad the copied
550 * data to the requested size using zero bytes.
552 * An alternate version - __copy_from_user_inatomic() - may be called from
553 * atomic context and will fail rather than sleep. In this case the
554 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
555 * for explanation of why this is needed.
557 static __always_inline unsigned long
558 __copy_from_user(void *to, const void __user *from, unsigned long n)
560 might_sleep();
561 if (__builtin_constant_p(n)) {
562 unsigned long ret;
564 switch (n) {
565 case 1:
566 __get_user_size(*(u8 *)to, from, 1, ret, 1);
567 return ret;
568 case 2:
569 __get_user_size(*(u16 *)to, from, 2, ret, 2);
570 return ret;
571 case 4:
572 __get_user_size(*(u32 *)to, from, 4, ret, 4);
573 return ret;
576 return __copy_from_user_ll(to, from, n);
579 #define ARCH_HAS_NOCACHE_UACCESS
581 static __always_inline unsigned long __copy_from_user_nocache(void *to,
582 const void __user *from, unsigned long n)
584 might_sleep();
585 if (__builtin_constant_p(n)) {
586 unsigned long ret;
588 switch (n) {
589 case 1:
590 __get_user_size(*(u8 *)to, from, 1, ret, 1);
591 return ret;
592 case 2:
593 __get_user_size(*(u16 *)to, from, 2, ret, 2);
594 return ret;
595 case 4:
596 __get_user_size(*(u32 *)to, from, 4, ret, 4);
597 return ret;
600 return __copy_from_user_ll_nocache(to, from, n);
603 static __always_inline unsigned long
604 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
605 unsigned long n)
607 return __copy_from_user_ll_nocache_nozero(to, from, n);
610 unsigned long __must_check copy_to_user(void __user *to,
611 const void *from, unsigned long n);
612 unsigned long __must_check copy_from_user(void *to,
613 const void __user *from,
614 unsigned long n);
615 long __must_check strncpy_from_user(char *dst, const char __user *src,
616 long count);
617 long __must_check __strncpy_from_user(char *dst,
618 const char __user *src, long count);
621 * strlen_user: - Get the size of a string in user space.
622 * @str: The string to measure.
624 * Context: User context only. This function may sleep.
626 * Get the size of a NUL-terminated string in user space.
628 * Returns the size of the string INCLUDING the terminating NUL.
629 * On exception, returns 0.
631 * If there is a limit on the length of a valid string, you may wish to
632 * consider using strnlen_user() instead.
634 #define strlen_user(str) strnlen_user(str, LONG_MAX)
636 long strnlen_user(const char __user *str, long n);
637 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
638 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
640 #endif /* __i386_UACCESS_H */