uaccess.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * include/asm-s390/uaccess.h
  3. *
  4. * S390 version
  5. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Hartmut Penner (hp@de.ibm.com),
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. *
  9. * Derived from "include/asm-i386/uaccess.h"
  10. */
  11. #ifndef __S390_UACCESS_H
  12. #define __S390_UACCESS_H
  13. /*
  14. * User space memory access functions
  15. */
  16. #include <linux/sched.h>
  17. #include <linux/errno.h>
  18. #define VERIFY_READ 0
  19. #define VERIFY_WRITE 1
  20. /*
  21. * The fs value determines whether argument validity checking should be
  22. * performed or not. If get_fs() == USER_DS, checking is performed, with
  23. * get_fs() == KERNEL_DS, checking is bypassed.
  24. *
  25. * For historical reasons, these macros are grossly misnamed.
  26. */
  27. #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
  28. #define KERNEL_DS MAKE_MM_SEG(0)
  29. #define USER_DS MAKE_MM_SEG(1)
  30. #define get_ds() (KERNEL_DS)
  31. #define get_fs() (current->thread.mm_segment)
  32. #ifdef __s390x__
  33. #define set_fs(x) \
  34. ({ \
  35. unsigned long __pto; \
  36. current->thread.mm_segment = (x); \
  37. __pto = current->thread.mm_segment.ar4 ? \
  38. S390_lowcore.user_asce : S390_lowcore.kernel_asce; \
  39. asm volatile ("lctlg 7,7,%0" : : "m" (__pto) ); \
  40. })
  41. #else /* __s390x__ */
  42. #define set_fs(x) \
  43. ({ \
  44. unsigned long __pto; \
  45. current->thread.mm_segment = (x); \
  46. __pto = current->thread.mm_segment.ar4 ? \
  47. S390_lowcore.user_asce : S390_lowcore.kernel_asce; \
  48. asm volatile ("lctl 7,7,%0" : : "m" (__pto) ); \
  49. })
  50. #endif /* __s390x__ */
  51. #define segment_eq(a,b) ((a).ar4 == (b).ar4)
  52. static inline int __access_ok(const void __user *addr, unsigned long size)
  53. {
  54. return 1;
  55. }
  56. #define access_ok(type,addr,size) __access_ok(addr,size)
  57. /*
  58. * The exception table consists of pairs of addresses: the first is the
  59. * address of an instruction that is allowed to fault, and the second is
  60. * the address at which the program should continue. No registers are
  61. * modified, so it is entirely up to the continuation code to figure out
  62. * what to do.
  63. *
  64. * All the routines below use bits of fixup code that are out of line
  65. * with the main instruction path. This means when everything is well,
  66. * we don't even have to jump over them. Further, they do not intrude
  67. * on our cache or tlb entries.
  68. */
  69. struct exception_table_entry
  70. {
  71. unsigned long insn, fixup;
  72. };
  73. struct uaccess_ops {
  74. size_t (*copy_from_user)(size_t, const void __user *, void *);
  75. size_t (*copy_from_user_small)(size_t, const void __user *, void *);
  76. size_t (*copy_to_user)(size_t, void __user *, const void *);
  77. size_t (*copy_to_user_small)(size_t, void __user *, const void *);
  78. size_t (*copy_in_user)(size_t, void __user *, const void __user *);
  79. size_t (*clear_user)(size_t, void __user *);
  80. size_t (*strnlen_user)(size_t, const char __user *);
  81. size_t (*strncpy_from_user)(size_t, const char __user *, char *);
  82. int (*futex_atomic_op)(int op, int __user *, int oparg, int *old);
  83. int (*futex_atomic_cmpxchg)(int __user *, int old, int new);
  84. };
  85. extern struct uaccess_ops uaccess;
  86. extern struct uaccess_ops uaccess_std;
  87. extern struct uaccess_ops uaccess_mvcos;
  88. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  89. {
  90. size = uaccess.copy_to_user_small(size, ptr, x);
  91. return size ? -EFAULT : size;
  92. }
  93. static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
  94. {
  95. size = uaccess.copy_from_user_small(size, ptr, x);
  96. return size ? -EFAULT : size;
  97. }
  98. /*
  99. * These are the main single-value transfer routines. They automatically
  100. * use the right size if we just have the right pointer type.
  101. */
  102. #define __put_user(x, ptr) \
  103. ({ \
  104. __typeof__(*(ptr)) __x = (x); \
  105. int __pu_err = -EFAULT; \
  106. __chk_user_ptr(ptr); \
  107. switch (sizeof (*(ptr))) { \
  108. case 1: \
  109. case 2: \
  110. case 4: \
  111. case 8: \
  112. __pu_err = __put_user_fn(sizeof (*(ptr)), \
  113. ptr, &__x); \
  114. break; \
  115. default: \
  116. __put_user_bad(); \
  117. break; \
  118. } \
  119. __pu_err; \
  120. })
  121. #define put_user(x, ptr) \
  122. ({ \
  123. might_sleep(); \
  124. __put_user(x, ptr); \
  125. })
  126. extern int __put_user_bad(void) __attribute__((noreturn));
  127. #define __get_user(x, ptr) \
  128. ({ \
  129. int __gu_err = -EFAULT; \
  130. __chk_user_ptr(ptr); \
  131. switch (sizeof(*(ptr))) { \
  132. case 1: { \
  133. unsigned char __x; \
  134. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  135. ptr, &__x); \
  136. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  137. break; \
  138. }; \
  139. case 2: { \
  140. unsigned short __x; \
  141. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  142. ptr, &__x); \
  143. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  144. break; \
  145. }; \
  146. case 4: { \
  147. unsigned int __x; \
  148. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  149. ptr, &__x); \
  150. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  151. break; \
  152. }; \
  153. case 8: { \
  154. unsigned long long __x; \
  155. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  156. ptr, &__x); \
  157. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  158. break; \
  159. }; \
  160. default: \
  161. __get_user_bad(); \
  162. break; \
  163. } \
  164. __gu_err; \
  165. })
  166. #define get_user(x, ptr) \
  167. ({ \
  168. might_sleep(); \
  169. __get_user(x, ptr); \
  170. })
  171. extern int __get_user_bad(void) __attribute__((noreturn));
  172. #define __put_user_unaligned __put_user
  173. #define __get_user_unaligned __get_user
  174. /**
  175. * __copy_to_user: - Copy a block of data into user space, with less checking.
  176. * @to: Destination address, in user space.
  177. * @from: Source address, in kernel space.
  178. * @n: Number of bytes to copy.
  179. *
  180. * Context: User context only. This function may sleep.
  181. *
  182. * Copy data from kernel space to user space. Caller must check
  183. * the specified block with access_ok() before calling this function.
  184. *
  185. * Returns number of bytes that could not be copied.
  186. * On success, this will be zero.
  187. */
  188. static inline unsigned long
  189. __copy_to_user(void __user *to, const void *from, unsigned long n)
  190. {
  191. if (__builtin_constant_p(n) && (n <= 256))
  192. return uaccess.copy_to_user_small(n, to, from);
  193. else
  194. return uaccess.copy_to_user(n, to, from);
  195. }
  196. #define __copy_to_user_inatomic __copy_to_user
  197. #define __copy_from_user_inatomic __copy_from_user
  198. /**
  199. * copy_to_user: - Copy a block of data into user space.
  200. * @to: Destination address, in user space.
  201. * @from: Source address, in kernel space.
  202. * @n: Number of bytes to copy.
  203. *
  204. * Context: User context only. This function may sleep.
  205. *
  206. * Copy data from kernel space to user space.
  207. *
  208. * Returns number of bytes that could not be copied.
  209. * On success, this will be zero.
  210. */
  211. static inline unsigned long
  212. copy_to_user(void __user *to, const void *from, unsigned long n)
  213. {
  214. might_sleep();
  215. if (access_ok(VERIFY_WRITE, to, n))
  216. n = __copy_to_user(to, from, n);
  217. return n;
  218. }
  219. /**
  220. * __copy_from_user: - Copy a block of data from user space, with less checking.
  221. * @to: Destination address, in kernel space.
  222. * @from: Source address, in user space.
  223. * @n: Number of bytes to copy.
  224. *
  225. * Context: User context only. This function may sleep.
  226. *
  227. * Copy data from user space to kernel space. Caller must check
  228. * the specified block with access_ok() before calling this function.
  229. *
  230. * Returns number of bytes that could not be copied.
  231. * On success, this will be zero.
  232. *
  233. * If some data could not be copied, this function will pad the copied
  234. * data to the requested size using zero bytes.
  235. */
  236. static inline unsigned long
  237. __copy_from_user(void *to, const void __user *from, unsigned long n)
  238. {
  239. if (__builtin_constant_p(n) && (n <= 256))
  240. return uaccess.copy_from_user_small(n, from, to);
  241. else
  242. return uaccess.copy_from_user(n, from, to);
  243. }
  244. /**
  245. * copy_from_user: - Copy a block of data from user space.
  246. * @to: Destination address, in kernel space.
  247. * @from: Source address, in user space.
  248. * @n: Number of bytes to copy.
  249. *
  250. * Context: User context only. This function may sleep.
  251. *
  252. * Copy data from user space to kernel space.
  253. *
  254. * Returns number of bytes that could not be copied.
  255. * On success, this will be zero.
  256. *
  257. * If some data could not be copied, this function will pad the copied
  258. * data to the requested size using zero bytes.
  259. */
  260. static inline unsigned long
  261. copy_from_user(void *to, const void __user *from, unsigned long n)
  262. {
  263. might_sleep();
  264. if (access_ok(VERIFY_READ, from, n))
  265. n = __copy_from_user(to, from, n);
  266. else
  267. memset(to, 0, n);
  268. return n;
  269. }
  270. static inline unsigned long
  271. __copy_in_user(void __user *to, const void __user *from, unsigned long n)
  272. {
  273. return uaccess.copy_in_user(n, to, from);
  274. }
  275. static inline unsigned long
  276. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  277. {
  278. might_sleep();
  279. if (__access_ok(from,n) && __access_ok(to,n))
  280. n = __copy_in_user(to, from, n);
  281. return n;
  282. }
  283. /*
  284. * Copy a null terminated string from userspace.
  285. */
  286. static inline long
  287. strncpy_from_user(char *dst, const char __user *src, long count)
  288. {
  289. long res = -EFAULT;
  290. might_sleep();
  291. if (access_ok(VERIFY_READ, src, 1))
  292. res = uaccess.strncpy_from_user(count, src, dst);
  293. return res;
  294. }
  295. static inline unsigned long
  296. strnlen_user(const char __user * src, unsigned long n)
  297. {
  298. might_sleep();
  299. return uaccess.strnlen_user(n, src);
  300. }
  301. /**
  302. * strlen_user: - Get the size of a string in user space.
  303. * @str: The string to measure.
  304. *
  305. * Context: User context only. This function may sleep.
  306. *
  307. * Get the size of a NUL-terminated string in user space.
  308. *
  309. * Returns the size of the string INCLUDING the terminating NUL.
  310. * On exception, returns 0.
  311. *
  312. * If there is a limit on the length of a valid string, you may wish to
  313. * consider using strnlen_user() instead.
  314. */
  315. #define strlen_user(str) strnlen_user(str, ~0UL)
  316. /*
  317. * Zero Userspace
  318. */
  319. static inline unsigned long
  320. __clear_user(void __user *to, unsigned long n)
  321. {
  322. return uaccess.clear_user(n, to);
  323. }
  324. static inline unsigned long
  325. clear_user(void __user *to, unsigned long n)
  326. {
  327. might_sleep();
  328. if (access_ok(VERIFY_WRITE, to, n))
  329. n = uaccess.clear_user(n, to);
  330. return n;
  331. }
  332. #endif /* __S390_UACCESS_H */