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-04 19:52:39 -05:00
|
|
|
private void SvcExitProcess(CpuThreadState ThreadState)
|
2018-02-27 18:45:07 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
System.Scheduler.GetCurrentProcess().Terminate();
|
2018-02-27 18:45:07 -05:00
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SignalEvent64(CpuThreadState ThreadState)
|
2018-03-05 10:58:19 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
private KernelResult SignalEvent(int Handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KWritableEvent WritableEvent = Process.HandleTable.GetObject<KWritableEvent>(Handle);
|
2018-03-05 10:58:19 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (WritableEvent != null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
WritableEvent.Signal();
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = KernelResult.Success;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = KernelResult.InvalidHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Operation failed with error: " + Result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void ClearEvent64(CpuThreadState ThreadState)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = (ulong)ClearEvent((int)ThreadState.X0);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult ClearEvent(int Handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KWritableEvent WritableEvent = Process.HandleTable.GetObject<KWritableEvent>(Handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (WritableEvent == null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KReadableEvent ReadableEvent = Process.HandleTable.GetObject<KReadableEvent>(Handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = ReadableEvent?.Clear() ?? KernelResult.InvalidHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = WritableEvent.Clear();
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, "Operation failed with error: " + Result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-03-05 10:58:19 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcCloseHandle(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Handle = (int)ThreadState.X0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
object Obj = Process.HandleTable.GetObject<object>(Handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Process.HandleTable.CloseHandle(Handle);
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Obj == null)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid handle 0x{Handle:x8}!");
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Obj is KSession Session)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Session.Dispose();
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
2018-12-04 19:52:39 -05:00
|
|
|
else if (Obj is KTransferMemory TransferMemory)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void ResetSignal64(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
private KernelResult ResetSignal(int Handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KReadableEvent ReadableEvent = CurrentProcess.HandleTable.GetObject<KReadableEvent>(Handle);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result;
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (ReadableEvent != null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = ReadableEvent.ClearIfSignaled();
|
2018-03-19 14:58:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess Process = CurrentProcess.HandleTable.GetKProcess(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Process != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = Process.ClearIfNotExited();
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (Result == KernelResult.InvalidState)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintDebug(LogClass.KernelSvc, "Operation failed with error: " + Result + "!");
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-12-04 19:52:39 -05:00
|
|
|
else if (Result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcGetSystemTick(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = ThreadState.CntpctEl0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcConnectToNamedPort(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long StackPtr = (long)ThreadState.X0;
|
|
|
|
long NamePtr = (long)ThreadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
KSession Session = new KSession(ServiceFactory.MakeService(System, Name), Name);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = 0;
|
|
|
|
ThreadState.X1 = (uint)Handle;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcSendSyncRequest(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
SendSyncRequest(ThreadState, ThreadState.Tpidr, 0x100, (int)ThreadState.X0);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
ThreadState,
|
|
|
|
(long)ThreadState.X0,
|
|
|
|
(long)ThreadState.X1,
|
|
|
|
(int)ThreadState.X2);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SendSyncRequest(CpuThreadState ThreadState, long MessagePtr, long Size, int Handle)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
byte[] MessageData = Memory.ReadBytes(MessagePtr, Size);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KSession Session = Process.HandleTable.GetObject<KSession>(Handle);
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Session != null)
|
2018-03-19 14:58:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
System.CriticalSection.Enter();
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
2018-08-14 18:02:42 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
CurrentThread.SignaledObj = null;
|
|
|
|
CurrentThread.ObjSyncResult = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
CurrentThread,
|
|
|
|
Session,
|
|
|
|
Message,
|
|
|
|
MessagePtr));
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
System.ThreadCounter.AddCount();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = (ulong)CurrentThread.ObjSyncResult;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid session handle 0x{Handle:x8}!");
|
2018-02-19 14:37:13 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
private void ProcessIpcRequest(object State)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
HleIpcMessage IpcMessage = (HleIpcMessage)State;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
System.ThreadCounter.Signal();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
IpcMessage.Thread.Reschedule(ThreadSchedState.Running);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void GetProcessId64(CpuThreadState ThreadState)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Handle = (int)ThreadState.X1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = GetProcessId(Handle, out long Pid);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
ThreadState.X1 = (ulong)Pid;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult GetProcessId(int Handle, out long Pid)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess Process = CurrentProcess.HandleTable.GetKProcess(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Process == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KThread Thread = CurrentProcess.HandleTable.GetKThread(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Thread != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Process = Thread.Owner;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: KDebugEvent.
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Pid = Process?.Pid ?? 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Process != null
|
2018-11-28 17:18:09 -05:00
|
|
|
? KernelResult.Success
|
|
|
|
: KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcBreak(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Reason & (1 << 31)) == 0)
|
2018-10-08 14:53:08 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
CurrentThread.PrintGuestStackTrace();
|
2018-10-08 14:53:08 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void SvcOutputDebugString(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
long Size = (long)ThreadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
string Str = MemoryHelper.ReadAsciiString(Memory, Position, Size);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Logger.PrintWarning(LogClass.KernelSvc, Str);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void GetInfo64(CpuThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
KernelResult Result = GetInfo(Id, Handle, SubId, out long Value);
|
2018-02-06 18:28:32 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ThreadState.X0 = (ulong)Result;
|
|
|
|
ThreadState.X1 = (ulong)Value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult GetInfo(uint Id, int Handle, long SubId, out long Value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Value = 0;
|
2018-02-06 18:28:32 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (SubId != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess Process = CurrentProcess.HandleTable.GetKProcess(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Process == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
switch (Id)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -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-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
case 6: Value = (long)Process.GetMemoryCapacity(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 7: Value = (long)Process.GetMemoryUsage(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 12: Value = (long)Process.MemoryManager.GetAddrSpaceBaseAddr(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 13: Value = (long)Process.MemoryManager.GetAddrSpaceSize(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
case 16: Value = (long)Process.PersonalMmHeapPagesCount * KMemoryManager.PageSize; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
case 17:
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Process.PersonalMmHeapPagesCount != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Value = Process.MemoryManager.GetMmUsedPages() * KMemoryManager.PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 18: Value = Process.TitleId; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 20: Value = (long)Process.UserExceptionContextAddress; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
case 21: Value = (long)Process.GetMemoryCapacityWithoutPersonalMmHeap(); break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -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-04 19:52:39 -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-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (Handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SubId != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (CurrentProcess.ResourceLimit != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KHandleTable HandleTable = CurrentProcess.HandleTable;
|
|
|
|
KResourceLimit ResourceLimit = CurrentProcess.ResourceLimit;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = HandleTable.GenerateHandle(ResourceLimit, out int ResLimHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (Handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int CurrentCore = System.Scheduler.GetCurrentThread().CurrentCore;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SubId != -1 && SubId != CurrentCore)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (Handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((ulong)SubId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (SubId < -1 || SubId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KThread Thread = System.Scheduler.GetCurrentProcess().HandleTable.GetKThread(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Thread == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int CurrentCore = CurrentThread.CurrentCore;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
KCoreContext CoreContext = System.Scheduler.CoreContexts[CurrentCore];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
long TimeDelta = PerformanceCounter.ElapsedMilliseconds - CoreContext.LastContextSwitchTime;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SubId != -1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Value = KTimeManager.ConvertMillisecondsToTicks(TimeDelta);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long TotalTimeRunning = Thread.TotalTimeRunning;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Thread == CurrentThread)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
TotalTimeRunning += TimeDelta;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
private void CreateEvent64(CpuThreadState State)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = CreateEvent(out int WEventHandle, out int REventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
State.X0 = (ulong)Result;
|
|
|
|
State.X1 = (ulong)WEventHandle;
|
|
|
|
State.X2 = (ulong)REventHandle;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult CreateEvent(out int WEventHandle, out int REventHandle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KEvent Event = new KEvent(System);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = Process.HandleTable.GenerateHandle(Event.WritableEvent, out WEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result == KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = Process.HandleTable.GenerateHandle(Event.ReadableEvent, out REventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Process.HandleTable.CloseHandle(WEventHandle);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
REventHandle = 0;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void GetProcessList64(CpuThreadState State)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
ulong Address = State.X1;
|
|
|
|
int MaxOut = (int)State.X2;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = GetProcessList(Address, MaxOut, out int Count);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
State.X0 = (ulong)Result;
|
|
|
|
State.X1 = (ulong)Count;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult GetProcessList(ulong Address, int MaxCount, out int Count)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Count = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((MaxCount >> 28) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (MaxCount != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ulong CopySize = (ulong)MaxCount * 8;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Address + CopySize <= Address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (CurrentProcess.MemoryManager.OutsideAddrSpace(Address, CopySize))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int CopyCount = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
lock (System.Processes)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
foreach (KProcess Process in System.Processes.Values)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (CopyCount < MaxCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
CopyCount++;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Count = CopyCount;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void GetSystemInfo64(CpuThreadState State)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
KernelResult Result = GetSystemInfo(Id, Handle, SubId, out long Value);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
State.X0 = (ulong)Result;
|
|
|
|
State.X1 = (ulong)Value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult GetSystemInfo(uint Id, int Handle, long SubId, out long Value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Value = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Id > 2)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidEnumValue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Handle != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Id < 2)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((ulong)SubId > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KMemoryRegionManager Region = System.MemoryRegions[SubId];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
switch (Id)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
//Memory region capacity.
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
ulong FreePagesCount = Region.GetFreePages();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Value = (long)(FreePagesCount * KMemoryManager.PageSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* if (Id == 2) */
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((ulong)SubId > 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
switch (SubId)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
private void CreatePort64(CpuThreadState State)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
int MaxSessions,
|
|
|
|
bool IsLight,
|
|
|
|
long NameAddress,
|
|
|
|
out int ServerPortHandle,
|
|
|
|
out int ClientPortHandle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
ServerPortHandle = ClientPortHandle = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (MaxSessions < 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KPort Port = new KPort(System);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Port.Initialize(MaxSessions, IsLight, NameAddress);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = CurrentProcess.HandleTable.GenerateHandle(Port.ClientPort, out ClientPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = CurrentProcess.HandleTable.GenerateHandle(Port.ServerPort, out ServerPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
CurrentProcess.HandleTable.CloseHandle(ClientPortHandle);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void ManageNamedPort64(CpuThreadState State)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long NameAddress = (long)State.X1;
|
|
|
|
int MaxSessions = (int)State.X2;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = ManageNamedPort(NameAddress, MaxSessions, out int Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
State.X0 = (ulong)Result;
|
|
|
|
State.X1 = (ulong)Handle;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult ManageNamedPort(long NameAddress, int MaxSessions, out int Handle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Handle = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (!KernelTransfer.UserToKernelString(System, NameAddress, 12, out string Name))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.UserCopyFailed;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (MaxSessions < 0 || Name.Length > 11)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (MaxSessions == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return KClientPort.RemoveName(System, Name);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KPort Port = new KPort(System);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = CurrentProcess.HandleTable.GenerateHandle(Port.ServerPort, out Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Port.Initialize(MaxSessions, false, 0);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = Port.SetName(Name);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
CurrentProcess.HandleTable.CloseHandle(Handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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
|
|
|
}
|