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