NvDeviceFile.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Kernel.Memory;
  3. using Ryujinx.HLE.HOS.Kernel.Process;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices
  10. {
  11. abstract class NvDeviceFile
  12. {
  13. public readonly ServiceCtx Context;
  14. public readonly KProcess Owner;
  15. public string Path;
  16. public NvDeviceFile(ServiceCtx context)
  17. {
  18. Context = context;
  19. Owner = context.Process;
  20. }
  21. public virtual NvInternalResult QueryEvent(out int eventHandle, uint eventId)
  22. {
  23. eventHandle = 0;
  24. return NvInternalResult.NotImplemented;
  25. }
  26. public virtual NvInternalResult MapSharedMemory(KSharedMemory sharedMemory, uint argument)
  27. {
  28. return NvInternalResult.NotImplemented;
  29. }
  30. public virtual NvInternalResult Ioctl(NvIoctl command, Span<byte> arguments)
  31. {
  32. return NvInternalResult.NotImplemented;
  33. }
  34. public virtual NvInternalResult Ioctl2(NvIoctl command, Span<byte> arguments, Span<byte> inlineInBuffer)
  35. {
  36. return NvInternalResult.NotImplemented;
  37. }
  38. public virtual NvInternalResult Ioctl3(NvIoctl command, Span<byte> arguments, Span<byte> inlineOutBuffer)
  39. {
  40. return NvInternalResult.NotImplemented;
  41. }
  42. protected delegate NvInternalResult IoctlProcessor<T>(ref T arguments);
  43. protected delegate NvInternalResult IoctlProcessorSpan<T>(Span<T> arguments);
  44. protected delegate NvInternalResult IoctlProcessorInline<T, T1>(ref T arguments, ref T1 inlineData);
  45. protected delegate NvInternalResult IoctlProcessorInlineSpan<T, T1>(ref T arguments, Span<T1> inlineData);
  46. private static NvInternalResult PrintResult(MethodInfo info, NvInternalResult result)
  47. {
  48. Logger.Debug?.Print(LogClass.ServiceNv, $"{info.Name} returned result {result}");
  49. return result;
  50. }
  51. protected static NvInternalResult CallIoctlMethod<T>(IoctlProcessor<T> callback, Span<byte> arguments) where T : struct
  52. {
  53. Debug.Assert(arguments.Length == Unsafe.SizeOf<T>());
  54. return PrintResult(callback.Method, callback(ref MemoryMarshal.Cast<byte, T>(arguments)[0]));
  55. }
  56. protected static NvInternalResult CallIoctlMethod<T, T1>(IoctlProcessorInline<T, T1> callback, Span<byte> arguments, Span<byte> inlineBuffer) where T : struct where T1 : struct
  57. {
  58. Debug.Assert(arguments.Length == Unsafe.SizeOf<T>());
  59. Debug.Assert(inlineBuffer.Length == Unsafe.SizeOf<T1>());
  60. return PrintResult(callback.Method, callback(ref MemoryMarshal.Cast<byte, T>(arguments)[0], ref MemoryMarshal.Cast<byte, T1>(inlineBuffer)[0]));
  61. }
  62. protected static NvInternalResult CallIoctlMethod<T>(IoctlProcessorSpan<T> callback, Span<byte> arguments) where T : struct
  63. {
  64. return PrintResult(callback.Method, callback(MemoryMarshal.Cast<byte, T>(arguments)));
  65. }
  66. protected static NvInternalResult CallIoctlMethod<T, T1>(IoctlProcessorInlineSpan<T, T1> callback, Span<byte> arguments, Span<byte> inlineBuffer) where T : struct where T1 : struct
  67. {
  68. Debug.Assert(arguments.Length == Unsafe.SizeOf<T>());
  69. return PrintResult(callback.Method, callback(ref MemoryMarshal.Cast<byte, T>(arguments)[0], MemoryMarshal.Cast<byte, T1>(inlineBuffer)));
  70. }
  71. public abstract void Close();
  72. }
  73. }