IPC refactor part 3+4: New server HIPC message processor (#4188)

* IPC refactor part 3 + 4: New server HIPC message processor with source generator based serialization

* Make types match on calls to AlignUp/AlignDown

* Formatting

* Address some PR feedback

* Move BitfieldExtensions to Ryujinx.Common.Utilities and consolidate implementations

* Rename Reader/Writer to SpanReader/SpanWriter and move to Ryujinx.Common.Memory

* Implement EventType

* Address more PR feedback

* Log request processing errors since they are not normal

* Rename waitable to multiwait and add missing lock

* PR feedback

* Ac_K PR feedback
This commit is contained in:
gdkchan 2023-01-04 19:15:45 -03:00 committed by GitHub
parent c6a139a6e7
commit 08831eecf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
213 changed files with 9762 additions and 1010 deletions

View file

@ -0,0 +1,54 @@
using Ryujinx.Horizon.Sdk.Sf.Hipc;
using Ryujinx.Horizon.Sdk.Sm;
using Ryujinx.Horizon.Sm;
namespace Ryujinx.Horizon.LogManager
{
class LmIpcServer
{
private const int LogMaxSessionsCount = 42;
private const int PointerBufferSize = 0x400;
private const int MaxDomains = 31;
private const int MaxDomainObjects = 61;
private const int MaxPortsCount = 1;
private static readonly ManagerOptions _logManagerOptions = new ManagerOptions(
PointerBufferSize,
MaxDomains,
MaxDomainObjects,
false);
private static readonly ServiceName _logServiceName = ServiceName.Encode("lm");
private SmApi _sm;
private ServerManager _serverManager;
private LmLog _logServiceObject;
public void Initialize()
{
HeapAllocator allocator = new HeapAllocator();
_sm = new SmApi();
_sm.Initialize().AbortOnFailure();
_serverManager = new ServerManager(allocator, _sm, MaxPortsCount, _logManagerOptions, LogMaxSessionsCount);
_logServiceObject = new LmLog();
_serverManager.RegisterObjectForServer(_logServiceObject, _logServiceName, LogMaxSessionsCount);
}
public void ServiceRequests()
{
_serverManager.ServiceRequests();
}
public void Shutdown()
{
_serverManager.Dispose();
}
}
}

View file

@ -0,0 +1,19 @@
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Lm;
using Ryujinx.Horizon.Sdk.Sf;
namespace Ryujinx.Horizon.LogManager
{
partial class LmLog : IServiceObject
{
public LogDestination LogDestination { get; set; } = LogDestination.TargetManager;
[CmifCommand(0)]
public Result OpenLogger(out LmLogger logger, [ClientProcessId] ulong clientProcessId)
{
logger = new LmLogger(this, clientProcessId);
return Result.Success;
}
}
}

View file

@ -0,0 +1,139 @@
using Ryujinx.Common.Logging;
using Ryujinx.Common.Memory;
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Lm;
using Ryujinx.Horizon.Sdk.Sf;
using Ryujinx.Horizon.Sdk.Sf.Hipc;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Ryujinx.Horizon.LogManager
{
partial class LmLogger : IServiceObject
{
private readonly LmLog _log;
private readonly ulong _clientProcessId;
public LmLogger(LmLog log, ulong clientProcessId)
{
_log = log;
_clientProcessId = clientProcessId;
}
[CmifCommand(0)]
public Result Log([Buffer(HipcBufferFlags.In | HipcBufferFlags.AutoSelect)] Span<byte> message)
{
if (!SetProcessId(message, _clientProcessId))
{
return Result.Success;
}
Logger.Guest?.Print(LogClass.ServiceLm, LogImpl(message));
return Result.Success;
}
[CmifCommand(1)]
public Result SetDestination(LogDestination destination)
{
_log.LogDestination = destination;
return Result.Success;
}
private static bool SetProcessId(Span<byte> message, ulong processId)
{
ref LogPacketHeader header = ref MemoryMarshal.Cast<byte, LogPacketHeader>(message)[0];
uint expectedMessageSize = (uint)Unsafe.SizeOf<LogPacketHeader>() + header.PayloadSize;
if (expectedMessageSize != (uint)message.Length)
{
Logger.Warning?.Print(LogClass.ServiceLm, $"Invalid message size (expected 0x{expectedMessageSize:X} but got 0x{message.Length:X}).");
return false;
}
header.ProcessId = processId;
return true;
}
private static string LogImpl(ReadOnlySpan<byte> message)
{
SpanReader reader = new SpanReader(message);
LogPacketHeader header = reader.Read<LogPacketHeader>();
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Guest Log:\n Log level: {header.Severity}");
while (reader.Length > 0)
{
int type = ReadUleb128(ref reader);
int size = ReadUleb128(ref reader);
LogDataChunkKey field = (LogDataChunkKey)type;
string fieldStr = string.Empty;
if (field == LogDataChunkKey.Start)
{
reader.Skip(size);
continue;
}
else if (field == LogDataChunkKey.Stop)
{
break;
}
else if (field == LogDataChunkKey.Line)
{
fieldStr = $"{field}: {reader.Read<int>()}";
}
else if (field == LogDataChunkKey.DropCount)
{
fieldStr = $"{field}: {reader.Read<long>()}";
}
else if (field == LogDataChunkKey.Time)
{
fieldStr = $"{field}: {reader.Read<long>()}s";
}
else if (field < LogDataChunkKey.Count)
{
fieldStr = $"{field}: '{Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd()}'";
}
else
{
fieldStr = $"Field{field}: '{Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd()}'";
}
sb.AppendLine($" {fieldStr}");
}
return sb.ToString();
}
private static int ReadUleb128(ref SpanReader reader)
{
int result = 0;
int count = 0;
byte encoded;
do
{
encoded = reader.Read<byte>();
result += (encoded & 0x7F) << (7 * count);
count++;
} while ((encoded & 0x80) != 0);
return result;
}
}
}

View file

@ -0,0 +1,14 @@
namespace Ryujinx.Horizon.LogManager
{
class LmMain : IService
{
public static void Main()
{
LmIpcServer ipcServer = new LmIpcServer();
ipcServer.Initialize();
ipcServer.ServiceRequests();
ipcServer.Shutdown();
}
}
}