Commit | Line | Data |
---|---|---|
c22ce143 HY |
1 | #ifndef __LINUX_UACCESS_H__ |
2 | #define __LINUX_UACCESS_H__ | |
3 | ||
8bcbde54 | 4 | #include <linux/sched.h> |
c22ce143 HY |
5 | #include <asm/uaccess.h> |
6 | ||
8bcbde54 DH |
7 | static __always_inline void pagefault_disabled_inc(void) |
8 | { | |
9 | current->pagefault_disabled++; | |
10 | } | |
11 | ||
12 | static __always_inline void pagefault_disabled_dec(void) | |
13 | { | |
14 | current->pagefault_disabled--; | |
15 | WARN_ON(current->pagefault_disabled < 0); | |
16 | } | |
17 | ||
a866374a | 18 | /* |
8bcbde54 DH |
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 | * | |
8222dbe2 DH |
22 | * User access methods will not sleep when called from a pagefault_disabled() |
23 | * environment. | |
a866374a PZ |
24 | */ |
25 | static inline void pagefault_disable(void) | |
26 | { | |
8bcbde54 | 27 | pagefault_disabled_inc(); |
a866374a PZ |
28 | /* |
29 | * make sure to have issued the store before a pagefault | |
30 | * can hit. | |
31 | */ | |
32 | barrier(); | |
33 | } | |
34 | ||
35 | static inline void pagefault_enable(void) | |
36 | { | |
37 | /* | |
38 | * make sure to issue those last loads/stores before enabling | |
39 | * the pagefault handler again. | |
40 | */ | |
41 | barrier(); | |
8bcbde54 | 42 | pagefault_disabled_dec(); |
a866374a PZ |
43 | } |
44 | ||
8bcbde54 DH |
45 | /* |
46 | * Is the pagefault handler disabled? If so, user access methods will not sleep. | |
47 | */ | |
48 | #define pagefault_disabled() (current->pagefault_disabled != 0) | |
49 | ||
70ffdb93 DH |
50 | /* |
51 | * The pagefault handler is in general disabled by pagefault_disable() or | |
52 | * when in irq context (via in_atomic()). | |
53 | * | |
54 | * This function should only be used by the fault handlers. Other users should | |
55 | * stick to pagefault_disabled(). | |
56 | * Please NEVER use preempt_disable() to disable the fault handler. With | |
57 | * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled. | |
58 | * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT. | |
59 | */ | |
60 | #define faulthandler_disabled() (pagefault_disabled() || in_atomic()) | |
61 | ||
c22ce143 HY |
62 | #ifndef ARCH_HAS_NOCACHE_UACCESS |
63 | ||
64 | static inline unsigned long __copy_from_user_inatomic_nocache(void *to, | |
65 | const void __user *from, unsigned long n) | |
66 | { | |
67 | return __copy_from_user_inatomic(to, from, n); | |
68 | } | |
69 | ||
70 | static inline unsigned long __copy_from_user_nocache(void *to, | |
71 | const void __user *from, unsigned long n) | |
72 | { | |
73 | return __copy_from_user(to, from, n); | |
74 | } | |
75 | ||
76 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ | |
77 | ||
1b79e551 AM |
78 | /** |
79 | * probe_kernel_address(): safely attempt to read from a location | |
80 | * @addr: address to read from - its type is type typeof(retval)* | |
81 | * @retval: read into this variable | |
82 | * | |
83 | * Safely read from address @addr into variable @revtal. If a kernel fault | |
84 | * happens, handle that and return -EFAULT. | |
85 | * We ensure that the __get_user() is executed in atomic context so that | |
86 | * do_page_fault() doesn't attempt to take mmap_sem. This makes | |
87 | * probe_kernel_address() suitable for use within regions where the caller | |
88 | * already holds mmap_sem, or other locks which nest inside mmap_sem. | |
20aa7b21 AM |
89 | * This must be a macro because __get_user() needs to know the types of the |
90 | * args. | |
91 | * | |
92 | * We don't include enough header files to be able to do the set_fs(). We | |
93 | * require that the probe_kernel_address() caller will do that. | |
1b79e551 AM |
94 | */ |
95 | #define probe_kernel_address(addr, retval) \ | |
96 | ({ \ | |
97 | long ret; \ | |
20aa7b21 | 98 | mm_segment_t old_fs = get_fs(); \ |
1b79e551 | 99 | \ |
20aa7b21 | 100 | set_fs(KERNEL_DS); \ |
a866374a | 101 | pagefault_disable(); \ |
fb71e453 | 102 | ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval)); \ |
a866374a | 103 | pagefault_enable(); \ |
20aa7b21 | 104 | set_fs(old_fs); \ |
1b79e551 AM |
105 | ret; \ |
106 | }) | |
107 | ||
c33fa9f5 IM |
108 | /* |
109 | * probe_kernel_read(): safely attempt to read from a location | |
110 | * @dst: pointer to the buffer that shall take the data | |
111 | * @src: address to read from | |
112 | * @size: size of the data chunk | |
113 | * | |
114 | * Safely read from address @src to the buffer at @dst. If a kernel fault | |
115 | * happens, handle that and return -EFAULT. | |
116 | */ | |
f29c5041 SR |
117 | extern long probe_kernel_read(void *dst, const void *src, size_t size); |
118 | extern long __probe_kernel_read(void *dst, const void *src, size_t size); | |
c33fa9f5 IM |
119 | |
120 | /* | |
121 | * probe_kernel_write(): safely attempt to write to a location | |
122 | * @dst: address to write to | |
123 | * @src: pointer to the data that shall be written | |
124 | * @size: size of the data chunk | |
125 | * | |
126 | * Safely write to address @dst from the buffer at @src. If a kernel fault | |
127 | * happens, handle that and return -EFAULT. | |
128 | */ | |
f29c5041 SR |
129 | extern long notrace probe_kernel_write(void *dst, const void *src, size_t size); |
130 | extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size); | |
c33fa9f5 | 131 | |
c22ce143 | 132 | #endif /* __LINUX_UACCESS_H__ */ |