extable.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Kernel exception handling table support. Derived from arch/alpha/mm/extable.c.
  3. *
  4. * Copyright (C) 1998, 1999, 2001-2002, 2004 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. */
  7. #include <linux/config.h>
  8. #include <linux/sort.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/module.h>
  11. static int cmp_ex(const void *a, const void *b)
  12. {
  13. const struct exception_table_entry *l = a, *r = b;
  14. u64 lip = (u64) &l->addr + l->addr;
  15. u64 rip = (u64) &r->addr + r->addr;
  16. /* avoid overflow */
  17. if (lip > rip)
  18. return 1;
  19. if (lip < rip)
  20. return -1;
  21. return 0;
  22. }
  23. static void swap_ex(void *a, void *b, int size)
  24. {
  25. struct exception_table_entry *l = a, *r = b, tmp;
  26. u64 delta = (u64) r - (u64) l;
  27. tmp = *l;
  28. l->addr = r->addr + delta;
  29. l->cont = r->cont + delta;
  30. r->addr = tmp.addr - delta;
  31. r->cont = tmp.cont - delta;
  32. }
  33. /*
  34. * Sort the exception table. It's usually already sorted, but there
  35. * may be unordered entries due to multiple text sections (such as the
  36. * .init text section). Note that the exception-table-entries contain
  37. * location-relative addresses, which requires a bit of care during
  38. * sorting to avoid overflows in the offset members (e.g., it would
  39. * not be safe to make a temporary copy of an exception-table entry on
  40. * the stack, because the stack may be more than 2GB away from the
  41. * exception-table).
  42. */
  43. void sort_extable (struct exception_table_entry *start,
  44. struct exception_table_entry *finish)
  45. {
  46. sort(start, finish - start, sizeof(struct exception_table_entry),
  47. cmp_ex, swap_ex);
  48. }
  49. const struct exception_table_entry *
  50. search_extable (const struct exception_table_entry *first,
  51. const struct exception_table_entry *last,
  52. unsigned long ip)
  53. {
  54. const struct exception_table_entry *mid;
  55. unsigned long mid_ip;
  56. long diff;
  57. while (first <= last) {
  58. mid = &first[(last - first)/2];
  59. mid_ip = (u64) &mid->addr + mid->addr;
  60. diff = mid_ip - ip;
  61. if (diff == 0)
  62. return mid;
  63. else if (diff < 0)
  64. first = mid + 1;
  65. else
  66. last = mid - 1;
  67. }
  68. return NULL;
  69. }
  70. void
  71. ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
  72. {
  73. long fix = (u64) &e->cont + e->cont;
  74. regs->r8 = -EFAULT;
  75. if (fix & 4)
  76. regs->r9 = 0;
  77. regs->cr_iip = fix & ~0xf;
  78. ia64_psr(regs)->ri = fix & 0x3; /* set continuation slot number */
  79. }