2019-05-08 10:22:53 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
2019-02-28 13:16:58 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
2019-05-08 10:22:53 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
//
|
2019-02-28 13:16:58 +00:00
|
|
|
|
|
|
|
extern crate arch;
|
2019-03-07 13:56:43 +00:00
|
|
|
extern crate devices;
|
2019-03-18 20:59:50 +00:00
|
|
|
extern crate epoll;
|
2019-02-28 13:16:58 +00:00
|
|
|
extern crate kvm_ioctls;
|
2019-02-28 14:26:30 +00:00
|
|
|
extern crate libc;
|
2019-02-28 13:16:58 +00:00
|
|
|
extern crate linux_loader;
|
2019-05-09 15:01:42 +00:00
|
|
|
extern crate net_util;
|
2019-07-15 09:42:40 +00:00
|
|
|
extern crate vfio;
|
2019-05-06 17:27:40 +00:00
|
|
|
extern crate vm_allocator;
|
2019-02-28 13:16:58 +00:00
|
|
|
extern crate vm_memory;
|
2019-05-06 17:27:40 +00:00
|
|
|
extern crate vm_virtio;
|
2019-02-28 14:26:30 +00:00
|
|
|
extern crate vmm_sys_util;
|
2019-02-28 13:16:58 +00:00
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
use crate::config::{ConsoleOutputMode, VmConfig};
|
2019-07-17 16:54:11 +00:00
|
|
|
use arch::RegionType;
|
2019-06-18 17:31:50 +00:00
|
|
|
use devices::ioapic;
|
|
|
|
use kvm_bindings::{
|
|
|
|
kvm_enable_cap, kvm_msi, kvm_pit_config, kvm_userspace_memory_region, KVM_CAP_SPLIT_IRQCHIP,
|
|
|
|
KVM_PIT_SPEAKER_DUMMY,
|
|
|
|
};
|
2019-02-28 13:16:58 +00:00
|
|
|
use kvm_ioctls::*;
|
2019-05-22 20:06:49 +00:00
|
|
|
use libc::O_TMPFILE;
|
2019-03-07 13:56:43 +00:00
|
|
|
use libc::{c_void, siginfo_t, EFD_NONBLOCK};
|
2019-02-28 13:16:58 +00:00
|
|
|
use linux_loader::loader::KernelLoader;
|
2019-05-23 19:48:05 +00:00
|
|
|
use net_util::Tap;
|
2019-05-29 23:33:29 +00:00
|
|
|
use pci::{
|
2019-06-05 16:53:24 +00:00
|
|
|
InterruptDelivery, InterruptParameters, PciConfigIo, PciDevice, PciInterruptPin, PciRoot,
|
2019-05-29 23:33:29 +00:00
|
|
|
};
|
2019-05-10 07:27:56 +00:00
|
|
|
use qcow::{self, ImageType, QcowFile};
|
2019-02-28 13:16:58 +00:00
|
|
|
use std::ffi::CString;
|
2019-05-06 17:27:40 +00:00
|
|
|
use std::fs::{File, OpenOptions};
|
2019-03-07 13:56:43 +00:00
|
|
|
use std::io::{self, stdout};
|
2019-05-22 20:06:49 +00:00
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2019-03-18 20:59:50 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2019-06-19 20:48:37 +00:00
|
|
|
use std::ptr::null_mut;
|
2019-03-07 13:56:43 +00:00
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
|
|
|
use std::{result, str, thread};
|
2019-07-15 09:42:40 +00:00
|
|
|
use vfio::{VfioDevice, VfioPciDevice, VfioPciError};
|
2019-07-11 09:48:14 +00:00
|
|
|
use vm_allocator::{GsiApic, SystemAllocator};
|
2019-05-22 20:06:49 +00:00
|
|
|
use vm_memory::guest_memory::FileOffset;
|
2019-02-28 13:16:58 +00:00
|
|
|
use vm_memory::{
|
2019-06-26 09:06:49 +00:00
|
|
|
Address, Bytes, Error as MmapError, GuestAddress, GuestMemory, GuestMemoryMmap,
|
|
|
|
GuestMemoryRegion, GuestUsize,
|
2019-02-28 13:16:58 +00:00
|
|
|
};
|
2019-05-06 17:27:40 +00:00
|
|
|
use vm_virtio::transport::VirtioPciDevice;
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-02-28 14:26:30 +00:00
|
|
|
use vmm_sys_util::signal::register_signal_handler;
|
2019-03-18 20:59:50 +00:00
|
|
|
use vmm_sys_util::terminal::Terminal;
|
2019-02-28 13:16:58 +00:00
|
|
|
|
2019-02-28 14:26:30 +00:00
|
|
|
const VCPU_RTSIG_OFFSET: i32 = 0;
|
2019-05-06 17:27:40 +00:00
|
|
|
const X86_64_IRQ_BASE: u32 = 5;
|
2019-05-29 23:33:29 +00:00
|
|
|
const DEFAULT_MSIX_VEC_NUM: u16 = 2;
|
2019-02-28 13:16:58 +00:00
|
|
|
|
2019-04-25 17:10:42 +00:00
|
|
|
// CPUID feature bits
|
2019-06-07 21:30:20 +00:00
|
|
|
const TSC_DEADLINE_TIMER_ECX_BIT: u8 = 24; // tsc deadline timer ecx bit.
|
|
|
|
const HYPERVISOR_ECX_BIT: u8 = 31; // Hypervisor ecx bit.
|
2019-04-25 17:10:42 +00:00
|
|
|
|
2019-06-10 09:14:02 +00:00
|
|
|
// 64 bit direct boot entry offset for bzImage
|
|
|
|
const KERNEL_64BIT_ENTRY_OFFSET: u64 = 0x200;
|
|
|
|
|
2019-07-17 16:54:11 +00:00
|
|
|
// IOAPIC address range
|
|
|
|
const IOAPIC_RANGE_ADDR: u64 = 0xfec0_0000;
|
|
|
|
const IOAPIC_RANGE_SIZE: u64 = 0x20;
|
|
|
|
|
2019-05-10 08:21:53 +00:00
|
|
|
/// Errors associated with VM management
|
2019-02-28 13:16:58 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Cannot open the VM file descriptor.
|
|
|
|
VmFd(io::Error),
|
|
|
|
|
|
|
|
/// Cannot create the KVM instance
|
|
|
|
VmCreate(io::Error),
|
|
|
|
|
|
|
|
/// Cannot set the VM up
|
|
|
|
VmSetup(io::Error),
|
|
|
|
|
|
|
|
/// Cannot open the kernel image
|
|
|
|
KernelFile(io::Error),
|
|
|
|
|
|
|
|
/// Mmap backed guest memory error
|
|
|
|
GuestMemory(MmapError),
|
|
|
|
|
|
|
|
/// Cannot load the kernel in memory
|
|
|
|
KernelLoad(linux_loader::loader::Error),
|
|
|
|
|
|
|
|
/// Cannot load the command line in memory
|
|
|
|
CmdLine,
|
2019-02-28 14:26:30 +00:00
|
|
|
|
|
|
|
/// Cannot open the VCPU file descriptor.
|
|
|
|
VcpuFd(io::Error),
|
|
|
|
|
|
|
|
/// Cannot run the VCPUs.
|
|
|
|
VcpuRun(io::Error),
|
|
|
|
|
|
|
|
/// Cannot spawn a new vCPU thread.
|
|
|
|
VcpuSpawn(io::Error),
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Cannot set the local interruption due to bad configuration.
|
|
|
|
LocalIntConfiguration(arch::x86_64::interrupts::Error),
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Error configuring the MSR registers
|
|
|
|
MSRSConfiguration(arch::x86_64::regs::Error),
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Error configuring the general purpose registers
|
|
|
|
REGSConfiguration(arch::x86_64::regs::Error),
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Error configuring the special registers
|
|
|
|
SREGSConfiguration(arch::x86_64::regs::Error),
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// Error configuring the floating point related registers
|
|
|
|
FPUConfiguration(arch::x86_64::regs::Error),
|
2019-03-01 15:22:26 +00:00
|
|
|
|
|
|
|
/// The call to KVM_SET_CPUID2 failed.
|
|
|
|
SetSupportedCpusFailed(io::Error),
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Cannot create a device manager.
|
|
|
|
DeviceManager(DeviceManagerError),
|
|
|
|
|
2019-03-07 13:56:43 +00:00
|
|
|
/// Cannot create EventFd.
|
|
|
|
EventFd(io::Error),
|
|
|
|
|
|
|
|
/// Cannot add legacy device to Bus.
|
|
|
|
BusError(devices::BusError),
|
2019-03-18 20:59:50 +00:00
|
|
|
|
|
|
|
/// Cannot create epoll context.
|
|
|
|
EpollError(io::Error),
|
|
|
|
|
|
|
|
/// Write to the serial console failed.
|
2019-08-02 14:23:52 +00:00
|
|
|
Serial(vmm_sys_util::errno::Error),
|
2019-03-18 21:03:00 +00:00
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
/// Write to the virtio console failed.
|
2019-08-02 14:23:52 +00:00
|
|
|
Console(vmm_sys_util::errno::Error),
|
2019-07-22 19:29:02 +00:00
|
|
|
|
2019-05-07 18:34:03 +00:00
|
|
|
/// Cannot setup terminal in raw mode.
|
2019-08-02 14:23:52 +00:00
|
|
|
SetTerminalRaw(vmm_sys_util::errno::Error),
|
2019-05-07 18:34:03 +00:00
|
|
|
|
|
|
|
/// Cannot setup terminal in canonical mode.
|
2019-08-02 14:23:52 +00:00
|
|
|
SetTerminalCanon(vmm_sys_util::errno::Error),
|
2019-05-07 18:34:03 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Cannot create the system allocator
|
|
|
|
CreateSystemAllocator,
|
2019-05-06 17:27:40 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Failed parsing network parameters
|
|
|
|
ParseNetworkParameters,
|
2019-05-19 02:24:47 +00:00
|
|
|
|
|
|
|
/// Unexpected KVM_RUN exit reason
|
|
|
|
VcpuUnhandledKvmExit,
|
2019-06-10 09:14:02 +00:00
|
|
|
|
|
|
|
/// Memory is overflow
|
|
|
|
MemOverflow,
|
2019-05-22 20:06:49 +00:00
|
|
|
|
|
|
|
/// Failed to create shared file.
|
|
|
|
SharedFileCreate(io::Error),
|
|
|
|
|
|
|
|
/// Failed to set shared file length.
|
|
|
|
SharedFileSetLen(io::Error),
|
2019-07-17 16:54:11 +00:00
|
|
|
|
|
|
|
/// Failed to allocate a memory range.
|
|
|
|
MemoryRangeAllocation,
|
|
|
|
|
|
|
|
/// Failed to allocate the IOAPIC memory range.
|
|
|
|
IoapicRangeAllocation,
|
2019-05-14 01:12:40 +00:00
|
|
|
}
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
|
|
|
/// Errors associated with device manager
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DeviceManagerError {
|
|
|
|
/// Cannot create EventFd.
|
|
|
|
EventFd(io::Error),
|
2019-05-06 17:27:40 +00:00
|
|
|
|
|
|
|
/// Cannot open disk path
|
|
|
|
Disk(io::Error),
|
|
|
|
|
|
|
|
/// Cannot create virtio-blk device
|
|
|
|
CreateVirtioBlock(io::Error),
|
|
|
|
|
2019-05-09 15:01:42 +00:00
|
|
|
/// Cannot create virtio-net device
|
|
|
|
CreateVirtioNet(vm_virtio::net::Error),
|
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
/// Cannot create virtio-console device
|
|
|
|
CreateVirtioConsole(io::Error),
|
|
|
|
|
2019-05-09 05:01:48 +00:00
|
|
|
/// Cannot create virtio-rng device
|
|
|
|
CreateVirtioRng(io::Error),
|
|
|
|
|
2019-05-22 20:06:49 +00:00
|
|
|
/// Cannot create virtio-fs device
|
|
|
|
CreateVirtioFs(vm_virtio::fs::Error),
|
|
|
|
|
2019-06-19 20:48:37 +00:00
|
|
|
/// Cannot create virtio-pmem device
|
|
|
|
CreateVirtioPmem(io::Error),
|
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Failed parsing disk image format
|
|
|
|
DetectImageType(qcow::Error),
|
2019-05-09 15:01:42 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Cannot open qcow disk path
|
|
|
|
QcowDeviceCreate(qcow::Error),
|
vmm: Add support for letting the VMM create the TAP interface
Until now, the only way to get some networking with cloud-hypervisor
was to let the user create a TAP interface first, and then to provide
the name of this interface to the VMM.
This patch extend the previous behavior by adding the support for the
creation of a brand new TAP interface from the VMM itself. In case no
interface name is provided through "tap=<if_name>", we will assume
the user wants the VMM to create and set the interface on its behalf,
no matter the value of other parameters (ip, mask, and mac).
In this same scenario, because the user expects the VMM to create the
TAP interface, he can also provide the associated IP address and subnet
mask associated with it. In case those values are not provided, some
default ones will be picked.
No matter the value of "tap", the MAC address will always be set, and
if no value is provided, the VMM will come up with a default value for
it.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-05-09 18:21:15 +00:00
|
|
|
|
|
|
|
/// Cannot open tap interface
|
|
|
|
OpenTap(net_util::TapError),
|
2019-05-10 07:27:56 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Cannot allocate IRQ.
|
|
|
|
AllocateIrq,
|
2019-05-10 07:27:56 +00:00
|
|
|
|
2019-05-14 01:12:40 +00:00
|
|
|
/// Cannot configure the IRQ.
|
|
|
|
Irq(io::Error),
|
|
|
|
|
|
|
|
/// Cannot allocate PCI BARs
|
|
|
|
AllocateBars(pci::PciDeviceError),
|
|
|
|
|
|
|
|
/// Cannot register ioevent.
|
|
|
|
RegisterIoevent(io::Error),
|
|
|
|
|
|
|
|
/// Cannot create virtio device
|
2019-08-02 14:23:52 +00:00
|
|
|
VirtioDevice(vmm_sys_util::errno::Error),
|
2019-05-14 01:12:40 +00:00
|
|
|
|
|
|
|
/// Cannot add PCI device
|
|
|
|
AddPciDevice(pci::PciRootError),
|
2019-06-19 20:48:37 +00:00
|
|
|
|
|
|
|
/// Cannot open persistent memory file
|
|
|
|
PmemFileOpen(io::Error),
|
|
|
|
|
|
|
|
/// Cannot set persistent memory file size
|
|
|
|
PmemFileSetLen(io::Error),
|
|
|
|
|
|
|
|
/// Cannot find a memory range for persistent memory
|
|
|
|
PmemRangeAllocation,
|
2019-07-10 14:41:46 +00:00
|
|
|
|
|
|
|
/// Error creating serial output file
|
|
|
|
SerialOutputFileOpen(io::Error),
|
2019-07-22 19:29:02 +00:00
|
|
|
|
|
|
|
/// Error creating console output file
|
|
|
|
ConsoleOutputFileOpen(io::Error),
|
2019-07-15 09:42:40 +00:00
|
|
|
|
|
|
|
/// Cannot create a VFIO device
|
|
|
|
VfioCreate(vfio::VfioError),
|
|
|
|
|
|
|
|
/// Cannot create a VFIO PCI device
|
|
|
|
VfioPciCreate(vfio::VfioPciError),
|
|
|
|
|
|
|
|
/// Failed to map VFIO MMIO region.
|
|
|
|
VfioMapRegion(VfioPciError),
|
2019-07-25 04:15:46 +00:00
|
|
|
|
|
|
|
/// Failed to create the KVM device.
|
|
|
|
CreateKvmDevice(io::Error),
|
2019-02-28 13:16:58 +00:00
|
|
|
}
|
2019-05-14 01:12:40 +00:00
|
|
|
pub type DeviceManagerResult<T> = result::Result<T, DeviceManagerError>;
|
2019-02-28 13:16:58 +00:00
|
|
|
|
2019-06-11 13:53:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum CpuidReg {
|
|
|
|
EAX,
|
|
|
|
EBX,
|
|
|
|
ECX,
|
|
|
|
EDX,
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:42:31 +00:00
|
|
|
struct CpuidPatch {
|
|
|
|
function: u32,
|
|
|
|
index: u32,
|
|
|
|
flags_bit: Option<u8>,
|
|
|
|
eax_bit: Option<u8>,
|
|
|
|
ebx_bit: Option<u8>,
|
|
|
|
ecx_bit: Option<u8>,
|
|
|
|
edx_bit: Option<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CpuidPatch {
|
|
|
|
fn set_cpuid_reg(
|
|
|
|
cpuid: &mut CpuId,
|
|
|
|
function: u32,
|
|
|
|
index: Option<u32>,
|
|
|
|
reg: CpuidReg,
|
|
|
|
value: u32,
|
|
|
|
) {
|
|
|
|
let entries = cpuid.mut_entries_slice();
|
|
|
|
|
|
|
|
for entry in entries.iter_mut() {
|
|
|
|
if entry.function == function && (index == None || index.unwrap() == entry.index) {
|
|
|
|
match reg {
|
|
|
|
CpuidReg::EAX => {
|
|
|
|
entry.eax = value;
|
|
|
|
}
|
|
|
|
CpuidReg::EBX => {
|
|
|
|
entry.ebx = value;
|
|
|
|
}
|
|
|
|
CpuidReg::ECX => {
|
|
|
|
entry.ecx = value;
|
|
|
|
}
|
|
|
|
CpuidReg::EDX => {
|
|
|
|
entry.edx = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn patch_cpuid(cpuid: &mut CpuId, patches: Vec<CpuidPatch>) {
|
|
|
|
let entries = cpuid.mut_entries_slice();
|
|
|
|
|
|
|
|
for entry in entries.iter_mut() {
|
|
|
|
for patch in patches.iter() {
|
|
|
|
if entry.function == patch.function && entry.index == patch.index {
|
|
|
|
if let Some(flags_bit) = patch.flags_bit {
|
|
|
|
entry.flags |= 1 << flags_bit;
|
|
|
|
}
|
|
|
|
if let Some(eax_bit) = patch.eax_bit {
|
|
|
|
entry.eax |= 1 << eax_bit;
|
|
|
|
}
|
|
|
|
if let Some(ebx_bit) = patch.ebx_bit {
|
|
|
|
entry.ebx |= 1 << ebx_bit;
|
|
|
|
}
|
|
|
|
if let Some(ecx_bit) = patch.ecx_bit {
|
|
|
|
entry.ecx |= 1 << ecx_bit;
|
|
|
|
}
|
|
|
|
if let Some(edx_bit) = patch.edx_bit {
|
|
|
|
entry.edx |= 1 << edx_bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:26:30 +00:00
|
|
|
/// A wrapper around creating and using a kvm-based VCPU.
|
|
|
|
pub struct Vcpu {
|
|
|
|
fd: VcpuFd,
|
|
|
|
id: u8,
|
2019-05-19 02:24:47 +00:00
|
|
|
io_bus: devices::Bus,
|
|
|
|
mmio_bus: devices::Bus,
|
2019-06-18 17:31:50 +00:00
|
|
|
ioapic: Option<Arc<Mutex<ioapic::Ioapic>>>,
|
2019-02-28 14:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vcpu {
|
|
|
|
/// Constructs a new VCPU for `vm`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `id` - Represents the CPU number between [0, max vcpus).
|
|
|
|
/// * `vm` - The virtual machine this vcpu will get attached to.
|
2019-06-18 17:31:50 +00:00
|
|
|
pub fn new(
|
|
|
|
id: u8,
|
|
|
|
vm: &Vm,
|
|
|
|
io_bus: devices::Bus,
|
|
|
|
mmio_bus: devices::Bus,
|
|
|
|
ioapic: Option<Arc<Mutex<ioapic::Ioapic>>>,
|
|
|
|
) -> Result<Self> {
|
2019-02-28 14:26:30 +00:00
|
|
|
let kvm_vcpu = vm.fd.create_vcpu(id).map_err(Error::VcpuFd)?;
|
|
|
|
// Initially the cpuid per vCPU is the one supported by this VM.
|
2019-05-19 02:24:47 +00:00
|
|
|
Ok(Vcpu {
|
|
|
|
fd: kvm_vcpu,
|
|
|
|
id,
|
|
|
|
io_bus,
|
|
|
|
mmio_bus,
|
2019-06-18 17:31:50 +00:00
|
|
|
ioapic,
|
2019-05-19 02:24:47 +00:00
|
|
|
})
|
2019-02-28 14:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures a x86_64 specific vcpu and should be called once per vcpu from the vcpu's thread.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `machine_config` - Specifies necessary info used for the CPUID configuration.
|
|
|
|
/// * `kernel_start_addr` - Offset from `guest_mem` at which the kernel starts.
|
|
|
|
/// * `vm` - The virtual machine this vcpu will get attached to.
|
|
|
|
pub fn configure(&mut self, kernel_start_addr: GuestAddress, vm: &Vm) -> Result<()> {
|
2019-06-11 13:53:38 +00:00
|
|
|
let mut cpuid = vm.cpuid.clone();
|
2019-06-12 13:42:31 +00:00
|
|
|
CpuidPatch::set_cpuid_reg(&mut cpuid, 0xb, None, CpuidReg::EDX, u32::from(self.id));
|
2019-03-01 15:22:26 +00:00
|
|
|
self.fd
|
2019-06-11 13:53:38 +00:00
|
|
|
.set_cpuid2(&cpuid)
|
2019-03-01 15:22:26 +00:00
|
|
|
.map_err(Error::SetSupportedCpusFailed)?;
|
|
|
|
|
2019-02-28 14:26:30 +00:00
|
|
|
arch::x86_64::regs::setup_msrs(&self.fd).map_err(Error::MSRSConfiguration)?;
|
|
|
|
// Safe to unwrap because this method is called after the VM is configured
|
|
|
|
let vm_memory = vm.get_memory();
|
|
|
|
arch::x86_64::regs::setup_regs(
|
|
|
|
&self.fd,
|
|
|
|
kernel_start_addr.raw_value(),
|
|
|
|
arch::x86_64::layout::BOOT_STACK_POINTER.raw_value(),
|
|
|
|
arch::x86_64::layout::ZERO_PAGE_START.raw_value(),
|
|
|
|
)
|
|
|
|
.map_err(Error::REGSConfiguration)?;
|
|
|
|
arch::x86_64::regs::setup_fpu(&self.fd).map_err(Error::FPUConfiguration)?;
|
|
|
|
arch::x86_64::regs::setup_sregs(vm_memory, &self.fd).map_err(Error::SREGSConfiguration)?;
|
|
|
|
arch::x86_64::interrupts::set_lint(&self.fd).map_err(Error::LocalIntConfiguration)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the VCPU until it exits, returning the reason.
|
|
|
|
///
|
|
|
|
/// Note that the state of the VCPU and associated VM must be setup first for this to do
|
|
|
|
/// anything useful.
|
2019-05-19 02:24:47 +00:00
|
|
|
pub fn run(&self) -> Result<()> {
|
|
|
|
match self.fd.run() {
|
|
|
|
Ok(run) => match run {
|
|
|
|
VcpuExit::IoIn(addr, data) => {
|
|
|
|
self.io_bus.read(u64::from(addr), data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
VcpuExit::IoOut(addr, data) => {
|
|
|
|
self.io_bus.write(u64::from(addr), data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
VcpuExit::MmioRead(addr, data) => {
|
|
|
|
self.mmio_bus.read(addr as u64, data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
VcpuExit::MmioWrite(addr, data) => {
|
|
|
|
self.mmio_bus.write(addr as u64, data);
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-06-18 17:31:50 +00:00
|
|
|
VcpuExit::IoapicEoi(vector) => {
|
|
|
|
if let Some(ioapic) = &self.ioapic {
|
|
|
|
ioapic.lock().unwrap().end_of_interrupt(vector);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-05-19 02:24:47 +00:00
|
|
|
r => {
|
|
|
|
error!("Unexpected exit reason on vcpu run: {:?}", r);
|
|
|
|
Err(Error::VcpuUnhandledKvmExit)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(ref e) => match e.raw_os_error().unwrap() {
|
|
|
|
libc::EAGAIN | libc::EINTR => Ok(()),
|
|
|
|
_ => {
|
|
|
|
error!("VCPU {:?} error {:?}", self.id, e);
|
|
|
|
Err(Error::VcpuUnhandledKvmExit)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2019-02-28 14:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
struct VmInfo<'a> {
|
|
|
|
memory: GuestMemoryMmap,
|
|
|
|
vm_fd: &'a Arc<VmFd>,
|
|
|
|
vm_cfg: &'a VmConfig<'a>,
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:50:30 +00:00
|
|
|
struct BusInfo<'a> {
|
|
|
|
io: &'a mut devices::Bus,
|
|
|
|
mmio: &'a mut devices::Bus,
|
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
struct InterruptInfo<'a> {
|
|
|
|
msi_capable: bool,
|
|
|
|
ioapic: &'a Option<Arc<Mutex<ioapic::Ioapic>>>,
|
|
|
|
}
|
|
|
|
|
2019-06-11 17:49:19 +00:00
|
|
|
struct KernelIoapicIrq {
|
|
|
|
evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KernelIoapicIrq {
|
|
|
|
fn new(evt: EventFd) -> Self {
|
|
|
|
KernelIoapicIrq { evt }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl devices::Interrupt for KernelIoapicIrq {
|
|
|
|
fn deliver(&self) -> result::Result<(), io::Error> {
|
|
|
|
self.evt.write(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
struct UserIoapicIrq {
|
|
|
|
ioapic: Arc<Mutex<ioapic::Ioapic>>,
|
|
|
|
irq: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserIoapicIrq {
|
|
|
|
fn new(ioapic: Arc<Mutex<ioapic::Ioapic>>, irq: usize) -> Self {
|
|
|
|
UserIoapicIrq { ioapic, irq }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl devices::Interrupt for UserIoapicIrq {
|
|
|
|
fn deliver(&self) -> result::Result<(), io::Error> {
|
|
|
|
self.ioapic
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.service_irq(self.irq)
|
|
|
|
.map_err(|e| {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
format!("failed to inject IRQ #{}: {:?}", self.irq, e),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:56:43 +00:00
|
|
|
struct DeviceManager {
|
|
|
|
io_bus: devices::Bus,
|
2019-05-06 17:27:40 +00:00
|
|
|
mmio_bus: devices::Bus,
|
2019-03-07 13:56:43 +00:00
|
|
|
|
|
|
|
// Serial port on 0x3f8
|
2019-07-10 14:41:46 +00:00
|
|
|
serial: Option<Arc<Mutex<devices::legacy::Serial>>>,
|
2019-07-22 19:29:02 +00:00
|
|
|
console: Option<Arc<vm_virtio::ConsoleInput>>,
|
2019-04-15 18:57:30 +00:00
|
|
|
|
|
|
|
// i8042 device for exit
|
|
|
|
i8042: Arc<Mutex<devices::legacy::I8042Device>>,
|
2019-04-15 17:59:38 +00:00
|
|
|
exit_evt: EventFd,
|
2019-04-18 10:26:09 +00:00
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
// IOAPIC
|
|
|
|
ioapic: Option<Arc<Mutex<ioapic::Ioapic>>>,
|
|
|
|
|
2019-04-18 10:26:09 +00:00
|
|
|
// PCI root
|
|
|
|
pci: Arc<Mutex<PciConfigIo>>,
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceManager {
|
2019-05-06 19:15:44 +00:00
|
|
|
fn new(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-05-06 19:15:44 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
2019-05-29 23:33:29 +00:00
|
|
|
msi_capable: bool,
|
2019-06-18 17:31:50 +00:00
|
|
|
userspace_ioapic: bool,
|
2019-08-01 16:00:08 +00:00
|
|
|
mut mem_slots: u32,
|
2019-05-14 01:12:40 +00:00
|
|
|
) -> DeviceManagerResult<Self> {
|
2019-07-19 17:50:30 +00:00
|
|
|
let mut io_bus = devices::Bus::new();
|
2019-05-06 17:27:40 +00:00
|
|
|
let mut mmio_bus = devices::Bus::new();
|
2019-06-11 17:49:19 +00:00
|
|
|
|
2019-07-19 17:50:30 +00:00
|
|
|
let mut buses = BusInfo {
|
|
|
|
io: &mut io_bus,
|
|
|
|
mmio: &mut mmio_bus,
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
let ioapic = if userspace_ioapic {
|
|
|
|
// Create IOAPIC
|
2019-08-01 16:27:23 +00:00
|
|
|
Some(Arc::new(Mutex::new(ioapic::Ioapic::new(
|
|
|
|
vm_info.vm_fd.clone(),
|
|
|
|
))))
|
2019-06-18 17:31:50 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let interrupt_info = InterruptInfo {
|
|
|
|
msi_capable,
|
|
|
|
ioapic: &ioapic,
|
|
|
|
};
|
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
let serial_writer: Option<Box<io::Write + Send>> = match vm_info.vm_cfg.serial.mode {
|
2019-07-22 19:29:02 +00:00
|
|
|
ConsoleOutputMode::File => Some(Box::new(
|
2019-08-01 16:27:23 +00:00
|
|
|
File::create(vm_info.vm_cfg.serial.file.unwrap())
|
2019-07-10 14:41:46 +00:00
|
|
|
.map_err(DeviceManagerError::SerialOutputFileOpen)?,
|
|
|
|
)),
|
2019-07-22 19:29:02 +00:00
|
|
|
ConsoleOutputMode::Tty => Some(Box::new(stdout())),
|
|
|
|
ConsoleOutputMode::Off => None,
|
2019-06-18 17:31:50 +00:00
|
|
|
};
|
2019-07-10 14:41:46 +00:00
|
|
|
let serial = if serial_writer.is_some() {
|
|
|
|
// Serial is tied to IRQ #4
|
|
|
|
let serial_irq = 4;
|
|
|
|
let interrupt: Box<devices::Interrupt> = if let Some(ioapic) = &ioapic {
|
|
|
|
Box::new(UserIoapicIrq::new(ioapic.clone(), serial_irq))
|
|
|
|
} else {
|
|
|
|
let serial_evt = EventFd::new(EFD_NONBLOCK).map_err(DeviceManagerError::EventFd)?;
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info
|
|
|
|
.vm_fd
|
2019-07-10 14:41:46 +00:00
|
|
|
.register_irqfd(serial_evt.as_raw_fd(), serial_irq as u32)
|
|
|
|
.map_err(DeviceManagerError::Irq)?;
|
2019-06-11 17:49:19 +00:00
|
|
|
|
2019-07-10 14:41:46 +00:00
|
|
|
Box::new(KernelIoapicIrq::new(serial_evt))
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Arc::new(Mutex::new(devices::legacy::Serial::new_out(
|
|
|
|
interrupt,
|
|
|
|
serial_writer.unwrap(),
|
|
|
|
))))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-06-11 17:49:19 +00:00
|
|
|
// Add a shutdown device (i8042)
|
2019-05-14 01:12:40 +00:00
|
|
|
let exit_evt = EventFd::new(EFD_NONBLOCK).map_err(DeviceManagerError::EventFd)?;
|
2019-04-15 18:57:30 +00:00
|
|
|
let i8042 = Arc::new(Mutex::new(devices::legacy::I8042Device::new(
|
2019-05-14 01:12:40 +00:00
|
|
|
exit_evt.try_clone().map_err(DeviceManagerError::EventFd)?,
|
2019-04-15 18:57:30 +00:00
|
|
|
)));
|
2019-04-15 17:59:38 +00:00
|
|
|
|
2019-06-04 06:51:00 +00:00
|
|
|
let pci_root = PciRoot::new(None);
|
|
|
|
let mut pci = PciConfigIo::new(pci_root);
|
2019-05-06 17:27:40 +00:00
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
let console_writer: Option<Box<io::Write + Send>> = match vm_info.vm_cfg.console.mode {
|
2019-07-22 20:12:58 +00:00
|
|
|
ConsoleOutputMode::File => Some(Box::new(
|
2019-08-01 16:27:23 +00:00
|
|
|
File::create(vm_info.vm_cfg.console.file.unwrap())
|
2019-07-22 20:12:58 +00:00
|
|
|
.map_err(DeviceManagerError::ConsoleOutputFileOpen)?,
|
|
|
|
)),
|
|
|
|
ConsoleOutputMode::Tty => Some(Box::new(stdout())),
|
|
|
|
ConsoleOutputMode::Off => None,
|
|
|
|
};
|
|
|
|
let console = if console_writer.is_some() {
|
|
|
|
let (virtio_console_device, console) = vm_virtio::Console::new(console_writer)
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioConsole)?;
|
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
Box::new(virtio_console_device),
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
&mut pci,
|
|
|
|
&mut buses,
|
|
|
|
&interrupt_info,
|
|
|
|
)?;
|
|
|
|
Some(console)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
DeviceManager::add_virtio_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator,
|
|
|
|
&mut pci,
|
|
|
|
&mut buses,
|
|
|
|
&interrupt_info,
|
2019-08-01 16:00:08 +00:00
|
|
|
&mut mem_slots,
|
2019-07-22 20:12:58 +00:00
|
|
|
)?;
|
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
DeviceManager::add_vfio_devices(vm_info, allocator, &mut pci, &mut buses, mem_slots)?;
|
2019-07-15 09:42:40 +00:00
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
let pci = Arc::new(Mutex::new(pci));
|
|
|
|
|
|
|
|
Ok(DeviceManager {
|
|
|
|
io_bus,
|
|
|
|
mmio_bus,
|
|
|
|
serial,
|
|
|
|
console,
|
|
|
|
i8042,
|
|
|
|
exit_evt,
|
|
|
|
ioapic,
|
|
|
|
pci,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_virtio_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
2019-08-01 16:00:08 +00:00
|
|
|
mut mem_slots: &mut u32,
|
2019-07-22 20:12:58 +00:00
|
|
|
) -> DeviceManagerResult<()> {
|
2019-07-08 22:48:39 +00:00
|
|
|
// Add virtio-blk if required
|
2019-08-01 16:27:23 +00:00
|
|
|
DeviceManager::add_virtio_block_devices(vm_info, allocator, pci, buses, &interrupt_info)?;
|
2019-07-22 20:12:58 +00:00
|
|
|
|
|
|
|
// Add virtio-net if required
|
2019-08-01 16:27:23 +00:00
|
|
|
DeviceManager::add_virtio_net_devices(vm_info, allocator, pci, buses, &interrupt_info)?;
|
2019-07-22 20:12:58 +00:00
|
|
|
|
|
|
|
// Add virtio-rng if required
|
2019-08-01 16:27:23 +00:00
|
|
|
DeviceManager::add_virtio_rng_devices(vm_info, allocator, pci, buses, &interrupt_info)?;
|
2019-07-22 20:12:58 +00:00
|
|
|
|
|
|
|
// Add virtio-fs if required
|
2019-08-01 16:27:23 +00:00
|
|
|
DeviceManager::add_virtio_fs_devices(vm_info, allocator, pci, buses, &interrupt_info)?;
|
2019-07-22 20:12:58 +00:00
|
|
|
|
|
|
|
// Add virtio-pmem if required
|
|
|
|
DeviceManager::add_virtio_pmem_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator,
|
|
|
|
pci,
|
|
|
|
buses,
|
|
|
|
&interrupt_info,
|
2019-08-01 16:00:08 +00:00
|
|
|
&mut mem_slots,
|
2019-07-22 20:12:58 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_virtio_block_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
|
|
|
) -> DeviceManagerResult<()> {
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(disk_list_cfg) = &vm_info.vm_cfg.disks {
|
2019-07-08 22:48:39 +00:00
|
|
|
for disk_cfg in disk_list_cfg.iter() {
|
|
|
|
// Open block device path
|
|
|
|
let raw_img: File = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.open(disk_cfg.path)
|
|
|
|
.map_err(DeviceManagerError::Disk)?;
|
|
|
|
|
|
|
|
let image_type = qcow::detect_image_type(&raw_img)
|
|
|
|
.map_err(DeviceManagerError::DetectImageType)?;
|
|
|
|
let block = match image_type {
|
|
|
|
ImageType::Raw => {
|
|
|
|
let raw_img = vm_virtio::RawFile::new(raw_img);
|
|
|
|
let dev =
|
|
|
|
vm_virtio::Block::new(raw_img, disk_cfg.path.to_path_buf(), false)
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioBlock)?;
|
|
|
|
Box::new(dev) as Box<vm_virtio::VirtioDevice>
|
|
|
|
}
|
|
|
|
ImageType::Qcow2 => {
|
|
|
|
let qcow_img = QcowFile::from(raw_img)
|
|
|
|
.map_err(DeviceManagerError::QcowDeviceCreate)?;
|
|
|
|
let dev =
|
|
|
|
vm_virtio::Block::new(qcow_img, disk_cfg.path.to_path_buf(), false)
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioBlock)?;
|
|
|
|
Box::new(dev) as Box<vm_virtio::VirtioDevice>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
block,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-07-08 22:48:39 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
pci,
|
|
|
|
buses,
|
2019-07-08 22:48:39 +00:00
|
|
|
&interrupt_info,
|
|
|
|
)?;
|
|
|
|
}
|
2019-05-22 13:56:22 +00:00
|
|
|
}
|
2019-05-08 10:41:24 +00:00
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_virtio_net_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
|
|
|
) -> DeviceManagerResult<()> {
|
2019-05-08 10:41:24 +00:00
|
|
|
// Add virtio-net if required
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(net_list_cfg) = &vm_info.vm_cfg.net {
|
2019-07-08 22:31:13 +00:00
|
|
|
for net_cfg in net_list_cfg.iter() {
|
|
|
|
let mut virtio_net_device: vm_virtio::Net;
|
|
|
|
|
|
|
|
if let Some(tap_if_name) = net_cfg.tap {
|
|
|
|
let tap = Tap::open_named(tap_if_name).map_err(DeviceManagerError::OpenTap)?;
|
|
|
|
virtio_net_device = vm_virtio::Net::new_with_tap(tap, Some(&net_cfg.mac))
|
2019-05-23 19:48:05 +00:00
|
|
|
.map_err(DeviceManagerError::CreateVirtioNet)?;
|
2019-07-08 22:31:13 +00:00
|
|
|
} else {
|
|
|
|
virtio_net_device =
|
|
|
|
vm_virtio::Net::new(net_cfg.ip, net_cfg.mask, Some(&net_cfg.mac))
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioNet)?;
|
|
|
|
}
|
2019-05-23 19:48:05 +00:00
|
|
|
|
2019-07-08 22:31:13 +00:00
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
Box::new(virtio_net_device),
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-07-08 22:31:13 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
pci,
|
|
|
|
buses,
|
2019-07-08 22:31:13 +00:00
|
|
|
&interrupt_info,
|
|
|
|
)?;
|
|
|
|
}
|
2019-05-08 10:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-07-22 19:29:02 +00:00
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
fn add_virtio_rng_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
|
|
|
) -> DeviceManagerResult<()> {
|
2019-05-09 05:01:48 +00:00
|
|
|
// Add virtio-rng if required
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(rng_path) = vm_info.vm_cfg.rng.src.to_str() {
|
2019-05-09 05:01:48 +00:00
|
|
|
let virtio_rng_device =
|
2019-05-14 01:12:40 +00:00
|
|
|
vm_virtio::Rng::new(rng_path).map_err(DeviceManagerError::CreateVirtioRng)?;
|
2019-05-09 05:01:48 +00:00
|
|
|
|
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
Box::new(virtio_rng_device),
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-05-09 05:01:48 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
pci,
|
|
|
|
buses,
|
2019-06-18 17:31:50 +00:00
|
|
|
&interrupt_info,
|
2019-05-09 05:01:48 +00:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_virtio_fs_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
|
|
|
) -> DeviceManagerResult<()> {
|
2019-05-22 20:06:49 +00:00
|
|
|
// Add virtio-fs if required
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(fs_list_cfg) = &vm_info.vm_cfg.fs {
|
2019-06-27 16:14:11 +00:00
|
|
|
for fs_cfg in fs_list_cfg.iter() {
|
|
|
|
if let Some(fs_sock) = fs_cfg.sock.to_str() {
|
|
|
|
let virtio_fs_device = vm_virtio::Fs::new(
|
|
|
|
fs_sock,
|
|
|
|
fs_cfg.tag,
|
|
|
|
fs_cfg.num_queues,
|
|
|
|
fs_cfg.queue_size,
|
|
|
|
)
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioFs)?;
|
|
|
|
|
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
Box::new(virtio_fs_device),
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-06-27 16:14:11 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
pci,
|
|
|
|
buses,
|
2019-06-27 16:14:11 +00:00
|
|
|
&interrupt_info,
|
|
|
|
)?;
|
|
|
|
}
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_virtio_pmem_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-22 20:12:58 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
interrupt_info: &InterruptInfo,
|
2019-08-01 16:00:08 +00:00
|
|
|
mem_slots: &mut u32,
|
2019-07-22 20:12:58 +00:00
|
|
|
) -> DeviceManagerResult<()> {
|
2019-06-19 20:48:37 +00:00
|
|
|
// Add virtio-pmem if required
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(pmem_list_cfg) = &vm_info.vm_cfg.pmem {
|
2019-06-28 08:45:58 +00:00
|
|
|
for pmem_cfg in pmem_list_cfg.iter() {
|
|
|
|
let size = pmem_cfg.size;
|
|
|
|
|
2019-07-17 16:54:11 +00:00
|
|
|
// The memory needs to be 2MiB aligned in order to support
|
|
|
|
// hugepages.
|
2019-06-28 08:45:58 +00:00
|
|
|
let pmem_guest_addr = allocator
|
2019-07-17 16:54:11 +00:00
|
|
|
.allocate_mmio_addresses(None, size as GuestUsize, Some(0x0020_0000))
|
2019-06-28 08:45:58 +00:00
|
|
|
.ok_or(DeviceManagerError::PmemRangeAllocation)?;
|
|
|
|
|
|
|
|
let (custom_flags, set_len) = if pmem_cfg.file.is_dir() {
|
|
|
|
(O_TMPFILE, true)
|
|
|
|
} else {
|
|
|
|
(0, false)
|
|
|
|
};
|
|
|
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.custom_flags(custom_flags)
|
|
|
|
.open(pmem_cfg.file)
|
|
|
|
.map_err(DeviceManagerError::PmemFileOpen)?;
|
|
|
|
|
|
|
|
if set_len {
|
|
|
|
file.set_len(size)
|
|
|
|
.map_err(DeviceManagerError::PmemFileSetLen)?;
|
|
|
|
}
|
2019-06-19 20:48:37 +00:00
|
|
|
|
2019-06-28 08:45:58 +00:00
|
|
|
let addr = unsafe {
|
|
|
|
libc::mmap(
|
|
|
|
null_mut(),
|
|
|
|
size as usize,
|
|
|
|
libc::PROT_READ | libc::PROT_WRITE,
|
|
|
|
libc::MAP_NORESERVE | libc::MAP_SHARED,
|
|
|
|
file.as_raw_fd(),
|
|
|
|
0 as libc::off_t,
|
|
|
|
) as *mut u8
|
|
|
|
};
|
|
|
|
|
|
|
|
let mem_region = kvm_userspace_memory_region {
|
2019-08-01 16:00:08 +00:00
|
|
|
slot: *mem_slots as u32,
|
2019-06-28 08:45:58 +00:00
|
|
|
guest_phys_addr: pmem_guest_addr.raw_value(),
|
|
|
|
memory_size: size,
|
|
|
|
userspace_addr: addr as u64,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
// Safe because the guest regions are guaranteed not to overlap.
|
2019-08-01 16:27:23 +00:00
|
|
|
let _ = unsafe { vm_info.vm_fd.set_user_memory_region(mem_region) };
|
2019-06-28 08:45:58 +00:00
|
|
|
|
2019-08-01 16:00:08 +00:00
|
|
|
// Increment the KVM slot number
|
|
|
|
*mem_slots += 1;
|
|
|
|
|
2019-06-28 08:45:58 +00:00
|
|
|
let virtio_pmem_device =
|
|
|
|
vm_virtio::Pmem::new(file, pmem_guest_addr, size as GuestUsize)
|
|
|
|
.map_err(DeviceManagerError::CreateVirtioPmem)?;
|
|
|
|
|
|
|
|
DeviceManager::add_virtio_pci_device(
|
|
|
|
Box::new(virtio_pmem_device),
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.memory.clone(),
|
2019-06-28 08:45:58 +00:00
|
|
|
allocator,
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info.vm_fd,
|
2019-07-22 20:12:58 +00:00
|
|
|
pci,
|
|
|
|
buses,
|
2019-06-28 08:45:58 +00:00
|
|
|
&interrupt_info,
|
|
|
|
)?;
|
2019-06-19 20:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:12:58 +00:00
|
|
|
Ok(())
|
2019-05-08 10:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 04:15:46 +00:00
|
|
|
fn create_kvm_device(vm: &Arc<VmFd>) -> DeviceManagerResult<DeviceFd> {
|
|
|
|
let mut vfio_dev = kvm_bindings::kvm_create_device {
|
|
|
|
type_: kvm_bindings::kvm_device_type_KVM_DEV_TYPE_VFIO,
|
|
|
|
fd: 0,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
vm.create_device(&mut vfio_dev)
|
|
|
|
.map_err(DeviceManagerError::CreateKvmDevice)
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:42:40 +00:00
|
|
|
fn add_vfio_devices(
|
2019-08-01 16:27:23 +00:00
|
|
|
vm_info: &VmInfo,
|
2019-07-15 09:42:40 +00:00
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
pci: &mut PciConfigIo,
|
|
|
|
buses: &mut BusInfo,
|
|
|
|
mem_slots: u32,
|
|
|
|
) -> DeviceManagerResult<()> {
|
2019-07-25 04:36:53 +00:00
|
|
|
let mut mem_slot = mem_slots;
|
2019-08-01 16:27:23 +00:00
|
|
|
if let Some(device_list_cfg) = &vm_info.vm_cfg.devices {
|
2019-07-25 04:15:46 +00:00
|
|
|
// Create the KVM VFIO device
|
2019-08-01 16:27:23 +00:00
|
|
|
let device_fd = DeviceManager::create_kvm_device(vm_info.vm_fd)?;
|
2019-07-25 04:15:46 +00:00
|
|
|
let device_fd = Arc::new(device_fd);
|
|
|
|
|
2019-07-15 09:42:40 +00:00
|
|
|
for device_cfg in device_list_cfg.iter() {
|
2019-07-25 04:15:46 +00:00
|
|
|
let vfio_device =
|
2019-08-01 16:27:23 +00:00
|
|
|
VfioDevice::new(device_cfg.path, device_fd.clone(), vm_info.memory.clone())
|
2019-07-25 04:15:46 +00:00
|
|
|
.map_err(DeviceManagerError::VfioCreate)?;
|
2019-07-15 09:42:40 +00:00
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
let mut vfio_pci_device = VfioPciDevice::new(vm_info.vm_fd, allocator, vfio_device)
|
2019-07-15 09:42:40 +00:00
|
|
|
.map_err(DeviceManagerError::VfioPciCreate)?;
|
|
|
|
|
|
|
|
let bars = vfio_pci_device
|
|
|
|
.allocate_bars(allocator)
|
|
|
|
.map_err(DeviceManagerError::AllocateBars)?;
|
|
|
|
|
2019-07-25 04:36:53 +00:00
|
|
|
mem_slot = vfio_pci_device
|
2019-08-01 16:27:23 +00:00
|
|
|
.map_mmio_regions(vm_info.vm_fd, mem_slot)
|
2019-07-15 09:42:40 +00:00
|
|
|
.map_err(DeviceManagerError::VfioMapRegion)?;
|
|
|
|
|
|
|
|
let vfio_pci_device = Arc::new(Mutex::new(vfio_pci_device));
|
|
|
|
|
|
|
|
pci.add_device(vfio_pci_device.clone())
|
|
|
|
.map_err(DeviceManagerError::AddPciDevice)?;
|
|
|
|
|
|
|
|
pci.register_mapping(vfio_pci_device.clone(), buses.io, buses.mmio, bars)
|
|
|
|
.map_err(DeviceManagerError::AddPciDevice)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:41:24 +00:00
|
|
|
fn add_virtio_pci_device(
|
|
|
|
virtio_device: Box<vm_virtio::VirtioDevice>,
|
|
|
|
memory: GuestMemoryMmap,
|
|
|
|
allocator: &mut SystemAllocator,
|
2019-05-29 23:33:29 +00:00
|
|
|
vm_fd: &Arc<VmFd>,
|
2019-06-04 06:51:00 +00:00
|
|
|
pci: &mut PciConfigIo,
|
2019-07-19 17:50:30 +00:00
|
|
|
buses: &mut BusInfo,
|
2019-06-18 17:31:50 +00:00
|
|
|
interrupt_info: &InterruptInfo,
|
2019-05-14 01:12:40 +00:00
|
|
|
) -> DeviceManagerResult<()> {
|
2019-06-18 17:31:50 +00:00
|
|
|
let msix_num = if interrupt_info.msi_capable {
|
|
|
|
DEFAULT_MSIX_VEC_NUM
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2019-06-07 16:19:46 +00:00
|
|
|
|
|
|
|
let mut virtio_pci_device = VirtioPciDevice::new(memory, virtio_device, msix_num)
|
|
|
|
.map_err(DeviceManagerError::VirtioDevice)?;
|
2019-06-03 20:57:26 +00:00
|
|
|
|
2019-05-06 17:27:40 +00:00
|
|
|
let bars = virtio_pci_device
|
|
|
|
.allocate_bars(allocator)
|
2019-05-14 01:12:40 +00:00
|
|
|
.map_err(DeviceManagerError::AllocateBars)?;
|
2019-05-06 17:27:40 +00:00
|
|
|
|
|
|
|
for (event, addr, _) in virtio_pci_device.ioeventfds() {
|
|
|
|
let io_addr = IoEventAddress::Mmio(addr);
|
|
|
|
vm_fd
|
|
|
|
.register_ioevent(event.as_raw_fd(), &io_addr, NoDatamatch)
|
2019-05-14 01:12:40 +00:00
|
|
|
.map_err(DeviceManagerError::RegisterIoevent)?;
|
2019-05-06 17:27:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
if interrupt_info.msi_capable {
|
2019-05-29 23:33:29 +00:00
|
|
|
let vm_fd_clone = vm_fd.clone();
|
|
|
|
|
2019-06-05 16:53:24 +00:00
|
|
|
let msi_cb = Arc::new(Box::new(move |p: InterruptParameters| {
|
|
|
|
if let Some(entry) = p.msix {
|
|
|
|
let msi_queue = kvm_msi {
|
|
|
|
address_lo: entry.msg_addr_lo,
|
|
|
|
address_hi: entry.msg_addr_hi,
|
|
|
|
data: entry.msg_data,
|
|
|
|
flags: 0u32,
|
|
|
|
devid: 0u32,
|
|
|
|
pad: [0u8; 12],
|
|
|
|
};
|
|
|
|
|
|
|
|
return vm_fd_clone.signal_msi(msi_queue).map(|ret| {
|
|
|
|
if ret > 0 {
|
|
|
|
debug!("MSI message successfully delivered");
|
|
|
|
} else if ret == 0 {
|
|
|
|
warn!("failed to deliver MSI message, blocked by guest");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"missing MSI-X entry",
|
|
|
|
))
|
|
|
|
}) as InterruptDelivery);
|
2019-06-03 20:57:26 +00:00
|
|
|
|
2019-05-29 23:33:29 +00:00
|
|
|
virtio_pci_device.assign_msix(msi_cb);
|
|
|
|
} else {
|
|
|
|
let irq_num = allocator
|
|
|
|
.allocate_irq()
|
|
|
|
.ok_or(DeviceManagerError::AllocateIrq)?;
|
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
let irq_cb = if let Some(ioapic) = interrupt_info.ioapic {
|
|
|
|
let ioapic_clone = ioapic.clone();
|
|
|
|
Box::new(move |_p: InterruptParameters| {
|
|
|
|
ioapic_clone
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.service_irq(irq_num as usize)
|
|
|
|
.map_err(|e| {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
format!("failed to inject IRQ #{}: {:?}", irq_num, e),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}) as InterruptDelivery
|
|
|
|
} else {
|
|
|
|
let irqfd = EventFd::new(EFD_NONBLOCK).map_err(DeviceManagerError::EventFd)?;
|
|
|
|
vm_fd
|
|
|
|
.register_irqfd(irqfd.as_raw_fd(), irq_num)
|
|
|
|
.map_err(DeviceManagerError::Irq)?;
|
|
|
|
|
2019-06-05 16:53:24 +00:00
|
|
|
Box::new(move |_p: InterruptParameters| irqfd.write(1)) as InterruptDelivery
|
2019-06-18 17:31:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtio_pci_device.assign_pin_irq(
|
|
|
|
Arc::new(irq_cb),
|
|
|
|
irq_num as u32,
|
|
|
|
PciInterruptPin::IntA,
|
2019-06-05 16:53:24 +00:00
|
|
|
);
|
2019-05-29 23:33:29 +00:00
|
|
|
}
|
2019-05-06 17:27:40 +00:00
|
|
|
|
|
|
|
let virtio_pci_device = Arc::new(Mutex::new(virtio_pci_device));
|
|
|
|
|
2019-06-04 06:51:00 +00:00
|
|
|
pci.add_device(virtio_pci_device.clone())
|
2019-06-05 09:24:18 +00:00
|
|
|
.map_err(DeviceManagerError::AddPciDevice)?;
|
|
|
|
|
2019-07-19 17:50:30 +00:00
|
|
|
pci.register_mapping(
|
|
|
|
virtio_pci_device.clone(),
|
|
|
|
&mut buses.io,
|
|
|
|
&mut buses.mmio,
|
|
|
|
bars,
|
|
|
|
)
|
|
|
|
.map_err(DeviceManagerError::AddPciDevice)?;
|
2019-05-06 17:27:40 +00:00
|
|
|
|
2019-05-08 10:41:24 +00:00
|
|
|
Ok(())
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_devices(&mut self) -> Result<()> {
|
2019-07-10 14:41:46 +00:00
|
|
|
if self.serial.is_some() {
|
|
|
|
// Insert serial device
|
|
|
|
self.io_bus
|
|
|
|
.insert(self.serial.as_ref().unwrap().clone(), 0x3f8, 0x8)
|
|
|
|
.map_err(Error::BusError)?;
|
|
|
|
}
|
2019-04-15 18:57:30 +00:00
|
|
|
|
|
|
|
// Insert i8042 device
|
|
|
|
self.io_bus
|
|
|
|
.insert(self.i8042.clone(), 0x61, 0x4)
|
|
|
|
.map_err(Error::BusError)?;
|
|
|
|
|
2019-04-18 10:26:09 +00:00
|
|
|
// Insert the PCI root configuration space.
|
|
|
|
self.io_bus
|
|
|
|
.insert(self.pci.clone(), 0xcf8, 0x8)
|
|
|
|
.map_err(Error::BusError)?;
|
2019-06-18 17:31:50 +00:00
|
|
|
|
|
|
|
if let Some(ioapic) = &self.ioapic {
|
|
|
|
// Insert IOAPIC
|
|
|
|
self.mmio_bus
|
2019-07-17 16:54:11 +00:00
|
|
|
.insert(ioapic.clone(), IOAPIC_RANGE_ADDR, IOAPIC_RANGE_SIZE)
|
2019-06-18 17:31:50 +00:00
|
|
|
.map_err(Error::BusError)?;
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:56:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:14:31 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
enum EpollDispatch {
|
2019-04-15 17:59:38 +00:00
|
|
|
Exit,
|
2019-04-15 17:14:31 +00:00
|
|
|
Stdin,
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:59:50 +00:00
|
|
|
pub struct EpollContext {
|
|
|
|
raw_fd: RawFd,
|
2019-04-15 17:14:31 +00:00
|
|
|
dispatch_table: Vec<Option<EpollDispatch>>,
|
2019-03-18 20:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EpollContext {
|
|
|
|
pub fn new() -> result::Result<EpollContext, io::Error> {
|
|
|
|
let raw_fd = epoll::create(true)?;
|
2019-04-15 17:14:31 +00:00
|
|
|
|
|
|
|
// Initial capacity needs to be large enough to hold:
|
2019-04-15 17:59:38 +00:00
|
|
|
// * 1 exit event
|
2019-04-15 17:14:31 +00:00
|
|
|
// * 1 stdin event
|
2019-04-15 17:59:38 +00:00
|
|
|
let mut dispatch_table = Vec::with_capacity(3);
|
2019-04-15 17:14:31 +00:00
|
|
|
dispatch_table.push(None);
|
|
|
|
|
|
|
|
Ok(EpollContext {
|
|
|
|
raw_fd,
|
|
|
|
dispatch_table,
|
|
|
|
})
|
2019-03-18 20:59:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:14:31 +00:00
|
|
|
pub fn add_stdin(&mut self) -> result::Result<(), io::Error> {
|
|
|
|
let dispatch_index = self.dispatch_table.len() as u64;
|
2019-03-18 20:59:50 +00:00
|
|
|
epoll::ctl(
|
|
|
|
self.raw_fd,
|
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
libc::STDIN_FILENO,
|
2019-04-15 17:14:31 +00:00
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
|
2019-03-18 20:59:50 +00:00
|
|
|
)?;
|
|
|
|
|
2019-04-15 17:14:31 +00:00
|
|
|
self.dispatch_table.push(Some(EpollDispatch::Stdin));
|
|
|
|
|
2019-03-18 20:59:50 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-15 17:59:38 +00:00
|
|
|
|
|
|
|
fn add_event<T>(&mut self, fd: &T, token: EpollDispatch) -> result::Result<(), io::Error>
|
|
|
|
where
|
|
|
|
T: AsRawFd,
|
|
|
|
{
|
|
|
|
let dispatch_index = self.dispatch_table.len() as u64;
|
|
|
|
epoll::ctl(
|
|
|
|
self.raw_fd,
|
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
fd.as_raw_fd(),
|
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
|
|
|
|
)?;
|
|
|
|
self.dispatch_table.push(Some(token));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-18 20:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRawFd for EpollContext {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.raw_fd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
pub struct Vm<'a> {
|
2019-05-29 23:33:29 +00:00
|
|
|
fd: Arc<VmFd>,
|
2019-02-28 13:16:58 +00:00
|
|
|
kernel: File,
|
|
|
|
memory: GuestMemoryMmap,
|
2019-05-08 06:47:59 +00:00
|
|
|
vcpus: Vec<thread::JoinHandle<()>>,
|
2019-03-07 13:56:43 +00:00
|
|
|
devices: DeviceManager,
|
2019-03-01 15:22:26 +00:00
|
|
|
cpuid: CpuId,
|
2019-02-28 13:16:58 +00:00
|
|
|
config: VmConfig<'a>,
|
2019-03-18 20:59:50 +00:00
|
|
|
epoll: EpollContext,
|
2019-05-30 15:17:57 +00:00
|
|
|
on_tty: bool,
|
2019-02-28 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Vm<'a> {
|
2019-03-07 13:56:43 +00:00
|
|
|
pub fn new(kvm: &Kvm, config: VmConfig<'a>) -> Result<Self> {
|
2019-05-23 19:48:05 +00:00
|
|
|
let kernel = File::open(&config.kernel.path).map_err(Error::KernelFile)?;
|
2019-02-28 13:16:58 +00:00
|
|
|
let fd = kvm.create_vm().map_err(Error::VmCreate)?;
|
2019-05-29 23:33:29 +00:00
|
|
|
let fd = Arc::new(fd);
|
2019-02-28 13:16:58 +00:00
|
|
|
|
|
|
|
// Init guest memory
|
2019-07-09 03:13:02 +00:00
|
|
|
let arch_mem_regions = arch::arch_memory_regions(config.memory.size);
|
2019-05-24 19:21:23 +00:00
|
|
|
|
2019-07-17 16:54:11 +00:00
|
|
|
let ram_regions: Vec<(GuestAddress, usize)> = arch_mem_regions
|
|
|
|
.iter()
|
|
|
|
.filter(|r| r.2 == RegionType::Ram)
|
|
|
|
.map(|r| (r.0, r.1))
|
|
|
|
.collect();
|
2019-07-25 08:20:59 +00:00
|
|
|
let sub_regions: Vec<(GuestAddress, usize)> = arch_mem_regions
|
|
|
|
.iter()
|
|
|
|
.filter(|r| r.2 == RegionType::SubRegion)
|
|
|
|
.map(|r| (r.0, r.1))
|
|
|
|
.collect();
|
2019-07-17 16:54:11 +00:00
|
|
|
|
|
|
|
// Check the number of reserved regions, and only take the first one
|
|
|
|
// that's acrtually a 32-bit hole.
|
|
|
|
let mut mem_hole = (GuestAddress(0), 0);
|
2019-07-25 08:20:59 +00:00
|
|
|
for region in sub_regions.iter() {
|
2019-07-17 16:54:11 +00:00
|
|
|
if region.0.unchecked_add(region.1 as u64).raw_value() <= 0x1_0000_0000 {
|
|
|
|
mem_hole = (region.0, region.1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:21:23 +00:00
|
|
|
let guest_memory = match config.memory.file {
|
|
|
|
Some(file) => {
|
|
|
|
let mut mem_regions = Vec::<(GuestAddress, usize, Option<FileOffset>)>::new();
|
2019-07-17 16:54:11 +00:00
|
|
|
for region in ram_regions.iter() {
|
2019-05-24 19:21:23 +00:00
|
|
|
let file = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.custom_flags(O_TMPFILE)
|
|
|
|
.open(file)
|
|
|
|
.map_err(Error::SharedFileCreate)?;
|
|
|
|
|
|
|
|
file.set_len(region.1 as u64)
|
|
|
|
.map_err(Error::SharedFileSetLen)?;
|
|
|
|
|
|
|
|
mem_regions.push((region.0, region.1, Some(FileOffset::new(file, 0))));
|
|
|
|
}
|
2019-05-22 20:06:49 +00:00
|
|
|
|
2019-05-24 19:21:23 +00:00
|
|
|
GuestMemoryMmap::with_files(&mem_regions).map_err(Error::GuestMemory)?
|
|
|
|
}
|
2019-07-17 16:54:11 +00:00
|
|
|
None => GuestMemoryMmap::new(&ram_regions).map_err(Error::GuestMemory)?,
|
2019-05-24 19:21:23 +00:00
|
|
|
};
|
2019-02-28 13:16:58 +00:00
|
|
|
|
|
|
|
guest_memory
|
|
|
|
.with_regions(|index, region| {
|
|
|
|
let mem_region = kvm_userspace_memory_region {
|
|
|
|
slot: index as u32,
|
|
|
|
guest_phys_addr: region.start_addr().raw_value(),
|
|
|
|
memory_size: region.len() as u64,
|
|
|
|
userspace_addr: region.as_ptr() as u64,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Safe because the guest regions are guaranteed not to overlap.
|
2019-06-03 21:09:01 +00:00
|
|
|
unsafe { fd.set_user_memory_region(mem_region) }
|
2019-02-28 13:16:58 +00:00
|
|
|
})
|
|
|
|
.map_err(|_| Error::GuestMemory(MmapError::NoMemoryRegion))?;
|
|
|
|
|
|
|
|
// Set TSS
|
|
|
|
fd.set_tss_address(arch::x86_64::layout::KVM_TSS_ADDRESS.raw_value() as usize)
|
|
|
|
.map_err(Error::VmSetup)?;
|
|
|
|
|
2019-03-01 15:22:26 +00:00
|
|
|
// Supported CPUID
|
2019-04-25 17:10:42 +00:00
|
|
|
let mut cpuid = kvm
|
2019-03-01 15:22:26 +00:00
|
|
|
.get_supported_cpuid(MAX_KVM_CPUID_ENTRIES)
|
|
|
|
.map_err(Error::VmSetup)?;
|
2019-06-07 21:30:20 +00:00
|
|
|
|
2019-06-18 17:31:50 +00:00
|
|
|
let msi_capable = kvm.check_extension(Cap::SignalMsi);
|
|
|
|
|
2019-06-07 21:30:20 +00:00
|
|
|
let mut cpuid_patches = Vec::new();
|
2019-06-18 17:31:50 +00:00
|
|
|
let mut userspace_ioapic = false;
|
2019-06-07 21:30:20 +00:00
|
|
|
if kvm.check_extension(Cap::TscDeadlineTimer) {
|
2019-06-18 17:31:50 +00:00
|
|
|
if kvm.check_extension(Cap::SplitIrqchip) && msi_capable {
|
|
|
|
// Create split irqchip
|
|
|
|
// Only the local APIC is emulated in kernel, both PICs and IOAPIC
|
|
|
|
// are not.
|
|
|
|
let mut cap: kvm_enable_cap = Default::default();
|
|
|
|
cap.cap = KVM_CAP_SPLIT_IRQCHIP;
|
|
|
|
cap.args[0] = ioapic::NUM_IOAPIC_PINS as u64;
|
|
|
|
fd.enable_cap(&cap).map_err(Error::VmSetup)?;
|
|
|
|
|
|
|
|
// Because of the split irqchip, we need a userspace IOAPIC.
|
|
|
|
userspace_ioapic = true;
|
|
|
|
} else {
|
|
|
|
// Create irqchip
|
|
|
|
// A local APIC, 2 PICs and an IOAPIC are emulated in kernel.
|
|
|
|
fd.create_irq_chip().map_err(Error::VmSetup)?;
|
|
|
|
}
|
|
|
|
|
2019-06-07 21:30:20 +00:00
|
|
|
// Patch tsc deadline timer bit
|
|
|
|
cpuid_patches.push(CpuidPatch {
|
|
|
|
function: 1,
|
|
|
|
index: 0,
|
|
|
|
flags_bit: None,
|
|
|
|
eax_bit: None,
|
|
|
|
ebx_bit: None,
|
|
|
|
ecx_bit: Some(TSC_DEADLINE_TIMER_ECX_BIT),
|
|
|
|
edx_bit: None,
|
|
|
|
});
|
|
|
|
} else {
|
2019-06-18 17:31:50 +00:00
|
|
|
// Create irqchip
|
|
|
|
// A local APIC, 2 PICs and an IOAPIC are emulated in kernel.
|
|
|
|
fd.create_irq_chip().map_err(Error::VmSetup)?;
|
2019-06-07 21:30:20 +00:00
|
|
|
// Creates an in-kernel device model for the PIT.
|
|
|
|
let mut pit_config = kvm_pit_config::default();
|
|
|
|
// We need to enable the emulation of a dummy speaker port stub so that writing to port 0x61
|
|
|
|
// (i.e. KVM_SPEAKER_BASE_ADDRESS) does not trigger an exit to user space.
|
|
|
|
pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
|
|
|
|
fd.create_pit2(pit_config).map_err(Error::VmSetup)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch hypervisor bit
|
|
|
|
cpuid_patches.push(CpuidPatch {
|
|
|
|
function: 1,
|
|
|
|
index: 0,
|
|
|
|
flags_bit: None,
|
|
|
|
eax_bit: None,
|
|
|
|
ebx_bit: None,
|
|
|
|
ecx_bit: Some(HYPERVISOR_ECX_BIT),
|
|
|
|
edx_bit: None,
|
|
|
|
});
|
|
|
|
|
2019-06-12 13:42:31 +00:00
|
|
|
CpuidPatch::patch_cpuid(&mut cpuid, cpuid_patches);
|
2019-03-01 15:22:26 +00:00
|
|
|
|
2019-07-11 09:48:14 +00:00
|
|
|
let ioapic = GsiApic::new(
|
|
|
|
X86_64_IRQ_BASE,
|
|
|
|
ioapic::NUM_IOAPIC_PINS as u32 - X86_64_IRQ_BASE,
|
|
|
|
);
|
|
|
|
|
2019-05-06 17:27:40 +00:00
|
|
|
// Let's allocate 64 GiB of addressable MMIO space, starting at 0.
|
|
|
|
let mut allocator = SystemAllocator::new(
|
2019-06-26 16:02:00 +00:00
|
|
|
GuestAddress(0),
|
|
|
|
1 << 16 as GuestUsize,
|
2019-05-06 17:27:40 +00:00
|
|
|
GuestAddress(0),
|
|
|
|
1 << 36 as GuestUsize,
|
2019-07-17 16:54:11 +00:00
|
|
|
mem_hole.0,
|
|
|
|
mem_hole.1 as GuestUsize,
|
2019-07-11 09:48:14 +00:00
|
|
|
vec![ioapic],
|
2019-05-06 17:27:40 +00:00
|
|
|
)
|
|
|
|
.ok_or(Error::CreateSystemAllocator)?;
|
|
|
|
|
2019-07-17 16:54:11 +00:00
|
|
|
// Allocate RAM and Reserved address ranges.
|
|
|
|
for region in arch_mem_regions.iter() {
|
|
|
|
allocator
|
|
|
|
.allocate_mmio_addresses(Some(region.0), region.1 as GuestUsize, None)
|
|
|
|
.ok_or(Error::MemoryRangeAllocation)?;
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:27:23 +00:00
|
|
|
let vm_info = VmInfo {
|
|
|
|
memory: guest_memory.clone(),
|
|
|
|
vm_fd: &fd,
|
|
|
|
vm_cfg: &config,
|
|
|
|
};
|
|
|
|
|
2019-05-29 23:33:29 +00:00
|
|
|
let device_manager = DeviceManager::new(
|
2019-08-01 16:27:23 +00:00
|
|
|
&vm_info,
|
2019-05-29 23:33:29 +00:00
|
|
|
&mut allocator,
|
2019-06-18 17:31:50 +00:00
|
|
|
msi_capable,
|
|
|
|
userspace_ioapic,
|
2019-07-25 04:31:44 +00:00
|
|
|
ram_regions.len() as u32,
|
2019-05-29 23:33:29 +00:00
|
|
|
)
|
|
|
|
.map_err(Error::DeviceManager)?;
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-03-18 20:59:50 +00:00
|
|
|
// Let's add our STDIN fd.
|
2019-04-15 17:14:31 +00:00
|
|
|
let mut epoll = EpollContext::new().map_err(Error::EpollError)?;
|
2019-05-30 15:17:57 +00:00
|
|
|
|
|
|
|
let on_tty = unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0;
|
|
|
|
if on_tty {
|
|
|
|
epoll.add_stdin().map_err(Error::EpollError)?;
|
|
|
|
}
|
2019-03-18 20:59:50 +00:00
|
|
|
|
2019-04-15 17:59:38 +00:00
|
|
|
// Let's add an exit event.
|
|
|
|
epoll
|
|
|
|
.add_event(&device_manager.exit_evt, EpollDispatch::Exit)
|
|
|
|
.map_err(Error::EpollError)?;
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
let vcpus = Vec::with_capacity(u8::from(&config.cpus) as usize);
|
2019-05-08 06:47:59 +00:00
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
Ok(Vm {
|
|
|
|
fd,
|
|
|
|
kernel,
|
|
|
|
memory: guest_memory,
|
2019-05-08 06:47:59 +00:00
|
|
|
vcpus,
|
2019-03-07 13:56:43 +00:00
|
|
|
devices: device_manager,
|
2019-03-01 15:22:26 +00:00
|
|
|
cpuid,
|
2019-03-07 13:56:43 +00:00
|
|
|
config,
|
2019-03-18 20:59:50 +00:00
|
|
|
epoll,
|
2019-05-30 15:17:57 +00:00
|
|
|
on_tty,
|
2019-02-28 13:16:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_kernel(&mut self) -> Result<GuestAddress> {
|
2019-05-06 20:24:57 +00:00
|
|
|
let cmdline_cstring =
|
2019-05-23 19:48:05 +00:00
|
|
|
CString::new(self.config.cmdline.args.clone()).map_err(|_| Error::CmdLine)?;
|
2019-06-10 09:14:02 +00:00
|
|
|
let entry_addr = match linux_loader::loader::Elf::load(
|
2019-02-28 13:16:58 +00:00
|
|
|
&self.memory,
|
|
|
|
None,
|
|
|
|
&mut self.kernel,
|
|
|
|
Some(arch::HIMEM_START),
|
2019-06-10 09:14:02 +00:00
|
|
|
) {
|
|
|
|
Ok(entry_addr) => entry_addr,
|
|
|
|
Err(linux_loader::loader::Error::InvalidElfMagicNumber) => {
|
|
|
|
linux_loader::loader::BzImage::load(
|
|
|
|
&self.memory,
|
|
|
|
None,
|
|
|
|
&mut self.kernel,
|
|
|
|
Some(arch::HIMEM_START),
|
|
|
|
)
|
|
|
|
.map_err(Error::KernelLoad)?
|
|
|
|
}
|
|
|
|
_ => panic!("Invalid elf file"),
|
|
|
|
};
|
2019-02-28 13:16:58 +00:00
|
|
|
|
|
|
|
linux_loader::loader::load_cmdline(
|
|
|
|
&self.memory,
|
2019-05-23 19:48:05 +00:00
|
|
|
self.config.cmdline.offset,
|
2019-02-28 13:16:58 +00:00
|
|
|
&cmdline_cstring,
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::CmdLine)?;
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
let vcpu_count = u8::from(&self.config.cpus);
|
2019-02-28 13:16:58 +00:00
|
|
|
|
2019-06-10 09:14:02 +00:00
|
|
|
match entry_addr.setup_header {
|
|
|
|
Some(hdr) => {
|
|
|
|
arch::configure_system(
|
|
|
|
&self.memory,
|
|
|
|
self.config.cmdline.offset,
|
|
|
|
cmdline_cstring.to_bytes().len() + 1,
|
|
|
|
vcpu_count,
|
|
|
|
Some(hdr),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::CmdLine)?;
|
|
|
|
|
|
|
|
let load_addr = entry_addr
|
|
|
|
.kernel_load
|
|
|
|
.raw_value()
|
|
|
|
.checked_add(KERNEL_64BIT_ENTRY_OFFSET)
|
|
|
|
.ok_or(Error::MemOverflow)?;
|
|
|
|
|
|
|
|
Ok(GuestAddress(load_addr))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
arch::configure_system(
|
|
|
|
&self.memory,
|
|
|
|
self.config.cmdline.offset,
|
|
|
|
cmdline_cstring.to_bytes().len() + 1,
|
|
|
|
vcpu_count,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::CmdLine)?;
|
|
|
|
|
|
|
|
Ok(entry_addr.kernel_load)
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:14:31 +00:00
|
|
|
pub fn control_loop(&mut self) -> Result<()> {
|
2019-03-18 20:59:50 +00:00
|
|
|
// Let's start the STDIN polling thread.
|
2019-04-15 17:14:31 +00:00
|
|
|
const EPOLL_EVENTS_LEN: usize = 100;
|
|
|
|
|
2019-03-18 20:59:50 +00:00
|
|
|
let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN];
|
|
|
|
let epoll_fd = self.epoll.as_raw_fd();
|
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
if (self.devices.serial.is_some() || self.devices.console.is_some()) && self.on_tty {
|
2019-05-30 15:17:57 +00:00
|
|
|
io::stdin()
|
|
|
|
.lock()
|
|
|
|
.set_raw_mode()
|
|
|
|
.map_err(Error::SetTerminalRaw)?;
|
|
|
|
}
|
2019-05-07 18:34:03 +00:00
|
|
|
|
2019-05-30 15:17:57 +00:00
|
|
|
'outer: loop {
|
2019-08-01 20:00:14 +00:00
|
|
|
let num_events = match epoll::wait(epoll_fd, -1, &mut events[..]) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::Interrupted {
|
|
|
|
// It's well defined from the epoll_wait() syscall
|
|
|
|
// documentation that the epoll loop can be interrupted
|
|
|
|
// before any of the requested events occurred or the
|
|
|
|
// timeout expired. In both those cases, epoll_wait()
|
|
|
|
// returns an error of type EINTR, but this should not
|
|
|
|
// be considered as a regular error. Instead it is more
|
|
|
|
// appropriate to retry, by calling into epoll_wait().
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Err(Error::EpollError(e));
|
|
|
|
}
|
|
|
|
};
|
2019-03-18 20:59:50 +00:00
|
|
|
|
|
|
|
for event in events.iter().take(num_events) {
|
2019-04-15 17:14:31 +00:00
|
|
|
let dispatch_idx = event.data as usize;
|
|
|
|
|
|
|
|
if let Some(dispatch_type) = self.epoll.dispatch_table[dispatch_idx] {
|
|
|
|
match dispatch_type {
|
2019-04-15 17:59:38 +00:00
|
|
|
EpollDispatch::Exit => {
|
|
|
|
// Consume the event.
|
|
|
|
self.devices.exit_evt.read().map_err(Error::EventFd)?;
|
|
|
|
|
2019-05-30 15:17:57 +00:00
|
|
|
break 'outer;
|
2019-04-15 17:59:38 +00:00
|
|
|
}
|
2019-04-15 17:14:31 +00:00
|
|
|
EpollDispatch::Stdin => {
|
2019-07-22 19:29:02 +00:00
|
|
|
let mut out = [0u8; 64];
|
|
|
|
let count = io::stdin()
|
|
|
|
.lock()
|
|
|
|
.read_raw(&mut out)
|
|
|
|
.map_err(Error::Serial)?;
|
2019-07-10 14:41:46 +00:00
|
|
|
|
2019-08-09 10:48:03 +00:00
|
|
|
if self.devices.serial.is_some()
|
|
|
|
&& self.config.serial.mode.input_enabled()
|
|
|
|
{
|
2019-07-10 14:41:46 +00:00
|
|
|
self.devices
|
|
|
|
.serial
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.expect("Failed to process stdin event due to poisoned lock")
|
|
|
|
.queue_input_bytes(&out[..count])
|
|
|
|
.map_err(Error::Serial)?;
|
|
|
|
}
|
2019-07-22 19:29:02 +00:00
|
|
|
|
2019-08-09 10:48:03 +00:00
|
|
|
if self.devices.console.is_some()
|
|
|
|
&& self.config.console.mode.input_enabled()
|
|
|
|
{
|
2019-07-22 19:29:02 +00:00
|
|
|
self.devices
|
|
|
|
.console
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.queue_input_bytes(&out[..count]);
|
|
|
|
}
|
2019-04-15 17:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 20:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 15:17:57 +00:00
|
|
|
|
|
|
|
if self.on_tty {
|
|
|
|
// Don't forget to set the terminal in canonical mode
|
|
|
|
// before to exit.
|
|
|
|
io::stdin()
|
|
|
|
.lock()
|
|
|
|
.set_canon_mode()
|
|
|
|
.map_err(Error::SetTerminalCanon)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-03-18 20:59:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 14:26:30 +00:00
|
|
|
pub fn start(&mut self, entry_addr: GuestAddress) -> Result<()> {
|
2019-03-07 13:56:43 +00:00
|
|
|
self.devices.register_devices()?;
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
let vcpu_count = u8::from(&self.config.cpus);
|
2019-02-28 14:26:30 +00:00
|
|
|
|
2019-05-08 06:47:59 +00:00
|
|
|
// let vcpus: Vec<thread::JoinHandle<()>> = Vec::with_capacity(vcpu_count as usize);
|
2019-02-28 14:26:30 +00:00
|
|
|
let vcpu_thread_barrier = Arc::new(Barrier::new((vcpu_count + 1) as usize));
|
|
|
|
|
|
|
|
for cpu_id in 0..vcpu_count {
|
2019-03-07 13:56:43 +00:00
|
|
|
let io_bus = self.devices.io_bus.clone();
|
2019-05-06 17:27:40 +00:00
|
|
|
let mmio_bus = self.devices.mmio_bus.clone();
|
2019-06-18 17:31:50 +00:00
|
|
|
let ioapic = if let Some(ioapic) = &self.devices.ioapic {
|
|
|
|
Some(ioapic.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut vcpu = Vcpu::new(cpu_id, &self, io_bus, mmio_bus, ioapic)?;
|
2019-02-28 14:26:30 +00:00
|
|
|
vcpu.configure(entry_addr, &self)?;
|
|
|
|
|
2019-03-06 11:04:14 +00:00
|
|
|
let vcpu_thread_barrier = vcpu_thread_barrier.clone();
|
|
|
|
|
2019-05-08 06:47:59 +00:00
|
|
|
self.vcpus.push(
|
2019-02-28 14:26:30 +00:00
|
|
|
thread::Builder::new()
|
|
|
|
.name(format!("cloud-hypervisor_vcpu{}", vcpu.id))
|
|
|
|
.spawn(move || {
|
|
|
|
unsafe {
|
|
|
|
extern "C" fn handle_signal(_: i32, _: *mut siginfo_t, _: *mut c_void) {
|
|
|
|
}
|
|
|
|
// This uses an async signal safe handler to kill the vcpu handles.
|
|
|
|
register_signal_handler(
|
|
|
|
VCPU_RTSIG_OFFSET,
|
|
|
|
vmm_sys_util::signal::SignalHandler::Siginfo(handle_signal),
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
.expect("Failed to register vcpu signal handler");
|
|
|
|
}
|
|
|
|
|
2019-03-06 11:04:14 +00:00
|
|
|
// Block until all CPUs are ready.
|
2019-02-28 14:26:30 +00:00
|
|
|
vcpu_thread_barrier.wait();
|
|
|
|
|
2019-05-19 02:24:47 +00:00
|
|
|
while vcpu.run().is_ok() {}
|
2019-02-28 14:26:30 +00:00
|
|
|
})
|
|
|
|
.map_err(Error::VcpuSpawn)?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-06 11:04:14 +00:00
|
|
|
// Unblock all CPU threads.
|
2019-02-28 14:26:30 +00:00
|
|
|
vcpu_thread_barrier.wait();
|
2019-03-06 11:04:14 +00:00
|
|
|
|
2019-04-15 17:14:31 +00:00
|
|
|
self.control_loop()?;
|
2019-03-18 20:59:50 +00:00
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-02-28 14:26:30 +00:00
|
|
|
|
|
|
|
/// Gets a reference to the guest memory owned by this VM.
|
|
|
|
///
|
|
|
|
/// Note that `GuestMemory` does not include any device memory that may have been added after
|
|
|
|
/// this VM was constructed.
|
|
|
|
pub fn get_memory(&self) -> &GuestMemoryMmap {
|
|
|
|
&self.memory
|
|
|
|
}
|
2019-02-28 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn test_vm() {
|
|
|
|
// This example based on https://lwn.net/Articles/658511/
|
|
|
|
let code = [
|
|
|
|
0xba, 0xf8, 0x03, /* mov $0x3f8, %dx */
|
|
|
|
0x00, 0xd8, /* add %bl, %al */
|
|
|
|
0x04, b'0', /* add $'0', %al */
|
|
|
|
0xee, /* out %al, (%dx) */
|
|
|
|
0xb0, b'\n', /* mov $'\n', %al */
|
|
|
|
0xee, /* out %al, (%dx) */
|
|
|
|
0xf4, /* hlt */
|
|
|
|
];
|
|
|
|
|
|
|
|
let mem_size = 0x1000;
|
|
|
|
let load_addr = GuestAddress(0x1000);
|
|
|
|
let mem = GuestMemoryMmap::new(&[(load_addr, mem_size)]).unwrap();
|
|
|
|
|
|
|
|
let kvm = Kvm::new().expect("new KVM instance creation failed");
|
|
|
|
let vm_fd = kvm.create_vm().expect("new VM fd creation failed");
|
|
|
|
|
|
|
|
mem.with_regions(|index, region| {
|
|
|
|
let mem_region = kvm_userspace_memory_region {
|
|
|
|
slot: index as u32,
|
|
|
|
guest_phys_addr: region.start_addr().raw_value(),
|
|
|
|
memory_size: region.len() as u64,
|
|
|
|
userspace_addr: region.as_ptr() as u64,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Safe because the guest regions are guaranteed not to overlap.
|
2019-06-03 21:09:01 +00:00
|
|
|
unsafe { vm_fd.set_user_memory_region(mem_region) }
|
2019-02-28 13:16:58 +00:00
|
|
|
})
|
|
|
|
.expect("Cannot configure guest memory");
|
|
|
|
mem.write_slice(&code, load_addr)
|
|
|
|
.expect("Writing code to memory failed");
|
|
|
|
|
|
|
|
let vcpu_fd = vm_fd.create_vcpu(0).expect("new VcpuFd failed");
|
|
|
|
|
|
|
|
let mut vcpu_sregs = vcpu_fd.get_sregs().expect("get sregs failed");
|
|
|
|
vcpu_sregs.cs.base = 0;
|
|
|
|
vcpu_sregs.cs.selector = 0;
|
|
|
|
vcpu_fd.set_sregs(&vcpu_sregs).expect("set sregs failed");
|
|
|
|
|
|
|
|
let mut vcpu_regs = vcpu_fd.get_regs().expect("get regs failed");
|
|
|
|
vcpu_regs.rip = 0x1000;
|
|
|
|
vcpu_regs.rax = 2;
|
|
|
|
vcpu_regs.rbx = 3;
|
|
|
|
vcpu_regs.rflags = 2;
|
|
|
|
vcpu_fd.set_regs(&vcpu_regs).expect("set regs failed");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match vcpu_fd.run().expect("run failed") {
|
|
|
|
VcpuExit::IoIn(addr, data) => {
|
|
|
|
println!(
|
|
|
|
"IO in -- addr: {:#x} data [{:?}]",
|
|
|
|
addr,
|
|
|
|
str::from_utf8(&data).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
VcpuExit::IoOut(addr, data) => {
|
|
|
|
println!(
|
|
|
|
"IO out -- addr: {:#x} data [{:?}]",
|
|
|
|
addr,
|
|
|
|
str::from_utf8(&data).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
VcpuExit::MmioRead(_addr, _data) => {}
|
|
|
|
VcpuExit::MmioWrite(_addr, _data) => {}
|
|
|
|
VcpuExit::Unknown => {}
|
|
|
|
VcpuExit::Exception => {}
|
|
|
|
VcpuExit::Hypercall => {}
|
|
|
|
VcpuExit::Debug => {}
|
|
|
|
VcpuExit::Hlt => {
|
|
|
|
println!("HLT");
|
|
|
|
}
|
|
|
|
VcpuExit::IrqWindowOpen => {}
|
|
|
|
VcpuExit::Shutdown => {}
|
|
|
|
VcpuExit::FailEntry => {}
|
|
|
|
VcpuExit::Intr => {}
|
|
|
|
VcpuExit::SetTpr => {}
|
|
|
|
VcpuExit::TprAccess => {}
|
|
|
|
VcpuExit::S390Sieic => {}
|
|
|
|
VcpuExit::S390Reset => {}
|
|
|
|
VcpuExit::Dcr => {}
|
|
|
|
VcpuExit::Nmi => {}
|
|
|
|
VcpuExit::InternalError => {}
|
|
|
|
VcpuExit::Osi => {}
|
|
|
|
VcpuExit::PaprHcall => {}
|
|
|
|
VcpuExit::S390Ucontrol => {}
|
|
|
|
VcpuExit::Watchdog => {}
|
|
|
|
VcpuExit::S390Tsch => {}
|
|
|
|
VcpuExit::Epr => {}
|
|
|
|
VcpuExit::SystemEvent => {}
|
|
|
|
VcpuExit::S390Stsi => {}
|
2019-06-18 17:31:50 +00:00
|
|
|
VcpuExit::IoapicEoi(_vector) => {}
|
2019-02-28 13:16:58 +00:00
|
|
|
VcpuExit::Hyperv => {}
|
|
|
|
}
|
|
|
|
// r => panic!("unexpected exit reason: {:?}", r),
|
|
|
|
}
|
|
|
|
}
|