uaccess.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef _ASM_UACCES_H_
  2. #define _ASM_UACCES_H_
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/compiler.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>
  13. #define VERIFY_READ 0
  14. #define VERIFY_WRITE 1
  15. /*
  16. * The fs value determines whether argument validity checking should be
  17. * performed or not. If get_fs() == USER_DS, checking is performed, with
  18. * get_fs() == KERNEL_DS, checking is bypassed.
  19. *
  20. * For historical reasons, these macros are grossly misnamed.
  21. */
  22. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  23. #define KERNEL_DS MAKE_MM_SEG(-1UL)
  24. #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
  25. #define get_ds() (KERNEL_DS)
  26. #define get_fs() (current_thread_info()->addr_limit)
  27. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  28. #define segment_eq(a, b) ((a).seg == (b).seg)
  29. /*
  30. * Test whether a block of memory is a valid user space address.
  31. * Returns 0 if the range is valid, nonzero otherwise.
  32. *
  33. * This is equivalent to the following test:
  34. * (u33)addr + (u33)size >= (u33)current->addr_limit.seg (u65 for x86_64)
  35. *
  36. * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
  37. */
  38. #define __range_not_ok(addr, size) \
  39. ({ \
  40. unsigned long flag, roksum; \
  41. __chk_user_ptr(addr); \
  42. asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \
  43. : "=&r" (flag), "=r" (roksum) \
  44. : "1" (addr), "g" ((long)(size)), \
  45. "rm" (current_thread_info()->addr_limit.seg)); \
  46. flag; \
  47. })
  48. /**
  49. * access_ok: - Checks if a user space pointer is valid
  50. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  51. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  52. * to write to a block, it is always safe to read from it.
  53. * @addr: User space pointer to start of block to check
  54. * @size: Size of block to check
  55. *
  56. * Context: User context only. This function may sleep.
  57. *
  58. * Checks if a pointer to a block of memory in user space is valid.
  59. *
  60. * Returns true (nonzero) if the memory block may be valid, false (zero)
  61. * if it is definitely invalid.
  62. *
  63. * Note that, depending on architecture, this function probably just
  64. * checks that the pointer is in the user space range - after calling
  65. * this function, memory access functions may still return -EFAULT.
  66. */
  67. #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
  68. /*
  69. * The exception table consists of pairs of addresses: the first is the
  70. * address of an instruction that is allowed to fault, and the second is
  71. * the address at which the program should continue. No registers are
  72. * modified, so it is entirely up to the continuation code to figure out
  73. * what to do.
  74. *
  75. * All the routines below use bits of fixup code that are out of line
  76. * with the main instruction path. This means when everything is well,
  77. * we don't even have to jump over them. Further, they do not intrude
  78. * on our cache or tlb entries.
  79. */
  80. struct exception_table_entry {
  81. unsigned long insn, fixup;
  82. };
  83. extern int fixup_exception(struct pt_regs *regs);
  84. /*
  85. * These are the main single-value transfer routines. They automatically
  86. * use the right size if we just have the right pointer type.
  87. *
  88. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  89. * and yet we don't want to do any pointers, because that is too much
  90. * of a performance impact. Thus we have a few rather ugly macros here,
  91. * and hide all the ugliness from the user.
  92. *
  93. * The "__xxx" versions of the user access functions are versions that
  94. * do not verify the address space, that must have been done previously
  95. * with a separate "access_ok()" call (this is used when we do multiple
  96. * accesses to the same area of user memory).
  97. */
  98. extern int __get_user_1(void);
  99. extern int __get_user_2(void);
  100. extern int __get_user_4(void);
  101. extern int __get_user_8(void);
  102. extern int __get_user_bad(void);
  103. #define __get_user_x(size, ret, x, ptr) \
  104. asm volatile("call __get_user_" #size \
  105. : "=a" (ret),"=d" (x) \
  106. : "0" (ptr)) \
  107. /* Careful: we have to cast the result to the type of the pointer
  108. * for sign reasons */
  109. /**
  110. * get_user: - Get a simple variable from user space.
  111. * @x: Variable to store result.
  112. * @ptr: Source address, in user space.
  113. *
  114. * Context: User context only. This function may sleep.
  115. *
  116. * This macro copies a single simple variable from user space to kernel
  117. * space. It supports simple types like char and int, but not larger
  118. * data types like structures or arrays.
  119. *
  120. * @ptr must have pointer-to-simple-variable type, and the result of
  121. * dereferencing @ptr must be assignable to @x without a cast.
  122. *
  123. * Returns zero on success, or -EFAULT on error.
  124. * On error, the variable @x is set to zero.
  125. */
  126. #ifdef CONFIG_X86_32
  127. #define __get_user_8(__ret_gu, __val_gu, ptr) \
  128. __get_user_x(X, __ret_gu, __val_gu, ptr)
  129. #else
  130. #define __get_user_8(__ret_gu, __val_gu, ptr) \
  131. __get_user_x(8, __ret_gu, __val_gu, ptr)
  132. #endif
  133. #define get_user(x, ptr) \
  134. ({ \
  135. int __ret_gu; \
  136. unsigned long __val_gu; \
  137. __chk_user_ptr(ptr); \
  138. switch (sizeof(*(ptr))) { \
  139. case 1: \
  140. __get_user_x(1, __ret_gu, __val_gu, ptr); \
  141. break; \
  142. case 2: \
  143. __get_user_x(2, __ret_gu, __val_gu, ptr); \
  144. break; \
  145. case 4: \
  146. __get_user_x(4, __ret_gu, __val_gu, ptr); \
  147. break; \
  148. case 8: \
  149. __get_user_8(__ret_gu, __val_gu, ptr); \
  150. break; \
  151. default: \
  152. __get_user_x(X, __ret_gu, __val_gu, ptr); \
  153. break; \
  154. } \
  155. (x) = (__typeof__(*(ptr)))__val_gu; \
  156. __ret_gu; \
  157. })
  158. #ifdef CONFIG_X86_32
  159. # include "uaccess_32.h"
  160. #else
  161. # include "uaccess_64.h"
  162. #endif
  163. #endif