extable.c 3.0 KB

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