Ryujinx/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs

330 lines
11 KiB
C#
Raw Normal View History

using ChocolArm64.Memory;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Memory;
using Ryujinx.HLE.HOS.Kernel;
using Ryujinx.HLE.HOS.Services.Nv.NvMap;
using System;
using System.Collections.Concurrent;
namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS
{
class NvGpuASIoctl
{
private const int FlagFixedOffset = 1;
private const int FlagRemapSubRange = 0x100;
2018-12-01 21:01:59 +01:00
private static ConcurrentDictionary<KProcess, NvGpuASCtx> _asCtxs;
static NvGpuASIoctl()
{
2018-12-01 21:01:59 +01:00
_asCtxs = new ConcurrentDictionary<KProcess, NvGpuASCtx>();
}
2018-12-01 21:01:59 +01:00
public static int ProcessIoctl(ServiceCtx context, int cmd)
{
2018-12-01 21:01:59 +01:00
switch (cmd & 0xffff)
{
2018-12-01 21:01:59 +01:00
case 0x4101: return BindChannel (context);
case 0x4102: return AllocSpace (context);
case 0x4103: return FreeSpace (context);
case 0x4105: return UnmapBuffer (context);
case 0x4106: return MapBufferEx (context);
case 0x4108: return GetVaRegions(context);
case 0x4109: return InitializeEx(context);
case 0x4114: return Remap (context, cmd);
}
2018-12-01 21:01:59 +01:00
throw new NotImplementedException(cmd.ToString("x8"));
}
2018-12-01 21:01:59 +01:00
private static int BindChannel(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");
return NvResult.Success;
}
2018-12-01 21:01:59 +01:00
private static int AllocSpace(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 21:01:59 +01:00
NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
2018-12-01 21:01:59 +01:00
NvGpuASCtx asCtx = GetASCtx(context);
2018-12-01 21:01:59 +01:00
ulong size = (ulong)args.Pages *
(ulong)args.PageSize;
2018-12-01 21:01:59 +01:00
int result = NvResult.Success;
2018-12-01 21:01:59 +01:00
lock (asCtx)
{
//Note: When the fixed offset flag is not set,
//the Offset field holds the alignment size instead.
2018-12-01 21:01:59 +01:00
if ((args.Flags & FlagFixedOffset) != 0)
{
2018-12-01 21:01:59 +01:00
args.Offset = asCtx.Vmm.ReserveFixed(args.Offset, (long)size);
}
else
{
2018-12-01 21:01:59 +01:00
args.Offset = asCtx.Vmm.Reserve((long)size, args.Offset);
}
2018-12-01 21:01:59 +01:00
if (args.Offset < 0)
{
2018-12-01 21:01:59 +01:00
args.Offset = 0;
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Failed to allocate size {size:x16}!");
2018-12-01 21:01:59 +01:00
result = NvResult.OutOfMemory;
}
else
{
2018-12-01 21:01:59 +01:00
asCtx.AddReservation(args.Offset, (long)size);
}
}
2018-12-01 21:01:59 +01:00
MemoryHelper.Write(context.Memory, outputPosition, args);
2018-12-01 21:01:59 +01:00
return result;
}
2018-12-01 21:01:59 +01:00
private static int FreeSpace(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 21:01:59 +01:00
NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
2018-12-01 21:01:59 +01:00
NvGpuASCtx asCtx = GetASCtx(context);
2018-12-01 21:01:59 +01:00
int result = NvResult.Success;
2018-12-01 21:01:59 +01:00
lock (asCtx)
{
2018-12-01 21:01:59 +01:00
ulong size = (ulong)args.Pages *
(ulong)args.PageSize;
2018-12-01 21:01:59 +01:00
if (asCtx.RemoveReservation(args.Offset))
{
2018-12-01 21:01:59 +01:00
asCtx.Vmm.Free(args.Offset, (long)size);
}
else
{
Logger.PrintWarning(LogClass.ServiceNv,
2018-12-01 21:01:59 +01:00
$"Failed to free offset 0x{args.Offset:x16} size 0x{size:x16}!");
2018-12-01 21:01:59 +01:00
result = NvResult.InvalidInput;
}
}
2018-12-01 21:01:59 +01:00
return result;
}
2018-12-01 21:01:59 +01:00
private static int UnmapBuffer(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 21:01:59 +01:00
NvGpuASUnmapBuffer args = MemoryHelper.Read<NvGpuASUnmapBuffer>(context.Memory, inputPosition);
2018-12-01 21:01:59 +01:00
NvGpuASCtx asCtx = GetASCtx(context);
2018-12-01 21:01:59 +01:00
lock (asCtx)
{
2018-12-01 21:01:59 +01:00
if (asCtx.RemoveMap(args.Offset, out long size))
{
2018-12-01 21:01:59 +01:00
if (size != 0)
{
2018-12-01 21:01:59 +01:00
asCtx.Vmm.Free(args.Offset, size);
}
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid buffer offset {args.Offset:x16}!");
}
}
return NvResult.Success;
}
2018-12-01 21:01:59 +01:00
private static int MapBufferEx(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
const string mapErrorMsg = "Failed to map fixed buffer with offset 0x{0:x16} and size 0x{1:x16}!";
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
2018-12-01 21:01:59 +01:00
NvGpuASMapBufferEx args = MemoryHelper.Read<NvGpuASMapBufferEx>(context.Memory, inputPosition);
2018-12-01 21:01:59 +01:00
NvGpuASCtx asCtx = GetASCtx(context);
2018-12-01 21:01:59 +01:00
NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
2018-12-01 21:01:59 +01:00
if (map == null)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 21:01:59 +01:00
long pa;
2018-12-01 21:01:59 +01:00
if ((args.Flags & FlagRemapSubRange) != 0)
{
2018-12-01 21:01:59 +01:00
lock (asCtx)
{
2018-12-01 21:01:59 +01:00
if (asCtx.TryGetMapPhysicalAddress(args.Offset, out pa))
{
2018-12-01 21:01:59 +01:00
long va = args.Offset + args.BufferOffset;
2018-12-01 21:01:59 +01:00
pa += args.BufferOffset;
2018-12-01 21:01:59 +01:00
if (asCtx.Vmm.Map(pa, va, args.MappingSize) < 0)
{
2018-12-01 21:01:59 +01:00
string msg = string.Format(mapErrorMsg, va, args.MappingSize);
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, msg);
return NvResult.InvalidInput;
}
return NvResult.Success;
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Address 0x{args.Offset:x16} not mapped!");
return NvResult.InvalidInput;
}
}
}
2018-12-01 21:01:59 +01:00
pa = map.Address + args.BufferOffset;
2018-12-01 21:01:59 +01:00
long size = args.MappingSize;
2018-12-01 21:01:59 +01:00
if (size == 0)
{
2018-12-01 21:01:59 +01:00
size = (uint)map.Size;
}
2018-12-01 21:01:59 +01:00
int result = NvResult.Success;
2018-12-01 21:01:59 +01:00
lock (asCtx)
{
//Note: When the fixed offset flag is not set,
//the Offset field holds the alignment size instead.
2018-12-01 21:01:59 +01:00
bool vaAllocated = (args.Flags & FlagFixedOffset) == 0;
2018-12-01 21:01:59 +01:00
if (!vaAllocated)
{
2018-12-01 21:01:59 +01:00
if (asCtx.ValidateFixedBuffer(args.Offset, size))
{
2018-12-01 21:01:59 +01:00
args.Offset = asCtx.Vmm.Map(pa, args.Offset, size);
}
else
{
2018-12-01 21:01:59 +01:00
string msg = string.Format(mapErrorMsg, args.Offset, size);
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, msg);
2018-12-01 21:01:59 +01:00
result = NvResult.InvalidInput;
}
}
else
{
2018-12-01 21:01:59 +01:00
args.Offset = asCtx.Vmm.Map(pa, size);
}
2018-12-01 21:01:59 +01:00
if (args.Offset < 0)
{
2018-12-01 21:01:59 +01:00
args.Offset = 0;
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Failed to map size 0x{size:x16}!");
2018-12-01 21:01:59 +01:00
result = NvResult.InvalidInput;
}
else
{
2018-12-01 21:01:59 +01:00
asCtx.AddMap(args.Offset, size, pa, vaAllocated);
}
}
2018-12-01 21:01:59 +01:00
MemoryHelper.Write(context.Memory, outputPosition, args);
2018-12-01 21:01:59 +01:00
return result;
}
2018-12-01 21:01:59 +01:00
private static int GetVaRegions(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");
return NvResult.Success;
}
2018-12-01 21:01:59 +01:00
private static int InitializeEx(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
long outputPosition = context.Request.GetBufferType0x22().Position;
Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");
return NvResult.Success;
}
2018-12-01 21:01:59 +01:00
private static int Remap(ServiceCtx context, int cmd)
{
2018-12-01 21:01:59 +01:00
int count = ((cmd >> 16) & 0xff) / 0x14;
2018-12-01 21:01:59 +01:00
long inputPosition = context.Request.GetBufferType0x21().Position;
2018-12-01 21:01:59 +01:00
for (int index = 0; index < count; index++, inputPosition += 0x14)
{
2018-12-01 21:01:59 +01:00
NvGpuASRemap args = MemoryHelper.Read<NvGpuASRemap>(context.Memory, inputPosition);
2018-12-01 21:01:59 +01:00
NvGpuVmm vmm = GetASCtx(context).Vmm;
2018-12-01 21:01:59 +01:00
NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
2018-12-01 21:01:59 +01:00
if (map == null)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 21:01:59 +01:00
long result = vmm.Map(map.Address, (long)(uint)args.Offset << 16,
(long)(uint)args.Pages << 16);
2018-12-01 21:01:59 +01:00
if (result < 0)
{
Logger.PrintWarning(LogClass.ServiceNv,
2018-12-01 21:01:59 +01:00
$"Page 0x{args.Offset:x16} size 0x{args.Pages:x16} not allocated!");
return NvResult.InvalidInput;
}
}
return NvResult.Success;
}
2018-12-01 21:01:59 +01:00
public static NvGpuASCtx GetASCtx(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
return _asCtxs.GetOrAdd(context.Process, (key) => new NvGpuASCtx(context));
}
2018-12-01 21:01:59 +01:00
public static void UnloadProcess(KProcess process)
{
2018-12-01 21:01:59 +01:00
_asCtxs.TryRemove(process, out _);
}
}
}