2023-03-17 12:14:50 +00:00
|
|
|
using Microsoft.IO;
|
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Memory;
|
2023-03-11 20:05:48 +00:00
|
|
|
using System;
|
2023-03-17 12:14:50 +00:00
|
|
|
using System.Buffers;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System.Collections.Generic;
|
2021-05-05 21:44:26 +00:00
|
|
|
using System.Diagnostics;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System.IO;
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Ipc
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
class IpcMessage
|
|
|
|
{
|
|
|
|
public IpcMessageType Type { get; set; }
|
|
|
|
|
|
|
|
public IpcHandleDesc HandleDesc { get; set; }
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public List<IpcPtrBuffDesc> PtrBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> SendBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> ReceiveBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> ExchangeBuff { get; private set; }
|
|
|
|
public List<IpcRecvListBuffDesc> RecvListBuff { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public List<int> ObjectIds { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public byte[] RawData { get; set; }
|
|
|
|
|
|
|
|
public IpcMessage()
|
|
|
|
{
|
2023-04-24 02:06:23 +00:00
|
|
|
PtrBuff = new List<IpcPtrBuffDesc>(0);
|
|
|
|
SendBuff = new List<IpcBuffDesc>(0);
|
|
|
|
ReceiveBuff = new List<IpcBuffDesc>(0);
|
|
|
|
ExchangeBuff = new List<IpcBuffDesc>(0);
|
|
|
|
RecvListBuff = new List<IpcRecvListBuffDesc>(0);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
ObjectIds = new List<int>(0);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
public IpcMessage(ReadOnlySpan<byte> data, long cmdPtr)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
using (RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream(data))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
BinaryReader reader = new BinaryReader(ms);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
int word0 = reader.ReadInt32();
|
|
|
|
int word1 = reader.ReadInt32();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
Type = (IpcMessageType)(word0 & 0xffff);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
int ptrBuffCount = (word0 >> 16) & 0xf;
|
|
|
|
int sendBuffCount = (word0 >> 20) & 0xf;
|
|
|
|
int recvBuffCount = (word0 >> 24) & 0xf;
|
|
|
|
int xchgBuffCount = (word0 >> 28) & 0xf;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
int rawDataSize = (word1 >> 0) & 0x3ff;
|
|
|
|
int recvListFlags = (word1 >> 10) & 0xf;
|
|
|
|
bool hndDescEnable = ((word1 >> 31) & 0x1) != 0;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
if (hndDescEnable)
|
|
|
|
{
|
|
|
|
HandleDesc = new IpcHandleDesc(reader);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
PtrBuff = new List<IpcPtrBuffDesc>(ptrBuffCount);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
for (int index = 0; index < ptrBuffCount; index++)
|
|
|
|
{
|
|
|
|
PtrBuff.Add(new IpcPtrBuffDesc(reader));
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
static List<IpcBuffDesc> ReadBuff(BinaryReader reader, int count)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-04-24 02:06:23 +00:00
|
|
|
List<IpcBuffDesc> buff = new List<IpcBuffDesc>(count);
|
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
buff.Add(new IpcBuffDesc(reader));
|
|
|
|
}
|
|
|
|
|
|
|
|
return buff;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
SendBuff = ReadBuff(reader, sendBuffCount);
|
|
|
|
ReceiveBuff = ReadBuff(reader, recvBuffCount);
|
|
|
|
ExchangeBuff = ReadBuff(reader, xchgBuffCount);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
rawDataSize *= 4;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
long recvListPos = reader.BaseStream.Position + rawDataSize;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
// Only CMIF has the padding requirements.
|
2021-04-13 22:01:24 +00:00
|
|
|
if (Type < IpcMessageType.TipcCloseSession)
|
2020-12-01 23:23:43 +00:00
|
|
|
{
|
2021-04-13 22:01:24 +00:00
|
|
|
long pad0 = GetPadSize16(reader.BaseStream.Position + cmdPtr);
|
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
if (rawDataSize != 0)
|
|
|
|
{
|
|
|
|
rawDataSize -= (int)pad0;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.BaseStream.Seek(pad0, SeekOrigin.Current);
|
2021-04-13 22:01:24 +00:00
|
|
|
}
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
int recvListCount = recvListFlags - 2;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
if (recvListCount == 0)
|
|
|
|
{
|
|
|
|
recvListCount = 1;
|
|
|
|
}
|
|
|
|
else if (recvListCount < 0)
|
|
|
|
{
|
|
|
|
recvListCount = 0;
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
RawData = reader.ReadBytes(rawDataSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
RecvListBuff = new List<IpcRecvListBuffDesc>(recvListCount);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
for (int index = 0; index < recvListCount; index++)
|
|
|
|
{
|
|
|
|
RecvListBuff.Add(new IpcRecvListBuffDesc(reader.ReadUInt64()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectIds = new List<int>(0);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public RecyclableMemoryStream GetStream(long cmdPtr, ulong recvListAddr)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int word0;
|
|
|
|
int word1;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
word0 = (int)Type;
|
|
|
|
word0 |= (PtrBuff.Count & 0xf) << 16;
|
|
|
|
word0 |= (SendBuff.Count & 0xf) << 20;
|
|
|
|
word0 |= (ReceiveBuff.Count & 0xf) << 24;
|
|
|
|
word0 |= (ExchangeBuff.Count & 0xf) << 28;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
using RecyclableMemoryStream handleDataStream = HandleDesc?.GetStream();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int dataLength = RawData?.Length ?? 0;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
dataLength = (dataLength + 3) & ~3;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int rawLength = dataLength;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int pad0 = (int)GetPadSize16(cmdPtr + 8 + (handleDataStream?.Length ?? 0) + PtrBuff.Count * 8);
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
// Apparently, padding after Raw Data is 16 bytes, however when there is
|
|
|
|
// padding before Raw Data too, we need to subtract the size of this padding.
|
|
|
|
// This is the weirdest padding I've seen so far...
|
|
|
|
int pad1 = 0x10 - pad0;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
dataLength = (dataLength + pad0 + pad1) / 4;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
word1 = (dataLength & 0x3ff) | (2 << 10);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (HandleDesc != null)
|
|
|
|
{
|
|
|
|
word1 |= 1 << 31;
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ms.Write(word0);
|
|
|
|
ms.Write(word1);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (handleDataStream != null)
|
|
|
|
{
|
|
|
|
ms.Write(handleDataStream);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
foreach (IpcPtrBuffDesc ptrBuffDesc in PtrBuff)
|
|
|
|
{
|
|
|
|
ms.Write(ptrBuffDesc.GetWord0());
|
|
|
|
ms.Write(ptrBuffDesc.GetWord1());
|
|
|
|
}
|
2020-12-01 23:23:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ms.WriteByte(0, pad0);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (RawData != null)
|
|
|
|
{
|
|
|
|
ms.Write(RawData);
|
|
|
|
ms.WriteByte(0, rawLength - RawData.Length);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ms.WriteByte(0, pad1);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ms.Write(recvListAddr);
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
|
|
|
return ms;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public RecyclableMemoryStream GetStreamTipc()
|
2021-05-05 21:44:26 +00:00
|
|
|
{
|
|
|
|
Debug.Assert(PtrBuff.Count == 0);
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int word0;
|
|
|
|
int word1;
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
word0 = (int)Type;
|
|
|
|
word0 |= (SendBuff.Count & 0xf) << 20;
|
|
|
|
word0 |= (ReceiveBuff.Count & 0xf) << 24;
|
|
|
|
word0 |= (ExchangeBuff.Count & 0xf) << 28;
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
using RecyclableMemoryStream handleDataStream = HandleDesc?.GetStream();
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
int dataLength = RawData?.Length ?? 0;
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
dataLength = ((dataLength + 3) & ~3) / 4;
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
word1 = (dataLength & 0x3ff);
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (HandleDesc != null)
|
|
|
|
{
|
|
|
|
word1 |= 1 << 31;
|
|
|
|
}
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
ms.Write(word0);
|
|
|
|
ms.Write(word1);
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (handleDataStream != null)
|
|
|
|
{
|
|
|
|
ms.Write(handleDataStream);
|
|
|
|
}
|
2021-05-05 21:44:26 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
if (RawData != null)
|
|
|
|
{
|
|
|
|
ms.Write(RawData);
|
2021-05-05 21:44:26 +00:00
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
|
|
|
|
return ms;
|
2021-05-05 21:44:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private long GetPadSize16(long position)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((position & 0xf) != 0)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return 0x10 - (position & 0xf);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
// ReSharper disable once InconsistentNaming
|
2021-04-24 10:16:01 +00:00
|
|
|
public (ulong Position, ulong Size) GetBufferType0x21(int index = 0)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2021-06-22 17:32:22 +00:00
|
|
|
if (PtrBuff.Count > index && PtrBuff[index].Position != 0)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return (PtrBuff[index].Position, PtrBuff[index].Size);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 17:32:22 +00:00
|
|
|
if (SendBuff.Count > index)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return (SendBuff[index].Position, SendBuff[index].Size);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 22:46:09 +00:00
|
|
|
return (0, 0);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
// ReSharper disable once InconsistentNaming
|
2021-04-24 10:16:01 +00:00
|
|
|
public (ulong Position, ulong Size) GetBufferType0x22(int index = 0)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2021-06-22 17:32:22 +00:00
|
|
|
if (RecvListBuff.Count > index && RecvListBuff[index].Position != 0)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return (RecvListBuff[index].Position, RecvListBuff[index].Size);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 17:32:22 +00:00
|
|
|
if (ReceiveBuff.Count > index)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return (ReceiveBuff[index].Position, ReceiveBuff[index].Size);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 22:46:09 +00:00
|
|
|
return (0, 0);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-02-24 23:08:54 +00:00
|
|
|
}
|