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