blob: b8af5eb6a92911acc661f094e5631785bee14c59 [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
148 * delivered, faults. See also do_general_protection below.
149 */
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
Eric W. Biederman164881b2018-04-16 14:29:39 -0500249#define IP ((void __user *)uprobe_get_trap_addr(regs))
250#define DO_ERROR(trapnr, signr, sicode, addr, str, name) \
251dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
252{ \
253 do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Eric W. Biederman164881b2018-04-16 14:29:39 -0500256DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, 0, NULL, "coprocessor segment overrun", coprocessor_segment_overrun)
257DO_ERROR(X86_TRAP_TS, SIGSEGV, 0, NULL, "invalid TSS", invalid_TSS)
258DO_ERROR(X86_TRAP_NP, SIGBUS, 0, NULL, "segment not present", segment_not_present)
259DO_ERROR(X86_TRAP_SS, SIGBUS, 0, NULL, "stack segment", stack_segment)
Eric W. Biederman164881b2018-04-16 14:29:39 -0500260#undef IP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -0800262dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code)
263{
264 char *str = "alignment check";
265
266 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
267
268 if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
269 return;
270
271 if (!user_mode(regs))
272 die("Split lock detected\n", regs, error_code);
273
274 local_irq_enable();
275
276 if (handle_user_split_lock(regs, error_code))
277 return;
278
279 do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
280 error_code, BUS_ADRALN, NULL);
281}
282
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700283#ifdef CONFIG_VMAP_STACK
Andy Lutomirski6271cfd2016-08-30 17:27:57 -0700284__visible void __noreturn handle_stack_overflow(const char *message,
285 struct pt_regs *regs,
286 unsigned long fault_address)
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700287{
288 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
289 (void *)fault_address, current->stack,
290 (char *)current->stack + THREAD_SIZE - 1);
291 die(message, regs, 0);
292
293 /* Be absolutely certain we don't return. */
Rasmus Villemoes2022cce2018-10-27 00:20:04 +0200294 panic("%s", message);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700295}
296#endif
297
Andy Lutomirski7d8d8cf2019-11-20 23:06:41 -0800298/*
299 * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
300 *
301 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
302 * SDM's warnings about double faults being unrecoverable, returning works as
303 * expected. Presumably what the SDM actually means is that the CPU may get
304 * the register state wrong on entry, so returning could be a bad idea.
305 *
306 * Various CPU engineers have promised that double faults due to an IRET fault
307 * while the stack is read-only are, in fact, recoverable.
308 *
309 * On x86_32, this is entered through a task gate, and regs are synthesized
310 * from the TSS. Returning is, in principle, okay, but changes to regs will
311 * be lost. If, for some reason, we need to return to a context with modified
312 * regs, the shim code could be adjusted to synchronize the registers.
313 */
Peter Zijlstraa0d14b82019-07-11 13:40:59 +0200314dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200315{
316 static const char str[] = "double fault";
317 struct task_struct *tsk = current;
318
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800319#ifdef CONFIG_X86_ESPFIX64
320 extern unsigned char native_irq_return_iret[];
321
322 /*
323 * If IRET takes a non-IST fault on the espfix64 stack, then we
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100324 * end up promoting it to a doublefault. In that case, take
325 * advantage of the fact that we're not using the normal (TSS.sp0)
326 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
327 * and then modify our own IRET frame so that, when we return,
328 * we land directly at the #GP(0) vector with the stack already
329 * set up according to its expectations.
330 *
331 * The net result is that our #GP handler will think that we
332 * entered from usermode with the bad user context.
Andy Lutomirski95927472014-11-19 17:41:09 -0800333 *
Peter Zijlstra0d004492020-02-19 09:46:43 +0100334 * No need for nmi_enter() here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800335 */
Andy Lutomirskic739f932017-12-12 07:56:36 -0800336 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800337 regs->cs == __KERNEL_CS &&
338 regs->ip == (unsigned long)native_irq_return_iret)
339 {
Andy Lutomirskic482fee2017-12-04 15:07:29 +0100340 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Peter Zijlstrae9660392020-02-20 13:17:27 +0100341 unsigned long *p = (unsigned long *)regs->sp;
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800342
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100343 /*
344 * regs->sp points to the failing IRET frame on the
345 * ESPFIX64 stack. Copy it to the entry stack. This fills
346 * in gpregs->ss through gpregs->ip.
347 *
348 */
Peter Zijlstrae9660392020-02-20 13:17:27 +0100349 gpregs->ip = p[0];
350 gpregs->cs = p[1];
351 gpregs->flags = p[2];
352 gpregs->sp = p[3];
353 gpregs->ss = p[4];
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100354 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
355
356 /*
357 * Adjust our frame so that we return straight to the #GP
358 * vector with the expected RSP value. This is safe because
359 * we won't enable interupts or schedule before we invoke
360 * general_protection, so nothing will clobber the stack
361 * frame we just set up.
Andy Lutomirskibd7b1f72018-09-03 15:59:42 -0700362 *
363 * We will enter general_protection with kernel GSBASE,
364 * which is what the stub expects, given that the faulting
365 * RIP will be the IRET instruction.
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100366 */
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800367 regs->ip = (unsigned long)general_protection;
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100368 regs->sp = (unsigned long)&gpregs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800369
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800370 return;
371 }
372#endif
373
Peter Zijlstra0d004492020-02-19 09:46:43 +0100374 nmi_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800375 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200376
377 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530378 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200379
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700380#ifdef CONFIG_VMAP_STACK
381 /*
382 * If we overflow the stack into a guard page, the CPU will fail
383 * to deliver #PF and will send #DF instead. Similarly, if we
384 * take any non-IST exception while too close to the bottom of
385 * the stack, the processor will get a page fault while
386 * delivering the exception and will generate a double fault.
387 *
388 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
389 * Page-Fault Exception (#PF):
390 *
391 * Processors update CR2 whenever a page fault is detected. If a
392 * second page fault occurs while an earlier page fault is being
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100393 * delivered, the faulting linear address of the second fault will
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700394 * overwrite the contents of CR2 (replacing the previous
395 * address). These updates to CR2 occur even if the page fault
396 * results in a double fault or occurs during the delivery of a
397 * double fault.
398 *
399 * The logic below has a small possibility of incorrectly diagnosing
400 * some errors as stack overflows. For example, if the IDT or GDT
401 * gets corrupted such that #GP delivery fails due to a bad descriptor
402 * causing #GP and we hit this condition while CR2 coincidentally
403 * points to the stack guard page, we'll think we overflowed the
404 * stack. Given that we're going to panic one way or another
405 * if this happens, this isn't necessarily worth fixing.
406 *
407 * If necessary, we could improve the test by only diagnosing
408 * a stack overflow if the saved RSP points within 47 bytes of
409 * the bottom of the stack: if RSP == tsk_stack + 48 and we
410 * take an exception, the stack is already aligned and there
411 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
412 * possible error code, so a stack overflow would *not* double
413 * fault. With any less space left, exception delivery could
414 * fail, and, as a practical matter, we've overflowed the
415 * stack even if the actual trigger for the double fault was
416 * something else.
417 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700418 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
419 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
420#endif
421
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800422 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
Andy Lutomirski0337b7e2019-11-25 22:37:44 -0800423 die("double fault", regs, error_code);
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800424 panic("Machine halted.");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200425}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200426
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100427DEFINE_IDTENTRY(exc_bounds)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800428{
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100429 if (notify_die(DIE_TRAP, "bounds", regs, 0,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800430 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700431 return;
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100432 cond_local_irq_enable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800433
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700434 if (!user_mode(regs))
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100435 die("bounds", regs, 0);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800436
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100437 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200438
439 cond_local_irq_disable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800440}
441
Jann Horn59c1dcb2019-12-19 00:11:48 +0100442enum kernel_gp_hint {
443 GP_NO_HINT,
444 GP_NON_CANONICAL,
445 GP_CANONICAL
446};
447
448/*
449 * When an uncaught #GP occurs, try to determine the memory address accessed by
450 * the instruction and return that address to the caller. Also, try to figure
451 * out whether any part of the access to that address was non-canonical.
452 */
453static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
454 unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Jann Horn59c1dcb2019-12-19 00:11:48 +0100456 u8 insn_buf[MAX_INSN_SIZE];
457 struct insn insn;
458
459 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
460 return GP_NO_HINT;
461
462 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
463 insn_get_modrm(&insn);
464 insn_get_sib(&insn);
465
466 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
467 if (*addr == -1UL)
468 return GP_NO_HINT;
469
470#ifdef CONFIG_X86_64
471 /*
472 * Check that:
473 * - the operand is not in the kernel half
474 * - the last byte of the operand is not in the user canonical half
475 */
476 if (*addr < ~__VIRTUAL_MASK &&
477 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
478 return GP_NON_CANONICAL;
479#endif
480
481 return GP_CANONICAL;
482}
483
484#define GPFSTR "general protection fault"
485
486dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code)
487{
488 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
Borislav Petkov36209762019-12-31 17:15:35 +0100489 enum kernel_gp_hint hint = GP_NO_HINT;
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200490 struct task_struct *tsk;
Borislav Petkov36209762019-12-31 17:15:35 +0100491 unsigned long gp_addr;
492 int ret;
Ingo Molnarb5964402008-02-26 11:15:50 +0100493
Linus Torvalds57780772015-09-01 08:40:25 -0700494 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100495 cond_local_irq_enable(regs);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200496
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800497 if (static_cpu_has(X86_FEATURE_UMIP)) {
498 if (user_mode(regs) && fixup_umip_exception(regs))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200499 goto exit;
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800500 }
501
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700502 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200503 local_irq_enable();
504 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200505 local_irq_disable();
Andy Lutomirski8c840142015-07-03 12:44:32 -0700506 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200509 tsk = current;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200510
Borislav Petkov36209762019-12-31 17:15:35 +0100511 if (user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200512 tsk->thread.error_code = error_code;
513 tsk->thread.trap_nr = X86_TRAP_GP;
Jann Horn76dee4a2018-08-28 22:14:16 +0200514
Borislav Petkov36209762019-12-31 17:15:35 +0100515 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
516 force_sig(SIGSEGV);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200517 goto exit;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Borislav Petkov36209762019-12-31 17:15:35 +0100520 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200521 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100522
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200523 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530524 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100525
Borislav Petkov36209762019-12-31 17:15:35 +0100526 /*
527 * To be potentially processing a kprobe fault and to trust the result
528 * from kprobe_running(), we have to be non-preemptible.
529 */
530 if (!preemptible() &&
531 kprobe_running() &&
532 kprobe_fault_handler(regs, X86_TRAP_GP))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200533 goto exit;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200534
Borislav Petkov36209762019-12-31 17:15:35 +0100535 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
536 if (ret == NOTIFY_STOP)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200537 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100538
539 if (error_code)
540 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
541 else
542 hint = get_kernel_gp_address(regs, &gp_addr);
543
544 if (hint != GP_NO_HINT)
545 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
546 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
547 : "maybe for address",
548 gp_addr);
549
550 /*
551 * KASAN is interested only in the non-canonical case, clear it
552 * otherwise.
553 */
554 if (hint != GP_NON_CANONICAL)
555 gp_addr = 0;
556
557 die_addr(desc, regs, error_code, gp_addr);
558
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200559exit:
560 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900562NOKPROBE_SYMBOL(do_general_protection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Masami Hiramatsu93266382014-04-17 17:18:14 +0900564dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Jiri Kosina17f41572013-07-23 10:09:28 +0200566 if (poke_int3_handler(regs))
567 return;
568
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700569 /*
Peter Zijlstra0d004492020-02-19 09:46:43 +0100570 * Unlike any other non-IST entry, we can be called from pretty much
571 * any location in the kernel through kprobes -- text_poke() will most
572 * likely be handled by poke_int3_handler() above. This means this
573 * handler is effectively NMI-like.
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700574 */
Peter Zijlstra0d004492020-02-19 09:46:43 +0100575 if (!user_mode(regs))
576 nmi_enter();
Andy Lutomirski65c668f2020-02-25 22:36:46 +0100577
Jason Wesself503b5a2010-05-20 21:04:25 -0500578#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
Kees Cookc9408262012-03-09 16:07:10 -0800579 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
580 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200581 goto exit;
Jason Wesself503b5a2010-05-20 21:04:25 -0500582#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
Srikar Dronamrajucc3a1bf2011-10-25 19:51:59 +0530583
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900584#ifdef CONFIG_KPROBES
585 if (kprobe_int3_handler(regs))
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000586 goto exit;
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900587#endif
588
Kees Cookc9408262012-03-09 16:07:10 -0800589 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
590 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200591 goto exit;
Ingo Molnarb5964402008-02-26 11:15:50 +0100592
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100593 cond_local_irq_enable(regs);
Eric W. Biederman164881b2018-04-16 14:29:39 -0500594 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, 0, NULL);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100595 cond_local_irq_disable(regs);
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700596
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200597exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100598 if (!user_mode(regs))
599 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900601NOKPROBE_SYMBOL(do_int3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200603#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100604/*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100605 * Help handler running on a per-cpu (IST or entry trampoline) stack
606 * to switch to the normal thread stack if the interrupted code was in
607 * user mode. The actual stack switch is done in entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100608 */
Thomas Gleixnerdaf7a692020-03-25 23:47:51 +0100609asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200610{
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100611 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
612 if (regs != eregs)
613 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200614 return regs;
615}
Andy Lutomirskib645af22014-11-22 18:00:33 -0800616
617struct bad_iret_stack {
618 void *error_entry_ret;
619 struct pt_regs regs;
620};
621
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100622asmlinkage __visible noinstr
Andy Lutomirskib645af22014-11-22 18:00:33 -0800623struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
624{
625 /*
626 * This is called from entry_64.S early in handling a fault
627 * caused by a bad iret to user mode. To handle the fault
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100628 * correctly, we want to move our stack frame to where it would
629 * be had we entered directly on the entry stack (rather than
630 * just below the IRET frame) and we want to pretend that the
631 * exception came from the IRET target.
Andy Lutomirskib645af22014-11-22 18:00:33 -0800632 */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100633 struct bad_iret_stack tmp, *new_stack =
634 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Andy Lutomirskib645af22014-11-22 18:00:33 -0800635
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100636 /* Copy the IRET target to the temporary storage. */
637 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800638
639 /* Copy the remainder of the stack from the current stack. */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100640 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
641
642 /* Update the entry stack */
643 memcpy(new_stack, &tmp, sizeof(tmp));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800644
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700645 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800646 return new_stack;
647}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200648#endif
649
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800650static bool is_sysenter_singlestep(struct pt_regs *regs)
651{
652 /*
653 * We don't try for precision here. If we're anywhere in the region of
654 * code that can be single-stepped in the SYSENTER entry path, then
655 * assume that this is a useless single-step trap due to SYSENTER
656 * being invoked with TF set. (We don't know in advance exactly
657 * which instructions will be hit because BTF could plausibly
658 * be set.)
659 */
660#ifdef CONFIG_X86_32
661 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
662 (unsigned long)__end_SYSENTER_singlestep_region -
663 (unsigned long)__begin_SYSENTER_singlestep_region;
664#elif defined(CONFIG_IA32_EMULATION)
665 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
666 (unsigned long)__end_entry_SYSENTER_compat -
667 (unsigned long)entry_SYSENTER_compat;
668#else
669 return false;
670#endif
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
674 * Our handling of the processor debug registers is non-trivial.
675 * We do not clear them on entry and exit from the kernel. Therefore
676 * it is possible to get a watchpoint trap here from inside the kernel.
677 * However, the code in ./ptrace.c has ensured that the user can
678 * only set watchpoints on userspace addresses. Therefore the in-kernel
679 * watchpoint trap can only occur in code which is reading/writing
680 * from user space. Such code must not hold kernel locks (since it
681 * can equally take a page fault), therefore it is safe to call
682 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100683 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * Code in ./signal.c ensures that the debug control register
685 * is restored before we deliver any signal, and therefore that
686 * user code runs with the correct debug control register even though
687 * we clear it here.
688 *
689 * Being careful here means that we don't have to be as careful in a
690 * lot of more complicated places (task switching can be a bit lazy
691 * about restoring all the debug state, and ptrace doesn't have to
692 * find every occurrence of the TF bit that could be saved away even
693 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200694 *
695 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900697dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct task_struct *tsk = current;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200700 int user_icebp = 0;
K.Prasad08d68322009-06-01 23:44:08 +0530701 unsigned long dr6;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530702 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Peter Zijlstra0d004492020-02-19 09:46:43 +0100704 nmi_enter();
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000705
K.Prasad08d68322009-06-01 23:44:08 +0530706 get_debugreg(dr6, 6);
Andy Lutomirski8bb56432016-03-09 19:00:29 -0800707 /*
708 * The Intel SDM says:
709 *
710 * Certain debug exceptions may clear bits 0-3. The remaining
711 * contents of the DR6 register are never cleared by the
712 * processor. To avoid confusion in identifying debug
713 * exceptions, debug handlers should clear the register before
714 * returning to the interrupted task.
715 *
716 * Keep it simple: clear DR6 immediately.
717 */
718 set_debugreg(0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
K.Prasad40f92492010-01-28 16:44:01 +0530720 /* Filter out all the reserved bits which are preset to 1 */
721 dr6 &= ~DR6_RESERVED;
722
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200723 /*
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800724 * The SDM says "The processor clears the BTF flag when it
725 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
726 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
727 */
728 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
729
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800730 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
731 is_sysenter_singlestep(regs))) {
732 dr6 &= ~DR_STEP;
733 if (!dr6)
734 goto exit;
735 /*
736 * else we might have gotten a single-step trap and hit a
737 * watchpoint at the same time, in which case we should fall
738 * through and handle the watchpoint.
739 */
740 }
741
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800742 /*
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200743 * If dr6 has no reason to give us about the origin of this trap,
744 * then it's very likely the result of an icebp/int01 trap.
745 * User wants a sigtrap for that.
746 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700747 if (!dr6 && user_mode(regs))
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200748 user_icebp = 1;
749
K.Prasad08d68322009-06-01 23:44:08 +0530750 /* Store the virtualized DR6 value */
751 tsk->thread.debugreg6 = dr6;
752
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900753#ifdef CONFIG_KPROBES
754 if (kprobe_debug_handler(regs))
755 goto exit;
756#endif
757
Rusty Russell5a802e12013-06-16 14:12:47 +0930758 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
K.Prasad62edab92009-06-01 23:47:06 +0530759 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200760 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200761
Steven Rostedt42181182011-12-16 11:43:02 -0500762 /*
763 * Let others (NMI) know that the debug stack is in use
764 * as we may switch to the interrupt stack.
765 */
766 debug_stack_usage_inc();
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* It's safe to allow irq's after DR6 has been saved */
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100769 cond_local_irq_enable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700771 if (v8086_mode(regs)) {
Kees Cookc9408262012-03-09 16:07:10 -0800772 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
773 X86_TRAP_DB);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100774 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500775 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200776 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800779 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
780 /*
781 * Historical junk that used to handle SYSENTER single-stepping.
782 * This should be unreachable now. If we survive for a while
783 * without anyone hitting this warning, we'll turn this into
784 * an oops.
785 */
K.Prasad08d68322009-06-01 23:44:08 +0530786 tsk->thread.debugreg6 &= ~DR_STEP;
787 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
788 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
K.Prasad08d68322009-06-01 23:44:08 +0530790 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200791 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
Eric W. Biederman28d42ea2019-02-05 18:25:11 -0600792 send_sigtrap(regs, error_code, si_code);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100793 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500794 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200796exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100797 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900799NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801/*
802 * Note that we play around with the 'TS' bit in an attempt to get
803 * the correct behaviour even in the presence of the asynchronous
804 * IRQ13 behaviour
805 */
Oleg Nesterov5e1b05b2014-05-08 20:34:00 +0200806static void math_error(struct pt_regs *regs, int error_code, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Brian Gerste2e75c912010-03-21 09:00:45 -0400808 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200809 struct fpu *fpu = &task->thread.fpu;
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200810 int si_code;
Kees Cookc9408262012-03-09 16:07:10 -0800811 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
812 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400813
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100814 cond_local_irq_enable(regs);
Brian Gerste2e75c912010-03-21 09:00:45 -0400815
Ingo Molnare1cebad2015-04-30 09:29:38 +0200816 if (!user_mode(regs)) {
Jann Horn81fd9c12018-08-28 22:14:19 +0200817 if (fixup_exception(regs, trapnr, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200818 goto exit;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000819
820 task->thread.error_code = error_code;
821 task->thread.trap_nr = trapnr;
822
823 if (notify_die(DIE_TRAP, str, regs, error_code,
824 trapnr, SIGFPE) != NOTIFY_STOP)
Brian Gerste2e75c912010-03-21 09:00:45 -0400825 die(str, regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200826 goto exit;
Brian Gerste2e75c912010-03-21 09:00:45 -0400827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /*
830 * Save the info for the exception handler and clear the error.
831 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200832 fpu__save(fpu);
833
834 task->thread.trap_nr = trapnr;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400835 task->thread.error_code = error_code;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800836
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200837 si_code = fpu__exception_code(fpu, trapnr);
Ingo Molnare1cebad2015-04-30 09:29:38 +0200838 /* Retry when we get spurious exceptions: */
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200839 if (!si_code)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200840 goto exit;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200841
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200842 force_sig_fault(SIGFPE, si_code,
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500843 (void __user *)uprobe_get_trap_addr(regs));
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200844exit:
845 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200848dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds57780772015-09-01 08:40:25 -0700850 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Kees Cookc9408262012-03-09 16:07:10 -0800851 math_error(regs, error_code, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200854dotraplinkage void
855do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Linus Torvalds57780772015-09-01 08:40:25 -0700857 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Kees Cookc9408262012-03-09 16:07:10 -0800858 math_error(regs, error_code, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200861dotraplinkage void
862do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Thomas Gleixnerd244d0e2020-02-25 22:36:41 +0100864 /*
865 * This addresses a Pentium Pro Erratum:
866 *
867 * PROBLEM: If the APIC subsystem is configured in mixed mode with
868 * Virtual Wire mode implemented through the local APIC, an
869 * interrupt vector of 0Fh (Intel reserved encoding) may be
870 * generated by the local APIC (Int 15). This vector may be
871 * generated upon receipt of a spurious interrupt (an interrupt
872 * which is removed before the system receives the INTA sequence)
873 * instead of the programmed 8259 spurious interrupt vector.
874 *
875 * IMPLICATION: The spurious interrupt vector programmed in the
876 * 8259 is normally handled by an operating system's spurious
877 * interrupt handler. However, a vector of 0Fh is unknown to some
878 * operating systems, which would crash if this erratum occurred.
879 *
880 * In theory this could be limited to 32bit, but the handler is not
881 * hurting and who knows which other CPUs suffer from this.
882 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100885DEFINE_IDTENTRY(exc_device_not_available)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200886{
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100887 unsigned long cr0 = read_cr0();
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700888
Brian Gersta334fe42010-09-03 21:17:15 -0400889#ifdef CONFIG_MATH_EMULATION
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100890 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
Tejun Heod3157602009-02-09 22:17:39 +0900891 struct math_emu_info info = { };
892
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100893 cond_local_irq_enable(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900894
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500895 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900896 math_emulate(&info);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200897
898 cond_local_irq_disable(regs);
Brian Gersta334fe42010-09-03 21:17:15 -0400899 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200900 }
Brian Gersta334fe42010-09-03 21:17:15 -0400901#endif
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700902
903 /* This should not happen. */
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700904 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
905 /* Try to fix it up and carry on. */
906 write_cr0(cr0 & ~X86_CR0_TS);
907 } else {
908 /*
909 * Something terrible happened, and we're better off trying
910 * to kill the task than getting stuck in a never-ending
911 * loop of #NM faults.
912 */
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100913 die("unexpected #NM exception", regs, 0);
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700914 }
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200915}
916
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200917#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200918dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200919{
Linus Torvalds57780772015-09-01 08:40:25 -0700920 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200921 local_irq_enable();
922
Kees Cookc9408262012-03-09 16:07:10 -0800923 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200924 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
925 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500926 ILL_BADSTK, (void __user *)NULL);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200927 }
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200928 local_irq_disable();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200929}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200930#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932void __init trap_init(void)
933{
Andy Lutomirski40e7f942017-12-04 15:07:26 +0100934 /* Init cpu_entry_area before IST entries are set up */
935 setup_cpu_entry_areas();
936
Thomas Gleixnerb70543a2017-08-28 08:47:53 +0200937 idt_setup_traps();
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
Kees Cook4eefbe72013-04-10 12:24:22 -0700940 * Set the IDT descriptor to a fixed read-only location, so that the
941 * "sidt" instruction will not leak the location of the kernel, and
942 * to defend the IDT against arbitrary memory write vulnerabilities.
943 * It will be reloaded in cpu_init() */
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100944 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
945 PAGE_KERNEL_RO);
946 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
Kees Cook4eefbe72013-04-10 12:24:22 -0700947
948 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100949 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951 cpu_init();
952
Thomas Gleixner90f62252017-08-28 08:47:52 +0200953 idt_setup_ist_traps();
Wang Nanb4d83272015-02-26 13:49:39 +0800954
Thomas Gleixner0a309082017-08-28 08:47:51 +0200955 idt_setup_debugidt_traps();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}