Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp1907129rdb; Thu, 17 Aug 2023 05:49:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWHkCguSKTCwBdg0G9AKEYNmi63+Pb8uMdVXHUmxKjGV7Lkj0Rod+XY1IBr1fTRQJXAkt7 X-Received: by 2002:a05:6a00:805:b0:687:4b10:adf3 with SMTP id m5-20020a056a00080500b006874b10adf3mr5508827pfk.14.1692276588470; Thu, 17 Aug 2023 05:49:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692276588; cv=none; d=google.com; s=arc-20160816; b=g9ffAQnp7Wx6mRmaIe0T8CBEHzpxFDrved4S46iAlklY3OUMr1OM12wiI4F1dwd0dZ qQcifRBMbVVeExgZPFDHjI8o1tl1WUOSHiJI/JLDG7khlaD5UePErXWB7GHbIySXrNKm xer+Effd6xxwAWtA2ZP/67f1MMOydb78CcCXiJpJzBV1e4V8MkN1JAOtmRhxC1tPErcJ 8Cst9RLmuZHTK/NYJ1Q1Lq079z7yZLogZz0Vmo0LQQPJU/fOpU9nmK3L7/SRsbbIIr6k YcvQbdzsg6taqqckS4yT95L7zfPKr2Fuq8wlWypCBgThj0I3yt16m/wKcmH8VbyMMI8h 8lUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=RnKW0b0+Sub6XRdN/yRWQbInT7WImiF6o0q58J98Lm8=; fh=NHp5wDEIFClwe9WhFz3etp1idycoE0VYhAX33qCrjvU=; b=fe5XhMM/wVvllZ4cylVLSTp3EYQ5h1UXKi2YM+7wlVeYaLKxFlEhZ3nl+jhREBqChW kO3kUqhTk+gck3DgC4Ft2zCutBQrcJItUwEnvjHVqS/FtjbR3Uox0+uR5IGvhG42ZuHa 4EOZbjU1DsYPrhu0MMi4BnfHLf0a0r5eEXIlyWjxheNR1FWmBL+4IJNyK5qkketFNll8 ucwOFXEY3l1O/xqna6xJLA/0qL7XEO80Z5raanQI4WPcNDDp3PLLikbVK2HZaA1UfzeB 6mOdoUwsNQxbbWhcU6XcUHuPsOZc1AfwtC9URolxFpl+75CzONvC2YlNxYSoB8xYJjtH Z2AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FN4Un9Sg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a63d809000000b00565dfc22f1bsi4367792pgh.865.2023.08.17.05.49.36; Thu, 17 Aug 2023 05:49:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FN4Un9Sg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349343AbjHQKFQ (ORCPT + 99 others); Thu, 17 Aug 2023 06:05:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349973AbjHQKFF (ORCPT ); Thu, 17 Aug 2023 06:05:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 209CC2D61 for ; Thu, 17 Aug 2023 03:04:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 912E766860 for ; Thu, 17 Aug 2023 10:04:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF8F1C433C7; Thu, 17 Aug 2023 10:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692266697; bh=W1jtb9kryF03zGezQ0pFDhPp1SJkF/jrhW+cT+eIlSQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=FN4Un9Sg//ad7XLVVZgBBvXK0Sn95lIzpAQwILtk9wyxiVlJE/MyuGmGpTRhTKJCq uaM89Fb64+xlgQOQ9yBk6oZhKOrrPhMc9ZXJ5OvagsHIQVb7SIHij0khzUzMJ7jHs3 5zOZSxDuu+gWPmAd4/+w7vSYmUk48QlJCc3Nz44dAa95YmIsAlZ9qb+Qv96Mj3RN29 8KX383jcdGqxD+stkclCrtqawhpGdWgKpi2QnwggNFLmp17tNdj5gix1rgPmOKirOI mh8dhezdHP0Iqt8IShCkgdI+4J5LbK5/6XeJpSAJ9+yU0e4nSkY+M+dkTVj6SJS6Jw g7fJ8i4i/I7rA== 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 1qWZs6-005eA6-EC; Thu, 17 Aug 2023 11:04:54 +0100 Date: Thu, 17 Aug 2023 11:04:54 +0100 Message-ID: <86ttsyf0hl.wl-maz@kernel.org> From: Marc Zyngier To: Ganapatrao Kulkarni Cc: linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, eauger@redhat.com, miguel.luis@oracle.com, darren@os.amperecomputing.com, scott@os.amperecomputing.com, Christoffer Dall Subject: Re: [PATCH 2/2] KVM: arm64: timers: Adjust CVAL of a ptimer across guest entry and exits In-Reply-To: <0c5fb304-8c69-80c3-6f1e-487828554244@os.amperecomputing.com> References: <20230817060314.535987-1-gankulkarni@os.amperecomputing.com> <20230817060314.535987-3-gankulkarni@os.amperecomputing.com> <87bkf6oyyt.wl-maz@kernel.org> <0c5fb304-8c69-80c3-6f1e-487828554244@os.amperecomputing.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/28.2 (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=UTF-8 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: gankulkarni@os.amperecomputing.com, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, eauger@redhat.com, miguel.luis@oracle.com, darren@os.amperecomputing.com, scott@os.amperecomputing.com, Christoffer.Dall@arm.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 17 Aug 2023 10:27:55 +0100, Ganapatrao Kulkarni wrote: >=20 >=20 > Hi Marc, >=20 > On 17-08-2023 01:57 pm, Marc Zyngier wrote: > > [Fixing Christoffer's email address] >=20 > Thanks. > >=20 > > On Thu, 17 Aug 2023 07:03:14 +0100, > > Ganapatrao Kulkarni wrote: > >>=20 > >> As per FEAT_ECV, when HCR_EL2.{E2H, TGE} =3D=3D {1, 1}, Enhanced Count= er > >> Virtualization functionality is disabled and CNTPOFF_EL2 value is trea= ted > >> as zero. On VHE host, E2H and TGE are set, hence it is required > >> to adjust CVAL by incrementing it by CNTPOFF_EL2 after guest > >> exit to avoid false physical timer interrupts and also > >> decrement/restore CVAL before the guest entry. > >=20 > > No, this is wrong. Neither E2H nor TGE have any impact on writing to > > CNTPOFF_EL2, nor does it have an impact on CNTP_CVAL_EL0. Just read > > the pseudocode to convince yourself. > >=20 > > CNTPOFF_EL2 is applied at exactly two points: when SW is reading > > CNTPCT_EL0 from EL1 while {E2H,TGE}=3D=3D{1, 0} and when the HW is > > comparing CNTPCT_EL0 with the CNTP_CVAL_EL0. In both cases the offset > > is subtracted from the counter. And that's the point where the running > > EL matters. Which means that CNTPOFF_EL2 behaves exactly like > > CNTVOFF_EL2. No ifs, no buts. >=20 > As per ARM ARM (ARM DDI 0487J.a page D11-5989) > "When FEAT_ECV is implemented, the CNTPOFF_EL2 register allows an > offset to be applied to the physical counter, as viewed from EL1 and > EL0, and to the EL1 physical timer. The functionality of this 64-bit > register is affected by CNTHCTL_EL2.ECV." >=20 > As per ARM ARM (ARM DDI 0487J.a page D19-7857) > "When HCR_EL2.{E2H, TGE} =3D=3D {1, 1} or SCR_EL3.{NS, EEL2} =3D=3D {0, 0= }, then > Enhanced Counter Virtualization functionality is disabled." I think this is just bad writing. The functionality isn't disabled, since you can access CNTPOFF_EL2. It is just that, as for CNTVOFF, the offset isn't applied to reads of the physical counter. >=20 > "The EL1 physical timer interrupt is triggered when ((PCount<63:0> - > CNTPOFF_EL2<63:0>) - PCVal<63:0>) is greater than or equal to 0." >=20 > As per ARM ARM (ARM DDI 0487J.a page D19-7938) > "When EL2 is implemented and enabled in the current Security state, > the physical counter uses a fixed physical offset of *zero* if any of > the following are true: > =E2=80=A2 CNTHCTL_EL2.ECV is 0. > =E2=80=A2 SCR_EL3.ECVEn is 0. > =E2=80=A2 HCR_EL2.{E2H, TGE} is {1, 1}." >=20 > In VHE host hypervisor, E2H=3DTGE=3D1 hence ECV is disabled and Ptimer > interrupt is triggered based on PCount<63:0> - PCVal<63:0> ECV is *not* disabled. It is just that CNTPOFF isn't applied to the counter as viewed from EL2. >=20 > Since cval is set by Guest as per offsetted PCounter value and pCount > is not subtracted by CNTPOFF when in VHE-L0, results in cval becoming > much lesser than physical counter(bumped up since CNTPOFF is zero) and > timer interrupt trigger condition is met falsely. Again, you're misreading the spec. When the guest reads the counter, it reads something that is offset by CNTPOFF. When the guest programs CVAL, it is an offset from the value it has read. When the HW compares the physical counter to CVAL, it applies CNTPOFF to the counter. There is no context to these things, and the value of TGE should not matter. And even if it did, at the point where we enter the guest, everything should be hunky dory: the HW has all the information, and no interrupt should fire if the offseted counter is less than the deadline. Again, this is exactly like CNTVOFF. > There is no issue/impact on cval due to ECV, however it can be/is > manipulated to handle this on and off of CNTPOFF/ECV. >=20 > IIUC, CNTPOFF and CNTVOFF are not same as per specification. We clearly are reading different things in the spec, as I cannot see *any* difference in how they are described. If your HW has built them differently, you have a problem. Thanks, M. --=20 Without deviation from the norm, progress is not possible.