Skip to contentSkip to conversation

Add Kconfig option for each extension

PATCH New Vivian Wang 3 participants 10 messages Oct 10, 2022 – Oct 23, 2022 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Add Kconfig option for each extension

This series adds support for disabling SBI extensions via Kconfig. In the future, Kconfig can also be used to disable obsolete extensions by default.

All of the SBI extensions except Base extension is optional [1]. Users of OpenSBI may wish to disable support for some of the extensions for reasons such as:

- Future-proofing S-mode software, replacing SBI calls with new ISA functionality.
- Testing S-mode software to make sure they work correctly without certain extensions present.
- Disabling unused extensions to minimize the attack surface of M-mode software. See [2].

[1]: https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc#1-introduction
[2]: https://github.com/riscv-non-isa/riscv-sbi-doc/issues/102

Vivian Wang (3):
Makefile: Add rules for carray sources in lib/sbi
lib: sbi_ecall: Generate extensions list with carray
lib: sbi_ecall: Add Kconfig option for each extension

 Kconfig                       |  2 ++
 Makefile                      |  7 +++++++
 include/sbi/sbi_ecall.h       | 10 ---------
 lib/sbi/Kconfig               | 37 +++++++++++++++++++++++++++++++++
 lib/sbi/objects.mk            | 28 ++++++++++++++++++-------
 lib/sbi/sbi_ecall.c           | 39 ++++++++++-------------------------
 lib/sbi/sbi_ecall_exts.carray |  3 +++
 lib/sbi/sbi_ecall_replace.c   |  8 +++++++
 8 files changed, 89 insertions(+), 45 deletions(-)
 create mode 100644 lib/sbi/Kconfig
 create mode 100644 lib/sbi/sbi_ecall_exts.carray
Signature · 1 line
2.37.1
#1 lore raw

PATCH 1/3 Makefile: Add rules for carray sources in lib/sbi

Add back the missing rules needed to build carray files in lib/sbi. This allows future usage of carray in lib/sbi.

Fixes de80e9337d81 ("Makefile: Compile lib/utils sources separately for each platform")
---
 Makefile | 7 +++++++
 1 file changed, 7 insertions(+)
Makefile +7 -0
index 0ab9f22..2a3d080 100644
@@ -478,6 +478,13 @@ $(build_dir)/lib/libsbi.a: $(libsbi-objs-path-y)
 $(platform_build_dir)/lib/libplatsbi.a: $(libsbi-objs-path-y) $(libsbiutils-objs-path-y) $(platform-objs-path-y)
 	$(call compile_ar,$@,$^)
 
+$(build_dir)/%.dep: $(src_dir)/%.carray $(KCONFIG_CONFIG)
+	$(call compile_gen_dep,$@,.c,$< $(KCONFIG_CONFIG))
+	$(call compile_gen_dep,$@,.o,$(@:.dep=.c))
+
+$(build_dir)/%.c: $(src_dir)/%.carray
+	$(call compile_carray,$@,$<)
+
 $(build_dir)/%.dep: $(src_dir)/%.c $(KCONFIG_CONFIG)
 	$(call compile_cc_dep,$@,$<)
 
Signature · 1 line
2.37.1
Applied
#2 lore raw

PATCH 1/3 Makefile: Add rules for carray sources in lib/sbi

Quoted 6 lines · Vivian Wang

Add back the missing rules needed to build carray files in lib/sbi. This allows future usage of carray in lib/sbi.

Fixes de80e9337d81 ("Makefile: Compile lib/utils sources separately for each platform")

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 30 lines
---
 Makefile | 7 +++++++
 1 file changed, 7 insertions(+)
Makefile +7 -1
index 0ab9f22..2a3d080 100644
@@ -478,6 +478,13 @@ $(build_dir)/lib/libsbi.a: $(libsbi-objs-path-y)
 $(platform_build_dir)/lib/libplatsbi.a: $(libsbi-objs-path-y) $(libsbiutils-objs-path-y) $(platform-objs-path-y)
        $(call compile_ar,$@,$^)
 
+$(build_dir)/%.dep: $(src_dir)/%.carray $(KCONFIG_CONFIG)
+       $(call compile_gen_dep,$@,.c,$< $(KCONFIG_CONFIG))
+       $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
+
+$(build_dir)/%.c: $(src_dir)/%.carray
+       $(call compile_carray,$@,$<)
+
 $(build_dir)/%.dep: $(src_dir)/%.c $(KCONFIG_CONFIG)
        $(call compile_cc_dep,$@,$<)
 
--

2.37.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH 2/3 lib: sbi_ecall: Generate extensions list with carray

In reply to Vivian Wang

Instead of hard-coding the list of extensions in C code, use carray to generate the list of extensions.

Using carray makes adding and removing extensions slightly cleaner. This also paves the way for using Kconfig to disable unneeded extensions.

---
 include/sbi/sbi_ecall.h       | 10 ---------
 lib/sbi/objects.mk            | 22 ++++++++++++++++----
 lib/sbi/sbi_ecall.c           | 39 ++++++++++-------------------------
 lib/sbi/sbi_ecall_exts.carray |  3 +++
 4 files changed, 32 insertions(+), 42 deletions(-)
 create mode 100644 lib/sbi/sbi_ecall_exts.carray
include/sbi/sbi_ecall.h +0 -10
index 046a56a..ff9bf8e 100644
@@ -31,16 +31,6 @@ struct sbi_ecall_extension {
 		       struct sbi_trap_info *out_trap);
 };
 
-extern struct sbi_ecall_extension ecall_base;
-extern struct sbi_ecall_extension ecall_legacy;
-extern struct sbi_ecall_extension ecall_time;
-extern struct sbi_ecall_extension ecall_rfence;
-extern struct sbi_ecall_extension ecall_ipi;
-extern struct sbi_ecall_extension ecall_vendor;
-extern struct sbi_ecall_extension ecall_hsm;
-extern struct sbi_ecall_extension ecall_srst;
-extern struct sbi_ecall_extension ecall_pmu;
-
 u16 sbi_ecall_version_major(void);
 
 u16 sbi_ecall_version_minor(void);
lib/sbi/objects.mk +18 -4
index 1ed1983..fa20b7b 100644
@@ -12,17 +12,31 @@ libsbi-objs-y += riscv_atomic.o
 libsbi-objs-y += riscv_hardfp.o
 libsbi-objs-y += riscv_locks.o
 
-libsbi-objs-y += sbi_bitmap.o
-libsbi-objs-y += sbi_bitops.o
-libsbi-objs-y += sbi_console.o
-libsbi-objs-y += sbi_domain.o
 libsbi-objs-y += sbi_ecall.o
+libsbi-objs-y += sbi_ecall_exts.o
+
+# The order of below extensions is performance optimized
+carray-sbi_ecall_exts-y += ecall_time
+carray-sbi_ecall_exts-y += ecall_rfence
+carray-sbi_ecall_exts-y += ecall_ipi
+carray-sbi_ecall_exts-y += ecall_base
+carray-sbi_ecall_exts-y += ecall_hsm
+carray-sbi_ecall_exts-y += ecall_srst
+carray-sbi_ecall_exts-y += ecall_pmu
+carray-sbi_ecall_exts-y += ecall_legacy
+carray-sbi_ecall_exts-y += ecall_vendor
+
 libsbi-objs-y += sbi_ecall_base.o
 libsbi-objs-y += sbi_ecall_hsm.o
 libsbi-objs-y += sbi_ecall_legacy.o
 libsbi-objs-y += sbi_ecall_pmu.o
 libsbi-objs-y += sbi_ecall_replace.o
 libsbi-objs-y += sbi_ecall_vendor.o
+
+libsbi-objs-y += sbi_bitmap.o
+libsbi-objs-y += sbi_bitops.o
+libsbi-objs-y += sbi_console.o
+libsbi-objs-y += sbi_domain.o
 libsbi-objs-y += sbi_emulate_csr.o
 libsbi-objs-y += sbi_fifo.o
 libsbi-objs-y += sbi_hart.o
lib/sbi/sbi_ecall.c +11 -28
index ce021eb..25dd78c 100644
@@ -13,6 +13,9 @@
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_trap.h>
 
+extern struct sbi_ecall_extension *sbi_ecall_exts[];
+extern unsigned long sbi_ecall_exts_size;
+
 u16 sbi_ecall_version_major(void)
 {
 	return SBI_ECALL_VERSION_MAJOR;
@@ -144,35 +147,15 @@ int sbi_ecall_handler(struct sbi_trap_regs *regs)
 int sbi_ecall_init(void)
 {
 	int ret;
+	struct sbi_ecall_extension *ext;
+	unsigned long i;
 
-	/* The order of below registrations is performance optimized */
-	ret = sbi_ecall_register_extension(&ecall_time);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_rfence);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_ipi);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_base);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_hsm);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_srst);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_pmu);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_legacy);
-	if (ret)
-		return ret;
-	ret = sbi_ecall_register_extension(&ecall_vendor);
-	if (ret)
-		return ret;
+	for (i = 0; i < sbi_ecall_exts_size; i++) {
+		ext = sbi_ecall_exts[i];
+		ret = sbi_ecall_register_extension(ext);
+		if (ret)
+			return ret;
+	}
 
 	return 0;
 }
lib/sbi/sbi_ecall_exts.carray new file +3 -0
new file mode 100644
index 0000000..710e3ed
@@ -0,0 +1,3 @@
+HEADER: sbi/sbi_ecall.h
+TYPE: struct sbi_ecall_extension
+NAME: sbi_ecall_exts
Signature · 1 line
2.37.1
Applied
#4 lore raw

PATCH 2/3 lib: sbi_ecall: Generate extensions list with carray

Quoted 8 lines · Vivian Wang

Instead of hard-coding the list of extensions in C code, use carray to generate the list of extensions.

Using carray makes adding and removing extensions slightly cleaner. This also paves the way for using Kconfig to disable unneeded extensions.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 144 lines
---
 include/sbi/sbi_ecall.h       | 10 ---------
 lib/sbi/objects.mk            | 22 ++++++++++++++++----
 lib/sbi/sbi_ecall.c           | 39 ++++++++++-------------------------
 lib/sbi/sbi_ecall_exts.carray |  3 +++
 4 files changed, 32 insertions(+), 42 deletions(-)
 create mode 100644 lib/sbi/sbi_ecall_exts.carray
include/sbi/sbi_ecall.h +0 -10
index 046a56a..ff9bf8e 100644
@@ -31,16 +31,6 @@ struct sbi_ecall_extension {
                       struct sbi_trap_info *out_trap);
 };
 
-extern struct sbi_ecall_extension ecall_base;
-extern struct sbi_ecall_extension ecall_legacy;
-extern struct sbi_ecall_extension ecall_time;
-extern struct sbi_ecall_extension ecall_rfence;
-extern struct sbi_ecall_extension ecall_ipi;
-extern struct sbi_ecall_extension ecall_vendor;
-extern struct sbi_ecall_extension ecall_hsm;
-extern struct sbi_ecall_extension ecall_srst;
-extern struct sbi_ecall_extension ecall_pmu;
-
 u16 sbi_ecall_version_major(void);
 
 u16 sbi_ecall_version_minor(void);
lib/sbi/objects.mk +18 -4
index 1ed1983..fa20b7b 100644
@@ -12,17 +12,31 @@ libsbi-objs-y += riscv_atomic.o
 libsbi-objs-y += riscv_hardfp.o
 libsbi-objs-y += riscv_locks.o
 
-libsbi-objs-y += sbi_bitmap.o
-libsbi-objs-y += sbi_bitops.o
-libsbi-objs-y += sbi_console.o
-libsbi-objs-y += sbi_domain.o
 libsbi-objs-y += sbi_ecall.o
+libsbi-objs-y += sbi_ecall_exts.o
+
+# The order of below extensions is performance optimized
+carray-sbi_ecall_exts-y += ecall_time
+carray-sbi_ecall_exts-y += ecall_rfence
+carray-sbi_ecall_exts-y += ecall_ipi
+carray-sbi_ecall_exts-y += ecall_base
+carray-sbi_ecall_exts-y += ecall_hsm
+carray-sbi_ecall_exts-y += ecall_srst
+carray-sbi_ecall_exts-y += ecall_pmu
+carray-sbi_ecall_exts-y += ecall_legacy
+carray-sbi_ecall_exts-y += ecall_vendor
+
 libsbi-objs-y += sbi_ecall_base.o
 libsbi-objs-y += sbi_ecall_hsm.o
 libsbi-objs-y += sbi_ecall_legacy.o
 libsbi-objs-y += sbi_ecall_pmu.o
 libsbi-objs-y += sbi_ecall_replace.o
 libsbi-objs-y += sbi_ecall_vendor.o
+
+libsbi-objs-y += sbi_bitmap.o
+libsbi-objs-y += sbi_bitops.o
+libsbi-objs-y += sbi_console.o
+libsbi-objs-y += sbi_domain.o
 libsbi-objs-y += sbi_emulate_csr.o
 libsbi-objs-y += sbi_fifo.o
 libsbi-objs-y += sbi_hart.o
lib/sbi/sbi_ecall.c +11 -28
index ce021eb..25dd78c 100644
@@ -13,6 +13,9 @@
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_trap.h>
 
+extern struct sbi_ecall_extension *sbi_ecall_exts[];
+extern unsigned long sbi_ecall_exts_size;
+
 u16 sbi_ecall_version_major(void)
 {
        return SBI_ECALL_VERSION_MAJOR;
@@ -144,35 +147,15 @@ int sbi_ecall_handler(struct sbi_trap_regs *regs)
 int sbi_ecall_init(void)
 {
        int ret;
+       struct sbi_ecall_extension *ext;
+       unsigned long i;
 
-       /* The order of below registrations is performance optimized */
-       ret = sbi_ecall_register_extension(&ecall_time);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_rfence);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_ipi);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_base);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_hsm);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_srst);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_pmu);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_legacy);
-       if (ret)
-               return ret;
-       ret = sbi_ecall_register_extension(&ecall_vendor);
-       if (ret)
-               return ret;
+       for (i = 0; i < sbi_ecall_exts_size; i++) {
+               ext = sbi_ecall_exts[i];
+               ret = sbi_ecall_register_extension(ext);
+               if (ret)
+                       return ret;
+       }
 
        return 0;
 }
lib/sbi/sbi_ecall_exts.carray new file +3 -1
new file mode 100644
index 0000000..710e3ed
@@ -0,0 +1,3 @@
+HEADER: sbi/sbi_ecall.h
+TYPE: struct sbi_ecall_extension
+NAME: sbi_ecall_exts
--

2.37.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

PATCH 3/3 lib: sbi_ecall: Add Kconfig option for each extension

In reply to Vivian Wang

For each SBI extension, we:

- Add a Kconfig option for it
- Add the extension to sbi_ecall_exts only if the extension is enabled
- Add the corresponding sbi_ecall_* object file only if the extension is enabled

Special cases are as follows:

- The legacy extensions are lumped together as one 'big' extension, as has always been the case in OpenSBI code.
- The platform-defined vendor extensions are regarded as one extension.
- The Base extension cannot be disabled.
- sbi_ecall_replace implements multiple extensions, so it's not easy to avoid linking it in. Enable it always, and use #ifdef to disable/enable individual extensions.

---
 Kconfig                     |  2 ++
 lib/sbi/Kconfig             | 37 +++++++++++++++++++++++++++++++++++++
 lib/sbi/objects.mk          | 24 ++++++++++++------------
 lib/sbi/sbi_ecall_replace.c |  8 ++++++++
 4 files changed, 59 insertions(+), 12 deletions(-)
 create mode 100644 lib/sbi/Kconfig
Kconfig +2 -0
index b3213a9..acfc138 100644
@@ -18,6 +18,8 @@ menu "Platform Options"
 source "$(OPENSBI_PLATFORM_SRC_DIR)/Kconfig"
 endmenu
 
+source "$(OPENSBI_SRC_DIR)/lib/sbi/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/firmware/Kconfig"
lib/sbi/Kconfig new file +37 -0
new file mode 100644
index 0000000..df74bba
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "SBI Extension Support"
+
+config SBI_ECALL_TIME
+	bool "Timer extension"
+	default y
+
+config SBI_ECALL_RFENCE
+	bool "RFENCE extension"
+	default y
+
+config SBI_ECALL_IPI
+	bool "IPI extension"
+	default y
+
+config SBI_ECALL_HSM
+	bool "Hart State Management extension"
+	default y
+
+config SBI_ECALL_SRST
+	bool "System Reset extension"
+	default y
+
+config SBI_ECALL_PMU
+	bool "Performance Monitoring Unit extension"
+	default y
+
+config SBI_ECALL_LEGACY
+	bool "SBI v0.1 legacy extensions"
+	default y
+
+config SBI_ECALL_VENDOR
+	bool "Platform-defined vendor extensions"
+	default y
+
+endmenu
lib/sbi/objects.mk +12 -12
index fa20b7b..783c46d 100644
@@ -16,22 +16,22 @@ libsbi-objs-y += sbi_ecall.o
 libsbi-objs-y += sbi_ecall_exts.o
 
 # The order of below extensions is performance optimized
-carray-sbi_ecall_exts-y += ecall_time
-carray-sbi_ecall_exts-y += ecall_rfence
-carray-sbi_ecall_exts-y += ecall_ipi
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_TIME) += ecall_time
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_RFENCE) += ecall_rfence
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_IPI) += ecall_ipi
 carray-sbi_ecall_exts-y += ecall_base
-carray-sbi_ecall_exts-y += ecall_hsm
-carray-sbi_ecall_exts-y += ecall_srst
-carray-sbi_ecall_exts-y += ecall_pmu
-carray-sbi_ecall_exts-y += ecall_legacy
-carray-sbi_ecall_exts-y += ecall_vendor
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_HSM) += ecall_hsm
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_SRST) += ecall_srst
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_PMU) += ecall_pmu
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_LEGACY) += ecall_legacy
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_VENDOR) += ecall_vendor
 
 libsbi-objs-y += sbi_ecall_base.o
-libsbi-objs-y += sbi_ecall_hsm.o
-libsbi-objs-y += sbi_ecall_legacy.o
-libsbi-objs-y += sbi_ecall_pmu.o
+libsbi-objs-$(CONFIG_SBI_ECALL_HSM) += sbi_ecall_hsm.o
+libsbi-objs-$(CONFIG_SBI_ECALL_LEGACY) += sbi_ecall_legacy.o
+libsbi-objs-$(CONFIG_SBI_ECALL_PMU) += sbi_ecall_pmu.o
 libsbi-objs-y += sbi_ecall_replace.o
-libsbi-objs-y += sbi_ecall_vendor.o
+libsbi-objs-$(CONFIG_SBI_ECALL_VENDOR) += sbi_ecall_vendor.o
 
 libsbi-objs-y += sbi_bitmap.o
 libsbi-objs-y += sbi_bitops.o
lib/sbi/sbi_ecall_replace.c +8 -0
index 93f44da..0ea00d6 100644
@@ -19,6 +19,7 @@
 #include <sbi/sbi_tlb.h>
 #include <sbi/sbi_trap.h>
 
+#ifdef CONFIG_SBI_ECALL_TIME
 static int sbi_ecall_time_handler(unsigned long extid, unsigned long funcid,
 				  const struct sbi_trap_regs *regs,
 				  unsigned long *out_val,
@@ -43,7 +44,9 @@ struct sbi_ecall_extension ecall_time = {
 	.extid_end = SBI_EXT_TIME,
 	.handle = sbi_ecall_time_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_RFENCE
 static int sbi_ecall_rfence_handler(unsigned long extid, unsigned long funcid,
 				    const struct sbi_trap_regs *regs,
 				    unsigned long *out_val,
@@ -113,7 +116,9 @@ struct sbi_ecall_extension ecall_rfence = {
 	.extid_end = SBI_EXT_RFENCE,
 	.handle = sbi_ecall_rfence_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_IPI
 static int sbi_ecall_ipi_handler(unsigned long extid, unsigned long funcid,
 				 const struct sbi_trap_regs *regs,
 				 unsigned long *out_val,
@@ -134,7 +139,9 @@ struct sbi_ecall_extension ecall_ipi = {
 	.extid_end = SBI_EXT_IPI,
 	.handle = sbi_ecall_ipi_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_SRST
 static int sbi_ecall_srst_handler(unsigned long extid, unsigned long funcid,
 				  const struct sbi_trap_regs *regs,
 				  unsigned long *out_val,
@@ -194,3 +201,4 @@ struct sbi_ecall_extension ecall_srst = {
 	.handle = sbi_ecall_srst_handler,
 	.probe = sbi_ecall_srst_probe,
 };
+#endif
Signature · 1 line
2.37.1
#6 lore raw

PATCH 3/3 lib: sbi_ecall: Add Kconfig option for each extension

Quoted 9 lines · Vivian Wang

For each SBI extension, we:

- Add a Kconfig option for it
- Add the extension to sbi_ecall_exts only if the extension is enabled
- Add the corresponding sbi_ecall_* object file only if the extension is enabled

Special cases are as follows:

Quoted 7 lines

- The legacy extensions are lumped together as one 'big' extension, as has always been the case in OpenSBI code.
- The platform-defined vendor extensions are regarded as one extension.
- The Base extension cannot be disabled.
- sbi_ecall_replace implements multiple extensions, so it's not easy to avoid linking it in. Enable it always, and use #ifdef to disable/enable individual extensions.

It's tempting to use this as an opportunity to break sbi_ecall_replace up into different files, but I'm not sure it's worth it.

#7 lore raw

PATCH 3/3 lib: sbi_ecall: Add Kconfig option for each extension

Quoted 19 lines · Andrew Jones
Quoted 9 lines · Vivian Wang

For each SBI extension, we:

- Add a Kconfig option for it
- Add the extension to sbi_ecall_exts only if the extension is enabled
- Add the corresponding sbi_ecall_* object file only if the extension is enabled

Special cases are as follows:

Quoted 7 lines

- The legacy extensions are lumped together as one 'big' extension, as has always been the case in OpenSBI code.
- The platform-defined vendor extensions are regarded as one extension.
- The Base extension cannot be disabled.
- sbi_ecall_replace implements multiple extensions, so it's not easy to avoid linking it in. Enable it always, and use #ifdef to disable/enable individual extensions.

Quoted 2 lines

It's tempting to use this as an opportunity to break sbi_ecall_replace up into different files, but I'm not sure it's worth it.

I agree with you.

@Vivan/Drew, can one of you break-up sbi_ecall_replace.c so that we can avoid the #ifdef in sources ?

Regards,
Anup

Quoted 5 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#8 lore raw

PATCH 3/3 lib: sbi_ecall: Add Kconfig option for each extension

In reply to Vivian Wang
Quoted 18 lines · Vivian Wang

For each SBI extension, we:

- Add a Kconfig option for it
- Add the extension to sbi_ecall_exts only if the extension is enabled
- Add the corresponding sbi_ecall_* object file only if the extension is enabled

Special cases are as follows:

- The legacy extensions are lumped together as one 'big' extension, as has always been the case in OpenSBI code.
- The platform-defined vendor extensions are regarded as one extension.
- The Base extension cannot be disabled.
- sbi_ecall_replace implements multiple extensions, so it's not easy to avoid linking it in. Enable it always, and use #ifdef to disable/enable individual extensions.

Quoted 1 line

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 158 lines
---
 Kconfig                     |  2 ++
 lib/sbi/Kconfig             | 37 +++++++++++++++++++++++++++++++++++++
 lib/sbi/objects.mk          | 24 ++++++++++++------------
 lib/sbi/sbi_ecall_replace.c |  8 ++++++++
 4 files changed, 59 insertions(+), 12 deletions(-)
 create mode 100644 lib/sbi/Kconfig
Kconfig +2 -0
index b3213a9..acfc138 100644
@@ -18,6 +18,8 @@ menu "Platform Options"
 source "$(OPENSBI_PLATFORM_SRC_DIR)/Kconfig"
 endmenu
 
+source "$(OPENSBI_SRC_DIR)/lib/sbi/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/firmware/Kconfig"
lib/sbi/Kconfig new file +37 -0
new file mode 100644
index 0000000..df74bba
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "SBI Extension Support"
+
+config SBI_ECALL_TIME
+       bool "Timer extension"
+       default y
+
+config SBI_ECALL_RFENCE
+       bool "RFENCE extension"
+       default y
+
+config SBI_ECALL_IPI
+       bool "IPI extension"
+       default y
+
+config SBI_ECALL_HSM
+       bool "Hart State Management extension"
+       default y
+
+config SBI_ECALL_SRST
+       bool "System Reset extension"
+       default y
+
+config SBI_ECALL_PMU
+       bool "Performance Monitoring Unit extension"
+       default y
+
+config SBI_ECALL_LEGACY
+       bool "SBI v0.1 legacy extensions"
+       default y
+
+config SBI_ECALL_VENDOR
+       bool "Platform-defined vendor extensions"
+       default y
+
+endmenu
lib/sbi/objects.mk +12 -12
index fa20b7b..783c46d 100644
@@ -16,22 +16,22 @@ libsbi-objs-y += sbi_ecall.o
 libsbi-objs-y += sbi_ecall_exts.o
 
 # The order of below extensions is performance optimized
-carray-sbi_ecall_exts-y += ecall_time
-carray-sbi_ecall_exts-y += ecall_rfence
-carray-sbi_ecall_exts-y += ecall_ipi
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_TIME) += ecall_time
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_RFENCE) += ecall_rfence
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_IPI) += ecall_ipi
 carray-sbi_ecall_exts-y += ecall_base
-carray-sbi_ecall_exts-y += ecall_hsm
-carray-sbi_ecall_exts-y += ecall_srst
-carray-sbi_ecall_exts-y += ecall_pmu
-carray-sbi_ecall_exts-y += ecall_legacy
-carray-sbi_ecall_exts-y += ecall_vendor
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_HSM) += ecall_hsm
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_SRST) += ecall_srst
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_PMU) += ecall_pmu
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_LEGACY) += ecall_legacy
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_VENDOR) += ecall_vendor
 
 libsbi-objs-y += sbi_ecall_base.o
-libsbi-objs-y += sbi_ecall_hsm.o
-libsbi-objs-y += sbi_ecall_legacy.o
-libsbi-objs-y += sbi_ecall_pmu.o
+libsbi-objs-$(CONFIG_SBI_ECALL_HSM) += sbi_ecall_hsm.o
+libsbi-objs-$(CONFIG_SBI_ECALL_LEGACY) += sbi_ecall_legacy.o
+libsbi-objs-$(CONFIG_SBI_ECALL_PMU) += sbi_ecall_pmu.o
 libsbi-objs-y += sbi_ecall_replace.o
-libsbi-objs-y += sbi_ecall_vendor.o
+libsbi-objs-$(CONFIG_SBI_ECALL_VENDOR) += sbi_ecall_vendor.o
 
 libsbi-objs-y += sbi_bitmap.o
 libsbi-objs-y += sbi_bitops.o
lib/sbi/sbi_ecall_replace.c +8 -1
index 93f44da..0ea00d6 100644
@@ -19,6 +19,7 @@
 #include <sbi/sbi_tlb.h>
 #include <sbi/sbi_trap.h>
 
+#ifdef CONFIG_SBI_ECALL_TIME
 static int sbi_ecall_time_handler(unsigned long extid, unsigned long funcid,
                                  const struct sbi_trap_regs *regs,
                                  unsigned long *out_val,
@@ -43,7 +44,9 @@ struct sbi_ecall_extension ecall_time = {
        .extid_end = SBI_EXT_TIME,
        .handle = sbi_ecall_time_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_RFENCE
 static int sbi_ecall_rfence_handler(unsigned long extid, unsigned long funcid,
                                    const struct sbi_trap_regs *regs,
                                    unsigned long *out_val,
@@ -113,7 +116,9 @@ struct sbi_ecall_extension ecall_rfence = {
        .extid_end = SBI_EXT_RFENCE,
        .handle = sbi_ecall_rfence_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_IPI
 static int sbi_ecall_ipi_handler(unsigned long extid, unsigned long funcid,
                                 const struct sbi_trap_regs *regs,
                                 unsigned long *out_val,
@@ -134,7 +139,9 @@ struct sbi_ecall_extension ecall_ipi = {
        .extid_end = SBI_EXT_IPI,
        .handle = sbi_ecall_ipi_handler,
 };
+#endif
 
+#ifdef CONFIG_SBI_ECALL_SRST
 static int sbi_ecall_srst_handler(unsigned long extid, unsigned long funcid,
                                  const struct sbi_trap_regs *regs,
                                  unsigned long *out_val,
@@ -194,3 +201,4 @@ struct sbi_ecall_extension ecall_srst = {
        .handle = sbi_ecall_srst_handler,
        .probe = sbi_ecall_srst_probe,
 };
+#endif
--

2.37.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#9 lore raw

cover Add Kconfig option for each extension

In reply to Vivian Wang
Quoted 15 lines · Vivian Wang

This series adds support for disabling SBI extensions via Kconfig. In the future, Kconfig can also be used to disable obsolete extensions by default.

All of the SBI extensions except Base extension is optional [1]. Users of OpenSBI may wish to disable support for some of the extensions for reasons such as:

- Future-proofing S-mode software, replacing SBI calls with new ISA functionality.
- Testing S-mode software to make sure they work correctly without certain extensions present.
- Disabling unused extensions to minimize the attack surface of M-mode software. See [2].

Quoted 3 lines

[1]: https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc#1-introduction
[2]: https://github.com/riscv-non-isa/riscv-sbi-doc/issues/102

For the series