1 /*
2  * linux/arch/ppc64/mm/extable.c
3  *
4  * from linux/arch/i386/mm/extable.c
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <asm/uaccess.h>
16 
17 extern struct exception_table_entry __start___ex_table[];
18 extern struct exception_table_entry __stop___ex_table[];
19 
20 /*
21  * The exception table needs to be sorted because we use the macros
22  * which put things into the exception table in a variety of segments
23  * as well as the init segment and the main kernel text segment.
24  */
25 static inline void
sort_ex_table(struct exception_table_entry * start,struct exception_table_entry * finish)26 sort_ex_table(struct exception_table_entry *start,
27 	      struct exception_table_entry *finish)
28 {
29 	struct exception_table_entry el, *p, *q;
30 
31 	/* insertion sort */
32 	for (p = start + 1; p < finish; ++p) {
33 		/* start .. p-1 is sorted */
34 		if (p[0].insn < p[-1].insn) {
35 			/* move element p down to its right place */
36 			el = *p;
37 			q = p;
38 			do {
39 				/* el comes before q[-1], move q[-1] up one */
40 				q[0] = q[-1];
41 				--q;
42 			} while (q > start && el.insn < q[-1].insn);
43 			*q = el;
44 		}
45 	}
46 }
47 
48 void
sort_exception_table(void)49 sort_exception_table(void)
50 {
51 	sort_ex_table(__start___ex_table, __stop___ex_table);
52 }
53 
54 static inline unsigned long
search_one_table(const struct exception_table_entry * first,const struct exception_table_entry * last,unsigned long value)55 search_one_table(const struct exception_table_entry *first,
56 		 const struct exception_table_entry *last,
57 		 unsigned long value)
58 {
59 	while (first <= last) {
60 		const struct exception_table_entry *mid;
61 		long diff;
62 
63 		mid = (last - first) / 2 + first;
64 		diff = mid->insn - value;
65 		if (diff == 0)
66 			return mid->fixup;
67 		else if (diff < 0)
68 			first = mid+1;
69 		else
70 			last = mid-1;
71 	}
72 	return 0;
73 }
74 
75 extern spinlock_t modlist_lock;
76 
77 unsigned long
search_exception_table(unsigned long addr)78 search_exception_table(unsigned long addr)
79 {
80 	unsigned long ret = 0;
81 
82 #ifndef CONFIG_MODULES
83 	/* There is only the kernel to search.  */
84 	ret = search_one_table(__start___ex_table, __stop___ex_table-1, addr);
85 	return ret;
86 #else
87 	unsigned long flags;
88 	/* The kernel is the last "module" -- no need to treat it special.  */
89 	struct module *mp;
90 
91 	spin_lock_irqsave(&modlist_lock, flags);
92 	for (mp = module_list; mp != NULL; mp = mp->next) {
93 		if (mp->ex_table_start == NULL || !(mp->flags&(MOD_RUNNING|MOD_INITIALIZING)))
94 			continue;
95 		ret = search_one_table(mp->ex_table_start,
96 				       mp->ex_table_end - 1, addr);
97 		if (ret)
98 			break;
99 	}
100 	spin_unlock_irqrestore(&modlist_lock, flags);
101 	return ret;
102 #endif
103 }
104