blob: 0d0e922fafc149400b4320c793e1d311c96d147b [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>
24#include <linux/module.h>
25#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>
32#include <linux/timer.h>
33#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080034#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010035#include <linux/nmi.h>
36#include <linux/mm.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020037#include <linux/smp.h>
38#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#ifdef CONFIG_EISA
41#include <linux/ioport.h>
42#include <linux/eisa.h>
43#endif
44
Dave Jiangc0d12172007-07-19 01:49:46 -070045#if defined(CONFIG_EDAC)
46#include <linux/edac.h>
47#endif
48
Vegard Nossumf8561292008-04-04 00:53:23 +020049#include <asm/kmemcheck.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010050#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/debugreg.h>
Arun Sharma600634972011-07-26 16:09:06 -070053#include <linux/atomic.h>
Steven Rostedt08d636b2011-08-16 09:57:10 -040054#include <asm/ftrace.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020055#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/desc.h>
57#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080058#include <asm/fpu-internal.h>
Hidetoshi Seto9e55e442009-06-15 17:22:15 +090059#include <asm/mce.h>
Kees Cook4eefbe72013-04-10 12:24:22 -070060#include <asm/fixmap.h>
Ingo Molnar1164dd02009-01-28 19:34:09 +010061#include <asm/mach_traps.h>
Jiri Kosina17f41572013-07-23 10:09:28 +020062#include <asm/alternative.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020063
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020064#ifdef CONFIG_X86_64
Thomas Gleixner428cf902009-08-20 10:35:46 +020065#include <asm/x86_init.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020066#include <asm/pgalloc.h>
67#include <asm/proto.h>
Kees Cook4df05f32013-07-16 11:34:41 -070068
69/* No need to be aligned, but done to keep all IDTs defined the same way. */
70gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020071#else
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020072#include <asm/processor-flags.h>
Ingo Molnar8e6dafd2009-02-23 00:34:39 +010073#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075asmlinkage int system_call(void);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Kees Cook4df05f32013-07-16 11:34:41 -070078/* Must be page-aligned because the real IDT is used in a fixmap. */
79gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
80
Yinghai Lub77b8812008-12-19 15:23:44 -080081DECLARE_BITMAP(used_vectors, NR_VECTORS);
82EXPORT_SYMBOL_GPL(used_vectors);
83
Alexander van Heukelum762db432008-09-09 21:55:55 +020084static inline void conditional_sti(struct pt_regs *regs)
85{
86 if (regs->flags & X86_EFLAGS_IF)
87 local_irq_enable();
88}
89
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020090static inline void preempt_conditional_sti(struct pt_regs *regs)
91{
Peter Zijlstrabdb43802013-09-10 12:15:23 +020092 preempt_count_inc();
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020093 if (regs->flags & X86_EFLAGS_IF)
94 local_irq_enable();
95}
96
Thomas Gleixnerbe716612009-01-13 23:36:34 +010097static inline void conditional_cli(struct pt_regs *regs)
98{
99 if (regs->flags & X86_EFLAGS_IF)
100 local_irq_disable();
101}
102
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200103static inline void preempt_conditional_cli(struct pt_regs *regs)
104{
105 if (regs->flags & X86_EFLAGS_IF)
106 local_irq_disable();
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200107 preempt_count_dec();
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200108}
109
Masami Hiramatsu93266382014-04-17 17:18:14 +0900110static nokprobe_inline int
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200111do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
112 struct pt_regs *regs, long error_code)
113{
114#ifdef CONFIG_X86_32
115 if (regs->flags & X86_VM_MASK) {
116 /*
117 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
118 * On nmi (interrupt 2), do_trap should not be called.
119 */
120 if (trapnr < X86_TRAP_UD) {
121 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
122 error_code, trapnr))
123 return 0;
124 }
125 return -1;
126 }
127#endif
128 if (!user_mode(regs)) {
129 if (!fixup_exception(regs)) {
130 tsk->thread.error_code = error_code;
131 tsk->thread.trap_nr = trapnr;
132 die(str, regs, error_code);
133 }
134 return 0;
135 }
136
137 return -1;
138}
139
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200140static siginfo_t *fill_trap_info(struct pt_regs *regs, int signr, int trapnr,
141 siginfo_t *info)
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200142{
143 unsigned long siaddr;
144 int sicode;
145
146 switch (trapnr) {
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200147 default:
148 return SEND_SIG_PRIV;
149
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200150 case X86_TRAP_DE:
151 sicode = FPE_INTDIV;
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200152 siaddr = uprobe_get_trap_addr(regs);
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200153 break;
154 case X86_TRAP_UD:
155 sicode = ILL_ILLOPN;
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200156 siaddr = uprobe_get_trap_addr(regs);
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200157 break;
158 case X86_TRAP_AC:
159 sicode = BUS_ADRALN;
160 siaddr = 0;
161 break;
162 }
163
164 info->si_signo = signr;
165 info->si_errno = 0;
166 info->si_code = sicode;
167 info->si_addr = (void __user *)siaddr;
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200168 return info;
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200169}
170
Masami Hiramatsu93266382014-04-17 17:18:14 +0900171static void
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200172do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100173 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700175 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200178 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
179 return;
Ingo Molnarb5964402008-02-26 11:15:50 +0100180 /*
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530181 * We want error_code and trap_nr set for userspace faults and
Ingo Molnarb5964402008-02-26 11:15:50 +0100182 * kernelspace faults which result in die(), but not
183 * kernelspace faults which are fixed up. die() gives the
184 * process no chance to handle the signal and notice the
185 * kernel fault information, so that won't result in polluting
186 * the information about previously queued, but not yet
187 * delivered, faults. See also do_general_protection below.
188 */
189 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530190 tsk->thread.trap_nr = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100191
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200192#ifdef CONFIG_X86_64
193 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
194 printk_ratelimit()) {
Joe Perchesc767a542012-05-21 19:50:07 -0700195 pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
196 tsk->comm, tsk->pid, str,
197 regs->ip, regs->sp, error_code);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200198 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700199 pr_cont("\n");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200200 }
201#endif
202
Oleg Nesterov38cad572014-05-07 16:47:09 +0200203 force_sig_info(signr, info ?: SEND_SIG_PRIV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900205NOKPROBE_SYMBOL(do_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Oleg Nesterovdff07962014-05-07 17:21:34 +0200207static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200208 unsigned long trapnr, int signr)
Oleg Nesterovdff07962014-05-07 17:21:34 +0200209{
210 enum ctx_state prev_state = exception_enter();
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200211 siginfo_t info;
Oleg Nesterovdff07962014-05-07 17:21:34 +0200212
213 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
214 NOTIFY_STOP) {
215 conditional_sti(regs);
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200216 do_trap(trapnr, signr, str, regs, error_code,
217 fill_trap_info(regs, signr, trapnr, &info));
Oleg Nesterovdff07962014-05-07 17:21:34 +0200218 }
219
220 exception_exit(prev_state);
221}
222
Ingo Molnarb5964402008-02-26 11:15:50 +0100223#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200224dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100225{ \
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200226 do_error_trap(regs, error_code, str, trapnr, signr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200229DO_ERROR(X86_TRAP_DE, SIGFPE, "divide error", divide_error)
230DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
231DO_ERROR(X86_TRAP_BR, SIGSEGV, "bounds", bounds)
232DO_ERROR(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op)
233DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",coprocessor_segment_overrun)
234DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
235DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200236#ifdef CONFIG_X86_32
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200237DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200238#endif
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200239DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200241#ifdef CONFIG_X86_64
242/* Runs on IST stack */
243dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
244{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100245 enum ctx_state prev_state;
246
247 prev_state = exception_enter();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200248 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200249 X86_TRAP_SS, SIGBUS) != NOTIFY_STOP) {
250 preempt_conditional_sti(regs);
251 do_trap(X86_TRAP_SS, SIGBUS, "stack segment", regs, error_code, NULL);
252 preempt_conditional_cli(regs);
253 }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100254 exception_exit(prev_state);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200255}
256
257dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
258{
259 static const char str[] = "double fault";
260 struct task_struct *tsk = current;
261
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100262 exception_enter();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200263 /* Return not checked because double check cannot be ignored */
Kees Cookc9408262012-03-09 16:07:10 -0800264 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200265
266 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530267 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200268
Borislav Petkov4d067d82013-05-09 12:02:29 +0200269#ifdef CONFIG_DOUBLEFAULT
270 df_debug(regs, error_code);
271#endif
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100272 /*
273 * This is always a kernel trap and never fixable (and thus must
274 * never return).
275 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200276 for (;;)
277 die(str, regs, error_code);
278}
279#endif
280
Masami Hiramatsu93266382014-04-17 17:18:14 +0900281dotraplinkage void
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200282do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200284 struct task_struct *tsk;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100285 enum ctx_state prev_state;
Ingo Molnarb5964402008-02-26 11:15:50 +0100286
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100287 prev_state = exception_enter();
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200288 conditional_sti(regs);
289
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200290#ifdef CONFIG_X86_32
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200291 if (regs->flags & X86_VM_MASK) {
292 local_irq_enable();
293 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200294 goto exit;
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200295 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200298 tsk = current;
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200299 if (!user_mode(regs)) {
300 if (fixup_exception(regs))
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200301 goto exit;
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200302
303 tsk->thread.error_code = error_code;
304 tsk->thread.trap_nr = X86_TRAP_GP;
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200305 if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
306 X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200307 die("general protection fault", regs, error_code);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200308 goto exit;
Frederic Weisbeckeref3f6282012-09-24 21:05:52 +0200309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200311 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530312 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100313
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200314 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
315 printk_ratelimit()) {
Joe Perchesc767a542012-05-21 19:50:07 -0700316 pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200317 tsk->comm, task_pid_nr(tsk),
318 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100319 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700320 pr_cont("\n");
Andi Kleen03252912008-01-30 13:33:18 +0100321 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200322
Oleg Nesterov38cad572014-05-07 16:47:09 +0200323 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200324exit:
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100325 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900327NOKPROBE_SYMBOL(do_general_protection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200329/* May run on IST stack. */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900330dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100332 enum ctx_state prev_state;
333
Steven Rostedt08d636b2011-08-16 09:57:10 -0400334#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedta192cd02012-05-30 13:26:37 -0400335 /*
336 * ftrace must be first, everything else may cause a recursive crash.
337 * See note by declaration of modifying_ftrace_code in ftrace.c
338 */
339 if (unlikely(atomic_read(&modifying_ftrace_code)) &&
340 ftrace_int3_handler(regs))
Steven Rostedt08d636b2011-08-16 09:57:10 -0400341 return;
342#endif
Jiri Kosina17f41572013-07-23 10:09:28 +0200343 if (poke_int3_handler(regs))
344 return;
345
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000346 prev_state = exception_enter();
Jason Wesself503b5a2010-05-20 21:04:25 -0500347#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
Kees Cookc9408262012-03-09 16:07:10 -0800348 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
349 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200350 goto exit;
Jason Wesself503b5a2010-05-20 21:04:25 -0500351#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
Srikar Dronamrajucc3a1bf2011-10-25 19:51:59 +0530352
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900353#ifdef CONFIG_KPROBES
354 if (kprobe_int3_handler(regs))
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000355 goto exit;
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900356#endif
357
Kees Cookc9408262012-03-09 16:07:10 -0800358 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
359 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200360 goto exit;
Ingo Molnarb5964402008-02-26 11:15:50 +0100361
Steven Rostedt42181182011-12-16 11:43:02 -0500362 /*
363 * Let others (NMI) know that the debug stack is in use
364 * as we may switch to the interrupt stack.
365 */
366 debug_stack_usage_inc();
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200367 preempt_conditional_sti(regs);
Kees Cookc9408262012-03-09 16:07:10 -0800368 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200369 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500370 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200371exit:
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100372 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900374NOKPROBE_SYMBOL(do_int3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200376#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100377/*
378 * Help handler running on IST stack to switch back to user stack
379 * for scheduling or signal handling. The actual stack switch is done in
380 * entry.S
381 */
Ingo Molnarec000102014-06-06 07:55:06 +0200382asmlinkage __visible struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200383{
384 struct pt_regs *regs = eregs;
385 /* Did already sync */
386 if (eregs == (struct pt_regs *)eregs->sp)
387 ;
388 /* Exception from user space */
389 else if (user_mode(eregs))
390 regs = task_pt_regs(current);
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100391 /*
392 * Exception from kernel and interrupts are enabled. Move to
393 * kernel process stack.
394 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200395 else if (eregs->flags & X86_EFLAGS_IF)
396 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
397 if (eregs != regs)
398 *regs = *eregs;
399 return regs;
400}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900401NOKPROBE_SYMBOL(sync_regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200402#endif
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
405 * Our handling of the processor debug registers is non-trivial.
406 * We do not clear them on entry and exit from the kernel. Therefore
407 * it is possible to get a watchpoint trap here from inside the kernel.
408 * However, the code in ./ptrace.c has ensured that the user can
409 * only set watchpoints on userspace addresses. Therefore the in-kernel
410 * watchpoint trap can only occur in code which is reading/writing
411 * from user space. Such code must not hold kernel locks (since it
412 * can equally take a page fault), therefore it is safe to call
413 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100414 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * Code in ./signal.c ensures that the debug control register
416 * is restored before we deliver any signal, and therefore that
417 * user code runs with the correct debug control register even though
418 * we clear it here.
419 *
420 * Being careful here means that we don't have to be as careful in a
421 * lot of more complicated places (task switching can be a bit lazy
422 * about restoring all the debug state, and ptrace doesn't have to
423 * find every occurrence of the TF bit that could be saved away even
424 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200425 *
426 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900428dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct task_struct *tsk = current;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100431 enum ctx_state prev_state;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200432 int user_icebp = 0;
K.Prasad08d68322009-06-01 23:44:08 +0530433 unsigned long dr6;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530434 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000436 prev_state = exception_enter();
437
K.Prasad08d68322009-06-01 23:44:08 +0530438 get_debugreg(dr6, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
K.Prasad40f92492010-01-28 16:44:01 +0530440 /* Filter out all the reserved bits which are preset to 1 */
441 dr6 &= ~DR6_RESERVED;
442
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200443 /*
444 * If dr6 has no reason to give us about the origin of this trap,
445 * then it's very likely the result of an icebp/int01 trap.
446 * User wants a sigtrap for that.
447 */
448 if (!dr6 && user_mode(regs))
449 user_icebp = 1;
450
Vegard Nossumf8561292008-04-04 00:53:23 +0200451 /* Catch kmemcheck conditions first of all! */
Ingo Molnareadb8a02009-06-17 12:52:15 +0200452 if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200453 goto exit;
Vegard Nossumf8561292008-04-04 00:53:23 +0200454
K.Prasad08d68322009-06-01 23:44:08 +0530455 /* DR6 may or may not be cleared by the CPU */
456 set_debugreg(0, 6);
Roland McGrath10faa812008-01-30 13:30:54 +0100457
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100458 /*
459 * The processor cleared BTF, so don't mark that we need it set.
460 */
461 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
462
K.Prasad08d68322009-06-01 23:44:08 +0530463 /* Store the virtualized DR6 value */
464 tsk->thread.debugreg6 = dr6;
465
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900466#ifdef CONFIG_KPROBES
467 if (kprobe_debug_handler(regs))
468 goto exit;
469#endif
470
Rusty Russell5a802e12013-06-16 14:12:47 +0930471 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
K.Prasad62edab92009-06-01 23:47:06 +0530472 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200473 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200474
Steven Rostedt42181182011-12-16 11:43:02 -0500475 /*
476 * Let others (NMI) know that the debug stack is in use
477 * as we may switch to the interrupt stack.
478 */
479 debug_stack_usage_inc();
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200482 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
K.Prasad08d68322009-06-01 23:44:08 +0530484 if (regs->flags & X86_VM_MASK) {
Kees Cookc9408262012-03-09 16:07:10 -0800485 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
486 X86_TRAP_DB);
Bart Oldeman65542872010-09-23 13:16:58 -0400487 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500488 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200489 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
K.Prasad08d68322009-06-01 23:44:08 +0530493 * Single-stepping through system calls: ignore any exceptions in
494 * kernel space, but re-enable TF when returning to user mode.
495 *
496 * We already checked v86 mode above, so we can check for kernel mode
497 * by just checking the CPL of CS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
K.Prasad08d68322009-06-01 23:44:08 +0530499 if ((dr6 & DR_STEP) && !user_mode(regs)) {
500 tsk->thread.debugreg6 &= ~DR_STEP;
501 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
502 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
K.Prasad08d68322009-06-01 23:44:08 +0530504 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200505 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
K.Prasad08d68322009-06-01 23:44:08 +0530506 send_sigtrap(tsk, regs, error_code, si_code);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200507 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500508 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200510exit:
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100511 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900513NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/*
516 * Note that we play around with the 'TS' bit in an attempt to get
517 * the correct behaviour even in the presence of the asynchronous
518 * IRQ13 behaviour
519 */
Oleg Nesterov5e1b05b2014-05-08 20:34:00 +0200520static void math_error(struct pt_regs *regs, int error_code, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Brian Gerste2e75c92010-03-21 09:00:45 -0400522 struct task_struct *task = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 siginfo_t info;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400524 unsigned short err;
Kees Cookc9408262012-03-09 16:07:10 -0800525 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
526 "simd exception";
Brian Gerste2e75c92010-03-21 09:00:45 -0400527
528 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
529 return;
530 conditional_sti(regs);
531
532 if (!user_mode_vm(regs))
533 {
534 if (!fixup_exception(regs)) {
535 task->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530536 task->thread.trap_nr = trapnr;
Brian Gerste2e75c92010-03-21 09:00:45 -0400537 die(str, regs, error_code);
538 }
539 return;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * Save the info for the exception handler and clear the error.
544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 save_init_fpu(task);
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530546 task->thread.trap_nr = trapnr;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400547 task->thread.error_code = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 info.si_signo = SIGFPE;
549 info.si_errno = 0;
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200550 info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
Kees Cookc9408262012-03-09 16:07:10 -0800551 if (trapnr == X86_TRAP_MF) {
Brian Gerst9b6dba92010-03-21 09:00:44 -0400552 unsigned short cwd, swd;
553 /*
554 * (~cwd & swd) will mask out exceptions that are not set to unmasked
555 * status. 0x3f is the exception bits in these regs, 0x200 is the
556 * C1 reg you need in case of a stack fault, 0x040 is the stack
557 * fault bit. We should only be taking one exception at a time,
558 * so if this combination doesn't produce any single exception,
559 * then we have a bad program that isn't synchronizing its FPU usage
560 * and it will suffer the consequences since we won't be able to
561 * fully reproduce the context of the exception
562 */
563 cwd = get_fpu_cwd(task);
564 swd = get_fpu_swd(task);
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800565
Brian Gerst9b6dba92010-03-21 09:00:44 -0400566 err = swd & ~cwd;
567 } else {
568 /*
569 * The SIMD FPU exceptions are handled a little differently, as there
570 * is only a single status/control register. Thus, to determine which
571 * unmasked exception was caught we must mask the exception mask bits
572 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
573 */
574 unsigned short mxcsr = get_fpu_mxcsr(task);
575 err = ~(mxcsr >> 7) & mxcsr;
576 }
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800577
578 if (err & 0x001) { /* Invalid op */
Ingo Molnarb5964402008-02-26 11:15:50 +0100579 /*
580 * swd & 0x240 == 0x040: Stack Underflow
581 * swd & 0x240 == 0x240: Stack Overflow
582 * User must clear the SF bit (0x40) if set
583 */
584 info.si_code = FPE_FLTINV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800585 } else if (err & 0x004) { /* Divide by Zero */
Ingo Molnarb5964402008-02-26 11:15:50 +0100586 info.si_code = FPE_FLTDIV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800587 } else if (err & 0x008) { /* Overflow */
Ingo Molnarb5964402008-02-26 11:15:50 +0100588 info.si_code = FPE_FLTOVF;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800589 } else if (err & 0x012) { /* Denormal, Underflow */
590 info.si_code = FPE_FLTUND;
591 } else if (err & 0x020) { /* Precision */
Ingo Molnarb5964402008-02-26 11:15:50 +0100592 info.si_code = FPE_FLTRES;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800593 } else {
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100594 /*
Kees Cookc9408262012-03-09 16:07:10 -0800595 * If we're using IRQ 13, or supposedly even some trap
596 * X86_TRAP_MF implementations, it's possible
597 * we get a spurious trap, which is not an error.
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100598 */
Kees Cookc9408262012-03-09 16:07:10 -0800599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 force_sig_info(SIGFPE, &info, task);
602}
603
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200604dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100606 enum ctx_state prev_state;
607
608 prev_state = exception_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800609 math_error(regs, error_code, X86_TRAP_MF);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100610 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200613dotraplinkage void
614do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100616 enum ctx_state prev_state;
617
618 prev_state = exception_enter();
Kees Cookc9408262012-03-09 16:07:10 -0800619 math_error(regs, error_code, X86_TRAP_XF);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100620 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200623dotraplinkage void
624do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200626 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#if 0
628 /* No need to warn about this any longer. */
Joe Perchesc767a542012-05-21 19:50:07 -0700629 pr_info("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#endif
631}
632
Andi Kleen2605fc22014-05-02 00:44:37 +0200633asmlinkage __visible void __attribute__((weak)) smp_thermal_interrupt(void)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200634{
635}
636
Andi Kleen2605fc22014-05-02 00:44:37 +0200637asmlinkage __visible void __attribute__((weak)) smp_threshold_interrupt(void)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200638{
639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641/*
Linus Torvalds34ddc812012-02-18 12:56:35 -0800642 * 'math_state_restore()' saves the current math information in the
643 * old math state array, and gets the new ones from the current task
644 *
645 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
646 * Don't touch unless you *really* know how it works.
647 *
648 * Must be called with kernel preemption disabled (eg with local
649 * local interrupts as in the case of do_device_not_available).
650 */
651void math_state_restore(void)
652{
653 struct task_struct *tsk = current;
654
655 if (!tsk_used_math(tsk)) {
656 local_irq_enable();
657 /*
658 * does a slab alloc which can sleep
659 */
660 if (init_fpu(tsk)) {
661 /*
662 * ran out of memory!
663 */
664 do_group_exit(SIGKILL);
665 return;
666 }
667 local_irq_disable();
668 }
669
670 __thread_fpu_begin(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700671
Linus Torvalds80ab6f12012-02-19 11:48:44 -0800672 /*
673 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
674 */
675 if (unlikely(restore_fpu_checking(tsk))) {
Suresh Siddha304bced2012-08-24 14:13:02 -0700676 drop_init_fpu(tsk);
Oleg Nesterov38cad572014-05-07 16:47:09 +0200677 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
Linus Torvalds80ab6f12012-02-19 11:48:44 -0800678 return;
679 }
Linus Torvaldsb3b08702012-02-16 15:45:23 -0800680
Vineet Guptac375f152013-11-12 15:08:46 -0800681 tsk->thread.fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700683EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Masami Hiramatsu93266382014-04-17 17:18:14 +0900685dotraplinkage void
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500686do_device_not_available(struct pt_regs *regs, long error_code)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200687{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100688 enum ctx_state prev_state;
689
690 prev_state = exception_enter();
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700691 BUG_ON(use_eager_fpu());
Suresh Siddha304bced2012-08-24 14:13:02 -0700692
Brian Gersta334fe42010-09-03 21:17:15 -0400693#ifdef CONFIG_MATH_EMULATION
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200694 if (read_cr0() & X86_CR0_EM) {
Tejun Heod3157602009-02-09 22:17:39 +0900695 struct math_emu_info info = { };
696
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200697 conditional_sti(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900698
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500699 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900700 math_emulate(&info);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100701 exception_exit(prev_state);
Brian Gersta334fe42010-09-03 21:17:15 -0400702 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200703 }
Brian Gersta334fe42010-09-03 21:17:15 -0400704#endif
705 math_state_restore(); /* interrupts still off */
706#ifdef CONFIG_X86_32
707 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200708#endif
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100709 exception_exit(prev_state);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200710}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900711NOKPROBE_SYMBOL(do_device_not_available);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200712
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200713#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200714dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200715{
716 siginfo_t info;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100717 enum ctx_state prev_state;
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200718
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100719 prev_state = exception_enter();
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200720 local_irq_enable();
721
722 info.si_signo = SIGILL;
723 info.si_errno = 0;
724 info.si_code = ILL_BADSTK;
Hannes Ederfc6fcdf2009-02-22 01:00:57 +0100725 info.si_addr = NULL;
Kees Cookc9408262012-03-09 16:07:10 -0800726 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200727 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
728 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
729 &info);
730 }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +0100731 exception_exit(prev_state);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200732}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200733#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200734
Jan Kiszka29c84392010-05-20 21:04:29 -0500735/* Set of traps needed for early debugging. */
736void __init early_trap_init(void)
737{
Kees Cookc9408262012-03-09 16:07:10 -0800738 set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
Jan Kiszka29c84392010-05-20 21:04:29 -0500739 /* int3 can be called from all */
Kees Cookc9408262012-03-09 16:07:10 -0800740 set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800741#ifdef CONFIG_X86_32
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400742 set_intr_gate(X86_TRAP_PF, page_fault);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800743#endif
Jan Kiszka29c84392010-05-20 21:04:29 -0500744 load_idt(&idt_descr);
745}
746
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800747void __init early_trap_pf_init(void)
748{
749#ifdef CONFIG_X86_64
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400750 set_intr_gate(X86_TRAP_PF, page_fault);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800751#endif
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754void __init trap_init(void)
755{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200756 int i;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100759 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100760
761 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100763 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#endif
765
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400766 set_intr_gate(X86_TRAP_DE, divide_error);
Kees Cookc9408262012-03-09 16:07:10 -0800767 set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200768 /* int4 can be called from all */
Kees Cookc9408262012-03-09 16:07:10 -0800769 set_system_intr_gate(X86_TRAP_OF, &overflow);
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400770 set_intr_gate(X86_TRAP_BR, bounds);
771 set_intr_gate(X86_TRAP_UD, invalid_op);
772 set_intr_gate(X86_TRAP_NM, device_not_available);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200773#ifdef CONFIG_X86_32
Kees Cookc9408262012-03-09 16:07:10 -0800774 set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200775#else
Kees Cookc9408262012-03-09 16:07:10 -0800776 set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200777#endif
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400778 set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
779 set_intr_gate(X86_TRAP_TS, invalid_TSS);
780 set_intr_gate(X86_TRAP_NP, segment_not_present);
Kees Cookc9408262012-03-09 16:07:10 -0800781 set_intr_gate_ist(X86_TRAP_SS, &stack_segment, STACKFAULT_STACK);
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400782 set_intr_gate(X86_TRAP_GP, general_protection);
783 set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
784 set_intr_gate(X86_TRAP_MF, coprocessor_error);
785 set_intr_gate(X86_TRAP_AC, alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_X86_MCE
Kees Cookc9408262012-03-09 16:07:10 -0800787 set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#endif
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400789 set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800791 /* Reserve all the builtin and the syscall vector: */
792 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
793 set_bit(i, used_vectors);
794
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200795#ifdef CONFIG_IA32_EMULATION
796 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800797 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200798#endif
799
800#ifdef CONFIG_X86_32
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200801 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200802 set_bit(SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200803#endif
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
Kees Cook4eefbe72013-04-10 12:24:22 -0700806 * Set the IDT descriptor to a fixed read-only location, so that the
807 * "sidt" instruction will not leak the location of the kernel, and
808 * to defend the IDT against arbitrary memory write vulnerabilities.
809 * It will be reloaded in cpu_init() */
810 __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
811 idt_descr.address = fix_to_virt(FIX_RO_IDT);
812
813 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100814 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
816 cpu_init();
817
Thomas Gleixner428cf902009-08-20 10:35:46 +0200818 x86_init.irqs.trap_init();
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500819
820#ifdef CONFIG_X86_64
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400821 memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
Kees Cookc9408262012-03-09 16:07:10 -0800822 set_nmi_gate(X86_TRAP_DB, &debug);
823 set_nmi_gate(X86_TRAP_BP, &int3);
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}