//! Pinnacle: a mobile operating system written from scratch in Rust. #![no_std] #![no_main] // The PC runs the kernel's core so far, not yet its programs: what it // does not yet call is not dead. #![cfg_attr(target_arch = "x86_64", allow(dead_code))] extern crate alloc; // One kernel for both architectures: a module with an `x86_64/` twin keeps // its interface, so the rest (the scheduler, the system calls, the // drivers) is the same code on both. #[cfg(target_arch = "x86_64")] #[path = "x86_64/acpi.rs"] mod acpi; #[cfg(target_arch = "x86_64")] #[path = "x86_64/apic.rs"] mod apic; #[cfg_attr(target_arch = "x86_64", path = "x86_64/arch.rs")] mod arch; mod args; mod board; #[cfg_attr(target_arch = "x86_64", path = "x86_64/boot.rs")] mod boot; mod chain; mod check; mod clock; mod console; mod device; mod drivers; #[cfg_attr(target_arch = "x86_64", path = "x86_64/interrupts.rs")] mod exceptions; mod fdt; mod futex; #[cfg(target_arch = "x86_64")] #[path = "x86_64/gdt.rs"] mod gdt; #[cfg_attr(target_arch = "x86_64", path = "x86_64/gic.rs")] mod gic; mod install; #[cfg(target_arch = "x86_64")] #[path = "x86_64/ioapic.rs"] mod ioapic; #[cfg_attr(target_arch = "x86_64", path = "x86_64/irq.rs")] mod irq; #[cfg(target_arch = "x86_64")] #[path = "x86_64/loader.rs"] mod loader; mod mm; #[cfg_attr(target_arch = "x86_64", path = "x86_64/mmu.rs")] mod mmu; mod object; mod pci; #[cfg(target_arch = "x86_64")] #[path = "x86_64/power.rs"] mod power; mod process; #[cfg(target_arch = "aarch64")] mod profile; mod programs; mod sched; mod selfcheck; #[cfg_attr(target_arch = "x86_64", path = "x86_64/smp.rs")] mod smp; mod sync; mod syscall; #[cfg_attr(target_arch = "x86_64", path = "x86_64/timer.rs")] mod timer; /// Entered from [`boot`] on a PC's first core, in long mode, at the /// image's addresses, with a stack; `start_info` is what the loader hands /// over, `magic` what it left in EAX (Multiboot2's says it is GRUB). #[cfg(target_arch = "x86_64")] extern "C" fn kmain(start_info: usize, magic: u32) -> ! { smp::set_core(0); gdt::load(0); println!("Pinnacle {} on {}", env!("CARGO_PKG_VERSION"), board::NAME); // What the loader hands over first, before anything is allocated: what // its map does not call RAM (ACPI's tables at the top among it) stays // out, and Multiboot2's information, in RAM, is copied out. let boot = loader::read(start_info, magic).expect("nothing from the loader"); println!( "x86-64: entered by {}, its information at {start_info:#x}; long mode, paging on", boot.protocol ); let mapped = mmu::map_above_4gib(|start, end| boot.ram().iter().any(|r| r.start < end && r.end > start)); // Write-combining as one of the page types, for the screen. mmu::set_pat(); // The screen the firmware lit, before the allocator, which must not // hand out its memory (outside the RAM map, so it does not). if let Some(fb) = boot.framebuffer { board::note_firmware_screen(&fb); } drivers::scanout::find(0); if boot.ranges == 0 { mm::init(0); } else { // Within what is mapped, and what the board's allocator can keep. let end = boot.ram_end().min((mapped as u64) << 30); mm::init_from_map(boot.holes(mm::RAM.1 as u64), end as usize); } args::set(boot.command_line()); drivers::screen_log::start(); let ram: u64 = boot.ram().iter().map(|r| r.end - r.start).sum(); println!( "memory map: {} ranges of RAM, {} MiB, up to {:#x}; {mapped} GiB mapped", boot.ranges, ram >> 20, boot.ram_end() ); if boot.dropped > 0 { println!("memory map: {} more ranges of RAM left out", boot.dropped); } if mm::ram_size() < boot.ram_end() as usize { println!( "memory map: RAM above {} MiB left out (unmapped, or past the board's most)", mm::ram_size() >> 20 ); } if let Some(fb) = boot.framebuffer { println!( "framebuffer: {}x{} at {:#x}, {} bits, pitch {}", fb.width, fb.height, fb.address, fb.bits_per_pixel, fb.pitch ); } println!("cpu: {}", arch::cpu_name()); check::memory(); arch::enable_sse(); let (sha, aes) = arch::crypto_features(); pinnacle_crypto::hw::enable_features(sha, aes, true); timer::calibrate(); println!( "timer: the time-stamp counter, {} MHz", timer::frequency() / 1_000_000 ); check::crypto(); exceptions::init(); let madt = acpi::madt(boot.rsdp).expect("no MADT in ACPI's tables"); println!( "acpi: {} cores (APIC IDs {:?}), {} I/O APIC, local APIC at {:#x}", madt.cores.len(), madt.cores, madt.io_apics.len(), madt.local_apic ); apic::init( madt.local_apic, boot.command_line().split(' ').any(|arg| arg == "xapic"), ); smp::set_boot_apic_id(apic::id()); println!( "apic: {} mode, the boot core's ID {}", if apic::x2apic() { "x2APIC" } else { "xAPIC" }, apic::id() ); apic::start_timer(timer::HZ); let pins = ioapic::init(&madt.io_apics, &madt.overrides); println!("ioapic: {pins} pins, {} ISA overrides", madt.overrides.len()); for (ecam, first, last) in acpi::mcfg(boot.rsdp) { let host = pci::Host { ecam, buses: (first, last), window: None, interrupt_map: None, interrupt_mask: None, }; println!("pci: {} functions", pci::scan(&host)); } irq::enable(); check::interrupts(); if acpi::init_power(boot.rsdp).is_none() { println!("acpi: no FADT: off and the power button by QEMU's means only"); } // The firmware's AML: the embedded controller, battery, charger, lid. power::init(boot.rsdp); // The panel's brightness: Intel's graphics, else the firmware's methods. drivers::backlight::init(); // A touchpad on I²C, found through the AML; read by a task of its own, // started beside `power::watch`. drivers::i2c_hid::init(); // USB before the disks: a stick is one of them. // Not inside println!: the driver prints as it goes, and the console's // lock is held while println!'s arguments are worked out. let usb = drivers::usb::init(); println!("usb: {usb} devices"); // The system the boot record names, if it is not this one: handed the // machine before any other core runs (and while the identity map the // handover runs from is open), and not returned from. match boot.slot() { None => chain::boot(0, 0), Some(slot) => chain::handed_over(slot), } let cores = smp::start_cores(&madt.cores); mmu::close_identity(); println!("smp: {cores} cores running"); // A machine goes on with the cores it has; QEMU's runs must have all. if cores != madt.cores.len().min(smp::MAX_CORES) { assert!(!args::has("selfcheck"), "a core did not start"); println!("smp: going on without the cores that did not start"); } timer::start(); selfcheck::scheduler(); selfcheck::smp(); selfcheck::programs(); selfcheck::frits(); selfcheck::kees(); // The devices, all on PCI: the same drivers and checks as ARM's. match drivers::gpu::init() { Some((w, h)) => println!("gpu: virtio-gpu, a {w}x{h} scanout"), None => println!("gpu: none"), } selfcheck::display(); selfcheck::files(); selfcheck::vault(); selfcheck::io(); selfcheck::storage(); selfcheck::usb(); // A laptop's own keyboard and touchpad. drivers::ps2::init(); install::from_command_line(); match drivers::sound::init() { Some(info) => println!( "sound: {}, {} stream{}, 48 kHz stereo", info.device, info.streams, if info.streams == 1 { "" } else { "s" } ), None => println!("sound: no device"), } selfcheck::audio(); let random = drivers::random::init(); println!("random: seeded{}", random.describe()); // QEMU's virtio-rng is the device whose interrupt proves message // interrupts arrive; a laptop has none (its disks' and USB's prove it). if random.virtio { check::device_interrupt(); } if let Some((mac, what)) = drivers::net::init() { println!( "net: {what} {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}, asking DHCP", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); } // Intel's Wi-Fi, as far as its firmware's ALIVE; silent without one. drivers::iwlwifi::init(); let checking = boot.command_line().split(' ').any(|arg| arg == "selfcheck"); { use device::Class::*; // What QEMU's self-check machine has; a laptop has its own. selfcheck::devices(if checking { &[Storage, Network, Display, Sound, Random][..] } else { &[] }); } // The network's check wants QEMU's network: its router's IPv6, the // package source beside it. On any other it fails, and a laptop with a // cable in would not start. if checking { selfcheck::network(); selfcheck::network_grant(); } selfcheck::idle(); println!("pc ok"); // `selfcheck` on the command line (`cargo xtask pc`): the check is the // point. Otherwise the system starts, as on ARM. if checking { board::power_off(0) } drivers::usb::watch(); power::watch(); drivers::i2c_hid::watch(); let inputs = drivers::input::init(); println!("input: {inputs} devices"); drivers::screen_log::stop(); let display = programs::find(b"display").expect("built without the display server"); let id = sched::spawn_program(display, 0, alloc::vec::Vec::new(), true, false, None) .expect("could not start the display server"); sched::name(id, b"display"); println!("running"); sched::idle() } #[cfg(target_arch = "aarch64")] /// Entered from [`boot`] on core 0, at EL1, with the MMU and caches on, a /// stack and a cleared `.bss`; `entered_at` is the EL the loader left us at; /// `slot` the slot a boot kernel handed over from (`chain`), 0 when a loader /// started this one. extern "C" fn kmain(device_tree: usize, entered_at: usize, slot: u64) -> ! { println!("Pinnacle {} on {}", env!("CARGO_PKG_VERSION"), board::NAME); println!( "running at EL{}, entered at EL{entered_at}, caches {}, device tree at {device_tree:#x}", arch::current_el(), if arch::caches_on() { "on" } else { "off" }, ); mmu::close_lower_half(); let description = device::locate(device_tree); // Before the allocator, which must not hand out the screen's memory. drivers::scanout::find(description); mm::init(description); args::set(fdt::DeviceTree::at(device_tree).map_or("selfcheck", |tree| tree.bootargs())); drivers::screen_log::start(); check::memory(); println!("devices: {} in the device tree", device::discover(description)); for &(compatible, driver, class) in board::OWN { device::mark_compatible(compatible, driver, class); } // The core's own crypto instructions, where it has them. pinnacle_crypto::hw::enable(arch::isar0(), true); check::crypto(); exceptions::init(); gic::init(); if let Some(host) = pci::host_from_device_tree() { println!("pci: {} functions", pci::scan(&host)); } timer::start(); println!( "timer: {} Hz counter, tickless, {} ms slices", timer::frequency(), timer::SLICE_MS ); // Asked once, here: fw_cfg is one register pair, which cores asking // at once from their ticks garbled. profile::init(); // USB before the disks: a stick is one of them, and the system the // boot record names may be on it. // Not inside println!: the driver prints as it goes, and the console's // lock is held while println!'s arguments are worked out. let usb = drivers::usb::init(); println!("usb: {usb} devices"); // The system the boot record names, if it is not this one: handed the // machine before any other core runs, and not returned from. if slot == 0 { chain::boot(device_tree, entered_at); } else { chain::handed_over(slot); } let cores = smp::start_cores(entered_at); println!("smp: {cores} cores running"); selfcheck::scheduler(); selfcheck::smp(); selfcheck::programs(); selfcheck::frits(); selfcheck::kees(); match drivers::gpu::init() { Some((w, h)) => println!("gpu: virtio-gpu, a {w}x{h} scanout"), None => println!("gpu: none; the framebuffer is a ramfb"), } selfcheck::display(); selfcheck::files(); selfcheck::vault(); selfcheck::io(); selfcheck::storage(); selfcheck::usb(); install::from_command_line(); match drivers::sound::init() { Some(info) => println!( "sound: {}, {} streams, 48 kHz stereo; {}", info.device, info.streams, if info.capture { "plays and records" } else { "plays only" } ), None => println!("sound: no device"), } selfcheck::audio(); match drivers::serial::init() { 0 => {} 1 => println!("serial: a port to the Bluetooth controller (virtio-console)"), 2 => println!("serial: ports to the Bluetooth controller and the modem (virtio-console)"), _ => println!( "serial: ports to the Bluetooth controller, the modem and the Wi-Fi radio (virtio-console)" ), } selfcheck::bluetooth(); selfcheck::modem(); let random = drivers::random::init(); println!("random: seeded{}", random.describe()); let hardware = random.virtio; // On PCI (`cargo xtask test` puts it there): its INTx, routed by the // device tree's interrupt map. if hardware { check::device_interrupt(); } let network = drivers::net::init(); if let Some((mac, what)) = network { println!( "net: {what} {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}, asking DHCP", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); } // Intel's Wi-Fi on a board's PCIe, as on a PC; silent without one. drivers::iwlwifi::init(); { use device::Class::*; selfcheck::devices(&[Interrupts, Storage, Network, Display, Sound, Random]); } // `selfcheck` on the command line (what `cargo xtask test` passes), or // no device tree at all (an ELF booted bare): the check is the point. let bootargs = fdt::DeviceTree::at(device_tree).map_or("selfcheck", |tree| tree.bootargs()); let checking = bootargs.split(' ').any(|arg| arg == "selfcheck"); selfcheck::network(); selfcheck::network_grant(); selfcheck::std(); selfcheck::ports(); selfcheck::https(); // Only when checking: it waits for the host to connect, which only // `cargo xtask test` does; a system booted to be used would wait 15 s // and stop. if checking { selfcheck::listen(); } selfcheck::idle(); if checking { board::power_off(0) } // The system: input devices, then the display server, which starts the // rest. It is the one privileged program. drivers::usb::watch(); let inputs = drivers::input::init(); println!("input: {inputs} devices"); drivers::screen_log::stop(); let display = programs::find(b"display").expect("built without the display server"); let id = sched::spawn_program(display, 0, alloc::vec::Vec::new(), true, false, None) .expect("could not start the display server"); sched::name(id, b"display"); println!("running"); sched::idle() } #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { use core::fmt::Write as _; drivers::screen_log::panicking(); // Not through the console's lock: the panic may be inside it. let _ = writeln!(console::Raw, "panic on core {}: {info}", arch::core_id()); board::power_off(1) }