1 #include <linux/module.h>
2 #include <asm/uaccess.h>
4 #include <asm/kdebug.h>
6 typedef bool (*ex_handler_t
)(const struct exception_table_entry
*,
7 struct pt_regs
*, int);
9 static inline unsigned long
10 ex_fixup_addr(const struct exception_table_entry
*x
)
12 return (unsigned long)&x
->fixup
+ x
->fixup
;
14 static inline ex_handler_t
15 ex_fixup_handler(const struct exception_table_entry
*x
)
17 return (ex_handler_t
)((unsigned long)&x
->handler
+ x
->handler
);
20 bool ex_handler_default(const struct exception_table_entry
*fixup
,
21 struct pt_regs
*regs
, int trapnr
)
23 regs
->ip
= ex_fixup_addr(fixup
);
26 EXPORT_SYMBOL(ex_handler_default
);
28 bool ex_handler_fault(const struct exception_table_entry
*fixup
,
29 struct pt_regs
*regs
, int trapnr
)
31 regs
->ip
= ex_fixup_addr(fixup
);
35 EXPORT_SYMBOL_GPL(ex_handler_fault
);
37 bool ex_handler_ext(const struct exception_table_entry
*fixup
,
38 struct pt_regs
*regs
, int trapnr
)
40 /* Special hack for uaccess_err */
41 current_thread_info()->uaccess_err
= 1;
42 regs
->ip
= ex_fixup_addr(fixup
);
45 EXPORT_SYMBOL(ex_handler_ext
);
47 bool ex_handler_rdmsr_unsafe(const struct exception_table_entry
*fixup
,
48 struct pt_regs
*regs
, int trapnr
)
50 if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pF)\n",
51 (unsigned int)regs
->cx
, regs
->ip
, (void *)regs
->ip
))
52 show_stack_regs(regs
);
54 /* Pretend that the read succeeded and returned 0. */
55 regs
->ip
= ex_fixup_addr(fixup
);
60 EXPORT_SYMBOL(ex_handler_rdmsr_unsafe
);
62 bool ex_handler_wrmsr_unsafe(const struct exception_table_entry
*fixup
,
63 struct pt_regs
*regs
, int trapnr
)
65 if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pF)\n",
66 (unsigned int)regs
->cx
, (unsigned int)regs
->dx
,
67 (unsigned int)regs
->ax
, regs
->ip
, (void *)regs
->ip
))
68 show_stack_regs(regs
);
70 /* Pretend that the write succeeded. */
71 regs
->ip
= ex_fixup_addr(fixup
);
74 EXPORT_SYMBOL(ex_handler_wrmsr_unsafe
);
76 bool ex_handler_clear_fs(const struct exception_table_entry
*fixup
,
77 struct pt_regs
*regs
, int trapnr
)
79 if (static_cpu_has(X86_BUG_NULL_SEG
))
80 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS
));
81 asm volatile ("mov %0, %%fs" : : "rm" (0));
82 return ex_handler_default(fixup
, regs
, trapnr
);
84 EXPORT_SYMBOL(ex_handler_clear_fs
);
86 bool ex_has_fault_handler(unsigned long ip
)
88 const struct exception_table_entry
*e
;
91 e
= search_exception_tables(ip
);
94 handler
= ex_fixup_handler(e
);
96 return handler
== ex_handler_fault
;
99 int fixup_exception(struct pt_regs
*regs
, int trapnr
)
101 const struct exception_table_entry
*e
;
102 ex_handler_t handler
;
104 #ifdef CONFIG_PNPBIOS
105 if (unlikely(SEGMENT_IS_PNP_CODE(regs
->cs
))) {
106 extern u32 pnp_bios_fault_eip
, pnp_bios_fault_esp
;
107 extern u32 pnp_bios_is_utter_crap
;
108 pnp_bios_is_utter_crap
= 1;
109 printk(KERN_CRIT
"PNPBIOS fault.. attempting recovery.\n");
113 : : "g" (pnp_bios_fault_esp
), "g" (pnp_bios_fault_eip
));
114 panic("do_trap: can't hit this");
118 e
= search_exception_tables(regs
->ip
);
122 handler
= ex_fixup_handler(e
);
123 return handler(e
, regs
, trapnr
);
126 extern unsigned int early_recursion_flag
;
128 /* Restricted version used during very early boot */
129 void __init
early_fixup_exception(struct pt_regs
*regs
, int trapnr
)
131 /* Ignore early NMIs. */
132 if (trapnr
== X86_TRAP_NMI
)
135 if (early_recursion_flag
> 2)
138 if (regs
->cs
!= __KERNEL_CS
)
142 * The full exception fixup machinery is available as soon as
143 * the early IDT is loaded. This means that it is the
144 * responsibility of extable users to either function correctly
145 * when handlers are invoked early or to simply avoid causing
146 * exceptions before they're ready to handle them.
148 * This is better than filtering which handlers can be used,
149 * because refusing to call a handler here is guaranteed to
150 * result in a hard-to-debug panic.
152 * Keep in mind that not all vectors actually get here. Early
153 * fage faults, for example, are special.
155 if (fixup_exception(regs
, trapnr
))
159 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
160 (unsigned)trapnr
, (unsigned long)regs
->cs
, regs
->ip
,
161 regs
->orig_ax
, read_cr2());