1 /* 2 * OpenRISC Linux 3 * 4 * Linux architectural port borrowing liberally from similar works of 5 * others. All original copyrights apply as per the original source 6 * declaration. 7 * 8 * OpenRISC implementation: 9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> 10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se> 11 * et al. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 */ 18 19 #ifndef __ASM_OPENRISC_UACCESS_H 20 #define __ASM_OPENRISC_UACCESS_H 21 22 /* 23 * User space memory access functions 24 */ 25 #include <linux/prefetch.h> 26 #include <linux/string.h> 27 #include <asm/page.h> 28 29 /* 30 * The fs value determines whether argument validity checking should be 31 * performed or not. If get_fs() == USER_DS, checking is performed, with 32 * get_fs() == KERNEL_DS, checking is bypassed. 33 * 34 * For historical reasons, these macros are grossly misnamed. 35 */ 36 37 /* addr_limit is the maximum accessible address for the task. we misuse 38 * the KERNEL_DS and USER_DS values to both assign and compare the 39 * addr_limit values through the equally misnamed get/set_fs macros. 40 * (see above) 41 */ 42 43 #define KERNEL_DS (~0UL) 44 #define get_ds() (KERNEL_DS) 45 46 #define USER_DS (TASK_SIZE) 47 #define get_fs() (current_thread_info()->addr_limit) 48 #define set_fs(x) (current_thread_info()->addr_limit = (x)) 49 50 #define segment_eq(a, b) ((a) == (b)) 51 52 /* Ensure that the range from addr to addr+size is all within the process' 53 * address space 54 */ 55 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size)) 56 57 /* Ensure that addr is below task's addr_limit */ 58 #define __addr_ok(addr) ((unsigned long) addr < get_fs()) 59 60 #define access_ok(type, addr, size) \ 61 __range_ok((unsigned long)addr, (unsigned long)size) 62 63 /* 64 * The exception table consists of pairs of addresses: the first is the 65 * address of an instruction that is allowed to fault, and the second is 66 * the address at which the program should continue. No registers are 67 * modified, so it is entirely up to the continuation code to figure out 68 * what to do. 69 * 70 * All the routines below use bits of fixup code that are out of line 71 * with the main instruction path. This means when everything is well, 72 * we don't even have to jump over them. Further, they do not intrude 73 * on our cache or tlb entries. 74 */ 75 76 struct exception_table_entry { 77 unsigned long insn, fixup; 78 }; 79 80 /* 81 * These are the main single-value transfer routines. They automatically 82 * use the right size if we just have the right pointer type. 83 * 84 * This gets kind of ugly. We want to return _two_ values in "get_user()" 85 * and yet we don't want to do any pointers, because that is too much 86 * of a performance impact. Thus we have a few rather ugly macros here, 87 * and hide all the uglyness from the user. 88 * 89 * The "__xxx" versions of the user access functions are versions that 90 * do not verify the address space, that must have been done previously 91 * with a separate "access_ok()" call (this is used when we do multiple 92 * accesses to the same area of user memory). 93 * 94 * As we use the same address space for kernel and user data on the 95 * PowerPC, we can just do these as direct assignments. (Of course, the 96 * exception handling means that it's no longer "just"...) 97 */ 98 #define get_user(x, ptr) \ 99 __get_user_check((x), (ptr), sizeof(*(ptr))) 100 #define put_user(x, ptr) \ 101 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 102 103 #define __get_user(x, ptr) \ 104 __get_user_nocheck((x), (ptr), sizeof(*(ptr))) 105 #define __put_user(x, ptr) \ 106 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 107 108 extern long __put_user_bad(void); 109 110 #define __put_user_nocheck(x, ptr, size) \ 111 ({ \ 112 long __pu_err; \ 113 __put_user_size((x), (ptr), (size), __pu_err); \ 114 __pu_err; \ 115 }) 116 117 #define __put_user_check(x, ptr, size) \ 118 ({ \ 119 long __pu_err = -EFAULT; \ 120 __typeof__(*(ptr)) *__pu_addr = (ptr); \ 121 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \ 122 __put_user_size((x), __pu_addr, (size), __pu_err); \ 123 __pu_err; \ 124 }) 125 126 #define __put_user_size(x, ptr, size, retval) \ 127 do { \ 128 retval = 0; \ 129 switch (size) { \ 130 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \ 131 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \ 132 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \ 133 case 8: __put_user_asm2(x, ptr, retval); break; \ 134 default: __put_user_bad(); \ 135 } \ 136 } while (0) 137 138 struct __large_struct { 139 unsigned long buf[100]; 140 }; 141 #define __m(x) (*(struct __large_struct *)(x)) 142 143 /* 144 * We don't tell gcc that we are accessing memory, but this is OK 145 * because we do not write to any memory gcc knows about, so there 146 * are no aliasing issues. 147 */ 148 #define __put_user_asm(x, addr, err, op) \ 149 __asm__ __volatile__( \ 150 "1: "op" 0(%2),%1\n" \ 151 "2:\n" \ 152 ".section .fixup,\"ax\"\n" \ 153 "3: l.addi %0,r0,%3\n" \ 154 " l.j 2b\n" \ 155 " l.nop\n" \ 156 ".previous\n" \ 157 ".section __ex_table,\"a\"\n" \ 158 " .align 2\n" \ 159 " .long 1b,3b\n" \ 160 ".previous" \ 161 : "=r"(err) \ 162 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err)) 163 164 #define __put_user_asm2(x, addr, err) \ 165 __asm__ __volatile__( \ 166 "1: l.sw 0(%2),%1\n" \ 167 "2: l.sw 4(%2),%H1\n" \ 168 "3:\n" \ 169 ".section .fixup,\"ax\"\n" \ 170 "4: l.addi %0,r0,%3\n" \ 171 " l.j 3b\n" \ 172 " l.nop\n" \ 173 ".previous\n" \ 174 ".section __ex_table,\"a\"\n" \ 175 " .align 2\n" \ 176 " .long 1b,4b\n" \ 177 " .long 2b,4b\n" \ 178 ".previous" \ 179 : "=r"(err) \ 180 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err)) 181 182 #define __get_user_nocheck(x, ptr, size) \ 183 ({ \ 184 long __gu_err, __gu_val; \ 185 __get_user_size(__gu_val, (ptr), (size), __gu_err); \ 186 (x) = (__force __typeof__(*(ptr)))__gu_val; \ 187 __gu_err; \ 188 }) 189 190 #define __get_user_check(x, ptr, size) \ 191 ({ \ 192 long __gu_err = -EFAULT, __gu_val = 0; \ 193 const __typeof__(*(ptr)) * __gu_addr = (ptr); \ 194 if (access_ok(VERIFY_READ, __gu_addr, size)) \ 195 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \ 196 (x) = (__force __typeof__(*(ptr)))__gu_val; \ 197 __gu_err; \ 198 }) 199 200 extern long __get_user_bad(void); 201 202 #define __get_user_size(x, ptr, size, retval) \ 203 do { \ 204 retval = 0; \ 205 switch (size) { \ 206 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \ 207 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \ 208 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \ 209 case 8: __get_user_asm2(x, ptr, retval); \ 210 default: (x) = __get_user_bad(); \ 211 } \ 212 } while (0) 213 214 #define __get_user_asm(x, addr, err, op) \ 215 __asm__ __volatile__( \ 216 "1: "op" %1,0(%2)\n" \ 217 "2:\n" \ 218 ".section .fixup,\"ax\"\n" \ 219 "3: l.addi %0,r0,%3\n" \ 220 " l.addi %1,r0,0\n" \ 221 " l.j 2b\n" \ 222 " l.nop\n" \ 223 ".previous\n" \ 224 ".section __ex_table,\"a\"\n" \ 225 " .align 2\n" \ 226 " .long 1b,3b\n" \ 227 ".previous" \ 228 : "=r"(err), "=r"(x) \ 229 : "r"(addr), "i"(-EFAULT), "0"(err)) 230 231 #define __get_user_asm2(x, addr, err) \ 232 __asm__ __volatile__( \ 233 "1: l.lwz %1,0(%2)\n" \ 234 "2: l.lwz %H1,4(%2)\n" \ 235 "3:\n" \ 236 ".section .fixup,\"ax\"\n" \ 237 "4: l.addi %0,r0,%3\n" \ 238 " l.addi %1,r0,0\n" \ 239 " l.addi %H1,r0,0\n" \ 240 " l.j 3b\n" \ 241 " l.nop\n" \ 242 ".previous\n" \ 243 ".section __ex_table,\"a\"\n" \ 244 " .align 2\n" \ 245 " .long 1b,4b\n" \ 246 " .long 2b,4b\n" \ 247 ".previous" \ 248 : "=r"(err), "=&r"(x) \ 249 : "r"(addr), "i"(-EFAULT), "0"(err)) 250 251 /* more complex routines */ 252 253 extern unsigned long __must_check 254 __copy_tofrom_user(void *to, const void *from, unsigned long size); 255 256 #define __copy_from_user(to, from, size) \ 257 __copy_tofrom_user(to, from, size) 258 #define __copy_to_user(to, from, size) \ 259 __copy_tofrom_user(to, from, size) 260 261 #define __copy_to_user_inatomic __copy_to_user 262 #define __copy_from_user_inatomic __copy_from_user 263 264 static inline unsigned long 265 copy_from_user(void *to, const void *from, unsigned long n) 266 { 267 unsigned long res = n; 268 269 if (likely(access_ok(VERIFY_READ, from, n))) 270 res = __copy_tofrom_user(to, from, n); 271 if (unlikely(res)) 272 memset(to + (n - res), 0, res); 273 return res; 274 } 275 276 static inline unsigned long 277 copy_to_user(void *to, const void *from, unsigned long n) 278 { 279 if (likely(access_ok(VERIFY_WRITE, to, n))) 280 n = __copy_tofrom_user(to, from, n); 281 return n; 282 } 283 284 extern unsigned long __clear_user(void *addr, unsigned long size); 285 286 static inline __must_check unsigned long 287 clear_user(void *addr, unsigned long size) 288 { 289 if (likely(access_ok(VERIFY_WRITE, addr, size))) 290 size = __clear_user(addr, size); 291 return size; 292 } 293 294 #define user_addr_max() \ 295 (uaccess_kernel() ? ~0UL : TASK_SIZE) 296 297 extern long strncpy_from_user(char *dest, const char __user *src, long count); 298 299 extern __must_check long strlen_user(const char __user *str); 300 extern __must_check long strnlen_user(const char __user *str, long n); 301 302 #endif /* __ASM_OPENRISC_UACCESS_H */ 303