Add Smsd supervisor domain protection
v7 cover Add Smsd supervisor domain protection
NOTE: This series is updated for the latest RISC-V SDAP (Supervisor domains access protection) spec 0.9 which has gone though some changes which are not yet adapted in Qemu. This series is only for review. Rest of the coverletter is old and will be Updated once the Qemu is available with SDAP v0.9 support.
https://github.com/riscv/riscv-smmtt/releases/download/v0.9.0/supervisor-domains.pdf
This series adds OpenSBI support for the RISC-V SDAP specification sub-extensions - Smsd (supervisor domain).
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 Sdap 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_v7
Further Development:
This series add base infrastructure for Smsd. Development and testing with more then one Supervisor Domain. Extensive testing with all the modes and other platform configurations. SDID allocation through DT through a per domain property.
v6 -> v7:
1. Naming convention improvements, changes from smmpt to smsd/mpt.
v5 -> v6:
1. Revert back to state_finalize callback for per domain state finalization(v4) instead of having a notifier.
2. Introduce memory class for TYPE_MEMORY hart protection mechanisms.
3. Added the fdt_cpu_has_extension() helper to check for an extension in the DT before the hart extensions are parsed.
4. Updated to RISC-V SMMTT specification v0.9
5. Allow only root domain to initialize if the SDIDLEN=0 instead of disabling MPT for the system.
6. The MPT heap memory is reserved only when a hart in the DT implements Smsd extension.
7. Rebase on the latest master.
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 (6):
lib: sbi_domain: Add finalize callback for per-domain state
lib: sbi: Introduce memory class for memory type hart protection
lib: utils: fdt: Add helper to check for an extension in DT
riscv: Add Smsd (supervisor domain) hart extensions
mpt: Add Smsd (supervisor domain) core
lib: sbi: Initialize Smsd during coldboot
include/sbi/sbi_domain_state.h | 22 + include/sbi/sbi_hart.h | 2 + include/sbi/sbi_hart_mpt.h | 314 +++++++++ include/sbi/sbi_hart_protection.h | 20 + include/sbi_utils/fdt/fdt_helper.h | 4 + lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain.c | 16 + lib/sbi/sbi_domain_state.c | 25 + lib/sbi/sbi_hart.c | 7 + lib/sbi/sbi_hart_mpt.c | 991 +++++++++++++++++++++++++++ lib/sbi/sbi_hart_pmp.c | 2 + lib/sbi/sbi_hart_protection.c | 84 +-- lib/sbi/sbi_mpt.c | 1000 ++++++++++++++++++++++++++++ lib/utils/fdt/fdt_helper.c | 61 +- platform/generic/platform.c | 23 +- 15 files changed, 2502 insertions(+), 71 deletions(-) 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 v7 1/6 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
@@ -947,6 +947,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 */
@@ -967,6 +968,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
RFC PATCH v7 2/6 lib: sbi: Introduce memory class for memory type hart protection
In reply to Rahul PathakIn TYPE_MEMORY type mechanisms, some of the hart protection mechanisms can be exclusive and other can work as an addon with the other TYPE_MEMORY type mechanisms. For example, in PMP, SMEPMP and SMSD which are currently the TYPE_MEMORY type protection mechanisms, the PMP and SMEPMP are exclusive to each other and the SMSD if present will work together with PMP or SMEPMP. So it will be either PMP+SMSD or SMEPMP+SMSD.
For such configurations selection, add memory class in which types within same memory class are exclusive but other types in different memory class selected will work together.
--- include/sbi/sbi_hart_protection.h | 20 ++++++++ lib/sbi/sbi_hart_pmp.c | 2 + lib/sbi/sbi_hart_protection.c | 84 ++++++++++++------------------- 3 files changed, 53 insertions(+), 53 deletions(-)
include/sbi/sbi_hart_protection.h +20 -0
@@ -20,6 +20,23 @@ enum sbi_hart_protection_type {SBI_HART_PROTECTION_TYPE_MAX }; +/** +* Hart protection class for memory protection. +* +* Memory protection mechanisms of the same class use the same +* hardware for memory access which makes them mutually exclusive and +* among those the rating will choose which one is active. +* Mechanisms of different classes can work together so a hart can have +* PMP + MPT or ePMP + MPT. +*/ +enum sbi_hart_protection_memory_class { +/** PMP CSRs: pmp, epmp */ +SBI_HART_PROTECTION_MEMORY_CLASS_PMP =0 , +/** mmpt CSR: MPT (Smsd) */ +SBI_HART_PROTECTION_MEMORY_CLASS_MPT , +SBI_HART_PROTECTION_MEMORY_CLASS_MAX +}; +/** Representation of hart protection mechanism */ struct sbi_hart_protection {/** List head */
@@ -31,6 +48,9 @@ struct sbi_hart_protection {/** Type of the hart protection mechanism */ enum sbi_hart_protection_type type; +/** Hardware programmed by the mechanism */ +enum sbi_hart_protection_memory_class memory_class; +/** Ratings of the hart protection mechanism (higher is better) */ unsigned long rating;
lib/sbi/sbi_hart_pmp.c +2 -0
@@ -426,6 +426,7 @@ static struct sbi_hart_protection pmp_protection = { .name = "pmp", .rating = 100, .type = SBI_HART_PROTECTION_TYPE_MEMORY, + .memory_class = SBI_HART_PROTECTION_MEMORY_CLASS_PMP, .configure = sbi_hart_oldpmp_configure, .unconfigure = sbi_hart_pmp_unconfigure, };
@@ -434,6 +435,7 @@ static struct sbi_hart_protection epmp_protection = { .name = "epmp", .rating = 200, .type = SBI_HART_PROTECTION_TYPE_MEMORY, + .memory_class = SBI_HART_PROTECTION_MEMORY_CLASS_PMP, .configure = sbi_hart_smepmp_configure, .unconfigure = sbi_hart_pmp_unconfigure, .temp_map_range = sbi_hart_smepmp_temp_map_range,
lib/sbi/sbi_hart_protection.c +31 -53
155 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 v7 2/6] lib: sbi: Introduce memory class for memory type hart protection
Quoted 8 lines · Rahul Pathak
In
TYPE_MEMORYtype mechanisms, some of the hart protection mechanisms can be exclusive and other can work as an addon with the otherTYPE_MEMORYtype mechanisms. For example, in PMP, SMEPMP and SMSD which are currently theTYPE_MEMORYtype protection mechanisms, the PMP and SMEPMP are exclusive to each other and the SMSD if present will work together with PMP or SMEPMP. So it will be either PMP+SMSD or SMEPMP+SMSD.
This memory class attribute in hart_protection does not really help us reduce PMP regions when SMSD is present.
PMP+SMSD or SMEPMP + SMSD is supposed to be composite hart_protection which programs both PMP and SMSD such that it only programs PMP for M-only and M+S regions and enforcement of S-only regions is only through MPT.
I suggest dropping this patch and extending your SMSD hart protection to use PMP at appropriate places for M-only and M+S regions whereas use MPT for other regions.
Quoted 241 lines
For such configurations selection, add memory class in which types within same memory class are exclusive but other types in different memory class selected will work together.
Signed-off-by Rahul Pathak <[email protected]>--- include/sbi/sbi_hart_protection.h | 20 ++++++++ lib/sbi/sbi_hart_pmp.c | 2 + lib/sbi/sbi_hart_protection.c | 84 ++++++++++++------------------- 3 files changed, 53 insertions(+), 53 deletions(-)include/sbi/sbi_hart_protection.h +20
-0@@ -20,6 +20,23 @@ enum sbi_hart_protection_type {SBI_HART_PROTECTION_TYPE_MAX }; +/** +* Hart protection class for memory protection. +* +* Memory protection mechanisms of the same class use the same +* hardware for memory access which makes them mutually exclusive and +* among those the rating will choose which one is active. +* Mechanisms of different classes can work together so a hart can have +* PMP + MPT or ePMP + MPT. +*/ +enum sbi_hart_protection_memory_class { +/** PMP CSRs: pmp, epmp */ +SBI_HART_PROTECTION_MEMORY_CLASS_PMP =0 , +/** mmpt CSR: MPT (Smsd) */ +SBI_HART_PROTECTION_MEMORY_CLASS_MPT , +SBI_HART_PROTECTION_MEMORY_CLASS_MAX +}; +/** Representation of hart protection mechanism */ struct sbi_hart_protection {/** List head */ @@ -31,6 +48,9 @@ struct sbi_hart_protection {/** Type of the hart protection mechanism */ enum sbi_hart_protection_type type; +/** Hardware programmed by the mechanism */ +enum sbi_hart_protection_memory_class memory_class; +/** Ratings of the hart protection mechanism (higher is better) */ unsigned long rating;lib/sbi/sbi_hart_pmp.c +2
-0@@ -426,6 +426,7 @@ static struct sbi_hart_protection pmp_protection = { .name = "pmp", .rating = 100, .type = SBI_HART_PROTECTION_TYPE_MEMORY, + .memory_class = SBI_HART_PROTECTION_MEMORY_CLASS_PMP, .configure = sbi_hart_oldpmp_configure, .unconfigure = sbi_hart_pmp_unconfigure, };@@ -434,6 +435,7 @@ static struct sbi_hart_protection epmp_protection = { .name = "epmp", .rating = 200, .type = SBI_HART_PROTECTION_TYPE_MEMORY, + .memory_class = SBI_HART_PROTECTION_MEMORY_CLASS_PMP, .configure = sbi_hart_smepmp_configure, .unconfigure = sbi_hart_pmp_unconfigure, .temp_map_range = sbi_hart_smepmp_temp_map_range,lib/sbi/sbi_hart_protection.c +31
-54@@ -12,21 +12,37 @@static SBI_LIST_HEAD (hart_protection_list); -static struct sbi_hart_protection *__hart_memory_protection_best (void ) +static struct sbi_hart_protection * +__hart_memory_protection_best (enum sbi_hart_protection_memory_class memclass) {struct sbi_hart_protection *pos;sbi_list_for_each_entry (pos, &hart_protection_list, head) { -if (pos->type ==SBI_HART_PROTECTION_TYPE_MEMORY ) +if (pos->type ==SBI_HART_PROTECTION_TYPE_MEMORY && + pos->memory_class == memclass)return pos; }return NULL ; } +/* +* Select the highest rating protection mechanism from each class +*/ +static bool __hart_protection_selected (struct sbi_hart_protection *hprot) +{ +switch (hprot->type) { +case SBI_HART_PROTECTION_TYPE_MEMORY : +return hprot ==__hart_memory_protection_best (hprot->memory_class); +case SBI_HART_PROTECTION_TYPE_ID : +return true; +default : +return false; + } +} +void sbi_hart_protection_get_str (char *out_str,int out_str_size) { -bool memory_protect_done = false;struct sbi_hart_protection *pos;int offset =0 ;@@ -35,11 +51,8 @@ void sbi_hart_protection_get_str(char *out_str, int out_str_size)sbi_memset (out_str,0 , out_str_size);sbi_list_for_each_entry (pos, &hart_protection_list, head) { -if (pos->type ==SBI_HART_PROTECTION_TYPE_MEMORY ) { -if (memory_protect_done) -continue ; - memory_protect_done = true; - } +if (!__hart_protection_selected (pos)) +continue ;sbi_snprintf (out_str + offset, out_str_size - offset,"%s," , pos->name); offset = offset +sbi_strlen (pos->name) +1 ; }@@ -59,6 +72,9 @@ int sbi_hart_protection_register(struct sbi_hart_protection *hprot)return SBI_EINVAL ;if (hprot->type >=SBI_HART_PROTECTION_TYPE_MAX )return SBI_EINVAL ; +if (hprot->type ==SBI_HART_PROTECTION_TYPE_MEMORY && + hprot->memory_class >=SBI_HART_PROTECTION_MEMORY_CLASS_MAX ) +return SBI_EINVAL ;sbi_list_for_each_entry (pos, &hart_protection_list, head) {if (hprot->rating > pos->rating) {@@ -108,24 +124,11 @@ static void __hart_protection_unconfigure(struct sbi_scratch *scratch,int sbi_hart_protection_configure (struct sbi_scratch *scratch,struct sbi_domain *dom) { -bool do_configure, memory_protect_done = false;struct sbi_hart_protection *hprot;int ret;sbi_list_for_each_entry (hprot, &hart_protection_list, head) { - do_configure = false; -switch (hprot->type) { -case SBI_HART_PROTECTION_TYPE_MEMORY : - do_configure = !memory_protect_done; - memory_protect_done = true; -break ; -case SBI_HART_PROTECTION_TYPE_ID : - do_configure = true; -break ; -default : -break ; - } -if (!do_configure) +if (!__hart_protection_selected (hprot))continue ; ret =__hart_protection_configure (scratch, hprot, dom);@@ -139,24 +142,10 @@ int sbi_hart_protection_configure(struct sbi_scratch *scratch,void sbi_hart_protection_unconfigure (struct sbi_scratch *scratch,struct sbi_domain *dom) { - -bool do_unconfigure, memory_protect_done = false;struct sbi_hart_protection *hprot;sbi_list_for_each_entry (hprot, &hart_protection_list, head) { - do_unconfigure = false; -switch (hprot->type) { -case SBI_HART_PROTECTION_TYPE_MEMORY : - do_unconfigure = !memory_protect_done; - memory_protect_done = true; -break ; -case SBI_HART_PROTECTION_TYPE_ID : - do_unconfigure = true; -break ; -default : -break ; - } -if (!do_unconfigure) +if (!__hart_protection_selected (hprot))continue ;__hart_protection_unconfigure (scratch, hprot, dom);@@ -167,24 +156,11 @@ int sbi_hart_protection_reconfigure(struct sbi_scratch *scratch,struct sbi_domain *current_dom, struct sbi_domain *next_dom) { - bool do_reconfigure, memory_protect_done = false; struct sbi_hart_protection *hprot;int ret;sbi_list_for_each_entry (hprot, &hart_protection_list, head) { - do_reconfigure = false; -switch (hprot->type) { -case SBI_HART_PROTECTION_TYPE_MEMORY : - do_reconfigure = !memory_protect_done; - memory_protect_done = true; -break ; -case SBI_HART_PROTECTION_TYPE_ID : - do_reconfigure = true; -break ; -default : -break ; - } -if (!do_reconfigure) +if (!__hart_protection_selected (hprot))continue ;__hart_protection_unconfigure (scratch, hprot, current_dom);@@ -198,7 +174,8 @@ int sbi_hart_protection_reconfigure(struct sbi_scratch *scratch,int sbi_hart_protection_temp_map_range (unsigned long base,unsigned long size) { -struct sbi_hart_protection *hprot =__hart_memory_protection_best (); +struct sbi_hart_protection *hprot = +__hart_memory_protection_best (SBI_HART_PROTECTION_MEMORY_CLASS_PMP );if (!hprot || !hprot->temp_map_range)return 0 ;@@ -208,7 +185,8 @@ int sbi_hart_protection_temp_map_range(unsigned long base, unsigned long size)int sbi_hart_protection_temp_unmap_range (unsigned long base,unsigned long size) { -struct sbi_hart_protection *hprot =__hart_memory_protection_best (); +struct sbi_hart_protection *hprot = +__hart_memory_protection_best (SBI_HART_PROTECTION_MEMORY_CLASS_PMP );if (!hprot || !hprot->temp_unmap_range)return 0 ; --2.53.0
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
RFC PATCH v7 3/6 lib: utils: fdt: Add helper to check for an extension in DT
In reply to Rahul PathakIn the early boot stage a platform may need to check for an extension in DT. This early stage can be before the sbi_hart_init is called which later parses all extensions available on the harts.
Add a new helper function for this detection.
--- include/sbi_utils/fdt/fdt_helper.h | 4 ++ lib/utils/fdt/fdt_helper.c | 61 +++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 14 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +4 -0
@@ -12,6 +12,7 @@#include <sbi/sbi_types.h> #include <sbi/sbi_domain.h> +#include <sbi/sbi_hart.h> struct fdt_match {const char *compatible;
@@ -56,6 +57,9 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);int fdt_parse_isa_extensions_all_harts (const void *fdt); +bool fdt_cpu_has_extension (const void *fdt,int cpu_offset, +enum sbi_hart_extensions ext); +int fdt_parse_gaisler_uart_node (const void *fdt,int nodeoffset,struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +47 -14
78 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 v7 4/6 riscv: Add Smsd (supervisor domain) hart extensions
In reply to Rahul PathakAdd hart extension identifiers for the RISC-V Smsd (supervisor domain) extension.
--- include/sbi/sbi_hart.h | 2 ++ lib/sbi/sbi_hart.c | 1 + 2 files changed, 3 insertions(+)
include/sbi/sbi_hart.h +2 -0
@@ -97,6 +97,8 @@ enum sbi_hart_extensions {SBI_HART_EXT_F ,/** Hart has D extension */ SBI_HART_EXT_D , +/** Hart has Smsd extension */ +SBI_HART_EXT_SMSD ,/** Maximum index of Hart extension */ SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +1 -0
@@ -355,6 +355,7 @@ 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 (smsd,SBI_HART_EXT_SMSD ), }; _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 v7 5/6 mpt: Add Smsd (supervisor domain) core
In reply to Rahul PathakIntroduce supervisor domain memory-protection (Smsd) 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 Smsd with SBI domains and other core layers.
--- include/sbi/sbi_hart_mpt.h | 314 +++++++++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_hart_mpt.c | 991 +++++++++++++++++++++++++++++++++++ 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 +314 -0
@@ -0,0 +1,314 @@ +/* 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> +#include <sbi/sbi_domain.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 + */ +/* Domain memory regions plus the root domain baseline and firmware lock */ +#define SBI_MPT_MAX_REGIONS_DOMAIN (SBI_DOMAIN_MEMREGION_MAX + 2U) + +/* + * Smsd 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 — MPT 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 MPT 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 +991 -0
991 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 v7 6/6 lib: sbi: Initialize Smsd during coldboot
In reply to Rahul PathakCall sbi_mpt_init() function from coldboot path of sbi_hart_init() to initialize the Smsd 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 | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-)
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; + +/* Smsd is optional. Continue if Smsd is not present. */ + rc =sbi_mpt_init (); +if (rc && rc !=SBI_ENODEV ) +return rc; }return sbi_hart_reinit (scratch);
platform/generic/platform.c +19 -4
@@ -33,7 +33,7 @@/* List of platform override modules generated at compile time */ extern const struct fdt_driver *const platform_override_modules[]; -static u32 fw_platform_calculate_heap_size (u32 hart_count) +static u32 fw_platform_calculate_heap_size (u32 hart_count,bool has_smsd) {u32 heap_size;
@@ -42,10 +42,21 @@ 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, only when a hart implements Smsd. +* 1 MiB memory for MPT allocated currently. +* +* TODO: Need better way to get the memory budget based on active +* MPT mode. +*/ +if (has_smsd) + heap_size +=1024 *1024 ; +return BIT_ALIGN (heap_size,HEAP_BASE_ALIGN ); } -static u32 fw_platform_get_heap_size (const void *fdt,u32 hart_count) +static u32 fw_platform_get_heap_size (const void *fdt,u32 hart_count, +bool has_smsd) {int chosen_offset, config_offset, len;const fdt32_t *val;
@@ -64,7 +75,7 @@ static u32 fw_platform_get_heap_size(const void *fdt, u32 hart_count)return BIT_ALIGN (fdt32_to_cpu (*val),HEAP_BASE_ALIGN ); default_config: -return fw_platform_calculate_heap_size (hart_count); +return fw_platform_calculate_heap_size (hart_count, has_smsd); }extern struct sbi_platform platform;
@@ -149,6 +160,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,int rc, root_offset, cpus_offset, cpu_offset, len;unsigned long cbom_block_size =0 ;unsigned long tmp =0 ; +bool has_smsd = false; root_offset =fdt_path_offset (fdt,"/" );if (root_offset <0 )
@@ -177,6 +189,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, generic_hart_index2id[hart_count++] = hartid; +if (fdt_cpu_has_extension (fdt, cpu_offset,SBI_HART_EXT_SMSD )) + has_smsd = true; + rc =fdt_parse_cbom_block_size (fdt, cpu_offset, &tmp);if (rc)continue ;
@@ -184,7 +199,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, } platform.hart_count = hart_count; - platform.heap_size =fw_platform_get_heap_size (fdt, hart_count); + platform.heap_size =fw_platform_get_heap_size (fdt, hart_count, has_smsd); platform_has_mlevel_imsic =fdt_check_imsic_mlevel (fdt); platform.cbom_block_size = cbom_block_size;
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi