uaccess_32.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef _ASM_X86_UACCESS_32_H
  2. #define _ASM_X86_UACCESS_32_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/thread_info.h>
  8. #include <linux/string.h>
  9. #include <asm/asm.h>
  10. #include <asm/page.h>
  11. unsigned long __must_check __copy_to_user_ll
  12. (void __user *to, const void *from, unsigned long n);
  13. unsigned long __must_check __copy_from_user_ll
  14. (void *to, const void __user *from, unsigned long n);
  15. unsigned long __must_check __copy_from_user_ll_nozero
  16. (void *to, const void __user *from, unsigned long n);
  17. unsigned long __must_check __copy_from_user_ll_nocache
  18. (void *to, const void __user *from, unsigned long n);
  19. unsigned long __must_check __copy_from_user_ll_nocache_nozero
  20. (void *to, const void __user *from, unsigned long n);
  21. /**
  22. * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
  23. * @to: Destination address, in user space.
  24. * @from: Source address, in kernel space.
  25. * @n: Number of bytes to copy.
  26. *
  27. * Context: User context only.
  28. *
  29. * Copy data from kernel space to user space. Caller must check
  30. * the specified block with access_ok() before calling this function.
  31. * The caller should also make sure he pins the user space address
  32. * so that we don't result in page fault and sleep.
  33. *
  34. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  35. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  36. * If a store crosses a page boundary and gets a fault, the x86 will not write
  37. * anything, so this is accurate.
  38. */
  39. static __always_inline unsigned long __must_check
  40. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  41. {
  42. if (__builtin_constant_p(n)) {
  43. unsigned long ret;
  44. switch (n) {
  45. case 1:
  46. __put_user_size(*(u8 *)from, (u8 __user *)to,
  47. 1, ret, 1);
  48. return ret;
  49. case 2:
  50. __put_user_size(*(u16 *)from, (u16 __user *)to,
  51. 2, ret, 2);
  52. return ret;
  53. case 4:
  54. __put_user_size(*(u32 *)from, (u32 __user *)to,
  55. 4, ret, 4);
  56. return ret;
  57. }
  58. }
  59. return __copy_to_user_ll(to, from, n);
  60. }
  61. /**
  62. * __copy_to_user: - Copy a block of data into user space, with less checking.
  63. * @to: Destination address, in user space.
  64. * @from: Source address, in kernel space.
  65. * @n: Number of bytes to copy.
  66. *
  67. * Context: User context only. This function may sleep.
  68. *
  69. * Copy data from kernel space to user space. Caller must check
  70. * the specified block with access_ok() before calling this function.
  71. *
  72. * Returns number of bytes that could not be copied.
  73. * On success, this will be zero.
  74. */
  75. static __always_inline unsigned long __must_check
  76. __copy_to_user(void __user *to, const void *from, unsigned long n)
  77. {
  78. might_fault();
  79. return __copy_to_user_inatomic(to, from, n);
  80. }
  81. static __always_inline unsigned long
  82. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  83. {
  84. /* Avoid zeroing the tail if the copy fails..
  85. * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
  86. * but as the zeroing behaviour is only significant when n is not
  87. * constant, that shouldn't be a problem.
  88. */
  89. if (__builtin_constant_p(n)) {
  90. unsigned long ret;
  91. switch (n) {
  92. case 1:
  93. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  94. return ret;
  95. case 2:
  96. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  97. return ret;
  98. case 4:
  99. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  100. return ret;
  101. }
  102. }
  103. return __copy_from_user_ll_nozero(to, from, n);
  104. }
  105. /**
  106. * __copy_from_user: - Copy a block of data from user space, with less checking.
  107. * @to: Destination address, in kernel space.
  108. * @from: Source address, in user space.
  109. * @n: Number of bytes to copy.
  110. *
  111. * Context: User context only. This function may sleep.
  112. *
  113. * Copy data from user space to kernel space. Caller must check
  114. * the specified block with access_ok() before calling this function.
  115. *
  116. * Returns number of bytes that could not be copied.
  117. * On success, this will be zero.
  118. *
  119. * If some data could not be copied, this function will pad the copied
  120. * data to the requested size using zero bytes.
  121. *
  122. * An alternate version - __copy_from_user_inatomic() - may be called from
  123. * atomic context and will fail rather than sleep. In this case the
  124. * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
  125. * for explanation of why this is needed.
  126. */
  127. static __always_inline unsigned long
  128. __copy_from_user(void *to, const void __user *from, unsigned long n)
  129. {
  130. might_fault();
  131. if (__builtin_constant_p(n)) {
  132. unsigned long ret;
  133. switch (n) {
  134. case 1:
  135. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  136. return ret;
  137. case 2:
  138. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  139. return ret;
  140. case 4:
  141. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  142. return ret;
  143. }
  144. }
  145. return __copy_from_user_ll(to, from, n);
  146. }
  147. static __always_inline unsigned long __copy_from_user_nocache(void *to,
  148. const void __user *from, unsigned long n)
  149. {
  150. might_fault();
  151. if (__builtin_constant_p(n)) {
  152. unsigned long ret;
  153. switch (n) {
  154. case 1:
  155. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  156. return ret;
  157. case 2:
  158. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  159. return ret;
  160. case 4:
  161. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  162. return ret;
  163. }
  164. }
  165. return __copy_from_user_ll_nocache(to, from, n);
  166. }
  167. static __always_inline unsigned long
  168. __copy_from_user_inatomic_nocache(void *to, const void __user *from,
  169. unsigned long n)
  170. {
  171. return __copy_from_user_ll_nocache_nozero(to, from, n);
  172. }
  173. unsigned long __must_check copy_to_user(void __user *to,
  174. const void *from, unsigned long n);
  175. unsigned long __must_check _copy_from_user(void *to,
  176. const void __user *from,
  177. unsigned long n);
  178. extern void copy_from_user_overflow(void)
  179. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  180. __compiletime_error("copy_from_user() buffer size is not provably correct")
  181. #else
  182. __compiletime_warning("copy_from_user() buffer size is not provably correct")
  183. #endif
  184. ;
  185. static inline unsigned long __must_check copy_from_user(void *to,
  186. const void __user *from,
  187. unsigned long n)
  188. {
  189. int sz = __compiletime_object_size(to);
  190. if (likely(sz == -1 || sz >= n))
  191. n = _copy_from_user(to, from, n);
  192. else
  193. copy_from_user_overflow();
  194. return n;
  195. }
  196. /**
  197. * strlen_user: - Get the size of a string in user space.
  198. * @str: The string to measure.
  199. *
  200. * Context: User context only. This function may sleep.
  201. *
  202. * Get the size of a NUL-terminated string in user space.
  203. *
  204. * Returns the size of the string INCLUDING the terminating NUL.
  205. * On exception, returns 0.
  206. *
  207. * If there is a limit on the length of a valid string, you may wish to
  208. * consider using strnlen_user() instead.
  209. */
  210. #define strlen_user(str) strnlen_user(str, LONG_MAX)
  211. long strnlen_user(const char __user *str, long n);
  212. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  213. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  214. #endif /* _ASM_X86_UACCESS_32_H */