Skip to content

Commit

Permalink
feat: working on vmm
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Sep 11, 2024
1 parent 26478d9 commit 77d443e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
17 changes: 5 additions & 12 deletions src/kernel/include/memory/virtual_memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,17 @@ class VirtualMemoryManager {
~VirtualMemoryManager() = default;
/// @}

/**
* @brief 初始化
* @return true 成功
* @return false 失败
*/
bool init(void);

/**
* @brief 获取当前页目录
* @return pt_t 当前页目录
*/
pt_t get_pgd(void);
pt_t GetPageDirectory();

/**
* @brief 设置当前页目录
* @param _pgd 要设置的页目录
*/
void set_pgd(const pt_t _pgd);
void SetPageDirectory(const pt_t _pgd);

/**
* @brief 映射物理地址到虚拟地址
Expand All @@ -96,14 +89,14 @@ class VirtualMemoryManager {
* @param _pa 物理地址
* @param _flag 属性
*/
void mmap(const pt_t _pgd, uintptr_t _va, uintptr_t _pa, uint32_t _flag);
void Mmap(const pt_t _pgd, uintptr_t _va, uintptr_t _pa, uint32_t _flag);

/**
* @brief 取消映射
* @param _pgd 要操作的页目录
* @param _va 要取消映射的虚拟地址
*/
void unmmap(const pt_t _pgd, uintptr_t _va);
void Unmmap(const pt_t _pgd, uintptr_t _va);

/**
* @brief 获取映射的物理地址
Expand All @@ -113,7 +106,7 @@ class VirtualMemoryManager {
* @return true 已映射
* @return false 未映射
*/
bool get_mmap(const pt_t _pgd, uintptr_t _va, const void *_pa);
bool GetMmap(const pt_t _pgd, uintptr_t _va, const void *_pa);

private:
/// @todo
Expand Down
25 changes: 11 additions & 14 deletions src/kernel/virtual_memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,29 @@ VirtualMemoryManager::VirtualMemoryManager(uint32_t, uint8_t*) {
addr < kBasicInfo.GetInstance().kernel_addr + kKernelSpaceSize;
addr += PhysicalMemoryManager::kPageSize) {
// TODO: 区分代码/数据等段分别映射
mmap(pgd_kernel, addr, addr,
Mmap(pgd_kernel, addr, addr,
cpu::vmm::VMM_PAGE_READABLE | cpu::vmm::VMM_PAGE_WRITABLE |
cpu::vmm::VMM_PAGE_EXECUTABLE);
}
// 设置页目录
klog::Debug("set_pgd: 0x%X\n", pgd_kernel);
set_pgd(pgd_kernel);
klog::Debug("SetPageDirectory: 0x%X\n", pgd_kernel);
SetPageDirectory(pgd_kernel);
// 开启分页
cpu::vmm::EnablePage();
}

pt_t VirtualMemoryManager::get_pgd(void) {
pt_t VirtualMemoryManager::GetPageDirectory() {
return (pt_t)cpu::vmm::GetPageDirectory();
}

void VirtualMemoryManager::set_pgd(const pt_t _pgd) {
void VirtualMemoryManager::SetPageDirectory(const pt_t _pgd) {
// 设置页目录
cpu::vmm::SetPageDirectory((uint64_t)_pgd);
// 刷新缓存
cpu::vmm::FlushPage(0);
return;
}

void VirtualMemoryManager::mmap(const pt_t _pgd, uintptr_t _va, uintptr_t _pa,
void VirtualMemoryManager::Mmap(const pt_t _pgd, uintptr_t _va, uintptr_t _pa,
uint32_t _flag) {
pte_t* pte = find(_pgd, _va, true);
// 一般情况下不应该为空
Expand All @@ -79,31 +78,29 @@ void VirtualMemoryManager::mmap(const pt_t _pgd, uintptr_t _va, uintptr_t _pa,
// 刷新缓存
cpu::vmm::FlushPage(0);
}
return;
}

void VirtualMemoryManager::unmmap(const pt_t _pgd, uintptr_t _va) {
void VirtualMemoryManager::Unmmap(const pt_t _pgd, uintptr_t _va) {
pte_t* pte = find(_pgd, _va, false);
// 找到页表项
// 未找到
if (pte == nullptr) {
klog::Warn("VirtualMemoryManager::unmmap: find.\n");
klog::Warn("VirtualMemoryManager::Unmmap: find.\n");
return;
}
// 找到了,但是并没有被映射
if ((*pte & cpu::vmm::VMM_PAGE_VALID) == 0) {
klog::Warn("VirtualMemoryManager::unmmap: not mapped.\n");
klog::Warn("VirtualMemoryManager::Unmmap: not mapped.\n");
}
// 置零
*pte = 0x00;
// 刷新缓存
cpu::vmm::FlushPage(0);
// TODO: 如果一页表都被 unmap,释放占用的物理内存
return;
}

bool VirtualMemoryManager::get_mmap(const pt_t _pgd, uintptr_t _va,
const void* _pa) {
bool VirtualMemoryManager::GetMmap(const pt_t _pgd, uintptr_t _va,
const void* _pa) {
pte_t* pte = find(_pgd, _va, false);
bool res = false;
// pte 不为空且有效,说明映射了
Expand Down

0 comments on commit 77d443e

Please sign in to comment.