Skip to content

Commit 6571ffa

Browse files
Lawrence Esswoodalevy
authored andcommitted
Fix ProcessArray
Current pattern for process array is UB as both the scheduler and process loading logic have concurrent &mut references to it. This change adds Cells internally. It also adds in new wrapper types / constructor methods so that boards do not need to be so aware of the exact types they are allocating. This makes future changes to the main process array not need to touch every board (like this has to). Change-Id: I8f200e0051af618076c16b0d512bf20863342769
1 parent 1eccd0b commit 6571ffa

File tree

42 files changed

+348
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+348
-284
lines changed

boards/apollo3/redboard_artemis_nano/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mod tests;
4242
const NUM_PROCS: usize = 4;
4343

4444
// Actual memory for holding the active process structures.
45-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] = [None; 4];
45+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
4646

4747
// Static reference to chip for panic dumps.
4848
static mut CHIP: Option<&'static apollo3::chip::Apollo3<Apollo3DefaultPeripherals>> = None;
@@ -435,7 +435,6 @@ unsafe fn setup() -> (
435435
core::ptr::addr_of_mut!(_sappmem),
436436
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
437437
),
438-
&mut *addr_of_mut!(PROCESSES),
439438
&FAULT_RESPONSE,
440439
&process_mgmt_cap,
441440
)

boards/arty_e21/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
3636
capsules_system::process_policies::PanicFaultPolicy {};
3737

3838
// Actual memory for holding the active process structures.
39-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
40-
[None, None, None, None];
39+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
4140

4241
// Reference to the chip for panic dumps.
4342
static mut CHIP: Option<&'static arty_e21_chip::chip::ArtyExx<ArtyExxDefaultPeripherals>> = None;
@@ -289,7 +288,6 @@ unsafe fn start() -> (
289288
core::ptr::addr_of_mut!(_sappmem),
290289
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
291290
),
292-
&mut *addr_of_mut!(PROCESSES),
293291
&FAULT_RESPONSE,
294292
&process_mgmt_cap,
295293
)

boards/clue_nrf52840/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ const FAULT_RESPONSE: capsules_system::process_policies::StopWithDebugFaultPolic
110110
// Number of concurrent processes this platform supports.
111111
const NUM_PROCS: usize = 8;
112112

113-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
114-
[None; NUM_PROCS];
113+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
115114

116115
static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
117116
static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
@@ -827,7 +826,6 @@ unsafe fn start() -> (
827826
core::ptr::addr_of_mut!(_sappmem),
828827
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
829828
),
830-
&mut *addr_of_mut!(PROCESSES),
831829
&FAULT_RESPONSE,
832830
&process_management_capability,
833831
)

boards/components/src/loader/sequential.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub struct ProcessLoaderSequentialComponent<
3838
const NUM_PROCS: usize,
3939
> {
4040
checker: &'static kernel::process::ProcessCheckerMachine,
41-
processes: &'static mut [Option<&'static dyn kernel::process::Process>],
4241
kernel: &'static kernel::Kernel,
4342
chip: &'static C,
4443
fault_policy: &'static dyn kernel::process::ProcessFaultPolicy,
@@ -51,7 +50,6 @@ impl<C: Chip, D: ProcessStandardDebug, const NUM_PROCS: usize>
5150
{
5251
pub fn new(
5352
checker: &'static kernel::process::ProcessCheckerMachine,
54-
processes: &'static mut [Option<&'static dyn kernel::process::Process>],
5553
kernel: &'static kernel::Kernel,
5654
chip: &'static C,
5755
fault_policy: &'static dyn kernel::process::ProcessFaultPolicy,
@@ -60,7 +58,6 @@ impl<C: Chip, D: ProcessStandardDebug, const NUM_PROCS: usize>
6058
) -> Self {
6159
Self {
6260
checker,
63-
processes,
6461
kernel,
6562
chip,
6663
fault_policy,
@@ -87,39 +84,22 @@ impl<C: Chip, D: ProcessStandardDebug, const NUM_PROCS: usize> Component
8784
const ARRAY_REPEAT_VALUE: Option<kernel::process::ProcessBinary> = None;
8885
let process_binary_array = s.1.write([ARRAY_REPEAT_VALUE; NUM_PROCS]);
8986

90-
// These symbols are defined in the standard Tock linker script.
91-
extern "C" {
92-
/// Beginning of the ROM region containing app images.
93-
static _sapps: u8;
94-
/// End of the ROM region containing app images.
95-
static _eapps: u8;
96-
/// Beginning of the RAM region for app memory.
97-
static mut _sappmem: u8;
98-
/// End of the RAM region for app memory.
99-
static _eappmem: u8;
100-
}
87+
let (flash, ram) = unsafe { kernel::process_loading::get_mems() };
10188

102-
let loader = unsafe {
89+
let loader =
10390
s.0.write(kernel::process::SequentialProcessLoaderMachine::new(
10491
self.checker,
105-
*core::ptr::addr_of_mut!(self.processes),
10692
process_binary_array,
10793
self.kernel,
10894
self.chip,
109-
core::slice::from_raw_parts(
110-
core::ptr::addr_of!(_sapps),
111-
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
112-
),
113-
core::slice::from_raw_parts_mut(
114-
core::ptr::addr_of_mut!(_sappmem),
115-
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
116-
),
95+
flash,
96+
ram,
11797
self.fault_policy,
11898
self.storage_policy,
11999
self.appid_policy,
120100
&proc_manage_cap,
121-
))
122-
};
101+
));
102+
123103
self.checker.set_client(loader);
124104
loader.register();
125105
loader.start();

boards/components/src/sched/cooperative.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
use core::mem::MaybeUninit;
1919
use kernel::component::Component;
20-
use kernel::process::Process;
2120
use kernel::scheduler::cooperative::{CoopProcessNode, CooperativeSched};
21+
use kernel::ProcEntry;
2222

2323
#[macro_export]
2424
macro_rules! cooperative_component_static {
@@ -34,13 +34,11 @@ macro_rules! cooperative_component_static {
3434
}
3535

3636
pub struct CooperativeComponent<const NUM_PROCS: usize> {
37-
processes: &'static [Option<&'static dyn Process>],
37+
processes: &'static [ProcEntry],
3838
}
3939

4040
impl<const NUM_PROCS: usize> CooperativeComponent<NUM_PROCS> {
41-
pub fn new(
42-
processes: &'static [Option<&'static dyn Process>],
43-
) -> CooperativeComponent<NUM_PROCS> {
41+
pub fn new(processes: &'static [ProcEntry]) -> CooperativeComponent<NUM_PROCS> {
4442
CooperativeComponent { processes }
4543
}
4644
}
@@ -59,7 +57,7 @@ impl<const NUM_PROCS: usize> Component for CooperativeComponent<NUM_PROCS> {
5957
let nodes = static_buffer.1.write([UNINIT; NUM_PROCS]);
6058

6159
for (i, node) in nodes.iter_mut().enumerate() {
62-
let init_node = node.write(CoopProcessNode::new(&self.processes[i]));
60+
let init_node = node.write(CoopProcessNode::new(&self.processes[i].proc_ref));
6361
scheduler.processes.push_head(init_node);
6462
}
6563
scheduler

boards/components/src/sched/mlfq.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use core::mem::MaybeUninit;
1414
use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
1515
use kernel::component::Component;
1616
use kernel::hil::time;
17-
use kernel::process::Process;
1817
use kernel::scheduler::mlfq::{MLFQProcessNode, MLFQSched};
18+
use kernel::ProcEntry;
1919

2020
#[macro_export]
2121
macro_rules! mlfq_component_static {
@@ -39,13 +39,13 @@ macro_rules! mlfq_component_static {
3939

4040
pub struct MLFQComponent<A: 'static + time::Alarm<'static>, const NUM_PROCS: usize> {
4141
alarm_mux: &'static MuxAlarm<'static, A>,
42-
processes: &'static [Option<&'static dyn Process>],
42+
processes: &'static [ProcEntry],
4343
}
4444

4545
impl<A: 'static + time::Alarm<'static>, const NUM_PROCS: usize> MLFQComponent<A, NUM_PROCS> {
4646
pub fn new(
4747
alarm_mux: &'static MuxAlarm<'static, A>,
48-
processes: &'static [Option<&'static dyn Process>],
48+
processes: &'static [ProcEntry],
4949
) -> MLFQComponent<A, NUM_PROCS> {
5050
MLFQComponent {
5151
alarm_mux,
@@ -74,7 +74,7 @@ impl<A: 'static + time::Alarm<'static>, const NUM_PROCS: usize> Component
7474
let nodes = static_buffer.2.write([UNINIT; NUM_PROCS]);
7575

7676
for (i, node) in nodes.iter_mut().enumerate() {
77-
let init_node = node.write(MLFQProcessNode::new(&self.processes[i]));
77+
let init_node = node.write(MLFQProcessNode::new(&self.processes[i].proc_ref));
7878
scheduler.processes[0].push_head(init_node);
7979
}
8080
scheduler

boards/components/src/sched/round_robin.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
// Last modified: 03/31/2020
1818

1919
use core::mem::MaybeUninit;
20+
use kernel::collections::list::ListLink;
2021
use kernel::component::Component;
21-
use kernel::process::Process;
2222
use kernel::scheduler::round_robin::{RoundRobinProcessNode, RoundRobinSched};
23+
use kernel::ProcEntry;
2324

2425
#[macro_export]
2526
macro_rules! round_robin_component_static {
@@ -36,13 +37,11 @@ macro_rules! round_robin_component_static {
3637
}
3738

3839
pub struct RoundRobinComponent<const NUM_PROCS: usize> {
39-
processes: &'static [Option<&'static dyn Process>],
40+
processes: &'static [ProcEntry],
4041
}
4142

4243
impl<const NUM_PROCS: usize> RoundRobinComponent<NUM_PROCS> {
43-
pub fn new(
44-
processes: &'static [Option<&'static dyn Process>],
45-
) -> RoundRobinComponent<NUM_PROCS> {
44+
pub fn new(processes: &'static [ProcEntry]) -> RoundRobinComponent<NUM_PROCS> {
4645
RoundRobinComponent { processes }
4746
}
4847
}

boards/esp32-c3-devkitM-1/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const NUM_PROCS: usize = 4;
3535
//
3636
// Actual memory for holding the active process structures. Need an empty list
3737
// at least.
38-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
39-
[None; NUM_PROCS];
38+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
4039

4140
// Reference to the chip for panic dumps.
4241
static mut CHIP: Option<&'static esp32_c3::chip::Esp32C3<Esp32C3DefaultPeripherals>> = None;
@@ -315,7 +314,6 @@ unsafe fn setup() -> (
315314
core::ptr::addr_of_mut!(_sappmem),
316315
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
317316
),
318-
&mut *addr_of_mut!(PROCESSES),
319317
&FAULT_RESPONSE,
320318
&process_mgmt_cap,
321319
)

boards/hail/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ mod test_take_map_cell;
3939
const NUM_PROCS: usize = 20;
4040

4141
// Actual memory for holding the active process structures.
42-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
43-
[None; NUM_PROCS];
42+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
4443

4544
static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;
4645
static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
@@ -550,7 +549,6 @@ unsafe fn start() -> (
550549
core::ptr::addr_of_mut!(_sappmem),
551550
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
552551
),
553-
&mut *addr_of_mut!(PROCESSES),
554552
fault_policy,
555553
&process_management_capability,
556554
)

boards/hifive1/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ pub const NUM_PROCS: usize = 4;
3636
//
3737
// Actual memory for holding the active process structures. Need an empty list
3838
// at least.
39-
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
40-
[None; NUM_PROCS];
39+
static mut PROCESSES: kernel::ProcessArray<NUM_PROCS> = kernel::Kernel::init_process_array();
4140

4241
// Reference to the chip for panic dumps.
4342
static mut CHIP: Option<&'static e310_g002::chip::E310x<E310G002DefaultPeripherals>> = None;
@@ -165,7 +164,6 @@ fn load_processes_not_inlined<C: Chip>(board_kernel: &'static Kernel, chip: &'st
165164
chip,
166165
app_flash,
167166
app_memory,
168-
unsafe { &mut *addr_of_mut!(PROCESSES) },
169167
&FAULT_RESPONSE,
170168
&process_mgmt_cap,
171169
)

0 commit comments

Comments
 (0)