2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
2018-10-17 13:15:50 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-16 19:47:36 -04:00
|
|
|
using Ryujinx.HLE.Exceptions;
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Services;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
2018-02-19 14:37:13 -05:00
|
|
|
using System.Threading;
|
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-12-06 06:16:24 -05:00
|
|
|
private void SvcExitProcess(CpuThreadState threadState)
|
2018-02-27 18:45:07 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_system.Scheduler.GetCurrentProcess().Terminate();
|
2018-02-27 18:45:07 -05:00
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SignalEvent64(CpuThreadState threadState)
|
2018-03-05 10:58:19 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)SignalEvent((int)threadState.X0);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-03-05 10:58:19 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult SignalEvent(int handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KWritableEvent writableEvent = _process.HandleTable.GetObject<KWritableEvent>(handle);
|
2018-03-05 10:58:19 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (writableEvent != null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
writableEvent.Signal();
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = KernelResult.Success;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = KernelResult.InvalidHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Operation failed with error: " + result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void ClearEvent64(CpuThreadState threadState)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)ClearEvent((int)threadState.X0);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult ClearEvent(int handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KWritableEvent writableEvent = _process.HandleTable.GetObject<KWritableEvent>(handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (writableEvent == null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KReadableEvent readableEvent = _process.HandleTable.GetObject<KReadableEvent>(handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = readableEvent?.Clear() ?? KernelResult.InvalidHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = writableEvent.Clear();
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Operation failed with error: " + result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-03-05 10:58:19 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcCloseHandle(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int handle = (int)threadState.X0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
object obj = _process.HandleTable.GetObject<object>(handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_process.HandleTable.CloseHandle(handle);
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (obj == null)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid handle 0x{handle:x8}!");
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (obj is KSession session)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
session.Dispose();
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (obj is KTransferMemory transferMemory)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_process.MemoryManager.ResetTransferMemory(
|
|
|
|
transferMemory.Address,
|
|
|
|
transferMemory.Size);
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void ResetSignal64(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)ResetSignal((int)threadState.X0);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult ResetSignal(int handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KReadableEvent readableEvent = currentProcess.HandleTable.GetObject<KReadableEvent>(handle);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result;
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (readableEvent != null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = readableEvent.ClearIfSignaled();
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess process = currentProcess.HandleTable.GetKProcess(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (process != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = process.ClearIfNotExited();
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = KernelResult.InvalidHandle;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.InvalidState)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintDebug(LogClass.KernelSvc, "Operation failed with error: " + result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Operation failed with error: " + result + "!");
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcGetSystemTick(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = threadState.CntpctEl0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcConnectToNamedPort(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long stackPtr = (long)threadState.X0;
|
|
|
|
long namePtr = (long)threadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
string name = MemoryHelper.ReadAsciiString(_memory, namePtr, 8);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
//TODO: Validate that app has perms to access the service, and that the service
|
|
|
|
//actually exists, return error codes otherwise.
|
2018-12-06 06:16:24 -05:00
|
|
|
KSession session = new KSession(ServiceFactory.MakeService(_system, name), name);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (_process.HandleTable.GenerateHandle(session, out int handle) != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2018-04-04 20:01:36 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = 0;
|
|
|
|
threadState.X1 = (uint)handle;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcSendSyncRequest(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
SendSyncRequest(threadState, threadState.Tpidr, 0x100, (int)threadState.X0);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcSendSyncRequestWithUserBuffer(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-19 14:58:46 -04:00
|
|
|
SendSyncRequest(
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState,
|
|
|
|
(long)threadState.X0,
|
|
|
|
(long)threadState.X1,
|
|
|
|
(int)threadState.X2);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SendSyncRequest(CpuThreadState threadState, long messagePtr, long size, int handle)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
byte[] messageData = _memory.ReadBytes(messagePtr, size);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KSession session = _process.HandleTable.GetObject<KSession>(handle);
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (session != null)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_system.CriticalSection.Enter();
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-08-14 18:02:42 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.SignaledObj = null;
|
|
|
|
currentThread.ObjSyncResult = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
IpcMessage message = new IpcMessage(messageData, messagePtr);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-09-18 19:36:43 -04:00
|
|
|
ThreadPool.QueueUserWorkItem(ProcessIpcRequest, new HleIpcMessage(
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread,
|
|
|
|
session,
|
|
|
|
message,
|
|
|
|
messagePtr));
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_system.ThreadCounter.AddCount();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)currentThread.ObjSyncResult;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid session handle 0x{handle:x8}!");
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void ProcessIpcRequest(object state)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
HleIpcMessage ipcMessage = (HleIpcMessage)state;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ipcMessage.Thread.ObjSyncResult = (int)IpcHandler.IpcCall(
|
|
|
|
_device,
|
|
|
|
_process,
|
|
|
|
_memory,
|
|
|
|
ipcMessage.Session,
|
|
|
|
ipcMessage.Message,
|
|
|
|
ipcMessage.MessagePtr);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_system.ThreadCounter.Signal();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ipcMessage.Thread.Reschedule(ThreadSchedState.Running);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void GetProcessId64(CpuThreadState threadState)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int handle = (int)threadState.X1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = GetProcessId(handle, out long pid);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)result;
|
|
|
|
threadState.X1 = (ulong)pid;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult GetProcessId(int handle, out long pid)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess process = currentProcess.HandleTable.GetKProcess(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (process == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread thread = currentProcess.HandleTable.GetKThread(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (thread != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
process = thread.Owner;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: KDebugEvent.
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
pid = process?.Pid ?? 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return process != null
|
2018-11-28 17:18:09 -05:00
|
|
|
? KernelResult.Success
|
|
|
|
: KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcBreak(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long reason = (long)threadState.X0;
|
|
|
|
long unknown = (long)threadState.X1;
|
|
|
|
long info = (long)threadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((reason & (1 << 31)) == 0)
|
2018-10-08 14:53:08 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.PrintGuestStackTrace();
|
2018-04-22 01:48:17 -04:00
|
|
|
|
2018-10-08 14:53:08 -04:00
|
|
|
throw new GuestBrokeExecutionException();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Logger.PrintInfo(LogClass.KernelSvc, "Debugger triggered.");
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.PrintGuestStackTrace();
|
2018-10-08 14:53:08 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SvcOutputDebugString(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long position = (long)threadState.X0;
|
|
|
|
long size = (long)threadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
string str = MemoryHelper.ReadAsciiString(_memory, position, size);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, str);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void GetInfo64(CpuThreadState threadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long stackPtr = (long)threadState.X0;
|
|
|
|
uint id = (uint)threadState.X1;
|
|
|
|
int handle = (int)threadState.X2;
|
|
|
|
long subId = (long)threadState.X3;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = GetInfo(id, handle, subId, out long value);
|
2018-02-06 18:28:32 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
threadState.X0 = (ulong)result;
|
|
|
|
threadState.X1 = (ulong)value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult GetInfo(uint id, int handle, long subId, out long value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
value = 0;
|
2018-02-06 18:28:32 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (id)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-27 18:45:07 -05:00
|
|
|
case 0:
|
2018-11-28 17:18:09 -05:00
|
|
|
case 1:
|
2018-02-27 18:45:07 -05:00
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
2018-11-28 17:18:09 -05:00
|
|
|
case 12:
|
|
|
|
case 13:
|
|
|
|
case 14:
|
|
|
|
case 15:
|
|
|
|
case 16:
|
|
|
|
case 17:
|
|
|
|
case 18:
|
|
|
|
case 20:
|
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess process = currentProcess.HandleTable.GetKProcess(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (process == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (id)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
case 0: value = process.Capabilities.AllowedCpuCoresMask; break;
|
|
|
|
case 1: value = process.Capabilities.AllowedThreadPriosMask; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 2: value = (long)process.MemoryManager.AliasRegionStart; break;
|
|
|
|
case 3: value = (long)(process.MemoryManager.AliasRegionEnd -
|
|
|
|
process.MemoryManager.AliasRegionStart); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 4: value = (long)process.MemoryManager.HeapRegionStart; break;
|
|
|
|
case 5: value = (long)(process.MemoryManager.HeapRegionEnd -
|
|
|
|
process.MemoryManager.HeapRegionStart); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 6: value = (long)process.GetMemoryCapacity(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 7: value = (long)process.GetMemoryUsage(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 12: value = (long)process.MemoryManager.GetAddrSpaceBaseAddr(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 13: value = (long)process.MemoryManager.GetAddrSpaceSize(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 14: value = (long)process.MemoryManager.StackRegionStart; break;
|
|
|
|
case 15: value = (long)(process.MemoryManager.StackRegionEnd -
|
|
|
|
process.MemoryManager.StackRegionStart); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 16: value = (long)process.PersonalMmHeapPagesCount * KMemoryManager.PageSize; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
case 17:
|
2018-12-06 06:16:24 -05:00
|
|
|
if (process.PersonalMmHeapPagesCount != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
value = process.MemoryManager.GetMmUsedPages() * KMemoryManager.PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 18: value = process.TitleId; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 20: value = (long)process.UserExceptionContextAddress; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 21: value = (long)process.GetMemoryCapacityWithoutPersonalMmHeap(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case 22: value = (long)process.GetMemoryUsageWithoutPersonalMmHeap(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
|
|
|
case 8:
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = _system.Scheduler.GetCurrentProcess().Debug ? 1 : 0;
|
2018-02-27 18:45:07 -05:00
|
|
|
|
|
|
|
break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
case 9:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (currentProcess.ResourceLimit != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleTable handleTable = currentProcess.HandleTable;
|
|
|
|
KResourceLimit resourceLimit = currentProcess.ResourceLimit;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = handleTable.GenerateHandle(resourceLimit, out int resLimHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = (uint)resLimHandle;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-02-27 18:45:07 -05:00
|
|
|
|
|
|
|
break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int currentCore = _system.Scheduler.GetCurrentThread().CurrentCore;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != -1 && subId != currentCore)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = _system.Scheduler.CoreContexts[currentCore].TotalIdleTimeTicks;
|
2018-06-10 20:46:42 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((ulong)subId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = currentProcess.RandomEntropy[subId];
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-05-22 16:40:46 -04:00
|
|
|
break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 0xf0000002u:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId < -1 || subId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread thread = _system.Scheduler.GetCurrentProcess().HandleTable.GetKThread(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (thread == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int currentCore = currentThread.CurrentCore;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != -1 && subId != currentCore)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KCoreContext coreContext = _system.Scheduler.CoreContexts[currentCore];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
long timeDelta = PerformanceCounter.ElapsedMilliseconds - coreContext.LastContextSwitchTime;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (subId != -1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
value = KTimeManager.ConvertMillisecondsToTicks(timeDelta);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long totalTimeRunning = thread.TotalTimeRunning;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (thread == currentThread)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
totalTimeRunning += timeDelta;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = KTimeManager.ConvertMillisecondsToTicks(totalTimeRunning);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-04-22 01:48:17 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
default: return KernelResult.InvalidEnumValue;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void CreateEvent64(CpuThreadState state)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = CreateEvent(out int wEventHandle, out int rEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
state.X0 = (ulong)result;
|
|
|
|
state.X1 = (ulong)wEventHandle;
|
|
|
|
state.X2 = (ulong)rEventHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult CreateEvent(out int wEventHandle, out int rEventHandle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KEvent Event = new KEvent(_system);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = _process.HandleTable.GenerateHandle(Event.WritableEvent, out wEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = _process.HandleTable.GenerateHandle(Event.ReadableEvent, out rEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_process.HandleTable.CloseHandle(wEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
rEventHandle = 0;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void GetProcessList64(CpuThreadState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address = state.X1;
|
|
|
|
int maxOut = (int)state.X2;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = GetProcessList(address, maxOut, out int count);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
state.X0 = (ulong)result;
|
|
|
|
state.X1 = (ulong)count;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult GetProcessList(ulong address, int maxCount, out int count)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
count = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((maxCount >> 28) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (maxCount != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong copySize = (ulong)maxCount * 8;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (address + copySize <= address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (currentProcess.MemoryManager.OutsideAddrSpace(address, copySize))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int copyCount = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_system.Processes)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
foreach (KProcess process in _system.Processes.Values)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (copyCount < maxCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!KernelTransfer.KernelToUserInt64(_system, (long)address + copyCount * 8, process.Pid))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.UserCopyFailed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
copyCount++;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
count = copyCount;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void GetSystemInfo64(CpuThreadState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
uint id = (uint)state.X1;
|
|
|
|
int handle = (int)state.X2;
|
|
|
|
long subId = (long)state.X3;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = GetSystemInfo(id, handle, subId, out long value);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
state.X0 = (ulong)result;
|
|
|
|
state.X1 = (ulong)value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult GetSystemInfo(uint id, int handle, long subId, out long value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
value = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (id > 2)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidEnumValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (id < 2)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((ulong)subId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryRegionManager region = _system.MemoryRegions[subId];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (id)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
//Memory region capacity.
|
2018-12-06 06:16:24 -05:00
|
|
|
case 0: value = (long)region.Size; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
//Memory region free space.
|
|
|
|
case 1:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong freePagesCount = region.GetFreePages();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
value = (long)(freePagesCount * KMemoryManager.PageSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* if (Id == 2) */
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((ulong)subId > 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (subId)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
case 0: value = _system.PrivilegedProcessLowestId; break;
|
|
|
|
case 1: value = _system.PrivilegedProcessHighestId; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void CreatePort64(CpuThreadState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int maxSessions = (int)state.X2;
|
|
|
|
bool isLight = (state.X3 & 1) != 0;
|
|
|
|
long nameAddress = (long)state.X4;
|
|
|
|
|
|
|
|
KernelResult result = CreatePort(
|
|
|
|
maxSessions,
|
|
|
|
isLight,
|
|
|
|
nameAddress,
|
|
|
|
out int serverPortHandle,
|
|
|
|
out int clientPortHandle);
|
|
|
|
|
|
|
|
state.X0 = (ulong)result;
|
|
|
|
state.X1 = (ulong)serverPortHandle;
|
|
|
|
state.X2 = (ulong)clientPortHandle;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult CreatePort(
|
2018-12-06 06:16:24 -05:00
|
|
|
int maxSessions,
|
|
|
|
bool isLight,
|
|
|
|
long nameAddress,
|
|
|
|
out int serverPortHandle,
|
|
|
|
out int clientPortHandle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
serverPortHandle = clientPortHandle = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (maxSessions < 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPort port = new KPort(_system);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
port.Initialize(maxSessions, isLight, nameAddress);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = currentProcess.HandleTable.GenerateHandle(port.ClientPort, out clientPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = currentProcess.HandleTable.GenerateHandle(port.ServerPort, out serverPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
currentProcess.HandleTable.CloseHandle(clientPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void ManageNamedPort64(CpuThreadState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long nameAddress = (long)state.X1;
|
|
|
|
int maxSessions = (int)state.X2;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = ManageNamedPort(nameAddress, maxSessions, out int handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
state.X0 = (ulong)result;
|
|
|
|
state.X1 = (ulong)handle;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult ManageNamedPort(long nameAddress, int maxSessions, out int handle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
handle = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!KernelTransfer.UserToKernelString(_system, nameAddress, 12, out string name))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.UserCopyFailed;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (maxSessions < 0 || name.Length > 11)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (maxSessions == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return KClientPort.RemoveName(_system, name);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPort port = new KPort(_system);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = currentProcess.HandleTable.GenerateHandle(port.ServerPort, out handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
port.Initialize(maxSessions, false, 0);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = port.SetName(name);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
currentProcess.HandleTable.CloseHandle(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-24 18:09:10 -05:00
|
|
|
}
|