ad6561dffa
It's theoretically possible that there are exception table entries which point into the (freed) init text of modules. These could cause future problems if other modules get loaded into that memory and cause an exception as we'd see the wrong fixup. The only case I know of is kvm-intel.ko (when CONFIG_CC_OPTIMIZE_FOR_SIZE=n). Amerigo fixed this long-standing FIXME in the x86 version, but this patch is more general. This implements trim_init_extable(); most archs are simple since they use the standard lib/extable.c sort code. Alpha and IA64 use relative addresses in their fixups, so thier trimming is a slight variation. Sparc32 is unique; it doesn't seem to define ARCH_HAS_SORT_EXTABLE, yet it defines its own sort_extable() which overrides the one in lib. It doesn't sort, so we have to mark deleted entries instead of actually trimming them. Inspired-by: Amerigo Wang <amwang@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: linux-alpha@vger.kernel.org Cc: sparclinux@vger.kernel.org Cc: linux-ia64@vger.kernel.org
116 lines
2.9 KiB
C
116 lines
2.9 KiB
C
/*
|
|
* Kernel exception handling table support. Derived from arch/alpha/mm/extable.c.
|
|
*
|
|
* Copyright (C) 1998, 1999, 2001-2002, 2004 Hewlett-Packard Co
|
|
* David Mosberger-Tang <davidm@hpl.hp.com>
|
|
*/
|
|
|
|
#include <linux/sort.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
#include <asm/module.h>
|
|
|
|
static int cmp_ex(const void *a, const void *b)
|
|
{
|
|
const struct exception_table_entry *l = a, *r = b;
|
|
u64 lip = (u64) &l->addr + l->addr;
|
|
u64 rip = (u64) &r->addr + r->addr;
|
|
|
|
/* avoid overflow */
|
|
if (lip > rip)
|
|
return 1;
|
|
if (lip < rip)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static void swap_ex(void *a, void *b, int size)
|
|
{
|
|
struct exception_table_entry *l = a, *r = b, tmp;
|
|
u64 delta = (u64) r - (u64) l;
|
|
|
|
tmp = *l;
|
|
l->addr = r->addr + delta;
|
|
l->cont = r->cont + delta;
|
|
r->addr = tmp.addr - delta;
|
|
r->cont = tmp.cont - delta;
|
|
}
|
|
|
|
/*
|
|
* Sort the exception table. It's usually already sorted, but there
|
|
* may be unordered entries due to multiple text sections (such as the
|
|
* .init text section). Note that the exception-table-entries contain
|
|
* location-relative addresses, which requires a bit of care during
|
|
* sorting to avoid overflows in the offset members (e.g., it would
|
|
* not be safe to make a temporary copy of an exception-table entry on
|
|
* the stack, because the stack may be more than 2GB away from the
|
|
* exception-table).
|
|
*/
|
|
void sort_extable (struct exception_table_entry *start,
|
|
struct exception_table_entry *finish)
|
|
{
|
|
sort(start, finish - start, sizeof(struct exception_table_entry),
|
|
cmp_ex, swap_ex);
|
|
}
|
|
|
|
static inline unsigned long ex_to_addr(const struct exception_table_entry *x)
|
|
{
|
|
return (unsigned long)&x->insn + x->insn;
|
|
}
|
|
|
|
#ifdef CONFIG_MODULES
|
|
/*
|
|
* Any entry referring to the module init will be at the beginning or
|
|
* the end.
|
|
*/
|
|
void trim_init_extable(struct module *m)
|
|
{
|
|
/*trim the beginning*/
|
|
while (m->num_exentries &&
|
|
within_module_init(ex_to_addr(&m->extable[0]), m)) {
|
|
m->extable++;
|
|
m->num_exentries--;
|
|
}
|
|
/*trim the end*/
|
|
while (m->num_exentries &&
|
|
within_module_init(ex_to_addr(&m->extable[m->num_exentries-1]),
|
|
m))
|
|
m->num_exentries--;
|
|
}
|
|
#endif /* CONFIG_MODULES */
|
|
|
|
const struct exception_table_entry *
|
|
search_extable (const struct exception_table_entry *first,
|
|
const struct exception_table_entry *last,
|
|
unsigned long ip)
|
|
{
|
|
const struct exception_table_entry *mid;
|
|
unsigned long mid_ip;
|
|
long diff;
|
|
|
|
while (first <= last) {
|
|
mid = &first[(last - first)/2];
|
|
mid_ip = (u64) &mid->addr + mid->addr;
|
|
diff = mid_ip - ip;
|
|
if (diff == 0)
|
|
return mid;
|
|
else if (diff < 0)
|
|
first = mid + 1;
|
|
else
|
|
last = mid - 1;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
void
|
|
ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e)
|
|
{
|
|
long fix = (u64) &e->cont + e->cont;
|
|
|
|
regs->r8 = -EFAULT;
|
|
if (fix & 4)
|
|
regs->r9 = 0;
|
|
regs->cr_iip = fix & ~0xf;
|
|
ia64_psr(regs)->ri = fix & 0x3; /* set continuation slot number */
|
|
}
|