From patchwork Sat Jul 9 04:15:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corneliu ZUZU X-Patchwork-Id: 9221977 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 741E760467 for ; Sat, 9 Jul 2016 04:18:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 63C802868F for ; Sat, 9 Jul 2016 04:18:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58249286A0; Sat, 9 Jul 2016 04:18:16 +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 26C812868F for ; Sat, 9 Jul 2016 04:18:15 +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 1bLjgP-0001a5-Q8; Sat, 09 Jul 2016 04:16:01 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bLjgN-0001Zc-Kq for xen-devel@lists.xen.org; Sat, 09 Jul 2016 04:15:59 +0000 Received: from [85.158.137.68] by server-16.bemta-3.messagelabs.com id C1/BB-23871-E7A70875; Sat, 09 Jul 2016 04:15:58 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrMIsWRWlGSWpSXmKPExsUSfTxjoW5dVUO 4wbVXPBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bpTxeZC04HVky89JOxgfGZbRcjJ4eQgLvE i5ddzF2MXED2GkaJe+2/WCASJxklHs2t62LkALLdJPa8YYGoWcco0XgBpIGTg01AW+LcoXtMI LaIgLTEtc+XGUGKmAU+Mkp82NLLCtIsLOAi8fB3FojJIqAq8W5RMEg5L1B0ycl2FpCwhICcxI IL6SBhTgFXiXNvX7NCXOAicWPfJLDpEgI5EiufzWKHKJeS+N+qBBFeyiKx64sYhC0j8WjiTbY JjEILGBlWMWoUpxaVpRbpGhrqJRVlpmeU5CZm5ugaGhjr5aYWFyemp+YkJhXrJefnbmIEBiAD EOxgXP3b6RCjJAeTkijvBduGcCG+pPyUyozE4oz4otKc1OJDjDIcHEoSvNMqgXKCRanpqRVpm TnAWIBJS3DwKInwWoGkeYsLEnOLM9MhUqcYdTmOzb2xlkmIJS8/L1VKnHc2SJEASFFGaR7cCF hcXmKUlRLmZQQ6SoinILUoN7MEVf4VozgHo5Iwbw3IFJ7MvBK4Ta+AjmACOsIgoB7kiJJEhJR UA6NQrH/0f5+1PTzRRd91glc4bjn3IfN816v1wedKOmtvtH7uWHi8ep7ro/KnfKn+W5f92HJV MNX+ZdeTiQE1hwLl9xt+b7x2tH5T771dm/Scp/76FsSzdNPmCy4clQ5vm7at/G9uHljDFxW19 1649j6FK9a9HW9uvmPc7uy3mWNxwY8p2dVbpx9VYinOSDTUYi4qTgQAQjFjmsYCAAA= X-Env-Sender: czuzu@bitdefender.com X-Msg-Ref: server-6.tower-31.messagelabs.com!1468037757!23370606!1 X-Originating-IP: [91.199.104.161] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 61309 invoked from network); 9 Jul 2016 04:15:58 -0000 Received: from mx01.bbu.dsd.mx.bitdefender.com (HELO mx01.bbu.dsd.mx.bitdefender.com) (91.199.104.161) by server-6.tower-31.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 9 Jul 2016 04:15:58 -0000 Received: (qmail 11861 invoked from network); 9 Jul 2016 07:15:57 +0300 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; 9 Jul 2016 07:15:57 +0300 Received: from smtp03.buh.bitdefender.org (unknown [10.17.80.77]) by mx-sr.buh.bitdefender.com (Postfix) with ESMTP id 4D4B77FBEA for ; Sat, 9 Jul 2016 07:15:57 +0300 (EEST) Received: (qmail 4413 invoked from network); 9 Jul 2016 07:15:57 +0300 Received: from 188-24-34-246.rdsnet.ro (HELO localhost.localdomain) (czuzu@bitdefender.com@188.24.34.246) by smtp03.buh.bitdefender.org with SMTP; 9 Jul 2016 07:15:57 +0300 From: Corneliu ZUZU To: xen-devel@lists.xen.org Date: Sat, 9 Jul 2016 07:15:07 +0300 Message-Id: <1468037707-6663-1-git-send-email-czuzu@bitdefender.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1468037509-6428-1-git-send-email-czuzu@bitdefender.com> References: <1468037509-6428-1-git-send-email-czuzu@bitdefender.com> X-BitDefender-Scanner: Clean, Agent: BitDefender qmail 3.1.6 on smtp03.buh.bitdefender.org, sigver: 7.66242 X-BitDefender-Spam: No (0) X-BitDefender-SpamStamp: Build: [Engines: 2.15.6.911, Dats: 425852, Stamp: 3], Multi: [Enabled, t: (0.000010, 0.008741)], BW: [Enabled, t: (0.000006,0.000001)], RBL DNSBL: [Disabled], APM: [Enabled, Score: 500, t: (0.006722), Flags: 85D2ED72; NN_TP_TAG_HTTP; NN_NO_CONTENT_TYPE; NN_LEGIT_SUMM_400_WORDS; NN_LEGIT_BITDEFENDER; NN_LEGIT_S_SQARE_BRACKETS; NN_LEGIT_MAILING_LIST_TO], SGN: [Enabled, t: (0.013412)], URL: [Enabled, t: (0.000012)], RTDA: [Enabled, t: (0.019384), Hit: No, Details: v2.3.10; Id: 2m1ghic.1aml0sh1c.u9ap], total: 0(775) X-BitDefender-CF-Stamp: none Cc: Kevin Tian , Tamas K Lengyel , Jan Beulich , Razvan Cojocaru , Andrew Cooper , Jun Nakajima Subject: [Xen-devel] [PATCH 05/16] x86/monitor: relocate code more appropriately 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 For readability: * Add function monitor_ctrlreg_write_data() (in x86/monitor.c) and separate handling of monitor_write_data there (previously done directly in hvm_do_resume()). * Separate enabling/disabling of CPU_BASED_CR3_LOAD_EXITING for CR3 monitor vm-events from CR0 node @ vmx_update_guest_cr(v, 0) into newly added function vmx_vm_event_update_cr3_traps(d); also, for a better interface, create generic functions monitor_ctrlreg_adjust_traps() and monitor_ctrlreg_disable_traps() (in x86/monitor.c) which deal with setting/unsetting any traps for cr-write monitor vm-events. Signed-off-by: Corneliu ZUZU --- xen/arch/x86/hvm/hvm.c | 46 ++++++++++---------------- xen/arch/x86/hvm/vmx/vmx.c | 59 ++++++++++++++++++++++++++++++--- xen/arch/x86/monitor.c | 68 +++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/hvm/vmx/vmx.h | 1 + xen/include/asm-x86/monitor.h | 2 ++ 5 files changed, 135 insertions(+), 41 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 7f99087..79ba185 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -473,8 +473,6 @@ void hvm_do_resume(struct vcpu *v) if ( unlikely(v->arch.vm_event) ) { - struct monitor_write_data *w = &v->arch.vm_event->write_data; - if ( unlikely(v->arch.vm_event->emulate_flags) ) { enum emul_kind kind = EMUL_KIND_NORMAL; @@ -492,29 +490,7 @@ void hvm_do_resume(struct vcpu *v) v->arch.vm_event->emulate_flags = 0; } - if ( w->do_write.msr ) - { - hvm_msr_write_intercept(w->msr, w->value, 0); - w->do_write.msr = 0; - } - - if ( w->do_write.cr0 ) - { - hvm_set_cr0(w->cr0, 0); - w->do_write.cr0 = 0; - } - - if ( w->do_write.cr4 ) - { - hvm_set_cr4(w->cr4, 0); - w->do_write.cr4 = 0; - } - - if ( w->do_write.cr3 ) - { - hvm_set_cr3(w->cr3, 0); - w->do_write.cr3 = 0; - } + monitor_ctrlreg_write_data(v); } /* Inject pending hw/sw trap */ @@ -2204,7 +2180,10 @@ int hvm_set_cr0(unsigned long value, bool_t may_defer) if ( hvm_monitor_crX(CR0, value, old_value) ) { - /* The actual write will occur in hvm_do_resume(), if permitted. */ + /* + * The actual write will occur in monitor_ctrlreg_write_data(), if + * permitted. + */ v->arch.vm_event->write_data.do_write.cr0 = 1; v->arch.vm_event->write_data.cr0 = value; @@ -2306,7 +2285,10 @@ int hvm_set_cr3(unsigned long value, bool_t may_defer) if ( hvm_monitor_crX(CR3, value, old) ) { - /* The actual write will occur in hvm_do_resume(), if permitted. */ + /* + * The actual write will occur in monitor_ctrlreg_write_data(), if + * permitted. + */ v->arch.vm_event->write_data.do_write.cr3 = 1; v->arch.vm_event->write_data.cr3 = value; @@ -2386,7 +2368,10 @@ int hvm_set_cr4(unsigned long value, bool_t may_defer) if ( hvm_monitor_crX(CR4, value, old_cr) ) { - /* The actual write will occur in hvm_do_resume(), if permitted. */ + /* + * The actual write will occur in monitor_ctrlreg_write_data(), if + * permitted. + */ v->arch.vm_event->write_data.do_write.cr4 = 1; v->arch.vm_event->write_data.cr4 = value; @@ -3765,7 +3750,10 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content, { ASSERT(v->arch.vm_event); - /* The actual write will occur in hvm_do_resume() (if permitted). */ + /* + * The actual write will occur in monitor_ctrlreg_write_data(), if + * permitted. + */ v->arch.vm_event->write_data.do_write.msr = 1; v->arch.vm_event->write_data.msr = msr; v->arch.vm_event->write_data.value = msr_content; diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 0798245..7a31582 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -1441,11 +1441,6 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) if ( !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) ) v->arch.hvm_vmx.exec_control |= cr3_ctls; - /* Trap CR3 updates if CR3 memory events are enabled. */ - if ( v->domain->arch.monitor.write_ctrlreg_enabled & - monitor_ctrlreg_bitmask(VM_EVENT_X86_CR3) ) - v->arch.hvm_vmx.exec_control |= CPU_BASED_CR3_LOAD_EXITING; - if ( old_ctls != v->arch.hvm_vmx.exec_control ) vmx_update_cpu_exec_control(v); } @@ -3898,6 +3893,60 @@ void vmx_vmenter_helper(const struct cpu_user_regs *regs) } /* + * Adjusts domain CR3 load-exiting execution control for CR3-write monitor + * vm-event. + */ +void vmx_vm_event_update_cr3_traps(const struct domain *d) +{ + struct vcpu *v; + struct arch_vmx_struct *avmx; + unsigned int cr3_bitmask; + bool_t cr3_vmevent, cr3_ldexit; + + /* Domain must be paused. */ + ASSERT(atomic_read(&d->pause_count)); + + /* Non-hap domains trap CR3 writes unconditionally. */ + if ( !paging_mode_hap(d) ) + { +#ifndef NDEBUG + for_each_vcpu ( d, v ) + ASSERT(v->arch.hvm_vmx.exec_control & CPU_BASED_CR3_LOAD_EXITING); +#endif + return; + } + + cr3_bitmask = monitor_ctrlreg_bitmask(VM_EVENT_X86_CR3); + cr3_vmevent = !!(d->arch.monitor.write_ctrlreg_enabled & cr3_bitmask); + + for_each_vcpu ( d, v ) + { + avmx = &v->arch.hvm_vmx; + cr3_ldexit = !!(avmx->exec_control & CPU_BASED_CR3_LOAD_EXITING); + + if ( cr3_vmevent == cr3_ldexit ) + continue; + + /* + * If domain paging is disabled (CR0.PG=0) and + * the domain is not in real-mode, then CR3 load-exiting + * must remain enabled (see vmx_update_guest_cr(v, cr) when cr = 0). + */ + if ( cr3_ldexit && !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) ) + continue; + + if ( cr3_vmevent ) + avmx->exec_control |= CPU_BASED_CR3_LOAD_EXITING; + else + avmx->exec_control &= ~CPU_BASED_CR3_LOAD_EXITING; + + vmx_vmcs_enter(v); + vmx_update_cpu_exec_control(v); + vmx_vmcs_exit(v); + } +} + +/* * Local variables: * mode: C * c-file-style: "BSD" diff --git a/xen/arch/x86/monitor.c b/xen/arch/x86/monitor.c index 06d21b1..29188e5 100644 --- a/xen/arch/x86/monitor.c +++ b/xen/arch/x86/monitor.c @@ -19,10 +19,39 @@ * License along with this program; If not, see . */ +#include #include #include #include +static inline +void monitor_ctrlreg_adjust_traps(struct domain *d, unsigned int index) +{ + /* For now, VMX only. */ + ASSERT(cpu_has_vmx); + + /* Other CRs than CR3 are always trapped. */ + if ( VM_EVENT_X86_CR3 == index ) + vmx_vm_event_update_cr3_traps(d); +} + +static inline void monitor_ctrlreg_disable_traps(struct domain *d) +{ + unsigned int old = d->arch.monitor.write_ctrlreg_enabled; + + d->arch.monitor.write_ctrlreg_enabled = 0; + + if ( old ) + { + /* For now, VMX only. */ + ASSERT(cpu_has_vmx); + + /* Was CR3 load-exiting enabled due to monitoring? */ + if ( old & monitor_ctrlreg_bitmask(VM_EVENT_X86_CR3) ) + vmx_vm_event_update_cr3_traps(d); + } +} + int monitor_init_domain(struct domain *d) { if ( !d->arch.monitor.msr_bitmap ) @@ -38,6 +67,8 @@ void monitor_cleanup_domain(struct domain *d) { xfree(d->arch.monitor.msr_bitmap); + monitor_ctrlreg_disable_traps(d); + memset(&d->arch.monitor, 0, sizeof(d->arch.monitor)); memset(&d->monitor, 0, sizeof(d->monitor)); } @@ -79,6 +110,35 @@ void monitor_ctrlreg_write_resume(struct vcpu *v, vm_event_response_t *rsp) } } +void monitor_ctrlreg_write_data(struct vcpu *v) +{ + struct monitor_write_data *w = &v->arch.vm_event->write_data; + + if ( w->do_write.msr ) + { + hvm_msr_write_intercept(w->msr, w->value, 0); + w->do_write.msr = 0; + } + + if ( w->do_write.cr0 ) + { + hvm_set_cr0(w->cr0, 0); + w->do_write.cr0 = 0; + } + + if ( w->do_write.cr4 ) + { + hvm_set_cr4(w->cr4, 0); + w->do_write.cr4 = 0; + } + + if ( w->do_write.cr3 ) + { + hvm_set_cr3(w->cr3, 0); + w->do_write.cr3 = 0; + } +} + static unsigned long *monitor_bitmap_for_msr(const struct domain *d, u32 *msr) { ASSERT(d->arch.monitor.msr_bitmap && msr); @@ -197,13 +257,7 @@ int arch_monitor_domctl_event(struct domain *d, else ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask; - if ( VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index ) - { - struct vcpu *v; - /* Latches new CR3 mask through CR0 code. */ - for_each_vcpu ( d, v ) - hvm_update_guest_cr(v, 0); - } + monitor_ctrlreg_adjust_traps(d, mop->u.mov_to_cr.index); domain_unpause(d); diff --git a/xen/include/asm-x86/hvm/vmx/vmx.h b/xen/include/asm-x86/hvm/vmx/vmx.h index 359b2a9..301df56 100644 --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -101,6 +101,7 @@ void vmx_update_debug_state(struct vcpu *v); void vmx_update_exception_bitmap(struct vcpu *v); void vmx_update_cpu_exec_control(struct vcpu *v); void vmx_update_secondary_exec_control(struct vcpu *v); +void vmx_vm_event_update_cr3_traps(const struct domain *d); #define POSTED_INTR_ON 0 #define POSTED_INTR_SN 1 diff --git a/xen/include/asm-x86/monitor.h b/xen/include/asm-x86/monitor.h index adca378..3ae24dd 100644 --- a/xen/include/asm-x86/monitor.h +++ b/xen/include/asm-x86/monitor.h @@ -93,6 +93,8 @@ void monitor_cleanup_domain(struct domain *d); void monitor_ctrlreg_write_resume(struct vcpu *v, vm_event_response_t *rsp); +void monitor_ctrlreg_write_data(struct vcpu *v); + bool_t monitored_msr(const struct domain *d, u32 msr); #endif /* __ASM_X86_MONITOR_H__ */