1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_POWERPC_UACCESS_H
3 #define _ARCH_POWERPC_UACCESS_H
4
5 #include <asm/ppc_asm.h>
6 #include <asm/processor.h>
7 #include <asm/page.h>
8 #include <asm/extable.h>
9 #include <asm/kup.h>
10
11 #ifdef __powerpc64__
12 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
13 #define TASK_SIZE_MAX TASK_SIZE_USER64
14 #else
15 #define TASK_SIZE_MAX TASK_SIZE
16 #endif
17
__access_ok(unsigned long addr,unsigned long size)18 static inline bool __access_ok(unsigned long addr, unsigned long size)
19 {
20 return addr < TASK_SIZE_MAX && size <= TASK_SIZE_MAX - addr;
21 }
22
23 #define access_ok(addr, size) \
24 (__chk_user_ptr(addr), \
25 __access_ok((unsigned long)(addr), (size)))
26
27 /*
28 * These are the main single-value transfer routines. They automatically
29 * use the right size if we just have the right pointer type.
30 *
31 * This gets kind of ugly. We want to return _two_ values in "get_user()"
32 * and yet we don't want to do any pointers, because that is too much
33 * of a performance impact. Thus we have a few rather ugly macros here,
34 * and hide all the ugliness from the user.
35 *
36 * The "__xxx" versions of the user access functions are versions that
37 * do not verify the address space, that must have been done previously
38 * with a separate "access_ok()" call (this is used when we do multiple
39 * accesses to the same area of user memory).
40 *
41 * As we use the same address space for kernel and user data on the
42 * PowerPC, we can just do these as direct assignments. (Of course, the
43 * exception handling means that it's no longer "just"...)
44 *
45 */
46 #define get_user(x, ptr) \
47 __get_user_check((x), (ptr), sizeof(*(ptr)))
48 #define put_user(x, ptr) \
49 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
50
51 #define __get_user(x, ptr) \
52 __get_user_nocheck((x), (ptr), sizeof(*(ptr)), true)
53 #define __put_user(x, ptr) \
54 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
55 #define __put_user_goto(x, ptr, label) \
56 __put_user_nocheck_goto((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)), label)
57
58 #define __get_user_allowed(x, ptr) \
59 __get_user_nocheck((x), (ptr), sizeof(*(ptr)), false)
60
61 #define __get_user_inatomic(x, ptr) \
62 __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
63 #define __put_user_inatomic(x, ptr) \
64 __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
65
66 #ifdef CONFIG_PPC64
67
68 #define ___get_user_instr(gu_op, dest, ptr) \
69 ({ \
70 long __gui_ret = 0; \
71 unsigned long __gui_ptr = (unsigned long)ptr; \
72 struct ppc_inst __gui_inst; \
73 unsigned int __prefix, __suffix; \
74 __gui_ret = gu_op(__prefix, (unsigned int __user *)__gui_ptr); \
75 if (__gui_ret == 0) { \
76 if ((__prefix >> 26) == OP_PREFIX) { \
77 __gui_ret = gu_op(__suffix, \
78 (unsigned int __user *)__gui_ptr + 1); \
79 __gui_inst = ppc_inst_prefix(__prefix, \
80 __suffix); \
81 } else { \
82 __gui_inst = ppc_inst(__prefix); \
83 } \
84 if (__gui_ret == 0) \
85 (dest) = __gui_inst; \
86 } \
87 __gui_ret; \
88 })
89
90 #define get_user_instr(x, ptr) \
91 ___get_user_instr(get_user, x, ptr)
92
93 #define __get_user_instr(x, ptr) \
94 ___get_user_instr(__get_user, x, ptr)
95
96 #define __get_user_instr_inatomic(x, ptr) \
97 ___get_user_instr(__get_user_inatomic, x, ptr)
98
99 #else /* !CONFIG_PPC64 */
100 #define get_user_instr(x, ptr) \
101 get_user((x).val, (u32 __user *)(ptr))
102
103 #define __get_user_instr(x, ptr) \
104 __get_user_nocheck((x).val, (u32 __user *)(ptr), sizeof(u32), true)
105
106 #define __get_user_instr_inatomic(x, ptr) \
107 __get_user_nosleep((x).val, (u32 __user *)(ptr), sizeof(u32))
108
109 #endif /* CONFIG_PPC64 */
110
111 extern long __put_user_bad(void);
112
113 #define __put_user_size_allowed(x, ptr, size, retval) \
114 do { \
115 __label__ __pu_failed; \
116 \
117 retval = 0; \
118 __put_user_size_goto(x, ptr, size, __pu_failed); \
119 break; \
120 \
121 __pu_failed: \
122 retval = -EFAULT; \
123 } while (0)
124
125 #define __put_user_size(x, ptr, size, retval) \
126 do { \
127 allow_write_to_user(ptr, size); \
128 __put_user_size_allowed(x, ptr, size, retval); \
129 prevent_write_to_user(ptr, size); \
130 } while (0)
131
132 #define __put_user_nocheck(x, ptr, size) \
133 ({ \
134 long __pu_err; \
135 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
136 __typeof__(*(ptr)) __pu_val = (x); \
137 __typeof__(size) __pu_size = (size); \
138 \
139 if (!is_kernel_addr((unsigned long)__pu_addr)) \
140 might_fault(); \
141 __chk_user_ptr(__pu_addr); \
142 __put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err); \
143 \
144 __pu_err; \
145 })
146
147 #define __put_user_check(x, ptr, size) \
148 ({ \
149 long __pu_err = -EFAULT; \
150 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
151 __typeof__(*(ptr)) __pu_val = (x); \
152 __typeof__(size) __pu_size = (size); \
153 \
154 might_fault(); \
155 if (access_ok(__pu_addr, __pu_size)) \
156 __put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err); \
157 \
158 __pu_err; \
159 })
160
161 #define __put_user_nosleep(x, ptr, size) \
162 ({ \
163 long __pu_err; \
164 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
165 __typeof__(*(ptr)) __pu_val = (x); \
166 __typeof__(size) __pu_size = (size); \
167 \
168 __chk_user_ptr(__pu_addr); \
169 __put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err); \
170 \
171 __pu_err; \
172 })
173
174
175 /*
176 * We don't tell gcc that we are accessing memory, but this is OK
177 * because we do not write to any memory gcc knows about, so there
178 * are no aliasing issues.
179 */
180 #define __put_user_asm_goto(x, addr, label, op) \
181 asm_volatile_goto( \
182 "1: " op "%U1%X1 %0,%1 # put_user\n" \
183 EX_TABLE(1b, %l2) \
184 : \
185 : "r" (x), "m"UPD_CONSTR (*addr) \
186 : \
187 : label)
188
189 #ifdef __powerpc64__
190 #define __put_user_asm2_goto(x, ptr, label) \
191 __put_user_asm_goto(x, ptr, label, "std")
192 #else /* __powerpc64__ */
193 #define __put_user_asm2_goto(x, addr, label) \
194 asm_volatile_goto( \
195 "1: stw%X1 %0, %1\n" \
196 "2: stw%X1 %L0, %L1\n" \
197 EX_TABLE(1b, %l2) \
198 EX_TABLE(2b, %l2) \
199 : \
200 : "r" (x), "m" (*addr) \
201 : \
202 : label)
203 #endif /* __powerpc64__ */
204
205 #define __put_user_size_goto(x, ptr, size, label) \
206 do { \
207 switch (size) { \
208 case 1: __put_user_asm_goto(x, ptr, label, "stb"); break; \
209 case 2: __put_user_asm_goto(x, ptr, label, "sth"); break; \
210 case 4: __put_user_asm_goto(x, ptr, label, "stw"); break; \
211 case 8: __put_user_asm2_goto(x, ptr, label); break; \
212 default: __put_user_bad(); \
213 } \
214 } while (0)
215
216 #define __put_user_nocheck_goto(x, ptr, size, label) \
217 do { \
218 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
219 __chk_user_ptr(ptr); \
220 __put_user_size_goto((x), __pu_addr, (size), label); \
221 } while (0)
222
223
224 extern long __get_user_bad(void);
225
226 /*
227 * This does an atomic 128 byte aligned load from userspace.
228 * Upto caller to do enable_kernel_vmx() before calling!
229 */
230 #define __get_user_atomic_128_aligned(kaddr, uaddr, err) \
231 __asm__ __volatile__( \
232 "1: lvx 0,0,%1 # get user\n" \
233 " stvx 0,0,%2 # put kernel\n" \
234 "2:\n" \
235 ".section .fixup,\"ax\"\n" \
236 "3: li %0,%3\n" \
237 " b 2b\n" \
238 ".previous\n" \
239 EX_TABLE(1b, 3b) \
240 : "=r" (err) \
241 : "b" (uaddr), "b" (kaddr), "i" (-EFAULT), "0" (err))
242
243 #define __get_user_asm(x, addr, err, op) \
244 __asm__ __volatile__( \
245 "1: "op"%U2%X2 %1, %2 # get_user\n" \
246 "2:\n" \
247 ".section .fixup,\"ax\"\n" \
248 "3: li %0,%3\n" \
249 " li %1,0\n" \
250 " b 2b\n" \
251 ".previous\n" \
252 EX_TABLE(1b, 3b) \
253 : "=r" (err), "=r" (x) \
254 : "m"UPD_CONSTR (*addr), "i" (-EFAULT), "0" (err))
255
256 #ifdef __powerpc64__
257 #define __get_user_asm2(x, addr, err) \
258 __get_user_asm(x, addr, err, "ld")
259 #else /* __powerpc64__ */
260 #define __get_user_asm2(x, addr, err) \
261 __asm__ __volatile__( \
262 "1: lwz%X2 %1, %2\n" \
263 "2: lwz%X2 %L1, %L2\n" \
264 "3:\n" \
265 ".section .fixup,\"ax\"\n" \
266 "4: li %0,%3\n" \
267 " li %1,0\n" \
268 " li %1+1,0\n" \
269 " b 3b\n" \
270 ".previous\n" \
271 EX_TABLE(1b, 4b) \
272 EX_TABLE(2b, 4b) \
273 : "=r" (err), "=&r" (x) \
274 : "m" (*addr), "i" (-EFAULT), "0" (err))
275 #endif /* __powerpc64__ */
276
277 #define __get_user_size_allowed(x, ptr, size, retval) \
278 do { \
279 retval = 0; \
280 __chk_user_ptr(ptr); \
281 if (size > sizeof(x)) \
282 (x) = __get_user_bad(); \
283 switch (size) { \
284 case 1: __get_user_asm(x, (u8 __user *)ptr, retval, "lbz"); break; \
285 case 2: __get_user_asm(x, (u16 __user *)ptr, retval, "lhz"); break; \
286 case 4: __get_user_asm(x, (u32 __user *)ptr, retval, "lwz"); break; \
287 case 8: __get_user_asm2(x, (u64 __user *)ptr, retval); break; \
288 default: (x) = __get_user_bad(); \
289 } \
290 } while (0)
291
292 #define __get_user_size(x, ptr, size, retval) \
293 do { \
294 allow_read_from_user(ptr, size); \
295 __get_user_size_allowed(x, ptr, size, retval); \
296 prevent_read_from_user(ptr, size); \
297 } while (0)
298
299 /*
300 * This is a type: either unsigned long, if the argument fits into
301 * that type, or otherwise unsigned long long.
302 */
303 #define __long_type(x) \
304 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
305
306 #define __get_user_nocheck(x, ptr, size, do_allow) \
307 ({ \
308 long __gu_err; \
309 __long_type(*(ptr)) __gu_val; \
310 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
311 __typeof__(size) __gu_size = (size); \
312 \
313 __chk_user_ptr(__gu_addr); \
314 if (do_allow && !is_kernel_addr((unsigned long)__gu_addr)) \
315 might_fault(); \
316 barrier_nospec(); \
317 if (do_allow) \
318 __get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err); \
319 else \
320 __get_user_size_allowed(__gu_val, __gu_addr, __gu_size, __gu_err); \
321 (x) = (__typeof__(*(ptr)))__gu_val; \
322 \
323 __gu_err; \
324 })
325
326 #define __get_user_check(x, ptr, size) \
327 ({ \
328 long __gu_err = -EFAULT; \
329 __long_type(*(ptr)) __gu_val = 0; \
330 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
331 __typeof__(size) __gu_size = (size); \
332 \
333 might_fault(); \
334 if (access_ok(__gu_addr, __gu_size)) { \
335 barrier_nospec(); \
336 __get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err); \
337 } \
338 (x) = (__force __typeof__(*(ptr)))__gu_val; \
339 \
340 __gu_err; \
341 })
342
343 #define __get_user_nosleep(x, ptr, size) \
344 ({ \
345 long __gu_err; \
346 __long_type(*(ptr)) __gu_val; \
347 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
348 __typeof__(size) __gu_size = (size); \
349 \
350 __chk_user_ptr(__gu_addr); \
351 barrier_nospec(); \
352 __get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err); \
353 (x) = (__force __typeof__(*(ptr)))__gu_val; \
354 \
355 __gu_err; \
356 })
357
358
359 /* more complex routines */
360
361 extern unsigned long __copy_tofrom_user(void __user *to,
362 const void __user *from, unsigned long size);
363
364 #ifdef CONFIG_ARCH_HAS_COPY_MC
365 unsigned long __must_check
366 copy_mc_generic(void *to, const void *from, unsigned long size);
367
368 static inline unsigned long __must_check
copy_mc_to_kernel(void * to,const void * from,unsigned long size)369 copy_mc_to_kernel(void *to, const void *from, unsigned long size)
370 {
371 return copy_mc_generic(to, from, size);
372 }
373 #define copy_mc_to_kernel copy_mc_to_kernel
374
375 static inline unsigned long __must_check
copy_mc_to_user(void __user * to,const void * from,unsigned long n)376 copy_mc_to_user(void __user *to, const void *from, unsigned long n)
377 {
378 if (likely(check_copy_size(from, n, true))) {
379 if (access_ok(to, n)) {
380 allow_write_to_user(to, n);
381 n = copy_mc_generic((void *)to, from, n);
382 prevent_write_to_user(to, n);
383 }
384 }
385
386 return n;
387 }
388 #endif
389
390 #ifdef __powerpc64__
391 static inline unsigned long
raw_copy_in_user(void __user * to,const void __user * from,unsigned long n)392 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
393 {
394 unsigned long ret;
395
396 barrier_nospec();
397 allow_read_write_user(to, from, n);
398 ret = __copy_tofrom_user(to, from, n);
399 prevent_read_write_user(to, from, n);
400 return ret;
401 }
402 #endif /* __powerpc64__ */
403
raw_copy_from_user(void * to,const void __user * from,unsigned long n)404 static inline unsigned long raw_copy_from_user(void *to,
405 const void __user *from, unsigned long n)
406 {
407 unsigned long ret;
408 if (__builtin_constant_p(n) && (n <= 8)) {
409 ret = 1;
410
411 switch (n) {
412 case 1:
413 barrier_nospec();
414 __get_user_size(*(u8 *)to, from, 1, ret);
415 break;
416 case 2:
417 barrier_nospec();
418 __get_user_size(*(u16 *)to, from, 2, ret);
419 break;
420 case 4:
421 barrier_nospec();
422 __get_user_size(*(u32 *)to, from, 4, ret);
423 break;
424 case 8:
425 barrier_nospec();
426 __get_user_size(*(u64 *)to, from, 8, ret);
427 break;
428 }
429 if (ret == 0)
430 return 0;
431 }
432
433 barrier_nospec();
434 allow_read_from_user(from, n);
435 ret = __copy_tofrom_user((__force void __user *)to, from, n);
436 prevent_read_from_user(from, n);
437 return ret;
438 }
439
440 static inline unsigned long
raw_copy_to_user_allowed(void __user * to,const void * from,unsigned long n)441 raw_copy_to_user_allowed(void __user *to, const void *from, unsigned long n)
442 {
443 if (__builtin_constant_p(n) && (n <= 8)) {
444 unsigned long ret = 1;
445
446 switch (n) {
447 case 1:
448 __put_user_size_allowed(*(u8 *)from, (u8 __user *)to, 1, ret);
449 break;
450 case 2:
451 __put_user_size_allowed(*(u16 *)from, (u16 __user *)to, 2, ret);
452 break;
453 case 4:
454 __put_user_size_allowed(*(u32 *)from, (u32 __user *)to, 4, ret);
455 break;
456 case 8:
457 __put_user_size_allowed(*(u64 *)from, (u64 __user *)to, 8, ret);
458 break;
459 }
460 if (ret == 0)
461 return 0;
462 }
463
464 return __copy_tofrom_user(to, (__force const void __user *)from, n);
465 }
466
467 static inline unsigned long
raw_copy_to_user(void __user * to,const void * from,unsigned long n)468 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
469 {
470 unsigned long ret;
471
472 allow_write_to_user(to, n);
473 ret = raw_copy_to_user_allowed(to, from, n);
474 prevent_write_to_user(to, n);
475 return ret;
476 }
477
478 unsigned long __arch_clear_user(void __user *addr, unsigned long size);
479
clear_user(void __user * addr,unsigned long size)480 static inline unsigned long clear_user(void __user *addr, unsigned long size)
481 {
482 unsigned long ret = size;
483 might_fault();
484 if (likely(access_ok(addr, size))) {
485 allow_write_to_user(addr, size);
486 ret = __arch_clear_user(addr, size);
487 prevent_write_to_user(addr, size);
488 }
489 return ret;
490 }
491
__clear_user(void __user * addr,unsigned long size)492 static inline unsigned long __clear_user(void __user *addr, unsigned long size)
493 {
494 return clear_user(addr, size);
495 }
496
497 extern long strncpy_from_user(char *dst, const char __user *src, long count);
498 extern __must_check long strnlen_user(const char __user *str, long n);
499
500 extern long __copy_from_user_flushcache(void *dst, const void __user *src,
501 unsigned size);
502 extern void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
503 size_t len);
504
user_access_begin(const void __user * ptr,size_t len)505 static __must_check inline bool user_access_begin(const void __user *ptr, size_t len)
506 {
507 if (unlikely(!access_ok(ptr, len)))
508 return false;
509
510 might_fault();
511
512 allow_read_write_user((void __user *)ptr, ptr, len);
513 return true;
514 }
515 #define user_access_begin user_access_begin
516 #define user_access_end prevent_current_access_user
517 #define user_access_save prevent_user_access_return
518 #define user_access_restore restore_user_access
519
520 static __must_check inline bool
user_read_access_begin(const void __user * ptr,size_t len)521 user_read_access_begin(const void __user *ptr, size_t len)
522 {
523 if (unlikely(!access_ok(ptr, len)))
524 return false;
525
526 might_fault();
527
528 allow_read_from_user(ptr, len);
529 return true;
530 }
531 #define user_read_access_begin user_read_access_begin
532 #define user_read_access_end prevent_current_read_from_user
533
534 static __must_check inline bool
user_write_access_begin(const void __user * ptr,size_t len)535 user_write_access_begin(const void __user *ptr, size_t len)
536 {
537 if (unlikely(!access_ok(ptr, len)))
538 return false;
539
540 might_fault();
541
542 allow_write_to_user((void __user *)ptr, len);
543 return true;
544 }
545 #define user_write_access_begin user_write_access_begin
546 #define user_write_access_end prevent_current_write_to_user
547
548 #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
549 #define unsafe_get_user(x, p, e) unsafe_op_wrap(__get_user_allowed(x, p), e)
550 #define unsafe_put_user(x, p, e) __put_user_goto(x, p, e)
551
552 #define unsafe_copy_to_user(d, s, l, e) \
553 do { \
554 u8 __user *_dst = (u8 __user *)(d); \
555 const u8 *_src = (const u8 *)(s); \
556 size_t _len = (l); \
557 int _i; \
558 \
559 for (_i = 0; _i < (_len & ~(sizeof(long) - 1)); _i += sizeof(long)) \
560 __put_user_goto(*(long*)(_src + _i), (long __user *)(_dst + _i), e);\
561 if (IS_ENABLED(CONFIG_PPC64) && (_len & 4)) { \
562 __put_user_goto(*(u32*)(_src + _i), (u32 __user *)(_dst + _i), e); \
563 _i += 4; \
564 } \
565 if (_len & 2) { \
566 __put_user_goto(*(u16*)(_src + _i), (u16 __user *)(_dst + _i), e); \
567 _i += 2; \
568 } \
569 if (_len & 1) \
570 __put_user_goto(*(u8*)(_src + _i), (u8 __user *)(_dst + _i), e);\
571 } while (0)
572
573 #define HAVE_GET_KERNEL_NOFAULT
574
575 #define __get_kernel_nofault(dst, src, type, err_label) \
576 do { \
577 int __kr_err; \
578 \
579 __get_user_size_allowed(*((type *)(dst)), (__force type __user *)(src),\
580 sizeof(type), __kr_err); \
581 if (unlikely(__kr_err)) \
582 goto err_label; \
583 } while (0)
584
585 #define __put_kernel_nofault(dst, src, type, err_label) \
586 __put_user_size_goto(*((type *)(src)), \
587 (__force type __user *)(dst), sizeof(type), err_label)
588
589 #endif /* _ARCH_POWERPC_UACCESS_H */
590