2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Logging;
|
|
|
|
using Ryujinx.HLE.OsHle.Handles;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
using static Ryujinx.HLE.OsHle.ErrorCode;
|
2018-03-05 20:18:37 +01:00
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
namespace Ryujinx.HLE.OsHle.Kernel
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcSetHeapSize(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
uint Size = (uint)ThreadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
long Position = MemoryRegions.HeapRegionAddress;
|
|
|
|
|
|
|
|
if (Size > CurrentHeapSize)
|
|
|
|
{
|
|
|
|
Memory.Manager.Map(Position, Size, (int)MemoryType.Heap, AMemoryPerm.RW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Memory.Manager.Unmap(Position + Size, (long)CurrentHeapSize - Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentHeapSize = Size;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-28 00:45:07 +01:00
|
|
|
ThreadState.X1 = (ulong)Position;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcSetMemoryAttribute(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
long Size = (long)ThreadState.X1;
|
|
|
|
int State0 = (int)ThreadState.X2;
|
|
|
|
int State1 = (int)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-26 02:53:01 +01:00
|
|
|
if ((State0 == 0 && State1 == 0) ||
|
|
|
|
(State0 == 8 && State1 == 0))
|
|
|
|
{
|
|
|
|
Memory.Manager.ClearAttrBit(Position, Size, 3);
|
|
|
|
}
|
|
|
|
else if (State0 == 8 && State1 == 8)
|
|
|
|
{
|
|
|
|
Memory.Manager.SetAttrBit(Position, Size, 3);
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcMapMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long Dst = (long)ThreadState.X0;
|
|
|
|
long Src = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
if (!IsValidPosition(Src))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid src address {Src:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidMapPosition(Dst))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid dst address {Dst:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
AMemoryMapInfo SrcInfo = Memory.Manager.GetMapInfo(Src);
|
|
|
|
|
|
|
|
Memory.Manager.Map(Dst, Size, (int)MemoryType.MappedMemory, SrcInfo.Perm);
|
|
|
|
|
|
|
|
Memory.Manager.Reprotect(Src, Size, AMemoryPerm.None);
|
|
|
|
|
|
|
|
Memory.Manager.SetAttrBit(Src, Size, 0);
|
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcUnmapMemory(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long Dst = (long)ThreadState.X0;
|
|
|
|
long Src = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
if (!IsValidPosition(Src))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid src address {Src:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidMapPosition(Dst))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid dst address {Dst:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
AMemoryMapInfo DstInfo = Memory.Manager.GetMapInfo(Dst);
|
|
|
|
|
|
|
|
Memory.Manager.Unmap(Dst, Size, (int)MemoryType.MappedMemory);
|
|
|
|
|
|
|
|
Memory.Manager.Reprotect(Src, Size, DstInfo.Perm);
|
|
|
|
|
|
|
|
Memory.Manager.ClearAttrBit(Src, Size, 0);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcQueryMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long InfoPtr = (long)ThreadState.X0;
|
|
|
|
long Position = (long)ThreadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
AMemoryMapInfo MapInfo = Memory.Manager.GetMapInfo(Position);
|
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
if (MapInfo == null)
|
|
|
|
{
|
2018-03-11 00:39:16 +01:00
|
|
|
long AddrSpaceEnd = MemoryRegions.AddrSpaceStart + MemoryRegions.AddrSpaceSize;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
long ReservedSize = (long)(ulong.MaxValue - (ulong)AddrSpaceEnd) + 1;
|
|
|
|
|
|
|
|
MapInfo = new AMemoryMapInfo(AddrSpaceEnd, ReservedSize, (int)MemoryType.Reserved, 0, AMemoryPerm.None);
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
Memory.WriteInt64(InfoPtr + 0x00, MapInfo.Position);
|
|
|
|
Memory.WriteInt64(InfoPtr + 0x08, MapInfo.Size);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x10, MapInfo.Type);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x14, MapInfo.Attr);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x18, (int)MapInfo.Perm);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x1c, 0);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x20, 0);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x24, 0);
|
2018-02-05 00:08:20 +01:00
|
|
|
//TODO: X1.
|
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-18 20:28:07 +01:00
|
|
|
ThreadState.X1 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcMapSharedMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
long Src = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
|
|
|
int Perm = (int)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
if (!IsValidPosition(Src))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address {Src:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
HSharedMem SharedMem = Process.HandleTable.GetData<HSharedMem>(Handle);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-17 22:36:08 +01:00
|
|
|
if (SharedMem != null)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-11 00:39:16 +01:00
|
|
|
Memory.Manager.Map(Src, Size, (int)MemoryType.SharedMemory, AMemoryPerm.Write);
|
|
|
|
|
2018-03-03 02:49:17 +01:00
|
|
|
AMemoryHelper.FillWithZeros(Memory, Src, (int)Size);
|
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
Memory.Manager.Reprotect(Src, Size, (AMemoryPerm)Perm);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
lock (MappedSharedMems)
|
|
|
|
{
|
|
|
|
MappedSharedMems.Add((SharedMem, Src));
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMem.AddVirtualPosition(Memory, Src);
|
2018-02-10 01:13:18 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Error codes.
|
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcUnmapSharedMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-11 00:39:16 +01:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
long Src = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
|
|
|
|
|
|
|
if (!IsValidPosition(Src))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address {Src:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
HSharedMem SharedMem = Process.HandleTable.GetData<HSharedMem>(Handle);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
if (SharedMem != null)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-11 00:39:16 +01:00
|
|
|
Memory.Manager.Unmap(Src, Size, (int)MemoryType.SharedMemory);
|
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
SharedMem.RemoveVirtualPosition(Memory, Src);
|
|
|
|
|
|
|
|
lock (MappedSharedMems)
|
|
|
|
{
|
|
|
|
MappedSharedMems.Remove((SharedMem, Src));
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Error codes.
|
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcCreateTransferMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-11 00:39:16 +01:00
|
|
|
long Src = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
|
|
|
int Perm = (int)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
if (!IsValidPosition(Src))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address {Src:x16}!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
AMemoryMapInfo MapInfo = Memory.Manager.GetMapInfo(Src);
|
|
|
|
|
|
|
|
Memory.Manager.Reprotect(Src, Size, (AMemoryPerm)Perm);
|
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
HTransferMem TMem = new HTransferMem(Memory, MapInfo.Perm, Src, Size);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
ulong Handle = (ulong)Process.HandleTable.OpenHandle(TMem);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-03-12 05:04:52 +01:00
|
|
|
ThreadState.X1 = Handle;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-05-22 22:40:46 +02:00
|
|
|
private void SvcMapPhysicalMemory(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
uint Size = (uint)ThreadState.X1;
|
|
|
|
|
|
|
|
Memory.Manager.Map(Position, Size, (int)MemoryType.Heap, AMemoryPerm.RW);
|
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcUnmapPhysicalMemory(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
uint Size = (uint)ThreadState.X1;
|
|
|
|
|
|
|
|
Memory.Manager.Unmap(Position, Size);
|
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
2018-03-11 00:39:16 +01:00
|
|
|
private static bool IsValidPosition(long Position)
|
|
|
|
{
|
|
|
|
return Position >= MemoryRegions.AddrSpaceStart &&
|
2018-04-19 04:52:23 +02:00
|
|
|
Position < MemoryRegions.AddrSpaceStart + MemoryRegions.AddrSpaceSize;
|
2018-03-11 00:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsValidMapPosition(long Position)
|
|
|
|
{
|
|
|
|
return Position >= MemoryRegions.MapRegionAddress &&
|
2018-04-19 04:52:23 +02:00
|
|
|
Position < MemoryRegions.MapRegionAddress + MemoryRegions.MapRegionSize;
|
2018-03-11 00:39:16 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|