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