2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.State;
|
2018-10-17 13:15:50 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-03-05 14:18:37 -05:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcSetHeapSize(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-08-21 12:05:12 -04:00
|
|
|
ulong Size = ThreadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if ((Size & 0xfffffffe001fffff) != 0)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Heap size 0x{Size:x16} is not aligned!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.SetHeapSize(Size, out ulong Position);
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result == KernelResult.Success)
|
2018-02-27 18:45:07 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ThreadState.X1 = Position;
|
2018-02-27 18:45:07 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{Result}\".");
|
2018-02-27 18:45:07 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcSetMemoryAttribute(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Position = ThreadState.X0;
|
|
|
|
ulong Size = ThreadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Position))
|
2018-02-25 20:53:01 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Position:x16} is not page aligned!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
2018-02-25 20:53:01 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-02-25 20:53:01 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
2018-02-25 20:53:01 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute AttributeMask = (MemoryAttribute)ThreadState.X2;
|
|
|
|
MemoryAttribute AttributeValue = (MemoryAttribute)ThreadState.X3;
|
|
|
|
|
|
|
|
MemoryAttribute Attributes = AttributeMask | AttributeValue;
|
|
|
|
|
|
|
|
if (Attributes != AttributeMask ||
|
|
|
|
(Attributes | MemoryAttribute.Uncached) != MemoryAttribute.Uncached)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Invalid memory attributes!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMaskValue);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.SetMemoryAttribute(
|
2018-08-15 14:59:51 -04:00
|
|
|
Position,
|
|
|
|
Size,
|
|
|
|
AttributeMask,
|
|
|
|
AttributeValue);
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{Result}\".");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Memory.StopObservingRegion((long)Position, (long)Size);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcMapMemory(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Dst = ThreadState.X0;
|
|
|
|
ulong Src = ThreadState.X1;
|
|
|
|
ulong Size = ThreadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Src | Dst))
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Src + Size <= Src || Dst + Size <= Dst)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
if (!CurrentProcess.MemoryManager.InsideAddrSpace(Src, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Src address 0x{Src:x16} out of range!");
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (CurrentProcess.MemoryManager.OutsideStackRegion(Dst, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideHeapRegion (Dst, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideAliasRegion (Dst, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{Dst:x16} out of range!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.Map(Dst, Src, Size);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-27 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcUnmapMemory(CpuThreadState ThreadState)
|
2018-02-27 18:45:07 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Dst = ThreadState.X0;
|
|
|
|
ulong Src = ThreadState.X1;
|
|
|
|
ulong Size = ThreadState.X2;
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Src | Dst))
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Src + Size <= Src || Dst + Size <= Dst)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
if (!CurrentProcess.MemoryManager.InsideAddrSpace(Src, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Src address 0x{Src:x16} out of range!");
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (CurrentProcess.MemoryManager.OutsideStackRegion(Dst, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideHeapRegion (Dst, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideAliasRegion (Dst, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{Dst:x16} out of range!");
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.Unmap(Dst, Src, Size);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcQueryMemory(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
long InfoPtr = (long)ThreadState.X0;
|
|
|
|
ulong Position = ThreadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
KMemoryInfo BlkInfo = Process.MemoryManager.QueryMemory(Position);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
Memory.WriteUInt64(InfoPtr + 0x00, BlkInfo.Address);
|
|
|
|
Memory.WriteUInt64(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);
|
|
|
|
Memory.WriteInt32 (InfoPtr + 0x24, 0);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-05 14:18:37 -05:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X1 = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcMapSharedMemory(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
ulong Address = ThreadState.X1;
|
|
|
|
ulong Size = ThreadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (!PageAligned(Address))
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} is not page aligned!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Address + Size <= Address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Address:x16} / size 0x{Size:x16}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryPermission Permission = (MemoryPermission)ThreadState.X3;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if ((Permission | MemoryPermission.Write) != MemoryPermission.ReadAndWrite)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid permission {Permission}!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPermission);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-08-04 17:38:49 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
KSharedMemory SharedMemory = CurrentProcess.HandleTable.GetObject<KSharedMemory>(Handle);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
if (SharedMemory == null)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{Handle:x8}!");
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (CurrentProcess.MemoryManager.IsInvalidRegion (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideHeapRegion (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideAliasRegion(Address, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} out of range!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = SharedMemory.MapIntoProcess(
|
|
|
|
CurrentProcess.MemoryManager,
|
|
|
|
Address,
|
|
|
|
Size,
|
|
|
|
CurrentProcess,
|
|
|
|
Permission);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{Result}\".");
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcUnmapSharedMemory(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
ulong Address = ThreadState.X1;
|
|
|
|
ulong Size = ThreadState.X2;
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (!PageAligned(Address))
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} is not page aligned!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Address + Size <= Address)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Address:x16} / size 0x{Size:x16}!");
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
KSharedMemory SharedMemory = CurrentProcess.HandleTable.GetObject<KSharedMemory>(Handle);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
if (SharedMemory == null)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{Handle:x8}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (CurrentProcess.MemoryManager.IsInvalidRegion (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideHeapRegion (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.InsideAliasRegion(Address, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} out of range!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = SharedMemory.UnmapFromProcess(
|
|
|
|
CurrentProcess.MemoryManager,
|
|
|
|
Address,
|
|
|
|
Size,
|
|
|
|
CurrentProcess);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{Result}\".");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcCreateTransferMemory(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Address = ThreadState.X1;
|
|
|
|
ulong Size = ThreadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (!PageAligned(Address))
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} is not page aligned!");
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Address + Size <= Address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Address:x16} / size 0x{Size:x16}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryPermission Permission = (MemoryPermission)ThreadState.X3;
|
|
|
|
|
|
|
|
if (Permission > MemoryPermission.ReadAndWrite || Permission == MemoryPermission.Write)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid permission {Permission}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPermission);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
Process.MemoryManager.ReserveTransferMemory(Address, Size, Permission);
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KTransferMemory TransferMemory = new KTransferMemory(Address, Size);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-09-23 14:11:46 -04:00
|
|
|
KernelResult Result = Process.HandleTable.GenerateHandle(TransferMemory, out int Handle);
|
2018-04-18 22:52:23 -04:00
|
|
|
|
2018-09-23 14:11:46 -04:00
|
|
|
ThreadState.X0 = (uint)Result;
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X1 = (ulong)Handle;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcMapPhysicalMemory(CpuThreadState ThreadState)
|
2018-05-22 16:40:46 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Address = ThreadState.X0;
|
|
|
|
ulong Size = ThreadState.X1;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (!PageAligned(Address))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} is not page aligned!");
|
2018-05-22 16:40:46 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-05-22 16:40:46 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Address + Size <= Address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Address:x16} / size 0x{Size:x16}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
if ((CurrentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"System resource size is zero.");
|
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CurrentProcess.MemoryManager.InsideAddrSpace (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.OutsideAliasRegion(Address, Size))
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address {Address:x16}.");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.MapPhysicalMemory(Address, Size);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
2018-05-22 16:40:46 -04:00
|
|
|
}
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private void SvcUnmapPhysicalMemory(CpuThreadState ThreadState)
|
2018-05-22 16:40:46 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
ulong Address = ThreadState.X0;
|
|
|
|
ulong Size = ThreadState.X1;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (!PageAligned(Address))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{Address:x16} is not page aligned!");
|
2018-05-22 16:40:46 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-05-22 16:40:46 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PageAligned(Size) || Size == 0)
|
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{Size:x16} is not page aligned or is zero!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Address + Size <= Address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{Address:x16} / size 0x{Size:x16}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
if ((CurrentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"System resource size is zero.");
|
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CurrentProcess.MemoryManager.InsideAddrSpace (Address, Size) ||
|
|
|
|
CurrentProcess.MemoryManager.OutsideAliasRegion(Address, Size))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address {Address:x16}.");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
KernelResult Result = Process.MemoryManager.UnmapPhysicalMemory(Address, Size);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-10-17 13:15:50 -04:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{Result:x}!");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
private static bool PageAligned(ulong Position)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
return (Position & (KMemoryManager.PageSize - 1)) == 0;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|