1 /* 2 * Copyright (C) 1991, 1992 Linus Torvalds 3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs 4 * 5 * Pentium III FXSR, SSE support 6 * Gareth Hughes <gareth@valinux.com>, May 2000 7 */ 8 9 /* 10 * Handle hardware traps and faults. 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/context_tracking.h> 16 #include <linux/interrupt.h> 17 #include <linux/kallsyms.h> 18 #include <linux/spinlock.h> 19 #include <linux/kprobes.h> 20 #include <linux/uaccess.h> 21 #include <linux/kdebug.h> 22 #include <linux/kgdb.h> 23 #include <linux/kernel.h> 24 #include <linux/export.h> 25 #include <linux/ptrace.h> 26 #include <linux/uprobes.h> 27 #include <linux/string.h> 28 #include <linux/delay.h> 29 #include <linux/errno.h> 30 #include <linux/kexec.h> 31 #include <linux/sched.h> 32 #include <linux/sched/task_stack.h> 33 #include <linux/timer.h> 34 #include <linux/init.h> 35 #include <linux/bug.h> 36 #include <linux/nmi.h> 37 #include <linux/mm.h> 38 #include <linux/smp.h> 39 #include <linux/io.h> 40 #include <asm/stacktrace.h> 41 #include <asm/processor.h> 42 #include <asm/debugreg.h> 43 #include <linux/atomic.h> 44 #include <asm/text-patching.h> 45 #include <asm/ftrace.h> 46 #include <asm/traps.h> 47 #include <asm/desc.h> 48 #include <asm/fpu/internal.h> 49 #include <asm/cpu_entry_area.h> 50 #include <asm/mce.h> 51 #include <asm/fixmap.h> 52 #include <asm/mach_traps.h> 53 #include <asm/alternative.h> 54 #include <asm/fpu/xstate.h> 55 #include <asm/trace/mpx.h> 56 #include <asm/mpx.h> 57 #include <asm/vm86.h> 58 #include <asm/umip.h> 59 #include <asm/insn.h> 60 #include <asm/insn-eval.h> 61 62 #ifdef CONFIG_X86_64 63 #include <asm/x86_init.h> 64 #include <asm/pgalloc.h> 65 #include <asm/proto.h> 66 #else 67 #include <asm/processor-flags.h> 68 #include <asm/setup.h> 69 #include <asm/proto.h> 70 #endif 71 72 DECLARE_BITMAP(system_vectors, NR_VECTORS); 73 74 static inline void cond_local_irq_enable(struct pt_regs *regs) 75 { 76 if (regs->flags & X86_EFLAGS_IF) 77 local_irq_enable(); 78 } 79 80 static inline void cond_local_irq_disable(struct pt_regs *regs) 81 { 82 if (regs->flags & X86_EFLAGS_IF) 83 local_irq_disable(); 84 } 85 86 /* 87 * In IST context, we explicitly disable preemption. This serves two 88 * purposes: it makes it much less likely that we would accidentally 89 * schedule in IST context and it will force a warning if we somehow 90 * manage to schedule by accident. 91 */ 92 void ist_enter(struct pt_regs *regs) 93 { 94 if (user_mode(regs)) { 95 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 96 } else { 97 /* 98 * We might have interrupted pretty much anything. In 99 * fact, if we're a machine check, we can even interrupt 100 * NMI processing. We don't want in_nmi() to return true, 101 * but we need to notify RCU. 102 */ 103 rcu_nmi_enter(); 104 } 105 106 preempt_disable(); 107 108 /* This code is a bit fragile. Test it. */ 109 RCU_LOCKDEP_WARN(!rcu_is_watching(), "ist_enter didn't work"); 110 } 111 NOKPROBE_SYMBOL(ist_enter); 112 113 void ist_exit(struct pt_regs *regs) 114 { 115 preempt_enable_no_resched(); 116 117 if (!user_mode(regs)) 118 rcu_nmi_exit(); 119 } 120 121 /** 122 * ist_begin_non_atomic() - begin a non-atomic section in an IST exception 123 * @regs: regs passed to the IST exception handler 124 * 125 * IST exception handlers normally cannot schedule. As a special 126 * exception, if the exception interrupted userspace code (i.e. 127 * user_mode(regs) would return true) and the exception was not 128 * a double fault, it can be safe to schedule. ist_begin_non_atomic() 129 * begins a non-atomic section within an ist_enter()/ist_exit() region. 130 * Callers are responsible for enabling interrupts themselves inside 131 * the non-atomic section, and callers must call ist_end_non_atomic() 132 * before ist_exit(). 133 */ 134 void ist_begin_non_atomic(struct pt_regs *regs) 135 { 136 BUG_ON(!user_mode(regs)); 137 138 /* 139 * Sanity check: we need to be on the normal thread stack. This 140 * will catch asm bugs and any attempt to use ist_preempt_enable 141 * from double_fault. 142 */ 143 BUG_ON(!on_thread_stack()); 144 145 preempt_enable_no_resched(); 146 } 147 148 /** 149 * ist_end_non_atomic() - begin a non-atomic section in an IST exception 150 * 151 * Ends a non-atomic section started with ist_begin_non_atomic(). 152 */ 153 void ist_end_non_atomic(void) 154 { 155 preempt_disable(); 156 } 157 158 int is_valid_bugaddr(unsigned long addr) 159 { 160 unsigned short ud; 161 162 if (addr < TASK_SIZE_MAX) 163 return 0; 164 165 if (probe_kernel_address((unsigned short *)addr, ud)) 166 return 0; 167 168 return ud == INSN_UD0 || ud == INSN_UD2; 169 } 170 171 int fixup_bug(struct pt_regs *regs, int trapnr) 172 { 173 if (trapnr != X86_TRAP_UD) 174 return 0; 175 176 switch (report_bug(regs->ip, regs)) { 177 case BUG_TRAP_TYPE_NONE: 178 case BUG_TRAP_TYPE_BUG: 179 break; 180 181 case BUG_TRAP_TYPE_WARN: 182 regs->ip += LEN_UD2; 183 return 1; 184 } 185 186 return 0; 187 } 188 189 static nokprobe_inline int 190 do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str, 191 struct pt_regs *regs, long error_code) 192 { 193 if (v8086_mode(regs)) { 194 /* 195 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86. 196 * On nmi (interrupt 2), do_trap should not be called. 197 */ 198 if (trapnr < X86_TRAP_UD) { 199 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs, 200 error_code, trapnr)) 201 return 0; 202 } 203 } else if (!user_mode(regs)) { 204 if (fixup_exception(regs, trapnr, error_code, 0)) 205 return 0; 206 207 tsk->thread.error_code = error_code; 208 tsk->thread.trap_nr = trapnr; 209 die(str, regs, error_code); 210 } 211 212 /* 213 * We want error_code and trap_nr set for userspace faults and 214 * kernelspace faults which result in die(), but not 215 * kernelspace faults which are fixed up. die() gives the 216 * process no chance to handle the signal and notice the 217 * kernel fault information, so that won't result in polluting 218 * the information about previously queued, but not yet 219 * delivered, faults. See also do_general_protection below. 220 */ 221 tsk->thread.error_code = error_code; 222 tsk->thread.trap_nr = trapnr; 223 224 return -1; 225 } 226 227 static void show_signal(struct task_struct *tsk, int signr, 228 const char *type, const char *desc, 229 struct pt_regs *regs, long error_code) 230 { 231 if (show_unhandled_signals && unhandled_signal(tsk, signr) && 232 printk_ratelimit()) { 233 pr_info("%s[%d] %s%s ip:%lx sp:%lx error:%lx", 234 tsk->comm, task_pid_nr(tsk), type, desc, 235 regs->ip, regs->sp, error_code); 236 print_vma_addr(KERN_CONT " in ", regs->ip); 237 pr_cont("\n"); 238 } 239 } 240 241 static void 242 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs, 243 long error_code, int sicode, void __user *addr) 244 { 245 struct task_struct *tsk = current; 246 247 248 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code)) 249 return; 250 251 show_signal(tsk, signr, "trap ", str, regs, error_code); 252 253 if (!sicode) 254 force_sig(signr); 255 else 256 force_sig_fault(signr, sicode, addr); 257 } 258 NOKPROBE_SYMBOL(do_trap); 259 260 static void do_error_trap(struct pt_regs *regs, long error_code, char *str, 261 unsigned long trapnr, int signr, int sicode, void __user *addr) 262 { 263 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 264 265 /* 266 * WARN*()s end up here; fix them up before we call the 267 * notifier chain. 268 */ 269 if (!user_mode(regs) && fixup_bug(regs, trapnr)) 270 return; 271 272 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) != 273 NOTIFY_STOP) { 274 cond_local_irq_enable(regs); 275 do_trap(trapnr, signr, str, regs, error_code, sicode, addr); 276 } 277 } 278 279 #define IP ((void __user *)uprobe_get_trap_addr(regs)) 280 #define DO_ERROR(trapnr, signr, sicode, addr, str, name) \ 281 dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \ 282 { \ 283 do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \ 284 } 285 286 DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error) 287 DO_ERROR(X86_TRAP_OF, SIGSEGV, 0, NULL, "overflow", overflow) 288 DO_ERROR(X86_TRAP_UD, SIGILL, ILL_ILLOPN, IP, "invalid opcode", invalid_op) 289 DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, 0, NULL, "coprocessor segment overrun", coprocessor_segment_overrun) 290 DO_ERROR(X86_TRAP_TS, SIGSEGV, 0, NULL, "invalid TSS", invalid_TSS) 291 DO_ERROR(X86_TRAP_NP, SIGBUS, 0, NULL, "segment not present", segment_not_present) 292 DO_ERROR(X86_TRAP_SS, SIGBUS, 0, NULL, "stack segment", stack_segment) 293 DO_ERROR(X86_TRAP_AC, SIGBUS, BUS_ADRALN, NULL, "alignment check", alignment_check) 294 #undef IP 295 296 #ifdef CONFIG_VMAP_STACK 297 __visible void __noreturn handle_stack_overflow(const char *message, 298 struct pt_regs *regs, 299 unsigned long fault_address) 300 { 301 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n", 302 (void *)fault_address, current->stack, 303 (char *)current->stack + THREAD_SIZE - 1); 304 die(message, regs, 0); 305 306 /* Be absolutely certain we don't return. */ 307 panic("%s", message); 308 } 309 #endif 310 311 #if defined(CONFIG_X86_64) || defined(CONFIG_DOUBLEFAULT) 312 /* 313 * Runs on an IST stack for x86_64 and on a special task stack for x86_32. 314 * 315 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the 316 * SDM's warnings about double faults being unrecoverable, returning works as 317 * expected. Presumably what the SDM actually means is that the CPU may get 318 * the register state wrong on entry, so returning could be a bad idea. 319 * 320 * Various CPU engineers have promised that double faults due to an IRET fault 321 * while the stack is read-only are, in fact, recoverable. 322 * 323 * On x86_32, this is entered through a task gate, and regs are synthesized 324 * from the TSS. Returning is, in principle, okay, but changes to regs will 325 * be lost. If, for some reason, we need to return to a context with modified 326 * regs, the shim code could be adjusted to synchronize the registers. 327 */ 328 dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2) 329 { 330 static const char str[] = "double fault"; 331 struct task_struct *tsk = current; 332 333 #ifdef CONFIG_X86_ESPFIX64 334 extern unsigned char native_irq_return_iret[]; 335 336 /* 337 * If IRET takes a non-IST fault on the espfix64 stack, then we 338 * end up promoting it to a doublefault. In that case, take 339 * advantage of the fact that we're not using the normal (TSS.sp0) 340 * stack right now. We can write a fake #GP(0) frame at TSS.sp0 341 * and then modify our own IRET frame so that, when we return, 342 * we land directly at the #GP(0) vector with the stack already 343 * set up according to its expectations. 344 * 345 * The net result is that our #GP handler will think that we 346 * entered from usermode with the bad user context. 347 * 348 * No need for ist_enter here because we don't use RCU. 349 */ 350 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY && 351 regs->cs == __KERNEL_CS && 352 regs->ip == (unsigned long)native_irq_return_iret) 353 { 354 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1; 355 356 /* 357 * regs->sp points to the failing IRET frame on the 358 * ESPFIX64 stack. Copy it to the entry stack. This fills 359 * in gpregs->ss through gpregs->ip. 360 * 361 */ 362 memmove(&gpregs->ip, (void *)regs->sp, 5*8); 363 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */ 364 365 /* 366 * Adjust our frame so that we return straight to the #GP 367 * vector with the expected RSP value. This is safe because 368 * we won't enable interupts or schedule before we invoke 369 * general_protection, so nothing will clobber the stack 370 * frame we just set up. 371 * 372 * We will enter general_protection with kernel GSBASE, 373 * which is what the stub expects, given that the faulting 374 * RIP will be the IRET instruction. 375 */ 376 regs->ip = (unsigned long)general_protection; 377 regs->sp = (unsigned long)&gpregs->orig_ax; 378 379 return; 380 } 381 #endif 382 383 ist_enter(regs); 384 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV); 385 386 tsk->thread.error_code = error_code; 387 tsk->thread.trap_nr = X86_TRAP_DF; 388 389 #ifdef CONFIG_VMAP_STACK 390 /* 391 * If we overflow the stack into a guard page, the CPU will fail 392 * to deliver #PF and will send #DF instead. Similarly, if we 393 * take any non-IST exception while too close to the bottom of 394 * the stack, the processor will get a page fault while 395 * delivering the exception and will generate a double fault. 396 * 397 * According to the SDM (footnote in 6.15 under "Interrupt 14 - 398 * Page-Fault Exception (#PF): 399 * 400 * Processors update CR2 whenever a page fault is detected. If a 401 * second page fault occurs while an earlier page fault is being 402 * delivered, the faulting linear address of the second fault will 403 * overwrite the contents of CR2 (replacing the previous 404 * address). These updates to CR2 occur even if the page fault 405 * results in a double fault or occurs during the delivery of a 406 * double fault. 407 * 408 * The logic below has a small possibility of incorrectly diagnosing 409 * some errors as stack overflows. For example, if the IDT or GDT 410 * gets corrupted such that #GP delivery fails due to a bad descriptor 411 * causing #GP and we hit this condition while CR2 coincidentally 412 * points to the stack guard page, we'll think we overflowed the 413 * stack. Given that we're going to panic one way or another 414 * if this happens, this isn't necessarily worth fixing. 415 * 416 * If necessary, we could improve the test by only diagnosing 417 * a stack overflow if the saved RSP points within 47 bytes of 418 * the bottom of the stack: if RSP == tsk_stack + 48 and we 419 * take an exception, the stack is already aligned and there 420 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a 421 * possible error code, so a stack overflow would *not* double 422 * fault. With any less space left, exception delivery could 423 * fail, and, as a practical matter, we've overflowed the 424 * stack even if the actual trigger for the double fault was 425 * something else. 426 */ 427 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE) 428 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2); 429 #endif 430 431 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code); 432 die("double fault", regs, error_code); 433 panic("Machine halted."); 434 } 435 #endif 436 437 dotraplinkage void do_bounds(struct pt_regs *regs, long error_code) 438 { 439 const struct mpx_bndcsr *bndcsr; 440 441 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 442 if (notify_die(DIE_TRAP, "bounds", regs, error_code, 443 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP) 444 return; 445 cond_local_irq_enable(regs); 446 447 if (!user_mode(regs)) 448 die("bounds", regs, error_code); 449 450 if (!cpu_feature_enabled(X86_FEATURE_MPX)) { 451 /* The exception is not from Intel MPX */ 452 goto exit_trap; 453 } 454 455 /* 456 * We need to look at BNDSTATUS to resolve this exception. 457 * A NULL here might mean that it is in its 'init state', 458 * which is all zeros which indicates MPX was not 459 * responsible for the exception. 460 */ 461 bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR); 462 if (!bndcsr) 463 goto exit_trap; 464 465 trace_bounds_exception_mpx(bndcsr); 466 /* 467 * The error code field of the BNDSTATUS register communicates status 468 * information of a bound range exception #BR or operation involving 469 * bound directory. 470 */ 471 switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) { 472 case 2: /* Bound directory has invalid entry. */ 473 if (mpx_handle_bd_fault()) 474 goto exit_trap; 475 break; /* Success, it was handled */ 476 case 1: /* Bound violation. */ 477 { 478 struct task_struct *tsk = current; 479 struct mpx_fault_info mpx; 480 481 if (mpx_fault_info(&mpx, regs)) { 482 /* 483 * We failed to decode the MPX instruction. Act as if 484 * the exception was not caused by MPX. 485 */ 486 goto exit_trap; 487 } 488 /* 489 * Success, we decoded the instruction and retrieved 490 * an 'mpx' containing the address being accessed 491 * which caused the exception. This information 492 * allows and application to possibly handle the 493 * #BR exception itself. 494 */ 495 if (!do_trap_no_signal(tsk, X86_TRAP_BR, "bounds", regs, 496 error_code)) 497 break; 498 499 show_signal(tsk, SIGSEGV, "trap ", "bounds", regs, error_code); 500 501 force_sig_bnderr(mpx.addr, mpx.lower, mpx.upper); 502 break; 503 } 504 case 0: /* No exception caused by Intel MPX operations. */ 505 goto exit_trap; 506 default: 507 die("bounds", regs, error_code); 508 } 509 510 return; 511 512 exit_trap: 513 /* 514 * This path out is for all the cases where we could not 515 * handle the exception in some way (like allocating a 516 * table or telling userspace about it. We will also end 517 * up here if the kernel has MPX turned off at compile 518 * time.. 519 */ 520 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, 0, NULL); 521 } 522 523 enum kernel_gp_hint { 524 GP_NO_HINT, 525 GP_NON_CANONICAL, 526 GP_CANONICAL 527 }; 528 529 /* 530 * When an uncaught #GP occurs, try to determine the memory address accessed by 531 * the instruction and return that address to the caller. Also, try to figure 532 * out whether any part of the access to that address was non-canonical. 533 */ 534 static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs, 535 unsigned long *addr) 536 { 537 u8 insn_buf[MAX_INSN_SIZE]; 538 struct insn insn; 539 540 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE)) 541 return GP_NO_HINT; 542 543 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE); 544 insn_get_modrm(&insn); 545 insn_get_sib(&insn); 546 547 *addr = (unsigned long)insn_get_addr_ref(&insn, regs); 548 if (*addr == -1UL) 549 return GP_NO_HINT; 550 551 #ifdef CONFIG_X86_64 552 /* 553 * Check that: 554 * - the operand is not in the kernel half 555 * - the last byte of the operand is not in the user canonical half 556 */ 557 if (*addr < ~__VIRTUAL_MASK && 558 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK) 559 return GP_NON_CANONICAL; 560 #endif 561 562 return GP_CANONICAL; 563 } 564 565 #define GPFSTR "general protection fault" 566 567 dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code) 568 { 569 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR; 570 enum kernel_gp_hint hint = GP_NO_HINT; 571 struct task_struct *tsk; 572 unsigned long gp_addr; 573 int ret; 574 575 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 576 cond_local_irq_enable(regs); 577 578 if (static_cpu_has(X86_FEATURE_UMIP)) { 579 if (user_mode(regs) && fixup_umip_exception(regs)) 580 return; 581 } 582 583 if (v8086_mode(regs)) { 584 local_irq_enable(); 585 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code); 586 return; 587 } 588 589 tsk = current; 590 591 if (user_mode(regs)) { 592 tsk->thread.error_code = error_code; 593 tsk->thread.trap_nr = X86_TRAP_GP; 594 595 show_signal(tsk, SIGSEGV, "", desc, regs, error_code); 596 force_sig(SIGSEGV); 597 598 return; 599 } 600 601 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0)) 602 return; 603 604 tsk->thread.error_code = error_code; 605 tsk->thread.trap_nr = X86_TRAP_GP; 606 607 /* 608 * To be potentially processing a kprobe fault and to trust the result 609 * from kprobe_running(), we have to be non-preemptible. 610 */ 611 if (!preemptible() && 612 kprobe_running() && 613 kprobe_fault_handler(regs, X86_TRAP_GP)) 614 return; 615 616 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV); 617 if (ret == NOTIFY_STOP) 618 return; 619 620 if (error_code) 621 snprintf(desc, sizeof(desc), "segment-related " GPFSTR); 622 else 623 hint = get_kernel_gp_address(regs, &gp_addr); 624 625 if (hint != GP_NO_HINT) 626 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx", 627 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address" 628 : "maybe for address", 629 gp_addr); 630 631 /* 632 * KASAN is interested only in the non-canonical case, clear it 633 * otherwise. 634 */ 635 if (hint != GP_NON_CANONICAL) 636 gp_addr = 0; 637 638 die_addr(desc, regs, error_code, gp_addr); 639 640 } 641 NOKPROBE_SYMBOL(do_general_protection); 642 643 dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code) 644 { 645 if (poke_int3_handler(regs)) 646 return; 647 648 /* 649 * Use ist_enter despite the fact that we don't use an IST stack. 650 * We can be called from a kprobe in non-CONTEXT_KERNEL kernel 651 * mode or even during context tracking state changes. 652 * 653 * This means that we can't schedule. That's okay. 654 */ 655 ist_enter(regs); 656 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 657 #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP 658 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP, 659 SIGTRAP) == NOTIFY_STOP) 660 goto exit; 661 #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */ 662 663 #ifdef CONFIG_KPROBES 664 if (kprobe_int3_handler(regs)) 665 goto exit; 666 #endif 667 668 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP, 669 SIGTRAP) == NOTIFY_STOP) 670 goto exit; 671 672 cond_local_irq_enable(regs); 673 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, 0, NULL); 674 cond_local_irq_disable(regs); 675 676 exit: 677 ist_exit(regs); 678 } 679 NOKPROBE_SYMBOL(do_int3); 680 681 #ifdef CONFIG_X86_64 682 /* 683 * Help handler running on a per-cpu (IST or entry trampoline) stack 684 * to switch to the normal thread stack if the interrupted code was in 685 * user mode. The actual stack switch is done in entry_64.S 686 */ 687 asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs) 688 { 689 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1; 690 if (regs != eregs) 691 *regs = *eregs; 692 return regs; 693 } 694 NOKPROBE_SYMBOL(sync_regs); 695 696 struct bad_iret_stack { 697 void *error_entry_ret; 698 struct pt_regs regs; 699 }; 700 701 asmlinkage __visible notrace 702 struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) 703 { 704 /* 705 * This is called from entry_64.S early in handling a fault 706 * caused by a bad iret to user mode. To handle the fault 707 * correctly, we want to move our stack frame to where it would 708 * be had we entered directly on the entry stack (rather than 709 * just below the IRET frame) and we want to pretend that the 710 * exception came from the IRET target. 711 */ 712 struct bad_iret_stack *new_stack = 713 (struct bad_iret_stack *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1; 714 715 /* Copy the IRET target to the new stack. */ 716 memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8); 717 718 /* Copy the remainder of the stack from the current stack. */ 719 memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip)); 720 721 BUG_ON(!user_mode(&new_stack->regs)); 722 return new_stack; 723 } 724 NOKPROBE_SYMBOL(fixup_bad_iret); 725 #endif 726 727 static bool is_sysenter_singlestep(struct pt_regs *regs) 728 { 729 /* 730 * We don't try for precision here. If we're anywhere in the region of 731 * code that can be single-stepped in the SYSENTER entry path, then 732 * assume that this is a useless single-step trap due to SYSENTER 733 * being invoked with TF set. (We don't know in advance exactly 734 * which instructions will be hit because BTF could plausibly 735 * be set.) 736 */ 737 #ifdef CONFIG_X86_32 738 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) < 739 (unsigned long)__end_SYSENTER_singlestep_region - 740 (unsigned long)__begin_SYSENTER_singlestep_region; 741 #elif defined(CONFIG_IA32_EMULATION) 742 return (regs->ip - (unsigned long)entry_SYSENTER_compat) < 743 (unsigned long)__end_entry_SYSENTER_compat - 744 (unsigned long)entry_SYSENTER_compat; 745 #else 746 return false; 747 #endif 748 } 749 750 /* 751 * Our handling of the processor debug registers is non-trivial. 752 * We do not clear them on entry and exit from the kernel. Therefore 753 * it is possible to get a watchpoint trap here from inside the kernel. 754 * However, the code in ./ptrace.c has ensured that the user can 755 * only set watchpoints on userspace addresses. Therefore the in-kernel 756 * watchpoint trap can only occur in code which is reading/writing 757 * from user space. Such code must not hold kernel locks (since it 758 * can equally take a page fault), therefore it is safe to call 759 * force_sig_info even though that claims and releases locks. 760 * 761 * Code in ./signal.c ensures that the debug control register 762 * is restored before we deliver any signal, and therefore that 763 * user code runs with the correct debug control register even though 764 * we clear it here. 765 * 766 * Being careful here means that we don't have to be as careful in a 767 * lot of more complicated places (task switching can be a bit lazy 768 * about restoring all the debug state, and ptrace doesn't have to 769 * find every occurrence of the TF bit that could be saved away even 770 * by user code) 771 * 772 * May run on IST stack. 773 */ 774 dotraplinkage void do_debug(struct pt_regs *regs, long error_code) 775 { 776 struct task_struct *tsk = current; 777 int user_icebp = 0; 778 unsigned long dr6; 779 int si_code; 780 781 ist_enter(regs); 782 783 get_debugreg(dr6, 6); 784 /* 785 * The Intel SDM says: 786 * 787 * Certain debug exceptions may clear bits 0-3. The remaining 788 * contents of the DR6 register are never cleared by the 789 * processor. To avoid confusion in identifying debug 790 * exceptions, debug handlers should clear the register before 791 * returning to the interrupted task. 792 * 793 * Keep it simple: clear DR6 immediately. 794 */ 795 set_debugreg(0, 6); 796 797 /* Filter out all the reserved bits which are preset to 1 */ 798 dr6 &= ~DR6_RESERVED; 799 800 /* 801 * The SDM says "The processor clears the BTF flag when it 802 * generates a debug exception." Clear TIF_BLOCKSTEP to keep 803 * TIF_BLOCKSTEP in sync with the hardware BTF flag. 804 */ 805 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP); 806 807 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) && 808 is_sysenter_singlestep(regs))) { 809 dr6 &= ~DR_STEP; 810 if (!dr6) 811 goto exit; 812 /* 813 * else we might have gotten a single-step trap and hit a 814 * watchpoint at the same time, in which case we should fall 815 * through and handle the watchpoint. 816 */ 817 } 818 819 /* 820 * If dr6 has no reason to give us about the origin of this trap, 821 * then it's very likely the result of an icebp/int01 trap. 822 * User wants a sigtrap for that. 823 */ 824 if (!dr6 && user_mode(regs)) 825 user_icebp = 1; 826 827 /* Store the virtualized DR6 value */ 828 tsk->thread.debugreg6 = dr6; 829 830 #ifdef CONFIG_KPROBES 831 if (kprobe_debug_handler(regs)) 832 goto exit; 833 #endif 834 835 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code, 836 SIGTRAP) == NOTIFY_STOP) 837 goto exit; 838 839 /* 840 * Let others (NMI) know that the debug stack is in use 841 * as we may switch to the interrupt stack. 842 */ 843 debug_stack_usage_inc(); 844 845 /* It's safe to allow irq's after DR6 has been saved */ 846 cond_local_irq_enable(regs); 847 848 if (v8086_mode(regs)) { 849 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 850 X86_TRAP_DB); 851 cond_local_irq_disable(regs); 852 debug_stack_usage_dec(); 853 goto exit; 854 } 855 856 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) { 857 /* 858 * Historical junk that used to handle SYSENTER single-stepping. 859 * This should be unreachable now. If we survive for a while 860 * without anyone hitting this warning, we'll turn this into 861 * an oops. 862 */ 863 tsk->thread.debugreg6 &= ~DR_STEP; 864 set_tsk_thread_flag(tsk, TIF_SINGLESTEP); 865 regs->flags &= ~X86_EFLAGS_TF; 866 } 867 si_code = get_si_code(tsk->thread.debugreg6); 868 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp) 869 send_sigtrap(regs, error_code, si_code); 870 cond_local_irq_disable(regs); 871 debug_stack_usage_dec(); 872 873 exit: 874 ist_exit(regs); 875 } 876 NOKPROBE_SYMBOL(do_debug); 877 878 /* 879 * Note that we play around with the 'TS' bit in an attempt to get 880 * the correct behaviour even in the presence of the asynchronous 881 * IRQ13 behaviour 882 */ 883 static void math_error(struct pt_regs *regs, int error_code, int trapnr) 884 { 885 struct task_struct *task = current; 886 struct fpu *fpu = &task->thread.fpu; 887 int si_code; 888 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" : 889 "simd exception"; 890 891 cond_local_irq_enable(regs); 892 893 if (!user_mode(regs)) { 894 if (fixup_exception(regs, trapnr, error_code, 0)) 895 return; 896 897 task->thread.error_code = error_code; 898 task->thread.trap_nr = trapnr; 899 900 if (notify_die(DIE_TRAP, str, regs, error_code, 901 trapnr, SIGFPE) != NOTIFY_STOP) 902 die(str, regs, error_code); 903 return; 904 } 905 906 /* 907 * Save the info for the exception handler and clear the error. 908 */ 909 fpu__save(fpu); 910 911 task->thread.trap_nr = trapnr; 912 task->thread.error_code = error_code; 913 914 si_code = fpu__exception_code(fpu, trapnr); 915 /* Retry when we get spurious exceptions: */ 916 if (!si_code) 917 return; 918 919 force_sig_fault(SIGFPE, si_code, 920 (void __user *)uprobe_get_trap_addr(regs)); 921 } 922 923 dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code) 924 { 925 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 926 math_error(regs, error_code, X86_TRAP_MF); 927 } 928 929 dotraplinkage void 930 do_simd_coprocessor_error(struct pt_regs *regs, long error_code) 931 { 932 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 933 math_error(regs, error_code, X86_TRAP_XF); 934 } 935 936 dotraplinkage void 937 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code) 938 { 939 cond_local_irq_enable(regs); 940 } 941 942 dotraplinkage void 943 do_device_not_available(struct pt_regs *regs, long error_code) 944 { 945 unsigned long cr0 = read_cr0(); 946 947 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 948 949 #ifdef CONFIG_MATH_EMULATION 950 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) { 951 struct math_emu_info info = { }; 952 953 cond_local_irq_enable(regs); 954 955 info.regs = regs; 956 math_emulate(&info); 957 return; 958 } 959 #endif 960 961 /* This should not happen. */ 962 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) { 963 /* Try to fix it up and carry on. */ 964 write_cr0(cr0 & ~X86_CR0_TS); 965 } else { 966 /* 967 * Something terrible happened, and we're better off trying 968 * to kill the task than getting stuck in a never-ending 969 * loop of #NM faults. 970 */ 971 die("unexpected #NM exception", regs, error_code); 972 } 973 } 974 NOKPROBE_SYMBOL(do_device_not_available); 975 976 #ifdef CONFIG_X86_32 977 dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code) 978 { 979 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); 980 local_irq_enable(); 981 982 if (notify_die(DIE_TRAP, "iret exception", regs, error_code, 983 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) { 984 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code, 985 ILL_BADSTK, (void __user *)NULL); 986 } 987 } 988 #endif 989 990 void __init trap_init(void) 991 { 992 /* Init cpu_entry_area before IST entries are set up */ 993 setup_cpu_entry_areas(); 994 995 idt_setup_traps(); 996 997 /* 998 * Set the IDT descriptor to a fixed read-only location, so that the 999 * "sidt" instruction will not leak the location of the kernel, and 1000 * to defend the IDT against arbitrary memory write vulnerabilities. 1001 * It will be reloaded in cpu_init() */ 1002 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table), 1003 PAGE_KERNEL_RO); 1004 idt_descr.address = CPU_ENTRY_AREA_RO_IDT; 1005 1006 /* 1007 * Should be a barrier for any external CPU state: 1008 */ 1009 cpu_init(); 1010 1011 idt_setup_ist_traps(); 1012 1013 x86_init.irqs.trap_init(); 1014 1015 idt_setup_debugidt_traps(); 1016 } 1017