Goal: restore values to the context that owns them
Running VM A and VM B in turn on one physical CPU requires keeping their timer configurations separate. The goal is to preserve offset, CVAL, ENABLE, and IMASK as a VM-owned context and explain why evaluation after restoration uses that VM's values. Build on virtual count as physical count minus offset and the distinction between expiry and output. Each VM has one vCPU; migration across several guest CPUs is outside this selected model.
Imagine A waiting at virtual count 20 for deadline 25 while B waits at count 100 for deadline 110. Saving only CVAL and retaining B's offset can make A appear overdue immediately after restoration. Every integer can be individually valid while the assembled context is wrong. List the fields being saved and track the VM IDs of source, destination, and restoration. Ownership of a saved value is as important as its numerical interpretation, because a correct subtraction with another VM's offset still produces the wrong guest clock.
A context switch does not reset or freeze this clock
Under the selected policy, physical count can advance a VM's derived virtual clock even when the VM is not executing. A context switch is not a counter reset. Saving A's old virtual count and reusing that stale number as current time on return would violate this policy. Recompute from the saved offset and current physical count, then compare with the preserved CVAL. A deadline may pass while a vCPU waits, but this does not mean its guest handler executed during that interval.
A normal trace switches from A to B, advances physical count, and returns to A. A's configuration is preserved even though its derived count has increased. Test the exact-deadline boundary on return as well. Disabling B's timer must not disable A's timer. Equal initial values in both VMs hide swapped contexts, so choose distinct offsets, deadlines, and masks. Such contrasting values reveal ownership mistakes more effectively than replaying the same configuration twice and observing a superficially plausible output.
| Operation | Physical | Running VM | A offset/CVAL | A virtual/due |
|---|---|---|---|---|
| Start A | 120 | A | 100 / 25 | 20 / no |
| Switch to B | 120 | B | Preserve 100 / 25 | 20 / no |
| Advance count | 125 | B | Keep 100 / 25 | 25 / yes |
| Return to A | 125 | A | Restore 100 / 25 | 25 / yes |
EOI alone does not remove an expired timer condition
A virtual timer retains its level condition while the deadline is due and output controls permit signaling. EOI and deactivation can finish interrupt handling while CVAL remains in the past, allowing the request to become pending again. Repeating controller end operations does not repair the source. The handler must use an appropriate source action, such as rearming to a future deadline, masking, or disabling the timer. Record that action separately rather than hiding it inside a generic completion operation.
The demonstration shows source deassertion separately from clearing active state. Rearming can lower the source without automatically deactivating an acknowledged interrupt. Deactivation can clear active while an unresolved source still requests service. The timer and interrupt controller are connected but are not the same state machine. This separation explains both a retained active flag after rearming and a repeated request after deactivation. Neither observation is contradictory once each component's postcondition is written explicitly.
Test restoration with deliberately different contexts
The example stores each VM's configuration separately and derives virtual count from current physical count and the selected owner. Rearming A must not overwrite B's deadline. In a reference-based language, also check that both VM keys do not accidentally refer to the same mutable object. Sharing one configuration object at initialization can make a seemingly local rearm change the other guest. Distinct initial values make that aliasing error observable instead of letting identical outputs conceal it.
This code does not implement architectural save and restore instructions. It tests the ownership relationship among values that continuity requires. An implementation project must additionally specify guest-visible timer state, management storage, and the ordering of updates to the selected CPU context. Similar register names are not enough to define a complete context. Determine which state matters under the chosen configuration. Two fields can share a programming-language type while describing different domains, owners, or responsibilities, so a successful assignment is not proof that a restoration is semantically correct.
const contexts = new Map([
['A', {offset:100, cval:25, enabled:true, masked:false}],
['B', {offset:20, cval:110, enabled:true, masked:true}]
]);
const physical = 130;
const a = contexts.get('A');
const before = physical - a.offset >= a.cval;
a.cval = physical - a.offset + 10;
console.log(before, physical - a.offset >= a.cval, contexts.get('B').cval);
Choose the reference for periodic rearming
A periodic timer can schedule from current time plus a period or from the previous planned deadline plus a period. Scheduling from current time incorporates handling delay into the new origin and can shift phase. Scheduling from the previous deadline can retain phase, but after a long delay one increment may still leave the deadline in the past. Before diagnosing repeated signaling as a controller defect, calculate whether the new CVAL is actually later than the current virtual count.
For a planned deadline of 25, period 10, and handling at virtual count 48, moving the deadline to 35 is insufficient. Even 45 is already due; a policy that retains phase while selecting a future deadline would choose 55. Whether missed periods represent work that must be processed individually or coalesced is an application decision. The teaching model exposes the new CVAL and resulting level condition. It does not claim to implement compensation for every missed event merely because rearming clears the immediate source.
Repair ownership and conditions rather than the display
Hiding a context mix-up by rewriting only the displayed virtual count breaks comparison with CVAL or fails at the next switch. Repair which VM owns the offset and controls used for calculation. Similarly, printing a notification only once does not clear a persistent level source. Separate computed state from presentation and verify that regenerating the display from the repaired state produces the same correct result. A quiet message area alone is not evidence that the source condition has been resolved.
Useful tests include both A-to-B and B-to-A switches, returns before and after expiry, and different initial enable and mask values. Checking only the VM that completed handling can miss corruption of the other VM's settings. After repairing A, compare B's count, CVAL, and mask with their expected values as well. Establish ownership, deadline, and signal invariants on small examples before increasing the numerical range. Those independently checkable properties provide better diagnostic evidence than a single successful final screenshot.
Exercise: diagnose swapped context and missing rearm
The first task restores B's offset while returning to A. Repair the context selection and verify that B's own settings remain unchanged. The second task handles an expired timer with EOI and deactivation but no source action. Rearm to an appropriate future CVAL to clear the level, then complete the necessary interrupt-controller operations. Explain whether the repair corrected the clock origin or resolved the deadline condition. Both can affect delivery, but they repair different causes.
Vary physical starting count and switch positions for regression cases. Do not accept a solution only because A and B happen to show equal virtual counts in one example; use contrasting offsets to test ownership. For rearming, include both a small delay and a delay exceeding one period, checking numerically that the selected next deadline is in the future. The following block-I/O lessons use the same habit of tracking completed work separately from the source and delivery of its notification.