uaccess.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
  114. {
  115. size = __copy_to_user(ptr, x, size);
  116. return size ? -EFAULT : 0;
  117. }
  118. static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
  119. {
  120. size = __copy_from_user(x, ptr, size);
  121. return size ? -EFAULT : 0;
  122. }
  123. /*
  124. * These are the main single-value transfer routines. They automatically
  125. * use the right size if we just have the right pointer type.
  126. */
  127. #define __put_user(x, ptr) \
  128. ({ \
  129. __typeof__(*(ptr)) __x = (x); \
  130. int __pu_err = -EFAULT; \
  131. __chk_user_ptr(ptr); \
  132. switch (sizeof (*(ptr))) { \
  133. case 1: \
  134. case 2: \
  135. case 4: \
  136. case 8: \
  137. __pu_err = __put_user_fn(&__x, ptr, \
  138. sizeof(*(ptr))); \
  139. break; \
  140. default: \
  141. __put_user_bad(); \
  142. break; \
  143. } \
  144. __pu_err; \
  145. })
  146. #define put_user(x, ptr) \
  147. ({ \
  148. might_fault(); \
  149. __put_user(x, ptr); \
  150. })
  151. int __put_user_bad(void) __attribute__((noreturn));
  152. #define __get_user(x, ptr) \
  153. ({ \
  154. int __gu_err = -EFAULT; \
  155. __chk_user_ptr(ptr); \
  156. switch (sizeof(*(ptr))) { \
  157. case 1: { \
  158. unsigned char __x; \
  159. __gu_err = __get_user_fn(&__x, ptr, \
  160. sizeof(*(ptr))); \
  161. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  162. break; \
  163. }; \
  164. case 2: { \
  165. unsigned short __x; \
  166. __gu_err = __get_user_fn(&__x, ptr, \
  167. sizeof(*(ptr))); \
  168. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  169. break; \
  170. }; \
  171. case 4: { \
  172. unsigned int __x; \
  173. __gu_err = __get_user_fn(&__x, ptr, \
  174. sizeof(*(ptr))); \
  175. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  176. break; \
  177. }; \
  178. case 8: { \
  179. unsigned long long __x; \
  180. __gu_err = __get_user_fn(&__x, ptr, \
  181. sizeof(*(ptr))); \
  182. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  183. break; \
  184. }; \
  185. default: \
  186. __get_user_bad(); \
  187. break; \
  188. } \
  189. __gu_err; \
  190. })
  191. #define get_user(x, ptr) \
  192. ({ \
  193. might_fault(); \
  194. __get_user(x, ptr); \
  195. })
  196. int __get_user_bad(void) __attribute__((noreturn));
  197. #define __put_user_unaligned __put_user
  198. #define __get_user_unaligned __get_user
  199. /**
  200. * copy_to_user: - Copy a block of data into user space.
  201. * @to: Destination address, in user space.
  202. * @from: Source address, in kernel space.
  203. * @n: Number of bytes to copy.
  204. *
  205. * Context: User context only. This function may sleep.
  206. *
  207. * Copy data from kernel space to user space.
  208. *
  209. * Returns number of bytes that could not be copied.
  210. * On success, this will be zero.
  211. */
  212. static inline unsigned long __must_check
  213. copy_to_user(void __user *to, const void *from, unsigned long n)
  214. {
  215. might_fault();
  216. return __copy_to_user(to, from, n);
  217. }
  218. void copy_from_user_overflow(void)
  219. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  220. __compiletime_warning("copy_from_user() buffer size is not provably correct")
  221. #endif
  222. ;
  223. /**
  224. * copy_from_user: - Copy a block of data from user space.
  225. * @to: Destination address, in kernel space.
  226. * @from: Source address, in user space.
  227. * @n: Number of bytes to copy.
  228. *
  229. * Context: User context only. This function may sleep.
  230. *
  231. * Copy data from user space to kernel space.
  232. *
  233. * Returns number of bytes that could not be copied.
  234. * On success, this will be zero.
  235. *
  236. * If some data could not be copied, this function will pad the copied
  237. * data to the requested size using zero bytes.
  238. */
  239. static inline unsigned long __must_check
  240. copy_from_user(void *to, const void __user *from, unsigned long n)
  241. {
  242. unsigned int sz = __compiletime_object_size(to);
  243. might_fault();
  244. if (unlikely(sz != -1 && sz < n)) {
  245. copy_from_user_overflow();
  246. return n;
  247. }
  248. return __copy_from_user(to, from, n);
  249. }
  250. unsigned long __must_check
  251. __copy_in_user(void __user *to, const void __user *from, unsigned long n);
  252. static inline unsigned long __must_check
  253. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  254. {
  255. might_fault();
  256. return __copy_in_user(to, from, n);
  257. }
  258. /*
  259. * Copy a null terminated string from userspace.
  260. */
  261. long __strncpy_from_user(char *dst, const char __user *src, long count);
  262. static inline long __must_check
  263. strncpy_from_user(char *dst, const char __user *src, long count)
  264. {
  265. might_fault();
  266. return __strncpy_from_user(dst, src, count);
  267. }
  268. unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
  269. static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
  270. {
  271. might_fault();
  272. return __strnlen_user(src, n);
  273. }
  274. /**
  275. * strlen_user: - Get the size of a string in user space.
  276. * @str: The string to measure.
  277. *
  278. * Context: User context only. This function may sleep.
  279. *
  280. * Get the size of a NUL-terminated string in user space.
  281. *
  282. * Returns the size of the string INCLUDING the terminating NUL.
  283. * On exception, returns 0.
  284. *
  285. * If there is a limit on the length of a valid string, you may wish to
  286. * consider using strnlen_user() instead.
  287. */
  288. #define strlen_user(str) strnlen_user(str, ~0UL)
  289. /*
  290. * Zero Userspace
  291. */
  292. unsigned long __must_check __clear_user(void __user *to, unsigned long size);
  293. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  294. {
  295. might_fault();
  296. return __clear_user(to, n);
  297. }
  298. int copy_to_user_real(void __user *dest, void *src, unsigned long count);
  299. #endif /* __S390_UACCESS_H */