-
Notifications
You must be signed in to change notification settings - Fork 13k
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 support for cmse_nonsecure_entry attribute #75810
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M | ||
extension. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0775 | ||
#![feature(cmse_nonsecure_entry)] | ||
|
||
#[cmse_nonsecure_entry] | ||
pub extern "C" fn entry_function() {} | ||
``` | ||
|
||
To fix this error, compile your code for a Rust target that supports the | ||
TrustZone-M extension. The current possible targets are: | ||
* `thumbv8m.main-none-eabi` | ||
* `thumbv8m.main-none-eabihf` | ||
* `thumbv8m.base-none-eabi` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
`#[cmse_nonsecure_entry]` functions require a C ABI | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0776 | ||
#![feature(cmse_nonsecure_entry)] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
pub fn entry_function(input: Vec<u32>) {} | ||
``` | ||
|
||
To fix this error, declare your entry function with a C ABI, using `extern "C"`. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
81 changes: 81 additions & 0 deletions
81
src/doc/unstable-book/src/language-features/cmse-nonsecure-entry.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# `cmse_nonsecure_entry` | ||
|
||
The tracking issue for this feature is: [#75835] | ||
|
||
[#75835]: https://github.com/rust-lang/rust/issues/75835 | ||
|
||
------------------------ | ||
|
||
The [TrustZone-M | ||
feature](https://developer.arm.com/documentation/100690/latest/) is available | ||
for targets with the Armv8-M architecture profile (`thumbv8m` in their target | ||
name). | ||
LLVM, the Rust compiler and the linker are providing | ||
[support](https://developer.arm.com/documentation/ecm0359818/latest/) for the | ||
TrustZone-M feature. | ||
|
||
One of the things provided, with this unstable feature, is the | ||
`cmse_nonsecure_entry` attribute. This attribute marks a Secure function as an | ||
entry function (see [section | ||
5.4](https://developer.arm.com/documentation/ecm0359818/latest/) for details). | ||
With this attribute, the compiler will do the following: | ||
* add a special symbol on the function which is the `__acle_se_` prefix and the | ||
standard function name | ||
* constrain the number of parameters to avoid using the Non-Secure stack | ||
jonas-schievink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* before returning from the function, clear registers that might contain Secure | ||
information | ||
* use the `BXNS` instruction to return | ||
|
||
Because the stack can not be used to pass parameters, there will be compilation | ||
errors if: | ||
* the total size of all parameters is too big (for example more than four 32 | ||
bits integers) | ||
* the entry function is not using a C ABI | ||
|
||
The special symbol `__acle_se_` will be used by the linker to generate a secure | ||
gateway veneer. | ||
|
||
<!-- NOTE(ignore) this example is specific to thumbv8m targets --> | ||
|
||
``` rust,ignore | ||
#![feature(cmse_nonsecure_entry)] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
pub extern "C" fn entry_function(input: u32) -> u32 { | ||
input + 6 | ||
} | ||
``` | ||
|
||
``` text | ||
$ rustc --emit obj --crate-type lib --target thumbv8m.main-none-eabi function.rs | ||
$ arm-none-eabi-objdump -D function.o | ||
|
||
00000000 <entry_function>: | ||
0: b580 push {r7, lr} | ||
2: 466f mov r7, sp | ||
4: b082 sub sp, #8 | ||
6: 9001 str r0, [sp, #4] | ||
8: 1d81 adds r1, r0, #6 | ||
a: 460a mov r2, r1 | ||
c: 4281 cmp r1, r0 | ||
e: 9200 str r2, [sp, #0] | ||
10: d30b bcc.n 2a <entry_function+0x2a> | ||
12: e7ff b.n 14 <entry_function+0x14> | ||
14: 9800 ldr r0, [sp, #0] | ||
16: b002 add sp, #8 | ||
18: e8bd 4080 ldmia.w sp!, {r7, lr} | ||
1c: 4671 mov r1, lr | ||
1e: 4672 mov r2, lr | ||
20: 4673 mov r3, lr | ||
22: 46f4 mov ip, lr | ||
24: f38e 8800 msr CPSR_f, lr | ||
28: 4774 bxns lr | ||
2a: f240 0000 movw r0, #0 | ||
2e: f2c0 0000 movt r0, #0 | ||
32: f240 0200 movw r2, #0 | ||
36: f2c0 0200 movt r2, #0 | ||
3a: 211c movs r1, #28 | ||
3c: f7ff fffe bl 0 <_ZN4core9panicking5panic17h5c028258ca2fb3f5E> | ||
40: defe udf #254 ; 0xfe | ||
``` |
Submodule llvm-project
updated
2 files
+16 −3 | llvm/lib/Target/ARM/ARMISelLowering.cpp | |
+74 −0 | llvm/test/CodeGen/ARM/cmse-errors.ll |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// gate-test-cmse_nonsecure_entry | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
//~^ ERROR [E0775] | ||
//~| ERROR [E0658] | ||
pub extern "C" fn entry_function(input: u32) -> u32 { | ||
input + 6 | ||
} | ||
|
||
fn main() {} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0658]: the `#[cmse_nonsecure_entry]` attribute is an experimental feature | ||
--> $DIR/gate_test.rs:4:1 | ||
| | ||
LL | #[cmse_nonsecure_entry] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #75835 <https://github.com/rust-lang/rust/issues/75835> for more information | ||
= help: add `#![feature(cmse_nonsecure_entry)]` to the crate attributes to enable | ||
|
||
error[E0775]: `#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension | ||
--> $DIR/gate_test.rs:4:1 | ||
| | ||
LL | #[cmse_nonsecure_entry] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0658, E0775. | ||
For more information about an error, try `rustc --explain E0658`. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// build-pass | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(cmse_nonsecure_entry)] | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32) -> u32 { | ||
a + b + c + d | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(cmse_nonsecure_entry)] | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32, e: u32) -> u32 { //~ ERROR | ||
a + b + c + d + e | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: <unknown>:0:0: in function entry_function i32 (i32, i32, i32, i32, i32): secure entry function requires arguments on stack | ||
jonas-schievink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
error: aborting due to previous error | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// ignore-thumbv8m.main-none-eabi | ||
#![feature(cmse_nonsecure_entry)] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] //~ ERROR [E0775] | ||
pub extern "C" fn entry_function(input: u32) -> u32 { | ||
input + 6 | ||
} | ||
|
||
fn main() {} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0775]: `#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension | ||
--> $DIR/trustzone-only.rs:5:1 | ||
| | ||
LL | #[cmse_nonsecure_entry] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0775`. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(cmse_nonsecure_entry)] | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
#[cmse_nonsecure_entry] | ||
pub fn entry_function(a: u32, b: u32, c: u32, d: u32) -> u32 { //~ ERROR [E0776] | ||
a + b + c + d | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0776]: `#[cmse_nonsecure_entry]` functions require C ABI | ||
--> $DIR/wrong-abi.rs:7:1 | ||
| | ||
LL | #[cmse_nonsecure_entry] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0776`. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean the function's name will be mangled differently or is this an aliased or additional symbol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is an additional symbol. So that if you set this attribute on a function named
toto
, the compiler will generate the following assembly:and the
toto
symbol will be set as weak. That way the linker can create the veneer like so:and the symbol
toto
and its address is passed to NS.This is explained section 3.4.4 in here.
I don't think it modifies the mangling. However, as you can see in the examples in this PR, I believe you would always use the
no_mangle
andextern "C"
ABI on the entry functions as they go through FFI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think we should definitely forbid using this attribute with non-
"C"
ABI functions then. Since the Rust ABI is unstable, it can pass arguments either directly in registers, or in memory, and that would be a problem here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that makes sense, I am looking if there is a way to check what the ABI of the function compiled is.
[edit]: and add tests with and without the C ABI as well!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was easier than anticipated 😃 Added a new error code for that.