extable.c 2.1 KB

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