forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#75810 - hug-dev:cmse-nonsecure-entry, r=jonas…
…-schievink Add support for cmse_nonsecure_entry attribute This pull request adds the `cmse_nonsecure_entry` attribute under an unstable feature. I was not sure if it was fine for me to send directly the pull-request or if I should submit a RFC first. I was told on Zulip that it was fine to do so but please close it if I need first submit a RFC or follow another process instead. The `cmse_nonsecure_entry` attribute is a LLVM attribute that will be available in LLVM 11. I plan to rebase on the [upgrade PR](rust-lang#73526) once merged to make this one compile. This attribute modifies code generation of the function as explained [here](https://developer.arm.com/documentation/ecm0359818/latest/) to make it work with the TrustZone-M hardware feature. This feature is only available on `thumbv8m` targets so I created an error for that if one tries to use this attribute for another target. I added this attribute in Rust as any other LLVM attribute are added but since this one is target-dependent I am not sure if it was the best thing to do. Please indicate me if you think of other ways, like isolating target-dependent attributes together. ---------------- Tracking issue: rust-lang#75835
- Loading branch information
Showing
25 changed files
with
254 additions
and
1 deletion.
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 | ||
* 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 | ||
|
||
|
||
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`. |