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 #include <asm/extable.h> 29 30 /* 31 * The fs value determines whether argument validity checking should be 32 * performed or not. If get_fs() == USER_DS, checking is performed, with 33 * get_fs() == KERNEL_DS, checking is bypassed. 34 * 35 * For historical reasons, these macros are grossly misnamed. 36 */ 37 38 /* addr_limit is the maximum accessible address for the task. we misuse 39 * the KERNEL_DS and USER_DS values to both assign and compare the 40 * addr_limit values through the equally misnamed get/set_fs macros. 41 * (see above) 42 */ 43 44 #define KERNEL_DS (~0UL) 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(addr, size) \ 61 ({ \ 62 unsigned long __ao_addr = (unsigned long)(addr); \ 63 unsigned long __ao_size = (unsigned long)(size); \ 64 __range_ok(__ao_addr, __ao_size); \ 65 }) 66 67 /* 68 * These are the main single-value transfer routines. They automatically 69 * use the right size if we just have the right pointer type. 70 * 71 * This gets kind of ugly. We want to return _two_ values in "get_user()" 72 * and yet we don't want to do any pointers, because that is too much 73 * of a performance impact. Thus we have a few rather ugly macros here, 74 * and hide all the uglyness from the user. 75 * 76 * The "__xxx" versions of the user access functions are versions that 77 * do not verify the address space, that must have been done previously 78 * with a separate "access_ok()" call (this is used when we do multiple 79 * accesses to the same area of user memory). 80 * 81 * As we use the same address space for kernel and user data on the 82 * PowerPC, we can just do these as direct assignments. (Of course, the 83 * exception handling means that it's no longer "just"...) 84 */ 85 #define get_user(x, ptr) \ 86 __get_user_check((x), (ptr), sizeof(*(ptr))) 87 #define put_user(x, ptr) \ 88 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 89 90 #define __get_user(x, ptr) \ 91 __get_user_nocheck((x), (ptr), sizeof(*(ptr))) 92 #define __put_user(x, ptr) \ 93 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 94 95 extern long __put_user_bad(void); 96 97 #define __put_user_nocheck(x, ptr, size) \ 98 ({ \ 99 long __pu_err; \ 100 __put_user_size((x), (ptr), (size), __pu_err); \ 101 __pu_err; \ 102 }) 103 104 #define __put_user_check(x, ptr, size) \ 105 ({ \ 106 long __pu_err = -EFAULT; \ 107 __typeof__(*(ptr)) *__pu_addr = (ptr); \ 108 if (access_ok(__pu_addr, size)) \ 109 __put_user_size((x), __pu_addr, (size), __pu_err); \ 110 __pu_err; \ 111 }) 112 113 #define __put_user_size(x, ptr, size, retval) \ 114 do { \ 115 retval = 0; \ 116 switch (size) { \ 117 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \ 118 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \ 119 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \ 120 case 8: __put_user_asm2(x, ptr, retval); break; \ 121 default: __put_user_bad(); \ 122 } \ 123 } while (0) 124 125 struct __large_struct { 126 unsigned long buf[100]; 127 }; 128 #define __m(x) (*(struct __large_struct *)(x)) 129 130 /* 131 * We don't tell gcc that we are accessing memory, but this is OK 132 * because we do not write to any memory gcc knows about, so there 133 * are no aliasing issues. 134 */ 135 #define __put_user_asm(x, addr, err, op) \ 136 __asm__ __volatile__( \ 137 "1: "op" 0(%2),%1\n" \ 138 "2:\n" \ 139 ".section .fixup,\"ax\"\n" \ 140 "3: l.addi %0,r0,%3\n" \ 141 " l.j 2b\n" \ 142 " l.nop\n" \ 143 ".previous\n" \ 144 ".section __ex_table,\"a\"\n" \ 145 " .align 2\n" \ 146 " .long 1b,3b\n" \ 147 ".previous" \ 148 : "=r"(err) \ 149 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err)) 150 151 #define __put_user_asm2(x, addr, err) \ 152 __asm__ __volatile__( \ 153 "1: l.sw 0(%2),%1\n" \ 154 "2: l.sw 4(%2),%H1\n" \ 155 "3:\n" \ 156 ".section .fixup,\"ax\"\n" \ 157 "4: l.addi %0,r0,%3\n" \ 158 " l.j 3b\n" \ 159 " l.nop\n" \ 160 ".previous\n" \ 161 ".section __ex_table,\"a\"\n" \ 162 " .align 2\n" \ 163 " .long 1b,4b\n" \ 164 " .long 2b,4b\n" \ 165 ".previous" \ 166 : "=r"(err) \ 167 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err)) 168 169 #define __get_user_nocheck(x, ptr, size) \ 170 ({ \ 171 long __gu_err, __gu_val; \ 172 __get_user_size(__gu_val, (ptr), (size), __gu_err); \ 173 (x) = (__force __typeof__(*(ptr)))__gu_val; \ 174 __gu_err; \ 175 }) 176 177 #define __get_user_check(x, ptr, size) \ 178 ({ \ 179 long __gu_err = -EFAULT, __gu_val = 0; \ 180 const __typeof__(*(ptr)) * __gu_addr = (ptr); \ 181 if (access_ok(__gu_addr, size)) \ 182 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \ 183 (x) = (__force __typeof__(*(ptr)))__gu_val; \ 184 __gu_err; \ 185 }) 186 187 extern long __get_user_bad(void); 188 189 #define __get_user_size(x, ptr, size, retval) \ 190 do { \ 191 retval = 0; \ 192 switch (size) { \ 193 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \ 194 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \ 195 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \ 196 case 8: __get_user_asm2(x, ptr, retval); break; \ 197 default: (x) = __get_user_bad(); \ 198 } \ 199 } while (0) 200 201 #define __get_user_asm(x, addr, err, op) \ 202 __asm__ __volatile__( \ 203 "1: "op" %1,0(%2)\n" \ 204 "2:\n" \ 205 ".section .fixup,\"ax\"\n" \ 206 "3: l.addi %0,r0,%3\n" \ 207 " l.addi %1,r0,0\n" \ 208 " l.j 2b\n" \ 209 " l.nop\n" \ 210 ".previous\n" \ 211 ".section __ex_table,\"a\"\n" \ 212 " .align 2\n" \ 213 " .long 1b,3b\n" \ 214 ".previous" \ 215 : "=r"(err), "=r"(x) \ 216 : "r"(addr), "i"(-EFAULT), "0"(err)) 217 218 #define __get_user_asm2(x, addr, err) \ 219 __asm__ __volatile__( \ 220 "1: l.lwz %1,0(%2)\n" \ 221 "2: l.lwz %H1,4(%2)\n" \ 222 "3:\n" \ 223 ".section .fixup,\"ax\"\n" \ 224 "4: l.addi %0,r0,%3\n" \ 225 " l.addi %1,r0,0\n" \ 226 " l.addi %H1,r0,0\n" \ 227 " l.j 3b\n" \ 228 " l.nop\n" \ 229 ".previous\n" \ 230 ".section __ex_table,\"a\"\n" \ 231 " .align 2\n" \ 232 " .long 1b,4b\n" \ 233 " .long 2b,4b\n" \ 234 ".previous" \ 235 : "=r"(err), "=&r"(x) \ 236 : "r"(addr), "i"(-EFAULT), "0"(err)) 237 238 /* more complex routines */ 239 240 extern unsigned long __must_check 241 __copy_tofrom_user(void *to, const void *from, unsigned long size); 242 static inline unsigned long 243 raw_copy_from_user(void *to, const void __user *from, unsigned long size) 244 { 245 return __copy_tofrom_user(to, (__force const void *)from, size); 246 } 247 static inline unsigned long 248 raw_copy_to_user(void *to, const void __user *from, unsigned long size) 249 { 250 return __copy_tofrom_user((__force void *)to, from, size); 251 } 252 #define INLINE_COPY_FROM_USER 253 #define INLINE_COPY_TO_USER 254 255 extern unsigned long __clear_user(void *addr, unsigned long size); 256 257 static inline __must_check unsigned long 258 clear_user(void *addr, unsigned long size) 259 { 260 if (likely(access_ok(addr, size))) 261 size = __clear_user(addr, size); 262 return size; 263 } 264 265 #define user_addr_max() \ 266 (uaccess_kernel() ? ~0UL : TASK_SIZE) 267 268 extern long strncpy_from_user(char *dest, const char __user *src, long count); 269 270 extern __must_check long strnlen_user(const char __user *str, long n); 271 272 #endif /* __ASM_OPENRISC_UACCESS_H */ 273