From 7a975acf3379d0aea70ae367d33e8d4e6dd1fa47 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 13 Sep 2021 17:33:15 +0200 Subject: [PATCH] Hack for Nordic in process.rs --- .../nordic/nrf52840_dongle_opensk/src/main.rs | 1 - boards/nordic/nrf52840_mdk_dfu/src/main.rs | 1 - boards/nordic/nrf52840dk_opensk/src/main.rs | 1 - patches/tock/01-persistent-storage.patch | 61 ++++++++++--------- patches/tock/06-upgrade-partitions.patch | 5 +- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs index d4309eb..ca14616 100644 --- a/boards/nordic/nrf52840_dongle_opensk/src/main.rs +++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs @@ -67,7 +67,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { address: 0xC0000, size: 0x14000, // NUM_PAGES = 20 - unallocated_size: 0x40000, // MPU limitation storage_type: kernel::StorageType::STORE, }]; diff --git a/boards/nordic/nrf52840_mdk_dfu/src/main.rs b/boards/nordic/nrf52840_mdk_dfu/src/main.rs index e57c1fc..2a5d3e2 100644 --- a/boards/nordic/nrf52840_mdk_dfu/src/main.rs +++ b/boards/nordic/nrf52840_mdk_dfu/src/main.rs @@ -61,7 +61,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { address: 0xC0000, size: 0x14000, // NUM_PAGES = 20 - unallocated_size: 0x40000, // MPU limitation storage_type: kernel::StorageType::STORE, }]; diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs index 176c55a..8f62e3d 100644 --- a/boards/nordic/nrf52840dk_opensk/src/main.rs +++ b/boards/nordic/nrf52840dk_opensk/src/main.rs @@ -131,7 +131,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { address: 0xC0000, size: 0x14000, // NUM_PAGES = 20 - unallocated_size: 0x40000, // MPU limitation storage_type: kernel::StorageType::STORE, }]; diff --git a/patches/tock/01-persistent-storage.patch b/patches/tock/01-persistent-storage.patch index cbaf32a..5c39f71 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..26a7c47d3 100644 +index c52754be3..f5c833ea7 100644 --- a/kernel/src/process.rs +++ b/kernel/src/process.rs @@ -359,6 +359,15 @@ pub trait ProcessType { @@ -404,33 +404,37 @@ index c52754be3..26a7c47d3 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,33 @@ impl Process<'_, C> { +@@ -1751,6 +1789,37 @@ 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. -+ for storage_location in kernel.storage_locations() { -+ if chip -+ .mpu() -+ .allocate_region( -+ storage_location.address as *const u8, -+ storage_location.unallocated_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 -+ ); ++ '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 ++ ); ++ } + } + return Ok((None, remaining_memory)); + } @@ -439,10 +443,10 @@ index c52754be3..26a7c47d3 100644 // memory space just for kernel and grant state. We need to make // sure we allocate enough memory just for that. diff --git a/kernel/src/sched.rs b/kernel/src/sched.rs -index 10626a2e1..61401b04a 100644 +index 10626a2e1..8844bc6c3 100644 --- a/kernel/src/sched.rs +++ b/kernel/src/sched.rs -@@ -118,6 +118,13 @@ pub enum SchedulingDecision { +@@ -118,6 +118,12 @@ pub enum SchedulingDecision { TrySleep, } @@ -450,13 +454,12 @@ index 10626a2e1..61401b04a 100644 +pub struct StorageLocation { + pub address: usize, + pub size: usize, -+ pub unallocated_size: usize, +} + /// Main object for the kernel. Each board will need to create one. pub struct Kernel { /// How many "to-do" items exist at any given time. These include -@@ -127,6 +134,9 @@ pub struct Kernel { +@@ -127,6 +133,9 @@ pub struct Kernel { /// This holds a pointer to the static array of Process pointers. processes: &'static [Option<&'static dyn process::ProcessType>], @@ -466,7 +469,7 @@ index 10626a2e1..61401b04a 100644 /// A counter which keeps track of how many process identifiers have been /// created. This is used to create new unique identifiers for processes. process_identifier_max: Cell, -@@ -170,9 +180,17 @@ pub enum StoppedExecutingReason { +@@ -170,9 +179,17 @@ pub enum StoppedExecutingReason { impl Kernel { pub fn new(processes: &'static [Option<&'static dyn process::ProcessType>]) -> Kernel { @@ -484,7 +487,7 @@ index 10626a2e1..61401b04a 100644 process_identifier_max: Cell::new(0), grant_counter: Cell::new(0), grants_finalized: Cell::new(false), -@@ -900,4 +918,8 @@ impl Kernel { +@@ -900,4 +917,8 @@ impl Kernel { (return_reason, time_executed_us) } diff --git a/patches/tock/06-upgrade-partitions.patch b/patches/tock/06-upgrade-partitions.patch index 93b23c6..d94e835 100644 --- a/patches/tock/06-upgrade-partitions.patch +++ b/patches/tock/06-upgrade-partitions.patch @@ -31,10 +31,10 @@ index 5465c95f4..e596648f7 100644 } } diff --git a/kernel/src/sched.rs b/kernel/src/sched.rs -index 61401b04a..e9a58c018 100644 +index 8844bc6c3..692bad2d3 100644 --- a/kernel/src/sched.rs +++ b/kernel/src/sched.rs -@@ -118,11 +118,20 @@ pub enum SchedulingDecision { +@@ -118,10 +118,19 @@ pub enum SchedulingDecision { TrySleep, } @@ -50,7 +50,6 @@ index 61401b04a..e9a58c018 100644 pub struct StorageLocation { pub address: usize, pub size: usize, - pub unallocated_size: usize, + pub storage_type: StorageType, }