uaccess.h 7.2 KB

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