Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Since 1.2
=========

* [#1044](https://github.com/tock/tock/pull/1044) creates a `Kernel` struct
with a method for the kernel's main loop, instead of a global function in
the kernel's base module. Board configurations (i.e. each board's
`main.rs`), as a result needs to instantiate a statically allocate this new
struct. Arguments to the main loop haven't changed:

```rust
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

board_kernel.kernel_loop(&hail, &mut chip, &mut PROCESSES, Some(&hail.ipc));
```

5 changes: 4 additions & 1 deletion boards/ek-tm4c1294xl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,20 @@ pub unsafe fn reset_handler() {

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

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
///
/// This symbol is defined in the linker script.
static _sapps: u8;
}
kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);
kernel::kernel_loop(&tm4c1294, &mut chip, &mut PROCESSES, Some(&tm4c1294.ipc));
board_kernel.kernel_loop(&tm4c1294, &mut chip, &mut PROCESSES, Some(&tm4c1294.ipc));
}
5 changes: 4 additions & 1 deletion boards/hail/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ pub unsafe fn reset_handler() {

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

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
///
Expand All @@ -496,10 +498,11 @@ pub unsafe fn reset_handler() {
}

kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);
kernel::kernel_loop(&hail, &mut chip, &mut PROCESSES, Some(&hail.ipc));
board_kernel.kernel_loop(&hail, &mut chip, &mut PROCESSES, Some(&hail.ipc));
}
6 changes: 5 additions & 1 deletion boards/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,20 @@ pub unsafe fn reset_handler() {
rf233.start();

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

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
}
kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);

kernel::kernel_loop(&imix, &mut chip, &mut PROCESSES, Some(&imix.ipc));
board_kernel.kernel_loop(&imix, &mut chip, &mut PROCESSES, Some(&imix.ipc));
}
5 changes: 4 additions & 1 deletion boards/launchxl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,22 @@ pub unsafe fn reset_handler() {

let mut chip = cc26x2::chip::Cc26X2::new();

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
}

kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);

kernel::kernel_loop(
board_kernel.kernel_loop(
&launchxl,
&mut chip,
&mut PROCESSES,
Expand Down
6 changes: 5 additions & 1 deletion boards/nordic/nrf51dk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,22 @@ pub unsafe fn reset_handler() {
chip.systick().enable(true);

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

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
}
kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);

kernel::kernel_loop(
board_kernel.kernel_loop(
&platform,
&mut chip,
&mut PROCESSES,
Expand Down
15 changes: 15 additions & 0 deletions boards/nordic/nrf52dk_base/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion boards/nordic/nrf52dk_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,19 @@ pub unsafe fn setup_board(
debug!("Initialization complete. Entering main loop\r");
debug!("{}", &nrf52::ficr::FICR_INSTANCE);

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
}
kernel::procs::load_processes(
board_kernel,
&_sapps as *const u8,
app_memory,
process_pointers,
app_fault_response,
);

kernel::kernel_loop(&platform, &mut chip, process_pointers, Some(&platform.ipc));
board_kernel.kernel_loop(&platform, &mut chip, process_pointers, Some(&platform.ipc));
}
2 changes: 1 addition & 1 deletion kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use platform::systick::SysTick;
pub use platform::{mpu, Chip, Platform};
pub use platform::{ClockInterface, NoClockControl, NO_CLOCK_CONTROL};
pub use returncode::ReturnCode;
pub use sched::kernel_loop;
pub use sched::Kernel;

// Export only select items from the process module. To remove the name conflict
// this cannot be called `process`, so we use a shortened version. These
Expand Down
85 changes: 38 additions & 47 deletions kernel/src/process.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Support for creating and running userspace applications.

use callback::AppId;
use common::cells::VolatileCell;
use common::{Queue, RingBuffer};

use core::cell::Cell;
Expand All @@ -13,6 +12,7 @@ use grant;
use common::math;
use platform::mpu;
use returncode::ReturnCode;
use sched::Kernel;
use syscall::Syscall;
use tbfheader;

Expand Down Expand Up @@ -52,6 +52,7 @@ pub static mut PROCS: &'static mut [Option<&mut Process<'static>>] = &mut [];
/// provided array. How process faults are handled by the kernel is also
/// selected.
pub unsafe fn load_processes(
kernel: &'static Kernel,
start_of_flash: *const u8,
app_memory: &mut [u8],
procs: &mut [Option<&mut Process<'static>>],
Expand All @@ -62,6 +63,7 @@ pub unsafe fn load_processes(
let mut app_memory_size = app_memory.len();
for i in 0..procs.len() {
let (process, flash_offset, memory_offset) = Process::create(
kernel,
apps_in_flash_ptr,
app_memory_ptr,
app_memory_size,
Expand Down Expand Up @@ -95,29 +97,7 @@ pub fn schedule(callback: FunctionCall, appid: AppId) -> bool {

match procs[idx] {
None => false,
Some(ref mut p) => {
// If this app is in the `Fault` state then we shouldn't schedule
// any work for it.
if p.current_state() == State::Fault {
return false;
}

unsafe {
HAVE_WORK.set(HAVE_WORK.get() + 1);
}

let ret = p.tasks.enqueue(Task::FunctionCall(callback));

// Make a note that we lost this callback if the enqueue function
// fails.
if ret == false {
p.debug
.dropped_callback_count
.set(p.debug.dropped_callback_count.get() + 1);
}

ret
}
Some(ref mut p) => p.schedule(callback),
}
}

Expand Down Expand Up @@ -236,6 +216,9 @@ struct ProcessDebug {
}

pub struct Process<'a> {
/// Pointer to the main Kernel struct.
kernel: &'static Kernel,

/// Application memory layout:
///
/// ```text
Expand Down Expand Up @@ -324,18 +307,31 @@ pub struct Process<'a> {
debug: ProcessDebug,
}

// Stores the current number of callbacks enqueued + processes in Running state
static mut HAVE_WORK: VolatileCell<usize> = VolatileCell::new(0);
impl Process<'a> {
pub fn schedule(&mut self, callback: FunctionCall) -> bool {
// If this app is in the `Fault` state then we shouldn't schedule
// any work for it.
if self.current_state() == State::Fault {
return false;
}

pub fn processes_blocked() -> bool {
unsafe { HAVE_WORK.get() == 0 }
}
self.kernel.increment_work();

impl Process<'a> {
pub fn schedule_ipc(&mut self, from: AppId, cb_type: IPCType) {
unsafe {
HAVE_WORK.set(HAVE_WORK.get() + 1);
let ret = self.tasks.enqueue(Task::FunctionCall(callback));

// Make a note that we lost this callback if the enqueue function
// fails.
if ret == false {
self.debug
.dropped_callback_count
.set(self.debug.dropped_callback_count.get() + 1);
}

ret
}

pub fn schedule_ipc(&mut self, from: AppId, cb_type: IPCType) {
self.kernel.increment_work();
let ret = self.tasks.enqueue(Task::IPC((from, cb_type)));

// Make a note that we lost this callback if the enqueue function
Expand All @@ -354,9 +350,7 @@ impl Process<'a> {
pub fn yield_state(&mut self) {
if self.state == State::Running {
self.state = State::Yielded;
unsafe {
HAVE_WORK.set(HAVE_WORK.get() - 1);
}
self.kernel.decrement_work();
}
}

Expand All @@ -372,11 +366,8 @@ impl Process<'a> {
FaultResponse::Restart => {
// Remove the tasks that were scheduled for the app from the
// amount of work queue.
if HAVE_WORK.get() < self.tasks.len() {
// This case should never happen.
HAVE_WORK.set(0);
} else {
HAVE_WORK.set(HAVE_WORK.get() - self.tasks.len());
for _ in 0..self.tasks.len() {
self.kernel.decrement_work();
}

// And remove those tasks
Expand Down Expand Up @@ -422,16 +413,14 @@ impl Process<'a> {
r3: self.app_break as usize,
}));

HAVE_WORK.set(HAVE_WORK.get() + 1);
self.kernel.increment_work();
}
}
}

pub fn dequeue_task(&mut self) -> Option<Task> {
self.tasks.dequeue().map(|cb| {
unsafe {
HAVE_WORK.set(HAVE_WORK.get() - 1);
}
self.kernel.decrement_work();
cb
})
}
Expand Down Expand Up @@ -592,6 +581,7 @@ impl Process<'a> {
}

pub unsafe fn create(
kernel: &'static Kernel,
app_flash_address: *const u8,
remaining_app_memory: *mut u8,
remaining_app_memory_size: usize,
Expand Down Expand Up @@ -694,6 +684,7 @@ impl Process<'a> {
let mut process: &mut Process =
&mut *(process_struct_memory_location as *mut Process<'static>);

process.kernel = kernel;
process.memory = app_memory;
process.header = tbf_header;
process.kernel_memory_break = kernel_memory_break;
Expand Down Expand Up @@ -752,7 +743,7 @@ impl Process<'a> {
r3: process.app_break as usize,
}));

HAVE_WORK.set(HAVE_WORK.get() + 1);
kernel.increment_work();

return (Some(process), app_flash_size, app_ram_size);
}
Expand Down Expand Up @@ -846,7 +837,7 @@ impl Process<'a> {

/// Context switch to the process.
pub unsafe fn push_function_call(&mut self, callback: FunctionCall) {
HAVE_WORK.set(HAVE_WORK.get() + 1);
self.kernel.increment_work();

self.state = State::Running;
// Fill in initial stack expected by SVC handler
Expand Down
Loading