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