blob: ba26bebfed7263a0051b8c4ce945fc884d096b65 [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
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -0800273dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code)
274{
275 char *str = "alignment check";
276
277 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
278
279 if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
280 return;
281
282 if (!user_mode(regs))
283 die("Split lock detected\n", regs, error_code);
284
285 local_irq_enable();
286
287 if (handle_user_split_lock(regs, error_code))
288 return;
289
290 do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
291 error_code, BUS_ADRALN, NULL);
292}
293
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700294#ifdef CONFIG_VMAP_STACK
Andy Lutomirski6271cfd2016-08-30 17:27:57 -0700295__visible void __noreturn handle_stack_overflow(const char *message,
296 struct pt_regs *regs,
297 unsigned long fault_address)
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700298{
299 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
300 (void *)fault_address, current->stack,
301 (char *)current->stack + THREAD_SIZE - 1);
302 die(message, regs, 0);
303
304 /* Be absolutely certain we don't return. */
Rasmus Villemoes2022cce2018-10-27 00:20:04 +0200305 panic("%s", message);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700306}
307#endif
308
Andy Lutomirski7d8d8cf2019-11-20 23:06:41 -0800309/*
310 * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
311 *
312 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
313 * SDM's warnings about double faults being unrecoverable, returning works as
314 * expected. Presumably what the SDM actually means is that the CPU may get
315 * the register state wrong on entry, so returning could be a bad idea.
316 *
317 * Various CPU engineers have promised that double faults due to an IRET fault
318 * while the stack is read-only are, in fact, recoverable.
319 *
320 * On x86_32, this is entered through a task gate, and regs are synthesized
321 * from the TSS. Returning is, in principle, okay, but changes to regs will
322 * be lost. If, for some reason, we need to return to a context with modified
323 * regs, the shim code could be adjusted to synchronize the registers.
324 */
Peter Zijlstraa0d14b82019-07-11 13:40:59 +0200325dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200326{
327 static const char str[] = "double fault";
328 struct task_struct *tsk = current;
329
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800330#ifdef CONFIG_X86_ESPFIX64
331 extern unsigned char native_irq_return_iret[];
332
333 /*
334 * If IRET takes a non-IST fault on the espfix64 stack, then we
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100335 * end up promoting it to a doublefault. In that case, take
336 * advantage of the fact that we're not using the normal (TSS.sp0)
337 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
338 * and then modify our own IRET frame so that, when we return,
339 * we land directly at the #GP(0) vector with the stack already
340 * set up according to its expectations.
341 *
342 * The net result is that our #GP handler will think that we
343 * entered from usermode with the bad user context.
Andy Lutomirski95927472014-11-19 17:41:09 -0800344 *
Peter Zijlstra0d004492020-02-19 09:46:43 +0100345 * No need for nmi_enter() here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800346 */
Andy Lutomirskic739f932017-12-12 07:56:36 -0800347 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800348 regs->cs == __KERNEL_CS &&
349 regs->ip == (unsigned long)native_irq_return_iret)
350 {
Andy Lutomirskic482fee2017-12-04 15:07:29 +0100351 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Peter Zijlstrae9660392020-02-20 13:17:27 +0100352 unsigned long *p = (unsigned long *)regs->sp;
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800353
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100354 /*
355 * regs->sp points to the failing IRET frame on the
356 * ESPFIX64 stack. Copy it to the entry stack. This fills
357 * in gpregs->ss through gpregs->ip.
358 *
359 */
Peter Zijlstrae9660392020-02-20 13:17:27 +0100360 gpregs->ip = p[0];
361 gpregs->cs = p[1];
362 gpregs->flags = p[2];
363 gpregs->sp = p[3];
364 gpregs->ss = p[4];
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100365 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
366
367 /*
368 * Adjust our frame so that we return straight to the #GP
369 * vector with the expected RSP value. This is safe because
370 * we won't enable interupts or schedule before we invoke
371 * general_protection, so nothing will clobber the stack
372 * frame we just set up.
Andy Lutomirskibd7b1f72018-09-03 15:59:42 -0700373 *
374 * We will enter general_protection with kernel GSBASE,
375 * which is what the stub expects, given that the faulting
376 * RIP will be the IRET instruction.
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100377 */
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100378 regs->ip = (unsigned long)asm_exc_general_protection;
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100379 regs->sp = (unsigned long)&gpregs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800380
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800381 return;
382 }
383#endif
384
Peter Zijlstra0d004492020-02-19 09:46:43 +0100385 nmi_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800386 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200387
388 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530389 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200390
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700391#ifdef CONFIG_VMAP_STACK
392 /*
393 * If we overflow the stack into a guard page, the CPU will fail
394 * to deliver #PF and will send #DF instead. Similarly, if we
395 * take any non-IST exception while too close to the bottom of
396 * the stack, the processor will get a page fault while
397 * delivering the exception and will generate a double fault.
398 *
399 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
400 * Page-Fault Exception (#PF):
401 *
402 * Processors update CR2 whenever a page fault is detected. If a
403 * second page fault occurs while an earlier page fault is being
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100404 * delivered, the faulting linear address of the second fault will
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700405 * overwrite the contents of CR2 (replacing the previous
406 * address). These updates to CR2 occur even if the page fault
407 * results in a double fault or occurs during the delivery of a
408 * double fault.
409 *
410 * The logic below has a small possibility of incorrectly diagnosing
411 * some errors as stack overflows. For example, if the IDT or GDT
412 * gets corrupted such that #GP delivery fails due to a bad descriptor
413 * causing #GP and we hit this condition while CR2 coincidentally
414 * points to the stack guard page, we'll think we overflowed the
415 * stack. Given that we're going to panic one way or another
416 * if this happens, this isn't necessarily worth fixing.
417 *
418 * If necessary, we could improve the test by only diagnosing
419 * a stack overflow if the saved RSP points within 47 bytes of
420 * the bottom of the stack: if RSP == tsk_stack + 48 and we
421 * take an exception, the stack is already aligned and there
422 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
423 * possible error code, so a stack overflow would *not* double
424 * fault. With any less space left, exception delivery could
425 * fail, and, as a practical matter, we've overflowed the
426 * stack even if the actual trigger for the double fault was
427 * something else.
428 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700429 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
430 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
431#endif
432
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800433 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
Andy Lutomirski0337b7e2019-11-25 22:37:44 -0800434 die("double fault", regs, error_code);
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800435 panic("Machine halted.");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200436}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200437
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100438DEFINE_IDTENTRY(exc_bounds)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800439{
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100440 if (notify_die(DIE_TRAP, "bounds", regs, 0,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800441 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700442 return;
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100443 cond_local_irq_enable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800444
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700445 if (!user_mode(regs))
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100446 die("bounds", regs, 0);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800447
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100448 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200449
450 cond_local_irq_disable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800451}
452
Jann Horn59c1dcb2019-12-19 00:11:48 +0100453enum kernel_gp_hint {
454 GP_NO_HINT,
455 GP_NON_CANONICAL,
456 GP_CANONICAL
457};
458
459/*
460 * When an uncaught #GP occurs, try to determine the memory address accessed by
461 * the instruction and return that address to the caller. Also, try to figure
462 * out whether any part of the access to that address was non-canonical.
463 */
464static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
465 unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Jann Horn59c1dcb2019-12-19 00:11:48 +0100467 u8 insn_buf[MAX_INSN_SIZE];
468 struct insn insn;
469
470 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
471 return GP_NO_HINT;
472
473 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
474 insn_get_modrm(&insn);
475 insn_get_sib(&insn);
476
477 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
478 if (*addr == -1UL)
479 return GP_NO_HINT;
480
481#ifdef CONFIG_X86_64
482 /*
483 * Check that:
484 * - the operand is not in the kernel half
485 * - the last byte of the operand is not in the user canonical half
486 */
487 if (*addr < ~__VIRTUAL_MASK &&
488 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
489 return GP_NON_CANONICAL;
490#endif
491
492 return GP_CANONICAL;
493}
494
495#define GPFSTR "general protection fault"
496
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100497DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
Jann Horn59c1dcb2019-12-19 00:11:48 +0100498{
499 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
Borislav Petkov36209762019-12-31 17:15:35 +0100500 enum kernel_gp_hint hint = GP_NO_HINT;
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200501 struct task_struct *tsk;
Borislav Petkov36209762019-12-31 17:15:35 +0100502 unsigned long gp_addr;
503 int ret;
Ingo Molnarb5964402008-02-26 11:15:50 +0100504
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100505 cond_local_irq_enable(regs);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200506
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800507 if (static_cpu_has(X86_FEATURE_UMIP)) {
508 if (user_mode(regs) && fixup_umip_exception(regs))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200509 goto exit;
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800510 }
511
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700512 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200513 local_irq_enable();
514 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200515 local_irq_disable();
Andy Lutomirski8c840142015-07-03 12:44:32 -0700516 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200519 tsk = current;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200520
Borislav Petkov36209762019-12-31 17:15:35 +0100521 if (user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200522 tsk->thread.error_code = error_code;
523 tsk->thread.trap_nr = X86_TRAP_GP;
Jann Horn76dee4a2018-08-28 22:14:16 +0200524
Borislav Petkov36209762019-12-31 17:15:35 +0100525 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
526 force_sig(SIGSEGV);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200527 goto exit;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Borislav Petkov36209762019-12-31 17:15:35 +0100530 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200531 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100532
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200533 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530534 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100535
Borislav Petkov36209762019-12-31 17:15:35 +0100536 /*
537 * To be potentially processing a kprobe fault and to trust the result
538 * from kprobe_running(), we have to be non-preemptible.
539 */
540 if (!preemptible() &&
541 kprobe_running() &&
542 kprobe_fault_handler(regs, X86_TRAP_GP))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200543 goto exit;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200544
Borislav Petkov36209762019-12-31 17:15:35 +0100545 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
546 if (ret == NOTIFY_STOP)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200547 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100548
549 if (error_code)
550 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
551 else
552 hint = get_kernel_gp_address(regs, &gp_addr);
553
554 if (hint != GP_NO_HINT)
555 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
556 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
557 : "maybe for address",
558 gp_addr);
559
560 /*
561 * KASAN is interested only in the non-canonical case, clear it
562 * otherwise.
563 */
564 if (hint != GP_NON_CANONICAL)
565 gp_addr = 0;
566
567 die_addr(desc, regs, error_code, gp_addr);
568
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200569exit:
570 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Masami Hiramatsu93266382014-04-17 17:18:14 +0900573dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jiri Kosina17f41572013-07-23 10:09:28 +0200575 if (poke_int3_handler(regs))
576 return;
577
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700578 /*
Peter Zijlstra0d004492020-02-19 09:46:43 +0100579 * Unlike any other non-IST entry, we can be called from pretty much
580 * any location in the kernel through kprobes -- text_poke() will most
581 * likely be handled by poke_int3_handler() above. This means this
582 * handler is effectively NMI-like.
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700583 */
Peter Zijlstra0d004492020-02-19 09:46:43 +0100584 if (!user_mode(regs))
585 nmi_enter();
Andy Lutomirski65c668f2020-02-25 22:36:46 +0100586
Jason Wesself503b5a2010-05-20 21:04:25 -0500587#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
Kees Cookc9408262012-03-09 16:07:10 -0800588 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
589 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200590 goto exit;
Jason Wesself503b5a2010-05-20 21:04:25 -0500591#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
Srikar Dronamrajucc3a1bf2011-10-25 19:51:59 +0530592
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900593#ifdef CONFIG_KPROBES
594 if (kprobe_int3_handler(regs))
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000595 goto exit;
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900596#endif
597
Kees Cookc9408262012-03-09 16:07:10 -0800598 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
599 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200600 goto exit;
Ingo Molnarb5964402008-02-26 11:15:50 +0100601
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100602 cond_local_irq_enable(regs);
Eric W. Biederman164881b2018-04-16 14:29:39 -0500603 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, 0, NULL);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100604 cond_local_irq_disable(regs);
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700605
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200606exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100607 if (!user_mode(regs))
608 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900610NOKPROBE_SYMBOL(do_int3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200612#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100613/*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100614 * Help handler running on a per-cpu (IST or entry trampoline) stack
615 * to switch to the normal thread stack if the interrupted code was in
616 * user mode. The actual stack switch is done in entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100617 */
Thomas Gleixnerdaf7a692020-03-25 23:47:51 +0100618asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200619{
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100620 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
621 if (regs != eregs)
622 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200623 return regs;
624}
Andy Lutomirskib645af22014-11-22 18:00:33 -0800625
626struct bad_iret_stack {
627 void *error_entry_ret;
628 struct pt_regs regs;
629};
630
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100631asmlinkage __visible noinstr
Andy Lutomirskib645af22014-11-22 18:00:33 -0800632struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
633{
634 /*
635 * This is called from entry_64.S early in handling a fault
636 * caused by a bad iret to user mode. To handle the fault
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100637 * correctly, we want to move our stack frame to where it would
638 * be had we entered directly on the entry stack (rather than
639 * just below the IRET frame) and we want to pretend that the
640 * exception came from the IRET target.
Andy Lutomirskib645af22014-11-22 18:00:33 -0800641 */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100642 struct bad_iret_stack tmp, *new_stack =
643 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Andy Lutomirskib645af22014-11-22 18:00:33 -0800644
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100645 /* Copy the IRET target to the temporary storage. */
646 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800647
648 /* Copy the remainder of the stack from the current stack. */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100649 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
650
651 /* Update the entry stack */
652 memcpy(new_stack, &tmp, sizeof(tmp));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800653
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700654 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800655 return new_stack;
656}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200657#endif
658
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800659static bool is_sysenter_singlestep(struct pt_regs *regs)
660{
661 /*
662 * We don't try for precision here. If we're anywhere in the region of
663 * code that can be single-stepped in the SYSENTER entry path, then
664 * assume that this is a useless single-step trap due to SYSENTER
665 * being invoked with TF set. (We don't know in advance exactly
666 * which instructions will be hit because BTF could plausibly
667 * be set.)
668 */
669#ifdef CONFIG_X86_32
670 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
671 (unsigned long)__end_SYSENTER_singlestep_region -
672 (unsigned long)__begin_SYSENTER_singlestep_region;
673#elif defined(CONFIG_IA32_EMULATION)
674 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
675 (unsigned long)__end_entry_SYSENTER_compat -
676 (unsigned long)entry_SYSENTER_compat;
677#else
678 return false;
679#endif
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * Our handling of the processor debug registers is non-trivial.
684 * We do not clear them on entry and exit from the kernel. Therefore
685 * it is possible to get a watchpoint trap here from inside the kernel.
686 * However, the code in ./ptrace.c has ensured that the user can
687 * only set watchpoints on userspace addresses. Therefore the in-kernel
688 * watchpoint trap can only occur in code which is reading/writing
689 * from user space. Such code must not hold kernel locks (since it
690 * can equally take a page fault), therefore it is safe to call
691 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * Code in ./signal.c ensures that the debug control register
694 * is restored before we deliver any signal, and therefore that
695 * user code runs with the correct debug control register even though
696 * we clear it here.
697 *
698 * Being careful here means that we don't have to be as careful in a
699 * lot of more complicated places (task switching can be a bit lazy
700 * about restoring all the debug state, and ptrace doesn't have to
701 * find every occurrence of the TF bit that could be saved away even
702 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200703 *
704 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900706dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct task_struct *tsk = current;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200709 int user_icebp = 0;
K.Prasad08d68322009-06-01 23:44:08 +0530710 unsigned long dr6;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530711 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Peter Zijlstra0d004492020-02-19 09:46:43 +0100713 nmi_enter();
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000714
K.Prasad08d68322009-06-01 23:44:08 +0530715 get_debugreg(dr6, 6);
Andy Lutomirski8bb56432016-03-09 19:00:29 -0800716 /*
717 * The Intel SDM says:
718 *
719 * Certain debug exceptions may clear bits 0-3. The remaining
720 * contents of the DR6 register are never cleared by the
721 * processor. To avoid confusion in identifying debug
722 * exceptions, debug handlers should clear the register before
723 * returning to the interrupted task.
724 *
725 * Keep it simple: clear DR6 immediately.
726 */
727 set_debugreg(0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
K.Prasad40f92492010-01-28 16:44:01 +0530729 /* Filter out all the reserved bits which are preset to 1 */
730 dr6 &= ~DR6_RESERVED;
731
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200732 /*
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800733 * The SDM says "The processor clears the BTF flag when it
734 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
735 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
736 */
737 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
738
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800739 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
740 is_sysenter_singlestep(regs))) {
741 dr6 &= ~DR_STEP;
742 if (!dr6)
743 goto exit;
744 /*
745 * else we might have gotten a single-step trap and hit a
746 * watchpoint at the same time, in which case we should fall
747 * through and handle the watchpoint.
748 */
749 }
750
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800751 /*
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200752 * If dr6 has no reason to give us about the origin of this trap,
753 * then it's very likely the result of an icebp/int01 trap.
754 * User wants a sigtrap for that.
755 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700756 if (!dr6 && user_mode(regs))
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200757 user_icebp = 1;
758
K.Prasad08d68322009-06-01 23:44:08 +0530759 /* Store the virtualized DR6 value */
760 tsk->thread.debugreg6 = dr6;
761
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900762#ifdef CONFIG_KPROBES
763 if (kprobe_debug_handler(regs))
764 goto exit;
765#endif
766
Rusty Russell5a802e12013-06-16 14:12:47 +0930767 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
K.Prasad62edab92009-06-01 23:47:06 +0530768 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200769 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200770
Steven Rostedt42181182011-12-16 11:43:02 -0500771 /*
772 * Let others (NMI) know that the debug stack is in use
773 * as we may switch to the interrupt stack.
774 */
775 debug_stack_usage_inc();
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* It's safe to allow irq's after DR6 has been saved */
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100778 cond_local_irq_enable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700780 if (v8086_mode(regs)) {
Kees Cookc9408262012-03-09 16:07:10 -0800781 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
782 X86_TRAP_DB);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100783 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500784 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200785 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800788 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
789 /*
790 * Historical junk that used to handle SYSENTER single-stepping.
791 * This should be unreachable now. If we survive for a while
792 * without anyone hitting this warning, we'll turn this into
793 * an oops.
794 */
K.Prasad08d68322009-06-01 23:44:08 +0530795 tsk->thread.debugreg6 &= ~DR_STEP;
796 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
797 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
K.Prasad08d68322009-06-01 23:44:08 +0530799 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200800 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
Eric W. Biederman28d42ea2019-02-05 18:25:11 -0600801 send_sigtrap(regs, error_code, si_code);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100802 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500803 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200805exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100806 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900808NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/*
811 * Note that we play around with the 'TS' bit in an attempt to get
812 * the correct behaviour even in the presence of the asynchronous
813 * IRQ13 behaviour
814 */
Oleg Nesterov5e1b05b2014-05-08 20:34:00 +0200815static void math_error(struct pt_regs *regs, int error_code, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Brian Gerste2e75c912010-03-21 09:00:45 -0400817 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200818 struct fpu *fpu = &task->thread.fpu;
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200819 int si_code;
Kees Cookc9408262012-03-09 16:07:10 -0800820 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
821 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400822
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100823 cond_local_irq_enable(regs);
Brian Gerste2e75c912010-03-21 09:00:45 -0400824
Ingo Molnare1cebad2015-04-30 09:29:38 +0200825 if (!user_mode(regs)) {
Jann Horn81fd9c12018-08-28 22:14:19 +0200826 if (fixup_exception(regs, trapnr, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200827 goto exit;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000828
829 task->thread.error_code = error_code;
830 task->thread.trap_nr = trapnr;
831
832 if (notify_die(DIE_TRAP, str, regs, error_code,
833 trapnr, SIGFPE) != NOTIFY_STOP)
Brian Gerste2e75c912010-03-21 09:00:45 -0400834 die(str, regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200835 goto exit;
Brian Gerste2e75c912010-03-21 09:00:45 -0400836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /*
839 * Save the info for the exception handler and clear the error.
840 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200841 fpu__save(fpu);
842
843 task->thread.trap_nr = trapnr;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400844 task->thread.error_code = error_code;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800845
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200846 si_code = fpu__exception_code(fpu, trapnr);
Ingo Molnare1cebad2015-04-30 09:29:38 +0200847 /* Retry when we get spurious exceptions: */
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200848 if (!si_code)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200849 goto exit;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200850
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200851 force_sig_fault(SIGFPE, si_code,
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500852 (void __user *)uprobe_get_trap_addr(regs));
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200853exit:
854 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Thomas Gleixner14a8bd22020-02-25 23:16:27 +0100857DEFINE_IDTENTRY(exc_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Thomas Gleixner14a8bd22020-02-25 23:16:27 +0100859 math_error(regs, 0, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200862dotraplinkage void
863do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds57780772015-09-01 08:40:25 -0700865 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Kees Cookc9408262012-03-09 16:07:10 -0800866 math_error(regs, error_code, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Thomas Gleixnerdad71062020-02-25 23:16:26 +0100869DEFINE_IDTENTRY(exc_spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Thomas Gleixnerd244d0e2020-02-25 22:36:41 +0100871 /*
872 * This addresses a Pentium Pro Erratum:
873 *
874 * PROBLEM: If the APIC subsystem is configured in mixed mode with
875 * Virtual Wire mode implemented through the local APIC, an
876 * interrupt vector of 0Fh (Intel reserved encoding) may be
877 * generated by the local APIC (Int 15). This vector may be
878 * generated upon receipt of a spurious interrupt (an interrupt
879 * which is removed before the system receives the INTA sequence)
880 * instead of the programmed 8259 spurious interrupt vector.
881 *
882 * IMPLICATION: The spurious interrupt vector programmed in the
883 * 8259 is normally handled by an operating system's spurious
884 * interrupt handler. However, a vector of 0Fh is unknown to some
885 * operating systems, which would crash if this erratum occurred.
886 *
887 * In theory this could be limited to 32bit, but the handler is not
888 * hurting and who knows which other CPUs suffer from this.
889 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200890}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100892DEFINE_IDTENTRY(exc_device_not_available)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200893{
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100894 unsigned long cr0 = read_cr0();
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700895
Brian Gersta334fe42010-09-03 21:17:15 -0400896#ifdef CONFIG_MATH_EMULATION
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100897 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
Tejun Heod3157602009-02-09 22:17:39 +0900898 struct math_emu_info info = { };
899
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100900 cond_local_irq_enable(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900901
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500902 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900903 math_emulate(&info);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200904
905 cond_local_irq_disable(regs);
Brian Gersta334fe42010-09-03 21:17:15 -0400906 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200907 }
Brian Gersta334fe42010-09-03 21:17:15 -0400908#endif
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700909
910 /* This should not happen. */
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700911 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
912 /* Try to fix it up and carry on. */
913 write_cr0(cr0 & ~X86_CR0_TS);
914 } else {
915 /*
916 * Something terrible happened, and we're better off trying
917 * to kill the task than getting stuck in a never-ending
918 * loop of #NM faults.
919 */
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100920 die("unexpected #NM exception", regs, 0);
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700921 }
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200922}
923
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200924#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200925dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200926{
Linus Torvalds57780772015-09-01 08:40:25 -0700927 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200928 local_irq_enable();
929
Kees Cookc9408262012-03-09 16:07:10 -0800930 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200931 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
932 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500933 ILL_BADSTK, (void __user *)NULL);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200934 }
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200935 local_irq_disable();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200936}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200937#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939void __init trap_init(void)
940{
Andy Lutomirski40e7f942017-12-04 15:07:26 +0100941 /* Init cpu_entry_area before IST entries are set up */
942 setup_cpu_entry_areas();
943
Thomas Gleixnerb70543a2017-08-28 08:47:53 +0200944 idt_setup_traps();
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
Kees Cook4eefbe72013-04-10 12:24:22 -0700947 * Set the IDT descriptor to a fixed read-only location, so that the
948 * "sidt" instruction will not leak the location of the kernel, and
949 * to defend the IDT against arbitrary memory write vulnerabilities.
950 * It will be reloaded in cpu_init() */
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100951 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
952 PAGE_KERNEL_RO);
953 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
Kees Cook4eefbe72013-04-10 12:24:22 -0700954
955 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100956 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958 cpu_init();
959
Thomas Gleixner90f62252017-08-28 08:47:52 +0200960 idt_setup_ist_traps();
Wang Nanb4d83272015-02-26 13:49:39 +0800961
Thomas Gleixner0a309082017-08-28 08:47:51 +0200962 idt_setup_debugidt_traps();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}