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 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);
112 EXPORT_SYMBOL(__copy_from_user);
114 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
117 register unsigned long reg0 asm("0") = 0x810000UL;
118 unsigned long tmp1, tmp2;
122 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
128 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
129 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
131 " clgr %0,%4\n" /* copy crosses next page boundary? */
133 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\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");
144 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
147 unsigned long tmp1, tmp2;
153 "0: mvcs 0(%0,%1),0(%2),%3\n"
158 "2: mvcs 0(%0,%1),0(%2),%3\n"
161 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
163 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
165 " clgr %0,%4\n" /* copy crosses next page boundary? */
167 "4: mvcs 0(%4,%1),0(%2),%3\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)
179 unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
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);
186 EXPORT_SYMBOL(__copy_to_user);
188 static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
191 register unsigned long reg0 asm("0") = 0x810081UL;
192 unsigned long tmp1, tmp2;
195 /* FIXME: copy with reduced length. */
197 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
206 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
207 : "d" (reg0) : "cc", "memory");
211 static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
223 "1: mvc 0(1,%1),0(%2)\n"
229 "2: mvc 0(256,%1),0(%2)\n"
234 "4: ex %0,1b-0b(%3)\n"
237 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
238 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
243 unsigned long __copy_in_user(void __user *to, const void __user *from, unsigned long n)
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);
249 EXPORT_SYMBOL(__copy_in_user);
251 static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
253 register unsigned long reg0 asm("0") = 0x810000UL;
254 unsigned long tmp1, tmp2;
258 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
263 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
264 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
266 " clgr %0,%3\n" /* copy crosses next page boundary? */
268 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\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");
279 static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
281 unsigned long tmp1, tmp2;
289 " xc 0(1,%1),0(%1)\n"
291 " la %2,255(%1)\n" /* %2 = ptr + 255 */
293 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
295 " clgr %0,%2\n" /* clear crosses next page boundary? */
302 "2: xc 0(256,%1),0(%1)\n"
309 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
310 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
315 unsigned long __clear_user(void __user *to, unsigned long size)
317 if (static_branch_likely(&have_mvcos))
318 return clear_user_mvcos(to, size);
319 return clear_user_xc(to, size);
321 EXPORT_SYMBOL(__clear_user);
323 static inline unsigned long strnlen_user_srst(const char __user *src,
326 register unsigned long reg0 asm("0") = 0;
327 unsigned long tmp1, tmp2;
336 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
340 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
341 : "d" (reg0) : "cc", "memory");
345 unsigned long __strnlen_user(const char __user *src, unsigned long size)
350 return strnlen_user_srst(src, size);
352 EXPORT_SYMBOL(__strnlen_user);
354 long __strncpy_from_user(char *dst, const char __user *src, long size)
356 size_t done, len, offset, len_str;
358 if (unlikely(size <= 0))
362 offset = (size_t)src & ~PAGE_MASK;
363 len = min(size - done, PAGE_SIZE - offset);
364 if (copy_from_user(dst, src, len))
366 len_str = strnlen(dst, len);
370 } while ((len_str == len) && (done < size));
373 EXPORT_SYMBOL(__strncpy_from_user);
375 static int __init uaccess_init(void)
377 if (test_facility(27))
378 static_branch_enable(&have_mvcos);
381 early_initcall(uaccess_init);