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