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:
82
third_party/lang-items/Cargo.lock
generated
vendored
82
third_party/lang-items/Cargo.lock
generated
vendored
@@ -6,32 +6,53 @@ version = 3
|
||||
name = "lang_items"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_core",
|
||||
"libtock_console",
|
||||
"libtock_drivers",
|
||||
"libtock_leds",
|
||||
"libtock_low_level_debug",
|
||||
"libtock_platform",
|
||||
"libtock_runtime",
|
||||
"linked_list_allocator",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_codegen"
|
||||
name = "libtock_console"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_core"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_codegen",
|
||||
"libtock_platform",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_drivers"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_core",
|
||||
"libtock_console",
|
||||
"libtock_platform",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_leds"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_platform",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_low_level_debug"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_platform",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtock_platform"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "libtock_runtime"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libtock_platform",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -39,38 +60,3 @@ name = "linked_list_allocator"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e322f259d225fbae43a1b053b2dc6a5968a6bdf8b205f5de684dab485b95030e"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
|
||||
|
||||
16
third_party/lang-items/Cargo.toml
vendored
16
third_party/lang-items/Cargo.toml
vendored
@@ -8,10 +8,22 @@ authors = [
|
||||
license = "MIT/Apache-2.0"
|
||||
edition = "2018"
|
||||
|
||||
[target.'cfg(any(target_arch = "arm", target_arch = "riscv32"))'.dependencies.libtock_runtime]
|
||||
path = "../../third_party/libtock-rs/runtime"
|
||||
default-features = false
|
||||
features = ["no_auto_layout", "alloc_init"]
|
||||
|
||||
[dependencies]
|
||||
libtock_core = { path = "../../third_party/libtock-rs/core", default-features = false, features = ["alloc_init", "custom_panic_handler", "custom_alloc_error_handler"] }
|
||||
libtock_drivers = { path = "../libtock-drivers" }
|
||||
linked_list_allocator = { version = "0.10.2", default-features = false, features = ["const_mut_refs"] }
|
||||
libtock_platform = { path = "../../third_party/libtock-rs/platform" }
|
||||
libtock_low_level_debug = { path = "../../third_party/libtock-rs/apis/low_level_debug" }
|
||||
libtock_leds = { path = "../../third_party/libtock-rs/apis/leds" }
|
||||
libtock_console = { path = "../../third_party/libtock-rs/apis/console" }
|
||||
|
||||
[dependencies.linked_list_allocator]
|
||||
version = "0.10.4"
|
||||
default-features = false
|
||||
features = ["const_mut_refs"]
|
||||
|
||||
[features]
|
||||
debug_allocations = []
|
||||
|
||||
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