uaccess.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef __ASM_GENERIC_UACCESS_H
  2. #define __ASM_GENERIC_UACCESS_H
  3. /*
  4. * User space memory access functions, these should work
  5. * on any machine that has kernel and user data in the same
  6. * address space, e.g. all NOMMU machines.
  7. */
  8. #include <linux/string.h>
  9. #include <asm/segment.h>
  10. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  11. #ifndef KERNEL_DS
  12. #define KERNEL_DS MAKE_MM_SEG(~0UL)
  13. #endif
  14. #ifndef USER_DS
  15. #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
  16. #endif
  17. #ifndef get_fs
  18. #define get_ds() (KERNEL_DS)
  19. #define get_fs() (current_thread_info()->addr_limit)
  20. static inline void set_fs(mm_segment_t fs)
  21. {
  22. current_thread_info()->addr_limit = fs;
  23. }
  24. #endif
  25. #ifndef segment_eq
  26. #define segment_eq(a, b) ((a).seg == (b).seg)
  27. #endif
  28. #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
  29. /*
  30. * The architecture should really override this if possible, at least
  31. * doing a check on the get_fs()
  32. */
  33. #ifndef __access_ok
  34. static inline int __access_ok(unsigned long addr, unsigned long size)
  35. {
  36. return 1;
  37. }
  38. #endif
  39. /*
  40. * These are the main single-value transfer routines. They automatically
  41. * use the right size if we just have the right pointer type.
  42. * This version just falls back to copy_{from,to}_user, which should
  43. * provide a fast-path for small values.
  44. */
  45. #define __put_user(x, ptr) \
  46. ({ \
  47. __typeof__(*(ptr)) __x = (x); \
  48. int __pu_err = -EFAULT; \
  49. __chk_user_ptr(ptr); \
  50. switch (sizeof (*(ptr))) { \
  51. case 1: \
  52. case 2: \
  53. case 4: \
  54. case 8: \
  55. __pu_err = __put_user_fn(sizeof (*(ptr)), \
  56. ptr, &__x); \
  57. break; \
  58. default: \
  59. __put_user_bad(); \
  60. break; \
  61. } \
  62. __pu_err; \
  63. })
  64. #define put_user(x, ptr) \
  65. ({ \
  66. void *__p = (ptr); \
  67. might_fault(); \
  68. access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
  69. __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
  70. -EFAULT; \
  71. })
  72. #ifndef __put_user_fn
  73. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  74. {
  75. return unlikely(__copy_to_user(ptr, x, size)) ? -EFAULT : 0;
  76. }
  77. #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
  78. #endif
  79. extern int __put_user_bad(void) __attribute__((noreturn));
  80. #define __get_user(x, ptr) \
  81. ({ \
  82. int __gu_err = -EFAULT; \
  83. __chk_user_ptr(ptr); \
  84. switch (sizeof(*(ptr))) { \
  85. case 1: { \
  86. unsigned char __x = 0; \
  87. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  88. ptr, &__x); \
  89. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  90. break; \
  91. }; \
  92. case 2: { \
  93. unsigned short __x = 0; \
  94. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  95. ptr, &__x); \
  96. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  97. break; \
  98. }; \
  99. case 4: { \
  100. unsigned int __x = 0; \
  101. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  102. ptr, &__x); \
  103. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  104. break; \
  105. }; \
  106. case 8: { \
  107. unsigned long long __x = 0; \
  108. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  109. ptr, &__x); \
  110. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  111. break; \
  112. }; \
  113. default: \
  114. __get_user_bad(); \
  115. break; \
  116. } \
  117. __gu_err; \
  118. })
  119. #define get_user(x, ptr) \
  120. ({ \
  121. const void *__p = (ptr); \
  122. might_fault(); \
  123. access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
  124. __get_user((x), (__typeof__(*(ptr)) *)__p) : \
  125. ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
  126. })
  127. #ifndef __get_user_fn
  128. static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
  129. {
  130. return unlikely(__copy_from_user(x, ptr, size)) ? -EFAULT : 0;
  131. }
  132. #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
  133. #endif
  134. extern int __get_user_bad(void) __attribute__((noreturn));
  135. #ifndef __copy_from_user_inatomic
  136. #define __copy_from_user_inatomic __copy_from_user
  137. #endif
  138. #ifndef __copy_to_user_inatomic
  139. #define __copy_to_user_inatomic __copy_to_user
  140. #endif
  141. static inline long copy_from_user(void *to,
  142. const void __user * from, unsigned long n)
  143. {
  144. unsigned long res = n;
  145. might_fault();
  146. if (likely(access_ok(VERIFY_READ, from, n)))
  147. res = __copy_from_user(to, from, n);
  148. if (unlikely(res))
  149. memset(to + (n - res), 0, res);
  150. return res;
  151. }
  152. static inline long copy_to_user(void __user *to,
  153. const void *from, unsigned long n)
  154. {
  155. might_fault();
  156. if (access_ok(VERIFY_WRITE, to, n))
  157. return __copy_to_user(to, from, n);
  158. else
  159. return n;
  160. }
  161. /*
  162. * Copy a null terminated string from userspace.
  163. */
  164. #ifndef __strncpy_from_user
  165. static inline long
  166. __strncpy_from_user(char *dst, const char __user *src, long count)
  167. {
  168. char *tmp;
  169. strncpy(dst, (const char __force *)src, count);
  170. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  171. ;
  172. return (tmp - dst);
  173. }
  174. #endif
  175. static inline long
  176. strncpy_from_user(char *dst, const char __user *src, long count)
  177. {
  178. if (!access_ok(VERIFY_READ, src, 1))
  179. return -EFAULT;
  180. return __strncpy_from_user(dst, src, count);
  181. }
  182. /*
  183. * Return the size of a string (including the ending 0)
  184. *
  185. * Return 0 on exception, a value greater than N if too long
  186. */
  187. #ifndef __strnlen_user
  188. #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
  189. #endif
  190. /*
  191. * Unlike strnlen, strnlen_user includes the nul terminator in
  192. * its returned count. Callers should check for a returned value
  193. * greater than N as an indication the string is too long.
  194. */
  195. static inline long strnlen_user(const char __user *src, long n)
  196. {
  197. if (!access_ok(VERIFY_READ, src, 1))
  198. return 0;
  199. return __strnlen_user(src, n);
  200. }
  201. static inline long strlen_user(const char __user *src)
  202. {
  203. return strnlen_user(src, 32767);
  204. }
  205. /*
  206. * Zero Userspace
  207. */
  208. #ifndef __clear_user
  209. static inline __must_check unsigned long
  210. __clear_user(void __user *to, unsigned long n)
  211. {
  212. memset((void __force *)to, 0, n);
  213. return 0;
  214. }
  215. #endif
  216. static inline __must_check unsigned long
  217. clear_user(void __user *to, unsigned long n)
  218. {
  219. might_fault();
  220. if (!access_ok(VERIFY_WRITE, to, n))
  221. return n;
  222. return __clear_user(to, n);
  223. }
  224. #include <asm/extable.h>
  225. #endif /* __ASM_GENERIC_UACCESS_H */