Skip to content

libs2e: fixed lost interrupts and timer issues in the lapic - #119

Merged
vitalych merged 16 commits into
masterfrom
issue/xxx-lapic-fixes
Oct 17, 2026
Merged

vitalych merged 16 commits into
masterfrom
issue/xxx-lapic-fixes

Conversation

@vitalych

@vitalych vitalych commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Problem

A Windows 10 guest resumed from its ready snapshot was stuck with a frozen clock: the mouse worked, but the guest never received another clock interrupt.

Windows 10 drives its clock from HPET timer 0 in one-shot mode and re-arms it from the tick handler. The snapshot had been saved with the comparator already expired and the corresponding interrupt neither pending nor in service, so the guest waited forever for a tick.

The snapshot is created under libs2e, where KVM_IRQ_LINE called from QEMU's main thread only queues the interrupt for delivery on the next KVM_RUN. When the vm state is saved before the cpu runs again, the queued interrupt is not part of the lapic state and is lost. This is timing dependent, the snapshot has to be taken right after a tick.

Fixing this uncovered more problems in the lapic emulation, in how the kvm client and the lapic share the interrupt request, and in state switches. They are fixed here as well.

Changes

One commit per fix.

Lapic

  • Deliver queued interrupts before accessing the lapic state. KVM_GET_LAPIC and KVM_SET_LAPIC drain the queue first. This is the fix for the lost clock interrupt.
  • Fixed truncation of the lapic timer current count. The elapsed tick count was truncated to 32 bits before the modulo in periodic mode.
  • Ignore fixed interrupts when the lapic is disabled. The PIT pulse at machine init left a stray vector 0 in the IRR.
  • Use the lapic for cr8 and apic base when there is one. kvm_run and sregs reported the stale state of the legacy vapic, and the vapic address is ignored when the lapic is emulated.
  • Preserve the BSP flag of the lapic base. Writes to the apic base msr cleared it. The flag is now owned by the apic, like in the QEMU userspace apic.
  • Start the vm clock when creating the vm. The clock only started on the first state switch, so the lapic timer never expired before that.
  • Block halted cpu in KVM_RUN when emulating the lapic. QEMU re-enters right after KVM_EXIT_HLT in split irqchip mode, which used a full host core while the guest was idle. The cpu now sleeps until the next libcpu timer deadline, an interrupt, or an exit request. Adds libcpu_get_next_timer_delay_ns().

Interrupt injection

  • Separate interrupt requests for the kvm client and the lapic. Both shared CPU_INTERRUPT_HARD, which lost interrupts when both had one pending: taking the vector of the client cleared the request of the lapic, and KVM_INTERRUPT failed with EEXIST when only the lapic had one pending, so the PIC interrupt that QEMU had already acknowledged was dropped. Each source now has its own bit (CPU_INTERRUPT_HARD_EXT, CPU_INTERRUPT_HARD_APIC), and CPU_INTERRUPT_HARD is the mask of both. This replaces an earlier, partial fix on this branch. The lapic also ignores the ExtINT delivery mode, like KVM.
  • Keep the interrupt window closed during an interrupt shadow (the instruction after sti or mov ss), like KVM.
  • Abort when an exit reason overwrites one the kvm client did not see. The lapic sets KVM_EXIT_IOAPIC_EOI without yielding, so a later exit in the same translation block could hide the EOI from QEMU. This is instrumentation for a possible bug that has not been reproduced.

State switches and device state

  • Make state switches atomic for the kvm client. The three separate exits to flush the disk, save and restore the device state are replaced with KVM_EXIT_SWITCH_STATE and KVM_SWITCH_STATE_CONTINUE. The client keeps its lock for the whole switch, so its main loop can no longer run device code in the middle of it (e.g., an rtc interrupt raised between saving the old state and restoring the new one). The protocol is described in the KVM interface docs.
  • Check the cpu state of the kvm client when handling device state. Abort if the client's copy of the concrete cpu state does not match the engine. The lapic state now comes from the client's snapshots.
  • libcpu: do not chain a flushed tb to the next one. A tb flush requested through se_tb_safe_flush() could chain a freed tb to the new buffer and later patch through reused memory.

Requirements

The state switch change needs a kvm client that supports KVM_EXIT_SWITCH_STATE, i.e., the matching QEMU change ("kvm: handle state switches atomically"). An older QEMU does not work with this branch.

Testing

  • Cold boot of the Windows 10 1909 x64 image with split irqchip reaches the logon screen, clock interrupts keep firing at about 61 per second.
  • Traced that an interrupt queued from the main thread is delivered before the first lapic state access.
  • An idle guest went from 100% to about 1% host cpu.
  • The vm clock advances from vm creation.
  • The separate interrupt requests and the interrupt shadow check were built and tested on top of the state switch change. The testsuite has not triggered the new exit reason abort.

Not tested: a guest that uses the lapic timer, a guest that uses the PIC and the lapic timer together (e.g., Linux with noapic), a snapshot retaken with these fixes, and the BSP flag commit beyond compiling. The docs were not built with Sphinx.

Existing snapshots that already lost their clock interrupt stay broken and must be retaken.

🤖 Generated with Claude Code

vitalych and others added 5 commits October 11, 2026 12:46
KVM_IRQ_LINE called from a non-cpu thread only queues the interrupt, which
is delivered on the next KVM_RUN. If the client saved the vm state before
running the cpu again, the interrupt was missing from the lapic state and
lost. A snapshot taken right after a one-shot HPET tick resumed with a
guest that never got another clock interrupt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The number of ticks elapsed since the initial count was loaded is not
bounded by 32 bits. Truncating it before taking the modulo returned
wrong values in periodic mode after 2^32 ticks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The interrupt raised by the PIT at machine init was latched in the IRR
while the lapic was still software-disabled, leaving a stray vector 0
pending forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kvm_run and sregs reported the tpr and the apic base of the legacy
vapic state, which is stale when the lapic is emulated by libs2e.
The kvm client ended up with a wrong tpr after every KVM_RUN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The vm clock only started ticking on the first state switch. Until then
the lapic timer never expired and its current count stayed constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vitalych
vitalych force-pushed the issue/xxx-lapic-fixes branch from 10a6d26 to 53cae31 Compare October 11, 2026 22:08
vitalych and others added 11 commits October 12, 2026 00:44
A kvm client that does not emulate the lapic calls KVM_RUN again right
after KVM_EXIT_HLT, which burned a full host core while the guest was
idle. Sleep until the next timer deadline, interrupt, or exit request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Writing the apic base msr cleared the BSP flag, so the guest read back
0xfee00800 after 0xfee00900 was set. Do like the QEMU apic: the flag
belongs to the apic and is not affected by writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…apic has none

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A tb flush requested through se_tb_safe_flush() runs in tb_find_fast(),
but tb_find_slow() then cleared tb_invalidated_flag, so the execution
loop chained the previous (now freed) tb to the first tb of the new
buffer. Invalidating that tb later walked its jump list and patched
through the stale tb, whose memory had been reused for generated code.

Set the flag in tb_flush() itself and let fetch_and_run_tb() be the only
place that consumes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The symbolic execution engine manages the entire cpu state and ignores what
the kvm client writes while it handles a device state snapshot. The client
keeps its own copy of the concrete cpu state, which it may use internally.
Abort if that copy does not match the state of the engine.

The client now gets the program counter and the system flags when it saves
a snapshot. They are always concrete and it needs the flags to interpret
the segment registers. The fpu state may be symbolic, do not let the client
concretize it.

The lapic is a device, its state comes from the snapshots of the client.
Apply the apic base msr when restoring a snapshot, and the apic base of the
system registers otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching states used three separate exits to flush the disk, save and
restore the device state. The kvm client released its global lock between
them, so its main loop could run device code in the middle of a switch.
For example, an expired rtc timer raised an interrupt after the old state
was saved and before the new one was restored. That interrupt belonged to
neither state.

Replace the three exits with KVM_EXIT_SWITCH_STATE. The client keeps its
lock, performs the requested step and calls KVM_SWITCH_STATE_CONTINUE to
let the engine do its part and get the next step, until the switch is done.

The engine delimits the steps with s2e_kvm_begin_state_switch and
s2e_kvm_end_state_switch. Requesting a step outside of them aborts.

This requires a kvm client that supports KVM_EXIT_SWITCH_STATE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KVM_RUN returns KVM_EXIT_SWITCH_STATE when the engine forks or switches
to another execution path. Document the steps that the client performs,
the KVM_SWITCH_STATE_CONTINUE command that resumes the switch, and the
requirement that the client handles the whole sequence atomically.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the lapic

The kvm client (KVM_INTERRUPT) and the lapic of libs2e shared
CPU_INTERRUPT_HARD. This lost interrupts when both had one pending:

- When the cpu took the vector of the client, it cleared the request of
  the lapic as well. The lapic interrupt stayed in IRR until the next
  lapic event.
- KVM_INTERRUPT failed with EEXIST when only the lapic had a pending
  interrupt. The client had already acknowledged the interrupt in its
  pic and could not inject it again.

Each source now has its own bit, CPU_INTERRUPT_HARD_EXT and
CPU_INTERRUPT_HARD_APIC, which it sets and clears alone.
CPU_INTERRUPT_HARD is the mask of both. cpu_get_pic_interrupt clears
the bit of the source that supplies the vector, the client first.

The lapic also ignores the ExtINT delivery mode, like KVM. It raised
CPU_INTERRUPT_HARD without a vector to deliver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hadow

The kvm client could inject an interrupt during an interrupt shadow (the
instruction after sti or mov ss), because ready_for_interrupt_injection
only checked the interrupt flag. KVM also requires that there is no
shadow (kvm_arch_interrupt_allowed).

VCPU::ready_for_interrupt_injection() replaces the two copies of the
computation in VCPU::run and checks HF_INHIBIT_IRQ_MASK. The window
check of execution_loop has the same condition, otherwise the loop
would exit after sti with the window closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ot see

The lapic sets KVM_EXIT_IOAPIC_EOI without yielding. A yield with another
exit reason before the cpu loop exits would lose the EOI. All exit reasons
set from the cpu coroutine now go through set_exit_reason, which aborts if
the previous one was not delivered. KVM_SWITCH_STATE_CONTINUE now resets the
exit reason to -1 like KVM_RUN does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vitalych
vitalych force-pushed the issue/xxx-lapic-fixes branch from 53cae31 to b10b9b1 Compare October 17, 2026 14:23
@vitalych
vitalych merged commit 7357771 into master Oct 17, 2026
3 checks passed
@vitalych
vitalych deleted the issue/xxx-lapic-fixes branch October 17, 2026 14:51
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.

1 participant