blob: 991dfb2639987f23475f7cb261031ae5dedb3b39 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Rusty Russell61d0b5a2013-03-18 13:22:19 +10302#ifndef UACCESS_H
3#define UACCESS_H
Rusty Russell61d0b5a2013-03-18 13:22:19 +10304
Mark Rutlandea9156f2016-11-24 10:25:14 +00005#include <linux/compiler.h>
6
7extern void *__user_addr_min, *__user_addr_max;
Rusty Russell61d0b5a2013-03-18 13:22:19 +10308
9static inline void __chk_user_ptr(const volatile void *p, size_t size)
10{
11 assert(p >= __user_addr_min && p + size <= __user_addr_max);
12}
13
14#define put_user(x, ptr) \
15({ \
16 typeof(ptr) __pu_ptr = (ptr); \
17 __chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr)); \
Mark Rutlandea9156f2016-11-24 10:25:14 +000018 WRITE_ONCE(*(__pu_ptr), x); \
Rusty Russell61d0b5a2013-03-18 13:22:19 +103019 0; \
20})
21
22#define get_user(x, ptr) \
23({ \
24 typeof(ptr) __pu_ptr = (ptr); \
25 __chk_user_ptr(__pu_ptr, sizeof(*__pu_ptr)); \
Mark Rutlandea9156f2016-11-24 10:25:14 +000026 x = READ_ONCE(*(__pu_ptr)); \
Rusty Russell61d0b5a2013-03-18 13:22:19 +103027 0; \
28})
29
30static void volatile_memcpy(volatile char *to, const volatile char *from,
31 unsigned long n)
32{
33 while (n--)
34 *(to++) = *(from++);
35}
36
37static inline int copy_from_user(void *to, const void __user volatile *from,
38 unsigned long n)
39{
40 __chk_user_ptr(from, n);
41 volatile_memcpy(to, from, n);
42 return 0;
43}
44
45static inline int copy_to_user(void __user volatile *to, const void *from,
46 unsigned long n)
47{
48 __chk_user_ptr(to, n);
49 volatile_memcpy(to, from, n);
50 return 0;
51}
52#endif /* UACCESS_H */