Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 1 | #include <linux/module.h> |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 2 | #include <asm/uaccess.h> |
| 3 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 4 | typedef bool (*ex_handler_t)(const struct exception_table_entry *, |
| 5 | struct pt_regs *, int); |
| 6 | |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 7 | static inline unsigned long |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 8 | ex_fixup_addr(const struct exception_table_entry *x) |
| 9 | { |
| 10 | return (unsigned long)&x->fixup + x->fixup; |
| 11 | } |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 12 | static inline ex_handler_t |
| 13 | ex_fixup_handler(const struct exception_table_entry *x) |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 14 | { |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 15 | return (ex_handler_t)((unsigned long)&x->handler + x->handler); |
| 16 | } |
| 17 | |
| 18 | bool ex_handler_default(const struct exception_table_entry *fixup, |
| 19 | struct pt_regs *regs, int trapnr) |
| 20 | { |
| 21 | regs->ip = ex_fixup_addr(fixup); |
| 22 | return true; |
| 23 | } |
| 24 | EXPORT_SYMBOL(ex_handler_default); |
| 25 | |
| 26 | bool ex_handler_fault(const struct exception_table_entry *fixup, |
| 27 | struct pt_regs *regs, int trapnr) |
| 28 | { |
| 29 | regs->ip = ex_fixup_addr(fixup); |
| 30 | regs->ax = trapnr; |
| 31 | return true; |
| 32 | } |
| 33 | EXPORT_SYMBOL_GPL(ex_handler_fault); |
| 34 | |
| 35 | bool ex_handler_ext(const struct exception_table_entry *fixup, |
| 36 | struct pt_regs *regs, int trapnr) |
| 37 | { |
| 38 | /* Special hack for uaccess_err */ |
| 39 | current_thread_info()->uaccess_err = 1; |
| 40 | regs->ip = ex_fixup_addr(fixup); |
| 41 | return true; |
| 42 | } |
| 43 | EXPORT_SYMBOL(ex_handler_ext); |
| 44 | |
| 45 | bool ex_has_fault_handler(unsigned long ip) |
| 46 | { |
| 47 | const struct exception_table_entry *e; |
| 48 | ex_handler_t handler; |
| 49 | |
| 50 | e = search_exception_tables(ip); |
| 51 | if (!e) |
| 52 | return false; |
| 53 | handler = ex_fixup_handler(e); |
| 54 | |
| 55 | return handler == ex_handler_fault; |
| 56 | } |
| 57 | |
| 58 | int fixup_exception(struct pt_regs *regs, int trapnr) |
| 59 | { |
| 60 | const struct exception_table_entry *e; |
| 61 | ex_handler_t handler; |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 62 | |
| 63 | #ifdef CONFIG_PNPBIOS |
| 64 | if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) { |
| 65 | extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp; |
| 66 | extern u32 pnp_bios_is_utter_crap; |
| 67 | pnp_bios_is_utter_crap = 1; |
| 68 | printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n"); |
| 69 | __asm__ volatile( |
| 70 | "movl %0, %%esp\n\t" |
| 71 | "jmp *%1\n\t" |
| 72 | : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip)); |
| 73 | panic("do_trap: can't hit this"); |
| 74 | } |
| 75 | #endif |
| 76 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 77 | e = search_exception_tables(regs->ip); |
| 78 | if (!e) |
| 79 | return 0; |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 80 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 81 | handler = ex_fixup_handler(e); |
| 82 | return handler(e, regs, trapnr); |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 83 | } |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 84 | |
| 85 | /* Restricted version used during very early boot */ |
Andy Lutomirski | 7bbcdb1 | 2016-04-02 07:01:32 -0700 | [diff] [blame^] | 86 | int __init early_fixup_exception(struct pt_regs *regs, int trapnr) |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 87 | { |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 88 | const struct exception_table_entry *e; |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 89 | unsigned long new_ip; |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 90 | ex_handler_t handler; |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 91 | |
Andy Lutomirski | 7bbcdb1 | 2016-04-02 07:01:32 -0700 | [diff] [blame^] | 92 | e = search_exception_tables(regs->ip); |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 93 | if (!e) |
| 94 | return 0; |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 95 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 96 | new_ip = ex_fixup_addr(e); |
| 97 | handler = ex_fixup_handler(e); |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 98 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 99 | /* special handling not supported during early boot */ |
| 100 | if (handler != ex_handler_default) |
| 101 | return 0; |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 102 | |
Andy Lutomirski | 7bbcdb1 | 2016-04-02 07:01:32 -0700 | [diff] [blame^] | 103 | regs->ip = new_ip; |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 104 | return 1; |
H. Peter Anvin | 6a1ea27 | 2012-04-19 15:24:20 -0700 | [diff] [blame] | 105 | } |