NvHostCtrlIoctl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Kernel.Process;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Text;
  7. using System.Threading;
  8. namespace Ryujinx.HLE.HOS.Services.Nv.NvHostCtrl
  9. {
  10. class NvHostCtrlIoctl
  11. {
  12. private static ConcurrentDictionary<KProcess, NvHostCtrlUserCtx> _userCtxs;
  13. private static bool _isProductionMode = true;
  14. static NvHostCtrlIoctl()
  15. {
  16. _userCtxs = new ConcurrentDictionary<KProcess, NvHostCtrlUserCtx>();
  17. if (Set.NxSettings.Settings.TryGetValue("nv!rmos_set_production_mode", out object productionModeSetting))
  18. {
  19. _isProductionMode = ((string)productionModeSetting) != "0"; // Default value is ""
  20. }
  21. }
  22. public static int ProcessIoctl(ServiceCtx context, int cmd)
  23. {
  24. switch (cmd & 0xffff)
  25. {
  26. case 0x0014: return SyncptRead (context);
  27. case 0x0015: return SyncptIncr (context);
  28. case 0x0016: return SyncptWait (context);
  29. case 0x0019: return SyncptWaitEx (context);
  30. case 0x001a: return SyncptReadMax (context);
  31. case 0x001b: return GetConfig (context);
  32. case 0x001d: return EventWait (context);
  33. case 0x001e: return EventWaitAsync(context);
  34. case 0x001f: return EventRegister (context);
  35. }
  36. throw new NotImplementedException(cmd.ToString("x8"));
  37. }
  38. private static int SyncptRead(ServiceCtx context)
  39. {
  40. return SyncptReadMinOrMax(context, max: false);
  41. }
  42. private static int SyncptIncr(ServiceCtx context)
  43. {
  44. long inputPosition = context.Request.GetBufferType0x21().Position;
  45. int id = context.Memory.ReadInt32(inputPosition);
  46. if ((uint)id >= NvHostSyncpt.SyncptsCount)
  47. {
  48. return NvResult.InvalidInput;
  49. }
  50. GetUserCtx(context).Syncpt.Increment(id);
  51. return NvResult.Success;
  52. }
  53. private static int SyncptWait(ServiceCtx context)
  54. {
  55. return SyncptWait(context, extended: false);
  56. }
  57. private static int SyncptWaitEx(ServiceCtx context)
  58. {
  59. return SyncptWait(context, extended: true);
  60. }
  61. private static int SyncptReadMax(ServiceCtx context)
  62. {
  63. return SyncptReadMinOrMax(context, max: true);
  64. }
  65. private static int GetConfig(ServiceCtx context)
  66. {
  67. if (!_isProductionMode)
  68. {
  69. long inputPosition = context.Request.GetBufferType0x21().Position;
  70. long outputPosition = context.Request.GetBufferType0x22().Position;
  71. string domain = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0, 0x41);
  72. string name = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0x41, 0x41);
  73. if (Set.NxSettings.Settings.TryGetValue($"{domain}!{name}", out object nvSetting))
  74. {
  75. byte[] settingBuffer = new byte[0x101];
  76. if (nvSetting is string stringValue)
  77. {
  78. if (stringValue.Length > 0x100)
  79. {
  80. Logger.PrintError(LogClass.ServiceNv, $"{domain}!{name} String value size is too big!");
  81. }
  82. else
  83. {
  84. settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
  85. }
  86. }
  87. if (nvSetting is int intValue)
  88. {
  89. settingBuffer = BitConverter.GetBytes(intValue);
  90. }
  91. else if (nvSetting is bool boolValue)
  92. {
  93. settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
  94. }
  95. else
  96. {
  97. throw new NotImplementedException(nvSetting.GetType().Name);
  98. }
  99. context.Memory.WriteBytes(outputPosition + 0x82, settingBuffer);
  100. Logger.PrintDebug(LogClass.ServiceNv, $"Got setting {domain}!{name}");
  101. }
  102. return NvResult.Success;
  103. }
  104. return NvResult.NotAvailableInProduction;
  105. }
  106. private static int EventWait(ServiceCtx context)
  107. {
  108. return EventWait(context, async: false);
  109. }
  110. private static int EventWaitAsync(ServiceCtx context)
  111. {
  112. return EventWait(context, async: true);
  113. }
  114. private static int EventRegister(ServiceCtx context)
  115. {
  116. long inputPosition = context.Request.GetBufferType0x21().Position;
  117. long outputPosition = context.Request.GetBufferType0x22().Position;
  118. int eventId = context.Memory.ReadInt32(inputPosition);
  119. Logger.PrintStub(LogClass.ServiceNv);
  120. return NvResult.Success;
  121. }
  122. private static int SyncptReadMinOrMax(ServiceCtx context, bool max)
  123. {
  124. long inputPosition = context.Request.GetBufferType0x21().Position;
  125. long outputPosition = context.Request.GetBufferType0x22().Position;
  126. NvHostCtrlSyncptRead args = MemoryHelper.Read<NvHostCtrlSyncptRead>(context.Memory, inputPosition);
  127. if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
  128. {
  129. return NvResult.InvalidInput;
  130. }
  131. if (max)
  132. {
  133. args.Value = GetUserCtx(context).Syncpt.GetMax(args.Id);
  134. }
  135. else
  136. {
  137. args.Value = GetUserCtx(context).Syncpt.GetMin(args.Id);
  138. }
  139. MemoryHelper.Write(context.Memory, outputPosition, args);
  140. return NvResult.Success;
  141. }
  142. private static int SyncptWait(ServiceCtx context, bool extended)
  143. {
  144. long inputPosition = context.Request.GetBufferType0x21().Position;
  145. long outputPosition = context.Request.GetBufferType0x22().Position;
  146. NvHostCtrlSyncptWait args = MemoryHelper.Read<NvHostCtrlSyncptWait>(context.Memory, inputPosition);
  147. NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
  148. if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
  149. {
  150. return NvResult.InvalidInput;
  151. }
  152. int result;
  153. if (syncpt.MinCompare(args.Id, args.Thresh))
  154. {
  155. result = NvResult.Success;
  156. }
  157. else if (args.Timeout == 0)
  158. {
  159. result = NvResult.TryAgain;
  160. }
  161. else
  162. {
  163. Logger.PrintDebug(LogClass.ServiceNv, "Waiting syncpt with timeout of " + args.Timeout + "ms...");
  164. using (ManualResetEvent waitEvent = new ManualResetEvent(false))
  165. {
  166. syncpt.AddWaiter(args.Thresh, waitEvent);
  167. //Note: Negative (> INT_MAX) timeouts aren't valid on .NET,
  168. //in this case we just use the maximum timeout possible.
  169. int timeout = args.Timeout;
  170. if (timeout < -1)
  171. {
  172. timeout = int.MaxValue;
  173. }
  174. if (timeout == -1)
  175. {
  176. waitEvent.WaitOne();
  177. result = NvResult.Success;
  178. }
  179. else if (waitEvent.WaitOne(timeout))
  180. {
  181. result = NvResult.Success;
  182. }
  183. else
  184. {
  185. result = NvResult.TimedOut;
  186. }
  187. }
  188. Logger.PrintDebug(LogClass.ServiceNv, "Resuming...");
  189. }
  190. if (extended)
  191. {
  192. context.Memory.WriteInt32(outputPosition + 0xc, syncpt.GetMin(args.Id));
  193. }
  194. return result;
  195. }
  196. private static int EventWait(ServiceCtx context, bool async)
  197. {
  198. long inputPosition = context.Request.GetBufferType0x21().Position;
  199. long outputPosition = context.Request.GetBufferType0x22().Position;
  200. NvHostCtrlSyncptWaitEx args = MemoryHelper.Read<NvHostCtrlSyncptWaitEx>(context.Memory, inputPosition);
  201. if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
  202. {
  203. return NvResult.InvalidInput;
  204. }
  205. void WriteArgs()
  206. {
  207. MemoryHelper.Write(context.Memory, outputPosition, args);
  208. }
  209. NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
  210. if (syncpt.MinCompare(args.Id, args.Thresh))
  211. {
  212. args.Value = syncpt.GetMin(args.Id);
  213. WriteArgs();
  214. return NvResult.Success;
  215. }
  216. if (!async)
  217. {
  218. args.Value = 0;
  219. }
  220. if (args.Timeout == 0)
  221. {
  222. WriteArgs();
  223. return NvResult.TryAgain;
  224. }
  225. NvHostEvent Event;
  226. int result, eventIndex;
  227. if (async)
  228. {
  229. eventIndex = args.Value;
  230. if ((uint)eventIndex >= NvHostCtrlUserCtx.EventsCount)
  231. {
  232. return NvResult.InvalidInput;
  233. }
  234. Event = GetUserCtx(context).Events[eventIndex];
  235. }
  236. else
  237. {
  238. Event = GetFreeEvent(context, syncpt, args.Id, out eventIndex);
  239. }
  240. if (Event != null &&
  241. (Event.State == NvHostEventState.Registered ||
  242. Event.State == NvHostEventState.Free))
  243. {
  244. Event.Id = args.Id;
  245. Event.Thresh = args.Thresh;
  246. Event.State = NvHostEventState.Waiting;
  247. if (!async)
  248. {
  249. args.Value = ((args.Id & 0xfff) << 16) | 0x10000000;
  250. }
  251. else
  252. {
  253. args.Value = args.Id << 4;
  254. }
  255. args.Value |= eventIndex;
  256. result = NvResult.TryAgain;
  257. }
  258. else
  259. {
  260. result = NvResult.InvalidInput;
  261. }
  262. WriteArgs();
  263. return result;
  264. }
  265. private static NvHostEvent GetFreeEvent(
  266. ServiceCtx context,
  267. NvHostSyncpt syncpt,
  268. int id,
  269. out int eventIndex)
  270. {
  271. NvHostEvent[] events = GetUserCtx(context).Events;
  272. eventIndex = NvHostCtrlUserCtx.EventsCount;
  273. int nullIndex = NvHostCtrlUserCtx.EventsCount;
  274. for (int index = 0; index < NvHostCtrlUserCtx.EventsCount; index++)
  275. {
  276. NvHostEvent Event = events[index];
  277. if (Event != null)
  278. {
  279. if (Event.State == NvHostEventState.Registered ||
  280. Event.State == NvHostEventState.Free)
  281. {
  282. eventIndex = index;
  283. if (Event.Id == id)
  284. {
  285. return Event;
  286. }
  287. }
  288. }
  289. else if (nullIndex == NvHostCtrlUserCtx.EventsCount)
  290. {
  291. nullIndex = index;
  292. }
  293. }
  294. if (nullIndex < NvHostCtrlUserCtx.EventsCount)
  295. {
  296. eventIndex = nullIndex;
  297. return events[nullIndex] = new NvHostEvent();
  298. }
  299. if (eventIndex < NvHostCtrlUserCtx.EventsCount)
  300. {
  301. return events[eventIndex];
  302. }
  303. return null;
  304. }
  305. public static NvHostCtrlUserCtx GetUserCtx(ServiceCtx context)
  306. {
  307. return _userCtxs.GetOrAdd(context.Process, (key) => new NvHostCtrlUserCtx());
  308. }
  309. public static void UnloadProcess(KProcess process)
  310. {
  311. _userCtxs.TryRemove(process, out _);
  312. }
  313. }
  314. }