|
7 | 7 |
|
8 | 8 | use core::ops::ControlFlow; |
9 | 9 |
|
10 | | -use crate::arch::trap::Trap; |
11 | | -use crate::mem::VirtualAddress; |
12 | | -use crate::state::global; |
| 10 | +use riscv::scause::Exception; |
13 | 11 |
|
14 | | -pub fn handle_page_fault(_trap: Trap, _tval: VirtualAddress) -> ControlFlow<()> { |
15 | | - let current_task = global() |
16 | | - .executor |
17 | | - .current_scheduler() |
18 | | - .unwrap() |
19 | | - .current_task(); |
| 12 | +use crate::arch::trap::Trap; |
| 13 | +use crate::mem::{PageFaultFlags, VirtualAddress, with_kernel_aspace}; |
20 | 14 |
|
21 | | - let Some(_current_task) = current_task.as_ref() else { |
22 | | - // if we're not inside a task we're inside some critical kernel code |
23 | | - // none of that should use ever trap |
24 | | - tracing::warn!("no currently active task"); |
25 | | - return ControlFlow::Continue(()); |
| 15 | +pub fn handle_page_fault(trap: Trap, tval: VirtualAddress) -> ControlFlow<()> { |
| 16 | + let flags = match trap { |
| 17 | + Trap::Exception(Exception::LoadPageFault) => PageFaultFlags::LOAD, |
| 18 | + Trap::Exception(Exception::StorePageFault) => PageFaultFlags::STORE, |
| 19 | + Trap::Exception(Exception::InstructionPageFault) => PageFaultFlags::INSTRUCTION, |
| 20 | + _ => return ControlFlow::Continue(()), |
26 | 21 | }; |
27 | 22 |
|
28 | | - // FIXME we have no page fault handling right now |
29 | | - ControlFlow::Continue(()) |
30 | | - |
31 | | - // let mut aspace = current_task.header().aspace.as_ref().unwrap().lock(); |
32 | | - |
33 | | - // let flags = match trap { |
34 | | - // Trap::Exception(Exception::LoadPageFault) => PageFaultFlags::LOAD, |
35 | | - // Trap::Exception(Exception::StorePageFault) => PageFaultFlags::STORE, |
36 | | - // Trap::Exception(Exception::InstructionPageFault) => PageFaultFlags::INSTRUCTION, |
37 | | - // // not a page fault exception, continue with the next fault handler |
38 | | - // _ => return ControlFlow::Continue(()), |
39 | | - // }; |
40 | | - // |
41 | | - // if let Err(err) = aspace.page_fault(tval, flags) { |
42 | | - // // the address space knew about the faulting address, but the requested access was invalid |
43 | | - // tracing::warn!("page fault handler couldn't correct fault {err}"); |
44 | | - // ControlFlow::Continue(()) |
45 | | - // } else { |
46 | | - // // the address space knew about the faulting address and could correct the fault |
47 | | - // tracing::trace!("page fault handler successfully corrected fault"); |
48 | | - // ControlFlow::Break(()) |
49 | | - // } |
| 23 | + // For now, use kernel address space for page faults |
| 24 | + // WASM tests run in kernel context, so this should work for our current needs |
| 25 | + // TODO: In the future, tasks should carry their own address space as metadata |
| 26 | + with_kernel_aspace(|aspace| { |
| 27 | + let mut aspace = aspace.lock(); |
| 28 | + if let Err(err) = aspace.page_fault(tval, flags) { |
| 29 | + tracing::warn!("page fault handler couldn't correct fault {err}"); |
| 30 | + ControlFlow::Continue(()) |
| 31 | + } else { |
| 32 | + tracing::trace!("page fault handler successfully corrected fault"); |
| 33 | + ControlFlow::Break(()) |
| 34 | + } |
| 35 | + }) |
50 | 36 | } |
0 commit comments