Add Smsdid and Smmpt supervisor domain protection
v4 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]/
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
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_v4
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.
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 finalize callback for per-domain state
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_state.h | 22 + include/sbi/sbi_hart.h | 4 + include/sbi/sbi_hart_mpt.h | 312 ++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain.c | 16 + lib/sbi/sbi_domain_state.c | 25 + lib/sbi/sbi_hart.c | 10 +- lib/sbi/sbi_hart_mpt.c | 993 +++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt.c | 1000 ++++++++++++++++++++++++++++++++ platform/generic/platform.c | 9 + 10 files changed, 2392 insertions(+), 1 deletion(-) 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 v4 1/4 lib: sbi_domain: Add finalize callback for per-domain state
Per-domain state is registered via sbi_domain_state in state_setup() but during that time the domain memory regions are not final. Add optional state_finalize() callback which is called from sbi_domain_finalize for each domain after all domains are registered and their memory regions are final.
--- include/sbi/sbi_domain_state.h | 22 ++++++++++++++++++++++ lib/sbi/sbi_domain.c | 16 ++++++++++++++++ lib/sbi/sbi_domain_state.c | 25 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+)
include/sbi/sbi_domain_state.h +22 -0
@@ -40,6 +40,18 @@ struct sbi_domain_state {/** Optional callback to setup domain state */ int (*state_setup)(struct sbi_domain *dom,struct sbi_domain_state *state,void *state_ptr); +/** +* Optional callback to finalize domain state +* +* Called for each domain from sbi_domain_finalize() after all +* domains are registered and memory regions are final. +* +* State from the domain memory regions must be setup here instead +* of state_setup() +*/ +int (*state_finalize)(struct sbi_domain *dom, +struct sbi_domain_state *state,void *state_ptr); +/** Optional callback to cleanup domain state */ void (*state_cleanup)(struct sbi_domain *dom,struct sbi_domain_state *state,void *state_ptr);
@@ -64,6 +76,16 @@ void *sbi_domain_state_ptr(struct sbi_domain *dom, struct sbi_domain_state *stat*/ int sbi_domain_setup_state (struct sbi_domain *dom); +/** +* Finalize all domain state for a domain +* @param dom pointer to domain +* +* @return 0 on success and negative error code on failure +* +* Note: This function is used internally within domain framework. +*/ +int sbi_domain_finalize_state (struct sbi_domain *dom); +/** * Cleanup all domain state for a domain * @param dom pointer to domain
lib/sbi/sbi_domain.c +16 -0
@@ -845,6 +845,7 @@ int sbi_domain_startup(struct sbi_scratch *scratch, u32 cold_hartid)int sbi_domain_finalize (struct sbi_scratch *scratch) {int rc; +struct sbi_domain *dom;const struct sbi_platform *plat =sbi_platform_ptr (scratch);/* Sanity checks */
@@ -865,6 +866,21 @@ int sbi_domain_finalize(struct sbi_scratch *scratch)*/ domain_finalized = true; +/* +* Finalize per-domain state of each domain. Now all domains +* are finalized already and their memory regions are final. +* State which is derived from the domain memory regions is +* set up below. +*/ +sbi_domain_for_each (dom) { + rc =sbi_domain_finalize_state (dom); +if (rc) { +sbi_printf ("%s: domain state finalize failed for %s" +" (error %d)\n" , __func__, dom->name, rc); +return rc; + } + } +return 0 ; }
lib/sbi/sbi_domain_state.c +25 -0
@@ -84,6 +84,31 @@ int sbi_domain_setup_state(struct sbi_domain *dom)return 0 ; } +int sbi_domain_finalize_state (struct sbi_domain *dom) +{ +struct sbi_domain_state *state; +void *state_ptr; +int rc; + +if (!dom) +return SBI_EINVAL ; + +sbi_list_for_each_entry (state, &state_list, head) { +if (!state->state_finalize) +continue ; + + state_ptr =sbi_domain_state_ptr (dom, state); +if (!state_ptr) +continue ; + + rc = state->state_finalize (dom, state, state_ptr); +if (rc) +return rc; + } + +return 0 ; +} +void sbi_domain_cleanup_state (struct sbi_domain *dom) {struct sbi_domain_state *state;
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v4 1/4] lib: sbi_domain: Add finalize callback for per-domain state
Quoted 77 lines · Rahul Pathak
Per-domain state is registered via
sbi_domain_stateinstate_setup()but during that time the domain memory regions are not final. Add optionalstate_finalize()callback which is called fromsbi_domain_finalizefor each domain after all domains are registered and their memory regions are final.Signed-off-by Rahul Pathak <[email protected]>--- include/sbi/sbi_domain_state.h | 22 ++++++++++++++++++++++ lib/sbi/sbi_domain.c | 16 ++++++++++++++++ lib/sbi/sbi_domain_state.c | 25 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+)include/sbi/sbi_domain_state.h +22
-0@@ -40,6 +40,18 @@ struct sbi_domain_state {/** Optional callback to setup domain state */ int (*state_setup)(struct sbi_domain *dom,struct sbi_domain_state *state,void *state_ptr); +/** +* Optional callback to finalize domain state +* +* Called for each domain from sbi_domain_finalize() after all +* domains are registered and memory regions are final. +* +* State from the domain memory regions must be setup here instead +* of state_setup() +*/ +int (*state_finalize)(struct sbi_domain *dom, +struct sbi_domain_state *state,void *state_ptr); +/** Optional callback to cleanup domain state */ void (*state_cleanup)(struct sbi_domain *dom,struct sbi_domain_state *state,void *state_ptr);@@ -64,6 +76,16 @@ void *sbi_domain_state_ptr(struct sbi_domain *dom, struct sbi_domain_state *stat*/ int sbi_domain_setup_state (struct sbi_domain *dom); +/** +* Finalize all domain state for a domain +* @param dom pointer to domain +* +* @return 0 on success and negative error code on failure +* +* Note: This function is used internally within domain framework. +*/ +int sbi_domain_finalize_state (struct sbi_domain *dom); +/** * Cleanup all domain state for a domain * @param dom pointer to domain lib/sbi/sbi_domain.c +8
-0@@ -845,6 +845,7 @@ int sbi_domain_startup(struct sbi_scratch *scratch, u32 cold_hartid)int sbi_domain_finalize (struct sbi_scratch *scratch) {int rc; +struct sbi_domain *dom;const struct sbi_platform *plat =sbi_platform_ptr (scratch);/* Sanity checks */ @@ -865,6 +866,21 @@ int sbi_domain_finalize(struct sbi_scratch *scratch)*/ domain_finalized = true; +/* +* Finalize per-domain state of each domain. Now all domains +* are finalized already and their memory regions are final. +* State which is derived from the domain memory regions is +* set up below. +*/ +sbi_domain_for_each (dom) {
lib/sbi/sbi_domain.c int sbi_domain_finalize(struct sbi_scratch *scratch) · line 885
Quoted 10 lines
lib/sbi/sbi_domain.c +8
-0+ rc =sbi_domain_finalize_state (dom); +if (rc) { +sbi_printf ("%s: domain state finalize failed for %s" +" (error %d)\n" , __func__, dom->name, rc); +return rc; + } + } +return 0 ; }
Oza:
I am not sure if this infrastructure would be useful to you for state finalization. but have a look if you think you could reuse this notifier infrastructure which is inflight athe the moment.
refer to this patch. this notifier is called before the
[PATCH v2 1/3] lib: sbi: domain: add domain registration notifier infrastructure
but if you look sbi_domain_finalize calls sbi_platform_domains_init which in turn calls sbi_platform_ops(plat)->domains_init(); and generic_domains_init will eventually call into fdt_domains_populate and during domina_register this call back notifier will be called.
have a look to see if it makes sense to use it ? I could be missing some subtle thing though here.
Quoted 45 lines
lib/sbi/sbi_domain_state.c +25
-1@@ -84,6 +84,31 @@ int sbi_domain_setup_state(struct sbi_domain *dom)return 0 ; } +int sbi_domain_finalize_state (struct sbi_domain *dom) +{ +struct sbi_domain_state *state; +void *state_ptr; +int rc; + +if (!dom) +return SBI_EINVAL ; + +sbi_list_for_each_entry (state, &state_list, head) { +if (!state->state_finalize) +continue ; + + state_ptr =sbi_domain_state_ptr (dom, state); +if (!state_ptr) +continue ; + + rc = state->state_finalize (dom, state, state_ptr); +if (rc) +return rc; + } + +return 0 ; +} +void sbi_domain_cleanup_state (struct sbi_domain *dom) {struct sbi_domain_state *state; --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
RFC PATCH v4 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 v4 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 | 993 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_mpt.c | 1000 ++++++++++++++++++++++++++++++++++++ 4 files changed, 2307 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 +993 -0
993 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 v4 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 | 8 +++++++- platform/generic/platform.c | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
lib/sbi/sbi_hart.c +7 -1
@@ -1,4 +1,4 @@ -/* +/*sbi_hart. * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2019 Western Digital Corporation or its affiliates.
@@ -21,6 +21,7 @@#include <sbi/sbi_pmu.h> #include <sbi/sbi_string.h> #include <sbi/sbi_trap.h> +#include <sbi/sbi_hart_mpt.h> extern void __sbi_expected_trap (void );extern void __sbi_expected_trap_hext (void );
@@ -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 +9 -0
@@ -42,6 +42,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 ); }
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [RFC PATCH v4 4/4] lib: sbi: Initialize SMMPT during coldboot
Quoted 20 lines · Rahul Pathak
Call
sbi_mpt_init()function from coldboot path ofsbi_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
Signed-off-by Rahul Pathak <[email protected]>--- lib/sbi/sbi_hart.c | 8 +++++++- platform/generic/platform.c | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)lib/sbi/sbi_hart.c +0
-0
lib/sbi/sbi_hart.c line 1
Quoted 2 lines
lib/sbi/sbi_hart.c +1
-1@@ -1,4 +1,4 @@ -/* +/*sbi_hart.
RS: Looks like this is mistakenly added.
Quoted 29 lines
lib/sbi/sbi_hart.c +6
-0* SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2019 Western Digital Corporation or its affiliates. @@ -21,6 +21,7 @@#include <sbi/sbi_pmu.h> #include <sbi/sbi_string.h> #include <sbi/sbi_trap.h> +#include <sbi/sbi_hart_mpt.h> extern void __sbi_expected_trap (void );extern void __sbi_expected_trap_hext (void );@@ -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 +0
-0@@ -42,6 +42,15 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count)/* For TLB fifo */
platform/generic/platform.c static u32 fw_platform_calculate_heap_size(u32 hart_count) · line 52
Quoted 10 lines
platform/generic/platform.c +8
-0heap_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 ;
RS: See if it is better to define and use macros here like
#define MB(x) (x * 1024 * 1024)
heap_size += MB(1)Quoted 12 lines
platform/generic/platform.c +1
-1+return BIT_ALIGN (heap_size,HEAP_BASE_ALIGN ); } --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 v4 4/4] lib: sbi: Initialize SMMPT during coldboot
Quoted 18 lines · Ranbir Singh
Quoted 15 lines · Rahul Pathak
Call
sbi_mpt_init()function from coldboot path ofsbi_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
Signed-off-by Rahul Pathak <[email protected]>--- lib/sbi/sbi_hart.c | 8 +++++++- platform/generic/platform.c | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
Quoted 9 lines
lib/sbi/sbi_hart.c line 1
Quoted 7 lines
lib/sbi/sbi_hart.c +1
-1@@ -1,4 +1,4 @@ -/* +/*sbi_hart. RS: Looks like this is mistakenly added.
Yes, typo, will correct it
Quoted 32 lines
Quoted 32 lines
lib/sbi/sbi_hart.c +6
-0* SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2019 Western Digital Corporation or its affiliates. @@ -21,6 +21,7 @@#include <sbi/sbi_pmu.h> #include <sbi/sbi_string.h> #include <sbi/sbi_trap.h> +#include <sbi/sbi_hart_mpt.h> extern void __sbi_expected_trap (void );extern void __sbi_expected_trap_hext (void );@@ -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 +0
-0@@ -42,6 +42,15 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count)/* For TLB fifo */ heap_size +=SBI_TLB_INFO_SIZE * (hart_count) * (hart_count);
Quoted 10 lines
platform/generic/platform.c static u32 fw_platform_calculate_heap_size(u32 hart_count) · line 52
Quoted 8 lines
platform/generic/platform.c +8
-0+/* +* 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 ;RS: See if it is better to define and use macros here like
I agree, will update
Quoted 16 lines
#define MB(x) (x * 1024 * 1024) heap_size += MB(1)Quoted 12 lines
platform/generic/platform.c +1
-1+return BIT_ALIGN (heap_size,HEAP_BASE_ALIGN ); } --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