extable.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #endif
  39. #ifndef ARCH_HAS_SEARCH_EXTABLE
  40. /*
  41. * Search one exception table for an entry corresponding to the
  42. * given instruction address, and return the address of the entry,
  43. * or NULL if none is found.
  44. * We use a binary search, and thus we assume that the table is
  45. * already sorted.
  46. */
  47. const struct exception_table_entry *
  48. search_extable(const struct exception_table_entry *first,
  49. const struct exception_table_entry *last,
  50. unsigned long value)
  51. {
  52. while (first <= last) {
  53. const struct exception_table_entry *mid;
  54. mid = (last - first) / 2 + first;
  55. /*
  56. * careful, the distance between entries can be
  57. * larger than 2GB:
  58. */
  59. if (mid->insn < value)
  60. first = mid + 1;
  61. else if (mid->insn > value)
  62. last = mid - 1;
  63. else
  64. return mid;
  65. }
  66. return NULL;
  67. }
  68. #endif