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