blob: 88ba5f0400fd3538212534c1500ab55a3584ad6e [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
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
Eric W. Biederman164881b2018-04-16 14:29:39 -0500267#define IP ((void __user *)uprobe_get_trap_addr(regs))
268#define DO_ERROR(trapnr, signr, sicode, addr, str, name) \
269dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
270{ \
271 do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Eric W. Biederman164881b2018-04-16 14:29:39 -0500274DO_ERROR(X86_TRAP_SS, SIGBUS, 0, NULL, "stack segment", stack_segment)
Eric W. Biederman164881b2018-04-16 14:29:39 -0500275#undef IP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Peter Zijlstra (Intel)6650cdd2020-01-26 12:05:35 -0800277dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code)
278{
279 char *str = "alignment check";
280
281 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
282
283 if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
284 return;
285
286 if (!user_mode(regs))
287 die("Split lock detected\n", regs, error_code);
288
289 local_irq_enable();
290
291 if (handle_user_split_lock(regs, error_code))
292 return;
293
294 do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
295 error_code, BUS_ADRALN, NULL);
296}
297
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700298#ifdef CONFIG_VMAP_STACK
Andy Lutomirski6271cfd2016-08-30 17:27:57 -0700299__visible void __noreturn handle_stack_overflow(const char *message,
300 struct pt_regs *regs,
301 unsigned long fault_address)
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700302{
303 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
304 (void *)fault_address, current->stack,
305 (char *)current->stack + THREAD_SIZE - 1);
306 die(message, regs, 0);
307
308 /* Be absolutely certain we don't return. */
Rasmus Villemoes2022cce2018-10-27 00:20:04 +0200309 panic("%s", message);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700310}
311#endif
312
Andy Lutomirski7d8d8cf2019-11-20 23:06:41 -0800313/*
314 * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
315 *
316 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
317 * SDM's warnings about double faults being unrecoverable, returning works as
318 * expected. Presumably what the SDM actually means is that the CPU may get
319 * the register state wrong on entry, so returning could be a bad idea.
320 *
321 * Various CPU engineers have promised that double faults due to an IRET fault
322 * while the stack is read-only are, in fact, recoverable.
323 *
324 * On x86_32, this is entered through a task gate, and regs are synthesized
325 * from the TSS. Returning is, in principle, okay, but changes to regs will
326 * be lost. If, for some reason, we need to return to a context with modified
327 * regs, the shim code could be adjusted to synchronize the registers.
328 */
Peter Zijlstraa0d14b82019-07-11 13:40:59 +0200329dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200330{
331 static const char str[] = "double fault";
332 struct task_struct *tsk = current;
333
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800334#ifdef CONFIG_X86_ESPFIX64
335 extern unsigned char native_irq_return_iret[];
336
337 /*
338 * If IRET takes a non-IST fault on the espfix64 stack, then we
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100339 * end up promoting it to a doublefault. In that case, take
340 * advantage of the fact that we're not using the normal (TSS.sp0)
341 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
342 * and then modify our own IRET frame so that, when we return,
343 * we land directly at the #GP(0) vector with the stack already
344 * set up according to its expectations.
345 *
346 * The net result is that our #GP handler will think that we
347 * entered from usermode with the bad user context.
Andy Lutomirski95927472014-11-19 17:41:09 -0800348 *
Peter Zijlstra0d004492020-02-19 09:46:43 +0100349 * No need for nmi_enter() here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800350 */
Andy Lutomirskic739f932017-12-12 07:56:36 -0800351 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800352 regs->cs == __KERNEL_CS &&
353 regs->ip == (unsigned long)native_irq_return_iret)
354 {
Andy Lutomirskic482fee2017-12-04 15:07:29 +0100355 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Peter Zijlstrae9660392020-02-20 13:17:27 +0100356 unsigned long *p = (unsigned long *)regs->sp;
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800357
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100358 /*
359 * regs->sp points to the failing IRET frame on the
360 * ESPFIX64 stack. Copy it to the entry stack. This fills
361 * in gpregs->ss through gpregs->ip.
362 *
363 */
Peter Zijlstrae9660392020-02-20 13:17:27 +0100364 gpregs->ip = p[0];
365 gpregs->cs = p[1];
366 gpregs->flags = p[2];
367 gpregs->sp = p[3];
368 gpregs->ss = p[4];
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100369 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
370
371 /*
372 * Adjust our frame so that we return straight to the #GP
373 * vector with the expected RSP value. This is safe because
374 * we won't enable interupts or schedule before we invoke
375 * general_protection, so nothing will clobber the stack
376 * frame we just set up.
Andy Lutomirskibd7b1f72018-09-03 15:59:42 -0700377 *
378 * We will enter general_protection with kernel GSBASE,
379 * which is what the stub expects, given that the faulting
380 * RIP will be the IRET instruction.
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100381 */
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800382 regs->ip = (unsigned long)general_protection;
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100383 regs->sp = (unsigned long)&gpregs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800384
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800385 return;
386 }
387#endif
388
Peter Zijlstra0d004492020-02-19 09:46:43 +0100389 nmi_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800390 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200391
392 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530393 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200394
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700395#ifdef CONFIG_VMAP_STACK
396 /*
397 * If we overflow the stack into a guard page, the CPU will fail
398 * to deliver #PF and will send #DF instead. Similarly, if we
399 * take any non-IST exception while too close to the bottom of
400 * the stack, the processor will get a page fault while
401 * delivering the exception and will generate a double fault.
402 *
403 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
404 * Page-Fault Exception (#PF):
405 *
406 * Processors update CR2 whenever a page fault is detected. If a
407 * second page fault occurs while an earlier page fault is being
Andy Lutomirski6d9256f2017-12-04 15:07:22 +0100408 * delivered, the faulting linear address of the second fault will
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700409 * overwrite the contents of CR2 (replacing the previous
410 * address). These updates to CR2 occur even if the page fault
411 * results in a double fault or occurs during the delivery of a
412 * double fault.
413 *
414 * The logic below has a small possibility of incorrectly diagnosing
415 * some errors as stack overflows. For example, if the IDT or GDT
416 * gets corrupted such that #GP delivery fails due to a bad descriptor
417 * causing #GP and we hit this condition while CR2 coincidentally
418 * points to the stack guard page, we'll think we overflowed the
419 * stack. Given that we're going to panic one way or another
420 * if this happens, this isn't necessarily worth fixing.
421 *
422 * If necessary, we could improve the test by only diagnosing
423 * a stack overflow if the saved RSP points within 47 bytes of
424 * the bottom of the stack: if RSP == tsk_stack + 48 and we
425 * take an exception, the stack is already aligned and there
426 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
427 * possible error code, so a stack overflow would *not* double
428 * fault. With any less space left, exception delivery could
429 * fail, and, as a practical matter, we've overflowed the
430 * stack even if the actual trigger for the double fault was
431 * something else.
432 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -0700433 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
434 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
435#endif
436
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800437 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
Andy Lutomirski0337b7e2019-11-25 22:37:44 -0800438 die("double fault", regs, error_code);
Andy Lutomirski93efbde2019-11-20 22:12:38 -0800439 panic("Machine halted.");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200440}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200441
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100442DEFINE_IDTENTRY(exc_bounds)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800443{
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100444 if (notify_die(DIE_TRAP, "bounds", regs, 0,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800445 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700446 return;
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100447 cond_local_irq_enable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800448
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700449 if (!user_mode(regs))
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100450 die("bounds", regs, 0);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800451
Thomas Gleixner58d9c812020-02-25 23:16:17 +0100452 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200453
454 cond_local_irq_disable(regs);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800455}
456
Jann Horn59c1dcb2019-12-19 00:11:48 +0100457enum kernel_gp_hint {
458 GP_NO_HINT,
459 GP_NON_CANONICAL,
460 GP_CANONICAL
461};
462
463/*
464 * When an uncaught #GP occurs, try to determine the memory address accessed by
465 * the instruction and return that address to the caller. Also, try to figure
466 * out whether any part of the access to that address was non-canonical.
467 */
468static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
469 unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jann Horn59c1dcb2019-12-19 00:11:48 +0100471 u8 insn_buf[MAX_INSN_SIZE];
472 struct insn insn;
473
474 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
475 return GP_NO_HINT;
476
477 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
478 insn_get_modrm(&insn);
479 insn_get_sib(&insn);
480
481 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
482 if (*addr == -1UL)
483 return GP_NO_HINT;
484
485#ifdef CONFIG_X86_64
486 /*
487 * Check that:
488 * - the operand is not in the kernel half
489 * - the last byte of the operand is not in the user canonical half
490 */
491 if (*addr < ~__VIRTUAL_MASK &&
492 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
493 return GP_NON_CANONICAL;
494#endif
495
496 return GP_CANONICAL;
497}
498
499#define GPFSTR "general protection fault"
500
501dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code)
502{
503 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
Borislav Petkov36209762019-12-31 17:15:35 +0100504 enum kernel_gp_hint hint = GP_NO_HINT;
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200505 struct task_struct *tsk;
Borislav Petkov36209762019-12-31 17:15:35 +0100506 unsigned long gp_addr;
507 int ret;
Ingo Molnarb5964402008-02-26 11:15:50 +0100508
Linus Torvalds57780772015-09-01 08:40:25 -0700509 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100510 cond_local_irq_enable(regs);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200511
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800512 if (static_cpu_has(X86_FEATURE_UMIP)) {
513 if (user_mode(regs) && fixup_umip_exception(regs))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200514 goto exit;
Ricardo Neri6fc9dc82017-11-05 18:27:55 -0800515 }
516
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700517 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200518 local_irq_enable();
519 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200520 local_irq_disable();
Andy Lutomirski8c840142015-07-03 12:44:32 -0700521 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200524 tsk = current;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200525
Borislav Petkov36209762019-12-31 17:15:35 +0100526 if (user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200527 tsk->thread.error_code = error_code;
528 tsk->thread.trap_nr = X86_TRAP_GP;
Jann Horn76dee4a2018-08-28 22:14:16 +0200529
Borislav Petkov36209762019-12-31 17:15:35 +0100530 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
531 force_sig(SIGSEGV);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200532 goto exit;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Borislav Petkov36209762019-12-31 17:15:35 +0100535 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200536 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100537
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200538 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530539 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100540
Borislav Petkov36209762019-12-31 17:15:35 +0100541 /*
542 * To be potentially processing a kprobe fault and to trust the result
543 * from kprobe_running(), we have to be non-preemptible.
544 */
545 if (!preemptible() &&
546 kprobe_running() &&
547 kprobe_fault_handler(regs, X86_TRAP_GP))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200548 goto exit;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200549
Borislav Petkov36209762019-12-31 17:15:35 +0100550 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
551 if (ret == NOTIFY_STOP)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200552 goto exit;
Borislav Petkov36209762019-12-31 17:15:35 +0100553
554 if (error_code)
555 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
556 else
557 hint = get_kernel_gp_address(regs, &gp_addr);
558
559 if (hint != GP_NO_HINT)
560 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
561 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
562 : "maybe for address",
563 gp_addr);
564
565 /*
566 * KASAN is interested only in the non-canonical case, clear it
567 * otherwise.
568 */
569 if (hint != GP_NON_CANONICAL)
570 gp_addr = 0;
571
572 die_addr(desc, regs, error_code, gp_addr);
573
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200574exit:
575 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900577NOKPROBE_SYMBOL(do_general_protection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Masami Hiramatsu93266382014-04-17 17:18:14 +0900579dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Jiri Kosina17f41572013-07-23 10:09:28 +0200581 if (poke_int3_handler(regs))
582 return;
583
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700584 /*
Peter Zijlstra0d004492020-02-19 09:46:43 +0100585 * Unlike any other non-IST entry, we can be called from pretty much
586 * any location in the kernel through kprobes -- text_poke() will most
587 * likely be handled by poke_int3_handler() above. This means this
588 * handler is effectively NMI-like.
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700589 */
Peter Zijlstra0d004492020-02-19 09:46:43 +0100590 if (!user_mode(regs))
591 nmi_enter();
Andy Lutomirski65c668f2020-02-25 22:36:46 +0100592
Jason Wesself503b5a2010-05-20 21:04:25 -0500593#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
Kees Cookc9408262012-03-09 16:07:10 -0800594 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
595 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200596 goto exit;
Jason Wesself503b5a2010-05-20 21:04:25 -0500597#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
Srikar Dronamrajucc3a1bf2011-10-25 19:51:59 +0530598
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900599#ifdef CONFIG_KPROBES
600 if (kprobe_int3_handler(regs))
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000601 goto exit;
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900602#endif
603
Kees Cookc9408262012-03-09 16:07:10 -0800604 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
605 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200606 goto exit;
Ingo Molnarb5964402008-02-26 11:15:50 +0100607
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100608 cond_local_irq_enable(regs);
Eric W. Biederman164881b2018-04-16 14:29:39 -0500609 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, 0, NULL);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100610 cond_local_irq_disable(regs);
Andy Lutomirskid8ba61b2015-07-23 15:37:48 -0700611
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200612exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100613 if (!user_mode(regs))
614 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900616NOKPROBE_SYMBOL(do_int3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200618#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100619/*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100620 * Help handler running on a per-cpu (IST or entry trampoline) stack
621 * to switch to the normal thread stack if the interrupted code was in
622 * user mode. The actual stack switch is done in entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100623 */
Thomas Gleixnerdaf7a692020-03-25 23:47:51 +0100624asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200625{
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100626 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
627 if (regs != eregs)
628 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200629 return regs;
630}
Andy Lutomirskib645af22014-11-22 18:00:33 -0800631
632struct bad_iret_stack {
633 void *error_entry_ret;
634 struct pt_regs regs;
635};
636
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100637asmlinkage __visible noinstr
Andy Lutomirskib645af22014-11-22 18:00:33 -0800638struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
639{
640 /*
641 * This is called from entry_64.S early in handling a fault
642 * caused by a bad iret to user mode. To handle the fault
Andy Lutomirski7f2590a2017-12-04 15:07:23 +0100643 * correctly, we want to move our stack frame to where it would
644 * be had we entered directly on the entry stack (rather than
645 * just below the IRET frame) and we want to pretend that the
646 * exception came from the IRET target.
Andy Lutomirskib645af22014-11-22 18:00:33 -0800647 */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100648 struct bad_iret_stack tmp, *new_stack =
649 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
Andy Lutomirskib645af22014-11-22 18:00:33 -0800650
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100651 /* Copy the IRET target to the temporary storage. */
652 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800653
654 /* Copy the remainder of the stack from the current stack. */
Thomas Gleixnerd73a3322020-03-25 19:53:38 +0100655 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
656
657 /* Update the entry stack */
658 memcpy(new_stack, &tmp, sizeof(tmp));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800659
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700660 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800661 return new_stack;
662}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200663#endif
664
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800665static bool is_sysenter_singlestep(struct pt_regs *regs)
666{
667 /*
668 * We don't try for precision here. If we're anywhere in the region of
669 * code that can be single-stepped in the SYSENTER entry path, then
670 * assume that this is a useless single-step trap due to SYSENTER
671 * being invoked with TF set. (We don't know in advance exactly
672 * which instructions will be hit because BTF could plausibly
673 * be set.)
674 */
675#ifdef CONFIG_X86_32
676 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
677 (unsigned long)__end_SYSENTER_singlestep_region -
678 (unsigned long)__begin_SYSENTER_singlestep_region;
679#elif defined(CONFIG_IA32_EMULATION)
680 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
681 (unsigned long)__end_entry_SYSENTER_compat -
682 (unsigned long)entry_SYSENTER_compat;
683#else
684 return false;
685#endif
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
689 * Our handling of the processor debug registers is non-trivial.
690 * We do not clear them on entry and exit from the kernel. Therefore
691 * it is possible to get a watchpoint trap here from inside the kernel.
692 * However, the code in ./ptrace.c has ensured that the user can
693 * only set watchpoints on userspace addresses. Therefore the in-kernel
694 * watchpoint trap can only occur in code which is reading/writing
695 * from user space. Such code must not hold kernel locks (since it
696 * can equally take a page fault), therefore it is safe to call
697 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * Code in ./signal.c ensures that the debug control register
700 * is restored before we deliver any signal, and therefore that
701 * user code runs with the correct debug control register even though
702 * we clear it here.
703 *
704 * Being careful here means that we don't have to be as careful in a
705 * lot of more complicated places (task switching can be a bit lazy
706 * about restoring all the debug state, and ptrace doesn't have to
707 * find every occurrence of the TF bit that could be saved away even
708 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200709 *
710 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900712dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct task_struct *tsk = current;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200715 int user_icebp = 0;
K.Prasad08d68322009-06-01 23:44:08 +0530716 unsigned long dr6;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530717 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Peter Zijlstra0d004492020-02-19 09:46:43 +0100719 nmi_enter();
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000720
K.Prasad08d68322009-06-01 23:44:08 +0530721 get_debugreg(dr6, 6);
Andy Lutomirski8bb56432016-03-09 19:00:29 -0800722 /*
723 * The Intel SDM says:
724 *
725 * Certain debug exceptions may clear bits 0-3. The remaining
726 * contents of the DR6 register are never cleared by the
727 * processor. To avoid confusion in identifying debug
728 * exceptions, debug handlers should clear the register before
729 * returning to the interrupted task.
730 *
731 * Keep it simple: clear DR6 immediately.
732 */
733 set_debugreg(0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
K.Prasad40f92492010-01-28 16:44:01 +0530735 /* Filter out all the reserved bits which are preset to 1 */
736 dr6 &= ~DR6_RESERVED;
737
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200738 /*
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800739 * The SDM says "The processor clears the BTF flag when it
740 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
741 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
742 */
743 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
744
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800745 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
746 is_sysenter_singlestep(regs))) {
747 dr6 &= ~DR_STEP;
748 if (!dr6)
749 goto exit;
750 /*
751 * else we might have gotten a single-step trap and hit a
752 * watchpoint at the same time, in which case we should fall
753 * through and handle the watchpoint.
754 */
755 }
756
Andy Lutomirski81edd9f2016-03-09 19:00:28 -0800757 /*
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200758 * If dr6 has no reason to give us about the origin of this trap,
759 * then it's very likely the result of an icebp/int01 trap.
760 * User wants a sigtrap for that.
761 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700762 if (!dr6 && user_mode(regs))
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200763 user_icebp = 1;
764
K.Prasad08d68322009-06-01 23:44:08 +0530765 /* Store the virtualized DR6 value */
766 tsk->thread.debugreg6 = dr6;
767
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900768#ifdef CONFIG_KPROBES
769 if (kprobe_debug_handler(regs))
770 goto exit;
771#endif
772
Rusty Russell5a802e12013-06-16 14:12:47 +0930773 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
K.Prasad62edab92009-06-01 23:47:06 +0530774 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200775 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200776
Steven Rostedt42181182011-12-16 11:43:02 -0500777 /*
778 * Let others (NMI) know that the debug stack is in use
779 * as we may switch to the interrupt stack.
780 */
781 debug_stack_usage_inc();
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* It's safe to allow irq's after DR6 has been saved */
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100784 cond_local_irq_enable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700786 if (v8086_mode(regs)) {
Kees Cookc9408262012-03-09 16:07:10 -0800787 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
788 X86_TRAP_DB);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100789 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500790 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200791 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800794 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
795 /*
796 * Historical junk that used to handle SYSENTER single-stepping.
797 * This should be unreachable now. If we survive for a while
798 * without anyone hitting this warning, we'll turn this into
799 * an oops.
800 */
K.Prasad08d68322009-06-01 23:44:08 +0530801 tsk->thread.debugreg6 &= ~DR_STEP;
802 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
803 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
K.Prasad08d68322009-06-01 23:44:08 +0530805 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200806 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
Eric W. Biederman28d42ea2019-02-05 18:25:11 -0600807 send_sigtrap(regs, error_code, si_code);
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100808 cond_local_irq_disable(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500809 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200811exit:
Peter Zijlstra0d004492020-02-19 09:46:43 +0100812 nmi_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900814NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/*
817 * Note that we play around with the 'TS' bit in an attempt to get
818 * the correct behaviour even in the presence of the asynchronous
819 * IRQ13 behaviour
820 */
Oleg Nesterov5e1b05b2014-05-08 20:34:00 +0200821static void math_error(struct pt_regs *regs, int error_code, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Brian Gerste2e75c912010-03-21 09:00:45 -0400823 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200824 struct fpu *fpu = &task->thread.fpu;
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200825 int si_code;
Kees Cookc9408262012-03-09 16:07:10 -0800826 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
827 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400828
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100829 cond_local_irq_enable(regs);
Brian Gerste2e75c912010-03-21 09:00:45 -0400830
Ingo Molnare1cebad2015-04-30 09:29:38 +0200831 if (!user_mode(regs)) {
Jann Horn81fd9c12018-08-28 22:14:19 +0200832 if (fixup_exception(regs, trapnr, error_code, 0))
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200833 goto exit;
Siarhei Liakh3ae62952018-06-14 19:36:07 +0000834
835 task->thread.error_code = error_code;
836 task->thread.trap_nr = trapnr;
837
838 if (notify_die(DIE_TRAP, str, regs, error_code,
839 trapnr, SIGFPE) != NOTIFY_STOP)
Brian Gerste2e75c912010-03-21 09:00:45 -0400840 die(str, regs, error_code);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200841 goto exit;
Brian Gerste2e75c912010-03-21 09:00:45 -0400842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * Save the info for the exception handler and clear the error.
846 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200847 fpu__save(fpu);
848
849 task->thread.trap_nr = trapnr;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400850 task->thread.error_code = error_code;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800851
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200852 si_code = fpu__exception_code(fpu, trapnr);
Ingo Molnare1cebad2015-04-30 09:29:38 +0200853 /* Retry when we get spurious exceptions: */
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200854 if (!si_code)
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200855 goto exit;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200856
Eric W. Biederman0a996c12018-09-18 01:16:39 +0200857 force_sig_fault(SIGFPE, si_code,
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500858 (void __user *)uprobe_get_trap_addr(regs));
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200859exit:
860 cond_local_irq_disable(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200863dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds57780772015-09-01 08:40:25 -0700865 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Kees Cookc9408262012-03-09 16:07:10 -0800866 math_error(regs, error_code, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200869dotraplinkage void
870do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Linus Torvalds57780772015-09-01 08:40:25 -0700872 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Kees Cookc9408262012-03-09 16:07:10 -0800873 math_error(regs, error_code, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200876dotraplinkage void
877do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Thomas Gleixnerd244d0e2020-02-25 22:36:41 +0100879 /*
880 * This addresses a Pentium Pro Erratum:
881 *
882 * PROBLEM: If the APIC subsystem is configured in mixed mode with
883 * Virtual Wire mode implemented through the local APIC, an
884 * interrupt vector of 0Fh (Intel reserved encoding) may be
885 * generated by the local APIC (Int 15). This vector may be
886 * generated upon receipt of a spurious interrupt (an interrupt
887 * which is removed before the system receives the INTA sequence)
888 * instead of the programmed 8259 spurious interrupt vector.
889 *
890 * IMPLICATION: The spurious interrupt vector programmed in the
891 * 8259 is normally handled by an operating system's spurious
892 * interrupt handler. However, a vector of 0Fh is unknown to some
893 * operating systems, which would crash if this erratum occurred.
894 *
895 * In theory this could be limited to 32bit, but the handler is not
896 * hurting and who knows which other CPUs suffer from this.
897 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200898}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100900DEFINE_IDTENTRY(exc_device_not_available)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200901{
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100902 unsigned long cr0 = read_cr0();
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700903
Brian Gersta334fe42010-09-03 21:17:15 -0400904#ifdef CONFIG_MATH_EMULATION
Borislav Petkovee35b9b2019-01-17 13:02:05 +0100905 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
Tejun Heod3157602009-02-09 22:17:39 +0900906 struct math_emu_info info = { };
907
Alexander Kuleshovd99e1bd2016-01-25 20:41:46 +0100908 cond_local_irq_enable(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900909
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500910 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900911 math_emulate(&info);
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200912
913 cond_local_irq_disable(regs);
Brian Gersta334fe42010-09-03 21:17:15 -0400914 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200915 }
Brian Gersta334fe42010-09-03 21:17:15 -0400916#endif
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700917
918 /* This should not happen. */
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700919 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
920 /* Try to fix it up and carry on. */
921 write_cr0(cr0 & ~X86_CR0_TS);
922 } else {
923 /*
924 * Something terrible happened, and we're better off trying
925 * to kill the task than getting stuck in a never-ending
926 * loop of #NM faults.
927 */
Thomas Gleixner866ae2c2020-02-25 23:16:19 +0100928 die("unexpected #NM exception", regs, 0);
Andy Lutomirskibef8b6d2016-10-31 15:18:47 -0700929 }
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200930}
931
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200932#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200933dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200934{
Linus Torvalds57780772015-09-01 08:40:25 -0700935 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200936 local_irq_enable();
937
Kees Cookc9408262012-03-09 16:07:10 -0800938 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200939 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
940 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
Eric W. Biederman164881b2018-04-16 14:29:39 -0500941 ILL_BADSTK, (void __user *)NULL);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200942 }
Thomas Gleixnerca4c6a92019-10-23 14:27:10 +0200943 local_irq_disable();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200944}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200945#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947void __init trap_init(void)
948{
Andy Lutomirski40e7f942017-12-04 15:07:26 +0100949 /* Init cpu_entry_area before IST entries are set up */
950 setup_cpu_entry_areas();
951
Thomas Gleixnerb70543a2017-08-28 08:47:53 +0200952 idt_setup_traps();
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
Kees Cook4eefbe72013-04-10 12:24:22 -0700955 * Set the IDT descriptor to a fixed read-only location, so that the
956 * "sidt" instruction will not leak the location of the kernel, and
957 * to defend the IDT against arbitrary memory write vulnerabilities.
958 * It will be reloaded in cpu_init() */
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100959 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
960 PAGE_KERNEL_RO);
961 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
Kees Cook4eefbe72013-04-10 12:24:22 -0700962
963 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100964 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
966 cpu_init();
967
Thomas Gleixner90f62252017-08-28 08:47:52 +0200968 idt_setup_ist_traps();
Wang Nanb4d83272015-02-26 13:49:39 +0800969
Thomas Gleixner0a309082017-08-28 08:47:51 +0200970 idt_setup_debugidt_traps();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}