CPUID
In the x86 architecture, the CPUID instruction is a processor supplementary instruction allowing software to discover details of the processor. It was introduced by Intel in 1993 with the launch of the Pentium and SL-enhanced 486 processors.
A program can use the
CPUID
to determine processor type and whether features such as MMX/SSE are implemented.History
Prior to the general availability of theCPUID
instruction, programmers would write esoteric machine code which exploited minor differences in CPU behavior in order to determine the processor make and model. With the introduction of the 80386 processor, EDX on reset indicated the revision but this was only readable after reset and there was no standard way for applications to read the value.Outside the x86 family, developers are mostly still required to use esoteric processes to determine the variations in CPU design that are present.
In the Motorola 680x0 family — that never had a CPUID instruction of any kind — certain specific instructions required elevated privileges. These could be used to tell various CPU family members apart. In the Motorola 68010 the instruction MOVE from SR became privileged. This notable instruction change allowed the 68010 to meet the Popek and Goldberg virtualization requirements. Because the 68000 offered an unprivileged MOVE from SR the 2 different CPUs could be told apart by a CPU error condition being triggered.
While the
CPUID
instruction is specific to the x86 architecture, other architectures often provide on-chip registers which can be read in prescribed ways to obtain the same sorts of information provided by the x86 CPUID instruction.Calling CPUID
TheCPUID
opcode is 0Fh, A2h.In assembly language, the
CPUID
instruction takes no parameters as CPUID
implicitly uses the EAX register to determine the main category of information returned. In Intel's more recent terminology, this is called the CPUID leaf. CPUID
should be called with EAX = 0
first, as this will store in the EAX register the highest EAX calling parameter that the CPU implements.To obtain extended function information
CPUID
should be called with the most significant bit of EAX set. To determine the highest extended function calling parameter, call CPUID
with EAX = 80000000h
.CPUID leaves greater than 3 but less than 80000000 are accessible only when the model-specific registers have IA32_MISC_DISABLE.BOOT_NT4 = 0. As the name suggests, Windows NT 4.0 until SP6 did not boot properly unless this bit was set, but later versions of Windows do not need it, so basic leaves greater than 4 can be assumed visible on current Windows systems., basic valid leaves go up to 14h, but the information returned by some leaves are not disclosed in publicly available documentation, i.e. they are "reserved".
Some of the more recently added leaves also have sub-leaves, which are selected via the ECX register before calling CPUID.
EAX=0: Highest Function Parameter and Manufacturer ID
This returns the CPU's manufacturer ID string a twelve-character ASCII string stored in EBX, EDX, ECX. The highest basic calling parameter is returned in EAX.Here is a list of processors and the highest function implemented.
The following are known processor manufacturer ID strings:
- "AMDisbetter!" early engineering samples of AMD K5 processor
- "AuthenticAMD" AMD
- "CentaurHauls" IDT WinChip/Centaur
- "CyrixInstead" Cyrix/early STMicroelectronics and IBM
- "GenuineIntel" Intel
- "TransmetaCPU" Transmeta
- "GenuineTMx86" Transmeta
- "Geode by NSC" National Semiconductor
- "NexGenDriven" NexGen
- "RiseRiseRise" Rise
- "SiS SiS SiS " SiS
- "UMC UMC UMC " UMC
- "VIA VIA VIA " VIA
- "Vortex86 SoC" DM&P Vortex
- " Shanghai " Zhaoxin
- "HygonGenuine" Hygon
- "E2K MACHINE" MCST Elbrus
- "bhyve bhyve " bhyve
- " KVMKVMKVM " KVM
- "TCGTCGTCGTCG" QEMU
- "Microsoft Hv" Microsoft Hyper-V or Windows Virtual PC
- " lrpepyh vr" Parallels
- "VMwareVMware" VMware
- "XenVMMXenVMM" Xen HVM
- "ACRNACRNACRN"
- " QNXQVMBSQG " QNX Hypervisor
- "VirtualApple" Apple Rosetta 2
.data
s0:.asciz "CPUID: %x\n"
s1:.asciz "Largest basic function number implemented: %i\n"
s2:.asciz "Vendor ID: %.12s\n"
.text
.align 32
.globl main
main:
pushq %rbp
movq %rsp,%rbp
subq $16,%rsp
movl $1,%eax
cpuid
movq $s0,%rdi
movl %eax,%esi
xorl %eax,%eax
call printf
pushq %rbx // -fPIC
xorl %eax,%eax
cpuid
movl %ebx,0
movl %edx,4
movl %ecx,8
popq %rbx // -fPIC
movq $s1,%rdi
movl %eax,%esi
xorl %eax,%eax
call printf
movq $s2,%rdi
movq %rsp,%rsi
xorl %eax,%eax
call printf
movq %rbp,%rsp
popq %rbp
// ret
movl $1,%eax
int $0x80
EAX=1: Processor Info and Feature Bits
This returns the CPU's stepping, model, and family information in register EAX, feature flags in registers EDX and ECX, and additional feature info in register EBX.- Stepping ID is a product revision number assigned due to fixed errata or other changes.
- The actual processor model is derived from the Model, Extended Model ID and Family ID fields. If the Family ID field is either 6 or 15, the model is equal to the sum of the Extended Model ID field shifted left by 4 bits and the Model field. Otherwise, the model is equal to the value of the Model field.
- The actual processor family is derived from the Family ID and Extended Family ID fields. If the Family ID field is equal to 15, the family is equal to the sum of the Extended Family ID and the Family ID fields. Otherwise, the family is equal to value of the Family ID field.
- The meaning of the Processor Type field is given by the table below.
Type | Encoding in Binary |
Original OEM Processor | 00 |
Intel Overdrive Processor | 01 |
Dual processor | 10 |
Reserved value | 11 |
Bits | EBX | Valid |
7:0 | Brand Index | |
15:8 | CLFLUSH line size | if CLFLUSH feature flag is set. CPUID.01.EDX.CLFSH = 1 |
23:16 | Maximum number of addressable IDs for logical processors in this physical package; The nearest power-of-2 integer that is not smaller than this value is the number of unique initial APIC IDs reserved for addressing different logical processors in a physical package. Former use: Number of logical processors per physical processor; two for the Pentium 4 processor with Hyper-Threading Technology. | if Hyper-threading feature flag is set. CPUID.01.EDX.HTT = 1 |
31:24 | Local APIC ID: The initial APIC-ID is used to identify the executing logical processor. It can also be identified via the cpuid 0BH leaf. | Pentium 4 and subsequent processors. |
The processor info and feature flags are manufacturer specific but usually the Intel values are used by other manufacturers for the sake of compatibility.
Reserved fields should be masked before using them for processor identification purposes.
EAX=2: Cache and TLB Descriptor information
This returns a list of descriptors indicating cache and TLB capabilities in EAX, EBX, ECX and EDX registers.EAX=3: Processor Serial Number
This returns the processor's serial number. The processor serial number was introduced on Intel Pentium III, but due to privacy concerns, this feature is no longer implemented on later models. Transmeta's Efficeon and Crusoe processors also provide this feature. AMD CPUs however, do not implement this feature in any CPU models.For Intel Pentium III CPUs, the serial number is returned in the EDX:ECX registers. For Transmeta Efficeon CPUs, it is returned in the EBX:EAX registers. And for Transmeta Crusoe CPUs, it is returned in the EBX register only.
Note that the processor serial number feature must be enabled in the BIOS setting in order to function.
EAX=4 and EAX=Bh: Intel thread/core and cache topology
These two leaves are used for processor topology and cache hierarchy enumeration in Intel multi-core processors. AMD does not use these leaves but has alternate ways of doing the core enumeration.Unlike most other CPUID leaves, leaf Bh will return different values in EDX depending on which logical processor the CPUID instruction runs; the value returned in EDX is actually the x2APIC id of the logical processor. The x2APIC id space is not continuously mapped to logical processors, however; there can be gaps in the mapping, meaning that some intermediate x2APIC ids don't necessarily correspond to any logical processor. Additional information for mapping the x2APIC ids to cores is provided in the other registers. Although the leaf Bh has sub-leaves, the value returned in EDX is only affected by the logical processor on which the instruction is running but not by the subleaf.
The processor topology exposed by leaf Bh is a hierarchical one, but with the strange caveat that the order of levels in this hierarchy doesn't necessarily correspond the order in the physical hierarchy. However, every logical level can be queried as an ECX subleaf for its correspondence to a "level type", which can be either SMT, core, or "invalid". The level id space starts at 0 and is continuous, meaning that if a level id is invalid, all higher level ids will also be invalid. The level type is returned in bits 15:08 of ECX, while the number of logical processors at the level queried is returned in EBX. Finally, the connection between these levels and x2APIC ids is returned in EAX as the number of bits that the x2APIC id must be shifted in order to obtain a unique id at the next level.
As an example, a dual-core Westmere processor capable of hyperthreading could have x2APIC ids 0, 1, 4 and 5 for its four logical processors. Leaf Bh, subleaf 0 of CPUID could for instance return 100h in ECX, meaning that level 0 describes the SMT layer, and return 2 in EBX because there are two logical processors per physical core. The value returned in EAX for this 0-subleaf should be 1 in this case, because shifting the aforementioned x2APIC ids to the right by one bit gives a unique core number and erases the SMT id bit inside each core. A simpler way to interpret this information is that the last bit of the x2APIC id identifies the SMT/hyperthreading unit inside each core in our example. Advancing to subleaf 1 could for instance return 201h in ECX, meaning that this is a core-type level, and 4 in EBX because there are 4 logical processors in the package; EAX returned could be any value greater than 3, because it so happens that bit number 2 is used to identify the core in the x2APIC id. Note that bit number 1 of the x2APIC id is not used in this example. However EAX returned at this level could well be 4 because that also gives a unique id at the package level when shifting the x2APIC id by 4 bits. Finally, you may wonder what the EAX=4 leaf can tell us that we didn't find out already. In EAX it returns the APIC mask bits reserved for a package; that would be 111b in our example because bits 0 to 2 are used for identifying logical processors inside this package, but bit 1 is also reserved although not used as part of the logical processor identification scheme. In other words, APIC ids 0 to 7 are reserved for the package, even though half of these values don't map to a logical processor.
The cache hierarchy of the processor is explored by looking at the sub-leaves of leaf 4. The APIC ids are also used in this hierarchy to convey information about how the different levels of cache are shared by the SMT units and cores. To continue our example, the L2 cache, which is shared by SMT units of the same core but not between physical cores on the Westmere is indicated by EAX being set to 1, while the information that the L3 cache is shared by the whole package is indicated by setting those bits to 111b. The cache details, including cache type, size, and associativity are communicated via the other registers on leaf 4.
Beware that older versions of the Intel app note 485 contain some misleading information, particularly with respect to identifying and counting cores in a multi-core processor; errors from misinterpreting this information have even been incorporated in the Microsoft sample code for using cpuid, even for the 2013 edition of Visual Studio, and also in the sandpile.org page for CPUID, but the Intel code sample for identifying processor topology has the correct interpretation, and the current Intel Software Developer’s Manual has more clear language. The cross-platform production code from Wildfire Games also implements the correct interpretation of the Intel documentation.
Topology detection examples involving older Intel processors that lack x2APIC are given in a 2010 Intel presentation. Beware that using that older detection method on 2010 and newer Intel processors may overestimate the number of cores and logical processors because the old detection method assumes there are no gaps in the APIC id space, and this assumption is violated by some newer processors, but these newer processors also come with an x2APIC, so their topology can be correctly determined using the EAX=Bh leaf method.
EAX=6: Thermal and power management
EAX=7, ECX=0: Extended Features
This returns extended feature flags in EBX, ECX, and EDX.EAX=7, ECX=1: Extended Features
This returns extended feature flags in EAX.EAX=80000000h: Get Highest Extended Function Implemented
The highest calling parameter is returned in EAX.EAX=80000001h: Extended Processor Info and Feature Bits
This returns extended feature flags in EDX and ECX.AMD feature flags are as follows:
EAX=80000002h,80000003h,80000004h: Processor Brand String
These return the processor brand string in EAX, EBX, ECX and EDX.CPUID
must be issued with each parameter in sequence to get the entire 48-byte null-terminated ASCII processor brand string. It is necessary to check whether the feature is present in the CPU by issuing CPUID
with EAX = 80000000h
first and checking if the returned value is greater or equal to 80000004h.- include
// GCC-provided - include
- include
EAX=80000005h: L1 Cache and TLB Identifiers
This function contains the processor’s L1 cache and TLB characteristics.EAX=80000006h: Extended L2 Cache Features
Returns details of the L2 cache in ECX, including the line size in bytes, type of associativity and the cache size in KiB.- include
// GCC-provided - include
- include
EAX=80000007h: Advanced Power Management Information
This function provides advanced power management feature identifiers. EDX bit 8 indicates support for invariant TSC.EAX=80000008h: Virtual and Physical address Sizes
Returns largest virtual and physical address sizes in EAX.- Bits 07-00: #Physical Address Bits.
- Bits 15-8: #Linear Address Bits.
- Bits 31-16: Reserved = 0.
EBX is used for features:
- Bit 0: CLZERO, Clear cache line with address in RAX.
- Bit 4: RDPRU, Read MPERF or APERF from ring 3.
- Bit 8: MCOMMIT, commit stores to memory. For memory fencing and retriving ECC errors.
- Bit 9: WBNOINVD, Write Back and Do Not Invalidate Cache.
- Bits 07-00: #Physical Cores minus one.
- Bits 11-8: Reserved = 0.
- Bits 15-12: #APIC ID Bits. 2 raised to this power would be the physical core count, as long as it's non-zero.
- Bits 17-16: Performance time-stamp counter size.
- Bits 31-18: Reserved = 0.
EAX=8FFFFFFFh: AMD Easter Egg
Specific to AMD K7 and K8 CPUs, this returns the string "IT'S HAMMER TIME" in EAX, EBX, ECX and EDX.CPUID usage from high-level languages
Inline assembly
This information is easy to access from other languages as well. For instance, the C code for gcc below prints the first five values, returned by the cpuid:- include
int main
In MSVC and Borland/Embarcadero C compilers flavored inline assembly, the clobbering information is implicit in the instructions:
- include
If either version was written in plain assembly language, the programmer must manually save the results of EAX, EBX, ECX, and EDX elsewhere if they want to keep using the values.
Wrapper functions
GCC also provides a header called<cpuid.h>
on systems that have CPUID. The __cpuid
is a macro expanding to inline assembly. Typical usage would be:- include
- include
main
But if one requested an extended feature not present on this CPU, they would not notice and might get random, unexpected results. Safer version is also provided in
<cpuid.h>
. It checks for extended features and does some more safety checks. The output values are not passed using reference-like macro parameters, but more conventional pointers.- include
- include
main
Notice the ampersands in
&a, &b, &c, &d
and the conditional statement. If the __get_cpuid
call receives a correct request, it will return a non-zero value, if it fails, zero.Microsoft Visual C compiler has builtin function
__cpuid
so the cpuid instruction may be embedded without using inline assembly, which is handy since the x86-64 version of MSVC does not allow inline assembly at all. The same program for MSVC would be:- include
- include
Many interpreted or compiled scripting languages are capable of using CPUID via an FFI library. shows usage of the Ruby FFI module to execute assembly language that includes the CPUID opcode.
CPU-specific information outside x86
Some of the non-x86 CPU architectures also provide certain forms of structured information about the processor's abilities, commonly as a set of special registers:- ARM architectures have a
CPUID
coprocessor register which requires EL1 or above to access. - The IBM System z mainframe processors have a Store CPU ID instruction since the 1983 IBM 4381 for querying the processor ID.
- The IBM System z mainframe processors also have a Store Facilities List Extended instruction which lists the installed hardware features.
- The MIPS32/64 architecture defines a mandatory Processor Identification and a series of daisy-chained Configuration Registers.
- The PowerPC processor has the 32-bit read-only Processor Version Register identifying the processor model in use. The instruction requires supervisor access level.