Skip to contentSkip to conversation

RISCV basic exception handling implementation

PATCH Superseded Oleksii Kurochko 2 participants 18 messages Jul 24, 2024 – Aug 2, 2024 RISC-V

v11 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#7 lore raw

PATCH v11 4/5 xen/riscv: enable GENERIC_BUG_FRAME

In reply to Oleksii Kurochko

To have working BUG(), WARN(), ASSERT, run_in_exception_handler() it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak instruction was executed.

Also, <xen/lib.h> is needed to be included for the reason that panic() and printk() are used in common/bug.c and RISC-V fails if it is not included.

---
Changes in V11:
- update the commit message
- change "%lx" to "%#x" for PC register printing.
- drop +1 in argument of is_kernel_text(pc) and is_kernel_inittext(pc).
- drop return for case CAUSE_BREAKPOINT.
- add break to default and add a blank like above it.
- add a comment CAUSE_BREAKPOINT is handled instead of illegal instruction.
---
Changes in V10:
- put 'select GENERIC_BUG_FRAME' in "Config RISCV".
- rework do_trap() to not fetch an instruction in case when the cause of trap
is BUG_insn.
- drop read_instr() and is_valid_bug_insn().
- update the commit message.
---
Changes in V9:
- Rebase on the top of current staging.
- use GENERIC_BUG_FRAME as now we have common code available.
- add xen/lib.h to bug.c to fix a compilation error around printk.
- update the commit message.
- update the code of read_instr() in traps.c
- fold two-s if into 1 in do_trap.
---
Changes in V8:
- remove Pointless initializer of id.
- make bug_frames[] array constant.
- 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:
- move to this patch the definition of cast_to_bug_frame() from the previous patch.
- update the comment in bug.h.
- update the comment above do_bug_frame().
- fix code style.
- add comment to read_instr func.
- add space for bug_frames in lds.S.
---
Changes in V6:
- Avoid LINK_TO_LOAD() as bug.h functionality expected to be used
after MMU is enabled.
- Change early_printk() to printk()
---
Changes in V5:
- 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
- Refactor is_valid_bugaddr() function.
- introduce macros cast_to_bug_frame(addr) to hide casts.
- use LINK_TO_LOAD() for addresses which are linker time relative.
---
Changes in V4:
- Updates in RISC-V's <asm/bug.h>:
* Add explanatory comment about why there is only defined for 32-bits length instructions and 16/32-bits BUG_INSN_{16,32}.
* Change 'unsigned long' to 'unsigned int' inside GET_INSN_LENGTH().
* Update declaration of is_valid_bugaddr(): switch return type from int to bool and the argument from 'unsigned int' to 'vaddr'.
- Updates in RISC-V's traps.c:
* replace /xen and /asm includes
* update definition of is_valid_bugaddr():switch return type from int to bool
and the argument from 'unsigned int' to 'vaddr'. Code style inside function
was updated too.
* do_bug_frame() refactoring:
* local variables start and bug became 'const struct bug_frame'
* bug_frames[] array became 'static const struct bug_frame[] = ...'
* remove all casts
* remove unneeded comments and add an explanatory comment that the do_bug_frame() will be switched to a generic one.
* do_trap() refactoring:
* read 16-bits value instead of 32-bits as compressed instruction can
be used and it might happen than only 16-bits may be accessible.
* code style updates
* re-use instr variable instead of re-reading instruction.
- Updates in setup.c:
* add blank line between xen/ and asm/ includes.
---
Changes in V3:
- 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]
---
Changes in V2:
- Remove __ in define namings
- Update run_in_exception_handler() with

    register void *fn_ asm(__stringify(BUG_FN_REG)) = (fn);
  - Remove bug_instr_t type and change it's usage to uint32_t
---
 xen/arch/riscv/Kconfig |  1 +
 xen/arch/riscv/traps.c | 35 ++++++++++++++++++++++++++++++++++-
 xen/common/bug.c       |  1 +
 3 files changed, 36 insertions(+), 1 deletion(-)
xen/arch/riscv/Kconfig +1 -0
index b4b354a778..f531e96657 100644
@@ -1,6 +1,7 @@
 config RISCV
 	def_bool y
 	select FUNCTION_ALIGNMENT_16B
+	select GENERIC_BUG_FRAME
 
 config RISCV_64
 	def_bool y
xen/arch/riscv/traps.c +34 -1
index cb18b30ff2..6802827eb5 100644
@@ -5,6 +5,7 @@
  * RISC-V Trap handlers
  */
 
+#include <xen/bug.h>
 #include <xen/lib.h>
 #include <xen/sched.h>
 
@@ -103,7 +104,39 @@ static void do_unexpected_trap(const struct cpu_user_regs *regs)
 
 void do_trap(struct cpu_user_regs *cpu_regs)
 {
-    do_unexpected_trap(cpu_regs);
+    register_t pc = cpu_regs->sepc;
+    unsigned long cause = csr_read(CSR_SCAUSE);
+
+    switch ( cause )
+    {
+    /*
+     * ebreak is used as BUG_insn so when macros from <xen/bug.h> are
+     * used, an exception with BREAKPOINT cause will be generated.
+     *
+     * ebreak triggers a debug trap, which starts in debug mode and is
+     * then filtered by every mode. A debugger will first handle the
+     * debug trap and check if ebreak was set by it or not. If not,
+     * CAUSE_BREAKPOINT will be generated for the mode where the ebreak
+     * instruction was executed.
+     */
+    case CAUSE_BREAKPOINT:
+        if ( do_bug_frame(cpu_regs, pc) >= 0 )
+        {
+            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) )
+            {
+                printk("Something wrong with PC: %#lx\n", pc);
+                die();
+            }
+
+            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
+        }
+
+        break;
+
+    default:
+        do_unexpected_trap(cpu_regs);
+        break;
+    }
 }
 
 void vcpu_show_execution_state(struct vcpu *v)
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
#2 lore raw

PATCH v11 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.

---
Changes in V11:
- Nothing changed. Only rebase.
---
Changes in V10:
- add Acked-by: Jan Beulich <[email protected]>
---
Changes in V9:
- This patch was reverted as breaks both release and randconfig builds.
I don't see the failures now. ( probably it was because of printk usage
which was not ready at that moment ).
- drop inclusion of <asm/csr.h> and <asm/early_printk.h>
- add <asm/riscv_encoding.h> for CAUSE_* in decode_trap_cause().
---
Changes in V8:
- fix typo in return string from decode_reserved_interrupt_cause
- add Acked-by: Alistair Francis <[email protected]>
---
Changes in V7:
- Nothing changed. Only rebase.
---
Changes in V6:
- Remove usage of LINK_TO_LOAD() due to the MMU being enabled first.
- Change early_printk() to printk()
---
Changes in V5:
- 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
- use LINK_TO_LOAD() for addresses which can be linker time relative.
---
Changes in V4:
- fix string in decode_reserved_interrupt_cause()
---
Changes in V3:
- Nothing changed
---
Changes in V2:
- Make decode_trap_cause() more optimization friendly.
- Merge the pathc which introduces do_unexpected_trap() to the current one.

---
 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
#6 lore raw

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

In reply to Oleksii Kurochko

Note that trap_init() isn't declared with the __init attribute to avoid removing __init when multi-CPU support for Xen is added.

---
Changes in V11:
- update the commit message
- add Acked-by: Jan Beulich <[email protected]>
---
Changes in V10:
- update the commit message
---
Changes in V9:
- drop inclusion of <asm/mm.h> in setup.c
- drop #include <public/version.h>
- ... <xen/bug.h>
- introduce stub for
void arch_get_xen_caps(xen_capabilities_info_t *info);

- resolve rebase conflicts.
---
Changes in V8:
- nothing changed. only rebase was done.
---
Changes in V7:
- #define cast_to_bug_frame(addr) ((const struct bug_frame *)(addr)).
- remove unnecessary comments in trap_init() function.
---
Changes in V6:
- trap_init() is now called after enabling the MMU.
- Add additional explanatory comments.
---
Changes in V5:
- Nothing changed
---
Changes in V4:
- Nothing changed
---
Changes in V3:
- Nothing changed
---
Changes in V2:
- Rename setup_trap_handler() to trap_init().
- Add Reviewed-by to the commit message.

---
 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
#1 lore raw

PATCH v11 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".

---
Changes in V11:
- Nothing changed. Only rebase.
---
Changes in V10:
- add Acked-by: Jan Beulich <[email protected]>
---
Changes in V9:
- new patch

---
 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
#17 lore raw

PATCH v11 5/5 xen/riscv: test basic exception handling stuff

In reply to Oleksii Kurochko

Introduces testing of some macros from <xen/bug.h>.

Also wraps this testing into SELF_TESTS config to not produce a noise in the log related to functionality testing ( in the current case, it is macros from xen/bug.h ) when CONFIG_SELF_TESTS is disabled.

---
 xen/arch/riscv/setup.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/arch/riscv/setup.c +20 -0
index a6a29a1508..4defad68f4 100644
@@ -19,6 +19,22 @@ void arch_get_xen_caps(xen_capabilities_info_t *info)
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
+#ifdef CONFIG_SELF_TESTS
+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");
+}
+#endif
+
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
@@ -26,6 +42,10 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     trap_init();
 
+#ifdef CONFIG_SELF_TESTS
+    test_macros_from_bug_h();
+#endif
+
     printk("All set up\n");
 
     for ( ;; )
Signature · 1 line
2.45.2
#0 lore raw

v11 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 V10 - V11:
- No generic changes to describe here. Please look at patch-specific changes.
---
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 exception handling stuff

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

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

In reply to Oleksii Kurochko
Quoted 11 lines · Oleksii Kurochko

To have working BUG(), WARN(), ASSERT, run_in_exception_handler() it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak instruction was executed.

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

Quoted 23 lines
xen/arch/riscv/traps.c +18 -1
@@ -103,7 +104,39 @@ static void do_unexpected_trap(const struct cpu_user_regs *regs)
 
 void do_trap(struct cpu_user_regs *cpu_regs)
 {
-    do_unexpected_trap(cpu_regs);
+    register_t pc = cpu_regs->sepc;
+    unsigned long cause = csr_read(CSR_SCAUSE);
+
+    switch ( cause )
+    {
+    /*
+     * ebreak is used as BUG_insn so when macros from <xen/bug.h> are
+     * used, an exception with BREAKPOINT cause will be generated.
+     *
+     * ebreak triggers a debug trap, which starts in debug mode and is
+     * then filtered by every mode. A debugger will first handle the
+     * debug trap and check if ebreak was set by it or not. If not,
+     * CAUSE_BREAKPOINT will be generated for the mode where the ebreak
+     * instruction was executed.
+     */
+    case CAUSE_BREAKPOINT:
+        if ( do_bug_frame(cpu_regs, pc) >= 0 )
+        {

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

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) )
+            {
+                printk("Something wrong with PC: %#lx\n", pc);
+                die();
+            }
+
+            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
+        }
+
+        break;

Wouldn't you better fall through here, with the "break" moved into the if()'s body?

Jan

Quoted 5 lines
xen/arch/riscv/traps.c +4 -0
+    default:
+        do_unexpected_trap(cpu_regs);
+        break;
+    }
 }
#9 lore raw

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

Quoted 13 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko

To have working BUG(), WARN(), ASSERT, run_in_exception_handler() it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak instruction was executed.

Quoted 6 lines

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

I am using GDB now and everything working well.

Quoted 35 lines
Quoted 35 lines
(quoted diff) +0 -0
@@ -103,7 +104,39 @@ static void do_unexpected_trap(const struct
 cpu_user_regs *regs)


 void do_trap(struct cpu_user_regs *cpu_regs)
 {
-    do_unexpected_trap(cpu_regs);
+    register_t pc = cpu_regs->sepc;
+    unsigned long cause = csr_read(CSR_SCAUSE);
+
+    switch ( cause )
+    {
+    /*
+     * ebreak is used as BUG_insn so when macros from <xen/bug.h> are
+     * used, an exception with BREAKPOINT cause will be generated. +     *
+     * ebreak triggers a debug trap, which starts in debug mode and is
+     * then filtered by every mode. A debugger will first handle the
+     * debug trap and check if ebreak was set by it or not. If not,
+     * CAUSE_BREAKPOINT will be generated for the mode where the ebreak
+     * instruction was executed.
+     */
+    case CAUSE_BREAKPOINT:
+        if ( do_bug_frame(cpu_regs, pc) >= 0 )
+        {
+            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) ) +            {
+                printk("Something wrong with PC: %#lx\n", pc); +                die();
+            }

Quoted 8 lines

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

Quoted 5 lines
xen/arch/riscv/traps.c +5 -0
+
+            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
+        }
+
+        break;

Wouldn't you better fall through here, with the "break" moved into the if()'s body?

It makes sense to me. Thanks.

~ Oleksii

#3 lore raw

Re: [PATCH v11 2/5] xen/riscv: introduce decode_cause() stuff

In reply to Oleksii Kurochko
Quoted 80 lines · Oleksii Kurochko

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

---
Changes in V11:
- Nothing changed. Only rebase.
---
Changes in V10:
- add Acked-by: Jan Beulich <[email protected]>
---
Changes in V9:
- This patch was reverted as breaks both release and randconfig builds.
I don't see the failures now. ( probably it was because of printk usage
which was not ready at that moment ).
- drop inclusion of <asm/csr.h> and <asm/early_printk.h>
- add <asm/riscv_encoding.h> for CAUSE_* in decode_trap_cause().
---
Changes in V8:
- fix typo in return string from decode_reserved_interrupt_cause
- add Acked-by: Alistair Francis <[email protected]>
---
Changes in V7:
- Nothing changed. Only rebase.
---
Changes in V6:
- Remove usage of LINK_TO_LOAD() due to the MMU being enabled first.
- Change early_printk() to printk()
---
Changes in V5:
- 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
- use LINK_TO_LOAD() for addresses which can be linker time relative.
---
Changes in V4:
- fix string in decode_reserved_interrupt_cause()
---
Changes in V3:
- Nothing changed
---
Changes in V2:
- Make decode_trap_cause() more optimization friendly.
- Merge the pathc which introduces do_unexpected_trap() to the current one.

---
 xen/arch/riscv/traps.c | 80 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)
xen/arch/riscv/traps.c +18 -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",

xen/arch/riscv/traps.c line 41

Quoted 10 lines
xen/arch/riscv/traps.c +10 -0
+        [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";
+}

While I committed this as-is, two more points: First, as soon any any such array access becomes potentially reachable because of guest activity, it wants to use array_access_nospec() or alike. Second, and somewhat related, it may be desirable to avoid such double accesses. Hopefully the compiler will do that for you. But

    const char *res = cause < ARRAY_SIZE(trap_causes) ? trap_causes[cause]
                                                      : NULL;

    return res ?: "UNKNOWN";

might be worthwhile anyway.

Jan

#4 lore raw

Re: [PATCH v11 2/5] xen/riscv: introduce decode_cause() stuff

Quoted 100 lines · Jan Beulich
Quoted 99 lines · Oleksii Kurochko

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

---
Changes in V11:
 - Nothing changed. Only rebase.
---
Changes in V10:
 - add Acked-by: Jan Beulich <[email protected]>
---
Changes in V9:
 - This patch was reverted as breaks both release and randconfig builds.
   I don't see the failures now. ( probably it was because of printk usage
   which was not ready at that moment ).
 - drop inclusion of <asm/csr.h> and <asm/early_printk.h>
 - add <asm/riscv_encoding.h> for CAUSE_* in decode_trap_cause().
---
Changes in V8:
  - fix typo in return string from decode_reserved_interrupt_cause
  - add Acked-by: Alistair Francis <[email protected]>
---
Changes in V7:
 - Nothing changed. Only rebase.
---
Changes in V6:
 - Remove usage of LINK_TO_LOAD() due to the MMU being enabled first.
 - Change early_printk() to printk()
---
Changes in V5:
  - 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
  - use LINK_TO_LOAD() for addresses which can be linker time relative.
---
Changes in V4:
  - fix string in decode_reserved_interrupt_cause()
---
Changes in V3:
  - Nothing changed
---
Changes in V2:
  - Make decode_trap_cause() more optimization friendly.
  - Merge the pathc which introduces do_unexpected_trap() to the current one.
---
 xen/arch/riscv/traps.c | 80
+++++++++++++++++++++++++++++++++++++++++-

 1 file changed, 79 insertions(+), 1 deletion(-)
xen/arch/riscv/traps.c +0 -0
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>

xen/arch/riscv/traps.c +4 -1
-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",
+    };
+

Quoted 8 lines
Quoted 4 lines

+    if ( cause < ARRAY_SIZE(trap_causes) && trap_causes[cause] ) +        return trap_causes[cause];
+    return "UNKNOWN";
+}

While I committed this as-is, two more points: First, as soon any any such array access becomes potentially reachable because of guest activity, it wants to use array_access_nospec() or alike.

Could you please explain why ( specifically ) guest activity affects if array access becomes potentially reachable?

Quoted 11 lines

Second, and
somewhat related, it may be desirable to avoid such double accesses. Hopefully the compiler will do that for you. But

    const char *res = cause < ARRAY_SIZE(trap_causes) ? trap_causes[cause]
                                                      : NULL;

    return res ?: "UNKNOWN";

might be worthwhile anyway.

Thanks.
I will apply this suggestion in the next version of this patch series.

~ Oleksii

#5 lore raw

Re: [PATCH v11 2/5] xen/riscv: introduce decode_cause() stuff

Quoted 109 lines · On 01.08.2024 22:05, [email protected] wrote:
Quoted 100 lines · Jan Beulich
Quoted 99 lines · Oleksii Kurochko

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

---
Changes in V11:
 - Nothing changed. Only rebase.
---
Changes in V10:
 - add Acked-by: Jan Beulich <[email protected]>
---
Changes in V9:
 - This patch was reverted as breaks both release and randconfig builds.
   I don't see the failures now. ( probably it was because of printk usage
   which was not ready at that moment ).
 - drop inclusion of <asm/csr.h> and <asm/early_printk.h>
 - add <asm/riscv_encoding.h> for CAUSE_* in decode_trap_cause().
---
Changes in V8:
  - fix typo in return string from decode_reserved_interrupt_cause
  - add Acked-by: Alistair Francis <[email protected]>
---
Changes in V7:
 - Nothing changed. Only rebase.
---
Changes in V6:
 - Remove usage of LINK_TO_LOAD() due to the MMU being enabled first.
 - Change early_printk() to printk()
---
Changes in V5:
  - 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
  - use LINK_TO_LOAD() for addresses which can be linker time relative.
---
Changes in V4:
  - fix string in decode_reserved_interrupt_cause()
---
Changes in V3:
  - Nothing changed
---
Changes in V2:
  - Make decode_trap_cause() more optimization friendly.
  - Merge the pathc which introduces do_unexpected_trap() to the current one.
---
 xen/arch/riscv/traps.c | 80
+++++++++++++++++++++++++++++++++++++++++-

 1 file changed, 79 insertions(+), 1 deletion(-)
xen/arch/riscv/traps.c +0 -0
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>

xen/arch/riscv/traps.c +4 -1
-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",
+    };
+

Quoted 8 lines
Quoted 4 lines

+    if ( cause < ARRAY_SIZE(trap_causes) && trap_causes[cause] ) +        return trap_causes[cause];
+    return "UNKNOWN";
+}

While I committed this as-is, two more points: First, as soon any any such array access becomes potentially reachable because of guest activity, it wants to use array_access_nospec() or alike.

Quoted 2 lines

Could you please explain why ( specifically ) guest activity affects if array access becomes potentially reachable?

Anything the guest can affect wants to be as immune as possible towards the guest trying to drive speculation the wrong way, and thus be able to infer information from resulting internal state of the CPU. I'm sure you're aware of at least the fact that there was a multitude of such issues over the last several years.

Jan

#10 lore raw

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

In reply to Jan Beulich
Quoted 12 lines · Jan Beulich
Quoted 11 lines

To have working BUG(), WARN(), ASSERT, run_in_exception_handler() it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak instruction was executed.

Quoted 6 lines

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

GDB keeps track of what addresses it has breakpoints at.

And if QEMU is being used, GDB isn't actually inserting ebreak instructions because QEMU has an infinite amount of "hardware" breakpoints.

If it answers your original question I could add this to commit message/code in the next patch version.

~ Oleksii

#11 lore raw

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

Quoted 13 lines · On 02.08.2024 11:14, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 11 lines

To have working BUG(), WARN(), ASSERT, run_in_exception_handler() it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak instruction was executed.

Quoted 7 lines
Quoted 6 lines

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

GDB keeps track of what addresses it has breakpoints at.

Of course it does. But in Xen how do you decide whether to trigger the debugger when you've hit an ebreak? (Just to repeat, my question is about the purely software debugging case; no hardware debugging extensions. In such a case, aiui, Xen gains control first and then decides whether to trigger the debugger, or whether to handle the exception internally. Sure, none of this infrastructure is in place right now, but imo it wants taking into consideration.)

Quoted 6 lines

And if QEMU is being used, GDB isn't actually inserting ebreak instructions because QEMU has an infinite amount of "hardware" breakpoints.

If it answers your original question I could add this to commit message/code in the next patch version.

I'm afraid it still doesn't.

Jan

#12 lore raw

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

Quoted 24 lines · Jan Beulich
Quoted 15 lines · On 02.08.2024 11:14, [email protected] wrote:
Quoted 14 lines · Jan Beulich
Quoted 13 lines

To have working BUG(), WARN(), ASSERT,
run_in_exception_handler()
it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak
instruction was executed.

Quoted 7 lines
Quoted 6 lines

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

GDB keeps track of what addresses it has breakpoints at.

Quoted 7 lines

Of course it does. But in Xen how do you decide whether to trigger the debugger when you've hit an ebreak? (Just to repeat, my question is about the purely software debugging case; no hardware debugging extensions. In such a case, aiui, Xen gains control first and then decides whether to trigger the debugger, or whether to handle the exception internally. Sure, none of this infrastructure is in place right now, but imo it wants taking into consideration.)

Well, then something like KGDB is needed for Xen and mechanism to notify guests to something similar to:

Right now Xen detects that 'ebreak' is inserted by using the function do_bug_frame():

```
    case CAUSE_BREAKPOINT:
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) )
            {
                printk("Something wrong with PC: %#lx\n", pc);
                die();
            }

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

So if do_bug_frame() return < 0 then it should be ebreak inserted by the debugger so need to notify GDB that he should handle that. At the moment I think we can add():

```
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
		...

            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
        }
	else
	{
	    printk("this is not Xen's ebreak\n");
            die();
        }
```

Probably, to have the message is enough ( "this is not Xen's ebreak\n" ):

```
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
		...
        }
	else
	    printk("this is not Xen's ebreak\n");

        cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
```
Quoted 9 lines
Quoted 7 lines

And if QEMU is being used, GDB isn't actually inserting ebreak instructions because QEMU has an infinite amount of "hardware" breakpoints.

If it answers your original question I could add this to commit message/code in the next patch version.

I'm afraid it still doesn't.

So if we are speaking about the case QEMU + GDB then no ebreak instruction will be inserted. So what happens is:
1. QEMU notices PC hits breakpoint ( "hardware" breakpoints are used + GDB keeps track of what addresses it has breakpoints at )
2. QEMU tells GDB that the program received SIGTRAP
3. GDB does debugger tasks.

In case of real hardware it will be JTAG and OpenOCD to interface with it and GDB will use OpenOCD for debugging purpose which follows RISC-V debug spec and everything will work as I described in the commit message/code.

Or it could be still without external h/w debugger ( using ebreak instruction ) but then hypervisor should have an infrastrucutre to notify GDB that a breakpoint is hited ( something like KGDB in Linux kernel ) and for this case we will print the message mentioned above (
+ stop Xen if it is necessary )

~ Oleksii

#13 lore raw

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

Quoted 54 lines · On 02.08.2024 12:22, [email protected] wrote:
Quoted 24 lines · Jan Beulich
Quoted 15 lines · On 02.08.2024 11:14, [email protected] wrote:
Quoted 14 lines · Jan Beulich
Quoted 13 lines

To have working BUG(), WARN(), ASSERT,
run_in_exception_handler()
it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are used, an exception with BREAKPOINT cause will be generated.

ebreak triggers a debug trap, which starts in debug mode and is then filtered by every mode. A debugger will first handle the debug trap and check if ebreak was set by it or not. If not, CAUSE_BREAKPOINT will be generated for the mode where the ebreak
instruction was executed.

Quoted 7 lines
Quoted 6 lines

Here and in the similar code comment you talk about an external debugger, requiring debug mode, which is an optional feature. In my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing ebreak uses for breakpoints from ebreak uses for e.g. BUG() and WARN() is going to be feasible.

GDB keeps track of what addresses it has breakpoints at.

Quoted 7 lines

Of course it does. But in Xen how do you decide whether to trigger the debugger when you've hit an ebreak? (Just to repeat, my question is about the purely software debugging case; no hardware debugging extensions. In such a case, aiui, Xen gains control first and then decides whether to trigger the debugger, or whether to handle the exception internally. Sure, none of this infrastructure is in place right now, but imo it wants taking into consideration.)

Well, then something like KGDB is needed for Xen and mechanism to notify guests to something similar to:

Right now Xen detects that 'ebreak' is inserted by using the function do_bug_frame():

```
    case CAUSE_BREAKPOINT:
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) )
            {
                printk("Something wrong with PC: %#lx\n", pc);
                die();
            }

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

So if do_bug_frame() return < 0 then it should be ebreak inserted by the debugger so need to notify GDB that he should handle that. At the moment I think we can add():

Quoted 13 lines
```
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
		...

            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
        }
	else
	{
	    printk("this is not Xen's ebreak\n");
            die();
        }
```

Except that, as previously said, this will break if the debugger inserted a breakpoint where Xen already has an ebreak. That's where we started from in this discussion. Recall that my question was how the use of ebreak can be correct / reliable here. It is for a reason that on x86 we do not use the shorter INT3 instruction, but the longer UD2 one for BUG() etc.

Jan

#14 lore raw

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

Quoted 84 lines · Jan Beulich
Quoted 69 lines · On 02.08.2024 12:22, [email protected] wrote:
Quoted 32 lines · Jan Beulich
Quoted 29 lines · On 02.08.2024 11:14, [email protected] wrote:
Quoted 18 lines · Jan Beulich
Quoted 8 lines

To have working BUG(), WARN(), ASSERT,
run_in_exception_handler()
it is needed to enable GENERIC_BUG_FRAME.

ebreak is used as BUG_insn so when macros from <xen/bug.h> are
used, an exception with BREAKPOINT cause will be generated.

Quoted 9 lines

ebreak triggers a debug trap, which starts in debug mode and is
then filtered by every mode. A debugger will first handle the
debug trap and check if ebreak was set by it or not. If not,
CAUSE_BREAKPOINT will be generated for the mode where the ebreak
instruction was executed.

Quoted 10 lines

Here and in the similar code comment you talk about an external
debugger, requiring debug mode, which is an optional feature. In
my earlier comments what I was referring to was pure software debugging. I continue to fail to see how properly distinguishing
ebreak uses for breakpoints from ebreak uses for e.g. BUG() and
WARN() is going to be feasible.

Quoted 1 line

GDB keeps track of what addresses it has breakpoints at.

Quoted 12 lines

Of course it does. But in Xen how do you decide whether to trigger
the debugger when you've hit an ebreak? (Just to repeat, my question
is about the purely software debugging case; no hardware debugging
extensions. In such a case, aiui, Xen gains control first and then
decides whether to trigger the debugger, or whether to handle the exception internally. Sure, none of this infrastructure is in place
right now, but imo it wants taking into consideration.)

Well, then something like KGDB is needed for Xen and mechanism to notify guests to something similar to:

Right now Xen detects that 'ebreak' is inserted by using the function
do_bug_frame():

```
    case CAUSE_BREAKPOINT:
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
            if ( !(is_kernel_text(pc) || is_kernel_inittext(pc)) )
            {
                printk("Something wrong with PC: %#lx\n", pc);
                die();
            }

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

So if do_bug_frame() return < 0 then it should be ebreak inserted by
the debugger so need to notify GDB that he should handle that. At the moment I think we can add():

Quoted 13 lines
```
        if ( do_bug_frame(cpu_regs, pc) >= 0 )
        {
		...

            cpu_regs->sepc += GET_INSN_LENGTH(*(uint16_t *)pc);
        }
	else
	{
	    printk("this is not Xen's ebreak\n");
            die();
        }
```
Quoted 9 lines

Except that, as previously said, this will break if the debugger inserted
a breakpoint where Xen already has an ebreak. That's where we started from
in this discussion. Recall that my question was how the use of ebreak can
be correct / reliable here. It is for a reason that on x86 we do not use
the shorter INT3 instruction, but the longer UD2 one for BUG() etc.

Okay, in this case it will be an issue but only in case if the debugger really will insert ebreak instruction but not something else. I wasn't able to find a case when it is really inserted ebreak in case of RISC- V.

However I agree that it would be safer to use something like UD2 but RISC-V doesn't have such instruction ( at least I don't see such in a spec).

There is something what we need is mentioned here:
https://github.com/riscv-non-isa/riscv-asm-manual/blob/main/riscv-asm.md#instruction-aliases
But the problem is that is "de-facto" standard, but not really standard instruction. Anyway, I will update the patch to use (C0001073) instead of ebreak.

~ Oleksii

#15 lore raw

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

Quoted 5 lines · On 02.08.2024 13:02, [email protected] wrote:

There is something what we need is mentioned here:
https://github.com/riscv-non-isa/riscv-asm-manual/blob/main/riscv-asm.md#instruction-aliases
But the problem is that is "de-facto" standard, but not really standard instruction. Anyway, I will update the patch to use (C0001073) instead of ebreak.

Well, the official spec, in the C extension, mentions 0x0000 as "defined illegal instruction". Wouldn't using that work? It's going to be undefined no matter whether the C extension is actually implemented.

Jan

#16 lore raw

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

Quoted 9 lines · Jan Beulich
Quoted 7 lines · On 02.08.2024 13:02, [email protected] wrote:

There is something what we need is mentioned here:
https://github.com/riscv-non-isa/riscv-asm-manual/blob/main/riscv-asm.md#instruction-aliases
But the problem is that is "de-facto" standard, but not really standard
instruction. Anyway, I will update the patch to use (C0001073) instead
of ebreak.

Quoted 5 lines

Well, the official spec, in the C extension, mentions 0x0000 as "defined
illegal instruction". Wouldn't using that work? It's going to be undefined
no matter whether the C extension is actually implemented.

It should work. I will use 0x0000 then. Thanks.

~ Oleksii