I encountered problems while working on a project, specifically when dealing with intrinsic functions like __rdtsc, __readmsr, and __writemsr in Visual Studio. I noticed that using these functions was causing unexpected stability issues in my system, including my mouse becoming unresponsive. After some experimentation, I found a solution by manually coding the equivalent functionality in assembly.
Problem:
When using intrinsic functions to perform certain 64-bit operations, my system exhibited stability issues. This was particularly evident when trying to read from MSRs or using the timestamp counter.
Attempted Solutions:
- Checked for correct privilege levels and context for MSR operations.
- Ensured the latest updates for my development environment.
- Searched for known issues with these intrinsics or potential bugs in the compiler.
Working Solution:
I switched to using manual assembly within my code, specifically with the rdmsr instruction followed by shl and or operations to correctly handle the 64-bit result. This approach resolved the stability issues entirely.
Code Snippet:
rdmsr ; Read MSR into EDX:EAX
shl rdx, 32 ; Shift the high 32 bits left
or rax, rdx ; Combine the high and low parts
Has anyone else experienced similar issues with intrinsic functions in Visual Studio or other environments?
Are there known limitations or considerations with these intrinsics that I might have overlooked?
Could this solution imply a deeper underlying issue with my system's configuration or the compiler?
Environment:
- Compiler: visual studio 2019
- OS: Windows 10 version 20H2 build 19042.804
- Hardware: cpu AMD K10
Minimal Reproducible Example:
unsigned __int64 AmdP0Freq() {
unsigned __int64 msr = __readmsr(0xc0010064); // MSR address for P0 frequency
int r = msr & 0x1f;
__int64 hz = ((r + 0x10) * 100000000ll) / (1 << ((msr >> 6) & 0x07));
return hz;
}