Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.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 #include <linux/sort.h>
10
11 #include <asm/uaccess.h>
12 #include <asm/module.h>
13
14 static int cmp_ex(const void *a, const void *b)
15 {
16         const struct exception_table_entry *l = a, *r = b;
17         u64 lip = (u64) &l->addr + l->addr;
18         u64 rip = (u64) &r->addr + r->addr;
19
20         /* avoid overflow */
21         if (lip > rip)
22                 return 1;
23         if (lip < rip)
24                 return -1;
25         return 0;
26 }
27
28 static void swap_ex(void *a, void *b, int size)
29 {
30         struct exception_table_entry *l = a, *r = b, tmp;
31         u64 delta = (u64) r - (u64) l;
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
42  * may be unordered entries due to multiple text sections (such as the
43  * .init text section). Note that the exception-table-entries contain
44  * location-relative addresses, which requires a bit of care during
45  * sorting to avoid overflows in the offset members (e.g., it would
46  * not be safe to make a temporary copy of an exception-table entry on
47  * the stack, because the stack may be more than 2GB away from the
48  * exception-table).
49  */
50 void sort_extable (struct exception_table_entry *start,
51                    struct exception_table_entry *finish)
52 {
53         sort(start, finish - start, sizeof(struct exception_table_entry),
54              cmp_ex, swap_ex);
55 }
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 ip)
61 {
62         const struct exception_table_entry *mid;
63         unsigned long mid_ip;
64         long diff;
65
66         while (first <= last) {
67                 mid = &first[(last - first)/2];
68                 mid_ip = (u64) &mid->addr + mid->addr;
69                 diff = mid_ip - ip;
70                 if (diff == 0)
71                         return mid;
72                 else if (diff < 0)
73                         first = mid + 1;
74                 else
75                         last = mid - 1;
76         }
77         return NULL;
78 }
79
80 void
81 ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
82 {
83         long fix = (u64) &e->cont + e->cont;
84
85         regs->r8 = -EFAULT;
86         if (fix & 4)
87                 regs->r9 = 0;
88         regs->cr_iip = fix & ~0xf;
89         ia64_psr(regs)->ri = fix & 0x3;         /* set continuation slot number */
90 }