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"
44 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
45 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
46 : "d" (reg0
) : "cc", "memory");
50 static inline unsigned long copy_from_user_mvcp(void *x
, const void __user
*ptr
,
53 unsigned long tmp1
, tmp2
;
59 "0: mvcp 0(%0,%2),0(%1),%3\n"
64 "2: mvcp 0(%0,%2),0(%1),%3\n"
67 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
69 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
71 " clgr %0,%4\n" /* copy crosses next page boundary? */
73 "4: mvcp 0(%4,%2),0(%1),%3\n"
78 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
79 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
80 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
85 unsigned long raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
87 if (static_branch_likely(&have_mvcos
))
88 return copy_from_user_mvcos(to
, from
, n
);
89 return copy_from_user_mvcp(to
, from
, n
);
91 EXPORT_SYMBOL(raw_copy_from_user
);
93 static inline unsigned long copy_to_user_mvcos(void __user
*ptr
, const void *x
,
96 register unsigned long reg0
asm("0") = 0x810000UL
;
97 unsigned long tmp1
, tmp2
;
101 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
107 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
108 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
110 " clgr %0,%4\n" /* copy crosses next page boundary? */
112 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
117 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
118 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
119 : "d" (reg0
) : "cc", "memory");
123 static inline unsigned long copy_to_user_mvcs(void __user
*ptr
, const void *x
,
126 unsigned long tmp1
, tmp2
;
132 "0: mvcs 0(%0,%1),0(%2),%3\n"
137 "2: mvcs 0(%0,%1),0(%2),%3\n"
140 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
142 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
144 " clgr %0,%4\n" /* copy crosses next page boundary? */
146 "4: mvcs 0(%4,%1),0(%2),%3\n"
151 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
152 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
153 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
158 unsigned long raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
160 if (static_branch_likely(&have_mvcos
))
161 return copy_to_user_mvcos(to
, from
, n
);
162 return copy_to_user_mvcs(to
, from
, n
);
164 EXPORT_SYMBOL(raw_copy_to_user
);
166 static inline unsigned long copy_in_user_mvcos(void __user
*to
, const void __user
*from
,
169 register unsigned long reg0
asm("0") = 0x810081UL
;
170 unsigned long tmp1
, tmp2
;
173 /* FIXME: copy with reduced length. */
175 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
184 : "+a" (size
), "+a" (to
), "+a" (from
), "+a" (tmp1
), "=a" (tmp2
)
185 : "d" (reg0
) : "cc", "memory");
189 static inline unsigned long copy_in_user_mvc(void __user
*to
, const void __user
*from
,
201 "1: mvc 0(1,%1),0(%2)\n"
207 "2: mvc 0(256,%1),0(%2)\n"
212 "4: ex %0,1b-0b(%3)\n"
215 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
216 : "+a" (size
), "+a" (to
), "+a" (from
), "=a" (tmp1
)
221 unsigned long raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
223 if (static_branch_likely(&have_mvcos
))
224 return copy_in_user_mvcos(to
, from
, n
);
225 return copy_in_user_mvc(to
, from
, n
);
227 EXPORT_SYMBOL(raw_copy_in_user
);
229 static inline unsigned long clear_user_mvcos(void __user
*to
, unsigned long size
)
231 register unsigned long reg0
asm("0") = 0x810000UL
;
232 unsigned long tmp1
, tmp2
;
236 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
241 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
242 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
244 " clgr %0,%3\n" /* copy crosses next page boundary? */
246 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
251 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
)
252 : "+a" (size
), "+a" (to
), "+a" (tmp1
), "=a" (tmp2
)
253 : "a" (empty_zero_page
), "d" (reg0
) : "cc", "memory");
257 static inline unsigned long clear_user_xc(void __user
*to
, unsigned long size
)
259 unsigned long tmp1
, tmp2
;
267 " xc 0(1,%1),0(%1)\n"
269 " la %2,255(%1)\n" /* %2 = ptr + 255 */
271 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
273 " clgr %0,%2\n" /* clear crosses next page boundary? */
280 "2: xc 0(256,%1),0(%1)\n"
287 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
288 : "+a" (size
), "+a" (to
), "=a" (tmp1
), "=a" (tmp2
)
293 unsigned long __clear_user(void __user
*to
, unsigned long size
)
295 if (static_branch_likely(&have_mvcos
))
296 return clear_user_mvcos(to
, size
);
297 return clear_user_xc(to
, size
);
299 EXPORT_SYMBOL(__clear_user
);
301 static inline unsigned long strnlen_user_srst(const char __user
*src
,
304 register unsigned long reg0
asm("0") = 0;
305 unsigned long tmp1
, tmp2
;
314 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
318 : "+a" (size
), "+a" (src
), "=a" (tmp1
), "=a" (tmp2
)
319 : "d" (reg0
) : "cc", "memory");
323 unsigned long __strnlen_user(const char __user
*src
, unsigned long size
)
328 return strnlen_user_srst(src
, size
);
330 EXPORT_SYMBOL(__strnlen_user
);
332 long __strncpy_from_user(char *dst
, const char __user
*src
, long size
)
334 size_t done
, len
, offset
, len_str
;
336 if (unlikely(size
<= 0))
340 offset
= (size_t)src
& (L1_CACHE_BYTES
- 1);
341 len
= min(size
- done
, L1_CACHE_BYTES
- offset
);
342 if (copy_from_user(dst
, src
, len
))
344 len_str
= strnlen(dst
, len
);
348 } while ((len_str
== len
) && (done
< size
));
351 EXPORT_SYMBOL(__strncpy_from_user
);
353 static int __init
uaccess_init(void)
355 if (test_facility(27))
356 static_branch_enable(&have_mvcos
);
359 early_initcall(uaccess_init
);