xref: /linux/arch/x86/include/asm/uaccess_32.h (revision 7cefa5a05dbda1f0bbbd98e9d2861b09a35cc6ea)
1 #ifndef _ASM_X86_UACCESS_32_H
2 #define _ASM_X86_UACCESS_32_H
3 
4 /*
5  * User space memory access functions
6  */
7 #include <linux/string.h>
8 #include <asm/asm.h>
9 #include <asm/page.h>
10 
11 unsigned long __must_check __copy_to_user_ll
12 		(void __user *to, const void *from, unsigned long n);
13 unsigned long __must_check __copy_from_user_ll
14 		(void *to, const void __user *from, unsigned long n);
15 unsigned long __must_check __copy_from_user_ll_nozero
16 		(void *to, const void __user *from, unsigned long n);
17 unsigned long __must_check __copy_from_user_ll_nocache
18 		(void *to, const void __user *from, unsigned long n);
19 unsigned long __must_check __copy_from_user_ll_nocache_nozero
20 		(void *to, const void __user *from, unsigned long n);
21 
22 /**
23  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
24  * @to:   Destination address, in user space.
25  * @from: Source address, in kernel space.
26  * @n:    Number of bytes to copy.
27  *
28  * Context: User context only.
29  *
30  * Copy data from kernel space to user space.  Caller must check
31  * the specified block with access_ok() before calling this function.
32  * The caller should also make sure he pins the user space address
33  * so that we don't result in page fault and sleep.
34  */
35 static __always_inline unsigned long __must_check
36 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
37 {
38 	check_object_size(from, n, true);
39 	return __copy_to_user_ll(to, from, n);
40 }
41 
42 /**
43  * __copy_to_user: - Copy a block of data into user space, with less checking.
44  * @to:   Destination address, in user space.
45  * @from: Source address, in kernel space.
46  * @n:    Number of bytes to copy.
47  *
48  * Context: User context only. This function may sleep if pagefaults are
49  *          enabled.
50  *
51  * Copy data from kernel space to user space.  Caller must check
52  * the specified block with access_ok() before calling this function.
53  *
54  * Returns number of bytes that could not be copied.
55  * On success, this will be zero.
56  */
57 static __always_inline unsigned long __must_check
58 __copy_to_user(void __user *to, const void *from, unsigned long n)
59 {
60 	might_fault();
61 	return __copy_to_user_inatomic(to, from, n);
62 }
63 
64 static __always_inline unsigned long
65 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
66 {
67 	return __copy_from_user_ll_nozero(to, from, n);
68 }
69 
70 /**
71  * __copy_from_user: - Copy a block of data from user space, with less checking.
72  * @to:   Destination address, in kernel space.
73  * @from: Source address, in user space.
74  * @n:    Number of bytes to copy.
75  *
76  * Context: User context only. This function may sleep if pagefaults are
77  *          enabled.
78  *
79  * Copy data from user space to kernel space.  Caller must check
80  * the specified block with access_ok() before calling this function.
81  *
82  * Returns number of bytes that could not be copied.
83  * On success, this will be zero.
84  *
85  * If some data could not be copied, this function will pad the copied
86  * data to the requested size using zero bytes.
87  *
88  * An alternate version - __copy_from_user_inatomic() - may be called from
89  * atomic context and will fail rather than sleep.  In this case the
90  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
91  * for explanation of why this is needed.
92  */
93 static __always_inline unsigned long
94 __copy_from_user(void *to, const void __user *from, unsigned long n)
95 {
96 	might_fault();
97 	check_object_size(to, n, false);
98 	if (__builtin_constant_p(n)) {
99 		unsigned long ret;
100 
101 		switch (n) {
102 		case 1:
103 			__uaccess_begin();
104 			__get_user_size(*(u8 *)to, from, 1, ret, 1);
105 			__uaccess_end();
106 			return ret;
107 		case 2:
108 			__uaccess_begin();
109 			__get_user_size(*(u16 *)to, from, 2, ret, 2);
110 			__uaccess_end();
111 			return ret;
112 		case 4:
113 			__uaccess_begin();
114 			__get_user_size(*(u32 *)to, from, 4, ret, 4);
115 			__uaccess_end();
116 			return ret;
117 		}
118 	}
119 	return __copy_from_user_ll(to, from, n);
120 }
121 
122 static __always_inline unsigned long __copy_from_user_nocache(void *to,
123 				const void __user *from, unsigned long n)
124 {
125 	might_fault();
126 	if (__builtin_constant_p(n)) {
127 		unsigned long ret;
128 
129 		switch (n) {
130 		case 1:
131 			__uaccess_begin();
132 			__get_user_size(*(u8 *)to, from, 1, ret, 1);
133 			__uaccess_end();
134 			return ret;
135 		case 2:
136 			__uaccess_begin();
137 			__get_user_size(*(u16 *)to, from, 2, ret, 2);
138 			__uaccess_end();
139 			return ret;
140 		case 4:
141 			__uaccess_begin();
142 			__get_user_size(*(u32 *)to, from, 4, ret, 4);
143 			__uaccess_end();
144 			return ret;
145 		}
146 	}
147 	return __copy_from_user_ll_nocache(to, from, n);
148 }
149 
150 static __always_inline unsigned long
151 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
152 				  unsigned long n)
153 {
154        return __copy_from_user_ll_nocache_nozero(to, from, n);
155 }
156 
157 #endif /* _ASM_X86_UACCESS_32_H */
158