-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new ABI to support cmse_nonsecure_call
This commit adds a new ABI to be selected via `extern "C-cmse-nonsecure-call"` on function pointers in order for the compiler to apply the corresponding cmse_nonsecure_call callsite attribute. For Armv8-M targets supporting TrustZone-M, this will perform a non-secure function call by saving, clearing and calling a non-secure function pointer using the BLXNS instruction. See the page on the unstable book for details. Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
- Loading branch information
Showing
35 changed files
with
251 additions
and
3 deletions.
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,12 @@ | ||
The `C-cmse-nonsecure-call` ABI can only be used with function pointers. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0781 | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
pub extern "C-cmse-nonsecure-call" fn test() {} | ||
``` | ||
|
||
The `C-cmse-nonsecure-call` ABI should be used by casting function pointers to | ||
specific addresses. |
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 |
---|---|---|
|
@@ -551,6 +551,7 @@ pub enum Conv { | |
|
||
// Target-specific calling conventions. | ||
ArmAapcs, | ||
CCmseNonSecureCall, | ||
|
||
Msp430Intr, | ||
|
||
|
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
88 changes: 88 additions & 0 deletions
88
src/doc/unstable-book/src/language-features/abi-c-cmse-nonsecure-call.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,88 @@ | ||
# `abi_c_cmse_nonsecure_call` | ||
|
||
The tracking issue for this feature is: [#81391] | ||
|
||
[#81391]: https://github.com/rust-lang/rust/issues/81391 | ||
|
||
------------------------ | ||
|
||
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 | ||
`C-cmse-nonsecure-call` function ABI. This ABI is used on function pointers to | ||
non-secure code to mark a non-secure function call (see [section | ||
5.5](https://developer.arm.com/documentation/ecm0359818/latest/) for details). | ||
|
||
With this ABI, the compiler will do the following to perform the call: | ||
* save registers needed after the call to Secure memory | ||
* clear all registers that might contain confidential information | ||
* clear the Least Significant Bit of the function address | ||
* branches using the BLXNS instruction | ||
|
||
To avoid using the non-secure stack, the compiler will constrain the number and | ||
type of parameters/return value. | ||
|
||
The `extern "C-cmse-nonsecure-call"` ABI is otherwise equivalent to the | ||
`extern "C"` ABI. | ||
|
||
<!-- NOTE(ignore) this example is specific to thumbv8m targets --> | ||
|
||
``` rust,ignore | ||
#![no_std] | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
#[no_mangle] | ||
pub fn call_nonsecure_function(addr: usize) -> u32 { | ||
let non_secure_function = | ||
unsafe { core::mem::transmute::<usize, extern "C-cmse-nonsecure-call" fn() -> u32>(addr) }; | ||
non_secure_function() | ||
} | ||
``` | ||
|
||
``` text | ||
$ rustc --emit asm --crate-type lib --target thumbv8m.main-none-eabi function.rs | ||
call_nonsecure_function: | ||
.fnstart | ||
.save {r7, lr} | ||
push {r7, lr} | ||
.setfp r7, sp | ||
mov r7, sp | ||
.pad #16 | ||
sub sp, #16 | ||
str r0, [sp, #12] | ||
ldr r0, [sp, #12] | ||
str r0, [sp, #8] | ||
b .LBB0_1 | ||
.LBB0_1: | ||
ldr r0, [sp, #8] | ||
push.w {r4, r5, r6, r7, r8, r9, r10, r11} | ||
bic r0, r0, #1 | ||
mov r1, r0 | ||
mov r2, r0 | ||
mov r3, r0 | ||
mov r4, r0 | ||
mov r5, r0 | ||
mov r6, r0 | ||
mov r7, r0 | ||
mov r8, r0 | ||
mov r9, r0 | ||
mov r10, r0 | ||
mov r11, r0 | ||
mov r12, r0 | ||
msr apsr_nzcvq, r0 | ||
blxns r0 | ||
pop.w {r4, r5, r6, r7, r8, r9, r10, r11} | ||
str r0, [sp, #4] | ||
b .LBB0_2 | ||
.LBB0_2: | ||
ldr r0, [sp, #4] | ||
add sp, #16 | ||
pop {r7, pc} | ||
``` |
11 changes: 11 additions & 0 deletions
11
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/gate_test.rs
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-abi_c_cmse_nonsecure_call | ||
fn main() { | ||
let non_secure_function = unsafe { | ||
core::mem::transmute::<usize, extern "C-cmse-nonsecure-call" fn(i32, i32, i32, i32) -> i32>( | ||
//~^ ERROR [E0658] | ||
0x10000004, | ||
) | ||
}; | ||
let mut toto = 5; | ||
toto += non_secure_function(toto, 2, 3, 5); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/gate_test.stderr
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,12 @@ | ||
error[E0658]: C-cmse-nonsecure-call ABI is experimental and subject to change | ||
--> $DIR/gate_test.rs:4:46 | ||
| | ||
LL | core::mem::transmute::<usize, extern "C-cmse-nonsecure-call" fn(i32, i32, i32, i32) -> i32>( | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #81391 <https://github.com/rust-lang/rust/issues/81391> for more information | ||
= help: add `#![feature(abi_c_cmse_nonsecure_call)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
15 changes: 15 additions & 0 deletions
15
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-registers.rs
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,15 @@ | ||
// build-pass | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
pub fn test(a: u32, b: u32, c: u32, d: u32) -> u32 { | ||
let non_secure_function = unsafe { | ||
core::mem::transmute::<usize, extern "C-cmse-nonsecure-call" fn(u32, u32, u32, u32) -> u32>( | ||
0x10000004, | ||
) | ||
}; | ||
non_secure_function(a, b, c, d) | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack.rs
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 @@ | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
pub fn test(a: u32, b: u32, c: u32, d: u32, e: u32) -> u32 { | ||
let non_secure_function = unsafe { | ||
core::mem::transmute::< | ||
usize, | ||
extern "C-cmse-nonsecure-call" fn(u32, u32, u32, u32, u32) -> u32> | ||
( | ||
0x10000004, | ||
) | ||
}; | ||
non_secure_function(a, b, c, d, e) | ||
} |
5 changes: 5 additions & 0 deletions
5
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack.stderr
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 test i32 (i32, i32, i32, i32, i32): call to non-secure function would require passing arguments on stack | ||
|
||
|
||
error: aborting due to previous error | ||
|
6 changes: 6 additions & 0 deletions
6
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/wrong-abi-location-1.rs
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,6 @@ | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
#![no_std] | ||
|
||
pub extern "C-cmse-nonsecure-call" fn test() {} //~ ERROR [E0781] |
9 changes: 9 additions & 0 deletions
9
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/wrong-abi-location-1.stderr
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[E0781]: the `"cmse-nonsecure-call"` ABI is only allowed on function pointers. | ||
--> $DIR/wrong-abi-location-1.rs:6:1 | ||
| | ||
LL | pub extern "C-cmse-nonsecure-call" fn test() {} //~ ERROR [E0781] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0781`. |
8 changes: 8 additions & 0 deletions
8
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/wrong-abi-location-2.rs
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,8 @@ | ||
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
// only-thumbv8m.main-none-eabi | ||
#![feature(abi_c_cmse_nonsecure_call)] | ||
#![no_std] | ||
|
||
extern "C-cmse-nonsecure-call" { //~ ERROR [E0781] | ||
fn test(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/ui/cmse-nonsecure/cmse-nonsecure-call/wrong-abi-location-2.stderr
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 @@ | ||
error[E0781]: the `"C-cmse-nonsecure-call"` ABI is only allowed on function pointers. | ||
--> $DIR/wrong-abi-location-2.rs:6:1 | ||
| | ||
LL | / extern "C-cmse-nonsecure-call" { | ||
LL | | fn test(); //~ ERROR [E0781] | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0781`. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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