Repository navigation
libs2e: fixed lost interrupts and timer issues in the lapic - #119
Merged
Merged
Conversation
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
force-pushed
the
issue/xxx-lapic-fixes
branch
from
October 11, 2026 22:08
10a6d26 to
53cae31
Compare
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
force-pushed
the
issue/xxx-lapic-fixes
branch
from
October 17, 2026 14:23
53cae31 to
b10b9b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A Windows 10 guest resumed from its
readysnapshot 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_LINEcalled from QEMU's main thread only queues the interrupt for delivery on the nextKVM_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
KVM_GET_LAPICandKVM_SET_LAPICdrain the queue first. This is the fix for the lost clock interrupt.kvm_runand sregs reported the stale state of the legacy vapic, and the vapic address is ignored when the lapic is emulated.KVM_RUNwhen emulating the lapic. QEMU re-enters right afterKVM_EXIT_HLTin 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. Addslibcpu_get_next_timer_delay_ns().Interrupt injection
CPU_INTERRUPT_HARD, which lost interrupts when both had one pending: taking the vector of the client cleared the request of the lapic, andKVM_INTERRUPTfailed withEEXISTwhen 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), andCPU_INTERRUPT_HARDis the mask of both. This replaces an earlier, partial fix on this branch. The lapic also ignores the ExtINT delivery mode, like KVM.stiormov ss), like KVM.KVM_EXIT_IOAPIC_EOIwithout 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
KVM_EXIT_SWITCH_STATEandKVM_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.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
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