uaccess.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <asm/processor.h>
  15. #include <asm/ctl_reg.h>
  16. #include <asm/extable.h>
  17. /*
  18. * The fs value determines whether argument validity checking should be
  19. * performed or not. If get_fs() == USER_DS, checking is performed, with
  20. * get_fs() == KERNEL_DS, checking is bypassed.
  21. *
  22. * For historical reasons, these macros are grossly misnamed.
  23. */
  24. #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
  25. #define KERNEL_DS MAKE_MM_SEG(0)
  26. #define USER_DS MAKE_MM_SEG(1)
  27. #define get_ds() (KERNEL_DS)
  28. #define get_fs() (current->thread.mm_segment)
  29. #define segment_eq(a,b) ((a).ar4 == (b).ar4)
  30. static inline void set_fs(mm_segment_t fs)
  31. {
  32. current->thread.mm_segment = fs;
  33. if (uaccess_kernel()) {
  34. set_cpu_flag(CIF_ASCE_SECONDARY);
  35. __ctl_load(S390_lowcore.kernel_asce, 7, 7);
  36. } else {
  37. clear_cpu_flag(CIF_ASCE_SECONDARY);
  38. __ctl_load(S390_lowcore.user_asce, 7, 7);
  39. }
  40. }
  41. static inline int __range_ok(unsigned long addr, unsigned long size)
  42. {
  43. return 1;
  44. }
  45. #define __access_ok(addr, size) \
  46. ({ \
  47. __chk_user_ptr(addr); \
  48. __range_ok((unsigned long)(addr), (size)); \
  49. })
  50. #define access_ok(type, addr, size) __access_ok(addr, size)
  51. unsigned long __must_check
  52. raw_copy_from_user(void *to, const void __user *from, unsigned long n);
  53. unsigned long __must_check
  54. raw_copy_to_user(void __user *to, const void *from, unsigned long n);
  55. #define INLINE_COPY_FROM_USER
  56. #define INLINE_COPY_TO_USER
  57. #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
  58. #define __put_get_user_asm(to, from, size, spec) \
  59. ({ \
  60. register unsigned long __reg0 asm("0") = spec; \
  61. int __rc; \
  62. \
  63. asm volatile( \
  64. "0: mvcos %1,%3,%2\n" \
  65. "1: xr %0,%0\n" \
  66. "2:\n" \
  67. ".pushsection .fixup, \"ax\"\n" \
  68. "3: lhi %0,%5\n" \
  69. " jg 2b\n" \
  70. ".popsection\n" \
  71. EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
  72. : "=d" (__rc), "+Q" (*(to)) \
  73. : "d" (size), "Q" (*(from)), \
  74. "d" (__reg0), "K" (-EFAULT) \
  75. : "cc"); \
  76. __rc; \
  77. })
  78. static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
  79. {
  80. unsigned long spec = 0x810000UL;
  81. int rc;
  82. switch (size) {
  83. case 1:
  84. rc = __put_get_user_asm((unsigned char __user *)ptr,
  85. (unsigned char *)x,
  86. size, spec);
  87. break;
  88. case 2:
  89. rc = __put_get_user_asm((unsigned short __user *)ptr,
  90. (unsigned short *)x,
  91. size, spec);
  92. break;
  93. case 4:
  94. rc = __put_get_user_asm((unsigned int __user *)ptr,
  95. (unsigned int *)x,
  96. size, spec);
  97. break;
  98. case 8:
  99. rc = __put_get_user_asm((unsigned long __user *)ptr,
  100. (unsigned long *)x,
  101. size, spec);
  102. break;
  103. }
  104. return rc;
  105. }
  106. static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
  107. {
  108. unsigned long spec = 0x81UL;
  109. int rc;
  110. switch (size) {
  111. case 1:
  112. rc = __put_get_user_asm((unsigned char *)x,
  113. (unsigned char __user *)ptr,
  114. size, spec);
  115. break;
  116. case 2:
  117. rc = __put_get_user_asm((unsigned short *)x,
  118. (unsigned short __user *)ptr,
  119. size, spec);
  120. break;
  121. case 4:
  122. rc = __put_get_user_asm((unsigned int *)x,
  123. (unsigned int __user *)ptr,
  124. size, spec);
  125. break;
  126. case 8:
  127. rc = __put_get_user_asm((unsigned long *)x,
  128. (unsigned long __user *)ptr,
  129. size, spec);
  130. break;
  131. }
  132. return rc;
  133. }
  134. #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
  135. static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
  136. {
  137. size = raw_copy_to_user(ptr, x, size);
  138. return size ? -EFAULT : 0;
  139. }
  140. static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
  141. {
  142. size = raw_copy_from_user(x, ptr, size);
  143. return size ? -EFAULT : 0;
  144. }
  145. #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
  146. /*
  147. * These are the main single-value transfer routines. They automatically
  148. * use the right size if we just have the right pointer type.
  149. */
  150. #define __put_user(x, ptr) \
  151. ({ \
  152. __typeof__(*(ptr)) __x = (x); \
  153. int __pu_err = -EFAULT; \
  154. __chk_user_ptr(ptr); \
  155. switch (sizeof (*(ptr))) { \
  156. case 1: \
  157. case 2: \
  158. case 4: \
  159. case 8: \
  160. __pu_err = __put_user_fn(&__x, ptr, \
  161. sizeof(*(ptr))); \
  162. break; \
  163. default: \
  164. __put_user_bad(); \
  165. break; \
  166. } \
  167. __builtin_expect(__pu_err, 0); \
  168. })
  169. #define put_user(x, ptr) \
  170. ({ \
  171. might_fault(); \
  172. __put_user(x, ptr); \
  173. })
  174. int __put_user_bad(void) __attribute__((noreturn));
  175. #define __get_user(x, ptr) \
  176. ({ \
  177. int __gu_err = -EFAULT; \
  178. __chk_user_ptr(ptr); \
  179. switch (sizeof(*(ptr))) { \
  180. case 1: { \
  181. unsigned char __x = 0; \
  182. __gu_err = __get_user_fn(&__x, ptr, \
  183. sizeof(*(ptr))); \
  184. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  185. break; \
  186. }; \
  187. case 2: { \
  188. unsigned short __x = 0; \
  189. __gu_err = __get_user_fn(&__x, ptr, \
  190. sizeof(*(ptr))); \
  191. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  192. break; \
  193. }; \
  194. case 4: { \
  195. unsigned int __x = 0; \
  196. __gu_err = __get_user_fn(&__x, ptr, \
  197. sizeof(*(ptr))); \
  198. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  199. break; \
  200. }; \
  201. case 8: { \
  202. unsigned long long __x = 0; \
  203. __gu_err = __get_user_fn(&__x, ptr, \
  204. sizeof(*(ptr))); \
  205. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  206. break; \
  207. }; \
  208. default: \
  209. __get_user_bad(); \
  210. break; \
  211. } \
  212. __builtin_expect(__gu_err, 0); \
  213. })
  214. #define get_user(x, ptr) \
  215. ({ \
  216. might_fault(); \
  217. __get_user(x, ptr); \
  218. })
  219. int __get_user_bad(void) __attribute__((noreturn));
  220. #define __put_user_unaligned __put_user
  221. #define __get_user_unaligned __get_user
  222. unsigned long __must_check
  223. raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
  224. /*
  225. * Copy a null terminated string from userspace.
  226. */
  227. long __strncpy_from_user(char *dst, const char __user *src, long count);
  228. static inline long __must_check
  229. strncpy_from_user(char *dst, const char __user *src, long count)
  230. {
  231. might_fault();
  232. return __strncpy_from_user(dst, src, count);
  233. }
  234. unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
  235. static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
  236. {
  237. might_fault();
  238. return __strnlen_user(src, n);
  239. }
  240. /**
  241. * strlen_user: - Get the size of a string in user space.
  242. * @str: The string to measure.
  243. *
  244. * Context: User context only. This function may sleep if pagefaults are
  245. * enabled.
  246. *
  247. * Get the size of a NUL-terminated string in user space.
  248. *
  249. * Returns the size of the string INCLUDING the terminating NUL.
  250. * On exception, returns 0.
  251. *
  252. * If there is a limit on the length of a valid string, you may wish to
  253. * consider using strnlen_user() instead.
  254. */
  255. #define strlen_user(str) strnlen_user(str, ~0UL)
  256. /*
  257. * Zero Userspace
  258. */
  259. unsigned long __must_check __clear_user(void __user *to, unsigned long size);
  260. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  261. {
  262. might_fault();
  263. return __clear_user(to, n);
  264. }
  265. int copy_to_user_real(void __user *dest, void *src, unsigned long count);
  266. void s390_kernel_write(void *dst, const void *src, size_t size);
  267. #endif /* __S390_UACCESS_H */