blob: b786ca2419b4fc42120c3c18bb6d22c7a3a2fb13 [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
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070010#include <asm/uaccess.h>
11
David Hildenbrand8bcbde52015-05-11 17:52:06 +020012static __always_inline void pagefault_disabled_inc(void)
13{
14 current->pagefault_disabled++;
15}
16
17static __always_inline void pagefault_disabled_dec(void)
18{
19 current->pagefault_disabled--;
20 WARN_ON(current->pagefault_disabled < 0);
21}
22
Peter Zijlstraa8663742006-12-06 20:32:20 -080023/*
David Hildenbrand8bcbde52015-05-11 17:52:06 +020024 * These routines enable/disable the pagefault handler. If disabled, it will
25 * not take any locks and go straight to the fixup table.
Peter Zijlstraa8663742006-12-06 20:32:20 -080026 *
David Hildenbrand8222dbe2015-05-11 17:52:20 +020027 * User access methods will not sleep when called from a pagefault_disabled()
28 * environment.
Peter Zijlstraa8663742006-12-06 20:32:20 -080029 */
30static inline void pagefault_disable(void)
31{
David Hildenbrand8bcbde52015-05-11 17:52:06 +020032 pagefault_disabled_inc();
Peter Zijlstraa8663742006-12-06 20:32:20 -080033 /*
34 * make sure to have issued the store before a pagefault
35 * can hit.
36 */
37 barrier();
38}
39
40static inline void pagefault_enable(void)
41{
42 /*
43 * make sure to issue those last loads/stores before enabling
44 * the pagefault handler again.
45 */
46 barrier();
David Hildenbrand8bcbde52015-05-11 17:52:06 +020047 pagefault_disabled_dec();
Peter Zijlstraa8663742006-12-06 20:32:20 -080048}
49
David Hildenbrand8bcbde52015-05-11 17:52:06 +020050/*
51 * Is the pagefault handler disabled? If so, user access methods will not sleep.
52 */
53#define pagefault_disabled() (current->pagefault_disabled != 0)
54
David Hildenbrand70ffdb92015-05-11 17:52:11 +020055/*
56 * The pagefault handler is in general disabled by pagefault_disable() or
57 * when in irq context (via in_atomic()).
58 *
59 * This function should only be used by the fault handlers. Other users should
60 * stick to pagefault_disabled().
61 * Please NEVER use preempt_disable() to disable the fault handler. With
62 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
63 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
64 */
65#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
66
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070067#ifndef ARCH_HAS_NOCACHE_UACCESS
68
69static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
70 const void __user *from, unsigned long n)
71{
72 return __copy_from_user_inatomic(to, from, n);
73}
74
75static inline unsigned long __copy_from_user_nocache(void *to,
76 const void __user *from, unsigned long n)
77{
78 return __copy_from_user(to, from, n);
79}
80
81#endif /* ARCH_HAS_NOCACHE_UACCESS */
82
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020083/*
84 * probe_kernel_read(): safely attempt to read from a location
85 * @dst: pointer to the buffer that shall take the data
86 * @src: address to read from
87 * @size: size of the data chunk
88 *
89 * Safely read from address @src to the buffer at @dst. If a kernel fault
90 * happens, handle that and return -EFAULT.
91 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040092extern long probe_kernel_read(void *dst, const void *src, size_t size);
93extern long __probe_kernel_read(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020094
95/*
96 * probe_kernel_write(): safely attempt to write to a location
97 * @dst: address to write to
98 * @src: pointer to the data that shall be written
99 * @size: size of the data chunk
100 *
101 * Safely write to address @dst from the buffer at @src. If a kernel fault
102 * happens, handle that and return -EFAULT.
103 */
Steven Rostedtf29c5042011-05-19 14:35:33 -0400104extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
105extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200106
Alexei Starovoitov1a6877b2015-08-28 15:56:22 -0700107extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
108
Andrew Morton0ab32b6f2015-11-05 18:46:03 -0800109/**
110 * probe_kernel_address(): safely attempt to read from a location
111 * @addr: address to read from
112 * @retval: read into this variable
113 *
114 * Returns 0 on success, or -EFAULT.
115 */
116#define probe_kernel_address(addr, retval) \
117 probe_kernel_read(&retval, addr, sizeof(retval))
118
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800119#ifndef user_access_begin
120#define user_access_begin() do { } while (0)
121#define user_access_end() do { } while (0)
Linus Torvalds1bd44032016-08-08 13:02:01 -0700122#define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
123#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 -0800124#endif
125
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700126#endif /* __LINUX_UACCESS_H__ */