1 #include <linux/module.h>
2 #include <linux/spinlock.h>
3 #include <linux/sort.h>
4 #include <asm/uaccess.h>
6 typedef bool (*ex_handler_t)(const struct exception_table_entry *,
7 struct pt_regs *, int);
9 static inline unsigned long
10 ex_insn_addr(const struct exception_table_entry *x)
12 return (unsigned long)&x->insn + x->insn;
14 static inline unsigned long
15 ex_fixup_addr(const struct exception_table_entry *x)
17 return (unsigned long)&x->fixup + x->fixup;
19 static inline ex_handler_t
20 ex_fixup_handler(const struct exception_table_entry *x)
22 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
25 bool ex_handler_default(const struct exception_table_entry *fixup,
26 struct pt_regs *regs, int trapnr)
28 regs->ip = ex_fixup_addr(fixup);
31 EXPORT_SYMBOL(ex_handler_default);
33 bool ex_handler_fault(const struct exception_table_entry *fixup,
34 struct pt_regs *regs, int trapnr)
36 regs->ip = ex_fixup_addr(fixup);
40 EXPORT_SYMBOL_GPL(ex_handler_fault);
42 bool ex_handler_ext(const struct exception_table_entry *fixup,
43 struct pt_regs *regs, int trapnr)
45 /* Special hack for uaccess_err */
46 current_thread_info()->uaccess_err = 1;
47 regs->ip = ex_fixup_addr(fixup);
50 EXPORT_SYMBOL(ex_handler_ext);
52 bool ex_has_fault_handler(unsigned long ip)
54 const struct exception_table_entry *e;
57 e = search_exception_tables(ip);
60 handler = ex_fixup_handler(e);
62 return handler == ex_handler_fault;
65 int fixup_exception(struct pt_regs *regs, int trapnr)
67 const struct exception_table_entry *e;
71 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
72 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
73 extern u32 pnp_bios_is_utter_crap;
74 pnp_bios_is_utter_crap = 1;
75 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
79 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
80 panic("do_trap: can't hit this");
84 e = search_exception_tables(regs->ip);
88 handler = ex_fixup_handler(e);
89 return handler(e, regs, trapnr);
92 /* Restricted version used during very early boot */
93 int __init early_fixup_exception(unsigned long *ip)
95 const struct exception_table_entry *e;
99 e = search_exception_tables(*ip);
103 new_ip = ex_fixup_addr(e);
104 handler = ex_fixup_handler(e);
106 /* special handling not supported during early boot */
107 if (handler != ex_handler_default)
115 * Search one exception table for an entry corresponding to the
116 * given instruction address, and return the address of the entry,
117 * or NULL if none is found.
118 * We use a binary search, and thus we assume that the table is
121 const struct exception_table_entry *
122 search_extable(const struct exception_table_entry *first,
123 const struct exception_table_entry *last,
126 while (first <= last) {
127 const struct exception_table_entry *mid;
130 mid = ((last - first) >> 1) + first;
131 addr = ex_insn_addr(mid);
134 else if (addr > value)
143 * The exception table needs to be sorted so that the binary
144 * search that we use to find entries in it works properly.
145 * This is used both for the kernel exception table and for
146 * the exception tables of modules that get loaded.
149 static int cmp_ex(const void *a, const void *b)
151 const struct exception_table_entry *x = a, *y = b;
154 * This value will always end up fittin in an int, because on
155 * both i386 and x86-64 the kernel symbol-reachable address
158 * This compare is only valid after normalization.
160 return x->insn - y->insn;
163 void sort_extable(struct exception_table_entry *start,
164 struct exception_table_entry *finish)
166 struct exception_table_entry *p;
169 /* Convert all entries to being relative to the start of the section */
171 for (p = start; p < finish; p++) {
180 sort(start, finish - start, sizeof(struct exception_table_entry),
183 /* Denormalize all entries */
185 for (p = start; p < finish; p++) {
195 #ifdef CONFIG_MODULES
197 * If the exception table is sorted, any referring to the module init
198 * will be at the beginning or the end.
200 void trim_init_extable(struct module *m)
202 /*trim the beginning*/
203 while (m->num_exentries &&
204 within_module_init(ex_insn_addr(&m->extable[0]), m)) {
209 while (m->num_exentries &&
210 within_module_init(ex_insn_addr(&m->extable[m->num_exentries-1]), m))
213 #endif /* CONFIG_MODULES */