extable.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * linux/arch/alpha/mm/extable.c
  3. */
  4. #include <linux/module.h>
  5. #include <linux/sort.h>
  6. #include <asm/uaccess.h>
  7. static inline unsigned long ex_to_addr(const struct exception_table_entry *x)
  8. {
  9. return (unsigned long)&x->insn + x->insn;
  10. }
  11. static void swap_ex(void *a, void *b, int size)
  12. {
  13. struct exception_table_entry *ex_a = a, *ex_b = b;
  14. unsigned long addr_a = ex_to_addr(ex_a), addr_b = ex_to_addr(ex_b);
  15. unsigned int t = ex_a->fixup.unit;
  16. ex_a->fixup.unit = ex_b->fixup.unit;
  17. ex_b->fixup.unit = t;
  18. ex_a->insn = (int)(addr_b - (unsigned long)&ex_a->insn);
  19. ex_b->insn = (int)(addr_a - (unsigned long)&ex_b->insn);
  20. }
  21. /*
  22. * The exception table needs to be sorted so that the binary
  23. * search that we use to find entries in it works properly.
  24. * This is used both for the kernel exception table and for
  25. * the exception tables of modules that get loaded.
  26. */
  27. static int cmp_ex(const void *a, const void *b)
  28. {
  29. const struct exception_table_entry *x = a, *y = b;
  30. /* avoid overflow */
  31. if (ex_to_addr(x) > ex_to_addr(y))
  32. return 1;
  33. if (ex_to_addr(x) < ex_to_addr(y))
  34. return -1;
  35. return 0;
  36. }
  37. void sort_extable(struct exception_table_entry *start,
  38. struct exception_table_entry *finish)
  39. {
  40. sort(start, finish - start, sizeof(struct exception_table_entry),
  41. cmp_ex, swap_ex);
  42. }
  43. #ifdef CONFIG_MODULES
  44. /*
  45. * Any entry referring to the module init will be at the beginning or
  46. * the end.
  47. */
  48. void trim_init_extable(struct module *m)
  49. {
  50. /*trim the beginning*/
  51. while (m->num_exentries &&
  52. within_module_init(ex_to_addr(&m->extable[0]), m)) {
  53. m->extable++;
  54. m->num_exentries--;
  55. }
  56. /*trim the end*/
  57. while (m->num_exentries &&
  58. within_module_init(ex_to_addr(&m->extable[m->num_exentries-1]),
  59. m))
  60. m->num_exentries--;
  61. }
  62. #endif /* CONFIG_MODULES */
  63. const struct exception_table_entry *
  64. search_extable(const struct exception_table_entry *first,
  65. const struct exception_table_entry *last,
  66. unsigned long value)
  67. {
  68. while (first <= last) {
  69. const struct exception_table_entry *mid;
  70. unsigned long mid_value;
  71. mid = (last - first) / 2 + first;
  72. mid_value = ex_to_addr(mid);
  73. if (mid_value == value)
  74. return mid;
  75. else if (mid_value < value)
  76. first = mid+1;
  77. else
  78. last = mid-1;
  79. }
  80. return NULL;
  81. }