Add Smsdid and Smmpt supervisor domain protection
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.
Testing:
Booted Linux when Smmpt was active. The boot dump shows the expected perregion 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 requried to be passed to Qemu are -
./qemu-system-riscv64 -nographic -M virt -cpu rv64,x-smsdid=true,x-smmpt=true
Please note that extra some logs in OpenSBI and Qemu are added for testing which will be removed later.
The Qemu log for smmpt_check_access is currently commented in the qemu branch to let linux boot logs print. But its present there for anyone to enable and check.
Boot Logs:
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
QEMU: smmpt_check_access: addr=0x80010000 mode=2 sdid=0 mptppn=0x80098 xwr=0 accesstype=0 -> DENY
QEMU: smmpt_check_access: addr=0x80200000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU: SELFTEST fw(0x80010000)=DENY(ok) kernel(0x80200000)=PERMIT(ok)
QEMU: smmpt_check_access: addr=0x80200000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU: smmpt_check_access: addr=0x802010d8 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU: smmpt_check_access: addr=0x81c25000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU: smmpt_check_access: addr=0x81c26000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU: smmpt_check_access: addr=0x81c27000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU: smmpt_check_access: addr=0x81c28000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU: smmpt_check_access: addr=0x81c29000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
The corresponding Qemu Smmpt support was also added which is present here -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1
These OpenSBI changes are also present in github -
https://github.com/pathakraul/opensbi/tree/rpathak_smmpt_v1
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.
Rahul Pathak (5):
riscv: Add Smsdid and Smmpt hart extensions
mpt: Add Smsdid and Smmpt supervisor domain core platform/generic: Initialise MPT at boot
sbi: hsm: Activate MPT for domain on hart start/resume
sbi: init: Print supervisor domains at boot
include/sbi/sbi_hart.h | 5 +- include/sbi/sbi_mpt.h | 321 +++++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_hart.c | 2 + lib/sbi/sbi_hsm.c | 7 + lib/sbi/sbi_init.c | 4 + lib/sbi/sbi_mpt.c | 882 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt_mode.c | 891 ++++++++++++++++++++++++++++++++++++ platform/generic/platform.c | 101 ++++ 9 files changed, 2214 insertions(+), 1 deletion(-) create mode 100644 include/sbi/sbi_mpt.h create mode 100644 lib/sbi/sbi_mpt.c create mode 100644 lib/sbi/sbi_mpt_mode.c
Signature · 7 lines
2.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH 1/5 riscv: Add Smsdid and Smmpt hart extensions
Add hart extension identifiers for the RISC-V Smsdid (supervisor domain ID) and Smmpt (supervisor domain memory protection) extensions.
--- include/sbi/sbi_hart.h | 5 ++++- lib/sbi/sbi_hart.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-)
include/sbi/sbi_hart.h +4 -1
@@ -95,8 +95,11 @@ 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.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH 1/5] riscv: Add Smsdid and Smmpt hart extensions
Quoted 41 lines · Rahul Pathak
Add hart extension identifiers for the RISC-V Smsdid (supervisor domain ID) and Smmpt (supervisor domain memory protection) extensions.
Signed-off-by Rahul Pathak <[email protected]>--- include/sbi/sbi_hart.h | 5 ++++- lib/sbi/sbi_hart.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-)include/sbi/sbi_hart.h +4
-1@@ -95,8 +95,11 @@ 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
-1@@ -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), --
Quoted 1 line
2.51.0
Quoted 6 lines
--
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 1/5] riscv: Add Smsdid and Smmpt hart extensions
In reply to Rahul PathakQuoted 15 lines · Rahul Pathak
Add hart extension identifiers for the RISC-V Smsdid (supervisor domain ID) and Smmpt (supervisor domain memory protection) extensions.
Signed-off-by Rahul Pathak <[email protected]>--- include/sbi/sbi_hart.h | 5 ++++- lib/sbi/sbi_hart.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-)include/sbi/sbi_hart.h +0
-0
include/sbi/sbi_hart.h enum sbi_hart_extensions { · line 99
Quoted 9 lines
include/sbi/sbi_hart.h +4
-1@@ -95,8 +95,11 @@ 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 */
This seems an undesired removal and should be retained.
Quoted 20 lines
include/sbi/sbi_hart.h +0
-0SBI_HART_EXT_MAX, };lib/sbi/sbi_hart.c +2
-1@@ -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), --2.51.0
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Otherwise, LGTM
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH 1/5] riscv: Add Smsdid and Smmpt hart extensions
Quoted 26 lines · Ranbir Singh
Quoted 23 lines · Rahul Pathak
Add hart extension identifiers for the RISC-V Smsdid (supervisor domain ID) and Smmpt (supervisor domain memory protection) extensions.
Signed-off-by Rahul Pathak <[email protected]>--- include/sbi/sbi_hart.h | 5 ++++- lib/sbi/sbi_hart.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-)include/sbi/sbi_hart.h +4
-0@@ -95,8 +95,11 @@ 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,
Quoted 3 lines
include/sbi/sbi_hart.h enum sbi_hart_extensions { · line 99
Quoted 1 line
include/sbi/sbi_hart.h +0
-1-/** Maximum index of Hart extension */ This seems an undesired removal and should be retained.
Yes, this is an unintentional removal. Will update
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH 2/5 mpt: Add Smsdid and Smmpt supervisor domain core
In reply to Rahul PathakIntroduce the supervisord omain 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_mpt.h | 321 +++++++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_mpt.c | 882 ++++++++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt_mode.c | 891 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 2096 insertions(+) create mode 100644 include/sbi/sbi_mpt.h create mode 100644 lib/sbi/sbi_mpt.c create mode 100644 lib/sbi/sbi_mpt_mode.c
include/sbi/sbi_mpt.h new file +321 -0
@@ -0,0 +1,321 @@ +/* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2026 Qualcomm Inc. +* +* Authors: Rahul Pathak <[email protected]> +*/ + +#ifndef __SBI_MPT_H__ +#define __SBI_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 UL(30) +# define SBI_MMPT_MODE_MASK (UL(0x3) << SBI_MMPT_MODE_SHIFT) +# define SBI_MMPT_SDID_SHIFT UL(22) +# 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 - UL(1)); + + __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 - UL(1)); + + __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 - UL(1)); + + __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; + 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); + int (*map_full_range)(struct sbi_mpt_domain *dom, u8 xwr); +}; + +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; + 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; +} + +/* + * Domain creation config + * + * sbi_dom and regions are mutually exclusive. + * when a supervisor domain is backed by a SBI domain + * then the regions list and access permissions are derived + * from the SBI domain structures. + */ +struct sbi_mpt_domain_config { + /* SBI domain whose S/U memregions are mapped. */ + struct sbi_domain *sbi_dom; + /* permission for regions inherited from sbi_dom */ + u8 xwr; + /* protect firmware regions for the domain */ + bool fw_protect; + /* + * explicit region list mapped in MPT table. + * NULL when sbi_dom covers everything + */ + struct sbi_mpt_region *regions; + u32 nregions; +}; + +int sbi_mpt_init(void); + +int sbi_mpt_domain_create(const struct sbi_mpt_domain_config *cfg, u32 *out_sdid); + +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); + +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); + +void sbi_mpt_dump(void); + +#endif /* __SBI_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_mpt.o +libsbi-objs-y += sbi_mpt_mode.o
lib/sbi/sbi_mpt.c new file +882 -0
882 lines not on this page · raw message
lib/sbi/sbi_mpt_mode.c new file +891 -0
891 lines not on this page · raw message
Signature · 7 lines
2.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH 3/5 platform/generic: Initialise MPT at boot
In reply to Rahul PathakIntegrates MPT initialisation into the generic platform boot flow.
mpt_early_init() probes for SMMPT during cold-boot early init and initialises the structures required to build MPT tables.
mpt_final_init() creates the root supervisor domain which is mapping all physical memory with the firmware region explcitly denied to S-mode and adds support for an MPT domain for each additional registered SBI domain.
The heap budget is extended to reserve space for MPT tables.
With this patch MPT tables are built at boot.
--- platform/generic/platform.c | 101 ++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)
platform/generic/platform.c +101 -0
@@ -17,6 +17,9 @@#include <sbi/sbi_string.h> #include <sbi/sbi_system.h> #include <sbi/sbi_tlb.h> +#include <sbi/sbi_mpt.h> +#include <sbi/sbi_domain.h> +#include <sbi/sbi_console.h> #include <sbi_utils/cache/fdt_cmo_helper.h> #include <sbi_utils/fdt/fdt_domain.h> #include <sbi_utils/fdt/fdt_driver.h>
@@ -33,6 +36,86 @@/* List of platform override modules generated at compile time */ extern const struct fdt_driver *const platform_override_modules[]; +/* +* mpt_early_init(): Detects the SMMPT mode and initializes the +* structures (not MPT tables) requried to prepare MPT tables and +* domain programming. +*/ +int mpt_early_init (bool cold_boot) +{ +int rc; + +if (!cold_boot) +return 0 ; + + rc =sbi_mpt_init (); +if (rc ==SBI_ENODEV ) +return 0 ; + +return rc; +} + +/* +* mpt_final_init(): Performs the final initialzation including +* creation of Root supervisor domain. +*/ +int mpt_final_init (bool cold_boot) +{ +int rc; +u32 sdid; +struct sbi_domain *dom, *root_dom; +struct sbi_mpt_domain_config cfg; + +if (!cold_boot) +return 0 ; + +/* SMMPT may be unavailable */ +if (!sbi_mpt_ctrl_get ()->ready) +return 0 ; + +/* +* Create MPT domain for Root domain. +* +* Root domain maps all physical memory so the +* firmware region must be explicitly denied to S-mode. +* +* regions pointer is NULL and nregions is 0 since there +* are no other regions other than respective SBI domain for mapping. +*/ + root_dom =sbi_domain_thishart_ptr (); + cfg.sbi_dom = root_dom; + cfg.xwr =SBI_MPT_PERM_RWX ; + cfg.fw_protect = true; + cfg.regions =NULL ; + cfg.nregions =0 ; + + rc =sbi_mpt_domain_create (&cfg, &sdid); +if (rc) { +sbi_printf ("mpt: Root domain create failed rc=%d\n" , rc); +return rc; + } + +sbi_memset (&cfg,0 ,sizeof (cfg)); + +/* Create MPT domain for all other registered SBI domains. */ +sbi_domain_for_each (dom) { +if (dom == root_dom) +continue ; + + cfg.sbi_dom = dom; + cfg.xwr =SBI_MPT_PERM_RWX ; + cfg.fw_protect = false; + cfg.regions =NULL ; + cfg.nregions =0 ; + + rc =sbi_mpt_domain_create (&cfg, &sdid); +if (rc) +sbi_printf ("mpt: domain '%s' failed rc=%d\n" , dom->name, rc); + } + +return 0 ; +} +static u32 fw_platform_calculate_heap_size (u32 hart_count) {u32 heap_size;
@@ -42,6 +125,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 +=1024 *1024 ; +return BIT_ALIGN (heap_size,HEAP_BASE_ALIGN ); }
@@ -229,6 +321,10 @@ int generic_early_init(bool cold_boot)return rc;fdt_driver_init_all (fdt, fdt_early_drivers); + + rc =mpt_early_init (cold_boot); +if (rc) +return rc; }return fdt_cmo_init (cold_boot);
@@ -236,11 +332,16 @@ int generic_early_init(bool cold_boot)int generic_final_init (bool cold_boot) { +int rc;void *fdt =fdt_get_address_rw ();if (!cold_boot)return 0 ; + rc =mpt_final_init (cold_boot); +if (rc) +return rc; +fdt_cpu_fixup (fdt);fdt_fixups (fdt);fdt_domain_fixup (fdt);
Signature · 7 lines
2.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH 4/5 sbi: hsm: Activate MPT for domain on hart start/resume
In reply to Rahul PathakProgram the hart mmpt CSR for its supervisor domain on the HSM start and resume paths before entering S-mode.
--- lib/sbi/sbi_hsm.c | 7 +++++++ 1 file changed, 7 insertions(+)
lib/sbi/sbi_hsm.c +7 -0
@@ -25,6 +25,7 @@#include <sbi/sbi_system.h> #include <sbi/sbi_timer.h> #include <sbi/sbi_console.h> +#include <sbi/sbi_mpt.h> #define __sbi_hsm_hart_change_state (hdata, oldstate, newstate) \ ({ \
@@ -154,6 +155,9 @@ void __noreturn sbi_hsm_hart_start_finish(struct sbi_scratch *scratch, next_mode = scratch->next_mode;hsm_start_ticket_release (hdata); +/* Program hart mmpt for its supervisor domain before entering S-mode. */ +sbi_mpt_hart_activate_for_domain (sbi_domain_thishart_ptr ()); +sbi_hart_switch_mode (hartid, next_arg1, next_addr, next_mode, false); }
@@ -478,6 +482,9 @@ void __noreturn sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch,*/ __sbi_hsm_suspend_non_ret_restore (scratch); +/* Reprogram mmpt on the a resumed hart */ +sbi_mpt_hart_activate_for_domain (sbi_domain_thishart_ptr ()); +sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, scratch->next_mode, false);
Signature · 7 lines
2.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH 5/5 sbi: init: Print supervisor domains at boot
In reply to Rahul PathakPrint the configured supervisor domains alongside the existing domain details during boot.
--- lib/sbi/sbi_init.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/sbi/sbi_init.c +4 -0
@@ -35,6 +35,7 @@#include <sbi/sbi_tlb.h> #include <sbi/sbi_version.h> #include <sbi/sbi_unit_test.h> +#include <sbi/sbi_mpt.h> #define BANNER \ " ____ _____ ____ _____\n" \
@@ -162,6 +163,9 @@ static void sbi_boot_print_domains(struct sbi_scratch *scratch)/* Domain details */ sbi_domain_dump_all (" " ); + +/* Print Supervisor Domains */ +sbi_mpt_dump (); }static void sbi_boot_print_hart (struct sbi_scratch *scratch,u32 hartid)
Signature · 7 lines
2.51.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Hi,
I also should have mentioned that this is based on RISC-V SMMTT spec 0.49 -
https://github.com/riscv/riscv-smmtt/releases/tag/v0.49
which is the latest version currently.
Thanks
Rahul
Quoted 109 lines · Rahul Pathak
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.
Testing:
Booted Linux when Smmpt was active. The boot dump shows the expected perregion 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 requried to be passed to Qemu are -
./qemu-system-riscv64 -nographic -M virt -cpu rv64,x-smsdid=true,x-smmpt=true
Please note that extra some logs in OpenSBI and Qemu are added for testing which will be removed later.
The Qemu log for
smmpt_check_accessis currently commented in the qemu branch to let linux boot logs print. But its present there for anyone to enable and check.Boot Logs:
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
QEMU:smmpt_check_access: addr=0x80010000 mode=2 sdid=0 mptppn=0x80098 xwr=0 accesstype=0 -> DENY
QEMU:smmpt_check_access: addr=0x80200000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU: SELFTEST fw(0x80010000)=DENY(ok) kernel(0x80200000)=PERMIT(ok)
QEMU:smmpt_check_access: addr=0x80200000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU:smmpt_check_access: addr=0x802010d8 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=2 -> PERMIT
QEMU:smmpt_check_access: addr=0x81c25000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU:smmpt_check_access: addr=0x81c26000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU:smmpt_check_access: addr=0x81c27000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU:smmpt_check_access: addr=0x81c28000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMIT
QEMU:smmpt_check_access: addr=0x81c29000 mode=2 sdid=0 mptppn=0x80098 xwr=7 accesstype=1 -> PERMITThe corresponding Qemu Smmpt support was also added which is present here -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1These OpenSBI changes are also present in github -
https://github.com/pathakraul/opensbi/tree/rpathak_smmpt_v1Further 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.Rahul Pathak (5):
riscv: Add Smsdid and Smmpt hart extensions
mpt: Add Smsdid and Smmpt supervisor domain core platform/generic: Initialise MPT at boot
sbi: hsm: Activate MPT for domain on hart start/resume
sbi: init: Print supervisor domains at bootinclude/sbi/sbi_hart.h | 5 +- include/sbi/sbi_mpt.h | 321 +++++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_hart.c | 2 + lib/sbi/sbi_hsm.c | 7 + lib/sbi/sbi_init.c | 4 + lib/sbi/sbi_mpt.c | 882 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt_mode.c | 891 ++++++++++++++++++++++++++++++++++++ platform/generic/platform.c | 101 ++++ 9 files changed, 2214 insertions(+), 1 deletion(-) create mode 100644 include/sbi/sbi_mpt.h create mode 100644 lib/sbi/sbi_mpt.c create mode 100644 lib/sbi/sbi_mpt_mode.c--
2.51.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi