2 * Standard user space access functions based on mvcp/mvcs and doing
3 * interesting things in the secondary space mode.
5 * Copyright IBM Corp. 2006,2014
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
10 #include <linux/jump_label.h>
11 #include <linux/uaccess.h>
12 #include <linux/export.h>
13 #include <linux/errno.h>
15 #include <asm/mmu_context.h>
16 #include <asm/facility.h>
18 static DEFINE_STATIC_KEY_FALSE(have_mvcos);
20 static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
23 register unsigned long reg0 asm("0") = 0x81UL;
24 unsigned long tmp1, tmp2;
28 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
34 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
35 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
37 " clgr %0,%4\n" /* copy crosses next page boundary? */
39 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
43 " algr %4,%0\n" /* copy remaining size, subtract 1 */
44 " bras %3,6f\n" /* memset loop */
46 "5: xc 0(256,%2),0(%2)\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");
60 static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
63 unsigned long tmp1, tmp2;
69 "0: mvcp 0(%0,%2),0(%1),%3\n"
74 "2: mvcp 0(%0,%2),0(%1),%3\n"
77 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
79 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
81 " clgr %0,%4\n" /* copy crosses next page boundary? */
83 "4: mvcp 0(%4,%2),0(%1),%3\n"
87 " algr %4,%0\n" /* copy remaining size, subtract 1 */
88 " bras %3,7f\n" /* memset loop */
90 "6: xc 0(256,%2),0(%2)\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)
105 unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
107 if (static_branch_likely(&have_mvcos))
108 return copy_from_user_mvcos(to, from, n);
109 return copy_from_user_mvcp(to, from, n);
111 EXPORT_SYMBOL(__copy_from_user);
113 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
116 register unsigned long reg0 asm("0") = 0x810000UL;
117 unsigned long tmp1, tmp2;
121 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
127 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
128 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
130 " clgr %0,%4\n" /* copy crosses next page boundary? */
132 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
137 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
138 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
139 : "d" (reg0) : "cc", "memory");
143 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
146 unsigned long tmp1, tmp2;
152 "0: mvcs 0(%0,%1),0(%2),%3\n"
157 "2: mvcs 0(%0,%1),0(%2),%3\n"
160 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
162 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
164 " clgr %0,%4\n" /* copy crosses next page boundary? */
166 "4: mvcs 0(%4,%1),0(%2),%3\n"
171 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
172 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
173 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
178 unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
180 if (static_branch_likely(&have_mvcos))
181 return copy_to_user_mvcos(to, from, n);
182 return copy_to_user_mvcs(to, from, n);
184 EXPORT_SYMBOL(__copy_to_user);
186 static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
189 register unsigned long reg0 asm("0") = 0x810081UL;
190 unsigned long tmp1, tmp2;
193 /* FIXME: copy with reduced length. */
195 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
204 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
205 : "d" (reg0) : "cc", "memory");
209 static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
221 "1: mvc 0(1,%1),0(%2)\n"
227 "2: mvc 0(256,%1),0(%2)\n"
232 "4: ex %0,1b-0b(%3)\n"
235 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
236 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
241 unsigned long __copy_in_user(void __user *to, const void __user *from, unsigned long n)
243 if (static_branch_likely(&have_mvcos))
244 return copy_in_user_mvcos(to, from, n);
245 return copy_in_user_mvc(to, from, n);
247 EXPORT_SYMBOL(__copy_in_user);
249 static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
251 register unsigned long reg0 asm("0") = 0x810000UL;
252 unsigned long tmp1, tmp2;
256 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
261 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
262 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
264 " clgr %0,%3\n" /* copy crosses next page boundary? */
266 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
271 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
272 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
273 : "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
277 static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
279 unsigned long tmp1, tmp2;
287 " xc 0(1,%1),0(%1)\n"
289 " la %2,255(%1)\n" /* %2 = ptr + 255 */
291 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
293 " clgr %0,%2\n" /* clear crosses next page boundary? */
300 "2: xc 0(256,%1),0(%1)\n"
307 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
308 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
313 unsigned long __clear_user(void __user *to, unsigned long size)
315 if (static_branch_likely(&have_mvcos))
316 return clear_user_mvcos(to, size);
317 return clear_user_xc(to, size);
319 EXPORT_SYMBOL(__clear_user);
321 static inline unsigned long strnlen_user_srst(const char __user *src,
324 register unsigned long reg0 asm("0") = 0;
325 unsigned long tmp1, tmp2;
334 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
338 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
339 : "d" (reg0) : "cc", "memory");
343 unsigned long __strnlen_user(const char __user *src, unsigned long size)
348 return strnlen_user_srst(src, size);
350 EXPORT_SYMBOL(__strnlen_user);
352 long __strncpy_from_user(char *dst, const char __user *src, long size)
354 size_t done, len, offset, len_str;
356 if (unlikely(size <= 0))
360 offset = (size_t)src & ~PAGE_MASK;
361 len = min(size - done, PAGE_SIZE - offset);
362 if (copy_from_user(dst, src, len))
364 len_str = strnlen(dst, len);
368 } while ((len_str == len) && (done < size));
371 EXPORT_SYMBOL(__strncpy_from_user);
373 static int __init uaccess_init(void)
375 if (test_facility(27))
376 static_branch_enable(&have_mvcos);
379 early_initcall(uaccess_init);