/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AuMemorySwapLock.cpp Date: 2022-3-21 Author: Reece ***/ #include #include "AuMemory.hpp" #include "AuMemorySwapLock.hpp" #include namespace Aurora::Memory::SwapLock { static AuPair ToPagePair(AuPair 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> &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(uBase), uLength)) { SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", uBase, uLength); return false; } } return true; } AUKN_SYM bool Unlock(const AuList> &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(uBase), uLength)) { SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", uBase, uLength); bRet = false; } } return bRet; } }