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