Revert the MPU hack by splitting the store in 2

This commit is contained in:
Julien Cretin
2021-09-15 19:36:35 +02:00
committed by Julien Cretin
parent 596b47886c
commit 18ba4368e4
4 changed files with 62 additions and 42 deletions

View File

@@ -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 {
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [
// We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU.
kernel::StorageLocation {
address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20
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<Nrf52840DefaultPeripherals>> = None;

View File

@@ -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 {
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [
// We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU.
kernel::StorageLocation {
address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20
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<Nrf52840DefaultPeripherals>> = None;

View File

@@ -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 {
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 2] = [
// We implement NUM_PAGES = 20 as 16 + 4 to satisfy the MPU.
kernel::StorageLocation {
address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20
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<Nrf52840DefaultPeripherals>> = None;

View File

@@ -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,38 +404,34 @@ 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<C: 'static + Chip> Process<'_, C> {
@@ -1751,6 +1789,33 @@ impl<C: 'static + Chip> 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] {
+ for storage_location in kernel.storage_locations() {
+ if chip
+ .mpu()
+ .allocate_region(
+ storage_location.address as *const u8,
+ unallocated_size,
+ storage_location.size,
+ storage_location.size,
+ mpu::Permissions::ReadOnly,
+ &mut mpu_config,
+ )
+ .is_some()
+ {
+ continue 'storage_location;
+ continue;
+ }
+ if config::CONFIG.debug_load_processes {
+ debug!(
+ "[!] flash=[{:#010X}:{:#010X}] process={:?} - couldn't allocate flash region",
+ storage_location.address,
+ storage_location.address + unallocated_size,
+ storage_location.address + storage_location.size,
+ process_name
+ );
+ }
+ }
+ return Ok((None, remaining_memory));
+ }
+