Skip to content

Commit

Permalink
Merge pull request tock#4036 from tock/components-loader
Browse files Browse the repository at this point in the history
Components: Add component for sequential process loader
  • Loading branch information
alevy authored Jun 19, 2024
2 parents eacb24b + f90ee05 commit b0f8201
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 192 deletions.
1 change: 1 addition & 0 deletions boards/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod l3gd20;
pub mod led;
pub mod led_matrix;
pub mod lldb;
pub mod loader;
pub mod lpm013m126;
pub mod lps22hb;
pub mod lps25hb;
Expand Down
5 changes: 5 additions & 0 deletions boards/components/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

pub mod sequential;
115 changes: 115 additions & 0 deletions boards/components/src/loader/sequential.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

//! Component for creating a sequential process loader.
//!
//! `ProcessLoaderSequentialComponent` uses the standard Tock assumptions about
//! where processes are stored in flash and what RAM is allocated for process
//! use.
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::deferred_call::DeferredCallClient;
use kernel::platform::chip::Chip;
use kernel::process::ProcessLoadingAsync;

#[macro_export]
macro_rules! process_loader_sequential_component_static {
($C:ty, $NUMPROCS:expr $(,)?) => {{
let loader = kernel::static_buf!(kernel::process::SequentialProcessLoaderMachine<
$C,
>);
let process_binary_array = kernel::static_buf!(
[Option<kernel::process::ProcessBinary>; $NUMPROCS]
);

(loader, process_binary_array)
};};
}

pub type ProcessLoaderSequentialComponentType<C> =
kernel::process::SequentialProcessLoaderMachine<'static, C>;

pub struct ProcessLoaderSequentialComponent<C: Chip + 'static, const NUM_PROCS: usize> {
checker: &'static kernel::process::ProcessCheckerMachine,
processes: &'static mut [Option<&'static dyn kernel::process::Process>],
kernel: &'static kernel::Kernel,
chip: &'static C,
fault_policy: &'static dyn kernel::process::ProcessFaultPolicy,
appid_policy: &'static dyn kernel::process_checker::AppIdPolicy,
}

impl<C: Chip, const NUM_PROCS: usize> ProcessLoaderSequentialComponent<C, NUM_PROCS> {
pub fn new(
checker: &'static kernel::process::ProcessCheckerMachine,
processes: &'static mut [Option<&'static dyn kernel::process::Process>],
kernel: &'static kernel::Kernel,
chip: &'static C,
fault_policy: &'static dyn kernel::process::ProcessFaultPolicy,
appid_policy: &'static dyn kernel::process_checker::AppIdPolicy,
) -> Self {
Self {
checker,
processes,
kernel,
chip,
fault_policy,
appid_policy,
}
}
}

impl<C: Chip, const NUM_PROCS: usize> Component for ProcessLoaderSequentialComponent<C, NUM_PROCS> {
type StaticInput = (
&'static mut MaybeUninit<kernel::process::SequentialProcessLoaderMachine<'static, C>>,
&'static mut MaybeUninit<[Option<kernel::process::ProcessBinary>; NUM_PROCS]>,
);

type Output = &'static kernel::process::SequentialProcessLoaderMachine<'static, C>;

fn finalize(mut self, s: Self::StaticInput) -> Self::Output {
let proc_manage_cap =
kernel::create_capability!(kernel::capabilities::ProcessManagementCapability);

const ARRAY_REPEAT_VALUE: Option<kernel::process::ProcessBinary> = None;
let process_binary_array = s.1.write([ARRAY_REPEAT_VALUE; NUM_PROCS]);

// These symbols are defined in the standard Tock linker script.
extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
/// End of the ROM region containing app images.
static _eapps: u8;
/// Beginning of the RAM region for app memory.
static mut _sappmem: u8;
/// End of the RAM region for app memory.
static _eappmem: u8;
}

let loader = unsafe {
s.0.write(kernel::process::SequentialProcessLoaderMachine::new(
self.checker,
*core::ptr::addr_of_mut!(self.processes),
process_binary_array,
self.kernel,
self.chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
self.fault_policy,
self.appid_policy,
&proc_manage_cap,
))
};
self.checker.set_client(loader);
loader.register();
loader.start();
loader
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
use core::ptr::{addr_of, addr_of_mut};

use kernel::component::Component;
use kernel::deferred_call::DeferredCallClient;
use kernel::hil::led::LedLow;
use kernel::hil::time::Counter;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::process::ProcessLoadingAsync;
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::{capabilities, create_capability, static_init};
use nrf52840::gpio::Pin;
Expand Down Expand Up @@ -193,8 +191,6 @@ pub unsafe fn main() {

// Create capabilities that the board needs to call certain protected kernel
// functions.
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -275,48 +271,19 @@ pub unsafe fn main() {
let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
.finalize(components::process_checker_machine_component_static!());

// These symbols are defined in the linker script.
extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
/// End of the ROM region containing app images.
static _eapps: u8;
/// Beginning of the RAM region for app memory.
static mut _sappmem: u8;
/// End of the RAM region for app memory.
static _eappmem: u8;
}

let process_binary_array = static_init!(
[Option<kernel::process::ProcessBinary>; NUM_PROCS],
[None, None, None, None, None, None, None, None]
);

let loader = static_init!(
kernel::process::SequentialProcessLoaderMachine<
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
>,
kernel::process::SequentialProcessLoaderMachine::new(
checker,
&mut *addr_of_mut!(PROCESSES),
process_binary_array,
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&FAULT_RESPONSE,
assigner,
&process_management_capability
)
);

checker.set_client(loader);
// Create and start the asynchronous process loader.
let _loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
checker,
&mut *addr_of_mut!(PROCESSES),
board_kernel,
chip,
&FAULT_RESPONSE,
assigner,
)
.finalize(components::process_loader_sequential_component_static!(
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
NUM_PROCS
));

//--------------------------------------------------------------------------
// PLATFORM SETUP, SCHEDULER, AND START KERNEL LOOP
Expand All @@ -333,9 +300,6 @@ pub unsafe fn main() {
systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
};

loader.register();
loader.start();

board_kernel.kernel_loop(
&platform,
chip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use core::ptr::{addr_of, addr_of_mut};

use kernel::component::Component;
use kernel::deferred_call::DeferredCallClient;
use kernel::hil::led::LedLow;
use kernel::hil::time::Counter;
use kernel::platform::{KernelResources, SyscallDriverLookup};
Expand Down Expand Up @@ -210,8 +209,6 @@ pub unsafe fn main() {

// Create capabilities that the board needs to call certain protected kernel
// functions.
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -309,48 +306,19 @@ pub unsafe fn main() {
let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
.finalize(components::process_checker_machine_component_static!());

// These symbols are defined in the linker script.
extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
/// End of the ROM region containing app images.
static _eapps: u8;
/// Beginning of the RAM region for app memory.
static mut _sappmem: u8;
/// End of the RAM region for app memory.
static _eappmem: u8;
}

let process_binary_array = static_init!(
[Option<kernel::process::ProcessBinary>; NUM_PROCS],
[None, None, None, None, None, None, None, None]
);

let loader = static_init!(
kernel::process::SequentialProcessLoaderMachine<
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
>,
kernel::process::SequentialProcessLoaderMachine::new(
checker,
&mut *addr_of_mut!(PROCESSES),
process_binary_array,
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&FAULT_RESPONSE,
assigner,
&process_management_capability
)
);

checker.set_client(loader);
// Create and start the asynchronous process loader.
let loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
checker,
&mut *addr_of_mut!(PROCESSES),
board_kernel,
chip,
&FAULT_RESPONSE,
assigner,
)
.finalize(components::process_loader_sequential_component_static!(
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
NUM_PROCS
));

//--------------------------------------------------------------------------
// PLATFORM SETUP, SCHEDULER, AND START KERNEL LOOP
Expand All @@ -369,10 +337,7 @@ pub unsafe fn main() {
systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
}
);

loader.set_client(platform);
loader.register();
loader.start();

let _ = pconsole.start();

Expand Down
60 changes: 13 additions & 47 deletions boards/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use kernel::hil::radio;
use kernel::hil::radio::{RadioConfig, RadioData};
use kernel::hil::symmetric_encryption::AES128;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::process::ProcessLoadingAsync;
use kernel::scheduler::round_robin::RoundRobinSched;

//use kernel::hil::time::Alarm;
Expand Down Expand Up @@ -349,7 +348,6 @@ pub unsafe fn main() {

// Create capabilities that the board needs to call certain protected kernel
// functions.
let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
let main_cap = create_capability!(capabilities::MainLoopCapability);
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

Expand Down Expand Up @@ -717,48 +715,19 @@ pub unsafe fn main() {
let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
.finalize(components::process_checker_machine_component_static!());

// These symbols are defined in the linker script.
extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
/// End of the ROM region containing app images.
static _eapps: u8;
/// Beginning of the RAM region for app memory.
static mut _sappmem: u8;
/// End of the RAM region for app memory.
static _eappmem: u8;
}

let process_binary_array = static_init!(
[Option<kernel::process::ProcessBinary>; NUM_PROCS],
[None, None, None, None]
);

let loader = static_init!(
kernel::process::SequentialProcessLoaderMachine<
sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
>,
kernel::process::SequentialProcessLoaderMachine::new(
checker,
&mut *addr_of_mut!(PROCESSES),
process_binary_array,
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&FAULT_RESPONSE,
assigner,
&process_mgmt_cap
)
);

checker.set_client(loader);
// Create and start the asynchronous process loader.
let _loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
checker,
&mut *addr_of_mut!(PROCESSES),
board_kernel,
chip,
&FAULT_RESPONSE,
assigner,
)
.finalize(components::process_loader_sequential_component_static!(
sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
NUM_PROCS
));

let imix = Imix {
pconsole,
Expand Down Expand Up @@ -846,8 +815,5 @@ pub unsafe fn main() {

debug!("Initialization complete. Entering main loop");

loader.register();
loader.start();

board_kernel.kernel_loop(&imix, chip, Some(&imix.ipc), &main_cap);
}
Loading

0 comments on commit b0f8201

Please sign in to comment.