uaccess.h 1.1 KB

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