2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64;
|
2018-02-26 01:14:58 +00:00
|
|
|
using ChocolArm64.Events;
|
2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-04-22 04:21:49 +00:00
|
|
|
using ChocolArm64.State;
|
2018-06-11 00:46:42 +00:00
|
|
|
using Ryujinx.HLE.Loaders;
|
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
|
using Ryujinx.HLE.Logging;
|
|
|
|
using Ryujinx.HLE.OsHle.Diagnostics;
|
|
|
|
using Ryujinx.HLE.OsHle.Exceptions;
|
|
|
|
using Ryujinx.HLE.OsHle.Handles;
|
|
|
|
using Ryujinx.HLE.OsHle.Kernel;
|
|
|
|
using Ryujinx.HLE.OsHle.Services.Nv;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
2018-04-22 04:21:49 +00:00
|
|
|
using System.Text;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-06-11 00:46:42 +00:00
|
|
|
namespace Ryujinx.HLE.OsHle
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
class Process : IDisposable
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-04-06 13:53:18 +00:00
|
|
|
private const int TlsSize = 0x200;
|
|
|
|
|
|
|
|
private const int TotalTlsSlots = (int)MemoryRegions.TlsPagesSize / TlsSize;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-14 00:24:17 +00:00
|
|
|
private const int TickFreq = 19_200_000;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
private Switch Ns;
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
public bool NeedsHbAbi { get; private set; }
|
|
|
|
|
|
|
|
public long HbAbiDataPosition { get; private set; }
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
public int ProcessId { get; private set; }
|
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
private ATranslator Translator;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
public AMemory Memory { get; private set; }
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
public KProcessScheduler Scheduler { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
public List<KThread> ThreadArbiterList { get; private set; }
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
public object ThreadSyncLock { get; private set; }
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
public KProcessHandleTable HandleTable { get; private set; }
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
public AppletStateMgr AppletState { get; private set; }
|
|
|
|
|
2018-02-14 05:43:21 +00:00
|
|
|
private SvcHandler SvcHandler;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
private ConcurrentDictionary<int, AThread> TlsSlots;
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
private ConcurrentDictionary<long, KThread> Threads;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-22 04:21:49 +00:00
|
|
|
private KThread MainThread;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
private List<Executable> Executables;
|
|
|
|
|
|
|
|
private long ImageBase;
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
private bool ShouldDispose;
|
|
|
|
|
|
|
|
private bool Disposed;
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
public Process(Switch Ns, KProcessScheduler Scheduler, int ProcessId)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
this.Ns = Ns;
|
2018-04-19 02:52:23 +00:00
|
|
|
this.Scheduler = Scheduler;
|
2018-02-04 23:08:20 +00:00
|
|
|
this.ProcessId = ProcessId;
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
Memory = new AMemory();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
ThreadArbiterList = new List<KThread>();
|
|
|
|
|
|
|
|
ThreadSyncLock = new object();
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
HandleTable = new KProcessHandleTable();
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
AppletState = new AppletStateMgr();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
SvcHandler = new SvcHandler(Ns, this);
|
|
|
|
|
|
|
|
TlsSlots = new ConcurrentDictionary<int, AThread>();
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
Threads = new ConcurrentDictionary<long, KThread>();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
Executables = new List<Executable>();
|
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
ImageBase = MemoryRegions.AddrSpaceStart;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
MapRWMemRegion(
|
|
|
|
MemoryRegions.TlsPagesAddress,
|
|
|
|
MemoryRegions.TlsPagesSize,
|
|
|
|
MemoryType.ThreadLocal);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
public void LoadProgram(IExecutable Program)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"Image base at 0x{ImageBase:x16}.");
|
2018-02-17 21:06:11 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
Executable Executable = new Executable(Program, Memory, ImageBase);
|
|
|
|
|
|
|
|
Executables.Add(Executable);
|
|
|
|
|
|
|
|
ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetEmptyArgs()
|
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
//TODO: This should be part of Run.
|
2018-02-04 23:08:20 +00:00
|
|
|
ImageBase += AMemoryMgr.PageSize;
|
|
|
|
}
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
public bool Run(bool NeedsHbAbi = false)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.NeedsHbAbi = NeedsHbAbi;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
if (Executables.Count == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
MapRWMemRegion(
|
|
|
|
MemoryRegions.MainStackAddress,
|
|
|
|
MemoryRegions.MainStackSize,
|
|
|
|
MemoryType.Normal);
|
2018-04-04 22:29:34 +00:00
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
long StackTop = MemoryRegions.MainStackAddress + MemoryRegions.MainStackSize;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
int Handle = MakeThread(Executables[0].ImageBase, StackTop, 0, 44, 0);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
if (Handle == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
MainThread = HandleTable.GetData<KThread>(Handle);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
if (NeedsHbAbi)
|
2018-02-24 00:59:38 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd);
|
2018-02-24 00:59:38 +00:00
|
|
|
|
2018-02-26 01:44:30 +00:00
|
|
|
Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle);
|
|
|
|
|
|
|
|
MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
|
2018-02-24 00:59:38 +00:00
|
|
|
MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
Scheduler.StartThread(MainThread);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-27 23:45:07 +00:00
|
|
|
private void MapRWMemRegion(long Position, long Size, MemoryType Type)
|
|
|
|
{
|
|
|
|
Memory.Manager.Map(Position, Size, (int)Type, AMemoryPerm.RW);
|
|
|
|
}
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
public void StopAllThreadsAsync()
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
if (MainThread != null)
|
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
MainThread.Thread.StopExecution();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (AThread Thread in TlsSlots.Values)
|
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
Thread.StopExecution();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int MakeThread(
|
|
|
|
long EntryPoint,
|
|
|
|
long StackTop,
|
|
|
|
long ArgsPtr,
|
|
|
|
int Priority,
|
|
|
|
int ProcessorId)
|
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
if (Disposed)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 22:20:19 +00:00
|
|
|
AThread CpuThread = new AThread(GetTranslator(), Memory, EntryPoint);
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
KThread Thread = new KThread(CpuThread, this, ProcessorId, Priority);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-06-26 04:09:32 +00:00
|
|
|
Thread.LastPc = EntryPoint;
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
int Handle = HandleTable.OpenHandle(Thread);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
int ThreadId = GetFreeTlsSlot(CpuThread);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
long Tpidr = MemoryRegions.TlsPagesAddress + ThreadId * TlsSize;
|
2018-02-27 23:45:07 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
CpuThread.ThreadState.ProcessId = ProcessId;
|
|
|
|
CpuThread.ThreadState.ThreadId = ThreadId;
|
|
|
|
CpuThread.ThreadState.CntfrqEl0 = TickFreq;
|
|
|
|
CpuThread.ThreadState.Tpidr = Tpidr;
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
CpuThread.ThreadState.X0 = (ulong)ArgsPtr;
|
|
|
|
CpuThread.ThreadState.X1 = (ulong)Handle;
|
|
|
|
CpuThread.ThreadState.X31 = (ulong)StackTop;
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
CpuThread.ThreadState.Break += BreakHandler;
|
|
|
|
CpuThread.ThreadState.SvcCall += SvcHandler.SvcCall;
|
|
|
|
CpuThread.ThreadState.Undefined += UndefinedHandler;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
CpuThread.WorkFinished += ThreadFinished;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
Threads.TryAdd(CpuThread.ThreadState.Tpidr, Thread);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
return Handle;
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:14:58 +00:00
|
|
|
private void BreakHandler(object sender, AInstExceptionEventArgs e)
|
2018-02-10 13:24:16 +00:00
|
|
|
{
|
|
|
|
throw new GuestBrokeExecutionException();
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:14:58 +00:00
|
|
|
private void UndefinedHandler(object sender, AInstUndefinedEventArgs e)
|
2018-02-10 17:20:46 +00:00
|
|
|
{
|
|
|
|
throw new UndefinedInstructionException(e.Position, e.RawOpCode);
|
|
|
|
}
|
|
|
|
|
2018-07-03 22:20:19 +00:00
|
|
|
public void EnableCpuTracing()
|
|
|
|
{
|
|
|
|
Translator.EnableCpuTrace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DisableCpuTracing()
|
|
|
|
{
|
|
|
|
Translator.EnableCpuTrace = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CpuTraceHandler(object sender, ACpuTraceEventArgs e)
|
|
|
|
{
|
|
|
|
Executable Exe = GetExecutable(e.Position);
|
|
|
|
|
|
|
|
if (Exe == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryGetSubName(Exe, e.Position, out string SubName))
|
|
|
|
{
|
|
|
|
SubName = string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Offset = e.Position - Exe.ImageBase;
|
|
|
|
|
|
|
|
string ExeNameWithAddr = $"{Exe.Name}:0x{Offset:x8}";
|
|
|
|
|
|
|
|
Ns.Log.PrintDebug(LogClass.Cpu, ExeNameWithAddr + " " + SubName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ATranslator GetTranslator()
|
|
|
|
{
|
|
|
|
if (Translator == null)
|
|
|
|
{
|
|
|
|
Translator = new ATranslator();
|
|
|
|
|
|
|
|
Translator.CpuTrace += CpuTraceHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Translator;
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
public void PrintStackTrace(AThreadState ThreadState)
|
2018-02-26 01:14:58 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
StringBuilder Trace = new StringBuilder();
|
|
|
|
|
|
|
|
Trace.AppendLine("Guest stack trace:");
|
2018-02-26 01:14:58 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
void AppendTrace(long Position)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-07-03 21:25:48 +00:00
|
|
|
Executable Exe = GetExecutable(Position);
|
2018-07-03 21:18:03 +00:00
|
|
|
|
|
|
|
if (Exe == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryGetSubName(Exe, Position, out string SubName))
|
|
|
|
{
|
|
|
|
SubName = $"Sub{Position:x16}";
|
|
|
|
}
|
|
|
|
else if (SubName.StartsWith("_Z"))
|
2018-02-26 01:14:58 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
SubName = Demangler.Parse(SubName);
|
2018-02-26 01:14:58 +00:00
|
|
|
}
|
2018-07-03 21:18:03 +00:00
|
|
|
|
|
|
|
long Offset = Position - Exe.ImageBase;
|
|
|
|
|
2018-07-03 22:20:19 +00:00
|
|
|
string ExeNameWithAddr = $"{Exe.Name}:0x{Offset:x8}";
|
2018-07-03 21:18:03 +00:00
|
|
|
|
2018-07-03 22:20:19 +00:00
|
|
|
Trace.AppendLine(" " + ExeNameWithAddr + " " + SubName);
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
2018-02-26 01:14:58 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
long FramePointer = (long)ThreadState.X29;
|
|
|
|
|
|
|
|
while (FramePointer != 0)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
AppendTrace(Memory.ReadInt64(FramePointer + 8));
|
2018-02-26 01:14:58 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
FramePointer = Memory.ReadInt64(FramePointer);
|
2018-02-26 01:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
Ns.Log.PrintInfo(LogClass.Cpu, Trace.ToString());
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
private bool TryGetSubName(Executable Exe, long Position, out string Name)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
Position -= Exe.ImageBase;
|
2018-04-22 04:21:49 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
int Left = 0;
|
|
|
|
int Right = Exe.SymbolTable.Count - 1;
|
2018-03-04 03:06:44 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
while (Left <= Right)
|
2018-03-04 03:06:44 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
int Size = Right - Left;
|
2018-04-04 22:29:34 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
int Middle = Left + (Size >> 1);
|
2018-03-04 03:06:44 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
ElfSym Symbol = Exe.SymbolTable[Middle];
|
2018-02-26 01:14:58 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
long EndPosition = Symbol.Value + Symbol.Size;
|
2018-04-22 04:21:49 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
if ((ulong)Position >= (ulong)Symbol.Value && (ulong)Position < (ulong)EndPosition)
|
|
|
|
{
|
|
|
|
Name = Symbol.Name;
|
2018-04-22 04:21:49 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-22 04:21:49 +00:00
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
if ((ulong)Position < (ulong)Symbol.Value)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
Right = Middle - 1;
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
2018-07-03 21:18:03 +00:00
|
|
|
else
|
2018-05-22 20:40:02 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
Left = Middle + 1;
|
2018-05-22 20:40:02 +00:00
|
|
|
}
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
Name = null;
|
|
|
|
|
|
|
|
return false;
|
2018-03-03 01:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:25:48 +00:00
|
|
|
private Executable GetExecutable(long Position)
|
2018-03-03 01:49:17 +00:00
|
|
|
{
|
2018-04-22 04:21:49 +00:00
|
|
|
string Name = string.Empty;
|
|
|
|
|
|
|
|
for (int Index = Executables.Count - 1; Index >= 0; Index--)
|
|
|
|
{
|
2018-07-03 21:38:40 +00:00
|
|
|
if ((ulong)Position >= (ulong)Executables[Index].ImageBase)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-07-03 21:18:03 +00:00
|
|
|
return Executables[Index];
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:18:03 +00:00
|
|
|
return null;
|
2018-03-03 01:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
private int GetFreeTlsSlot(AThread Thread)
|
|
|
|
{
|
|
|
|
for (int Index = 1; Index < TotalTlsSlots; Index++)
|
|
|
|
{
|
|
|
|
if (TlsSlots.TryAdd(Index, Thread))
|
|
|
|
{
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
throw new InvalidOperationException();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ThreadFinished(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is AThread Thread)
|
|
|
|
{
|
2018-02-18 19:28:07 +00:00
|
|
|
TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-04-26 03:07:12 +00:00
|
|
|
Threads.TryRemove(Thread.ThreadState.Tpidr, out KThread KernelThread);
|
2018-04-19 02:52:23 +00:00
|
|
|
|
|
|
|
Scheduler.RemoveThread(KernelThread);
|
|
|
|
|
|
|
|
KernelThread.WaitEvent.Set();
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
if (TlsSlots.Count == 0)
|
|
|
|
{
|
|
|
|
if (ShouldDispose)
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ns.Os.ExitProcess(ProcessId);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetTlsSlot(long Position)
|
|
|
|
{
|
2018-02-27 23:45:07 +00:00
|
|
|
return (int)((Position - MemoryRegions.TlsPagesAddress) / TlsSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
public KThread GetThread(long Tpidr)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-21 19:07:16 +00:00
|
|
|
if (!Threads.TryGetValue(Tpidr, out KThread Thread))
|
2018-02-19 19:37:13 +00:00
|
|
|
{
|
2018-04-24 18:57:39 +00:00
|
|
|
throw new InvalidOperationException();
|
2018-02-19 19:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Thread;
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
if (Disposing && !Disposed)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-03-12 04:04:52 +00:00
|
|
|
//If there is still some thread running, disposing the objects is not
|
|
|
|
//safe as the thread may try to access those resources. Instead, we set
|
|
|
|
//the flag to have the Process disposed when all threads finishes.
|
|
|
|
//Note: This may not happen if the guest code gets stuck on a infinite loop.
|
|
|
|
if (TlsSlots.Count > 0)
|
|
|
|
{
|
|
|
|
ShouldDispose = true;
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"Process {ProcessId} waiting all threads terminate...");
|
2018-03-12 04:04:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disposed = true;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
foreach (object Obj in HandleTable.Clear())
|
|
|
|
{
|
|
|
|
if (Obj is KSession Session)
|
|
|
|
{
|
|
|
|
Session.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 18:53:23 +00:00
|
|
|
INvDrvServices.UnloadProcess(this);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
AppletState.Dispose();
|
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
SvcHandler.Dispose();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-03-12 04:04:52 +00:00
|
|
|
Memory.Dispose();
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
Ns.Log.PrintInfo(LogClass.Loader, $"Process {ProcessId} exiting...");
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|