uaccess.h 11 KB

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