From 1087e9bb7cd31e6fd88532ead08a927168e36de7 Mon Sep 17 00:00:00 2001 From: Ian McIntyre Date: Mon, 26 Oct 2026 10:52:57 -0400 Subject: Zero unallocated memory, too If the CPU speculates into the RAM we haven't initialized, we can fault when the ECC controller reports bad data. Didn't have that problem when blasting all of RAM... We can use the linker to help us figure out the range of these unused regions. If we don't need to zero anything, the host emits 0 for the start and end, skipping the zeroing. I'm also zeroing the 32 byte reservation at the start of ITCM, along with any padding introduced before the vector table. With this in place, the rip_through_ram firmware does not report any exceptions. --- src/host.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) (limited to 'src/host.rs') diff --git a/src/host.rs b/src/host.rs index 42d5e5e..b9809dd 100644 --- a/src/host.rs +++ b/src/host.rs @@ -824,6 +824,18 @@ impl RuntimeBuilder { let link_x = include_bytes!("host/imxrt-link.x"); writer.write_all(link_x)?; + // ECC configurations, varying by user build settings, need to go at + // the back. We're using a linker script to figure out the spans of + // unused RAM regions for zeroing. We need the other sections placed + // before we do that. + write_ecc_config( + writer, + self.family, + &self.flexram_layout, + self.flexram_ecc, + self.mecc64, + )?; + Ok(()) } @@ -937,18 +949,54 @@ fn write_ecc_config( let itcm_count = layout_count_of(FlexRamKind::Itcm, flexram_layout); let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout); let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout); + let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64); + + let itcm_ecc = itcm_count > 0 && flexram_ecc.is_enable(); + let dtcm_ecc = dtcm_count > 0 && flexram_ecc.is_enable(); + let ocram_ecc = ocram.size > 0 && (mecc64.is_enable() || flexram_ecc.is_enable()); + let any_ecc_ram = itcm_ecc || dtcm_ecc || ocram_ecc; + + if any_ecc_ram { + writeln!(output, "SECTIONS {{")?; + } + if itcm_ecc { + writeln!( + output, + " .unused.itcm (NOLOAD) : ALIGN(8) {{ . = ALIGN(8); __sitcm_unused = .; . = ORIGIN(ITCM) + LENGTH(ITCM); __eitcm_unused = .; }} > ITCM" + )?; + } else { + writeln!(output, "__sitcm_unused = 0; __eitcm_unused = 0;")?; + } + if dtcm_ecc { + writeln!( + output, + " .unused.dtcm (NOLOAD) : ALIGN(8) {{ . = ALIGN(8); __sdtcm_unused = .; . = ORIGIN(DTCM) + LENGTH(DTCM); __edtcm_unused = .; }} > DTCM" + )?; + } else { + writeln!(output, "__sdtcm_unused = 0; __edtcm_unused = 0;")?; + } + if ocram_ecc { + writeln!( + output, + " .unused.ocram (NOLOAD) : ALIGN(8) {{ . = ALIGN(8); __socram_unused = .; . = ORIGIN(OCRAM) + LENGTH(OCRAM); __eocram_unused = .; }} > OCRAM" + )?; + } else { + writeln!(output, "__socram_unused = 0; __eocram_unused = 0;")?; + } + if any_ecc_ram { + writeln!(output, "}}")?; + } let mut ecc_ram_config = 0_u32; - if itcm_count > 0 && flexram_ecc.is_enable() { + if itcm_ecc { ecc_ram_config |= crate::ECC_ITCM_ACTIVE; } - if dtcm_count > 0 && flexram_ecc.is_enable() { + if dtcm_ecc { ecc_ram_config |= crate::ECC_DTCM_ACTIVE; } - let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64); - if ocram.size > 0 && (mecc64.is_enable() || flexram_ecc.is_enable()) { + if ocram_ecc { ecc_ram_config |= crate::ECC_OCRAM_ACTIVE; } @@ -980,7 +1028,6 @@ 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_config(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } @@ -1002,7 +1049,6 @@ fn write_ram_memory_map( writeln!(output, "MEMORY {{")?; write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; writeln!(output, "}}")?; - write_ecc_config(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } -- cgit v1.2.3