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(void);
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, spec) \
54 register unsigned long __reg0 asm("0") = spec; \
58 "0: mvcos %1,%3,%2\n" \
61 ".pushsection .fixup, \"ax\"\n" \
65 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
66 : "=d" (__rc), "+Q" (*(to)) \
67 : "d" (size), "Q" (*(from)), \
68 "d" (__reg0), "K" (-EFAULT) \
73 static __always_inline
int __put_user_fn(void *x
, void __user
*ptr
, unsigned long size
)
75 unsigned long spec
= 0x810000UL
;
80 rc
= __put_get_user_asm((unsigned char __user
*)ptr
,
85 rc
= __put_get_user_asm((unsigned short __user
*)ptr
,
90 rc
= __put_get_user_asm((unsigned int __user
*)ptr
,
95 rc
= __put_get_user_asm((unsigned long __user
*)ptr
,
106 static __always_inline
int __get_user_fn(void *x
, const void __user
*ptr
, unsigned long size
)
108 unsigned long spec
= 0x81UL
;
113 rc
= __put_get_user_asm((unsigned char *)x
,
114 (unsigned char __user
*)ptr
,
118 rc
= __put_get_user_asm((unsigned short *)x
,
119 (unsigned short __user
*)ptr
,
123 rc
= __put_get_user_asm((unsigned int *)x
,
124 (unsigned int __user
*)ptr
,
128 rc
= __put_get_user_asm((unsigned long *)x
,
129 (unsigned long __user
*)ptr
,
139 #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
141 static inline int __put_user_fn(void *x
, void __user
*ptr
, unsigned long size
)
143 size
= raw_copy_to_user(ptr
, x
, size
);
144 return size
? -EFAULT
: 0;
147 static inline int __get_user_fn(void *x
, const void __user
*ptr
, unsigned long size
)
149 size
= raw_copy_from_user(x
, ptr
, size
);
150 return size
? -EFAULT
: 0;
153 #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
156 * These are the main single-value transfer routines. They automatically
157 * use the right size if we just have the right pointer type.
159 #define __put_user(x, ptr) \
161 __typeof__(*(ptr)) __x = (x); \
162 int __pu_err = -EFAULT; \
163 __chk_user_ptr(ptr); \
164 switch (sizeof (*(ptr))) { \
169 __pu_err = __put_user_fn(&__x, ptr, \
176 __builtin_expect(__pu_err, 0); \
179 #define put_user(x, ptr) \
182 __put_user(x, ptr); \
186 #define __get_user(x, ptr) \
188 int __gu_err = -EFAULT; \
189 __chk_user_ptr(ptr); \
190 switch (sizeof(*(ptr))) { \
192 unsigned char __x = 0; \
193 __gu_err = __get_user_fn(&__x, ptr, \
195 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
199 unsigned short __x = 0; \
200 __gu_err = __get_user_fn(&__x, ptr, \
202 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
206 unsigned int __x = 0; \
207 __gu_err = __get_user_fn(&__x, ptr, \
209 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
213 unsigned long long __x = 0; \
214 __gu_err = __get_user_fn(&__x, ptr, \
216 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
223 __builtin_expect(__gu_err, 0); \
226 #define get_user(x, ptr) \
229 __get_user(x, ptr); \
232 unsigned long __must_check
233 raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
);
236 * Copy a null terminated string from userspace.
239 long __strncpy_from_user(char *dst
, const char __user
*src
, long count
);
241 static inline long __must_check
242 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
245 return __strncpy_from_user(dst
, src
, count
);
248 unsigned long __must_check
__strnlen_user(const char __user
*src
, unsigned long count
);
250 static inline unsigned long strnlen_user(const char __user
*src
, unsigned long n
)
253 return __strnlen_user(src
, n
);
259 unsigned long __must_check
__clear_user(void __user
*to
, unsigned long size
);
261 static inline unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
264 return __clear_user(to
, n
);
267 int copy_to_user_real(void __user
*dest
, void *src
, unsigned long count
);
268 void *s390_kernel_write(void *dst
, const void *src
, size_t size
);
270 #define HAVE_GET_KERNEL_NOFAULT
272 int __noreturn
__put_kernel_bad(void);
274 #define __put_kernel_asm(val, to, insn) \
279 "0: " insn " %2,%1\n" \
282 ".pushsection .fixup, \"ax\"\n" \
286 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
287 : "=d" (__rc), "+Q" (*(to)) \
288 : "d" (val), "K" (-EFAULT) \
293 #define __put_kernel_nofault(dst, src, type, err_label) \
295 u64 __x = (u64)(*((type *)(src))); \
298 switch (sizeof(type)) { \
300 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stc"); \
303 __pk_err = __put_kernel_asm(__x, (type *)(dst), "sth"); \
306 __pk_err = __put_kernel_asm(__x, (type *)(dst), "st"); \
309 __pk_err = __put_kernel_asm(__x, (type *)(dst), "stg"); \
312 __pk_err = __put_kernel_bad(); \
315 if (unlikely(__pk_err)) \
319 int __noreturn
__get_kernel_bad(void);
321 #define __get_kernel_asm(val, from, insn) \
326 "0: " insn " %1,%2\n" \
329 ".pushsection .fixup, \"ax\"\n" \
333 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
334 : "=d" (__rc), "+d" (val) \
335 : "Q" (*(from)), "K" (-EFAULT) \
340 #define __get_kernel_nofault(dst, src, type, err_label) \
344 switch (sizeof(type)) { \
348 __gk_err = __get_kernel_asm(__x, (type *)(src), "ic"); \
349 *((type *)(dst)) = (type)__x; \
355 __gk_err = __get_kernel_asm(__x, (type *)(src), "lh"); \
356 *((type *)(dst)) = (type)__x; \
362 __gk_err = __get_kernel_asm(__x, (type *)(src), "l"); \
363 *((type *)(dst)) = (type)__x; \
369 __gk_err = __get_kernel_asm(__x, (type *)(src), "lg"); \
370 *((type *)(dst)) = (type)__x; \
374 __gk_err = __get_kernel_bad(); \
377 if (unlikely(__gk_err)) \
381 #endif /* __S390_UACCESS_H */