xref: /openbmc/linux/lib/extable.c (revision b4edf06c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Derived from arch/ppc/mm/extable.c and arch/i386/mm/extable.c.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2004 Paul Mackerras, IBM Corp.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8a94c33ddSThomas Meyer #include <linux/bsearch.h>
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/sort.h>
127c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
138e72a7a4SValdis Kletnieks #include <linux/extable.h>
141da177e4SLinus Torvalds 
15a272858aSArd Biesheuvel #ifndef ARCH_HAS_RELATIVE_EXTABLE
16a272858aSArd Biesheuvel #define ex_to_insn(x)	((x)->insn)
17a272858aSArd Biesheuvel #else
ex_to_insn(const struct exception_table_entry * x)18a272858aSArd Biesheuvel static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
19a272858aSArd Biesheuvel {
20a272858aSArd Biesheuvel 	return (unsigned long)&x->insn + x->insn;
21a272858aSArd Biesheuvel }
22a272858aSArd Biesheuvel #endif
23a272858aSArd Biesheuvel 
24a272858aSArd Biesheuvel #ifndef ARCH_HAS_RELATIVE_EXTABLE
25a272858aSArd Biesheuvel #define swap_ex		NULL
26a272858aSArd Biesheuvel #else
swap_ex(void * a,void * b,int size)27a272858aSArd Biesheuvel static void swap_ex(void *a, void *b, int size)
28a272858aSArd Biesheuvel {
29a272858aSArd Biesheuvel 	struct exception_table_entry *x = a, *y = b, tmp;
30a272858aSArd Biesheuvel 	int delta = b - a;
31a272858aSArd Biesheuvel 
32a272858aSArd Biesheuvel 	tmp = *x;
33a272858aSArd Biesheuvel 	x->insn = y->insn + delta;
34a272858aSArd Biesheuvel 	y->insn = tmp.insn - delta;
35a272858aSArd Biesheuvel 
36a272858aSArd Biesheuvel #ifdef swap_ex_entry_fixup
37a272858aSArd Biesheuvel 	swap_ex_entry_fixup(x, y, tmp, delta);
38a272858aSArd Biesheuvel #else
39a272858aSArd Biesheuvel 	x->fixup = y->fixup + delta;
40a272858aSArd Biesheuvel 	y->fixup = tmp.fixup - delta;
41a272858aSArd Biesheuvel #endif
42a272858aSArd Biesheuvel }
43a272858aSArd Biesheuvel #endif /* ARCH_HAS_RELATIVE_EXTABLE */
44a272858aSArd Biesheuvel 
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * The exception table needs to be sorted so that the binary
471da177e4SLinus Torvalds  * search that we use to find entries in it works properly.
481da177e4SLinus Torvalds  * This is used both for the kernel exception table and for
491da177e4SLinus Torvalds  * the exception tables of modules that get loaded.
501da177e4SLinus Torvalds  */
cmp_ex_sort(const void * a,const void * b)51a94c33ddSThomas Meyer static int cmp_ex_sort(const void *a, const void *b)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	const struct exception_table_entry *x = a, *y = b;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	/* avoid overflow */
56a272858aSArd Biesheuvel 	if (ex_to_insn(x) > ex_to_insn(y))
571da177e4SLinus Torvalds 		return 1;
58a272858aSArd Biesheuvel 	if (ex_to_insn(x) < ex_to_insn(y))
591da177e4SLinus Torvalds 		return -1;
601da177e4SLinus Torvalds 	return 0;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
sort_extable(struct exception_table_entry * start,struct exception_table_entry * finish)631da177e4SLinus Torvalds void sort_extable(struct exception_table_entry *start,
641da177e4SLinus Torvalds 		  struct exception_table_entry *finish)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	sort(start, finish - start, sizeof(struct exception_table_entry),
67a94c33ddSThomas Meyer 	     cmp_ex_sort, swap_ex);
681da177e4SLinus Torvalds }
69ad6561dfSRusty Russell 
70ad6561dfSRusty Russell #ifdef CONFIG_MODULES
71ad6561dfSRusty Russell /*
72ad6561dfSRusty Russell  * If the exception table is sorted, any referring to the module init
73ad6561dfSRusty Russell  * will be at the beginning or the end.
74ad6561dfSRusty Russell  */
trim_init_extable(struct module * m)75ad6561dfSRusty Russell void trim_init_extable(struct module *m)
76ad6561dfSRusty Russell {
77ad6561dfSRusty Russell 	/*trim the beginning*/
78a272858aSArd Biesheuvel 	while (m->num_exentries &&
79a272858aSArd Biesheuvel 	       within_module_init(ex_to_insn(&m->extable[0]), m)) {
80ad6561dfSRusty Russell 		m->extable++;
81ad6561dfSRusty Russell 		m->num_exentries--;
82ad6561dfSRusty Russell 	}
83ad6561dfSRusty Russell 	/*trim the end*/
84ad6561dfSRusty Russell 	while (m->num_exentries &&
85a272858aSArd Biesheuvel 	       within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]),
86a272858aSArd Biesheuvel 				  m))
87ad6561dfSRusty Russell 		m->num_exentries--;
88ad6561dfSRusty Russell }
89ad6561dfSRusty Russell #endif /* CONFIG_MODULES */
90a94c33ddSThomas Meyer 
cmp_ex_search(const void * key,const void * elt)91a94c33ddSThomas Meyer static int cmp_ex_search(const void *key, const void *elt)
92a94c33ddSThomas Meyer {
93a94c33ddSThomas Meyer 	const struct exception_table_entry *_elt = elt;
94a94c33ddSThomas Meyer 	unsigned long _key = *(unsigned long *)key;
95a94c33ddSThomas Meyer 
96a94c33ddSThomas Meyer 	/* avoid overflow */
97a94c33ddSThomas Meyer 	if (_key > ex_to_insn(_elt))
98a94c33ddSThomas Meyer 		return 1;
99a94c33ddSThomas Meyer 	if (_key < ex_to_insn(_elt))
100a94c33ddSThomas Meyer 		return -1;
101a94c33ddSThomas Meyer 	return 0;
102a94c33ddSThomas Meyer }
103a94c33ddSThomas Meyer 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Search one exception table for an entry corresponding to the
1061da177e4SLinus Torvalds  * given instruction address, and return the address of the entry,
1071da177e4SLinus Torvalds  * or NULL if none is found.
1081da177e4SLinus Torvalds  * We use a binary search, and thus we assume that the table is
1091da177e4SLinus Torvalds  * already sorted.
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds const struct exception_table_entry *
search_extable(const struct exception_table_entry * base,const size_t num,unsigned long value)112a94c33ddSThomas Meyer search_extable(const struct exception_table_entry *base,
113a94c33ddSThomas Meyer 	       const size_t num,
1141da177e4SLinus Torvalds 	       unsigned long value)
1151da177e4SLinus Torvalds {
116a94c33ddSThomas Meyer 	return bsearch(&value, base, num,
117a94c33ddSThomas Meyer 		       sizeof(struct exception_table_entry), cmp_ex_search);
1181da177e4SLinus Torvalds }
119