Skip to content

Commit 9e138af

Browse files
authored
Merge branch 'feature/microvm/dev' into cpu-profile/nvidia-n1x-whp
2 parents 229ecb9 + 1578666 commit 9e138af

20 files changed

Lines changed: 2050 additions & 111 deletions

File tree

‎Guide/src/reference/architecture/timekeeping.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ API is easiest to call.
126126
| --- | --- |
127127
| PIT | `chipset::pit` advances counting elements from elapsed `VmTime` and schedules wakeups for interrupt generation. |
128128
| ACPI PM timer | Software reads scale `VmTime` to 3.579545 MHz. With PM timer assist, the hypervisor handles reads using its own time source instead. |
129-
| CMOS RTC | Calendar registers use `LocalClock`; alarm, periodic, and update interrupt scheduling uses `VmTime`. |
129+
| CMOS RTC | Calendar registers use `LocalClock`; alarm, periodic, and update interrupt scheduling uses `VmTime`. Saved state keeps the interrupt deadlines. |
130130
| UEFI watchdog | `watchdog_core` receives a `VmTimeAccess`, so its deadline belongs to the software VM timeline. |
131131
| Software local APIC | `virt_support_apic` counts down using `VmTime`; backend-native APIC emulation instead has backend-owned timer state. |
132132
| Hyper-V synthetic timers | Deadlines use partition reference time. Native implementations belong to the hypervisor; `hv1_emulator::synic` evaluates them in software. |
@@ -164,6 +164,20 @@ created clock instances do not share guest-written offsets.
164164
RTC dates can advance during a pause while `VmTime`-based interrupt
165165
machinery is stopped. Periodic interrupt rates do not define wall time.
166166

167+
A snapshot instead keeps the RTC's time at its VM-time cut. While stopped,
168+
the RTC holds the time that it read as it stopped and runs it with
169+
`VmTime`, so a save records the time at the cut; a restore, and the
170+
downtime that it advances, set the clock as the RTC starts, and a plain
171+
pause sets nothing. The saved state also keeps the alarm, periodic, and
172+
update deadlines in `VmTime` and the time of the last update cycle, so a
173+
restore keeps each interrupt's phase and the update-in-progress window.
174+
A restore rejects a deadline more than its timer's period after the
175+
restored VM time; a saved state without deadlines arms the enabled
176+
timers a period after the restore. A restore, or the downtime that it
177+
advances, also rejects a time beyond the calendar's years 0 to 9999 by
178+
more than the Linux host clock's range of about 292 years, so that the
179+
`SystemTimeClock` offset cannot overflow.
180+
167181
On Arm, the UEFI time service exposes the supplied local clock through
168182
`GetTime`/`SetTime`. It retains timezone and daylight fields for readback
169183
but does not apply them as timezone conversions. Its saved state stores

‎Guide/src/user_guide/openvmm/snapshots.md‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ fails. VM time then runs from the instant at which the TSCs run again, rather
366366
than from the later start of the VM: the restore anchor on KVM, and on MSHV and
367367
WHP, which write the TSCs with partition time frozen, the instant at which
368368
partition time resumes. The time that the restore takes to start the VM thus
369-
does not shift the emulated devices' timers relative to the TSC.
369+
does not shift the emulated devices' timers relative to the TSC. The RTC
370+
keeps its time at the cut, and its alarm, periodic, and update interrupts
371+
keep their deadlines in VM time, so they keep their phase across a restore.
370372
Snapshots without the explicit capture-time `maxcpus` opt-in reject a restore
371373
target. This is not a post-readiness hotplug API and cannot add VPs absent
372374
from the saved topology.
@@ -484,8 +486,20 @@ generation, because a host profile derives from what the backend supports.
484486
A snapshot records the profile and the
485487
effective CPUID. Restore requires the same profile, pinned in this OpenVMM with
486488
the same digest, or, for a host profile, the snapshot's own copy of it, which
487-
must be a valid profile, its CPU time bits included, with the recorded digest;
489+
must be a valid profile, its CPU time bits included, with the recorded digest,
490+
and must follow the derivation policy of this OpenVMM (`E_PROFILE_DIGEST`);
488491
a host of the profile's generation; and an identical effective CPUID.
492+
The derivation policy decides which features a derived profile withholds from
493+
the guest, virtualization (VMX and SVM) among them. OpenVMM applies the policy's
494+
rules to a host profile's document and refuses a profile that they would change,
495+
such as a document that someone edited to give the guest VMX or SVM and recorded
496+
with its own digest; the error names at most eight of the differences. It
497+
applies the rules of this build, whatever policy the profile's provenance names,
498+
so a build whose policy differs from the capturing build's also refuses a host
499+
profile that the capturing build derived and its own policy would change, such
500+
as one with a feature that the newer policy withholds. A pinned profile needs no
501+
such check, because OpenVMM holds it as a constant, and an earlier revision
502+
stays pinned.
489503
OpenVMM computes the effective CPUID, including its own leaves and the bits
490504
it owns, such as the cache topology, so an OpenVMM build that computes it
491505
differently rejects earlier snapshots with `E_CPU_SURFACE`. That error names
@@ -690,6 +704,8 @@ immediately with a clear error if any active device does not support it.
690704
the capture, and an OpenVMM build that computes the same effective CPUID.
691705
A host profile's snapshot, including one of a cold boot whose `auto` fell
692706
back to a host profile, restores only on a host of the capture host's CPU
693-
model and stepping whose backend supports the profile.
694-
Snapshots from a build that computes a different effective CPUID must be
695-
recaptured.
707+
model and stepping whose backend supports the profile, and only on a build
708+
whose derivation policy leaves the recorded profile unchanged.
709+
Snapshots from a build that computes a different effective CPUID, or, for a
710+
host profile, whose policy differs in a way that changes the recorded
711+
profile, must be recaptured.

‎openvmm/openvmm_core/src/worker/dispatch/time_abi.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,30 @@ mod tests {
30693069
error_code(select(profile.id(), &host, Some(&swapped))),
30703070
ProfileErrorCode::ProfileDigest
30713071
);
3072+
// A host profile that gives the guest VMX, which the derivation always
3073+
// clears, recorded with its own digest, as whoever crafts the snapshot
3074+
// can record it.
3075+
let [eax, ebx, ecx, edx] = profile.lookup(1, 0);
3076+
let leaf1 = |ecx: u32| {
3077+
format!("\"value\":[\"{eax:#010x}\",\"{ebx:#010x}\",\"{ecx:#010x}\",\"{edx:#010x}\"]")
3078+
};
3079+
let document = String::from_utf8(profile.encode()).unwrap();
3080+
assert!(document.contains(&leaf1(ecx)));
3081+
let crafted = CpuProfile::decode(
3082+
document
3083+
.replace(&leaf1(ecx), &leaf1(ecx | 1 << 5))
3084+
.as_bytes(),
3085+
)
3086+
.unwrap();
3087+
let vmx = host_profile_record(&crafted, TEST_SIGNATURE);
3088+
let error = select(crafted.id(), &host, Some(&vmx)).unwrap_err();
3089+
assert_eq!(error.code, ProfileErrorCode::ProfileDigest, "{error}");
3090+
assert!(
3091+
error
3092+
.message
3093+
.contains("CPUID 0x1 ECX bit 5 is set, which the policy clears"),
3094+
"{error}"
3095+
);
30723096
// A pinned profile's snapshot needs no record: its ID selects it.
30733097
let mut pinned_snapshot = swapped.clone();
30743098
pinned_snapshot.id = TEST_PROFILE.to_owned();

‎openvmm/openvmm_entry/src/cli_args.rs‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,25 @@ use thiserror::Error;
5151
/// have a larger stack.
5252
fn on_big_stack<R: Send>(f: impl Send + FnOnce() -> R) -> R {
5353
if cfg!(windows) {
54-
std::thread::scope(|s| {
55-
std::thread::Builder::new()
56-
.stack_size(0x400000)
57-
.spawn_scoped(s, f)
58-
.unwrap()
59-
.join()
60-
.unwrap()
61-
})
54+
with_big_stack(f)
6255
} else {
6356
f()
6457
}
6558
}
6659

60+
/// Runs `f` on a thread with a 4 MiB stack, enough for clap to construct the
61+
/// `Command` instance for `Options` in debug builds.
62+
fn with_big_stack<R: Send>(f: impl Send + FnOnce() -> R) -> R {
63+
std::thread::scope(|s| {
64+
std::thread::Builder::new()
65+
.stack_size(0x400000)
66+
.spawn_scoped(s, f)
67+
.unwrap()
68+
.join()
69+
.unwrap()
70+
})
71+
}
72+
6773
/// Parse CLI options, using a thread with a larger stack on Windows to avoid
6874
/// stack overflow in debug builds due to clap's deep stack usage.
6975
/// See <https://github.com/clap-rs/clap/issues/5134>.
@@ -74,9 +80,13 @@ pub(crate) fn parse_options() -> Options {
7480
/// Returns the options of a command line that selects the microVM machine
7581
/// profile and nothing else, with every other option at its default, which
7682
/// the management RPC sets from a request.
83+
///
84+
/// The management RPC runs on a worker thread, whose default stack of 2 MiB
85+
/// is too small for clap in debug builds on every host, so the options are
86+
/// always parsed on a thread with a big stack.
7787
#[cfg(any(feature = "ttrpc", feature = "grpc"))]
7888
pub(crate) fn microvm_options() -> anyhow::Result<Options> {
79-
on_big_stack(|| Options::try_parse_from(["openvmm", "--machine", "microvm"]))
89+
with_big_stack(|| Options::try_parse_from(["openvmm", "--machine", "microvm"]))
8090
.context("failed to build the microVM command-line options")
8191
}
8292

‎openvmm/openvmm_helpers/src/snapshot/time.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,37 @@ pub(super) mod tests {
949949
code(result(&swapped, "auto", TEST_SIGNATURE)),
950950
TimeAbiCode::ProfileDigest
951951
);
952+
// A host profile that gives the guest VMX, which the derivation
953+
// always clears, recorded with its own digest and the effective CPUID
954+
// that it gives, as whoever crafts the snapshot can record them.
955+
let [eax, ebx, ecx, edx] = profile.lookup(1, 0);
956+
let leaf1 = |ecx: u32| {
957+
format!("\"value\":[\"{eax:#010x}\",\"{ebx:#010x}\",\"{ecx:#010x}\",\"{edx:#010x}\"]")
958+
};
959+
let document = String::from_utf8(profile.encode()).unwrap();
960+
assert!(document.contains(&leaf1(ecx)));
961+
let crafted = cpu_profile::CpuProfile::decode(
962+
document
963+
.replace(&leaf1(ecx), &leaf1(ecx | 1 << 5))
964+
.as_bytes(),
965+
)
966+
.unwrap();
967+
let mut vmx = contract.clone();
968+
*vmx.cpu_profile.as_mut().unwrap() = SnapshotCpuProfile {
969+
id: crafted.id().to_owned(),
970+
sha256: crafted.digest().to_vec(),
971+
profile: crafted.encode(),
972+
effective_cpuid: effective_cpuid_record(&test_effective_cpuid(&crafted, 2)),
973+
capture_cpu_signature: TEST_SIGNATURE,
974+
};
975+
let error = result(&vmx, "auto", TEST_SIGNATURE).unwrap_err();
976+
assert_eq!(error.code, TimeAbiCode::ProfileDigest, "{error}");
977+
assert!(
978+
error
979+
.to_string()
980+
.contains("CPUID 0x1 ECX bit 5 is set, which the policy clears"),
981+
"{error}"
982+
);
952983
}
953984

954985
#[test]

0 commit comments

Comments
 (0)