Implement many objects, improve logging. (#42)

* Implement many objects, improve logging.

Change and rename folders of Services
Add Logging of IpcMessage.
Add "lm" Log Service.
Parse Errors of SetTerminateResult
Add Svc Calls.
Add many object implementations.

* Corrections

Forgotten Debug Conf

* Corrections 2

* Corrections 3

* Corrections 4
This commit is contained in:
Ac_K 2018-02-25 19:58:16 +01:00 committed by gdkchan
parent fba0bf8732
commit e174100474
69 changed files with 660 additions and 27 deletions

View file

@ -0,0 +1,60 @@
using System.IO;
namespace Ryujinx.Core.OsHle.IpcServices.Android
{
struct GbpBuffer
{
public int Magic { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public int Stride { get; private set; }
public int Format { get; private set; }
public int Usage { get; private set; }
public int Pid { get; private set; }
public int RefCount { get; private set; }
public int FdsCount { get; private set; }
public int IntsCount { get; private set; }
public byte[] RawData { get; private set; }
public int Size => RawData.Length + 10 * 4;
public GbpBuffer(BinaryReader Reader)
{
Magic = Reader.ReadInt32();
Width = Reader.ReadInt32();
Height = Reader.ReadInt32();
Stride = Reader.ReadInt32();
Format = Reader.ReadInt32();
Usage = Reader.ReadInt32();
Pid = Reader.ReadInt32();
RefCount = Reader.ReadInt32();
FdsCount = Reader.ReadInt32();
IntsCount = Reader.ReadInt32();
RawData = Reader.ReadBytes((FdsCount + IntsCount) * 4);
}
public void Write(BinaryWriter Writer)
{
Writer.Write(Magic);
Writer.Write(Width);
Writer.Write(Height);
Writer.Write(Stride);
Writer.Write(Format);
Writer.Write(Usage);
Writer.Write(Pid);
Writer.Write(RefCount);
Writer.Write(FdsCount);
Writer.Write(IntsCount);
Writer.Write(RawData);
}
}
}

View file

@ -0,0 +1,176 @@
using ChocolArm64.Memory;
using Ryujinx.Core.OsHle.Handles;
using Ryujinx.Core.OsHle.Ipc;
using System.Collections.Generic;
using System.IO;
using static Ryujinx.Core.OsHle.IpcServices.Android.Parcel;
using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
namespace Ryujinx.Core.OsHle.IpcServices.Vi
{
class IApplicationDisplayService : IIpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IApplicationDisplayService()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 100, GetRelayService },
{ 101, GetSystemDisplayService },
{ 102, GetManagerDisplayService },
{ 103, GetIndirectDisplayTransactionService },
{ 1010, OpenDisplay },
{ 2020, OpenLayer },
{ 2030, CreateStrayLayer },
{ 2101, SetLayerScalingMode },
{ 5202, GetDisplayVSyncEvent }
};
}
public long GetRelayService(ServiceCtx Context)
{
MakeObject(Context, new IHOSBinderDriver());
return 0;
}
public long GetSystemDisplayService(ServiceCtx Context)
{
MakeObject(Context, new ISystemDisplayService());
return 0;
}
public long GetManagerDisplayService(ServiceCtx Context)
{
MakeObject(Context, new IManagerDisplayService());
return 0;
}
public long GetIndirectDisplayTransactionService(ServiceCtx Context)
{
MakeObject(Context, new IHOSBinderDriver());
return 0;
}
public long OpenDisplay(ServiceCtx Context)
{
string Name = GetDisplayName(Context);
long DisplayId = Context.Ns.Os.Displays.GenerateId(new Display(Name));
Context.ResponseData.Write(DisplayId);
return 0;
}
public long OpenLayer(ServiceCtx Context)
{
long LayerId = Context.RequestData.ReadInt64();
long UserId = Context.RequestData.ReadInt64();
long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
Context.ResponseData.Write((long)Parcel.Length);
return 0;
}
public long CreateStrayLayer(ServiceCtx Context)
{
long LayerFlags = Context.RequestData.ReadInt64();
long DisplayId = Context.RequestData.ReadInt64();
long ParcelPtr = Context.Request.ReceiveBuff[0].Position;
Display Disp = Context.Ns.Os.Displays.GetData<Display>((int)DisplayId);
byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);
AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);
Context.ResponseData.Write(0L);
Context.ResponseData.Write((long)Parcel.Length);
return 0;
}
public long SetLayerScalingMode(ServiceCtx Context)
{
int ScalingMode = Context.RequestData.ReadInt32();
long Unknown = Context.RequestData.ReadInt64();
return 0;
}
public long GetDisplayVSyncEvent(ServiceCtx Context)
{
string Name = GetDisplayName(Context);
int Handle = Context.Ns.Os.Handles.GenerateId(new HEvent());
Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
return 0;
}
private byte[] MakeIGraphicsBufferProducer(long BasePtr)
{
long Id = 0x20;
long CookiePtr = 0L;
using (MemoryStream MS = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MS);
//flat_binder_object (size is 0x28)
Writer.Write(2); //Type (BINDER_TYPE_WEAK_BINDER)
Writer.Write(0); //Flags
Writer.Write((int)(Id >> 0));
Writer.Write((int)(Id >> 32));
Writer.Write((int)(CookiePtr >> 0));
Writer.Write((int)(CookiePtr >> 32));
Writer.Write((byte)'d');
Writer.Write((byte)'i');
Writer.Write((byte)'s');
Writer.Write((byte)'p');
Writer.Write((byte)'d');
Writer.Write((byte)'r');
Writer.Write((byte)'v');
Writer.Write((byte)'\0');
Writer.Write(0L); //Pad
return MakeParcel(MS.ToArray(), new byte[] { 0, 0, 0, 0 });
}
}
private string GetDisplayName(ServiceCtx Context)
{
string Name = string.Empty;
for (int Index = 0; Index < 8 &&
Context.RequestData.BaseStream.Position <
Context.RequestData.BaseStream.Length; Index++)
{
byte Chr = Context.RequestData.ReadByte();
if (Chr >= 0x20 && Chr < 0x7f)
{
Name += (char)Chr;
}
}
return Name;
}
}
}

View file

@ -0,0 +1,76 @@
using ChocolArm64.Memory;
using Ryujinx.Core.OsHle.Ipc;
using Ryujinx.Core.OsHle.IpcServices.Android;
using System;
using System.Collections.Generic;
namespace Ryujinx.Core.OsHle.IpcServices.Vi
{
class IHOSBinderDriver : IIpcService, IDisposable
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
private NvFlinger Flinger;
public IHOSBinderDriver()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 0, TransactParcel },
{ 1, AdjustRefcount },
{ 2, GetNativeHandle }
};
Flinger = new NvFlinger();
}
public long TransactParcel(ServiceCtx Context)
{
int Id = Context.RequestData.ReadInt32();
int Code = Context.RequestData.ReadInt32();
long DataPos = Context.Request.SendBuff[0].Position;
long DataSize = Context.Request.SendBuff[0].Size;
byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, (int)DataSize);
Data = Parcel.GetParcelData(Data);
return Flinger.ProcessParcelRequest(Context, Data, Code);
}
public long AdjustRefcount(ServiceCtx Context)
{
int Id = Context.RequestData.ReadInt32();
int AddVal = Context.RequestData.ReadInt32();
int Type = Context.RequestData.ReadInt32();
return 0;
}
public long GetNativeHandle(ServiceCtx Context)
{
int Id = Context.RequestData.ReadInt32();
uint Unk = Context.RequestData.ReadUInt32();
Context.Response.HandleDesc = IpcHandleDesc.MakeMove(0xbadcafe);
return 0;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Flinger.Dispose();
}
}
}
}

View file

@ -0,0 +1,33 @@
using Ryujinx.Core.OsHle.Ipc;
using System.Collections.Generic;
namespace Ryujinx.Core.OsHle.IpcServices.Vi
{
class IManagerDisplayService : IIpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IManagerDisplayService()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 2010, CreateManagedLayer },
{ 6000, AddToLayerStack }
};
}
public static long CreateManagedLayer(ServiceCtx Context)
{
Context.ResponseData.Write(0L); //LayerId
return 0;
}
public static long AddToLayerStack(ServiceCtx Context)
{
return 0;
}
}
}

View file

@ -0,0 +1,25 @@
using Ryujinx.Core.OsHle.Ipc;
using System.Collections.Generic;
namespace Ryujinx.Core.OsHle.IpcServices.Vi
{
class ISystemDisplayService : IIpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public ISystemDisplayService()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 2205, SetLayerZ }
};
}
public static long SetLayerZ(ServiceCtx Context)
{
return 0;
}
}
}

View file

@ -0,0 +1,395 @@
using ChocolArm64.Memory;
using Ryujinx.Core.OsHle.Handles;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using static Ryujinx.Core.OsHle.IpcServices.Android.Parcel;
namespace Ryujinx.Core.OsHle.IpcServices.Android
{
class NvFlinger : IDisposable
{
private delegate long ServiceProcessParcel(ServiceCtx Context, BinaryReader ParcelReader);
private Dictionary<(string, int), ServiceProcessParcel> Commands;
private const int BufferQueueCount = 0x40;
private const int BufferQueueMask = BufferQueueCount - 1;
[Flags]
private enum HalTransform
{
FlipX = 1 << 0,
FlipY = 1 << 1,
Rotate90 = 1 << 2
}
private enum BufferState
{
Free,
Dequeued,
Queued,
Acquired
}
private struct BufferEntry
{
public BufferState State;
public HalTransform Transform;
public GbpBuffer Data;
}
private BufferEntry[] BufferQueue;
private ManualResetEvent WaitBufferFree;
private bool KeepRunning;
public NvFlinger()
{
Commands = new Dictionary<(string, int), ServiceProcessParcel>()
{
{ ("android.gui.IGraphicBufferProducer", 0x1), GbpRequestBuffer },
{ ("android.gui.IGraphicBufferProducer", 0x3), GbpDequeueBuffer },
{ ("android.gui.IGraphicBufferProducer", 0x7), GbpQueueBuffer },
{ ("android.gui.IGraphicBufferProducer", 0x8), GbpCancelBuffer },
{ ("android.gui.IGraphicBufferProducer", 0x9), GbpQuery },
{ ("android.gui.IGraphicBufferProducer", 0xa), GbpConnect },
{ ("android.gui.IGraphicBufferProducer", 0xe), GbpPreallocBuffer }
};
BufferQueue = new BufferEntry[0x40];
WaitBufferFree = new ManualResetEvent(false);
KeepRunning = true;
}
public long ProcessParcelRequest(ServiceCtx Context, byte[] ParcelData, int Code)
{
using (MemoryStream MS = new MemoryStream(ParcelData))
{
BinaryReader Reader = new BinaryReader(MS);
MS.Seek(4, SeekOrigin.Current);
int StrSize = Reader.ReadInt32();
string InterfaceName = Encoding.Unicode.GetString(Reader.ReadBytes(StrSize * 2));
long Remainder = MS.Position & 0xf;
if (Remainder != 0)
{
MS.Seek(0x10 - Remainder, SeekOrigin.Current);
}
MS.Seek(0x50, SeekOrigin.Begin);
if (Commands.TryGetValue((InterfaceName, Code), out ServiceProcessParcel ProcReq))
{
Logging.Debug($"{InterfaceName} {ProcReq.Method.Name}");
return ProcReq(Context, Reader);
}
else
{
throw new NotImplementedException($"{InterfaceName} {Code}");
}
}
}
private long GbpRequestBuffer(ServiceCtx Context, BinaryReader ParcelReader)
{
int Slot = ParcelReader.ReadInt32();
using (MemoryStream MS = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MS);
BufferEntry Entry = BufferQueue[Slot];
int BufferCount = 1; //?
long BufferSize = Entry.Data.Size;
Writer.Write(BufferCount);
Writer.Write(BufferSize);
Entry.Data.Write(Writer);
Writer.Write(0);
return MakeReplyParcel(Context, MS.ToArray());
}
}
private long GbpDequeueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
{
//TODO: Errors.
int Format = ParcelReader.ReadInt32();
int Width = ParcelReader.ReadInt32();
int Height = ParcelReader.ReadInt32();
int GetTimestamps = ParcelReader.ReadInt32();
int Usage = ParcelReader.ReadInt32();
int Slot = GetFreeSlotBlocking(Width, Height);
return MakeReplyParcel(Context, Slot, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
private long GbpQueueBuffer(ServiceCtx Context, BinaryReader ParcelReader)
{
//TODO: Errors.
int Slot = ParcelReader.ReadInt32();
int Unknown4 = ParcelReader.ReadInt32();
int Unknown8 = ParcelReader.ReadInt32();
int Unknownc = ParcelReader.ReadInt32();
int Timestamp = ParcelReader.ReadInt32();
int IsAutoTimestamp = ParcelReader.ReadInt32();
int CropTop = ParcelReader.ReadInt32();
int CropLeft = ParcelReader.ReadInt32();
int CropRight = ParcelReader.ReadInt32();
int CropBottom = ParcelReader.ReadInt32();
int ScalingMode = ParcelReader.ReadInt32();
int Transform = ParcelReader.ReadInt32();
int StickyTransform = ParcelReader.ReadInt32();
int Unknown34 = ParcelReader.ReadInt32();
int Unknown38 = ParcelReader.ReadInt32();
int IsFenceValid = ParcelReader.ReadInt32();
int Fence0Id = ParcelReader.ReadInt32();
int Fence0Value = ParcelReader.ReadInt32();
int Fence1Id = ParcelReader.ReadInt32();
int Fence1Value = ParcelReader.ReadInt32();
BufferQueue[Slot].Transform = (HalTransform)Transform;
BufferQueue[Slot].State = BufferState.Queued;
SendFrameBuffer(Context, Slot);
return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
}
private long GbpCancelBuffer(ServiceCtx Context, BinaryReader ParcelReader)
{
//TODO: Errors.
int Slot = ParcelReader.ReadInt32();
BufferQueue[Slot].State = BufferState.Free;
return MakeReplyParcel(Context, 0);
}
private long GbpQuery(ServiceCtx Context, BinaryReader ParcelReader)
{
return MakeReplyParcel(Context, 0, 0);
}
private long GbpConnect(ServiceCtx Context, BinaryReader ParcelReader)
{
return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
}
private long GbpPreallocBuffer(ServiceCtx Context, BinaryReader ParcelReader)
{
int Slot = ParcelReader.ReadInt32();
int BufferCount = ParcelReader.ReadInt32();
long BufferSize = ParcelReader.ReadInt64();
BufferQueue[Slot].State = BufferState.Free;
BufferQueue[Slot].Data = new GbpBuffer(ParcelReader);
return MakeReplyParcel(Context, 0);
}
private long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
{
using (MemoryStream MS = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MS);
foreach (int Int in Ints)
{
Writer.Write(Int);
}
return MakeReplyParcel(Context, MS.ToArray());
}
}
private long MakeReplyParcel(ServiceCtx Context, byte[] Data)
{
long ReplyPos = Context.Request.ReceiveBuff[0].Position;
long ReplySize = Context.Request.ReceiveBuff[0].Size;
byte[] Reply = MakeParcel(Data, new byte[0]);
AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, Reply);
return 0;
}
private unsafe void SendFrameBuffer(ServiceCtx Context, int Slot)
{
int FbWidth = BufferQueue[Slot].Data.Width;
int FbHeight = BufferQueue[Slot].Data.Height;
int FbSize = FbWidth * FbHeight * 4;
HNvMap NvMap = GetNvMap(Context, Slot);
if (FbSize < 0 || NvMap.Address < 0 || NvMap.Address + FbSize > AMemoryMgr.AddrSize)
{
Logging.Error($"Frame buffer address {NvMap.Address:x16} is invalid!");
BufferQueue[Slot].State = BufferState.Free;
WaitBufferFree.Set();
return;
}
BufferQueue[Slot].State = BufferState.Acquired;
float ScaleX = 1;
float ScaleY = 1;
float Rotate = 0;
if (BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipX))
{
ScaleX = -1;
}
if (BufferQueue[Slot].Transform.HasFlag(HalTransform.FlipY))
{
ScaleY = -1;
}
if (BufferQueue[Slot].Transform.HasFlag(HalTransform.Rotate90))
{
Rotate = MathF.PI * 0.5f;
}
byte* Fb = (byte*)Context.Ns.Ram + NvMap.Address;
Context.Ns.Gpu.Renderer.QueueAction(delegate()
{
Context.Ns.Gpu.Renderer.SetFrameBuffer(
Fb,
FbWidth,
FbHeight,
ScaleX,
ScaleY,
Rotate);
BufferQueue[Slot].State = BufferState.Free;
lock (WaitBufferFree)
{
WaitBufferFree.Set();
}
});
}
private HNvMap GetNvMap(ServiceCtx Context, int Slot)
{
int NvMapHandle = BitConverter.ToInt32(BufferQueue[Slot].Data.RawData, 0x4c);
if (!BitConverter.IsLittleEndian)
{
byte[] RawValue = BitConverter.GetBytes(NvMapHandle);
Array.Reverse(RawValue);
NvMapHandle = BitConverter.ToInt32(RawValue, 0);
}
return Context.Ns.Os.Handles.GetData<HNvMap>(NvMapHandle);
}
private int GetFreeSlotBlocking(int Width, int Height)
{
int Slot;
do
{
lock (WaitBufferFree)
{
if ((Slot = GetFreeSlot(Width, Height)) != -1)
{
break;
}
Logging.Debug("Waiting for a free BufferQueue slot...");
if (!KeepRunning)
{
break;
}
WaitBufferFree.Reset();
}
WaitBufferFree.WaitOne();
}
while (KeepRunning);
Logging.Debug($"Found free BufferQueue slot {Slot}!");
return Slot;
}
private int GetFreeSlot(int Width, int Height)
{
lock (BufferQueue)
{
for (int Slot = 0; Slot < BufferQueue.Length; Slot++)
{
if (BufferQueue[Slot].State != BufferState.Free)
{
continue;
}
GbpBuffer Data = BufferQueue[Slot].Data;
if (Data.Width == Width &&
Data.Height == Height)
{
BufferQueue[Slot].State = BufferState.Dequeued;
return Slot;
}
}
}
return -1;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
lock (WaitBufferFree)
{
KeepRunning = false;
WaitBufferFree.Set();
}
WaitBufferFree.Dispose();
}
}
}
}

View file

@ -0,0 +1,58 @@
using System;
using System.IO;
namespace Ryujinx.Core.OsHle.IpcServices.Android
{
static class Parcel
{
public static byte[] GetParcelData(byte[] Parcel)
{
if (Parcel == null)
{
throw new ArgumentNullException(nameof(Parcel));
}
using (MemoryStream MS = new MemoryStream(Parcel))
{
BinaryReader Reader = new BinaryReader(MS);
int DataSize = Reader.ReadInt32();
int DataOffset = Reader.ReadInt32();
int ObjsSize = Reader.ReadInt32();
int ObjsOffset = Reader.ReadInt32();
MS.Seek(DataOffset - 0x10, SeekOrigin.Current);
return Reader.ReadBytes(DataSize);
}
}
public static byte[] MakeParcel(byte[] Data, byte[] Objs)
{
if (Data == null)
{
throw new ArgumentNullException(nameof(Data));
}
if (Objs == null)
{
throw new ArgumentNullException(nameof(Objs));
}
using (MemoryStream MS = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MS);
Writer.Write(Data.Length);
Writer.Write(0x10);
Writer.Write(Objs.Length);
Writer.Write(Data.Length + 0x10);
Writer.Write(Data);
Writer.Write(Objs);
return MS.ToArray();
}
}
}
}

View file

@ -0,0 +1,31 @@
using Ryujinx.Core.OsHle.Ipc;
using System.Collections.Generic;
using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
namespace Ryujinx.Core.OsHle.IpcServices.Vi
{
class ServiceVi : IIpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public ServiceVi()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 2, GetDisplayService }
};
}
public long GetDisplayService(ServiceCtx Context)
{
int Unknown = Context.RequestData.ReadInt32();
MakeObject(Context, new IApplicationDisplayService());
return 0;
}
}
}