From 056c2840b1851657c3855fb72776837c89ff59d3 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Fri, 17 Aug 2018 10:39:35 -0300 Subject: [PATCH] Fix MapPhysicalMemory when block position is less than map position (#359) * Fix MapPhysicalMemory when block position is less than map position * Only try to free pages that are actually mapped * Only unmap after freeing the memory --- Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs b/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs index 0432aa88..3afdb570 100644 --- a/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs +++ b/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs @@ -259,9 +259,9 @@ namespace Ryujinx.HLE.HOS.Kernel InsertBlock(FreeAddr, PagesCount, MemoryState.Unmapped); - CpuMemory.Unmap(FreeAddr, DiffSize); - FreePages(FreeAddr, PagesCount); + + CpuMemory.Unmap(FreeAddr, DiffSize); } } } @@ -631,7 +631,14 @@ namespace Ryujinx.HLE.HOS.Kernel { long CurrSize = GetSizeInRange(Info, Position, End); - CpuMemory.Map(Info.Position, PA, CurrSize); + long MapPosition = Info.Position; + + if ((ulong)MapPosition < (ulong)Position) + { + MapPosition = Position; + } + + CpuMemory.Map(MapPosition, PA, CurrSize); PA += CurrSize; } @@ -705,10 +712,10 @@ namespace Ryujinx.HLE.HOS.Kernel InsertBlock(Position, PagesCount, MemoryState.Unmapped); - CpuMemory.Unmap(Position, Size); - FreePages(Position, PagesCount); + CpuMemory.Unmap(Position, Size); + return 0; } } @@ -737,6 +744,11 @@ namespace Ryujinx.HLE.HOS.Kernel { long VA = Position + Page * PageSize; + if (!CpuMemory.IsMapped(VA)) + { + continue; + } + long PA = CpuMemory.GetPhysicalAddress(VA); Allocator.Free(PA, PageSize);