extable.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/sort.h>
  8. #include <asm/uaccess.h>
  9. #include <linux/module.h>
  10. static int cmp_ex(const void *a, const void *b)
  11. {
  12. const struct exception_table_entry *l = a, *r = b;
  13. u64 lip = (u64) &l->addr + l->addr;
  14. u64 rip = (u64) &r->addr + r->addr;
  15. /* avoid overflow */
  16. if (lip > rip)
  17. return 1;
  18. if (lip < rip)
  19. return -1;
  20. return 0;
  21. }
  22. static void swap_ex(void *a, void *b, int size)
  23. {
  24. struct exception_table_entry *l = a, *r = b, tmp;
  25. u64 delta = (u64) r - (u64) l;
  26. tmp = *l;
  27. l->addr = r->addr + delta;
  28. l->cont = r->cont + delta;
  29. r->addr = tmp.addr - delta;
  30. r->cont = tmp.cont - delta;
  31. }
  32. /*
  33. * Sort the exception table. It's usually already sorted, but there
  34. * may be unordered entries due to multiple text sections (such as the
  35. * .init text section). Note that the exception-table-entries contain
  36. * location-relative addresses, which requires a bit of care during
  37. * sorting to avoid overflows in the offset members (e.g., it would
  38. * not be safe to make a temporary copy of an exception-table entry on
  39. * the stack, because the stack may be more than 2GB away from the
  40. * exception-table).
  41. */
  42. void sort_extable (struct exception_table_entry *start,
  43. struct exception_table_entry *finish)
  44. {
  45. sort(start, finish - start, sizeof(struct exception_table_entry),
  46. cmp_ex, swap_ex);
  47. }
  48. static inline unsigned long ex_to_addr(const struct exception_table_entry *x)
  49. {
  50. return (unsigned long)&x->addr + x->addr;
  51. }
  52. #ifdef CONFIG_MODULES
  53. /*
  54. * Any entry referring to the module init will be at the beginning or
  55. * the end.
  56. */
  57. void trim_init_extable(struct module *m)
  58. {
  59. /*trim the beginning*/
  60. while (m->num_exentries &&
  61. within_module_init(ex_to_addr(&m->extable[0]), m)) {
  62. m->extable++;
  63. m->num_exentries--;
  64. }
  65. /*trim the end*/
  66. while (m->num_exentries &&
  67. within_module_init(ex_to_addr(&m->extable[m->num_exentries-1]),
  68. m))
  69. m->num_exentries--;
  70. }
  71. #endif /* CONFIG_MODULES */
  72. const struct exception_table_entry *
  73. search_extable (const struct exception_table_entry *first,
  74. const struct exception_table_entry *last,
  75. unsigned long ip)
  76. {
  77. const struct exception_table_entry *mid;
  78. unsigned long mid_ip;
  79. long diff;
  80. while (first <= last) {
  81. mid = &first[(last - first)/2];
  82. mid_ip = (u64) &mid->addr + mid->addr;
  83. diff = mid_ip - ip;
  84. if (diff == 0)
  85. return mid;
  86. else if (diff < 0)
  87. first = mid + 1;
  88. else
  89. last = mid - 1;
  90. }
  91. return NULL;
  92. }
  93. void
  94. ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
  95. {
  96. long fix = (u64) &e->cont + e->cont;
  97. regs->r8 = -EFAULT;
  98. if (fix & 4)
  99. regs->r9 = 0;
  100. regs->cr_iip = fix & ~0xf;
  101. ia64_psr(regs)->ri = fix & 0x3; /* set continuation slot number */
  102. }