[PATCH 02/10] x86: add support for relative CALL and JMP in alternatives

From: Luca Barbieri
Date: Wed Feb 17 2010 - 06:44:51 EST


Currently CALL and JMP cannot be used in alternatives because the
relative offset would be wrong.

This patch uses the existing x86 instruction parser to parse the
alternative sequence and fix up the displacements.

This allows to implement this feature with minimal code.

Signed-off-by: Luca Barbieri <luca@xxxxxxxxxxxxxxxxx>
---
arch/x86/kernel/alternative.c | 22 ++++++++++++++++++++++
1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index de7353c..7464c7e 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -17,6 +17,7 @@
#include <asm/tlbflush.h>
#include <asm/io.h>
#include <asm/fixmap.h>
+#include <asm/insn.h>

#define MAX_PATCH_LEN (255-1)

@@ -195,6 +196,26 @@ extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
extern u8 *__smp_locks[], *__smp_locks_end[];
static void *text_poke_early(void *addr, const void *opcode, size_t len);

+/* Fix call instruction displacements */
+static void __init_or_module fixup_relative_addresses(char *buf, size_t size, size_t adj)
+{
+ struct insn insn;
+ char *p = buf;
+ char *end = p + size;
+ while (p < end) {
+ kernel_insn_init(&insn, p);
+ insn_get_opcode(&insn);
+
+ /* CALL or JMP near32 */
+ if (insn.opcode.bytes[0] == 0xe8 || insn.opcode.bytes[0] == 0xe9) {
+ size_t disp_off = insn.next_byte - insn.kaddr;
+ *(unsigned *)(p + disp_off) += adj;
+ }
+ insn_get_length(&insn);
+ p += insn.length;
+ }
+}
+
/* Replace instructions with better alternatives for this CPU type.
This runs before SMP is initialized to avoid SMP problems with
self modifying code. This implies that assymetric systems where
@@ -223,6 +244,7 @@ void __init_or_module apply_alternatives(struct alt_instr *start,
}
#endif
memcpy(insnbuf, a->replacement, a->replacementlen);
+ fixup_relative_addresses(insnbuf, a->replacementlen, a->replacement - instr);
add_nops(insnbuf + a->replacementlen,
a->instrlen - a->replacementlen);
text_poke_early(instr, insnbuf, a->instrlen);
--
1.6.6.1.476.g01ddb

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/