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