|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::convert::Infallible; |
| 5 | + |
| 6 | +use kvm_ioctls::Kvm as KvmFd; |
| 7 | + |
| 8 | +use crate::cpu_config::templates::KvmCapability; |
| 9 | + |
| 10 | +/// ['Kvm'] initialization can't fail for Aarch64 |
| 11 | +pub type KvmArchError = Infallible; |
| 12 | + |
| 13 | +/// Optional capabilities. |
| 14 | +#[derive(Debug, Default)] |
| 15 | +pub struct OptionalCapabilities { |
| 16 | + /// KVM_CAP_COUNTER_OFFSET |
| 17 | + pub counter_offset: bool, |
| 18 | +} |
| 19 | + |
| 20 | +/// Struct with kvm fd and kvm associated parameters. |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct Kvm { |
| 23 | + /// KVM fd. |
| 24 | + pub fd: KvmFd, |
| 25 | + /// Maximum number of memory slots allowed by KVM. |
| 26 | + pub max_memslots: usize, |
| 27 | + /// Additional capabilities that were specified in cpu template. |
| 28 | + pub kvm_cap_modifiers: Vec<KvmCapability>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Kvm { |
| 32 | + pub(crate) const DEFAULT_CAPABILITIES: [u32; 7] = [ |
| 33 | + kvm_bindings::KVM_CAP_IOEVENTFD, |
| 34 | + kvm_bindings::KVM_CAP_IRQFD, |
| 35 | + kvm_bindings::KVM_CAP_USER_MEMORY, |
| 36 | + kvm_bindings::KVM_CAP_ARM_PSCI_0_2, |
| 37 | + kvm_bindings::KVM_CAP_DEVICE_CTRL, |
| 38 | + kvm_bindings::KVM_CAP_MP_STATE, |
| 39 | + kvm_bindings::KVM_CAP_ONE_REG, |
| 40 | + ]; |
| 41 | + |
| 42 | + /// Initialize [`Kvm`] type for Aarch64 architecture |
| 43 | + pub fn init_arch( |
| 44 | + fd: KvmFd, |
| 45 | + max_memslots: usize, |
| 46 | + kvm_cap_modifiers: Vec<KvmCapability>, |
| 47 | + ) -> Result<Self, KvmArchError> { |
| 48 | + Ok(Self { |
| 49 | + fd, |
| 50 | + max_memslots, |
| 51 | + kvm_cap_modifiers, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns struct with optional capabilities statuses. |
| 56 | + pub fn optional_capabilities(&self) -> OptionalCapabilities { |
| 57 | + OptionalCapabilities { |
| 58 | + counter_offset: self |
| 59 | + .fd |
| 60 | + .check_extension_raw(kvm_bindings::KVM_CAP_COUNTER_OFFSET.into()) |
| 61 | + != 0, |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments