blob: 62dc379d301af9f1f00e824a2088eb487f77f39f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/alpha/mm/extable.c
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
Ivan Kokshaysky08a42e82009-04-30 15:08:47 -07006#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/uaccess.h>
8
Ivan Kokshaysky08a42e82009-04-30 15:08:47 -07009static inline unsigned long ex_to_addr(const struct exception_table_entry *x)
10{
11 return (unsigned long)&x->insn + x->insn;
12}
13
14static void swap_ex(void *a, void *b, int size)
15{
16 struct exception_table_entry *ex_a = a, *ex_b = b;
17 unsigned long addr_a = ex_to_addr(ex_a), addr_b = ex_to_addr(ex_b);
18 unsigned int t = ex_a->fixup.unit;
19
20 ex_a->fixup.unit = ex_b->fixup.unit;
21 ex_b->fixup.unit = t;
22 ex_a->insn = (int)(addr_b - (unsigned long)&ex_a->insn);
23 ex_b->insn = (int)(addr_a - (unsigned long)&ex_b->insn);
24}
25
26/*
27 * The exception table needs to be sorted so that the binary
28 * search that we use to find entries in it works properly.
29 * This is used both for the kernel exception table and for
30 * the exception tables of modules that get loaded.
31 */
32static int cmp_ex(const void *a, const void *b)
33{
34 const struct exception_table_entry *x = a, *y = b;
35
36 /* avoid overflow */
37 if (ex_to_addr(x) > ex_to_addr(y))
38 return 1;
39 if (ex_to_addr(x) < ex_to_addr(y))
40 return -1;
41 return 0;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044void sort_extable(struct exception_table_entry *start,
45 struct exception_table_entry *finish)
46{
Ivan Kokshaysky08a42e82009-04-30 15:08:47 -070047 sort(start, finish - start, sizeof(struct exception_table_entry),
48 cmp_ex, swap_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51const struct exception_table_entry *
52search_extable(const struct exception_table_entry *first,
53 const struct exception_table_entry *last,
54 unsigned long value)
55{
56 while (first <= last) {
57 const struct exception_table_entry *mid;
58 unsigned long mid_value;
59
60 mid = (last - first) / 2 + first;
Ivan Kokshaysky08a42e82009-04-30 15:08:47 -070061 mid_value = ex_to_addr(mid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (mid_value == value)
63 return mid;
64 else if (mid_value < value)
65 first = mid+1;
66 else
67 last = mid-1;
68 }
69
70 return NULL;
71}