uaccess.h 11 KB

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