Home

XGETBVXSETBV

XGETBV and XSETBV are x86 assembly instructions used to read and write the extended control register XCR0, which governs how the processor state is saved and restored by the operating system. They operate in conjunction with the XSAVE family of instructions and are essential for controlling which processor state components are preserved across context switches, such as x87, SSE, AVX, and newer extensions.

XGETBV reads the value of an extended control register specified by the ECX register and places the

The instructions are only usable if the CPU supports the XSETBV/XGETBV mechanism and the operating system supports

In practice, software uses XGETBV to query current state-saving settings and XSETBV to enable required states

64-bit
result
in
EDX:EAX
(RDX:RAX
in
64-bit
mode).
XSETBV
writes
a
64-bit
value
from
EDX:EAX
into
the
XCR
register
specified
by
ECX.
In
practice,
XCR0
(ECX
=
0)
is
the
commonly
used
register,
with
XGETBV
and
XSETBV
enabling
or
querying
the
OS’s
saved
state
mask.
saving
extended
processor
state.
CPU
support
is
indicated
by
the
OSXSAVE
feature
flag
(CPUID.1:ECX
bit
27).
If
OSXSAVE
is
not
present
or
not
enabled,
attempts
to
execute
XGETBV/XSETBV
may
trap
or
behave
unpredictably.
The
XCR0
register
determines
which
state
components
are
saved
by
XSAVE;
its
bitfield
enables
features
such
as
the
legacy
x87
state,
SSE
state,
AVX
YMM
state,
and
other
extended
states
as
defined
by
the
processor.
(e.g.,
enabling
AVX
by
setting
the
appropriate
XCR0
bits).
This
mechanism
is
central
to
maintaining
consistent
processor
state
across
context
switches
and
for
enabling
modern
vector
extensions.