//! A PC (x86-64): QEMU's `q35` machine (run with `-m 256M`), or a laptop //! booted by GRUB. use crate::mmu::{Kind, Region}; pub const NAME: &str = "pc"; /// RAM from 0 to at most 128 GiB, as much of it as the loader's memory map /// calls RAM; its first MiB (the BIOS's data, the loader's start info, the /// VGA window) kept out. The page allocator keeps a byte per page of it /// (32 MiB, in `.bss`). pub const MEMORY: &[Region] = &[Region { start: 0, end: 128 << 30, kind: Kind::Normal, }]; /// The BIOS's first MiB. pub const RESERVED: &[(usize, usize)] = &[(0, 0x10_0000)]; /// What the kernel drives here without a driver of its own: none listed, /// as a PC has no device tree. pub const OWN: &[(&str, &str, crate::device::Class)] = &[]; /// No fw_cfg framebuffer: the display comes with a PCI device. pub const FW_CFG: Option = None; /// A file of QEMU's fw_cfg, read through its I/O ports (selector 0x510, /// data 0x511) into `buffer`: how many bytes, or None if there is no such /// file (or no fw_cfg). pub fn fw_cfg_file(name: &[u8], buffer: &mut [u8]) -> Option { use crate::arch::{in8, out16}; const SELECTOR: u16 = 0x510; const DATA: u16 = 0x511; const FILE_DIR: u16 = 0x19; let bytes = |n: usize| { let mut b = [0u8; 4]; for byte in b.iter_mut().take(n) { *byte = in8(DATA); } b }; // The signature, "QEMU", says there is a fw_cfg at all. out16(SELECTOR, 0); if bytes(4) != *b"QEMU" { return None; } out16(SELECTOR, FILE_DIR); let count = u32::from_be_bytes(bytes(4)); let mut found = None; for _ in 0..count.min(256) { let size = u32::from_be_bytes(bytes(4)) as usize; let select = u16::from_be_bytes([in8(DATA), in8(DATA)]); let _reserved = bytes(2); let mut file = [0u8; 56]; for byte in &mut file { *byte = in8(DATA); } let len = file.iter().position(|&b| b == 0).unwrap_or(56); if found.is_none() && &file[..len] == name { found = Some((select, size)); } } let (select, size) = found?; out16(SELECTOR, select); let n = size.min(buffer.len()); for byte in &mut buffer[..n] { *byte = in8(DATA); } Some(n) } /// Seconds since 1970 from the CMOS clock (BCD or binary, 24-hour as the /// firmware leaves it), read when it is not mid-update. pub fn clock() -> u64 { use crate::arch::{in8, out8}; let read = |register: u8| { out8(0x70, register); in8(0x71) }; while read(0x0a) & 0x80 != 0 {} let binary = read(0x0b) & 0x04 != 0; let value = |register: u8| { let v = read(register); u64::from(if binary { v } else { (v & 0x0f) + (v >> 4) * 10 }) }; let (s, mi, h, d, mo, y) = ( value(0x00), value(0x02), value(0x04), value(0x07), value(0x08), value(0x09), ); // Days from 1970 to the date, by the civil calendar (2000–2099). let year = 2000 + y; let (y_, m_) = if mo <= 2 { (year - 1, mo + 9) } else { (year, mo - 3) }; let era_days = 365 * y_ + y_ / 4 - y_ / 100 + y_ / 400 + (153 * m_ + 2) / 5 + d - 1; let days = era_days - 719_468; days * 86_400 + h * 3600 + mi * 60 + s } /// Sets the CMOS clock to `seconds` since 1970 (2000–2099), in the form it /// keeps (BCD or binary), its updates held meanwhile (register B's SET). pub fn set_clock(seconds: u64) { use crate::arch::{in8, out8}; let days = seconds / 86_400; let rest = seconds % 86_400; // The date by the civil calendar (Howard Hinnant's civil-from-days). let z = days + 719_468; let (era, doe) = (z / 146_097, z % 146_097); let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let day = doy - (153 * mp + 2) / 5 + 1; let month = if mp < 10 { mp + 3 } else { mp - 9 }; let year = yoe + era * 400 + u64::from(month <= 2); if !(2000..2100).contains(&year) { return; } let register = |r: u8| { out8(0x70, r); in8(0x71) }; let write = |r: u8, v: u8| { out8(0x70, r); out8(0x71, v); }; let b = register(0x0b); let form = |v: u64| { let v = v as u8; if b & 0x04 != 0 { v } else { ((v / 10) << 4) | (v % 10) } }; write(0x0b, b | 0x80); write(0x00, form(rest % 60)); write(0x02, form(rest / 60 % 60)); write(0x04, form(rest / 3600)); // Sunday is 1; 1 January 1970 was a Thursday. write(0x06, form((days + 4) % 7 + 1)); write(0x07, form(day)); write(0x08, form(month)); write(0x09, form(year - 2000)); write(0x0b, b); } /// The first serial port, a 16550 at I/O port 0x3f8. QEMU needs no setup. pub mod uart { const DATA: u16 = 0x3f8; const LINE_STATUS: u16 = 0x3fd; const TRANSMIT_EMPTY: u8 = 1 << 5; pub fn put(byte: u8) { while crate::arch::in8(LINE_STATUS) & TRANSMIT_EMPTY == 0 {} crate::arch::out8(DATA, byte); } } /// Turns the PC off. In a virtual machine, first through QEMU's /// `isa-debug-exit` (port 0xf4), where it has one: its exit status is /// twice the value written plus one, 1 for success, 3 for a failure. Then, /// for a success, by ACPI (S5), as a laptop is. A failure on a real /// machine stays on, still: a laptop has no serial port, and the screen /// says what went wrong. pub fn power_off(code: u32) -> ! { // CPUID 1, ECX bit 31: under a hypervisor. if crate::arch::cpuid(1)[2] & (1 << 31) != 0 { crate::arch::out8(0xf4, code as u8); } if code == 0 { crate::acpi::power_off(); } let _masked = crate::irq::Masked::new(); loop { crate::arch::wait_for_interrupt(); } } /// Restarts the PC: by the FADT's reset register where it names one, else /// a full reset through the chipset's reset control register (0xcf9), and /// the keyboard controller's reset line (0x64) where that does nothing. pub fn restart() -> ! { crate::acpi::restart(); crate::arch::out8(0xcf9, 0x02); crate::arch::out8(0xcf9, 0x06); crate::arch::out8(0x64, 0xfe); loop { crate::arch::wait_for_interrupt(); } } /// The screen the firmware lit (UEFI's GOP, as the loader passed it on), /// noted before the allocator runs. static FIRMWARE_SCREEN: crate::sync::SpinLock> = crate::sync::SpinLock::new(None); /// Notes the loader's framebuffer as the screen to draw on: 32 bits a /// pixel with red, green and blue a byte each (blue lowest, as every GOP /// so far, or red lowest), or 16 bits as 5-6-5. Says why it takes no /// other. pub fn note_firmware_screen(fb: &crate::loader::Framebuffer) { use crate::drivers::scanout::{Format, Scanout}; let format = match (fb.bits_per_pixel, fb.colours) { (32, [(16, 8), (8, 8), (0, 8)]) => Format::Xrgb8888, (32, [(0, 8), (8, 8), (16, 8)]) => Format::Xbgr8888, (16, [(11, 5), (5, 6), (0, 5)]) => Format::Rgb565, (bits, colours) => { crate::println!( "display: the firmware's screen has {bits} bits a pixel as {colours:?}, not drawn" ); return; } }; *FIRMWARE_SCREEN.lock() = Some(Scanout { address: fb.address as usize, width: fb.width, height: fb.height, stride: fb.pitch, format, source: "firmware framebuffer", }); } /// The firmware's screen, if the loader passed one on: what a PC draws on /// until it has a driver for its graphics, and a laptop for good so far. pub fn lit_screen() -> Option { *FIRMWARE_SCREEN.lock() }