Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751551AbdGRRLP (ORCPT ); Tue, 18 Jul 2017 13:11:15 -0400 Received: from mail-io0-f170.google.com ([209.85.223.170]:34455 "EHLO mail-io0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751870AbdGRQ7y (ORCPT ); Tue, 18 Jul 2017 12:59:54 -0400 From: Jintack Lim To: kvmarm@lists.cs.columbia.edu, christoffer.dall@linaro.org, marc.zyngier@arm.com Cc: corbet@lwn.net, pbonzini@redhat.com, rkrcmar@redhat.com, linux@armlinux.org.uk, catalin.marinas@arm.com, will.deacon@arm.com, akpm@linux-foundation.org, mchehab@kernel.org, cov@codeaurora.org, daniel.lezcano@linaro.org, david.daney@cavium.com, mark.rutland@arm.com, suzuki.poulose@arm.com, stefan@hello-penguin.com, andy.gross@linaro.org, wcohen@redhat.com, ard.biesheuvel@linaro.org, shankerd@codeaurora.org, vladimir.murzin@arm.com, james.morse@arm.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jintack Lim Subject: [RFC PATCH v2 11/38] KVM: arm64: Set vcpu context depending on the guest exception level Date: Tue, 18 Jul 2017 11:58:37 -0500 Message-Id: <1500397144-16232-12-git-send-email-jintack.lim@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1500397144-16232-1-git-send-email-jintack.lim@linaro.org> References: <1500397144-16232-1-git-send-email-jintack.lim@linaro.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3435 Lines: 112 If the guest exception level is EL2, then set up the shadow context of the virtual EL2 to hardware. Otherwise, set the regular EL0/EL1 context. Note that the shadow context content will be prepared in subsequent patches. Signed-off-by: Jintack Lim --- arch/arm64/kvm/context.c | 74 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kvm/context.c b/arch/arm64/kvm/context.c index bc43e66..2645787 100644 --- a/arch/arm64/kvm/context.c +++ b/arch/arm64/kvm/context.c @@ -18,11 +18,29 @@ #include #include -/** - * kvm_arm_setup_shadow_state -- prepare shadow state based on emulated mode - * @vcpu: The VCPU pointer - */ -void kvm_arm_setup_shadow_state(struct kvm_vcpu *vcpu) +static void flush_shadow_special_regs(struct kvm_vcpu *vcpu) +{ + struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; + + ctxt->hw_pstate = *vcpu_cpsr(vcpu) & ~PSR_MODE_MASK; + /* + * We can emulate the guest's configuration of which + * stack pointer to use when executing in virtual EL2 by + * using the equivalent feature in EL1 to point to + * either the EL1 or EL0 stack pointer. + */ + if ((*vcpu_cpsr(vcpu) & PSR_MODE_MASK) == PSR_MODE_EL2h) + ctxt->hw_pstate |= PSR_MODE_EL1h; + else + ctxt->hw_pstate |= PSR_MODE_EL1t; + + ctxt->hw_sys_regs = ctxt->shadow_sys_regs; + ctxt->hw_sp_el1 = vcpu_el2_sreg(vcpu, SP_EL2); + ctxt->hw_elr_el1 = vcpu_el2_sreg(vcpu, ELR_EL2); + ctxt->hw_spsr_el1 = vcpu_el2_sreg(vcpu, SPSR_EL2); +} + +static void flush_special_regs(struct kvm_vcpu *vcpu) { struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; @@ -33,11 +51,18 @@ void kvm_arm_setup_shadow_state(struct kvm_vcpu *vcpu) ctxt->hw_spsr_el1 = ctxt->gp_regs.spsr[KVM_SPSR_EL1]; } -/** - * kvm_arm_restore_shadow_state -- write back shadow state from guest - * @vcpu: The VCPU pointer - */ -void kvm_arm_restore_shadow_state(struct kvm_vcpu *vcpu) +static void sync_shadow_special_regs(struct kvm_vcpu *vcpu) +{ + struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; + + *vcpu_cpsr(vcpu) &= PSR_MODE_MASK; + *vcpu_cpsr(vcpu) |= ctxt->hw_pstate & ~PSR_MODE_MASK; + vcpu_el2_sreg(vcpu, SP_EL2) = ctxt->hw_sp_el1; + vcpu_el2_sreg(vcpu, ELR_EL2) = ctxt->hw_elr_el1; + vcpu_el2_sreg(vcpu, SPSR_EL2) = ctxt->hw_spsr_el1; +} + +static void sync_special_regs(struct kvm_vcpu *vcpu) { struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; @@ -47,6 +72,35 @@ void kvm_arm_restore_shadow_state(struct kvm_vcpu *vcpu) ctxt->gp_regs.spsr[KVM_SPSR_EL1] = ctxt->hw_spsr_el1; } +/** + * kvm_arm_setup_shadow_state -- prepare shadow state based on emulated mode + * @vcpu: The VCPU pointer + */ +void kvm_arm_setup_shadow_state(struct kvm_vcpu *vcpu) +{ + struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; + + if (unlikely(vcpu_mode_el2(vcpu))) { + flush_shadow_special_regs(vcpu); + ctxt->hw_sys_regs = ctxt->shadow_sys_regs; + } else { + flush_special_regs(vcpu); + ctxt->hw_sys_regs = ctxt->sys_regs; + } +} + +/** + * kvm_arm_restore_shadow_state -- write back shadow state from guest + * @vcpu: The VCPU pointer + */ +void kvm_arm_restore_shadow_state(struct kvm_vcpu *vcpu) +{ + if (unlikely(vcpu_mode_el2(vcpu))) + sync_shadow_special_regs(vcpu); + else + sync_special_regs(vcpu); +} + void kvm_arm_init_cpu_context(kvm_cpu_context_t *cpu_ctxt) { /* This is to set hw_sys_regs of host_cpu_context */ -- 1.9.1