@@ -195,6 +195,13 @@ pub unsafe fn semihosting_syscall(nr: u32, arg: u32) -> u32 {
195195#[ cfg( cortex_m) ]
196196#[ inline( always) ]
197197pub unsafe fn enter_unprivileged_psp ( psp : * const u32 , entry : extern "C" fn ( ) -> !) -> ! {
198+ use crate :: register:: control:: { Control , Npriv , Spsel } ;
199+ const CONTROL_FLAGS : u32 = {
200+ let mut control = Control :: from_bits ( 0 ) ;
201+ control. set_npriv ( Npriv :: Unprivileged ) ;
202+ control. set_spsel ( Spsel :: Psp ) ;
203+ control. bits ( )
204+ } ;
198205 unsafe {
199206 core:: arch:: asm!(
200207 "msr PSP, {psp}" ,
@@ -204,7 +211,7 @@ pub unsafe fn enter_unprivileged_psp(psp: *const u32, entry: extern "C" fn() ->
204211 "isb" ,
205212 "bx {ent}" ,
206213 tmp = in( reg) 0 ,
207- flags = in( reg) 3 ,
214+ flags = in( reg) CONTROL_FLAGS ,
208215 psp = in( reg) psp,
209216 ent = in( reg) entry,
210217 options( noreturn, nostack)
@@ -230,11 +237,23 @@ pub unsafe fn enter_unprivileged_psp(psp: *const u32, entry: extern "C" fn() ->
230237#[ cfg( cortex_m) ]
231238#[ inline( always) ]
232239pub unsafe fn enter_privileged_psp ( psp : * const u32 , entry : extern "C" fn ( ) -> !) -> ! {
240+ use crate :: register:: control:: { Control , Npriv , Spsel } ;
241+ const CONTROL_FLAGS : u32 = {
242+ let mut control = Control :: from_bits ( 0 ) ;
243+ control. set_npriv ( Npriv :: Privileged ) ;
244+ control. set_spsel ( Spsel :: Psp ) ;
245+ control. bits ( )
246+ } ;
233247 unsafe {
234248 core:: arch:: asm!(
235- "msr PSP, {psp}" ,
249+ "msr PSP, {psp}" ,
250+ "mrs {tmp}, CONTROL" ,
251+ "orrs {tmp}, {flags}" ,
252+ "msr CONTROL, {tmp}" ,
236253 "isb" ,
237- "bx {ent}" ,
254+ "bx {ent}" ,
255+ tmp = in( reg) 0 ,
256+ flags = in( reg) CONTROL_FLAGS ,
238257 psp = in( reg) psp,
239258 ent = in( reg) entry,
240259 options( noreturn, nostack)
0 commit comments