extable.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const struct exception_table_entry *
  44. search_extable(const struct exception_table_entry *first,
  45. const struct exception_table_entry *last,
  46. unsigned long value)
  47. {
  48. while (first <= last) {
  49. const struct exception_table_entry *mid;
  50. unsigned long mid_value;
  51. mid = (last - first) / 2 + first;
  52. mid_value = ex_to_addr(mid);
  53. if (mid_value == value)
  54. return mid;
  55. else if (mid_value < value)
  56. first = mid+1;
  57. else
  58. last = mid-1;
  59. }
  60. return NULL;
  61. }