Skip to content

Commit

Permalink
添加注释,优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed Apr 14, 2024
1 parent 46a23a1 commit 29cc1df
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 165 deletions.
44 changes: 36 additions & 8 deletions kernel/src/arch/x86_64/mm/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::{
arch::{
interrupt::{trap::X86PfErrorCode, TrapFrame},
mm::{MemoryManagementArch, X86_64MMArch},
CurrentIrqArch, MMArch,
CurrentIrqArch, MMArch, ProtectionKey,
},
exception::InterruptArch,
kerror,
mm::{
fault::{FaultFlags, PageFault, PageFaultHandler},
fault::{FaultFlags, PageFault, PageFaultHandler, PageFaultMessage},
ucontext::{AddressSpace, LockedVMA},
VirtAddr, VmFaultReason, VmFlags,
ProtectionKeyTrait, VirtAddr, VmFaultReason, VmFlags,
},
};

Expand All @@ -41,8 +41,7 @@ impl PageFault for X86_64PageFault {
if foreign | vma.is_foreign() {
return true;
}
let guard = vma.lock();
super::pkru::pkru_allows_pkey(guard.pkey(), write)
super::pkey::pkru_allows_pkey(ProtectionKey::vma_pkey(vma), write)
}
}

Expand Down Expand Up @@ -160,13 +159,35 @@ impl X86_64PageFault {
panic!()
}

/// 内核态缺页异常处理
/// ## 参数
///
/// - `regs`: 中断栈帧
/// - `error_code`: 错误标志
/// - `address`: 发生缺页异常的虚拟地址
pub fn do_kern_addr_fault(
_regs: &'static TrapFrame,
_error_code: X86PfErrorCode,
_address: VirtAddr,
error_code: X86PfErrorCode,
address: VirtAddr,
) {
panic!(
"do_kern_addr_fault has not yet been implemented,
fault address: {:#x},
error_code: {:#b},
pid: {}\n",
address.data(),
error_code,
crate::process::ProcessManager::current_pid().data()
);
//TODO https://code.dragonos.org.cn/xref/linux-6.6.21/arch/x86/mm/fault.c#do_kern_addr_fault
}

/// 用户态缺页异常处理
/// ## 参数
///
/// - `regs`: 中断栈帧
/// - `error_code`: 错误标志
/// - `address`: 发生缺页异常的虚拟地址
pub unsafe fn do_user_addr_fault(
regs: &'static TrapFrame,
error_code: X86PfErrorCode,
Expand Down Expand Up @@ -242,7 +263,14 @@ impl X86_64PageFault {
}
let mapper = &mut space_guard.user_mapper.utable;

fault = PageFaultHandler::handle_mm_fault(vma.clone(), mapper, address, flags, regs);
fault = PageFaultHandler::handle_mm_fault(
PageFaultMessage {
vma: vma.clone(),
address,
flags,
},
mapper,
);

if fault.contains(VmFaultReason::VM_FAULT_COMPLETED) {
return;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod barrier;
pub mod bump;
pub mod fault;
pub mod pkru;
pub mod pkey;

use alloc::vec::Vec;
use hashbrown::HashSet;
Expand Down
52 changes: 52 additions & 0 deletions kernel/src/arch/x86_64/mm/pkey.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use alloc::sync::Arc;

use crate::mm::{ucontext::LockedVMA, ProtectionKeyTrait};

pub struct X86_64ProtectionKey;

impl ProtectionKeyTrait for X86_64ProtectionKey {
const VM_PKEY_SHIFT: usize = 32;

/// X86_64架构的ProtectionKey使用32、33、34、35四个比特位
const PKEY_MASK: usize = 1 << 32 | 1 << 33 | 1 << 34 | 1 << 35;

fn vma_pkey(vma: Arc<LockedVMA>) -> u16 {
let guard = vma.lock();
((guard.vm_flags().bits() & Self::PKEY_MASK as u64) >> Self::VM_PKEY_SHIFT) as u16
}
}

// TODO pkru实现参考:https://code.dragonos.org.cn/xref/linux-6.6.21/arch/x86/include/asm/pkru.h

const PKRU_AD_BIT: u16 = 0x1;
const PKRU_WD_BIT: u16 = 0x2;
const PKRU_BITS_PER_PKEY: u32 = 2;

pub fn pkru_allows_pkey(pkey: u16, write: bool) -> bool {
let pkru = read_pkru();

if !pkru_allows_read(pkru, pkey) {
return false;
}
if write & !pkru_allows_write(pkru, pkey) {
return false;
}

true
}

pub fn pkru_allows_read(pkru: u32, pkey: u16) -> bool {
let pkru_pkey_bits: u32 = pkey as u32 * PKRU_BITS_PER_PKEY;
pkru & ((PKRU_AD_BIT as u32) << pkru_pkey_bits) > 0
}

pub fn pkru_allows_write(pkru: u32, pkey: u16) -> bool {
let pkru_pkey_bits: u32 = pkey as u32 * PKRU_BITS_PER_PKEY;
pkru & (((PKRU_AD_BIT | PKRU_WD_BIT) as u32) << pkru_pkey_bits) > 0
}

pub fn read_pkru() -> u32 {
// TODO 实现读取pkru逻辑
// https://code.dragonos.org.cn/xref/linux-6.6.21/arch/x86/include/asm/pkru.h?fi=read_pkru#read_pkru
0
}
30 changes: 0 additions & 30 deletions kernel/src/arch/x86_64/mm/pkru.rs

This file was deleted.

2 changes: 2 additions & 0 deletions kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ pub use crate::arch::smp::X86_64SMPArch as CurrentSMPArch;
pub use crate::arch::sched::X86_64SchedArch as CurrentSchedArch;

pub use crate::arch::mm::fault::X86_64PageFault as PageFaultArch;

pub use crate::arch::mm::pkey::X86_64ProtectionKey as ProtectionKey;
Loading

0 comments on commit 29cc1df

Please sign in to comment.