Debian Codegraph @

Source/UnitTests/Core/PowerPC/PageTableHostMappingTest.cpp

raw symbols SCIP
1 // Copyright 2026 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <bit>
5 #include <set>
6 #include <utility>
7
8 #include <fmt/format.h>
9
10 #include "Common/Align.h"
11 #include "Common/CommonTypes.h"
12 #include "Common/Swap.h"
13 #include "Core/Core.h"
14 #include "Core/MemTools.h"
15 #include "Core/PowerPC/BreakPoints.h"
16 #include "Core/PowerPC/Gekko.h"
17 #include "Core/PowerPC/JitInterface.h"
18 #include "Core/PowerPC/MMU.h"
19 #include "Core/PowerPC/PowerPC.h"
20 #include "Core/System.h"
21
22 #include "../StubJit.h"
23
24 #include <gtest/gtest.h>
25
26 // All guest addresses used in this unit test are arbitrary, aside from alignment requirements
27 static constexpr u32 ALIGNED_PAGE_TABLE_BASE = 0x00020000;
28 static constexpr u32 ALIGNED_PAGE_TABLE_MASK_SMALL = 0x0000ffff;
29 static constexpr u32 ALIGNED_PAGE_TABLE_MASK_LARGE = 0x0001ffff;
30
31 static constexpr u32 MISALIGNED_PAGE_TABLE_BASE = 0x00050000;
32 static constexpr u32 MISALIGNED_PAGE_TABLE_BASE_ALIGNED = 0x00040000;
33 static constexpr u32 MISALIGNED_PAGE_TABLE_MASK = 0x0003ffff;
34
35 static constexpr u32 HOLE_MASK_PAGE_TABLE_BASE = 0x00080000;
36 static constexpr u32 HOLE_MASK_PAGE_TABLE_MASK = 0x0002ffff;
37 static constexpr u32 HOLE_MASK_PAGE_TABLE_MASK_WITHOUT_HOLE = 0x0003ffff;
38
39 static constexpr u32 MISALIGNED_HOLE_MASK_PAGE_TABLE_BASE = 0x000d0000;
40 static constexpr u32 MISALIGNED_HOLE_MASK_PAGE_TABLE_BASE_ALIGNED = 0x000c0000;
41 static constexpr u32 MISALIGNED_HOLE_MASK_PAGE_TABLE_MASK = 0x0002ffff;
42 static constexpr u32 MISALIGNED_HOLE_MASK_PAGE_TABLE_MASK_WITHOUT_HOLE = 0x0003ffff;
43
44 static constexpr u32 TEMPORARY_MEMORY = 0x00000000;
45 static u32 s_current_temporary_memory = TEMPORARY_MEMORY;
46
47 // This is the max that the unit test can handle, not the max that Core can handle
48 static constexpr u32 MAX_HOST_PAGE_SIZE = 64 * 1024;
49 static u32 s_minimum_mapping_size = 0;
50
51 static volatile const void* volatile s_detection_address = nullptr;
52 static volatile size_t s_detection_count = 0;
53 static u32 s_counter = 0;
54 static std::set<u32> s_temporary_mappings;
55
56 class PageFaultDetector : public StubJit
57 {
58 public:
59 explicit PageFaultDetector(Core::System& system) : StubJit(system), m_block_cache(*this) {}
60
61 bool HandleFault(uintptr_t access_address, SContext* ctx) override
62 {
63 if (access_address != reinterpret_cast<uintptr_t>(s_detection_address))
64 {
65 std::string logical_address;
66 auto& memory = Core::System::GetInstance().GetMemory();
67 auto logical_base = reinterpret_cast<uintptr_t>(memory.GetLogicalBase());
68 if (access_address >= logical_base && access_address < logical_base + 0x1'0000'0000)
69 logical_address = fmt::format(" (PPC {:#010x})", access_address - logical_base);
70
71 ADD_FAILURE() << fmt::format("Unexpected segfault at {:#x}{}", access_address,
72 logical_address);
73
74 return false;
75 }
76
77 s_detection_address = nullptr;
78 s_detection_count = s_detection_count + 1;
79
80 // After we return from the signal handler, the memory access will happen again.
81 // Let it succeed this time so the signal handler won't get called over and over.
82 auto& memory = Core::System::GetInstance().GetMemory();
83 const uintptr_t logical_base = reinterpret_cast<uintptr_t>(memory.GetLogicalBase());
84 const u32 logical_address = static_cast<u32>(access_address - logical_base);
85 const u32 mask = s_minimum_mapping_size - 1;
86 for (u32 i = logical_address & mask; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
87 {
88 const u32 current_logical_address = (logical_address & ~mask) + i;
89 memory.AddPageTableMapping(current_logical_address, s_current_temporary_memory + i, true);
90 s_temporary_mappings.emplace(current_logical_address);
91 }
92 return true;
93 }
94
95 bool WantsPageTableMappings() const override { return true; }
96
97 // PowerPC::MMU::DBATUpdated wants to clear blocks in the block cache,
98 // so this can't just return nullptr
99 JitBaseBlockCache* GetBlockCache() override { return &m_block_cache; }
100
101 private:
102 StubBlockCache m_block_cache;
103 };
104
105 // This is used as a performance optimization. If several page table updates are performed while
106 // DR is disabled, MMU.cpp will only have to rescan the page table one time once DR is enabled again
107 // instead of after each page table update.
108 class DisableDR final
109 {
110 public:
111 DisableDR()
112 {
113 auto& system = Core::System::GetInstance();
114 system.GetPPCState().msr.DR = 0;
115 system.GetPowerPC().MSRUpdated();
116 }
117
118 ~DisableDR()
119 {
120 auto& system = Core::System::GetInstance();
121 system.GetPPCState().msr.DR = 1;
122 system.GetPowerPC().MSRUpdated();
123 }
124 };
125
126 class PageTableHostMappingTest : public ::testing::Test
127 {
128 public:
129 static void SetUpTestSuite()
130 {
131 if (!EMM::IsExceptionHandlerSupported())
132 GTEST_SKIP() << "Skipping PageTableHostMappingTest because exception handler is unsupported.";
133
134 auto& system = Core::System::GetInstance();
135 auto& memory = system.GetMemory();
136 const u32 host_page_size = memory.GetHostPageSize();
137 s_minimum_mapping_size = std::max<u32>(host_page_size, PowerPC::HW_PAGE_SIZE);
138
139 if (!std::has_single_bit(host_page_size) || host_page_size > MAX_HOST_PAGE_SIZE)
140 {
141 GTEST_SKIP() << fmt::format(
142 "Skipping PageTableHostMappingTest because page size {} is unsupported.", host_page_size);
143 }
144
145 memory.Init();
146 if (!memory.InitFastmemArena())
147 {
148 memory.Shutdown();
149 GTEST_SKIP() << "Skipping PageTableHostMappingTest because InitFastmemArena failed.";
150 }
151
152 Core::DeclareAsCPUThread();
153 EMM::InstallExceptionHandler();
154 system.GetJitInterface().SetJit(std::make_unique<PageFaultDetector>(system));
155
156 // Make sure BATs and SRs are cleared
157 auto& power_pc = system.GetPowerPC();
158 power_pc.Reset();
159
160 // Set up an SR
161 SetSR(1, 123);
162
163 // Specify a page table
164 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_SMALL);
165
166 // Enable address translation
167 system.GetPPCState().msr.DR = 1;
168 system.GetPPCState().msr.IR = 1;
169 power_pc.MSRUpdated();
170 }
171
172 static void TearDownTestSuite()
173 {
174 auto& system = Core::System::GetInstance();
175
176 system.GetJitInterface().SetJit(nullptr);
177 EMM::UninstallExceptionHandler();
178 Core::UndeclareAsCPUThread();
179 system.GetMemory().Shutdown();
180 }
181
182 static void SetSR(size_t index, u32 vsid)
183 {
184 ASSERT_FALSE(index == 4 || index == 7)
185 << fmt::format("sr{} has conflicts with fake VMEM mapping", index);
186
187 UReg_SR sr{};
188 sr.VSID = vsid;
189
190 auto& system = Core::System::GetInstance();
191 system.GetPPCState().sr[index] = sr.Hex;
192 system.GetMMU().SRUpdated();
193 }
194
195 static void SetSDR(u32 page_table_base, u32 page_table_mask)
196 {
197 UReg_SDR1 sdr;
198 sdr.htabmask = page_table_mask >> 16;
199 sdr.reserved = 0;
200 sdr.htaborg = page_table_base >> 16;
201
202 auto& system = Core::System::GetInstance();
203 system.GetPPCState().spr[SPR_SDR] = sdr.Hex;
204 system.GetMMU().SDRUpdated();
205 }
206
207 static void SetBAT(u32 spr, UReg_BAT_Up batu, UReg_BAT_Lo batl)
208 {
209 auto& system = Core::System::GetInstance();
210 auto& ppc_state = system.GetPPCState();
211 auto& mmu = system.GetMMU();
212
213 ppc_state.spr[spr + 1] = batl.Hex;
214 ppc_state.spr[spr] = batu.Hex;
215
216 if ((spr >= SPR_IBAT0U && spr <= SPR_IBAT3L) || (spr >= SPR_IBAT4U && spr <= SPR_IBAT7L))
217 mmu.IBATUpdated();
218 if ((spr >= SPR_DBAT0U && spr <= SPR_DBAT3L) || (spr >= SPR_DBAT4U && spr <= SPR_DBAT7L))
219 mmu.DBATUpdated();
220 }
221
222 static void SetBAT(u32 spr, u32 logical_address, u32 physical_address, u32 size)
223 {
224 UReg_BAT_Up batu{};
225 batu.VP = 1;
226 batu.VS = 1;
227 batu.BL = (size - 1) >> PowerPC::BAT_INDEX_SHIFT;
228 batu.BEPI = logical_address >> PowerPC::BAT_INDEX_SHIFT;
229
230 UReg_BAT_Lo batl{};
231 batl.PP = 2;
232 batl.WIMG = 0;
233 batl.BRPN = physical_address >> PowerPC::BAT_INDEX_SHIFT;
234
235 SetBAT(spr, batu, batl);
236 }
237
238 static void ExpectMapped(u32 logical_address, u32 physical_address)
239 {
240 SCOPED_TRACE(
241 fmt::format("ExpectMapped({:#010x}, {:#010x})", logical_address, physical_address));
242
243 auto& memory = Core::System::GetInstance().GetMemory();
244 u8* physical_base = memory.GetPhysicalBase();
245 u8* logical_base = memory.GetLogicalBase();
246
247 auto* physical_ptr = reinterpret_cast<volatile u32*>(physical_base + physical_address);
248 auto* logical_ptr = reinterpret_cast<volatile u32*>(logical_base + logical_address);
249
250 *physical_ptr = ++s_counter;
251 EXPECT_EQ(*logical_ptr, s_counter)
252 << "Page was mapped to a different physical page than expected";
253
254 *logical_ptr = ++s_counter;
255 EXPECT_EQ(*physical_ptr, s_counter)
256 << "Page was mapped to a different physical page than expected";
257 }
258
259 #ifdef _MSC_VER
260 #define ASAN_DISABLE __declspec(no_sanitize_address)
261 #else
262 #define ASAN_DISABLE
263 #endif
264
265 static void ASAN_DISABLE ExpectReadOnlyMapped(u32 logical_address, u32 physical_address)
266 {
267 SCOPED_TRACE(
268 fmt::format("ExpectReadOnlyMapped({:#010x}, {:#010x})", logical_address, physical_address));
269
270 auto& memory = Core::System::GetInstance().GetMemory();
271 u8* physical_base = memory.GetPhysicalBase();
272 u8* logical_base = memory.GetLogicalBase();
273
274 auto* physical_ptr = reinterpret_cast<volatile u32*>(physical_base + physical_address);
275 auto* logical_ptr = reinterpret_cast<volatile u32*>(logical_base + logical_address);
276
277 *physical_ptr = ++s_counter;
278 EXPECT_EQ(*logical_ptr, s_counter)
279 << "Page was mapped to a different physical page than expected";
280
281 s_detection_address = logical_ptr;
282 s_detection_count = 0;
283
284 // This line should fault
285 *logical_ptr = ++s_counter;
286
287 memory.RemovePageTableMappings(s_temporary_mappings);
288 s_temporary_mappings.clear();
289 EXPECT_EQ(s_detection_count, u32(1)) << "Page was mapped as writeable, against expectations";
290 }
291
292 static void ASAN_DISABLE ExpectNotMapped(u32 logical_address)
293 {
294 SCOPED_TRACE(fmt::format("ExpectNotMapped({:#010x})", logical_address));
295
296 auto& memory = Core::System::GetInstance().GetMemory();
297 u8* logical_base = memory.GetLogicalBase();
298
299 auto* logical_ptr = reinterpret_cast<volatile u32*>(logical_base + logical_address);
300 s_detection_address = logical_ptr;
301 s_detection_count = 0;
302
303 // This line should fault
304 *logical_ptr;
305
306 memory.RemovePageTableMappings(s_temporary_mappings);
307 s_temporary_mappings.clear();
308 EXPECT_EQ(s_detection_count, u32(1)) << "Page was mapped, against expectations";
309 }
310
311 static void ExpectMappedOnlyIf4KHostPages(u32 logical_address, u32 physical_address)
312 {
313 if (s_minimum_mapping_size > PowerPC::HW_PAGE_SIZE)
314 ExpectNotMapped(logical_address);
315 else
316 ExpectMapped(logical_address, physical_address);
317 }
318
319 static std::pair<UPTE_Lo, UPTE_Hi> GetPTE(u32 logical_address, u32 index)
320 {
321 auto& system = Core::System::GetInstance();
322 auto& ppc_state = system.GetPPCState();
323
324 const UReg_SR sr(system.GetPPCState().sr[logical_address >> 28]);
325 u32 hash = sr.VSID ^ (logical_address >> 12);
326 if ((index & 0x8) != 0)
327 hash = ~hash;
328
329 const u32 pteg_addr = ((hash << 6) & ppc_state.pagetable_mask) | ppc_state.pagetable_base;
330 const u32 pte_addr = (index & 0x7) * 8 + pteg_addr;
331
332 const u8* physical_base = system.GetMemory().GetPhysicalBase();
333 const UPTE_Lo pte1(Common::swap32(physical_base + pte_addr));
334 const UPTE_Hi pte2(Common::swap32(physical_base + pte_addr + 4));
335
336 return {pte1, pte2};
337 }
338
339 static void SetPTE(UPTE_Lo pte1, UPTE_Hi pte2, u32 logical_address, u32 index)
340 {
341 auto& system = Core::System::GetInstance();
342 auto& ppc_state = system.GetPPCState();
343
344 pte1.H = (index & 0x8) != 0;
345
346 u32 hash = pte1.VSID ^ (logical_address >> 12);
347 if (pte1.H)
348 hash = ~hash;
349
350 const u32 pteg_addr = ((hash << 6) & ppc_state.pagetable_mask) | ppc_state.pagetable_base;
351 const u32 pte_addr = (index & 0x7) * 8 + pteg_addr;
352
353 u8* physical_base = system.GetMemory().GetPhysicalBase();
354 Common::WriteSwap32(physical_base + pte_addr, pte1.Hex);
355 Common::WriteSwap32(physical_base + pte_addr + 4, pte2.Hex);
356
357 system.GetMMU().InvalidateTLBEntry(logical_address);
358 }
359
360 static std::pair<UPTE_Lo, UPTE_Hi> CreateMapping(u32 logical_address, u32 physical_address)
361 {
362 auto& ppc_state = Core::System::GetInstance().GetPPCState();
363
364 UPTE_Lo pte1{};
365 pte1.API = logical_address >> 22;
366 pte1.VSID = UReg_SR{ppc_state.sr[logical_address >> 28]}.VSID;
367 pte1.V = 1; // Mapping is valid
368
369 UPTE_Hi pte2{};
370 pte2.C = 1; // Page has been written to (MMU.cpp won't map as writeable without this)
371 pte2.R = 1; // Page has been read from (MMU.cpp won't map at all without this)
372 pte2.RPN = physical_address >> 12;
373
374 return {pte1, pte2};
375 }
376
377 static void AddMapping(u32 logical_address, u32 physical_address, u32 index)
378 {
379 auto [pte1, pte2] = CreateMapping(logical_address, physical_address);
380 SetPTE(pte1, pte2, logical_address, index);
381 }
382
383 static void AddHostSizedMapping(u32 logical_address, u32 physical_address, u32 index)
384 {
385 DisableDR disable_dr;
386 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
387 AddMapping(logical_address + i, physical_address + i, index);
388 }
389
390 static void RemoveMapping(u32 logical_address, u32 physical_address, u32 index)
391 {
392 auto [pte1, pte2] = CreateMapping(logical_address, physical_address);
393 pte1.V = 0; // Mapping is invalid
394 SetPTE(pte1, pte2, logical_address, index);
395 }
396
397 static void RemoveHostSizedMapping(u32 logical_address, u32 physical_address, u32 index)
398 {
399 DisableDR disable_dr;
400 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
401 RemoveMapping(logical_address + i, physical_address + i, index);
402 }
403 };
404
405 TEST_F(PageTableHostMappingTest, Basic)
406 {
407 s_current_temporary_memory = 0x00100000;
408
409 // Create a basic mapping
410 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
411 {
412 ExpectNotMapped(0x10100000 + i);
413 AddMapping(0x10100000 + i, 0x00100000 + i, 0);
414 }
415 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
416 ExpectMapped(0x10100000 + i, 0x00100000 + i);
417
418 // Create another mapping pointing to the same physical address
419 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
420 {
421 ExpectNotMapped(0x10100000 + s_minimum_mapping_size + i);
422 AddMapping(0x10100000 + s_minimum_mapping_size + i, 0x00100000 + i, 0);
423 }
424 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
425 {
426 ExpectMapped(0x10100000 + i, 0x00100000 + i);
427 ExpectMapped(0x10100000 + s_minimum_mapping_size + i, 0x00100000 + i);
428 }
429
430 // Remove the first page
431 RemoveMapping(0x10100000, 0x00100000, 0);
432 ExpectNotMapped(0x10100000);
433 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
434 ExpectMapped(0x10100000 + s_minimum_mapping_size + i, 0x00100000 + i);
435
436 s_current_temporary_memory = TEMPORARY_MEMORY;
437 }
438
439 TEST_F(PageTableHostMappingTest, LargeHostPageMismatchedAddresses)
440 {
441 {
442 DisableDR disable_dr;
443 AddMapping(0x10110000, 0x00111000, 0);
444 for (u32 i = PowerPC::HW_PAGE_SIZE; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
445 AddMapping(0x10110000 + i, 0x00110000 + i, 0);
446 }
447
448 ExpectMappedOnlyIf4KHostPages(0x10110000, 0x00111000);
449 }
450
451 TEST_F(PageTableHostMappingTest, LargeHostPageMisalignedAddresses)
452 {
453 {
454 DisableDR disable_dr;
455 for (u32 i = 0; i < s_minimum_mapping_size * 2; i += PowerPC::HW_PAGE_SIZE)
456 AddMapping(0x10120000 + i, 0x00121000 + i, 0);
457 }
458
459 ExpectMappedOnlyIf4KHostPages(0x10120000, 0x00121000);
460 ExpectMappedOnlyIf4KHostPages(0x10120000 + s_minimum_mapping_size,
461 0x00121000 + s_minimum_mapping_size);
462 }
463
464 TEST_F(PageTableHostMappingTest, ChangeSR)
465 {
466 {
467 DisableDR disable_dr;
468 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
469 {
470 auto [pte1, pte2] = CreateMapping(0x20130000 + i, 0x00130000 + i);
471 pte1.VSID = 0xabc;
472 SetPTE(pte1, pte2, 0x20130000 + i, 0);
473 }
474 }
475 ExpectNotMapped(0x20130000);
476
477 SetSR(2, 0xabc);
478 ExpectMapped(0x20130000, 0x00130000);
479 ExpectNotMapped(0x30130000);
480
481 SetSR(3, 0xabc);
482 ExpectMapped(0x20130000, 0x00130000);
483 ExpectMapped(0x30130000, 0x00130000);
484 ExpectNotMapped(0x00130000);
485 ExpectNotMapped(0x10130000);
486 }
487
488 // DBAT takes priority over page table mappings.
489 TEST_F(PageTableHostMappingTest, DBATPriority)
490 {
491 SetSR(5, 5);
492
493 AddHostSizedMapping(0x50140000, 0x00150000, 0);
494 ExpectMapped(0x50140000, 0x00150000);
495
496 SetBAT(SPR_DBAT0U, 0x50000000, 0x00000000, 0x01000000);
497 ExpectMapped(0x50140000, 0x00140000);
498 }
499
500 // Host-side page table mappings are for data only, so IBAT has no effect on them.
501 TEST_F(PageTableHostMappingTest, IBATPriority)
502 {
503 SetSR(6, 6);
504
505 AddHostSizedMapping(0x60160000, 0x00170000, 0);
506 ExpectMapped(0x60160000, 0x00170000);
507
508 SetBAT(SPR_IBAT0U, 0x60000000, 0x00000000, 0x01000000);
509 ExpectMapped(0x60160000, 0x00170000);
510 }
511
512 TEST_F(PageTableHostMappingTest, Priority)
513 {
514 // Secondary PTEs for 0x10180000
515
516 AddHostSizedMapping(0x10180000, 0x00180000, 10);
517 ExpectMapped(0x10180000, 0x00180000);
518
519 AddHostSizedMapping(0x10180000, 0x00190000, 12);
520 ExpectMapped(0x10180000, 0x00180000);
521
522 AddHostSizedMapping(0x10180000, 0x001a0000, 8);
523 ExpectMapped(0x10180000, 0x001a0000);
524
525 RemoveHostSizedMapping(0x10180000, 0x00180000, 10);
526 ExpectMapped(0x10180000, 0x001a0000);
527
528 RemoveHostSizedMapping(0x10180000, 0x001a0000, 8);
529 ExpectMapped(0x10180000, 0x00190000);
530
531 // Primary PTEs for 0x10180000
532
533 AddHostSizedMapping(0x10180000, 0x00180000, 2);
534 ExpectMapped(0x10180000, 0x00180000);
535
536 AddHostSizedMapping(0x10180000, 0x001a0000, 4);
537 ExpectMapped(0x10180000, 0x00180000);
538
539 AddHostSizedMapping(0x10180000, 0x001b0000, 0);
540 ExpectMapped(0x10180000, 0x001b0000);
541
542 RemoveHostSizedMapping(0x10180000, 0x00180000, 2);
543 ExpectMapped(0x10180000, 0x001b0000);
544
545 RemoveHostSizedMapping(0x10180000, 0x001b0000, 0);
546 ExpectMapped(0x10180000, 0x001a0000);
547
548 // Return to secondary PTE for 0x10180000
549
550 RemoveHostSizedMapping(0x10180000, 0x001a0000, 4);
551 ExpectMapped(0x10180000, 0x00190000);
552
553 // Secondary PTEs for 0x11180000
554
555 AddHostSizedMapping(0x11180000, 0x01180000, 11);
556 ExpectMapped(0x11180000, 0x01180000);
557
558 AddHostSizedMapping(0x11180000, 0x01190000, 13);
559 ExpectMapped(0x11180000, 0x01180000);
560
561 AddHostSizedMapping(0x11180000, 0x011a0000, 9);
562 ExpectMapped(0x11180000, 0x011a0000);
563
564 RemoveHostSizedMapping(0x11180000, 0x01180000, 11);
565 ExpectMapped(0x11180000, 0x011a0000);
566
567 RemoveHostSizedMapping(0x11180000, 0x011a0000, 9);
568 ExpectMapped(0x11180000, 0x01190000);
569
570 // Primary PTEs for 0x11180000
571
572 AddHostSizedMapping(0x11180000, 0x01180000, 3);
573 ExpectMapped(0x11180000, 0x01180000);
574
575 AddHostSizedMapping(0x11180000, 0x011a0000, 5);
576 ExpectMapped(0x11180000, 0x01180000);
577
578 AddHostSizedMapping(0x11180000, 0x011b0000, 1);
579 ExpectMapped(0x11180000, 0x011b0000);
580
581 RemoveHostSizedMapping(0x11180000, 0x01180000, 3);
582 ExpectMapped(0x11180000, 0x011b0000);
583
584 RemoveHostSizedMapping(0x11180000, 0x011b0000, 1);
585 ExpectMapped(0x11180000, 0x011a0000);
586
587 // Return to secondary PTE for 0x11180000
588
589 RemoveHostSizedMapping(0x11180000, 0x011a0000, 5);
590 ExpectMapped(0x11180000, 0x01190000);
591
592 // Check that 0x10180000 is still working properly
593
594 ExpectMapped(0x10180000, 0x00190000);
595
596 AddHostSizedMapping(0x10180000, 0x00180000, 0);
597 ExpectMapped(0x10180000, 0x00180000);
598
599 // Check that 0x11180000 is still working properly
600
601 ExpectMapped(0x11180000, 0x01190000);
602
603 AddHostSizedMapping(0x11180000, 0x01180000, 1);
604 ExpectMapped(0x11180000, 0x01180000);
605 }
606
607 TEST_F(PageTableHostMappingTest, ChangeAddress)
608 {
609 // Initial mapping
610 AddHostSizedMapping(0x101c0000, 0x001c0000, 0);
611 ExpectMapped(0x101c0000, 0x001c0000);
612
613 // Change physical address
614 AddHostSizedMapping(0x101c0000, 0x001d0000, 0);
615 ExpectMapped(0x101c0000, 0x001d0000);
616
617 // Change logical address
618 AddHostSizedMapping(0x111c0000, 0x001d0000, 0);
619 ExpectMapped(0x111c0000, 0x001d0000);
620 ExpectNotMapped(0x101c0000);
621
622 // Change both logical address and physical address
623 AddHostSizedMapping(0x101c0000, 0x011d0000, 0);
624 ExpectMapped(0x101c0000, 0x011d0000);
625 ExpectNotMapped(0x111c0000);
626 }
627
628 TEST_F(PageTableHostMappingTest, InvalidPhysicalAddress)
629 {
630 AddHostSizedMapping(0x101d0000, 0x0ff00000, 0);
631 ExpectNotMapped(0x101d0000);
632 }
633
634 TEST_F(PageTableHostMappingTest, WIMG)
635 {
636 for (u32 i = 0; i < 16; ++i)
637 {
638 {
639 DisableDR disable_dr;
640 for (u32 j = 0; j < s_minimum_mapping_size; j += PowerPC::HW_PAGE_SIZE)
641 {
642 auto [pte1, pte2] = CreateMapping(0x101e0000 + j, 0x001e0000 + j);
643 pte2.WIMG = i;
644 SetPTE(pte1, pte2, 0x101e0000 + j, 0);
645 }
646 }
647
648 if ((i & 0b1100) != 0)
649 ExpectNotMapped(0x101e0000);
650 else
651 ExpectMapped(0x101e0000, 0x001e0000);
652 }
653 }
654
655 TEST_F(PageTableHostMappingTest, RC)
656 {
657 auto& mmu = Core::System::GetInstance().GetMMU();
658
659 const auto set_up_mapping = [] {
660 DisableDR disable_dr;
661 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
662 {
663 auto [pte1, pte2] = CreateMapping(0x101f0000 + i, 0x001f0000 + i);
664 pte2.R = 0;
665 pte2.C = 0;
666 SetPTE(pte1, pte2, 0x101f0000 + i, 0);
667 }
668 };
669
670 const auto expect_rc = [](u32 r, u32 c) {
671 auto [pte1, pte2] = GetPTE(0x101f0000, 0);
672 EXPECT_TRUE(pte1.V);
673 EXPECT_EQ(pte2.R, r);
674 EXPECT_EQ(pte2.C, c);
675 };
676
677 // Start with R=0, C=0
678 set_up_mapping();
679 ExpectNotMapped(0x101f0000);
680 expect_rc(0, 0);
681
682 // Automatically set R=1, C=0
683 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
684 mmu.Read<u32>(0x101f0000 + i);
685 ExpectReadOnlyMapped(0x101f0000, 0x001f0000);
686 expect_rc(1, 0);
687
688 // Automatically set R=1, C=1
689 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
690 mmu.Write<u32>(0x12345678, 0x101f0000 + i);
691 ExpectMapped(0x101f0000, 0x001f0000);
692 expect_rc(1, 1);
693
694 // Start over with R=0, C=0
695 set_up_mapping();
696 ExpectNotMapped(0x101f0000);
697 expect_rc(0, 0);
698
699 // Automatically set R=1, C=1
700 for (u32 i = 0; i < s_minimum_mapping_size; i += PowerPC::HW_PAGE_SIZE)
701 mmu.Write<u32>(0x12345678, 0x101f0000 + i);
702 ExpectMapped(0x101f0000, 0x001f0000);
703 expect_rc(1, 1);
704 }
705
706 TEST_F(PageTableHostMappingTest, ResizePageTable)
707 {
708 AddHostSizedMapping(0x10200000, 0x00200000, 0);
709 AddHostSizedMapping(0x10600000, 0x00210000, 1);
710 ExpectMapped(0x10200000, 0x00200000);
711 ExpectMapped(0x10600000, 0x00210000);
712
713 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_LARGE);
714 ExpectMapped(0x10200000, 0x00200000);
715 ExpectNotMapped(0x10600000);
716
717 AddHostSizedMapping(0x10600000, 0x00220000, 1);
718 ExpectMapped(0x10200000, 0x00200000);
719 ExpectMapped(0x10600000, 0x00220000);
720
721 AddHostSizedMapping(0x10610000, 0x00200000, 1);
722 ExpectMapped(0x10610000, 0x00200000);
723
724 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_SMALL);
725 ExpectMapped(0x10200000, 0x00200000);
726 ExpectMapped(0x10600000, 0x00210000);
727 ExpectNotMapped(0x10610000);
728 }
729
730 // The PEM says that all bits that are one in the page table mask must be zero in the page table
731 // address. What it doesn't tell you is that if this isn't obeyed, the Gekko will do a logical OR of
732 // the page table base and the page table offset, producing behavior that might not be intuitive.
733 TEST_F(PageTableHostMappingTest, MisalignedPageTable)
734 {
735 SetSDR(MISALIGNED_PAGE_TABLE_BASE + 0x10000, PowerPC::PAGE_TABLE_MIN_SIZE - 1);
736
737 AddHostSizedMapping(0x10a30000, 0x00230000, 4);
738 ExpectMapped(0x10a30000, 0x00230000);
739
740 SetSDR(MISALIGNED_PAGE_TABLE_BASE, MISALIGNED_PAGE_TABLE_MASK);
741
742 ExpectNotMapped(0x10a30000);
743
744 AddHostSizedMapping(0x10230000, 0x00240000, 0);
745 AddHostSizedMapping(0x10630000, 0x00250000, 1);
746 AddHostSizedMapping(0x10a30000, 0x00260000, 2);
747 AddHostSizedMapping(0x10e30000, 0x00270000, 3);
748
749 ExpectMapped(0x10230000, 0x00240000);
750 ExpectMapped(0x10630000, 0x00250000);
751 ExpectMapped(0x10a30000, 0x00260000);
752 ExpectMapped(0x10e30000, 0x00270000);
753
754 // Exercise the code for falling back to a secondary PTE after removing a primary PTE.
755 AddHostSizedMapping(0x10a30000, 0x00270000, 10);
756 AddHostSizedMapping(0x10e30000, 0x00260000, 11);
757 RemoveHostSizedMapping(0x10a30000, 0x00260000, 2);
758 RemoveHostSizedMapping(0x10e30000, 0x00250000, 3);
759
760 ExpectMapped(0x10230000, 0x00240000);
761 ExpectMapped(0x10630000, 0x00250000);
762 ExpectMapped(0x10a30000, 0x00270000);
763 ExpectMapped(0x10e30000, 0x00260000);
764
765 SetSDR(MISALIGNED_PAGE_TABLE_BASE_ALIGNED, MISALIGNED_PAGE_TABLE_MASK);
766
767 ExpectNotMapped(0x10230000);
768 ExpectMapped(0x10630000, 0x00250000);
769 ExpectMapped(0x10a30000, 0x00230000);
770 ExpectNotMapped(0x10e30000);
771
772 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_SMALL);
773 }
774
775 // Putting a zero in the middle of the page table mask's ones results in similar behavior
776 // to the scenario described above.
777 TEST_F(PageTableHostMappingTest, HoleInMask)
778 {
779 SetSDR(HOLE_MASK_PAGE_TABLE_BASE + 0x10000, PowerPC::PAGE_TABLE_MIN_SIZE - 1);
780
781 AddHostSizedMapping(0x10680000, 0x00280000, 4);
782 ExpectMapped(0x10680000, 0x00280000);
783
784 SetSDR(HOLE_MASK_PAGE_TABLE_BASE, HOLE_MASK_PAGE_TABLE_MASK);
785
786 ExpectNotMapped(0x10680000);
787
788 AddHostSizedMapping(0x10280000, 0x00290000, 0);
789 AddHostSizedMapping(0x10680000, 0x002a0000, 1);
790 AddHostSizedMapping(0x10a80000, 0x002b0000, 2);
791 AddHostSizedMapping(0x10e80000, 0x002c0000, 3);
792
793 ExpectMapped(0x10280000, 0x00290000);
794 ExpectMapped(0x10680000, 0x002a0000);
795 ExpectMapped(0x10a80000, 0x002b0000);
796 ExpectMapped(0x10e80000, 0x002c0000);
797
798 // Exercise the code for falling back to a secondary PTE after removing a primary PTE.
799 AddHostSizedMapping(0x10a80000, 0x002c0000, 10);
800 AddHostSizedMapping(0x10e80000, 0x002b0000, 11);
801 RemoveHostSizedMapping(0x10a80000, 0x002b0000, 2);
802 RemoveHostSizedMapping(0x10e80000, 0x002c0000, 3);
803
804 ExpectMapped(0x10280000, 0x00290000);
805 ExpectMapped(0x10680000, 0x002a0000);
806 ExpectMapped(0x10a80000, 0x002c0000);
807 ExpectMapped(0x10e80000, 0x002b0000);
808
809 SetSDR(HOLE_MASK_PAGE_TABLE_BASE, HOLE_MASK_PAGE_TABLE_MASK_WITHOUT_HOLE);
810
811 ExpectMapped(0x10280000, 0x00290000);
812 ExpectMapped(0x10680000, 0x00280000);
813 ExpectNotMapped(0x10a80000);
814 ExpectMapped(0x10e80000, 0x002b0000);
815
816 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_SMALL);
817 }
818
819 // If we combine the two scenarios above, both making the base misaligned and putting a hole in the
820 // mask, we get the same result as if we just make the base misaligned.
821 TEST_F(PageTableHostMappingTest, HoleInMaskMisalignedPageTable)
822 {
823 SetSDR(MISALIGNED_HOLE_MASK_PAGE_TABLE_BASE + 0x10000, PowerPC::PAGE_TABLE_MIN_SIZE - 1);
824
825 AddHostSizedMapping(0x10ad0000, 0x002d0000, 4);
826 ExpectMapped(0x10ad0000, 0x002d0000);
827
828 SetSDR(MISALIGNED_HOLE_MASK_PAGE_TABLE_BASE, MISALIGNED_HOLE_MASK_PAGE_TABLE_MASK);
829
830 ExpectNotMapped(0x10ad0000);
831
832 AddHostSizedMapping(0x102d0000, 0x002e0000, 0);
833 AddHostSizedMapping(0x106d0000, 0x002f0000, 1);
834 AddHostSizedMapping(0x10ad0000, 0x00300000, 2);
835 AddHostSizedMapping(0x10ed0000, 0x00310000, 3);
836
837 ExpectMapped(0x102d0000, 0x002e0000);
838 ExpectMapped(0x106d0000, 0x002f0000);
839 ExpectMapped(0x10ad0000, 0x00300000);
840 ExpectMapped(0x10ed0000, 0x00310000);
841
842 // Exercise the code for falling back to a secondary PTE after removing a primary PTE.
843 AddHostSizedMapping(0x10ad0000, 0x00310000, 10);
844 AddHostSizedMapping(0x10ed0000, 0x00300000, 11);
845 RemoveHostSizedMapping(0x10ad0000, 0x00300000, 2);
846 RemoveHostSizedMapping(0x10ed0000, 0x00310000, 3);
847
848 ExpectMapped(0x102d0000, 0x002e0000);
849 ExpectMapped(0x106d0000, 0x002f0000);
850 ExpectMapped(0x10ad0000, 0x00310000);
851 ExpectMapped(0x10ed0000, 0x00300000);
852
853 SetSDR(MISALIGNED_HOLE_MASK_PAGE_TABLE_BASE_ALIGNED,
854 MISALIGNED_HOLE_MASK_PAGE_TABLE_MASK_WITHOUT_HOLE);
855
856 ExpectNotMapped(0x102d0000);
857 ExpectMapped(0x106d0000, 0x002f0000);
858 ExpectMapped(0x10ad0000, 0x002d0000);
859 ExpectNotMapped(0x10ed0000);
860
861 SetSDR(ALIGNED_PAGE_TABLE_BASE, ALIGNED_PAGE_TABLE_MASK_SMALL);
862 }
863
864 TEST_F(PageTableHostMappingTest, MemChecks)
865 {
866 AddHostSizedMapping(0x10320000, 0x00330000, 0);
867 AddHostSizedMapping(0x10330000, 0x00320000, 0);
868 ExpectMapped(0x10320000, 0x00330000);
869 ExpectMapped(0x10330000, 0x00320000);
870
871 auto& memchecks = Core::System::GetInstance().GetPowerPC().GetMemChecks();
872 TMemCheck memcheck;
873 memcheck.start_address = 0x10320000;
874 memcheck.end_address = 0x10320001;
875 memchecks.Add(std::move(memcheck));
876
877 ExpectNotMapped(0x10320000);
878 ExpectMapped(0x10330000, 0x00320000);
879
880 memchecks.Remove(0x10320000);
881
882 ExpectMapped(0x10320000, 0x00330000);
883 ExpectMapped(0x10330000, 0x00320000);
884 }