xref: /openbmc/linux/arch/s390/lib/uaccess.c (revision 7211ec63)
1 /*
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 
18 #ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
19 static DEFINE_STATIC_KEY_FALSE(have_mvcos);
20 
21 static int __init uaccess_init(void)
22 {
23 	if (test_facility(27))
24 		static_branch_enable(&have_mvcos);
25 	return 0;
26 }
27 early_initcall(uaccess_init);
28 
29 static inline int copy_with_mvcos(void)
30 {
31 	if (static_branch_likely(&have_mvcos))
32 		return 1;
33 	return 0;
34 }
35 #else
36 static inline int copy_with_mvcos(void)
37 {
38 	return 1;
39 }
40 #endif
41 
42 static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
43 						 unsigned long size)
44 {
45 	register unsigned long reg0 asm("0") = 0x81UL;
46 	unsigned long tmp1, tmp2;
47 
48 	tmp1 = -4096UL;
49 	asm volatile(
50 		"0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
51 		"6: jz    4f\n"
52 		"1: algr  %0,%3\n"
53 		"   slgr  %1,%3\n"
54 		"   slgr  %2,%3\n"
55 		"   j     0b\n"
56 		"2: la    %4,4095(%1)\n"/* %4 = ptr + 4095 */
57 		"   nr    %4,%3\n"	/* %4 = (ptr + 4095) & -4096 */
58 		"   slgr  %4,%1\n"
59 		"   clgr  %0,%4\n"	/* copy crosses next page boundary? */
60 		"   jnh   5f\n"
61 		"3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
62 		"7: slgr  %0,%4\n"
63 		"   j     5f\n"
64 		"4: slgr  %0,%0\n"
65 		"5:\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");
69 	return size;
70 }
71 
72 static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
73 						unsigned long size)
74 {
75 	unsigned long tmp1, tmp2;
76 
77 	load_kernel_asce();
78 	tmp1 = -256UL;
79 	asm volatile(
80 		"   sacf  0\n"
81 		"0: mvcp  0(%0,%2),0(%1),%3\n"
82 		"7: jz    5f\n"
83 		"1: algr  %0,%3\n"
84 		"   la    %1,256(%1)\n"
85 		"   la    %2,256(%2)\n"
86 		"2: mvcp  0(%0,%2),0(%1),%3\n"
87 		"8: jnz   1b\n"
88 		"   j     5f\n"
89 		"3: la    %4,255(%1)\n"	/* %4 = ptr + 255 */
90 		"   lghi  %3,-4096\n"
91 		"   nr    %4,%3\n"	/* %4 = (ptr + 255) & -4096 */
92 		"   slgr  %4,%1\n"
93 		"   clgr  %0,%4\n"	/* copy crosses next page boundary? */
94 		"   jnh   6f\n"
95 		"4: mvcp  0(%4,%2),0(%1),%3\n"
96 		"9: slgr  %0,%4\n"
97 		"   j     6f\n"
98 		"5: slgr  %0,%0\n"
99 		"6: sacf  768\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)
103 		: : "cc", "memory");
104 	return size;
105 }
106 
107 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
108 {
109 	if (copy_with_mvcos())
110 		return copy_from_user_mvcos(to, from, n);
111 	return copy_from_user_mvcp(to, from, n);
112 }
113 EXPORT_SYMBOL(raw_copy_from_user);
114 
115 static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
116 					       unsigned long size)
117 {
118 	register unsigned long reg0 asm("0") = 0x810000UL;
119 	unsigned long tmp1, tmp2;
120 
121 	tmp1 = -4096UL;
122 	asm volatile(
123 		"0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
124 		"6: jz    4f\n"
125 		"1: algr  %0,%3\n"
126 		"   slgr  %1,%3\n"
127 		"   slgr  %2,%3\n"
128 		"   j     0b\n"
129 		"2: la    %4,4095(%1)\n"/* %4 = ptr + 4095 */
130 		"   nr    %4,%3\n"	/* %4 = (ptr + 4095) & -4096 */
131 		"   slgr  %4,%1\n"
132 		"   clgr  %0,%4\n"	/* copy crosses next page boundary? */
133 		"   jnh   5f\n"
134 		"3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
135 		"7: slgr  %0,%4\n"
136 		"   j     5f\n"
137 		"4: slgr  %0,%0\n"
138 		"5:\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");
142 	return size;
143 }
144 
145 static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
146 					      unsigned long size)
147 {
148 	unsigned long tmp1, tmp2;
149 
150 	load_kernel_asce();
151 	tmp1 = -256UL;
152 	asm volatile(
153 		"   sacf  0\n"
154 		"0: mvcs  0(%0,%1),0(%2),%3\n"
155 		"7: jz    5f\n"
156 		"1: algr  %0,%3\n"
157 		"   la    %1,256(%1)\n"
158 		"   la    %2,256(%2)\n"
159 		"2: mvcs  0(%0,%1),0(%2),%3\n"
160 		"8: jnz   1b\n"
161 		"   j     5f\n"
162 		"3: la    %4,255(%1)\n" /* %4 = ptr + 255 */
163 		"   lghi  %3,-4096\n"
164 		"   nr    %4,%3\n"	/* %4 = (ptr + 255) & -4096 */
165 		"   slgr  %4,%1\n"
166 		"   clgr  %0,%4\n"	/* copy crosses next page boundary? */
167 		"   jnh   6f\n"
168 		"4: mvcs  0(%4,%1),0(%2),%3\n"
169 		"9: slgr  %0,%4\n"
170 		"   j     6f\n"
171 		"5: slgr  %0,%0\n"
172 		"6: sacf  768\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)
176 		: : "cc", "memory");
177 	return size;
178 }
179 
180 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
181 {
182 	if (copy_with_mvcos())
183 		return copy_to_user_mvcos(to, from, n);
184 	return copy_to_user_mvcs(to, from, n);
185 }
186 EXPORT_SYMBOL(raw_copy_to_user);
187 
188 static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
189 					       unsigned long size)
190 {
191 	register unsigned long reg0 asm("0") = 0x810081UL;
192 	unsigned long tmp1, tmp2;
193 
194 	tmp1 = -4096UL;
195 	/* FIXME: copy with reduced length. */
196 	asm volatile(
197 		"0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
198 		"   jz	  2f\n"
199 		"1: algr  %0,%3\n"
200 		"   slgr  %1,%3\n"
201 		"   slgr  %2,%3\n"
202 		"   j	  0b\n"
203 		"2:slgr  %0,%0\n"
204 		"3: \n"
205 		EX_TABLE(0b,3b)
206 		: "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
207 		: "d" (reg0) : "cc", "memory");
208 	return size;
209 }
210 
211 static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
212 					     unsigned long size)
213 {
214 	unsigned long tmp1;
215 
216 	load_kernel_asce();
217 	asm volatile(
218 		"   sacf  256\n"
219 		"   aghi  %0,-1\n"
220 		"   jo	  5f\n"
221 		"   bras  %3,3f\n"
222 		"0: aghi  %0,257\n"
223 		"1: mvc	  0(1,%1),0(%2)\n"
224 		"   la	  %1,1(%1)\n"
225 		"   la	  %2,1(%2)\n"
226 		"   aghi  %0,-1\n"
227 		"   jnz	  1b\n"
228 		"   j	  5f\n"
229 		"2: mvc	  0(256,%1),0(%2)\n"
230 		"   la	  %1,256(%1)\n"
231 		"   la	  %2,256(%2)\n"
232 		"3: aghi  %0,-256\n"
233 		"   jnm	  2b\n"
234 		"4: ex	  %0,1b-0b(%3)\n"
235 		"5: slgr  %0,%0\n"
236 		"6: sacf  768\n"
237 		EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
238 		: "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
239 		: : "cc", "memory");
240 	return size;
241 }
242 
243 unsigned long raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
244 {
245 	if (copy_with_mvcos())
246 		return copy_in_user_mvcos(to, from, n);
247 	return copy_in_user_mvc(to, from, n);
248 }
249 EXPORT_SYMBOL(raw_copy_in_user);
250 
251 static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
252 {
253 	register unsigned long reg0 asm("0") = 0x810000UL;
254 	unsigned long tmp1, tmp2;
255 
256 	tmp1 = -4096UL;
257 	asm volatile(
258 		"0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
259 		"   jz	  4f\n"
260 		"1: algr  %0,%2\n"
261 		"   slgr  %1,%2\n"
262 		"   j	  0b\n"
263 		"2: la	  %3,4095(%1)\n"/* %4 = to + 4095 */
264 		"   nr	  %3,%2\n"	/* %4 = (to + 4095) & -4096 */
265 		"   slgr  %3,%1\n"
266 		"   clgr  %0,%3\n"	/* copy crosses next page boundary? */
267 		"   jnh	  5f\n"
268 		"3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
269 		"   slgr  %0,%3\n"
270 		"   j	  5f\n"
271 		"4: slgr  %0,%0\n"
272 		"5:\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");
276 	return size;
277 }
278 
279 static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
280 {
281 	unsigned long tmp1, tmp2;
282 
283 	load_kernel_asce();
284 	asm volatile(
285 		"   sacf  256\n"
286 		"   aghi  %0,-1\n"
287 		"   jo    5f\n"
288 		"   bras  %3,3f\n"
289 		"   xc    0(1,%1),0(%1)\n"
290 		"0: aghi  %0,257\n"
291 		"   la    %2,255(%1)\n" /* %2 = ptr + 255 */
292 		"   srl   %2,12\n"
293 		"   sll   %2,12\n"	/* %2 = (ptr + 255) & -4096 */
294 		"   slgr  %2,%1\n"
295 		"   clgr  %0,%2\n"	/* clear crosses next page boundary? */
296 		"   jnh   5f\n"
297 		"   aghi  %2,-1\n"
298 		"1: ex    %2,0(%3)\n"
299 		"   aghi  %2,1\n"
300 		"   slgr  %0,%2\n"
301 		"   j     5f\n"
302 		"2: xc    0(256,%1),0(%1)\n"
303 		"   la    %1,256(%1)\n"
304 		"3: aghi  %0,-256\n"
305 		"   jnm   2b\n"
306 		"4: ex    %0,0(%3)\n"
307 		"5: slgr  %0,%0\n"
308 		"6: sacf  768\n"
309 		EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
310 		: "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
311 		: : "cc", "memory");
312 	return size;
313 }
314 
315 unsigned long __clear_user(void __user *to, unsigned long size)
316 {
317 	if (copy_with_mvcos())
318 			return clear_user_mvcos(to, size);
319 	return clear_user_xc(to, size);
320 }
321 EXPORT_SYMBOL(__clear_user);
322 
323 static inline unsigned long strnlen_user_srst(const char __user *src,
324 					      unsigned long size)
325 {
326 	register unsigned long reg0 asm("0") = 0;
327 	unsigned long tmp1, tmp2;
328 
329 	asm volatile(
330 		"   la    %2,0(%1)\n"
331 		"   la    %3,0(%0,%1)\n"
332 		"   slgr  %0,%0\n"
333 		"   sacf  256\n"
334 		"0: srst  %3,%2\n"
335 		"   jo    0b\n"
336 		"   la    %0,1(%3)\n"	/* strnlen_user results includes \0 */
337 		"   slgr  %0,%1\n"
338 		"1: sacf  768\n"
339 		EX_TABLE(0b,1b)
340 		: "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
341 		: "d" (reg0) : "cc", "memory");
342 	return size;
343 }
344 
345 unsigned long __strnlen_user(const char __user *src, unsigned long size)
346 {
347 	if (unlikely(!size))
348 		return 0;
349 	load_kernel_asce();
350 	return strnlen_user_srst(src, size);
351 }
352 EXPORT_SYMBOL(__strnlen_user);
353 
354 long __strncpy_from_user(char *dst, const char __user *src, long size)
355 {
356 	size_t done, len, offset, len_str;
357 
358 	if (unlikely(size <= 0))
359 		return 0;
360 	done = 0;
361 	do {
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))
365 			return -EFAULT;
366 		len_str = strnlen(dst, len);
367 		done += len_str;
368 		src += len_str;
369 		dst += len_str;
370 	} while ((len_str == len) && (done < size));
371 	return done;
372 }
373 EXPORT_SYMBOL(__strncpy_from_user);
374