1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2005-2017 Andes Technology Corporation
4 #ifndef _ASMANDES_UACCESS_H
5 #define _ASMANDES_UACCESS_H
8 * User space memory access functions
10 #include <linux/sched.h>
11 #include <asm/errno.h>
12 #include <asm/memory.h>
13 #include <asm/types.h>
17 #define VERIFY_WRITE 1
19 #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
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
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.
34 struct exception_table_entry {
35 unsigned long insn, fixup;
38 extern int fixup_exception(struct pt_regs *regs);
40 #define KERNEL_DS ((mm_segment_t) { ~0UL })
41 #define USER_DS ((mm_segment_t) {TASK_SIZE - 1})
43 #define get_ds() (KERNEL_DS)
44 #define get_fs() (current_thread_info()->addr_limit)
45 #define user_addr_max get_fs
47 static inline void set_fs(mm_segment_t fs)
49 current_thread_info()->addr_limit = fs;
52 #define segment_eq(a, b) ((a) == (b))
54 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size))
56 #define access_ok(type, addr, size) \
57 __range_ok((unsigned long)addr, (unsigned long)size)
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
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
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).
78 #define get_user(x, ptr) \
81 __get_user_check((x), (ptr), __gu_err); \
85 #define __get_user_error(x, ptr, err) \
87 __get_user_check((x), (ptr), (err)); \
91 #define __get_user(x, ptr) \
94 const __typeof__(*(ptr)) __user *__p = (ptr); \
95 __get_user_err((x), __p, (__gu_err)); \
99 #define __get_user_check(x, ptr, err) \
101 const __typeof__(*(ptr)) __user *__p = (ptr); \
103 if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \
104 __get_user_err((x), __p, (err)); \
106 (x) = 0; (err) = -EFAULT; \
110 #define __get_user_err(x, ptr, err) \
112 unsigned long __gu_val; \
113 __chk_user_ptr(ptr); \
114 switch (sizeof(*(ptr))) { \
116 __get_user_asm("lbi", __gu_val, (ptr), (err)); \
119 __get_user_asm("lhi", __gu_val, (ptr), (err)); \
122 __get_user_asm("lwi", __gu_val, (ptr), (err)); \
125 __get_user_asm_dword(__gu_val, (ptr), (err)); \
131 (x) = (__force __typeof__(*(ptr)))__gu_val; \
134 #define __get_user_asm(inst, x, addr, err) \
135 __asm__ __volatile__ ( \
136 "1: "inst" %1,[%2]\n" \
138 " .section .fixup,\"ax\"\n" \
144 " .section __ex_table,\"a\"\n" \
148 : "+r" (err), "=&r" (x) \
149 : "r" (addr), "i" (-EFAULT) \
153 #define __gu_reg_oper0 "%H1"
154 #define __gu_reg_oper1 "%L1"
156 #define __gu_reg_oper0 "%L1"
157 #define __gu_reg_oper1 "%H1"
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" \
165 " .section .fixup,\"ax\"\n" \
170 " .section __ex_table,\"a\"\n" \
175 : "+r"(err), "=&r"(x) \
176 : "r"(addr), "i"(-EFAULT) \
179 #define put_user(x, ptr) \
182 __put_user_check((x), (ptr), __pu_err); \
186 #define __put_user(x, ptr) \
189 __typeof__(*(ptr)) __user *__p = (ptr); \
190 __put_user_err((x), __p, __pu_err); \
194 #define __put_user_error(x, ptr, err) \
196 __put_user_err((x), (ptr), (err)); \
200 #define __put_user_check(x, ptr, err) \
202 __typeof__(*(ptr)) __user *__p = (ptr); \
204 if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \
205 __put_user_err((x), __p, (err)); \
211 #define __put_user_err(x, ptr, err) \
213 __typeof__(*(ptr)) __pu_val = (x); \
214 __chk_user_ptr(ptr); \
215 switch (sizeof(*(ptr))) { \
217 __put_user_asm("sbi", __pu_val, (ptr), (err)); \
220 __put_user_asm("shi", __pu_val, (ptr), (err)); \
223 __put_user_asm("swi", __pu_val, (ptr), (err)); \
226 __put_user_asm_dword(__pu_val, (ptr), (err)); \
234 #define __put_user_asm(inst, x, addr, err) \
235 __asm__ __volatile__ ( \
236 "1: "inst" %1,[%2]\n" \
238 " .section .fixup,\"ax\"\n" \
243 " .section __ex_table,\"a\"\n" \
248 : "r" (x), "r" (addr), "i" (-EFAULT) \
252 #define __pu_reg_oper0 "%H2"
253 #define __pu_reg_oper1 "%L2"
255 #define __pu_reg_oper0 "%L2"
256 #define __pu_reg_oper1 "%H2"
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" \
264 " .section .fixup,\"ax\"\n" \
269 " .section __ex_table,\"a\"\n" \
275 : "r"(addr), "r"(x), "i"(-EFAULT) \
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,
284 extern unsigned long __arch_copy_to_user(void __user * to, const void *from,
287 #define raw_copy_from_user __arch_copy_from_user
288 #define raw_copy_to_user __arch_copy_to_user
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)
294 if (access_ok(VERIFY_WRITE, to, n))
295 n = __arch_clear_user(to, n);
299 static inline unsigned long __clear_user(void __user * to, unsigned long n)
301 return __arch_clear_user(to, n);
304 #endif /* _ASMNDS32_UACCESS_H */