extable.c 3.1 KB

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