uaccess.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999, 2000
  5. * Author(s): Hartmut Penner (hp@de.ibm.com),
  6. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. *
  8. * Derived from "include/asm-i386/uaccess.h"
  9. */
  10. #ifndef __S390_UACCESS_H
  11. #define __S390_UACCESS_H
  12. /*
  13. * User space memory access functions
  14. */
  15. #include <linux/pgtable.h>
  16. #include <asm/asm-extable.h>
  17. #include <asm/processor.h>
  18. #include <asm/extable.h>
  19. #include <asm/facility.h>
  20. #include <asm-generic/access_ok.h>
  21. #include <asm/asce.h>
  22. #include <linux/instrumented.h>
  23. void debug_user_asce(int exit);
  24. #ifdef CONFIG_KMSAN
  25. #define uaccess_kmsan_or_inline noinline __maybe_unused __no_sanitize_memory
  26. #else
  27. #define uaccess_kmsan_or_inline __always_inline
  28. #endif
  29. #define INLINE_COPY_FROM_USER
  30. #define INLINE_COPY_TO_USER
  31. static uaccess_kmsan_or_inline __must_check unsigned long
  32. raw_copy_from_user(void *to, const void __user *from, unsigned long size)
  33. {
  34. unsigned long osize;
  35. int cc;
  36. while (1) {
  37. osize = size;
  38. asm_inline volatile(
  39. " lhi %%r0,%[spec]\n"
  40. "0: mvcos %[to],%[from],%[size]\n"
  41. "1: nopr %%r7\n"
  42. CC_IPM(cc)
  43. EX_TABLE_UA_MVCOS_FROM(0b, 0b)
  44. EX_TABLE_UA_MVCOS_FROM(1b, 0b)
  45. : CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char *)to)
  46. : [spec] "I" (0x81), [from] "Q" (*(const char __user *)from)
  47. : CC_CLOBBER_LIST("memory", "0"));
  48. if (__builtin_constant_p(osize) && osize <= 4096)
  49. return osize - size;
  50. if (likely(CC_TRANSFORM(cc) == 0))
  51. return osize - size;
  52. size -= 4096;
  53. to += 4096;
  54. from += 4096;
  55. }
  56. }
  57. static uaccess_kmsan_or_inline __must_check unsigned long
  58. raw_copy_to_user(void __user *to, const void *from, unsigned long size)
  59. {
  60. unsigned long osize;
  61. int cc;
  62. while (1) {
  63. osize = size;
  64. asm_inline volatile(
  65. " llilh %%r0,%[spec]\n"
  66. "0: mvcos %[to],%[from],%[size]\n"
  67. "1: nopr %%r7\n"
  68. CC_IPM(cc)
  69. EX_TABLE_UA_MVCOS_TO(0b, 0b)
  70. EX_TABLE_UA_MVCOS_TO(1b, 0b)
  71. : CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char __user *)to)
  72. : [spec] "I" (0x81), [from] "Q" (*(const char *)from)
  73. : CC_CLOBBER_LIST("memory", "0"));
  74. if (__builtin_constant_p(osize) && osize <= 4096)
  75. return osize - size;
  76. if (likely(CC_TRANSFORM(cc) == 0))
  77. return osize - size;
  78. size -= 4096;
  79. to += 4096;
  80. from += 4096;
  81. }
  82. }
  83. unsigned long __must_check
  84. _copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key);
  85. static __always_inline unsigned long __must_check
  86. copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key)
  87. {
  88. if (check_copy_size(to, n, false))
  89. n = _copy_from_user_key(to, from, n, key);
  90. return n;
  91. }
  92. unsigned long __must_check
  93. _copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key);
  94. static __always_inline unsigned long __must_check
  95. copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key)
  96. {
  97. if (check_copy_size(from, n, true))
  98. n = _copy_to_user_key(to, from, n, key);
  99. return n;
  100. }
  101. int __noreturn __put_user_bad(void);
  102. #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
  103. #define DEFINE_PUT_USER_NOINSTR(type) \
  104. static uaccess_kmsan_or_inline int \
  105. __put_user_##type##_noinstr(unsigned type __user *to, \
  106. unsigned type *from, \
  107. unsigned long size) \
  108. { \
  109. asm goto( \
  110. " llilh %%r0,%[spec]\n" \
  111. "0: mvcos %[to],%[from],%[size]\n" \
  112. "1: nopr %%r7\n" \
  113. EX_TABLE(0b, %l[Efault]) \
  114. EX_TABLE(1b, %l[Efault]) \
  115. : [to] "+Q" (*to) \
  116. : [size] "d" (size), [from] "Q" (*from), \
  117. [spec] "I" (0x81) \
  118. : "cc", "0" \
  119. : Efault \
  120. ); \
  121. return 0; \
  122. Efault: \
  123. return -EFAULT; \
  124. }
  125. #else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  126. #define DEFINE_PUT_USER_NOINSTR(type) \
  127. static uaccess_kmsan_or_inline int \
  128. __put_user_##type##_noinstr(unsigned type __user *to, \
  129. unsigned type *from, \
  130. unsigned long size) \
  131. { \
  132. int rc; \
  133. \
  134. asm_inline volatile( \
  135. " llilh %%r0,%[spec]\n" \
  136. "0: mvcos %[to],%[from],%[size]\n" \
  137. "1: lhi %[rc],0\n" \
  138. "2:\n" \
  139. EX_TABLE_UA_FAULT(0b, 2b, %[rc]) \
  140. EX_TABLE_UA_FAULT(1b, 2b, %[rc]) \
  141. : [rc] "=d" (rc), [to] "+Q" (*to) \
  142. : [size] "d" (size), [from] "Q" (*from), \
  143. [spec] "I" (0x81) \
  144. : "cc", "0"); \
  145. return rc; \
  146. }
  147. #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  148. DEFINE_PUT_USER_NOINSTR(char);
  149. DEFINE_PUT_USER_NOINSTR(short);
  150. DEFINE_PUT_USER_NOINSTR(int);
  151. DEFINE_PUT_USER_NOINSTR(long);
  152. #define DEFINE_PUT_USER(type) \
  153. static __always_inline int \
  154. __put_user_##type(unsigned type __user *to, unsigned type *from, \
  155. unsigned long size) \
  156. { \
  157. int rc; \
  158. \
  159. rc = __put_user_##type##_noinstr(to, from, size); \
  160. instrument_put_user(*from, to, size); \
  161. return rc; \
  162. }
  163. DEFINE_PUT_USER(char);
  164. DEFINE_PUT_USER(short);
  165. DEFINE_PUT_USER(int);
  166. DEFINE_PUT_USER(long);
  167. #define __put_user(x, ptr) \
  168. ({ \
  169. __typeof__(*(ptr)) __x = (x); \
  170. int __prc; \
  171. \
  172. __chk_user_ptr(ptr); \
  173. switch (sizeof(*(ptr))) { \
  174. case 1: \
  175. __prc = __put_user_char((unsigned char __user *)(ptr), \
  176. (unsigned char *)&__x, \
  177. sizeof(*(ptr))); \
  178. break; \
  179. case 2: \
  180. __prc = __put_user_short((unsigned short __user *)(ptr),\
  181. (unsigned short *)&__x, \
  182. sizeof(*(ptr))); \
  183. break; \
  184. case 4: \
  185. __prc = __put_user_int((unsigned int __user *)(ptr), \
  186. (unsigned int *)&__x, \
  187. sizeof(*(ptr))); \
  188. break; \
  189. case 8: \
  190. __prc = __put_user_long((unsigned long __user *)(ptr), \
  191. (unsigned long *)&__x, \
  192. sizeof(*(ptr))); \
  193. break; \
  194. default: \
  195. __prc = __put_user_bad(); \
  196. break; \
  197. } \
  198. __builtin_expect(__prc, 0); \
  199. })
  200. #define put_user(x, ptr) \
  201. ({ \
  202. might_fault(); \
  203. __put_user(x, ptr); \
  204. })
  205. int __noreturn __get_user_bad(void);
  206. #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
  207. #define DEFINE_GET_USER_NOINSTR(type) \
  208. static uaccess_kmsan_or_inline int \
  209. __get_user_##type##_noinstr(unsigned type *to, \
  210. const unsigned type __user *from, \
  211. unsigned long size) \
  212. { \
  213. asm goto( \
  214. " lhi %%r0,%[spec]\n" \
  215. "0: mvcos %[to],%[from],%[size]\n" \
  216. "1: nopr %%r7\n" \
  217. EX_TABLE(0b, %l[Efault]) \
  218. EX_TABLE(1b, %l[Efault]) \
  219. : [to] "=Q" (*to) \
  220. : [size] "d" (size), [from] "Q" (*from), \
  221. [spec] "I" (0x81) \
  222. : "cc", "0" \
  223. : Efault \
  224. ); \
  225. return 0; \
  226. Efault: \
  227. *to = 0; \
  228. return -EFAULT; \
  229. }
  230. #else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  231. #define DEFINE_GET_USER_NOINSTR(type) \
  232. static uaccess_kmsan_or_inline int \
  233. __get_user_##type##_noinstr(unsigned type *to, \
  234. const unsigned type __user *from, \
  235. unsigned long size) \
  236. { \
  237. int rc; \
  238. \
  239. asm_inline volatile( \
  240. " lhi %%r0,%[spec]\n" \
  241. "0: mvcos %[to],%[from],%[size]\n" \
  242. "1: lhi %[rc],0\n" \
  243. "2:\n" \
  244. EX_TABLE_UA_FAULT(0b, 2b, %[rc]) \
  245. EX_TABLE_UA_FAULT(1b, 2b, %[rc]) \
  246. : [rc] "=d" (rc), [to] "=Q" (*to) \
  247. : [size] "d" (size), [from] "Q" (*from), \
  248. [spec] "I" (0x81) \
  249. : "cc", "0"); \
  250. if (likely(!rc)) \
  251. return 0; \
  252. *to = 0; \
  253. return rc; \
  254. }
  255. #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  256. DEFINE_GET_USER_NOINSTR(char);
  257. DEFINE_GET_USER_NOINSTR(short);
  258. DEFINE_GET_USER_NOINSTR(int);
  259. DEFINE_GET_USER_NOINSTR(long);
  260. #define DEFINE_GET_USER(type) \
  261. static __always_inline int \
  262. __get_user_##type(unsigned type *to, const unsigned type __user *from, \
  263. unsigned long size) \
  264. { \
  265. int rc; \
  266. \
  267. rc = __get_user_##type##_noinstr(to, from, size); \
  268. instrument_get_user(*to); \
  269. return rc; \
  270. }
  271. DEFINE_GET_USER(char);
  272. DEFINE_GET_USER(short);
  273. DEFINE_GET_USER(int);
  274. DEFINE_GET_USER(long);
  275. #define __get_user(x, ptr) \
  276. ({ \
  277. const __user void *____guptr = (ptr); \
  278. int __grc; \
  279. \
  280. __chk_user_ptr(ptr); \
  281. switch (sizeof(*(ptr))) { \
  282. case 1: { \
  283. const unsigned char __user *__guptr = ____guptr; \
  284. unsigned char __x; \
  285. \
  286. __grc = __get_user_char(&__x, __guptr, sizeof(*(ptr))); \
  287. (x) = *(__force __typeof__(*(ptr)) *)&__x; \
  288. break; \
  289. }; \
  290. case 2: { \
  291. const unsigned short __user *__guptr = ____guptr; \
  292. unsigned short __x; \
  293. \
  294. __grc = __get_user_short(&__x, __guptr, sizeof(*(ptr)));\
  295. (x) = *(__force __typeof__(*(ptr)) *)&__x; \
  296. break; \
  297. }; \
  298. case 4: { \
  299. const unsigned int __user *__guptr = ____guptr; \
  300. unsigned int __x; \
  301. \
  302. __grc = __get_user_int(&__x, __guptr, sizeof(*(ptr))); \
  303. (x) = *(__force __typeof__(*(ptr)) *)&__x; \
  304. break; \
  305. }; \
  306. case 8: { \
  307. const unsigned long __user *__guptr = ____guptr; \
  308. unsigned long __x; \
  309. \
  310. __grc = __get_user_long(&__x, __guptr, sizeof(*(ptr))); \
  311. (x) = *(__force __typeof__(*(ptr)) *)&__x; \
  312. break; \
  313. }; \
  314. default: \
  315. __grc = __get_user_bad(); \
  316. break; \
  317. } \
  318. __builtin_expect(__grc, 0); \
  319. })
  320. #define get_user(x, ptr) \
  321. ({ \
  322. might_fault(); \
  323. __get_user(x, ptr); \
  324. })
  325. /*
  326. * Copy a null terminated string from userspace.
  327. */
  328. long __must_check strncpy_from_user(char *dst, const char __user *src, long count);
  329. long __must_check strnlen_user(const char __user *src, long count);
  330. static uaccess_kmsan_or_inline __must_check unsigned long
  331. __clear_user(void __user *to, unsigned long size)
  332. {
  333. unsigned long osize;
  334. int cc;
  335. while (1) {
  336. osize = size;
  337. asm_inline volatile(
  338. " llilh %%r0,%[spec]\n"
  339. "0: mvcos %[to],%[from],%[size]\n"
  340. "1: nopr %%r7\n"
  341. CC_IPM(cc)
  342. EX_TABLE_UA_MVCOS_TO(0b, 0b)
  343. EX_TABLE_UA_MVCOS_TO(1b, 0b)
  344. : CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char __user *)to)
  345. : [spec] "I" (0x81), [from] "Q" (*(const char *)empty_zero_page)
  346. : CC_CLOBBER_LIST("memory", "0"));
  347. if (__builtin_constant_p(osize) && osize <= 4096)
  348. return osize - size;
  349. if (CC_TRANSFORM(cc) == 0)
  350. return osize - size;
  351. size -= 4096;
  352. to += 4096;
  353. }
  354. }
  355. static __always_inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  356. {
  357. might_fault();
  358. return __clear_user(to, n);
  359. }
  360. void *__s390_kernel_write(void *dst, const void *src, size_t size);
  361. static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
  362. {
  363. if (__is_defined(__DECOMPRESSOR))
  364. return memcpy(dst, src, size);
  365. return __s390_kernel_write(dst, src, size);
  366. }
  367. void __noreturn __mvc_kernel_nofault_bad(void);
  368. #if defined(CONFIG_CC_HAS_ASM_GOTO_OUTPUT) && defined(CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS)
  369. #define __mvc_kernel_nofault(dst, src, type, err_label) \
  370. do { \
  371. switch (sizeof(type)) { \
  372. case 1: \
  373. case 2: \
  374. case 4: \
  375. case 8: \
  376. asm goto( \
  377. "0: mvc %O[_dst](%[_len],%R[_dst]),%[_src]\n" \
  378. "1: nopr %%r7\n" \
  379. EX_TABLE(0b, %l[err_label]) \
  380. EX_TABLE(1b, %l[err_label]) \
  381. : [_dst] "=Q" (*(type *)dst) \
  382. : [_src] "Q" (*(type *)(src)), \
  383. [_len] "I" (sizeof(type)) \
  384. : \
  385. : err_label); \
  386. break; \
  387. default: \
  388. __mvc_kernel_nofault_bad(); \
  389. break; \
  390. } \
  391. } while (0)
  392. #else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT) && CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS */
  393. #define __mvc_kernel_nofault(dst, src, type, err_label) \
  394. do { \
  395. type *(__dst) = (type *)(dst); \
  396. int __rc; \
  397. \
  398. switch (sizeof(type)) { \
  399. case 1: \
  400. case 2: \
  401. case 4: \
  402. case 8: \
  403. asm_inline volatile( \
  404. "0: mvc 0(%[_len],%[_dst]),%[_src]\n" \
  405. "1: lhi %[_rc],0\n" \
  406. "2:\n" \
  407. EX_TABLE_UA_FAULT(0b, 2b, %[_rc]) \
  408. EX_TABLE_UA_FAULT(1b, 2b, %[_rc]) \
  409. : [_rc] "=d" (__rc), \
  410. "=m" (*__dst) \
  411. : [_src] "Q" (*(type *)(src)), \
  412. [_dst] "a" (__dst), \
  413. [_len] "I" (sizeof(type))); \
  414. if (__rc) \
  415. goto err_label; \
  416. break; \
  417. default: \
  418. __mvc_kernel_nofault_bad(); \
  419. break; \
  420. } \
  421. } while (0)
  422. #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT && CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS */
  423. #define arch_get_kernel_nofault __mvc_kernel_nofault
  424. #define arch_put_kernel_nofault __mvc_kernel_nofault
  425. int __cmpxchg_key1(void *address, unsigned char *uval, unsigned char old,
  426. unsigned char new, unsigned long key);
  427. int __cmpxchg_key2(void *address, unsigned short *uval, unsigned short old,
  428. unsigned short new, unsigned long key);
  429. int __cmpxchg_key4(void *address, unsigned int *uval, unsigned int old,
  430. unsigned int new, unsigned long key);
  431. int __cmpxchg_key8(void *address, unsigned long *uval, unsigned long old,
  432. unsigned long new, unsigned long key);
  433. int __cmpxchg_key16(void *address, __uint128_t *uval, __uint128_t old,
  434. __uint128_t new, unsigned long key);
  435. #endif /* __S390_UACCESS_H */