NvDeviceFile.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices
  8. {
  9. abstract class NvDeviceFile
  10. {
  11. public readonly ServiceCtx Context;
  12. public readonly ulong Owner;
  13. public string Path;
  14. public NvDeviceFile(ServiceCtx context, ulong owner)
  15. {
  16. Context = context;
  17. Owner = owner;
  18. }
  19. public virtual NvInternalResult QueryEvent(out int eventHandle, uint eventId)
  20. {
  21. eventHandle = 0;
  22. return NvInternalResult.NotImplemented;
  23. }
  24. public virtual NvInternalResult MapSharedMemory(int sharedMemoryHandle, uint argument)
  25. {
  26. // Close shared memory immediately as we don't use it.
  27. Context.Device.System.KernelContext.Syscall.CloseHandle(sharedMemoryHandle);
  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. }