From patchwork Wed Apr 27 19:27:07 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: 8962371 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 1A2899F1C1 for ; Wed, 27 Apr 2016 19:34:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AF4BE2021F for ; Wed, 27 Apr 2016 19:34:56 +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 4D65E201EC for ; Wed, 27 Apr 2016 19:34:55 +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 1avVD3-0007g6-6S; Wed, 27 Apr 2016 19:33:17 +0000 Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1avVD1-0007eb-SI for xen-devel@lists.xenproject.org; Wed, 27 Apr 2016 19:33:15 +0000 Received: from [85.158.143.35] by server-1.bemta-6.messagelabs.com id 67/52-18833-BF311275; Wed, 27 Apr 2016 19:33:15 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42LpnVTnqvtLWDH cYOpWHYvvWyYzOTB6HP5whSWAMYo1My8pvyKBNWPhitnsBcsDK5a8O8/SwHjOuYuRi0NIoINJ 4s7Ck4wQzhdGiZV7Z7NAOBsZJd5c3gvldDNKzH7TytTFyAnkFEkcf3eUtYuRg4NNwETizSpHk LCIwHJGiRdn8kDqmQVuMkqcXLOGHaRGWCBNYt0RO5AaFgFViSnH9rOA2LwCbhKvp/SzgJRICM hJLLiQDmJyAoXfrIuGWOQq8fDKd7BqCQFjib5ZfSwTGPkXMDKsYlQvTi0qSy3SNdRLKspMzyj JTczM0TU0MNPLTS0uTkxPzUlMKtZLzs/dxAgMHgYg2MG487nTIUZJDiYlUd4NfxTChfiS8lMq MxKLM+KLSnNSiw8xynBwKEnw/hFUDBcSLEpNT61Iy8wBhjFMWoKDR0mEV1sIKM1bXJCYW5yZD pE6xagoJc4bCJIQAElklObBtcFi5xKjrJQwLyPQIUI8BalFuZklqPKvGMU5GJWEef1ApvBk5p XATX8FtJgJaPHlQ7Igi0sSEVJSDYzVAp+/LsrO8eyKLle8o28UsfiE017NYz8PxoZfKssqV7m +e9aMRh5JgQU+Uydde/0+Qn32+4UmbaVlRcvnznuf/Z3pZ3dF8++42i1rHKcHOvbEhGc+eSq0 +Lzx7s2FE30X3S3edSChUnPRT4Meps4eW5vYtXbuFpcuOl+wlDrd1fDl+hLhBjslluKMREMt5 qLiRADNNUdamAIAAA== X-Env-Sender: konrad@char.us.oracle.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1461785590!5527893!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: 8.34; banners=-,-,- X-VirusChecked: Checked Received: (qmail 43438 invoked from network); 27 Apr 2016 19:33:14 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-4.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 27 Apr 2016 19:33:14 -0000 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u3RJS2Ku031102 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 27 Apr 2016 19:28:03 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.13.8/8.13.8) with ESMTP id u3RJS2dV002316 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 27 Apr 2016 19:28:02 GMT Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id u3RJS1ON005270; Wed, 27 Apr 2016 19:28:01 GMT Received: from char.us.oracle.com (/10.137.176.158) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 27 Apr 2016 12:28:01 -0700 Received: by char.us.oracle.com (Postfix, from userid 1000) id 440736A012F; Wed, 27 Apr 2016 15:27:58 -0400 (EDT) From: Konrad Rzeszutek Wilk To: konrad@kernel.org, xen-devel@lists.xenproject.org, sasha.levin@oracle.com, andrew.cooper3@citrix.com, ross.lagerwall@citrix.com, mpohlack@amazon.de Date: Wed, 27 Apr 2016 15:27:07 -0400 Message-Id: <1461785241-4481-11-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461785241-4481-1-git-send-email-konrad.wilk@oracle.com> References: <1461785241-4481-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Cc: Julien Grall , Stefano Stabellini , Keir Fraser , Jan Beulich , Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v10 10/24] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'. 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 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 Reviewed-by: Andrew Cooper Acked-by: Julien Grall [ARM] Acked-by: Jan Beulich Acked-by: Jan Beulich --- Cc: Stefano Stabellini Cc: Julien Grall Cc: Keir Fraser Cc: Jan Beulich Cc: Andrew Cooper v2: Do it using hypervisor Makefiles v3: Remove the stale linker file. Add Copyright and local definition block s/name/xen_hello_world_name/ v6: Remove the 'install', and 'uninstall' destinations. Remove xen/config.h from files. v7: Made the build target be called 'tests'. Changed the .name to have 'xen_extra_version' to be consistent with the spec. Add Julien's Ack and Andrew's Reviewed-by. v9: old_code and new_code are void, so drop the unsigned long cast and add void* - in both test-cases and document. Make tests target on ARM phony Add build dependencies on x86 build Include public/sysctl.h as CONFIG_XSPLICE may not be exposed. v10: Add Jan's Ack. Use filter to only have .o files on $LD and make xen_hello_world.o depend on config.h creation. --- .gitignore | 2 ++ docs/misc/xsplice.markdown | 37 ++++++++++++++++++++++++++++ xen/Makefile | 8 ++++-- xen/arch/arm/Makefile | 3 +++ xen/arch/x86/Makefile | 4 +++ xen/arch/x86/test/Makefile | 42 ++++++++++++++++++++++++++++++++ xen/arch/x86/test/xen_hello_world.c | 32 ++++++++++++++++++++++++ xen/arch/x86/test/xen_hello_world_func.c | 22 +++++++++++++++++ 8 files changed, 148 insertions(+), 2 deletions(-) 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/.gitignore b/.gitignore index 39eb779..4a81f43 100644 --- a/.gitignore +++ b/.gitignore @@ -246,6 +246,8 @@ xen/arch/x86/efi.lds xen/arch/x86/efi/check.efi xen/arch/x86/efi/disabled xen/arch/x86/efi/mkreloc +xen/arch/x86/test/config.h +xen/arch/x86/test/xen_hello_world.xsplice xen/arch/*/efi/boot.c xen/arch/*/efi/compat.c xen/arch/*/efi/efi.h diff --git a/docs/misc/xsplice.markdown b/docs/misc/xsplice.markdown index f5b150e..35ebc28 100644 --- a/docs/misc/xsplice.markdown +++ b/docs/misc/xsplice.markdown @@ -331,6 +331,43 @@ 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 of .xsplice.funcs + +A simple example of what a payload file can be: + +
+/* MUST be in sync with hypervisor. */  
+struct xsplice_patch_func {  
+    const char *name;  
+    void *new_addr;  
+    void *old_addr;  
+    uint32_t new_size;  
+    uint32_t old_size;  
+    uint8_t version;
+    uint8_t pad[31];  
+};  
+
+/* Our replacement function for xen_extra_version. */  
+const char *xen_hello_world(void)  
+{  
+    return "Hello World";  
+}  
+
+static unsigned char patch_this_fnc[] = "xen_extra_version";  
+
+struct xsplice_patch_func xsplice_hello_world = {  
+    .version = XSPLICE_PAYLOAD_VERSION,
+    .name = patch_this_fnc,  
+    .new_addr = xen_hello_world,  
+    .old_addr = (void *)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/xen/Makefile b/xen/Makefile index b483823..f49014b 100644 --- a/xen/Makefile +++ b/xen/Makefile @@ -39,8 +39,8 @@ dist: install build install:: include/config/auto.conf -.PHONY: build install uninstall clean distclean cscope TAGS tags MAP gtags -build install uninstall debug clean distclean cscope TAGS tags MAP gtags:: +.PHONY: build install uninstall clean distclean cscope TAGS tags MAP gtags tests +build install uninstall debug clean distclean cscope TAGS tags MAP gtags tests:: ifneq ($(XEN_TARGET_ARCH),x86_32) $(MAKE) -f Rules.mk _$@ else @@ -76,6 +76,10 @@ _install: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX) fi; \ fi +.PHONY: _tests +_tests: + $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) tests + .PHONY: _uninstall _uninstall: D=$(DESTDIR) _uninstall: T=$(notdir $(TARGET)) diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index eae5cb3..f77f8db 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -57,6 +57,9 @@ ifeq ($(CONFIG_ARM_64),y) ln -sf $(notdir $@) ../../$(notdir $@).efi endif +.PHONY: tests +tests: + $(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 f74fd2c..f8f6eeb 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -76,6 +76,9 @@ $(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/'` +.PHONY: tests +tests: + $(MAKE) -f $(BASEDIR)/Rules.mk -C test xsplice ALL_OBJS := $(BASEDIR)/arch/x86/boot/built_in.o $(BASEDIR)/arch/x86/efi/built_in.o $(ALL_OBJS) @@ -179,3 +182,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..bd81fcd --- /dev/null +++ b/xen/arch/x86/test/Makefile @@ -0,0 +1,42 @@ +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 + +XSPLICE := xen_hello_world.xsplice + +default: xsplice + +install: xsplice + $(INSTALL_DATA) $(XSPLICE) $(DESTDIR)$(DEBUG_DIR)/$(XSPLICE) +uninstall: + rm -f $(DESTDIR)$(DEBUG_DIR)/$(XSPLICE) + +.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. +# +.PHONY: config.h +config.h: OLD_CODE=$(call CODE_ADDR,$(BASEDIR)/xen-syms,xen_extra_version) +config.h: OLD_CODE_SZ=$(call CODE_SZ,$(BASEDIR)/xen-syms,xen_extra_version) +config.h: NEW_CODE_SZ=$(call CODE_SZ,$<,xen_hello_world) +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)") > $@ + +xen_hello_world.o: config.h + +.PHONY: $(XSPLICE) +$(XSPLICE): config.h xen_hello_world_func.o xen_hello_world.o + $(LD) $(LDFLAGS) -r -o $(XSPLICE) $(filter %.o,$^) + +.PHONY: xsplice +xsplice: $(XSPLICE) 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..f42b25c --- /dev/null +++ b/xen/arch/x86/test/xen_hello_world.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. + * + */ + +#include "config.h" +#include +#include + +#include + +static char hello_world_patch_this_fnc[] = "xen_extra_version"; +extern const char *xen_hello_world(void); + +struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = { + .version = XSPLICE_PAYLOAD_VERSION, + .name = hello_world_patch_this_fnc, + .new_addr = xen_hello_world, + .old_addr = (void *)OLD_CODE, + .new_size = NEW_CODE_SZ, + .old_size = OLD_CODE_SZ, +}; + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ 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..1ad002a --- /dev/null +++ b/xen/arch/x86/test/xen_hello_world_func.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. + * + */ + +#include + +/* Our replacement function for xen_extra_version. */ +const char *xen_hello_world(void) +{ + return "Hello World"; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */