Add helper function to enter unprivileged mode - #608
Merged
Merged
Conversation
Avoids future unsafe-in-unsafe warning,.
The function pointer is branched to by assembly, so we're relying on a certain ABI.
Contributor
Author
|
https://github.com/thejpster/psp-example/blob/main/src/main.rs contains an example showing how to use it. |
adamgreig
reviewed
Sep 13, 2025
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.
Contributor
Author
|
Updated the demo to match |
Contributor
Author
|
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 |
Contributor
Author
|
Until I added another function that called the broken function anyway, then it suddently noticed the assembly was wrong |
Contributor
Author
|
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)
------------------------------------------------------------------------ |
diondokter
reviewed
Oct 2, 2025
Turns out we don't check the assembly inside inline(asm) functions until the function is actually called (or referenced).
thejpster
force-pushed
the
add-unprivileged-mode
branch
from
October 3, 2025 16:11
ef8164b to
a951c1f
Compare
Contributor
Author
|
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.
thejpster
force-pushed
the
add-unprivileged-mode
branch
from
October 3, 2025 17:15
e3465c1 to
ecf5ddc
Compare
adamgreig
requested changes
Oct 6, 2025
adamgreig
approved these changes
Oct 6, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a data type for managing a PSP stack, and a function to enter PSP mode using it