hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/arch/riscv/include/asm/uaccess.h
....@@ -1,14 +1,6 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 * Copyright (C) 2012 Regents of the University of California
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of the GNU General Public License
6
- * as published by the Free Software Foundation, version 2.
7
- *
8
- * This program is distributed in the hope that it will be useful,
9
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- * GNU General Public License for more details.
124 *
135 * This file was copied from include/asm-generic/uaccess.h
146 */
....@@ -16,13 +8,17 @@
168 #ifndef _ASM_RISCV_UACCESS_H
179 #define _ASM_RISCV_UACCESS_H
1810
11
+#include <asm/pgtable.h> /* for TASK_SIZE */
12
+
1913 /*
2014 * User space memory access functions
2115 */
16
+#ifdef CONFIG_MMU
2217 #include <linux/errno.h>
2318 #include <linux/compiler.h>
2419 #include <linux/thread_info.h>
2520 #include <asm/byteorder.h>
21
+#include <asm/extable.h>
2622 #include <asm/asm.h>
2723
2824 #define __enable_user_access() \
....@@ -30,38 +26,8 @@
3026 #define __disable_user_access() \
3127 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
3228
33
-/*
34
- * The fs value determines whether argument validity checking should be
35
- * performed or not. If get_fs() == USER_DS, checking is performed, with
36
- * get_fs() == KERNEL_DS, checking is bypassed.
37
- *
38
- * For historical reasons, these macros are grossly misnamed.
39
- */
40
-
41
-#define KERNEL_DS (~0UL)
42
-#define USER_DS (TASK_SIZE)
43
-
44
-#define get_ds() (KERNEL_DS)
45
-#define get_fs() (current_thread_info()->addr_limit)
46
-
47
-static inline void set_fs(mm_segment_t fs)
48
-{
49
- current_thread_info()->addr_limit = fs;
50
-}
51
-
52
-#define segment_eq(a, b) ((a) == (b))
53
-
54
-#define user_addr_max() (get_fs())
55
-
56
-
57
-#define VERIFY_READ 0
58
-#define VERIFY_WRITE 1
59
-
6029 /**
6130 * access_ok: - Checks if a user space pointer is valid
62
- * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
63
- * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
64
- * to write to a block, it is always safe to read from it.
6531 * @addr: User space pointer to start of block to check
6632 * @size: Size of block to check
6733 *
....@@ -76,7 +42,7 @@
7642 * checks that the pointer is in the user space range - after calling
7743 * this function, memory access functions may still return -EFAULT.
7844 */
79
-#define access_ok(type, addr, size) ({ \
45
+#define access_ok(addr, size) ({ \
8046 __chk_user_ptr(addr); \
8147 likely(__access_ok((unsigned long __force)(addr), (size))); \
8248 })
....@@ -87,9 +53,7 @@
8753 */
8854 static inline int __access_ok(unsigned long addr, unsigned long size)
8955 {
90
- const mm_segment_t fs = get_fs();
91
-
92
- return (size <= fs) && (addr <= (fs - size));
56
+ return size <= TASK_SIZE && addr <= TASK_SIZE - size;
9357 }
9458
9559 /*
....@@ -105,21 +69,8 @@
10569 * on our cache or tlb entries.
10670 */
10771
108
-struct exception_table_entry {
109
- unsigned long insn, fixup;
110
-};
111
-
112
-extern int fixup_exception(struct pt_regs *state);
113
-
114
-#if defined(__LITTLE_ENDIAN)
115
-#define __MSW 1
11672 #define __LSW 0
117
-#elif defined(__BIG_ENDIAN)
118
-#define __MSW 0
119
-#define __LSW 1
120
-#else
121
-#error "Unknown endianness"
122
-#endif
73
+#define __MSW 1
12374
12475 /*
12576 * The "__xxx" versions of the user access functions do not verify the address
....@@ -131,7 +82,6 @@
13182 do { \
13283 uintptr_t __tmp; \
13384 __typeof__(x) __x; \
134
- __enable_user_access(); \
13585 __asm__ __volatile__ ( \
13686 "1:\n" \
13787 " " insn " %1, %3\n" \
....@@ -149,7 +99,6 @@
14999 " .previous" \
150100 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
151101 : "m" (*(ptr)), "i" (-EFAULT)); \
152
- __disable_user_access(); \
153102 (x) = __x; \
154103 } while (0)
155104
....@@ -162,7 +111,6 @@
162111 u32 __user *__ptr = (u32 __user *)(ptr); \
163112 u32 __lo, __hi; \
164113 uintptr_t __tmp; \
165
- __enable_user_access(); \
166114 __asm__ __volatile__ ( \
167115 "1:\n" \
168116 " lw %1, %4\n" \
....@@ -186,12 +134,30 @@
186134 "=r" (__tmp) \
187135 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
188136 "i" (-EFAULT)); \
189
- __disable_user_access(); \
190137 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
191138 (((u64)__hi << 32) | __lo))); \
192139 } while (0)
193140 #endif /* CONFIG_64BIT */
194141
142
+#define __get_user_nocheck(x, __gu_ptr, __gu_err) \
143
+do { \
144
+ switch (sizeof(*__gu_ptr)) { \
145
+ case 1: \
146
+ __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
147
+ break; \
148
+ case 2: \
149
+ __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
150
+ break; \
151
+ case 4: \
152
+ __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
153
+ break; \
154
+ case 8: \
155
+ __get_user_8((x), __gu_ptr, __gu_err); \
156
+ break; \
157
+ default: \
158
+ BUILD_BUG(); \
159
+ } \
160
+} while (0)
195161
196162 /**
197163 * __get_user: - Get a simple variable from user space, with less checking.
....@@ -215,25 +181,15 @@
215181 */
216182 #define __get_user(x, ptr) \
217183 ({ \
218
- register long __gu_err = 0; \
219184 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
185
+ long __gu_err = 0; \
186
+ \
220187 __chk_user_ptr(__gu_ptr); \
221
- switch (sizeof(*__gu_ptr)) { \
222
- case 1: \
223
- __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
224
- break; \
225
- case 2: \
226
- __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
227
- break; \
228
- case 4: \
229
- __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
230
- break; \
231
- case 8: \
232
- __get_user_8((x), __gu_ptr, __gu_err); \
233
- break; \
234
- default: \
235
- BUILD_BUG(); \
236
- } \
188
+ \
189
+ __enable_user_access(); \
190
+ __get_user_nocheck(x, __gu_ptr, __gu_err); \
191
+ __disable_user_access(); \
192
+ \
237193 __gu_err; \
238194 })
239195
....@@ -258,16 +214,15 @@
258214 ({ \
259215 const __typeof__(*(ptr)) __user *__p = (ptr); \
260216 might_fault(); \
261
- access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
217
+ access_ok(__p, sizeof(*__p)) ? \
262218 __get_user((x), __p) : \
263
- ((x) = 0, -EFAULT); \
219
+ ((x) = (__force __typeof__(x))0, -EFAULT); \
264220 })
265221
266222 #define __put_user_asm(insn, x, ptr, err) \
267223 do { \
268224 uintptr_t __tmp; \
269225 __typeof__(*(ptr)) __x = x; \
270
- __enable_user_access(); \
271226 __asm__ __volatile__ ( \
272227 "1:\n" \
273228 " " insn " %z3, %2\n" \
....@@ -284,7 +239,6 @@
284239 " .previous" \
285240 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
286241 : "rJ" (__x), "i" (-EFAULT)); \
287
- __disable_user_access(); \
288242 } while (0)
289243
290244 #ifdef CONFIG_64BIT
....@@ -296,7 +250,6 @@
296250 u32 __user *__ptr = (u32 __user *)(ptr); \
297251 u64 __x = (__typeof__((x)-(x)))(x); \
298252 uintptr_t __tmp; \
299
- __enable_user_access(); \
300253 __asm__ __volatile__ ( \
301254 "1:\n" \
302255 " sw %z4, %2\n" \
....@@ -318,35 +271,11 @@
318271 "=m" (__ptr[__LSW]), \
319272 "=m" (__ptr[__MSW]) \
320273 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
321
- __disable_user_access(); \
322274 } while (0)
323275 #endif /* CONFIG_64BIT */
324276
325
-
326
-/**
327
- * __put_user: - Write a simple value into user space, with less checking.
328
- * @x: Value to copy to user space.
329
- * @ptr: Destination address, in user space.
330
- *
331
- * Context: User context only. This function may sleep.
332
- *
333
- * This macro copies a single simple value from kernel space to user
334
- * space. It supports simple types like char and int, but not larger
335
- * data types like structures or arrays.
336
- *
337
- * @ptr must have pointer-to-simple-variable type, and @x must be assignable
338
- * to the result of dereferencing @ptr.
339
- *
340
- * Caller must check the pointer with access_ok() before calling this
341
- * function.
342
- *
343
- * Returns zero on success, or -EFAULT on error.
344
- */
345
-#define __put_user(x, ptr) \
346
-({ \
347
- register long __pu_err = 0; \
348
- __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
349
- __chk_user_ptr(__gu_ptr); \
277
+#define __put_user_nocheck(x, __gu_ptr, __pu_err) \
278
+do { \
350279 switch (sizeof(*__gu_ptr)) { \
351280 case 1: \
352281 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
....@@ -363,6 +292,41 @@
363292 default: \
364293 BUILD_BUG(); \
365294 } \
295
+} while (0)
296
+
297
+/**
298
+ * __put_user: - Write a simple value into user space, with less checking.
299
+ * @x: Value to copy to user space.
300
+ * @ptr: Destination address, in user space.
301
+ *
302
+ * Context: User context only. This function may sleep.
303
+ *
304
+ * This macro copies a single simple value from kernel space to user
305
+ * space. It supports simple types like char and int, but not larger
306
+ * data types like structures or arrays.
307
+ *
308
+ * @ptr must have pointer-to-simple-variable type, and @x must be assignable
309
+ * to the result of dereferencing @ptr. The value of @x is copied to avoid
310
+ * re-ordering where @x is evaluated inside the block that enables user-space
311
+ * access (thus bypassing user space protection if @x is a function).
312
+ *
313
+ * Caller must check the pointer with access_ok() before calling this
314
+ * function.
315
+ *
316
+ * Returns zero on success, or -EFAULT on error.
317
+ */
318
+#define __put_user(x, ptr) \
319
+({ \
320
+ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
321
+ __typeof__(*__gu_ptr) __val = (x); \
322
+ long __pu_err = 0; \
323
+ \
324
+ __chk_user_ptr(__gu_ptr); \
325
+ \
326
+ __enable_user_access(); \
327
+ __put_user_nocheck(__val, __gu_ptr, __pu_err); \
328
+ __disable_user_access(); \
329
+ \
366330 __pu_err; \
367331 })
368332
....@@ -386,15 +350,15 @@
386350 ({ \
387351 __typeof__(*(ptr)) __user *__p = (ptr); \
388352 might_fault(); \
389
- access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
353
+ access_ok(__p, sizeof(*__p)) ? \
390354 __put_user((x), __p) : \
391355 -EFAULT; \
392356 })
393357
394358
395
-extern unsigned long __must_check __asm_copy_to_user(void __user *to,
359
+unsigned long __must_check __asm_copy_to_user(void __user *to,
396360 const void *from, unsigned long n);
397
-extern unsigned long __must_check __asm_copy_from_user(void *to,
361
+unsigned long __must_check __asm_copy_from_user(void *to,
398362 const void __user *from, unsigned long n);
399363
400364 static inline unsigned long
....@@ -421,7 +385,7 @@
421385 unsigned long __must_check clear_user(void __user *to, unsigned long n)
422386 {
423387 might_fault();
424
- return access_ok(VERIFY_WRITE, to, n) ?
388
+ return access_ok(to, n) ?
425389 __clear_user(to, n) : n;
426390 }
427391
....@@ -500,4 +464,27 @@
500464 __ret; \
501465 })
502466
467
+#define HAVE_GET_KERNEL_NOFAULT
468
+
469
+#define __get_kernel_nofault(dst, src, type, err_label) \
470
+do { \
471
+ long __kr_err; \
472
+ \
473
+ __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \
474
+ if (unlikely(__kr_err)) \
475
+ goto err_label; \
476
+} while (0)
477
+
478
+#define __put_kernel_nofault(dst, src, type, err_label) \
479
+do { \
480
+ long __kr_err; \
481
+ \
482
+ __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \
483
+ if (unlikely(__kr_err)) \
484
+ goto err_label; \
485
+} while (0)
486
+
487
+#else /* CONFIG_MMU */
488
+#include <asm-generic/uaccess.h>
489
+#endif /* CONFIG_MMU */
503490 #endif /* _ASM_RISCV_UACCESS_H */