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