32 auto& vm = VirtualMemorySingleton::instance();
38 "virtual_memory_test: failed to create user page directory");
40 klog::Info(
"virtual_memory_test: user page directory created at {}",
44 void* virt_addr =
reinterpret_cast<void*
>(0x200000);
45 void* phys_addr =
reinterpret_cast<void*
>(0x90000000);
48 vm.MapPage(user_page_dir, virt_addr, phys_addr,
51 "virtual_memory_test: failed to map page");
52 klog::Info(
"virtual_memory_test: mapped va={} to pa={}", virt_addr,
56 auto mapped = vm.GetMapping(user_page_dir, virt_addr);
57 EXPECT_TRUE(mapped.has_value(),
"virtual_memory_test: failed to get mapping");
60 "virtual_memory_test: mapping address mismatch");
61 klog::Info(
"virtual_memory_test: verified mapping pa={}", *mapped);
65 constexpr size_t kNumPages = 5;
66 for (
size_t i = 0; i < kNumPages; ++i) {
67 void* va =
reinterpret_cast<void*
>(0x300000 +
69 void* pa =
reinterpret_cast<void*
>(0x91000000 +
72 auto result = vm.MapPage(user_page_dir, va, pa,
75 "virtual_memory_test: failed to map multiple pages");
77 klog::Info(
"virtual_memory_test: mapped {} pages", kNumPages);
80 for (
size_t i = 0; i < kNumPages; ++i) {
81 void* va =
reinterpret_cast<void*
>(0x300000 +
83 void* pa =
reinterpret_cast<void*
>(0x91000000 +
86 auto m = vm.GetMapping(user_page_dir, va);
88 "virtual_memory_test: multiple page mapping not found");
90 EXPECT_EQ(*m, pa,
"virtual_memory_test: multiple page mapping mismatch");
93 klog::Info(
"virtual_memory_test: verified {} page mappings", kNumPages);
96 auto unmap_result = vm.UnmapPage(user_page_dir, virt_addr);
98 "virtual_memory_test: failed to unmap page");
100 auto unmapped = vm.GetMapping(user_page_dir, virt_addr);
102 "virtual_memory_test: page still mapped after unmap");
103 klog::Info(
"virtual_memory_test: unmapped va={}", virt_addr);
106 auto clone_result = vm.ClonePageDirectory(user_page_dir,
true);
108 "virtual_memory_test: failed to clone page directory");
109 void* cloned_page_dir = clone_result.value();
111 "virtual_memory_test: cloned page dir same as source");
112 klog::Info(
"virtual_memory_test: cloned page directory to {}",
116 for (
size_t i = 0; i < kNumPages; ++i) {
117 void* va =
reinterpret_cast<void*
>(0x300000 +
119 void* pa =
reinterpret_cast<void*
>(0x91000000 +
122 auto src_m = vm.GetMapping(user_page_dir, va);
123 auto dst_m = vm.GetMapping(cloned_page_dir, va);
126 "virtual_memory_test: source mapping lost after clone");
128 "virtual_memory_test: cloned mapping not found");
130 if (src_m && dst_m) {
132 "virtual_memory_test: source mapping changed after clone");
133 EXPECT_EQ(*dst_m, pa,
"virtual_memory_test: cloned mapping incorrect");
135 "virtual_memory_test: source and clone mappings differ");
138 klog::Info(
"virtual_memory_test: verified cloned mappings");
141 auto clone_no_map_result = vm.ClonePageDirectory(user_page_dir,
false);
143 "virtual_memory_test: failed to clone page dir (no mappings)");
144 void* cloned_no_map = clone_no_map_result.value();
145 klog::Info(
"virtual_memory_test: cloned page directory (no mappings) to {}",
149 for (
size_t i = 0; i < kNumPages; ++i) {
150 void* va =
reinterpret_cast<void*
>(0x300000 +
153 auto m = vm.GetMapping(cloned_no_map, va);
155 "virtual_memory_test: cloned (no map) should have no mappings");
157 klog::Info(
"virtual_memory_test: verified no mappings in cloned page dir");
160 void* new_va =
reinterpret_cast<void*
>(0x400000);
161 void* new_pa =
reinterpret_cast<void*
>(0x92000000);
163 auto clone_map_result =
164 vm.MapPage(cloned_no_map, new_va, new_pa,
167 "virtual_memory_test: failed to map in cloned page dir");
170 auto user_m = vm.GetMapping(user_page_dir, new_va);
171 auto clone_m = vm.GetMapping(cloned_no_map, new_va);
174 "virtual_memory_test: mapping leaked to original page dir");
176 "virtual_memory_test: new mapping not in cloned page dir");
179 "virtual_memory_test: new mapping address incorrect");
181 klog::Info(
"virtual_memory_test: verified independent mappings");
184 vm.DestroyPageDirectory(user_page_dir,
false);
185 klog::Info(
"virtual_memory_test: destroyed user page directory");
187 vm.DestroyPageDirectory(cloned_page_dir,
false);
188 klog::Info(
"virtual_memory_test: destroyed cloned page directory");
190 vm.DestroyPageDirectory(cloned_no_map,
false);
191 klog::Info(
"virtual_memory_test: destroyed cloned (no map) page directory");
197 "virtual_memory_test: failed to create test page dir");
200 void* test_va =
reinterpret_cast<void*
>(0x500000);
201 void* test_pa1 =
reinterpret_cast<void*
>(0x93000000);
202 void* test_pa2 =
reinterpret_cast<void*
>(0x94000000);
205 (void)vm.MapPage(test_page_dir, test_va, test_pa1,
208 auto m1 = vm.GetMapping(test_page_dir, test_va);
209 EXPECT_TRUE(m1.has_value(),
"virtual_memory_test: first mapping failed");
211 EXPECT_EQ(*m1, test_pa1,
"virtual_memory_test: first mapping incorrect");
215 (void)vm.MapPage(test_page_dir, test_va, test_pa2,
218 auto m2 = vm.GetMapping(test_page_dir, test_va);
219 EXPECT_TRUE(m2.has_value(),
"virtual_memory_test: remap failed");
221 EXPECT_EQ(*m2, test_pa2,
"virtual_memory_test: remap address incorrect");
223 klog::Info(
"virtual_memory_test: verified remap from {} to {}", test_pa1,
227 vm.DestroyPageDirectory(test_page_dir,
false);
229 klog::Info(
"virtual_memory_test: all tests passed");