NvMapIoctl.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Memory;
  4. using Ryujinx.HLE.HOS.Kernel.Process;
  5. using Ryujinx.HLE.Utilities;
  6. using System.Collections.Concurrent;
  7. namespace Ryujinx.HLE.HOS.Services.Nv.NvMap
  8. {
  9. class NvMapIoctl
  10. {
  11. private const int FlagNotFreedYet = 1;
  12. private static ConcurrentDictionary<KProcess, IdDictionary> _maps;
  13. static NvMapIoctl()
  14. {
  15. _maps = new ConcurrentDictionary<KProcess, IdDictionary>();
  16. }
  17. public static int ProcessIoctl(ServiceCtx context, int cmd)
  18. {
  19. switch (cmd & 0xffff)
  20. {
  21. case 0x0101: return Create(context);
  22. case 0x0103: return FromId(context);
  23. case 0x0104: return Alloc (context);
  24. case 0x0105: return Free (context);
  25. case 0x0109: return Param (context);
  26. case 0x010e: return GetId (context);
  27. }
  28. Logger.PrintWarning(LogClass.ServiceNv, $"Unsupported Ioctl command 0x{cmd:x8}!");
  29. return NvResult.NotSupported;
  30. }
  31. private static int Create(ServiceCtx context)
  32. {
  33. long inputPosition = context.Request.GetBufferType0x21().Position;
  34. long outputPosition = context.Request.GetBufferType0x22().Position;
  35. NvMapCreate args = MemoryHelper.Read<NvMapCreate>(context.Memory, inputPosition);
  36. if (args.Size == 0)
  37. {
  38. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid size 0x{args.Size:x8}!");
  39. return NvResult.InvalidInput;
  40. }
  41. int size = IntUtils.AlignUp(args.Size, NvGpuVmm.PageSize);
  42. args.Handle = AddNvMap(context, new NvMapHandle(size));
  43. Logger.PrintInfo(LogClass.ServiceNv, $"Created map {args.Handle} with size 0x{size:x8}!");
  44. MemoryHelper.Write(context.Memory, outputPosition, args);
  45. return NvResult.Success;
  46. }
  47. private static int FromId(ServiceCtx context)
  48. {
  49. long inputPosition = context.Request.GetBufferType0x21().Position;
  50. long outputPosition = context.Request.GetBufferType0x22().Position;
  51. NvMapFromId args = MemoryHelper.Read<NvMapFromId>(context.Memory, inputPosition);
  52. NvMapHandle map = GetNvMap(context, args.Id);
  53. if (map == null)
  54. {
  55. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
  56. return NvResult.InvalidInput;
  57. }
  58. map.IncrementRefCount();
  59. args.Handle = args.Id;
  60. MemoryHelper.Write(context.Memory, outputPosition, args);
  61. return NvResult.Success;
  62. }
  63. private static int Alloc(ServiceCtx context)
  64. {
  65. long inputPosition = context.Request.GetBufferType0x21().Position;
  66. long outputPosition = context.Request.GetBufferType0x22().Position;
  67. NvMapAlloc args = MemoryHelper.Read<NvMapAlloc>(context.Memory, inputPosition);
  68. NvMapHandle map = GetNvMap(context, args.Handle);
  69. if (map == null)
  70. {
  71. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
  72. return NvResult.InvalidInput;
  73. }
  74. if ((args.Align & (args.Align - 1)) != 0)
  75. {
  76. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid alignment 0x{args.Align:x8}!");
  77. return NvResult.InvalidInput;
  78. }
  79. if ((uint)args.Align < NvGpuVmm.PageSize)
  80. {
  81. args.Align = NvGpuVmm.PageSize;
  82. }
  83. int result = NvResult.Success;
  84. if (!map.Allocated)
  85. {
  86. map.Allocated = true;
  87. map.Align = args.Align;
  88. map.Kind = (byte)args.Kind;
  89. int size = IntUtils.AlignUp(map.Size, NvGpuVmm.PageSize);
  90. long address = args.Address;
  91. if (address == 0)
  92. {
  93. //When the address is zero, we need to allocate
  94. //our own backing memory for the NvMap.
  95. //TODO: Is this allocation inside the transfer memory?
  96. result = NvResult.OutOfMemory;
  97. }
  98. if (result == NvResult.Success)
  99. {
  100. map.Size = size;
  101. map.Address = address;
  102. }
  103. }
  104. MemoryHelper.Write(context.Memory, outputPosition, args);
  105. return result;
  106. }
  107. private static int Free(ServiceCtx context)
  108. {
  109. long inputPosition = context.Request.GetBufferType0x21().Position;
  110. long outputPosition = context.Request.GetBufferType0x22().Position;
  111. NvMapFree args = MemoryHelper.Read<NvMapFree>(context.Memory, inputPosition);
  112. NvMapHandle map = GetNvMap(context, args.Handle);
  113. if (map == null)
  114. {
  115. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
  116. return NvResult.InvalidInput;
  117. }
  118. if (map.DecrementRefCount() <= 0)
  119. {
  120. DeleteNvMap(context, args.Handle);
  121. Logger.PrintInfo(LogClass.ServiceNv, $"Deleted map {args.Handle}!");
  122. args.Address = map.Address;
  123. args.Flags = 0;
  124. }
  125. else
  126. {
  127. args.Address = 0;
  128. args.Flags = FlagNotFreedYet;
  129. }
  130. args.Size = map.Size;
  131. MemoryHelper.Write(context.Memory, outputPosition, args);
  132. return NvResult.Success;
  133. }
  134. private static int Param(ServiceCtx context)
  135. {
  136. long inputPosition = context.Request.GetBufferType0x21().Position;
  137. long outputPosition = context.Request.GetBufferType0x22().Position;
  138. NvMapParam args = MemoryHelper.Read<NvMapParam>(context.Memory, inputPosition);
  139. NvMapHandle map = GetNvMap(context, args.Handle);
  140. if (map == null)
  141. {
  142. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
  143. return NvResult.InvalidInput;
  144. }
  145. switch ((NvMapHandleParam)args.Param)
  146. {
  147. case NvMapHandleParam.Size: args.Result = map.Size; break;
  148. case NvMapHandleParam.Align: args.Result = map.Align; break;
  149. case NvMapHandleParam.Heap: args.Result = 0x40000000; break;
  150. case NvMapHandleParam.Kind: args.Result = map.Kind; break;
  151. case NvMapHandleParam.Compr: args.Result = 0; break;
  152. //Note: Base is not supported and returns an error.
  153. //Any other value also returns an error.
  154. default: return NvResult.InvalidInput;
  155. }
  156. MemoryHelper.Write(context.Memory, outputPosition, args);
  157. return NvResult.Success;
  158. }
  159. private static int GetId(ServiceCtx context)
  160. {
  161. long inputPosition = context.Request.GetBufferType0x21().Position;
  162. long outputPosition = context.Request.GetBufferType0x22().Position;
  163. NvMapGetId args = MemoryHelper.Read<NvMapGetId>(context.Memory, inputPosition);
  164. NvMapHandle map = GetNvMap(context, args.Handle);
  165. if (map == null)
  166. {
  167. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
  168. return NvResult.InvalidInput;
  169. }
  170. args.Id = args.Handle;
  171. MemoryHelper.Write(context.Memory, outputPosition, args);
  172. return NvResult.Success;
  173. }
  174. private static int AddNvMap(ServiceCtx context, NvMapHandle map)
  175. {
  176. IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>
  177. {
  178. IdDictionary newDict = new IdDictionary();
  179. newDict.Add(0, new NvMapHandle());
  180. return newDict;
  181. });
  182. return dict.Add(map);
  183. }
  184. private static bool DeleteNvMap(ServiceCtx context, int handle)
  185. {
  186. if (_maps.TryGetValue(context.Process, out IdDictionary dict))
  187. {
  188. return dict.Delete(handle) != null;
  189. }
  190. return false;
  191. }
  192. public static void InitializeNvMap(ServiceCtx context)
  193. {
  194. IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>new IdDictionary());
  195. dict.Add(0, new NvMapHandle());
  196. }
  197. public static NvMapHandle GetNvMapWithFb(ServiceCtx context, int handle)
  198. {
  199. if (_maps.TryGetValue(context.Process, out IdDictionary dict))
  200. {
  201. return dict.GetData<NvMapHandle>(handle);
  202. }
  203. return null;
  204. }
  205. public static NvMapHandle GetNvMap(ServiceCtx context, int handle)
  206. {
  207. if (handle != 0 && _maps.TryGetValue(context.Process, out IdDictionary dict))
  208. {
  209. return dict.GetData<NvMapHandle>(handle);
  210. }
  211. return null;
  212. }
  213. public static void UnloadProcess(KProcess process)
  214. {
  215. _maps.TryRemove(process, out _);
  216. }
  217. }
  218. }