uaccess.h 10 KB

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