diff --git a/crates/aienos-boot/src/handoff.rs b/crates/aienos-boot/src/handoff.rs index 135ae2a6..af7053d1 100644 --- a/crates/aienos-boot/src/handoff.rs +++ b/crates/aienos-boot/src/handoff.rs @@ -166,6 +166,7 @@ fn enter_kernel_mmu( uart: Option, xhci_mmio: Option, ecam: Option, + gic: Option, ) -> (usize, usize, bool) { let descriptors: alloc::vec::Vec<_> = memory_map .entries() @@ -198,6 +199,20 @@ fn enter_kernel_mmu( if let Some(range) = ecam { mmio.push(range); } + if let Some(gic) = gic { + if let Some(base) = gic.distributor { + mmio.push(AddressRange { + start: base, + length: 0x10000, + }); + } + if let Some((base, size)) = gic.redistributor { + mmio.push(AddressRange { + start: base, + length: u64::from(size).max(0x20000), + }); + } + } let (plan, decision) = map_plan::build_map_plan(&descriptors, image.start, image, sections, attrs, &mmio) .expect("identity map plan rejected firmware layout"); @@ -318,6 +333,71 @@ static VAR_WRITES: AtomicU8 = AtomicU8::new(0); static CONSOLE: SpinLock> = SpinLock::new(None); static FREQUENCY_HZ: AtomicU64 = AtomicU64::new(0); static FRAMEBUFFER: SpinLock> = SpinLock::new(None); +static IRQ_TICKS: AtomicU64 = AtomicU64::new(0); +static TIMER_DEADLINE: AtomicU64 = AtomicU64::new(0); + +extern "C" fn timer_irq() { + let mut cpu = aienos_kernel::gic::Aarch64GicCpuInterface; + let id = aienos_kernel::gic::GicCpuInterface::acknowledge(&mut cpu); + if id == 30 { + IRQ_TICKS.fetch_add(1, Ordering::Relaxed); + let next = TIMER_DEADLINE.load(Ordering::Relaxed); + let frequency = aienos_kernel::timer::TimerRegisters::frequency( + &aienos_kernel::timer::Aarch64TimerRegisters, + ); + let deadline = next.wrapping_add(frequency / 100); + TIMER_DEADLINE.store(deadline, Ordering::Relaxed); + let mut timer = aienos_kernel::timer::Aarch64TimerRegisters; + aienos_kernel::timer::TimerRegisters::set_compare(&mut timer, deadline); + aienos_kernel::timer::TimerRegisters::enable_timer(&mut timer, true); + } + // End every acknowledged interrupt (not just the timer), or it stays + // active in the GIC and blocks its priority level. INTIDs 1020-1023 are + // special/spurious and must not be ended. + if id < 1020 { + aienos_kernel::gic::GicCpuInterface::end_interrupt(&mut cpu, id); + } +} + +fn run_timer_window(gic: Option) -> Option<(u64, u64)> { + let bases = gic?; + let distributor = bases.distributor? as usize; + let (redistributor, _) = bases.redistributor?; + let mut rd = aienos_kernel::gic::GicRedistributor(aienos_kernel::gic::MmioGicRegisters { + base: redistributor as usize, + }); + if !rd.wake(1_000_000) { + return None; + } + rd.configure_ppi(30, 0x80); + let mut dist = aienos_kernel::gic::GicDistributor(aienos_kernel::gic::MmioGicRegisters { + base: distributor, + }); + dist.enable_group1(); + let mut cpu = aienos_kernel::gic::Aarch64GicCpuInterface; + aienos_kernel::gic::GicCpuInterface::set_priority_mask(&mut cpu, 0xff); + aienos_kernel::gic::GicCpuInterface::enable_group1(&mut cpu); + let _ = distributor; + let mut timer = aienos_kernel::timer::Aarch64TimerRegisters; + let hz = aienos_kernel::timer::TimerRegisters::frequency(&timer); + let start = aienos_kernel::timer::TimerRegisters::counter(&timer); + let deadline = start.wrapping_add(hz / 100); + TIMER_DEADLINE.store(deadline, Ordering::Relaxed); + aienos_kernel::timer::TimerRegisters::set_compare(&mut timer, deadline); + aienos_kernel::timer::TimerRegisters::enable_timer(&mut timer, true); + aienos_kernel::fatal::set_irq_hook(timer_irq); + unsafe { + core::arch::asm!("msr daifclr, #2", "isb", options(nostack)); + } + while aienos_kernel::timer::TimerRegisters::counter(&timer).wrapping_sub(start) < hz / 5 { + core::hint::spin_loop(); + } + unsafe { + core::arch::asm!("msr daifset, #2", "isb", options(nostack)); + } + aienos_kernel::timer::TimerRegisters::enable_timer(&mut timer, false); + Some((IRQ_TICKS.load(Ordering::Relaxed), 200)) +} fn stage(s: u8) { STAGE.store(s, Ordering::SeqCst); @@ -617,6 +697,7 @@ unsafe fn table_at<'a>(addr: usize) -> Option<&'a [u8]> { struct AcpiFacts { cpu: Option, spcr: Option, + gic: Option, #[cfg(feature = "usb-keyboard")] mcfg: Option<&'static [u8]>, } @@ -651,7 +732,10 @@ fn discover_acpi(boot_mpidr: u64) -> AcpiFacts { continue; }; match &table[..4] { - b"APIC" => facts.cpu = acpi::madt_cpu_topology_for(table, Some(boot_mpidr)).ok(), + b"APIC" => { + facts.cpu = acpi::madt_cpu_topology_for(table, Some(boot_mpidr)).ok(); + facts.gic = acpi::madt_gic_bases(table).ok(); + } b"SPCR" => facts.spcr = acpi::spcr_console(table).ok(), #[cfg(feature = "usb-keyboard")] b"MCFG" => facts.mcfg = Some(table), @@ -1050,6 +1134,7 @@ fn main() -> Status { }), #[cfg(not(feature = "usb-keyboard"))] None, + acpi_facts.gic, ); fatal::set_fault_hook(on_fault); @@ -1087,6 +1172,7 @@ fn main() -> Status { gb10, cpu, ); + let irq_result = run_timer_window(acpi_facts.gic); let mut report = Report::new(); header(&mut report, "final"); @@ -1128,6 +1214,11 @@ fn main() -> Status { } ); let _ = write!(report, "{}", kernel_report.as_str()); + if let Some((ticks, ms)) = irq_result { + let _ = writeln!(report, "gic: v3\ntimer_irq: {ticks} ticks in {ms} ms"); + } else { + let _ = writeln!(report, "gic: unavailable\ntimer_irq: unavailable"); + } if gb10.is_none() { gb10_discovery.write_diagnostics(&mut report); } diff --git a/crates/aienos-kernel/src/fatal.rs b/crates/aienos-kernel/src/fatal.rs index be225420..13b1352b 100644 --- a/crates/aienos-kernel/src/fatal.rs +++ b/crates/aienos-kernel/src/fatal.rs @@ -167,6 +167,19 @@ pub type FaultHook = fn(&FaultInfo) -> !; #[cfg_attr(not(target_arch = "aarch64"), allow(dead_code))] static HOOK: AtomicUsize = AtomicUsize::new(0); +static IRQ_HOOK: AtomicUsize = AtomicUsize::new(0); + +pub fn set_irq_hook(hook: extern "C" fn()) { + IRQ_HOOK.store(hook as usize, Ordering::SeqCst); +} +#[cfg(target_arch = "aarch64")] +fn call_irq_hook() { + let raw = IRQ_HOOK.load(Ordering::SeqCst); + if raw != 0 { + let hook: extern "C" fn() = unsafe { core::mem::transmute(raw) }; + hook(); + } +} pub fn set_fault_hook(hook: FaultHook) { HOOK.store(hook as usize, Ordering::SeqCst); @@ -185,7 +198,7 @@ fn call_hook(info: &FaultInfo) -> ! { #[cfg(target_arch = "aarch64")] mod vectors { - use super::{call_hook, FaultInfo}; + use super::{call_hook, call_irq_hook, FaultInfo}; use crate::arch::aarch64::current_el; const STACK_BYTES: usize = 16 * 1024; @@ -241,9 +254,78 @@ mod vectors { "aienos_exception_vectors:", ".irp slot, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15", ".balign 128", + ".if \\slot == 5", + "b aienos_irq_trampoline", + ".else", "mov x0, #\\slot", "b aienos_exception_trampoline", + ".endif", ".endr", + // IRQ entry: save everything the handler (Rust, AAPCS64) may clobber: + // x0-x18 and x30, ELR/SPSR_EL1, FPCR/FPSR, and SIMD/FP caller-saved + // q0-q7 and q16-q31 (compiler-generated SIMD must not corrupt the + // interrupted code). Frame: 576 bytes, 16-byte aligned. + "aienos_irq_trampoline:", + "sub sp, sp, #576", + "stp x0, x1, [sp, #0]", + "stp x2, x3, [sp, #16]", + "stp x4, x5, [sp, #32]", + "stp x6, x7, [sp, #48]", + "stp x8, x9, [sp, #64]", + "stp x10, x11, [sp, #80]", + "stp x12, x13, [sp, #96]", + "stp x14, x15, [sp, #112]", + "stp x16, x17, [sp, #128]", + "stp x18, x30, [sp, #144]", + "mrs x0, elr_el1", + "mrs x1, spsr_el1", + "stp x0, x1, [sp, #160]", + "mrs x0, fpcr", + "mrs x1, fpsr", + "stp x0, x1, [sp, #176]", + "stp q0, q1, [sp, #192]", + "stp q2, q3, [sp, #224]", + "stp q4, q5, [sp, #256]", + "stp q6, q7, [sp, #288]", + "stp q16, q17, [sp, #320]", + "stp q18, q19, [sp, #352]", + "stp q20, q21, [sp, #384]", + "stp q22, q23, [sp, #416]", + "stp q24, q25, [sp, #448]", + "stp q26, q27, [sp, #480]", + "stp q28, q29, [sp, #512]", + "stp q30, q31, [sp, #544]", + "bl {irq_handler}", + "ldp q0, q1, [sp, #192]", + "ldp q2, q3, [sp, #224]", + "ldp q4, q5, [sp, #256]", + "ldp q6, q7, [sp, #288]", + "ldp q16, q17, [sp, #320]", + "ldp q18, q19, [sp, #352]", + "ldp q20, q21, [sp, #384]", + "ldp q22, q23, [sp, #416]", + "ldp q24, q25, [sp, #448]", + "ldp q26, q27, [sp, #480]", + "ldp q28, q29, [sp, #512]", + "ldp q30, q31, [sp, #544]", + "ldp x0, x1, [sp, #176]", + "msr fpcr, x0", + "msr fpsr, x1", + "ldp x0, x1, [sp, #160]", + "msr elr_el1, x0", + "msr spsr_el1, x1", + "ldp x0, x1, [sp, #0]", + "ldp x2, x3, [sp, #16]", + "ldp x4, x5, [sp, #32]", + "ldp x6, x7, [sp, #48]", + "ldp x8, x9, [sp, #64]", + "ldp x10, x11, [sp, #80]", + "ldp x12, x13, [sp, #96]", + "ldp x14, x15, [sp, #112]", + "ldp x16, x17, [sp, #128]", + "ldp x18, x30, [sp, #144]", + "add sp, sp, #576", + "eret", "aienos_exception_trampoline:", "adrp x1, {stack}", "add x1, x1, :lo12:{stack}", @@ -255,6 +337,7 @@ mod vectors { stack = sym EXCEPTION_STACK, stack_bytes_hi = const STACK_BYTES >> 12, handler = sym aienos_exception_handler, + irq_handler = sym call_irq_hook, ); extern "C" { diff --git a/crates/aienos-kernel/src/gic.rs b/crates/aienos-kernel/src/gic.rs index ce665545..7c4fb068 100644 --- a/crates/aienos-kernel/src/gic.rs +++ b/crates/aienos-kernel/src/gic.rs @@ -131,7 +131,7 @@ pub struct Aarch64GicCpuInterface; impl GicCpuInterface for Aarch64GicCpuInterface { fn enable_group1(&mut self) { unsafe { - core::arch::asm!("msr ICC_IGRPEN1_EL1, {0}", "isb", in(reg) 1u64, options(nostack)); + core::arch::asm!("mov x9, #1", "msr ICC_SRE_EL1, x9", "isb", "msr ICC_IGRPEN1_EL1, x9", "isb", out("x9") _, options(nostack)); } } fn set_priority_mask(&mut self, mask: u8) { diff --git a/scripts/qemu_boot_test.sh b/scripts/qemu_boot_test.sh index 32928c1e..52d6f288 100755 --- a/scripts/qemu_boot_test.sh +++ b/scripts/qemu_boot_test.sh @@ -33,7 +33,7 @@ qemu_accel=(-accel tcg,thread=single) started=$(date +%s) set +e timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \ - -M virt,virtualization=on "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ -drive if=pflash,format=raw,file="${work}/vars.fd" \ -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ @@ -61,6 +61,14 @@ check "left firmware and entered the kernel" "kernel: alive" check "kernel entered EL1h" "kernel_el: EL1h" check "cooperative threads interleaved" "threads: ok" check "EL1 page tables and caches enabled" "mmu: enabled" +check "GICv3 enabled" "gic: v3" +if grep -qE 'timer_irq: ([0-9]+) ticks' "${work}/serial.txt"; then + ticks=$(grep -oE 'timer_irq: ([0-9]+) ticks' "${work}/serial.txt" | tail -1 | grep -oE '[0-9]+') + [[ "${ticks}" -ge 5 ]] && echo "PASS timer IRQ delivered at least five ticks" || { echo "FAIL timer IRQ delivered fewer than five ticks"; failed=1; } +else + echo "FAIL timer IRQ count reported" + failed=1 +fi check "final report on the SPCR console" "report_kind: final" check "no panic or fault" "report_kind: final" if grep -qE "report_kind: (panic|fault)" "${work}/serial.txt"; then diff --git a/scripts/qemu_keyboard_test.sh b/scripts/qemu_keyboard_test.sh index 297b8918..d48de561 100755 --- a/scripts/qemu_keyboard_test.sh +++ b/scripts/qemu_keyboard_test.sh @@ -74,7 +74,7 @@ boot_once() { # Single-threaded TCG: multi-threaded TCG intermittently loses the # firmware's timer wake-up and hangs before AIENOS output (#61). qemu-system-aarch64 \ - -M virt,virtualization=on -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ -drive if=pflash,format=raw,file="${work}/vars.fd" \ -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ diff --git a/scripts/qemu_security_suite.sh b/scripts/qemu_security_suite.sh index 5f4a44c3..ae54a7a9 100755 --- a/scripts/qemu_security_suite.sh +++ b/scripts/qemu_security_suite.sh @@ -57,7 +57,7 @@ log="${work}/subtest1/serial_tpm.log" set +e timeout ${AIENOS_QEMU_TIMEOUT:-120} qemu-system-aarch64 \ - -M virt,virtualization=on "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ -chardev socket,id=chrtpm,path="${tpm_dir}/swtpm-sock" \ -tpmdev emulator,id=tpm0,chardev=chrtpm \ -device tpm-tis-device,tpmdev=tpm0 \ @@ -89,7 +89,7 @@ for i in $(seq 1 "${ITERATIONS}"); do soak_log="${soak_work}/serial_soak.log" set +e timeout ${AIENOS_QEMU_TIMEOUT:-120} qemu-system-aarch64 \ - -M virt,virtualization=on "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ -drive if=pflash,format=raw,file="${soak_work}/vars.fd" \ -drive if=none,id=esp,format=raw,file=fat:rw:"${soak_work}/esp" \ @@ -117,7 +117,7 @@ corrupt_log="${corrupt_work}/serial_corrupt.log" set +e timeout 30 qemu-system-aarch64 \ - -M virt,virtualization=on "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \ -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ -drive if=pflash,format=raw,file="${corrupt_work}/vars.fd" \ -drive if=none,id=esp,format=raw,file=fat:rw:"${corrupt_work}/esp" \