xSplice v1 design and implementation.
PATCH 1/1 xSplice v1 design and implementation.
Hey!
Changelog:
v8: since http://lists.xen.org/archives/html/xen-devel/2016-04/msg01873.html
- Posting the _RIGHT_ set of patches.
v7: http://lists.xen.org/archives/html/xen-devel/2016-04/msg01476.html
- Ingested newer version of x86/mm: Introduce modify_xen_mappings()
- Implemented faster symbol table lookup (NEW)
- Carried out tests on large CPU machine (240CPUs)
- Made the struct xsplice_patch_func work on ARM32 and changed its size (64bit it is 64bytes, 32bit is 52 bytes - and the offset is different too)
- Changed a bunch of printk to dprintk, adjusted XENLOG_ levels.
- Resurrected the XENVER_build_id.
- Reverted VERSION_op.
v6: http://lists.xen.org/archives/html/xen-devel/2016-04/msg00871.html
- Acted on all comments from Andrew, Julien, and Jan.
- Got help from Andrew on one of them over the weekend.
- Dropped: xsplice: Add .xsplice.hooks functions and test-case,
xsplice: Add support for shadow variables.
v5: http://lists.xen.org/archives/html/xen-devel/2016-03/msg03286.html
- Acted at all comments from Jan.
v4: http://lists.xen.org/archives/html/xen-devel/2016-03/msg01776.html
- Lots of review. Lots of rework. Some patches checked in.
v3: http://www.gossamer-threads.com/lists/xen/devel/418262
and
http://lists.xenproject.org/archives/html/xen-devel/2016-02/msg04106.html - Act on all reviews.
- Redo the flow of patches
v2: http://lists.xen.org/archives/html/xen-devel/2016-01/msg01597.html
- Updated code/docs/design with review comments.
- Make xen also have an PT_NOTE
- Added more of Ross's patches
- Combined build-id patchset with this.
(since the RFC and the Seattle Xen presentation)
- Finished off some of the work around the build-id.
- Settled on the preemption mechanism.
- Cleaned the patches a lot up, broke them up to easy
review for maintainers.
v1: http://lists.xenproject.org/archives/html/xen-devel/2015-09/msg02116.html
- Put all the design comments in the code
Prototype: http://lists.xenproject.org/archives/html/xen-devel/2015-10/msg02595.html
[Posting by Ross]
- Took all reviews into account.
- Redid the patches
*Tools Maintainers*
Since v7, two new patches, please review:
XENVER_build_id/libxc: Provide ld-embedded build-id
libxl: info: Display build_id of the hypervisor.
*Maintainers*
Legend:
* - See below R - Reviewed R+ - Reviewed by two folks A - Acked by maintainer of the area (hypervisor or toolstack)
* Revert "libxc/libxl/python/xenstat/ocaml: Use new
* Revert "HYPERCALL_version_op. New hypercall mirroring
A xsplice: Design document
R xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
AR libxc: Implementation of XEN_XSPLICE_op in libxc
AR xen-xsplice: Tool to manipulate xsplice payloads
AR arm/x86: Use struct virtual_region to do bug, symbol, and (x86) exception tables lookup.
[Julien Acked the ARM part]
AR arm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type
[Julien Acked the ARM part]
R+ x86/mm: Introduce modify_xen_mappings()
R xsplice: Add helper elf routines
R xsplice: Implement payload loading
* R xsplice: Implement support for applying/reverting/replacing patches.
R x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'.
R xsplice,symbols: Implement symbol name resolution on address.
* xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
R x86, xsplice: Print payload's symbol name and payload name in backtraces
R xsplice: Add support for bug frames.
R xsplice: Add support for exception tables.
R xsplice: Add support for alternatives
AR build_id: Provide ld-embedded build-ids
[Julien Acked the ARM part]
AR xsplice: Print build_id in keyhandler and on bootup.
* XENVER_build_id/libxc: Provide ld-embedded build-id
* libxl: info: Display build_id of the hypervisor.
R xsplice: Stacking build-id dependency checking.
R xsplice/xen_replace_world: Test-case for XSPLICE_ACTION_REPLACE
R xsplice: Prevent duplicate payloads from being loaded.
R MAINTAINERS/xsplice: Add myself and Ross as the maintainers.Since v7, the
[PATCH v8.1 01/27] Revert "libxc/libxl/python/xenstat/ocaml: Use new [PATCH v8.1 02/27] Revert "HYPERCALL_version_op. New hypercall mirroring
[PATCH v8.1 15/27] xsplice, symbols: Implement fast symbol names -> [PATCH v8.1 22/27] XENVER_build_id/libxc: Provide ld-embedded build-id [PATCH v8.1 23/27] libxl: info: Display build_id of the hypervisor.
haven't been reviewed (they are new). Thought honestly the reverts don't really need a review.
Julien, Stefano, I dropped Julien's Ack on
[PATCH v8.1 11/27] xsplice: Implement payload loading
as Jan's expressed a desire not to have BITS_PER_LONG exposed in the public headers. I've put CONFIG_ARM_32 in there. On IRC Stefano was OK (relucantly) with it.
Patchset has been tested on ARM (tweaking the Kconfig to build xSplice and then booting and using it under ARM CubieTruck - granted there is no patching yet but the hypercalls work), ARM64 (only built it), and x86 (legacy and EFI)
*VERSION_OP being reverted?*
Ian (thank you taking the stressful role of tiebreaker) pointed out that at the end of the day - less code from a maintaince perspective is easier. Hence the revert. We can always revisit this in Xen 4.8 :-)
*Are there any TODOs left from v5,v6,v7 reviews?*
Just how the sysctl.h definition for 'struct xsplice_patch_func' should be:
#define XSPLICE_PAYLOAD_VERSION 1
/*
* .xsplice.funcs structure layout defined in the Payload format
* section in the xSplice design document.
*
* The size should be exactly 64 bytes (64) or 52 bytes (32). *
* We guard this with __XEN__ as toolstacks do not need to use it. */
#ifdef __XEN__
struct xsplice_patch_func {
const char *name; /* Name of function to be patched. */
#if CONFIG_ARM_32
uint32_t new_addr;
uint32_t old_addr;#else
uint64_t new_addr;
uint64_t old_addr; /* Can be zero and name will be looked up. */
#endif
uint32_t new_size;
uint32_t old_size;
uint8_t version; /* MUST be XSPLICE_PAYLOAD_VERSION. */
uint8_t pad[31]; /* MUST be zero filled. */
};
typedef struct xsplice_patch_func xsplice_patch_func_t; #endif
Please note the __XEN__ so even if toolstack does include the public/sysctl.h it won't find it the structure.
With this I am able to compile early-test-cases-prototype-work xSplice on ARM32.
*What is xSplice?*
A mechanism to binarily patch the running hypervisor with new opcodes that have come about due to primarily security updates.
*What will this patchset do once I've it*
Patch the hypervisor.
*Why are you emailing me?*
Please please review as many patches as possible.
*OK, what do you have?*
They are located at a git tree:
git://xenbits.xen.org/people/konradwilk/xen.git xsplice.v8.drop_VERSION_op
(Copying from Ross's email):
Much of the work is implementing a basic version of the Linux kernel module loader. The code:
* Loading of xSplice ELF payloads.
* Copying allocated sections into a new executable region of memory.
* Resolving symbols.
* Applying relocations.
* Patching of altinstructions.
* Special handling of bug frames and exception tables.
* Unloading of xSplice ELF payloads.
* Compiling a sample xSplice ELF payload
* Resolving symbols
* Using build-id dependencies
* Support for shadow variable framework
* Support for executing ELF payload functions on load/unload.
The other main bit of this work is applying and reverting the patches safely. As implemented, the code is patched with each CPU waiting in the return-to-guest path (i.e. with no stack) or on the cpu-idle path which appears to be the safest way of patching. While it is safe we should still (in the next wave of patches) to verify to not patch cetain critical sections (say the code doing the patching)
All of the following should work:
* Applying patches safely.
* Reverting patches safely.
* Replacing patches safely (e.g. reverting any applied patches and applying a new patch).
* Bug frames as part of modules. This means adding or
changing WARN, ASSERT, BUG, and run_in_exception_handler works correctly. Line number only changes _are ignored_.
* Exception tables as part of modules. E.g. wrmsr_safe and copy_to_user work correctly when used in a patch module.
* Stacking of patches on top of each other
* Resolving symbols (even of patches)
*Limitations*
The above is enough to fully implement an update system where multiple source patches are combined (using combinediff) and built into a single binary which then atomically replaces any existing loaded patches
(this is why Ross added a REPLACE operation). This is the approach used by kPatch and kGraft.
Multiple completely independent patches can also be loaded but unexpected interactions may occur.
As it stands, the patches are statically linked which means that independent patches cannot be linked against one another (e.g. if one introduces a new symbol). Using the combinediff approach above fixes this.
Backtraces containing functions from a patch module do not show the symbol name.
There is no checking that a patch which is loaded is built for the correct hypervisor (need to use build-id).
Binary patching works at the function level.
*Testing*
You can use the example code included in this patchset:
# xl info | grep extra xen_extra : -unstable # xen-xsplice load /usr/lib/debug/xen_hello_world.xsplice Uploading /usr/lib/debug/xen_hello_world.xsplice (2071 bytes) Performing check: completed Performing apply:. completed # 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
Or you can use git://xenbits.xen.org/people/konradwilk/xsplice-build-tools.git
which generates the ELF payloads.
This link has a nice description of how to use the tool:
http://lists.xenproject.org/archives/html/xen-devel/2015-10/msg02595.html
.gitignore | 5 + Config.mk | 12 + MAINTAINERS | 10 + docs/misc/xsplice.markdown | 1116 +++++++++++++++++++ tools/flask/policy/policy/modules/xen/xen.te | 9 +- tools/libxc/include/xenctrl.h | 94 +- tools/libxc/xc_core.c | 35 +- tools/libxc/xc_dom_boot.c | 12 +- tools/libxc/xc_domain.c | 3 +- tools/libxc/xc_misc.c | 355 ++++++ tools/libxc/xc_private.c | 60 +- tools/libxc/xc_private.h | 18 +- tools/libxc/xc_resume.c | 3 +- tools/libxc/xc_sr_save.c | 9 +- tools/libxc/xg_save_restore.h | 6 +- tools/libxl/libxl.c | 108 +- tools/libxl/libxl.h | 6 + tools/libxl/libxl_types.idl | 1 + tools/libxl/xl_cmdimpl.c | 1 + tools/misc/Makefile | 4 + tools/misc/xen-xsplice.c | 463 ++++++++ tools/ocaml/libs/xc/xenctrl_stubs.c | 39 +- tools/python/xen/lowlevel/xc/xc.c | 30 +- tools/xenstat/libxenstat/src/xenstat.c | 12 +- tools/xentrace/xenctx.c | 3 +- xen/Makefile | 8 +- xen/arch/arm/Makefile | 5 +- xen/arch/arm/kernel.c | 2 +- xen/arch/arm/mm.c | 2 +- xen/arch/arm/setup.c | 4 + xen/arch/arm/traps.c | 40 +- xen/arch/arm/xen.lds.S | 15 +- xen/arch/arm/xsplice.c | 87 ++ xen/arch/x86/Makefile | 58 +- xen/arch/x86/alternative.c | 42 +- xen/arch/x86/boot/mkelf32.c | 129 ++- xen/arch/x86/domain.c | 2 + xen/arch/x86/extable.c | 41 +- xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/mm.c | 77 +- xen/arch/x86/setup.c | 28 +- xen/arch/x86/test/Makefile | 85 ++ xen/arch/x86/test/xen_bye_world.c | 34 + xen/arch/x86/test/xen_bye_world_func.c | 24 + xen/arch/x86/test/xen_hello_world.c | 33 + xen/arch/x86/test/xen_hello_world_func.c | 38 + xen/arch/x86/test/xen_replace_world.c | 34 + xen/arch/x86/test/xen_replace_world_func.c | 24 + xen/arch/x86/traps.c | 45 +- xen/arch/x86/x86_64/compat/entry.S | 2 - xen/arch/x86/x86_64/entry.S | 2 - xen/arch/x86/xen.lds.S | 28 + xen/arch/x86/xsplice.c | 264 +++++ xen/common/Kconfig | 28 + xen/common/Makefile | 3 + xen/common/compat/kernel.c | 2 - xen/common/kernel.c | 226 ++-- xen/common/symbols-dummy.c | 6 + xen/common/symbols.c | 118 +- xen/common/sysctl.c | 6 + xen/common/version.c | 70 ++ xen/common/virtual_region.c | 148 +++ xen/common/vmap.c | 202 ++-- xen/common/vsprintf.c | 19 +- xen/common/xsplice.c | 1541 ++++++++++++++++++++++++++ xen/common/xsplice_elf.c | 495 +++++++++ xen/drivers/acpi/osl.c | 2 +- xen/include/asm-x86/alternative.h | 4 + xen/include/asm-x86/current.h | 10 +- xen/include/asm-x86/uaccess.h | 2 + xen/include/public/arch-arm.h | 2 - xen/include/public/sysctl.h | 193 ++++ xen/include/public/version.h | 82 +- xen/include/public/xen.h | 1 - xen/include/xen/elfstructs.h | 7 + xen/include/xen/hypercall.h | 4 - xen/include/xen/mm.h | 2 + xen/include/xen/symbols.h | 11 + xen/include/xen/version.h | 1 + xen/include/xen/virtual_region.h | 47 + xen/include/xen/vmap.h | 21 +- xen/include/xen/xsplice.h | 146 +++ xen/include/xen/xsplice_elf.h | 58 + xen/include/xsm/dummy.h | 21 - xen/include/xsm/xsm.h | 6 - xen/tools/symbols.c | 93 +- xen/xsm/dummy.c | 1 - xen/xsm/flask/hooks.c | 40 +- xen/xsm/flask/policy/access_vectors | 25 +- 89 files changed, 6496 insertions(+), 715 deletions(-)
Andrew Cooper (1):
x86/mm: Introduce modify_xen_mappings()
Konrad Rzeszutek Wilk (17):
Revert "libxc/libxl/python/xenstat/ocaml: Use new XEN_VERSION hypercall" Revert "HYPERCALL_version_op. New hypercall mirroring XENVER_ but sane."
xsplice: Design document
xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
libxc: Implementation of XEN_XSPLICE_op in libxc
xen-xsplice: Tool to manipulate xsplice payloads
arm/x86: Use struct virtual_region to do bug, symbol, and (x86) exception tables lookup. arm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'. xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
build_id: Provide ld-embedded build-ids
xsplice: Print build_id in keyhandler and on bootup. XENVER_build_id/libxc: Provide ld-embedded build-id
libxl: info: Display build_id of the hypervisor.
xsplice: Stacking build-id dependency checking. xsplice/xen_replace_world: Test-case for XSPLICE_ACTION_REPLACE MAINTAINERS/xsplice: Add myself and Ross as the maintainers.
Ross Lagerwall (9):
xsplice: Add helper elf routines
xsplice: Implement payload loading
xsplice: Implement support for applying/reverting/replacing patches. xsplice,symbols: Implement symbol name resolution on address. x86, xsplice: Print payload's symbol name and payload name in backtraces
xsplice: Add support for bug frames.
xsplice: Add support for exception tables.
xsplice: Add support for alternatives
xsplice: Prevent duplicate payloads from being loaded.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 1/27 Revert "libxc/libxl/python/xenstat/ocaml: Use new XEN_VERSION hypercall"
This reverts commit d275ec9ca8a86f7c9c213f3551194d471ce90fbd.
As we prefer to still utilize the old XENVER_ hypercall.
--- tools/libxc/include/xenctrl.h | 32 +------------- tools/libxc/xc_core.c | 35 ++++++++-------- tools/libxc/xc_dom_boot.c | 12 +----- tools/libxc/xc_domain.c | 3 +- tools/libxc/xc_private.c | 53 +++++++++++++++++++---- tools/libxc/xc_private.h | 7 ++-- tools/libxc/xc_resume.c | 3 +- tools/libxc/xc_sr_save.c | 9 ++-- tools/libxc/xg_save_restore.h | 6 +-- tools/libxl/libxl.c | 77 +++++++++++++--------------------- tools/ocaml/libs/xc/xenctrl_stubs.c | 39 ++++++++++------- tools/python/xen/lowlevel/xc/xc.c | 30 ++++++------- tools/xenstat/libxenstat/src/xenstat.c | 12 +++--- tools/xentrace/xenctx.c | 3 +- 14 files changed, 146 insertions(+), 175 deletions(-)
tools/libxc/include/xenctrl.h +1 -31
@@ -1536,37 +1536,7 @@ int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask);int xc_domctl (xc_interface *xch,struct xen_domctl *domctl);int xc_sysctl (xc_interface *xch,struct xen_sysctl *sysctl); -/** -* This function returns the size of buffer to be allocated for -* the cmd. The cmd are XEN_VERSION_*. -*/ -ssize_t xc_version_len (xc_interface *xch,unsigned int cmd); - -/** -* This function retrieves the information from the version_op hypercall. -* The len is the size of the arg buffer. If arg is NULL, will not -* perform hypercall - instead will just return the size of arg -* buffer that is needed. -* -* Note that prior to Xen 4.7 this would return 0 for success and -* negative value (-1) for error (with the error in errno). In Xen 4.7 -* and later for success it will return an positive value which is the -* number of bytes copied in arg. -* -* It can also return -1 with various errno values: -* - EPERM - not permitted. -* - ENOBUFS - the len was to short, output in arg truncated. -* - ENOSYS - not implemented. -* -* @parm xch a handle to an open hypervisor interface -* @parm cmd XEN_VERSION_* value -* @param arg Pointer to xen_version_op_buf_t or xen_version_op_val_t -* @param len Size of arg -* @return size of bytes copied in arg on success, -1 on failure (and -* errno will contain the error) -* -*/ -int xc_version (xc_interface *xch,unsigned int cmd,void *arg,size_t len); +int xc_version (xc_interface *xch,int cmd,void *arg);int xc_flask_op (xc_interface *xch,xen_flask_op_t *op);
tools/libxc/xc_core.c +17 -18
@@ -270,43 +270,42 @@ elfnote_fill_xen_version(xc_interface *xch, *xen_version) {int rc; -xen_version_op_val_t val =0 ;memset (xen_version,0 ,sizeof (*xen_version)); - rc =xc_version (xch, XEN_VERSION_version, &val,sizeof (val)); + rc =xc_version (xch, XENVER_version,NULL );if ( rc <0 )return rc; - xen_version->major_version = val >>16 ; - xen_version->minor_version = val & ((1 <<16 ) -1 ); + xen_version->major_version = rc >>16 ; + xen_version->minor_version = rc & ((1 <<16 ) -1 ); - rc =xc_version (xch, XEN_VERSION_extraversion, - xen_version->extra_version, -sizeof (xen_version->extra_version)); + rc =xc_version (xch, XENVER_extraversion, + &xen_version->extra_version);if ( rc <0 )return rc; - rc =xc_version (xch, XEN_VERSION_capabilities, - xen_version->capabilities, -sizeof (xen_version->capabilities)); + rc =xc_version (xch, XENVER_compile_info, + &xen_version->compile_info);if ( rc <0 )return rc; - rc =xc_version (xch, XEN_VERSION_changeset, xen_version->changeset, -sizeof (xen_version->changeset)); + rc =xc_version (xch, + XENVER_capabilities, &xen_version->capabilities);if ( rc <0 )return rc; - rc =xc_version (xch, XEN_VERSION_platform_parameters, - &xen_version->platform_parameters, -sizeof (xen_version->platform_parameters)); + rc =xc_version (xch, XENVER_changeset, &xen_version->changeset);if ( rc <0 )return rc; - val =0 ; - rc =xc_version (xch, XEN_VERSION_pagesize, &val,sizeof (val)); + rc =xc_version (xch, XENVER_platform_parameters, + &xen_version->platform_parameters);if ( rc <0 )return rc; - xen_version->pagesize = val; + + rc =xc_version (xch, XENVER_pagesize,NULL ); +if ( rc <0 ) +return rc; + xen_version->pagesize = rc;return 0 ; }
tools/libxc/xc_dom_boot.c +2 -10
@@ -112,19 +112,11 @@ int xc_dom_compat_check(struct xc_dom_image *dom)int xc_dom_boot_xen_init (struct xc_dom_image *dom,xc_interface *xch,domid_t domid) { -xen_version_op_val_t val =0 ; - -if (xc_version (xch, XEN_VERSION_version, &val,sizeof (val)) <0 ) - { -xc_dom_panic (xch,XC_INTERNAL_ERROR ,"can't get Xen version!" ); -return -1 ; - } - dom->xen_version = val; dom->xch = xch; dom->guest_domid = domid; -if (xc_version (xch, XEN_VERSION_capabilities, dom->xen_caps, -sizeof (dom->xen_caps)) <0 ) + dom->xen_version =xc_version (xch, XENVER_version,NULL ); +if (xc_version (xch, XENVER_capabilities, &dom->xen_caps) <0 ) {xc_dom_panic (xch,XC_INTERNAL_ERROR ,"can't get xen capabilities" );return -1 ;
tools/libxc/xc_domain.c +1 -2
@@ -2084,8 +2084,7 @@ int xc_map_domain_meminfo(xc_interface *xch, int domid, _di.guest_width = minfo->guest_width;/* Get page table levels (see get_platform_info() in xg_save_restore.h */ -if (xc_version (xch, XEN_VERSION_capabilities, xen_caps, -sizeof (xen_caps)) <0 ) +if (xc_version (xch, XENVER_capabilities, &xen_caps) ) {PERROR ("Could not get Xen capabilities (for page table levels)" );return -1 ;
tools/libxc/xc_private.c +44 -9
67 lines not on this page · raw message
tools/libxc/xc_private.h +3 -4
@@ -214,12 +214,11 @@ void xc__hypercall_buffer_cache_release(xc_interface *xch);* Hypercall interfaces. */ -static inline long do_version_op (xc_interface *xch,int cmd, -xc_hypercall_buffer_t *dest,ssize_t len) +static inline int do_xen_version (xc_interface *xch,int cmd,xc_hypercall_buffer_t *dest) {DECLARE_HYPERCALL_BUFFER_ARGUMENT (dest); -return xencall3 (xch->xcall, __HYPERVISOR_version_op, - cmd,HYPERCALL_BUFFER_AS_ARG (dest), len); +return xencall2 (xch->xcall, __HYPERVISOR_xen_version, + cmd,HYPERCALL_BUFFER_AS_ARG (dest)); }static inline int do_physdev_op (xc_interface *xch,int cmd,void *op,size_t len)
tools/libxc/xc_resume.c +1 -2
@@ -56,8 +56,7 @@ static int modify_returncode(xc_interface *xch, uint32_t domid)return 0 ;/* HVM guests have host address width. */ -if (xc_version (xch, XEN_VERSION_capabilities, caps, -sizeof (caps)) <0 ) +if (xc_version (xch, XENVER_capabilities, &caps) !=0 ) {PERROR ("Could not get Xen capabilities" );return -1 ;
tools/libxc/xc_sr_save.c +4 -5
@@ -9,7 +9,7 @@static int write_headers (struct xc_sr_context *ctx,uint16_t guest_type) {xc_interface *xch = ctx->xch; -xen_version_op_val_t xen_version; +int32_t xen_version =xc_version (xch, XENVER_version,NULL );struct xc_sr_ihdr ihdr = { .marker =IHDR_MARKER ,
@@ -21,16 +21,15 @@ static int write_headers(struct xc_sr_context *ctx, uint16_t guest_type) { .type = guest_type, .page_shift = XC_PAGE_SHIFT, + .xen_major = (xen_version >> 16) & 0xffff, + .xen_minor = (xen_version) & 0xffff, }; -if (xc_version (xch, XEN_VERSION_version, &xen_version, -sizeof (xen_version)) <0 ) +if ( xen_version <0 ) {PERROR ("Unable to obtain Xen Version" );return -1 ; } - dhdr.xen_major = (xen_version >>16 ) &0xffff ; - dhdr.xen_minor = (xen_version) &0xffff ;if (write_exact (ctx->fd, &ihdr,sizeof (ihdr)) ) {
tools/libxc/xg_save_restore.h +2 -4
@@ -57,12 +57,10 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom,xen_capabilities_info_t xen_caps ="" ;xen_platform_parameters_t xen_params; -if (xc_version (xch, XEN_VERSION_platform_parameters, &xen_params, -sizeof (xen_params)) <0 ) +if (xc_version (xch, XENVER_platform_parameters, &xen_params) !=0 )return 0 ; -if (xc_version (xch, XEN_VERSION_capabilities, xen_caps, -sizeof (xen_caps)) <0 ) +if (xc_version (xch, XENVER_capabilities, &xen_caps) !=0 )return 0 ;if (xc_maximum_ram_page (xch, max_mfn))
tools/libxl/libxl.c +28 -49
99 lines not on this page · raw message
tools/ocaml/libs/xc/xenctrl_stubs.c +23 -16
@@ -853,21 +853,21 @@ CAMLprim value stub_xc_version_version(value xch)CAMLparam1 (xch);CAMLlocal1 (result);xen_extraversion_t extra; -xen_version_op_val_t packed; +long packed;int retval;caml_enter_blocking_section (); - retval =xc_version (_H (xch), XEN_VERSION_version, &packed,sizeof (packed)); + packed =xc_version (_H (xch), XENVER_version,NULL );caml_leave_blocking_section (); -if (retval <0 ) +if (packed <0 )failwith_xc (_H (xch));caml_enter_blocking_section (); - retval =xc_version (_H (xch), XEN_VERSION_extraversion, &extra,sizeof (extra)); + retval =xc_version (_H (xch), XENVER_extraversion, &extra);caml_leave_blocking_section (); -if (retval <0 ) +if (retval)failwith_xc (_H (xch)); result =caml_alloc_tuple (3 );
@@ -884,28 +884,37 @@ CAMLprim value stub_xc_version_compile_info(value xch) {CAMLparam1 (xch);CAMLlocal1 (result); +xen_compile_info_t ci; +int retval; + +caml_enter_blocking_section (); + retval =xc_version (_H (xch), XENVER_compile_info, &ci); +caml_leave_blocking_section (); + +if (retval) +failwith_xc (_H (xch)); result =caml_alloc_tuple (4 ); -Store_field (result,0 ,caml_copy_string ("" )); -Store_field (result,1 ,caml_copy_string ("" )); -Store_field (result,2 ,caml_copy_string ("" )); -Store_field (result,3 ,caml_copy_string ("" )); +Store_field (result,0 ,caml_copy_string (ci.compiler)); +Store_field (result,1 ,caml_copy_string (ci.compile_by)); +Store_field (result,2 ,caml_copy_string (ci.compile_domain)); +Store_field (result,3 ,caml_copy_string (ci.compile_date));CAMLreturn (result); } -static value xc_version_single_string (value xch,int code,void *info,ssize_t len) +static value xc_version_single_string (value xch,int code,void *info) {CAMLparam1 (xch);int retval;caml_enter_blocking_section (); - retval =xc_version (_H (xch), code, info, len); + retval =xc_version (_H (xch), code, info);caml_leave_blocking_section (); -if (retval <0 ) +if (retval)failwith_xc (_H (xch));CAMLreturn (caml_copy_string ((char *)info));
@@ -916,8 +925,7 @@ CAMLprim value stub_xc_version_changeset(value xch) {xen_changeset_info_t ci; -return xc_version_single_string (xch, XEN_VERSION_changeset, - &ci,sizeof (ci)); +return xc_version_single_string (xch, XENVER_changeset, &ci); }
@@ -925,8 +933,7 @@ CAMLprim value stub_xc_version_capabilities(value xch) {xen_capabilities_info_t ci; -return xc_version_single_string (xch, XEN_VERSION_capabilities, - &ci,sizeof (ci)); +return xc_version_single_string (xch, XENVER_capabilities, &ci); }
tools/python/xen/lowlevel/xc/xc.c +12 -18
@@ -1204,40 +1204,34 @@ static PyObject *pyxc_xeninfo(XcObject *self)xen_capabilities_info_t xen_caps;xen_platform_parameters_t p_parms;xen_commandline_t xen_commandline; -xen_version_op_val_t xen_version; -xen_version_op_val_t xen_pagesize; +long xen_version; +long xen_pagesize;char str[128 ]; -if (xc_version (self->xc_handle, XEN_VERSION_version, &xen_version, -sizeof (xen_version)) <0 ) -return pyxc_error_to_exception (self->xc_handle); + xen_version =xc_version (self->xc_handle, XENVER_version,NULL ); -if (xc_version (self->xc_handle, XEN_VERSION_extraversion, &xen_extra, -sizeof (xen_extra)) <0 ) +if (xc_version (self->xc_handle, XENVER_extraversion, &xen_extra) !=0 )return pyxc_error_to_exception (self->xc_handle); -memset (&xen_cc,0 ,sizeof (xen_cc)); +if (xc_version (self->xc_handle, XENVER_compile_info, &xen_cc) !=0 ) +return pyxc_error_to_exception (self->xc_handle); -if (xc_version (self->xc_handle, XEN_VERSION_changeset, &xen_chgset, -sizeof (xen_chgset)) <0 ) +if (xc_version (self->xc_handle, XENVER_changeset, &xen_chgset) !=0 )return pyxc_error_to_exception (self->xc_handle); -if (xc_version (self->xc_handle, XEN_VERSION_capabilities, &xen_caps, -sizeof (xen_caps)) <0 ) +if (xc_version (self->xc_handle, XENVER_capabilities, &xen_caps) !=0 )return pyxc_error_to_exception (self->xc_handle); -if (xc_version (self->xc_handle, XEN_VERSION_platform_parameters, - &p_parms,sizeof (p_parms)) <0 ) +if (xc_version (self->xc_handle, XENVER_platform_parameters, &p_parms) !=0 )return pyxc_error_to_exception (self->xc_handle); -if (xc_version (self->xc_handle, XEN_VERSION_commandline, - &xen_commandline,sizeof (xen_commandline)) <0 ) +if (xc_version (self->xc_handle, XENVER_commandline, &xen_commandline) !=0 )return pyxc_error_to_exception (self->xc_handle);snprintf (str,sizeof (str),"virt_start=0x%" PRI_xen_ulong, p_parms.virt_start); -if (xc_version (self->xc_handle, XEN_VERSION_pagesize, &xen_pagesize, -sizeof (xen_pagesize)) <0 ) + xen_pagesize =xc_version (self->xc_handle, XENVER_pagesize,NULL ); +if (xen_pagesize <0 )return pyxc_error_to_exception (self->xc_handle); return Py_BuildValue("{s:i,s:i,s:s,s:s,s:i,s:s,s:s,s:s,s:s,s:s,s:s,s:s}",
tools/xenstat/libxenstat/src/xenstat.c +7 -5
@@ -621,18 +621,20 @@ unsigned long long xenstat_network_tdrop(xenstat_network * network)/* Collect Xen version information */ static int xenstat_collect_xen_version (xenstat_node * node) { -xen_version_op_val_t vnum =0 ; +long vnum =0 ;xen_extraversion_t version;/* Collect Xen version information if not already collected */ if (node->handle->xen_version[0 ] =='\0' ) {/* Get the Xen version number and extraversion string */ -if (xc_version (node->handle->xc_handle, - XEN_VERSION_version, &vnum,sizeof (vnum)) <0 ) + vnum =xc_version (node->handle->xc_handle, + XENVER_version,NULL ); + +if (vnum <0 )return 0 ; -if (xc_version (node->handle->xc_handle, XEN_VERSION_extraversion, - &version,sizeof (version)) <0 ) +if (xc_version (node->handle->xc_handle, XENVER_extraversion, + &version) <0 )return 0 ;/* Format the version information as a string and store it */ snprintf(node->handle->xen_version, VERSION_SIZE, "%ld.%ld%s",
tools/xentrace/xenctx.c +1 -2
@@ -1000,8 +1000,7 @@ static void dump_ctx(int vcpu) guest_word_size = (cpuctx.msr_efer &0x400 ) ?8 : guest_protected_mode ?4 :2 ;/* HVM guest context records are always host-sized */ -if (xc_version (xenctx.xc_handle, XEN_VERSION_capabilities, - &xen_caps,sizeof (xen_caps)) <0 ) { +if (xc_version (xenctx.xc_handle, XENVER_capabilities, &xen_caps) !=0 ) {perror ("xc_version" );return ; }
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 2/27 Revert "HYPERCALL_version_op. New hypercall mirroring XENVER_ but sane."
In reply to Konrad Rzeszutek WilkThis reverts commit 2716d875379d538c1dfccad78a99ca7db2e09f90.
As it was decided that the existing XENVER hypercall - while having grown organically over the years can still be expanded.
--- tools/flask/policy/policy/modules/xen/xen.te | 7 +- xen/arch/arm/traps.c | 1 - xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/x86_64/compat/entry.S | 2 - xen/arch/x86/x86_64/entry.S | 2 - xen/common/compat/kernel.c | 2 - xen/common/kernel.c | 212 +++++---------------------- xen/include/public/arch-arm.h | 2 - xen/include/public/version.h | 72 +-------- xen/include/public/xen.h | 1 - xen/include/xen/hypercall.h | 4 - xen/include/xsm/dummy.h | 21 --- xen/include/xsm/xsm.h | 6 - xen/xsm/dummy.c | 1 - xen/xsm/flask/hooks.c | 35 ----- xen/xsm/flask/policy/access_vectors | 21 +-- 16 files changed, 43 insertions(+), 347 deletions(-)
tools/flask/policy/policy/modules/xen/xen.te +2 -5
@@ -76,12 +76,11 @@ allow dom0_t xen_t:xen2 { get_cpu_featureset }; -# Allow dom0 to use all XENVER_ subops and VERSION subops that have checks. +# Allow dom0 to use all XENVER_ subops that have checks. # Note that dom0 is part of domain_type so this has duplicates. allow dom0_t xen_t:version { xen_extraversion xen_compile_info xen_capabilities xen_changeset xen_pagesize xen_guest_handle xen_commandline - extraversion capabilities changeset pagesize guest_handle commandline }; allow dom0_t xen_t:mmu memorymap;
@@ -148,12 +147,10 @@ if (guest_writeconsole) { # pmu_ctrl is for) allow domain_type xen_t:xen2 pmu_use; -# For normal guests all possible except XENVER_commandline, VERSION_changeset, -# and VERSION_commandline +# For normal guests all possible except XENVER_commandline. allow domain_type xen_t:version { xen_extraversion xen_compile_info xen_capabilities xen_changeset xen_pagesize xen_guest_handle - extraversion capabilities pagesize guest_handle }; ###############################################################################
xen/arch/arm/traps.c +0 -1
@@ -1274,7 +1274,6 @@ static arm_hypercall_t arm_hypercall_table[] = {HYPERCALL (multicall,2 ),HYPERCALL (platform_op,1 ), HYPERCALL_ARM(vcpu_op, 3), - HYPERCALL(version_op, 3), };#ifndef NDEBUG
xen/arch/x86/hvm/hvm.c +0 -1
@@ -4054,7 +4054,6 @@ static const struct {COMPAT_CALL (platform_op),COMPAT_CALL (mmuext_op),HYPERCALL (xenpmu_op), -HYPERCALL (version_op), HYPERCALL(arch_1) };
xen/arch/x86/x86_64/compat/entry.S +0 -2
@@ -399,7 +399,6 @@ ENTRY(compat_hypercall_table) .quad do_tmem_op .quad do_ni_hypercall /* reserved for XenClient */ .quad do_xenpmu_op /* 40 */ - .quad do_version_op .rept __HYPERVISOR_arch_0-((.-compat_hypercall_table)/8) .quad compat_ni_hypercall .endr
@@ -451,7 +450,6 @@ ENTRY(compat_hypercall_args_table) .byte 1 /* do_tmem_op */ .byte 0 /* reserved for XenClient */ .byte 2 /* do_xenpmu_op */ /* 40 */ - .byte 3 /* do_version_op */ .rept __HYPERVISOR_arch_0-(.-compat_hypercall_args_table) .byte 0 /* compat_ni_hypercall */ .endr
xen/arch/x86/x86_64/entry.S +0 -2
@@ -735,7 +735,6 @@ ENTRY(hypercall_table) .quad do_tmem_op .quad do_ni_hypercall /* reserved for XenClient */ .quad do_xenpmu_op /* 40 */ - .quad do_version_op .rept __HYPERVISOR_arch_0-((.-hypercall_table)/8) .quad do_ni_hypercall .endr
@@ -787,7 +786,6 @@ ENTRY(hypercall_args_table) .byte 1 /* do_tmem_op */ .byte 0 /* reserved for XenClient */ .byte 2 /* do_xenpmu_op */ /* 40 */ - .byte 3 /* do_version_op */ .rept __HYPERVISOR_arch_0-(.-hypercall_args_table) .byte 0 /* do_ni_hypercall */ .endr
xen/common/compat/kernel.c +0 -2
@@ -39,8 +39,6 @@ CHECK_TYPE(capabilities_info);CHECK_TYPE (domain_handle); -CHECK_TYPE (version_op_val); -#define xennmi_callback compat_nmi_callback#define xennmi_callback_t compat_nmi_callback_t
xen/common/kernel.c +37 -175
240 lines not on this page · raw message
xen/include/public/arch-arm.h +0 -2
@@ -128,8 +128,6 @@* * VCPUOP_register_vcpu_info * * VCPUOP_register_runstate_memory_area * -* HYPERVISOR_version_op -* All generic sub-operations * * Other notes on the ARM ABI: *
xen/include/public/version.h +1 -71
84 lines not on this page · raw message
xen/include/public/xen.h +0 -1
@@ -115,7 +115,6 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);#define __HYPERVISOR_tmem_op 38#define __HYPERVISOR_xc_reserved_op 39/* reserved for XenClient */ #define __HYPERVISOR_xenpmu_op 40 -#define __HYPERVISOR_version_op 41/* supersedes xen_version (17) */ /* Architecture-specific hypercall definitions. */ #define __HYPERVISOR_arch_0 48
xen/include/xen/hypercall.h +0 -4
@@ -147,10 +147,6 @@ do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);extern long do_xenpmu_op (unsigned int op,XEN_GUEST_HANDLE_PARAM (xen_pmu_params_t) arg); -extern long -do_version_op (unsigned int cmd, -XEN_GUEST_HANDLE_PARAM (void ) arg,unsigned int len); -#ifdef CONFIG_COMPAT extern int
xen/include/xsm/dummy.h +0 -21
@@ -751,24 +751,3 @@ static XSM_INLINE int xsm_xen_version (XSM_DEFAULT_ARG uint32_t op)return xsm_default_action (XSM_PRIV , current->domain,NULL ); } } - -static XSM_INLINE int xsm_version_op (XSM_DEFAULT_ARG uint32_t op) -{ -XSM_ASSERT_ACTION (XSM_OTHER ); -switch ( op ) - { -case XEN_VERSION_version: -case XEN_VERSION_platform_parameters: -case XEN_VERSION_get_features: -/* These MUST always be accessible to any guest by default. */ -return 0 ; -case XEN_VERSION_extraversion: -case XEN_VERSION_capabilities: -case XEN_VERSION_pagesize: -case XEN_VERSION_guest_handle: -/* These can be accessible to a guest. */ -return xsm_default_action (XSM_HOOK , current->domain,NULL ); -default : -return xsm_default_action (XSM_PRIV , current->domain,NULL ); - } -}
xen/include/xsm/xsm.h +0 -6
@@ -197,7 +197,6 @@ struct xsm_operations {int (*pmu_op) (struct domain *d,unsigned int op);#endif int (*xen_version) (uint32_t cmd); -int (*version_op) (uint32_t cmd); };#ifdef CONFIG_XSM
@@ -741,11 +740,6 @@ static inline int xsm_xen_version (xsm_default_t def, uint32_t op) return xsm_ops->xen_version(op); } -static inline int xsm_version_op (xsm_default_t def,uint32_t op) -{ -return xsm_ops->version_op (op); -} -#endif /* XSM_NO_WRAPPERS */ #ifdef CONFIG_MULTIBOOT
xen/xsm/dummy.c +0 -1
@@ -163,5 +163,4 @@ void xsm_fixup_ops (struct xsm_operations *ops)set_to_dummy_if_null (ops, pmu_op);#endif set_to_dummy_if_null (ops, xen_version); -set_to_dummy_if_null (ops, version_op); }
xen/xsm/flask/hooks.c +0 -35
@@ -1664,40 +1664,6 @@ static int flask_xen_version (uint32_t op) } } -static int flask_version_op (uint32_t op) -{ -u32 dsid =domain_sid (current->domain); - -switch ( op ) - { -case XEN_VERSION_version: -case XEN_VERSION_platform_parameters: -case XEN_VERSION_get_features: -/* These MUST always be accessible to any guest by default. */ -return 0 ; -case XEN_VERSION_extraversion: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__EXTRAVERSION ,NULL ); -case XEN_VERSION_capabilities: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__CAPABILITIES ,NULL ); -case XEN_VERSION_changeset: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__CHANGESET ,NULL ); -case XEN_VERSION_pagesize: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__PAGESIZE ,NULL ); -case XEN_VERSION_guest_handle: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__GUEST_HANDLE ,NULL ); -case XEN_VERSION_commandline: -return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , -VERSION__COMMANDLINE ,NULL ); -default : -return -EPERM ; - } -} -long do_flask_op (XEN_GUEST_HANDLE_PARAM (xsm_op_t) u_flask_op);int compat_flask_op (XEN_GUEST_HANDLE_PARAM (xsm_op_t) u_flask_op);
@@ -1837,7 +1803,6 @@ static struct xsm_operations flask_ops = { .pmu_op = flask_pmu_op, #endif .xen_version = flask_xen_version, - .version_op = flask_version_op, };static __init void flask_init (void )
xen/xsm/flask/policy/access_vectors +3 -18
@@ -500,14 +500,12 @@ class security del_ocontext } -# Class version is used to describe the XENVER_ and VERSION hypercall. +# Class version is used to describe the XENVER_ hypercall. # Almost all sub-ops are described here - in the default case all of them should -# be allowed except the XENVER_commandline, VERSION_commandline, and -# VERSION_changeset. +# be allowed except the XENVER_commandline. # # The ones that are omitted are XENVER_version, XENVER_platform_parameters, -# XENVER_get_features, XEN_VERSION_version, XEN_VERSION_platform_parameters, -# and XEN_VERSION_get_features - as they MUST always be returned to a guest. +# and XENVER_get_features - as they MUST always be returned to a guest. # class version {
@@ -525,17 +523,4 @@ class version xen_guest_handle # Xen command line. xen_commandline -# --- VERSION hypercall --- -# Extra informations (-unstable). - extraversion -# Such as "xen-3.0-x86_64 xen-3.0-x86_32p hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64". - capabilities -# Source code changeset. - changeset -# Page size the hypervisor uses. - pagesize -# An value that the control stack can choose. - guest_handle -# Xen command line. - commandline }
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 02/27] Revert "HYPERCALL_version_op. New hypercall mirroring XENVER_ but sane."
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:04 AM >>>
This reverts commit
2716d875379d538c1dfccad78a99ca7db2e09f90.As it was decided that the existing XENVER hypercall - while having grown organically over the years can still be expanded.
Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 3/27 xsplice: Design document
In reply to Konrad Rzeszutek WilkA mechanism is required to binarily patch the running hypervisor with new opcodes that have come about due to primarily security updates.
This document describes the design of the API that would allow us to upload to the hypervisor binary patches.
This document has been shaped by the input from:
Martin Pohlack <[email protected]>
Jan Beulich <[email protected]>
Thank you!
Input-from: Martin Pohlack <[email protected]>
Input-from: Jan Beulich <[email protected]>
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
v1-2: review
v3: Split document in v1 and v2 (todo) to simplify implementation goals.
- Add const on some structures. Truncate size to uint16_t where it makes sense.
- Convert 'id' to 'name', Add Ross's comments about what is implemented.
- Wei's and Ross's reviews.
- Jan's review comments.
- Jan's review comments.
s/int32_t state/uint32_t state/ now that return code is in seperate field (rc). Add various other types, such as R_X86_64_PC64 in the list. Mention the need for compiler check.
v4:
- Drop the LOADED->CHECKED state and go directly to CHECKED state. Drop LOADED.
v5: Julien mentioned ARM 32-bit would not use ELF64, so make the .xsplice.func
use uintXX_t types instead of ELF ones. Remove the OUT on idx subfield. Mention that 'nr' being zero can be used for probing the number of payloads. Update what 'idx' means.
v6: Update what 'idx' means again!
Move the "Interdependencies section" to make it easier to in the design doc the movement of text (when the patch implements it).
Add also 'version' field to payload.
---
--- docs/misc/xsplice.markdown | 1044 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1044 insertions(+) create mode 100644 docs/misc/xsplice.markdown
docs/misc/xsplice.markdown new file +1044 -0
@@ -0,0 +1,1044 @@ +# xSplice Design v1 + +## Rationale + +A mechanism is required to binarily patch the running hypervisor with new +opcodes that have come about due to primarily security updates. + +This document describes the design of the API that would allow us to +upload to the hypervisor binary patches. + +The document is split in four sections: + + * Detailed descriptions of the problem statement. + * Design of the data structures. + * Design of the hypercalls. + * Implementation notes that should be taken into consideration. + + +## Glossary + + * splice - patch in the binary code with new opcodes + * trampoline - a jump to a new instruction. + * payload - telemetries of the old code along with binary blob of the new + function (if needed). + * reloc - telemetries contained in the payload to construct proper trampoline. + +## History + +The document has gone under various reviews and only covers v1 design. + +The end of the document has a section titled `Not Yet Done` which +outlines ideas and design for the future version of this work. + +## Multiple ways to patch + +The mechanism needs to be flexible to patch the hypervisor in multiple ways +and be as simple as possible. The compiled code is contiguous in memory with +no gaps - so we have no luxury of 'moving' existing code and must either +insert a trampoline to the new code to be executed - or only modify in-place +the code if there is sufficient space. The placement of new code has to be done +by hypervisor and the virtual address for the new code is allocated dynamically. + +This implies that the hypervisor must compute the new offsets when splicing +in the new trampoline code. Where the trampoline is added (inside +the function we are patching or just the callers?) is also important. + +To lessen the amount of code in hypervisor, the consumer of the API +is responsible for identifying which mechanism to employ and how many locations +to patch. Combinations of modifying in-place code, adding trampoline, etc +has to be supported. The API should allow read/write any memory within +the hypervisor virtual address space. + +We must also have a mechanism to query what has been applied and a mechanism +to revert it if needed. + +## Workflow + +The expected workflows of higher-level tools that manage multiple patches +on production machines would be: + + * The first obvious task is loading all available / suggested + hotpatches when they are available. + * Whenever new hotpatches are installed, they should be loaded too. + * One wants to query which modules have been loaded at runtime. + * If unloading is deemed safe (see unloading below), one may want to + support a workflow where a specific hotpatch is marked as bad and + unloaded. + +## Patching code + +The first mechanism to patch that comes in mind is in-place replacement. +That is replace the affected code with new code. Unfortunately the x86 +ISA is variable size which places limits on how much space we have available +to replace the instructions. That is not a problem if the change is smaller +than the original opcode and we can fill it with nops. Problems will +appear if the replacement code is longer. + +The second mechanism is by ti replace the call or jump to the +old function with the address of the new function. + +A third mechanism is to add a jump to the new function at the +start of the old function. N.B. The Xen hypervisor implements the third +mechanism. See `Trampoline (e9 opcode)` section for more details. + +### Example of trampoline and in-place splicing + +As example we will assume the hypervisor does not have XSA-132 (see +*domctl/sysctl: don't leak hypervisor stack to toolstacks* +4ff3449f0e9d175ceb9551d3f2aecb59273f639d) and we would like to binary patch +the hypervisor with it. The original code looks as so: + +<pre> + 48 89 e0 mov %rsp,%rax + 48 25 00 80 ff ff and $0xffffffffffff8000,%rax +</pre> + +while the new patched hypervisor would be: + +<pre> + 48 c7 45 b8 00 00 00 00 movq $0x0,-0x48(%rbp) + 48 c7 45 c0 00 00 00 00 movq $0x0,-0x40(%rbp) + 48 c7 45 c8 00 00 00 00 movq $0x0,-0x38(%rbp) + 48 89 e0 mov %rsp,%rax + 48 25 00 80 ff ff and $0xffffffffffff8000,%rax +</pre> + +This is inside the arch_do_domctl. This new change adds 21 extra +bytes of code which alters all the offsets inside the function. To alter +these offsets and add the extra 21 bytes of code we might not have enough +space in .text to squeeze this in. + +As such we could simplify this problem by only patching the site +which calls arch_do_domctl: + +<pre> +do_domctl: + e8 4b b1 05 00 callq ffff82d08015fbb9 <arch_do_domctl> +</pre> + +with a new address for where the new `arch_do_domctl` would be (this +area would be allocated dynamically). + +Astute readers will wonder what we need to do if we were to patch `do_domctl` +- which is not called directly by hypervisor but on behalf of the guests via +the `compat_hypercall_table` and `hypercall_table`. +Patching the offset in `hypercall_table` for `do_domctl: +(ffff82d080103079 <do_domctl>:) + +<pre> + + ffff82d08024d490: 79 30 + ffff82d08024d492: 10 80 d0 82 ff ff + +</pre> + +with the new address where the new `do_domctl` is possible. The other +place where it is used is in `hvm_hypercall64_table` which would need +to be patched in a similar way. This would require an in-place splicing +of the new virtual address of `arch_do_domctl`. + +In summary this example patched the callee of the affected function by + * allocating memory for the new code to live in, + * changing the virtual address in all the functions which called the old + code (computing the new offset, patching the callq with a new callq). + * changing the function pointer tables with the new virtual address of + the function (splicing in the new virtual address). Since this table + resides in the .rodata section we would need to temporarily change the + page table permissions during this part. + +However it has drawbacks - the safety checks which have to make sure +the function is not on the stack - must also check every caller. For some +patches this could mean - if there were an sufficient large amount of +callers - that we would never be able to apply the update. + +Having the patching done at predetermined instances where the stacks +are not deep mostly solves this problem. + +### Example of different trampoline patching. + +An alternative mechanism exists where we can insert a trampoline in the +existing function to be patched to jump directly to the new code. This +lessens the locations to be patched to one but it puts pressure on the +CPU branching logic (I-cache, but it is just one unconditional jump). + +For this example we will assume that the hypervisor has not been compiled +with fe2e079f642effb3d24a6e1a7096ef26e691d93e (XSA-125: *pre-fill structures +for certain HYPERVISOR_xen_version sub-ops*) which mem-sets an structure +in `xen_version` hypercall. This function is not called **anywhere** in +the hypervisor (it is called by the guest) but referenced in the +`compat_hypercall_table` and `hypercall_table` (and indirectly called +from that). Patching the offset in `hypercall_table` for the old +`do_xen_version` (ffff82d080112f9e <do_xen_version>) + +</pre> + ffff82d08024b270 <hypercall_table>: + ... + ffff82d08024b2f8: 9e 2f 11 80 d0 82 ff ff + +</pre> + +with the new address where the new `do_xen_version` is possible. The other +place where it is used is in `hvm_hypercall64_table` which would need +to be patched in a similar way. This would require an in-place splicing +of the new virtual address of `do_xen_version`. + +An alternative solution would be to patch insert a trampoline in the +old `do_xen_version' function to directly jump to the new `do_xen_version`. + +<pre> + ffff82d080112f9e do_xen_version: + ffff82d080112f9e: 48 c7 c0 da ff ff ff mov $0xffffffffffffffda,%rax + ffff82d080112fa5: 83 ff 09 cmp $0x9,%edi + ffff82d080112fa8: 0f 87 24 05 00 00 ja ffff82d0801134d2 ; do_xen_version+0x534 +</pre> + +with: + +<pre> + ffff82d080112f9e do_xen_version: + ffff82d080112f9e: e9 XX YY ZZ QQ jmpq [new do_xen_version] +</pre> + +which would lessen the amount of patching to just one location. + +In summary this example patched the affected function to jump to the +new replacement function which required: + * allocating memory for the new code to live in, + * inserting trampoline with new offset in the old function to point to the + new function. + * Optionally we can insert in the old function a trampoline jump to an function + providing an BUG_ON to catch errant code. + +The disadvantage of this are that the unconditional jump will consume a small +I-cache penalty. However the simplicity of the patching and higher chance +of passing safety checks make this a worthwhile option. + +This patching has a similar drawback as inline patching - the safety +checks have to make sure the function is not on the stack. However +since we are replacing at a higher level (a full function as opposed +to various offsets within functions) the checks are simpler. + +Having the patching done at predetermined instances where the stacks +are not deep mostly solves this problem as well. + +### Security + +With this method we can re-write the hypervisor - and as such we **MUST** be +diligent in only allowing certain guests to perform this operation. + +Furthermore with SecureBoot or tboot, we **MUST** also verify the signature +of the payload to be certain it came from a trusted source and integrity +was intact. + +As such the hypercall **MUST** support an XSM policy to limit what the guest +is allowed to invoke. If the system is booted with signature checking the +signature checking will be enforced. + +## Design of payload format + +The payload **MUST** contain enough data to allow us to apply the update +and also safely reverse it. As such we **MUST** know: + + * The locations in memory to be patched. This can be determined dynamically + via symbols or via virtual addresses. + * The new code that will be patched in. + +This binary format can be constructed using an custom binary format but +there are severe disadvantages of it: + + * The format might need to be changed and we need an mechanism to accommodate + that. + * It has to be platform agnostic. + * Easily constructed using existing tools. + +As such having the payload in an ELF file is the sensible way. We would be +carrying the various sets of structures (and data) in the ELF sections under +different names and with definitions. + +Note that every structure has padding. This is added so that the hypervisor +can re-use those fields as it sees fit. + +Earlier design attempted to ineptly explain the relations of the ELF sections +to each other without using proper ELF mechanism (sh_info, sh_link, data +structures using Elf types, etc). This design will explain the structures +and how they are used together and not dig in the ELF format - except mention +that the section names should match the structure names. + +The xSplice payload is a relocatable ELF binary. A typical binary would have: + + * One or more .text sections. + * Zero or more read-only data sections. + * Zero or more data sections. + * Relocations for each of these sections. + +It may also have some architecture-specific sections. For example: + + * Alternatives instructions. + * Bug frames. + * Exception tables. + * Relocations for each of these sections. + +The xSplice core code loads the payload as a standard ELF binary, relocates it +and handles the architecture-specifc sections as needed. This process is much +like what the Linux kernel module loader does. + +The payload contains a section (xsplice_patch_func) with an array of structures +describing the functions to be patched: + +<pre> +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 version; + uint8_t pad[31]; +}; +</pre> + +The size of the structure is 64 bytes. + +* `name` is the symbol name of the old function. Only used if `old_addr` is + zero, otherwise will be used during dynamic linking (when hypervisor loads + the payload). + +* `old_addr` is the address of the function to be patched and is filled in at + payload generation time if hypervisor function address is known. If unknown, + the value *MUST* be zero and the hypervisor will attempt to resolve the address. + +* `new_addr` is the address of the function that is replacing the old + function. The address is filled in during relocation. The value **MUST** be + the address of the new function in the file. + +* `old_size` and `new_size` contain the sizes of the respective functions in bytes. + The value of `old_size` **MUST** not be zero. + +* `version` is to be one. + +* `pad` **MUST** be zero. + +The size of the `xsplice_patch_func` array is determined from the ELF section +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`. + +## Hypercalls + +We will employ the sub operations of the system management hypercall (sysctl). +There are to be four sub-operations: + + * upload the payloads. + * listing of payloads summary uploaded and their state. + * getting an particular payload summary and its state. + * command to apply, delete, or revert the payload. + +Most of the actions are asynchronous therefore the caller is responsible +to verify that it has been applied properly by retrieving the summary of it +and verifying that there are no error codes associated with the payload. + +We **MUST** make some of them asynchronous due to the nature of patching +it requires every physical CPU to be lock-step with each other. +The patching mechanism while an implementation detail, is not an short +operation and as such the design **MUST** assume it will be an long-running +operation. + +The sub-operations will spell out how preemption is to be handled (if at all). + +Furthermore it is possible to have multiple different payloads for the same +function. As such an unique name per payload has to be visible to allow proper manipulation. + +The hypercall is part of the `xen_sysctl`. The top level structure contains +one uint32_t to determine the sub-operations and one padding field which +*MUST* always be zero. + +<pre> +struct xen_sysctl_xsplice_op { + uint32_t cmd; /* IN: XEN_SYSCTL_XSPLICE_*. */ + uint32_t pad; /* IN: Always zero. */ + union { + ... see below ... + } u; +}; + +</pre> +while the rest of hypercall specific structures are part of the this structure. + +### Basic type: struct xen_xsplice_name + +Most of the hypercalls employ an shared structure called `struct xen_xsplice_name` +which contains: + + * `name` - pointer where the string for the name is located. + * `size` - the size of the string + * `pad` - padding - to be zero. + +The structure is as follow: + +<pre> +/* + * Uniquely identifies the payload. Should be human readable. + * Includes the NUL terminator + */ +#define XEN_XSPLICE_NAME_SIZE 128 +struct xen_xsplice_name { + XEN_GUEST_HANDLE_64(char) name; /* IN, pointer to name. */ + uint16_t size; /* IN, size of name. May be upto + XEN_XSPLICE_NAME_SIZE. */ + uint16_t pad[3]; /* IN: MUST be zero. */ +}; +</pre> + +### XEN_SYSCTL_XSPLICE_UPLOAD (0) + +Upload a payload to the hypervisor. The payload is verified +against basic checks and if there are any issues the proper return code +will be returned. The payload is not applied at this time - that is +controlled by *XEN_SYSCTL_XSPLICE_ACTION*. + +The caller provides: + + * A `struct xen_xsplice_name` called `name` which has the unique name. + * `size` the size of the ELF payload (in bytes). + * `payload` the virtual address of where the ELF payload is. + +The `name` could be an UUID that stays fixed forever for a given +payload. It can be embedded into the ELF payload at creation time +and extracted by tools. + +The return value is zero if the payload was succesfully uploaded. +Otherwise an -XEN_EXX return value is provided. Duplicate `name` are not supported. + +The `payload` is the ELF payload as mentioned in the `Payload format` section. + +The structure is as follow: + +<pre> +struct xen_sysctl_xsplice_upload { + xen_xsplice_name_t name; /* IN, name of the patch. */ + uint64_t size; /* IN, size of the ELF file. */ + XEN_GUEST_HANDLE_64(uint8) payload; /* IN: ELF file. */ +}; +</pre> + +### XEN_SYSCTL_XSPLICE_GET (1) + +Retrieve an status of an specific payload. This caller provides: + + * A `struct xen_xsplice_name` called `name` which has the unique name. + * A `struct xen_xsplice_status` structure. The member values will + be over-written upon completion. + +Upon completion the `struct xen_xsplice_status` is updated. + + * `status` - indicates the current status of the payload: + * *XSPLICE_STATUS_CHECKED* (1) loaded and the ELF payload safety checks passed. + * *XSPLICE_STATUS_APPLIED* (2) loaded, checked, and applied. + * No other value is possible. + * `rc` - -XEN_EXX type errors encountered while performing the last + XSPLICE_ACTION_* operation. The normal values can be zero or -XEN_EAGAIN which + respectively mean: success or operation in progress. Other values + imply an error occurred. If there is an error in `rc`, `status` will **NOT** + have changed. + +The return value of the hypercall is zero on success and -XEN_EXX on failure. +(Note that the `rc`` value can be different from the return value, as in +rc=-XEN_EAGAIN and return value can be 0). + +For example, supposing there is an payload: + +<pre> + status: XSPLICE_STATUS_CHECKED + rc: 0 +</pre> + +We apply an action - XSPLICE_ACTION_REVERT - to revert it (which won't work +as we have not even applied it. Afterwards we will have: + +<pre> + status: XSPLICE_STATUS_CHECKED + rc: -XEN_EINVAL +</pre> + +It has failed but it remains loaded. + +This operation is synchronous and does not require preemption. + +The structure is as follow: + +<pre> +struct xen_xsplice_status { +#define XSPLICE_STATUS_CHECKED 1 +#define XSPLICE_STATUS_APPLIED 2 + uint32_t state; /* OUT: XSPLICE_STATE_*. */ + int32_t rc; /* OUT: 0 if no error, otherwise -XEN_EXX. */ +}; + +struct xen_sysctl_xsplice_get { + xen_xsplice_name_t name; /* IN, the name of the payload. */ + xen_xsplice_status_t status; /* IN/OUT: status of the payload. */ +}; +</pre> + +### XEN_SYSCTL_XSPLICE_LIST (2) + +Retrieve an array of abbreviated status and names of payloads that are loaded in the +hypervisor. + +The caller provides: + + * `version`. Version of the payload. Caller should re-use the field provided by + the hypervisor. If the value differs the data is stale. + * `idx` index iterator. The index into the hypervisor's payload count. It is + recommended that on first invocation zero be used so that `nr` (which the + hypervisor will update with the remaining payload count) be provided. + Also the hypervisor will provide `version` with the most current value. + * `nr` the max number of entries to populate. Can be zero which will result + in the hypercall being a probing one and return the number of payloads + (and update the `version`). + * `pad` - *MUST* be zero. + * `status` virtual address of where to write `struct xen_xsplice_status` + structures. Caller *MUST* allocate up to `nr` of them. + * `name` - virtual address of where to write the unique name of the payload. + Caller *MUST* allocate up to `nr` of them. Each *MUST* be of + **XEN_XSPLICE_NAME_SIZE** size. Note that **XEN_XSPLICE_NAME_SIZE** includes + the NUL terminator. + * `len` - virtual address of where to write the length of each unique name + of the payload. Caller *MUST* allocate up to `nr` of them. Each *MUST* be + of sizeof(uint32_t) (4 bytes). + +If the hypercall returns an positive number, it is the number (upto `nr` +provided to the hypercall) of the payloads returned, along with `nr` updated +with the number of remaining payloads, `version` updated (it may be the same +across hypercalls - if it varies the data is stale and further calls could +fail). The `status`, `name`, and `len`' are updated at their designed index +value (`idx`) with the returned value of data. + +If the hypercall returns -XEN_E2BIG the `nr` is too big and should be +lowered. + +If the hypercall returns an zero value there are no more payloads. + +Note that due to the asynchronous nature of hypercalls the control domain might +have added or removed a number of payloads making this information stale. It is +the responsibility of the toolstack to use the `version` field to check +between each invocation. if the version differs it should discard the stale +data and start from scratch. It is OK for the toolstack to use the new +`version` field. + +The `struct xen_xsplice_status` structure contains an status of payload which includes: + + * `status` - indicates the current status of the payload: + * *XSPLICE_STATUS_CHECKED* (1) loaded and the ELF payload safety checks passed. + * *XSPLICE_STATUS_APPLIED* (2) loaded, checked, and applied. + * No other value is possible. + * `rc` - -XEN_EXX type errors encountered while performing the last + XSPLICE_ACTION_* operation. The normal values can be zero or -XEN_EAGAIN which + respectively mean: success or operation in progress. Other values + imply an error occurred. If there is an error in `rc`, `status` will **NOT** + have changed. + +The structure is as follow: + +<pre> +struct xen_sysctl_xsplice_list { + uint32_t version; /* OUT: Hypervisor stamps value. + If varies between calls, we are + getting stale data. */ + uint32_t idx; /* IN: Index into hypervisor list. */ + uint32_t nr; /* IN: How many status, names, and len + should be filled out. Can be zero to get + amount of payloads and version. + OUT: How many payloads left. */ + uint32_t pad; /* IN: Must be zero. */ + XEN_GUEST_HANDLE_64(xen_xsplice_status_t) status; /* OUT. Must have enough + space allocate for nr of them. */ + XEN_GUEST_HANDLE_64(char) id; /* OUT: Array of names. Each member + MUST XEN_XSPLICE_NAME_SIZE in size. + Must have nr of them. */ + XEN_GUEST_HANDLE_64(uint32) len; /* OUT: Array of lengths of name's. + Must have nr of them. */ +}; +</pre> + +### XEN_SYSCTL_XSPLICE_ACTION (3) + +Perform an operation on the payload structure referenced by the `name` field. +The operation request is asynchronous and the status should be retrieved +by using either **XEN_SYSCTL_XSPLICE_GET** or **XEN_SYSCTL_XSPLICE_LIST** hypercall. + +The caller provides: + + * A 'struct xen_xsplice_name` `name` containing the unique name. + * `cmd` the command requested: + * *XSPLICE_ACTION_CHECK* (1) check that the payload will apply properly. + This also verfies the payload - which may require SecureBoot firmware + calls. This is the initial state an payload is in. + * *XSPLICE_ACTION_UNLOAD* (2) unload the payload. + Any further hypercalls against the `name` will result in failure unless + **XEN_SYSCTL_XSPLICE_UPLOAD** hypercall is perfomed with same `name`. + * *XSPLICE_ACTION_REVERT* (3) revert the payload. If the operation takes + more time than the upper bound of time the `rc` in `xen_xsplice_status' + retrieved via **XEN_SYSCTL_XSPLICE_GET** will be -XEN_EBUSY. + * *XSPLICE_ACTION_APPLY* (4) apply the payload. If the operation takes + more time than the upper bound of time the `rc` in `xen_xsplice_status' + retrieved via **XEN_SYSCTL_XSPLICE_GET** will be -XEN_EBUSY. + * *XSPLICE_ACTION_REPLACE* (5) revert all applied payloads and apply this + payload. If the operation takes more time than the upper bound of time + the `rc` in `xen_xsplice_status' retrieved via **XEN_SYSCTL_XSPLICE_GET** + will be -XEN_EBUSY. + * `time` the upper bound of time (ms) the cmd should take. Zero means infinite. + If within the time the operation does not succeed the operation would go in + error state. + * `pad` - *MUST* be zero. + +The return value will be zero unless the provided fields are incorrect. + +The structure is as follow: + +<pre> +#define XSPLICE_ACTION_CHECK 1 +#define XSPLICE_ACTION_UNLOAD 2 +#define XSPLICE_ACTION_REVERT 3 +#define XSPLICE_ACTION_APPLY 4 +#define XSPLICE_ACTION_REPLACE 5 +struct xen_sysctl_xsplice_action { + xen_xsplice_name_t name; /* IN, name of the patch. */ + uint32_t cmd; /* IN: XSPLICE_ACTION_* */ + uint32_t time; /* IN: Zero if no timeout. */ + /* Or upper bound of time (ms) */ + /* for operation to take. */ +}; + +</pre> + +## State diagrams of XSPLICE_ACTION commands. + +There is a strict ordering state of what the commands can be. +The XSPLICE_ACTION prefix has been dropped to easy reading and +does not include the XSPLICE_STATES: + +<pre> + /->\ + \ / + UNLOAD <--- CHECK ---> REPLACE|APPLY --> REVERT --\ + \ | + \-------------------<-------------/ + +</pre> +## State transition table of XSPLICE_ACTION commands and XSPLICE_STATUS. + +Note that: + + - The CHECKED state is the starting one achieved with *XEN_SYSCTL_XSPLICE_UPLOAD* hypercall. + - The REVERT operation on success will automatically move to the CHECKED state. + - There are two STATES: CHECKED and APPLIED. + - There are five actions (aka commands): CHECK, APPLY, REPLACE, REVERT, and UNLOAD. + +The state transition table of valid states and action states: + +<pre> + ++---------+---------+--------------------------------+-------+--------+ +| ACTION | Current | Result | Next STATE: | +| ACTION | STATE | |CHECKED|APPLIED | ++---------+----------+-------------------------------+-------+--------+ +| CHECK | CHECKED | Check payload (once more, no) | x | | +| | | errors) | | | ++---------+---------+--------------------------------+-------+--------+ +| CHECK | CHECKED | Check payload (once more, with | | | +| | | errors) | | | ++---------+---------+--------------------------------+-------+--------+ +| UNLOAD | CHECKED | Unload payload. Always works. | | | +| | | No next states. | | | ++---------+---------+--------------------------------+-------+--------+ +| APPLY | CHECKED | Apply payload (success). | | x | ++---------+---------+--------------------------------+-------+--------+ +| APPLY | CHECKED | Apply payload (error|timeout) | x | | ++---------+---------+--------------------------------+-------+--------+ +| REPLACE | CHECKED | Revert payloads and apply new | | x | +| | | payload with success. | | | ++---------+---------+--------------------------------+-------+--------+ +| REPLACE | CHECKED | Revert payloads and apply new | x | | +| | | payload with error. | | | ++---------+---------+--------------------------------+-------+--------+ +| REVERT | APPLIED | Revert payload (success). | x | | ++---------+---------+--------------------------------+-------+--------+ +| REVERT | APPLIED | Revert payload (error|timeout) | | x | ++---------+---------+--------------------------------+-------+--------+ +</pre> + +All the other state transitions are invalid. + +## Sequence of events. + +The normal sequence of events is to: + + 1. *XEN_SYSCTL_XSPLICE_UPLOAD* to upload the payload. If there are errors *STOP* here. + 2. *XEN_SYSCTL_XSPLICE_GET* to check the `->rc`. If *-XEN_EAGAIN* spin. If zero go to next step. + 3. *XEN_SYSCTL_XSPLICE_ACTION* with *XSPLICE_ACTION_APPLY* to apply the patch. + 4. *XEN_SYSCTL_XSPLICE_GET* to check the `->rc`. If in *-XEN_EAGAIN* spin. If zero exit with success. + + +## Addendum + +Implementation quirks should not be discussed in a design document. + +However these observations can provide aid when developing against this +document. + + +### Alternative assembler + +Alternative assembler is a mechanism to use different instructions depending +on what the CPU supports. This is done by providing multiple streams of code +that can be patched in - or if the CPU does not support it - padded with +`nop` operations. The alternative assembler macros cause the compiler to +expand the code to place a most generic code in place - emit a special +ELF .section header to tag this location. During run-time the hypervisor +can leave the areas alone or patch them with an better suited opcodes. + +Note that patching functions that copy to or from guest memory requires +to support alternative support. For example this can be due to SMAP +(specifically *stac* and *clac* operations) which is enabled on Broadwell +and later architectures. It may be related to other alternative instructions. + +### When to patch + +During the discussion on the design two candidates bubbled where +the call stack for each CPU would be deterministic. This would +minimize the chance of the patch not being applied due to safety +checks failing. Safety checks such as not patching code which +is on the stack - which can lead to corruption. + +#### Rendezvous code instead of stop_machine for patching + +The hypervisor's time rendezvous code runs synchronously across all CPUs +every second. Using the stop_machine to patch can stall the time rendezvous +code and result in NMI. As such having the patching be done at the tail +of rendezvous code should avoid this problem. + +However the entrance point for that code is +do_softirq->timer_softirq_action->time_calibration +which ends up calling on_selected_cpus on remote CPUs. + +The remote CPUs receive CALL_FUNCTION_VECTOR IPI and execute the +desired function. + +#### Before entering the guest code. + +Before we call VMXResume we check whether any soft IRQs need to be executed. +This is a good spot because all Xen stacks are effectively empty at +that point. + +To randezvous all the CPUs an barrier with an maximum timeout (which +could be adjusted), combined with forcing all other CPUs through the +hypervisor with IPIs, can be utilized to execute lockstep instructions +on all CPUs. + +The approach is similar in concept to stop_machine and the time rendezvous +but is time-bound. However the local CPU stack is much shorter and +a lot more deterministic. + +This is implemented in the Xen Project hypervisor. + +### Compiling the hypervisor code + +Hotpatch generation often requires support for compiling the target +with -ffunction-sections / -fdata-sections. Changes would have to +be done to the linker scripts to support this. + +### Generation of xSplice ELF payloads + +The design of that is not discussed in this design. + +This is implemented in a seperate tool which lives in a seperate +GIT repo. + +Currently it resides at https://github.com/rosslagerwall/xsplice-build + +### Exception tables and symbol tables growth + +We may need support for adapting or augmenting exception tables if +patching such code. Hotpatches may need to bring their own small +exception tables (similar to how Linux modules support this). + +If supporting hotpatches that introduce additional exception-locations +is not important, one could also change the exception table in-place +and reorder it afterwards. + +As found almost every patch (XSA) to a non-trivial function requires +additional entries in the exception table and/or the bug frames. + +This is implemented in the Xen Project hypervisor. + +### .rodata sections + +The patching might require strings to be updated as well. As such we must be +also able to patch the strings as needed. This sounds simple - but the compiler +has a habit of coalescing strings that are the same - which means if we in-place +alter the strings - other users will be inadvertently affected as well. + +This is also where pointers to functions live - and we may need to patch this +as well. And switch-style jump tables. + +To guard against that we must be prepared to do patching similar to +trampoline patching or in-line depending on the flavour. If we can +do in-line patching we would need to: + + * alter `.rodata` to be writeable. + * inline patch. + * alter `.rodata` to be read-only. + +If are doing trampoline patching we would need to: + + * allocate a new memory location for the string. + * all locations which use this string will have to be updated to use the + offset to the string. + * mark the region RO when we are done. + +The trampoline patching is implemented in the Xen Project hypervisor. + +### .bss and .data sections. + +In place patching writable data is not suitable as it is unclear what should be done +depending on the current state of data. As such it should not be attempted. + +However, functions which are being patched can bring in changes to strings +(.data or .rodata section changes), or even to .bss sections. + +As such the ELF payload can introduce new .rodata, .bss, and .data sections. +Patching in the new function will end up also patching in the new .rodata +section and the new function will reference the new string in the new +.rodata section. + +This is implemented in the Xen Project hypervisor. + +### Security + +Only the privileged domain should be allowed to do this operation. + + +# Not Yet Done + +This is for further development of xSplice. + +## TODO Goals + +The implementation must also have a mechanism for (in no particular order): + + * Be able to lookup in the Xen hypervisor the symbol names of functions from the ELF payload. + * Be able to patch .rodata, .bss, and .data sections. + * Deal with NMI/MCE checks during patching instead of ignoring them. + * Further safety checks (blacklist of which functions cannot be patched, check + the stack, make sure the payload is built with same compiler as hypervisor). + Specifically we want to make sure that xSplice codepaths cannot be patched. + * NOP out the code sequence if `new_size` is zero. + * Deal with other relocation types: R_X86_64_[8,16,32,32S], R_X86_64_PC[8,16,64] in payload file. + * An dependency mechanism for the payloads. To use that information to load: + - The appropiate payload. To verify that payload is built against the + hypervisor. This can be done via the `build-id` + or via providing an copy of the old code - so that the hypervisor can + verify it against the code in memory. + - To construct an appropiate order of payloads to load in case they + depend on each other. + +### Handle inlined __LINE__ + +This problem is related to hotpatch construction +and potentially has influence on the design of the hotpatching +infrastructure in Xen. + +For example: + +We have file1.c with functions f1 and f2 (in that order). f2 contains a +BUG() (or WARN()) macro and at that point embeds the source line number +into the generated code for f2. + +Now we want to hotpatch f1 and the hotpatch source-code patch adds 2 +lines to f1 and as a consequence shifts out f2 by two lines. The newly +constructed file1.o will now contain differences in both binary +functions f1 (because we actually changed it with the applied patch) and +f2 (because the contained BUG macro embeds the new line number). + +Without additional information, an algorithm comparing file1.o before +and after hotpatch application will determine both functions to be +changed and will have to include both into the binary hotpatch. + +Options: + +1. Transform source code patches for hotpatches to be line-neutral for + each chunk. This can be done in almost all cases with either + reformatting of the source code or by introducing artificial + preprocessor "#line n" directives to adjust for the introduced + differences. + + This approach is low-tech and simple. Potentially generated + backtraces and existing debug information refers to the original + build and does not reflect hotpatching state except for actually + hotpatched functions but should be mostly correct. + +2. Ignoring the problem and living with artificially large hotpatches + that unnecessarily patch many functions. + + This approach might lead to some very large hotpatches depending on + content of specific source file. It may also trigger pulling in + functions into the hotpatch that cannot reasonable be hotpatched due + to limitations of a hotpatching framework (init-sections, parts of + the hotpatching framework itself, ...) and may thereby prevent us + from patching a specific problem. + + The decision between 1. and 2. can be made on a patch--by-patch + basis. + +3. Introducing an indirection table for storing line numbers and + treating that specially for binary diffing. Linux may follow + this approach. + + We might either use this indirection table for runtime use and patch + that with each hotpatch (similarly to exception tables) or we might + purely use it when building hotpatches to ignore functions that only + differ at exactly the location where a line-number is embedded. + +For BUG(), WARN(), etc., the line number is embedded into the bug frame, not +the function itself. + +Similar considerations are true to a lesser extent for __FILE__, but it +could be argued that file renaming should be done outside of hotpatches. + +### xSplice interdependencies + +xSplice patches interdependencies are tricky. + +There are the ways this can be addressed: + * A single large patch that subsumes and replaces all previous ones. + Over the life-time of patching the hypervisor this large patch + grows to accumulate all the code changes. + * Hotpatch stack - where an mechanism exists that loads the hotpatches + in the same order they were built in. We would need an build-id + of the hypevisor to make sure the hot-patches are build against the + correct build. + * Payload containing the old code to check against that. That allows + the hotpatches to be loaded indepedently (if they don't overlap) - or + if the old code also containst previously patched code - even if they + overlap. + +The disadvantage of the first large patch is that it can grow over +time and not provide an bisection mechanism to identify faulty patches. + +The hot-patch stack puts stricts requirements on the order of the patches +being loaded and requires an hypervisor build-id to match against. + +The old code allows much more flexibility and an additional guard, +but is more complex to implement. + +## Signature checking requirements. + +The signature checking requires that the layout of the data in memory +**MUST** be same for signature to be verified. This means that the payload +data layout in ELF format **MUST** match what the hypervisor would be +expecting such that it can properly do signature verification. + +The signature is based on the all of the payloads continuously laid out +in memory. The signature is to be appended at the end of the ELF payload +prefixed with the string '~Module signature appended~\n', followed by +an signature header then followed by the signature, key identifier, and signers +name. + +Specifically the signature header would be: + +<pre> +#define PKEY_ALGO_DSA 0 +#define PKEY_ALGO_RSA 1 + +#define PKEY_ID_PGP 0 /* OpenPGP generated key ID */ +#define PKEY_ID_X509 1 /* X.509 arbitrary subjectKeyIdentifier */ + +#define HASH_ALGO_MD4 0 +#define HASH_ALGO_MD5 1 +#define HASH_ALGO_SHA1 2 +#define HASH_ALGO_RIPE_MD_160 3 +#define HASH_ALGO_SHA256 4 +#define HASH_ALGO_SHA384 5 +#define HASH_ALGO_SHA512 6 +#define HASH_ALGO_SHA224 7 +#define HASH_ALGO_RIPE_MD_128 8 +#define HASH_ALGO_RIPE_MD_256 9 +#define HASH_ALGO_RIPE_MD_320 10 +#define HASH_ALGO_WP_256 11 +#define HASH_ALGO_WP_384 12 +#define HASH_ALGO_WP_512 13 +#define HASH_ALGO_TGR_128 14 +#define HASH_ALGO_TGR_160 15 +#define HASH_ALGO_TGR_192 16 + + +struct elf_payload_signature { + u8 algo; /* Public-key crypto algorithm PKEY_ALGO_*. */ + u8 hash; /* Digest algorithm: HASH_ALGO_*. */ + u8 id_type; /* Key identifier type PKEY_ID*. */ + u8 signer_len; /* Length of signer's name */ + u8 key_id_len; /* Length of key identifier */ + u8 __pad[3]; + __be32 sig_len; /* Length of signature data */ +}; + +</pre> +(Note that this has been borrowed from Linux module signature code.). + + +### .bss and .data sections. + +In place patching writable data is not suitable as it is unclear what should be done +depending on the current state of data. As such it should not be attempted. + +That said we should provide hook functions so that the existing data +can be changed during payload application. + + +### Inline patching + +The hypervisor should verify that the in-place patching would fit within +the code or data. + +### Trampoline (e9 opcode) + +The e9 opcode used for jmpq uses a 32-bit signed displacement. That means +we are limited to up to 2GB of virtual address to place the new code +from the old code. That should not be a problem since Xen hypervisor has +a very small footprint. + +However if we need - we can always add two trampolines. One at the 2GB +limit that calls the next trampoline. + +Please note there is a small limitation for trampolines in +function entries: The target function (+ trailing padding) must be able +to accomodate the trampoline. On x86 with +-2 GB relative jumps, +this means 5 bytes are required. + +Depending on compiler settings, there are several functions in Xen that +are smaller (without inter-function padding). + +<pre> +readelf -sW xen-syms | grep " FUNC " | \ + awk '{ if ($3 < 5) print $3, $4, $5, $8 }' + +... +3 FUNC LOCAL wbinvd_ipi +3 FUNC LOCAL shadow_l1_index +... +</pre> +A compile-time check for, e.g., a minimum alignment of functions or a +runtime check that verifies symbol size (+ padding to next symbols) for +that in the hypervisor is advised. + +The tool for generating payloads currently does perform a compile-time +check to ensure that the function to be replaced is large enough. +
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 4/27 xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
In reply to Konrad Rzeszutek WilkThe implementation does not actually do any patching.
It just adds the framework for doing the hypercalls, keeping track of ELF payloads, and the basic operations:
- query which payloads exist,
- query for specific payloads,
- check*1, apply*1, replace*1, and unload payloads.
*1: Which of course in this patch are nops.
The functionality is disabled on ARM until all arch components are implemented.
Also by default it is disabled until the implementation is in place.
We also use recursive spinlocks to so that the find_payload function does not need to have a 'lock' and 'non-lock' variant.
---
Cc: Daniel De Graaf <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
v2: Rebased on keyhandler: rework keyhandler infrastructure
v3: Fixed XSM.
- Removed REVERTED state.
Split status and error code.
Add REPLACE action.
Separate payload data from the payload structure. s/XSPLICE_ID_../XSPLICE_NAME_../
- Add xsplice and CONFIG_XSPLICE build toption.
Fix code per Jan's review.
Update the sysctl.h (change bits to enum like)
- Rebase on Kconfig changes.
- Add missing pad checks. Re-order keyhandler.h to build on ARM.
- Rebase on build: hook the schedulers into Kconfig
- s/id/name/; s/payload_list_lock/payload_lock/
- Put #ifdef CONFIG_XSPLICE in header file per Doug review.
- Andrew review:
- use recursive spinlocks, change name to xsplice_op,
sprinkle new-lines, add local variable block, include
state diagram, squash two goto labels, use vzalloc instead of alloc_xenheap_pages.
- change 'state' from int32 to uint32_t
- remove the err label out of xsplice_upload
- use void* instaed of uint8_t
- move code around to make it easier to read.
- Add vmap.h to compiler under ARM.
- Add missing Copyright in header file
- Dropped LOADED state, make the payload go in CHECKED.
v4: Made it only work on x86 per Julien's (ARM) maintainer request.
v5: Dropped the load->check state example in sysctl.h
Made the ->nr=0 call work. Remove rc=0 in lots of cases. Update header from design doc.
v6: Update what 'idx' means. Don't drop lock in find_payload. Make
find_name copy data.
v7: Don't print -EINVAL when payload_cnt is zero (and toolstack provides
idx as zero). Change return code to -ENOSYS, so change callback setting based on -ENOSYS and -EOPNOTSUPP.
Add extra printk in keyhandler.
Use if (..) else in xsplice_upload instead of two 'if'.
Remove #ifdef in XSM machinery.
Add Andrew's Reviewed-by.
Rebase on x86/cpu: Sysctl and common infrastructure for levelling context switching and xen+tools: Export maximum host and guest cpu featuresets via SYSCTL
---
--- tools/flask/policy/policy/modules/xen/xen.te | 1 + xen/common/Kconfig | 12 + xen/common/Makefile | 1 + xen/common/sysctl.c | 6 + xen/common/xsplice.c | 404 +++++++++++++++++++++++++++ xen/include/public/sysctl.h | 166 +++++++++++ xen/include/xen/xsplice.h | 35 +++ xen/xsm/flask/hooks.c | 4 + xen/xsm/flask/policy/access_vectors | 2 + 9 files changed, 631 insertions(+) create mode 100644 xen/common/xsplice.c create mode 100644 xen/include/xen/xsplice.h
tools/flask/policy/policy/modules/xen/xen.te +1 -0
@@ -74,6 +74,7 @@ allow dom0_t xen_t:xen2 { get_symbol get_cpu_levelling_caps get_cpu_featureset + xsplice_op }; # Allow dom0 to use all XENVER_ subops that have checks.
xen/common/Kconfig +12 -0
@@ -188,4 +188,16 @@ config SCHED_DEFAULT endmenu +# Enable/Disable xsplice support +config XSPLICE + bool "xSplice live patching support" + default n + depends on X86 + ---help--- + Allows a running Xen hypervisor to be dynamically patched using + binary patches without rebooting. This is primarily used to binarily + patch in the field an hypervisor with XSA fixes. + + If unsure, say Y. + endmenu
xen/common/Makefile +1 -0
@@ -57,6 +57,7 @@ obj-y += vsprintf.o obj-y += wait.o obj-$(CONFIG_XENOPROF) += xenoprof.o obj-y += xmalloc_tlsf.o +obj-$(CONFIG_XSPLICE) += xsplice.o obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
xen/common/sysctl.c +6 -0
@@ -28,6 +28,7 @@#include <xsm/xsm.h> #include <xen/pmstat.h> #include <xen/gcov.h> +#include <xen/xsplice.h> long do_sysctl (XEN_GUEST_HANDLE_PARAM (xen_sysctl_t) u_sysctl) {
@@ -460,6 +461,11 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =tmem_control (&op->u.tmem_op);break ; +case XEN_SYSCTL_xsplice_op: + ret =xsplice_op (&op->u.xsplice); + copyback = (ret == -ENOSYS || ret == -EOPNOTSUPP ) ?0 :1 ; +break ; +default : ret =arch_do_sysctl (op, u_sysctl); copyback =0 ;
xen/common/xsplice.c new file +404 -0
404 lines not on this page · raw message
xen/include/public/sysctl.h +166 -0
184 lines not on this page · raw message
xen/include/xen/xsplice.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. +* +*/ + +#ifndef __XEN_XSPLICE_H__ +#define __XEN_XSPLICE_H__ + +struct xen_sysctl_xsplice_op ; + +#ifdef CONFIG_XSPLICE + +int xsplice_op (struct xen_sysctl_xsplice_op *); + +#else + +#include <xen/errno.h> /* For -ENOSYS */ +static inline int xsplice_op (struct xen_sysctl_xsplice_op *op) +{ +return -ENOSYS ; +} + +#endif /* CONFIG_XSPLICE */ + +#endif /* __XEN_XSPLICE_H__ */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/xsm/flask/hooks.c +4 -0
@@ -814,6 +814,10 @@ static int flask_sysctl(int cmd)case XEN_SYSCTL_get_cpu_featureset:return domain_has_xen (current->domain,XEN2__GET_CPU_FEATURESET ); +case XEN_SYSCTL_xsplice_op: +return avc_current_has_perm (SECINITSID_XEN ,SECCLASS_XEN2 , +XEN2__XSPLICE_OP ,NULL ); +default :printk ("flask_sysctl: Unknown op %d\n" , cmd);return -EPERM ;
xen/xsm/flask/policy/access_vectors +2 -0
@@ -97,6 +97,8 @@ class xen2 get_cpu_levelling_caps # XEN_SYSCTL_get_cpu_featureset get_cpu_featureset +# XEN_SYSCTL_xsplice_op + xsplice_op } # Classes domain and domain2 consist of operations that a domain performs on
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 466
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/sysctl.c +3
-0@@ -460,6 +461,11 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =tmem_control (&op->u.tmem_op);break ; +case XEN_SYSCTL_xsplice_op: + ret =xsplice_op (&op->u.xsplice); + copyback = (ret == -ENOSYS || ret == -EOPNOTSUPP ) ?0 :1 ;
Why use a conditional expression here when its condition already is a boolean one just needing negating?
xen/common/xsplice.c line 47
Quoted 13 lines
xen/common/xsplice.c new file +13
-0+static int verify_name (const xen_xsplice_name_t *name,char *n) +{ +if ( !name->size || name->size >XEN_XSPLICE_NAME_SIZE ) +return -EINVAL ; + +if ( name->pad[0 ] || name->pad[1 ] || name->pad[2 ] ) +return -EINVAL ; + +if ( !guest_handle_okay (name->name, name->size) ) +return -EINVAL ; + +if (__copy_from_guest (n, name->name, name->size) ) +return -EFAULT ;
Is there a particular reason why you open code copy_from_guest() here? And considering that you now also read the string here, isn't the function name somewhat off?
Quoted 15 lines
xen/common/xsplice.c new file +15
-0+static int xsplice_upload (xen_sysctl_xsplice_upload_t *upload) +{ +struct payload *data =NULL , *found; +char n[XEN_XSPLICE_NAME_SIZE ]; +int rc; + + rc =verify_payload (upload, n); +if ( rc ) +return rc; + +spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + {
xen/common/xsplice.c line 122
Quoted 10 lines
xen/common/xsplice.c new file +10
-0+ rc =PTR_ERR (found); + goto out; + } +else if ( found ) + { + rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);
I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
Quoted 11 lines · Jan Beulich
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 466
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/sysctl.c +3
-0@@ -460,6 +461,11 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =tmem_control (&op->u.tmem_op);break ; +case XEN_SYSCTL_xsplice_op: + ret =xsplice_op (&op->u.xsplice); + copyback = (ret == -ENOSYS || ret == -EOPNOTSUPP ) ?0 :1 ;Why use a conditional expression here when its condition already is a boolean one just needing negating?
B/c I thought you would want it this way.
I changed it to
466 if ( ret != -ENOSYS && ret != -EOPNOTSUPP )
467 copyback = 1;
But I don't think this what you meant by 'negating'? As in:
copyback = !rc ?
But one of the subops returns the number of items and we definitly want copyback=1 for that.
Quoted 10 lines
Quoted 10 lines
xen/common/xsplice.c new file +9
-0+static int verify_name (const xen_xsplice_name_t *name,char *n) +{ +if ( !name->size || name->size >XEN_XSPLICE_NAME_SIZE ) +return -EINVAL ; + +if ( name->pad[0 ] || name->pad[1 ] || name->pad[2 ] ) +return -EINVAL ; + +if ( !guest_handle_okay (name->name, name->size) )
Quoted 8 lines
xen/common/xsplice.c line 47
Quoted 4 lines
xen/common/xsplice.c new file +4
-0+return -EINVAL ; + +if (__copy_from_guest (n, name->name, name->size) ) +return -EFAULT ;Is there a particular reason why you open code
copy_from_guest()here? And considering that you now also read the string here, isn't the function name somewhat off?
Yes. In the earlier versions I only verified the name->name and then later on made the copy. You pointed out that in effect I had invalidated the earlier checks. Also you asked to make the
name have a NUL padding - so I put that together - the verification here checks the name for this. And since to verify I need to copy it in first..
I will change the name of the function to 'get_name'
Quoted 21 lines
Quoted 21 lines
xen/common/xsplice.c new file +20
-0+static int xsplice_upload (xen_sysctl_xsplice_upload_t *upload) +{ +struct payload *data =NULL , *found; +char n[XEN_XSPLICE_NAME_SIZE ]; +int rc; + + rc =verify_payload (upload, n); +if ( rc ) +return rc; + +spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + { + rc =PTR_ERR (found); + goto out; + } +else if ( found ) + {
Quoted 8 lines
xen/common/xsplice.c line 122
Quoted 5 lines
xen/common/xsplice.c new file +5
-0+ rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
I can certainly unlock and then lock again (when adding it to the list).
That has the positive aspect that in the later patches when we do vzalloc for the payload we can do it without holding locks.
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
Quoted 14 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/15/16 4:29 AM >>>
Quoted 11 lines · Jan Beulich
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 466
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/sysctl.c +3
-0@@ -460,6 +461,11 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =tmem_control (&op->u.tmem_op);break ; +case XEN_SYSCTL_xsplice_op: + ret =xsplice_op (&op->u.xsplice); + copyback = (ret == -ENOSYS || ret == -EOPNOTSUPP ) ?0 :1 ;Why use a conditional expression here when its condition already is a boolean one just needing negating?
Quoted 6 lines
B/c I thought you would want it this way.
I changed it to
466 if ( ret !=
-ENOSYS&& ret !=-EOPNOTSUPP)
467 copyback = 1;
That's fine too.
Quoted 6 lines
But I don't think this what you meant by 'negating'? As in:
copyback = !rc ?
But one of the subops returns the number of items and we definitly want copyback=1 for that.
What I mean with "negating" was
copyback = (ret != -ENOSYS && ret != -EOPNOTSUPP);
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
xen/common/xsplice.c new file +10
-0+spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + { + rc =PTR_ERR (found); + goto out; + } +else if ( found ) + {Quoted 8 lines
xen/common/xsplice.c line 122
Quoted 5 lines
xen/common/xsplice.c new file +5
-0+ rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
Quoted 2 lines
I can certainly unlock and then lock again (when adding it to the list).
That would create a race again afaict. Instead what I have been trying to hint at is that the allocation should be done before taking the lock, freeing the object again if in the end it turned out it's not going to be needed. Hence the referral to possibly complicating the code.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
Quoted 53 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/15/16 4:29 AM >>>
Quoted 11 lines · Jan Beulich
xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 466
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/sysctl.c +3
-0@@ -460,6 +461,11 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =tmem_control (&op->u.tmem_op);break ; +case XEN_SYSCTL_xsplice_op: + ret =xsplice_op (&op->u.xsplice); + copyback = (ret == -ENOSYS || ret == -EOPNOTSUPP ) ?0 :1 ;Why use a conditional expression here when its condition already is a boolean one just needing negating?
Quoted 6 lines
B/c I thought you would want it this way.
I changed it to
466 if ( ret !=
-ENOSYS&& ret !=-EOPNOTSUPP)
467 copyback = 1;That's fine too.
Quoted 6 lines
But I don't think this what you meant by 'negating'? As in:
copyback = !rc ?
But one of the subops returns the number of items and we definitly want copyback=1 for that.
What I mean with "negating" was
copyback = (ret !=
-ENOSYS&& ret !=-EOPNOTSUPP);Quoted 19 lines
Quoted 18 lines
Quoted 5 lines
xen/common/xsplice.c new file +5
-0+spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + {xen/common/xsplice.c line 122
Quoted 10 lines
xen/common/xsplice.c new file +10
-0+ rc =PTR_ERR (found); + goto out; + } +else if ( found ) + { + rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
Quoted 6 lines
Quoted 2 lines
I can certainly unlock and then lock again (when adding it to the list).
That would create a race again afaict. Instead what I have been trying to hint at is that the allocation should be done before taking the lock, freeing the object again if in the end it turned out it's not going to be needed. Hence the referral to
What if I get -ENOMEM and that the user supplied an payload we already have? In that case I would return -ENOMEM while I would expect us to return -EEXIST.
Unless I add some extra checks to continue on?
Also one could do a bit of memory DoS (perhaps by mistake) by continously uploading the same and same payload and us first allocating the memory, and then doing the check for the payload existence (which would then free the memory). Since the allocation is outside the lock we can eat a bit of memory.
Quoted 4 lines
possibly complicating the code.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
Quoted 31 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/18/16 9:50 AM >>>
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/15/16 4:29 AM >>>
Quoted 19 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/xsplice.c new file +5
-0+spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + {xen/common/xsplice.c line 122
Quoted 10 lines
xen/common/xsplice.c new file +10
-0+ rc =PTR_ERR (found); + goto out; + } +else if ( found ) + { + rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
Quoted 6 lines
Quoted 2 lines
I can certainly unlock and then lock again (when adding it to the list).
That would create a race again afaict. Instead what I have been trying to hint at is that the allocation should be done before taking the lock, freeing the object again if in the end it turned out it's not going to be needed. Hence the referral to
Quoted 5 lines
What if I get
-ENOMEMand that the user supplied an payload we already have? In that case I would return-ENOMEMwhile I would expect us to return-EEXIST.Unless I add some extra checks to continue on?
Or unless you didn't check the allocation result right after the allocation call, but only where you check it now.
Quoted 5 lines
Also one could do a bit of memory DoS (perhaps by mistake) by continously uploading the same and same payload and us first allocating the memory, and then doing the check for the payload existence (which would then free the memory). Since the allocation is outside the lock we can eat a bit of memory.
Why that? You'd free the memory right away on the error path.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 04/27] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op
Quoted 31 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/18/16 9:50 AM >>>
Quoted 28 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/15/16 4:29 AM >>>
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:05 AM >>>
xen/common/xsplice.c new file +10
-0+spin_lock (&payload_lock); + + found =find_payload (n); +if (IS_ERR (found) ) + { + rc =PTR_ERR (found); + goto out; + } +else if ( found ) + {Quoted 8 lines
xen/common/xsplice.c line 122
Quoted 5 lines
xen/common/xsplice.c new file +5
-0+ rc = -EEXIST ; + goto out; + } + + data = xzalloc(struct payload);I generally advocate for not doing allocations with locks held, and I don't think it would severely complicate the code here doing so.
Quoted 2 lines
I can certainly unlock and then lock again (when adding it to the list).
That would create a race again afaict. Instead what I have been trying to hint at is that the allocation should be done before taking the lock, freeing the object again if in the end it turned out it's not going to be needed. Hence the referral to
Quoted 8 lines
Quoted 5 lines
What if I get
-ENOMEMand that the user supplied an payload we already have? In that case I would return-ENOMEMwhile I would expect us to return-EEXIST.Unless I add some extra checks to continue on?
Or unless you didn't check the allocation result right after the allocation call, but only where you check it now.
Which is OK for data = xzalloc(struct payload);
That is not much data.
The issue is later (the 'Implement payload loading') is the allocation of the raw_data which wants memory of size by ELF file.
This can be say 100kB or such.
Quoted 11 lines
Quoted 6 lines
Also one could do a bit of memory DoS (perhaps by mistake) by continously uploading the same and same payload and us first allocating the memory, and then doing the check for the payload existence (which would then free the memory). Since the allocation is outside the lock we can eat a bit of memory.
Why that? You'd free the memory right away on the error path.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 5/27 libxc: Implementation of XEN_XSPLICE_op in libxc
In reply to Konrad Rzeszutek WilkThe underlaying toolstack code to do the basic operations when using the XEN_XSPLICE_op syscalls:
- upload the payload,
- get status of an payload,
- list all the payloads,
- apply, check, replace, and revert the payload.
---
Cc: Ian Jackson <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
v2: Actually set zero for the _pad entries.
v3: Split status into state and error code.
Add REPLACE action.
- Use timeout and utilize pads.
- Update per Wei's review.
- Extra space slipped in, remove it
v4: Add Wei's review, update comment and Ack.
v7: Sprinkle errno=-EINVAL on all the 'if (!len)', etc checks.
Added Reviewed-by from Andrew.
---
--- tools/libxc/include/xenctrl.h | 62 ++++++++ tools/libxc/xc_misc.c | 355 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 417 insertions(+)
tools/libxc/include/xenctrl.h +62 -0
@@ -2610,6 +2610,68 @@ const uint32_t *xc_get_feature_deep_deps(uint32_t feature);#endif +int xc_xsplice_upload (xc_interface *xch, +char *name,unsigned char *payload,uint32_t size); + +int xc_xsplice_get (xc_interface *xch, +char *name, +xen_xsplice_status_t *status); + +/* +* The heart of this function is to get an array of xen_xsplice_status_t. +* +* However it is complex because it has to deal with the hypervisor +* returning some of the requested data or data being stale +* (another hypercall might alter the list). +* +* The parameters that the function expects to contain data from +* the hypervisor are: 'info', 'name', and 'len'. The 'done' and +* 'left' are also updated with the number of entries filled out +* and respectively the number of entries left to get from hypervisor. +* +* It is expected that the caller of this function will take the +* 'left' and use the value for 'start'. This way we have an +* cursor in the array. Note that the 'info','name', and 'len' will +* be updated at the subsequent calls. +* +* The 'max' is to be provided by the caller with the maximum +* number of entries that 'info', 'name', and 'len' arrays can +* be filled up with. +* +* Each entry in the 'name' array is expected to be of XEN_XSPLICE_NAME_SIZE +* length. +* +* Each entry in the 'info' array is expected to be of xen_xsplice_status_t +* structure size. +* +* Each entry in the 'len' array is expected to be of uint32_t size. +* +* The return value is zero if the hypercall completed successfully. +* Note that the return value is _not_ the amount of entries filled +* out - that is saved in 'done'. +* +* If there was an error performing the operation, the return value +* will contain an negative -EXX type value. The 'done' and 'left' +* will contain the number of entries that had been succesfully +* retrieved (if any). +*/ +int xc_xsplice_list (xc_interface *xch,unsigned int max,unsigned int start, +xen_xsplice_status_t *info,char *name, +uint32_t *len,unsigned int *done, +unsigned int *left); + +/* +* The operations are asynchronous and the hypervisor may take a while +* to complete them. The `timeout` offers an option to expire the +* operation if it could not be completed within the specified time +* (in ms). Value of 0 means let hypervisor decide the best timeout. +*/ +int xc_xsplice_apply (xc_interface *xch,char *name,uint32_t timeout); +int xc_xsplice_revert (xc_interface *xch,char *name,uint32_t timeout); +int xc_xsplice_unload (xc_interface *xch,char *name,uint32_t timeout); +int xc_xsplice_check (xc_interface *xch,char *name,uint32_t timeout); +int xc_xsplice_replace (xc_interface *xch,char *name,uint32_t timeout); +/* Compat shims */ #include "xenctrl_compat.h"
tools/libxc/xc_misc.c +355 -0
361 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 6/27 xen-xsplice: Tool to manipulate xsplice payloads
In reply to Konrad Rzeszutek WilkA simple tool that allows an system admin to perform basic xsplice operations:
- Upload a xsplice file (with an unique name)
- List all the xsplice payloads loaded.
- Apply, revert, replace, or unload the payload using the unique name.
- Do all two - upload, and apply the payload in one go (load). Also will use the name of the file as the <name>
---
Cc: Ian Jackson <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
v2:
- Removed REVERTED state.
- Fixed bugs handling XSPLICE_STATUS_PROGRESS.
- Split status into state and error.
Add REPLACE action.
v3:
- Utilize the timeout and use the default one (let the hypervisor pick it).
- Change the s/all/load and infer the <id> from name of file.
- s/id/name/
- Don't use hypercall buffer in upload_func, instead do it in libxc
- Remove the debug printk.
- Remove goto's (per Wei's review)
- Use fprintf(stderr in error paths.
- Add local variable block.
- Syntax, expand comment, and don't overwrite rc if xc_xsplice_upload failed.
v4:
- Remove LOADED state. Only have CHECKED state.
---
--- .gitignore | 1 + tools/misc/Makefile | 4 + tools/misc/xen-xsplice.c | 463 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 468 insertions(+) create mode 100644 tools/misc/xen-xsplice.c
.gitignore +1 -0
@@ -182,6 +182,7 @@ tools/misc/xen_cpuperf tools/misc/xen-cpuid tools/misc/xen-detect tools/misc/xen-tmem-list-parse +tools/misc/xen-xsplice tools/misc/xenperf tools/misc/xenpm tools/misc/xen-hvmctx
tools/misc/Makefile +4 -0
@@ -32,6 +32,7 @@ INSTALL_SBIN += xenlockprof INSTALL_SBIN += xenperf INSTALL_SBIN += xenpm INSTALL_SBIN += xenwatchdogd +INSTALL_SBIN += xen-xsplice INSTALL_SBIN += $(INSTALL_SBIN-y) # Everything to be installed in a private bin/
@@ -103,6 +104,9 @@ xen-mfndump: xen-mfndump.o xenwatchdogd: xenwatchdogd.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) +xen-xsplice: xen-xsplice.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) + xen-lowmemd: xen-lowmemd.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
tools/misc/xen-xsplice.c new file +463 -0
463 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 7/27 arm/x86: Use struct virtual_region to do bug, symbol, and (x86) exception tables lookup.
In reply to Konrad Rzeszutek WilkDuring execution of the hypervisor we have two regions of executable code - stext -> _etext, and _sinittext -> _einitext.
The later is not needed after bootup.
We also have various built-in macros and functions to search in between those two swaths depending on the state of the system.
That is either for bug_frames, exceptions (x86) or symbol names for the instruction.
With xSplice in the picture - we need a mechanism for new payloads to searched as well for all of this.
Originally we had extra 'if (xsplice)...' but that gets a bit tiring and does not hook up nicely.
This 'struct virtual_region' and virtual_region_list provide a mechanism to search for the bug_frames, exception table, and symbol names entries without having various calls in other sub-components in the system.
Code which wishes to participate in bug_frames and exception table entries search has to only use two public APIs:
- register_virtual_region
- unregister_virtual_region
to let the core code know.
If the ->lookup_symbol is not then the default internal symbol lookup mechanism is used.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v4: New patch.
v5:
- Rename to virtual_region.
- Ditch the 'skip' function.
- Remove the _stext.
- Use RCU lists.
- Add a search function.
- Remove extern, add rcu_read_lock. remove __ from name.
v6: s/search_for_text/find_text_region/.
- Drop the uaccess.h need. Make setup_virtual_regions accept two parameters. Remove #ifdef.
- Constify struct exception_table_entry.
- Remove some newlines.
- Change header file guard #define to proper name.
---
--- xen/arch/arm/setup.c | 4 ++ xen/arch/arm/traps.c | 39 +++++++---- xen/arch/x86/extable.c | 12 +++- xen/arch/x86/setup.c | 6 ++ xen/arch/x86/traps.c | 40 ++++++----- xen/common/Makefile | 1 + xen/common/symbols.c | 11 ++- xen/common/virtual_region.c | 148 +++++++++++++++++++++++++++++++++++++++ xen/include/xen/symbols.h | 9 +++ xen/include/xen/virtual_region.h | 47 +++++++++++++ 10 files changed, 280 insertions(+), 37 deletions(-) create mode 100644 xen/common/virtual_region.c create mode 100644 xen/include/xen/virtual_region.h
xen/arch/arm/setup.c +4 -0
@@ -34,6 +34,7 @@#include <xen/keyhandler.h> #include <xen/cpu.h> #include <xen/pfn.h> +#include <xen/virtual_region.h> #include <xen/vmap.h> #include <xen/libfdt/libfdt.h> #include <xen/acpi.h>
@@ -860,6 +861,9 @@ void __init start_xen(unsigned long boot_phys_offset, system_state = SYS_STATE_active; +/* Must be done past setting system_state. */ +unregister_init_virtual_region (); +domain_unpause_by_systemcontroller (dom0);/* Switch on to the dynamically allocated stack for the idle vcpu
xen/arch/arm/traps.c +24 -15
@@ -31,6 +31,7 @@#include <xen/softirq.h> #include <xen/domain_page.h> #include <xen/perfc.h> +#include <xen/virtual_region.h> #include <public/sched.h> #include <public/xen.h> #include <asm/debugger.h>
@@ -101,6 +102,8 @@ integer_param("debug_stack_lines", debug_stack_lines);void init_traps (void ) { +setup_virtual_regions (NULL ,NULL ); +/* Setup Hyp vector base */ WRITE_SYSREG ((vaddr_t )hyp_traps_vector,VBAR_EL2 );
@@ -1116,27 +1119,33 @@ void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs)int do_bug_frame (struct cpu_user_regs *regs,vaddr_t pc) { -const struct bug_frame *bug; +const struct bug_frame *bug =NULL ;const char *prefix ="" , *filename, *predicate;unsigned long fixup; -int id, lineno; -static const struct bug_frame *const stop_frames[] = { - __stop_bug_frames_0, - __stop_bug_frames_1, - __stop_bug_frames_2, -NULL - }; +int id =-1 , lineno; +const struct virtual_region *region; -for ( bug = __start_bug_frames, id =0 ; stop_frames[id]; ++bug ) + region =find_text_region (pc); +if ( region ) { -while (unlikely (bug == stop_frames[id]) ) - ++id; +for ( id =0 ; id <BUGFRAME_NR ; id++ ) + { +const struct bug_frame *b; +unsigned int i; -if ( ((vaddr_t )bug_loc (bug)) == pc ) -break ; +for ( i =0 , b = region->frame[id].bugs; + i < region->frame[id].n_bugs; b++, i++ ) + { +if ( ((vaddr_t )bug_loc (b)) == pc ) + { + bug = b; + goto found; + } + } + } } - -if ( !stop_frames[id] ) + found: +if ( !bug )return -ENOENT ;/* WARN, BUG or ASSERT: decode the filename pointer and line number. */
xen/arch/x86/extable.c +9 -3
@@ -1,10 +1,12 @@ -#include <xen/config.h> #include <xen/init.h> +#include <xen/list.h> #include <xen/perfc.h> +#include <xen/rcupdate.h> #include <xen/sort.h> #include <xen/spinlock.h> #include <asm/uaccess.h> +#include <xen/virtual_region.h> #define EX_FIELD (ptr, field) ((unsigned long)&(ptr)->field + (ptr)->field)
@@ -80,8 +82,12 @@ search_one_table(const struct exception_table_entry *first,unsigned long search_exception_table (unsigned long addr) { -return search_one_table ( - __start___ex_table, __stop___ex_table-1 , addr); +const struct virtual_region *region =find_text_region (addr); + +if ( region && region->ex ) +return search_one_table (region->ex, region->ex_end -1 , addr); + +return 0 ; }unsigned long
xen/arch/x86/setup.c +6 -0
@@ -26,6 +26,7 @@#include <xen/pfn.h> #include <xen/nodemask.h> #include <xen/tmem_xen.h> +#include <xen/virtual_region.h> #include <xen/watchdog.h> #include <public/version.h> #include <compat/platform.h>
@@ -515,6 +516,9 @@ static void noinline init_done(void) system_state = SYS_STATE_active; +/* MUST be done prior to removing .init data. */ +unregister_init_virtual_region (); +domain_unpause_by_systemcontroller (hardware_domain);/* Zero the .init code and data. */
@@ -617,6 +621,8 @@ void __init noreturn __start_xen(unsigned long mbi_p)smp_prepare_boot_cpu ();sort_exception_tables (); +setup_virtual_regions (__start___ex_table, __stop___ex_table); +/* Full exception support from here on in. */ loader = (mbi->flags &MBI_LOADERNAME )
xen/arch/x86/traps.c +24 -16
@@ -48,6 +48,7 @@#include <xen/kexec.h> #include <xen/trace.h> #include <xen/paging.h> +#include <xen/virtual_region.h> #include <xen/watchdog.h> #include <asm/system.h> #include <asm/io.h>
@@ -1229,18 +1230,12 @@ static int emulate_forced_invalid_op(struct cpu_user_regs *regs)void do_invalid_op (struct cpu_user_regs *regs) { -const struct bug_frame *bug; +const struct bug_frame *bug =NULL ;u8 bug_insn[2 ];const char *prefix ="" , *filename, *predicate, *eip = (char *)regs->eip;unsigned long fixup; -int id, lineno; -static const struct bug_frame *const stop_frames[] = { - __stop_bug_frames_0, - __stop_bug_frames_1, - __stop_bug_frames_2, - __stop_bug_frames_3, -NULL - }; +int id =-1 , lineno; +const struct virtual_region *region;DEBUGGER_trap_entry (TRAP_invalid_op, regs);
@@ -1257,16 +1252,29 @@ void do_invalid_op(struct cpu_user_regs *regs) memcmp(bug_insn, "\xf\xb", sizeof(bug_insn)) ) goto die; -for ( bug = __start_bug_frames, id =0 ; stop_frames[id]; ++bug ) + region =find_text_region (regs->eip); +if ( region ) { -while (unlikely (bug == stop_frames[id]) ) - ++id; -if (bug_loc (bug) == eip ) -break ; +for ( id =0 ; id <BUGFRAME_NR ; id++ ) + { +const struct bug_frame *b; +unsigned int i; + +for ( i =0 , b = region->frame[id].bugs; + i < region->frame[id].n_bugs; b++, i++ ) + { +if (bug_loc (b) == eip ) + { + bug = b; + goto found; + } + } + } } -if ( !stop_frames[id] ) - goto die; + found: +if ( !bug ) + goto die; eip +=sizeof (bug_insn);if ( id == BUGFRAME_run_fn ) {
xen/common/Makefile +1 -0
@@ -51,6 +51,7 @@ obj-y += time.o obj-y += timer.o obj-y += trace.o obj-y += version.o +obj-y += virtual_region.o obj-y += vm_event.o obj-y += vmap.o obj-y += vsprintf.o
xen/common/symbols.c +8 -3
@@ -17,6 +17,7 @@#include <xen/lib.h> #include <xen/string.h> #include <xen/spinlock.h> +#include <xen/virtual_region.h> #include <public/platform.h> #include <xen/guest_access.h>
@@ -97,8 +98,7 @@ static unsigned int get_symbol_offset(unsigned long pos)bool_t is_active_kernel_text (unsigned long addr) { -return (is_kernel_text (addr) || - (system_state < SYS_STATE_active &&is_kernel_inittext (addr))); +return !!find_text_region (addr); } const char *symbols_lookup(unsigned long addr,
@@ -108,13 +108,18 @@ const char *symbols_lookup(unsigned long addr, {unsigned long i, low, high, mid;unsigned long symbol_end =0 ; +const struct virtual_region *region; namebuf[KSYM_NAME_LEN ] =0 ; namebuf[0 ] =0 ; -if (!is_active_kernel_text (addr)) + region =find_text_region (addr); +if (!region)return NULL ; +if (region->symbols_lookup) +return region->symbols_lookup (addr, symbolsize, offset, namebuf); +/* do a binary search on the sorted symbols_addresses array */ low =0 ; high = symbols_num_syms;
xen/common/virtual_region.c new file +148 -0
148 lines not on this page · raw message
xen/include/xen/symbols.h +9 -0
@@ -5,6 +5,15 @@#define KSYM_NAME_LEN 127 +/* +* Typedef for the callback functions that symbols_lookup +* can call if virtual_region_list has an callback for it. +*/ +typedef const char *symbols_lookup_t (unsigned long addr, +unsigned long *symbolsize, +unsigned long *offset, +char *namebuf); +/* Lookup an address. */ const char *symbols_lookup(unsigned long addr, unsigned long *symbolsize,
xen/include/xen/virtual_region.h new file +47 -0
47 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 07/27] arm/x86: Use struct virtual_region to do bug, symbol, and (x86) exception tables lookup.
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:04 AM >>>
+/*
+ * RCU locking. Additions are done either at startup (when there is only
+ * one CPU) or when all CPUs are running without IRQs.
+ *
+ * Deletions are big tricky. We do it when xSplicing (all CPUs running
"big tricky"?
Quoted 8 lines
+ * without IRQs) or during bootup (when clearing the init).
+ *
+ * Hence we uselist_del_rcu(which sports an memory fence) and a spinlock
+ * on deletion.
+ *
+ * All readers ofvirtual_region_listMUST use listlist_for_each_entry_rcu.
+ *
+ */
Stray empty comment line.
xen/include/xen/virtual_region.h line 6
Quoted 9 lines
xen/include/xen/virtual_region.h new file +6
-0@@ -0,0 +1,47 @@ +/* +* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. +* +*/ + +#ifndef __XEN_VIRTUAL_REGION__
__XEN_VIRTUAL_REGION_H__
With those minor aspects taken care of,
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 8/27 arm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type
In reply to Konrad Rzeszutek WilkFor those users who want to use the virtual addresses that are in the hypervisor's code/data/ region address space - these three new functions allow that.
Implementation wise the vmap API keeps track of two virtual address regions now:
a) VMAP_VIRT_START
b) Any provided virtual address space (need start and end).
The a) one is the default one and the existing behavior for users of vmalloc, vmap, etc is the same.
If however one wishes to use the b) one only has to use the vm_init_type to initialize and the vmalloc_type to utilize it.
This allows users (such as xSplice) to provide their own mechanism to change the the page flags, and also use virtual addresses closer to the hypervisor virtual addresses (at least on x86) while not having to deal with the allocation of pages.
For example of users, see patch titled "xsplice: Implement payload loading", where we parse the payload's ELF relocations - which is defined to be signed 32-bit (so max displacement is 2GB virtual spacE). The displacement of the hypervisor virtual addresses to the vmalloc (on x86) is more than 32-bits - which means that ELF relocations would truncate the 34 and 33th bit. Hence this alternate API.
We also add add extra checks in case the b) range has not been initialized.
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
v4: New patch.
v5: Update per Jan's comments.
v6: Drop the stray parentheses on typedefs.
Ditch the vunmap callback. Stash away the virtual addresses in lists. Ditch the vmap callback. Just provide virtual address. Ditch the vmalloc_range. Require users of alternative virtual address to call vmap_init_type first.
v7: Don't expose the vmalloc_type and such. Instead provide an wrapper
called vmalloc_xen for those.
Rename the enum, change one of the names.
Moved the vunmap_type around in c file so we don't have to declare it in the header.
---
--- xen/arch/arm/kernel.c | 2 +- xen/arch/arm/mm.c | 2 +- xen/arch/x86/mm.c | 2 +- xen/common/vmap.c | 202 ++++++++++++++++++++++++++++++------------------- xen/drivers/acpi/osl.c | 2 +- xen/include/xen/vmap.h | 21 ++++- 6 files changed, 148 insertions(+), 83 deletions(-)
xen/arch/arm/kernel.c +1 -1
@@ -299,7 +299,7 @@ static __init int kernel_decompress(struct bootmodule *mod)return -ENOMEM ; } mfn =_mfn (page_to_mfn (pages)); - output =__vmap (&mfn,1 << kernel_order_out,1 ,1 ,PAGE_HYPERVISOR ); + output =__vmap (&mfn,1 << kernel_order_out,1 ,1 ,PAGE_HYPERVISOR ,VMAP_DEFAULT ); rc =perform_gunzip (output, input, size);clean_dcache_va_range (output, output_size);
xen/arch/arm/mm.c +1 -1
@@ -807,7 +807,7 @@ void *ioremap_attr(paddr_t pa, size_t len, unsigned int attributes)mfn_t mfn =_mfn (PFN_DOWN (pa));unsigned int offs = pa & (PAGE_SIZE -1 );unsigned int nr =PFN_UP (offs + len); -void *ptr =__vmap (&mfn, nr,1 ,1 , attributes); +void *ptr =__vmap (&mfn, nr,1 ,1 , attributes,VMAP_DEFAULT );if ( ptr ==NULL )return NULL ;
xen/arch/x86/mm.c +1 -1
@@ -6124,7 +6124,7 @@ void __iomem *ioremap(paddr_t pa, size_t len)unsigned int offs = pa & (PAGE_SIZE -1 );unsigned int nr =PFN_UP (offs + len); - va =__vmap (&mfn, nr,1 ,1 ,PAGE_HYPERVISOR_NOCACHE ) + offs; + va =__vmap (&mfn, nr,1 ,1 ,PAGE_HYPERVISOR_NOCACHE ,VMAP_DEFAULT ) + offs; }return (void __force __iomem *)va;
xen/common/vmap.c +126 -76
366 lines not on this page · raw message
xen/drivers/acpi/osl.c +1 -1
@@ -97,7 +97,7 @@ acpi_os_map_memory(acpi_physical_address phys, acpi_size size)if (IS_ENABLED (CONFIG_X86 ) && !((phys + size -1 ) >>20 ))return __va (phys);return __vmap (&mfn,PFN_UP (offs + size),1 ,1 , -ACPI_MAP_MEM_ATTR ) + offs; +ACPI_MAP_MEM_ATTR ,VMAP_DEFAULT ) + offs; }return __acpi_map_table (phys, size); }
xen/include/xen/vmap.h +18 -3
@@ -4,16 +4,28 @@#include <xen/mm.h> #include <asm/page.h> +enum vmap_region { +VMAP_DEFAULT , +VMAP_XEN , +VMAP_REGION_NR , +}; + +void vm_init_type (enum vmap_region type,void *start,void *end); +void *vm_alloc (unsigned int nr,unsigned int align);void vm_free (const void *); -void *__vmap (const mfn_t *mfn,unsigned int granularity, -unsigned int nr,unsigned int align,unsigned int flags); +void *__vmap (const mfn_t *mfn,unsigned int granularity,unsigned int nr, +unsigned int align,unsigned int flags,enum vmap_region );void *vmap (const mfn_t *mfn,unsigned int nr);void vunmap (const void *); +void *vmalloc (size_t size); +void *vmalloc_xen (size_t size); +void *vzalloc (size_t size);void vfree (void *va); +void vfree_xen (void *va);void __iomem *ioremap (paddr_t ,size_t );
@@ -24,7 +36,10 @@ static inline void iounmap(void __iomem *va) vunmap((void *)(addr & PAGE_MASK)); } -void vm_init (void );void *arch_vmap_virt_end (void ); +static inline void vm_init (void ) +{ +vm_init_type (VMAP_DEFAULT , (void *)VMAP_VIRT_START ,arch_vmap_virt_end ()); +}#endif /* __XEN_VMAP_H__ */
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 08/27] arm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Signed-off-by Konrad Rzeszutek Wilk <[email protected]>Suggested-by Jan Beulich <[email protected]>Reviewed-by Andrew Cooper <[email protected]>
Is that really the case? I think it was Andrew who suggested to not expose a second variant of vfree(), as the address type can be inferred from the address.
Review void *vm_alloc(unsigned int nr, unsigned int align)
Quoted 1 line
(quoted diff) +0
-0@@ -52,27 +55,30 @@ void *vm_alloc(unsigned int nr, unsigned int align)
>else if ( align & (align - 1) )
>align &= -align;
xen/common/vmap.c void *vm_alloc(unsigned int nr, unsigned int align) · line 59
Quoted 3 lines
xen/common/vmap.c +2
-0+if ( !vm_base[t] ) +return NULL ;
How about ASSERT()ing that t is in range before using it as an array index?
With the address/type parameter redundancy dropped at the very least from the non-internal functions, I think everything else here is fine.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 9/27 x86/mm: Introduce modify_xen_mappings()
In reply to Konrad Rzeszutek WilkFrom: Andrew Cooper <[email protected]>
To simply change the permissions on existing Xen mappings. The existing destroy_xen_mappings() is altered to support a change the PTE permissions.
A new destroy_xen_mappings() is introduced, as the special case of not passing _PAGE_PRESENT to modify_xen_mappings().
As cleanup (and an ideal functional test), the boot logic which remaps Xen's code and data with reduced permissions is altered to use modify_xen_mappings(), rather than map_pages_to_xen() passing the same mfn's back in.
---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>
v7: New
v8:
* Allow callers to pass PAGE_HYPERVISOR_xxx constants, for consistently with similar functions.
* Replace one opencoded 0 with its logical equivalent, _PAGE_NONE.
* Add check for creating new mappings of 4K entries.
* Correct the continue logic. Invert the sense of the comment to match the code.
* Added Reviewed-by George and Jan. Fix missing "are" in comment.
---
--- xen/arch/x86/mm.c | 75 +++++++++++++++++++++++++++++++++++++++++++++------- xen/arch/x86/setup.c | 22 +++++++-------- xen/include/xen/mm.h | 2 ++ 3 files changed, 76 insertions(+), 23 deletions(-)
xen/arch/x86/mm.c +65 -10
@@ -5956,7 +5956,19 @@ int populate_pt_range(unsigned long virt, unsigned long mfn,return map_pages_to_xen (virt, mfn, nr_mfns,MAP_SMALL_PAGES ); } -void destroy_xen_mappings (unsigned long s,unsigned long e) +/* +* Alter the permissions of a range of Xen virtual address space. +* +* Does not create new mappings, and does not modify the mfn in existing +* mappings, but will shatter superpages if necessary, and will destroy +* mappings if not passed _PAGE_PRESENT. +* +* The only flags considered are NX, RW and PRESENT. All other input flags +* are ignored. +* +* It is an error to call with present flags over an unpopulated range. +*/ +void modify_xen_mappings (unsigned long s,unsigned long e,unsigned int nf) {bool_t locking = system_state > SYS_STATE_boot;l2_pgentry_t *pl2e;
@@ -5964,6 +5976,10 @@ void destroy_xen_mappings(unsigned long s, unsigned long e)unsigned int i;unsigned long v = s; +/* Set of valid PTE bits which may be altered. */ +#define FLAGS_MASK (_PAGE_NX|_PAGE_RW|_PAGE_PRESENT) + nf &=FLAGS_MASK ; +ASSERT (IS_ALIGNED (s,PAGE_SIZE ));ASSERT (IS_ALIGNED (e,PAGE_SIZE ));
@@ -5973,6 +5989,9 @@ void destroy_xen_mappings(unsigned long s, unsigned long e)if ( !(l3e_get_flags (*pl3e) & _PAGE_PRESENT) ) { +/* Confirm the caller isn't trying to create new mappings. */ +ASSERT (!(nf & _PAGE_PRESENT)); + v +=1UL <<L3_PAGETABLE_SHIFT ; v &= ~((1UL <<L3_PAGETABLE_SHIFT ) -1 );continue ;
@@ -5984,8 +6003,12 @@ void destroy_xen_mappings(unsigned long s, unsigned long e) l1_table_offset(v) == 0 && ((e - v) >= (1UL << L3_PAGETABLE_SHIFT)) ) { -/* PAGE1GB: whole superpage is destroyed. */ -l3e_write_atomic (pl3e,l3e_empty ()); +/* PAGE1GB: whole superpage is modified. */ +l3_pgentry_t nl3e = !(nf & _PAGE_PRESENT) ?l3e_empty () + :l3e_from_pfn (l3e_get_pfn (*pl3e), + (l3e_get_flags (*pl3e) & ~FLAGS_MASK ) | nf); + +l3e_write_atomic (pl3e, nl3e); v +=1UL <<L3_PAGETABLE_SHIFT ;continue ; }
@@ -6016,6 +6039,9 @@ void destroy_xen_mappings(unsigned long s, unsigned long e)if ( !(l2e_get_flags (*pl2e) & _PAGE_PRESENT) ) { +/* Confirm the caller isn't trying to create new mappings. */ +ASSERT (!(nf & _PAGE_PRESENT)); + v +=1UL <<L2_PAGETABLE_SHIFT ; v &= ~((1UL <<L2_PAGETABLE_SHIFT ) -1 );continue ;
@@ -6026,8 +6052,12 @@ void destroy_xen_mappings(unsigned long s, unsigned long e)if ( (l1_table_offset (v) ==0 ) && ((e-v) >= (1UL <<L2_PAGETABLE_SHIFT )) ) { -/* PSE: whole superpage is destroyed. */ -l2e_write_atomic (pl2e,l2e_empty ()); +/* PSE: whole superpage is modified. */ +l2_pgentry_t nl2e = !(nf & _PAGE_PRESENT) ?l2e_empty () + :l2e_from_pfn (l2e_get_pfn (*pl2e), + (l2e_get_flags (*pl2e) & ~FLAGS_MASK ) | nf); + +l2e_write_atomic (pl2e, nl2e); v +=1UL <<L2_PAGETABLE_SHIFT ; }else
@@ -6055,13 +6085,27 @@ void destroy_xen_mappings(unsigned long s, unsigned long e) }else { +l1_pgentry_t nl1e; +/* Ordinary 4kB mapping. */ pl1e =l2e_to_l1e (*pl2e) +l1_table_offset (v); -l1e_write_atomic (pl1e,l1e_empty ()); + +/* Confirm the caller isn't trying to create new mappings. */ +if ( !(l1e_get_flags (*pl1e) & _PAGE_PRESENT) ) +ASSERT (!(nf & _PAGE_PRESENT)); + + nl1e = !(nf & _PAGE_PRESENT) ?l1e_empty () + :l1e_from_pfn (l1e_get_pfn (*pl1e), + (l1e_get_flags (*pl1e) & ~FLAGS_MASK ) | nf); + +l1e_write_atomic (pl1e, nl1e); v +=PAGE_SIZE ; -/* If we are done with the L2E, check if it is now empty. */ -if ( (v != e) && (l1_table_offset (v) !=0 ) ) +/* +* If we not destroying mappings, or are not done with the L2E, +* skip the empty&free check. +*/ +if ( (nf & _PAGE_PRESENT) || ((v != e) && (l1_table_offset (v) !=0 )) )continue ; pl1e =l2e_to_l1e (*pl2e);for ( i =0 ; i <L1_PAGETABLE_ENTRIES ; i++ )
@@ -6076,8 +6120,12 @@ void destroy_xen_mappings(unsigned long s, unsigned long e) } } -/* If we are done with the L3E, check if it is now empty. */ -if ( (v != e) && (l2_table_offset (v) +l1_table_offset (v) !=0 ) ) +/* +* If we are not destroying mappings, or not done with the L3E, +* skip the empty&free check. +*/ +if ( (nf & _PAGE_PRESENT) || + ((v != e) && (l2_table_offset (v) +l1_table_offset (v) !=0 )) )continue ; pl2e =l3e_to_l2e (*pl3e);for ( i =0 ; i <L2_PAGETABLE_ENTRIES ; i++ )
@@ -6093,10 +6141,17 @@ void destroy_xen_mappings(unsigned long s, unsigned long e) }flush_area (NULL ,FLUSH_TLB_GLOBAL ); + +#undef FLAGS_MASK }#undef flush_area +void destroy_xen_mappings (unsigned long s,unsigned long e) +{ +modify_xen_mappings (s, e, _PAGE_NONE); +} +void __set_fixmap (enum fixed_addresses idx,unsigned long mfn,unsigned long flags) {
xen/arch/x86/setup.c +9 -13
@@ -1220,23 +1220,19 @@ void __init noreturn __start_xen(unsigned long mbi_p)if ( !using_2M_mapping () ) {/* Mark .text as RX (avoiding the first 2M superpage). */ -map_pages_to_xen (XEN_VIRT_START +MB (2 ), -PFN_DOWN (__pa (XEN_VIRT_START +MB (2 ))), -PFN_DOWN (__2M_text_end - - (const char *)(XEN_VIRT_START +MB (2 ))), -PAGE_HYPERVISOR_RX ); +modify_xen_mappings (XEN_VIRT_START +MB (2 ), + (unsigned long )&__2M_text_end, +PAGE_HYPERVISOR_RX );/* Mark .rodata as RO. */ -map_pages_to_xen ((unsigned long )&__2M_rodata_start, -PFN_DOWN (__pa (__2M_rodata_start)), -PFN_DOWN (__2M_rodata_end - __2M_rodata_start), -PAGE_HYPERVISOR_RO ); +modify_xen_mappings ((unsigned long )&__2M_rodata_start, + (unsigned long )&__2M_rodata_end, +PAGE_HYPERVISOR_RO );/* Mark .data and .bss as RW. */ -map_pages_to_xen ((unsigned long )&__2M_rwdata_start, -PFN_DOWN (__pa (__2M_rwdata_start)), -PFN_DOWN (__2M_rwdata_end - __2M_rwdata_start), -PAGE_HYPERVISOR_RW ); +modify_xen_mappings ((unsigned long )&__2M_rwdata_start, + (unsigned long )&__2M_rwdata_end, +PAGE_HYPERVISOR_RW );/* Drop the remaining mappings in the shattered superpage. */ destroy_xen_mappings((unsigned long)&__2M_rwdata_end,
xen/include/xen/mm.h +2 -0
@@ -103,6 +103,8 @@ int map_pages_to_xen(unsigned long mfn, unsigned long nr_mfns, unsigned int flags); +/* Alter the permissions of a range of Xen virtual address space. */ +void modify_xen_mappings (unsigned long s,unsigned long e,unsigned int flags);void destroy_xen_mappings (unsigned long v,unsigned long e);/* * Create only non-leaf page table entries for the
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 09/27] x86/mm: Introduce modify_xen_mappings()
xen/arch/x86/mm.c void destroy_xen_mappings(unsigned long s, unsigned long e) · line 6106
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
xen/arch/x86/mm.c +3
-2-/* If we are done with the L2E, check if it is now empty. */ -if ( (v != e) && (l1_table_offset (v) !=0 ) ) +/* +* If we not destroying mappings, or are not done with the L2E, +* skip the empty&free check.
It would have been really nice if you had added the missing "are" ...
xen/arch/x86/mm.c void destroy_xen_mappings(unsigned long s, unsigned long e) · line 6125
Quoted 5 lines
xen/arch/x86/mm.c +3
-2-/* If we are done with the L3E, check if it is now empty. */ -if ( (v != e) && (l2_table_offset (v) +l1_table_offset (v) !=0 ) ) +/* +* If we are not destroying mappings, or not done with the L3E, +* skip the empty&free check.
... just like you did here.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 09/27] x86/mm: Introduce modify_xen_mappings()
Quoted 9 lines · Jan Beulich
xen/arch/x86/mm.c void destroy_xen_mappings(unsigned long s, unsigned long e) · line 6106
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
xen/arch/x86/mm.c +3
-2-/* If we are done with the L2E, check if it is now empty. */ -if ( (v != e) && (l1_table_offset (v) !=0 ) ) +/* +* If we not destroying mappings, or are not done with the L2E, +* skip the empty&free check. It would have been really nice if you had added the missing "are" ...
Quoted 7 lines
xen/arch/x86/mm.c void destroy_xen_mappings(unsigned long s, unsigned long e) · line 6125
Quoted 5 lines
xen/arch/x86/mm.c +3
-2-/* If we are done with the L3E, check if it is now empty. */ -if ( (v != e) && (l2_table_offset (v) +l1_table_offset (v) !=0 ) ) +/* +* If we are not destroying mappings, or not done with the L3E, +* skip the empty&free check. ... just like you did here.
<smack his head>
Yes. Fixed!
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 10/27 xsplice: Add helper elf routines
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Add Elf routines and data structures in preparation for loading an xSplice payload.
We make an assumption that the max number of sections an ELF payload can have is 64. We can in future make this be dependent on the names of the sections and verifying against a list, but for right now this suffices.
Also we a whole lot of checks to make sure that the ELF payload file is not corrupted nor that the offsets point past the file.
For most of the checks we print an message if the hypervisor is built with debug enabled.
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
v2: - With the #define ELFSIZE in the ARM file we can use the common
#defines instead of using #ifdef CONFIG_ARM_32. Moved to another
patch.
- Add checks for ELF file.
- Add name to be printed.
- Add len for easier ELF checks.
- Expand on the checks. Add macro.
v3: Remove the return_ macro
- Add return_ macro back but make it depend on debug=y
- Per Andrew review: add local variable. Fix memory leak in elf_resolve_sections, Remove macro and use dprintk. Fix alignment. Use void* instead of uint8_t to handle raw payload.
v4 - Fix memory leak in elf_get_sym
- Add XSPLICE to printk/dprintk
v5: Sprinkle newlines.
v6: Squash the ELF header checks from 'xsplice: Implement payload loading' here,
Do better job at checking string sections and the users of them (sh_size), Use XSPLICE as a string literal,
Move some checks outside the loop,
Make sure that SHT_STRTAB are really what they say
Sprinkle consts.
v7:
Check sh_entsize and sh_offset.
Added Andrew's Reviewed-by and Ian's Acked-by
Redo check on sh_entsize to not be !=
v8: Make all the dprintk(XENLOG_DEBUG be XENLOG_ERR
---
--- xen/common/Makefile | 1 + xen/common/xsplice_elf.c | 360 ++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/xsplice.h | 3 + xen/include/xen/xsplice_elf.h | 51 ++++++ 4 files changed, 415 insertions(+) create mode 100644 xen/common/xsplice_elf.c create mode 100644 xen/include/xen/xsplice_elf.h
xen/common/Makefile +1 -0
@@ -59,6 +59,7 @@ obj-y += wait.o obj-$(CONFIG_XENOPROF) += xenoprof.o obj-y += xmalloc_tlsf.o obj-$(CONFIG_XSPLICE) += xsplice.o +obj-$(CONFIG_XSPLICE) += xsplice_elf.o obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
xen/common/xsplice_elf.c new file +360 -0
360 lines not on this page · raw message
xen/include/xen/xsplice.h +3 -0
@@ -10,6 +10,9 @@ struct xen_sysctl_xsplice_op;#ifdef CONFIG_XSPLICE +/* Convenience define for printk. */ +#define XSPLICE "xsplice: " +int xsplice_op (struct xen_sysctl_xsplice_op *);#else
xen/include/xen/xsplice_elf.h new file +51 -0
51 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 10/27] xsplice: Add helper elf routines
xen/common/xsplice_elf.c line 27
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
xen/common/xsplice_elf.c new file +4
-0+static int elf_verify_strtab (const struct xsplice_elf_sec *sec) +{ +const Elf_Shdr *s; +const uint8_t *contents;
Considering it's a string table, perhaps better const char *?
xen/common/xsplice_elf.c line 35
Quoted 7 lines
+ s =
sec->sec;xen/common/xsplice_elf.c new file +6
-0+ +if ( s->sh_type !=SHT_STRTAB ) +return -EINVAL ; + +if ( !s->sh_size ) +return -EOPNOTSUPP ;
Why not also -EINVAL? I don't think the standard permits empty string table sections.
Quoted 1 line
+ contents = (const
uint8_t*)sec->data;
Pointless cast.
Quoted 9 lines
xen/common/xsplice_elf.c new file +9
-0+static int elf_resolve_sections (struct xsplice_elf *elf,const void *data) +{ +struct xsplice_elf_sec *sec; +unsigned int i; +Elf_Off delta; +int rc; + +/* xsplice_elf_load sanity checked e_shnum. */ + sec = xmalloc_array(struct xsplice_elf_sec, elf->hdr->e_shnum);
xen/common/xsplice_elf.c line 63
Quoted 10 lines
xen/common/xsplice_elf.c new file +10
-0+if ( !sec ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not allocate memory for section table!\n" , + elf->name); +return -ENOMEM ; + } + + elf->sec = sec; + + delta = elf->hdr->e_shoff + elf->hdr->e_shnum * elf->hdr->e_shentsize;
I think Andrew had asked this before - are we not worried of overflow here?
xen/common/xsplice_elf.c line 75
Quoted 12 lines
+ if ( delta >
elf->len)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Section table is past end of payload!\n",
+elf->name);
+ return-EINVAL;
+ }xen/common/xsplice_elf.c new file +6
-0+ +for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { + delta = elf->hdr->e_shoff + i * elf->hdr->e_shentsize; + + sec[i].sec = (void *)data + delta;
Casting away constness is one of the main reasons I complain about casts in general. If you really need to modify some fields in the section header, you should imo cast away constness just there. But even better would be if you left the section header alone.
Quoted 7 lines
+ delta = sec[i].sec->sh_offset;
+
+ /*
+ * N.B.elf_resolve_section_names,elf_get_symskip this check as
+ * we do it here.
+ */
+ if ( delta && (delta + sec[i].sec->sh_size >elf->len) )
Allowing delta to be zero here seems suspicious: Are you doing that because some sections come without data (and hence without offset)? In that case you'd better skip the check on those section types (e.g. SHT_NOBITS) only. In fact the offset being below the end of the ELF header would likely indicate a broken image.
Quoted 1 line
+ /* Name is populated in
xsplice_elf_sections_name. */
Stale function name afaict.
Quoted 7 lines
+ /*
+ *elf->symtab->sec->sh_linkwould point to the right section
+ * but we hadn't finished parsing all the sections.
+ */
+ if (elf->symtab->sec->sh_link>elf->hdr->e_shnum)= (and I know I had asked before to check all these bounds checks for off
by one errors)
Quoted 2 lines
+ if ( !elf->symtab->sec->sh_size ||
+elf->symtab->sec->sh_entsize<sizeof(Elf_Sym))
|| sh_size % sh_entsize
Quoted 4 lines
+ /*
+ * There can be multipleSHT_STRTAB(.shstrtab, .strtab) so pick one
+ * associated with the symbol table.
+ */
... pick the one ...
Quoted 27 lines
xen/common/xsplice_elf.c new file +27
-0+static int elf_resolve_section_names (struct xsplice_elf *elf,const void *data) +{ +const char *shstrtab; +unsigned int i; +Elf_Off offset, delta; +struct xsplice_elf_sec *sec; +int rc; + +/* +* The elf->sec[0 -> e_shnum] structures have been verified by +* elf_resolve_sections. Find file offset for section string table +* (normally called .shstrtab) +*/ + sec = &elf->sec[elf->hdr->e_shstrndx]; + + rc =elf_verify_strtab (sec); +if ( rc ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section string table is corrupted\n" , + elf->name); +return rc; + } + +/* Verified in elf_resolve_sections but just in case. */ + offset = sec->sec->sh_offset; +ASSERT (offset < elf->len && (offset + sec->sec->sh_size <= elf->len)); +
xen/common/xsplice_elf.c line 187
Quoted 10 lines
xen/common/xsplice_elf.c new file +10
-0+ shstrtab = data + offset; + +for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { + delta = elf->sec[i].sec->sh_name; + +if ( delta && delta >= sec->sec->sh_size ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: shstrtab [%u] data is past end of payload!\n" , + elf->name, i);
Isn't this redundant with the check in elf_resolve_sections()? Or is this needed because the one here runs before that other one?
Quoted 31 lines
xen/common/xsplice_elf.c new file +31
-0+static int elf_get_sym (struct xsplice_elf *elf,const void *data) +{ +const struct xsplice_elf_sec *symtab_sec, *strtab_sec; +struct xsplice_elf_sym *sym; +unsigned int i, delta, offset, nsym; + + symtab_sec = elf->symtab; + strtab_sec = elf->strtab; + +/* Pointers arithmetic to get file offset. */ + offset = strtab_sec->data - data; + +/* Checked already in elf_resolve_sections, but just in case. */ +ASSERT (offset == strtab_sec->sec->sh_offset); +ASSERT (offset < elf->len && (offset + strtab_sec->sec->sh_size <= elf->len)); + +/* symtab_sec->data was computed in elf_resolve_sections. */ +ASSERT ((symtab_sec->sec->sh_offset + data) == symtab_sec->data); + +/* No need to check values as elf_resolve_sections did it. */ + nsym = symtab_sec->sec->sh_size / symtab_sec->sec->sh_entsize; + + sym = xmalloc_array(struct xsplice_elf_sym, nsym); +if ( !sym ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not allocate memory for symbols\n" , + elf->name); +return -ENOMEM ; + } + +/* So we don't leak memory. */
xen/common/xsplice_elf.c line 237
Quoted 10 lines
xen/common/xsplice_elf.c new file +10
-0+ elf->sym = sym; + +for ( i =1 ; i < nsym; i++ ) + { +Elf_Sym *s = &((Elf_Sym *)symtab_sec->data)[i]; + +/* If st->name is STN_UNDEF zero, the check will always be true. */ + delta = s->st_name; + +if ( delta && (delta > strtab_sec->sec->sh_size) )
Now here I don't see why you special case delta being zero, the more with the comment right above.
Quoted 37 lines
xen/common/xsplice_elf.c new file +37
-0+static int xsplice_header_check (const struct xsplice_elf *elf) +{ +const Elf_Ehdr *hdr = elf->hdr; + +if (sizeof (*elf->hdr) > elf->len ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section header is bigger than payload!\n" , + elf->name); +return -EINVAL ; + } + +if ( !IS_ELF (*hdr) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Not an ELF payload!\n" , elf->name); +return -EINVAL ; + } + +if ( hdr->e_ident[EI_CLASS ] !=ELFCLASS64 || + hdr->e_ident[EI_DATA ] !=ELFDATA2LSB || + hdr->e_ident[EI_OSABI ] !=ELFOSABI_SYSV || + hdr->e_type !=ET_REL || + hdr->e_phnum !=0 ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Invalid ELF payload!\n" , elf->name); +return -EOPNOTSUPP ; + } + +if ( elf->hdr->e_shstrndx ==SHN_UNDEF ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section name idx is undefined!?\n" , + elf->name); +return -EINVAL ; + } + +/* Check that section name index is within the sections. */ +if ( elf->hdr->e_shstrndx >= elf->hdr->e_shnum ) + {
xen/common/xsplice_elf.c line 298
Quoted 10 lines
xen/common/xsplice_elf.c new file +10
-0+dprintk (XENLOG_ERR ,XSPLICE "%s: Section name idx (%u) is past end of sections (%u)!\n" , + elf->name, elf->hdr->e_shstrndx, elf->hdr->e_shnum); +return -EINVAL ; + } + +if ( elf->hdr->e_shnum >64 ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Too many (%u) sections!\n" , + elf->name, elf->hdr->e_shnum); +return -EINVAL ;
This isn't invalid, but unsupported.
Quoted 5 lines
+ if (
elf->hdr->e_shoff>elf->len)
+ {
+dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
+ return-EINVAL;
+ }
If this check is needed, then >=. But I think it should be redundant with the more complete one in elf_resolve_sections().
Quoted 2 lines
+struct
xsplice_elf_sec{
+Elf_Shdr*sec; /* Hooked up in elf_resolve_sections.*/
const
Quoted 2 lines
+struct
xsplice_elf_sym{
+Elf_Sym*sym;
const
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 10/27] xsplice: Add helper elf routines
. snip..
Quoted 13 lines
Quoted 13 lines
xen/common/xsplice_elf.c new file +13
-0+static int elf_resolve_sections (struct xsplice_elf *elf,const void *data) +{ +struct xsplice_elf_sec *sec; +unsigned int i; +Elf_Off delta; +int rc; + +/* xsplice_elf_load sanity checked e_shnum. */ + sec = xmalloc_array(struct xsplice_elf_sec, elf->hdr->e_shnum); +if ( !sec ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not allocate memory for section table!\n" , + elf->name);
Quoted 8 lines
xen/common/xsplice_elf.c line 63
Quoted 6 lines
+ return
-ENOMEM;
+ }xen/common/xsplice_elf.c new file +4
-0+ + elf->sec = sec; + + delta = elf->hdr->e_shoff + elf->hdr->e_shnum * elf->hdr->e_shentsize;I think Andrew had asked this before - are we not worried of overflow here?
Yes. That ends up being checked in xsplice_header_check. Added comment here.
Quoted 17 lines
xen/common/xsplice_elf.c line 75
Quoted 13 lines
+ if ( delta >
elf->len)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Section table is past end of payload!\n",
+elf->name);
+ return-EINVAL;
+ }xen/common/xsplice_elf.c new file +6
-0+ +for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { + delta = elf->hdr->e_shoff + i * elf->hdr->e_shentsize; + + sec[i].sec = (void *)data + delta;Casting away constness is one of the main reasons I complain about casts in general. If you really need to modify some fields in the section header, you should imo cast away constness just there. But even better would be if you left the
In earlier reviews you said that casting away constness in function would be a big no no.
Quoted 1 line
section header alone.
I modify sec[i].sec->sh_entsize in calc_section
Quoted 9 lines
Quoted 8 lines
+ delta = sec[i].sec->sh_offset;
+
+ /*
+ * N.B.elf_resolve_section_names,elf_get_symskip this check as
+ * we do it here.
+ */
+ if ( delta && (delta + sec[i].sec->sh_size >elf->len) )
Quoted 5 lines
Allowing delta to be zero here seems suspicious: Are you doing that because some sections come without data (and hence without offset)? In that case you'd better skip the check on those section types (e.g.
SHT_NOBITS) only. In fact the offset being below the end of the ELF header would likely indicate a broken image.
The loop (earlier) had started at 0 so the delta could have been zero. Anyhow that not being the case anymore I will just do:
if ( !delta )
return -EINVAL;
Quoted 58 lines
Quoted 2 lines
+ /* Name is populated in
xsplice_elf_sections_name. */Stale function name afaict.
Quoted 7 lines
+ /*
+ *elf->symtab->sec->sh_linkwould point to the right section
+ * but we hadn't finished parsing all the sections.
+ */
+ if (elf->symtab->sec->sh_link>elf->hdr->e_shnum)= (and I know I had asked before to check all these bounds checks for off
by one errors)
Quoted 2 lines
+ if ( !elf->symtab->sec->sh_size ||
+elf->symtab->sec->sh_entsize<sizeof(Elf_Sym))||
sh_size%sh_entsizeQuoted 4 lines
+ /*
+ * There can be multipleSHT_STRTAB(.shstrtab, .strtab) so pick one
+ * associated with the symbol table.
+ */... pick the one ...
Quoted 32 lines
xen/common/xsplice_elf.c new file +32
-0+static int elf_resolve_section_names (struct xsplice_elf *elf,const void *data) +{ +const char *shstrtab; +unsigned int i; +Elf_Off offset, delta; +struct xsplice_elf_sec *sec; +int rc; + +/* +* The elf->sec[0 -> e_shnum] structures have been verified by +* elf_resolve_sections. Find file offset for section string table +* (normally called .shstrtab) +*/ + sec = &elf->sec[elf->hdr->e_shstrndx]; + + rc =elf_verify_strtab (sec); +if ( rc ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section string table is corrupted\n" , + elf->name); +return rc; + } + +/* Verified in elf_resolve_sections but just in case. */ + offset = sec->sec->sh_offset; +ASSERT (offset < elf->len && (offset + sec->sec->sh_size <= elf->len)); + + shstrtab = data + offset; + +for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { + delta = elf->sec[i].sec->sh_name;
Quoted 8 lines
Quoted 5 lines
+
+ if ( delta && delta >=sec->sec->sh_size)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: shstrtab [%u] data is past end of payload!\n",
+elf->name, i);Isn't this redundant with the check in
elf_resolve_sections()? Or is this needed because the one here runs before that other one?
We don't verify sh_name in elf_resolve_sections.
This is where we check that sh_name is within the shstrtab.
Quoted 37 lines
Quoted 37 lines
xen/common/xsplice_elf.c new file +36
-0+static int elf_get_sym (struct xsplice_elf *elf,const void *data) +{ +const struct xsplice_elf_sec *symtab_sec, *strtab_sec; +struct xsplice_elf_sym *sym; +unsigned int i, delta, offset, nsym; + + symtab_sec = elf->symtab; + strtab_sec = elf->strtab; + +/* Pointers arithmetic to get file offset. */ + offset = strtab_sec->data - data; + +/* Checked already in elf_resolve_sections, but just in case. */ +ASSERT (offset == strtab_sec->sec->sh_offset); +ASSERT (offset < elf->len && (offset + strtab_sec->sec->sh_size <= elf->len)); + +/* symtab_sec->data was computed in elf_resolve_sections. */ +ASSERT ((symtab_sec->sec->sh_offset + data) == symtab_sec->data); + +/* No need to check values as elf_resolve_sections did it. */ + nsym = symtab_sec->sec->sh_size / symtab_sec->sec->sh_entsize; + + sym = xmalloc_array(struct xsplice_elf_sym, nsym); +if ( !sym ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not allocate memory for symbols\n" , + elf->name); +return -ENOMEM ; + } + +/* So we don't leak memory. */ + elf->sym = sym; + +for ( i =1 ; i < nsym; i++ ) + { +Elf_Sym *s = &((Elf_Sym *)symtab_sec->data)[i];
Quoted 8 lines
xen/common/xsplice_elf.c line 237
Quoted 5 lines
xen/common/xsplice_elf.c new file +5
-0+ +/* If st->name is STN_UNDEF zero, the check will always be true. */ + delta = s->st_name; + +if ( delta && (delta > strtab_sec->sec->sh_size) )Now here I don't see why you special case delta being zero, the more with the comment right above.
That one is stale. Removed the 'if ( delta)' part and the comment.
Quoted 51 lines
Quoted 38 lines
xen/common/xsplice_elf.c new file +37
-0+static int xsplice_header_check (const struct xsplice_elf *elf) +{ +const Elf_Ehdr *hdr = elf->hdr; + +if (sizeof (*elf->hdr) > elf->len ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section header is bigger than payload!\n" , + elf->name); +return -EINVAL ; + } + +if ( !IS_ELF (*hdr) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Not an ELF payload!\n" , elf->name); +return -EINVAL ; + } + +if ( hdr->e_ident[EI_CLASS ] !=ELFCLASS64 || + hdr->e_ident[EI_DATA ] !=ELFDATA2LSB || + hdr->e_ident[EI_OSABI ] !=ELFOSABI_SYSV || + hdr->e_type !=ET_REL || + hdr->e_phnum !=0 ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Invalid ELF payload!\n" , elf->name); +return -EOPNOTSUPP ; + } + +if ( elf->hdr->e_shstrndx ==SHN_UNDEF ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Section name idx is undefined!?\n" , + elf->name); +return -EINVAL ; + } + +/* Check that section name index is within the sections. */ +if ( elf->hdr->e_shstrndx >= elf->hdr->e_shnum ) + {xen/common/xsplice_elf.c line 298
Quoted 10 lines
xen/common/xsplice_elf.c new file +10
-0+dprintk (XENLOG_ERR ,XSPLICE "%s: Section name idx (%u) is past end of sections (%u)!\n" , + elf->name, elf->hdr->e_shstrndx, elf->hdr->e_shnum); +return -EINVAL ; + } + +if ( elf->hdr->e_shnum >64 ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Too many (%u) sections!\n" , + elf->name, elf->hdr->e_shnum); +return -EINVAL ;This isn't invalid, but unsupported.
Quoted 8 lines
Quoted 5 lines
+ if (
elf->hdr->e_shoff>elf->len)
+ {
+dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
+ return-EINVAL;
+ }If this check is needed, then >=. But I think it should be redundant with the more complete one in
elf_resolve_sections().
This is the overflow check Andrew asked for.
Quoted 10 lines
Quoted 3 lines
+struct
xsplice_elf_sec{
+Elf_Shdr*sec; /* Hooked up in elf_resolve_sections.*/const
Quoted 2 lines
+struct
xsplice_elf_sym{
+Elf_Sym*sym;const
That will make 'xsplice_elf_resolve_symbols' unhappy:
elf->sym[i].sym->st_value = (unsigned
long)symbols_lookup_by_name(elf->sym[i].name);
Unless I cast away constness.
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 10/27] xsplice: Add helper elf routines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Konrad Rzeszutek Wilk <[email protected]> 04/18/16 7:55 AM >>>
+ if ( delta >elf->len)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Section table is past end of payload!\n",
+elf->name);
+ return-EINVAL;
+ }
+
+ for ( i = 1; i <elf->hdr->e_shnum; i++ )Quoted 8 lines
Quoted 4 lines
+ {
+ delta =elf->hdr->e_shoff+ i *elf->hdr->e_shentsize; +
+ sec[i].sec = (void *)data + delta;Casting away constness is one of the main reasons I complain about casts in general. If you really need to modify some fields in the section header, you should imo cast away constness just there. But even better would be if you left the
Quoted 2 lines
In earlier reviews you said that casting away constness in function would be a big no no.
Exactly. Yet what do you do above?
Quoted 3 lines
Quoted 1 line
section header alone.
I modify sec[i].sec->sh_entsize in
calc_section
See the comment there (in the next patch it was, I think).
Quoted 15 lines
Quoted 9 lines
Quoted 8 lines
+ delta = sec[i].sec->sh_offset;
+
+ /*
+ * N.B.elf_resolve_section_names,elf_get_symskip this check as
+ * we do it here.
+ */
+ if ( delta && (delta + sec[i].sec->sh_size >elf->len) )Quoted 5 lines
Allowing delta to be zero here seems suspicious: Are you doing that because some sections come without data (and hence without offset)? In that case you'd better skip the check on those section types (e.g.
SHT_NOBITS) only. In fact the offset being below the end of the ELF header would likely indicate a broken image.
Quoted 4 lines
The loop (earlier) had started at 0 so the delta could have been zero. Anyhow that not being the case anymore I will just do:
if ( !delta )
>return -EINVAL;
As said, I'd suggest making this even more strict by checking at least against the ELF header size.
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
+ if (
elf->hdr->e_shoff>elf->len)
+ {
+dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
+ return-EINVAL;
+ }If this check is needed, then >=. But I think it should be redundant with the more complete one in
elf_resolve_sections().This is the overflow check Andrew asked for.
I don't think so - there's no overflow being checked for here at all, this is just a within bounds check. Just compare with the other one.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 10/27] xsplice: Add helper elf routines
Quoted 21 lines · Jan Beulich
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Konrad Rzeszutek Wilk <[email protected]> 04/18/16 7:55 AM >>>
+ if ( delta >elf->len)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Section table is past end of payload!\n",
+elf->name);
+ return-EINVAL;
+ }
+
+ for ( i = 1; i <elf->hdr->e_shnum; i++ )Quoted 8 lines
Quoted 4 lines
+ {
+ delta =elf->hdr->e_shoff+ i *elf->hdr->e_shentsize; +
+ sec[i].sec = (void *)data + delta;Casting away constness is one of the main reasons I complain about casts in general. If you really need to modify some fields in the section header, you should imo cast away constness just there. But even better would be if you left the
Quoted 2 lines
In earlier reviews you said that casting away constness in function would be a big no no.
Quoted 7 lines
Exactly. Yet what do you do above?
Quoted 3 lines
Quoted 1 line
section header alone.
I modify sec[i].sec->sh_entsize in
calc_sectionSee the comment there (in the next patch it was, I think).
OK, and re-reading above you say "imo cast away constness just there." so you are OK then with functions casting away constness if they need to modify the structure.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 9 lines
+ delta = sec[i].sec->sh_offset;
+
+ /*
+ * N.B.elf_resolve_section_names,elf_get_symskip this check as
+ * we do it here.
+ */
+ if ( delta && (delta + sec[i].sec->sh_size >elf->len) )Allowing delta to be zero here seems suspicious: Are you doing that because some sections come without data (and hence without offset)? In that case you'd better skip the check on those section types (e.g.
SHT_NOBITS) only. In fact the offset being below the end of the ELF header would likely indicate a broken image.
Quoted 8 lines
Quoted 4 lines
The loop (earlier) had started at 0 so the delta could have been zero. Anyhow that not being the case anymore I will just do:
if ( !delta )
>return -EINVAL;As said, I'd suggest making this even more strict by checking at least against the ELF header size.
I am not sure I understand you. Initially I thought you mean the p_filesz (which is the program header) - but this ELF file is not a program - it is an relocatable object so there are no program header.
Ah, I think I mislead you! (with the 'if (!delta)..')The code will have:
if ( !delta )
return -EINVAL; if ( delta + sec[i].sec->sh_size > elf->len ) return -EINVAL;
(Or rather a combination of these and make the printk print the right warning).
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ if (
elf->hdr->e_shoff>elf->len)
+ {
+dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
+ return-EINVAL;
+ }
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If this check is needed, then >=. But I think it should be redundant with the more complete one in
elf_resolve_sections().This is the overflow check Andrew asked for.
I don't think so - there's no overflow being checked for here at all, this is just a within bounds check. Just compare with the other one.
So earlier (in common/xsplice, verify_payload) there was a check on the size of the payload (up to 2MB), hence my check here would catch the overflow violation ((-1U) > 2(MB), but it is not very clear.
I modified this above to be:
303 if ( elf->hdr->e_shoff > ULONG_MAX - elf->hdr->e_shoff )
304 {
305 dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
306 return -EINVAL;
307 }
308
I can also add back the elf->hdr->e_shoff > elf->len in this conditional if you would like (along with the comment about 2(MB))
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 10/27] xsplice: Add helper elf routines
Quoted 27 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/20/16 6:09 PM >>>
Quoted 25 lines · Jan Beulich
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Konrad Rzeszutek Wilk <[email protected]> 04/18/16 7:55 AM >>>
+ if ( delta >elf->len)
+ {
+ dprintk(XENLOG_ERR, XSPLICE "%s: Section table is past end of payload!\n",
+elf->name);
+ return-EINVAL;
+ }
+
+ for ( i = 1; i <elf->hdr->e_shnum; i++ )Quoted 8 lines
Quoted 4 lines
+ {
+ delta =elf->hdr->e_shoff+ i *elf->hdr->e_shentsize; +
+ sec[i].sec = (void *)data + delta;Casting away constness is one of the main reasons I complain about casts in general. If you really need to modify some fields in the section header, you should imo cast away constness just there. But even better would be if you left the
Quoted 2 lines
In earlier reviews you said that casting away constness in function would be a big no no.
Exactly. Yet what do you do above?
Quoted 2 lines
Quoted 1 line
section header alone.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
I modify sec[i].sec->sh_entsize in
calc_sectionSee the comment there (in the next patch it was, I think).
OK, and re-reading above you say "imo cast away constness just there." so you are OK then with functions casting away constness if they need to modify the structure.
If it's really just one exceptional place where this happens, I guess it's okay as, well, an exception. Otherwise the const would need to be dropped wherever necessary to avoid having to cast it away in certain places.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
Quoted 7 lines
+ delta = sec[i].sec->sh_offset;
+
+ /*
+ * N.B.elf_resolve_section_names,elf_get_symskip this check as
+ * we do it here.
+ */
+ if ( delta && (delta + sec[i].sec->sh_size >elf->len) )Quoted 5 lines
Allowing delta to be zero here seems suspicious: Are you doing that because some sections come without data (and hence without offset)? In that case you'd better skip the check on those section types (e.g.
SHT_NOBITS) only. In fact the offset being below the end of the ELF header would likely indicate a broken image.The loop (earlier) had started at 0 so the delta could have been zero. Anyhow that not being the case anymore I will just do:
if ( !delta )
Quoted 8 lines
Quoted 4 lines
>return -EINVAL;As said, I'd suggest making this even more strict by checking at least against the ELF header size.
I am not sure I understand you. Initially I thought you mean the
p_filesz(which is the program header) - but this ELF file is not a program - it is an relocatable object so there are no program header.
Right, and I indeed mean the ELF header: The section headers clearly can't start inside it.
Quoted 5 lines
Ah, I think I mislead you! (with the 'if (!delta)..')The code will have:
if ( !delta )
return-EINVAL;
if ( delta + sec[i].sec->sh_size >elf->len)
return-EINVAL;
That's what I had understood, and the first if() is which I'd like to see made slightly more strict.
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
+ if (
elf->hdr->e_shoff>elf->len)
+ {
+dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
+ return-EINVAL;
+ }Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
If this check is needed, then >=. But I think it should be redundant with the more complete one in
elf_resolve_sections().This is the overflow check Andrew asked for.
I don't think so - there's no overflow being checked for here at all, this is just a within bounds check. Just compare with the other one.
Quoted 7 lines
So earlier (in common/xsplice,
verify_payload) there was a check on the size of the payload (up to 2MB), hence my check here would catch the overflow violation ((-1U) > 2(MB), but it is not very clear.I modified this above to be:
303 if (
elf->hdr->e_shoff>ULONG_MAX-elf->hdr->e_shoff)
Something's clearly wrong here, with e_shoff used on both sides.
Quoted 8 lines
304 {
305dprintk(XENLOG_ERR, XSPLICE "%s: Bogus e_shoff!\n", elf->name);
306 return-EINVAL;
307 }
308I can also add back the
elf->hdr->e_shoff>elf->lenin this conditional if you would like (along with the comment about 2(MB))
It's not because I like it, but for consistency with all the other elf->len checks.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 11/27 xsplice: Implement payload loading
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Add support for loading xsplice payloads. This is somewhat similar to the Linux kernel module loader, implementing the following steps:
- Verify the elf file.
- Parse the elf file.
- Allocate a region of memory mapped within a free area of [xen_virt_end, XEN_VIRT_END].
- Copy allocated sections into the new region. Split them in three regions - .text, .data, and .rodata. MUST have at least .text.
- Resolve section symbols. All other symbols must be absolute addresses. (Note that patch titled "xsplice,symbols: Implement symbol name resolution on address" implements that)
- Perform relocations.
- Secure the the regions (.text,.data,.rodata) with proper permissions.
We capitalize on the vmalloc callback API (see patch titled:
"rm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type") to allocate a region of memory within the [xen_virt_end, XEN_VIRT_END] for the code.
We also use the "x86/mm: Introduce modify_xen_mappings()" to change the virtual address page-table permissions.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v2: - Change the 'xsplice_patch_func' structure layout/size.
- Add more error checking. Fix memory leak.
- Move elf_resolve and elf_perform relocs in elf file.
- Print the payload address and pages in keyhandler.
v3:
- Make it build under ARM
- Build it without using the return_ macro.
- Add fixes from Ross.
- Add the _return macro back - but only use it during debug builds.
- Remove the macro, prefix arch_ on arch specific calls.
v4:
- Move alloc_payload to arch specific file.
- Use void* instead of uint8_t, use const
- Add copyrights
- Unroll the vmap code to add ASSERT. Change while to not incur
potential long error loop
- Use vmalloc/vfree cb APIs
- Secure .text pages to be RX instead of RWX.
v5:
- Fix allocation of virtual addresses only allowing one page to be allocated.
- Create .text, .data, and .rodata regions with different permissions.
- Make the find_space_t not a typedef to pointer to a function.
- Allocate memory in here.
v6: Drop parentheses on typedefs.
- s/an xSplice/a xSplice/
- Rebase on "vmap: Add vmalloc_cb"
- Rebase on "vmap: Add vmalloc_type and vm_init_type"
- s/uint8_t/void/ on load_addr
- Set xsplice_elf on stack without using memset.
v7:
- Changed the check on delta = elf->hdr->e_shoff + elf->hdr->e_shnum * elf->hdr->e_shentsize; The sections can be right at the back of the file (different linker!), so the failing conditional for 'if (delta >= elf->len)' is incorrect and should have been '>'.
- Changed dprintk(XENLOG_DEBUG to XENLOG_ERR, then back to DEBUG. Converted
some of the printk to dprintk.
- Rebase on " arm/x86/vmap: Add vmalloc_xen, vfree_xen and vm_init_type"
- Changed some of the printk XENLOG_ERR to XENLOG_DEBUG
- Check the idx in the relocation to make sure it is within bounds and
implemented.
- Use "x86/mm: Introduce modify_xen_mappings()"
- Introduce PRIxElfAddr
- Check for overflow in R_X86_64_PC32
- Return -EOPNOTSUPP if we don't support types in ELF64_R_TYPE
v8:
- Change dprintk and printk XENLOG_DEBUG to XENLOG_ERR
- Convert four of the printks in dprintk.
---
--- xen/arch/arm/Makefile | 1 + xen/arch/arm/xsplice.c | 54 ++++++++++ xen/arch/x86/Makefile | 1 + xen/arch/x86/xsplice.c | 188 +++++++++++++++++++++++++++++++++++ xen/common/xsplice.c | 226 +++++++++++++++++++++++++++++++++++++++++- xen/common/xsplice_elf.c | 124 ++++++++++++++++++++++- xen/include/xen/elfstructs.h | 4 + xen/include/xen/xsplice.h | 29 ++++++ xen/include/xen/xsplice_elf.h | 9 +- 9 files changed, 631 insertions(+), 5 deletions(-) create mode 100644 xen/arch/arm/xsplice.c create mode 100644 xen/arch/x86/xsplice.c
xen/arch/arm/Makefile +1 -0
@@ -40,6 +40,7 @@ obj-y += device.o obj-y += decode.o obj-y += processor.o obj-y += smc.o +obj-$(CONFIG_XSPLICE) += xsplice.o #obj-bin-y += ....o
xen/arch/arm/xsplice.c new file +54 -0
54 lines not on this page · raw message
xen/arch/x86/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-y += vm_event.o obj-y += xstate.o obj-$(crash_debug) += gdbstub.o +obj-$(CONFIG_XSPLICE) += xsplice.o x86_emulate.o: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
xen/arch/x86/xsplice.c new file +188 -0
188 lines not on this page · raw message
xen/common/xsplice.c +223 -3
283 lines not on this page · raw message
xen/common/xsplice_elf.c +123 -1
145 lines not on this page · raw message
xen/include/xen/elfstructs.h +4 -0
@@ -472,6 +472,8 @@ typedef struct {#endif #if defined(ELFSIZE ) && (ELFSIZE ==32 ) +#define PRIxElfAddr "08x" +#define Elf_Ehdr Elf32_Ehdr#define Elf_Phdr Elf32_Phdr#define Elf_Shdr Elf32_Shdr
@@ -497,6 +499,8 @@ typedef struct {#define AuxInfo Aux32Info#elif defined(ELFSIZE ) && (ELFSIZE ==64 ) +#define PRIxElfAddr "lx" +#define Elf_Ehdr Elf64_Ehdr#define Elf_Phdr Elf64_Phdr#define Elf_Shdr Elf64_Shdr
xen/include/xen/xsplice.h +29 -0
@@ -6,6 +6,9 @@#ifndef __XEN_XSPLICE_H__#define __XEN_XSPLICE_H__ +struct xsplice_elf ; +struct xsplice_elf_sec ; +struct xsplice_elf_sym ;struct xen_sysctl_xsplice_op ;#ifdef CONFIG_XSPLICE
@@ -15,6 +18,32 @@ struct xen_sysctl_xsplice_op;int xsplice_op (struct xen_sysctl_xsplice_op *); +/* Arch hooks. */ +int arch_xsplice_verify_elf (const struct xsplice_elf *elf); +int arch_xsplice_perform_rel (struct xsplice_elf *elf, +const struct xsplice_elf_sec *base, +const struct xsplice_elf_sec *rela); +int arch_xsplice_perform_rela (struct xsplice_elf *elf, +const struct xsplice_elf_sec *base, +const struct xsplice_elf_sec *rela); +enum va_type { +XSPLICE_VA_RX ,/* .text */ +XSPLICE_VA_RW ,/* .data */ +XSPLICE_VA_RO ,/* .rodata */ +}; + +#include <xen/mm.h> +void *arch_xsplice_alloc_payload (unsigned int pages); + +/* +* Function to secure the allocate pages (from arch_xsplice_alloc_payload) +* with the right page permissions. +*/ +int arch_xsplice_secure (void *va,unsigned int pages,enum va_type types); + +void arch_xsplice_free_payload (void *va); + +void arch_xsplice_init (void );#else #include <xen/errno.h> /* For -ENOSYS */
xen/include/xen/xsplice_elf.h +8 -1
@@ -15,6 +15,8 @@ struct xsplice_elf_sec {elf_resolve_section_names. */ const void *data;/* Pointer to the section (done by elf_resolve_sections). */ +void *load_addr;/* A pointer to the allocated destination. +Done by load_payload_data. */ };struct xsplice_elf_sym {
@@ -29,8 +31,10 @@ struct xsplice_elf {struct xsplice_elf_sec *sec;/* Array of sections, allocated by us. */ struct xsplice_elf_sym *sym;/* Array of symbols , allocated by us. */ unsigned int nsym; -const struct xsplice_elf_sec *symtab;/* Pointer to .symtab section - aka to sec[x]. */ +const struct xsplice_elf_sec *symtab;/* Pointer to .symtab section - aka to +sec[symtab_idx]. */ const struct xsplice_elf_sec *strtab;/* Pointer to .strtab section - aka to sec[y]. */ +unsigned int symtab_idx; }; const struct xsplice_elf_sec *xsplice_elf_sec_by_name(const struct xsplice_elf *elf,
@@ -38,6 +42,9 @@ const struct xsplice_elf_sec *xsplice_elf_sec_by_name(const struct xsplice_elf *int xsplice_elf_load (struct xsplice_elf *elf,const void *data);void xsplice_elf_free (struct xsplice_elf *elf); +int xsplice_elf_resolve_symbols (struct xsplice_elf *elf); +int xsplice_elf_perform_relocs (struct xsplice_elf *elf); +#endif /* __XEN_XSPLICE_ELF_H__ */ /*
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:04 AM >>>
xen/arch/x86/Makefile +0
-0@@ -64,6 +64,7 @@ obj-y += vm_event.oobj-y += xstate.o
obj-$(crash_debug) += gdbstub.o
+obj-$(CONFIG_XSPLICE) += xsplice.o
This should be added in the main section, somewhere ahead of the xstate.o
still visible in context.
xen/arch/x86/Makefile
Quoted 11 lines
xen/arch/x86/Makefile +11
-0+int arch_xsplice_perform_rela(struct xsplice_elf *elf, + const struct xsplice_elf_sec *base, + const struct xsplice_elf_sec *rela) +{ + const Elf_RelA *r; + unsigned int symndx, i; + uint64_t val; + uint8_t *dest; + + if ( !rela->sec->sh_entsize || !rela->sec->sh_size || + rela->sec->sh_entsize != sizeof(Elf_RelA) )
< and again missing the sh_size % sh_entsize check. Plus an empty relocation section is in no way invalid.
xen/arch/x86/Makefile
Quoted 7 lines
xen/arch/x86/Makefile +7
-0+ for ( i = 0; i < (rela->sec->sh_size / rela->sec->sh_entsize); i++ ) + { + r = rela->data + i * rela->sec->sh_entsize; + if ( (unsigned long)r > (unsigned long)(elf->hdr + elf->len) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Relative entry %u in %s is past end!\n", + elf->name, i, rela->name);
With sections having got verified already, I don't see the point of this check.
xen/arch/x86/Makefile
Quoted 7 lines
xen/arch/x86/Makefile +2
-0+ symndx = ELF64_R_SYM(r->r_info); + if ( symndx > elf->nsym )=
xen/arch/x86/Makefile +2
-0+ { + dprintk(XENLOG_ERR, XSPLICE "%s: Relative symbol wants symbol@%u which is past end!\n",
"Relative relocation wants ..."
Quoted 1 line
+ dest =
base->load_addr+r->r_offset;
I'm sure I've commented on this before: What if r_offset points outside the section being relocated?
Quoted 11 lines
xen/arch/x86/Makefile +11
-0+ val = r->r_addend + elf->sym[symndx].sym->st_value; + + switch ( ELF64_R_TYPE(r->r_info) ) + { + case R_X86_64_NONE: + break; + + case R_X86_64_64: + *(uint64_t *)dest = val; + break; +
xen/arch/x86/Makefile
Quoted 10 lines
xen/arch/x86/Makefile +10
-0+ case R_X86_64_PLT32: + /* + * Xen uses -fpic which normally uses PLT relocations + * except that it sets visibility to hidden which means + * that they are not used. However, when gcc cannot + * inline memcpy it emits memcpy with default visibility + * which then creates a PLT relocation. It can just be + * treated the same as R_X86_64_PC32. + */ + /* Fall through */
Same here, just to repeat an earlier review comment: This second comment isn't really useful.
xen/arch/x86/Makefile
Quoted 3 lines
xen/arch/x86/Makefile +3
-0+ case R_X86_64_PC32: + val -= (uint64_t)dest; + *(uint32_t *)dest = val;
int32_t
Quoted 1 line
+ if ( (s64)val != *(s32 *)dest )
int64_t and int32_t please (for consistency); I actually doubt the cast on the left side is really needed at all.
xen/arch/x86/Makefile
Quoted 9 lines
xen/arch/x86/Makefile +9
-0+void *arch_xsplice_alloc_payload(unsigned int pages) +{ + unsigned int i; + void *p; + + ASSERT(pages); + + p = vmalloc_xen(pages * PAGE_SIZE); + WARN_ON(!p);
Why? If at all, a dprintk() in the else to ...
xen/arch/x86/Makefile
Quoted 6 lines
xen/arch/x86/Makefile +6
-0+ if ( p ) + { + /* By default they are PAGE_HYPERVISOR aka PAGE_HYPERVISOR_RWX.*/ + for ( i = 0; i < pages; i++ ) + clear_page(p + (i * PAGE_SIZE) ); + }
... this.
Quoted 1 line
+int
arch_xsplice_secure(void *va, unsigned int pages, enum va_type type)
const void *va
xen/arch/x86/Makefile
Quoted 3 lines
xen/arch/x86/Makefile +3
-0+{ + unsigned long start = (unsigned long)va; + int flag;
unsigned int flags;
xen/arch/x86/Makefile
Quoted 10 lines
xen/arch/x86/Makefile +10
-0+ + ASSERT(va); + ASSERT(pages); + + if ( type == XSPLICE_VA_RX ) /* PAGE_HYPERVISOR_RX */ + flag = _PAGE_PRESENT; + else if ( type == XSPLICE_VA_RW ) /* PAGE_HYPERVISOR_RW */ + flag = _PAGE_RW | _PAGE_NX | _PAGE_PRESENT; + else /* PAGE_HYPERVISOR_RO */ + flag = _PAGE_NX | _PAGE_PRESENT;
According to the most recent version of that patch, shouldn't you use the values in the comments here instead of the open coded variants?
xen/arch/x86/Makefile
Quoted 4 lines
xen/arch/x86/Makefile +4
-0+void arch_xsplice_free_payload(void *va) +{ + vfree_xen(va); +}
What is the idea behind having this hook (instead of generic code just calling vfree_xen() [or really just vfree()])?
xen/arch/x86/Makefile struct payload {
Quoted 1 line
xen/arch/x86/Makefile +0
-0@@ -29,6 +30,13 @@ struct payload {
>uint32_t state; /* One of the XSPLICE_STATE_*. */
>int32_t rc; /* 0 or -XEN_EXX. */
>struct list_head list; /* Linked to 'payload_list'. */
>+ void *text_addr; /* Virtual address of .text. */
>+ size_t text_size; /* .. and its size. */
>+ void *rw_addr; /* Virtual address of .data. */
>+ size_t rw_size; /* .. and its size (if any). */
>+ void *ro_addr; /* Virtual address of .rodata. */
>+ size_t ro_size; /* .. and its size (if any). */And again the question: Do these pointers really need to be non-const?
xen/arch/x86/Makefile struct payload { · line 30
Quoted 1 line
xen/arch/x86/Makefile +1
-0+ size_t pages; /* Total pages for [text,rw,ro]_addr */
Why size_t and not just unsigned int?
xen/arch/x86/Makefile struct payload { · line 37
Quoted 7 lines
xen/arch/x86/Makefile +7
-0+static void calc_section(struct xsplice_elf_sec *sec, size_t *size) +{ + Elf_Shdr *s = sec->sec; + size_t align_size; + + align_size = ROUNDUP(*size, s->sh_addralign); + s->sh_entsize = align_size;
So this is one of the places (the only one?) where the section header gets altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control sections...
Quoted 32 lines
xen/arch/x86/Makefile +32
-0+static int move_payload(struct payload *payload, struct xsplice_elf *elf) +{ + uint8_t *buf; + unsigned int i; + size_t size = 0; + + /* Compute text regions. */ + for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + if ( (elf->sec[i].sec->sh_flags & (SHF_ALLOC|SHF_EXECINSTR)) == + (SHF_ALLOC|SHF_EXECINSTR) ) + calc_section(&elf->sec[i], &payload->text_size); + } + + /* Compute rw data. */ + for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + if ( (elf->sec[i].sec->sh_flags & SHF_ALLOC) && + !(elf->sec[i].sec->sh_flags & SHF_EXECINSTR) && + (elf->sec[i].sec->sh_flags & SHF_WRITE) ) + calc_section(&elf->sec[i], &payload->rw_size); + } + + /* Compute ro data. */ + for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + if ( (elf->sec[i].sec->sh_flags & SHF_ALLOC) && + !(elf->sec[i].sec->sh_flags & SHF_EXECINSTR) && + !(elf->sec[i].sec->sh_flags & SHF_WRITE) ) + calc_section(&elf->sec[i], &payload->ro_size); + } +
xen/arch/x86/Makefile struct payload {
Quoted 10 lines
xen/arch/x86/Makefile +10
-0+ /* Do not accept wx. */ + for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + if ( !(elf->sec[i].sec->sh_flags & SHF_ALLOC) && + (elf->sec[i].sec->sh_flags & SHF_EXECINSTR) && + (elf->sec[i].sec->sh_flags & SHF_WRITE) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: No WX sections!\n", elf->name); + return -EINVAL; + }
Is there any reason to have four loops here, with quite a bit of redundancy in the if()s, instead of just one loop with a if/else sequence?
Also it's not really clear whether you really mean to honor non-progbits, non- nobits sections with SHF_ALLOC set. Perhaps such would better be refused for the now at least.
xen/arch/x86/Makefile
Quoted 9 lines
xen/arch/x86/Makefile +9
-0+ for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + if ( elf->sec[i].sec->sh_flags & SHF_ALLOC ) + { + if ( (elf->sec[i].sec->sh_flags & SHF_EXECINSTR) ) + buf = payload->text_addr; + else if ( (elf->sec[i].sec->sh_flags & SHF_WRITE) ) + buf = payload->rw_addr; + else
Something's wrong with indentation here (not visible above anymore due to the limitations of this web frontend of our mail system).
xen/arch/x86/Makefile
Quoted 8 lines
xen/arch/x86/Makefile +8
-0+ /* Don't copy NOBITS - such as BSS. */ + if ( elf->sec[i].sec->sh_type != SHT_NOBITS ) + { + memcpy(elf->sec[i].load_addr, elf->sec[i].data, + elf->sec[i].sec->sh_size); + dprintk(XENLOG_DEBUG, XSPLICE "%s: Loaded %s at 0x%p\n", + elf->name, elf->sec[i].name, elf->sec[i].load_addr); + }
"else memset();" is what I would have expected here. Now I see that the allocation function clears the pages (in a bogusly open coded way, instead of using vzalloc()), but why is that so?
Quoted 4 lines
xen/arch/x86/Makefile +4
-0+int xsplice_elf_resolve_symbols(struct xsplice_elf *elf) +{ + unsigned int i; + int rc = 0;
xen/arch/x86/Makefile
Quoted 10 lines
xen/arch/x86/Makefile +10
-0+ + /* + * The first entry of an ELF symbol table is the "undefined symbol index". + * aka reserved so we skip it. + */ + ASSERT(elf->sym); + + for ( i = 1; i < elf->nsym; i++ ) + { + uint16_t idx = elf->sym[i].sym->st_shndx;
Any reason not to use unsigned int here?
Quoted 1 line
+ rc = 0;
rc is already zero whenever you get here afaict.
Quoted 12 lines
xen/arch/x86/Makefile +3
-0+ default: + /* SHN_COMMON and SHN_ABS are above. */ + if ( idx > SHN_LORESERVE )=
xen/arch/x86/Makefile +3
-0+ rc = -EOPNOTSUPP; + /* SHN_UNDEF (0) above. */ + else if ( idx > elf->hdr->e_shnum && idx < SHN_LORESERVE )= and the right side of the && seems pointless due to the preceding
if().
xen/arch/x86/Makefile
Quoted 2 lines
xen/arch/x86/Makefile +2
-0+ if ( !(elf->sec[idx].sec->sh_flags & SHF_ALLOC) ) + break;
If you really mean to check this, shouldn't this be done earlier, avoiding needless errors on unsupported symbol kinds above?
xen/arch/x86/Makefile
Quoted 10 lines
xen/arch/x86/Makefile +10
-0+int xsplice_elf_perform_relocs(struct xsplice_elf *elf) +{ + struct xsplice_elf_sec *rela, *base; + unsigned int i; + int rc = 0; + + /* + * The first entry of an ELF symbol table is the "undefined symbol index". + * aka reserved so we skip it. + */
This comment seems at least misplaced, if not pointless.
xen/arch/x86/Makefile
Quoted 9 lines
xen/arch/x86/Makefile +9
-0+ ASSERT(elf->sym); + + for ( i = 1; i < elf->hdr->e_shnum; i++ ) + { + rela = &elf->sec[i]; + + if ( (rela->sec->sh_type != SHT_RELA) && + (rela->sec->sh_type != SHT_REL) ) + continue;
With this the variable name "rela" is bogus (as it suggests RELA only).
xen/arch/x86/Makefile typedef struct { · line 502
Quoted 5 lines
xen/arch/x86/Makefile +1
-0@@ -497,6 +499,8 @@ typedef struct { #define AuxInfo Aux32Info #elif defined(ELFSIZE) && (ELFSIZE == 64) +#define PRIxElfAddr "lx"
PRIx64?
Quoted 21 lines
xen/include/xen/xsplice.h +8
-0@@ -6,6 +6,9 @@#ifndef __XEN_XSPLICE_H__#define __XEN_XSPLICE_H__ +struct xsplice_elf ; +struct xsplice_elf_sec ; +struct xsplice_elf_sym ;struct xen_sysctl_xsplice_op ;#ifdef CONFIG_XSPLICE @@ -15,6 +18,32 @@ struct xen_sysctl_xsplice_op;int xsplice_op (struct xen_sysctl_xsplice_op *); +/* Arch hooks. */ +int arch_xsplice_verify_elf (const struct xsplice_elf *elf); +int arch_xsplice_perform_rel (struct xsplice_elf *elf, +const struct xsplice_elf_sec *base, +const struct xsplice_elf_sec *rela);
xen/include/xen/xsplice.h struct xen_sysctl_xsplice_op; · line 35
Quoted 10 lines
xen/include/xen/xsplice.h +10
-0+int arch_xsplice_perform_rela (struct xsplice_elf *elf, +const struct xsplice_elf_sec *base, +const struct xsplice_elf_sec *rela); +enum va_type { +XSPLICE_VA_RX ,/* .text */ +XSPLICE_VA_RW ,/* .data */ +XSPLICE_VA_RO ,/* .rodata */ +}; + +#include <xen/mm.h>
Why at all? And if needed, why placed in the middle?
xen/include/xen/xsplice.h struct xsplice_elf {
Quoted 1 line
xen/include/xen/xsplice.h +0
-0@@ -29,8 +31,10 @@ struct xsplice_elf {
>struct xsplice_elf_sec *sec; /* Array of sections, allocated by us. */
>struct xsplice_elf_sym *sym; /* Array of symbols , allocated by us. */
>unsigned int nsym;
xen/include/xen/xsplice.h struct xsplice_elf { · line 32
Quoted 3 lines
xen/include/xen/xsplice.h +2
-1-const struct xsplice_elf_sec *symtab;/* Pointer to .symtab section - aka to sec[x]. */ +const struct xsplice_elf_sec *symtab;/* Pointer to .symtab section - aka to +sec[symtab_idx]. */
>const struct xsplice_elf_sec *strtab;/* Pointer to .strtab section - aka to sec[y]. */
With the adjustment above - who is y? (But perhaps this needs taking care of in an earlier patch.)
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 7 lines
xen/arch/x86/xsplice.c line 167
Quoted 4 lines
xen/arch/x86/xsplice.c new file +4
-0+void arch_xsplice_free_payload (void *va) +{ +vfree_xen (va); +}What is the idea behind having this hook (instead of generic code just calling
vfree_xen()[or really justvfree()])?
To have an symmetry with the allocation one. I don't know enough about ARM to know whether this logic above can be hoisted in the common code and hence called (or compiled) on ARM.
Let me try.
Quoted 12 lines
Review struct payload {
Quoted 2 lines
(quoted diff) +0
-0@@ -29,6 +30,13 @@ struct payload {>uint32_t state; /* One of the XSPLICE_STATE_*. */ >int32_t rc; /* 0 or -XEN_EXX. */ >struct list_head list; /* Linked to 'payload_list'. */ >+ void *text_addr; /* Virtual address of .text. */ >+ size_t text_size; /* .. and its size. */ >+ void *rw_addr; /* Virtual address of .data. */ >+ size_t rw_size; /* .. and its size (if any). */ >+ void *ro_addr; /* Virtual address of .rodata. */ >+ size_t ro_size; /* .. and its size (if any). */
Quoted 1 line
And again the question: Do these pointers really need to be non-const?
I know I tried making them const and the compiler was not happy. I will follow up with the reasoning.
Quoted 4 lines
xen/common/xsplice.c struct payload { · line 39
Quoted 2 lines
xen/common/xsplice.c +1
-0+size_t pages;/* Total pages for [text,rw,ro]_addr */ Why
size_tand not just unsigned int?
Oh. I was somehow under the impression you liked size_t more than unsignged int! I will change it over.
Quoted 12 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 129
Quoted 8 lines
xen/common/xsplice.c +7
-0+static void calc_section (struct xsplice_elf_sec *sec,size_t *size) +{ +Elf_Shdr *s = sec->sec; +size_t align_size; + + align_size =ROUNDUP (*size, s->sh_addralign); + s->sh_entsize = align_size;So this is one of the places (the only one?) where the section header gets altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control sections...
The 'man elf' tells me :
"Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each entry.
This member contains zero if the section does not hold a table of
fixed-size entries."
We may have an value for an payload with one symbol but we don't depend on this value having any value and just re-use.
We could change the logic to save the aligned size (which would then alter the amount of pages to allocate along with the amount of bytes to copy) in some 'per-section' temporary variable (so adding an extra field in 'struct xsplice_elf_sec').
Let me prototype that.
Quoted 3 lines
Quoted 3 lines
(quoted diff) +2
-0+static int move_payload(struct payload *payload, struct xsplice_elf *elf) +{
..snip..
Quoted 10 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 175
Quoted 9 lines
xen/common/xsplice.c +9
-0+for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { +if ( !(elf->sec[i].sec->sh_flags &SHF_ALLOC ) && + (elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) && + (elf->sec[i].sec->sh_flags &SHF_WRITE ) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: No WX sections!\n" , elf->name); +return -EINVAL ; + }
Quoted 2 lines
Is there any reason to have four loops here, with quite a bit of redundancy in the if()s, instead of just one loop with a if/else sequence?
There was a historical reason - we wre re-using 'size' but that is no longer the case.
Quoted 4 lines
Also it's not really clear whether you really mean to honor non-progbits, non- nobits sections with
SHF_ALLOCset. Perhaps such would better be refused for the now at least.
OK.
Quoted 23 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 208
Quoted 10 lines
xen/common/xsplice.c +9
-0+for ( i =1 ; i < elf->hdr->e_shnum; i++ ) + { +if ( elf->sec[i].sec->sh_flags &SHF_ALLOC ) + { +if ( (elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) ) + buf = payload->text_addr; +else if ( (elf->sec[i].sec->sh_flags &SHF_WRITE ) ) + buf = payload->rw_addr; +else Something's wrong with indentation here (not visible above anymore due to the limitations of this web frontend of our mail system).
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 220
Quoted 8 lines
xen/common/xsplice.c +8
-0+/* Don't copy NOBITS - such as BSS. */ +if ( elf->sec[i].sec->sh_type !=SHT_NOBITS ) + { +memcpy (elf->sec[i].load_addr, elf->sec[i].data, + elf->sec[i].sec->sh_size); +dprintk (XENLOG_DEBUG ,XSPLICE "%s: Loaded %s at 0x%p\n" , + elf->name, elf->sec[i].name, elf->sec[i].load_addr); + }
Quoted 3 lines
"else
memset();" is what I would have expected here. Now I see that the allocation function clears the pages (in a bogusly open coded way, instead of usingvzalloc()), but why is that so?
B/c we end up having vzalloc_xen (oh wait, we made that go away). Yes we do need an memset or introduce vzalloc_xen (and keep vmalloc_xen?).
Your call - memset or introduce vzalloc_xen ?
Quoted 2 lines
Quoted 2 lines
+int
xsplice_elf_resolve_symbols(struct xsplice_elf *elf)
.. snip..
Quoted 10 lines
Quoted 9 lines
xen/common/xsplice_elf.c +3
-0+default : +/* SHN_COMMON and SHN_ABS are above. */ +if ( idx >SHN_LORESERVE )=
xen/common/xsplice_elf.c +3
-0+ rc = -EOPNOTSUPP ; +/* SHN_UNDEF (0) above. */ + else if ( idx > elf->hdr->e_shnum && idx < SHN_LORESERVE )
Quoted 7 lines
Review
Quoted 4 lines
= and the right side of the && seems pointless due to the preceding
if().(quoted diff) +2
-0+ if ( !(elf->sec[idx].sec->sh_flags & SHF_ALLOC) ) + break;If you really mean to check this, shouldn't this be done earlier, avoiding needless errors on unsupported symbol kinds above?
Right! Will move those checks right above the switch statement.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Konrad Rzeszutek Wilk <[email protected]> 04/20/16 6:00 PM >>>
+size_tpages; /* Total pages for [text,rw,ro]_addr */Why
size_tand not just unsigned int?Oh. I was somehow under the impression you liked
size_tmore than unsignged int! I will change it over.
When used where actually talking about sizes, I indeed prefer size_t. But here we have a count which we know will be much lower than UINT_MAX.
Quoted 12 lines
Quoted 11 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 129
Quoted 7 lines
xen/common/xsplice.c +7
-0+static void calc_section (struct xsplice_elf_sec *sec,size_t *size) +{ +Elf_Shdr *s = sec->sec; +size_t align_size; + + align_size =ROUNDUP (*size, s->sh_addralign); + s->sh_entsize = align_size;So this is one of the places (the only one?) where the section header gets altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control sections...
Quoted 5 lines
The 'man elf' tells me :
"Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each entry. This member contains zero if the section does not hold a table of fixed-size entries."
My main concern is that SHF_MERGE sections (i.e. ordinary data ones) also already use this field.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
xen/common/xsplice.c +8
-0+/* Don't copy NOBITS - such as BSS. */ +if ( elf->sec[i].sec->sh_type !=SHT_NOBITS ) + { +memcpy (elf->sec[i].load_addr, elf->sec[i].data, + elf->sec[i].sec->sh_size); +dprintk (XENLOG_DEBUG ,XSPLICE "%s: Loaded %s at 0x%p\n" , + elf->name, elf->sec[i].name, elf->sec[i].load_addr); + }
Quoted 8 lines
Quoted 3 lines
"else
memset();" is what I would have expected here. Now I see that the allocation function clears the pages (in a bogusly open coded way, instead of usingvzalloc()), but why is that so?B/c we end up having
vzalloc_xen(oh wait, we made that go away). Yes we do need an memset or introducevzalloc_xen(and keepvmalloc_xen?).Your call - memset or introduce
vzalloc_xen?
The latter would be my preference.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Konrad Rzeszutek Wilk <[email protected]> 04/20/16 6:00 PM >>>
+size_tpages; /* Total pages for [text,rw,ro]_addr */
Quoted 17 lines
Quoted 4 lines
Quoted 1 line
Why
size_tand not just unsigned int?Oh. I was somehow under the impression you liked
size_tmore than unsignged int! I will change it over.When used where actually talking about sizes, I indeed prefer
size_t. But here we have a count which we know will be much lower thanUINT_MAX.Quoted 9 lines
Quoted 9 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 129
Quoted 7 lines
xen/common/xsplice.c +7
-0+static void calc_section (struct xsplice_elf_sec *sec,size_t *size) +{ +Elf_Shdr *s = sec->sec; +size_t align_size; + + align_size =ROUNDUP (*size, s->sh_addralign); + s->sh_entsize = align_size;So this is one of the places (the only one?) where the section header
gets
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control
sections...
Quoted 4 lines
Quoted 4 lines
The 'man elf' tells me :
"Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each
entry.
Quoted 5 lines
Quoted 2 lines
This member contains zero if the section does not hold a table of fixed-size entries."
My main concern is that
SHF_MERGEsections (i.e. ordinary data ones) also already use this field.
OK. Let me add the 'offset' member (in xsplice_elf_sec) to store the value.
Albeit it only gets used during move_payload. And later that offset value can easily computed using ->load_addr minus ->data.
Perhaps I should introduce an temporary array to hold this value instead?
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name)
Quoted 7 lines
xen/common/xsplice.c +6
-0+/* Don't copy NOBITS - such as BSS. */ + if ( elf->sec[i].sec->sh_type != SHT_NOBITS ) + { + memcpy(elf->sec[i].load_addr, elf->sec[i].data, + elf->sec[i].sec->sh_size); + dprintk(XENLOG_DEBUG, XSPLICE "%s: Loaded %s at
0x%p\n",
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
elf->name, elf->sec[i].name,
elf->sec[i].load_addr);
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
+ }
"else
memset();" is what I would have expected here. Now I see that the allocation function clears the pages (in a bogusly open coded way,
instead
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
of using
vzalloc()), but why is that so?B/c we end up having
vzalloc_xen(oh wait, we made that go away). Yes we do need an memset or introducevzalloc_xen(and keepvmalloc_xen?).Your call - memset or introduce
vzalloc_xen?The latter would be my preference.
OK, will do.
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 20.04.16 at 19:36, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Konrad Rzeszutek Wilk <[email protected]> 04/20/16 6:00 PM >>>
+size_tpages; /* Total pages for [text,rw,ro]_addr */Why
size_tand not just unsigned int?Oh. I was somehow under the impression you liked
size_tmore than unsignged int! I will change it over.When used where actually talking about sizes, I indeed prefer
size_t. But here we have a count which we know will be much lower thanUINT_MAX.Quoted 12 lines
Quoted 11 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 129
Quoted 7 lines
xen/common/xsplice.c +7
-0+static void calc_section (struct xsplice_elf_sec *sec,size_t *size) +{ +Elf_Shdr *s = sec->sec; +size_t align_size; + + align_size =ROUNDUP (*size, s->sh_addralign); + s->sh_entsize = align_size;So this is one of the places (the only one?) where the section header gets altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control sections...
Quoted 8 lines
Quoted 5 lines
The 'man elf' tells me :
"Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each entry. This member contains zero if the section does not hold a table of fixed-size entries."My main concern is that
SHF_MERGEsections (i.e. ordinary data ones) also already use this field.
Quoted 6 lines
OK. Let me add the 'offset' member (in
xsplice_elf_sec) to store the value.Albeit it only gets used during
move_payload. And later that offset value can easily computed using ->load_addr minus ->data.Perhaps I should introduce an temporary array to hold this value instead?
Whichever is more feasible / less convoluted, I would say.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
In reply to Konrad Rzeszutek WilkQuoted 8 lines · Konrad Rzeszutek Wilk
Quoted 7 lines
xen/arch/x86/xsplice.c line 167
Quoted 4 lines
xen/arch/x86/xsplice.c new file +4
-0+void arch_xsplice_free_payload (void *va) +{ +vfree_xen (va); +}What is the idea behind having this hook (instead of generic code just calling
vfree_xen()[or really justvfree()])?
Quoted 5 lines
To have an symmetry with the allocation one. I don't know enough about ARM to know whether this logic above can be hoisted in the common code and hence called (or compiled) on ARM.
Let me try.
That worked out nicely. vzalloc_xen and vfree and the arch_xsplice_alloc_payload and arch_xsplice_free_payload are gone.
Quoted 12 lines
Quoted 12 lines
Review struct payload {
Quoted 2 lines
(quoted diff) +0
-0@@ -29,6 +30,13 @@ struct payload {>uint32_t state; /* One of the XSPLICE_STATE_*. */ >int32_t rc; /* 0 or -XEN_EXX. */ >struct list_head list; /* Linked to 'payload_list'. */ >+ void *text_addr; /* Virtual address of .text. */ >+ size_t text_size; /* .. and its size. */ >+ void *rw_addr; /* Virtual address of .data. */ >+ size_t rw_size; /* .. and its size (if any). */ >+ void *ro_addr; /* Virtual address of .rodata. */ >+ size_t ro_size; /* .. and its size (if any). */
Quoted 4 lines
Quoted 1 line
And again the question: Do these pointers really need to be non-const?
I know I tried making them const and the compiler was not happy. I will follow up with the reasoning.
The big problem I ran in was that I had to do casting in 'modify_payload'. To lower the amount of casting I ended making
load_addr be void * (if it was const void * I would have to cast away the constness in xsplice_elf.c):
elf->sec[i].load_addr = (void *)buf + offset[i];
where 'buf' is:
buf = payload->text_addr;
(and buf it also a const void *).
Quoted 22 lines
Quoted 12 lines
xen/common/xsplice.c static struct payload *find_payload(const char *name) · line 129
Quoted 8 lines
xen/common/xsplice.c +7
-0+static void calc_section (struct xsplice_elf_sec *sec,size_t *size) +{ +Elf_Shdr *s = sec->sec; +size_t align_size; + + align_size =ROUNDUP (*size, s->sh_addralign); + s->sh_entsize = align_size;So this is one of the places (the only one?) where the section header gets altered. Are you not expecting problems down the road resulting from overwriting this field? After all it's used not just in control sections...
The 'man elf' tells me :
"Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each entry.
This member contains zero if the section does not hold a table of
fixed-size entries."We may have an value for an payload with one symbol but we don't depend on this value having any value and just re-use.
Quoted 6 lines
We could change the logic to save the aligned size (which would then alter the amount of pages to allocate along with the amount of bytes to copy) in some 'per-section' temporary variable (so adding an extra field in 'struct
xsplice_elf_sec').Let me prototype that.
And it worked out nicely.
.. snip..
Quoted 3 lines
Quoted 1 line
Quoted 1 line
xen/common/xsplice_elf.c +1
-0+int xsplice_elf_resolve_symbols (struct xsplice_elf *elf).. snip..
Quoted 1 line
Quoted 1 line
+ default:
.. snip..
Quoted 6 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ( !(elf->sec[idx].sec->sh_flags & SHF_ALLOC) ) + break;If you really mean to check this, shouldn't this be done earlier, avoiding needless errors on unsupported symbol kinds above?
So I am coming back to this. The ones that this hits are:
Symbol's section .note.GNU-stack is !SHF_ALLOC Symbol's section .comment is !SHF_ALLOC Symbol's section .debug_aranges is !SHF_ALLOC Symbol's section .debug_pubnames is !SHF_ALLOC Symbol's section .debug_info is !SHF_ALLOC Symbol's section .debug_abbrev is !SHF_ALLOC Symbol's section .debug_line is !SHF_ALLOC Symbol's section .debug_frame is !SHF_ALLOC Symbol's section .debug_str is !SHF_ALLOC Symbol's section .debug_loc is !SHF_ALLOC Symbol's section .debug_pubtypes is !SHF_ALLOC
I am not sure what to make out of it. As in the code ignores these sections. I tried to ake the test code strip these out:
strip -d
But that stripped out the relocation entries out to! Doing just:
strip -R .comment
Did the same exact thing (ripped out .rela*).
Keep in mind that this code above does not set 'rc' at all - so the rc is 0 by default and we just ignore this specific section.
I would rather keep it this way - otherwise I have to make the test code go through an hand-rolled linker script - I can't use Xen's (xen.lds.S) as it has the ASSERTS that get triggered:
#ifdef CONFIG_KEXECASSERT(kexec_reloc_size - kexec_reloc <= PAGE_SIZE, "kexec_reloc is too large")
#endif
As the object file has none of these entries in it.
What's your preference? Leave it as is (so skip over those sections), or be pedantic on them and require no !SHF_ALLOC sections?
And also bail out if any of the sections don't have an st_type that we implement, such as .comment:
[28] .comment PROGBITS 0000000000000000 000002a4
000000000000005a 0000000000000001 MS 0 0 1
Or let them be but not do anything to them (whcih is what we currently do).
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 17:15, <[email protected]> wrote:
Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 11 lines
Review struct payload {
Quoted 1 line
(quoted diff) +0
-0@@ -29,6 +30,13 @@ struct payload {>uint32_t state; /* One of the XSPLICE_STATE_*. */ >int32_t rc; /* 0 or -XEN_EXX. */ >struct list_head list; /* Linked to 'payload_list'. */ >+ void *text_addr; /* Virtual address of .text. */ >+ size_t text_size; /* .. and its size. */ >+ void *rw_addr; /* Virtual address of .data. */ >+ size_t rw_size; /* .. and its size (if any). */ >+ void *ro_addr; /* Virtual address of .rodata. */ >+ size_t ro_size; /* .. and its size (if any). */Quoted 4 lines
Quoted 1 line
And again the question: Do these pointers really need to be non-const?
I know I tried making them const and the compiler was not happy. I will follow up with the reasoning.
The big problem I ran in was that I had to do casting in '
modify_payload'. To lower the amount of casting I ended making
load_addrbe void * (if it was const void * I would have to cast away the constness inxsplice_elf.c):
Quoted 6 lines
elf->sec[i].load_addr = (void *)buf + offset[i];
where 'buf' is:
buf =payload->text_addr;(and buf it also a const void *).
Hence (taking all of the above) without any cast and with load_addr being const void *, text_addr could be so, too. Or else the picture above isn't the complete one.
Quoted 10 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
xen/common/xsplice_elf.c +1
-0+int xsplice_elf_resolve_symbols (struct xsplice_elf *elf).. snip..
Quoted 1 line
Quoted 1 line
+ default:
.. snip..
Quoted 4 lines
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if ( !(elf->sec[idx].sec->sh_flags & SHF_ALLOC) ) + break;If you really mean to check this, shouldn't this be done earlier, avoiding
needless
Quoted 1 line
Quoted 1 line
errors on unsupported symbol kinds above?
With all of the ..snip..-s above, it's now really hard to answer this:
Quoted 13 lines
So I am coming back to this. The ones that this hits are:
Symbol's section .note.GNU-stack is !SHF_ALLOC Symbol's section .comment is !SHF_ALLOC Symbol's section .debug_aranges is !SHF_ALLOC Symbol's section .debug_pubnames is !SHF_ALLOC Symbol's section .debug_info is !SHF_ALLOC Symbol's section .debug_abbrev is !SHF_ALLOC Symbol's section .debug_line is !SHF_ALLOC Symbol's section .debug_frame is !SHF_ALLOC Symbol's section .debug_str is !SHF_ALLOC Symbol's section .debug_loc is !SHF_ALLOC Symbol's section .debug_pubtypes is !SHF_ALLOC
Quoted 2 lines
I am not sure what to make out of it. As in the code ignores these sections.
Right, because these sections are of no interest to the loading process. And hence none of the symbols (if there really are any) in those sections should be of any interest either.
Quoted 5 lines
I tried to ake the test code strip these out:
strip -d
But that stripped out the relocation entries out to!
Was that all relocations, or just those referencing the sections above?
Quoted 5 lines
Doing just:
strip -R .comment
Did the same exact thing (ripped out .rela*).
That sounds wrong.
Quoted 12 lines
Keep in mind that this code above does not set 'rc' at all - so the rc is 0 by default and we just ignore this specific section.
I would rather keep it this way - otherwise I have to make the test code go through an hand-rolled linker script - I can't use Xen's (
xen.lds.S) as it has the ASSERTS that get triggered:#ifdef
CONFIG_KEXECASSERT(kexec_reloc_size - kexec_reloc <= PAGE_SIZE, "kexec_reloc is too large")
#endif
Quoted 4 lines
As the object file has none of these entries in it.
What's your preference? Leave it as is (so skip over those sections), or be pedantic on them and require no !SHF_ALLOC sections?
I'm not sure I'm with you here: I didn't ask for anything to be removed or added, all I had asked is whether that check shouldn't be moved up.
Quoted 8 lines
And also bail out if any of the sections don't have an
st_typethat we implement, such as .comment:[28] .comment PROGBITS 0000000000000000
000002a4000000000000005a 0000000000000001 MS 0 0 1Or let them be but not do anything to them (whcih is what we currently do).
I don't think I can follow you here either.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 36 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 17:15, <[email protected]> wrote:
On Wed, Apr 20, 2016 at 11:59:34AM -0400, Konrad Rzeszutek Wilk
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Review struct payload {
Quoted 1 line
(quoted diff) +0
-0@@ -29,6 +30,13 @@ struct payload {>uint32_t state; /* One of the
XSPLICE_STATE_*. */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
>int32_t rc; /* 0 or -XEN_EXX. */
>structlist_headlist; /* Linked to'
payload_list'. */Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 30
Quoted 1 line
(quoted diff) +1
-0+ void *text_addr; /* Virtual address of.text. */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review struct payload { · line 32
Quoted 2 lines
(quoted diff) +2
-0+ size_t text_size; /* .. and its size. */ + void *rw_addr; /* Virtual address of.data. */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 33
Quoted 1 line
(quoted diff) +1
-0+ size_t rw_size; /* .. and its size (ifany). */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 34
Quoted 1 line
(quoted diff) +1
-0+ void *ro_addr; /* Virtual address of.rodata. */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 35
Quoted 1 line
(quoted diff) +1
-0+ size_t ro_size; /* .. and its size (ifany). */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
And again the question: Do these pointers really need to be
non-const?
Quoted 2 lines
Quoted 2 lines
I know I tried making them const and the compiler was not happy. I
will
Quoted 10 lines
Quoted 1 line
follow up with the reasoning.
The big problem I ran in was that I had to do casting in '
modify_payload'. To lower the amount of casting I ended making
load_addrbe void * (if it was const void * I would have to cast away the constness inxsplice_elf.c):elf->sec[i].load_addr = (void *)buf + offset[i];
Quoted 9 lines
Quoted 4 lines
where 'buf' is:
buf =payload->text_addr;(and buf it also a const void *).
Hence (taking all of the above) without any cast and with
load_addrbeing const void *,text_addrcould be so, too. Or else the picture above isn't the complete one.
If I did that I will need to uncast constness when memcpy data in (a couple of lines below in move_payload). And also when doing relocation.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
xen/common/xsplice_elf.c +1
-0+int xsplice_elf_resolve_symbols (struct xsplice_elf *elf).. snip..
Quoted 1 line
Quoted 1 line
+ default:
.. snip..
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
(quoted diff) +2
-0+ if ( !(elf->sec[idx].sec->sh_flags & SHF_ALLOC) ) + break;
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
If you really mean to check this, shouldn't this be done earlier,
avoiding
Quoted 2 lines
needless
Quoted 1 line
Quoted 1 line
errors on unsupported symbol kinds above?
With all of the ..snip..-s above, it's now really hard to answer this:
Sorry.
Quoted 14 lines
Quoted 14 lines
So I am coming back to this. The ones that this hits are:
Symbol's section .note.GNU-stack is !SHF_ALLOC Symbol's section .comment is !SHF_ALLOC Symbol's section .debug_aranges is !SHF_ALLOC Symbol's section .debug_pubnames is !SHF_ALLOC Symbol's section .debug_info is !SHF_ALLOC Symbol's section .debug_abbrev is !SHF_ALLOC Symbol's section .debug_line is !SHF_ALLOC Symbol's section .debug_frame is !SHF_ALLOC Symbol's section .debug_str is !SHF_ALLOC Symbol's section .debug_loc is !SHF_ALLOC Symbol's section .debug_pubtypes is !SHF_ALLOC
Quoted 7 lines
Quoted 2 lines
I am not sure what to make out of it. As in the code ignores these sections.
Right, because these sections are of no interest to the loading process. And hence none of the symbols (if there really are any) in those sections should be of any interest either.
OK, so we are in agreement.
Quoted 9 lines
Quoted 5 lines
I tried to ake the test code strip these out:
strip -d
But that stripped out the relocation entries out to!
Was that all relocations, or just those referencing the sections above?
All. For .text as well.
Quoted 7 lines
Quoted 5 lines
Doing just:
strip -R .comment
Did the same exact thing (ripped out .rela*).
That sounds wrong.
Yes. Maybe the version I have is buggy.
Quoted 14 lines
Quoted 10 lines
Keep in mind that this code above does not set 'rc' at all - so the rc is 0 by default and we just ignore this specific section.
I would rather keep it this way - otherwise I have to make the test code go through an hand-rolled linker script - I can't use Xen's (
xen.lds.S) as it has the ASSERTS that get triggered:#ifdef
CONFIG_KEXEC
ASSERT(kexec_reloc_size -kexec_reloc<=PAGE_SIZE, "kexec_relocistoo
Quoted 3 lines
large")
#endif
Quoted 7 lines
Quoted 4 lines
As the object file has none of these entries in it.
What's your preference? Leave it as is (so skip over those sections), or be pedantic on them and require no !SHF_ALLOC sections?
I'm not sure I'm with you here: I didn't ask for anything to be removed or added, all I had asked is whether that check shouldn't be moved up.
Oh. Yes, I can do that!
Quoted 5 lines
Quoted 2 lines
And also bail out if any of the sections don't have an
st_typethatwe
Quoted 2 lines
implement, such as .comment:
Quoted 8 lines
Quoted 4 lines
[28] .comment PROGBITS 0000000000000000
000002a4000000000000005a 0000000000000001 MS 0 0 1Or let them be but not do anything to them (whcih is what we
currently
Quoted 1 line
do).
I don't think I can follow you here either.
This is a bit of tangent on this above .Let me reply on proper thread.
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 11/27] xsplice: Implement payload loading
Quoted 43 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 18:47, <[email protected]> wrote:
Quoted 41 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 17:15, <[email protected]> wrote:
On Wed, Apr 20, 2016 at 11:59:34AM -0400, Konrad Rzeszutek Wilk
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Review struct payload {
Quoted 1 line
(quoted diff) +0
-0@@ -29,6 +30,13 @@ struct payload {>uint32_t state; /* One of the
XSPLICE_STATE_*. */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
>int32_t rc; /* 0 or -XEN_EXX. */
>structlist_headlist; /* Linked to'
payload_list'. */Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 30
Quoted 1 line
(quoted diff) +1
-0+ void *text_addr; /* Virtual address of.text. */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review struct payload { · line 32
Quoted 2 lines
(quoted diff) +2
-0+ size_t text_size; /* .. and its size. */ + void *rw_addr; /* Virtual address of.data. */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 33
Quoted 1 line
(quoted diff) +1
-0+ size_t rw_size; /* .. and its size (ifany). */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 34
Quoted 1 line
(quoted diff) +1
-0+ void *ro_addr; /* Virtual address of.rodata. */
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review struct payload { · line 35
Quoted 1 line
(quoted diff) +1
-0+ size_t ro_size; /* .. and its size (ifany). */
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
And again the question: Do these pointers really need to be
non-const?
Quoted 2 lines
Quoted 2 lines
I know I tried making them const and the compiler was not happy. I
will
Quoted 14 lines
Quoted 1 line
follow up with the reasoning.
The big problem I ran in was that I had to do casting in '
modify_payload'. To lower the amount of casting I ended making
load_addrbe void * (if it was const void * I would have to cast away the constness inxsplice_elf.c):elf->sec[i].load_addr = (void *)buf + offset[i];
where 'buf' is:
buf =payload->text_addr;(and buf it also a const void *).
Quoted 7 lines
Quoted 4 lines
Hence (taking all of the above) without any cast and with
load_addrbeing const void *,text_addrcould be so, too. Or else the picture above isn't the complete one.If I did that I will need to uncast constness when memcpy data in (a couple of lines below in
move_payload).
Ehem - no, not really. This is what the patch has:
uint8_t *buf;
...
buf = arch_xsplice_alloc_payload(size);
...
elf->sec[i].load_addr = buf + elf->sec[i].sec->sh_entsize;
...
memcpy(elf->sec[i].load_addr, elf->sec[i].data,
elf->sec[i].sec->sh_size);So all it would take is to substitute the first argument to memcpy() by an expression involving buf (i.e. the right side of the earlier assignment, or some intermediate variable used to avoid having the same expression twice).
Quoted 1 line
And also when doing relocation.
There it would then be really unavoidable. That's a place where I would say casting away constness could be considered acceptable, but I'd like to come back to your earlier reply first: You refer to modify_payload() and an expression involving "buf + offset[i]", neither of which I can spot anywhere in this patch. Oh, looks like you mean this
if ( (elf->sec[i].sec->sh_flags & SHF_EXECINSTR) )
buf = payload->text_addr;
else if ( (elf->sec[i].sec->sh_flags & SHF_WRITE) )
buf = payload->rw_addr;
else
buf = payload->ro_addr;
elf->sec[i].load_addr = buf + elf->sec[i].sec->sh_entsize;in move_payload(). But that's the same as above then - by using suitable intermediate variables you can deal with the structure fields pointing to const quite easily, since the loop the above is in is preceded by
payload->text_addr = buf;
payload->rw_addr = payload->text_addr + PAGE_ALIGN(payload->text_size);
payload->ro_addr = payload->rw_addr + PAGE_ALIGN(payload->rw_size);and buf points to non-const.
Bottom line: While doing the loading it is clear that some of the image needs to be modified, and hence you need pointers to non-const, or cast away constness under rare circumstances. But since for the whole rest of the lifetime of the module you do want to express that the image pieces are not supposed to change, the goal ought to be to have as many as possible pointers to const in the control structure.
And I'm really sorry for being this picky, you simply suffer from me having written more than one image loader as well as a linker and object file analysis/massaging tools in the past.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 12/27 xsplice: Implement support for applying/reverting/replacing patches.
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Implement support for the apply, revert and replace actions.
To perform and action on a payload, the hypercall sets up a data structure to schedule the work. A hook is added in the reset_stack_and_jump to check for work and execute it if needed (specifically we check an per-cpu flag to make this as quick as possible).
In this way, patches can be applied with all CPUs idle and without stacks. The first CPU to run check_for_xsplice_work() becomes the master and triggers a reschedule softirq to trigger all the other CPUs to enter check_for_xsplice_work() with no stack. Once all CPUs have rendezvoused, all CPUs disable IRQs and NMIs are ignored. The system is then quiscient and the master performs the action. After this, all CPUs enable IRQs and NMIs are re-enabled.
Note that it is unsafe to patch do_nmi and the xSplice internal functions. Patching functions on NMI/MCE path is liable to end in disaster. This is not addressed in this patch and is mentioned in the design doc as a further TODO.
The action to perform is one of:
- APPLY: For each function in the module, store the first 5 bytes of the old function and replace it with a jump to the new function.
- REVERT: Copy the previously stored bytes into the first 5 bytes of the old function.
- REPLACE: Revert each applied module and then apply the new module.
To prevent a deadlock with any other barrier in the system, the master will wait for up to 30ms before timing out.
Measurements found that the patch application to take about 100 μs on a 72 CPU system, whether idle or fully loaded.
We also add an BUILD_ON to make sure that the size of the structure of the payload is not inadvertly changed and that the offsets are correct on both 32 and 64-bit hypervisor (ARM32 and ARM64). To make this work, we make the public header use architecture neutral members (xen_ulong_t) but for per-architecture have to fiddle with padding.
--
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
v2: - Pluck the 'struct xsplice_patch_func' in this patch.
- Modify code per review comments.
- Add more data in the keyboard handler.
- Redo the patching code, split it in functions.
v3: - Add return_ macro for debug builds.
- Move s/payload_list_lock/payload_list/ to earlier patch
- Remove const and use ELF types for xsplice_patch_func
- Add check routine to do simple sanity checks for various sections.
- s/%p/PRIx64/ as ARM builds complain.
- Move code around. Add more dprintk. Add XSPLICE in front of all printks/dprintk.
Put the NMIs back if we fail patching.
Add per-cpu to lessen contention for global structure. Extract from xsplice_do_single patching code into xsplice_do_action Squash xsplice_do_single and check_for_xsplice_work together to have all rendezvous in one place.
Made XSPLICE_ACTION_REPLACE work again (wrong list iterator) s/find_special_sections/prepare_payload/
Use list_del_init and INIT_LIST_HEAD for applied_list
v4:
- Add comment, adjust spacing for "Timed out on CPU semaphore"
- Added CR0.WP manipulations when altering the .text of hypervisor.
- Added fix from Andrew for CR0.WP manipulation.
v5: - Made xsplice_patch_func use uintXX_t instead of ELF_ types to easy
making it work under ARM (32bit). Add more BUILD-BUG-ON checks.
- Add more BUILD_ON checks. Sprinkle newlines.
v6: Rebase on "arm/x86: Alter nmi_callback_t typedef"
- Drop the recursive spinlock usage.
- Move NMI callbacks in arch specific.
- Fold the 'check_for_xsplice_work' in reset_stack_and_jump
- Add arch specific check for .xsplice.funcs.
- Seperate external and internal structure of .xsplice.funcs.
- Changed per Jan's review
- Modified the .xsplice.funcs checks
v7:
- Modified old_ptr to void* instead of uint8_t*
- Modified the xsplice_patch_func_internal for ARM32 to have padding.
- Used #if BITS_PER_LONG == 64 for the xsplice_patch_func_internal along with ifndef CONFIG_ARM for the undo (which may be different size on ARM64)
v8:
- Add "is empty" if special sections are in fact empty.
- Added Andrew's Reviewed-by:
- Rebase on v7.2 of x86/mm: Introduce modify_xen_mappings()
- Change some of printk to dprintk and some of the dprintk to printk.
- Make the xsplice_patch_func (and the internal) structure have uint32_t (instead of uint64_t) if BITS_PER_LONG==32. This makes the size and offset different so note that in the design and common code.
- Add #undef ACTION
- Guard struct xsplice_patch_func in sysctl.h with __XEN__ as toolstacks will fail to compile. We do have BITS_PER_LONG defined in xc_bitops.h but that will go away (and also that macro uses sizeof and the pre-processor will choke on that).
- Dropped Julien's Acked as I replaced BITS_PER_LONG/CONFIG_ARM_32. (Stefano is OK with it, but would prefer BITS_PER_LONG, Jan does not want BITS_PER_LONG).
--- docs/misc/xsplice.markdown | 8 +- xen/arch/arm/xsplice.c | 33 +++ xen/arch/x86/domain.c | 2 + xen/arch/x86/xsplice.c | 88 +++++++- xen/common/xsplice.c | 473 +++++++++++++++++++++++++++++++++++++++++- xen/include/asm-x86/current.h | 10 +- xen/include/public/sysctl.h | 27 +++ xen/include/xen/xsplice.h | 47 +++++ 8 files changed, 670 insertions(+), 18 deletions(-)
docs/misc/xsplice.markdown +7 -1
@@ -289,8 +289,13 @@ describing the functions to be patched: <pre> struct xsplice_patch_func { const char *name; +#if BITS_PER_LONG == 32 + uint32_t new_addr; + uint32_t old_addr; +#else uint64_t new_addr; uint64_t old_addr; +#endif uint32_t new_size; uint32_t old_size; uint8_t version;
@@ -298,7 +303,8 @@ struct xsplice_patch_func { }; </pre> -The size of the structure is 64 bytes. +The size of the structure is 64 bytes or 52 bytes if compiled under 32-bit +hypervisors. * `name` is the symbol name of the old function. Only used if `old_addr` is zero, otherwise will be used during dynamic linking (when hypervisor loads
xen/arch/arm/xsplice.c +33 -0
@@ -6,6 +6,39 @@#include <xen/xsplice_elf.h> #include <xen/xsplice.h> +void arch_xsplice_patching_enter (void ) +{ +} + +void arch_xsplice_patching_leave (void ) +{ +} + +int arch_xsplice_verify_func (const struct xsplice_patch_func_internal *func) +{ +return -ENOSYS ; +} + +void arch_xsplice_apply_jmp (struct xsplice_patch_func_internal *func) +{ +} + +void arch_xsplice_revert_jmp (const struct xsplice_patch_func_internal *func) +{ +} + +void arch_xsplice_post_action (void ) +{ +} + +void arch_xsplice_mask (void ) +{ +} + +void arch_xsplice_unmask (void ) +{ +} +int arch_xsplice_verify_elf (const struct xsplice_elf *elf) {return -ENOSYS ;
xen/arch/x86/domain.c +2 -0
@@ -36,6 +36,7 @@#include <xen/cpu.h> #include <xen/wait.h> #include <xen/guest_access.h> +#include <xen/xsplice.h> #include <public/sysctl.h> #include <public/hvm/hvm_vcpu.h> #include <asm/regs.h>
@@ -120,6 +121,7 @@ static void idle_loop(void) (*pm_idle)();do_tasklet ();do_softirq (); +check_for_xsplice_work ();/* Must be last. */ } }
xen/arch/x86/xsplice.c +82 -6
100 lines not on this page · raw message
xen/common/xsplice.c +463 -10
568 lines not on this page · raw message
xen/include/asm-x86/current.h +9 -1
@@ -86,10 +86,18 @@ static inline struct cpu_info *get_cpu_info(void)unsigned long get_stack_trace_bottom (unsigned long sp);unsigned long get_stack_dump_bottom (unsigned long sp); +#ifdef CONFIG_XSPLICE +# define __CHECK_FOR_XSPLICE_WORK "call check_for_xsplice_work;" +#else +# define __CHECK_FOR_XSPLICE_WORK "" +#endif +#define reset_stack_and_jump (__fn) \ ({ \ __asm__ __volatile__ ( \ - "mov %0,%%"__OP"sp; jmp %c1" \ + "mov %0,%%"__OP"sp;" \ + __CHECK_FOR_XSPLICE_WORK \ + "jmp %c1" \ : : "r" (guest_cpu_user_regs()), "i" (__fn) : "memory" ); \ unreachable(); \ })
xen/include/public/sysctl.h +27 -0
@@ -833,6 +833,33 @@ DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t);* If zero exit with success. */ +#define XSPLICE_PAYLOAD_VERSION 1 +/* +* .xsplice.funcs structure layout defined in the `Payload format` +* section in the xSplice design document. +* +* The size should be exactly 64 bytes (64) or 52 bytes (32). +* +* We guard this with __XEN__ as toolstacks do not need to use it. +*/ +#ifdef __XEN__ +struct xsplice_patch_func { +const char *name;/* Name of function to be patched. */ +#if CONFIG_ARM_32 +uint32_t new_addr; +uint32_t old_addr; +#else +uint64_t new_addr; +uint64_t old_addr;/* Can be zero and name will be looked up. */ +#endif +uint32_t new_size; +uint32_t old_size; +uint8_t version;/* MUST be XSPLICE_PAYLOAD_VERSION. */ +uint8_t pad[31 ];/* MUST be zero filled. */ +}; +typedef struct xsplice_patch_func xsplice_patch_func_t ; +#endif +/* * Structure describing an ELF payload. Uniquely identifies the * payload. Should be human readable.
xen/include/xen/xsplice.h +47 -0
71 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
docs/misc/xsplice.markdown +0
-0@@ -289,8 +289,13 @@ describing the functions to be patched:<pre>
structxsplice_patch_func{
>const char *name;
docs/misc/xsplice.markdown describing the functions to be patched: · line 295
Quoted 4 lines
docs/misc/xsplice.markdown +4
-0+#if BITS_PER_LONG == 32 + uint32_t new_addr; + uint32_t old_addr; +#else
>uint64_t new_addr;
>uint64_t old_addr; Quoted 1 line
+#endif
I don't think this belongs here (instead it should go wherever the structure gets introduced).
docs/misc/xsplice.markdown struct xsplice_patch_func { · line 307
Quoted 7 lines
docs/misc/xsplice.markdown +0
-0@@ -298,7 +303,8 @@ struct xsplice_patch_func { };</pre>
docs/misc/xsplice.markdown +2
-1-The size of the structure is 64 bytes. +The size of the structure is 64 bytes or 52 bytes if compiled under 32-bit +hypervisors.
And I'm uncertain this sentence is of much value.
docs/misc/xsplice.markdown static void idle_loop(void)
Quoted 1 line
docs/misc/xsplice.markdown +0
-0@@ -120,6 +121,7 @@ static void idle_loop(void)
>(*pm_idle)();
>do_tasklet();
>do_softirq();docs/misc/xsplice.markdown static void idle_loop(void) · line 121
Quoted 1 line
docs/misc/xsplice.markdown +1
-0+ check_for_xsplice_work(); /* Must be last. */
Without also saying why, the comment isn't very useful imo.
docs/misc/xsplice.markdown static void idle_loop(void)
Quoted 7 lines
docs/misc/xsplice.markdown +7
-0+void arch_xsplice_apply_jmp(struct xsplice_patch_func_internal *func) +{ + int32_t val; + uint8_t *old_ptr; + + BUILD_BUG_ON(PATCH_INSN_SIZE > sizeof(func->u.undo)); + BUILD_BUG_ON(PATCH_INSN_SIZE != (1 + sizeof val));
Standard sizeof() please.
docs/misc/xsplice.markdown
Quoted 6 lines
docs/misc/xsplice.markdown +6
-0+ old_ptr = func->old_addr; + memcpy(func->u.undo, old_ptr, PATCH_INSN_SIZE); + + *old_ptr++ = 0xe9; /* Relative jump */ + val = func->new_addr - func->old_addr - PATCH_INSN_SIZE; + memcpy(old_ptr, &val, sizeof val);
Again (and I won't make further comments to that effect, the more that this was requested in a previous round already anyway).
Quoted 4 lines
+/*
+ * Note that because of this NOP code thedo_nmiis not safely patchable.
+ * Also if we do receive 'real' NMIs we have lost them. Ditto for MCE.
+ */
The reference to MCE is misleading here: You don't redirect the latter here, so its handling is not the same as NMI.
docs/misc/xsplice.markdown int arch_xsplice_secure(void *va, unsigned int pages, enum va_type type)
Quoted 1 line
docs/misc/xsplice.markdown +0
-0@@ -148,12 +224,12 @@ int arch_xsplice_secure(void *va, unsigned int pages, enum va_type type)
>ASSERT(va);
>ASSERT(pages);docs/misc/xsplice.markdown int arch_xsplice_secure(void *va, unsigned int pages, enum va_type type) · line 229
Quoted 13 lines
docs/misc/xsplice.markdown +6
-6- if ( type == XSPLICE_VA_RX ) /* PAGE_HYPERVISOR_RX */ - flag = _PAGE_PRESENT; - else if ( type == XSPLICE_VA_RW ) /* PAGE_HYPERVISOR_RW */ - flag = _PAGE_RW | _PAGE_NX | _PAGE_PRESENT; - else /* PAGE_HYPERVISOR_RO */ - flag = _PAGE_NX | _PAGE_PRESENT; + if ( type == XSPLICE_VA_RX ) + flag = PAGE_HYPERVISOR_RX; + else if ( type == XSPLICE_VA_RW ) + flag = PAGE_HYPERVISOR_RW; + else + flag = PAGE_HYPERVISOR_RO;
Misplaced hunk (belongs in an earlier patch, and matches exactly what I had asked
for there).
Quoted 15 lines
docs/misc/xsplice.markdown +2
-1@@ -11,18 +12,28 @@ #include <xen/mm.h> #include <xen/sched.h> #include <xen/smp.h> +#include <xen/softirq.h> #include <xen/spinlock.h> #include <xen/vmap.h> +#include <xen/wait.h> #include <xen/xsplice_elf.h> #include <xen/xsplice.h> #include <asm/event.h> #include <public/sysctl.h> -/* Protects against payload_list operations. */
docs/misc/xsplice.markdown line 34
Quoted 10 lines
docs/misc/xsplice.markdown +7
-0+/* + * Protects against payload_list operations and also allows only one + * caller in schedule_work. + */ static DEFINE_SPINLOCK(payload_lock); static LIST_HEAD(payload_list); +/* + * Patches which have been applied. + */
Ehem - comment style (still).
docs/misc/xsplice.markdown line 39
Quoted 5 lines
docs/misc/xsplice.markdown +5
-0+/* Defines an outstanding patching action. */ +struct xsplice_work +{ + atomic_t semaphore; /* Used for rendezvous. */ + atomic_t irq_semaphore; /* Used to signal all IRQs disabled. */
Why do you, btw, need two of them? I would seem to me that having just one for both purposes should be fine.
Quoted 15 lines
docs/misc/xsplice.markdown +15
-0+static int check_special_sections(const struct xsplice_elf *elf) +{ + unsigned int i; + static const char *const names[] = { ".xsplice.funcs" }; + unsigned int count[ARRAY_SIZE(names)] = { 0 }; + + for ( i = 0; i < ARRAY_SIZE(names); i++ ) + { + const struct xsplice_elf_sec *sec; + + sec = xsplice_elf_sec_by_name(elf, names[i]); + if ( !sec ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: %s is missing!\n", + elf->name, names[i]);
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+ return -EINVAL; + } + + if ( !sec->sec->sh_size ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: %s is empty!\n", + elf->name, names[i]); + return -EINVAL; + } + count[i]++;
To conserve stack space, use a bool_t array instead and do the check-and-exit here instead of in a second loop.
docs/misc/xsplice.markdown
Quoted 6 lines
docs/misc/xsplice.markdown +6
-0+static int prepare_payload(struct payload *payload, + struct xsplice_elf *elf) +{ + const struct xsplice_elf_sec *sec; + unsigned int i; + struct xsplice_patch_func_internal *f;
Why is there a second ("internal") variant of this structure now? What guarantees it to remain in sync with the non-"internal" one?
Quoted 1 line
+ sec =
xsplice_elf_sec_by_name(elf, ".xsplice.funcs");
Since the string literal occurs at least twice, I'd suggest having a #define for it.
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+ ASSERT(sec); + if ( sec->sec->sh_size % sizeof(*payload->funcs) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .xsplice.funcs!\n", + elf->name); + return -EINVAL; + } + + payload->funcs = sec->load_addr; + payload->nfuncs = sec->sec->sh_size / sizeof(*payload->funcs);
Following to our discussion yesterday - can't we (ab)use the section merge flag here to report the structure size, along the lines of what relocation sections do for their elements?
Quoted 19 lines
docs/misc/xsplice.markdown +19
-0+ for ( i = 0; i < payload->nfuncs; i++ ) + { + int rc; + unsigned int j; + + f = &(payload->funcs[i]); + + if ( f->version != XSPLICE_PAYLOAD_VERSION ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong version (%u). Expected %d!\n", + elf->name, f->version, XSPLICE_PAYLOAD_VERSION); + return -EOPNOTSUPP; + } + + if ( !f->new_addr || !f->new_size ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Address or size fields are zero!\n", + elf->name); + return -EINVAL;
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+ } + + rc = arch_xsplice_verify_func(f); + if ( rc ) + return rc; + + for ( j = 0; j < ARRAY_SIZE(f->u.pad); j++ ) + if ( f->u.pad[j] ) + return -EINVAL; + }
Is that actually useful? It doesn't really matter what's in that space.
docs/misc/xsplice.markdown
Quoted 5 lines
docs/misc/xsplice.markdown +5
-0+static int apply_payload(struct payload *data) +{ + unsigned int i; + + printk(XENLOG_INFO XSPLICE "%s: Applying %u functions.\n",
No full stops in log messages please.
Quoted 5 lines
+/*
+ * This function is executed having all other CPUs with no stack (we may
+ * havecpu_idleon it) and IRQs disabled. We guard against NMI by temporarily
+ * installing our NOP NMI handler.
+ */
The recurring (from the design doc) use of the wording "with no stack" has been puzzling me from the beginning. Nothing really executes with no stack. How about "without deep call stack" or some such?
Also - aren't we in common code here? If so, a reference to NMI doesn't belong here (and a reference to MCE should really be added if the comment got moved elsewhere).
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+static void xsplice_do_action(void) +{ + int rc; + struct payload *data, *other, *tmp; + + data = xsplice_work.data; + /* + * Now this function should be the only one on any stack. + * No need to lock the payload list or applied list. + */
Is "the only one" really a precise, architecture independent statement? Aren't we getting here perhaps with at least one more return address on the stack (the transition back from C to assembly code)?
Quoted 7 lines
docs/misc/xsplice.markdown +7
-0+static int xsplice_spin(atomic_t *counter, s_time_t timeout, + unsigned int cpus, const char *s) +{ + int rc = 0; + + while ( atomic_read(counter) != cpus && NOW() < timeout ) + cpu_relax();
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+ + /* Log & abort. */ + if ( atomic_read(counter) != cpus ) + { + printk(XENLOG_ERR XSPLICE "%s: Timed out on %s semaphore %u/%u\n", + xsplice_work.data->name, s, atomic_read(counter), cpus); + rc = -EBUSY; + xsplice_work.data->rc = rc; + xsplice_work.do_work = 0; + smp_wmb();
Doesn't this need to move one line up? There's no further write following here, so there's nothing this barrier separates right now.
docs/misc/xsplice.markdown
Quoted 3 lines
docs/misc/xsplice.markdown +3
-0+ /* + * Do NOT decrement semaphore down - as that may cause the other + * CPU (which may be at this ready to increment it)
Odd wording?
docs/misc/xsplice.markdown
Quoted 4 lines
docs/misc/xsplice.markdown +4
-0+ * to assume the role of master and then needlessly time out + * out (as do_work is zero). + */ + smp_wmb();
Same comment as above regarding placement.
docs/misc/xsplice.markdown
Quoted 2 lines
docs/misc/xsplice.markdown +2
-0+ /* Flush the CPU i-cache via CPUID instruction (on x86). */ + arch_xsplice_post_action();
CPUID, as mentioned before, does not do any i-cache flushing.
docs/misc/xsplice.markdown
Quoted 7 lines
+ abort:
docs/misc/xsplice.markdown +6
-0+ per_cpu(work_to_do, cpu) = 0; + xsplice_work.do_work = 0; + + smp_wmb(); /* MUST complete writes before put_cpu_maps(). */ + + put_cpu_maps();
put_cpu_maps() is a spin unlock, i.e. a write barrier anyway.
docs/misc/xsplice.markdown static void xsplice_printall(unsigned char key)
Quoted 1 line
docs/misc/xsplice.markdown +0
-0@@ -597,15 +1022,43 @@ static void xsplice_printall(unsigned char key)
>}
Quoted 1 line
>list_for_each_entry ( data, &payload_list, list )
Quoted 1 line
+ {
>printk(" name=%s state=%s(%d) %p (.data=%p, .rodata=%p) using %zu pages.\n",
>data->name, state2str(data->state), data->state, data->text_addr,
>data->rw_addr, data->ro_addr, data->pages);docs/misc/xsplice.markdown
Quoted 12 lines
docs/misc/xsplice.markdown +11
-0+ for ( i = 0; i < data->nfuncs; i++ ) + { + struct xsplice_patch_func_internal *f = &(data->funcs[i]); + printk(" %s patch %p(%u) with %p (%u)\n", + f->name, f->old_addr, f->old_size, f->new_addr, f->new_size); + + if ( i && !(i % 64) ) + { + spin_unlock(&payload_lock); + process_pending_softirqs(); + spin_lock(&payload_lock);
Iirc the initial acquire now intentionally is a try-lock. Hence it's quite clear that this one can't be anything else either.
Quoted 4 lines
static int
__initxsplice_init(void)
{docs/misc/xsplice.markdown +2
-0+ BUILD_BUG_ON( sizeof(struct xsplice_patch_func) != + sizeof(struct xsplice_patch_func_internal) );
docs/misc/xsplice.markdown
Quoted 10 lines
docs/misc/xsplice.markdown +10
-0+ BUILD_BUG_ON( sizeof(struct xsplice_patch_func_internal) != + XSPLICE_PATCH_FUNC_INTERNAL_SIZE ); + +#if BITS_PER_LONG == 64 + BUILD_BUG_ON( offsetof(struct xsplice_patch_func, new_addr) != 8 ); + BUILD_BUG_ON( offsetof(struct xsplice_patch_func, new_size) != 24 ); +#else + BUILD_BUG_ON( offsetof(struct xsplice_patch_func, new_addr) != 4 ); + BUILD_BUG_ON( offsetof(struct xsplice_patch_func, new_size) != 12 ); +#endif
I don't see the point of these: If anything, you want to make sure "internal" and normal structure offsets don't disagree.
xen/include/asm-x86/current.h static inline struct cpu_info *get_cpu_info(void) · line 93
Quoted 11 lines
xen/include/asm-x86/current.h +5
-0@@ -86,10 +86,18 @@ static inline struct cpu_info *get_cpu_info(void)unsigned long get_stack_trace_bottom (unsigned long sp);unsigned long get_stack_dump_bottom (unsigned long sp); +#ifdef CONFIG_XSPLICE +# define __CHECK_FOR_XSPLICE_WORK "call check_for_xsplice_work;" +#else +# define __CHECK_FOR_XSPLICE_WORK "" +#endif
No leading underscores please.
xen/include/public/sysctl.h DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t); · line 841
Quoted 12 lines
xen/include/public/sysctl.h +6
-0@@ -833,6 +833,33 @@ DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t);* If zero exit with success. */ +#define XSPLICE_PAYLOAD_VERSION 1 +/* +* .xsplice.funcs structure layout defined in the `Payload format` +* section in the xSplice design document. +* +* The size should be exactly 64 bytes (64) or 52 bytes (32).
Putting aside the usefulness of this comment, the items in parentheses aren't really clear - DYM 64-bit and 32-bit respectively?
xen/include/public/sysctl.h DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t); · line 843
Quoted 1 line
xen/include/public/sysctl.h +1
-0+* We guard this with __XEN__ as toolstacks do not need to use it.
I'd make this even more strict, if you want such a comment at all.
xen/include/public/sysctl.h DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t); · line 855
Quoted 13 lines
xen/include/public/sysctl.h +13
-0+/* +* The structure which defines the patching. This is what the hypervisor +* expects in the '.xsplice.func' section of the ELF file. +* +* This MUST be in sync with what the tools generate. We expose +* for the tools the 'struct xsplice_patch_func' which does not have +* platform specific entries. +*/ +#if BITS_PER_LONG ==64 +#define XSPLICE_PATCH_FUNC_INTERNAL_SIZE 64 +#else +#define XSPLICE_PATCH_FUNC_INTERNAL_SIZE 52 +#endif
This is getting really odd. I can only hope all this goes away with ...
xen/include/public/sysctl.h DEFINE_XEN_GUEST_HANDLE(xen_sysctl_featureset_t); · line 856
Quoted 1 line
xen/include/public/sysctl.h +1
-0+struct xsplice_patch_func_internal {
... this getting folded back into the public header version.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 9 lines
xen/common/xsplice.c static int secure_payload(struct payload *payload, struct xsplice_elf *elf) · line 340
Quoted 6 lines
xen/common/xsplice.c +6
-0+static int prepare_payload (struct payload *payload, +struct xsplice_elf *elf) +{ +const struct xsplice_elf_sec *sec; +unsigned int i; +struct xsplice_patch_func_internal *f;Why is there a second ("internal") variant of this structure now? What guarantees it to remain in sync with the non-"internal" one?
The internal is gone now.
Quoted 4 lines
Quoted 2 lines
+ sec =
xsplice_elf_sec_by_name(elf, ".xsplice.funcs");Since the string literal occurs at least twice, I'd suggest having a #define for it.
OK, I also did it (in the later patches) for the .xsplice.depends and .gnu....
Quoted 12 lines
xen/common/xsplice.c static int secure_payload(struct payload *payload, struct xsplice_elf *elf) · line 352
Quoted 11 lines
xen/common/xsplice.c +10
-0+ASSERT (sec); +if ( sec->sec->sh_size %sizeof (*payload->funcs) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Wrong size of .xsplice.funcs!\n" , + elf->name); +return -EINVAL ; + } + + payload->funcs = sec->load_addr; + payload->nfuncs = sec->sec->sh_size /sizeof (*payload->funcs);
Quoted 3 lines
Following to our discussion yesterday - can't we (ab)use the section merge flag here to report the structure size, along the lines of what relocation sections do for their elements?
In other words - the xsplice-tools.git and the test-cases (in arch/x86/xen/test) would modify the sec->sh_entsize to have the sizeof(xsplice_patch_func) aka 64 (0x40) ?
[Note this could also extend to .xsplice.hooks.load and .xsplice.hooks.unload]
The xsplice-tools.git could surely do it, but the built-in test-cases - not so much. I would need to do some in-place binary handrolling - unless you know of some tool or some __section modifiers?
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 21 lines
Quoted 12 lines
xen/common/xsplice.c static int secure_payload(struct payload *payload, struct xsplice_elf *elf) · line 352
Quoted 11 lines · On 21.04.16 at 02:28, <[email protected]> wrote:
xen/common/xsplice.c +10
-0+ASSERT (sec); +if ( sec->sec->sh_size %sizeof (*payload->funcs) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Wrong size of .xsplice.funcs!\n" , + elf->name); +return -EINVAL ; + } + + payload->funcs = sec->load_addr; + payload->nfuncs = sec->sec->sh_size /sizeof (*payload->funcs);Quoted 3 lines
Following to our discussion yesterday - can't we (ab)use the section merge flag here to report the structure size, along the lines of what relocation sections do for their elements?
In other words - the xsplice-tools.git and the test-cases (in
arch/x86/xen/test) would modify thesec->sh_entsizeto have thesizeof(xsplice_patch_func)aka 64 (0x40) ?
Quoted 6 lines
[Note this could also extend to .xsplice.hooks.load and .xsplice.hooks.unload]
The xsplice-tools.git could surely do it, but the built-in test-cases - not so much. I would need to do some in-place binary handrolling - unless you know of some tool or some
__sectionmodifiers?
Well, that's why I suggest (ab) using the SHF_MERGE flag, i.e. to see whether we can make gas do what we want. Fiddling with the binary I would not recommend, i.e. if we can't get gas to do what we want we should rather stay with what there is.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 12 lines
Review
Quoted 11 lines · On 21.04.16 at 02:28, <[email protected]> wrote:
(quoted diff) +10
-0+ ASSERT(sec); + if ( sec->sec->sh_size % sizeof(*payload->funcs) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .xsplice.funcs!\n", + elf->name); + return -EINVAL; + } + + payload->funcs = sec->load_addr; + payload->nfuncs = sec->sec->sh_size / sizeof(*payload->funcs);Quoted 3 lines
Following to our discussion yesterday - can't we (ab)use the section merge flag here to report the structure size, along the lines of what relocation sections do for their elements?
In other words - the xsplice-tools.git and the test-cases (in
arch/x86/xen/test) would modify thesec->sh_entsizeto have thesizeof(xsplice_patch_func)aka 64 (0x40) ?[Note this could also extend to .xsplice.hooks.load and .xsplice.hooks.unload]
Quoted 8 lines
Quoted 3 lines
The xsplice-tools.git could surely do it, but the built-in test-cases - not so much. I would need to do some in-place binary handrolling - unless you know of some tool or some
__sectionmodifiers?Well, that's why I suggest (ab) using the
SHF_MERGEflag, i.e. to see whether we can make gas do what we want. Fiddling with the binary I would not recommend, i.e. if we can't get gas to do what we want we should rather stay with what there is.
This:
xen/arch/x86/test/xen_hello_world.c +7 -0
@@ -47,6 +47,13 @@ XSPLICE_UNLOAD_HOOK(hi_func);XSPLICE_UNLOAD_HOOK (Z_check_fnc); +#define XSPLICE_FUNC () \ + asm ( \ + ".pushsection .xsplice.funcs, \"awM\", @progbits, 64\n " \ + ".popsection") + +XSPLICE_FUNC (); + struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = { .version = XSPLICE_PAYLOAD_VERSION, .name = hello_world_patch_this_fnc,
Makes the .xsplice.funcs have sh_entsize have the size of 64 bytes:
[ 6] .xsplice.funcs PROGBITS 0000000000000000 000000a0
0000000000000040 0000000000000000 WA 0 0 32
vs
[ 6] .xsplice.funcs PROGBITS 0000000000000000 000000a0
0000000000000040 0000000000000040 WAM 0 0 32
Unfortunatly the compilation throws this:
FC-64 <konrad@build-external:~/xtt-x86_64/xen/xen> make -j4 tests 1>1
dnsdomainname: Name or service not known
{standard input}: Assembler messages:
{standard input}:184: Warning: ignoring changed section attributes for .xsplice.funcs
This is with:
binutils-2.20.51.0.2-15.fc13.x86_64
and with:
binutils-2.25-9.fc22.x86_64
Ideas?
I could try to wrap the definition of these
struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world using an macro - but that did not work very well (I couldn't manage it to take the value of say XSPLICE_PAYLOAD_VERSION).
Perhaps skip this?
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 77 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 22:27, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 12 lines
Review
Quoted 11 lines · On 21.04.16 at 02:28, <[email protected]> wrote:
(quoted diff) +10
-0+ ASSERT(sec); + if ( sec->sec->sh_size % sizeof(*payload->funcs) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .xsplice.funcs!\n", + elf->name); + return -EINVAL; + } + + payload->funcs = sec->load_addr; + payload->nfuncs = sec->sec->sh_size / sizeof(*payload->funcs);Quoted 3 lines
Following to our discussion yesterday - can't we (ab)use the section merge flag here to report the structure size, along the lines of what relocation sections do for their elements?
In other words - the xsplice-tools.git and the test-cases (in
arch/x86/xen/test) would modify thesec->sh_entsizeto have thesizeof(xsplice_patch_func)aka 64 (0x40) ?[Note this could also extend to .xsplice.hooks.load and .xsplice.hooks.unload]
Quoted 8 lines
Quoted 3 lines
The xsplice-tools.git could surely do it, but the built-in test-cases - not so much. I would need to do some in-place binary handrolling - unless you know of some tool or some
__sectionmodifiers?Well, that's why I suggest (ab) using the
SHF_MERGEflag, i.e. to see whether we can make gas do what we want. Fiddling with the binary I would not recommend, i.e. if we can't get gas to do what we want we should rather stay with what there is.This:
xen/arch/x86/test/xen_hello_world.c +7
-0@@ -47,6 +47,13 @@ XSPLICE_UNLOAD_HOOK(hi_func);XSPLICE_UNLOAD_HOOK (Z_check_fnc); +#define XSPLICE_FUNC () \ + asm ( \ + ".pushsection .xsplice.funcs, \"awM\", @progbits, 64\n " \ + ".popsection") + +XSPLICE_FUNC (); + struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = { .version = XSPLICE_PAYLOAD_VERSION,.name =
hello_world_patch_this_fnc,Makes the .xsplice.funcs have
sh_entsizehave the size of 64 bytes:[ 6] .xsplice.funcs PROGBITS 0000000000000000
000000a00000000000000040 0000000000000000 WA 0 0 32vs
[ 6] .xsplice.funcs PROGBITS 0000000000000000
000000a00000000000000040 0000000000000040 WAM 0 0 32Unfortunatly the compilation throws this:
FC-64 <konrad@build-external:~/xtt-x86_64/xen/xen> make -j4 tests 1>1
dnsdomainname: Name or service not known
{standard input}: Assembler messages:
{standard input}:184: Warning: ignoring changed section attributes for .xsplice.funcs
Quoted 6 lines
This is with:
binutils-2.20.51.0.2-15.fc13.x86_64
and with:
binutils-2.25-9.fc22.x86_64Ideas?
Well, this presumably being a redundant .section directive somewhere it would require seeing the full .c together with the intermediate .s in order to tell. But I guess to avoid you spending more time on this secondary aspect, let's just drop this for now and just put it on the todo list (as long as the thing is tech preview we can still alter requirements we place on the actual payloads).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 8 lines
Quoted 1 line
Ideas?
Well, this presumably being a redundant .section directive somewhere it would require seeing the full .c together with the intermediate .s in order to tell. But I guess to avoid you spending more time on this secondary aspect, let's just drop this for now and just put it on the todo list (as long as the thing is tech preview we can still alter requirements we place on the actual payloads).
Yes to "Tech Preview". I put it on http://wiki.xen.org/wiki/XSplice#Hypervisor
so I would not forget.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
In reply to Jan BeulichQuoted 8 lines
xen/common/xsplice.c struct payload { · line 61
Quoted 5 lines
xen/common/xsplice.c +5
-0+/* Defines an outstanding patching action. */ +struct xsplice_work +{ +atomic_t semaphore;/* Used for rendezvous. */ +atomic_t irq_semaphore;/* Used to signal all IRQs disabled. */ Why do you, btw, need two of them? I would seem to me that having just one for both purposes should be fine.
The 'semaphore' is used to get all of the CPUs synced up in the code. Once they have been synced up (so semaphore == total_cpus-1) we go to the next stage where all of them disable their IRQs -- and the master patches. Once the patching is done all of them renable their IRQs.
I am not exactly sure how I would reuse the 'semaphore' on the second part of this (disable IRQs).
Let me ponder this over the weekend with some crayons and paper..
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 12/27] xsplice: Implement support for applying/reverting/replacing patches.
Quoted 17 lines
Quoted 9 lines
xen/common/xsplice.c struct payload { · line 61
Quoted 6 lines · On 22.04.16 at 19:26, <[email protected]> wrote:
xen/common/xsplice.c +5
-0+/* Defines an outstanding patching action. */ +struct xsplice_work +{ +atomic_t semaphore;/* Used for rendezvous. */ +atomic_t irq_semaphore;/* Used to signal all IRQs disabled. */ Why do you, btw, need two of them? I would seem to me that having just one for both purposes should be fine.
The 'semaphore' is used to get all of the CPUs synced up in the code. Once they have been synced up (so semaphore == total_cpus-1) we go to the next stage
where all of them disable their IRQs -- and the master patches. Once the patching
is done all of them renable their IRQs.
Quoted 3 lines
I am not exactly sure how I would reuse the 'semaphore' on the second part of this
(disable IRQs).
Right now you have
+ timeout = xsplice_work.timeout + NOW();
+ if ( xsplice_spin(&xsplice_work.semaphore, timeout, cpus, "CPU") )
+ goto abort;
+
+ /* All CPUs are waiting, now signal to disable IRQs. */ + xsplice_work.ready = 1; + smp_wmb();
+
+ atomic_inc(&xsplice_work.irq_semaphore);
This atomic_inc() could simply become atomic_set(&xsplice_work.semaphore, 0), while at the same time getting moved up ahead of the setting of xsplice_work.ready (with suitable addition of barriers).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 13/27 x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'.
In reply to Konrad Rzeszutek WilkThis 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 extraxen_extra : Hello World
$xen-xsplice revert xen_hello_world
Performing revert: completed
$xen-xsplice unload xen_hello_world
Performing unload: completed
$xl info | grep extraxen_extra : -unstable
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
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.
---
--- .gitignore | 2 ++ docs/misc/xsplice.markdown | 37 +++++++++++++++++++++++++++ xen/Makefile | 8 ++++-- xen/arch/arm/Makefile | 2 ++ xen/arch/x86/Makefile | 4 +++ xen/arch/x86/test/Makefile | 44 ++++++++++++++++++++++++++++++++ xen/arch/x86/test/xen_hello_world.c | 30 ++++++++++++++++++++++ xen/arch/x86/test/xen_hello_world_func.c | 22 ++++++++++++++++ 8 files changed, 147 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
.gitignore +2 -0
@@ -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
docs/misc/xsplice.markdown +37 -0
@@ -336,6 +336,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: + +<pre> +/* 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 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 = (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"))); + +</pre> + +Code must be compiled with -fPIC. + ## Hypercalls We will employ the sub operations of the system management hypercall (sysctl).
xen/Makefile +6 -2
@@ -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))
xen/arch/arm/Makefile +2 -0
@@ -57,6 +57,8 @@ ifeq ($(CONFIG_ARM_64),y) ln -sf $(notdir $@) ../../$(notdir $@).efi endif +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
xen/arch/x86/Makefile +4 -0
@@ -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
xen/arch/x86/test/Makefile new file +44 -0
44 lines not on this page · raw message
xen/arch/x86/test/xen_hello_world.c new file +30 -0
@@ -0,0 +1,30 @@ +/* +* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. +* +*/ + +#include "config.h" +#include <xen/types.h> +#include <xen/xsplice.h> + +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 = (unsigned long)(xen_hello_world), + .old_addr = 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: +*/
xen/arch/x86/test/xen_hello_world_func.c new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. +* +*/ + +#include <xen/types.h> + +/* 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: +*/
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 13/27] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'.
xen/arch/arm/Makefile ifeq ($(CONFIG_ARM_64),y)
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
xen/arch/arm/Makefile +0
-0@@ -57,6 +57,8 @@ ifeq ($(CONFIG_ARM_64),y)
>ln -sf $(notdir $@) ../../$(notdir $@).efi
Quoted 3 lines
endif
+tests:
I think this should be phony just like ...
xen/arch/x86/Makefile $(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32
Quoted 3 lines
xen/arch/x86/Makefile +0
-0@@ -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/'
xen/arch/x86/Makefile $(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32 · line 81
Quoted 4 lines
xen/arch/x86/Makefile +3
-0+.PHONY: tests +tests: + $(MAKE) -f $(BASEDIR)/Rules.mk -C test xsplice
... this one.
Quoted 19 lines
xen/arch/x86/test/Makefile new file +16
-0@@ -0,0 +1,44 @@ +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) +
xen/arch/x86/test/Makefile line 26
Quoted 10 lines
xen/arch/x86/test/Makefile new file +10
-0+.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.
Stale comment afaict.
xen/arch/x86/test/Makefile line 42
Quoted 5 lines
xen/arch/x86/test/Makefile new file +5
-0+.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
Why do these two need doing in order? And why can't $(XSPLICE) not just depend on its to object files, without needing explict make invocation to generate them?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 13/27] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'.
Quoted 8 lines
xen/arch/x86/test/Makefile line 42
Quoted 5 lines
+.PHONY: xsplice
xen/arch/x86/test/Makefile new file +4
-0+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.oWhy do these two need doing in order? And why can't $(XSPLICE) not just depend on its to object files, without needing explict make invocation to generate them?
That is b/c the the xen_hello_world.c depends on the generation of config.h.
I _think_ I can do:
xsplice: config.h xen_hello_world_func.o
As the xen_hello_world_func.c should be able to be built without any dependencies.
Or maybe I can do:
xen_hello_world.o: config.h
$(CC) $(CFLAGS) -c $< -o $@ ^^^ - copy-n-paste from Rules.mk
xsplice: config.h xen_hello_world.o xen_hello_world_func.o
But I recall having issues with the $(CC).. I recall that I tried the same thing as what Rules.mk has for %.o build (as in copy-n-paste) but it had issues. But it may have been an earlier bug in my code that I tripped over.
Let me play with this a bit.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 13/27] x86/xen_hello_world.xsplice: Test payload for patching 'xen_extra_version'.
Quoted 16 lines
Quoted 9 lines
xen/arch/x86/test/Makefile line 42
Quoted 6 lines
Konrad Rzeszutek Wilk <[email protected]> 04/20/16 6:10 PM >>> +.PHONY: xsplice
xen/arch/x86/test/Makefile new file +4
-0+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.oWhy do these two need doing in order? And why can't $(XSPLICE) not just depend on its to object files, without needing explict make invocation to generate them?
That is b/c the the
xen_hello_world.cdepends on the generation ofconfig.h.I _think_ I can do:
xsplice:config.hxen_hello_world_func.o
Quoted 8 lines
As the
xen_hello_world_func.cshould be able to be built without any dependencies.Or maybe I can do:
xen_hello_world.o:
config.h
$(CC) $(CFLAGS) -c $< -o $@
^^^ - copy-n-paste from Rules.mk
But why would you want to duplicate the rule? All you need is representation of the dependency.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 14/27 xsplice, symbols: Implement symbol name resolution on address.
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
If in the payload we do not have the old_addr we can resolve the virtual address based on the UNDEFined symbols.
We also use an boolean flag: new_symbol to track symbols. The usual case this is used is by:
* A payload may introduce a new symbol
* A payload may override an existing symbol (introduced in Xen or another payload)
* Overriding symbols must exist in the symtab for backtraces.
* A payload must always link against the object which defines the new symbol.
Considering that payloads may be loaded in any order it would be incorrect to link against a payload which simply overrides a symbol because you could end up with a chain of jumps which is inefficient and may result in the expected function not being executed.
Also we include a local definition block in the symbols.c file.
---
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v1: Ross original version.
v2: Include test-case and document update.
v2: s/size_t/ssize_t/
Include core_text_size, core_text calculation
v4: Cast on dprintk to uint64_t to make ELF 32bit build.
v6: Rebase where the spinlock is no more recursive. Drop the spinlock
usage in xsplice_symbols_lookup_by_name
v7: Add Andrew's Reviewed-by
Initialize addr and symname to zero as xensyms_read uses it.
v8: Change one XENLOG_DEBUG to XENLOG_ERR.
Change printk to dprintk on symbols and one error case.
--- xen/arch/x86/Makefile | 16 +++- xen/arch/x86/test/Makefile | 4 +- xen/arch/x86/test/xen_hello_world.c | 5 +- xen/common/symbols.c | 34 ++++++++ xen/common/xsplice.c | 156 +++++++++++++++++++++++++++++++++++- xen/common/xsplice_elf.c | 19 ++++- xen/include/xen/symbols.h | 2 + xen/include/xen/xsplice.h | 8 ++ 8 files changed, 232 insertions(+), 12 deletions(-)
xen/arch/x86/Makefile +12 -4
@@ -72,6 +72,12 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ -O $(BASEDIR)/include/xen/compile.h ]; then \ echo '$(TARGET).efi'; fi) +ifdef CONFIG_XSPLICE +all_symbols = --all-symbols +else +all_symbols = +endif + $(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/'`
@@ -111,12 +117,14 @@ $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0 $(NM) -pa --format=sysv $(@D)/.$(@F).0 \ - | $(BASEDIR)/tools/symbols --sysv --sort >$(@D)/.$(@F).0.S + | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort \ + >$(@D)/.$(@F).0.S $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0.o $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ $(@D)/.$(@F).0.o -o $(@D)/.$(@F).1 $(NM) -pa --format=sysv $(@D)/.$(@F).1 \ - | $(BASEDIR)/tools/symbols --sysv --sort --warn-dup >$(@D)/.$(@F).1.S + | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort --warn-dup \ + >$(@D)/.$(@F).1.S $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1.o $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ $(@D)/.$(@F).1.o -o $@
@@ -140,14 +148,14 @@ $(TARGET).efi: prelink-efi.o efi.lds efi/relocs-dummy.o $(BASEDIR)/common/symbol $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).$(base).0 &&) : $(guard) efi/mkreloc $(foreach base,$(VIRT_BASE) $(ALT_BASE),$(@D)/.$(@F).$(base).0) >$(@D)/.$(@F).0r.S $(guard) $(NM) -pa --format=sysv $(@D)/.$(@F).$(VIRT_BASE).0 \ - | $(guard) $(BASEDIR)/tools/symbols --sysv --sort >$(@D)/.$(@F).0s.S + | $(guard) $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort >$(@D)/.$(@F).0s.S $(guard) $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0r.o $(@D)/.$(@F).0s.o $(foreach base, $(VIRT_BASE) $(ALT_BASE), \ $(guard) $(LD) $(call EFI_LDFLAGS,$(base)) -T efi.lds -N $< \ $(@D)/.$(@F).0r.o $(@D)/.$(@F).0s.o -o $(@D)/.$(@F).$(base).1 &&) : $(guard) efi/mkreloc $(foreach base,$(VIRT_BASE) $(ALT_BASE),$(@D)/.$(@F).$(base).1) >$(@D)/.$(@F).1r.S $(guard) $(NM) -pa --format=sysv $(@D)/.$(@F).$(VIRT_BASE).1 \ - | $(guard) $(BASEDIR)/tools/symbols --sysv --sort >$(@D)/.$(@F).1s.S + | $(guard) $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort >$(@D)/.$(@F).1s.S $(guard) $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o $(guard) $(LD) $(call EFI_LDFLAGS,$(VIRT_BASE)) -T efi.lds -N $< \ $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o -o $@
xen/arch/x86/test/Makefile +1 -3
@@ -26,14 +26,12 @@ clean:: # 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,$(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)") > $@ + echo "#define OLD_CODE_SZ $(OLD_CODE_SZ)") > $@ .PHONY: xsplice xsplice: config.h
xen/arch/x86/test/xen_hello_world.c +4 -1
@@ -10,11 +10,14 @@static char hello_world_patch_this_fnc[] ="xen_extra_version" ;extern const char *xen_hello_world (void ); +/* External symbol. */ +extern const char *xen_extra_version (void ); + struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = { .version = XSPLICE_PAYLOAD_VERSION, .name = hello_world_patch_this_fnc, .new_addr = (unsigned long)(xen_hello_world), - .old_addr = OLD_CODE, + .old_addr = (unsigned long)(xen_extra_version), .new_size = NEW_CODE_SZ, .old_size = OLD_CODE_SZ, };
xen/common/symbols.c +34 -0
@@ -207,3 +207,37 @@ int xensyms_read(uint32_t *symnum, char *type,return 0 ; } + +unsigned long symbols_lookup_by_name (const char *symname) +{ +char name[KSYM_NAME_LEN +1 ] = {0 }; +uint32_t symnum =0 ; +char type; +uint64_t addr =0 ; +int rc; + +if ( symname =='\0' ) +return 0 ; + +do { + rc =xensyms_read (&symnum, &type, &addr, name); +if ( rc ) +break ; + +if ( !strcmp (name, symname) ) +return addr; + + }while ( name[0 ] !='\0' ); + +return 0 ; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/common/xsplice.c +155 -1
204 lines not on this page · raw message
xen/common/xsplice_elf.c +16 -3
@@ -4,6 +4,7 @@#include <xen/errno.h> #include <xen/lib.h> +#include <xen/symbols.h> #include <xen/xsplice_elf.h> #include <xen/xsplice.h>
@@ -274,9 +275,21 @@ int xsplice_elf_resolve_symbols(struct xsplice_elf *elf)break ;case SHN_UNDEF : -dprintk (XENLOG_ERR ,XSPLICE "%s: Unknown symbol: %s\n" , - elf->name, elf->sym[i].name); - rc = -ENOENT ; + elf->sym[i].sym->st_value = (unsigned long )symbols_lookup_by_name (elf->sym[i].name); +if ( !elf->sym[i].sym->st_value ) + { + elf->sym[i].sym->st_value = (unsigned long ) +xsplice_symbols_lookup_by_name (elf->sym[i].name); +if ( !elf->sym[i].sym->st_value ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Unknown symbol: %s\n" , + elf->name, elf->sym[i].name); + rc = -ENOENT ; +break ; + } + } +dprintk (XENLOG_DEBUG ,XSPLICE "%s: Undefined symbol resolved: %s => %#" PRIxElfAddr"\n" , + elf->name, elf->sym[i].name, elf->sym[i].sym->st_value);break ;case SHN_ABS :
xen/include/xen/symbols.h +2 -0
@@ -23,4 +23,6 @@ const char *symbols_lookup(unsigned long addr,int xensyms_read (uint32_t *symnum,char *type,uint64_t *address,char *name); +unsigned long symbols_lookup_by_name (const char *symname); +#endif /*_XEN_SYMBOLS_H*/
xen/include/xen/xsplice.h +8 -0
@@ -46,8 +46,16 @@ struct xsplice_patch_func_internal {/* Convenience define for printk. */ #define XSPLICE "xsplice: " +struct xsplice_symbol { +const char *name; +uint64_t value; +size_t size; +bool_t new_symbol; +}; +int xsplice_op (struct xen_sysctl_xsplice_op *);void check_for_xsplice_work (void ); +unsigned long xsplice_symbols_lookup_by_name (const char *symname);/* Arch hooks. */ int arch_xsplice_verify_elf (const struct xsplice_elf *elf);
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
xen/arch/x86/test/xen_hello_world.c line 14
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/arch/x86/test/xen_hello_world.c +2
-0@@ -10,11 +10,14 @@static char hello_world_patch_this_fnc[] ="xen_extra_version" ;extern const char *xen_hello_world (void ); +/* External symbol. */ +extern const char *xen_extra_version (void );
To give a good example, I think this would better include the respective header.
xen/arch/x86/test/xen_hello_world.c line 16
Quoted 1 line
xen/arch/x86/test/xen_hello_world.c +0
-0struct xsplice_patch_func __section(".xsplice.funcs") xsplice_xen_hello_world = {
>.version = XSPLICE_PAYLOAD_VERSION, >.name = hello_world_patch_this_fnc, >.new_addr = (unsigned long)(xen_hello_world),
xen/arch/x86/test/xen_hello_world.c line 20
Quoted 2 lines
xen/arch/x86/test/xen_hello_world.c +1
-1- .old_addr = OLD_CODE, + .old_addr = (unsigned long)(xen_extra_version),
Pointless parentheses (also btw on the new_addr initializer, which would be nice to get fixed in the earlier patch).
xen/arch/x86/test/xen_hello_world.c line 19
Quoted 3 lines
xen/arch/x86/test/xen_hello_world.c +3
-0+unsigned long symbols_lookup_by_name (const char *symname) +{ +char name[KSYM_NAME_LEN +1 ] = {0 };
I continue to think that this initializer is pointless. And if this or a variant filling just the first byte was needed, then please be consistent with the rest of the function and either use '\0' here or plain 0 in the other two relevant places.
xen/arch/x86/test/xen_hello_world.c line 22
Quoted 3 lines
xen/arch/x86/test/xen_hello_world.c +3
-0+uint32_t symnum =0 ; +char type; +uint64_t addr =0 ;
Same here.
xen/arch/x86/test/xen_hello_world.c
Quoted 3 lines
xen/arch/x86/test/xen_hello_world.c +3
-0+int rc; + +if ( symname =='\0' )
DYM *symname?
xen/arch/x86/test/xen_hello_world.c struct payload {
Quoted 1 line
xen/arch/x86/test/xen_hello_world.c +0
-0@@ -51,6 +52,9 @@ struct payload {
>struct list_head applied_list; /* Linked to 'applied_list'. */
>struct xsplice_patch_func_internal *funcs; /* The array of functions to patch. */
>unsigned int nfuncs; /* Nr of functions to patch. */
xen/arch/x86/test/xen_hello_world.c struct payload { · line 53
Quoted 2 lines
xen/arch/x86/test/xen_hello_world.c +2
-0+struct xsplice_symbol *symtab;/* All symbols. */ +char *strtab;/* Pointer to .strtab. */
At least the latter of the two I'm convinced can be const.
xen/arch/x86/test/xen_hello_world.c struct payload { · line 56
Quoted 3 lines
xen/arch/x86/test/xen_hello_world.c +3
-0+unsigned long xsplice_symbols_lookup_by_name (const char *symname) +{ +struct payload *data;
const
xen/arch/x86/test/xen_hello_world.c struct payload {
Quoted 9 lines
xen/arch/x86/test/xen_hello_world.c +9
-0+ASSERT (spin_is_locked (&payload_lock)); + list_for_each_entry ( data, &payload_list, list ) + { +unsigned int i; + +for ( i =0 ; i < data->nsyms; i++ ) + { +if ( !data->symtab[i].new_symbol ) +continue ;
Do you need symbols other than those marked "new_symbol" past the loading of the module? If not, wouldn't it be worthwhile to shrink the symbol table to just those, likely speeding up the lookup?
xen/arch/x86/test/xen_hello_world.c static int prepare_payload(struct payload *payload,
Quoted 1 line
xen/arch/x86/test/xen_hello_world.c +0
-0@@ -379,11 +405,129 @@ static int prepare_payload(struct payload *payload,
>for ( j = 0; j < ARRAY_SIZE(f->u.pad); j++ )
>if ( f->u.pad[j] )
>return -EINVAL;xen/arch/x86/test/xen_hello_world.c static int prepare_payload(struct payload *payload, · line 412
Quoted 8 lines
xen/arch/x86/test/xen_hello_world.c +8
-0+ +/* Lookup function's old address if not already resolved. */ +if ( !f->old_addr ) + { + f->old_addr = (void *)symbols_lookup_by_name (f->name); +if ( !f->old_addr ) + { + f->old_addr = (void *)xsplice_symbols_lookup_by_name (f->name);
The two casts make me wonder whether the two functions shouldn't return void *, and then whether struct xsplice_symbol's value field shouldn't then perhaps be void * too.
xen/arch/x86/test/xen_hello_world.c static int prepare_payload(struct payload *payload, · line 422
Quoted 10 lines
xen/arch/x86/test/xen_hello_world.c +10
-0+if ( !f->old_addr ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not resolve old address of %s\n" , + elf->name, f->name); +return -ENOENT ; + } + } +dprintk (XENLOG_DEBUG ,XSPLICE "%s: Resolved old address %s => %p\n" , + elf->name, f->name, f->old_addr); + }
>}
Quoted 1 line
>return 0;
Quoted 1 line
}
So one thing I'm realizing only now: Is there no support for using <symbol>+<offset> to fill ->old_addr?
xen/arch/x86/test/xen_hello_world.c
Quoted 10 lines
xen/arch/x86/test/xen_hello_world.c +10
-0+static bool_t is_payload_symbol (const struct xsplice_elf *elf, +const struct xsplice_elf_sym *sym) +{ +if ( sym->sym->st_shndx ==SHN_UNDEF || + sym->sym->st_shndx >= elf->hdr->e_shnum ) +return 0 ; + +return (elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC ) && + (ELF64_ST_TYPE (sym->sym->st_info) ==STT_OBJECT || +ELF64_ST_TYPE (sym->sym->st_info) ==STT_FUNC );
I don't recall having seen a reply to the question on not allowing STT_NOTYPE here.
Quoted 30 lines
xen/arch/x86/test/xen_hello_world.c +30
-0+static int build_symbol_table (struct payload *payload, +const struct xsplice_elf *elf) +{ +unsigned int i, j, nsyms =0 ; +size_t strtab_len =0 ; +struct xsplice_symbol *symtab; +char *strtab; + +ASSERT (payload->nfuncs); + +/* Recall that section @0 is always NULL. */ +for ( i =1 ; i < elf->nsym; i++ ) + { +if (is_payload_symbol (elf, elf->sym + i) ) + { + nsyms++; + strtab_len +=strlen (elf->sym[i].name) +1 ; + } + } + + symtab = xmalloc_array(struct xsplice_symbol, nsyms); + strtab =xmalloc_array (char, strtab_len); + +if ( !strtab || !symtab ) + { +xfree (strtab); +xfree (symtab); +return -ENOMEM ; + } +
xen/arch/x86/test/xen_hello_world.c
Quoted 10 lines
xen/arch/x86/test/xen_hello_world.c +10
-0+ nsyms =0 ; + strtab_len =0 ; +for ( i =1 ; i < elf->nsym; i++ ) + { +if (is_payload_symbol (elf, elf->sym + i) ) + { + symtab[nsyms].name = strtab + strtab_len; + symtab[nsyms].size = elf->sym[i].sym->st_size; + symtab[nsyms].value = elf->sym[i].sym->st_value; + symtab[nsyms].new_symbol =0 ;/* To be checked below. */
Why "checked"? The only thing happening further down is this possibly getting overwritten with 1.
xen/arch/x86/test/xen_hello_world.c int xsplice_elf_resolve_symbols(struct xsplice_elf *elf)
Quoted 1 line
xen/arch/x86/test/xen_hello_world.c +0
-0@@ -274,9 +275,21 @@ int xsplice_elf_resolve_symbols(struct xsplice_elf *elf)
>break;
Quoted 1 line
>case SHN_UNDEF:
xen/arch/x86/test/xen_hello_world.c
Quoted 4 lines
xen/arch/x86/test/xen_hello_world.c +1
-3-dprintk (XENLOG_ERR ,XSPLICE "%s: Unknown symbol: %s\n" , - elf->name, elf->sym[i].name); - rc = -ENOENT ; + elf->sym[i].sym->st_value = (unsigned long )symbols_lookup_by_name (elf->sym[i].name);
With its current return value, the cast is pointless. But this makes clear that either here or above a cast is going to be needed. So perhaps just keep it as is, except for this unnecessary cast.
xen/arch/x86/test/xen_hello_world.c
Quoted 3 lines
xen/arch/x86/test/xen_hello_world.c +3
-0+if ( !elf->sym[i].sym->st_value ) + { + elf->sym[i].sym->st_value = (unsigned long )xsplice_symbols_lookup_by_name (elf->sym[i].name);
And this one then, obviously.
xen/include/xen/xsplice.h struct xsplice_patch_func_internal { · line 51
Quoted 9 lines
xen/include/xen/xsplice.h +3
-0@@ -46,8 +46,16 @@ struct xsplice_patch_func_internal {/* Convenience define for printk. */ #define XSPLICE "xsplice: " +struct xsplice_symbol { +const char *name; +uint64_t value;
Either void * (see above) or unsigned long. No point in being 64-bit for e.g. ARM32.
xen/include/xen/xsplice.h struct xsplice_patch_func_internal { · line 52
Quoted 1 line
xen/include/xen/xsplice.h +1
-0+size_t size;
I don't see this field being used for anything, it seems to only ever get written to.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
On 04/19/2016 08:31 PM, Jan Beulich wrote:
snip
Quoted 10 lines
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 133
Quoted 9 lines
xen/common/xsplice.c +9
-0+ASSERT (spin_is_locked (&payload_lock)); + list_for_each_entry ( data, &payload_list, list ) + { +unsigned int i; + +for ( i =0 ; i < data->nsyms; i++ ) + { +if ( !data->symtab[i].new_symbol ) +continue ;
Quoted 3 lines
Do you need symbols other than those marked "
new_symbol" past the loading of the module? If not, wouldn't it be worthwhile to shrink the symbol table to just those, likely speeding up the lookup?
new_symbol indicates whether it is newly introduced in this binary patch or whether it overrides a symbol in a previous binary patch (or the core binary). Either way, the symbol will be used when resolving a backtrace.
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
In reply to Jan BeulichQuoted 7 lines
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 123
Quoted 3 lines
xen/common/xsplice.c +3
-0+unsigned long xsplice_symbols_lookup_by_name (const char *symname) +{ +struct payload *data;Do you need symbols other than those marked "
new_symbol" past the loading of the module? If not, wouldn't it be worthwhile to shrink the symbol table to just those, likely speeding up the lookup?
Ross hopefully answered that to your satisfaction?
Quoted 14 lines
Review static int prepare_payload(struct payload *payload,
Quoted 2 lines
(quoted diff) +0
-0@@ -379,11 +405,129 @@ static int prepare_payload(struct payload *payload,>for ( j = 0; j < ARRAY_SIZE(f->u.pad); j++ ) >if ( f->u.pad[j] ) >return -EINVAL;xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 415
Quoted 8 lines
xen/common/xsplice.c +8
-0+ +/* Lookup function's old address if not already resolved. */ +if ( !f->old_addr ) + { + f->old_addr = (void *)symbols_lookup_by_name (f->name); +if ( !f->old_addr ) + { + f->old_addr = (void *)xsplice_symbols_lookup_by_name (f->name);
Quoted 3 lines
The two casts make me wonder whether the two functions shouldn't return void *, and then whether struct xsplice_symbol's value field shouldn't then perhaps be void * too.
So I did try that and it all worked nicely on x86. However on ARM32:
arm <konrad@localhost:~/xen/xen> make -j8 1>1symbols.c: In function 'symbols_lookup_by_name':
symbols.c:287:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
275 uint64_t addr = 0; /* MUST be initialized. */
286 if ( !strcmp(name, symname) )
287 return (void *)addr;
Which is rather unfortunate. I think I will have to make it unsigned long.
Quoted 11 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 425
Quoted 11 lines
xen/common/xsplice.c +10
-0+if ( !f->old_addr ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Could not resolve old address of %s\n" , + elf->name, f->name); +return -ENOENT ; + } + } +dprintk (XENLOG_DEBUG ,XSPLICE "%s: Resolved old address %s => %p\n" , + elf->name, f->name, f->old_addr); + }
Quoted 7 lines
>}Quoted 1 line
>return 0;Quoted 1 line
}
So one thing I'm realizing only now: Is there no support for using <symbol>+<offset> to fill ->old_addr?
No. Just <symbol>. I updated the design document to outline this missing functionality in the Todo section.
Quoted 13 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 440
Quoted 11 lines
xen/common/xsplice.c +10
-0+static bool_t is_payload_symbol (const struct xsplice_elf *elf, +const struct xsplice_elf_sym *sym) +{ +if ( sym->sym->st_shndx ==SHN_UNDEF || + sym->sym->st_shndx >= elf->hdr->e_shnum ) +return 0 ; + +return (elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC ) && + (ELF64_ST_TYPE (sym->sym->st_info) ==STT_OBJECT || +ELF64_ST_TYPE (sym->sym->st_info) ==STT_FUNC );I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.
Ross, could you elaborate a bit please on this?
Quoted 4 lines
Quoted 4 lines
xen/common/xsplice.c +3
-0+static int build_symbol_table (struct payload *payload, +const struct xsplice_elf *elf) +{
..snip..
Quoted 8 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 482
Quoted 5 lines
xen/common/xsplice.c +5
-0+ +/* Recall that section @0 is always NULL. */ +for ( i =1 ; i < elf->nsym; i++ ) + { + symtab[nsyms].new_symbol =0 ;/* To be checked below. */ Why "checked"? The only thing happening further down is this possibly getting overwritten with 1.
I changed it to say "May be overwritten below."
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 10 lines
Quoted 8 lines
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 123
Quoted 4 lines · On 21.04.16 at 02:26, <[email protected]> wrote:
xen/common/xsplice.c +3
-0+unsigned long xsplice_symbols_lookup_by_name (const char *symname) +{ +struct payload *data;Do you need symbols other than those marked "
new_symbol" past the loading of the module? If not, wouldn't it be worthwhile to shrink the symbol table to just those, likely speeding up the lookup?Ross hopefully answered that to your satisfaction?
Yes. Thinko of mine.
Quoted 27 lines
Quoted 13 lines
Review static int prepare_payload(struct payload *payload,
Quoted 1 line
(quoted diff) +0
-0@@ -379,11 +405,129 @@ static int prepare_payload(struct payload *payload,>for ( j = 0; j < ARRAY_SIZE(f->u.pad); j++ ) >if ( f->u.pad[j] ) >return -EINVAL;xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 415
Quoted 8 lines
xen/common/xsplice.c +8
-0+ +/* Lookup function's old address if not already resolved. */ +if ( !f->old_addr ) + { + f->old_addr = (void *)symbols_lookup_by_name (f->name); +if ( !f->old_addr ) + { + f->old_addr = (void *)xsplice_symbols_lookup_by_name (f->name);Quoted 3 lines
The two casts make me wonder whether the two functions shouldn't return void *, and then whether struct xsplice_symbol's value field shouldn't then perhaps be void * too.
So I did try that and it all worked nicely on x86. However on ARM32:
arm <konrad@localhost:~/xen/xen> make -j8 1>1
symbols.c: In function 'symbols_lookup_by_name':
symbols.c:287:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]275
uint64_taddr = 0; /* MUST be initialized. */
Quoted 6 lines
286 if ( !strcmp(name, symname) )
287 return (void *)addr;
Which is rather unfortunate. I think I will have to make it unsigned long.
Well, why would any kind of address be uint64_t on ARM32?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
A message that is not in the archive (sent off-list or not yet received): CACJDEmrucgYZpCDv3EAkDJUOtdcP9P2T=Vine1o2pzUmwJDY1w@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CACJDEmrzieYh6__MHJH_okoZPk+RA56PuQKv-oid0j1t1po1oQ@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CACJDEmrdi3sTZjGowkvGP67-_DH3+TLvArC8qZfArsyPb6fpuA@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CACJDEmrQ6onv-xqYOi3nekioCSASb4c1eZHJ-rzMxU3Wa7SXTQ@mail.gmail.com
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 10 lines
Quoted 10 lines
So I did try that and it all worked nicely on x86. However on ARM32:
arm <konrad@localhost:~/xen/xen> make -j8 1>1
symbols.c: In function 'symbols_lookup_by_name':
symbols.c:287:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]275
uint64_taddr = 0; /* MUST be initialized. */
Quoted 9 lines
Quoted 6 lines
286 if ( !strcmp(name, symname) )
287 return (void *)addr;
Which is rather unfortunate. I think I will have to make it unsigned long.
Well, why would any kind of address be
uint64_ton ARM32?
Oh, the xensyms_read - which is used in a hypercalls (for reading the symbols) as well - requires an pointer to an uint64_t as an argument. Hence the need for using that type.
I can certainly muck with it so that it can do xen_pfn_t.
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines · On 21.04.16 at 15:56, <[email protected]> wrote:
So I did try that and it all worked nicely on x86. However on ARM32:
arm <konrad@localhost:~/xen/xen> make -j8 1>1
symbols.c: In function 'symbols_lookup_by_name':
symbols.c:287:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]275
uint64_taddr = 0; /* MUST be initialized. */286 if ( !strcmp(name, symname) )
287 return (void *)addr;
Which is rather unfortunate. I think I will have to make it unsigned long.
Quoted 8 lines
Quoted 2 lines
Well, why would any kind of address be
uint64_ton ARM32?Oh, the
xensyms_read- which is used in a hypercalls (for reading the symbols) as well - requires an pointer to anuint64_tas an argument. Hence the need for using that type.I can certainly muck with it so that it can do
xen_pfn_t.
Which also is a 64-bit type on ARM, and which would be really wrong to use here. There's really no point in xensyms_read() taking other than an unsigned long *, and if the sysctl wants a uint64_t, then it should use a temporary variable to do the conversion.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
In reply to Konrad Rzeszutek WilkOn 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote:
snip
Quoted 14 lines
Quoted 13 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 440
Quoted 11 lines
xen/common/xsplice.c +10
-0+static bool_t is_payload_symbol (const struct xsplice_elf *elf, +const struct xsplice_elf_sym *sym) +{ +if ( sym->sym->st_shndx ==SHN_UNDEF || + sym->sym->st_shndx >= elf->hdr->e_shnum ) +return 0 ; + +return (elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC ) && + (ELF64_ST_TYPE (sym->sym->st_info) ==STT_OBJECT || +ELF64_ST_TYPE (sym->sym->st_info) ==STT_FUNC );I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.
Quoted 1 line
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1
10: 0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2
11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3
12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4
13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed STT_NOTYPE. Perhaps not the best solution but...
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote:
snipQuoted 13 lines
Quoted 13 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 440
Quoted 11 lines
xen/common/xsplice.c +10
-0+static bool_t is_payload_symbol (const struct xsplice_elf *elf, +const struct xsplice_elf_sym *sym) +{ +if ( sym->sym->st_shndx ==SHN_UNDEF || + sym->sym->st_shndx >= elf->hdr->e_shnum ) +return 0 ; + +return (elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC ) && + (ELF64_ST_TYPE (sym->sym->st_info) ==STT_OBJECT || +ELF64_ST_TYPE (sym->sym->st_info) ==STT_FUNC );I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10: 0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5
Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by xen/tools/symbols.c for the core symbol table? Quite obviously the symbol name generation should match between core and modules...
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...
Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABS X86_FEATURE_FFXSR'...
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
Quoted 11 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Rather than ignoring STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...
I can confirm the symbols getting generated in the .s file, ...
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
Quoted 11 lines
Quoted 10 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...
Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
Quoted 2 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".
That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 50 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 7 lines
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
/home/konrad/xen/xen/arch/x86
[konrad@build-external x86]$ readelf --symbols microcode.o | grep \.LC
50: 0000000000000000 0 NOTYPE LOCAL DEFAULT 12 .LC0
51: 00000000000000e8 0 NOTYPE LOCAL DEFAULT 13 .LC5
52: 0000000000000110 0 NOTYPE LOCAL DEFAULT 13 .LC6?
(GCC) 4.4.4 20100503 (Red Hat 4.4.4-2)
Quoted 19 lines
Quoted 12 lines
Quoted 11 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Quoted 8 lines
Why not? They could still serve as anchor for subsequent patches.
Quoted 2 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
And they seem to - see above on the .o file.
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 60 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:28, <[email protected]> wrote:
Quoted 50 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 7 lines
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
Quoted 8 lines
/home/konrad/xen/xen/arch/x86
[konrad@build-external x86]$ readelf --symbols microcode.o | grep \.LC50: 0000000000000000 0 NOTYPE LOCAL DEFAULT 12 .LC0 51: 00000000000000e8 0 NOTYPE LOCAL DEFAULT 13 .LC5 52: 0000000000000110 0 NOTYPE LOCAL DEFAULT 13 .LC6?
(GCC) 4.4.4 20100503 (Red Hat 4.4.4-2)
Ah, this happens only with optimization, and I had tried only without. Looks to indeed be connected to these strings getting put into mergeable string sections, instead of just plain .rodata.
Quoted 21 lines
Quoted 21 lines
Quoted 11 lines
Quoted 10 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
And they seem to - see above on the .o file.
Above .o file was a core one, and doesn't tell anyway whether in the runtime symbol table the local symbols would be properly prefixed by file name. Or did I misunderstand you?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 97 lines · Jan Beulich
Quoted 60 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:28, <[email protected]> wrote:
Quoted 50 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 7 lines
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
Quoted 8 lines
/home/konrad/xen/xen/arch/x86
[konrad@build-external x86]$ readelf --symbols microcode.o | grep \.LC50: 0000000000000000 0 NOTYPE LOCAL DEFAULT 12 .LC0 51: 00000000000000e8 0 NOTYPE LOCAL DEFAULT 13 .LC5 52: 0000000000000110 0 NOTYPE LOCAL DEFAULT 13 .LC6?
(GCC) 4.4.4 20100503 (Red Hat 4.4.4-2)Ah, this happens only with optimization, and I had tried only without. Looks to indeed be connected to these strings getting put into mergeable string sections, instead of just plain .rodata.
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 10 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
Quoted 2 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
And they seem to - see above on the .o file.
Above .o file was a core one, and doesn't tell anyway whether in the runtime symbol table the local symbols would be properly prefixed by file name. Or did I misunderstand you?
I had 'built_in.o' or 'prelink.o' as the final one in mind - in which the .LCx are present. But I think you are thinking about 'xen-syms' output.
Looking at the Makefile runes we hit this:
127 $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o
128 $(LD) $(LDFLAGS) -T xen.lds -N prelink.o $(build_id_linker) \
129 $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
And that ends up causing the .xen-syms.0 file to drop all the .LC0.
However we can't do that. We _have_ to produce an relocatable object and not do the final linking (so we append -r to the linker invocation).
If we do not:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400144 xen_hello_world.o:(.xsplice.funcs+0x10): undefined reference to `xen_extra_version'
ld: xen_hello_world.xsplice: hidden symbol `xen_extra_version' isn't defined make[3]: *** [xen_hello_world.xsplice] Error 1
I tried for fun to use strip:
xen/xen/arch/x86/test> strip --strip-symbol=".LC0" xen_replace_world.xsplice
strip: not stripping symbol `.LC0' because it is named in a relocation
But it has some hard rules about it.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 13:08, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:28, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
And they seem to - see above on the .o file.
Above .o file was a core one, and doesn't tell anyway whether in the runtime symbol table the local symbols would be properly prefixed by file name. Or did I misunderstand you?
I had 'built_in.o' or 'prelink.o' as the final one in mind - in which the .LCx are present. But I think you are thinking about 'xen-syms' output.
Quoted 6 lines
Looking at the
Makefilerunes we hit this:
127 $(TARGET)-syms: prelink.oxen.lds$(BASEDIR)/common/symbols-dummy.o
128 $(LD) $(LDFLAGS) -Txen.lds-N prelink.o $(build_id_linker) \
129 $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0And that ends up causing the .xen-syms.0 file to drop all the .LC0.
That's odd: I don't know of us telling the linker to do so, and imo it shouldn't do this by default. But yes, linkers often do strange things...
Quoted 2 lines
However we can't do that. We _have_ to produce an relocatable object and not do the final linking (so we append -r to the linker invocation).
Of course.
Quoted 3 lines
I tried for fun to use strip:
xen/xen/arch/x86/test> strip --strip-symbol=".LC0" xen_replace_world.xsplice
strip: not stripping symbol `.LC0' because it is named in a relocation
Which it is rightfully saying.
So with the linker stripping .LC* (and why knows what else), you stripping them when building the runtime symbol table would be fine with me, provided we can somehow identify in the linker why this stripping happens, and which precise set of symbols get stripped (which you should then match in our code).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 40 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 13:08, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:28, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
And they seem to - see above on the .o file.
Above .o file was a core one, and doesn't tell anyway whether in the runtime symbol table the local symbols would be properly prefixed by file name. Or did I misunderstand you?
I had 'built_in.o' or 'prelink.o' as the final one in mind - in which the .LCx are present. But I think you are thinking about 'xen-syms' output.
Quoted 6 lines
Looking at the
Makefilerunes we hit this:
127 $(TARGET)-syms: prelink.oxen.lds$(BASEDIR)/common/symbols-dummy.o
128 $(LD) $(LDFLAGS) -Txen.lds-N prelink.o $(build_id_linker) \
129 $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0And that ends up causing the .xen-syms.0 file to drop all the .LC0.
That's odd: I don't know of us telling the linker to do so, and imo it shouldn't do this by default. But yes, linkers often do strange things...
Quoted 2 lines
However we can't do that. We _have_ to produce an relocatable object and not do the final linking (so we append -r to the linker invocation).
Of course.
Quoted 3 lines
I tried for fun to use strip:
xen/xen/arch/x86/test> strip --strip-symbol=".LC0" xen_replace_world.xsplice
strip: not stripping symbol `.LC0' because it is named in a relocation
Quoted 7 lines
Which it is rightfully saying.
So with the linker stripping .LC* (and why knows what else), you stripping them when building the runtime symbol table would be fine with me, provided we can somehow identify in the linker why this stripping happens, and which precise set of symbols get stripped (which you should then match in our code).
I spoke over the weekend with Dan Jacobowitz (who in the past worked on binutils) who mentioned that ELF visibility is what one should pay attention to. And that we shouldn't need to look at LOCAL symbols at all if we are resolving between objects.
Also he mentions that the handling of .L* symbols is special since they're intermediate artifacts of assembly. They're ... "even more local".
Digging in the binutils over the weekend I found:
/* Return whether a symbol name implies a local symbol. Most targets
use this function for the is_local_label_name entry point, but some override it. */
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
const char *name)
{
/* Normal local symbols start with ``.L''. */ if (name[0] == '.' && name[1] == 'L')
return TRUE;
..
The function prototype is suppose to adhere to:
DESCRIPTION
Return TRUE if a symbol with the name @var{name} in the BFD @var{abfd} is a compiler generated local label,
Looking at the callchain I saw:
/* Link an input file into the linker output file. This function
handles all the sections and relocations of the input file at once. This is so that we only have to read the local symbols once, and don't have to keep them in memory. */
static bfd_booleanelf_link_input_bfd (..
And inside there is a big loop in which:
/* See if we are discarding symbols with this name. */
if ((flinfo->info->strip == strip_some
&& (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
== NULL))
|| (((flinfo->info->discard == discard_sec_merge
&& (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
|| flinfo->info->discard == discard_l)
&& bfd_is_local_label_name (input_bfd, name)))
continue;Which really matches with what I see - that is the .LCx symbols are in mergable sections:
[konrad@x230 x86]$ readelf --symbols prelink.o |grep LC | awk '{print $7}' | sort | uniq 22
23
33
34
[22] .rodata.str1.1 PROGBITS 0000000000000000 0019d500
000000000000a088 0000000000000001 AMS 0 0 1
[23] .rodata.str1.8 PROGBITS 0000000000000000 001a7588
0000000000020a31 0000000000000001 AMS 0 0 8 [33] .init.rodata.str1 PROGBITS 0000000000000000 001d5a78
0000000000000a1e 0000000000000001 AMS 0 0 1
[34] .init.rodata.str1 PROGBITS 0000000000000000 001d6498
0000000000002331 0000000000000001 AMS 0 0 8 [22] .rodata.str1.1 PROGBITS 0000000000000000 0019d500
000000000000a088 0000000000000001 AMS 0 0 1
[23] .rodata.str1.8 PROGBITS 0000000000000000 001a7588
0000000000020a31 0000000000000001 AMS 0 0 8 [33] .init.rodata.str1 PROGBITS 0000000000000000 001d5a78
0000000000000a1e 0000000000000001 AMS 0 0 1
[34] .init.rodata.str1 PROGBITS 0000000000000000 001d6498
0000000000002331 0000000000000001 AMS 0 0 8And when we do the final linking:
ld -melf_x86_64 -T xen.lds -N prelink.o --build-id=sha1 \
/tmp/xen/xen/common/symbols-dummy.o -o /tmp/xen/xen/.xen-syms.0 The xen.lds.S linker has:
.rodata : {
..
*(.rodata.*)
} :textwhich collapses these.
(For fun I wrote a seperate entry:
.rodata.str : {
__lc_start = .;
*(.rodata.str1.8)
__lc_end = .;
} :textand sure enough - that section (.rodata.str) is there and looks. There are no symbols for it - except the __lc_start and __lc_end.
So coming back to this discussion - the code has this:
static bool_t is_payload_symbol(const struct xsplice_elf *elf,
const struct xsplice_elf_sym *sym)
{
if ( sym->sym->st_shndx == SHN_UNDEF ||
sym->sym->st_shndx >= elf->hdr->e_shnum )
return 0;
return (elf->sec[sym->sym->st_shndx].sec->sh_flags & SHF_ALLOC) &&
(ELF64_ST_TYPE(sym->sym->st_info) == STT_OBJECT ||
ELF64_ST_TYPE(sym->sym->st_info) == STT_FUNC);
}You OK with it being the way it is? I would naturally include a comment about these local symbols like .L*
?
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 24.04.16 at 23:41, <[email protected]> wrote:
I spoke over the weekend with Dan Jacobowitz (who in the past worked on binutils) who mentioned that ELF visibility is what one should pay attention to. And that we shouldn't need to look at LOCAL symbols at all if we are resolving between objects.
Well, in general that's correct, but xSplice intentionally hooks up to all the symbols available (including local ones in the core image).
Quoted 10 lines
Also he mentions that the handling of .L* symbols is special since they're intermediate artifacts of assembly. They're ... "even more local".
Digging in the binutils over the weekend I found:
/* Return whether a symbol name implies a local symbol. Most targets
use this function for theis_local_label_nameentry point, but some override it. */
Quoted 7 lines
_bfd_elf_is_local_label_name(bfd *abfdATTRIBUTE_UNUSED,
const char *name)
{
/* Normal local symbols start with ``.L''. */ if (name[0] == '.' && name[1] == 'L')return TRUE;..
Yes, that's then what we want to match in behavior (as also suggested by Ross in his reply).
Quoted 39 lines
Looking at the callchain I saw:
/* Link an input file into the linker output file. This function
handles all the sections and relocations of the input file at once. This is so that we only have to read the local symbols once, and don't have to keep them in memory. */static
bfd_booleanelf_link_input_bfd(..And inside there is a big loop in which:
/* See if we are discarding symbols with this name. */ if ((flinfo->info->strip == strip_some && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) == NULL)) || (((flinfo->info->discard == discard_sec_merge && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable) || flinfo->info->discard == discard_l) && bfd_is_local_label_name (input_bfd, name))) continue;Which really matches with what I see - that is the .LCx symbols are in mergable sections:
[...]
So coming back to this discussion - the code has this:static
bool_tis_payload_symbol(const structxsplice_elf*elf,
const structxsplice_elf_sym*sym){ if ( sym->sym->st_shndx == SHN_UNDEF || sym->sym->st_shndx >= elf->hdr->e_shnum ) return 0; return (elf->sec[sym->sym->st_shndx].sec->sh_flags & SHF_ALLOC) && (ELF64_ST_TYPE(sym->sym->st_info) == STT_OBJECT || ELF64_ST_TYPE(sym->sym->st_info) == STT_FUNC); }
Quoted 2 lines
You OK with it being the way it is? I would naturally include a comment about these local symbols like .L*
But that's what is there already, or am I overlooking some change you did to it? I.e. STT_NOTYPE are still not being considered, and there's still no dependency on the .L name prefix.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
In reply to Jan BeulichQuoted 50 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 7 lines
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
I've looked into this a little further. The only .LC* symbols left in the .o file are the ones which are used in bug_frame relas. These symbols do not make it into the core symbol table because the relas are dropped when the xen binary is linked just before tools/symbols is run. Obviously we can't drop the rela sections for xsplice because it needs to be relocatable.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...
Quoted 8 lines
Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
They're not useful because they're autogenerated and the numbering may change from build to build. So two patch modules may have conflicting symbols just because they happen to use the same .LCx symbol.
Quoted 7 lines
Quoted 3 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
Yes. And, as best I can tell, .L symbols are not in the core table so this would then make it consistent for modules.
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 56 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 13:13, <[email protected]> wrote:
Quoted 54 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
Quoted 6 lines
Quoted 3 lines
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
I've looked into this a little further. The only .LC* symbols left in the .o file are the ones which are used in
bug_framerelas.
Not according to what I see - there are certainly also .text relocations referencing .LC*.
Quoted 5 lines
These
symbols do not make it into the core symbol table because the relas are dropped when the xen binary is linked just beforetools/symbolsis run. Obviously we can't drop the rela sections for xsplice because it needs to be relocatable.
As per my earlier reply to Konrad, there must be more to this. I.e. "normal" local symbols won't get dropped together with relocations referencing them getting resolved.
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
They're not useful because they're autogenerated and the numbering may change from build to build.
That's not of interest.
Quoted 2 lines
So two patch modules may have conflicting
symbols just because they happen to use the same .LCx symbol.
Not if they're, as mentioned before, get properly prefixed with the respective file name.
But as said in reply to Konrad, I'm fine with them getting dropped as long as our rule matches ld's (which btw may be arch dependent).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 3 lines
As per my earlier reply to Konrad, there must be more to this. I.e. "normal" local symbols won't get dropped together with relocations referencing them getting resolved.
Correct. These .LCx symbols only cover .rodata.* sections. Any other local symbols:
[konrad@x230 x86]$ readelf --symbols prelink.o |grep bad_hypercall
8946: 0000000000145549 0 NOTYPE LOCAL DEFAULT 1 compat_bad_hypercall 8967: 0000000000145c79 0 NOTYPE LOCAL DEFAULT 1 bad_hypercall
don't get dropped and do show up in the binary (nm --defined finds them).
While .LC matches the type:
9147: 00000000000008c0 0 NOTYPE LOCAL DEFAULT 33 .LC5
They are in four other sections:
konrad@x230 x86]$ readelf --symbols prelink.o |grep LC | awk '{print $7}' | sort | uniq
22
23
33
34
[22] .rodata.str1.1 PROGBITS 0000000000000000 0019d500
000000000000a088 0000000000000001 AMS 0 0 1
[23] .rodata.str1.8 PROGBITS 0000000000000000 001a7588
0000000000020a31 0000000000000001 AMS 0 0 8
[33] .init.rodata.str1 PROGBITS 0000000000000000 001d5a78
0000000000000a1e 0000000000000001 AMS 0 0 1
[34] .init.rodata.str1 PROGBITS 0000000000000000 001d6498
0000000000002331 0000000000000001 AMS 0 0 8_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 24 lines · Konrad Rzeszutek Wilk
Quoted 3 lines
As per my earlier reply to Konrad, there must be more to this. I.e. "normal" local symbols won't get dropped together with relocations referencing them getting resolved.
Correct. These .LCx symbols only cover .rodata.* sections. Any other local symbols:
[konrad@x230 x86]$ readelf --symbols prelink.o |grep
bad_hypercall8946: 0000000000145549 0 NOTYPE LOCAL DEFAULT 1 compat_bad_hypercall 8967: 0000000000145c79 0 NOTYPE LOCAL DEFAULT 1 bad_hypercalldon't get dropped and do show up in the binary (nm --defined finds them).
While .LC matches the type:
9147:00000000000008c00 NOTYPE LOCAL DEFAULT 33 .LC5They are in four other sections:
konrad@x230 x86]$ readelf --symbols prelink.o |grep LC | awk '{print $7}' | sort | uniq
22
23
33
34
Quoted 9 lines
[22] .rodata.str1.1 PROGBITS 0000000000000000
0019d500000000000000a088 0000000000000001 AMS 0 0 1 [23] .rodata.str1.8 PROGBITS 0000000000000000 001a7588 0000000000020a31 0000000000000001 AMS 0 0 8 [33] .init.rodata.str1 PROGBITS 0000000000000000 001d5a78 0000000000000a1e 0000000000000001 AMS 0 0 1 [34] .init.rodata.str1 PROGBITS 0000000000000000 001d6498 0000000000002331 0000000000000001 AMS 0 0 8
With some helpful investigation by Konrad, I've found this snippet in binutils:
/* See if we are discarding symbols with this name. */ if ((flinfo->info->strip == strip_some
&& (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
== NULL))
|| (((flinfo->info->discard == discard_sec_merge
&& (isec->flags & SEC_MERGE)
&& !bfd_link_relocatable (flinfo->info))
|| flinfo->info->discard == discard_l)
&& bfd_is_local_label_name (input_bfd, name)))
continue;The default value for info->discard is discard_sec_merge, so:
Local labels referring to a mergeable section are discarded when the output is not relocatable. A local label is defined as:
1) Starts with .X (for i386 unixware compilers)
2) Starts with .L
3) Starts with .. (for SRV4 compilers)
4) Starts with _.L_ (for older buggy GCC)
5) Matches L0^A.*
6) Matches L[0-9]+{^A|^B}[0-9]* (for assembler generated local labels)
So to match what is used by the default hypervisor build, I think we should change the check to discard when the symbol matches (2), (5), or (6) above and refers to a mergeable section. The above rules are defined in _bfd_elf_is_local_label_name and elf_i386_is_local_label_name.
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
Quoted 62 lines
Quoted 1 line
Quoted 1 line · On 25.04.16 at 08:41, <[email protected]> wrote:
Quoted 9 lines · Konrad Rzeszutek Wilk
Quoted 3 lines
As per my earlier reply to Konrad, there must be more to this. I.e. "normal" local symbols won't get dropped together with relocations referencing them getting resolved.
Correct. These .LCx symbols only cover .rodata.* sections. Any other local symbols:
[konrad@x230 x86]$ readelf --symbols prelink.o |grep
bad_hypercall
8946: 0000000000145549 0 NOTYPE LOCAL DEFAULT 1
compat_bad_hypercallQuoted 15 lines
8967:
0000000000145c790 NOTYPE LOCAL DEFAULT 1bad_hypercalldon't get dropped and do show up in the binary (nm --defined finds them).
While .LC matches the type:
9147:00000000000008c00 NOTYPE LOCAL DEFAULT 33 .LC5They are in four other sections:
konrad@x230 x86]$ readelf --symbols prelink.o |grep LC | awk '{print $7}' | sort | uniq
22
23
33
34Quoted 9 lines
[22] .rodata.str1.1 PROGBITS 0000000000000000
0019d500000000000000a088 0000000000000001 AMS 0 0 1 [23] .rodata.str1.8 PROGBITS 0000000000000000 001a7588 0000000000020a31 0000000000000001 AMS 0 0 8 [33] .init.rodata.str1 PROGBITS 0000000000000000 001d5a78 0000000000000a1e 0000000000000001 AMS 0 0 1 [34] .init.rodata.str1 PROGBITS 0000000000000000 001d6498 0000000000002331 0000000000000001 AMS 0 0 8With some helpful investigation by Konrad, I've found this snippet in binutils:
/* See if we are discarding symbols with this name. */ if ((flinfo->info->strip == strip_some && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) == NULL)) || (((flinfo->info->discard == discard_sec_merge && (isec->flags & SEC_MERGE) && !bfd_link_relocatable (flinfo->info)) || flinfo->info->discard == discard_l) && bfd_is_local_label_name (input_bfd, name))) continue;The default value for
info->discardisdiscard_sec_merge, so:
Local labels referring to a mergeable section are discarded when the output is not relocatable.
A local label is defined as:
1) Starts with .X (for i386 unixware compilers)
2) Starts with .L
3) Starts with .. (for SRV4 compilers)
4) Starts with _.L_ (for older buggy GCC)
5) Matches L0^A.*
6) Matches L[0-9]+{^A|^B}[0-9]* (for assembler generated local labels)
Quoted 4 lines
So to match what is used by the default hypervisor build, I think we should change the check to discard when the symbol matches (2), (5), or
(6) above and refers to a mergeable section. The above rules are defined in_bfd_elf_is_local_label_nameandelf_i386_is_local_label_name.
Yes. (Note that we don't really care about elf_i386_is_local_label_name(), as we don't have a 32-bit hypervisor anymore.)
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 14/27] xsplice, symbols: Implement symbol name resolution on address.
In reply to Ross LagerwallQuoted 59 lines · Ross Lagerwall
Quoted 50 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 10:45, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 09:17, <[email protected]> wrote:
On 04/21/2016 01:26 AM, Konrad Rzeszutek Wilk wrote: snip
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
+static
bool_tis_payload_symbol(const structxsplice_elf
*elf, + const structxsplice_elf_sym*sym) +{ + if (sym->sym->st_shndx==SHN_UNDEF|| +sym->sym->st_shndx>=elf->hdr->e_shnum) + return 0; + + return
(elf->sec[sym->sym->st_shndx].sec->sh_flags &SHF_ALLOC) && + (ELF64_ST_TYPE(sym->sym->st_info)==STT_OBJECT|| +ELF64_ST_TYPE(sym->sym->st_info)==STT_FUNC);Quoted 2 lines
Quoted 2 lines
I don't recall having seen a reply to the question on not allowing
STT_NOTYPEhere.Quoted 2 lines
Ross, could you elaborate a bit please on this?
The payload will typically have many entries like:
9: 0000000000000000 0 NOTYPE LOCAL DEFAULT 5 .LC1 10:
0000000000000006 0 NOTYPE LOCAL DEFAULT 5 .LC2 11: 000000000000000d 0 NOTYPE LOCAL DEFAULT 5 .LC3 12: 0000000000000028 0 NOTYPE LOCAL DEFAULT 4 .LC4 13: 0000000000000058 0 NOTYPE LOCAL DEFAULT 4 .LC5Quoted 5 lines
used when referencing strings (due to the use of -fPIC). While it is not a problem for them to go into the symbol table, if more than one payload is loaded, there will be duplicate conflicting symbols. So, to prevent these symbols from going into the symbol table, I disallowed
STT_NOTYPE. Perhaps not the best solution but...Quoted 6 lines
First of all symbols starting with .L aren't meant to and up in the symbol table at all (i.e. even that of any intermediate .o file). So there's likely (but not necessarily) something wrong with the tool chain used (i.e. normally such symbols wouldn't be needed for e.g. relocations, as those should get converted to section relative ones).
Quoted 5 lines
This is not particular to the xsplice build process. Any version of GCC+binutils that I've tested with will generate .LC
symbols for strings into the .o file. Clang generates similar .L.str* symbols, in addition to other useless ones like 'NOTYPE LOCAL DEFAULT ABSX86_FEATURE_FFXSR'...I can confirm the symbols getting generated in the .s file, ...
Quoted 7 lines
Quoted 3 lines
Maybe it uses these .LC symbols rather than section relative ones because they point to a mergeable string section, and merging string sections would be harder with section relative references?
... but I can't confirm them making it into the .o file, not to speak of being used for relocations. I've tried gcc 4.3.4 as well as 5.3.0 (with and without -fPIC).
Quoted 5 lines
I've looked into this a little further. The only .LC* symbols left in the .o file are the ones which are used in
bug_framerelas. These symbols do not make it into the core symbol table because the relas are dropped when the xen binary is linked just beforetools/symbolsis run. Obviously we can't drop the rela sections for xsplice because it needs to be relocatable.
In my case I seem to have one of the test-cases have one the .text section.
That is:
File: arch/x86/test/xen_bye_world.xsplice
Relocation section '.rela.text' at offset 0x11a8 contains 1 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000007 001300000002 R_X86_64_PC32 0000000000000000 .LC0 - 4
Which .. is related to the string:
Disassembly of section .text:
0000000000000000 <xen_bye_world>:
0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # b <xen_bye_world+0xb> b: c9 leaveq c: c3 retq
Which we obviously do need.
Quoted 30 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Yet _if_ such symbols make it into the symbol table of a .o, then there is no reason for them to not also make it into the runtime symbol table. Of course similar ones from different modules then shouldn't conflict with one another, and as these are local symbols perhaps the reason for them conflicting is that in the process of creating the runtime symbol table entries you neglect to prefix them with their source or object file names, as is done by
xen/tools/symbols.cfor the core symbol table? Quite obviously the symbol name generation should match between core and modules...Quoted 8 lines
Quoted 6 lines
The build tool does prefix the required functions and objects with their source/object file names. The problem is that these are generated symbols, so even if you had e.g. keyhandler.c#.LC0, keyhandler.c#.LC1, in the symbol table, they might be completed unrelated if you change the source even slightly. Having these entries in the symbol table would not make any sense.
Why not? They could still serve as anchor for subsequent patches.
They're not useful because they're autogenerated and the numbering may change from build to build. So two patch modules may have conflicting symbols just because they happen to use the same .LCx symbol.
Quoted 4 lines
Quoted 3 lines
Rather than ignoring
STT_NOTYPE, an alternative would be to ignore symbols starting with ".L".
Quoted 9 lines
Quoted 3 lines
That's an option, but as said before, the rules for which symbols to enter into the symbol table should be consistent for core and modules.
Yes. And, as best I can tell, .L symbols are not in the core table so this would then make it consistent for modules.
Signature · 1 line
Ross Lagerwall
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 15/27 xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
In reply to Konrad Rzeszutek WilkThe current mechanism is geared towards fast virtual address -> symbol names lookup. This is fine for the normal use cases (BUG_ON, WARN_ON, etc), but for xSplice - where we need to find hypervisor symbols - it is slow.
To understand this patch, a description of the existing method is explained first. For folks familar go to 'NEW CODE:'.
HOW IT WORKS:
The symbol table lookup mechanism uses a simple encoding mechanism where it extracts the common ascii characters that the symbol's use.
This saves us space. The lookup mechanism is geared towards looking up symbols based on address. We have one 0..N (where N is the number of symbols, so 6849 for example) table:
symbols_addresses[0..N]
And an 1-1 (in a loose fashion) of the symbols (encoded) in a symbols_names stream of size N.
The N is variable (later on that below)
The symbols_names are sorted based on symbols_addresses, which means that the decoded entries inside symbols_names are not in ascending or descending order.
There is also the encoding mechanism - the table of 255 entries called symbols_token_index[]. And the symbols_token_table which is an stream of ASCIIZ characters, such as (it really is not a table as the values are variable):
@0 .asciz "credit" @6 .asciz "mask"
..
@300 .asciz "S"
And the symbols_token_index:
@0 .short 0 @1 .short 7 @2 .short 12 @4 .short 16
...
@84 .short 300
The relationship between them is that the symbols_token_index gives us the offset to symbols_token_table.
The symbol_names[] array is a stream of encoded values. Each value follows the same pattern - <len> followed by <encoding values>. And the another <len> followed by <encoding values>.
Hence to find the right one you need to read <len>, add <len> (to skip over), read <len>, add <len>, and so on until one finds the right tuple offset.
The <encoding values> are the indicies into the symbols_token_index.
Meaning if you have:
0x04, 0x54, 0xda, 0xe2, 0x74
[4, 84, 218, 226, 116 in human numbering]
The 0x04 tells us that the symbol is four bytes past this one (so next symbol offset starts at 5). If we lookup symbols_token_index[84] we get 300. symbols_token[300] gets us the "S". And so on, the string eventually end up being decode to be 'S_stext'. The first character is the type, then optionally follwed by the filename (and # right after filename) and then lastly the symbol, such as:
tvpmu_intel.c#core2_vpmu_do_interrupt
Keep in mind that there are two fixed sized tables:
symbols_addresses[0..symbols_num_syms], and symbols_markers[0..symbols_num_syms/255].
The symbols_markers is used to speed searching for the right address. It gives us the offsets within symbol_names that start at the <len><encoded value>.
The way to find a symbol based on the address is:
1) Figure out the 'tuple offset' from symbols_address[0..symbols_num_syms]. This table is sorted by virtual addresses so finding the value is simple.
2) Get starting offset of symbol_names by retrieving value of symbol_markers['tuple offset' / 255].
3). Iterate up to 'tuple_offset & 255' in symbols_markers stream starting
at 'offset'.
4). Decode the <len><encoded value>
This however does not work very well if we want to search the other way - we have the symbol name and want to find the address.
NEW CODE:
To make that work we add three tables:
1) symbols_addresses_index_sorted[0..symbols_num_syms]. The values are indicies into symbols_addresses array. This is a 1-1 table:
symbols_addresses_index_sorted:
.long 6315
.long 6097 To get the address of a symbol at offset 0 in symbols_name_sorted (see below) we look in symbols_addresses_index_sorted[0] to get 6315 and then look in symbols_addresses_index[6315] to find it.
2) The symbols_names_sorted stream - same format as symbols_names but constructued by sorting the <type>[filename#]<symbol> by <symbol>, then [filename#] and lastly type. Recall that symbol_names is sorted based on addresses of the symbols.
3) To make it simpler to search we also add an symbols_markers_sorted array (of size symbols_num_syms/255).
Searching for symbols is simplified as we can do a binary search on symbol_names_sorted (and using symbols_markers_sorted). Since the symbols are sorted it takes on average 13 calls to symbols_expand_symbol.
Doing small tests (search for five different symbols) using the old and new mechanism show 2ms vs 0ms improvement.
---
CC: Keir Fraser <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
v8: New
- Remove the debug code
- Return the 'mid' index in symbol_addresses, not the 'low'.
---
--- xen/arch/x86/Makefile | 3 ++ xen/common/Kconfig | 12 ++++++ xen/common/symbols-dummy.c | 6 +++ xen/common/symbols.c | 75 +++++++++++++++++++++++++++++++++---- xen/tools/symbols.c | 93 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 176 insertions(+), 13 deletions(-)
xen/arch/x86/Makefile +3 -0
@@ -74,6 +74,9 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ ifdef CONFIG_XSPLICE all_symbols = --all-symbols +ifdef CONFIG_FAST_SYMBOL_LOOKUP +all_symbols = --all-symbols --add-extra-sorted +endif else all_symbols = endif
xen/common/Kconfig +12 -0
@@ -200,4 +200,16 @@ config XSPLICE If unsure, say Y. +config FAST_SYMBOL_LOOKUP + bool "Fast symbol lookup (bigger binary)" + default y + depends on XSPLICE + ---help--- + When searching for symbol addresses we can use the built-in system + that is optimized for searching symbols using addresses as the key. + However using it for the inverse (find address using the symbol name) + it is slow. This extra data (~128kB) speeds up the search. + The only user of this is xSplice. + + If unsure, say Y. endmenu
xen/common/symbols-dummy.c +6 -0
@@ -14,6 +14,12 @@ const unsigned long symbols_addresses[1];const unsigned int symbols_num_syms;const u8 symbols_names[1 ]; +#ifdef CONFIG_FAST_SYMBOL_LOOKUP +const u8 symbols_names_sorted[1 ]; +const unsigned int symbols_addresses_index_sorted[1 ]; +const unsigned int symbols_markers_sorted[1 ]; +#endif +const u8 symbols_token_table[1 ];const u16 symbols_token_index[1 ];
xen/common/symbols.c +67 -8
138 lines not on this page · raw message
xen/tools/symbols.c +88 -5
158 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 15/27] xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
NEW CODE:
To make that work we add three tables:
Why three? Two (or a single one with element pairs) ought to be sufficient:
Afaict you could just have (symbol-address,symbol-offset) pairs which you then sort and search by name (using symbols_expand_symbol() on symbol-offset, which in turn you store using the result of get_symbol_offset() during setup of the table).
Quoted 4 lines
1) symbols_addresses_index_sorted[0..symbols_num_syms]. The values are indicies into
symbols_addressesarray. This is a 1-1 table:
symbols_addresses_index_sorted:
>.long 6315
>.long 6097Quoted 9 lines
To get the address of a symbol at offset 0 in
symbols_name_sorted(see below) we look in symbols_addresses_index_sorted[0] to get 6315 and then look in symbols_addresses_index[6315] to find it.2) The
symbols_names_sortedstream - same format assymbols_namesbut constructued by sorting the <type>[filename#]<symbol> by <symbol>, then [filename#] and lastly type. Recall thatsymbol_namesis sorted based on addresses of the symbols.
Why would you treat the <filename># prefix specially? The full thing is the symbol you should care about imo.
Quoted 9 lines
3) To make it simpler to search we also add an
symbols_markers_sortedarray (of size symbols_num_syms/255).Searching for symbols is simplified as we can do a binary search on
symbol_names_sorted(and usingsymbols_markers_sorted). Since the symbols are sorted it takes on average 13 calls tosymbols_expand_symbol.Doing small tests (search for five different symbols) using the old and new mechanism show 2ms vs 0ms improvement.
What does "2m vs 0ms" improvement mean here? If you mean to say that on some symbols there's no gain at all, then I'd question whether millisecond granularity is enough to see an improvement.
With the basic data question above I'm not going to look at the actual code for now; I'll leave this until either I understand why the suggested variant can't work, or until a future version implementing that one.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 15/27] xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
Quoted 40 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
NEW CODE:
To make that work we add three tables:
Why three? Two (or a single one with element pairs) ought to be sufficient:
Afaict you could just have (symbol-address,symbol-offset) pairs which you then sort and search by name (usingsymbols_expand_symbol()on symbol-offset, which in turn you store using the result ofget_symbol_offset()during setup of the table).Quoted 4 lines
1) symbols_addresses_index_sorted[0..symbols_num_syms]. The values are indicies into
symbols_addressesarray. This is a 1-1 table:
symbols_addresses_index_sorted:>.long 6315 >.long 6097Quoted 9 lines
To get the address of a symbol at offset 0 in
symbols_name_sorted(see below) we look in symbols_addresses_index_sorted[0] to get 6315 and then look in symbols_addresses_index[6315] to find it.2) The
symbols_names_sortedstream - same format assymbols_namesbut constructued by sorting the <type>[filename#]<symbol> by <symbol>, then [filename#] and lastly type. Recall thatsymbol_namesis sorted based on addresses of the symbols.Why would you treat the <filename># prefix specially? The full thing is the symbol you should care about imo.
Quoted 9 lines
3) To make it simpler to search we also add an
symbols_markers_sortedarray (of size symbols_num_syms/255).Searching for symbols is simplified as we can do a binary search on
symbol_names_sorted(and usingsymbols_markers_sorted). Since the symbols are sorted it takes on average 13 calls tosymbols_expand_symbol.Doing small tests (search for five different symbols) using the old and new mechanism show 2ms vs 0ms improvement.
Quoted 7 lines
What does "2m vs 0ms" improvement mean here? If you mean to say that on some symbols there's no gain at all, then I'd question whether millisecond granularity is enough to see an improvement.
With the basic data question above I'm not going to look at the actual code for now; I'll leave this until either I understand why the suggested variant can't work, or until a future version implementing that one.
Here is what I came up using your idea. Do you want me to add 'Suggested-by' Jan on it?
From 37173f674e4bce0bc0515d28b2536d29c55b7e7f Mon Sep 17 00:00:00 2001
From: Konrad Rzeszutek Wilk <[email protected]>
Date: Thu, 21 Apr 2016 16:47:43 -0400
Subject: [PATCH] xsplice, symbols: Implement fast symbol names -> virtual
addresses lookup
The current mechanism is geared towards fast virtual address -> symbol names lookup. This is fine for the normal use cases (BUG_ON, WARN_ON, etc), but for xSplice - where we need to find hypervisor symbols - it is slow.
To understand this patch, a description of the existing method is explained first. For folks familar go to 'NEW CODE:'.
HOW IT WORKS:
The symbol table lookup mechanism uses a simple encoding mechanism where it extracts the common ascii characters that the symbol's use.
This saves us space. The lookup mechanism is geared towards looking up symbols based on address. We have one 0..N (where N is the number of symbols, so 6849 for example) table:
symbols_addresses[0..N]
And an 1-1 (in a loose fashion) of the symbols (encoded) in a symbols_names stream of size N.
The N is variable (later on that below)
The symbols_names are sorted based on symbols_addresses, which means that the decoded entries inside symbols_names are not in ascending or descending order.
There is also the encoding mechanism - the table of 255 entries called symbols_token_index[]. And the symbols_token_table which is an stream of ASCIIZ characters, such as (it really is not a table as the values are variable):
@0 .asciz "credit" @6 .asciz "mask"
..
@300 .asciz "S"
And the symbols_token_index:
@0 .short 0 @1 .short 7 @2 .short 12 @4 .short 16
...
@84 .short 300
The relationship between them is that the symbols_token_index gives us the offset to symbols_token_table.
The symbol_names[] array is a stream of encoded values. Each value follows the same pattern - <len> followed by <encoding values>. And the another <len> followed by <encoding values>.
Hence to find the right one you need to read <len>, add <len> (to skip over), read <len>, add <len>, and so on until one finds the right tuple offset.
The <encoding values> are the indicies into the symbols_token_index.
Meaning if you have:
0x04, 0x54, 0xda, 0xe2, 0x74
[4, 84, 218, 226, 116 in human numbering]
The 0x04 tells us that the symbol is four bytes past this one (so next symbol offset starts at 5). If we lookup symbols_token_index[84] we get 300. symbols_token[300] gets us the "S". And so on, the string eventually end up being decode to be 'S_stext'. The first character is the type, then optionally follwed by the filename (and # right after filename) and then lastly the symbol, such as:
tvpmu_intel.c#core2_vpmu_do_interrupt
Keep in mind that there are two fixed sized tables:
symbols_addresses[0..symbols_num_syms], and symbols_markers[0..symbols_num_syms/255].
The symbols_markers is used to speed searching for the right address. It gives us the offsets within symbol_names that start at the <len><encoded value>.
The way to find a symbol based on the address is:
1) Figure out the 'tuple offset' from symbols_address[0..symbols_num_syms]. This table is sorted by virtual addresses so finding the value is simple.
2) Get starting offset of symbol_names by retrieving value of symbol_markers['tuple offset' / 255].
3). Iterate up to 'tuple_offset & 255' in symbols_markers stream starting
at 'offset'.
4). Decode the <len><encoded value>
This however does not work very well if we want to search the other way - we have the symbol name and want to find the address.
NEW CODE:
To make that work we add one fixed size table called symbols_sorted_offsets which has two elements: offset in symbol stream, offset in the symbol-address.
This whole array is sorted on the original symbol name (in case of collision we also take into account the type).
The values are for example:
symbols_sorted_offsets:
.long 83363, 6302 # [.bss, len=5]
.long 80459, 6084 # [.data, len=5]..
[The # added for clarity]
Which makes it incredibly easy to get in the symbols_names and also symbols_addresses (or symbols_offsets)
Searching for symbols is simplified as we can do a binary search on symbols_sorted_offsets. Since the symbols are sorted it takes on average 13 calls to symbols_expand_symbol.
---
CC: Keir Fraser <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
v8: New
- Remove the debug code
- Return the 'mid' index in symbol_addresses, not the 'low'.
v9:
- Make it return void*
- Ditch the old implementation. Use a single fixed-size array with two uint32_t values - offset in stream and offset in address.
--- xen/arch/x86/Makefile | 3 +++ xen/common/Kconfig | 12 ++++++++++ xen/common/symbols-dummy.c | 5 ++++ xen/common/symbols.c | 42 +++++++++++++++++++++++++++++++++ xen/include/xen/symbols.h | 10 ++++++++ xen/tools/symbols.c | 59 ++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 129 insertions(+), 2 deletions(-)
xen/arch/x86/Makefile +3 -0
@@ -74,6 +74,9 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ ifdef CONFIG_XSPLICE all_symbols = --all-symbols +ifdef CONFIG_FAST_SYMBOL_LOOKUP +all_symbols = --all-symbols --add-extra-sorted +endif else all_symbols = endif
xen/common/Kconfig +12 -0
@@ -200,4 +200,16 @@ config XSPLICE If unsure, say Y. +config FAST_SYMBOL_LOOKUP + bool "Fast symbol lookup (bigger binary)" + default y + depends on XSPLICE + ---help--- + When searching for symbol addresses we can use the built-in system + that is optimized for searching symbols using addresses as the key. + However using it for the inverse (find address using the symbol name) + it is slow. This extra data and code (~55kB) speeds up the search. + The only user of this is xSplice. + + If unsure, say Y. endmenu
xen/common/symbols-dummy.c +5 -0
@@ -5,6 +5,7 @@#include <xen/config.h> #include <xen/types.h> +#include <xen/symbols.h> #ifdef SYMBOLS_ORIGIN const unsigned int symbols_offsets[1 ];
@@ -14,6 +15,10 @@ const unsigned long symbols_addresses[1];const unsigned int symbols_num_syms;const u8 symbols_names[1 ]; +#ifdef CONFIG_FAST_SYMBOL_LOOKUP +const struct symbol_offset symbols_sorted_offsets[1 ]; +#endif +const u8 symbols_token_table[1 ];const u16 symbols_token_index[1 ];
xen/common/symbols.c +42 -0
62 lines not on this page · raw message
xen/include/xen/symbols.h +10 -0
@@ -25,4 +25,14 @@ int xensyms_read(uint32_t *symnum, char *type,void *symbols_lookup_by_name (const char *symname); +#ifdef CONFIG_FAST_SYMBOL_LOOKUP +/* +* A sorted (by symbols) lookup table table to symbols_names (stream) +* and symbols_address (or offset). +*/ +struct symbol_offset { +uint32_t stream;/* .. in the compressed stream.*/ +uint32_t addr;/* .. and in the fixed size address array. */ +}; +#endif #endif /*_XEN_SYMBOLS_H*/
xen/tools/symbols.c +11 -0
@@ -40,6 +40,10 @@ struct sym_entry {unsigned long long addr;unsigned int len;unsigned char *sym; +char *orig_symbol; +unsigned int addr_idx; +unsigned int stream_offset; +unsigned char type; };#define SYMBOL_NAME (s) ((char *)(s)->sym + 1)
@@ -47,8 +51,10 @@ static struct sym_entry *table;static unsigned int table_size, table_cnt;static unsigned long long _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;static int all_symbols =0 ; +static int extra_sorted =0 ;static char symbol_prefix_char ='\0' ;static enum { fmt_bsd, fmt_sysv } input_format; +static int compare_name (const void *p1,const void *p2);int token_profit[0x10000 ];
@@ -174,9 +180,13 @@ static int read_symbol(FILE *in, struct sym_entry *s) sym =stpcpy (sym, filename); *sym++ ='#' ; } +strcpy (sym, str); +if (extra_sorted) { + s->orig_symbol =strdup (SYMBOL_NAME (s)); + s->type = stype;/* As s->sym[0] ends mangled. */ + } s->sym[0 ] = stype; rc =0 ;
skip_tail:
@@ -276,6 +286,27 @@ static int expand_symbol(unsigned char *data, int len, char *result)
return total;
}
+/* Sort by original (non mangled) symbol name, then type. */ +static int compare_name_orig(const void *p1, const void *p2) +{
+ const struct sym_entry *sym1 = p1;
+ const struct sym_entry *sym2 = p2;
+ int rc;
+
+ rc = strcmp(sym1->orig_symbol, sym2->orig_symbol);
+
+ if (!rc) {
+ if (sym1->type < sym2->type)
+ rc = -1;
+ else if (sym1->type > sym2->type)
+ rc = 1;
+ else
+ rc = 0;
+ }
+
+ return rc;
+}
+
static void write_src(void)
{
unsigned int i, k, off;
@@ -325,6 +356,7 @@ static void write_src(void)
printf(", 0x%02x", table[i].sym[k]);
printf("\n");
+ table[i].stream_offset = off;
off += table[i].len + 1;
}
printf("\n");
@@ -334,7 +366,6 @@ static void write_src(void)
printf("\t.long\t%d\n", markers[i]);
printf("\n");
- free(markers);
output_label("symbols_token_table"); off = 0;
@@ -350,6 +381,27 @@ static void write_src(void)
for (i = 0; i < 256; i++)
printf("\t.short\t%d\n", best_idx[i]);
printf("\n");
+
+ if (!extra_sorted) {
+ free(markers);
+ return;
+ }
+
+ /* Sorted by original symbol names, filename, and lastly type. */
+ qsort(table, table_cnt, sizeof(*table), compare_name_orig);
+
+ output_label("symbols_sorted_offsets");
+ /* An fixed sized array with two entries: offset in the
+ * compressed stream (for symbol name), and offset in
+ * symbols_addresses (or symbols_offset). */
+ for (i = 0; i < table_cnt; i++) {
+ printf("\t.long %d", table[i].stream_offset);
+ printf(", %d", table[i].addr_idx);
+ printf("\n");
+ }
+ printf("\n");
+
+ free(markers);
}
@@ -410,6 +462,7 @@ static void compress_symbols(unsigned char *str, int idx)
len = table[i].len;
p1 = table[i].sym;
+ table[i].addr_idx = i;
/* find the token on the symbol */
p2 = memmem_pvt(p1, len, str, 2);
if (!p2) continue;
@@ -561,6 +614,8 @@ int main(int argc, char **argv)
input_format = fmt_sysv;
else if (strcmp(argv[i], "--sort") == 0)
unsorted = true;
+ else if (strcmp(argv[i], "--add-extra-sorted") == 0)
+ extra_sorted = 1;
else if (strcmp(argv[i], "--warn-dup") == 0)
warn_dup = true;
else
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 15/27] xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 17:21, <[email protected]> wrote:
Here is what I came up using your idea. Do you want me to add 'Suggested-by' Jan on it?
I'll leave that up to you; it was really just a vague idea that I gave...
xen/arch/x86/Makefile efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ · line 82
Quoted 12 lines
xen/arch/x86/Makefile +3
-0@@ -74,6 +74,9 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ ifdef CONFIG_XSPLICE all_symbols = --all-symbols +ifdef CONFIG_FAST_SYMBOL_LOOKUP +all_symbols = --all-symbols --add-extra-sorted +endif else all_symbols = endif
This now even more calls for using the common list model, at least as later cleanup.
Quoted 11 lines
xen/common/symbols-dummy.c +1
-0@@ -5,6 +5,7 @@#include <xen/config.h> #include <xen/types.h> +#include <xen/symbols.h> #ifdef SYMBOLS_ORIGIN const unsigned int symbols_offsets[1 ];
xen/common/symbols-dummy.c const unsigned long symbols_addresses[1]; · line 20
Quoted 6 lines
xen/common/symbols-dummy.c +3
-0@@ -14,6 +15,10 @@ const unsigned long symbols_addresses[1];const unsigned int symbols_num_syms;const u8 symbols_names[1 ]; +#ifdef CONFIG_FAST_SYMBOL_LOOKUP +const struct symbol_offset symbols_sorted_offsets[1 ]; +#endif
Oh, nice - you even do the sorting at build time!
xen/common/symbols.c extern const unsigned long symbols_addresses[]; · line 36
Quoted 9 lines
xen/common/symbols.c +3
-0@@ -31,6 +31,10 @@ extern const unsigned long symbols_addresses[];extern const unsigned int symbols_num_syms;extern const u8 symbols_names[]; +#ifdef CONFIG_FAST_SYMBOL_LOOKUP +extern const struct symbol_offset symbols_sorted_offsets[]; +#endif
No need for an #ifdef around just a declaration.
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type, · line 220
Quoted 10 lines
xen/common/symbols.c +4
-0@@ -208,8 +212,45 @@ int xensyms_read(uint32_t *symnum, char *type,return 0 ; } +#ifdef CONFIG_FAST_SYMBOL_LOOKUP void *symbols_lookup_by_name (const char *symname) { +char namebuf[KSYM_NAME_LEN +1 ]; +unsigned long low, high; +static const char *filename_token ="#" ;
It's being used just once, so I don't see the value of this. But if you want to retain it, please at least make it an array instead of a pointer.
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type, · line 227
Quoted 6 lines
xen/common/symbols.c +6
-0+if ( *symname =='\0' ) +return NULL ; + +/* Unsupported search for filename in symbol right now. */ +if (strpbrk (symname, filename_token) ) +return NULL ;
That's unfortunate - why are you filtering these out? (Related to the earlier comment - this could even be strchr(), with no need at all for a string literal.)
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type, · line 239
Quoted 11 lines
xen/common/symbols.c +11
-0+ low =0 ; + high = symbols_num_syms; +while ( low < high ) + { +unsigned long mid = low + ((high - low) /2 ); +const struct symbol_offset *s; +int rc; + + s = &symbols_sorted_offsets[mid]; + (void )symbols_expand_symbol (s->stream, namebuf); +/* Format is: [filename]#<symbol>. symbols_expand_symbol eats type.*/
[<filename>#]<symbol>
And what relevance does the type part of the comment have here?
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type, · line 240
Quoted 1 line
xen/common/symbols.c +1
-0+ rc =strcmp (symname, namebuf);
So looking at this I really don't see why you filter filename prefixed input names above.
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type, · line 253
Quoted 13 lines
xen/common/symbols.c +13
-0+if ( rc <0 ) + high = mid; +else if ( rc >0 ) + low = mid +1 ; +else + return (void *)symbols_address(s->addr); + } +return NULL ; +} + +#else +void *symbols_lookup_by_name (const char *symname) + {
Please put the #ifdef/#else/#endif inside the function body, to avoid duplicating code as much as possible. Putting them outside would be needed primarily if the function type varied between the two instances.
Quoted 6 lines
xen/common/symbols.c +6
-0+/* Sort by original (non mangled) symbol name, then type. */ +static int compare_name_orig (const void *p1,const void *p2) +{ +const struct sym_entry *sym1 = p1; +const struct sym_entry *sym2 = p2; +int rc;
xen/common/symbols.c int xensyms_read(uint32_t *symnum, char *type,
Quoted 10 lines
xen/common/symbols.c +10
-0+ + rc =strcmp (sym1->orig_symbol, sym2->orig_symbol); + +if (!rc) { +if (sym1->type < sym2->type) + rc =-1 ; +else if (sym1->type > sym2->type) + rc =1 ; +else + rc =0 ;
How about just "rc = sym1->type - sym2->type"?
Quoted 5 lines
xen/common/symbols.c +1
-0@@ -350,6 +381,27 @@ static void write_src(void)for (i =0 ; i <256 ; i++)printf ("\t.short\t%d\n" , best_idx[i]);printf ("\n" ); +
xen/common/symbols.c static void write_src(void) · line 394
Quoted 10 lines
xen/common/symbols.c +10
-0+if (!extra_sorted) { +free (markers); +return ; + } + +/* Sorted by original symbol names, filename, and lastly type. */ +qsort (table, table_cnt,sizeof (*table), compare_name_orig); + +output_label ("symbols_sorted_offsets" ); +/* An fixed sized array with two entries: offset in the
"A fixed size ..."
xen/common/symbols.c static void write_src(void) · line 400
Quoted 6 lines
xen/common/symbols.c +6
-0+* compressed stream (for symbol name), and offset in +* symbols_addresses (or symbols_offset). */ +for (i =0 ; i < table_cnt; i++) { +printf ("\t.long %d" , table[i].stream_offset); +printf (", %d" , table[i].addr_idx); +printf ("\n" );
How about making this just a single printf()? Also both are unsigned, so please at least %u, but even better %#x.
xen/common/symbols.c int main(int argc, char **argv) · line 618
Quoted 6 lines
xen/common/symbols.c +2
-0@@ -561,6 +614,8 @@ int main(int argc, char **argv) input_format = fmt_sysv; else if (strcmp(argv[i], "--sort") == 0) unsorted = true; + else if (strcmp(argv[i], "--add-extra-sorted") == 0) + extra_sorted = 1;
s/extra/name/ ?
Or --sort-by-name?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 15/27] xsplice, symbols: Implement fast symbol names -> virtual addresses lookup
Quoted 10 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 17:21, <[email protected]> wrote:
Here is what I came up using your idea. Do you want me to add 'Suggested-by' Jan on it?
I'll leave that up to you; it was really just a vague idea that I gave...
Quoted 4 lines
xen/arch/x86/Makefile +0
-0@@ -74,6 +74,9 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \
Quoted 11 lines
xen/arch/x86/Makefile efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ · line 82
Quoted 8 lines
xen/arch/x86/Makefile +3
-0ifdef CONFIG_XSPLICE all_symbols = --all-symbols +ifdef CONFIG_FAST_SYMBOL_LOOKUP +all_symbols = --all-symbols --add-extra-sorted +endif else all_symbols = endifThis now even more calls for using the common list model, at least as later cleanup.
OK, put it on the Wiki.
Quoted 15 lines
Quoted 15 lines
xen/common/symbols-dummy.c +1
-0@@ -5,6 +5,7 @@#include <xen/config.h> #include <xen/types.h> +#include <xen/symbols.h> #ifdef SYMBOLS_ORIGIN const unsigned int symbols_offsets[1 ];@@ -14,6 +15,10 @@ const unsigned long symbols_addresses[1];const unsigned int symbols_num_syms;const u8 symbols_names[1 ];
Quoted 5 lines
xen/common/symbols-dummy.c const unsigned long symbols_addresses[1]; · line 20
Quoted 3 lines
xen/common/symbols-dummy.c +3
-0+#ifdef CONFIG_FAST_SYMBOL_LOOKUP +const struct symbol_offset symbols_sorted_offsets[1 ]; +#endif Oh, nice - you even do the sorting at build time!
:-)
..snip..
Quoted 3 lines
Quoted 3 lines
xen/common/symbols-dummy.c +1
-0+#ifdef CONFIG_FAST_SYMBOL_LOOKUP void *symbols_lookup_by_name (const char *symname) {
..snip..
Quoted 7 lines
xen/common/symbols-dummy.c
Quoted 3 lines
xen/common/symbols-dummy.c +3
-0+/* Unsupported search for filename in symbol right now. */ +if (strpbrk (symname, filename_token) ) +return NULL ;That's unfortunate - why are you filtering these out? (Related to the earlier comment - this could even be
strchr(), with no need at all for a string literal.)
This was an artificat of the earlier version. Removed it all now.
Quoted 13 lines
xen/common/symbols-dummy.c
Quoted 12 lines
xen/common/symbols-dummy.c +11
-0+ low =0 ; + high = symbols_num_syms; +while ( low < high ) + { +unsigned long mid = low + ((high - low) /2 ); +const struct symbol_offset *s; +int rc; + + s = &symbols_sorted_offsets[mid]; + (void )symbols_expand_symbol (s->stream, namebuf); +/* Format is: [filename]#<symbol>. symbols_expand_symbol eats type.*/
Quoted 3 lines
[<filename>#]<symbol>
And what relevance does the type part of the comment have here?
Artifact of the older version - Where I was searching for #. .. snip.p
I removed the comment.
Quoted 7 lines
xen/common/symbols-dummy.c
Quoted 4 lines
xen/common/symbols-dummy.c +4
-0+for (i =0 ; i < table_cnt; i++) { +printf ("\t.long %d" , table[i].stream_offset); +printf (", %d" , table[i].addr_idx); +printf ("\n" );How about making this just a single
printf()? Also both are unsigned, so please at least %u, but even better %#x.
I would rather have it as %u. As when debugging this I found it quite useful for these values to be decimal as I could edit the .xen-sym.0.S file and see in the editor where it would match up with the symbols_addresses or the symbol_names in a quite easy way. Doing it in hex means doing extra calculations.
Quoted 10 lines
xen/common/symbols-dummy.c int main(int argc, char **argv) · line 618
Quoted 7 lines
xen/common/symbols-dummy.c +2
-0@@ -561,6 +614,8 @@ int main(int argc, char **argv) input_format = fmt_sysv; else if (strcmp(argv[i], "--sort") == 0) unsorted = true; + else if (strcmp(argv[i], "--add-extra-sorted") == 0) + extra_sorted = 1;s/extra/name/ ?
Quoted 3 lines
Or --sort-by-name?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 16/27 x86, xsplice: Print payload's symbol name and payload name in backtraces
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Naturally the backtrace is presented when an instruction hits an bug_frame or %p is used.
The payloads do not support bug_frames yet - however the functions the payloads call could hit an BUG() or WARN().
The traps.c has logic to scan for it this - and eventually it will find the correct bug_frame and the walk the stack using %p to print the backtrace. For %p and symbols to print a string - the 'is_active_kernel_text' is consulted which uses an 'struct virtual_region'.
Therefore we register our start->end addresses so that 'is_active_kernel_text' will include our payload address.
We also register our symbol lookup table function so that it can scan the list of payloads and retrieve the correct name.
Lastly we change vsprintf to take into account s and namebuf. For core code they are the same, but for payloads they are different. This gets us:
Xen call trace:
[<ffff82d080a00041>] revert_hook+0x31/0x35 [xen_hello_world] [<ffff82d0801431bd>] xsplice.c#revert_payload+0x86/0xc6 [<ffff82d080143502>] check_for_xsplice_work+0x233/0x3cd [<ffff82d08017a0b2>] domain.c#continue_idle_domain+0x9/0x1f
Which is great if payloads have similar or same symbol names.
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
v2: Add missing full stop.
v3: s/module/payload/
v4: Expand comment and include registration of 'virtual_region'
Redo the vsprintf handling of payload name.
Drop the ->skip function
v6: Add comment explaining the purpose behind the strcmp.
Redid per Jan's review.
v7: Add Andrew's Review-by
Drop the strcmp and just do pointer checks.
---
--- xen/common/vsprintf.c | 19 +++++++++++++++-- xen/common/xsplice.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-)
xen/common/vsprintf.c +17 -2
@@ -20,6 +20,7 @@#include <xen/symbols.h> #include <xen/lib.h> #include <xen/sched.h> +#include <xen/xsplice.h> #include <asm/div64.h> #include <asm/page.h>
@@ -331,16 +332,23 @@ static char *pointer(char *str, char *end, const char **fmt_ptr, {unsigned long sym_size, sym_offset;char namebuf[KSYM_NAME_LEN +1 ]; +bool_t payload =0 ;/* Advance parents fmt string, as we have consumed 's' or 'S' */ ++*fmt_ptr; s =symbols_lookup ((unsigned long )arg, &sym_size, &sym_offset, namebuf); - -/* If the symbol is not found, fall back to printing the address */ +/* If the symbol is not found, fall back to printing the address. */ if ( !s )break ; +/* +* namebuf contents and s for core hypervisor are same but for xSplice +* payloads they differ (namebuf contains the name of the payload). +*/ +if ( namebuf != s ) + payload =1 ; +/* Print symbol name */ str =string (str, end, s,-1 ,-1 ,0 );
@@ -354,6 +362,13 @@ static char *pointer(char *str, char *end, const char **fmt_ptr, str =number (str, end, sym_size,16 ,-1 ,-1 ,SPECIAL ); } +if ( payload ) + { + str =string (str, end," [" ,-1 ,-1 ,0 ); + str =string (str, end, namebuf,-1 ,-1 ,0 ); + str =string (str, end,"]" ,-1 ,-1 ,0 ); + } +return str; }
xen/common/xsplice.c +59 -0
102 lines not on this page · raw message
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 16/27] x86, xsplice: Print payload's symbol name and payload name in backtraces
Review static char *pointer(char *str, char *end, const char **fmt_ptr,
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
(quoted diff) +0
-0@@ -331,16 +332,23 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
>{
>unsigned long sym_size, sym_offset;
>char namebuf[KSYM_NAME_LEN+1];xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 335
Quoted 1 line
xen/common/vsprintf.c +1
-0+bool_t payload =0 ;
I don't see the point of this variable. You can simply do ...
>/* Advance parents fmt string, as we have consumed 's' or 'S' */
Review static char *pointer(char *str, char *end, const char **fmt_ptr, · line 334
Quoted 2 lines
(quoted diff) +1
-0++*fmt_ptr;
>s = symbols_lookup((unsigned long)arg, &sym_size, &sym_offset, namebuf);
xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 341
Quoted 3 lines
xen/common/vsprintf.c +1
-2- -/* If the symbol is not found, fall back to printing the address */ +/* If the symbol is not found, fall back to printing the address. */
... (perhaps omitting this unrelated style correction) ...
xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 349
Quoted 8 lines
xen/common/vsprintf.c +5
-0if ( !s )break ; +/* +* namebuf contents and s for core hypervisor are same but for xSplice +* payloads they differ (namebuf contains the name of the payload). +*/ +if ( namebuf != s )
... this comparison ...
xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 362
Quoted 2 lines
xen/common/vsprintf.c +0
-0@@ -354,6 +362,13 @@ static char *pointer(char *str, char *end, const char **fmt_ptr, str =number (str, end, sym_size,16 ,-1 ,-1 ,SPECIAL );
>}
xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 365
Quoted 2 lines
xen/common/vsprintf.c +1
-0+if ( payload )
... here.
xen/common/xsplice.c unsigned long xsplice_symbols_lookup_by_name(const char *symname) · line 152
Quoted 6 lines
xen/common/xsplice.c +6
-0+static const char *xsplice_symbols_lookup (unsigned long addr, +unsigned long *symbolsize, +unsigned long *offset, +char *namebuf) +{ +struct payload *data;
const
Quoted 11 lines
xen/common/xsplice.c +11
-0+unsigned int i; +int best; + +/* +* No locking since this list is only ever changed during apply or revert +* context. +*/ + list_for_each_entry ( data, &applied_list, applied_list ) + { +if ( (void *)addr < data->text_addr && + (void *)addr >= (data->text_addr + data->pages *PAGE_SIZE ) )
xen/common/xsplice.c unsigned long xsplice_symbols_lookup_by_name(const char *symname) · line 173
Quoted 10 lines
xen/common/xsplice.c +10
-0+continue ; + + best =-1 ; + +for ( i =0 ; i < data->nsyms; i++ ) + { +if ( data->symtab[i].value <= addr && + (best ==-1 || + data->symtab[best].value < data->symtab[i].value) ) + best = i;
Considering this, "best" would probably also better be unsigned int, and you could then set and compare it to data->nsyms or UINT_MAX.
xen/common/xsplice.c unsigned long xsplice_symbols_lookup_by_name(const char *symname) · line 180
Quoted 7 lines
xen/common/xsplice.c +7
-0+ } + +if ( best ==-1 ) +return NULL ; + +if ( symbolsize ) + *symbolsize = data->symtab[best].size;
Ah, here is where you need the size.
Review static int prepare_payload(struct payload *payload,
Quoted 1 line
(quoted diff) +0
-0@@ -425,6 +475,13 @@ static int prepare_payload(struct payload *payload,
>}
>}xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 483
Quoted 7 lines
xen/common/xsplice.c +6
-0+/* Setup the virtual region with proper data. */ + region = &payload->region; + + region->symbols_lookup = xsplice_symbols_lookup; + region->start = (unsigned long )payload->text_addr; + region->end = (unsigned long )(payload->text_addr + payload->text_size);
Again makes questionable whether region's start and end fields wouldn't better be const void *.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 17/27 xsplice: Add support for bug frames.
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Add support for handling bug frames contained with xsplice modules. If a trap occurs search either the kernel bug table or an applied payload's bug table depending on the instruction pointer.
---
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v2:- s/module/payload/
- add build time check in case amount of bug frames expands.
- add define for the number of bug-frames.
v3:
- add missing BUGFRAME_NR, squash s/core_size/core/ in earlier patch.
- Moved code around.
- Changed per Andrew's recommendation.
- Fixed style changes.
- Made it compile under ARM (PRIu32,PRIu64)
v4: Use 'struct virtual_region'
- Rip more of the is_active_text code.
- Use one function for the ->skip
- Include test-case
v5: Rip out the ->skip function.
v7: Add a text check as well.
Add Andrew's Reviewed-by.
v8: Changed dprintk XENLOG_DEBUG to XENLOG_ERR
---
--- xen/arch/x86/traps.c | 5 +++-- xen/common/xsplice.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/xsplice.h | 5 +++++ 3 files changed, 58 insertions(+), 2 deletions(-)
xen/arch/x86/traps.c +3 -2
@@ -50,6 +50,7 @@#include <xen/paging.h> #include <xen/virtual_region.h> #include <xen/watchdog.h> +#include <xen/xsplice.h> #include <asm/system.h> #include <asm/io.h> #include <asm/atomic.h>
@@ -1287,7 +1288,7 @@ void do_invalid_op(struct cpu_user_regs *regs)/* WARN, BUG or ASSERT: decode the filename pointer and line number. */ filename =bug_ptr (bug); -if ( !is_kernel (filename) ) +if ( !is_kernel (filename) && !is_patch (filename) ) goto die; fixup =strlen (filename);if ( fixup >50 )
@@ -1314,7 +1315,7 @@ void do_invalid_op(struct cpu_user_regs *regs)case BUGFRAME_assert:/* ASSERT: decode the predicate string pointer. */ predicate =bug_msg (bug); -if ( !is_kernel (predicate) ) +if ( !is_kernel (predicate) && !is_patch (predicate) ) predicate ="<unknown>" ; printk("Assertion '%s' failed at %s%s:%d\n",
xen/common/xsplice.c +50 -0
62 lines not on this page · raw message
xen/include/xen/xsplice.h +5 -0
@@ -55,6 +55,7 @@ struct xsplice_symbol {int xsplice_op (struct xen_sysctl_xsplice_op *);void check_for_xsplice_work (void ); +bool_t is_patch (const void *addr);unsigned long xsplice_symbols_lookup_by_name (const char *symname);/* Arch hooks. */
@@ -107,6 +108,10 @@ static inline int xsplice_op(struct xen_sysctl_xsplice_op *op) }static inline void check_for_xsplice_work (void ) { }; +static inline bool_t is_patch (const void *addr) +{ +return 0 ; +}#endif /* CONFIG_XSPLICE */ #endif /* __XEN_XSPLICE_H__ */
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;
You guess it: const.
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 132
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/
What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Review static int prepare_payload(struct payload *payload,
Quoted 1 line
(quoted diff) +0
-0@@ -482,6 +508,30 @@ static int prepare_payload(struct payload *payload,
>region->start = (unsigned long)payload->text_addr;
>region->end = (unsigned long)(payload->text_addr + payload->text_size);xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 522
Quoted 13 lines
xen/common/xsplice.c +12
-0+/* Optional sections. */ +for ( i =0 ; i <BUGFRAME_NR ; i++ ) + { +char str[14 ]; + +snprintf (str,sizeof (str),".bug_frames.%u" , i); + sec =xsplice_elf_sec_by_name (elf, str); +if ( !sec ) +continue ; + +if ( sec->sec->sh_size && + (sec->sec->sh_size %sizeof (*region->frame[i].bugs)) )
The left side of the && seems pointless.
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 532
Quoted 10 lines
xen/common/xsplice.c +10
-0+ { +dprintk (XENLOG_ERR ,XSPLICE "%s: Wrong size of .bug_frames.%u!\n" , + elf->name, i); +return -EINVAL ; + } + + region->frame[i].bugs = sec->load_addr; +if ( sec->sec->sh_size) + region->frame[i].n_bugs = sec->sec->sh_size / +sizeof (*region->frame[i].bugs);
As does the conditional here.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 7 lines · Jan Beulich
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;You guess it: const.
Quoted 7 lines
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 132
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/ What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Yes! We only add the struct payload to this applied_list _after_ the patching has been done.` Hence if we crashed the list would not contain the struct payload that was patching - but would be safe to traverse.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:29, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;You guess it: const.
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/
Quoted 6 lines
Quoted 2 lines
What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Yes! We only add the struct payload to this
applied_list_after_ the patching has been done.` Hence if we crashed the list would not contain the struct payload that was patching - but would be safe to traverse.
Well, that only partly answers the question. Patch application to me means the entire process, i.e. up to and including adding the new patch to the list (read: the crash could also happen there). Hence I'd still like it to be made sure that even if the addition has got done only partially, the list remains traversable. Or IOW, I'm not sure the standard list macros make any guarantees like that.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 21 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:29, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;You guess it: const.
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/ Quoted 6 lines
Quoted 2 lines
What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Yes! We only add the struct payload to this
applied_list_after_ the patching has been done.` Hence if we crashed the list would not contain the struct payload that was patching - but would be safe to traverse.
Quoted 6 lines
Well, that only partly answers the question. Patch application to me means the entire process, i.e. up to and including adding the new patch to the list (read: the crash could also happen there). Hence I'd still like it to be made sure that even if the addition has got done only partially, the list remains traversable. Or IOW, I'm not sure the standard list macros make any guarantees like that.
It does. I've copied-n-pasted the list_add_tail hacking it have an BUG_ON(1):
+static inline void __list_add_crash(struct list_head *new,
+ struct list_head *prev, /* applied list */
+ struct list_head *next /* applied_list */)
+{
+ next->prev = new; /* applied_list->prev = new */ + new->next = next; /* new->next = applied_list */ + new->prev = prev; /* new->prev = applied_list */ + BUG_ON(1); + prev->next = new; /* applied_list->next=new */
+}
+static inline void list_add_tail_crash(struct list_head *new, struct list_head *head)
+{
+ __list_add_crash(new, head->prev /* applied_list */, head /* applied_list */);
+}
static int apply_payload(struct payload *data)
{
unsigned int i;@@ -967,7 +981,7 @@ static int apply_payload(struct payload *data)
arch_xsplice_patching_leave();
- list_add_tail(&data->applied_list, &applied_list);
+ list_add_tail_crash(&data->applied_list, &applied_list);
register_virtual_region(&data->region);
return 0;And we get:
(XEN) xsplice: xen_hello_world: Applying 1 functions (XEN) Xen BUG at xsplice.c:963
(XEN) *** DOUBLE FAULT ***
A bit more debugging shows that "is_patch" does not iterate over the list - (which is inline with what I expected!) - so the insertion is safe when crashing.
Now why I get a double fault.. will have to investigate that at some point.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:10, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:29, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;You guess it: const.
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/ Quoted 6 lines
Quoted 2 lines
What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Yes! We only add the struct payload to this
applied_list_after_ the patching has been done.` Hence if we crashed the list would not contain the struct payload that was patching - but would be safe to traverse.Quoted 6 lines
Well, that only partly answers the question. Patch application to me means the entire process, i.e. up to and including adding the new patch to the list (read: the crash could also happen there). Hence I'd still like it to be made sure that even if the addition has got done only partially, the list remains traversable. Or IOW, I'm not sure the standard list macros make any guarantees like that.
It does. I've copied-n-pasted the
list_add_tailhacking it have anBUG_ON(1):
Review
Quoted 10 lines
(quoted diff) +10
-0+static inline void __list_add_crash(struct list_head *new, + struct list_head *prev, /* applied list */ + struct list_head *next /* applied_list */) +{ + next->prev = new; /* applied_list->prev = new */ + new->next = next; /* new->next = applied_list */ + new->prev = prev; /* new->prev = applied_list */ + BUG_ON(1); + prev->next = new; /* applied_list->next=new */ +}
This doesn't mean anything - in the absence of the BUG_ON() the compiler is free to re-order all four assignments.
Quoted 3 lines
A bit more debugging shows that "
is_patch" does not iterate over the list - (which is inline with what I expected!) - so the insertion is safe when crashing.
How does that match up with
bool_t is_patch(const void *ptr)
{
struct payload *data;
/*
* No locking since this list is only ever changed during apply or revert
* context.
*/
list_for_each_entry ( data, &applied_list, applied_list )
...i.e. where we started from here? I really think you want to at least consider using list_add_rcu() at the insertion site, as your main concern is that prev->next shouldn't get updated before new->next.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:10, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:29, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/common/xsplice.c static int verify_payload(const xen_sysctl_xsplice_upload_t *upload, char *n) · line 127
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/common/xsplice.c +3
-0+bool_t is_patch (const void *ptr) +{ +struct payload *data;You guess it: const.
Quoted 4 lines
xen/common/xsplice.c +4
-0+/* +* No locking since this list is only ever changed during apply or revert +* context. +*/ Quoted 6 lines
Quoted 2 lines
What if you crash while applying or reverting a patch? Is the list update at least done such that the (then nested) traversal remains safe?
Yes! We only add the struct payload to this
applied_list_after_ the patching has been done.` Hence if we crashed the list would not contain the struct payload that was patching - but would be safe to traverse.Quoted 6 lines
Well, that only partly answers the question. Patch application to me means the entire process, i.e. up to and including adding the new patch to the list (read: the crash could also happen there). Hence I'd still like it to be made sure that even if the addition has got done only partially, the list remains traversable. Or IOW, I'm not sure the standard list macros make any guarantees like that.
It does. I've copied-n-pasted the
list_add_tailhacking it have anBUG_ON(1):
Quoted 13 lines
Review
Quoted 10 lines
(quoted diff) +10
-0+static inline void __list_add_crash(struct list_head *new, + struct list_head *prev, /* applied list */ + struct list_head *next /* applied_list */) +{ + next->prev = new; /* applied_list->prev = new */ + new->next = next; /* new->next = applied_list */ + new->prev = prev; /* new->prev = applied_list */ + BUG_ON(1); + prev->next = new; /* applied_list->next=new */ +}This doesn't mean anything - in the absence of the
BUG_ON()the compiler is free to re-order all four assignments.
/me nods.
Quoted 18 lines
Quoted 4 lines
A bit more debugging shows that "
is_patch" does not iterate over the list - (which is inline with what I expected!) - so the insertion is safe when crashing.How does that match up with
bool_tis_patch(const void *ptr){ struct payload *data; /* * No locking since this list is only ever changed during apply or revert * context. */ list_for_each_entry ( data, &applied_list, applied_list ) ...
Quoted 1 line
i.e. where we started from here? I really think you want to at least
The thing that makes it "safe" (in lieu of your comment about re-ordering) is that the trap code does:
1289 /* WARN, BUG or ASSERT: decode the filename pointer and line number. */ 1290 filename = bug_ptr(bug); 1291 if ( !is_kernel(filename) && !is_patch(filename) )
1292 goto die;
Which means we first try 'is_kernel'. Since the list addition cannot be part of the payload it will always be part of the hypervisor.
Quoted 2 lines
consider using
list_add_rcu()at the insertion site, as your main concern is thatprev->nextshouldn't get updated beforenew->next.
/me nods. Let me look at that.
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:54, <[email protected]> wrote:
The thing that makes it "safe" (in lieu of your comment about re-ordering) is that the trap code does:
Quoted 8 lines
1289 /* WARN, BUG or ASSERT: decode the filename pointer and line
number. */
1290 filename =bug_ptr(bug);
1291 if ( !is_kernel(filename) && !is_patch(filename) )
1292 goto die;Which means we first try '
is_kernel'. Since the list addition cannot be part of the payload it will always be part of the hypervisor.
I'm sorry, but no. The bug hit (and hence the referenced file name) may be in some other payload, requiring list traversal.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 17/27] xsplice: Add support for bug frames.
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.04.16 at 12:54, <[email protected]> wrote:
The thing that makes it "safe" (in lieu of your comment about re-ordering) is that the trap code does:
1289 /* WARN, BUG or ASSERT: decode the filename pointer and line
number. */
1290 filename =bug_ptr(bug);
1291 if ( !is_kernel(filename) && !is_patch(filename) )
1292 goto die;
Quoted 5 lines
Quoted 2 lines
Which means we first try '
is_kernel'. Since the list addition cannot be part of the payload it will always be part of the hypervisor.I'm sorry, but no. The bug hit (and hence the referenced file name) may be in some other payload, requiring list traversal.
Ah yes! Let me dig in the rcu list to thwart the compiler from re-ordering.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 18/27 xsplice: Add support for exception tables.
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Add support for exception tables contained within xSplice payloads. If an exception occurs search either the main exception table or a particular active payload's exception table depending on the instruction pointer.
Also we add an test-case to make sure we have an exception that is handled.
To not grow the code-base if xSplice is not compiled in we add certain #define to help in determining if code needs to be __init or not.
---
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v3:
- s/module/payload/
- sanity checks.
- Move code around.
- s/module/payload/
v4: Use 'struct virtual_region'
v5:
- Expand test-case.
- Deal with struct exception_table_entry being const.
v6:
- Make the code have __init if not compiled with xSplice
- Remove not needed declarations.
v7:
- Make the non_canonical_addr be 0xdead000000000000ULL
- Remove casts
- Add Reviewed-by from Andrew
- Change ifdef to be !ARM
v8:
- Change dprintk XENLOG_DEBUG to XENLOG_ERR on dprintk.
---
--- xen/arch/x86/extable.c | 31 ++++++++++++++++++------------- xen/arch/x86/test/xen_hello_world_func.c | 11 +++++++++++ xen/common/xsplice.c | 25 +++++++++++++++++++++++++ xen/include/asm-x86/uaccess.h | 2 ++ xen/include/xen/xsplice.h | 17 +++++++++++++++++ 5 files changed, 73 insertions(+), 13 deletions(-)
xen/arch/x86/extable.c +18 -13
@@ -7,6 +7,7 @@#include <xen/spinlock.h> #include <asm/uaccess.h> #include <xen/virtual_region.h> +#include <xen/xsplice.h> #define EX_FIELD (ptr, field) ((unsigned long)&(ptr)->field + (ptr)->field)
@@ -20,7 +21,7 @@ static inline unsigned long ex_cont(const struct exception_table_entry *x)return EX_FIELD (x, cont); } -static int __init cmp_ex (const void *a,const void *b) +static int __INIT cmp_ex(const void *a, const void *b) {const struct exception_table_entry *l = a, *r = b;unsigned long lip =ex_addr (l);
@@ -35,7 +36,7 @@ static int __init cmp_ex(const void *a, const void *b) }#ifndef swap_ex -static void __init swap_ex (void *a,void *b,int size) +static void __INIT swap_ex(void *a, void *b, int size) {struct exception_table_entry *l = a, *r = b, tmp;long delta = b - a;
@@ -48,19 +49,23 @@ static void __init swap_ex(void *a, void *b, int size) }#endif -void __init sort_exception_tables (void ) +void __INIT sort_exception_table(struct exception_table_entry *start, +struct exception_table_entry *stop) { -sort (__start___ex_table, __stop___ex_table - __start___ex_table, -sizeof (struct exception_table_entry ), cmp_ex, swap_ex); -sort (__start___pre_ex_table, - __stop___pre_ex_table - __start___pre_ex_table, +sort (start, stop - start,sizeof (struct exception_table_entry ), cmp_ex, swap_ex); } -static inline unsigned long -search_one_table (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) +void __init sort_exception_tables (void ) +{ +sort_exception_table (__start___ex_table, __stop___ex_table); +sort_exception_table (__start___pre_ex_table, __stop___pre_ex_table); +} + +unsigned long +search_one_extable (const struct exception_table_entry *first, +const struct exception_table_entry *last, +unsigned long value) {const struct exception_table_entry *mid;long diff;
@@ -85,7 +90,7 @@ search_exception_table(unsigned long addr)const struct virtual_region *region =find_text_region (addr);if ( region && region->ex ) -return search_one_table (region->ex, region->ex_end -1 , addr); +return search_one_extable (region->ex, region->ex_end -1 , addr);return 0 ; }
@@ -94,7 +99,7 @@ unsigned long search_pre_exception_table(struct cpu_user_regs *regs) {unsigned long addr = (unsigned long )regs->eip; -unsigned long fixup =search_one_table ( +unsigned long fixup =search_one_extable ( __start___pre_ex_table, __stop___pre_ex_table-1 , addr);if ( fixup ) {
xen/arch/x86/test/xen_hello_world_func.c +11 -0
@@ -5,9 +5,20 @@#include <xen/types.h> +static unsigned long *non_canonical_addr = (unsigned long *)(0xdead000000000000ULL ); +/* Our replacement function for xen_extra_version. */ const char *xen_hello_world (void ) { +unsigned long tmp; +int rc; +/* +* Any BUG, or WARN_ON will contain symbol and payload name. Furthermore +* exceptions will be caught and processed properly. +*/ + rc =__get_user (tmp, non_canonical_addr); +BUG_ON (rc != -EFAULT ); +return "Hello World" ; }
xen/common/xsplice.c +25 -0
@@ -532,6 +532,31 @@ static int prepare_payload(struct payload *payload,sizeof (*region->frame[i].bugs); } +#ifndef CONFIG_ARM + sec =xsplice_elf_sec_by_name (elf,".ex_table" ); +if ( sec ) + { +struct exception_table_entry *s, *e; + +if ( !sec->sec->sh_size || + (sec->sec->sh_size %sizeof (*region->ex)) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Wrong size of .ex_table (exp:%lu vs %lu)!\n" , + elf->name,sizeof (*region->ex), + sec->sec->sh_size); +return -EINVAL ; + } + + s = sec->load_addr; + e = sec->load_addr + sec->sec->sh_size; + +sort_exception_table (s ,e); + + region->ex = s; + region->ex_end = e; + } +#endif +return 0 ; }
xen/include/asm-x86/uaccess.h +2 -0
@@ -277,5 +277,7 @@ extern struct exception_table_entry __stop___pre_ex_table[];extern unsigned long search_exception_table (unsigned long );extern void sort_exception_tables (void ); +extern void sort_exception_table (struct exception_table_entry *start, +struct exception_table_entry *stop);#endif /* __X86_UACCESS_H__ */
xen/include/xen/xsplice.h +17 -0
@@ -43,6 +43,15 @@ struct xsplice_patch_func_internal { } u; }; +/* +* We use alternative and exception table code - which by default are __init +* only, however we need them during runtime. These macros allows us to build +* the image with these functions built-in. (See the #else below). +*/ +#define __INITCONST +#define __INITDATA +#define __INIT +/* Convenience define for printk. */ #define XSPLICE "xsplice: "
@@ -101,6 +110,14 @@ void arch_xsplice_mask(void);void arch_xsplice_unmask (void );#else +/* +* If not compiling with xSplice certain functionality should stay as +* __init. +*/ +#define __INITCONST __initconst +#define __INITDATA __initdata +#define __INIT __init +#include <xen/errno.h> /* For -ENOSYS */ static inline int xsplice_op (struct xen_sysctl_xsplice_op *op) {
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 18/27] xsplice: Add support for exception tables.
xen/arch/x86/extable.c static void __init swap_ex(void *a, void *b, int size) · line 53
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
xen/arch/x86/extable.c +2
-1@@ -48,19 +49,23 @@ static void __init swap_ex(void *a, void *b, int size) }#endif -void __init sort_exception_tables (void ) +void __INIT sort_exception_table(struct exception_table_entry *start, +struct exception_table_entry *stop)
The latter can (and hence should) be const.
xen/arch/x86/test/xen_hello_world_func.c line 8
Quoted 7 lines
xen/arch/x86/test/xen_hello_world_func.c +1
-0@@ -5,9 +5,20 @@#include <xen/types.h> +static unsigned long *non_canonical_addr = (unsigned long *)(0xdead000000000000ULL );
Stray parentheses.
xen/include/xen/xsplice.h struct xsplice_patch_func_internal {
Quoted 3 lines
xen/include/xen/xsplice.h +0
-0@@ -43,6 +43,15 @@ struct xsplice_patch_func_internal {
>} u;
xen/include/xen/xsplice.h struct xsplice_patch_func_internal { · line 53
Quoted 10 lines
xen/include/xen/xsplice.h +8
-0}; +/* +* We use alternative and exception table code - which by default are __init +* only, however we need them during runtime. These macros allows us to build +* the image with these functions built-in. (See the #else below). +*/ +#define __INITCONST +#define __INITDATA +#define __INIT
I'm generally opposed to new symbols with double underscores in front of them. Maybe (considering the non-xSplice case) in this case they're warranted, but I'd prefer something along the lines of Linux'es __init_or_module (without the double underscores then of course).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 19/27 xsplice: Add support for alternatives
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
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.
---
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
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.
---
--- xen/arch/x86/Makefile | 4 +++ xen/arch/x86/alternative.c | 42 ++++++++++++++++++++------------ xen/arch/x86/test/xen_hello_world_func.c | 5 ++++ xen/common/xsplice.c | 34 ++++++++++++++++++++++++++ xen/include/asm-x86/alternative.h | 4 +++ 5 files changed, 74 insertions(+), 15 deletions(-)
xen/arch/x86/Makefile +4 -0
@@ -6,7 +6,11 @@ subdir-y += mm subdir-$(CONFIG_XENOPROF) += oprofile subdir-y += x86_64 +ifdef CONFIG_XSPLICE +obj-y += alternative.o +else obj-bin-y += alternative.init.o +endif obj-y += apic.o obj-y += bitops.o obj-bin-y += bzimage.init.o
xen/arch/x86/alternative.c +27 -15
105 lines not on this page · raw message
xen/arch/x86/test/xen_hello_world_func.c +5 -0
@@ -3,6 +3,9 @@* */ +#include <asm/alternative.h> +#include <asm/nops.h> +#include <asm/uaccess.h> #include <xen/types.h> static unsigned long *non_canonical_addr = (unsigned long *)(0xdead000000000000ULL );
@@ -12,6 +15,8 @@ const char *xen_hello_world(void) {unsigned long tmp;int rc; + +alternative (ASM_NOP1 ,ASM_NOP1 ,X86_FEATURE_LM );/* * Any BUG, or WARN_ON will contain symbol and payload name. Furthermore * exceptions will be caught and processed properly.
xen/common/xsplice.c +34 -0
@@ -533,6 +533,39 @@ 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 || + (sec->sec->sh_size %sizeof (*a)) ) + { +dprintk (XENLOG_ERR ,XSPLICE "%s: Wrong size of .alt_instr (exp:%lu vs %lu)!\n" , + elf->name,sizeof (*a), + sec->sec->sh_size); +return -EINVAL ; + } + + start = sec->load_addr; + end = sec->load_addr + sec->sec->sh_size; + +for ( a = start; a < end; a++ ) + { +unsigned long instr = (unsigned long )(&a->instr_offset + a->instr_offset); +unsigned long replacement = (unsigned long )(&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: 0x%lx!\n" , + elf->name, instr); +return -EINVAL ; + } + } +apply_alternatives_nocheck (start, end); + } + sec =xsplice_elf_sec_by_name (elf,".ex_table" );if ( sec ) {
@@ -555,6 +588,7 @@ static int prepare_payload(struct payload *payload, region->ex = s; region->ex_end = e; } +#endif return 0 ;
xen/include/asm-x86/alternative.h +4 -0
@@ -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"
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/arch/x86/Makefile +0
-0@@ -6,7 +6,11 @@ subdir-y += mmsubdir-$(CONFIG_XENOPROF) += oprofile
subdir-y +=x86_64xen/arch/x86/Makefile +3
-0+ifdef CONFIG_XSPLICE +obj-y += alternative.o +else
Quoted 2 lines
obj-bin-y += alternative.init.o
+endif
For now this is okay, but please try to use the list model in favor of ifdef-s in the future, whenever possible.
xen/arch/x86/Makefile
Quoted 3 lines
#ifdef
K8_NOP1xen/arch/x86/Makefile +1
-1-static const unsigned char k8nops[] __initconst = { +static const unsigned char k8nops[] __INITCONST = {
>K8_NOP1,
>K8_NOP2,
>K8_NOP3,xen/arch/x86/Makefile static const unsigned char * const k8_nops[ASM_NOP_MAX+1] __initconstrel = {
Quoted 1 line
xen/arch/x86/Makefile +0
-0@@ -52,7 +53,7 @@ static const unsigned char * const k8_nops[ASM_NOP_MAX+1] __initconstrel = {
The context here suggests you didn't complete your conversion work: The __initconstrel hardly can remain unchanged if the objects are now being referenced by objects in non-init sections.
xen/arch/x86/Makefile static void __init add_nops(void *insns, unsigned int len) · line 130
Quoted 7 lines
xen/arch/x86/Makefile +2
-1@@ -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 has never + * been executed.
The second half is misleading - there's no such requirement for early boot patching. The requirement is that the code must not currently be executing (which at that time is guaranteed by there only being a single CPU, which is in a well defined region of code).
xen/arch/x86/Makefile const char *xen_hello_world(void) · line 15
Quoted 2 lines
xen/arch/x86/Makefile +0
-0@@ -12,6 +15,8 @@ const char *xen_hello_world(void) {
>unsigned long tmp;
>int rc;xen/arch/x86/Makefile const char *xen_hello_world(void) · line 17
Quoted 2 lines
xen/arch/x86/Makefile +2
-0+ + alternative(ASM_NOP1, ASM_NOP1, X86_FEATURE_LM);
So to cover the issue pointed out above you should be patching a longer instruction with a shorter one here.
xen/common/xsplice.c static int prepare_payload(struct payload *payload,
Quoted 3 lines
xen/common/xsplice.c +0
-0@@ -533,6 +533,39 @@ static int prepare_payload(struct payload *payload,
>}
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 541
Quoted 8 lines
xen/common/xsplice.c +6
-0#ifndef CONFIG_ARM + sec =xsplice_elf_sec_by_name (elf,".altinstructions" ); +if ( sec ) + { +struct alt_instr *a, *start, *end; + + if ( !sec->sec->sh_size ||
Once again I don't see what harm a zero size section would do.
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 545
Quoted 4 lines
xen/common/xsplice.c +4
-0+ (sec->sec->sh_size % sizeof(*a)) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .alt_instr (exp:%lu vs %lu)!\n", + elf->name, sizeof(*a),
sizeof() wants to be formatted with %zu and ...
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 546
Quoted 1 line
xen/common/xsplice.c +1
-0+ sec->sec->sh_size);
... didn't you add an ELF-specific format macro for cases like this?
Also if you printed the message outside of the conditional, expected and actual values would likely disagree anyway (as soon as there are multiple entries), so some editing of the text would be nice too.
Quoted 5 lines
xen/common/xsplice.c +5
-0+return -EINVAL ; + } + + start = sec->load_addr; + end = sec->load_addr + sec->sec->sh_size;
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 561
Quoted 10 lines
xen/common/xsplice.c +10
-0+ +for ( a = start; a < end; a++ ) + { +unsigned long instr = (unsigned long )(&a->instr_offset + a->instr_offset); +unsigned long replacement = (unsigned long )(&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: 0x%lx!\n",
%#lx please
xen/common/xsplice.c static int prepare_payload(struct payload *payload,
Quoted 1 line
xen/common/xsplice.c +0
-0@@ -555,6 +588,7 @@ static int prepare_payload(struct payload *payload,
>region->ex = s;
>region->ex_end = e;
>}xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 592
Quoted 2 lines
xen/common/xsplice.c +1
-0+#endif
Stray addition of a blank line.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/arch/x86/Makefile +0
-0@@ -6,7 +6,11 @@ subdir-y += mmsubdir-$(CONFIG_XENOPROF) += oprofile
subdir-y +=x86_64
Quoted 8 lines
Quoted 5 lines
xen/arch/x86/Makefile +3
-0+ifdef CONFIG_XSPLICE +obj-y += alternative.o +elseobj-bin-y += alternative.init.o
+endifFor now this is okay, but please try to use the list model in favor of ifdef-s in the future, whenever possible.
As in:
obj-$(CONFIG_XSPLICE) += alternative.o xsplice.o
.. but then how do I define an build for alternative.init.o?
ifndef CONFIG_XSPLICE
obj-bin-$(CONFIG_XSPLICE) += alternative.init.o
endif
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:30, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:02 AM >>>
xen/arch/x86/Makefile +0
-0@@ -6,7 +6,11 @@ subdir-y += mmsubdir-$(CONFIG_XENOPROF) += oprofile
subdir-y +=x86_64Quoted 8 lines
Quoted 5 lines
xen/arch/x86/Makefile +3
-0+ifdef CONFIG_XSPLICE +obj-y += alternative.o +elseobj-bin-y += alternative.init.o
+endifFor now this is okay, but please try to use the list model in favor of ifdef-s in the future, whenever possible.
Quoted 8 lines
As in:
obj-$(CONFIG_XSPLICE) += alternative.o xsplice.o.. but then how do I define an build for alternative.init.o?
ifndef
CONFIG_XSPLICE
obj-bin-$(CONFIG_XSPLICE) += alternative.init.o
endif
alternative-y := alternative.init.o
alternative-$(CONFIG_XSPLICE) :=
obj-$(CONFIG_XSPLICE) += alternative.o xsplice.o
obj-bin-y += $(alternative-y)
The main goal with this purely list based approach being that this way it doesn't matter whether CONFIG_XSPLICE is undefined, defined but empty, or CONFIG_XSPLICE=n.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
In reply to Jan BeulichQuoted 11 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload,
Quoted 3 lines
xen/common/xsplice.c +0
-0@@ -533,6 +533,39 @@ static int prepare_payload(struct payload *payload,>}Quoted 7 lines
xen/common/xsplice.c +5
-0#ifndef CONFIG_ARM + sec =xsplice_elf_sec_by_name (elf,".altinstructions" ); +if ( sec ) + { +struct alt_instr *a, *start, *end; +
.. snip..
Quoted 7 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 545
Quoted 4 lines
xen/common/xsplice.c +4
-0+ (sec->sec->sh_size % sizeof(*a)) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .alt_instr (exp:%lu vs %lu)!\n", + elf->name, sizeof(*a),
sizeof()wants to be formatted with %zu and ...
Quoted 7 lines
Quoted 1 line
+
sec->sec->sh_size);... didn't you add an ELF-specific format macro for cases like this?
Also if you printed the message outside of the conditional, expected and actual values would likely disagree anyway (as soon as there are multiple entries), so some editing of the text would be nice too.
Why would I want to print the message outside the conditional? Or did you mean:
dprintk(XENLOG_ERR, XSPLICE "%s: Size of .alt_instr is not multiple of %zu!\n",
elf->name, sizeof(*a));
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
Quoted 25 lines
Quoted 12 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload,
Quoted 4 lines · On 21.04.16 at 02:31, <[email protected]> wrote:
xen/common/xsplice.c +0
-0@@ -533,6 +533,39 @@ static int prepare_payload(struct payload *payload,>}Quoted 7 lines
xen/common/xsplice.c +5
-0#ifndef CONFIG_ARM + sec =xsplice_elf_sec_by_name (elf,".altinstructions" ); +if ( sec ) + { +struct alt_instr *a, *start, *end; +.. snip..
Quoted 3 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload,
Quoted 3 lines
xen/common/xsplice.c +3
-0+ (sec->sec->sh_size % sizeof(*a)) ) + { + dprintk(XENLOG_ERR, XSPLICE "%s: Wrong size of .alt_instr(exp:%lu vs %lu)!\n",
Quoted 8 lines
Quoted 1 line
+
elf->name,sizeof(*a),
sizeof()wants to be formatted with %zu and ...Quoted 1 line
+
sec->sec->sh_size);... didn't you add an ELF-specific format macro for cases like this?
Quoted 8 lines
Quoted 1 line
Also if you printed the message outside of the conditional, expected and
actual
Quoted 1 line
values would likely disagree anyway (as soon as there are multiple entries),
so
Quoted 1 line
some editing of the text would be nice too.
Why would I want to print the message outside the conditional?
You wouldn't want to. This was just mentioned to help explain the issue. But maybe I managed to confuse you.
Quoted 5 lines
Or did you mean:
dprintk(XENLOG_ERR, XSPLICE "%s: Size of .alt_instr is not
multiple of %zu!\n",elf->name, sizeof(*a));
Indeed that's what I meant to get at.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 19/27] xsplice: Add support for alternatives
In reply to Jan BeulichQuoted 9 lines
Quoted 9 lines
xen/common/xsplice.c +9
-0+return -EINVAL ; + } + + start = sec->load_addr; + end = sec->load_addr + sec->sec->sh_size; + +for ( a = start; a < end; a++ ) + { +unsigned long instr = (unsigned long )(&a->instr_offset + a->instr_offset);
Quoted 8 lines
xen/common/xsplice.c static int prepare_payload(struct payload *payload, · line 561
Quoted 6 lines
xen/common/xsplice.c +6
-0+unsigned long replacement = (unsigned long )(&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: 0x%lx!\n",%#lx please
To drop on the casting I changed the instr and replacement to be 'const void *' (and also made region->start, and end be const void * as well) so this instead of being %#lx ended up being %p.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 20/27 build_id: Provide ld-embedded build-ids
In reply to Konrad Rzeszutek WilkThis patch enables the Elf to be built with the build-id and provide in the Xen hypervisor the code to extract it.
The man-page for ld --build-id says it is:
"Request the creation of a ".note.gnu.build-id" ELF note section or a ".build-id" COFF section. The contents of the note are unique bits identifying this linked file. style can be "uuid" to use 128 random bits, "sha1" to use a 160-bit SHA1 hash on the normative parts of the output contents, ..."
One can also retrieve the value of the build-id by doing 'readelf -n xen-syms'.
For EFI builds we re-use the same build-id that the xen-syms was built with.
The version of ld that first implemented --build-id is v2.18. We check for to see if the linker supports the --build-id parameter and if so use it.
For x86 we have two binaries - the xen-syms and the xen - an smaller version with lots of sections removed. To make it possible for readelf -n xen we also modify mkelf32 and xen.lds.S to include the PT_NOTE ELF section.
The EFI binary is more complicated. We only build one type of binary and expanding the amount of sections the EFI binary has to include an .note one is pointless - as there is no concept of PT_NOTE. The best we can do is move this .note in the .rodata section.
Further development wise should move it to .buildid section so that DataDirectory debug data nor CodeView can view it. (The author has no clue what those are).
Note that in earlier patches the linker script had:
__note_gnu_build_id_start = .;
*(.rodata.note.gnu.build-id)
__note_gnu_build_id_end = .;
*(.note)
*(.note.*)
Which meant you could have different ELF notes _outside_ the __note_gnu_build_id_end. However for EFI builds we take the whole .note* section and jam it in the EFI to be between __note_gnu_build_id_start and __note_gnu_build_id_end. To not make this happend we make on the ELF build the section be called .note.gnu.build-id (instead of just .note). If there is a need for a different type of note other folks can add it as a different section name.
Note that we do call --binary-id=sha1 on all linker invocations. We have to do to enforce that the symbol offsets don't changes (the side effect is that we we would have multiple binary ids - except that the last one is the final one).
Without this working the symbol table embedded in Xen ends up incorrect - some of the values it contains would be offset by the size of the included build id.
This obviously causes problems when resolving symbols.
We also define the NT_GNU_BUILD_ID in the elfstructs.h as we need to use it in various places.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v1: Rebase it on Martin's initial patch
v2: Move it to XENVER hypercall
v3: Fix EFI building (Ross's fix)
Don't use the third argument for length.
Use new structure for XENVER_build_id with variable buf. Include Ross's fix.
Include detection of bin-utils for build-id support, add probing for size, and return -EPERM for XSM denied calls. Build xen_build_id under ARM, required adding ELFSIZE in proper file. Rebase on top XSM version class.
v4:
Include the build-id .note in the xen ELF binary. s/build_id/build_id_linker/
For EFI build, moved the --build-id values in .data section Rebase on staging.
Split patch in two. Always do --build-id call. Include the .note in .rodata. USe const void * and ssize_t
Use -S to make build_id.o and objcopy differently (Andrew suggested)
v5: Put back the #ifdef LOCK_PROFILE on ARM. (Bad change). Move the _erodata
around. s/ssize_t/unsigned int/
v6: Redid it per Jan's review.
v7: Move build-id note in .rodata.note for EFI builds only.
Move build-id note in .rodata for EFI builds only. Retain
it in .note. Change name of object file used by EFI builds to notes.o Make on ELF builds the PT_NOTE section name be .note.gnu.build-id and ingest that in ELF build.
Define NT_GNU_BUILD_ID in elfstructs.h
v8: s/num_phdrs/notes_phdrs/
Added Andrew's Reviewed-by
---
--- Config.mk | 11 ++++ xen/arch/arm/Makefile | 2 +- xen/arch/arm/xen.lds.S | 15 ++++- xen/arch/x86/Makefile | 30 ++++++++-- xen/arch/x86/boot/mkelf32.c | 129 ++++++++++++++++++++++++++++++++++++++----- xen/arch/x86/xen.lds.S | 28 ++++++++++ xen/common/version.c | 50 +++++++++++++++++ xen/include/xen/elfstructs.h | 3 + xen/include/xen/version.h | 1 + 9 files changed, 247 insertions(+), 22 deletions(-)
Config.mk +11 -0
@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y) + +export XEN_HAS_BUILD_ID ?= n +ifeq ($(call ld-ver-build-id,$(LD)),n) +build_id_linker := +else +CFLAGS += -DBUILD_ID +build_id_linker := --build-id=sha1 +endif + # as-insn: Check whether assembler supports an instruction. # Usage: cflags-y += $(call as-insn "insn",option-yes,option-no) as-insn = $(if $(shell echo 'void _(void) { asm volatile ( $(2) ); }' \
xen/arch/arm/Makefile +1 -1
@@ -92,7 +92,7 @@ $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o $(NM) -pa --format=sysv $(@D)/.$(@F).1 \ | $(BASEDIR)/tools/symbols --sysv --sort >$(@D)/.$(@F).1.S $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1.o - $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ + $(LD) $(LDFLAGS) -T xen.lds -N prelink.o $(build_id_linker) \ $(@D)/.$(@F).1.o -o $@ rm -f $(@D)/.$(@F).[0-9]*
xen/arch/arm/xen.lds.S +13 -2
@@ -22,6 +22,9 @@ OUTPUT_ARCH(FORMAT) PHDRS { text PT_LOAD /* XXX should be AT ( XEN_PHYS_START ) */ ; +#if defined(BUILD_ID) + note PT_NOTE ; +#endif } SECTIONS {
@@ -57,10 +60,18 @@ SECTIONS *(.lockprofile.data) __lock_profile_end = .; #endif - - _erodata = .; /* End of read-only data */ } :text +#if defined(BUILD_ID) + . = ALIGN(4); + .note.gnu.build-id : { + __note_gnu_build_id_start = .; + *(.note.gnu.build-id) + __note_gnu_build_id_end = .; + } :note :text +#endif + _erodata = .; /* End of read-only data */ + .data : { /* Data */ . = ALIGN(PAGE_SIZE); *(.data.page_aligned)
xen/arch/x86/Makefile +25 -5
@@ -76,6 +76,12 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ -O $(BASEDIR)/include/xen/compile.h ]; then \ echo '$(TARGET).efi'; fi) +ifneq ($(build_id_linker),) +notes_phdrs = --notes +else +notes_phdrs = +endif + ifdef CONFIG_XSPLICE all_symbols = --all-symbols ifdef CONFIG_FAST_SYMBOL_LOOKUP
@@ -87,7 +93,8 @@ endif $(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/'` + `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` \ + $(notes_phdrs) .PHONY: tests tests:
@@ -121,22 +128,28 @@ $(BASEDIR)/common/symbols-dummy.o: $(MAKE) -f $(BASEDIR)/Rules.mk -C $(BASEDIR)/common symbols-dummy.o $(TARGET)-syms: prelink.o xen.lds $(BASEDIR)/common/symbols-dummy.o - $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ + $(LD) $(LDFLAGS) -T xen.lds -N prelink.o $(build_id_linker) \ $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0 $(NM) -pa --format=sysv $(@D)/.$(@F).0 \ | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort \ >$(@D)/.$(@F).0.S $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0.o - $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ + $(LD) $(LDFLAGS) -T xen.lds -N prelink.o $(build_id_linker) \ $(@D)/.$(@F).0.o -o $(@D)/.$(@F).1 $(NM) -pa --format=sysv $(@D)/.$(@F).1 \ | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort --warn-dup \ >$(@D)/.$(@F).1.S $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1.o - $(LD) $(LDFLAGS) -T xen.lds -N prelink.o \ + $(LD) $(LDFLAGS) -T xen.lds -N prelink.o $(build_id_linker) \ $(@D)/.$(@F).1.o -o $@ rm -f $(@D)/.$(@F).[0-9]* +note.o: $(TARGET)-syms + $(OBJCOPY) -O binary --only-section=.note.gnu.build-id $(BASEDIR)/xen-syms [email protected] + $(OBJCOPY) -I binary -O elf64-x86-64 -B i386:x86-64 \ + --rename-section=.data=.note.gnu.build-id -S [email protected] $@ + rm -f [email protected] + EFI_LDFLAGS = $(patsubst -m%,-mi386pep,$(LDFLAGS)) --subsystem=10 EFI_LDFLAGS += --image-base=$(1) --stack=0,0 --heap=0,0 --strip-debug EFI_LDFLAGS += --section-alignment=0x200000 --file-alignment=0x20
@@ -149,6 +162,13 @@ $(TARGET).efi: VIRT_BASE = 0x$(shell $(NM) efi/relocs-dummy.o | sed -n 's, A VIR $(TARGET).efi: ALT_BASE = 0x$(shell $(NM) efi/relocs-dummy.o | sed -n 's, A ALT_START$$,,p') # Don't use $(wildcard ...) here - at least make 3.80 expands this too early! $(TARGET).efi: guard = $(if $(shell echo efi/dis* | grep disabled),:) +ifneq ($(build_id_linker),) +$(TARGET).efi: note.o +note_file := note.o +else +note_file := +endif + $(TARGET).efi: prelink-efi.o efi.lds efi/relocs-dummy.o $(BASEDIR)/common/symbols-dummy.o efi/mkreloc $(foreach base, $(VIRT_BASE) $(ALT_BASE), \ $(guard) $(LD) $(call EFI_LDFLAGS,$(base)) -T efi.lds -N $< efi/relocs-dummy.o \
@@ -165,7 +185,7 @@ $(TARGET).efi: prelink-efi.o efi.lds efi/relocs-dummy.o $(BASEDIR)/common/symbol | $(guard) $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort >$(@D)/.$(@F).1s.S $(guard) $(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o $(guard) $(LD) $(call EFI_LDFLAGS,$(VIRT_BASE)) -T efi.lds -N $< \ - $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o -o $@ + $(@D)/.$(@F).1r.o $(@D)/.$(@F).1s.o $(note_file) -o $@ if $(guard) false; then rm -f $@; echo 'EFI support disabled'; fi rm -f $(@D)/.$(@F).[0-9]*
xen/arch/x86/boot/mkelf32.c +115 -14
205 lines not on this page · raw message
xen/arch/x86/xen.lds.S +28 -0
@@ -31,6 +31,9 @@ OUTPUT_ARCH(i386:x86-64) PHDRS { text PT_LOAD ; +#if defined(BUILD_ID) && !defined(EFI) + note PT_NOTE ; +#endif } SECTIONS {
@@ -79,6 +82,16 @@ SECTIONS *(.rodata) *(.rodata.*) +#if defined(BUILD_ID) && defined(EFI) +/* + * No mechanism to put an PT_NOTE in the EFI file - so put + * it in .rodata section. (notes.o supplies us with .note.gnu.build-id). + */ + . = ALIGN(4); + __note_gnu_build_id_start = .; + *(.note.gnu.build-id) + __note_gnu_build_id_end = .; +#endif . = ALIGN(8); /* Exception table */ __start___ex_table = .;
@@ -99,6 +112,21 @@ SECTIONS _erodata = .; } :text +#if defined(BUILD_ID) && !defined(EFI) +/* + * What a strange section name. The reason is that on ELF builds this section + * is extracted to notes.o (which then is ingested in the EFI file). But the + * compiler may want to inject other things in the .note which we don't care + * about - hence this unique name. + */ + . = ALIGN(4); + .note.gnu.build-id : { + __note_gnu_build_id_start = .; + *(.note.gnu.build-id) + __note_gnu_build_id_end = .; + } :note :text +#endif + #ifdef EFI . = ALIGN(MB(2)); #else
xen/common/version.c +50 -0
60 lines not on this page · raw message
xen/include/xen/elfstructs.h +3 -0
@@ -40,6 +40,9 @@ typedef uint32_t Elf64_Word;typedef int64_t Elf64_Sxword ;typedef uint64_t Elf64_Xword ; +/* Unique build id string format when using --build-id. */ +#define NT_GNU_BUILD_ID 3 +/* * e_ident[] identification indexes * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
xen/include/xen/version.h +1 -0
@@ -13,5 +13,6 @@ const char *xen_extra_version(void);const char *xen_changeset (void );const char *xen_banner (void );const char *xen_deny (void ); +int xen_build_id (const void **p,unsigned int *len);#endif /* __XEN_VERSION_H__ */
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 20/27] build_id: Provide ld-embedded build-ids
Config.mk endef · line 130
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Config.mk +2
-0@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y)
What about localized binutils?
Quoted 3 lines
Config.mk +0
-0@@ -87,7 +93,8 @@ endif$(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32
>./boot/mkelf32 $(TARGET)-syms $(TARGET) 0x100000 \
Config.mk
Quoted 3 lines
Config.mk +2
-1- `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` + `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` \ + $(notes_phdrs)
The placement makes it somewhat non-obvious that this is an option passed
to mkelf32 - it'd probably better go on the first line.
Config.mk
Quoted 5 lines
Config.mk +5
-0+note.o: $(TARGET)-syms + $(OBJCOPY) -O binary --only-section=.note.gnu.build-id $(BASEDIR)/xen-syms [email protected] + $(OBJCOPY) -I binary -O elf64-x86-64 -B i386:x86-64 \ + --rename-section=.data=.note.gnu.build-id -S [email protected] $@ + rm -f [email protected]
This, btw, should be accompanied by an adjustment to the clean target, for the case where a build gets interrupted before the rule above manages to delete the intermediate file.
Config.mk SECTIONS
Quoted 1 line
Config.mk +0
-0@@ -99,6 +112,21 @@ SECTIONS
>_erodata = .;
Quoted 6 lines
Config.mk +4
-0} :text +#if defined(BUILD_ID) && !defined(EFI) +/* + * What a strange section name. The reason is that on ELF builds this section + * is extracted to notes.o (which then is ingested in the EFI file). But the
Config.mk SECTIONS · line 127
Quoted 10 lines
Config.mk +10
-0+ * compiler may want to inject other things in the .note which we don't care + * about - hence this unique name. + */ + . = ALIGN(4); + .note.gnu.build-id : { + __note_gnu_build_id_start = .; + *(.note.gnu.build-id) + __note_gnu_build_id_end = .; + } :note :text +#endif
Am I misremembering that we had agreed for this to live ahead of _erodata?
Config.mk const char *xen_deny(void)
Quoted 1 line
Config.mk +0
-0@@ -61,6 +66,51 @@ const char *xen_deny(void)
>return "<denied>";
Config.mk const char *xen_deny(void) · line 69
Quoted 4 lines
Config.mk +2
-0} +static const void *build_id_p; +static unsigned int build_id_len;
__read_mostly? Also these variables are kind of pointless to have in a build-id- less build. But I guess that's going to be rare anyway.
Anyway,
with or without any of the above suggestions taken care of.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 20/27] build_id: Provide ld-embedded build-ids
Quoted 11 lines · Jan Beulich
Config.mk endef · line 130
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Config.mk +2
-0@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y)What about localized binutils?
I couldn't find the localized entries for binutils, but that may change in the future.
Do you think it would be better to go back to the version check?
Or look at this at some pointer later - since you mentioned you were OK with giving an Ack even as is?
Quoted 11 lines
Quoted 4 lines
Config.mk +0
-0@@ -87,7 +93,8 @@ endif$(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32
>./boot/mkelf32 $(TARGET)-syms $(TARGET)
0x100000\Config.mk
Quoted 3 lines
Config.mk +2
-1- `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` + `$(NM) -nr $(TARGET)-syms | head -n 1 | sed -e 's/^\([^ ]*\).*/0x\1/'` \ + $(notes_phdrs)The placement makes it somewhat non-obvious that this is an option passed
to mkelf32 - it'd probably better go on the first line.
Sure thing.
..s nip..
Quoted 8 lines
Quoted 8 lines
Config.mk +8
-0+#if defined(BUILD_ID) && !defined(EFI) +/* + * What a strange section name. The reason is that on ELF builds this section + * is extracted to notes.o (which then is ingested in the EFI file). But the + * compiler may want to inject other things in the .note which we don't care + * about - hence this unique name. + */ + . = ALIGN(4);
Quoted 8 lines
Config.mk
Quoted 6 lines
Config.mk +6
-0+ .note.gnu.build-id : { + __note_gnu_build_id_start = .; + *(.note.gnu.build-id) + __note_gnu_build_id_end = .; + } :note :text +#endifAm I misremembering that we had agreed for this to live ahead of
_erodata?
No, I misremebered! Back in.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 20/27] build_id: Provide ld-embedded build-ids
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:33, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Config.mk endef · line 130
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Config.mk +2
-0@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y)What about localized binutils?
Quoted 4 lines
I couldn't find the localized entries for binutils, but that may change in the future.
Do you think it would be better to go back to the version check?
No, definitely not. Just that you shouldn't look at textual output imo, but at the linker actually groking the option.
Quoted 2 lines
Or look at this at some pointer later - since you mentioned you were OK with giving an Ack even as is?
Yes, looking at this later is fine, as long as it doesn't get forgotten.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 20/27] build_id: Provide ld-embedded build-ids
Quoted 19 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:33, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Config.mk endef · line 130
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Config.mk +2
-0@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y)What about localized binutils?
Quoted 4 lines
I couldn't find the localized entries for binutils, but that may change in the future.
Do you think it would be better to go back to the version check?
Quoted 7 lines
No, definitely not. Just that you shouldn't look at textual output imo, but at the linker actually groking the option.
Quoted 2 lines
Or look at this at some pointer later - since you mentioned you were OK with giving an Ack even as is?
Yes, looking at this later is fine, as long as it doesn't get forgotten.
Added it on http://wiki.xen.org/wiki/XSplice#Hypervisor so I will not forget.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 20/27] build_id: Provide ld-embedded build-ids
In reply to Jan BeulichQuoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.04.16 at 02:33, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Config.mk endef · line 130
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:03 AM >>>
Config.mk +2
-0@@ -126,6 +126,17 @@ endef check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) +ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ + grep -q unrecognized && echo n || echo y)What about localized binutils?
Quoted 8 lines
Quoted 4 lines
I couldn't find the localized entries for binutils, but that may change in the future.
Do you think it would be better to go back to the version check?
No, definitely not. Just that you shouldn't look at textual output imo, but at the linker actually groking the option.
Which in retrospective is easy:
Config.mk +1 -1
@@ -127,7 +127,7 @@ check-$(gcc) = $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1") $(eval $(check-y)) ld-ver-build-id = $(shell $(1) --build-id 2>&1 | \ - grep -q unrecognized && echo n || echo y) + grep -q build-id && echo n || echo y) export XEN_HAS_BUILD_ID ?= n ifeq ($(call ld-ver-build-id,$(LD)),n)
[konrad@localhost xen]$
Quoted 7 lines
Quoted 2 lines
Or look at this at some pointer later - since you mentioned you were OK with giving an Ack even as is?
Yes, looking at this later is fine, as long as it doesn't get forgotten.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH 21/27 xsplice: Print build_id in keyhandler and on bootup.
In reply to Konrad Rzeszutek WilkAs it should be an useful debug mechanism.
--
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
v2: s/char */const void *
v5: s/ssize_t/unsigned int/
v6: Remove pointless initializers, use string literal instead of %s,
add Jan's Ack.
v7: Add Andrew's Reviewed-by
---
--- xen/common/xsplice.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
xen/common/xsplice.c +12 -0
@@ -16,6 +16,7 @@#include <xen/spinlock.h> #include <xen/string.h> #include <xen/symbols.h> +#include <xen/version.h> #include <xen/virtual_region.h> #include <xen/vmap.h> #include <xen/wait.h>
@@ -1333,10 +1334,15 @@ static const char *state2str(uint32_t state)static void xsplice_printall (unsigned char key) {struct payload *data; +const void *binary_id =NULL ; +unsigned int len =0 ;unsigned int i;printk ("'%u' pressed - Dumping all xsplice patches\n" , key); +if ( !xen_build_id (&binary_id, &len) ) +printk ("build-id: %*phN\n" , len, binary_id); +if ( !spin_trylock (&payload_lock) ) {printk ("Lock held. Try again.\n" );
@@ -1369,6 +1375,9 @@ static void xsplice_printall(unsigned char key)static int __init xsplice_init (void ) { +const void *binary_id; +unsigned int len; +BUILD_BUG_ON (sizeof (struct xsplice_patch_func ) !=sizeof (struct xsplice_patch_func_internal ) ); BUILD_BUG_ON( sizeof(struct xsplice_patch_func_internal) !=
@@ -1381,6 +1390,9 @@ static int __init xsplice_init(void)BUILD_BUG_ON ( offsetof(struct xsplice_patch_func , new_addr) !=4 );BUILD_BUG_ON ( offsetof(struct xsplice_patch_func , new_size) !=12 );#endif +if ( !xen_build_id (&binary_id, &len) ) + printk(XENLOG_INFO XSPLICE ": build-id: %*phN\n", len, binary_id); +register_keyhandler ('x' , xsplice_printall,"print xsplicing info" ,1 );arch_xsplice_init ();
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 22/27 XENVER_build_id/libxc: Provide ld-embedded build-id
In reply to Konrad Rzeszutek WilkIf the hypervisor was built with build-ids we can expose the build-id value to the toolstack (if it is not built with it will just return -ENODATA). This is a priviligied operation so only the controlling stack is able to request this.
---
CC: Daniel De Graaf <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
v1: Rebase it on Martin's initial patch
v2: Move it to XENVER hypercall
v3: Don't use the third argument for length.
- Use new structure for XENVER_build_id with variable buf.
v8: Resurrected from v3!
---
--- tools/flask/policy/policy/modules/xen/xen.te | 1 + tools/libxc/xc_private.c | 7 ++++++ tools/libxc/xc_private.h | 11 +++++++++ xen/common/kernel.c | 36 ++++++++++++++++++++++++++++ xen/include/public/version.h | 18 +++++++++++++- xen/xsm/flask/hooks.c | 3 +++ xen/xsm/flask/policy/access_vectors | 2 ++ 7 files changed, 77 insertions(+), 1 deletion(-)
tools/flask/policy/policy/modules/xen/xen.te +1 -0
@@ -82,6 +82,7 @@ allow dom0_t xen_t:xen2 { allow dom0_t xen_t:version { xen_extraversion xen_compile_info xen_capabilities xen_changeset xen_pagesize xen_guest_handle xen_commandline + xen_build_id }; allow dom0_t xen_t:mmu memorymap;
tools/libxc/xc_private.c +7 -0
@@ -495,6 +495,13 @@ int xc_version(xc_interface *xch, int cmd, void *arg)case XENVER_commandline: sz =sizeof (xen_commandline_t);break ; +case XENVER_build_id: + { +xen_build_id_t *build_id = (xen_build_id_t *)arg; + sz =sizeof (*build_id) + build_id->len; +HYPERCALL_BOUNCE_SET_DIR (arg,XC_HYPERCALL_BUFFER_BOUNCE_BOTH ); +break ; + }default :ERROR ("xc_version: unknown command %d\n" , cmd);return -EINVAL ;
tools/libxc/xc_private.h +11 -0
@@ -197,6 +197,17 @@ enum {#define HYPERCALL_BOUNCE_SET_SIZE (_buf, _sz) do { (HYPERCALL_BUFFER(_buf))->sz = _sz; } while (0)/* +* Change the direction. +* +* Can only be used if the bounce_pre/bounce_post commands have +* not been used. +*/ +#define HYPERCALL_BOUNCE_SET_DIR (_buf, _dir) do { if ((HYPERCALL_BUFFER(_buf))->hbuf) \ + assert(1); \ + (HYPERCALL_BUFFER(_buf))->dir = _dir; \ + } while (0) + +/* * Initialise and free hypercall safe memory. Takes care of any required * copying. */
xen/common/kernel.c +36 -0
@@ -376,6 +376,42 @@ DO(xen_version)(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)return -EFAULT ;return 0 ; } + +case XENVER_build_id: + { +xen_build_id_t build_id; +unsigned int sz =0 ; +int rc =0 ; +const void *p =NULL ; + +if ( deny ) +return -EPERM ; + +/* Only return size. */ +if ( !guest_handle_is_null (arg) ) + { +if (copy_from_guest (&build_id, arg,1 ) ) +return -EFAULT ; + +if ( build_id.len ==0 ) +return -EINVAL ; + } + + rc =xen_build_id (&p, &sz); +if ( rc ) +return rc; + +if (guest_handle_is_null (arg) ) +return sz; + +if ( sz > build_id.len ) +return -ENOBUFS ; + +if (copy_to_guest_offset (arg, offsetof(xen_build_id_t , buf), p, sz) ) +return -EFAULT ; + +return sz; + } }return -ENOSYS ;
xen/include/public/version.h +17 -1
@@ -30,7 +30,8 @@#include "xen.h" -/* NB. All ops return zero on success, except XENVER_{version,pagesize} */ +/* NB. All ops return zero on success, except XENVER_{version,pagesize} +* XENVER_{version,pagesize,build_id} */ /* arg == NULL; returns major:minor (16:16). */ #define XENVER_version 0
@@ -87,6 +88,21 @@ typedef struct xen_feature_info xen_feature_info_t;#define XENVER_commandline 9typedef char xen_commandline_t [1024 ]; +/* +* Return value is the number of bytes written, or XEN_Exx on error. +* Calling with empty parameter returns the size of build_id. +*/ +#define XENVER_build_id 10 +struct xen_build_id { +uint32_t len;/* IN: size of buf[]. */ +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >=199901L +unsigned char buf[]; +#elif defined(__GNUC__) +unsigned char buf[1 ];/* OUT: Variable length buffer with build_id. */ +#endif +}; +typedef struct xen_build_id xen_build_id_t ; +#endif /* __XEN_PUBLIC_VERSION_H__ */ /*
xen/xsm/flask/hooks.c +3 -0
@@ -1663,6 +1663,9 @@ static int flask_xen_version (uint32_t op)case XENVER_commandline:return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION ,VERSION__XEN_COMMANDLINE ,NULL ); +case XENVER_build_id: +return avc_has_perm (dsid,SECINITSID_XEN ,SECCLASS_VERSION , +VERSION__XEN_BUILD_ID ,NULL );default :return -EPERM ; }
xen/xsm/flask/policy/access_vectors +2 -0
@@ -525,4 +525,6 @@ class version xen_guest_handle # Xen command line. xen_commandline +# Xen build id + xen_build_id }
Signature · 7 lines
2.5.0 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [PATCH v8.1 22/27] XENVER_build_id/libxc: Provide ld-embedded build-id
Quoted 10 lines · Konrad Rzeszutek Wilk
If the hypervisor was built with build-ids we can expose the build-id value to the toolstack (if it is not built with it will just return
-ENODATA). This is a priviligied operation so only the controlling stack is able to request this.Signed-off-by Konrad Rzeszutek Wilk <[email protected]>---
CC: Daniel De Graaf <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 22/27] XENVER_build_id/libxc: Provide ld-embedded build-id
In reply to Konrad Rzeszutek WilkQuoted 6 lines · Konrad Rzeszutek Wilk
If the hypervisor was built with build-ids we can expose the build-id value to the toolstack (if it is not built with it will just return
-ENODATA). This is a priviligied operation so only the controlling stack is able to request this.Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v8.1 22/27] XENVER_build_id/libxc: Provide ld-embedded build-id
In reply to Konrad Rzeszutek Wilkxen/common/kernel.c DO(xen_version)(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 04/14/16 12:04 AM >>>
xen/common/kernel.c +0
-0@@ -376,6 +376,42 @@ DO(xen_version)(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
>return -EFAULT;
>return 0;
>}xen/common/kernel.c DO(xen_version)(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 385
Quoted 7 lines
xen/common/kernel.c +7
-0+ +case XENVER_build_id: + { +xen_build_id_t build_id; +unsigned int sz =0 ; +int rc =0 ; +const void *p =NULL ;
All three initializers seem to be pointless.
With that taken care of,
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel