1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Derived from arch/ppc/mm/extable.c and arch/i386/mm/extable.c.
4 *
5 * Copyright (C) 2004 Paul Mackerras, IBM Corp.
6 */
7
8 #include <linux/bsearch.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/sort.h>
12 #include <linux/uaccess.h>
13 #include <linux/extable.h>
14
15 #ifndef ARCH_HAS_RELATIVE_EXTABLE
16 #define ex_to_insn(x) ((x)->insn)
17 #else
ex_to_insn(const struct exception_table_entry * x)18 static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
19 {
20 return (unsigned long)&x->insn + x->insn;
21 }
22 #endif
23
24 #ifndef ARCH_HAS_RELATIVE_EXTABLE
25 #define swap_ex NULL
26 #else
swap_ex(void * a,void * b,int size)27 static void swap_ex(void *a, void *b, int size)
28 {
29 struct exception_table_entry *x = a, *y = b, tmp;
30 int delta = b - a;
31
32 tmp = *x;
33 x->insn = y->insn + delta;
34 y->insn = tmp.insn - delta;
35
36 #ifdef swap_ex_entry_fixup
37 swap_ex_entry_fixup(x, y, tmp, delta);
38 #else
39 x->fixup = y->fixup + delta;
40 y->fixup = tmp.fixup - delta;
41 #endif
42 }
43 #endif /* ARCH_HAS_RELATIVE_EXTABLE */
44
45 /*
46 * The exception table needs to be sorted so that the binary
47 * search that we use to find entries in it works properly.
48 * This is used both for the kernel exception table and for
49 * the exception tables of modules that get loaded.
50 */
cmp_ex_sort(const void * a,const void * b)51 static int cmp_ex_sort(const void *a, const void *b)
52 {
53 const struct exception_table_entry *x = a, *y = b;
54
55 /* avoid overflow */
56 if (ex_to_insn(x) > ex_to_insn(y))
57 return 1;
58 if (ex_to_insn(x) < ex_to_insn(y))
59 return -1;
60 return 0;
61 }
62
sort_extable(struct exception_table_entry * start,struct exception_table_entry * finish)63 void sort_extable(struct exception_table_entry *start,
64 struct exception_table_entry *finish)
65 {
66 sort(start, finish - start, sizeof(struct exception_table_entry),
67 cmp_ex_sort, swap_ex);
68 }
69
70 #ifdef CONFIG_MODULES
71 /*
72 * If the exception table is sorted, any referring to the module init
73 * will be at the beginning or the end.
74 */
trim_init_extable(struct module * m)75 void trim_init_extable(struct module *m)
76 {
77 /*trim the beginning*/
78 while (m->num_exentries &&
79 within_module_init(ex_to_insn(&m->extable[0]), m)) {
80 m->extable++;
81 m->num_exentries--;
82 }
83 /*trim the end*/
84 while (m->num_exentries &&
85 within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]),
86 m))
87 m->num_exentries--;
88 }
89 #endif /* CONFIG_MODULES */
90
cmp_ex_search(const void * key,const void * elt)91 static int cmp_ex_search(const void *key, const void *elt)
92 {
93 const struct exception_table_entry *_elt = elt;
94 unsigned long _key = *(unsigned long *)key;
95
96 /* avoid overflow */
97 if (_key > ex_to_insn(_elt))
98 return 1;
99 if (_key < ex_to_insn(_elt))
100 return -1;
101 return 0;
102 }
103
104 /*
105 * Search one exception table for an entry corresponding to the
106 * given instruction address, and return the address of the entry,
107 * or NULL if none is found.
108 * We use a binary search, and thus we assume that the table is
109 * already sorted.
110 */
111 const struct exception_table_entry *
search_extable(const struct exception_table_entry * base,const size_t num,unsigned long value)112 search_extable(const struct exception_table_entry *base,
113 const size_t num,
114 unsigned long value)
115 {
116 return bsearch(&value, base, num,
117 sizeof(struct exception_table_entry), cmp_ex_search);
118 }
119