uaccess.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. __typeof__(*(ptr)) __x; \
  181. int __gu_err; \
  182. __chk_user_ptr(ptr); \
  183. switch (sizeof(*(ptr))) { \
  184. case 1: \
  185. case 2: \
  186. case 4: \
  187. case 8: \
  188. __get_user_asm(__x, ptr, __gu_err); \
  189. break; \
  190. default: \
  191. __get_user_bad(); \
  192. break; \
  193. } \
  194. (x) = __x; \
  195. __gu_err; \
  196. })
  197. #define get_user(x, ptr) \
  198. ({ \
  199. might_sleep(); \
  200. __get_user(x, ptr); \
  201. })
  202. extern int __get_user_bad(void) __attribute__((noreturn));
  203. #define __put_user_unaligned __put_user
  204. #define __get_user_unaligned __get_user
  205. extern long __copy_to_user_asm(const void *from, long n, void __user *to);
  206. /**
  207. * __copy_to_user: - Copy a block of data into user space, with less checking.
  208. * @to: Destination address, in user space.
  209. * @from: Source address, in kernel space.
  210. * @n: Number of bytes to copy.
  211. *
  212. * Context: User context only. This function may sleep.
  213. *
  214. * Copy data from kernel space to user space. Caller must check
  215. * the specified block with access_ok() before calling this function.
  216. *
  217. * Returns number of bytes that could not be copied.
  218. * On success, this will be zero.
  219. */
  220. static inline unsigned long
  221. __copy_to_user(void __user *to, const void *from, unsigned long n)
  222. {
  223. return __copy_to_user_asm(from, n, to);
  224. }
  225. #define __copy_to_user_inatomic __copy_to_user
  226. #define __copy_from_user_inatomic __copy_from_user
  227. /**
  228. * copy_to_user: - Copy a block of data into user space.
  229. * @to: Destination address, in user space.
  230. * @from: Source address, in kernel space.
  231. * @n: Number of bytes to copy.
  232. *
  233. * Context: User context only. This function may sleep.
  234. *
  235. * Copy data from kernel space to user space.
  236. *
  237. * Returns number of bytes that could not be copied.
  238. * On success, this will be zero.
  239. */
  240. static inline unsigned long
  241. copy_to_user(void __user *to, const void *from, unsigned long n)
  242. {
  243. might_sleep();
  244. if (access_ok(VERIFY_WRITE, to, n))
  245. n = __copy_to_user(to, from, n);
  246. return n;
  247. }
  248. extern long __copy_from_user_asm(void *to, long n, const void __user *from);
  249. /**
  250. * __copy_from_user: - Copy a block of data from user space, with less checking.
  251. * @to: Destination address, in kernel space.
  252. * @from: Source address, in user space.
  253. * @n: Number of bytes to copy.
  254. *
  255. * Context: User context only. This function may sleep.
  256. *
  257. * Copy data from user space to kernel space. Caller must check
  258. * the specified block with access_ok() before calling this function.
  259. *
  260. * Returns number of bytes that could not be copied.
  261. * On success, this will be zero.
  262. *
  263. * If some data could not be copied, this function will pad the copied
  264. * data to the requested size using zero bytes.
  265. */
  266. static inline unsigned long
  267. __copy_from_user(void *to, const void __user *from, unsigned long n)
  268. {
  269. return __copy_from_user_asm(to, n, from);
  270. }
  271. /**
  272. * copy_from_user: - Copy a block of data from user space.
  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.
  280. *
  281. * Returns number of bytes that could not be copied.
  282. * On success, this will be zero.
  283. *
  284. * If some data could not be copied, this function will pad the copied
  285. * data to the requested size using zero bytes.
  286. */
  287. static inline unsigned long
  288. copy_from_user(void *to, const void __user *from, unsigned long n)
  289. {
  290. might_sleep();
  291. if (access_ok(VERIFY_READ, from, n))
  292. n = __copy_from_user(to, from, n);
  293. else
  294. memset(to, 0, n);
  295. return n;
  296. }
  297. extern unsigned long __copy_in_user_asm(const void __user *from, long n,
  298. void __user *to);
  299. static inline unsigned long
  300. __copy_in_user(void __user *to, const void __user *from, unsigned long n)
  301. {
  302. return __copy_in_user_asm(from, n, to);
  303. }
  304. static inline unsigned long
  305. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  306. {
  307. might_sleep();
  308. if (__access_ok(from,n) && __access_ok(to,n))
  309. n = __copy_in_user_asm(from, n, to);
  310. return n;
  311. }
  312. /*
  313. * Copy a null terminated string from userspace.
  314. */
  315. extern long __strncpy_from_user_asm(long count, char *dst,
  316. const char __user *src);
  317. static inline long
  318. strncpy_from_user(char *dst, const char __user *src, long count)
  319. {
  320. long res = -EFAULT;
  321. might_sleep();
  322. if (access_ok(VERIFY_READ, src, 1))
  323. res = __strncpy_from_user_asm(count, dst, src);
  324. return res;
  325. }
  326. extern long __strnlen_user_asm(long count, const char __user *src);
  327. static inline unsigned long
  328. strnlen_user(const char __user * src, unsigned long n)
  329. {
  330. might_sleep();
  331. return __strnlen_user_asm(n, src);
  332. }
  333. /**
  334. * strlen_user: - Get the size of a string in user space.
  335. * @str: The string to measure.
  336. *
  337. * Context: User context only. This function may sleep.
  338. *
  339. * Get the size of a NUL-terminated string in user space.
  340. *
  341. * Returns the size of the string INCLUDING the terminating NUL.
  342. * On exception, returns 0.
  343. *
  344. * If there is a limit on the length of a valid string, you may wish to
  345. * consider using strnlen_user() instead.
  346. */
  347. #define strlen_user(str) strnlen_user(str, ~0UL)
  348. /*
  349. * Zero Userspace
  350. */
  351. extern long __clear_user_asm(void __user *to, long n);
  352. static inline unsigned long
  353. __clear_user(void __user *to, unsigned long n)
  354. {
  355. return __clear_user_asm(to, n);
  356. }
  357. static inline unsigned long
  358. clear_user(void __user *to, unsigned long n)
  359. {
  360. might_sleep();
  361. if (access_ok(VERIFY_WRITE, to, n))
  362. n = __clear_user_asm(to, n);
  363. return n;
  364. }
  365. #endif /* __S390_UACCESS_H */