Skip to contentSkip to conversation

RISCV basic exception handling implementation

PATCH Superseded Oleksii Kurochko 3 participants 20 messages Jul 2, 2024 – Jul 11, 2024 RISC-V

v9 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v9 cover RISCV basic exception handling implementation

The patch series is based on:
Enable build of full Xen for RISC-V [1]
which haven't fully been merged yet.

The patch series provides a basic implementation of exception handling. It can do only basic things such as decode a cause of an exception, save/restore registers and execute "wfi" instruction if an exception can not be handled.

To verify that exception handling works well it was used macros from <asm/bug.h> such as BUG/WARN/run_in_exception/assert_failed.

It wasn't implemented show_execution_state() and stack trace discovering as it's not necessary now.

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#m8ee766297511fb6fb4a38b7753ab6b792361be04

---
Changes in V9:
- Update the cover letter message.
- s/early_printk/printk.
- use GENERIC_BUG_FRAME instead of arch-specific implementation of do_bug_frame().
- Rebase on top of current staging and riscv-full-xen-build-v14.
---
Changes in V8:
- Update the commit message of the cover letter.
- Remove the patch with an introduction of dummy asm/bug.h and introduce normal asm/bug.h
- Fix typo in return string from decode_reserved_interrupt_cause
- Add "Acked-by: Alistair Francis <[email protected]>" for the patch "xen/riscv: introduce decode_cause() stuff"
- Remove Pointless initializer of id in do_bug_frame().
- Make bug_frames[] array constant in do_bug_frame().
- Remove cast_to_bug_frame(addr).
- Rename is_valig_bugaddr to is_valid_bug_insn().
- Add check that read_instr is used only on Xen code
- Update the commit message.
---
Changes in V7:
- Update the depenency ( mentioned in the cover letter message ) of the current patch series.
- clean up comments.
- code style fixes.
- move definition of cast_to_bug_frame() from patch 4 to 5.
---
Changes in V6:
- Update the cover letter message: the patch set is based on MMU patch series.
- Introduce new patch with temporary printk functionality. ( it will be removed when Xen common code will be ready )
- Change early_printk() to printk().
- Remove usage of LINK_TO_LOAD() due to the MMU being enabled first.
- Add additional explanatory comments.
- Remove patch "xen/riscv: initialize boot_info structure" from the patch series.
---
Changes in V5:
- Rebase on top of [1] and [2]
- Add new patch which introduces stub for <asm/bug.h> to keep Xen compilable as in the patch [xen/riscv: introduce decode_cause() stuff] is used
header <xen/lib.h> which requires <asm/bug.h>.
- Remove <xen/error.h> from riscv/traps/c as nothing would require
inclusion.
- decode_reserved_interrupt_cause(), decode_interrupt_cause(),
decode_cause, do_unexpected_trap() were made as static they are expected
to be used only in traps.c
- Remove "#include <xen/types.h>" from <asm/bug.h> as there is no any need in it anymore
- Update macros GET_INSN_LENGTH: remove UL and 'unsigned int len;' from it
- Remove " include <xen/bug.h>" from risc/setup.c. it is not needed in the current version of the patch
- change an argument type from vaddr_t to uint32_t for is_valid_bugaddr and introduce read_instr() to read instruction properly as the length of qinstruction can be either 32 or 16 bits.
- Code style fixes
- update the comments before do_bug_frame() in riscv/trap.c
- [[PATCH v4 5/5] automation: modify RISC-V smoke test ] was dropped as it was provided more simple solution by Andrew. CI: Simplify RISCV smoke testing
- Refactor is_valid_bugaddr() function.
- 2 new patches ([PATCH v5 {1-2}/7]) were introduced, the goal of which is to recalculate addresses used in traps.c, which can be linker time relative. It is needed as we don't have enabled MMU yet.
---
Changes in V4:
- Rebase the patch series on top of new version of [introduce generic implementation of macros from bug.h] patch series.
- Update the cover letter message as 'Early printk' was merged and
the current one patch series is based only on [introduce generic implementation of macros from bug.h] which hasn't been commited yet.
- The following patches of the patch series were merged to staging:
[PATCH v3 01/14] xen/riscv: change ISA to r64G
[PATCH v3 02/14] xen/riscv: add <asm/asm.h> header
[PATCH v3 03/14] xen/riscv: add <asm/riscv_encoding.h header
[PATCH v3 04/14] xen/riscv: add <asm/csr.h> header
[PATCH v3 05/14] xen/riscv: introduce empty <asm/string.h>
[PATCH v3 06/14] xen/riscv: introduce empty <asm/cache.h>
[PATCH v3 07/14] xen/riscv: introduce exception context
[PATCH v3 08/14] xen/riscv: introduce exception handlers implementation [PATCH v3 10/14] xen/riscv: mask all interrupts
- Fix addressed comments in xen-devel mailing list.

---
Changes in V3:
- Change the name of config RISCV_ISA_RV64IMA to RISCV_ISA_RV64G as instructions from Zicsr and Zifencei extensions aren't part of I extension any more.
- Rebase the patch "xen/riscv: introduce an implementation of macros from <asm/bug.h>" on top of patch series [introduce generic implementation of macros from bug.h]
- Update commit messages
---
Changes in V2:
- take the latest riscv_encoding.h from OpenSBI, update it with Xen related changes, and update the commit message with "Origin:" tag and the commit message itself.
- add "Origin:" tag to the commit messag of the patch [xen/riscv: add <asm/csr.h> header].
- Remove the patch [xen/riscv: add early_printk_hnum() function] as the functionality provided by the patch isn't used now.
- Refactor prcoess.h: move structure offset defines to asm-offsets.c, change register_t to unsigned long.
- Refactor entry.S to use offsets defined in asm-offsets.C
- Rename {__,}handle_exception to handle_trap() and do_trap() to be more consistent with RISC-V spec.
- Merge the pathc which introduces do_unexpected_trap() with the patch [xen/riscv: introduce exception handlers implementation].
- Rename setup_trap_handler() to trap_init() and update correspondingly the patches in the patch series.
- Refactor bug.h, remove bug_instr_t type from it.
- Refactor decode_trap_cause() function to be more optimization-friendly.
- Add two new empty headers: <cache.h> and <string.h> as they are needed to include <xen/lib.h> which provides ARRAY_SIZE and other macros.
- Code style fixes.
---

Oleksii Kurochko (5):
xen/riscv: use printk() instead of early_printk() xen/riscv: introduce decode_cause() stuff xen/riscv: introduce trap_init() xen/riscv: enable GENERIC_BUG_FRAME xen/riscv: test basic handling stuff

 xen/arch/riscv/Kconfig             |   1 +
 xen/arch/riscv/include/asm/traps.h |   1 +
 xen/arch/riscv/setup.c             |  21 ++++-
 xen/arch/riscv/traps.c             | 123 ++++++++++++++++++++++++++++-
 xen/common/bug.c                   |   1 +
 5 files changed, 144 insertions(+), 3 deletions(-)
Signature · 1 line
2.45.2
#6 lore raw

PATCH v9 3/5 xen/riscv: introduce trap_init()

---
 xen/arch/riscv/include/asm/traps.h |  1 +
 xen/arch/riscv/setup.c             |  3 +++
 xen/arch/riscv/traps.c             | 12 ++++++++++++
 3 files changed, 16 insertions(+)
xen/arch/riscv/include/asm/traps.h +1 -0
index 3fef318478..c30118e095 100644
@@ -9,6 +9,7 @@
 
 void do_trap(struct cpu_user_regs *cpu_regs);
 void handle_trap(void);
+void trap_init(void);
 
 #endif /* __ASSEMBLY__ */
 
xen/arch/riscv/setup.c +3 -0
index e3cb0866d5..a6a29a1508 100644
@@ -8,6 +8,7 @@
 #include <public/version.h>
 
 #include <asm/early_printk.h>
+#include <asm/traps.h>
 
 void arch_get_xen_caps(xen_capabilities_info_t *info)
 {
@@ -23,6 +24,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 {
     remove_identity_mapping();
 
+    trap_init();
+
     printk("All set up\n");
 
     for ( ;; )
xen/arch/riscv/traps.c +12 -0
index 37cec40dfa..cb18b30ff2 100644
@@ -12,6 +12,18 @@
 #include <asm/riscv_encoding.h>
 #include <asm/traps.h>
 
+/*
+ * Initialize the trap handling.
+ *
+ * The function is called after MMU is enabled.
+ */
+void trap_init(void)
+{
+    unsigned long addr = (unsigned long)&handle_trap;
+
+    csr_write(CSR_STVEC, addr);
+}
+
 static const char *decode_trap_cause(unsigned long cause)
 {
     static const char *const trap_causes[] = {
Signature · 1 line
2.45.2
#10 lore raw

PATCH v9 4/5 xen/riscv: enable GENERIC_BUG_FRAME

In reply to Oleksii Kurochko
---
 xen/arch/riscv/Kconfig |  1 +
 xen/arch/riscv/traps.c | 31 +++++++++++++++++++++++++++++++
 xen/common/bug.c       |  1 +
 3 files changed, 33 insertions(+)
xen/arch/riscv/Kconfig +1 -0
index b4b354a778..74ad019fe7 100644
@@ -5,6 +5,7 @@ config RISCV
 config RISCV_64
 	def_bool y
 	select 64BIT
+	select GENERIC_BUG_FRAME
 
 config ARCH_DEFCONFIG
 	string
xen/arch/riscv/traps.c +31 -0
index cb18b30ff2..7ba16252fc 100644
@@ -5,6 +5,7 @@
  * RISC-V Trap handlers
  */
 
+#include <xen/bug.h>
 #include <xen/lib.h>
 #include <xen/sched.h>
 
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const struct cpu_user_regs *regs)
     die();
 }
 
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc + 1));
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc + 3));
+
+    return *(uint32_t *)pc;
+}
+
 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && ( do_bug_frame(cpu_regs, pc) >= 0 ) )
+    {
+        cpu_regs->sepc += GET_INSN_LENGTH(instr);
+        return;
+    }
+
     do_unexpected_trap(cpu_regs);
 }
 
xen/common/bug.c +1 -0
index b7c5d8fd4d..75cb35fcfa 100644
@@ -1,6 +1,7 @@
 #include <xen/bug.h>
 #include <xen/errno.h>
 #include <xen/kernel.h>
+#include <xen/lib.h>
 #include <xen/livepatch.h>
 #include <xen/string.h>
 #include <xen/types.h>
Signature · 1 line
2.45.2
#16 lore raw

PATCH v9 5/5 xen/riscv: test basic handling stuff

In reply to Oleksii Kurochko
---
 xen/arch/riscv/setup.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
xen/arch/riscv/setup.c +16 -0
index a6a29a1508..4f06203b46 100644
@@ -19,6 +19,20 @@ void arch_get_xen_caps(xen_capabilities_info_t *info)
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
+static void test_run_in_exception(const struct cpu_user_regs *regs)
+{
+    printk("If you see this message, ");
+    printk("run_in_exception_handler is most likely working\n");
+}
+
+static void test_macros_from_bug_h(void)
+{
+    run_in_exception_handler(test_run_in_exception);
+    WARN();
+    printk("If you see this message, ");
+    printk("WARN is most likely working\n");
+}
+
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
@@ -26,6 +40,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     trap_init();
 
+    test_macros_from_bug_h();
+
     printk("All set up\n");
 
     for ( ;; )
Signature · 1 line
2.45.2
#1 lore raw

PATCH v9 1/5 xen/riscv: use printk() instead of early_printk()

In reply to Oleksii Kurochko

As common code is available it is better to use printk() instead of early_printk().

Also the printing of "Hello from RISC-V world" is dropped as it is useless and "All set up is enough".

---
 xen/arch/riscv/setup.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
xen/arch/riscv/setup.c +1 -3
index 8bb5bdb2ae..e3cb0866d5 100644
@@ -23,9 +23,7 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 {
     remove_identity_mapping();
 
-    early_printk("Hello from C env\n");
-
-    early_printk("All set up\n");
+    printk("All set up\n");
 
     for ( ;; )
         asm volatile ("wfi");
Signature · 1 line
2.45.2
#4 lore raw

PATCH v9 2/5 xen/riscv: introduce decode_cause() stuff

In reply to Oleksii Kurochko

The patch introduces stuff needed to decode a reason of an exception.

---
 xen/arch/riscv/traps.c | 80 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)
xen/arch/riscv/traps.c +79 -1
index 5415cf8d90..37cec40dfa 100644
@@ -9,13 +9,91 @@
 #include <xen/sched.h>
 
 #include <asm/processor.h>
+#include <asm/riscv_encoding.h>
 #include <asm/traps.h>
 
-void do_trap(struct cpu_user_regs *cpu_regs)
+static const char *decode_trap_cause(unsigned long cause)
+{
+    static const char *const trap_causes[] = {
+        [CAUSE_MISALIGNED_FETCH] = "Instruction Address Misaligned",
+        [CAUSE_FETCH_ACCESS] = "Instruction Access Fault",
+        [CAUSE_ILLEGAL_INSTRUCTION] = "Illegal Instruction",
+        [CAUSE_BREAKPOINT] = "Breakpoint",
+        [CAUSE_MISALIGNED_LOAD] = "Load Address Misaligned",
+        [CAUSE_LOAD_ACCESS] = "Load Access Fault",
+        [CAUSE_MISALIGNED_STORE] = "Store/AMO Address Misaligned",
+        [CAUSE_STORE_ACCESS] = "Store/AMO Access Fault",
+        [CAUSE_USER_ECALL] = "Environment Call from U-Mode",
+        [CAUSE_SUPERVISOR_ECALL] = "Environment Call from S-Mode",
+        [CAUSE_MACHINE_ECALL] = "Environment Call from M-Mode",
+        [CAUSE_FETCH_PAGE_FAULT] = "Instruction Page Fault",
+        [CAUSE_LOAD_PAGE_FAULT] = "Load Page Fault",
+        [CAUSE_STORE_PAGE_FAULT] = "Store/AMO Page Fault",
+        [CAUSE_FETCH_GUEST_PAGE_FAULT] = "Instruction Guest Page Fault",
+        [CAUSE_LOAD_GUEST_PAGE_FAULT] = "Load Guest Page Fault",
+        [CAUSE_VIRTUAL_INST_FAULT] = "Virtualized Instruction Fault",
+        [CAUSE_STORE_GUEST_PAGE_FAULT] = "Guest Store/AMO Page Fault",
+    };
+
+    if ( cause < ARRAY_SIZE(trap_causes) && trap_causes[cause] )
+        return trap_causes[cause];
+    return "UNKNOWN";
+}
+
+static const char *decode_reserved_interrupt_cause(unsigned long irq_cause)
+{
+    switch ( irq_cause )
+    {
+    case IRQ_M_SOFT:
+        return "M-mode Software Interrupt";
+    case IRQ_M_TIMER:
+        return "M-mode Timer Interrupt";
+    case IRQ_M_EXT:
+        return "M-mode External Interrupt";
+    default:
+        return "UNKNOWN IRQ type";
+    }
+}
+
+static const char *decode_interrupt_cause(unsigned long cause)
+{
+    unsigned long irq_cause = cause & ~CAUSE_IRQ_FLAG;
+
+    switch ( irq_cause )
+    {
+    case IRQ_S_SOFT:
+        return "Supervisor Software Interrupt";
+    case IRQ_S_TIMER:
+        return "Supervisor Timer Interrupt";
+    case IRQ_S_EXT:
+        return "Supervisor External Interrupt";
+    default:
+        return decode_reserved_interrupt_cause(irq_cause);
+    }
+}
+
+static const char *decode_cause(unsigned long cause)
+{
+    if ( cause & CAUSE_IRQ_FLAG )
+        return decode_interrupt_cause(cause);
+
+    return decode_trap_cause(cause);
+}
+
+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));
+
     die();
 }
 
+void do_trap(struct cpu_user_regs *cpu_regs)
+{
+    do_unexpected_trap(cpu_regs);
+}
+
 void vcpu_show_execution_state(struct vcpu *v)
 {
     BUG_ON("unimplemented");
Signature · 1 line
2.45.2
Acked-by on v9 1/5
#2 lore raw

Re: [PATCH v9 1/5] xen/riscv: use printk() instead of early_printk()

In reply to Oleksii Kurochko
Quoted 2 lines · Oleksii Kurochko

As common code is available it is better to use printk() instead of early_printk().

IOW the function is not just available (allowing linking to succeed), but is also fully functional (i.e. the line is making it out, and hence CI, which iirc is looking for that string, is going to be "happy")? If so:

Jan

#7 lore raw

Re: [PATCH v9 3/5] xen/riscv: introduce trap_init()

In reply to Oleksii Kurochko

xen/arch/riscv/traps.c line 20

Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +6 -0
@@ -12,6 +12,18 @@
 #include <asm/riscv_encoding.h>
 #include <asm/traps.h>
 
+/*
+ * Initialize the trap handling.
+ *
+ * The function is called after MMU is enabled.
+ */
+void trap_init(void)

It may be deliberate that this isn't __init, but for the present needs it certainly could be. This is the kind of thing that imo want saying in the description (which sadly once again is entirely empty).

Jan

#11 lore raw

Re: [PATCH v9 4/5] xen/riscv: enable GENERIC_BUG_FRAME

In reply to Oleksii Kurochko
Quoted 12 lines · Oleksii Kurochko
---
 xen/arch/riscv/Kconfig |  1 +
 xen/arch/riscv/traps.c | 31 +++++++++++++++++++++++++++++++
 xen/common/bug.c       |  1 +
 3 files changed, 33 insertions(+)
xen/arch/riscv/Kconfig +0 -0
index b4b354a778..74ad019fe7 100644

xen/arch/riscv/Kconfig config RISCV · line 8

Quoted 4 lines
xen/arch/riscv/Kconfig +1 -0
@@ -5,6 +5,7 @@ config RISCV
 config RISCV_64
 	def_bool y
 	select 64BIT
+	select GENERIC_BUG_FRAME

Any particular reason to put this here, and not higher up with RISCV?

Quoted 14 lines
xen/arch/riscv/Kconfig +10 -0
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const struct cpu_user_regs *regs)
     die();
 }
 
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;

xen/arch/riscv/Kconfig static void do_unexpected_trap(const struct cpu_user_regs *regs) · line 124

Quoted 10 lines
xen/arch/riscv/Kconfig +10 -0
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc + 1));
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc + 3));
+
+    return *(uint32_t *)pc;
+}

Related to the point made further down: If either of these assertions fails, won't we come back again right here? If either of the is_kernel_*text() wasn't working quite right, wouldn't we be at risk of entering an infinite loop (presumably not quite infinite because of the stack overflowing at some point)?

xen/arch/riscv/Kconfig static void do_unexpected_trap(const struct cpu_user_regs *regs) · line 130

Quoted 6 lines
xen/arch/riscv/Kconfig +4 -0
 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && ( do_bug_frame(cpu_regs, pc) >= 0 ) )

No consideration of the kind of exception? I'd expect it is one very specific one which the BUG insn would raise, and then there's no point fetching the insn when it's a different kind of exception.

Further, nit: Certainly no need for the parentheses on the lhs of the &&. Having them on the rhs is a matter of taste, so okay, but then the blanks immediately inside will want dropping.

xen/common/bug.c line 7

Quoted 10 lines
xen/common/bug.c +1 -0
@@ -1,6 +1,7 @@
 #include <xen/bug.h>
 #include <xen/errno.h>
 #include <xen/kernel.h>
+#include <xen/lib.h>
 #include <xen/livepatch.h>
 #include <xen/string.h>
 #include <xen/types.h>

Unrelated change? Or did you simply forget to mention in the description why it's needed?

Jan

#17 lore raw

Re: [PATCH v9 5/5] xen/riscv: test basic handling stuff

In reply to Oleksii Kurochko
Quoted 17 lines · Oleksii Kurochko
---
 xen/arch/riscv/setup.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
xen/arch/riscv/setup.c +3 -0
index a6a29a1508..4f06203b46 100644
@@ -19,6 +19,20 @@ void arch_get_xen_caps(xen_capabilities_info_t *info)
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
+static void test_run_in_exception(const struct cpu_user_regs *regs)
+{
+    printk("If you see this message, ");

xen/arch/riscv/setup.c void arch_get_xen_caps(xen_capabilities_info_t *info) · line 34

Quoted 10 lines
xen/arch/riscv/setup.c +10 -0
+    printk("run_in_exception_handler is most likely working\n");
+}
+
+static void test_macros_from_bug_h(void)
+{
+    run_in_exception_handler(test_run_in_exception);
+    WARN();
+    printk("If you see this message, ");
+    printk("WARN is most likely working\n");
+}

While for the moment this may be okay, in the longer run WARN() will cause quite a bit of output that you don't want on every boot. The further plans here will want mentioning in the description.

Additionally as part of re-basing I think you would have wanted to put this under the (relatively new) SELF_TESTS Kconfig control.

Jan

Quoted 9 lines
xen/arch/riscv/setup.c +2 -0
@@ -26,6 +40,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     trap_init();
 
+    test_macros_from_bug_h();
+
     printk("All set up\n");
 
     for ( ;; )
#18 lore raw

Re: [PATCH v9 5/5] xen/riscv: test basic handling stuff

Quoted 33 lines · Jan Beulich
Quoted 17 lines · Oleksii Kurochko
---
 xen/arch/riscv/setup.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
xen/arch/riscv/setup.c +3 -0
index a6a29a1508..4f06203b46 100644
@@ -19,6 +19,20 @@ void arch_get_xen_caps(xen_capabilities_info_t *info)
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
+static void test_run_in_exception(const struct cpu_user_regs *regs)
+{
+    printk("If you see this message, ");

xen/arch/riscv/setup.c void arch_get_xen_caps(xen_capabilities_info_t *info) · line 34

Quoted 10 lines
xen/arch/riscv/setup.c +10 -0
+    printk("run_in_exception_handler is most likely working\n");
+}
+
+static void test_macros_from_bug_h(void)
+{
+    run_in_exception_handler(test_run_in_exception);
+    WARN();
+    printk("If you see this message, ");
+    printk("WARN is most likely working\n");
+}

While for the moment this may be okay, in the longer run WARN() will cause quite a bit of output that you don't want on every boot. The further plans here will want mentioning in the description.

Quoted 2 lines

Additionally as part of re-basing I think you would have wanted to put this under the (relatively new) SELF_TESTS Kconfig control.

Oh, and: Is it possible there's a word ("exception"?) missing from the title?

Jan

author
#3 lore raw

Re: [PATCH v9 1/5] xen/riscv: use printk() instead of early_printk()

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 2 lines · Oleksii Kurochko

As common code is available it is better to use printk() instead of early_printk().

IOW the function is not just available (allowing linking to succeed), but is also fully functional (i.e. the line is making it out, and hence
CI, which iirc is looking for that string, is going to be "happy")? If
so:

I ran our gitlab CI and it works well on my side:
https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/1355999714

~ Oleksii

author
#8 lore raw

Re: [PATCH v9 3/5] xen/riscv: introduce trap_init()

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +0 -0
@@ -12,6 +12,18 @@
  #include <asm/riscv_encoding.h>
  #include <asm/traps.h>


+/*
+ * Initialize the trap handling.
+ *
+ * The function is called after MMU is enabled.

Quoted 8 lines
Quoted 2 lines

+ */
+void trap_init(void)

It may be deliberate that this isn't __init, but for the present needs it
certainly could be. This is the kind of thing that imo want saying in the
description (which sadly once again is entirely empty).

I just missed to add __init so I have to added it in the next patch version.

Thanks for finding that.

~ Oleksii

#9 lore raw

Re: [PATCH v9 3/5] xen/riscv: introduce trap_init()

Quoted 19 lines · Oleksii
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +0 -0
@@ -12,6 +12,18 @@
  #include <asm/riscv_encoding.h>
  #include <asm/traps.h>


+/*
+ * Initialize the trap handling.
+ *

Quoted 8 lines
Quoted 3 lines

+ * The function is called after MMU is enabled.
+ */
+void trap_init(void)

It may be deliberate that this isn't __init, but for the present needs it
certainly could be. This is the kind of thing that imo want saying in the
description (which sadly once again is entirely empty).

Quoted 4 lines

I just missed to add __init so I have to added it in the next patch version.

Thanks for finding that.

Don't be so hasty.  It will definitely need to not be __init as soon as you add multi-CPU support to Xen.

Just say so in the commit message, and save yourself needing to undo the __init in a later patch.

~Andrew

author
#12 lore raw

Re: [PATCH v9 4/5] xen/riscv: enable GENERIC_BUG_FRAME

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko

---
 xen/arch/riscv/Kconfig |  1 + xen/arch/riscv/traps.c | 31 +++++++++++++++++++++++++++++++ xen/common/bug.c       |  1 +

 3 files changed, 33 insertions(+)
Quoted 11 lines

xen/arch/riscv/Kconfig config RISCV · line 8

Quoted 9 lines
xen/arch/riscv/Kconfig +0 -0
index b4b354a778..74ad019fe7 100644
@@ -5,6 +5,7 @@ config RISCV

 config RISCV_64
  def_bool y
  select 64BIT

xen/arch/riscv/Kconfig +1 -0
+	select GENERIC_BUG_FRAME

Any particular reason to put this here, and not higher up with RISCV?

Yes, you are right it would be better to put it inside "config RISCV".

Quoted 28 lines
Quoted 16 lines
xen/arch/riscv/Kconfig +0 -0
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const struct
 cpu_user_regs *regs)
      die();
  }

xen/arch/riscv/Kconfig +10 -0
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;

xen/arch/riscv/Kconfig

Quoted 10 lines
xen/arch/riscv/Kconfig +10 -0
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc + 1));
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc + 3));
+
+    return *(uint32_t *)pc;
+}

Related to the point made further down: If either of these assertions

Quoted 8 lines

fails,
won't we come back again right here? If either of the is_kernel_*text()
wasn't working quite right, wouldn't we be at risk of entering an infinite
loop (presumably not quite infinite because of the stack overflowing at some
point)?

It is really possible to have infinite loop here so it should be better to use 'if' with die() or panic().

Quoted 9 lines
Quoted 8 lines

 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && ( do_bug_frame(cpu_regs, pc) >= 0 ) )

Quoted 4 lines

No consideration of the kind of exception? I'd expect it is one very specific one which the BUG insn would raise, and then there's no point
fetching the insn when it's a different kind of exception.

Good point.

We should have 0x3 ( breakpoint exception ) in scause register. We can just check that without reading instruction and then also is_valid_bug_insn could be dropped too.

Quoted 8 lines

Further, nit: Certainly no need for the parentheses on the lhs of the &&.
Having them on the rhs is a matter of taste, so okay, but then the blanks immediately inside will want dropping.

Quoted 14 lines

xen/common/bug.c line 7

Quoted 10 lines
xen/common/bug.c +1 -0
@@ -1,6 +1,7 @@
  #include <xen/bug.h>
  #include <xen/errno.h>
  #include <xen/kernel.h>
+#include <xen/lib.h>
  #include <xen/livepatch.h>
  #include <xen/string.h>
  #include <xen/types.h>

Unrelated change? Or did you simply forget to mention in the description
why it's needed?

I added it to "Changes in ..." which I forgot to add, but I will add an explanation to the description. It is better place for it.

<xen/lib.h> is needed to be included for the reason that panic() and printk() is used in common/bug.c and RISC-V fails if it is not included with the following errors:
common/bug.c:69:9: error: implicit declaration of function 'printk' [-Werror=implicit-function-declaration]
69 | printk("Xen WARN at %s%s:%d\n", prefix, filename,

   lineno);
         |         ^~~~~~

common/bug.c:77:9: error: implicit declaration of function 'panic' [-Werror=implicit-function-declaration]
77 | panic("Xen BUG at %s%s:%d\n", prefix, filename,
lineno);

Quoted 2 lines

~ Oleksii

author
#19 lore raw

Re: [PATCH v9 5/5] xen/riscv: test basic handling stuff

In reply to Jan Beulich
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko

---
 xen/arch/riscv/setup.c | 16 ++++++++++++++++

 1 file changed, 16 insertions(+)
xen/arch/riscv/setup.c +0 -0
index a6a29a1508..4f06203b46 100644
@@ -19,6 +19,20 @@ void arch_get_xen_caps(xen_capabilities_info_t
 *info)

 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);

+static void test_run_in_exception(const struct cpu_user_regs *regs)

xen/arch/riscv/setup.c +2 -0
+{
+    printk("If you see this message, ");

xen/arch/riscv/setup.c void arch_get_xen_caps(xen_capabilities_info_t *info) · line 34

Quoted 10 lines
xen/arch/riscv/setup.c +10 -0
+    printk("run_in_exception_handler is most likely working\n");
+}
+
+static void test_macros_from_bug_h(void)
+{
+    run_in_exception_handler(test_run_in_exception);
+    WARN();
+    printk("If you see this message, ");
+    printk("WARN is most likely working\n");
+}

While for the moment this may be okay, in the longer run WARN() will cause
quite a bit of output that you don't want on every boot. The further plans
here will want mentioning in the description.

Quoted 3 lines
Quoted 3 lines

Additionally as part of re-basing I think you would have wanted to put this
under the (relatively new) SELF_TESTS Kconfig control.

I will consider SELF_TESTS Kconfig, there is not a lot of sense to print that every boot.

Quoted 3 lines

Oh, and: Is it possible there's a word ("exception"?) missing from the title?

Yes, I missed that. I will update the title to: " test basic exception handling stuff ". Thanks.

~ Oleksii

#13 lore raw

Re: [PATCH v9 4/5] xen/riscv: enable GENERIC_BUG_FRAME

In reply to Oleksii
Quoted 53 lines · Oleksii
Quoted 28 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const struct
 cpu_user_regs *regs)
      die();
  }

xen/arch/riscv/traps.c +10 -0
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;

xen/arch/riscv/traps.c static void do_unexpected_trap(const struct cpu_user_regs *regs) · line 124

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc + 1));
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc + 3));
+
+    return *(uint32_t *)pc;
+}

Related to the point made further down: If either of these assertions

Quoted 8 lines

fails,
won't we come back again right here? If either of the is_kernel_*text()
wasn't working quite right, wouldn't we be at risk of entering an infinite
loop (presumably not quite infinite because of the stack overflowing at some
point)?

It is really possible to have infinite loop here so it should be better to use 'if' with die() or panic().

Quoted 9 lines
Quoted 8 lines

 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && ( do_bug_frame(cpu_regs, pc) >= 0 ) )

Quoted 4 lines

No consideration of the kind of exception? I'd expect it is one very specific one which the BUG insn would raise, and then there's no point
fetching the insn when it's a different kind of exception.

Quoted 5 lines

Good point.

We should have 0x3 ( breakpoint exception ) in scause register. We can just check that without reading instruction and then also is_valid_bug_insn could be dropped too.

Just that then you'll also lose the is_kernel_*text() checking, which I understand is there to remind you/us that one this becomes reachable from non-Xen code, adjustments are going to be needed.

Jan

author
#14 lore raw

Re: [PATCH v9 4/5] xen/riscv: enable GENERIC_BUG_FRAME

Quoted 69 lines · Jan Beulich
Quoted 61 lines · Oleksii
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const

struct
cpu_user_regs *regs)
     die();
 }

xen/arch/riscv/traps.c +12 -0
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc +

1));

xen/arch/riscv/traps.c +5 -0
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc +

3));

xen/arch/riscv/traps.c static void do_unexpected_trap(const struct cpu_user_regs *regs) · line 124

Quoted 3 lines
xen/arch/riscv/traps.c +3 -0
+
+    return *(uint32_t *)pc;
+}

Related to the point made further down: If either of these assertions
fails,

Quoted 8 lines

won't we come back again right here? If either of the is_kernel_*text()
wasn't working quite right, wouldn't we be at risk of entering an infinite
loop (presumably not quite infinite because of the stack overflowing
at some
point)?

It is really possible to have infinite loop here so it should be better
to use 'if' with die() or panic().

Quoted 10 lines
Quoted 9 lines

 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && (
do_bug_frame(cpu_regs,
pc) >= 0 ) )

Quoted 5 lines

No consideration of the kind of exception? I'd expect it is one very
specific one which the BUG insn would raise, and then there's no point
fetching the insn when it's a different kind of exception.

Quoted 6 lines

Good point.

We should have 0x3 ( breakpoint exception ) in scause register. We can
just check that without reading instruction and then also is_valid_bug_insn could be dropped too.

Quoted 4 lines

Just that then you'll also lose the is_kernel_*text() checking, which I
understand is there to remind you/us that one this becomes reachable from non-Xen code, adjustments are going to be needed.

One thing I wrote incorrectly is that we still need fetch instruction or at least 16 bits to identify the length of instruction to set proper sepc:

    cpu_regs->sepc += GET_INSN_LENGTH(instr);

We could write that in the following way:

    cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);

Would it be okay?

~ Oleksii

#15 lore raw

Re: [PATCH v9 4/5] xen/riscv: enable GENERIC_BUG_FRAME

Quoted 74 lines · Oleksii
Quoted 69 lines · Jan Beulich
Quoted 61 lines · Oleksii
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -101,8 +102,38 @@ static void do_unexpected_trap(const

struct
cpu_user_regs *regs)
     die();
 }

xen/arch/riscv/traps.c +12 -0
+static bool is_valid_bug_insn(uint32_t insn)
+{
+    return insn == BUG_INSN_32 ||
+           (insn & COMPRESSED_INSN_MASK) == BUG_INSN_16;
+}
+
+/* Should be used only on Xen code */
+static uint32_t read_instr(unsigned long pc)
+{
+    uint16_t instr16 = *(uint16_t *)pc;
+
+    ASSERT(is_kernel_text(pc + 1) || is_kernel_inittext(pc +

1));

xen/arch/riscv/traps.c +5 -0
+
+    if ( GET_INSN_LENGTH(instr16) == 2 )
+        return instr16;
+
+    ASSERT(is_kernel_text(pc + 3) || is_kernel_inittext(pc +

3));

xen/arch/riscv/traps.c static void do_unexpected_trap(const struct cpu_user_regs *regs) · line 124

Quoted 3 lines
xen/arch/riscv/traps.c +3 -0
+
+    return *(uint32_t *)pc;
+}

Related to the point made further down: If either of these assertions
fails,

Quoted 8 lines

won't we come back again right here? If either of the is_kernel_*text()
wasn't working quite right, wouldn't we be at risk of entering an infinite
loop (presumably not quite infinite because of the stack overflowing
at some
point)?

It is really possible to have infinite loop here so it should be better
to use 'if' with die() or panic().

Quoted 10 lines
Quoted 9 lines

 void do_trap(struct cpu_user_regs *cpu_regs)
 {
+    register_t pc = cpu_regs->sepc;
+    uint32_t instr = read_instr(pc);
+
+    if ( ( is_valid_bug_insn(instr) ) && (
do_bug_frame(cpu_regs,
pc) >= 0 ) )

Quoted 5 lines

No consideration of the kind of exception? I'd expect it is one very
specific one which the BUG insn would raise, and then there's no point
fetching the insn when it's a different kind of exception.

Quoted 6 lines

Good point.

We should have 0x3 ( breakpoint exception ) in scause register. We can
just check that without reading instruction and then also is_valid_bug_insn could be dropped too.

Quoted 4 lines

Just that then you'll also lose the is_kernel_*text() checking, which I
understand is there to remind you/us that one this becomes reachable from non-Xen code, adjustments are going to be needed.

Quoted 8 lines

One thing I wrote incorrectly is that we still need fetch instruction or at least 16 bits to identify the length of instruction to set proper sepc:

    cpu_regs->sepc += GET_INSN_LENGTH(instr);

We could write that in the following way:

    cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);

Would it be okay?

I think so, as long as you retain the assertion in some way, ahead of the deref of pc.

Jan