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 #ifdef __KERNEL__ 15 #ifndef __ASSEMBLY__ 16 17 #include <linux/kernel.h> 18 #include <linux/errno.h> 19 #include <linux/sched.h> /* RLIMIT_FSIZE */ 20 #include <linux/mm.h> 21 22 #include <asm/mmu.h> 23 #include <asm/page.h> 24 #include <asm/pgtable.h> 25 #include <linux/string.h> 26 27 #define VERIFY_READ 0 28 #define VERIFY_WRITE 1 29 30 /* 31 * On Microblaze the fs value is actually the top of the corresponding 32 * address space. 33 * 34 * The fs value determines whether argument validity checking should be 35 * performed or not. If get_fs() == USER_DS, checking is performed, with 36 * get_fs() == KERNEL_DS, checking is bypassed. 37 * 38 * For historical reasons, these macros are grossly misnamed. 39 * 40 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal. 41 */ 42 # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 43 44 # ifndef CONFIG_MMU 45 # define KERNEL_DS MAKE_MM_SEG(0) 46 # define USER_DS KERNEL_DS 47 # else 48 # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) 49 # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1) 50 # endif 51 52 # define get_ds() (KERNEL_DS) 53 # define get_fs() (current_thread_info()->addr_limit) 54 # define set_fs(val) (current_thread_info()->addr_limit = (val)) 55 56 # define segment_eq(a, b) ((a).seg == (b).seg) 57 58 /* 59 * The exception table consists of pairs of addresses: the first is the 60 * address of an instruction that is allowed to fault, and the second is 61 * the address at which the program should continue. No registers are 62 * modified, so it is entirely up to the continuation code to figure out 63 * what to do. 64 * 65 * All the routines below use bits of fixup code that are out of line 66 * with the main instruction path. This means when everything is well, 67 * we don't even have to jump over them. Further, they do not intrude 68 * on our cache or tlb entries. 69 */ 70 struct exception_table_entry { 71 unsigned long insn, fixup; 72 }; 73 74 #ifndef CONFIG_MMU 75 76 /* Check against bounds of physical memory */ 77 static inline int ___range_ok(unsigned long addr, unsigned long size) 78 { 79 return ((addr < memory_start) || 80 ((addr + size - 1) > (memory_start + memory_size - 1))); 81 } 82 83 #define __range_ok(addr, size) \ 84 ___range_ok((unsigned long)(addr), (unsigned long)(size)) 85 86 #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0) 87 88 #else 89 90 static inline int access_ok(int type, const void __user *addr, 91 unsigned long size) 92 { 93 if (!size) 94 goto ok; 95 96 if ((get_fs().seg < ((unsigned long)addr)) || 97 (get_fs().seg < ((unsigned long)addr + size - 1))) { 98 pr_devel("ACCESS fail: %s at 0x%08x (size 0x%x), seg 0x%08x\n", 99 type ? "WRITE" : "READ ", (__force u32)addr, (u32)size, 100 (u32)get_fs().seg); 101 return 0; 102 } 103 ok: 104 pr_devel("ACCESS OK: %s at 0x%08x (size 0x%x), seg 0x%08x\n", 105 type ? "WRITE" : "READ ", (__force u32)addr, (u32)size, 106 (u32)get_fs().seg); 107 return 1; 108 } 109 #endif 110 111 #ifdef CONFIG_MMU 112 # define __FIXUP_SECTION ".section .fixup,\"ax\"\n" 113 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n" 114 #else 115 # define __FIXUP_SECTION ".section .discard,\"ax\"\n" 116 # define __EX_TABLE_SECTION ".section .discard,\"ax\"\n" 117 #endif 118 119 extern unsigned long __copy_tofrom_user(void __user *to, 120 const void __user *from, unsigned long size); 121 122 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */ 123 static inline unsigned long __must_check __clear_user(void __user *to, 124 unsigned long n) 125 { 126 /* normal memset with two words to __ex_table */ 127 __asm__ __volatile__ ( \ 128 "1: sb r0, %1, r0;" \ 129 " addik %0, %0, -1;" \ 130 " bneid %0, 1b;" \ 131 " addik %1, %1, 1;" \ 132 "2: " \ 133 __EX_TABLE_SECTION \ 134 ".word 1b,2b;" \ 135 ".previous;" \ 136 : "=r"(n), "=r"(to) \ 137 : "0"(n), "1"(to) 138 ); 139 return n; 140 } 141 142 static inline unsigned long __must_check clear_user(void __user *to, 143 unsigned long n) 144 { 145 might_fault(); 146 if (unlikely(!access_ok(VERIFY_WRITE, to, n))) 147 return n; 148 149 return __clear_user(to, n); 150 } 151 152 /* put_user and get_user macros */ 153 extern long __user_bad(void); 154 155 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ 156 ({ \ 157 __asm__ __volatile__ ( \ 158 "1:" insn " %1, %2, r0;" \ 159 " addk %0, r0, r0;" \ 160 "2: " \ 161 __FIXUP_SECTION \ 162 "3: brid 2b;" \ 163 " addik %0, r0, %3;" \ 164 ".previous;" \ 165 __EX_TABLE_SECTION \ 166 ".word 1b,3b;" \ 167 ".previous;" \ 168 : "=&r"(__gu_err), "=r"(__gu_val) \ 169 : "r"(__gu_ptr), "i"(-EFAULT) \ 170 ); \ 171 }) 172 173 /** 174 * get_user: - Get a simple variable from user space. 175 * @x: Variable to store result. 176 * @ptr: Source address, in user space. 177 * 178 * Context: User context only. This function may sleep if pagefaults are 179 * enabled. 180 * 181 * This macro copies a single simple variable from user space to kernel 182 * space. It supports simple types like char and int, but not larger 183 * data types like structures or arrays. 184 * 185 * @ptr must have pointer-to-simple-variable type, and the result of 186 * dereferencing @ptr must be assignable to @x without a cast. 187 * 188 * Returns zero on success, or -EFAULT on error. 189 * On error, the variable @x is set to zero. 190 */ 191 #define get_user(x, ptr) \ 192 __get_user_check((x), (ptr), sizeof(*(ptr))) 193 194 #define __get_user_check(x, ptr, size) \ 195 ({ \ 196 unsigned long __gu_val = 0; \ 197 const typeof(*(ptr)) __user *__gu_addr = (ptr); \ 198 int __gu_err = 0; \ 199 \ 200 if (access_ok(VERIFY_READ, __gu_addr, size)) { \ 201 switch (size) { \ 202 case 1: \ 203 __get_user_asm("lbu", __gu_addr, __gu_val, \ 204 __gu_err); \ 205 break; \ 206 case 2: \ 207 __get_user_asm("lhu", __gu_addr, __gu_val, \ 208 __gu_err); \ 209 break; \ 210 case 4: \ 211 __get_user_asm("lw", __gu_addr, __gu_val, \ 212 __gu_err); \ 213 break; \ 214 default: \ 215 __gu_err = __user_bad(); \ 216 break; \ 217 } \ 218 } else { \ 219 __gu_err = -EFAULT; \ 220 } \ 221 x = (__force typeof(*(ptr)))__gu_val; \ 222 __gu_err; \ 223 }) 224 225 #define __get_user(x, ptr) \ 226 ({ \ 227 unsigned long __gu_val = 0; \ 228 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \ 229 long __gu_err; \ 230 switch (sizeof(*(ptr))) { \ 231 case 1: \ 232 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \ 233 break; \ 234 case 2: \ 235 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \ 236 break; \ 237 case 4: \ 238 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \ 239 break; \ 240 default: \ 241 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\ 242 } \ 243 x = (__force __typeof__(*(ptr))) __gu_val; \ 244 __gu_err; \ 245 }) 246 247 248 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ 249 ({ \ 250 __asm__ __volatile__ ( \ 251 "1:" insn " %1, %2, r0;" \ 252 " addk %0, r0, r0;" \ 253 "2: " \ 254 __FIXUP_SECTION \ 255 "3: brid 2b;" \ 256 " addik %0, r0, %3;" \ 257 ".previous;" \ 258 __EX_TABLE_SECTION \ 259 ".word 1b,3b;" \ 260 ".previous;" \ 261 : "=&r"(__gu_err) \ 262 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ 263 ); \ 264 }) 265 266 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \ 267 ({ \ 268 __asm__ __volatile__ (" lwi %0, %1, 0;" \ 269 "1: swi %0, %2, 0;" \ 270 " lwi %0, %1, 4;" \ 271 "2: swi %0, %2, 4;" \ 272 " addk %0, r0, r0;" \ 273 "3: " \ 274 __FIXUP_SECTION \ 275 "4: brid 3b;" \ 276 " addik %0, r0, %3;" \ 277 ".previous;" \ 278 __EX_TABLE_SECTION \ 279 ".word 1b,4b,2b,4b;" \ 280 ".previous;" \ 281 : "=&r"(__gu_err) \ 282 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ 283 ); \ 284 }) 285 286 /** 287 * put_user: - Write a simple value into user space. 288 * @x: Value to copy to user space. 289 * @ptr: Destination address, in user space. 290 * 291 * Context: User context only. This function may sleep if pagefaults are 292 * enabled. 293 * 294 * This macro copies a single simple value from kernel space to user 295 * space. It supports simple types like char and int, but not larger 296 * data types like structures or arrays. 297 * 298 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 299 * to the result of dereferencing @ptr. 300 * 301 * Returns zero on success, or -EFAULT on error. 302 */ 303 #define put_user(x, ptr) \ 304 __put_user_check((x), (ptr), sizeof(*(ptr))) 305 306 #define __put_user_check(x, ptr, size) \ 307 ({ \ 308 typeof(*(ptr)) volatile __pu_val = x; \ 309 typeof(*(ptr)) __user *__pu_addr = (ptr); \ 310 int __pu_err = 0; \ 311 \ 312 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \ 313 switch (size) { \ 314 case 1: \ 315 __put_user_asm("sb", __pu_addr, __pu_val, \ 316 __pu_err); \ 317 break; \ 318 case 2: \ 319 __put_user_asm("sh", __pu_addr, __pu_val, \ 320 __pu_err); \ 321 break; \ 322 case 4: \ 323 __put_user_asm("sw", __pu_addr, __pu_val, \ 324 __pu_err); \ 325 break; \ 326 case 8: \ 327 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\ 328 break; \ 329 default: \ 330 __pu_err = __user_bad(); \ 331 break; \ 332 } \ 333 } else { \ 334 __pu_err = -EFAULT; \ 335 } \ 336 __pu_err; \ 337 }) 338 339 #define __put_user(x, ptr) \ 340 ({ \ 341 __typeof__(*(ptr)) volatile __gu_val = (x); \ 342 long __gu_err = 0; \ 343 switch (sizeof(__gu_val)) { \ 344 case 1: \ 345 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \ 346 break; \ 347 case 2: \ 348 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \ 349 break; \ 350 case 4: \ 351 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \ 352 break; \ 353 case 8: \ 354 __put_user_asm_8((ptr), __gu_val, __gu_err); \ 355 break; \ 356 default: \ 357 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \ 358 } \ 359 __gu_err; \ 360 }) 361 362 363 /* copy_to_from_user */ 364 #define __copy_from_user(to, from, n) \ 365 __copy_tofrom_user((__force void __user *)(to), \ 366 (void __user *)(from), (n)) 367 #define __copy_from_user_inatomic(to, from, n) \ 368 __copy_from_user((to), (from), (n)) 369 370 static inline long copy_from_user(void *to, 371 const void __user *from, unsigned long n) 372 { 373 unsigned long res = n; 374 might_fault(); 375 if (likely(access_ok(VERIFY_READ, from, n))) 376 res = __copy_from_user(to, from, n); 377 if (unlikely(res)) 378 memset(to + (n - res), 0, res); 379 return res; 380 } 381 382 #define __copy_to_user(to, from, n) \ 383 __copy_tofrom_user((void __user *)(to), \ 384 (__force const void __user *)(from), (n)) 385 #define __copy_to_user_inatomic(to, from, n) __copy_to_user((to), (from), (n)) 386 387 static inline long copy_to_user(void __user *to, 388 const void *from, unsigned long n) 389 { 390 might_fault(); 391 if (access_ok(VERIFY_WRITE, to, n)) 392 return __copy_to_user(to, from, n); 393 return n; 394 } 395 396 /* 397 * Copy a null terminated string from userspace. 398 */ 399 extern int __strncpy_user(char *to, const char __user *from, int len); 400 401 #define __strncpy_from_user __strncpy_user 402 403 static inline long 404 strncpy_from_user(char *dst, const char __user *src, long count) 405 { 406 if (!access_ok(VERIFY_READ, src, 1)) 407 return -EFAULT; 408 return __strncpy_from_user(dst, src, count); 409 } 410 411 /* 412 * Return the size of a string (including the ending 0) 413 * 414 * Return 0 on exception, a value greater than N if too long 415 */ 416 extern int __strnlen_user(const char __user *sstr, int len); 417 418 static inline long strnlen_user(const char __user *src, long n) 419 { 420 if (!access_ok(VERIFY_READ, src, 1)) 421 return 0; 422 return __strnlen_user(src, n); 423 } 424 425 #endif /* __ASSEMBLY__ */ 426 #endif /* __KERNEL__ */ 427 428 #endif /* _ASM_MICROBLAZE_UACCESS_H */ 429