git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/board/qemu_virt.rs
//! QEMU's `virt` machine, run with `-m 256M` (the self-check) or more.
use crate::mmu::{Kind, Region};
pub const NAME: &str = "qemu-virt";
/// Flash, the GIC and the UART below 1 GiB; RAM from 1 GiB. Mapped for RAM:
/// 2.5 GiB and 2 MiB (what a guest of `-m 3G` has below 4 GiB, as far as
/// a 2 MiB block more than whole GiBs, which keeps the 2 MiB path of the
/// tables under test). The pages handed out are the RAM the device tree
/// says the machine has (`mm::ram_end`): 256 MiB for the self-check.
pub const MEMORY: &[Region] = &[
Region {
start: 0,
end: 0x4000_0000,
kind: Kind::Device,
},
Region {
start: 0x4000_0000,
end: 0xE020_0000,
kind: Kind::Normal,
},
];
/// A PL011 at 0x0900_0000. QEMU needs no setup.
pub mod uart {
const DR: *mut u32 = crate::mmu::virt(0x0900_0000) as *mut u32;
const FR: *const u32 = crate::mmu::virt(0x0900_0018) as *const u32;
const FR_TXFF: u32 = 1 << 5;
pub fn put(byte: u8) {
// SAFETY: the PL011's registers, mapped as Device memory.
unsafe {
while FR.read_volatile() & FR_TXFF != 0 {}
DR.write_volatile(u32::from(byte));
}
}
}
/// Ends QEMU with `code` as its exit status, through semihosting
/// (`-semihosting-config enable=on`): how a test run reports its result.
pub fn power_off(code: u32) -> ! {
const SYS_EXIT: u64 = 0x18;
const ADP_STOPPED_APPLICATION_EXIT: u64 = 0x2_0026;
let block = [ADP_STOPPED_APPLICATION_EXIT, u64::from(code)];
// SAFETY: a semihosting call; QEMU does not return from SYS_EXIT.
unsafe {
core::arch::asm!("hlt #0xf000", in("x0") SYS_EXIT, in("x1") block.as_ptr(), options(nostack));
}
loop {
core::hint::spin_loop();
}
}
/// Restarts the machine: PSCI SYSTEM_RESET, which QEMU answers by starting
/// it again from its loader (and with `-no-reboot` by ending).
pub fn restart() -> ! {
crate::smp::system_reset()
}
/// The GICv3 (`-M virt,gic-version=3`): the distributor, and the first
/// core's redistributor (each next core's is 128 KiB further). Where it is
/// when no device tree says.
pub mod gic {
pub const DISTRIBUTOR: usize = crate::mmu::virt(0x0800_0000);
pub const REDISTRIBUTOR: usize = crate::mmu::virt(0x080a_0000);
}
/// RAM the kernel must not hand out besides its own image: none here.
pub const RESERVED: &[(usize, usize)] = &[];
/// QEMU answers PSCI itself: by HVC, or by SMC when it emulates EL2.
pub const PSCI_SMC: bool = false;
/// What the kernel drives here without a driver of its own: the console
/// and the clock, for the device list.
pub const OWN: &[(&str, &str, crate::device::Class)] = &[
("arm,pl011", "console", crate::device::Class::Serial),
("arm,pl031", "real-time clock", crate::device::Class::Clock),
];
/// QEMU's firmware configuration device (fw_cfg), which sets up `ramfb`.
pub const FW_CFG: Option<usize> = Some(crate::mmu::virt(0x0902_0000));
const PL031: usize = crate::mmu::virt(0x0901_0000);
/// The PL031 real-time clock: seconds since 1970 in its data register.
pub fn clock() -> u64 {
// SAFETY: the RTC's data register, mapped as Device memory.
u64::from(unsafe { (PL031 as *const u32).read_volatile() })
}
/// Sets the PL031: its load register (RTCLR) starts it counting from
/// `seconds`.
pub fn set_clock(seconds: u64) {
// SAFETY: the RTC's load register, mapped as Device memory.
unsafe { ((PL031 + 8) as *mut u32).write_volatile(seconds as u32) };
}
/// No display controller here that a loader leaves lit: a screen comes
/// from the device tree, or from a driver.
pub fn lit_screen() -> Option<crate::drivers::scanout::Scanout> {
None
}