1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright IBM Corp. 1999, 2000
8 * Derived from "include/asm-i386/uaccess.h"
10 #ifndef __S390_UACCESS_H
11 #define __S390_UACCESS_H
14 * User space memory access functions
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 <linux/instrumented.h>
23 void debug_user_asce(int exit);
25 unsigned long __must_check
26 raw_copy_from_user(void *to, const void __user *from, unsigned long n);
28 unsigned long __must_check
29 raw_copy_to_user(void __user *to, const void *from, unsigned long n);
32 #define INLINE_COPY_FROM_USER
33 #define INLINE_COPY_TO_USER
36 unsigned long __must_check
37 _copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key);
39 static __always_inline unsigned long __must_check
40 copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key)
42 if (check_copy_size(to, n, false))
43 n = _copy_from_user_key(to, from, n, key);
47 unsigned long __must_check
48 _copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key);
50 static __always_inline unsigned long __must_check
51 copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key)
53 if (check_copy_size(from, n, true))
54 n = _copy_to_user_key(to, from, n, key);
62 unsigned short key : 4;
64 unsigned short as : 2;
70 unsigned short key : 4;
72 unsigned short as : 2;
80 int __noreturn __put_user_bad(void);
83 #define get_put_user_noinstr_attributes \
84 noinline __maybe_unused __no_sanitize_memory
86 #define get_put_user_noinstr_attributes __always_inline
89 #define DEFINE_PUT_USER(type) \
90 static get_put_user_noinstr_attributes int \
91 __put_user_##type##_noinstr(unsigned type __user *to, \
92 unsigned type *from, \
95 union oac __oac_spec = { \
96 .oac1.as = PSW_BITS_AS_SECONDARY, \
103 "0: mvcos %[_to],%[_from],%[_size]\n" \
104 "1: xr %[rc],%[rc]\n" \
106 EX_TABLE_UA_STORE(0b, 2b, %[rc]) \
107 EX_TABLE_UA_STORE(1b, 2b, %[rc]) \
108 : [rc] "=&d" (rc), [_to] "+Q" (*(to)) \
109 : [_size] "d" (size), [_from] "Q" (*(from)), \
110 [spec] "d" (__oac_spec.val) \
115 static __always_inline int \
116 __put_user_##type(unsigned type __user *to, unsigned type *from, \
117 unsigned long size) \
121 rc = __put_user_##type##_noinstr(to, from, size); \
122 instrument_put_user(*from, to, size); \
126 DEFINE_PUT_USER(char);
127 DEFINE_PUT_USER(short);
128 DEFINE_PUT_USER(int);
129 DEFINE_PUT_USER(long);
131 static __always_inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
137 rc = __put_user_char((unsigned char __user *)ptr,
142 rc = __put_user_short((unsigned short __user *)ptr,
147 rc = __put_user_int((unsigned int __user *)ptr,
152 rc = __put_user_long((unsigned long __user *)ptr,
163 int __noreturn __get_user_bad(void);
165 #define DEFINE_GET_USER(type) \
166 static get_put_user_noinstr_attributes int \
167 __get_user_##type##_noinstr(unsigned type *to, \
168 unsigned type __user *from, \
169 unsigned long size) \
171 union oac __oac_spec = { \
172 .oac2.as = PSW_BITS_AS_SECONDARY, \
179 "0: mvcos 0(%[_to]),%[_from],%[_size]\n" \
180 "1: xr %[rc],%[rc]\n" \
182 EX_TABLE_UA_LOAD_MEM(0b, 2b, %[rc], %[_to], %[_ksize]) \
183 EX_TABLE_UA_LOAD_MEM(1b, 2b, %[rc], %[_to], %[_ksize]) \
184 : [rc] "=&d" (rc), "=Q" (*(to)) \
185 : [_size] "d" (size), [_from] "Q" (*(from)), \
186 [spec] "d" (__oac_spec.val), [_to] "a" (to), \
187 [_ksize] "K" (size) \
192 static __always_inline int \
193 __get_user_##type(unsigned type *to, unsigned type __user *from, \
194 unsigned long size) \
198 rc = __get_user_##type##_noinstr(to, from, size); \
199 instrument_get_user(*to); \
203 DEFINE_GET_USER(char);
204 DEFINE_GET_USER(short);
205 DEFINE_GET_USER(int);
206 DEFINE_GET_USER(long);
208 static __always_inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
214 rc = __get_user_char((unsigned char *)x,
215 (unsigned char __user *)ptr,
219 rc = __get_user_short((unsigned short *)x,
220 (unsigned short __user *)ptr,
224 rc = __get_user_int((unsigned int *)x,
225 (unsigned int __user *)ptr,
229 rc = __get_user_long((unsigned long *)x,
230 (unsigned long __user *)ptr,
241 * These are the main single-value transfer routines. They automatically
242 * use the right size if we just have the right pointer type.
244 #define __put_user(x, ptr) \
246 __typeof__(*(ptr)) __x = (x); \
247 int __pu_err = -EFAULT; \
249 __chk_user_ptr(ptr); \
250 switch (sizeof(*(ptr))) { \
255 __pu_err = __put_user_fn(&__x, ptr, sizeof(*(ptr))); \
261 __builtin_expect(__pu_err, 0); \
264 #define put_user(x, ptr) \
267 __put_user(x, ptr); \
270 #define __get_user(x, ptr) \
272 int __gu_err = -EFAULT; \
274 __chk_user_ptr(ptr); \
275 switch (sizeof(*(ptr))) { \
279 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
280 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
284 unsigned short __x; \
286 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
287 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
293 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
294 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
300 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
301 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
308 __builtin_expect(__gu_err, 0); \
311 #define get_user(x, ptr) \
314 __get_user(x, ptr); \
318 * Copy a null terminated string from userspace.
320 long __must_check strncpy_from_user(char *dst, const char __user *src, long count);
322 long __must_check strnlen_user(const char __user *src, long count);
327 unsigned long __must_check __clear_user(void __user *to, unsigned long size);
329 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
332 return __clear_user(to, n);
335 void *__s390_kernel_write(void *dst, const void *src, size_t size);
337 static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
339 if (__is_defined(__DECOMPRESSOR))
340 return memcpy(dst, src, size);
341 return __s390_kernel_write(dst, src, size);
344 int __noreturn __put_kernel_bad(void);
346 #define __put_kernel_asm(val, to, insn) \
351 "0: " insn " %[_val],%[_to]\n" \
352 "1: xr %[rc],%[rc]\n" \
354 EX_TABLE_UA_STORE(0b, 2b, %[rc]) \
355 EX_TABLE_UA_STORE(1b, 2b, %[rc]) \
356 : [rc] "=d" (__rc), [_to] "+Q" (*(to)) \
362 #define __put_kernel_nofault(dst, src, type, err_label) \
364 unsigned long __x = (unsigned long)(*((type *)(src))); \
367 switch (sizeof(type)) { \
369 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stc"); \
372 __pk_err = __put_kernel_asm(__x, (type *)(dst), "sth"); \
375 __pk_err = __put_kernel_asm(__x, (type *)(dst), "st"); \
378 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stg"); \
381 __pk_err = __put_kernel_bad(); \
384 if (unlikely(__pk_err)) \
388 int __noreturn __get_kernel_bad(void);
390 #define __get_kernel_asm(val, from, insn) \
395 "0: " insn " %[_val],%[_from]\n" \
396 "1: xr %[rc],%[rc]\n" \
398 EX_TABLE_UA_LOAD_REG(0b, 2b, %[rc], %[_val]) \
399 EX_TABLE_UA_LOAD_REG(1b, 2b, %[rc], %[_val]) \
400 : [rc] "=d" (__rc), [_val] "=d" (val) \
401 : [_from] "Q" (*(from)) \
406 #define __get_kernel_nofault(dst, src, type, err_label) \
410 switch (sizeof(type)) { \
414 __gk_err = __get_kernel_asm(__x, (type *)(src), "ic"); \
415 *((type *)(dst)) = (type)__x; \
419 unsigned short __x; \
421 __gk_err = __get_kernel_asm(__x, (type *)(src), "lh"); \
422 *((type *)(dst)) = (type)__x; \
428 __gk_err = __get_kernel_asm(__x, (type *)(src), "l"); \
429 *((type *)(dst)) = (type)__x; \
435 __gk_err = __get_kernel_asm(__x, (type *)(src), "lg"); \
436 *((type *)(dst)) = (type)__x; \
440 __gk_err = __get_kernel_bad(); \
443 if (unlikely(__gk_err)) \
447 void __cmpxchg_user_key_called_with_bad_pointer(void);
449 #define CMPXCHG_USER_KEY_MAX_LOOPS 128
451 static __always_inline int __cmpxchg_user_key(unsigned long address, void *uval,
452 __uint128_t old, __uint128_t new,
453 unsigned long key, int size)
459 unsigned int prev, shift, mask, _old, _new;
462 shift = (3 ^ (address & 3)) << 3;
463 address ^= address & 3;
464 _old = ((unsigned int)old & 0xff) << shift;
465 _new = ((unsigned int)new & 0xff) << shift;
466 mask = ~(0xff << shift);
470 " llill %[count],%[max_loops]\n"
471 "0: l %[prev],%[address]\n"
472 "1: nr %[prev],%[mask]\n"
473 " xilf %[mask],0xffffffff\n"
474 " or %[new],%[prev]\n"
475 " or %[prev],%[tmp]\n"
476 "2: lr %[tmp],%[prev]\n"
477 "3: cs %[prev],%[new],%[address]\n"
479 " xr %[tmp],%[prev]\n"
480 " xr %[new],%[tmp]\n"
481 " nr %[tmp],%[mask]\n"
483 " brct %[count],2b\n"
485 " spka %[default_key]\n"
486 EX_TABLE_UA_LOAD_REG(0b, 5b, %[rc], %[prev])
487 EX_TABLE_UA_LOAD_REG(1b, 5b, %[rc], %[prev])
488 EX_TABLE_UA_LOAD_REG(3b, 5b, %[rc], %[prev])
489 EX_TABLE_UA_LOAD_REG(4b, 5b, %[rc], %[prev])
492 [address] "+Q" (*(int *)address),
497 : [key] "%[count]" (key << 4),
498 [default_key] "J" (PAGE_DEFAULT_KEY),
499 [max_loops] "J" (CMPXCHG_USER_KEY_MAX_LOOPS)
501 *(unsigned char *)uval = prev >> shift;
507 unsigned int prev, shift, mask, _old, _new;
510 shift = (2 ^ (address & 2)) << 3;
511 address ^= address & 2;
512 _old = ((unsigned int)old & 0xffff) << shift;
513 _new = ((unsigned int)new & 0xffff) << shift;
514 mask = ~(0xffff << shift);
518 " llill %[count],%[max_loops]\n"
519 "0: l %[prev],%[address]\n"
520 "1: nr %[prev],%[mask]\n"
521 " xilf %[mask],0xffffffff\n"
522 " or %[new],%[prev]\n"
523 " or %[prev],%[tmp]\n"
524 "2: lr %[tmp],%[prev]\n"
525 "3: cs %[prev],%[new],%[address]\n"
527 " xr %[tmp],%[prev]\n"
528 " xr %[new],%[tmp]\n"
529 " nr %[tmp],%[mask]\n"
531 " brct %[count],2b\n"
533 " spka %[default_key]\n"
534 EX_TABLE_UA_LOAD_REG(0b, 5b, %[rc], %[prev])
535 EX_TABLE_UA_LOAD_REG(1b, 5b, %[rc], %[prev])
536 EX_TABLE_UA_LOAD_REG(3b, 5b, %[rc], %[prev])
537 EX_TABLE_UA_LOAD_REG(4b, 5b, %[rc], %[prev])
540 [address] "+Q" (*(int *)address),
545 : [key] "%[count]" (key << 4),
546 [default_key] "J" (PAGE_DEFAULT_KEY),
547 [max_loops] "J" (CMPXCHG_USER_KEY_MAX_LOOPS)
549 *(unsigned short *)uval = prev >> shift;
555 unsigned int prev = old;
560 "0: cs %[prev],%[new],%[address]\n"
562 " spka %[default_key]\n"
563 EX_TABLE_UA_LOAD_REG(0b, 1b, %[rc], %[prev])
564 EX_TABLE_UA_LOAD_REG(1b, 1b, %[rc], %[prev])
567 [address] "+Q" (*(int *)address)
568 : [new] "d" ((unsigned int)new),
569 [key] "a" (key << 4),
570 [default_key] "J" (PAGE_DEFAULT_KEY)
572 *(unsigned int *)uval = prev;
576 unsigned long prev = old;
581 "0: csg %[prev],%[new],%[address]\n"
583 " spka %[default_key]\n"
584 EX_TABLE_UA_LOAD_REG(0b, 1b, %[rc], %[prev])
585 EX_TABLE_UA_LOAD_REG(1b, 1b, %[rc], %[prev])
588 [address] "+QS" (*(long *)address)
589 : [new] "d" ((unsigned long)new),
590 [key] "a" (key << 4),
591 [default_key] "J" (PAGE_DEFAULT_KEY)
593 *(unsigned long *)uval = prev;
597 __uint128_t prev = old;
602 "0: cdsg %[prev],%[new],%[address]\n"
604 " spka %[default_key]\n"
605 EX_TABLE_UA_LOAD_REGPAIR(0b, 1b, %[rc], %[prev])
606 EX_TABLE_UA_LOAD_REGPAIR(1b, 1b, %[rc], %[prev])
609 [address] "+QS" (*(__int128_t *)address)
611 [key] "a" (key << 4),
612 [default_key] "J" (PAGE_DEFAULT_KEY)
614 *(__uint128_t *)uval = prev;
618 __cmpxchg_user_key_called_with_bad_pointer();
623 * cmpxchg_user_key() - cmpxchg with user space target, honoring storage keys
624 * @ptr: User space address of value to compare to @old and exchange with
625 * @new. Must be aligned to sizeof(*@ptr).
626 * @uval: Address where the old value of *@ptr is written to.
627 * @old: Old value. Compared to the content pointed to by @ptr in order to
628 * determine if the exchange occurs. The old value read from *@ptr is
630 * @new: New value to place at *@ptr.
631 * @key: Access key to use for checking storage key protection.
633 * Perform a cmpxchg on a user space target, honoring storage key protection.
634 * @key alone determines how key checking is performed, neither
635 * storage-protection-override nor fetch-protection-override apply.
636 * The caller must compare *@uval and @old to determine if values have been
637 * exchanged. In case of an exception *@uval is set to zero.
639 * Return: 0: cmpxchg executed
640 * -EFAULT: an exception happened when trying to access *@ptr
641 * -EAGAIN: maxed out number of retries (byte and short only)
643 #define cmpxchg_user_key(ptr, uval, old, new, key) \
645 __typeof__(ptr) __ptr = (ptr); \
646 __typeof__(uval) __uval = (uval); \
648 BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(*(__uval))); \
650 __chk_user_ptr(__ptr); \
651 __cmpxchg_user_key((unsigned long)(__ptr), (void *)(__uval), \
652 (old), (new), (key), sizeof(*(__ptr))); \
655 #endif /* __S390_UACCESS_H */