[PATCH v5 4/9] lib: sbi_hart: lock mwid CSR for RoT immutability
Anup Patel
anup at brainfault.org
Sun Nov 22 19:35:20 PST 2026
On Mon, Nov 23, 2026 at 7:22 AM Yu-Chien Peter Lin <peter.lin at sifive.com> wrote:
>
> Hi Anup,
>
> On Thu, Nov 19, 2026 at 06:33:23PM +0530, Anup Patel wrote:
> > On Wed, Nov 18, 2026 at 7:30 AM Yu-Chien Peter Lin <peter.lin at sifive.com> wrote:
> > >
> > > Hi Anup,
> > >
> > > On Mon, Nov 16, 2026 at 06:49:50PM +0530, Anup Patel wrote:
> > > > On Tue, Nov 10, 2026 at 3:20 PM Yu-Chien Peter Lin <peter.lin at sifive.com> wrote:
> > > > >
> > > > > Lock the M-mode World ID (mwid) CSR during hart re-initialization
> > > > > to enforce immutability of the WID established by the root-of-trust.
> > > > >
> > > > > OpenSBI does not assign the WID value itself; it only sets MWID_LOCK
> > > > > to freeze the value established by prior RoT stage. The MWID_LOCK bit
> > > > > at XLEN-1 is sticky and makes the CSR read-only until reset, enforcing
> > > > > a temporal security boundary per the RISC-V Worlds specification.
> > > > >
> > > > > Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
> > > > > ---
> > > > > Changes v4->v5:
> > > > > - Move the MWID lock out of sbi_hart_reinit() into smwid_hart_init()
> > > > > - Drop RB-tags from Oza and Anup
> > > > > ---
> > > > > lib/sbi/sbi_hart.c | 12 ++++++++++++
> > > > > 1 file changed, 12 insertions(+)
> > > > >
> > > > > diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
> > > > > index d86abf07..a721bef4 100644
> > > > > --- a/lib/sbi/sbi_hart.c
> > > > > +++ b/lib/sbi/sbi_hart.c
> > > > > @@ -469,6 +469,16 @@ static int hart_mhpm_get_allowed_bits(void)
> > > > > return num_bits;
> > > > > }
> > > > >
> > > > > +static void smwid_hart_init(struct sbi_scratch *scratch)
> > > > > +{
> > > > > + /*
> > > > > + * Assume MWID is restored by root-of-trust M-mode in previous
> > > > > + * stage. Lock mwid so RoT-defined WID remains immutable.
> > > > > + */
> > > > > + if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMWID))
> > > > > + csr_set(CSR_MWID, MWID_LOCK);
> > > >
> > > > If pmwid is available then we should set pmwid in MWID CSR
> > > > before locking it. This will help platforms where OpenSBI boots
> > > > first without any prior booting stage.
> > > >
> > > > What do you think ?
> > >
> > > Thanks for the suggestion. I'd prefer lock-only, because writing pmwid before the
> > > lock is either a no-op or can undo an earlier stage's decision.
> > >
> > > The spec says "On-reset, the mwid CSR is expected to show the platform-defined pmwid
> > > value", so if OpenSBI is the first stage, mwid already equals pmwid and the write
> > > changes nothing.
> > >
> > > It only changes something when mwid != pmwid, which means an earlier stage has already
> > > programmed mwid. Example with nWorlds=16, trusted WID = 15:
> > >
> > > 1. Out of reset, mwid = pmwid = 15. The RoT runs in the trusted WID.
> > > 2. The RoT configures the wgCheckers so that WID 3 can only access OpenSBI's
> > > RAM/devices and cannot touch the wgChecker configuration registers.
> > > - The RoT writes mwid = 3 to demote runtime M-mode, but leaves the lock
> > > bit unset and hands over to OpenSBI.
> > > 4. OpenSBI writes pmwid (15) to mwid, then sets the lock bit.
> > > 5. OpenSBI is now back in the trusted WID, and locked there until the next reset.
> > > It can reconfigure the wgCheckers and the RoT's demotion is undone.
> > >
> > > Given the case above, I think we should keep mwid untouched.
> >
> > At least, check the mwid in CSR with pmwid when pmwid is available in DT.
> > If pmwid is not available in DT then leave mwid untouched.
>
> - If pmwid is in DT and mwid (without the lock bit) != pmwid, print
> a info message showing pmwid value.
I would suggest print a warning in this case.
> - In all cases, only set the lock bit and never modify the mwid.
This is fine.
Regards,
Anup
More information about the opensbi
mailing list