blob: 8e65d8a9b8db67a424ce1604bd31869c53a0097e [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>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020057#include <asm/fpu/internal.h>
Hidetoshi Seto9e55e442009-06-15 17:22:15 +090058#include <asm/mce.h>
Kees Cook4eefbe72013-04-10 12:24:22 -070059#include <asm/fixmap.h>
Ingo Molnar1164dd02009-01-28 19:34:09 +010060#include <asm/mach_traps.h>
Jiri Kosina17f41572013-07-23 10:09:28 +020061#include <asm/alternative.h>
Dave Hansena84eeaa2015-06-07 11:37:01 -070062#include <asm/fpu/xstate.h>
Dave Hansene7126cf2015-06-07 11:37:03 -070063#include <asm/trace/mpx.h>
Dave Hansenfe3d1972014-11-14 07:18:29 -080064#include <asm/mpx.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020065
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020066#ifdef CONFIG_X86_64
Thomas Gleixner428cf902009-08-20 10:35:46 +020067#include <asm/x86_init.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020068#include <asm/pgalloc.h>
69#include <asm/proto.h>
Kees Cook4df05f32013-07-16 11:34:41 -070070
71/* No need to be aligned, but done to keep all IDTs defined the same way. */
72gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020073#else
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020074#include <asm/processor-flags.h>
Ingo Molnar8e6dafd2009-02-23 00:34:39 +010075#include <asm/setup.h>
Ingo Molnarb2502b42015-06-08 08:42:03 +020076#include <asm/proto.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020077#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Kees Cook4df05f32013-07-16 11:34:41 -070079/* Must be page-aligned because the real IDT is used in a fixmap. */
80gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
81
Yinghai Lub77b8812008-12-19 15:23:44 -080082DECLARE_BITMAP(used_vectors, NR_VECTORS);
83EXPORT_SYMBOL_GPL(used_vectors);
84
Alexander van Heukelum762db432008-09-09 21:55:55 +020085static inline void conditional_sti(struct pt_regs *regs)
86{
87 if (regs->flags & X86_EFLAGS_IF)
88 local_irq_enable();
89}
90
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020091static inline void preempt_conditional_sti(struct pt_regs *regs)
92{
Peter Zijlstrabdb43802013-09-10 12:15:23 +020093 preempt_count_inc();
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020094 if (regs->flags & X86_EFLAGS_IF)
95 local_irq_enable();
96}
97
Thomas Gleixnerbe716612009-01-13 23:36:34 +010098static inline void conditional_cli(struct pt_regs *regs)
99{
100 if (regs->flags & X86_EFLAGS_IF)
101 local_irq_disable();
102}
103
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200104static inline void preempt_conditional_cli(struct pt_regs *regs)
105{
106 if (regs->flags & X86_EFLAGS_IF)
107 local_irq_disable();
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200108 preempt_count_dec();
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200109}
110
Andy Lutomirski8c840142015-07-03 12:44:32 -0700111void ist_enter(struct pt_regs *regs)
Andy Lutomirski95927472014-11-19 17:41:09 -0800112{
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700113 if (user_mode(regs)) {
Andy Lutomirski8c840142015-07-03 12:44:32 -0700114 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Andy Lutomirski95927472014-11-19 17:41:09 -0800115 } else {
116 /*
117 * We might have interrupted pretty much anything. In
118 * fact, if we're a machine check, we can even interrupt
119 * NMI processing. We don't want in_nmi() to return true,
120 * but we need to notify RCU.
121 */
122 rcu_nmi_enter();
Andy Lutomirski95927472014-11-19 17:41:09 -0800123 }
Andy Lutomirskib926e6f2015-01-31 04:53:53 -0800124
125 /*
Andy Lutomirski8c840142015-07-03 12:44:32 -0700126 * We are atomic because we're on the IST stack; or we're on
127 * x86_32, in which case we still shouldn't schedule; or we're
128 * on x86_64 and entered from user mode, in which case we're
129 * still atomic unless ist_begin_non_atomic is called.
Andy Lutomirskib926e6f2015-01-31 04:53:53 -0800130 */
131 preempt_count_add(HARDIRQ_OFFSET);
132
133 /* This code is a bit fragile. Test it. */
134 rcu_lockdep_assert(rcu_is_watching(), "ist_enter didn't work");
Andy Lutomirski95927472014-11-19 17:41:09 -0800135}
136
Andy Lutomirski8c840142015-07-03 12:44:32 -0700137void ist_exit(struct pt_regs *regs)
Andy Lutomirski95927472014-11-19 17:41:09 -0800138{
139 preempt_count_sub(HARDIRQ_OFFSET);
140
Andy Lutomirski8c840142015-07-03 12:44:32 -0700141 if (!user_mode(regs))
Andy Lutomirski95927472014-11-19 17:41:09 -0800142 rcu_nmi_exit();
143}
144
Andy Lutomirskibced35b2014-11-19 17:59:41 -0800145/**
146 * ist_begin_non_atomic() - begin a non-atomic section in an IST exception
147 * @regs: regs passed to the IST exception handler
148 *
149 * IST exception handlers normally cannot schedule. As a special
150 * exception, if the exception interrupted userspace code (i.e.
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700151 * user_mode(regs) would return true) and the exception was not
Andy Lutomirskibced35b2014-11-19 17:59:41 -0800152 * a double fault, it can be safe to schedule. ist_begin_non_atomic()
153 * begins a non-atomic section within an ist_enter()/ist_exit() region.
154 * Callers are responsible for enabling interrupts themselves inside
Andy Lutomirski8c840142015-07-03 12:44:32 -0700155 * the non-atomic section, and callers must call ist_end_non_atomic()
Andy Lutomirskibced35b2014-11-19 17:59:41 -0800156 * before ist_exit().
157 */
158void ist_begin_non_atomic(struct pt_regs *regs)
159{
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700160 BUG_ON(!user_mode(regs));
Andy Lutomirskibced35b2014-11-19 17:59:41 -0800161
162 /*
163 * Sanity check: we need to be on the normal thread stack. This
164 * will catch asm bugs and any attempt to use ist_preempt_enable
165 * from double_fault.
166 */
Andy Lutomirskia7fcf282015-03-06 17:50:19 -0800167 BUG_ON((unsigned long)(current_top_of_stack() -
168 current_stack_pointer()) >= THREAD_SIZE);
Andy Lutomirskibced35b2014-11-19 17:59:41 -0800169
170 preempt_count_sub(HARDIRQ_OFFSET);
171}
172
173/**
174 * ist_end_non_atomic() - begin a non-atomic section in an IST exception
175 *
176 * Ends a non-atomic section started with ist_begin_non_atomic().
177 */
178void ist_end_non_atomic(void)
179{
180 preempt_count_add(HARDIRQ_OFFSET);
181}
182
Masami Hiramatsu93266382014-04-17 17:18:14 +0900183static nokprobe_inline int
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200184do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
185 struct pt_regs *regs, long error_code)
186{
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700187 if (v8086_mode(regs)) {
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200188 /*
189 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
190 * On nmi (interrupt 2), do_trap should not be called.
191 */
192 if (trapnr < X86_TRAP_UD) {
193 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
194 error_code, trapnr))
195 return 0;
196 }
197 return -1;
198 }
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700199
Ingo Molnar55474c42015-03-29 11:02:34 +0200200 if (!user_mode(regs)) {
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200201 if (!fixup_exception(regs)) {
202 tsk->thread.error_code = error_code;
203 tsk->thread.trap_nr = trapnr;
204 die(str, regs, error_code);
205 }
206 return 0;
207 }
208
209 return -1;
210}
211
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200212static siginfo_t *fill_trap_info(struct pt_regs *regs, int signr, int trapnr,
213 siginfo_t *info)
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200214{
215 unsigned long siaddr;
216 int sicode;
217
218 switch (trapnr) {
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200219 default:
220 return SEND_SIG_PRIV;
221
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200222 case X86_TRAP_DE:
223 sicode = FPE_INTDIV;
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200224 siaddr = uprobe_get_trap_addr(regs);
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200225 break;
226 case X86_TRAP_UD:
227 sicode = ILL_ILLOPN;
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200228 siaddr = uprobe_get_trap_addr(regs);
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200229 break;
230 case X86_TRAP_AC:
231 sicode = BUS_ADRALN;
232 siaddr = 0;
233 break;
234 }
235
236 info->si_signo = signr;
237 info->si_errno = 0;
238 info->si_code = sicode;
239 info->si_addr = (void __user *)siaddr;
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200240 return info;
Oleg Nesterov958d3d72014-05-07 17:59:39 +0200241}
242
Masami Hiramatsu93266382014-04-17 17:18:14 +0900243static void
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200244do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100245 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700247 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Frederic Weisbeckerc416ddf2012-09-25 14:51:19 +0200250 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
251 return;
Ingo Molnarb5964402008-02-26 11:15:50 +0100252 /*
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530253 * We want error_code and trap_nr set for userspace faults and
Ingo Molnarb5964402008-02-26 11:15:50 +0100254 * kernelspace faults which result in die(), but not
255 * kernelspace faults which are fixed up. die() gives the
256 * process no chance to handle the signal and notice the
257 * kernel fault information, so that won't result in polluting
258 * the information about previously queued, but not yet
259 * delivered, faults. See also do_general_protection below.
260 */
261 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530262 tsk->thread.trap_nr = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100263
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200264#ifdef CONFIG_X86_64
265 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
266 printk_ratelimit()) {
Joe Perchesc767a542012-05-21 19:50:07 -0700267 pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
268 tsk->comm, tsk->pid, str,
269 regs->ip, regs->sp, error_code);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200270 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700271 pr_cont("\n");
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200272 }
273#endif
274
Oleg Nesterov38cad572014-05-07 16:47:09 +0200275 force_sig_info(signr, info ?: SEND_SIG_PRIV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900277NOKPROBE_SYMBOL(do_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Oleg Nesterovdff07962014-05-07 17:21:34 +0200279static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200280 unsigned long trapnr, int signr)
Oleg Nesterovdff07962014-05-07 17:21:34 +0200281{
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200282 siginfo_t info;
Oleg Nesterovdff07962014-05-07 17:21:34 +0200283
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700284 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
285
Oleg Nesterovdff07962014-05-07 17:21:34 +0200286 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
287 NOTIFY_STOP) {
288 conditional_sti(regs);
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200289 do_trap(trapnr, signr, str, regs, error_code,
290 fill_trap_info(regs, signr, trapnr, &info));
Oleg Nesterovdff07962014-05-07 17:21:34 +0200291 }
Oleg Nesterovdff07962014-05-07 17:21:34 +0200292}
293
Ingo Molnarb5964402008-02-26 11:15:50 +0100294#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200295dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100296{ \
Oleg Nesterov1c326c42014-05-08 20:04:11 +0200297 do_error_trap(regs, error_code, str, trapnr, signr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200300DO_ERROR(X86_TRAP_DE, SIGFPE, "divide error", divide_error)
301DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200302DO_ERROR(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op)
303DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",coprocessor_segment_overrun)
304DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
305DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200306DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
Oleg Nesterov0eb14832014-05-08 20:12:24 +0200307DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200309#ifdef CONFIG_X86_64
310/* Runs on IST stack */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200311dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
312{
313 static const char str[] = "double fault";
314 struct task_struct *tsk = current;
315
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800316#ifdef CONFIG_X86_ESPFIX64
317 extern unsigned char native_irq_return_iret[];
318
319 /*
320 * If IRET takes a non-IST fault on the espfix64 stack, then we
321 * end up promoting it to a doublefault. In that case, modify
322 * the stack to make it look like we just entered the #GP
323 * handler from user space, similar to bad_iret.
Andy Lutomirski95927472014-11-19 17:41:09 -0800324 *
325 * No need for ist_enter here because we don't use RCU.
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800326 */
327 if (((long)regs->sp >> PGDIR_SHIFT) == ESPFIX_PGD_ENTRY &&
328 regs->cs == __KERNEL_CS &&
329 regs->ip == (unsigned long)native_irq_return_iret)
330 {
331 struct pt_regs *normal_regs = task_pt_regs(current);
332
333 /* Fake a #GP(0) from userspace. */
334 memmove(&normal_regs->ip, (void *)regs->sp, 5*8);
335 normal_regs->orig_ax = 0; /* Missing (lost) #GP error code */
336 regs->ip = (unsigned long)general_protection;
337 regs->sp = (unsigned long)&normal_regs->orig_ax;
Andy Lutomirski95927472014-11-19 17:41:09 -0800338
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800339 return;
340 }
341#endif
342
Andy Lutomirski8c840142015-07-03 12:44:32 -0700343 ist_enter(regs);
Kees Cookc9408262012-03-09 16:07:10 -0800344 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200345
346 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530347 tsk->thread.trap_nr = X86_TRAP_DF;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200348
Borislav Petkov4d067d82013-05-09 12:02:29 +0200349#ifdef CONFIG_DOUBLEFAULT
350 df_debug(regs, error_code);
351#endif
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100352 /*
353 * This is always a kernel trap and never fixable (and thus must
354 * never return).
355 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200356 for (;;)
357 die(str, regs, error_code);
358}
359#endif
360
Dave Hansenfe3d1972014-11-14 07:18:29 -0800361dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
362{
Dave Hansena84eeaa2015-06-07 11:37:01 -0700363 const struct bndcsr *bndcsr;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800364 siginfo_t *info;
365
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700366 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800367 if (notify_die(DIE_TRAP, "bounds", regs, error_code,
368 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
Andy Lutomirski8c840142015-07-03 12:44:32 -0700369 return;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800370 conditional_sti(regs);
371
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700372 if (!user_mode(regs))
Dave Hansenfe3d1972014-11-14 07:18:29 -0800373 die("bounds", regs, error_code);
374
375 if (!cpu_feature_enabled(X86_FEATURE_MPX)) {
376 /* The exception is not from Intel MPX */
377 goto exit_trap;
378 }
379
380 /*
381 * We need to look at BNDSTATUS to resolve this exception.
Dave Hansena84eeaa2015-06-07 11:37:01 -0700382 * A NULL here might mean that it is in its 'init state',
383 * which is all zeros which indicates MPX was not
384 * responsible for the exception.
Dave Hansenfe3d1972014-11-14 07:18:29 -0800385 */
Dave Hansena84eeaa2015-06-07 11:37:01 -0700386 bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800387 if (!bndcsr)
388 goto exit_trap;
389
Dave Hansene7126cf2015-06-07 11:37:03 -0700390 trace_bounds_exception_mpx(bndcsr);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800391 /*
392 * The error code field of the BNDSTATUS register communicates status
393 * information of a bound range exception #BR or operation involving
394 * bound directory.
395 */
396 switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) {
397 case 2: /* Bound directory has invalid entry. */
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700398 if (mpx_handle_bd_fault())
Dave Hansenfe3d1972014-11-14 07:18:29 -0800399 goto exit_trap;
400 break; /* Success, it was handled */
401 case 1: /* Bound violation. */
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700402 info = mpx_generate_siginfo(regs);
Dan Carpentere10abb22014-11-25 20:21:14 +0300403 if (IS_ERR(info)) {
Dave Hansenfe3d1972014-11-14 07:18:29 -0800404 /*
405 * We failed to decode the MPX instruction. Act as if
406 * the exception was not caused by MPX.
407 */
408 goto exit_trap;
409 }
410 /*
411 * Success, we decoded the instruction and retrieved
412 * an 'info' containing the address being accessed
413 * which caused the exception. This information
414 * allows and application to possibly handle the
415 * #BR exception itself.
416 */
417 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, info);
418 kfree(info);
419 break;
420 case 0: /* No exception caused by Intel MPX operations. */
421 goto exit_trap;
422 default:
423 die("bounds", regs, error_code);
424 }
425
Dave Hansenfe3d1972014-11-14 07:18:29 -0800426 return;
Andy Lutomirski8c840142015-07-03 12:44:32 -0700427
Dave Hansenfe3d1972014-11-14 07:18:29 -0800428exit_trap:
429 /*
430 * This path out is for all the cases where we could not
431 * handle the exception in some way (like allocating a
432 * table or telling userspace about it. We will also end
433 * up here if the kernel has MPX turned off at compile
434 * time..
435 */
436 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800437}
438
Masami Hiramatsu93266382014-04-17 17:18:14 +0900439dotraplinkage void
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200440do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200442 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100443
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700444 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200445 conditional_sti(regs);
446
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700447 if (v8086_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200448 local_irq_enable();
449 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
Andy Lutomirski8c840142015-07-03 12:44:32 -0700450 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200453 tsk = current;
Ingo Molnar55474c42015-03-29 11:02:34 +0200454 if (!user_mode(regs)) {
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200455 if (fixup_exception(regs))
Andy Lutomirski8c840142015-07-03 12:44:32 -0700456 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200457
458 tsk->thread.error_code = error_code;
459 tsk->thread.trap_nr = X86_TRAP_GP;
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200460 if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
461 X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200462 die("general protection fault", regs, error_code);
Andy Lutomirski8c840142015-07-03 12:44:32 -0700463 return;
Frederic Weisbeckeref3f62882012-09-24 21:05:52 +0200464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200466 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530467 tsk->thread.trap_nr = X86_TRAP_GP;
Ingo Molnarb5964402008-02-26 11:15:50 +0100468
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200469 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
470 printk_ratelimit()) {
Joe Perchesc767a542012-05-21 19:50:07 -0700471 pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200472 tsk->comm, task_pid_nr(tsk),
473 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100474 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700475 pr_cont("\n");
Andi Kleen03252912008-01-30 13:33:18 +0100476 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200477
Oleg Nesterov38cad572014-05-07 16:47:09 +0200478 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900480NOKPROBE_SYMBOL(do_general_protection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200482/* May run on IST stack. */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900483dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Steven Rostedt08d636b2011-08-16 09:57:10 -0400485#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedta192cd02012-05-30 13:26:37 -0400486 /*
487 * ftrace must be first, everything else may cause a recursive crash.
488 * See note by declaration of modifying_ftrace_code in ftrace.c
489 */
490 if (unlikely(atomic_read(&modifying_ftrace_code)) &&
491 ftrace_int3_handler(regs))
Steven Rostedt08d636b2011-08-16 09:57:10 -0400492 return;
493#endif
Jiri Kosina17f41572013-07-23 10:09:28 +0200494 if (poke_int3_handler(regs))
495 return;
496
Andy Lutomirski8c840142015-07-03 12:44:32 -0700497 ist_enter(regs);
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700498 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Jason Wesself503b5a2010-05-20 21:04:25 -0500499#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
Kees Cookc9408262012-03-09 16:07:10 -0800500 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
501 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200502 goto exit;
Jason Wesself503b5a2010-05-20 21:04:25 -0500503#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
Srikar Dronamrajucc3a1bf2011-10-25 19:51:59 +0530504
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900505#ifdef CONFIG_KPROBES
506 if (kprobe_int3_handler(regs))
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000507 goto exit;
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900508#endif
509
Kees Cookc9408262012-03-09 16:07:10 -0800510 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
511 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200512 goto exit;
Ingo Molnarb5964402008-02-26 11:15:50 +0100513
Steven Rostedt42181182011-12-16 11:43:02 -0500514 /*
515 * Let others (NMI) know that the debug stack is in use
516 * as we may switch to the interrupt stack.
517 */
518 debug_stack_usage_inc();
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200519 preempt_conditional_sti(regs);
Kees Cookc9408262012-03-09 16:07:10 -0800520 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200521 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500522 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200523exit:
Andy Lutomirski8c840142015-07-03 12:44:32 -0700524 ist_exit(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900526NOKPROBE_SYMBOL(do_int3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200528#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100529/*
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800530 * Help handler running on IST stack to switch off the IST stack if the
531 * interrupted code was in user mode. The actual stack switch is done in
532 * entry_64.S
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100533 */
Andy Lutomirski7ddc6a22014-11-24 17:39:06 -0800534asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200535{
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800536 struct pt_regs *regs = task_pt_regs(current);
537 *regs = *eregs;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200538 return regs;
539}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900540NOKPROBE_SYMBOL(sync_regs);
Andy Lutomirskib645af22014-11-22 18:00:33 -0800541
542struct bad_iret_stack {
543 void *error_entry_ret;
544 struct pt_regs regs;
545};
546
Andy Lutomirski7ddc6a22014-11-24 17:39:06 -0800547asmlinkage __visible notrace
Andy Lutomirskib645af22014-11-22 18:00:33 -0800548struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
549{
550 /*
551 * This is called from entry_64.S early in handling a fault
552 * caused by a bad iret to user mode. To handle the fault
553 * correctly, we want move our stack frame to task_pt_regs
554 * and we want to pretend that the exception came from the
555 * iret target.
556 */
557 struct bad_iret_stack *new_stack =
558 container_of(task_pt_regs(current),
559 struct bad_iret_stack, regs);
560
561 /* Copy the IRET target to the new stack. */
562 memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
563
564 /* Copy the remainder of the stack from the current stack. */
565 memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
566
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700567 BUG_ON(!user_mode(&new_stack->regs));
Andy Lutomirskib645af22014-11-22 18:00:33 -0800568 return new_stack;
569}
Andy Lutomirski7ddc6a22014-11-24 17:39:06 -0800570NOKPROBE_SYMBOL(fixup_bad_iret);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200571#endif
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * Our handling of the processor debug registers is non-trivial.
575 * We do not clear them on entry and exit from the kernel. Therefore
576 * it is possible to get a watchpoint trap here from inside the kernel.
577 * However, the code in ./ptrace.c has ensured that the user can
578 * only set watchpoints on userspace addresses. Therefore the in-kernel
579 * watchpoint trap can only occur in code which is reading/writing
580 * from user space. Such code must not hold kernel locks (since it
581 * can equally take a page fault), therefore it is safe to call
582 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Code in ./signal.c ensures that the debug control register
585 * is restored before we deliver any signal, and therefore that
586 * user code runs with the correct debug control register even though
587 * we clear it here.
588 *
589 * Being careful here means that we don't have to be as careful in a
590 * lot of more complicated places (task switching can be a bit lazy
591 * about restoring all the debug state, and ptrace doesn't have to
592 * find every occurrence of the TF bit that could be saved away even
593 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200594 *
595 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900597dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct task_struct *tsk = current;
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200600 int user_icebp = 0;
K.Prasad08d68322009-06-01 23:44:08 +0530601 unsigned long dr6;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530602 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Andy Lutomirski8c840142015-07-03 12:44:32 -0700604 ist_enter(regs);
Masami Hiramatsu4cdf77a2014-06-14 06:47:12 +0000605
K.Prasad08d68322009-06-01 23:44:08 +0530606 get_debugreg(dr6, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
K.Prasad40f92492010-01-28 16:44:01 +0530608 /* Filter out all the reserved bits which are preset to 1 */
609 dr6 &= ~DR6_RESERVED;
610
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200611 /*
612 * If dr6 has no reason to give us about the origin of this trap,
613 * then it's very likely the result of an icebp/int01 trap.
614 * User wants a sigtrap for that.
615 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700616 if (!dr6 && user_mode(regs))
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200617 user_icebp = 1;
618
Vegard Nossumf8561292008-04-04 00:53:23 +0200619 /* Catch kmemcheck conditions first of all! */
Ingo Molnareadb8a02009-06-17 12:52:15 +0200620 if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200621 goto exit;
Vegard Nossumf8561292008-04-04 00:53:23 +0200622
K.Prasad08d68322009-06-01 23:44:08 +0530623 /* DR6 may or may not be cleared by the CPU */
624 set_debugreg(0, 6);
Roland McGrath10faa812008-01-30 13:30:54 +0100625
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100626 /*
627 * The processor cleared BTF, so don't mark that we need it set.
628 */
629 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
630
K.Prasad08d68322009-06-01 23:44:08 +0530631 /* Store the virtualized DR6 value */
632 tsk->thread.debugreg6 = dr6;
633
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +0900634#ifdef CONFIG_KPROBES
635 if (kprobe_debug_handler(regs))
636 goto exit;
637#endif
638
Rusty Russell5a802e12013-06-16 14:12:47 +0930639 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
K.Prasad62edab92009-06-01 23:47:06 +0530640 SIGTRAP) == NOTIFY_STOP)
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200641 goto exit;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200642
Steven Rostedt42181182011-12-16 11:43:02 -0500643 /*
644 * Let others (NMI) know that the debug stack is in use
645 * as we may switch to the interrupt stack.
646 */
647 debug_stack_usage_inc();
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200650 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Andy Lutomirskid74ef112015-03-18 18:33:35 -0700652 if (v8086_mode(regs)) {
Kees Cookc9408262012-03-09 16:07:10 -0800653 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
654 X86_TRAP_DB);
Bart Oldeman65542872010-09-23 13:16:58 -0400655 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500656 debug_stack_usage_dec();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200657 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
K.Prasad08d68322009-06-01 23:44:08 +0530661 * Single-stepping through system calls: ignore any exceptions in
662 * kernel space, but re-enable TF when returning to user mode.
663 *
664 * We already checked v86 mode above, so we can check for kernel mode
665 * by just checking the CPL of CS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Ingo Molnar55474c42015-03-29 11:02:34 +0200667 if ((dr6 & DR_STEP) && !user_mode(regs)) {
K.Prasad08d68322009-06-01 23:44:08 +0530668 tsk->thread.debugreg6 &= ~DR_STEP;
669 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
670 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
K.Prasad08d68322009-06-01 23:44:08 +0530672 si_code = get_si_code(tsk->thread.debugreg6);
Frederic Weisbeckera1e80fa2010-06-30 15:09:06 +0200673 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
K.Prasad08d68322009-06-01 23:44:08 +0530674 send_sigtrap(tsk, regs, error_code, si_code);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200675 preempt_conditional_cli(regs);
Steven Rostedt42181182011-12-16 11:43:02 -0500676 debug_stack_usage_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200678exit:
Andy Lutomirski8c840142015-07-03 12:44:32 -0700679 ist_exit(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900681NOKPROBE_SYMBOL(do_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683/*
684 * Note that we play around with the 'TS' bit in an attempt to get
685 * the correct behaviour even in the presence of the asynchronous
686 * IRQ13 behaviour
687 */
Oleg Nesterov5e1b05b2014-05-08 20:34:00 +0200688static void math_error(struct pt_regs *regs, int error_code, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Brian Gerste2e75c912010-03-21 09:00:45 -0400690 struct task_struct *task = current;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200691 struct fpu *fpu = &task->thread.fpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 siginfo_t info;
Kees Cookc9408262012-03-09 16:07:10 -0800693 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
694 "simd exception";
Brian Gerste2e75c912010-03-21 09:00:45 -0400695
696 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
697 return;
698 conditional_sti(regs);
699
Ingo Molnare1cebad2015-04-30 09:29:38 +0200700 if (!user_mode(regs)) {
Brian Gerste2e75c912010-03-21 09:00:45 -0400701 if (!fixup_exception(regs)) {
702 task->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530703 task->thread.trap_nr = trapnr;
Brian Gerste2e75c912010-03-21 09:00:45 -0400704 die(str, regs, error_code);
705 }
706 return;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * Save the info for the exception handler and clear the error.
711 */
Ingo Molnare1cebad2015-04-30 09:29:38 +0200712 fpu__save(fpu);
713
714 task->thread.trap_nr = trapnr;
Brian Gerst9b6dba92010-03-21 09:00:44 -0400715 task->thread.error_code = error_code;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200716 info.si_signo = SIGFPE;
717 info.si_errno = 0;
718 info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
H. Peter Anvinadf77bac2008-12-22 17:56:05 -0800719
Ingo Molnare1cebad2015-04-30 09:29:38 +0200720 info.si_code = fpu__exception_code(fpu, trapnr);
H. Peter Anvinadf77bac2008-12-22 17:56:05 -0800721
Ingo Molnare1cebad2015-04-30 09:29:38 +0200722 /* Retry when we get spurious exceptions: */
723 if (!info.si_code)
Kees Cookc9408262012-03-09 16:07:10 -0800724 return;
Ingo Molnare1cebad2015-04-30 09:29:38 +0200725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 force_sig_info(SIGFPE, &info, task);
727}
728
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200729dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700731 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Kees Cookc9408262012-03-09 16:07:10 -0800732 math_error(regs, error_code, X86_TRAP_MF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200735dotraplinkage void
736do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700738 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Kees Cookc9408262012-03-09 16:07:10 -0800739 math_error(regs, error_code, X86_TRAP_XF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200742dotraplinkage void
743do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200745 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Masami Hiramatsu93266382014-04-17 17:18:14 +0900748dotraplinkage void
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500749do_device_not_available(struct pt_regs *regs, long error_code)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200750{
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700751 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700752 BUG_ON(use_eager_fpu());
Suresh Siddha304bced2012-08-24 14:13:02 -0700753
Brian Gersta334fe42010-09-03 21:17:15 -0400754#ifdef CONFIG_MATH_EMULATION
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200755 if (read_cr0() & X86_CR0_EM) {
Tejun Heod3157602009-02-09 22:17:39 +0900756 struct math_emu_info info = { };
757
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200758 conditional_sti(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900759
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500760 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900761 math_emulate(&info);
Brian Gersta334fe42010-09-03 21:17:15 -0400762 return;
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200763 }
Brian Gersta334fe42010-09-03 21:17:15 -0400764#endif
Ingo Molnare1884d62015-05-04 11:49:58 +0200765 fpu__restore(&current->thread.fpu); /* interrupts still off */
Brian Gersta334fe42010-09-03 21:17:15 -0400766#ifdef CONFIG_X86_32
767 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200768#endif
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200769}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900770NOKPROBE_SYMBOL(do_device_not_available);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200771
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200772#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200773dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200774{
775 siginfo_t info;
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200776
Andy Lutomirski02fdcd52015-07-03 12:44:24 -0700777 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200778 local_irq_enable();
779
780 info.si_signo = SIGILL;
781 info.si_errno = 0;
782 info.si_code = ILL_BADSTK;
Hannes Ederfc6fcdf2009-02-22 01:00:57 +0100783 info.si_addr = NULL;
Kees Cookc9408262012-03-09 16:07:10 -0800784 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200785 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
786 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
787 &info);
788 }
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200789}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200790#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200791
Jan Kiszka29c84392010-05-20 21:04:29 -0500792/* Set of traps needed for early debugging. */
793void __init early_trap_init(void)
794{
Wang Nanb4d83272015-02-26 13:49:39 +0800795 /*
Wang Nan5eca7452015-02-27 12:19:49 +0800796 * Don't use IST to set DEBUG_STACK as it doesn't work until TSS
797 * is ready in cpu_init() <-- trap_init(). Before trap_init(),
798 * CPU runs at ring 0 so it is impossible to hit an invalid
799 * stack. Using the original stack works well enough at this
800 * early stage. DEBUG_STACK will be equipped after cpu_init() in
Wang Nanb4d83272015-02-26 13:49:39 +0800801 * trap_init().
Wang Nan5eca7452015-02-27 12:19:49 +0800802 *
803 * We don't need to set trace_idt_table like set_intr_gate(),
804 * since we don't have trace_debug and it will be reset to
805 * 'debug' in trap_init() by set_intr_gate_ist().
Wang Nanb4d83272015-02-26 13:49:39 +0800806 */
Wang Nan5eca7452015-02-27 12:19:49 +0800807 set_intr_gate_notrace(X86_TRAP_DB, debug);
Jan Kiszka29c84392010-05-20 21:04:29 -0500808 /* int3 can be called from all */
Wang Nan5eca7452015-02-27 12:19:49 +0800809 set_system_intr_gate(X86_TRAP_BP, &int3);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800810#ifdef CONFIG_X86_32
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400811 set_intr_gate(X86_TRAP_PF, page_fault);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800812#endif
Jan Kiszka29c84392010-05-20 21:04:29 -0500813 load_idt(&idt_descr);
814}
815
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800816void __init early_trap_pf_init(void)
817{
818#ifdef CONFIG_X86_64
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400819 set_intr_gate(X86_TRAP_PF, page_fault);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800820#endif
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823void __init trap_init(void)
824{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200825 int i;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100828 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100829
830 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100832 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
834
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400835 set_intr_gate(X86_TRAP_DE, divide_error);
Kees Cookc9408262012-03-09 16:07:10 -0800836 set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200837 /* int4 can be called from all */
Kees Cookc9408262012-03-09 16:07:10 -0800838 set_system_intr_gate(X86_TRAP_OF, &overflow);
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400839 set_intr_gate(X86_TRAP_BR, bounds);
840 set_intr_gate(X86_TRAP_UD, invalid_op);
841 set_intr_gate(X86_TRAP_NM, device_not_available);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200842#ifdef CONFIG_X86_32
Kees Cookc9408262012-03-09 16:07:10 -0800843 set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200844#else
Kees Cookc9408262012-03-09 16:07:10 -0800845 set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200846#endif
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400847 set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
848 set_intr_gate(X86_TRAP_TS, invalid_TSS);
849 set_intr_gate(X86_TRAP_NP, segment_not_present);
Andy Lutomirski6f442be2014-11-22 18:00:32 -0800850 set_intr_gate(X86_TRAP_SS, stack_segment);
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400851 set_intr_gate(X86_TRAP_GP, general_protection);
852 set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
853 set_intr_gate(X86_TRAP_MF, coprocessor_error);
854 set_intr_gate(X86_TRAP_AC, alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#ifdef CONFIG_X86_MCE
Kees Cookc9408262012-03-09 16:07:10 -0800856 set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#endif
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400858 set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800860 /* Reserve all the builtin and the syscall vector: */
861 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
862 set_bit(i, used_vectors);
863
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200864#ifdef CONFIG_IA32_EMULATION
Ingo Molnar2cd23552015-06-08 08:28:07 +0200865 set_system_intr_gate(IA32_SYSCALL_VECTOR, entry_INT80_compat);
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800866 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200867#endif
868
869#ifdef CONFIG_X86_32
Ingo Molnarb2502b42015-06-08 08:42:03 +0200870 set_system_trap_gate(IA32_SYSCALL_VECTOR, entry_INT80_32);
Brian Gerst51bb9282015-05-09 11:36:52 -0400871 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200872#endif
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
Kees Cook4eefbe72013-04-10 12:24:22 -0700875 * Set the IDT descriptor to a fixed read-only location, so that the
876 * "sidt" instruction will not leak the location of the kernel, and
877 * to defend the IDT against arbitrary memory write vulnerabilities.
878 * It will be reloaded in cpu_init() */
879 __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
880 idt_descr.address = fix_to_virt(FIX_RO_IDT);
881
882 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100883 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
885 cpu_init();
886
Wang Nanb4d83272015-02-26 13:49:39 +0800887 /*
888 * X86_TRAP_DB and X86_TRAP_BP have been set
Wang Nan5eca7452015-02-27 12:19:49 +0800889 * in early_trap_init(). However, ITS works only after
Wang Nanb4d83272015-02-26 13:49:39 +0800890 * cpu_init() loads TSS. See comments in early_trap_init().
891 */
892 set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
893 /* int3 can be called from all */
894 set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
895
Thomas Gleixner428cf902009-08-20 10:35:46 +0200896 x86_init.irqs.trap_init();
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500897
898#ifdef CONFIG_X86_64
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400899 memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
Kees Cookc9408262012-03-09 16:07:10 -0800900 set_nmi_gate(X86_TRAP_DB, &debug);
901 set_nmi_gate(X86_TRAP_BP, &int3);
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}