NvDeviceFile.cs 3.7 KB

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