This is an attempt to upstream a set of Proton patches that correct the value of HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\*\~MHz to not be the maximum frequency of the processor, but the calibrated TSC.
Some games like Horizon Zero Dawn and most likely some more obscure benchmark/profiling tools use this as indicated in this forum post: https://community.osr.com/discussion/288014/how-to-find-out-tsc-frequency
The last comment also suggests querying the above registry key for the TSC.
To my understanding the calibration code has been successfully in use for some time now without any known issues.
I tried to be as faithful to the original history as possible with separating out my changes into their own commits.
If everything should be squashed to be prettier, just let me know!
FYI @rbernon
-- v3: wineboot: Compute and write the TSC frequency to registry ~Mhz.
From: Rémi Bernon rbernon@codeweavers.com
In HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
Some games such as Horizon Zero Dawn use this registry value to correlate values from rtdsc to real time.
Returning the nominal/maximum cpu frequency here causes the game to run in slow motion as it may not match the tsc frequency of the processor.
Based on patches from Arkadiusz Hiler and Joshua Ashton. --- programs/wineboot/wineboot.c | 140 +++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 5 deletions(-)
diff --git a/programs/wineboot/wineboot.c b/programs/wineboot/wineboot.c index 728c41fffa9..00b19d046fe 100644 --- a/programs/wineboot/wineboot.c +++ b/programs/wineboot/wineboot.c @@ -82,6 +82,8 @@
WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
+#define TICKSPERSEC 10000000 + extern BOOL shutdown_close_windows( BOOL force ); extern BOOL shutdown_all_desktops( BOOL force ); extern void kill_processes( BOOL kill_desktop ); @@ -240,15 +242,138 @@ static void initialize_xstate_features(struct _KUSER_SHARED_DATA *data) TRACE("XSAVE feature 2 %#x, %#x, %#x, %#x.\n", regs[0], regs[1], regs[2], regs[3]); }
+static UINT64 read_tsc_frequency( BOOL has_rdtscp ) +{ + UINT64 freq = 0; + LONGLONG time0, time1, tsc0, tsc1, tsc2, tsc3, freq0, freq1, error; + unsigned int aux; + UINT retries = 50; + int regs[4]; + + do + { + if (has_rdtscp) + { + tsc0 = __rdtscp( &aux ); + time0 = RtlGetSystemTimePrecise(); + tsc1 = __rdtscp( &aux ); + Sleep( 1 ); + tsc2 = __rdtscp( &aux ); + time1 = RtlGetSystemTimePrecise(); + tsc3 = __rdtscp( &aux ); + } + else + { + tsc0 = __rdtsc(); __cpuid( regs, 0 ); + time0 = RtlGetSystemTimePrecise(); + tsc1 = __rdtsc(); __cpuid( regs, 0 ); + Sleep(1); + tsc2 = __rdtsc(); __cpuid( regs, 0 ); + time1 = RtlGetSystemTimePrecise(); + tsc3 = __rdtsc(); __cpuid( regs, 0 ); + } + + freq0 = (tsc2 - tsc0) * 10000000 / (time1 - time0); + freq1 = (tsc3 - tsc1) * 10000000 / (time1 - time0); + error = llabs( (freq1 - freq0) * 1000000 / min( freq1, freq0 ) ); + } + while (error > 500 && --retries); + + if (!retries) WARN( "TSC frequency calibration failed, unstable TSC?\n" ); + else + { + freq = (freq0 + freq1) / 2; + TRACE( "TSC frequency calibration complete, found %I64u Hz\n", freq ); + } + + return freq; +} + +static BOOL is_tsc_trusted_by_the_kernel(void) +{ + char buf[4] = {}; + DWORD num_read; + HANDLE handle; + BOOL ret = TRUE; + + /* Darwin for x86-64 uses the TSC internally for timekeeping, so it can always + * be trusted. + * For BSDs there seems to be no unified interface to query TSC quality. + * If there is a sysfs entry with clocksource information, use it to check though. */ + handle = CreateFileA( "\??\unix\sys\bus\clocksource\devices\clocksource0\current_clocksource", + GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 ); + if (handle == INVALID_HANDLE_VALUE) return TRUE; + + if (ReadFile( handle, buf, sizeof(buf) - 1, &num_read, NULL ) && strcmp( "tsc", buf )) + ret = FALSE; + + CloseHandle( handle ); + return ret; +} + +static void initialize_qpc_features( struct _KUSER_SHARED_DATA *data, UINT64 *tsc_frequency ) +{ + BOOL has_rdtscp = FALSE; + int regs[4]; + + data->QpcBypassEnabled = 0; + data->QpcFrequency = TICKSPERSEC; + data->QpcShift = 0; + data->QpcBias = 0; + *tsc_frequency = 0; + + if (!is_tsc_trusted_by_the_kernel()) + { + WARN( "Failed to compute TSC frequency, not trusted by the kernel.\n" ); + return; + } + + if (!data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE]) + { + WARN( "Failed to compute TSC frequency, RDTSC instruction not supported.\n" ); + return; + } + + __cpuid( regs, 0x80000000 ); + if (regs[0] < 0x80000007) + { + WARN( "Failed to compute TSC frequency, unable to check invariant TSC.\n" ); + return; + } + + /* check for invariant tsc bit */ + __cpuid( regs, 0x80000007 ); + if (!(regs[3] & (1 << 8))) + { + WARN( "Failed to compute TSC frequency, no invariant TSC.\n" ); + return; + } + + /* check for rdtscp support bit */ + __cpuid( regs, 0x80000001 ); + if ((regs[3] & (1 << 27))) has_rdtscp = TRUE; + + *tsc_frequency = read_tsc_frequency( has_rdtscp ); +} + #else
static void initialize_xstate_features(struct _KUSER_SHARED_DATA *data) { }
+static void initialize_qpc_features( struct _KUSER_SHARED_DATA *data, UINT64 *tsc_frequency ) +{ + data->QpcBypassEnabled = 0; + data->QpcFrequency = TICKSPERSEC; + data->QpcShift = 0; + data->QpcBias = 0; + *tsc_frequency = 0; +} + #endif
-static void create_user_shared_data(void) +static void create_user_shared_data( UINT64 *tsc_frequency ) { struct _KUSER_SHARED_DATA *data; RTL_OSVERSIONINFOEXW version; @@ -367,6 +492,7 @@ static void create_user_shared_data(void) data->ActiveGroupCount = 1;
initialize_xstate_features( data ); + initialize_qpc_features( data, tsc_frequency );
UnmapViewOfFile( data ); } @@ -659,7 +785,7 @@ done: }
/* create the volatile hardware registry keys */ -static void create_hardware_registry_keys(void) +static void create_hardware_registry_keys( UINT64 tsc_frequency ) { unsigned int i; HKEY hkey, system_key, cpu_key, fpu_key; @@ -736,13 +862,16 @@ static void create_hardware_registry_keys(void) if (!RegCreateKeyExW( cpu_key, numW, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL )) { + DWORD tsc_freq_mhz = (DWORD)(tsc_frequency / 1000000ull); /* Hz -> Mhz */ + if (!tsc_freq_mhz) tsc_freq_mhz = power_info[i].MaxMhz; + RegSetValueExW( hkey, L"FeatureSet", 0, REG_DWORD, (BYTE *)&sci.ProcessorFeatureBits, sizeof(DWORD) ); set_reg_value( hkey, L"Identifier", id ); /* TODO: report ARM properly */ RegSetValueExA( hkey, "ProcessorNameString", 0, REG_SZ, (const BYTE *)name_buffer, strlen( (char *)name_buffer ) + 1 ); set_reg_value( hkey, L"VendorIdentifier", vendorid ); - RegSetValueExW( hkey, L"~MHz", 0, REG_DWORD, (BYTE *)&power_info[i].MaxMhz, sizeof(DWORD) ); + RegSetValueExW( hkey, L"~MHz", 0, REG_DWORD, (BYTE *)&tsc_freq_mhz, sizeof(DWORD) ); RegCloseKey( hkey ); } if (sci.ProcessorArchitecture != PROCESSOR_ARCHITECTURE_ARM && @@ -1627,6 +1756,7 @@ int __cdecl main( int argc, char *argv[] ) BOOL end_session, force, init, kill, restart, shutdown, update; HANDLE event; OBJECT_ATTRIBUTES attr; + UINT64 tsc_frequency = 0; UNICODE_STRING nameW = RTL_CONSTANT_STRING( L"\KernelObjects\__wineboot_event" ); BOOL is_wow64;
@@ -1712,8 +1842,8 @@ int __cdecl main( int argc, char *argv[] )
ResetEvent( event ); /* in case this is a restart */
- create_user_shared_data(); - create_hardware_registry_keys(); + create_user_shared_data( &tsc_frequency ); + create_hardware_registry_keys( tsc_frequency ); create_dynamic_registry_keys(); create_environment_registry_keys(); create_computer_name_keys();
Thanks for the feedback, I squashed the commits and edited the message.
Rémi Bernon (@rbernon) commented about programs/wineboot/wineboot.c:
- return freq;
+}
+static BOOL is_tsc_trusted_by_the_kernel(void) +{
- char buf[4] = {};
- DWORD num_read;
- HANDLE handle;
- BOOL ret = TRUE;
- /* Darwin for x86-64 uses the TSC internally for timekeeping, so it can always
* be trusted.
* For BSDs there seems to be no unified interface to query TSC quality.
* If there is a sysfs entry with clocksource information, use it to check though. */
- handle = CreateFileA( "\??\unix\sys\bus\clocksource\devices\clocksource0\current_clocksource",
```suggestion:-0+0 handle = CreateFileW( L"\??\unix\sys\bus\clocksource\devices\clocksource0\current_clocksource", ```
We use W versions everywhere. Not that it matters much here but it's more consistent.
Rémi Bernon (@rbernon) commented about programs/wineboot/wineboot.c:
- }
- while (error > 500 && --retries);
- if (!retries) WARN( "TSC frequency calibration failed, unstable TSC?\n" );
- else
- {
freq = (freq0 + freq1) / 2;
TRACE( "TSC frequency calibration complete, found %I64u Hz\n", freq );
- }
- return freq;
+}
+static BOOL is_tsc_trusted_by_the_kernel(void) +{
- char buf[4] = {};
```suggestion:-0+0 char buf[4] = {0}; ```
Rémi Bernon (@rbernon) commented about programs/wineboot/wineboot.c:
- if (ReadFile( handle, buf, sizeof(buf) - 1, &num_read, NULL ) && strcmp( "tsc", buf ))
ret = FALSE;
- CloseHandle( handle );
- return ret;
+}
+static void initialize_qpc_features( struct _KUSER_SHARED_DATA *data, UINT64 *tsc_frequency ) +{
- BOOL has_rdtscp = FALSE;
- int regs[4];
- data->QpcBypassEnabled = 0;
- data->QpcFrequency = TICKSPERSEC;
- data->QpcShift = 0;
- data->QpcBias = 0;
I don't think we really need these. At least not in this MR, it was used in an attempt at implementing Qpc bypass, but that ended up complicated.
Rémi Bernon (@rbernon) commented about programs/wineboot/wineboot.c:
- *tsc_frequency = read_tsc_frequency( has_rdtscp );
+}
#else
static void initialize_xstate_features(struct _KUSER_SHARED_DATA *data) { }
+static void initialize_qpc_features( struct _KUSER_SHARED_DATA *data, UINT64 *tsc_frequency ) +{
- data->QpcBypassEnabled = 0;
- data->QpcFrequency = TICKSPERSEC;
- data->QpcShift = 0;
- data->QpcBias = 0;
Same, this could probably be dropped. The data parameter would also not be needed.
Then, maybe a FIXME message would be appropriate to indicate that a better frequency measurement needs to be implemented.
Last nitpick, about the contribution workflow, as you have kept the original commit author and it's not you, you'll need to add yourself as a reviewer here (which may require requesting for access to the https://gitlab.winehq.org/wine/wine project), and approve the MR.