diff options
| author | Ian McIntyre <me@mciantyre.dev> | 2026-10-24 07:30:47 -0400 |
|---|---|---|
| committer | Ian McIntyre <me@mciantyre.dev> | 2026-10-27 11:35:22 -0400 |
| commit | 428ea28a9102d966eda5b9c39644ff66c5f59c59 (patch) | |
| tree | cc553277851f4f421701563ce340f827d83ea4db | |
| parent | 5ad4b42b4bd810eecf47d3a13853658ffb263a16 (diff) | |
Preload RAM sections, maybe, if ECC RAM is active
Blasting all of RAM with zeros would wipe ourselves away if we were
executing from RAM. It also wipes out the HAB event log, which seems to
reside in the boot ROM reservation.
We now zero individual memory regions. Similar to the copy loops,
we'll skip the loop if the VMA and LMA are equal. It looks silly to
zero things like bss when we know cortex-m's reset handler will do it;
but, the usual loop doesn't use the 64 bit writes we'd need to preload
ciphers in ITCM and OCRAM.
Tested RAM loading with probe-rs, using an 1170EVK w/ ECC RAM active.
| -rw-r--r-- | src/host.rs | 45 | ||||
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | src/target.rs | 53 | ||||
| -rw-r--r-- | tests/inspect_elf.rs | 35 |
4 files changed, 63 insertions, 78 deletions
diff --git a/src/host.rs b/src/host.rs index e809895..42d5e5e 100644 --- a/src/host.rs +++ b/src/host.rs @@ -926,8 +926,8 @@ fn write_flexram_memories( Ok(()) } -/// Generate spans for zeroing ECC RAM. -fn write_ecc_zero_spans( +/// Generate the symbol content for ECC RAM configurations. +fn write_ecc_config( output: &mut dyn Write, family: Family, flexram_layout: &[FlexRamKind], @@ -938,44 +938,21 @@ fn write_ecc_zero_spans( let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout); let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout); - // If memory isn't available or doesn't need - // use ECC, then we don't need to zero it. - // Generate dummy values that skip the loops. - + let mut ecc_ram_config = 0_u32; if itcm_count > 0 && flexram_ecc.is_enable() { - // Make sure to zero the reserved regions, too, - // just in case they're accidentally / intentionally accessed. - let itcm = family.itcm_start_size(itcm_count); - let start = itcm.start - 32; - let end = start + itcm.size + 32; - writeln!(output, "__sitcm = {:#010X};", start)?; - writeln!(output, "__eitcm = {:#010X};", end)?; - } else { - writeln!(output, "__sitcm = 0;")?; - writeln!(output, "__eitcm = 0;")?; - }; + ecc_ram_config |= crate::ECC_ITCM_ACTIVE; + } if dtcm_count > 0 && flexram_ecc.is_enable() { - writeln!(output, "__sdtcm = ORIGIN(DTCM);")?; - writeln!(output, "__edtcm = ORIGIN(DTCM) + LENGTH(DTCM);")?; - } else { - writeln!(output, "__sdtcm = 0;")?; - writeln!(output, "__edtcm = 0;")?; - }; + ecc_ram_config |= crate::ECC_DTCM_ACTIVE; + } let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64); - - // The zeroization should specifically target one of the two regions. - // But we're not prepared to do that, so incur the loop for the whole - // region no matter which is enabled. if ocram.size > 0 && (mecc64.is_enable() || flexram_ecc.is_enable()) { - writeln!(output, "__socram = {:#010X};", ocram.start)?; - writeln!(output, "__eocram = {:#010X};", ocram.start + ocram.size)?; - } else { - writeln!(output, "__socram = 0;")?; - writeln!(output, "__eocram = 0;")?; + ecc_ram_config |= crate::ECC_OCRAM_ACTIVE; } + writeln!(output, "__ecc_ram_config = {ecc_ram_config:#010X};")?; Ok(()) } @@ -1003,7 +980,7 @@ fn write_flash_memory_map( write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; writeln!(output, "}}")?; writeln!(output, "__fcb_offset = {:#X};", family.fcb_offset())?; - write_ecc_zero_spans(output, family, flexram_layout, flexram_ecc, mecc64)?; + write_ecc_config(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } @@ -1025,7 +1002,7 @@ fn write_ram_memory_map( writeln!(output, "MEMORY {{")?; write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; writeln!(output, "}}")?; - write_ecc_zero_spans(output, family, flexram_layout, flexram_ecc, mecc64)?; + write_ecc_config(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } @@ -198,11 +198,19 @@ #![cfg_attr(all(target_arch = "arm", target_os = "none"), no_std)] +/// `__ecc_ram_config` bit set when ITCM ECC is active. +const ECC_ITCM_ACTIVE: u32 = 1 << 0; +/// `__ecc_ram_config` bit set when DTCM ECC is active. +const ECC_DTCM_ACTIVE: u32 = 1 << 1; + cfg_if::cfg_if! { if #[cfg(all(target_arch = "arm", target_os = "none"))] { mod target; pub use target::*; } else { + /// `__ecc_ram_config` bit set when OCRAM ECC is active. + const ECC_OCRAM_ACTIVE: u32 = 1 << 2; + mod host; pub use host::*; } diff --git a/src/target.rs b/src/target.rs index bfe6e7e..95dc7b6 100644 --- a/src/target.rs +++ b/src/target.rs @@ -71,6 +71,26 @@ global_asm! {r#" .endm +.macro maybe_zero_section64 dst, src, end + ldr r0, =\dst + ldr r2, =\src + cmp r2, r0 + beq 999f + + ldr r1, =\end + movw r2, #0 + + 888: + cmp r0, r1 + bge 999f + + strd r2, r2, [r0], #8 + b 888b + + 999: + +.endm + __pre_init: ldr r0, =__imxrt_rt_v0.2 @ Need to know which chip family we're initializing. ldr r1, =0x1180 @@ -107,11 +127,8 @@ __pre_init: # - the CPU issues RMW operations for smaller accesses. # - the CPU retries operations when signaled. # - # If there's no ECC region, the host sets the ends - # to zero, and we skip the instructions. - movw r3, #0 - ldr r0, =__eitcm - cmp r0, r3 + ldr r2, =__ecc_ram_config + tst r2, #{ecc_itcm} itttt ne ldrne r0, =0xE000EF90 @ ITCMCR ldrne r1, [r0] @ r1 = *ITCMCR @@ -119,8 +136,7 @@ __pre_init: strne r1, [r0] @ *ITCMCR = r1 # Same as above, but for DTCM. - ldr r0, =__edtcm - cmp r0, r3 + tst r2, #{ecc_dtcm} itttt ne ldrne r0, =0xE000EF94 @ DTCMCR ldrne r1, [r0] @ r1 = *DTCMCR @@ -130,12 +146,19 @@ __pre_init: dsb isb - # Zero memory regions for any ECC support. - # If ECC isn't enabled, then the host generates - # values that short circuit the loops. - zero_section64 __sitcm , __eitcm - zero_section64 __sdtcm , __edtcm - zero_section64 __socram , __eocram + # If any ECC is active, try to zero the RAM-resident sections with 64-bit writes. + # We may skip sections if the contents should have already been loaded (by the + # debugger, perhaps). + cmp r2, #0 + beq 1000f + zero_section64 __estack , __sstack + maybe_zero_section64 __svector_table , __sivector_table , __evector_table + maybe_zero_section64 __stext , __sitext , __etext + maybe_zero_section64 __srodata , __sirodata , __erodata + maybe_zero_section64 __sdata , __sidata , __edata + zero_section64 __sbss , __ebss + zero_section64 __suninit , __euninit + zero_section64 __sheap , __eheap b 1000f @@ -154,7 +177,9 @@ __pre_init: .cfi_endproc .size __pre_init, . - __pre_init -"# +"#, + ecc_itcm = const crate::ECC_ITCM_ACTIVE, + ecc_dtcm = const crate::ECC_DTCM_ACTIVE, } /// Returns a pointer to the end of the heap. diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs index 177bf50..7f7cc79 100644 --- a/tests/inspect_elf.rs +++ b/tests/inspect_elf.rs @@ -266,12 +266,7 @@ fn imxrt1010evk() { binary.fcb().unwrap() ); assert_eq!(binary.flexram_config().unwrap(), 0b11_10_0101); - assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__socram").unwrap(), 0); - assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -386,12 +381,7 @@ fn imxrt1010evk_ram() { let binary = ImxrtBinary::new(&elf, &contents); - assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__socram").unwrap(), 0); - assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); let stack = binary.section(".stack").unwrap(); assert_eq!( @@ -508,12 +498,7 @@ fn baseline_teensy4( binary.flexram_config().unwrap(), 0b11111111_101010101010101010101010 ); - assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__socram").unwrap(), 0); - assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -754,12 +739,7 @@ fn imxrt1170evk_cm7() { binary.fcb().unwrap() ); assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); - assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0x4_0000); - assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0x2000_0000); - assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0x2004_0000); - assert_eq!(binary.symbol_value("__socram").unwrap(), 0x2024_0000); - assert_eq!(binary.symbol_value("__eocram").unwrap(), 0x2034_0000); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0b111); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -881,12 +861,7 @@ fn imxrt1170evk_cm7_nonboot() { assert!(binary.fcb().is_err()); assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); - assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); - assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0x4_0000); - assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0x2000_0000); - assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0x2004_0000); - assert_eq!(binary.symbol_value("__socram").unwrap(), 0x2024_0000); - assert_eq!(binary.symbol_value("__eocram").unwrap(), 0x2034_0000); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0b111); assert!( binary.ivt().is_err(), |
