Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Standard user space access functions based on mvcp/mvcs and doing
4 * interesting things in the secondary space mode.
5 *
6 * Copyright IBM Corp. 2006,2014
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
9 */
10
11#include <linux/uaccess.h>
12#include <linux/export.h>
13#include <linux/mm.h>
14#include <asm/asm-extable.h>
15#include <asm/ctlreg.h>
16
17#ifdef CONFIG_DEBUG_ENTRY
18void debug_user_asce(int exit)
19{
20 struct ctlreg cr1, cr7;
21
22 local_ctl_store(1, &cr1);
23 local_ctl_store(7, &cr7);
24 if (cr1.val == S390_lowcore.kernel_asce.val && cr7.val == S390_lowcore.user_asce.val)
25 return;
26 panic("incorrect ASCE on kernel %s\n"
27 "cr1: %016lx cr7: %016lx\n"
28 "kernel: %016lx user: %016lx\n",
29 exit ? "exit" : "entry", cr1.val, cr7.val,
30 S390_lowcore.kernel_asce.val, S390_lowcore.user_asce.val);
31}
32#endif /*CONFIG_DEBUG_ENTRY */
33
34static unsigned long raw_copy_from_user_key(void *to, const void __user *from,
35 unsigned long size, unsigned long key)
36{
37 unsigned long rem;
38 union oac spec = {
39 .oac2.key = key,
40 .oac2.as = PSW_BITS_AS_SECONDARY,
41 .oac2.k = 1,
42 .oac2.a = 1,
43 };
44
45 asm volatile(
46 " lr 0,%[spec]\n"
47 "0: mvcos 0(%[to]),0(%[from]),%[size]\n"
48 "1: jz 5f\n"
49 " algr %[size],%[val]\n"
50 " slgr %[from],%[val]\n"
51 " slgr %[to],%[val]\n"
52 " j 0b\n"
53 "2: la %[rem],4095(%[from])\n" /* rem = from + 4095 */
54 " nr %[rem],%[val]\n" /* rem = (from + 4095) & -4096 */
55 " slgr %[rem],%[from]\n"
56 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
57 " jnh 6f\n"
58 "3: mvcos 0(%[to]),0(%[from]),%[rem]\n"
59 "4: slgr %[size],%[rem]\n"
60 " j 6f\n"
61 "5: slgr %[size],%[size]\n"
62 "6:\n"
63 EX_TABLE(0b, 2b)
64 EX_TABLE(1b, 2b)
65 EX_TABLE(3b, 6b)
66 EX_TABLE(4b, 6b)
67 : [size] "+&a" (size), [from] "+&a" (from), [to] "+&a" (to), [rem] "=&a" (rem)
68 : [val] "a" (-4096UL), [spec] "d" (spec.val)
69 : "cc", "memory", "0");
70 return size;
71}
72
73unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
74{
75 return raw_copy_from_user_key(to, from, n, 0);
76}
77EXPORT_SYMBOL(raw_copy_from_user);
78
79unsigned long _copy_from_user_key(void *to, const void __user *from,
80 unsigned long n, unsigned long key)
81{
82 unsigned long res = n;
83
84 might_fault();
85 if (!should_fail_usercopy()) {
86 instrument_copy_from_user_before(to, from, n);
87 res = raw_copy_from_user_key(to, from, n, key);
88 instrument_copy_from_user_after(to, from, n, res);
89 }
90 if (unlikely(res))
91 memset(to + (n - res), 0, res);
92 return res;
93}
94EXPORT_SYMBOL(_copy_from_user_key);
95
96static unsigned long raw_copy_to_user_key(void __user *to, const void *from,
97 unsigned long size, unsigned long key)
98{
99 unsigned long rem;
100 union oac spec = {
101 .oac1.key = key,
102 .oac1.as = PSW_BITS_AS_SECONDARY,
103 .oac1.k = 1,
104 .oac1.a = 1,
105 };
106
107 asm volatile(
108 " lr 0,%[spec]\n"
109 "0: mvcos 0(%[to]),0(%[from]),%[size]\n"
110 "1: jz 5f\n"
111 " algr %[size],%[val]\n"
112 " slgr %[to],%[val]\n"
113 " slgr %[from],%[val]\n"
114 " j 0b\n"
115 "2: la %[rem],4095(%[to])\n" /* rem = to + 4095 */
116 " nr %[rem],%[val]\n" /* rem = (to + 4095) & -4096 */
117 " slgr %[rem],%[to]\n"
118 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
119 " jnh 6f\n"
120 "3: mvcos 0(%[to]),0(%[from]),%[rem]\n"
121 "4: slgr %[size],%[rem]\n"
122 " j 6f\n"
123 "5: slgr %[size],%[size]\n"
124 "6:\n"
125 EX_TABLE(0b, 2b)
126 EX_TABLE(1b, 2b)
127 EX_TABLE(3b, 6b)
128 EX_TABLE(4b, 6b)
129 : [size] "+&a" (size), [to] "+&a" (to), [from] "+&a" (from), [rem] "=&a" (rem)
130 : [val] "a" (-4096UL), [spec] "d" (spec.val)
131 : "cc", "memory", "0");
132 return size;
133}
134
135unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
136{
137 return raw_copy_to_user_key(to, from, n, 0);
138}
139EXPORT_SYMBOL(raw_copy_to_user);
140
141unsigned long _copy_to_user_key(void __user *to, const void *from,
142 unsigned long n, unsigned long key)
143{
144 might_fault();
145 if (should_fail_usercopy())
146 return n;
147 instrument_copy_to_user(to, from, n);
148 return raw_copy_to_user_key(to, from, n, key);
149}
150EXPORT_SYMBOL(_copy_to_user_key);
151
152unsigned long __clear_user(void __user *to, unsigned long size)
153{
154 unsigned long rem;
155 union oac spec = {
156 .oac1.as = PSW_BITS_AS_SECONDARY,
157 .oac1.a = 1,
158 };
159
160 asm volatile(
161 " lr 0,%[spec]\n"
162 "0: mvcos 0(%[to]),0(%[zeropg]),%[size]\n"
163 "1: jz 5f\n"
164 " algr %[size],%[val]\n"
165 " slgr %[to],%[val]\n"
166 " j 0b\n"
167 "2: la %[rem],4095(%[to])\n" /* rem = to + 4095 */
168 " nr %[rem],%[val]\n" /* rem = (to + 4095) & -4096 */
169 " slgr %[rem],%[to]\n"
170 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
171 " jnh 6f\n"
172 "3: mvcos 0(%[to]),0(%[zeropg]),%[rem]\n"
173 "4: slgr %[size],%[rem]\n"
174 " j 6f\n"
175 "5: slgr %[size],%[size]\n"
176 "6:\n"
177 EX_TABLE(0b, 2b)
178 EX_TABLE(1b, 2b)
179 EX_TABLE(3b, 6b)
180 EX_TABLE(4b, 6b)
181 : [size] "+&a" (size), [to] "+&a" (to), [rem] "=&a" (rem)
182 : [val] "a" (-4096UL), [zeropg] "a" (empty_zero_page), [spec] "d" (spec.val)
183 : "cc", "memory", "0");
184 return size;
185}
186EXPORT_SYMBOL(__clear_user);
1/*
2 * Standard user space access functions based on mvcp/mvcs and doing
3 * interesting things in the secondary space mode.
4 *
5 * Copyright IBM Corp. 2006,2014
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
8 */
9
10#include <linux/jump_label.h>
11#include <linux/uaccess.h>
12#include <linux/export.h>
13#include <linux/errno.h>
14#include <linux/mm.h>
15#include <asm/mmu_context.h>
16#include <asm/facility.h>
17
18static DEFINE_STATIC_KEY_FALSE(have_mvcos);
19
20static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
21 unsigned long size)
22{
23 register unsigned long reg0 asm("0") = 0x81UL;
24 unsigned long tmp1, tmp2;
25
26 tmp1 = -4096UL;
27 asm volatile(
28 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
29 "9: jz 7f\n"
30 "1: algr %0,%3\n"
31 " slgr %1,%3\n"
32 " slgr %2,%3\n"
33 " j 0b\n"
34 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
35 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
36 " slgr %4,%1\n"
37 " clgr %0,%4\n" /* copy crosses next page boundary? */
38 " jnh 4f\n"
39 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
40 "10:slgr %0,%4\n"
41 " algr %2,%4\n"
42 "4: lghi %4,-1\n"
43 " algr %4,%0\n" /* copy remaining size, subtract 1 */
44 " bras %3,6f\n" /* memset loop */
45 " xc 0(1,%2),0(%2)\n"
46 "5: xc 0(256,%2),0(%2)\n"
47 " la %2,256(%2)\n"
48 "6: aghi %4,-256\n"
49 " jnm 5b\n"
50 " ex %4,0(%3)\n"
51 " j 8f\n"
52 "7: slgr %0,%0\n"
53 "8:\n"
54 EX_TABLE(0b,2b) EX_TABLE(3b,4b) EX_TABLE(9b,2b) EX_TABLE(10b,4b)
55 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
56 : "d" (reg0) : "cc", "memory");
57 return size;
58}
59
60static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
61 unsigned long size)
62{
63 unsigned long tmp1, tmp2;
64
65 load_kernel_asce();
66 tmp1 = -256UL;
67 asm volatile(
68 " sacf 0\n"
69 "0: mvcp 0(%0,%2),0(%1),%3\n"
70 "10:jz 8f\n"
71 "1: algr %0,%3\n"
72 " la %1,256(%1)\n"
73 " la %2,256(%2)\n"
74 "2: mvcp 0(%0,%2),0(%1),%3\n"
75 "11:jnz 1b\n"
76 " j 8f\n"
77 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
78 " lghi %3,-4096\n"
79 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
80 " slgr %4,%1\n"
81 " clgr %0,%4\n" /* copy crosses next page boundary? */
82 " jnh 5f\n"
83 "4: mvcp 0(%4,%2),0(%1),%3\n"
84 "12:slgr %0,%4\n"
85 " algr %2,%4\n"
86 "5: lghi %4,-1\n"
87 " algr %4,%0\n" /* copy remaining size, subtract 1 */
88 " bras %3,7f\n" /* memset loop */
89 " xc 0(1,%2),0(%2)\n"
90 "6: xc 0(256,%2),0(%2)\n"
91 " la %2,256(%2)\n"
92 "7: aghi %4,-256\n"
93 " jnm 6b\n"
94 " ex %4,0(%3)\n"
95 " j 9f\n"
96 "8: slgr %0,%0\n"
97 "9: sacf 768\n"
98 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,5b)
99 EX_TABLE(10b,3b) EX_TABLE(11b,3b) EX_TABLE(12b,5b)
100 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
101 : : "cc", "memory");
102 return size;
103}
104
105unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
106{
107 check_object_size(to, n, false);
108 if (static_branch_likely(&have_mvcos))
109 return copy_from_user_mvcos(to, from, n);
110 return copy_from_user_mvcp(to, from, n);
111}
112EXPORT_SYMBOL(__copy_from_user);
113
114static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
115 unsigned long size)
116{
117 register unsigned long reg0 asm("0") = 0x810000UL;
118 unsigned long tmp1, tmp2;
119
120 tmp1 = -4096UL;
121 asm volatile(
122 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
123 "6: jz 4f\n"
124 "1: algr %0,%3\n"
125 " slgr %1,%3\n"
126 " slgr %2,%3\n"
127 " j 0b\n"
128 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
129 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
130 " slgr %4,%1\n"
131 " clgr %0,%4\n" /* copy crosses next page boundary? */
132 " jnh 5f\n"
133 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
134 "7: slgr %0,%4\n"
135 " j 5f\n"
136 "4: slgr %0,%0\n"
137 "5:\n"
138 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
139 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
140 : "d" (reg0) : "cc", "memory");
141 return size;
142}
143
144static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
145 unsigned long size)
146{
147 unsigned long tmp1, tmp2;
148
149 load_kernel_asce();
150 tmp1 = -256UL;
151 asm volatile(
152 " sacf 0\n"
153 "0: mvcs 0(%0,%1),0(%2),%3\n"
154 "7: jz 5f\n"
155 "1: algr %0,%3\n"
156 " la %1,256(%1)\n"
157 " la %2,256(%2)\n"
158 "2: mvcs 0(%0,%1),0(%2),%3\n"
159 "8: jnz 1b\n"
160 " j 5f\n"
161 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
162 " lghi %3,-4096\n"
163 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
164 " slgr %4,%1\n"
165 " clgr %0,%4\n" /* copy crosses next page boundary? */
166 " jnh 6f\n"
167 "4: mvcs 0(%4,%1),0(%2),%3\n"
168 "9: slgr %0,%4\n"
169 " j 6f\n"
170 "5: slgr %0,%0\n"
171 "6: sacf 768\n"
172 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
173 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
174 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
175 : : "cc", "memory");
176 return size;
177}
178
179unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
180{
181 check_object_size(from, n, true);
182 if (static_branch_likely(&have_mvcos))
183 return copy_to_user_mvcos(to, from, n);
184 return copy_to_user_mvcs(to, from, n);
185}
186EXPORT_SYMBOL(__copy_to_user);
187
188static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
189 unsigned long size)
190{
191 register unsigned long reg0 asm("0") = 0x810081UL;
192 unsigned long tmp1, tmp2;
193
194 tmp1 = -4096UL;
195 /* FIXME: copy with reduced length. */
196 asm volatile(
197 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
198 " jz 2f\n"
199 "1: algr %0,%3\n"
200 " slgr %1,%3\n"
201 " slgr %2,%3\n"
202 " j 0b\n"
203 "2:slgr %0,%0\n"
204 "3: \n"
205 EX_TABLE(0b,3b)
206 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
207 : "d" (reg0) : "cc", "memory");
208 return size;
209}
210
211static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
212 unsigned long size)
213{
214 unsigned long tmp1;
215
216 load_kernel_asce();
217 asm volatile(
218 " sacf 256\n"
219 " aghi %0,-1\n"
220 " jo 5f\n"
221 " bras %3,3f\n"
222 "0: aghi %0,257\n"
223 "1: mvc 0(1,%1),0(%2)\n"
224 " la %1,1(%1)\n"
225 " la %2,1(%2)\n"
226 " aghi %0,-1\n"
227 " jnz 1b\n"
228 " j 5f\n"
229 "2: mvc 0(256,%1),0(%2)\n"
230 " la %1,256(%1)\n"
231 " la %2,256(%2)\n"
232 "3: aghi %0,-256\n"
233 " jnm 2b\n"
234 "4: ex %0,1b-0b(%3)\n"
235 "5: slgr %0,%0\n"
236 "6: sacf 768\n"
237 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
238 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
239 : : "cc", "memory");
240 return size;
241}
242
243unsigned long __copy_in_user(void __user *to, const void __user *from, unsigned long n)
244{
245 if (static_branch_likely(&have_mvcos))
246 return copy_in_user_mvcos(to, from, n);
247 return copy_in_user_mvc(to, from, n);
248}
249EXPORT_SYMBOL(__copy_in_user);
250
251static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
252{
253 register unsigned long reg0 asm("0") = 0x810000UL;
254 unsigned long tmp1, tmp2;
255
256 tmp1 = -4096UL;
257 asm volatile(
258 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
259 " jz 4f\n"
260 "1: algr %0,%2\n"
261 " slgr %1,%2\n"
262 " j 0b\n"
263 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
264 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
265 " slgr %3,%1\n"
266 " clgr %0,%3\n" /* copy crosses next page boundary? */
267 " jnh 5f\n"
268 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
269 " slgr %0,%3\n"
270 " j 5f\n"
271 "4: slgr %0,%0\n"
272 "5:\n"
273 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
274 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
275 : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
276 return size;
277}
278
279static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
280{
281 unsigned long tmp1, tmp2;
282
283 load_kernel_asce();
284 asm volatile(
285 " sacf 256\n"
286 " aghi %0,-1\n"
287 " jo 5f\n"
288 " bras %3,3f\n"
289 " xc 0(1,%1),0(%1)\n"
290 "0: aghi %0,257\n"
291 " la %2,255(%1)\n" /* %2 = ptr + 255 */
292 " srl %2,12\n"
293 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
294 " slgr %2,%1\n"
295 " clgr %0,%2\n" /* clear crosses next page boundary? */
296 " jnh 5f\n"
297 " aghi %2,-1\n"
298 "1: ex %2,0(%3)\n"
299 " aghi %2,1\n"
300 " slgr %0,%2\n"
301 " j 5f\n"
302 "2: xc 0(256,%1),0(%1)\n"
303 " la %1,256(%1)\n"
304 "3: aghi %0,-256\n"
305 " jnm 2b\n"
306 "4: ex %0,0(%3)\n"
307 "5: slgr %0,%0\n"
308 "6: sacf 768\n"
309 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
310 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
311 : : "cc", "memory");
312 return size;
313}
314
315unsigned long __clear_user(void __user *to, unsigned long size)
316{
317 if (static_branch_likely(&have_mvcos))
318 return clear_user_mvcos(to, size);
319 return clear_user_xc(to, size);
320}
321EXPORT_SYMBOL(__clear_user);
322
323static inline unsigned long strnlen_user_srst(const char __user *src,
324 unsigned long size)
325{
326 register unsigned long reg0 asm("0") = 0;
327 unsigned long tmp1, tmp2;
328
329 asm volatile(
330 " la %2,0(%1)\n"
331 " la %3,0(%0,%1)\n"
332 " slgr %0,%0\n"
333 " sacf 256\n"
334 "0: srst %3,%2\n"
335 " jo 0b\n"
336 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
337 " slgr %0,%1\n"
338 "1: sacf 768\n"
339 EX_TABLE(0b,1b)
340 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
341 : "d" (reg0) : "cc", "memory");
342 return size;
343}
344
345unsigned long __strnlen_user(const char __user *src, unsigned long size)
346{
347 if (unlikely(!size))
348 return 0;
349 load_kernel_asce();
350 return strnlen_user_srst(src, size);
351}
352EXPORT_SYMBOL(__strnlen_user);
353
354long __strncpy_from_user(char *dst, const char __user *src, long size)
355{
356 size_t done, len, offset, len_str;
357
358 if (unlikely(size <= 0))
359 return 0;
360 done = 0;
361 do {
362 offset = (size_t)src & ~PAGE_MASK;
363 len = min(size - done, PAGE_SIZE - offset);
364 if (copy_from_user(dst, src, len))
365 return -EFAULT;
366 len_str = strnlen(dst, len);
367 done += len_str;
368 src += len_str;
369 dst += len_str;
370 } while ((len_str == len) && (done < size));
371 return done;
372}
373EXPORT_SYMBOL(__strncpy_from_user);
374
375static int __init uaccess_init(void)
376{
377 if (test_facility(27))
378 static_branch_enable(&have_mvcos);
379 return 0;
380}
381early_initcall(uaccess_init);