Skip to content

Commit 7c7b363

Browse files
author
Marc Zyngier
committed
Merge branch kvm-arm64/pkvm-fixed-features-prologue into kvmarm-master/next
* kvm-arm64/pkvm-fixed-features-prologue: : Rework a bunch of common infrastructure as a prologue : to Fuad Tabba's protected VM fixed feature series. KVM: arm64: Upgrade trace_kvm_arm_set_dreg32() to 64bit KVM: arm64: Add config register bit definitions KVM: arm64: Add feature register flag definitions KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug KVM: arm64: Restore mdcr_el2 from vcpu KVM: arm64: Refactor sys_regs.h,c for nVHE reuse KVM: arm64: Fix names of config register fields KVM: arm64: MDCR_EL2 is a 64-bit register KVM: arm64: Remove trailing whitespace in comment KVM: arm64: placeholder to check if VM is protected Signed-off-by: Marc Zyngier <[email protected]>
2 parents deb151a + 411d63d commit 7c7b363

File tree

18 files changed

+140
-104
lines changed

18 files changed

+140
-104
lines changed

arch/arm64/include/asm/cpufeature.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,14 @@ static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)
602602
{
603603
u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SHIFT);
604604

605-
return val == ID_AA64PFR0_EL1_32BIT_64BIT;
605+
return val == ID_AA64PFR0_ELx_32BIT_64BIT;
606606
}
607607

608608
static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
609609
{
610610
u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL0_SHIFT);
611611

612-
return val == ID_AA64PFR0_EL0_32BIT_64BIT;
612+
return val == ID_AA64PFR0_ELx_32BIT_64BIT;
613613
}
614614

615615
static inline bool id_aa64pfr0_sve(u64 pfr0)

arch/arm64/include/asm/kvm_arm.h

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
#include <asm/types.h>
1313

1414
/* Hyp Configuration Register (HCR) bits */
15+
16+
#define HCR_TID5 (UL(1) << 58)
17+
#define HCR_DCT (UL(1) << 57)
1518
#define HCR_ATA_SHIFT 56
1619
#define HCR_ATA (UL(1) << HCR_ATA_SHIFT)
20+
#define HCR_AMVOFFEN (UL(1) << 51)
21+
#define HCR_FIEN (UL(1) << 47)
1722
#define HCR_FWB (UL(1) << 46)
1823
#define HCR_API (UL(1) << 41)
1924
#define HCR_APK (UL(1) << 40)
@@ -32,9 +37,9 @@
3237
#define HCR_TVM (UL(1) << 26)
3338
#define HCR_TTLB (UL(1) << 25)
3439
#define HCR_TPU (UL(1) << 24)
35-
#define HCR_TPC (UL(1) << 23)
40+
#define HCR_TPC (UL(1) << 23) /* HCR_TPCP if FEAT_DPB */
3641
#define HCR_TSW (UL(1) << 22)
37-
#define HCR_TAC (UL(1) << 21)
42+
#define HCR_TACR (UL(1) << 21)
3843
#define HCR_TIDCP (UL(1) << 20)
3944
#define HCR_TSC (UL(1) << 19)
4045
#define HCR_TID3 (UL(1) << 18)
@@ -56,12 +61,13 @@
5661
#define HCR_PTW (UL(1) << 2)
5762
#define HCR_SWIO (UL(1) << 1)
5863
#define HCR_VM (UL(1) << 0)
64+
#define HCR_RES0 ((UL(1) << 48) | (UL(1) << 39))
5965

6066
/*
6167
* The bits we set in HCR:
6268
* TLOR: Trap LORegion register accesses
6369
* RW: 64bit by default, can be overridden for 32bit VMs
64-
* TAC: Trap ACTLR
70+
* TACR: Trap ACTLR
6571
* TSC: Trap SMC
6672
* TSW: Trap cache operations by set/way
6773
* TWE: Trap WFE
@@ -76,7 +82,7 @@
7682
* PTW: Take a stage2 fault if a stage1 walk steps in device memory
7783
*/
7884
#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | \
79-
HCR_BSU_IS | HCR_FB | HCR_TAC | \
85+
HCR_BSU_IS | HCR_FB | HCR_TACR | \
8086
HCR_AMO | HCR_SWIO | HCR_TIDCP | HCR_RW | HCR_TLOR | \
8187
HCR_FMO | HCR_IMO | HCR_PTW )
8288
#define HCR_VIRT_EXCP_MASK (HCR_VSE | HCR_VI | HCR_VF)
@@ -275,24 +281,40 @@
275281
#define CPTR_EL2_TTA (1 << 20)
276282
#define CPTR_EL2_TFP (1 << CPTR_EL2_TFP_SHIFT)
277283
#define CPTR_EL2_TZ (1 << 8)
278-
#define CPTR_EL2_RES1 0x000032ff /* known RES1 bits in CPTR_EL2 */
279-
#define CPTR_EL2_DEFAULT CPTR_EL2_RES1
284+
#define CPTR_NVHE_EL2_RES1 0x000032ff /* known RES1 bits in CPTR_EL2 (nVHE) */
285+
#define CPTR_EL2_DEFAULT CPTR_NVHE_EL2_RES1
286+
#define CPTR_NVHE_EL2_RES0 (GENMASK(63, 32) | \
287+
GENMASK(29, 21) | \
288+
GENMASK(19, 14) | \
289+
BIT(11))
280290

281291
/* Hyp Debug Configuration Register bits */
282292
#define MDCR_EL2_E2TB_MASK (UL(0x3))
283293
#define MDCR_EL2_E2TB_SHIFT (UL(24))
284-
#define MDCR_EL2_TTRF (1 << 19)
285-
#define MDCR_EL2_TPMS (1 << 14)
294+
#define MDCR_EL2_HPMFZS (UL(1) << 36)
295+
#define MDCR_EL2_HPMFZO (UL(1) << 29)
296+
#define MDCR_EL2_MTPME (UL(1) << 28)
297+
#define MDCR_EL2_TDCC (UL(1) << 27)
298+
#define MDCR_EL2_HCCD (UL(1) << 23)
299+
#define MDCR_EL2_TTRF (UL(1) << 19)
300+
#define MDCR_EL2_HPMD (UL(1) << 17)
301+
#define MDCR_EL2_TPMS (UL(1) << 14)
286302
#define MDCR_EL2_E2PB_MASK (UL(0x3))
287303
#define MDCR_EL2_E2PB_SHIFT (UL(12))
288-
#define MDCR_EL2_TDRA (1 << 11)
289-
#define MDCR_EL2_TDOSA (1 << 10)
290-
#define MDCR_EL2_TDA (1 << 9)
291-
#define MDCR_EL2_TDE (1 << 8)
292-
#define MDCR_EL2_HPME (1 << 7)
293-
#define MDCR_EL2_TPM (1 << 6)
294-
#define MDCR_EL2_TPMCR (1 << 5)
295-
#define MDCR_EL2_HPMN_MASK (0x1F)
304+
#define MDCR_EL2_TDRA (UL(1) << 11)
305+
#define MDCR_EL2_TDOSA (UL(1) << 10)
306+
#define MDCR_EL2_TDA (UL(1) << 9)
307+
#define MDCR_EL2_TDE (UL(1) << 8)
308+
#define MDCR_EL2_HPME (UL(1) << 7)
309+
#define MDCR_EL2_TPM (UL(1) << 6)
310+
#define MDCR_EL2_TPMCR (UL(1) << 5)
311+
#define MDCR_EL2_HPMN_MASK (UL(0x1F))
312+
#define MDCR_EL2_RES0 (GENMASK(63, 37) | \
313+
GENMASK(35, 30) | \
314+
GENMASK(25, 24) | \
315+
GENMASK(22, 20) | \
316+
BIT(18) | \
317+
GENMASK(16, 15))
296318

297319
/* For compatibility with fault code shared with 32-bit */
298320
#define FSC_FAULT ESR_ELx_FSC_FAULT

arch/arm64/include/asm/kvm_asm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extern u64 __vgic_v3_read_vmcr(void);
209209
extern void __vgic_v3_write_vmcr(u32 vmcr);
210210
extern void __vgic_v3_init_lrs(void);
211211

212-
extern u32 __kvm_get_mdcr_el2(void);
212+
extern u64 __kvm_get_mdcr_el2(void);
213213

214214
#define __KVM_EXTABLE(from, to) \
215215
" .pushsection __kvm_ex_table, \"a\"\n" \

arch/arm64/include/asm/kvm_host.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,13 @@ struct kvm_vcpu_arch {
286286
/* Stage 2 paging state used by the hardware on next switch */
287287
struct kvm_s2_mmu *hw_mmu;
288288

289-
/* HYP configuration */
289+
/* Values of trap registers for the guest. */
290290
u64 hcr_el2;
291-
u32 mdcr_el2;
291+
u64 mdcr_el2;
292+
u64 cptr_el2;
293+
294+
/* Values of trap registers for the host before guest entry. */
295+
u64 mdcr_el2_host;
292296

293297
/* Exception Information */
294298
struct kvm_vcpu_fault_info fault;
@@ -771,6 +775,11 @@ void kvm_arch_free_vm(struct kvm *kvm);
771775

772776
int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type);
773777

778+
static inline bool kvm_vm_is_protected(struct kvm *kvm)
779+
{
780+
return false;
781+
}
782+
774783
int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
775784
bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
776785

arch/arm64/include/asm/kvm_hyp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void __sve_restore_state(void *sve_pffr, u32 *fpsr);
9595

9696
#ifndef __KVM_NVHE_HYPERVISOR__
9797
void activate_traps_vhe_load(struct kvm_vcpu *vcpu);
98-
void deactivate_traps_vhe_put(void);
98+
void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu);
9999
#endif
100100

101101
u64 __guest_enter(struct kvm_vcpu *vcpu);

arch/arm64/include/asm/sysreg.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,14 +784,13 @@
784784
#define ID_AA64PFR0_AMU 0x1
785785
#define ID_AA64PFR0_SVE 0x1
786786
#define ID_AA64PFR0_RAS_V1 0x1
787+
#define ID_AA64PFR0_RAS_V1P1 0x2
787788
#define ID_AA64PFR0_FP_NI 0xf
788789
#define ID_AA64PFR0_FP_SUPPORTED 0x0
789790
#define ID_AA64PFR0_ASIMD_NI 0xf
790791
#define ID_AA64PFR0_ASIMD_SUPPORTED 0x0
791-
#define ID_AA64PFR0_EL1_64BIT_ONLY 0x1
792-
#define ID_AA64PFR0_EL1_32BIT_64BIT 0x2
793-
#define ID_AA64PFR0_EL0_64BIT_ONLY 0x1
794-
#define ID_AA64PFR0_EL0_32BIT_64BIT 0x2
792+
#define ID_AA64PFR0_ELx_64BIT_ONLY 0x1
793+
#define ID_AA64PFR0_ELx_32BIT_64BIT 0x2
795794

796795
/* id_aa64pfr1 */
797796
#define ID_AA64PFR1_MPAMFRAC_SHIFT 16
@@ -847,6 +846,9 @@
847846
#define ID_AA64MMFR0_ASID_SHIFT 4
848847
#define ID_AA64MMFR0_PARANGE_SHIFT 0
849848

849+
#define ID_AA64MMFR0_ASID_8 0x0
850+
#define ID_AA64MMFR0_ASID_16 0x2
851+
850852
#define ID_AA64MMFR0_TGRAN4_NI 0xf
851853
#define ID_AA64MMFR0_TGRAN4_SUPPORTED_MIN 0x0
852854
#define ID_AA64MMFR0_TGRAN4_SUPPORTED_MAX 0x7
@@ -911,6 +913,7 @@
911913
#define ID_AA64MMFR2_CNP_SHIFT 0
912914

913915
/* id_aa64dfr0 */
916+
#define ID_AA64DFR0_MTPMU_SHIFT 48
914917
#define ID_AA64DFR0_TRBE_SHIFT 44
915918
#define ID_AA64DFR0_TRACE_FILT_SHIFT 40
916919
#define ID_AA64DFR0_DOUBLELOCK_SHIFT 36
@@ -1167,6 +1170,11 @@
11671170
#define ICH_VTR_A3V_SHIFT 21
11681171
#define ICH_VTR_A3V_MASK (1 << ICH_VTR_A3V_SHIFT)
11691172

1173+
#define ARM64_FEATURE_FIELD_BITS 4
1174+
1175+
/* Create a mask for the feature bits of the specified feature. */
1176+
#define ARM64_FEATURE_MASK(x) (GENMASK_ULL(x##_SHIFT + ARM64_FEATURE_FIELD_BITS - 1, x##_SHIFT))
1177+
11701178
#ifdef __ASSEMBLY__
11711179

11721180
.irp num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30

arch/arm64/kernel/cpufeature.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
239239
S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
240240
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
241241
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
242-
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
243-
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
242+
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_ELx_64BIT_ONLY),
243+
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_ELx_64BIT_ONLY),
244244
ARM64_FTR_END,
245245
};
246246

@@ -1956,7 +1956,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
19561956
.sys_reg = SYS_ID_AA64PFR0_EL1,
19571957
.sign = FTR_UNSIGNED,
19581958
.field_pos = ID_AA64PFR0_EL0_SHIFT,
1959-
.min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1959+
.min_field_value = ID_AA64PFR0_ELx_32BIT_64BIT,
19601960
},
19611961
#ifdef CONFIG_KVM
19621962
{
@@ -1967,7 +1967,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
19671967
.sys_reg = SYS_ID_AA64PFR0_EL1,
19681968
.sign = FTR_UNSIGNED,
19691969
.field_pos = ID_AA64PFR0_EL1_SHIFT,
1970-
.min_field_value = ID_AA64PFR0_EL1_32BIT_64BIT,
1970+
.min_field_value = ID_AA64PFR0_ELx_32BIT_64BIT,
19711971
},
19721972
{
19731973
.desc = "Protected KVM",

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
11221122
}
11231123

11241124
vcpu_reset_hcr(vcpu);
1125+
vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
11251126

11261127
/*
11271128
* Handle the "start in power-off" case.

arch/arm64/kvm/debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
DBG_MDSCR_KDE | \
2222
DBG_MDSCR_MDE)
2323

24-
static DEFINE_PER_CPU(u32, mdcr_el2);
24+
static DEFINE_PER_CPU(u64, mdcr_el2);
2525

2626
/**
2727
* save/restore_guest_debug_regs

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
9292
write_sysreg(0, pmselr_el0);
9393
write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
9494
}
95+
96+
vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
9597
write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
9698
}
9799

98-
static inline void __deactivate_traps_common(void)
100+
static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
99101
{
102+
write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
103+
100104
write_sysreg(0, hstr_el2);
101105
if (kvm_arm_support_pmu_v3())
102106
write_sysreg(0, pmuserenr_el0);

0 commit comments

Comments
 (0)