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/processor.h>
17 #include <asm/ctl_reg.h>
18 #include <asm/extable.h>
19 #include <asm/facility.h>
21 void debug_user_asce(int exit);
23 static inline int __range_ok(unsigned long addr, unsigned long size)
28 #define __access_ok(addr, size) \
30 __chk_user_ptr(addr); \
31 __range_ok((unsigned long)(addr), (size)); \
34 #define access_ok(addr, size) __access_ok(addr, size)
36 unsigned long __must_check
37 raw_copy_from_user(void *to, const void __user *from, unsigned long n);
39 unsigned long __must_check
40 raw_copy_to_user(void __user *to, const void *from, unsigned long n);
43 #define INLINE_COPY_FROM_USER
44 #define INLINE_COPY_TO_USER
47 int __put_user_bad(void) __attribute__((noreturn));
48 int __get_user_bad(void) __attribute__((noreturn));
50 #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
52 #define __put_get_user_asm(to, from, size, insn) \
58 "0: mvcos %[_to],%[_from],%[_size]\n" \
59 "1: xr %[rc],%[rc]\n" \
61 ".pushsection .fixup, \"ax\"\n" \
62 "3: lhi %[rc],%[retval]\n" \
65 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
66 : [rc] "=&d" (__rc), [_to] "+Q" (*(to)) \
67 : [_size] "d" (size), [_from] "Q" (*(from)), \
68 [retval] "K" (-EFAULT), [spec] "K" (0x81UL) \
73 static __always_inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
79 rc = __put_get_user_asm((unsigned char __user *)ptr,
84 rc = __put_get_user_asm((unsigned short __user *)ptr,
89 rc = __put_get_user_asm((unsigned int __user *)ptr,
94 rc = __put_get_user_asm((unsigned long __user *)ptr,
105 static __always_inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
111 rc = __put_get_user_asm((unsigned char *)x,
112 (unsigned char __user *)ptr,
116 rc = __put_get_user_asm((unsigned short *)x,
117 (unsigned short __user *)ptr,
121 rc = __put_get_user_asm((unsigned int *)x,
122 (unsigned int __user *)ptr,
126 rc = __put_get_user_asm((unsigned long *)x,
127 (unsigned long __user *)ptr,
137 #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
139 static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
141 size = raw_copy_to_user(ptr, x, size);
142 return size ? -EFAULT : 0;
145 static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
147 size = raw_copy_from_user(x, ptr, size);
148 return size ? -EFAULT : 0;
151 #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
154 * These are the main single-value transfer routines. They automatically
155 * use the right size if we just have the right pointer type.
157 #define __put_user(x, ptr) \
159 __typeof__(*(ptr)) __x = (x); \
160 int __pu_err = -EFAULT; \
161 __chk_user_ptr(ptr); \
162 switch (sizeof (*(ptr))) { \
167 __pu_err = __put_user_fn(&__x, ptr, \
174 __builtin_expect(__pu_err, 0); \
177 #define put_user(x, ptr) \
180 __put_user(x, ptr); \
184 #define __get_user(x, ptr) \
186 int __gu_err = -EFAULT; \
187 __chk_user_ptr(ptr); \
188 switch (sizeof(*(ptr))) { \
190 unsigned char __x = 0; \
191 __gu_err = __get_user_fn(&__x, ptr, \
193 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
197 unsigned short __x = 0; \
198 __gu_err = __get_user_fn(&__x, ptr, \
200 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
204 unsigned int __x = 0; \
205 __gu_err = __get_user_fn(&__x, ptr, \
207 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
211 unsigned long long __x = 0; \
212 __gu_err = __get_user_fn(&__x, ptr, \
214 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
221 __builtin_expect(__gu_err, 0); \
224 #define get_user(x, ptr) \
227 __get_user(x, ptr); \
230 unsigned long __must_check
231 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
234 * Copy a null terminated string from userspace.
237 long __strncpy_from_user(char *dst, const char __user *src, long count);
239 static inline long __must_check
240 strncpy_from_user(char *dst, const char __user *src, long count)
243 return __strncpy_from_user(dst, src, count);
246 unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
248 static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
251 return __strnlen_user(src, n);
257 unsigned long __must_check __clear_user(void __user *to, unsigned long size);
259 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
262 return __clear_user(to, n);
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);
268 #define HAVE_GET_KERNEL_NOFAULT
270 int __noreturn __put_kernel_bad(void);
272 #define __put_kernel_asm(val, to, insn) \
277 "0: " insn " %2,%1\n" \
280 ".pushsection .fixup, \"ax\"\n" \
284 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
285 : "=d" (__rc), "+Q" (*(to)) \
286 : "d" (val), "K" (-EFAULT) \
291 #define __put_kernel_nofault(dst, src, type, err_label) \
293 u64 __x = (u64)(*((type *)(src))); \
296 switch (sizeof(type)) { \
298 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stc"); \
301 __pk_err = __put_kernel_asm(__x, (type *)(dst), "sth"); \
304 __pk_err = __put_kernel_asm(__x, (type *)(dst), "st"); \
307 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stg"); \
310 __pk_err = __put_kernel_bad(); \
313 if (unlikely(__pk_err)) \
317 int __noreturn __get_kernel_bad(void);
319 #define __get_kernel_asm(val, from, insn) \
324 "0: " insn " %1,%2\n" \
327 ".pushsection .fixup, \"ax\"\n" \
331 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
332 : "=d" (__rc), "+d" (val) \
333 : "Q" (*(from)), "K" (-EFAULT) \
338 #define __get_kernel_nofault(dst, src, type, err_label) \
342 switch (sizeof(type)) { \
346 __gk_err = __get_kernel_asm(__x, (type *)(src), "ic"); \
347 *((type *)(dst)) = (type)__x; \
353 __gk_err = __get_kernel_asm(__x, (type *)(src), "lh"); \
354 *((type *)(dst)) = (type)__x; \
360 __gk_err = __get_kernel_asm(__x, (type *)(src), "l"); \
361 *((type *)(dst)) = (type)__x; \
367 __gk_err = __get_kernel_asm(__x, (type *)(src), "lg"); \
368 *((type *)(dst)) = (type)__x; \
372 __gk_err = __get_kernel_bad(); \
375 if (unlikely(__gk_err)) \
379 #endif /* __S390_UACCESS_H */