1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sort.h>
15#include <asm/uaccess.h>
16
17#ifndef ARCH_HAS_RELATIVE_EXTABLE
18#define ex_to_insn(x) ((x)->insn)
19#else
20static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
21{
22 return (unsigned long)&x->insn + x->insn;
23}
24#endif
25
26#ifndef ARCH_HAS_SORT_EXTABLE
27#ifndef ARCH_HAS_RELATIVE_EXTABLE
28#define swap_ex NULL
29#else
30static void swap_ex(void *a, void *b, int size)
31{
32 struct exception_table_entry *x = a, *y = b, tmp;
33 int delta = b - a;
34
35 tmp = *x;
36 x->insn = y->insn + delta;
37 y->insn = tmp.insn - delta;
38
39#ifdef swap_ex_entry_fixup
40 swap_ex_entry_fixup(x, y, tmp, delta);
41#else
42 x->fixup = y->fixup + delta;
43 y->fixup = tmp.fixup - delta;
44#endif
45}
46#endif
47
48
49
50
51
52
53
54static int cmp_ex(const void *a, const void *b)
55{
56 const struct exception_table_entry *x = a, *y = b;
57
58
59 if (ex_to_insn(x) > ex_to_insn(y))
60 return 1;
61 if (ex_to_insn(x) < ex_to_insn(y))
62 return -1;
63 return 0;
64}
65
66void sort_extable(struct exception_table_entry *start,
67 struct exception_table_entry *finish)
68{
69 sort(start, finish - start, sizeof(struct exception_table_entry),
70 cmp_ex, swap_ex);
71}
72
73#ifdef CONFIG_MODULES
74
75
76
77
78void trim_init_extable(struct module *m)
79{
80
81 while (m->num_exentries &&
82 within_module_init(ex_to_insn(&m->extable[0]), m)) {
83 m->extable++;
84 m->num_exentries--;
85 }
86
87 while (m->num_exentries &&
88 within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]),
89 m))
90 m->num_exentries--;
91}
92#endif
93#endif
94
95#ifndef ARCH_HAS_SEARCH_EXTABLE
96
97
98
99
100
101
102
103const struct exception_table_entry *
104search_extable(const struct exception_table_entry *first,
105 const struct exception_table_entry *last,
106 unsigned long value)
107{
108 while (first <= last) {
109 const struct exception_table_entry *mid;
110
111 mid = ((last - first) >> 1) + first;
112
113
114
115
116 if (ex_to_insn(mid) < value)
117 first = mid + 1;
118 else if (ex_to_insn(mid) > value)
119 last = mid - 1;
120 else
121 return mid;
122 }
123 return NULL;
124}
125#endif
126