2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
2018-07-20 17:53:06 -04:00
|
|
|
using Ryujinx.HLE.Loaders.Npdm;
|
2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE.Logging;
|
2018-08-15 14:59:51 -04:00
|
|
|
using Ryujinx.HLE.OsHle.Font;
|
2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE.OsHle.Handles;
|
2018-08-14 18:02:42 -04:00
|
|
|
using Ryujinx.HLE.OsHle.SystemState;
|
2018-02-20 15:09:23 -05:00
|
|
|
using System;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-06-10 20:46:42 -04:00
|
|
|
namespace Ryujinx.HLE.OsHle
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
public class Horizon : IDisposable
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
internal const int HidSize = 0x40000;
|
2018-08-15 14:59:51 -04:00
|
|
|
internal const int FontSize = 0x1100000;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-04-24 16:14:26 -04:00
|
|
|
private Switch Ns;
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
private KProcessScheduler Scheduler;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
private ConcurrentDictionary<int, Process> Processes;
|
|
|
|
|
2018-04-29 19:18:46 -04:00
|
|
|
public SystemStateMgr SystemState { get; private set; }
|
2018-04-24 16:14:26 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
internal KSharedMemory HidSharedMem { get; private set; }
|
|
|
|
internal KSharedMemory FontSharedMem { get; private set; }
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 14:53:23 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
internal SharedFontManager Font { get; private set; }
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
internal KEvent VsyncEvent { get; private set; }
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
public Horizon(Switch Ns)
|
|
|
|
{
|
|
|
|
this.Ns = Ns;
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
Scheduler = new KProcessScheduler(Ns.Log);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Processes = new ConcurrentDictionary<int, Process>();
|
|
|
|
|
2018-04-24 16:14:26 -04:00
|
|
|
SystemState = new SystemStateMgr();
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!Ns.Memory.Allocator.TryAllocate(HidSize, out long HidPA) ||
|
|
|
|
!Ns.Memory.Allocator.TryAllocate(FontSize, out long FontPA))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
HidSharedMem = new KSharedMemory(HidPA, HidSize);
|
|
|
|
FontSharedMem = new KSharedMemory(FontPA, FontSize);
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 14:53:23 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
Font = new SharedFontManager(Ns, FontSharedMem.PA);
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
VsyncEvent = new KEvent();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadCart(string ExeFsDir, string RomFsFile = null)
|
|
|
|
{
|
|
|
|
if (RomFsFile != null)
|
|
|
|
{
|
|
|
|
Ns.VFs.LoadRomFs(RomFsFile);
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
string NpdmFileName = Path.Combine(ExeFsDir, "main.npdm");
|
|
|
|
|
|
|
|
Npdm MetaData = null;
|
|
|
|
|
|
|
|
if (File.Exists(NpdmFileName))
|
|
|
|
{
|
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"Loading main.npdm...");
|
|
|
|
|
|
|
|
using (FileStream Input = new FileStream(NpdmFileName, FileMode.Open))
|
|
|
|
{
|
|
|
|
MetaData = new Npdm(Input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Ns.Log.PrintWarning(LogClass.Loader, $"NPDM file not found, using default values!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Process MainProcess = MakeProcess(MetaData);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
void LoadNso(string FileName)
|
|
|
|
{
|
|
|
|
foreach (string File in Directory.GetFiles(ExeFsDir, FileName))
|
|
|
|
{
|
|
|
|
if (Path.GetExtension(File) != string.Empty)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"Loading {Path.GetFileNameWithoutExtension(File)}...");
|
2018-02-17 16:06:11 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
using (FileStream Input = new FileStream(File, FileMode.Open))
|
|
|
|
{
|
2018-04-22 00:21:49 -04:00
|
|
|
string Name = Path.GetFileNameWithoutExtension(File);
|
|
|
|
|
|
|
|
Nso Program = new Nso(Input, Name);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
MainProcess.LoadProgram(Program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
if (!MainProcess.MetaData.Is64Bits)
|
2018-07-20 17:53:06 -04:00
|
|
|
{
|
2018-08-02 20:58:48 -04:00
|
|
|
throw new NotImplementedException("32-bit titles are unsupported!");
|
2018-07-20 17:53:06 -04:00
|
|
|
}
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
LoadNso("rtld");
|
|
|
|
|
|
|
|
MainProcess.SetEmptyArgs();
|
|
|
|
|
|
|
|
LoadNso("main");
|
|
|
|
LoadNso("subsdk*");
|
|
|
|
LoadNso("sdk");
|
|
|
|
|
|
|
|
MainProcess.Run();
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:14:27 -04:00
|
|
|
public void LoadProgram(string FilePath)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-07-17 15:14:27 -04:00
|
|
|
bool IsNro = Path.GetExtension(FilePath).ToLower() == ".nro";
|
2018-02-23 19:59:38 -05:00
|
|
|
|
2018-07-17 15:14:27 -04:00
|
|
|
string Name = Path.GetFileNameWithoutExtension(FilePath);
|
|
|
|
string SwitchFilePath = Ns.VFs.SystemPathToSwitchPath(FilePath);
|
|
|
|
|
|
|
|
if (IsNro && (SwitchFilePath == null || !SwitchFilePath.StartsWith("sdmc:/")))
|
|
|
|
{
|
|
|
|
string SwitchPath = $"sdmc:/switch/{Name}{Homebrew.TemporaryNroSuffix}";
|
|
|
|
string TempPath = Ns.VFs.SwitchPathToSystemPath(SwitchPath);
|
|
|
|
|
|
|
|
string SwitchDir = Path.GetDirectoryName(TempPath);
|
|
|
|
if (!Directory.Exists(SwitchDir))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(SwitchDir);
|
|
|
|
}
|
|
|
|
File.Copy(FilePath, TempPath, true);
|
|
|
|
|
|
|
|
FilePath = TempPath;
|
|
|
|
}
|
2018-04-22 00:21:49 -04:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
Process MainProcess = MakeProcess();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-07-17 15:14:27 -04:00
|
|
|
using (FileStream Input = new FileStream(FilePath, FileMode.Open))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
MainProcess.LoadProgram(IsNro
|
2018-07-17 15:14:27 -04:00
|
|
|
? (IExecutable)new Nro(Input, FilePath)
|
|
|
|
: (IExecutable)new Nso(Input, FilePath));
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MainProcess.SetEmptyArgs();
|
2018-02-23 19:59:38 -05:00
|
|
|
MainProcess.Run(IsNro);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
public void SignalVsync() => VsyncEvent.WaitEvent.Set();
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
private Process MakeProcess(Npdm MetaData = null)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Process Process;
|
|
|
|
|
|
|
|
lock (Processes)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
int ProcessId = 0;
|
|
|
|
|
|
|
|
while (Processes.ContainsKey(ProcessId))
|
|
|
|
{
|
|
|
|
ProcessId++;
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
Process = new Process(Ns, Scheduler, ProcessId, MetaData);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
Processes.TryAdd(ProcessId, Process);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
InitializeProcess(Process);
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
return Process;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
private void InitializeProcess(Process Process)
|
|
|
|
{
|
|
|
|
Process.AppletState.SetFocus(true);
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
internal void ExitProcess(int ProcessId)
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.TryGetValue(ProcessId, out Process Process) && Process.NeedsHbAbi)
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
string NextNro = Homebrew.ReadHbAbiNextLoadPath(Process.Memory, Process.HbAbiDataPosition);
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"HbAbi NextLoadPath {NextNro}");
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
if (NextNro == string.Empty)
|
|
|
|
{
|
|
|
|
NextNro = "sdmc:/hbmenu.nro";
|
|
|
|
}
|
|
|
|
|
|
|
|
NextNro = NextNro.Replace("sdmc:", string.Empty);
|
|
|
|
|
|
|
|
NextNro = Ns.VFs.GetFullPath(Ns.VFs.GetSdCardPath(), NextNro);
|
|
|
|
|
|
|
|
if (File.Exists(NextNro))
|
|
|
|
{
|
|
|
|
LoadProgram(NextNro);
|
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.TryRemove(ProcessId, out Process))
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Process.StopAllThreadsAsync();
|
|
|
|
Process.Dispose();
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.Count == 0)
|
|
|
|
{
|
|
|
|
Ns.OnFinish(EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
}
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
internal bool TryGetProcess(int ProcessId, out Process Process)
|
|
|
|
{
|
2018-02-20 05:54:00 -05:00
|
|
|
return Processes.TryGetValue(ProcessId, out Process);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public void Dispose()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Dispose(true);
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
foreach (Process Process in Processes.Values)
|
|
|
|
{
|
|
|
|
Process.StopAllThreadsAsync();
|
|
|
|
Process.Dispose();
|
|
|
|
}
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
VsyncEvent.Dispose();
|
2018-04-18 23:19:22 -04:00
|
|
|
|
|
|
|
Scheduler.Dispose();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 17:31:09 -04:00
|
|
|
}
|