extable.c 1.9 KB

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