uaccess.h 1.1 KB

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