From patchwork Thu Nov 10 08:35:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Razvan Cojocaru X-Patchwork-Id: 9420731 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B7EF060484 for ; Thu, 10 Nov 2016 08:38:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AAEA12956F for ; Thu, 10 Nov 2016 08:38:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9E29829563; Thu, 10 Nov 2016 08:38:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7F6B829563 for ; Thu, 10 Nov 2016 08:38:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c4kq0-00014y-8U; Thu, 10 Nov 2016 08:36:00 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c4kpz-00014s-9Q for xen-devel@lists.xen.org; Thu, 10 Nov 2016 08:35:59 +0000 Received: from [85.158.137.68] by server-4.bemta-3.messagelabs.com id F6/08-20885-E6134285; Thu, 10 Nov 2016 08:35:58 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprPIsWRWlGSWpSXmKPExsUSfTxjoW6uoUq EwbOV4hZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8aTxzPYC/akVBxvOsDWwHgloIuRk0NIwENi /q37zF2MXED2WkaJN0v/skAkrjJKrGxKginaNPkmVNF+Rol/q7YzgSTYBAwlVm9sYQOxRQSkJ a59vswIUsQsMIlJYuXy26wgCWEBX4nDfxvYQWwWAVWJvf9+gW3gFfCUuLTsIlhcQkBO4uSxya wQdo7E4SXPgbZxANlSEv9blUBmSgisZ5G49W0fVL2MxKOJN9kmMAosYGRYxahenFpUllqka6q XVJSZnlGSm5iZo2toYKyXm1pcnJiempOYVKyXnJ+7iREYWPUMDIw7GC9/dTrEKMnBpCTKK35P OUKILyk/pTIjsTgjvqg0J7X4EKMMB4eSBC+DgUqEkGBRanpqRVpmDjDEYdISHDxKIrzn9YHSv MUFibnFmekQqVOMuhxvdr18wCTEkpeflyolzvsEpEgApCijNA9uBCzeLjHKSgnzMjIwMAjxFK QW5WaWoMq/YhTnYFQS5r0FMoUnM68EbtMroCOYgI6oilEAOaIkESEl1cBoa3XqiEm23jcB9Tn KV6OqT1cvPWaxNOjvoR++6x2UfrHwmd4pMpSY4GL+Z5+Sw5+J2y/uzJ1zZlEqd8Ii3uv31vZ9 WnlZ43zW8riVjvd1TuXYPjjJ+j5bdrvx3hsZ1V1ucx7/afAXOWFRmuos27X+raBW/ovum/b+P hfUtx78zTD3W9mRjXFuSizFGYmGWsxFxYkAmrBFhbICAAA= X-Env-Sender: rcojocaru@bitdefender.com X-Msg-Ref: server-2.tower-31.messagelabs.com!1478766956!58517502!1 X-Originating-IP: [91.199.104.161] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 45662 invoked from network); 10 Nov 2016 08:35:56 -0000 Received: from mx01.bbu.dsd.mx.bitdefender.com (HELO mx01.bbu.dsd.mx.bitdefender.com) (91.199.104.161) by server-2.tower-31.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 10 Nov 2016 08:35:56 -0000 Received: (qmail 22509 invoked from network); 10 Nov 2016 10:35:55 +0200 Received: from unknown (HELO mx-sr.buh.bitdefender.com) (10.17.80.103) by mx01.bbu.dsd.mx.bitdefender.com with AES256-GCM-SHA384 encrypted SMTP; 10 Nov 2016 10:35:55 +0200 Received: from smtp01.buh.bitdefender.com (smtp.bitdefender.biz [10.17.80.75]) by mx-sr.buh.bitdefender.com (Postfix) with ESMTP id 796867FC02 for ; Thu, 10 Nov 2016 10:35:55 +0200 (EET) Received: (qmail 26547 invoked from network); 10 Nov 2016 10:35:55 +0200 Received: from xen.dsd.ro (HELO xen.dsd.bitdefender.biz) (rcojocaru@bitdefender.com@10.10.14.109) by smtp01.buh.bitdefender.com with AES128-SHA256 encrypted SMTP; 10 Nov 2016 10:35:55 +0200 From: Razvan Cojocaru To: xen-devel@lists.xen.org Date: Thu, 10 Nov 2016 10:35:41 +0200 Message-Id: <1478766941-14003-1-git-send-email-rcojocaru@bitdefender.com> X-Mailer: git-send-email 1.9.1 X-BitDefender-Scanner: Clean, Agent: BitDefender qmail 3.1.6 on smtp01.buh.bitdefender.com, sigver: 7.67948 X-BitDefender-Spam: No (0) X-BitDefender-SpamStamp: Build: [Engines: 2.15.6.911, Dats: 434740, Stamp: 3], Multi: [Enabled, t: (0.000016, 0.014668)], BW: [Enabled, t: (0.000010,0.000001)], RBL DNSBL: [Disabled], APM: [Enabled, Score: 500, t: (0.014338), Flags: 85D2ED72; NN_LENGTH; NN_NO_CONTENT_TYPE; NN_LEGIT_SUMM_400_WORDS; NN_NO_LINK_NMD; NN_LEGIT_BITDEFENDER; NN_LEGIT_S_SQARE_BRACKETS; NN_LEGIT_MAILING_LIST_TO], SGN: [Enabled, t: (0.013387)], URL: [Enabled, t: (0.000006)], RTDA: [Enabled, t: (0.015542), Hit: No, Details: v2.4.0; Id: 2m1ghn0.1b14pcvru.k4l6], total: 0(775) X-BitDefender-CF-Stamp: none Cc: kevin.tian@intel.com, tamas@tklengyel.com, suravee.suthikulpanit@amd.com, Razvan Cojocaru , andrew.cooper3@citrix.com, julien.grall@arm.com, jbeulich@suse.com, sstabellini@kernel.org, jun.nakajima@intel.com, boris.ostrovsky@oracle.com Subject: [Xen-devel] [PATCH V2] x86/vm_event: Added support for VM_EVENT_REASON_INTERRUPT X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Added support for a new event type, VM_EVENT_REASON_INTERRUPT, which is now fired in a one-shot manner when enabled via the new VM_EVENT_FLAG_GET_NEXT_INTERRUPT vm_event response flag. The patch also fixes the behaviour of the xc_hvm_inject_trap() hypercall, which would lead to non-architectural interrupts overwriting pending (specifically reinjected) architectural ones. Signed-off-by: Razvan Cojocaru --- Changes since V1: - Modified the if() in hvm_do_resume() for readability. - Replaced hard tab with spaces. - Removed a local variable used only once. - Moved cr2 assignment to the common part of the code. - Now listing the new event in the x86 vm_event capability list. - Moved struct variables for readability. - Padding for struct arch_vm_event and struct vm_event_interrupt. - Renamed vm_event_interrupt to vm_event_interrupt_x86 and added the interrupt union. --- xen/arch/x86/hvm/hvm.c | 23 ++++++++++++++++++++++- xen/arch/x86/hvm/monitor.c | 14 ++++++++++++++ xen/arch/x86/hvm/svm/svm.c | 15 +++++++++++++++ xen/arch/x86/hvm/vmx/vmx.c | 20 ++++++++++++++++++++ xen/arch/x86/vm_event.c | 6 ++++++ xen/common/vm_event.c | 3 +++ xen/include/asm-arm/vm_event.h | 6 ++++++ xen/include/asm-x86/hvm/hvm.h | 3 +++ xen/include/asm-x86/hvm/monitor.h | 2 ++ xen/include/asm-x86/monitor.h | 3 ++- xen/include/asm-x86/vm_event.h | 1 + xen/include/public/domctl.h | 1 + xen/include/public/vm_event.h | 18 ++++++++++++++++++ xen/include/xen/vm_event.h | 2 ++ 14 files changed, 115 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 704fd64..4adb894 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -535,9 +535,24 @@ void hvm_do_resume(struct vcpu *v) /* Inject pending hw/sw trap */ if ( v->arch.hvm_vcpu.inject_trap.vector != -1 ) { - hvm_inject_trap(&v->arch.hvm_vcpu.inject_trap); + if ( !hvm_event_pending(v) ) + hvm_inject_trap(&v->arch.hvm_vcpu.inject_trap); + v->arch.hvm_vcpu.inject_trap.vector = -1; } + + if ( unlikely(v->arch.vm_event) && + v->arch.vm_event->monitor_next_interrupt ) + { + struct hvm_trap info; + + if ( hvm_get_pending_event(v, &info) ) + { + hvm_monitor_interrupt(info.vector, info.type, info.error_code, + info.cr2); + v->arch.vm_event->monitor_next_interrupt = false; + } + } } static int hvm_print_line( @@ -6047,6 +6062,12 @@ void hvm_domain_soft_reset(struct domain *d) hvm_destroy_all_ioreq_servers(d); } +bool hvm_get_pending_event(struct vcpu *v, struct hvm_trap *info) +{ + info->cr2 = v->arch.hvm_vcpu.guest_cr[2]; + return hvm_funcs.get_pending_event(v, info); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/hvm/monitor.c b/xen/arch/x86/hvm/monitor.c index 401a8c6..69a88ad 100644 --- a/xen/arch/x86/hvm/monitor.c +++ b/xen/arch/x86/hvm/monitor.c @@ -150,6 +150,20 @@ int hvm_monitor_cpuid(unsigned long insn_length, unsigned int leaf, return monitor_traps(curr, 1, &req); } +void hvm_monitor_interrupt(unsigned int vector, unsigned int type, + unsigned int err, uint64_t cr2) +{ + vm_event_request_t req = { + .reason = VM_EVENT_REASON_INTERRUPT, + .u.interrupt.x86.vector = vector, + .u.interrupt.x86.type = type, + .u.interrupt.x86.error_code = err, + .u.interrupt.x86.cr2 = cr2, + }; + + monitor_traps(current, 1, &req); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 16427f6..af5d458 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -2220,6 +2220,20 @@ static void svm_invlpg(struct vcpu *v, unsigned long vaddr) svm_asid_g_invlpg(v, vaddr); } +static bool svm_get_pending_event(struct vcpu *v, struct hvm_trap *info) +{ + struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb; + + if ( vmcb->eventinj.fields.v ) + return false; + + info->vector = vmcb->eventinj.fields.vector; + info->type = vmcb->eventinj.fields.type; + info->error_code = vmcb->eventinj.fields.errorcode; + + return true; +} + static struct hvm_function_table __initdata svm_function_table = { .name = "SVM", .cpu_up_prepare = svm_cpu_up_prepare, @@ -2272,6 +2286,7 @@ static struct hvm_function_table __initdata svm_function_table = { .tsc_scaling = { .max_ratio = ~TSC_RATIO_RSVD_BITS, }, + .get_pending_event = svm_get_pending_event, }; void svm_vmexit_handler(struct cpu_user_regs *regs) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 9a8f694..3634289 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2134,6 +2134,25 @@ static int vmx_set_mode(struct vcpu *v, int mode) return 0; } +static bool vmx_get_pending_event(struct vcpu *v, struct hvm_trap *info) +{ + unsigned long intr_info, error_code; + + vmx_vmcs_enter(v); + __vmread(VM_ENTRY_INTR_INFO, &intr_info); + __vmread(VM_ENTRY_EXCEPTION_ERROR_CODE, &error_code); + vmx_vmcs_exit(v); + + if ( !(intr_info & INTR_INFO_VALID_MASK) ) + return false; + + info->vector = MASK_EXTR(intr_info, INTR_INFO_VECTOR_MASK); + info->type = MASK_EXTR(intr_info, INTR_INFO_INTR_TYPE_MASK); + info->error_code = error_code; + + return true; +} + static struct hvm_function_table __initdata vmx_function_table = { .name = "VMX", .cpu_up_prepare = vmx_cpu_up_prepare, @@ -2203,6 +2222,7 @@ static struct hvm_function_table __initdata vmx_function_table = { .max_ratio = VMX_TSC_MULTIPLIER_MAX, .setup = vmx_setup_tsc_scaling, }, + .get_pending_event = vmx_get_pending_event, }; /* Handle VT-d posted-interrupt when VCPU is blocked. */ diff --git a/xen/arch/x86/vm_event.c b/xen/arch/x86/vm_event.c index 1e88d67..a153ec5 100644 --- a/xen/arch/x86/vm_event.c +++ b/xen/arch/x86/vm_event.c @@ -134,6 +134,12 @@ void vm_event_set_registers(struct vcpu *v, vm_event_response_t *rsp) v->arch.user_regs.eip = rsp->data.regs.x86.rip; } +void vm_event_monitor_next_interrupt(struct vcpu *v) +{ + ASSERT(v->arch.vm_event); + v->arch.vm_event->monitor_next_interrupt = true; +} + void vm_event_fill_regs(vm_event_request_t *req) { const struct cpu_user_regs *regs = guest_cpu_user_regs(); diff --git a/xen/common/vm_event.c b/xen/common/vm_event.c index 907ab40..c947706 100644 --- a/xen/common/vm_event.c +++ b/xen/common/vm_event.c @@ -433,6 +433,9 @@ void vm_event_resume(struct domain *d, struct vm_event_domain *ved) if ( rsp.flags & VM_EVENT_FLAG_SET_REGISTERS ) vm_event_set_registers(v, &rsp); + if ( rsp.flags & VM_EVENT_FLAG_GET_NEXT_INTERRUPT ) + vm_event_monitor_next_interrupt(v); + if ( rsp.flags & VM_EVENT_FLAG_VCPU_PAUSED ) vm_event_vcpu_unpause(v); } diff --git a/xen/include/asm-arm/vm_event.h b/xen/include/asm-arm/vm_event.h index 66f2474..ab9c8cb 100644 --- a/xen/include/asm-arm/vm_event.h +++ b/xen/include/asm-arm/vm_event.h @@ -52,4 +52,10 @@ void vm_event_emulate_check(struct vcpu *v, vm_event_response_t *rsp) /* Not supported on ARM. */ } +static inline +void vm_event_monitor_next_interrupt(struct vcpu *v) +{ + /* Not supported on ARM. */ +} + #endif /* __ASM_ARM_VM_EVENT_H__ */ diff --git a/xen/include/asm-x86/hvm/hvm.h b/xen/include/asm-x86/hvm/hvm.h index 7e7462e..3370472 100644 --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -157,6 +157,7 @@ struct hvm_function_table { void (*init_hypercall_page)(struct domain *d, void *hypercall_page); int (*event_pending)(struct vcpu *v); + bool (*get_pending_event)(struct vcpu *v, struct hvm_trap *info); void (*invlpg)(struct vcpu *v, unsigned long vaddr); int (*cpu_up_prepare)(unsigned int cpu); @@ -428,6 +429,8 @@ static inline int hvm_event_pending(struct vcpu *v) return hvm_funcs.event_pending(v); } +bool hvm_get_pending_event(struct vcpu *v, struct hvm_trap *info); + /* These bits in CR4 are owned by the host. */ #define HVM_CR4_HOST_MASK (mmu_cr4_features & \ (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE)) diff --git a/xen/include/asm-x86/hvm/monitor.h b/xen/include/asm-x86/hvm/monitor.h index 82b85ec..85ca678 100644 --- a/xen/include/asm-x86/hvm/monitor.h +++ b/xen/include/asm-x86/hvm/monitor.h @@ -42,6 +42,8 @@ int hvm_monitor_debug(unsigned long rip, enum hvm_monitor_debug_type type, unsigned long trap_type, unsigned long insn_length); int hvm_monitor_cpuid(unsigned long insn_length, unsigned int leaf, unsigned int subleaf); +void hvm_monitor_interrupt(unsigned int vector, unsigned int type, + unsigned int err, uint64_t cr2); #endif /* __ASM_X86_HVM_MONITOR_H__ */ diff --git a/xen/include/asm-x86/monitor.h b/xen/include/asm-x86/monitor.h index 63a994b..e409373 100644 --- a/xen/include/asm-x86/monitor.h +++ b/xen/include/asm-x86/monitor.h @@ -76,7 +76,8 @@ static inline uint32_t arch_monitor_get_capabilities(struct domain *d) (1U << XEN_DOMCTL_MONITOR_EVENT_SOFTWARE_BREAKPOINT) | (1U << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST) | (1U << XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION) | - (1U << XEN_DOMCTL_MONITOR_EVENT_CPUID); + (1U << XEN_DOMCTL_MONITOR_EVENT_CPUID) | + (1U << XEN_DOMCTL_MONITOR_EVENT_INTERRUPT); /* Since we know this is on VMX, we can just call the hvm func */ if ( hvm_is_singlestep_supported() ) diff --git a/xen/include/asm-x86/vm_event.h b/xen/include/asm-x86/vm_event.h index ca73f99..38c624c 100644 --- a/xen/include/asm-x86/vm_event.h +++ b/xen/include/asm-x86/vm_event.h @@ -27,6 +27,7 @@ */ struct arch_vm_event { uint32_t emulate_flags; + bool monitor_next_interrupt; union { struct vm_event_emul_read_data read; struct vm_event_emul_insn_data insn; diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 177319d..85cbb7c 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1086,6 +1086,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t); #define XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION 5 #define XEN_DOMCTL_MONITOR_EVENT_CPUID 6 #define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL 7 +#define XEN_DOMCTL_MONITOR_EVENT_INTERRUPT 8 struct xen_domctl_monitor_op { uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */ diff --git a/xen/include/public/vm_event.h b/xen/include/public/vm_event.h index c28be5a..ba9accf 100644 --- a/xen/include/public/vm_event.h +++ b/xen/include/public/vm_event.h @@ -105,6 +105,11 @@ * if any of those flags are set, only those will be honored). */ #define VM_EVENT_FLAG_SET_EMUL_INSN_DATA (1 << 9) +/* + * Have a one-shot VM_EVENT_REASON_INTERRUPT event sent for the first + * interrupt occuring after resuming the VCPU. + */ +#define VM_EVENT_FLAG_GET_NEXT_INTERRUPT (1 << 10) /* * Reasons for the vm event request @@ -139,6 +144,8 @@ * These kinds of events will be filtered out in future versions. */ #define VM_EVENT_REASON_PRIVILEGED_CALL 11 +/* An interrupt has been delivered. */ +#define VM_EVENT_REASON_INTERRUPT 12 /* Supported values for the vm_event_write_ctrlreg index. */ #define VM_EVENT_X86_CR0 0 @@ -259,6 +266,14 @@ struct vm_event_cpuid { uint32_t _pad; }; +struct vm_event_interrupt_x86 { + uint32_t vector; + uint32_t type; + uint32_t error_code; + uint64_t cr2; + uint32_t _pad; +}; + #define MEM_PAGING_DROP_PAGE (1 << 0) #define MEM_PAGING_EVICT_FAIL (1 << 1) @@ -302,6 +317,9 @@ typedef struct vm_event_st { struct vm_event_debug software_breakpoint; struct vm_event_debug debug_exception; struct vm_event_cpuid cpuid; + union { + struct vm_event_interrupt_x86 x86; + } interrupt; } u; union { diff --git a/xen/include/xen/vm_event.h b/xen/include/xen/vm_event.h index 4f088c8..2fb3951 100644 --- a/xen/include/xen/vm_event.h +++ b/xen/include/xen/vm_event.h @@ -78,6 +78,8 @@ void vm_event_vcpu_unpause(struct vcpu *v); void vm_event_fill_regs(vm_event_request_t *req); void vm_event_set_registers(struct vcpu *v, vm_event_response_t *rsp); +void vm_event_monitor_next_interrupt(struct vcpu *v); + #endif /* __VM_EVENT_H__ */ /*