From 9d937ab93c1ee96db4916b327b63b029ddb845c6 Mon Sep 17 00:00:00 2001 From: Thog Date: Thu, 25 Jul 2019 13:59:57 +0200 Subject: [PATCH] Address comment --- ChocolArm64/Memory/MemoryManager.cs | 28 ---------- .../HOS/Services/Time/Clock/ClockTypes.cs | 8 +-- .../Clock/EphemeralNetworkSystemClockCore.cs | 8 +-- .../Clock/StandardLocalSystemClockCore.cs | 8 +-- .../Clock/StandardNetworkSystemClockCore.cs | 8 +-- .../Time/Clock/StandardSteadyClockCore.cs | 8 +-- .../Time/Clock/StandardUserSystemClockCore.cs | 8 +-- .../Services/Time/Clock/SystemClockCore.cs | 4 +- .../Time/Clock/TickBasedSteadyClockCore.cs | 10 ++-- .../HOS/Services/Time/IStaticService.cs | 56 +++++++++++++------ 10 files changed, 70 insertions(+), 76 deletions(-) diff --git a/ChocolArm64/Memory/MemoryManager.cs b/ChocolArm64/Memory/MemoryManager.cs index afa5aff97e..364f6b58a5 100644 --- a/ChocolArm64/Memory/MemoryManager.cs +++ b/ChocolArm64/Memory/MemoryManager.cs @@ -759,19 +759,6 @@ namespace ChocolArm64.Memory } } - public unsafe T ReadStruct(long position) - where T : struct - { - int size = Marshal.SizeOf(); - - byte[] data = ReadBytes(position, size); - - fixed (byte* ptr = data) - { - return Marshal.PtrToStructure((IntPtr)ptr); - } - } - public void WriteSByte(long position, sbyte value) { WriteByte(position, (byte)value); @@ -965,21 +952,6 @@ namespace ChocolArm64.Memory } } - public unsafe void WriteStruct(long position, T value) - where T : struct - { - long size = Marshal.SizeOf(); - - byte[] data = new byte[size]; - - fixed (byte* ptr = data) - { - Marshal.StructureToPtr(value, (IntPtr)ptr, false); - } - - WriteBytes(position, data); - } - public void CopyBytes(long src, long dst, long size) { // Note: This will be moved later. diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/ClockTypes.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/ClockTypes.cs index 27113c2ca9..c70819c003 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/ClockTypes.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/ClockTypes.cs @@ -7,6 +7,8 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock [StructLayout(LayoutKind.Sequential)] struct TimeSpanType { + private const long NanoSecondsPerSecond = 1000000000; + public long NanoSeconds; public TimeSpanType(long nanoSeconds) @@ -16,12 +18,12 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock public long ToSeconds() { - return NanoSeconds / 1000000000; + return NanoSeconds / NanoSecondsPerSecond; } public static TimeSpanType FromSeconds(long seconds) { - return new TimeSpanType(seconds * 1000000000); + return new TimeSpanType(seconds * NanoSecondsPerSecond); } public static TimeSpanType FromTicks(ulong ticks, ulong frequency) @@ -65,7 +67,6 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock public SteadyClockTimePoint SteadyTimePoint; } - [StructLayout(LayoutKind.Sequential, Size = 0xD0)] struct ClockSnapshot { @@ -87,7 +88,6 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock public byte Type; public ushort Unknown; - public static ResultCode GetCurrentTime(out long currentTime, SteadyClockTimePoint steadyClockTimePoint, SystemClockContext context) { currentTime = 0; diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/EphemeralNetworkSystemClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/EphemeralNetworkSystemClockCore.cs index 4fd2d16fb7..8e9073a40a 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/EphemeralNetworkSystemClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/EphemeralNetworkSystemClockCore.cs @@ -2,18 +2,18 @@ { class EphemeralNetworkSystemClockCore : SystemClockCore { - private static EphemeralNetworkSystemClockCore instance; + private static EphemeralNetworkSystemClockCore _instance; public static EphemeralNetworkSystemClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new EphemeralNetworkSystemClockCore(TickBasedSteadyClockCore.Instance); + _instance = new EphemeralNetworkSystemClockCore(TickBasedSteadyClockCore.Instance); } - return instance; + return _instance; } } diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardLocalSystemClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardLocalSystemClockCore.cs index 1742430c78..a172797608 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardLocalSystemClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardLocalSystemClockCore.cs @@ -2,18 +2,18 @@ { class StandardLocalSystemClockCore : SystemClockCore { - private static StandardLocalSystemClockCore instance; + private static StandardLocalSystemClockCore _instance; public static StandardLocalSystemClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new StandardLocalSystemClockCore(StandardSteadyClockCore.Instance); + _instance = new StandardLocalSystemClockCore(StandardSteadyClockCore.Instance); } - return instance; + return _instance; } } diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardNetworkSystemClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardNetworkSystemClockCore.cs index 5b06438c8c..5037fb6006 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardNetworkSystemClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardNetworkSystemClockCore.cs @@ -6,18 +6,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock { private TimeSpanType _standardNetworkClockSufficientAccuracy; - private static StandardNetworkSystemClockCore instance; + private static StandardNetworkSystemClockCore _instance; public static StandardNetworkSystemClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new StandardNetworkSystemClockCore(StandardSteadyClockCore.Instance); + _instance = new StandardNetworkSystemClockCore(StandardSteadyClockCore.Instance); } - return instance; + return _instance; } } diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardSteadyClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardSteadyClockCore.cs index d2cf96c554..fea5bf2f6b 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardSteadyClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardSteadyClockCore.cs @@ -11,18 +11,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock private TimeSpanType _testOffset; private TimeSpanType _internalOffset; - private static StandardSteadyClockCore instance; + private static StandardSteadyClockCore _instance; public static StandardSteadyClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new StandardSteadyClockCore(); + _instance = new StandardSteadyClockCore(); } - return instance; + return _instance; } } diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardUserSystemClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardUserSystemClockCore.cs index 7227f74f2d..c98b0064b6 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/StandardUserSystemClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/StandardUserSystemClockCore.cs @@ -8,18 +8,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock private StandardNetworkSystemClockCore _networkSystemClockCore; private bool _autoCorrectionEnabled; - private static StandardUserSystemClockCore instance; + private static StandardUserSystemClockCore _instance; public static StandardUserSystemClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new StandardUserSystemClockCore(StandardLocalSystemClockCore.Instance, StandardNetworkSystemClockCore.Instance); + _instance = new StandardUserSystemClockCore(StandardLocalSystemClockCore.Instance, StandardNetworkSystemClockCore.Instance); } - return instance; + return _instance; } } diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/SystemClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/SystemClockCore.cs index 9e2970463b..52f3c9083c 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/SystemClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/SystemClockCore.cs @@ -4,8 +4,8 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock { abstract class SystemClockCore { - private SteadyClockCore _steadyClockCore; - private SystemClockContext _context; + private SteadyClockCore _steadyClockCore; + private SystemClockContext _context; public SystemClockCore(SteadyClockCore steadyClockCore) { diff --git a/Ryujinx.HLE/HOS/Services/Time/Clock/TickBasedSteadyClockCore.cs b/Ryujinx.HLE/HOS/Services/Time/Clock/TickBasedSteadyClockCore.cs index d797294078..7a69b014bc 100644 --- a/Ryujinx.HLE/HOS/Services/Time/Clock/TickBasedSteadyClockCore.cs +++ b/Ryujinx.HLE/HOS/Services/Time/Clock/TickBasedSteadyClockCore.cs @@ -5,18 +5,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock { class TickBasedSteadyClockCore : SteadyClockCore { - private static TickBasedSteadyClockCore instance; + private static TickBasedSteadyClockCore _instance; public static TickBasedSteadyClockCore Instance { get { - if (instance == null) + if (_instance == null) { - instance = new TickBasedSteadyClockCore(); + _instance = new TickBasedSteadyClockCore(); } - return instance; + return _instance; } } @@ -26,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock { SteadyClockTimePoint result = new SteadyClockTimePoint { - TimePoint = 0, + TimePoint = 0, ClockSourceId = GetClockSourceId() }; diff --git a/Ryujinx.HLE/HOS/Services/Time/IStaticService.cs b/Ryujinx.HLE/HOS/Services/Time/IStaticService.cs index 89c34f32e3..9ee038d586 100644 --- a/Ryujinx.HLE/HOS/Services/Time/IStaticService.cs +++ b/Ryujinx.HLE/HOS/Services/Time/IStaticService.cs @@ -5,6 +5,9 @@ using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Services.Time.Clock; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; namespace Ryujinx.HLE.HOS.Services.Time { @@ -131,8 +134,7 @@ namespace Ryujinx.HLE.HOS.Services.Time // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64 public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context) { - SystemClockContext otherContext = context.RequestData.ReadStruct(); - + SystemClockContext otherContext = context.RequestData.ReadStruct(); SteadyClockTimePoint currentTimePoint = StandardSteadyClockCore.Instance.GetCurrentTimePoint(context.Thread); ResultCode result = ResultCode.TimeMismatch; @@ -140,8 +142,7 @@ namespace Ryujinx.HLE.HOS.Services.Time if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId) { TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(context.Thread.Context.ThreadState.CntpctEl0, context.Thread.Context.ThreadState.CntfrqEl0); - - long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds(); + long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds(); context.ResponseData.Write(baseTimePoint); @@ -157,9 +158,6 @@ namespace Ryujinx.HLE.HOS.Services.Time { byte type = context.RequestData.ReadByte(); - long bufferPosition = context.Request.RecvListBuff[0].Position; - long bufferSize = context.Request.RecvListBuff[0].Size; - ResultCode result = StandardUserSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext userContext); if (result == ResultCode.Success) @@ -172,7 +170,7 @@ namespace Ryujinx.HLE.HOS.Services.Time if (result == ResultCode.Success) { - context.Memory.WriteStruct(bufferPosition, clockSnapshot); + WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot); } } } @@ -185,18 +183,17 @@ namespace Ryujinx.HLE.HOS.Services.Time public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context) { byte type = context.RequestData.ReadByte(); + context.RequestData.BaseStream.Position += 7; SystemClockContext userContext = context.RequestData.ReadStruct(); SystemClockContext networkContext = context.RequestData.ReadStruct(); - long bufferPosition = context.Request.RecvListBuff[0].Position; - long bufferSize = context.Request.RecvListBuff[0].Size; ResultCode result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot); if (result == ResultCode.Success) { - context.Memory.WriteStruct(bufferPosition, clockSnapshot); + WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot); } return result; @@ -206,8 +203,9 @@ namespace Ryujinx.HLE.HOS.Services.Time // CalculateStandardUserSystemClockDifferenceByUser(buffer, buffer) -> nn::TimeSpanType public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context) { - ClockSnapshot clockSnapshotA = context.Memory.ReadStruct(context.Request.ExchangeBuff[0].Position); - ClockSnapshot clockSnapshotB = context.Memory.ReadStruct(context.Request.ExchangeBuff[1].Position); + + ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]); + ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]); TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset); if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled)) @@ -224,8 +222,8 @@ namespace Ryujinx.HLE.HOS.Services.Time // CalculateSpanBetween(buffer, buffer) -> nn::TimeSpanType public ResultCode CalculateSpanBetween(ServiceCtx context) { - ClockSnapshot clockSnapshotA = context.Memory.ReadStruct(context.Request.ExchangeBuff[0].Position); - ClockSnapshot clockSnapshotB = context.Memory.ReadStruct(context.Request.ExchangeBuff[1].Position); + ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]); + ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]); TimeSpanType result; @@ -259,8 +257,7 @@ namespace Ryujinx.HLE.HOS.Services.Time { clockSnapshot = new ClockSnapshot(); - SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance; - + SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance; SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread); clockSnapshot.IsAutomaticCorrectionEnabled = StandardUserSystemClockCore.Instance.IsAutomaticCorrectionEnabled(); @@ -306,5 +303,30 @@ namespace Ryujinx.HLE.HOS.Services.Time return result; } + + private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcBuffDesc ipcDesc) + { + Debug.Assert(ipcDesc.Size == Marshal.SizeOf()); + + using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(ipcDesc.Position, ipcDesc.Size)))) + { + return bufferReader.ReadStruct(); + } + } + + private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot) + { + Debug.Assert(ipcDesc.Size == Marshal.SizeOf()); + + MemoryStream memory = new MemoryStream((int)ipcDesc.Size); + + using (BinaryWriter bufferWriter = new BinaryWriter(memory)) + { + bufferWriter.WriteStruct(clockSnapshot); + } + + context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray()); + memory.Dispose(); + } } } \ No newline at end of file