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