| 208 | } |
| 209 | |
| 210 | auto VirtualMemory::RecursiveFreePageTable(uint64_t* table, size_t level, |
| 211 | bool free_pages) -> void { |
| 212 | if (table == nullptr) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | // 遍历页表中的所有条目 |
| 217 | for (size_t i = 0; i < kEntriesPerTable; ++i) { |
| 218 | uint64_t pte = table[i]; |
| 219 | if (!cpu_io::virtual_memory::IsPageTableEntryValid(pte)) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | auto pa = cpu_io::virtual_memory::PageTableEntryToPhysical(pte); |
| 224 | |
| 225 | // 如果不是最后一级,递归释放子页表 |
| 226 | if (level > 0) { |
| 227 | RecursiveFreePageTable(reinterpret_cast<uint64_t*>(pa), level - 1, |
| 228 | free_pages); |
| 229 | } else if (free_pages) { |
| 230 | // 最后一级页表,释放物理页 |
| 231 | aligned_free(reinterpret_cast<void*>(pa)); |
| 232 | } |
| 233 | |
| 234 | // 清除页表项 |
| 235 | table[i] = 0; |
| 236 | } |
| 237 | |
| 238 | // 如果不是根页表,释放当前页表 |
| 239 | if (level < cpu_io::virtual_memory::kPageTableLevels - 1) { |
| 240 | aligned_free(table); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | auto VirtualMemory::RecursiveClonePageTable(uint64_t* src_table, |
| 245 | uint64_t* dst_table, size_t level, |
nothing calls this directly
no test coverage detected