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

Split cpuid_basic_identify() function and add cpuid_get_vendor() function

These changes are needed to improve cpu_vendor() function. Discussion about these changes come from here (french forum): https://forums.archlinux.fr/viewtopic.php?f=18&t=15973&e=1&view=unread#p147593
The original patch, written by Benjamin ROBIN, is available here: http://pastebin.com/5BHUiCB1
This commit is contained in:
Xorg 2015-10-15 16:27:29 +02:00
parent 32ddb7d734
commit a853fcd25b
2 changed files with 45 additions and 11 deletions

View file

@ -239,10 +239,10 @@ static void load_features_common(struct cpu_raw_data_t* raw, struct cpu_id_t* da
} }
} }
static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data) static cpu_vendor_t cpuid_vendor_identify(const uint32_t *raw_vendor, char *vendor_str)
{ {
int i, j, basic, xmodel, xfamily, ext; int i;
char brandstr[64] = {0}; cpu_vendor_t vendor = VENDOR_UNKNOWN;
const struct { cpu_vendor_t vendor; char match[16]; } const struct { cpu_vendor_t vendor; char match[16]; }
matchtable[NUM_CPU_VENDORS] = { matchtable[NUM_CPU_VENDORS] = {
/* source: http://www.sandpile.org/ia32/cpuid.htm */ /* source: http://www.sandpile.org/ia32/cpuid.htm */
@ -257,18 +257,27 @@ static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* dat
{ VENDOR_SIS , "SiS SiS SiS " }, { VENDOR_SIS , "SiS SiS SiS " },
{ VENDOR_NSC , "Geode by NSC" }, { VENDOR_NSC , "Geode by NSC" },
}; };
memcpy(data->vendor_str + 0, &raw->basic_cpuid[0][1], 4); memcpy(vendor_str + 0, &raw_vendor[1], 4);
memcpy(data->vendor_str + 4, &raw->basic_cpuid[0][3], 4); memcpy(vendor_str + 4, &raw_vendor[3], 4);
memcpy(data->vendor_str + 8, &raw->basic_cpuid[0][2], 4); memcpy(vendor_str + 8, &raw_vendor[2], 4);
data->vendor_str[12] = 0; vendor_str[12] = 0;
/* Determine vendor: */ /* Determine vendor: */
data->vendor = VENDOR_UNKNOWN;
for (i = 0; i < NUM_CPU_VENDORS; i++) for (i = 0; i < NUM_CPU_VENDORS; i++)
if (!strcmp(data->vendor_str, matchtable[i].match)) { if (!strcmp(vendor_str, matchtable[i].match)) {
data->vendor = matchtable[i].vendor; vendor = matchtable[i].vendor;
break; break;
} }
return vendor;
}
static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
{
int i, j, basic, xmodel, xfamily, ext;
char brandstr[64] = {0};
data->vendor = cpuid_vendor_identify(raw->basic_cpuid[0], data->vendor_str);
if (data->vendor == VENDOR_UNKNOWN) if (data->vendor == VENDOR_UNKNOWN)
return set_error(ERR_CPU_UNKN); return set_error(ERR_CPU_UNKN);
basic = raw->basic_cpuid[0][0]; basic = raw->basic_cpuid[0][0];
@ -638,6 +647,23 @@ void cpuid_set_verbosiness_level(int level)
_current_verboselevel = level; _current_verboselevel = level;
} }
cpu_vendor_t cpuid_get_vendor(void)
{
static cpu_vendor_t vendor = VENDOR_UNKNOWN;
uint32_t raw_vendor[4];
char vendor_str[VENDOR_STR_MAX];
if(vendor == VENDOR_UNKNOWN) {
if (!cpuid_present())
set_error(ERR_NO_CPUID);
else {
cpu_exec_cpuid(0, raw_vendor);
vendor = cpuid_vendor_identify(raw_vendor, vendor_str);
}
}
return vendor;
}
void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list) void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list)
{ {
switch (vendor) { switch (vendor) {

View file

@ -754,6 +754,14 @@ libcpuid_warn_fn_t cpuid_set_warn_function(libcpuid_warn_fn_t warn_fun);
void cpuid_set_verbosiness_level(int level); void cpuid_set_verbosiness_level(int level);
/**
* @brief Obtains the CPU vendor from CPUID from the current CPU
* @note The result is cached.
* @returns VENDOR_UNKNOWN if failed, otherwise the CPU vendor type.
* @see cpu_vendor_t
*/
cpu_vendor_t cpuid_get_vendor(void);
/** /**
* @brief a structure that holds a list of processor names * @brief a structure that holds a list of processor names
*/ */