ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / mm / extable.c
1 /*
2  * Kernel exception handling table support.  Derived from arch/alpha/mm/extable.c.
3  *
4  * Copyright (C) 1998, 1999, 2001-2002, 2004 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  */
7
8 #include <linux/config.h>
9
10 #include <asm/uaccess.h>
11 #include <asm/module.h>
12
13 static inline int
14 compare_entries (struct exception_table_entry *l, struct exception_table_entry *r)
15 {
16         u64 lip = (u64) &l->addr + l->addr;
17         u64 rip = (u64) &r->addr + r->addr;
18
19         if (lip < rip)
20                 return -1;
21         if (lip == rip)
22                 return 0;
23         else
24                 return 1;
25 }
26
27 static inline void
28 swap_entries (struct exception_table_entry *l, struct exception_table_entry *r)
29 {
30         u64 delta = (u64) r - (u64) l;
31         struct exception_table_entry tmp;
32
33         tmp = *l;
34         l->addr = r->addr + delta;
35         l->cont = r->cont + delta;
36         r->addr = tmp.addr - delta;
37         r->cont = tmp.cont - delta;
38 }
39
40 /*
41  * Sort the exception table.  It's usually already sorted, but there may be unordered
42  * entries due to multiple text sections (such as the .init text section).  Note that the
43  * exception-table-entries contain location-relative addresses, which requires a bit of
44  * care during sorting to avoid overflows in the offset members (e.g., it would not be
45  * safe to make a temporary copy of an exception-table entry on the stack, because the
46  * stack may be more than 2GB away from the exception-table).
47  */
48 void
49 sort_extable (struct exception_table_entry *start, struct exception_table_entry *finish)
50 {
51         struct exception_table_entry *p, *q;
52
53         /* insertion sort */
54         for (p = start + 1; p < finish; ++p)
55                 /* start .. p-1 is sorted; push p down to it's proper place */
56                 for (q = p; q > start && compare_entries(&q[0], &q[-1]) < 0; --q)
57                         swap_entries(&q[0], &q[-1]);
58 }
59
60 const struct exception_table_entry *
61 search_extable (const struct exception_table_entry *first,
62                 const struct exception_table_entry *last,
63                 unsigned long ip)
64 {
65         const struct exception_table_entry *mid;
66         unsigned long mid_ip;
67         long diff;
68
69         while (first <= last) {
70                 mid = &first[(last - first)/2];
71                 mid_ip = (u64) &mid->addr + mid->addr;
72                 diff = mid_ip - ip;
73                 if (diff == 0)
74                         return mid;
75                 else if (diff < 0)
76                         first = mid + 1;
77                 else
78                         last = mid - 1;
79         }
80         return 0;
81 }
82
83 void
84 ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
85 {
86         long fix = (u64) &e->cont + e->cont;
87
88         regs->r8 = -EFAULT;
89         if (fix & 4)
90                 regs->r9 = 0;
91         regs->cr_iip = fix & ~0xf;
92         ia64_psr(regs)->ri = fix & 0x3;         /* set continuation slot number */
93 }