2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.State;
|
2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.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-12-01 21:01:59 +01:00
|
|
|
private void SvcSetHeapSize(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong size = threadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if ((size & 0xfffffffe001fffff) != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Heap size 0x{size:x16} is not aligned!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.SetHeapSize(size, out ulong position);
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-02-28 00:45:07 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X1 = position;
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcSetMemoryAttribute(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong position = threadState.X0;
|
|
|
|
ulong size = threadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(position))
|
2018-02-26 02:53:01 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{position:x16} is not page aligned!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
2018-02-26 02:53:01 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-02-26 02:53:01 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
2018-02-26 02:53:01 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
MemoryAttribute attributeMask = (MemoryAttribute)threadState.X2;
|
|
|
|
MemoryAttribute attributeValue = (MemoryAttribute)threadState.X3;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
MemoryAttribute attributes = attributeMask | attributeValue;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (attributes != attributeMask ||
|
|
|
|
(attributes | MemoryAttribute.Uncached) != MemoryAttribute.Uncached)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Invalid memory attributes!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMaskValue);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.SetMemoryAttribute(
|
|
|
|
position,
|
|
|
|
size,
|
|
|
|
attributeMask,
|
|
|
|
attributeValue);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
_memory.StopObservingRegion((long)position, (long)size);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcMapMemory(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong dst = threadState.X0;
|
|
|
|
ulong src = threadState.X1;
|
|
|
|
ulong size = threadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(src | dst))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (src + size <= src || dst + size <= dst)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!currentProcess.MemoryManager.InsideAddrSpace(src, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Src address 0x{src:x16} out of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
if (currentProcess.MemoryManager.OutsideStackRegion(dst, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideHeapRegion (dst, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideAliasRegion (dst, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{dst:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.Map(dst, src, size);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-02-28 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcUnmapMemory(CpuThreadState threadState)
|
2018-02-28 00:45:07 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong dst = threadState.X0;
|
|
|
|
ulong src = threadState.X1;
|
|
|
|
ulong size = threadState.X2;
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(src | dst))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses are not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (src + size <= src || dst + size <= dst)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Addresses outside of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!currentProcess.MemoryManager.InsideAddrSpace(src, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Src address 0x{src:x16} out of range!");
|
2018-02-28 00:45:07 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
if (currentProcess.MemoryManager.OutsideStackRegion(dst, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideHeapRegion (dst, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideAliasRegion (dst, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Dst address 0x{dst:x16} out of range!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidMemRange);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.Unmap(dst, src, size);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcQueryMemory(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
long infoPtr = (long)threadState.X0;
|
|
|
|
ulong position = threadState.X2;
|
|
|
|
|
|
|
|
KMemoryInfo blkInfo = _process.MemoryManager.QueryMemory(position);
|
|
|
|
|
|
|
|
_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);
|
|
|
|
|
|
|
|
threadState.X0 = 0;
|
|
|
|
threadState.X1 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcMapSharedMemory(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
int handle = (int)threadState.X0;
|
|
|
|
ulong address = threadState.X1;
|
|
|
|
ulong size = threadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(address))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} is not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (address + size <= address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{address:x16} / size 0x{size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
MemoryPermission permission = (MemoryPermission)threadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if ((permission | MemoryPermission.Write) != MemoryPermission.ReadAndWrite)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid permission {permission}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KSharedMemory sharedMemory = currentProcess.HandleTable.GetObject<KSharedMemory>(handle);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (sharedMemory == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{handle:x8}!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (currentProcess.MemoryManager.IsInvalidRegion (address, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideHeapRegion (address, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideAliasRegion(address, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = sharedMemory.MapIntoProcess(
|
|
|
|
currentProcess.MemoryManager,
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
currentProcess,
|
|
|
|
permission);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcUnmapSharedMemory(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
int handle = (int)threadState.X0;
|
|
|
|
ulong address = threadState.X1;
|
|
|
|
ulong size = threadState.X2;
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(address))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} is not page aligned!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (address + size <= address)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{address:x16} / size 0x{size:x16}!");
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KSharedMemory sharedMemory = currentProcess.HandleTable.GetObject<KSharedMemory>(handle);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (sharedMemory == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid shared memory handle 0x{handle:x8}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (currentProcess.MemoryManager.IsInvalidRegion (address, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideHeapRegion (address, size) ||
|
|
|
|
currentProcess.MemoryManager.InsideAliasRegion(address, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} out of range!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = sharedMemory.UnmapFromProcess(
|
|
|
|
currentProcess.MemoryManager,
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
currentProcess);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcCreateTransferMemory(CpuThreadState threadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong address = threadState.X1;
|
|
|
|
ulong size = threadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(address))
|
2018-03-11 00:39:16 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} is not page aligned!");
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
|
|
|
if (address + size <= address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{address:x16} / size 0x{size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
MemoryPermission permission = (MemoryPermission)threadState.X3;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (permission > MemoryPermission.ReadAndWrite || permission == MemoryPermission.Write)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid permission {permission}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPermission);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
_process.MemoryManager.ReserveTransferMemory(address, size, permission);
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KTransferMemory transferMemory = new KTransferMemory(address, size);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.HandleTable.GenerateHandle(transferMemory, out int handle);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (uint)result;
|
|
|
|
threadState.X1 = (ulong)handle;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-03-11 00:39:16 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcMapPhysicalMemory(CpuThreadState threadState)
|
2018-05-22 22:40:46 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong address = threadState.X0;
|
|
|
|
ulong size = threadState.X1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(address))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} is not page aligned!");
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-12-01 21:01:59 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (address + size <= address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{address:x16} / size 0x{size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if ((currentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"System resource size is zero.");
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!currentProcess.MemoryManager.InsideAddrSpace (address, size) ||
|
|
|
|
currentProcess.MemoryManager.OutsideAliasRegion(address, size))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address {address:x16}.");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.MapPhysicalMemory(address, size);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-05-22 22:40:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private void SvcUnmapPhysicalMemory(CpuThreadState threadState)
|
2018-05-22 22:40:46 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
ulong address = threadState.X0;
|
|
|
|
ulong size = threadState.X1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(address))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Address 0x{address:x16} is not page aligned!");
|
2018-05-22 22:40:46 +02:00
|
|
|
|
2018-12-01 21:01:59 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!PageAligned(size) || size == 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Size 0x{size:x16} is not page aligned or is zero!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (address + size <= address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid region address 0x{address:x16} / size 0x{size:x16}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if ((currentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"System resource size is zero.");
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (!currentProcess.MemoryManager.InsideAddrSpace (address, size) ||
|
|
|
|
currentProcess.MemoryManager.OutsideAliasRegion(address, size))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address {address:x16}.");
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
KernelResult result = _process.MemoryManager.UnmapPhysicalMemory(address, size);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
threadState.X0 = (ulong)result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 21:01:59 +01:00
|
|
|
private static bool PageAligned(ulong position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-01 21:01:59 +01:00
|
|
|
return (position & (KMemoryManager.PageSize - 1)) == 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|