Tock V2 port - rebased and updated (#620)
* Changes from #580 * fixes USB cancel panic * style fixes * Update src/env/tock/storage.rs Co-authored-by: Zach Halvorsen <zhalvorsen@google.com> --------- Co-authored-by: Zach Halvorsen <zhalvorsen@google.com>
This commit is contained in:
32
third_party/lang-items/src/allocator.rs
vendored
32
third_party/lang-items/src/allocator.rs
vendored
@@ -9,14 +9,17 @@ use core::sync::atomic;
|
||||
#[cfg(feature = "debug_allocations")]
|
||||
use core::sync::atomic::AtomicUsize;
|
||||
#[cfg(any(feature = "debug_allocations", feature = "panic_console"))]
|
||||
use libtock_drivers::console::Console;
|
||||
use libtock_console::Console;
|
||||
#[cfg(feature = "panic_console")]
|
||||
use libtock_platform::{ErrorCode, Syscalls};
|
||||
use libtock_runtime::TockSyscalls;
|
||||
use linked_list_allocator::Heap;
|
||||
|
||||
static mut HEAP: Heap = Heap::empty();
|
||||
|
||||
#[no_mangle]
|
||||
unsafe fn libtock_alloc_init(app_heap_start: usize, app_heap_size: usize) {
|
||||
HEAP.init(app_heap_start as *mut u8, app_heap_size);
|
||||
unsafe fn libtock_alloc_init(app_heap_bottom: *mut u8, app_heap_size: usize) {
|
||||
HEAP.init(app_heap_bottom, app_heap_size);
|
||||
}
|
||||
|
||||
// With the "debug_allocations" feature, we use `AtomicUsize` to store the
|
||||
@@ -54,7 +57,7 @@ unsafe impl GlobalAlloc for TockAllocator {
|
||||
self.count.fetch_add(1, atomic::Ordering::SeqCst);
|
||||
self.size.fetch_add(layout.size(), atomic::Ordering::SeqCst);
|
||||
writeln!(
|
||||
Console::new(),
|
||||
Console::<TockSyscalls>::writer(),
|
||||
"alloc[{}, {}] = {:?} ({} ptrs, {} bytes)",
|
||||
layout.size(),
|
||||
layout.align(),
|
||||
@@ -73,7 +76,7 @@ unsafe impl GlobalAlloc for TockAllocator {
|
||||
self.count.fetch_sub(1, atomic::Ordering::SeqCst);
|
||||
self.size.fetch_sub(layout.size(), atomic::Ordering::SeqCst);
|
||||
writeln!(
|
||||
Console::new(),
|
||||
Console::<TockSyscalls>::writer(),
|
||||
"dealloc[{}, {}] = {:?} ({} ptrs, {} bytes)",
|
||||
layout.size(),
|
||||
layout.align(),
|
||||
@@ -93,17 +96,20 @@ static ALLOCATOR: TockAllocator = TockAllocator::new();
|
||||
|
||||
#[alloc_error_handler]
|
||||
unsafe fn alloc_error_handler(_layout: Layout) -> ! {
|
||||
util::signal_oom();
|
||||
util::signal_panic();
|
||||
util::Util::<TockSyscalls>::signal_oom();
|
||||
util::Util::<TockSyscalls>::signal_panic();
|
||||
|
||||
#[cfg(feature = "panic_console")]
|
||||
{
|
||||
writeln!(Console::new(), "Couldn't allocate: {:?}", _layout).ok();
|
||||
// Force the kernel to report the panic cause, by reading an invalid address.
|
||||
// The memory protection unit should be setup by the Tock kernel to prevent apps from accessing
|
||||
// address zero.
|
||||
core::ptr::read_volatile(0 as *const usize);
|
||||
writeln!(
|
||||
Console::<TockSyscalls>::writer(),
|
||||
"Couldn't allocate: {:?}",
|
||||
_layout
|
||||
)
|
||||
.ok();
|
||||
TockSyscalls::exit_terminate(ErrorCode::Fail as u32);
|
||||
}
|
||||
|
||||
util::cycle_leds()
|
||||
#[cfg(not(feature = "panic_console"))]
|
||||
util::Util::<TockSyscalls>::cycle_leds()
|
||||
}
|
||||
|
||||
2
third_party/lang-items/src/lib.rs
vendored
2
third_party/lang-items/src/lib.rs
vendored
@@ -10,7 +10,7 @@ mod util;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[no_mangle]
|
||||
unsafe fn libtock_alloc_init(_app_heap_start: usize, _app_heap_size: usize) {
|
||||
unsafe fn libtock_alloc_init(_app_heap_bottom: *mut u8, _app_heap_size: usize) {
|
||||
// Stub so that the symbol is present.
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
29
third_party/lang-items/src/panic_handler.rs
vendored
29
third_party/lang-items/src/panic_handler.rs
vendored
@@ -1,26 +1,25 @@
|
||||
//! Custom panic handler for OpenSK
|
||||
|
||||
use crate::util;
|
||||
#[cfg(feature = "panic_console")]
|
||||
use core::fmt::Write;
|
||||
use core::panic::PanicInfo;
|
||||
#[cfg(feature = "panic_console")]
|
||||
use libtock_drivers::console::Console;
|
||||
use libtock_console::Console;
|
||||
#[allow(unused_imports)]
|
||||
use libtock_platform::{ErrorCode, Syscalls};
|
||||
use libtock_runtime::TockSyscalls;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic_handler(_info: &PanicInfo) -> ! {
|
||||
util::signal_panic();
|
||||
fn panic_handler(_info: &core::panic::PanicInfo) -> ! {
|
||||
util::Util::<TockSyscalls>::signal_panic();
|
||||
|
||||
#[cfg(feature = "panic_console")]
|
||||
{
|
||||
let mut console = Console::new();
|
||||
writeln!(console, "{}", _info).ok();
|
||||
console.flush();
|
||||
// Force the kernel to report the panic cause, by reading an invalid address.
|
||||
// The memory protection unit should be setup by the Tock kernel to prevent apps from accessing
|
||||
// address zero.
|
||||
unsafe {
|
||||
core::ptr::read_volatile(0 as *const usize);
|
||||
}
|
||||
let mut writer = Console::<TockSyscalls>::writer();
|
||||
writeln!(writer, "{}", _info).ok();
|
||||
// Exit with a non-zero exit code to indicate failure.
|
||||
TockSyscalls::exit_terminate(ErrorCode::Fail as u32);
|
||||
}
|
||||
|
||||
util::flash_all_leds();
|
||||
#[cfg(not(feature = "panic_console"))]
|
||||
util::Util::<TockSyscalls>::flash_all_leds();
|
||||
}
|
||||
|
||||
86
third_party/lang-items/src/util.rs
vendored
86
third_party/lang-items/src/util.rs
vendored
@@ -1,45 +1,55 @@
|
||||
use libtock_drivers::led;
|
||||
use libtock_drivers::timer::{self, Duration};
|
||||
use libtock_drivers::timer;
|
||||
use libtock_leds::Leds;
|
||||
use libtock_low_level_debug::{AlertCode, LowLevelDebug};
|
||||
use libtock_platform as platform;
|
||||
use libtock_platform::Syscalls;
|
||||
use platform::DefaultConfig;
|
||||
|
||||
// Signal a panic using the LowLevelDebug capsule (if available).
|
||||
pub fn signal_panic() {
|
||||
let _ = libtock_core::syscalls::command1_insecure(8, 1, 1);
|
||||
}
|
||||
pub struct Util<S: Syscalls, C: platform::subscribe::Config = DefaultConfig>(S, C);
|
||||
|
||||
// Signal an out-of-memory error using the LowLevelDebug capsule (if available).
|
||||
pub fn signal_oom() {
|
||||
let _ = libtock_core::syscalls::command1_insecure(8, 2, 1);
|
||||
}
|
||||
|
||||
pub fn flash_all_leds() -> ! {
|
||||
// Flash all LEDs (if available). All errors from syscalls are ignored: we are already inside a
|
||||
// panic handler so there is nothing much to do if simple drivers (timer, LEDs) don't work.
|
||||
loop {
|
||||
if let Ok(leds) = led::all() {
|
||||
for led in leds {
|
||||
let _ = led.on();
|
||||
}
|
||||
}
|
||||
let _ = timer::sleep(Duration::from_ms(100));
|
||||
if let Ok(leds) = led::all() {
|
||||
for led in leds {
|
||||
let _ = led.off();
|
||||
}
|
||||
}
|
||||
let _ = timer::sleep(Duration::from_ms(100));
|
||||
impl<S: Syscalls, C: platform::subscribe::Config> Util<S, C> {
|
||||
/// Signal a panic using the LowLevelDebug capsule (if available).
|
||||
pub fn signal_panic() {
|
||||
LowLevelDebug::<S>::print_alert_code(AlertCode::Panic);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cycle_leds() -> ! {
|
||||
// Cycle though all LEDs (if available). All errors from syscalls are ignored: we are already
|
||||
// inside an error handler so there is nothing much to do if simple drivers (timer, LEDs) don't
|
||||
// work.
|
||||
loop {
|
||||
if let Ok(leds) = led::all() {
|
||||
for led in leds {
|
||||
let _ = led.on();
|
||||
let _ = timer::sleep(Duration::from_ms(100));
|
||||
let _ = led.off();
|
||||
/// Signal an out-of-memory error using the LowLevelDebug capsule (if available).
|
||||
pub fn signal_oom() {
|
||||
LowLevelDebug::<S>::print_alert_code(AlertCode::WrongLocation);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn flash_all_leds() -> ! {
|
||||
// Flash all LEDs (if available). All errors from syscalls are ignored: we are already inside a
|
||||
// panic handler so there is nothing much to do if simple drivers (timer, LEDs) don't work.
|
||||
loop {
|
||||
if let Ok(led_count) = Leds::<S>::count() {
|
||||
for led in 0..led_count {
|
||||
let _ = Leds::<S>::on(led);
|
||||
}
|
||||
}
|
||||
let _ = timer::Alarm::<S, C>::sleep_for(timer::Milliseconds(100));
|
||||
if let Ok(led_count) = Leds::<S>::count() {
|
||||
for led in 0..led_count {
|
||||
let _ = Leds::<S>::off(led);
|
||||
}
|
||||
}
|
||||
let _ = timer::Alarm::<S, C>::sleep_for(timer::Milliseconds(100));
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn cycle_leds() -> ! {
|
||||
// Cycle though all LEDs (if available). All errors from syscalls are ignored: we are already
|
||||
// inside an error handler so there is nothing much to do if simple drivers (timer, LEDs) don't
|
||||
// work.
|
||||
loop {
|
||||
if let Ok(leds) = Leds::<S>::count() {
|
||||
for led in 0..leds {
|
||||
let _ = Leds::<S>::on(led);
|
||||
let _ = timer::Alarm::<S, C>::sleep_for(timer::Milliseconds(100));
|
||||
let _ = Leds::<S>::off(led);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user