blob: 79af913e78a388cd486d09e66706fe8f07d4ad75 [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.
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100322 *
323 * The 32bit #DF shim provides CR2 already as an argument. On 64bit it needs
324 * to be read before doing anything else.
Andy Lutomirski7d8d8cf2019-11-20 23:06:41 -0800325 */
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100326DEFINE_IDTENTRY_DF(exc_double_fault)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200327{
328 static const char str[] = "double fault";
329 struct task_struct *tsk = current;
330
Ingo Molnar7102cb02020-05-25 09:42:41 +0200331#ifdef CONFIG_VMAP_STACK
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100332 unsigned long address = read_cr2();
333#endif
334
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800335#ifdef CONFIG_X86_ESPFIX64
336 extern unsigned char native_irq_return_iret[];
337
338 /*
339 * If IRET takes a non-IST fault on the espfix64 stack, then we
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100340 * end up promoting it to a doublefault. In that case, take
341 * advantage of the fact that we're not using the normal (TSS.sp0)
342 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
343 * and then modify our own IRET frame so that, when we return,
344 * we land directly at the #GP(0) vector with the stack already
345 * set up according to its expectations.
346 *
347 * The net result is that our #GP handler will think that we
348 * entered from usermode with the bad user context.
Andy Lutomirski95927472014-11-19 17:41:09 -0800349 *
Peter Zijlstra0d004492020-02-19 09:46:43 +0100350 * No need for nmi_enter() here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800351 */
Andy Lutomirskic739f932017-12-12 07:56:36 -0800352 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800353 regs->cs == __KERNEL_CS &&
354 regs->ip == (unsigned long)native_irq_return_iret)
355 {
Andy Lutomirskic482fee2017-12-04 15:07:29 +0100356 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Peter Zijlstrae9660392020-02-20 13:17:27 +0100357 unsigned long *p = (unsigned long *)regs->sp;
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800358
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100359 /*
360 * regs->sp points to the failing IRET frame on the
361 * ESPFIX64 stack. Copy it to the entry stack. This fills
362 * in gpregs->ss through gpregs->ip.
363 *
364 */
Peter Zijlstrae9660392020-02-20 13:17:27 +0100365 gpregs->ip = p[0];
366 gpregs->cs = p[1];
367 gpregs->flags = p[2];
368 gpregs->sp = p[3];
369 gpregs->ss = p[4];
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100370 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
371
372 /*
373 * Adjust our frame so that we return straight to the #GP
374 * vector with the expected RSP value. This is safe because
375 * we won't enable interupts or schedule before we invoke
376 * general_protection, so nothing will clobber the stack
377 * frame we just set up.
Andy Lutomirskibd7b1f72018-09-03 15:59:42 -0700378 *
379 * We will enter general_protection with kernel GSBASE,
380 * which is what the stub expects, given that the faulting
381 * RIP will be the IRET instruction.
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100382 */
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100383 regs->ip = (unsigned long)asm_exc_general_protection;
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100384 regs->sp = (unsigned long)&gpregs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800385
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800386 return;
387 }
388#endif
389
Peter Zijlstra0d004492020-02-19 09:46:43 +0100390 nmi_enter();
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100391 instrumentation_begin();
Kees Cookc9408262012-03-09 16:07:10 -0800392 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200393
394 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530395 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200396
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700397#ifdef CONFIG_VMAP_STACK
398 /*
399 * If we overflow the stack into a guard page, the CPU will fail
400 * to deliver #PF and will send #DF instead. Similarly, if we
401 * take any non-IST exception while too close to the bottom of
402 * the stack, the processor will get a page fault while
403 * delivering the exception and will generate a double fault.
404 *
405 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
406 * Page-Fault Exception (#PF):
407 *
408 * Processors update CR2 whenever a page fault is detected. If a
409 * second page fault occurs while an earlier page fault is being
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100410 * delivered, the faulting linear address of the second fault will
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700411 * overwrite the contents of CR2 (replacing the previous
412 * address). These updates to CR2 occur even if the page fault
413 * results in a double fault or occurs during the delivery of a
414 * double fault.
415 *
416 * The logic below has a small possibility of incorrectly diagnosing
417 * some errors as stack overflows. For example, if the IDT or GDT
418 * gets corrupted such that #GP delivery fails due to a bad descriptor
419 * causing #GP and we hit this condition while CR2 coincidentally
420 * points to the stack guard page, we'll think we overflowed the
421 * stack. Given that we're going to panic one way or another
422 * if this happens, this isn't necessarily worth fixing.
423 *
424 * If necessary, we could improve the test by only diagnosing
425 * a stack overflow if the saved RSP points within 47 bytes of
426 * the bottom of the stack: if RSP == tsk_stack + 48 and we
427 * take an exception, the stack is already aligned and there
428 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
429 * possible error code, so a stack overflow would *not* double
430 * fault. With any less space left, exception delivery could
431 * fail, and, as a practical matter, we've overflowed the
432 * stack even if the actual trigger for the double fault was
433 * something else.
434 */
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100435 if ((unsigned long)task_stack_page(tsk) - 1 - address < PAGE_SIZE) {
436 handle_stack_overflow("kernel stack overflow (double-fault)",
437 regs, address);
438 }
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700439#endif
440
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800441 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
Andy Lutomirski0337b7e2019-11-25 22:37:44 -0800442 die("double fault", regs, error_code);
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800443 panic("Machine halted.");
Thomas Gleixnerc29c7752020-02-25 23:33:31 +0100444 instrumentation_end();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200445}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200446
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100447DEFINE_IDTENTRY(exc_bounds)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800448{
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100449 if (notify_die(DIE_TRAP, "bounds", regs, 0,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800450 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700451 return;
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100452 cond_local_irq_enable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800453
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700454 if (!user_mode(regs))
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100455 die("bounds", regs, 0);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800456
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100457 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200458
459 cond_local_irq_disable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800460}
461
Jann Horn59c1dcb2019-12-19 00:11:48 +0100462enum kernel_gp_hint {
463 GP_NO_HINT,
464 GP_NON_CANONICAL,
465 GP_CANONICAL
466};
467
468/*
469 * When an uncaught #GP occurs, try to determine the memory address accessed by
470 * the instruction and return that address to the caller. Also, try to figure
471 * out whether any part of the access to that address was non-canonical.
472 */
473static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
474 unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jann Horn59c1dcb2019-12-19 00:11:48 +0100476 u8 insn_buf[MAX_INSN_SIZE];
477 struct insn insn;
478
479 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
480 return GP_NO_HINT;
481
482 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
483 insn_get_modrm(&insn);
484 insn_get_sib(&insn);
485
486 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
487 if (*addr == -1UL)
488 return GP_NO_HINT;
489
490#ifdef CONFIG_X86_64
491 /*
492 * Check that:
493 * - the operand is not in the kernel half
494 * - the last byte of the operand is not in the user canonical half
495 */
496 if (*addr < ~__VIRTUAL_MASK &&
497 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
498 return GP_NON_CANONICAL;
499#endif
500
501 return GP_CANONICAL;
502}
503
504#define GPFSTR "general protection fault"
505
Thomas Gleixnerbe4c11a2020-02-25 23:16:25 +0100506DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
Jann Horn59c1dcb2019-12-19 00:11:48 +0100507{
508 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
Borislav Petkov36209762019-12-31 17:15:35 +0100509 enum kernel_gp_hint hint = GP_NO_HINT;
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200510 struct task_struct *tsk;
Borislav Petkov36209762019-12-31 17:15:35 +0100511 unsigned long gp_addr;
512 int ret;
Ingo Molnarb5964402008-02-26 11:15:50 +0100513
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100514 cond_local_irq_enable(regs);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200515
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800516 if (static_cpu_has(X86_FEATURE_UMIP)) {
517 if (user_mode(regs) && fixup_umip_exception(regs))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200518 goto exit;
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800519 }
520
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700521 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200522 local_irq_enable();
523 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200524 local_irq_disable();
Andy Lutomirski8c840142015-07-03 12:44:32 -0700525 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200528 tsk = current;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200529
Borislav Petkov36209762019-12-31 17:15:35 +0100530 if (user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200531 tsk->thread.error_code = error_code;
532 tsk->thread.trap_nr = X86_TRAP_GP;
Jann Horn76dee4a2018-08-28 22:14:16 +0200533
Borislav Petkov36209762019-12-31 17:15:35 +0100534 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
535 force_sig(SIGSEGV);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200536 goto exit;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Borislav Petkov36209762019-12-31 17:15:35 +0100539 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200540 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100541
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200542 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530543 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100544
Borislav Petkov36209762019-12-31 17:15:35 +0100545 /*
546 * To be potentially processing a kprobe fault and to trust the result
547 * from kprobe_running(), we have to be non-preemptible.
548 */
549 if (!preemptible() &&
550 kprobe_running() &&
551 kprobe_fault_handler(regs, X86_TRAP_GP))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200552 goto exit;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200553
Borislav Petkov36209762019-12-31 17:15:35 +0100554 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
555 if (ret == NOTIFY_STOP)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200556 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100557
558 if (error_code)
559 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
560 else
561 hint = get_kernel_gp_address(regs, &gp_addr);
562
563 if (hint != GP_NO_HINT)
564 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
565 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
566 : "maybe for address",
567 gp_addr);
568
569 /*
570 * KASAN is interested only in the non-canonical case, clear it
571 * otherwise.
572 */
573 if (hint != GP_NON_CANONICAL)
574 gp_addr = 0;
575
576 die_addr(desc, regs, error_code, gp_addr);
577
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200578exit:
579 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Peter Zijlstra21e28292020-03-05 16:09:52 +0100582static bool do_int3(struct pt_regs *regs)
583{
584 int res;
585
586#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
587 if (kgdb_ll_trap(DIE_INT3, "int3", regs, 0, X86_TRAP_BP,
588 SIGTRAP) == NOTIFY_STOP)
589 return true;
590#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
591
592#ifdef CONFIG_KPROBES
593 if (kprobe_int3_handler(regs))
594 return true;
595#endif
596 res = notify_die(DIE_INT3, "int3", regs, 0, X86_TRAP_BP, SIGTRAP);
597
598 return res == NOTIFY_STOP;
599}
600
601static void do_int3_user(struct pt_regs *regs)
602{
603 if (do_int3(regs))
604 return;
605
606 cond_local_irq_enable(regs);
607 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, 0, 0, NULL);
608 cond_local_irq_disable(regs);
609}
610
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100611DEFINE_IDTENTRY_RAW(exc_int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Peter Zijlstraf64366e2020-02-20 13:28:06 +0100613 /*
614 * poke_int3_handler() is completely self contained code; it does (and
615 * must) *NOT* call out to anything, lest it hits upon yet another
616 * INT3.
617 */
Jiri Kosina17f41572013-07-23 10:09:28 +0200618 if (poke_int3_handler(regs))
619 return;
620
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700621 /*
Thomas Gleixnerfa95d7d2020-05-21 22:05:19 +0200622 * idtentry_enter_user() uses static_branch_{,un}likely() and therefore
Thomas Gleixner8edd7e32020-02-25 23:16:16 +0100623 * can trigger INT3, hence poke_int3_handler() must be done
624 * before. If the entry came from kernel mode, then use nmi_enter()
625 * because the INT3 could have been hit in any context including
626 * NMI.
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700627 */
Peter Zijlstra21e28292020-03-05 16:09:52 +0100628 if (user_mode(regs)) {
Thomas Gleixnerfa95d7d2020-05-21 22:05:19 +0200629 idtentry_enter_user(regs);
Peter Zijlstra21e28292020-03-05 16:09:52 +0100630 instrumentation_begin();
631 do_int3_user(regs);
632 instrumentation_end();
Thomas Gleixnerfa95d7d2020-05-21 22:05:19 +0200633 idtentry_exit_user(regs);
Peter Zijlstra21e28292020-03-05 16:09:52 +0100634 } else {
635 nmi_enter();
636 instrumentation_begin();
Peter Zijlstrabf2b3002020-05-29 23:27:40 +0200637 trace_hardirqs_off_finish();
Peter Zijlstra21e28292020-03-05 16:09:52 +0100638 if (!do_int3(regs))
639 die("int3", regs, 0);
Thomas Gleixner3ffdfdc2020-05-21 22:05:51 +0200640 if (regs->flags & X86_EFLAGS_IF)
641 trace_hardirqs_on_prepare();
Peter Zijlstra21e28292020-03-05 16:09:52 +0100642 instrumentation_end();
Peter Zijlstra0d004492020-02-19 09:46:43 +0100643 nmi_exit();
Peter Zijlstra21e28292020-03-05 16:09:52 +0100644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200647#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100648/*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100649 * Help handler running on a per-cpu (IST or entry trampoline) stack
650 * to switch to the normal thread stack if the interrupted code was in
651 * user mode. The actual stack switch is done in entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100652 */
Thomas Gleixnerdaf7a692020-03-25 23:47:51 +0100653asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200654{
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100655 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
656 if (regs != eregs)
657 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200658 return regs;
659}
Andy Lutomirskib645af22014-11-22 18:00:33 -0800660
661struct bad_iret_stack {
662 void *error_entry_ret;
663 struct pt_regs regs;
664};
665
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100666asmlinkage __visible noinstr
Andy Lutomirskib645af22014-11-22 18:00:33 -0800667struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
668{
669 /*
670 * This is called from entry_64.S early in handling a fault
671 * caused by a bad iret to user mode. To handle the fault
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100672 * correctly, we want to move our stack frame to where it would
673 * be had we entered directly on the entry stack (rather than
674 * just below the IRET frame) and we want to pretend that the
675 * exception came from the IRET target.
Andy Lutomirskib645af22014-11-22 18:00:33 -0800676 */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100677 struct bad_iret_stack tmp, *new_stack =
678 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Andy Lutomirskib645af22014-11-22 18:00:33 -0800679
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100680 /* Copy the IRET target to the temporary storage. */
681 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800682
683 /* Copy the remainder of the stack from the current stack. */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100684 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
685
686 /* Update the entry stack */
687 memcpy(new_stack, &tmp, sizeof(tmp));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800688
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700689 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800690 return new_stack;
691}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200692#endif
693
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800694static bool is_sysenter_singlestep(struct pt_regs *regs)
695{
696 /*
697 * We don't try for precision here. If we're anywhere in the region of
698 * code that can be single-stepped in the SYSENTER entry path, then
699 * assume that this is a useless single-step trap due to SYSENTER
700 * being invoked with TF set. (We don't know in advance exactly
701 * which instructions will be hit because BTF could plausibly
702 * be set.)
703 */
704#ifdef CONFIG_X86_32
705 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
706 (unsigned long)__end_SYSENTER_singlestep_region -
707 (unsigned long)__begin_SYSENTER_singlestep_region;
708#elif defined(CONFIG_IA32_EMULATION)
709 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
710 (unsigned long)__end_entry_SYSENTER_compat -
711 (unsigned long)entry_SYSENTER_compat;
712#else
713 return false;
714#endif
715}
716
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200717static __always_inline void debug_enter(unsigned long *dr6, unsigned long *dr7)
718{
719 /*
720 * Disable breakpoints during exception handling; recursive exceptions
721 * are exceedingly 'fun'.
722 *
723 * Since this function is NOKPROBE, and that also applies to
724 * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a
725 * HW_BREAKPOINT_W on our stack)
726 *
727 * Entry text is excluded for HW_BP_X and cpu_entry_area, which
728 * includes the entry stack is excluded for everything.
729 */
Peter Zijlstrae1de11d2020-05-29 23:27:33 +0200730 *dr7 = local_db_save();
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200731
732 /*
733 * The Intel SDM says:
734 *
735 * Certain debug exceptions may clear bits 0-3. The remaining
736 * contents of the DR6 register are never cleared by the
737 * processor. To avoid confusion in identifying debug
738 * exceptions, debug handlers should clear the register before
739 * returning to the interrupted task.
740 *
741 * Keep it simple: clear DR6 immediately.
742 */
743 get_debugreg(*dr6, 6);
744 set_debugreg(0, 6);
745 /* Filter out all the reserved bits which are preset to 1 */
746 *dr6 &= ~DR6_RESERVED;
747}
748
749static __always_inline void debug_exit(unsigned long dr7)
750{
Peter Zijlstrae1de11d2020-05-29 23:27:33 +0200751 local_db_restore(dr7);
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200752}
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 */
Thomas Gleixner9347f412020-05-04 19:56:26 +0200778static void noinstr handle_debug(struct pt_regs *regs, unsigned long dr6,
779 bool user_icebp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct task_struct *tsk = current;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530782 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
K.Prasad08d68322009-06-01 23:44:08 +0530784 /* Store the virtualized DR6 value */
785 tsk->thread.debugreg6 = dr6;
786
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200787 instrumentation_begin();
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900788#ifdef CONFIG_KPROBES
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200789 if (kprobe_debug_handler(regs)) {
790 instrumentation_end();
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100791 return;
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200792 }
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900793#endif
794
Thomas Gleixner2bbc68f2020-02-25 23:33:26 +0100795 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, 0,
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200796 SIGTRAP) == NOTIFY_STOP) {
797 instrumentation_end();
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100798 return;
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200799 }
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* It's safe to allow irq's after DR6 has been saved */
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100802 cond_local_irq_enable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700804 if (v8086_mode(regs)) {
Thomas Gleixner2bbc68f2020-02-25 23:33:26 +0100805 handle_vm86_trap((struct kernel_vm86_regs *) regs, 0,
806 X86_TRAP_DB);
Thomas Gleixner9347f412020-05-04 19:56:26 +0200807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800810 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
811 /*
812 * Historical junk that used to handle SYSENTER single-stepping.
813 * This should be unreachable now. If we survive for a while
814 * without anyone hitting this warning, we'll turn this into
815 * an oops.
816 */
K.Prasad08d68322009-06-01 23:44:08 +0530817 tsk->thread.debugreg6 &= ~DR_STEP;
818 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
819 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Thomas Gleixner9347f412020-05-04 19:56:26 +0200821
K.Prasad08d68322009-06-01 23:44:08 +0530822 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200823 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
Thomas Gleixner2bbc68f2020-02-25 23:33:26 +0100824 send_sigtrap(regs, 0, si_code);
Thomas Gleixner9347f412020-05-04 19:56:26 +0200825
826out:
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100827 cond_local_irq_disable(regs);
Thomas Gleixner75347bb22020-04-30 11:07:20 +0200828 instrumentation_end();
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100831static __always_inline void exc_debug_kernel(struct pt_regs *regs,
832 unsigned long dr6)
833{
834 nmi_enter();
Thomas Gleixner3ffdfdc2020-05-21 22:05:51 +0200835 instrumentation_begin();
Peter Zijlstrabf2b3002020-05-29 23:27:40 +0200836 trace_hardirqs_off_finish();
Thomas Gleixner3ffdfdc2020-05-21 22:05:51 +0200837 instrumentation_end();
838
Thomas Gleixner9347f412020-05-04 19:56:26 +0200839 /*
840 * The SDM says "The processor clears the BTF flag when it
841 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
842 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
843 */
844 clear_thread_flag(TIF_BLOCKSTEP);
845
846 /*
847 * Catch SYSENTER with TF set and clear DR_STEP. If this hit a
848 * watchpoint at the same time then that will still be handled.
849 */
850 if ((dr6 & DR_STEP) && is_sysenter_singlestep(regs))
851 dr6 &= ~DR_STEP;
852
853 /*
854 * If DR6 is zero, no point in trying to handle it. The kernel is
855 * not using INT1.
856 */
857 if (dr6)
858 handle_debug(regs, dr6, false);
859
Thomas Gleixner3ffdfdc2020-05-21 22:05:51 +0200860 instrumentation_begin();
861 if (regs->flags & X86_EFLAGS_IF)
862 trace_hardirqs_on_prepare();
863 instrumentation_end();
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100864 nmi_exit();
865}
866
867static __always_inline void exc_debug_user(struct pt_regs *regs,
868 unsigned long dr6)
869{
Thomas Gleixnerfa95d7d2020-05-21 22:05:19 +0200870 idtentry_enter_user(regs);
Thomas Gleixner9347f412020-05-04 19:56:26 +0200871 clear_thread_flag(TIF_BLOCKSTEP);
872
873 /*
874 * If dr6 has no reason to give us about the origin of this trap,
875 * then it's very likely the result of an icebp/int01 trap.
876 * User wants a sigtrap for that.
877 */
878 handle_debug(regs, dr6, !dr6);
Thomas Gleixnerfa95d7d2020-05-21 22:05:19 +0200879 idtentry_exit_user(regs);
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100880}
881
882#ifdef CONFIG_X86_64
883/* IST stack entry */
884DEFINE_IDTENTRY_DEBUG(exc_debug)
885{
886 unsigned long dr6, dr7;
887
888 debug_enter(&dr6, &dr7);
889 exc_debug_kernel(regs, dr6);
Peter Zijlstra9f58fdde2020-04-06 21:02:56 +0200890 debug_exit(dr7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Thomas Gleixner4c0dcd82020-02-25 23:33:29 +0100893/* User entry, runs on regular task stack */
894DEFINE_IDTENTRY_DEBUG_USER(exc_debug)
895{
896 unsigned long dr6, dr7;
897
898 debug_enter(&dr6, &dr7);
899 exc_debug_user(regs, dr6);
900 debug_exit(dr7);
901}
902#else
903/* 32 bit does not have separate entry points. */
904DEFINE_IDTENTRY_DEBUG(exc_debug)
905{
906 unsigned long dr6, dr7;
907
908 debug_enter(&dr6, &dr7);
909
910 if (user_mode(regs))
911 exc_debug_user(regs, dr6);
912 else
913 exc_debug_kernel(regs, dr6);
914
915 debug_exit(dr7);
916}
917#endif
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/*
920 * Note that we play around with the 'TS' bit in an attempt to get
921 * the correct behaviour even in the presence of the asynchronous
922 * IRQ13 behaviour
923 */
Thomas Gleixner48227e22020-02-25 23:16:29 +0100924static void math_error(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Brian Gerste2e75c912010-03-21 09:00:45 -0400926 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200927 struct fpu *fpu = &task->thread.fpu;
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200928 int si_code;
Kees Cookc9408262012-03-09 16:07:10 -0800929 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
930 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400931
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100932 cond_local_irq_enable(regs);
Brian Gerste2e75c912010-03-21 09:00:45 -0400933
Ingo Molnare1cebad2015-04-30 09:29:38 +0200934 if (!user_mode(regs)) {
Thomas Gleixner48227e22020-02-25 23:16:29 +0100935 if (fixup_exception(regs, trapnr, 0, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200936 goto exit;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000937
Thomas Gleixner48227e22020-02-25 23:16:29 +0100938 task->thread.error_code = 0;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000939 task->thread.trap_nr = trapnr;
940
Thomas Gleixner48227e22020-02-25 23:16:29 +0100941 if (notify_die(DIE_TRAP, str, regs, 0, trapnr,
942 SIGFPE) != NOTIFY_STOP)
943 die(str, regs, 0);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200944 goto exit;
Brian Gerste2e75c912010-03-21 09:00:45 -0400945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /*
948 * Save the info for the exception handler and clear the error.
949 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200950 fpu__save(fpu);
951
952 task->thread.trap_nr = trapnr;
Thomas Gleixner48227e22020-02-25 23:16:29 +0100953 task->thread.error_code = 0;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800954
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200955 si_code = fpu__exception_code(fpu, trapnr);
Ingo Molnare1cebad2015-04-30 09:29:38 +0200956 /* Retry when we get spurious exceptions: */
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200957 if (!si_code)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200958 goto exit;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200959
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200960 force_sig_fault(SIGFPE, si_code,
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500961 (void __user *)uprobe_get_trap_addr(regs));
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200962exit:
963 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Thomas Gleixner14a8bd22020-02-25 23:16:27 +0100966DEFINE_IDTENTRY(exc_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Thomas Gleixner48227e22020-02-25 23:16:29 +0100968 math_error(regs, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Thomas Gleixner48227e22020-02-25 23:16:29 +0100971DEFINE_IDTENTRY(exc_simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Thomas Gleixner48227e22020-02-25 23:16:29 +0100973 if (IS_ENABLED(CONFIG_X86_INVD_BUG)) {
974 /* AMD 486 bug: INVD in CPL 0 raises #XF instead of #GP */
975 if (!static_cpu_has(X86_FEATURE_XMM)) {
976 __exc_general_protection(regs, 0);
977 return;
978 }
979 }
980 math_error(regs, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Thomas Gleixnerdad71062020-02-25 23:16:26 +0100983DEFINE_IDTENTRY(exc_spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Thomas Gleixnerd244d0e2020-02-25 22:36:41 +0100985 /*
986 * This addresses a Pentium Pro Erratum:
987 *
988 * PROBLEM: If the APIC subsystem is configured in mixed mode with
989 * Virtual Wire mode implemented through the local APIC, an
990 * interrupt vector of 0Fh (Intel reserved encoding) may be
991 * generated by the local APIC (Int 15). This vector may be
992 * generated upon receipt of a spurious interrupt (an interrupt
993 * which is removed before the system receives the INTA sequence)
994 * instead of the programmed 8259 spurious interrupt vector.
995 *
996 * IMPLICATION: The spurious interrupt vector programmed in the
997 * 8259 is normally handled by an operating system's spurious
998 * interrupt handler. However, a vector of 0Fh is unknown to some
999 * operating systems, which would crash if this erratum occurred.
1000 *
1001 * In theory this could be limited to 32bit, but the handler is not
1002 * hurting and who knows which other CPUs suffer from this.
1003 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Thomas Gleixner866ae2c2020-02-25 23:16:19 +01001006DEFINE_IDTENTRY(exc_device_not_available)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +02001007{
Borislav Petkovee35b9b2019-01-17 13:02:05 +01001008 unsigned long cr0 = read_cr0();
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -07001009
Brian Gersta334fe42010-09-03 21:17:15 -04001010#ifdef CONFIG_MATH_EMULATION
Borislav Petkovee35b9b2019-01-17 13:02:05 +01001011 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
Tejun Heod3157602009-02-09 22:17:39 +09001012 struct math_emu_info info = { };
1013
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +01001014 cond_local_irq_enable(regs);
Tejun Heod3157602009-02-09 22:17:39 +09001015
Brian Gerstaa78bcf2009-02-10 09:51:45 -05001016 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +09001017 math_emulate(&info);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +02001018
1019 cond_local_irq_disable(regs);
Brian Gersta334fe42010-09-03 21:17:15 -04001020 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +02001021 }
Brian Gersta334fe42010-09-03 21:17:15 -04001022#endif
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -07001023
1024 /* This should not happen. */
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -07001025 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
1026 /* Try to fix it up and carry on. */
1027 write_cr0(cr0 & ~X86_CR0_TS);
1028 } else {
1029 /*
1030 * Something terrible happened, and we're better off trying
1031 * to kill the task than getting stuck in a never-ending
1032 * loop of #NM faults.
1033 */
Thomas Gleixner866ae2c2020-02-25 23:16:19 +01001034 die("unexpected #NM exception", regs, 0);
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -07001035 }
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +02001036}
1037
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001038#ifdef CONFIG_X86_32
Thomas Gleixnerd7729052020-02-25 23:16:30 +01001039DEFINE_IDTENTRY_SW(iret_error)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +02001040{
Alexander van Heukelumf8e08702008-09-09 21:56:13 +02001041 local_irq_enable();
Thomas Gleixnerd7729052020-02-25 23:16:30 +01001042 if (notify_die(DIE_TRAP, "iret exception", regs, 0,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +02001043 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
Thomas Gleixnerd7729052020-02-25 23:16:30 +01001044 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, 0,
Eric W. Biederman164881b2018-04-16 14:29:39 -05001045 ILL_BADSTK, (void __user *)NULL);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +02001046 }
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +02001047 local_irq_disable();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +02001048}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001049#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +02001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051void __init trap_init(void)
1052{
Andy Lutomirski40e7f942017-12-04 15:07:26 +01001053 /* Init cpu_entry_area before IST entries are set up */
1054 setup_cpu_entry_areas();
1055
Thomas Gleixnerb70543a2017-08-28 08:47:53 +02001056 idt_setup_traps();
Yinghai Lubb3f0b52009-01-25 02:38:09 -08001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
Kees Cook4eefbe72013-04-10 12:24:22 -07001059 * Set the IDT descriptor to a fixed read-only location, so that the
1060 * "sidt" instruction will not leak the location of the kernel, and
1061 * to defend the IDT against arbitrary memory write vulnerabilities.
1062 * It will be reloaded in cpu_init() */
Thomas Gleixner92a0f812017-12-20 18:51:31 +01001063 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
1064 PAGE_KERNEL_RO);
1065 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
Kees Cook4eefbe72013-04-10 12:24:22 -07001066
1067 /*
Ingo Molnarb5964402008-02-26 11:15:50 +01001068 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
1070 cpu_init();
1071
Thomas Gleixner90f62252017-08-28 08:47:52 +02001072 idt_setup_ist_traps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}