From patchwork Fri Oct 30 21:56:32 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mario Smarduch X-Patchwork-Id: 7529441 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CC14D9F37F for ; Fri, 30 Oct 2015 21:59:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B464B20773 for ; Fri, 30 Oct 2015 21:59:09 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 886CE2076E for ; Fri, 30 Oct 2015 21:59:08 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZsHfr-0004xk-AF; Fri, 30 Oct 2015 21:57:27 +0000 Received: from mailout4.w2.samsung.com ([211.189.100.14] helo=usmailout4.samsung.com) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZsHfa-0004ue-0V for linux-arm-kernel@lists.infradead.org; Fri, 30 Oct 2015 21:57:11 +0000 Received: from uscpsbgex4.samsung.com (u125.gpu85.samsung.co.kr [203.254.195.125]) by usmailout4.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NX100MO1ZN3ZV50@usmailout4.samsung.com> for linux-arm-kernel@lists.infradead.org; Fri, 30 Oct 2015 17:57:03 -0400 (EDT) X-AuditID: cbfec37d-f79746d0000068cf-de-5633e7af7310 Received: from usmmp2.samsung.com ( [203.254.195.78]) by uscpsbgex4.samsung.com (USCPEXMTA) with SMTP id A9.0D.26831.FA7E3365; Fri, 30 Oct 2015 17:57:03 -0400 (EDT) Received: from sisasmtp.sisa.samsung.com ([105.144.21.116]) by usmmp2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NX100IR2ZN3TX60@usmmp2.samsung.com>; Fri, 30 Oct 2015 17:57:03 -0400 (EDT) Received: from mjsmard-530U3C-530U4C-532U3C.sisa.samsung.com (105.145.28.253) by SISAEX02SJ.sisa.samsung.com (105.144.21.116) with Microsoft SMTP Server (TLS) id 14.3.224.2; Fri, 30 Oct 2015 14:57:02 -0700 From: Mario Smarduch To: kvmarm@lists.cs.columbia.edu, christoffer.dall@linaro.org, marc.zyngier@arm.com Subject: [PATCH v3 2/3] KVM/arm/arm64: enable enhanced armv7 fp/simd lazy switch Date: Fri, 30 Oct 2015 14:56:32 -0700 Message-id: <1446242193-8424-3-git-send-email-m.smarduch@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1446242193-8424-1-git-send-email-m.smarduch@samsung.com> References: <1446242193-8424-1-git-send-email-m.smarduch@samsung.com> MIME-version: 1.0 X-Originating-IP: [105.145.28.253] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrMLMWRmVeSWpSXmKPExsVy+t9hP931z43DDPrvylg03f/BavHi9T9G izlTCy0+njrObrHp8TVWi793/rE5sHmsmbeG0aPlyFtWjzvX9rB5nN+0htlj85J6j8+b5ALY orhsUlJzMstSi/TtErgytr26zlQw37LizKsZjA2Mk/W6GDk5JARMJA7O3ckCYYtJXLi3nq2L kYtDSGAZo8S74zMYQRJCAk1MEi9nskMkLjJKvD15DyzBJqArsf/eRnYQW0QgVGLK8tdMIEXM Aq2MEreWzwcbKywQIHHl3FWgsRwcLAKqEs+/SYGEeQVcJe6/msEMsVlO4uSxyawgJZwCbhJT W+og9rpKrDl2kx2iXFDix+R7LCAlzAISEs8/K0GUqEpsu/mcESQsIaAksWO79gRGoVlIGmYh NCxgZFrFKFZanFxQnJSeWmGiV5yYW1yal66XnJ+7iRES9LU7GO9/tTnEKMDBqMTDq7HdOEyI NbGsuDL3EKMEB7OSCC/TVKAQb0piZVVqUX58UWlOavEhRmkOFiVx3ghJuVAhgfTEktTs1NSC 1CKYLBMHp1QDo1S44vn1P230Il4wnRORyd/w7u3u1bkGX5Pn8xz1WzLXtnXzZpsLyw2udq9h 8xFLS/3efkX78t9J5VlMf7+p7Y1wXjlpZXqpuDZvTMqjP2L6i17suO1WyTzF5LdY3uZjs1oN ZXwPuUzWb+y3jJ0unsrVUb3gwtndvzludVpcmai59v1X6b8TZZRYijMSDbWYi4oTATUb7Zd2 AgAA X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151030_145710_159984_AB649BE7 X-CRM114-Status: GOOD ( 18.78 ) X-Spam-Score: -7.9 (-------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, antonios.motakis@huawei.com, Mario Smarduch Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch tracks vfp/simd hardware state with a vcpu lazy flag. vCPU lazy flag is set on guest access and traps to vfp/simd hardware switch handler. On vm-enter if lazy flag is set skip trap enable and save host fpexc. On vm-exit if flag is set skip hardware context switch and return to host with guest context. In vcpu_put check if vcpu lazy flag is set, and execute a hardware context switch to restore host. Also some arm64 field and empty function are added to compile for arm64. Signed-off-by: Mario Smarduch --- arch/arm/include/asm/kvm_host.h | 1 + arch/arm/kvm/arm.c | 6 ++++ arch/arm/kvm/interrupts.S | 60 ++++++++++++++++++++++++++++----------- arch/arm/kvm/interrupts_head.S | 14 +++++---- arch/arm64/include/asm/kvm_host.h | 4 +++ 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index f1bf551..a9e86e0 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -227,6 +227,7 @@ int kvm_perf_teardown(void); void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot); struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); +void kvm_restore_host_vfp_state(struct kvm_vcpu *); static inline void kvm_arch_hardware_disable(void) {} static inline void kvm_arch_hardware_unsetup(void) {} diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index dc017ad..11a56fe 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -296,6 +296,12 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) { /* + * If fp/simd registers are dirty save guest, restore host before + * releasing the cpu. + */ + if (vcpu->arch.vfp_dirty) + kvm_restore_host_vfp_state(vcpu); + /* * The arch-generic KVM code expects the cpu field of a vcpu to be -1 * if the vcpu is no longer assigned to a cpu. This is used for the * optimized make_all_cpus_request path. diff --git a/arch/arm/kvm/interrupts.S b/arch/arm/kvm/interrupts.S index 900ef6d..ca25314 100644 --- a/arch/arm/kvm/interrupts.S +++ b/arch/arm/kvm/interrupts.S @@ -28,6 +28,32 @@ #include "interrupts_head.S" .text +/** + * void kvm_restore_host_vfp_state(struct vcpu *vcpu) - Executes lazy + * fp/simd switch, saves the guest, restores host. Called from host + * mode, placed outside of hyp region start/end. + */ +ENTRY(kvm_restore_host_vfp_state) +#ifdef CONFIG_VFPv3 + push {r4-r7} + + add r7, vcpu, #VCPU_VFP_GUEST + store_vfp_state r7 + + add r7, vcpu, #VCPU_VFP_HOST + ldr r7, [r7] + restore_vfp_state r7 + + ldr r3, [vcpu, #VCPU_VFP_HOST_FPEXC] + VFPFMXR FPEXC, r3 + + mov r3, #0 + strb r3, [vcpu, #VCPU_VFP_DIRTY] + + pop {r4-r7} +#endif + bx lr +ENDPROC(kvm_restore_host_vfp_state) __kvm_hyp_code_start: .globl __kvm_hyp_code_start @@ -119,11 +145,16 @@ ENTRY(__kvm_vcpu_run) @ If the host kernel has not been configured with VFPv3 support, @ then it is safer if we deny guests from using it as well. #ifdef CONFIG_VFPv3 - @ Set FPEXC_EN so the guest doesn't trap floating point instructions + @ fp/simd register file has already been accessed, so skip host fpexc + @ save and access trap enable. + vfp_inlazy_mode r7, skip_guest_vfp_trap + VFPFMRX r2, FPEXC @ VMRS - push {r2} + str r2, [vcpu, #VCPU_VFP_HOST_FPEXC] orr r2, r2, #FPEXC_EN VFPFMXR FPEXC, r2 @ VMSR + set_hcptr vmentry, (HCPTR_TCP(10) | HCPTR_TCP(11)) +skip_guest_vfp_trap: #endif @ Configure Hyp-role @@ -131,7 +162,7 @@ ENTRY(__kvm_vcpu_run) @ Trap coprocessor CRx accesses set_hstr vmentry - set_hcptr vmentry, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11)) + set_hcptr vmentry, (HCPTR_TTA) set_hdcr vmentry @ Write configured ID register into MIDR alias @@ -170,22 +201,15 @@ __kvm_vcpu_return: @ Don't trap coprocessor accesses for host kernel set_hstr vmexit set_hdcr vmexit - set_hcptr vmexit, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11)), after_vfp_restore + set_hcptr vmexit, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11)) #ifdef CONFIG_VFPv3 - @ Switch VFP/NEON hardware state to the host's - add r7, vcpu, #VCPU_VFP_GUEST - store_vfp_state r7 - add r7, vcpu, #VCPU_VFP_HOST - ldr r7, [r7] - restore_vfp_state r7 - -after_vfp_restore: - @ Restore FPEXC_EN which we clobbered on entry - pop {r2} + @ If fp/simd not dirty, restore FPEXC which we clobbered on entry. + @ Otherwise return with guest FPEXC, later saved in vcpu_put. + vfp_inlazy_mode r2, skip_restore_host_fpexc + ldr r2, [vcpu, #VCPU_VFP_HOST_FPEXC] VFPFMXR FPEXC, r2 -#else -after_vfp_restore: +skip_restore_host_fpexc: #endif @ Reset Hyp-role @@ -485,6 +509,10 @@ switch_to_guest_vfp: @ NEON/VFP used. Turn on VFP access. set_hcptr vmtrap, (HCPTR_TCP(10) | HCPTR_TCP(11)) + @ set lazy mode flag, switch hardware context on vcpu_put + mov r1, #1 + strb r1, [vcpu, #VCPU_VFP_DIRTY] + @ Switch VFP/NEON hardware state to the guest's add r7, r0, #VCPU_VFP_HOST ldr r7, [r7] diff --git a/arch/arm/kvm/interrupts_head.S b/arch/arm/kvm/interrupts_head.S index 51a5950..34e71ee 100644 --- a/arch/arm/kvm/interrupts_head.S +++ b/arch/arm/kvm/interrupts_head.S @@ -593,10 +593,8 @@ ARM_BE8(rev r6, r6 ) * (hardware reset value is 0). Keep previous value in r2. * An ISB is emited on vmexit/vmtrap, but executed on vmexit only if * VFP wasn't already enabled (always executed on vmtrap). - * If a label is specified with vmexit, it is branched to if VFP wasn't - * enabled. */ -.macro set_hcptr operation, mask, label = none +.macro set_hcptr operation, mask mrc p15, 4, r2, c1, c1, 2 ldr r3, =\mask .if \operation == vmentry @@ -611,13 +609,17 @@ ARM_BE8(rev r6, r6 ) beq 1f .endif isb - .if \label != none - b \label - .endif 1: .endif .endm +/* Checks if VFP/SIMD dirty flag is set, if it is branch to label. */ +.macro vfp_inlazy_mode, reg, label + ldr \reg, [vcpu, #VCPU_VFP_DIRTY] + cmp \reg, #1 + beq \label +.endm + /* Configures the HDCR (Hyp Debug Configuration Register) on entry/return * (hardware reset value is 0) */ .macro set_hdcr operation diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 4562459..26a2347 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -157,6 +157,9 @@ struct kvm_vcpu_arch { /* Interrupt related fields */ u64 irq_lines; /* IRQ and FIQ levels */ + /* fp/simd dirty flag true if guest accessed register file */ + bool vfp_dirty; + /* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache; @@ -248,6 +251,7 @@ static inline void kvm_arch_hardware_unsetup(void) {} static inline void kvm_arch_sync_events(struct kvm *kvm) {} static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {} static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} +static inline void kvm_restore_host_vfp_state(struct kvm_vcpu *vcpu) {} void kvm_arm_init_debug(void); void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);