Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Cargo.lock
layout.ld
platform
target
22 changes: 15 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ license = "MIT/Apache-2.0"
edition = "2018"

[features]
alloc = [ "linked_list_allocator" ]
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" }
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]
Expand All @@ -22,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]
Expand All @@ -52,4 +59,5 @@ lto = true
members = [
"async-support",
"codegen",
"core"
]
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ 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
test:
Expand Down
18 changes: 18 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "libtock-core"
version = "0.1.0"
authors = ["Tock Project Developers <tock-dev@googlegroups.com>"]
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 }

[[example]]
name = "custom_panic"
path = "examples/custom_handler.rs"
required-features = ["custom_panic_handler"]
12 changes: 12 additions & 0 deletions core/examples/custom_handler.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
40 changes: 40 additions & 0 deletions core/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use core::alloc::GlobalAlloc;
use core::alloc::Layout;
use core::ptr;
use core::ptr::NonNull;
use linked_list_allocator::Heap;

pub static mut HEAP: Heap = Heap::empty();

struct TockAllocator;

unsafe impl GlobalAlloc for TockAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
HEAP.allocate_first_fit(layout)
.ok()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
HEAP.deallocate(NonNull::new_unchecked(ptr), layout)
}
}

#[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();
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions core/src/debug/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
3 changes: 3 additions & 0 deletions core/src/debug/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn get_stack_pointer() -> usize {
panic!("No generic implementation.")
}
5 changes: 5 additions & 0 deletions core/src/debug/platform_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn get_stack_pointer() -> usize {
let stack_pointer;
unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };
stack_pointer
}
5 changes: 5 additions & 0 deletions core/src/debug/platform_riscv32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn get_stack_pointer() -> usize {
let stack_pointer;
unsafe { asm!("mv $0, sp" : "=r"(stack_pointer) : : : "volatile") };
stack_pointer
}
4 changes: 0 additions & 4 deletions src/entry_point/mod.rs → core/src/entry_point/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
46 changes: 6 additions & 40 deletions src/lang_items.rs → core/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
//! `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 core::panic::PanicInfo;
use futures::future;
use crate::syscalls;

#[lang = "start"]
extern "C" fn start<T>(main: fn() -> T, _argc: isize, _argv: *const *const u8) -> bool
Expand All @@ -45,52 +38,25 @@ impl Termination for () {
fn check_result(self) {}
}

impl Termination for TockResult<()> {
impl<S, T> Termination for Result<S, T> {
fn check_result(self) {
if self.is_err() {
unsafe { report_panic() };
}
}
}

#[cfg(not(feature = "custom_panic_handler"))]
#[panic_handler]
unsafe fn panic_handler(_info: &PanicInfo) -> ! {
unsafe fn panic_handler(_info: &core::panic::PanicInfo) -> ! {
report_panic()
}

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);

// 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();
}
}
17 changes: 17 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![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;

pub mod callback;
pub mod debug;
pub mod memop;
pub mod result;
pub mod shared_memory;
pub mod syscalls;
pub mod unwind_symbols;
File renamed without changes.
30 changes: 30 additions & 0 deletions core/src/result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[derive(Copy, Clone)]
pub struct SubscribeError {
pub driver_number: usize,
pub subscribe_number: usize,
pub return_code: isize,
}

#[derive(Copy, Clone)]
pub struct CommandError {
pub driver_number: usize,
pub command_number: usize,
pub arg1: usize,
pub arg2: usize,
pub return_code: isize,
}

#[derive(Copy, Clone)]
pub struct AllowError {
pub driver_number: usize,
pub allow_number: usize,
pub return_code: isize,
}

pub const SUCCESS: isize = 0;
pub const FAIL: isize = -1;
pub const EBUSY: isize = -2;
pub const EALREADY: isize = -3;
pub const EINVAL: isize = -6;
pub const ESIZE: isize = -7;
pub const ENOMEM: isize = -9;
File renamed without changes.
4 changes: 0 additions & 4 deletions src/syscalls/mod.rs → core/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 0 additions & 16 deletions examples-alloc/alloc_error.rs

This file was deleted.

31 changes: 31 additions & 0 deletions examples-features/alloc_error.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions examples-features/panic.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
File renamed without changes.
10 changes: 0 additions & 10 deletions examples/panic.rs

This file was deleted.

Loading