Initial commit

This commit is contained in:
Jean-Michel Picod
2020-01-28 15:09:10 +01:00
commit f91d2fd3db
90 changed files with 31123 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,9 @@
[dependencies]
linked_list_allocator = "0.6.4"
+
+[features]
+panic_console = []
[dev-dependencies]
corepack = { version = "0.4.0", default-features = false, features = ["alloc"] }
diff --git a/src/lang_items.rs b/src/lang_items.rs
index ab2c945..deef73b 100644
--- a/src/lang_items.rs
+++ b/src/lang_items.rs
@@ -18,10 +18,14 @@
//! `rustc_main`. That's covered by the `_start` function in the root of this
//! crate.
+#[cfg(feature = "panic_console")]
+use crate::console::Console;
use crate::led;
use crate::timer;
use crate::timer::Duration;
use core::alloc::Layout;
+#[cfg(feature = "panic_console")]
+use core::fmt::Write;
use core::panic::PanicInfo;
#[lang = "start"]
@@ -42,11 +46,7 @@
}
}
-#[panic_handler]
-fn panic_handler(_info: &PanicInfo) -> ! {
+fn flash_all_leds() -> ! {
- // Signal a panic using the LowLevelDebug capsule (if available).
- super::debug::low_level_status_code(1);
-
// Flash all LEDs (if available).
loop {
for led in led::all() {
@@ -60,6 +59,34 @@
}
}
+#[cfg(feature = "panic_console")]
+#[panic_handler]
+fn panic_handler(info: &PanicInfo) -> ! {
+ // Signal a panic using the LowLevelDebug capsule (if available).
+ super::debug::low_level_status_code(1);
+
+ let mut console = Console::new();
+ writeln!(console, "{}", info).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.
+ unsafe {
+ core::ptr::read_volatile(0 as *const usize);
+ }
+ // We still flash the LEDs in case for some reason the previous line didn't cause a crash in
+ // the kernel.
+ flash_all_leds();
+}
+
+#[cfg(not(feature = "panic_console"))]
+#[panic_handler]
+fn panic_handler(_info: &PanicInfo) -> ! {
+ // Signal a panic using the LowLevelDebug capsule (if available).
+ super::debug::low_level_status_code(1);
+
+ flash_all_leds();
+}
+
#[alloc_error_handler]
fn cycle_leds(_: Layout) -> ! {
loop {

View File

@@ -0,0 +1,72 @@
diff --git a/src/timer.rs b/src/timer.rs
index ae60b07..2e7d544 100644
--- a/src/timer.rs
+++ b/src/timer.rs
@@ -178,7 +178,7 @@ impl<'a> Timer<'a> {
}
}
-#[derive(Copy, Clone, Debug)]
+#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ClockFrequency {
hz: usize,
}
@@ -196,21 +196,53 @@ pub struct ClockValue {
}
impl ClockValue {
+ pub const fn new(num_ticks: isize, clock_hz: usize) -> ClockValue {
+ ClockValue {
+ num_ticks,
+ clock_frequency: ClockFrequency { hz: clock_hz },
+ }
+ }
+
pub fn num_ticks(&self) -> isize {
self.num_ticks
}
+ // Computes (value * factor) / divisor, even when value * factor >= isize::MAX.
+ fn scale_int(value: isize, factor: isize, divisor: isize) -> isize {
+ // As long as isize is not i64, this should be fine. If not, this is an alternative:
+ // factor * (value / divisor) + ((value % divisor) * factor) / divisor
+ ((value as i64 * factor as i64) / divisor as i64) as isize
+ }
+
pub fn ms(&self) -> isize {
- if self.num_ticks.abs() < isize::MAX / 1000 {
- (1000 * self.num_ticks) / self.clock_frequency.hz() as isize
- } else {
- 1000 * (self.num_ticks / self.clock_frequency.hz() as isize)
- }
+ ClockValue::scale_int(self.num_ticks, 1000, self.clock_frequency.hz() as isize)
}
pub fn ms_f64(&self) -> f64 {
1000.0 * (self.num_ticks as f64) / (self.clock_frequency.hz() as f64)
}
+
+ pub fn wrapping_add(self, duration: Duration<isize>) -> ClockValue {
+ // This is a precision preserving formula for scaling an isize.
+ let duration_ticks =
+ ClockValue::scale_int(duration.ms, self.clock_frequency.hz() as isize, 1000);
+ ClockValue {
+ num_ticks: self.num_ticks.wrapping_add(duration_ticks),
+ clock_frequency: self.clock_frequency,
+ }
+ }
+
+ pub fn wrapping_sub(self, other: ClockValue) -> Option<Duration<isize>> {
+ if self.clock_frequency == other.clock_frequency {
+ let clock_duration = ClockValue {
+ num_ticks: self.num_ticks - other.num_ticks,
+ clock_frequency: self.clock_frequency,
+ };
+ Some(Duration::from_ms(clock_duration.ms()))
+ } else {
+ None
+ }
+ }
}
pub struct Alarm {

View File

@@ -0,0 +1,13 @@
diff --git a/src/lib.rs b/src/lib.rs
index 1d4f26b..c1483e9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,7 +42,7 @@ pub mod syscalls;
#[cfg(not(any(target_arch = "arm", target_arch = "riscv32")))]
#[path = "syscalls_mock.rs"]
-mod syscalls;
+pub mod syscalls;
#[cfg(any(target_arch = "arm", target_arch = "riscv32"))]
#[global_allocator]

View File

@@ -0,0 +1,13 @@
diff --git a/src/entry_point.rs b/src/entry_point.rs
index a24958c..e907e33 100644
--- a/src/entry_point.rs
+++ b/src/entry_point.rs
@@ -348,7 +348,7 @@ pub unsafe extern "C" fn rust_start(app_start: usize, stacktop: usize, app_heap_
// Heap size is set using `elf2tab` with `--app-heap` option, which is
// currently at 1024. If you change the `elf2tab` heap size, make sure to
// make the corresponding change here.
- const HEAP_SIZE: usize = 1024;
+ const HEAP_SIZE: usize = 90000;
// we could have also bss_end for app_heap_start
let app_heap_start = app_heap_break;