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
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 #ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
19 static DEFINE_STATIC_KEY_FALSE(have_mvcos);
21 static int __init uaccess_init(void)
23 if (test_facility(27))
24 static_branch_enable(&have_mvcos);
27 early_initcall(uaccess_init);
29 static inline int copy_with_mvcos(void)
31 if (static_branch_likely(&have_mvcos))
36 static inline int copy_with_mvcos(void)
42 static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
45 register unsigned long reg0 asm("0") = 0x81UL;
46 unsigned long tmp1, tmp2;
50 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
56 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
57 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
59 " clgr %0,%4\n" /* copy crosses next page boundary? */
61 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
66 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
67 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
68 : "d" (reg0) : "cc", "memory");
72 static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
75 unsigned long tmp1, tmp2;
81 "0: mvcp 0(%0,%2),0(%1),%3\n"
86 "2: mvcp 0(%0,%2),0(%1),%3\n"
89 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
91 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
93 " clgr %0,%4\n" /* copy crosses next page boundary? */
95 "4: mvcp 0(%4,%2),0(%1),%3\n"
100 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
101 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
102 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
107 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
109 if (copy_with_mvcos())
110 return copy_from_user_mvcos(to, from, n);
111 return copy_from_user_mvcp(to, from, n);
113 EXPORT_SYMBOL(raw_copy_from_user);
115 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
118 register unsigned long reg0 asm("0") = 0x810000UL;
119 unsigned long tmp1, tmp2;
123 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
129 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
130 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
132 " clgr %0,%4\n" /* copy crosses next page boundary? */
134 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
139 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
140 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
141 : "d" (reg0) : "cc", "memory");
145 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
148 unsigned long tmp1, tmp2;
154 "0: mvcs 0(%0,%1),0(%2),%3\n"
159 "2: mvcs 0(%0,%1),0(%2),%3\n"
162 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
164 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
166 " clgr %0,%4\n" /* copy crosses next page boundary? */
168 "4: mvcs 0(%4,%1),0(%2),%3\n"
173 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
174 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
175 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
180 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
182 if (copy_with_mvcos())
183 return copy_to_user_mvcos(to, from, n);
184 return copy_to_user_mvcs(to, from, n);
186 EXPORT_SYMBOL(raw_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 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
245 if (copy_with_mvcos())
246 return copy_in_user_mvcos(to, from, n);
247 return copy_in_user_mvc(to, from, n);
249 EXPORT_SYMBOL(raw_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 (copy_with_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 & (L1_CACHE_BYTES - 1);
363 len = min(size - done, L1_CACHE_BYTES - 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);