blob: 061a237583547614911877a7bc1642f04db6b05f [file] [log] [blame]
Harvey Harrison6d485832008-01-30 13:31:41 +01001#include <linux/module.h>
Harvey Harrison6d485832008-01-30 13:31:41 +01002#include <asm/uaccess.h>
Andy Lutomirski0d0efc02016-04-02 07:01:33 -07003#include <asm/traps.h>
Harvey Harrison6d485832008-01-30 13:31:41 +01004
Tony Luck548acf12016-02-17 10:20:12 -08005typedef bool (*ex_handler_t)(const struct exception_table_entry *,
6 struct pt_regs *, int);
7
H. Peter Anvin70627652012-04-20 17:12:48 -07008static inline unsigned long
H. Peter Anvin70627652012-04-20 17:12:48 -07009ex_fixup_addr(const struct exception_table_entry *x)
10{
11 return (unsigned long)&x->fixup + x->fixup;
12}
Tony Luck548acf12016-02-17 10:20:12 -080013static inline ex_handler_t
14ex_fixup_handler(const struct exception_table_entry *x)
Harvey Harrison6d485832008-01-30 13:31:41 +010015{
Tony Luck548acf12016-02-17 10:20:12 -080016 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
17}
18
19bool ex_handler_default(const struct exception_table_entry *fixup,
20 struct pt_regs *regs, int trapnr)
21{
22 regs->ip = ex_fixup_addr(fixup);
23 return true;
24}
25EXPORT_SYMBOL(ex_handler_default);
26
27bool ex_handler_fault(const struct exception_table_entry *fixup,
28 struct pt_regs *regs, int trapnr)
29{
30 regs->ip = ex_fixup_addr(fixup);
31 regs->ax = trapnr;
32 return true;
33}
34EXPORT_SYMBOL_GPL(ex_handler_fault);
35
36bool ex_handler_ext(const struct exception_table_entry *fixup,
37 struct pt_regs *regs, int trapnr)
38{
39 /* Special hack for uaccess_err */
40 current_thread_info()->uaccess_err = 1;
41 regs->ip = ex_fixup_addr(fixup);
42 return true;
43}
44EXPORT_SYMBOL(ex_handler_ext);
45
46bool ex_has_fault_handler(unsigned long ip)
47{
48 const struct exception_table_entry *e;
49 ex_handler_t handler;
50
51 e = search_exception_tables(ip);
52 if (!e)
53 return false;
54 handler = ex_fixup_handler(e);
55
56 return handler == ex_handler_fault;
57}
58
59int fixup_exception(struct pt_regs *regs, int trapnr)
60{
61 const struct exception_table_entry *e;
62 ex_handler_t handler;
Harvey Harrison6d485832008-01-30 13:31:41 +010063
64#ifdef CONFIG_PNPBIOS
65 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
66 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
67 extern u32 pnp_bios_is_utter_crap;
68 pnp_bios_is_utter_crap = 1;
69 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
70 __asm__ volatile(
71 "movl %0, %%esp\n\t"
72 "jmp *%1\n\t"
73 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
74 panic("do_trap: can't hit this");
75 }
76#endif
77
Tony Luck548acf12016-02-17 10:20:12 -080078 e = search_exception_tables(regs->ip);
79 if (!e)
80 return 0;
H. Peter Anvin70627652012-04-20 17:12:48 -070081
Tony Luck548acf12016-02-17 10:20:12 -080082 handler = ex_fixup_handler(e);
83 return handler(e, regs, trapnr);
Harvey Harrison6d485832008-01-30 13:31:41 +010084}
H. Peter Anvin6a1ea272012-04-19 15:24:20 -070085
Andy Lutomirski0e861fb2016-04-02 07:01:34 -070086extern unsigned int early_recursion_flag;
87
H. Peter Anvin6a1ea272012-04-19 15:24:20 -070088/* Restricted version used during very early boot */
Andy Lutomirski0e861fb2016-04-02 07:01:34 -070089void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
H. Peter Anvin6a1ea272012-04-19 15:24:20 -070090{
Andy Lutomirski0d0efc02016-04-02 07:01:33 -070091 /* Ignore early NMIs. */
92 if (trapnr == X86_TRAP_NMI)
Andy Lutomirski0e861fb2016-04-02 07:01:34 -070093 return;
94
95 if (early_recursion_flag > 2)
96 goto halt_loop;
97
98 if (regs->cs != __KERNEL_CS)
99 goto fail;
Andy Lutomirski0d0efc02016-04-02 07:01:33 -0700100
Andy Lutomirskiae7ef452016-04-02 07:01:35 -0700101 if (fixup_exception(regs, trapnr))
102 return;
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700103
104fail:
105 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
106 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
107 regs->orig_ax, read_cr2());
108
109 show_regs(regs);
110
111halt_loop:
112 while (true)
113 halt();
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700114}