Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E483F50276 for ; Tue, 21 May 2024 21:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716325731; cv=none; b=itrTvN1Rv+dzgVVEMDTrT7aLzAX/cr32SIno8Wxzh1dZ0A1zMtWxB9hjJ3i9t8nhU+GQwcO0pnEo+r1ZXkv+FozmXQv46mDSeIRpoYuEkONFdRCHIPy1vnCst9kPD92WNAYsSbEnAn1ixImLfSL2L3UvXEkHLMDPtmrRaFPgYkY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716325731; c=relaxed/simple; bh=Q6+t4Wag+AbwutnQpFC+4WXVks6pqEcCpIp21PwqiUU=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=NcW3xxr5pBvL0FPVbESThfbyNBHSid7mBTqu8vGkFlDOnAp8wFXWrbIXvJCyeXAen9h8neW04oUxmNyu+DmvqM1Xh9wDNoieAJvfP3zQMDK6KhLqaeGqksJBxNCm9NtljgKq2BD7IWZXP5ZEO7hE1sujFYSTk125VPrlcWZn0hs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=D43alZzv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="D43alZzv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AF8FC2BD11; Tue, 21 May 2024 21:08:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716325730; bh=Q6+t4Wag+AbwutnQpFC+4WXVks6pqEcCpIp21PwqiUU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=D43alZzvGYPwZoSkfntRkCe/KpgdTG41rxSb44HDLM9XPR5LCswfG56y1EeGAEOhN AKwi11HCM3lwB9JG9cubkFGaHSEyJ2YYL88PQIXTKUTP/yZwGcud5ojq8R9auTpS70 mbkf9FXg+lPHsL7xvHmYm0GXfHybQJRp97vcHzY0jcI9tZfc2LjjoLEtE886LoFQMP H9/ICGbnxSYJcbEn9U5EmBinqYHmZtpWQGKUOxdWx9i9DyfzMGXHEVMoSXwrTbjyNb k/Hfw3+x2N+nj7NYExgBMdfnOtA6xR+IPGEPukmvKzalDtJvtMdgbK6Ji/wiJwkW/j u4Wa8LLVPdrXA== 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 1s9Wj1-00Enoa-EH; Tue, 21 May 2024 22:08:47 +0100 Date: Tue, 21 May 2024 22:08:46 +0100 Message-ID: <86seyana8x.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, will@kernel.org, qperret@google.com, seanjc@google.com, alexandru.elisei@arm.com, catalin.marinas@arm.com, philmd@linaro.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, mark.rutland@arm.com, broonie@kernel.org, joey.gouly@arm.com, rananta@google.com, yuzenghui@huawei.com Subject: Re: [PATCH v2 2/7] KVM: arm64: Abstract set/clear of CPTR_EL2 bits behind helper In-Reply-To: <20240521163720.3812851-3-tabba@google.com> References: <20240521163720.3812851-1-tabba@google.com> <20240521163720.3812851-3-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.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: 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, will@kernel.org, qperret@google.com, seanjc@google.com, alexandru.elisei@arm.com, catalin.marinas@arm.com, philmd@linaro.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, mark.rutland@arm.com, broonie@kernel.org, joey.gouly@arm.com, rananta@google.com, yuzenghui@huawei.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Tue, 21 May 2024 17:37:15 +0100, Fuad Tabba wrote: > > The same traps controlled by CPTR_EL2 or CPACR_EL1 need to be > toggled in different parts of the code, but the exact bits and > their polarity differ between these two formats and the mode > (vhe/nvhe/hvhe). > > To reduce the amount of duplicated code and the chance of getting > the wrong bit/polarity or missing a field, abstract the set/clear > of CPTR_EL2 bits behind a helper. > > Since (h)VHE is the way of the future, use the CPACR_EL1 format, > which is a subset of the VHE CPTR_EL2, as a reference. > > No functional change intended. > > Suggested-by: Oliver Upton > Signed-off-by: Fuad Tabba > --- > arch/arm64/include/asm/kvm_emulate.h | 34 +++++++++++++++++++++++++ > arch/arm64/kvm/hyp/include/hyp/switch.h | 17 +++---------- > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 +---- > 3 files changed, 39 insertions(+), 18 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index 501e3e019c93..74837d1762e5 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -557,6 +557,40 @@ static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu) > vcpu_set_flag((v), e); \ > } while (0) > > + > +static inline void __cptr_clear_set_nvhe(u64 cpacr_clr, u64 cpacr_set) > +{ > + u64 clr = 0, set = 0; > + > + if (cpacr_clr & CPACR_ELx_FPEN) > + set |= CPTR_EL2_TFP; > + if (cpacr_clr & CPACR_ELx_ZEN) > + set |= CPTR_EL2_TZ; > + if (cpacr_clr & CPACR_ELx_SMEN) These 3 fields are actually pairs of bits. Can we have a compile-time check that both bits are set? > + set |= CPTR_EL2_TSM; > + if (cpacr_clr & CPACR_ELx_TTA) > + clr |= CPTR_EL2_TTA; How about TCPAC, TAM, and E0POE? > + > + if (cpacr_set & CPACR_ELx_FPEN) > + clr |= CPTR_EL2_TFP; > + if (cpacr_set & CPACR_ELx_ZEN) > + clr |= CPTR_EL2_TZ; > + if (cpacr_set & CPACR_ELx_SMEN) > + clr |= CPTR_EL2_TSM; > + if (cpacr_set & CPACR_ELx_TTA) > + set |= CPTR_EL2_TTA; The duplication is pretty unfortunate. Having a single helper that translate a register layout into another would be better. > + > + sysreg_clear_set(cptr_el2, clr, set); And omit this... > +} > + > +static inline void cpacr_clear_set(u64 clr, u64 set) > +{ > + if (has_vhe() || has_hvhe()) > + sysreg_clear_set(cpacr_el1, clr, set); > + else > + __cptr_clear_set_nvhe(clr, set); So that this could read as: sysreg_clear_set(cptr_el2, cpacr_to_cptr(clr), cpacr_to_cptr(set)); Thanks, M. -- Without deviation from the norm, progress is not possible.