2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
2018-02-20 21:09:23 +01:00
|
|
|
using Ryujinx.Core.OsHle.Exceptions;
|
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
|
|
|
using Ryujinx.Core.OsHle.Ipc;
|
2018-03-20 21:00:00 +01:00
|
|
|
using Ryujinx.Core.OsHle.Services;
|
2018-02-05 00:08:20 +01:00
|
|
|
using System;
|
2018-02-19 20:37:13 +01:00
|
|
|
using System.Threading;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
using static Ryujinx.Core.OsHle.ErrorCode;
|
|
|
|
|
2018-02-20 21:09:23 +01:00
|
|
|
namespace Ryujinx.Core.OsHle.Svc
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-02-28 00:45:07 +01:00
|
|
|
private const int AllowedCpuIdBitmask = 0b1111;
|
|
|
|
|
|
|
|
private const bool EnableProcessDebugging = false;
|
|
|
|
|
|
|
|
private void SvcExitProcess(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
Ns.Os.ExitProcess(ThreadState.ProcessId);
|
|
|
|
}
|
2018-02-15 13:16:16 +01:00
|
|
|
|
2018-03-05 16:58:19 +01:00
|
|
|
private void SvcClearEvent(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
int Handle = (int)ThreadState.X0;
|
|
|
|
|
|
|
|
//TODO: Implement events.
|
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-03-05 16:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcCloseHandle(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
int Handle = (int)ThreadState.X0;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
object Obj = Process.HandleTable.CloseHandle(Handle);
|
|
|
|
|
|
|
|
if (Obj == null)
|
|
|
|
{
|
2018-04-14 03:02:24 +02:00
|
|
|
Logging.Warn(LogClass.KernelSvc, $"Tried to CloseHandle on invalid handle 0x{Handle:x8}!");
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Obj is KSession Session)
|
|
|
|
{
|
|
|
|
Session.Dispose();
|
|
|
|
}
|
|
|
|
else if (Obj is HTransferMem TMem)
|
|
|
|
{
|
|
|
|
TMem.Memory.Manager.Reprotect(
|
|
|
|
TMem.Position,
|
|
|
|
TMem.Size,
|
|
|
|
TMem.Perm);
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcResetSignal(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
int Handle = (int)ThreadState.X0;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
KEvent Event = Process.HandleTable.GetData<KEvent>(Handle);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
if (Event != null)
|
|
|
|
{
|
|
|
|
Event.Handle.Reset();
|
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-14 03:02:24 +02:00
|
|
|
Logging.Warn(LogClass.KernelSvc, $"Tried to ResetSignal on invalid event handle 0x{Handle:x8}!");
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcWaitSynchronization(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
long HandlesPtr = (long)ThreadState.X1;
|
2018-02-18 20:28:07 +01:00
|
|
|
int HandlesCount = (int)ThreadState.X2;
|
|
|
|
long Timeout = (long)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
WaitHandle[] Handles = new WaitHandle[HandlesCount];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < HandlesCount; Index++)
|
|
|
|
{
|
|
|
|
int Handle = Memory.ReadInt32(HandlesPtr + Index * 4);
|
|
|
|
|
|
|
|
KSynchronizationObject SyncObj = Process.HandleTable.GetData<KSynchronizationObject>(Handle);
|
|
|
|
|
|
|
|
if (SyncObj == null)
|
|
|
|
{
|
2018-04-14 03:02:24 +02:00
|
|
|
Logging.Warn(LogClass.KernelSvc, $"Tried to WaitSynchronization on invalid handle 0x{Handle:x8}!");
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handles[Index] = SyncObj.Handle;
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Process.Scheduler.Suspend(CurrThread.ProcessorId);
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
int HandleIndex;
|
|
|
|
|
|
|
|
ulong Result = 0;
|
|
|
|
|
|
|
|
if (Timeout != -1)
|
|
|
|
{
|
|
|
|
HandleIndex = WaitHandle.WaitAny(Handles, (int)(Timeout / 1000000));
|
|
|
|
|
|
|
|
if (HandleIndex == WaitHandle.WaitTimeout)
|
|
|
|
{
|
|
|
|
Result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HandleIndex = WaitHandle.WaitAny(Handles);
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Process.Scheduler.Resume(CurrThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
ThreadState.X0 = Result;
|
|
|
|
|
|
|
|
if (Result == 0)
|
|
|
|
{
|
|
|
|
ThreadState.X1 = (ulong)HandleIndex;
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcGetSystemTick(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-11 00:51:55 +01:00
|
|
|
ThreadState.X0 = ThreadState.CntpctEl0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcConnectToNamedPort(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long StackPtr = (long)ThreadState.X0;
|
|
|
|
long NamePtr = (long)ThreadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);
|
|
|
|
|
|
|
|
//TODO: Validate that app has perms to access the service, and that the service
|
|
|
|
//actually exists, return error codes otherwise.
|
2018-04-06 07:38:59 +02:00
|
|
|
KSession Session = new KSession(ServiceFactory.MakeService(Name), Name);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
ulong Handle = (ulong)Process.HandleTable.OpenHandle(Session);
|
2018-04-05 02:01:36 +02:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-03-12 05:04:52 +01:00
|
|
|
ThreadState.X1 = Handle;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcSendSyncRequest(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
SendSyncRequest(ThreadState, ThreadState.Tpidr, 0x100, (int)ThreadState.X0);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcSendSyncRequestWithUserBuffer(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
SendSyncRequest(
|
|
|
|
ThreadState,
|
|
|
|
(long)ThreadState.X0,
|
|
|
|
(long)ThreadState.X1,
|
|
|
|
(int)ThreadState.X2);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private void SendSyncRequest(AThreadState ThreadState, long CmdPtr, long Size, int Handle)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
byte[] CmdData = AMemoryHelper.ReadBytes(Memory, CmdPtr, Size);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
KSession Session = Process.HandleTable.GetData<KSession>(Handle);
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
if (Session != null)
|
|
|
|
{
|
|
|
|
Process.Scheduler.Suspend(CurrThread.ProcessorId);
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
IpcMessage Cmd = new IpcMessage(CmdData, CmdPtr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
IpcHandler.IpcCall(Ns, Process, Memory, Session, Cmd, CmdPtr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
Thread.Yield();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
Process.Scheduler.Resume(CurrThread);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-14 03:02:24 +02:00
|
|
|
Logging.Warn(LogClass.KernelSvc, $"Tried to SendSyncRequest on invalid session handle 0x{Handle:x8}!");
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcBreak(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long Reason = (long)ThreadState.X0;
|
|
|
|
long Unknown = (long)ThreadState.X1;
|
|
|
|
long Info = (long)ThreadState.X2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
throw new GuestBrokeExecutionException();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcOutputDebugString(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
long Size = (long)ThreadState.X1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
string Str = AMemoryHelper.ReadAsciiString(Memory, Position, Size);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-04-14 03:02:24 +02:00
|
|
|
Logging.Info(LogClass.KernelSvc, Str);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:28:07 +01:00
|
|
|
private void SvcGetInfo(AThreadState ThreadState)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-18 20:28:07 +01:00
|
|
|
long StackPtr = (long)ThreadState.X0;
|
|
|
|
int InfoType = (int)ThreadState.X1;
|
|
|
|
long Handle = (long)ThreadState.X2;
|
|
|
|
int InfoId = (int)ThreadState.X3;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-07 00:28:32 +01:00
|
|
|
//Fail for info not available on older Kernel versions.
|
|
|
|
if (InfoType == 18 ||
|
2018-03-30 17:27:48 +02:00
|
|
|
InfoType == 19 ||
|
|
|
|
InfoType == 20)
|
2018-02-07 00:28:32 +01:00
|
|
|
{
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidInfo);
|
2018-02-07 00:28:32 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-05 00:08:20 +01:00
|
|
|
switch (InfoType)
|
|
|
|
{
|
2018-02-28 00:45:07 +01:00
|
|
|
case 0:
|
|
|
|
ThreadState.X1 = AllowedCpuIdBitmask;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
ThreadState.X1 = MemoryRegions.MapRegionAddress;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
ThreadState.X1 = MemoryRegions.MapRegionSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
ThreadState.X1 = MemoryRegions.HeapRegionAddress;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2018-03-10 03:12:57 +01:00
|
|
|
ThreadState.X1 = MemoryRegions.HeapRegionSize;
|
2018-02-28 00:45:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
ThreadState.X1 = MemoryRegions.TotalMemoryAvailable;
|
|
|
|
break;
|
2018-04-05 02:01:36 +02:00
|
|
|
|
2018-02-28 00:45:07 +01:00
|
|
|
case 7:
|
|
|
|
ThreadState.X1 = MemoryRegions.TotalMemoryUsed + CurrentHeapSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
ThreadState.X1 = EnableProcessDebugging ? 1 : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
ThreadState.X1 = (ulong)Rng.Next() + ((ulong)Rng.Next() << 32);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 12:
|
|
|
|
ThreadState.X1 = MemoryRegions.AddrSpaceStart;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
ThreadState.X1 = MemoryRegions.AddrSpaceSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 14:
|
|
|
|
ThreadState.X1 = MemoryRegions.MapRegionAddress;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
ThreadState.X1 = MemoryRegions.MapRegionSize;
|
|
|
|
break;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
default: throw new NotImplementedException($"SvcGetInfo: {InfoType} {Handle} {InfoId}");
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:18:37 +01:00
|
|
|
ThreadState.X0 = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 00:09:10 +01:00
|
|
|
}
|