blob: fa05d01b2c9052fb405f5e3e1aefe1d51e646f4f [file] [log] [blame]
Rusty Russell61d0b5a2013-03-18 13:22:19 +10301#ifndef UACCESS_H
2#define UACCESS_H
Rusty Russell61d0b5a2013-03-18 13:22:19 +10303
Mark Rutlandea9156f2016-11-24 10:25:14 +00004#include <linux/compiler.h>
5
6extern void *__user_addr_min, *__user_addr_max;
Rusty Russell61d0b5a2013-03-18 13:22:19 +10307
8static 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)); \
Mark Rutlandea9156f2016-11-24 10:25:14 +000017 WRITE_ONCE(*(__pu_ptr), x); \
Rusty Russell61d0b5a2013-03-18 13:22:19 +103018 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)); \
Mark Rutlandea9156f2016-11-24 10:25:14 +000025 x = READ_ONCE(*(__pu_ptr)); \
Rusty Russell61d0b5a2013-03-18 13:22:19 +103026 0; \
27})
28
29static void volatile_memcpy(volatile char *to, const volatile char *from,
30 unsigned long n)
31{
32 while (n--)
33 *(to++) = *(from++);
34}
35
36static 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
44static 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 */