Skip to content

Commit 1aaafac

Browse files
committed
Add support for cmse_nonsecure_entry attribute
This patch adds support for the LLVM cmse_nonsecure_entry attribute. This is a target-dependent attribute that only has sense for the thumbv8m Rust targets. You can find more information about this attribute here: https://developer.arm.com/documentation/ecm0359818/latest/ Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
1 parent d255d70 commit 1aaafac

File tree

15 files changed

+154
-1
lines changed

15 files changed

+154
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
294294
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
295295
Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
296296
}
297+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
298+
llvm::AddFunctionAttrString(llfn, Function, const_cstr!("cmse_nonsecure_entry"));
299+
}
297300
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
298301

299302
// Always annotate functions with the target-cpu they are compiled for.

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ pub fn AddFunctionAttrStringValue(llfn: &'a Value, idx: AttributePlace, attr: &C
3737
}
3838
}
3939

40+
pub fn AddFunctionAttrString(llfn: &'a Value, idx: AttributePlace, attr: &CStr) {
41+
unsafe {
42+
LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), std::ptr::null())
43+
}
44+
}
45+
4046
#[derive(Copy, Clone)]
4147
pub enum AttributePlace {
4248
ReturnValue,

compiler/rustc_error_codes/src/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ E0770: include_str!("./error_codes/E0770.md"),
458458
E0771: include_str!("./error_codes/E0771.md"),
459459
E0773: include_str!("./error_codes/E0773.md"),
460460
E0774: include_str!("./error_codes/E0774.md"),
461+
E0775: include_str!("./error_codes/E0775.md"),
461462
;
462463
// E0006, // merged with E0005
463464
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M
2+
extension.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0775
7+
#![feature(cmse_nonsecure_entry)]
8+
9+
#[cmse_nonsecure_entry]
10+
fn toto() {}
11+
```
12+
13+
To fix this error, compile your code for a Rust target that supports the
14+
TrustZone-M extension. The current possible targets are:
15+
* `thumbv8m.main-none-eabi`
16+
* `thumbv8m.main-none-eabihf`
17+
* `thumbv8m.base-none-eabi`

compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ declare_features! (
590590
/// Allows using and casting function pointers in a `const fn`.
591591
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
592592

593+
/// Allows to use the `#[cmse_nonsecure_entry]` attribute.
594+
(active, cmse_nonsecure_entry, "1.48.0", Some(75835), None),
595+
593596
// -------------------------------------------------------------------------
594597
// feature-group-end: actual feature gates
595598
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
349349
experimental!(register_tool),
350350
),
351351

352+
gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
353+
352354
// ==========================================================================
353355
// Internal attributes: Stability, deprecation, and unsafe:
354356
// ==========================================================================

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ bitflags! {
7979
/// #[ffi_const]: applies clang's `const` attribute to a foreign function
8080
/// declaration.
8181
const FFI_CONST = 1 << 13;
82+
/// #[cmse_nonsecure_entry]: with a TrustZone-M extension, declare a
83+
/// function as an entry function from Non-Secure code.
84+
const CMSE_NONSECURE_ENTRY = 1 << 14;
8285
}
8386
}
8487

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ symbols! {
337337
closure_to_fn_coercion,
338338
cmp,
339339
cmpxchg16b_target_feature,
340+
cmse_nonsecure_entry,
340341
coerce_unsized,
341342
cold,
342343
column,

compiler/rustc_typeck/src/collect.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,12 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25432543
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
25442544
} else if tcx.sess.check_name(attr, sym::used) {
25452545
codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
2546+
} else if tcx.sess.check_name(attr, sym::cmse_nonsecure_entry) {
2547+
if !tcx.sess.target.target.llvm_target.contains("thumbv8m") {
2548+
struct_span_err!(tcx.sess, attr.span, E0775, "`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension")
2549+
.emit();
2550+
}
2551+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY;
25462552
} else if tcx.sess.check_name(attr, sym::thread_local) {
25472553
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
25482554
} else if tcx.sess.check_name(attr, sym::track_caller) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# `cmse_nonsecure_entry`
2+
3+
The tracking issue for this feature is: [#75835]
4+
5+
[#75835]: https://github.com/rust-lang/rust/issues/75835
6+
7+
------------------------
8+
9+
The [TrustZone-M
10+
feature](https://developer.arm.com/documentation/100690/latest/) is available
11+
for targets with the Armv8-M architecture profile (`thumbv8m` in their target
12+
name).
13+
LLVM, the Rust compiler and the linker are providing
14+
[support](https://developer.arm.com/documentation/ecm0359818/latest/) for the
15+
TrustZone-M feature.
16+
17+
One of the things provided, with this unstable feature, is the
18+
`cmse_nonsecure_entry` attribute. This attribute marks a Secure function as an
19+
entry function (see [section
20+
5.4](https://developer.arm.com/documentation/ecm0359818/latest/) for details).
21+
With this attribute, the compiler will do the following:
22+
* add a special symbol on the function which is the `__acle_se_` prefix and the
23+
standard function name
24+
* constrain the number of parameters to avoid using the Non-Secure stack
25+
* before returning from the function, clear registers that might contain Secure
26+
information
27+
* use the `BXNS` instruction to return
28+
29+
The special symbol `__acle_se_` will be used by the linker to generate a secure
30+
gateway veneer.
31+
32+
<!-- NOTE(ignore) this example is specific to thumbv8m targets -->
33+
34+
``` rust,ignore
35+
#![feature(cmse_nonsecure_entry)]
36+
37+
#[no_mangle]
38+
#[cmse_nonsecure_entry]
39+
pub extern "C" fn entry_function(input: u32) -> u32 {
40+
input + 6
41+
}
42+
```
43+
44+
``` text
45+
$ rustc --emit obj --crate-type lib --target thumbv8m.main-none-eabi function.rs
46+
$ arm-none-eabi-objdump -D function.o
47+
48+
00000000 <entry_function>:
49+
0: b580 push {r7, lr}
50+
2: 466f mov r7, sp
51+
4: b082 sub sp, #8
52+
6: 9001 str r0, [sp, #4]
53+
8: 1d81 adds r1, r0, #6
54+
a: 460a mov r2, r1
55+
c: 4281 cmp r1, r0
56+
e: 9200 str r2, [sp, #0]
57+
10: d30b bcc.n 2a <entry_function+0x2a>
58+
12: e7ff b.n 14 <entry_function+0x14>
59+
14: 9800 ldr r0, [sp, #0]
60+
16: b002 add sp, #8
61+
18: e8bd 4080 ldmia.w sp!, {r7, lr}
62+
1c: 4671 mov r1, lr
63+
1e: 4672 mov r2, lr
64+
20: 4673 mov r3, lr
65+
22: 46f4 mov ip, lr
66+
24: f38e 8800 msr CPSR_f, lr
67+
28: 4774 bxns lr
68+
2a: f240 0000 movw r0, #0
69+
2e: f2c0 0000 movt r0, #0
70+
32: f240 0200 movw r2, #0
71+
36: f2c0 0200 movt r2, #0
72+
3a: 211c movs r1, #28
73+
3c: f7ff fffe bl 0 <_ZN4core9panicking5panic17h5c028258ca2fb3f5E>
74+
40: defe udf #254 ; 0xfe
75+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// gate-test-cmse_nonsecure_entry
2+
// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib
3+
// only-thumbv8m.main-none-eabi
4+
#![feature(cmse_nonsecure_entry)]
5+
#![no_std]
6+
7+
#[no_mangle]
8+
#[cmse_nonsecure_entry]
9+
pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32, e: u32) -> u32 {
10+
a + b + c + d + e
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: <unknown>:0:0: in function entry_function i32 (i32, i32, i32, i32, i32): secure entry function requires arguments on stack
2+
3+
4+
error: aborting due to previous error
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// gate-test-cmse_nonsecure_entry
2+
// ignore-thumbv8m.main-none-eabi
3+
#![feature(cmse_nonsecure_entry)]
4+
5+
#[no_mangle]
6+
#[cmse_nonsecure_entry] //~ ERROR [E0775]
7+
pub extern "C" fn entry_function(input: u32) -> u32 {
8+
input + 6
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0775]: `#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension
2+
--> $DIR/trustzone-only.rs:6:1
3+
|
4+
LL | #[cmse_nonsecure_entry]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0775`.

0 commit comments

Comments
 (0)