blob: 1e5bb2b86c423fefee7987ec45483077748e8ca8 [file] [log] [blame]
Heiko Carstens457f2182014-03-21 10:42:25 +01001/*
2 * Standard user space access functions based on mvcp/mvcs and doing
3 * interesting things in the secondary space mode.
4 *
5 * Copyright IBM Corp. 2006,2014
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
8 */
9
10#include <linux/jump_label.h>
11#include <linux/uaccess.h>
12#include <linux/export.h>
13#include <linux/errno.h>
14#include <linux/mm.h>
15#include <asm/mmu_context.h>
16#include <asm/facility.h>
17
Heiko Carstensed79e942015-07-29 08:31:24 +020018static DEFINE_STATIC_KEY_FALSE(have_mvcos);
Heiko Carstens457f2182014-03-21 10:42:25 +010019
20static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
21 unsigned long size)
22{
23 register unsigned long reg0 asm("0") = 0x81UL;
24 unsigned long tmp1, tmp2;
25
26 tmp1 = -4096UL;
27 asm volatile(
28 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
Al Viro37096002017-03-28 15:06:24 -040029 "6: jz 4f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010030 "1: algr %0,%3\n"
31 " slgr %1,%3\n"
32 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010033 " j 0b\n"
34 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
35 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +010036 " slgr %4,%1\n"
37 " clgr %0,%4\n" /* copy crosses next page boundary? */
Al Viro37096002017-03-28 15:06:24 -040038 " jnh 5f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010039 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
Al Viro37096002017-03-28 15:06:24 -040040 "7: slgr %0,%4\n"
41 " j 5f\n"
42 "4: slgr %0,%0\n"
43 "5:\n"
44 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
Heiko Carstens457f2182014-03-21 10:42:25 +010045 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
46 : "d" (reg0) : "cc", "memory");
47 return size;
48}
49
50static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
51 unsigned long size)
52{
53 unsigned long tmp1, tmp2;
54
Martin Schwidefskybeef5602014-04-14 15:11:26 +020055 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +010056 tmp1 = -256UL;
57 asm volatile(
58 " sacf 0\n"
59 "0: mvcp 0(%0,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -040060 "7: jz 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010061 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010062 " la %1,256(%1)\n"
63 " la %2,256(%2)\n"
64 "2: mvcp 0(%0,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -040065 "8: jnz 1b\n"
66 " j 5f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010067 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a798592015-02-12 13:08:27 +010068 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010069 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +010070 " slgr %4,%1\n"
71 " clgr %0,%4\n" /* copy crosses next page boundary? */
Al Viro37096002017-03-28 15:06:24 -040072 " jnh 6f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010073 "4: mvcp 0(%4,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -040074 "9: slgr %0,%4\n"
75 " j 6f\n"
76 "5: slgr %0,%0\n"
77 "6: sacf 768\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)
Heiko Carstens457f2182014-03-21 10:42:25 +010080 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
81 : : "cc", "memory");
82 return size;
83}
84
Al Viro37096002017-03-28 15:06:24 -040085unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
Heiko Carstens457f2182014-03-21 10:42:25 +010086{
Heiko Carstensed79e942015-07-29 08:31:24 +020087 if (static_branch_likely(&have_mvcos))
Heiko Carstens457f2182014-03-21 10:42:25 +010088 return copy_from_user_mvcos(to, from, n);
89 return copy_from_user_mvcp(to, from, n);
90}
Al Viro37096002017-03-28 15:06:24 -040091EXPORT_SYMBOL(raw_copy_from_user);
Heiko Carstens457f2182014-03-21 10:42:25 +010092
93static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
94 unsigned long size)
95{
96 register unsigned long reg0 asm("0") = 0x810000UL;
97 unsigned long tmp1, tmp2;
98
99 tmp1 = -4096UL;
100 asm volatile(
101 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
102 "6: jz 4f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100103 "1: algr %0,%3\n"
104 " slgr %1,%3\n"
105 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100106 " j 0b\n"
107 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
108 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100109 " slgr %4,%1\n"
110 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100111 " jnh 5f\n"
112 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100113 "7: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100114 " j 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100115 "4: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100116 "5:\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");
120 return size;
121}
122
123static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
124 unsigned long size)
125{
126 unsigned long tmp1, tmp2;
127
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200128 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100129 tmp1 = -256UL;
130 asm volatile(
131 " sacf 0\n"
132 "0: mvcs 0(%0,%1),0(%2),%3\n"
133 "7: jz 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100134 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100135 " la %1,256(%1)\n"
136 " la %2,256(%2)\n"
137 "2: mvcs 0(%0,%1),0(%2),%3\n"
138 "8: jnz 1b\n"
139 " j 5f\n"
140 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100141 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100142 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100143 " slgr %4,%1\n"
144 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100145 " jnh 6f\n"
146 "4: mvcs 0(%4,%1),0(%2),%3\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100147 "9: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100148 " j 6f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100149 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100150 "6: sacf 768\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)
154 : : "cc", "memory");
155 return size;
156}
157
Al Viro37096002017-03-28 15:06:24 -0400158unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
Heiko Carstens457f2182014-03-21 10:42:25 +0100159{
Heiko Carstensed79e942015-07-29 08:31:24 +0200160 if (static_branch_likely(&have_mvcos))
Heiko Carstens457f2182014-03-21 10:42:25 +0100161 return copy_to_user_mvcos(to, from, n);
162 return copy_to_user_mvcs(to, from, n);
163}
Al Viro37096002017-03-28 15:06:24 -0400164EXPORT_SYMBOL(raw_copy_to_user);
Heiko Carstens457f2182014-03-21 10:42:25 +0100165
166static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
167 unsigned long size)
168{
169 register unsigned long reg0 asm("0") = 0x810081UL;
170 unsigned long tmp1, tmp2;
171
172 tmp1 = -4096UL;
173 /* FIXME: copy with reduced length. */
174 asm volatile(
175 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
176 " jz 2f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100177 "1: algr %0,%3\n"
178 " slgr %1,%3\n"
179 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100180 " j 0b\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100181 "2:slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100182 "3: \n"
183 EX_TABLE(0b,3b)
184 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
185 : "d" (reg0) : "cc", "memory");
186 return size;
187}
188
189static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
190 unsigned long size)
191{
192 unsigned long tmp1;
193
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200194 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100195 asm volatile(
196 " sacf 256\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100197 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100198 " jo 5f\n"
199 " bras %3,3f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100200 "0: aghi %0,257\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100201 "1: mvc 0(1,%1),0(%2)\n"
202 " la %1,1(%1)\n"
203 " la %2,1(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100204 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100205 " jnz 1b\n"
206 " j 5f\n"
207 "2: mvc 0(256,%1),0(%2)\n"
208 " la %1,256(%1)\n"
209 " la %2,256(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100210 "3: aghi %0,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100211 " jnm 2b\n"
212 "4: ex %0,1b-0b(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100213 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100214 "6: sacf 768\n"
215 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
216 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
217 : : "cc", "memory");
218 return size;
219}
220
Al Viro37096002017-03-28 15:06:24 -0400221unsigned long raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
Heiko Carstens457f2182014-03-21 10:42:25 +0100222{
Heiko Carstensed79e942015-07-29 08:31:24 +0200223 if (static_branch_likely(&have_mvcos))
Heiko Carstens457f2182014-03-21 10:42:25 +0100224 return copy_in_user_mvcos(to, from, n);
225 return copy_in_user_mvc(to, from, n);
226}
Al Viro37096002017-03-28 15:06:24 -0400227EXPORT_SYMBOL(raw_copy_in_user);
Heiko Carstens457f2182014-03-21 10:42:25 +0100228
229static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
230{
231 register unsigned long reg0 asm("0") = 0x810000UL;
232 unsigned long tmp1, tmp2;
233
234 tmp1 = -4096UL;
235 asm volatile(
236 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
237 " jz 4f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100238 "1: algr %0,%2\n"
239 " slgr %1,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100240 " j 0b\n"
241 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
242 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100243 " slgr %3,%1\n"
244 " clgr %0,%3\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100245 " jnh 5f\n"
246 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100247 " slgr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100248 " j 5f\n"
Heiko Carstensb8ac5e22016-06-10 09:35:51 +0200249 "4: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100250 "5:\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");
254 return size;
255}
256
257static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
258{
259 unsigned long tmp1, tmp2;
260
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200261 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100262 asm volatile(
263 " sacf 256\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100264 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100265 " jo 5f\n"
266 " bras %3,3f\n"
267 " xc 0(1,%1),0(%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100268 "0: aghi %0,257\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100269 " la %2,255(%1)\n" /* %2 = ptr + 255 */
270 " srl %2,12\n"
271 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100272 " slgr %2,%1\n"
273 " clgr %0,%2\n" /* clear crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100274 " jnh 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100275 " aghi %2,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100276 "1: ex %2,0(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100277 " aghi %2,1\n"
278 " slgr %0,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100279 " j 5f\n"
280 "2: xc 0(256,%1),0(%1)\n"
281 " la %1,256(%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100282 "3: aghi %0,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100283 " jnm 2b\n"
284 "4: ex %0,0(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100285 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100286 "6: sacf 768\n"
287 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
288 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
289 : : "cc", "memory");
290 return size;
291}
292
293unsigned long __clear_user(void __user *to, unsigned long size)
294{
Heiko Carstensed79e942015-07-29 08:31:24 +0200295 if (static_branch_likely(&have_mvcos))
Heiko Carstens457f2182014-03-21 10:42:25 +0100296 return clear_user_mvcos(to, size);
297 return clear_user_xc(to, size);
298}
299EXPORT_SYMBOL(__clear_user);
300
301static inline unsigned long strnlen_user_srst(const char __user *src,
302 unsigned long size)
303{
304 register unsigned long reg0 asm("0") = 0;
305 unsigned long tmp1, tmp2;
306
Heiko Carstens457f2182014-03-21 10:42:25 +0100307 asm volatile(
308 " la %2,0(%1)\n"
309 " la %3,0(%0,%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100310 " slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100311 " sacf 256\n"
312 "0: srst %3,%2\n"
313 " jo 0b\n"
314 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100315 " slgr %0,%1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100316 "1: sacf 768\n"
317 EX_TABLE(0b,1b)
318 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
319 : "d" (reg0) : "cc", "memory");
320 return size;
321}
322
323unsigned long __strnlen_user(const char __user *src, unsigned long size)
324{
Heiko Carstensfa255f52014-04-09 09:42:58 +0200325 if (unlikely(!size))
326 return 0;
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200327 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100328 return strnlen_user_srst(src, size);
329}
330EXPORT_SYMBOL(__strnlen_user);
331
332long __strncpy_from_user(char *dst, const char __user *src, long size)
333{
334 size_t done, len, offset, len_str;
335
336 if (unlikely(size <= 0))
337 return 0;
338 done = 0;
339 do {
340 offset = (size_t)src & ~PAGE_MASK;
341 len = min(size - done, PAGE_SIZE - offset);
342 if (copy_from_user(dst, src, len))
343 return -EFAULT;
344 len_str = strnlen(dst, len);
345 done += len_str;
346 src += len_str;
347 dst += len_str;
348 } while ((len_str == len) && (done < size));
349 return done;
350}
351EXPORT_SYMBOL(__strncpy_from_user);
352
Heiko Carstens457f2182014-03-21 10:42:25 +0100353static int __init uaccess_init(void)
354{
Heiko Carstenscabc4ab2015-08-13 13:44:34 +0200355 if (test_facility(27))
Heiko Carstensed79e942015-07-29 08:31:24 +0200356 static_branch_enable(&have_mvcos);
Heiko Carstens457f2182014-03-21 10:42:25 +0100357 return 0;
358}
359early_initcall(uaccess_init);