From 18ba4368e4d4bf12faf0ddba4de4431c09af7d77 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Wed, 15 Sep 2021 19:36:35 +0200 Subject: [PATCH] Revert the MPU hack by splitting the store in 2 --- .../nordic/nrf52840_dongle_opensk/src/main.rs | 18 +++++-- boards/nordic/nrf52840_mdk_dfu/src/main.rs | 18 +++++-- boards/nordic/nrf52840dk_opensk/src/main.rs | 18 +++++-- patches/tock/01-persistent-storage.patch | 50 +++++++++---------- 4 files changed, 62 insertions(+), 42 deletions(-) diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs index ca14616..63ad6df 100644 --- a/boards/nordic/nrf52840_dongle_opensk/src/main.rs +++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs @@ -64,11 +64,19 @@ const NUM_PROCS: usize = 8; static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; NUM_PROCS]; -static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { - address: 0xC0000, - size: 0x14000, // NUM_PAGES = 20 - storage_type: kernel::StorageType::STORE, -}]; +static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [ + // We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU. + kernel::StorageLocation { + address: 0xC0000, + size: 0x10000, // 16 pages + storage_type: kernel::StorageType::STORE, + }, + kernel::StorageLocation { + address: 0xD0000, + size: 0x4000, // 4 pages + storage_type: kernel::StorageType::STORE, + }, +]; // Static reference to chip for panic dumps static mut CHIP: Option<&'static nrf52840::chip::NRF52> = None; diff --git a/boards/nordic/nrf52840_mdk_dfu/src/main.rs b/boards/nordic/nrf52840_mdk_dfu/src/main.rs index 2a5d3e2..29926f8 100644 --- a/boards/nordic/nrf52840_mdk_dfu/src/main.rs +++ b/boards/nordic/nrf52840_mdk_dfu/src/main.rs @@ -58,11 +58,19 @@ const NUM_PROCS: usize = 8; static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; NUM_PROCS]; -static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { - address: 0xC0000, - size: 0x14000, // NUM_PAGES = 20 - storage_type: kernel::StorageType::STORE, -}]; +static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [ + // We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU. + kernel::StorageLocation { + address: 0xC0000, + size: 0x10000, // 16 pages + storage_type: kernel::StorageType::STORE, + }, + kernel::StorageLocation { + address: 0xD0000, + size: 0x4000, // 4 pages + storage_type: kernel::StorageType::STORE, + }, +]; // Static reference to chip for panic dumps static mut CHIP: Option<&'static nrf52840::chip::NRF52> = None; diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs index 8f62e3d..83fd0bb 100644 --- a/boards/nordic/nrf52840dk_opensk/src/main.rs +++ b/boards/nordic/nrf52840dk_opensk/src/main.rs @@ -128,11 +128,19 @@ const NUM_PROCS: usize = 8; static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; NUM_PROCS]; -static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { - address: 0xC0000, - size: 0x14000, // NUM_PAGES = 20 - storage_type: kernel::StorageType::STORE, -}]; +static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [ + // We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU. + kernel::StorageLocation { + address: 0xC0000, + size: 0x10000, // 16 pages + storage_type: kernel::StorageType::STORE, + }, + kernel::StorageLocation { + address: 0xD0000, + size: 0x4000, // 4 pages + storage_type: kernel::StorageType::STORE, + }, +]; static mut CHIP: Option<&'static nrf52840::chip::NRF52> = None; diff --git a/patches/tock/01-persistent-storage.patch b/patches/tock/01-persistent-storage.patch index 5c39f71..aed7581 100644 --- a/patches/tock/01-persistent-storage.patch +++ b/patches/tock/01-persistent-storage.patch @@ -349,7 +349,7 @@ index 348c746a5..5465c95f4 100644 } } diff --git a/kernel/src/process.rs b/kernel/src/process.rs -index c52754be3..f5c833ea7 100644 +index c52754be3..ae6a58341 100644 --- a/kernel/src/process.rs +++ b/kernel/src/process.rs @@ -359,6 +359,15 @@ pub trait ProcessType { @@ -404,37 +404,33 @@ index c52754be3..f5c833ea7 100644 fn update_stack_start_pointer(&self, stack_pointer: *const u8) { if stack_pointer >= self.mem_start() && stack_pointer < self.mem_end() { self.debug.map(|debug| { -@@ -1751,6 +1789,37 @@ impl Process<'_, C> { +@@ -1751,6 +1789,33 @@ impl Process<'_, C> { return Err(ProcessLoadError::MpuInvalidFlashLength); } + // Allocate MPU region for the storage locations. The storage locations are currently + // readable by all processes due to lack of stable app id. -+ 'storage_location: for storage_location in kernel.storage_locations() { -+ // We work around MPU limitations on Nordic by also trying unallocated_size = 0x40000. -+ // Note that just using the next power of 2 doesn't work. -+ for &unallocated_size in &[storage_location.size, 0x40000] { -+ if chip -+ .mpu() -+ .allocate_region( -+ storage_location.address as *const u8, -+ unallocated_size, -+ storage_location.size, -+ mpu::Permissions::ReadOnly, -+ &mut mpu_config, -+ ) -+ .is_some() -+ { -+ continue 'storage_location; -+ } -+ if config::CONFIG.debug_load_processes { -+ debug!( -+ "[!] flash=[{:#010X}:{:#010X}] process={:?} - couldn't allocate flash region", -+ storage_location.address, -+ storage_location.address + unallocated_size, -+ process_name -+ ); -+ } ++ for storage_location in kernel.storage_locations() { ++ if chip ++ .mpu() ++ .allocate_region( ++ storage_location.address as *const u8, ++ storage_location.size, ++ storage_location.size, ++ mpu::Permissions::ReadOnly, ++ &mut mpu_config, ++ ) ++ .is_some() ++ { ++ continue; ++ } ++ if config::CONFIG.debug_load_processes { ++ debug!( ++ "[!] flash=[{:#010X}:{:#010X}] process={:?} - couldn't allocate flash region", ++ storage_location.address, ++ storage_location.address + storage_location.size, ++ process_name ++ ); + } + return Ok((None, remaining_memory)); + }