2018-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult WaitSynchronization64([R(1)] ulong handlesPtr, [R(2)] int handlesCount, [R(3)] long timeout, [R(1)] out int handleIndex)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return WaitSynchronization(handlesPtr, handlesCount, timeout, out handleIndex);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult WaitSynchronization32(
|
|
|
|
[R(0)] uint timeoutLow,
|
|
|
|
[R(1)] uint handlesPtr,
|
|
|
|
[R(2)] int handlesCount,
|
|
|
|
[R(3)] uint timeoutHigh,
|
|
|
|
[R(1)] out int handleIndex)
|
|
|
|
{
|
|
|
|
long timeout = (long)(timeoutLow | ((ulong)timeoutHigh << 32));
|
|
|
|
|
|
|
|
return WaitSynchronization(handlesPtr, handlesCount, timeout, out handleIndex);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult WaitSynchronization(ulong handlesPtr, int handlesCount, long timeout, out int handleIndex)
|
|
|
|
{
|
|
|
|
handleIndex = 0;
|
|
|
|
|
|
|
|
if ((uint)handlesCount > 0x40)
|
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<KSynchronizationObject> syncObjs = new List<KSynchronizationObject>();
|
|
|
|
|
|
|
|
for (int index = 0; index < handlesCount; index++)
|
|
|
|
{
|
2020-05-03 22:54:50 +00:00
|
|
|
int handle = _process.CpuMemory.Read<int>(handlesPtr + (ulong)index * 4);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
KSynchronizationObject syncObj = _process.HandleTable.GetObject<KSynchronizationObject>(handle);
|
|
|
|
|
|
|
|
if (syncObj == null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
syncObjs.Add(syncObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _system.Synchronization.WaitFor(syncObjs.ToArray(), timeout, out handleIndex);
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult CancelSynchronization64([R(0)] int handle)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return CancelSynchronization(handle);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult CancelSynchronization32([R(0)] int handle)
|
|
|
|
{
|
|
|
|
return CancelSynchronization(handle);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult CancelSynchronization(int handle)
|
|
|
|
{
|
|
|
|
KThread thread = _process.HandleTable.GetKThread(handle);
|
|
|
|
|
|
|
|
if (thread == null)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread.CancelSynchronization();
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult ArbitrateLock64([R(0)] int ownerHandle, [R(1)] ulong mutexAddress, [R(2)] int requesterHandle)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult ArbitrateLock32([R(0)] int ownerHandle, [R(1)] uint mutexAddress, [R(2)] int requesterHandle)
|
|
|
|
{
|
|
|
|
return ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult ArbitrateLock(int ownerHandle, ulong mutexAddress, int requesterHandle)
|
|
|
|
{
|
|
|
|
if (IsPointingInsideKernel(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAddressNotWordAligned(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
return currentProcess.AddressArbiter.ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult ArbitrateUnlock64([R(0)] ulong mutexAddress)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return ArbitrateUnlock(mutexAddress);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult ArbitrateUnlock32([R(0)] uint mutexAddress)
|
|
|
|
{
|
|
|
|
return ArbitrateUnlock(mutexAddress);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult ArbitrateUnlock(ulong mutexAddress)
|
|
|
|
{
|
|
|
|
if (IsPointingInsideKernel(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAddressNotWordAligned(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
return currentProcess.AddressArbiter.ArbitrateUnlock(mutexAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult WaitProcessWideKeyAtomic64(
|
2020-01-13 02:04:28 +00:00
|
|
|
[R(0)] ulong mutexAddress,
|
|
|
|
[R(1)] ulong condVarAddress,
|
|
|
|
[R(2)] int handle,
|
|
|
|
[R(3)] long timeout)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return WaitProcessWideKeyAtomic(mutexAddress, condVarAddress, handle, timeout);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult WaitProcessWideKeyAtomic32(
|
|
|
|
[R(0)] uint mutexAddress,
|
|
|
|
[R(1)] uint condVarAddress,
|
|
|
|
[R(2)] int handle,
|
|
|
|
[R(3)] uint timeoutLow,
|
|
|
|
[R(4)] uint timeoutHigh)
|
|
|
|
{
|
|
|
|
long timeout = (long)(timeoutLow | ((ulong)timeoutHigh << 32));
|
|
|
|
|
|
|
|
return WaitProcessWideKeyAtomic(mutexAddress, condVarAddress, handle, timeout);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult WaitProcessWideKeyAtomic(
|
|
|
|
ulong mutexAddress,
|
|
|
|
ulong condVarAddress,
|
|
|
|
int handle,
|
|
|
|
long timeout)
|
|
|
|
{
|
|
|
|
if (IsPointingInsideKernel(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAddressNotWordAligned(mutexAddress))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
return currentProcess.AddressArbiter.WaitProcessWideKeyAtomic(
|
|
|
|
mutexAddress,
|
|
|
|
condVarAddress,
|
|
|
|
handle,
|
|
|
|
timeout);
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult SignalProcessWideKey64([R(0)] ulong address, [R(1)] int count)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return SignalProcessWideKey(address, count);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult SignalProcessWideKey32([R(0)] uint address, [R(1)] int count)
|
|
|
|
{
|
|
|
|
return SignalProcessWideKey(address, count);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult SignalProcessWideKey(ulong address, int count)
|
|
|
|
{
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
currentProcess.AddressArbiter.SignalProcessWideKey(address, count);
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult WaitForAddress64([R(0)] ulong address, [R(1)] ArbitrationType type, [R(2)] int value, [R(3)] long timeout)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return WaitForAddress(address, type, value, timeout);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult WaitForAddress32([R(0)] uint address, [R(1)] ArbitrationType type, [R(2)] int value, [R(3)] uint timeoutLow, [R(4)] uint timeoutHigh)
|
|
|
|
{
|
|
|
|
long timeout = (long)(timeoutLow | ((ulong)timeoutHigh << 32));
|
|
|
|
|
|
|
|
return WaitForAddress(address, type, value, timeout);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult WaitForAddress(ulong address, ArbitrationType type, int value, long timeout)
|
|
|
|
{
|
|
|
|
if (IsPointingInsideKernel(address))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAddressNotWordAligned(address))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
KernelResult result;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ArbitrationType.WaitIfLessThan:
|
|
|
|
result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, false, timeout);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ArbitrationType.DecrementAndWaitIfLessThan:
|
|
|
|
result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, true, timeout);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ArbitrationType.WaitIfEqual:
|
|
|
|
result = currentProcess.AddressArbiter.WaitForAddressIfEqual(address, value, timeout);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result = KernelResult.InvalidEnumValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:04:28 +00:00
|
|
|
public KernelResult SignalToAddress64([R(0)] ulong address, [R(1)] SignalType type, [R(2)] int value, [R(3)] int count)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
|
|
|
return SignalToAddress(address, type, value, count);
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:21:53 +00:00
|
|
|
public KernelResult SignalToAddress32([R(0)] uint address, [R(1)] SignalType type, [R(2)] int value, [R(3)] int count)
|
|
|
|
{
|
|
|
|
return SignalToAddress(address, type, value, count);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
private KernelResult SignalToAddress(ulong address, SignalType type, int value, int count)
|
|
|
|
{
|
|
|
|
if (IsPointingInsideKernel(address))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAddressNotWordAligned(address))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
KernelResult result;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case SignalType.Signal:
|
|
|
|
result = currentProcess.AddressArbiter.Signal(address, count);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType.SignalAndIncrementIfEqual:
|
|
|
|
result = currentProcess.AddressArbiter.SignalAndIncrementIfEqual(address, value, count);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType.SignalAndModifyIfEqual:
|
|
|
|
result = currentProcess.AddressArbiter.SignalAndModifyIfEqual(address, value, count);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result = KernelResult.InvalidEnumValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsPointingInsideKernel(ulong address)
|
|
|
|
{
|
|
|
|
return (address + 0x1000000000) < 0xffffff000;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsAddressNotWordAligned(ulong address)
|
|
|
|
{
|
|
|
|
return (address & 3) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|