Horizon.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.Loaders.Executables;
  3. using Ryujinx.Core.OsHle.Handles;
  4. using Ryujinx.Core.OsHle.Utilities;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.IO;
  8. namespace Ryujinx.Core.OsHle
  9. {
  10. public class Horizon
  11. {
  12. internal const int HidSize = 0x40000;
  13. internal const int FontSize = 0x50;
  14. internal int HidHandle { get; private set; }
  15. internal int FontHandle { get; private set; }
  16. public long HidOffset { get; private set; }
  17. public long FontOffset { get; private set; }
  18. internal IdPool IdGen { get; private set; }
  19. internal IdPool NvMapIds { get; private set; }
  20. internal IdPoolWithObj Handles { get; private set; }
  21. internal IdPoolWithObj Fds { get; private set; }
  22. internal IdPoolWithObj Displays { get; private set; }
  23. public ConcurrentDictionary<long, Mutex> Mutexes { get; private set; }
  24. public ConcurrentDictionary<long, CondVar> CondVars { get; private set; }
  25. private ConcurrentDictionary<int, Process> Processes;
  26. private HSharedMem HidSharedMem;
  27. private AMemoryAlloc Allocator;
  28. private Switch Ns;
  29. public Horizon(Switch Ns)
  30. {
  31. this.Ns = Ns;
  32. IdGen = new IdPool();
  33. NvMapIds = new IdPool();
  34. Handles = new IdPoolWithObj();
  35. Fds = new IdPoolWithObj();
  36. Displays = new IdPoolWithObj();
  37. Mutexes = new ConcurrentDictionary<long, Mutex>();
  38. CondVars = new ConcurrentDictionary<long, CondVar>();
  39. Processes = new ConcurrentDictionary<int, Process>();
  40. Allocator = new AMemoryAlloc();
  41. HidOffset = Allocator.Alloc(HidSize);
  42. FontOffset = Allocator.Alloc(FontSize);
  43. HidSharedMem = new HSharedMem(HidOffset);
  44. HidSharedMem.MemoryMapped += HidInit;
  45. HidHandle = Handles.GenerateId(HidSharedMem);
  46. FontHandle = Handles.GenerateId(new HSharedMem(FontOffset));
  47. }
  48. public void LoadCart(string ExeFsDir, string RomFsFile = null)
  49. {
  50. if (RomFsFile != null)
  51. {
  52. Ns.VFs.LoadRomFs(RomFsFile);
  53. }
  54. int ProcessId = IdGen.GenerateId();
  55. Process MainProcess = new Process(Ns, Allocator, ProcessId);
  56. void LoadNso(string FileName)
  57. {
  58. foreach (string File in Directory.GetFiles(ExeFsDir, FileName))
  59. {
  60. if (Path.GetExtension(File) != string.Empty)
  61. {
  62. continue;
  63. }
  64. Logging.Info($"Loading {Path.GetFileNameWithoutExtension(File)}...");
  65. using (FileStream Input = new FileStream(File, FileMode.Open))
  66. {
  67. Nso Program = new Nso(Input);
  68. MainProcess.LoadProgram(Program);
  69. }
  70. }
  71. }
  72. LoadNso("rtld");
  73. MainProcess.SetEmptyArgs();
  74. LoadNso("main");
  75. LoadNso("subsdk*");
  76. LoadNso("sdk");
  77. MainProcess.InitializeHeap();
  78. MainProcess.Run();
  79. Processes.TryAdd(ProcessId, MainProcess);
  80. }
  81. public void LoadProgram(string FileName)
  82. {
  83. int ProcessId = IdGen.GenerateId();
  84. Process MainProcess = new Process(Ns, Allocator, ProcessId);
  85. using (FileStream Input = new FileStream(FileName, FileMode.Open))
  86. {
  87. if (Path.GetExtension(FileName).ToLower() == ".nro")
  88. {
  89. MainProcess.LoadProgram(new Nro(Input));
  90. }
  91. else
  92. {
  93. MainProcess.LoadProgram(new Nso(Input));
  94. }
  95. }
  96. MainProcess.SetEmptyArgs();
  97. MainProcess.InitializeHeap();
  98. MainProcess.Run();
  99. Processes.TryAdd(ProcessId, MainProcess);
  100. }
  101. public void FinalizeAllProcesses()
  102. {
  103. foreach (Process Process in Processes.Values)
  104. {
  105. Process.StopAllThreads();
  106. Process.Dispose();
  107. }
  108. }
  109. internal bool ExitProcess(int ProcessId)
  110. {
  111. bool Success = Processes.TryRemove(ProcessId, out Process Process);
  112. if (Success)
  113. {
  114. Process.StopAllThreads();
  115. }
  116. if (Processes.Count == 0)
  117. {
  118. Ns.OnFinish(EventArgs.Empty);
  119. }
  120. return Success;
  121. }
  122. internal bool TryGetProcess(int ProcessId, out Process Process)
  123. {
  124. return Processes.TryGetValue(ProcessId, out Process);
  125. }
  126. internal void CloseHandle(int Handle)
  127. {
  128. object HndData = Handles.GetData<object>(Handle);
  129. if (HndData is HTransferMem TransferMem)
  130. {
  131. TransferMem.Memory.Manager.Reprotect(
  132. TransferMem.Position,
  133. TransferMem.Size,
  134. TransferMem.Perm);
  135. }
  136. Handles.Delete(Handle);
  137. }
  138. private void HidInit(object sender, EventArgs e)
  139. {
  140. HSharedMem SharedMem = (HSharedMem)sender;
  141. if (SharedMem.TryGetLastVirtualPosition(out long Position))
  142. {
  143. Logging.Info($"HID shared memory successfully mapped to {Position:x16}!");
  144. Ns.Hid.Init(Position);
  145. }
  146. }
  147. }
  148. }