From 2efe8c48ef2e53da70d2478577c0285f67b1a273 Mon Sep 17 00:00:00 2001 From: Woyten Date: Fri, 13 Mar 2020 13:39:25 +0100 Subject: [PATCH 01/11] Do nothing but yielding in error handlers (saves roughly 1150 bytes) --- src/alloc.rs | 34 ++-------------------------------- src/lang_items.rs | 37 ++----------------------------------- 2 files changed, 4 insertions(+), 67 deletions(-) diff --git a/src/alloc.rs b/src/alloc.rs index 523cb646..bcc5d6e8 100644 --- a/src/alloc.rs +++ b/src/alloc.rs @@ -1,14 +1,8 @@ -use crate::drivers; -use crate::leds::LedsDriver; -use crate::result::TockResult; -use crate::timer::Duration; -use crate::timer::ParallelSleepDriver; +use crate::syscalls; use core::alloc::GlobalAlloc; use core::alloc::Layout; -use core::executor; use core::ptr; use core::ptr::NonNull; -use futures::future; use linked_list_allocator::Heap; pub static mut HEAP: Heap = Heap::empty(); @@ -32,31 +26,7 @@ static ALLOCATOR: TockAllocator = TockAllocator; #[alloc_error_handler] unsafe fn alloc_error_handler(_: Layout) -> ! { - executor::block_on(async { - let mut drivers = drivers::retrieve_drivers_unsafe(); - - let leds_driver = drivers.leds.init_driver(); - let mut timer_driver = drivers.timer.create_timer_driver(); - let timer_driver = timer_driver.activate(); - - if let (Ok(leds_driver), Ok(timer_driver)) = (leds_driver, timer_driver) { - let _ = cycle_all_leds(&leds_driver, &timer_driver).await; - } else { - future::pending::<()>().await - } - loop {} - }) -} - -async fn cycle_all_leds( - leds_driver: &LedsDriver<'_>, - timer_driver: &ParallelSleepDriver<'_>, -) -> TockResult<()> { loop { - for led in leds_driver.leds() { - led.on()?; - timer_driver.sleep(Duration::from_ms(100)).await?; - led.off()?; - } + syscalls::raw::yieldk(); } } diff --git a/src/lang_items.rs b/src/lang_items.rs index 91a20a1a..5a281863 100644 --- a/src/lang_items.rs +++ b/src/lang_items.rs @@ -18,14 +18,9 @@ //! `rustc_main`. That's covered by the `_start` function in the root of this //! crate. -use crate::drivers; -use crate::leds::LedsDriver; use crate::result::TockResult; -use crate::timer::Duration; -use crate::timer::ParallelSleepDriver; -use core::executor; +use crate::syscalls; use core::panic::PanicInfo; -use futures::future; #[lang = "start"] extern "C" fn start(main: fn() -> T, _argc: isize, _argv: *const *const u8) -> bool @@ -62,35 +57,7 @@ unsafe fn report_panic() -> ! { // Signal a panic using the LowLevelDebug capsule (if available). super::debug::low_level_status_code(1); - // Flash all LEDs (if available). - executor::block_on(async { - let mut drivers = drivers::retrieve_drivers_unsafe(); - - let leds_driver = drivers.leds.init_driver(); - let mut timer_driver = drivers.timer.create_timer_driver(); - let timer_driver = timer_driver.activate(); - - if let (Ok(leds_driver), Ok(timer_driver)) = (leds_driver, timer_driver) { - let _ = blink_all_leds(&leds_driver, &timer_driver).await; - } else { - future::pending::<()>().await - } - loop {} - }) -} - -async fn blink_all_leds( - leds_driver: &LedsDriver<'_>, - timer_driver: &ParallelSleepDriver<'_>, -) -> TockResult<()> { loop { - for led in leds_driver.leds() { - led.on()?; - } - timer_driver.sleep(Duration::from_ms(100)).await?; - for led in leds_driver.leds() { - led.off()?; - } - timer_driver.sleep(Duration::from_ms(100)).await?; + syscalls::raw::yieldk(); } } From a3d10cbf303533c2f4737cca3aa15895eeb23c00 Mon Sep 17 00:00:00 2001 From: Woyten Date: Fri, 13 Mar 2020 16:00:47 +0100 Subject: [PATCH 02/11] Split out libtock_core --- Cargo.toml | 2 ++ core/Cargo.toml | 9 +++++++++ {src => core/src}/callback.rs | 0 core/src/debug/mod.rs | 5 +++++ core/src/debug/platform.rs | 3 +++ core/src/debug/platform_arm.rs | 5 +++++ core/src/debug/platform_riscv32.rs | 5 +++++ {src => core/src}/entry_point/mod.rs | 4 ---- .../src/entry_point/start_item.rs | 0 {src => core/src}/entry_point/start_item_arm.rs | 0 .../src}/entry_point/start_item_riscv32.rs | 0 {src => core/src}/lang_items.rs | 2 +- core/src/lib.rs | 14 ++++++++++++++ {src => core/src}/memop.rs | 0 {src => core/src}/result.rs | 0 {src => core/src}/shared_memory.rs | 0 {src => core/src}/syscalls/mod.rs | 4 ---- .../src/syscalls/platform.rs | 0 {src => core/src}/syscalls/platform_arm.rs | 0 {src => core/src}/syscalls/platform_riscv32.rs | 0 {src => core/src}/unwind_symbols.rs | 0 src/debug/mod.rs | 10 ++-------- src/lib.rs | 11 +---------- 23 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 core/Cargo.toml rename {src => core/src}/callback.rs (100%) create mode 100644 core/src/debug/mod.rs create mode 100644 core/src/debug/platform.rs create mode 100644 core/src/debug/platform_arm.rs create mode 100644 core/src/debug/platform_riscv32.rs rename {src => core/src}/entry_point/mod.rs (98%) rename src/entry_point/start_item_mock.rs => core/src/entry_point/start_item.rs (100%) rename {src => core/src}/entry_point/start_item_arm.rs (100%) rename {src => core/src}/entry_point/start_item_riscv32.rs (100%) rename {src => core/src}/lang_items.rs (96%) create mode 100644 core/src/lib.rs rename {src => core/src}/memop.rs (100%) rename {src => core/src}/result.rs (100%) rename {src => core/src}/shared_memory.rs (100%) rename {src => core/src}/syscalls/mod.rs (97%) rename src/syscalls/platform_mock.rs => core/src/syscalls/platform.rs (100%) rename {src => core/src}/syscalls/platform_arm.rs (100%) rename {src => core/src}/syscalls/platform_riscv32.rs (100%) rename {src => core/src}/unwind_symbols.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 2fcb4bfa..50c20ef8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ alloc = [ "linked_list_allocator" ] [dependencies] core = { package = "async-support", path = "async-support" } +libtock-core = { path = "core" } libtock_codegen = { path = "codegen" } linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] } @@ -52,4 +53,5 @@ lto = true members = [ "async-support", "codegen", + "core" ] diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 00000000..6f300f46 --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "libtock-core" +version = "0.1.0" +authors = ["torfmaster ", "Woyten "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/callback.rs b/core/src/callback.rs similarity index 100% rename from src/callback.rs rename to core/src/callback.rs diff --git a/core/src/debug/mod.rs b/core/src/debug/mod.rs new file mode 100644 index 00000000..1f306446 --- /dev/null +++ b/core/src/debug/mod.rs @@ -0,0 +1,5 @@ +#[cfg_attr(target_arch = "arm", path = "platform_arm.rs")] +#[cfg_attr(target_arch = "riscv32", path = "platform_riscv32.rs")] +mod platform; + +pub use platform::*; diff --git a/core/src/debug/platform.rs b/core/src/debug/platform.rs new file mode 100644 index 00000000..6be01d81 --- /dev/null +++ b/core/src/debug/platform.rs @@ -0,0 +1,3 @@ +pub fn get_stack_pointer() -> usize { + panic!("Not implemented yet") +} diff --git a/core/src/debug/platform_arm.rs b/core/src/debug/platform_arm.rs new file mode 100644 index 00000000..46211b3b --- /dev/null +++ b/core/src/debug/platform_arm.rs @@ -0,0 +1,5 @@ +pub fn get_stack_pointer() -> usize { + let stack_pointer; + unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") }; + stack_pointer +} diff --git a/core/src/debug/platform_riscv32.rs b/core/src/debug/platform_riscv32.rs new file mode 100644 index 00000000..7f4a4e0b --- /dev/null +++ b/core/src/debug/platform_riscv32.rs @@ -0,0 +1,5 @@ +pub fn get_stack_pointer() -> usize { + let stack_pointer; + unsafe { asm!("mv $0, sp" : "=r"(stack_pointer) : : : "volatile") }; + stack_pointer +} diff --git a/src/entry_point/mod.rs b/core/src/entry_point/mod.rs similarity index 98% rename from src/entry_point/mod.rs rename to core/src/entry_point/mod.rs index 09044bdb..d63b1747 100644 --- a/src/entry_point/mod.rs +++ b/core/src/entry_point/mod.rs @@ -57,10 +57,6 @@ use core::ptr; #[cfg_attr(target_arch = "riscv32", path = "start_item_riscv32.rs")] #[cfg_attr(target_arch = "arm", path = "start_item_arm.rs")] -#[cfg_attr( - not(any(target_arch = "arm", target_arch = "riscv32")), - path = "start_item_mock.rs" -)] mod start_item; /// The header encoded at the beginning of .text by the linker script. It is diff --git a/src/entry_point/start_item_mock.rs b/core/src/entry_point/start_item.rs similarity index 100% rename from src/entry_point/start_item_mock.rs rename to core/src/entry_point/start_item.rs diff --git a/src/entry_point/start_item_arm.rs b/core/src/entry_point/start_item_arm.rs similarity index 100% rename from src/entry_point/start_item_arm.rs rename to core/src/entry_point/start_item_arm.rs diff --git a/src/entry_point/start_item_riscv32.rs b/core/src/entry_point/start_item_riscv32.rs similarity index 100% rename from src/entry_point/start_item_riscv32.rs rename to core/src/entry_point/start_item_riscv32.rs diff --git a/src/lang_items.rs b/core/src/lang_items.rs similarity index 96% rename from src/lang_items.rs rename to core/src/lang_items.rs index 5a281863..6bbb257d 100644 --- a/src/lang_items.rs +++ b/core/src/lang_items.rs @@ -55,7 +55,7 @@ unsafe fn panic_handler(_info: &PanicInfo) -> ! { unsafe fn report_panic() -> ! { // Signal a panic using the LowLevelDebug capsule (if available). - super::debug::low_level_status_code(1); + let _ = syscalls::command1_insecure(8, 1, 1); loop { syscalls::raw::yieldk(); diff --git a/core/src/lib.rs b/core/src/lib.rs new file mode 100644 index 00000000..c8ffa54a --- /dev/null +++ b/core/src/lib.rs @@ -0,0 +1,14 @@ +#![feature(asm, lang_items, naked_functions)] +#![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)] + +mod entry_point; +#[cfg(any(target_arch = "arm", target_arch = "riscv32"))] +mod lang_items; + +pub mod callback; +pub mod debug; +pub mod memop; +pub mod result; +pub mod shared_memory; +pub mod syscalls; +pub mod unwind_symbols; diff --git a/src/memop.rs b/core/src/memop.rs similarity index 100% rename from src/memop.rs rename to core/src/memop.rs diff --git a/src/result.rs b/core/src/result.rs similarity index 100% rename from src/result.rs rename to core/src/result.rs diff --git a/src/shared_memory.rs b/core/src/shared_memory.rs similarity index 100% rename from src/shared_memory.rs rename to core/src/shared_memory.rs diff --git a/src/syscalls/mod.rs b/core/src/syscalls/mod.rs similarity index 97% rename from src/syscalls/mod.rs rename to core/src/syscalls/mod.rs index c0289e5e..833c356a 100644 --- a/src/syscalls/mod.rs +++ b/core/src/syscalls/mod.rs @@ -1,9 +1,5 @@ #[cfg_attr(target_arch = "riscv32", path = "platform_riscv32.rs")] #[cfg_attr(target_arch = "arm", path = "platform_arm.rs")] -#[cfg_attr( - not(any(target_arch = "arm", target_arch = "riscv32")), - path = "platform_mock.rs" -)] mod platform; use crate::callback::CallbackSubscription; diff --git a/src/syscalls/platform_mock.rs b/core/src/syscalls/platform.rs similarity index 100% rename from src/syscalls/platform_mock.rs rename to core/src/syscalls/platform.rs diff --git a/src/syscalls/platform_arm.rs b/core/src/syscalls/platform_arm.rs similarity index 100% rename from src/syscalls/platform_arm.rs rename to core/src/syscalls/platform_arm.rs diff --git a/src/syscalls/platform_riscv32.rs b/core/src/syscalls/platform_riscv32.rs similarity index 100% rename from src/syscalls/platform_riscv32.rs rename to core/src/syscalls/platform_riscv32.rs diff --git a/src/unwind_symbols.rs b/core/src/unwind_symbols.rs similarity index 100% rename from src/unwind_symbols.rs rename to core/src/unwind_symbols.rs diff --git a/src/debug/mod.rs b/src/debug/mod.rs index 4596aaa1..3bae6832 100644 --- a/src/debug/mod.rs +++ b/src/debug/mod.rs @@ -3,6 +3,7 @@ mod low_level_debug; use crate::drivers; +use libtock_core::debug as core_debug; pub use low_level_debug::*; @@ -21,22 +22,15 @@ pub fn print_as_hex(value: usize) { let _ = console.write(buffer); } -#[cfg(target_arch = "arm")] pub fn print_stack_pointer() { - let stack_pointer; - unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") }; - let mut buffer = [b'\n'; 15]; buffer[0..4].clone_from_slice(b"SP: "); - write_as_hex(&mut buffer[4..15], stack_pointer); + write_as_hex(&mut buffer[4..15], core_debug::get_stack_pointer()); let drivers = unsafe { drivers::retrieve_drivers_unsafe() }; let mut console = drivers.console.create_console(); let _ = console.write(buffer); } -#[cfg(target_arch = "riscv32")] -pub fn print_stack_pointer() {} - #[inline(always)] /// Dumps address /// # Safety diff --git a/src/lib.rs b/src/lib.rs index 0744a53e..9ff7725b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,13 @@ -#![feature(asm, lang_items, naked_functions)] #![cfg_attr(feature = "alloc", feature(alloc_error_handler))] #![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)] #[cfg(feature = "alloc")] mod alloc; -mod entry_point; -#[cfg(any(target_arch = "arm", target_arch = "riscv32"))] -mod lang_items; pub mod adc; pub mod ble_composer; pub mod ble_parser; pub mod buttons; -pub mod callback; pub mod console; pub mod debug; pub mod drivers; @@ -20,16 +15,12 @@ pub mod electronics; pub mod futures; pub mod gpio; pub mod leds; -pub mod memop; -pub mod result; pub mod rng; pub mod sensors; -pub mod shared_memory; pub mod simple_ble; -pub mod syscalls; pub mod temperature; pub mod timer; -pub mod unwind_symbols; pub use drivers::retrieve_drivers; pub use libtock_codegen::main; +pub use libtock_core::*; From 190adffe02f537336938aa692e930615a93d4e45 Mon Sep 17 00:00:00 2001 From: Woyten Date: Fri, 13 Mar 2020 17:04:53 +0100 Subject: [PATCH 03/11] Move result to libtock-rs --- core/src/lang_items.rs | 3 +- core/src/result.rs | 68 ----------------------------------------- src/lib.rs | 1 + src/result.rs | 69 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 70 deletions(-) create mode 100644 src/result.rs diff --git a/core/src/lang_items.rs b/core/src/lang_items.rs index 6bbb257d..49324207 100644 --- a/core/src/lang_items.rs +++ b/core/src/lang_items.rs @@ -18,7 +18,6 @@ //! `rustc_main`. That's covered by the `_start` function in the root of this //! crate. -use crate::result::TockResult; use crate::syscalls; use core::panic::PanicInfo; @@ -40,7 +39,7 @@ impl Termination for () { fn check_result(self) {} } -impl Termination for TockResult<()> { +impl Termination for Result { fn check_result(self) { if self.is_err() { unsafe { report_panic() }; diff --git a/core/src/result.rs b/core/src/result.rs index 9dc4f161..95087383 100644 --- a/core/src/result.rs +++ b/core/src/result.rs @@ -1,23 +1,3 @@ -use core::fmt; - -pub type TockResult = Result; - -#[derive(Copy, Clone)] -pub enum TockError { - Subscribe(SubscribeError), - Command(CommandError), - Allow(AllowError), - Format, - Other(OtherError), -} - -#[cfg(not(any(target_arch = "arm", target_arch = "riscv32")))] -impl core::fmt::Debug for TockError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - writeln!(f, "impl Debug only for test builds") - } -} - #[derive(Copy, Clone)] pub struct SubscribeError { pub driver_number: usize, @@ -25,12 +5,6 @@ pub struct SubscribeError { pub return_code: isize, } -impl From for TockError { - fn from(subscribe_error: SubscribeError) -> Self { - TockError::Subscribe(subscribe_error) - } -} - #[derive(Copy, Clone)] pub struct CommandError { pub driver_number: usize, @@ -40,12 +14,6 @@ pub struct CommandError { pub return_code: isize, } -impl From for TockError { - fn from(command_error: CommandError) -> Self { - TockError::Command(command_error) - } -} - #[derive(Copy, Clone)] pub struct AllowError { pub driver_number: usize, @@ -53,42 +21,6 @@ pub struct AllowError { pub return_code: isize, } -impl From for TockError { - fn from(allow_error: AllowError) -> Self { - TockError::Allow(allow_error) - } -} - -impl From for TockError { - fn from(fmt::Error: fmt::Error) -> Self { - TockError::Format - } -} - -#[derive(Copy, Clone)] -pub enum OtherError { - ButtonsDriverInvalidState, - GpioDriverInvalidState, - TimerDriverDurationOutOfRange, - TimerDriverErroneousClockFrequency, - DriverAlreadyTaken, - OutOfRangeError, -} - -impl From for TockError { - fn from(other: OtherError) -> Self { - TockError::Other(other) - } -} - -pub struct OutOfRangeError; - -impl From for TockError { - fn from(_other: OutOfRangeError) -> Self { - TockError::Other(OtherError::OutOfRangeError) - } -} - pub const SUCCESS: isize = 0; pub const FAIL: isize = -1; pub const EBUSY: isize = -2; diff --git a/src/lib.rs b/src/lib.rs index 9ff7725b..2ff6ccac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ pub mod electronics; pub mod futures; pub mod gpio; pub mod leds; +pub mod result; pub mod rng; pub mod sensors; pub mod simple_ble; diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 00000000..74b4786a --- /dev/null +++ b/src/result.rs @@ -0,0 +1,69 @@ +use core::fmt; + +pub use libtock_core::result::*; + +pub type TockResult = Result; + +#[derive(Copy, Clone)] +pub enum TockError { + Subscribe(SubscribeError), + Command(CommandError), + Allow(AllowError), + Format, + Other(OtherError), +} + +#[cfg(not(any(target_arch = "arm", target_arch = "riscv32")))] +impl core::fmt::Debug for TockError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + writeln!(f, "impl Debug only for test builds") + } +} + +impl From for TockError { + fn from(subscribe_error: SubscribeError) -> Self { + TockError::Subscribe(subscribe_error) + } +} + +impl From for TockError { + fn from(command_error: CommandError) -> Self { + TockError::Command(command_error) + } +} + +impl From for TockError { + fn from(allow_error: AllowError) -> Self { + TockError::Allow(allow_error) + } +} + +impl From for TockError { + fn from(fmt::Error: fmt::Error) -> Self { + TockError::Format + } +} + +#[derive(Copy, Clone)] +pub enum OtherError { + ButtonsDriverInvalidState, + GpioDriverInvalidState, + TimerDriverDurationOutOfRange, + TimerDriverErroneousClockFrequency, + DriverAlreadyTaken, + OutOfRangeError, +} + +impl From for TockError { + fn from(other: OtherError) -> Self { + TockError::Other(other) + } +} + +pub struct OutOfRangeError; + +impl From for TockError { + fn from(_other: OutOfRangeError) -> Self { + TockError::Other(OtherError::OutOfRangeError) + } +} From 043ce90276da236e2789b8b202f6ff8f8a1bcf12 Mon Sep 17 00:00:00 2001 From: Woyten Date: Fri, 13 Mar 2020 17:15:45 +0100 Subject: [PATCH 04/11] Move allocator to libtock_core --- Cargo.toml | 3 +-- core/Cargo.toml | 4 ++++ {src => core/src}/alloc.rs | 0 core/src/lib.rs | 3 +++ src/lib.rs | 4 ---- 5 files changed, 8 insertions(+), 6 deletions(-) rename {src => core/src}/alloc.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 50c20ef8..51e49db3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,13 +6,12 @@ license = "MIT/Apache-2.0" edition = "2018" [features] -alloc = [ "linked_list_allocator" ] +alloc = ["libtock-core/alloc"] [dependencies] core = { package = "async-support", path = "async-support" } libtock-core = { path = "core" } libtock_codegen = { path = "codegen" } -linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] } [dev-dependencies] diff --git a/core/Cargo.toml b/core/Cargo.toml index 6f300f46..b1545521 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,4 +6,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +alloc = [ "linked_list_allocator" ] + [dependencies] +linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } \ No newline at end of file diff --git a/src/alloc.rs b/core/src/alloc.rs similarity index 100% rename from src/alloc.rs rename to core/src/alloc.rs diff --git a/core/src/lib.rs b/core/src/lib.rs index c8ffa54a..e465f4d3 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,6 +1,9 @@ #![feature(asm, lang_items, naked_functions)] #![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)] +#![cfg_attr(feature = "alloc", feature(alloc_error_handler))] +#[cfg(feature = "alloc")] +mod alloc; mod entry_point; #[cfg(any(target_arch = "arm", target_arch = "riscv32"))] mod lang_items; diff --git a/src/lib.rs b/src/lib.rs index 2ff6ccac..0aabb1ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,5 @@ -#![cfg_attr(feature = "alloc", feature(alloc_error_handler))] #![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)] -#[cfg(feature = "alloc")] -mod alloc; - pub mod adc; pub mod ble_composer; pub mod ble_parser; From b2680dd966d9ad79e9517cad030b650e73bebcb2 Mon Sep 17 00:00:00 2001 From: Woyten Date: Sat, 14 Mar 2020 23:41:25 +0100 Subject: [PATCH 05/11] Ignore platform again --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e8cecc04..da66475f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Cargo.lock layout.ld +platform target From 9d9edc9ea1240b11bf076665ca02fbd03ffc7f83 Mon Sep 17 00:00:00 2001 From: Woyten Date: Tue, 17 Mar 2020 22:58:07 +0100 Subject: [PATCH 06/11] Add custom_panic_handler and custom_alloc_error_handler feature --- Cargo.toml | 2 ++ core/Cargo.toml | 2 ++ core/src/alloc.rs | 10 +++++++++- core/src/lang_items.rs | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 51e49db3..549e72ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2018" [features] alloc = ["libtock-core/alloc"] +custom_panic_handler = ["libtock-core/custom_panic_handler"] +custom_alloc_error_handler = ["libtock-core/custom_alloc_error_handler"] [dependencies] core = { package = "async-support", path = "async-support" } diff --git a/core/Cargo.toml b/core/Cargo.toml index b1545521..004819cd 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -8,6 +8,8 @@ edition = "2018" [features] alloc = [ "linked_list_allocator" ] +custom_panic_handler = [] +custom_alloc_error_handler = [] [dependencies] linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } \ No newline at end of file diff --git a/core/src/alloc.rs b/core/src/alloc.rs index bcc5d6e8..05e96695 100644 --- a/core/src/alloc.rs +++ b/core/src/alloc.rs @@ -1,4 +1,3 @@ -use crate::syscalls; use core::alloc::GlobalAlloc; use core::alloc::Layout; use core::ptr; @@ -24,8 +23,17 @@ unsafe impl GlobalAlloc for TockAllocator { #[global_allocator] static ALLOCATOR: TockAllocator = TockAllocator; +#[cfg(not(feature = "custom_alloc_error_handler"))] #[alloc_error_handler] unsafe fn alloc_error_handler(_: Layout) -> ! { + use crate::syscalls; + + // Print 0x01 using the LowLevelDebug capsule (if available). + let _ = syscalls::command1_insecure(8, 2, 0x01); + + // Signal a panic using the LowLevelDebug capsule (if available). + let _ = syscalls::command1_insecure(8, 1, 0x01); + loop { syscalls::raw::yieldk(); } diff --git a/core/src/lang_items.rs b/core/src/lang_items.rs index 49324207..b5c2cd47 100644 --- a/core/src/lang_items.rs +++ b/core/src/lang_items.rs @@ -19,7 +19,6 @@ //! crate. use crate::syscalls; -use core::panic::PanicInfo; #[lang = "start"] extern "C" fn start(main: fn() -> T, _argc: isize, _argv: *const *const u8) -> bool @@ -47,8 +46,9 @@ impl Termination for Result { } } +#[cfg(not(feature = "custom_panic_handler"))] #[panic_handler] -unsafe fn panic_handler(_info: &PanicInfo) -> ! { +unsafe fn panic_handler(_info: &core::panic::PanicInfo) -> ! { report_panic() } From 3fa574a286bba8db72d88339888a6df1fd26f0d3 Mon Sep 17 00:00:00 2001 From: torfmaster Date: Wed, 18 Mar 2020 19:17:31 +0100 Subject: [PATCH 07/11] Address some smaller review comments --- core/Cargo.toml | 6 ++---- core/src/debug/platform.rs | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 004819cd..64fa96ea 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,15 +1,13 @@ [package] name = "libtock-core" version = "0.1.0" -authors = ["torfmaster ", "Woyten "] +authors = ["Tock Project Developers "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [features] alloc = [ "linked_list_allocator" ] custom_panic_handler = [] custom_alloc_error_handler = [] [dependencies] -linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } \ No newline at end of file +linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } diff --git a/core/src/debug/platform.rs b/core/src/debug/platform.rs index 6be01d81..5f1f9f4a 100644 --- a/core/src/debug/platform.rs +++ b/core/src/debug/platform.rs @@ -1,3 +1,3 @@ pub fn get_stack_pointer() -> usize { - panic!("Not implemented yet") + panic!("No generic implementation.") } From 8d93e276acfe660e8398d0631a07f78ade6e287f Mon Sep 17 00:00:00 2001 From: torfmaster Date: Wed, 18 Mar 2020 20:23:01 +0100 Subject: [PATCH 08/11] Add example for custom error handler --- Makefile | 1 + core/Cargo.toml | 5 +++++ core/examples/custom_handler.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 core/examples/custom_handler.rs diff --git a/Makefile b/Makefile index d9128138..34d223e4 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ examples: PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples --features=alloc PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples + cd core && cargo build --release --target=thumbv7em-none-eabi --examples --features=custom_panic_handler && cd .. .PHONY: test test: diff --git a/core/Cargo.toml b/core/Cargo.toml index 64fa96ea..f0477eb7 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -11,3 +11,8 @@ custom_alloc_error_handler = [] [dependencies] linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } + +[[example]] +name = "custom_panic" +path = "examples/custom_handler.rs" +required-features = ["custom_panic_handler"] \ No newline at end of file diff --git a/core/examples/custom_handler.rs b/core/examples/custom_handler.rs new file mode 100644 index 00000000..8cccca91 --- /dev/null +++ b/core/examples/custom_handler.rs @@ -0,0 +1,12 @@ +#![no_std] + +use libtock_core::result::CommandError; + +fn main() -> Result<(), CommandError> { + panic!("Bye world!"); +} + +#[panic_handler] +unsafe fn panic_handler(_info: &core::panic::PanicInfo) -> ! { + loop {} +} From 964bb728bf5ba6daa17aa03bf1ed7ea485032196 Mon Sep 17 00:00:00 2001 From: Woyten Date: Wed, 18 Mar 2020 20:25:58 +0100 Subject: [PATCH 09/11] Add compilation of examples with new features to test suite --- Cargo.toml | 15 ++++++--- Makefile | 4 ++- examples-alloc/alloc_error.rs | 16 ---------- examples-features/alloc_error.rs | 31 +++++++++++++++++++ .../ble_scanning.rs | 0 .../libtock_test.rs | 0 examples-features/panic.rs | 24 ++++++++++++++ .../simple_ble.rs | 0 examples/panic.rs | 10 ------ 9 files changed, 68 insertions(+), 32 deletions(-) delete mode 100644 examples-alloc/alloc_error.rs create mode 100644 examples-features/alloc_error.rs rename {examples-alloc => examples-features}/ble_scanning.rs (100%) rename {examples-alloc => examples-features}/libtock_test.rs (100%) create mode 100644 examples-features/panic.rs rename {examples-alloc => examples-features}/simple_ble.rs (100%) delete mode 100644 examples/panic.rs diff --git a/Cargo.toml b/Cargo.toml index 549e72ab..5641d3ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,22 +24,27 @@ serde = { version = "=1.0.84", default-features = false, features = ["derive"] } [[example]] name = "alloc_error" -path = "examples-alloc/alloc_error.rs" -required-features = ["alloc"] +path = "examples-features/alloc_error.rs" +required-features = ["alloc", "custom_alloc_error_handler"] [[example]] name = "ble_scanning" -path = "examples-alloc/ble_scanning.rs" +path = "examples-features/ble_scanning.rs" required-features = ["alloc"] [[example]] name = "libtock_test" -path = "examples-alloc/libtock_test.rs" +path = "examples-features/libtock_test.rs" required-features = ["alloc"] +[[example]] +name = "panic" +path = "examples-features/panic.rs" +required-features = ["custom_panic_handler"] + [[example]] name = "simple_ble" -path = "examples-alloc/simple_ble.rs" +path = "examples-features/simple_ble.rs" required-features = ["alloc"] [profile.dev] diff --git a/Makefile b/Makefile index 34d223e4..7d87c283 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,9 @@ setup: examples: PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples --features=alloc - PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples + PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example panic --features=custom_panic_handler,custom_alloc_error_handler + PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example alloc_error --features=alloc,custom_alloc_error_handler + PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples # Important: This is testing a platform without atomics support cd core && cargo build --release --target=thumbv7em-none-eabi --examples --features=custom_panic_handler && cd .. .PHONY: test diff --git a/examples-alloc/alloc_error.rs b/examples-alloc/alloc_error.rs deleted file mode 100644 index ec42b5ac..00000000 --- a/examples-alloc/alloc_error.rs +++ /dev/null @@ -1,16 +0,0 @@ -// Triggers the out-of-memory handler. Should make all LEDs cycle. - -#![no_std] - -extern crate alloc; - -use alloc::vec::Vec; -use libtock::result::TockResult; - -#[libtock::main] -fn main() -> TockResult<()> { - let mut vec = Vec::new(); - loop { - vec.push(0); - } -} diff --git a/examples-features/alloc_error.rs b/examples-features/alloc_error.rs new file mode 100644 index 00000000..40ae9394 --- /dev/null +++ b/examples-features/alloc_error.rs @@ -0,0 +1,31 @@ +// Triggers the out-of-memory handler. Should print an error message. + +#![no_std] +#![feature(alloc_error_handler)] + +extern crate alloc; + +use alloc::vec::Vec; +use core::alloc::Layout; +use core::fmt::Write; +use libtock::result::TockResult; +use libtock::syscalls; + +#[libtock::main] +fn main() -> TockResult<()> { + let mut vec = Vec::new(); + loop { + vec.push(0); + } +} + +#[alloc_error_handler] +unsafe fn alloc_error_handler(_: Layout) -> ! { + if let Ok(drivers) = libtock::retrieve_drivers() { + let mut console = drivers.console.create_console(); + let _ = writeln!(console, "alloc_error_handler called"); + } + loop { + syscalls::raw::yieldk(); + } +} diff --git a/examples-alloc/ble_scanning.rs b/examples-features/ble_scanning.rs similarity index 100% rename from examples-alloc/ble_scanning.rs rename to examples-features/ble_scanning.rs diff --git a/examples-alloc/libtock_test.rs b/examples-features/libtock_test.rs similarity index 100% rename from examples-alloc/libtock_test.rs rename to examples-features/libtock_test.rs diff --git a/examples-features/panic.rs b/examples-features/panic.rs new file mode 100644 index 00000000..5a9123af --- /dev/null +++ b/examples-features/panic.rs @@ -0,0 +1,24 @@ +// Triggers the panic handler. Should print an error message. + +#![no_std] + +use core::fmt::Write; +use core::panic::PanicInfo; +use libtock::result::TockResult; +use libtock::syscalls; + +#[libtock::main] +async fn main() -> TockResult<()> { + panic!("Bye world!"); +} + +#[panic_handler] +unsafe fn panic_handler(_info: &PanicInfo) -> ! { + if let Ok(drivers) = libtock::retrieve_drivers() { + let mut console = drivers.console.create_console(); + let _ = writeln!(console, "panic_handler called"); + } + loop { + syscalls::raw::yieldk(); + } +} diff --git a/examples-alloc/simple_ble.rs b/examples-features/simple_ble.rs similarity index 100% rename from examples-alloc/simple_ble.rs rename to examples-features/simple_ble.rs diff --git a/examples/panic.rs b/examples/panic.rs deleted file mode 100644 index 5abc01af..00000000 --- a/examples/panic.rs +++ /dev/null @@ -1,10 +0,0 @@ -// Triggers the panic handler. Should make all LEDs flash. - -#![no_std] - -use libtock::result::TockResult; - -#[libtock::main] -async fn main() -> TockResult<()> { - panic!("Bye world!"); -} From e44103159f1f6b4fb45d519f78f2afe1d238b94f Mon Sep 17 00:00:00 2001 From: torfmaster Date: Thu, 19 Mar 2020 09:11:37 +0100 Subject: [PATCH 10/11] Remove panic handler example from core crate --- Makefile | 1 - core/Cargo.toml | 5 ----- core/examples/custom_handler.rs | 12 ------------ 3 files changed, 18 deletions(-) delete mode 100644 core/examples/custom_handler.rs diff --git a/Makefile b/Makefile index 7d87c283..3e97e223 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,6 @@ examples: PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example panic --features=custom_panic_handler,custom_alloc_error_handler PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example alloc_error --features=alloc,custom_alloc_error_handler PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples # Important: This is testing a platform without atomics support - cd core && cargo build --release --target=thumbv7em-none-eabi --examples --features=custom_panic_handler && cd .. .PHONY: test test: diff --git a/core/Cargo.toml b/core/Cargo.toml index f0477eb7..64fa96ea 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -11,8 +11,3 @@ custom_alloc_error_handler = [] [dependencies] linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false } - -[[example]] -name = "custom_panic" -path = "examples/custom_handler.rs" -required-features = ["custom_panic_handler"] \ No newline at end of file diff --git a/core/examples/custom_handler.rs b/core/examples/custom_handler.rs deleted file mode 100644 index 8cccca91..00000000 --- a/core/examples/custom_handler.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![no_std] - -use libtock_core::result::CommandError; - -fn main() -> Result<(), CommandError> { - panic!("Bye world!"); -} - -#[panic_handler] -unsafe fn panic_handler(_info: &core::panic::PanicInfo) -> ! { - loop {} -} From 76f96bb4fe0b9278a48ba8c50872fd69ecfb4b9c Mon Sep 17 00:00:00 2001 From: torfmaster Date: Thu, 19 Mar 2020 09:17:19 +0100 Subject: [PATCH 11/11] add readme for libtock-core --- core/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 core/README.md diff --git a/core/README.md b/core/README.md new file mode 100644 index 00000000..ba9602a3 --- /dev/null +++ b/core/README.md @@ -0,0 +1,33 @@ +# libtock-core + +Core crate of `libtock-rs`. It contains the architecture specific code of `libtock-rs`. In particular: + + * the entry point + * `panic` and `alloc_error` handlers + * the syscalls + * the allocator (optional) + +It has three important feature flags + + * `alloc` - allow for heap. Enables a linked list allocator. + * `custom_panic_handler` - disable the default panic handler and allow definition of a custom one using `#[panic_handler]` + * `custom_alloc_error_handler` - disable the default alloc error handler and allow definition of a custom one using `#[alloc_error_handler]` + +## License + +Licensed under either of + +- Apache License, Version 2.0 + ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +- MIT license + ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. + +The contribution guidelines are identical to those of `libtock-rs` and can be found here: [contribution guidelines](../CONTRIBUTING.md)