Skip to content

Commit

Permalink
Merge #1191
Browse files Browse the repository at this point in the history
1191: kernel: make user-kernel boundary in Chip r=alevy a=bradjc



### Pull Request Overview

This pull request adds a new trait type to the `Chip` trait that represents the code that handles switching between kernelspace and userspace. Because this is a hardware feature, it makes sense to include it with the chip so that a user doesn't accidentally choose the wrong arch for a given MCU.

```rust
pub trait Chip {
    type MPU: mpu::MPU;
    type UserspaceKernelBoundary: syscall::UserspaceKernelBoundary;
    type SysTick: systick::SysTick;
    ...
```

This also simplifies main.rs as now setting up processes and the kernel only requires passing in the chip.

### Testing Strategy

This pull request was tested by running a hail kernel.


### TODO or Help Wanted

n/a


### Documentation Updated

- [x] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [x] Ran `make formatall`.


Co-authored-by: Brad Campbell <bradjc5@gmail.com>
  • Loading branch information
bors[bot] and bradjc committed Oct 29, 2018
2 parents 526edbb + 0891b60 commit 6c3c7d9
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 65 deletions.
4 changes: 1 addition & 3 deletions boards/ek-tm4c1294xl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use capsules::virtual_uart::{UartDevice, UartMux};
use kernel::capabilities;
use kernel::hil;
use kernel::hil::Controller;
use kernel::Chip;
use kernel::Platform;

#[macro_use]
Expand Down Expand Up @@ -255,8 +254,7 @@ pub unsafe fn reset_handler() {
}
kernel::procs::load_processes(
board_kernel,
&cortexm4::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
Expand Down
4 changes: 1 addition & 3 deletions boards/hail/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use kernel::hil::entropy::Entropy32;
use kernel::hil::rng::Rng;
use kernel::hil::spi::SpiMaster;
use kernel::hil::Controller;
use kernel::Chip;
use kernel::Platform;

/// Support routines for debugging I/O.
Expand Down Expand Up @@ -620,8 +619,7 @@ pub unsafe fn reset_handler() {

kernel::procs::load_processes(
board_kernel,
&cortexm4::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
Expand Down
4 changes: 1 addition & 3 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::spi::SpiMaster;
use kernel::hil::Controller;
use kernel::Chip;

use components::adc::AdcComponent;
use components::alarm::AlarmDriverComponent;
Expand Down Expand Up @@ -452,8 +451,7 @@ pub unsafe fn reset_handler() {
}
kernel::procs::load_processes(
board_kernel,
&cortexm4::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
Expand Down
4 changes: 1 addition & 3 deletions boards/launchxl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use kernel::hil;
use kernel::hil::entropy::Entropy32;
use kernel::hil::i2c::I2CMaster;
use kernel::hil::rng::Rng;
use kernel::Chip;

#[macro_use]
pub mod io;
Expand Down Expand Up @@ -312,8 +311,7 @@ pub unsafe fn reset_handler() {

kernel::procs::load_processes(
board_kernel,
&cortexm4::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
Expand Down
3 changes: 1 addition & 2 deletions boards/nordic/nrf51dk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ pub unsafe fn reset_handler() {
}
kernel::procs::load_processes(
board_kernel,
&cortexm0::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
Expand Down
4 changes: 1 addition & 3 deletions boards/nordic/nrf52dk_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use kernel::capabilities;
use kernel::hil;
use kernel::hil::entropy::Entropy32;
use kernel::hil::rng::Rng;
use kernel::Chip;
use nrf5x::rtc::Rtc;

/// Pins for SPI for the flash chip MX25R6435F
Expand Down Expand Up @@ -438,8 +437,7 @@ pub unsafe fn setup_board(
}
kernel::procs::load_processes(
board_kernel,
&cortexm4::syscall::SysCall::new(),
chip.mpu(),
chip,
&_sapps as *const u8,
app_memory,
process_pointers,
Expand Down
8 changes: 8 additions & 0 deletions chips/cc26x2/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use uart;

pub struct Cc26X2 {
mpu: cortexm4::mpu::MPU,
userspace_kernel_boundary: cortexm4::syscall::SysCall,
systick: cortexm4::systick::SysTick,
}

impl Cc26X2 {
pub unsafe fn new() -> Cc26X2 {
Cc26X2 {
mpu: cortexm4::mpu::MPU::new(),
userspace_kernel_boundary: cortexm4::syscall::SysCall::new(),
// The systick clocks with 48MHz by default
systick: cortexm4::systick::SysTick::new_with_calibration(48 * 1000000),
}
Expand All @@ -24,6 +26,7 @@ impl Cc26X2 {

impl kernel::Chip for Cc26X2 {
type MPU = cortexm4::mpu::MPU;
type UserspaceKernelBoundary = cortexm4::syscall::SysCall;
type SysTick = cortexm4::systick::SysTick;

fn mpu(&self) -> &Self::MPU {
Expand All @@ -33,6 +36,11 @@ impl kernel::Chip for Cc26X2 {
fn systick(&self) -> &Self::SysTick {
&self.systick
}

fn userspace_kernel_boundary(&self) -> &Self::UserspaceKernelBoundary {
&self.userspace_kernel_boundary
}

fn service_pending_interrupts(&self) {
unsafe {
while let Some(interrupt) = nvic::next_pending() {
Expand Down
17 changes: 13 additions & 4 deletions chips/nrf51/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@ use nrf5x::peripheral_interrupts;
use radio;
use uart;

pub struct NRF51(());
pub struct NRF51 {
userspace_kernel_boundary: cortexm0::syscall::SysCall,
}

impl NRF51 {
pub unsafe fn new() -> NRF51 {
NRF51(())
NRF51 {
userspace_kernel_boundary: cortexm0::syscall::SysCall::new(),
}
}
}

impl kernel::Chip for NRF51 {
type MPU = ();
type UserspaceKernelBoundary = cortexm0::syscall::SysCall;
type SysTick = ();

fn mpu(&self) -> &Self::MPU {
&self.0
&()
}

fn systick(&self) -> &Self::SysTick {
&self.0
&()
}

fn userspace_kernel_boundary(&self) -> &cortexm0::syscall::SysCall {
&self.userspace_kernel_boundary
}

fn service_pending_interrupts(&self) {
Expand Down
7 changes: 7 additions & 0 deletions chips/nrf52/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use uart;

pub struct NRF52 {
mpu: cortexm4::mpu::MPU,
userspace_kernel_boundary: cortexm4::syscall::SysCall,
systick: cortexm4::systick::SysTick,
}

impl NRF52 {
pub unsafe fn new() -> NRF52 {
NRF52 {
mpu: cortexm4::mpu::MPU::new(),
userspace_kernel_boundary: cortexm4::syscall::SysCall::new(),
// The NRF52's systick is uncalibrated, but is clocked from the
// 64Mhz CPU clock.
systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
Expand All @@ -29,6 +31,7 @@ impl NRF52 {

impl kernel::Chip for NRF52 {
type MPU = cortexm4::mpu::MPU;
type UserspaceKernelBoundary = cortexm4::syscall::SysCall;
type SysTick = cortexm4::systick::SysTick;

fn mpu(&self) -> &Self::MPU {
Expand All @@ -39,6 +42,10 @@ impl kernel::Chip for NRF52 {
&self.systick
}

fn userspace_kernel_boundary(&self) -> &Self::UserspaceKernelBoundary {
&self.userspace_kernel_boundary
}

fn service_pending_interrupts(&self) {
unsafe {
loop {
Expand Down
11 changes: 9 additions & 2 deletions chips/sam4l/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use usart;
use usbc;

pub struct Sam4l {
pub mpu: cortexm4::mpu::MPU,
pub systick: cortexm4::systick::SysTick,
mpu: cortexm4::mpu::MPU,
userspace_kernel_boundary: cortexm4::syscall::SysCall,
systick: cortexm4::systick::SysTick,
}

impl Sam4l {
Expand Down Expand Up @@ -62,13 +63,15 @@ impl Sam4l {

Sam4l {
mpu: cortexm4::mpu::MPU::new(),
userspace_kernel_boundary: cortexm4::syscall::SysCall::new(),
systick: cortexm4::systick::SysTick::new(),
}
}
}

impl Chip for Sam4l {
type MPU = cortexm4::mpu::MPU;
type UserspaceKernelBoundary = cortexm4::syscall::SysCall;
type SysTick = cortexm4::systick::SysTick;

fn service_pending_interrupts(&self) {
Expand Down Expand Up @@ -163,6 +166,10 @@ impl Chip for Sam4l {
&self.systick
}

fn userspace_kernel_boundary(&self) -> &cortexm4::syscall::SysCall {
&self.userspace_kernel_boundary
}

fn sleep(&self) {
if pm::deep_sleep_ready() {
unsafe {
Expand Down
11 changes: 9 additions & 2 deletions chips/tm4c129x/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ use kernel::Chip;
use uart;

pub struct Tm4c129x {
pub mpu: cortexm4::mpu::MPU,
pub systick: cortexm4::systick::SysTick,
mpu: cortexm4::mpu::MPU,
userspace_kernel_boundary: cortexm4::syscall::SysCall,
systick: cortexm4::systick::SysTick,
}

impl Tm4c129x {
pub unsafe fn new() -> Tm4c129x {
Tm4c129x {
mpu: cortexm4::mpu::MPU::new(),
userspace_kernel_boundary: cortexm4::syscall::SysCall::new(),
systick: cortexm4::systick::SysTick::new(),
}
}
}

impl Chip for Tm4c129x {
type MPU = cortexm4::mpu::MPU;
type UserspaceKernelBoundary = cortexm4::syscall::SysCall;
type SysTick = cortexm4::systick::SysTick;

fn service_pending_interrupts(&self) {
Expand Down Expand Up @@ -58,6 +61,10 @@ impl Chip for Tm4c129x {
&self.systick
}

fn userspace_kernel_boundary(&self) -> &cortexm4::syscall::SysCall {
&self.userspace_kernel_boundary
}

fn sleep(&self) {
/*if pm::deep_sleep_ready() {
unsafe {
Expand Down
3 changes: 3 additions & 0 deletions kernel/src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Interface for chips and boards.

use driver::Driver;
use syscall;

pub mod mpu;
crate mod systick;
Expand All @@ -17,12 +18,14 @@ pub trait Platform {
/// Interface for individual MCUs.
pub trait Chip {
type MPU: mpu::MPU;
type UserspaceKernelBoundary: syscall::UserspaceKernelBoundary;
type SysTick: systick::SysTick;

fn service_pending_interrupts(&self);
fn has_pending_interrupts(&self) -> bool;
fn mpu(&self) -> &Self::MPU;
fn systick(&self) -> &Self::SysTick;
fn userspace_kernel_boundary(&self) -> &Self::UserspaceKernelBoundary;
fn sleep(&self);
unsafe fn atomic<F, R>(&self, f: F) -> R
where
Expand Down
Loading

0 comments on commit 6c3c7d9

Please sign in to comment.