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 #ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
20 static DEFINE_STATIC_KEY_FALSE(have_mvcos);
22 static int __init uaccess_init(void)
24 if (test_facility(27))
25 static_branch_enable(&have_mvcos);
28 early_initcall(uaccess_init);
30 static inline int copy_with_mvcos(void)
32 if (static_branch_likely(&have_mvcos))
37 static inline int copy_with_mvcos(void)
43 static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
46 register unsigned long reg0 asm("0") = 0x81UL;
47 unsigned long tmp1, tmp2;
51 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
57 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
58 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
60 " clgr %0,%4\n" /* copy crosses next page boundary? */
62 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
67 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
68 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
69 : "d" (reg0) : "cc", "memory");
73 static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
76 unsigned long tmp1, tmp2;
82 "0: mvcp 0(%0,%2),0(%1),%3\n"
87 "2: mvcp 0(%0,%2),0(%1),%3\n"
90 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
92 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
94 " clgr %0,%4\n" /* copy crosses next page boundary? */
96 "4: mvcp 0(%4,%2),0(%1),%3\n"
101 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
102 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
103 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
108 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
110 if (copy_with_mvcos())
111 return copy_from_user_mvcos(to, from, n);
112 return copy_from_user_mvcp(to, from, n);
114 EXPORT_SYMBOL(raw_copy_from_user);
116 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
119 register unsigned long reg0 asm("0") = 0x810000UL;
120 unsigned long tmp1, tmp2;
124 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
130 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
131 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
133 " clgr %0,%4\n" /* copy crosses next page boundary? */
135 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
140 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
141 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
142 : "d" (reg0) : "cc", "memory");
146 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
149 unsigned long tmp1, tmp2;
155 "0: mvcs 0(%0,%1),0(%2),%3\n"
160 "2: mvcs 0(%0,%1),0(%2),%3\n"
163 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
165 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
167 " clgr %0,%4\n" /* copy crosses next page boundary? */
169 "4: mvcs 0(%4,%1),0(%2),%3\n"
174 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
175 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
176 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
181 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
183 if (copy_with_mvcos())
184 return copy_to_user_mvcos(to, from, n);
185 return copy_to_user_mvcs(to, from, n);
187 EXPORT_SYMBOL(raw_copy_to_user);
189 static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
192 register unsigned long reg0 asm("0") = 0x810081UL;
193 unsigned long tmp1, tmp2;
196 /* FIXME: copy with reduced length. */
198 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
207 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
208 : "d" (reg0) : "cc", "memory");
212 static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
224 "1: mvc 0(1,%1),0(%2)\n"
230 "2: mvc 0(256,%1),0(%2)\n"
235 "4: ex %0,1b-0b(%3)\n"
238 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
239 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
244 unsigned long raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
246 if (copy_with_mvcos())
247 return copy_in_user_mvcos(to, from, n);
248 return copy_in_user_mvc(to, from, n);
250 EXPORT_SYMBOL(raw_copy_in_user);
252 static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
254 register unsigned long reg0 asm("0") = 0x810000UL;
255 unsigned long tmp1, tmp2;
259 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
264 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
265 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
267 " clgr %0,%3\n" /* copy crosses next page boundary? */
269 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
274 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
275 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
276 : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
280 static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
282 unsigned long tmp1, tmp2;
290 " xc 0(1,%1),0(%1)\n"
292 " la %2,255(%1)\n" /* %2 = ptr + 255 */
294 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
296 " clgr %0,%2\n" /* clear crosses next page boundary? */
303 "2: xc 0(256,%1),0(%1)\n"
310 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
311 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
316 unsigned long __clear_user(void __user *to, unsigned long size)
318 if (copy_with_mvcos())
319 return clear_user_mvcos(to, size);
320 return clear_user_xc(to, size);
322 EXPORT_SYMBOL(__clear_user);
324 static inline unsigned long strnlen_user_srst(const char __user *src,
327 register unsigned long reg0 asm("0") = 0;
328 unsigned long tmp1, tmp2;
337 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
341 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
342 : "d" (reg0) : "cc", "memory");
346 unsigned long __strnlen_user(const char __user *src, unsigned long size)
351 return strnlen_user_srst(src, size);
353 EXPORT_SYMBOL(__strnlen_user);
355 long __strncpy_from_user(char *dst, const char __user *src, long size)
357 size_t done, len, offset, len_str;
359 if (unlikely(size <= 0))
363 offset = (size_t)src & (L1_CACHE_BYTES - 1);
364 len = min(size - done, L1_CACHE_BYTES - offset);
365 if (copy_from_user(dst, src, len))
367 len_str = strnlen(dst, len);
371 } while ((len_str == len) && (done < size));
374 EXPORT_SYMBOL(__strncpy_from_user);