Horizon.cs 4.8 KB

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