NvMapIoctl.cs 9.4 KB

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