Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mm): 实现缺页中断处理 #715

Merged
merged 16 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kernel/crates/klog_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub enum AllocatorLogType {
Alloc(AllocLogItem),
AllocZeroed(AllocLogItem),
Free(AllocLogItem),
LazyAlloc(AllocLogItem),
}

#[repr(C)]
Expand Down
14 changes: 14 additions & 0 deletions kernel/src/arch/riscv64/mm/fault.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::mm::fault::PageFault;

pub struct RiscV64PageFault;

impl PageFault for RiscV64PageFault {
fn vma_access_permitted(
vma: alloc::sync::Arc<crate::mm::ucontext::LockedVMA>,
write: bool,
execute: bool,
foreign: bool,
) -> bool {
true
}
}
4 changes: 2 additions & 2 deletions kernel/src/arch/riscv64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
use self::init::riscv_mm_init;

pub mod bump;
pub mod fault;
pub(super) mod init;

pub type PageMapper = crate::mm::page::PageMapper<RiscV64MMArch, LockedFrameAllocator>;
Expand All @@ -44,8 +45,6 @@ pub(self) static INNER_ALLOCATOR: SpinLock<Option<BuddyAllocator<MMArch>>> = Spi
pub struct RiscV64MMArch;

impl RiscV64MMArch {
pub const ENTRY_FLAG_GLOBAL: usize = 1 << 5;

/// 使远程cpu的TLB中,指定地址范围的页失效
pub fn remote_invalidate_page(
cpu: ProcessorId,
Expand Down Expand Up @@ -121,6 +120,7 @@ impl MemoryManagementArch for RiscV64MMArch {
const ENTRY_FLAG_EXEC: usize = (1 << 3);
const ENTRY_FLAG_ACCESSED: usize = (1 << 6);
const ENTRY_FLAG_DIRTY: usize = (1 << 7);
const ENTRY_FLAG_GLOBAL: usize = (1 << 5);

const PHYS_OFFSET: usize = 0xffff_ffc0_0000_0000;
const KERNEL_LINK_OFFSET: usize = 0x1000000;
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pub use self::elf::RiscV64ElfArch as CurrentElfArch;
pub use crate::arch::smp::RiscV64SMPArch as CurrentSMPArch;

pub use crate::arch::sched::RiscV64SchedArch as CurrentSchedArch;

pub use self::mm::fault::RiscV64PageFault as PageFaultArch;
131 changes: 96 additions & 35 deletions kernel/src/arch/x86_64/interrupt/trap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use system_error::SystemError;

use crate::{
arch::CurrentIrqArch, exception::InterruptArch, kerror, kwarn, mm::VirtAddr, print,
process::ProcessManager, smp::core::smp_get_processor_id,
arch::{mm::fault::X86_64PageFault, CurrentIrqArch},
exception::InterruptArch,
kerror, kwarn,
mm::VirtAddr,
process::ProcessManager,
smp::core::smp_get_processor_id,
};

use super::{
Expand Down Expand Up @@ -33,6 +37,46 @@ extern "C" {
fn trap_virtualization_exception();
}

bitflags! {
pub struct TrapNr: u64 {
const X86_TRAP_DE = 0;
const X86_TRAP_DB = 1;
const X86_TRAP_NMI = 2;
const X86_TRAP_BP = 3;
const X86_TRAP_OF = 4;
const X86_TRAP_BR = 5;
const X86_TRAP_UD = 6;
const X86_TRAP_NM = 7;
const X86_TRAP_DF = 8;
const X86_TRAP_OLD_MF = 9;
const X86_TRAP_TS = 10;
const X86_TRAP_NP = 11;
const X86_TRAP_SS = 12;
const X86_TRAP_GP = 13;
const X86_TRAP_PF = 14;
const X86_TRAP_SPURIOUS = 15;
const X86_TRAP_MF = 16;
const X86_TRAP_AC = 17;
const X86_TRAP_MC = 18;
const X86_TRAP_XF = 19;
const X86_TRAP_VE = 20;
const X86_TRAP_CP = 21;
const X86_TRAP_VC = 29;
const X86_TRAP_IRET = 32;
}

pub struct X86PfErrorCode : u32{
const X86_PF_PROT = 1 << 0;
const X86_PF_WRITE = 1 << 1;
const X86_PF_USER = 1 << 2;
const X86_PF_RSVD = 1 << 3;
const X86_PF_INSTR = 1 << 4;
const X86_PF_PK = 1 << 5;
const X86_PF_SHSTK = 1 << 6;
const X86_PF_SGX = 1 << 15;
}
}

#[inline(never)]
pub fn arch_trap_init() -> Result<(), SystemError> {
unsafe {
Expand Down Expand Up @@ -319,42 +363,59 @@ Segment Selector Index: {:#x}\n
/// 处理页错误 14 #PF
#[no_mangle]
unsafe extern "C" fn do_page_fault(regs: &'static TrapFrame, error_code: u64) {
kerror!(
"do_page_fault(14), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}, \nFault Address: {:#x}",
error_code,
regs.rsp,
regs.rip,
smp_get_processor_id().data(),
ProcessManager::current_pid(),
x86::controlregs::cr2()
);

if (error_code & 0x01) == 0 {
print!("Page Not Present,\t");
}
if (error_code & 0x02) != 0 {
print!("Write Access,\t");
} else {
print!("Read Access,\t");
}

if (error_code & 0x04) != 0 {
print!("Fault in user(3),\t");
// kerror!(
// "do_page_fault(14), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}, \nFault Address: {:#x}",
// error_code,
// regs.rsp,
// regs.rip,
// smp_get_processor_id().data(),
// ProcessManager::current_pid(),
// x86::controlregs::cr2()
// );

// if (error_code & 0x01) == 0 {
// print!("Page Not Present,\t");
// }
// if (error_code & 0x02) != 0 {
// print!("Write Access,\t");
// } else {
// print!("Read Access,\t");
// }

// if (error_code & 0x04) != 0 {
// print!("Fault in user(3),\t");
// } else {
// print!("Fault in supervisor(0,1,2),\t");
// }

// if (error_code & 0x08) != 0 {
// print!("Reserved bit violation cause fault,\t");
// }

// if (error_code & 0x10) != 0 {
// print!("Instruction fetch cause fault,\t");
// }
// print!("\n");

// CurrentIrqArch::interrupt_enable();
// panic!("Page Fault");
CurrentIrqArch::interrupt_disable();
let address = x86::controlregs::cr2();
// crate::kinfo!(
// "fault address: {:#x}, error_code: {:#b}, pid: {}\n",
// address,
// error_code,
// ProcessManager::current_pid().data()
// );

let address = VirtAddr::new(address);
let error_code = X86PfErrorCode::from_bits_truncate(error_code as u32);
if address.check_user() {
X86_64PageFault::do_user_addr_fault(regs, error_code, address);
} else {
print!("Fault in supervisor(0,1,2),\t");
X86_64PageFault::do_kern_addr_fault(regs, error_code, address);
}

if (error_code & 0x08) != 0 {
print!("Reserved bit violation cause fault,\t");
}

if (error_code & 0x10) != 0 {
print!("Instruction fetch cause fault,\t");
}
print!("\n");

CurrentIrqArch::interrupt_enable();
panic!("Page Fault");
}

/// 处理x87 FPU错误 16 #MF
Expand Down
Loading
Loading