Skip to contentSkip to conversation

RISC-V: Introduce vSBI framework

PATCH Superseded Oleksii Kurochko 2 participants 15 messages Dec 17, 2025 – Dec 22, 2025 RISC-V

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#5 lore raw

PATCH v2 2/3 xen/riscv: add RISC-V legacy SBI extension support for guests

In reply to Oleksii Kurochko

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

The implementation uses the existing virtual SBI framework to handle legacy SBI ecalls, ensuring compatibility with older SBI specifications in RISC-V guests.

---
Changes in v2:
- s/vsbi-legacy-extension.*/legacy-extension.*
- Correct padding for SBI_EXT_0_1_* macros.
- Set ret = SBI_ERR_NOT_SUPPORTED; in the case of vsbi_legacy_ecall_handler() instead of regs->a0 = ... as regs->a0 will be overwritten in vsbi_handle_ecall().
- Use domain_crash() instead of panic() in vsbi_legacy_ecall_handler() and add TODO.
- Use newly introduced VSBI_EXT macros instead of VSBI_EXT_{START,END}.
- Introduce vsbi_print_line() and use it instead of plain printk() in the handler of SBI_EXT_0_1_CONSOLE_PUTCHAR.

---
 xen/arch/riscv/include/asm/sbi.h       | 11 ++++-
 xen/arch/riscv/vsbi/Makefile           |  1 +
 xen/arch/riscv/vsbi/legacy-extension.c | 65 ++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/riscv/vsbi/legacy-extension.c
xen/arch/riscv/include/asm/sbi.h +9 -2
index ade24a572d..751bae6d66 100644
@@ -14,8 +14,15 @@
 
 #include <xen/cpumask.h>
 
-#define SBI_EXT_0_1_CONSOLE_PUTCHAR		0x1
-#define SBI_EXT_0_1_SHUTDOWN			0x8
+#define SBI_EXT_0_1_SET_TIMER               0x0
+#define SBI_EXT_0_1_CONSOLE_PUTCHAR         0x1
+#define SBI_EXT_0_1_CONSOLE_GETCHAR         0x2
+#define SBI_EXT_0_1_CLEAR_IPI               0x3
+#define SBI_EXT_0_1_SEND_IPI                0x4
+#define SBI_EXT_0_1_REMOTE_FENCE_I          0x5
+#define SBI_EXT_0_1_REMOTE_SFENCE_VMA       0x6
+#define SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID  0x7
+#define SBI_EXT_0_1_SHUTDOWN                0x8
 
 #define SBI_EXT_BASE                    0x10
 #define SBI_EXT_RFENCE                  0x52464E43
xen/arch/riscv/vsbi/Makefile +1 -0
index 820eb10ac2..bc5755cb13 100644
@@ -1 +1,2 @@
 obj-y += core.o
+obj-y += legacy-extension.o
xen/arch/riscv/vsbi/legacy-extension.c new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.52.0
#0 lore raw

v2 cover RISC-V: Introduce vSBI framework

Introduce the vSBI framework to handle extension ecall instruction calls from a guest.

In this patch series, support is added for only a few extensions and FIDs, just enough to boot the first guest domains and obtain logs from them. This keeps the patch series independent from other ongoing work.

It was decided to start with support for the Legacy Extension, as it is still supported by Linux (so we may need it anyway), and because some of its FIDs require less functionality to implement at this stage compared to the more modern extensions.

---
Chabges in v2:
- Address a comments from v1.
---

Oleksii Kurochko (3):
xen/riscv: introduce vSBI extension framework
xen/riscv: add RISC-V legacy SBI extension support for guests xen/riscv: add RISC-V virtual SBI base extension support for guests

 xen/arch/riscv/Makefile                |  1 +
 xen/arch/riscv/entry.S                 |  6 +++
 xen/arch/riscv/include/asm/processor.h |  1 +
 xen/arch/riscv/include/asm/sbi.h       | 18 ++++++-
 xen/arch/riscv/include/asm/vsbi.h      | 32 ++++++++++++
 xen/arch/riscv/riscv64/asm-offsets.c   |  1 +
 xen/arch/riscv/setup.c                 |  3 ++
 xen/arch/riscv/traps.c                 |  8 +++
 xen/arch/riscv/vsbi/Makefile           |  3 ++
 xen/arch/riscv/vsbi/base-extension.c   | 71 ++++++++++++++++++++++++++
 xen/arch/riscv/vsbi/core.c             | 56 ++++++++++++++++++++
 xen/arch/riscv/vsbi/legacy-extension.c | 65 +++++++++++++++++++++++
 xen/arch/riscv/xen.lds.S               |  7 +++
 13 files changed, 270 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/riscv/include/asm/vsbi.h
 create mode 100644 xen/arch/riscv/vsbi/Makefile
 create mode 100644 xen/arch/riscv/vsbi/base-extension.c
 create mode 100644 xen/arch/riscv/vsbi/core.c
 create mode 100644 xen/arch/riscv/vsbi/legacy-extension.c
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/3 xen/riscv: introduce vSBI extension framework

This commit introduces support for handling virtual SBI extensions in Xen.

The changes include:
- Added new vsbi/core.c and vsbi.h files to implement virtual SBI extension handling.
- Modified traps.c to handle CAUSE_VIRTUAL_SUPERVISOR_ECALL by calling vsbi_handle_ecall() when the trap originates from VS-mode.
- Updated xen.lds.S to include a new .vsbi.exts section for virtual SBI extension data.
- Updated Makefile to include the new vsbi/ directory in the build.
- Add hstatus register to struct cpu_user_regs as it is needed for a check that CAUSE_VIRTUAL_SUPERVISOR_ECALL happens from VS-mode. Also, add storing/restoring of hstatus register in handle_trap().
- Introduce vsbi_find_extension() to check if vsbi extension is supported by Xen. For now it is called only inside vsbi/core.c, but in future it is going to be called from other files.
- Introduce check_vsbi_ext_ranges() to check if there EIDs ranges overlapping between extensions.

The implementation allows for registration and handling of SBI extensions via a new vsbi_ext structure and ".vsbi.exts" section, enabling extensible virtual SBI support for RISC-V guests.

Note: All EIDs are printed in the format #%#lx and all FIDs in #%#lu, as the SBI spec uses these formats. Printing them this way makes it easier to search for them in the SBI spec.

---
Changes in v2:
- s/struct regs/struct cpu_user_regs.
- s/handle/handler.
- Drop extid_ prefix inside VSBI_EXT_START().
- use BUG_ON() instead of panic to be sure that CAUSE_VIRTUAL_SUPERVISOR_ECALL comes from VS-mode.
- s/ext_id/eid for vsbi_find_extension().
- Add the comment above VSBI_EXT_START about [start,end] range.
- Drop check "&& ext->handler" in vsbi_handle_ecall() as it isn't be so that handler() isn't provided.
- s/vsbi.c/core.c
- s/vsbi_ext/ext for local variable inside vsbi_find_extension().
- Update the commit message: add a note about FID and EID printing formats, add some information about vsbo_find_extension() function, and add info about check_vsbi_ext_ranges().
- Introduce check_vsbi_ext_ranges() to be sure that there is no overlapping in EIDs range(s).
- Add storing/restoring of hstatus register in handle_trap()[entry.S].

---
 xen/arch/riscv/Makefile                |  1 +
 xen/arch/riscv/entry.S                 |  6 +++
 xen/arch/riscv/include/asm/processor.h |  1 +
 xen/arch/riscv/include/asm/vsbi.h      | 32 +++++++++++++++
 xen/arch/riscv/riscv64/asm-offsets.c   |  1 +
 xen/arch/riscv/setup.c                 |  3 ++
 xen/arch/riscv/traps.c                 |  8 ++++
 xen/arch/riscv/vsbi/Makefile           |  1 +
 xen/arch/riscv/vsbi/core.c             | 56 ++++++++++++++++++++++++++
 xen/arch/riscv/xen.lds.S               |  7 ++++
 10 files changed, 116 insertions(+)
 create mode 100644 xen/arch/riscv/include/asm/vsbi.h
 create mode 100644 xen/arch/riscv/vsbi/Makefile
 create mode 100644 xen/arch/riscv/vsbi/core.c
xen/arch/riscv/Makefile +1 -0
index 9dde693db4..87c1148b00 100644
@@ -20,6 +20,7 @@ obj-y += time.o
 obj-y += traps.o
 obj-y += vmid.o
 obj-y += vm_event.o
+obj-y += vsbi/
 
 $(TARGET): $(TARGET)-syms
 	$(OBJCOPY) -O binary -S $< $@
xen/arch/riscv/entry.S +6 -0
index 4db818ba8d..202a35fb03 100644
@@ -48,11 +48,17 @@ save_to_stack:
         csrr    t0, CSR_SSTATUS
         REG_S   t0, CPU_USER_REGS_SSTATUS(sp)
 
+        csrr    t0, CSR_HSTATUS
+        REG_S   t0, CPU_USER_REGS_HSTATUS(sp)
+
         mv      a0, sp
         call    do_trap
 
 restore_registers:
         /* Restore stack_cpu_regs */
+        REG_L	t0, CPU_USER_REGS_HSTATUS(sp)
+        csrw	CSR_HSTATUS, t0
+
         REG_L   t0, CPU_USER_REGS_SEPC(sp)
         csrw    CSR_SEPC, t0
         REG_L   t0, CPU_USER_REGS_SSTATUS(sp)
xen/arch/riscv/include/asm/processor.h +1 -0
index 2502045642..6b89df4a2d 100644
@@ -49,6 +49,7 @@ struct cpu_user_regs
     unsigned long t6;
     unsigned long sepc;
     unsigned long sstatus;
+    unsigned long hstatus;
     /* pointer to previous stack_cpu_regs */
     unsigned long pregs;
 };
xen/arch/riscv/include/asm/vsbi.h new file +32 -0
new file mode 100644
index 0000000000..47a38dce02
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier:  GPL-2.0-only */
+
+#ifndef ASM_RISCV_VSBI_H
+#define ASM_RISCV_VSBI_H
+
+struct cpu_user_regs;
+struct vcpu;
+
+struct vsbi_ext {
+    const char *name;
+    unsigned long eid_start;
+    unsigned long eid_end;
+    int (*handler)(struct vcpu *vcpu, unsigned long eid,
+                   unsigned long fid, struct cpu_user_regs *regs);
+};
+
+/* Ranges (start and end) are inclusive within an extension */
+#define VSBI_EXT(ext, start, end, handle)           \
+static const struct vsbi_ext vsbi_ext_##ext __used  \
+__section(".vsbi.exts") = {                         \
+    .name = #ext,                                   \
+    .eid_start = start,                             \
+    .eid_end = end,                                 \
+    .handler = handle,                              \
+};
+
+void vsbi_handle_ecall(struct vcpu *vcpu, struct cpu_user_regs *regs);
+const struct vsbi_ext *vsbi_find_extension(unsigned long eid);
+
+void check_vsbi_ext_ranges(void);
+
+#endif
xen/arch/riscv/riscv64/asm-offsets.c +1 -0
index 3b5daf3b36..472cced4f8 100644
@@ -49,6 +49,7 @@ void asm_offsets(void)
     OFFSET(CPU_USER_REGS_T6, struct cpu_user_regs, t6);
     OFFSET(CPU_USER_REGS_SEPC, struct cpu_user_regs, sepc);
     OFFSET(CPU_USER_REGS_SSTATUS, struct cpu_user_regs, sstatus);
+    OFFSET(CPU_USER_REGS_HSTATUS, struct cpu_user_regs, hstatus);
     OFFSET(CPU_USER_REGS_PREGS, struct cpu_user_regs, pregs);
     BLANK();
     DEFINE(PCPU_INFO_SIZE, sizeof(struct pcpu_info));
xen/arch/riscv/setup.c +3 -0
index 8f46f1a1de..9b4835960d 100644
@@ -26,6 +26,7 @@
 #include <asm/sbi.h>
 #include <asm/setup.h>
 #include <asm/traps.h>
+#include <asm/vsbi.h>
 
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
@@ -110,6 +111,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     end_boot_allocator();
 
+    check_vsbi_ext_ranges();
+
     /*
      * The memory subsystem has been initialized, we can now switch from
      * early_boot -> boot.
xen/arch/riscv/traps.c +8 -0
index f061004d83..5c3d1988d7 100644
@@ -15,6 +15,7 @@
 #include <asm/processor.h>
 #include <asm/riscv_encoding.h>
 #include <asm/traps.h>
+#include <asm/vsbi.h>
 
 /*
  * Initialize the trap handling.
@@ -114,6 +115,13 @@ void do_trap(struct cpu_user_regs *cpu_regs)
 
     switch ( cause )
     {
+    case CAUSE_VIRTUAL_SUPERVISOR_ECALL:
+        /* CAUSE_VIRTUAL_SUPERVISOR_ECALL should come from VS-mode */
+        BUG_ON(!(cpu_regs->hstatus & HSTATUS_SPV));
+
+        vsbi_handle_ecall(current, cpu_regs);
+        break;
+
     case CAUSE_ILLEGAL_INSTRUCTION:
         if ( do_bug_frame(cpu_regs, pc) >= 0 )
         {
xen/arch/riscv/vsbi/Makefile new file +1 -0
new file mode 100644
index 0000000000..820eb10ac2
@@ -0,0 +1 @@
+obj-y += core.o
xen/arch/riscv/vsbi/core.c new file +56 -0

56 lines not on this page · raw message

xen/arch/riscv/xen.lds.S +7 -0
index 45d2e053d0..331a7d63d3 100644
@@ -61,6 +61,13 @@ SECTIONS
         __note_gnu_build_id_end = .;
     } :note :text
     #endif
+
+    . = ALIGN(POINTER_ALIGN);
+    DECL_SECTION(.vsbi.exts) {
+        _svsbi_exts = .;
+        *(.vsbi.exts)
+        _evsbi_exts = .;
+    } :text
     _erodata = .;                /* End of read-only data */
 
     . = ALIGN(PAGE_SIZE);
Signature · 1 line
2.52.0
#11 lore raw

PATCH v2 3/3 xen/riscv: add RISC-V virtual SBI base extension support for guests

In reply to Oleksii Kurochko

Add support of virtual SBI base extension calls for RISC-V guests, delegating hardware-specific queries to the underlying SBI and handling version and firmware ID queries directly.

The changes include:
1. Define new SBI base extension function IDs (SBI_EXT_BASE_GET_MVENDORID, SBI_EXT_BASE_GET_MARCHID, SBI_EXT_BASE_GET_MIMPID).
2. Introduce XEN_SBI_VER_MAJOR, XEN_SBI_VER_MINOR for imeplenataion of SBI_EXT_BASE_GET_SPEC_VERSION.
4. Introduce VCPU_SBI_IMPID to implement SBI_EXT_BASE_GET_IMP_ID.
5. Implement handling of SBI base extension functions, including version, firmware ID, and machine-specific queries.

---
Changes in v2:
- s/vsbi-base-extension.*/base-extension.*
- Introduce VCPU_SBI_IMPID, XEN_SBI_VER_MINOR and XEN_SBI_VER_MAJOR.
- Return VCPU_SBI_IMPID in the case of SBI_EXT_BASE_GET_IMP_ID.
- Return Xen version in the case of SBI_EXT_BASE_GET_IMP_VERSION.
- Use domain_crash() instead of panic() for default case.
- For SBI_EXT_BASE_GET_{MVENDORID,MARCHID,MIMPID} abd SBI_EXT_BASE_PROBE_EXT add handling of a domain is h/w or not.

---
 xen/arch/riscv/include/asm/sbi.h     |  7 +++
 xen/arch/riscv/vsbi/Makefile         |  1 +
 xen/arch/riscv/vsbi/base-extension.c | 71 ++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 xen/arch/riscv/vsbi/base-extension.c
xen/arch/riscv/include/asm/sbi.h +7 -0
index 751bae6d66..eb710950cc 100644
@@ -14,6 +14,10 @@
 
 #include <xen/cpumask.h>
 
+#define XEN_SBI_VER_MAJOR 0
+#define XEN_SBI_VER_MINOR 2
+#define XEN_SBI_IMPID 7
+
 #define SBI_EXT_0_1_SET_TIMER               0x0
 #define SBI_EXT_0_1_CONSOLE_PUTCHAR         0x1
 #define SBI_EXT_0_1_CONSOLE_GETCHAR         0x2
@@ -32,6 +36,9 @@
 #define SBI_EXT_BASE_GET_IMP_ID         0x1
 #define SBI_EXT_BASE_GET_IMP_VERSION    0x2
 #define SBI_EXT_BASE_PROBE_EXT          0x3
+#define SBI_EXT_BASE_GET_MVENDORID      0x4
+#define SBI_EXT_BASE_GET_MARCHID        0x5
+#define SBI_EXT_BASE_GET_MIMPID         0x6
 
 /* SBI function IDs for RFENCE extension */
 #define SBI_EXT_RFENCE_REMOTE_FENCE_I           0x0
xen/arch/riscv/vsbi/Makefile +1 -0
index bc5755cb13..8ce470f787 100644
@@ -1,2 +1,3 @@
+obj-y += base-extension.o
 obj-y += core.o
 obj-y += legacy-extension.o
xen/arch/riscv/vsbi/base-extension.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v2 1/3] xen/riscv: introduce vSBI extension framework

In reply to Oleksii Kurochko
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/vsbi/core.c new file +4 -0
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/sched.h>
+

xen/arch/riscv/vsbi/core.c line 14

Quoted 10 lines
xen/arch/riscv/vsbi/core.c new file +10 -0
+#include <asm/processor.h>
+#include <asm/sbi.h>
+#include <asm/vsbi.h>
+
+extern const struct vsbi_ext _svsbi_exts[], _evsbi_exts[];
+
+void __init check_vsbi_ext_ranges(void)
+{
+    for ( const struct vsbi_ext *a = _svsbi_exts; a != _evsbi_exts; a++ )
+        for ( const struct vsbi_ext *b = a + 1; b != _evsbi_exts; b++ )

Like here, ...

xen/arch/riscv/vsbi/core.c line 26

Quoted 12 lines
xen/arch/riscv/vsbi/core.c new file +12 -0
+            if ( !(a->eid_end < b->eid_start || b->eid_end < a->eid_start) )
+                panic("EID range overlap detected: "
+                      "%s:[#%#lx..#%#lx] vs %s:[#%#lx..#%#lx]\n",
+                      a->name, a->eid_start, a->eid_end,
+                      b->name, b->eid_start, b->eid_end);
+}
+
+const struct vsbi_ext *vsbi_find_extension(unsigned long eid)
+{
+    const struct vsbi_ext *ext;
+
+    for ( ext = _svsbi_exts; ext != _evsbi_exts; ext++ )

... declare "ext" inside the for()?

Quoted 8 lines
xen/arch/riscv/vsbi/core.c new file +8 -0
+        if ( (eid >= ext->eid_start) && (eid <= ext->eid_end) )
+            return ext;
+
+    return NULL;
+}
+
+void vsbi_handle_ecall(struct vcpu *vcpu, struct cpu_user_regs *regs)
+{

xen/arch/riscv/vsbi/core.c line 44

Quoted 10 lines
xen/arch/riscv/vsbi/core.c new file +10 -0
+    const unsigned long eid = regs->a7;
+    const unsigned long fid = regs->a6;
+    const struct vsbi_ext *ext = vsbi_find_extension(eid);
+    int ret;
+
+    if ( ext )
+        ret = ext->handler(vcpu, eid, fid, regs);
+    else
+    {
+        printk("Unsupported Guest SBI EID #%#lx, FID #%lu\n", eid, regs->a1);

As before - anything guest triggered must not cause log spam issues. Minimally you want to use XENLOG_GUEST in such cases, but I think you really mean gprintk() here.

A connected question then arises: Why is "vcpu" being passed in, when the sole caller only ever passes "current"? (The connection here is that gprintk() also uses current, and hence would be wrong to use when vcpu != current.) Same question goes for the ->handler() hook.

Jan

#4 lore raw

Re: [PATCH v2 1/3] xen/riscv: introduce vSBI extension framework

In reply to Oleksii Kurochko
Quoted 1 line · Oleksii Kurochko

This commit introduces support for handling virtual SBI extensions in Xen.

Nit: As before, no "this commit" in commit messages, please.

Quoted 17 lines

The changes include:
- Added new vsbi/core.c and vsbi.h files to implement virtual SBI extension handling.
- Modified traps.c to handle CAUSE_VIRTUAL_SUPERVISOR_ECALL by calling vsbi_handle_ecall() when the trap originates from VS-mode.
- Updated xen.lds.S to include a new .vsbi.exts section for virtual SBI extension data.
- Updated Makefile to include the new vsbi/ directory in the build.
- Add hstatus register to struct cpu_user_regs as it is needed for a check that CAUSE_VIRTUAL_SUPERVISOR_ECALL happens from VS-mode. Also, add storing/restoring of hstatus register in handle_trap().
- Introduce vsbi_find_extension() to check if vsbi extension is supported by Xen. For now it is called only inside vsbi/core.c, but in future it is going to be called from other files.
- Introduce check_vsbi_ext_ranges() to check if there EIDs ranges overlapping between extensions.

Quoted 7 lines

The implementation allows for registration and handling of SBI extensions via a new vsbi_ext structure and ".vsbi.exts" section, enabling extensible virtual SBI support for RISC-V guests.

Note: All EIDs are printed in the format #%#lx and all FIDs in #%#lu, as the SBI spec uses these formats. Printing them this way makes it easier to search for them in the SBI spec.

Luckily the oddity exists only here: I don't think '#' would be very useful with 'u'.

Jan

#6 lore raw

Re: [PATCH v2 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests

In reply to Oleksii Kurochko
Quoted 8 lines · Oleksii Kurochko

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

I can't spot any actual support for GETCHAR.

Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +1 -0
@@ -0,0 +1,65 @@
+

xen/arch/riscv/vsbi/legacy-extension.c line 11

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/console.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+
+#include <asm/processor.h>
+#include <asm/vsbi.h>
+
+static void vsbi_print_line(char c)

Misleading function name? The parameter doesn't fit the name, and ...

xen/arch/riscv/vsbi/legacy-extension.c line 13

Quoted 2 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +2 -0
+{
+    struct domain *cd = current->domain;

I guess you copied this code from somewhere, but a variable of this type and contents wants to be named "currd".

xen/arch/riscv/vsbi/legacy-extension.c line 26

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+    struct domain_console *cons = cd->console;
+
+    if ( !is_console_printable(c) )
+        return;
+
+    spin_lock(&cons->lock);
+    ASSERT(cons->idx < ARRAY_SIZE(cons->buf));
+    if ( c != '\n' )
+        cons->buf[cons->idx++] = c;
+    if ( (cons->idx == (ARRAY_SIZE(cons->buf) - 1)) || (c == '\n') )
+    {
+        cons->buf[cons->idx] = '\0';
+        guest_printk(cd, XENLOG_G_DEBUG "%s\n", cons->buf);

... you also only print a line under certain conditions.

Quoted 5 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +5 -0
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+

xen/arch/riscv/vsbi/legacy-extension.c line 41

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+static int vsbi_legacy_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                     unsigned long fid,
+                                     struct cpu_user_regs *regs)
+{
+    int ret = 0;
+
+    switch ( eid )
+    {
+    case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+        vsbi_print_line((char)regs->a0);

The cast isn't really needed, is it? And just to double-check: The spec demands the upper bits to be ignored? (A link to the spec could have been useful, e.g. in the cover letter.)

xen/arch/riscv/vsbi/legacy-extension.c line 54

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+        break;
+
+    case SBI_EXT_0_1_CONSOLE_GETCHAR:
+        ret = SBI_ERR_NOT_SUPPORTED;
+        break;
+
+    default:
+        /*
+         * TODO: domain_crash() is acceptable here while things are still under
+         * development.
+         * It shouldn't stay like this in the end though: guests should not
+         * be punished like this for something Xen hasn't implemented.
+         */

Question then is why SBI_EXT_0_1_CONSOLE_GETCHAR gets a separate case block.

Jan

#12 lore raw

Re: [PATCH v2 3/3] xen/riscv: add RISC-V virtual SBI base extension support for guests

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/sbi.h line 19

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/sbi.h +3 -0
@@ -14,6 +14,10 @@
 
 #include <xen/cpumask.h>
 
+#define XEN_SBI_VER_MAJOR 0
+#define XEN_SBI_VER_MINOR 2
+#define XEN_SBI_IMPID 7

Are these numbers part of the spec (sorry, lack of a reference makes me wonder, plus if that were the case, I'd kind of expect the names to be SBI_XEN_..., not XEN_SBI_...)?

Quoted 10 lines
xen/arch/riscv/vsbi/base-extension.c new file +7 -0
@@ -0,0 +1,71 @@
+
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/version.h>
+

xen/arch/riscv/vsbi/base-extension.c line 17

Quoted 10 lines
xen/arch/riscv/vsbi/base-extension.c new file +10 -0
+#include <asm/processor.h>
+#include <asm/sbi.h>
+#include <asm/vsbi.h>
+
+static int vsbi_base_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                   unsigned long fid,
+                                   struct cpu_user_regs *regs)
+{
+    int ret = 0;
+    struct sbiret sbi_ret;
    ASSERT(eid == SBI_EXT_BASE);

xen/arch/riscv/vsbi/base-extension.c line 19

Quoted 1 line
xen/arch/riscv/vsbi/base-extension.c new file +1 -0
+    switch ( fid ) {

Nit: Brace placement.

xen/arch/riscv/vsbi/base-extension.c line 29

Quoted 10 lines
xen/arch/riscv/vsbi/base-extension.c new file +10 -0
+    case SBI_EXT_BASE_GET_SPEC_VERSION:
+        regs->a1 = MASK_INSR(XEN_SBI_VER_MAJOR, SBI_SPEC_VERSION_MAJOR_MASK) |
+                   XEN_SBI_VER_MINOR;
+        break;
+    case SBI_EXT_BASE_GET_IMP_ID:
+        regs->a1 = XEN_SBI_IMPID;
+        break;
+    case SBI_EXT_BASE_GET_IMP_VERSION:
+        regs->a1 = (xen_major_version() << 16) | xen_minor_version();
+        break;

Along those lines here - are we free to use an arbitrary layout (shifting major by 16 bits), or is this mandated by the spec? At least in the latter case, the 16 will want to gain a #define.

Also - blank lines please between non-fall-through case blocks.

Jan

#3 lore raw

Re: [PATCH v2 1/3] xen/riscv: introduce vSBI extension framework

In reply to Jan Beulich
Quoted 20 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/vsbi/core.c new file +4 -0
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/sched.h>
+

xen/arch/riscv/vsbi/core.c line 14

Quoted 10 lines
xen/arch/riscv/vsbi/core.c new file +10 -0
+#include <asm/processor.h>
+#include <asm/sbi.h>
+#include <asm/vsbi.h>
+
+extern const struct vsbi_ext _svsbi_exts[], _evsbi_exts[];
+
+void __init check_vsbi_ext_ranges(void)
+{
+    for ( const struct vsbi_ext *a = _svsbi_exts; a != _evsbi_exts; a++ )
+        for ( const struct vsbi_ext *b = a + 1; b != _evsbi_exts; b++ )

Like here, ...

Quoted 13 lines

xen/arch/riscv/vsbi/core.c line 26

Quoted 12 lines
xen/arch/riscv/vsbi/core.c new file +12 -0
+            if ( !(a->eid_end < b->eid_start || b->eid_end < a->eid_start) )
+                panic("EID range overlap detected: "
+                      "%s:[#%#lx..#%#lx] vs %s:[#%#lx..#%#lx]\n",
+                      a->name, a->eid_start, a->eid_end,
+                      b->name, b->eid_start, b->eid_end);
+}
+
+const struct vsbi_ext *vsbi_find_extension(unsigned long eid)
+{
+    const struct vsbi_ext *ext;
+
+    for ( ext = _svsbi_exts; ext != _evsbi_exts; ext++ )

... declare "ext" inside the for()?

I decided that it would be better then the following alignment of for loop:

    for ( const struct vsbi_ext *ext = _svsbi_exts;
          ext != _evsbi_exts;
          ext++ )

I am okay to change that to be aligned with the code above.

Quoted 23 lines
Quoted 9 lines
xen/arch/riscv/vsbi/core.c new file +8 -0
+        if ( (eid >= ext->eid_start) && (eid <= ext->eid_end) )
+            return ext;
+
+    return NULL;
+}
+
+void vsbi_handle_ecall(struct vcpu *vcpu, struct cpu_user_regs *regs)
+{

xen/arch/riscv/vsbi/core.c line 44

Quoted 10 lines
xen/arch/riscv/vsbi/core.c new file +10 -0
+    const unsigned long eid = regs->a7;
+    const unsigned long fid = regs->a6;
+    const struct vsbi_ext *ext = vsbi_find_extension(eid);
+    int ret;
+
+    if ( ext )
+        ret = ext->handler(vcpu, eid, fid, regs);
+    else
+    {
+        printk("Unsupported Guest SBI EID #%#lx, FID #%lu\n", eid, regs->a1);

As before - anything guest triggered must not cause log spam issues. Minimally you want to use XENLOG_GUEST in such cases, but I think you really mean gprintk() here.

Quoted 4 lines

A connected question then arises: Why is "vcpu" being passed in, when the sole caller only ever passes "current"? (The connection here is that gprintk() also uses current, and hence would be wrong to use when vcpu != current.) Same question goes for the ->handler() hook.

Good question. I tried to keep this more generic, but after your comment I cannot find a case where this argument is actually used with something other than|current in downstream code.|

Let’s drop the|vcpu| argument for now, and if it is needed in the future, we can reintroduce it then.

Thanks.

~ Oleksii

#7 lore raw

Re: [PATCH v2 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests

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

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for

    SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

I can't spot any actual support for GETCHAR.

Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +1 -0
@@ -0,0 +1,65 @@
+

xen/arch/riscv/vsbi/legacy-extension.c line 11

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/console.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+
+#include <asm/processor.h>
+#include <asm/vsbi.h>
+
+static void vsbi_print_line(char c)

Misleading function name? The parameter doesn't fit the name, and ...

It was called only because of ...

Quoted 6 lines

xen/arch/riscv/vsbi/legacy-extension.c line 13

Quoted 3 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +2 -0
+{
+    struct domain *cd = current->domain;

I guess you copied this code from somewhere, but a variable of this type and contents wants to be named "currd".

Quoted 14 lines

xen/arch/riscv/vsbi/legacy-extension.c line 26

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+    struct domain_console *cons = cd->console;
+
+    if ( !is_console_printable(c) )
+        return;
+
+    spin_lock(&cons->lock);
+    ASSERT(cons->idx < ARRAY_SIZE(cons->buf));
+    if ( c != '\n' )
+        cons->buf[cons->idx++] = c;
+    if ( (cons->idx == (ARRAY_SIZE(cons->buf) - 1)) || (c == '\n') )
+    {
+        cons->buf[cons->idx] = '\0';
+        guest_printk(cd, XENLOG_G_DEBUG "%s\n", cons->buf);

... you also only print a line under certain conditions.

... this. (for the same reason as hvm_print_line() which has an argument 'uint32_t *val' but inside function working with chars because of 'char c = *val;')

I will change the name to vsbi_print_char().

Quoted 17 lines
Quoted 6 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +5 -0
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+

xen/arch/riscv/vsbi/legacy-extension.c line 41

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+static int vsbi_legacy_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                     unsigned long fid,
+                                     struct cpu_user_regs *regs)
+{
+    int ret = 0;
+
+    switch ( eid )
+    {
+    case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+        vsbi_print_line((char)regs->a0);

The cast isn't really needed, is it?

No, I think it could be omitted.

Quoted 3 lines

And just to double-check: The spec demands
the upper bits to be ignored? (A link to the spec could have been useful, e.g. in the cover letter.)

It doesn't mention anything about upper bit for PUTCHAR:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#extension-console-putchar-eid-0x01
(I will add a link to the spec in the cover letter.)
So I assume that they don't care about it. (IIUC your question correctly).

I also checked what OpenSBI does and it just call the following function:
void sbi_putc(char ch)

  {
	nputs_all(&ch, 1);

}
in the following way: sbi_putc(regs->a0);

Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +6 -0
+        break;
+
+    case SBI_EXT_0_1_CONSOLE_GETCHAR:
+        ret = SBI_ERR_NOT_SUPPORTED;
+        break;
+
Quoted 8 lines

xen/arch/riscv/vsbi/legacy-extension.c line 54

Quoted 7 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +7 -0
+    default:
+        /*
+         * TODO: domain_crash() is acceptable here while things are still under
+         * development.
+         * It shouldn't stay like this in the end though: guests should not
+         * be punished like this for something Xen hasn't implemented.
+         */

Question then is why SBI_EXT_0_1_CONSOLE_GETCHAR gets a separate case block.

Because we intentionally do not support|SBI_EXT_0_1_CONSOLE_GETCHAR|,|domain_crash()| should not be called for it. Currently,|domain_crash()| is used only for debugging purposes while Xen for RISC-V is still under development. In the future, the default case should simply return|SBI_ERR_NOT_SUPPORTED|, and the|SBI_EXT_0_1_CONSOLE_GETCHAR| case block will be removed.

Thanks.

~ Oleksii

#13 lore raw

Re: [PATCH v2 3/3] xen/riscv: add RISC-V virtual SBI base extension support for guests

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/sbi.h +0 -0
@@ -14,6 +14,10 @@
 
Quoted 8 lines

xen/arch/riscv/include/asm/sbi.h line 19

Quoted 5 lines
xen/arch/riscv/include/asm/sbi.h +3 -0
  #include <xen/cpumask.h>
 
+#define XEN_SBI_VER_MAJOR 0
+#define XEN_SBI_VER_MINOR 2
+#define XEN_SBI_IMPID 7

Are these numbers part of the spec (sorry, lack of a reference makes me wonder, plus if that were the case, I'd kind of expect the names to be SBI_XEN_..., not XEN_SBI_...)?

XEN_SBI_IMPID is a number defined by the SBI specification:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-base.adoc#sbi-implementation-ids

XEN_SBI_VER_MAJOR and XEN_SBI_VER_MINOR somehow also is a part of the spec, there is no such defines explicitly, but it is real numbers of the SBI version.

I will rename the defines accordingly:
- s/XEN_SBI_VER_MAJOR/SBI_XEN_VER_MAJOR
- s/XEN_SBI_VER_MINOR/SBI_XEN_VER_MINOR
- s/XEN_SBI_IMPID/SBI_XEN_IMPID

Quoted 36 lines
Quoted 11 lines
xen/arch/riscv/vsbi/base-extension.c new file +7 -0
@@ -0,0 +1,71 @@
+
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/version.h>
+

xen/arch/riscv/vsbi/base-extension.c line 17

Quoted 10 lines
xen/arch/riscv/vsbi/base-extension.c new file +10 -0
+#include <asm/processor.h>
+#include <asm/sbi.h>
+#include <asm/vsbi.h>
+
+static int vsbi_base_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                   unsigned long fid,
+                                   struct cpu_user_regs *regs)
+{
+    int ret = 0;
+    struct sbiret sbi_ret;
     ASSERT(eid == SBI_EXT_BASE);

xen/arch/riscv/vsbi/base-extension.c line 19

Quoted 1 line
xen/arch/riscv/vsbi/base-extension.c new file +1 -0
+    switch ( fid ) {

Nit: Brace placement.

xen/arch/riscv/vsbi/base-extension.c line 29

Quoted 10 lines
xen/arch/riscv/vsbi/base-extension.c new file +10 -0
+    case SBI_EXT_BASE_GET_SPEC_VERSION:
+        regs->a1 = MASK_INSR(XEN_SBI_VER_MAJOR, SBI_SPEC_VERSION_MAJOR_MASK) |
+                   XEN_SBI_VER_MINOR;
+        break;
+    case SBI_EXT_BASE_GET_IMP_ID:
+        regs->a1 = XEN_SBI_IMPID;
+        break;
+    case SBI_EXT_BASE_GET_IMP_VERSION:
+        regs->a1 = (xen_major_version() << 16) | xen_minor_version();
+        break;
Quoted 3 lines

Along those lines here - are we free to use an arbitrary layout (shifting major by 16 bits), or is this mandated by the spec? At least in the latter case, the 16 will want to gain a #define.

I would say that we are free to use an arbitrary layout. The specification says:
The encoding of this version number is specific to the SBI implementation.
(https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-base.adoc#function-get-sbi-implementation-version-fid-2)

So this fully depends on how a specific SBI implementation decides to encode the version. For Xen, I simply copied the approach used by OpenSBI:
/**
* OpenSBI 32-bit version with:
* 1. upper 16-bits as major number
* 2. lower 16-bits as minor number
*/
#define OPENSBI_VERSION ((OPENSBI_VERSION_MAJOR << 16) | \
(OPENSBI_VERSION_MINOR))

Therefore, I think it is fine to keep Xen’s implementation as it is now, without introducing additional defines.

Thanks.

~ Oleksii

#8 lore raw

Re: [PATCH v2 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests

In reply to Oleksii Kurochko
Quoted 77 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for

    SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

I can't spot any actual support for GETCHAR.

Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +1 -0
@@ -0,0 +1,65 @@
+

xen/arch/riscv/vsbi/legacy-extension.c line 11

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/console.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+
+#include <asm/processor.h>
+#include <asm/vsbi.h>
+
+static void vsbi_print_line(char c)

Misleading function name? The parameter doesn't fit the name, and ...

It was called only because of ...

Quoted 6 lines

xen/arch/riscv/vsbi/legacy-extension.c line 13

Quoted 3 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +2 -0
+{
+    struct domain *cd = current->domain;

I guess you copied this code from somewhere, but a variable of this type and contents wants to be named "currd".

Quoted 14 lines

xen/arch/riscv/vsbi/legacy-extension.c line 26

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+    struct domain_console *cons = cd->console;
+
+    if ( !is_console_printable(c) )
+        return;
+
+    spin_lock(&cons->lock);
+    ASSERT(cons->idx < ARRAY_SIZE(cons->buf));
+    if ( c != '\n' )
+        cons->buf[cons->idx++] = c;
+    if ( (cons->idx == (ARRAY_SIZE(cons->buf) - 1)) || (c == '\n') )
+    {
+        cons->buf[cons->idx] = '\0';
+        guest_printk(cd, XENLOG_G_DEBUG "%s\n", cons->buf);

... you also only print a line under certain conditions.

... this. (for the same reason as hvm_print_line() which has an argument 'uint32_t *val' but inside function working with chars because of 'char c = *val;')

I will change the name to vsbi_print_char().

Quoted 17 lines
Quoted 6 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +5 -0
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+

xen/arch/riscv/vsbi/legacy-extension.c line 41

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+static int vsbi_legacy_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                     unsigned long fid,
+                                     struct cpu_user_regs *regs)
+{
+    int ret = 0;
+
+    switch ( eid )
+    {
+    case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+        vsbi_print_line((char)regs->a0);

The cast isn't really needed, is it?

No, I think it could be omitted.

Quoted 8 lines
Quoted 3 lines

And just to double-check: The spec demands
the upper bits to be ignored? (A link to the spec could have been useful, e.g. in the cover letter.)

It doesn't mention anything about upper bit for PUTCHAR:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#extension-console-putchar-eid-0x01
(I will add a link to the spec in the cover letter.)
So I assume that they don't care about it. (IIUC your question correctly).

I fear such a conclusion cannot be easily drawn. The parameter there is even "int". Anything not ASCII will remain unclear how to handle until the spec is changed to actually say what is intended.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +6 -0
+        break;
+
+    case SBI_EXT_0_1_CONSOLE_GETCHAR:
+        ret = SBI_ERR_NOT_SUPPORTED;
+        break;
+
Quoted 8 lines

xen/arch/riscv/vsbi/legacy-extension.c line 54

Quoted 7 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +7 -0
+    default:
+        /*
+         * TODO: domain_crash() is acceptable here while things are still under
+         * development.
+         * It shouldn't stay like this in the end though: guests should not
+         * be punished like this for something Xen hasn't implemented.
+         */

Question then is why SBI_EXT_0_1_CONSOLE_GETCHAR gets a separate case block.

Quoted 2 lines

Because we intentionally do not support|SBI_EXT_0_1_CONSOLE_GETCHAR|,|domain_crash()| should not be called for it.

That contradicts the patch description saying "starting with support for SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR." (Still in context at the top.)

Jan

#14 lore raw

Re: [PATCH v2 3/3] xen/riscv: add RISC-V virtual SBI base extension support for guests

In reply to Oleksii Kurochko
Quoted 15 lines · Oleksii Kurochko
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/sbi.h +0 -0
@@ -14,6 +14,10 @@
 
Quoted 8 lines

xen/arch/riscv/include/asm/sbi.h line 19

Quoted 5 lines
xen/arch/riscv/include/asm/sbi.h +3 -0
  #include <xen/cpumask.h>
 
+#define XEN_SBI_VER_MAJOR 0
+#define XEN_SBI_VER_MINOR 2
+#define XEN_SBI_IMPID 7

Are these numbers part of the spec (sorry, lack of a reference makes me wonder, plus if that were the case, I'd kind of expect the names to be SBI_XEN_..., not XEN_SBI_...)?

Quoted 2 lines

XEN_SBI_IMPID is a number defined by the SBI specification:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-base.adoc#sbi-implementation-ids

I see. For this the name change below is then appropriate; for ...

Quoted 2 lines

XEN_SBI_VER_MAJOR and XEN_SBI_VER_MINOR somehow also is a part of the spec, there is no such defines explicitly, but it is real numbers of the SBI version.

... these two I'm unconvinced, otoh: If it is us to control the versions used here, the present names should imo remain. Brief comments may then also want adding to clarify the different origin of the numbers.

Jan

#9 lore raw

Re: [PATCH v2 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests

In reply to Jan Beulich
Quoted 76 lines · Jan Beulich
Quoted 75 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for

     SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

I can't spot any actual support for GETCHAR.

Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +1 -0
@@ -0,0 +1,65 @@
+

xen/arch/riscv/vsbi/legacy-extension.c line 11

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/console.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+
+#include <asm/processor.h>
+#include <asm/vsbi.h>
+
+static void vsbi_print_line(char c)

Misleading function name? The parameter doesn't fit the name, and ...

It was called only because of ...

Quoted 5 lines

xen/arch/riscv/vsbi/legacy-extension.c line 13

Quoted 2 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +2 -0
+{
+    struct domain *cd = current->domain;

I guess you copied this code from somewhere, but a variable of this type and contents wants to be named "currd".

Quoted 14 lines

xen/arch/riscv/vsbi/legacy-extension.c line 26

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+    struct domain_console *cons = cd->console;
+
+    if ( !is_console_printable(c) )
+        return;
+
+    spin_lock(&cons->lock);
+    ASSERT(cons->idx < ARRAY_SIZE(cons->buf));
+    if ( c != '\n' )
+        cons->buf[cons->idx++] = c;
+    if ( (cons->idx == (ARRAY_SIZE(cons->buf) - 1)) || (c == '\n') )
+    {
+        cons->buf[cons->idx] = '\0';
+        guest_printk(cd, XENLOG_G_DEBUG "%s\n", cons->buf);

... you also only print a line under certain conditions.

... this. (for the same reason as hvm_print_line() which has an argument 'uint32_t *val' but inside function working with chars because of 'char c = *val;')

I will change the name to vsbi_print_char().

Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +8 -0
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+
+static int vsbi_legacy_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                     unsigned long fid,
+                                     struct cpu_user_regs *regs)
Quoted 8 lines

xen/arch/riscv/vsbi/legacy-extension.c line 41

Quoted 7 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +7 -0
+{
+    int ret = 0;
+
+    switch ( eid )
+    {
+    case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+        vsbi_print_line((char)regs->a0);

The cast isn't really needed, is it?

No, I think it could be omitted.

Quoted 3 lines

And just to double-check: The spec demands
the upper bits to be ignored? (A link to the spec could have been useful, e.g. in the cover letter.)

Quoted 7 lines
Quoted 4 lines

It doesn't mention anything about upper bit for PUTCHAR:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#extension-console-putchar-eid-0x01
(I will add a link to the spec in the cover letter.)
So I assume that they don't care about it. (IIUC your question correctly).

I fear such a conclusion cannot be easily drawn. The parameter there is even "int". Anything not ASCII will remain unclear how to handle until the spec is changed to actually say what is intended.

Then it makes sense to add "WARN_ON(regs->a0 >> __CHAR_BIT__);" in SBI_EXT_0_1_CONSOLE_PUTCHAR case block.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +3 -0
+        break;
+
+    case SBI_EXT_0_1_CONSOLE_GETCHAR:

xen/arch/riscv/vsbi/legacy-extension.c line 54

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+        ret = SBI_ERR_NOT_SUPPORTED;
+        break;
+
+    default:
+        /*
+         * TODO: domain_crash() is acceptable here while things are still under
+         * development.
+         * It shouldn't stay like this in the end though: guests should not
+         * be punished like this for something Xen hasn't implemented.
+         */

Question then is why SBI_EXT_0_1_CONSOLE_GETCHAR gets a separate case block.

Quoted 4 lines
Quoted 2 lines

Because we intentionally do not support|SBI_EXT_0_1_CONSOLE_GETCHAR|,|domain_crash()| should not be called for it.

That contradicts the patch description saying "starting with support for SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR." (Still in context at the top.)

I will update the patch description. I just thought that that returning of "not supported" for *_GETCHAR() could count as an implementation.

Thanks.

~ Oleksii

#10 lore raw

Re: [PATCH v2 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests

Quoted 81 lines · Oleksii Kurochko
Quoted 80 lines · Jan Beulich
Quoted 79 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

This commit adds support for legacy SBI extensions (version 0.1) in Xen for guest domains.

The changes include:
1. Define all legacy SBI extension IDs (0x0 to 0x8) for better clarity and completeness.
2. Implement handling of legacy SBI extensions, starting with support for

     SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR.

I can't spot any actual support for GETCHAR.

Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +1 -0
@@ -0,0 +1,65 @@
+

xen/arch/riscv/vsbi/legacy-extension.c line 11

Quoted 10 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +10 -0
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/console.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+
+#include <asm/processor.h>
+#include <asm/vsbi.h>
+
+static void vsbi_print_line(char c)

Misleading function name? The parameter doesn't fit the name, and ...

It was called only because of ...

Quoted 5 lines

xen/arch/riscv/vsbi/legacy-extension.c line 13

Quoted 2 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +2 -0
+{
+    struct domain *cd = current->domain;

I guess you copied this code from somewhere, but a variable of this type and contents wants to be named "currd".

Quoted 14 lines

xen/arch/riscv/vsbi/legacy-extension.c line 26

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+    struct domain_console *cons = cd->console;
+
+    if ( !is_console_printable(c) )
+        return;
+
+    spin_lock(&cons->lock);
+    ASSERT(cons->idx < ARRAY_SIZE(cons->buf));
+    if ( c != '\n' )
+        cons->buf[cons->idx++] = c;
+    if ( (cons->idx == (ARRAY_SIZE(cons->buf) - 1)) || (c == '\n') )
+    {
+        cons->buf[cons->idx] = '\0';
+        guest_printk(cd, XENLOG_G_DEBUG "%s\n", cons->buf);

... you also only print a line under certain conditions.

... this. (for the same reason as hvm_print_line() which has an argument 'uint32_t *val' but inside function working with chars because of 'char c = *val;')

I will change the name to vsbi_print_char().

Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +8 -0
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+
+static int vsbi_legacy_ecall_handler(struct vcpu *vcpu, unsigned long eid,
+                                     unsigned long fid,
+                                     struct cpu_user_regs *regs)
Quoted 8 lines

xen/arch/riscv/vsbi/legacy-extension.c line 41

Quoted 7 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +7 -0
+{
+    int ret = 0;
+
+    switch ( eid )
+    {
+    case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+        vsbi_print_line((char)regs->a0);

The cast isn't really needed, is it?

No, I think it could be omitted.

Quoted 3 lines

And just to double-check: The spec demands
the upper bits to be ignored? (A link to the spec could have been useful, e.g. in the cover letter.)

It doesn't mention anything about upper bit for PUTCHAR:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#extension-console-putchar-eid-0x01
(I will add a link to the spec in the cover letter.)
So I assume that they don't care about it. (IIUC your question correctly).

Quoted 6 lines
Quoted 3 lines

I fear such a conclusion cannot be easily drawn. The parameter there is even "int". Anything not ASCII will remain unclear how to handle until the spec is changed to actually say what is intended.

Then it makes sense to add "WARN_ON(regs->a0 >> __CHAR_BIT__);" in SBI_EXT_0_1_CONSOLE_PUTCHAR case block.

Well, no, or at least not longer term: Like you may not ASSERT() or BUG_ON() on guest controlled data, you may not WARN_ON() on such.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

xen/arch/riscv/vsbi/legacy-extension.c line 54

Quoted 13 lines
xen/arch/riscv/vsbi/legacy-extension.c new file +13 -0
+        break;
+
+    case SBI_EXT_0_1_CONSOLE_GETCHAR:
+        ret = SBI_ERR_NOT_SUPPORTED;
+        break;
+
+    default:
+        /*
+         * TODO: domain_crash() is acceptable here while things are still under
+         * development.
+         * It shouldn't stay like this in the end though: guests should not
+         * be punished like this for something Xen hasn't implemented.
+         */

Question then is why SBI_EXT_0_1_CONSOLE_GETCHAR gets a separate case block.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Because we intentionally do not support|SBI_EXT_0_1_CONSOLE_GETCHAR|,|domain_crash()| should not be called for it.

That contradicts the patch description saying "starting with support for SBI_EXT_0_1_CONSOLE_{PUT,GET}CHAR." (Still in context at the top.)

I will update the patch description. I just thought that that returning of "not supported" for *_GETCHAR() could count as an implementation.

It counts as an implementation, but calling such "support" goes too far imo.

Jan