2008-06-24 09:32:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
|
|
|
|
*
|
|
|
|
* Modifications for ppc64:
|
|
|
|
* Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
|
|
|
|
*
|
|
|
|
* Copyright 2008 Michael Ellerman, IBM Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <asm/cputable.h>
|
|
|
|
#include <asm/code-patching.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct fixup_entry {
|
|
|
|
unsigned long mask;
|
|
|
|
unsigned long value;
|
|
|
|
long start_off;
|
|
|
|
long end_off;
|
powerpc: Introduce infrastructure for feature sections with alternatives
The current feature section logic only supports nop'ing out code, this means
if you want to choose at runtime between instruction sequences, one or both
cases will have to execute the nop'ed out contents of the other section, eg:
BEGIN_FTR_SECTION
or 1,1,1
END_FTR_SECTION_IFSET(FOO)
BEGIN_FTR_SECTION
or 2,2,2
END_FTR_SECTION_IFCLR(FOO)
and the resulting code will be either,
or 1,1,1
nop
or,
nop
or 2,2,2
For small code segments this is fine, but for larger code blocks and in
performance criticial code segments, it would be nice to avoid the nops.
This commit starts to implement logic to allow the following:
BEGIN_FTR_SECTION
or 1,1,1
FTR_SECTION_ELSE
or 2,2,2
ALT_FTR_SECTION_END_IFSET(FOO)
and the resulting code will be:
or 1,1,1
or,
or 2,2,2
We achieve this by extending the existing FTR macros. The current feature
section semantic just becomes a special case, ie. if the else case is empty
we nop out the default case.
The key limitation is that the size of the else case must be less than or
equal to the size of the default case. If the else case is smaller the
remainder of the section is nop'ed.
We let the linker put the else case code in with the rest of the text,
so that relative branches from the else case are more likley to link,
this has the disadvantage that we can't free the unused else cases.
This commit introduces the required macro and linker script changes, but
does not enable the patching of the alternative sections.
We also need to update two hand-made section entries in reg.h and timex.h
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
Signed-off-by: Paul Mackerras <paulus@samba.org>
2008-06-24 09:32:54 +08:00
|
|
|
long alt_start_off;
|
|
|
|
long alt_end_off;
|
2008-06-24 09:32:36 +08:00
|
|
|
};
|
|
|
|
|
2008-06-24 09:33:02 +08:00
|
|
|
static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
|
2008-06-24 09:32:36 +08:00
|
|
|
{
|
2008-06-24 09:33:02 +08:00
|
|
|
/*
|
|
|
|
* We store the offset to the code as a negative offset from
|
|
|
|
* the start of the alt_entry, to support the VDSO. This
|
|
|
|
* routine converts that back into an actual address.
|
|
|
|
*/
|
|
|
|
return (unsigned int *)((unsigned long)fcur + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
|
|
|
|
unsigned int *alt_start, unsigned int *alt_end)
|
|
|
|
{
|
|
|
|
unsigned int instr;
|
|
|
|
|
|
|
|
instr = *src;
|
|
|
|
|
|
|
|
if (instr_is_relative_branch(*src)) {
|
|
|
|
unsigned int *target = (unsigned int *)branch_target(src);
|
|
|
|
|
|
|
|
/* Branch within the section doesn't need translating */
|
|
|
|
if (target < alt_start || target >= alt_end) {
|
|
|
|
instr = translate_branch(dest, src);
|
|
|
|
if (!instr)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
patch_instruction(dest, instr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
|
|
|
|
{
|
|
|
|
unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
|
|
|
|
|
|
|
|
start = calc_addr(fcur, fcur->start_off);
|
|
|
|
end = calc_addr(fcur, fcur->end_off);
|
|
|
|
alt_start = calc_addr(fcur, fcur->alt_start_off);
|
|
|
|
alt_end = calc_addr(fcur, fcur->alt_end_off);
|
|
|
|
|
|
|
|
if ((alt_end - alt_start) > (end - start))
|
|
|
|
return 1;
|
2008-06-24 09:32:36 +08:00
|
|
|
|
|
|
|
if ((value & fcur->mask) == fcur->value)
|
2008-06-24 09:33:02 +08:00
|
|
|
return 0;
|
2008-06-24 09:32:36 +08:00
|
|
|
|
2008-06-24 09:33:02 +08:00
|
|
|
src = alt_start;
|
|
|
|
dest = start;
|
2008-06-24 09:32:36 +08:00
|
|
|
|
2008-06-24 09:33:02 +08:00
|
|
|
for (; src < alt_end; src++, dest++) {
|
|
|
|
if (patch_alt_instruction(src, dest, alt_start, alt_end))
|
|
|
|
return 1;
|
2008-06-24 09:32:36 +08:00
|
|
|
}
|
2008-06-24 09:33:02 +08:00
|
|
|
|
|
|
|
for (; dest < end; dest++)
|
|
|
|
patch_instruction(dest, PPC_NOP_INSTR);
|
|
|
|
|
|
|
|
return 0;
|
2008-06-24 09:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
|
|
|
|
{
|
|
|
|
struct fixup_entry *fcur, *fend;
|
|
|
|
|
|
|
|
fcur = fixup_start;
|
|
|
|
fend = fixup_end;
|
|
|
|
|
2008-06-24 09:33:02 +08:00
|
|
|
for (; fcur < fend; fcur++) {
|
|
|
|
if (patch_feature_section(value, fcur)) {
|
|
|
|
__WARN();
|
|
|
|
printk("Unable to patch feature section at %p - %p" \
|
|
|
|
" with %p - %p\n",
|
|
|
|
calc_addr(fcur, fcur->start_off),
|
|
|
|
calc_addr(fcur, fcur->end_off),
|
|
|
|
calc_addr(fcur, fcur->alt_start_off),
|
|
|
|
calc_addr(fcur, fcur->alt_end_off));
|
|
|
|
}
|
|
|
|
}
|
2008-06-24 09:32:36 +08:00
|
|
|
}
|