diff --git a/libcpuid/embed_drivers.cpp b/libcpuid/embed_drivers.cpp index 5eec770..38d7470 100644 --- a/libcpuid/embed_drivers.cpp +++ b/libcpuid/embed_drivers.cpp @@ -46,10 +46,10 @@ bool read_source(const char* filename) return true; } -void print_image(FILE* f, const char* image, int size) +void print_image(FILE* f, const char* arch, const char* image, int size) { - fprintf(f, "int cc_driver_code_size = %d;\n", size); - fprintf(f, "uint8_t cc_driver_code[%d] = {", size); + fprintf(f, "int cc_%sdriver_code_size = %d;\n", arch, size); + fprintf(f, "uint8_t cc_%sdriver_code[%d] = {", arch, size); for (int i = 0; i < size; i++) { if (i % 18 == 0) fprintf(f, "\n\t"); fprintf(f, "0x%02x,", (unsigned) (unsigned char) image[i]); @@ -76,11 +76,8 @@ int main(void) if (on) fprintf(f, "%s\n", source[i].c_str()); if (source[i] == "//begin {") { on = false; - fprintf(f, "#ifdef PLATFORM_X86\n"); - print_image(f, images[0], sizes[0]); - fprintf(f, "#else\n"); - print_image(f, images[1], sizes[1]); - fprintf(f, "#endif // PLATFORM_X86\n"); + print_image(f, "x86", images[0], sizes[0]); + print_image(f, "x64", images[1], sizes[1]); } } return 0; diff --git a/libcpuid/msrdriver.c b/libcpuid/msrdriver.c index b5440ce..870fdeb 100644 --- a/libcpuid/msrdriver.c +++ b/libcpuid/msrdriver.c @@ -40,9 +40,8 @@ #ifdef _WIN32 #include "asm-bits.h" //begin { -#ifdef PLATFORM_X86 -int cc_driver_code_size = 4608; -uint8_t cc_driver_code[4608] = { +int cc_x86driver_code_size = 4608; +uint8_t cc_x86driver_code[4608] = { 0x4d,0x5a,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xff,0xff,0x00,0x00,0xb8,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -300,9 +299,8 @@ uint8_t cc_driver_code[4608] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; -#else -int cc_driver_code_size = 5120; -uint8_t cc_driver_code[5120] = { +int cc_x64driver_code_size = 5120; +uint8_t cc_x64driver_code[5120] = { 0x4d,0x5a,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xff,0xff,0x00,0x00,0xb8,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -589,6 +587,5 @@ uint8_t cc_driver_code[5120] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; -#endif // PLATFORM_X86 //} end #endif // _WIN32 diff --git a/libcpuid/rdmsr.c b/libcpuid/rdmsr.c index ab7d635..b7d2130 100644 --- a/libcpuid/rdmsr.c +++ b/libcpuid/rdmsr.c @@ -52,8 +52,10 @@ int cpu_msr_driver_close(struct msr_driver_t* driver) #else /* _WIN32 */ #include -extern uint8_t cc_driver_code[]; -extern int cc_driver_code_size; +extern uint8_t cc_x86driver_code[]; +extern int cc_x86driver_code_size; +extern uint8_t cc_x64driver_code[]; +extern int cc_x64driver_code_size; struct msr_driver_t { char driver_path[MAX_PATH + 1]; @@ -107,6 +109,18 @@ static int rdmsr_supported(void) return id->flags[CPU_FEATURE_MSR]; } +typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); +static BOOL is_running_x64(void) +{ + BOOL bIsWow64 = FALSE; + + LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(__TEXT("kernel32")), "IsWow64Process"); + if(NULL != fnIsWow64Process) + fnIsWow64Process(GetCurrentProcess(), &bIsWow64); + return bIsWow64; +} + + static int extract_driver(struct msr_driver_t* driver) { FILE *f; @@ -115,7 +129,10 @@ static int extract_driver(struct msr_driver_t* driver) f = fopen(driver->driver_path, "wb"); if (!f) return 0; - fwrite(cc_driver_code, 1, cc_driver_code_size, f); + if (is_running_x64()) + fwrite(cc_x64driver_code, 1, cc_x64driver_code_size, f); + else + fwrite(cc_x86driver_code, 1, cc_x86driver_code_size, f); fclose(f); return 1; }