blob: de5120e2fbe12664dba98978fd3de0549af6d82c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +02003 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 */
8
9/*
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020010 * Handle hardware traps and faults.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Joe Perchesc767a542012-05-21 19:50:07 -070012
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010015#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kallsyms.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010018#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kprobes.h>
Andrew Morton1e2af922006-09-27 01:51:15 -070020#include <linux/uaccess.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010021#include <linux/kdebug.h>
Jason Wesself503b5a2010-05-20 21:04:25 -050022#include <linux/kgdb.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010023#include <linux/kernel.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040024#include <linux/export.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010025#include <linux/ptrace.h>
Oleg Nesterovb02ef202014-05-12 18:24:45 +020026#include <linux/uprobes.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010027#include <linux/string.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010028#include <linux/delay.h>
29#include <linux/errno.h>
30#include <linux/kexec.h>
31#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010032#include <linux/sched/task_stack.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010033#include <linux/timer.h>
34#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080035#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010036#include <linux/nmi.h>
37#include <linux/mm.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020038#include <linux/smp.h>
39#include <linux/io.h>
Peter Zijlstra0d004492020-02-19 09:46:43 +010040#include <linux/hardirq.h>
41#include <linux/atomic.h>
42
Ingo Molnarb5964402008-02-26 11:15:50 +010043#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/debugreg.h>
Andy Lutomirski35de5b02016-04-26 12:23:24 -070046#include <asm/text-patching.h>
Steven Rostedt08d636b2011-08-16 09:57:10 -040047#include <asm/ftrace.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020048#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/desc.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020050#include <asm/fpu/internal.h>
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -080051#include <asm/cpu.h>
Thomas Gleixnered1bbc42017-12-20 18:28:54 +010052#include <asm/cpu_entry_area.h>
Hidetoshi Seto9e55e442009-06-15 17:22:15 +090053#include <asm/mce.h>
Kees Cook4eefbe72013-04-10 12:24:22 -070054#include <asm/fixmap.h>
Ingo Molnar1164dd02009-01-28 19:34:09 +010055#include <asm/mach_traps.h>
Jiri Kosina17f41572013-07-23 10:09:28 +020056#include <asm/alternative.h>
Dave Hansena84eeaa2015-06-07 11:37:01 -070057#include <asm/fpu/xstate.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040058#include <asm/vm86.h>
Ricardo Neri6fc9dc82017-11-05 18:27:55 -080059#include <asm/umip.h>
Jann Horn59c1dcb2019-12-19 00:11:48 +010060#include <asm/insn.h>
61#include <asm/insn-eval.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020062
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020063#ifdef CONFIG_X86_64
Thomas Gleixner428cf902009-08-20 10:35:46 +020064#include <asm/x86_init.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020065#include <asm/pgalloc.h>
66#include <asm/proto.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020067#else
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020068#include <asm/processor-flags.h>
Ingo Molnar8e6dafd2009-02-23 00:34:39 +010069#include <asm/setup.h>
Ingo Molnarb2502b42015-06-08 08:42:03 +020070#include <asm/proto.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Gleixner7854f822017-09-13 23:29:26 +020073DECLARE_BITMAP(system_vectors, NR_VECTORS);
Yinghai Lub77b8812008-12-19 15:23:44 -080074
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +010075static inline void cond_local_irq_enable(struct pt_regs *regs)
Alexander van Heukelum762db432008-09-09 21:55:55 +020076{
77 if (regs->flags & X86_EFLAGS_IF)
78 local_irq_enable();
79}
80
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +010081static inline void cond_local_irq_disable(struct pt_regs *regs)
Thomas Gleixnerbe716612009-01-13 23:36:34 +010082{
83 if (regs->flags & X86_EFLAGS_IF)
84 local_irq_disable();
85}
86
Peter Zijlstra9a938482017-02-02 14:43:51 +010087int 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
Peter Zijlstra8a524f82017-06-12 13:52:46 +0200100int fixup_bug(struct pt_regs *regs, int trapnr)
Peter Zijlstra9a938482017-02-02 14:43:51 +0100101{
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:
Peter Zijlstra3b3a3712018-02-09 13:16:59 +0100111 regs->ip += LEN_UD2;
Peter Zijlstra9a938482017-02-02 14:43:51 +0100112 return 1;
113 }
114
115 return 0;
116}
117
Masami Hiramatsu93266382014-04-17 17:18:14 +0900118static nokprobe_inline int
Eric W. Biederman79e21d62017-08-04 14:01:50 -0500119do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str,
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200120 struct pt_regs *regs, long error_code)
121{
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700122 if (v8086_mode(regs)) {
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200123 /*
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 }
Eric W. Biederman79e21d62017-08-04 14:01:50 -0500132 } else if (!user_mode(regs)) {
Jann Horn81fd9c12018-08-28 22:14:19 +0200133 if (fixup_exception(regs, trapnr, error_code, 0))
Peter Zijlstra9a938482017-02-02 14:43:51 +0100134 return 0;
135
Peter Zijlstra9a938482017-02-02 14:43:51 +0100136 tsk->thread.error_code = error_code;
137 tsk->thread.trap_nr = trapnr;
138 die(str, regs, error_code);
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200139 }
140
Ingo Molnarb5964402008-02-26 11:15:50 +0100141 /*
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530142 * We want error_code and trap_nr set for userspace faults and
Ingo Molnarb5964402008-02-26 11:15:50 +0100143 * 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
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100148 * delivered, faults. See also exc_general_protection below.
Ingo Molnarb5964402008-02-26 11:15:50 +0100149 */
150 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530151 tsk->thread.trap_nr = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100152
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800153 return -1;
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700154}
155
Eric W. Biederman6ace1092017-08-03 14:34:24 -0500156static 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{
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200160 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
161 printk_ratelimit()) {
Eric W. Biederman6ace1092017-08-03 14:34:24 -0500162 pr_info("%s[%d] %s%s ip:%lx sp:%lx error:%lx",
163 tsk->comm, task_pid_nr(tsk), type, desc,
Joe Perchesc767a542012-05-21 19:50:07 -0700164 regs->ip, regs->sp, error_code);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200165 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700166 pr_cont("\n");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200167 }
Eric W. Biederman6ace1092017-08-03 14:34:24 -0500168}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void
171do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500172 long error_code, int sicode, void __user *addr)
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700173{
174 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700176 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100177 return;
Ingo Molnarb5964402008-02-26 11:15:50 +0100178
Eric W. Biederman6ace1092017-08-03 14:34:24 -0500179 show_signal(tsk, signr, "trap ", str, regs, error_code);
Ingo Molnarb5964402008-02-26 11:15:50 +0100180
Eric W. Biederman164881b2018-04-16 14:29:39 -0500181 if (!sicode)
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500182 force_sig(signr);
Eric W. Biederman164881b2018-04-16 14:29:39 -0500183 else
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500184 force_sig_fault(signr, sicode, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900186NOKPROBE_SYMBOL(do_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Oleg Nesterovdff07962014-05-07 17:21:34 +0200188static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500189 unsigned long trapnr, int signr, int sicode, void __user *addr)
Oleg Nesterovdff07962014-05-07 17:21:34 +0200190{
Linus Torvalds57780772015-09-01 08:40:25 -0700191 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700192
Alexander Shishkinb8347c212017-07-24 13:04:28 +0300193 /*
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
Oleg Nesterovdff07962014-05-07 17:21:34 +0200200 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
201 NOTIFY_STOP) {
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100202 cond_local_irq_enable(regs);
Eric W. Biederman164881b2018-04-16 14:29:39 -0500203 do_trap(trapnr, signr, str, regs, error_code, sicode, addr);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200204 cond_local_irq_disable(regs);
Oleg Nesterovdff07962014-05-07 17:21:34 +0200205 }
Oleg Nesterovdff07962014-05-07 17:21:34 +0200206}
207
Thomas Gleixner218e31b2020-02-25 23:16:13 +0100208/*
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 */
218static __always_inline void __user *error_get_trap_addr(struct pt_regs *regs)
219{
220 return (void __user *)uprobe_get_trap_addr(regs);
221}
222
Thomas Gleixner9d06c402020-02-25 23:16:14 +0100223DEFINE_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
Thomas Gleixner4b6b9112020-02-25 23:16:15 +0100229DEFINE_IDTENTRY(exc_overflow)
230{
231 do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
232}
233
Thomas Gleixner49893c52020-02-25 23:16:18 +0100234#ifdef CONFIG_X86_F00F_BUG
235void handle_invalid_op(struct pt_regs *regs)
236#else
237static inline void handle_invalid_op(struct pt_regs *regs)
238#endif
239{
240 do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
241 ILL_ILLOPN, error_get_trap_addr(regs));
242}
243
244DEFINE_IDTENTRY(exc_invalid_op)
245{
246 handle_invalid_op(regs);
247}
248
Thomas Gleixnerf95658f2020-02-25 23:16:20 +0100249DEFINE_IDTENTRY(exc_coproc_segment_overrun)
250{
251 do_error_trap(regs, 0, "coprocessor segment overrun",
252 X86_TRAP_OLD_MF, SIGFPE, 0, NULL);
253}
254
Thomas Gleixner97b3d292020-02-25 23:16:22 +0100255DEFINE_IDTENTRY_ERRORCODE(exc_invalid_tss)
256{
257 do_error_trap(regs, error_code, "invalid TSS", X86_TRAP_TS, SIGSEGV,
258 0, NULL);
259}
260
Thomas Gleixner99a3fb82020-02-25 23:16:23 +0100261DEFINE_IDTENTRY_ERRORCODE(exc_segment_not_present)
262{
263 do_error_trap(regs, error_code, "segment not present", X86_TRAP_NP,
264 SIGBUS, 0, NULL);
265}
266
Thomas Gleixnerfd9689b2020-02-25 23:16:24 +0100267DEFINE_IDTENTRY_ERRORCODE(exc_stack_segment)
268{
269 do_error_trap(regs, error_code, "stack segment", X86_TRAP_SS, SIGBUS,
270 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Thomas Gleixner436608b2020-02-25 23:16:28 +0100273DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check)
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -0800274{
275 char *str = "alignment check";
276
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -0800277 if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
278 return;
279
280 if (!user_mode(regs))
281 die("Split lock detected\n", regs, error_code);
282
283 local_irq_enable();
284
285 if (handle_user_split_lock(regs, error_code))
286 return;
287
288 do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
289 error_code, BUS_ADRALN, NULL);
290}
291
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700292#ifdef CONFIG_VMAP_STACK
Andy Lutomirski6271cfd2016-08-30 17:27:57 -0700293__visible void __noreturn handle_stack_overflow(const char *message,
294 struct pt_regs *regs,
295 unsigned long fault_address)
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700296{
297 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
298 (void *)fault_address, current->stack,
299 (char *)current->stack + THREAD_SIZE - 1);
300 die(message, regs, 0);
301
302 /* Be absolutely certain we don't return. */
Rasmus Villemoes2022cce2018-10-27 00:20:04 +0200303 panic("%s", message);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700304}
305#endif
306
Andy Lutomirski7d8d8cf2019-11-20 23:06:41 -0800307/*
308 * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
309 *
310 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
311 * SDM's warnings about double faults being unrecoverable, returning works as
312 * expected. Presumably what the SDM actually means is that the CPU may get
313 * the register state wrong on entry, so returning could be a bad idea.
314 *
315 * Various CPU engineers have promised that double faults due to an IRET fault
316 * while the stack is read-only are, in fact, recoverable.
317 *
318 * On x86_32, this is entered through a task gate, and regs are synthesized
319 * from the TSS. Returning is, in principle, okay, but changes to regs will
320 * be lost. If, for some reason, we need to return to a context with modified
321 * regs, the shim code could be adjusted to synchronize the registers.
322 */
Peter Zijlstraa0d14b82019-07-11 13:40:59 +0200323dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200324{
325 static const char str[] = "double fault";
326 struct task_struct *tsk = current;
327
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800328#ifdef CONFIG_X86_ESPFIX64
329 extern unsigned char native_irq_return_iret[];
330
331 /*
332 * If IRET takes a non-IST fault on the espfix64 stack, then we
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100333 * end up promoting it to a doublefault. In that case, take
334 * advantage of the fact that we're not using the normal (TSS.sp0)
335 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
336 * and then modify our own IRET frame so that, when we return,
337 * we land directly at the #GP(0) vector with the stack already
338 * set up according to its expectations.
339 *
340 * The net result is that our #GP handler will think that we
341 * entered from usermode with the bad user context.
Andy Lutomirski95927472014-11-19 17:41:09 -0800342 *
Peter Zijlstra0d004492020-02-19 09:46:43 +0100343 * No need for nmi_enter() here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800344 */
Andy Lutomirskic739f932017-12-12 07:56:36 -0800345 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800346 regs->cs == __KERNEL_CS &&
347 regs->ip == (unsigned long)native_irq_return_iret)
348 {
Andy Lutomirskic482fee2017-12-04 15:07:29 +0100349 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Peter Zijlstrae9660392020-02-20 13:17:27 +0100350 unsigned long *p = (unsigned long *)regs->sp;
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800351
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100352 /*
353 * regs->sp points to the failing IRET frame on the
354 * ESPFIX64 stack. Copy it to the entry stack. This fills
355 * in gpregs->ss through gpregs->ip.
356 *
357 */
Peter Zijlstrae9660392020-02-20 13:17:27 +0100358 gpregs->ip = p[0];
359 gpregs->cs = p[1];
360 gpregs->flags = p[2];
361 gpregs->sp = p[3];
362 gpregs->ss = p[4];
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100363 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.
Andy Lutomirskibd7b1f72018-09-03 15:59:42 -0700371 *
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.
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100375 */
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100376 regs->ip = (unsigned long)asm_exc_general_protection;
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100377 regs->sp = (unsigned long)&gpregs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800378
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800379 return;
380 }
381#endif
382
Peter Zijlstra0d004492020-02-19 09:46:43 +0100383 nmi_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800384 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200385
386 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530387 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200388
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700389#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
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100402 * delivered, the faulting linear address of the second fault will
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700403 * 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 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700427 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
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800431 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
Andy Lutomirski0337b7e2019-11-25 22:37:44 -0800432 die("double fault", regs, error_code);
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800433 panic("Machine halted.");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200434}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200435
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100436DEFINE_IDTENTRY(exc_bounds)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800437{
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100438 if (notify_die(DIE_TRAP, "bounds", regs, 0,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800439 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700440 return;
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100441 cond_local_irq_enable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800442
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700443 if (!user_mode(regs))
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100444 die("bounds", regs, 0);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800445
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100446 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200447
448 cond_local_irq_disable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800449}
450
Jann Horn59c1dcb2019-12-19 00:11:48 +0100451enum kernel_gp_hint {
452 GP_NO_HINT,
453 GP_NON_CANONICAL,
454 GP_CANONICAL
455};
456
457/*
458 * When an uncaught #GP occurs, try to determine the memory address accessed by
459 * the instruction and return that address to the caller. Also, try to figure
460 * out whether any part of the access to that address was non-canonical.
461 */
462static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
463 unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jann Horn59c1dcb2019-12-19 00:11:48 +0100465 u8 insn_buf[MAX_INSN_SIZE];
466 struct insn insn;
467
468 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
469 return GP_NO_HINT;
470
471 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
472 insn_get_modrm(&insn);
473 insn_get_sib(&insn);
474
475 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
476 if (*addr == -1UL)
477 return GP_NO_HINT;
478
479#ifdef CONFIG_X86_64
480 /*
481 * Check that:
482 * - the operand is not in the kernel half
483 * - the last byte of the operand is not in the user canonical half
484 */
485 if (*addr < ~__VIRTUAL_MASK &&
486 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
487 return GP_NON_CANONICAL;
488#endif
489
490 return GP_CANONICAL;
491}
492
493#define GPFSTR "general protection fault"
494
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100495DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
Jann Horn59c1dcb2019-12-19 00:11:48 +0100496{
497 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
Borislav Petkov36209762019-12-31 17:15:35 +0100498 enum kernel_gp_hint hint = GP_NO_HINT;
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200499 struct task_struct *tsk;
Borislav Petkov36209762019-12-31 17:15:35 +0100500 unsigned long gp_addr;
501 int ret;
Ingo Molnarb5964402008-02-26 11:15:50 +0100502
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100503 cond_local_irq_enable(regs);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200504
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800505 if (static_cpu_has(X86_FEATURE_UMIP)) {
506 if (user_mode(regs) && fixup_umip_exception(regs))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200507 goto exit;
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800508 }
509
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700510 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200511 local_irq_enable();
512 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200513 local_irq_disable();
Andy Lutomirski8c840142015-07-03 12:44:32 -0700514 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200517 tsk = current;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200518
Borislav Petkov36209762019-12-31 17:15:35 +0100519 if (user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200520 tsk->thread.error_code = error_code;
521 tsk->thread.trap_nr = X86_TRAP_GP;
Jann Horn76dee4a2018-08-28 22:14:16 +0200522
Borislav Petkov36209762019-12-31 17:15:35 +0100523 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
524 force_sig(SIGSEGV);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200525 goto exit;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Borislav Petkov36209762019-12-31 17:15:35 +0100528 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200529 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100530
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200531 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530532 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100533
Borislav Petkov36209762019-12-31 17:15:35 +0100534 /*
535 * To be potentially processing a kprobe fault and to trust the result
536 * from kprobe_running(), we have to be non-preemptible.
537 */
538 if (!preemptible() &&
539 kprobe_running() &&
540 kprobe_fault_handler(regs, X86_TRAP_GP))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200541 goto exit;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200542
Borislav Petkov36209762019-12-31 17:15:35 +0100543 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
544 if (ret == NOTIFY_STOP)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200545 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100546
547 if (error_code)
548 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
549 else
550 hint = get_kernel_gp_address(regs, &gp_addr);
551
552 if (hint != GP_NO_HINT)
553 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
554 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
555 : "maybe for address",
556 gp_addr);
557
558 /*
559 * KASAN is interested only in the non-canonical case, clear it
560 * otherwise.
561 */
562 if (hint != GP_NON_CANONICAL)
563 gp_addr = 0;
564
565 die_addr(desc, regs, error_code, gp_addr);
566
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200567exit:
568 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Peter Zijlstra21e28292020-03-05 16:09:52 +0100571static bool do_int3(struct pt_regs *regs)
572{
573 int res;
574
575#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
576 if (kgdb_ll_trap(DIE_INT3, "int3", regs, 0, X86_TRAP_BP,
577 SIGTRAP) == NOTIFY_STOP)
578 return true;
579#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
580
581#ifdef CONFIG_KPROBES
582 if (kprobe_int3_handler(regs))
583 return true;
584#endif
585 res = notify_die(DIE_INT3, "int3", regs, 0, X86_TRAP_BP, SIGTRAP);
586
587 return res == NOTIFY_STOP;
588}
589
590static void do_int3_user(struct pt_regs *regs)
591{
592 if (do_int3(regs))
593 return;
594
595 cond_local_irq_enable(regs);
596 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, 0, 0, NULL);
597 cond_local_irq_disable(regs);
598}
599
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100600DEFINE_IDTENTRY_RAW(exc_int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Peter Zijlstraf64366e2020-02-20 13:28:06 +0100602 /*
603 * poke_int3_handler() is completely self contained code; it does (and
604 * must) *NOT* call out to anything, lest it hits upon yet another
605 * INT3.
606 */
Jiri Kosina17f41572013-07-23 10:09:28 +0200607 if (poke_int3_handler(regs))
608 return;
609
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700610 /*
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100611 * idtentry_enter() uses static_branch_{,un}likely() and therefore
612 * can trigger INT3, hence poke_int3_handler() must be done
613 * before. If the entry came from kernel mode, then use nmi_enter()
614 * because the INT3 could have been hit in any context including
615 * NMI.
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700616 */
Peter Zijlstra21e28292020-03-05 16:09:52 +0100617 if (user_mode(regs)) {
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100618 idtentry_enter(regs);
Peter Zijlstra21e28292020-03-05 16:09:52 +0100619 instrumentation_begin();
620 do_int3_user(regs);
621 instrumentation_end();
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100622 idtentry_exit(regs);
Peter Zijlstra21e28292020-03-05 16:09:52 +0100623 } else {
624 nmi_enter();
625 instrumentation_begin();
626 if (!do_int3(regs))
627 die("int3", regs, 0);
628 instrumentation_end();
Peter Zijlstra0d004492020-02-19 09:46:43 +0100629 nmi_exit();
Peter Zijlstra21e28292020-03-05 16:09:52 +0100630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200633#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100634/*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100635 * Help handler running on a per-cpu (IST or entry trampoline) stack
636 * to switch to the normal thread stack if the interrupted code was in
637 * user mode. The actual stack switch is done in entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100638 */
Thomas Gleixnerdaf7a692020-03-25 23:47:51 +0100639asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200640{
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100641 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
642 if (regs != eregs)
643 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200644 return regs;
645}
Andy Lutomirskib645af22014-11-22 18:00:33 -0800646
647struct bad_iret_stack {
648 void *error_entry_ret;
649 struct pt_regs regs;
650};
651
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100652asmlinkage __visible noinstr
Andy Lutomirskib645af22014-11-22 18:00:33 -0800653struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
654{
655 /*
656 * This is called from entry_64.S early in handling a fault
657 * caused by a bad iret to user mode. To handle the fault
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100658 * correctly, we want to move our stack frame to where it would
659 * be had we entered directly on the entry stack (rather than
660 * just below the IRET frame) and we want to pretend that the
661 * exception came from the IRET target.
Andy Lutomirskib645af22014-11-22 18:00:33 -0800662 */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100663 struct bad_iret_stack tmp, *new_stack =
664 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Andy Lutomirskib645af22014-11-22 18:00:33 -0800665
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100666 /* Copy the IRET target to the temporary storage. */
667 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800668
669 /* Copy the remainder of the stack from the current stack. */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100670 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
671
672 /* Update the entry stack */
673 memcpy(new_stack, &tmp, sizeof(tmp));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800674
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700675 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800676 return new_stack;
677}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200678#endif
679
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800680static bool is_sysenter_singlestep(struct pt_regs *regs)
681{
682 /*
683 * We don't try for precision here. If we're anywhere in the region of
684 * code that can be single-stepped in the SYSENTER entry path, then
685 * assume that this is a useless single-step trap due to SYSENTER
686 * being invoked with TF set. (We don't know in advance exactly
687 * which instructions will be hit because BTF could plausibly
688 * be set.)
689 */
690#ifdef CONFIG_X86_32
691 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
692 (unsigned long)__end_SYSENTER_singlestep_region -
693 (unsigned long)__begin_SYSENTER_singlestep_region;
694#elif defined(CONFIG_IA32_EMULATION)
695 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
696 (unsigned long)__end_entry_SYSENTER_compat -
697 (unsigned long)entry_SYSENTER_compat;
698#else
699 return false;
700#endif
701}
702
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200703static __always_inline void debug_enter(unsigned long *dr6, unsigned long *dr7)
704{
705 /*
706 * Disable breakpoints during exception handling; recursive exceptions
707 * are exceedingly 'fun'.
708 *
709 * Since this function is NOKPROBE, and that also applies to
710 * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a
711 * HW_BREAKPOINT_W on our stack)
712 *
713 * Entry text is excluded for HW_BP_X and cpu_entry_area, which
714 * includes the entry stack is excluded for everything.
715 */
716 get_debugreg(*dr7, 7);
717 set_debugreg(0, 7);
718
719 /*
720 * Ensure the compiler doesn't lower the above statements into
721 * the critical section; disabling breakpoints late would not
722 * be good.
723 */
724 barrier();
725
726 /*
727 * The Intel SDM says:
728 *
729 * Certain debug exceptions may clear bits 0-3. The remaining
730 * contents of the DR6 register are never cleared by the
731 * processor. To avoid confusion in identifying debug
732 * exceptions, debug handlers should clear the register before
733 * returning to the interrupted task.
734 *
735 * Keep it simple: clear DR6 immediately.
736 */
737 get_debugreg(*dr6, 6);
738 set_debugreg(0, 6);
739 /* Filter out all the reserved bits which are preset to 1 */
740 *dr6 &= ~DR6_RESERVED;
741}
742
743static __always_inline void debug_exit(unsigned long dr7)
744{
745 /*
746 * Ensure the compiler doesn't raise this statement into
747 * the critical section; enabling breakpoints early would
748 * not be good.
749 */
750 barrier();
751 set_debugreg(dr7, 7);
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/*
755 * Our handling of the processor debug registers is non-trivial.
756 * We do not clear them on entry and exit from the kernel. Therefore
757 * it is possible to get a watchpoint trap here from inside the kernel.
758 * However, the code in ./ptrace.c has ensured that the user can
759 * only set watchpoints on userspace addresses. Therefore the in-kernel
760 * watchpoint trap can only occur in code which is reading/writing
761 * from user space. Such code must not hold kernel locks (since it
762 * can equally take a page fault), therefore it is safe to call
763 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * Code in ./signal.c ensures that the debug control register
766 * is restored before we deliver any signal, and therefore that
767 * user code runs with the correct debug control register even though
768 * we clear it here.
769 *
770 * Being careful here means that we don't have to be as careful in a
771 * lot of more complicated places (task switching can be a bit lazy
772 * about restoring all the debug state, and ptrace doesn't have to
773 * find every occurrence of the TF bit that could be saved away even
774 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200775 *
776 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900778dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct task_struct *tsk = current;
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200781 unsigned long dr6, dr7;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200782 int user_icebp = 0;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530783 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200785 debug_enter(&dr6, &dr7);
786
Peter Zijlstra0d004492020-02-19 09:46:43 +0100787 nmi_enter();
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000788
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200789 /*
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800790 * The SDM says "The processor clears the BTF flag when it
791 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
792 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
793 */
794 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
795
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800796 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
797 is_sysenter_singlestep(regs))) {
798 dr6 &= ~DR_STEP;
799 if (!dr6)
800 goto exit;
801 /*
802 * else we might have gotten a single-step trap and hit a
803 * watchpoint at the same time, in which case we should fall
804 * through and handle the watchpoint.
805 */
806 }
807
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800808 /*
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200809 * If dr6 has no reason to give us about the origin of this trap,
810 * then it's very likely the result of an icebp/int01 trap.
811 * User wants a sigtrap for that.
812 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700813 if (!dr6 && user_mode(regs))
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200814 user_icebp = 1;
815
K.Prasad08d68322009-06-01 23:44:08 +0530816 /* Store the virtualized DR6 value */
817 tsk->thread.debugreg6 = dr6;
818
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900819#ifdef CONFIG_KPROBES
820 if (kprobe_debug_handler(regs))
821 goto exit;
822#endif
823
Rusty Russell5a802e12013-06-16 14:12:47 +0930824 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200825 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200826 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200827
Steven Rostedt42181182011-12-16 11:43:02 -0500828 /*
829 * Let others (NMI) know that the debug stack is in use
830 * as we may switch to the interrupt stack.
831 */
832 debug_stack_usage_inc();
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* It's safe to allow irq's after DR6 has been saved */
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100835 cond_local_irq_enable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700837 if (v8086_mode(regs)) {
Kees Cookc9408262012-03-09 16:07:10 -0800838 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
839 X86_TRAP_DB);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100840 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500841 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200842 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800845 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
846 /*
847 * Historical junk that used to handle SYSENTER single-stepping.
848 * This should be unreachable now. If we survive for a while
849 * without anyone hitting this warning, we'll turn this into
850 * an oops.
851 */
K.Prasad08d68322009-06-01 23:44:08 +0530852 tsk->thread.debugreg6 &= ~DR_STEP;
853 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
854 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
K.Prasad08d68322009-06-01 23:44:08 +0530856 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200857 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
Eric W. Biederman28d42ea2019-02-05 18:25:11 -0600858 send_sigtrap(regs, error_code, si_code);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100859 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500860 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200862exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100863 nmi_exit();
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200864 debug_exit(dr7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900866NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868/*
869 * Note that we play around with the 'TS' bit in an attempt to get
870 * the correct behaviour even in the presence of the asynchronous
871 * IRQ13 behaviour
872 */
Thomas Gleixner48227e22020-02-25 23:16:29 +0100873static void math_error(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Brian Gerste2e75c912010-03-21 09:00:45 -0400875 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200876 struct fpu *fpu = &task->thread.fpu;
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200877 int si_code;
Kees Cookc9408262012-03-09 16:07:10 -0800878 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
879 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400880
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100881 cond_local_irq_enable(regs);
Brian Gerste2e75c912010-03-21 09:00:45 -0400882
Ingo Molnare1cebad2015-04-30 09:29:38 +0200883 if (!user_mode(regs)) {
Thomas Gleixner48227e22020-02-25 23:16:29 +0100884 if (fixup_exception(regs, trapnr, 0, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200885 goto exit;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000886
Thomas Gleixner48227e22020-02-25 23:16:29 +0100887 task->thread.error_code = 0;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000888 task->thread.trap_nr = trapnr;
889
Thomas Gleixner48227e22020-02-25 23:16:29 +0100890 if (notify_die(DIE_TRAP, str, regs, 0, trapnr,
891 SIGFPE) != NOTIFY_STOP)
892 die(str, regs, 0);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200893 goto exit;
Brian Gerste2e75c912010-03-21 09:00:45 -0400894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
897 * Save the info for the exception handler and clear the error.
898 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200899 fpu__save(fpu);
900
901 task->thread.trap_nr = trapnr;
Thomas Gleixner48227e22020-02-25 23:16:29 +0100902 task->thread.error_code = 0;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800903
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200904 si_code = fpu__exception_code(fpu, trapnr);
Ingo Molnare1cebad2015-04-30 09:29:38 +0200905 /* Retry when we get spurious exceptions: */
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200906 if (!si_code)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200907 goto exit;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200908
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200909 force_sig_fault(SIGFPE, si_code,
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500910 (void __user *)uprobe_get_trap_addr(regs));
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200911exit:
912 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Thomas Gleixner14a8bd22020-02-25 23:16:27 +0100915DEFINE_IDTENTRY(exc_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Thomas Gleixner48227e22020-02-25 23:16:29 +0100917 math_error(regs, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Thomas Gleixner48227e22020-02-25 23:16:29 +0100920DEFINE_IDTENTRY(exc_simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Thomas Gleixner48227e22020-02-25 23:16:29 +0100922 if (IS_ENABLED(CONFIG_X86_INVD_BUG)) {
923 /* AMD 486 bug: INVD in CPL 0 raises #XF instead of #GP */
924 if (!static_cpu_has(X86_FEATURE_XMM)) {
925 __exc_general_protection(regs, 0);
926 return;
927 }
928 }
929 math_error(regs, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Thomas Gleixnerdad71062020-02-25 23:16:26 +0100932DEFINE_IDTENTRY(exc_spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Thomas Gleixnerd244d0e2020-02-25 22:36:41 +0100934 /*
935 * This addresses a Pentium Pro Erratum:
936 *
937 * PROBLEM: If the APIC subsystem is configured in mixed mode with
938 * Virtual Wire mode implemented through the local APIC, an
939 * interrupt vector of 0Fh (Intel reserved encoding) may be
940 * generated by the local APIC (Int 15). This vector may be
941 * generated upon receipt of a spurious interrupt (an interrupt
942 * which is removed before the system receives the INTA sequence)
943 * instead of the programmed 8259 spurious interrupt vector.
944 *
945 * IMPLICATION: The spurious interrupt vector programmed in the
946 * 8259 is normally handled by an operating system's spurious
947 * interrupt handler. However, a vector of 0Fh is unknown to some
948 * operating systems, which would crash if this erratum occurred.
949 *
950 * In theory this could be limited to 32bit, but the handler is not
951 * hurting and who knows which other CPUs suffer from this.
952 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200953}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100955DEFINE_IDTENTRY(exc_device_not_available)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200956{
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100957 unsigned long cr0 = read_cr0();
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700958
Brian Gersta334fe42010-09-03 21:17:15 -0400959#ifdef CONFIG_MATH_EMULATION
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100960 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
Tejun Heod3157602009-02-09 22:17:39 +0900961 struct math_emu_info info = { };
962
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100963 cond_local_irq_enable(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900964
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500965 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900966 math_emulate(&info);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200967
968 cond_local_irq_disable(regs);
Brian Gersta334fe42010-09-03 21:17:15 -0400969 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200970 }
Brian Gersta334fe42010-09-03 21:17:15 -0400971#endif
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700972
973 /* This should not happen. */
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700974 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
975 /* Try to fix it up and carry on. */
976 write_cr0(cr0 & ~X86_CR0_TS);
977 } else {
978 /*
979 * Something terrible happened, and we're better off trying
980 * to kill the task than getting stuck in a never-ending
981 * loop of #NM faults.
982 */
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100983 die("unexpected #NM exception", regs, 0);
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700984 }
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200985}
986
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200987#ifdef CONFIG_X86_32
Thomas Gleixnerd7729052020-02-25 23:16:30 +0100988DEFINE_IDTENTRY_SW(iret_error)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200989{
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200990 local_irq_enable();
Thomas Gleixnerd7729052020-02-25 23:16:30 +0100991 if (notify_die(DIE_TRAP, "iret exception", regs, 0,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200992 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
Thomas Gleixnerd7729052020-02-25 23:16:30 +0100993 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, 0,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500994 ILL_BADSTK, (void __user *)NULL);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200995 }
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200996 local_irq_disable();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200997}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200998#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000void __init trap_init(void)
1001{
Andy Lutomirski40e7f942017-12-04 15:07:26 +01001002 /* Init cpu_entry_area before IST entries are set up */
1003 setup_cpu_entry_areas();
1004
Thomas Gleixnerb70543a2017-08-28 08:47:53 +02001005 idt_setup_traps();
Yinghai Lubb3f0b52009-01-25 02:38:09 -08001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
Kees Cook4eefbe72013-04-10 12:24:22 -07001008 * Set the IDT descriptor to a fixed read-only location, so that the
1009 * "sidt" instruction will not leak the location of the kernel, and
1010 * to defend the IDT against arbitrary memory write vulnerabilities.
1011 * It will be reloaded in cpu_init() */
Thomas Gleixner92a0f812017-12-20 18:51:31 +01001012 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
1013 PAGE_KERNEL_RO);
1014 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
Kees Cook4eefbe72013-04-10 12:24:22 -07001015
1016 /*
Ingo Molnarb5964402008-02-26 11:15:50 +01001017 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
1019 cpu_init();
1020
Thomas Gleixner90f62252017-08-28 08:47:52 +02001021 idt_setup_ist_traps();
Wang Nanb4d83272015-02-26 13:49:39 +08001022
Thomas Gleixner0a309082017-08-28 08:47:51 +02001023 idt_setup_debugidt_traps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}