Skip to content

Instantly share code, notes, and snippets.

@sam-falvo
Created October 30, 2016 05:34
Show Gist options
  • Save sam-falvo/6d18be9fa564edf7df2fec5d6d1c7da2 to your computer and use it in GitHub Desktop.
Save sam-falvo/6d18be9fa564edf7df2fec5d6d1c7da2 to your computer and use it in GitHub Desktop.
SIE in software for RISC-V?
The first thing we need to do when invoking SIE is save the current thread state.
The invokING thread called us via an ECALL, so some of this state is in mstatus and mepc.
So, save it.
_SIE: ; A0 -> SIEBK to invoke.
csrrw t0, mscratch, x0 ; mscratch = t0, t0 = pointer to current SIEBK.
sd x1, _X1(t0)
sd x2, _X2(t0)
...
sd x31, _X31(t0)
addi x1, t0, 0
sd x1, _X5(t0) ; or whatever it maps to; I forget off-hand.
csrrw x1, mstatus, x0 ; Get current mstatus
sd x1, _MSTATUS(t0)
csrrw x1, mepc, x0
sd x1, _MEPC(t0)
; save other CSRs here, such as page tables, and whatnot.
Now that we have our current thread state saved, we need to switch to the new SIEBK.
Because exceptions in the invokED task will cause a return to the invokING task,
we need to establish the back-link.
sd t0, _ReturnLink(a0)
addi t0, a0, 0 ; Assume the new task identity
; ... reload important CSRs here in reverse order.
ld x1, _MEPC(t0)
csrrw x0, mepc, x1
ld x1, _MSTATUS(t0)
csrrw x0, mstatus, x1 ; Pray this doesn't put us into a bizarre state.
ld x1, _X5(t0)
csrrw x0, mscratch, x1
ld x31, _X31(t0)
ld x30, _X30(t0)
....
ld x2, _X2(t0)
ld x1, _X1(t0)
csrrw t0, mscratch, t0
mret ; BOOM, start running new VM thread here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment