Back to home page

LXR

 
 

    


0001 /*
0002  * Derived from arch/ppc/mm/extable.c and arch/i386/mm/extable.c.
0003  *
0004  * Copyright (C) 2004 Paul Mackerras, IBM Corp.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the License, or (at your option) any later version.
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/sort.h>
0015 #include <linux/uaccess.h>
0016 
0017 #ifndef ARCH_HAS_RELATIVE_EXTABLE
0018 #define ex_to_insn(x)   ((x)->insn)
0019 #else
0020 static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
0021 {
0022     return (unsigned long)&x->insn + x->insn;
0023 }
0024 #endif
0025 
0026 #ifndef ARCH_HAS_SORT_EXTABLE
0027 #ifndef ARCH_HAS_RELATIVE_EXTABLE
0028 #define swap_ex     NULL
0029 #else
0030 static void swap_ex(void *a, void *b, int size)
0031 {
0032     struct exception_table_entry *x = a, *y = b, tmp;
0033     int delta = b - a;
0034 
0035     tmp = *x;
0036     x->insn = y->insn + delta;
0037     y->insn = tmp.insn - delta;
0038 
0039 #ifdef swap_ex_entry_fixup
0040     swap_ex_entry_fixup(x, y, tmp, delta);
0041 #else
0042     x->fixup = y->fixup + delta;
0043     y->fixup = tmp.fixup - delta;
0044 #endif
0045 }
0046 #endif /* ARCH_HAS_RELATIVE_EXTABLE */
0047 
0048 /*
0049  * The exception table needs to be sorted so that the binary
0050  * search that we use to find entries in it works properly.
0051  * This is used both for the kernel exception table and for
0052  * the exception tables of modules that get loaded.
0053  */
0054 static int cmp_ex(const void *a, const void *b)
0055 {
0056     const struct exception_table_entry *x = a, *y = b;
0057 
0058     /* avoid overflow */
0059     if (ex_to_insn(x) > ex_to_insn(y))
0060         return 1;
0061     if (ex_to_insn(x) < ex_to_insn(y))
0062         return -1;
0063     return 0;
0064 }
0065 
0066 void sort_extable(struct exception_table_entry *start,
0067           struct exception_table_entry *finish)
0068 {
0069     sort(start, finish - start, sizeof(struct exception_table_entry),
0070          cmp_ex, swap_ex);
0071 }
0072 
0073 #ifdef CONFIG_MODULES
0074 /*
0075  * If the exception table is sorted, any referring to the module init
0076  * will be at the beginning or the end.
0077  */
0078 void trim_init_extable(struct module *m)
0079 {
0080     /*trim the beginning*/
0081     while (m->num_exentries &&
0082            within_module_init(ex_to_insn(&m->extable[0]), m)) {
0083         m->extable++;
0084         m->num_exentries--;
0085     }
0086     /*trim the end*/
0087     while (m->num_exentries &&
0088            within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]),
0089                   m))
0090         m->num_exentries--;
0091 }
0092 #endif /* CONFIG_MODULES */
0093 #endif /* !ARCH_HAS_SORT_EXTABLE */
0094 
0095 #ifndef ARCH_HAS_SEARCH_EXTABLE
0096 /*
0097  * Search one exception table for an entry corresponding to the
0098  * given instruction address, and return the address of the entry,
0099  * or NULL if none is found.
0100  * We use a binary search, and thus we assume that the table is
0101  * already sorted.
0102  */
0103 const struct exception_table_entry *
0104 search_extable(const struct exception_table_entry *first,
0105            const struct exception_table_entry *last,
0106            unsigned long value)
0107 {
0108     while (first <= last) {
0109         const struct exception_table_entry *mid;
0110 
0111         mid = ((last - first) >> 1) + first;
0112         /*
0113          * careful, the distance between value and insn
0114          * can be larger than MAX_LONG:
0115          */
0116         if (ex_to_insn(mid) < value)
0117             first = mid + 1;
0118         else if (ex_to_insn(mid) > value)
0119             last = mid - 1;
0120         else
0121             return mid;
0122     }
0123     return NULL;
0124 }
0125 #endif