diff --git a/src/SharpEmu.Libs/Kernel/KernelMemoryCompatExports.cs b/src/SharpEmu.Libs/Kernel/KernelMemoryCompatExports.cs index ece78e8..8f3c391 100644 --- a/src/SharpEmu.Libs/Kernel/KernelMemoryCompatExports.cs +++ b/src/SharpEmu.Libs/Kernel/KernelMemoryCompatExports.cs @@ -67,13 +67,6 @@ public static class KernelMemoryCompatExports private const int Einval = 22; private const int Erange = 34; private const int Struncate = 80; - private const int ClockRealtime = 0; - private const int ClockVirtual = 1; - private const int ClockProf = 2; - private const int ClockMonotonic = 4; - private const int ClockUptime = 5; - private const int ClockRealtimeFast = 10; - private const int ClockMonotonicFast = 12; private const nuint DefaultLibcHeapAlignment = 16; private const ushort KernelStatModeDirectory = 0x41FF; private const ushort KernelStatModeRegular = 0x81FF; @@ -2011,29 +2004,11 @@ public static class KernelMemoryCompatExports long seconds; long nanoseconds; - switch (clockId) + if (!KernelRuntimeCompatExports.ResolveClockTime(clockId, out seconds, out nanoseconds)) { - case ClockRealtime: - case ClockRealtimeFast: - case ClockVirtual: - case ClockProf: - { - var now = DateTimeOffset.UtcNow; - seconds = now.ToUnixTimeSeconds(); - nanoseconds = (now.Ticks % TimeSpan.TicksPerSecond) * 100; - break; - } - - case ClockMonotonic: - case ClockMonotonicFast: - case ClockUptime: - KernelRuntimeCompatExports.GetProcessMonotonicTime(out seconds, out nanoseconds); - break; - - default: - KernelRuntimeCompatExports.TrySetErrno(ctx, Einval); - ctx[CpuRegister.Rax] = unchecked((ulong)-1); - return -1; + KernelRuntimeCompatExports.TrySetErrno(ctx, Einval); + ctx[CpuRegister.Rax] = unchecked((ulong)-1); + return -1; } Span timespecBuffer = stackalloc byte[16]; diff --git a/src/SharpEmu.Libs/Kernel/KernelRuntimeCompatExports.cs b/src/SharpEmu.Libs/Kernel/KernelRuntimeCompatExports.cs index 9ffcf1c..6e07f41 100644 --- a/src/SharpEmu.Libs/Kernel/KernelRuntimeCompatExports.cs +++ b/src/SharpEmu.Libs/Kernel/KernelRuntimeCompatExports.cs @@ -14,6 +14,15 @@ namespace SharpEmu.Libs.Kernel; public static class KernelRuntimeCompatExports { + internal const int ClockRealtime = 0; + internal const int ClockVirtual = 1; + internal const int ClockProf = 2; + internal const int ClockMonotonic = 4; + internal const int ClockUptime = 5; + internal const int ClockRealtimePrecise = 9; + internal const int ClockMonotonicPrecise = 11; + internal const int ClockRealtimeFast = 10; + internal const int ClockMonotonicFast = 12; private const int Efault = 14; private const ulong TlsErrnoOffset = 0x40; private const ulong TlsStackChkGuardBaseOffset = 0x800; @@ -187,15 +196,9 @@ public static class KernelRuntimeCompatExports long seconds; long nanoseconds; - if (clockId == 0) + if (!ResolveClockTime(clockId, out seconds, out nanoseconds)) { - var now = DateTimeOffset.UtcNow; - seconds = now.ToUnixTimeSeconds(); - nanoseconds = (now.Ticks % TimeSpan.TicksPerSecond) * 100; - } - else - { - GetProcessMonotonicTime(out seconds, out nanoseconds); + return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT; } Span timespecBuffer = stackalloc byte[16]; @@ -632,6 +635,36 @@ public static class KernelRuntimeCompatExports return address != 0 && ctx.TryWriteInt32(address, value); } + internal static bool ResolveClockTime(int clockId, out long seconds, out long nanoseconds) + { + switch (clockId) + { + case ClockRealtime: + case ClockRealtimePrecise: + case ClockRealtimeFast: + case ClockVirtual: + case ClockProf: + { + var now = DateTimeOffset.UtcNow; + seconds = now.ToUnixTimeSeconds(); + nanoseconds = (now.Ticks % TimeSpan.TicksPerSecond) * 100; + return true; + } + + case ClockMonotonic: + case ClockMonotonicPrecise: + case ClockMonotonicFast: + case ClockUptime: + GetProcessMonotonicTime(out seconds, out nanoseconds); + return true; + + default: + seconds = 0; + nanoseconds = 0; + return false; + } + } + internal static void GetProcessMonotonicTime(out long seconds, out long nanoseconds) { var elapsedTicks = Stopwatch.GetTimestamp() - _processStartCounter;