uaccess.h 3.3 KB

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