MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / virtual_memory_test

Function virtual_memory_test

tests/system_test/virtual_memory_test.cpp:29–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27// VirtualMemorySingleton is defined in virtual_memory.hpp
28
29auto virtual_memory_test() -> bool {
30 klog::Info("virtual_memory_test: start");
31
32 auto& vm = VirtualMemorySingleton::instance();
33
34 // Test 1: 创建用户页表
35 void* user_page_dir = aligned_alloc(cpu_io::virtual_memory::kPageSize,
36 cpu_io::virtual_memory::kPageSize);
37 EXPECT_TRUE(user_page_dir != nullptr,
38 "virtual_memory_test: failed to create user page directory");
39 memset(user_page_dir, 0, cpu_io::virtual_memory::kPageSize);
40 klog::Info("virtual_memory_test: user page directory created at {}",
41 user_page_dir);
42
43 // Test 2: 映射页面
44 void* virt_addr = reinterpret_cast<void*>(0x200000);
45 void* phys_addr = reinterpret_cast<void*>(0x90000000);
46
47 auto map_result =
48 vm.MapPage(user_page_dir, virt_addr, phys_addr,
49 cpu_io::virtual_memory::GetUserPagePermissions());
50 EXPECT_TRUE(map_result.has_value(),
51 "virtual_memory_test: failed to map page");
52 klog::Info("virtual_memory_test: mapped va={} to pa={}", virt_addr,
53 phys_addr);
54
55 // Test 3: 获取映射
56 auto mapped = vm.GetMapping(user_page_dir, virt_addr);
57 EXPECT_TRUE(mapped.has_value(), "virtual_memory_test: failed to get mapping");
58 if (mapped) {
59 EXPECT_EQ(*mapped, phys_addr,
60 "virtual_memory_test: mapping address mismatch");
61 klog::Info("virtual_memory_test: verified mapping pa={}", *mapped);
62 }
63
64 // Test 4: 映射多个页面
65 constexpr size_t kNumPages = 5;
66 for (size_t i = 0; i < kNumPages; ++i) {
67 void* va = reinterpret_cast<void*>(0x300000 +
68 i * cpu_io::virtual_memory::kPageSize);
69 void* pa = reinterpret_cast<void*>(0x91000000 +
70 i * cpu_io::virtual_memory::kPageSize);
71
72 auto result = vm.MapPage(user_page_dir, va, pa,
73 cpu_io::virtual_memory::GetUserPagePermissions());
74 EXPECT_TRUE(result.has_value(),
75 "virtual_memory_test: failed to map multiple pages");
76 }
77 klog::Info("virtual_memory_test: mapped {} pages", kNumPages);
78
79 // Test 5: 验证多页映射
80 for (size_t i = 0; i < kNumPages; ++i) {
81 void* va = reinterpret_cast<void*>(0x300000 +
82 i * cpu_io::virtual_memory::kPageSize);
83 void* pa = reinterpret_cast<void*>(0x91000000 +
84 i * cpu_io::virtual_memory::kPageSize);
85
86 auto m = vm.GetMapping(user_page_dir, va);

Callers

nothing calls this directly

Calls 9

InfoFunction · 0.85
memsetFunction · 0.85
GetUserPagePermissionsFunction · 0.85
MapPageMethod · 0.80
GetMappingMethod · 0.80
UnmapPageMethod · 0.80
ClonePageDirectoryMethod · 0.80
DestroyPageDirectoryMethod · 0.80
aligned_allocFunction · 0.50

Tested by

no test coverage detected