Description
This is a tracking issue for the PR #81346.
The feature gate for the issue is #![feature(abi_c_cmse_nonsecure_call)]
.
Description
The TrustZone-M feature 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 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 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.
Example
#![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()
}
$ 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}
Steps
- Initial implementation: Add a new ABI to support cmse_nonsecure_call #81346ABI compliance should be checked by rustc instead of LLVM: CMSE features should check ABI conformance in rustc, not LLVM #81347
Activity
nagisa commentedon Jan 26, 2021
What's the name of the actual underlying calling convention? Is it AAPCS? I think the extern name should contain it too somehow.
hug-dev commentedon Jan 26, 2021
It will use the C convention ultimately, the current implementation maps it to
llvm::CCallConv
(I guess similar than AAPCS as this feature is only available on Arm processors?).Do you mean that because it might be possible to have the
cmse_nonsecure_call
feature available for other ABIs as well?I think restricting it to only ever use the C ABI is not a bad thing: this is used to switch to functions that are defined in other executable files that could have been written in any programming language.
nagisa commentedon Jan 26, 2021
Well, what I really want is for the underlying ABI to be explicit in the ABI string, whatever it is, so that it is more obvious that a transmute as given in the example above is… valid. It would also, as you mention, enable us to add non-secure options for other calling conventions if necessary.
So a couple of proposals:
C_cmse_nonsecure
orcmse_nonsecure_C
.hug-dev commentedon Jan 26, 2021
Ok makes sense! Will modify the implementation PR and this with the
C
in front:extern "C-cmse-nonsecure-call"
.[-]Tracking Issue for the cmse-nonsecure-call ABI[/-][+]Tracking Issue for the C-cmse-nonsecure-call ABI[/+]C-cmse-nonsecure-call
: improved error messages #127814Rollup merge of rust-lang#127814 - folkertdev:c-cmse-nonsecure-call-e…
Unrolled build for rust-lang#127814
C-cmse-nonsecure-call
to list of ABIs rust-lang/reference#1551folkertdev commentedon Jul 30, 2024
Request for Stabilization
Summary
We propose to stabilize the
C-cmse-nonsecure-call
ABI. It can only be used in function pointer types, never in an actual *extern "C-cmse-nonsecure-call" {}
block. Such function pointers are typically received via FFI.Usage example
https://godbolt.org/z/KT6hc5Y7W
Which produces this LLVM IR:
Which produces this assembly:
Error messages
The calling requirements for this ABI are checked within rustc to produce good error messsages.
Produces the following errors:
Documentation
C-cmse-nonsecure-call
to list of ABIs reference#1551Tests
Test cases are in
https://github.com/rust-lang/rust/tree/master/tests/ui/cmse-nonsecure/cmse-nonsecure-call
extern
blocks orextern fn
definitions (and therefore is only allowed in function pointers)The tests in the (as yet unmerged) PR for cmse-nonsecure-entry validate the assembly output of this ABI.
C examples
Clang: https://godbolt.org/z/7ch3xcz96
GCC: https://godbolt.org/z/16arxab5x
tdittr commentedon Jul 30, 2024
This tracking issues seems to be missing some tags compared to #75835
@rustbot label +T-compiler +T-lang +A-codegen
26 remaining items