INvDrvServices.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Cpu;
  4. using Ryujinx.HLE.Exceptions;
  5. using Ryujinx.HLE.HOS.Ipc;
  6. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices;
  7. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostAsGpu;
  8. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel;
  9. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl;
  10. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrlGpu;
  11. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostDbgGpu;
  12. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostProfGpu;
  13. using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
  14. using Ryujinx.HLE.HOS.Services.Nv.Types;
  15. using Ryujinx.Memory;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Reflection;
  19. namespace Ryujinx.HLE.HOS.Services.Nv
  20. {
  21. [Service("nvdrv")]
  22. [Service("nvdrv:a")]
  23. [Service("nvdrv:s")]
  24. [Service("nvdrv:t")]
  25. class INvDrvServices : IpcService
  26. {
  27. private static readonly List<string> _deviceFileDebugRegistry = new List<string>()
  28. {
  29. "/dev/nvhost-dbg-gpu",
  30. "/dev/nvhost-prof-gpu"
  31. };
  32. private static readonly Dictionary<string, Type> _deviceFileRegistry = new Dictionary<string, Type>()
  33. {
  34. { "/dev/nvmap", typeof(NvMapDeviceFile) },
  35. { "/dev/nvhost-ctrl", typeof(NvHostCtrlDeviceFile) },
  36. { "/dev/nvhost-ctrl-gpu", typeof(NvHostCtrlGpuDeviceFile) },
  37. { "/dev/nvhost-as-gpu", typeof(NvHostAsGpuDeviceFile) },
  38. { "/dev/nvhost-gpu", typeof(NvHostGpuDeviceFile) },
  39. //{ "/dev/nvhost-msenc", typeof(NvHostChannelDeviceFile) },
  40. { "/dev/nvhost-nvdec", typeof(NvHostChannelDeviceFile) },
  41. //{ "/dev/nvhost-nvjpg", typeof(NvHostChannelDeviceFile) },
  42. { "/dev/nvhost-vic", typeof(NvHostChannelDeviceFile) },
  43. //{ "/dev/nvhost-display", typeof(NvHostChannelDeviceFile) },
  44. { "/dev/nvhost-dbg-gpu", typeof(NvHostDbgGpuDeviceFile) },
  45. { "/dev/nvhost-prof-gpu", typeof(NvHostProfGpuDeviceFile) },
  46. };
  47. public static IdDictionary DeviceFileIdRegistry = new IdDictionary();
  48. private IVirtualMemoryManager _clientMemory;
  49. private ulong _owner;
  50. private bool _transferMemInitialized = false;
  51. // TODO: This should call set:sys::GetDebugModeFlag
  52. private bool _debugModeEnabled = false;
  53. public INvDrvServices(ServiceCtx context) : base(context.Device.System.NvDrvServer)
  54. {
  55. _owner = 0;
  56. }
  57. private NvResult Open(ServiceCtx context, string path, out int fd)
  58. {
  59. fd = -1;
  60. if (!_debugModeEnabled && _deviceFileDebugRegistry.Contains(path))
  61. {
  62. return NvResult.NotSupported;
  63. }
  64. if (_deviceFileRegistry.TryGetValue(path, out Type deviceFileClass))
  65. {
  66. ConstructorInfo constructor = deviceFileClass.GetConstructor(new Type[] { typeof(ServiceCtx), typeof(IVirtualMemoryManager), typeof(ulong) });
  67. NvDeviceFile deviceFile = (NvDeviceFile)constructor.Invoke(new object[] { context, _clientMemory, _owner });
  68. deviceFile.Path = path;
  69. fd = DeviceFileIdRegistry.Add(deviceFile);
  70. return NvResult.Success;
  71. }
  72. Logger.Warning?.Print(LogClass.ServiceNv, $"Cannot find file device \"{path}\"!");
  73. return NvResult.FileOperationFailed;
  74. }
  75. private NvResult GetIoctlArgument(ServiceCtx context, NvIoctl ioctlCommand, out Span<byte> arguments)
  76. {
  77. (ulong inputDataPosition, ulong inputDataSize) = context.Request.GetBufferType0x21(0);
  78. (ulong outputDataPosition, ulong outputDataSize) = context.Request.GetBufferType0x22(0);
  79. NvIoctl.Direction ioctlDirection = ioctlCommand.DirectionValue;
  80. uint ioctlSize = ioctlCommand.Size;
  81. bool isRead = (ioctlDirection & NvIoctl.Direction.Read) != 0;
  82. bool isWrite = (ioctlDirection & NvIoctl.Direction.Write) != 0;
  83. if ((isWrite && ioctlSize > outputDataSize) || (isRead && ioctlSize > inputDataSize))
  84. {
  85. arguments = null;
  86. Logger.Warning?.Print(LogClass.ServiceNv, "Ioctl size inconsistency found!");
  87. return NvResult.InvalidSize;
  88. }
  89. if (isRead && isWrite)
  90. {
  91. if (outputDataSize < inputDataSize)
  92. {
  93. arguments = null;
  94. Logger.Warning?.Print(LogClass.ServiceNv, "Ioctl size inconsistency found!");
  95. return NvResult.InvalidSize;
  96. }
  97. byte[] outputData = new byte[outputDataSize];
  98. byte[] temp = new byte[inputDataSize];
  99. context.Memory.Read(inputDataPosition, temp);
  100. Buffer.BlockCopy(temp, 0, outputData, 0, temp.Length);
  101. arguments = new Span<byte>(outputData);
  102. }
  103. else if (isWrite)
  104. {
  105. byte[] outputData = new byte[outputDataSize];
  106. arguments = new Span<byte>(outputData);
  107. }
  108. else
  109. {
  110. byte[] temp = new byte[inputDataSize];
  111. context.Memory.Read(inputDataPosition, temp);
  112. arguments = new Span<byte>(temp);
  113. }
  114. return NvResult.Success;
  115. }
  116. private NvResult GetDeviceFileFromFd(int fd, out NvDeviceFile deviceFile)
  117. {
  118. deviceFile = null;
  119. if (fd < 0)
  120. {
  121. return NvResult.InvalidParameter;
  122. }
  123. deviceFile = DeviceFileIdRegistry.GetData<NvDeviceFile>(fd);
  124. if (deviceFile == null)
  125. {
  126. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid file descriptor {fd}");
  127. return NvResult.NotImplemented;
  128. }
  129. if (deviceFile.Owner != _owner)
  130. {
  131. return NvResult.AccessDenied;
  132. }
  133. return NvResult.Success;
  134. }
  135. private NvResult EnsureInitialized()
  136. {
  137. if (_owner == 0)
  138. {
  139. Logger.Warning?.Print(LogClass.ServiceNv, "INvDrvServices is not initialized!");
  140. return NvResult.NotInitialized;
  141. }
  142. return NvResult.Success;
  143. }
  144. private static NvResult ConvertInternalErrorCode(NvInternalResult errorCode)
  145. {
  146. switch (errorCode)
  147. {
  148. case NvInternalResult.Success:
  149. return NvResult.Success;
  150. case NvInternalResult.Unknown0x72:
  151. return NvResult.AlreadyAllocated;
  152. case NvInternalResult.TimedOut:
  153. case NvInternalResult.TryAgain:
  154. case NvInternalResult.Interrupted:
  155. return NvResult.Timeout;
  156. case NvInternalResult.InvalidAddress:
  157. return NvResult.InvalidAddress;
  158. case NvInternalResult.NotSupported:
  159. case NvInternalResult.Unknown0x18:
  160. return NvResult.NotSupported;
  161. case NvInternalResult.InvalidState:
  162. return NvResult.InvalidState;
  163. case NvInternalResult.ReadOnlyAttribute:
  164. return NvResult.ReadOnlyAttribute;
  165. case NvInternalResult.NoSpaceLeft:
  166. case NvInternalResult.FileTooBig:
  167. return NvResult.InvalidSize;
  168. case NvInternalResult.FileTableOverflow:
  169. case NvInternalResult.BadFileNumber:
  170. return NvResult.FileOperationFailed;
  171. case NvInternalResult.InvalidInput:
  172. return NvResult.InvalidValue;
  173. case NvInternalResult.NotADirectory:
  174. return NvResult.DirectoryOperationFailed;
  175. case NvInternalResult.Busy:
  176. return NvResult.Busy;
  177. case NvInternalResult.BadAddress:
  178. return NvResult.InvalidAddress;
  179. case NvInternalResult.AccessDenied:
  180. case NvInternalResult.OperationNotPermitted:
  181. return NvResult.AccessDenied;
  182. case NvInternalResult.OutOfMemory:
  183. return NvResult.InsufficientMemory;
  184. case NvInternalResult.DeviceNotFound:
  185. return NvResult.ModuleNotPresent;
  186. case NvInternalResult.IoError:
  187. return NvResult.ResourceError;
  188. default:
  189. return NvResult.IoctlFailed;
  190. }
  191. }
  192. [CommandHipc(0)]
  193. // Open(buffer<bytes, 5> path) -> (s32 fd, u32 error_code)
  194. public ResultCode Open(ServiceCtx context)
  195. {
  196. NvResult errorCode = EnsureInitialized();
  197. int fd = -1;
  198. if (errorCode == NvResult.Success)
  199. {
  200. ulong pathPtr = context.Request.SendBuff[0].Position;
  201. ulong pathSize = context.Request.SendBuff[0].Size;
  202. string path = MemoryHelper.ReadAsciiString(context.Memory, pathPtr, (long)pathSize);
  203. errorCode = Open(context, path, out fd);
  204. }
  205. context.ResponseData.Write(fd);
  206. context.ResponseData.Write((uint)errorCode);
  207. return ResultCode.Success;
  208. }
  209. [CommandHipc(1)]
  210. // Ioctl(s32 fd, u32 ioctl_cmd, buffer<bytes, 0x21> in_args) -> (u32 error_code, buffer<bytes, 0x22> out_args)
  211. public ResultCode Ioctl(ServiceCtx context)
  212. {
  213. NvResult errorCode = EnsureInitialized();
  214. if (errorCode == NvResult.Success)
  215. {
  216. int fd = context.RequestData.ReadInt32();
  217. NvIoctl ioctlCommand = context.RequestData.ReadStruct<NvIoctl>();
  218. errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
  219. if (errorCode == NvResult.Success)
  220. {
  221. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  222. if (errorCode == NvResult.Success)
  223. {
  224. NvInternalResult internalResult = deviceFile.Ioctl(ioctlCommand, arguments);
  225. if (internalResult == NvInternalResult.NotImplemented)
  226. {
  227. throw new NvIoctlNotImplementedException(context, deviceFile, ioctlCommand);
  228. }
  229. errorCode = ConvertInternalErrorCode(internalResult);
  230. if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
  231. {
  232. context.Memory.Write(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
  233. }
  234. }
  235. }
  236. }
  237. context.ResponseData.Write((uint)errorCode);
  238. return ResultCode.Success;
  239. }
  240. [CommandHipc(2)]
  241. // Close(s32 fd) -> u32 error_code
  242. public ResultCode Close(ServiceCtx context)
  243. {
  244. NvResult errorCode = EnsureInitialized();
  245. if (errorCode == NvResult.Success)
  246. {
  247. int fd = context.RequestData.ReadInt32();
  248. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  249. if (errorCode == NvResult.Success)
  250. {
  251. deviceFile.Close();
  252. DeviceFileIdRegistry.Delete(fd);
  253. }
  254. }
  255. context.ResponseData.Write((uint)errorCode);
  256. return ResultCode.Success;
  257. }
  258. [CommandHipc(3)]
  259. // Initialize(u32 transfer_memory_size, handle<copy, process> current_process, handle<copy, transfer_memory> transfer_memory) -> u32 error_code
  260. public ResultCode Initialize(ServiceCtx context)
  261. {
  262. long transferMemSize = context.RequestData.ReadInt64();
  263. int transferMemHandle = context.Request.HandleDesc.ToCopy[1];
  264. // TODO: When transfer memory will be implemented, this could be removed.
  265. _transferMemInitialized = true;
  266. int clientHandle = context.Request.HandleDesc.ToCopy[0];
  267. _clientMemory = context.Process.HandleTable.GetKProcess(clientHandle).CpuMemory;
  268. context.Device.System.KernelContext.Syscall.GetProcessId(out _owner, clientHandle);
  269. context.ResponseData.Write((uint)NvResult.Success);
  270. // Close the process and transfer memory handles immediately as we don't use them.
  271. context.Device.System.KernelContext.Syscall.CloseHandle(clientHandle);
  272. context.Device.System.KernelContext.Syscall.CloseHandle(transferMemHandle);
  273. return ResultCode.Success;
  274. }
  275. [CommandHipc(4)]
  276. // QueryEvent(s32 fd, u32 event_id) -> (u32, handle<copy, event>)
  277. public ResultCode QueryEvent(ServiceCtx context)
  278. {
  279. NvResult errorCode = EnsureInitialized();
  280. if (errorCode == NvResult.Success)
  281. {
  282. int fd = context.RequestData.ReadInt32();
  283. uint eventId = context.RequestData.ReadUInt32();
  284. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  285. if (errorCode == NvResult.Success)
  286. {
  287. NvInternalResult internalResult = deviceFile.QueryEvent(out int eventHandle, eventId);
  288. if (internalResult == NvInternalResult.NotImplemented)
  289. {
  290. throw new NvQueryEventNotImplementedException(context, deviceFile, eventId);
  291. }
  292. errorCode = ConvertInternalErrorCode(internalResult);
  293. if (errorCode == NvResult.Success)
  294. {
  295. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(eventHandle);
  296. }
  297. }
  298. }
  299. context.ResponseData.Write((uint)errorCode);
  300. return ResultCode.Success;
  301. }
  302. [CommandHipc(5)]
  303. // MapSharedMemory(s32 fd, u32 argument, handle<copy, shared_memory>) -> u32 error_code
  304. public ResultCode MapSharedMemory(ServiceCtx context)
  305. {
  306. NvResult errorCode = EnsureInitialized();
  307. if (errorCode == NvResult.Success)
  308. {
  309. int fd = context.RequestData.ReadInt32();
  310. uint argument = context.RequestData.ReadUInt32();
  311. int sharedMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  312. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  313. if (errorCode == NvResult.Success)
  314. {
  315. errorCode = ConvertInternalErrorCode(deviceFile.MapSharedMemory(sharedMemoryHandle, argument));
  316. }
  317. }
  318. context.ResponseData.Write((uint)errorCode);
  319. return ResultCode.Success;
  320. }
  321. [CommandHipc(6)]
  322. // GetStatus() -> (unknown<0x20>, u32 error_code)
  323. public ResultCode GetStatus(ServiceCtx context)
  324. {
  325. // TODO: When transfer memory will be implemented, check if it's mapped instead.
  326. if (_transferMemInitialized)
  327. {
  328. // TODO: Populate values when more RE will be done.
  329. NvStatus nvStatus = new NvStatus
  330. {
  331. MemoryValue1 = 0, // GetMemStats(transfer_memory + 0x60, 3)
  332. MemoryValue2 = 0, // GetMemStats(transfer_memory + 0x60, 5)
  333. MemoryValue3 = 0, // transfer_memory + 0x78
  334. MemoryValue4 = 0 // transfer_memory + 0x80
  335. };
  336. context.ResponseData.WriteStruct(nvStatus);
  337. context.ResponseData.Write((uint)NvResult.Success);
  338. Logger.Stub?.PrintStub(LogClass.ServiceNv);
  339. }
  340. else
  341. {
  342. context.ResponseData.Write((uint)NvResult.NotInitialized);
  343. }
  344. return ResultCode.Success;
  345. }
  346. [CommandHipc(7)]
  347. // ForceSetClientPid(u64) -> u32 error_code
  348. public ResultCode ForceSetClientPid(ServiceCtx context)
  349. {
  350. throw new ServiceNotImplementedException(this, context);
  351. }
  352. [CommandHipc(8)]
  353. // SetClientPID(u64, pid) -> u32 error_code
  354. public ResultCode SetClientPid(ServiceCtx context)
  355. {
  356. long pid = context.RequestData.ReadInt64();
  357. context.ResponseData.Write(0);
  358. return ResultCode.Success;
  359. }
  360. [CommandHipc(9)]
  361. // DumpGraphicsMemoryInfo()
  362. public ResultCode DumpGraphicsMemoryInfo(ServiceCtx context)
  363. {
  364. Logger.Stub?.PrintStub(LogClass.ServiceNv);
  365. return ResultCode.Success;
  366. }
  367. [CommandHipc(10)] // 3.0.0+
  368. // InitializeDevtools(u32, handle<copy>) -> u32 error_code;
  369. public ResultCode InitializeDevtools(ServiceCtx context)
  370. {
  371. throw new ServiceNotImplementedException(this, context);
  372. }
  373. [CommandHipc(11)] // 3.0.0+
  374. // Ioctl2(s32 fd, u32 ioctl_cmd, buffer<bytes, 0x21> in_args, buffer<bytes, 0x21> inline_in_buffer) -> (u32 error_code, buffer<bytes, 0x22> out_args)
  375. public ResultCode Ioctl2(ServiceCtx context)
  376. {
  377. NvResult errorCode = EnsureInitialized();
  378. if (errorCode == NvResult.Success)
  379. {
  380. int fd = context.RequestData.ReadInt32();
  381. NvIoctl ioctlCommand = context.RequestData.ReadStruct<NvIoctl>();
  382. (ulong inlineInBufferPosition, ulong inlineInBufferSize) = context.Request.GetBufferType0x21(1);
  383. errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
  384. byte[] temp = new byte[inlineInBufferSize];
  385. context.Memory.Read(inlineInBufferPosition, temp);
  386. Span<byte> inlineInBuffer = new Span<byte>(temp);
  387. if (errorCode == NvResult.Success)
  388. {
  389. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  390. if (errorCode == NvResult.Success)
  391. {
  392. NvInternalResult internalResult = deviceFile.Ioctl2(ioctlCommand, arguments, inlineInBuffer);
  393. if (internalResult == NvInternalResult.NotImplemented)
  394. {
  395. throw new NvIoctlNotImplementedException(context, deviceFile, ioctlCommand);
  396. }
  397. errorCode = ConvertInternalErrorCode(internalResult);
  398. if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
  399. {
  400. context.Memory.Write(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
  401. }
  402. }
  403. }
  404. }
  405. context.ResponseData.Write((uint)errorCode);
  406. return ResultCode.Success;
  407. }
  408. [CommandHipc(12)] // 3.0.0+
  409. // Ioctl3(s32 fd, u32 ioctl_cmd, buffer<bytes, 0x21> in_args) -> (u32 error_code, buffer<bytes, 0x22> out_args, buffer<bytes, 0x22> inline_out_buffer)
  410. public ResultCode Ioctl3(ServiceCtx context)
  411. {
  412. NvResult errorCode = EnsureInitialized();
  413. if (errorCode == NvResult.Success)
  414. {
  415. int fd = context.RequestData.ReadInt32();
  416. NvIoctl ioctlCommand = context.RequestData.ReadStruct<NvIoctl>();
  417. (ulong inlineOutBufferPosition, ulong inlineOutBufferSize) = context.Request.GetBufferType0x22(1);
  418. errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
  419. byte[] temp = new byte[inlineOutBufferSize];
  420. context.Memory.Read(inlineOutBufferPosition, temp);
  421. Span<byte> inlineOutBuffer = new Span<byte>(temp);
  422. if (errorCode == NvResult.Success)
  423. {
  424. errorCode = GetDeviceFileFromFd(fd, out NvDeviceFile deviceFile);
  425. if (errorCode == NvResult.Success)
  426. {
  427. NvInternalResult internalResult = deviceFile.Ioctl3(ioctlCommand, arguments, inlineOutBuffer);
  428. if (internalResult == NvInternalResult.NotImplemented)
  429. {
  430. throw new NvIoctlNotImplementedException(context, deviceFile, ioctlCommand);
  431. }
  432. errorCode = ConvertInternalErrorCode(internalResult);
  433. if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
  434. {
  435. context.Memory.Write(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
  436. context.Memory.Write(inlineOutBufferPosition, inlineOutBuffer.ToArray());
  437. }
  438. }
  439. }
  440. }
  441. context.ResponseData.Write((uint)errorCode);
  442. return ResultCode.Success;
  443. }
  444. [CommandHipc(13)] // 3.0.0+
  445. // FinishInitialize(unknown<8>)
  446. public ResultCode FinishInitialize(ServiceCtx context)
  447. {
  448. Logger.Stub?.PrintStub(LogClass.ServiceNv);
  449. return ResultCode.Success;
  450. }
  451. public static void Destroy()
  452. {
  453. NvHostChannelDeviceFile.Destroy();
  454. foreach (object entry in DeviceFileIdRegistry.Values)
  455. {
  456. NvDeviceFile deviceFile = (NvDeviceFile)entry;
  457. deviceFile.Close();
  458. }
  459. DeviceFileIdRegistry.Clear();
  460. }
  461. }
  462. }