Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use core::ops::ControlFlow;
9
10use k23_riscv::scause::Exception;
11use kmem::VirtualAddress;
12
13use crate::arch::trap::Trap;
14use crate::mem::{PageFaultFlags, with_kernel_aspace};
15
16pub fn handle_page_fault(trap: Trap, tval: VirtualAddress) -> ControlFlow<()> {
17 let flags = match trap {
18 Trap::Exception(Exception::LoadPageFault) => PageFaultFlags::LOAD,
19 Trap::Exception(Exception::StorePageFault) => PageFaultFlags::STORE,
20 Trap::Exception(Exception::InstructionPageFault) => PageFaultFlags::INSTRUCTION,
21 _ => return ControlFlow::Continue(()),
22 };
23
24 // For now, use kernel address space for page faults
25 // WASM tests run in kernel context, so this should work for our current needs
26 // TODO: In the future, tasks should carry their own address space as metadata
27 with_kernel_aspace(|aspace| {
28 let mut aspace = aspace.lock();
29 if let Err(err) = aspace.page_fault(tval, flags) {
30 tracing::warn!("page fault handler couldn't correct fault {err}");
31 ControlFlow::Continue(())
32 } else {
33 tracing::trace!("page fault handler successfully corrected fault");
34 ControlFlow::Break(())
35 }
36 })
37}