Received: by 2002:ab2:3b09:0:b0:1ed:14ea:9113 with SMTP id b9csp75289lqc; Thu, 29 Feb 2024 10:41:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWdoB3yr3i+mUs+qiW86mzI6+Vaja6uzQU41QdngNhQb6tGUWb0KNVzlUbeoB8AxtslzDUqKw8Qtrm7qNq0Sk8G2skkpDwKrfGC60+BLQ== X-Google-Smtp-Source: AGHT+IEENnZzTpuJ+nOiZcdqkOQWDbK3UM5TqxNjuYC/GQCG8fq1bzahDQvPRCWNJbNx4XggW1dI X-Received: by 2002:ac8:7f81:0:b0:42e:5da9:801e with SMTP id z1-20020ac87f81000000b0042e5da9801emr2986544qtj.11.1709232066536; Thu, 29 Feb 2024 10:41:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709232066; cv=pass; d=google.com; s=arc-20160816; b=a2n2c42dr1JVBVvD0zt/cUT6D5zaumUNnE1FwqTK0bRkRfknyPPc4dECuCXb3kLFev f/L5P1qYhtqrHxPRxl+85tlqB0RPLxfnjAz9MWSR6RQbXr2zG+JCAXyNiWH8BMO8NaID 0QpAg1+kJlnPOCvpe1cS0RGpmsft0Bn8XlkirQJYPb/WsgMVkjVJA/w7+s4CRQMNWmRh /t+li5vVSoX0CeMiX9juasFHH0RO4GUs0hUowFsGAoRajHAtMoKvIxuP4psKeI93MJNO etuaH+KJWTKGjv0IktXv6hvMgoN3lvQt89NWdlQMpNhDDNj+Jqph92ftatks5+O3C5HG 6MrQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=meIy5mAEw7lbE077USzTZRfh8nfpypP8AJ6RvItrSqI=; fh=MualLUEgSTyoTxSPxizY4L9ZPpNq/c8zmiKP0BtA4kU=; b=MY+z3+JLNuLU7WlYlE49kAmio9bSTVPM9Nz9QixsXHTKPD9NBkx1cgQAyazj25zPQS S1o/neW2HabuiukF3bEMuGGQYtHqSWaYs2hCNDdHme1/NEGMoLyjc2f5ynfI+oyk0bSd 2bRbf9zvfd4VoPrTvwk9U/8MrgtYJI4UPgZ2Oz3aoBtxjlXabtj22bL7I2t5tj6BrCbM QdkjHacjaIIjUmxNowiYgi0FwsYOWAenjH5MFCBIjylRWv5n6DN513nleC8ZMksxZDJG j9PZCekt8jFjKe2Buy58OB+8frqwUAB7sHtDndX46h1tKNfRghNsqzBGu+PFL5HyamGX k1Vw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Z4C0qKd/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-87305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87305-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t16-20020a05622a149000b0042c2d914903si2026044qtx.760.2024.02.29.10.41.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 10:41:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Z4C0qKd/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-87305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87305-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3959D1C21422 for ; Thu, 29 Feb 2024 18:41:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1ECCF12EBD0; Thu, 29 Feb 2024 18:41:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Z4C0qKd/" 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 139707828D; Thu, 29 Feb 2024 18:40:58 +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=1709232059; cv=none; b=l/y5ovLxuLvAhI14G6C5M0zUTvKgrHpEaeKMpCJiq3eDJBExvfMAFwUZqAOsTMgKlwyAaJaCl/tSiAYJbeRYgjW9BCZFqlWzEXO1qtgwzHz+aGZHFjaJlMygQUc6/AzVdXo4mquxZkTlUb+IBuDRJMb/TNTilpSJR1Y8QnIfqHE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709232059; c=relaxed/simple; bh=XOIwSKahWpPR9jGAWZnUuaG5OHH6sWJYuNj9i3WFYso=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=S7uQqUJVScrQzx2qfpkpen3z3OFduL19my/oxwL80+9yt13pUXTA9AqvrYWr0zYQp+2K81ShkjG9xszAJGYLBqQ6m6zYBfOITkhzXo7hJvx91Uz5XwBDvw19BtNkPdF8Ozr1J5XV9Gc9ThVpu4ew+R/BCgGXnnpiJOD4c34ejI8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z4C0qKd/; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B3F5C433C7; Thu, 29 Feb 2024 18:40:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709232058; bh=XOIwSKahWpPR9jGAWZnUuaG5OHH6sWJYuNj9i3WFYso=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Z4C0qKd/Rb41/Yaf+xCj37agyrwn3r1ddkExRoiOTQQ1rOv6K0esta8w4jH7PujD+ kD7/qP8No/C/78+0smVm9IZtu4d/jrk7LEqvuhyFtMxBc3j0LJtbp0URAThI+keGei qks/8U5YbqWJsiittU3E6aFngNaeGWrH/DmfvXcvuQjZSKoYhQxx9U49A0Gkb/15KR j9sQH34m8e6nV3/YFkW66ccdvDbTCUuikr9q5EpBgPuov6ncnUJDohVrx0fTvIKk01 sVVwxjS5r90wPV2vfn4N5Gn1d3uC8zTkPgkNbpWl1EoFhHIEm+KMpBrfUVqBbnH6QE aw4KV5QlkVDCA== 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 1rflKx-0088Cq-QW; Thu, 29 Feb 2024 18:40:56 +0000 Date: Thu, 29 Feb 2024 18:40:54 +0000 Message-ID: <8634tb2jwp.wl-maz@kernel.org> From: Marc Zyngier To: Anshuman Khandual Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, Mark Brown , James Clark , Rob Herring , Suzuki Poulose , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , linux-perf-users@vger.kernel.org, Oliver Upton , James Morse , kvmarm@lists.linux.dev Subject: Re: [PATCH V16 5/8] KVM: arm64: nvhe: Disable branch generation in nVHE guests In-Reply-To: <20240125094119.2542332-6-anshuman.khandual@arm.com> References: <20240125094119.2542332-1-anshuman.khandual@arm.com> <20240125094119.2542332-6-anshuman.khandual@arm.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.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org 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: anshuman.khandual@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, broonie@kernel.org, james.clark@arm.com, robh@kernel.org, suzuki.poulose@arm.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, linux-perf-users@vger.kernel.org, oliver.upton@linux.dev, james.morse@arm.com, kvmarm@lists.linux.dev X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Thu, 25 Jan 2024 09:41:16 +0000, Anshuman Khandual wrote: > > Disable the BRBE before we enter the guest, saving the status and enable it > back once we get out of the guest. This avoids capturing branch records in > the guest kernel or userspace, which would be confusing the host samples. > > Cc: Marc Zyngier > Cc: Oliver Upton > Cc: James Morse > Cc: Suzuki K Poulose > Cc: Catalin Marinas > Cc: Will Deacon > Cc: kvmarm@lists.linux.dev > Cc: linux-arm-kernel@lists.infradead.org > CC: linux-kernel@vger.kernel.org > Signed-off-by: Anshuman Khandual > --- > Changes in V16: > > - Dropped BRBCR_EL1 and BRBFCR_EL1 from enum vcpu_sysreg > - Reverted back the KVM NVHE patch - used host_debug_state based 'brbcr_el1' > element, and dropped the previous dependency on Jame's coresight series > > arch/arm64/include/asm/kvm_host.h | 5 ++++- > arch/arm64/kvm/debug.c | 5 +++++ > arch/arm64/kvm/hyp/nvhe/debug-sr.c | 33 ++++++++++++++++++++++++++++++ > 3 files changed, 42 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 21c57b812569..bce8792092af 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -569,7 +569,7 @@ struct kvm_vcpu_arch { > u8 cflags; > > /* Input flags to the hypervisor code, potentially cleared after use */ > - u8 iflags; > + u16 iflags; > > /* State flags for kernel bookkeeping, unused by the hypervisor code */ > u8 sflags; > @@ -610,6 +610,7 @@ struct kvm_vcpu_arch { > u64 pmscr_el1; > /* Self-hosted trace */ > u64 trfcr_el1; > + u64 brbcr_el1; > } host_debug_state; > > /* VGIC state */ > @@ -779,6 +780,8 @@ struct kvm_vcpu_arch { > #define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6)) > /* vcpu running in HYP context */ > #define VCPU_HYP_CONTEXT __vcpu_single_flag(iflags, BIT(7)) > +/* Save BRBE context if active */ > +#define DEBUG_STATE_SAVE_BRBE __vcpu_single_flag(iflags, BIT(8)) > > /* SVE enabled for host EL0 */ > #define HOST_SVE_ENABLED __vcpu_single_flag(sflags, BIT(0)) > diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c > index 8725291cb00a..99f85d8acbf3 100644 > --- a/arch/arm64/kvm/debug.c > +++ b/arch/arm64/kvm/debug.c > @@ -335,10 +335,15 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) > if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) && > !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P)) > vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE); > + > + /* Check if we have BRBE implemented and available at the host */ > + if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT)) > + vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_BRBE); > } > > void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) > { > vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); > vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); > + vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_BRBE); > } > diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > index 4558c02eb352..79bcf0fb1326 100644 > --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c > +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > @@ -79,6 +79,34 @@ static void __debug_restore_trace(u64 trfcr_el1) > write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1); > } > > +static void __debug_save_brbe(u64 *brbcr_el1) > +{ > + *brbcr_el1 = 0; > + > + /* Check if the BRBE is enabled */ > + if (!(read_sysreg_s(SYS_BRBCR_EL1) & (BRBCR_ELx_E0BRE | BRBCR_ELx_ExBRE))) > + return; > + > + /* > + * Prohibit branch record generation while we are in guest. > + * Since access to BRBCR_EL1 is trapped, the guest can't > + * modify the filtering set by the host. > + */ > + *brbcr_el1 = read_sysreg_s(SYS_BRBCR_EL1); > + write_sysreg_s(0, SYS_BRBCR_EL1); As for TRFCR and PMSCR, this is broken on hVHE. Please see [1] M. [1] https://lore.kernel.org/r/20240229145417.3606279-1-maz@kernel.org -- Without deviation from the norm, progress is not possible.