2019-07-07 18:03:45 +00:00
|
|
|
/* This program can be used to convert CPUID Dump from http://instlatx64.atw.hu/
|
|
|
|
* To build: gcc convert_instlatx64.c -o convert_instlatx64
|
|
|
|
* To run: ./convert_instlatx64 <input file (from instlatx64)> <output file (without extension)>
|
|
|
|
* Then, you can use the create_test.py script to create the test file */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2020-05-03 15:09:08 +00:00
|
|
|
#include <string.h>
|
2020-11-14 12:44:42 +00:00
|
|
|
#include <unistd.h>
|
2020-05-09 20:46:13 +00:00
|
|
|
#include "../libcpuid/libcpuid.h"
|
2019-07-07 18:03:45 +00:00
|
|
|
|
2020-05-09 20:46:13 +00:00
|
|
|
#define LINE_LEN 60
|
2020-05-03 15:09:08 +00:00
|
|
|
#define FILENAME_LEN 128
|
2021-03-20 17:14:06 +00:00
|
|
|
#define LIB_DIR_LEN 1024
|
|
|
|
#define TOOL_LEN 2048
|
|
|
|
#define CMD_LEN 4096
|
2019-07-07 18:03:45 +00:00
|
|
|
#define EXT_CPUID 0x80000000
|
|
|
|
|
2020-05-09 20:46:13 +00:00
|
|
|
#define NOT_AFFECTED(__line) (__line[0] == 0xffffffff) && (__line[1] == 0xffffffff) && (__line[2] == 0xffffffff) && (__line[3] == 0xffffffff)
|
|
|
|
#define AFFECT_DATA_LINE(__line) __line[0] = eax ; __line[1] = ebx ; __line[2] = ecx ; __line[3] = edx
|
|
|
|
#define AFFECT_DATA_SUBLEAF(__array) __array[subleaf][0] = eax ; __array[subleaf][1] = ebx ; __array[subleaf][2] = ecx ; __array[subleaf][3] = edx
|
|
|
|
|
|
|
|
int serialize_raw_data(struct cpu_raw_data_t* data, FILE *f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CPUID_LEVEL; i++)
|
|
|
|
fprintf(f, "basic_cpuid[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->basic_cpuid[i][EAX], data->basic_cpuid[i][EBX],
|
|
|
|
data->basic_cpuid[i][ECX], data->basic_cpuid[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_EXT_CPUID_LEVEL; i++)
|
|
|
|
fprintf(f, "ext_cpuid[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->ext_cpuid[i][EAX], data->ext_cpuid[i][EBX],
|
|
|
|
data->ext_cpuid[i][ECX], data->ext_cpuid[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_INTELFN4_LEVEL; i++)
|
|
|
|
fprintf(f, "intel_fn4[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->intel_fn4[i][EAX], data->intel_fn4[i][EBX],
|
|
|
|
data->intel_fn4[i][ECX], data->intel_fn4[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_INTELFN11_LEVEL; i++)
|
|
|
|
fprintf(f, "intel_fn11[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->intel_fn11[i][EAX], data->intel_fn11[i][EBX],
|
|
|
|
data->intel_fn11[i][ECX], data->intel_fn11[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_INTELFN12H_LEVEL; i++)
|
|
|
|
fprintf(f, "intel_fn12h[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->intel_fn12h[i][EAX], data->intel_fn12h[i][EBX],
|
|
|
|
data->intel_fn12h[i][ECX], data->intel_fn12h[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_INTELFN14H_LEVEL; i++)
|
|
|
|
fprintf(f, "intel_fn14h[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->intel_fn14h[i][EAX], data->intel_fn14h[i][EBX],
|
|
|
|
data->intel_fn14h[i][ECX], data->intel_fn14h[i][EDX]);
|
|
|
|
for (i = 0; i < MAX_AMDFN8000001DH_LEVEL; i++)
|
|
|
|
fprintf(f, "amd_fn8000001dh[%d]=%08x %08x %08x %08x\n", i,
|
|
|
|
data->amd_fn8000001dh[i][EAX], data->amd_fn8000001dh[i][EBX],
|
|
|
|
data->amd_fn8000001dh[i][ECX], data->amd_fn8000001dh[i][EDX]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-07 18:03:45 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-05-09 20:46:13 +00:00
|
|
|
int assigned, subleaf;
|
2020-05-03 15:09:08 +00:00
|
|
|
uint32_t addr, prev_addr, eax, ebx, ecx, edx;
|
2020-11-14 12:44:42 +00:00
|
|
|
char line[LINE_LEN], raw_filename[FILENAME_LEN], report_filename[FILENAME_LEN];
|
2021-03-20 17:14:06 +00:00
|
|
|
char libcpuid_directory[LIB_DIR_LEN], cpuid_tool[TOOL_LEN], cmd[CMD_LEN];
|
2020-11-14 12:44:42 +00:00
|
|
|
FILE *fin = NULL, *fout = NULL, *ftmp = NULL;
|
2020-05-09 20:46:13 +00:00
|
|
|
struct cpu_raw_data_t *raw = &(struct cpu_raw_data_t) {};
|
2019-07-07 18:03:45 +00:00
|
|
|
|
|
|
|
if(argc < 3)
|
|
|
|
{
|
2021-01-31 14:26:13 +00:00
|
|
|
fprintf(stderr, "Usage: %s <input file (from instlatx64)> <output file (without extension)> [--create]\n", argv[0]);
|
2019-07-07 18:03:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:09:08 +00:00
|
|
|
const char *input_filename = argv[1];
|
|
|
|
const char *output_filename = argv[2];
|
|
|
|
snprintf(raw_filename, FILENAME_LEN, "%s.raw", output_filename);
|
|
|
|
snprintf(report_filename, FILENAME_LEN, "%s_report.txt", output_filename);
|
2020-05-09 20:46:13 +00:00
|
|
|
memset(raw, 0xff, sizeof(struct cpu_raw_data_t)); // ffffffff ffffffff ffffffff ffffffff means data is missing in output test file
|
2019-07-07 18:03:45 +00:00
|
|
|
|
2020-11-14 12:44:42 +00:00
|
|
|
/* Find libcpuid root directory */
|
|
|
|
if((ftmp = popen("git rev-parse --show-toplevel", "r")) == NULL)
|
|
|
|
{
|
|
|
|
perror("Failed to run 'git' command");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-20 17:14:06 +00:00
|
|
|
if(fgets(libcpuid_directory, LIB_DIR_LEN, ftmp) == NULL)
|
2020-11-14 12:44:42 +00:00
|
|
|
{
|
|
|
|
perror("Failed to get source directory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pclose(ftmp);
|
|
|
|
libcpuid_directory[strlen(libcpuid_directory) - 1] = '\0';
|
|
|
|
|
2020-05-03 15:09:08 +00:00
|
|
|
/* Open files */
|
|
|
|
if((fin = fopen(input_filename, "r")) == NULL)
|
2019-07-07 18:03:45 +00:00
|
|
|
{
|
|
|
|
perror("Failed to open input file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if((fout = fopen(raw_filename, "w")) == NULL)
|
|
|
|
{
|
|
|
|
perror("Failed to open output file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:46:13 +00:00
|
|
|
/* Parse file and store data in cpu_raw_data_t */
|
2020-05-03 15:09:08 +00:00
|
|
|
prev_addr = -1;
|
2019-07-07 18:03:45 +00:00
|
|
|
while(fgets(line, LINE_LEN, fin) != NULL)
|
|
|
|
{
|
2020-05-09 20:46:13 +00:00
|
|
|
int assigned = sscanf(line, "CPUID %x: %x-%x-%x-%x [SL %02i]", &addr, &eax, &ebx, &ecx, &edx, &subleaf);
|
|
|
|
printf("[%i] %s", assigned, line);
|
|
|
|
if(assigned >= 5)
|
|
|
|
{
|
|
|
|
if(addr < MAX_CPUID_LEVEL)
|
|
|
|
{
|
|
|
|
if(NOT_AFFECTED(raw->basic_cpuid[addr]))
|
|
|
|
{
|
|
|
|
AFFECT_DATA_LINE(raw->basic_cpuid[addr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((addr >= EXT_CPUID) && ((addr - EXT_CPUID) < MAX_EXT_CPUID_LEVEL))
|
|
|
|
{
|
|
|
|
if(NOT_AFFECTED(raw->ext_cpuid[addr - EXT_CPUID]))
|
|
|
|
{
|
|
|
|
AFFECT_DATA_LINE(raw->ext_cpuid[addr - EXT_CPUID]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(assigned == 6)
|
|
|
|
{
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case 4: AFFECT_DATA_SUBLEAF(raw->intel_fn4); break;
|
|
|
|
case 11: AFFECT_DATA_SUBLEAF(raw->intel_fn11); break;
|
|
|
|
case 0x12: AFFECT_DATA_SUBLEAF(raw->intel_fn12h); break;
|
|
|
|
case 0x14: AFFECT_DATA_SUBLEAF(raw->intel_fn14h); break;
|
|
|
|
case 0x8000001D: AFFECT_DATA_SUBLEAF(raw->amd_fn8000001dh); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 20:46:13 +00:00
|
|
|
/* Write stored data in output file */
|
|
|
|
serialize_raw_data(raw, fout);
|
2019-07-07 18:03:45 +00:00
|
|
|
fclose(fout);
|
2020-05-09 20:46:13 +00:00
|
|
|
|
|
|
|
/* Invoke cpuid_tool */
|
2021-03-20 17:14:06 +00:00
|
|
|
snprintf(cpuid_tool, TOOL_LEN, "%s/build/cpuid_tool/cpuid_tool", libcpuid_directory);
|
2020-11-14 12:44:42 +00:00
|
|
|
if(access(cpuid_tool, F_OK) == 0)
|
|
|
|
snprintf(cmd, CMD_LEN, "%s --load=%s --report --outfile=%s", cpuid_tool, raw_filename, report_filename);
|
|
|
|
else
|
|
|
|
snprintf(cmd, CMD_LEN, "%s/cpuid_tool/cpuid_tool --load=%s --report --outfile=%s", libcpuid_directory, raw_filename, report_filename);
|
|
|
|
|
2020-05-21 17:00:26 +00:00
|
|
|
if(system(cmd))
|
|
|
|
{
|
|
|
|
perror("Failed to load raw file in cpuid_tool");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-09 15:34:07 +00:00
|
|
|
|
2020-05-03 15:09:08 +00:00
|
|
|
/* Invoke create_test */
|
2021-01-31 14:26:13 +00:00
|
|
|
snprintf(cmd, CMD_LEN, "%s/tests/create_test.py %s %s > %s.test", libcpuid_directory, raw_filename, report_filename, output_filename);
|
2020-05-03 15:09:08 +00:00
|
|
|
if((argc > 3) && !strcmp(argv[3], "--create"))
|
|
|
|
{
|
2020-05-21 17:00:26 +00:00
|
|
|
if(system(cmd))
|
|
|
|
{
|
|
|
|
perror("Failed to create test");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-03 15:09:08 +00:00
|
|
|
remove(raw_filename);
|
|
|
|
remove(report_filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Done. Use the following command to create the test file:\n");
|
|
|
|
printf("%s\n", cmd);
|
|
|
|
}
|
2019-07-07 18:03:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|