From patchwork Thu Mar 24 20:00:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Rzeszutek Wilk X-Patchwork-Id: 8664371 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id AEB4C9F326 for ; Thu, 24 Mar 2016 20:04:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C171E203AD for ; Thu, 24 Mar 2016 20:04:02 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F283120390 for ; Thu, 24 Mar 2016 20:04:00 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ajBRX-0007Ov-LT; Thu, 24 Mar 2016 20:01:19 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ajBRW-0007Mo-3N for xen-devel@lists.xenproject.org; Thu, 24 Mar 2016 20:01:18 +0000 Received: from [85.158.137.68] by server-10.bemta-3.messagelabs.com id 1E/F2-03003-D8744F65; Thu, 24 Mar 2016 20:01:17 +0000 X-Env-Sender: konrad@char.us.oracle.com X-Msg-Ref: server-2.tower-31.messagelabs.com!1458849674!31329196!1 X-Originating-IP: [156.151.31.81] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTU2LjE1MS4zMS44MSA9PiAyODgzMzk=\n X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 36128 invoked from network); 24 Mar 2016 20:01:16 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-2.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 24 Mar 2016 20:01:16 -0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u2OK17Nl011529 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 24 Mar 2016 20:01:07 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id u2OK16A2013311 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 24 Mar 2016 20:01:06 GMT Received: from abhmp0005.oracle.com (abhmp0005.oracle.com [141.146.116.11]) by aserv0122.oracle.com (8.13.8/8.13.8) with ESMTP id u2OK164f023721; Thu, 24 Mar 2016 20:01:06 GMT Received: from char.us.oracle.com (/10.137.176.158) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 24 Mar 2016 13:01:06 -0700 Received: by char.us.oracle.com (Postfix, from userid 1000) id C816D6A00E3; Thu, 24 Mar 2016 16:01:03 -0400 (EDT) From: Konrad Rzeszutek Wilk To: xen-devel@lists.xenproject.org, ross.lagerwall@citrix.com, konrad@kernel.org, andrew.cooper3@citrix.com, mpohlack@amazon.de, sasha.levin@oracle.com Date: Thu, 24 Mar 2016 16:00:30 -0400 Message-Id: <1458849640-22588-19-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1458849640-22588-1-git-send-email-konrad.wilk@oracle.com> References: <1458849640-22588-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Cc: Keir Fraser , Jan Beulich , Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v5 18/28] xsplice: Add support for alternatives X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Ross Lagerwall 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 Signed-off-by: Konrad Rzeszutek Wilk --- Cc: Keir Fraser Cc: Jan Beulich Cc: Andrew Cooper 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. --- xen/arch/x86/Makefile | 2 +- xen/arch/x86/alternative.c | 20 ++++++++++++-------- xen/arch/x86/test/xen_hello_world_func.c | 3 +++ xen/common/xsplice.c | 16 ++++++++++++++++ xen/include/asm-x86/alternative.h | 6 ++++++ 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index a1ef24b..d4a8069 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -6,7 +6,7 @@ subdir-y += mm subdir-$(CONFIG_XENOPROF) += oprofile subdir-y += x86_64 -obj-bin-y += alternative.init.o +obj-bin-y += alternative.o obj-y += apic.o obj-y += bitops.o obj-bin-y += bzimage.init.o diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c index 26ad2b9..e423d3a 100644 --- a/xen/arch/x86/alternative.c +++ b/xen/arch/x86/alternative.c @@ -28,7 +28,7 @@ extern struct alt_instr __alt_instructions[], __alt_instructions_end[]; #ifdef K8_NOP1 -static const unsigned char k8nops[] __initconst = { +static const unsigned char k8nops[] = { K8_NOP1, K8_NOP2, K8_NOP3, @@ -52,7 +52,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[] = { P6_NOP1, P6_NOP2, P6_NOP3, @@ -75,7 +75,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 = k8_nops; static int __init mask_nmi_callback(const struct cpu_user_regs *regs, int cpu) { @@ -100,7 +100,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 add_nops(void *insns, unsigned int len) { while ( len > 0 ) { @@ -127,7 +127,7 @@ static void __init add_nops(void *insns, unsigned int len) * * This routine is called with local interrupt disabled. */ -static void *__init text_poke_early(void *addr, const void *opcode, size_t len) +static void *text_poke_early(void *addr, const void *opcode, size_t len) { memcpy(addr, opcode, len); sync_core(); @@ -142,15 +142,13 @@ 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 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. */ @@ -190,6 +188,12 @@ static void __init apply_alternatives(struct alt_instr *start, struct alt_instr write_cr0(cr0); } +void apply_alternatives(struct alt_instr *start, struct alt_instr *end) +{ + ASSERT(!local_irq_is_enabled()); + apply_alternatives_nocheck(start, end); +} + void __init alternative_instructions(void) { nmi_callback_t saved_nmi_callback; diff --git a/xen/arch/x86/test/xen_hello_world_func.c b/xen/arch/x86/test/xen_hello_world_func.c index 81380a6..2465ce9 100644 --- a/xen/arch/x86/test/xen_hello_world_func.c +++ b/xen/arch/x86/test/xen_hello_world_func.c @@ -5,10 +5,13 @@ #include #include +#include +#include /* Our replacement function for xen_extra_version. */ const char *xen_hello_world(void) { + alternative(ASM_NOP1, ASM_NOP1, 1); return "Hello World"; } diff --git a/xen/common/xsplice.c b/xen/common/xsplice.c index 4548b8b..bf8cb1c 100644 --- a/xen/common/xsplice.c +++ b/xen/common/xsplice.c @@ -590,6 +590,22 @@ static int prepare_payload(struct payload *payload, region->ex_end = (struct exception_table_entry *)(sec->load_addr + sec->sec->sh_size); sort_exception_table(region->ex, region->ex_end); + + } + sec = xsplice_elf_sec_by_name(elf, ".altinstructions"); + if ( sec ) + { + if ( !sec->sec->sh_size || + (sec->sec->sh_size % sizeof (struct alt_instr)) ) + { + dprintk(XENLOG_DEBUG, "%s%s: Wrong size of .alt_instr (exp:%lu vs %lu)!\n", + XSPLICE, elf->name, sizeof (struct alt_instr), + sec->sec->sh_size); + return -EINVAL; + } + apply_alternatives_nocheck((struct alt_instr *)sec->load_addr, + (struct alt_instr *)(sec->load_addr + + sec->sec->sh_size)); } #endif return 0; diff --git a/xen/include/asm-x86/alternative.h b/xen/include/asm-x86/alternative.h index 1056630..d50c0b5 100644 --- a/xen/include/asm-x86/alternative.h +++ b/xen/include/asm-x86/alternative.h @@ -23,6 +23,12 @@ struct alt_instr { u8 replacementlen; /* length of new instruction, <= instrlen */ }; +/* + * An variant to be used on code that can be patched without many checks. + */ +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"