Add Smsdid and Smmpt supervisor domain protection
v5 cover Add Smsdid and Smmpt supervisor domain protection
This series adds OpenSBI support for the RISC-V SMMPT specification sub-extensions - Smsdid (supervisor domain ID) and
Smmpt (supervisor domain memory protection).
Together these let M-mode firmware confine each supervisor domain to a physical address space that is enforced in hardware, via a per domain memory protection table (MPT) selected by the mmpt CSR.
Every supervisor domain gets its own MPT built from the memory regions of its SBI domain. Per-region XWR permissions are derived from the region's S/U access flags from SBI domain, so a region that does not have S/U access is mapped as an deny instead of skipped.
The Root domain maps all physical memory, with the firmware region explicitly denied to S-mode. Regions may be shared between domains, each side keeping its own permissions.
NOTE: This series is based on another unmerged series which is present here:
https://lore.kernel.org/all/[email protected]/
This version of series is based on another series by Pawandeep which adds support for the domain notifier infrastructure.
https://lore.kernel.org/opensbi/[email protected]/
Testing:
Booted Linux when Smmpt was active. The boot dump shows the expected per region permissions. Also checked with a small self-test in Qemu which confirms the firmware region denied and the kernel region permitted from S-mode.
The Qemu RISC-V Virt machine was used to test this. The new cli args required to be passed to Qemu are -
./qemu-system-riscv64 -nographic -M virt -cpu rv64,x-smsdid=true,x-smmpt=true
or
./qemu-system-riscv64 -nographic -M virt -cpu rv64,smepmp=true,x-smsdid=true,x-smmpt=true
Boot Logs:
NOTE: The below logs are due the sbi_mpt_dump() function which is present but not called. Its kept for debugging purposes.
sbi_mpt: mode=Smmpt52 SDIDLEN=6 max_domains=64
root_size=0x1000 root_align=0x1000
fw 0x0000000080000000+0x19e000 pool=global heap SDID 0 root=0x0000000080098000 regions=9 dom=root
S-Domain0 Region01 : 0x0000000080000000+0x80000 : xwr=0 (---)
S-Domain0 Region02 : 0x0000000080000000+0x200000 : xwr=0 (---)
S-Domain0 Region03 : 0x0000000000100000+0x1000 : xwr=3 (RW-)
S-Domain0 Region04 : 0x0000000010000000+0x1000 : xwr=3 (RW-)
S-Domain0 Region05 : 0x0000000002000000+0x10000 : xwr=0 (---)
S-Domain0 Region06 : 0x000000000c400000+0x200000 : xwr=3 (RW-)
S-Domain0 Region07 : 0x000000000c000000+0x400000 : xwr=3 (RW-)
S-Domain0 Region08 : 0x0000000080000000+0x19e000 : xwr=0 (---) [LOCKED]
S-Domain0 Default : Rest of the address space : xwr=7 (RWX) [baseline, overlay by regions above]
Boot HART ID : 0
Boot HART Domain : root
Boot HART Priv Version : v1.12
Boot HART Base ISA : rv64imafdch
Boot HART ISA Extensions : sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu,f,d,smsdid,smmpt
Boot HART PMP Count : 16
Boot HART PMP Granularity : 2 bits
Boot HART PMP Address Bits : 54
Boot HART MHPM Info : 16 (0x0007fff8)
Boot HART Debug Triggers : 2 triggers
Boot HART MIDELEG : 0x0000000000001666
Boot HART MEDELEG : 0x0000000000f4b509
The corresponding Qemu Smmpt support from LIU Zhiwei
https://patchew.org/QEMU/[email protected]/
This is part of collaboration work for RISC-V SMMTT development. More details are here:
https://lists.gnu.org/archive/html/qemu-riscv/2026-07/msg01018.html
These OpenSBI changes are also present in github -
https://github.com/pathakraul/opensbi/tree/rpathak_smmpt_v5
Further Development:
This series add base infrastructure for Smsdid and Smmpt. Development and testing with more then one Supervisor Domain. Extensive testing with all the modes and other platform configurations.
v4 -> v5:
1. Drop the state_finalize callback on sbi_domain_state and use domain finalize notifier built on the registration notifier infrastructure instead.
2. Remove stray text in copyright header in sbi_hart.c.
v3 -> v4:
1. Drop the map_range/unmap_range rename patch from this series.
2. Move sbi_mpt_init() function out of the platform.c and make it part of the sbi hart protection mechanism.
3. Drop mpt_early_init() and mpt_final_init() functions. The SMMPT core registers the sbi domain state callbacks for initialization, activation and cleanup.
4. Add state_finalize callback in for per domain state finalization.
5. Address the review comments like typos and add relevant macros instead of using numbers for clarity.
6. Remove the sbi_mpt_dump() function call.
v2 -> v3:
1. Adapt to sbi hart protection abstraction.
2. Add missing sbi_mpt_hart_deactivate() decl in sbi_mpt.h.
3. Rename the *map_range and *unmap_range set of functions in sbi_hart_protection.
4. Update the copyright string.
v1 -> v2:
1. Rebase to latest master
2. Change probe order between mode and sdid.
3. Add sbi_mpt_query_access() function.
4. Formatting changes.
Rahul Pathak (4):
lib: sbi_domain: Add domain register finalize notifier
riscv: Add Smsdid and Smmpt hart extensions
mpt: Add Smsdid and Smmpt supervisor domain core
lib: sbi: Initialize SMMPT during coldboot
include/sbi/sbi_domain.h | 16 + include/sbi/sbi_hart.h | 4 + include/sbi/sbi_hart_mpt.h | 312 +++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain.c | 80 +++ lib/sbi/sbi_hart.c | 8 + lib/sbi/sbi_hart_mpt.c | 1007 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt.c | 1000 ++++++++++++++++++++++++++++++++++ platform/generic/platform.c | 11 + 9 files changed, 2440 insertions(+) create mode 100644 include/sbi/sbi_hart_mpt.h create mode 100644 lib/sbi/sbi_hart_mpt.c create mode 100644 lib/sbi/sbi_mpt.c
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH v5 1/4 lib: sbi_domain: Add domain register finalize notifier
sbi domain registeration notifier is invoked when memory regions of the domains are not finalized. The root domain is registered early and the drivers keep adding the regions until sbi_domain_finalize() is called. Such regions are added to the root after root domain is already registered and notification for that has already been sent.
Per-domain configuration which is based on the final memory regions of the domains especially the root domain is not possible only with a domain register notifier.
Add a domain finalize notifier for such case which is called for every domain once all the domains are already registered and their memory regions are finalized.
--- include/sbi/sbi_domain.h | 16 ++++++++ lib/sbi/sbi_domain.c | 80 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+)
include/sbi/sbi_domain.h +16 -0
@@ -362,10 +362,26 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid);typedef void (*sbi_domain_notifier_fn )(const struct sbi_domain *dom,void *priv); +/** +* Domain finalize notifier callback +* +* Invoked from sbi_domain_finalize function after every domain gets +* registered and memory regions of all domains are finalized. +*/ +typedef int (*sbi_domain_finalize_notifier_fn )(struct sbi_domain *dom,void *priv); +/** Regitser domain notifier */ int sbi_domain_register_notifier (sbi_domain_notifier_fn notifier,void *priv);/** Unregitser domain notifier */ int sbi_domain_unregister_notifier (sbi_domain_notifier_fn notifier,void *priv); +/** Register domain finalize notifier */ +int sbi_domain_register_finalize_notifier (sbi_domain_finalize_notifier_fn notifier, +void *priv); + +/** Unregister domain finalize notifier */ +int sbi_domain_unregister_finalize_notifier (sbi_domain_finalize_notifier_fn notifier, +void *priv); +#endif
lib/sbi/sbi_domain.c +80 -0
100 lines not on this page · raw message
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v5 1/4] lib: sbi_domain: Add domain register finalize notifier
Quoted 12 lines · Rahul Pathak
sbi domain registeration notifier is invoked when memory regions of the domains are not finalized. The root domain is registered early and the drivers keep adding the regions until
sbi_domain_finalize()is called. Such regions are added to the root after root domain is already registered and notification for that has already been sent.Per-domain configuration which is based on the final memory regions of the domains especially the root domain is not possible only with a domain register notifier.
Quoted 5 lines
Add a domain finalize notifier for such case which is called for every domain once all the domains are already registered and their memory regions are finalized.
Signed-off-by Rahul Pathak <[email protected]>
Clearly, MPT table is per-domain state so why not use sbi_domain_state for this ??
Regards,
Anup
Quoted 151 lines
--- include/sbi/sbi_domain.h | 16 ++++++++ lib/sbi/sbi_domain.c | 80 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+)include/sbi/sbi_domain.h +16
-0@@ -362,10 +362,26 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid);typedef void (*sbi_domain_notifier_fn )(const struct sbi_domain *dom,void *priv); +/** +* Domain finalize notifier callback +* +* Invoked from sbi_domain_finalize function after every domain gets +* registered and memory regions of all domains are finalized. +*/ +typedef int (*sbi_domain_finalize_notifier_fn )(struct sbi_domain *dom,void *priv); +/** Regitser domain notifier */ int sbi_domain_register_notifier (sbi_domain_notifier_fn notifier,void *priv);/** Unregitser domain notifier */ int sbi_domain_unregister_notifier (sbi_domain_notifier_fn notifier,void *priv); +/** Register domain finalize notifier */ +int sbi_domain_register_finalize_notifier (sbi_domain_finalize_notifier_fn notifier, +void *priv); + +/** Unregister domain finalize notifier */ +int sbi_domain_unregister_finalize_notifier (sbi_domain_finalize_notifier_fn notifier, +void *priv); +#endif lib/sbi/sbi_domain.c +80
-1@@ -42,8 +42,15 @@ struct sbi_domain_notifier_entry { }; +struct sbi_domain_finalize_notifier_entry { +struct sbi_dlist node; +sbi_domain_finalize_notifier_fn fn; +void *priv; +}; +static unsigned long domain_hart_ptr_offset;static SBI_LIST_HEAD (domain_notifier_list); +static SBI_LIST_HEAD (domain_finalize_notifier_list);int sbi_domain_register_notifier (sbi_domain_notifier_fn notifier,void *priv) {@@ -95,6 +102,67 @@ void sbi_domain_notify_all(const struct sbi_domain *dom) } } +int sbi_domain_register_finalize_notifier (sbi_domain_finalize_notifier_fn notifier,void *priv) +{ +struct sbi_domain_finalize_notifier_entry *entry; + +if (!notifier) +return SBI_EINVAL ; + +sbi_list_for_each_entry (entry, &domain_finalize_notifier_list, node) { +if (entry->fn == notifier && entry->priv == priv) +return SBI_OK ; + } + + entry =sbi_zalloc (sizeof (*entry)); +if (!entry) +return SBI_ENOMEM ; + + entry->fn = notifier; + entry->priv = priv; +sbi_list_add_tail (&entry->node, &domain_finalize_notifier_list); + +return SBI_OK ; +} + +int sbi_domain_unregister_finalize_notifier (sbi_domain_finalize_notifier_fn notifier,void *priv) +{ +struct sbi_domain_finalize_notifier_entry *entry, *tmp; + +if (!notifier) +return SBI_EINVAL ; + +sbi_list_for_each_entry_safe (entry, tmp, &domain_finalize_notifier_list, node) { +if (entry->fn == notifier && entry->priv == priv) { +sbi_list_del (&entry->node); +sbi_free (entry); +return SBI_OK ; + } + } + +return SBI_ENODEV ; +} + +static int sbi_domain_notify_finalize_all (void ) +{ +struct sbi_domain_finalize_notifier_entry *entry; +struct sbi_domain *dom; +int rc; + +sbi_list_for_each_entry (entry, &domain_finalize_notifier_list, node) { +if (!entry->fn) +continue ; + +sbi_domain_for_each (dom) { + rc = entry->fn (dom, entry->priv); +if (rc) +return rc; + } + } + +return 0 ; +} +struct sbi_domain *sbi_hartindex_to_domain (u32 hartindex) {struct sbi_scratch *scratch;@@ -924,6 +992,18 @@ int sbi_domain_finalize(struct sbi_scratch *scratch)*/ domain_finalized = true; +/* +* All domains are registered and their memory regions are final +* so notify the subsystems which derive per-domain configuration +* from the domain memory regions. +*/ + rc =sbi_domain_notify_finalize_all (); +if (rc) { +sbi_printf ("%s: domain finalize notifier failed (error %d)\n" , + __func__, rc); +return rc; + } +return 0 ; } --2.53.0
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v5 1/4] lib: sbi_domain: Add domain register finalize notifier
Quoted 15 lines · Anup Patel
Quoted 12 lines · Rahul Pathak
sbi domain registeration notifier is invoked when memory regions of the domains are not finalized. The root domain is registered early and the drivers keep adding the regions until
sbi_domain_finalize()is called. Such regions are added to the root after root domain is already registered and notification for that has already been sent.Per-domain configuration which is based on the final memory regions of the domains especially the root domain is not possible only with a domain register notifier.
Quoted 9 lines
Quoted 5 lines
Add a domain finalize notifier for such case which is called for every domain once all the domains are already registered and their memory regions are finalized.
Signed-off-by Rahul Pathak <[email protected]>Clearly, MPT table is per-domain state so why not use
sbi_domain_statefor this ??
The v4 had used sbi_domain_state only for this
https://lists.infradead.org/pipermail/opensbi/2026-August/010446.html
but as suggested by Pawandeep to reuse the inflight notifier support for domain events, I dropped that to use a notifier event.
https://lists.infradead.org/pipermail/opensbi/2026-August/010454.html
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v5 1/4] lib: sbi_domain: Add domain register finalize notifier
Quoted 27 lines · Rahul Pathak
Quoted 15 lines · Anup Patel
Quoted 12 lines · Rahul Pathak
sbi domain registeration notifier is invoked when memory regions of the domains are not finalized. The root domain is registered early and the drivers keep adding the regions until
sbi_domain_finalize()is called. Such regions are added to the root after root domain is already registered and notification for that has already been sent.Per-domain configuration which is based on the final memory regions of the domains especially the root domain is not possible only with a domain register notifier.
Quoted 9 lines
Quoted 5 lines
Add a domain finalize notifier for such case which is called for every domain once all the domains are already registered and their memory regions are finalized.
Signed-off-by Rahul Pathak <[email protected]>Clearly, MPT table is per-domain state so why not use
sbi_domain_statefor this ??
Quoted 7 lines
The v4 had used
sbi_domain_stateonly for this
https://lists.infradead.org/pipermail/opensbi/2026-August/010446.htmlbut as suggested by Pawandeep to reuse the inflight notifier support for domain events, I dropped that to use a notifier event.
https://lists.infradead.org/pipermail/opensbi/2026-August/010454.html
Actually, both notifier and sbi_domain_state will work in this case but sbi_domain_state should be preferred because smmpt does create a per-domain state which is the MPT table.
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v5 1/4] lib: sbi_domain: Add domain register finalize notifier
Quoted 36 lines · Anup Patel
Quoted 33 lines · Rahul Pathak
Quoted 15 lines · Anup Patel
Quoted 12 lines · Rahul Pathak
sbi domain registeration notifier is invoked when memory regions of the domains are not finalized. The root domain is registered early and the drivers keep adding the regions until
sbi_domain_finalize()is called. Such regions are added to the root after root domain is already registered and notification for that has already been sent.Per-domain configuration which is based on the final memory regions of the domains especially the root domain is not possible only with a domain register notifier.
Quoted 9 lines
Quoted 5 lines
Add a domain finalize notifier for such case which is called for every domain once all the domains are already registered and their memory regions are finalized.
Signed-off-by Rahul Pathak <[email protected]>Clearly, MPT table is per-domain state so why not use
sbi_domain_statefor this ??The v4 had used
sbi_domain_stateonly for this
https://lists.infradead.org/pipermail/opensbi/2026-August/010446.htmlbut as suggested by Pawandeep to reuse the inflight notifier support for domain events, I dropped that to use a notifier event.
Quoted 7 lines
Actually, both notifier and
sbi_domain_statewill work in this case butsbi_domain_stateshould be preferred because smmpt does create a per-domain state which is the MPT table.
Sure, will revert to the sbi_domain_state way from v4
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH v5 2/4 riscv: Add Smsdid and Smmpt hart extensions
In reply to Rahul PathakAdd hart extension identifiers for the RISC-V Smsdid (supervisor domain ID) and Smmpt (supervisor domain memory protection) extensions.
--- include/sbi/sbi_hart.h | 4 ++++ lib/sbi/sbi_hart.c | 2 ++ 2 files changed, 6 insertions(+)
include/sbi/sbi_hart.h +4 -0
@@ -95,6 +95,10 @@ enum sbi_hart_extensions {SBI_HART_EXT_F ,/** Hart has D extension */ SBI_HART_EXT_D , +/** Hart has SMMTT - SMSDID extension */ +SBI_HART_EXT_SMSDID , +/** Hart has SMMTT - SMMPT extension */ +SBI_HART_EXT_SMMPT ,/** Maximum index of Hart extension */ SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +2 -0
@@ -360,6 +360,8 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (v,SBI_HART_EXT_V ),__SBI_HART_EXT_DATA (f,SBI_HART_EXT_F ),__SBI_HART_EXT_DATA (d,SBI_HART_EXT_D ), +__SBI_HART_EXT_DATA (smsdid,SBI_HART_EXT_SMSDID ), +__SBI_HART_EXT_DATA (smmpt,SBI_HART_EXT_SMMPT ), }; _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH v5 3/4 mpt: Add Smsdid and Smmpt supervisor domain core
In reply to Rahul PathakIntroduce the supervisor domain memory-protection (Smmpt) table management core. This adds the mode agnostic core layer and the per mode table walkers/builder.
Add defines for the mmpt CSR encoding, MPTE bit layout, permission flags, structures, fence helpers and the public interfaces.
It implements the RV64 supported modes (Smmpt43/52/64) and the RV32 mode (Smmpt34), its MPT table support.
Also implements mpt core which integrates Smmpt with SBI domains and other core layers.
--- include/sbi/sbi_hart_mpt.h | 312 +++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_hart_mpt.c | 1007 ++++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt.c | 1000 +++++++++++++++++++++++++++++++++++ 4 files changed, 2321 insertions(+) create mode 100644 include/sbi/sbi_hart_mpt.h create mode 100644 lib/sbi/sbi_hart_mpt.c create mode 100644 lib/sbi/sbi_mpt.c
include/sbi/sbi_hart_mpt.h new file +312 -0
@@ -0,0 +1,312 @@ +/* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* +* Authors: Rahul Pathak <[email protected]> +*/ + +#ifndef __SBI_HART_MPT_H__ +#define __SBI_HART_MPT_H__ + +#include <sbi/sbi_types.h> +#include <sbi/riscv_locks.h> +#include <sbi/sbi_scratch.h> +#include <sbi/sbi_bitmap.h> +#include <sbi/riscv_asm.h> + +struct sbi_domain; +struct sbi_mpt_domain; +struct sbi_mpt_ctrl; +struct sbi_domain_memregion; + +/* CSR addresses */ +#define SBI_CSR_MMPT 0x382 +#define SBI_CSR_MSDCFG 0x74E + +/* mmpt register field definitions */ +#if __riscv_xlen == 64 +# define SBI_MMPT_MODE_SHIFT 60UL +# define SBI_MMPT_MODE_MASK (UL(0xF) << SBI_MMPT_MODE_SHIFT) +# define SBI_MMPT_SDID_SHIFT 52UL +# define SBI_MMPT_SDID_MASK (UL(0x3F) << SBI_MMPT_SDID_SHIFT) +# define SBI_MMPT_PPN_MASK UL(0x00000FFFFFFFFFFF) +#else +# define SBI_MMPT_MODE_SHIFT 30UL +# define SBI_MMPT_MODE_MASK (UL(0x3) << SBI_MMPT_MODE_SHIFT) +# define SBI_MMPT_SDID_SHIFT 22UL +# define SBI_MMPT_SDID_MASK (UL(0x3F) << SBI_MMPT_SDID_SHIFT) +# define SBI_MMPT_PPN_MASK UL(0x3FFFFF) +#endif + +#define SBI_MMPT_MODE_BARE 0UL +#if __riscv_xlen == 64 +# define SBI_MMPT_MODE_SMMPT43 1UL +# define SBI_MMPT_MODE_SMMPT52 2UL +# define SBI_MMPT_MODE_SMMPT64 3UL +#else +# define SBI_MMPT_MODE_SMMPT34 1UL +#endif + +/* + * sbi_mmpt_encode() — assemble the full mmpt CSR value. + */ +static inline unsigned long sbi_mmpt_encode(u32 mode, u32 sdid, + unsigned long ppn) +{ + return (((unsigned long)mode << SBI_MMPT_MODE_SHIFT) & SBI_MMPT_MODE_MASK) | + (((unsigned long)sdid << SBI_MMPT_SDID_SHIFT) & SBI_MMPT_SDID_MASK) | + (ppn & SBI_MMPT_PPN_MASK); +} + +/* + * MPTE bit definitions + */ +#define SBI_MPTE_V (1UL << 0) +#define SBI_MPTE_L (1UL << 1) +#define SBI_MPTE_N (1UL << 2) +#define SBI_MPTE_PPN_SHIFT 10U +#define SBI_MPT_PAGE_SHIFT 12U +#define SBI_MPT_PAGE_SIZE 4096UL +#define SBI_MPT_PAGE_MASK (SBI_MPT_PAGE_SIZE - 1UL) +#define SBI_MPTE_XWR_BASE 8U +#define SBI_MPTE_XWR_WIDTH 3U +#define SBI_MPTE_XWR_MASK 7UL + +static inline u32 sbi_mpte_xwr_shift(u32 n) +{ + return (SBI_MPTE_XWR_BASE + n * SBI_MPTE_XWR_WIDTH); +} + +static inline unsigned long sbi_mpte_leaf_set_xwr(unsigned long mpte, + u32 n, u8 xwr) +{ + u32 sh = sbi_mpte_xwr_shift(n); + + mpte &= ~(SBI_MPTE_XWR_MASK << sh); + mpte |= (xwr & SBI_MPTE_XWR_MASK) << sh; + return mpte; +} + +static inline unsigned long sbi_mpte_nonleaf(unsigned long table_pa) +{ + return (((table_pa >> SBI_MPT_PAGE_SHIFT) << SBI_MPTE_PPN_SHIFT) + | SBI_MPTE_V); +} + +static inline unsigned long sbi_mpte_nonleaf_table_pa(unsigned long mpte) +{ + return ((mpte >> SBI_MPTE_PPN_SHIFT) << SBI_MPT_PAGE_SHIFT); +} + +/* + * XWR permission constants (XWR=000 means no access) + */ +#define SBI_MPT_PERM_NONE 0U +#define SBI_MPT_PERM_R 1U +#define SBI_MPT_PERM_W 2U +#define SBI_MPT_PERM_X 4U +#define SBI_MPT_PERM_RW (SBI_MPT_PERM_R | SBI_MPT_PERM_W) +#define SBI_MPT_PERM_RX (SBI_MPT_PERM_R | SBI_MPT_PERM_X) +#define SBI_MPT_PERM_RWX (SBI_MPT_PERM_R | SBI_MPT_PERM_W | SBI_MPT_PERM_X) + +/* + * SDID constants + */ +#define SBI_MPT_SDIDMAX 6U +#define SBI_MPT_MAX_DOMAINS (1UL << SBI_MPT_SDIDMAX) +#define SBI_MPT_SDID_INVALID SBI_MPT_MAX_DOMAINS + +/* + * SBI_MPT_MAX_REGIONS_DOMAIN: Number of regions a domain may have + * Used for tracking of regions + */ +#define SBI_MPT_MAX_REGIONS_DOMAIN 32U + +/* + * SMSDID mfence and minval raw encodings + * + * R-type, opcode=SYSTEM(0x73), funct3=0, rd=x0, rs1=PADDR, rs2=SDID + * mfence.pa funct7=0x19 -> 0x32000073 where rs1=rs2=x0 + * minval.pa funct7=0x1b -> 0x36000073 where rs1=rs2=x0 + * + * .insn r opcode, func3, func7, rd, rs1, rs2 + */ +#define SBI_MPT_MFENCE_PA(rs1, rs2) ".insn r 0x73, 0, 0x19, x0, " rs1 ", " rs2 + +#define SBI_MPT_MINVAL_PA(rs1, rs2) ".insn r 0x73, 0, 0x1b, x0, " rs1 ", " rs2 + +/* + * Fence and invalidation helpers + */ +static inline void sbi_mpt_fence_all(void) +{ + __asm__ volatile(SBI_MPT_MFENCE_PA("x0", "x0") ::: "memory"); +} + +static inline void sbi_mpt_fence_sdid(u32 sdid) +{ + unsigned long s = (unsigned long)sdid & (SBI_MPT_MAX_DOMAINS - 1UL); + + __asm__ volatile(SBI_MPT_MFENCE_PA("x0", "%0") :: "r"(s) : "memory"); +} + +/* + * rs1=paddr and rs2=x0 + */ +static inline void sbi_mpt_mfence_addr(unsigned long paddr) +{ + __asm__ volatile(SBI_MPT_MFENCE_PA("%0", "x0") :: "r"(paddr) : "memory"); +} + +/* + * rs1=paddr and rs2=sdid + */ +static inline void sbi_mpt_mfence_pa(unsigned long paddr, u32 sdid) +{ + unsigned long s = (unsigned long)sdid & (SBI_MPT_MAX_DOMAINS - 1UL); + + __asm__ volatile(SBI_MPT_MFENCE_PA("%0", "%1") + :: "r"(paddr), "r"(s) : "memory"); +} + +/* + * rs1=0 and rs2=0 + */ +static inline void sbi_mpt_minval_all(void) +{ + /* Prior stores globally visible before invalidation */ + __asm__ volatile("sfence.w.inval" ::: "memory"); + __asm__ volatile(SBI_MPT_MINVAL_PA("x0", "x0") ::: "memory"); + /* Invalidation completes before subsequent implicit accesses */ + __asm__ volatile("sfence.inval.ir" ::: "memory"); +} + +/* + * rs1=paddr and rs2=sdid + */ +static inline void sbi_mpt_minval_sdid(unsigned long pa, u32 sdid) +{ + unsigned long s = (unsigned long)sdid & (SBI_MPT_MAX_DOMAINS - 1UL); + + __asm__ volatile("sfence.w.inval" ::: "memory"); + __asm__ volatile(SBI_MPT_MINVAL_PA("%0", "%1") + :: "r"(pa), "r"(s) : "memory"); + __asm__ volatile("sfence.inval.ir" ::: "memory"); +} + +/* + * Core data structures + */ +struct sbi_mpt_region { + unsigned long pa; + unsigned long size; + u8 xwr; + /* region is locked and deny modification via add/remove_region */ + bool locked; + /* mapped into more than one domain */ + bool shared; +}; + +/* + * struct sbi_mpt_mode — SMMPT mode description. + */ +struct sbi_mpt_mode { + const char *name; + u32 mode_val; + + /* Assemble the MMPT CSR value */ + unsigned long (*encode_mmpt)(unsigned long ppn, u32 sdid); + + /* map a range in MPT table */ + int (*map_range)(struct sbi_mpt_domain *dom, + unsigned long pa, unsigned long size, u8 xwr); + + /* number bytes for root table allocation */ + unsigned long (*root_table_size)(void); + + /* required root table physical address alignment */ + unsigned long (*root_table_align)(void); + + /* check if [pa,pa+size] fits in smmpt mode supported address space */ + bool (*pa_in_range)(unsigned long pa, unsigned long size); + + /* map full range of address space which is supported by the mode */ + int (*map_full_range)(struct sbi_mpt_domain *dom, u8 xwr); + + /* read the xwr for a PA */ + u8 (*get_xwr)(struct sbi_mpt_domain *dom, unsigned long pa); +}; + +struct sbi_mpt_domain { + u32 sdid; + bool valid; + unsigned long root_pa; + struct sbi_mpt_mode *mode; + struct sbi_domain *sbi_dom; + unsigned int nregions; + struct sbi_mpt_region regions[SBI_MPT_MAX_REGIONS_DOMAIN]; + spinlock_t lock; +}; + +/* + * struct sbi_mpt_ctrl — RDSM MPT state + */ +struct sbi_mpt_ctrl { + bool ready; + struct sbi_mpt_mode *mode; + /* detected sdidlen from mmpt probe */ + u32 sdid_len; + /* 2^sdid_len — runtime limit for all domain operations */ + u32 max_domains; + unsigned long fw_pa; + unsigned long fw_size; + /* Per domain state */ + struct sbi_mpt_domain *domains[SBI_MPT_MAX_DOMAINS]; + u32 ndomain; + /* sdid offset in scratch */ + unsigned long sdid_offset; + /* bitmap to track assigned/free SDID (bit N set = SDID N free) */ + DECLARE_BITMAP(sdid_bitmap, SBI_MPT_MAX_DOMAINS); +}; + +struct sbi_mpt_ctrl *sbi_mpt_ctrl_get(void); + +static inline u32 sbi_mpt_thishart_sdid(void) +{ + struct sbi_mpt_ctrl *ctrl = sbi_mpt_ctrl_get(); + u32 *p; + + if (!ctrl->ready || !ctrl->sdid_offset) + return (u32)SBI_MPT_SDID_INVALID; + p = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(), + ctrl->sdid_offset); + return *p; +} + +int sbi_mpt_init(void); + +int sbi_mpt_domain_add_region(u32 sdid, unsigned long pa, unsigned long size, u8 xwr); + +int sbi_mpt_domain_remove_region(u32 sdid, unsigned long pa, unsigned long size); + +int sbi_mpt_share_region(u32 src_sdid, u32 dst_sdid, unsigned long pa, unsigned long size, u8 dst_xwr); + +int sbi_mpt_unshare_region(u32 sdid, unsigned long pa, unsigned long size); + +int sbi_mpt_hart_activate(u32 sdid); + +void sbi_mpt_hart_deactivate(void); + +int sbi_mpt_hart_activate_for_domain(struct sbi_domain *sbi_dom); + +struct sbi_mpt_domain *sbi_mpt_domain_get(u32 sdid); + +unsigned long sbi_mpt_pool_alloc(unsigned long size, unsigned long align); + +void sbi_mpt_sdid_free(u32 sdid); + +int sbi_mpt_query_access(u32 sdid, unsigned long pa, u8 *out_xwr); + +void sbi_mpt_dump(void); + +#endif /* __SBI_HART_MPT_H__ */
lib/sbi/objects.mk +2 -0
@@ -108,3 +108,5 @@ libsbi-objs-y += sbi_expected_trap.o libsbi-objs-y += sbi_cppc.o libsbi-objs-$(CC_SUPPORT_VECTOR) += sbi_vector.o libsbi-objs-y += sbi_fp.o +libsbi-objs-y += sbi_hart_mpt.o +libsbi-objs-y += sbi_mpt.o
lib/sbi/sbi_hart_mpt.c new file +1007 -0
1,007 lines not on this page · raw message
lib/sbi/sbi_mpt.c new file +1000 -0
1,000 lines not on this page · raw message
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH v5 4/4 lib: sbi: Initialize SMMPT during coldboot
In reply to Rahul PathakCall sbi_mpt_init() function from coldboot path of sbi_hart_init() to initialize the SMMPT core. It will create and the MPT tables for each SBI domain and maps its memregions with appropriate permissions in MPT tables.
Also each MPT table requires to memory to install tables so reserve more space in heap for MPT tables
--- lib/sbi/sbi_hart.c | 6 ++++++ platform/generic/platform.c | 11 +++++++++++ 2 files changed, 17 insertions(+)
lib/sbi/sbi_hart.c +6 -0
@@ -16,6 +16,7 @@#include <sbi/sbi_csr_detect.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h> +#include <sbi/sbi_hart_mpt.h> #include <sbi/sbi_hart_pmp.h> #include <sbi/sbi_platform.h> #include <sbi/sbi_pmu.h>
@@ -731,6 +732,11 @@ int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot) rc =sbi_hart_pmp_init (scratch);if (rc)return rc; + +/* Smmpt is optional. Continue if the Smmpt is not present. */ + rc =sbi_mpt_init (); +if (rc && rc !=SBI_ENODEV ) +return rc; }return sbi_hart_reinit (scratch);
platform/generic/platform.c +11 -0
@@ -30,6 +30,8 @@#include <sbi_utils/serial/semihosting.h> #include <sbi_utils/timer/fdt_timer.h> +#define SBI_PLATFORM_MPT_HEAP 1048576/* 1 MiB */ +/* List of platform override modules generated at compile time */ extern const struct fdt_driver *const platform_override_modules[];
@@ -42,6 +44,15 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count)/* For TLB fifo */ heap_size +=SBI_TLB_INFO_SIZE * (hart_count) * (hart_count); +/* +* MPT table budget +* 1 MiB memory for MPT allocated currently. +* +* TODO: Need better way to get the memory budget based on active +* SMMPT mode. +*/ + heap_size +=SBI_PLATFORM_MPT_HEAP ; +return BIT_ALIGN (heap_size,HEAP_BASE_ALIGN ); }
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Hi Rahul,
One design point on the SDID assignment.
mpt_state_setup() currently allocates one SDID per SBI domain, at domain registration time, taking the first free bit:
for (sdid = 0; sdid < ctrl->max_domains; sdid++)
if (bitmap_test(ctrl->sdid_bitmap, sdid))
break;
if (sdid >= ctrl->max_domains)
return SBI_ENOSPC;SDIDLEN is implementation defined and may be as small as 1, i.e. two SDIDs in total. OpenSBI domains are also used for purposes unrelated to MPT (per-hart PMP partitioning, per-domain MPXY channels, IPI grouping), so a platform can easily have more domains than SDIDs while only one or two of them actually need a separate MPT. On such hardware the current binding turns a configuration choice into a boot failure: SBI_ENOSPC propagates from mpt_state_setup() to sbi_domain_finalize() and sbi_init() ends up calling sbi_hart_hang(). Note that sbi_mpt_init() already treats missing Smmpt hardware as non-fatal (returns SBI_ENODEV and sbi_hart_init() ignores it), so "not enough SDIDs" is handled more strictly than "no Smmpt at all".
Proposal: make the SDID assignment opt-in per domain instead of implicit for every domain.
- Add an optional u32 property "sdid" to the opensbi,domain,instance node. When absent, the domain runs under SDID 0, i.e. under the root domain's MPT, and relies on PMP for its own isolation.
- The MPT of SDID N is built from the memregions of the domain that owns N. SDID 0 is always owned by the root domain and its tree is built once. Domains aliasing to an already-owned SDID do not get a tree of their own.
- An explicit "sdid" must be < 2^SDIDLEN and unique across domains.
- A domain aliasing to SDID N must still program mmpt explicitly on activation. Right now sbi_mpt_hart_activate_for_domain() returns 0 when !sd->valid and sbi_mpt_hart_deactivate() is empty, so a hart switched away from an MPT-backed domain keeps the previous SDID in mmpt and would run under the previous domain's MPT. Today every domain has a valid tree so this is not reachable, but with opt-in assignment it becomes the normal path.
- When SDIDLEN is smaller than the number of explicitly requested SDIDs, report it and continue with the domains that fit rather than hanging.
This keeps the common case (PMP-only platform, Smmpt capable hart) working without any MPT memory, and lets a platform with SDIDLEN=1 run root plus one protected domain.
The assumption behind defaulting to SDID 0 is that PMP/ePMP remains the primary memory protection for domains without their own SDID; Smmpt is layered on top (sbi_hart_protection_configure() applies the TYPE_MEMORY protection and the TYPE_ID protections independently, so the effective permission of such a domain is its PMP entry set intersected with the SDID 0 MPT). If the intent is for Smmpt to replace PMP on some platforms, then sharing SDID 0 is not safe and this proposal does not apply.
Regards,
Huamao Wu
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Hi,
Thanks for the feedback, I have comments inline
Quoted 14 lines · Huamao Wu
Hi Rahul,
One design point on the SDID assignment.
mpt_state_setup()currently allocates one SDID per SBI domain, at domain registration time, taking the first free bit:for (sdid = 0; sdid < ctrl->max_domains; sdid++) if (bitmap_test(ctrl->sdid_bitmap, sdid)) break; if (sdid >= ctrl->max_domains) return SBI_ENOSPC;
Quoted 10 lines
SDIDLEN is implementation defined and may be as small as 1, i.e. two SDIDs in total. OpenSBI domains are also used for purposes unrelated to MPT (per-hart PMP partitioning, per-domain MPXY channels, IPI grouping), so a platform can easily have more domains than SDIDs while only one or two of them actually need a separate MPT. On such hardware the current binding turns a configuration choice into a boot failure:
SBI_ENOSPCpropagates frommpt_state_setup()tosbi_domain_finalize()andsbi_init()ends up callingsbi_hart_hang(). Note thatsbi_mpt_init()already treats missing Smmpt hardware as non-fatal (returnsSBI_ENODEVandsbi_hart_init()ignores it), so "not enough SDIDs" is handled more strictly than "no Smmpt at all".
A sensible hardware designed with SMMPT will need to make sure that there are sufficient SDIDLEN to support the use cases for which that hardware is meant for. It is exactly like having a system with 0 PMP entries though its allowed but the RISC-V ISA.
The current design keeps 1:1 mapping between the SBI domain and the MPT domain. Which means each SBI domain will have a MPT domain along with a SDID assigned. From a memory-isolation standpoint, a SBI domain is the software construct that SMMPT enforces in hardware.
This keeps the design and isolation model simple and avoids all sorts of problems
which you have highlighted.
The current error handling is the result of design assumptions plus what spec states-
- If the SMMPT + SMSDID both are not present then the system can function without it
and SBI domain backed by PMP/ePMP will work.
- If the SMMPT + SMSDID are present then any error in the initialization which also
includes not having sufficient SDIDs should result in a fatal error. Otherwise SBI domain backed by (PMP/ePMP + MPT) will be the enforcer.
The cost of sharing SDID is huge which involves a system wide mfence resulting in performance issues. If two domains share an SDID but have different MPT tables
then the SDID tagged MPT/TLB caching forces a mfence of that SDID on every domain switch between them which discards the optimization the SDID tag exists for and making the switch slower.
I will change the current handling of caase where SDIDs are not sufficient and make it diagnosable instead of making is fatal silently rather than make SDID assignment opt-in. I will also make SDID part of the DT nodes for SBI domains.
Quoted 35 lines
Proposal: make the SDID assignment opt-in per domain instead of implicit for every domain.
- Add an optional u32 property "sdid" to the opensbi,domain,instance node. When absent, the domain runs under SDID 0, i.e. under the root domain's MPT, and relies on PMP for its own isolation.
- The MPT of SDID N is built from the memregions of the domain that owns N. SDID 0 is always owned by the root domain and its tree is built once. Domains aliasing to an already-owned SDID do not get a tree of their own.
- An explicit "sdid" must be < 2^SDIDLEN and unique across domains.
- A domain aliasing to SDID N must still program mmpt explicitly on activation. Right nowsbi_mpt_hart_activate_for_domain()returns 0 when !sd->valid andsbi_mpt_hart_deactivate()is empty, so a hart switched away from an MPT-backed domain keeps the previous SDID in mmpt and would run under the previous domain's MPT. Today every domain has a valid tree so this is not reachable, but with opt-in assignment it becomes the normal path.
- When SDIDLEN is smaller than the number of explicitly requested SDIDs, report it and continue with the domains that fit rather than hanging.This keeps the common case (PMP-only platform, Smmpt capable hart) working without any MPT memory, and lets a platform with SDIDLEN=1 run root plus one protected domain.
The assumption behind defaulting to SDID 0 is that PMP/ePMP remains the primary memory protection for domains without their own SDID; Smmpt is layered on top (
sbi_hart_protection_configure()applies theTYPE_MEMORYprotection and theTYPE_IDprotections independently, so the effective permission of such a domain is its PMP entry set intersected with the SDID 0 MPT). If the intent is for Smmpt to replace PMP on some platforms, then sharing SDID 0 is not safe and this proposal does not apply.Regards,
Huamao Wu
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Hi Rahul,
Thanks, agreed on making the SDID shortage diagnosable and on moving SDID into the DT node. Comments inline.
Quoted 35 lines · Rahul Pathak
Hi,
Thanks for the feedback, I have comments inline
Quoted 14 lines · Huamao Wu
Hi Rahul,
One design point on the SDID assignment.
mpt_state_setup()currently allocates one SDID per SBI domain, at domain registration time, taking the first free bit:for (sdid = 0; sdid < ctrl->max_domains; sdid++) if (bitmap_test(ctrl->sdid_bitmap, sdid)) break; if (sdid >= ctrl->max_domains) return SBI_ENOSPC;Quoted 10 lines
SDIDLEN is implementation defined and may be as small as 1, i.e. two SDIDs in total. OpenSBI domains are also used for purposes unrelated to MPT (per-hart PMP partitioning, per-domain MPXY channels, IPI grouping), so a platform can easily have more domains than SDIDs while only one or two of them actually need a separate MPT. On such hardware the current binding turns a configuration choice into a boot failure:
SBI_ENOSPCpropagates frommpt_state_setup()tosbi_domain_finalize()andsbi_init()ends up callingsbi_hart_hang(). Note thatsbi_mpt_init()already treats missing Smmpt hardware as non-fatal (returnsSBI_ENODEVandsbi_hart_init()ignores it), so "not enough SDIDs" is handled more strictly than "no Smmpt at all".A sensible hardware designed with SMMPT will need to make sure that there are sufficient SDIDLEN to support the use cases for which that hardware is meant for. It is exactly like having a system with 0 PMP entries though its allowed but the RISC-V ISA.
Quoted 4 lines
The current design keeps 1:1 mapping between the SBI domain and the MPT domain. Which means each SBI domain will have a MPT domain along with a SDID assigned. From a memory-isolation standpoint, a SBI domain is the software construct that SMMPT enforces in hardware.
The tree is a function of the memregions alone. mpt_state_finalize() builds it from the domain's memregions plus "index == 0" for the firmware deny, and never reads the assigned HARTs, so two domains declared with one memory view map the same ranges at the same XWR. They remain two SBI domains by every other measure - separate next-addr, next-arg1, PMP entries, MPXY channels - and one MPT describes both of them exactly. Sharing the SDID between them is not a weaker isolation model; it is the same enforcement with one tree.
Quoted 17 lines
This keeps the design and isolation model simple and avoids all sorts of problems
which you have highlighted.The current error handling is the result of design assumptions plus what spec states-
- If the SMMPT + SMSDID both are not present then the system can function without it
and SBI domain backed by PMP/ePMP will work.- If the SMMPT + SMSDID are present then any error in the initialization which also
includes not having sufficient SDIDs should result in a fatal error. Otherwise SBI domain backed by (PMP/ePMP + MPT) will be the enforcer.
Quoted 6 lines
The cost of sharing SDID is huge which involves a system wide mfence resulting in performance issues. If two domains share an SDID but have different MPT tables
then the SDID tagged MPT/TLB caching forces a mfence of that SDID on every domain switch between them which discards the optimization the SDID tag exists for and making the switch slower.
If two domains do share an SDID, neither condition of that paragraph is met when their memory view is identical: the tree never differs, so a switch rewrites mmpt with the same encoding and there is no new MPT to make visible. The fence is paid either way - sbi_mpt_hart_activate() writes mmpt and fences the SDID on every domain switch - so sharing adds no fence that 1:1 does not already have. Two SDIDs on one tree would not help either: it saves the table but not the SDID, which is the scarce resource, and the code cannot express it - mpt_state_finalize() allocates a root unconditionally per domain, and sbi_mpt_share_region() copies region entries into the second tree rather than pointing the second SDID at the first.
Quoted 5 lines
I will change the current handling of caase where SDIDs are not sufficient and make it diagnosable instead of making is fatal silently rather than make SDID assignment opt-in. I will also make SDID part of the DT nodes for SBI domains.
Then the binding has to say what an absent sdid means, since that decides whether "more domains than SDIDs" is describable at all:
a) derived from the domain index, so 1:1 holds for every DT and a
too-small SDIDLEN stays an intended fatal error;
b) the domain owns no MPT and relies on PMP/ePMP.
Under (a) the property only picks a number and that platform stays undescribable; under (b) it becomes describable, while a DT that sets sdid for every domain behaves exactly as today. Either way please document the property as optional. And where two domains may share an sdid, require their memregion sets to be equal, EINVAL otherwise - sanitize_domain() has already sorted each set and dropped covered regions, so it is a count and array compare. There is no sdid property today, so the check has to arrive with the property, not after it.
Quoted 34 lines
Quoted 34 lines
Proposal: make the SDID assignment opt-in per domain instead of implicit for every domain.
- Add an optional u32 property "sdid" to the opensbi,domain,instance node. When absent, the domain runs under SDID 0, i.e. under the root domain's MPT, and relies on PMP for its own isolation.
- The MPT of SDID N is built from the memregions of the domain that owns N. SDID 0 is always owned by the root domain and its tree is built once. Domains aliasing to an already-owned SDID do not get a tree of their own.
- An explicit "sdid" must be < 2^SDIDLEN and unique across domains.
- A domain aliasing to SDID N must still program mmpt explicitly on activation. Right nowsbi_mpt_hart_activate_for_domain()returns 0 when !sd->valid andsbi_mpt_hart_deactivate()is empty, so a hart switched away from an MPT-backed domain keeps the previous SDID in mmpt and would run under the previous domain's MPT. Today every domain has a valid tree so this is not reachable, but with opt-in assignment it becomes the normal path.
- When SDIDLEN is smaller than the number of explicitly requested SDIDs, report it and continue with the domains that fit rather than hanging.This keeps the common case (PMP-only platform, Smmpt capable hart) working without any MPT memory, and lets a platform with SDIDLEN=1 run root plus one protected domain.
The assumption behind defaulting to SDID 0 is that PMP/ePMP remains the primary memory protection for domains without their own SDID; Smmpt is layered on top (
sbi_hart_protection_configure()applies theTYPE_MEMORYprotection and theTYPE_IDprotections independently, so the effective permission of such a domain is its PMP entry set intersected with the SDID 0 MPT). If the intent is for Smmpt to replace PMP on some platforms, then sharing SDID 0 is not safe and this proposal does not apply.
Quoted 2 lines
Quoted 2 lines
Regards,
Huamao Wu
Regards,
Huamao Wu
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi