From 79ff6e94ecc75bc034639919ec94f818db21629e Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Mon, 14 Oct 2024 03:38:46 +0000 Subject: [PATCH] feat(vmm): vmm working for riscv64 Signed-off-by: Zone.N --- src/kernel/arch/riscv64/include/cpu/cpu.hpp | 8 +++-- src/kernel/arch/riscv64/include/cpu/regs.hpp | 33 ++------------------ 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/kernel/arch/riscv64/include/cpu/cpu.hpp b/src/kernel/arch/riscv64/include/cpu/cpu.hpp index f6d776b3..f2bb5130 100644 --- a/src/kernel/arch/riscv64/include/cpu/cpu.hpp +++ b/src/kernel/arch/riscv64/include/cpu/cpu.hpp @@ -91,13 +91,17 @@ inline void DisablePage() { * @return true 成功 * @return false 失败 */ -inline void SetPageDirectory(uint64_t pgd) { kAllCsr.satp.ppn.Write(pgd); } +inline void SetPageDirectory(uint64_t pgd) { + kAllCsr.satp.ppn.Write(pgd >> register_info::csr::SatpInfo::kPpnOffset); +} /** * @brief 获取页目录 * @return uint64_t 页目录值 */ -inline uint64_t GetPageDirectory() { return kAllCsr.satp.ppn.Get(); } +inline uint64_t GetPageDirectory() { + return kAllCsr.satp.ppn.Get() << register_info::csr::SatpInfo::kPpnOffset; +} inline void FlushPage(uint64_t addr) { (void)addr; diff --git a/src/kernel/arch/riscv64/include/cpu/regs.hpp b/src/kernel/arch/riscv64/include/cpu/regs.hpp index fe29ec56..46b2950e 100644 --- a/src/kernel/arch/riscv64/include/cpu/regs.hpp +++ b/src/kernel/arch/riscv64/include/cpu/regs.hpp @@ -38,37 +38,6 @@ */ namespace cpu { -// namespace vmm_info { -// /// 有效位 -// static constexpr const uint8_t VMM_PAGE_VALID = CPU::pte_t::VALID; -// /// 可读位 -// static constexpr const uint8_t VMM_PAGE_READABLE = CPU::pte_t::READ; -// /// 可写位s -// static constexpr const uint8_t VMM_PAGE_WRITABLE = CPU::pte_t::WRITE; -// /// 可执行位 -// static constexpr const uint8_t VMM_PAGE_EXECUTABLE = CPU::pte_t::EXEC; -// /// 用户位 -// static constexpr const uint8_t VMM_PAGE_USER = CPU::pte_t::USER; -// /// 全局位,我们不会使用 -// static constexpr const uint8_t VMM_PAGE_GLOBAL = CPU::pte_t::GLOBAL; -// /// 已使用位,用于替换算法 -// static constexpr const uint8_t VMM_PAGE_ACCESSED = CPU::pte_t::ACCESSED; -// /// 已修改位,用于替换算法 -// static constexpr const uint8_t VMM_PAGE_DIRTY = CPU::pte_t::DIRTY; -// /// 内核虚拟地址相对物理地址的偏移 -// static constexpr const size_t KERNEL_OFFSET = 0x0; -// /// PTE 属性位数 -// static constexpr const size_t VMM_PTE_PROP_BITS = 10; -// /// PTE 页内偏移位数 -// static constexpr const size_t VMM_PAGE_OFF_BITS = 12; -// /// VPN 位数 -// static constexpr const size_t VMM_VPN_BITS = 9; -// /// VPN 位数掩码,9 位 VPN -// static constexpr const size_t VMM_VPN_BITS_MASK = 0x1FF; -// /// riscv64 使用了三级页表 -// static constexpr const size_t VMM_PT_LEVEL = 3; -// }; // namespace vmm_info - // 第一部分:寄存器定义 namespace register_info { @@ -378,6 +347,8 @@ struct SatpInfo : public RegInfoBase { "Reserved", "Reserved", "SV39", "SV48", "SV57", "SV64", }; + static constexpr const uint64_t kPpnOffset = 12; + struct Ppn { using DataType = uint64_t; static constexpr uint64_t kBitOffset = 0;