Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 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 }) |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 41 | #define USER_DS ((mm_segment_t) {TASK_SIZE - 1}) |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 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 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 52 | #define segment_eq(a, b) ((a) == (b)) |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 53 | |
| 54 | #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size)) |
| 55 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 56 | #define access_ok(type, addr, size) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 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 | |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 78 | #define get_user __get_user \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 79 | |
| 80 | #define __get_user(x, ptr) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 81 | ({ \ |
| 82 | long __gu_err = 0; \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 83 | __get_user_check((x), (ptr), __gu_err); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 84 | __gu_err; \ |
| 85 | }) |
| 86 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 87 | #define __get_user_error(x, ptr, err) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 88 | ({ \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 89 | __get_user_check((x), (ptr), (err)); \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 90 | (void)0; \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 91 | }) |
| 92 | |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 93 | #define __get_user_check(x, ptr, err) \ |
| 94 | ({ \ |
| 95 | const __typeof__(*(ptr)) __user *__p = (ptr); \ |
| 96 | might_fault(); \ |
| 97 | if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \ |
| 98 | __get_user_err((x), __p, (err)); \ |
| 99 | } else { \ |
| 100 | (x) = 0; (err) = -EFAULT; \ |
| 101 | } \ |
| 102 | }) |
| 103 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 104 | #define __get_user_err(x, ptr, err) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 105 | do { \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 106 | unsigned long __gu_val; \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 107 | __chk_user_ptr(ptr); \ |
| 108 | switch (sizeof(*(ptr))) { \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 109 | case 1: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 110 | __get_user_asm("lbi", __gu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 111 | break; \ |
| 112 | case 2: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 113 | __get_user_asm("lhi", __gu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 114 | break; \ |
| 115 | case 4: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 116 | __get_user_asm("lwi", __gu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 117 | break; \ |
| 118 | case 8: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 119 | __get_user_asm_dword(__gu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 120 | break; \ |
| 121 | default: \ |
| 122 | BUILD_BUG(); \ |
| 123 | break; \ |
| 124 | } \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 125 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 126 | } while (0) |
| 127 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 128 | #define __get_user_asm(inst, x, addr, err) \ |
| 129 | __asm__ __volatile__ ( \ |
| 130 | "1: "inst" %1,[%2]\n" \ |
| 131 | "2:\n" \ |
| 132 | " .section .fixup,\"ax\"\n" \ |
| 133 | " .align 2\n" \ |
| 134 | "3: move %0, %3\n" \ |
| 135 | " move %1, #0\n" \ |
| 136 | " b 2b\n" \ |
| 137 | " .previous\n" \ |
| 138 | " .section __ex_table,\"a\"\n" \ |
| 139 | " .align 3\n" \ |
| 140 | " .long 1b, 3b\n" \ |
| 141 | " .previous" \ |
| 142 | : "+r" (err), "=&r" (x) \ |
| 143 | : "r" (addr), "i" (-EFAULT) \ |
| 144 | : "cc") |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 145 | |
| 146 | #ifdef __NDS32_EB__ |
| 147 | #define __gu_reg_oper0 "%H1" |
| 148 | #define __gu_reg_oper1 "%L1" |
| 149 | #else |
| 150 | #define __gu_reg_oper0 "%L1" |
| 151 | #define __gu_reg_oper1 "%H1" |
| 152 | #endif |
| 153 | |
| 154 | #define __get_user_asm_dword(x, addr, err) \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 155 | __asm__ __volatile__ ( \ |
| 156 | "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \ |
| 157 | "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \ |
| 158 | "3:\n" \ |
| 159 | " .section .fixup,\"ax\"\n" \ |
| 160 | " .align 2\n" \ |
| 161 | "4: move %0, %3\n" \ |
| 162 | " b 3b\n" \ |
| 163 | " .previous\n" \ |
| 164 | " .section __ex_table,\"a\"\n" \ |
| 165 | " .align 3\n" \ |
| 166 | " .long 1b, 4b\n" \ |
| 167 | " .long 2b, 4b\n" \ |
| 168 | " .previous" \ |
| 169 | : "+r"(err), "=&r"(x) \ |
| 170 | : "r"(addr), "i"(-EFAULT) \ |
| 171 | : "cc") |
| 172 | |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 173 | #define put_user __put_user \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 174 | |
| 175 | #define __put_user(x, ptr) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 176 | ({ \ |
| 177 | long __pu_err = 0; \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 178 | __put_user_err((x), (ptr), __pu_err); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 179 | __pu_err; \ |
| 180 | }) |
| 181 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 182 | #define __put_user_error(x, ptr, err) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 183 | ({ \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 184 | __put_user_err((x), (ptr), (err)); \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 185 | (void)0; \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 186 | }) |
| 187 | |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 188 | #define __put_user_check(x, ptr, err) \ |
| 189 | ({ \ |
| 190 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
| 191 | might_fault(); \ |
| 192 | if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \ |
| 193 | __put_user_err((x), __p, (err)); \ |
| 194 | } else { \ |
| 195 | (err) = -EFAULT; \ |
| 196 | } \ |
| 197 | }) |
| 198 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 199 | #define __put_user_err(x, ptr, err) \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 200 | do { \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 201 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 202 | __chk_user_ptr(ptr); \ |
| 203 | switch (sizeof(*(ptr))) { \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 204 | case 1: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 205 | __put_user_asm("sbi", __pu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 206 | break; \ |
| 207 | case 2: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 208 | __put_user_asm("shi", __pu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 209 | break; \ |
| 210 | case 4: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 211 | __put_user_asm("swi", __pu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 212 | break; \ |
| 213 | case 8: \ |
Zong Li | 487913a | 2018-08-13 16:02:53 +0800 | [diff] [blame] | 214 | __put_user_asm_dword(__pu_val, (ptr), (err)); \ |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 215 | break; \ |
| 216 | default: \ |
| 217 | BUILD_BUG(); \ |
| 218 | break; \ |
| 219 | } \ |
| 220 | } while (0) |
| 221 | |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 222 | #define __put_user_asm(inst, x, addr, err) \ |
| 223 | __asm__ __volatile__ ( \ |
| 224 | "1: "inst" %1,[%2]\n" \ |
| 225 | "2:\n" \ |
| 226 | " .section .fixup,\"ax\"\n" \ |
| 227 | " .align 2\n" \ |
| 228 | "3: move %0, %3\n" \ |
| 229 | " b 2b\n" \ |
| 230 | " .previous\n" \ |
| 231 | " .section __ex_table,\"a\"\n" \ |
| 232 | " .align 3\n" \ |
| 233 | " .long 1b, 3b\n" \ |
| 234 | " .previous" \ |
| 235 | : "+r" (err) \ |
| 236 | : "r" (x), "r" (addr), "i" (-EFAULT) \ |
| 237 | : "cc") |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 238 | |
| 239 | #ifdef __NDS32_EB__ |
| 240 | #define __pu_reg_oper0 "%H2" |
| 241 | #define __pu_reg_oper1 "%L2" |
| 242 | #else |
| 243 | #define __pu_reg_oper0 "%L2" |
| 244 | #define __pu_reg_oper1 "%H2" |
| 245 | #endif |
| 246 | |
| 247 | #define __put_user_asm_dword(x, addr, err) \ |
Zong Li | 7ef3954 | 2018-08-13 15:08:52 +0800 | [diff] [blame] | 248 | __asm__ __volatile__ ( \ |
| 249 | "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \ |
| 250 | "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \ |
| 251 | "3:\n" \ |
| 252 | " .section .fixup,\"ax\"\n" \ |
| 253 | " .align 2\n" \ |
| 254 | "4: move %0, %3\n" \ |
| 255 | " b 3b\n" \ |
| 256 | " .previous\n" \ |
| 257 | " .section __ex_table,\"a\"\n" \ |
| 258 | " .align 3\n" \ |
| 259 | " .long 1b, 4b\n" \ |
| 260 | " .long 2b, 4b\n" \ |
| 261 | " .previous" \ |
| 262 | : "+r"(err) \ |
| 263 | : "r"(addr), "r"(x), "i"(-EFAULT) \ |
| 264 | : "cc") |
| 265 | |
Greentime Hu | ace02e2 | 2017-10-25 14:27:22 +0800 | [diff] [blame] | 266 | extern unsigned long __arch_clear_user(void __user * addr, unsigned long n); |
| 267 | extern long strncpy_from_user(char *dest, const char __user * src, long count); |
| 268 | extern __must_check long strlen_user(const char __user * str); |
| 269 | extern __must_check long strnlen_user(const char __user * str, long n); |
| 270 | extern unsigned long __arch_copy_from_user(void *to, const void __user * from, |
| 271 | unsigned long n); |
| 272 | extern unsigned long __arch_copy_to_user(void __user * to, const void *from, |
| 273 | unsigned long n); |
| 274 | |
| 275 | #define raw_copy_from_user __arch_copy_from_user |
| 276 | #define raw_copy_to_user __arch_copy_to_user |
| 277 | |
| 278 | #define INLINE_COPY_FROM_USER |
| 279 | #define INLINE_COPY_TO_USER |
| 280 | static inline unsigned long clear_user(void __user * to, unsigned long n) |
| 281 | { |
| 282 | if (access_ok(VERIFY_WRITE, to, n)) |
| 283 | n = __arch_clear_user(to, n); |
| 284 | return n; |
| 285 | } |
| 286 | |
| 287 | static inline unsigned long __clear_user(void __user * to, unsigned long n) |
| 288 | { |
| 289 | return __arch_clear_user(to, n); |
| 290 | } |
| 291 | |
| 292 | #endif /* _ASMNDS32_UACCESS_H */ |