Skip to content

Commit 1173dca

Browse files
authored
Merge pull request #179 from junyu0312/dev
feat: Fix acpi and cpuid
2 parents cfca91f + f7146e3 commit 1173dca

5 files changed

Lines changed: 50 additions & 19 deletions

File tree

crates/vm-core/src/virtualization/kvm/vcpu.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::virtualization::vcpu::command::VcpuCommandRequest;
2121
use crate::virtualization::vcpu::command::VcpuCommandResponse;
2222
use crate::virtualization::vcpu::error::VcpuError;
2323

24+
#[cfg(target_arch = "x86_64")]
25+
mod cpu_id;
2426
mod vm_exit;
2527

2628
pub struct KvmVcpuInternal<'a> {
@@ -57,7 +59,17 @@ impl KvmVcpu {
5759
) -> Result<Self, VcpuError> {
5860
let mut vcpu_fd = vm_fd.create_vcpu(vcpu_id)?;
5961
#[cfg(target_arch = "x86_64")]
60-
vcpu_fd.set_cpuid2(supported_cpuid)?;
62+
{
63+
use crate::virtualization::kvm::vcpu::cpu_id::update_cpuid;
64+
65+
let cpuid = update_cpuid(
66+
supported_cpuid,
67+
vcpu_id
68+
.try_into()
69+
.map_err(|_| VcpuError::UpdateCpuid("vcpu_id too large"))?,
70+
);
71+
vcpu_fd.set_cpuid2(&cpuid)?;
72+
}
6173

6274
let (command_tx, mut command_rx) = mpsc::channel(8);
6375
let is_running = Arc::new(AtomicBool::new(false));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use kvm_bindings::CpuId;
2+
3+
pub fn update_cpuid(cpuid: &CpuId, vcpu_id: u8) -> CpuId {
4+
let mut cpuid = cpuid.clone();
5+
6+
for entry in cpuid.as_mut_slice() {
7+
match entry.function {
8+
// Version and Features
9+
0x01 => {
10+
entry.ebx &= 0xffffff;
11+
// Update INITIAL_APIC_ID
12+
entry.ebx |= (vcpu_id as u32) << 24;
13+
}
14+
_ => continue,
15+
}
16+
}
17+
18+
cpuid
19+
}

crates/vm-core/src/virtualization/vcpu/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use crate::cpu::vm_exit::VmExitHandlerError;
44

55
#[derive(Error, Debug)]
66
pub enum VcpuError {
7+
#[error("Failed to update cpuid, err: {0}")]
8+
UpdateCpuid(&'static str),
9+
710
#[error("Vcpu command channel disconnected")]
811
VcpuCommandDisconnected,
912

crates/vm-device/src/device/dummy.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ pub struct Dummy;
99

1010
impl Dummy {
1111
pub fn new(pio_allocator: &mut RangeAllocator<u16>) -> Result<Self, DeviceError> {
12-
let _ = pio_allocator.reserve(0x1004, 1)?;
13-
let _ = pio_allocator.reserve(0x1006, 1)?;
1412
let _ = pio_allocator.reserve(0x87, 1)?;
1513

1614
Ok(Dummy)
@@ -33,12 +31,11 @@ impl Device for Dummy {
3331

3432
impl PioDevice for Dummy {
3533
fn ports(&self) -> Vec<Range<u16>> {
34+
let range = 0x87..0x88;
35+
3636
vec![
37-
// acpi pm1a
38-
0x1004..0x1005,
39-
0x1006..0x1007,
40-
// TODO
41-
0x87..0x88,
37+
// TODO: What's this
38+
range,
4239
]
4340
}
4441

crates/vm-firmware/src/acpi/type/fadt.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ use crate::acpi::r#type::common_header::CommonHeader;
1414
use crate::acpi::r#type::generic_address_structure_format::GenericAddressStructureFormat;
1515
use crate::acpi::utils::checksum;
1616

17+
// A zero indicates the power button is handled as a fixed feature programming model;
18+
// a one indicates the power button is handled as a control method device.
19+
// If the system does not have a power button, this value would be “1” and no power button device would be present.
20+
const ACPI_FADT_POWER_BUTTON: u32 = 1 << 4; /* 04: [V1] Power button is handled as a control method device */
21+
// A zero indicates the sleep button is handled as a fixed feature programming model;
22+
// a one indicates the sleep button is handled as a control method device.
23+
// If the system does not have a sleep button, this value would be “1” and no sleep button device would be present.
24+
const ACPI_FADT_SLEEP_BUTTON: u32 = 1 << 5; /* 05: [V1] Sleep button is handled as a control method device */
25+
const FADT_F_HW_REDUCED_ACPI: u32 = 1 << 20; /* 20: [V5] ACPI hardware is not implemented (ACPI 5.0) */
26+
1727
#[derive(Default, Immutable, IntoBytes)]
1828
#[repr(C, packed)]
1929
pub struct Fadt {
@@ -89,20 +99,10 @@ impl Fadt {
8999
creator_id: CREATOR_ID,
90100
creator_revision: CREATOR_REVISION,
91101
},
92-
flags: 0,
102+
flags: ACPI_FADT_POWER_BUTTON | ACPI_FADT_SLEEP_BUTTON | FADT_F_HW_REDUCED_ACPI,
93103
fadt_minor_version: 5, // ACPI 6.6 specification says it is 5.
94104
x_dsdt,
95105
hypervisor_vendor_id: HYPERVISOR_VENDOR_ID,
96-
// TODO
97-
pm1a_cnt_blk: 0x1000,
98-
// TODO
99-
pm1_evt_len: 16,
100-
// TODO
101-
pm1a_evt_blk: 0x1004,
102-
// TODO
103-
pm1_cnt_len: 32,
104-
// TODO
105-
sci_int: 9,
106106
..Default::default()
107107
};
108108

0 commit comments

Comments
 (0)