uaccess.h 7.4 KB

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