uaccess.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999, 2000
  4. * Author(s): Hartmut Penner (hp@de.ibm.com),
  5. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  6. *
  7. * Derived from "include/asm-i386/uaccess.h"
  8. */
  9. #ifndef __S390_UACCESS_H
  10. #define __S390_UACCESS_H
  11. /*
  12. * User space memory access functions
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/errno.h>
  16. #include <asm/ctl_reg.h>
  17. #define VERIFY_READ 0
  18. #define VERIFY_WRITE 1
  19. /*
  20. * The fs value determines whether argument validity checking should be
  21. * performed or not. If get_fs() == USER_DS, checking is performed, with
  22. * get_fs() == KERNEL_DS, checking is bypassed.
  23. *
  24. * For historical reasons, these macros are grossly misnamed.
  25. */
  26. #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
  27. #define KERNEL_DS MAKE_MM_SEG(0)
  28. #define USER_DS MAKE_MM_SEG(1)
  29. #define get_ds() (KERNEL_DS)
  30. #define get_fs() (current->thread.mm_segment)
  31. #define set_fs(x) \
  32. ({ \
  33. unsigned long __pto; \
  34. current->thread.mm_segment = (x); \
  35. __pto = current->thread.mm_segment.ar4 ? \
  36. S390_lowcore.user_asce : S390_lowcore.kernel_asce; \
  37. __ctl_load(__pto, 7, 7); \
  38. })
  39. #define segment_eq(a,b) ((a).ar4 == (b).ar4)
  40. static inline int __range_ok(unsigned long addr, unsigned long size)
  41. {
  42. return 1;
  43. }
  44. #define __access_ok(addr, size) \
  45. ({ \
  46. __chk_user_ptr(addr); \
  47. __range_ok((unsigned long)(addr), (size)); \
  48. })
  49. #define access_ok(type, addr, size) __access_ok(addr, size)
  50. /*
  51. * The exception table consists of pairs of addresses: the first is the
  52. * address of an instruction that is allowed to fault, and the second is
  53. * the address at which the program should continue. No registers are
  54. * modified, so it is entirely up to the continuation code to figure out
  55. * what to do.
  56. *
  57. * All the routines below use bits of fixup code that are out of line
  58. * with the main instruction path. This means when everything is well,
  59. * we don't even have to jump over them. Further, they do not intrude
  60. * on our cache or tlb entries.
  61. */
  62. struct exception_table_entry
  63. {
  64. int insn, fixup;
  65. };
  66. static inline unsigned long extable_insn(const struct exception_table_entry *x)
  67. {
  68. return (unsigned long)&x->insn + x->insn;
  69. }
  70. static inline unsigned long extable_fixup(const struct exception_table_entry *x)
  71. {
  72. return (unsigned long)&x->fixup + x->fixup;
  73. }
  74. #define ARCH_HAS_SORT_EXTABLE
  75. #define ARCH_HAS_SEARCH_EXTABLE
  76. /**
  77. * __copy_from_user: - Copy a block of data from user space, with less checking.
  78. * @to: Destination address, in kernel space.
  79. * @from: Source address, in user space.
  80. * @n: Number of bytes to copy.
  81. *
  82. * Context: User context only. This function may sleep.
  83. *
  84. * Copy data from user space to kernel space. Caller must check
  85. * the specified block with access_ok() before calling this function.
  86. *
  87. * Returns number of bytes that could not be copied.
  88. * On success, this will be zero.
  89. *
  90. * If some data could not be copied, this function will pad the copied
  91. * data to the requested size using zero bytes.
  92. */
  93. unsigned long __must_check __copy_from_user(void *to, const void __user *from,
  94. unsigned long n);
  95. /**
  96. * __copy_to_user: - Copy a block of data into user space, with less checking.
  97. * @to: Destination address, in user space.
  98. * @from: Source address, in kernel space.
  99. * @n: Number of bytes to copy.
  100. *
  101. * Context: User context only. This function may sleep.
  102. *
  103. * Copy data from kernel space to user space. Caller must check
  104. * the specified block with access_ok() before calling this function.
  105. *
  106. * Returns number of bytes that could not be copied.
  107. * On success, this will be zero.
  108. */
  109. unsigned long __must_check __copy_to_user(void __user *to, const void *from,
  110. unsigned long n);
  111. #define __copy_to_user_inatomic __copy_to_user
  112. #define __copy_from_user_inatomic __copy_from_user
  113. #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
  114. #define __put_get_user_asm(to, from, size, spec) \
  115. ({ \
  116. register unsigned long __reg0 asm("0") = spec; \
  117. int __rc; \
  118. \
  119. asm volatile( \
  120. "0: mvcos %1,%3,%2\n" \
  121. "1: xr %0,%0\n" \
  122. "2:\n" \
  123. ".pushsection .fixup, \"ax\"\n" \
  124. "3: lhi %0,%5\n" \
  125. " jg 2b\n" \
  126. ".popsection\n" \
  127. EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
  128. : "=d" (__rc), "=Q" (*(to)) \
  129. : "d" (size), "Q" (*(from)), \
  130. "d" (__reg0), "K" (-EFAULT) \
  131. : "cc"); \
  132. __rc; \
  133. })
  134. #define __put_user_fn(x, ptr, size) __put_get_user_asm(ptr, x, size, 0x810000UL)
  135. #define __get_user_fn(x, ptr, size) __put_get_user_asm(x, ptr, size, 0x81UL)
  136. #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
  137. static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
  138. {
  139. size = __copy_to_user(ptr, x, size);
  140. return size ? -EFAULT : 0;
  141. }
  142. static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
  143. {
  144. size = __copy_from_user(x, ptr, size);
  145. return size ? -EFAULT : 0;
  146. }
  147. #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
  148. /*
  149. * These are the main single-value transfer routines. They automatically
  150. * use the right size if we just have the right pointer type.
  151. */
  152. #define __put_user(x, ptr) \
  153. ({ \
  154. __typeof__(*(ptr)) __x = (x); \
  155. int __pu_err = -EFAULT; \
  156. __chk_user_ptr(ptr); \
  157. switch (sizeof (*(ptr))) { \
  158. case 1: \
  159. case 2: \
  160. case 4: \
  161. case 8: \
  162. __pu_err = __put_user_fn(&__x, ptr, \
  163. sizeof(*(ptr))); \
  164. break; \
  165. default: \
  166. __put_user_bad(); \
  167. break; \
  168. } \
  169. __pu_err; \
  170. })
  171. #define put_user(x, ptr) \
  172. ({ \
  173. might_fault(); \
  174. __put_user(x, ptr); \
  175. })
  176. int __put_user_bad(void) __attribute__((noreturn));
  177. #define __get_user(x, ptr) \
  178. ({ \
  179. int __gu_err = -EFAULT; \
  180. __chk_user_ptr(ptr); \
  181. switch (sizeof(*(ptr))) { \
  182. case 1: { \
  183. unsigned char __x; \
  184. __gu_err = __get_user_fn(&__x, ptr, \
  185. sizeof(*(ptr))); \
  186. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  187. break; \
  188. }; \
  189. case 2: { \
  190. unsigned short __x; \
  191. __gu_err = __get_user_fn(&__x, ptr, \
  192. sizeof(*(ptr))); \
  193. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  194. break; \
  195. }; \
  196. case 4: { \
  197. unsigned int __x; \
  198. __gu_err = __get_user_fn(&__x, ptr, \
  199. sizeof(*(ptr))); \
  200. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  201. break; \
  202. }; \
  203. case 8: { \
  204. unsigned long long __x; \
  205. __gu_err = __get_user_fn(&__x, ptr, \
  206. sizeof(*(ptr))); \
  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_fault(); \
  219. __get_user(x, ptr); \
  220. })
  221. int __get_user_bad(void) __attribute__((noreturn));
  222. #define __put_user_unaligned __put_user
  223. #define __get_user_unaligned __get_user
  224. /**
  225. * copy_to_user: - Copy a block of data into user space.
  226. * @to: Destination address, in user space.
  227. * @from: Source address, in kernel space.
  228. * @n: Number of bytes to copy.
  229. *
  230. * Context: User context only. This function may sleep.
  231. *
  232. * Copy data from kernel space to user space.
  233. *
  234. * Returns number of bytes that could not be copied.
  235. * On success, this will be zero.
  236. */
  237. static inline unsigned long __must_check
  238. copy_to_user(void __user *to, const void *from, unsigned long n)
  239. {
  240. might_fault();
  241. return __copy_to_user(to, from, n);
  242. }
  243. void copy_from_user_overflow(void)
  244. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  245. __compiletime_warning("copy_from_user() buffer size is not provably correct")
  246. #endif
  247. ;
  248. /**
  249. * copy_from_user: - Copy a block of data from user space.
  250. * @to: Destination address, in kernel space.
  251. * @from: Source address, in user space.
  252. * @n: Number of bytes to copy.
  253. *
  254. * Context: User context only. This function may sleep.
  255. *
  256. * Copy data from user space to kernel space.
  257. *
  258. * Returns number of bytes that could not be copied.
  259. * On success, this will be zero.
  260. *
  261. * If some data could not be copied, this function will pad the copied
  262. * data to the requested size using zero bytes.
  263. */
  264. static inline unsigned long __must_check
  265. copy_from_user(void *to, const void __user *from, unsigned long n)
  266. {
  267. unsigned int sz = __compiletime_object_size(to);
  268. might_fault();
  269. if (unlikely(sz != -1 && sz < n)) {
  270. copy_from_user_overflow();
  271. return n;
  272. }
  273. return __copy_from_user(to, from, n);
  274. }
  275. unsigned long __must_check
  276. __copy_in_user(void __user *to, const void __user *from, unsigned long n);
  277. static inline unsigned long __must_check
  278. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  279. {
  280. might_fault();
  281. return __copy_in_user(to, from, n);
  282. }
  283. /*
  284. * Copy a null terminated string from userspace.
  285. */
  286. long __strncpy_from_user(char *dst, const char __user *src, long count);
  287. static inline long __must_check
  288. strncpy_from_user(char *dst, const char __user *src, long count)
  289. {
  290. might_fault();
  291. return __strncpy_from_user(dst, src, count);
  292. }
  293. unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
  294. static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
  295. {
  296. might_fault();
  297. return __strnlen_user(src, n);
  298. }
  299. /**
  300. * strlen_user: - Get the size of a string in user space.
  301. * @str: The string to measure.
  302. *
  303. * Context: User context only. This function may sleep.
  304. *
  305. * Get the size of a NUL-terminated string in user space.
  306. *
  307. * Returns the size of the string INCLUDING the terminating NUL.
  308. * On exception, returns 0.
  309. *
  310. * If there is a limit on the length of a valid string, you may wish to
  311. * consider using strnlen_user() instead.
  312. */
  313. #define strlen_user(str) strnlen_user(str, ~0UL)
  314. /*
  315. * Zero Userspace
  316. */
  317. unsigned long __must_check __clear_user(void __user *to, unsigned long size);
  318. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  319. {
  320. might_fault();
  321. return __clear_user(to, n);
  322. }
  323. int copy_to_user_real(void __user *dest, void *src, unsigned long count);
  324. void s390_kernel_write(void *dst, const void *src, size_t size);
  325. #endif /* __S390_UACCESS_H */