AuroraRuntime/Source/Memory/AuMemorySwapLock.cpp

103 lines
2.7 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuMemorySwapLock.cpp
Date: 2022-3-21
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuMemory.hpp"
#include "AuMemorySwapLock.hpp"
#include <Source/HWInfo/AuHWInfo.hpp>
namespace Aurora::Memory::SwapLock
{
static AuPair<AuUInt, AuUInt> ToPagePair(AuPair<AuUInt, AuUInt> addressRange)
{
auto uBase = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize));
auto uLength = AuPageRoundUp(addressRange.second + (addressRange.first - uBase), AuUInt(AuHwInfo::gPageSize));
return AuMakePair(uBase, uLength);
}
AUKN_SYM bool Lock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
for (AU_ITERATE_N(i, addressRanges.size()))
{
bool bBypass {};
AuUInt uBase, uLength;
AuTupleTie(uBase, uLength) = ToPagePair(addressRanges[i]);
for (AU_ITERATE_N(z, i))
{
AuUInt uBase2, uLength2;
AuTupleTie(uBase2, uLength2) = ToPagePair(addressRanges[z]);
if (uBase < uBase2)
{
continue;
}
if ((uBase + uLength) > (uBase2 + uLength2))
{
continue;
}
bBypass = true;
break;
}
if (!bBypass &&
!SysMemoryLockPages(AuReinterpretCast<const void *>(uBase), uLength))
{
SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", uBase, uLength);
return false;
}
}
return true;
}
AUKN_SYM bool Unlock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
bool bRet { true };
for (AU_ITERATE_N(i, addressRanges.size()))
{
bool bBypass {};
AuUInt uBase, uLength;
AuTupleTie(uBase, uLength) = ToPagePair(addressRanges[i]);
for (AU_ITERATE_N(z, i))
{
AuUInt uBase2, uLength2;
AuTupleTie(uBase2, uLength2) = ToPagePair(addressRanges[z]);
if (uBase < uBase2)
{
continue;
}
if ((uBase + uLength) > (uBase2 + uLength2))
{
continue;
}
bBypass = true;
break;
}
if (!bBypass &&
!SysMemoryUnlockPages(AuReinterpretCast<const void *>(uBase), uLength))
{
SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", uBase, uLength);
bRet = false;
}
}
return bRet;
}
}