From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 008501FF0BC for ; Wed, 02 Dec 2026 18:46:24 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 602DB21779; Wed, 02 Dec 2026 18:46:19 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v4 3/7] cpu config: add hv-reenlightenment flag and set TSC frequency for it Date: Wed, 2 Dec 2026 18:45:20 +0100 Message-ID: <20261202174530.565487-4-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202174530.565487-1-j.theisen@proxmox.com> References: <20261202174530.565487-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796233546612 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.053 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: A24P73PMYSYGHUUWGYP5WQCMTEM3NSTM X-Message-ID-Hash: A24P73PMYSYGHUUWGYP5WQCMTEM3NSTM X-MailFrom: j.theisen@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Re-enlightenment notifications, together with the TSC frequency MSRs of 'hv-frequencies', let nested Hyper-V guests, for example Windows with WSL2 or VBS enabled, pass a stable clock source on to their own guests across live migrations [0]. Without the frequency MSRs, guests do not use the notifications, for example Linux then reports "reenlightenment support is unavailable". This patch does not enable 'hv-frequencies', but custom CPU models can. KVM does not emulate the old TSC frequency after a migration, so QEMU refuses to load the migration stream of a guest that enabled these notifications if no TSC frequency was set explicitly. Set it to the host's TSC frequency, as reported by KVM, whenever the flag is enabled for the VM or by a custom CPU model. Do not set it for confidential VMs. These cannot be live migrated, snapshotted with RAM or hibernated, so it would be of no use, and QEMU refuses to start TDX VMs with a TSC frequency that is not a multiple of 25 MHz. Treat the CPU configuration as abstracted if the flag is set, so that the TSC frequency of the running instance is kept for live migration, snapshots with RAM and hibernation, as the host a VM is resumed on can have a different frequency. [0]: https://gitlab.com/qemu-project/qemu/-/blob/v11.1.0/docs/system/i386/hyperv.rst?plain=1#L145 Signed-off-by: Jonas Theisen --- src/PVE/QemuServer/CPUConfig.pm | 32 +++++++++++++++++++ src/PVE/QemuServer/CPUFlags.pm | 8 +++++ src/test/QemuServerTest/CommandLineMocks.pm | 6 ++++ .../cpu-custom-hv-reenlightenment.conf | 6 ++++ .../cpu-custom-hv-reenlightenment.conf.cmd | 26 +++++++++++++++ .../cpu-hv-reenlightenment-disabled.conf | 6 ++++ .../cpu-hv-reenlightenment-disabled.conf.cmd | 26 +++++++++++++++ src/test/cfg2cmd/cpu-hv-reenlightenment.conf | 6 ++++ .../cfg2cmd/cpu-hv-reenlightenment.conf.cmd | 26 +++++++++++++++ src/test/cfg2cmd/tdx-hv-reenlightenment.conf | 9 ++++++ .../cfg2cmd/tdx-hv-reenlightenment.conf.cmd | 30 +++++++++++++++++ 11 files changed, 181 insertions(+) create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm index 689bfe89..3dde24a6 100644 --- a/src/PVE/QemuServer/CPUConfig.pm +++ b/src/PVE/QemuServer/CPUConfig.pm @@ -718,6 +718,8 @@ determined from the configuration alone. Possible abstractions: =item abstract flag: for example, nested-virt changes with the host +=item host-dependent setting: for example, the TSC frequency set for hv-reenlightenment + =back =cut @@ -732,6 +734,7 @@ sub is_abstracted($cpu_property_string) { for my $flag (split(";", $cpu_conf->{flags})) { if ($flag =~ $cpu_flag_supported_re) { return 1 if $2 eq 'nested-virt'; + return 1 if $2 eq 'hv-reenlightenment' && $1 eq '+'; } } return; @@ -861,6 +864,19 @@ my sub check_phys_bits_above_40_compat($bios, $cpu_type, $cpu_flags) { } } +=head3 get_tsc_frequency_flags + + my @flags = get_tsc_frequency_flags(); + +Returns the CPU flags for which the TSC frequency of the VM is set to the one of the host it is +started on, so that QEMU can migrate and save the state of VMs using these flags. + +=cut + +sub get_tsc_frequency_flags() { + return qw(hv-reenlightenment); +} + # Calculate QEMU's '-cpu' argument from a given VM configuration sub get_cpu_options( $conf, $arch, $kvm, $kvm_off, $version_guard, $winversion, $gpu_passthrough, @@ -965,6 +981,22 @@ sub get_cpu_options( $vm_flags, $pve_forced_flags, ); + + # Without an explicit TSC frequency, QEMU refuses to migrate a guest that enabled + # re-enlightenment notifications, as KVM does not emulate the old frequency afterwards. + # Confidential VMs cannot be migrated or have their state saved anyway, and TDX only accepts + # multiples of 25 MHz. + my @tsc_frequency_flags = grep { + my $flag = $resolved_flags->{$_}; + $flag && ($flag->{op} // '') ne '-'; + } get_tsc_frequency_flags(); + if ($kvm && $arch eq 'x86_64' && !get_cvm_type($conf) && scalar(@tsc_frequency_flags)) { + my $tsc_khz = PVE::QemuServer::Helpers::get_host_tsc_info()->{khz} + or die "unable to determine the host TSC frequency required for " + . join(' and ', map { "'$_'" } @tsc_frequency_flags) . "\n"; + $resolved_flags->{'tsc-frequency'} = { op => '', value => $tsc_khz * 1000 }; + } + $cpu_str .= print_cpu_flags($resolved_flags); my $using_phys_bits_above_40; diff --git a/src/PVE/QemuServer/CPUFlags.pm b/src/PVE/QemuServer/CPUFlags.pm index 27b64da7..ef1f19d9 100644 --- a/src/PVE/QemuServer/CPUFlags.pm +++ b/src/PVE/QemuServer/CPUFlags.pm @@ -77,6 +77,14 @@ my $supported_vm_specific_cpu_flags_by_arch = { description => "Improve performance for nested virtualization. Only supported on Intel" . " CPUs.", }, + { + name => 'hv-reenlightenment', + description => + "Lets nested Hyper-V, for example with WSL2 or VBS, keep a stable clock for its" + . " guests across live migrations if 'hv-frequencies' is enabled via a custom CPU" + . " model. Sets the VM's TSC frequency, so live migration needs TSC scaling or a" + . " matching TSC frequency on the target.", + }, { name => 'aes', description => "Activate AES instruction set for HW acceleration.", diff --git a/src/test/QemuServerTest/CommandLineMocks.pm b/src/test/QemuServerTest/CommandLineMocks.pm index 7fd4cc23..c1462327 100644 --- a/src/test/QemuServerTest/CommandLineMocks.pm +++ b/src/test/QemuServerTest/CommandLineMocks.pm @@ -395,6 +395,9 @@ $qemu_server_helpers->mock( get_host_phys_address_bits => sub { return 46; }, + get_host_tsc_info => sub { + return { khz => 2999999, scaling => 1 }; + }, kvm_user_version => sub { return get_test_qemu_version(); }, @@ -480,6 +483,9 @@ cpu-model: qemu64 cpu-model: alldefault +cpu-model: reenlightenment + flags +hv-reenlightenment + EOF ); }, diff --git a/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf new file mode 100644 index 00000000..b23d3222 --- /dev/null +++ b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf @@ -0,0 +1,6 @@ +# TEST: Test that a custom CPU model enabling hv-reenlightenment sets the VM's TSC frequency to the host's +cpu: custom-reenlightenment +memory: 768 +numa: 0 +ostype: win11 +sockets: 1 diff --git a/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd new file mode 100644 index 00000000..3b822df6 --- /dev/null +++ b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd @@ -0,0 +1,26 @@ +/usr/bin/kvm \ + -id 8006 \ + -name vm8006 \ + -no-shutdown \ + -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \ + -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \ + -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \ + -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \ + -pidfile /var/run/qemu-server/8006.pid \ + -daemonize \ + -smp '1,sockets=1,cores=1,maxcpus=1' \ + -nodefaults \ + -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \ + -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \ + -global 'kvm-pit.lost_tick_policy=discard' \ + -cpu 'kvm64,-cet-ibt,-cet-ss,enforce,+hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+lahf_lm,+sep,tsc-frequency=2999999000' \ + -m 768 \ + -device 'pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e' \ + -device 'pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f' \ + -device 'piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2' \ + -device 'usb-tablet,id=tablet,bus=uhci.0,port=1' \ + -device 'VGA,id=vga,bus=pci.0,addr=0x2,edid=off' \ + -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \ + -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \ + -rtc 'driftfix=slew,base=localtime' \ + -machine 'hpet=off,type=pc-i440fx-5.1+pve0' diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf new file mode 100644 index 00000000..ebf69ea5 --- /dev/null +++ b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf @@ -0,0 +1,6 @@ +# TEST: Test that disabling hv-reenlightenment does not set the TSC frequency +cpu: x86-64-v2-AES,flags=-hv-reenlightenment +memory: 768 +numa: 0 +ostype: win11 +sockets: 1 diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd new file mode 100644 index 00000000..e0c70911 --- /dev/null +++ b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd @@ -0,0 +1,26 @@ +/usr/bin/kvm \ + -id 8006 \ + -name vm8006 \ + -no-shutdown \ + -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \ + -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \ + -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \ + -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \ + -pidfile /var/run/qemu-server/8006.pid \ + -daemonize \ + -smp '1,sockets=1,cores=1,maxcpus=1' \ + -nodefaults \ + -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \ + -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \ + -global 'kvm-pit.lost_tick_policy=discard' \ + -cpu 'qemu64,+aes,-cet-ibt,-cet-ss,enforce,-hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3' \ + -m 768 \ + -device 'pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e' \ + -device 'pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f' \ + -device 'piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2' \ + -device 'usb-tablet,id=tablet,bus=uhci.0,port=1' \ + -device 'VGA,id=vga,bus=pci.0,addr=0x2,edid=off' \ + -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \ + -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \ + -rtc 'driftfix=slew,base=localtime' \ + -machine 'hpet=off,type=pc-i440fx-5.1+pve0' diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment.conf b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf new file mode 100644 index 00000000..b5f1814f --- /dev/null +++ b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf @@ -0,0 +1,6 @@ +# TEST: Test that enabling hv-reenlightenment sets the VM's TSC frequency to the host's +cpu: x86-64-v2-AES,flags=+hv-reenlightenment +memory: 768 +numa: 0 +ostype: win11 +sockets: 1 diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd new file mode 100644 index 00000000..c3d8e49c --- /dev/null +++ b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd @@ -0,0 +1,26 @@ +/usr/bin/kvm \ + -id 8006 \ + -name vm8006 \ + -no-shutdown \ + -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \ + -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \ + -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \ + -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \ + -pidfile /var/run/qemu-server/8006.pid \ + -daemonize \ + -smp '1,sockets=1,cores=1,maxcpus=1' \ + -nodefaults \ + -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \ + -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \ + -global 'kvm-pit.lost_tick_policy=discard' \ + -cpu 'qemu64,+aes,-cet-ibt,-cet-ss,enforce,+hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3,tsc-frequency=2999999000' \ + -m 768 \ + -device 'pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e' \ + -device 'pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f' \ + -device 'piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2' \ + -device 'usb-tablet,id=tablet,bus=uhci.0,port=1' \ + -device 'VGA,id=vga,bus=pci.0,addr=0x2,edid=off' \ + -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \ + -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \ + -rtc 'driftfix=slew,base=localtime' \ + -machine 'hpet=off,type=pc-i440fx-5.1+pve0' diff --git a/src/test/cfg2cmd/tdx-hv-reenlightenment.conf b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf new file mode 100644 index 00000000..883802f6 --- /dev/null +++ b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf @@ -0,0 +1,9 @@ +# TEST: Test that hv-reenlightenment does not set the TSC frequency for TDX +# HW_CAPABILITIES: {"intel-tdx":{"tdx-support":true}} +bios: ovmf +cpu: x86-64-v2-AES,flags=+hv-reenlightenment +intel-tdx: type=tdx +machine: q35 +ostype: l26 +smbios1: uuid=7b10d7af-b932-4c66-b2c3-3996152ec465 +vmgenid: c773c261-d800-4348-9f5d-167fadd53cf8 diff --git a/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd new file mode 100644 index 00000000..5300ef2d --- /dev/null +++ b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd @@ -0,0 +1,30 @@ +/usr/bin/kvm \ + -id 8006 \ + -name vm8006 \ + -no-shutdown \ + -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \ + -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \ + -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \ + -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \ + -pidfile /var/run/qemu-server/8006.pid \ + -daemonize \ + -smbios 'type=1,uuid=7b10d7af-b932-4c66-b2c3-3996152ec465' \ + -bios /usr/share/pve-edk2-firmware//OVMF_TDX_4M.ms.fd \ + -global 'ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off' \ + -smp '1,sockets=1,cores=1,maxcpus=1' \ + -nodefaults \ + -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \ + -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \ + -cpu qemu64,+aes,enforce,+hv-reenlightenment,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3 \ + -m 512 \ + -global 'ICH9-LPC.disable_s3=1' \ + -global 'ICH9-LPC.disable_s4=1' \ + -readconfig /usr/share/qemu-server/pve-q35-4.0.cfg \ + -device 'pci-bridge,id=pci.4,chassis_nr=4,bus=pci.1,addr=0x1c' \ + -device 'vmgenid,guid=c773c261-d800-4348-9f5d-167fadd53cf8' \ + -device 'usb-tablet,id=tablet,bus=ehci.0,port=1' \ + -device 'VGA,id=vga,bus=pcie.0,addr=0x1' \ + -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3,free-page-reporting=on' \ + -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \ + -object '{"id":"tdx0","qom-type":"tdx-guest","quote-generation-socket":{"cid":"2","port":"4050","type":"vsock"}}' \ + -machine 'hpet=off,type=q35+pve0,confidential-guest-support=tdx0,kernel_irqchip=split' -- 2.47.3