Skip to contentSkip to conversation

Add WorldGuard support with wgchecker2

PATCH Superseded Raymond Mao 2 participants 7 messages Jul 25 – Aug 10 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add WorldGuard support with wgchecker2

From: Raymond Mao <[email protected]>

This series adds WorldGuard support according to the wgchecker2 DT- binding [1] with QEMU virt integration.

The implementation base on prerequisite series [2]:
- reuse sbi_hart_protection for hart-local runtime reconfiguration
- reuse sbi_domain_data for per-domain WorldGuard state
- keep WorldGuard-specific setup and checker programming in platform code

At boot, the generic platform code invokes a QEMU virt WorldGuard setup path which:
- parses wgchecker2 checker instances
- parses resource access-controller entries and programs checker MMIO
- parses per-hart CPU defaults from /cpus
- parses per-domain WorldGuard metadata under hw-isolation

During domain transitions, the WorldGuard hart protection implementation reprograms MLWID and, when available, MWIDDELEG and SLWID.

The series is split as follows:
1. add reusable FDT helpers needed by the WorldGuard parser
2. add WorldGuard CSR definitions and hart extension flags
3. document the current DT layout consumed by the implementation
4. add the wgchecker2 / WorldGuard runtime and QEMU virt integration

[1] dt-bindings: riscv: Add RISC-V Worlds and SiFive WorldGuard DT

    bindings
    https://patchwork.kernel.org/project/linux-riscv/list/?series=1113881
[2] [PATCH 0/5] Extend hart protection abstraction for ID configuration
    https://lore.kernel.org/opensbi/[email protected]/

Raymond Mao (4):
lib: utils: fdt: add generic domain and property parsing helpers
hart: add WorldGuard CSR IDs and hart extension flags
docs: document WorldGuard DT bindings
platform: generic: add WorldGuard support with wgchecker2

 docs/domain_support.md                        | 186 ++++++
 include/sbi/riscv_encoding.h                  |   3 +
 include/sbi/sbi_hart.h                        |   4 +
 include/sbi_utils/fdt/fdt_domain.h            |  13 +
 include/sbi_utils/fdt/fdt_helper.h            |  10 +
 lib/sbi/sbi_hart.c                            |   2 +
 lib/utils/fdt/fdt_domain.c                    |  46 +-
 lib/utils/fdt/fdt_helper.c                    |  76 +++
 .../generic/include/qemu_virt_worldguard.h    |  13 +
 platform/generic/include/wgchecker2.h         |  78 +++
 platform/generic/include/worldguard.h         |  44 ++
 platform/generic/objects.mk                   |   3 +
 platform/generic/platform.c                   |  15 +-
 platform/generic/virt/qemu_virt_worldguard.c  |  25 +
 platform/generic/wgchecker2.c                 | 500 ++++++++++++++++
 platform/generic/worldguard.c                 | 553 ++++++++++++++++++
 16 files changed, 1569 insertions(+), 2 deletions(-)
 create mode 100644 platform/generic/include/qemu_virt_worldguard.h
 create mode 100644 platform/generic/include/wgchecker2.h
 create mode 100644 platform/generic/include/worldguard.h
 create mode 100644 platform/generic/virt/qemu_virt_worldguard.c
 create mode 100644 platform/generic/wgchecker2.c
 create mode 100644 platform/generic/worldguard.c
Signature · 6 lines
2.25.1

-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

PATCH v2 1/4 lib: utils: fdt: add generic domain and property parsing helpers

From: Raymond Mao <[email protected]>

Add reusable FDT helpers for domain-oriented and other FDT-based consumers.

This adds helpers to:
- find a domain instance DT node by exact name match
- parse a single u32 property
- parse a single u64 property
- parse a u32 property list into a bitmask

Also tighten the internal domain-name matching to use exact node-name matching.

---
 include/sbi_utils/fdt/fdt_domain.h | 13 +++++
 include/sbi_utils/fdt/fdt_helper.h | 10 ++++
 lib/utils/fdt/fdt_domain.c         | 46 +++++++++++++++++-
 lib/utils/fdt/fdt_helper.c         | 76 ++++++++++++++++++++++++++++++
 4 files changed, 144 insertions(+), 1 deletion(-)
include/sbi_utils/fdt/fdt_domain.h +13 -0
index 8c2dee09..60d51d6c 100644
@@ -11,12 +11,18 @@
 #ifndef __FDT_DOMAIN_H__
 #define __FDT_DOMAIN_H__
 
+#include <sbi/sbi_error.h>
 #include <sbi/sbi_types.h>
 
 #ifdef CONFIG_FDT_DOMAIN
 
 struct sbi_domain;
 
+struct fdt_find_domain_offset_info {
+	const char *name;
+	int domain_offset;
+};
+
 /**
  * Iterate over each domains in device tree
  *
@@ -45,6 +51,8 @@ int fdt_iterate_each_memregion(void *fdt, int domain_offset, void *opaque,
 					 int region_offset, u32 region_access,
 					 void *opaque));
 
+int fdt_find_domain_offset(const void *fdt, const struct sbi_domain *dom);
+
 /**
  * Fix up the domain configuration in the device tree
  *
@@ -76,6 +84,11 @@ int fdt_domains_populate(const void *fdt);
 
 static inline void fdt_domain_fixup(void *fdt) { }
 static inline int fdt_domains_populate(const void *fdt) { return 0; }
+static inline int fdt_find_domain_offset(const void *fdt,
+					 const struct sbi_domain *dom)
+{
+	return SBI_ENOENT;
+}
 
 #endif
 
include/sbi_utils/fdt/fdt_helper.h +10 -0
index 75a564d1..05ca9f79 100644
@@ -50,6 +50,16 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_u32(const void *fdt, int nodeoff, const char *prop_name,
+		  u32 *out_val);
+
+int fdt_parse_u64(const void *fdt, int nodeoff, const char *prop_name,
+		  u64 *out_val);
+
+int fdt_parse_u32_array_bitmask(const void *fdt, int nodeoff,
+				const char *prop_name, u32 max_bits,
+				u32 *out_mask);
+
 int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/utils/fdt/fdt_domain.c +45 -1

59 lines not on this page · raw message

lib/utils/fdt/fdt_helper.c +76 -0

82 lines not on this page · raw message

Signature · 7 lines
2.25.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

PATCH v2 2/4 hart: add WorldGuard CSR IDs and hart extension flags

In reply to Raymond Mao

From: Raymond Mao <[email protected]>

Define the WorldGuard-related CSR numbers and claim smwg / sswg hart extensions so platform code can probe support before programming MLWID, MWIDDELEG and SLWID.

---
 include/sbi/riscv_encoding.h | 3 +++
 include/sbi/sbi_hart.h       | 4 ++++
 lib/sbi/sbi_hart.c           | 2 ++
 3 files changed, 9 insertions(+)
include/sbi/riscv_encoding.h +3 -0
index ffe7666c..4d8b2fba 100644
@@ -373,6 +373,7 @@
 
 /* Supervisor Protection and Translation */
 #define CSR_SATP			0x180
+#define CSR_SLWID			0x190
 
 /* Supervisor Indirect Register Alias */
 #define CSR_SISELECT			0x150
@@ -510,6 +511,7 @@
 /* Machine Configuration */
 #define CSR_MENVCFG			0x30a
 #define CSR_MENVCFGH			0x31a
+#define CSR_MLWID			0x390
 
 /* Machine Trap Handling */
 #define CSR_MSCRATCH			0x340
@@ -736,6 +738,7 @@
 /* Machine Security Configuration CSR (mseccfg) */
 #define CSR_MSECCFG			0x747
 #define CSR_MSECCFGH			0x757
+#define CSR_MWIDDELEG			0x748
 
 #define MSECCFG_MML_SHIFT		(0)
 #define MSECCFG_MML			(_UL(1) << MSECCFG_MML_SHIFT)
include/sbi/sbi_hart.h +4 -0
index 543393bb..0fe8d439 100644
@@ -63,6 +63,10 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SSCSRIND,
 	/** Hart has Ssccfg extension */
 	SBI_HART_EXT_SSCCFG,
+	/** Hart has Smwg extension */
+	SBI_HART_EXT_SMWG,
+	/** Hart has Sswg extension */
+	SBI_HART_EXT_SSWG,
 	/** Hart has Svade extension */
 	SBI_HART_EXT_SVADE,
 	/** Hart has Svadu extension */
lib/sbi/sbi_hart.c +2 -0
index 21713816..2b7b9cdd 100644
@@ -386,6 +386,8 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(smcdeleg, SBI_HART_EXT_SMCDELEG),
 	__SBI_HART_EXT_DATA(sscsrind, SBI_HART_EXT_SSCSRIND),
 	__SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
+	__SBI_HART_EXT_DATA(smwg, SBI_HART_EXT_SMWG),
+	__SBI_HART_EXT_DATA(sswg, SBI_HART_EXT_SSWG),
 	__SBI_HART_EXT_DATA(svade, SBI_HART_EXT_SVADE),
 	__SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
 	__SBI_HART_EXT_DATA(smnpm, SBI_HART_EXT_SMNPM),
Signature · 7 lines
2.25.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw

PATCH v2 3/4 docs: document WorldGuard DT bindings

In reply to Raymond Mao

From: Raymond Mao <[email protected]>

Document the WorldGuard device-tree metadata used by the current hart protection runtime flow on WorldGuard-enabled platforms.

---
 docs/domain_support.md | 186 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 186 insertions(+)
docs/domain_support.md +186 -0
index e267a9f7..596d4dae 100644
@@ -204,6 +204,192 @@ The DT properties of a domain instance DT node are as follows:
   whether the domain instance is allowed to do system reset.
 * **system-suspend-allowed** (Optional) - A boolean flag representing
   whether the domain instance is allowed to do system suspend.
+* **hw-isolation** (Optional) - A child node used by platform-specific
+  isolation mechanisms. The current OpenSBI WorldGuard implementation uses
+  this node only as a container for per-domain WorldGuard execution metadata.
+
+WorldGuard Device Tree Binding
+------------------------------
+
+The current OpenSBI WorldGuard support is built from:
+
+* **sbi_hart_protection** for hart-local runtime reconfiguration during
+  domain transitions
+* **sbi_domain_data** for per-domain WorldGuard state
+* platform-specific setup code that parses and programs WorldGuard checkers
+  before domains are populated
+
+The WorldGuard-specific DT state is split across two places:
+
+* under a domain instance node, for per-domain execution metadata
+* in the normal system DT topology, for CPU defaults and checker policy
+
+### Domain-local WorldGuard Metadata
+
+The optional **hw-isolation** child node under a domain instance may contain
+one WorldGuard child node:
+
+* **compatible = "sifive,wgchecker2"**
+* **worldguard,wid** - Machine world ID selected when entering the domain
+* **worldguard,widlist** - List of world IDs delegated to the domain
+
+The current implementation looks up the domain node in the FDT, finds its
+**hw-isolation** child, and parses the first child node compatible with
+**sifive,wgchecker2**.
+
+### System-level WorldGuard Metadata
+
+The current implementation also parses the following system-level DT state:
+
+* **/cpus**
+  * **riscv,nworlds** - Total number of supported worlds
+  * **sifive,trustedwid** (Optional) - Default trusted machine world ID.
+    If omitted, the highest valid world ID is used.
+* **/cpus/cpu@X**
+  * **riscv,pmwid** - Default machine world ID for the hart
+  * **riscv,pmwidlist** - Machine-visible world IDs allowed for the hart
+  * **riscv,pmlwidlist** - World IDs that may be delegated by the hart
+* **sifive,wgchecker2** nodes
+  * **reg** - Checker MMIO base/size
+  * **#access-controller-cells** - Must match the expected checker cell count
+* protected resource nodes with **access-controllers** properties that point
+  to a checker and encode protected address range, permission bitmap, and slot
+  configuration data
+
+For the current implementation, checker policy is parsed from
+**access-controllers** references on protected resources and applied during
+platform-specific WorldGuard setup.
+
+### Runtime Behavior
+
+At boot:
+
+* platform setup detects whether checker programming and/or runtime WID
+  switching are present in the DT
+* if runtime WID support is present, OpenSBI registers a WorldGuard
+  **sbi_hart_protection** mechanism and a **sbi_domain_data** provider
+* per-hart default WID state is parsed from **/cpus**
+* checker instances are parsed, validated, and programmed before domain
+  population completes
+
+During a domain transition:
+
+* the WorldGuard **sbi_domain_data** provider supplies per-domain
+  **worldguard,wid** and **worldguard,widlist** state
+* the WorldGuard **sbi_hart_protection** implementation reprograms
+  **MLWID**
+* when **sswg** is present, it also reprograms **MWIDDELEG** and **SLWID**
+* when a domain is quiesced, the hart is returned to its per-hart fallback
+  machine world state
+
+Runtime WID switching is enabled only when the CPU runtime properties are
+present. A DT that only describes checker hardware does not, by itself,
+enable hart-local WorldGuard reconfiguration.
+
+WorldGuard Examples
+-------------------
+
+Domain instance with WorldGuard execution metadata:
+
+```text
+    chosen {
+        opensbi-domains {
+            compatible = "opensbi,domain,config";
+
+            example_domain: domain@1 {
+                compatible = "opensbi,domain,instance";
+                possible-harts = <&cpu2>;
+                regions = <&mem0 0x3f>;
+                boot-hart = <&cpu2>;
+                next-addr = <0x00000000 0x80200000>;
+                next-mode = <0x1>;
+
+                hw-isolation {
+                    worldguard {
+                        compatible = "sifive,wgchecker2";
+                        worldguard,wid = <0x0 0x1>;
+                        worldguard,widlist = <1 3>;
+                    };
+                };
+            };
+        };
+    };
+```
+
+CPU default state, checker, and protected resource example:
+
+```text
+    cpus {
+        riscv,nworlds = <4>;
+        sifive,trustedwid = <3>;
+
+        cpu0: cpu@0 {
+            reg = <0>;
+            riscv,pmwid = <0x0 0x3>;
+            riscv,pmwidlist = <0x0 0xb>;
+            riscv,pmlwidlist = <0x0 0xb>;
+        };
+    };
+
+    memory0: memory@80000000 {
+        reg = <0x0 0x80000000 0x0 0x80000000>;
+        access-controllers =
+            <0x100
+             0x00000000 0x80000000 0x00000000 0x40000000
+             0x00000000 0x000000cf 0x0f>,
+            <0x100
+             0x00000000 0xc0000000 0x00000000 0x01000000
+             0x00000000 0x000000cc 0x0f>,
+            <0x100
+             0x00000000 0xc1000000 0x00000000 0x3f000000
+             0x00000000 0x000000cf 0x0f>;
+    };
+
+    flash0: flash@20000000 {
+        reg = <0x0 0x20000000 0x0 0x04000000>;
+        access-controllers =
+            <0x101
+             0x00000000 0x20000000 0x00000000 0x04000000
+             0x00000000 0x000000c3 0x0f>;
+    };
+
+    uart0: serial@10000000 {
+        reg = <0x0 0x10000000 0x0 0x00001000>;
+        access-controllers =
+            <0x102
+             0x00000000 0x10000000 0x00000000 0x00001000
+             0x00000000 0x000000c0 0x0f>;
+    };
+
+    wgchecker0: wgchecker@6000000 {
+        compatible = "sifive,wgchecker2";
+        reg = <0x0 0x06000000 0x0 0x1000>;
+        #access-controller-cells = <7>;
+        phandle = <0x100>;
+    };
+
+    wgchecker1: wgchecker@6001000 {
+        compatible = "sifive,wgchecker2";
+        reg = <0x0 0x06001000 0x0 0x1000>;
+        #access-controller-cells = <7>;
+        phandle = <0x101>;
+    };
+
+    wgchecker2: wgchecker@6002000 {
+        compatible = "sifive,wgchecker2";
+        reg = <0x0 0x06002000 0x0 0x1000>;
+        #access-controller-cells = <7>;
+        phandle = <0x102>;
+    };
+```
+
+The test overlay used in this tree is at:
+
+* **platform/generic/virt/qemu-virt-wg-overlay.dts**
+
+That overlay supplies the current QEMU virt test metadata for OpenSBI
+domains and protected resources. The base DTB must still provide the checker
+nodes and the CPU-level WorldGuard properties consumed by the runtime code.
 
 ### Assigning HART To Domain Instance
 
Signature · 7 lines
2.25.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH v2 4/4 platform: generic: add WorldGuard support with wgchecker2

In reply to Raymond Mao

From: Raymond Mao <[email protected]>

Add WorldGuard support for the hart protection runtime on the generic platform.

Implement boot-time parsing of sifive,wgchecker2 checker instances and resource access-controller entries from the FDT, then program checker MMIO state according to the described protected ranges, permissions, and slot configuration.

Add WorldGuard runtime support to parse per-hart CPU defaults from /cpus and per-domain WorldGuard metadata under domain hw-isolation nodes, and use that state to reprogram MLWID, MWIDDELEG, and SLWID during domain transitions.

Keep the QEMU virt platform code as the integration layer that invokes WorldGuard setup for the virt platform.

---
 .../generic/include/qemu_virt_worldguard.h    |  13 +
 platform/generic/include/wgchecker2.h         |  78 +++
 platform/generic/include/worldguard.h         |  44 ++
 platform/generic/objects.mk                   |   3 +
 platform/generic/platform.c                   |  15 +-
 platform/generic/virt/qemu_virt_worldguard.c  |  25 +
 platform/generic/wgchecker2.c                 | 500 ++++++++++++++++
 platform/generic/worldguard.c                 | 553 ++++++++++++++++++
 8 files changed, 1230 insertions(+), 1 deletion(-)
 create mode 100644 platform/generic/include/qemu_virt_worldguard.h
 create mode 100644 platform/generic/include/wgchecker2.h
 create mode 100644 platform/generic/include/worldguard.h
 create mode 100644 platform/generic/virt/qemu_virt_worldguard.c
 create mode 100644 platform/generic/wgchecker2.c
 create mode 100644 platform/generic/worldguard.c
platform/generic/include/qemu_virt_worldguard.h new file +13 -0
new file mode 100644
index 00000000..8db30a24
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 RISCstar Solutions Corporation.
+ *
+ * Author: Raymond Mao <[email protected]>
+ */
+
+#ifndef __PLATFORM_GENERIC_QEMU_VIRT_WORLDGUARD_H__
+#define __PLATFORM_GENERIC_QEMU_VIRT_WORLDGUARD_H__
+
+int qemu_virt_worldguard_setup(const void *fdt);
+
+#endif
platform/generic/include/wgchecker2.h new file +78 -0

78 lines not on this page · raw message

platform/generic/include/worldguard.h new file +44 -0

44 lines not on this page · raw message

platform/generic/objects.mk +3 -0
index ca7fb8b7..9df03e87 100644
@@ -20,6 +20,9 @@ platform-runcmd = qemu-system-riscv$(PLATFORM_RISCV_XLEN) -M virt -m 256M \
 # Objects to build
 platform-objs-y += platform.o
 platform-objs-y += platform_override_modules.carray.o
+platform-objs-y += worldguard.o
+platform-objs-y += wgchecker2.o
+platform-objs-y += virt/qemu_virt_worldguard.o
 
 # Blobs to build
 FW_DYNAMIC=y
platform/generic/platform.c +14 -1
index 1df0280d..ed56ab0b 100644
@@ -11,6 +11,8 @@
 #include <platform_override.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
 #include <sbi/sbi_hartmask.h>
 #include <sbi/sbi_heap.h>
 #include <sbi/sbi_platform.h>
@@ -29,6 +31,7 @@
 #include <sbi_utils/serial/fdt_serial.h>
 #include <sbi_utils/serial/semihosting.h>
 #include <sbi_utils/timer/fdt_timer.h>
+#include <qemu_virt_worldguard.h>
 
 /* List of platform override modules generated at compile time */
 extern const struct fdt_driver *const platform_override_modules[];
@@ -267,9 +270,19 @@ int generic_domains_init(void)
 	const void *fdt = fdt_get_address();
 	int offset, ret;
 
+	ret = qemu_virt_worldguard_setup(fdt);
+	if (ret < 0) {
+		sbi_printf("%s: qemu_virt_worldguard_setup failed (error %d)\n",
+			   __func__, ret);
+		return ret;
+	}
+
 	ret = fdt_domains_populate(fdt);
-	if (ret < 0)
+	if (ret < 0) {
+		sbi_printf("%s: fdt_domains_populate failed (error %d)\n",
+			   __func__, ret);
 		return ret;
+	}
 
 	offset = fdt_path_offset(fdt, "/chosen");
 
platform/generic/virt/qemu_virt_worldguard.c new file +25 -0
new file mode 100644
index 00000000..f376d5a2
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * QEMU virt WorldGuard registration shim
+ *
+ * Copyright (c) 2026 RISCstar Solutions Corporation.
+ *
+ * Author: Raymond Mao <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <qemu_virt_worldguard.h>
+#include <sbi/sbi_error.h>
+#include <worldguard.h>
+
+int qemu_virt_worldguard_setup(const void *fdt)
+{
+	if (!fdt)
+		return 0;
+
+	if (fdt_node_check_compatible(fdt, 0, "riscv-virtio") &&
+	    fdt_node_check_compatible(fdt, 0, "qemu,virt"))
+		return 0;
+
+	return worldguard_setup(fdt);
+}
platform/generic/wgchecker2.c new file +500 -0

500 lines not on this page · raw message

platform/generic/worldguard.c new file +553 -0

553 lines not on this page · raw message

Signature · 7 lines
2.25.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#5 lore raw
In reply to Raymond Mao

Hi All,

As discussed with Raymond today, the ISA part of this series will be addressed in my next version of [RFC,00/12] Add RISC-V Worlds ISA support to OpenSBI [1] series, and wgChecker driver will also need to align with the new version of DT bindings.

Given these changes, let's skip reviewing this series for now.

Thanks,
Peter Lin

[1] https://patchwork.ozlabs.org/project/opensbi/cover/[email protected]/

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#6 lore raw

Hi Peter,

Quoted 11 lines · Yu-Chien Peter Lin

Hi All,

As discussed with Raymond today, the ISA part of this series will be addressed in my next version of [RFC,00/12] Add RISC-V Worlds ISA support to OpenSBI [1] series, and wgChecker driver will also need to align with the new version of DT bindings.

Given these changes, let's skip reviewing this series for now.

No worries, will send a v3 shortly to align with the v2 DT-bindings.

Raymond

Quoted 4 lines

Thanks,
Peter Lin

[1] https://patchwork.ozlabs.org/project/opensbi/cover/[email protected]/

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi