extable.c 2.9 KB

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