-
Notifications
You must be signed in to change notification settings - Fork 177
Add helper function to enter unprivileged mode #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper function to enter unprivileged mode #608
Conversation
Avoids future unsafe-in-unsafe warning,.
The function pointer is branched to by assembly, so we're relying on a certain ABI.
https://github.com/thejpster/psp-example/blob/main/src/main.rs contains an example showing how to use it. |
Now offers both Priv and Unpriv modes, and has a handle to represent ownership of a static Stack object. The load/store check on `Stack::taken` is not perfectly thread safe, but it's probably good enough and doing better requires a critical-section or CAS atomics.
Updated the demo to match |
The asm is not Armv6-M compatible but inline(always) hides the issue and allows the code to build. See https://rust.godbolt.org/z/sYWMGah8b and #t-compiler > inline(always) caused inline assembly to not get checked |
Until I added another function that called the broken function anyway, then it suddently noticed the assembly was wrong |
Tested with https://github.com/thejpster/psp-example $ cargo run --bin unpriv_demo
Compiling cortex-m v0.7.7 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
Compiling cortex-m-rt v0.7.5 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
Compiling cortex-m-rt-macros v0.7.5 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
Compiling defmt-semihosting v0.3.0
Compiling psp-example v0.1.0 (/home/jonathan/Documents/github/thejpster/psp-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s
Running `/home/jonathan/Documents/github/thejpster/psp-example/./qemu-run.sh target/thumbv7em-none-eabihf/debug/unpriv_demo`
ELF_BINARY=target/thumbv7em-none-eabihf/debug/unpriv_demo
Running on '-cpu cortex-m4 -machine mps2-an386'...
------------------------------------------------------------------------
[INFO ] Using MSP. addr(x) = 203fffc4 (bin/unpriv_demo.rs:17)
[INFO ] PSP stack is at 20000000..20004000 (bin/unpriv_demo.rs:23)
[INFO ] Got SVCall, ptr=20003fec (bin/unpriv_demo.rs:62)
[INFO ] Got SVCall, ptr=20003ff0 (bin/unpriv_demo.rs:62)
[INFO ] Got SVCall, ptr=00000000 (bin/unpriv_demo.rs:62)
[INFO ] Got exit request, leaving now! (bin/unpriv_demo.rs:64)
------------------------------------------------------------------------
$ cargo run --bin priv_demo
Compiling psp-example v0.1.0 (/home/jonathan/Documents/github/thejpster/psp-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
Running `/home/jonathan/Documents/github/thejpster/psp-example/./qemu-run.sh target/thumbv7em-none-eabihf/debug/priv_demo`
ELF_BINARY=target/thumbv7em-none-eabihf/debug/priv_demo
Running on '-cpu cortex-m4 -machine mps2-an386'...
------------------------------------------------------------------------
[INFO ] Hello! (bin/priv_demo.rs:14)
[INFO ] Using MSP. addr(x) = 203fffc0 (bin/priv_demo.rs:15)
[INFO ] PSP stack is at 20000000..20004000 (bin/priv_demo.rs:20)
[INFO ] User mode, ptr=203fff74 (bin/priv_demo.rs:31)
------------------------------------------------------------------------ |
Turns out we don't check the assembly inside inline(asm) functions until the function is actually called (or referenced).
ef8164b
to
a951c1f
Compare
Fixed a bug, but had to make some functions const to do it. Also now sets the PSPLIM register. |
Also fixes the enter_privilged_function, which forgot to the change the mode so we didn't actually start using the PSP.
e3465c1
to
ecf5ddc
Compare
Adds a data type for managing a PSP stack, and a function to enter PSP mode using it