uaccess.h 3.6 KB

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