diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/arm64/native_clock.cpp | 24 | ||||
| -rw-r--r-- | src/common/arm64/native_clock.h | 10 | ||||
| -rw-r--r-- | src/common/atomic_ops.h | 79 | ||||
| -rw-r--r-- | src/common/common_types.h | 1 | ||||
| -rw-r--r-- | src/common/fs/file.h | 2 | ||||
| -rw-r--r-- | src/common/memory_detect.h | 2 | ||||
| -rw-r--r-- | src/common/overflow.h | 18 | ||||
| -rw-r--r-- | src/common/page_table.cpp | 34 | ||||
| -rw-r--r-- | src/common/settings.h | 13 | ||||
| -rw-r--r-- | src/common/settings_common.cpp | 2 | ||||
| -rw-r--r-- | src/common/settings_common.h | 2 | ||||
| -rw-r--r-- | src/common/time_zone.cpp | 12 | ||||
| -rw-r--r-- | src/common/uuid.h | 12 | ||||
| -rw-r--r-- | src/common/wall_clock.cpp | 32 | ||||
| -rw-r--r-- | src/common/wall_clock.h | 9 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 26 | ||||
| -rw-r--r-- | src/common/x64/native_clock.h | 9 |
17 files changed, 133 insertions, 154 deletions
diff --git a/src/common/arm64/native_clock.cpp b/src/common/arm64/native_clock.cpp index f437d7187..76ffb74ba 100644 --- a/src/common/arm64/native_clock.cpp +++ b/src/common/arm64/native_clock.cpp | |||
| @@ -30,27 +30,27 @@ NativeClock::NativeClock() { | |||
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | std::chrono::nanoseconds NativeClock::GetTimeNS() const { | 32 | std::chrono::nanoseconds NativeClock::GetTimeNS() const { |
| 33 | return std::chrono::nanoseconds{MultiplyHigh(GetHostTicksElapsed(), ns_cntfrq_factor)}; | 33 | return std::chrono::nanoseconds{MultiplyHigh(GetUptime(), ns_cntfrq_factor)}; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | std::chrono::microseconds NativeClock::GetTimeUS() const { | 36 | std::chrono::microseconds NativeClock::GetTimeUS() const { |
| 37 | return std::chrono::microseconds{MultiplyHigh(GetHostTicksElapsed(), us_cntfrq_factor)}; | 37 | return std::chrono::microseconds{MultiplyHigh(GetUptime(), us_cntfrq_factor)}; |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | std::chrono::milliseconds NativeClock::GetTimeMS() const { | 40 | std::chrono::milliseconds NativeClock::GetTimeMS() const { |
| 41 | return std::chrono::milliseconds{MultiplyHigh(GetHostTicksElapsed(), ms_cntfrq_factor)}; | 41 | return std::chrono::milliseconds{MultiplyHigh(GetUptime(), ms_cntfrq_factor)}; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | u64 NativeClock::GetCNTPCT() const { | 44 | s64 NativeClock::GetCNTPCT() const { |
| 45 | return MultiplyHigh(GetHostTicksElapsed(), guest_cntfrq_factor); | 45 | return MultiplyHigh(GetUptime(), guest_cntfrq_factor); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | u64 NativeClock::GetGPUTick() const { | 48 | s64 NativeClock::GetGPUTick() const { |
| 49 | return MultiplyHigh(GetHostTicksElapsed(), gputick_cntfrq_factor); | 49 | return MultiplyHigh(GetUptime(), gputick_cntfrq_factor); |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | u64 NativeClock::GetHostTicksNow() const { | 52 | s64 NativeClock::GetUptime() const { |
| 53 | u64 cntvct_el0 = 0; | 53 | s64 cntvct_el0 = 0; |
| 54 | asm volatile("dsb ish\n\t" | 54 | asm volatile("dsb ish\n\t" |
| 55 | "mrs %[cntvct_el0], cntvct_el0\n\t" | 55 | "mrs %[cntvct_el0], cntvct_el0\n\t" |
| 56 | "dsb ish\n\t" | 56 | "dsb ish\n\t" |
| @@ -58,15 +58,11 @@ u64 NativeClock::GetHostTicksNow() const { | |||
| 58 | return cntvct_el0; | 58 | return cntvct_el0; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | u64 NativeClock::GetHostTicksElapsed() const { | ||
| 62 | return GetHostTicksNow(); | ||
| 63 | } | ||
| 64 | |||
| 65 | bool NativeClock::IsNative() const { | 61 | bool NativeClock::IsNative() const { |
| 66 | return true; | 62 | return true; |
| 67 | } | 63 | } |
| 68 | 64 | ||
| 69 | u64 NativeClock::GetHostCNTFRQ() { | 65 | s64 NativeClock::GetHostCNTFRQ() { |
| 70 | u64 cntfrq_el0 = 0; | 66 | u64 cntfrq_el0 = 0; |
| 71 | std::string_view board{""}; | 67 | std::string_view board{""}; |
| 72 | #ifdef ANDROID | 68 | #ifdef ANDROID |
diff --git a/src/common/arm64/native_clock.h b/src/common/arm64/native_clock.h index a28b419f2..94bc1882e 100644 --- a/src/common/arm64/native_clock.h +++ b/src/common/arm64/native_clock.h | |||
| @@ -17,17 +17,15 @@ public: | |||
| 17 | 17 | ||
| 18 | std::chrono::milliseconds GetTimeMS() const override; | 18 | std::chrono::milliseconds GetTimeMS() const override; |
| 19 | 19 | ||
| 20 | u64 GetCNTPCT() const override; | 20 | s64 GetCNTPCT() const override; |
| 21 | 21 | ||
| 22 | u64 GetGPUTick() const override; | 22 | s64 GetGPUTick() const override; |
| 23 | 23 | ||
| 24 | u64 GetHostTicksNow() const override; | 24 | s64 GetUptime() const override; |
| 25 | |||
| 26 | u64 GetHostTicksElapsed() const override; | ||
| 27 | 25 | ||
| 28 | bool IsNative() const override; | 26 | bool IsNative() const override; |
| 29 | 27 | ||
| 30 | static u64 GetHostCNTFRQ(); | 28 | static s64 GetHostCNTFRQ(); |
| 31 | 29 | ||
| 32 | public: | 30 | public: |
| 33 | using FactorType = unsigned __int128; | 31 | using FactorType = unsigned __int128; |
diff --git a/src/common/atomic_ops.h b/src/common/atomic_ops.h index c18bb33c4..9bf6f2f81 100644 --- a/src/common/atomic_ops.h +++ b/src/common/atomic_ops.h | |||
| @@ -15,25 +15,34 @@ namespace Common { | |||
| 15 | 15 | ||
| 16 | #if _MSC_VER | 16 | #if _MSC_VER |
| 17 | 17 | ||
| 18 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { | 18 | template <typename T> |
| 19 | [[nodiscard]] inline bool AtomicCompareAndSwap(T* pointer, T value, T expected); | ||
| 20 | template <typename T> | ||
| 21 | [[nodiscard]] inline bool AtomicCompareAndSwap(T* pointer, T value, T expected, T& actual); | ||
| 22 | |||
| 23 | template <> | ||
| 24 | [[nodiscard]] inline bool AtomicCompareAndSwap<u8>(u8* pointer, u8 value, u8 expected) { | ||
| 19 | const u8 result = | 25 | const u8 result = |
| 20 | _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); | 26 | _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); |
| 21 | return result == expected; | 27 | return result == expected; |
| 22 | } | 28 | } |
| 23 | 29 | ||
| 24 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { | 30 | template <> |
| 31 | [[nodiscard]] inline bool AtomicCompareAndSwap<u16>(u16* pointer, u16 value, u16 expected) { | ||
| 25 | const u16 result = | 32 | const u16 result = |
| 26 | _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); | 33 | _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); |
| 27 | return result == expected; | 34 | return result == expected; |
| 28 | } | 35 | } |
| 29 | 36 | ||
| 30 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { | 37 | template <> |
| 38 | [[nodiscard]] inline bool AtomicCompareAndSwap<u32>(u32* pointer, u32 value, u32 expected) { | ||
| 31 | const u32 result = | 39 | const u32 result = |
| 32 | _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); | 40 | _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); |
| 33 | return result == expected; | 41 | return result == expected; |
| 34 | } | 42 | } |
| 35 | 43 | ||
| 36 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { | 44 | template <> |
| 45 | [[nodiscard]] inline bool AtomicCompareAndSwap<u64>(u64* pointer, u64 value, u64 expected) { | ||
| 37 | const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), | 46 | const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), |
| 38 | value, expected); | 47 | value, expected); |
| 39 | return result == expected; | 48 | return result == expected; |
| @@ -45,29 +54,32 @@ namespace Common { | |||
| 45 | reinterpret_cast<__int64*>(expected.data())) != 0; | 54 | reinterpret_cast<__int64*>(expected.data())) != 0; |
| 46 | } | 55 | } |
| 47 | 56 | ||
| 48 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected, | 57 | template <> |
| 49 | u8& actual) { | 58 | [[nodiscard]] inline bool AtomicCompareAndSwap<u8>(u8* pointer, u8 value, u8 expected, u8& actual) { |
| 50 | actual = | 59 | actual = |
| 51 | _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); | 60 | _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); |
| 52 | return actual == expected; | 61 | return actual == expected; |
| 53 | } | 62 | } |
| 54 | 63 | ||
| 55 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected, | 64 | template <> |
| 56 | u16& actual) { | 65 | [[nodiscard]] inline bool AtomicCompareAndSwap<u16>(u16* pointer, u16 value, u16 expected, |
| 66 | u16& actual) { | ||
| 57 | actual = | 67 | actual = |
| 58 | _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); | 68 | _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); |
| 59 | return actual == expected; | 69 | return actual == expected; |
| 60 | } | 70 | } |
| 61 | 71 | ||
| 62 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected, | 72 | template <> |
| 63 | u32& actual) { | 73 | [[nodiscard]] inline bool AtomicCompareAndSwap<u32>(u32* pointer, u32 value, u32 expected, |
| 74 | u32& actual) { | ||
| 64 | actual = | 75 | actual = |
| 65 | _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); | 76 | _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); |
| 66 | return actual == expected; | 77 | return actual == expected; |
| 67 | } | 78 | } |
| 68 | 79 | ||
| 69 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected, | 80 | template <> |
| 70 | u64& actual) { | 81 | [[nodiscard]] inline bool AtomicCompareAndSwap<u64>(u64* pointer, u64 value, u64 expected, |
| 82 | u64& actual) { | ||
| 71 | actual = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), value, | 83 | actual = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), value, |
| 72 | expected); | 84 | expected); |
| 73 | return actual == expected; | 85 | return actual == expected; |
| @@ -91,23 +103,12 @@ namespace Common { | |||
| 91 | 103 | ||
| 92 | #else | 104 | #else |
| 93 | 105 | ||
| 94 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { | 106 | template <typename T> |
| 95 | return __sync_bool_compare_and_swap(pointer, expected, value); | 107 | [[nodiscard]] inline bool AtomicCompareAndSwap(T* pointer, T value, T expected) { |
| 96 | } | ||
| 97 | |||
| 98 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { | ||
| 99 | return __sync_bool_compare_and_swap(pointer, expected, value); | ||
| 100 | } | ||
| 101 | |||
| 102 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { | ||
| 103 | return __sync_bool_compare_and_swap(pointer, expected, value); | 108 | return __sync_bool_compare_and_swap(pointer, expected, value); |
| 104 | } | 109 | } |
| 105 | 110 | ||
| 106 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { | 111 | [[nodiscard]] inline bool AtomicCompareAndSwap(u64* pointer, u128 value, u128 expected) { |
| 107 | return __sync_bool_compare_and_swap(pointer, expected, value); | ||
| 108 | } | ||
| 109 | |||
| 110 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected) { | ||
| 111 | unsigned __int128 value_a; | 112 | unsigned __int128 value_a; |
| 112 | unsigned __int128 expected_a; | 113 | unsigned __int128 expected_a; |
| 113 | std::memcpy(&value_a, value.data(), sizeof(u128)); | 114 | std::memcpy(&value_a, value.data(), sizeof(u128)); |
| @@ -115,31 +116,13 @@ namespace Common { | |||
| 115 | return __sync_bool_compare_and_swap((unsigned __int128*)pointer, expected_a, value_a); | 116 | return __sync_bool_compare_and_swap((unsigned __int128*)pointer, expected_a, value_a); |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected, | 119 | template <typename T> |
| 119 | u8& actual) { | 120 | [[nodiscard]] inline bool AtomicCompareAndSwap(T* pointer, T value, T expected, T& actual) { |
| 120 | actual = __sync_val_compare_and_swap(pointer, expected, value); | 121 | actual = __sync_val_compare_and_swap(pointer, expected, value); |
| 121 | return actual == expected; | 122 | return actual == expected; |
| 122 | } | 123 | } |
| 123 | 124 | ||
| 124 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected, | 125 | [[nodiscard]] inline bool AtomicCompareAndSwap(u64* pointer, u128 value, u128 expected, |
| 125 | u16& actual) { | ||
| 126 | actual = __sync_val_compare_and_swap(pointer, expected, value); | ||
| 127 | return actual == expected; | ||
| 128 | } | ||
| 129 | |||
| 130 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected, | ||
| 131 | u32& actual) { | ||
| 132 | actual = __sync_val_compare_and_swap(pointer, expected, value); | ||
| 133 | return actual == expected; | ||
| 134 | } | ||
| 135 | |||
| 136 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected, | ||
| 137 | u64& actual) { | ||
| 138 | actual = __sync_val_compare_and_swap(pointer, expected, value); | ||
| 139 | return actual == expected; | ||
| 140 | } | ||
| 141 | |||
| 142 | [[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected, | ||
| 143 | u128& actual) { | 126 | u128& actual) { |
| 144 | unsigned __int128 value_a; | 127 | unsigned __int128 value_a; |
| 145 | unsigned __int128 expected_a; | 128 | unsigned __int128 expected_a; |
| @@ -151,7 +134,7 @@ namespace Common { | |||
| 151 | return actual_a == expected_a; | 134 | return actual_a == expected_a; |
| 152 | } | 135 | } |
| 153 | 136 | ||
| 154 | [[nodiscard]] inline u128 AtomicLoad128(volatile u64* pointer) { | 137 | [[nodiscard]] inline u128 AtomicLoad128(u64* pointer) { |
| 155 | unsigned __int128 zeros_a = 0; | 138 | unsigned __int128 zeros_a = 0; |
| 156 | unsigned __int128 result_a = | 139 | unsigned __int128 result_a = |
| 157 | __sync_val_compare_and_swap((unsigned __int128*)pointer, zeros_a, zeros_a); | 140 | __sync_val_compare_and_swap((unsigned __int128*)pointer, zeros_a, zeros_a); |
diff --git a/src/common/common_types.h b/src/common/common_types.h index 0fc225aff..ae04c4d60 100644 --- a/src/common/common_types.h +++ b/src/common/common_types.h | |||
| @@ -45,6 +45,7 @@ using f32 = float; ///< 32-bit floating point | |||
| 45 | using f64 = double; ///< 64-bit floating point | 45 | using f64 = double; ///< 64-bit floating point |
| 46 | 46 | ||
| 47 | using VAddr = u64; ///< Represents a pointer in the userspace virtual address space. | 47 | using VAddr = u64; ///< Represents a pointer in the userspace virtual address space. |
| 48 | using DAddr = u64; ///< Represents a pointer in the device specific virtual address space. | ||
| 48 | using PAddr = u64; ///< Represents a pointer in the ARM11 physical address space. | 49 | using PAddr = u64; ///< Represents a pointer in the ARM11 physical address space. |
| 49 | using GPUVAddr = u64; ///< Represents a pointer in the GPU virtual address space. | 50 | using GPUVAddr = u64; ///< Represents a pointer in the GPU virtual address space. |
| 50 | 51 | ||
diff --git a/src/common/fs/file.h b/src/common/fs/file.h index 167c4d826..2e2396075 100644 --- a/src/common/fs/file.h +++ b/src/common/fs/file.h | |||
| @@ -37,7 +37,7 @@ void OpenFileStream(FileStream& file_stream, const std::filesystem::path& path, | |||
| 37 | template <typename FileStream, typename Path> | 37 | template <typename FileStream, typename Path> |
| 38 | void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::openmode open_mode) { | 38 | void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::openmode open_mode) { |
| 39 | if constexpr (IsChar<typename Path::value_type>) { | 39 | if constexpr (IsChar<typename Path::value_type>) { |
| 40 | file_stream.open(ToU8String(path), open_mode); | 40 | file_stream.open(std::filesystem::path{ToU8String(path)}, open_mode); |
| 41 | } else { | 41 | } else { |
| 42 | file_stream.open(std::filesystem::path{path}, open_mode); | 42 | file_stream.open(std::filesystem::path{path}, open_mode); |
| 43 | } | 43 | } |
diff --git a/src/common/memory_detect.h b/src/common/memory_detect.h index a345e6d28..c8f239aed 100644 --- a/src/common/memory_detect.h +++ b/src/common/memory_detect.h | |||
| @@ -18,4 +18,4 @@ struct MemoryInfo { | |||
| 18 | */ | 18 | */ |
| 19 | [[nodiscard]] const MemoryInfo& GetMemInfo(); | 19 | [[nodiscard]] const MemoryInfo& GetMemInfo(); |
| 20 | 20 | ||
| 21 | } // namespace Common \ No newline at end of file | 21 | } // namespace Common |
diff --git a/src/common/overflow.h b/src/common/overflow.h index 44d8e7e73..e184ead95 100644 --- a/src/common/overflow.h +++ b/src/common/overflow.h | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <algorithm> | ||
| 6 | #include <type_traits> | 7 | #include <type_traits> |
| 7 | #include "bit_cast.h" | 8 | #include "bit_cast.h" |
| 8 | 9 | ||
| @@ -19,4 +20,21 @@ inline T WrappingAdd(T lhs, T rhs) { | |||
| 19 | return BitCast<T>(lhs_u + rhs_u); | 20 | return BitCast<T>(lhs_u + rhs_u); |
| 20 | } | 21 | } |
| 21 | 22 | ||
| 23 | template <typename T> | ||
| 24 | requires(std::is_integral_v<T> && std::is_signed_v<T>) | ||
| 25 | inline bool CanAddWithoutOverflow(T lhs, T rhs) { | ||
| 26 | #ifdef _MSC_VER | ||
| 27 | if (lhs >= 0 && rhs >= 0) { | ||
| 28 | return WrappingAdd(lhs, rhs) >= std::max(lhs, rhs); | ||
| 29 | } else if (lhs < 0 && rhs < 0) { | ||
| 30 | return WrappingAdd(lhs, rhs) <= std::min(lhs, rhs); | ||
| 31 | } else { | ||
| 32 | return true; | ||
| 33 | } | ||
| 34 | #else | ||
| 35 | T res; | ||
| 36 | return !__builtin_add_overflow(lhs, rhs, &res); | ||
| 37 | #endif | ||
| 38 | } | ||
| 39 | |||
| 22 | } // namespace Common | 40 | } // namespace Common |
diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp index 166dc3dce..85dc18c11 100644 --- a/src/common/page_table.cpp +++ b/src/common/page_table.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include "common/page_table.h" | 4 | #include "common/page_table.h" |
| 5 | #include "common/scope_exit.h" | ||
| 5 | 6 | ||
| 6 | namespace Common { | 7 | namespace Common { |
| 7 | 8 | ||
| @@ -11,29 +12,10 @@ PageTable::~PageTable() noexcept = default; | |||
| 11 | 12 | ||
| 12 | bool PageTable::BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context, | 13 | bool PageTable::BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context, |
| 13 | Common::ProcessAddress address) const { | 14 | Common::ProcessAddress address) const { |
| 14 | // Setup invalid defaults. | 15 | out_context->next_offset = GetInteger(address); |
| 15 | out_entry->phys_addr = 0; | 16 | out_context->next_page = address / page_size; |
| 16 | out_entry->block_size = page_size; | ||
| 17 | out_context->next_page = 0; | ||
| 18 | |||
| 19 | // Validate that we can read the actual entry. | ||
| 20 | const auto page = address / page_size; | ||
| 21 | if (page >= backing_addr.size()) { | ||
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | // Validate that the entry is mapped. | ||
| 26 | const auto phys_addr = backing_addr[page]; | ||
| 27 | if (phys_addr == 0) { | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 | 17 | ||
| 31 | // Populate the results. | 18 | return this->ContinueTraversal(out_entry, out_context); |
| 32 | out_entry->phys_addr = phys_addr + GetInteger(address); | ||
| 33 | out_context->next_page = page + 1; | ||
| 34 | out_context->next_offset = GetInteger(address) + page_size; | ||
| 35 | |||
| 36 | return true; | ||
| 37 | } | 19 | } |
| 38 | 20 | ||
| 39 | bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const { | 21 | bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const { |
| @@ -41,6 +23,12 @@ bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* c | |||
| 41 | out_entry->phys_addr = 0; | 23 | out_entry->phys_addr = 0; |
| 42 | out_entry->block_size = page_size; | 24 | out_entry->block_size = page_size; |
| 43 | 25 | ||
| 26 | // Regardless of whether the page was mapped, advance on exit. | ||
| 27 | SCOPE_EXIT({ | ||
| 28 | context->next_page += 1; | ||
| 29 | context->next_offset += page_size; | ||
| 30 | }); | ||
| 31 | |||
| 44 | // Validate that we can read the actual entry. | 32 | // Validate that we can read the actual entry. |
| 45 | const auto page = context->next_page; | 33 | const auto page = context->next_page; |
| 46 | if (page >= backing_addr.size()) { | 34 | if (page >= backing_addr.size()) { |
| @@ -55,8 +43,6 @@ bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* c | |||
| 55 | 43 | ||
| 56 | // Populate the results. | 44 | // Populate the results. |
| 57 | out_entry->phys_addr = phys_addr + context->next_offset; | 45 | out_entry->phys_addr = phys_addr + context->next_offset; |
| 58 | context->next_page = page + 1; | ||
| 59 | context->next_offset += page_size; | ||
| 60 | 46 | ||
| 61 | return true; | 47 | return true; |
| 62 | } | 48 | } |
diff --git a/src/common/settings.h b/src/common/settings.h index 07dba53ab..16749ab68 100644 --- a/src/common/settings.h +++ b/src/common/settings.h | |||
| @@ -419,9 +419,16 @@ struct Values { | |||
| 419 | linkage, false, "custom_rtc_enabled", Category::System, Specialization::Paired, true, true}; | 419 | linkage, false, "custom_rtc_enabled", Category::System, Specialization::Paired, true, true}; |
| 420 | SwitchableSetting<s64> custom_rtc{ | 420 | SwitchableSetting<s64> custom_rtc{ |
| 421 | linkage, 0, "custom_rtc", Category::System, Specialization::Time, | 421 | linkage, 0, "custom_rtc", Category::System, Specialization::Time, |
| 422 | true, true, &custom_rtc_enabled}; | 422 | false, true, &custom_rtc_enabled}; |
| 423 | // Set on game boot, reset on stop. Seconds difference between current time and `custom_rtc` | 423 | SwitchableSetting<s64, true> custom_rtc_offset{linkage, |
| 424 | s64 custom_rtc_differential; | 424 | 0, |
| 425 | std::numeric_limits<int>::min(), | ||
| 426 | std::numeric_limits<int>::max(), | ||
| 427 | "custom_rtc_offset", | ||
| 428 | Category::System, | ||
| 429 | Specialization::Countable, | ||
| 430 | true, | ||
| 431 | true}; | ||
| 425 | SwitchableSetting<bool> rng_seed_enabled{ | 432 | SwitchableSetting<bool> rng_seed_enabled{ |
| 426 | linkage, false, "rng_seed_enabled", Category::System, Specialization::Paired, true, true}; | 433 | linkage, false, "rng_seed_enabled", Category::System, Specialization::Paired, true, true}; |
| 427 | SwitchableSetting<u32> rng_seed{ | 434 | SwitchableSetting<u32> rng_seed{ |
diff --git a/src/common/settings_common.cpp b/src/common/settings_common.cpp index 5960b78aa..b90e3509c 100644 --- a/src/common/settings_common.cpp +++ b/src/common/settings_common.cpp | |||
| @@ -35,7 +35,7 @@ bool BasicSetting::Save() const { | |||
| 35 | return save; | 35 | return save; |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | bool BasicSetting::RuntimeModfiable() const { | 38 | bool BasicSetting::RuntimeModifiable() const { |
| 39 | return runtime_modifiable; | 39 | return runtime_modifiable; |
| 40 | } | 40 | } |
| 41 | 41 | ||
diff --git a/src/common/settings_common.h b/src/common/settings_common.h index 1a290ad77..987489e8a 100644 --- a/src/common/settings_common.h +++ b/src/common/settings_common.h | |||
| @@ -186,7 +186,7 @@ public: | |||
| 186 | /** | 186 | /** |
| 187 | * @returns true if the current setting can be changed while the guest is running. | 187 | * @returns true if the current setting can be changed while the guest is running. |
| 188 | */ | 188 | */ |
| 189 | [[nodiscard]] bool RuntimeModfiable() const; | 189 | [[nodiscard]] bool RuntimeModifiable() const; |
| 190 | 190 | ||
| 191 | /** | 191 | /** |
| 192 | * @returns A unique number corresponding to the setting. | 192 | * @returns A unique number corresponding to the setting. |
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 69e728a9d..f77df604f 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -88,7 +88,17 @@ std::string FindSystemTimeZone() { | |||
| 88 | LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index); | 88 | LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index); |
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); | 91 | |
| 92 | // For some reason the Etc/GMT times are reversed. GMT+6 contains -21600 as its offset, | ||
| 93 | // -6 hours instead of +6 hours, so these signs are purposefully reversed to fix it. | ||
| 94 | std::string postfix{""}; | ||
| 95 | if (hours > 0) { | ||
| 96 | postfix = fmt::format("-{:d}", std::abs(hours)); | ||
| 97 | } else if (hours < 0) { | ||
| 98 | postfix = fmt::format("+{:d}", std::abs(hours)); | ||
| 99 | } | ||
| 100 | |||
| 101 | return fmt::format("Etc/GMT{:s}", postfix); | ||
| 92 | } | 102 | } |
| 93 | 103 | ||
| 94 | } // namespace Common::TimeZone | 104 | } // namespace Common::TimeZone |
diff --git a/src/common/uuid.h b/src/common/uuid.h index 7172ca165..81bfefbbb 100644 --- a/src/common/uuid.h +++ b/src/common/uuid.h | |||
| @@ -12,9 +12,8 @@ | |||
| 12 | namespace Common { | 12 | namespace Common { |
| 13 | 13 | ||
| 14 | struct UUID { | 14 | struct UUID { |
| 15 | std::array<u8, 0x10> uuid{}; | 15 | std::array<u8, 0x10> uuid; |
| 16 | 16 | ||
| 17 | /// Constructs an invalid UUID. | ||
| 18 | constexpr UUID() = default; | 17 | constexpr UUID() = default; |
| 19 | 18 | ||
| 20 | /// Constructs a UUID from a reference to a 128 bit array. | 19 | /// Constructs a UUID from a reference to a 128 bit array. |
| @@ -34,14 +33,6 @@ struct UUID { | |||
| 34 | */ | 33 | */ |
| 35 | explicit UUID(std::string_view uuid_string); | 34 | explicit UUID(std::string_view uuid_string); |
| 36 | 35 | ||
| 37 | ~UUID() = default; | ||
| 38 | |||
| 39 | constexpr UUID(const UUID&) noexcept = default; | ||
| 40 | constexpr UUID(UUID&&) noexcept = default; | ||
| 41 | |||
| 42 | constexpr UUID& operator=(const UUID&) noexcept = default; | ||
| 43 | constexpr UUID& operator=(UUID&&) noexcept = default; | ||
| 44 | |||
| 45 | /** | 36 | /** |
| 46 | * Returns whether the stored UUID is valid or not. | 37 | * Returns whether the stored UUID is valid or not. |
| 47 | * | 38 | * |
| @@ -121,6 +112,7 @@ struct UUID { | |||
| 121 | friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default; | 112 | friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default; |
| 122 | }; | 113 | }; |
| 123 | static_assert(sizeof(UUID) == 0x10, "UUID has incorrect size."); | 114 | static_assert(sizeof(UUID) == 0x10, "UUID has incorrect size."); |
| 115 | static_assert(std::is_trivial_v<UUID>); | ||
| 124 | 116 | ||
| 125 | /// An invalid UUID. This UUID has all its bytes set to 0. | 117 | /// An invalid UUID. This UUID has all its bytes set to 0. |
| 126 | constexpr UUID InvalidUUID = {}; | 118 | constexpr UUID InvalidUUID = {}; |
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 012fdc1e0..e14bf3e65 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp | |||
| @@ -18,42 +18,40 @@ namespace Common { | |||
| 18 | 18 | ||
| 19 | class StandardWallClock final : public WallClock { | 19 | class StandardWallClock final : public WallClock { |
| 20 | public: | 20 | public: |
| 21 | explicit StandardWallClock() : start_time{SteadyClock::Now()} {} | 21 | explicit StandardWallClock() {} |
| 22 | 22 | ||
| 23 | std::chrono::nanoseconds GetTimeNS() const override { | 23 | std::chrono::nanoseconds GetTimeNS() const override { |
| 24 | return SteadyClock::Now() - start_time; | 24 | return std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 25 | std::chrono::system_clock::now().time_since_epoch()); | ||
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | std::chrono::microseconds GetTimeUS() const override { | 28 | std::chrono::microseconds GetTimeUS() const override { |
| 28 | return static_cast<std::chrono::microseconds>(GetHostTicksElapsed() / NsToUsRatio::den); | 29 | return std::chrono::duration_cast<std::chrono::microseconds>( |
| 30 | std::chrono::system_clock::now().time_since_epoch()); | ||
| 29 | } | 31 | } |
| 30 | 32 | ||
| 31 | std::chrono::milliseconds GetTimeMS() const override { | 33 | std::chrono::milliseconds GetTimeMS() const override { |
| 32 | return static_cast<std::chrono::milliseconds>(GetHostTicksElapsed() / NsToMsRatio::den); | 34 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 35 | std::chrono::system_clock::now().time_since_epoch()); | ||
| 33 | } | 36 | } |
| 34 | 37 | ||
| 35 | u64 GetCNTPCT() const override { | 38 | s64 GetCNTPCT() const override { |
| 36 | return GetHostTicksElapsed() * NsToCNTPCTRatio::num / NsToCNTPCTRatio::den; | 39 | return GetUptime() * NsToCNTPCTRatio::num / NsToCNTPCTRatio::den; |
| 37 | } | 40 | } |
| 38 | 41 | ||
| 39 | u64 GetGPUTick() const override { | 42 | s64 GetGPUTick() const override { |
| 40 | return GetHostTicksElapsed() * NsToGPUTickRatio::num / NsToGPUTickRatio::den; | 43 | return GetUptime() * NsToGPUTickRatio::num / NsToGPUTickRatio::den; |
| 41 | } | 44 | } |
| 42 | 45 | ||
| 43 | u64 GetHostTicksNow() const override { | 46 | s64 GetUptime() const override { |
| 44 | return static_cast<u64>(SteadyClock::Now().time_since_epoch().count()); | 47 | return std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 45 | } | 48 | std::chrono::steady_clock::now().time_since_epoch()) |
| 46 | 49 | .count(); | |
| 47 | u64 GetHostTicksElapsed() const override { | ||
| 48 | return static_cast<u64>(GetTimeNS().count()); | ||
| 49 | } | 50 | } |
| 50 | 51 | ||
| 51 | bool IsNative() const override { | 52 | bool IsNative() const override { |
| 52 | return false; | 53 | return false; |
| 53 | } | 54 | } |
| 54 | |||
| 55 | private: | ||
| 56 | SteadyClock::time_point start_time; | ||
| 57 | }; | 55 | }; |
| 58 | 56 | ||
| 59 | std::unique_ptr<WallClock> CreateOptimalClock() { | 57 | std::unique_ptr<WallClock> CreateOptimalClock() { |
diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index f45d3d8c5..3a0c43909 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h | |||
| @@ -29,16 +29,13 @@ public: | |||
| 29 | virtual std::chrono::milliseconds GetTimeMS() const = 0; | 29 | virtual std::chrono::milliseconds GetTimeMS() const = 0; |
| 30 | 30 | ||
| 31 | /// @returns The guest CNTPCT ticks since the construction of this clock. | 31 | /// @returns The guest CNTPCT ticks since the construction of this clock. |
| 32 | virtual u64 GetCNTPCT() const = 0; | 32 | virtual s64 GetCNTPCT() const = 0; |
| 33 | 33 | ||
| 34 | /// @returns The guest GPU ticks since the construction of this clock. | 34 | /// @returns The guest GPU ticks since the construction of this clock. |
| 35 | virtual u64 GetGPUTick() const = 0; | 35 | virtual s64 GetGPUTick() const = 0; |
| 36 | 36 | ||
| 37 | /// @returns The raw host timer ticks since an indeterminate epoch. | 37 | /// @returns The raw host timer ticks since an indeterminate epoch. |
| 38 | virtual u64 GetHostTicksNow() const = 0; | 38 | virtual s64 GetUptime() const = 0; |
| 39 | |||
| 40 | /// @returns The raw host timer ticks since the construction of this clock. | ||
| 41 | virtual u64 GetHostTicksElapsed() const = 0; | ||
| 42 | 39 | ||
| 43 | /// @returns Whether the clock directly uses the host's hardware clock. | 40 | /// @returns Whether the clock directly uses the host's hardware clock. |
| 44 | virtual bool IsNative() const = 0; | 41 | virtual bool IsNative() const = 0; |
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 7d2a26bd9..d2d27fafe 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp | |||
| @@ -8,39 +8,35 @@ | |||
| 8 | namespace Common::X64 { | 8 | namespace Common::X64 { |
| 9 | 9 | ||
| 10 | NativeClock::NativeClock(u64 rdtsc_frequency_) | 10 | NativeClock::NativeClock(u64 rdtsc_frequency_) |
| 11 | : start_ticks{FencedRDTSC()}, rdtsc_frequency{rdtsc_frequency_}, | 11 | : rdtsc_frequency{rdtsc_frequency_}, ns_rdtsc_factor{GetFixedPoint64Factor(NsRatio::den, |
| 12 | ns_rdtsc_factor{GetFixedPoint64Factor(NsRatio::den, rdtsc_frequency)}, | 12 | rdtsc_frequency)}, |
| 13 | us_rdtsc_factor{GetFixedPoint64Factor(UsRatio::den, rdtsc_frequency)}, | 13 | us_rdtsc_factor{GetFixedPoint64Factor(UsRatio::den, rdtsc_frequency)}, |
| 14 | ms_rdtsc_factor{GetFixedPoint64Factor(MsRatio::den, rdtsc_frequency)}, | 14 | ms_rdtsc_factor{GetFixedPoint64Factor(MsRatio::den, rdtsc_frequency)}, |
| 15 | cntpct_rdtsc_factor{GetFixedPoint64Factor(CNTFRQ, rdtsc_frequency)}, | 15 | cntpct_rdtsc_factor{GetFixedPoint64Factor(CNTFRQ, rdtsc_frequency)}, |
| 16 | gputick_rdtsc_factor{GetFixedPoint64Factor(GPUTickFreq, rdtsc_frequency)} {} | 16 | gputick_rdtsc_factor{GetFixedPoint64Factor(GPUTickFreq, rdtsc_frequency)} {} |
| 17 | 17 | ||
| 18 | std::chrono::nanoseconds NativeClock::GetTimeNS() const { | 18 | std::chrono::nanoseconds NativeClock::GetTimeNS() const { |
| 19 | return std::chrono::nanoseconds{MultiplyHigh(GetHostTicksElapsed(), ns_rdtsc_factor)}; | 19 | return std::chrono::nanoseconds{MultiplyHigh(GetUptime(), ns_rdtsc_factor)}; |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | std::chrono::microseconds NativeClock::GetTimeUS() const { | 22 | std::chrono::microseconds NativeClock::GetTimeUS() const { |
| 23 | return std::chrono::microseconds{MultiplyHigh(GetHostTicksElapsed(), us_rdtsc_factor)}; | 23 | return std::chrono::microseconds{MultiplyHigh(GetUptime(), us_rdtsc_factor)}; |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | std::chrono::milliseconds NativeClock::GetTimeMS() const { | 26 | std::chrono::milliseconds NativeClock::GetTimeMS() const { |
| 27 | return std::chrono::milliseconds{MultiplyHigh(GetHostTicksElapsed(), ms_rdtsc_factor)}; | 27 | return std::chrono::milliseconds{MultiplyHigh(GetUptime(), ms_rdtsc_factor)}; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | u64 NativeClock::GetCNTPCT() const { | 30 | s64 NativeClock::GetCNTPCT() const { |
| 31 | return MultiplyHigh(GetHostTicksElapsed(), cntpct_rdtsc_factor); | 31 | return MultiplyHigh(GetUptime(), cntpct_rdtsc_factor); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | u64 NativeClock::GetGPUTick() const { | 34 | s64 NativeClock::GetGPUTick() const { |
| 35 | return MultiplyHigh(GetHostTicksElapsed(), gputick_rdtsc_factor); | 35 | return MultiplyHigh(GetUptime(), gputick_rdtsc_factor); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | u64 NativeClock::GetHostTicksNow() const { | 38 | s64 NativeClock::GetUptime() const { |
| 39 | return FencedRDTSC(); | 39 | return static_cast<s64>(FencedRDTSC()); |
| 40 | } | ||
| 41 | |||
| 42 | u64 NativeClock::GetHostTicksElapsed() const { | ||
| 43 | return FencedRDTSC() - start_ticks; | ||
| 44 | } | 40 | } |
| 45 | 41 | ||
| 46 | bool NativeClock::IsNative() const { | 42 | bool NativeClock::IsNative() const { |
diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 334415eff..b2629b031 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h | |||
| @@ -17,18 +17,15 @@ public: | |||
| 17 | 17 | ||
| 18 | std::chrono::milliseconds GetTimeMS() const override; | 18 | std::chrono::milliseconds GetTimeMS() const override; |
| 19 | 19 | ||
| 20 | u64 GetCNTPCT() const override; | 20 | s64 GetCNTPCT() const override; |
| 21 | 21 | ||
| 22 | u64 GetGPUTick() const override; | 22 | s64 GetGPUTick() const override; |
| 23 | 23 | ||
| 24 | u64 GetHostTicksNow() const override; | 24 | s64 GetUptime() const override; |
| 25 | |||
| 26 | u64 GetHostTicksElapsed() const override; | ||
| 27 | 25 | ||
| 28 | bool IsNative() const override; | 26 | bool IsNative() const override; |
| 29 | 27 | ||
| 30 | private: | 28 | private: |
| 31 | u64 start_ticks; | ||
| 32 | u64 rdtsc_frequency; | 29 | u64 rdtsc_frequency; |
| 33 | 30 | ||
| 34 | u64 ns_rdtsc_factor; | 31 | u64 ns_rdtsc_factor; |