uaccess.h 11 KB

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