aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan McIntyre <me@mciantyre.dev>2025-09-09 08:29:25 -0400
committerIan McIntyre <me@mciantyre.dev>2025-09-09 08:55:06 -0400
commitce97696bcd4515b5b2556163aa50c1b4723e7c0f (patch)
tree9572f07b999c9f1f27ff42390b9bc312e0b7e070
parente014e4ec81a947ae8235a51dc2349af58bb5752d (diff)
Reserve address 0 by shifting ITCM start address
The runtime previously allowed function placement at address 0 in ITCM. However, if you ever formed a pointer to the function placed there, it would look like a null pointer. And you would never be able to call that function if you relied on null pointer optimization. Also, most MCU reference manuals (RM) recommend against this placement. This commit reduces the total capacity of ITCM by 32 bytes, the smallest possible size of a MPU region. Note that this is greater than the RM's recommendation of a four byte reservation. It affects all supported MCUs, except the 1180. If you're so inclined, your MPU could disallow loads, stores, and execution from this reservation. Revised unit tests should cover this change. Additionally, you can manually verify that the ITCM region lengths are reduced by 32 bytes by opening the linker scripts generated by the ELF test suite.
-rw-r--r--CHANGELOG.md4
-rw-r--r--src/host.rs19
-rw-r--r--tests/inspect_elf.rs2
3 files changed, 20 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a43b8e5..7e59f3a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## [Unreleased]
+Establish a 32 byte reservation at the start of ITCM where instructions are
+never placed. This reduces the total capacity of ITCM by 32 bytes on nearly
+all MCUs, except the 1180.
+
## [0.1.7] 2025-06-14
Introduce `RuntimeBuilder::in_flash` for creating images that can be launched
diff --git a/src/host.rs b/src/host.rs
index 43472fe..ba299dd 100644
--- a/src/host.rs
+++ b/src/host.rs
@@ -946,7 +946,7 @@ impl Family {
/// Returns the start and size of the ITCM memory region.
const fn itcm_start_size(self, itcm_banks: u32) -> (u32, u32) {
- let itcm_size = itcm_banks * self.flexram_bank_size();
+ let mut itcm_size = itcm_banks * self.flexram_bank_size();
let itcm_start = match self {
Family::Imxrt1010
| Family::Imxrt1015
@@ -956,7 +956,13 @@ impl Family {
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1160
- | Family::Imxrt1170 => 0x00000000,
+ | Family::Imxrt1170 => {
+ // Establish a reservation for null pointers.
+ // Note that this reservation is the minimum
+ // size of an MPU region.
+ itcm_size = itcm_size.saturating_sub(32);
+ 32
+ }
Family::Imxrt1180 => 0x10000000 - itcm_size,
};
(itcm_start, itcm_size)
@@ -1252,11 +1258,16 @@ mod tests {
#[test]
fn itcm_start_size() {
// Most parts have an ITCM that could touch address 0.
+ // However, the implementation reserves an MPU region
+ // at address 0.
for family in MOST_FAMILIES {
for itcm_banks in 0..=family.flexram_bank_count() {
let (start, size) = family.itcm_start_size(itcm_banks);
- assert_eq!(start, 0);
- assert_eq!(size, family.flexram_bank_size() * itcm_banks);
+ assert_eq!(start, 32);
+ assert_eq!(
+ size,
+ (family.flexram_bank_size() * itcm_banks).saturating_sub(32)
+ );
}
}
diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs
index 8108f3f..e9a8f73 100644
--- a/tests/inspect_elf.rs
+++ b/tests/inspect_elf.rs
@@ -198,7 +198,7 @@ struct Section {
}
const DTCM: u64 = 0x2000_0000;
-const ITCM: u64 = 0x0000_0000;
+const ITCM: u64 = 0x0000_0020;
const fn aligned(value: u64, alignment: u64) -> u64 {
(value + (alignment - 1)) & !(alignment - 1)