2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64;
|
2018-02-25 20:14:58 -05:00
|
|
|
using ChocolArm64.Events;
|
2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-20 15:09:23 -05:00
|
|
|
using Ryujinx.Core.Loaders;
|
|
|
|
using Ryujinx.Core.Loaders.Executables;
|
|
|
|
using Ryujinx.Core.OsHle.Exceptions;
|
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
|
|
|
using Ryujinx.Core.OsHle.Svc;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-02-20 15:09:23 -05:00
|
|
|
namespace Ryujinx.Core.OsHle
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
class Process : IDisposable
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
private const int TlsSize = 0x200;
|
|
|
|
private const int TotalTlsSlots = 32;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-13 20:24:17 -04:00
|
|
|
private const int TickFreq = 19_200_000;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private Switch Ns;
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public bool NeedsHbAbi { get; private set; }
|
|
|
|
|
|
|
|
public long HbAbiDataPosition { get; private set; }
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
public int ProcessId { get; private set; }
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
private ATranslator Translator;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
public AMemory Memory { get; private set; }
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public ServiceMgr Services { get; private set; }
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
public KProcessScheduler Scheduler { get; private set; }
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public KProcessHandleTable HandleTable { get; private set; }
|
|
|
|
|
2018-02-14 00:43:21 -05:00
|
|
|
private SvcHandler SvcHandler;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
private ConcurrentDictionary<int, AThread> TlsSlots;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
private ConcurrentDictionary<long, HThread> ThreadsByTpidr;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private List<Executable> Executables;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
private HThread MainThread;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private long ImageBase;
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
private bool ShouldDispose;
|
|
|
|
|
|
|
|
private bool Disposed;
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
public Process(Switch Ns, int ProcessId)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
this.Ns = Ns;
|
|
|
|
this.ProcessId = ProcessId;
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
Memory = new AMemory();
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
Services = new ServiceMgr();
|
|
|
|
|
|
|
|
HandleTable = new KProcessHandleTable();
|
|
|
|
|
|
|
|
Scheduler = new KProcessScheduler();
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
SvcHandler = new SvcHandler(Ns, this);
|
|
|
|
|
|
|
|
TlsSlots = new ConcurrentDictionary<int, AThread>();
|
|
|
|
|
|
|
|
ThreadsByTpidr = new ConcurrentDictionary<long, HThread>();
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
Executables = new List<Executable>();
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
ImageBase = MemoryRegions.AddrSpaceStart;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
MapRWMemRegion(
|
|
|
|
MemoryRegions.TlsPagesAddress,
|
|
|
|
MemoryRegions.TlsPagesSize,
|
|
|
|
MemoryType.ThreadLocal);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:28:32 -05:00
|
|
|
public void LoadProgram(IExecutable Program)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:06:11 -05:00
|
|
|
Logging.Info($"Image base at 0x{ImageBase:x16}.");
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
Executable Executable = new Executable(Program, Memory, ImageBase);
|
|
|
|
|
|
|
|
Executables.Add(Executable);
|
|
|
|
|
|
|
|
ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetEmptyArgs()
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
//TODO: This should be part of Run.
|
2018-02-04 18:08:20 -05:00
|
|
|
ImageBase += AMemoryMgr.PageSize;
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public bool Run(bool NeedsHbAbi = false)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.NeedsHbAbi = NeedsHbAbi;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
if (Executables.Count == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
MapRWMemRegion(
|
|
|
|
MemoryRegions.MainStackAddress,
|
|
|
|
MemoryRegions.MainStackSize,
|
|
|
|
MemoryType.Normal);
|
|
|
|
|
|
|
|
long StackTop = MemoryRegions.MainStackAddress + MemoryRegions.MainStackSize;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
int Handle = MakeThread(Executables[0].ImageBase, StackTop, 0, 0, 0);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (Handle == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
MainThread = HandleTable.GetData<HThread>(Handle);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (NeedsHbAbi)
|
2018-02-23 19:59:38 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd);
|
2018-02-23 19:59:38 -05:00
|
|
|
|
2018-02-25 20:44:30 -05:00
|
|
|
Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle);
|
|
|
|
|
|
|
|
MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
|
2018-02-23 19:59:38 -05:00
|
|
|
MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
Scheduler.StartThread(MainThread);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:45:07 -05:00
|
|
|
private void MapRWMemRegion(long Position, long Size, MemoryType Type)
|
|
|
|
{
|
|
|
|
Memory.Manager.Map(Position, Size, (int)Type, AMemoryPerm.RW);
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public void StopAllThreadsAsync()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
|
|
|
}
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
if (MainThread != null)
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
MainThread.Thread.StopExecution();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (AThread Thread in TlsSlots.Values)
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Thread.StopExecution();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int MakeThread(
|
|
|
|
long EntryPoint,
|
|
|
|
long StackTop,
|
|
|
|
long ArgsPtr,
|
|
|
|
int Priority,
|
|
|
|
int ProcessorId)
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Disposed)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
throw new ObjectDisposedException(nameof(Process));
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
AThread Thread = new AThread(GetTranslator(), Memory, EntryPoint);
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
int Handle = HandleTable.OpenHandle(ThreadHnd);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
int ThreadId = GetFreeTlsSlot(Thread);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
long Tpidr = MemoryRegions.TlsPagesAddress + ThreadId * TlsSize;
|
2018-02-27 18:45:07 -05:00
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
Thread.ThreadState.Break += BreakHandler;
|
|
|
|
Thread.ThreadState.SvcCall += SvcHandler.SvcCall;
|
|
|
|
Thread.ThreadState.Undefined += UndefinedHandler;
|
|
|
|
Thread.ThreadState.ProcessId = ProcessId;
|
2018-03-12 00:04:52 -04:00
|
|
|
Thread.ThreadState.ThreadId = ThreadId;
|
2018-03-13 20:24:17 -04:00
|
|
|
Thread.ThreadState.CntfrqEl0 = TickFreq;
|
2018-02-27 18:45:07 -05:00
|
|
|
Thread.ThreadState.Tpidr = Tpidr;
|
2018-02-18 14:28:07 -05:00
|
|
|
Thread.ThreadState.X0 = (ulong)ArgsPtr;
|
|
|
|
Thread.ThreadState.X1 = (ulong)Handle;
|
|
|
|
Thread.ThreadState.X31 = (ulong)StackTop;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Thread.WorkFinished += ThreadFinished;
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
ThreadsByTpidr.TryAdd(Thread.ThreadState.Tpidr, ThreadHnd);
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
return Handle;
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:14:58 -05:00
|
|
|
private void BreakHandler(object sender, AInstExceptionEventArgs e)
|
2018-02-10 08:24:16 -05:00
|
|
|
{
|
|
|
|
throw new GuestBrokeExecutionException();
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:14:58 -05:00
|
|
|
private void UndefinedHandler(object sender, AInstUndefinedEventArgs e)
|
2018-02-10 12:20:46 -05:00
|
|
|
{
|
|
|
|
throw new UndefinedInstructionException(e.Position, e.RawOpCode);
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:14:58 -05:00
|
|
|
private ATranslator GetTranslator()
|
|
|
|
{
|
|
|
|
if (Translator == null)
|
|
|
|
{
|
|
|
|
Dictionary<long, string> SymbolTable = new Dictionary<long, string>();
|
|
|
|
|
|
|
|
foreach (Executable Exe in Executables)
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<long, string> KV in Exe.SymbolTable)
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-02-25 20:44:30 -05:00
|
|
|
SymbolTable.TryAdd(Exe.ImageBase + KV.Key, KV.Value);
|
2018-02-25 20:14:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Translator = new ATranslator(SymbolTable);
|
|
|
|
|
|
|
|
Translator.CpuTrace += CpuTraceHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CpuTraceHandler(object sender, ACpuTraceEventArgs e)
|
|
|
|
{
|
2018-03-03 22:06:44 -05:00
|
|
|
string NsoName = string.Empty;
|
|
|
|
|
|
|
|
for (int Index = Executables.Count - 1; Index >= 0; Index--)
|
|
|
|
{
|
|
|
|
if (e.Position >= Executables[Index].ImageBase)
|
|
|
|
{
|
|
|
|
NsoName = $"{(e.Position - Executables[Index].ImageBase):x16}";
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logging.Trace($"Executing at 0x{e.Position:x16} {e.SubName} {NsoName}");
|
2018-02-25 20:14:58 -05:00
|
|
|
}
|
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
public void EnableCpuTracing()
|
|
|
|
{
|
|
|
|
Translator.EnableCpuTrace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DisableCpuTracing()
|
|
|
|
{
|
|
|
|
Translator.EnableCpuTrace = false;
|
|
|
|
}
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private int GetFreeTlsSlot(AThread Thread)
|
|
|
|
{
|
|
|
|
for (int Index = 1; Index < TotalTlsSlots; Index++)
|
|
|
|
{
|
|
|
|
if (TlsSlots.TryAdd(Index, Thread))
|
|
|
|
{
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
throw new InvalidOperationException();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ThreadFinished(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is AThread Thread)
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Logging.Info($"Thread {Thread.ThreadId} exiting...");
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (TlsSlots.Count == 0)
|
|
|
|
{
|
|
|
|
if (ShouldDispose)
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Logging.Info($"No threads running, now exiting Process {ProcessId}...");
|
|
|
|
|
|
|
|
Ns.Os.ExitProcess(ProcessId);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetTlsSlot(long Position)
|
|
|
|
{
|
2018-02-27 18:45:07 -05:00
|
|
|
return (int)((Position - MemoryRegions.TlsPagesAddress) / TlsSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
public HThread GetThread(long Tpidr)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
if (!ThreadsByTpidr.TryGetValue(Tpidr, out HThread Thread))
|
|
|
|
{
|
|
|
|
Logging.Error($"Thread with TPIDR 0x{Tpidr:x16} not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Thread;
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Disposing && !Disposed)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04: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;
|
|
|
|
|
|
|
|
Logging.Info($"Process {ProcessId} waiting all threads terminate...");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disposed = true;
|
|
|
|
|
|
|
|
Services.Dispose();
|
|
|
|
HandleTable.Dispose();
|
2018-02-13 21:43:08 -05:00
|
|
|
Scheduler.Dispose();
|
2018-03-12 00:04:52 -04:00
|
|
|
SvcHandler.Dispose();
|
|
|
|
Memory.Dispose();
|
|
|
|
|
|
|
|
Logging.Info($"Process {ProcessId} exiting...");
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|