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