首页 > 解决方案 > 无法在 GCC 的 VC++ 版本中获得预期结果

问题描述

我需要将用 gcc 编写的 vm 检测代码转换为 vc++

这是代码

static inline unsigned long long rdtsc_diff_vmexit() {
unsigned long long ret, ret2;
unsigned eax, edx;
__asm__ volatile("rdtsc" : "=a" (eax), "=d" (edx));
ret  = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
/* vm exit forced here. it uses: eax = 0; cpuid; */
__asm__ volatile("cpuid" : /* no output */ : "a"(0x00));
/**/
__asm__ volatile("rdtsc" : "=a" (eax), "=d" (edx));
ret2  = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
return ret2 - ret;}

我找到了一些在 vc++ 中执行相同操作的示例,如下所示

BOOL rdtsc_diff_vmexit(){
ULONGLONG tsc1 = 0;


ULONGLONG tsc2 = 0;
ULONGLONG avg = 0;
INT cpuInfo[4] = {};

// Try this 10 times in case of small fluctuations
for (INT i = 0; i < 10; i++)
{
    tsc1 = __rdtsc();
    __cpuid(cpuInfo, 0);
    tsc2 = __rdtsc();

    // Get the delta of the two RDTSC
    avg += (tsc2 - tsc1);
}

// We repeated the process 10 times so we make sure our check is as much reliable as we can
avg = avg / 10;
return (avg < 1000 && avg > 0) ? FALSE : TRUE;}

但问题是如果在虚拟框中运行 GCC 版本返回 true,而 vc++ 返回 false,请告诉我有什么问题。想知道 gcc asm 代码是否可以为 VC++ 重写?

标签: c++visual-c++

解决方案


推荐阅读