2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
2018-02-14 02:43:08 +00:00
|
|
|
using Ryujinx.OsHle.Exceptions;
|
2018-02-04 23:08:20 +00:00
|
|
|
using Ryujinx.OsHle.Handles;
|
|
|
|
using Ryujinx.OsHle.Ipc;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.OsHle.Svc
|
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcCloseHandle(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
int Handle = (int)Registers.X0;
|
|
|
|
|
|
|
|
Ns.Os.CloseHandle(Handle);
|
|
|
|
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcResetSignal(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
int Handle = (int)Registers.X0;
|
|
|
|
|
|
|
|
//TODO: Implement events.
|
|
|
|
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcWaitSynchronization(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
long HandlesPtr = (long)Registers.X0;
|
|
|
|
int HandlesCount = (int)Registers.X2;
|
|
|
|
long Timeout = (long)Registers.X3;
|
|
|
|
|
|
|
|
//TODO: Implement events.
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
//Logging.Info($"SvcWaitSynchronization Thread {Registers.ThreadId}");
|
|
|
|
|
|
|
|
if (Process.TryGetThread(Registers.Tpidr, out HThread Thread))
|
|
|
|
{
|
|
|
|
Process.Scheduler.Yield(Thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logging.Error($"Thread with TPIDR_EL0 0x{Registers.Tpidr:x16} not found!");
|
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcGetSystemTick(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-06 15:15:08 +00:00
|
|
|
Registers.X0 = (ulong)Registers.CntpctEl0;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcConnectToNamedPort(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
long StackPtr = (long)Registers.X0;
|
|
|
|
long NamePtr = (long)Registers.X1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Registers.X1 = (ulong)Ns.Os.Handles.GenerateId(Session);
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcSendSyncRequest(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
SendSyncRequest(Registers, false);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcSendSyncRequestWithUserBuffer(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
SendSyncRequest(Registers, true);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SendSyncRequest(ARegisters Registers, bool UserBuffer)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-06 15:15:08 +00:00
|
|
|
long CmdPtr = Registers.Tpidr;
|
2018-02-04 23:08:20 +00:00
|
|
|
long Size = 0x100;
|
|
|
|
int Handle = 0;
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
if (UserBuffer)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
CmdPtr = (long)Registers.X0;
|
|
|
|
Size = (long)Registers.X1;
|
|
|
|
Handle = (int)Registers.X2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Handle = (int)Registers.X0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 00:23:16 +00:00
|
|
|
IpcHandler.IpcCall(Ns, Memory, Session, Cmd, CmdPtr, Handle);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
byte[] Response = AMemoryHelper.ReadBytes(Memory, CmdPtr, (int)Size);
|
|
|
|
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Registers.X0 = (int)SvcResult.ErrBadIpcReq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcBreak(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
long Reason = (long)Registers.X0;
|
|
|
|
long Unknown = (long)Registers.X1;
|
|
|
|
long Info = (long)Registers.X2;
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
throw new GuestBrokeExecutionException();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcOutputDebugString(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
long Position = (long)Registers.X0;
|
|
|
|
long Size = (long)Registers.X1;
|
|
|
|
|
|
|
|
string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);
|
|
|
|
|
2018-02-09 00:43:22 +00:00
|
|
|
Logging.Info($"SvcOutputDebugString: {Str}");
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private void SvcGetInfo(ARegisters Registers)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
long StackPtr = (long)Registers.X0;
|
|
|
|
int InfoType = (int)Registers.X1;
|
|
|
|
long Handle = (long)Registers.X2;
|
|
|
|
int InfoId = (int)Registers.X3;
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
//Fail for info not available on older Kernel versions.
|
|
|
|
if (InfoType == 18 ||
|
|
|
|
InfoType == 19)
|
|
|
|
{
|
|
|
|
Registers.X0 = (int)SvcResult.ErrBadInfo;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
switch (InfoType)
|
|
|
|
{
|
2018-02-06 23:28:32 +00:00
|
|
|
case 2: Registers.X1 = GetMapRegionBaseAddr(); break;
|
|
|
|
case 3: Registers.X1 = GetMapRegionSize(); break;
|
|
|
|
case 4: Registers.X1 = GetHeapRegionBaseAddr(); break;
|
|
|
|
case 5: Registers.X1 = GetHeapRegionSize(); break;
|
2018-02-14 02:43:08 +00:00
|
|
|
case 6: Registers.X1 = GetTotalMem(); break;
|
|
|
|
case 7: Registers.X1 = GetUsedMem(); break;
|
2018-02-06 23:28:32 +00:00
|
|
|
case 11: Registers.X1 = GetRnd64(); break;
|
|
|
|
case 12: Registers.X1 = GetAddrSpaceBaseAddr(); break;
|
|
|
|
case 13: Registers.X1 = GetAddrSpaceSize(); break;
|
|
|
|
case 14: Registers.X1 = GetMapRegionBaseAddr(); break;
|
|
|
|
case 15: Registers.X1 = GetMapRegionSize(); break;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
default: throw new NotImplementedException($"SvcGetInfo: {InfoType} {Handle} {InfoId}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Registers.X0 = (int)SvcResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetTotalMem()
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
return (ulong)Memory.Manager.GetTotalMemorySize();
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetUsedMem()
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
return (ulong)Memory.Manager.GetUsedMemorySize();
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetRnd64()
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
return (ulong)Rng.Next() + ((ulong)Rng.Next() << 32);
|
|
|
|
}
|
2018-02-06 23:28:32 +00:00
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetAddrSpaceBaseAddr()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return 0x08000000;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetAddrSpaceSize()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return AMemoryMgr.AddrSize - GetAddrSpaceBaseAddr();
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetMapRegionBaseAddr()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return 0x80000000;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetMapRegionSize()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return 0x40000000;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetHeapRegionBaseAddr()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return GetMapRegionBaseAddr() + GetMapRegionSize();
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private ulong GetHeapRegionSize()
|
2018-02-06 23:28:32 +00:00
|
|
|
{
|
|
|
|
return 0x40000000;
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|