Skip to content

Commit 3d4f9c8

Browse files
Merge #2342
2342: use const generics to tie number of IPC regions to number of processes on board r=alevy a=hudson-ayers ### Pull Request Overview This pull request fixes #1583 . Now, IPC works even if one of the apps has an index higher than 7. Further, when boards are compiled with support for less than 8 processes, `(8-NUM_PROCS)*48` bytes of memory are saved. ### Testing Strategy This pull request was tested by flashing the IPC apps on an nrf52840dk. ### TODO or Help Wanted N/A ### Documentation Updated - [x] no updates are required. ### Formatting - [x] Ran `make prepush`. Co-authored-by: Hudson Ayers <hayers@stanford.edu>
2 parents 797111e + 1f84d76 commit 3d4f9c8

File tree

24 files changed

+88
-40
lines changed

24 files changed

+88
-40
lines changed

boards/acd52832/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct Platform {
7070
>,
7171
rng: &'static capsules::rng::RngDriver<'static>,
7272
temp: &'static capsules::temperature::TemperatureSensor<'static>,
73-
ipc: kernel::ipc::IPC,
73+
ipc: kernel::ipc::IPC<NUM_PROCS>,
7474
alarm: &'static capsules::alarm::AlarmDriver<
7575
'static,
7676
VirtualMuxAlarm<'static, nrf52832::rtc::Rtc<'static>>,

boards/arty_e21/src/main.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct ArtyE21 {
5656
hil::led::LedHigh<'static, arty_e21_chip::gpio::GpioPin<'static>>,
5757
>,
5858
button: &'static capsules::button::Button<'static, arty_e21_chip::gpio::GpioPin<'static>>,
59-
// ipc: kernel::ipc::IPC,
59+
// ipc: kernel::ipc::IPC<NUM_PROCS>,
6060
}
6161

6262
/// Mapping of integer syscalls to objects that implement syscalls.
@@ -249,5 +249,11 @@ pub unsafe fn reset_handler() {
249249

250250
let scheduler = components::sched::priority::PriorityComponent::new(board_kernel).finalize(());
251251

252-
board_kernel.kernel_loop(&artye21, chip, None, scheduler, &main_loop_cap);
252+
board_kernel.kernel_loop(
253+
&artye21,
254+
chip,
255+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
256+
scheduler,
257+
&main_loop_cap,
258+
);
253259
}

boards/clue_nrf52840/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub struct Platform {
119119
button: &'static capsules::button::Button<'static, nrf52::gpio::GPIOPin<'static>>,
120120
screen: &'static capsules::screen::Screen<'static>,
121121
rng: &'static capsules::rng::RngDriver<'static>,
122-
ipc: kernel::ipc::IPC,
122+
ipc: kernel::ipc::IPC<NUM_PROCS>,
123123
alarm: &'static capsules::alarm::AlarmDriver<
124124
'static,
125125
capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>,

boards/earlgrey-nexysvideo/src/main.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ mod multi_alarm_test;
3131
pub mod io;
3232
pub mod usb;
3333

34+
const NUM_PROCS: usize = 4;
35+
3436
//
3537
// Actual memory for holding the active process structures. Need an empty list
3638
// at least.
37-
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; 4] =
38-
[None, None, None, None];
39+
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; 4] = [None; NUM_PROCS];
3940

4041
static mut CHIP: Option<
4142
&'static earlgrey::chip::EarlGrey<
@@ -331,5 +332,11 @@ pub unsafe fn reset_handler() {
331332
debug!("OpenTitan initialisation complete. Entering main loop");
332333

333334
let scheduler = components::sched::priority::PriorityComponent::new(board_kernel).finalize(());
334-
board_kernel.kernel_loop(&earlgrey_nexysvideo, chip, None, scheduler, &main_loop_cap);
335+
board_kernel.kernel_loop(
336+
&earlgrey_nexysvideo,
337+
chip,
338+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
339+
scheduler,
340+
&main_loop_cap,
341+
);
335342
}

boards/hail/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct Hail {
7070
led: &'static capsules::led::LedDriver<'static, LedLow<'static, sam4l::gpio::GPIOPin<'static>>>,
7171
button: &'static capsules::button::Button<'static, sam4l::gpio::GPIOPin<'static>>,
7272
rng: &'static capsules::rng::RngDriver<'static>,
73-
ipc: kernel::ipc::IPC,
73+
ipc: kernel::ipc::IPC<NUM_PROCS>,
7474
crc: &'static capsules::crc::Crc<'static, sam4l::crccu::Crccu<'static>>,
7575
dac: &'static capsules::dac::Dac<'static>,
7676
}

boards/hifive1/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,11 @@ pub unsafe fn reset_handler() {
257257

258258
let scheduler = components::sched::cooperative::CooperativeComponent::new(&PROCESSES)
259259
.finalize(components::coop_component_helper!(NUM_PROCS));
260-
board_kernel.kernel_loop(&hifive1, chip, None, scheduler, &main_loop_cap);
260+
board_kernel.kernel_loop(
261+
&hifive1,
262+
chip,
263+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
264+
scheduler,
265+
&main_loop_cap,
266+
);
261267
}

boards/imix/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct Imix {
127127
'static,
128128
VirtualSpiMasterDevice<'static, sam4l::spi::SpiHw>,
129129
>,
130-
ipc: kernel::ipc::IPC,
130+
ipc: kernel::ipc::IPC<NUM_PROCS>,
131131
ninedof: &'static capsules::ninedof::NineDof<'static>,
132132
radio_driver: &'static capsules::ieee802154::RadioDriver<'static>,
133133
udp_driver: &'static capsules::net::udp::UDPDriver<'static>,

boards/imxrt1050-evkb/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct Imxrt1050EVKB {
7373
button: &'static capsules::button::Button<'static, imxrt1050::gpio::Pin<'static>>,
7474
console: &'static capsules::console::Console<'static>,
7575
gpio: &'static capsules::gpio::GPIO<'static, imxrt1050::gpio::Pin<'static>>,
76-
ipc: kernel::ipc::IPC,
76+
ipc: kernel::ipc::IPC<NUM_PROCS>,
7777
led: &'static capsules::led::LedDriver<'static, LedLow<'static, imxrt1050::gpio::Pin<'static>>>,
7878
ninedof: &'static capsules::ninedof::NineDof<'static>,
7979
}

boards/litex/arty/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -466,5 +466,11 @@ pub unsafe fn reset_handler() {
466466

467467
let scheduler = components::sched::cooperative::CooperativeComponent::new(&PROCESSES)
468468
.finalize(components::coop_component_helper!(NUM_PROCS));
469-
board_kernel.kernel_loop(&litex_arty, chip, None, scheduler, &main_loop_cap);
469+
board_kernel.kernel_loop(
470+
&litex_arty,
471+
chip,
472+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
473+
scheduler,
474+
&main_loop_cap,
475+
);
470476
}

boards/litex/sim/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,11 @@ pub unsafe fn reset_handler() {
415415

416416
let scheduler = components::sched::cooperative::CooperativeComponent::new(&PROCESSES)
417417
.finalize(components::coop_component_helper!(NUM_PROCS));
418-
board_kernel.kernel_loop(&litex_sim, chip, None, scheduler, &main_loop_cap);
418+
board_kernel.kernel_loop(
419+
&litex_sim,
420+
chip,
421+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
422+
scheduler,
423+
&main_loop_cap,
424+
);
419425
}

boards/microbit_v2/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct Platform {
9292
ninedof: &'static capsules::ninedof::NineDof<'static>,
9393
lsm303agr: &'static capsules::lsm303agr::Lsm303agrI2C<'static>,
9494
temperature: &'static capsules::temperature::TemperatureSensor<'static>,
95-
ipc: kernel::ipc::IPC,
95+
ipc: kernel::ipc::IPC<NUM_PROCS>,
9696
adc: &'static capsules::adc::AdcVirtualized<'static>,
9797
alarm: &'static capsules::alarm::AlarmDriver<
9898
'static,

boards/msp_exp432p401r/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct MspExp432P401R {
5656
'static,
5757
capsules::virtual_alarm::VirtualMuxAlarm<'static, msp432::timer::TimerA<'static>>,
5858
>,
59-
ipc: kernel::ipc::IPC,
59+
ipc: kernel::ipc::IPC<NUM_PROCS>,
6060
adc: &'static capsules::adc::AdcDedicated<'static, msp432::adc::Adc<'static>>,
6161
}
6262

boards/nano33ble/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct Platform {
110110
gpio: &'static capsules::gpio::GPIO<'static, nrf52::gpio::GPIOPin<'static>>,
111111
led: &'static capsules::led::LedDriver<'static, LedLow<'static, nrf52::gpio::GPIOPin<'static>>>,
112112
rng: &'static capsules::rng::RngDriver<'static>,
113-
ipc: kernel::ipc::IPC,
113+
ipc: kernel::ipc::IPC<NUM_PROCS>,
114114
alarm: &'static capsules::alarm::AlarmDriver<
115115
'static,
116116
capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>,

boards/nordic/nrf52840_dongle/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct Platform {
8888
>,
8989
rng: &'static capsules::rng::RngDriver<'static>,
9090
temp: &'static capsules::temperature::TemperatureSensor<'static>,
91-
ipc: kernel::ipc::IPC,
91+
ipc: kernel::ipc::IPC<NUM_PROCS>,
9292
analog_comparator: &'static capsules::analog_comparator::AnalogComparator<
9393
'static,
9494
nrf52840::acomp::Comparator<'static>,

boards/nordic/nrf52840dk/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub struct Platform {
162162
>,
163163
rng: &'static capsules::rng::RngDriver<'static>,
164164
temp: &'static capsules::temperature::TemperatureSensor<'static>,
165-
ipc: kernel::ipc::IPC,
165+
ipc: kernel::ipc::IPC<NUM_PROCS>,
166166
analog_comparator: &'static capsules::analog_comparator::AnalogComparator<
167167
'static,
168168
nrf52840::acomp::Comparator<'static>,

boards/nordic/nrf52dk/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub struct Platform {
147147
>,
148148
rng: &'static capsules::rng::RngDriver<'static>,
149149
temp: &'static capsules::temperature::TemperatureSensor<'static>,
150-
ipc: kernel::ipc::IPC,
150+
ipc: kernel::ipc::IPC<NUM_PROCS>,
151151
analog_comparator: &'static capsules::analog_comparator::AnalogComparator<
152152
'static,
153153
nrf52832::acomp::Comparator<'static>,

boards/nucleo_f429zi/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
4848
/// capsules for this platform.
4949
struct NucleoF429ZI {
5050
console: &'static capsules::console::Console<'static>,
51-
ipc: kernel::ipc::IPC,
51+
ipc: kernel::ipc::IPC<NUM_PROCS>,
5252
led: &'static capsules::led::LedDriver<
5353
'static,
5454
LedHigh<'static, stm32f429zi::gpio::Pin<'static>>,

boards/nucleo_f446re/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
5050
/// capsules for this platform.
5151
struct NucleoF446RE {
5252
console: &'static capsules::console::Console<'static>,
53-
ipc: kernel::ipc::IPC,
53+
ipc: kernel::ipc::IPC<NUM_PROCS>,
5454
led: &'static capsules::led::LedDriver<
5555
'static,
5656
LedHigh<'static, stm32f446re::gpio::Pin<'static>>,

boards/redboard_artemis_nano/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,11 @@ pub unsafe fn reset_handler() {
264264
let scheduler = components::sched::round_robin::RoundRobinComponent::new(&PROCESSES)
265265
.finalize(components::rr_component_helper!(NUM_PROCS));
266266

267-
board_kernel.kernel_loop(artemis_nano, chip, None, scheduler, &main_loop_cap);
267+
board_kernel.kernel_loop(
268+
artemis_nano,
269+
chip,
270+
None::<&kernel::ipc::IPC<NUM_PROCS>>,
271+
scheduler,
272+
&main_loop_cap,
273+
);
268274
}

boards/stm32f3discovery/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
5353
/// capsules for this platform.
5454
struct STM32F3Discovery {
5555
console: &'static capsules::console::Console<'static>,
56-
ipc: kernel::ipc::IPC,
56+
ipc: kernel::ipc::IPC<NUM_PROCS>,
5757
gpio: &'static capsules::gpio::GPIO<'static, stm32f303xc::gpio::Pin<'static>>,
5858
led: &'static capsules::led::LedDriver<
5959
'static,

boards/stm32f412gdiscovery/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
4747
/// capsules for this platform.
4848
struct STM32F412GDiscovery {
4949
console: &'static capsules::console::Console<'static>,
50-
ipc: kernel::ipc::IPC,
50+
ipc: kernel::ipc::IPC<NUM_PROCS>,
5151
led:
5252
&'static capsules::led::LedDriver<'static, LedLow<'static, stm32f412g::gpio::Pin<'static>>>,
5353
button: &'static capsules::button::Button<'static, stm32f412g::gpio::Pin<'static>>,

boards/teensy40/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct Teensy40 {
3434
led:
3535
&'static capsules::led::LedDriver<'static, LedHigh<'static, imxrt1060::gpio::Pin<'static>>>,
3636
console: &'static capsules::console::Console<'static>,
37-
ipc: kernel::ipc::IPC,
37+
ipc: kernel::ipc::IPC<NUM_PROCS>,
3838
alarm: &'static capsules::alarm::AlarmDriver<
3939
'static,
4040
capsules::virtual_alarm::VirtualMuxAlarm<'static, imxrt1060::gpt::Gpt1<'static>>,

kernel/src/ipc.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,38 @@ pub enum IPCCallbackType {
2727
}
2828

2929
/// State that is stored in each process's grant region to support IPC.
30-
#[derive(Default)]
31-
struct IPCData {
30+
struct IPCData<const NUM_PROCS: usize> {
3231
/// An array of app slices that this application has shared with other
3332
/// applications.
34-
shared_memory: [Option<AppSlice<Shared, u8>>; 8],
33+
shared_memory: [Option<AppSlice<Shared, u8>>; NUM_PROCS],
3534
/// An array of callbacks this process has registered to receive callbacks
3635
/// from other services.
37-
client_callbacks: [Option<Callback>; 8],
36+
client_callbacks: [Option<Callback>; NUM_PROCS],
3837
/// The callback setup by a service. Each process can only be one service.
3938
callback: Option<Callback>,
4039
}
4140

41+
impl<const NUM_PROCS: usize> Default for IPCData<NUM_PROCS> {
42+
fn default() -> IPCData<NUM_PROCS> {
43+
// need this until const_in_array_repeat_expressions is stable
44+
const NONE_APPSLICE: Option<AppSlice<Shared, u8>> = None;
45+
IPCData {
46+
shared_memory: [NONE_APPSLICE; NUM_PROCS],
47+
client_callbacks: [None; NUM_PROCS],
48+
callback: None,
49+
}
50+
}
51+
}
52+
4253
/// The IPC mechanism struct.
43-
pub struct IPC {
54+
pub struct IPC<const NUM_PROCS: usize> {
4455
/// The grant regions for each process that holds the per-process IPC data.
45-
data: Grant<IPCData>,
56+
data: Grant<IPCData<NUM_PROCS>>,
4657
}
4758

48-
impl IPC {
49-
pub fn new(kernel: &'static Kernel, capability: &dyn MemoryAllocationCapability) -> IPC {
50-
IPC {
59+
impl<const NUM_PROCS: usize> IPC<NUM_PROCS> {
60+
pub fn new(kernel: &'static Kernel, capability: &dyn MemoryAllocationCapability) -> Self {
61+
Self {
5162
data: kernel.create_grant(capability),
5263
}
5364
}
@@ -106,7 +117,7 @@ impl IPC {
106117
}
107118
}
108119

109-
impl Driver for IPC {
120+
impl<const NUM_PROCS: usize> Driver for IPC<NUM_PROCS> {
110121
/// subscribe enables processes using IPC to register callbacks that fire
111122
/// when notify() is called.
112123
fn subscribe(
@@ -151,7 +162,7 @@ impl Driver for IPC {
151162
.enter(app_id, |data, _| {
152163
match otherapp.map_or(None, |oa| oa.index()) {
153164
Some(i) => {
154-
if i > 8 {
165+
if i >= NUM_PROCS {
155166
ReturnCode::EINVAL
156167
} else {
157168
data.client_callbacks[i] = callback;

kernel/src/sched.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,11 @@ impl Kernel {
454454
///
455455
/// Most of the behavior of this loop is controlled by the `Scheduler`
456456
/// implementation in use.
457-
pub fn kernel_loop<P: Platform, C: Chip, SC: Scheduler<C>>(
457+
pub fn kernel_loop<P: Platform, C: Chip, SC: Scheduler<C>, const NUM_PROCS: usize>(
458458
&self,
459459
platform: &P,
460460
chip: &C,
461-
ipc: Option<&ipc::IPC>,
461+
ipc: Option<&ipc::IPC<NUM_PROCS>>,
462462
scheduler: &SC,
463463
_capability: &dyn capabilities::MainLoopCapability,
464464
) -> ! {
@@ -551,13 +551,13 @@ impl Kernel {
551551
/// cooperatively). Notably, time spent in this function by the kernel,
552552
/// executing system calls or merely setting up the switch to/from
553553
/// userspace, is charged to the process.
554-
unsafe fn do_process<P: Platform, C: Chip, S: Scheduler<C>>(
554+
unsafe fn do_process<P: Platform, C: Chip, S: Scheduler<C>, const NUM_PROCS: usize>(
555555
&self,
556556
platform: &P,
557557
chip: &C,
558558
scheduler: &S,
559559
process: &dyn process::ProcessType,
560-
ipc: Option<&crate::ipc::IPC>,
560+
ipc: Option<&crate::ipc::IPC<NUM_PROCS>>,
561561
timeslice_us: Option<u32>,
562562
) -> (StoppedExecutingReason, Option<u32>) {
563563
// We must use a dummy scheduler timer if the process should be executed

0 commit comments

Comments
 (0)