From 7b185baec82f0a9e873b1003f0387da8769610e4 Mon Sep 17 00:00:00 2001 From: Xorg Date: Sun, 4 Sep 2022 19:50:13 +0200 Subject: [PATCH] Add cpu_request_core_type function --- libcpuid/cpuid_main.c | 21 +++++++++++++++++++++ libcpuid/exports.def | 1 + libcpuid/libcpuid.h | 21 +++++++++++++++++++++ libcpuid/libcpuid.sym | 1 + 4 files changed, 44 insertions(+) diff --git a/libcpuid/cpuid_main.c b/libcpuid/cpuid_main.c index 54f804e..4b5a130 100644 --- a/libcpuid/cpuid_main.c +++ b/libcpuid/cpuid_main.c @@ -755,6 +755,26 @@ int cpu_identify_all(struct cpu_raw_data_array_t *raw_array, struct system_id_t* return ret_error; } +struct cpu_id_t* cpu_request_core_type(cpu_purpose_t purpose, struct cpu_raw_data_array_t* raw_array, struct system_id_t* system) +{ + uint8_t cpu_type_index = 0; + struct system_id_t my_system; + + if (!system) { + system = &my_system; + if (cpu_identify_all(raw_array, system) != ERR_OK) + return NULL; + } + + for (cpu_type_index = 0; cpu_type_index < system->num_cpu_types; cpu_type_index++) { + if (purpose == system->cpu_types[cpu_type_index].purpose) + return &system->cpu_types[cpu_type_index]; + } + + set_error(ERR_NOT_FOUND); + return NULL; +} + const char* cpu_architecture_str(cpu_architecture_t architecture) { const struct { cpu_architecture_t architecture; const char* name; } @@ -940,6 +960,7 @@ const char* cpuid_error(void) { ERR_INVCNB , "Invalid core number"}, { ERR_HANDLE_R , "Error on handle read"}, { ERR_INVRANGE , "Invalid given range"}, + { ERR_NOT_FOUND, "Requested type not found"}, }; unsigned i; for (i = 0; i < COUNT_OF(matchtable); i++) diff --git a/libcpuid/exports.def b/libcpuid/exports.def index fa17205..7ecb383 100644 --- a/libcpuid/exports.def +++ b/libcpuid/exports.def @@ -34,5 +34,6 @@ cpu_rdmsr_range @30 cpuid_get_epc @31 msr_serialize_raw_data @32 cpu_identify_all @36 +cpu_request_core_type @37 cpu_architecture_str @38 cpu_purpose_str @39 diff --git a/libcpuid/libcpuid.h b/libcpuid/libcpuid.h index b64bfd9..28ef8e6 100644 --- a/libcpuid/libcpuid.h +++ b/libcpuid/libcpuid.h @@ -654,6 +654,7 @@ typedef enum { ERR_INVCNB = -14, /*!< Invalid core number */ ERR_HANDLE_R = -15, /*!< Error on handle read */ ERR_INVRANGE = -16, /*!< Invalid given range */ + ERR_NOT_FOUND= -17, /*!< Requested type not found */ } cpu_error_t; /** @@ -784,6 +785,26 @@ int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data); */ int cpu_identify_all(struct cpu_raw_data_array_t *raw_array, struct system_id_t* system); +/** + * @brief Identifies a given CPU type + * @param purpose - Input - a \ref cpu_purpose_t to request + * @param raw_array - Optional input - a pointer to the array of raw CPUID data, which is obtained + * either by cpuid_get_all_raw_data or cpuid_deserialize_all_raw_data. + * Can also be NULL, in which case the functions calls + * cpuid_get_all_raw_data itself. + * @param system - Optional input - the decoded CPU features/info for each CPU type, which is obtained + * by cpu_identify_all. + * Can also be NULL, in which case the functions calls + * cpu_identify_all itself. + * @note The function is based on cpu_identify_all. Refer to cpu_identify notes. + * @returns the decoded CPU features/info matching purpose if purpose found successfully, + * ERR_NOT_FOUND if CPU type not found, + * or a different negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +struct cpu_id_t* cpu_request_core_type(cpu_purpose_t purpose, struct cpu_raw_data_array_t* raw_array, struct system_id_t* system); + /** * @brief Returns the short textual representation of a CPU architecture * @param architecture - the architecture, whose textual representation is wanted. diff --git a/libcpuid/libcpuid.sym b/libcpuid/libcpuid.sym index 00d09bf..0e6ad4e 100644 --- a/libcpuid/libcpuid.sym +++ b/libcpuid/libcpuid.sym @@ -31,5 +31,6 @@ cpu_rdmsr_range cpuid_get_epc msr_serialize_raw_data cpu_identify_all +cpu_request_core_type cpu_architecture_str cpu_purpose_str