blob: ecd3319dac33140a21a7c8fd89a2c95c18ce9e42 [file] [log] [blame]
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001#ifndef __LINUX_UACCESS_H__
2#define __LINUX_UACCESS_H__
3
Peter Zijlstraa8663742006-12-06 20:32:20 -08004#include <linux/preempt.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07005#include <asm/uaccess.h>
6
Peter Zijlstraa8663742006-12-06 20:32:20 -08007/*
8 * These routines enable/disable the pagefault handler in that
9 * it will not take any locks and go straight to the fixup table.
10 *
11 * They have great resemblance to the preempt_disable/enable calls
12 * and in fact they are identical; this is because currently there is
13 * no other way to make the pagefault handlers do this. So we do
14 * disable preemption but we don't necessarily care about that.
15 */
16static inline void pagefault_disable(void)
17{
Peter Zijlstrabdb43802013-09-10 12:15:23 +020018 preempt_count_inc();
Peter Zijlstraa8663742006-12-06 20:32:20 -080019 /*
20 * make sure to have issued the store before a pagefault
21 * can hit.
22 */
23 barrier();
24}
25
26static inline void pagefault_enable(void)
27{
Peter Zijlstra62b94a02013-11-20 16:52:19 +010028#ifndef CONFIG_PREEMPT
Peter Zijlstraa8663742006-12-06 20:32:20 -080029 /*
30 * make sure to issue those last loads/stores before enabling
31 * the pagefault handler again.
32 */
33 barrier();
Peter Zijlstrabdb43802013-09-10 12:15:23 +020034 preempt_count_dec();
Peter Zijlstra62b94a02013-11-20 16:52:19 +010035#else
36 preempt_enable();
37#endif
Peter Zijlstraa8663742006-12-06 20:32:20 -080038}
39
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070040#ifndef ARCH_HAS_NOCACHE_UACCESS
41
42static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
43 const void __user *from, unsigned long n)
44{
45 return __copy_from_user_inatomic(to, from, n);
46}
47
48static inline unsigned long __copy_from_user_nocache(void *to,
49 const void __user *from, unsigned long n)
50{
51 return __copy_from_user(to, from, n);
52}
53
54#endif /* ARCH_HAS_NOCACHE_UACCESS */
55
Andrew Morton1b79e552006-09-27 01:51:14 -070056/**
57 * probe_kernel_address(): safely attempt to read from a location
58 * @addr: address to read from - its type is type typeof(retval)*
59 * @retval: read into this variable
60 *
61 * Safely read from address @addr into variable @revtal. If a kernel fault
62 * happens, handle that and return -EFAULT.
63 * We ensure that the __get_user() is executed in atomic context so that
64 * do_page_fault() doesn't attempt to take mmap_sem. This makes
65 * probe_kernel_address() suitable for use within regions where the caller
66 * already holds mmap_sem, or other locks which nest inside mmap_sem.
Andrew Morton20aa7b22006-12-06 20:36:40 -080067 * This must be a macro because __get_user() needs to know the types of the
68 * args.
69 *
70 * We don't include enough header files to be able to do the set_fs(). We
71 * require that the probe_kernel_address() caller will do that.
Andrew Morton1b79e552006-09-27 01:51:14 -070072 */
73#define probe_kernel_address(addr, retval) \
74 ({ \
75 long ret; \
Andrew Morton20aa7b22006-12-06 20:36:40 -080076 mm_segment_t old_fs = get_fs(); \
Andrew Morton1b79e552006-09-27 01:51:14 -070077 \
Andrew Morton20aa7b22006-12-06 20:36:40 -080078 set_fs(KERNEL_DS); \
Peter Zijlstraa8663742006-12-06 20:32:20 -080079 pagefault_disable(); \
Hiroshi Shimamotofb71e452008-09-15 18:04:26 -070080 ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval)); \
Peter Zijlstraa8663742006-12-06 20:32:20 -080081 pagefault_enable(); \
Andrew Morton20aa7b22006-12-06 20:36:40 -080082 set_fs(old_fs); \
Andrew Morton1b79e552006-09-27 01:51:14 -070083 ret; \
84 })
85
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020086/*
87 * probe_kernel_read(): safely attempt to read from a location
88 * @dst: pointer to the buffer that shall take the data
89 * @src: address to read from
90 * @size: size of the data chunk
91 *
92 * Safely read from address @src to the buffer at @dst. If a kernel fault
93 * happens, handle that and return -EFAULT.
94 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040095extern long probe_kernel_read(void *dst, const void *src, size_t size);
96extern long __probe_kernel_read(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020097
98/*
99 * probe_kernel_write(): safely attempt to write to a location
100 * @dst: address to write to
101 * @src: pointer to the data that shall be written
102 * @size: size of the data chunk
103 *
104 * Safely write to address @dst from the buffer at @src. If a kernel fault
105 * happens, handle that and return -EFAULT.
106 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400107extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
108extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200109
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700110#endif /* __LINUX_UACCESS_H__ */