NvGpuASIoctl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.HOS.Services.Nv.NvMap;
  6. using System;
  7. using System.Collections.Concurrent;
  8. namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS
  9. {
  10. class NvGpuASIoctl
  11. {
  12. private const int FlagFixedOffset = 1;
  13. private const int FlagRemapSubRange = 0x100;
  14. private static ConcurrentDictionary<KProcess, NvGpuASCtx> _asCtxs;
  15. static NvGpuASIoctl()
  16. {
  17. _asCtxs = new ConcurrentDictionary<KProcess, NvGpuASCtx>();
  18. }
  19. public static int ProcessIoctl(ServiceCtx context, int cmd)
  20. {
  21. switch (cmd & 0xffff)
  22. {
  23. case 0x4101: return BindChannel (context);
  24. case 0x4102: return AllocSpace (context);
  25. case 0x4103: return FreeSpace (context);
  26. case 0x4105: return UnmapBuffer (context);
  27. case 0x4106: return MapBufferEx (context);
  28. case 0x4108: return GetVaRegions(context);
  29. case 0x4109: return InitializeEx(context);
  30. case 0x4114: return Remap (context, cmd);
  31. }
  32. throw new NotImplementedException(cmd.ToString("x8"));
  33. }
  34. private static int BindChannel(ServiceCtx context)
  35. {
  36. long inputPosition = context.Request.GetBufferType0x21().Position;
  37. long outputPosition = context.Request.GetBufferType0x22().Position;
  38. Logger.PrintStub(LogClass.ServiceNv);
  39. return NvResult.Success;
  40. }
  41. private static int AllocSpace(ServiceCtx context)
  42. {
  43. long inputPosition = context.Request.GetBufferType0x21().Position;
  44. long outputPosition = context.Request.GetBufferType0x22().Position;
  45. NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
  46. NvGpuASCtx asCtx = GetASCtx(context);
  47. ulong size = (ulong)args.Pages *
  48. (ulong)args.PageSize;
  49. int result = NvResult.Success;
  50. lock (asCtx)
  51. {
  52. //Note: When the fixed offset flag is not set,
  53. //the Offset field holds the alignment size instead.
  54. if ((args.Flags & FlagFixedOffset) != 0)
  55. {
  56. args.Offset = asCtx.Vmm.ReserveFixed(args.Offset, (long)size);
  57. }
  58. else
  59. {
  60. args.Offset = asCtx.Vmm.Reserve((long)size, args.Offset);
  61. }
  62. if (args.Offset < 0)
  63. {
  64. args.Offset = 0;
  65. Logger.PrintWarning(LogClass.ServiceNv, $"Failed to allocate size {size:x16}!");
  66. result = NvResult.OutOfMemory;
  67. }
  68. else
  69. {
  70. asCtx.AddReservation(args.Offset, (long)size);
  71. }
  72. }
  73. MemoryHelper.Write(context.Memory, outputPosition, args);
  74. return result;
  75. }
  76. private static int FreeSpace(ServiceCtx context)
  77. {
  78. long inputPosition = context.Request.GetBufferType0x21().Position;
  79. long outputPosition = context.Request.GetBufferType0x22().Position;
  80. NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
  81. NvGpuASCtx asCtx = GetASCtx(context);
  82. int result = NvResult.Success;
  83. lock (asCtx)
  84. {
  85. ulong size = (ulong)args.Pages *
  86. (ulong)args.PageSize;
  87. if (asCtx.RemoveReservation(args.Offset))
  88. {
  89. asCtx.Vmm.Free(args.Offset, (long)size);
  90. }
  91. else
  92. {
  93. Logger.PrintWarning(LogClass.ServiceNv,
  94. $"Failed to free offset 0x{args.Offset:x16} size 0x{size:x16}!");
  95. result = NvResult.InvalidInput;
  96. }
  97. }
  98. return result;
  99. }
  100. private static int UnmapBuffer(ServiceCtx context)
  101. {
  102. long inputPosition = context.Request.GetBufferType0x21().Position;
  103. long outputPosition = context.Request.GetBufferType0x22().Position;
  104. NvGpuASUnmapBuffer args = MemoryHelper.Read<NvGpuASUnmapBuffer>(context.Memory, inputPosition);
  105. NvGpuASCtx asCtx = GetASCtx(context);
  106. lock (asCtx)
  107. {
  108. if (asCtx.RemoveMap(args.Offset, out long size))
  109. {
  110. if (size != 0)
  111. {
  112. asCtx.Vmm.Free(args.Offset, size);
  113. }
  114. }
  115. else
  116. {
  117. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid buffer offset {args.Offset:x16}!");
  118. }
  119. }
  120. return NvResult.Success;
  121. }
  122. private static int MapBufferEx(ServiceCtx context)
  123. {
  124. const string mapErrorMsg = "Failed to map fixed buffer with offset 0x{0:x16} and size 0x{1:x16}!";
  125. long inputPosition = context.Request.GetBufferType0x21().Position;
  126. long outputPosition = context.Request.GetBufferType0x22().Position;
  127. NvGpuASMapBufferEx args = MemoryHelper.Read<NvGpuASMapBufferEx>(context.Memory, inputPosition);
  128. NvGpuASCtx asCtx = GetASCtx(context);
  129. NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
  130. if (map == null)
  131. {
  132. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
  133. return NvResult.InvalidInput;
  134. }
  135. long pa;
  136. if ((args.Flags & FlagRemapSubRange) != 0)
  137. {
  138. lock (asCtx)
  139. {
  140. if (asCtx.TryGetMapPhysicalAddress(args.Offset, out pa))
  141. {
  142. long va = args.Offset + args.BufferOffset;
  143. pa += args.BufferOffset;
  144. if (asCtx.Vmm.Map(pa, va, args.MappingSize) < 0)
  145. {
  146. string msg = string.Format(mapErrorMsg, va, args.MappingSize);
  147. Logger.PrintWarning(LogClass.ServiceNv, msg);
  148. return NvResult.InvalidInput;
  149. }
  150. return NvResult.Success;
  151. }
  152. else
  153. {
  154. Logger.PrintWarning(LogClass.ServiceNv, $"Address 0x{args.Offset:x16} not mapped!");
  155. return NvResult.InvalidInput;
  156. }
  157. }
  158. }
  159. pa = map.Address + args.BufferOffset;
  160. long size = args.MappingSize;
  161. if (size == 0)
  162. {
  163. size = (uint)map.Size;
  164. }
  165. int result = NvResult.Success;
  166. lock (asCtx)
  167. {
  168. //Note: When the fixed offset flag is not set,
  169. //the Offset field holds the alignment size instead.
  170. bool vaAllocated = (args.Flags & FlagFixedOffset) == 0;
  171. if (!vaAllocated)
  172. {
  173. if (asCtx.ValidateFixedBuffer(args.Offset, size))
  174. {
  175. args.Offset = asCtx.Vmm.Map(pa, args.Offset, size);
  176. }
  177. else
  178. {
  179. string msg = string.Format(mapErrorMsg, args.Offset, size);
  180. Logger.PrintWarning(LogClass.ServiceNv, msg);
  181. result = NvResult.InvalidInput;
  182. }
  183. }
  184. else
  185. {
  186. args.Offset = asCtx.Vmm.Map(pa, size);
  187. }
  188. if (args.Offset < 0)
  189. {
  190. args.Offset = 0;
  191. Logger.PrintWarning(LogClass.ServiceNv, $"Failed to map size 0x{size:x16}!");
  192. result = NvResult.InvalidInput;
  193. }
  194. else
  195. {
  196. asCtx.AddMap(args.Offset, size, pa, vaAllocated);
  197. }
  198. }
  199. MemoryHelper.Write(context.Memory, outputPosition, args);
  200. return result;
  201. }
  202. private static int GetVaRegions(ServiceCtx context)
  203. {
  204. long inputPosition = context.Request.GetBufferType0x21().Position;
  205. long outputPosition = context.Request.GetBufferType0x22().Position;
  206. Logger.PrintStub(LogClass.ServiceNv);
  207. return NvResult.Success;
  208. }
  209. private static int InitializeEx(ServiceCtx context)
  210. {
  211. long inputPosition = context.Request.GetBufferType0x21().Position;
  212. long outputPosition = context.Request.GetBufferType0x22().Position;
  213. Logger.PrintStub(LogClass.ServiceNv);
  214. return NvResult.Success;
  215. }
  216. private static int Remap(ServiceCtx context, int cmd)
  217. {
  218. int count = ((cmd >> 16) & 0xff) / 0x14;
  219. long inputPosition = context.Request.GetBufferType0x21().Position;
  220. for (int index = 0; index < count; index++, inputPosition += 0x14)
  221. {
  222. NvGpuASRemap args = MemoryHelper.Read<NvGpuASRemap>(context.Memory, inputPosition);
  223. NvGpuVmm vmm = GetASCtx(context).Vmm;
  224. NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
  225. if (map == null)
  226. {
  227. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
  228. return NvResult.InvalidInput;
  229. }
  230. long result = vmm.Map(map.Address, (long)(uint)args.Offset << 16,
  231. (long)(uint)args.Pages << 16);
  232. if (result < 0)
  233. {
  234. Logger.PrintWarning(LogClass.ServiceNv,
  235. $"Page 0x{args.Offset:x16} size 0x{args.Pages:x16} not allocated!");
  236. return NvResult.InvalidInput;
  237. }
  238. }
  239. return NvResult.Success;
  240. }
  241. public static NvGpuASCtx GetASCtx(ServiceCtx context)
  242. {
  243. return _asCtxs.GetOrAdd(context.Process, (key) => new NvGpuASCtx(context));
  244. }
  245. public static void UnloadProcess(KProcess process)
  246. {
  247. _asCtxs.TryRemove(process, out _);
  248. }
  249. }
  250. }