1
0
Fork 0
mirror of https://github.com/anrieff/libcpuid synced 2024-11-10 22:59:13 +00:00

The logic behind the big IFDEF at msrdriver.c was wrong. You could compile a 32-bit build and run it on 64-bit windows, and in this case you will still need the 64-bit driver. So the platform detection is made runtime, and the correct driver for the platform is extracted on demand.

git-svn-id: https://svn.code.sf.net/p/libcpuid/code/HEAD/libcpuid@70 3b4be424-7ac5-41d7-8526-f4ddcb85d872
This commit is contained in:
Veselin Georgiev 2009-09-30 15:27:39 +00:00
parent e96082c67f
commit 0bd7a6d83f
3 changed files with 29 additions and 18 deletions

View file

@ -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;

View file

@ -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

View file

@ -52,8 +52,10 @@ int cpu_msr_driver_close(struct msr_driver_t* driver)
#else /* _WIN32 */
#include <windows.h>
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;
}