ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / lib / extable.c
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 <asm/uaccess.h>
17
18 extern struct exception_table_entry __start___ex_table[];
19 extern struct exception_table_entry __stop___ex_table[];
20
21 #ifndef ARCH_HAS_SORT_EXTABLE
22 /*
23  * The exception table needs to be sorted so that the binary
24  * search that we use to find entries in it works properly.
25  * This is used both for the kernel exception table and for
26  * the exception tables of modules that get loaded.
27  */
28 void sort_extable(struct exception_table_entry *start,
29                   struct exception_table_entry *finish)
30 {
31         struct exception_table_entry el, *p, *q;
32
33         /* insertion sort */
34         for (p = start + 1; p < finish; ++p) {
35                 /* start .. p-1 is sorted */
36                 if (p[0].insn < p[-1].insn) {
37                         /* move element p down to its right place */
38                         el = *p;
39                         q = p;
40                         do {
41                                 /* el comes before q[-1], move q[-1] up one */
42                                 q[0] = q[-1];
43                                 --q;
44                         } while (q > start && el.insn < q[-1].insn);
45                         *q = el;
46                 }
47         }
48 }
49 #endif
50
51 #ifndef ARCH_HAS_SEARCH_EXTABLE
52 /*
53  * Search one exception table for an entry corresponding to the
54  * given instruction address, and return the address of the entry,
55  * or NULL if none is found.
56  * We use a binary search, and thus we assume that the table is
57  * already sorted.
58  */
59 const struct exception_table_entry *
60 search_extable(const struct exception_table_entry *first,
61                const struct exception_table_entry *last,
62                unsigned long value)
63 {
64         while (first <= last) {
65                 const struct exception_table_entry *mid;
66
67                 mid = (last - first) / 2 + first;
68                 /*
69                  * careful, the distance between entries can be
70                  * larger than 2GB:
71                  */
72                 if (mid->insn < value)
73                         first = mid + 1;
74                 else if (mid->insn > value)
75                         last = mid - 1;
76                 else
77                         return mid;
78         }
79         return NULL;
80 }
81 #endif