1 // SPDX-License-Identifier: GPL-2.0
3 * Standard user space access functions based on mvcp/mvcs and doing
4 * interesting things in the secondary space mode.
6 * Copyright IBM Corp. 2006,2014
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
11 #include <linux/jump_label.h>
12 #include <linux/uaccess.h>
13 #include <linux/export.h>
14 #include <linux/errno.h>
16 #include <asm/mmu_context.h>
17 #include <asm/facility.h>
19 #ifdef CONFIG_DEBUG_ENTRY
20 void debug_user_asce(int exit)
22 unsigned long cr1, cr7;
24 __ctl_store(cr1, 1, 1);
25 __ctl_store(cr7, 7, 7);
26 if (cr1 == S390_lowcore.kernel_asce && cr7 == S390_lowcore.user_asce)
28 panic("incorrect ASCE on kernel %s\n"
29 "cr1: %016lx cr7: %016lx\n"
30 "kernel: %016llx user: %016llx\n",
31 exit ? "exit" : "entry", cr1, cr7,
32 S390_lowcore.kernel_asce, S390_lowcore.user_asce);
35 #endif /*CONFIG_DEBUG_ENTRY */
37 #ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
38 static DEFINE_STATIC_KEY_FALSE(have_mvcos);
40 static int __init uaccess_init(void)
42 if (test_facility(27))
43 static_branch_enable(&have_mvcos);
46 early_initcall(uaccess_init);
48 static inline int copy_with_mvcos(void)
50 if (static_branch_likely(&have_mvcos))
55 static inline int copy_with_mvcos(void)
61 static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
64 unsigned long tmp1, tmp2;
69 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
75 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
76 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
78 " clgr %0,%4\n" /* copy crosses next page boundary? */
80 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
85 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
86 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
88 : "cc", "memory", "0");
92 static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
95 unsigned long tmp1, tmp2;
100 "0: mvcp 0(%0,%2),0(%1),%3\n"
105 "2: mvcp 0(%0,%2),0(%1),%3\n"
108 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
110 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
112 " clgr %0,%4\n" /* copy crosses next page boundary? */
114 "4: mvcp 0(%4,%2),0(%1),%3\n"
119 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
120 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
121 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
126 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
128 if (copy_with_mvcos())
129 return copy_from_user_mvcos(to, from, n);
130 return copy_from_user_mvcp(to, from, n);
132 EXPORT_SYMBOL(raw_copy_from_user);
134 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
137 unsigned long tmp1, tmp2;
142 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
148 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
149 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
151 " clgr %0,%4\n" /* copy crosses next page boundary? */
153 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
158 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
159 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
160 : [spec] "K" (0x81UL)
161 : "cc", "memory", "0");
165 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
168 unsigned long tmp1, tmp2;
173 "0: mvcs 0(%0,%1),0(%2),%3\n"
178 "2: mvcs 0(%0,%1),0(%2),%3\n"
181 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
183 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
185 " clgr %0,%4\n" /* copy crosses next page boundary? */
187 "4: mvcs 0(%4,%1),0(%2),%3\n"
192 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
193 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
194 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
199 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
201 if (copy_with_mvcos())
202 return copy_to_user_mvcos(to, from, n);
203 return copy_to_user_mvcs(to, from, n);
205 EXPORT_SYMBOL(raw_copy_to_user);
207 static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
210 unsigned long tmp1, tmp2;
213 /* FIXME: copy with reduced length. */
216 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
225 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
226 : [spec] "d" (0x810081UL)
227 : "cc", "memory", "0");
231 static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
242 "1: mvc 0(1,%1),0(%2)\n"
248 "2: mvc 0(256,%1),0(%2)\n"
253 "4: ex %0,1b-0b(%3)\n"
256 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
257 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
262 unsigned long raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
264 if (copy_with_mvcos())
265 return copy_in_user_mvcos(to, from, n);
266 return copy_in_user_mvc(to, from, n);
268 EXPORT_SYMBOL(raw_copy_in_user);
270 static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
272 unsigned long tmp1, tmp2;
277 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
282 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
283 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
285 " clgr %0,%3\n" /* copy crosses next page boundary? */
287 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
292 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
293 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
294 : "a" (empty_zero_page), [spec] "K" (0x81UL)
295 : "cc", "memory", "0");
299 static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
301 unsigned long tmp1, tmp2;
308 " xc 0(1,%1),0(%1)\n"
310 " la %2,255(%1)\n" /* %2 = ptr + 255 */
312 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
314 " clgr %0,%2\n" /* clear crosses next page boundary? */
321 "2: xc 0(256,%1),0(%1)\n"
328 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
329 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
334 unsigned long __clear_user(void __user *to, unsigned long size)
336 if (copy_with_mvcos())
337 return clear_user_mvcos(to, size);
338 return clear_user_xc(to, size);
340 EXPORT_SYMBOL(__clear_user);