Ryujinx/Ryujinx.HLE/HOS/Kernel/SvcThreadSync.cs

373 lines
13 KiB
C#
Raw Normal View History

using ChocolArm64.State;
using Ryujinx.Common.Logging;
using System.Collections.Generic;
using static Ryujinx.HLE.HOS.ErrorCode;
namespace Ryujinx.HLE.HOS.Kernel
{
partial class SvcHandler
{
2018-12-01 21:01:59 +01:00
private void SvcWaitSynchronization(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long handlesPtr = (long)threadState.X1;
int handlesCount = (int)threadState.X2;
long timeout = (long)threadState.X3;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"HandlesPtr = 0x" + handlesPtr .ToString("x16") + ", " +
"HandlesCount = 0x" + handlesCount.ToString("x8") + ", " +
"Timeout = 0x" + timeout .ToString("x16"));
2018-12-01 21:01:59 +01:00
if ((uint)handlesCount > 0x40)
{
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.CountOutOfRange);
return;
}
2018-12-01 21:01:59 +01:00
List<KSynchronizationObject> syncObjs = new List<KSynchronizationObject>();
2018-12-01 21:01:59 +01:00
for (int index = 0; index < handlesCount; index++)
{
2018-12-01 21:01:59 +01:00
int handle = _memory.ReadInt32(handlesPtr + index * 4);
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, $"Sync handle 0x{handle:x8}");
2018-12-01 21:01:59 +01:00
KSynchronizationObject syncObj = _process.HandleTable.GetObject<KSynchronizationObject>(handle);
2018-12-01 21:01:59 +01:00
if (syncObj == null)
{
break;
}
2018-12-01 21:01:59 +01:00
syncObjs.Add(syncObj);
}
2018-12-01 21:01:59 +01:00
int hndIndex = (int)threadState.X1;
2018-12-01 21:01:59 +01:00
ulong high = threadState.X1 & (0xffffffffUL << 32);
2018-12-01 21:01:59 +01:00
long result = _system.Synchronization.WaitFor(syncObjs.ToArray(), timeout, ref hndIndex);
2018-12-01 21:01:59 +01:00
if (result != 0)
{
2018-12-01 21:01:59 +01:00
if (result == MakeError(ErrorModule.Kernel, KernelErr.Timeout) ||
result == MakeError(ErrorModule.Kernel, KernelErr.Cancelled))
{
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
}
}
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
threadState.X1 = (uint)hndIndex | high;
}
2018-12-01 21:01:59 +01:00
private void SvcCancelSynchronization(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int threadHandle = (int)threadState.X0;
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, "ThreadHandle = 0x" + threadHandle.ToString("x8"));
2018-12-01 21:01:59 +01:00
KThread thread = _process.HandleTable.GetKThread(threadHandle);
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{threadHandle: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.CancelSynchronization();
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
}
2018-12-01 21:01:59 +01:00
private void SvcArbitrateLock(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
int ownerHandle = (int)threadState.X0;
long mutexAddress = (long)threadState.X1;
int requesterHandle = (int)threadState.X2;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"OwnerHandle = 0x" + ownerHandle .ToString("x8") + ", " +
"MutexAddress = 0x" + mutexAddress .ToString("x16") + ", " +
"RequesterHandle = 0x" + requesterHandle.ToString("x8"));
2018-12-01 21:01:59 +01:00
if (IsPointingInsideKernel(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
return;
}
2018-12-01 21:01:59 +01:00
if (IsAddressNotWordAligned(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
long result = currentProcess.AddressArbiter.ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
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-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private void SvcArbitrateUnlock(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long mutexAddress = (long)threadState.X0;
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, "MutexAddress = 0x" + mutexAddress.ToString("x16"));
2018-12-01 21:01:59 +01:00
if (IsPointingInsideKernel(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
return;
}
2018-12-01 21:01:59 +01:00
if (IsAddressNotWordAligned(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
long result = currentProcess.AddressArbiter.ArbitrateUnlock(mutexAddress);
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-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private void SvcWaitProcessWideKeyAtomic(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long mutexAddress = (long)threadState.X0;
long condVarAddress = (long)threadState.X1;
int threadHandle = (int)threadState.X2;
long timeout = (long)threadState.X3;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"MutexAddress = 0x" + mutexAddress .ToString("x16") + ", " +
"CondVarAddress = 0x" + condVarAddress.ToString("x16") + ", " +
"ThreadHandle = 0x" + threadHandle .ToString("x8") + ", " +
"Timeout = 0x" + timeout .ToString("x16"));
2018-12-01 21:01:59 +01:00
if (IsPointingInsideKernel(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
return;
}
2018-12-01 21:01:59 +01:00
if (IsAddressNotWordAligned(mutexAddress))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{mutexAddress:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
long result = currentProcess.AddressArbiter.WaitProcessWideKeyAtomic(
mutexAddress,
condVarAddress,
threadHandle,
timeout);
2018-12-01 21:01:59 +01:00
if (result != 0)
{
2018-12-01 21:01:59 +01:00
if (result == MakeError(ErrorModule.Kernel, KernelErr.Timeout))
{
2018-12-01 21:01:59 +01:00
Logger.PrintDebug(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Operation failed with error 0x{result:x}!");
}
}
2018-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private void SvcSignalProcessWideKey(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long address = (long)threadState.X0;
int count = (int)threadState.X1;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"Address = 0x" + address.ToString("x16") + ", " +
"Count = 0x" + count .ToString("x8"));
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
currentProcess.AddressArbiter.SignalProcessWideKey(address, count);
2018-12-01 21:01:59 +01:00
threadState.X0 = 0;
}
2018-12-01 21:01:59 +01:00
private void SvcWaitForAddress(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long address = (long)threadState.X0;
ArbitrationType type = (ArbitrationType)threadState.X1;
int value = (int)threadState.X2;
long timeout = (long)threadState.X3;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"Address = 0x" + address.ToString("x16") + ", " +
"Type = " + type .ToString() + ", " +
"Value = 0x" + value .ToString("x8") + ", " +
"Timeout = 0x" + timeout.ToString("x16"));
2018-12-01 21:01:59 +01:00
if (IsPointingInsideKernel(address))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address 0x{address:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
return;
}
2018-12-01 21:01:59 +01:00
if (IsAddressNotWordAligned(address))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Unaligned address 0x{address:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
long result;
2018-12-01 21:01:59 +01:00
switch (type)
{
case ArbitrationType.WaitIfLessThan:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, false, timeout);
break;
case ArbitrationType.DecrementAndWaitIfLessThan:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, true, timeout);
break;
case ArbitrationType.WaitIfEqual:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.WaitForAddressIfEqual(address, value, timeout);
break;
default:
2018-12-01 21:01:59 +01:00
result = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
break;
}
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-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private void SvcSignalToAddress(CpuThreadState threadState)
{
2018-12-01 21:01:59 +01:00
long address = (long)threadState.X0;
SignalType type = (SignalType)threadState.X1;
int value = (int)threadState.X2;
int count = (int)threadState.X3;
Logger.PrintDebug(LogClass.KernelSvc,
2018-12-01 21:01:59 +01:00
"Address = 0x" + address.ToString("x16") + ", " +
"Type = " + type .ToString() + ", " +
"Value = 0x" + value .ToString("x8") + ", " +
"Count = 0x" + count .ToString("x8"));
2018-12-01 21:01:59 +01:00
if (IsPointingInsideKernel(address))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Invalid address 0x{address:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
return;
}
2018-12-01 21:01:59 +01:00
if (IsAddressNotWordAligned(address))
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.KernelSvc, $"Unaligned address 0x{address:x16}!");
2018-12-01 21:01:59 +01:00
threadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
2018-12-01 21:01:59 +01:00
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
2018-12-01 21:01:59 +01:00
long result;
2018-12-01 21:01:59 +01:00
switch (type)
{
case SignalType.Signal:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.Signal(address, count);
break;
case SignalType.SignalAndIncrementIfEqual:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.SignalAndIncrementIfEqual(address, value, count);
break;
case SignalType.SignalAndModifyIfEqual:
2018-12-01 21:01:59 +01:00
result = currentProcess.AddressArbiter.SignalAndModifyIfEqual(address, value, count);
break;
default:
2018-12-01 21:01:59 +01:00
result = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
break;
}
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-12-01 21:01:59 +01:00
threadState.X0 = (ulong)result;
}
2018-12-01 21:01:59 +01:00
private bool IsPointingInsideKernel(long address)
{
2018-12-01 21:01:59 +01:00
return ((ulong)address + 0x1000000000) < 0xffffff000;
}
2018-12-01 21:01:59 +01:00
private bool IsAddressNotWordAligned(long address)
{
2018-12-01 21:01:59 +01:00
return (address & 3) != 0;
}
}
}