From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03A77C4338F for ; Thu, 19 Aug 2021 14:36:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D1FFB60C3E for ; Thu, 19 Aug 2021 14:36:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240486AbhHSOgg (ORCPT ); Thu, 19 Aug 2021 10:36:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:41972 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238613AbhHSOge (ORCPT ); Thu, 19 Aug 2021 10:36:34 -0400 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8DDF26103A; Thu, 19 Aug 2021 14:35:58 +0000 (UTC) Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mGj9A-005zY7-JG; Thu, 19 Aug 2021 15:35:56 +0100 Date: Thu, 19 Aug 2021 15:35:56 +0100 Message-ID: <87zgtdqz77.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvmarm@lists.cs.columbia.edu, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, christoffer.dall@arm.com, pbonzini@redhat.com, drjones@redhat.com, oupton@google.com, qperret@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com Subject: Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp In-Reply-To: <87fsv6snup.wl-maz@kernel.org> References: <20210817081134.2918285-1-tabba@google.com> <20210817081134.2918285-12-tabba@google.com> <87fsv6snup.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: tabba@google.com, kvmarm@lists.cs.columbia.edu, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, christoffer.dall@arm.com, pbonzini@redhat.com, drjones@redhat.com, oupton@google.com, qperret@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hi Fuad, On Wed, 18 Aug 2021 17:45:50 +0100, Marc Zyngier wrote: > > On Tue, 17 Aug 2021 09:11:30 +0100, > Fuad Tabba wrote: > > > > Add an array of pointers to handlers for various trap reasons in > > nVHE code. > > > > The current code selects how to fixup a guest on exit based on a > > series of if/else statements. Future patches will also require > > different handling for guest exists. Create an array of handlers > > to consolidate them. > > > > No functional change intended as the array isn't populated yet. > > > > Acked-by: Will Deacon > > Signed-off-by: Fuad Tabba > > --- > > arch/arm64/kvm/hyp/include/hyp/switch.h | 43 +++++++++++++++++++++++++ > > arch/arm64/kvm/hyp/nvhe/switch.c | 33 +++++++++++++++++++ > > 2 files changed, 76 insertions(+) > > > > diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h > > index a0e78a6027be..5a2b89b96c67 100644 > > --- a/arch/arm64/kvm/hyp/include/hyp/switch.h > > +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h > > @@ -409,6 +409,46 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) > > return true; > > } > > > > +typedef int (*exit_handle_fn)(struct kvm_vcpu *); > > This returns an int... > > > + > > +exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu); > > + > > +static exit_handle_fn kvm_get_hyp_exit_handler(struct kvm_vcpu *vcpu) > > +{ > > + return is_nvhe_hyp_code() ? kvm_get_nvhe_exit_handler(vcpu) : NULL; > > +} > > + > > +/* > > + * Allow the hypervisor to handle the exit with an exit handler if it has one. > > + * > > + * Returns true if the hypervisor handled the exit, and control should go back > > + * to the guest, or false if it hasn't. > > + */ > > +static bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu) > > +{ > > + bool is_handled = false; > > ... which you then implicitly cast as a bool. > > > + exit_handle_fn exit_handler = kvm_get_hyp_exit_handler(vcpu); > > + > > + if (exit_handler) { > > + /* > > + * There's limited vcpu context here since it's not synced yet. > > + * Ensure that relevant vcpu context that might be used by the > > + * exit_handler is in sync before it's called and if handled. > > + */ > > + *vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR); > > + *vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR); > > + > > + is_handled = exit_handler(vcpu); > > What does 'is_handled' mean here? By definition, any trap *must* be > handled, one way or another. By the look of it, what you really mean > is something like "I have updated the vcpu state and you'd better > reload it". Is that what it means? > > > + > > + if (is_handled) { > > + write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR); > > + write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR); > > + } > > + } > > + > > + return is_handled; > > +} > > All these functions really should be marked inline. Have you checked > how this expands on VHE? I think some compilers could be pretty > unhappy about the undefined symbol in kvm_get_hyp_exit_handler(). > > It is also unfortunate that we get a bunch of tests for various > flavours of traps (FP, PAuth, page faults...), only to hit yet another > decoding tree. Is there a way we could use this infrastructure for > everything? I realised that I wasn't very forthcoming here. I've decided to put the code where my mouth is and pushed out a branch [1] with your first 10 patches, followed by my own take on this particular problem. It compiles, and even managed to boot a Debian guest on a nVHE box. As you can see, most of the early exit handling is now moved to specific handlers, unifying the handling. For the protected mode, you can provide your own handler array (just hack kvm_get_exit_handler_array() to return something else), which will do the right thing as long as you call into the existing handlers first. When it comes to the ELR/SPSR handling, it is better left to the individual handlers (which we already do in some cases, see how we skip instructions, for example). Please let me know what you think. Thanks, M. [1] https://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git/log/?h=kvm-arm64/pkvm-fixed-features -- Without deviation from the norm, progress is not possible.