On this page

The avx2 flag in /proc/cpuinfo is a hardware promise, not OS permission: without the right bits set in XCR0, 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:

cpuid_probe.asm
; 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 progbits

cpuid 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.

AVX in CPUID?
ECX[28] = 1

OSXSAVE in CPUID?
ECX[27] = 1

AVX disabled

XGETBV(0)

AVX disabled:
OS does not save YMM

XCR0[2:1] = 11?

AVX & AVX2 enabled

AVX & AVX2 disabled

XCR0[7:5] = 111?

AVX-512 enabled

AVX-512 disabled

yesnoyesnoyesnoyesno

AVX in CPUID?
ECX[28] = 1

OSXSAVE in CPUID?
ECX[27] = 1

AVX disabled

XGETBV(0)

AVX disabled:
OS does not save YMM

XCR0[2:1] = 11?

AVX & AVX2 enabled

AVX & AVX2 disabled

XCR0[7:5] = 111?

AVX-512 enabled

AVX-512 disabled

yesnoyesnoyesnoyesno
Fig. 1. Checking OSXSAVE and XCR0 bits before using AVX

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..zmm31

Microarchitecture 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: ok

Reading the report:

  • max_leaf: 0x00000010 — basic leaves up through 0x10 are supported, so leaf 7 exists; LZCNT comes from the extended leaf 0x80000001, which is also present on this processor.
  • family: 25 model: 116 stepping: 1 — family 0x19, model 0x74 (Zen 4 microarchitecture), and stepping: 1 identifies the silicon revision within that model.
  • xcr0: 0x00000000000002E7 — bits 0, 1, 2, 5, 6, 7, and 9 are set: x87, SSE, AVX, opmasks, upper zmm halves, and PKRU.
  • level: x86-64-v4 — all five mandatory AVX-512 bits plus the 0xE6 XCR0 mask 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 sse2

abm 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 OK

The 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],eax
  • 0f a2 is cpuid;
  • 0f 01 d0 inside asm_xgetbv0 is xgetbv with ecx = 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

  1. Treating /proc/cpuinfo as a raw CPUID dump.
    Symptom: The kernel prints abm instead of lzcnt and omits osxsave, even though those CPUID bits are set.
    Cause: /proc/cpuinfo is a filtered, kernel-renamed summary, not a raw register dump.
    Fix: Probe bits directly via CPUID, using /proc/cpuinfo only as a quick sanity check.

  2. Calling cpuid like an ordinary helper without saving rbx.
    Symptom: cpuid overwrites eax, ebx, ecx, and edx; without push rbx/pop rbx, the caller loses a callee-saved register.
    Cause: All four registers carry inputs/outputs of cpuid, and rbx happens to be one of them.
    Fix: Preserve rbx inside your cpuid wrapper and keep the output array pointer in a register cpuid doesn’t touch.

  3. Executing xgetbv unconditionally.
    Symptom: When OSXSAVE in leaf 1 is zero, xgetbv raises #UD.
    Cause: xgetbv is only unlocked once the OS enables extended state saving.
    Fix: Verify OSXSAVE first before calling xgetbv to read XCR0.

  4. Assuming AVX-512F implies every AVX-512 extension.
    Symptom: AVX-512VBMI, AVX-512VNNI, and AVX-512IFMA have separate bits, while x86-64-v4 only requires F, BW, CD, DQ, and VL.
    Cause: F stands for Foundation—not the entire AVX-512 family.
    Fix: Check each feature bit individually rather than inferring optional extensions from AVX-512F.

  5. Thinking a CPUID bit alone means an instruction is safe to run.
    Symptom: With AVX = 1 in CPUID and (XCR0 & 0x6) != 0x6, AVX instructions trap with #UD.
    Cause: The CPU supports the instruction in silicon, yet the OS isn’t saving ymm state across context switches.
    Fix: Check both the CPUID bit and the corresponding XCR0 bitmask.

Lab

Goal. Add two optional AVX-512 extensions to scripts/cpu_features.c: AVX-512VBMI2 and AVX-512VP2INTERSECT.

Contract.

  • AVX-512VBMI2 is read from leaf 7, subleaf 0, register ECX, bit 6.
  • AVX-512VP2INTERSECT is read from leaf 7, subleaf 0, register EDX, bit 8.
  • Print two new lines in the report: avx512vbmi2: yes/no and avx512vp2intersect: yes/no.
  • If either bit is set without AVX-512F or without (XCR0 & 0xE6) == 0xE6, exit with status 1 and print the reason.
  • Leave all existing output lines unchanged.

Acceptance criteria.

  1. make test passes.
  2. make leaks reports zero ASan errors.
  3. ./build/cpu_features output includes both new lines.
  4. Cross-checking against /proc/cpuinfo reveals 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: no

If 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

  1. CPUID reports what the CPU silicon supports, while XCR0 reports what the OS has enabled.
  2. Pass the leaf in eax and subleaf in ecx; results return across eax, ebx, ecx, and edx.
  3. cpuid clobbers rbx, so any System V ABI wrapper must save and restore it.
  4. xgetbv requires OSXSAVE, with XCR0 masks 0x6 for AVX and 0xE6 for AVX-512.
  5. Legacy, VEX, and EVEX encodings differ in operand count, vector width, and writemask support.
  6. Microarchitecture levels x86-64-v1..v4 define baseline contracts rather than exhaustive lists of every optional extension.
Bridge to the next lesson
The ISA matrix answers what the processor supports and what the OS permits. Lesson 26 tackles what happens when code crashes anyway: signals, exit codes, GDB backtraces, and the root cause each dump pinpoints.