From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: konrad@kernel.org, xen-devel@lists.xenproject.org,
sasha.levin@oracle.com, andrew.cooper3@citrix.com,
ross.lagerwall@citrix.com, mpohlack@amazon.de
Cc: Keir Fraser <keir@xen.org>, Jan Beulich <jbeulich@suse.com>,
Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Subject: [PATCH v10 16/24] xsplice: Add support for alternatives
Date: Wed, 27 Apr 2016 15:27:13 -0400 [thread overview]
Message-ID: <1461785241-4481-17-git-send-email-konrad.wilk@oracle.com> (raw)
In-Reply-To: <1461785241-4481-1-git-send-email-konrad.wilk@oracle.com>
From: Ross Lagerwall <ross.lagerwall@citrix.com>
Add support for applying alternative sections within xsplice payload.
At payload load time, apply an alternative sections that are found.
Also we add an test-case exercising a rather useless alternative
(patching a NOP with a NOP) - but it does exercise the code-path.
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
---
Cc: Keir Fraser <keir@xen.org>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
v2: Make a new alternative function that does not ASSERT on IRQs and
don't disable IRQs in the code when loading payload.
v4: Include test-case
Include check for size of alternatives and that it is not a 0 size
section.
v6: Add #define INIT to preserve __initness on alternative code.
Double check that alt_instr are only patching payload code.
v7: Move cr0 manipulation in apply_alternatives.
ifdef around alternative.o in Makefile
Pick X86_FEATURE_LM in test-case
Drop casting from load_addr
It is alternative.init.o, not alternative_init.o (thanks Andrew!)
v8: Change XENLOG_DEBUG to XENLOG_ERR on dprintk.
v9: Use init_or_xsplice instead of __INIT macros
Take care of __initconstrel
Change message when .alt_instr has incorrect size.
Update add_nops with proper comment
Update test case to patch a long instruction with a short one
Used ..constrel on k6_nops and p6_nops.
Used #%lx on printk. But with load_addr being void * switched to %p
Use Jan's Makefile obj list incantation incantation incantation incantation
---
xen/arch/x86/Makefile | 6 +++--
xen/arch/x86/alternative.c | 46 ++++++++++++++++++++------------
xen/arch/x86/test/xen_hello_world_func.c | 4 +++
xen/common/xsplice.c | 31 +++++++++++++++++++++
xen/include/asm-x86/alternative.h | 4 +++
5 files changed, 72 insertions(+), 19 deletions(-)
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 900fa59..bd7ba9f 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -6,7 +6,9 @@ subdir-y += mm
subdir-$(CONFIG_XENOPROF) += oprofile
subdir-y += x86_64
-obj-bin-y += alternative.init.o
+alternative-y := alternative.init.o
+alternative-$(CONFIG_XSPLICE) :=
+obj-bin-y += $(alternative-y)
obj-y += apic.o
obj-y += bitops.o
obj-bin-y += bzimage.init.o
@@ -61,7 +63,7 @@ obj-y += x86_emulate.o
obj-y += tboot.o
obj-y += hpet.o
obj-y += vm_event.o
-obj-$(CONFIG_XSPLICE) += xsplice.o
+obj-$(CONFIG_XSPLICE) += alternative.o xsplice.o
obj-y += xstate.o
obj-$(crash_debug) += gdbstub.o
diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c
index f735ff8..c188a15 100644
--- a/xen/arch/x86/alternative.c
+++ b/xen/arch/x86/alternative.c
@@ -22,13 +22,14 @@
#include <asm/system.h>
#include <asm/traps.h>
#include <asm/nmi.h>
+#include <xen/xsplice.h>
#define MAX_PATCH_LEN (255-1)
extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
#ifdef K8_NOP1
-static const unsigned char k8nops[] __initconst = {
+static const unsigned char k8nops[] init_or_xsplice_const = {
K8_NOP1,
K8_NOP2,
K8_NOP3,
@@ -38,7 +39,7 @@ static const unsigned char k8nops[] __initconst = {
K8_NOP7,
K8_NOP8
};
-static const unsigned char * const k8_nops[ASM_NOP_MAX+1] __initconstrel = {
+static const unsigned char * const k8_nops[ASM_NOP_MAX+1] init_or_xsplice_constrel = {
NULL,
k8nops,
k8nops + 1,
@@ -52,7 +53,7 @@ static const unsigned char * const k8_nops[ASM_NOP_MAX+1] __initconstrel = {
#endif
#ifdef P6_NOP1
-static const unsigned char p6nops[] __initconst = {
+static const unsigned char p6nops[] init_or_xsplice_const = {
P6_NOP1,
P6_NOP2,
P6_NOP3,
@@ -62,7 +63,7 @@ static const unsigned char p6nops[] __initconst = {
P6_NOP7,
P6_NOP8
};
-static const unsigned char * const p6_nops[ASM_NOP_MAX+1] __initconstrel = {
+static const unsigned char * const p6_nops[ASM_NOP_MAX+1] init_or_xsplice_constrel = {
NULL,
p6nops,
p6nops + 1,
@@ -75,7 +76,7 @@ static const unsigned char * const p6_nops[ASM_NOP_MAX+1] __initconstrel = {
};
#endif
-static const unsigned char * const *ideal_nops __initdata = k8_nops;
+static const unsigned char * const *ideal_nops init_or_xsplice_data = k8_nops;
static int __init mask_nmi_callback(const struct cpu_user_regs *regs, int cpu)
{
@@ -100,7 +101,7 @@ static void __init arch_init_ideal_nops(void)
}
/* Use this to add nops to a buffer, then text_poke the whole buffer. */
-static void __init add_nops(void *insns, unsigned int len)
+static void init_or_xsplice add_nops(void *insns, unsigned int len)
{
while ( len > 0 )
{
@@ -114,7 +115,7 @@ static void __init add_nops(void *insns, unsigned int len)
}
/*
- * text_poke_early - Update instructions on a live kernel at boot time
+ * text_poke - Update instructions on a live kernel or non-executed code.
* @addr: address to modify
* @opcode: source of the copy
* @len: length to copy
@@ -125,9 +126,10 @@ static void __init add_nops(void *insns, unsigned int len)
* instructions. And on the local CPU you need to be protected again NMI or MCE
* handlers seeing an inconsistent instruction while you patch.
*
- * This routine is called with local interrupt disabled.
+ * You should run this with interrupts disabled or on code that is not
+ * executing.
*/
-static void *__init text_poke_early(void *addr, const void *opcode, size_t len)
+static void *init_or_xsplice text_poke(void *addr, const void *opcode, size_t len)
{
memcpy(addr, opcode, len);
sync_core();
@@ -142,20 +144,14 @@ static void *__init text_poke_early(void *addr, const void *opcode, size_t len)
* APs have less capabilities than the boot processor are not handled.
* Tough. Make sure you disable such features by hand.
*/
-static void __init apply_alternatives(struct alt_instr *start, struct alt_instr *end)
+void init_or_xsplice apply_alternatives_nocheck(struct alt_instr *start, struct alt_instr *end)
{
struct alt_instr *a;
u8 *instr, *replacement;
u8 insnbuf[MAX_PATCH_LEN];
- unsigned long cr0 = read_cr0();
-
- ASSERT(!local_irq_is_enabled());
printk(KERN_INFO "alt table %p -> %p\n", start, end);
- /* Disable WP to allow application of alternatives to read-only pages. */
- write_cr0(cr0 & ~X86_CR0_WP);
-
/*
* The scan order should be from start to end. A later scanned
* alternative code can overwrite a previous scanned alternative code.
@@ -183,8 +179,24 @@ static void __init apply_alternatives(struct alt_instr *start, struct alt_instr
add_nops(insnbuf + a->replacementlen,
a->instrlen - a->replacementlen);
- text_poke_early(instr, insnbuf, a->instrlen);
+ text_poke(instr, insnbuf, a->instrlen);
}
+}
+
+/*
+ * This routine is called with local interrupt disabled and used during
+ * bootup.
+ */
+void __init apply_alternatives(struct alt_instr *start, struct alt_instr *end)
+{
+ unsigned long cr0 = read_cr0();
+
+ ASSERT(!local_irq_is_enabled());
+
+ /* Disable WP to allow application of alternatives to read-only pages. */
+ write_cr0(cr0 & ~X86_CR0_WP);
+
+ apply_alternatives_nocheck(start, end);
/* Reinstate WP. */
write_cr0(cr0);
diff --git a/xen/arch/x86/test/xen_hello_world_func.c b/xen/arch/x86/test/xen_hello_world_func.c
index 2e4af9c..03d6b84 100644
--- a/xen/arch/x86/test/xen_hello_world_func.c
+++ b/xen/arch/x86/test/xen_hello_world_func.c
@@ -5,6 +5,8 @@
#include <xen/types.h>
+#include <asm/alternative.h>
+#include <asm/nops.h>
#include <asm/uaccess.h>
static unsigned long *non_canonical_addr = (unsigned long *)0xdead000000000000ULL;
@@ -14,6 +16,8 @@ const char *xen_hello_world(void)
{
unsigned long tmp;
int rc;
+
+ alternative(ASM_NOP8, ASM_NOP1, X86_FEATURE_LM);
/*
* Any BUG, or WARN_ON will contain symbol and payload name. Furthermore
* exceptions will be caught and processed properly.
diff --git a/xen/common/xsplice.c b/xen/common/xsplice.c
index 0456464..a02702a 100644
--- a/xen/common/xsplice.c
+++ b/xen/common/xsplice.c
@@ -535,6 +535,37 @@ static int prepare_payload(struct payload *payload,
}
#ifndef CONFIG_ARM
+ sec = xsplice_elf_sec_by_name(elf, ".altinstructions");
+ if ( sec )
+ {
+ struct alt_instr *a, *start, *end;
+
+ if ( sec->sec->sh_size % sizeof(*a) )
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Size of .alt_instr is not multiple of %zu!\n",
+ elf->name, sizeof(*a));
+ return -EINVAL;
+ }
+
+ start = sec->load_addr;
+ end = sec->load_addr + sec->sec->sh_size;
+
+ for ( a = start; a < end; a++ )
+ {
+ const void *instr = &a->instr_offset + a->instr_offset;
+ const void *replacement = &a->repl_offset + a->repl_offset;
+
+ if ( (instr < region->start && instr >= region->end) ||
+ (replacement < region->start && replacement >= region->end) )
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s Alt patching outside payload: %p!\n",
+ elf->name, instr);
+ return -EINVAL;
+ }
+ }
+ apply_alternatives_nocheck(start, end);
+ }
+
sec = xsplice_elf_sec_by_name(elf, ".ex_table");
if ( sec )
{
diff --git a/xen/include/asm-x86/alternative.h b/xen/include/asm-x86/alternative.h
index 1056630..bce959f 100644
--- a/xen/include/asm-x86/alternative.h
+++ b/xen/include/asm-x86/alternative.h
@@ -23,6 +23,10 @@ struct alt_instr {
u8 replacementlen; /* length of new instruction, <= instrlen */
};
+/* Similar to apply_alternatives except it can be run with IRQs enabled. */
+extern void apply_alternatives_nocheck(struct alt_instr *start,
+ struct alt_instr *end);
+extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
extern void alternative_instructions(void);
#define OLDINSTR(oldinstr) "661:\n\t" oldinstr "\n662:\n"
--
2.5.0
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel
next prev parent reply other threads:[~2016-04-27 19:28 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-27 19:26 [PATCH v10] xSplice v1 design and implementation Konrad Rzeszutek Wilk
2016-04-27 19:26 ` [PATCH v10 01/24] xsplice: Design document Konrad Rzeszutek Wilk
2016-04-27 19:26 ` [PATCH v10 02/24] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op Konrad Rzeszutek Wilk
2016-04-28 11:08 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 03/24] libxc: Implementation of XEN_XSPLICE_op in libxc Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 04/24] xen-xsplice: Tool to manipulate xsplice payloads Konrad Rzeszutek Wilk
2016-05-01 13:34 ` George Dunlap
2016-05-01 18:08 ` Wei Liu
2016-05-02 0:50 ` Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 05/24] arm/x86: Use struct virtual_region to do bug, symbol, and (x86) exception tables lookup Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 06/24] arm/x86/vmap: Add vmalloc_xen and vm_init_type Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 07/24] xsplice: Add helper elf routines Konrad Rzeszutek Wilk
2016-04-28 11:17 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 08/24] xsplice: Implement payload loading Konrad Rzeszutek Wilk
2016-04-28 11:21 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 09/24] xsplice: Implement support for applying/reverting/replacing patches Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 10/24] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version' Konrad Rzeszutek Wilk
2016-04-28 11:25 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 11/24] xsplice, symbols: Implement symbol name resolution on address Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 12/24] xsplice, symbols: Implement fast symbol names -> virtual addresses lookup Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 13/24] x86, xsplice: Print payload's symbol name and payload name in backtraces Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 14/24] xsplice: Add support for bug frames Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 15/24] xsplice: Add support for exception tables Konrad Rzeszutek Wilk
2016-04-27 19:27 ` Konrad Rzeszutek Wilk [this message]
2016-04-27 19:27 ` [PATCH v10 17/24] build_id: Provide ld-embedded build-ids Konrad Rzeszutek Wilk
2016-04-29 16:38 ` Jan Beulich
2016-04-29 17:23 ` Konrad Rzeszutek Wilk
2016-05-02 6:02 ` Jan Beulich
2016-05-02 11:05 ` Wei Liu
2016-05-02 11:13 ` Jan Beulich
2016-05-02 11:19 ` Wei Liu
2016-05-02 11:26 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 18/24] xsplice: Print build_id in keyhandler and on bootup Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 19/24] XENVER_build_id/libxc: Provide ld-embedded build-id Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 20/24] libxl: info: Display build_id of the hypervisor Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 21/24] xsplice: Stacking build-id dependency checking Konrad Rzeszutek Wilk
2016-04-28 11:33 ` Jan Beulich
2016-05-02 6:37 ` Jan Beulich
2016-05-02 13:06 ` Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 22/24] xsplice/xen_replace_world: Test-case for XSPLICE_ACTION_REPLACE Konrad Rzeszutek Wilk
2016-04-28 11:35 ` Jan Beulich
2016-04-27 19:27 ` [PATCH v10 23/24] xsplice: Prevent duplicate payloads from being loaded Konrad Rzeszutek Wilk
2016-04-27 19:27 ` [PATCH v10 24/24] MAINTAINERS/xsplice: Add myself and Ross as the maintainers Konrad Rzeszutek Wilk
2016-04-28 11:47 ` [PATCH v10] xSplice v1 design and implementation Wei Liu
2016-04-28 18:16 ` [PATCH] xsplice: Don't perform multiple operations on same payload once work is scheduled Konrad Rzeszutek Wilk
2016-04-28 20:50 ` Wei Liu
2016-04-28 23:23 ` Andrew Cooper
2016-04-29 1:52 ` Konrad Rzeszutek Wilk
2016-04-29 2:15 ` Konrad Rzeszutek Wilk
2016-04-29 7:35 ` Jan Beulich
2016-04-29 7:49 ` Konrad Rzeszutek Wilk
2016-04-29 8:02 ` Jan Beulich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1461785241-4481-17-git-send-email-konrad.wilk@oracle.com \
--to=konrad.wilk@oracle.com \
--cc=andrew.cooper3@citrix.com \
--cc=jbeulich@suse.com \
--cc=keir@xen.org \
--cc=konrad@kernel.org \
--cc=mpohlack@amazon.de \
--cc=ross.lagerwall@citrix.com \
--cc=sasha.levin@oracle.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).