1
2
3
4
5
6
7
8#ifndef _ASM_RISCV_UACCESS_H
9#define _ASM_RISCV_UACCESS_H
10
11
12
13
14
15extern unsigned long __must_check __asm_copy_to_user(void __user *to,
16 const void *from, unsigned long n);
17extern unsigned long __must_check __asm_copy_from_user(void *to,
18 const void __user *from, unsigned long n);
19
20static inline unsigned long
21raw_copy_from_user(void *to, const void __user *from, unsigned long n)
22{
23 return __asm_copy_from_user(to, from, n);
24}
25
26static inline unsigned long
27raw_copy_to_user(void __user *to, const void *from, unsigned long n)
28{
29 return __asm_copy_to_user(to, from, n);
30}
31
32#ifdef CONFIG_MMU
33#include <linux/errno.h>
34#include <linux/compiler.h>
35#include <linux/thread_info.h>
36#include <asm/byteorder.h>
37#include <asm/extable.h>
38#include <asm/asm.h>
39
40#define __enable_user_access() \
41 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
42#define __disable_user_access() \
43 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
44
45
46
47
48
49
50
51
52
53#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
54
55#define KERNEL_DS MAKE_MM_SEG(~0UL)
56#define USER_DS MAKE_MM_SEG(TASK_SIZE)
57
58#define get_fs() (current_thread_info()->addr_limit)
59
60static inline void set_fs(mm_segment_t fs)
61{
62 current_thread_info()->addr_limit = fs;
63}
64
65#define segment_eq(a, b) ((a).seg == (b).seg)
66
67#define user_addr_max() (get_fs().seg)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86#define access_ok(addr, size) ({ \
87 __chk_user_ptr(addr); \
88 likely(__access_ok((unsigned long __force)(addr), (size))); \
89})
90
91
92
93
94
95static inline int __access_ok(unsigned long addr, unsigned long size)
96{
97 const mm_segment_t fs = get_fs();
98
99 return size <= fs.seg && addr <= fs.seg - size;
100}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115#define __LSW 0
116#define __MSW 1
117
118
119
120
121
122
123
124#define __get_user_asm(insn, x, ptr, err) \
125do { \
126 uintptr_t __tmp; \
127 __typeof__(x) __x; \
128 __enable_user_access(); \
129 __asm__ __volatile__ ( \
130 "1:\n" \
131 " " insn " %1, %3\n" \
132 "2:\n" \
133 " .section .fixup,\"ax\"\n" \
134 " .balign 4\n" \
135 "3:\n" \
136 " li %0, %4\n" \
137 " li %1, 0\n" \
138 " jump 2b, %2\n" \
139 " .previous\n" \
140 " .section __ex_table,\"a\"\n" \
141 " .balign " RISCV_SZPTR "\n" \
142 " " RISCV_PTR " 1b, 3b\n" \
143 " .previous" \
144 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
145 : "m" (*(ptr)), "i" (-EFAULT)); \
146 __disable_user_access(); \
147 (x) = __x; \
148} while (0)
149
150#ifdef CONFIG_64BIT
151#define __get_user_8(x, ptr, err) \
152 __get_user_asm("ld", x, ptr, err)
153#else
154#define __get_user_8(x, ptr, err) \
155do { \
156 u32 __user *__ptr = (u32 __user *)(ptr); \
157 u32 __lo, __hi; \
158 uintptr_t __tmp; \
159 __enable_user_access(); \
160 __asm__ __volatile__ ( \
161 "1:\n" \
162 " lw %1, %4\n" \
163 "2:\n" \
164 " lw %2, %5\n" \
165 "3:\n" \
166 " .section .fixup,\"ax\"\n" \
167 " .balign 4\n" \
168 "4:\n" \
169 " li %0, %6\n" \
170 " li %1, 0\n" \
171 " li %2, 0\n" \
172 " jump 3b, %3\n" \
173 " .previous\n" \
174 " .section __ex_table,\"a\"\n" \
175 " .balign " RISCV_SZPTR "\n" \
176 " " RISCV_PTR " 1b, 4b\n" \
177 " " RISCV_PTR " 2b, 4b\n" \
178 " .previous" \
179 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
180 "=r" (__tmp) \
181 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
182 "i" (-EFAULT)); \
183 __disable_user_access(); \
184 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
185 (((u64)__hi << 32) | __lo))); \
186} while (0)
187#endif
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210#define __get_user(x, ptr) \
211({ \
212 register long __gu_err = 0; \
213 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
214 __chk_user_ptr(__gu_ptr); \
215 switch (sizeof(*__gu_ptr)) { \
216 case 1: \
217 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
218 break; \
219 case 2: \
220 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
221 break; \
222 case 4: \
223 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
224 break; \
225 case 8: \
226 __get_user_8((x), __gu_ptr, __gu_err); \
227 break; \
228 default: \
229 BUILD_BUG(); \
230 } \
231 __gu_err; \
232})
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251#define get_user(x, ptr) \
252({ \
253 const __typeof__(*(ptr)) __user *__p = (ptr); \
254 might_fault(); \
255 access_ok(__p, sizeof(*__p)) ? \
256 __get_user((x), __p) : \
257 ((x) = 0, -EFAULT); \
258})
259
260#define __put_user_asm(insn, x, ptr, err) \
261do { \
262 uintptr_t __tmp; \
263 __typeof__(*(ptr)) __x = x; \
264 __enable_user_access(); \
265 __asm__ __volatile__ ( \
266 "1:\n" \
267 " " insn " %z3, %2\n" \
268 "2:\n" \
269 " .section .fixup,\"ax\"\n" \
270 " .balign 4\n" \
271 "3:\n" \
272 " li %0, %4\n" \
273 " jump 2b, %1\n" \
274 " .previous\n" \
275 " .section __ex_table,\"a\"\n" \
276 " .balign " RISCV_SZPTR "\n" \
277 " " RISCV_PTR " 1b, 3b\n" \
278 " .previous" \
279 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
280 : "rJ" (__x), "i" (-EFAULT)); \
281 __disable_user_access(); \
282} while (0)
283
284#ifdef CONFIG_64BIT
285#define __put_user_8(x, ptr, err) \
286 __put_user_asm("sd", x, ptr, err)
287#else
288#define __put_user_8(x, ptr, err) \
289do { \
290 u32 __user *__ptr = (u32 __user *)(ptr); \
291 u64 __x = (__typeof__((x)-(x)))(x); \
292 uintptr_t __tmp; \
293 __enable_user_access(); \
294 __asm__ __volatile__ ( \
295 "1:\n" \
296 " sw %z4, %2\n" \
297 "2:\n" \
298 " sw %z5, %3\n" \
299 "3:\n" \
300 " .section .fixup,\"ax\"\n" \
301 " .balign 4\n" \
302 "4:\n" \
303 " li %0, %6\n" \
304 " jump 3b, %1\n" \
305 " .previous\n" \
306 " .section __ex_table,\"a\"\n" \
307 " .balign " RISCV_SZPTR "\n" \
308 " " RISCV_PTR " 1b, 4b\n" \
309 " " RISCV_PTR " 2b, 4b\n" \
310 " .previous" \
311 : "+r" (err), "=r" (__tmp), \
312 "=m" (__ptr[__LSW]), \
313 "=m" (__ptr[__MSW]) \
314 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
315 __disable_user_access(); \
316} while (0)
317#endif
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339#define __put_user(x, ptr) \
340({ \
341 register long __pu_err = 0; \
342 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
343 __chk_user_ptr(__gu_ptr); \
344 switch (sizeof(*__gu_ptr)) { \
345 case 1: \
346 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
347 break; \
348 case 2: \
349 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
350 break; \
351 case 4: \
352 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
353 break; \
354 case 8: \
355 __put_user_8((x), __gu_ptr, __pu_err); \
356 break; \
357 default: \
358 BUILD_BUG(); \
359 } \
360 __pu_err; \
361})
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379#define put_user(x, ptr) \
380({ \
381 __typeof__(*(ptr)) __user *__p = (ptr); \
382 might_fault(); \
383 access_ok(__p, sizeof(*__p)) ? \
384 __put_user((x), __p) : \
385 -EFAULT; \
386})
387
388extern long strncpy_from_user(char *dest, const char __user *src, long count);
389
390extern long __must_check strlen_user(const char __user *str);
391extern long __must_check strnlen_user(const char __user *str, long n);
392
393extern
394unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
395
396static inline
397unsigned long __must_check clear_user(void __user *to, unsigned long n)
398{
399 might_fault();
400 return access_ok(to, n) ?
401 __clear_user(to, n) : n;
402}
403
404
405
406
407
408
409#define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
410({ \
411 __typeof__(ptr) __ptr = (ptr); \
412 __typeof__(*(ptr)) __old = (old); \
413 __typeof__(*(ptr)) __new = (new); \
414 __typeof__(*(ptr)) __ret; \
415 __typeof__(err) __err = 0; \
416 register unsigned int __rc; \
417 __enable_user_access(); \
418 switch (size) { \
419 case 4: \
420 __asm__ __volatile__ ( \
421 "0:\n" \
422 " lr.w" #scb " %[ret], %[ptr]\n" \
423 " bne %[ret], %z[old], 1f\n" \
424 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
425 " bnez %[rc], 0b\n" \
426 "1:\n" \
427 ".section .fixup,\"ax\"\n" \
428 ".balign 4\n" \
429 "2:\n" \
430 " li %[err], %[efault]\n" \
431 " jump 1b, %[rc]\n" \
432 ".previous\n" \
433 ".section __ex_table,\"a\"\n" \
434 ".balign " RISCV_SZPTR "\n" \
435 " " RISCV_PTR " 1b, 2b\n" \
436 ".previous\n" \
437 : [ret] "=&r" (__ret), \
438 [rc] "=&r" (__rc), \
439 [ptr] "+A" (*__ptr), \
440 [err] "=&r" (__err) \
441 : [old] "rJ" (__old), \
442 [new] "rJ" (__new), \
443 [efault] "i" (-EFAULT)); \
444 break; \
445 case 8: \
446 __asm__ __volatile__ ( \
447 "0:\n" \
448 " lr.d" #scb " %[ret], %[ptr]\n" \
449 " bne %[ret], %z[old], 1f\n" \
450 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
451 " bnez %[rc], 0b\n" \
452 "1:\n" \
453 ".section .fixup,\"ax\"\n" \
454 ".balign 4\n" \
455 "2:\n" \
456 " li %[err], %[efault]\n" \
457 " jump 1b, %[rc]\n" \
458 ".previous\n" \
459 ".section __ex_table,\"a\"\n" \
460 ".balign " RISCV_SZPTR "\n" \
461 " " RISCV_PTR " 1b, 2b\n" \
462 ".previous\n" \
463 : [ret] "=&r" (__ret), \
464 [rc] "=&r" (__rc), \
465 [ptr] "+A" (*__ptr), \
466 [err] "=&r" (__err) \
467 : [old] "rJ" (__old), \
468 [new] "rJ" (__new), \
469 [efault] "i" (-EFAULT)); \
470 break; \
471 default: \
472 BUILD_BUG(); \
473 } \
474 __disable_user_access(); \
475 (err) = __err; \
476 __ret; \
477})
478
479#else
480#include <asm-generic/uaccess.h>
481#endif
482#endif
483