1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-11 07:32:01 +00:00
Ryujinx/Ryujinx.HLE/HOS/Services/Nv/NvHostCtrl/NvHostCtrlIoctl.cs

400 lines
12 KiB
C#
Raw Normal View History

using ChocolArm64.Memory;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel;
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
namespace Ryujinx.HLE.HOS.Services.Nv.NvHostCtrl
{
class NvHostCtrlIoctl
{
2018-12-01 20:01:59 +00:00
private static ConcurrentDictionary<KProcess, NvHostCtrlUserCtx> _userCtxs;
2018-12-01 20:01:59 +00:00
private static bool _isProductionMode = true;
static NvHostCtrlIoctl()
{
2018-12-01 20:01:59 +00:00
_userCtxs = new ConcurrentDictionary<KProcess, NvHostCtrlUserCtx>();
2018-12-01 20:01:59 +00:00
if (Set.NxSettings.Settings.TryGetValue("nv!rmos_set_production_mode", out object productionModeSetting))
{
2018-12-01 20:01:59 +00:00
_isProductionMode = ((string)productionModeSetting) != "0"; // Default value is ""
}
}
2018-12-01 20:01:59 +00:00
public static int ProcessIoctl(ServiceCtx context, int cmd)
{
2018-12-01 20:01:59 +00:00
switch (cmd & 0xffff)
{
2018-12-01 20:01:59 +00:00
case 0x0014: return SyncptRead (context);
case 0x0015: return SyncptIncr (context);
case 0x0016: return SyncptWait (context);
case 0x0019: return SyncptWaitEx (context);
case 0x001a: return SyncptReadMax (context);
case 0x001b: return GetConfig (context);
case 0x001d: return EventWait (context);
case 0x001e: return EventWaitAsync(context);
case 0x001f: return EventRegister (context);
}
2018-12-01 20:01:59 +00:00
throw new NotImplementedException(cmd.ToString("x8"));
}
2018-12-01 20:01:59 +00:00
private static int SyncptRead(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return SyncptReadMinOrMax(context, max: false);
}
2018-12-01 20:01:59 +00:00
private static int SyncptIncr(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
2018-12-01 20:01:59 +00:00
int id = context.Memory.ReadInt32(inputPosition);
2018-12-01 20:01:59 +00:00
if ((uint)id >= NvHostSyncpt.SyncptsCount)
{
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
GetUserCtx(context).Syncpt.Increment(id);
return NvResult.Success;
}
2018-12-01 20:01:59 +00:00
private static int SyncptWait(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return SyncptWait(context, extended: false);
}
2018-12-01 20:01:59 +00:00
private static int SyncptWaitEx(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return SyncptWait(context, extended: true);
}
2018-12-01 20:01:59 +00:00
private static int SyncptReadMax(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return SyncptReadMinOrMax(context, max: true);
}
2018-12-01 20:01:59 +00:00
private static int GetConfig(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
if (!_isProductionMode)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 20:01:59 +00:00
string domain = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0, 0x41);
string name = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0x41, 0x41);
2018-12-01 20:01:59 +00:00
if (Set.NxSettings.Settings.TryGetValue($"{domain}!{name}", out object nvSetting))
{
2018-12-01 20:01:59 +00:00
byte[] settingBuffer = new byte[0x101];
2018-12-01 20:01:59 +00:00
if (nvSetting is string stringValue)
{
2018-12-01 20:01:59 +00:00
if (stringValue.Length > 0x100)
{
2018-12-01 20:01:59 +00:00
Logger.PrintError(LogClass.ServiceNv, $"{domain}!{name} String value size is too big!");
}
else
{
2018-12-01 20:01:59 +00:00
settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
}
}
2018-12-01 20:01:59 +00:00
if (nvSetting is int intValue)
{
2018-12-01 20:01:59 +00:00
settingBuffer = BitConverter.GetBytes(intValue);
}
2018-12-01 20:01:59 +00:00
else if (nvSetting is bool boolValue)
{
2018-12-01 20:01:59 +00:00
settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
}
else
{
2018-12-01 20:01:59 +00:00
throw new NotImplementedException(nvSetting.GetType().Name);
}
2018-12-01 20:01:59 +00:00
context.Memory.WriteBytes(outputPosition + 0x82, settingBuffer);
2018-12-01 20:01:59 +00:00
Logger.PrintDebug(LogClass.ServiceNv, $"Got setting {domain}!{name}");
}
return NvResult.Success;
}
return NvResult.NotAvailableInProduction;
}
2018-12-01 20:01:59 +00:00
private static int EventWait(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return EventWait(context, async: false);
}
2018-12-01 20:01:59 +00:00
private static int EventWaitAsync(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return EventWait(context, async: true);
}
2018-12-01 20:01:59 +00:00
private static int EventRegister(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 20:01:59 +00:00
int eventId = context.Memory.ReadInt32(inputPosition);
Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");
return NvResult.Success;
}
2018-12-01 20:01:59 +00:00
private static int SyncptReadMinOrMax(ServiceCtx context, bool max)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 20:01:59 +00:00
NvHostCtrlSyncptRead args = MemoryHelper.Read<NvHostCtrlSyncptRead>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
{
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
if (max)
{
2018-12-01 20:01:59 +00:00
args.Value = GetUserCtx(context).Syncpt.GetMax(args.Id);
}
else
{
2018-12-01 20:01:59 +00:00
args.Value = GetUserCtx(context).Syncpt.GetMin(args.Id);
}
2018-12-01 20:01:59 +00:00
MemoryHelper.Write(context.Memory, outputPosition, args);
return NvResult.Success;
}
2018-12-01 20:01:59 +00:00
private static int SyncptWait(ServiceCtx context, bool extended)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 20:01:59 +00:00
NvHostCtrlSyncptWait args = MemoryHelper.Read<NvHostCtrlSyncptWait>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
2018-12-01 20:01:59 +00:00
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
{
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
int result;
2018-12-01 20:01:59 +00:00
if (syncpt.MinCompare(args.Id, args.Thresh))
{
2018-12-01 20:01:59 +00:00
result = NvResult.Success;
}
2018-12-01 20:01:59 +00:00
else if (args.Timeout == 0)
{
2018-12-01 20:01:59 +00:00
result = NvResult.TryAgain;
}
else
{
2018-12-01 20:01:59 +00:00
Logger.PrintDebug(LogClass.ServiceNv, "Waiting syncpt with timeout of " + args.Timeout + "ms...");
2018-12-01 20:01:59 +00:00
using (ManualResetEvent waitEvent = new ManualResetEvent(false))
{
2018-12-01 20:01:59 +00:00
syncpt.AddWaiter(args.Thresh, waitEvent);
//Note: Negative (> INT_MAX) timeouts aren't valid on .NET,
//in this case we just use the maximum timeout possible.
2018-12-01 20:01:59 +00:00
int timeout = args.Timeout;
2018-12-01 20:01:59 +00:00
if (timeout < -1)
{
2018-12-01 20:01:59 +00:00
timeout = int.MaxValue;
}
2018-12-01 20:01:59 +00:00
if (timeout == -1)
{
2018-12-01 20:01:59 +00:00
waitEvent.WaitOne();
2018-12-01 20:01:59 +00:00
result = NvResult.Success;
}
2018-12-01 20:01:59 +00:00
else if (waitEvent.WaitOne(timeout))
{
2018-12-01 20:01:59 +00:00
result = NvResult.Success;
}
else
{
2018-12-01 20:01:59 +00:00
result = NvResult.TimedOut;
}
}
Logger.PrintDebug(LogClass.ServiceNv, "Resuming...");
}
2018-12-01 20:01:59 +00:00
if (extended)
{
2018-12-01 20:01:59 +00:00
context.Memory.WriteInt32(outputPosition + 0xc, syncpt.GetMin(args.Id));
}
2018-12-01 20:01:59 +00:00
return result;
}
2018-12-01 20:01:59 +00:00
private static int EventWait(ServiceCtx context, bool async)
{
2018-12-01 20:01:59 +00:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 20:01:59 +00:00
NvHostCtrlSyncptWaitEx args = MemoryHelper.Read<NvHostCtrlSyncptWaitEx>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
{
return NvResult.InvalidInput;
}
void WriteArgs()
{
2018-12-01 20:01:59 +00:00
MemoryHelper.Write(context.Memory, outputPosition, args);
}
2018-12-01 20:01:59 +00:00
NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
2018-12-01 20:01:59 +00:00
if (syncpt.MinCompare(args.Id, args.Thresh))
{
2018-12-01 20:01:59 +00:00
args.Value = syncpt.GetMin(args.Id);
WriteArgs();
return NvResult.Success;
}
2018-12-01 20:01:59 +00:00
if (!async)
{
2018-12-01 20:01:59 +00:00
args.Value = 0;
}
2018-12-01 20:01:59 +00:00
if (args.Timeout == 0)
{
WriteArgs();
return NvResult.TryAgain;
}
NvHostEvent Event;
2018-12-01 20:01:59 +00:00
int result, eventIndex;
2018-12-01 20:01:59 +00:00
if (async)
{
2018-12-01 20:01:59 +00:00
eventIndex = args.Value;
2018-12-01 20:01:59 +00:00
if ((uint)eventIndex >= NvHostCtrlUserCtx.EventsCount)
{
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
Event = GetUserCtx(context).Events[eventIndex];
}
else
{
2018-12-01 20:01:59 +00:00
Event = GetFreeEvent(context, syncpt, args.Id, out eventIndex);
}
if (Event != null &&
(Event.State == NvHostEventState.Registered ||
Event.State == NvHostEventState.Free))
{
2018-12-01 20:01:59 +00:00
Event.Id = args.Id;
Event.Thresh = args.Thresh;
Event.State = NvHostEventState.Waiting;
2018-12-01 20:01:59 +00:00
if (!async)
{
2018-12-01 20:01:59 +00:00
args.Value = ((args.Id & 0xfff) << 16) | 0x10000000;
}
else
{
2018-12-01 20:01:59 +00:00
args.Value = args.Id << 4;
}
2018-12-01 20:01:59 +00:00
args.Value |= eventIndex;
2018-12-01 20:01:59 +00:00
result = NvResult.TryAgain;
}
else
{
2018-12-01 20:01:59 +00:00
result = NvResult.InvalidInput;
}
WriteArgs();
2018-12-01 20:01:59 +00:00
return result;
}
private static NvHostEvent GetFreeEvent(
2018-12-01 20:01:59 +00:00
ServiceCtx context,
NvHostSyncpt syncpt,
int id,
out int eventIndex)
{
2018-12-01 20:01:59 +00:00
NvHostEvent[] events = GetUserCtx(context).Events;
2018-12-01 20:01:59 +00:00
eventIndex = NvHostCtrlUserCtx.EventsCount;
2018-12-01 20:01:59 +00:00
int nullIndex = NvHostCtrlUserCtx.EventsCount;
2018-12-01 20:01:59 +00:00
for (int index = 0; index < NvHostCtrlUserCtx.EventsCount; index++)
{
2018-12-01 20:01:59 +00:00
NvHostEvent Event = events[index];
if (Event != null)
{
if (Event.State == NvHostEventState.Registered ||
Event.State == NvHostEventState.Free)
{
2018-12-01 20:01:59 +00:00
eventIndex = index;
2018-12-01 20:01:59 +00:00
if (Event.Id == id)
{
return Event;
}
}
}
2018-12-01 20:01:59 +00:00
else if (nullIndex == NvHostCtrlUserCtx.EventsCount)
{
2018-12-01 20:01:59 +00:00
nullIndex = index;
}
}
2018-12-01 20:01:59 +00:00
if (nullIndex < NvHostCtrlUserCtx.EventsCount)
{
2018-12-01 20:01:59 +00:00
eventIndex = nullIndex;
2018-12-01 20:01:59 +00:00
return events[nullIndex] = new NvHostEvent();
}
2018-12-01 20:01:59 +00:00
if (eventIndex < NvHostCtrlUserCtx.EventsCount)
{
2018-12-01 20:01:59 +00:00
return events[eventIndex];
}
return null;
}
2018-12-01 20:01:59 +00:00
public static NvHostCtrlUserCtx GetUserCtx(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
return _userCtxs.GetOrAdd(context.Process, (key) => new NvHostCtrlUserCtx());
}
2018-12-01 20:01:59 +00:00
public static void UnloadProcess(KProcess process)
{
2018-12-01 20:01:59 +00:00
_userCtxs.TryRemove(process, out _);
}
}
}