From: Oliver Upton <oliver.upton@linux.dev>
To: kvmarm@lists.linux.dev
Cc: Marc Zyngier <maz@kernel.org>, James Morse <james.morse@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>, Will Deacon <will@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>
Subject: [PATCH 1/3] KVM: arm64: pkvm: Actually enable/disable PMU events when running vCPU
Date: Tue, 5 Mar 2024 18:48:38 +0000 [thread overview]
Message-ID: <20240305184840.636212-2-oliver.upton@linux.dev> (raw)
In-Reply-To: <20240305184840.636212-1-oliver.upton@linux.dev>
flush_hyp_vcpu() does not carry through the PMU event context from the
host, regardless of whether the VM is protected or not. This is
problematic for two reasons:
- The expectation for non-protected VMs is that the protected-mode
hypervisor has feature parity with the 'normal' KVM configuration.
However, PMU events programmed to run with the guest do not work.
- The protected-mode hypervisor needs to prevent the host from peering
in on the guest. Nothing stops the host from leaving an event enabled
straight past guest entry...
Address the both of these issues by priming the hyp vCPU's PMU event
context before entering the guest. Take special care to not trust the
host in the case of pVMs by referring directly to what hardware says is
enabled.
Cc: Will Deacon <will@kernel.org>
Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
---
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 2385fd03ed87..8621f8383f0c 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -23,6 +23,26 @@ DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);
+static void flush_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
+{
+ struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
+
+ hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
+ hyp_vcpu->vcpu.arch.debug_ptr = kern_hyp_va(host_vcpu->arch.debug_ptr);
+
+ /*
+ * The host's PMU context cannot be trusted for protected VMs. Refer to
+ * hardware to determine which PMCs need to be disabled/enabled on vCPU
+ * entry/exit, respectively.
+ */
+ if (kvm_vm_is_protected(kern_hyp_va(host_vcpu->kvm))) {
+ hyp_vcpu->vcpu.arch.pmu.events.events_host = read_sysreg(pmcntenclr_el0);
+ hyp_vcpu->vcpu.arch.pmu.events.events_guest = 0;
+ } else {
+ hyp_vcpu->vcpu.arch.pmu.events = host_vcpu->arch.pmu.events;
+ }
+}
+
static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
{
struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
@@ -35,18 +55,18 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu;
hyp_vcpu->vcpu.arch.hcr_el2 = host_vcpu->arch.hcr_el2;
- hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
hyp_vcpu->vcpu.arch.cptr_el2 = host_vcpu->arch.cptr_el2;
hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags;
hyp_vcpu->vcpu.arch.fp_state = host_vcpu->arch.fp_state;
- hyp_vcpu->vcpu.arch.debug_ptr = kern_hyp_va(host_vcpu->arch.debug_ptr);
hyp_vcpu->vcpu.arch.host_fpsimd_state = host_vcpu->arch.host_fpsimd_state;
hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;
hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
+
+ flush_debug_state(hyp_vcpu);
}
static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
--
2.44.0.278.ge034bb2e1d-goog
next prev parent reply other threads:[~2024-03-05 18:48 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 18:48 [PATCH 0/3] KVM: arm64: PMU fixes for nVHE, protected mode Oliver Upton
2024-03-05 18:48 ` Oliver Upton [this message]
2024-03-06 10:06 ` [PATCH 1/3] KVM: arm64: pkvm: Actually enable/disable PMU events when running vCPU Marc Zyngier
2024-03-06 10:18 ` Oliver Upton
2024-03-06 13:23 ` Fuad Tabba
2024-03-05 18:48 ` [PATCH 2/3] KVM: arm64: Fix host-programmed guest events in nVHE Oliver Upton
2024-03-06 9:59 ` Marc Zyngier
2024-03-05 18:48 ` [PATCH 3/3] KVM: arm64: Do not disable preemption in kvm_vcpu_pmu_restore_guest() Oliver Upton
2024-03-06 9:52 ` Marc Zyngier
2024-03-06 9:54 ` Oliver Upton
2024-03-26 13:44 ` (subset) [PATCH 0/3] KVM: arm64: PMU fixes for nVHE, protected mode Oliver Upton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240305184840.636212-2-oliver.upton@linux.dev \
--to=oliver.upton@linux.dev \
--cc=james.morse@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=maz@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox