1 #ifndef _ASM_X86_UACCESS_H 2 #define _ASM_X86_UACCESS_H 3 /* 4 * User space memory access functions 5 */ 6 #include <linux/errno.h> 7 #include <linux/compiler.h> 8 #include <linux/thread_info.h> 9 #include <linux/string.h> 10 #include <asm/asm.h> 11 #include <asm/page.h> 12 #include <asm/smap.h> 13 14 #define VERIFY_READ 0 15 #define VERIFY_WRITE 1 16 17 /* 18 * The fs value determines whether argument validity checking should be 19 * performed or not. If get_fs() == USER_DS, checking is performed, with 20 * get_fs() == KERNEL_DS, checking is bypassed. 21 * 22 * For historical reasons, these macros are grossly misnamed. 23 */ 24 25 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 26 27 #define KERNEL_DS MAKE_MM_SEG(-1UL) 28 #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX) 29 30 #define get_ds() (KERNEL_DS) 31 #define get_fs() (current_thread_info()->addr_limit) 32 #define set_fs(x) (current_thread_info()->addr_limit = (x)) 33 34 #define segment_eq(a, b) ((a).seg == (b).seg) 35 36 #define user_addr_max() (current_thread_info()->addr_limit.seg) 37 #define __addr_ok(addr) \ 38 ((unsigned long __force)(addr) < user_addr_max()) 39 40 /* 41 * Test whether a block of memory is a valid user space address. 42 * Returns 0 if the range is valid, nonzero otherwise. 43 * 44 * This is equivalent to the following test: 45 * (u33)addr + (u33)size > (u33)current->addr_limit.seg (u65 for x86_64) 46 * 47 * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry... 48 */ 49 50 #define __range_not_ok(addr, size, limit) \ 51 ({ \ 52 unsigned long flag, roksum; \ 53 __chk_user_ptr(addr); \ 54 asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \ 55 : "=&r" (flag), "=r" (roksum) \ 56 : "1" (addr), "g" ((long)(size)), \ 57 "rm" (limit)); \ 58 flag; \ 59 }) 60 61 /** 62 * access_ok: - Checks if a user space pointer is valid 63 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that 64 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe 65 * to write to a block, it is always safe to read from it. 66 * @addr: User space pointer to start of block to check 67 * @size: Size of block to check 68 * 69 * Context: User context only. This function may sleep. 70 * 71 * Checks if a pointer to a block of memory in user space is valid. 72 * 73 * Returns true (nonzero) if the memory block may be valid, false (zero) 74 * if it is definitely invalid. 75 * 76 * Note that, depending on architecture, this function probably just 77 * checks that the pointer is in the user space range - after calling 78 * this function, memory access functions may still return -EFAULT. 79 */ 80 #define access_ok(type, addr, size) \ 81 (likely(__range_not_ok(addr, size, user_addr_max()) == 0)) 82 83 /* 84 * The exception table consists of pairs of addresses relative to the 85 * exception table enty itself: the first is the address of an 86 * instruction that is allowed to fault, and the second is the address 87 * at which the program should continue. No registers are modified, 88 * so it is entirely up to the continuation code to figure out what to 89 * do. 90 * 91 * All the routines below use bits of fixup code that are out of line 92 * with the main instruction path. This means when everything is well, 93 * we don't even have to jump over them. Further, they do not intrude 94 * on our cache or tlb entries. 95 */ 96 97 struct exception_table_entry { 98 int insn, fixup; 99 }; 100 /* This is not the generic standard exception_table_entry format */ 101 #define ARCH_HAS_SORT_EXTABLE 102 #define ARCH_HAS_SEARCH_EXTABLE 103 104 extern int fixup_exception(struct pt_regs *regs); 105 extern int early_fixup_exception(unsigned long *ip); 106 107 /* 108 * These are the main single-value transfer routines. They automatically 109 * use the right size if we just have the right pointer type. 110 * 111 * This gets kind of ugly. We want to return _two_ values in "get_user()" 112 * and yet we don't want to do any pointers, because that is too much 113 * of a performance impact. Thus we have a few rather ugly macros here, 114 * and hide all the ugliness from the user. 115 * 116 * The "__xxx" versions of the user access functions are versions that 117 * do not verify the address space, that must have been done previously 118 * with a separate "access_ok()" call (this is used when we do multiple 119 * accesses to the same area of user memory). 120 */ 121 122 extern int __get_user_1(void); 123 extern int __get_user_2(void); 124 extern int __get_user_4(void); 125 extern int __get_user_8(void); 126 extern int __get_user_bad(void); 127 128 #define __get_user_x(size, ret, x, ptr) \ 129 asm volatile("call __get_user_" #size \ 130 : "=a" (ret), "=d" (x) \ 131 : "0" (ptr)) \ 132 133 /* Careful: we have to cast the result to the type of the pointer 134 * for sign reasons */ 135 136 /** 137 * get_user: - Get a simple variable from user space. 138 * @x: Variable to store result. 139 * @ptr: Source address, in user space. 140 * 141 * Context: User context only. This function may sleep. 142 * 143 * This macro copies a single simple variable from user space to kernel 144 * space. It supports simple types like char and int, but not larger 145 * data types like structures or arrays. 146 * 147 * @ptr must have pointer-to-simple-variable type, and the result of 148 * dereferencing @ptr must be assignable to @x without a cast. 149 * 150 * Returns zero on success, or -EFAULT on error. 151 * On error, the variable @x is set to zero. 152 */ 153 #ifdef CONFIG_X86_32 154 #define __get_user_8(__ret_gu, __val_gu, ptr) \ 155 __get_user_x(X, __ret_gu, __val_gu, ptr) 156 #else 157 #define __get_user_8(__ret_gu, __val_gu, ptr) \ 158 __get_user_x(8, __ret_gu, __val_gu, ptr) 159 #endif 160 161 #define get_user(x, ptr) \ 162 ({ \ 163 int __ret_gu; \ 164 unsigned long __val_gu; \ 165 __chk_user_ptr(ptr); \ 166 might_fault(); \ 167 switch (sizeof(*(ptr))) { \ 168 case 1: \ 169 __get_user_x(1, __ret_gu, __val_gu, ptr); \ 170 break; \ 171 case 2: \ 172 __get_user_x(2, __ret_gu, __val_gu, ptr); \ 173 break; \ 174 case 4: \ 175 __get_user_x(4, __ret_gu, __val_gu, ptr); \ 176 break; \ 177 case 8: \ 178 __get_user_8(__ret_gu, __val_gu, ptr); \ 179 break; \ 180 default: \ 181 __get_user_x(X, __ret_gu, __val_gu, ptr); \ 182 break; \ 183 } \ 184 (x) = (__typeof__(*(ptr)))__val_gu; \ 185 __ret_gu; \ 186 }) 187 188 #define __put_user_x(size, x, ptr, __ret_pu) \ 189 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \ 190 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx") 191 192 193 194 #ifdef CONFIG_X86_32 195 #define __put_user_asm_u64(x, addr, err, errret) \ 196 asm volatile(ASM_STAC "\n" \ 197 "1: movl %%eax,0(%2)\n" \ 198 "2: movl %%edx,4(%2)\n" \ 199 "3: " ASM_CLAC "\n" \ 200 ".section .fixup,\"ax\"\n" \ 201 "4: movl %3,%0\n" \ 202 " jmp 3b\n" \ 203 ".previous\n" \ 204 _ASM_EXTABLE(1b, 4b) \ 205 _ASM_EXTABLE(2b, 4b) \ 206 : "=r" (err) \ 207 : "A" (x), "r" (addr), "i" (errret), "0" (err)) 208 209 #define __put_user_asm_ex_u64(x, addr) \ 210 asm volatile(ASM_STAC "\n" \ 211 "1: movl %%eax,0(%1)\n" \ 212 "2: movl %%edx,4(%1)\n" \ 213 "3: " ASM_CLAC "\n" \ 214 _ASM_EXTABLE_EX(1b, 2b) \ 215 _ASM_EXTABLE_EX(2b, 3b) \ 216 : : "A" (x), "r" (addr)) 217 218 #define __put_user_x8(x, ptr, __ret_pu) \ 219 asm volatile("call __put_user_8" : "=a" (__ret_pu) \ 220 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx") 221 #else 222 #define __put_user_asm_u64(x, ptr, retval, errret) \ 223 __put_user_asm(x, ptr, retval, "q", "", "er", errret) 224 #define __put_user_asm_ex_u64(x, addr) \ 225 __put_user_asm_ex(x, addr, "q", "", "er") 226 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu) 227 #endif 228 229 extern void __put_user_bad(void); 230 231 /* 232 * Strange magic calling convention: pointer in %ecx, 233 * value in %eax(:%edx), return value in %eax. clobbers %rbx 234 */ 235 extern void __put_user_1(void); 236 extern void __put_user_2(void); 237 extern void __put_user_4(void); 238 extern void __put_user_8(void); 239 240 /** 241 * put_user: - Write a simple value into user space. 242 * @x: Value to copy to user space. 243 * @ptr: Destination address, in user space. 244 * 245 * Context: User context only. This function may sleep. 246 * 247 * This macro copies a single simple value from kernel space to user 248 * space. It supports simple types like char and int, but not larger 249 * data types like structures or arrays. 250 * 251 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 252 * to the result of dereferencing @ptr. 253 * 254 * Returns zero on success, or -EFAULT on error. 255 */ 256 #define put_user(x, ptr) \ 257 ({ \ 258 int __ret_pu; \ 259 __typeof__(*(ptr)) __pu_val; \ 260 __chk_user_ptr(ptr); \ 261 might_fault(); \ 262 __pu_val = x; \ 263 switch (sizeof(*(ptr))) { \ 264 case 1: \ 265 __put_user_x(1, __pu_val, ptr, __ret_pu); \ 266 break; \ 267 case 2: \ 268 __put_user_x(2, __pu_val, ptr, __ret_pu); \ 269 break; \ 270 case 4: \ 271 __put_user_x(4, __pu_val, ptr, __ret_pu); \ 272 break; \ 273 case 8: \ 274 __put_user_x8(__pu_val, ptr, __ret_pu); \ 275 break; \ 276 default: \ 277 __put_user_x(X, __pu_val, ptr, __ret_pu); \ 278 break; \ 279 } \ 280 __ret_pu; \ 281 }) 282 283 #define __put_user_size(x, ptr, size, retval, errret) \ 284 do { \ 285 retval = 0; \ 286 __chk_user_ptr(ptr); \ 287 switch (size) { \ 288 case 1: \ 289 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \ 290 break; \ 291 case 2: \ 292 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \ 293 break; \ 294 case 4: \ 295 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \ 296 break; \ 297 case 8: \ 298 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \ 299 errret); \ 300 break; \ 301 default: \ 302 __put_user_bad(); \ 303 } \ 304 } while (0) 305 306 #define __put_user_size_ex(x, ptr, size) \ 307 do { \ 308 __chk_user_ptr(ptr); \ 309 switch (size) { \ 310 case 1: \ 311 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \ 312 break; \ 313 case 2: \ 314 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \ 315 break; \ 316 case 4: \ 317 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \ 318 break; \ 319 case 8: \ 320 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \ 321 break; \ 322 default: \ 323 __put_user_bad(); \ 324 } \ 325 } while (0) 326 327 #ifdef CONFIG_X86_32 328 #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad() 329 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad() 330 #else 331 #define __get_user_asm_u64(x, ptr, retval, errret) \ 332 __get_user_asm(x, ptr, retval, "q", "", "=r", errret) 333 #define __get_user_asm_ex_u64(x, ptr) \ 334 __get_user_asm_ex(x, ptr, "q", "", "=r") 335 #endif 336 337 #define __get_user_size(x, ptr, size, retval, errret) \ 338 do { \ 339 retval = 0; \ 340 __chk_user_ptr(ptr); \ 341 switch (size) { \ 342 case 1: \ 343 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \ 344 break; \ 345 case 2: \ 346 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \ 347 break; \ 348 case 4: \ 349 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \ 350 break; \ 351 case 8: \ 352 __get_user_asm_u64(x, ptr, retval, errret); \ 353 break; \ 354 default: \ 355 (x) = __get_user_bad(); \ 356 } \ 357 } while (0) 358 359 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \ 360 asm volatile(ASM_STAC "\n" \ 361 "1: mov"itype" %2,%"rtype"1\n" \ 362 "2: " ASM_CLAC "\n" \ 363 ".section .fixup,\"ax\"\n" \ 364 "3: mov %3,%0\n" \ 365 " xor"itype" %"rtype"1,%"rtype"1\n" \ 366 " jmp 2b\n" \ 367 ".previous\n" \ 368 _ASM_EXTABLE(1b, 3b) \ 369 : "=r" (err), ltype(x) \ 370 : "m" (__m(addr)), "i" (errret), "0" (err)) 371 372 #define __get_user_size_ex(x, ptr, size) \ 373 do { \ 374 __chk_user_ptr(ptr); \ 375 switch (size) { \ 376 case 1: \ 377 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \ 378 break; \ 379 case 2: \ 380 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \ 381 break; \ 382 case 4: \ 383 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \ 384 break; \ 385 case 8: \ 386 __get_user_asm_ex_u64(x, ptr); \ 387 break; \ 388 default: \ 389 (x) = __get_user_bad(); \ 390 } \ 391 } while (0) 392 393 #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \ 394 asm volatile("1: mov"itype" %1,%"rtype"0\n" \ 395 "2:\n" \ 396 _ASM_EXTABLE_EX(1b, 2b) \ 397 : ltype(x) : "m" (__m(addr))) 398 399 #define __put_user_nocheck(x, ptr, size) \ 400 ({ \ 401 int __pu_err; \ 402 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \ 403 __pu_err; \ 404 }) 405 406 #define __get_user_nocheck(x, ptr, size) \ 407 ({ \ 408 int __gu_err; \ 409 unsigned long __gu_val; \ 410 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \ 411 (x) = (__force __typeof__(*(ptr)))__gu_val; \ 412 __gu_err; \ 413 }) 414 415 /* FIXME: this hack is definitely wrong -AK */ 416 struct __large_struct { unsigned long buf[100]; }; 417 #define __m(x) (*(struct __large_struct __user *)(x)) 418 419 /* 420 * Tell gcc we read from memory instead of writing: this is because 421 * we do not write to any memory gcc knows about, so there are no 422 * aliasing issues. 423 */ 424 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \ 425 asm volatile(ASM_STAC "\n" \ 426 "1: mov"itype" %"rtype"1,%2\n" \ 427 "2: " ASM_CLAC "\n" \ 428 ".section .fixup,\"ax\"\n" \ 429 "3: mov %3,%0\n" \ 430 " jmp 2b\n" \ 431 ".previous\n" \ 432 _ASM_EXTABLE(1b, 3b) \ 433 : "=r"(err) \ 434 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err)) 435 436 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \ 437 asm volatile("1: mov"itype" %"rtype"0,%1\n" \ 438 "2:\n" \ 439 _ASM_EXTABLE_EX(1b, 2b) \ 440 : : ltype(x), "m" (__m(addr))) 441 442 /* 443 * uaccess_try and catch 444 */ 445 #define uaccess_try do { \ 446 current_thread_info()->uaccess_err = 0; \ 447 stac(); \ 448 barrier(); 449 450 #define uaccess_catch(err) \ 451 clac(); \ 452 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \ 453 } while (0) 454 455 /** 456 * __get_user: - Get a simple variable from user space, with less checking. 457 * @x: Variable to store result. 458 * @ptr: Source address, in user space. 459 * 460 * Context: User context only. This function may sleep. 461 * 462 * This macro copies a single simple variable from user space to kernel 463 * space. It supports simple types like char and int, but not larger 464 * data types like structures or arrays. 465 * 466 * @ptr must have pointer-to-simple-variable type, and the result of 467 * dereferencing @ptr must be assignable to @x without a cast. 468 * 469 * Caller must check the pointer with access_ok() before calling this 470 * function. 471 * 472 * Returns zero on success, or -EFAULT on error. 473 * On error, the variable @x is set to zero. 474 */ 475 476 #define __get_user(x, ptr) \ 477 __get_user_nocheck((x), (ptr), sizeof(*(ptr))) 478 479 /** 480 * __put_user: - Write a simple value into user space, with less checking. 481 * @x: Value to copy to user space. 482 * @ptr: Destination address, in user space. 483 * 484 * Context: User context only. This function may sleep. 485 * 486 * This macro copies a single simple value from kernel space to user 487 * space. It supports simple types like char and int, but not larger 488 * data types like structures or arrays. 489 * 490 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 491 * to the result of dereferencing @ptr. 492 * 493 * Caller must check the pointer with access_ok() before calling this 494 * function. 495 * 496 * Returns zero on success, or -EFAULT on error. 497 */ 498 499 #define __put_user(x, ptr) \ 500 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 501 502 #define __get_user_unaligned __get_user 503 #define __put_user_unaligned __put_user 504 505 /* 506 * {get|put}_user_try and catch 507 * 508 * get_user_try { 509 * get_user_ex(...); 510 * } get_user_catch(err) 511 */ 512 #define get_user_try uaccess_try 513 #define get_user_catch(err) uaccess_catch(err) 514 515 #define get_user_ex(x, ptr) do { \ 516 unsigned long __gue_val; \ 517 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \ 518 (x) = (__force __typeof__(*(ptr)))__gue_val; \ 519 } while (0) 520 521 #define put_user_try uaccess_try 522 #define put_user_catch(err) uaccess_catch(err) 523 524 #define put_user_ex(x, ptr) \ 525 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) 526 527 extern unsigned long 528 copy_from_user_nmi(void *to, const void __user *from, unsigned long n); 529 extern __must_check long 530 strncpy_from_user(char *dst, const char __user *src, long count); 531 532 extern __must_check long strlen_user(const char __user *str); 533 extern __must_check long strnlen_user(const char __user *str, long n); 534 535 unsigned long __must_check clear_user(void __user *mem, unsigned long len); 536 unsigned long __must_check __clear_user(void __user *mem, unsigned long len); 537 538 /* 539 * movsl can be slow when source and dest are not both 8-byte aligned 540 */ 541 #ifdef CONFIG_X86_INTEL_USERCOPY 542 extern struct movsl_mask { 543 int mask; 544 } ____cacheline_aligned_in_smp movsl_mask; 545 #endif 546 547 #define ARCH_HAS_NOCACHE_UACCESS 1 548 549 #ifdef CONFIG_X86_32 550 # include <asm/uaccess_32.h> 551 #else 552 # include <asm/uaccess_64.h> 553 #endif 554 555 #endif /* _ASM_X86_UACCESS_H */ 556 557