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