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 D43D2C4828F for ; Sun, 4 Feb 2024 11:09:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fW71PQnWZyUAkKotVpNlRfp86IqBDBt70773r9NwcXA=; b=fOoI6ObLzrgkR6 wYCmqAHAvA0sM9BQznGsx8BLS6W6/Ns18hgFPkstztfcYjMo2pWlWZIw87vU7gMN8sPnKHFWzr3a+ Z+BntXNV10XKHnkAbtvqL65nWPNJKrCIM1ryqGdVRmKcgryzWVWqNtrL/ClIMcKHbRG6OQXw9x8x4 epLYhnL6awp5SSEi5hqT81nDyFinQ/MFUmVeij3LIVhqmrS+vDyZNwILQlTavXSnjjMK7mp7Kn4pq x+F2ssfU5rIr3GFWh6yckHH9TFDXjDcXZIuBSS1p0AsHF/HiMZZVd9+nwgwwhVhTrFx9Sn4+Oa367 Kg7ixq6hkSbDoI31VUbQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rWaMs-00000000dcs-0AhB; Sun, 04 Feb 2024 11:08:58 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rWaMp-00000000dbv-0JzS for linux-arm-kernel@lists.infradead.org; Sun, 04 Feb 2024 11:08:56 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4ED8460959; Sun, 4 Feb 2024 11:08:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E90DDC433F1; Sun, 4 Feb 2024 11:08:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707044929; bh=+4yK6IlEFR619F5ubKn5DvaduDKb7hyVB2D83YttV/o=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OvcZ4rWFqXAS2Rbp3gKm0wevNDSQBG0JWUYT1FImtdXHRg5/s27oD/O3Jg7wZVBS2 bYQxFWZFg4UETjTZEw81So5ss/ZMuVae2kTJ2RoogE9rTzOb4W5A8SOI5WFiT0fnbv Z7oJ0mSYETEMW1xfLCUuo/Ejj9uezZjE1x/RRv4dMKGD2xxrP0qbRgGgwisKOkrIDy 7Asahq1Ut+CcvjI2UPf0KnrEAJg/y6HqlE/x4BOlQr+/HNWeLTsosmKL5pA/Zw/74y XfLbVUcrxvB35w5q0TpytyjPTMPEd2534lF7Acb6gra5SboYlH/X6tycojmqPSgjqD EEEhvShN7LIlw== Received: from sofa.misterjones.org ([185.219.108.64] helo=wait-a-minute.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 1rWaMg-0009pR-8V; Sun, 04 Feb 2024 11:08:46 +0000 Date: Sun, 04 Feb 2024 11:08:45 +0000 Message-ID: <87mssgwkoy.wl-maz@kernel.org> From: Marc Zyngier To: Suzuki K Poulose Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, James Morse , Oliver Upton , Zenghui Yu , Catalin Marinas , Will Deacon , Joey Gouly , Mark Brown Subject: Re: [PATCH v2 02/25] KVM: arm64: Add feature checking helpers In-Reply-To: References: <20240130204533.693853-1-maz@kernel.org> <20240130204533.693853-3-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/28.2 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: suzuki.poulose@arm.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, joey.gouly@arm.com, broonie@kernel.org 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-20240204_030855_231771_821BA6D1 X-CRM114-Status: GOOD ( 30.64 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 02 Feb 2024 17:13:07 +0000, Suzuki K Poulose wrote: > > Hi Marc, > > On 30/01/2024 20:45, Marc Zyngier wrote: > > In order to make it easier to check whether a particular feature > > is exposed to a guest, add a new set of helpers, with kvm_has_feat() > > being the most useful. > > > > Let's start making use of them in the PMU code (courtesy of Oliver). > > Follow-up work will intricude additional use patterns. > > I think there is a bit of inconsistency in the macros for signed > and unsigned. The unsigned fields are extracted (i.e., as if they > were shifted to bit 0). But the signed fields are not shifted > completely to bit '0' (in fact to different positions) and eventually > we compare wrong things. > > Using ID_AA64PFR0_EL1, fld=EL2, val=IMP for unsigned and > ID_AA64PFR0_EL1, AdvSIMD, NI for signed. > > > > > Co-developed--by: Oliver Upton > > Signed-off-by: Oliver Upton > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/include/asm/kvm_host.h | 53 +++++++++++++++++++++++++++++++ > > arch/arm64/kvm/pmu-emul.c | 11 ++++--- > > arch/arm64/kvm/sys_regs.c | 6 ++-- > > include/kvm/arm_pmu.h | 11 ------- > > 4 files changed, 61 insertions(+), 20 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > > index 21c57b812569..c0cf9c5f5e8d 100644 > > --- a/arch/arm64/include/asm/kvm_host.h > > +++ b/arch/arm64/include/asm/kvm_host.h > > @@ -1233,4 +1233,57 @@ static inline void kvm_hyp_reserve(void) { } > > void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); > > bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu); > > +#define __expand_field_sign_unsigned(id, fld, val) > > \ > > + ((u64)(id##_##fld##_##val)) > > For unsigned features we get the actual "field" value, not the value > in position. e.g,: ID_AA64PFR0_EL1_EL2_IMP = (0x1) > > > + > > +#define __expand_field_sign_signed(id, fld, val) \ > > + ({ \ > > + s64 __val = id##_##fld##_##val; \ > > + __val <<= 64 - id##_##fld##_WIDTH; \ > > + __val >>= 64 - id##_##fld##_SHIFT - id##_##fld##_WIDTH; \ > > But for signed fields, we shift them back into the "position" as in > the ID_REG. e.g., > > ID_AA64PFR0_EL1, AdvSIMD, NI we get: > > __val = ID_AA64PFR0_EL1_AdvSIMD_NI; /* = 0xf */ > __val <<= 64 - 4; /* 0xf0_00_00_00_00_00_00_00 */ > __val >>= 64 - 20 - 4; /* 0xff_ff_ff_ff_ff_f0_00_00 */ > > I think the last line instead should be: > __val >>= 64 - id##_##fld##_WIDTH; Huh, you're absolutely right. > > > + \ > > + __val; \ > > + }) > > + > > +#define expand_field_sign(id, fld, val) \ > > + (id##_##fld##_SIGNED ? \ > > + __expand_field_sign_signed(id, fld, val) : \ > > + __expand_field_sign_unsigned(id, fld, val)) > > + > > +#define get_idreg_field_unsigned(kvm, id, fld) \ > > + ({ \ > > + u64 __val = IDREG(kvm, SYS_##id); \ > > + __val &= id##_##fld##_MASK; \ > > + __val >>= id##_##fld##_SHIFT; \ > > + \ > > We extract the field value for unsigned field, i.e., shifted to bit"0" > and that matches the expand_field_sign(). > > > + __val; \ > > + }) > > + > > +#define get_idreg_field_signed(kvm, id, fld) \ > > + ({ \ > > + s64 __val = IDREG(kvm, SYS_##id); \ > > + __val <<= 64 - id##_##fld##_SHIFT - id##_##fld##_WIDTH; \ > > + __val >>= id##_##fld##_SHIFT; \ > > However, here we get (assuming value ID_AA64PFR0_EL1_ASIMD = 0xf, and > all other fields are 0 for clarity) > > __val = IDREG(kvm, SYS_ID_AA64PFR0_EL1); = 0xf0_00_00; /* 0xf << 20 */ > __val <<= 64 - 20 - 4; /* = 0xf0_00_00_00_00_00_00_00 */ > __val >>= 20; /* = 0xff_ff_ff_00_00_00_00_00 */ Gah... again! > > Thus they don;t match. Instead the last line should be : > > __val >>= id##_##fld##_WIDTH; Shouldn't this be (64 - WIDTH) instead, since we want the value to be shifted to bit 0? Otherwise, you get 0xff_00_00_00_00_00_00_00 (as per your example). Thanks a lot for spotting those, much appreciated. M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel