Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 867bd42

Browse files
committed
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
2 parents d92d28e + 2588287 commit 867bd42

File tree

25 files changed

+254
-1
lines changed

25 files changed

+254
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 3 additions & 0 deletions
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/back/write.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
344344
.expect("non-UTF8 diagnostic");
345345
diag_handler.warn(&msg);
346346
}
347+
llvm::diagnostic::Unsupported(diagnostic_ref) => {
348+
let msg = llvm::build_string(|s| {
349+
llvm::LLVMRustWriteDiagnosticInfoToString(diagnostic_ref, s)
350+
})
351+
.expect("non-UTF8 diagnostic");
352+
diag_handler.err(&msg);
353+
}
347354
llvm::diagnostic::UnknownDiagnostic(..) => {}
348355
}
349356
}

compiler/rustc_codegen_llvm/src/llvm/diagnostic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub enum Diagnostic<'ll> {
118118
InlineAsm(InlineAsmDiagnostic<'ll>),
119119
PGO(&'ll DiagnosticInfo),
120120
Linker(&'ll DiagnosticInfo),
121+
Unsupported(&'ll DiagnosticInfo),
121122

122123
/// LLVM has other types that we do not wrap here.
123124
UnknownDiagnostic(&'ll DiagnosticInfo),
@@ -159,6 +160,7 @@ impl Diagnostic<'ll> {
159160

160161
Dk::PGOProfile => PGO(di),
161162
Dk::Linker => Linker(di),
163+
Dk::Unsupported => Unsupported(di),
162164

163165
_ => UnknownDiagnostic(di),
164166
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ pub enum DiagnosticKind {
483483
OptimizationFailure,
484484
PGOProfile,
485485
Linker,
486+
Unsupported,
486487
}
487488

488489
/// LLVMRustDiagnosticLevel

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ 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"),
462+
E0776: include_str!("./error_codes/E0776.md"),
461463
;
462464
// E0006, // merged with E0005
463465
// E0008, // cannot bind by-move into a pattern guard
Lines changed: 17 additions & 0 deletions
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+
pub extern "C" fn entry_function() {}
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`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
`#[cmse_nonsecure_entry]` functions require a C ABI
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0776
6+
#![feature(cmse_nonsecure_entry)]
7+
8+
#[no_mangle]
9+
#[cmse_nonsecure_entry]
10+
pub fn entry_function(input: Vec<u32>) {}
11+
```
12+
13+
To fix this error, declare your entry function with a C ABI, using `extern "C"`.

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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
// ==========================================================================

0 commit comments

Comments
 (0)