1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
4  * Copyright (C) 2015 Richard Weinberger (richard@nod.at)
5  */
6 
7 #ifndef __UM_UACCESS_H
8 #define __UM_UACCESS_H
9 
10 #include <asm/elf.h>
11 #include <linux/unaligned.h>
12 #include <sysdep/faultinfo.h>
13 
14 #define __under_task_size(addr, size) \
15 	(((unsigned long) (addr) < TASK_SIZE) && \
16 	 (((unsigned long) (addr) + (size)) < TASK_SIZE))
17 
18 #define __access_ok_vsyscall(addr, size) \
19 	  (((unsigned long) (addr) >= FIXADDR_USER_START) && \
20 	  ((unsigned long) (addr) + (size) <= FIXADDR_USER_END) && \
21 	  ((unsigned long) (addr) + (size) >= (unsigned long)(addr)))
22 
23 #define __addr_range_nowrap(addr, size) \
24 	((unsigned long) (addr) <= ((unsigned long) (addr) + (size)))
25 
26 extern unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n);
27 extern unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n);
28 extern unsigned long __clear_user(void __user *mem, unsigned long len);
29 static inline int __access_ok(const void __user *ptr, unsigned long size);
30 
31 /* Teach asm-generic/uaccess.h that we have C functions for these. */
32 #define __access_ok __access_ok
33 #define __clear_user __clear_user
34 
35 #define INLINE_COPY_FROM_USER
36 #define INLINE_COPY_TO_USER
37 
38 #include <asm-generic/uaccess.h>
39 
40 static inline int __access_ok(const void __user *ptr, unsigned long size)
41 {
42 	unsigned long addr = (unsigned long)ptr;
43 	return __addr_range_nowrap(addr, size) &&
44 		(__under_task_size(addr, size) ||
45 		 __access_ok_vsyscall(addr, size));
46 }
47 
48 #define __get_kernel_nofault(dst, src, type, err_label)			\
49 do {									\
50 	int __faulted;							\
51 									\
52 	___backtrack_faulted(__faulted);				\
53 	if (__faulted) {						\
54 		*((type *)dst) = (type) 0;				\
55 		goto err_label;						\
56 	}								\
57 	*((type *)dst) = get_unaligned((type *)(src));			\
58 	barrier();							\
59 	current->thread.segv_continue = NULL;				\
60 } while (0)
61 
62 #define __put_kernel_nofault(dst, src, type, err_label)			\
63 do {									\
64 	int __faulted;							\
65 									\
66 	___backtrack_faulted(__faulted);				\
67 	if (__faulted)							\
68 		goto err_label;						\
69 	put_unaligned(*((type *)src), (type *)(dst));			\
70 	barrier();							\
71 	current->thread.segv_continue = NULL;				\
72 } while (0)
73 
74 #endif
75