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;
  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. }