diff --git a/Cargo.toml b/Cargo.toml index 7b0b330..86c2cc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,19 @@ [package] -name = "axvmconfig" -version = "0.1.0" -edition = "2021" authors = [ - "Keyang Hu ", - "Mingxian Su ", -] -keywords = ["axvisor", "config", "toml"] -homepage = "https://github.com/arceos-hypervisor/axvmconfig" -description = "A simple VM configuration tool for ArceOS-Hypervisor." + "Keyang Hu ", + "Mingxian Su ", +] +description = "A simple VM configuration tool for ArceOS-Hypervisor." +edition = "2021" +homepage = "https://github.com/arceos-hypervisor/axvmconfig" +keywords = ["axvisor", "config", "toml"] license = "GPL-3.0-or-later OR Apache-2.0 OR MulanPSL-2.0" # MulanPubL2 is not included in SPDX +name = "axvmconfig" +version = "0.1.0" [features] default = ["std"] -std = ["dep:clap", "dep:env_logger"] +std = ["dep:clap", "dep:env_logger", "dep:schemars"] [lib] name = "axvmconfig" @@ -24,11 +24,12 @@ name = "axvmconfig" path = "src/main.rs" [dependencies] -env_logger = { version = "0.11.3", optional = true } +axerrno = "0.1.0" +clap = {version = "4.5.23", optional = true, features = ["derive"]} +enumerable = {version = "1.2", default-features = false} +env_logger = {version = "0.11.3", optional = true} log = "0.4.21" -serde = { version = "1.0.204", default-features = false, features = ["derive"] } +schemars = {version = "1.1.0", optional = true} +serde = {version = "1.0.204", default-features = false, features = ["derive"]} serde_repr = "0.1" -toml = { version = "0.9.5", default-features = false, features = ["serde", "parse", "display"] } -axerrno = "0.1.0" -enumerable = { version = "1.2", default-features = false } -clap = { version = "4.5.23", optional = true, features = ["derive"]} +toml = {version = "0.9.5", default-features = false, features = ["serde", "parse", "display"]} diff --git a/src/lib.rs b/src/lib.rs index 8441859..8e19403 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; use axerrno::AxResult; +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] /// A part of `AxVMConfig`, which represents guest VM type. #[derive(Default, Clone, Copy, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)] pub enum VMType { @@ -52,6 +53,7 @@ impl From for usize { /// /// Defines how virtual machine memory regions are mapped to host physical memory. /// This affects memory allocation and management strategies in the hypervisor. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Clone, PartialEq, Eq, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)] #[repr(u8)] pub enum VmMemMappingType { @@ -59,6 +61,8 @@ pub enum VmMemMappingType { MapAlloc = 0, /// The memory region is identical to the host physical memory region. MapIdentical = 1, + /// The memory region is reserved memory for the guest OS. + MapReserved = 2, } /// The default value of `VmMemMappingType` is `MapAlloc`. @@ -73,6 +77,7 @@ impl Default for VmMemMappingType { /// Represents a contiguous memory region within the guest's physical address space. /// Each region has specific properties including address, size, access permissions, /// and mapping type that determine how it's handled by the hypervisor. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct VmMemConfig { /// The start address of the memory region in GPA (Guest Physical Address). @@ -99,6 +104,7 @@ pub struct VmMemConfig { /// - 0x80 - 0xDF: Reserved for future use. /// - 0xE0 - 0xEF: Virtio devices. /// - 0xF0 - 0xFF: Reserved for future use. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize_repr, Deserialize_repr, Enumerable)] #[repr(u8)] pub enum EmulatedDeviceType { @@ -216,6 +222,7 @@ impl EmulatedDeviceType { } /// A part of `AxVMConfig`, which represents the configuration of an emulated device for a virtual machine. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct EmulatedDeviceConfig { /// The name of the device. @@ -233,21 +240,39 @@ pub struct EmulatedDeviceConfig { } /// A part of `AxVMConfig`, which represents the configuration of a pass-through device for a virtual machine. -#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] +#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)] pub struct PassThroughDeviceConfig { /// The name of the device. pub name: String, /// The base GPA (Guest Physical Address) of the device. + #[serde(default)] pub base_gpa: usize, /// The base HPA (Host Physical Address) of the device. + #[serde(default)] pub base_hpa: usize, /// The address length of the device. + #[serde(default)] pub length: usize, /// The IRQ (Interrupt Request) ID of the device. + #[serde(default)] pub irq_id: usize, } +/// A part of `AxVMConfig`, which represents the configuration of a pass-through address for a virtual machine. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] +#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct PassThroughAddressConfig { + /// The base GPA (Guest Physical Address). + #[serde(default)] + pub base_gpa: usize, + /// The address length. + #[serde(default)] + pub length: usize, +} + /// The configuration structure for the guest VM base info. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct VMBaseConfig { /// VM ID. @@ -279,6 +304,7 @@ pub struct VMBaseConfig { } /// The configuration structure for the guest VM kernel. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct VMKernelConfig { /// The entry point of the kernel image. @@ -310,6 +336,7 @@ pub struct VMKernelConfig { } /// Specifies how the VM should handle interrupts and interrupt controllers. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum VMInterruptMode { /// The VM will not handle interrupts, and the guest OS should not use interrupts. @@ -330,6 +357,7 @@ impl Default for VMInterruptMode { } /// The configuration structure for the guest VM devices. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct VMDevicesConfig { /// Emu device Information @@ -339,10 +367,17 @@ pub struct VMDevicesConfig { /// How the VM should handle interrupts and interrupt controllers. #[serde(default)] pub interrupt_mode: VMInterruptMode, + ///we would not like to pass through devices + #[serde(default)] + pub excluded_devices: Vec>, + ///we would like to pass through address + #[serde(default)] + pub passthrough_addresses: Vec, } /// The configuration structure for the guest VM serialized from a toml file provided by user, /// and then converted to `AxVMConfig` for the VM creation. +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)] pub struct AxVMCrateConfig { /// The base configuration for the VM. diff --git a/src/templates.rs b/src/templates.rs index 7262501..69df4a7 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -66,6 +66,8 @@ pub fn get_vm_config_template( emu_devices: vec![], // No emulated devices by default passthrough_devices: vec![], // No passthrough devices by default interrupt_mode: Default::default(), // Use default interrupt mode + excluded_devices: vec![], // No excluded devices by default + passthrough_addresses: vec![], // No passthrough addresses by default }, } }