2019-07-14 22:50:11 +02:00
|
|
|
using Ryujinx.Common;
|
2022-05-31 21:29:35 +02:00
|
|
|
using Ryujinx.Cpu;
|
2019-10-08 05:48:49 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2019-07-14 22:50:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
2023-01-04 23:15:45 +01:00
|
|
|
using Ryujinx.Horizon.Common;
|
2019-10-08 05:48:49 +02:00
|
|
|
using System;
|
2018-02-10 01:14:55 +01:00
|
|
|
|
2019-09-19 02:45:11 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
class ISystemClock : IpcService
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
private SystemClockCore _clockCore;
|
|
|
|
private bool _writePermission;
|
2019-10-08 05:48:49 +02:00
|
|
|
private bool _bypassUninitializedClock;
|
|
|
|
private int _operationEventReadableHandle;
|
2018-02-10 01:14:55 +01:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
public ISystemClock(SystemClockCore clockCore, bool writePermission, bool bypassUninitializedClock)
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
_clockCore = clockCore;
|
|
|
|
_writePermission = writePermission;
|
|
|
|
_bypassUninitializedClock = bypassUninitializedClock;
|
|
|
|
_operationEventReadableHandle = 0;
|
2018-02-10 01:14:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(0)]
|
2019-07-12 03:13:43 +02:00
|
|
|
// GetCurrentTime() -> nn::time::PosixTime
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetCurrentTime(ServiceCtx context)
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
2019-07-14 22:50:11 +02:00
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
ResultCode result = _clockCore.GetCurrentTime(tickSource, out long posixTime);
|
2018-02-14 06:43:21 +01:00
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
if (result == ResultCode.Success)
|
2018-02-14 06:43:21 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
context.ResponseData.Write(posixTime);
|
2019-07-14 22:50:11 +02:00
|
|
|
}
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
return result;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(1)]
|
2019-07-12 03:13:43 +02:00
|
|
|
// SetCurrentTime(nn::time::PosixTime)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode SetCurrentTime(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
if (!_writePermission)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
return ResultCode.PermissionDenied;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
2019-07-14 22:50:11 +02:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
return ResultCode.UninitializedClock;
|
2019-07-14 22:50:11 +02:00
|
|
|
}
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
return _clockCore.SetCurrentTime(tickSource, posixTime);
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(2)]
|
2019-10-08 05:48:49 +02:00
|
|
|
// GetClockContext() -> nn::time::SystemClockContext
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetSystemClockContext(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
ResultCode result = _clockCore.GetClockContext(tickSource, out SystemClockContext clockContext);
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
if (result == ResultCode.Success)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
context.ResponseData.WriteStruct(clockContext);
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
return result;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(3)]
|
2019-10-08 05:48:49 +02:00
|
|
|
// SetClockContext(nn::time::SystemClockContext)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode SetSystemClockContext(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
if (!_writePermission)
|
|
|
|
{
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
SystemClockContext clockContext = context.RequestData.ReadStruct<SystemClockContext>();
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
ResultCode result = _clockCore.SetSystemClockContext(clockContext);
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(4)] // 9.0.0+
|
2019-10-08 05:48:49 +02:00
|
|
|
// GetOperationEventReadableHandle() -> handle<copy>
|
|
|
|
public ResultCode GetOperationEventReadableHandle(ServiceCtx context)
|
|
|
|
{
|
|
|
|
if (_operationEventReadableHandle == 0)
|
2019-07-14 22:50:11 +02:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KEvent kEvent = new KEvent(context.Device.System.KernelContext);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
_clockCore.RegisterOperationEvent(kEvent.WritableEvent);
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
if (context.Process.HandleTable.GenerateHandle(kEvent.ReadableEvent, out _operationEventReadableHandle) != Result.Success)
|
2019-10-08 05:48:49 +02:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2019-07-14 22:50:11 +02:00
|
|
|
}
|
2018-02-10 01:14:55 +01:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_operationEventReadableHandle);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2018-02-10 01:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|