From patchwork Fri Feb 12 18:05:46 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: 8295001 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 7157A9F6E4 for ; Fri, 12 Feb 2016 18:09:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A64292042A for ; Fri, 12 Feb 2016 18:09:41 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2D1CA2041F for ; Fri, 12 Feb 2016 18:09:39 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aUI7N-000661-Cc; Fri, 12 Feb 2016 18:06:57 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aUI7J-000634-3j for xen-devel@lists.xenproject.org; Fri, 12 Feb 2016 18:06:53 +0000 Received: from [85.158.137.68] by server-1.bemta-3.messagelabs.com id BB/73-02745-C3F1EB65; Fri, 12 Feb 2016 18:06:52 +0000 X-Env-Sender: konrad@char.us.oracle.com X-Msg-Ref: server-14.tower-31.messagelabs.com!1455300410!22063752!1 X-Originating-IP: [141.146.126.69] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTQxLjE0Ni4xMjYuNjkgPT4gMjc3MjE4\n X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 36866 invoked from network); 12 Feb 2016 18:06:51 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-14.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 12 Feb 2016 18:06:51 -0000 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u1CI6hGu025055 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Feb 2016 18:06:44 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0022.oracle.com (8.14.4/8.13.8) with ESMTP id u1CI6gSi006022 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Fri, 12 Feb 2016 18:06:43 GMT Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0122.oracle.com (8.13.8/8.13.8) with ESMTP id u1CI6gON024666; Fri, 12 Feb 2016 18:06:42 GMT Received: from char.us.oracle.com (/10.137.176.158) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 12 Feb 2016 10:06:42 -0800 Received: by char.us.oracle.com (Postfix, from userid 1000) id E5D3F6A4BF8; Fri, 12 Feb 2016 13:06:39 -0500 (EST) From: Konrad Rzeszutek Wilk To: xen-devel@lists.xenproject.org, andrew.cooper3@citrix.com, konrad@kernel.org, mpohlack@amazon.de, ross.lagerwall@citrix.com, sasha.levin@citrix.com, jinsong.liu@alibaba-inc.com, Ian Jackson , Stefano Stabellini , Ian Campbell , Wei Liu , Stefano Stabellini , Keir Fraser , Jan Beulich , xen-devel@lists.xen.org Date: Fri, 12 Feb 2016 13:05:46 -0500 Message-Id: <1455300361-13092-9-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1455300361-13092-1-git-send-email-konrad.wilk@oracle.com> References: <1455300361-13092-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: userv0022.oracle.com [156.151.31.74] Cc: Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v3 08/23] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'. (v2) X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org 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 This change demonstrates how to generate an xSplice ELF payload. The idea here is that we want to patch in the hypervisor the 'xen_version_extra' function with an function that will return 'Hello World'. The 'xl info | grep extraversion' will reflect the new value after the patching. To generate this ELF payload file we need: - C code of the new code (xen_hello_world_func.c). - C code generating the .xsplice.funcs structure (xen_hello_world.c) - The address of the old code (xen_extra_version). We retrieve it by using 'nm --defined' on xen-syms. - The size of the new and old code for which we use nm --defined -S on our code and xen-syms respectively. There are two C files and one header files generated during build. One could make this one C file if the size of the newly patched function size was known in advance (or an random value was choosen). There is also a strict order of compiling: 1) xen_hello_world_func.c 2) config.h - extract the size of the new function, the old function and the old function address. 3) xen_hello_world.c - which contains the .xsplice.funcs structure. 4) Link the object files in an xen_hello_world.xsplice file. The use-case is simple: $xen-xsplice load /usr/lib/debug/xen_hello_world.xsplice $xen-xsplice list ID | status ----------------------------------------+------------ xen_hello_world APPLIED $xl info | grep extra xen_extra : Hello World $xen-xsplice revert xen_hello_world Performing revert: completed $xen-xsplice unload xen_hello_world Performing unload: completed $xl info | grep extra xen_extra : -unstable Signed-off-by: Konrad Rzeszutek Wilk --- docs/misc/xsplice.markdown | 36 +++++++++++++++++++++++ tools/misc/xsplice.lds | 11 +++++++ xen/Makefile | 2 ++ xen/arch/arm/Makefile | 4 +++ xen/arch/x86/Makefile | 6 ++++ xen/arch/x86/test/Makefile | 50 ++++++++++++++++++++++++++++++++ xen/arch/x86/test/xen_hello_world.c | 15 ++++++++++ xen/arch/x86/test/xen_hello_world_func.c | 8 +++++ 8 files changed, 132 insertions(+) create mode 100644 tools/misc/xsplice.lds create mode 100644 xen/arch/x86/test/Makefile create mode 100644 xen/arch/x86/test/xen_hello_world.c create mode 100644 xen/arch/x86/test/xen_hello_world_func.c diff --git a/docs/misc/xsplice.markdown b/docs/misc/xsplice.markdown index 9a95243..0a5b87b 100644 --- a/docs/misc/xsplice.markdown +++ b/docs/misc/xsplice.markdown @@ -321,11 +321,47 @@ size. When applying the patch the hypervisor iterates over each `xsplice_patch_func` structure and the core code inserts a trampoline at `old_addr` to `new_addr`. +The `new_addr` is altered when the ELF payload is loaded. When reverting a patch, the hypervisor iterates over each `xsplice_patch_func` and the core code copies the data from the undo buffer (private internal copy) to `old_addr`. +### Example + +A simple example of what a payload file can be: + +
+/* MUST be in sync with hypervisor. */  
+struct xsplice_patch_func {  
+    const char *name;  
+    uint64_t new_addr;  
+    uint64_t old_addr;  
+    uint32_t new_size;  
+    uint32_t old_size;  
+    uint8_t pad[32];  
+};  
+
+/* Our replacement function for xen_extra_version. */  
+const char *xen_hello_world(void)  
+{  
+    return "Hello World";  
+}  
+
+static unsigned char name[] = "xen_hello_world";  
+
+struct xsplice_patch_func xsplice_hello_world = {  
+    .name = name,  
+    .new_addr = (unsigned long)(xen_hello_world),  
+    .old_addr = 0xffff82d08013963c, /* Extracted from xen-syms. */  
+    .new_size = 13, /* To be be computed by scripts. */  
+    .old_size = 13, /* -----------""---------------  */  
+} __attribute__((__section__(".xsplice.funcs")));  
+
+
+ +Code must be compiled with -fPIC. + ## Hypercalls We will employ the sub operations of the system management hypercall (sysctl). diff --git a/tools/misc/xsplice.lds b/tools/misc/xsplice.lds new file mode 100644 index 0000000..f52eb8c --- /dev/null +++ b/tools/misc/xsplice.lds @@ -0,0 +1,11 @@ +OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") +OUTPUT_ARCH(i386:x86-64) +ENTRY(xsplice_hello_world) +SECTIONS +{ + /* The hypervisor expects ".xsplice.func", so change + * the ".data.xsplice_hello_world" to it. */ + + .xsplice.funcs : { *(*.xsplice_hello_world) } + +} diff --git a/xen/Makefile b/xen/Makefile index 5d98bcb..f702525 100644 --- a/xen/Makefile +++ b/xen/Makefile @@ -75,6 +75,7 @@ _install: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX) echo 'EFI installation only partially done (EFI_VENDOR not set)' >&2; \ fi; \ fi + $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) install .PHONY: _uninstall _uninstall: D=$(DESTDIR) @@ -92,6 +93,7 @@ _uninstall: rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).efi rm -f $(D)$(EFI_DIR)/$(T).efi rm -f $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi + $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) uninstall .PHONY: _debug _debug: diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index f144c14..35ba293 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -56,6 +56,10 @@ ifeq ($(CONFIG_ARM_64),y) ln -sf $(notdir $@) ../../$(notdir $@).efi endif +install: + +uninstall: + $(TARGET).axf: $(TARGET)-syms # XXX: VE model loads by VMA so instead of # making a proper ELF we link with LMA == VMA and adjust crudely diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index f7d3e39..51d12ac 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -74,7 +74,12 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ $(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32 ./boot/mkelf32 $(TARGET)-syms $(TARGET) 0x100000 \ `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` + $(MAKE) -f $(BASEDIR)/Rules.mk -C test +install: + $(MAKE) -f $(BASEDIR)/Rules.mk -C test install +uninstall: + $(MAKE) -f $(BASEDIR)/Rules.mk -C test uninstall ALL_OBJS := $(BASEDIR)/arch/x86/boot/built_in.o $(BASEDIR)/arch/x86/efi/built_in.o $(ALL_OBJS) @@ -178,3 +183,4 @@ clean:: rm -f $(BASEDIR)/.xen-syms.[0-9]* boot/.*.d rm -f $(BASEDIR)/.xen.efi.[0-9]* efi/*.o efi/.*.d efi/*.efi efi/disabled efi/mkreloc rm -f boot/reloc.S boot/reloc.lnk boot/reloc.bin + $(MAKE) -f $(BASEDIR)/Rules.mk -C test clean diff --git a/xen/arch/x86/test/Makefile b/xen/arch/x86/test/Makefile new file mode 100644 index 0000000..3fe951d --- /dev/null +++ b/xen/arch/x86/test/Makefile @@ -0,0 +1,50 @@ +include $(XEN_ROOT)/Config.mk + +CODE_ADDR=$(shell nm --defined $(1) | grep $(2) | awk '{print "0x"$$1}') +CODE_SZ=$(shell nm --defined -S $(1) | grep $(2) | awk '{ print "0x"$$2}') + +.PHONY: default +ifdef CONFIG_XSPLICE + +XSPLICE := xen_hello_world.xsplice + +default: xsplice + +install: xsplice + $(INSTALL_DATA) $(XSPLICE) $(DESTDIR)$(DEBUG_DIR)/$(XSPLICE) +uninstall: + rm -f $(DESTDIR)$(DEBUG_DIR)/$(XSPLICE) +else +default: +install: +uninstall: +endif + +.PHONY: clean +clean:: + rm -f *.o .*.o.d $(XSPLICE) config.h + +# +# To compute these values we need the binary files: xen-syms +# and xen_hello_world_func.o to be already compiled. +# +# We can be assured that xen-syms is already built as we are +# the last entry in the build target. +# +.PHONY: config.h +config.h: OLD_CODE=$(call CODE_ADDR,$(BASEDIR)/xen-syms,xen_extra_version) +config.h: OLD_CODE_SZ=$(call CODE_SZ,$<,xen_hello_world) +config.h: NEW_CODE_SZ=$(call CODE_SZ,$(BASEDIR)/xen-syms,xen_extra_version) +config.h: xen_hello_world_func.o + (set -e; \ + echo "#define NEW_CODE_SZ $(NEW_CODE_SZ)"; \ + echo "#define OLD_CODE_SZ $(OLD_CODE_SZ)"; \ + echo "#define OLD_CODE $(OLD_CODE)") > $@ + +.PHONY: xsplice +xsplice: config.h + # Need to have these done in sequential order + $(MAKE) -f $(BASEDIR)/Rules.mk xen_hello_world_func.o + $(MAKE) -f $(BASEDIR)/Rules.mk xen_hello_world.o + $(LD) $(LDFLAGS) -r -o $(XSPLICE) xen_hello_world_func.o xen_hello_world.o + diff --git a/xen/arch/x86/test/xen_hello_world.c b/xen/arch/x86/test/xen_hello_world.c new file mode 100644 index 0000000..6a1775b --- /dev/null +++ b/xen/arch/x86/test/xen_hello_world.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include "config.h" + +static char name[] = "xen_hello_world"; +extern const char *xen_hello_world(void); + +struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = { + .name = name, + .new_addr = (unsigned long)(xen_hello_world), + .old_addr = OLD_CODE, + .new_size = NEW_CODE_SZ, + .old_size = OLD_CODE_SZ, +}; diff --git a/xen/arch/x86/test/xen_hello_world_func.c b/xen/arch/x86/test/xen_hello_world_func.c new file mode 100644 index 0000000..95ffcbd --- /dev/null +++ b/xen/arch/x86/test/xen_hello_world_func.c @@ -0,0 +1,8 @@ +#include +#include + +/* Our replacement function for xen_extra_version. */ +const char *xen_hello_world(void) +{ + return "Hello World"; +}