blob: 9c3ae8706e9dd1f4f81c518f7352593d3be9af50 [file] [log] [blame]
Hiro Yoshiokac22ce142006-06-23 02:04:16 -07001#ifndef __LINUX_UACCESS_H__
2#define __LINUX_UACCESS_H__
3
David Hildenbrand8bcbde52015-05-11 17:52:06 +02004#include <linux/sched.h>
Al Viroaf1d5b32016-12-27 18:14:09 -05005#include <linux/thread_info.h>
Al Viro5e6039d2016-12-27 18:00:15 -05006
7#define VERIFY_READ 0
8#define VERIFY_WRITE 1
9
Al Virodb68ce12017-03-20 21:08:07 -040010#define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
11
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070012#include <asm/uaccess.h>
13
David Hildenbrand8bcbde52015-05-11 17:52:06 +020014static __always_inline void pagefault_disabled_inc(void)
15{
16 current->pagefault_disabled++;
17}
18
19static __always_inline void pagefault_disabled_dec(void)
20{
21 current->pagefault_disabled--;
22 WARN_ON(current->pagefault_disabled < 0);
23}
24
Peter Zijlstraa8663742006-12-06 20:32:20 -080025/*
David Hildenbrand8bcbde52015-05-11 17:52:06 +020026 * These routines enable/disable the pagefault handler. If disabled, it will
27 * not take any locks and go straight to the fixup table.
Peter Zijlstraa8663742006-12-06 20:32:20 -080028 *
David Hildenbrand8222dbe2015-05-11 17:52:20 +020029 * User access methods will not sleep when called from a pagefault_disabled()
30 * environment.
Peter Zijlstraa8663742006-12-06 20:32:20 -080031 */
32static inline void pagefault_disable(void)
33{
David Hildenbrand8bcbde52015-05-11 17:52:06 +020034 pagefault_disabled_inc();
Peter Zijlstraa8663742006-12-06 20:32:20 -080035 /*
36 * make sure to have issued the store before a pagefault
37 * can hit.
38 */
39 barrier();
40}
41
42static inline void pagefault_enable(void)
43{
44 /*
45 * make sure to issue those last loads/stores before enabling
46 * the pagefault handler again.
47 */
48 barrier();
David Hildenbrand8bcbde52015-05-11 17:52:06 +020049 pagefault_disabled_dec();
Peter Zijlstraa8663742006-12-06 20:32:20 -080050}
51
David Hildenbrand8bcbde52015-05-11 17:52:06 +020052/*
53 * Is the pagefault handler disabled? If so, user access methods will not sleep.
54 */
55#define pagefault_disabled() (current->pagefault_disabled != 0)
56
David Hildenbrand70ffdb92015-05-11 17:52:11 +020057/*
58 * The pagefault handler is in general disabled by pagefault_disable() or
59 * when in irq context (via in_atomic()).
60 *
61 * This function should only be used by the fault handlers. Other users should
62 * stick to pagefault_disabled().
63 * Please NEVER use preempt_disable() to disable the fault handler. With
64 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
65 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
66 */
67#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
68
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070069#ifndef ARCH_HAS_NOCACHE_UACCESS
70
71static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
72 const void __user *from, unsigned long n)
73{
74 return __copy_from_user_inatomic(to, from, n);
75}
76
77static inline unsigned long __copy_from_user_nocache(void *to,
78 const void __user *from, unsigned long n)
79{
80 return __copy_from_user(to, from, n);
81}
82
83#endif /* ARCH_HAS_NOCACHE_UACCESS */
84
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020085/*
86 * probe_kernel_read(): safely attempt to read from a location
87 * @dst: pointer to the buffer that shall take the data
88 * @src: address to read from
89 * @size: size of the data chunk
90 *
91 * Safely read from address @src to the buffer at @dst. If a kernel fault
92 * happens, handle that and return -EFAULT.
93 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040094extern long probe_kernel_read(void *dst, const void *src, size_t size);
95extern long __probe_kernel_read(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020096
97/*
98 * probe_kernel_write(): safely attempt to write to a location
99 * @dst: address to write to
100 * @src: pointer to the data that shall be written
101 * @size: size of the data chunk
102 *
103 * Safely write to address @dst from the buffer at @src. If a kernel fault
104 * happens, handle that and return -EFAULT.
105 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400106extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
107extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200108
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700109extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
110
Andrew Morton0ab32b6f2015-11-05 18:46:03 -0800111/**
112 * probe_kernel_address(): safely attempt to read from a location
113 * @addr: address to read from
114 * @retval: read into this variable
115 *
116 * Returns 0 on success, or -EFAULT.
117 */
118#define probe_kernel_address(addr, retval) \
119 probe_kernel_read(&retval, addr, sizeof(retval))
120
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800121#ifndef user_access_begin
122#define user_access_begin() do { } while (0)
123#define user_access_end() do { } while (0)
Linus Torvalds1bd44032016-08-08 13:02:01 -0700124#define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
125#define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800126#endif
127
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700128#endif /* __LINUX_UACCESS_H__ */