blob: 0d002a746bec157b9bd38a5006564bba26bb8b04 [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 Carstens457f2182014-03-21 10:42:25 +010018static struct static_key have_mvcos = STATIC_KEY_INIT_FALSE;
19
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"
29 "9: jz 7f\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? */
Heiko Carstens457f2182014-03-21 10:42:25 +010038 " jnh 4f\n"
39 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010040 "10:slgr %0,%4\n"
41 " algr %2,%4\n"
42 "4: lghi %4,-1\n"
43 " algr %4,%0\n" /* copy remaining size, subtract 1 */
Heiko Carstens457f2182014-03-21 10:42:25 +010044 " bras %3,6f\n" /* memset loop */
45 " xc 0(1,%2),0(%2)\n"
46 "5: xc 0(256,%2),0(%2)\n"
47 " la %2,256(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010048 "6: aghi %4,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010049 " jnm 5b\n"
50 " ex %4,0(%3)\n"
51 " j 8f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010052 "7:slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010053 "8:\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");
57 return size;
58}
59
60static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
61 unsigned long size)
62{
63 unsigned long tmp1, tmp2;
64
Martin Schwidefskybeef5602014-04-14 15:11:26 +020065 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +010066 tmp1 = -256UL;
67 asm volatile(
68 " sacf 0\n"
69 "0: mvcp 0(%0,%2),0(%1),%3\n"
70 "10:jz 8f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010071 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010072 " la %1,256(%1)\n"
73 " la %2,256(%2)\n"
74 "2: mvcp 0(%0,%2),0(%1),%3\n"
75 "11:jnz 1b\n"
76 " j 8f\n"
77 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a798592015-02-12 13:08:27 +010078 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010079 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +010080 " slgr %4,%1\n"
81 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +010082 " jnh 5f\n"
83 "4: mvcp 0(%4,%2),0(%1),%3\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010084 "12:slgr %0,%4\n"
85 " algr %2,%4\n"
86 "5: lghi %4,-1\n"
87 " algr %4,%0\n" /* copy remaining size, subtract 1 */
Heiko Carstens457f2182014-03-21 10:42:25 +010088 " bras %3,7f\n" /* memset loop */
89 " xc 0(1,%2),0(%2)\n"
90 "6: xc 0(256,%2),0(%2)\n"
91 " la %2,256(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010092 "7: aghi %4,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010093 " jnm 6b\n"
94 " ex %4,0(%3)\n"
95 " j 9f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +010096 "8:slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010097 "9: sacf 768\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)
101 : : "cc", "memory");
102 return size;
103}
104
105unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
106{
107 if (static_key_false(&have_mvcos))
108 return copy_from_user_mvcos(to, from, n);
109 return copy_from_user_mvcp(to, from, n);
110}
111EXPORT_SYMBOL(__copy_from_user);
112
113static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
114 unsigned long size)
115{
116 register unsigned long reg0 asm("0") = 0x810000UL;
117 unsigned long tmp1, tmp2;
118
119 tmp1 = -4096UL;
120 asm volatile(
121 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
122 "6: jz 4f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100123 "1: algr %0,%3\n"
124 " slgr %1,%3\n"
125 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100126 " j 0b\n"
127 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
128 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100129 " slgr %4,%1\n"
130 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100131 " jnh 5f\n"
132 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100133 "7: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100134 " j 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100135 "4: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100136 "5:\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");
140 return size;
141}
142
143static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
144 unsigned long size)
145{
146 unsigned long tmp1, tmp2;
147
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200148 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100149 tmp1 = -256UL;
150 asm volatile(
151 " sacf 0\n"
152 "0: mvcs 0(%0,%1),0(%2),%3\n"
153 "7: jz 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100154 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100155 " la %1,256(%1)\n"
156 " la %2,256(%2)\n"
157 "2: mvcs 0(%0,%1),0(%2),%3\n"
158 "8: jnz 1b\n"
159 " j 5f\n"
160 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100161 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100162 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100163 " slgr %4,%1\n"
164 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100165 " jnh 6f\n"
166 "4: mvcs 0(%4,%1),0(%2),%3\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100167 "9: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100168 " j 6f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100169 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100170 "6: sacf 768\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)
174 : : "cc", "memory");
175 return size;
176}
177
178unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
179{
180 if (static_key_false(&have_mvcos))
181 return copy_to_user_mvcos(to, from, n);
182 return copy_to_user_mvcs(to, from, n);
183}
184EXPORT_SYMBOL(__copy_to_user);
185
186static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
187 unsigned long size)
188{
189 register unsigned long reg0 asm("0") = 0x810081UL;
190 unsigned long tmp1, tmp2;
191
192 tmp1 = -4096UL;
193 /* FIXME: copy with reduced length. */
194 asm volatile(
195 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
196 " jz 2f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100197 "1: algr %0,%3\n"
198 " slgr %1,%3\n"
199 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100200 " j 0b\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100201 "2:slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100202 "3: \n"
203 EX_TABLE(0b,3b)
204 : "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
205 : "d" (reg0) : "cc", "memory");
206 return size;
207}
208
209static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
210 unsigned long size)
211{
212 unsigned long tmp1;
213
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200214 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100215 asm volatile(
216 " sacf 256\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100217 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100218 " jo 5f\n"
219 " bras %3,3f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100220 "0: aghi %0,257\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100221 "1: mvc 0(1,%1),0(%2)\n"
222 " la %1,1(%1)\n"
223 " la %2,1(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100224 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100225 " jnz 1b\n"
226 " j 5f\n"
227 "2: mvc 0(256,%1),0(%2)\n"
228 " la %1,256(%1)\n"
229 " la %2,256(%2)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100230 "3: aghi %0,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100231 " jnm 2b\n"
232 "4: ex %0,1b-0b(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100233 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100234 "6: sacf 768\n"
235 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
236 : "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
237 : : "cc", "memory");
238 return size;
239}
240
241unsigned long __copy_in_user(void __user *to, const void __user *from, unsigned long n)
242{
243 if (static_key_false(&have_mvcos))
244 return copy_in_user_mvcos(to, from, n);
245 return copy_in_user_mvc(to, from, n);
246}
247EXPORT_SYMBOL(__copy_in_user);
248
249static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
250{
251 register unsigned long reg0 asm("0") = 0x810000UL;
252 unsigned long tmp1, tmp2;
253
254 tmp1 = -4096UL;
255 asm volatile(
256 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
257 " jz 4f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100258 "1: algr %0,%2\n"
259 " slgr %1,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100260 " j 0b\n"
261 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
262 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100263 " slgr %3,%1\n"
264 " clgr %0,%3\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100265 " jnh 5f\n"
266 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100267 " slgr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100268 " j 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100269 "4:slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100270 "5:\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");
274 return size;
275}
276
277static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
278{
279 unsigned long tmp1, tmp2;
280
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200281 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100282 asm volatile(
283 " sacf 256\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100284 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100285 " jo 5f\n"
286 " bras %3,3f\n"
287 " xc 0(1,%1),0(%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100288 "0: aghi %0,257\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100289 " la %2,255(%1)\n" /* %2 = ptr + 255 */
290 " srl %2,12\n"
291 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100292 " slgr %2,%1\n"
293 " clgr %0,%2\n" /* clear crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100294 " jnh 5f\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100295 " aghi %2,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100296 "1: ex %2,0(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100297 " aghi %2,1\n"
298 " slgr %0,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100299 " j 5f\n"
300 "2: xc 0(256,%1),0(%1)\n"
301 " la %1,256(%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100302 "3: aghi %0,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100303 " jnm 2b\n"
304 "4: ex %0,0(%3)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100305 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100306 "6: sacf 768\n"
307 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
308 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
309 : : "cc", "memory");
310 return size;
311}
312
313unsigned long __clear_user(void __user *to, unsigned long size)
314{
315 if (static_key_false(&have_mvcos))
316 return clear_user_mvcos(to, size);
317 return clear_user_xc(to, size);
318}
319EXPORT_SYMBOL(__clear_user);
320
321static inline unsigned long strnlen_user_srst(const char __user *src,
322 unsigned long size)
323{
324 register unsigned long reg0 asm("0") = 0;
325 unsigned long tmp1, tmp2;
326
Heiko Carstens457f2182014-03-21 10:42:25 +0100327 asm volatile(
328 " la %2,0(%1)\n"
329 " la %3,0(%0,%1)\n"
Heiko Carstens5a798592015-02-12 13:08:27 +0100330 " slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100331 " sacf 256\n"
332 "0: srst %3,%2\n"
333 " jo 0b\n"
334 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
Heiko Carstens5a798592015-02-12 13:08:27 +0100335 " slgr %0,%1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100336 "1: sacf 768\n"
337 EX_TABLE(0b,1b)
338 : "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
339 : "d" (reg0) : "cc", "memory");
340 return size;
341}
342
343unsigned long __strnlen_user(const char __user *src, unsigned long size)
344{
Heiko Carstensfa255f52014-04-09 09:42:58 +0200345 if (unlikely(!size))
346 return 0;
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200347 load_kernel_asce();
Heiko Carstens457f2182014-03-21 10:42:25 +0100348 return strnlen_user_srst(src, size);
349}
350EXPORT_SYMBOL(__strnlen_user);
351
352long __strncpy_from_user(char *dst, const char __user *src, long size)
353{
354 size_t done, len, offset, len_str;
355
356 if (unlikely(size <= 0))
357 return 0;
358 done = 0;
359 do {
360 offset = (size_t)src & ~PAGE_MASK;
361 len = min(size - done, PAGE_SIZE - offset);
362 if (copy_from_user(dst, src, len))
363 return -EFAULT;
364 len_str = strnlen(dst, len);
365 done += len_str;
366 src += len_str;
367 dst += len_str;
368 } while ((len_str == len) && (done < size));
369 return done;
370}
371EXPORT_SYMBOL(__strncpy_from_user);
372
Heiko Carstens457f2182014-03-21 10:42:25 +0100373static int __init uaccess_init(void)
374{
Heiko Carstenscabc4ab2015-08-13 13:44:34 +0200375 if (test_facility(27))
Heiko Carstens457f2182014-03-21 10:42:25 +0100376 static_key_slow_inc(&have_mvcos);
377 return 0;
378}
379early_initcall(uaccess_init);