uaccess.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef __LINUX_UACCESS_H__
  2. #define __LINUX_UACCESS_H__
  3. #include <linux/sched.h>
  4. #include <linux/thread_info.h>
  5. #define VERIFY_READ 0
  6. #define VERIFY_WRITE 1
  7. #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
  8. #include <asm/uaccess.h>
  9. static __always_inline void pagefault_disabled_inc(void)
  10. {
  11. current->pagefault_disabled++;
  12. }
  13. static __always_inline void pagefault_disabled_dec(void)
  14. {
  15. current->pagefault_disabled--;
  16. WARN_ON(current->pagefault_disabled < 0);
  17. }
  18. /*
  19. * These routines enable/disable the pagefault handler. If disabled, it will
  20. * not take any locks and go straight to the fixup table.
  21. *
  22. * User access methods will not sleep when called from a pagefault_disabled()
  23. * environment.
  24. */
  25. static inline void pagefault_disable(void)
  26. {
  27. pagefault_disabled_inc();
  28. /*
  29. * make sure to have issued the store before a pagefault
  30. * can hit.
  31. */
  32. barrier();
  33. }
  34. static inline void pagefault_enable(void)
  35. {
  36. /*
  37. * make sure to issue those last loads/stores before enabling
  38. * the pagefault handler again.
  39. */
  40. barrier();
  41. pagefault_disabled_dec();
  42. }
  43. /*
  44. * Is the pagefault handler disabled? If so, user access methods will not sleep.
  45. */
  46. #define pagefault_disabled() (current->pagefault_disabled != 0)
  47. /*
  48. * The pagefault handler is in general disabled by pagefault_disable() or
  49. * when in irq context (via in_atomic()).
  50. *
  51. * This function should only be used by the fault handlers. Other users should
  52. * stick to pagefault_disabled().
  53. * Please NEVER use preempt_disable() to disable the fault handler. With
  54. * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
  55. * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
  56. */
  57. #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
  58. #ifndef ARCH_HAS_NOCACHE_UACCESS
  59. static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
  60. const void __user *from, unsigned long n)
  61. {
  62. return __copy_from_user_inatomic(to, from, n);
  63. }
  64. static inline unsigned long __copy_from_user_nocache(void *to,
  65. const void __user *from, unsigned long n)
  66. {
  67. return __copy_from_user(to, from, n);
  68. }
  69. #endif /* ARCH_HAS_NOCACHE_UACCESS */
  70. /*
  71. * probe_kernel_read(): safely attempt to read from a location
  72. * @dst: pointer to the buffer that shall take the data
  73. * @src: address to read from
  74. * @size: size of the data chunk
  75. *
  76. * Safely read from address @src to the buffer at @dst. If a kernel fault
  77. * happens, handle that and return -EFAULT.
  78. */
  79. extern long probe_kernel_read(void *dst, const void *src, size_t size);
  80. extern long __probe_kernel_read(void *dst, const void *src, size_t size);
  81. /*
  82. * probe_kernel_write(): safely attempt to write to a location
  83. * @dst: address to write to
  84. * @src: pointer to the data that shall be written
  85. * @size: size of the data chunk
  86. *
  87. * Safely write to address @dst from the buffer at @src. If a kernel fault
  88. * happens, handle that and return -EFAULT.
  89. */
  90. extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
  91. extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
  92. extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
  93. /**
  94. * probe_kernel_address(): safely attempt to read from a location
  95. * @addr: address to read from
  96. * @retval: read into this variable
  97. *
  98. * Returns 0 on success, or -EFAULT.
  99. */
  100. #define probe_kernel_address(addr, retval) \
  101. probe_kernel_read(&retval, addr, sizeof(retval))
  102. #ifndef user_access_begin
  103. #define user_access_begin() do { } while (0)
  104. #define user_access_end() do { } while (0)
  105. #define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
  106. #define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
  107. #endif
  108. #endif /* __LINUX_UACCESS_H__ */