Skip to contentSkip to conversation

KVM: arm64: Fixed features for protected VMs

PATCH Superseded Fuad Tabba 5 participants 34 messages Aug 17, 2021 – Aug 23, 2021

Subsystems (3)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

v4 cover KVM: arm64: Fixed features for protected VMs

Hi,

Changes since v3 [1]:
- Redid calculating restricted values of feature register fields, ensuring that the code distinguishes between unsigned and (potentially in the future) signed fields (Will)
- Refactoring and fixes (Drew, Will)
- More documentation and comments (Oliver, Will)
- Dropped patch "Restrict protected VM capabilities", since it should come with or after the user ABI series for pKVM (Will)
- Carried Will's acks

Changes since v2 [2]:
- Both trapping and setting of feature id registers are toggled by an allowed features bitmap of the feature id registers (Will)
- Documentation explaining the rationale behind allowed/blocked features (Drew)
- Restrict protected VM features by checking and restricting VM capabilities
- Misc small fixes and tidying up (mostly Will)
- Remove dependency on Will's protected VM user ABI series [3]
- Rebase on 5.14-rc2
- Carried Will's acks

Changes since v1 [4]:
- Restrict protected VM features based on an allowed features rather than rejected ones (Drew)
- Add more background describing protected KVM to the cover letter (Alex)

This patch series adds support for restricting CPU features for protected VMs in KVM (pKVM) [5].

Various VM feature configurations are allowed in KVM/arm64, each requiring specific handling logic to deal with traps, context-switching and potentially emulation. Achieving feature parity in pKVM therefore requires either elevating this logic to EL2 (and substantially increasing the TCB) or continuing to trust the host handlers at EL1. Since neither of these options are especially appealing, pKVM instead limits the CPU features exposed to a guest to a fixed configuration based on the underlying hardware and which can mostly be provided straightforwardly by EL2.

This series approaches that by restricting CPU features exposed to protected guests. Features advertised through feature registers are limited, which pKVM enforces by trapping register accesses and instructions associated with these features.

This series is based on 5.14-rc2. You can find the applied series here [6].

Cheers,
/fuad

[1] https://lore.kernel.org/kvmarm/[email protected]/

[2] https://lore.kernel.org/kvmarm/[email protected]/

[3] https://lore.kernel.org/kvmarm/[email protected]/

[4] https://lore.kernel.org/kvmarm/[email protected]/

[5] Once complete, protected KVM adds the ability to create protected VMs. These protected VMs are protected from the host Linux kernel (and from other VMs), where the host does not have access to guest memory,even if compromised. Normal (nVHE) guests can still be created and run in parallel with protected VMs. Their functionality should not be affected.

For protected VMs, the host should not even have access to a protected guest's state or anything that would enable it to manipulate it (e.g., vcpu register context and el2 system registers); only hyp would have that access. If the host could access that state, then it might be able to get around the protection provided. Therefore, anything that is sensitive and that would require such access needs to happen at hyp, hence the code in nvhe running only at hyp.

For more details about pKVM, please refer to Will's talk at KVM Forum 2020:
https://mirrors.edge.kernel.org/pub/linux/kernel/people/will/slides/kvmforum-2020-edited.pdf
https://www.youtube.com/watch?v=edqJSzsDRxk

[6] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/el2_fixed_feature_v4

Fuad Tabba (15):
KVM: arm64: placeholder to check if VM is protected
KVM: arm64: Remove trailing whitespace in comment
KVM: arm64: MDCR_EL2 is a 64-bit register
KVM: arm64: Fix names of config register fields
KVM: arm64: Refactor sys_regs.h,c for nVHE reuse
KVM: arm64: Restore mdcr_el2 from vcpu
KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug
KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch
KVM: arm64: Add feature register flag definitions
KVM: arm64: Add config register bit definitions
KVM: arm64: Guest exit handlers for nVHE hyp
KVM: arm64: Add trap handlers for protected VMs
KVM: arm64: Move sanitized copies of CPU features
KVM: arm64: Trap access to pVM restricted features
KVM: arm64: Handle protected guests at 32 bits

 arch/arm64/include/asm/cpufeature.h       |   4 +-
 arch/arm64/include/asm/kvm_arm.h          |  54 ++-
 arch/arm64/include/asm/kvm_asm.h          |   2 +-
 arch/arm64/include/asm/kvm_fixed_config.h | 170 +++++++++
 arch/arm64/include/asm/kvm_host.h         |  15 +-
 arch/arm64/include/asm/kvm_hyp.h          |   5 +-
 arch/arm64/include/asm/sysreg.h           |  17 +-
 arch/arm64/kernel/cpufeature.c            |   8 +-
 arch/arm64/kvm/Makefile                   |   2 +-
 arch/arm64/kvm/arm.c                      |  12 +
 arch/arm64/kvm/debug.c                    |   2 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h   |  52 ++-
 arch/arm64/kvm/hyp/nvhe/Makefile          |   2 +-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c        |   2 +-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c     |   6 -
 arch/arm64/kvm/hyp/nvhe/switch.c          |  87 ++++-
 arch/arm64/kvm/hyp/nvhe/sys_regs.c        | 432 ++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/debug-sr.c         |   2 +-
 arch/arm64/kvm/hyp/vhe/switch.c           |  12 +-
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c        |   2 +-
 arch/arm64/kvm/pkvm.c                     | 185 +++++++++
 arch/arm64/kvm/sys_regs.c                 |  64 +---
 arch/arm64/kvm/sys_regs.h                 |  31 ++
 23 files changed, 1059 insertions(+), 109 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_fixed_config.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/sys_regs.c
 create mode 100644 arch/arm64/kvm/pkvm.c

base-commit: c500bee1c5b2f1d59b1081ac879d73268ab0ff17

Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#1 lore raw

PATCH v4 1/15 KVM: arm64: placeholder to check if VM is protected

Add a function to check whether a VM is protected (under pKVM). Since the creation of protected VMs isn't enabled yet, this is a placeholder that always returns false. The intention is for this to become a check for protected VMs in the future (see Will's RFC).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/include/asm/kvm_host.h +5 -0
index 41911585ae0c..347781f99b6a 100644
@@ -771,6 +771,11 @@ void kvm_arch_free_vm(struct kvm *kvm);
 
 int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type);
 
+static inline bool kvm_vm_is_protected(struct kvm *kvm)
+{
+	return false;
+}
+
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#2 lore raw

PATCH v4 2/15 KVM: arm64: Remove trailing whitespace in comment

In reply to Fuad Tabba

Remove trailing whitespace from comment in trap_dbgauthstatus_el1().

No functional change intended.

---
 arch/arm64/kvm/sys_regs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -2
index f6f126eb6ac1..80a6e41cadad 100644
@@ -318,14 +318,14 @@ static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
 /*
  * We want to avoid world-switching all the DBG registers all the
  * time:
- * 
+ *
  * - If we've touched any debug register, it is likely that we're
  *   going to touch more of them. It then makes sense to disable the
  *   traps and start doing the save/restore dance
  * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
  *   then mandatory to save/restore the registers, as the guest
  *   depends on them.
- * 
+ *
  * For this, we use a DIRTY bit, indicating the guest has modified the
  * debug registers, used as follow:
  *
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#3 lore raw

PATCH v4 3/15 KVM: arm64: MDCR_EL2 is a 64-bit register

In reply to Fuad Tabba

Fix the places in KVM that treat MDCR_EL2 as a 32-bit register. More recent features (e.g., FEAT_SPEv1p2) use bits above 31.

No functional change intended.

---
 arch/arm64/include/asm/kvm_arm.h   | 20 ++++++++++----------
 arch/arm64/include/asm/kvm_asm.h   |  2 +-
 arch/arm64/include/asm/kvm_host.h  |  2 +-
 arch/arm64/kvm/debug.c             |  2 +-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c |  2 +-
 arch/arm64/kvm/hyp/vhe/debug-sr.c  |  2 +-
 6 files changed, 15 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_arm.h +10 -10
index d436831dd706..6a523ec83415 100644
@@ -281,18 +281,18 @@
 /* Hyp Debug Configuration Register bits */
 #define MDCR_EL2_E2TB_MASK	(UL(0x3))
 #define MDCR_EL2_E2TB_SHIFT	(UL(24))
-#define MDCR_EL2_TTRF		(1 << 19)
-#define MDCR_EL2_TPMS		(1 << 14)
+#define MDCR_EL2_TTRF		(UL(1) << 19)
+#define MDCR_EL2_TPMS		(UL(1) << 14)
 #define MDCR_EL2_E2PB_MASK	(UL(0x3))
 #define MDCR_EL2_E2PB_SHIFT	(UL(12))
-#define MDCR_EL2_TDRA		(1 << 11)
-#define MDCR_EL2_TDOSA		(1 << 10)
-#define MDCR_EL2_TDA		(1 << 9)
-#define MDCR_EL2_TDE		(1 << 8)
-#define MDCR_EL2_HPME		(1 << 7)
-#define MDCR_EL2_TPM		(1 << 6)
-#define MDCR_EL2_TPMCR		(1 << 5)
-#define MDCR_EL2_HPMN_MASK	(0x1F)
+#define MDCR_EL2_TDRA		(UL(1) << 11)
+#define MDCR_EL2_TDOSA		(UL(1) << 10)
+#define MDCR_EL2_TDA		(UL(1) << 9)
+#define MDCR_EL2_TDE		(UL(1) << 8)
+#define MDCR_EL2_HPME		(UL(1) << 7)
+#define MDCR_EL2_TPM		(UL(1) << 6)
+#define MDCR_EL2_TPMCR		(UL(1) << 5)
+#define MDCR_EL2_HPMN_MASK	(UL(0x1F))
 
 /* For compatibility with fault code shared with 32-bit */
 #define FSC_FAULT	ESR_ELx_FSC_FAULT
arch/arm64/include/asm/kvm_asm.h +1 -1
index 9f0bf2109be7..63ead9060ab5 100644
@@ -210,7 +210,7 @@ extern u64 __vgic_v3_read_vmcr(void);
 extern void __vgic_v3_write_vmcr(u32 vmcr);
 extern void __vgic_v3_init_lrs(void);
 
-extern u32 __kvm_get_mdcr_el2(void);
+extern u64 __kvm_get_mdcr_el2(void);
 
 #define __KVM_EXTABLE(from, to)						\
 	"	.pushsection	__kvm_ex_table, \"a\"\n"		\
arch/arm64/include/asm/kvm_host.h +1 -1
index 347781f99b6a..4d2d974c1522 100644
@@ -289,7 +289,7 @@ struct kvm_vcpu_arch {
 
 	/* HYP configuration */
 	u64 hcr_el2;
-	u32 mdcr_el2;
+	u64 mdcr_el2;
 
 	/* Exception Information */
 	struct kvm_vcpu_fault_info fault;
arch/arm64/kvm/debug.c +1 -1
index d5e79d7ee6e9..db9361338b2a 100644
@@ -21,7 +21,7 @@
 				DBG_MDSCR_KDE | \
 				DBG_MDSCR_MDE)
 
-static DEFINE_PER_CPU(u32, mdcr_el2);
+static DEFINE_PER_CPU(u64, mdcr_el2);
 
 /**
  * save/restore_guest_debug_regs
arch/arm64/kvm/hyp/nvhe/debug-sr.c +1 -1
index 7d3f25868cae..df361d839902 100644
@@ -109,7 +109,7 @@ void __debug_switch_to_host(struct kvm_vcpu *vcpu)
 	__debug_switch_to_host_common(vcpu);
 }
 
-u32 __kvm_get_mdcr_el2(void)
+u64 __kvm_get_mdcr_el2(void)
 {
 	return read_sysreg(mdcr_el2);
 }
arch/arm64/kvm/hyp/vhe/debug-sr.c +1 -1
index f1e2e5a00933..289689b2682d 100644
@@ -20,7 +20,7 @@ void __debug_switch_to_host(struct kvm_vcpu *vcpu)
 	__debug_switch_to_host_common(vcpu);
 }
 
-u32 __kvm_get_mdcr_el2(void)
+u64 __kvm_get_mdcr_el2(void)
 {
 	return read_sysreg(mdcr_el2);
 }
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#5 lore raw

PATCH v4 4/15 KVM: arm64: Fix names of config register fields

In reply to Fuad Tabba

Change the names of hcr_el2 register fields to match the Arm Architecture Reference Manual. Easier for cross-referencing and for grepping.

Also, change the name of CPTR_EL2_RES1 to CPTR_NVHE_EL2_RES1, because res1 bits are different for VHE.

No functional change intended.

---
 arch/arm64/include/asm/kvm_arm.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_arm.h +6 -6
index 6a523ec83415..a928b2dc0b0f 100644
@@ -32,9 +32,9 @@
 #define HCR_TVM		(UL(1) << 26)
 #define HCR_TTLB	(UL(1) << 25)
 #define HCR_TPU		(UL(1) << 24)
-#define HCR_TPC		(UL(1) << 23)
+#define HCR_TPC		(UL(1) << 23) /* HCR_TPCP if FEAT_DPB */
 #define HCR_TSW		(UL(1) << 22)
-#define HCR_TAC		(UL(1) << 21)
+#define HCR_TACR	(UL(1) << 21)
 #define HCR_TIDCP	(UL(1) << 20)
 #define HCR_TSC		(UL(1) << 19)
 #define HCR_TID3	(UL(1) << 18)
@@ -61,7 +61,7 @@
  * The bits we set in HCR:
  * TLOR:	Trap LORegion register accesses
  * RW:		64bit by default, can be overridden for 32bit VMs
- * TAC:		Trap ACTLR
+ * TACR:	Trap ACTLR
  * TSC:		Trap SMC
  * TSW:		Trap cache operations by set/way
  * TWE:		Trap WFE
@@ -76,7 +76,7 @@
  * PTW:		Take a stage2 fault if a stage1 walk steps in device memory
  */
 #define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | \
-			 HCR_BSU_IS | HCR_FB | HCR_TAC | \
+			 HCR_BSU_IS | HCR_FB | HCR_TACR | \
 			 HCR_AMO | HCR_SWIO | HCR_TIDCP | HCR_RW | HCR_TLOR | \
 			 HCR_FMO | HCR_IMO | HCR_PTW )
 #define HCR_VIRT_EXCP_MASK (HCR_VSE | HCR_VI | HCR_VF)
@@ -275,8 +275,8 @@
 #define CPTR_EL2_TTA	(1 << 20)
 #define CPTR_EL2_TFP	(1 << CPTR_EL2_TFP_SHIFT)
 #define CPTR_EL2_TZ	(1 << 8)
-#define CPTR_EL2_RES1	0x000032ff /* known RES1 bits in CPTR_EL2 */
-#define CPTR_EL2_DEFAULT	CPTR_EL2_RES1
+#define CPTR_NVHE_EL2_RES1	0x000032ff /* known RES1 bits in CPTR_EL2 (nVHE) */
+#define CPTR_EL2_DEFAULT	CPTR_NVHE_EL2_RES1
 
 /* Hyp Debug Configuration Register bits */
 #define MDCR_EL2_E2TB_MASK	(UL(0x3))
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#6 lore raw

PATCH v4 5/15 KVM: arm64: Refactor sys_regs.h,c for nVHE reuse

In reply to Fuad Tabba

Refactor sys_regs.h and sys_regs.c to make it easier to reuse common code. It will be used in nVHE in a later patch.

Note that the refactored code uses __inline_bsearch for find_reg instead of bsearch to avoid copying the bsearch code for nVHE.

No functional change intended.

---
 arch/arm64/include/asm/sysreg.h |  5 +++
 arch/arm64/kvm/sys_regs.c       | 60 +++++++++------------------------
 arch/arm64/kvm/sys_regs.h       | 31 +++++++++++++++++
 3 files changed, 52 insertions(+), 44 deletions(-)
arch/arm64/include/asm/sysreg.h +5 -0
index 7b9c3acba684..53a93a9c5253 100644
@@ -1153,6 +1153,11 @@
 #define ICH_VTR_A3V_SHIFT	21
 #define ICH_VTR_A3V_MASK	(1 << ICH_VTR_A3V_SHIFT)
 
+#define ARM64_FEATURE_FIELD_BITS	4
+
+/* Create a mask for the feature bits of the specified feature. */
+#define ARM64_FEATURE_MASK(x)	(GENMASK_ULL(x##_SHIFT + ARM64_FEATURE_FIELD_BITS - 1, x##_SHIFT))
+
 #ifdef __ASSEMBLY__
 
 	.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/kvm/sys_regs.c +16 -44

110 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +31 -0
index 9d0621417c2a..cc0cc95a0280 100644
@@ -11,6 +11,12 @@
 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
 #define __ARM64_KVM_SYS_REGS_LOCAL_H__
 
+#include <linux/bsearch.h>
+
+#define reg_to_encoding(x)						\
+	sys_reg((u32)(x)->Op0, (u32)(x)->Op1,				\
+		(u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2)
+
 struct sys_reg_params {
 	u8	Op0;
 	u8	Op1;
@@ -21,6 +27,14 @@ struct sys_reg_params {
 	bool	is_write;
 };
 
+#define esr_sys64_to_params(esr)                                               \
+	((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3,                    \
+				  .Op1 = ((esr) >> 14) & 0x7,                  \
+				  .CRn = ((esr) >> 10) & 0xf,                  \
+				  .CRm = ((esr) >> 1) & 0xf,                   \
+				  .Op2 = ((esr) >> 17) & 0x7,                  \
+				  .is_write = !((esr) & 1) })
+
 struct sys_reg_desc {
 	/* Sysreg string for debug */
 	const char *name;
@@ -152,6 +166,23 @@ static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
 	return i1->Op2 - i2->Op2;
 }
 
+static inline int match_sys_reg(const void *key, const void *elt)
+{
+	const unsigned long pval = (unsigned long)key;
+	const struct sys_reg_desc *r = elt;
+
+	return pval - reg_to_encoding(r);
+}
+
+static inline const struct sys_reg_desc *
+find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
+	 unsigned int num)
+{
+	unsigned long pval = reg_to_encoding(params);
+
+	return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
+}
+
 const struct sys_reg_desc *find_reg_by_id(u64 id,
 					  struct sys_reg_params *params,
 					  const struct sys_reg_desc table[],
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#7 lore raw

PATCH v4 6/15 KVM: arm64: Restore mdcr_el2 from vcpu

In reply to Fuad Tabba

On deactivating traps, restore the value of mdcr_el2 from the newly created and preserved host value vcpu context, rather than directly reading the hardware register.

Up until and including this patch the two values are the same, i.e., the hardware register and the vcpu one. A future patch will be changing the value of mdcr_el2 on activating traps, and this ensures that its value will be restored.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h       |  5 ++++-
 arch/arm64/include/asm/kvm_hyp.h        |  2 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h |  6 +++++-
 arch/arm64/kvm/hyp/nvhe/switch.c        | 13 +++++--------
 arch/arm64/kvm/hyp/vhe/switch.c         | 14 +++++---------
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c      |  2 +-
 6 files changed, 21 insertions(+), 21 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -1
index 4d2d974c1522..76462c6a91ee 100644
@@ -287,10 +287,13 @@ struct kvm_vcpu_arch {
 	/* Stage 2 paging state used by the hardware on next switch */
 	struct kvm_s2_mmu *hw_mmu;
 
-	/* HYP configuration */
+	/* Values of trap registers for the guest. */
 	u64 hcr_el2;
 	u64 mdcr_el2;
 
+	/* Values of trap registers for the host before guest entry. */
+	u64 mdcr_el2_host;
+
 	/* Exception Information */
 	struct kvm_vcpu_fault_info fault;
 
arch/arm64/include/asm/kvm_hyp.h +1 -1
index 9d60b3006efc..657d0c94cf82 100644
@@ -95,7 +95,7 @@ void __sve_restore_state(void *sve_pffr, u32 *fpsr);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
 void activate_traps_vhe_load(struct kvm_vcpu *vcpu);
-void deactivate_traps_vhe_put(void);
+void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu);
 #endif
 
 u64 __guest_enter(struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/include/hyp/switch.h +5 -1
index e4a2f295a394..a0e78a6027be 100644
@@ -92,11 +92,15 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 		write_sysreg(0, pmselr_el0);
 		write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
 	}
+
+	vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
 	write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
 }
 
-static inline void __deactivate_traps_common(void)
+static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
 {
+	write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
+
 	write_sysreg(0, hstr_el2);
 	if (kvm_arm_support_pmu_v3())
 		write_sysreg(0, pmuserenr_el0);
arch/arm64/kvm/hyp/nvhe/switch.c +5 -8
index f7af9688c1f7..2ea764a48958 100644
@@ -69,12 +69,10 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 static void __deactivate_traps(struct kvm_vcpu *vcpu)
 {
 	extern char __kvm_hyp_host_vector[];
-	u64 mdcr_el2, cptr;
+	u64 cptr;
 
 	___deactivate_traps(vcpu);
 
-	mdcr_el2 = read_sysreg(mdcr_el2);
-
 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
 		u64 val;
 
@@ -92,13 +90,12 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 		isb();
 	}
 
-	__deactivate_traps_common();
+	vcpu->arch.mdcr_el2_host &= MDCR_EL2_HPMN_MASK |
+				    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
+				    MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
 
-	mdcr_el2 &= MDCR_EL2_HPMN_MASK;
-	mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
-	mdcr_el2 |= MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
+	__deactivate_traps_common(vcpu);
 
-	write_sysreg(mdcr_el2, mdcr_el2);
 	write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
 
 	cptr = CPTR_EL2_DEFAULT;
arch/arm64/kvm/hyp/vhe/switch.c +5 -9
index b3229924d243..ec158fa41ae6 100644
@@ -91,17 +91,13 @@ void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
 	__activate_traps_common(vcpu);
 }
 
-void deactivate_traps_vhe_put(void)
+void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu)
 {
-	u64 mdcr_el2 = read_sysreg(mdcr_el2);
+	vcpu->arch.mdcr_el2_host &= MDCR_EL2_HPMN_MASK |
+				    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
+				    MDCR_EL2_TPMS;
 
-	mdcr_el2 &= MDCR_EL2_HPMN_MASK |
-		    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
-		    MDCR_EL2_TPMS;
-
-	write_sysreg(mdcr_el2, mdcr_el2);
-
-	__deactivate_traps_common();
+	__deactivate_traps_common(vcpu);
 }
 
 /* Switch to the guest for VHE systems running in EL2 */
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +1 -1
index 2a0b8c88d74f..007a12dd4351 100644
@@ -101,7 +101,7 @@ void kvm_vcpu_put_sysregs_vhe(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *host_ctxt;
 
 	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
-	deactivate_traps_vhe_put();
+	deactivate_traps_vhe_put(vcpu);
 
 	__sysreg_save_el1_state(guest_ctxt);
 	__sysreg_save_user_state(guest_ctxt);
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#10 lore raw

PATCH v4 7/15 KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug

In reply to Fuad Tabba

__init_el2_debug configures mdcr_el2 at initialization based on, among other things, available hardware support. Trap deactivation doesn't check that, so keep the initial value.

No functional change intended. However, the value of mdcr_el2 might be different after deactivating traps than it was before this patch.

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 4 ----
 arch/arm64/kvm/hyp/vhe/switch.c  | 4 ----
 2 files changed, 8 deletions(-)
arch/arm64/kvm/hyp/nvhe/switch.c +0 -4
index 2ea764a48958..1778593a08a9 100644
@@ -90,10 +90,6 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 		isb();
 	}
 
-	vcpu->arch.mdcr_el2_host &= MDCR_EL2_HPMN_MASK |
-				    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
-				    MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
-
 	__deactivate_traps_common(vcpu);
 
 	write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
arch/arm64/kvm/hyp/vhe/switch.c +0 -4
index ec158fa41ae6..0d0c9550fb08 100644
@@ -93,10 +93,6 @@ void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
 
 void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.mdcr_el2_host &= MDCR_EL2_HPMN_MASK |
-				    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
-				    MDCR_EL2_TPMS;
-
 	__deactivate_traps_common(vcpu);
 }
 
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#15 lore raw

PATCH v4 10/15 KVM: arm64: Add config register bit definitions

In reply to Fuad Tabba

Add hardware configuration register bit definitions for HCR_EL2 and MDCR_EL2. Future patches toggle these hyp configuration register bits to trap on certain accesses.

No functional change intended.

---
 arch/arm64/include/asm/kvm_arm.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/arm64/include/asm/kvm_arm.h +22 -0
index a928b2dc0b0f..327120c0089f 100644
@@ -12,8 +12,13 @@
 #include <asm/types.h>
 
 /* Hyp Configuration Register (HCR) bits */
+
+#define HCR_TID5	(UL(1) << 58)
+#define HCR_DCT		(UL(1) << 57)
 #define HCR_ATA_SHIFT	56
 #define HCR_ATA		(UL(1) << HCR_ATA_SHIFT)
+#define HCR_AMVOFFEN	(UL(1) << 51)
+#define HCR_FIEN	(UL(1) << 47)
 #define HCR_FWB		(UL(1) << 46)
 #define HCR_API		(UL(1) << 41)
 #define HCR_APK		(UL(1) << 40)
@@ -56,6 +61,7 @@
 #define HCR_PTW		(UL(1) << 2)
 #define HCR_SWIO	(UL(1) << 1)
 #define HCR_VM		(UL(1) << 0)
+#define HCR_RES0	((UL(1) << 48) | (UL(1) << 39))
 
 /*
  * The bits we set in HCR:
@@ -277,11 +283,21 @@
 #define CPTR_EL2_TZ	(1 << 8)
 #define CPTR_NVHE_EL2_RES1	0x000032ff /* known RES1 bits in CPTR_EL2 (nVHE) */
 #define CPTR_EL2_DEFAULT	CPTR_NVHE_EL2_RES1
+#define CPTR_NVHE_EL2_RES0	(GENMASK(63, 32) |	\
+				 GENMASK(29, 21) |	\
+				 GENMASK(19, 14) |	\
+				 BIT(11))
 
 /* Hyp Debug Configuration Register bits */
 #define MDCR_EL2_E2TB_MASK	(UL(0x3))
 #define MDCR_EL2_E2TB_SHIFT	(UL(24))
+#define MDCR_EL2_HPMFZS		(UL(1) << 36)
+#define MDCR_EL2_HPMFZO		(UL(1) << 29)
+#define MDCR_EL2_MTPME		(UL(1) << 28)
+#define MDCR_EL2_TDCC		(UL(1) << 27)
+#define MDCR_EL2_HCCD		(UL(1) << 23)
 #define MDCR_EL2_TTRF		(UL(1) << 19)
+#define MDCR_EL2_HPMD		(UL(1) << 17)
 #define MDCR_EL2_TPMS		(UL(1) << 14)
 #define MDCR_EL2_E2PB_MASK	(UL(0x3))
 #define MDCR_EL2_E2PB_SHIFT	(UL(12))
@@ -293,6 +309,12 @@
 #define MDCR_EL2_TPM		(UL(1) << 6)
 #define MDCR_EL2_TPMCR		(UL(1) << 5)
 #define MDCR_EL2_HPMN_MASK	(UL(0x1F))
+#define MDCR_EL2_RES0		(GENMASK(63, 37) |	\
+				 GENMASK(35, 30) |	\
+				 GENMASK(25, 24) |	\
+				 GENMASK(22, 20) |	\
+				 BIT(18) |		\
+				 GENMASK(16, 15))
 
 /* For compatibility with fault code shared with 32-bit */
 #define FSC_FAULT	ESR_ELx_FSC_FAULT
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#13 lore raw

PATCH v4 9/15 KVM: arm64: Add feature register flag definitions

In reply to Fuad Tabba

Add feature register flag definitions to clarify which features might be supported.

Consolidate the various ID_AA64PFR0_ELx flags for all ELs.

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  4 ++--
 arch/arm64/include/asm/sysreg.h     | 12 ++++++++----
 arch/arm64/kernel/cpufeature.c      |  8 ++++----
 3 files changed, 14 insertions(+), 10 deletions(-)
arch/arm64/include/asm/cpufeature.h +2 -2
index 9bb9d11750d7..b7d9bb17908d 100644
@@ -602,14 +602,14 @@ static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)
 {
 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SHIFT);
 
-	return val == ID_AA64PFR0_EL1_32BIT_64BIT;
+	return val == ID_AA64PFR0_ELx_32BIT_64BIT;
 }
 
 static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
 {
 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL0_SHIFT);
 
-	return val == ID_AA64PFR0_EL0_32BIT_64BIT;
+	return val == ID_AA64PFR0_ELx_32BIT_64BIT;
 }
 
 static inline bool id_aa64pfr0_sve(u64 pfr0)
arch/arm64/include/asm/sysreg.h +8 -4
index 53a93a9c5253..f84a00f5874d 100644
@@ -784,14 +784,13 @@
 #define ID_AA64PFR0_AMU			0x1
 #define ID_AA64PFR0_SVE			0x1
 #define ID_AA64PFR0_RAS_V1		0x1
+#define ID_AA64PFR0_RAS_V1P1		0x2
 #define ID_AA64PFR0_FP_NI		0xf
 #define ID_AA64PFR0_FP_SUPPORTED	0x0
 #define ID_AA64PFR0_ASIMD_NI		0xf
 #define ID_AA64PFR0_ASIMD_SUPPORTED	0x0
-#define ID_AA64PFR0_EL1_64BIT_ONLY	0x1
-#define ID_AA64PFR0_EL1_32BIT_64BIT	0x2
-#define ID_AA64PFR0_EL0_64BIT_ONLY	0x1
-#define ID_AA64PFR0_EL0_32BIT_64BIT	0x2
+#define ID_AA64PFR0_ELx_64BIT_ONLY	0x1
+#define ID_AA64PFR0_ELx_32BIT_64BIT	0x2
 
 /* id_aa64pfr1 */
 #define ID_AA64PFR1_MPAMFRAC_SHIFT	16
@@ -847,12 +846,16 @@
 #define ID_AA64MMFR0_ASID_SHIFT		4
 #define ID_AA64MMFR0_PARANGE_SHIFT	0
 
+#define ID_AA64MMFR0_ASID_8		0x0
+#define ID_AA64MMFR0_ASID_16		0x2
+
 #define ID_AA64MMFR0_TGRAN4_NI		0xf
 #define ID_AA64MMFR0_TGRAN4_SUPPORTED	0x0
 #define ID_AA64MMFR0_TGRAN64_NI		0xf
 #define ID_AA64MMFR0_TGRAN64_SUPPORTED	0x0
 #define ID_AA64MMFR0_TGRAN16_NI		0x0
 #define ID_AA64MMFR0_TGRAN16_SUPPORTED	0x1
+#define ID_AA64MMFR0_PARANGE_40		0x2
 #define ID_AA64MMFR0_PARANGE_48		0x5
 #define ID_AA64MMFR0_PARANGE_52		0x6
 
@@ -900,6 +903,7 @@
 #define ID_AA64MMFR2_CNP_SHIFT		0
 
 /* id_aa64dfr0 */
+#define ID_AA64DFR0_MTPMU_SHIFT		48
 #define ID_AA64DFR0_TRBE_SHIFT		44
 #define ID_AA64DFR0_TRACE_FILT_SHIFT	40
 #define ID_AA64DFR0_DOUBLELOCK_SHIFT	36
arch/arm64/kernel/cpufeature.c +4 -4
index 0ead8bfedf20..5b59fe5e26e4 100644
@@ -239,8 +239,8 @@ static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
 	S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
-	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
-	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_ELx_64BIT_ONLY),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_ELx_64BIT_ONLY),
 	ARM64_FTR_END,
 };
 
@@ -1956,7 +1956,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.sys_reg = SYS_ID_AA64PFR0_EL1,
 		.sign = FTR_UNSIGNED,
 		.field_pos = ID_AA64PFR0_EL0_SHIFT,
-		.min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
+		.min_field_value = ID_AA64PFR0_ELx_32BIT_64BIT,
 	},
 #ifdef CONFIG_KVM
 	{
@@ -1967,7 +1967,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.sys_reg = SYS_ID_AA64PFR0_EL1,
 		.sign = FTR_UNSIGNED,
 		.field_pos = ID_AA64PFR0_EL1_SHIFT,
-		.min_field_value = ID_AA64PFR0_EL1_32BIT_64BIT,
+		.min_field_value = ID_AA64PFR0_ELx_32BIT_64BIT,
 	},
 	{
 		.desc = "Protected KVM",
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#12 lore raw

PATCH v4 8/15 KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch

In reply to Fuad Tabba

Track the baseline guest value for cptr_el2 in struct kvm_vcpu_arch, similar to the other registers that control traps. Use this value when setting cptr_el2 for the guest.

Currently this value is unchanged (CPTR_EL2_DEFAULT), but future patches will set trapping bits based on features supported for the guest.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 1 +
 arch/arm64/kvm/arm.c              | 1 +
 arch/arm64/kvm/hyp/nvhe/switch.c  | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 76462c6a91ee..ac67d5699c68 100644
@@ -290,6 +290,7 @@ struct kvm_vcpu_arch {
 	/* Values of trap registers for the guest. */
 	u64 hcr_el2;
 	u64 mdcr_el2;
+	u64 cptr_el2;
 
 	/* Values of trap registers for the host before guest entry. */
 	u64 mdcr_el2_host;
arch/arm64/kvm/arm.c +1 -0
index e9a2b8f27792..14b12f2c08c0 100644
@@ -1104,6 +1104,7 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
 	}
 
 	vcpu_reset_hcr(vcpu);
+	vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
 
 	/*
 	 * Handle the "start in power-off" case.
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
index 1778593a08a9..86f3d6482935 100644
@@ -41,7 +41,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 	___activate_traps(vcpu);
 	__activate_traps_common(vcpu);
 
-	val = CPTR_EL2_DEFAULT;
+	val = vcpu->arch.cptr_el2;
 	val |= CPTR_EL2_TTA | CPTR_EL2_TAM;
 	if (!update_fp_enabled(vcpu)) {
 		val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#17 lore raw

PATCH v4 11/15 KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Fuad Tabba

Add an array of pointers to handlers for various trap reasons in nVHE code.

The current code selects how to fixup a guest on exit based on a series of if/else statements. Future patches will also require different handling for guest exists. Create an array of handlers to consolidate them.

No functional change intended as the array isn't populated yet.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 43 +++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c        | 33 +++++++++++++++++++
 2 files changed, 76 insertions(+)
arch/arm64/kvm/hyp/include/hyp/switch.h +43 -0
index a0e78a6027be..5a2b89b96c67 100644
@@ -409,6 +409,46 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu);
+
+static exit_handle_fn kvm_get_hyp_exit_handler(struct kvm_vcpu *vcpu)
+{
+	return is_nvhe_hyp_code() ? kvm_get_nvhe_exit_handler(vcpu) : NULL;
+}
+
+/*
+ * Allow the hypervisor to handle the exit with an exit handler if it has one.
+ *
+ * Returns true if the hypervisor handled the exit, and control should go back
+ * to the guest, or false if it hasn't.
+ */
+static bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu)
+{
+	bool is_handled = false;
+	exit_handle_fn exit_handler = kvm_get_hyp_exit_handler(vcpu);
+
+	if (exit_handler) {
+		/*
+		 * There's limited vcpu context here since it's not synced yet.
+		 * Ensure that relevant vcpu context that might be used by the
+		 * exit_handler is in sync before it's called and if handled.
+		 */
+		*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
+		*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
+
+		is_handled = exit_handler(vcpu);
+
+		if (is_handled) {
+			write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
+			write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
+		}
+	}
+
+	return is_handled;
+}
+
 /*
  * Return true when we were able to fixup the guest exit and should return to
  * the guest, false when we should restore the host state and return to the
@@ -496,6 +536,9 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
 			goto guest;
 	}
 
+	/* Check if there's an exit handler and allow it to handle the exit. */
+	if (kvm_hyp_handle_exit(vcpu))
+		goto guest;
 exit:
 	/* Return to the host kernel and handle the exit */
 	return false;
arch/arm64/kvm/hyp/nvhe/switch.c +33 -0
index 86f3d6482935..b7f25307a7b9 100644
@@ -158,6 +158,39 @@ static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
 		write_sysreg(pmu->events_host, pmcntenset_el0);
 }
 
+static exit_handle_fn hyp_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]		= NULL,
+	[ESR_ELx_EC_WFx]		= NULL,
+	[ESR_ELx_EC_CP15_32]		= NULL,
+	[ESR_ELx_EC_CP15_64]		= NULL,
+	[ESR_ELx_EC_CP14_MR]		= NULL,
+	[ESR_ELx_EC_CP14_LS]		= NULL,
+	[ESR_ELx_EC_CP14_64]		= NULL,
+	[ESR_ELx_EC_HVC32]		= NULL,
+	[ESR_ELx_EC_SMC32]		= NULL,
+	[ESR_ELx_EC_HVC64]		= NULL,
+	[ESR_ELx_EC_SMC64]		= NULL,
+	[ESR_ELx_EC_SYS64]		= NULL,
+	[ESR_ELx_EC_SVE]		= NULL,
+	[ESR_ELx_EC_IABT_LOW]		= NULL,
+	[ESR_ELx_EC_DABT_LOW]		= NULL,
+	[ESR_ELx_EC_SOFTSTP_LOW]	= NULL,
+	[ESR_ELx_EC_WATCHPT_LOW]	= NULL,
+	[ESR_ELx_EC_BREAKPT_LOW]	= NULL,
+	[ESR_ELx_EC_BKPT32]		= NULL,
+	[ESR_ELx_EC_BRK64]		= NULL,
+	[ESR_ELx_EC_FP_ASIMD]		= NULL,
+	[ESR_ELx_EC_PAC]		= NULL,
+};
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
+{
+	u32 esr = kvm_vcpu_get_esr(vcpu);
+	u8 esr_ec = ESR_ELx_EC(esr);
+
+	return hyp_exit_handlers[esr_ec];
+}
+
 /* Switch to the guest for legacy non-VHE systems */
 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 {
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#23 lore raw

PATCH v4 12/15 KVM: arm64: Add trap handlers for protected VMs

In reply to Fuad Tabba

Add trap handlers for protected VMs. These are mainly for Sys64 and debug traps.

No functional change intended as these are not hooked in yet to the guest exit handlers introduced earlier. So even when trapping is triggered, the exit handlers would let the host handle it, as before.

---
 arch/arm64/include/asm/kvm_fixed_config.h | 170 +++++++++
 arch/arm64/include/asm/kvm_host.h         |   2 +
 arch/arm64/include/asm/kvm_hyp.h          |   3 +
 arch/arm64/kvm/Makefile                   |   2 +-
 arch/arm64/kvm/arm.c                      |  11 +
 arch/arm64/kvm/hyp/nvhe/Makefile          |   2 +-
 arch/arm64/kvm/hyp/nvhe/sys_regs.c        | 430 ++++++++++++++++++++++
 arch/arm64/kvm/pkvm.c                     | 185 ++++++++++
 8 files changed, 803 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_fixed_config.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/sys_regs.c
 create mode 100644 arch/arm64/kvm/pkvm.c
arch/arm64/include/asm/kvm_fixed_config.h new file +170 -0
new file mode 100644
index 000000000000..40c18380156d
@@ -0,0 +1,170 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2021 Google LLC
+ * Author: Fuad Tabba <[email protected]>
+ */
+
+#ifndef __ARM64_KVM_FIXED_CONFIG_H__
+#define __ARM64_KVM_FIXED_CONFIG_H__
+
+#include <asm/sysreg.h>
+
+/*
+ * This file contains definitions for features to be allowed or restricted for
+ * guest virtual machines, depending on the mode KVM is running in and on the
+ * type of guest that is running.
+ *
+ * The ALLOW masks represent a bitmask of feature fields that are allowed
+ * without any restrictions as long as they are supported by the system.
+ *
+ * The RESTRICT_UNSIGNED masks, if present, represent unsigned fields for
+ * features that are restricted to support at most the specified feature.
+ *
+ * If a feature field is not present in either, than it is not supported.
+ *
+ * The approach taken for protected VMs is to allow features that are:
+ * - Needed by common Linux distributions (e.g., floating point)
+ * - Trivial to support, e.g., supporting the feature does not introduce or
+ * require tracking of additional state in KVM
+ * - Cannot be trapped or prevent the guest from using anyway
+ */
+
+/*
+ * Allow for protected VMs:
+ * - Floating-point and Advanced SIMD
+ * - Data Independent Timing
+ */
+#define PVM_ID_AA64PFR0_ALLOW (\
+	ARM64_FEATURE_MASK(ID_AA64PFR0_FP) | \
+	ARM64_FEATURE_MASK(ID_AA64PFR0_ASIMD) | \
+	ARM64_FEATURE_MASK(ID_AA64PFR0_DIT) \
+	)
+
+/*
+ * Restrict to the following *unsigned* features for protected VMs:
+ * - AArch64 guests only (no support for AArch32 guests):
+ *	AArch32 adds complexity in trap handling, emulation, condition codes,
+ *	etc...
+ * - RAS (v1)
+ *	Supported by KVM
+ */
+#define PVM_ID_AA64PFR0_RESTRICT_UNSIGNED (\
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL0), ID_AA64PFR0_ELx_64BIT_ONLY) | \
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1), ID_AA64PFR0_ELx_64BIT_ONLY) | \
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL2), ID_AA64PFR0_ELx_64BIT_ONLY) | \
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL3), ID_AA64PFR0_ELx_64BIT_ONLY) | \
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_RAS), ID_AA64PFR0_RAS_V1) \
+	)
+
+/*
+ * Allow for protected VMs:
+ * - Branch Target Identification
+ * - Speculative Store Bypassing
+ */
+#define PVM_ID_AA64PFR1_ALLOW (\
+	ARM64_FEATURE_MASK(ID_AA64PFR1_BT) | \
+	ARM64_FEATURE_MASK(ID_AA64PFR1_SSBS) \
+	)
+
+/*
+ * No support for Scalable Vectors for protected VMs:
+ *	Requires additional support from KVM, e.g., context-switching and
+ *	trapping at EL2
+ */
+#define PVM_ID_AA64ZFR0_ALLOW (0ULL)
+
+/*
+ * No support for debug, including breakpoints, and watchpoints for protected
+ * VMs:
+ *	The Arm architecture mandates support for at least the Armv8 debug
+ *	architecture, which would include at least 2 hardware breakpoints and
+ *	watchpoints. Providing that support to protected guests adds
+ *	considerable state and complexity. Therefore, the reserved value of 0 is
+ *	used for debug-related fields.
+ */
+#define PVM_ID_AA64DFR0_ALLOW (0ULL)
+
+/*
+ * Allow for protected VMs:
+ * - Mixed-endian
+ * - Distinction between Secure and Non-secure Memory
+ * - Mixed-endian at EL0 only
+ * - Non-context synchronizing exception entry and exit
+ */
+#define PVM_ID_AA64MMFR0_ALLOW (\
+	ARM64_FEATURE_MASK(ID_AA64MMFR0_BIGENDEL) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR0_SNSMEM) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR0_BIGENDEL0) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR0_EXS) \
+	)
+
+/*
+ * Restrict to the following *unsigned* features for protected VMs:
+ * - 40-bit IPA
+ * - 16-bit ASID
+ */
+#define PVM_ID_AA64MMFR0_RESTRICT_UNSIGNED (\
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64MMFR0_PARANGE), ID_AA64MMFR0_PARANGE_40) | \
+	FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64MMFR0_ASID), ID_AA64MMFR0_ASID_16) \
+	)
+
+/*
+ * Allow for protected VMs:
+ * - Hardware translation table updates to Access flag and Dirty state
+ * - Number of VMID bits from CPU
+ * - Hierarchical Permission Disables
+ * - Privileged Access Never
+ * - SError interrupt exceptions from speculative reads
+ * - Enhanced Translation Synchronization
+ */
+#define PVM_ID_AA64MMFR1_ALLOW (\
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_HADBS) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_VMIDBITS) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_HPD) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_PAN) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_SPECSEI) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR1_ETS) \
+	)
+
+/*
+ * Allow for protected VMs:
+ * - Common not Private translations
+ * - User Access Override
+ * - IESB bit in the SCTLR_ELx registers
+ * - Unaligned single-copy atomicity and atomic functions
+ * - ESR_ELx.EC value on an exception by read access to feature ID space
+ * - TTL field in address operations.
+ * - Break-before-make sequences when changing translation block size
+ * - E0PDx mechanism
+ */
+#define PVM_ID_AA64MMFR2_ALLOW (\
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_CNP) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_UAO) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_IESB) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_AT) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_IDS) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_TTL) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_BBM) | \
+	ARM64_FEATURE_MASK(ID_AA64MMFR2_E0PD) \
+	)
+
+/*
+ * Allow for protected VMs all features in this register:
+ * - LS64
+ * - XS
+ * - I8MM
+ * - DGB
+ * - BF16
+ * - SPECRES
+ * - SB
+ * - FRINTTS
+ * - PAuth
+ * - FPAC
+ * - LRCPC
+ * - FCMA
+ * - JSCVT
+ * - DPB
+ */
+#define PVM_ID_AA64ISAR1_ALLOW (~0ULL)
+
+#endif /* __ARM64_KVM_FIXED_CONFIG_H__ */
arch/arm64/include/asm/kvm_host.h +2 -0
index ac67d5699c68..e1ceadd69575 100644
@@ -780,6 +780,8 @@ static inline bool kvm_vm_is_protected(struct kvm *kvm)
 	return false;
 }
 
+void kvm_init_protected_traps(struct kvm_vcpu *vcpu);
+
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 
arch/arm64/include/asm/kvm_hyp.h +3 -0
index 657d0c94cf82..3f4866322f85 100644
@@ -115,7 +115,10 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus,
 void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
 #endif
 
+extern u64 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val);
+extern u64 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val);
 extern u64 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val);
 extern u64 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val);
+extern u64 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val);
 
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/Makefile +1 -1
index 989bb5dad2c8..0be63f5c495f 100644
@@ -14,7 +14,7 @@ kvm-y := $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o \
 	 $(KVM)/vfio.o $(KVM)/irqchip.o $(KVM)/binary_stats.o \
 	 arm.o mmu.o mmio.o psci.o perf.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o \
-	 guest.o debug.o reset.o sys_regs.o \
+	 guest.o debug.o pkvm.o reset.o sys_regs.o \
 	 vgic-sys-reg-v3.o fpsimd.o pmu.o \
 	 arch_timer.o trng.o\
 	 vgic/vgic.o vgic/vgic-init.o \
arch/arm64/kvm/arm.c +11 -0
index 14b12f2c08c0..75fff5cd6a6c 100644
@@ -618,6 +618,14 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
 
 	ret = kvm_arm_pmu_v3_enable(vcpu);
 
+	/*
+	 * Initialize traps for protected VMs.
+	 * NOTE: Move trap initialization to EL2 once the code is in place for
+	 * maintaining protected VM state at EL2 instead of the host.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		kvm_init_protected_traps(vcpu);
+
 	return ret;
 }
 
@@ -1781,8 +1789,11 @@ static int kvm_hyp_init_protection(u32 hyp_va_bits)
 	void *addr = phys_to_virt(hyp_mem_base);
 	int ret;
 
+	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
+	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
 
 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
 	if (ret)
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
index 5df6193fc430..a23f417a0c20 100644
@@ -14,7 +14,7 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs))
 
 obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \
-	 cache.o setup.o mm.o mem_protect.o
+	 cache.o setup.o mm.o mem_protect.o sys_regs.o
 obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
 obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/sys_regs.c new file +430 -0

430 lines not on this page · raw message

arch/arm64/kvm/pkvm.c new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#26 lore raw

PATCH v4 13/15 KVM: arm64: Move sanitized copies of CPU features

In reply to Fuad Tabba

Move the sanitized copies of the CPU feature registers to the recently created sys_regs.c. This consolidates all copies in a more relevant file.

No functional change intended.

---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 6 ------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c    | 2 ++
 2 files changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -6
index d938ce95d3bd..925c7db7fa34 100644
@@ -25,12 +25,6 @@ struct host_kvm host_kvm;
 
 static struct hyp_pool host_s2_pool;
 
-/*
- * Copies of the host's CPU features registers holding sanitized values.
- */
-u64 id_aa64mmfr0_el1_sys_val;
-u64 id_aa64mmfr1_el1_sys_val;
-
 static const u8 pkvm_hyp_id = 1;
 
 static void *host_s2_zalloc_pages_exact(size_t size)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -0
index cd126d45cbcc..d641bae0467d 100644
@@ -20,6 +20,8 @@
  */
 u64 id_aa64pfr0_el1_sys_val;
 u64 id_aa64pfr1_el1_sys_val;
+u64 id_aa64mmfr0_el1_sys_val;
+u64 id_aa64mmfr1_el1_sys_val;
 u64 id_aa64mmfr2_el1_sys_val;
 
 /*
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
author
#27 lore raw

PATCH v4 14/15 KVM: arm64: Trap access to pVM restricted features

In reply to Fuad Tabba

Trap accesses to restricted features for VMs running in protected mode.

Access to feature registers are emulated, and only supported features are exposed to protected VMs.

Accesses to restricted registers as well as restricted instructions are trapped, and an undefined exception is injected into the protected guests, i.e., with EC = 0x0 (unknown reason). This EC is the one used, according to the Arm Architecture Reference Manual, for unallocated or undefined system registers or instructions.

Only affects the functionality of protected VMs. Otherwise, should not affect non-protected VMs when KVM is running in protected mode.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h |  3 +++
 arch/arm64/kvm/hyp/nvhe/switch.c        | 34 ++++++++++++++-----------
 2 files changed, 22 insertions(+), 15 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +3 -0
index 5a2b89b96c67..8431f1514280 100644
@@ -33,6 +33,9 @@
 extern struct exception_table_entry __start___kvm_ex_table;
 extern struct exception_table_entry __stop___kvm_ex_table;
 
+int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu);
+int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu);
+
 /* Check whether the FP regs were dirtied while in the host-side run loop: */
 static inline bool update_fp_enabled(struct kvm_vcpu *vcpu)
 {
arch/arm64/kvm/hyp/nvhe/switch.c +19 -15
index b7f25307a7b9..398e62098898 100644
@@ -159,27 +159,27 @@ static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
 }
 
 static exit_handle_fn hyp_exit_handlers[] = {
-	[0 ... ESR_ELx_EC_MAX]		= NULL,
+	[0 ... ESR_ELx_EC_MAX]		= kvm_handle_pvm_restricted,
 	[ESR_ELx_EC_WFx]		= NULL,
-	[ESR_ELx_EC_CP15_32]		= NULL,
-	[ESR_ELx_EC_CP15_64]		= NULL,
-	[ESR_ELx_EC_CP14_MR]		= NULL,
-	[ESR_ELx_EC_CP14_LS]		= NULL,
-	[ESR_ELx_EC_CP14_64]		= NULL,
+	[ESR_ELx_EC_CP15_32]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_CP15_64]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_CP14_MR]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_CP14_LS]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_CP14_64]		= kvm_handle_pvm_restricted,
 	[ESR_ELx_EC_HVC32]		= NULL,
 	[ESR_ELx_EC_SMC32]		= NULL,
 	[ESR_ELx_EC_HVC64]		= NULL,
 	[ESR_ELx_EC_SMC64]		= NULL,
-	[ESR_ELx_EC_SYS64]		= NULL,
-	[ESR_ELx_EC_SVE]		= NULL,
+	[ESR_ELx_EC_SYS64]		= kvm_handle_pvm_sys64,
+	[ESR_ELx_EC_SVE]		= kvm_handle_pvm_restricted,
 	[ESR_ELx_EC_IABT_LOW]		= NULL,
 	[ESR_ELx_EC_DABT_LOW]		= NULL,
-	[ESR_ELx_EC_SOFTSTP_LOW]	= NULL,
-	[ESR_ELx_EC_WATCHPT_LOW]	= NULL,
-	[ESR_ELx_EC_BREAKPT_LOW]	= NULL,
-	[ESR_ELx_EC_BKPT32]		= NULL,
-	[ESR_ELx_EC_BRK64]		= NULL,
-	[ESR_ELx_EC_FP_ASIMD]		= NULL,
+	[ESR_ELx_EC_SOFTSTP_LOW]	= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_WATCHPT_LOW]	= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_BREAKPT_LOW]	= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_BKPT32]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_BRK64]		= kvm_handle_pvm_restricted,
+	[ESR_ELx_EC_FP_ASIMD]		= kvm_handle_pvm_restricted,
 	[ESR_ELx_EC_PAC]		= NULL,
 };
 
@@ -188,7 +188,11 @@ exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
 	u32 esr = kvm_vcpu_get_esr(vcpu);
 	u8 esr_ec = ESR_ELx_EC(esr);
 
-	return hyp_exit_handlers[esr_ec];
+	/* For now, only protected VMs have exit handlers. */
+	if (unlikely(kvm_vm_is_protected(kern_hyp_va(vcpu->kvm))))
+		return hyp_exit_handlers[esr_ec];
+	else
+		return NULL;
 }
 
 /* Switch to the guest for legacy non-VHE systems */
Signature · 6 lines
2.33.0.rc1.237.g0d66db33f3-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author
#29 lore raw

PATCH v4 15/15 KVM: arm64: Handle protected guests at 32 bits

In reply to Fuad Tabba

Protected KVM does not support protected AArch32 guests. However, it is possible for the guest to force run AArch32, potentially causing problems. Add an extra check so that if the hypervisor catches the guest doing that, it can prevent the guest from running again by resetting vcpu->arch.target and returning ARM_EXCEPTION_IL.

If this were to happen, The VMM can try and fix it by re- initializing the vcpu with KVM_ARM_VCPU_INIT, however, this is likely not possible for protected VMs.

Adapted from commit 22f553842b14 ("KVM: arm64: Handle Asymmetric AArch32 systems")

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 37 ++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
arch/arm64/kvm/hyp/nvhe/switch.c +37 -0
index 398e62098898..0c24b7f473bf 100644
@@ -20,6 +20,7 @@
 #include <asm/kprobes.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
+#include <asm/kvm_fixed_config.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_mmu.h>
 #include <asm/fpsimd.h>
@@ -195,6 +196,39 @@ exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
 		return NULL;
 }
 
+/*
+ * Some guests (e.g., protected VMs) might not be allowed to run in AArch32. The
+ * check below is based on the one in kvm_arch_vcpu_ioctl_run().
+ * The ARMv8 architecture does not give the hypervisor a mechanism to prevent a
+ * guest from dropping to AArch32 EL0 if implemented by the CPU. If the
+ * hypervisor spots a guest in such a state ensure it is handled, and don't
+ * trust the host to spot or fix it.
+ *
+ * Returns true if the check passed and the guest run loop can continue, or
+ * false if the guest should exit to the host.
+ */
+static bool check_aarch32_guest(struct kvm_vcpu *vcpu, u64 *exit_code)
+{
+	if (kvm_vm_is_protected(kern_hyp_va(vcpu->kvm)) &&
+	    vcpu_mode_is_32bit(vcpu) &&
+	    FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL0),
+					 PVM_ID_AA64PFR0_RESTRICT_UNSIGNED) <
+		ID_AA64PFR0_ELx_32BIT_64BIT) {
+		/*
+		 * As we have caught the guest red-handed, decide that it isn't
+		 * fit for purpose anymore by making the vcpu invalid. The VMM
+		 * can try and fix it by re-initializing the vcpu with
+		 * KVM_ARM_VCPU_INIT, however, this is likely not possible for
+		 * protected VMs.
+		 */
+		vcpu->arch.target = -1;
+		*exit_code = ARM_EXCEPTION_IL;
+		return false;
+	}
+
+	return true;
+}
+
 /* Switch to the guest for legacy non-VHE systems */
 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 {
@@ -255,6 +289,9 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 		/* Jump in the fire! */
 		exit_code = __guest_enter(vcpu);
 
+		if (unlikely(!check_aarch32_guest(vcpu, &exit_code)))
+			break;
+
 		/* And we're baaack! */
 	} while (fixup_guest_exit(vcpu, &exit_code));
 
Signature · 6 lines
2.33.0.rc1.237.g0d66db33f3-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#24 lore raw

Re: [PATCH v4 12/15] KVM: arm64: Add trap handlers for protected VMs

In reply to Fuad Tabba
Thank you for the patch! Perhaps something to improve:

Hi Fuad,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on c500bee1c5b2f1d59b1081ac879d73268ab0ff17]

url:    https://github.com/0day-ci/linux/commits/Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500
base:   c500bee1c5b2f1d59b1081ac879d73268ab0ff17
config: arm64-allyesconfig (attached as .config)
compiler: aarch64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/58bc1e79b35b3353db259b152a450671bc0822b2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500 git checkout 58bc1e79b35b3353db259b152a450671bc0822b2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/kvm/hyp/nvhe/sys_regs.c:390:5: warning: no previous prototype for 'kvm_handle_pvm_sys64' [-Wmissing-prototypes]
     390 | int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu)
         |     ^~~~~~~~~~~~~~~~~~~~
>> arch/arm64/kvm/hyp/nvhe/sys_regs.c:426:5: warning: no previous prototype for 'kvm_handle_pvm_restricted' [-Wmissing-prototypes]

426 | int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~~~

vim +/kvm_handle_pvm_sys64 +390 arch/arm64/kvm/hyp/nvhe/sys_regs.c

384
385 /*
386 * Handler for protected VM MSR, MRS or System instruction execution in AArch64.
387 *
388 * Return 1 if handled, or 0 if not.
389 */

 > 390	int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu)
   391	{
   392		const struct sys_reg_desc *r;
   393		struct sys_reg_params params;
   394		unsigned long esr = kvm_vcpu_get_esr(vcpu);
   395		int Rt = kvm_vcpu_sys_get_rt(vcpu);
   396	
   397		params = esr_sys64_to_params(esr);
   398		params.regval = vcpu_get_reg(vcpu, Rt);
   399	
   400		r = find_reg(&params, pvm_sys_reg_descs, ARRAY_SIZE(pvm_sys_reg_descs));
   401	
   402		/* Undefined access (RESTRICTED). */
   403		if (r == NULL) {
   404			inject_undef(vcpu);
   405			return 1;
   406		}
   407	
   408		/* Handled by the host (HOST_HANDLED) */
   409		if (r->access == NULL)
   410			return 0;
   411	
   412		/* Handled by hyp: skip instruction if instructed to do so. */
   413		if (r->access(vcpu, &params, r))
   414			__kvm_skip_instr(vcpu);
   415	
   416		vcpu_set_reg(vcpu, Rt, params.regval);
   417		return 1;
   418	}
   419	
   420	/*
   421	 * Handler for protected VM restricted exceptions.
   422	 *
   423	 * Inject an undefined exception into the guest and return 1 to indicate that
   424	 * it was handled.
   425	 */
 > 426	int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#18 lore raw

Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Fuad Tabba
Thank you for the patch! Perhaps something to improve:

Hi Fuad,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on c500bee1c5b2f1d59b1081ac879d73268ab0ff17]

url:    https://github.com/0day-ci/linux/commits/Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500
base:   c500bee1c5b2f1d59b1081ac879d73268ab0ff17
config: arm64-randconfig-r016-20210816 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/d20adc963041fb77c56e8da7935236d73b738e63
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500 git checkout d20adc963041fb77c56e8da7935236d73b738e63
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/kvm/hyp/nvhe/switch.c:163:22: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_WFx]                = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:164:26: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_CP15_32]            = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:165:26: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_CP15_64]            = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:166:26: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_CP14_MR]            = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:167:26: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_CP14_LS]            = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:168:26: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_CP14_64]            = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:169:24: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_HVC32]              = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:170:24: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [ESR_ELx_EC_SMC32]              = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:162:28: note: previous initialization is here
           [0 ... ESR_ELx_EC_MAX]          = NULL,
                                             ^~~~
   include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
   #define NULL ((void *)0)
                ^~~~~~~~~~~

arch/arm64/kvm/hyp/nvhe/switch.c:171:24: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[ESR_ELx_EC_HVC64] = NULL,
^~~~
include/linux/stddef.h:8:14: note: expanded from macro 'NULL'
#define NULL ((void *)0)

vim +163 arch/arm64/kvm/hyp/nvhe/switch.c

160
161 static exit_handle_fn hyp_exit_handlers[] = {
162 [0 ... ESR_ELx_EC_MAX] = NULL,

 > 163		[ESR_ELx_EC_WFx]		= NULL,

164 [ESR_ELx_EC_CP15_32] = NULL,
165 [ESR_ELx_EC_CP15_64] = NULL,
166 [ESR_ELx_EC_CP14_MR] = NULL,
167 [ESR_ELx_EC_CP14_LS] = NULL,
168 [ESR_ELx_EC_CP14_64] = NULL,
169 [ESR_ELx_EC_HVC32] = NULL,
170 [ESR_ELx_EC_SMC32] = NULL,
171 [ESR_ELx_EC_HVC64] = NULL,
172 [ESR_ELx_EC_SMC64] = NULL,
173 [ESR_ELx_EC_SYS64] = NULL,
174 [ESR_ELx_EC_SVE] = NULL,
175 [ESR_ELx_EC_IABT_LOW] = NULL,
176 [ESR_ELx_EC_DABT_LOW] = NULL,
177 [ESR_ELx_EC_SOFTSTP_LOW] = NULL,
178 [ESR_ELx_EC_WATCHPT_LOW] = NULL,
179 [ESR_ELx_EC_BREAKPT_LOW] = NULL,
180 [ESR_ELx_EC_BKPT32] = NULL,
181 [ESR_ELx_EC_BRK64] = NULL,
182 [ESR_ELx_EC_FP_ASIMD] = NULL,
183 [ESR_ELx_EC_PAC] = NULL,
184 };
185

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#25 lore raw

Re: [PATCH v4 12/15] KVM: arm64: Add trap handlers for protected VMs

In reply to Fuad Tabba
Thank you for the patch! Perhaps something to improve:

Hi Fuad,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on c500bee1c5b2f1d59b1081ac879d73268ab0ff17]

url:    https://github.com/0day-ci/linux/commits/Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500
base:   c500bee1c5b2f1d59b1081ac879d73268ab0ff17
config: arm64-randconfig-r016-20210816 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/58bc1e79b35b3353db259b152a450671bc0822b2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500 git checkout 58bc1e79b35b3353db259b152a450671bc0822b2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/kvm/hyp/nvhe/sys_regs.c:390:5: warning: no previous prototype for function 'kvm_handle_pvm_sys64' [-Wmissing-prototypes]
   int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu)
       ^
   arch/arm64/kvm/hyp/nvhe/sys_regs.c:390:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu)
   ^
   static 
>> arch/arm64/kvm/hyp/nvhe/sys_regs.c:426:5: warning: no previous prototype for function 'kvm_handle_pvm_restricted' [-Wmissing-prototypes]
   int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu)
       ^

arch/arm64/kvm/hyp/nvhe/sys_regs.c:426:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu)
^
static
2 warnings generated.

vim +/kvm_handle_pvm_sys64 +390 arch/arm64/kvm/hyp/nvhe/sys_regs.c

384
385 /*
386 * Handler for protected VM MSR, MRS or System instruction execution in AArch64.
387 *
388 * Return 1 if handled, or 0 if not.
389 */

 > 390	int kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu)
   391	{
   392		const struct sys_reg_desc *r;
   393		struct sys_reg_params params;
   394		unsigned long esr = kvm_vcpu_get_esr(vcpu);
   395		int Rt = kvm_vcpu_sys_get_rt(vcpu);
   396	
   397		params = esr_sys64_to_params(esr);
   398		params.regval = vcpu_get_reg(vcpu, Rt);
   399	
   400		r = find_reg(&params, pvm_sys_reg_descs, ARRAY_SIZE(pvm_sys_reg_descs));
   401	
   402		/* Undefined access (RESTRICTED). */
   403		if (r == NULL) {
   404			inject_undef(vcpu);
   405			return 1;
   406		}
   407	
   408		/* Handled by the host (HOST_HANDLED) */
   409		if (r->access == NULL)
   410			return 0;
   411	
   412		/* Handled by hyp: skip instruction if instructed to do so. */
   413		if (r->access(vcpu, &params, r))
   414			__kvm_skip_instr(vcpu);
   415	
   416		vcpu_set_reg(vcpu, Rt, params.regval);
   417		return 1;
   418	}
   419	
   420	/*
   421	 * Handler for protected VM restricted exceptions.
   422	 *
   423	 * Inject an undefined exception into the guest and return 1 to indicate that
   424	 * it was handled.
   425	 */
 > 426	int kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#28 lore raw

Re: [PATCH v4 14/15] KVM: arm64: Trap access to pVM restricted features

In reply to Fuad Tabba
Thank you for the patch! Perhaps something to improve:

Hi Fuad,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on c500bee1c5b2f1d59b1081ac879d73268ab0ff17]

url:    https://github.com/0day-ci/linux/commits/Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500
base:   c500bee1c5b2f1d59b1081ac879d73268ab0ff17
config: arm64-allyesconfig (attached as .config)
compiler: aarch64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c05d33084d216fbdd94a7165aa6e93fd7517e0d8
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Fuad-Tabba/KVM-arm64-Fixed-features-for-protected-VMs/20210817-161500 git checkout c05d33084d216fbdd94a7165aa6e93fd7517e0d8
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from include/uapi/linux/posix_types.h:5,

                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kvm_host.h:6,
                    from arch/arm64/include/asm/kvm_emulate.h:14,
                    from arch/arm64/kvm/hyp/include/hyp/adjust_pc.h:13,
                    from arch/arm64/kvm/hyp/include/hyp/switch.h:10,
                    from arch/arm64/kvm/hyp/nvhe/switch.c:7:
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:163:43: note: in expansion of macro 'NULL'
     163 |         [ESR_ELx_EC_WFx]                = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[1]')
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:163:43: note: in expansion of macro 'NULL'
     163 |         [ESR_ELx_EC_WFx]                = NULL,
         |                                           ^~~~
>> arch/arm64/kvm/hyp/nvhe/switch.c:164:43: warning: initialized field overwritten [-Woverride-init]
     164 |         [ESR_ELx_EC_CP15_32]            = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:164:43: note: (near initialization for 'hyp_exit_handlers[3]')
   arch/arm64/kvm/hyp/nvhe/switch.c:165:43: warning: initialized field overwritten [-Woverride-init]
     165 |         [ESR_ELx_EC_CP15_64]            = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:165:43: note: (near initialization for 'hyp_exit_handlers[4]')
   arch/arm64/kvm/hyp/nvhe/switch.c:166:43: warning: initialized field overwritten [-Woverride-init]
     166 |         [ESR_ELx_EC_CP14_MR]            = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:166:43: note: (near initialization for 'hyp_exit_handlers[5]')
   arch/arm64/kvm/hyp/nvhe/switch.c:167:43: warning: initialized field overwritten [-Woverride-init]
     167 |         [ESR_ELx_EC_CP14_LS]            = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:167:43: note: (near initialization for 'hyp_exit_handlers[6]')
   arch/arm64/kvm/hyp/nvhe/switch.c:168:43: warning: initialized field overwritten [-Woverride-init]
     168 |         [ESR_ELx_EC_CP14_64]            = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:168:43: note: (near initialization for 'hyp_exit_handlers[12]')
   In file included from include/uapi/linux/posix_types.h:5,
                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kvm_host.h:6,
                    from arch/arm64/include/asm/kvm_emulate.h:14,
                    from arch/arm64/kvm/hyp/include/hyp/adjust_pc.h:13,
                    from arch/arm64/kvm/hyp/include/hyp/switch.h:10,
                    from arch/arm64/kvm/hyp/nvhe/switch.c:7:
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:169:43: note: in expansion of macro 'NULL'
     169 |         [ESR_ELx_EC_HVC32]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[18]')
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:169:43: note: in expansion of macro 'NULL'
     169 |         [ESR_ELx_EC_HVC32]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:170:43: note: in expansion of macro 'NULL'
     170 |         [ESR_ELx_EC_SMC32]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[19]')
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:170:43: note: in expansion of macro 'NULL'
     170 |         [ESR_ELx_EC_SMC32]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:171:43: note: in expansion of macro 'NULL'
     171 |         [ESR_ELx_EC_HVC64]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[22]')
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:171:43: note: in expansion of macro 'NULL'
     171 |         [ESR_ELx_EC_HVC64]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:172:43: note: in expansion of macro 'NULL'
     172 |         [ESR_ELx_EC_SMC64]              = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[23]')
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:172:43: note: in expansion of macro 'NULL'
     172 |         [ESR_ELx_EC_SMC64]              = NULL,
         |                                           ^~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:173:43: warning: initialized field overwritten [-Woverride-init]
     173 |         [ESR_ELx_EC_SYS64]              = kvm_handle_pvm_sys64,
         |                                           ^~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:173:43: note: (near initialization for 'hyp_exit_handlers[24]')
   arch/arm64/kvm/hyp/nvhe/switch.c:174:43: warning: initialized field overwritten [-Woverride-init]
     174 |         [ESR_ELx_EC_SVE]                = kvm_handle_pvm_restricted,
         |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/arm64/kvm/hyp/nvhe/switch.c:174:43: note: (near initialization for 'hyp_exit_handlers[25]')
   In file included from include/uapi/linux/posix_types.h:5,
                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kvm_host.h:6,
                    from arch/arm64/include/asm/kvm_emulate.h:14,
                    from arch/arm64/kvm/hyp/include/hyp/adjust_pc.h:13,
                    from arch/arm64/kvm/hyp/include/hyp/switch.h:10,
                    from arch/arm64/kvm/hyp/nvhe/switch.c:7:
   include/linux/stddef.h:8:14: warning: initialized field overwritten [-Woverride-init]
       8 | #define NULL ((void *)0)
         |              ^
   arch/arm64/kvm/hyp/nvhe/switch.c:175:43: note: in expansion of macro 'NULL'
     175 |         [ESR_ELx_EC_IABT_LOW]           = NULL,
         |                                           ^~~~
   include/linux/stddef.h:8:14: note: (near initialization for 'hyp_exit_handlers[32]')
       8 | #define NULL ((void *)0)
         |              ^

vim +164 arch/arm64/kvm/hyp/nvhe/switch.c

160
161 static exit_handle_fn hyp_exit_handlers[] = {
162 [0 ... ESR_ELx_EC_MAX] = kvm_handle_pvm_restricted,

 > 163		[ESR_ELx_EC_WFx]		= NULL,
 > 164		[ESR_ELx_EC_CP15_32]		= kvm_handle_pvm_restricted,

165 [ESR_ELx_EC_CP15_64] = kvm_handle_pvm_restricted,
166 [ESR_ELx_EC_CP14_MR] = kvm_handle_pvm_restricted,
167 [ESR_ELx_EC_CP14_LS] = kvm_handle_pvm_restricted,
168 [ESR_ELx_EC_CP14_64] = kvm_handle_pvm_restricted,
169 [ESR_ELx_EC_HVC32] = NULL,
170 [ESR_ELx_EC_SMC32] = NULL,
171 [ESR_ELx_EC_HVC64] = NULL,
172 [ESR_ELx_EC_SMC64] = NULL,
173 [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64,
174 [ESR_ELx_EC_SVE] = kvm_handle_pvm_restricted,
175 [ESR_ELx_EC_IABT_LOW] = NULL,
176 [ESR_ELx_EC_DABT_LOW] = NULL,
177 [ESR_ELx_EC_SOFTSTP_LOW] = kvm_handle_pvm_restricted,
178 [ESR_ELx_EC_WATCHPT_LOW] = kvm_handle_pvm_restricted,
179 [ESR_ELx_EC_BREAKPT_LOW] = kvm_handle_pvm_restricted,
180 [ESR_ELx_EC_BKPT32] = kvm_handle_pvm_restricted,
181 [ESR_ELx_EC_BRK64] = kvm_handle_pvm_restricted,
182 [ESR_ELx_EC_FP_ASIMD] = kvm_handle_pvm_restricted,
183 [ESR_ELx_EC_PAC] = NULL,
184 };
185

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer Acked-by on v4 6/15
#8 lore raw

Re: [PATCH v4 06/15] KVM: arm64: Restore mdcr_el2 from vcpu

In reply to Fuad Tabba
Quoted 12 lines · Fuad Tabba

On deactivating traps, restore the value of mdcr_el2 from the newly created and preserved host value vcpu context, rather than directly reading the hardware register.

Up until and including this patch the two values are the same, i.e., the hardware register and the vcpu one. A future patch will be changing the value of mdcr_el2 on activating traps, and this ensures that its value will be restored.

No functional change intended.

Quoted 8 lines
---
 arch/arm64/include/asm/kvm_host.h       |  5 ++++-
 arch/arm64/include/asm/kvm_hyp.h        |  2 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h |  6 +++++-
 arch/arm64/kvm/hyp/nvhe/switch.c        | 13 +++++--------
 arch/arm64/kvm/hyp/vhe/switch.c         | 14 +++++---------
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c      |  2 +-
 6 files changed, 21 insertions(+), 21 deletions(-)

Will

maintainer Acked-by on v4 7/15
#11 lore raw

Re: [PATCH v4 07/15] KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug

In reply to Fuad Tabba
Quoted 7 lines · Fuad Tabba

__init_el2_debug configures mdcr_el2 at initialization based on, among other things, available hardware support. Trap deactivation doesn't check that, so keep the initial value.

No functional change intended. However, the value of mdcr_el2 might be different after deactivating traps than it was before this patch.

I think this sentence is very confusing, so I'd remove it. I also don't think it's correct, as the EL2 initialisation code only manipulates the bits which are being masked here.

So with that sentence removed:

Will

maintainer Acked-by on v4 9/15
#14 lore raw

Re: [PATCH v4 09/15] KVM: arm64: Add feature register flag definitions

In reply to Fuad Tabba
Quoted 5 lines · Fuad Tabba

Add feature register flag definitions to clarify which features might be supported.

Consolidate the various ID_AA64PFR0_ELx flags for all ELs.

Quoted 8 lines

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  4 ++--
 arch/arm64/include/asm/sysreg.h     | 12 ++++++++----
 arch/arm64/kernel/cpufeature.c      |  8 ++++----
 3 files changed, 14 insertions(+), 10 deletions(-)

Thanks, looks better now:

Will

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#4 lore raw

Re: [PATCH v4 03/15] KVM: arm64: MDCR_EL2 is a 64-bit register

In reply to Fuad Tabba
Quoted 68 lines · Fuad Tabba

Fix the places in KVM that treat MDCR_EL2 as a 32-bit register. More recent features (e.g., FEAT_SPEv1p2) use bits above 31.

No functional change intended.

---
 arch/arm64/include/asm/kvm_arm.h   | 20 ++++++++++----------
 arch/arm64/include/asm/kvm_asm.h   |  2 +-
 arch/arm64/include/asm/kvm_host.h  |  2 +-
 arch/arm64/kvm/debug.c             |  2 +-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c |  2 +-
 arch/arm64/kvm/hyp/vhe/debug-sr.c  |  2 +-
 6 files changed, 15 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_arm.h +10 -10
index d436831dd706..6a523ec83415 100644
@@ -281,18 +281,18 @@
 /* Hyp Debug Configuration Register bits */
 #define MDCR_EL2_E2TB_MASK	(UL(0x3))
 #define MDCR_EL2_E2TB_SHIFT	(UL(24))
-#define MDCR_EL2_TTRF		(1 << 19)
-#define MDCR_EL2_TPMS		(1 << 14)
+#define MDCR_EL2_TTRF		(UL(1) << 19)
+#define MDCR_EL2_TPMS		(UL(1) << 14)
 #define MDCR_EL2_E2PB_MASK	(UL(0x3))
 #define MDCR_EL2_E2PB_SHIFT	(UL(12))
-#define MDCR_EL2_TDRA		(1 << 11)
-#define MDCR_EL2_TDOSA		(1 << 10)
-#define MDCR_EL2_TDA		(1 << 9)
-#define MDCR_EL2_TDE		(1 << 8)
-#define MDCR_EL2_HPME		(1 << 7)
-#define MDCR_EL2_TPM		(1 << 6)
-#define MDCR_EL2_TPMCR		(1 << 5)
-#define MDCR_EL2_HPMN_MASK	(0x1F)
+#define MDCR_EL2_TDRA		(UL(1) << 11)
+#define MDCR_EL2_TDOSA		(UL(1) << 10)
+#define MDCR_EL2_TDA		(UL(1) << 9)
+#define MDCR_EL2_TDE		(UL(1) << 8)
+#define MDCR_EL2_HPME		(UL(1) << 7)
+#define MDCR_EL2_TPM		(UL(1) << 6)
+#define MDCR_EL2_TPMCR		(UL(1) << 5)
+#define MDCR_EL2_HPMN_MASK	(UL(0x1F))
 
 /* For compatibility with fault code shared with 32-bit */
 #define FSC_FAULT	ESR_ELx_FSC_FAULT
arch/arm64/include/asm/kvm_asm.h +1 -1
index 9f0bf2109be7..63ead9060ab5 100644
@@ -210,7 +210,7 @@ extern u64 __vgic_v3_read_vmcr(void);
 extern void __vgic_v3_write_vmcr(u32 vmcr);
 extern void __vgic_v3_init_lrs(void);
 
-extern u32 __kvm_get_mdcr_el2(void);
+extern u64 __kvm_get_mdcr_el2(void);
 
 #define __KVM_EXTABLE(from, to)						\
 	"	.pushsection	__kvm_ex_table, \"a\"\n"		\
arch/arm64/include/asm/kvm_host.h +0 -0
index 347781f99b6a..4d2d974c1522 100644

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 292

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -289,7 +289,7 @@ struct kvm_vcpu_arch {
 
 	/* HYP configuration */
 	u64 hcr_el2;
-	u32 mdcr_el2;
+	u64 mdcr_el2;

This breaks an existing trace in debug.c::kvm_arm_setup_mdcr_el2():

	trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);

which expects a 32bit value. I guess we could add an equivalent 64bit version, or silently upgrade the tracepoint to take a 64bit value. None of them are good solutions, but hey, something has to give...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#9 lore raw

Re: [PATCH v4 06/15] KVM: arm64: Restore mdcr_el2 from vcpu

In reply to Fuad Tabba
Quoted 27 lines · Fuad Tabba

On deactivating traps, restore the value of mdcr_el2 from the newly created and preserved host value vcpu context, rather than directly reading the hardware register.

Up until and including this patch the two values are the same, i.e., the hardware register and the vcpu one. A future patch will be changing the value of mdcr_el2 on activating traps, and this ensures that its value will be restored.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h       |  5 ++++-
 arch/arm64/include/asm/kvm_hyp.h        |  2 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h |  6 +++++-
 arch/arm64/kvm/hyp/nvhe/switch.c        | 13 +++++--------
 arch/arm64/kvm/hyp/vhe/switch.c         | 14 +++++---------
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c      |  2 +-
 6 files changed, 21 insertions(+), 21 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 4d2d974c1522..76462c6a91ee 100644

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 295

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +3 -1
@@ -287,10 +287,13 @@ struct kvm_vcpu_arch {
 	/* Stage 2 paging state used by the hardware on next switch */
 	struct kvm_s2_mmu *hw_mmu;
 
-	/* HYP configuration */
+	/* Values of trap registers for the guest. */
 	u64 hcr_el2;
 	u64 mdcr_el2;
 
+	/* Values of trap registers for the host before guest entry. */
+	u64 mdcr_el2_host;

This probably should then eventually replace the per-CPU copy of mdcr_el2 that lives in debug.c, shouldn't it?

Quoted 61 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+
 	/* Exception Information */
 	struct kvm_vcpu_fault_info fault;
 
arch/arm64/include/asm/kvm_hyp.h +1 -1
index 9d60b3006efc..657d0c94cf82 100644
@@ -95,7 +95,7 @@ void __sve_restore_state(void *sve_pffr, u32 *fpsr);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
 void activate_traps_vhe_load(struct kvm_vcpu *vcpu);
-void deactivate_traps_vhe_put(void);
+void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu);
 #endif
 
 u64 __guest_enter(struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/include/hyp/switch.h +5 -1
index e4a2f295a394..a0e78a6027be 100644
@@ -92,11 +92,15 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 		write_sysreg(0, pmselr_el0);
 		write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
 	}
+
+	vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
 	write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
 }
 
-static inline void __deactivate_traps_common(void)
+static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
 {
+	write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
+
 	write_sysreg(0, hstr_el2);
 	if (kvm_arm_support_pmu_v3())
 		write_sysreg(0, pmuserenr_el0);
arch/arm64/kvm/hyp/nvhe/switch.c +1 -4
index f7af9688c1f7..2ea764a48958 100644
@@ -69,12 +69,10 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 static void __deactivate_traps(struct kvm_vcpu *vcpu)
 {
 	extern char __kvm_hyp_host_vector[];
-	u64 mdcr_el2, cptr;
+	u64 cptr;
 
 	___deactivate_traps(vcpu);
 
-	mdcr_el2 = read_sysreg(mdcr_el2);
-
 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
 		u64 val;
 
@@ -92,13 +90,12 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 		isb();
 	}
 
-	__deactivate_traps_common();

arch/arm64/kvm/hyp/nvhe/switch.c static void __deactivate_traps(struct kvm_vcpu *vcpu) · line 101

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +4 -4
+	vcpu->arch.mdcr_el2_host &= MDCR_EL2_HPMN_MASK |
+				    MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
+				    MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
 
-	mdcr_el2 &= MDCR_EL2_HPMN_MASK;
-	mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
-	mdcr_el2 |= MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
+	__deactivate_traps_common(vcpu);
 
-	write_sysreg(mdcr_el2, mdcr_el2);

FWIW, I found this whole sequence massively confusing, and it is only when I came to patch #7 that the various pieces did come together.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#16 lore raw

Re: [PATCH v4 10/15] KVM: arm64: Add config register bit definitions

In reply to Fuad Tabba
Quoted 46 lines · Fuad Tabba

Add hardware configuration register bit definitions for HCR_EL2 and MDCR_EL2. Future patches toggle these hyp configuration register bits to trap on certain accesses.

No functional change intended.

---
 arch/arm64/include/asm/kvm_arm.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/arm64/include/asm/kvm_arm.h +9 -0
index a928b2dc0b0f..327120c0089f 100644
@@ -12,8 +12,13 @@
 #include <asm/types.h>
 
 /* Hyp Configuration Register (HCR) bits */
+
+#define HCR_TID5	(UL(1) << 58)
+#define HCR_DCT		(UL(1) << 57)
 #define HCR_ATA_SHIFT	56
 #define HCR_ATA		(UL(1) << HCR_ATA_SHIFT)
+#define HCR_AMVOFFEN	(UL(1) << 51)
+#define HCR_FIEN	(UL(1) << 47)
 #define HCR_FWB		(UL(1) << 46)
 #define HCR_API		(UL(1) << 41)
 #define HCR_APK		(UL(1) << 40)
@@ -56,6 +61,7 @@
 #define HCR_PTW		(UL(1) << 2)
 #define HCR_SWIO	(UL(1) << 1)
 #define HCR_VM		(UL(1) << 0)
+#define HCR_RES0	((UL(1) << 48) | (UL(1) << 39))
 
 /*
  * The bits we set in HCR:
@@ -277,11 +283,21 @@
 #define CPTR_EL2_TZ	(1 << 8)
 #define CPTR_NVHE_EL2_RES1	0x000032ff /* known RES1 bits in CPTR_EL2 (nVHE) */
 #define CPTR_EL2_DEFAULT	CPTR_NVHE_EL2_RES1
+#define CPTR_NVHE_EL2_RES0	(GENMASK(63, 32) |	\
+				 GENMASK(29, 21) |	\
+				 GENMASK(19, 14) |	\

arch/arm64/include/asm/kvm_arm.h line 298

Quoted 10 lines
arch/arm64/include/asm/kvm_arm.h +6 -0
+				 BIT(11))
 
 /* Hyp Debug Configuration Register bits */
 #define MDCR_EL2_E2TB_MASK	(UL(0x3))
 #define MDCR_EL2_E2TB_SHIFT	(UL(24))
+#define MDCR_EL2_HPMFZS		(UL(1) << 36)
+#define MDCR_EL2_HPMFZO		(UL(1) << 29)
+#define MDCR_EL2_MTPME		(UL(1) << 28)
+#define MDCR_EL2_TDCC		(UL(1) << 27)
+#define MDCR_EL2_HCCD		(UL(1) << 23)

Nit: If you're aiming for completeness, you're missing MDCR_EL2.HLP (bit 26).

Quoted 8 lines
arch/arm64/include/asm/kvm_arm.h +1 -0
 #define MDCR_EL2_TTRF		(UL(1) << 19)
+#define MDCR_EL2_HPMD		(UL(1) << 17)
 #define MDCR_EL2_TPMS		(UL(1) << 14)
 #define MDCR_EL2_E2PB_MASK	(UL(0x3))
 #define MDCR_EL2_E2PB_SHIFT	(UL(12))
@@ -293,6 +309,12 @@
 #define MDCR_EL2_TPM		(UL(1) << 6)
 #define MDCR_EL2_TPMCR		(UL(1) << 5)

arch/arm64/include/asm/kvm_arm.h line 320

Quoted 10 lines
arch/arm64/include/asm/kvm_arm.h +6 -0
 #define MDCR_EL2_HPMN_MASK	(UL(0x1F))
+#define MDCR_EL2_RES0		(GENMASK(63, 37) |	\
+				 GENMASK(35, 30) |	\
+				 GENMASK(25, 24) |	\
+				 GENMASK(22, 20) |	\
+				 BIT(18) |		\
+				 GENMASK(16, 15))
 
 /* For compatibility with fault code shared with 32-bit */
 #define FSC_FAULT	ESR_ELx_FSC_FAULT

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer
#19 lore raw

Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Fuad Tabba
Quoted 23 lines · Fuad Tabba

Add an array of pointers to handlers for various trap reasons in nVHE code.

The current code selects how to fixup a guest on exit based on a series of if/else statements. Future patches will also require different handling for guest exists. Create an array of handlers to consolidate them.

No functional change intended as the array isn't populated yet.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 43 +++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c        | 33 +++++++++++++++++++
 2 files changed, 76 insertions(+)
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
index a0e78a6027be..5a2b89b96c67 100644

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 412

Quoted 4 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -0
@@ -409,6 +409,46 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);

This returns an int...

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +7 -0
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu);
+
+static exit_handle_fn kvm_get_hyp_exit_handler(struct kvm_vcpu *vcpu)
+{
+	return is_nvhe_hyp_code() ? kvm_get_nvhe_exit_handler(vcpu) : NULL;
+}

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 429

Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10 -0
+
+/*
+ * Allow the hypervisor to handle the exit with an exit handler if it has one.
+ *
+ * Returns true if the hypervisor handled the exit, and control should go back
+ * to the guest, or false if it hasn't.
+ */
+static bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu)
+{
+	bool is_handled = false;

... which you then implicitly cast as a bool.

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 441

Quoted 12 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +12 -0
+	exit_handle_fn exit_handler = kvm_get_hyp_exit_handler(vcpu);
+
+	if (exit_handler) {
+		/*
+		 * There's limited vcpu context here since it's not synced yet.
+		 * Ensure that relevant vcpu context that might be used by the
+		 * exit_handler is in sync before it's called and if handled.
+		 */
+		*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
+		*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
+
+		is_handled = exit_handler(vcpu);

What does 'is_handled' mean here? By definition, any trap *must* be handled, one way or another. By the look of it, what you really mean is something like "I have updated the vcpu state and you'd better reload it". Is that what it means?

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 450

Quoted 9 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +9 -0
+
+		if (is_handled) {
+			write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
+			write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
+		}
+	}
+
+	return is_handled;
+}

All these functions really should be marked inline. Have you checked how this expands on VHE? I think some compilers could be pretty unhappy about the undefined symbol in kvm_get_hyp_exit_handler().

It is also unfortunate that we get a bunch of tests for various flavours of traps (FP, PAuth, page faults...), only to hit yet another decoding tree. Is there a way we could use this infrastructure for everything?

Quoted 35 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +4 -0
+
 /*
  * Return true when we were able to fixup the guest exit and should return to
  * the guest, false when we should restore the host state and return to the
@@ -496,6 +536,9 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
 			goto guest;
 	}
 
+	/* Check if there's an exit handler and allow it to handle the exit. */
+	if (kvm_hyp_handle_exit(vcpu))
+		goto guest;
 exit:
 	/* Return to the host kernel and handle the exit */
 	return false;
arch/arm64/kvm/hyp/nvhe/switch.c +13 -0
index 86f3d6482935..b7f25307a7b9 100644
@@ -158,6 +158,39 @@ static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
 		write_sysreg(pmu->events_host, pmcntenset_el0);
 }
 
+static exit_handle_fn hyp_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]		= NULL,
+	[ESR_ELx_EC_WFx]		= NULL,
+	[ESR_ELx_EC_CP15_32]		= NULL,
+	[ESR_ELx_EC_CP15_64]		= NULL,
+	[ESR_ELx_EC_CP14_MR]		= NULL,
+	[ESR_ELx_EC_CP14_LS]		= NULL,
+	[ESR_ELx_EC_CP14_64]		= NULL,
+	[ESR_ELx_EC_HVC32]		= NULL,
+	[ESR_ELx_EC_SMC32]		= NULL,
+	[ESR_ELx_EC_HVC64]		= NULL,
+	[ESR_ELx_EC_SMC64]		= NULL,
+	[ESR_ELx_EC_SYS64]		= NULL,

arch/arm64/kvm/hyp/nvhe/switch.c static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt) · line 183

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+	[ESR_ELx_EC_SVE]		= NULL,
+	[ESR_ELx_EC_IABT_LOW]		= NULL,
+	[ESR_ELx_EC_DABT_LOW]		= NULL,
+	[ESR_ELx_EC_SOFTSTP_LOW]	= NULL,
+	[ESR_ELx_EC_WATCHPT_LOW]	= NULL,
+	[ESR_ELx_EC_BREAKPT_LOW]	= NULL,
+	[ESR_ELx_EC_BKPT32]		= NULL,
+	[ESR_ELx_EC_BRK64]		= NULL,
+	[ESR_ELx_EC_FP_ASIMD]		= NULL,
+	[ESR_ELx_EC_PAC]		= NULL,

You can safely drop all these and only keep the top one for now. This will also keep the idiot robot at bay for until the next patch... ;-)

arch/arm64/kvm/hyp/nvhe/switch.c static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt) · line 196

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+};
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
+{
+	u32 esr = kvm_vcpu_get_esr(vcpu);
+	u8 esr_ec = ESR_ELx_EC(esr);
+
+	return hyp_exit_handlers[esr_ec];
+}
+
 /* Switch to the guest for legacy non-VHE systems */
 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 {

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#30 lore raw

Re: [PATCH v4 15/15] KVM: arm64: Handle protected guests at 32 bits

In reply to Fuad Tabba

Hi Fuad,

Quoted 38 lines · Fuad Tabba

Protected KVM does not support protected AArch32 guests. However, it is possible for the guest to force run AArch32, potentially causing problems. Add an extra check so that if the hypervisor catches the guest doing that, it can prevent the guest from running again by resetting vcpu->arch.target and returning ARM_EXCEPTION_IL.

If this were to happen, The VMM can try and fix it by re- initializing the vcpu with KVM_ARM_VCPU_INIT, however, this is likely not possible for protected VMs.

Adapted from commit 22f553842b14 ("KVM: arm64: Handle Asymmetric AArch32 systems")

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 37 ++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
arch/arm64/kvm/hyp/nvhe/switch.c +3 -0
index 398e62098898..0c24b7f473bf 100644
@@ -20,6 +20,7 @@
 #include <asm/kprobes.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
+#include <asm/kvm_fixed_config.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_mmu.h>
 #include <asm/fpsimd.h>
@@ -195,6 +196,39 @@ exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
                return NULL;
 }
 
+/*
+ * Some guests (e.g., protected VMs) might not be allowed to run in AArch32. The

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 210

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+ * check below is based on the one in kvm_arch_vcpu_ioctl_run().
+ * The ARMv8 architecture does not give the hypervisor a mechanism to prevent a
+ * guest from dropping to AArch32 EL0 if implemented by the CPU. If the
+ * hypervisor spots a guest in such a state ensure it is handled, and don't
+ * trust the host to spot or fix it.
+ *
+ * Returns true if the check passed and the guest run loop can continue, or
+ * false if the guest should exit to the host.
+ */
+static bool check_aarch32_guest(struct kvm_vcpu *vcpu, u64 *exit_code)

This does a bit more than just check & return, so maybe call it handle_aarch32_guest()?

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 212

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/switch.c +2 -0
+{
+       if (kvm_vm_is_protected(kern_hyp_va(vcpu->kvm)) &&

maybe initialize a local with a hyp pointer to the kvm structure.

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 216

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/switch.c +4 -0
+           vcpu_mode_is_32bit(vcpu) &&
+           FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL0),
+                                        PVM_ID_AA64PFR0_RESTRICT_UNSIGNED) <
+               ID_AA64PFR0_ELx_32BIT_64BIT) {

It may be more readable to initialize a local variable with this feature check, i.e:

bool aarch32_allowed = FIELD_GET(...) == ID_AA64PFR0_ELx_32BIT_64BIT;

and then:

if (kvm_vm_is_protected(kvm) && vcpu_mode_is_32bit(vcpu) &&
!aarch32_allowed) {

Quoted 29 lines
arch/arm64/kvm/hyp/nvhe/switch.c +18 -1
+               /*
+                * As we have caught the guest red-handed, decide that it isn't
+                * fit for purpose anymore by making the vcpu invalid. The VMM
+                * can try and fix it by re-initializing the vcpu with
+                * KVM_ARM_VCPU_INIT, however, this is likely not possible for
+                * protected VMs.
+                */
+               vcpu->arch.target = -1;
+               *exit_code = ARM_EXCEPTION_IL;
+               return false;
+       }
+
+       return true;
+}
+
 /* Switch to the guest for legacy non-VHE systems */
 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 {
@@ -255,6 +289,9 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
                /* Jump in the fire! */
                exit_code = __guest_enter(vcpu);
 
+               if (unlikely(!check_aarch32_guest(vcpu, &exit_code)))
+                       break;
+
                /* And we're baaack! */
        } while (fixup_guest_exit(vcpu, &exit_code));
 
--
Quoted 2 lines

2.33.0.rc1.237.g0d66db33f3-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#20 lore raw

Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Marc Zyngier

Hi Fuad,

Quoted 85 lines · Marc Zyngier
Quoted 23 lines · Fuad Tabba

Add an array of pointers to handlers for various trap reasons in nVHE code.

The current code selects how to fixup a guest on exit based on a series of if/else statements. Future patches will also require different handling for guest exists. Create an array of handlers to consolidate them.

No functional change intended as the array isn't populated yet.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 43 +++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c        | 33 +++++++++++++++++++
 2 files changed, 76 insertions(+)
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
index a0e78a6027be..5a2b89b96c67 100644

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 412

Quoted 4 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -0
@@ -409,6 +409,46 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
 	return true;
 }
 
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);

This returns an int...

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +7 -0
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu);
+
+static exit_handle_fn kvm_get_hyp_exit_handler(struct kvm_vcpu *vcpu)
+{
+	return is_nvhe_hyp_code() ? kvm_get_nvhe_exit_handler(vcpu) : NULL;
+}

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 429

Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10 -0
+
+/*
+ * Allow the hypervisor to handle the exit with an exit handler if it has one.
+ *
+ * Returns true if the hypervisor handled the exit, and control should go back
+ * to the guest, or false if it hasn't.
+ */
+static bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu)
+{
+	bool is_handled = false;

... which you then implicitly cast as a bool.

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 441

Quoted 12 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +12 -0
+	exit_handle_fn exit_handler = kvm_get_hyp_exit_handler(vcpu);
+
+	if (exit_handler) {
+		/*
+		 * There's limited vcpu context here since it's not synced yet.
+		 * Ensure that relevant vcpu context that might be used by the
+		 * exit_handler is in sync before it's called and if handled.
+		 */
+		*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
+		*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
+
+		is_handled = exit_handler(vcpu);

What does 'is_handled' mean here? By definition, any trap *must* be handled, one way or another. By the look of it, what you really mean is something like "I have updated the vcpu state and you'd better reload it". Is that what it means?

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 450

Quoted 9 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +9 -0
+
+		if (is_handled) {
+			write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
+			write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
+		}
+	}
+
+	return is_handled;
+}

All these functions really should be marked inline. Have you checked how this expands on VHE? I think some compilers could be pretty unhappy about the undefined symbol in kvm_get_hyp_exit_handler().

Quoted 4 lines

It is also unfortunate that we get a bunch of tests for various flavours of traps (FP, PAuth, page faults...), only to hit yet another decoding tree. Is there a way we could use this infrastructure for everything?

I realised that I wasn't very forthcoming here. I've decided to put the code where my mouth is and pushed out a branch [1] with your first 10 patches, followed by my own take on this particular problem. It compiles, and even managed to boot a Debian guest on a nVHE box.

As you can see, most of the early exit handling is now moved to specific handlers, unifying the handling. For the protected mode, you can provide your own handler array (just hack kvm_get_exit_handler_array() to return something else), which will do the right thing as long as you call into the existing handlers first.

When it comes to the ELR/SPSR handling, it is better left to the individual handlers (which we already do in some cases, see how we skip instructions, for example).

Please let me know what you think.

Thanks,

	M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git/log/?h=kvm-arm64/pkvm-fixed-features

Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#32 lore raw
In reply to Fuad Tabba
Quoted 11 lines · Fuad Tabba

Changes since v3 [1]:
- Redid calculating restricted values of feature register fields, ensuring that the code distinguishes between unsigned and (potentially in the future) signed fields (Will)
- Refactoring and fixes (Drew, Will)
- More documentation and comments (Oliver, Will)
- Dropped patch "Restrict protected VM capabilities", since it should come with or after the user ABI series for pKVM (Will)
- Carried Will's acks

[...]

I've taken the first 10 patches of this series in order to progress it. I also stashed a fixlet on top to address the tracepoint issue.

Hopefully we can resolve the rest of the issues quickly.

[01/15] KVM: arm64: placeholder to check if VM is protected

        commit: 2ea7f655800b00b109951f22539fe2025add210b
[02/15] KVM: arm64: Remove trailing whitespace in comment
        commit: e6bc555c96990046d680ff92c8e2e7b6b43b509f
[03/15] KVM: arm64: MDCR_EL2 is a 64-bit register
        commit: d6c850dd6ce9ce4b410142a600d8c34dc041d860
[04/15] KVM: arm64: Fix names of config register fields
        commit: dabb1667d8573302712a75530cccfee8f3ffff84
[05/15] KVM: arm64: Refactor sys_regs.h,c for nVHE reuse
        commit: f76f89e2f73d93720cfcad7fb7b24d022b2846bf
[06/15] KVM: arm64: Restore mdcr_el2 from vcpu
        commit: 1460b4b25fde52cbee746c11a4b1d3185f2e2847
[07/15] KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug
        commit: 12849badc6d2456f15f8f2c93037628d5176810b
[08/15] KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch
        commit: cd496228fd8de2e82b6636d3d89105631ea2b69c
[09/15] KVM: arm64: Add feature register flag definitions
        commit: 95b54c3e4c92b9185b15c83e8baab9ba312195f6
[10/15] KVM: arm64: Add config register bit definitions
        commit: 2d701243b9f231b5d7f9a8cb81870650d3eb32bc

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#21 lore raw

Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Marc Zyngier

Hi Marc,

Quoted 100 lines · Marc Zyngier

Hi Fuad,

Quoted 85 lines · Marc Zyngier
Quoted 23 lines · Fuad Tabba

Add an array of pointers to handlers for various trap reasons in nVHE code.

The current code selects how to fixup a guest on exit based on a series of if/else statements. Future patches will also require different handling for guest exists. Create an array of handlers to consolidate them.

No functional change intended as the array isn't populated yet.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 43 +++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c        | 33 +++++++++++++++++++
 2 files changed, 76 insertions(+)
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
index a0e78a6027be..5a2b89b96c67 100644

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 412

Quoted 4 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -0
@@ -409,6 +409,46 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
    return true;
 }
 
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);

This returns an int...

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +7 -0
+
+exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu);
+
+static exit_handle_fn kvm_get_hyp_exit_handler(struct kvm_vcpu *vcpu)
+{
+   return is_nvhe_hyp_code() ? kvm_get_nvhe_exit_handler(vcpu) : NULL;
+}

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 429

Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10 -0
+
+/*
+ * Allow the hypervisor to handle the exit with an exit handler if it has one.
+ *
+ * Returns true if the hypervisor handled the exit, and control should go back
+ * to the guest, or false if it hasn't.
+ */
+static bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu)
+{
+   bool is_handled = false;

... which you then implicitly cast as a bool.

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 441

Quoted 12 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +12 -0
+   exit_handle_fn exit_handler = kvm_get_hyp_exit_handler(vcpu);
+
+   if (exit_handler) {
+           /*
+            * There's limited vcpu context here since it's not synced yet.
+            * Ensure that relevant vcpu context that might be used by the
+            * exit_handler is in sync before it's called and if handled.
+            */
+           *vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
+           *vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
+
+           is_handled = exit_handler(vcpu);

What does 'is_handled' mean here? By definition, any trap *must* be handled, one way or another. By the look of it, what you really mean is something like "I have updated the vcpu state and you'd better reload it". Is that what it means?

arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu) · line 450

Quoted 9 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +9 -0
+
+           if (is_handled) {
+                   write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
+                   write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
+           }
+   }
+
+   return is_handled;
+}

All these functions really should be marked inline. Have you checked how this expands on VHE? I think some compilers could be pretty unhappy about the undefined symbol in kvm_get_hyp_exit_handler().

Quoted 4 lines

It is also unfortunate that we get a bunch of tests for various flavours of traps (FP, PAuth, page faults...), only to hit yet another decoding tree. Is there a way we could use this infrastructure for everything?

I realised that I wasn't very forthcoming here. I've decided to put the code where my mouth is and pushed out a branch [1] with your first 10 patches, followed by my own take on this particular problem. It compiles, and even managed to boot a Debian guest on a nVHE box.

Quoted 9 lines

As you can see, most of the early exit handling is now moved to specific handlers, unifying the handling. For the protected mode, you can provide your own handler array (just hack kvm_get_exit_handler_array() to return something else), which will do the right thing as long as you call into the existing handlers first. When it comes to the ELR/SPSR handling, it is better left to the individual handlers (which we already do in some cases, see how we skip instructions, for example).
Please let me know what you think.

Thanks a lot for this and sorry for being late to reply. I've been travelling.

I think that your proposal looks great. All handling is consolidated now and handling for protected VMs can just be added on top. There are some small issues with what parameters we need (e.g., passing struct kvm to kvm_get_exit_handler_array), but I will sort them out and submit them in the next round.

Cheers,
/fuad

Quoted 8 lines

Thanks,

        M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git/log/?h=kvm-arm64/pkvm-fixed-features

--
Without deviation from the norm, progress is not possible.

author
#33 lore raw
In reply to Marc Zyngier

Hi Marc,

Quoted 12 lines · Marc Zyngier
Quoted 10 lines · Fuad Tabba

Changes since v3 [1]:
- Redid calculating restricted values of feature register fields, ensuring that the code distinguishes between unsigned and (potentially in the future) signed fields (Will)
- Refactoring and fixes (Drew, Will)
- More documentation and comments (Oliver, Will)
- Dropped patch "Restrict protected VM capabilities", since it should come with or after the user ABI series for pKVM (Will)
- Carried Will's acks

Quoted 7 lines
Quoted 1 line

[...]

I've taken the first 10 patches of this series in order to progress it. I also stashed a fixlet on top to address the tracepoint issue.

Hopefully we can resolve the rest of the issues quickly.

Thanks. I am working on a patch series with the remaining patches to address the issues. Stay tuned :)

Cheers,
/fuad

Quoted 28 lines

[01/15] KVM: arm64: placeholder to check if VM is protected

        commit: 2ea7f655800b00b109951f22539fe2025add210b
[02/15] KVM: arm64: Remove trailing whitespace in comment
        commit: e6bc555c96990046d680ff92c8e2e7b6b43b509f
[03/15] KVM: arm64: MDCR_EL2 is a 64-bit register
        commit: d6c850dd6ce9ce4b410142a600d8c34dc041d860
[04/15] KVM: arm64: Fix names of config register fields
        commit: dabb1667d8573302712a75530cccfee8f3ffff84
[05/15] KVM: arm64: Refactor sys_regs.h,c for nVHE reuse
        commit: f76f89e2f73d93720cfcad7fb7b24d022b2846bf
[06/15] KVM: arm64: Restore mdcr_el2 from vcpu
        commit: 1460b4b25fde52cbee746c11a4b1d3185f2e2847
[07/15] KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug
        commit: 12849badc6d2456f15f8f2c93037628d5176810b
[08/15] KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch
        commit: cd496228fd8de2e82b6636d3d89105631ea2b69c
[09/15] KVM: arm64: Add feature register flag definitions
        commit: 95b54c3e4c92b9185b15c83e8baab9ba312195f6
[10/15] KVM: arm64: Add config register bit definitions
        commit: 2d701243b9f231b5d7f9a8cb81870650d3eb32bc

Cheers,

        M.

--
Without deviation from the norm, progress is not possible.

Quoted 48 lines · Marc Zyngier
Quoted 11 lines · Fuad Tabba

Changes since v3 [1]:
- Redid calculating restricted values of feature register fields, ensuring that the code distinguishes between unsigned and (potentially in the future) signed fields (Will)
- Refactoring and fixes (Drew, Will)
- More documentation and comments (Oliver, Will)
- Dropped patch "Restrict protected VM capabilities", since it should come with or after the user ABI series for pKVM (Will)
- Carried Will's acks

[...]

I've taken the first 10 patches of this series in order to progress it. I also stashed a fixlet on top to address the tracepoint issue.

Hopefully we can resolve the rest of the issues quickly.

[01/15] KVM: arm64: placeholder to check if VM is protected

        commit: 2ea7f655800b00b109951f22539fe2025add210b
[02/15] KVM: arm64: Remove trailing whitespace in comment
        commit: e6bc555c96990046d680ff92c8e2e7b6b43b509f
[03/15] KVM: arm64: MDCR_EL2 is a 64-bit register
        commit: d6c850dd6ce9ce4b410142a600d8c34dc041d860
[04/15] KVM: arm64: Fix names of config register fields
        commit: dabb1667d8573302712a75530cccfee8f3ffff84
[05/15] KVM: arm64: Refactor sys_regs.h,c for nVHE reuse
        commit: f76f89e2f73d93720cfcad7fb7b24d022b2846bf
[06/15] KVM: arm64: Restore mdcr_el2 from vcpu
        commit: 1460b4b25fde52cbee746c11a4b1d3185f2e2847
[07/15] KVM: arm64: Keep mdcr_el2's value as set by __init_el2_debug
        commit: 12849badc6d2456f15f8f2c93037628d5176810b
[08/15] KVM: arm64: Track value of cptr_el2 in struct kvm_vcpu_arch
        commit: cd496228fd8de2e82b6636d3d89105631ea2b69c
[09/15] KVM: arm64: Add feature register flag definitions
        commit: 95b54c3e4c92b9185b15c83e8baab9ba312195f6
[10/15] KVM: arm64: Add config register bit definitions
        commit: 2d701243b9f231b5d7f9a8cb81870650d3eb32bc

Cheers,

        M.

--
Without deviation from the norm, progress is not possible.

author
#31 lore raw

Re: [PATCH v4 15/15] KVM: arm64: Handle protected guests at 32 bits

In reply to Oliver Upton

Hi Oliver,

Quoted 56 lines · Oliver Upton

Hi Fuad,

Quoted 38 lines · Fuad Tabba

Protected KVM does not support protected AArch32 guests. However, it is possible for the guest to force run AArch32, potentially causing problems. Add an extra check so that if the hypervisor catches the guest doing that, it can prevent the guest from running again by resetting vcpu->arch.target and returning ARM_EXCEPTION_IL.

If this were to happen, The VMM can try and fix it by re- initializing the vcpu with KVM_ARM_VCPU_INIT, however, this is likely not possible for protected VMs.

Adapted from commit 22f553842b14 ("KVM: arm64: Handle Asymmetric AArch32 systems")

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 37 ++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
arch/arm64/kvm/hyp/nvhe/switch.c +3 -0
index 398e62098898..0c24b7f473bf 100644
@@ -20,6 +20,7 @@
 #include <asm/kprobes.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
+#include <asm/kvm_fixed_config.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_mmu.h>
 #include <asm/fpsimd.h>
@@ -195,6 +196,39 @@ exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu)
                return NULL;
 }
 
+/*
+ * Some guests (e.g., protected VMs) might not be allowed to run in AArch32. The

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 210

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+ * check below is based on the one in kvm_arch_vcpu_ioctl_run().
+ * The ARMv8 architecture does not give the hypervisor a mechanism to prevent a
+ * guest from dropping to AArch32 EL0 if implemented by the CPU. If the
+ * hypervisor spots a guest in such a state ensure it is handled, and don't
+ * trust the host to spot or fix it.
+ *
+ * Returns true if the check passed and the guest run loop can continue, or
+ * false if the guest should exit to the host.
+ */
+static bool check_aarch32_guest(struct kvm_vcpu *vcpu, u64 *exit_code)

This does a bit more than just check & return, so maybe call it handle_aarch32_guest()?

Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 212

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/switch.c +2 -0
+{
+       if (kvm_vm_is_protected(kern_hyp_va(vcpu->kvm)) &&

maybe initialize a local with a hyp pointer to the kvm structure.

Will do.

Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/switch.c exit_handle_fn kvm_get_nvhe_exit_handler(struct kvm_vcpu *vcpu) · line 216

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/switch.c +4 -0
+           vcpu_mode_is_32bit(vcpu) &&
+           FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL0),
+                                        PVM_ID_AA64PFR0_RESTRICT_UNSIGNED) <
+               ID_AA64PFR0_ELx_32BIT_64BIT) {

It may be more readable to initialize a local variable with this feature check, i.e:

Quoted 6 lines

bool aarch32_allowed = FIELD_GET(...) == ID_AA64PFR0_ELx_32BIT_64BIT;

and then:

if (kvm_vm_is_protected(kvm) && vcpu_mode_is_32bit(vcpu) &&
!aarch32_allowed) {

I agree.

Thanks,
/fuad

Quoted 31 lines
Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/switch.c +18 -1
+               /*
+                * As we have caught the guest red-handed, decide that it isn't
+                * fit for purpose anymore by making the vcpu invalid. The VMM
+                * can try and fix it by re-initializing the vcpu with
+                * KVM_ARM_VCPU_INIT, however, this is likely not possible for
+                * protected VMs.
+                */
+               vcpu->arch.target = -1;
+               *exit_code = ARM_EXCEPTION_IL;
+               return false;
+       }
+
+       return true;
+}
+
 /* Switch to the guest for legacy non-VHE systems */
 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 {
@@ -255,6 +289,9 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
                /* Jump in the fire! */
                exit_code = __guest_enter(vcpu);
 
+               if (unlikely(!check_aarch32_guest(vcpu, &exit_code)))
+                       break;
+
                /* And we're baaack! */
        } while (fixup_guest_exit(vcpu, &exit_code));
 
--

2.33.0.rc1.237.g0d66db33f3-goog

maintainer
#22 lore raw

Re: [PATCH v4 11/15] KVM: arm64: Guest exit handlers for nVHE hyp

In reply to Fuad Tabba

Hi Fuad,

Quoted 19 lines · Fuad Tabba

Hi Marc,

Quoted 5 lines · Marc Zyngier

I realised that I wasn't very forthcoming here. I've decided to put the code where my mouth is and pushed out a branch [1] with your first 10 patches, followed by my own take on this particular problem. It compiles, and even managed to boot a Debian guest on a nVHE box.

Quoted 9 lines

As you can see, most of the early exit handling is now moved to specific handlers, unifying the handling. For the protected mode, you can provide your own handler array (just hack kvm_get_exit_handler_array() to return something else), which will do the right thing as long as you call into the existing handlers first. When it comes to the ELR/SPSR handling, it is better left to the individual handlers (which we already do in some cases, see how we skip instructions, for example).
Please let me know what you think.

Quoted 2 lines

Thanks a lot for this and sorry for being late to reply. I've been travelling.

No worries, it should be me who apologies for getting to this that late.

Quoted 5 lines

I think that your proposal looks great. All handling is consolidated now and handling for protected VMs can just be added on top. There are some small issues with what parameters we need (e.g., passing struct kvm to kvm_get_exit_handler_array), but I will sort them out and submit them in the next round.

OK. Please base these changes on top of the three patches in my branch, which I will update with actual commit messages.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.