1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright IBM Corp. 1999, 2000
5 * Author(s): Hartmut Penner (hp@de.ibm.com),
6 * Martin Schwidefsky (schwidefsky@de.ibm.com)
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>
22 void debug_user_asce(int exit);
24 unsigned long __must_check
25 raw_copy_from_user(void *to, const void __user *from, unsigned long n);
27 unsigned long __must_check
28 raw_copy_to_user(void __user *to, const void *from, unsigned long n);
31 #define INLINE_COPY_FROM_USER
32 #define INLINE_COPY_TO_USER
35 unsigned long __must_check
36 _copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key);
38 static __always_inline unsigned long __must_check
39 copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key)
41 if (check_copy_size(to, n, false))
42 n = _copy_from_user_key(to, from, n, key);
46 unsigned long __must_check
47 _copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key);
49 static __always_inline unsigned long __must_check
50 copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key)
52 if (check_copy_size(from, n, true))
53 n = _copy_to_user_key(to, from, n, key);
61 unsigned short key : 4;
63 unsigned short as : 2;
69 unsigned short key : 4;
71 unsigned short as : 2;
79 int __noreturn __put_user_bad(void);
81 #define __put_user_asm(to, from, size) \
83 union oac __oac_spec = { \
84 .oac1.as = PSW_BITS_AS_SECONDARY, \
91 "0: mvcos %[_to],%[_from],%[_size]\n" \
92 "1: xr %[rc],%[rc]\n" \
94 EX_TABLE_UA_STORE(0b, 2b, %[rc]) \
95 EX_TABLE_UA_STORE(1b, 2b, %[rc]) \
96 : [rc] "=&d" (__rc), [_to] "+Q" (*(to)) \
97 : [_size] "d" (size), [_from] "Q" (*(from)), \
98 [spec] "d" (__oac_spec.val) \
103 static __always_inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
109 rc = __put_user_asm((unsigned char __user *)ptr,
114 rc = __put_user_asm((unsigned short __user *)ptr,
119 rc = __put_user_asm((unsigned int __user *)ptr,
124 rc = __put_user_asm((unsigned long __user *)ptr,
135 int __noreturn __get_user_bad(void);
137 #define __get_user_asm(to, from, size) \
139 union oac __oac_spec = { \
140 .oac2.as = PSW_BITS_AS_SECONDARY, \
147 "0: mvcos 0(%[_to]),%[_from],%[_size]\n" \
148 "1: xr %[rc],%[rc]\n" \
150 EX_TABLE_UA_LOAD_MEM(0b, 2b, %[rc], %[_to], %[_ksize]) \
151 EX_TABLE_UA_LOAD_MEM(1b, 2b, %[rc], %[_to], %[_ksize]) \
152 : [rc] "=&d" (__rc), "=Q" (*(to)) \
153 : [_size] "d" (size), [_from] "Q" (*(from)), \
154 [spec] "d" (__oac_spec.val), [_to] "a" (to), \
155 [_ksize] "K" (size) \
160 static __always_inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
166 rc = __get_user_asm((unsigned char *)x,
167 (unsigned char __user *)ptr,
171 rc = __get_user_asm((unsigned short *)x,
172 (unsigned short __user *)ptr,
176 rc = __get_user_asm((unsigned int *)x,
177 (unsigned int __user *)ptr,
181 rc = __get_user_asm((unsigned long *)x,
182 (unsigned long __user *)ptr,
193 * These are the main single-value transfer routines. They automatically
194 * use the right size if we just have the right pointer type.
196 #define __put_user(x, ptr) \
198 __typeof__(*(ptr)) __x = (x); \
199 int __pu_err = -EFAULT; \
201 __chk_user_ptr(ptr); \
202 switch (sizeof(*(ptr))) { \
207 __pu_err = __put_user_fn(&__x, ptr, sizeof(*(ptr))); \
213 __builtin_expect(__pu_err, 0); \
216 #define put_user(x, ptr) \
219 __put_user(x, ptr); \
222 #define __get_user(x, ptr) \
224 int __gu_err = -EFAULT; \
226 __chk_user_ptr(ptr); \
227 switch (sizeof(*(ptr))) { \
231 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
232 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
236 unsigned short __x; \
238 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
239 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
245 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
246 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
252 __gu_err = __get_user_fn(&__x, ptr, sizeof(*(ptr))); \
253 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
260 __builtin_expect(__gu_err, 0); \
263 #define get_user(x, ptr) \
266 __get_user(x, ptr); \
270 * Copy a null terminated string from userspace.
272 long __must_check strncpy_from_user(char *dst, const char __user *src, long count);
274 long __must_check strnlen_user(const char __user *src, long count);
279 unsigned long __must_check __clear_user(void __user *to, unsigned long size);
281 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
284 return __clear_user(to, n);
287 void *s390_kernel_write(void *dst, const void *src, size_t size);
289 int __noreturn __put_kernel_bad(void);
291 #define __put_kernel_asm(val, to, insn) \
296 "0: " insn " %[_val],%[_to]\n" \
297 "1: xr %[rc],%[rc]\n" \
299 EX_TABLE_UA_STORE(0b, 2b, %[rc]) \
300 EX_TABLE_UA_STORE(1b, 2b, %[rc]) \
301 : [rc] "=d" (__rc), [_to] "+Q" (*(to)) \
307 #define __put_kernel_nofault(dst, src, type, err_label) \
309 unsigned long __x = (unsigned long)(*((type *)(src))); \
312 switch (sizeof(type)) { \
314 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stc"); \
317 __pk_err = __put_kernel_asm(__x, (type *)(dst), "sth"); \
320 __pk_err = __put_kernel_asm(__x, (type *)(dst), "st"); \
323 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stg"); \
326 __pk_err = __put_kernel_bad(); \
329 if (unlikely(__pk_err)) \
333 int __noreturn __get_kernel_bad(void);
335 #define __get_kernel_asm(val, from, insn) \
340 "0: " insn " %[_val],%[_from]\n" \
341 "1: xr %[rc],%[rc]\n" \
343 EX_TABLE_UA_LOAD_REG(0b, 2b, %[rc], %[_val]) \
344 EX_TABLE_UA_LOAD_REG(1b, 2b, %[rc], %[_val]) \
345 : [rc] "=d" (__rc), [_val] "=d" (val) \
346 : [_from] "Q" (*(from)) \
351 #define __get_kernel_nofault(dst, src, type, err_label) \
355 switch (sizeof(type)) { \
359 __gk_err = __get_kernel_asm(__x, (type *)(src), "ic"); \
360 *((type *)(dst)) = (type)__x; \
364 unsigned short __x; \
366 __gk_err = __get_kernel_asm(__x, (type *)(src), "lh"); \
367 *((type *)(dst)) = (type)__x; \
373 __gk_err = __get_kernel_asm(__x, (type *)(src), "l"); \
374 *((type *)(dst)) = (type)__x; \
380 __gk_err = __get_kernel_asm(__x, (type *)(src), "lg"); \
381 *((type *)(dst)) = (type)__x; \
385 __gk_err = __get_kernel_bad(); \
388 if (unlikely(__gk_err)) \
392 void __cmpxchg_user_key_called_with_bad_pointer(void);
394 #define CMPXCHG_USER_KEY_MAX_LOOPS 128
396 static __always_inline int __cmpxchg_user_key(unsigned long address, void *uval,
397 __uint128_t old, __uint128_t new,
398 unsigned long key, int size)
404 unsigned int prev, shift, mask, _old, _new;
407 shift = (3 ^ (address & 3)) << 3;
408 address ^= address & 3;
409 _old = ((unsigned int)old & 0xff) << shift;
410 _new = ((unsigned int)new & 0xff) << shift;
411 mask = ~(0xff << shift);
415 " llill %[count],%[max_loops]\n"
416 "0: l %[prev],%[address]\n"
417 "1: nr %[prev],%[mask]\n"
418 " xilf %[mask],0xffffffff\n"
419 " or %[new],%[prev]\n"
420 " or %[prev],%[tmp]\n"
421 "2: lr %[tmp],%[prev]\n"
422 "3: cs %[prev],%[new],%[address]\n"
424 " xr %[tmp],%[prev]\n"
425 " xr %[new],%[tmp]\n"
426 " nr %[tmp],%[mask]\n"
428 " brct %[count],2b\n"
430 " spka %[default_key]\n"
431 EX_TABLE_UA_LOAD_REG(0b, 5b, %[rc], %[prev])
432 EX_TABLE_UA_LOAD_REG(1b, 5b, %[rc], %[prev])
433 EX_TABLE_UA_LOAD_REG(3b, 5b, %[rc], %[prev])
434 EX_TABLE_UA_LOAD_REG(4b, 5b, %[rc], %[prev])
437 [address] "+Q" (*(int *)address),
442 : [key] "%[count]" (key << 4),
443 [default_key] "J" (PAGE_DEFAULT_KEY),
444 [max_loops] "J" (CMPXCHG_USER_KEY_MAX_LOOPS)
446 *(unsigned char *)uval = prev >> shift;
452 unsigned int prev, shift, mask, _old, _new;
455 shift = (2 ^ (address & 2)) << 3;
456 address ^= address & 2;
457 _old = ((unsigned int)old & 0xffff) << shift;
458 _new = ((unsigned int)new & 0xffff) << shift;
459 mask = ~(0xffff << shift);
463 " llill %[count],%[max_loops]\n"
464 "0: l %[prev],%[address]\n"
465 "1: nr %[prev],%[mask]\n"
466 " xilf %[mask],0xffffffff\n"
467 " or %[new],%[prev]\n"
468 " or %[prev],%[tmp]\n"
469 "2: lr %[tmp],%[prev]\n"
470 "3: cs %[prev],%[new],%[address]\n"
472 " xr %[tmp],%[prev]\n"
473 " xr %[new],%[tmp]\n"
474 " nr %[tmp],%[mask]\n"
476 " brct %[count],2b\n"
478 " spka %[default_key]\n"
479 EX_TABLE_UA_LOAD_REG(0b, 5b, %[rc], %[prev])
480 EX_TABLE_UA_LOAD_REG(1b, 5b, %[rc], %[prev])
481 EX_TABLE_UA_LOAD_REG(3b, 5b, %[rc], %[prev])
482 EX_TABLE_UA_LOAD_REG(4b, 5b, %[rc], %[prev])
485 [address] "+Q" (*(int *)address),
490 : [key] "%[count]" (key << 4),
491 [default_key] "J" (PAGE_DEFAULT_KEY),
492 [max_loops] "J" (CMPXCHG_USER_KEY_MAX_LOOPS)
494 *(unsigned short *)uval = prev >> shift;
500 unsigned int prev = old;
505 "0: cs %[prev],%[new],%[address]\n"
507 " spka %[default_key]\n"
508 EX_TABLE_UA_LOAD_REG(0b, 1b, %[rc], %[prev])
509 EX_TABLE_UA_LOAD_REG(1b, 1b, %[rc], %[prev])
512 [address] "+Q" (*(int *)address)
513 : [new] "d" ((unsigned int)new),
514 [key] "a" (key << 4),
515 [default_key] "J" (PAGE_DEFAULT_KEY)
517 *(unsigned int *)uval = prev;
521 unsigned long prev = old;
526 "0: csg %[prev],%[new],%[address]\n"
528 " spka %[default_key]\n"
529 EX_TABLE_UA_LOAD_REG(0b, 1b, %[rc], %[prev])
530 EX_TABLE_UA_LOAD_REG(1b, 1b, %[rc], %[prev])
533 [address] "+QS" (*(long *)address)
534 : [new] "d" ((unsigned long)new),
535 [key] "a" (key << 4),
536 [default_key] "J" (PAGE_DEFAULT_KEY)
538 *(unsigned long *)uval = prev;
542 __uint128_t prev = old;
547 "0: cdsg %[prev],%[new],%[address]\n"
549 " spka %[default_key]\n"
550 EX_TABLE_UA_LOAD_REGPAIR(0b, 1b, %[rc], %[prev])
551 EX_TABLE_UA_LOAD_REGPAIR(1b, 1b, %[rc], %[prev])
554 [address] "+QS" (*(__int128_t *)address)
556 [key] "a" (key << 4),
557 [default_key] "J" (PAGE_DEFAULT_KEY)
559 *(__uint128_t *)uval = prev;
563 __cmpxchg_user_key_called_with_bad_pointer();
568 * cmpxchg_user_key() - cmpxchg with user space target, honoring storage keys
569 * @ptr: User space address of value to compare to @old and exchange with
570 * @new. Must be aligned to sizeof(*@ptr).
571 * @uval: Address where the old value of *@ptr is written to.
572 * @old: Old value. Compared to the content pointed to by @ptr in order to
573 * determine if the exchange occurs. The old value read from *@ptr is
575 * @new: New value to place at *@ptr.
576 * @key: Access key to use for checking storage key protection.
578 * Perform a cmpxchg on a user space target, honoring storage key protection.
579 * @key alone determines how key checking is performed, neither
580 * storage-protection-override nor fetch-protection-override apply.
581 * The caller must compare *@uval and @old to determine if values have been
582 * exchanged. In case of an exception *@uval is set to zero.
584 * Return: 0: cmpxchg executed
585 * -EFAULT: an exception happened when trying to access *@ptr
586 * -EAGAIN: maxed out number of retries (byte and short only)
588 #define cmpxchg_user_key(ptr, uval, old, new, key) \
590 __typeof__(ptr) __ptr = (ptr); \
591 __typeof__(uval) __uval = (uval); \
593 BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(*(__uval))); \
595 __chk_user_ptr(__ptr); \
596 __cmpxchg_user_key((unsigned long)(__ptr), (void *)(__uval), \
597 (old), (new), (key), sizeof(*(__ptr))); \
600 #endif /* __S390_UACCESS_H */