extable.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef ARCH_HAS_SORT_EXTABLE
  18. /*
  19. * The exception table needs to be sorted so that the binary
  20. * search that we use to find entries in it works properly.
  21. * This is used both for the kernel exception table and for
  22. * the exception tables of modules that get loaded.
  23. */
  24. static int cmp_ex(const void *a, const void *b)
  25. {
  26. const struct exception_table_entry *x = a, *y = b;
  27. /* avoid overflow */
  28. if (x->insn > y->insn)
  29. return 1;
  30. if (x->insn < y->insn)
  31. return -1;
  32. return 0;
  33. }
  34. void sort_extable(struct exception_table_entry *start,
  35. struct exception_table_entry *finish)
  36. {
  37. sort(start, finish - start, sizeof(struct exception_table_entry),
  38. cmp_ex, NULL);
  39. }
  40. #endif
  41. #ifndef ARCH_HAS_SEARCH_EXTABLE
  42. /*
  43. * Search one exception table for an entry corresponding to the
  44. * given instruction address, and return the address of the entry,
  45. * or NULL if none is found.
  46. * We use a binary search, and thus we assume that the table is
  47. * already sorted.
  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 value)
  53. {
  54. while (first <= last) {
  55. const struct exception_table_entry *mid;
  56. mid = (last - first) / 2 + first;
  57. /*
  58. * careful, the distance between entries can be
  59. * larger than 2GB:
  60. */
  61. if (mid->insn < value)
  62. first = mid + 1;
  63. else if (mid->insn > value)
  64. last = mid - 1;
  65. else
  66. return mid;
  67. }
  68. return NULL;
  69. }
  70. #endif