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 +++++++++++++++++-- src/host/imxrt-link.x | 14 ++++- src/target.rs | 7 +++ tests/inspect_elf.rs | 153 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 223 insertions(+), 9 deletions(-) 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(()) } diff --git a/src/host/imxrt-link.x b/src/host/imxrt-link.x index c04e3d4..48a792b 100644 --- a/src/host/imxrt-link.x +++ b/src/host/imxrt-link.x @@ -51,6 +51,13 @@ SECTIONS _stack_start = __sstack; } > REGION_STACK + .vector_table_pad (NOLOAD) : ALIGN(8) + { + __svector_table_pad = .; + . = ALIGN(1024); + __evector_table_pad = .; + } > REGION_VTABLE + .vector_table : ALIGN(1024) { FILL(0xff); @@ -170,6 +177,9 @@ SECTIONS ASSERT(__sstack % 8 == 0 && __estack % 8 == 0, " BUG(imxrt-rt): .stack is not 8-byte aligned"); +ASSERT(__svector_table_pad % 8 == 0 && __evector_table_pad % 8 == 0, " +BUG(imxrt-rt): .vector_table_pad is not 8-byte aligned"); + ASSERT(__stext % 8 == 0 && __etext % 8 == 0, " BUG(imxrt-rt): .text is not 8-byte aligned"); @@ -199,7 +209,9 @@ BUG(imxrt-rt): .heap is not 8-byte aligned"); /* # Position checks */ -/* ## .vector_table */ +ASSERT(__evector_table_pad == ADDR(.vector_table), " +BUG(imxrt-rt): .vector_table_pad does not end at the vector table"); + ASSERT(__reset_vector == ADDR(.vector_table) + 0x8, " BUG(imxrt-rt): the reset vector is missing"); diff --git a/src/target.rs b/src/target.rs index 95dc7b6..5e298e6 100644 --- a/src/target.rs +++ b/src/target.rs @@ -151,7 +151,10 @@ __pre_init: # debugger, perhaps). cmp r2, #0 beq 1000f + + zero_section64 0x00 , 0x20 zero_section64 __estack , __sstack + zero_section64 __svector_table_pad , __evector_table_pad maybe_zero_section64 __svector_table , __sivector_table , __evector_table maybe_zero_section64 __stext , __sitext , __etext maybe_zero_section64 __srodata , __sirodata , __erodata @@ -160,6 +163,10 @@ __pre_init: zero_section64 __suninit , __euninit zero_section64 __sheap , __eheap + zero_section64 __sitcm_unused , __eitcm_unused + zero_section64 __sdtcm_unused , __edtcm_unused + zero_section64 __socram_unused , __eocram_unused + b 1000f 1180: diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs index 7f7cc79..7352c8b 100644 --- a/tests/inspect_elf.rs +++ b/tests/inspect_elf.rs @@ -12,12 +12,21 @@ type Result = std::result::Result>; /// Build an example with optional environment variables, returning a path to the ELF. fn cargo_build_with_envs(board: &str, envs: &[(&str, &str)]) -> Result { + cargo_build_with_envs_in(board, board, envs) +} + +/// Like [`cargo_build_with_envs`], but builds in `target/{target_dir}`. +fn cargo_build_with_envs_in( + board: &str, + target_dir: &str, + envs: &[(&str, &str)], +) -> Result { let status = Command::new("cargo") .arg("build") .arg("--example=blink-rtic") .arg(format!("--features=board/{board},board/rtic")) .arg("--target=thumbv7em-none-eabihf") - .arg(format!("--target-dir=target/{board}")) + .arg(format!("--target-dir=target/{target_dir}")) .arg("--quiet") .envs(envs.iter().copied()) .spawn()? @@ -31,7 +40,7 @@ fn cargo_build_with_envs(board: &str, envs: &[(&str, &str)]) -> Result } let path = PathBuf::from(format!( - "target/{board}/thumbv7em-none-eabihf/debug/examples/blink-rtic" + "target/{target_dir}/thumbv7em-none-eabihf/debug/examples/blink-rtic" )); Ok(path) } @@ -267,6 +276,12 @@ fn imxrt1010evk() { ); assert_eq!(binary.flexram_config().unwrap(), 0b11_10_0101); assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); + assert_eq!(binary.symbol_value("__sitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -382,6 +397,12 @@ fn imxrt1010evk_ram() { let binary = ImxrtBinary::new(&elf, &contents); assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); + assert_eq!(binary.symbol_value("__sitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0); let stack = binary.section(".stack").unwrap(); assert_eq!( @@ -499,6 +520,12 @@ fn baseline_teensy4( 0b11111111_101010101010101010101010 ); assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); + assert_eq!(binary.symbol_value("__sitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -844,6 +871,84 @@ fn imxrt1170evk_cm7() { "increment_data is not XiP" ); assert!(binary.symbol("Reset").is_some()); + + assert_eq!( + binary.symbol_value("__sitcm_unused").unwrap(), + aligned(text.address + text.size, 8), + "Unused ITCM starts behind text" + ); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0x4_0000); + assert_eq!( + binary.symbol_value("__sdtcm_unused").unwrap(), + aligned(rodata.address + rodata.size, 8), + "Unused DTCM starts behind dtcm.data" + ); + assert_eq!( + binary.symbol_value("__edtcm_unused").unwrap(), + DTCM + 0x4_0000 + ); + assert_eq!( + binary.symbol_value("__socram_unused").unwrap(), + aligned(uninit.address + uninit.size, 8), + "Unused OCRAM starts behind uninit" + ); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0x2034_0000); +} + +#[test] +#[ignore = "building an example can take time"] +fn imxrt1170evk_cm7_vector_table_pad() { + let path = cargo_build_with_envs_in( + "imxrt1170evk-cm7", + "imxrt1170evk-cm7-vector-table-pad", + &[("BOARD_STACK", "8200")], // Introduce vector table padding. + ) + .expect("Unable to build example"); + let contents = fs::read(path).expect("Could not read ELF file"); + let elf = Elf::parse(&contents).expect("Could not parse ELF"); + + let binary = ImxrtBinary::new(&elf, &contents); + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0b111); + + let stack = binary.section(".stack").unwrap(); + assert_eq!( + Section { + address: DTCM, + size: 8200 + }, + stack, + "stack not at ORIGIN(DTCM), or not 8200 bytes large" + ); + + let vector_table = binary.section(".vector_table").unwrap(); + assert_eq!( + vector_table.address, + DTCM + 9 * 1024, + "vector table not at the next 1024-byte boundary behind the stack" + ); + + let vector_table_pad = binary.section(".vector_table_pad").unwrap(); + assert_eq!( + Section { + address: stack.address + stack.size, + size: vector_table.address - (stack.address + stack.size), + }, + vector_table_pad, + "vector table padding doesn't span the stack to the vector table" + ); + assert_eq!( + binary.section_header(".vector_table_pad").unwrap().sh_type, + goblin::elf::section_header::SHT_NOBITS, + "vector table padding is NOLOAD" + ); + assert_eq!( + binary.symbol_value("__svector_table_pad").unwrap(), + vector_table_pad.address + ); + assert_eq!( + binary.symbol_value("__evector_table_pad").unwrap(), + vector_table.address + ); } #[test] @@ -975,6 +1080,28 @@ fn imxrt1170evk_cm7_nonboot() { ); assert_eq!(binary.section_lma(".heap"), heap.address, "Heap is NOLOAD"); assert!(binary.symbol("Reset").is_some()); + + assert_eq!( + binary.symbol_value("__sitcm_unused").unwrap(), + aligned(text.address + text.size, 8), + "Unused ITCM starts behind text" + ); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0x4_0000); + assert_eq!( + binary.symbol_value("__sdtcm_unused").unwrap(), + aligned(rodata.address + rodata.size, 8), + "Unused DTCM starts behind dtcm.data" + ); + assert_eq!( + binary.symbol_value("__edtcm_unused").unwrap(), + DTCM + 0x4_0000 + ); + assert_eq!( + binary.symbol_value("__socram_unused").unwrap(), + aligned(uninit.address + uninit.size, 8), + "Unused OCRAM starts behind uninit" + ); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0x2034_0000); } #[test] @@ -1114,4 +1241,26 @@ fn imxrt1170evk_cm7_secondary() { "increment_data is not XiP" ); assert!(binary.symbol("Reset").is_some()); + + assert_eq!( + binary.symbol_value("__sitcm_unused").unwrap(), + aligned(text.address + text.size, 8), + "Unused ITCM starts behind text" + ); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0x4_0000); + assert_eq!( + binary.symbol_value("__sdtcm_unused").unwrap(), + aligned(rodata.address + rodata.size, 8), + "Unused DTCM starts behind dtcm.data" + ); + assert_eq!( + binary.symbol_value("__edtcm_unused").unwrap(), + DTCM + 0x4_0000 + ); + assert_eq!( + binary.symbol_value("__socram_unused").unwrap(), + aligned(uninit.address + uninit.size, 8), + "Unused OCRAM starts behind uninit" + ); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0x2034_0000); } -- cgit v1.2.3