From patchwork Tue Jul 18 16:58:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9849133 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 0D04A600CC for ; Tue, 18 Jul 2017 17:11:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E463828577 for ; Tue, 18 Jul 2017 17:11:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D8F0B285D1; Tue, 18 Jul 2017 17:11:48 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 54AEE28577 for ; Tue, 18 Jul 2017 17:11:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752337AbdGRRLN (ORCPT ); Tue, 18 Jul 2017 13:11:13 -0400 Received: from mail-io0-f170.google.com ([209.85.223.170]:36313 "EHLO mail-io0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752076AbdGRQ7y (ORCPT ); Tue, 18 Jul 2017 12:59:54 -0400 Received: by mail-io0-f170.google.com with SMTP id z62so17067894ioi.3 for ; Tue, 18 Jul 2017 09:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zEdP97lvEH1nBpbDgkV+Dg1HDKn/fGRjSSjAx+SHR0M=; b=NABViPUGnKlJ5uaHRBfYQrBEXL0b4mY+AdBA1ecaovZsSa6riIusYn9cWnIOvhnzvi +vrUG1ie7qwvE4sqA3rpBWSWOfolIvw0B9+txLvUi6IefTcHDWprt914FsR6WiPzhQE0 M22k7teVZDfcmXFDQ05/N5IWDmB1eGc3GIkK4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zEdP97lvEH1nBpbDgkV+Dg1HDKn/fGRjSSjAx+SHR0M=; b=H/Q4FJWpmckmlKHtDkp95SPjISwPNnBAVtoy6Qb5BfO60jbM1b97GBWwpkHFrMx+ym /1KY213sYEzdp6q8QvfD9b+/FzuDSbH2FXMewBCKyAmu8qM82P/EBkdEuSkOZU+VDdc9 w9mCuj5rAllZhqsUnL7X3VUQ67AW1u27UQVMCFydbBqxowYpOihr0167WIxqOTidZRhN rll/i957uoPL3IsDJDf7k1x4LhZ94B48knndYbjZDykP5CIMYvKCqPdYWtwax/ril9LS qBDabcrZIGIVgylq39OZo94CheZEdMqHcb9Yp2y5U6v5JyzWRLwjDiXLat33gu70qW/c vzew== X-Gm-Message-State: AIVw111SHOC+w5MwGYPsGVwsr8h2k60diI6d9P2Z4R3vtlA09EV34yTe eROSRKYwNjjVQ6Wq X-Received: by 10.107.205.136 with SMTP id d130mr2614845iog.109.1500397193105; Tue, 18 Jul 2017 09:59:53 -0700 (PDT) Received: from node.jintackl-qv26972.kvmarm-pg0.wisc.cloudlab.us (c220g1-030822.wisc.cloudlab.us. [128.104.222.82]) by smtp.gmail.com with ESMTPSA id j96sm1413075ioo.49.2017.07.18.09.59.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 09:59:52 -0700 (PDT) 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: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 */