Skip to contentSkip to conversation

xen/riscv: dump CSRs on unexpected traps

PATCH Superseded Oleksii Kurochko 2 participants 5 messages Jan 15 RISC-V

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/riscv: dump CSRs on unexpected traps

Provide more context on the exception state when an unexpected exception occurs by dumping various Supervisor, Virtual Supervisor and Hypervisor CSRs.

---
 xen/arch/riscv/traps.c | 55 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
xen/arch/riscv/traps.c +55 -0
index e9c967786312..d6c92588ea47 100644
@@ -17,6 +17,10 @@
 #include <asm/traps.h>
 #include <asm/vsbi.h>
 
+#define print_csr(csr) do { \
+    printk("\t" #csr ": %#02lx\n", csr_read(csr)); \
+} while ( 0 )
+
 /*
  * Initialize the trap handling.
  *
@@ -99,12 +103,63 @@ static const char *decode_cause(unsigned long cause)
     return decode_trap_cause(cause);
 }
 
+static void dump_csrs(unsigned long cause)
+{
+    unsigned long hstatus;
+    bool gva;
+
+    printk("\nDumping CSRs...\n");
+
+    printk("Supervisor CSRs\n");
+    print_csr(CSR_STVEC);
+    print_csr(CSR_SATP);
+    print_csr(CSR_SEPC);
+
+    hstatus = csr_read(CSR_HSTATUS);
+    gva = !!(hstatus & HSTATUS_GVA);
+
+    printk("\tCSR_STVAL: %#02lx%s\n", csr_read(CSR_STVAL),
+           gva ? ", (guest virtual address)" : "");
+
+    printk("\tCSR_SCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_SSTATUS);
+
+    printk("\nVirtual Supervisor CSRs\n");
+    print_csr(CSR_VSTVEC);
+    print_csr(CSR_VSATP);
+    print_csr(CSR_VSEPC);
+    print_csr(CSR_VSTVAL);
+    cause = csr_read(CSR_VSCAUSE);
+    printk("\tCSR_VSCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_VSSTATUS);
+
+    printk("\nHypervisor CSRs\n");
+
+    print_csr(CSR_HSTATUS);
+    printk("\t\thstatus.VTSR=%d\n", !!(hstatus & HSTATUS_VTSR));
+    printk("\t\thstatus.VTVM=%d\n", !!(hstatus & HSTATUS_VTVM));
+    printk("\t\thstatus.HU=%d\n", !!(hstatus & HSTATUS_HU));
+    printk("\t\thstatus.SPVP=%d\n", !!(hstatus & HSTATUS_SPVP));
+    printk("\t\thstatus.SPV=%d\n", !!(hstatus & HSTATUS_SPV));
+    printk("\t\thstatus.GVA=%d\n", !!(hstatus & HSTATUS_GVA));
+    print_csr(CSR_HGATP);
+    print_csr(CSR_HTVAL);
+    print_csr(CSR_HTINST);
+    print_csr(CSR_HEDELEG);
+    print_csr(CSR_HIDELEG);
+    print_csr(CSR_HSTATEEN0);
+}
+
 static void do_unexpected_trap(const struct cpu_user_regs *regs)
 {
     unsigned long cause = csr_read(CSR_SCAUSE);
 
     printk("Unhandled exception: %s\n", decode_cause(cause));
 
+    dump_csrs(cause);
+
     die();
 }
 
Signature · 1 line
2.52.0
#1 lore raw

xen/arch/riscv/traps.c line 21

Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +2 -0
@@ -17,6 +17,10 @@
 #include <asm/traps.h>
 #include <asm/vsbi.h>
 
+#define print_csr(csr) do { \
+    printk("\t" #csr ": %#02lx\n", csr_read(csr)); \

Why the 02 in the format? If you wanted to align things, you'd use %016lx. (I also don't think the 0x prefixes are overly useful in such dumping, but others may disagree.) As an aside, the width of 2 would be fully consumed by your use of #, except for zero which would (oddly) be printed as 00 afaict.

xen/arch/riscv/traps.c line 22

Quoted 1 line
xen/arch/riscv/traps.c +1 -0
+} while ( 0 )

Why the do/while wrapping, btw?

Quoted 8 lines
xen/arch/riscv/traps.c +4 -0
@@ -99,12 +103,63 @@ static const char *decode_cause(unsigned long cause)
     return decode_trap_cause(cause);
 }
 
+static void dump_csrs(unsigned long cause)
+{
+    unsigned long hstatus;
+    bool gva;

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 119

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+
+    printk("\nDumping CSRs...\n");
+
+    printk("Supervisor CSRs\n");
+    print_csr(CSR_STVEC);
+    print_csr(CSR_SATP);
+    print_csr(CSR_SEPC);
+
+    hstatus = csr_read(CSR_HSTATUS);
+    gva = !!(hstatus & HSTATUS_GVA);

No need for !! when the lhs type is bool. Question is whether gva is useful to have as a local in the first place, when ...

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 122

Quoted 2 lines
xen/arch/riscv/traps.c +2 -0
+    printk("\tCSR_STVAL: %#02lx%s\n", csr_read(CSR_STVAL),
+           gva ? ", (guest virtual address)" : "");

... this it's sole use (you don't use where you could further down).

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 136

Quoted 13 lines
xen/arch/riscv/traps.c +13 -0
+    printk("\tCSR_SCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_SSTATUS);
+
+    printk("\nVirtual Supervisor CSRs\n");
+    print_csr(CSR_VSTVEC);
+    print_csr(CSR_VSATP);
+    print_csr(CSR_VSEPC);
+    print_csr(CSR_VSTVAL);
+    cause = csr_read(CSR_VSCAUSE);
+    printk("\tCSR_VSCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_VSSTATUS);

Everything below I understand wants dumping, but for much of the above that's less clear to me. Why would guest's values be relevant when we have a hypervisor problem?

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 140

Quoted 3 lines
xen/arch/riscv/traps.c +3 -0
+    printk("\nHypervisor CSRs\n");
+
+    print_csr(CSR_HSTATUS);

Above you special-case VSCAUSE, but here you re-read HSTATUS.

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 146

Quoted 6 lines
xen/arch/riscv/traps.c +6 -0
+    printk("\t\thstatus.VTSR=%d\n", !!(hstatus & HSTATUS_VTSR));
+    printk("\t\thstatus.VTVM=%d\n", !!(hstatus & HSTATUS_VTVM));
+    printk("\t\thstatus.HU=%d\n", !!(hstatus & HSTATUS_HU));
+    printk("\t\thstatus.SPVP=%d\n", !!(hstatus & HSTATUS_SPVP));
+    printk("\t\thstatus.SPV=%d\n", !!(hstatus & HSTATUS_SPV));
+    printk("\t\thstatus.GVA=%d\n", !!(hstatus & HSTATUS_GVA));

Might these better be put on a single line, e.g. in the form

[VTSR SPVP SPV]

i.e. enumerating the (interesting) set bits textually?

Quoted 8 lines
xen/arch/riscv/traps.c +8 -0
+    print_csr(CSR_HGATP);
+    print_csr(CSR_HTVAL);
+    print_csr(CSR_HTINST);
+    print_csr(CSR_HEDELEG);
+    print_csr(CSR_HIDELEG);
+    print_csr(CSR_HSTATEEN0);
+}
+

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 164

Quoted 10 lines
xen/arch/riscv/traps.c +2 -0
 static void do_unexpected_trap(const struct cpu_user_regs *regs)
 {
     unsigned long cause = csr_read(CSR_SCAUSE);
 
     printk("Unhandled exception: %s\n", decode_cause(cause));
 
+    dump_csrs(cause);
+
     die();
 }

Apart from CSRs, how about also dumping GPRs?

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +0 -0
@@ -17,6 +17,10 @@
  #include <asm/traps.h>
  #include <asm/vsbi.h>
 
Quoted 6 lines

xen/arch/riscv/traps.c line 21

Quoted 2 lines
xen/arch/riscv/traps.c +2 -0
+#define print_csr(csr) do { \
+    printk("\t" #csr ": %#02lx\n", csr_read(csr)); \

Why the 02 in the format? If you wanted to align things, you'd use %016lx. (I also don't think the 0x prefixes are overly useful in such dumping, but others may disagree.) As an aside, the width of 2 would be fully consumed by your use of #, except for zero which would (oddly) be printed as 00 afaict.

I used before 0x%02lx and after switch to %# don't think that 02 would fully consumed by #.

I am okay to use just %lx.

Quoted 3 lines

xen/arch/riscv/traps.c line 22

Quoted 2 lines
xen/arch/riscv/traps.c +1 -0
+} while ( 0 )

Why the do/while wrapping, btw?

Added it automatically, there is no really to have it here. I'll drop.

Quoted 22 lines
Quoted 9 lines
xen/arch/riscv/traps.c +4 -0
@@ -99,12 +103,63 @@ static const char *decode_cause(unsigned long cause)
      return decode_trap_cause(cause);
  }
 
+static void dump_csrs(unsigned long cause)
+{
+    unsigned long hstatus;
+    bool gva;

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 119

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+
+    printk("\nDumping CSRs...\n");
+
+    printk("Supervisor CSRs\n");
+    print_csr(CSR_STVEC);
+    print_csr(CSR_SATP);
+    print_csr(CSR_SEPC);
+
+    hstatus = csr_read(CSR_HSTATUS);
+    gva = !!(hstatus & HSTATUS_GVA);

No need for !! when the lhs type is bool. Question is whether gva is useful to have as a local in the first place, when ...

Quoted 3 lines

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 122

Quoted 2 lines
xen/arch/riscv/traps.c +2 -0
+    printk("\tCSR_STVAL: %#02lx%s\n", csr_read(CSR_STVAL),
+           gva ? ", (guest virtual address)" : "");

... this it's sole use (you don't use where you could further down).

Agree, with such usage there is no really necessity for it.

Quoted 14 lines
Quoted 4 lines
xen/arch/riscv/traps.c +3 -0
+    printk("\tCSR_SCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_SSTATUS);

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 136

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+
+    printk("\nVirtual Supervisor CSRs\n");
+    print_csr(CSR_VSTVEC);
+    print_csr(CSR_VSATP);
+    print_csr(CSR_VSEPC);
+    print_csr(CSR_VSTVAL);
+    cause = csr_read(CSR_VSCAUSE);
+    printk("\tCSR_VSCAUSE: %#02lx\n", cause);
+    printk("\t\tDescription: %s\n", decode_cause(cause));
+    print_csr(CSR_VSSTATUS);
Quoted 3 lines

Everything below I understand wants dumping, but for much of the above that's less clear to me. Why would guest's values be relevant when we have a hypervisor problem?

It could be useful when we jump to guest, something wasn't configured correctly and so lets say an illegal instruction in guest happen and so it would be useful to at least understand what is this instruction based on VSEPC or VSTVAL.

All others probaly there is no need to have printed, I don't remember that I used them during debug.

Quoted 5 lines

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 140

Quoted 4 lines
xen/arch/riscv/traps.c +3 -0
+    printk("\nHypervisor CSRs\n");
+
+    print_csr(CSR_HSTATUS);

Above you special-case VSCAUSE, but here you re-read HSTATUS.

Because above I re-used 'cause' then for decode_cause().

Quoted 7 lines

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 146

Quoted 7 lines
xen/arch/riscv/traps.c +6 -0
+    printk("\t\thstatus.VTSR=%d\n", !!(hstatus & HSTATUS_VTSR));
+    printk("\t\thstatus.VTVM=%d\n", !!(hstatus & HSTATUS_VTVM));
+    printk("\t\thstatus.HU=%d\n", !!(hstatus & HSTATUS_HU));
+    printk("\t\thstatus.SPVP=%d\n", !!(hstatus & HSTATUS_SPVP));
+    printk("\t\thstatus.SPV=%d\n", !!(hstatus & HSTATUS_SPV));
+    printk("\t\thstatus.GVA=%d\n", !!(hstatus & HSTATUS_GVA));
Quoted 5 lines

Might these better be put on a single line, e.g. in the form

[VTSR SPVP SPV]

i.e. enumerating the (interesting) set bits textually?

Agree, visually it would be better.

Quoted 13 lines
Quoted 13 lines
xen/arch/riscv/traps.c +8 -0
+    print_csr(CSR_HGATP);
+    print_csr(CSR_HTVAL);
+    print_csr(CSR_HTINST);
+    print_csr(CSR_HEDELEG);
+    print_csr(CSR_HIDELEG);
+    print_csr(CSR_HSTATEEN0);
+}
+
  static void do_unexpected_trap(const struct cpu_user_regs *regs)
  {
      unsigned long cause = csr_read(CSR_SCAUSE);
 
Quoted 7 lines

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 164

Quoted 6 lines
xen/arch/riscv/traps.c +2 -0
      printk("Unhandled exception: %s\n", decode_cause(cause));
 
+    dump_csrs(cause);
+
      die();
  }

Apart from CSRs, how about also dumping GPRs?

Maybe, it is a good idea and it would be nice to add them.

I just almost never needed them for debugging so I am not printing them.

~ Oleksii

#3 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 164

Quoted 10 lines
  static void do_unexpected_trap(const struct cpu_user_regs *regs)
  {
      unsigned long cause = csr_read(CSR_SCAUSE);
      printk("Unhandled exception: %s\n", decode_cause(cause));
xen/arch/riscv/traps.c +2 -0
+    dump_csrs(cause);
+
      die();
  }

Apart from CSRs, how about also dumping GPRs?

Just to double-check, do you mean GPRs which are stired in regs argument of do_unexpected_trap?

~ Oleksii

#4 lore raw
Quoted 7 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 6 lines
  static void do_unexpected_trap(const struct cpu_user_regs *regs)
  {
      unsigned long cause = csr_read(CSR_SCAUSE);
      printk("Unhandled exception: %s\n", decode_cause(cause));
Quoted 8 lines
Quoted 5 lines

xen/arch/riscv/traps.c static const char *decode_cause(unsigned long cause) · line 164

Quoted 4 lines
xen/arch/riscv/traps.c +2 -0
+    dump_csrs(cause);
+
      die();
  }

Apart from CSRs, how about also dumping GPRs?

Just to double-check, do you mean GPRs which are stired in regs argument of do_unexpected_trap?

Sure, those are the ones pertaining to the trap.

Jan