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>
16 #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
19 * The exception table consists of pairs of addresses: the first is the
20 * address of an instruction that is allowed to fault, and the second is
21 * the address at which the program should continue. No registers are
22 * modified, so it is entirely up to the continuation code to figure out
25 * All the routines below use bits of fixup code that are out of line
26 * with the main instruction path. This means when everything is well,
27 * we don't even have to jump over them. Further, they do not intrude
28 * on our cache or tlb entries.
31 struct exception_table_entry {
32 unsigned long insn, fixup;
35 extern int fixup_exception(struct pt_regs *regs);
37 #define KERNEL_DS ((mm_segment_t) { ~0UL })
38 #define USER_DS ((mm_segment_t) {TASK_SIZE - 1})
40 #define get_fs() (current_thread_info()->addr_limit)
41 #define user_addr_max get_fs
43 static inline void set_fs(mm_segment_t fs)
45 current_thread_info()->addr_limit = fs;
48 #define segment_eq(a, b) ((a) == (b))
50 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size))
52 #define access_ok(addr, size) \
53 __range_ok((unsigned long)addr, (unsigned long)size)
55 * Single-value transfer routines. They automatically use the right
56 * size if we just have the right pointer type. Note that the functions
57 * which read from user space (*get_*) need to take care not to leak
58 * kernel data even if the calling code is buggy and fails to check
59 * the return value. This means zeroing out the destination variable
60 * or buffer on error. Normally this is done out of line by the
61 * fixup code, but there are a few places where it intrudes on the
62 * main code path. When we only write to user space, there is no
65 * The "__xxx" versions of the user access functions do not verify the
66 * address space - it must have been done previously with a separate
69 * The "xxx_error" versions set the third argument to EFAULT if an
70 * error occurs, and leave it unchanged on success. Note that these
71 * versions are void (ie, don't return a value as such).
74 #define get_user __get_user \
76 #define __get_user(x, ptr) \
79 __get_user_check((x), (ptr), __gu_err); \
83 #define __get_user_error(x, ptr, err) \
85 __get_user_check((x), (ptr), (err)); \
89 #define __get_user_check(x, ptr, err) \
91 const __typeof__(*(ptr)) __user *__p = (ptr); \
93 if (access_ok(__p, sizeof(*__p))) { \
94 __get_user_err((x), __p, (err)); \
96 (x) = 0; (err) = -EFAULT; \
100 #define __get_user_err(x, ptr, err) \
102 unsigned long __gu_val; \
103 __chk_user_ptr(ptr); \
104 switch (sizeof(*(ptr))) { \
106 __get_user_asm("lbi", __gu_val, (ptr), (err)); \
109 __get_user_asm("lhi", __gu_val, (ptr), (err)); \
112 __get_user_asm("lwi", __gu_val, (ptr), (err)); \
115 __get_user_asm_dword(__gu_val, (ptr), (err)); \
121 (x) = (__force __typeof__(*(ptr)))__gu_val; \
124 #define __get_user_asm(inst, x, addr, err) \
125 __asm__ __volatile__ ( \
126 "1: "inst" %1,[%2]\n" \
128 " .section .fixup,\"ax\"\n" \
134 " .section __ex_table,\"a\"\n" \
138 : "+r" (err), "=&r" (x) \
139 : "r" (addr), "i" (-EFAULT) \
143 #define __gu_reg_oper0 "%H1"
144 #define __gu_reg_oper1 "%L1"
146 #define __gu_reg_oper0 "%L1"
147 #define __gu_reg_oper1 "%H1"
150 #define __get_user_asm_dword(x, addr, err) \
151 __asm__ __volatile__ ( \
152 "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \
153 "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \
155 " .section .fixup,\"ax\"\n" \
160 " .section __ex_table,\"a\"\n" \
165 : "+r"(err), "=&r"(x) \
166 : "r"(addr), "i"(-EFAULT) \
169 #define put_user __put_user \
171 #define __put_user(x, ptr) \
174 __put_user_err((x), (ptr), __pu_err); \
178 #define __put_user_error(x, ptr, err) \
180 __put_user_err((x), (ptr), (err)); \
184 #define __put_user_check(x, ptr, err) \
186 __typeof__(*(ptr)) __user *__p = (ptr); \
188 if (access_ok(__p, sizeof(*__p))) { \
189 __put_user_err((x), __p, (err)); \
195 #define __put_user_err(x, ptr, err) \
197 __typeof__(*(ptr)) __pu_val = (x); \
198 __chk_user_ptr(ptr); \
199 switch (sizeof(*(ptr))) { \
201 __put_user_asm("sbi", __pu_val, (ptr), (err)); \
204 __put_user_asm("shi", __pu_val, (ptr), (err)); \
207 __put_user_asm("swi", __pu_val, (ptr), (err)); \
210 __put_user_asm_dword(__pu_val, (ptr), (err)); \
218 #define __put_user_asm(inst, x, addr, err) \
219 __asm__ __volatile__ ( \
220 "1: "inst" %1,[%2]\n" \
222 " .section .fixup,\"ax\"\n" \
227 " .section __ex_table,\"a\"\n" \
232 : "r" (x), "r" (addr), "i" (-EFAULT) \
236 #define __pu_reg_oper0 "%H2"
237 #define __pu_reg_oper1 "%L2"
239 #define __pu_reg_oper0 "%L2"
240 #define __pu_reg_oper1 "%H2"
243 #define __put_user_asm_dword(x, addr, err) \
244 __asm__ __volatile__ ( \
245 "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \
246 "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \
248 " .section .fixup,\"ax\"\n" \
253 " .section __ex_table,\"a\"\n" \
259 : "r"(addr), "r"(x), "i"(-EFAULT) \
262 extern unsigned long __arch_clear_user(void __user * addr, unsigned long n);
263 extern long strncpy_from_user(char *dest, const char __user * src, long count);
264 extern __must_check long strlen_user(const char __user * str);
265 extern __must_check long strnlen_user(const char __user * str, long n);
266 extern unsigned long __arch_copy_from_user(void *to, const void __user * from,
268 extern unsigned long __arch_copy_to_user(void __user * to, const void *from,
271 #define raw_copy_from_user __arch_copy_from_user
272 #define raw_copy_to_user __arch_copy_to_user
274 #define INLINE_COPY_FROM_USER
275 #define INLINE_COPY_TO_USER
276 static inline unsigned long clear_user(void __user * to, unsigned long n)
278 if (access_ok(to, n))
279 n = __arch_clear_user(to, n);
283 static inline unsigned long __clear_user(void __user * to, unsigned long n)
285 return __arch_clear_user(to, n);
288 #endif /* _ASMNDS32_UACCESS_H */