2019-11-25 18:54:05 +00:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
2019-10-17 07:33:42 +00:00
|
|
|
//
|
2022-01-15 20:50:11 +00:00
|
|
|
// Copyright (c) 2018-2022 Andre Richter <andre.o.richter@gmail.com>
|
2019-10-17 07:33:42 +00:00
|
|
|
|
|
|
|
// Rust embedded logo for `make doc`.
|
2022-04-27 20:08:55 +00:00
|
|
|
#![doc(
|
|
|
|
html_logo_url = "https://raw.githubusercontent.com/rust-embedded/wg/master/assets/logo/ewg-logo-blue-white-on-transparent.png"
|
|
|
|
)]
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2020-03-28 12:26:27 +00:00
|
|
|
//! The `kernel` binary.
|
2019-10-17 07:33:42 +00:00
|
|
|
//!
|
2020-03-28 12:26:27 +00:00
|
|
|
//! # 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,
|
2021-01-23 21:43:59 +00:00
|
|
|
//! architectural code that belongs to the `kernel`'s MMU subsystem (`src/memory/mmu.rs`) would go
|
|
|
|
//! into `src/_arch/aarch64/memory/mmu.rs`. The latter file is loaded as a module in
|
|
|
|
//! `src/memory/mmu.rs` using the `path attribute`. Usually, the chosen module name is the generic
|
|
|
|
//! module's name prefixed with `arch_`.
|
2020-03-28 12:26:27 +00:00
|
|
|
//!
|
2021-01-23 21:43:59 +00:00
|
|
|
//! For example, this is the top of `src/memory/mmu.rs`:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! #[cfg(target_arch = "aarch64")]
|
|
|
|
//! #[path = "../_arch/aarch64/memory/mmu.rs"]
|
|
|
|
//! mod arch_mmu;
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Often times, items from the `arch_ module` will be publicly reexported by the parent module.
|
|
|
|
//! This way, each architecture specific module can provide its implementation of an item, while the
|
|
|
|
//! caller must not be concerned which architecture has been conditionally compiled.
|
2020-03-28 12:26:27 +00:00
|
|
|
//!
|
|
|
|
//! ## 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
|
2021-01-23 21:43:59 +00:00
|
|
|
//! `kernel`'s subsystem modules, but there is no reexporting this time. That means whatever is
|
|
|
|
//! provided must be called starting from the `bsp` namespace, e.g. `bsp::driver::driver_manager()`.
|
2020-03-28 12:26:27 +00:00
|
|
|
//!
|
|
|
|
//! ## 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::*`
|
2021-01-23 21:43:59 +00:00
|
|
|
//!
|
|
|
|
//! # Boot flow
|
|
|
|
//!
|
2021-03-20 08:41:43 +00:00
|
|
|
//! 1. The kernel's entry point is the function `cpu::boot::arch_boot::_start()`.
|
|
|
|
//! - It is implemented in `src/_arch/__arch_name__/cpu/boot.s`.
|
2021-07-02 21:04:18 +00:00
|
|
|
//! 2. Once finished with architectural setup, the arch code calls `kernel_init()`.
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2021-04-29 21:08:49 +00:00
|
|
|
#![allow(clippy::upper_case_acronyms)]
|
2019-10-17 07:33:42 +00:00
|
|
|
#![feature(format_args_nl)]
|
|
|
|
#![feature(panic_info_message)]
|
|
|
|
#![feature(trait_alias)]
|
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
mod bsp;
|
2020-03-28 12:26:27 +00:00
|
|
|
mod console;
|
|
|
|
mod cpu;
|
|
|
|
mod driver;
|
2019-10-17 07:33:42 +00:00
|
|
|
mod panic_wait;
|
|
|
|
mod print;
|
2020-03-28 12:26:27 +00:00
|
|
|
mod synchronization;
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2019-10-20 12:42:59 +00:00
|
|
|
/// Early init code.
|
|
|
|
///
|
|
|
|
/// # 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() -> ! {
|
2020-03-28 12:26:27 +00:00
|
|
|
use driver::interface::DriverManager;
|
|
|
|
|
|
|
|
for i in bsp::driver::driver_manager().all_device_drivers().iter() {
|
2020-09-29 19:43:31 +00:00
|
|
|
if let Err(x) = i.init() {
|
|
|
|
panic!("Error loading driver: {}: {}", i.compatible(), x);
|
2019-10-20 12:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 12:26:27 +00:00
|
|
|
bsp::driver::driver_manager().post_device_driver_init();
|
2019-11-06 21:45:29 +00:00
|
|
|
// println! is usable from here on.
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2019-10-20 12:42:59 +00:00
|
|
|
// Transition from unsafe to safe.
|
|
|
|
kernel_main()
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:48:02 +00:00
|
|
|
const MINILOAD_LOGO: &str = r#"
|
|
|
|
__ __ _ _ _ _
|
|
|
|
| \/ (_)_ _ (_) | ___ __ _ __| |
|
|
|
|
| |\/| | | ' \| | |__/ _ \/ _` / _` |
|
|
|
|
|_| |_|_|_||_|_|____\___/\__,_\__,_|
|
|
|
|
"#;
|
|
|
|
|
2019-10-20 12:42:59 +00:00
|
|
|
/// The main function running after the early init.
|
|
|
|
fn kernel_main() -> ! {
|
2020-03-28 12:26:27 +00:00
|
|
|
use bsp::console::console;
|
|
|
|
use console::interface::All;
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2021-03-31 20:48:02 +00:00
|
|
|
println!("{}", MINILOAD_LOGO);
|
2019-10-17 07:33:42 +00:00
|
|
|
println!("{:^37}", bsp::board_name());
|
|
|
|
println!();
|
|
|
|
println!("[ML] Requesting binary");
|
2020-03-28 12:26:27 +00:00
|
|
|
console().flush();
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2021-01-04 13:37:17 +00:00
|
|
|
// Discard any spurious received characters before starting with the loader protocol.
|
|
|
|
console().clear_rx();
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2020-01-14 19:45:41 +00:00
|
|
|
// Notify `Minipush` to send the binary.
|
2019-10-17 07:33:42 +00:00
|
|
|
for _ in 0..3 {
|
2020-03-28 12:26:27 +00:00
|
|
|
console().write_char(3 as char);
|
2019-10-17 07:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the binary's size.
|
2020-03-28 12:26:27 +00:00
|
|
|
let mut size: u32 = u32::from(console().read_char() as u8);
|
|
|
|
size |= u32::from(console().read_char() as u8) << 8;
|
|
|
|
size |= u32::from(console().read_char() as u8) << 16;
|
|
|
|
size |= u32::from(console().read_char() as u8) << 24;
|
2019-10-17 07:33:42 +00:00
|
|
|
|
|
|
|
// Trust it's not too big.
|
2020-03-28 12:26:27 +00:00
|
|
|
console().write_char('O');
|
|
|
|
console().write_char('K');
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2020-09-29 19:43:31 +00:00
|
|
|
let kernel_addr: *mut u8 = bsp::memory::board_default_load_addr() as *mut u8;
|
2019-10-17 07:33:42 +00:00
|
|
|
unsafe {
|
|
|
|
// Read the kernel byte by byte.
|
|
|
|
for i in 0..size {
|
2020-10-28 15:29:50 +00:00
|
|
|
core::ptr::write_volatile(kernel_addr.offset(i as isize), console().read_char() as u8)
|
2019-10-17 07:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("[ML] Loaded! Executing the payload now\n");
|
2020-03-28 12:26:27 +00:00
|
|
|
console().flush();
|
2019-10-17 07:33:42 +00:00
|
|
|
|
2021-01-23 21:43:59 +00:00
|
|
|
// Use black magic to create a function pointer.
|
2020-10-28 22:10:01 +00:00
|
|
|
let kernel: fn() -> ! = unsafe { core::mem::transmute(kernel_addr) };
|
2019-10-17 07:33:42 +00:00
|
|
|
|
|
|
|
// Jump to loaded kernel!
|
|
|
|
kernel()
|
|
|
|
}
|