From 48e4d135c24b795a29bc1e26d04cc2f65ddfaf83 Mon Sep 17 00:00:00 2001 From: Andre Richter Date: Sat, 28 Mar 2020 13:25:50 +0100 Subject: [PATCH] Refactor tutorial 08 --- 08_timestamps/Makefile | 7 +- 08_timestamps/README.md | 582 +----------------- 08_timestamps/kernel | Bin 89800 -> 89928 bytes 08_timestamps/kernel8.img | Bin 13152 -> 13192 bytes .../{arch/aarch64.rs => _arch/aarch64/cpu.rs} | 37 +- 08_timestamps/src/_arch/aarch64/cpu/smp.rs | 22 + .../src/{arch => _arch}/aarch64/time.rs | 32 +- 08_timestamps/src/arch.rs | 11 - 08_timestamps/src/arch/aarch64/sync.rs | 53 -- 08_timestamps/src/bsp.rs | 8 +- .../src/bsp/{driver.rs => device_driver.rs} | 2 +- .../src/bsp/{driver => device_driver}/bcm.rs | 4 +- .../bcm/bcm2xxx_gpio.rs | 63 +- .../bcm/bcm2xxx_pl011_uart.rs | 82 +-- 08_timestamps/src/bsp/raspberrypi.rs | 38 ++ 08_timestamps/src/bsp/raspberrypi/console.rs | 30 + 08_timestamps/src/bsp/raspberrypi/cpu.rs | 15 + 08_timestamps/src/bsp/raspberrypi/driver.rs | 49 ++ .../src/bsp/{rpi => raspberrypi}/link.ld | 0 08_timestamps/src/bsp/raspberrypi/memory.rs | 36 ++ 08_timestamps/src/bsp/rpi.rs | 74 --- 08_timestamps/src/bsp/rpi/memory_map.rs | 18 - 08_timestamps/src/console.rs | 54 ++ 08_timestamps/src/cpu.rs | 12 + 08_timestamps/src/cpu/smp.rs | 10 + 08_timestamps/src/driver.rs | 41 ++ 08_timestamps/src/interface.rs | 133 ---- 08_timestamps/src/main.rs | 145 ++++- 08_timestamps/src/memory.rs | 4 + 08_timestamps/src/panic_wait.rs | 10 +- 08_timestamps/src/print.rs | 34 +- 08_timestamps/src/runtime_init.rs | 8 + 08_timestamps/src/synchronization.rs | 91 +++ 08_timestamps/src/time.rs | 35 ++ 34 files changed, 739 insertions(+), 1001 deletions(-) rename 08_timestamps/src/{arch/aarch64.rs => _arch/aarch64/cpu.rs} (67%) create mode 100644 08_timestamps/src/_arch/aarch64/cpu/smp.rs rename 08_timestamps/src/{arch => _arch}/aarch64/time.rs (69%) delete mode 100644 08_timestamps/src/arch.rs delete mode 100644 08_timestamps/src/arch/aarch64/sync.rs rename 08_timestamps/src/bsp/{driver.rs => device_driver.rs} (93%) rename 08_timestamps/src/bsp/{driver => device_driver}/bcm.rs (70%) rename 08_timestamps/src/bsp/{driver => device_driver}/bcm/bcm2xxx_gpio.rs (69%) rename 08_timestamps/src/bsp/{driver => device_driver}/bcm/bcm2xxx_pl011_uart.rs (89%) create mode 100644 08_timestamps/src/bsp/raspberrypi.rs create mode 100644 08_timestamps/src/bsp/raspberrypi/console.rs create mode 100644 08_timestamps/src/bsp/raspberrypi/cpu.rs create mode 100644 08_timestamps/src/bsp/raspberrypi/driver.rs rename 08_timestamps/src/bsp/{rpi => raspberrypi}/link.ld (100%) create mode 100644 08_timestamps/src/bsp/raspberrypi/memory.rs delete mode 100644 08_timestamps/src/bsp/rpi.rs delete mode 100644 08_timestamps/src/bsp/rpi/memory_map.rs create mode 100644 08_timestamps/src/console.rs create mode 100644 08_timestamps/src/cpu.rs create mode 100644 08_timestamps/src/cpu/smp.rs create mode 100644 08_timestamps/src/driver.rs delete mode 100644 08_timestamps/src/interface.rs create mode 100644 08_timestamps/src/synchronization.rs create mode 100644 08_timestamps/src/time.rs diff --git a/08_timestamps/Makefile b/08_timestamps/Makefile index 2f56a01b..1cf7c716 100644 --- a/08_timestamps/Makefile +++ b/08_timestamps/Makefile @@ -19,7 +19,7 @@ ifeq ($(BSP),rpi3) QEMU_BINARY = qemu-system-aarch64 QEMU_MACHINE_TYPE = raspi3 QEMU_RELEASE_ARGS = -serial stdio -display none - LINKER_FILE = src/bsp/rpi/link.ld + LINKER_FILE = src/bsp/raspberrypi/link.ld RUSTC_MISC_ARGS = -C target-cpu=cortex-a53 else ifeq ($(BSP),rpi4) TARGET = aarch64-unknown-none-softfloat @@ -27,7 +27,7 @@ else ifeq ($(BSP),rpi4) # QEMU_BINARY = qemu-system-aarch64 # QEMU_MACHINE_TYPE = # QEMU_RELEASE_ARGS = -serial stdio -display none - LINKER_FILE = src/bsp/rpi/link.ld + LINKER_FILE = src/bsp/raspberrypi/link.ld RUSTC_MISC_ARGS = -C target-cpu=cortex-a72 endif @@ -68,8 +68,7 @@ $(OUTPUT): $(CARGO_OUTPUT) $(OBJCOPY_CMD) $< $(OUTPUT) doc: - cargo xdoc --target=$(TARGET) --features bsp_$(BSP) --document-private-items - xdg-open target/$(TARGET)/doc/kernel/index.html + cargo xdoc --target=$(TARGET) --features bsp_$(BSP) --document-private-items --open ifeq ($(QEMU_MACHINE_TYPE),) qemu: diff --git a/08_timestamps/README.md b/08_timestamps/README.md index a887f800..ddda78fe 100644 --- a/08_timestamps/README.md +++ b/08_timestamps/README.md @@ -2,14 +2,14 @@ ## tl;dr -We add abstractions for the architectural timer, implement it for `aarch64` and -use it to annotate prints with timestamps; A `warn!()` macro is added. +We add abstractions for the architectural timer, implement it for `aarch64` and use it to annotate +prints with timestamps; A `warn!()` macro is added. ## Test it Check it out via chainboot (added in previous tutorial): ```console -» make chainboot +$ make chainboot [...] Minipush 1.0 @@ -26,572 +26,16 @@ Minipush 1.0 [MP] ⏩ Pushing 12 KiB =========================================🦀 100% 0 KiB/s Time: 00:00:00 [ML] Loaded! Executing the payload now -[ 0.585762] Booting on: Raspberry Pi 3 -[ 0.586849] Architectural timer resolution: 52 ns -[ 0.589152] Drivers loaded: -[ 0.590498] 1. GPIO -[ 0.591758] 2. PL011Uart -[W 0.593235] Spin duration smaller than architecturally supported, skipping -[ 0.596623] Spinning for 1 second -[ 1.598232] Spinning for 1 second -[ 2.599104] Spinning for 1 second +[ 0.586140] Booting on: Raspberry Pi 3 +[ 0.587227] Architectural timer resolution: 52 ns +[ 0.589530] Drivers loaded: +[ 0.590876] 1. BCM GPIO +[ 0.592309] 2. BCM PL011 UART +[W 0.594005] Spin duration smaller than architecturally supported, skipping +[ 0.597392] Spinning for 1 second +[ 1.599001] Spinning for 1 second +[ 2.599872] Spinning for 1 second + ``` ## Diff to previous -```diff -Binary files 07_uart_chainloader/demo_payload_rpi3.img and 08_timestamps/demo_payload_rpi3.img differ -Binary files 07_uart_chainloader/demo_payload_rpi4.img and 08_timestamps/demo_payload_rpi4.img differ - -diff -uNr 07_uart_chainloader/Makefile 08_timestamps/Makefile ---- 07_uart_chainloader/Makefile -+++ 08_timestamps/Makefile -@@ -20,8 +20,7 @@ - QEMU_MACHINE_TYPE = raspi3 - QEMU_RELEASE_ARGS = -serial stdio -display none - LINKER_FILE = src/bsp/rpi/link.ld -- RUSTC_MISC_ARGS = -C target-cpu=cortex-a53 -C relocation-model=pic -- CHAINBOOT_DEMO_PAYLOAD = demo_payload_rpi3.img -+ RUSTC_MISC_ARGS = -C target-cpu=cortex-a53 - else ifeq ($(BSP),rpi4) - TARGET = aarch64-unknown-none-softfloat - OUTPUT = kernel8.img -@@ -29,8 +28,7 @@ - # QEMU_MACHINE_TYPE = - # QEMU_RELEASE_ARGS = -serial stdio -display none - LINKER_FILE = src/bsp/rpi/link.ld -- RUSTC_MISC_ARGS = -C target-cpu=cortex-a72 -C relocation-model=pic -- CHAINBOOT_DEMO_PAYLOAD = demo_payload_rpi4.img -+ RUSTC_MISC_ARGS = -C target-cpu=cortex-a72 - endif - - RUSTFLAGS = -C link-arg=-T$(LINKER_FILE) $(RUSTC_MISC_ARGS) -@@ -58,7 +56,7 @@ - DOCKER_EXEC_QEMU = $(QEMU_BINARY) -M $(QEMU_MACHINE_TYPE) - DOCKER_EXEC_MINIPUSH = ruby /utils/minipush.rb - --.PHONY: all doc qemu qemuasm chainboot clippy clean readelf objdump nm -+.PHONY: all doc qemu chainboot clippy clean readelf objdump nm - - all: clean $(OUTPUT) - -@@ -76,25 +74,17 @@ - ifeq ($(QEMU_MACHINE_TYPE),) - qemu: - @echo "This board is not yet supported for QEMU." -- --qemuasm: -- @echo "This board is not yet supported for QEMU." - else - qemu: all - @$(DOCKER_CMD) $(DOCKER_ARG_DIR_TUT) $(DOCKER_IMAGE) \ - $(DOCKER_EXEC_QEMU) $(QEMU_RELEASE_ARGS) \ - -kernel $(OUTPUT) -- --qemuasm: all -- @$(DOCKER_CMD) $(DOCKER_ARG_DIR_TUT) $(DOCKER_IMAGE) \ -- $(DOCKER_EXEC_QEMU) $(QEMU_RELEASE_ARGS) \ -- -kernel $(OUTPUT) -d in_asm - endif - --chainboot: -+chainboot: all - @$(DOCKER_CMD) $(DOCKER_ARG_DIR_TUT) $(DOCKER_ARG_DIR_UTILS) $(DOCKER_ARG_TTY) \ - $(DOCKER_IMAGE) $(DOCKER_EXEC_MINIPUSH) $(DEV_SERIAL) \ -- $(CHAINBOOT_DEMO_PAYLOAD) -+ $(OUTPUT) - - clippy: - RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" cargo xclippy --target=$(TARGET) --features bsp_$(BSP) - -diff -uNr 07_uart_chainloader/src/arch/aarch64/time.rs 08_timestamps/src/arch/aarch64/time.rs ---- 07_uart_chainloader/src/arch/aarch64/time.rs -+++ 08_timestamps/src/arch/aarch64/time.rs -@@ -0,0 +1,81 @@ -+// SPDX-License-Identifier: MIT OR Apache-2.0 -+// -+// Copyright (c) 2018-2020 Andre Richter -+ -+//! Timer primitives. -+ -+use crate::{interface, warn}; -+use core::time::Duration; -+use cortex_a::regs::*; -+ -+const NS_PER_S: u64 = 1_000_000_000; -+ -+//-------------------------------------------------------------------------------------------------- -+// Arch-public -+//-------------------------------------------------------------------------------------------------- -+ -+pub struct Timer; -+ -+//-------------------------------------------------------------------------------------------------- -+// OS interface implementations -+//-------------------------------------------------------------------------------------------------- -+ -+impl interface::time::Timer for Timer { -+ fn resolution(&self) -> Duration { -+ Duration::from_nanos(NS_PER_S / (CNTFRQ_EL0.get() as u64)) -+ } -+ -+ fn uptime(&self) -> Duration { -+ let frq: u64 = CNTFRQ_EL0.get() as u64; -+ let current_count: u64 = CNTPCT_EL0.get() * NS_PER_S; -+ -+ Duration::from_nanos(current_count / frq) -+ } -+ -+ fn spin_for(&self, duration: Duration) { -+ // Instantly return on zero. -+ if duration.as_nanos() == 0 { -+ return; -+ } -+ -+ // Calculate the register compare value. -+ let frq = CNTFRQ_EL0.get() as u64; -+ let x = match frq.checked_mul(duration.as_nanos() as u64) { -+ None => { -+ warn!("Spin duration too long, skipping"); -+ return; -+ } -+ Some(val) => val, -+ }; -+ let tval = x / NS_PER_S; -+ -+ // Check if it is within supported bounds. -+ let warn: Option<&str> = if tval == 0 { -+ Some("smaller") -+ } else if tval > u32::max_value().into() { -+ Some("bigger") -+ } else { -+ None -+ }; -+ -+ if let Some(w) = warn { -+ warn!( -+ "Spin duration {} than architecturally supported, skipping", -+ w -+ ); -+ return; -+ } -+ -+ // Set the compare value register. -+ CNTP_TVAL_EL0.set(tval as u32); -+ -+ // Kick off the counting. // Disable timer interrupt. -+ CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE::SET + CNTP_CTL_EL0::IMASK::SET); -+ -+ // ISTATUS will be '1' when cval ticks have passed. Busy-check it. -+ while !CNTP_CTL_EL0.matches_all(CNTP_CTL_EL0::ISTATUS::SET) {} -+ -+ // Disable counting again. -+ CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE::CLEAR); -+ } -+} - -diff -uNr 07_uart_chainloader/src/arch/aarch64.rs 08_timestamps/src/arch/aarch64.rs ---- 07_uart_chainloader/src/arch/aarch64.rs -+++ 08_timestamps/src/arch/aarch64.rs -@@ -5,8 +5,9 @@ - //! AArch64. - - pub mod sync; -+mod time; - --use crate::bsp; -+use crate::{bsp, interface}; - use cortex_a::{asm, regs::*}; - - /// The entry of the `kernel` binary. -@@ -22,7 +23,7 @@ - - if bsp::BOOT_CORE_ID == MPIDR_EL1.get() & CORE_MASK { - SP.set(bsp::BOOT_CORE_STACK_START); -- crate::relocate::relocate_self::() -+ crate::runtime_init::runtime_init() - } else { - // If not core0, infinitely wait for events. - wait_forever() -@@ -30,6 +31,12 @@ - } - - //-------------------------------------------------------------------------------------------------- -+// Global instances -+//-------------------------------------------------------------------------------------------------- -+ -+static TIMER: time::Timer = time::Timer; -+ -+//-------------------------------------------------------------------------------------------------- - // Implementation of the kernel's architecture abstraction code - //-------------------------------------------------------------------------------------------------- - -@@ -42,6 +49,11 @@ - } - } - -+/// Return a reference to a `interface::time::TimeKeeper` implementation. -+pub fn timer() -> &'static impl interface::time::Timer { -+ &TIMER -+} -+ - /// Pause execution on the calling CPU core. - #[inline(always)] - pub fn wait_forever() -> ! { - -diff -uNr 07_uart_chainloader/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs 08_timestamps/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs ---- 07_uart_chainloader/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs -+++ 08_timestamps/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs -@@ -293,11 +293,18 @@ - arch::nop(); - } - -+ // Read one character. -+ let mut ret = inner.DR.get() as u8 as char; -+ -+ // Convert carrige return to newline. -+ if ret == '\r' { -+ ret = '\n' -+ } -+ - // Update statistics. - inner.chars_read += 1; - -- // Read one character. -- inner.DR.get() as u8 as char -+ ret - }) - } - - -diff -uNr 07_uart_chainloader/src/bsp/rpi/link.ld 08_timestamps/src/bsp/rpi/link.ld ---- 07_uart_chainloader/src/bsp/rpi/link.ld -+++ 08_timestamps/src/bsp/rpi/link.ld -@@ -5,10 +5,9 @@ - - SECTIONS - { -- /* Set the link address to the top-most 40 KiB of DRAM (assuming 1GiB) */ -- . = 0x3F000000 - 0x10000; -+ /* Set current address to the value from which the RPi starts execution */ -+ . = 0x80000; - -- __binary_start = .; - .text : - { - *(.text._start) *(.text*) -@@ -33,14 +32,5 @@ - __bss_end = .; - } - -- .got : -- { -- *(.got*) -- } -- -- /* Fill up to 8 byte, b/c relocating the binary is done in u64 chunks */ -- . = ALIGN(8); -- __binary_end = .; -- - /DISCARD/ : { *(.comment*) } - } - -diff -uNr 07_uart_chainloader/src/bsp/rpi.rs 08_timestamps/src/bsp/rpi.rs ---- 07_uart_chainloader/src/bsp/rpi.rs -+++ 08_timestamps/src/bsp/rpi.rs -@@ -16,9 +16,6 @@ - /// The early boot core's stack address. - pub const BOOT_CORE_STACK_START: u64 = 0x80_000; - --/// The address on which the RPi3 firmware loads every binary by default. --pub const BOARD_DEFAULT_LOAD_ADDRESS: usize = 0x80_000; -- - //-------------------------------------------------------------------------------------------------- - // Global BSP driver instances - //-------------------------------------------------------------------------------------------------- - -diff -uNr 07_uart_chainloader/src/interface.rs 08_timestamps/src/interface.rs ---- 07_uart_chainloader/src/interface.rs -+++ 08_timestamps/src/interface.rs -@@ -112,3 +112,22 @@ - } - } - } -+ -+/// Timekeeping interfaces. -+pub mod time { -+ use core::time::Duration; -+ -+ /// Timer functions. -+ pub trait Timer { -+ /// The timer's resolution. -+ fn resolution(&self) -> Duration; -+ -+ /// The uptime since power-on of the device. -+ /// -+ /// This includes time consumed by firmware and bootloaders. -+ fn uptime(&self) -> Duration; -+ -+ /// Spin for a given duration. -+ fn spin_for(&self, duration: Duration); -+ } -+} - -diff -uNr 07_uart_chainloader/src/main.rs 08_timestamps/src/main.rs ---- 07_uart_chainloader/src/main.rs -+++ 08_timestamps/src/main.rs -@@ -29,11 +29,7 @@ - // the first function to run. - mod arch; - --// `_start()` then calls `relocate::relocate_self()`. --mod relocate; -- --// `relocate::relocate_self()` calls `runtime_init()`, which on completion, jumps to --// `kernel_init()`. -+// `_start()` then calls `runtime_init()`, which on completion, jumps to `kernel_init()`. - mod runtime_init; - - // Conditionally includes the selected `BSP` code. -@@ -67,51 +63,25 @@ - - /// The main function running after the early init. - fn kernel_main() -> ! { -- use interface::console::All; -- -- println!(" __ __ _ _ _ _ "); -- println!("| \\/ (_)_ _ (_) | ___ __ _ __| |"); -- println!("| |\\/| | | ' \\| | |__/ _ \\/ _` / _` |"); -- println!("|_| |_|_|_||_|_|____\\___/\\__,_\\__,_|"); -- println!(); -- println!("{:^37}", bsp::board_name()); -- println!(); -- println!("[ML] Requesting binary"); -- bsp::console().flush(); -- -- // Clear the RX FIFOs, if any, of spurious received characters before starting with the loader -- // protocol. -- bsp::console().clear(); -- -- // Notify `Minipush` to send the binary. -- for _ in 0..3 { -- bsp::console().write_char(3 as char); -- } -+ use core::time::Duration; -+ use interface::time::Timer; - -- // Read the binary's size. -- let mut size: u32 = u32::from(bsp::console().read_char() as u8); -- size |= u32::from(bsp::console().read_char() as u8) << 8; -- size |= u32::from(bsp::console().read_char() as u8) << 16; -- size |= u32::from(bsp::console().read_char() as u8) << 24; -- -- // Trust it's not too big. -- bsp::console().write_char('O'); -- bsp::console().write_char('K'); -- -- let kernel_addr: *mut u8 = bsp::BOARD_DEFAULT_LOAD_ADDRESS as *mut u8; -- unsafe { -- // Read the kernel byte by byte. -- for i in 0..size { -- *kernel_addr.offset(i as isize) = bsp::console().read_char() as u8; -- } -+ info!("Booting on: {}", bsp::board_name()); -+ info!( -+ "Architectural timer resolution: {} ns", -+ arch::timer().resolution().as_nanos() -+ ); -+ -+ info!("Drivers loaded:"); -+ for (i, driver) in bsp::device_drivers().iter().enumerate() { -+ info!(" {}. {}", i + 1, driver.compatible()); - } - -- println!("[ML] Loaded! Executing the payload now\n"); -- bsp::console().flush(); -- -- // Use black magic to get a function pointer. -- let kernel: extern "C" fn() -> ! = unsafe { core::mem::transmute(kernel_addr as *const ()) }; -+ // Test a failing timer case. -+ arch::timer().spin_for(Duration::from_nanos(1)); - -- // Jump to loaded kernel! -- kernel() -+ loop { -+ info!("Spinning for 1 second"); -+ arch::timer().spin_for(Duration::from_secs(1)); -+ } - } - -diff -uNr 07_uart_chainloader/src/print.rs 08_timestamps/src/print.rs ---- 07_uart_chainloader/src/print.rs -+++ 08_timestamps/src/print.rs -@@ -32,3 +32,71 @@ - $crate::print::_print(format_args_nl!($($arg)*)); - }) - } -+ -+/// Prints an info, with newline. -+#[macro_export] -+macro_rules! info { -+ ($string:expr) => ({ -+ #[allow(unused_imports)] -+ use crate::interface::time::Timer; -+ -+ let timestamp = $crate::arch::timer().uptime(); -+ let timestamp_subsec_us = timestamp.subsec_micros(); -+ -+ $crate::print::_print(format_args_nl!( -+ concat!("[ {:>3}.{:03}{:03}] ", $string), -+ timestamp.as_secs(), -+ timestamp_subsec_us / 1_000, -+ timestamp_subsec_us modulo 1_000 -+ )); -+ }); -+ ($format_string:expr, $($arg:tt)*) => ({ -+ #[allow(unused_imports)] -+ use crate::interface::time::Timer; -+ -+ let timestamp = $crate::arch::timer().uptime(); -+ let timestamp_subsec_us = timestamp.subsec_micros(); -+ -+ $crate::print::_print(format_args_nl!( -+ concat!("[ {:>3}.{:03}{:03}] ", $format_string), -+ timestamp.as_secs(), -+ timestamp_subsec_us / 1_000, -+ timestamp_subsec_us modulo 1_000, -+ $($arg)* -+ )); -+ }) -+} -+ -+/// Prints a warning, with newline. -+#[macro_export] -+macro_rules! warn { -+ ($string:expr) => ({ -+ #[allow(unused_imports)] -+ use crate::interface::time::Timer; -+ -+ let timestamp = $crate::arch::timer().uptime(); -+ let timestamp_subsec_us = timestamp.subsec_micros(); -+ -+ $crate::print::_print(format_args_nl!( -+ concat!("[W {:>3}.{:03}{:03}] ", $string), -+ timestamp.as_secs(), -+ timestamp_subsec_us / 1_000, -+ timestamp_subsec_us modulo 1_000 -+ )); -+ }); -+ ($format_string:expr, $($arg:tt)*) => ({ -+ #[allow(unused_imports)] -+ use crate::interface::time::Timer; -+ -+ let timestamp = $crate::arch::timer().uptime(); -+ let timestamp_subsec_us = timestamp.subsec_micros(); -+ -+ $crate::print::_print(format_args_nl!( -+ concat!("[W {:>3}.{:03}{:03}] ", $format_string), -+ timestamp.as_secs(), -+ timestamp_subsec_us / 1_000, -+ timestamp_subsec_us modulo 1_000, -+ $($arg)* -+ )); -+ }) -+} - -diff -uNr 07_uart_chainloader/src/relocate.rs 08_timestamps/src/relocate.rs ---- 07_uart_chainloader/src/relocate.rs -+++ 08_timestamps/src/relocate.rs -@@ -1,46 +0,0 @@ --// SPDX-License-Identifier: MIT OR Apache-2.0 --// --// Copyright (c) 2018-2020 Andre Richter -- --//! Relocation code. -- --/// Relocates the own binary from `bsp::BOARD_DEFAULT_LOAD_ADDRESS` to the `__binary_start` address --/// from the linker script. --/// --/// # Safety --/// --/// - Only a single core must be active and running this function. --/// - Function must not use the `bss` section. --pub unsafe fn relocate_self() -> ! { -- extern "C" { -- static __binary_start: usize; -- static __binary_end: usize; -- } -- -- let binary_start_addr: usize = &__binary_start as *const _ as _; -- let binary_end_addr: usize = &__binary_end as *const _ as _; -- let binary_size_in_byte: usize = binary_end_addr - binary_start_addr; -- -- // Get the relocation destination address from the linker symbol. -- let mut reloc_dst_addr: *mut T = binary_start_addr as *mut T; -- -- // The address of where the previous firmware loaded us. -- let mut src_addr: *const T = crate::bsp::BOARD_DEFAULT_LOAD_ADDRESS as *const _; -- -- // Copy the whole binary. -- // -- // This is essentially a `memcpy()` optimized for throughput by transferring in chunks of T. -- let n = binary_size_in_byte / core::mem::size_of::(); -- for _ in 0..n { -- use core::ptr; -- -- ptr::write_volatile::(reloc_dst_addr, ptr::read_volatile::(src_addr)); -- reloc_dst_addr = reloc_dst_addr.offset(1); -- src_addr = src_addr.offset(1); -- } -- -- // Call `init()` through a trait object, causing the jump to use an absolute address to reach -- // the relocated binary. An elaborate explanation can be found in the runtime_init.rs source -- // comments. -- crate::runtime_init::get().runtime_init() --} - -diff -uNr 07_uart_chainloader/src/runtime_init.rs 08_timestamps/src/runtime_init.rs ---- 07_uart_chainloader/src/runtime_init.rs -+++ 08_timestamps/src/runtime_init.rs -@@ -36,32 +36,14 @@ - memory::zero_volatile(bss_range()); - } - --/// We are outsmarting the compiler here by using a trait as a layer of indirection. Because we are --/// generating PIC code, a static dispatch to `init()` would generate a relative jump from the --/// callee to `init()`. However, when calling `init()`, code just finished copying the binary to the --/// actual link-time address, and hence is still running at whatever location the previous loader --/// has put it. So we do not want a relative jump, because it would not jump to the relocated code. -+/// Equivalent to `crt0` or `c0` code in C/C++ world. Clears the `bss` section, then jumps to kernel -+/// init code. - /// --/// By indirecting through a trait object, we can make use of the property that vtables store --/// absolute addresses. So calling `init()` this way will kick execution to the relocated binary. --pub trait RunTimeInit { -- /// Equivalent to `crt0` or `c0` code in C/C++ world. Clears the `bss` section, then jumps to -- /// kernel init code. -- /// -- /// # Safety -- /// -- /// - Only a single core must be active and running this function. -- unsafe fn runtime_init(&self) -> ! { -- zero_bss(); -- -- crate::kernel_init() -- } --} -- --struct Traitor; --impl RunTimeInit for Traitor {} -+/// # Safety -+/// -+/// - Only a single core must be active and running this function. -+pub unsafe fn runtime_init() -> ! { -+ zero_bss(); - --/// Give the callee a `RunTimeInit` trait object. --pub fn get() -> &'static dyn RunTimeInit { -- &Traitor {} -+ crate::kernel_init() - } - -``` diff --git a/08_timestamps/kernel b/08_timestamps/kernel index 15e3bcfa42819203f1bf9728be5c0ca2e3086a6d..79edab12c8ca01d543c75c03f245f2789abebf09 100755 GIT binary patch delta 7795 zcmbVQ4R934mF_pIMY}@i2P7mAMk@&rlFVxUc6S!AS%DRV_%R3>f^95TtC5kxSeAtC z*rry_`7SsnE+&z$F*pgtNx|ZCz%i7Jui`@?b%ie}TZzv<$CXnOsjJ!r#|V5$(3P?7 z^-MRg>z_(xs-&Lz-s|`OUeD~=U%Jn}>E1hEYT9ho29>QEu`jU_0`@ceKMDV~YR>ak z&ExRBYCSslPLPH=_NH85dDaE3TGHP# zlAV=6{lgVoN~{qVu~Jp81oF%q_KZ%qCgPAt){#W=3<|816943Jj6yqy30Y!U&wm8< z_1=1-7e`)bNx8hWcl93bd7$~(-#;+z#Fbp+^6V-;aKZh-mG62+u55ebnU+1UkS+q4 zw?fLbF1-X+^R<=lPD7KL4i%U6wgjz9XGo%8SAR<(A;+1^Q+B<4fSKpnl{k<(1J*0o zI^R{)N!H7|CLi!z>v=cz4m}D`21E4U0cPh6w9l}TRm7@@&`$tLZeQ&RAR%$){ zT!QIs=d;K_fB6jynr7dyU+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W{$e96Ex4%)oqnV5-v{0*)rk9paV-+kV=PC8-E6lPv*>?`@%dK99%+xp zR@zO>s#@oLEUN*WB@sC!Yr3w6g9FPpfgg&xd+@JA)wX<)(Ep|V;egPEz59qN< AegFUf diff --git a/08_timestamps/kernel8.img b/08_timestamps/kernel8.img index 5b19e5c9f6577cc652f2706837b6114d44e67b10..84a284eea656ed99596acfcaaa4b09e89d17697f 100755 GIT binary patch delta 2531 zcma)7e@t7~6+ZVpjNdcHKw@Hu88#+hY!8fq(2S(TfYt^lRZ>G*X^9qSmac)WMP^;M zWyrMCHqx>xch_xK$hNTjkz80rHaF_%qzHyIC90I9&C*PfIvJ`-??D!5vbOOQ%FeaT z=s&CWr1$QxbIL~~&ouv7L{#KxxTsRJ^s|`ca##f*s%BymNNx(G);J7Xm zsyx<6y)YO~7_&G*-(_7knvj9&!`KS>X8tgW&>zbAoX0z{IEkrws5>eU{L(D^Fbu)Me9& zft*ogC2~!_!p4c6%&@S#P(2!%Juz_++u*(v>Uc6iS5@t_zqP< zg9lM*vf>qp%sYp|yUf3h+#)9Ag70__TbaC^MKD0wf$KYgS z0bvgl_Nr8_2C^x7vSYk=BnKH|Hzm7DN@-{&m+Bqtb7|AT`7DSHI@W{lEL{(tiwm~rq0m{geFDo)e*lu2 z&+O^K@-ra|4OxefwfcdJ|UZFOxi-V3E;ve6kxArxuYTnm=pa<7FUs&c30*X zF{B4mJdd>gbxPqva=i_b9OqX~t6V#6;?E$(>HKwyLwWy$V#}9Pj0%qJU&8VKp_nil z(#g4uzk=In8b2 zC~z`>Y`au=K%B}syc!_kqb^+E2N3&j*&CtZM<{L)+Te-1Dsp}RMP9+|eu{b&edMJ{ z3_ODSj^g_nX1Bk!q>0AneWJ=!=S2vUBg7FU8(^XA68rU$PbXELoHTJP}EB!LUtkdZUk$=*voN=UJkJLjr}CZ9#%@p&)H$6(|{Jk0d_%gY{0#@ zkq}}%{?E9?LytAGCFOBptk#fr_PpvK7uaj+BgDl%R6F9|GTkSI=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-!Fmsfn7=iyxpl8!%G4A{8+tckr|QJ14+QqA_38qFYT6sO+~&sF?VV8c~A z$)jPtijs^Sdv-l#J>JdcY>jN(mP5|4Nn0EFBP*%7lU{2@Ou)|A>c~>qZT}5%2ZD!> zxdQzAXoKry$G48u2D=|RFsbn=PJ98dXh3aLO6}lk+=-xBg98 zgnWYqYK!FJisUZG7_BW*o--w_r;W*Wcgo(gd$Zg9 -//! AArch64. +//! Architectural processor code. -pub mod sync; -mod time; - -use crate::{bsp, interface}; +use crate::{bsp, cpu}; use cortex_a::{asm, regs::*}; +//-------------------------------------------------------------------------------------------------- +// Boot Code +//-------------------------------------------------------------------------------------------------- + /// The entry of the `kernel` binary. /// /// The function must be named `_start`, because the linker is looking for this exact name. @@ -17,13 +18,15 @@ use cortex_a::{asm, regs::*}; /// # Safety /// /// - Linker script must ensure to place this function at `0x80_000`. +#[naked] #[no_mangle] pub unsafe extern "C" fn _start() -> ! { - const CORE_MASK: u64 = 0x3; + use crate::runtime_init; - if bsp::BOOT_CORE_ID == MPIDR_EL1.get() & CORE_MASK { - SP.set(bsp::BOOT_CORE_STACK_START); - crate::runtime_init::runtime_init() + // Expect the boot core to start in EL2. + if bsp::cpu::BOOT_CORE_ID == cpu::smp::core_id() { + SP.set(bsp::cpu::BOOT_CORE_STACK_START); + runtime_init::runtime_init() } else { // If not core0, infinitely wait for events. wait_forever() @@ -31,30 +34,20 @@ pub unsafe extern "C" fn _start() -> ! { } //-------------------------------------------------------------------------------------------------- -// Global instances -//-------------------------------------------------------------------------------------------------- - -static TIMER: time::Timer = time::Timer; - -//-------------------------------------------------------------------------------------------------- -// Implementation of the kernel's architecture abstraction code +// Public Code //-------------------------------------------------------------------------------------------------- pub use asm::nop; /// Spin for `n` cycles. +#[inline(always)] pub fn spin_for_cycles(n: usize) { for _ in 0..n { asm::nop(); } } -/// Return a reference to a `interface::time::TimeKeeper` implementation. -pub fn timer() -> &'static impl interface::time::Timer { - &TIMER -} - -/// Pause execution on the calling CPU core. +/// Pause execution on the core. #[inline(always)] pub fn wait_forever() -> ! { loop { diff --git a/08_timestamps/src/_arch/aarch64/cpu/smp.rs b/08_timestamps/src/_arch/aarch64/cpu/smp.rs new file mode 100644 index 00000000..8429e1d2 --- /dev/null +++ b/08_timestamps/src/_arch/aarch64/cpu/smp.rs @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! Architectural symmetric multiprocessing. + +use cortex_a::regs::*; + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + +/// Return the executing core's id. +#[inline(always)] +pub fn core_id() -> T +where + T: From, +{ + const CORE_MASK: u64 = 0b11; + + T::from((MPIDR_EL1.get() & CORE_MASK) as u8) +} diff --git a/08_timestamps/src/arch/aarch64/time.rs b/08_timestamps/src/_arch/aarch64/time.rs similarity index 69% rename from 08_timestamps/src/arch/aarch64/time.rs rename to 08_timestamps/src/_arch/aarch64/time.rs index 249c498a..fb01ced1 100644 --- a/08_timestamps/src/arch/aarch64/time.rs +++ b/08_timestamps/src/_arch/aarch64/time.rs @@ -2,25 +2,45 @@ // // Copyright (c) 2018-2020 Andre Richter -//! Timer primitives. +//! Architectural timer primitives. -use crate::{interface, warn}; +use crate::{time, warn}; use core::time::Duration; use cortex_a::regs::*; +//-------------------------------------------------------------------------------------------------- +// Private Definitions +//-------------------------------------------------------------------------------------------------- + const NS_PER_S: u64 = 1_000_000_000; //-------------------------------------------------------------------------------------------------- -// Arch-public +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// ARMv8 Generic Timer. +pub struct GenericTimer; + +//-------------------------------------------------------------------------------------------------- +// Global instances //-------------------------------------------------------------------------------------------------- -pub struct Timer; +static TIME_MANAGER: GenericTimer = GenericTimer; //-------------------------------------------------------------------------------------------------- -// OS interface implementations +// Public Code //-------------------------------------------------------------------------------------------------- -impl interface::time::Timer for Timer { +/// Return a reference to the time manager. +pub fn time_manager() -> &'static impl time::interface::TimeManager { + &TIME_MANAGER +} + +//------------------------------------------------------------------------------ +// OS Interface Code +//------------------------------------------------------------------------------ + +impl time::interface::TimeManager for GenericTimer { fn resolution(&self) -> Duration { Duration::from_nanos(NS_PER_S / (CNTFRQ_EL0.get() as u64)) } diff --git a/08_timestamps/src/arch.rs b/08_timestamps/src/arch.rs deleted file mode 100644 index 005f848b..00000000 --- a/08_timestamps/src/arch.rs +++ /dev/null @@ -1,11 +0,0 @@ -// SPDX-License-Identifier: MIT OR Apache-2.0 -// -// Copyright (c) 2018-2020 Andre Richter - -//! Conditional exporting of processor architecture code. - -#[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))] -mod aarch64; - -#[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))] -pub use aarch64::*; diff --git a/08_timestamps/src/arch/aarch64/sync.rs b/08_timestamps/src/arch/aarch64/sync.rs deleted file mode 100644 index 1d1e459f..00000000 --- a/08_timestamps/src/arch/aarch64/sync.rs +++ /dev/null @@ -1,53 +0,0 @@ -// SPDX-License-Identifier: MIT OR Apache-2.0 -// -// Copyright (c) 2018-2020 Andre Richter - -//! Synchronization primitives. - -use crate::interface; -use core::cell::UnsafeCell; - -//-------------------------------------------------------------------------------------------------- -// Arch-public -//-------------------------------------------------------------------------------------------------- - -/// A pseudo-lock for teaching purposes. -/// -/// Used to introduce [interior mutability]. -/// -/// In contrast to a real Mutex implementation, does not protect against concurrent access to the -/// contained data. This part is preserved for later lessons. -/// -/// The lock will only be used as long as it is safe to do so, i.e. as long as the kernel is -/// executing single-threaded, aka only running on a single core with interrupts disabled. -/// -/// [interior mutability]: https://doc.rust-lang.org/std/cell/index.html -pub struct NullLock { - data: UnsafeCell, -} - -unsafe impl Send for NullLock {} -unsafe impl Sync for NullLock {} - -impl NullLock { - /// Wraps `data` into a new `NullLock`. - pub const fn new(data: T) -> NullLock { - NullLock { - data: UnsafeCell::new(data), - } - } -} - -//-------------------------------------------------------------------------------------------------- -// OS interface implementations -//-------------------------------------------------------------------------------------------------- - -impl interface::sync::Mutex for &NullLock { - type Data = T; - - fn lock(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R { - // In a real lock, there would be code encapsulating this line that ensures that this - // mutable reference will ever only be given out once at a time. - f(unsafe { &mut *self.data.get() }) - } -} diff --git a/08_timestamps/src/bsp.rs b/08_timestamps/src/bsp.rs index 4d7861bb..3a5657ad 100644 --- a/08_timestamps/src/bsp.rs +++ b/08_timestamps/src/bsp.rs @@ -2,12 +2,12 @@ // // Copyright (c) 2018-2020 Andre Richter -//! Conditional exporting of Board Support Packages. +//! Conditional re-exporting of Board Support Packages. -mod driver; +mod device_driver; #[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))] -mod rpi; +mod raspberrypi; #[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))] -pub use rpi::*; +pub use raspberrypi::*; diff --git a/08_timestamps/src/bsp/driver.rs b/08_timestamps/src/bsp/device_driver.rs similarity index 93% rename from 08_timestamps/src/bsp/driver.rs rename to 08_timestamps/src/bsp/device_driver.rs index f75093a5..4508e953 100644 --- a/08_timestamps/src/bsp/driver.rs +++ b/08_timestamps/src/bsp/device_driver.rs @@ -2,7 +2,7 @@ // // Copyright (c) 2018-2020 Andre Richter -//! Drivers. +//! Device driver. #[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))] mod bcm; diff --git a/08_timestamps/src/bsp/driver/bcm.rs b/08_timestamps/src/bsp/device_driver/bcm.rs similarity index 70% rename from 08_timestamps/src/bsp/driver/bcm.rs rename to 08_timestamps/src/bsp/device_driver/bcm.rs index 40232f30..59071d5d 100644 --- a/08_timestamps/src/bsp/driver/bcm.rs +++ b/08_timestamps/src/bsp/device_driver/bcm.rs @@ -7,5 +7,5 @@ mod bcm2xxx_gpio; mod bcm2xxx_pl011_uart; -pub use bcm2xxx_gpio::GPIO; -pub use bcm2xxx_pl011_uart::{PL011Uart, PanicUart}; +pub use bcm2xxx_gpio::*; +pub use bcm2xxx_pl011_uart::*; diff --git a/08_timestamps/src/bsp/driver/bcm/bcm2xxx_gpio.rs b/08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_gpio.rs similarity index 69% rename from 08_timestamps/src/bsp/driver/bcm/bcm2xxx_gpio.rs rename to 08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_gpio.rs index 1bcc9a64..0c17f498 100644 --- a/08_timestamps/src/bsp/driver/bcm/bcm2xxx_gpio.rs +++ b/08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_gpio.rs @@ -2,11 +2,15 @@ // // Copyright (c) 2018-2020 Andre Richter -//! GPIO driver. +//! GPIO Driver. -use crate::{arch, arch::sync::NullLock, interface}; +use crate::{cpu, driver, synchronization, synchronization::NullLock}; use core::ops; -use register::{mmio::ReadWrite, register_bitfields, register_structs}; +use register::{mmio::*, register_bitfields, register_structs}; + +//-------------------------------------------------------------------------------------------------- +// Private Definitions +//-------------------------------------------------------------------------------------------------- // GPIO registers. // @@ -66,12 +70,23 @@ register_structs! { } } -/// The driver's private data. struct GPIOInner { base_addr: usize, } -/// Deref to RegisterBlock. +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Representation of the GPIO HW. +pub struct GPIO { + inner: NullLock, +} + +//-------------------------------------------------------------------------------------------------- +// Private Code +//-------------------------------------------------------------------------------------------------- + impl ops::Deref for GPIOInner { type Target = RegisterBlock; @@ -81,29 +96,28 @@ impl ops::Deref for GPIOInner { } impl GPIOInner { - const fn new(base_addr: usize) -> GPIOInner { - GPIOInner { base_addr } + const fn new(base_addr: usize) -> Self { + Self { base_addr } } - /// Return a pointer to the register block. + /// Return a pointer to the associated MMIO register block. fn ptr(&self) -> *const RegisterBlock { self.base_addr as *const _ } } //-------------------------------------------------------------------------------------------------- -// BSP-public +// Public Code //-------------------------------------------------------------------------------------------------- -use interface::sync::Mutex; - -/// The driver's main struct. -pub struct GPIO { - inner: NullLock, -} impl GPIO { - pub const unsafe fn new(base_addr: usize) -> GPIO { - GPIO { + /// Create an instance. + /// + /// # Safety + /// + /// - The user must ensure to provide the correct `base_addr`. + pub const unsafe fn new(base_addr: usize) -> Self { + Self { inner: NullLock::new(GPIOInner::new(base_addr)), } } @@ -122,24 +136,25 @@ impl GPIO { // Enable pins 14 and 15. inner.GPPUD.set(0); - arch::spin_for_cycles(150); + cpu::spin_for_cycles(150); inner .GPPUDCLK0 .write(GPPUDCLK0::PUDCLK14::AssertClock + GPPUDCLK0::PUDCLK15::AssertClock); - arch::spin_for_cycles(150); + cpu::spin_for_cycles(150); inner.GPPUDCLK0.set(0); }) } } -//-------------------------------------------------------------------------------------------------- -// OS interface implementations -//-------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ +// OS Interface Code +//------------------------------------------------------------------------------ +use synchronization::interface::Mutex; -impl interface::driver::DeviceDriver for GPIO { +impl driver::interface::DeviceDriver for GPIO { fn compatible(&self) -> &str { - "GPIO" + "BCM GPIO" } } diff --git a/08_timestamps/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs b/08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_pl011_uart.rs similarity index 89% rename from 08_timestamps/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs rename to 08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_pl011_uart.rs index b9dd63b6..b15ba818 100644 --- a/08_timestamps/src/bsp/driver/bcm/bcm2xxx_pl011_uart.rs +++ b/08_timestamps/src/bsp/device_driver/bcm/bcm2xxx_pl011_uart.rs @@ -4,10 +4,14 @@ //! PL011 UART driver. -use crate::{arch, arch::sync::NullLock, interface}; +use crate::{console, cpu, driver, synchronization, synchronization::NullLock}; use core::{fmt, ops}; use register::{mmio::*, register_bitfields, register_structs}; +//-------------------------------------------------------------------------------------------------- +// Private Definitions +//-------------------------------------------------------------------------------------------------- + // PL011 UART registers. // // Descriptions taken from @@ -109,6 +113,10 @@ register_bitfields! { ] } +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + register_structs! { #[allow(non_snake_case)] pub RegisterBlock { @@ -126,13 +134,24 @@ register_structs! { } } -/// The driver's mutex protected part. pub struct PL011UartInner { base_addr: usize, chars_written: usize, chars_read: usize, } +// Export the inner struct so that BSPs can use it for the panic handler. +pub use PL011UartInner as PanicUart; + +/// Representation of the UART. +pub struct PL011Uart { + inner: NullLock, +} + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + /// Deref to RegisterBlock. /// /// Allows writing @@ -152,8 +171,13 @@ impl ops::Deref for PL011UartInner { } impl PL011UartInner { - pub const unsafe fn new(base_addr: usize) -> PL011UartInner { - PL011UartInner { + /// Create an instance. + /// + /// # Safety + /// + /// - The user must ensure to provide the correct `base_addr`. + pub const unsafe fn new(base_addr: usize) -> Self { + Self { base_addr, chars_written: 0, chars_read: 0, @@ -164,7 +188,7 @@ impl PL011UartInner { /// /// Results in 8N1 and 230400 baud (if the clk has been previously set to 48 MHz by the /// firmware). - pub fn init(&self) { + pub fn init(&mut self) { // Turn it off temporarily. self.CR.set(0); @@ -186,7 +210,7 @@ impl PL011UartInner { fn write_char(&mut self, c: char) { // Spin while TX FIFO full is set, waiting for an empty slot. while self.FR.matches_all(FR::TXFF::SET) { - arch::nop(); + cpu::nop(); } // Write the character to the buffer. @@ -215,42 +239,28 @@ impl fmt::Write for PL011UartInner { } } -//-------------------------------------------------------------------------------------------------- -// Export the inner struct so that BSPs can use it for the panic handler -//-------------------------------------------------------------------------------------------------- -pub use PL011UartInner as PanicUart; - -//-------------------------------------------------------------------------------------------------- -// BSP-public -//-------------------------------------------------------------------------------------------------- - -/// The driver's main struct. -pub struct PL011Uart { - inner: NullLock, -} - impl PL011Uart { /// # Safety /// - /// The user must ensure to provide the correct `base_addr`. - pub const unsafe fn new(base_addr: usize) -> PL011Uart { - PL011Uart { + /// - The user must ensure to provide the correct `base_addr`. + pub const unsafe fn new(base_addr: usize) -> Self { + Self { inner: NullLock::new(PL011UartInner::new(base_addr)), } } } -//-------------------------------------------------------------------------------------------------- -// OS interface implementations -//-------------------------------------------------------------------------------------------------- -use interface::sync::Mutex; +//------------------------------------------------------------------------------ +// OS Interface Code +//------------------------------------------------------------------------------ +use synchronization::interface::Mutex; -impl interface::driver::DeviceDriver for PL011Uart { +impl driver::interface::DeviceDriver for PL011Uart { fn compatible(&self) -> &str { - "PL011Uart" + "BCM PL011 UART" } - fn init(&self) -> interface::driver::Result { + fn init(&self) -> Result<(), ()> { let mut r = &self.inner; r.lock(|inner| inner.init()); @@ -258,7 +268,7 @@ impl interface::driver::DeviceDriver for PL011Uart { } } -impl interface::console::Write for PL011Uart { +impl console::interface::Write for PL011Uart { /// Passthrough of `args` to the `core::fmt::Write` implementation, but guarded by a Mutex to /// serialize access. fn write_char(&self, c: char) { @@ -274,23 +284,23 @@ impl interface::console::Write for PL011Uart { } fn flush(&self) { - let mut r = &self.inner; // Spin until TX FIFO empty is set. + let mut r = &self.inner; r.lock(|inner| { while !inner.FR.matches_all(FR::TXFE::SET) { - arch::nop(); + cpu::nop(); } }); } } -impl interface::console::Read for PL011Uart { +impl console::interface::Read for PL011Uart { fn read_char(&self) -> char { let mut r = &self.inner; r.lock(|inner| { // Spin while RX FIFO empty is set. while inner.FR.matches_all(FR::RXFE::SET) { - arch::nop(); + cpu::nop(); } // Read one character. @@ -319,7 +329,7 @@ impl interface::console::Read for PL011Uart { } } -impl interface::console::Statistics for PL011Uart { +impl console::interface::Statistics for PL011Uart { fn chars_written(&self) -> usize { let mut r = &self.inner; r.lock(|inner| inner.chars_written) diff --git a/08_timestamps/src/bsp/raspberrypi.rs b/08_timestamps/src/bsp/raspberrypi.rs new file mode 100644 index 00000000..c976cc29 --- /dev/null +++ b/08_timestamps/src/bsp/raspberrypi.rs @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! Top-level BSP file for the Raspberry Pi 3 and 4. + +pub mod console; +pub mod cpu; +pub mod driver; +pub mod memory; + +//-------------------------------------------------------------------------------------------------- +// Global instances +//-------------------------------------------------------------------------------------------------- +use super::device_driver; + +static GPIO: device_driver::GPIO = + unsafe { device_driver::GPIO::new(memory::map::mmio::GPIO_BASE) }; + +static PL011_UART: device_driver::PL011Uart = + unsafe { device_driver::PL011Uart::new(memory::map::mmio::PL011_UART_BASE) }; + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + +/// Board identification. +pub fn board_name() -> &'static str { + #[cfg(feature = "bsp_rpi3")] + { + "Raspberry Pi 3" + } + + #[cfg(feature = "bsp_rpi4")] + { + "Raspberry Pi 4" + } +} diff --git a/08_timestamps/src/bsp/raspberrypi/console.rs b/08_timestamps/src/bsp/raspberrypi/console.rs new file mode 100644 index 00000000..061f9c1c --- /dev/null +++ b/08_timestamps/src/bsp/raspberrypi/console.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! BSP console facilities. + +use super::{super::device_driver, memory::map}; +use crate::console; +use core::fmt; + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + +/// In case of a panic, the panic handler uses this function to take a last shot at printing +/// something before the system is halted. +/// +/// # Safety +/// +/// - Use only for printing during a panic. +pub unsafe fn panic_console_out() -> impl fmt::Write { + let mut uart = device_driver::PanicUart::new(map::mmio::PL011_UART_BASE); + uart.init(); + uart +} + +/// Return a reference to the console. +pub fn console() -> &'static impl console::interface::All { + &super::PL011_UART +} diff --git a/08_timestamps/src/bsp/raspberrypi/cpu.rs b/08_timestamps/src/bsp/raspberrypi/cpu.rs new file mode 100644 index 00000000..19db276e --- /dev/null +++ b/08_timestamps/src/bsp/raspberrypi/cpu.rs @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! BSP Processor code. + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Used by `arch` code to find the early boot core. +pub const BOOT_CORE_ID: usize = 0; + +/// The early boot core's stack address. +pub const BOOT_CORE_STACK_START: u64 = 0x80_000; diff --git a/08_timestamps/src/bsp/raspberrypi/driver.rs b/08_timestamps/src/bsp/raspberrypi/driver.rs new file mode 100644 index 00000000..86526dc0 --- /dev/null +++ b/08_timestamps/src/bsp/raspberrypi/driver.rs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! BSP driver support. + +use crate::driver; + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Device Driver Manager type. +pub struct BSPDriverManager { + device_drivers: [&'static (dyn DeviceDriver + Sync); 2], +} + +//-------------------------------------------------------------------------------------------------- +// Global instances +//-------------------------------------------------------------------------------------------------- + +static BSP_DRIVER_MANAGER: BSPDriverManager = BSPDriverManager { + device_drivers: [&super::GPIO, &super::PL011_UART], +}; + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + +/// Return a reference to the driver manager. +pub fn driver_manager() -> &'static impl driver::interface::DriverManager { + &BSP_DRIVER_MANAGER +} + +//------------------------------------------------------------------------------ +// OS Interface Code +//------------------------------------------------------------------------------ +use driver::interface::DeviceDriver; + +impl driver::interface::DriverManager for BSPDriverManager { + fn all_device_drivers(&self) -> &[&'static (dyn DeviceDriver + Sync)] { + &self.device_drivers[..] + } + + fn post_device_driver_init(&self) { + // Configure PL011Uart's output pins. + super::GPIO.map_pl011_uart(); + } +} diff --git a/08_timestamps/src/bsp/rpi/link.ld b/08_timestamps/src/bsp/raspberrypi/link.ld similarity index 100% rename from 08_timestamps/src/bsp/rpi/link.ld rename to 08_timestamps/src/bsp/raspberrypi/link.ld diff --git a/08_timestamps/src/bsp/raspberrypi/memory.rs b/08_timestamps/src/bsp/raspberrypi/memory.rs new file mode 100644 index 00000000..7aef077a --- /dev/null +++ b/08_timestamps/src/bsp/raspberrypi/memory.rs @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! BSP Memory Management. + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// The board's memory map. +#[rustfmt::skip] +pub(super) mod map { + pub const GPIO_OFFSET: usize = 0x0020_0000; + pub const UART_OFFSET: usize = 0x0020_1000; + + /// Physical devices. + #[cfg(feature = "bsp_rpi3")] + pub mod mmio { + use super::*; + + pub const BASE: usize = 0x3F00_0000; + pub const GPIO_BASE: usize = BASE + GPIO_OFFSET; + pub const PL011_UART_BASE: usize = BASE + UART_OFFSET; + } + + /// Physical devices. + #[cfg(feature = "bsp_rpi4")] + pub mod mmio { + use super::*; + + pub const BASE: usize = 0xFE00_0000; + pub const GPIO_BASE: usize = BASE + GPIO_OFFSET; + pub const PL011_UART_BASE: usize = BASE + UART_OFFSET; + } +} diff --git a/08_timestamps/src/bsp/rpi.rs b/08_timestamps/src/bsp/rpi.rs deleted file mode 100644 index 336db45a..00000000 --- a/08_timestamps/src/bsp/rpi.rs +++ /dev/null @@ -1,74 +0,0 @@ -// SPDX-License-Identifier: MIT OR Apache-2.0 -// -// Copyright (c) 2018-2020 Andre Richter - -//! Board Support Package for the Raspberry Pi. - -mod memory_map; - -use super::driver; -use crate::interface; -use core::fmt; - -/// Used by `arch` code to find the early boot core. -pub const BOOT_CORE_ID: u64 = 0; - -/// The early boot core's stack address. -pub const BOOT_CORE_STACK_START: u64 = 0x80_000; - -//-------------------------------------------------------------------------------------------------- -// Global BSP driver instances -//-------------------------------------------------------------------------------------------------- - -static GPIO: driver::GPIO = unsafe { driver::GPIO::new(memory_map::mmio::GPIO_BASE) }; -static PL011_UART: driver::PL011Uart = - unsafe { driver::PL011Uart::new(memory_map::mmio::PL011_UART_BASE) }; - -//-------------------------------------------------------------------------------------------------- -// Implementation of the kernel's BSP calls -//-------------------------------------------------------------------------------------------------- - -/// Board identification. -pub fn board_name() -> &'static str { - #[cfg(feature = "bsp_rpi3")] - { - "Raspberry Pi 3" - } - - #[cfg(feature = "bsp_rpi4")] - { - "Raspberry Pi 4" - } -} - -/// Return a reference to a `console::All` implementation. -pub fn console() -> &'static impl interface::console::All { - &PL011_UART -} - -/// In case of a panic, the panic handler uses this function to take a last shot at printing -/// something before the system is halted. -/// -/// # Safety -/// -/// - Use only for printing during a panic. -pub unsafe fn panic_console_out() -> impl fmt::Write { - let uart = driver::PanicUart::new(memory_map::mmio::PL011_UART_BASE); - uart.init(); - uart -} - -/// Return an array of references to all `DeviceDriver` compatible `BSP` drivers. -/// -/// # Safety -/// -/// The order of devices is the order in which `DeviceDriver::init()` is called. -pub fn device_drivers() -> [&'static dyn interface::driver::DeviceDriver; 2] { - [&GPIO, &PL011_UART] -} - -/// BSP initialization code that runs after driver init. -pub fn post_driver_init() { - // Configure PL011Uart's output pins. - GPIO.map_pl011_uart(); -} diff --git a/08_timestamps/src/bsp/rpi/memory_map.rs b/08_timestamps/src/bsp/rpi/memory_map.rs deleted file mode 100644 index 6e0d6d80..00000000 --- a/08_timestamps/src/bsp/rpi/memory_map.rs +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: MIT OR Apache-2.0 -// -// Copyright (c) 2018-2020 Andre Richter - -//! The board's memory map. - -/// Physical devices. -#[rustfmt::skip] -pub mod mmio { - #[cfg(feature = "bsp_rpi3")] - pub const BASE: usize = 0x3F00_0000; - - #[cfg(feature = "bsp_rpi4")] - pub const BASE: usize = 0xFE00_0000; - - pub const GPIO_BASE: usize = BASE + 0x0020_0000; - pub const PL011_UART_BASE: usize = BASE + 0x0020_1000; -} diff --git a/08_timestamps/src/console.rs b/08_timestamps/src/console.rs new file mode 100644 index 00000000..e6323a20 --- /dev/null +++ b/08_timestamps/src/console.rs @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! System console. + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Console interfaces. +pub mod interface { + use core::fmt; + + /// Console write functions. + pub trait Write { + /// Write a single character. + fn write_char(&self, c: char); + + /// Write a Rust format string. + fn write_fmt(&self, args: fmt::Arguments) -> fmt::Result; + + /// Block execution until the last character has been physically put on the TX wire + /// (draining TX buffers/FIFOs, if any). + fn flush(&self); + } + + /// Console read functions. + pub trait Read { + /// Read a single character. + fn read_char(&self) -> char { + ' ' + } + + /// Clear RX buffers, if any. + fn clear(&self); + } + + /// Console statistics. + pub trait Statistics { + /// Return the number of characters written. + fn chars_written(&self) -> usize { + 0 + } + + /// Return the number of characters read. + fn chars_read(&self) -> usize { + 0 + } + } + + /// Trait alias for a full-fledged console. + pub trait All = Write + Read + Statistics; +} diff --git a/08_timestamps/src/cpu.rs b/08_timestamps/src/cpu.rs new file mode 100644 index 00000000..9c67c0e7 --- /dev/null +++ b/08_timestamps/src/cpu.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2020 Andre Richter + +//! Processor code. + +#[cfg(target_arch = "aarch64")] +#[path = "_arch/aarch64/cpu.rs"] +mod arch_cpu; +pub use arch_cpu::*; + +pub mod smp; diff --git a/08_timestamps/src/cpu/smp.rs b/08_timestamps/src/cpu/smp.rs new file mode 100644 index 00000000..b1428884 --- /dev/null +++ b/08_timestamps/src/cpu/smp.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! Symmetric multiprocessing. + +#[cfg(target_arch = "aarch64")] +#[path = "../_arch/aarch64/cpu/smp.rs"] +mod arch_cpu_smp; +pub use arch_cpu_smp::*; diff --git a/08_timestamps/src/driver.rs b/08_timestamps/src/driver.rs new file mode 100644 index 00000000..c63b8301 --- /dev/null +++ b/08_timestamps/src/driver.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2018-2020 Andre Richter + +//! Driver support. + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Driver interfaces. +pub mod interface { + + /// Device Driver functions. + pub trait DeviceDriver { + /// Return a compatibility string for identifying the driver. + fn compatible(&self) -> &str; + + /// Called by the kernel to bring up the device. + fn init(&self) -> Result<(), ()> { + Ok(()) + } + } + + /// Device driver management functions. + /// + /// The `BSP` is supposed to supply one global instance. + pub trait DriverManager { + /// Return a slice of references to all `BSP`-instantiated drivers. + /// + /// # Safety + /// + /// - The order of devices is the order in which `DeviceDriver::init()` is called. + fn all_device_drivers(&self) -> &[&'static (dyn DeviceDriver + Sync)]; + + /// Initialization code that runs after driver init. + /// + /// For example, device driver code that depends on other drivers already being online. + fn post_device_driver_init(&self); + } +} diff --git a/08_timestamps/src/interface.rs b/08_timestamps/src/interface.rs deleted file mode 100644 index d08f2077..00000000 --- a/08_timestamps/src/interface.rs +++ /dev/null @@ -1,133 +0,0 @@ -// SPDX-License-Identifier: MIT OR Apache-2.0 -// -// Copyright (c) 2018-2020 Andre Richter - -//! Trait definitions for coupling `kernel` and `BSP` code. -//! -//! ``` -//! +-------------------+ -//! | Interface (Trait) | -//! | | -//! +--+-------------+--+ -//! ^ ^ -//! | | -//! | | -//! +----------+--+ +--+----------+ -//! | Kernel code | | BSP Code | -//! | | | | -//! +-------------+ +-------------+ -//! ``` - -/// System console operations. -pub mod console { - use core::fmt; - - /// Console write functions. - pub trait Write { - /// Write a single character. - fn write_char(&self, c: char); - - /// Write a Rust format string. - fn write_fmt(&self, args: fmt::Arguments) -> fmt::Result; - - /// Block execution until the last character has been physically put on the TX wire - /// (draining TX buffers/FIFOs, if any). - fn flush(&self); - } - - /// Console read functions. - pub trait Read { - /// Read a single character. - fn read_char(&self) -> char { - ' ' - } - - /// Clear RX buffers, if any. - fn clear(&self); - } - - /// Console statistics. - pub trait Statistics { - /// Return the number of characters written. - fn chars_written(&self) -> usize { - 0 - } - - /// Return the number of characters read. - fn chars_read(&self) -> usize { - 0 - } - } - - /// Trait alias for a full-fledged console. - pub trait All = Write + Read + Statistics; -} - -/// Synchronization primitives. -pub mod sync { - /// Any object implementing this trait guarantees exclusive access to the data contained within - /// the mutex for the duration of the lock. - /// - /// The trait follows the [Rust embedded WG's - /// proposal](https://github.com/korken89/wg/blob/master/rfcs/0377-mutex-trait.md) and therefore - /// provides some goodness such as [deadlock - /// prevention](https://github.com/korken89/wg/blob/master/rfcs/0377-mutex-trait.md#design-decisions-and-compatibility). - /// - /// # Example - /// - /// Since the lock function takes an `&mut self` to enable deadlock-prevention, the trait is - /// best implemented **for a reference to a container struct**, and has a usage pattern that - /// might feel strange at first: - /// - /// ``` - /// static MUT: Mutex> = Mutex::new(RefCell::new(0)); - /// - /// fn foo() { - /// let mut r = &MUT; // Note that r is mutable - /// r.lock(|data| *data += 1); - /// } - /// ``` - pub trait Mutex { - /// Type of data encapsulated by the mutex. - type Data; - - /// Creates a critical section and grants temporary mutable access to the encapsulated data. - fn lock(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R; - } -} - -/// Driver interfaces. -pub mod driver { - /// Driver result type, e.g. for indicating successful driver init. - pub type Result = core::result::Result<(), ()>; - - /// Device Driver functions. - pub trait DeviceDriver { - /// Return a compatibility string for identifying the driver. - fn compatible(&self) -> &str; - - /// Called by the kernel to bring up the device. - fn init(&self) -> Result { - Ok(()) - } - } -} - -/// Timekeeping interfaces. -pub mod time { - use core::time::Duration; - - /// Timer functions. - pub trait Timer { - /// The timer's resolution. - fn resolution(&self) -> Duration; - - /// The uptime since power-on of the device. - /// - /// This includes time consumed by firmware and bootloaders. - fn uptime(&self) -> Duration; - - /// Spin for a given duration. - fn spin_for(&self, duration: Duration); - } -} diff --git a/08_timestamps/src/main.rs b/08_timestamps/src/main.rs index 9309329d..0a2705e2 100644 --- a/08_timestamps/src/main.rs +++ b/08_timestamps/src/main.rs @@ -5,56 +5,139 @@ // Rust embedded logo for `make doc`. #![doc(html_logo_url = "https://git.io/JeGIp")] -//! The `kernel` +//! The `kernel` binary. //! -//! The `kernel` is composed by glueing together code from +//! # TL;DR - Overview of important Kernel entities //! -//! - [Hardware-specific Board Support Packages] (`BSPs`). -//! - [Architecture-specific code]. -//! - HW- and architecture-agnostic `kernel` code. +//! - [`bsp::console::console()`] - Returns a reference to the kernel's [console interface]. +//! - [`bsp::driver::driver_manager()`] - Returns a reference to the kernel's [driver interface]. +//! - [`time::time_manager()`] - Returns a reference to the kernel's [timer interface]. //! -//! using the [`kernel::interface`] traits. +//! [console interface]: ../libkernel/console/interface/index.html +//! [driver interface]: ../libkernel/driver/interface/trait.DriverManager.html +//! [timer interface]: ../libkernel/time/interface/trait.TimeManager.html //! -//! [Hardware-specific Board Support Packages]: bsp/index.html -//! [Architecture-specific code]: arch/index.html -//! [`kernel::interface`]: interface/index.html +//! # Code organization and architecture +//! +//! The code is divided into different *modules*, each representing a typical **subsystem** of the +//! `kernel`. Top-level module files of subsystems reside directly in the `src` folder. For example, +//! `src/memory.rs` contains code that is concerned with all things memory management. +//! +//! ## Visibility of processor architecture code +//! +//! Some of the `kernel`'s subsystems depend on low-level code that is specific to the target +//! processor architecture. For each supported processor architecture, there exists a subfolder in +//! `src/_arch`, for example, `src/_arch/aarch64`. +//! +//! The architecture folders mirror the subsystem modules laid out in `src`. For example, +//! architectural code that belongs to the `kernel`'s memory subsystem (`src/memory.rs`) would go +//! into `src/_arch/aarch64/memory.rs`. The latter file is directly included and re-exported in +//! `src/memory.rs`, so that the architectural code parts are transparent with respect to the code's +//! module organization. That means a public function `foo()` defined in +//! `src/_arch/aarch64/memory.rs` would be reachable as `crate::memory::foo()` only. +//! +//! The `_` in `_arch` denotes that this folder is not part of the standard module hierarchy. +//! Rather, it's contents are conditionally pulled into respective files using the `#[path = +//! "_arch/xxx/yyy.rs"]` attribute. +//! +//! ## BSP code +//! +//! `BSP` stands for Board Support Package. `BSP` code is organized under `src/bsp.rs` and contains +//! target board specific definitions and functions. These are things such as the board's memory map +//! or instances of drivers for devices that are featured on the respective board. +//! +//! Just like processor architecture code, the `BSP` code's module structure tries to mirror the +//! `kernel`'s subsystem modules, but there is no transparent re-exporting this time. That means +//! whatever is provided must be called starting from the `bsp` namespace, e.g. +//! `bsp::driver::driver_manager()`. +//! +//! ## Kernel interfaces +//! +//! Both `arch` and `bsp` contain code that is conditionally compiled depending on the actual target +//! and board for which the kernel is compiled. For example, the `interrupt controller` hardware of +//! the `Raspberry Pi 3` and the `Raspberry Pi 4` is different, but we want the rest of the `kernel` +//! code to play nicely with any of the two without much hassle. +//! +//! In order to provide a clean abstraction between `arch`, `bsp` and `generic kernel code`, +//! `interface` traits are provided *whenever possible* and *where it makes sense*. They are defined +//! in the respective subsystem module and help to enforce the idiom of *program to an interface, +//! not an implementation*. For example, there will be a common IRQ handling interface which the two +//! different interrupt controller `drivers` of both Raspberrys will implement, and only export the +//! interface to the rest of the `kernel`. +//! +//! ``` +//! +-------------------+ +//! | Interface (Trait) | +//! | | +//! +--+-------------+--+ +//! ^ ^ +//! | | +//! | | +//! +----------+--+ +--+----------+ +//! | kernel code | | bsp code | +//! | | | arch code | +//! +-------------+ +-------------+ +//! ``` +//! +//! # Summary +//! +//! For a logical `kernel` subsystem, corresponding code can be distributed over several physical +//! locations. Here is an example for the **memory** subsystem: +//! +//! - `src/memory.rs` and `src/memory/**/*` +//! - Common code that is agnostic of target processor architecture and `BSP` characteristics. +//! - Example: A function to zero a chunk of memory. +//! - Interfaces for the memory subsystem that are implemented by `arch` or `BSP` code. +//! - Example: An `MMU` interface that defines `MMU` function prototypes. +//! - `src/bsp/__board_name__/memory.rs` and `src/bsp/__board_name__/memory/**/*` +//! - `BSP` specific code. +//! - Example: The board's memory map (physical addresses of DRAM and MMIO devices). +//! - `src/_arch/__arch_name__/memory.rs` and `src/_arch/__arch_name__/memory/**/*` +//! - Processor architecture specific code. +//! - Example: Implementation of the `MMU` interface for the `__arch_name__` processor +//! architecture. +//! +//! From a namespace perspective, **memory** subsystem code lives in: +//! +//! - `crate::memory::*` +//! - `crate::bsp::memory::*` #![feature(format_args_nl)] +#![feature(naked_functions)] #![feature(panic_info_message)] #![feature(trait_alias)] #![no_main] #![no_std] -// Conditionally includes the selected `architecture` code, which provides the `_start()` function, -// the first function to run. -mod arch; - -// `_start()` then calls `runtime_init()`, which on completion, jumps to `kernel_init()`. -mod runtime_init; +// `mod cpu` provides the `_start()` function, the first function to run. `_start()` then calls +// `runtime_init()`, which jumps to `kernel_init()`. -// Conditionally includes the selected `BSP` code. mod bsp; - -mod interface; +mod console; +mod cpu; +mod driver; mod memory; mod panic_wait; mod print; +mod runtime_init; +mod synchronization; +mod time; /// Early init code. /// -/// Concerned with with initializing `BSP` and `arch` parts. -/// /// # Safety /// /// - Only a single core must be active and running this function. /// - The init calls in this function must appear in the correct order. unsafe fn kernel_init() -> ! { - for i in bsp::device_drivers().iter() { - if let Err(()) = i.init() { + use driver::interface::DriverManager; + + for i in bsp::driver::driver_manager().all_device_drivers().iter() { + if i.init().is_err() { panic!("Error loading driver: {}", i.compatible()) } } - bsp::post_driver_init(); + bsp::driver::driver_manager().post_device_driver_init(); // println! is usable from here on. // Transition from unsafe to safe. @@ -64,24 +147,30 @@ unsafe fn kernel_init() -> ! { /// The main function running after the early init. fn kernel_main() -> ! { use core::time::Duration; - use interface::time::Timer; + use driver::interface::DriverManager; + use time::interface::TimeManager; info!("Booting on: {}", bsp::board_name()); + info!( "Architectural timer resolution: {} ns", - arch::timer().resolution().as_nanos() + time::time_manager().resolution().as_nanos() ); info!("Drivers loaded:"); - for (i, driver) in bsp::device_drivers().iter().enumerate() { + for (i, driver) in bsp::driver::driver_manager() + .all_device_drivers() + .iter() + .enumerate() + { info!(" {}. {}", i + 1, driver.compatible()); } // Test a failing timer case. - arch::timer().spin_for(Duration::from_nanos(1)); + time::time_manager().spin_for(Duration::from_nanos(1)); loop { info!("Spinning for 1 second"); - arch::timer().spin_for(Duration::from_secs(1)); + time::time_manager().spin_for(Duration::from_secs(1)); } } diff --git a/08_timestamps/src/memory.rs b/08_timestamps/src/memory.rs index f551a2cc..71dc0292 100644 --- a/08_timestamps/src/memory.rs +++ b/08_timestamps/src/memory.rs @@ -6,6 +6,10 @@ use core::ops::Range; +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + /// Zero out a memory region. /// /// # Safety diff --git a/08_timestamps/src/panic_wait.rs b/08_timestamps/src/panic_wait.rs index 67cf1ee6..1386e1e2 100644 --- a/08_timestamps/src/panic_wait.rs +++ b/08_timestamps/src/panic_wait.rs @@ -4,13 +4,17 @@ //! A panic handler that infinitely waits. -use crate::{arch, bsp}; +use crate::{bsp, cpu}; use core::{fmt, panic::PanicInfo}; +//-------------------------------------------------------------------------------------------------- +// Private Code +//-------------------------------------------------------------------------------------------------- + fn _panic_print(args: fmt::Arguments) { use fmt::Write; - unsafe { bsp::panic_console_out().write_fmt(args).unwrap() }; + unsafe { bsp::console::panic_console_out().write_fmt(args).unwrap() }; } /// Prints with a newline - only use from the panic handler. @@ -31,5 +35,5 @@ fn panic(info: &PanicInfo) -> ! { panic_println!("\nKernel panic!"); } - arch::wait_forever() + cpu::wait_forever() } diff --git a/08_timestamps/src/print.rs b/08_timestamps/src/print.rs index 1d0736af..cc303bfc 100644 --- a/08_timestamps/src/print.rs +++ b/08_timestamps/src/print.rs @@ -4,16 +4,24 @@ //! Printing facilities. -use crate::{bsp, interface}; +use crate::{bsp, console}; use core::fmt; +//-------------------------------------------------------------------------------------------------- +// Private Code +//-------------------------------------------------------------------------------------------------- + #[doc(hidden)] pub fn _print(args: fmt::Arguments) { - use interface::console::Write; + use console::interface::Write; - bsp::console().write_fmt(args).unwrap(); + bsp::console::console().write_fmt(args).unwrap(); } +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + /// Prints without a newline. /// /// Carbon copy from https://doc.rust-lang.org/src/std/macros.rs.html @@ -33,14 +41,14 @@ macro_rules! println { }) } -/// Prints an info, with newline. +/// Prints an info, with a newline. #[macro_export] macro_rules! info { ($string:expr) => ({ #[allow(unused_imports)] - use crate::interface::time::Timer; + use crate::time::interface::TimeManager; - let timestamp = $crate::arch::timer().uptime(); + let timestamp = $crate::time::time_manager().uptime(); let timestamp_subsec_us = timestamp.subsec_micros(); $crate::print::_print(format_args_nl!( @@ -52,9 +60,9 @@ macro_rules! info { }); ($format_string:expr, $($arg:tt)*) => ({ #[allow(unused_imports)] - use crate::interface::time::Timer; + use crate::time::interface::TimeManager; - let timestamp = $crate::arch::timer().uptime(); + let timestamp = $crate::time::time_manager().uptime(); let timestamp_subsec_us = timestamp.subsec_micros(); $crate::print::_print(format_args_nl!( @@ -67,14 +75,14 @@ macro_rules! info { }) } -/// Prints a warning, with newline. +/// Prints a warning, with a newline. #[macro_export] macro_rules! warn { ($string:expr) => ({ #[allow(unused_imports)] - use crate::interface::time::Timer; + use crate::time::interface::TimeManager; - let timestamp = $crate::arch::timer().uptime(); + let timestamp = $crate::time::time_manager().uptime(); let timestamp_subsec_us = timestamp.subsec_micros(); $crate::print::_print(format_args_nl!( @@ -86,9 +94,9 @@ macro_rules! warn { }); ($format_string:expr, $($arg:tt)*) => ({ #[allow(unused_imports)] - use crate::interface::time::Timer; + use crate::time::interface::TimeManager; - let timestamp = $crate::arch::timer().uptime(); + let timestamp = $crate::time::time_manager().uptime(); let timestamp_subsec_us = timestamp.subsec_micros(); $crate::print::_print(format_args_nl!( diff --git a/08_timestamps/src/runtime_init.rs b/08_timestamps/src/runtime_init.rs index 8132a20b..d517bd64 100644 --- a/08_timestamps/src/runtime_init.rs +++ b/08_timestamps/src/runtime_init.rs @@ -7,6 +7,10 @@ use crate::memory; use core::ops::Range; +//-------------------------------------------------------------------------------------------------- +// Private Code +//-------------------------------------------------------------------------------------------------- + /// Return the range spanning the .bss section. /// /// # Safety @@ -36,6 +40,10 @@ unsafe fn zero_bss() { memory::zero_volatile(bss_range()); } +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + /// Equivalent to `crt0` or `c0` code in C/C++ world. Clears the `bss` section, then jumps to kernel /// init code. /// diff --git a/08_timestamps/src/synchronization.rs b/08_timestamps/src/synchronization.rs new file mode 100644 index 00000000..caa2794a --- /dev/null +++ b/08_timestamps/src/synchronization.rs @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2020 Andre Richter + +//! Synchronization primitives. + +use core::cell::UnsafeCell; + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Synchronization interfaces. +pub mod interface { + + /// Any object implementing this trait guarantees exclusive access to the data contained within + /// the Mutex for the duration of the provided closure. + /// + /// The trait follows the [Rust embedded WG's + /// proposal](https://github.com/korken89/wg/blob/master/rfcs/0377-mutex-trait.md) and therefore + /// provides some goodness such as [deadlock + /// prevention](https://github.com/korken89/wg/blob/master/rfcs/0377-mutex-trait.md#design-decisions-and-compatibility). + /// + /// # Example + /// + /// Since the lock function takes an `&mut self` to enable deadlock-prevention, the trait is + /// best implemented **for a reference to a container struct**, and has a usage pattern that + /// might feel strange at first: + /// + /// ``` + /// static MUT: Mutex> = Mutex::new(RefCell::new(0)); + /// + /// fn foo() { + /// let mut r = &MUT; // Note that r is mutable + /// r.lock(|data| *data += 1); + /// } + /// ``` + pub trait Mutex { + /// The type of encapsulated data. + type Data; + + /// Creates a critical section and grants temporary mutable access to the encapsulated data. + fn lock(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R; + } +} + +/// A pseudo-lock for teaching purposes. +/// +/// Used to introduce [interior mutability]. +/// +/// In contrast to a real Mutex implementation, does not protect against concurrent access from +/// other cores to the contained data. This part is preserved for later lessons. +/// +/// The lock will only be used as long as it is safe to do so, i.e. as long as the kernel is +/// executing single-threaded, aka only running on a single core with interrupts disabled. +/// +/// [interior mutability]: https://doc.rust-lang.org/std/cell/index.html +pub struct NullLock { + data: UnsafeCell, +} + +//-------------------------------------------------------------------------------------------------- +// Public Code +//-------------------------------------------------------------------------------------------------- + +unsafe impl Sync for NullLock {} + +impl NullLock { + /// Wraps `data` into a new `NullLock`. + pub const fn new(data: T) -> Self { + Self { + data: UnsafeCell::new(data), + } + } +} + +//------------------------------------------------------------------------------ +// OS Interface Code +//------------------------------------------------------------------------------ + +impl interface::Mutex for &NullLock { + type Data = T; + + fn lock(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R { + // In a real lock, there would be code encapsulating this line that ensures that this + // mutable reference will ever only be given out once at a time. + let data = unsafe { &mut *self.data.get() }; + + f(data) + } +} diff --git a/08_timestamps/src/time.rs b/08_timestamps/src/time.rs new file mode 100644 index 00000000..cd3ceec3 --- /dev/null +++ b/08_timestamps/src/time.rs @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// +// Copyright (c) 2020 Andre Richter + +//! Timer primitives. + +#[cfg(target_arch = "aarch64")] +#[path = "_arch/aarch64/time.rs"] +mod arch_time; +pub use arch_time::*; + +//-------------------------------------------------------------------------------------------------- +// Public Definitions +//-------------------------------------------------------------------------------------------------- + +/// Timekeeping interfaces. +pub mod interface { + use core::time::Duration; + + /// Time management functions. + /// + /// The `BSP` is supposed to supply one global instance. + pub trait TimeManager { + /// The timer's resolution. + fn resolution(&self) -> Duration; + + /// The uptime since power-on of the device. + /// + /// This includes time consumed by firmware and bootloaders. + fn uptime(&self) -> Duration; + + /// Spin for a given duration. + fn spin_for(&self, duration: Duration); + } +}