Ryujinx/Ryujinx.HLE/HOS/Kernel/SvcThread.cs

464 lines
18 KiB
C#
Raw Normal View History

2018-02-05 00:08:20 +01:00
using ChocolArm64.State;
using Ryujinx.Common.Logging;
2018-02-05 00:08:20 +01:00
using static Ryujinx.HLE.HOS.ErrorCode;
2018-04-04 21:07:44 +02:00
namespace Ryujinx.HLE.HOS.Kernel
2018-02-05 00:08:20 +01:00
{
partial class SvcHandler
{
2018-12-01 21:01:59 +01:00
private void CreateThread64(CpuThreadState threadState)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
ulong entrypoint = threadState.X1;
ulong argsPtr = threadState.X2;
ulong stackTop = threadState.X3;
int priority = (int)threadState.X4;
int cpuCore = (int)threadState.X5;
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
KernelResult result = CreateThread(entrypoint, argsPtr, stackTop, priority, cpuCore, out int handle);
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
threadState.X1 = (ulong)handle;
}
private KernelResult CreateThread(
2018-12-01 21:01:59 +01:00
ulong entrypoint,
ulong argsPtr,
ulong stackTop,
int priority,
int cpuCore,
out int handle)
{
2018-12-01 21:01:59 +01:00
handle = 0;
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
if (cpuCore == -2)
{
2018-12-01 21:01:59 +01:00
cpuCore = currentProcess.DefaultCpuCore;
}
2018-12-01 21:01:59 +01:00
if ((uint)cpuCore >= KScheduler.CpuCoresCount || !currentProcess.IsCpuCoreAllowed(cpuCore))
2018-02-05 00:08:20 +01:00
{
return KernelResult.InvalidCpuCore;
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
if ((uint)priority >= KScheduler.PrioritiesCount || !currentProcess.IsPriorityAllowed(priority))
{
return KernelResult.InvalidPriority;
}
2018-12-01 21:01:59 +01:00
long timeout = KTimeManager.ConvertMillisecondsToNanoseconds(100);
2018-12-01 21:01:59 +01:00
if (currentProcess.ResourceLimit != null &&
!currentProcess.ResourceLimit.Reserve(LimitableResource.Thread, 1, timeout))
{
return KernelResult.ResLimitExceeded;
}
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
KThread thread = new KThread(_system);
2018-12-01 21:01:59 +01:00
KernelResult result = currentProcess.InitializeThread(
thread,
entrypoint,
argsPtr,
stackTop,
priority,
cpuCore);
2018-12-01 21:01:59 +01:00
if (result != KernelResult.Success)
{
2018-12-01 21:01:59 +01:00
currentProcess.ResourceLimit?.Release(LimitableResource.Thread, 1);
2018-12-01 21:01:59 +01:00
return result;
}
2018-12-01 21:01:59 +01:00
result = _process.HandleTable.GenerateHandle(thread, out handle);
2018-12-01 21:01:59 +01:00
if (result != KernelResult.Success)
{
2018-12-01 21:01:59 +01:00
thread.Terminate();
2018-12-01 21:01:59 +01:00
currentProcess.ResourceLimit?.Release(LimitableResource.Thread, 1);
}
2018-12-01 21:01:59 +01:00
return result;
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
private void SvcStartThread(CpuThreadState threadState)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X0;
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetObject<KThread>(handle);
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
if (thread != null)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
KernelResult result = thread.Start();
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
if (result != KernelResult.Success)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
}
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
2018-02-05 00:08:20 +01:00
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
}
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
private void SvcExitThread(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
KThread currentThread = _system.Scheduler.GetCurrentThread();
2018-12-01 21:01:59 +01:00
_system.Scheduler.ExitThread(currentThread);
2018-12-01 21:01:59 +01:00
currentThread.Exit();
}
2018-12-01 21:01:59 +01:00
private void SvcSleepThread(CpuThreadState threadState)
2018-04-04 21:07:44 +02:00
{
2018-12-01 21:01:59 +01:00
long timeout = (long)threadState.X0;
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, "Timeout = 0x" + timeout.ToString("x16"));
2018-12-01 21:01:59 +01:00
KThread currentThread = _system.Scheduler.GetCurrentThread();
2018-04-04 21:07:44 +02:00
2018-12-01 21:01:59 +01:00
if (timeout < 1)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
switch (timeout)
{
2018-12-01 21:01:59 +01:00
case 0: currentThread.Yield(); break;
case -1: currentThread.YieldWithLoadBalancing(); break;
case -2: currentThread.YieldAndWaitForLoadBalancing(); break;
}
2018-02-05 00:08:20 +01:00
}
else
{
2018-12-01 21:01:59 +01:00
currentThread.Sleep(timeout);
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
2018-02-05 00:08:20 +01:00
}
}
2018-12-01 21:01:59 +01:00
private void SvcGetThreadPriority(CpuThreadState threadState)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X1;
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(handle);
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
if (thread != null)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
threadState.X1 = (ulong)thread.DynamicPriority;
2018-02-05 00:08:20 +01:00
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
}
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
private void SvcSetThreadPriority(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X0;
int priority = (int)threadState.X1;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"Handle = 0x" + handle .ToString("x8") + ", " +
"Priority = 0x" + priority.ToString("x8"));
//TODO: NPDM check.
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(handle);
2018-12-01 21:01:59 +01:00
if (thread == null)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
return;
}
2018-12-01 21:01:59 +01:00
thread.SetPriority(priority);
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
}
2018-12-01 21:01:59 +01:00
private void SvcGetThreadCoreMask(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X2;
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, "Handle = 0x" + handle.ToString("x8"));
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(handle);
2018-12-01 21:01:59 +01:00
if (thread != null)
{
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
threadState.X1 = (ulong)thread.PreferredCore;
threadState.X2 = (ulong)thread.AffinityMask;
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
}
}
2018-12-01 21:01:59 +01:00
private void SetThreadCoreMask64(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X0;
int preferredCore = (int)threadState.X1;
long affinityMask = (long)threadState.X2;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"Handle = 0x" + handle .ToString("x8") + ", " +
"PreferredCore = 0x" + preferredCore.ToString("x8") + ", " +
"AffinityMask = 0x" + affinityMask .ToString("x16"));
2018-12-01 21:01:59 +01:00
KernelResult result = SetThreadCoreMask(handle, preferredCore, affinityMask);
2018-12-01 21:01:59 +01:00
if (result != KernelResult.Success)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error \"{result}\".");
}
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private KernelResult SetThreadCoreMask(int handle, int preferredCore, long affinityMask)
{
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
if (preferredCore == -2)
{
2018-12-01 21:01:59 +01:00
preferredCore = currentProcess.DefaultCpuCore;
2018-12-01 21:01:59 +01:00
affinityMask = 1 << preferredCore;
}
else
{
2018-12-01 21:01:59 +01:00
if ((currentProcess.Capabilities.AllowedCpuCoresMask | affinityMask) !=
currentProcess.Capabilities.AllowedCpuCoresMask)
{
return KernelResult.InvalidCpuCore;
}
2018-12-01 21:01:59 +01:00
if (affinityMask == 0)
{
return KernelResult.InvalidCombination;
}
2018-12-01 21:01:59 +01:00
if ((uint)preferredCore > 3)
{
2018-12-01 21:01:59 +01:00
if ((preferredCore | 2) != -1)
{
return KernelResult.InvalidCpuCore;
}
}
2018-12-01 21:01:59 +01:00
else if ((affinityMask & (1 << preferredCore)) == 0)
{
return KernelResult.InvalidCombination;
}
}
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(handle);
2018-12-01 21:01:59 +01:00
if (thread == null)
{
return KernelResult.InvalidHandle;
}
2018-12-01 21:01:59 +01:00
return thread.SetCoreAndAffinityMask(preferredCore, affinityMask);
}
2018-12-01 21:01:59 +01:00
private void SvcGetCurrentProcessorNumber(CpuThreadState threadState)
2018-04-05 00:16:59 +02:00
{
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)_system.Scheduler.GetCurrentThread().CurrentCore;
2018-04-05 00:16:59 +02:00
}
2018-12-01 21:01:59 +01:00
private void SvcGetThreadId(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X1;
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(handle);
2018-12-01 21:01:59 +01:00
if (thread != null)
{
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
threadState.X1 = (ulong)thread.ThreadUid;
}
2018-04-04 21:07:44 +02:00
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
}
}
2018-12-01 21:01:59 +01:00
private void SvcSetThreadActivity(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int handle = (int)threadState.X0;
bool pause = (int)threadState.X1 == 1;
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetObject<KThread>(handle);
2018-12-01 21:01:59 +01:00
if (thread == null)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
return;
}
2018-12-01 21:01:59 +01:00
if (thread.Owner != _system.Scheduler.GetCurrentProcess())
{
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread, it belongs to another process.");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
return;
}
2018-12-01 21:01:59 +01:00
if (thread == _system.Scheduler.GetCurrentThread())
{
Logger.PrintWarning(LogClass.KernelSvc, "Invalid thread, current thread is not accepted.");
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)KernelResult.InvalidThread;
return;
}
2018-12-01 21:01:59 +01:00
long result = thread.SetActivity(pause);
2018-12-01 21:01:59 +01:00
if (result != 0)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
2018-04-04 21:07:44 +02:00
}
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-06-26 06:09:32 +02:00
2018-12-01 21:01:59 +01:00
private void SvcGetThreadContext3(CpuThreadState threadState)
2018-06-26 06:09:32 +02:00
{
2018-12-01 21:01:59 +01:00
long position = (long)threadState.X0;
int handle = (int)threadState.X1;
2018-06-26 06:09:32 +02:00
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KThread currentThread = _system.Scheduler.GetCurrentThread();
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetObject<KThread>(handle);
2018-06-26 06:09:32 +02:00
2018-12-01 21:01:59 +01:00
if (thread == null)
2018-06-26 06:09:32 +02:00
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{handle:x8}!");
2018-06-26 06:09:32 +02:00
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
2018-06-26 06:09:32 +02:00
return;
}
2018-12-01 21:01:59 +01:00
if (thread.Owner != currentProcess)
{
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid thread, it belongs to another process.");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
return;
}
2018-12-01 21:01:59 +01:00
if (currentThread == thread)
2018-06-26 06:09:32 +02:00
{
Logger.PrintWarning(LogClass.KernelSvc, "Invalid thread, current thread is not accepted.");
2018-06-26 06:09:32 +02:00
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidThread);
2018-06-26 06:09:32 +02:00
return;
}
2018-12-01 21:01:59 +01:00
_memory.WriteUInt64(position + 0x0, thread.Context.ThreadState.X0);
_memory.WriteUInt64(position + 0x8, thread.Context.ThreadState.X1);
_memory.WriteUInt64(position + 0x10, thread.Context.ThreadState.X2);
_memory.WriteUInt64(position + 0x18, thread.Context.ThreadState.X3);
_memory.WriteUInt64(position + 0x20, thread.Context.ThreadState.X4);
_memory.WriteUInt64(position + 0x28, thread.Context.ThreadState.X5);
_memory.WriteUInt64(position + 0x30, thread.Context.ThreadState.X6);
_memory.WriteUInt64(position + 0x38, thread.Context.ThreadState.X7);
_memory.WriteUInt64(position + 0x40, thread.Context.ThreadState.X8);
_memory.WriteUInt64(position + 0x48, thread.Context.ThreadState.X9);
_memory.WriteUInt64(position + 0x50, thread.Context.ThreadState.X10);
_memory.WriteUInt64(position + 0x58, thread.Context.ThreadState.X11);
_memory.WriteUInt64(position + 0x60, thread.Context.ThreadState.X12);
_memory.WriteUInt64(position + 0x68, thread.Context.ThreadState.X13);
_memory.WriteUInt64(position + 0x70, thread.Context.ThreadState.X14);
_memory.WriteUInt64(position + 0x78, thread.Context.ThreadState.X15);
_memory.WriteUInt64(position + 0x80, thread.Context.ThreadState.X16);
_memory.WriteUInt64(position + 0x88, thread.Context.ThreadState.X17);
_memory.WriteUInt64(position + 0x90, thread.Context.ThreadState.X18);
_memory.WriteUInt64(position + 0x98, thread.Context.ThreadState.X19);
_memory.WriteUInt64(position + 0xa0, thread.Context.ThreadState.X20);
_memory.WriteUInt64(position + 0xa8, thread.Context.ThreadState.X21);
_memory.WriteUInt64(position + 0xb0, thread.Context.ThreadState.X22);
_memory.WriteUInt64(position + 0xb8, thread.Context.ThreadState.X23);
_memory.WriteUInt64(position + 0xc0, thread.Context.ThreadState.X24);
_memory.WriteUInt64(position + 0xc8, thread.Context.ThreadState.X25);
_memory.WriteUInt64(position + 0xd0, thread.Context.ThreadState.X26);
_memory.WriteUInt64(position + 0xd8, thread.Context.ThreadState.X27);
_memory.WriteUInt64(position + 0xe0, thread.Context.ThreadState.X28);
_memory.WriteUInt64(position + 0xe8, thread.Context.ThreadState.X29);
_memory.WriteUInt64(position + 0xf0, thread.Context.ThreadState.X30);
_memory.WriteUInt64(position + 0xf8, thread.Context.ThreadState.X31);
_memory.WriteInt64(position + 0x100, thread.LastPc);
_memory.WriteUInt64(position + 0x108, (ulong)thread.Context.ThreadState.Psr);
_memory.WriteVector128(position + 0x110, thread.Context.ThreadState.V0);
_memory.WriteVector128(position + 0x120, thread.Context.ThreadState.V1);
_memory.WriteVector128(position + 0x130, thread.Context.ThreadState.V2);
_memory.WriteVector128(position + 0x140, thread.Context.ThreadState.V3);
_memory.WriteVector128(position + 0x150, thread.Context.ThreadState.V4);
_memory.WriteVector128(position + 0x160, thread.Context.ThreadState.V5);
_memory.WriteVector128(position + 0x170, thread.Context.ThreadState.V6);
_memory.WriteVector128(position + 0x180, thread.Context.ThreadState.V7);
_memory.WriteVector128(position + 0x190, thread.Context.ThreadState.V8);
_memory.WriteVector128(position + 0x1a0, thread.Context.ThreadState.V9);
_memory.WriteVector128(position + 0x1b0, thread.Context.ThreadState.V10);
_memory.WriteVector128(position + 0x1c0, thread.Context.ThreadState.V11);
_memory.WriteVector128(position + 0x1d0, thread.Context.ThreadState.V12);
_memory.WriteVector128(position + 0x1e0, thread.Context.ThreadState.V13);
_memory.WriteVector128(position + 0x1f0, thread.Context.ThreadState.V14);
_memory.WriteVector128(position + 0x200, thread.Context.ThreadState.V15);
_memory.WriteVector128(position + 0x210, thread.Context.ThreadState.V16);
_memory.WriteVector128(position + 0x220, thread.Context.ThreadState.V17);
_memory.WriteVector128(position + 0x230, thread.Context.ThreadState.V18);
_memory.WriteVector128(position + 0x240, thread.Context.ThreadState.V19);
_memory.WriteVector128(position + 0x250, thread.Context.ThreadState.V20);
_memory.WriteVector128(position + 0x260, thread.Context.ThreadState.V21);
_memory.WriteVector128(position + 0x270, thread.Context.ThreadState.V22);
_memory.WriteVector128(position + 0x280, thread.Context.ThreadState.V23);
_memory.WriteVector128(position + 0x290, thread.Context.ThreadState.V24);
_memory.WriteVector128(position + 0x2a0, thread.Context.ThreadState.V25);
_memory.WriteVector128(position + 0x2b0, thread.Context.ThreadState.V26);
_memory.WriteVector128(position + 0x2c0, thread.Context.ThreadState.V27);
_memory.WriteVector128(position + 0x2d0, thread.Context.ThreadState.V28);
_memory.WriteVector128(position + 0x2e0, thread.Context.ThreadState.V29);
_memory.WriteVector128(position + 0x2f0, thread.Context.ThreadState.V30);
_memory.WriteVector128(position + 0x300, thread.Context.ThreadState.V31);
_memory.WriteInt32(position + 0x310, thread.Context.ThreadState.Fpcr);
_memory.WriteInt32(position + 0x314, thread.Context.ThreadState.Fpsr);
_memory.WriteInt64(position + 0x318, thread.Context.ThreadState.Tpidr);
threadState.X0 = 0;
2018-06-26 06:09:32 +02:00
}
2018-02-05 00:08:20 +01:00
}
}