uaccess.h 9.8 KB

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