| 19 | #include "sk_stdlib.h" |
| 20 | |
| 21 | VirtualMemory::VirtualMemory() { |
| 22 | // 分配根页表目录 |
| 23 | kernel_page_dir_ = aligned_alloc(cpu_io::virtual_memory::kPageSize, |
| 24 | cpu_io::virtual_memory::kPageSize); |
| 25 | assert(kernel_page_dir_ != nullptr && |
| 26 | "Failed to allocate kernel page directory"); |
| 27 | |
| 28 | // 清零页表目录 |
| 29 | std::memset(kernel_page_dir_, 0, cpu_io::virtual_memory::kPageSize); |
| 30 | |
| 31 | // 获取内核基本信息 |
| 32 | const auto& basic_info = BasicInfoSingleton::instance(); |
| 33 | |
| 34 | // 映射全部物理内存 |
| 35 | MapMMIO(basic_info.physical_memory_addr, basic_info.physical_memory_size) |
| 36 | .or_else([](auto&& err) -> Expected<void*> { |
| 37 | klog::Err("Failed to map kernel memory: {}", err.message()); |
| 38 | while (true) { |
| 39 | cpu_io::Pause(); |
| 40 | } |
| 41 | return {}; |
| 42 | }); |
| 43 | |
| 44 | klog::Info("Kernel memory mapped from {:#X} to {:#X}", |
| 45 | basic_info.physical_memory_addr, |
| 46 | basic_info.physical_memory_addr + basic_info.physical_memory_size); |
| 47 | } |
| 48 | |
| 49 | auto VirtualMemory::InitCurrentCore() const -> void { |
| 50 | cpu_io::virtual_memory::SetPageDirectory( |