uaccess.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/sched.h>
  9. #include <linux/string.h>
  10. #include <asm/segment.h>
  11. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  12. #ifndef KERNEL_DS
  13. #define KERNEL_DS MAKE_MM_SEG(~0UL)
  14. #endif
  15. #ifndef USER_DS
  16. #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
  17. #endif
  18. #ifndef get_fs
  19. #define get_ds() (KERNEL_DS)
  20. #define get_fs() (current_thread_info()->addr_limit)
  21. static inline void set_fs(mm_segment_t fs)
  22. {
  23. current_thread_info()->addr_limit = fs;
  24. }
  25. #endif
  26. #ifndef segment_eq
  27. #define segment_eq(a, b) ((a).seg == (b).seg)
  28. #endif
  29. #define VERIFY_READ 0
  30. #define VERIFY_WRITE 1
  31. #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
  32. /*
  33. * The architecture should really override this if possible, at least
  34. * doing a check on the get_fs()
  35. */
  36. #ifndef __access_ok
  37. static inline int __access_ok(unsigned long addr, unsigned long size)
  38. {
  39. return 1;
  40. }
  41. #endif
  42. /*
  43. * The exception table consists of pairs of addresses: the first is the
  44. * address of an instruction that is allowed to fault, and the second is
  45. * the address at which the program should continue. No registers are
  46. * modified, so it is entirely up to the continuation code to figure out
  47. * what to do.
  48. *
  49. * All the routines below use bits of fixup code that are out of line
  50. * with the main instruction path. This means when everything is well,
  51. * we don't even have to jump over them. Further, they do not intrude
  52. * on our cache or tlb entries.
  53. */
  54. struct exception_table_entry
  55. {
  56. unsigned long insn, fixup;
  57. };
  58. /*
  59. * architectures with an MMU should override these two
  60. */
  61. #ifndef __copy_from_user
  62. static inline __must_check long __copy_from_user(void *to,
  63. const void __user * from, unsigned long n)
  64. {
  65. if (__builtin_constant_p(n)) {
  66. switch(n) {
  67. case 1:
  68. *(u8 *)to = *(u8 __force *)from;
  69. return 0;
  70. case 2:
  71. *(u16 *)to = *(u16 __force *)from;
  72. return 0;
  73. case 4:
  74. *(u32 *)to = *(u32 __force *)from;
  75. return 0;
  76. #ifdef CONFIG_64BIT
  77. case 8:
  78. *(u64 *)to = *(u64 __force *)from;
  79. return 0;
  80. #endif
  81. default:
  82. break;
  83. }
  84. }
  85. memcpy(to, (const void __force *)from, n);
  86. return 0;
  87. }
  88. #endif
  89. #ifndef __copy_to_user
  90. static inline __must_check long __copy_to_user(void __user *to,
  91. const void *from, unsigned long n)
  92. {
  93. if (__builtin_constant_p(n)) {
  94. switch(n) {
  95. case 1:
  96. *(u8 __force *)to = *(u8 *)from;
  97. return 0;
  98. case 2:
  99. *(u16 __force *)to = *(u16 *)from;
  100. return 0;
  101. case 4:
  102. *(u32 __force *)to = *(u32 *)from;
  103. return 0;
  104. #ifdef CONFIG_64BIT
  105. case 8:
  106. *(u64 __force *)to = *(u64 *)from;
  107. return 0;
  108. #endif
  109. default:
  110. break;
  111. }
  112. }
  113. memcpy((void __force *)to, from, n);
  114. return 0;
  115. }
  116. #endif
  117. /*
  118. * These are the main single-value transfer routines. They automatically
  119. * use the right size if we just have the right pointer type.
  120. * This version just falls back to copy_{from,to}_user, which should
  121. * provide a fast-path for small values.
  122. */
  123. #define __put_user(x, ptr) \
  124. ({ \
  125. __typeof__(*(ptr)) __x = (x); \
  126. int __pu_err = -EFAULT; \
  127. __chk_user_ptr(ptr); \
  128. switch (sizeof (*(ptr))) { \
  129. case 1: \
  130. case 2: \
  131. case 4: \
  132. case 8: \
  133. __pu_err = __put_user_fn(sizeof (*(ptr)), \
  134. ptr, &__x); \
  135. break; \
  136. default: \
  137. __put_user_bad(); \
  138. break; \
  139. } \
  140. __pu_err; \
  141. })
  142. #define put_user(x, ptr) \
  143. ({ \
  144. void *__p = (ptr); \
  145. might_fault(); \
  146. access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
  147. __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
  148. -EFAULT; \
  149. })
  150. #ifndef __put_user_fn
  151. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  152. {
  153. size = __copy_to_user(ptr, x, size);
  154. return size ? -EFAULT : size;
  155. }
  156. #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
  157. #endif
  158. extern int __put_user_bad(void) __attribute__((noreturn));
  159. #define __get_user(x, ptr) \
  160. ({ \
  161. int __gu_err = -EFAULT; \
  162. __chk_user_ptr(ptr); \
  163. switch (sizeof(*(ptr))) { \
  164. case 1: { \
  165. unsigned char __x; \
  166. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  167. ptr, &__x); \
  168. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  169. break; \
  170. }; \
  171. case 2: { \
  172. unsigned short __x; \
  173. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  174. ptr, &__x); \
  175. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  176. break; \
  177. }; \
  178. case 4: { \
  179. unsigned int __x; \
  180. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  181. ptr, &__x); \
  182. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  183. break; \
  184. }; \
  185. case 8: { \
  186. unsigned long long __x; \
  187. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  188. ptr, &__x); \
  189. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  190. break; \
  191. }; \
  192. default: \
  193. __get_user_bad(); \
  194. break; \
  195. } \
  196. __gu_err; \
  197. })
  198. #define get_user(x, ptr) \
  199. ({ \
  200. const void *__p = (ptr); \
  201. might_fault(); \
  202. access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
  203. __get_user((x), (__typeof__(*(ptr)) *)__p) : \
  204. ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
  205. })
  206. #ifndef __get_user_fn
  207. static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
  208. {
  209. size_t n = __copy_from_user(x, ptr, size);
  210. if (unlikely(n)) {
  211. memset(x + (size - n), 0, n);
  212. return -EFAULT;
  213. }
  214. return 0;
  215. }
  216. #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
  217. #endif
  218. extern int __get_user_bad(void) __attribute__((noreturn));
  219. #ifndef __copy_from_user_inatomic
  220. #define __copy_from_user_inatomic __copy_from_user
  221. #endif
  222. #ifndef __copy_to_user_inatomic
  223. #define __copy_to_user_inatomic __copy_to_user
  224. #endif
  225. static inline long copy_from_user(void *to,
  226. const void __user * from, unsigned long n)
  227. {
  228. unsigned long res = n;
  229. might_fault();
  230. if (likely(access_ok(VERIFY_READ, from, n)))
  231. res = __copy_from_user(to, from, n);
  232. if (unlikely(res))
  233. memset(to + (n - res), 0, res);
  234. return res;
  235. }
  236. static inline long copy_to_user(void __user *to,
  237. const void *from, unsigned long n)
  238. {
  239. might_fault();
  240. if (access_ok(VERIFY_WRITE, to, n))
  241. return __copy_to_user(to, from, n);
  242. else
  243. return n;
  244. }
  245. /*
  246. * Copy a null terminated string from userspace.
  247. */
  248. #ifndef __strncpy_from_user
  249. static inline long
  250. __strncpy_from_user(char *dst, const char __user *src, long count)
  251. {
  252. char *tmp;
  253. strncpy(dst, (const char __force *)src, count);
  254. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  255. ;
  256. return (tmp - dst);
  257. }
  258. #endif
  259. static inline long
  260. strncpy_from_user(char *dst, const char __user *src, long count)
  261. {
  262. if (!access_ok(VERIFY_READ, src, 1))
  263. return -EFAULT;
  264. return __strncpy_from_user(dst, src, count);
  265. }
  266. /*
  267. * Return the size of a string (including the ending 0)
  268. *
  269. * Return 0 on exception, a value greater than N if too long
  270. */
  271. #ifndef __strnlen_user
  272. #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
  273. #endif
  274. /*
  275. * Unlike strnlen, strnlen_user includes the nul terminator in
  276. * its returned count. Callers should check for a returned value
  277. * greater than N as an indication the string is too long.
  278. */
  279. static inline long strnlen_user(const char __user *src, long n)
  280. {
  281. if (!access_ok(VERIFY_READ, src, 1))
  282. return 0;
  283. return __strnlen_user(src, n);
  284. }
  285. static inline long strlen_user(const char __user *src)
  286. {
  287. return strnlen_user(src, 32767);
  288. }
  289. /*
  290. * Zero Userspace
  291. */
  292. #ifndef __clear_user
  293. static inline __must_check unsigned long
  294. __clear_user(void __user *to, unsigned long n)
  295. {
  296. memset((void __force *)to, 0, n);
  297. return 0;
  298. }
  299. #endif
  300. static inline __must_check unsigned long
  301. clear_user(void __user *to, unsigned long n)
  302. {
  303. might_fault();
  304. if (!access_ok(VERIFY_WRITE, to, n))
  305. return n;
  306. return __clear_user(to, n);
  307. }
  308. #endif /* __ASM_GENERIC_UACCESS_H */