Refactor SVC handler (#540)
* Refactor SVC handler * Get rid of KernelErr * Split kernel code files into multiple folders
This commit is contained in:
parent
2534a7f10c
commit
0039bb6394
105 changed files with 1894 additions and 1982 deletions
10
Ryujinx.HLE/HOS/Kernel/Memory/AddressSpaceType.cs
Normal file
10
Ryujinx.HLE/HOS/Kernel/Memory/AddressSpaceType.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
enum AddressSpaceType
|
||||
{
|
||||
Addr32Bits = 0,
|
||||
Addr36Bits = 1,
|
||||
Addr32BitsNoMap = 2,
|
||||
Addr39Bits = 3
|
||||
}
|
||||
}
|
15
Ryujinx.HLE/HOS/Kernel/Memory/DramMemoryMap.cs
Normal file
15
Ryujinx.HLE/HOS/Kernel/Memory/DramMemoryMap.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
static class DramMemoryMap
|
||||
{
|
||||
public const ulong DramBase = 0x80000000;
|
||||
public const ulong DramSize = 0x100000000;
|
||||
public const ulong DramEnd = DramBase + DramSize;
|
||||
|
||||
public const ulong KernelReserveBase = DramBase + 0x60000;
|
||||
|
||||
public const ulong SlabHeapBase = KernelReserveBase + 0x85000;
|
||||
public const ulong SlapHeapSize = 0xa21000;
|
||||
public const ulong SlabHeapEnd = SlabHeapBase + SlapHeapSize;
|
||||
}
|
||||
}
|
22
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryArrange.cs
Normal file
22
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryArrange.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryArrange
|
||||
{
|
||||
public KMemoryArrangeRegion Service { get; private set; }
|
||||
public KMemoryArrangeRegion NvServices { get; private set; }
|
||||
public KMemoryArrangeRegion Applet { get; private set; }
|
||||
public KMemoryArrangeRegion Application { get; private set; }
|
||||
|
||||
public KMemoryArrange(
|
||||
KMemoryArrangeRegion service,
|
||||
KMemoryArrangeRegion nvServices,
|
||||
KMemoryArrangeRegion applet,
|
||||
KMemoryArrangeRegion application)
|
||||
{
|
||||
Service = service;
|
||||
NvServices = nvServices;
|
||||
Applet = applet;
|
||||
Application = application;
|
||||
}
|
||||
}
|
||||
}
|
16
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryArrangeRegion.cs
Normal file
16
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryArrangeRegion.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
struct KMemoryArrangeRegion
|
||||
{
|
||||
public ulong Address { get; private set; }
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
public ulong EndAddr => Address + Size;
|
||||
|
||||
public KMemoryArrangeRegion(ulong address, ulong size)
|
||||
{
|
||||
Address = address;
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
}
|
43
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs
Normal file
43
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryBlock
|
||||
{
|
||||
public ulong BaseAddress { get; set; }
|
||||
public ulong PagesCount { get; set; }
|
||||
|
||||
public MemoryState State { get; set; }
|
||||
public MemoryPermission Permission { get; set; }
|
||||
public MemoryAttribute Attribute { get; set; }
|
||||
|
||||
public int IpcRefCount { get; set; }
|
||||
public int DeviceRefCount { get; set; }
|
||||
|
||||
public KMemoryBlock(
|
||||
ulong baseAddress,
|
||||
ulong pagesCount,
|
||||
MemoryState state,
|
||||
MemoryPermission permission,
|
||||
MemoryAttribute attribute)
|
||||
{
|
||||
BaseAddress = baseAddress;
|
||||
PagesCount = pagesCount;
|
||||
State = state;
|
||||
Attribute = attribute;
|
||||
Permission = permission;
|
||||
}
|
||||
|
||||
public KMemoryInfo GetInfo()
|
||||
{
|
||||
ulong size = PagesCount * KMemoryManager.PageSize;
|
||||
|
||||
return new KMemoryInfo(
|
||||
BaseAddress,
|
||||
size,
|
||||
State,
|
||||
Permission,
|
||||
Attribute,
|
||||
IpcRefCount,
|
||||
DeviceRefCount);
|
||||
}
|
||||
}
|
||||
}
|
19
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockAllocator.cs
Normal file
19
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockAllocator.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryBlockAllocator
|
||||
{
|
||||
private ulong _capacityElements;
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public KMemoryBlockAllocator(ulong capacityElements)
|
||||
{
|
||||
_capacityElements = capacityElements;
|
||||
}
|
||||
|
||||
public bool CanAllocate(int count)
|
||||
{
|
||||
return (ulong)(Count + count) <= _capacityElements;
|
||||
}
|
||||
}
|
||||
}
|
33
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryInfo.cs
Normal file
33
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryInfo.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryInfo
|
||||
{
|
||||
public ulong Address { get; private set; }
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
public MemoryState State { get; private set; }
|
||||
public MemoryPermission Permission { get; private set; }
|
||||
public MemoryAttribute Attribute { get; private set; }
|
||||
|
||||
public int IpcRefCount { get; private set; }
|
||||
public int DeviceRefCount { get; private set; }
|
||||
|
||||
public KMemoryInfo(
|
||||
ulong address,
|
||||
ulong size,
|
||||
MemoryState state,
|
||||
MemoryPermission permission,
|
||||
MemoryAttribute attribute,
|
||||
int ipcRefCount,
|
||||
int deviceRefCount)
|
||||
{
|
||||
Address = address;
|
||||
Size = size;
|
||||
State = state;
|
||||
Attribute = attribute;
|
||||
Permission = permission;
|
||||
IpcRefCount = ipcRefCount;
|
||||
DeviceRefCount = deviceRefCount;
|
||||
}
|
||||
}
|
||||
}
|
2460
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs
Normal file
2460
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs
Normal file
File diff suppressed because it is too large
Load diff
43
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryRegionBlock.cs
Normal file
43
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryRegionBlock.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryRegionBlock
|
||||
{
|
||||
public long[][] Masks;
|
||||
|
||||
public ulong FreeCount;
|
||||
public int MaxLevel;
|
||||
public ulong StartAligned;
|
||||
public ulong SizeInBlocksTruncated;
|
||||
public ulong SizeInBlocksRounded;
|
||||
public int Order;
|
||||
public int NextOrder;
|
||||
|
||||
public bool TryCoalesce(int index, int size)
|
||||
{
|
||||
long mask = ((1L << size) - 1) << (index & 63);
|
||||
|
||||
index /= 64;
|
||||
|
||||
if ((mask & ~Masks[MaxLevel - 1][index]) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Masks[MaxLevel - 1][index] &= ~mask;
|
||||
|
||||
for (int level = MaxLevel - 2; level >= 0; level--, index /= 64)
|
||||
{
|
||||
Masks[level][index / 64] &= ~(1L << (index & 63));
|
||||
|
||||
if (Masks[level][index / 64] != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FreeCount -= (ulong)size;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
429
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryRegionManager.cs
Normal file
429
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryRegionManager.cs
Normal file
|
@ -0,0 +1,429 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryRegionManager
|
||||
{
|
||||
private static readonly int[] BlockOrders = new int[] { 12, 16, 21, 22, 25, 29, 30 };
|
||||
|
||||
public ulong Address { get; private set; }
|
||||
public ulong EndAddr { get; private set; }
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
private int _blockOrdersCount;
|
||||
|
||||
private KMemoryRegionBlock[] _blocks;
|
||||
|
||||
public KMemoryRegionManager(ulong address, ulong size, ulong endAddr)
|
||||
{
|
||||
_blocks = new KMemoryRegionBlock[BlockOrders.Length];
|
||||
|
||||
Address = address;
|
||||
Size = size;
|
||||
EndAddr = endAddr;
|
||||
|
||||
_blockOrdersCount = BlockOrders.Length;
|
||||
|
||||
for (int blockIndex = 0; blockIndex < _blockOrdersCount; blockIndex++)
|
||||
{
|
||||
_blocks[blockIndex] = new KMemoryRegionBlock();
|
||||
|
||||
_blocks[blockIndex].Order = BlockOrders[blockIndex];
|
||||
|
||||
int nextOrder = blockIndex == _blockOrdersCount - 1 ? 0 : BlockOrders[blockIndex + 1];
|
||||
|
||||
_blocks[blockIndex].NextOrder = nextOrder;
|
||||
|
||||
int currBlockSize = 1 << BlockOrders[blockIndex];
|
||||
int nextBlockSize = currBlockSize;
|
||||
|
||||
if (nextOrder != 0)
|
||||
{
|
||||
nextBlockSize = 1 << nextOrder;
|
||||
}
|
||||
|
||||
ulong startAligned = BitUtils.AlignDown(address, nextBlockSize);
|
||||
ulong endAddrAligned = BitUtils.AlignDown(endAddr, currBlockSize);
|
||||
|
||||
ulong sizeInBlocksTruncated = (endAddrAligned - startAligned) >> BlockOrders[blockIndex];
|
||||
|
||||
ulong endAddrRounded = BitUtils.AlignUp(address + size, nextBlockSize);
|
||||
|
||||
ulong sizeInBlocksRounded = (endAddrRounded - startAligned) >> BlockOrders[blockIndex];
|
||||
|
||||
_blocks[blockIndex].StartAligned = startAligned;
|
||||
_blocks[blockIndex].SizeInBlocksTruncated = sizeInBlocksTruncated;
|
||||
_blocks[blockIndex].SizeInBlocksRounded = sizeInBlocksRounded;
|
||||
|
||||
ulong currSizeInBlocks = sizeInBlocksRounded;
|
||||
|
||||
int maxLevel = 0;
|
||||
|
||||
do
|
||||
{
|
||||
maxLevel++;
|
||||
}
|
||||
while ((currSizeInBlocks /= 64) != 0);
|
||||
|
||||
_blocks[blockIndex].MaxLevel = maxLevel;
|
||||
|
||||
_blocks[blockIndex].Masks = new long[maxLevel][];
|
||||
|
||||
currSizeInBlocks = sizeInBlocksRounded;
|
||||
|
||||
for (int level = maxLevel - 1; level >= 0; level--)
|
||||
{
|
||||
currSizeInBlocks = (currSizeInBlocks + 63) / 64;
|
||||
|
||||
_blocks[blockIndex].Masks[level] = new long[currSizeInBlocks];
|
||||
}
|
||||
}
|
||||
|
||||
if (size != 0)
|
||||
{
|
||||
FreePages(address, size / KMemoryManager.PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult AllocatePages(ulong pagesCount, bool backwards, out KPageList pageList)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
return AllocatePagesImpl(pagesCount, backwards, out pageList);
|
||||
}
|
||||
}
|
||||
|
||||
private KernelResult AllocatePagesImpl(ulong pagesCount, bool backwards, out KPageList pageList)
|
||||
{
|
||||
pageList = new KPageList();
|
||||
|
||||
if (_blockOrdersCount > 0)
|
||||
{
|
||||
if (GetFreePagesImpl() < pagesCount)
|
||||
{
|
||||
return KernelResult.OutOfMemory;
|
||||
}
|
||||
}
|
||||
else if (pagesCount != 0)
|
||||
{
|
||||
return KernelResult.OutOfMemory;
|
||||
}
|
||||
|
||||
for (int blockIndex = _blockOrdersCount - 1; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
KMemoryRegionBlock block = _blocks[blockIndex];
|
||||
|
||||
ulong bestFitBlockSize = 1UL << block.Order;
|
||||
|
||||
ulong blockPagesCount = bestFitBlockSize / KMemoryManager.PageSize;
|
||||
|
||||
//Check if this is the best fit for this page size.
|
||||
//If so, try allocating as much requested pages as possible.
|
||||
while (blockPagesCount <= pagesCount)
|
||||
{
|
||||
ulong address = 0;
|
||||
|
||||
for (int currBlockIndex = blockIndex;
|
||||
currBlockIndex < _blockOrdersCount && address == 0;
|
||||
currBlockIndex++)
|
||||
{
|
||||
block = _blocks[currBlockIndex];
|
||||
|
||||
int index = 0;
|
||||
|
||||
bool zeroMask = false;
|
||||
|
||||
for (int level = 0; level < block.MaxLevel; level++)
|
||||
{
|
||||
long mask = block.Masks[level][index];
|
||||
|
||||
if (mask == 0)
|
||||
{
|
||||
zeroMask = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (backwards)
|
||||
{
|
||||
index = (index * 64 + 63) - BitUtils.CountLeadingZeros64(mask);
|
||||
}
|
||||
else
|
||||
{
|
||||
index = index * 64 + BitUtils.CountLeadingZeros64(BitUtils.ReverseBits64(mask));
|
||||
}
|
||||
}
|
||||
|
||||
if (block.SizeInBlocksTruncated <= (ulong)index || zeroMask)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
block.FreeCount--;
|
||||
|
||||
int tempIdx = index;
|
||||
|
||||
for (int level = block.MaxLevel - 1; level >= 0; level--, tempIdx /= 64)
|
||||
{
|
||||
block.Masks[level][tempIdx / 64] &= ~(1L << (tempIdx & 63));
|
||||
|
||||
if (block.Masks[level][tempIdx / 64] != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
address = block.StartAligned + ((ulong)index << block.Order);
|
||||
}
|
||||
|
||||
for (int currBlockIndex = blockIndex;
|
||||
currBlockIndex < _blockOrdersCount && address == 0;
|
||||
currBlockIndex++)
|
||||
{
|
||||
block = _blocks[currBlockIndex];
|
||||
|
||||
int index = 0;
|
||||
|
||||
bool zeroMask = false;
|
||||
|
||||
for (int level = 0; level < block.MaxLevel; level++)
|
||||
{
|
||||
long mask = block.Masks[level][index];
|
||||
|
||||
if (mask == 0)
|
||||
{
|
||||
zeroMask = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (backwards)
|
||||
{
|
||||
index = index * 64 + BitUtils.CountLeadingZeros64(BitUtils.ReverseBits64(mask));
|
||||
}
|
||||
else
|
||||
{
|
||||
index = (index * 64 + 63) - BitUtils.CountLeadingZeros64(mask);
|
||||
}
|
||||
}
|
||||
|
||||
if (block.SizeInBlocksTruncated <= (ulong)index || zeroMask)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
block.FreeCount--;
|
||||
|
||||
int tempIdx = index;
|
||||
|
||||
for (int level = block.MaxLevel - 1; level >= 0; level--, tempIdx /= 64)
|
||||
{
|
||||
block.Masks[level][tempIdx / 64] &= ~(1L << (tempIdx & 63));
|
||||
|
||||
if (block.Masks[level][tempIdx / 64] != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
address = block.StartAligned + ((ulong)index << block.Order);
|
||||
}
|
||||
|
||||
//The address being zero means that no free space was found on that order,
|
||||
//just give up and try with the next one.
|
||||
if (address == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
//If we are using a larger order than best fit, then we should
|
||||
//split it into smaller blocks.
|
||||
ulong firstFreeBlockSize = 1UL << block.Order;
|
||||
|
||||
if (firstFreeBlockSize > bestFitBlockSize)
|
||||
{
|
||||
FreePages(address + bestFitBlockSize, (firstFreeBlockSize - bestFitBlockSize) / KMemoryManager.PageSize);
|
||||
}
|
||||
|
||||
//Add new allocated page(s) to the pages list.
|
||||
//If an error occurs, then free all allocated pages and fail.
|
||||
KernelResult result = pageList.AddRange(address, blockPagesCount);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
FreePages(address, blockPagesCount);
|
||||
|
||||
foreach (KPageNode pageNode in pageList)
|
||||
{
|
||||
FreePages(pageNode.Address, pageNode.PagesCount);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
pagesCount -= blockPagesCount;
|
||||
}
|
||||
}
|
||||
|
||||
//Success case, all requested pages were allocated successfully.
|
||||
if (pagesCount == 0)
|
||||
{
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
//Error case, free allocated pages and return out of memory.
|
||||
foreach (KPageNode pageNode in pageList)
|
||||
{
|
||||
FreePages(pageNode.Address, pageNode.PagesCount);
|
||||
}
|
||||
|
||||
pageList = null;
|
||||
|
||||
return KernelResult.OutOfMemory;
|
||||
}
|
||||
|
||||
public void FreePages(KPageList pageList)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
foreach (KPageNode pageNode in pageList)
|
||||
{
|
||||
FreePages(pageNode.Address, pageNode.PagesCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FreePages(ulong address, ulong pagesCount)
|
||||
{
|
||||
ulong endAddr = address + pagesCount * KMemoryManager.PageSize;
|
||||
|
||||
int blockIndex = _blockOrdersCount - 1;
|
||||
|
||||
ulong addressRounded = 0;
|
||||
ulong endAddrTruncated = 0;
|
||||
|
||||
for (; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
KMemoryRegionBlock allocInfo = _blocks[blockIndex];
|
||||
|
||||
int blockSize = 1 << allocInfo.Order;
|
||||
|
||||
addressRounded = BitUtils.AlignUp (address, blockSize);
|
||||
endAddrTruncated = BitUtils.AlignDown(endAddr, blockSize);
|
||||
|
||||
if (addressRounded < endAddrTruncated)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void FreeRegion(ulong currAddress)
|
||||
{
|
||||
for (int currBlockIndex = blockIndex;
|
||||
currBlockIndex < _blockOrdersCount && currAddress != 0;
|
||||
currBlockIndex++)
|
||||
{
|
||||
KMemoryRegionBlock block = _blocks[currBlockIndex];
|
||||
|
||||
block.FreeCount++;
|
||||
|
||||
ulong freedBlocks = (currAddress - block.StartAligned) >> block.Order;
|
||||
|
||||
int index = (int)freedBlocks;
|
||||
|
||||
for (int level = block.MaxLevel - 1; level >= 0; level--, index /= 64)
|
||||
{
|
||||
long mask = block.Masks[level][index / 64];
|
||||
|
||||
block.Masks[level][index / 64] = mask | (1L << (index & 63));
|
||||
|
||||
if (mask != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int blockSizeDelta = 1 << (block.NextOrder - block.Order);
|
||||
|
||||
int freedBlocksTruncated = BitUtils.AlignDown((int)freedBlocks, blockSizeDelta);
|
||||
|
||||
if (!block.TryCoalesce(freedBlocksTruncated, blockSizeDelta))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
currAddress = block.StartAligned + ((ulong)freedBlocksTruncated << block.Order);
|
||||
}
|
||||
}
|
||||
|
||||
//Free inside aligned region.
|
||||
ulong baseAddress = addressRounded;
|
||||
|
||||
while (baseAddress < endAddrTruncated)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
|
||||
baseAddress += blockSize;
|
||||
}
|
||||
|
||||
int nextBlockIndex = blockIndex - 1;
|
||||
|
||||
//Free region between Address and aligned region start.
|
||||
baseAddress = addressRounded;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress - blockSize >= address)
|
||||
{
|
||||
baseAddress -= blockSize;
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
}
|
||||
}
|
||||
|
||||
//Free region between aligned region end and End Address.
|
||||
baseAddress = endAddrTruncated;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress + blockSize <= endAddr)
|
||||
{
|
||||
FreeRegion(baseAddress);
|
||||
|
||||
baseAddress += blockSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ulong GetFreePages()
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
return GetFreePagesImpl();
|
||||
}
|
||||
}
|
||||
|
||||
private ulong GetFreePagesImpl()
|
||||
{
|
||||
ulong availablePages = 0;
|
||||
|
||||
for (int blockIndex = 0; blockIndex < _blockOrdersCount; blockIndex++)
|
||||
{
|
||||
KMemoryRegionBlock block = _blocks[blockIndex];
|
||||
|
||||
ulong blockPagesCount = (1UL << block.Order) / KMemoryManager.PageSize;
|
||||
|
||||
availablePages += blockPagesCount * block.FreeCount;
|
||||
}
|
||||
|
||||
return availablePages;
|
||||
}
|
||||
}
|
||||
}
|
81
Ryujinx.HLE/HOS/Kernel/Memory/KPageList.cs
Normal file
81
Ryujinx.HLE/HOS/Kernel/Memory/KPageList.cs
Normal file
|
@ -0,0 +1,81 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KPageList : IEnumerable<KPageNode>
|
||||
{
|
||||
public LinkedList<KPageNode> Nodes { get; private set; }
|
||||
|
||||
public KPageList()
|
||||
{
|
||||
Nodes = new LinkedList<KPageNode>();
|
||||
}
|
||||
|
||||
public KernelResult AddRange(ulong address, ulong pagesCount)
|
||||
{
|
||||
if (pagesCount != 0)
|
||||
{
|
||||
if (Nodes.Last != null)
|
||||
{
|
||||
KPageNode lastNode = Nodes.Last.Value;
|
||||
|
||||
if (lastNode.Address + lastNode.PagesCount * KMemoryManager.PageSize == address)
|
||||
{
|
||||
address = lastNode.Address;
|
||||
pagesCount += lastNode.PagesCount;
|
||||
|
||||
Nodes.RemoveLast();
|
||||
}
|
||||
}
|
||||
|
||||
Nodes.AddLast(new KPageNode(address, pagesCount));
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public ulong GetPagesCount()
|
||||
{
|
||||
ulong sum = 0;
|
||||
|
||||
foreach (KPageNode node in Nodes)
|
||||
{
|
||||
sum += node.PagesCount;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
public bool IsEqual(KPageList other)
|
||||
{
|
||||
LinkedListNode<KPageNode> thisNode = Nodes.First;
|
||||
LinkedListNode<KPageNode> otherNode = other.Nodes.First;
|
||||
|
||||
while (thisNode != null && otherNode != null)
|
||||
{
|
||||
if (thisNode.Value.Address != otherNode.Value.Address ||
|
||||
thisNode.Value.PagesCount != otherNode.Value.PagesCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
thisNode = thisNode.Next;
|
||||
otherNode = otherNode.Next;
|
||||
}
|
||||
|
||||
return thisNode == null && otherNode == null;
|
||||
}
|
||||
|
||||
public IEnumerator<KPageNode> GetEnumerator()
|
||||
{
|
||||
return Nodes.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
14
Ryujinx.HLE/HOS/Kernel/Memory/KPageNode.cs
Normal file
14
Ryujinx.HLE/HOS/Kernel/Memory/KPageNode.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
struct KPageNode
|
||||
{
|
||||
public ulong Address;
|
||||
public ulong PagesCount;
|
||||
|
||||
public KPageNode(ulong address, ulong pagesCount)
|
||||
{
|
||||
Address = address;
|
||||
PagesCount = pagesCount;
|
||||
}
|
||||
}
|
||||
}
|
70
Ryujinx.HLE/HOS/Kernel/Memory/KSharedMemory.cs
Normal file
70
Ryujinx.HLE/HOS/Kernel/Memory/KSharedMemory.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KSharedMemory
|
||||
{
|
||||
private KPageList _pageList;
|
||||
|
||||
private long _ownerPid;
|
||||
|
||||
private MemoryPermission _ownerPermission;
|
||||
private MemoryPermission _userPermission;
|
||||
|
||||
public KSharedMemory(
|
||||
KPageList pageList,
|
||||
long ownerPid,
|
||||
MemoryPermission ownerPermission,
|
||||
MemoryPermission userPermission)
|
||||
{
|
||||
_pageList = pageList;
|
||||
_ownerPid = ownerPid;
|
||||
_ownerPermission = ownerPermission;
|
||||
_userPermission = userPermission;
|
||||
}
|
||||
|
||||
public KernelResult MapIntoProcess(
|
||||
KMemoryManager memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process,
|
||||
MemoryPermission permission)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
||||
|
||||
if (_pageList.GetPagesCount() != pagesCountRounded)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
MemoryPermission expectedPermission = process.Pid == _ownerPid
|
||||
? _ownerPermission
|
||||
: _userPermission;
|
||||
|
||||
if (permission != expectedPermission)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
|
||||
}
|
||||
|
||||
public KernelResult UnmapFromProcess(
|
||||
KMemoryManager memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
||||
|
||||
if (_pageList.GetPagesCount() != pagesCountRounded)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
|
||||
}
|
||||
}
|
||||
}
|
50
Ryujinx.HLE/HOS/Kernel/Memory/KSlabHeap.cs
Normal file
50
Ryujinx.HLE/HOS/Kernel/Memory/KSlabHeap.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KSlabHeap
|
||||
{
|
||||
private LinkedList<ulong> _items;
|
||||
|
||||
public KSlabHeap(ulong pa, ulong itemSize, ulong size)
|
||||
{
|
||||
_items = new LinkedList<ulong>();
|
||||
|
||||
int itemsCount = (int)(size / itemSize);
|
||||
|
||||
for (int index = 0; index < itemsCount; index++)
|
||||
{
|
||||
_items.AddLast(pa);
|
||||
|
||||
pa += itemSize;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetItem(out ulong pa)
|
||||
{
|
||||
lock (_items)
|
||||
{
|
||||
if (_items.First != null)
|
||||
{
|
||||
pa = _items.First.Value;
|
||||
|
||||
_items.RemoveFirst();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
pa = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Free(ulong pa)
|
||||
{
|
||||
lock (_items)
|
||||
{
|
||||
_items.AddFirst(pa);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs
Normal file
14
Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KTransferMemory
|
||||
{
|
||||
public ulong Address { get; private set; }
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
public KTransferMemory(ulong address, ulong size)
|
||||
{
|
||||
Address = address;
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
}
|
22
Ryujinx.HLE/HOS/Kernel/Memory/MemoryAttribute.cs
Normal file
22
Ryujinx.HLE/HOS/Kernel/Memory/MemoryAttribute.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
[Flags]
|
||||
enum MemoryAttribute : byte
|
||||
{
|
||||
None = 0,
|
||||
Mask = 0xff,
|
||||
|
||||
Borrowed = 1 << 0,
|
||||
IpcMapped = 1 << 1,
|
||||
DeviceMapped = 1 << 2,
|
||||
Uncached = 1 << 3,
|
||||
|
||||
IpcAndDeviceMapped = IpcMapped | DeviceMapped,
|
||||
|
||||
BorrowedAndIpcMapped = Borrowed | IpcMapped,
|
||||
|
||||
DeviceMappedAndUncached = DeviceMapped | Uncached
|
||||
}
|
||||
}
|
12
Ryujinx.HLE/HOS/Kernel/Memory/MemoryOperation.cs
Normal file
12
Ryujinx.HLE/HOS/Kernel/Memory/MemoryOperation.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
enum MemoryOperation
|
||||
{
|
||||
MapPa,
|
||||
MapVa,
|
||||
Allocate,
|
||||
Unmap,
|
||||
ChangePermRw,
|
||||
ChangePermsAndAttributes
|
||||
}
|
||||
}
|
18
Ryujinx.HLE/HOS/Kernel/Memory/MemoryPermission.cs
Normal file
18
Ryujinx.HLE/HOS/Kernel/Memory/MemoryPermission.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
[Flags]
|
||||
enum MemoryPermission : byte
|
||||
{
|
||||
None = 0,
|
||||
Mask = 0xff,
|
||||
|
||||
Read = 1 << 0,
|
||||
Write = 1 << 1,
|
||||
Execute = 1 << 2,
|
||||
|
||||
ReadAndWrite = Read | Write,
|
||||
ReadAndExecute = Read | Execute
|
||||
}
|
||||
}
|
10
Ryujinx.HLE/HOS/Kernel/Memory/MemoryRegion.cs
Normal file
10
Ryujinx.HLE/HOS/Kernel/Memory/MemoryRegion.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
enum MemoryRegion
|
||||
{
|
||||
Application = 0,
|
||||
Applet = 1,
|
||||
Service = 2,
|
||||
NvServices = 3
|
||||
}
|
||||
}
|
49
Ryujinx.HLE/HOS/Kernel/Memory/MemoryState.cs
Normal file
49
Ryujinx.HLE/HOS/Kernel/Memory/MemoryState.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
[Flags]
|
||||
enum MemoryState : uint
|
||||
{
|
||||
Unmapped = 0x00000000,
|
||||
Io = 0x00002001,
|
||||
Normal = 0x00042002,
|
||||
CodeStatic = 0x00DC7E03,
|
||||
CodeMutable = 0x03FEBD04,
|
||||
Heap = 0x037EBD05,
|
||||
SharedMemory = 0x00402006,
|
||||
ModCodeStatic = 0x00DD7E08,
|
||||
ModCodeMutable = 0x03FFBD09,
|
||||
IpcBuffer0 = 0x005C3C0A,
|
||||
Stack = 0x005C3C0B,
|
||||
ThreadLocal = 0x0040200C,
|
||||
TransferMemoryIsolated = 0x015C3C0D,
|
||||
TransferMemory = 0x005C380E,
|
||||
ProcessMemory = 0x0040380F,
|
||||
Reserved = 0x00000010,
|
||||
IpcBuffer1 = 0x005C3811,
|
||||
IpcBuffer3 = 0x004C2812,
|
||||
KernelStack = 0x00002013,
|
||||
CodeReadOnly = 0x00402214,
|
||||
CodeWritable = 0x00402015,
|
||||
Mask = 0xffffffff,
|
||||
|
||||
PermissionChangeAllowed = 1 << 8,
|
||||
ForceReadWritableByDebugSyscalls = 1 << 9,
|
||||
IpcSendAllowedType0 = 1 << 10,
|
||||
IpcSendAllowedType3 = 1 << 11,
|
||||
IpcSendAllowedType1 = 1 << 12,
|
||||
ProcessPermissionChangeAllowed = 1 << 14,
|
||||
MapAllowed = 1 << 15,
|
||||
UnmapProcessCodeMemoryAllowed = 1 << 16,
|
||||
TransferMemoryAllowed = 1 << 17,
|
||||
QueryPhysicalAddressAllowed = 1 << 18,
|
||||
MapDeviceAllowed = 1 << 19,
|
||||
MapDeviceAlignedAllowed = 1 << 20,
|
||||
IpcBufferAllowed = 1 << 21,
|
||||
IsPoolAllocated = 1 << 22,
|
||||
MapProcessAllowed = 1 << 23,
|
||||
AttributeChangeAllowed = 1 << 24,
|
||||
CodeMemoryAllowed = 1 << 25
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue