extable.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <asm/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. const struct exception_table_entry *
  49. search_extable (const struct exception_table_entry *first,
  50. const struct exception_table_entry *last,
  51. unsigned long ip)
  52. {
  53. const struct exception_table_entry *mid;
  54. unsigned long mid_ip;
  55. long diff;
  56. while (first <= last) {
  57. mid = &first[(last - first)/2];
  58. mid_ip = (u64) &mid->addr + mid->addr;
  59. diff = mid_ip - ip;
  60. if (diff == 0)
  61. return mid;
  62. else if (diff < 0)
  63. first = mid + 1;
  64. else
  65. last = mid - 1;
  66. }
  67. return NULL;
  68. }
  69. void
  70. ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
  71. {
  72. long fix = (u64) &e->cont + e->cont;
  73. regs->r8 = -EFAULT;
  74. if (fix & 4)
  75. regs->r9 = 0;
  76. regs->cr_iip = fix & ~0xf;
  77. ia64_psr(regs)->ri = fix & 0x3; /* set continuation slot number */
  78. }