public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe
@ 2025-09-09 13:36 Mostafa Saleh
  2025-09-09 13:36 ` [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic Mostafa Saleh
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Mostafa Saleh @ 2025-09-09 13:36 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel, kvmarm
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, perret, keirf, Mostafa Saleh

This small series, adds support for dumping the faulting instructions on
panic in pKVM and nvhe, similarly to what the kernel does as follow:
[   12.012126] kvm [190]: nVHE hyp BUG at: [<ffff8000811c5f64>] __kvm_nvhe_handle___kvm_vcpu_run+0x4/0x8!
[   12.015747] kvm [190]: Cannot dump pKVM nVHE stacktrace: !CONFIG_PROTECTED_NVHE_STACKTRACE
[   12.016044] Code: a8c17bfd d50323bf d65f03c0 d503245f (d4210000)
[   12.016082] kvm [190]: Hyp Offset: 0xffeff6887fe00000
[   12.016325] Kernel panic - not syncing: HYP panic:
[   12.016325] PS:204023c9 PC:000f8978013c5f64 ESR:00000000f2000800
[   12.016325] FAR:fff00000c016e01c HPFAR:00000000010016e0 PAR:0000000000000000

This can be useful in debugging cases of memory corruption.

The first patch adds this for nvhe and CONFIG_NVHE_EL2_DEBUG which is
straightforward as at the point of panic there is no stage-2 for the
host CPU, so it can re-use the kernel code to read and dump the faulting
instructions.

The second patch adds this support for pKVM, I splitted that into patches
as the pKVM changes are more fundamental, as now the hypervisor text would
be mapped in the host stage-2 as RO all the time.
An alternative is to make the hypervisor read its instructions on panic and
passes it to the kernel panic handler, but as we are out of registers
(X0-X7 used) for the arguments we would have to move that code to assembly.

Changes in v2:
- Harden instr dumping based on Will's feedback.
- Collected tags

Mostafa Saleh (2):
  KVM: arm64: Dump instruction on hyp panic
  KVM: arm64: Map hyp text as RO and dump instr on panic

 arch/arm64/include/asm/traps.h  |  1 +
 arch/arm64/kernel/traps.c       | 15 +++++++++------
 arch/arm64/kvm/handle_exit.c    |  3 +++
 arch/arm64/kvm/hyp/nvhe/setup.c | 12 ++++++++++--
 4 files changed, 23 insertions(+), 8 deletions(-)

-- 
2.51.0.384.g4c02a37b29-goog



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic
  2025-09-09 13:36 [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Mostafa Saleh
@ 2025-09-09 13:36 ` Mostafa Saleh
  2025-09-15 10:54   ` Will Deacon
  2025-09-09 13:36 ` [PATCH v2 2/2] KVM: arm64: Map hyp text as RO and dump instr on panic Mostafa Saleh
  2025-09-15 12:07 ` [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Marc Zyngier
  2 siblings, 1 reply; 5+ messages in thread
From: Mostafa Saleh @ 2025-09-09 13:36 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel, kvmarm
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, perret, keirf, Mostafa Saleh,
	Kunwu Chan

Similar to the kernel panic, where the instruction code is printed,
we can do the same for hypervisor panics.

This patch does that only in case of “CONFIG_NVHE_EL2_DEBUG” or nvhe.

The next patch adds support for pKVM.

Also, remove the hardcoded argument dump_kernel_instr().

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Tested-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
---
 arch/arm64/include/asm/traps.h |  1 +
 arch/arm64/kernel/traps.c      | 15 +++++++++------
 arch/arm64/kvm/handle_exit.c   |  5 +++++
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h
index e3e8944a71c3..e92e4a0e48fc 100644
--- a/arch/arm64/include/asm/traps.h
+++ b/arch/arm64/include/asm/traps.h
@@ -36,6 +36,7 @@ int kasan_brk_handler(struct pt_regs *regs, unsigned long esr);
 int ubsan_brk_handler(struct pt_regs *regs, unsigned long esr);
 
 int early_brk64(unsigned long addr, unsigned long esr, struct pt_regs *regs);
+void dump_kernel_instr(unsigned long kaddr);
 
 /*
  * Move regs->pc to next instruction and do necessary setup before it
diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c
index f528b6041f6a..83e6d1409e1f 100644
--- a/arch/arm64/kernel/traps.c
+++ b/arch/arm64/kernel/traps.c
@@ -149,19 +149,18 @@ pstate_check_t * const aarch32_opcode_cond_checks[16] = {
 
 int show_unhandled_signals = 0;
 
-static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
+void dump_kernel_instr(unsigned long kaddr)
 {
-	unsigned long addr = instruction_pointer(regs);
 	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
 	int i;
 
-	if (user_mode(regs))
+	if (!is_ttbr1_addr(kaddr))
 		return;
 
 	for (i = -4; i < 1; i++) {
 		unsigned int val, bad;
 
-		bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
+		bad = aarch64_insn_read(&((u32 *)kaddr)[i], &val);
 
 		if (!bad)
 			p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
@@ -169,7 +168,7 @@ static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
 			p += sprintf(p, i == 0 ? "(????????) " : "???????? ");
 	}
 
-	printk("%sCode: %s\n", lvl, str);
+	printk(KERN_EMERG "Code: %s\n", str);
 }
 
 #define S_SMP " SMP"
@@ -178,6 +177,7 @@ static int __die(const char *str, long err, struct pt_regs *regs)
 {
 	static int die_counter;
 	int ret;
+	unsigned long addr = instruction_pointer(regs);
 
 	pr_emerg("Internal error: %s: %016lx [#%d] " S_SMP "\n",
 		 str, err, ++die_counter);
@@ -190,7 +190,10 @@ static int __die(const char *str, long err, struct pt_regs *regs)
 	print_modules();
 	show_regs(regs);
 
-	dump_kernel_instr(KERN_EMERG, regs);
+	if (user_mode(regs))
+		return ret;
+
+	dump_kernel_instr(addr);
 
 	return ret;
 }
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index a598072f36d2..99a8205fc104 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -559,6 +559,11 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
 	/* Dump the nVHE hypervisor backtrace */
 	kvm_nvhe_dump_backtrace(hyp_offset);
 
+	/* Dump the faulting instruction */
+	if (!is_protected_kvm_enabled() ||
+	    IS_ENABLED(CONFIG_NVHE_EL2_DEBUG))
+		dump_kernel_instr(panic_addr + kaslr_offset());
+
 	/*
 	 * Hyp has panicked and we're going to handle that by panicking the
 	 * kernel. The kernel offset will be revealed in the panic so we're
-- 
2.51.0.384.g4c02a37b29-goog



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] KVM: arm64: Map hyp text as RO and dump instr on panic
  2025-09-09 13:36 [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Mostafa Saleh
  2025-09-09 13:36 ` [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic Mostafa Saleh
@ 2025-09-09 13:36 ` Mostafa Saleh
  2025-09-15 12:07 ` [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Marc Zyngier
  2 siblings, 0 replies; 5+ messages in thread
From: Mostafa Saleh @ 2025-09-09 13:36 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel, kvmarm
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, perret, keirf, Mostafa Saleh

Map the hyp text section as RO, there are no secrets there
and that allows the kernel extract info for debugging.

As in case of panic we can now dump the faulting instructions
similar to the kernel.

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Acked-by: Will Deacon <will@kernel.org>
---
 arch/arm64/kvm/handle_exit.c    |  4 +---
 arch/arm64/kvm/hyp/nvhe/setup.c | 12 ++++++++++--
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 99a8205fc104..d449e15680e4 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -560,9 +560,7 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
 	kvm_nvhe_dump_backtrace(hyp_offset);
 
 	/* Dump the faulting instruction */
-	if (!is_protected_kvm_enabled() ||
-	    IS_ENABLED(CONFIG_NVHE_EL2_DEBUG))
-		dump_kernel_instr(panic_addr + kaslr_offset());
+	dump_kernel_instr(panic_addr + kaslr_offset());
 
 	/*
 	 * Hyp has panicked and we're going to handle that by panicking the
diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c
index a48d3f5a5afb..90bd014e952f 100644
--- a/arch/arm64/kvm/hyp/nvhe/setup.c
+++ b/arch/arm64/kvm/hyp/nvhe/setup.c
@@ -192,6 +192,7 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	enum pkvm_page_state state;
 	struct hyp_page *page;
 	phys_addr_t phys;
+	enum kvm_pgtable_prot prot;
 
 	if (!kvm_pte_valid(ctx->old))
 		return 0;
@@ -210,11 +211,18 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	 * configured in the hypervisor stage-1, and make sure to propagate them
 	 * to the hyp_vmemmap state.
 	 */
-	state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(ctx->old));
+	prot = kvm_pgtable_hyp_pte_prot(ctx->old);
+	state = pkvm_getstate(prot);
 	switch (state) {
 	case PKVM_PAGE_OWNED:
 		set_hyp_state(page, PKVM_PAGE_OWNED);
-		return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
+		/* hyp text is RO in the host stage-2 to be inspected on panic. */
+		if (prot == PAGE_HYP_EXEC) {
+			set_host_state(page, PKVM_NOPAGE);
+			return host_stage2_idmap_locked(phys, PAGE_SIZE, KVM_PGTABLE_PROT_R);
+		} else {
+			return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
+		}
 	case PKVM_PAGE_SHARED_OWNED:
 		set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
 		set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
-- 
2.51.0.384.g4c02a37b29-goog



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic
  2025-09-09 13:36 ` [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic Mostafa Saleh
@ 2025-09-15 10:54   ` Will Deacon
  0 siblings, 0 replies; 5+ messages in thread
From: Will Deacon @ 2025-09-15 10:54 UTC (permalink / raw)
  To: Mostafa Saleh
  Cc: linux-arm-kernel, linux-kernel, kvmarm, catalin.marinas, maz,
	oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, perret,
	keirf, Kunwu Chan

On Tue, Sep 09, 2025 at 01:36:30PM +0000, Mostafa Saleh wrote:
> Similar to the kernel panic, where the instruction code is printed,
> we can do the same for hypervisor panics.
> 
> This patch does that only in case of “CONFIG_NVHE_EL2_DEBUG” or nvhe.
> 
> The next patch adds support for pKVM.
> 
> Also, remove the hardcoded argument dump_kernel_instr().
> 
> Signed-off-by: Mostafa Saleh <smostafa@google.com>
> Tested-by: Kunwu Chan <chentao@kylinos.cn>
> Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
> ---
>  arch/arm64/include/asm/traps.h |  1 +
>  arch/arm64/kernel/traps.c      | 15 +++++++++------
>  arch/arm64/kvm/handle_exit.c   |  5 +++++
>  3 files changed, 15 insertions(+), 6 deletions(-)

Thanks for the respin:

Acked-by: Will Deacon <will@kernel.org>

Will


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe
  2025-09-09 13:36 [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Mostafa Saleh
  2025-09-09 13:36 ` [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic Mostafa Saleh
  2025-09-09 13:36 ` [PATCH v2 2/2] KVM: arm64: Map hyp text as RO and dump instr on panic Mostafa Saleh
@ 2025-09-15 12:07 ` Marc Zyngier
  2 siblings, 0 replies; 5+ messages in thread
From: Marc Zyngier @ 2025-09-15 12:07 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel, kvmarm, Mostafa Saleh
  Cc: catalin.marinas, will, oliver.upton, joey.gouly, suzuki.poulose,
	yuzenghui, perret, keirf

On Tue, 09 Sep 2025 13:36:29 +0000, Mostafa Saleh wrote:
> This small series, adds support for dumping the faulting instructions on
> panic in pKVM and nvhe, similarly to what the kernel does as follow:
> [   12.012126] kvm [190]: nVHE hyp BUG at: [<ffff8000811c5f64>] __kvm_nvhe_handle___kvm_vcpu_run+0x4/0x8!
> [   12.015747] kvm [190]: Cannot dump pKVM nVHE stacktrace: !CONFIG_PROTECTED_NVHE_STACKTRACE
> [   12.016044] Code: a8c17bfd d50323bf d65f03c0 d503245f (d4210000)
> [   12.016082] kvm [190]: Hyp Offset: 0xffeff6887fe00000
> [   12.016325] Kernel panic - not syncing: HYP panic:
> [   12.016325] PS:204023c9 PC:000f8978013c5f64 ESR:00000000f2000800
> [   12.016325] FAR:fff00000c016e01c HPFAR:00000000010016e0 PAR:0000000000000000
> 
> [...]

Applied to next, thanks!

[1/2] KVM: arm64: Dump instruction on hyp panic
      commit: 92b7624fe052ffb0b7b70d96cd514e02e91664a8
[2/2] KVM: arm64: Map hyp text as RO and dump instr on panic
      commit: 6f1ece1e868839cf81a28c77e19420ff57df7ecf

Cheers,

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




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-09-15 12:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-09 13:36 [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Mostafa Saleh
2025-09-09 13:36 ` [PATCH v2 1/2] KVM: arm64: Dump instruction on hyp panic Mostafa Saleh
2025-09-15 10:54   ` Will Deacon
2025-09-09 13:36 ` [PATCH v2 2/2] KVM: arm64: Map hyp text as RO and dump instr on panic Mostafa Saleh
2025-09-15 12:07 ` [PATCH v2 0/2] Dump instructions on panic for pKVM/nvhe Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox