Hack for Nordic in process.rs

This commit is contained in:
Julien Cretin
2021-09-13 17:33:15 +02:00
committed by Julien Cretin
parent 2d5fdd1034
commit 7a975acf33
5 changed files with 34 additions and 35 deletions

View File

@@ -67,7 +67,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation {
address: 0xC0000, address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20 size: 0x14000, // NUM_PAGES = 20
unallocated_size: 0x40000, // MPU limitation
storage_type: kernel::StorageType::STORE, storage_type: kernel::StorageType::STORE,
}]; }];

View File

@@ -61,7 +61,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation {
address: 0xC0000, address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20 size: 0x14000, // NUM_PAGES = 20
unallocated_size: 0x40000, // MPU limitation
storage_type: kernel::StorageType::STORE, storage_type: kernel::StorageType::STORE,
}]; }];

View File

@@ -131,7 +131,6 @@ static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROC
static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation { static mut STORAGE_LOCATIONS: [kernel::StorageLocation; 1] = [kernel::StorageLocation {
address: 0xC0000, address: 0xC0000,
size: 0x14000, // NUM_PAGES = 20 size: 0x14000, // NUM_PAGES = 20
unallocated_size: 0x40000, // MPU limitation
storage_type: kernel::StorageType::STORE, storage_type: kernel::StorageType::STORE,
}]; }];

View File

@@ -349,7 +349,7 @@ index 348c746a5..5465c95f4 100644
} }
} }
diff --git a/kernel/src/process.rs b/kernel/src/process.rs 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 --- a/kernel/src/process.rs
+++ b/kernel/src/process.rs +++ b/kernel/src/process.rs
@@ -359,6 +359,15 @@ pub trait ProcessType { @@ -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) { fn update_stack_start_pointer(&self, stack_pointer: *const u8) {
if stack_pointer >= self.mem_start() && stack_pointer < self.mem_end() { if stack_pointer >= self.mem_start() && stack_pointer < self.mem_end() {
self.debug.map(|debug| { self.debug.map(|debug| {
@@ -1751,6 +1789,33 @@ impl<C: 'static + Chip> Process<'_, C> { @@ -1751,6 +1789,37 @@ impl<C: 'static + Chip> Process<'_, C> {
return Err(ProcessLoadError::MpuInvalidFlashLength); return Err(ProcessLoadError::MpuInvalidFlashLength);
} }
+ // Allocate MPU region for the storage locations. The storage locations are currently + // Allocate MPU region for the storage locations. The storage locations are currently
+ // readable by all processes due to lack of stable app id. + // readable by all processes due to lack of stable app id.
+ for storage_location in kernel.storage_locations() { + 'storage_location: for storage_location in kernel.storage_locations() {
+ if chip + // We work around MPU limitations on Nordic by also trying unallocated_size = 0x40000.
+ .mpu() + // Note that just using the next power of 2 doesn't work.
+ .allocate_region( + for &unallocated_size in &[storage_location.size, 0x40000] {
+ storage_location.address as *const u8, + if chip
+ storage_location.unallocated_size, + .mpu()
+ storage_location.size, + .allocate_region(
+ mpu::Permissions::ReadOnly, + storage_location.address as *const u8,
+ &mut mpu_config, + unallocated_size,
+ ) + storage_location.size,
+ .is_some() + mpu::Permissions::ReadOnly,
+ { + &mut mpu_config,
+ continue; + )
+ } + .is_some()
+ if config::CONFIG.debug_load_processes { + {
+ debug!( + continue 'storage_location;
+ "[!] flash=[{:#010X}:{:#010X}] process={:?} - couldn't allocate flash region", + }
+ storage_location.address, + if config::CONFIG.debug_load_processes {
+ storage_location.address + storage_location.size, + debug!(
+ process_name + "[!] flash=[{:#010X}:{:#010X}] process={:?} - couldn't allocate flash region",
+ ); + storage_location.address,
+ storage_location.address + unallocated_size,
+ process_name
+ );
+ }
+ } + }
+ return Ok((None, remaining_memory)); + 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 // memory space just for kernel and grant state. We need to make
// sure we allocate enough memory just for that. // sure we allocate enough memory just for that.
diff --git a/kernel/src/sched.rs b/kernel/src/sched.rs 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 --- a/kernel/src/sched.rs
+++ b/kernel/src/sched.rs +++ b/kernel/src/sched.rs
@@ -118,6 +118,13 @@ pub enum SchedulingDecision { @@ -118,6 +118,12 @@ pub enum SchedulingDecision {
TrySleep, TrySleep,
} }
@@ -450,13 +454,12 @@ index 10626a2e1..61401b04a 100644
+pub struct StorageLocation { +pub struct StorageLocation {
+ pub address: usize, + pub address: usize,
+ pub size: usize, + pub size: usize,
+ pub unallocated_size: usize,
+} +}
+ +
/// Main object for the kernel. Each board will need to create one. /// Main object for the kernel. Each board will need to create one.
pub struct Kernel { pub struct Kernel {
/// How many "to-do" items exist at any given time. These include /// 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. /// This holds a pointer to the static array of Process pointers.
processes: &'static [Option<&'static dyn process::ProcessType>], 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 /// A counter which keeps track of how many process identifiers have been
/// created. This is used to create new unique identifiers for processes. /// created. This is used to create new unique identifiers for processes.
process_identifier_max: Cell<usize>, process_identifier_max: Cell<usize>,
@@ -170,9 +180,17 @@ pub enum StoppedExecutingReason { @@ -170,9 +179,17 @@ pub enum StoppedExecutingReason {
impl Kernel { impl Kernel {
pub fn new(processes: &'static [Option<&'static dyn process::ProcessType>]) -> 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), process_identifier_max: Cell::new(0),
grant_counter: Cell::new(0), grant_counter: Cell::new(0),
grants_finalized: Cell::new(false), grants_finalized: Cell::new(false),
@@ -900,4 +918,8 @@ impl Kernel { @@ -900,4 +917,8 @@ impl Kernel {
(return_reason, time_executed_us) (return_reason, time_executed_us)
} }

View File

@@ -31,10 +31,10 @@ index 5465c95f4..e596648f7 100644
} }
} }
diff --git a/kernel/src/sched.rs b/kernel/src/sched.rs 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 --- a/kernel/src/sched.rs
+++ b/kernel/src/sched.rs +++ b/kernel/src/sched.rs
@@ -118,11 +118,20 @@ pub enum SchedulingDecision { @@ -118,10 +118,19 @@ pub enum SchedulingDecision {
TrySleep, TrySleep,
} }
@@ -50,7 +50,6 @@ index 61401b04a..e9a58c018 100644
pub struct StorageLocation { pub struct StorageLocation {
pub address: usize, pub address: usize,
pub size: usize, pub size: usize,
pub unallocated_size: usize,
+ pub storage_type: StorageType, + pub storage_type: StorageType,
} }