1 /* 2 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> 3 * Copyright (C) 2008-2009 PetaLogix 4 * Copyright (C) 2006 Atmark Techno, Inc. 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file "COPYING" in the main directory of this archive 8 * for more details. 9 */ 10 11 #ifndef _ASM_MICROBLAZE_UACCESS_H 12 #define _ASM_MICROBLAZE_UACCESS_H 13 14 #include <linux/kernel.h> 15 #include <linux/mm.h> 16 17 #include <asm/mmu.h> 18 #include <asm/page.h> 19 #include <asm/pgtable.h> 20 #include <asm/extable.h> 21 #include <linux/string.h> 22 23 /* 24 * On Microblaze the fs value is actually the top of the corresponding 25 * address space. 26 * 27 * The fs value determines whether argument validity checking should be 28 * performed or not. If get_fs() == USER_DS, checking is performed, with 29 * get_fs() == KERNEL_DS, checking is bypassed. 30 * 31 * For historical reasons, these macros are grossly misnamed. 32 * 33 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal. 34 */ 35 # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 36 37 # ifndef CONFIG_MMU 38 # define KERNEL_DS MAKE_MM_SEG(0) 39 # define USER_DS KERNEL_DS 40 # else 41 # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) 42 # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1) 43 # endif 44 45 # define get_fs() (current_thread_info()->addr_limit) 46 # define set_fs(val) (current_thread_info()->addr_limit = (val)) 47 48 # define segment_eq(a, b) ((a).seg == (b).seg) 49 50 #ifndef CONFIG_MMU 51 52 /* Check against bounds of physical memory */ 53 static inline int ___range_ok(unsigned long addr, unsigned long size) 54 { 55 return ((addr < memory_start) || 56 ((addr + size - 1) > (memory_start + memory_size - 1))); 57 } 58 59 #define __range_ok(addr, size) \ 60 ___range_ok((unsigned long)(addr), (unsigned long)(size)) 61 62 #define access_ok(addr, size) (__range_ok((addr), (size)) == 0) 63 64 #else 65 66 static inline int access_ok(const void __user *addr, unsigned long size) 67 { 68 if (!size) 69 goto ok; 70 71 if ((get_fs().seg < ((unsigned long)addr)) || 72 (get_fs().seg < ((unsigned long)addr + size - 1))) { 73 pr_devel("ACCESS fail at 0x%08x (size 0x%x), seg 0x%08x\n", 74 (__force u32)addr, (u32)size, 75 (u32)get_fs().seg); 76 return 0; 77 } 78 ok: 79 pr_devel("ACCESS OK at 0x%08x (size 0x%x), seg 0x%08x\n", 80 (__force u32)addr, (u32)size, 81 (u32)get_fs().seg); 82 return 1; 83 } 84 #endif 85 86 #ifdef CONFIG_MMU 87 # define __FIXUP_SECTION ".section .fixup,\"ax\"\n" 88 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n" 89 #else 90 # define __FIXUP_SECTION ".section .discard,\"ax\"\n" 91 # define __EX_TABLE_SECTION ".section .discard,\"ax\"\n" 92 #endif 93 94 extern unsigned long __copy_tofrom_user(void __user *to, 95 const void __user *from, unsigned long size); 96 97 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */ 98 static inline unsigned long __must_check __clear_user(void __user *to, 99 unsigned long n) 100 { 101 /* normal memset with two words to __ex_table */ 102 __asm__ __volatile__ ( \ 103 "1: sb r0, %1, r0;" \ 104 " addik %0, %0, -1;" \ 105 " bneid %0, 1b;" \ 106 " addik %1, %1, 1;" \ 107 "2: " \ 108 __EX_TABLE_SECTION \ 109 ".word 1b,2b;" \ 110 ".previous;" \ 111 : "=r"(n), "=r"(to) \ 112 : "0"(n), "1"(to) 113 ); 114 return n; 115 } 116 117 static inline unsigned long __must_check clear_user(void __user *to, 118 unsigned long n) 119 { 120 might_fault(); 121 if (unlikely(!access_ok(to, n))) 122 return n; 123 124 return __clear_user(to, n); 125 } 126 127 /* put_user and get_user macros */ 128 extern long __user_bad(void); 129 130 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ 131 ({ \ 132 __asm__ __volatile__ ( \ 133 "1:" insn " %1, %2, r0;" \ 134 " addk %0, r0, r0;" \ 135 "2: " \ 136 __FIXUP_SECTION \ 137 "3: brid 2b;" \ 138 " addik %0, r0, %3;" \ 139 ".previous;" \ 140 __EX_TABLE_SECTION \ 141 ".word 1b,3b;" \ 142 ".previous;" \ 143 : "=&r"(__gu_err), "=r"(__gu_val) \ 144 : "r"(__gu_ptr), "i"(-EFAULT) \ 145 ); \ 146 }) 147 148 /** 149 * get_user: - Get a simple variable from user space. 150 * @x: Variable to store result. 151 * @ptr: Source address, in user space. 152 * 153 * Context: User context only. This function may sleep if pagefaults are 154 * enabled. 155 * 156 * This macro copies a single simple variable from user space to kernel 157 * space. It supports simple types like char and int, but not larger 158 * data types like structures or arrays. 159 * 160 * @ptr must have pointer-to-simple-variable type, and the result of 161 * dereferencing @ptr must be assignable to @x without a cast. 162 * 163 * Returns zero on success, or -EFAULT on error. 164 * On error, the variable @x is set to zero. 165 */ 166 #define get_user(x, ptr) \ 167 __get_user_check((x), (ptr), sizeof(*(ptr))) 168 169 #define __get_user_check(x, ptr, size) \ 170 ({ \ 171 unsigned long __gu_val = 0; \ 172 const typeof(*(ptr)) __user *__gu_addr = (ptr); \ 173 int __gu_err = 0; \ 174 \ 175 if (access_ok(__gu_addr, size)) { \ 176 switch (size) { \ 177 case 1: \ 178 __get_user_asm("lbu", __gu_addr, __gu_val, \ 179 __gu_err); \ 180 break; \ 181 case 2: \ 182 __get_user_asm("lhu", __gu_addr, __gu_val, \ 183 __gu_err); \ 184 break; \ 185 case 4: \ 186 __get_user_asm("lw", __gu_addr, __gu_val, \ 187 __gu_err); \ 188 break; \ 189 default: \ 190 __gu_err = __user_bad(); \ 191 break; \ 192 } \ 193 } else { \ 194 __gu_err = -EFAULT; \ 195 } \ 196 x = (__force typeof(*(ptr)))__gu_val; \ 197 __gu_err; \ 198 }) 199 200 #define __get_user(x, ptr) \ 201 ({ \ 202 unsigned long __gu_val = 0; \ 203 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \ 204 long __gu_err; \ 205 switch (sizeof(*(ptr))) { \ 206 case 1: \ 207 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \ 208 break; \ 209 case 2: \ 210 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \ 211 break; \ 212 case 4: \ 213 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \ 214 break; \ 215 default: \ 216 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\ 217 } \ 218 x = (__force __typeof__(*(ptr))) __gu_val; \ 219 __gu_err; \ 220 }) 221 222 223 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ 224 ({ \ 225 __asm__ __volatile__ ( \ 226 "1:" insn " %1, %2, r0;" \ 227 " addk %0, r0, r0;" \ 228 "2: " \ 229 __FIXUP_SECTION \ 230 "3: brid 2b;" \ 231 " addik %0, r0, %3;" \ 232 ".previous;" \ 233 __EX_TABLE_SECTION \ 234 ".word 1b,3b;" \ 235 ".previous;" \ 236 : "=&r"(__gu_err) \ 237 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ 238 ); \ 239 }) 240 241 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \ 242 ({ \ 243 __asm__ __volatile__ (" lwi %0, %1, 0;" \ 244 "1: swi %0, %2, 0;" \ 245 " lwi %0, %1, 4;" \ 246 "2: swi %0, %2, 4;" \ 247 " addk %0, r0, r0;" \ 248 "3: " \ 249 __FIXUP_SECTION \ 250 "4: brid 3b;" \ 251 " addik %0, r0, %3;" \ 252 ".previous;" \ 253 __EX_TABLE_SECTION \ 254 ".word 1b,4b,2b,4b;" \ 255 ".previous;" \ 256 : "=&r"(__gu_err) \ 257 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ 258 ); \ 259 }) 260 261 /** 262 * put_user: - Write a simple value into user space. 263 * @x: Value to copy to user space. 264 * @ptr: Destination address, in user space. 265 * 266 * Context: User context only. This function may sleep if pagefaults are 267 * enabled. 268 * 269 * This macro copies a single simple value from kernel space to user 270 * space. It supports simple types like char and int, but not larger 271 * data types like structures or arrays. 272 * 273 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 274 * to the result of dereferencing @ptr. 275 * 276 * Returns zero on success, or -EFAULT on error. 277 */ 278 #define put_user(x, ptr) \ 279 __put_user_check((x), (ptr), sizeof(*(ptr))) 280 281 #define __put_user_check(x, ptr, size) \ 282 ({ \ 283 typeof(*(ptr)) volatile __pu_val = x; \ 284 typeof(*(ptr)) __user *__pu_addr = (ptr); \ 285 int __pu_err = 0; \ 286 \ 287 if (access_ok(__pu_addr, size)) { \ 288 switch (size) { \ 289 case 1: \ 290 __put_user_asm("sb", __pu_addr, __pu_val, \ 291 __pu_err); \ 292 break; \ 293 case 2: \ 294 __put_user_asm("sh", __pu_addr, __pu_val, \ 295 __pu_err); \ 296 break; \ 297 case 4: \ 298 __put_user_asm("sw", __pu_addr, __pu_val, \ 299 __pu_err); \ 300 break; \ 301 case 8: \ 302 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\ 303 break; \ 304 default: \ 305 __pu_err = __user_bad(); \ 306 break; \ 307 } \ 308 } else { \ 309 __pu_err = -EFAULT; \ 310 } \ 311 __pu_err; \ 312 }) 313 314 #define __put_user(x, ptr) \ 315 ({ \ 316 __typeof__(*(ptr)) volatile __gu_val = (x); \ 317 long __gu_err = 0; \ 318 switch (sizeof(__gu_val)) { \ 319 case 1: \ 320 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \ 321 break; \ 322 case 2: \ 323 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \ 324 break; \ 325 case 4: \ 326 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \ 327 break; \ 328 case 8: \ 329 __put_user_asm_8((ptr), __gu_val, __gu_err); \ 330 break; \ 331 default: \ 332 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \ 333 } \ 334 __gu_err; \ 335 }) 336 337 static inline unsigned long 338 raw_copy_from_user(void *to, const void __user *from, unsigned long n) 339 { 340 return __copy_tofrom_user((__force void __user *)to, from, n); 341 } 342 343 static inline unsigned long 344 raw_copy_to_user(void __user *to, const void *from, unsigned long n) 345 { 346 return __copy_tofrom_user(to, (__force const void __user *)from, n); 347 } 348 #define INLINE_COPY_FROM_USER 349 #define INLINE_COPY_TO_USER 350 351 /* 352 * Copy a null terminated string from userspace. 353 */ 354 extern int __strncpy_user(char *to, const char __user *from, int len); 355 356 static inline long 357 strncpy_from_user(char *dst, const char __user *src, long count) 358 { 359 if (!access_ok(src, 1)) 360 return -EFAULT; 361 return __strncpy_user(dst, src, count); 362 } 363 364 /* 365 * Return the size of a string (including the ending 0) 366 * 367 * Return 0 on exception, a value greater than N if too long 368 */ 369 extern int __strnlen_user(const char __user *sstr, int len); 370 371 static inline long strnlen_user(const char __user *src, long n) 372 { 373 if (!access_ok(src, 1)) 374 return 0; 375 return __strnlen_user(src, n); 376 } 377 378 #endif /* _ASM_MICROBLAZE_UACCESS_H */ 379