2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
2018-02-20 15:09:23 -05:00
|
|
|
using Ryujinx.Core.OsHle.Exceptions;
|
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
|
|
|
using Ryujinx.Core.OsHle.Ipc;
|
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-02-20 15:09:23 -05:00
|
|
|
namespace Ryujinx.Core.OsHle.Svc
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcExitProcess(AThreadState ThreadState) => Ns.Os.ExitProcess(ThreadState.ProcessId);
|
2018-02-15 07:16:16 -05:00
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcCloseHandle(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
int Handle = (int)ThreadState.X0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Ns.Os.CloseHandle(Handle);
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcResetSignal(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
int Handle = (int)ThreadState.X0;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
//TODO: Implement events.
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcWaitSynchronization(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
long HandlesPtr = (long)ThreadState.X0;
|
|
|
|
int HandlesCount = (int)ThreadState.X2;
|
|
|
|
long Timeout = (long)ThreadState.X3;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
//TODO: Implement events.
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
HThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
Process.Scheduler.Suspend(CurrThread.ProcessorId);
|
|
|
|
Process.Scheduler.Resume(CurrThread);
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcGetSystemTick(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (ulong)ThreadState.CntpctEl0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcConnectToNamedPort(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
long StackPtr = (long)ThreadState.X0;
|
|
|
|
long NamePtr = (long)ThreadState.X1;
|
2018-02-04 18:08:20 -05: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.
|
|
|
|
|
|
|
|
HSession Session = new HSession(Name);
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X1 = (ulong)Ns.Os.Handles.GenerateId(Session);
|
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcSendSyncRequest(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
SendSyncRequest(ThreadState, false);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcSendSyncRequestWithUserBuffer(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
SendSyncRequest(ThreadState, true);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SendSyncRequest(AThreadState ThreadState, bool UserBuffer)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
long CmdPtr = ThreadState.Tpidr;
|
2018-02-04 18:08:20 -05:00
|
|
|
long Size = 0x100;
|
|
|
|
int Handle = 0;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
if (UserBuffer)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
CmdPtr = (long)ThreadState.X0;
|
|
|
|
Size = (long)ThreadState.X1;
|
|
|
|
Handle = (int)ThreadState.X2;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
Handle = (int)ThreadState.X0;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
HThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
|
|
|
|
|
|
|
Process.Scheduler.Suspend(CurrThread.ProcessorId);
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
byte[] CmdData = AMemoryHelper.ReadBytes(Memory, CmdPtr, (int)Size);
|
|
|
|
|
|
|
|
HSession Session = Ns.Os.Handles.GetData<HSession>(Handle);
|
|
|
|
|
|
|
|
IpcMessage Cmd = new IpcMessage(CmdData, CmdPtr, Session is HDomain);
|
|
|
|
|
|
|
|
if (Session != null)
|
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
IpcHandler.IpcCall(Ns, Memory, Session, Cmd, ThreadState.ThreadId, CmdPtr, Handle);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
byte[] Response = AMemoryHelper.ReadBytes(Memory, CmdPtr, (int)Size);
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.ErrBadIpcReq;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-19 14:37:13 -05:00
|
|
|
|
|
|
|
Thread.Yield();
|
|
|
|
|
|
|
|
Process.Scheduler.Resume(CurrThread);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcBreak(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -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-02-13 21:43:08 -05:00
|
|
|
throw new GuestBrokeExecutionException();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcOutputDebugString(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
long Position = (long)ThreadState.X0;
|
|
|
|
long Size = (long)ThreadState.X1;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);
|
|
|
|
|
2018-02-08 19:43:22 -05:00
|
|
|
Logging.Info($"SvcOutputDebugString: {Str}");
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
private void SvcGetInfo(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
long StackPtr = (long)ThreadState.X0;
|
|
|
|
int InfoType = (int)ThreadState.X1;
|
|
|
|
long Handle = (long)ThreadState.X2;
|
|
|
|
int InfoId = (int)ThreadState.X3;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-06 18:28:32 -05:00
|
|
|
//Fail for info not available on older Kernel versions.
|
|
|
|
if (InfoType == 18 ||
|
|
|
|
InfoType == 19)
|
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.ErrBadInfo;
|
2018-02-06 18:28:32 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
switch (InfoType)
|
|
|
|
{
|
2018-02-24 18:09:10 -05:00
|
|
|
case 0: ThreadState.X1 = AllowedCpuIdBitmask(); break;
|
2018-02-21 19:51:17 -05:00
|
|
|
case 2: ThreadState.X1 = GetMapRegionBaseAddr(); break;
|
|
|
|
case 3: ThreadState.X1 = GetMapRegionSize(); break;
|
|
|
|
case 4: ThreadState.X1 = GetHeapRegionBaseAddr(); break;
|
|
|
|
case 5: ThreadState.X1 = GetHeapRegionSize(); break;
|
|
|
|
case 6: ThreadState.X1 = GetTotalMem(); break;
|
|
|
|
case 7: ThreadState.X1 = GetUsedMem(); break;
|
|
|
|
case 8: ThreadState.X1 = IsCurrentProcessBeingDebugged(); break;
|
|
|
|
case 11: ThreadState.X1 = GetRnd64(); break;
|
|
|
|
case 12: ThreadState.X1 = GetAddrSpaceBaseAddr(); break;
|
|
|
|
case 13: ThreadState.X1 = GetAddrSpaceSize(); break;
|
|
|
|
case 14: ThreadState.X1 = GetMapRegionBaseAddr(); break;
|
|
|
|
case 15: ThreadState.X1 = GetMapRegionSize(); break;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
default: throw new NotImplementedException($"SvcGetInfo: {InfoType} {Handle} {InfoId}");
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadState.X0 = (int)SvcResult.Success;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-24 18:09:10 -05:00
|
|
|
|
|
|
|
private ulong AllowedCpuIdBitmask()
|
|
|
|
{
|
|
|
|
return 0xF; //Mephisto value.
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetMapRegionBaseAddr()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
return MemoryRegions.MapRegionAddress;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetMapRegionSize()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
return MemoryRegions.MapRegionSize;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetHeapRegionBaseAddr()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
return MemoryRegions.HeapRegionAddress;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-06 18:28:32 -05:00
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetHeapRegionSize()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
return MemoryRegions.HeapRegionSize;
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetTotalMem()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-21 19:51:17 -05:00
|
|
|
return (ulong)Memory.Manager.GetTotalMemorySize();
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetUsedMem()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-21 19:51:17 -05:00
|
|
|
return (ulong)Memory.Manager.GetUsedMemorySize();
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong IsCurrentProcessBeingDebugged()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-21 19:51:17 -05:00
|
|
|
return (ulong)0;
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetRnd64()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-21 19:51:17 -05:00
|
|
|
return (ulong)Rng.Next() + ((ulong)Rng.Next() << 32);
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 19:51:17 -05:00
|
|
|
private ulong GetAddrSpaceBaseAddr()
|
2018-02-06 18:28:32 -05:00
|
|
|
{
|
2018-02-21 19:51:17 -05:00
|
|
|
return 0x08000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong GetAddrSpaceSize()
|
|
|
|
{
|
|
|
|
return AMemoryMgr.AddrSize - GetAddrSpaceBaseAddr();
|
2018-02-06 18:28:32 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-24 18:09:10 -05:00
|
|
|
}
|