Skip to content

Avoid using stale instruction length for EPT MMIO emulation - #341

Open
z-xhh wants to merge 1 commit into
moby:masterfrom
z-xhh:feature/ept-mmio-instruction-length
Open

z-xhh wants to merge 1 commit into
moby:masterfrom
z-xhh:feature/ept-mmio-instruction-length

Conversation

@z-xhh

@z-xhh z-xhh commented Nov 13, 2026

Copy link
Copy Markdown

Problem

HyperKit uses the VM-exit instruction-length field when fetching an instruction for EPT MMIO emulation. Intel SDM Volume 3C, section 28.2.5, does not define that field for an ordinary EPT violation caused by a memory access. A stale nonzero value can therefore truncate the fetched instruction or advance RIP by the wrong amount.

For example, the regression guest performs six-byte IOAPIC accesses. Supplying a stale length of 1 causes the unmodified build to fail with Failed to emulate instruction [0x89]; supplying 15 advances RIP into a halt guard without completing the test.

Change

Set vmexit->inst_length to zero in the EPT MMIO emulation branch, allowing the existing instruction emulator to fetch, decode, and determine the actual instruction length. Other exit branches are unchanged. Software-event reinjection consumes its required instruction length before this branch.

Add a regression guest that writes and reads an IOAPIC register using six-byte instructions, validates the read result, and powers off. A test-only interposer supplies lengths 0, 1, 2, 6, and 15 for the target EPT exits; the native path is also tested. Success requires the guest marker and exit status 0; injected cases must also reach both MMIO exits. A timeout is a failure.

Validation

Validation covers a standalone IOAPIC regression and Linux boot comparisons, run in a Hyper-V nested-virtualization environment.

  • Unmodified build: lengths 0 and 6 pass; native, 1, 2, and 15 fail the regression criteria.
  • Patched build: all six cases pass and exit with status 0.
  • With the same separate GS-state workaround and identical guest configuration, the unmodified build fails during both Linux boot tests. The patched build completes three minimal-Linux boots with eight userspace heartbeats each, and three LinuxKit 7.0.14 boots with the full EROFS root filesystem, shell commands, five userspace heartbeats, and guest poweroff with exit status 0.
  • The minimal-Linux heartbeat test is stopped by the harness after observing the required heartbeats; it is not a guest-shutdown test.

The GS-state workaround used for the Linux comparisons does not modify EPT instruction lengths. The standalone MMIO regression does not load that workaround and requires no downloaded guest kernel or disk image.

Build and regression command used for both variants:

make -j2 HAVE_OCAML_QCOW=NO CFLAGS_WARN="-Wall -Wextra -Wno-deprecated-declarations"
make HAVE_OCAML_QCOW=NO CFLAGS_WARN="-Wall -Wextra -Wno-deprecated-declarations" test-ept-mmio

The warning override accommodates existing upstream warnings with clang 17; this PR does not change the production build flags. The test interposer itself builds with -Wall -Wextra -Werror.

Bare-metal Intel Mac testing has not been performed.

Specification reference: Intel SDM Volume 3C, section 28.2.5.

Clear the undefined VM-exit instruction length in the EPT MMIO emulation
branch so the existing emulator determines the decoded length and next RIP.

Add an IOAPIC regression covering native and injected lengths 0, 1, 2, 6,
and 15. All six cases pass with the fix. With an identical separate GS
workaround, three minimal Linux boots and three LinuxKit shell/poweroff
tests also pass. Bare-metal Intel Mac validation is still unavailable.

Signed-off-by: MrXhh <2791341417@qq.com>
@z-xhh
z-xhh marked this pull request as ready for review November 24, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants