GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / s390 / include / asm / uaccess.h
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 /*
14  * User space memory access functions
15  */
16 #include <asm/processor.h>
17 #include <asm/ctl_reg.h>
18 #include <asm/extable.h>
19
20
21 /*
22  * The fs value determines whether argument validity checking should be
23  * performed or not.  If get_fs() == USER_DS, checking is performed, with
24  * get_fs() == KERNEL_DS, checking is bypassed.
25  *
26  * For historical reasons, these macros are grossly misnamed.
27  */
28
29 #define MAKE_MM_SEG(a)  ((mm_segment_t) { (a) })
30
31
32 #define KERNEL_DS       MAKE_MM_SEG(0)
33 #define USER_DS         MAKE_MM_SEG(1)
34
35 #define get_ds()        (KERNEL_DS)
36 #define get_fs()        (current->thread.mm_segment)
37 #define segment_eq(a,b) ((a).ar4 == (b).ar4)
38
39 static inline void set_fs(mm_segment_t fs)
40 {
41         current->thread.mm_segment = fs;
42         if (uaccess_kernel()) {
43                 set_cpu_flag(CIF_ASCE_SECONDARY);
44                 __ctl_load(S390_lowcore.kernel_asce, 7, 7);
45         } else {
46                 clear_cpu_flag(CIF_ASCE_SECONDARY);
47                 __ctl_load(S390_lowcore.user_asce, 7, 7);
48         }
49 }
50
51 static inline int __range_ok(unsigned long addr, unsigned long size)
52 {
53         return 1;
54 }
55
56 #define __access_ok(addr, size)                         \
57 ({                                                      \
58         __chk_user_ptr(addr);                           \
59         __range_ok((unsigned long)(addr), (size));      \
60 })
61
62 #define access_ok(type, addr, size) __access_ok(addr, size)
63
64 unsigned long __must_check
65 raw_copy_from_user(void *to, const void __user *from, unsigned long n);
66
67 unsigned long __must_check
68 raw_copy_to_user(void __user *to, const void *from, unsigned long n);
69
70 #ifndef CONFIG_KASAN
71 #define INLINE_COPY_FROM_USER
72 #define INLINE_COPY_TO_USER
73 #endif
74
75 #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
76
77 #define __put_get_user_asm(to, from, size, spec)                \
78 ({                                                              \
79         register unsigned long __reg0 asm("0") = spec;          \
80         int __rc;                                               \
81                                                                 \
82         asm volatile(                                           \
83                 "0:     mvcos   %1,%3,%2\n"                     \
84                 "1:     xr      %0,%0\n"                        \
85                 "2:\n"                                          \
86                 ".pushsection .fixup, \"ax\"\n"                 \
87                 "3:     lhi     %0,%5\n"                        \
88                 "       jg      2b\n"                           \
89                 ".popsection\n"                                 \
90                 EX_TABLE(0b,3b) EX_TABLE(1b,3b)                 \
91                 : "=d" (__rc), "+Q" (*(to))                     \
92                 : "d" (size), "Q" (*(from)),                    \
93                   "d" (__reg0), "K" (-EFAULT)                   \
94                 : "cc");                                        \
95         __rc;                                                   \
96 })
97
98 static __always_inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
99 {
100         unsigned long spec = 0x810000UL;
101         int rc;
102
103         switch (size) {
104         case 1:
105                 rc = __put_get_user_asm((unsigned char __user *)ptr,
106                                         (unsigned char *)x,
107                                         size, spec);
108                 break;
109         case 2:
110                 rc = __put_get_user_asm((unsigned short __user *)ptr,
111                                         (unsigned short *)x,
112                                         size, spec);
113                 break;
114         case 4:
115                 rc = __put_get_user_asm((unsigned int __user *)ptr,
116                                         (unsigned int *)x,
117                                         size, spec);
118                 break;
119         case 8:
120                 rc = __put_get_user_asm((unsigned long __user *)ptr,
121                                         (unsigned long *)x,
122                                         size, spec);
123                 break;
124         }
125         return rc;
126 }
127
128 static __always_inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
129 {
130         unsigned long spec = 0x81UL;
131         int rc;
132
133         switch (size) {
134         case 1:
135                 rc = __put_get_user_asm((unsigned char *)x,
136                                         (unsigned char __user *)ptr,
137                                         size, spec);
138                 break;
139         case 2:
140                 rc = __put_get_user_asm((unsigned short *)x,
141                                         (unsigned short __user *)ptr,
142                                         size, spec);
143                 break;
144         case 4:
145                 rc = __put_get_user_asm((unsigned int *)x,
146                                         (unsigned int __user *)ptr,
147                                         size, spec);
148                 break;
149         case 8:
150                 rc = __put_get_user_asm((unsigned long *)x,
151                                         (unsigned long __user *)ptr,
152                                         size, spec);
153                 break;
154         }
155         return rc;
156 }
157
158 #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
159
160 static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
161 {
162         size = raw_copy_to_user(ptr, x, size);
163         return size ? -EFAULT : 0;
164 }
165
166 static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
167 {
168         size = raw_copy_from_user(x, ptr, size);
169         return size ? -EFAULT : 0;
170 }
171
172 #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
173
174 /*
175  * These are the main single-value transfer routines.  They automatically
176  * use the right size if we just have the right pointer type.
177  */
178 #define __put_user(x, ptr) \
179 ({                                                              \
180         __typeof__(*(ptr)) __x = (x);                           \
181         int __pu_err = -EFAULT;                                 \
182         __chk_user_ptr(ptr);                                    \
183         switch (sizeof (*(ptr))) {                              \
184         case 1:                                                 \
185         case 2:                                                 \
186         case 4:                                                 \
187         case 8:                                                 \
188                 __pu_err = __put_user_fn(&__x, ptr,             \
189                                          sizeof(*(ptr)));       \
190                 break;                                          \
191         default:                                                \
192                 __put_user_bad();                               \
193                 break;                                          \
194          }                                                      \
195         __builtin_expect(__pu_err, 0);                          \
196 })
197
198 #define put_user(x, ptr)                                        \
199 ({                                                              \
200         might_fault();                                          \
201         __put_user(x, ptr);                                     \
202 })
203
204
205 int __put_user_bad(void) __attribute__((noreturn));
206
207 #define __get_user(x, ptr)                                      \
208 ({                                                              \
209         int __gu_err = -EFAULT;                                 \
210         __chk_user_ptr(ptr);                                    \
211         switch (sizeof(*(ptr))) {                               \
212         case 1: {                                               \
213                 unsigned char __x = 0;                          \
214                 __gu_err = __get_user_fn(&__x, ptr,             \
215                                          sizeof(*(ptr)));       \
216                 (x) = *(__force __typeof__(*(ptr)) *) &__x;     \
217                 break;                                          \
218         };                                                      \
219         case 2: {                                               \
220                 unsigned short __x = 0;                         \
221                 __gu_err = __get_user_fn(&__x, ptr,             \
222                                          sizeof(*(ptr)));       \
223                 (x) = *(__force __typeof__(*(ptr)) *) &__x;     \
224                 break;                                          \
225         };                                                      \
226         case 4: {                                               \
227                 unsigned int __x = 0;                           \
228                 __gu_err = __get_user_fn(&__x, ptr,             \
229                                          sizeof(*(ptr)));       \
230                 (x) = *(__force __typeof__(*(ptr)) *) &__x;     \
231                 break;                                          \
232         };                                                      \
233         case 8: {                                               \
234                 unsigned long long __x = 0;                     \
235                 __gu_err = __get_user_fn(&__x, ptr,             \
236                                          sizeof(*(ptr)));       \
237                 (x) = *(__force __typeof__(*(ptr)) *) &__x;     \
238                 break;                                          \
239         };                                                      \
240         default:                                                \
241                 __get_user_bad();                               \
242                 break;                                          \
243         }                                                       \
244         __builtin_expect(__gu_err, 0);                          \
245 })
246
247 #define get_user(x, ptr)                                        \
248 ({                                                              \
249         might_fault();                                          \
250         __get_user(x, ptr);                                     \
251 })
252
253 int __get_user_bad(void) __attribute__((noreturn));
254
255 unsigned long __must_check
256 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
257
258 /*
259  * Copy a null terminated string from userspace.
260  */
261
262 long __strncpy_from_user(char *dst, const char __user *src, long count);
263
264 static inline long __must_check
265 strncpy_from_user(char *dst, const char __user *src, long count)
266 {
267         might_fault();
268         return __strncpy_from_user(dst, src, count);
269 }
270
271 unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
272
273 static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
274 {
275         might_fault();
276         return __strnlen_user(src, n);
277 }
278
279 /*
280  * Zero Userspace
281  */
282 unsigned long __must_check __clear_user(void __user *to, unsigned long size);
283
284 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
285 {
286         might_fault();
287         return __clear_user(to, n);
288 }
289
290 int copy_to_user_real(void __user *dest, void *src, unsigned long count);
291 void s390_kernel_write(void *dst, const void *src, size_t size);
292
293 #endif /* __S390_UACCESS_H */