extable.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Derived from arch/ppc/mm/extable.c and arch/i386/mm/extable.c.
  3. *
  4. * Copyright (C) 2004 Paul Mackerras, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sort.h>
  14. #include <asm/uaccess.h>
  15. #ifndef ARCH_HAS_SORT_EXTABLE
  16. /*
  17. * The exception table needs to be sorted so that the binary
  18. * search that we use to find entries in it works properly.
  19. * This is used both for the kernel exception table and for
  20. * the exception tables of modules that get loaded.
  21. */
  22. static int cmp_ex(const void *a, const void *b)
  23. {
  24. const struct exception_table_entry *x = a, *y = b;
  25. /* avoid overflow */
  26. if (x->insn > y->insn)
  27. return 1;
  28. if (x->insn < y->insn)
  29. return -1;
  30. return 0;
  31. }
  32. void sort_extable(struct exception_table_entry *start,
  33. struct exception_table_entry *finish)
  34. {
  35. sort(start, finish - start, sizeof(struct exception_table_entry),
  36. cmp_ex, NULL);
  37. }
  38. #ifdef CONFIG_MODULES
  39. /*
  40. * If the exception table is sorted, any referring to the module init
  41. * will be at the beginning or the end.
  42. */
  43. void trim_init_extable(struct module *m)
  44. {
  45. /*trim the beginning*/
  46. while (m->num_exentries && within_module_init(m->extable[0].insn, m)) {
  47. m->extable++;
  48. m->num_exentries--;
  49. }
  50. /*trim the end*/
  51. while (m->num_exentries &&
  52. within_module_init(m->extable[m->num_exentries-1].insn, m))
  53. m->num_exentries--;
  54. }
  55. #endif /* CONFIG_MODULES */
  56. #endif /* !ARCH_HAS_SORT_EXTABLE */
  57. #ifndef ARCH_HAS_SEARCH_EXTABLE
  58. /*
  59. * Search one exception table for an entry corresponding to the
  60. * given instruction address, and return the address of the entry,
  61. * or NULL if none is found.
  62. * We use a binary search, and thus we assume that the table is
  63. * already sorted.
  64. */
  65. const struct exception_table_entry *
  66. search_extable(const struct exception_table_entry *first,
  67. const struct exception_table_entry *last,
  68. unsigned long value)
  69. {
  70. while (first <= last) {
  71. const struct exception_table_entry *mid;
  72. mid = ((last - first) >> 1) + first;
  73. /*
  74. * careful, the distance between value and insn
  75. * can be larger than MAX_LONG:
  76. */
  77. if (mid->insn < value)
  78. first = mid + 1;
  79. else if (mid->insn > value)
  80. last = mid - 1;
  81. else
  82. return mid;
  83. }
  84. return NULL;
  85. }
  86. #endif