Skip to content

Commit 2417218

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts
KVM caches MDCR_EL2 on a per-CPU basis in order to preserve the configuration of MDCR_EL2.HPMN while running a guest. This is a bit gross, since we're relying on some baked configuration rather than the hardware definition of implemented counters. Discover the number of implemented counters by reading PMCR_EL0.N instead. This works because: - In VHE the kernel runs at EL2, and N always returns the number of counters implemented in hardware - In {n,h}VHE, the EL2 setup code programs MDCR_EL2.HPMN with the EL2 view of PMCR_EL0.N for the host Lastly, avoid traps under nested virtualization by saving PMCR_EL0.N in host data. Tested-by: James Clark <[email protected]> Signed-off-by: Oliver Upton <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent 8ca19c4 commit 2417218

File tree

7 files changed

+18
-41
lines changed

7 files changed

+18
-41
lines changed

arch/arm64/include/asm/kvm_asm.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
enum __kvm_host_smccc_func {
5454
/* Hypercalls available only prior to pKVM finalisation */
5555
/* __KVM_HOST_SMCCC_FUNC___kvm_hyp_init */
56-
__KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 = __KVM_HOST_SMCCC_FUNC___kvm_hyp_init + 1,
57-
__KVM_HOST_SMCCC_FUNC___pkvm_init,
56+
__KVM_HOST_SMCCC_FUNC___pkvm_init = __KVM_HOST_SMCCC_FUNC___kvm_hyp_init + 1,
5857
__KVM_HOST_SMCCC_FUNC___pkvm_create_private_mapping,
5958
__KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector,
6059
__KVM_HOST_SMCCC_FUNC___kvm_enable_ssbs,
@@ -247,8 +246,6 @@ extern void __kvm_adjust_pc(struct kvm_vcpu *vcpu);
247246
extern u64 __vgic_v3_get_gic_config(void);
248247
extern void __vgic_v3_init_lrs(void);
249248

250-
extern u64 __kvm_get_mdcr_el2(void);
251-
252249
#define __KVM_EXTABLE(from, to) \
253250
" .pushsection __kvm_ex_table, \"a\"\n" \
254251
" .align 3\n" \

arch/arm64/include/asm/kvm_host.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ struct kvm_host_data {
642642
* host_debug_state contains the host registers which are
643643
* saved and restored during world switches.
644644
*/
645-
struct {
645+
struct {
646646
/* {Break,watch}point registers */
647647
struct kvm_guest_debug_arch regs;
648648
/* Statistical profiling extension */
@@ -652,6 +652,9 @@ struct kvm_host_data {
652652
/* Values of trap registers for the host before guest entry. */
653653
u64 mdcr_el2;
654654
} host_debug_state;
655+
656+
/* Number of programmable event counters (PMCR_EL0.N) for this CPU */
657+
unsigned int nr_event_counters;
655658
};
656659

657660
struct kvm_host_psci_config {
@@ -1332,7 +1335,7 @@ static inline bool kvm_system_needs_idmapped_vectors(void)
13321335

13331336
static inline void kvm_arch_sync_events(struct kvm *kvm) {}
13341337

1335-
void kvm_arm_init_debug(void);
1338+
void kvm_init_host_debug_data(void);
13361339
void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu);
13371340
void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);
13381341
void kvm_arm_clear_debug(struct kvm_vcpu *vcpu);

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,7 @@ static void cpu_set_hyp_vector(void)
21092109
static void cpu_hyp_init_context(void)
21102110
{
21112111
kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
2112+
kvm_init_host_debug_data();
21122113

21132114
if (!is_kernel_in_hyp_mode())
21142115
cpu_init_hyp_mode();
@@ -2117,7 +2118,6 @@ static void cpu_hyp_init_context(void)
21172118
static void cpu_hyp_init_features(void)
21182119
{
21192120
cpu_set_hyp_vector();
2120-
kvm_arm_init_debug();
21212121

21222122
if (is_kernel_in_hyp_mode())
21232123
kvm_timer_init_vhe();

arch/arm64/kvm/debug.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include "trace.h"
1818

19-
static DEFINE_PER_CPU(u64, mdcr_el2);
20-
2119
/*
2220
* save/restore_guest_debug_regs
2321
*
@@ -60,21 +58,6 @@ static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
6058
*vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
6159
}
6260

63-
/**
64-
* kvm_arm_init_debug - grab what we need for debug
65-
*
66-
* Currently the sole task of this function is to retrieve the initial
67-
* value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
68-
* presumably been set-up by some knowledgeable bootcode.
69-
*
70-
* It is called once per-cpu during CPU hyp initialisation.
71-
*/
72-
73-
void kvm_arm_init_debug(void)
74-
{
75-
__this_cpu_write(mdcr_el2, kvm_call_hyp_ret(__kvm_get_mdcr_el2));
76-
}
77-
7861
/**
7962
* kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
8063
*
@@ -94,7 +77,8 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
9477
* This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK
9578
* to disable guest access to the profiling and trace buffers
9679
*/
97-
vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
80+
vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
81+
*host_data_ptr(nr_event_counters));
9882
vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
9983
MDCR_EL2_TPMS |
10084
MDCR_EL2_TTRF |
@@ -338,3 +322,12 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
338322
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
339323
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
340324
}
325+
326+
void kvm_init_host_debug_data(void)
327+
{
328+
u64 dfr0 = read_sysreg(id_aa64dfr0_el1);
329+
330+
if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT) > 0)
331+
*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
332+
read_sysreg(pmcr_el0));
333+
}

arch/arm64/kvm/hyp/nvhe/debug-sr.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,3 @@ void __debug_switch_to_host(struct kvm_vcpu *vcpu)
106106
{
107107
__debug_switch_to_host_common(vcpu);
108108
}
109-
110-
u64 __kvm_get_mdcr_el2(void)
111-
{
112-
return read_sysreg(mdcr_el2);
113-
}

arch/arm64/kvm/hyp/nvhe/hyp-main.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,6 @@ static void handle___vgic_v3_init_lrs(struct kvm_cpu_context *host_ctxt)
264264
__vgic_v3_init_lrs();
265265
}
266266

267-
static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt)
268-
{
269-
cpu_reg(host_ctxt, 1) = __kvm_get_mdcr_el2();
270-
}
271-
272267
static void handle___vgic_v3_save_vmcr_aprs(struct kvm_cpu_context *host_ctxt)
273268
{
274269
DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1);
@@ -384,7 +379,6 @@ typedef void (*hcall_t)(struct kvm_cpu_context *);
384379

385380
static const hcall_t host_hcall[] = {
386381
/* ___kvm_hyp_init */
387-
HANDLE_FUNC(__kvm_get_mdcr_el2),
388382
HANDLE_FUNC(__pkvm_init),
389383
HANDLE_FUNC(__pkvm_create_private_mapping),
390384
HANDLE_FUNC(__pkvm_cpu_set_vector),

arch/arm64/kvm/hyp/vhe/debug-sr.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ void __debug_switch_to_host(struct kvm_vcpu *vcpu)
1919
{
2020
__debug_switch_to_host_common(vcpu);
2121
}
22-
23-
u64 __kvm_get_mdcr_el2(void)
24-
{
25-
return read_sysreg(mdcr_el2);
26-
}

0 commit comments

Comments
 (0)