hypervisor/
x86_64.rs

1// Copyright 2020 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#[cfg(any(unix, feature = "haxm", feature = "whpx"))]
6use std::arch::x86_64::__cpuid;
7use std::arch::x86_64::_rdtsc;
8use std::arch::x86_64::CpuidResult;
9use std::collections::BTreeMap;
10use std::collections::HashSet;
11use std::sync::Arc;
12
13use anyhow::Context;
14use base::custom_serde::deserialize_seq_to_arr;
15use base::custom_serde::serialize_arr;
16use base::error;
17use base::warn;
18use base::Result;
19use bit_field::*;
20use libc::c_void;
21use serde::Deserialize;
22use serde::Serialize;
23use snapshot::AnySnapshot;
24use vm_memory::GuestAddress;
25
26use crate::Hypervisor;
27use crate::IrqRoute;
28use crate::IrqSource;
29use crate::IrqSourceChip;
30use crate::NestedMode;
31use crate::Vcpu;
32use crate::Vm;
33
34const MSR_F15H_PERF_CTL0: u32 = 0xc0010200;
35const MSR_F15H_PERF_CTL1: u32 = 0xc0010202;
36const MSR_F15H_PERF_CTL2: u32 = 0xc0010204;
37const MSR_F15H_PERF_CTL3: u32 = 0xc0010206;
38const MSR_F15H_PERF_CTL4: u32 = 0xc0010208;
39const MSR_F15H_PERF_CTL5: u32 = 0xc001020a;
40const MSR_F15H_PERF_CTR0: u32 = 0xc0010201;
41const MSR_F15H_PERF_CTR1: u32 = 0xc0010203;
42const MSR_F15H_PERF_CTR2: u32 = 0xc0010205;
43const MSR_F15H_PERF_CTR3: u32 = 0xc0010207;
44const MSR_F15H_PERF_CTR4: u32 = 0xc0010209;
45const MSR_F15H_PERF_CTR5: u32 = 0xc001020b;
46const MSR_IA32_PERF_CAPABILITIES: u32 = 0x00000345;
47
48/// A trait for managing cpuids for an x86_64 hypervisor and for checking its capabilities.
49pub trait HypervisorX86_64: Hypervisor {
50    /// Get the system supported CPUID values.
51    fn get_supported_cpuid(&self) -> Result<CpuId>;
52
53    /// Gets the list of supported MSRs.
54    fn get_msr_index_list(&self) -> Result<Vec<u32>>;
55}
56
57/// A wrapper for using a VM on x86_64 and getting/setting its state.
58pub trait VmX86_64: Vm {
59    /// Gets the `HypervisorX86_64` that created this VM.
60    fn get_hypervisor(&self) -> &dyn HypervisorX86_64;
61
62    /// Create a Vcpu with the specified Vcpu ID.
63    fn create_vcpu(&self, id: usize) -> Result<Arc<dyn VcpuX86_64>>;
64
65    /// Sets the address of the three-page region in the VM's address space.
66    fn set_tss_addr(&self, addr: GuestAddress) -> Result<()>;
67
68    /// Sets the address of a one-page region in the VM's address space.
69    fn set_identity_map_addr(&self, addr: GuestAddress) -> Result<()>;
70
71    /// Load pVM firmware for the VM, creating a memslot for it as needed.
72    ///
73    /// Only works on protected VMs (i.e. those with vm_type == KVM_X86_PKVM_PROTECTED_VM).
74    fn load_protected_vm_firmware(&self, fw_addr: GuestAddress, fw_max_size: u64) -> Result<()>;
75}
76
77/// A wrapper around creating and using a VCPU on x86_64.
78pub trait VcpuX86_64: Vcpu {
79    /// Sets or clears the flag that requests the VCPU to exit when it becomes possible to inject
80    /// interrupts into the guest.
81    fn set_interrupt_window_requested(&self, requested: bool);
82
83    /// Checks if we can inject an interrupt into the VCPU.
84    fn ready_for_interrupt(&self) -> bool;
85
86    /// Injects interrupt vector `irq` into the VCPU.
87    ///
88    /// This function should only be called when [`Self::ready_for_interrupt`] returns true.
89    /// Otherwise the interrupt injection may fail or the next VCPU run may fail. However, if
90    /// [`Self::interrupt`] returns [`Ok`], the implementation must guarantee that the interrupt
91    /// isn't injected in an uninterruptible window (e.g. right after the mov ss instruction).
92    ///
93    /// The caller should avoid calling this function more than 1 time for one VMEXIT, because the
94    /// hypervisor may behave differently: some hypervisors(e.g. WHPX, KVM) will only try to inject
95    /// the last `irq` requested, while some other hypervisors(e.g. HAXM) may try to inject all
96    /// `irq`s requested.
97    fn interrupt(&self, irq: u8) -> Result<()>;
98
99    /// Injects a non-maskable interrupt into the VCPU.
100    fn inject_nmi(&self) -> Result<()>;
101
102    /// Gets the VCPU general purpose registers.
103    fn get_regs(&self) -> Result<Regs>;
104
105    /// Sets the VCPU general purpose registers.
106    fn set_regs(&self, regs: &Regs) -> Result<()>;
107
108    /// Gets the VCPU special registers.
109    fn get_sregs(&self) -> Result<Sregs>;
110
111    /// Sets the VCPU special registers.
112    fn set_sregs(&self, sregs: &Sregs) -> Result<()>;
113
114    /// Gets the VCPU FPU registers.
115    fn get_fpu(&self) -> Result<Fpu>;
116
117    /// Sets the VCPU FPU registers.
118    fn set_fpu(&self, fpu: &Fpu) -> Result<()>;
119
120    /// Gets the VCPU debug registers.
121    fn get_debugregs(&self) -> Result<DebugRegs>;
122
123    /// Sets the VCPU debug registers.
124    fn set_debugregs(&self, debugregs: &DebugRegs) -> Result<()>;
125
126    /// Gets the VCPU extended control registers.
127    fn get_xcrs(&self) -> Result<BTreeMap<u32, u64>>;
128
129    /// Sets a VCPU extended control register.
130    fn set_xcr(&self, xcr: u32, value: u64) -> Result<()>;
131
132    /// Gets the VCPU x87 FPU, MMX, XMM, YMM and MXCSR registers.
133    fn get_xsave(&self) -> Result<Xsave>;
134
135    /// Sets the VCPU x87 FPU, MMX, XMM, YMM and MXCSR registers.
136    fn set_xsave(&self, xsave: &Xsave) -> Result<()>;
137
138    /// Gets hypervisor specific state for this VCPU that must be
139    /// saved/restored for snapshotting.
140    /// This state is fetched after VCPUs are frozen and interrupts are flushed.
141    fn get_hypervisor_specific_state(&self) -> Result<AnySnapshot>;
142
143    /// Sets hypervisor specific state for this VCPU. Only used for
144    /// snapshotting.
145    fn set_hypervisor_specific_state(&self, data: AnySnapshot) -> Result<()>;
146
147    /// Gets a single model-specific register's value.
148    fn get_msr(&self, msr_index: u32) -> Result<u64>;
149
150    /// Gets the model-specific registers. Returns all the MSRs for the VCPU.
151    fn get_all_msrs(&self) -> Result<BTreeMap<u32, u64>>;
152
153    /// Sets a single model-specific register's value.
154    fn set_msr(&self, msr_index: u32, value: u64) -> Result<()>;
155
156    /// Sets up the data returned by the CPUID instruction.
157    fn set_cpuid(&self, cpuid: &CpuId) -> Result<()>;
158
159    /// Sets up debug registers and configure vcpu for handling guest debug events.
160    fn set_guest_debug(&self, addrs: &[GuestAddress], enable_singlestep: bool) -> Result<()>;
161
162    /// This function should be called after `Vcpu::run` returns `VcpuExit::Cpuid`, and `entry`
163    /// should represent the result of emulating the CPUID instruction. The `handle_cpuid` function
164    /// will then set the appropriate registers on the vcpu.
165    fn handle_cpuid(&self, entry: &CpuIdEntry) -> Result<()>;
166
167    /// Gets the guest->host TSC offset.
168    ///
169    /// The default implementation uses [`VcpuX86_64::get_msr()`] to read the guest TSC.
170    fn get_tsc_offset(&self) -> Result<u64> {
171        // SAFETY:
172        // Safe because _rdtsc takes no arguments
173        let host_before_tsc = unsafe { _rdtsc() };
174
175        // get guest TSC value from our hypervisor
176        let guest_tsc = self.get_msr(crate::MSR_IA32_TSC)?;
177
178        // SAFETY:
179        // Safe because _rdtsc takes no arguments
180        let host_after_tsc = unsafe { _rdtsc() };
181
182        // Average the before and after host tsc to get the best value
183        let host_tsc = ((host_before_tsc as u128 + host_after_tsc as u128) / 2) as u64;
184
185        Ok(guest_tsc.wrapping_sub(host_tsc))
186    }
187
188    /// Sets the guest->host TSC offset.
189    ///
190    /// The default implementation uses [`VcpuX86_64::set_tsc_value()`] to set the TSC value.
191    ///
192    /// It sets TSC_OFFSET (VMCS / CB field) by setting the TSC MSR to the current
193    /// host TSC value plus the desired offset. We rely on the fact that hypervisors
194    /// determine the value of TSC_OFFSET by computing TSC_OFFSET = `new_tsc_value - _rdtsc()` =
195    /// `_rdtsc() + offset - _rdtsc()` ~= `offset`. Note that the ~= is important: this is an
196    /// approximate operation, because the two _rdtsc() calls
197    /// are separated by at least a few ticks.
198    ///
199    /// Note: TSC_OFFSET, host TSC, guest TSC, and TSC MSR are all different
200    /// concepts.
201    /// * When a guest executes rdtsc, the value (guest TSC) returned is host_tsc * TSC_MULTIPLIER +
202    ///   TSC_OFFSET + TSC_ADJUST.
203    /// * The TSC MSR is a special MSR that when written to by the host, will cause TSC_OFFSET to be
204    ///   set accordingly by the hypervisor.
205    /// * When the guest *writes* to TSC MSR, it actually changes the TSC_ADJUST MSR *for the
206    ///   guest*. Generally this is only happens if the guest is trying to re-zero or synchronize
207    ///   TSCs.
208    fn set_tsc_offset(&self, offset: u64) -> Result<()> {
209        // SAFETY: _rdtsc takes no arguments.
210        let host_tsc = unsafe { _rdtsc() };
211        self.set_tsc_value(host_tsc.wrapping_add(offset))
212    }
213
214    /// Sets the guest TSC exactly to the provided value.
215    ///
216    /// The default implementation sets the guest's TSC by writing the value to the MSR directly.
217    ///
218    /// See [`VcpuX86_64::set_tsc_offset()`] for an explanation of how this value is actually read
219    /// by the guest after being set.
220    fn set_tsc_value(&self, value: u64) -> Result<()> {
221        self.set_msr(crate::MSR_IA32_TSC, value)
222    }
223
224    /// Some hypervisors require special handling to restore timekeeping when
225    /// a snapshot is restored. They are provided with a host TSC reference
226    /// moment, guaranteed to be the same across all Vcpus, and the Vcpu's TSC
227    /// offset at the moment it was snapshotted.
228    fn restore_timekeeping(&self, host_tsc_reference_moment: u64, tsc_offset: u64) -> Result<()>;
229
230    /// Snapshot vCPU state
231    fn snapshot(&self) -> anyhow::Result<VcpuSnapshot> {
232        Ok(VcpuSnapshot {
233            vcpu_id: self.id(),
234            regs: self.get_regs()?,
235            sregs: self.get_sregs()?,
236            debug_regs: self.get_debugregs()?,
237            xcrs: self.get_xcrs()?,
238            msrs: self.get_all_msrs()?,
239            xsave: self.get_xsave()?,
240            hypervisor_data: self.get_hypervisor_specific_state()?,
241            tsc_offset: self.get_tsc_offset()?,
242        })
243    }
244
245    fn restore(
246        &self,
247        snapshot: &VcpuSnapshot,
248        host_tsc_reference_moment: u64,
249    ) -> anyhow::Result<()> {
250        // List of MSRs that may fail to restore due to lack of support in the host kernel.
251        // Some hosts are may be running older kernels which do not support all MSRs, but
252        // get_all_msrs will still fetch the MSRs supported by the CPU. Trying to set those MSRs
253        // will result in failures, so they will throw a warning instead.
254        let msr_allowlist = HashSet::from([
255            MSR_F15H_PERF_CTL0,
256            MSR_F15H_PERF_CTL1,
257            MSR_F15H_PERF_CTL2,
258            MSR_F15H_PERF_CTL3,
259            MSR_F15H_PERF_CTL4,
260            MSR_F15H_PERF_CTL5,
261            MSR_F15H_PERF_CTR0,
262            MSR_F15H_PERF_CTR1,
263            MSR_F15H_PERF_CTR2,
264            MSR_F15H_PERF_CTR3,
265            MSR_F15H_PERF_CTR4,
266            MSR_F15H_PERF_CTR5,
267            MSR_IA32_PERF_CAPABILITIES,
268        ]);
269        assert_eq!(snapshot.vcpu_id, self.id());
270        self.set_regs(&snapshot.regs)?;
271        self.set_sregs(&snapshot.sregs)?;
272        self.set_debugregs(&snapshot.debug_regs)?;
273        for (xcr_index, value) in &snapshot.xcrs {
274            self.set_xcr(*xcr_index, *value)?;
275        }
276
277        for (msr_index, value) in snapshot.msrs.iter() {
278            if self.get_msr(*msr_index) == Ok(*value) {
279                continue; // no need to set MSR since the values are the same.
280            }
281            if let Err(e) = self.set_msr(*msr_index, *value) {
282                if msr_allowlist.contains(msr_index) {
283                    warn!(
284                        "Failed to set MSR. MSR might not be supported in this kernel. Err: {}",
285                        e
286                    );
287                } else {
288                    return Err(e).context(
289                        "Failed to set MSR. MSR might not be supported by the CPU or by the kernel,
290                         and was not allow-listed.",
291                    );
292                }
293            };
294        }
295        self.set_xsave(&snapshot.xsave)?;
296        self.set_hypervisor_specific_state(snapshot.hypervisor_data.clone())?;
297        self.restore_timekeeping(host_tsc_reference_moment, snapshot.tsc_offset)?;
298        Ok(())
299    }
300}
301
302/// x86 specific vCPU snapshot.
303#[derive(Clone, Debug, Serialize, Deserialize)]
304pub struct VcpuSnapshot {
305    pub vcpu_id: usize,
306    regs: Regs,
307    sregs: Sregs,
308    debug_regs: DebugRegs,
309    xcrs: BTreeMap<u32, u64>,
310    msrs: BTreeMap<u32, u64>,
311    xsave: Xsave,
312    hypervisor_data: AnySnapshot,
313    tsc_offset: u64,
314}
315
316// TSC MSR
317pub const MSR_IA32_TSC: u32 = 0x00000010;
318
319/// Gets host cpu max physical address bits.
320#[cfg(any(unix, feature = "haxm", feature = "whpx"))]
321pub(crate) fn host_phys_addr_bits() -> u8 {
322    // SAFETY: trivially safe
323    let highest_ext_function = unsafe { __cpuid(0x80000000) };
324    if highest_ext_function.eax >= 0x80000008 {
325        // SAFETY: trivially safe
326        let addr_size = unsafe { __cpuid(0x80000008) };
327        // Low 8 bits of 0x80000008 leaf: host physical address size in bits.
328        addr_size.eax as u8
329    } else {
330        36
331    }
332}
333
334/// Initial state for x86_64 VCPUs.
335#[derive(Clone, Default)]
336pub struct VcpuInitX86_64 {
337    /// General-purpose registers.
338    pub regs: Regs,
339
340    /// Special registers.
341    pub sregs: Sregs,
342
343    /// Floating-point registers.
344    pub fpu: Fpu,
345
346    /// Machine-specific registers.
347    pub msrs: BTreeMap<u32, u64>,
348}
349
350/// Hold the CPU feature configurations that are needed to setup a vCPU.
351#[derive(Clone, Debug, PartialEq, Eq)]
352pub struct CpuConfigX86_64 {
353    /// whether to force using a calibrated TSC leaf (0x15).
354    pub force_calibrated_tsc_leaf: bool,
355
356    /// whether enabling host cpu topology.
357    pub host_cpu_topology: bool,
358
359    /// whether expose HWP feature to the guest.
360    pub enable_hwp: bool,
361
362    /// Wheter diabling SMT (Simultaneous Multithreading).
363    pub no_smt: bool,
364
365    /// whether enabling ITMT scheduler
366    pub itmt: bool,
367
368    /// whether setting hybrid CPU type
369    pub hybrid_type: Option<CpuHybridType>,
370
371    /// how to expose nested virtualization to the guest.
372    pub nested: NestedMode,
373}
374
375impl CpuConfigX86_64 {
376    pub fn new(
377        force_calibrated_tsc_leaf: bool,
378        host_cpu_topology: bool,
379        enable_hwp: bool,
380        no_smt: bool,
381        itmt: bool,
382        hybrid_type: Option<CpuHybridType>,
383        nested: NestedMode,
384    ) -> Self {
385        CpuConfigX86_64 {
386            force_calibrated_tsc_leaf,
387            host_cpu_topology,
388            enable_hwp,
389            no_smt,
390            itmt,
391            hybrid_type,
392            nested,
393        }
394    }
395}
396
397/// A CpuId Entry contains supported feature information for the given processor.
398/// This can be modified by the hypervisor to pass additional information to the guest kernel
399/// about the hypervisor or vm. Information is returned in the eax, ebx, ecx and edx registers
400/// by the cpu for a given function and index/subfunction (passed into the cpu via the eax and ecx
401/// register respectively).
402#[repr(C)]
403#[derive(Clone, Copy, Debug, PartialEq, Eq)]
404pub struct CpuIdEntry {
405    pub function: u32,
406    pub index: u32,
407    // flags is needed for KVM.  We store it on CpuIdEntry to preserve the flags across
408    // get_supported_cpuids() -> kvm_cpuid2 -> CpuId -> kvm_cpuid2 -> set_cpuid().
409    pub flags: u32,
410    pub cpuid: CpuidResult,
411}
412
413/// A container for the list of cpu id entries for the hypervisor and underlying cpu.
414pub struct CpuId {
415    pub cpu_id_entries: Vec<CpuIdEntry>,
416}
417
418impl CpuId {
419    /// Constructs a new CpuId, with space allocated for `initial_capacity` CpuIdEntries.
420    pub fn new(initial_capacity: usize) -> Self {
421        CpuId {
422            cpu_id_entries: Vec::with_capacity(initial_capacity),
423        }
424    }
425}
426
427#[bitfield]
428#[derive(Clone, Copy, Debug, PartialEq, Eq)]
429pub enum DestinationMode {
430    Physical = 0,
431    Logical = 1,
432}
433
434#[bitfield]
435#[derive(Clone, Copy, Debug, PartialEq, Eq)]
436pub enum TriggerMode {
437    Edge = 0,
438    Level = 1,
439}
440
441#[bitfield]
442#[derive(Debug, Clone, Copy, PartialEq, Eq)]
443pub enum DeliveryMode {
444    Fixed = 0b000,
445    Lowest = 0b001,
446    SMI = 0b010,        // System management interrupt
447    RemoteRead = 0b011, // This is no longer supported by intel.
448    NMI = 0b100,        // Non maskable interrupt
449    Init = 0b101,
450    Startup = 0b110,
451    External = 0b111,
452}
453
454// These MSI structures are for Intel's implementation of MSI.  The PCI spec defines most of MSI,
455// but the Intel spec defines the format of messages for raising interrupts.  The PCI spec defines
456// three u32s -- the address, address_high, and data -- but Intel only makes use of the address and
457// data.  The Intel portion of the specification is in Volume 3 section 10.11.
458#[bitfield]
459#[derive(Clone, Copy, PartialEq, Eq)]
460pub struct MsiAddressMessage {
461    pub reserved: BitField2,
462    #[bits = 1]
463    pub destination_mode: DestinationMode,
464    pub redirection_hint: BitField1,
465    pub reserved_2: BitField8,
466    pub destination_id: BitField8,
467    // According to Intel's implementation of MSI, these bits must always be 0xfee.
468    pub always_0xfee: BitField12,
469}
470
471#[bitfield]
472#[derive(Clone, Copy, PartialEq, Eq)]
473pub struct MsiDataMessage {
474    pub vector: BitField8,
475    #[bits = 3]
476    pub delivery_mode: DeliveryMode,
477    pub reserved: BitField3,
478    #[bits = 1]
479    pub level: Level,
480    #[bits = 1]
481    pub trigger: TriggerMode,
482    pub reserved2: BitField16,
483}
484
485#[bitfield]
486#[derive(Debug, Clone, Copy, PartialEq, Eq)]
487pub enum DeliveryStatus {
488    Idle = 0,
489    Pending = 1,
490}
491
492/// The level of a level-triggered interrupt: asserted or deasserted.
493#[bitfield]
494#[derive(Debug, Clone, Copy, PartialEq, Eq)]
495pub enum Level {
496    Deassert = 0,
497    Assert = 1,
498}
499
500/// Represents a IOAPIC redirection table entry.
501#[bitfield]
502#[derive(Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
503pub struct IoapicRedirectionTableEntry {
504    vector: BitField8,
505    #[bits = 3]
506    delivery_mode: DeliveryMode,
507    #[bits = 1]
508    dest_mode: DestinationMode,
509    #[bits = 1]
510    delivery_status: DeliveryStatus,
511    polarity: BitField1,
512    remote_irr: bool,
513    #[bits = 1]
514    trigger_mode: TriggerMode,
515    interrupt_mask: bool, // true iff interrupts are masked.
516    reserved: BitField39,
517    dest_id: BitField8,
518}
519
520/// Number of pins on the standard KVM/IOAPIC.
521pub const NUM_IOAPIC_PINS: usize = 24;
522
523/// Represents the state of the IOAPIC.
524#[repr(C)]
525#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
526pub struct IoapicState {
527    /// base_address is the memory base address for this IOAPIC. It cannot be changed.
528    pub base_address: u64,
529    /// ioregsel register. Used for selecting which entry of the redirect table to read/write.
530    pub ioregsel: u8,
531    /// ioapicid register. Bits 24 - 27 contain the APIC ID for this device.
532    pub ioapicid: u32,
533    /// current_interrupt_level_bitmap represents a bitmap of the state of all of the irq lines
534    pub current_interrupt_level_bitmap: u32,
535    /// redirect_table contains the irq settings for each irq line
536    #[serde(
537        serialize_with = "serialize_arr",
538        deserialize_with = "deserialize_seq_to_arr"
539    )]
540    pub redirect_table: [IoapicRedirectionTableEntry; NUM_IOAPIC_PINS],
541}
542
543impl Default for IoapicState {
544    fn default() -> IoapicState {
545        // SAFETY: trivially safe
546        unsafe { std::mem::zeroed() }
547    }
548}
549
550#[repr(C)]
551#[derive(Debug, Clone, Copy, PartialEq, Eq)]
552pub enum PicSelect {
553    Primary = 0,
554    Secondary = 1,
555}
556
557#[repr(C)]
558#[derive(enumn::N, Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
559pub enum PicInitState {
560    #[default]
561    Icw1 = 0,
562    Icw2 = 1,
563    Icw3 = 2,
564    Icw4 = 3,
565}
566
567/// Convenience implementation for converting from a u8
568impl From<u8> for PicInitState {
569    fn from(item: u8) -> Self {
570        PicInitState::n(item).unwrap_or_else(|| {
571            error!("Invalid PicInitState {}, setting to 0", item);
572            PicInitState::Icw1
573        })
574    }
575}
576
577/// Represents the state of the PIC.
578#[repr(C)]
579#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
580pub struct PicState {
581    /// Edge detection.
582    pub last_irr: u8,
583    /// Interrupt Request Register.
584    pub irr: u8,
585    /// Interrupt Mask Register.
586    pub imr: u8,
587    /// Interrupt Service Register.
588    pub isr: u8,
589    /// Highest priority, for priority rotation.
590    pub priority_add: u8,
591    pub irq_base: u8,
592    pub read_reg_select: bool,
593    pub poll: bool,
594    pub special_mask: bool,
595    pub init_state: PicInitState,
596    pub auto_eoi: bool,
597    pub rotate_on_auto_eoi: bool,
598    pub special_fully_nested_mode: bool,
599    /// PIC takes either 3 or 4 bytes of initialization command word during
600    /// initialization. use_4_byte_icw is true if 4 bytes of ICW are needed.
601    pub use_4_byte_icw: bool,
602    /// "Edge/Level Control Registers", for edge trigger selection.
603    /// When a particular bit is set, the corresponding IRQ is in level-triggered mode. Otherwise
604    /// it is in edge-triggered mode.
605    pub elcr: u8,
606    pub elcr_mask: u8,
607}
608
609/// The LapicState represents the state of an x86 CPU's Local APIC.
610/// The Local APIC consists of 64 128-bit registers, but only the first 32-bits of each register
611/// can be used, so this structure only stores the first 32-bits of each register.
612#[repr(C)]
613#[derive(Clone, Copy, Serialize, Deserialize)]
614pub struct LapicState {
615    #[serde(
616        serialize_with = "serialize_arr",
617        deserialize_with = "deserialize_seq_to_arr"
618    )]
619    pub regs: [LapicRegister; 64],
620}
621
622pub type LapicRegister = u32;
623
624// rust arrays longer than 32 need custom implementations of Debug
625impl std::fmt::Debug for LapicState {
626    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
627        self.regs[..].fmt(formatter)
628    }
629}
630
631// rust arrays longer than 32 need custom implementations of PartialEq
632impl PartialEq for LapicState {
633    fn eq(&self, other: &LapicState) -> bool {
634        self.regs[..] == other.regs[..]
635    }
636}
637
638// Lapic equality is reflexive, so we impl Eq
639impl Eq for LapicState {}
640
641/// The PitState represents the state of the PIT (aka the Programmable Interval Timer).
642/// The state is simply the state of it's three channels.
643#[repr(C)]
644#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
645pub struct PitState {
646    pub channels: [PitChannelState; 3],
647    /// Hypervisor-specific flags for setting the pit state.
648    pub flags: u32,
649}
650
651/// The PitRWMode enum represents the access mode of a PIT channel.
652/// Reads and writes to the Pit happen over Port-mapped I/O, which happens one byte at a time,
653/// but the count values and latch values are two bytes. So the access mode controls which of the
654/// two bytes will be read when.
655#[repr(C)]
656#[derive(enumn::N, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
657pub enum PitRWMode {
658    /// None mode means that no access mode has been set.
659    None = 0,
660    /// Least mode means all reads/writes will read/write the least significant byte.
661    Least = 1,
662    /// Most mode means all reads/writes will read/write the most significant byte.
663    Most = 2,
664    /// Both mode means first the least significant byte will be read/written, then the
665    /// next read/write will read/write the most significant byte.
666    Both = 3,
667}
668
669/// Convenience implementation for converting from a u8
670impl From<u8> for PitRWMode {
671    fn from(item: u8) -> Self {
672        PitRWMode::n(item).unwrap_or_else(|| {
673            error!("Invalid PitRWMode value {}, setting to 0", item);
674            PitRWMode::None
675        })
676    }
677}
678
679/// The PitRWState enum represents the state of reading to or writing from a channel.
680/// This is related to the PitRWMode, it mainly gives more detail about the state of the channel
681/// with respect to PitRWMode::Both.
682#[repr(C)]
683#[derive(enumn::N, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
684pub enum PitRWState {
685    /// None mode means that no access mode has been set.
686    None = 0,
687    /// LSB means that the channel is in PitRWMode::Least access mode.
688    LSB = 1,
689    /// MSB means that the channel is in PitRWMode::Most access mode.
690    MSB = 2,
691    /// Word0 means that the channel is in PitRWMode::Both mode, and the least sginificant byte
692    /// has not been read/written yet.
693    Word0 = 3,
694    /// Word1 means that the channel is in PitRWMode::Both mode and the least significant byte
695    /// has already been read/written, and the next byte to be read/written will be the most
696    /// significant byte.
697    Word1 = 4,
698}
699
700/// Convenience implementation for converting from a u8
701impl From<u8> for PitRWState {
702    fn from(item: u8) -> Self {
703        PitRWState::n(item).unwrap_or_else(|| {
704            error!("Invalid PitRWState value {}, setting to 0", item);
705            PitRWState::None
706        })
707    }
708}
709
710/// The PitChannelState represents the state of one of the PIT's three counters.
711#[repr(C)]
712#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
713pub struct PitChannelState {
714    /// The starting value for the counter.
715    pub count: u32,
716    /// Stores the channel count from the last time the count was latched.
717    pub latched_count: u16,
718    /// Indicates the PitRWState state of reading the latch value.
719    pub count_latched: PitRWState,
720    /// Indicates whether ReadBack status has been latched.
721    pub status_latched: bool,
722    /// Stores the channel status from the last time the status was latched. The status contains
723    /// information about the access mode of this channel, but changing those bits in the status
724    /// will not change the behavior of the pit.
725    pub status: u8,
726    /// Indicates the PitRWState state of reading the counter.
727    pub read_state: PitRWState,
728    /// Indicates the PitRWState state of writing the counter.
729    pub write_state: PitRWState,
730    /// Stores the value with which the counter was initialized. Counters are 16-
731    /// bit values with an effective range of 1-65536 (65536 represented by 0).
732    pub reload_value: u16,
733    /// The command access mode of this channel.
734    pub rw_mode: PitRWMode,
735    /// The operation mode of this channel.
736    pub mode: u8,
737    /// Whether or not we are in bcd mode. Not supported by KVM or crosvm's PIT implementation.
738    pub bcd: bool,
739    /// Value of the gate input pin. This only applies to channel 2.
740    pub gate: bool,
741    /// Nanosecond timestamp of when the count value was loaded.
742    pub count_load_time: u64,
743}
744
745// Convenience constructors for IrqRoutes
746impl IrqRoute {
747    pub fn ioapic_irq_route(irq_num: u32) -> IrqRoute {
748        IrqRoute {
749            gsi: irq_num,
750            source: IrqSource::Irqchip {
751                chip: IrqSourceChip::Ioapic,
752                pin: irq_num,
753            },
754        }
755    }
756
757    pub fn pic_irq_route(id: IrqSourceChip, irq_num: u32) -> IrqRoute {
758        IrqRoute {
759            gsi: irq_num,
760            source: IrqSource::Irqchip {
761                chip: id,
762                pin: irq_num % 8,
763            },
764        }
765    }
766}
767
768/// State of a VCPU's general purpose registers.
769#[repr(C)]
770#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
771pub struct Regs {
772    pub rax: u64,
773    pub rbx: u64,
774    pub rcx: u64,
775    pub rdx: u64,
776    pub rsi: u64,
777    pub rdi: u64,
778    pub rsp: u64,
779    pub rbp: u64,
780    pub r8: u64,
781    pub r9: u64,
782    pub r10: u64,
783    pub r11: u64,
784    pub r12: u64,
785    pub r13: u64,
786    pub r14: u64,
787    pub r15: u64,
788    pub rip: u64,
789    pub rflags: u64,
790}
791
792impl Default for Regs {
793    fn default() -> Self {
794        Regs {
795            rax: 0,
796            rbx: 0,
797            rcx: 0,
798            rdx: 0,
799            rsi: 0,
800            rdi: 0,
801            rsp: 0,
802            rbp: 0,
803            r8: 0,
804            r9: 0,
805            r10: 0,
806            r11: 0,
807            r12: 0,
808            r13: 0,
809            r14: 0,
810            r15: 0,
811            rip: 0xfff0, // Reset vector.
812            rflags: 0x2, // Bit 1 (0x2) is always 1.
813        }
814    }
815}
816
817/// State of a memory segment.
818#[repr(C)]
819#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
820pub struct Segment {
821    pub base: u64,
822    /// Limit of the segment - always in bytes, regardless of granularity (`g`) field.
823    pub limit_bytes: u32,
824    pub selector: u16,
825    pub type_: u8,
826    pub present: u8,
827    pub dpl: u8,
828    pub db: u8,
829    pub s: u8,
830    pub l: u8,
831    pub g: u8,
832    pub avl: u8,
833}
834
835/// State of a global descriptor table or interrupt descriptor table.
836#[repr(C)]
837#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
838pub struct DescriptorTable {
839    pub base: u64,
840    pub limit: u16,
841}
842
843/// State of a VCPU's special registers.
844#[repr(C)]
845#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
846pub struct Sregs {
847    pub cs: Segment,
848    pub ds: Segment,
849    pub es: Segment,
850    pub fs: Segment,
851    pub gs: Segment,
852    pub ss: Segment,
853    pub tr: Segment,
854    pub ldt: Segment,
855    pub gdt: DescriptorTable,
856    pub idt: DescriptorTable,
857    pub cr0: u64,
858    pub cr2: u64,
859    pub cr3: u64,
860    pub cr4: u64,
861    pub cr8: u64,
862    pub efer: u64,
863}
864
865impl Default for Sregs {
866    fn default() -> Self {
867        // Intel SDM Vol. 3A, 3.4.5.1 ("Code- and Data-Segment Descriptor Types")
868        const SEG_TYPE_DATA: u8 = 0b0000;
869        const SEG_TYPE_DATA_WRITABLE: u8 = 0b0010;
870
871        const SEG_TYPE_CODE: u8 = 0b1000;
872        const SEG_TYPE_CODE_READABLE: u8 = 0b0010;
873
874        const SEG_TYPE_ACCESSED: u8 = 0b0001;
875
876        // Intel SDM Vol. 3A, 3.4.5 ("Segment Descriptors")
877        const SEG_S_SYSTEM: u8 = 0; // System segment.
878        const SEG_S_CODE_OR_DATA: u8 = 1; // Data/code segment.
879
880        // 16-bit real-mode code segment (reset vector).
881        let code_seg = Segment {
882            base: 0xffff0000,
883            limit_bytes: 0xffff,
884            selector: 0xf000,
885            type_: SEG_TYPE_CODE | SEG_TYPE_CODE_READABLE | SEG_TYPE_ACCESSED, // 11
886            present: 1,
887            s: SEG_S_CODE_OR_DATA,
888            ..Default::default()
889        };
890
891        // 16-bit real-mode data segment.
892        let data_seg = Segment {
893            base: 0,
894            limit_bytes: 0xffff,
895            selector: 0,
896            type_: SEG_TYPE_DATA | SEG_TYPE_DATA_WRITABLE | SEG_TYPE_ACCESSED, // 3
897            present: 1,
898            s: SEG_S_CODE_OR_DATA,
899            ..Default::default()
900        };
901
902        // 16-bit TSS segment.
903        let task_seg = Segment {
904            base: 0,
905            limit_bytes: 0xffff,
906            selector: 0,
907            type_: SEG_TYPE_CODE | SEG_TYPE_CODE_READABLE | SEG_TYPE_ACCESSED, // 11
908            present: 1,
909            s: SEG_S_SYSTEM,
910            ..Default::default()
911        };
912
913        // Local descriptor table.
914        let ldt = Segment {
915            base: 0,
916            limit_bytes: 0xffff,
917            selector: 0,
918            type_: SEG_TYPE_DATA | SEG_TYPE_DATA_WRITABLE, // 2
919            present: 1,
920            s: SEG_S_SYSTEM,
921            ..Default::default()
922        };
923
924        // Global descriptor table.
925        let gdt = DescriptorTable {
926            base: 0,
927            limit: 0xffff,
928        };
929
930        // Interrupt descriptor table.
931        let idt = DescriptorTable {
932            base: 0,
933            limit: 0xffff,
934        };
935
936        let cr0 = (1 << 4) // CR0.ET (reserved, always 1)
937                | (1 << 30); // CR0.CD (cache disable)
938
939        Sregs {
940            cs: code_seg,
941            ds: data_seg,
942            es: data_seg,
943            fs: data_seg,
944            gs: data_seg,
945            ss: data_seg,
946            tr: task_seg,
947            ldt,
948            gdt,
949            idt,
950            cr0,
951            cr2: 0,
952            cr3: 0,
953            cr4: 0,
954            cr8: 0,
955            efer: 0,
956        }
957    }
958}
959
960/// x87 80-bit floating point value.
961#[repr(C)]
962#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
963pub struct FpuReg {
964    /// 64-bit mantissa.
965    pub significand: u64,
966
967    /// 15-bit biased exponent and sign bit.
968    pub sign_exp: u16,
969}
970
971impl FpuReg {
972    /// Convert an array of 8x16-byte arrays to an array of 8 `FpuReg`.
973    ///
974    /// Ignores any data in the upper 6 bytes of each element; the values represent 80-bit FPU
975    /// registers, so the upper 48 bits are unused.
976    pub fn from_16byte_arrays(byte_arrays: &[[u8; 16]; 8]) -> [FpuReg; 8] {
977        let mut regs = [FpuReg::default(); 8];
978        for (dst, src) in regs.iter_mut().zip(byte_arrays.iter()) {
979            let tbyte: [u8; 10] = src[0..10].try_into().unwrap();
980            *dst = FpuReg::from(tbyte);
981        }
982        regs
983    }
984
985    /// Convert an array of 8 `FpuReg` into 8x16-byte arrays.
986    pub fn to_16byte_arrays(regs: &[FpuReg; 8]) -> [[u8; 16]; 8] {
987        let mut byte_arrays = [[0u8; 16]; 8];
988        for (dst, src) in byte_arrays.iter_mut().zip(regs.iter()) {
989            *dst = (*src).into();
990        }
991        byte_arrays
992    }
993}
994
995impl From<[u8; 10]> for FpuReg {
996    /// Construct a `FpuReg` from an 80-bit representation.
997    fn from(value: [u8; 10]) -> FpuReg {
998        // These array sub-slices can't fail, but there's no (safe) way to express that in Rust
999        // without an `unwrap()`.
1000        let significand_bytes = value[0..8].try_into().unwrap();
1001        let significand = u64::from_le_bytes(significand_bytes);
1002        let sign_exp_bytes = value[8..10].try_into().unwrap();
1003        let sign_exp = u16::from_le_bytes(sign_exp_bytes);
1004        FpuReg {
1005            significand,
1006            sign_exp,
1007        }
1008    }
1009}
1010
1011impl From<FpuReg> for [u8; 10] {
1012    /// Convert an `FpuReg` into its 80-bit "TBYTE" representation.
1013    fn from(value: FpuReg) -> [u8; 10] {
1014        let mut bytes = [0u8; 10];
1015        bytes[0..8].copy_from_slice(&value.significand.to_le_bytes());
1016        bytes[8..10].copy_from_slice(&value.sign_exp.to_le_bytes());
1017        bytes
1018    }
1019}
1020
1021impl From<FpuReg> for [u8; 16] {
1022    /// Convert an `FpuReg` into its 80-bit representation plus 6 unused upper bytes.
1023    /// This is a convenience function for converting to hypervisor types.
1024    fn from(value: FpuReg) -> [u8; 16] {
1025        let mut bytes = [0u8; 16];
1026        bytes[0..8].copy_from_slice(&value.significand.to_le_bytes());
1027        bytes[8..10].copy_from_slice(&value.sign_exp.to_le_bytes());
1028        bytes
1029    }
1030}
1031
1032/// State of a VCPU's floating point unit.
1033#[repr(C)]
1034#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
1035pub struct Fpu {
1036    pub fpr: [FpuReg; 8],
1037    pub fcw: u16,
1038    pub fsw: u16,
1039    pub ftwx: u8,
1040    pub last_opcode: u16,
1041    pub last_ip: u64,
1042    pub last_dp: u64,
1043    pub xmm: [[u8; 16usize]; 16usize],
1044    pub mxcsr: u32,
1045}
1046
1047impl Default for Fpu {
1048    fn default() -> Self {
1049        Fpu {
1050            fpr: Default::default(),
1051            fcw: 0x37f, // Intel SDM Vol. 1, 13.6
1052            fsw: 0,
1053            ftwx: 0,
1054            last_opcode: 0,
1055            last_ip: 0,
1056            last_dp: 0,
1057            xmm: Default::default(),
1058            mxcsr: 0x1f80, // Intel SDM Vol. 1, 11.6.4
1059        }
1060    }
1061}
1062
1063/// State of a VCPU's debug registers.
1064#[repr(C)]
1065#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
1066pub struct DebugRegs {
1067    pub db: [u64; 4usize],
1068    pub dr6: u64,
1069    pub dr7: u64,
1070}
1071
1072/// The hybrid type for intel hybrid CPU.
1073#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
1074pub enum CpuHybridType {
1075    /// Intel Atom.
1076    Atom,
1077    /// Intel Core.
1078    Core,
1079}
1080
1081/// State of the VCPU's x87 FPU, MMX, XMM, YMM registers.
1082/// May contain more state depending on enabled extensions.
1083#[derive(Clone, Debug, Serialize, Deserialize)]
1084pub struct Xsave {
1085    data: Vec<u32>,
1086
1087    // Actual length in bytes. May be smaller than data if a non-u32 multiple of bytes is
1088    // requested.
1089    len: usize,
1090}
1091
1092impl Xsave {
1093    /// Create a new buffer to store Xsave data.
1094    ///
1095    /// # Argments
1096    /// * `len` size in bytes.
1097    pub fn new(len: usize) -> Self {
1098        Xsave {
1099            data: vec![0; len.div_ceil(4)],
1100            len,
1101        }
1102    }
1103
1104    pub fn as_ptr(&self) -> *const c_void {
1105        self.data.as_ptr() as *const c_void
1106    }
1107
1108    pub fn as_mut_ptr(&mut self) -> *mut c_void {
1109        self.data.as_mut_ptr() as *mut c_void
1110    }
1111
1112    /// Length in bytes of the XSAVE data.
1113    pub fn len(&self) -> usize {
1114        self.len
1115    }
1116
1117    /// Returns true is length of XSAVE data is zero
1118    pub fn is_empty(&self) -> bool {
1119        self.len() == 0
1120    }
1121}