extable.c 3.2 KB

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