Skip to contentSkip to conversation

KVM: arm64: pkvm: Extend hypervisor entering tracing reasons

PATCH Applied Vincent Donnefort 3 participants 8 messages Nov 4 – Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover KVM: arm64: pkvm: Extend hypervisor entering tracing reasons

This series makes the hyp_enter event more detailed by adding a "from=" field and by using the ESR class when relevant. This is interesting for example to know when the guest has exited for an HVC.

Speaking of HVCs, protected VMs issue an HVC to share memory with the host. Without this memory sharing, it is impossible to get logs. So if for any reason it fails, debugging is quite cumbersome. Add a specific guest_hvc event that will give even more details than hyp_enter provides.

Also, add a host_hvc event, so that the host gets similar coverage to a protected VM.

Changelog:

v2:
* Add missing exception classes to kvm_arm_exception_class (Fuad)
* Split the HYP_REASON_PSCI -> HYP_REASON_ERET_HOST fix in __kvm_host_psci_cpu_entry() into a separate patch.
* Add missing <asm/esr.h> (Fuad)
* Read ESR_EL2 only when hyp_enter is enabled (Fuad)
* Drop the handled field from guest_hvc and trace only hypervisor-handled HVCs.
* Rename ret to x1 in host_hvc (Fuad) and val to x0 in guest_hvc.
* Log the SMCCC function ID in host_hvc instead of the host_hcall[] index. (Fuad)
* remote_event_format_show() field alignment fixed

    separately (https://lore.kernel.org/all/[email protected]/). (Fuad)

* Rebased on top of v7.4-rc1.

v1 (https://lore.kernel.org/all/[email protected]/)

Vincent Donnefort (5):
KVM: arm64: Add missing exception classes to kvm_arm_exception_class
KVM: arm64: Use HYP_REASON_ERET_HOST on trace_hyp_exit from
PSCI_CPU_ON
KVM: arm64: Add ESR class to the hyp_enter hyp event
KVM: arm64: Add guest_hvc hyp event
KVM: arm64: Add host_hvc hyp event

 arch/arm64/include/asm/kvm_arm.h       | 13 +++---
 arch/arm64/include/asm/kvm_hypevents.h | 55 ++++++++++++++++++++------
 arch/arm64/kvm/hyp/nvhe/hyp-main.c     |  9 ++---
 arch/arm64/kvm/hyp/nvhe/pkvm.c         | 12 +++++-
 arch/arm64/kvm/hyp/nvhe/psci-relay.c   |  2 +-
 arch/arm64/kvm/hyp/nvhe/switch.c       |  6 ++-
 arch/arm64/kvm/hyp_trace.c             | 52 ++++++++++++++++++------
 7 files changed, 111 insertions(+), 38 deletions(-)

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.0.455.g6c5a503b38-goog
#1 lore raw

PATCH v2 1/5 KVM: arm64: Add missing exception classes to kvm_arm_exception_class

kvm_arm_exception_class is missing several exception classes defined in esr.h, causing the event kvm_exit and hyp event hyp_enter to log an unknown ESR. Add the missing entries and while at it, order them by their value.

---
 arch/arm64/include/asm/kvm_arm.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_arm.h +7 -6
index ba0062849be9..91ef5d27b491 100644
@@ -244,14 +244,15 @@
 
 #define kvm_arm_exception_class \
 	ECN(UNKNOWN), ECN(WFx), ECN(CP15_32), ECN(CP15_64), ECN(CP14_MR), \
-	ECN(CP14_LS), ECN(FP_ASIMD), ECN(CP10_ID), ECN(PAC), ECN(CP14_64), \
-	ECN(SVC64), ECN(HVC64), ECN(SMC64), ECN(SYS64), ECN(SVE), \
-	ECN(IMP_DEF), ECN(IABT_LOW), ECN(IABT_CUR), \
-	ECN(PC_ALIGN), ECN(DABT_LOW), ECN(DABT_CUR), \
-	ECN(SP_ALIGN), ECN(FP_EXC32), ECN(FP_EXC64), ECN(SERROR), \
+	ECN(CP14_LS), ECN(FP_ASIMD), ECN(CP10_ID), ECN(PAC), ECN(OTHER), \
+	ECN(CP14_64), ECN(BTI), ECN(ILL), ECN(SVC32), ECN(HVC32), ECN(SMC32), \
+	ECN(SVC64), ECN(HVC64), ECN(SMC64), ECN(SYS64), ECN(SVE), ECN(ERET), \
+	ECN(FPAC), ECN(SME), ECN(IMP_DEF), ECN(IABT_LOW), ECN(IABT_CUR), \
+	ECN(PC_ALIGN), ECN(DABT_LOW), ECN(DABT_CUR), ECN(SP_ALIGN), ECN(MOPS), \
+	ECN(FP_EXC32), ECN(FP_EXC64), ECN(GCS), ECN(SERROR), \
 	ECN(BREAKPT_LOW), ECN(BREAKPT_CUR), ECN(SOFTSTP_LOW), \
 	ECN(SOFTSTP_CUR), ECN(WATCHPT_LOW), ECN(WATCHPT_CUR), \
-	ECN(BKPT32), ECN(VECTOR32), ECN(BRK64), ECN(ERET)
+	ECN(BKPT32), ECN(VECTOR32), ECN(BRK64)
 
 #define kvm_mode_names				\
 	{ PSR_MODE_EL0t,	"EL0t" },	\
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
#2 lore raw

PATCH v2 2/5 KVM: arm64: Use HYP_REASON_ERET_HOST on trace_hyp_exit from PSCI_CPU_ON

In reply to Vincent Donnefort

The hyp_exit on PSCI_CPU_ON logs PSCI as a reason but really we eret to the host. Use HYP_REASON_ERET_HOST instead.

---
 arch/arm64/kvm/hyp/nvhe/psci-relay.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/psci-relay.c +1 -1
index e20db999e328..48228991a7ba 100644
@@ -213,7 +213,7 @@ static void __noreturn __kvm_host_psci_cpu_entry(unsigned long pc, unsigned long
 	write_sysreg_el1(INIT_SCTLR_EL1_MMU_OFF, SYS_SCTLR);
 	write_sysreg(INIT_PSTATE_EL1, SPSR_EL2);
 
-	trace_hyp_exit(host_ctxt, HYP_REASON_PSCI);
+	trace_hyp_exit(host_ctxt, HYP_REASON_ERET_HOST);
 	__host_enter(host_ctxt);
 }
 
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
#3 lore raw

PATCH v2 3/5 KVM: arm64: Add ESR class to the hyp_enter hyp event

In reply to Vincent Donnefort

Give a more detailed reason the hyp has been entered by reading the ESR class when relevant (that is on trap). Encode the ESR class directly into the existing reason field.

To distinguish between a trap from host and from guest, add a "from=" field, which has 3 possibilities: "host", "vcpu" or "firmware". This value can be deduced based on a combination of the existing vcpu and reason fields.

Finally, add a "to=" field to the hyp_exit event to mirror "hyp_enter", allowing HYP_REASON_ERET_HOST and HYP_REASON_ERET_GUEST to be collapsed into a single HYP_REASON_ERET.

---
 arch/arm64/include/asm/kvm_hypevents.h | 29 ++++++++------
 arch/arm64/kvm/hyp/nvhe/hyp-main.c     |  7 +---
 arch/arm64/kvm/hyp/nvhe/psci-relay.c   |  2 +-
 arch/arm64/kvm/hyp/nvhe/switch.c       |  6 ++-
 arch/arm64/kvm/hyp_trace.c             | 52 ++++++++++++++++++++------
 5 files changed, 66 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_hypevents.h +18 -11
index 3396deb1a1f8..7486d95455c5 100644
@@ -3,22 +3,22 @@
 #if !defined(__ARM64_KVM_HYPEVENTS_H_) || defined(HYP_EVENT_MULTI_READ)
 #define __ARM64_KVM_HYPEVENTS_H_
 
+#include <asm/esr.h>
+
 #ifdef __KVM_NVHE_HYPERVISOR__
+#include <asm/kvm_hyp.h>
+
 #include <nvhe/trace.h>
 #endif
 
 #ifndef __HYP_ENTER_EXIT_REASON
 #define __HYP_ENTER_EXIT_REASON
 enum hyp_enter_exit_reason {
-	HYP_REASON_SMC,
-	HYP_REASON_HVC,
-	HYP_REASON_SYS,
+	HYP_REASON_SMC = ESR_ELx_EC_MAX + 1,
 	HYP_REASON_PSCI,
-	HYP_REASON_HOST_ABORT,
-	HYP_REASON_GUEST_EXIT,
-	HYP_REASON_ERET_HOST,
-	HYP_REASON_ERET_GUEST,
-	HYP_REASON_UNKNOWN	/* Must be last */
+	HYP_REASON_IRQ,
+	HYP_REASON_ERET,
+	HYP_REASON_USE_ESR, /* trace_hyp_enter() only, resolved to ESR_EL2.EC */
 };
 #endif
 
@@ -29,10 +29,14 @@ HYP_EVENT(hyp_enter,
 		he_field(pid_t, vcpu)
 	),
 	HE_ASSIGN(
-		__entry->reason = reason;
+		__entry->reason = reason == HYP_REASON_USE_ESR ?
+			ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) : reason;
 		__entry->vcpu = __tracing_get_vcpu_pid(host_ctxt);
 	),
-	HE_PRINTK("reason=%s vcpu=%d", __hyp_enter_exit_reason_str(__entry->reason), __entry->vcpu)
+	HE_PRINTK("from=%s reason=%s vcpu=%d",
+		  __hyp_enter_exit_str(__entry->reason, __entry->vcpu),
+		  __hyp_enter_exit_reason_str(__entry->reason),
+		  __entry->vcpu)
 );
 
 HYP_EVENT(hyp_exit,
@@ -45,7 +49,10 @@ HYP_EVENT(hyp_exit,
 		__entry->reason = reason;
 		__entry->vcpu = __tracing_get_vcpu_pid(host_ctxt);
 	),
-	HE_PRINTK("reason=%s vcpu=%d", __hyp_enter_exit_reason_str(__entry->reason), __entry->vcpu)
+	HE_PRINTK("to=%s reason=%s vcpu=%d",
+		  __hyp_enter_exit_str(__entry->reason, __entry->vcpu),
+		  __hyp_enter_exit_reason_str(__entry->reason),
+		  __entry->vcpu)
 );
 
 HYP_EVENT(selftest,
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -5
index 179d7c8277b8..28e0191e443f 100644
@@ -1758,23 +1758,20 @@ void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);
 
+	trace_hyp_enter(host_ctxt, ESR_ELx_EC(esr));
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
-		trace_hyp_enter(host_ctxt, HYP_REASON_HVC);
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		trace_hyp_enter(host_ctxt, HYP_REASON_SMC);
 		handle_host_smc(host_ctxt);
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
-		trace_hyp_enter(host_ctxt, HYP_REASON_HOST_ABORT);
 		handle_host_mem_abort(host_ctxt);
 		break;
 	case ESR_ELx_EC_SYS64:
-		trace_hyp_enter(host_ctxt, HYP_REASON_SYS);
 		if (handle_host_mte(esr))
 			break;
 		fallthrough;
@@ -1782,5 +1779,5 @@ void handle_trap(struct kvm_cpu_context *host_ctxt)
 		BUG();
 	}
 
-	trace_hyp_exit(host_ctxt, HYP_REASON_ERET_HOST);
+	trace_hyp_exit(host_ctxt, HYP_REASON_ERET);
 }
arch/arm64/kvm/hyp/nvhe/psci-relay.c +1 -1
index 48228991a7ba..c771c1d150c4 100644
@@ -213,7 +213,7 @@ static void __noreturn __kvm_host_psci_cpu_entry(unsigned long pc, unsigned long
 	write_sysreg_el1(INIT_SCTLR_EL1_MMU_OFF, SYS_SCTLR);
 	write_sysreg(INIT_PSTATE_EL1, SPSR_EL2);
 
-	trace_hyp_exit(host_ctxt, HYP_REASON_ERET_HOST);
+	trace_hyp_exit(host_ctxt, HYP_REASON_ERET);
 	__host_enter(host_ctxt);
 }
 
arch/arm64/kvm/hyp/nvhe/switch.c +4 -2
index 89fb9dd351ad..afb672dda5e8 100644
@@ -345,13 +345,15 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	__debug_switch_to_guest(vcpu);
 
 	do {
-		trace_hyp_exit(host_ctxt, HYP_REASON_ERET_GUEST);
+		trace_hyp_exit(host_ctxt, HYP_REASON_ERET);
 
 		/* Jump in the fire! */
 		exit_code = __guest_enter(vcpu);
 
 		/* And we're baaack! */
-		trace_hyp_enter(host_ctxt, HYP_REASON_GUEST_EXIT);
+		trace_hyp_enter(host_ctxt,
+				ARM_EXCEPTION_CODE(exit_code) == ARM_EXCEPTION_IRQ ?
+				HYP_REASON_IRQ : HYP_REASON_USE_ESR);
 	} while (fixup_guest_exit(vcpu, &exit_code));
 
 	__sysreg_save_state_nvhe(guest_ctxt);
arch/arm64/kvm/hyp_trace.c +41 -11

68 lines not on this page · raw message

Signature · 1 line
2.56.0.455.g6c5a503b38-goog
#4 lore raw

PATCH v2 4/5 KVM: arm64: Add guest_hvc hyp event

In reply to Vincent Donnefort

Protected VMs issue HVCs that are fully handled by the hypervisor. This makes a failure hard to debug solely based on host information. Create a guest_hvc hypervisor event for hypervisor handled HVCs and log the return value, stored in x0.

---
 arch/arm64/include/asm/kvm_hypevents.h | 13 +++++++++++++
 arch/arm64/kvm/hyp/nvhe/pkvm.c         | 12 ++++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_hypevents.h +13 -0
index 7486d95455c5..63d98dd92cb0 100644
@@ -66,6 +66,19 @@ HYP_EVENT(selftest,
 	RE_PRINTK("id=%llu", __entry->id)
 );
 
+HYP_EVENT(guest_hvc,
+	HE_PROTO(u32 id, u64 x0),
+	HE_STRUCT(
+		he_field(u32, id)
+		he_field(u64, x0)
+	),
+	HE_ASSIGN(
+		__entry->id = id;
+		__entry->x0 = x0;
+	),
+	HE_PRINTK("id=0x%x x0=0x%llx", __entry->id, __entry->x0)
+);
+
 /*
  * trace_hyp_printk() has too many specificities to be declared with HYP_EVENT().
  * However, we can use a REMOTE_EVENT macro to automatically do the declaration
arch/arm64/kvm/hyp/nvhe/pkvm.c +10 -2
index fc0475f0877d..df7bd34325de 100644
@@ -11,6 +11,7 @@
 #include <linux/mm.h>
 
 #include <asm/kvm_emulate.h>
+#include <asm/kvm_hypevents.h>
 #include <asm/spectre.h>
 
 #include <nvhe/alloc.h>
@@ -1445,11 +1446,12 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code)
 {
 	struct pkvm_hyp_vcpu *hyp_vcpu = container_of(vcpu, struct pkvm_hyp_vcpu, vcpu);
 	u64 val[4] = { SMCCC_RET_INVALID_PARAMETER };
+	u32 fn = smccc_get_function(vcpu);
 	bool handled = true;
 	u32 feature;
 	uuid_t uuid;
 
-	switch (smccc_get_function(vcpu)) {
+	switch (fn) {
 	case ARM_SMCCC_VERSION_FUNC_ID:
 		/* Nothing to be handled by the host. Go back to the guest. */
 		val[0] = ARM_SMCCC_VERSION_1_1;
@@ -1542,10 +1544,16 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code)
 		pkvm_memunshare_call(val, vcpu);
 		break;
 	default:
-		return pkvm_handle_psci(hyp_vcpu);
+		handled = pkvm_handle_psci(hyp_vcpu);
+		goto out;
 	}
 
 	if (handled)
 		smccc_set_retval(vcpu, val[0], val[1], val[2], val[3]);
+
+out:
+	if (handled)
+		trace_guest_hvc(fn, vcpu_get_reg(vcpu, 0));
+
 	return handled;
 }
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
#5 lore raw

PATCH v2 5/5 KVM: arm64: Add host_hvc hyp event

In reply to Vincent Donnefort

In a similar fashion to the guest_hvc hypervisor event, log hypervisor handled host HVCs with their return values, stored in x1.

---
 arch/arm64/include/asm/kvm_hypevents.h | 13 +++++++++++++
 arch/arm64/kvm/hyp/nvhe/hyp-main.c     |  2 ++
 2 files changed, 15 insertions(+)
arch/arm64/include/asm/kvm_hypevents.h +13 -0
index 63d98dd92cb0..9a240fa410ca 100644
@@ -66,6 +66,19 @@ HYP_EVENT(selftest,
 	RE_PRINTK("id=%llu", __entry->id)
 );
 
+HYP_EVENT(host_hvc,
+	HE_PROTO(u32 id, u64 x1),
+	HE_STRUCT(
+		he_field(u32, id)
+		he_field(u64, x1)
+	),
+	HE_ASSIGN(
+		__entry->id = id;
+		__entry->x1 = x1;
+	),
+	HE_PRINTK("id=0x%x x1=0x%llx", __entry->id, __entry->x1)
+);
+
 HYP_EVENT(guest_hvc,
 	HE_PROTO(u32 id, u64 x0),
 	HE_STRUCT(
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -0
index 28e0191e443f..1113cd828bd5 100644
@@ -1642,6 +1642,8 @@ static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
 	cpu_reg(host_ctxt, 0) = SMCCC_RET_SUCCESS;
 	hfn(host_ctxt);
 
+	trace_host_hvc(KVM_HOST_SMCCC_ID(id), cpu_reg(host_ctxt, 1));
+
 	return;
 inval:
 	cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
maintainer Applied
#7 lore raw
In reply to Vincent Donnefort
Quoted 11 lines · Vincent Donnefort

This series makes the hyp_enter event more detailed by adding a "from=" field and by using the ESR class when relevant. This is interesting for example to know when the guest has exited for an HVC.

Speaking of HVCs, protected VMs issue an HVC to share memory with the host. Without this memory sharing, it is impossible to get logs. So if for any reason it fails, debugging is quite cumbersome. Add a specific guest_hvc event that will give even more details than hyp_enter provides.

[...]

Applied to next, thanks!

[1/5] KVM: arm64: Add missing exception classes to kvm_arm_exception_class

      https://git.kernel.org/kvmarm/kvmarm/c/a5ae562e4ca5
[2/5] KVM: arm64: Use HYP_REASON_ERET_HOST on trace_hyp_exit from PSCI_CPU_ON
      https://git.kernel.org/kvmarm/kvmarm/c/dae4367bd9a1
[3/5] KVM: arm64: Add ESR class to the hyp_enter hyp event
      https://git.kernel.org/kvmarm/kvmarm/c/54570d08cc29
[4/5] KVM: arm64: Add guest_hvc hyp event
      https://git.kernel.org/kvmarm/kvmarm/c/172bf4e12f61
[5/5] KVM: arm64: Add host_hvc hyp event
      https://git.kernel.org/kvmarm/kvmarm/c/faa023639b95

--
Best,
Oliver