2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.State;
|
2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Logging;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-03-05 20:18:37 +01:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.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-08-21 18:05:12 +02:00
|
|
|
ulong Size = ThreadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-21 18:05:12 +02:00
|
|
|
if ((Size & 0xFFFFFFFE001FFFFF) != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Heap size 0x{Size:x16} is not aligned!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:05:12 +02:00
|
|
|
long Result = Process.MemoryManager.TrySetHeapSize((long)Size, out long Position);
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
|
|
|
|
if (Result == 0)
|
2018-02-28 00:45:07 +01:00
|
|
|
{
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X1 = (ulong)Position;
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
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;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Position))
|
2018-02-26 02:53:01 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
2018-02-26 02:53:01 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-02-26 02:53:01 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
2018-02-26 02:53:01 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryAttribute AttributeMask = (MemoryAttribute)ThreadState.X2;
|
|
|
|
MemoryAttribute AttributeValue = (MemoryAttribute)ThreadState.X3;
|
|
|
|
|
|
|
|
MemoryAttribute Attributes = AttributeMask | AttributeValue;
|
|
|
|
|
|
|
|
if (Attributes != AttributeMask ||
|
|
|
|
(Attributes | MemoryAttribute.Uncached) != MemoryAttribute.Uncached)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, "Invalid memory attributes!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMaskValue);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.SetMemoryAttribute(
|
|
|
|
Position,
|
|
|
|
Size,
|
|
|
|
AttributeMask,
|
|
|
|
AttributeValue);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Memory.StopObservingRegion(Position, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
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-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Src | Dst))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if ((ulong)(Src + Size) <= (ulong)Src || (ulong)(Dst + Size) <= (ulong)Dst)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsideAddrSpace(Src, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Src address 0x{Src:x16} out of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsideNewMapRegion(Dst, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{Dst:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.Map(Src, Dst, Size);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
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-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Src | Dst))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)(Src + Size) <= (ulong)Src || (ulong)(Dst + Size) <= (ulong)Dst)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!InsideAddrSpace(Src, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Src address 0x{Src:x16} out of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!InsideNewMapRegion(Dst, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{Dst:x16} out of range!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.Unmap(Src, Dst, Size);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
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
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
KMemoryInfo BlkInfo = Process.MemoryManager.QueryMemory(Position);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
Memory.WriteInt64(InfoPtr + 0x00, BlkInfo.Position);
|
|
|
|
Memory.WriteInt64(InfoPtr + 0x08, BlkInfo.Size);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x10, (int)BlkInfo.State & 0xff);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x14, (int)BlkInfo.Attribute);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x18, (int)BlkInfo.Permission);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x1c, BlkInfo.IpcRefCount);
|
|
|
|
Memory.WriteInt32(InfoPtr + 0x20, BlkInfo.DeviceRefCount);
|
2018-02-28 00:45:07 +01:00
|
|
|
Memory.WriteInt32(InfoPtr + 0x24, 0);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
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-08-15 20:59:51 +02:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
long Position = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Position))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)(Position + Size) <= (ulong)Position)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Position:x16} / size 0x{Size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryPermission Permission = (MemoryPermission)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if ((Permission | MemoryPermission.Write) != MemoryPermission.ReadAndWrite)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid permission {Permission}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPermission);
|
2018-03-03 02:49:17 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-08-04 23:38:49 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
KSharedMemory SharedMemory = Process.HandleTable.GetData<KSharedMemory>(Handle);
|
|
|
|
|
|
|
|
if (SharedMemory == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{Handle:x8}!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!InsideAddrSpace(Position, Size) || InsideMapRegion(Position, Size) || InsideHeapRegion(Position, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SharedMemory.Size != Size)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} does not match shared memory size 0x{SharedMemory.Size:16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.MapSharedMemory(SharedMemory, Permission, Position);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcUnmapSharedMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-08-15 20:59:51 +02:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
long Position = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Position))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if ((ulong)(Position + Size) <= (ulong)Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Position:x16} / size 0x{Size:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
KSharedMemory SharedMemory = Process.HandleTable.GetData<KSharedMemory>(Handle);
|
|
|
|
|
|
|
|
if (SharedMemory == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{Handle:x8}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!InsideAddrSpace(Position, Size) || InsideMapRegion(Position, Size) || InsideHeapRegion(Position, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.UnmapSharedMemory(Position, Size);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcCreateTransferMemory(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-08-15 20:59:51 +02:00
|
|
|
long Position = (long)ThreadState.X1;
|
|
|
|
long Size = (long)ThreadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if (!PageAligned(Position))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
if ((ulong)(Position + Size) <= (ulong)Position)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Position:x16} / size 0x{Size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryPermission Permission = (MemoryPermission)ThreadState.X3;
|
|
|
|
|
|
|
|
if (Permission > MemoryPermission.ReadAndWrite || Permission == MemoryPermission.Write)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid permission {Permission}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPermission);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
Process.MemoryManager.ReserveTransferMemory(Position, Size, Permission);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
KTransferMemory TransferMemory = new KTransferMemory(Position, Size);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
int Handle = Process.HandleTable.OpenHandle(TransferMemory);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X1 = (ulong)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;
|
2018-08-15 20:59:51 +02:00
|
|
|
long Size = (long)ThreadState.X1;
|
|
|
|
|
|
|
|
if (!PageAligned(Position))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)(Position + Size) <= (ulong)Position)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Position:x16} / size 0x{Size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsideAddrSpace(Position, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address {Position:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.MapPhysicalMemory(Position, Size);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-05-22 22:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcUnmapPhysicalMemory(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long Position = (long)ThreadState.X0;
|
2018-08-15 20:59:51 +02:00
|
|
|
long Size = (long)ThreadState.X1;
|
|
|
|
|
|
|
|
if (!PageAligned(Position))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)(Position + Size) <= (ulong)Position)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Position:x16} / size 0x{Size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsideAddrSpace(Position, Size))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address {Position:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Result = Process.MemoryManager.UnmapPhysicalMemory(Position, Size);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool PageAligned(long Position)
|
|
|
|
{
|
|
|
|
return (Position & (KMemoryManager.PageSize - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool InsideAddrSpace(long Position, long Size)
|
|
|
|
{
|
|
|
|
ulong Start = (ulong)Position;
|
|
|
|
ulong End = (ulong)Size + Start;
|
|
|
|
|
|
|
|
return Start >= (ulong)Process.MemoryManager.AddrSpaceStart &&
|
|
|
|
End < (ulong)Process.MemoryManager.AddrSpaceEnd;
|
2018-05-22 22:40:46 +02:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
private bool InsideMapRegion(long Position, long Size)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-15 20:59:51 +02:00
|
|
|
ulong Start = (ulong)Position;
|
|
|
|
ulong End = (ulong)Size + Start;
|
|
|
|
|
|
|
|
return Start >= (ulong)Process.MemoryManager.MapRegionStart &&
|
|
|
|
End < (ulong)Process.MemoryManager.MapRegionEnd;
|
2018-03-11 00:39:16 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
private bool InsideHeapRegion(long Position, long Size)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-08-15 20:59:51 +02:00
|
|
|
ulong Start = (ulong)Position;
|
|
|
|
ulong End = (ulong)Size + Start;
|
|
|
|
|
|
|
|
return Start >= (ulong)Process.MemoryManager.HeapRegionStart &&
|
|
|
|
End < (ulong)Process.MemoryManager.HeapRegionEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool InsideNewMapRegion(long Position, long Size)
|
|
|
|
{
|
|
|
|
ulong Start = (ulong)Position;
|
|
|
|
ulong End = (ulong)Size + Start;
|
|
|
|
|
|
|
|
return Start >= (ulong)Process.MemoryManager.NewMapRegionStart &&
|
|
|
|
End < (ulong)Process.MemoryManager.NewMapRegionEnd;
|
2018-03-11 00:39:16 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|