1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-04 18:12:01 +00:00
Ryujinx/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs

300 lines
9.3 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.Utilities;
using System.Collections.Concurrent;
namespace Ryujinx.HLE.HOS.Services.Nv.NvMap
{
class NvMapIoctl
{
private const int FlagNotFreedYet = 1;
2018-12-01 20:01:59 +00:00
private static ConcurrentDictionary<KProcess, IdDictionary> _maps;
static NvMapIoctl()
{
2018-12-01 20:01:59 +00:00
_maps = new ConcurrentDictionary<KProcess, IdDictionary>();
}
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 0x0101: return Create(context);
case 0x0103: return FromId(context);
case 0x0104: return Alloc (context);
case 0x0105: return Free (context);
case 0x0109: return Param (context);
case 0x010e: return GetId (context);
}
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Unsupported Ioctl command 0x{cmd:x8}!");
return NvResult.NotSupported;
}
2018-12-01 20:01:59 +00:00
private static int Create(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
NvMapCreate args = MemoryHelper.Read<NvMapCreate>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
if (args.Size == 0)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid size 0x{args.Size:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
int size = IntUtils.AlignUp(args.Size, NvGpuVmm.PageSize);
2018-12-01 20:01:59 +00:00
args.Handle = AddNvMap(context, new NvMapHandle(size));
2018-12-01 20:01:59 +00:00
Logger.PrintInfo(LogClass.ServiceNv, $"Created map {args.Handle} with size 0x{size:x8}!");
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 FromId(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
NvMapFromId args = MemoryHelper.Read<NvMapFromId>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvMapHandle map = GetNvMap(context, args.Id);
2018-12-01 20:01:59 +00:00
if (map == null)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
map.IncrementRefCount();
2018-12-01 20:01:59 +00:00
args.Handle = 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 Alloc(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
NvMapAlloc args = MemoryHelper.Read<NvMapAlloc>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvMapHandle map = GetNvMap(context, args.Handle);
2018-12-01 20:01:59 +00:00
if (map == null)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
if ((args.Align & (args.Align - 1)) != 0)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid alignment 0x{args.Align:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
if ((uint)args.Align < NvGpuVmm.PageSize)
{
2018-12-01 20:01:59 +00:00
args.Align = NvGpuVmm.PageSize;
}
2018-12-01 20:01:59 +00:00
int result = NvResult.Success;
2018-12-01 20:01:59 +00:00
if (!map.Allocated)
{
2018-12-01 20:01:59 +00:00
map.Allocated = true;
2018-12-01 20:01:59 +00:00
map.Align = args.Align;
map.Kind = (byte)args.Kind;
2018-12-01 20:01:59 +00:00
int size = IntUtils.AlignUp(map.Size, NvGpuVmm.PageSize);
2018-12-01 20:01:59 +00:00
long address = args.Address;
2018-12-01 20:01:59 +00:00
if (address == 0)
{
//When the address is zero, we need to allocate
//our own backing memory for the NvMap.
//TODO: Is this allocation inside the transfer memory?
2018-12-01 20:01:59 +00:00
result = NvResult.OutOfMemory;
}
2018-12-01 20:01:59 +00:00
if (result == NvResult.Success)
{
2018-12-01 20:01:59 +00:00
map.Size = size;
map.Address = address;
}
}
2018-12-01 20:01:59 +00:00
MemoryHelper.Write(context.Memory, outputPosition, args);
2018-12-01 20:01:59 +00:00
return result;
}
2018-12-01 20:01:59 +00:00
private static int Free(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
NvMapFree args = MemoryHelper.Read<NvMapFree>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvMapHandle map = GetNvMap(context, args.Handle);
2018-12-01 20:01:59 +00:00
if (map == null)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
if (map.DecrementRefCount() <= 0)
{
2018-12-01 20:01:59 +00:00
DeleteNvMap(context, args.Handle);
2018-12-01 20:01:59 +00:00
Logger.PrintInfo(LogClass.ServiceNv, $"Deleted map {args.Handle}!");
2018-12-01 20:01:59 +00:00
args.Address = map.Address;
args.Flags = 0;
}
else
{
2018-12-01 20:01:59 +00:00
args.Address = 0;
args.Flags = FlagNotFreedYet;
}
2018-12-01 20:01:59 +00:00
args.Size = map.Size;
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 Param(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
NvMapParam args = MemoryHelper.Read<NvMapParam>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvMapHandle map = GetNvMap(context, args.Handle);
2018-12-01 20:01:59 +00:00
if (map == null)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
switch ((NvMapHandleParam)args.Param)
{
2018-12-01 20:01:59 +00:00
case NvMapHandleParam.Size: args.Result = map.Size; break;
case NvMapHandleParam.Align: args.Result = map.Align; break;
case NvMapHandleParam.Heap: args.Result = 0x40000000; break;
case NvMapHandleParam.Kind: args.Result = map.Kind; break;
case NvMapHandleParam.Compr: args.Result = 0; break;
//Note: Base is not supported and returns an error.
//Any other value also returns an error.
default: return NvResult.InvalidInput;
}
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 GetId(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
NvMapGetId args = MemoryHelper.Read<NvMapGetId>(context.Memory, inputPosition);
2018-12-01 20:01:59 +00:00
NvMapHandle map = GetNvMap(context, args.Handle);
2018-12-01 20:01:59 +00:00
if (map == null)
{
2018-12-01 20:01:59 +00:00
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
return NvResult.InvalidInput;
}
2018-12-01 20:01:59 +00:00
args.Id = args.Handle;
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 AddNvMap(ServiceCtx context, NvMapHandle map)
{
2018-12-01 20:01:59 +00:00
IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>
{
2018-12-01 20:01:59 +00:00
IdDictionary newDict = new IdDictionary();
2018-12-01 20:01:59 +00:00
newDict.Add(0, new NvMapHandle());
2018-12-01 20:01:59 +00:00
return newDict;
});
2018-12-01 20:01:59 +00:00
return dict.Add(map);
}
2018-12-01 20:01:59 +00:00
private static bool DeleteNvMap(ServiceCtx context, int handle)
{
2018-12-01 20:01:59 +00:00
if (_maps.TryGetValue(context.Process, out IdDictionary dict))
{
2018-12-01 20:01:59 +00:00
return dict.Delete(handle) != null;
}
return false;
}
2018-12-01 20:01:59 +00:00
public static void InitializeNvMap(ServiceCtx context)
{
2018-12-01 20:01:59 +00:00
IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>new IdDictionary());
2018-12-01 20:01:59 +00:00
dict.Add(0, new NvMapHandle());
}
2018-12-01 20:01:59 +00:00
public static NvMapHandle GetNvMapWithFb(ServiceCtx context, int handle)
{
2018-12-01 20:01:59 +00:00
if (_maps.TryGetValue(context.Process, out IdDictionary dict))
{
2018-12-01 20:01:59 +00:00
return dict.GetData<NvMapHandle>(handle);
}
return null;
}
2018-12-01 20:01:59 +00:00
public static NvMapHandle GetNvMap(ServiceCtx context, int handle)
{
2018-12-01 20:01:59 +00:00
if (handle != 0 && _maps.TryGetValue(context.Process, out IdDictionary dict))
{
2018-12-01 20:01:59 +00:00
return dict.GetData<NvMapHandle>(handle);
}
return null;
}
2018-12-01 20:01:59 +00:00
public static void UnloadProcess(KProcess process)
{
2018-12-01 20:01:59 +00:00
_maps.TryRemove(process, out _);
}
}
}