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