KVM: arm64: Introduce hyp_dump_backtrace()
authorKalesh Singh <kaleshsingh@google.com>
Tue, 26 Jul 2022 07:37:44 +0000 (00:37 -0700)
committerMarc Zyngier <maz@kernel.org>
Tue, 26 Jul 2022 09:49:50 +0000 (10:49 +0100)
In non-protected nVHE mode, unwinds and dumps the hypervisor backtrace
from EL1. This is possible beacause the host can directly access the
hypervisor stack pages in non-protected mode.

The nVHE backtrace is dumped on hyp_panic(), before panicking the host.

[  101.498183] kvm [377]: nVHE call trace:
[  101.498363] kvm [377]:  [<ffff8000090a6570>] __kvm_nvhe_hyp_panic+0xac/0xf8
[  101.499045] kvm [377]:  [<ffff8000090a65cc>] __kvm_nvhe_hyp_panic_bad_stack+0x10/0x10
[  101.499498] kvm [377]:  [<ffff8000090a61e4>] __kvm_nvhe_recursive_death+0x24/0x34
. . .
[  101.524929] kvm [377]:  [<ffff8000090a61e4>] __kvm_nvhe_recursive_death+0x24/0x34
[  101.525062] kvm [377]:  [<ffff8000090a61e4>] __kvm_nvhe_recursive_death+0x24/0x34
[  101.525195] kvm [377]:  [<ffff8000090a5de4>] __kvm_nvhe___kvm_vcpu_run+0x30/0x40c
[  101.525333] kvm [377]:  [<ffff8000090a8b64>] __kvm_nvhe_handle___kvm_vcpu_run+0x30/0x48
[  101.525468] kvm [377]:  [<ffff8000090a88b8>] __kvm_nvhe_handle_trap+0xc4/0x128
[  101.525602] kvm [377]:  [<ffff8000090a7864>] __kvm_nvhe___host_exit+0x64/0x64
[  101.525745] kvm [377]: ---[ end nVHE call trace ]---

Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220726073750.3219117-12-kaleshsingh@google.com
arch/arm64/include/asm/stacktrace/nvhe.h
arch/arm64/kvm/handle_exit.c

index 21082fd4a0b7f80d3fa119981bab2708e6c3245e..170fe7459f7c108ef85fe96c313ce6e8451a7d1b 100644 (file)
 
 #include <asm/stacktrace/common.h>
 
+/*
+ * kvm_nvhe_unwind_init - Start an unwind from the given nVHE HYP fp and pc
+ *
+ * @state : unwind_state to initialize
+ * @fp    : frame pointer at which to start the unwinding.
+ * @pc    : program counter at which to start the unwinding.
+ */
+static inline void kvm_nvhe_unwind_init(struct unwind_state *state,
+                                       unsigned long fp,
+                                       unsigned long pc)
+{
+       unwind_init_common(state, NULL);
+
+       state->fp = fp;
+       state->pc = pc;
+}
+
 static inline bool on_hyp_stack(unsigned long sp, unsigned long size,
                                struct stack_info *info);
 
index f66c0142b3357d8266e6218d8199ff71975d54ed..e83e6f73510002304ad0a0f72ebbda4ef2482118 100644 (file)
@@ -17,6 +17,7 @@
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_mmu.h>
 #include <asm/debug-monitors.h>
+#include <asm/stacktrace/nvhe.h>
 #include <asm/traps.h>
 
 #include <kvm/arm_hypercalls.h>
@@ -318,6 +319,71 @@ void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index)
                kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu));
 }
 
+/*
+ * kvm_nvhe_dump_backtrace_entry - Symbolize and print an nVHE backtrace entry
+ *
+ * @arg    : the hypervisor offset, used for address translation
+ * @where  : the program counter corresponding to the stack frame
+ */
+static bool kvm_nvhe_dump_backtrace_entry(void *arg, unsigned long where)
+{
+       unsigned long va_mask = GENMASK_ULL(vabits_actual - 1, 0);
+       unsigned long hyp_offset = (unsigned long)arg;
+
+       /* Mask tags and convert to kern addr */
+       where = (where & va_mask) + hyp_offset;
+       kvm_err(" [<%016lx>] %pB\n", where, (void *)(where + kaslr_offset()));
+
+       return true;
+}
+
+static inline void kvm_nvhe_dump_backtrace_start(void)
+{
+       kvm_err("nVHE call trace:\n");
+}
+
+static inline void kvm_nvhe_dump_backtrace_end(void)
+{
+       kvm_err("---[ end nVHE call trace ]---\n");
+}
+
+/*
+ * hyp_dump_backtrace - Dump the non-protected nVHE backtrace.
+ *
+ * @hyp_offset: hypervisor offset, used for address translation.
+ *
+ * The host can directly access HYP stack pages in non-protected
+ * mode, so the unwinding is done directly from EL1. This removes
+ * the need for shared buffers between host and hypervisor for
+ * the stacktrace.
+ */
+static void hyp_dump_backtrace(unsigned long hyp_offset)
+{
+       struct kvm_nvhe_stacktrace_info *stacktrace_info;
+       struct unwind_state state;
+
+       stacktrace_info = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info);
+
+       kvm_nvhe_unwind_init(&state, stacktrace_info->fp, stacktrace_info->pc);
+
+       kvm_nvhe_dump_backtrace_start();
+       unwind(&state, kvm_nvhe_dump_backtrace_entry, (void *)hyp_offset);
+       kvm_nvhe_dump_backtrace_end();
+}
+
+/*
+ * kvm_nvhe_dump_backtrace - Dump KVM nVHE hypervisor backtrace.
+ *
+ * @hyp_offset: hypervisor offset, used for address translation.
+ */
+static void kvm_nvhe_dump_backtrace(unsigned long hyp_offset)
+{
+       if (is_protected_kvm_enabled())
+               return;
+       else
+               hyp_dump_backtrace(hyp_offset);
+}
+
 void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
                                              u64 elr_virt, u64 elr_phys,
                                              u64 par, uintptr_t vcpu,
@@ -353,6 +419,9 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
                                (void *)panic_addr);
        }
 
+       /* Dump the nVHE hypervisor backtrace */
+       kvm_nvhe_dump_backtrace(hyp_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