uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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
  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
  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. #ifndef __s390x__
  74. #define __uaccess_fixup \
  75. ".section .fixup,\"ax\"\n" \
  76. "2: lhi %0,%4\n" \
  77. " bras 1,3f\n" \
  78. " .long 1b\n" \
  79. "3: l 1,0(1)\n" \
  80. " br 1\n" \
  81. ".previous\n" \
  82. ".section __ex_table,\"a\"\n" \
  83. " .align 4\n" \
  84. " .long 0b,2b\n" \
  85. ".previous"
  86. #define __uaccess_clobber "cc", "1"
  87. #else /* __s390x__ */
  88. #define __uaccess_fixup \
  89. ".section .fixup,\"ax\"\n" \
  90. "2: lghi %0,%4\n" \
  91. " jg 1b\n" \
  92. ".previous\n" \
  93. ".section __ex_table,\"a\"\n" \
  94. " .align 8\n" \
  95. " .quad 0b,2b\n" \
  96. ".previous"
  97. #define __uaccess_clobber "cc"
  98. #endif /* __s390x__ */
  99. /*
  100. * These are the main single-value transfer routines. They automatically
  101. * use the right size if we just have the right pointer type.
  102. */
  103. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  104. #define __put_user_asm(x, ptr, err) \
  105. ({ \
  106. err = 0; \
  107. asm volatile( \
  108. "0: mvcs 0(%1,%2),%3,%0\n" \
  109. "1:\n" \
  110. __uaccess_fixup \
  111. : "+&d" (err) \
  112. : "d" (sizeof(*(ptr))), "a" (ptr), "Q" (x), \
  113. "K" (-EFAULT) \
  114. : __uaccess_clobber ); \
  115. })
  116. #else
  117. #define __put_user_asm(x, ptr, err) \
  118. ({ \
  119. err = 0; \
  120. asm volatile( \
  121. "0: mvcs 0(%1,%2),0(%3),%0\n" \
  122. "1:\n" \
  123. __uaccess_fixup \
  124. : "+&d" (err) \
  125. : "d" (sizeof(*(ptr))), "a" (ptr), "a" (&(x)), \
  126. "K" (-EFAULT), "m" (x) \
  127. : __uaccess_clobber ); \
  128. })
  129. #endif
  130. #define __put_user(x, ptr) \
  131. ({ \
  132. __typeof__(*(ptr)) __x = (x); \
  133. int __pu_err; \
  134. __chk_user_ptr(ptr); \
  135. switch (sizeof (*(ptr))) { \
  136. case 1: \
  137. case 2: \
  138. case 4: \
  139. case 8: \
  140. __put_user_asm(__x, ptr, __pu_err); \
  141. break; \
  142. default: \
  143. __put_user_bad(); \
  144. break; \
  145. } \
  146. __pu_err; \
  147. })
  148. #define put_user(x, ptr) \
  149. ({ \
  150. might_sleep(); \
  151. __put_user(x, ptr); \
  152. })
  153. extern int __put_user_bad(void) __attribute__((noreturn));
  154. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  155. #define __get_user_asm(x, ptr, err) \
  156. ({ \
  157. err = 0; \
  158. asm volatile ( \
  159. "0: mvcp %O1(%2,%R1),0(%3),%0\n" \
  160. "1:\n" \
  161. __uaccess_fixup \
  162. : "+&d" (err), "=Q" (x) \
  163. : "d" (sizeof(*(ptr))), "a" (ptr), \
  164. "K" (-EFAULT) \
  165. : __uaccess_clobber ); \
  166. })
  167. #else
  168. #define __get_user_asm(x, ptr, err) \
  169. ({ \
  170. err = 0; \
  171. asm volatile ( \
  172. "0: mvcp 0(%2,%5),0(%3),%0\n" \
  173. "1:\n" \
  174. __uaccess_fixup \
  175. : "+&d" (err), "=m" (x) \
  176. : "d" (sizeof(*(ptr))), "a" (ptr), \
  177. "K" (-EFAULT), "a" (&(x)) \
  178. : __uaccess_clobber ); \
  179. })
  180. #endif
  181. #define __get_user(x, ptr) \
  182. ({ \
  183. int __gu_err; \
  184. __chk_user_ptr(ptr); \
  185. switch (sizeof(*(ptr))) { \
  186. case 1: { \
  187. unsigned char __x; \
  188. __get_user_asm(__x, ptr, __gu_err); \
  189. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  190. break; \
  191. }; \
  192. case 2: { \
  193. unsigned short __x; \
  194. __get_user_asm(__x, ptr, __gu_err); \
  195. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  196. break; \
  197. }; \
  198. case 4: { \
  199. unsigned int __x; \
  200. __get_user_asm(__x, ptr, __gu_err); \
  201. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  202. break; \
  203. }; \
  204. case 8: { \
  205. unsigned long long __x; \
  206. __get_user_asm(__x, ptr, __gu_err); \
  207. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  208. break; \
  209. }; \
  210. default: \
  211. __get_user_bad(); \
  212. break; \
  213. } \
  214. __gu_err; \
  215. })
  216. #define get_user(x, ptr) \
  217. ({ \
  218. might_sleep(); \
  219. __get_user(x, ptr); \
  220. })
  221. extern int __get_user_bad(void) __attribute__((noreturn));
  222. #define __put_user_unaligned __put_user
  223. #define __get_user_unaligned __get_user
  224. extern long __copy_to_user_asm(const void *from, long n, void __user *to);
  225. /**
  226. * __copy_to_user: - Copy a block of data into user space, with less checking.
  227. * @to: Destination address, in user space.
  228. * @from: Source address, in kernel space.
  229. * @n: Number of bytes to copy.
  230. *
  231. * Context: User context only. This function may sleep.
  232. *
  233. * Copy data from kernel space to user space. Caller must check
  234. * the specified block with access_ok() before calling this function.
  235. *
  236. * Returns number of bytes that could not be copied.
  237. * On success, this will be zero.
  238. */
  239. static inline unsigned long
  240. __copy_to_user(void __user *to, const void *from, unsigned long n)
  241. {
  242. return __copy_to_user_asm(from, n, to);
  243. }
  244. #define __copy_to_user_inatomic __copy_to_user
  245. #define __copy_from_user_inatomic __copy_from_user
  246. /**
  247. * copy_to_user: - Copy a block of data into user space.
  248. * @to: Destination address, in user space.
  249. * @from: Source address, in kernel space.
  250. * @n: Number of bytes to copy.
  251. *
  252. * Context: User context only. This function may sleep.
  253. *
  254. * Copy data from kernel space to user space.
  255. *
  256. * Returns number of bytes that could not be copied.
  257. * On success, this will be zero.
  258. */
  259. static inline unsigned long
  260. copy_to_user(void __user *to, const void *from, unsigned long n)
  261. {
  262. might_sleep();
  263. if (access_ok(VERIFY_WRITE, to, n))
  264. n = __copy_to_user(to, from, n);
  265. return n;
  266. }
  267. extern long __copy_from_user_asm(void *to, long n, const void __user *from);
  268. /**
  269. * __copy_from_user: - Copy a block of data from user space, with less checking.
  270. * @to: Destination address, in kernel space.
  271. * @from: Source address, in user space.
  272. * @n: Number of bytes to copy.
  273. *
  274. * Context: User context only. This function may sleep.
  275. *
  276. * Copy data from user space to kernel space. Caller must check
  277. * the specified block with access_ok() before calling this function.
  278. *
  279. * Returns number of bytes that could not be copied.
  280. * On success, this will be zero.
  281. *
  282. * If some data could not be copied, this function will pad the copied
  283. * data to the requested size using zero bytes.
  284. */
  285. static inline unsigned long
  286. __copy_from_user(void *to, const void __user *from, unsigned long n)
  287. {
  288. return __copy_from_user_asm(to, n, from);
  289. }
  290. /**
  291. * copy_from_user: - Copy a block of data from user space.
  292. * @to: Destination address, in kernel space.
  293. * @from: Source address, in user space.
  294. * @n: Number of bytes to copy.
  295. *
  296. * Context: User context only. This function may sleep.
  297. *
  298. * Copy data from user space to kernel space.
  299. *
  300. * Returns number of bytes that could not be copied.
  301. * On success, this will be zero.
  302. *
  303. * If some data could not be copied, this function will pad the copied
  304. * data to the requested size using zero bytes.
  305. */
  306. static inline unsigned long
  307. copy_from_user(void *to, const void __user *from, unsigned long n)
  308. {
  309. might_sleep();
  310. if (access_ok(VERIFY_READ, from, n))
  311. n = __copy_from_user(to, from, n);
  312. else
  313. memset(to, 0, n);
  314. return n;
  315. }
  316. extern unsigned long __copy_in_user_asm(const void __user *from, long n,
  317. void __user *to);
  318. static inline unsigned long
  319. __copy_in_user(void __user *to, const void __user *from, unsigned long n)
  320. {
  321. return __copy_in_user_asm(from, n, to);
  322. }
  323. static inline unsigned long
  324. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  325. {
  326. might_sleep();
  327. if (__access_ok(from,n) && __access_ok(to,n))
  328. n = __copy_in_user_asm(from, n, to);
  329. return n;
  330. }
  331. /*
  332. * Copy a null terminated string from userspace.
  333. */
  334. extern long __strncpy_from_user_asm(long count, char *dst,
  335. const char __user *src);
  336. static inline long
  337. strncpy_from_user(char *dst, const char __user *src, long count)
  338. {
  339. long res = -EFAULT;
  340. might_sleep();
  341. if (access_ok(VERIFY_READ, src, 1))
  342. res = __strncpy_from_user_asm(count, dst, src);
  343. return res;
  344. }
  345. extern long __strnlen_user_asm(long count, const char __user *src);
  346. static inline unsigned long
  347. strnlen_user(const char __user * src, unsigned long n)
  348. {
  349. might_sleep();
  350. return __strnlen_user_asm(n, src);
  351. }
  352. /**
  353. * strlen_user: - Get the size of a string in user space.
  354. * @str: The string to measure.
  355. *
  356. * Context: User context only. This function may sleep.
  357. *
  358. * Get the size of a NUL-terminated string in user space.
  359. *
  360. * Returns the size of the string INCLUDING the terminating NUL.
  361. * On exception, returns 0.
  362. *
  363. * If there is a limit on the length of a valid string, you may wish to
  364. * consider using strnlen_user() instead.
  365. */
  366. #define strlen_user(str) strnlen_user(str, ~0UL)
  367. /*
  368. * Zero Userspace
  369. */
  370. extern long __clear_user_asm(void __user *to, long n);
  371. static inline unsigned long
  372. __clear_user(void __user *to, unsigned long n)
  373. {
  374. return __clear_user_asm(to, n);
  375. }
  376. static inline unsigned long
  377. clear_user(void __user *to, unsigned long n)
  378. {
  379. might_sleep();
  380. if (access_ok(VERIFY_WRITE, to, n))
  381. n = __clear_user_asm(to, n);
  382. return n;
  383. }
  384. #endif /* __S390_UACCESS_H */