]>
Commit | Line | Data |
---|---|---|
7f9ea6b7 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
ace02e2b GH |
2 | // Copyright (C) 2005-2017 Andes Technology Corporation |
3 | ||
4 | #ifndef _ASMANDES_UACCESS_H | |
5 | #define _ASMANDES_UACCESS_H | |
6 | ||
7 | /* | |
8 | * User space memory access functions | |
9 | */ | |
10 | #include <linux/sched.h> | |
11 | #include <asm/errno.h> | |
12 | #include <asm/memory.h> | |
13 | #include <asm/types.h> | |
967747bb | 14 | #include <asm-generic/access_ok.h> |
ace02e2b | 15 | |
ace02e2b GH |
16 | #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t" |
17 | ||
18 | /* | |
19 | * The exception table consists of pairs of addresses: the first is the | |
20 | * address of an instruction that is allowed to fault, and the second is | |
21 | * the address at which the program should continue. No registers are | |
22 | * modified, so it is entirely up to the continuation code to figure out | |
23 | * what to do. | |
24 | * | |
25 | * All the routines below use bits of fixup code that are out of line | |
26 | * with the main instruction path. This means when everything is well, | |
27 | * we don't even have to jump over them. Further, they do not intrude | |
28 | * on our cache or tlb entries. | |
29 | */ | |
30 | ||
31 | struct exception_table_entry { | |
32 | unsigned long insn, fixup; | |
33 | }; | |
34 | ||
35 | extern int fixup_exception(struct pt_regs *regs); | |
36 | ||
ace02e2b GH |
37 | /* |
38 | * Single-value transfer routines. They automatically use the right | |
39 | * size if we just have the right pointer type. Note that the functions | |
40 | * which read from user space (*get_*) need to take care not to leak | |
41 | * kernel data even if the calling code is buggy and fails to check | |
42 | * the return value. This means zeroing out the destination variable | |
43 | * or buffer on error. Normally this is done out of line by the | |
44 | * fixup code, but there are a few places where it intrudes on the | |
45 | * main code path. When we only write to user space, there is no | |
46 | * problem. | |
47 | * | |
48 | * The "__xxx" versions of the user access functions do not verify the | |
49 | * address space - it must have been done previously with a separate | |
50 | * "access_ok()" call. | |
51 | * | |
52 | * The "xxx_error" versions set the third argument to EFAULT if an | |
53 | * error occurs, and leave it unchanged on success. Note that these | |
54 | * versions are void (ie, don't return a value as such). | |
55 | */ | |
56 | ||
8926d88c | 57 | #define get_user(x, ptr) \ |
ace02e2b GH |
58 | ({ \ |
59 | long __gu_err = 0; \ | |
487913ab | 60 | __get_user_check((x), (ptr), __gu_err); \ |
ace02e2b GH |
61 | __gu_err; \ |
62 | }) | |
63 | ||
7ef39548 | 64 | #define __get_user_error(x, ptr, err) \ |
ace02e2b | 65 | ({ \ |
487913ab | 66 | __get_user_check((x), (ptr), (err)); \ |
7ef39548 | 67 | (void)0; \ |
ace02e2b GH |
68 | }) |
69 | ||
8926d88c AB |
70 | #define __get_user(x, ptr) \ |
71 | ({ \ | |
72 | long __gu_err = 0; \ | |
73 | const __typeof__(*(ptr)) __user *__p = (ptr); \ | |
74 | __get_user_err((x), __p, (__gu_err)); \ | |
75 | __gu_err; \ | |
76 | }) | |
77 | ||
487913ab ZL |
78 | #define __get_user_check(x, ptr, err) \ |
79 | ({ \ | |
80 | const __typeof__(*(ptr)) __user *__p = (ptr); \ | |
81 | might_fault(); \ | |
96d4f267 | 82 | if (access_ok(__p, sizeof(*__p))) { \ |
487913ab ZL |
83 | __get_user_err((x), __p, (err)); \ |
84 | } else { \ | |
85 | (x) = 0; (err) = -EFAULT; \ | |
86 | } \ | |
87 | }) | |
88 | ||
7ef39548 | 89 | #define __get_user_err(x, ptr, err) \ |
ace02e2b | 90 | do { \ |
ace02e2b | 91 | unsigned long __gu_val; \ |
487913ab ZL |
92 | __chk_user_ptr(ptr); \ |
93 | switch (sizeof(*(ptr))) { \ | |
ace02e2b | 94 | case 1: \ |
487913ab | 95 | __get_user_asm("lbi", __gu_val, (ptr), (err)); \ |
ace02e2b GH |
96 | break; \ |
97 | case 2: \ | |
487913ab | 98 | __get_user_asm("lhi", __gu_val, (ptr), (err)); \ |
ace02e2b GH |
99 | break; \ |
100 | case 4: \ | |
487913ab | 101 | __get_user_asm("lwi", __gu_val, (ptr), (err)); \ |
ace02e2b GH |
102 | break; \ |
103 | case 8: \ | |
487913ab | 104 | __get_user_asm_dword(__gu_val, (ptr), (err)); \ |
ace02e2b GH |
105 | break; \ |
106 | default: \ | |
107 | BUILD_BUG(); \ | |
108 | break; \ | |
109 | } \ | |
487913ab | 110 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
ace02e2b GH |
111 | } while (0) |
112 | ||
7ef39548 ZL |
113 | #define __get_user_asm(inst, x, addr, err) \ |
114 | __asm__ __volatile__ ( \ | |
115 | "1: "inst" %1,[%2]\n" \ | |
116 | "2:\n" \ | |
117 | " .section .fixup,\"ax\"\n" \ | |
118 | " .align 2\n" \ | |
119 | "3: move %0, %3\n" \ | |
120 | " move %1, #0\n" \ | |
121 | " b 2b\n" \ | |
122 | " .previous\n" \ | |
123 | " .section __ex_table,\"a\"\n" \ | |
124 | " .align 3\n" \ | |
125 | " .long 1b, 3b\n" \ | |
126 | " .previous" \ | |
127 | : "+r" (err), "=&r" (x) \ | |
128 | : "r" (addr), "i" (-EFAULT) \ | |
129 | : "cc") | |
ace02e2b GH |
130 | |
131 | #ifdef __NDS32_EB__ | |
132 | #define __gu_reg_oper0 "%H1" | |
133 | #define __gu_reg_oper1 "%L1" | |
134 | #else | |
135 | #define __gu_reg_oper0 "%L1" | |
136 | #define __gu_reg_oper1 "%H1" | |
137 | #endif | |
138 | ||
139 | #define __get_user_asm_dword(x, addr, err) \ | |
7ef39548 ZL |
140 | __asm__ __volatile__ ( \ |
141 | "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \ | |
142 | "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \ | |
143 | "3:\n" \ | |
144 | " .section .fixup,\"ax\"\n" \ | |
145 | " .align 2\n" \ | |
146 | "4: move %0, %3\n" \ | |
147 | " b 3b\n" \ | |
148 | " .previous\n" \ | |
149 | " .section __ex_table,\"a\"\n" \ | |
150 | " .align 3\n" \ | |
151 | " .long 1b, 4b\n" \ | |
152 | " .long 2b, 4b\n" \ | |
153 | " .previous" \ | |
154 | : "+r"(err), "=&r"(x) \ | |
155 | : "r"(addr), "i"(-EFAULT) \ | |
156 | : "cc") | |
157 | ||
8926d88c AB |
158 | #define put_user(x, ptr) \ |
159 | ({ \ | |
160 | long __pu_err = 0; \ | |
161 | __put_user_check((x), (ptr), __pu_err); \ | |
162 | __pu_err; \ | |
163 | }) | |
7ef39548 ZL |
164 | |
165 | #define __put_user(x, ptr) \ | |
ace02e2b GH |
166 | ({ \ |
167 | long __pu_err = 0; \ | |
8926d88c AB |
168 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
169 | __put_user_err((x), __p, __pu_err); \ | |
ace02e2b GH |
170 | __pu_err; \ |
171 | }) | |
172 | ||
7ef39548 | 173 | #define __put_user_error(x, ptr, err) \ |
ace02e2b | 174 | ({ \ |
487913ab | 175 | __put_user_err((x), (ptr), (err)); \ |
7ef39548 | 176 | (void)0; \ |
ace02e2b GH |
177 | }) |
178 | ||
487913ab ZL |
179 | #define __put_user_check(x, ptr, err) \ |
180 | ({ \ | |
181 | __typeof__(*(ptr)) __user *__p = (ptr); \ | |
182 | might_fault(); \ | |
96d4f267 | 183 | if (access_ok(__p, sizeof(*__p))) { \ |
487913ab ZL |
184 | __put_user_err((x), __p, (err)); \ |
185 | } else { \ | |
186 | (err) = -EFAULT; \ | |
187 | } \ | |
188 | }) | |
189 | ||
7ef39548 | 190 | #define __put_user_err(x, ptr, err) \ |
ace02e2b | 191 | do { \ |
487913ab ZL |
192 | __typeof__(*(ptr)) __pu_val = (x); \ |
193 | __chk_user_ptr(ptr); \ | |
194 | switch (sizeof(*(ptr))) { \ | |
ace02e2b | 195 | case 1: \ |
487913ab | 196 | __put_user_asm("sbi", __pu_val, (ptr), (err)); \ |
ace02e2b GH |
197 | break; \ |
198 | case 2: \ | |
487913ab | 199 | __put_user_asm("shi", __pu_val, (ptr), (err)); \ |
ace02e2b GH |
200 | break; \ |
201 | case 4: \ | |
487913ab | 202 | __put_user_asm("swi", __pu_val, (ptr), (err)); \ |
ace02e2b GH |
203 | break; \ |
204 | case 8: \ | |
487913ab | 205 | __put_user_asm_dword(__pu_val, (ptr), (err)); \ |
ace02e2b GH |
206 | break; \ |
207 | default: \ | |
208 | BUILD_BUG(); \ | |
209 | break; \ | |
210 | } \ | |
211 | } while (0) | |
212 | ||
7ef39548 ZL |
213 | #define __put_user_asm(inst, x, addr, err) \ |
214 | __asm__ __volatile__ ( \ | |
215 | "1: "inst" %1,[%2]\n" \ | |
216 | "2:\n" \ | |
217 | " .section .fixup,\"ax\"\n" \ | |
218 | " .align 2\n" \ | |
219 | "3: move %0, %3\n" \ | |
220 | " b 2b\n" \ | |
221 | " .previous\n" \ | |
222 | " .section __ex_table,\"a\"\n" \ | |
223 | " .align 3\n" \ | |
224 | " .long 1b, 3b\n" \ | |
225 | " .previous" \ | |
226 | : "+r" (err) \ | |
227 | : "r" (x), "r" (addr), "i" (-EFAULT) \ | |
228 | : "cc") | |
ace02e2b GH |
229 | |
230 | #ifdef __NDS32_EB__ | |
231 | #define __pu_reg_oper0 "%H2" | |
232 | #define __pu_reg_oper1 "%L2" | |
233 | #else | |
234 | #define __pu_reg_oper0 "%L2" | |
235 | #define __pu_reg_oper1 "%H2" | |
236 | #endif | |
237 | ||
238 | #define __put_user_asm_dword(x, addr, err) \ | |
7ef39548 ZL |
239 | __asm__ __volatile__ ( \ |
240 | "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \ | |
241 | "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \ | |
242 | "3:\n" \ | |
243 | " .section .fixup,\"ax\"\n" \ | |
244 | " .align 2\n" \ | |
245 | "4: move %0, %3\n" \ | |
246 | " b 3b\n" \ | |
247 | " .previous\n" \ | |
248 | " .section __ex_table,\"a\"\n" \ | |
249 | " .align 3\n" \ | |
250 | " .long 1b, 4b\n" \ | |
251 | " .long 2b, 4b\n" \ | |
252 | " .previous" \ | |
253 | : "+r"(err) \ | |
254 | : "r"(addr), "r"(x), "i"(-EFAULT) \ | |
255 | : "cc") | |
256 | ||
ace02e2b GH |
257 | extern unsigned long __arch_clear_user(void __user * addr, unsigned long n); |
258 | extern long strncpy_from_user(char *dest, const char __user * src, long count); | |
ace02e2b GH |
259 | extern __must_check long strnlen_user(const char __user * str, long n); |
260 | extern unsigned long __arch_copy_from_user(void *to, const void __user * from, | |
261 | unsigned long n); | |
262 | extern unsigned long __arch_copy_to_user(void __user * to, const void *from, | |
263 | unsigned long n); | |
264 | ||
265 | #define raw_copy_from_user __arch_copy_from_user | |
266 | #define raw_copy_to_user __arch_copy_to_user | |
267 | ||
268 | #define INLINE_COPY_FROM_USER | |
269 | #define INLINE_COPY_TO_USER | |
270 | static inline unsigned long clear_user(void __user * to, unsigned long n) | |
271 | { | |
96d4f267 | 272 | if (access_ok(to, n)) |
ace02e2b GH |
273 | n = __arch_clear_user(to, n); |
274 | return n; | |
275 | } | |
276 | ||
277 | static inline unsigned long __clear_user(void __user * to, unsigned long n) | |
278 | { | |
279 | return __arch_clear_user(to, n); | |
280 | } | |
281 | ||
282 | #endif /* _ASMNDS32_UACCESS_H */ |