RISC-V: turn sbi_ecall into a variadic macro
Subsystems (7)
cover RISC-V: turn sbi_ecall into a variadic macro
Padding the ecalls with zeros is not great for reasons in [1/2]. I could also redo the code to use sbi_ecall1~sbi_ecall8, and drop sbi_ecall if you think that variadic macros are too evil. [2/2] puts the new macro that into use, and I hopefully haven't deleted any intentional trailing 0.
For a related issue, I hoped that the tracepoints in __sbi_ecall would only add a nop or two, because __sbi_ecall without tracepoints looks like this:
0xffffffff80023ad0 <+0>: addi sp,sp,-32
0xffffffff80023ad2 <+2>: sd s0,16(sp)
0xffffffff80023ad4 <+4>: sd ra,24(sp)
0xffffffff80023ad6 <+6>: addi s0,sp,32
0xffffffff80023ad8 <+8>: ecall
0xffffffff80023adc <+12>: ld ra,24(sp)
0xffffffff80023ade <+14>: ld s0,16(sp)
0xffffffff80023ae0 <+16>: addi sp,sp,32
0xffffffff80023ae2 <+18>: ret
Maybe I am missing some kernel config, because __sbi_ecall currently looks like this:
0xffffffff80023e58 <+0>: addi sp,sp,-112
0xffffffff80023e5a <+2>: sd s0,96(sp)
0xffffffff80023e5c <+4>: sd s1,88(sp)
0xffffffff80023e5e <+6>: sd s3,72(sp)
0xffffffff80023e60 <+8>: sd s4,64(sp)
0xffffffff80023e62 <+10>: sd s5,56(sp)
0xffffffff80023e64 <+12>: sd s6,48(sp)
0xffffffff80023e66 <+14>: sd s7,40(sp)
0xffffffff80023e68 <+16>: sd s8,32(sp)
0xffffffff80023e6a <+18>: sd s9,24(sp)
0xffffffff80023e6c <+20>: sd ra,104(sp)
0xffffffff80023e6e <+22>: sd s2,80(sp)
0xffffffff80023e70 <+24>: addi s0,sp,112
0xffffffff80023e72 <+26>: mv s9,a0
0xffffffff80023e74 <+28>: mv s8,a1
0xffffffff80023e76 <+30>: mv s7,a2
0xffffffff80023e78 <+32>: mv s6,a3
0xffffffff80023e7a <+34>: mv s5,a4
0xffffffff80023e7c <+36>: mv s4,a5
0xffffffff80023e7e <+38>: mv s3,a6
0xffffffff80023e80 <+40>: mv s1,a7
0xffffffff80023e82 <+42>: nop
0xffffffff80023e84 <+44>: nop
0xffffffff80023e88 <+48>: mv a0,s9
0xffffffff80023e8a <+50>: mv a1,s8
0xffffffff80023e8c <+52>: mv a2,s7
0xffffffff80023e8e <+54>: mv a3,s6
0xffffffff80023e90 <+56>: mv a4,s5
0xffffffff80023e92 <+58>: mv a5,s4
0xffffffff80023e94 <+60>: mv a6,s3
0xffffffff80023e96 <+62>: mv a7,s1
0xffffffff80023e98 <+64>: ecall
0xffffffff80023e9c <+68>: mv s3,a0
0xffffffff80023e9e <+70>: mv s2,a1
0xffffffff80023ea0 <+72>: nop
0xffffffff80023ea4 <+76>: ld ra,104(sp)
0xffffffff80023ea6 <+78>: ld s0,96(sp)
0xffffffff80023ea8 <+80>: ld s1,88(sp)
0xffffffff80023eaa <+82>: ld s4,64(sp)
0xffffffff80023eac <+84>: ld s5,56(sp)
0xffffffff80023eae <+86>: ld s6,48(sp)
0xffffffff80023eb0 <+88>: ld s7,40(sp)
0xffffffff80023eb2 <+90>: ld s8,32(sp)
0xffffffff80023eb4 <+92>: ld s9,24(sp)
0xffffffff80023eb6 <+94>: mv a0,s3
0xffffffff80023eb8 <+96>: mv a1,s2
0xffffffff80023eba <+98>: ld s3,72(sp)
0xffffffff80023ebc <+100>: ld s2,80(sp)
0xffffffff80023ebe <+102>: addi sp,sp,112
0xffffffff80023ec0 <+104>: ret
...
[The actual tracepoint handling goes up to +328.]
Radim Krčmář (2):
RISC-V: sbi: turn sbi_ecall into variadic macro
RISC-V: make use of variadic sbi_ecall
arch/riscv/include/asm/kvm_nacl.h | 4 +-- arch/riscv/include/asm/sbi.h | 34 +++++++++++++++++- arch/riscv/kernel/cpu_ops_sbi.c | 6 ++-- arch/riscv/kernel/paravirt.c | 2 +- arch/riscv/kernel/sbi.c | 57 ++++++++++++++----------------- arch/riscv/kernel/sbi_ecall.c | 2 +- arch/riscv/kernel/suspend.c | 4 +-- arch/riscv/kvm/nacl.c | 7 ++-- drivers/acpi/riscv/cppc.c | 4 +-- drivers/perf/riscv_pmu_sbi.c | 48 +++++++++++++------------- 10 files changed, 98 insertions(+), 70 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/2 RISC-V: sbi: turn sbi_ecall into variadic macro
Counting the arguments to sbi_ecall() and padding with zeros gets old pretty quick. It's also harder to distinguish a tailing 0 argument and the padding. The patch changes sbi_ecall() to accept anything between 1 and 8 integer arguments.
Those who can count are also given sbi_ecall1() to sbi_ecall8(), which the variadic magic uses under the hood. The error messages upon a programmer error are a bit hairy, as expected of macros, and the static_assert is there to improve them a bit.
The final goal would be avoid clobbering registers that are not used in the ecall, but we first have to fix the code generation around tracepoints if sbi_ecall is expected to be used in paths where performance is critical.
--- arch/riscv/include/asm/sbi.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
arch/riscv/include/asm/sbi.h +33 -1
@@ -10,6 +10,7 @@#include <linux/types.h> #include <linux/cpumask.h> #include <linux/jump_label.h> +#include <linux/build_bug.h> #ifdef CONFIG_RISCV_SBI enum sbi_ext_id {
@@ -465,9 +466,40 @@ struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, int fid, int ext); -#define sbi_ecall (e, f, a0, a1, a2, a3, a4, a5) \ + +#define sbi_ecall1 (e) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, 0, e) +#define sbi_ecall2 (e, f) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall3 (e, f, a0) \ + __sbi_ecall(a0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall4 (e, f, a0, a1) \ + __sbi_ecall(a0, a1, 0, 0, 0, 0, f, e) +#define sbi_ecall5 (e, f, a0, a1, a2) \ + __sbi_ecall(a0, a1, a2, 0, 0, 0, f, e) +#define sbi_ecall6 (e, f, a0, a1, a2, a3) \ + __sbi_ecall(a0, a1, a2, a3, 0, 0, f, e) +#define sbi_ecall7 (e, f, a0, a1, a2, a3, a4) \ + __sbi_ecall(a0, a1, a2, a3, a4, 0, f, e) +#define sbi_ecall8 (e, f, a0, a1, a2, a3, a4, a5) \ __sbi_ecall(a0, a1, a2, a3, a4, a5, f, e) +#define __sbi_count_args_magic (_, a, b, c, d, e, f, g, h,N , ...) N +#define __sbi_count_args (...) \ + __sbi_count_args_magic(_, ## __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define __sbi_count_args2 (...) \ + (sizeof((unsigned long[]){0, ## __VA_ARGS__}) / sizeof(unsigned long) - 1) +#define __sbi_concat_expanded (a, b) a ## b +#define __sbi_concat (n) __sbi_concat_expanded(sbi_ecall, n) + +/* sbi_ecall selects the appropriate sbi_ecall1 to sbi_ecall8 */ +#define sbi_ecall (...) \ + ({ \ + static_assert(__sbi_count_args2(__VA_ARGS__) <= 8); \ + __sbi_concat(__sbi_count_args(__VA_ARGS__)) \ + (__VA_ARGS__); \ + }) +#ifdef CONFIG_RISCV_SBI_V01 void sbi_console_putchar (int ch);int sbi_console_getchar (void );
Signature · 1 line
2.49.0
Re: [PATCH 1/2] RISC-V: sbi: turn sbi_ecall into variadic macro
Quoted 48 lines · Radim Krčmář
Counting the arguments to
sbi_ecall()and padding with zeros gets old pretty quick. It's also harder to distinguish a tailing 0 argument and the padding. The patch changessbi_ecall()to accept anything between 1 and 8 integer arguments.Those who can count are also given
sbi_ecall1()tosbi_ecall8(), which the variadic magic uses under the hood. The error messages upon a programmer error are a bit hairy, as expected of macros, and thestatic_assertis there to improve them a bit.The final goal would be avoid clobbering registers that are not used in the ecall, but we first have to fix the code generation around tracepoints if
sbi_ecallis expected to be used in paths where performance is critical.Signed-off-by Radim Krčmář <[email protected]>--- arch/riscv/include/asm/sbi.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)arch/riscv/include/asm/sbi.h +12
-1@@ -10,6 +10,7 @@#include <linux/types.h> #include <linux/cpumask.h> #include <linux/jump_label.h> +#include <linux/build_bug.h> #ifdef CONFIG_RISCV_SBI enum sbi_ext_id {@@ -465,9 +466,40 @@ struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, int fid, int ext); -#define sbi_ecall (e, f, a0, a1, a2, a3, a4, a5) \ + +#define sbi_ecall1 (e) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, 0, e) +#define sbi_ecall2 (e, f) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall3 (e, f, a0) \ + __sbi_ecall(a0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall4 (e, f, a0, a1) \ + __sbi_ecall(a0, a1, 0, 0, 0, 0, f, e) +#define sbi_ecall5 (e, f, a0, a1, a2) \ + __sbi_ecall(a0, a1, a2, 0, 0, 0, f, e)
arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 489
Quoted 10 lines
arch/riscv/include/asm/sbi.h +8
-0+#define sbi_ecall6 (e, f, a0, a1, a2, a3) \ + __sbi_ecall(a0, a1, a2, a3, 0, 0, f, e) +#define sbi_ecall7 (e, f, a0, a1, a2, a3, a4) \ + __sbi_ecall(a0, a1, a2, a3, a4, 0, f, e) +#define sbi_ecall8 (e, f, a0, a1, a2, a3, a4, a5) \ __sbi_ecall(a0, a1, a2, a3, a4, a5, f, e) +#define __sbi_count_args_magic (_, a, b, c, d, e, f, g, h,N , ...) N +#define __sbi_count_args (...) \ + __sbi_count_args_magic(_, ## __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
This looks a lot like COUNT_ARGS() from include/linux/args.h.
arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 492
Quoted 3 lines
arch/riscv/include/asm/sbi.h +3
-0+#define __sbi_count_args2 (...) \ + (sizeof((unsigned long[]){0, ## __VA_ARGS__}) / sizeof(unsigned long) - 1) +#define __sbi_concat_expanded (a, b) a ## b
... and CONCATENATE()
arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 498
Quoted 6 lines
arch/riscv/include/asm/sbi.h +6
-0+#define __sbi_concat (n) __sbi_concat_expanded(sbi_ecall, n) + +/* sbi_ecall selects the appropriate sbi_ecall1 to sbi_ecall8 */ +#define sbi_ecall (...) \ + ({ \ + static_assert(__sbi_count_args2(__VA_ARGS__) <= 8); \
Why does this need to use __sbi_count_args2() ?
Quoted 10 lines
arch/riscv/include/asm/sbi.h +4
-0+__sbi_concat (__sbi_count_args (__VA_ARGS__)) \ + (__VA_ARGS__); \ + }) +#ifdef CONFIG_RISCV_SBI_V01 void sbi_console_putchar (int ch);int sbi_console_getchar (void );Signature · 1 line
2.49.0
Re: [PATCH 1/2] RISC-V: sbi: turn sbi_ecall into variadic macro
2025-06-12T17:14:09+02:00, Thomas Weißschuh <[email protected]>:
Quoted 62 lines · Radim Krčmář
Quoted 48 lines
Counting the arguments to
sbi_ecall()and padding with zeros gets old pretty quick. It's also harder to distinguish a tailing 0 argument and the padding. The patch changessbi_ecall()to accept anything between 1 and 8 integer arguments.Those who can count are also given
sbi_ecall1()tosbi_ecall8(), which the variadic magic uses under the hood. The error messages upon a programmer error are a bit hairy, as expected of macros, and thestatic_assertis there to improve them a bit.The final goal would be avoid clobbering registers that are not used in the ecall, but we first have to fix the code generation around tracepoints if
sbi_ecallis expected to be used in paths where performance is critical.Signed-off-by Radim Krčmář <[email protected]>--- arch/riscv/include/asm/sbi.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)arch/riscv/include/asm/sbi.h +12
-1@@ -10,6 +10,7 @@#include <linux/types.h> #include <linux/cpumask.h> #include <linux/jump_label.h> +#include <linux/build_bug.h> #ifdef CONFIG_RISCV_SBI enum sbi_ext_id {@@ -465,9 +466,40 @@ struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, int fid, int ext); -#define sbi_ecall (e, f, a0, a1, a2, a3, a4, a5) \ + +#define sbi_ecall1 (e) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, 0, e) +#define sbi_ecall2 (e, f) \ + __sbi_ecall(0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall3 (e, f, a0) \ + __sbi_ecall(a0, 0, 0, 0, 0, 0, f, e) +#define sbi_ecall4 (e, f, a0, a1) \ + __sbi_ecall(a0, a1, 0, 0, 0, 0, f, e) +#define sbi_ecall5 (e, f, a0, a1, a2) \ + __sbi_ecall(a0, a1, a2, 0, 0, 0, f, e)arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 489
Quoted 10 lines
arch/riscv/include/asm/sbi.h +8
-0+#define sbi_ecall6 (e, f, a0, a1, a2, a3) \ + __sbi_ecall(a0, a1, a2, a3, 0, 0, f, e) +#define sbi_ecall7 (e, f, a0, a1, a2, a3, a4) \ + __sbi_ecall(a0, a1, a2, a3, a4, 0, f, e) +#define sbi_ecall8 (e, f, a0, a1, a2, a3, a4, a5) \ __sbi_ecall(a0, a1, a2, a3, a4, a5, f, e) +#define __sbi_count_args_magic (_, a, b, c, d, e, f, g, h,N , ...) N +#define __sbi_count_args (...) \ + __sbi_count_args_magic(_, ## __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)This looks a lot like
COUNT_ARGS()frominclude/linux/args.h.
Quoted 5 lines
arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 492
Quoted 3 lines
arch/riscv/include/asm/sbi.h +3
-0+#define __sbi_count_args2 (...) \ + (sizeof((unsigned long[]){0, ## __VA_ARGS__}) / sizeof(unsigned long) - 1) +#define __sbi_concat_expanded (a, b) a ## b... and
CONCATENATE()
Thanks for pointing them out, I will use them in v2.
Quoted 8 lines
arch/riscv/include/asm/sbi.h struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1, · line 498
Quoted 6 lines
arch/riscv/include/asm/sbi.h +6
-0+#define __sbi_concat (n) __sbi_concat_expanded(sbi_ecall, n) + +/* sbi_ecall selects the appropriate sbi_ecall1 to sbi_ecall8 */ +#define sbi_ecall (...) \ + ({ \ + static_assert(__sbi_count_args2(__VA_ARGS__) <= 8); \Why does this need to use
__sbi_count_args2()?
When you go over the hardcoded maximum count in COUNT_ARGS, the macro stops counting and returns the arguments instead.
The array approach in __sbi_count_args2() counts any number of arguments, so it gives a much more understandable error.
I guess using COUNT_ARGS() and condemning programmers that overshoot the 15 counted arguments is better. We don't really need a static_assert then either, and v2 coud be just:
#define sbi_ecall(...) \ CONCATENATE(sbi_ecall, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__);
PATCH 2/2 RISC-V: make use of variadic sbi_ecall
In reply to Radim KrčmářThe new sbi_ecall doesn't have to list all 8 arguments anymore, so only pass the actual numbers of arguments for each SBI function.
Trailing 0 is sometimes intentional.
--- arch/riscv/include/asm/kvm_nacl.h | 4 +-- arch/riscv/kernel/cpu_ops_sbi.c | 6 ++-- arch/riscv/kernel/paravirt.c | 2 +- arch/riscv/kernel/sbi.c | 57 ++++++++++++++----------------- arch/riscv/kernel/sbi_ecall.c | 2 +- arch/riscv/kernel/suspend.c | 4 +-- arch/riscv/kvm/nacl.c | 7 ++-- drivers/acpi/riscv/cppc.c | 4 +-- drivers/perf/riscv_pmu_sbi.c | 48 +++++++++++++------------- 9 files changed, 65 insertions(+), 69 deletions(-)
arch/riscv/include/asm/kvm_nacl.h +2 -2
@@ -96,7 +96,7 @@ do { \#define nacl_sync_hfence (__e) \ sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SYNC_HFENCE, \ - (__e), 0, 0, 0, 0, 0) + (__e))#define nacl_hfence_mkconfig (__type, __order, __vmid, __asid) \ ({ \
@@ -196,7 +196,7 @@ do { \#define nacl_sync_csr (__csr) \ sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SYNC_CSR, \ - (__csr), 0, 0, 0, 0, 0) + (__csr))/* * Each ncsr_xyz() macro defined below has it's own static-branch so every
arch/riscv/kernel/cpu_ops_sbi.c +3 -3
@@ -29,7 +29,7 @@ static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,struct sbiret ret; ret =sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_START , - hartid, saddr, priv,0 ,0 ,0 ); + hartid, saddr, priv);if (ret.error)return sbi_err_map_linux_errno (ret.error);else
@@ -41,7 +41,7 @@ static int sbi_hsm_hart_stop(void) {struct sbiret ret; - ret =sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_STOP ,0 ,0 ,0 ,0 ,0 ,0 ); + ret =sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_STOP );if (ret.error)return sbi_err_map_linux_errno (ret.error);
@@ -54,7 +54,7 @@ static int sbi_hsm_hart_get_status(unsigned long hartid)struct sbiret ret; ret =sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_STATUS , - hartid,0 ,0 ,0 ,0 ,0 ); + hartid);if (ret.error)return sbi_err_map_linux_errno (ret.error);else
arch/riscv/kernel/paravirt.c +1 -1
@@ -60,7 +60,7 @@ static int sbi_sta_steal_time_set_shmem(unsigned long lo, unsigned long hi,struct sbiret ret; ret =sbi_ecall (SBI_EXT_STA ,SBI_EXT_STA_STEAL_TIME_SET_SHMEM , - lo, hi, flags,0 ,0 ,0 ); + lo, hi, flags);if (ret.error) {if (lo ==SBI_SHMEM_DISABLE && hi ==SBI_SHMEM_DISABLE )pr_warn ("Failed to disable steal-time shmem" );
arch/riscv/kernel/sbi.c +26 -31
173 lines not on this page · raw message
arch/riscv/kernel/sbi_ecall.c +1 -1
@@ -9,7 +9,7 @@ long __sbi_base_ecall(int fid) {struct sbiret ret; - ret =sbi_ecall (SBI_EXT_BASE , fid,0 ,0 ,0 ,0 ,0 ,0 ); + ret =sbi_ecall (SBI_EXT_BASE , fid);if (!ret.error)return ret.value;else
arch/riscv/kernel/suspend.c +2 -2
@@ -115,7 +115,7 @@ static int sbi_system_suspend(unsigned long sleep_type,struct sbiret ret; ret =sbi_ecall (SBI_EXT_SUSP ,SBI_EXT_SUSP_SYSTEM_SUSPEND , - sleep_type, resume_addr, opaque,0 ,0 ,0 ); + sleep_type, resume_addr, opaque);if (ret.error)return sbi_err_map_linux_errno (ret.error);
@@ -153,7 +153,7 @@ static int sbi_suspend_finisher(unsigned long suspend_type,struct sbiret ret; ret =sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_SUSPEND , - suspend_type, resume_addr, opaque,0 ,0 ,0 ); + suspend_type, resume_addr, opaque);return (ret.error) ?sbi_err_map_linux_errno (ret.error) :0 ; }
arch/riscv/kvm/nacl.c +3 -4
@@ -61,7 +61,7 @@ int kvm_riscv_nacl_enable(void) nacl =this_cpu_ptr (&kvm_riscv_nacl); ret =sbi_ecall (SBI_EXT_NACL ,SBI_EXT_NACL_SET_SHMEM , - nacl->shmem_phys,0 ,0 ,0 ,0 ,0 ); + nacl->shmem_phys,0 ,0 ); rc =sbi_err_map_linux_errno (ret.error);if (rc)return rc;
@@ -75,7 +75,7 @@ void kvm_riscv_nacl_disable(void)return ;sbi_ecall (SBI_EXT_NACL ,SBI_EXT_NACL_SET_SHMEM , -SBI_SHMEM_DISABLE ,SBI_SHMEM_DISABLE ,0 ,0 ,0 ,0 ); +SBI_SHMEM_DISABLE ,SBI_SHMEM_DISABLE ,0 ); }void kvm_riscv_nacl_exit (void )
@@ -106,8 +106,7 @@ static long nacl_probe_feature(long feature_id)if (!kvm_riscv_nacl_available ())return 0 ; - ret =sbi_ecall (SBI_EXT_NACL ,SBI_EXT_NACL_PROBE_FEATURE , - feature_id,0 ,0 ,0 ,0 ,0 ); + ret =sbi_ecall (SBI_EXT_NACL ,SBI_EXT_NACL_PROBE_FEATURE , feature_id);return ret.value; }
drivers/acpi/riscv/cppc.c +2 -2
@@ -53,7 +53,7 @@ static void sbi_cppc_read(void *read_data)struct sbi_cppc_data *data = (struct sbi_cppc_data *)read_data; data->ret =sbi_ecall (SBI_EXT_CPPC ,SBI_CPPC_READ , - data->reg,0 ,0 ,0 ,0 ,0 ); + data->reg); }static void sbi_cppc_write (void *write_data)
@@ -61,7 +61,7 @@ static void sbi_cppc_write(void *write_data)struct sbi_cppc_data *data = (struct sbi_cppc_data *)write_data; data->ret =sbi_ecall (SBI_EXT_CPPC ,SBI_CPPC_WRITE , - data->reg, data->val,0 ,0 ,0 ,0 ); + data->reg, data->val); }static void cppc_ffh_csr_read (void *read_data)
drivers/perf/riscv_pmu_sbi.c +25 -23
145 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH 2/2] RISC-V: make use of variadic sbi_ecall
Quoted 4 lines · On Thu, 12 Jun 2025 16:57:55 +0200 Radim Krčmář <[email protected]>…
The new
sbi_ecalldoesn't have to list all 8 arguments anymore, so only pass the actual numbers of arguments for each SBI function.Trailing 0 is sometimes intentional.
...
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu) · line 633
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +1
-1@@ -630,10 +630,10 @@ static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu)if (IS_ENABLED (CONFIG_32BIT )) ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , cpu_hw_evt->snapshot_addr_phys, - (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 ,0 ,0 ,0 ,0 ); + (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 );
That doesn't look right (and other similar ones).
The values are still 64bit - so get passed as two 32bit values (in some way) so that varargs code will get the wrong values.
I guess the previous change wasn't tested on 32bit?
David
Re: [PATCH 2/2] RISC-V: make use of variadic sbi_ecall
2025-06-13T11:54:59+01:00, David Laight <[email protected]>:
Quoted 8 lines
Quoted 4 lines · On Thu, 12 Jun 2025 16:57:55 +0200 Radim Krčmář <[email protected]>…
The new
sbi_ecalldoesn't have to list all 8 arguments anymore, so only pass the actual numbers of arguments for each SBI function.Trailing 0 is sometimes intentional.
...
Quoted 8 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu) · line 633
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +1
-1@@ -630,10 +630,10 @@ static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu)if (IS_ENABLED (CONFIG_32BIT )) ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , cpu_hw_evt->snapshot_addr_phys, - (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 ,0 ,0 ,0 ,0 ); + (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 );That doesn't look right (and other similar ones).
This one is wrong, but because I missed the flags. This patch should have been:
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, cpu_hw_evt->snapshot_addr_phys, (u64)(cpu_hw_evt->snapshot_addr_phys) >> 32, 0);
I'll fix that in v2, thanks. I think you might be referring to the fact that the code would make more sense as:
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, lower_32_bits(cpu_hw_evt->snapshot_addr_phys), upper_32_bits(cpu_hw_evt->snapshot_addr_phys))
I fully agree with that, but it's a different patch... I would even special case the if with CONFIG_32BIT && CONFIG_PHYS_ADDR_T_64BIT to make it extra clear why we're doing such a weird thing.
Quoted 2 lines
The values are still 64bit - so get passed as two 32bit values (in some way) so that varargs code will get the wrong values.
The SBI function prototype looks like this in the specification:
struct sbiret sbi_pmu_snapshot_set_shmem(unsigned long shmem_phys_lo,
unsigned long shmem_phys_hi, unsigned long flags)
SBI defines long to be the native register width, 32-bit with CONFIG_32BIT, and therefore uses 2 registers to pass the physical address, because the physical address can be up to 34 bits on RV32.
The macro will result in the same arguments as before, and it is what the sbi_ecall actually should do.
Quoted 1 line
I guess the previous change wasn't tested on 32bit?
It wasn't even compiled, because 64-bit phys_addr_t on CONFIG_32BIT requires CONFIG_PHYS_ADDR_T_64BIT, but that config combination seems impossible at this point. "(u64)(cpu_hw_evt->snapshot_addr_phys) >> 32)" is a fancy way to say 0.
If we were able to compile with CONFIG_PHYS_ADDR_T_64BIT, I think the patch would produce the desired result, hopefully with a warning that we're implicitly casting u64 to u32, but that was there even before this patch.
Enabling CONFIG_PHYS_ADDR_T_64BIT will have its share of issues -- I noticed a bug where other 32-bit function (SBI_EXT_NACL_SET_SHMEM) forgets to pass the upper part of the physical address, but I didn't include it in this series, because it made no difference right now.
Re: [PATCH 2/2] RISC-V: make use of variadic sbi_ecall
On Fri, 13 Jun 2025 16:10:52 +0200
Radim Krčmář <[email protected]> wrote:
...
Quoted 2 lines
The macro will result in the same arguments as before, and it is what the
sbi_ecallactually should do.
Ugg...
Are you using pre-processor 'magic' to add a pile of zeros and then select the first 'n' arguments?
That ought to be banned as error prone.
I think the one for strncpy() removes the immediate compile error for:
strncpy(dest, src, 1, 2);
David
Re: [PATCH 2/2] RISC-V: make use of variadic sbi_ecall
2025-06-13T16:52:26+01:00, David Laight <[email protected]>:
Quoted 10 lines
On Fri, 13 Jun 2025 16:10:52 +0200
Radim Krčmář <[email protected]> wrote:...
Quoted 2 lines
The macro will result in the same arguments as before, and it is what the
sbi_ecallactually should do.Ugg...
Are you using pre-processor 'magic' to add a pile of zeros and then select the first 'n' arguments?
Sadly, yes. The zeros should have never been introduced in the code, so I'm hiding them for the moment.
I planned to remove the useless zeros in future patches.
Quoted 1 line
That ought to be banned as error prone.
Sounds reasonable to me. I will try to fix the insane tracepoint bloat, and send a v2 that fills only the correct amount of registers for the ecall instruction.
Quoted 2 lines
I think the one for
strncpy()removes the immediate compile error for:strncpy(dest, src, 1, 2);
I would have preferred if sbi.h had a wrapper function for each SBI function, and we never allowed direct invocation of the ecall instruction outside of the library.