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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 64FB9E66894 for ; Sun, 24 Nov 2024 12:14:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=RQ6TC/caiuZSM7nzfZDPg3t03L77Xh3M7nclBTeg6KY=; b=0HglRqgd48yHzKrRpZRotY9qYV T368ryw0ejVMTo8pskfa0V/+rUr3988fNrxQgw6JXuw+HggEQLufUZhNQSL4gFCj/m+79kZ/HVcGN rZ645/t2ffVwwnq2KDE5nYHeC8CvjeujNV2yxbaIuQOKFzWe5Jo0faBI1bYMwVkDhEK78RoU0sHoR qLgEQkVE7btOTESD9YdJzip5gAcdWtDFLsq02T6kO58W0Fk9+AyzU8P4clHoqnoLbjdzUKvJY2Po4 Rb0eCKvOTpmYxQzrBvbf5r4ybA2mbbW1kH2P/BbqB0cgRKtSmx1TRA6Tvcmm1zaVK2cHOoo+ZdGod 2i8SCYAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tFBUw-00000005iS0-3SrW; Sun, 24 Nov 2024 12:13:54 +0000 Received: from nyc.source.kernel.org ([147.75.193.91]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tFBTz-00000005iP7-0Bgq for linux-arm-kernel@lists.infradead.org; Sun, 24 Nov 2024 12:12:56 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 2D7B0A4022D; Sun, 24 Nov 2024 12:11:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF004C4CECC; Sun, 24 Nov 2024 12:12:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732450372; bh=ZlRIlePa6qIOlCJ17wWF4SE2OcB7ifQ3WFcQDR8qU48=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=bkUbBfNrDqzyOq0zdUVVa95IO3Qjb31ayZ6uyojOCHqbn+QyAbMCRJs0kB6Mmdrfx PJXeWgOpn5bSdLyqw3A4G6f3tmIovDbSj9U9i0vsjORwmIt9Y/CU1K9568WmFmOquo DJggc27Tni8hI5fvQz43LZJfxaF+zGUVUkfI6NWN0O2hX6xPuVbI1eplCYRY7FWGco p6aqTIMYKuocrxMr9J+/uFShB1dhIPUkabYyYXl55c9UwAV2QubKCPlQG8O9rXH+SM 4BS7SC9apuWRctsjfHTRWeuowxJBVA3YkKxOse/5Gm3JUFIhN32b2IoEFPB4qpWKwF xHLjz/umCdBzw== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tFBTu-00FMNr-Iw; Sun, 24 Nov 2024 12:12:50 +0000 Date: Sun, 24 Nov 2024 12:12:50 +0000 Message-ID: <86ldx8vni5.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, james.clark@linaro.org, will@kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, broonie@kernel.org, qperret@google.com Subject: Re: [PATCH v2 05/12] KVM: arm64: Initialize feature id registers for protected VMs In-Reply-To: <20241122110622.3010118-6-tabba@google.com> References: <20241122110622.3010118-1-tabba@google.com> <20241122110622.3010118-6-tabba@google.com> 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/29.4 (aarch64-unknown-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.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, james.clark@linaro.org, will@kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, broonie@kernel.org, qperret@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241124_041255_218797_FA92E5A7 X-CRM114-Status: GOOD ( 33.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 22 Nov 2024 11:06:15 +0000, Fuad Tabba wrote: > > The hypervisor maintains the state of protected VMs. Initialize > the values for feature ID registers for protected VMs, to be used > when setting traps and when advertising features to protected > VMs. > > Signed-off-by: Fuad Tabba > --- > .../arm64/kvm/hyp/include/nvhe/fixed_config.h | 1 + > arch/arm64/kvm/hyp/nvhe/pkvm.c | 4 ++ > arch/arm64/kvm/hyp/nvhe/sys_regs.c | 54 +++++++++++++++++-- > 3 files changed, 56 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/fixed_config.h b/arch/arm64/kvm/hyp/include/nvhe/fixed_config.h > index d1e59b88ff66..69e26d1a0ebe 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/fixed_config.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/fixed_config.h > @@ -201,6 +201,7 @@ > u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id); > bool kvm_handle_pvm_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code); > bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code); > +void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu); > int kvm_check_pvm_sysreg_table(void); > > #endif /* __ARM64_KVM_FIXED_CONFIG_H__ */ > diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c > index 59ff6aac514c..4ef03294b2b4 100644 > --- a/arch/arm64/kvm/hyp/nvhe/pkvm.c > +++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c > @@ -381,6 +381,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, > hyp_vm->kvm.created_vcpus = nr_vcpus; > hyp_vm->kvm.arch.mmu.vtcr = host_mmu.arch.mmu.vtcr; > hyp_vm->kvm.arch.pkvm.enabled = READ_ONCE(host_kvm->arch.pkvm.enabled); > + hyp_vm->kvm.arch.flags = 0; > pkvm_init_features_from_host(hyp_vm, host_kvm); > } > > @@ -419,6 +420,9 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, > hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags); > hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED; > > + if (pkvm_hyp_vcpu_is_protected(hyp_vcpu)) > + kvm_init_pvm_id_regs(&hyp_vcpu->vcpu); > + > ret = pkvm_vcpu_init_traps(hyp_vcpu); > if (ret) > goto done; > diff --git a/arch/arm64/kvm/hyp/nvhe/sys_regs.c b/arch/arm64/kvm/hyp/nvhe/sys_regs.c > index 59fb2f056177..7008e9641f41 100644 > --- a/arch/arm64/kvm/hyp/nvhe/sys_regs.c > +++ b/arch/arm64/kvm/hyp/nvhe/sys_regs.c > @@ -204,8 +204,7 @@ static u64 get_pvm_id_aa64mmfr2(const struct kvm_vcpu *vcpu) > return id_aa64mmfr2_el1_sys_val & PVM_ID_AA64MMFR2_ALLOW; > } > > -/* Read a sanitized cpufeature ID register by its encoding */ > -u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id) > +static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id) > { > switch (id) { > case SYS_ID_AA64PFR0_EL1: > @@ -240,10 +239,25 @@ u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id) > } > } > > +/* Read a sanitized cpufeature ID register by its encoding */ > +u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id) > +{ > + return pvm_calc_id_reg(vcpu, id); > +} > + Whatever reason you have to keep this per-CPU, it really needs to change to be VM-scoped. Maybe not immediately, but that should be on your list. > static u64 read_id_reg(const struct kvm_vcpu *vcpu, > struct sys_reg_desc const *r) > { > - return pvm_read_id_reg(vcpu, reg_to_encoding(r)); > + struct kvm *kvm = vcpu->kvm; > + u32 reg = reg_to_encoding(r); > + > + if (WARN_ON_ONCE(!test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))) > + return 0; > + > + if (reg >= sys_reg(3, 0, 0, 4, 0) && reg <= sys_reg(3, 0, 0, 7, 7)) > + return kvm->arch.id_regs[IDREG_IDX(reg)]; Why the limit to the AA64 idregs? Aren't the other shadow sysreg values correctly initialised to 0? > + > + return 0; > } > > /* Handler to RAZ/WI sysregs */ > @@ -448,6 +462,40 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = { > /* Performance Monitoring Registers are restricted. */ > }; > > +/* > + * Initializes feature registers for protected vms. > + */ > +void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu) > +{ > + const u32 pvm_feat_id_regs[] = { static? > + SYS_ID_AA64PFR0_EL1, > + SYS_ID_AA64PFR1_EL1, > + SYS_ID_AA64ISAR0_EL1, > + SYS_ID_AA64ISAR1_EL1, > + SYS_ID_AA64ISAR2_EL1, > + SYS_ID_AA64ZFR0_EL1, > + SYS_ID_AA64MMFR0_EL1, > + SYS_ID_AA64MMFR1_EL1, > + SYS_ID_AA64MMFR2_EL1, > + SYS_ID_AA64MMFR4_EL1, > + SYS_ID_AA64DFR0_EL1, > + }; > + struct kvm *kvm = vcpu->kvm; > + unsigned long i; > + > + if (WARN_ON_ONCE(test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))) > + return; > + > + for (i = 0; i < ARRAY_SIZE(pvm_feat_id_regs); i++) { But do you really need this array? Can't you just iterate over the idreg space and compute the value? Sure, that'd be a bit longer, but at least you would define the idregs you care about in a single place. > + struct kvm_arch *ka = &kvm->arch; > + u32 reg = pvm_feat_id_regs[i]; > + > + ka->id_regs[IDREG_IDX(reg)] = pvm_calc_id_reg(vcpu, reg); > + } > + > + set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags); > +} > + > /* > * Checks that the sysreg table is unique and in-order. > * Thanks, M. -- Without deviation from the norm, progress is not possible.