[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH RFC V6 4/5] xen, libxc: Request page fault injection via libxc
Added new XEN_DOMCTL_set_pagefault_info hypercall, used by libxc's new xc_domain_set_pagefault_info() function to set per-domain page fault injection information. All a call does is set per-domain info, and nothing actually happens until VMENTRY time, and then only if all conditions are met (the guest is in user mode, the set value matches CR3, and there are no other pending traps). This mechanism allows bringing in swapped-out pages for inspection. Changes since V2: - Removed superfluous memset(&ctxt, 0, sizeof(struct hvm_hw_cpu)). - Now checking SS.DPL instead of CS.DPL to see if the guest is in user mode. - Removed superfluous fault_info members initialization. - Now returning -EINVAL if !has_hvm_container_domain(d) on XEN_DOMCTL_set_pagefault_info. - Moved struct fault_info from struct domain to struct hvm_domain. - Collapsed explicit initialization of the fault_info members into a one-line assignment on the tools/libxc side. - Now using write_access as a proper bool instead of shifting and OR-ing it with PFEC_user_mode. Changes since V3: - Now checking that is_hvm_domain(d) in do_domctl() (consistent with the test in the actual page fault injection code). - XEN_DOMCTL_set_pagefault_info now extends the proper range. - Added a likely() around the d->arch.hvm_domain.fault_info.virtual_address == 0 check. - Replaced hvm_funcs.save_cpu_ctxt() function call with lighter code. - Split the vmx.c page fault injection function into a check function and an inject function. Changes since V4: - Added "valid" flag instead of relying on virtual address 0 as a special case. - Using #defines instead of magic constants in vmx_vmcs_save(). - Padded struct xen_domctl_set_pagefault_info. - Coding style and readability changes. Changes since V5: - Renamed "struct domain *d" to "currd" where appropriate. - Using ! to test bool_t variables. - Replaced hvm_get_segment_register() with vmx_get_segment_register() in vmx.c. - Removed double blank line. - Proper comparison of CR3 and address_space (taking into account LM, PAE). - Using domain variable already set up in do_domctl(). - Removed optional parentheses. - Removed d->arch.hvm_domain.fault_info reset (only the "valid" flag counts anyway). Signed-off-by: Razvan Cojocaru <rcojocaru@xxxxxxxxxxxxxxx> --- tools/libxc/xc_domain.c | 16 +++++++++++ tools/libxc/xenctrl.h | 4 +++ xen/arch/x86/hvm/vmx/vmx.c | 59 ++++++++++++++++++++++++++++++++++++++ xen/common/domctl.c | 19 ++++++++++++ xen/include/asm-x86/hvm/domain.h | 8 ++++++ xen/include/public/domctl.h | 15 ++++++++++ 6 files changed, 121 insertions(+) diff --git a/tools/libxc/xc_domain.c b/tools/libxc/xc_domain.c index 0230c6c..49f0c61 100644 --- a/tools/libxc/xc_domain.c +++ b/tools/libxc/xc_domain.c @@ -506,6 +506,22 @@ int xc_domain_hvm_setcontext(xc_interface *xch, return ret; } +int xc_domain_set_pagefault_info(xc_interface *xch, + uint32_t domid, + xen_domctl_set_pagefault_info_t *info) +{ + DECLARE_DOMCTL; + + if (info == NULL) + return -1; + + domctl.cmd = XEN_DOMCTL_set_pagefault_info; + domctl.domain = (domid_t)domid; + domctl.u.set_pagefault_info = *info; + + return do_domctl(xch, &domctl); +} + int xc_vcpu_getcontext(xc_interface *xch, uint32_t domid, uint32_t vcpu, diff --git a/tools/libxc/xenctrl.h b/tools/libxc/xenctrl.h index f7785ec..85273b9 100644 --- a/tools/libxc/xenctrl.h +++ b/tools/libxc/xenctrl.h @@ -803,6 +803,10 @@ int xc_domain_hvm_setcontext(xc_interface *xch, const char *xc_domain_get_native_protocol(xc_interface *xch, uint32_t domid); +int xc_domain_set_pagefault_info(xc_interface *xch, + uint32_t domid, + xen_domctl_set_pagefault_info_t *info); + /** * This function returns information about the execution context of a * particular vcpu of a domain. diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 4cab482..4dba062 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -3115,6 +3115,62 @@ out: nvmx_idtv_handling(); } +static bool_t vmx_check_pf_injection(void) +{ + struct vcpu *curr = current; + const struct domain *currd = curr->domain; + struct segment_register seg; + unsigned long ev; + uint32_t pending_event = 0; + uint64_t mask; + + if ( !is_hvm_domain(currd) || + likely(!currd->arch.hvm_domain.fault_info.valid) ) + return 0; + + vmx_get_segment_register(curr, x86_seg_ss, &seg); + + if ( seg.attr.fields.dpl != 3 ) /* Guest is not in user mode */ + return 0; + + if ( hvm_long_mode_enabled(curr) ) + mask = 0x000ffffffffff000; /* Bits 51:12. */ + else if ( hvm_pae_enabled(curr) ) + mask = 0x00000000ffffffe0; /* Bits 31:5. */ + else + mask = 0x00000000fffff000; /* Bits 31:12. */ + + if ( (curr->arch.hvm_vcpu.guest_cr[3] & mask) != + (currd->arch.hvm_domain.fault_info.address_space & mask) ) + return 0; + + vmx_vmcs_enter(curr); + __vmread(VM_ENTRY_INTR_INFO, &ev); + + if ( (ev & INTR_INFO_VALID_MASK) && + hvm_event_needs_reinjection(MASK_EXTR(ev, INTR_INFO_INTR_TYPE_MASK), + ev & INTR_INFO_VECTOR_MASK) ) + pending_event = ev; + + vmx_vmcs_exit(curr); + return pending_event == 0; +} + +static void vmx_inject_pf(void) +{ + struct vcpu *curr = current; + struct domain *currd = curr->domain; + int errcode = PFEC_user_mode; + uint64_t virtual_address = currd->arch.hvm_domain.fault_info.virtual_address; + + currd->arch.hvm_domain.fault_info.valid = 0; + + if ( currd->arch.hvm_domain.fault_info.write_access ) + errcode |= PFEC_write_access; + + hvm_inject_page_fault(errcode, virtual_address); +} + void vmx_vmenter_helper(const struct cpu_user_regs *regs) { struct vcpu *curr = current; @@ -3155,6 +3211,9 @@ void vmx_vmenter_helper(const struct cpu_user_regs *regs) if ( unlikely(need_flush) ) vpid_sync_all(); + if ( vmx_check_pf_injection() ) + vmx_inject_pf(); + out: HVMTRACE_ND(VMENTRY, 0, 1/*cycles*/, 0, 0, 0, 0, 0, 0, 0); diff --git a/xen/common/domctl.c b/xen/common/domctl.c index c326aba..b56bd69 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -967,6 +967,25 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) } break; + case XEN_DOMCTL_set_pagefault_info: + { + ret = -EINVAL; + + if ( is_hvm_domain(d) ) + { + d->arch.hvm_domain.fault_info.address_space = + op->u.set_pagefault_info.address_space; + d->arch.hvm_domain.fault_info.virtual_address = + op->u.set_pagefault_info.virtual_address; + d->arch.hvm_domain.fault_info.write_access = + op->u.set_pagefault_info.write_access; + d->arch.hvm_domain.fault_info.valid = 1; + + ret = 0; + } + } + break; + default: ret = arch_do_domctl(op, d, u_domctl); break; diff --git a/xen/include/asm-x86/hvm/domain.h b/xen/include/asm-x86/hvm/domain.h index 947b061..f6d61ce 100644 --- a/xen/include/asm-x86/hvm/domain.h +++ b/xen/include/asm-x86/hvm/domain.h @@ -141,6 +141,14 @@ struct hvm_domain { */ uint64_t sync_tsc; + /* Memory introspection page fault injection data. */ + struct { + uint64_t address_space; + uint64_t virtual_address; + uint32_t write_access; + bool_t valid; + } fault_info; + union { struct vmx_domain vmx; struct svm_domain svm; diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index a3431ec..86b0de6 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -937,6 +937,19 @@ typedef struct xen_domctl_vcpu_msrs xen_domctl_vcpu_msrs_t; DEFINE_XEN_GUEST_HANDLE(xen_domctl_vcpu_msrs_t); #endif +/* XEN_DOMCTL_set_pagefault_info requests that a page fault occur at + * the next VMENTRY. + */ +struct xen_domctl_set_pagefault_info { + uint64_t address_space; + uint64_t virtual_address; + uint32_t write_access; + uint32_t _pad; +}; +typedef struct xen_domctl_set_pagefault_info xen_domctl_set_pagefault_info_t; +DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_pagefault_info_t); + + struct xen_domctl { uint32_t cmd; #define XEN_DOMCTL_createdomain 1 @@ -1009,6 +1022,7 @@ struct xen_domctl { #define XEN_DOMCTL_cacheflush 71 #define XEN_DOMCTL_get_vcpu_msrs 72 #define XEN_DOMCTL_set_vcpu_msrs 73 +#define XEN_DOMCTL_set_pagefault_info 74 #define XEN_DOMCTL_gdbsx_guestmemio 1000 #define XEN_DOMCTL_gdbsx_pausevcpu 1001 #define XEN_DOMCTL_gdbsx_unpausevcpu 1002 @@ -1069,6 +1083,7 @@ struct xen_domctl { struct xen_domctl_cacheflush cacheflush; struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu; struct xen_domctl_gdbsx_domstatus gdbsx_domstatus; + struct xen_domctl_set_pagefault_info set_pagefault_info; uint8_t pad[128]; } u; }; -- 1.7.9.5 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |