Skip to content

Commit 88c6f86

Browse files
bors[bot]Norbert Fabritiusadamgreig
authored
Merge #455
455: Add zero-init-ram feature r=adamgreig a=inorick Add the 'zero-init-ram' feature that initializes the RAM with zeros during startup. This is normally not necessary but might be required on custom hardware. If this step is skipped on such hardware, reading from memory that was never written to will cause a hard-fault. Co-authored-by: Norbert Fabritius <[email protected]> Co-authored-by: Adam Greig <[email protected]>
2 parents 8e4b187 + 9b51b40 commit 88c6f86

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

cortex-m-rt/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Add `zero-init-ram` feature to initialize RAM with zeros on startup. This can be necessary on
11+
safety-critical hardware to properly initialize memory integrity measures.
12+
1013
## [v0.7.3]
1114

1215
- Fixed a potential miscompilation caused by the initial stack pointer

cortex-m-rt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ required-features = ["device"]
4545
device = []
4646
set-sp = []
4747
set-vtor = []
48+
zero-init-ram = []
4849

4950
[package.metadata.docs.rs]
5051
features = ["device"]

cortex-m-rt/ci/script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ main() {
6363

6464
cargo rustc --target "$TARGET" --example minimal --features "set-sp,${needed_features}" -- $linker
6565
cargo rustc --target "$TARGET" --example minimal --features "set-sp,${needed_features}" --release -- $linker
66+
cargo rustc --target "$TARGET" --example minimal --features "zero-init-ram,${needed_features}" -- $linker
67+
cargo rustc --target "$TARGET" --example minimal --features "zero-init-ram,${needed_features}" --release -- $linker
6668
cargo rustc --target "$TARGET" --example minimal --features "set-vtor,${needed_features}" -- $linker
6769
cargo rustc --target "$TARGET" --example minimal --features "set-vtor,${needed_features}" --release -- $linker
6870
done

cortex-m-rt/link.x.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ PROVIDE(__pre_init = DefaultPreInit);
6060
/* # Sections */
6161
SECTIONS
6262
{
63-
PROVIDE(_stack_start = ORIGIN(RAM) + LENGTH(RAM));
63+
PROVIDE(_ram_start = ORIGIN(RAM));
64+
PROVIDE(_ram_end = ORIGIN(RAM) + LENGTH(RAM));
65+
PROVIDE(_stack_start = _ram_end);
6466

6567
/* ## Sections in FLASH */
6668
/* ### Vector table */

cortex-m-rt/src/lib.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@
169169
//! `_stack_start` value from the linker script. This is not usually required, but some debuggers
170170
//! do not initialise SP when performing a soft reset, which can lead to stack corruption.
171171
//!
172+
//! ## `zero-init-ram`
173+
//!
174+
//! If this feature is enabled, RAM is initialized with zeros during startup from the `_ram_start`
175+
//! value to the `_ram_end` value from the linker script. This is not usually required, but might be
176+
//! necessary to properly initialize checksum-based memory integrity measures on safety-critical
177+
//! hardware.
178+
//!
172179
//! ## `set-vtor`
173180
//!
174181
//! If this feature is enabled, the vector table offset register (VTOR) is initialised in the reset
@@ -529,9 +536,11 @@ cfg_global_asm! {
529536
// Example use cases include disabling default watchdogs or enabling RAM.
530537
"bl __pre_init",
531538

532-
// Initialise .bss memory. `__sbss` and `__ebss` come from the linker script.
533-
"ldr r0, =__sbss
534-
ldr r1, =__ebss
539+
// If enabled, initialize RAM with zeros. This is not usually required, but might be necessary
540+
// to properly initialize checksum-based memory integrity measures on safety-critical hardware.
541+
#[cfg(feature = "zero-init-ram")]
542+
"ldr r0, =_ram_start
543+
ldr r1, =_ram_end
535544
movs r2, #0
536545
0:
537546
cmp r1, r0
@@ -540,17 +549,29 @@ cfg_global_asm! {
540549
b 0b
541550
1:",
542551

552+
// Initialise .bss memory. `__sbss` and `__ebss` come from the linker script.
553+
#[cfg(not(feature = "zero-init-ram"))]
554+
"ldr r0, =__sbss
555+
ldr r1, =__ebss
556+
movs r2, #0
557+
2:
558+
cmp r1, r0
559+
beq 3f
560+
stm r0!, {{r2}}
561+
b 2b
562+
3:",
563+
543564
// Initialise .data memory. `__sdata`, `__sidata`, and `__edata` come from the linker script.
544565
"ldr r0, =__sdata
545566
ldr r1, =__edata
546567
ldr r2, =__sidata
547-
2:
568+
4:
548569
cmp r1, r0
549-
beq 3f
570+
beq 5f
550571
ldm r2!, {{r3}}
551572
stm r0!, {{r3}}
552-
b 2b
553-
3:",
573+
b 4b
574+
5:",
554575

555576
// Potentially enable an FPU.
556577
// SCB.CPACR is 0xE000_ED88.

0 commit comments

Comments
 (0)