cpuid: clean up, update. and expand some comments.

This commit is contained in:
Guy Harris 2022-06-01 00:42:47 -07:00
parent 02eb128bef
commit 6014d7ad2b
2 changed files with 25 additions and 7 deletions

View File

@ -26,10 +26,11 @@ get_cpu_info(GString *str)
char CPUBrandString[0x40]; char CPUBrandString[0x40];
unsigned nExIds; unsigned nExIds;
/* https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex */ /*
* Calling ws_cpuid with 0x80000000 as the selector argument, i.e.
/* Calling __cpuid with 0x80000000 as the InfoType argument */ * executing a cpuid instruction with EAX equal to 0x80000000 and
/* gets the number of valid extended IDs. */ * ECX equal to 0, gets the number of valid extended IDs.
*/
if (!ws_cpuid(CPUInfo, 0x80000000)) if (!ws_cpuid(CPUInfo, 0x80000000))
return; return;

View File

@ -9,10 +9,25 @@
*/ */
/* /*
* Get CPU info on platforms where the cpuid instruction can be used skip 32 bit versions for GCC * Get CPU info on platforms where the x86 cpuid instruction can be used.
*
* Skip 32-bit versions for GCC and Clang, as older IA-32 processors don't
* have cpuid.
*
* Intel has documented the CPUID instruction in the "Intel(r) 64 and IA-32 * Intel has documented the CPUID instruction in the "Intel(r) 64 and IA-32
* Architectures Developer's Manual" at http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2a-manual.html * Architectures Developer's Manual" at
* the ws_cpuid() routine will return 0 if cpuinfo isn't available. *
* https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2a-manual.html
*
* The ws_cpuid() routine will return 0 if cpuinfo isn't available, including
* on non-x86 platforms and on 32-bit x86 platforms with GCC and Clang, as
* well as non-MSVC and non-GCC-or-Clang platforms.
*
* The "selector" argument to ws_cpuid() is the "initial EAX value" for the
* instruction. The initial ECX value is 0.
*
* The "CPUInfo" argument points to 4 32-bit values into which the
* resulting values of EAX, EBX, ECX, and EDX are store, in order.
*/ */
#include "ws_attributes.h" #include "ws_attributes.h"
@ -32,6 +47,8 @@
static gboolean static gboolean
ws_cpuid(guint32 *CPUInfo, guint32 selector) ws_cpuid(guint32 *CPUInfo, guint32 selector)
{ {
/* https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex */
CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0; CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
__cpuid((int *) CPUInfo, selector); __cpuid((int *) CPUInfo, selector);
/* XXX, how to check if it's supported on MSVC? just in case clear all flags above */ /* XXX, how to check if it's supported on MSVC? just in case clear all flags above */