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

Oleksii Kurochko posted 3 patches 2 weeks, 2 days ago
There is a newer version of this series
[PATCH v3 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests
Posted by Oleksii Kurochko 2 weeks, 2 days ago
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_PUTCHAR. SBI_EXT_0_1_CONSOLE_GETCHAR is marked as
   not supported as legacy SBI console related stuff is expected to be used
   only for early debugging of guest.

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

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
Changes in v3:
 - s/vsbi_print_line/vsbi_print_char.
 - s/cd/currd in vsbi_print_char().
 - Drop a cast of regs->a0 when vsbi_print_line() is called.
 - Use current instead of `vcpu` argument as it was dropped from
   vsbi_base_ecall_handler() prototype.
---
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 | 64 ++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/riscv/vsbi/legacy-extension.c

diff --git a/xen/arch/riscv/include/asm/sbi.h b/xen/arch/riscv/include/asm/sbi.h
index ade24a572d..751bae6d66 100644
--- a/xen/arch/riscv/include/asm/sbi.h
+++ b/xen/arch/riscv/include/asm/sbi.h
@@ -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
diff --git a/xen/arch/riscv/vsbi/Makefile b/xen/arch/riscv/vsbi/Makefile
index 820eb10ac2..bc5755cb13 100644
--- a/xen/arch/riscv/vsbi/Makefile
+++ b/xen/arch/riscv/vsbi/Makefile
@@ -1 +1,2 @@
 obj-y += core.o
+obj-y += legacy-extension.o
diff --git a/xen/arch/riscv/vsbi/legacy-extension.c b/xen/arch/riscv/vsbi/legacy-extension.c
new file mode 100644
index 0000000000..ca869942d6
--- /dev/null
+++ b/xen/arch/riscv/vsbi/legacy-extension.c
@@ -0,0 +1,64 @@
+
+/* 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_char(char c)
+{
+    struct domain *currd = current->domain;
+    struct domain_console *cons = currd->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(currd, XENLOG_G_DEBUG "%s\n", cons->buf);
+        cons->idx = 0;
+    }
+    spin_unlock(&cons->lock);
+}
+
+static int vsbi_legacy_ecall_handler(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_char(regs->a0);
+        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.
+         */
+        domain_crash(current->domain,
+                     "%s: Unsupported ecall: FID: #%lx, EID: #%lx\n",
+                     __func__, fid, eid);
+        break;
+    }
+
+    return ret;
+}
+
+VSBI_EXT(legacy, SBI_EXT_0_1_SET_TIMER, SBI_EXT_0_1_SHUTDOWN,
+         vsbi_legacy_ecall_handler);
-- 
2.52.0
Re: [PATCH v3 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests
Posted by Jan Beulich 2 weeks, 1 day ago
On 22.12.2025 17:37, Oleksii Kurochko wrote:
> +static int vsbi_legacy_ecall_handler(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_char(regs->a0);
> +        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.
> +         */
> +        domain_crash(current->domain,
> +                     "%s: Unsupported ecall: FID: #%lx, EID: #%lx\n",

Hmm, wait - patch 1 says you would consistently use #%lu for FIDs. I can adjust
while committing, unless you tell me not to.

Jan
Re: [PATCH v3 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests
Posted by Oleksii Kurochko 2 weeks, 1 day ago
On 12/23/25 5:11 PM, Jan Beulich wrote:
> On 22.12.2025 17:37, Oleksii Kurochko wrote:
>> +static int vsbi_legacy_ecall_handler(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_char(regs->a0);
>> +        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.
>> +         */
>> +        domain_crash(current->domain,
>> +                     "%s: Unsupported ecall: FID: #%lx, EID: #%lx\n",
> Hmm, wait - patch 1 says you would consistently use #%lu for FIDs. I can adjust
> while committing, unless you tell me not to.

I think that we should drop printing FID at all for Legacy extension as according to
the spec.:
   The SBI function ID field in a6 register is ignored because these are encoded as multiple SBI
   extension IDs.
And according to "Function Listing" FID will be 0:
   https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#function-listing

I would be happy if you could drop printing of FID during commit. Let me know if you want me to
drop printing of FID in the next patch series version.

Thanks.

~ Oleksii
Re: [PATCH v3 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests
Posted by Oleksii Kurochko 2 weeks, 1 day ago
On 12/24/25 10:05 AM, Oleksii Kurochko wrote:
>
> On 12/23/25 5:11 PM, Jan Beulich wrote:
>> On 22.12.2025 17:37, Oleksii Kurochko wrote:
>>> +static int vsbi_legacy_ecall_handler(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_char(regs->a0);
>>> +        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.
>>> +         */
>>> +        domain_crash(current->domain,
>>> +                     "%s: Unsupported ecall: FID: #%lx, EID: #%lx\n",
>> Hmm, wait - patch 1 says you would consistently use #%lu for FIDs. I 
>> can adjust
>> while committing, unless you tell me not to.
>
> I think that we should drop printing FID at all for Legacy extension 
> as according to
> the spec.:
>   The SBI function ID field in a6 register is ignored because these 
> are encoded as multiple SBI
>   extension IDs.
> And according to "Function Listing" FID will be 0:
> https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-legacy.adoc#function-listing
>
> I would be happy if you could drop printing of FID during commit. Let 
> me know if you want me to
> drop printing of FID in the next patch series version.
>
EID printing should be also updated to #%#lx.

Sorry for noticing that only now.

~ Oleksii


Re: [PATCH v3 2/3] xen/riscv: add RISC-V legacy SBI extension support for guests
Posted by Jan Beulich 2 weeks, 1 day ago
On 22.12.2025 17:37, Oleksii Kurochko wrote:
> 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_PUTCHAR. SBI_EXT_0_1_CONSOLE_GETCHAR is marked as
>    not supported as legacy SBI console related stuff is expected to be used
>    only for early debugging of guest.
> 
> The implementation uses the existing virtual SBI framework to handle legacy
> SBI ecalls, ensuring compatibility with older SBI specifications in
> RISC-V guests.
> 
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>

Acked-by: Jan Beulich <jbeulich@suse.com>

Peeking ahead into patch 3, I notice that there you (properly) avoid saying
"this commit" in the description. Unlike here and in patch 1. Please try to
remember to avoid this and alike wording.

Jan