xref: /linux-6.15/tools/virtio/linux/uaccess.h (revision 564f7dfd)
1 #ifndef UACCESS_H
2 #define UACCESS_H
3 
4 #include <linux/compiler.h>
5 
6 extern void *__user_addr_min, *__user_addr_max;
7 
8 static inline void __chk_user_ptr(const volatile void *p, size_t size)
9 {
10 	assert(p >= __user_addr_min && p + size <= __user_addr_max);
11 }
12 
13 #define put_user(x, ptr)					\
14 ({								\
15 	typeof(ptr) __pu_ptr = (ptr);				\
16 	__chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr));		\
17 	WRITE_ONCE(*(__pu_ptr), x);				\
18 	0;							\
19 })
20 
21 #define get_user(x, ptr)					\
22 ({								\
23 	typeof(ptr) __pu_ptr = (ptr);				\
24 	__chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr));		\
25 	x = READ_ONCE(*(__pu_ptr));				\
26 	0;							\
27 })
28 
29 static void volatile_memcpy(volatile char *to, const volatile char *from,
30 			    unsigned long n)
31 {
32 	while (n--)
33 		*(to++) = *(from++);
34 }
35 
36 static inline int copy_from_user(void *to, const void __user volatile *from,
37 				 unsigned long n)
38 {
39 	__chk_user_ptr(from, n);
40 	volatile_memcpy(to, from, n);
41 	return 0;
42 }
43 
44 static inline int copy_to_user(void __user volatile *to, const void *from,
45 			       unsigned long n)
46 {
47 	__chk_user_ptr(to, n);
48 	volatile_memcpy(to, from, n);
49 	return 0;
50 }
51 #endif /* UACCESS_H */
52