uaccess.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /* Returns 0 if exception not found and fixup otherwise. */
  59. extern unsigned long search_exception_table(unsigned long);
  60. /*
  61. * architectures with an MMU should override these two
  62. */
  63. #ifndef __copy_from_user
  64. static inline __must_check long __copy_from_user(void *to,
  65. const void __user * from, unsigned long n)
  66. {
  67. if (__builtin_constant_p(n)) {
  68. switch(n) {
  69. case 1:
  70. *(u8 *)to = *(u8 __force *)from;
  71. return 0;
  72. case 2:
  73. *(u16 *)to = *(u16 __force *)from;
  74. return 0;
  75. case 4:
  76. *(u32 *)to = *(u32 __force *)from;
  77. return 0;
  78. #ifdef CONFIG_64BIT
  79. case 8:
  80. *(u64 *)to = *(u64 __force *)from;
  81. return 0;
  82. #endif
  83. default:
  84. break;
  85. }
  86. }
  87. memcpy(to, (const void __force *)from, n);
  88. return 0;
  89. }
  90. #endif
  91. #ifndef __copy_to_user
  92. static inline __must_check long __copy_to_user(void __user *to,
  93. const void *from, unsigned long n)
  94. {
  95. if (__builtin_constant_p(n)) {
  96. switch(n) {
  97. case 1:
  98. *(u8 __force *)to = *(u8 *)from;
  99. return 0;
  100. case 2:
  101. *(u16 __force *)to = *(u16 *)from;
  102. return 0;
  103. case 4:
  104. *(u32 __force *)to = *(u32 *)from;
  105. return 0;
  106. #ifdef CONFIG_64BIT
  107. case 8:
  108. *(u64 __force *)to = *(u64 *)from;
  109. return 0;
  110. #endif
  111. default:
  112. break;
  113. }
  114. }
  115. memcpy((void __force *)to, from, n);
  116. return 0;
  117. }
  118. #endif
  119. /*
  120. * These are the main single-value transfer routines. They automatically
  121. * use the right size if we just have the right pointer type.
  122. * This version just falls back to copy_{from,to}_user, which should
  123. * provide a fast-path for small values.
  124. */
  125. #define __put_user(x, ptr) \
  126. ({ \
  127. __typeof__(*(ptr)) __x = (x); \
  128. int __pu_err = -EFAULT; \
  129. __chk_user_ptr(ptr); \
  130. switch (sizeof (*(ptr))) { \
  131. case 1: \
  132. case 2: \
  133. case 4: \
  134. case 8: \
  135. __pu_err = __put_user_fn(sizeof (*(ptr)), \
  136. ptr, &__x); \
  137. break; \
  138. default: \
  139. __put_user_bad(); \
  140. break; \
  141. } \
  142. __pu_err; \
  143. })
  144. #define put_user(x, ptr) \
  145. ({ \
  146. might_fault(); \
  147. access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ? \
  148. __put_user(x, ptr) : \
  149. -EFAULT; \
  150. })
  151. #ifndef __put_user_fn
  152. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  153. {
  154. size = __copy_to_user(ptr, x, size);
  155. return size ? -EFAULT : size;
  156. }
  157. #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
  158. #endif
  159. extern int __put_user_bad(void) __attribute__((noreturn));
  160. #define __get_user(x, ptr) \
  161. ({ \
  162. int __gu_err = -EFAULT; \
  163. __chk_user_ptr(ptr); \
  164. switch (sizeof(*(ptr))) { \
  165. case 1: { \
  166. unsigned char __x; \
  167. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  168. ptr, &__x); \
  169. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  170. break; \
  171. }; \
  172. case 2: { \
  173. unsigned short __x; \
  174. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  175. ptr, &__x); \
  176. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  177. break; \
  178. }; \
  179. case 4: { \
  180. unsigned int __x; \
  181. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  182. ptr, &__x); \
  183. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  184. break; \
  185. }; \
  186. case 8: { \
  187. unsigned long long __x; \
  188. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  189. ptr, &__x); \
  190. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  191. break; \
  192. }; \
  193. default: \
  194. __get_user_bad(); \
  195. break; \
  196. } \
  197. __gu_err; \
  198. })
  199. #define get_user(x, ptr) \
  200. ({ \
  201. might_fault(); \
  202. access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ? \
  203. __get_user(x, ptr) : \
  204. -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 = __copy_from_user(x, ptr, size);
  210. return size ? -EFAULT : size;
  211. }
  212. #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
  213. #endif
  214. extern int __get_user_bad(void) __attribute__((noreturn));
  215. #ifndef __copy_from_user_inatomic
  216. #define __copy_from_user_inatomic __copy_from_user
  217. #endif
  218. #ifndef __copy_to_user_inatomic
  219. #define __copy_to_user_inatomic __copy_to_user
  220. #endif
  221. static inline long copy_from_user(void *to,
  222. const void __user * from, unsigned long n)
  223. {
  224. might_fault();
  225. if (access_ok(VERIFY_READ, from, n))
  226. return __copy_from_user(to, from, n);
  227. else
  228. return n;
  229. }
  230. static inline long copy_to_user(void __user *to,
  231. const void *from, unsigned long n)
  232. {
  233. might_fault();
  234. if (access_ok(VERIFY_WRITE, to, n))
  235. return __copy_to_user(to, from, n);
  236. else
  237. return n;
  238. }
  239. /*
  240. * Copy a null terminated string from userspace.
  241. */
  242. #ifndef __strncpy_from_user
  243. static inline long
  244. __strncpy_from_user(char *dst, const char __user *src, long count)
  245. {
  246. char *tmp;
  247. strncpy(dst, (const char __force *)src, count);
  248. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  249. ;
  250. return (tmp - dst);
  251. }
  252. #endif
  253. static inline long
  254. strncpy_from_user(char *dst, const char __user *src, long count)
  255. {
  256. if (!access_ok(VERIFY_READ, src, 1))
  257. return -EFAULT;
  258. return __strncpy_from_user(dst, src, count);
  259. }
  260. /*
  261. * Return the size of a string (including the ending 0)
  262. *
  263. * Return 0 on exception, a value greater than N if too long
  264. */
  265. #ifndef __strnlen_user
  266. #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
  267. #endif
  268. /*
  269. * Unlike strnlen, strnlen_user includes the nul terminator in
  270. * its returned count. Callers should check for a returned value
  271. * greater than N as an indication the string is too long.
  272. */
  273. static inline long strnlen_user(const char __user *src, long n)
  274. {
  275. if (!access_ok(VERIFY_READ, src, 1))
  276. return 0;
  277. return __strnlen_user(src, n);
  278. }
  279. static inline long strlen_user(const char __user *src)
  280. {
  281. return strnlen_user(src, 32767);
  282. }
  283. /*
  284. * Zero Userspace
  285. */
  286. #ifndef __clear_user
  287. static inline __must_check unsigned long
  288. __clear_user(void __user *to, unsigned long n)
  289. {
  290. memset((void __force *)to, 0, n);
  291. return 0;
  292. }
  293. #endif
  294. static inline __must_check unsigned long
  295. clear_user(void __user *to, unsigned long n)
  296. {
  297. might_fault();
  298. if (!access_ok(VERIFY_WRITE, to, n))
  299. return n;
  300. return __clear_user(to, n);
  301. }
  302. #endif /* __ASM_GENERIC_UACCESS_H */