uaccess.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. void *__p = (ptr); \
  147. might_fault(); \
  148. access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
  149. __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
  150. -EFAULT; \
  151. })
  152. #ifndef __put_user_fn
  153. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  154. {
  155. size = __copy_to_user(ptr, x, size);
  156. return size ? -EFAULT : size;
  157. }
  158. #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
  159. #endif
  160. extern int __put_user_bad(void) __attribute__((noreturn));
  161. #define __get_user(x, ptr) \
  162. ({ \
  163. int __gu_err = -EFAULT; \
  164. __chk_user_ptr(ptr); \
  165. switch (sizeof(*(ptr))) { \
  166. case 1: { \
  167. unsigned char __x; \
  168. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  169. ptr, &__x); \
  170. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  171. break; \
  172. }; \
  173. case 2: { \
  174. unsigned short __x; \
  175. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  176. ptr, &__x); \
  177. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  178. break; \
  179. }; \
  180. case 4: { \
  181. unsigned int __x; \
  182. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  183. ptr, &__x); \
  184. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  185. break; \
  186. }; \
  187. case 8: { \
  188. unsigned long long __x; \
  189. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  190. ptr, &__x); \
  191. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  192. break; \
  193. }; \
  194. default: \
  195. __get_user_bad(); \
  196. break; \
  197. } \
  198. __gu_err; \
  199. })
  200. #define get_user(x, ptr) \
  201. ({ \
  202. const void *__p = (ptr); \
  203. might_fault(); \
  204. access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
  205. __get_user((x), (__typeof__(*(ptr)) *)__p) : \
  206. ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
  207. })
  208. #ifndef __get_user_fn
  209. static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
  210. {
  211. size_t n = __copy_from_user(x, ptr, size);
  212. if (unlikely(n)) {
  213. memset(x + (size - n), 0, n);
  214. return -EFAULT;
  215. }
  216. return 0;
  217. }
  218. #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
  219. #endif
  220. extern int __get_user_bad(void) __attribute__((noreturn));
  221. #ifndef __copy_from_user_inatomic
  222. #define __copy_from_user_inatomic __copy_from_user
  223. #endif
  224. #ifndef __copy_to_user_inatomic
  225. #define __copy_to_user_inatomic __copy_to_user
  226. #endif
  227. static inline long copy_from_user(void *to,
  228. const void __user * from, unsigned long n)
  229. {
  230. unsigned long res = n;
  231. might_fault();
  232. if (likely(access_ok(VERIFY_READ, from, n)))
  233. res = __copy_from_user(to, from, n);
  234. if (unlikely(res))
  235. memset(to + (n - res), 0, res);
  236. return res;
  237. }
  238. static inline long copy_to_user(void __user *to,
  239. const void *from, unsigned long n)
  240. {
  241. might_fault();
  242. if (access_ok(VERIFY_WRITE, to, n))
  243. return __copy_to_user(to, from, n);
  244. else
  245. return n;
  246. }
  247. /*
  248. * Copy a null terminated string from userspace.
  249. */
  250. #ifndef __strncpy_from_user
  251. static inline long
  252. __strncpy_from_user(char *dst, const char __user *src, long count)
  253. {
  254. char *tmp;
  255. strncpy(dst, (const char __force *)src, count);
  256. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  257. ;
  258. return (tmp - dst);
  259. }
  260. #endif
  261. static inline long
  262. strncpy_from_user(char *dst, const char __user *src, long count)
  263. {
  264. if (!access_ok(VERIFY_READ, src, 1))
  265. return -EFAULT;
  266. return __strncpy_from_user(dst, src, count);
  267. }
  268. /*
  269. * Return the size of a string (including the ending 0)
  270. *
  271. * Return 0 on exception, a value greater than N if too long
  272. */
  273. #ifndef __strnlen_user
  274. #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
  275. #endif
  276. /*
  277. * Unlike strnlen, strnlen_user includes the nul terminator in
  278. * its returned count. Callers should check for a returned value
  279. * greater than N as an indication the string is too long.
  280. */
  281. static inline long strnlen_user(const char __user *src, long n)
  282. {
  283. if (!access_ok(VERIFY_READ, src, 1))
  284. return 0;
  285. return __strnlen_user(src, n);
  286. }
  287. static inline long strlen_user(const char __user *src)
  288. {
  289. return strnlen_user(src, 32767);
  290. }
  291. /*
  292. * Zero Userspace
  293. */
  294. #ifndef __clear_user
  295. static inline __must_check unsigned long
  296. __clear_user(void __user *to, unsigned long n)
  297. {
  298. memset((void __force *)to, 0, n);
  299. return 0;
  300. }
  301. #endif
  302. static inline __must_check unsigned long
  303. clear_user(void __user *to, unsigned long n)
  304. {
  305. might_fault();
  306. if (!access_ok(VERIFY_WRITE, to, n))
  307. return n;
  308. return __clear_user(to, n);
  309. }
  310. #endif /* __ASM_GENERIC_UACCESS_H */