x86/msr: Set the return value to zero when native_rdmsr_safe() fails
[linux-2.6-block.git] / arch / x86 / mm / extable.c
CommitLineData
6d48583b 1#include <linux/module.h>
6d48583b 2#include <asm/uaccess.h>
0d0efc07 3#include <asm/traps.h>
6d48583b 4
548acf19
TL
5typedef bool (*ex_handler_t)(const struct exception_table_entry *,
6 struct pt_regs *, int);
7
70627654
PA
8static inline unsigned long
9ex_fixup_addr(const struct exception_table_entry *x)
10{
11 return (unsigned long)&x->fixup + x->fixup;
12}
548acf19
TL
13static inline ex_handler_t
14ex_fixup_handler(const struct exception_table_entry *x)
15{
16 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
17}
6d48583b 18
548acf19
TL
19bool ex_handler_default(const struct exception_table_entry *fixup,
20 struct pt_regs *regs, int trapnr)
6d48583b 21{
548acf19
TL
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
fbd70437
AL
46bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
47 struct pt_regs *regs, int trapnr)
48{
49 WARN_ONCE(1, "unchecked MSR access error: RDMSR from 0x%x\n",
50 (unsigned int)regs->cx);
51
52 /* Pretend that the read succeeded and returned 0. */
53 regs->ip = ex_fixup_addr(fixup);
54 regs->ax = 0;
55 regs->dx = 0;
56 return true;
57}
58EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
59
60bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
61 struct pt_regs *regs, int trapnr)
62{
63 WARN_ONCE(1, "unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x)\n",
64 (unsigned int)regs->cx,
65 (unsigned int)regs->dx, (unsigned int)regs->ax);
66
67 /* Pretend that the write succeeded. */
68 regs->ip = ex_fixup_addr(fixup);
69 return true;
70}
71EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
72
548acf19
TL
73bool ex_has_fault_handler(unsigned long ip)
74{
75 const struct exception_table_entry *e;
76 ex_handler_t handler;
77
78 e = search_exception_tables(ip);
79 if (!e)
80 return false;
81 handler = ex_fixup_handler(e);
82
83 return handler == ex_handler_fault;
84}
85
86int fixup_exception(struct pt_regs *regs, int trapnr)
87{
88 const struct exception_table_entry *e;
89 ex_handler_t handler;
6d48583b
HH
90
91#ifdef CONFIG_PNPBIOS
92 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
93 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
94 extern u32 pnp_bios_is_utter_crap;
95 pnp_bios_is_utter_crap = 1;
96 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
97 __asm__ volatile(
98 "movl %0, %%esp\n\t"
99 "jmp *%1\n\t"
100 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
101 panic("do_trap: can't hit this");
102 }
103#endif
104
548acf19
TL
105 e = search_exception_tables(regs->ip);
106 if (!e)
107 return 0;
6d48583b 108
548acf19
TL
109 handler = ex_fixup_handler(e);
110 return handler(e, regs, trapnr);
6d48583b 111}
6a1ea279 112
0e861fbb
AL
113extern unsigned int early_recursion_flag;
114
6a1ea279 115/* Restricted version used during very early boot */
0e861fbb 116void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
6a1ea279 117{
0d0efc07
AL
118 /* Ignore early NMIs. */
119 if (trapnr == X86_TRAP_NMI)
0e861fbb
AL
120 return;
121
122 if (early_recursion_flag > 2)
123 goto halt_loop;
124
125 if (regs->cs != __KERNEL_CS)
126 goto fail;
0d0efc07 127
ae7ef45e
AL
128 if (fixup_exception(regs, trapnr))
129 return;
0e861fbb
AL
130
131fail:
132 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
133 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
134 regs->orig_ax, read_cr2());
135
136 show_regs(regs);
137
138halt_loop:
139 while (true)
140 halt();
6a1ea279 141}