The
avx2flag in/proc/cpuinfois a hardware promise, not OS permission: without the right bits set inXCR0, executing an AVX instruction still traps with#UD.
The cheatsheet in lesson 24 answers what belongs in registers and memory. The next question is what a specific CPU actually implements and what the OS has enabled it to run. Two instructions supply the answer: CPUID and XGETBV.
Mental Model: Three Different Questions
| Question | Where the answer lives |
|---|---|
| Does the CPU support AVX2? | Bit 5 of EBX in CPUID leaf 7 |
| Has the OS enabled AVX2? | Bits 1 and 2 of XCR0, read via XGETBV |
| Did the compiler emit AVX2? | The -mavx2 flag; the binary will still crash on older CPUs |
Does this match x86-64-v3? |
A combination of CPUID bits, XCR0, and baseline extensions |
Does /proc/cpuinfo expose every bit? |
No—the kernel filters and renames several flags |
CPUID: Leaves and Subleaves
CPUID takes no stack arguments: both inputs and outputs pass strictly through registers.
| Leaf | Subleaf | Output Registers | What it returns |
|---|---|---|---|
| 0 | 0 | eax, ebx, ecx, edx |
maximum supported basic leaf in eax, and vendor string across ebx/edx/ecx |
| 1 | 0 | edx, ecx |
family, model, stepping, and core feature bits: SSE2 in edx[26], OSXSAVE in ecx[27], AVX in ecx[28], FMA in ecx[12] |
| 7 | 0 | ebx |
extended feature bits: BMI1 in ebx[3], AVX2 in ebx[5], BMI2 in ebx[8], AVX-512F in ebx[16], AVX-512DQ in ebx[17], AVX-512CD in ebx[28], AVX-512BW in ebx[30], AVX-512VL in ebx[31] |
| 7 | 0 | ecx |
AVX-512VBMI in bit 1, AVX-512VNNI in bit 11, AVX-512BITALG in bit 12 |
| 7 | 0 | edx |
AVX-512VP2INTERSECT in bit 8 |
| 0x80000001 | 0 | ecx |
LZCNT in ecx[5]; belongs to the extended leaf range |
The three leaf-7 rows correspond to a single CPUID query: foundational extensions return in ebx, byte-manipulation/neural-network sets in ecx, and specialized optional extensions in edx. The remaining v2/v3 baseline flags such as SSE3–SSE4.2, POPCNT, and MOVBE (checked by isa_level below) also live in ecx of leaf 1.
At the core of the assembly harness is a cpuid wrapper that preserves rbx and writes all four output registers into an array:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; cpuid_probe.asm — raw CPUID and XGETBV probes without libc wrappers.
default rel
section .text
global asm_cpuid_leaf
global asm_xgetbv0
; ------------------------------------------------------------------------------
; void asm_cpuid_leaf(uint32_t leaf, uint32_t subleaf, uint32_t out[4])
; Input: edi = leaf, esi = subleaf, rdx = out
; Output: out[0..3] = eax, ebx, ecx, edx
; Clobbers: rax, rcx, rdx, r8 (flags untouched)
; Preserves: rbx (callee-saved; push/pop in prologue and epilogue)
; ------------------------------------------------------------------------------
asm_cpuid_leaf:
push rbx
mov r8, rdx
mov eax, edi
mov ecx, esi
cpuid
mov dword [r8], eax
mov dword [r8 + 4], ebx
mov dword [r8 + 8], ecx
mov dword [r8 + 12], edx
pop rbx
ret
; ------------------------------------------------------------------------------
; uint64_t asm_xgetbv0(void)
; Input: none
; Output: rax = XCR0 (edx:eax)
; Clobbers: rax, rcx, rdx, flags (caller-saved)
; ------------------------------------------------------------------------------
asm_xgetbv0:
xor ecx, ecx
xgetbv
shl rdx, 32
or rax, rdx
ret
section .note.GNU-stack noalloc noexec nowrite progbitscpuid overwrites all four output registers, including rbx. Under the System V ABI, rbx is callee-saved, so the wrapper saves it on the stack and restores it before ret. The output pointer is moved into r8 beforehand because rdx is also overwritten by cpuid.
XCR0: Why CPUID Alone Isn’t Enough
XCR0 is read with xgetbv: pass the register index in ecx, and the 64-bit value comes back in edx:eax.
| Bit | State Component | What it enables |
|---|---|---|
| 0 | x87 | st0–st7 registers |
| 1 | SSE | xmm registers |
| 2 | AVX | upper 128 bits of ymm |
| 5 | opmask | mask registers k0–k7 |
| 6 | ZMM_Hi256 | upper 256 bits of zmm0–zmm15 |
| 7 | Hi16_ZMM | zmm16–zmm31 registers |
AVX requires bits 1 and 2, forming mask 0x6. AVX-512 adds bits 5, 6, and 7, giving 0xE6. If CPUID reports AVX while (XCR0 & 0x6) != 0x6, the OS does not save ymm registers across context switches and leaves AVX disabled—executing an AVX instruction raises #UD.
The Instruction Set Matrix
512-bit vector computation belongs to AVX-512, encoded with the EVEX prefix.
| Extension | Width | Registers | Encoding | CPUID |
XCR0 |
Level |
|---|---|---|---|---|---|---|
SSE2 |
128 | xmm0–xmm15 |
Legacy | leaf 1, edx[26] |
bit 1 | v1 |
AVX |
256 | ymm0–ymm15 |
VEX | leaf 1, ecx[28] |
0x6 |
v3 |
AVX2 |
256 | ymm0–ymm15 |
VEX | leaf 7, ebx[5] |
0x6 |
v3 |
FMA |
256 | ymm0–ymm15 |
VEX | leaf 1, ecx[12] |
0x6 |
v3 |
AVX-512F |
512 | zmm0–zmm31 |
EVEX | leaf 7, ebx[16] |
0xE6 |
v4 |
AVX-512BW |
512 | zmm0–zmm31 |
EVEX | leaf 7, ebx[30] |
0xE6 |
v4 |
AVX-512DQ |
512 | zmm0–zmm31 |
EVEX | leaf 7, ebx[17] |
0xE6 |
v4 |
AVX-512CD |
512 | zmm0–zmm31 |
EVEX | leaf 7, ebx[28] |
0xE6 |
v4 |
AVX-512VL |
128/256 | xmm/ymm |
EVEX | leaf 7, ebx[31] |
0xE6 |
v4 |
BMI1 |
64 | GPR | VEX | leaf 7, ebx[3] |
none | v3 |
BMI2 |
64 | GPR | VEX | leaf 7, ebx[8] |
none | v3 |
Legacy encoding overwrites its first operand: addsd xmm0, xmm1 mutates xmm0. VEX introduces a non-destructive third operand and widens vectors to 256 bits: e.g., vpaddd ymm0, ymm1, ymm2 (or scalar vaddsd xmm0, xmm1, xmm2) writes to the first operand while leaving both sources intact. EVEX keeps the three-operand form, adds predication masks, and widens registers to 512 bits.
Legacy (SSE2): [prefixes][opcode][ModR/M] 2 operands (xmm0 += xmm1)
VEX (AVX2): [C5/C4 + length][opcode][ModR/M] 3 operands (ymm0 = ymm1 + ymm2)
EVEX (AVX512): [62 + mask k1..k7][opcode][ModR/M] 3 operands + mask + zmm0..zmm31Microarchitecture Levels x86-64-v1..v4
| Level | Required Extensions | Typical Hardware |
|---|---|---|
| v1 | SSE2 |
any x86-64 CPU |
| v2 | v1 plus SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT |
post-2009 CPUs |
| v3 | v2 plus AVX, AVX2, BMI1, BMI2, FMA, LZCNT, MOVBE, OSXSAVE, and XCR0 & 0x6 |
Haswell+ / Zen+ |
| v4 | v3 plus AVX-512F, AVX-512BW, AVX-512CD, AVX-512DQ, AVX-512VL, and XCR0 & 0xE6 |
server and select client cores |
A microarchitecture level is a baseline compatibility contract, not an exhaustive inventory of every instruction on the die. In the full x86-64 psABI, v2 also includes CMPXCHG16B and LAHF/SAHF, while v3 includes F16C (isa_level below checks the primary flag subset). A v4-capable CPU might still lack AVX-512VBMI or AVX-512VNNI—those are optional and must be probed via their own bits.
Testbed: scripts/cpu_features.c
The tool invokes our assembly probes, queries four CPUID leaves, reads XCR0 if OSXSAVE is set, and prints a compact capability summary. isa_level classifies the CPU level:
static const char *isa_level(const feature_bits_t *f, uint64_t xcr0)
{
bool v2 = f->sse3 && f->ssse3 && f->sse41 && f->sse42 && f->popcnt;
bool v3 = v2 && f->avx && f->avx2 && f->bmi1 && f->bmi2 && f->fma && f->movbe && f->lzcnt &&
f->osxsave && (xcr0 & 0x6U) == 0x6U;
bool v4 = v3 && f->avx512f && f->avx512bw && f->avx512cd && f->avx512dq && f->avx512vl &&
(xcr0 & 0xE6U) == 0xE6U;
if (v4) {
return "x86-64-v4";
}
if (v3) {
return "x86-64-v3";
}
if (v2) {
return "x86-64-v2";
}
return "x86-64-v1";
}On the Ryzen 7 7840H testbed, it outputs:
$ ./build/cpu_features
vendor: AuthenticAMD
max_leaf: 0x00000010
family: 25 model: 116 stepping: 1
sse2: yes
avx: yes
avx2: yes
avx512f: yes
avx512ifma: yes
avx512vbmi: yes
avx512vnni: yes
avx512bitalg: yes
avx512vpopcntdq: yes
bmi1: yes
bmi2: yes
xcr0: 0x00000000000002E7
level: x86-64-v4
cpu_features: okReading the report:
max_leaf: 0x00000010— basic leaves up through0x10are supported, so leaf 7 exists;LZCNTcomes from the extended leaf0x80000001, which is also present on this processor.family: 25 model: 116 stepping: 1— family0x19, model0x74(Zen 4 microarchitecture), andstepping: 1identifies the silicon revision within that model.xcr0: 0x00000000000002E7— bits 0, 1, 2, 5, 6, 7, and 9 are set:x87,SSE,AVX, opmasks, upperzmmhalves, andPKRU.level: x86-64-v4— all five mandatory AVX-512 bits plus the0xE6XCR0mask are satisfied.
Notice that Linux /proc/cpuinfo doesn’t use the exact same flag names as CPUID:
$ grep -m1 '^flags' /proc/cpuinfo | tr ' ' '\n' \
| grep -E '^(sse2|avx|avx2|avx512f|bmi1|bmi2|fma|movbe|abm)$' \
| sort | tr '\n' ' '
abm avx avx2 avx512f bmi1 bmi2 fma movbe sse2abm corresponds to LZCNT/ABM in Linux kernel terminology, and osxsave isn’t listed there even though CPUID reports it. Always query CPUID for exact capability checks, treating /proc/cpuinfo as a quick human overview.
Terminal Verification
$ make test
25-isa-matrix: CPUID and XCR0 invariants OKThe test enforces three invariants: SSE2 is always present, XCR0 enables x87 and AVX state, and the reported ISA level lies in x86-64-v1..v4. The opcodes are visible right in the object file:
$ objdump -d -M intel build/cpuid_probe.o | sed -n '/<asm_cpuid_leaf>:/,+6p'
0000000000000000 <asm_cpuid_leaf>:
0: 53 push rbx
1: 49 89 d0 mov r8,rdx
4: 89 f8 mov eax,edi
6: 89 f1 mov ecx,esi
8: 0f a2 cpuid
a: 41 89 00 mov DWORD PTR [r8],eax0f a2iscpuid;0f 01 d0insideasm_xgetbv0isxgetbvwithecx = 0.
Neither instruction requires ring-0 privileges: cpuid is always available in ring 3, while xgetbv only requires the OSXSAVE bit to be enabled.
Common Failure Modes
-
Treating
/proc/cpuinfoas a rawCPUIDdump.
Symptom: The kernel printsabminstead oflzcntand omitsosxsave, even though thoseCPUIDbits are set.
Cause:/proc/cpuinfois a filtered, kernel-renamed summary, not a raw register dump.
Fix: Probe bits directly viaCPUID, using/proc/cpuinfoonly as a quick sanity check. -
Calling
cpuidlike an ordinary helper without savingrbx.
Symptom:cpuidoverwriteseax,ebx,ecx, andedx; withoutpush rbx/pop rbx, the caller loses a callee-saved register.
Cause: All four registers carry inputs/outputs ofcpuid, andrbxhappens to be one of them.
Fix: Preserverbxinside yourcpuidwrapper and keep the output array pointer in a registercpuiddoesn’t touch. -
Executing
xgetbvunconditionally.
Symptom: WhenOSXSAVEin leaf 1 is zero,xgetbvraises#UD.
Cause:xgetbvis only unlocked once the OS enables extended state saving.
Fix: VerifyOSXSAVEfirst before callingxgetbvto readXCR0. -
Assuming
AVX-512Fimplies every AVX-512 extension.
Symptom:AVX-512VBMI,AVX-512VNNI, andAVX-512IFMAhave separate bits, whilex86-64-v4only requiresF,BW,CD,DQ, andVL.
Cause:Fstands for Foundation—not the entire AVX-512 family.
Fix: Check each feature bit individually rather than inferring optional extensions fromAVX-512F. -
Thinking a
CPUIDbit alone means an instruction is safe to run.
Symptom: WithAVX = 1inCPUIDand(XCR0 & 0x6) != 0x6, AVX instructions trap with#UD.
Cause: The CPU supports the instruction in silicon, yet the OS isn’t savingymmstate across context switches.
Fix: Check both theCPUIDbit and the correspondingXCR0bitmask.
Lab
Goal. Add two optional AVX-512 extensions to scripts/cpu_features.c: AVX-512VBMI2 and AVX-512VP2INTERSECT.
Contract.
AVX-512VBMI2is read from leaf 7, subleaf 0, registerECX, bit 6.AVX-512VP2INTERSECTis read from leaf 7, subleaf 0, registerEDX, bit 8.- Print two new lines in the report:
avx512vbmi2: yes/noandavx512vp2intersect: yes/no. - If either bit is set without
AVX-512For without(XCR0 & 0xE6) == 0xE6, exit with status1and print the reason. - Leave all existing output lines unchanged.
Acceptance criteria.
make testpasses.make leaksreports zero ASan errors../build/cpu_featuresoutput includes both new lines.- Cross-checking against
/proc/cpuinforeveals no contradictions (allowing for Linux’s flag-naming differences).
Hints, expected output, and walkthrough
On a Zen 4 testbed, avx512vbmi2 is present while avx512vp2intersect is absent. Expected lines:
avx512vbmi2: yes
avx512vp2intersect: noIf both fields are mistakenly read from EBX, their values will be wrong because leaf 7 packs different feature sets into ECX and EDX. If the XCR0 validation runs after printing, the program will print yes before failing.
Testbed sources, Makefile, and automated tests for this lesson: lessons/25-isa-matrix.
Summary
CPUIDreports what the CPU silicon supports, whileXCR0reports what the OS has enabled.- Pass the leaf in
eaxand subleaf inecx; results return acrosseax,ebx,ecx, andedx. cpuidclobbersrbx, so any System V ABI wrapper must save and restore it.xgetbvrequiresOSXSAVE, withXCR0masks0x6for AVX and0xE6for AVX-512.- Legacy, VEX, and EVEX encodings differ in operand count, vector width, and writemask support.
- Microarchitecture levels
x86-64-v1..v4define baseline contracts rather than exhaustive lists of every optional extension.