Skip to content

Commit 1dbaecb

Browse files
doge-rgbCMGS
authored andcommitted
vmm: acpi: provide FADT PM1a event/control blocks for nested Hyper-V
A Windows guest that launches nested Hyper-V (for example to run WSL2) fails to start its hypervisor on cloud-hypervisor's HW-reduced-ACPI FADT. hvloader's hypervisor-launch path (0x18000f01c -> 0x180015628 -> 0x180015788) registers every legacy PM register block via 0x1800158dc and rejects any block whose GAS address is 0 with status 8 (STATUS_INVALID_DEVICE_REQUEST). hvix64 then never launches and HypervisorPresent stays False. The HW-reduced FADT leaves those blocks zero. Emit valid PM1a event/control blocks (I/O ports, lengths and X_GAS) in the FADT and reserve those ports in the I/O allocator so nothing else claims them. The HW-reduced guest OS ignores the legacy ports; only hvloader's ACPI validation reads them. These blocks are only useful to a guest that itself runs an enlightened hypervisor, so emit them only when both guest nesting and the Hyper-V enlightenments are enabled (--cpu nested=on,kvm_hyperv=on). Signed-off-by: doge <me@crackerben.com>
1 parent 7117d05 commit 1dbaecb

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

vmm/src/acpi.rs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::time::Instant;
66

77
use acpi_tables::Aml;
8+
use acpi_tables::gas::{AccessSize, AddressSpace, GAS};
89
use acpi_tables::rsdp::Rsdp;
910
#[cfg(target_arch = "aarch64")]
1011
use acpi_tables::sdt::GenericAddress;
@@ -274,7 +275,11 @@ pub fn create_dsdt_table(
274275

275276
const FACP_DSDT_OFFSET: usize = 140;
276277

277-
fn create_facp_table(dsdt_offset: GuestAddress, device_manager: &DeviceManager) -> Sdt {
278+
fn create_facp_table(
279+
dsdt_offset: GuestAddress,
280+
device_manager: &DeviceManager,
281+
legacy_acpi_pm1a: bool,
282+
) -> Sdt {
278283
trace_scoped!("create_facp_table");
279284

280285
// Revision 6 of the ACPI FADT table is 276 bytes long
@@ -326,6 +331,50 @@ fn create_facp_table(dsdt_offset: GuestAddress, device_manager: &DeviceManager)
326331
// Hypervisor Vendor Identity
327332
facp.write_bytes(268, b"CLOUDHYP");
328333

334+
// Windows' nested-Hyper-V hvloader rejects a HW-reduced FADT whose PM1a GAS is
335+
// zero; point the blocks at unused ACPI I/O ports (conforming guests ignore them).
336+
if legacy_acpi_pm1a {
337+
const PM1A_EVT_PORT: u16 = 0x60c;
338+
facp.write(56usize, PM1A_EVT_PORT as u32); // PM1a_EVT_BLK (0x38)
339+
facp.write(88usize, 4u8); // PM1_EVT_LEN (0x58)
340+
facp.write_bytes(
341+
148usize, // X_PM1a_EVT_BLK (0x94)
342+
GAS::new(
343+
AddressSpace::SystemIo,
344+
32,
345+
0,
346+
AccessSize::WordAccess,
347+
PM1A_EVT_PORT.into(),
348+
)
349+
.as_bytes(),
350+
);
351+
352+
const PM1A_CNT_PORT: u16 = 0x610;
353+
facp.write(64usize, PM1A_CNT_PORT as u32); // PM1a_CNT_BLK (0x40)
354+
facp.write(89usize, 2u8); // PM1_CNT_LEN (0x59)
355+
facp.write_bytes(
356+
172usize, // X_PM1a_CNT_BLK (0xac)
357+
GAS::new(
358+
AddressSpace::SystemIo,
359+
16,
360+
0,
361+
AccessSize::WordAccess,
362+
PM1A_CNT_PORT.into(),
363+
)
364+
.as_bytes(),
365+
);
366+
367+
let mut allocator = device_manager.allocator().lock().unwrap();
368+
for (port, len) in [(PM1A_EVT_PORT, 4u64), (PM1A_CNT_PORT, 2u64)] {
369+
if allocator
370+
.allocate_io_addresses(Some(GuestAddress(port.into())), len, None)
371+
.is_none()
372+
{
373+
warn!("Could not reserve PM1a I/O port {port:#x} advertised in the FADT");
374+
}
375+
}
376+
}
377+
329378
facp.update_checksum();
330379

331380
facp
@@ -855,7 +904,12 @@ fn create_acpi_tables_internal(
855904
tables_bytes.extend_from_slice(dsdt.as_slice());
856905

857906
// FACP aka FADT
858-
let facp = create_facp_table(dsdt_addr, device_manager);
907+
// The legacy PM1a blocks are x86-only; there is nothing to emit on other arches.
908+
#[cfg(target_arch = "x86_64")]
909+
let legacy_acpi_pm1a = cpu_manager.nested() && cpu_manager.kvm_hyperv();
910+
#[cfg(not(target_arch = "x86_64"))]
911+
let legacy_acpi_pm1a = false;
912+
let facp = create_facp_table(dsdt_addr, device_manager, legacy_acpi_pm1a);
859913
let facp_addr = dsdt_addr.checked_add(dsdt.len() as u64).unwrap();
860914
tables_bytes.extend_from_slice(facp.as_slice());
861915
xsdt_table_pointers.push(facp_addr.0);
@@ -1130,7 +1184,12 @@ pub fn create_acpi_tables_tdx(
11301184
)];
11311185

11321186
// FACP aka FADT
1133-
tables.push(create_facp_table(GuestAddress(0), device_manager));
1187+
let legacy_acpi_pm1a = cpu_manager.nested() && cpu_manager.kvm_hyperv();
1188+
tables.push(create_facp_table(
1189+
GuestAddress(0),
1190+
device_manager,
1191+
legacy_acpi_pm1a,
1192+
));
11341193

11351194
// MADT
11361195
tables.push(cpu_manager.create_madt());

vmm/src/cpu.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,16 @@ impl CpuManager {
10891089
Ok(())
10901090
}
10911091

1092+
#[cfg(target_arch = "x86_64")]
1093+
pub fn nested(&self) -> bool {
1094+
self.config.nested
1095+
}
1096+
1097+
#[cfg(target_arch = "x86_64")]
1098+
pub fn kvm_hyperv(&self) -> bool {
1099+
self.config.kvm_hyperv
1100+
}
1101+
10921102
/// Only create new vCPUs if there aren't any inactive ones to reuse
10931103
fn create_vcpus(
10941104
&mut self,

0 commit comments

Comments
 (0)