Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 5 | use std::cmp::{max, min, Reverse}; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 6 | use std::convert::TryFrom; |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 7 | #[cfg(feature = "gpu")] |
| 8 | use std::env; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 9 | use std::error::Error as StdError; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 10 | use std::ffi::CStr; |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 11 | use std::fmt::{self, Display}; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 12 | use std::fs::{File, OpenOptions}; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 13 | use std::io::{self, stdin, Read}; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 14 | use std::iter; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 15 | use std::mem; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 16 | use std::net::Ipv4Addr; |
Daniel Verkamp | 6f9215c | 2019-08-20 09:41:22 -0700 | [diff] [blame] | 17 | #[cfg(feature = "gpu")] |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 18 | use std::num::NonZeroU8; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 19 | use std::num::ParseIntError; |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 20 | use std::os::unix::io::FromRawFd; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 21 | use std::os::unix::net::UnixStream; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 22 | use std::path::{Path, PathBuf}; |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 23 | use std::ptr; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 24 | use std::str; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 25 | use std::sync::{mpsc, Arc, Barrier}; |
| 26 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 27 | use std::thread; |
| 28 | use std::thread::JoinHandle; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 29 | use std::time::Duration; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 30 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 31 | use libc::{self, c_int, gid_t, uid_t}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 32 | |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 33 | use acpi_tables::sdt::SDT; |
| 34 | |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 35 | use base::net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener}; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 36 | use base::*; |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 37 | use devices::virtio::vhost::user::{ |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 38 | Block as VhostUserBlock, Error as VhostUserError, Fs as VhostUserFs, Net as VhostUserNet, |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 39 | Wl as VhostUserWl, |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 40 | }; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 41 | #[cfg(feature = "gpu")] |
| 42 | use devices::virtio::EventDevice; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 43 | use devices::virtio::{self, Console, VirtioDevice}; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 44 | #[cfg(feature = "audio")] |
| 45 | use devices::Ac97Dev; |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 46 | use devices::{ |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 47 | self, IrqChip, IrqEventIndex, KvmKernelIrqChip, PciDevice, VcpuRunState, VfioContainer, |
| 48 | VfioDevice, VfioPciDevice, VirtioPciDevice, |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 49 | }; |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 50 | #[cfg(feature = "usb")] |
| 51 | use devices::{HostBackendDeviceProvider, XhciController}; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 52 | use hypervisor::kvm::{Kvm, KvmVcpu, KvmVm}; |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 53 | use hypervisor::{HypervisorCap, Vcpu, VcpuExit, VcpuRunHandle, Vm, VmCap}; |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 54 | use minijail::{self, Minijail}; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 55 | use net_util::{Error as NetError, MacAddress, Tap}; |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 56 | use remain::sorted; |
Xiong Zhang | 87a3b44 | 2019-10-29 17:32:44 +0800 | [diff] [blame] | 57 | use resources::{Alloc, MmioType, SystemAllocator}; |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 58 | use rutabaga_gfx::RutabagaGralloc; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 59 | use sync::Mutex; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 60 | use vm_control::*; |
Sergey Senozhatsky | d78d05b | 2021-04-13 20:59:58 +0900 | [diff] [blame] | 61 | use vm_memory::{GuestAddress, GuestMemory, MemoryPolicy}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 62 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 63 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 64 | use crate::gdb::{gdb_thread, GdbStub}; |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 65 | use crate::{ |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 66 | Config, DiskOption, Executable, SharedDir, SharedDirKind, TouchDeviceOption, VhostUserFsOption, |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 67 | VhostUserOption, VhostUserWlOption, |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 68 | }; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 69 | use arch::{ |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 70 | self, LinuxArch, RunnableLinuxVm, SerialHardware, SerialParameters, VcpuAffinity, |
| 71 | VirtioDeviceStub, VmComponents, VmImage, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 72 | }; |
Sonny Rao | ed517d1 | 2018-02-13 22:09:43 -0800 | [diff] [blame] | 73 | |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 74 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 75 | use { |
| 76 | aarch64::AArch64 as Arch, |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 77 | devices::IrqChipAArch64 as IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 78 | hypervisor::{VcpuAArch64 as VcpuArch, VmAArch64 as VmArch}, |
| 79 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 80 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 81 | use { |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 82 | devices::{IrqChipX86_64 as IrqChipArch, KvmSplitIrqChip}, |
| 83 | hypervisor::{VcpuX86_64 as VcpuArch, VmX86_64 as VmArch}, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 84 | x86_64::X8664arch as Arch, |
| 85 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 86 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 87 | #[sorted] |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 88 | #[derive(Debug)] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 89 | pub enum Error { |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 90 | AddGpuDeviceMemory(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 91 | AddIrqChipVcpu(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 92 | AddPmemDeviceMemory(base::Error), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 93 | AllocateGpuDeviceAddress, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 94 | AllocatePmemDeviceAddress(resources::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 95 | BalloonActualTooLarge, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 96 | BalloonDeviceNew(virtio::BalloonError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 97 | BlockDeviceNew(base::Error), |
| 98 | BlockSignal(base::signal::Error), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 99 | BuildVm(<Arch as LinuxArch>::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 100 | ChownTpmStorage(base::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 101 | CloneEvent(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 102 | CloneVcpu(base::Error), |
| 103 | ConfigureVcpu(<Arch as LinuxArch>::Error), |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 104 | ConnectTube(io::Error), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 105 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 106 | CreateAc97(devices::PciDeviceError), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 107 | CreateConsole(arch::serial::Error), |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 108 | CreateControlServer(io::Error), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 109 | CreateDiskError(disk::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 110 | CreateEvent(base::Error), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 111 | CreateGrallocError(rutabaga_gfx::RutabagaError), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 112 | CreateGuestMemory(vm_memory::GuestMemoryError), |
| 113 | CreateIrqChip(base::Error), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 114 | CreateKvm(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 115 | CreateSignalFd(base::SignalFdError), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 116 | CreateSocket(io::Error), |
Chirantan Ekbote | 49fa08f | 2018-11-16 13:26:53 -0800 | [diff] [blame] | 117 | CreateTapDevice(NetError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 118 | CreateTimer(base::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 119 | CreateTpmStorage(PathBuf, io::Error), |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 120 | CreateTube(TubeError), |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 121 | #[cfg(feature = "usb")] |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 122 | CreateUsbProvider(devices::usb::host_backend::error::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 123 | CreateVcpu(base::Error), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 124 | CreateVfioDevice(devices::vfio::VfioError), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 125 | CreateVm(base::Error), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 126 | CreateWaitContext(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 127 | DeviceJail(minijail::Error), |
| 128 | DevicePivotRoot(minijail::Error), |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 129 | #[cfg(feature = "direct")] |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 130 | DirectIo(io::Error), |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 131 | #[cfg(feature = "direct")] |
| 132 | DirectIrq(devices::DirectIrqError), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 133 | Disk(PathBuf, io::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 134 | DiskImageLock(base::Error), |
| 135 | DropCapabilities(base::Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 136 | FsDeviceNew(virtio::fs::Error), |
| 137 | GetMaxOpenFiles(io::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 138 | GetSignalMask(signal::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 139 | GuestCachedMissing(), |
| 140 | GuestCachedTooLarge(std::num::TryFromIntError), |
| 141 | GuestFreeMissing(), |
| 142 | GuestFreeTooLarge(std::num::TryFromIntError), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 143 | GuestMemoryLayout(<Arch as LinuxArch>::Error), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 144 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 145 | HandleDebugCommand(<Arch as LinuxArch>::Error), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 146 | InputDeviceNew(virtio::InputError), |
| 147 | InputEventsOpen(std::io::Error), |
Dylan Reid | 2056644 | 2018-04-02 15:06:15 -0700 | [diff] [blame] | 148 | InvalidFdPath, |
Zach Reizner | 579bd2c | 2018-09-14 15:43:33 -0700 | [diff] [blame] | 149 | InvalidWaylandPath, |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 150 | IoJail(minijail::Error), |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 151 | LoadKernel(Box<dyn StdError>), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 152 | MemoryTooLarge, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 153 | NetDeviceNew(virtio::NetError), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 154 | OpenAcpiTable(PathBuf, io::Error), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 155 | OpenAndroidFstab(PathBuf, io::Error), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 156 | OpenBios(PathBuf, io::Error), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 157 | OpenInitrd(PathBuf, io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 158 | OpenKernel(PathBuf, io::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 159 | OpenVinput(PathBuf, io::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 160 | P9DeviceNew(virtio::P9Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 161 | ParseMaxOpenFiles(ParseIntError), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 162 | PivotRootDoesntExist(&'static str), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 163 | PmemDeviceImageTooBig, |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 164 | PmemDeviceNew(base::Error), |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 165 | ReadMemAvailable(io::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 166 | ReadStatm(io::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 167 | RegisterBalloon(arch::DeviceRegistrationError), |
| 168 | RegisterBlock(arch::DeviceRegistrationError), |
| 169 | RegisterGpu(arch::DeviceRegistrationError), |
| 170 | RegisterNet(arch::DeviceRegistrationError), |
| 171 | RegisterP9(arch::DeviceRegistrationError), |
| 172 | RegisterRng(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 173 | RegisterSignalHandler(base::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 174 | RegisterWayland(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 175 | ReserveGpuMemory(base::MmapError), |
| 176 | ReserveMemory(base::Error), |
| 177 | ReservePmemMemory(base::MmapError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 178 | ResetTimer(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 179 | RngDeviceNew(virtio::RngError), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 180 | RunnableVcpu(base::Error), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 181 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 182 | SendDebugStatus(Box<mpsc::SendError<VcpuDebugStatusMessage>>), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 183 | SettingGidMap(minijail::Error), |
| 184 | SettingMaxOpenFiles(minijail::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 185 | SettingSignalMask(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 186 | SettingUidMap(minijail::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 187 | SignalFd(base::SignalFdError), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 188 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 189 | SpawnGdbServer(io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 190 | SpawnVcpu(io::Error), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 191 | Timer(base::Error), |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 192 | ValidateRawDescriptor(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 193 | VhostNetDeviceNew(virtio::vhost::Error), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 194 | VhostUserBlockDeviceNew(VhostUserError), |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 195 | VhostUserFsDeviceNew(VhostUserError), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 196 | VhostUserNetDeviceNew(VhostUserError), |
| 197 | VhostUserNetWithNetArgs, |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 198 | VhostUserWlDeviceNew(VhostUserError), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 199 | VhostVsockDeviceNew(virtio::vhost::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 200 | VirtioPciDev(base::Error), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 201 | WaitContextAdd(base::Error), |
| 202 | WaitContextDelete(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 203 | WaylandDeviceNew(base::Error), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 204 | } |
| 205 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 206 | impl Display for Error { |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 207 | #[remain::check] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 208 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 209 | use self::Error::*; |
| 210 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 211 | #[sorted] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 212 | match self { |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 213 | AddGpuDeviceMemory(e) => write!(f, "failed to add gpu device memory: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 214 | AddIrqChipVcpu(e) => write!(f, "failed to add vcpu to irq chip: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 215 | AddPmemDeviceMemory(e) => write!(f, "failed to add pmem device memory: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 216 | AllocateGpuDeviceAddress => write!(f, "failed to allocate gpu device guest address"), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 217 | AllocatePmemDeviceAddress(e) => { |
| 218 | write!(f, "failed to allocate memory for pmem device: {}", e) |
| 219 | } |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 220 | BalloonActualTooLarge => write!(f, "balloon actual size is too large"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 221 | BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), |
| 222 | BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), |
| 223 | BlockSignal(e) => write!(f, "failed to block signal: {}", e), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 224 | BuildVm(e) => write!(f, "The architecture failed to build the vm: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 225 | ChownTpmStorage(e) => write!(f, "failed to chown tpm storage: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 226 | CloneEvent(e) => write!(f, "failed to clone event: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 227 | CloneVcpu(e) => write!(f, "failed to clone vcpu: {}", e), |
| 228 | ConfigureVcpu(e) => write!(f, "failed to configure vcpu: {}", e), |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 229 | ConnectTube(e) => write!(f, "failed to connect to tube: {}", e), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 230 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 231 | CreateAc97(e) => write!(f, "failed to create ac97 device: {}", e), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 232 | CreateConsole(e) => write!(f, "failed to create console device: {}", e), |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 233 | CreateControlServer(e) => write!(f, "failed to create control server: {}", e), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 234 | CreateDiskError(e) => write!(f, "failed to create virtual disk: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 235 | CreateEvent(e) => write!(f, "failed to create event: {}", e), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 236 | CreateGrallocError(e) => write!(f, "failed to create gralloc: {}", e), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 237 | CreateGuestMemory(e) => write!(f, "failed to create guest memory: {}", e), |
| 238 | CreateIrqChip(e) => write!(f, "failed to create IRQ chip: {}", e), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 239 | CreateKvm(e) => write!(f, "failed to create kvm: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 240 | CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), |
| 241 | CreateSocket(e) => write!(f, "failed to create socket: {}", e), |
| 242 | CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 243 | CreateTimer(e) => write!(f, "failed to create Timer: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 244 | CreateTpmStorage(p, e) => { |
| 245 | write!(f, "failed to create tpm storage dir {}: {}", p.display(), e) |
| 246 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 247 | CreateTube(e) => write!(f, "failed to create tube: {}", e), |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 248 | #[cfg(feature = "usb")] |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 249 | CreateUsbProvider(e) => write!(f, "failed to create usb provider: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 250 | CreateVcpu(e) => write!(f, "failed to create vcpu: {}", e), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 251 | CreateVfioDevice(e) => write!(f, "Failed to create vfio device {}", e), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 252 | CreateVm(e) => write!(f, "failed to create vm: {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 253 | CreateWaitContext(e) => write!(f, "failed to create wait context: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 254 | DeviceJail(e) => write!(f, "failed to jail device: {}", e), |
| 255 | DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 256 | #[cfg(feature = "direct")] |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 257 | DirectIo(e) => write!(f, "failed to open direct io device: {}", e), |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 258 | #[cfg(feature = "direct")] |
| 259 | DirectIrq(e) => write!(f, "failed to enable interrupt forwarding: {}", e), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 260 | Disk(p, e) => write!(f, "failed to load disk image {}: {}", p.display(), e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 261 | DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 262 | DropCapabilities(e) => write!(f, "failed to drop process capabilities: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 263 | FsDeviceNew(e) => write!(f, "failed to create fs device: {}", e), |
| 264 | GetMaxOpenFiles(e) => write!(f, "failed to get max number of open files: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 265 | GetSignalMask(e) => write!(f, "failed to retrieve signal mask for vcpu: {}", e), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 266 | GuestCachedMissing() => write!(f, "guest cached is missing from balloon stats"), |
| 267 | GuestCachedTooLarge(e) => write!(f, "guest cached is too large: {}", e), |
| 268 | GuestFreeMissing() => write!(f, "guest free is missing from balloon stats"), |
| 269 | GuestFreeTooLarge(e) => write!(f, "guest free is too large: {}", e), |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 270 | GuestMemoryLayout(e) => write!(f, "failed to create guest memory layout: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 271 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 272 | HandleDebugCommand(e) => write!(f, "failed to handle a gdb command: {}", e), |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 273 | InputDeviceNew(e) => write!(f, "failed to set up input device: {}", e), |
| 274 | InputEventsOpen(e) => write!(f, "failed to open event device: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 275 | InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), |
| 276 | InvalidWaylandPath => write!(f, "wayland socket path has no parent or file name"), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 277 | IoJail(e) => write!(f, "{}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 278 | LoadKernel(e) => write!(f, "failed to load kernel: {}", e), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 279 | MemoryTooLarge => write!(f, "requested memory size too large"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 280 | NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 281 | OpenAcpiTable(p, e) => write!(f, "failed to open ACPI file {}: {}", p.display(), e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 282 | OpenAndroidFstab(p, e) => write!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 283 | f, |
| 284 | "failed to open android fstab file {}: {}", |
| 285 | p.display(), |
| 286 | e |
| 287 | ), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 288 | OpenBios(p, e) => write!(f, "failed to open bios {}: {}", p.display(), e), |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 289 | OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), |
| 290 | OpenKernel(p, e) => write!(f, "failed to open kernel image {}: {}", p.display(), e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 291 | OpenVinput(p, e) => write!(f, "failed to open vinput device {}: {}", p.display(), e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 292 | P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 293 | ParseMaxOpenFiles(e) => write!(f, "failed to parse max number of open files: {}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 294 | PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 295 | PmemDeviceImageTooBig => { |
| 296 | write!(f, "failed to create pmem device: pmem device image too big") |
| 297 | } |
| 298 | PmemDeviceNew(e) => write!(f, "failed to create pmem device: {}", e), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 299 | ReadMemAvailable(e) => write!( |
| 300 | f, |
| 301 | "failed to read /sys/kernel/mm/chromeos-low_mem/available: {}", |
| 302 | e |
| 303 | ), |
| 304 | ReadStatm(e) => write!(f, "failed to read /proc/self/statm: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 305 | RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), |
| 306 | RegisterBlock(e) => write!(f, "error registering block device: {}", e), |
| 307 | RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), |
| 308 | RegisterNet(e) => write!(f, "error registering net device: {}", e), |
| 309 | RegisterP9(e) => write!(f, "error registering 9p device: {}", e), |
| 310 | RegisterRng(e) => write!(f, "error registering rng device: {}", e), |
| 311 | RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), |
| 312 | RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 313 | ReserveGpuMemory(e) => write!(f, "failed to reserve gpu memory: {}", e), |
| 314 | ReserveMemory(e) => write!(f, "failed to reserve memory: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 315 | ReservePmemMemory(e) => write!(f, "failed to reserve pmem memory: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 316 | ResetTimer(e) => write!(f, "failed to reset Timer: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 317 | RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 318 | RunnableVcpu(e) => write!(f, "failed to set thread id for vcpu: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 319 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 320 | SendDebugStatus(e) => write!(f, "failed to send a debug status to GDB thread: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 321 | SettingGidMap(e) => write!(f, "error setting GID map: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 322 | SettingMaxOpenFiles(e) => write!(f, "error setting max open files: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 323 | SettingSignalMask(e) => write!(f, "failed to set the signal mask for vcpu: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 324 | SettingUidMap(e) => write!(f, "error setting UID map: {}", e), |
| 325 | SignalFd(e) => write!(f, "failed to read signal fd: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 326 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 327 | SpawnGdbServer(e) => write!(f, "failed to spawn GDB thread: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 328 | SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 329 | Timer(e) => write!(f, "failed to read timer fd: {}", e), |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 330 | ValidateRawDescriptor(e) => write!(f, "failed to validate raw descriptor: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 331 | VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 332 | VhostUserBlockDeviceNew(e) => { |
| 333 | write!(f, "failed to set up vhost-user block device: {}", e) |
| 334 | } |
Tomasz Jeznach | ccb2694 | 2021-03-30 22:44:11 -0700 | [diff] [blame] | 335 | VhostUserFsDeviceNew(e) => write!(f, "failed to set up vhost-user fs device: {}", e), |
| 336 | VhostUserNetDeviceNew(e) => write!(f, "failed to set up vhost-user net device: {}", e), |
| 337 | VhostUserNetWithNetArgs => write!( |
| 338 | f, |
| 339 | "vhost-user-net cannot be used with any of --host_ip, --netmask or --mac" |
| 340 | ), |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 341 | VhostUserWlDeviceNew(e) => { |
| 342 | write!(f, "failed to set up vhost-user wl device: {}", e) |
| 343 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 344 | VhostVsockDeviceNew(e) => write!(f, "failed to set up virtual socket device: {}", e), |
| 345 | VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 346 | WaitContextAdd(e) => write!(f, "failed to add descriptor to wait context: {}", e), |
| 347 | WaitContextDelete(e) => { |
| 348 | write!(f, "failed to remove descriptor from wait context: {}", e) |
| 349 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 350 | WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 355 | impl From<minijail::Error> for Error { |
| 356 | fn from(err: minijail::Error) -> Self { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 357 | Error::IoJail(err) |
| 358 | } |
| 359 | } |
| 360 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 361 | impl std::error::Error for Error {} |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 362 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 363 | type Result<T> = std::result::Result<T, Error>; |
| 364 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 365 | enum TaggedControlTube { |
| 366 | Fs(Tube), |
| 367 | Vm(Tube), |
| 368 | VmMemory(Tube), |
| 369 | VmIrq(Tube), |
| 370 | VmMsync(Tube), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 373 | impl AsRef<Tube> for TaggedControlTube { |
| 374 | fn as_ref(&self) -> &Tube { |
| 375 | use self::TaggedControlTube::*; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 376 | match &self { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 377 | Fs(tube) | Vm(tube) | VmMemory(tube) | VmIrq(tube) | VmMsync(tube) => tube, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 382 | impl AsRawDescriptor for TaggedControlTube { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 383 | fn as_raw_descriptor(&self) -> RawDescriptor { |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 384 | self.as_ref().as_raw_descriptor() |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Andrew Walbran | f50bab6 | 2020-07-07 13:22:53 +0100 | [diff] [blame] | 388 | fn get_max_open_files() -> Result<u64> { |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 389 | let mut buf = mem::MaybeUninit::<libc::rlimit64>::zeroed(); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 390 | |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 391 | // Safe because this will only modify `buf` and we check the return value. |
| 392 | let res = unsafe { libc::prlimit64(0, libc::RLIMIT_NOFILE, ptr::null(), buf.as_mut_ptr()) }; |
| 393 | if res == 0 { |
| 394 | // Safe because the kernel guarantees that the struct is fully initialized. |
| 395 | let limit = unsafe { buf.assume_init() }; |
| 396 | Ok(limit.rlim_max) |
| 397 | } else { |
| 398 | Err(Error::GetMaxOpenFiles(io::Error::last_os_error())) |
| 399 | } |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 400 | } |
| 401 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 402 | struct SandboxConfig<'a> { |
| 403 | limit_caps: bool, |
| 404 | log_failures: bool, |
| 405 | seccomp_policy: &'a Path, |
| 406 | uid_map: Option<&'a str>, |
| 407 | gid_map: Option<&'a str>, |
| 408 | } |
| 409 | |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 410 | fn create_base_minijail( |
| 411 | root: &Path, |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 412 | r_limit: Option<u64>, |
| 413 | config: Option<&SandboxConfig>, |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 414 | ) -> Result<Minijail> { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 415 | // All child jails run in a new user namespace without any users mapped, |
| 416 | // they run as nobody unless otherwise configured. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 417 | let mut j = Minijail::new().map_err(Error::DeviceJail)?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 418 | |
| 419 | if let Some(config) = config { |
| 420 | j.namespace_pids(); |
| 421 | j.namespace_user(); |
| 422 | j.namespace_user_disable_setgroups(); |
| 423 | if config.limit_caps { |
| 424 | // Don't need any capabilities. |
| 425 | j.use_caps(0); |
| 426 | } |
| 427 | if let Some(uid_map) = config.uid_map { |
| 428 | j.uidmap(uid_map).map_err(Error::SettingUidMap)?; |
| 429 | } |
| 430 | if let Some(gid_map) = config.gid_map { |
| 431 | j.gidmap(gid_map).map_err(Error::SettingGidMap)?; |
| 432 | } |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 433 | // Run in a new mount namespace. |
| 434 | j.namespace_vfs(); |
| 435 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 436 | // Run in an empty network namespace. |
| 437 | j.namespace_net(); |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 438 | |
| 439 | // Don't allow the device to gain new privileges. |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 440 | j.no_new_privs(); |
| 441 | |
| 442 | // By default we'll prioritize using the pre-compiled .bpf over the .policy |
| 443 | // file (the .bpf is expected to be compiled using "trap" as the failure |
| 444 | // behavior instead of the default "kill" behavior). |
| 445 | // Refer to the code comment for the "seccomp-log-failures" |
| 446 | // command-line parameter for an explanation about why the |log_failures| |
| 447 | // flag forces the use of .policy files (and the build-time alternative to |
| 448 | // this run-time flag). |
| 449 | let bpf_policy_file = config.seccomp_policy.with_extension("bpf"); |
| 450 | if bpf_policy_file.exists() && !config.log_failures { |
| 451 | j.parse_seccomp_program(&bpf_policy_file) |
| 452 | .map_err(Error::DeviceJail)?; |
| 453 | } else { |
| 454 | // Use TSYNC only for the side effect of it using SECCOMP_RET_TRAP, |
| 455 | // which will correctly kill the entire device process if a worker |
| 456 | // thread commits a seccomp violation. |
| 457 | j.set_seccomp_filter_tsync(); |
| 458 | if config.log_failures { |
| 459 | j.log_seccomp_filter_failures(); |
| 460 | } |
| 461 | j.parse_seccomp_filters(&config.seccomp_policy.with_extension("policy")) |
| 462 | .map_err(Error::DeviceJail)?; |
| 463 | } |
| 464 | j.use_seccomp_filter(); |
| 465 | // Don't do init setup. |
| 466 | j.run_as_init(); |
| 467 | } |
| 468 | |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 469 | // Only pivot_root if we are not re-using the current root directory. |
| 470 | if root != Path::new("/") { |
| 471 | // It's safe to call `namespace_vfs` multiple times. |
| 472 | j.namespace_vfs(); |
| 473 | j.enter_pivot_root(root).map_err(Error::DevicePivotRoot)?; |
| 474 | } |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 475 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 476 | // Most devices don't need to open many fds. |
| 477 | let limit = if let Some(r) = r_limit { r } else { 1024u64 }; |
| 478 | j.set_rlimit(libc::RLIMIT_NOFILE as i32, limit, limit) |
| 479 | .map_err(Error::SettingMaxOpenFiles)?; |
| 480 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 481 | Ok(j) |
| 482 | } |
| 483 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 484 | fn simple_jail(cfg: &Config, policy: &str) -> Result<Option<Minijail>> { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 485 | if cfg.sandbox { |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 486 | let pivot_root: &str = option_env!("DEFAULT_PIVOT_ROOT").unwrap_or("/var/empty"); |
| 487 | // A directory for a jailed device's pivot root. |
| 488 | let root_path = Path::new(pivot_root); |
| 489 | if !root_path.exists() { |
| 490 | return Err(Error::PivotRootDoesntExist(pivot_root)); |
| 491 | } |
| 492 | let policy_path: PathBuf = cfg.seccomp_policy_dir.join(policy); |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 493 | let config = SandboxConfig { |
| 494 | limit_caps: true, |
| 495 | log_failures: cfg.seccomp_log_failures, |
| 496 | seccomp_policy: &policy_path, |
| 497 | uid_map: None, |
| 498 | gid_map: None, |
| 499 | }; |
| 500 | Ok(Some(create_base_minijail(root_path, None, Some(&config))?)) |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 501 | } else { |
| 502 | Ok(None) |
| 503 | } |
| 504 | } |
| 505 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 506 | type DeviceResult<T = VirtioDeviceStub> = std::result::Result<T, Error>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 507 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 508 | fn create_block_device(cfg: &Config, disk: &DiskOption, disk_device_tube: Tube) -> DeviceResult { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 509 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 510 | let raw_image: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 511 | // Safe because we will validate |raw_fd|. |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 512 | unsafe { File::from_raw_descriptor(raw_descriptor_from_path(&disk.path)?) } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 513 | } else { |
| 514 | OpenOptions::new() |
| 515 | .read(true) |
| 516 | .write(!disk.read_only) |
| 517 | .open(&disk.path) |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 518 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))? |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 519 | }; |
| 520 | // Lock the disk image to prevent other crosvm instances from using it. |
| 521 | let lock_op = if disk.read_only { |
| 522 | FlockOperation::LockShared |
| 523 | } else { |
| 524 | FlockOperation::LockExclusive |
| 525 | }; |
| 526 | flock(&raw_image, lock_op, true).map_err(Error::DiskImageLock)?; |
| 527 | |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 528 | let dev = if disk::async_ok(&raw_image).map_err(Error::CreateDiskError)? { |
| 529 | let async_file = disk::create_async_disk_file(raw_image).map_err(Error::CreateDiskError)?; |
| 530 | Box::new( |
| 531 | virtio::BlockAsync::new( |
| 532 | virtio::base_features(cfg.protected_vm), |
| 533 | async_file, |
| 534 | disk.read_only, |
| 535 | disk.sparse, |
| 536 | disk.block_size, |
Daniel Verkamp | dd0ee59 | 2021-03-29 13:05:22 -0700 | [diff] [blame] | 537 | disk.id, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 538 | Some(disk_device_tube), |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 539 | ) |
| 540 | .map_err(Error::BlockDeviceNew)?, |
| 541 | ) as Box<dyn VirtioDevice> |
| 542 | } else { |
| 543 | let disk_file = disk::create_disk_file(raw_image).map_err(Error::CreateDiskError)?; |
| 544 | Box::new( |
| 545 | virtio::Block::new( |
| 546 | virtio::base_features(cfg.protected_vm), |
| 547 | disk_file, |
| 548 | disk.read_only, |
| 549 | disk.sparse, |
| 550 | disk.block_size, |
| 551 | disk.id, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 552 | Some(disk_device_tube), |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 553 | ) |
| 554 | .map_err(Error::BlockDeviceNew)?, |
| 555 | ) as Box<dyn VirtioDevice> |
| 556 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 557 | |
| 558 | Ok(VirtioDeviceStub { |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 559 | dev, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 560 | jail: simple_jail(&cfg, "block_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 561 | }) |
| 562 | } |
| 563 | |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 564 | fn create_vhost_user_block_device(cfg: &Config, opt: &VhostUserOption) -> DeviceResult { |
| 565 | let dev = VhostUserBlock::new(virtio::base_features(cfg.protected_vm), &opt.socket) |
| 566 | .map_err(Error::VhostUserBlockDeviceNew)?; |
| 567 | |
| 568 | Ok(VirtioDeviceStub { |
| 569 | dev: Box::new(dev), |
| 570 | // no sandbox here because virtqueue handling is exported to a different process. |
| 571 | jail: None, |
| 572 | }) |
| 573 | } |
| 574 | |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 575 | fn create_vhost_user_fs_device(cfg: &Config, option: &VhostUserFsOption) -> DeviceResult { |
| 576 | let dev = VhostUserFs::new( |
| 577 | virtio::base_features(cfg.protected_vm), |
| 578 | &option.socket, |
| 579 | &option.tag, |
| 580 | ) |
| 581 | .map_err(Error::VhostUserFsDeviceNew)?; |
| 582 | |
| 583 | Ok(VirtioDeviceStub { |
| 584 | dev: Box::new(dev), |
| 585 | // no sandbox here because virtqueue handling is exported to a different process. |
| 586 | jail: None, |
| 587 | }) |
| 588 | } |
| 589 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 590 | fn create_rng_device(cfg: &Config) -> DeviceResult { |
Keiichi Watanabe | f70350b | 2020-11-24 21:57:53 +0900 | [diff] [blame] | 591 | let dev = |
| 592 | virtio::Rng::new(virtio::base_features(cfg.protected_vm)).map_err(Error::RngDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 593 | |
| 594 | Ok(VirtioDeviceStub { |
| 595 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 596 | jail: simple_jail(&cfg, "rng_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 597 | }) |
| 598 | } |
| 599 | |
| 600 | #[cfg(feature = "tpm")] |
| 601 | fn create_tpm_device(cfg: &Config) -> DeviceResult { |
| 602 | use std::ffi::CString; |
| 603 | use std::fs; |
| 604 | use std::process; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 605 | |
| 606 | let tpm_storage: PathBuf; |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 607 | let mut tpm_jail = simple_jail(&cfg, "tpm_device")?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 608 | |
| 609 | match &mut tpm_jail { |
| 610 | Some(jail) => { |
| 611 | // Create a tmpfs in the device's root directory for tpm |
| 612 | // simulator storage. The size is 20*1024, or 20 KB. |
| 613 | jail.mount_with_data( |
| 614 | Path::new("none"), |
| 615 | Path::new("/"), |
| 616 | "tmpfs", |
| 617 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 618 | "size=20480", |
| 619 | )?; |
| 620 | |
| 621 | let crosvm_ids = add_crosvm_user_to_jail(jail, "tpm")?; |
| 622 | |
| 623 | let pid = process::id(); |
| 624 | let tpm_pid_dir = format!("/run/vm/tpm.{}", pid); |
| 625 | tpm_storage = Path::new(&tpm_pid_dir).to_owned(); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 626 | fs::create_dir_all(&tpm_storage) |
| 627 | .map_err(|e| Error::CreateTpmStorage(tpm_storage.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 628 | let tpm_pid_dir_c = CString::new(tpm_pid_dir).expect("no nul bytes"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 629 | chown(&tpm_pid_dir_c, crosvm_ids.uid, crosvm_ids.gid) |
| 630 | .map_err(Error::ChownTpmStorage)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 631 | |
| 632 | jail.mount_bind(&tpm_storage, &tpm_storage, true)?; |
| 633 | } |
| 634 | None => { |
| 635 | // Path used inside cros_sdk which does not have /run/vm. |
| 636 | tpm_storage = Path::new("/tmp/tpm-simulator").to_owned(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | let dev = virtio::Tpm::new(tpm_storage); |
| 641 | |
| 642 | Ok(VirtioDeviceStub { |
| 643 | dev: Box::new(dev), |
| 644 | jail: tpm_jail, |
| 645 | }) |
| 646 | } |
| 647 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 648 | fn create_single_touch_device( |
| 649 | cfg: &Config, |
| 650 | single_touch_spec: &TouchDeviceOption, |
| 651 | idx: u32, |
| 652 | ) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 653 | let socket = single_touch_spec |
| 654 | .get_path() |
| 655 | .into_unix_stream() |
| 656 | .map_err(|e| { |
| 657 | error!("failed configuring virtio single touch: {:?}", e); |
| 658 | e |
| 659 | })?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 660 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 661 | let (width, height) = single_touch_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 662 | let dev = virtio::new_single_touch( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 663 | idx, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 664 | socket, |
| 665 | width, |
| 666 | height, |
| 667 | virtio::base_features(cfg.protected_vm), |
| 668 | ) |
| 669 | .map_err(Error::InputDeviceNew)?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 670 | Ok(VirtioDeviceStub { |
| 671 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 672 | jail: simple_jail(&cfg, "input_device")?, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 673 | }) |
| 674 | } |
| 675 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 676 | fn create_multi_touch_device( |
| 677 | cfg: &Config, |
| 678 | multi_touch_spec: &TouchDeviceOption, |
| 679 | idx: u32, |
| 680 | ) -> DeviceResult { |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 681 | let socket = multi_touch_spec |
| 682 | .get_path() |
| 683 | .into_unix_stream() |
| 684 | .map_err(|e| { |
| 685 | error!("failed configuring virtio multi touch: {:?}", e); |
| 686 | e |
| 687 | })?; |
| 688 | |
| 689 | let (width, height) = multi_touch_spec.get_size(); |
| 690 | let dev = virtio::new_multi_touch( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 691 | idx, |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 692 | socket, |
| 693 | width, |
| 694 | height, |
| 695 | virtio::base_features(cfg.protected_vm), |
| 696 | ) |
| 697 | .map_err(Error::InputDeviceNew)?; |
| 698 | |
| 699 | Ok(VirtioDeviceStub { |
| 700 | dev: Box::new(dev), |
| 701 | jail: simple_jail(&cfg, "input_device")?, |
| 702 | }) |
| 703 | } |
| 704 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 705 | fn create_trackpad_device( |
| 706 | cfg: &Config, |
| 707 | trackpad_spec: &TouchDeviceOption, |
| 708 | idx: u32, |
| 709 | ) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 710 | let socket = trackpad_spec.get_path().into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 711 | error!("failed configuring virtio trackpad: {}", e); |
| 712 | e |
| 713 | })?; |
| 714 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 715 | let (width, height) = trackpad_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 716 | let dev = virtio::new_trackpad( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 717 | idx, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 718 | socket, |
| 719 | width, |
| 720 | height, |
| 721 | virtio::base_features(cfg.protected_vm), |
| 722 | ) |
| 723 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 724 | |
| 725 | Ok(VirtioDeviceStub { |
| 726 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 727 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 728 | }) |
| 729 | } |
| 730 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 731 | fn create_mouse_device<T: IntoUnixStream>(cfg: &Config, mouse_socket: T, idx: u32) -> DeviceResult { |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 732 | let socket = mouse_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 733 | error!("failed configuring virtio mouse: {}", e); |
| 734 | e |
| 735 | })?; |
| 736 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 737 | let dev = virtio::new_mouse(idx, socket, virtio::base_features(cfg.protected_vm)) |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 738 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 739 | |
| 740 | Ok(VirtioDeviceStub { |
| 741 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 742 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 743 | }) |
| 744 | } |
| 745 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 746 | fn create_keyboard_device<T: IntoUnixStream>( |
| 747 | cfg: &Config, |
| 748 | keyboard_socket: T, |
| 749 | idx: u32, |
| 750 | ) -> DeviceResult { |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 751 | let socket = keyboard_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 752 | error!("failed configuring virtio keyboard: {}", e); |
| 753 | e |
| 754 | })?; |
| 755 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 756 | let dev = virtio::new_keyboard(idx, socket, virtio::base_features(cfg.protected_vm)) |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 757 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 758 | |
| 759 | Ok(VirtioDeviceStub { |
| 760 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 761 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 762 | }) |
| 763 | } |
| 764 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 765 | fn create_switches_device<T: IntoUnixStream>( |
| 766 | cfg: &Config, |
| 767 | switches_socket: T, |
| 768 | idx: u32, |
| 769 | ) -> DeviceResult { |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 770 | let socket = switches_socket.into_unix_stream().map_err(|e| { |
| 771 | error!("failed configuring virtio switches: {}", e); |
| 772 | e |
| 773 | })?; |
| 774 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 775 | let dev = virtio::new_switches(idx, socket, virtio::base_features(cfg.protected_vm)) |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 776 | .map_err(Error::InputDeviceNew)?; |
| 777 | |
| 778 | Ok(VirtioDeviceStub { |
| 779 | dev: Box::new(dev), |
| 780 | jail: simple_jail(&cfg, "input_device")?, |
| 781 | }) |
| 782 | } |
| 783 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 784 | fn create_vinput_device(cfg: &Config, dev_path: &Path) -> DeviceResult { |
| 785 | let dev_file = OpenOptions::new() |
| 786 | .read(true) |
| 787 | .write(true) |
| 788 | .open(dev_path) |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 789 | .map_err(|e| Error::OpenVinput(dev_path.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 790 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 791 | let dev = virtio::new_evdev(dev_file, virtio::base_features(cfg.protected_vm)) |
| 792 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 793 | |
| 794 | Ok(VirtioDeviceStub { |
| 795 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 796 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 797 | }) |
| 798 | } |
| 799 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 800 | fn create_balloon_device(cfg: &Config, tube: Tube) -> DeviceResult { |
| 801 | let dev = virtio::Balloon::new(virtio::base_features(cfg.protected_vm), tube) |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 802 | .map_err(Error::BalloonDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 803 | |
| 804 | Ok(VirtioDeviceStub { |
| 805 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 806 | jail: simple_jail(&cfg, "balloon_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 807 | }) |
| 808 | } |
| 809 | |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 810 | fn create_tap_net_device(cfg: &Config, tap_fd: RawDescriptor) -> DeviceResult { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 811 | // Safe because we ensure that we get a unique handle to the fd. |
| 812 | let tap = unsafe { |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 813 | Tap::from_raw_descriptor( |
| 814 | validate_raw_descriptor(tap_fd).map_err(Error::ValidateRawDescriptor)?, |
| 815 | ) |
| 816 | .map_err(Error::CreateTapDevice)? |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 817 | }; |
| 818 | |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 819 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 820 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 821 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 822 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 823 | vq_pairs = 1; |
| 824 | } |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 825 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 826 | let dev = virtio::Net::from(features, tap, vq_pairs).map_err(Error::NetDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 827 | |
| 828 | Ok(VirtioDeviceStub { |
| 829 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 830 | jail: simple_jail(&cfg, "net_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 831 | }) |
| 832 | } |
| 833 | |
| 834 | fn create_net_device( |
| 835 | cfg: &Config, |
| 836 | host_ip: Ipv4Addr, |
| 837 | netmask: Ipv4Addr, |
| 838 | mac_address: MacAddress, |
| 839 | mem: &GuestMemory, |
| 840 | ) -> DeviceResult { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 841 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 842 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 843 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 844 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 845 | vq_pairs = 1; |
| 846 | } |
| 847 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 848 | let features = virtio::base_features(cfg.protected_vm); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 849 | let dev = if cfg.vhost_net { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 850 | let dev = virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new( |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 851 | &cfg.vhost_net_device_path, |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 852 | features, |
| 853 | host_ip, |
| 854 | netmask, |
| 855 | mac_address, |
| 856 | mem, |
| 857 | ) |
| 858 | .map_err(Error::VhostNetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 859 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 860 | } else { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 861 | let dev = virtio::Net::<Tap>::new(features, host_ip, netmask, mac_address, vq_pairs) |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 862 | .map_err(Error::NetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 863 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 864 | }; |
| 865 | |
| 866 | let policy = if cfg.vhost_net { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 867 | "vhost_net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 868 | } else { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 869 | "net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | Ok(VirtioDeviceStub { |
| 873 | dev, |
| 874 | jail: simple_jail(&cfg, policy)?, |
| 875 | }) |
| 876 | } |
| 877 | |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 878 | fn create_vhost_user_net_device(cfg: &Config, opt: &VhostUserOption) -> DeviceResult { |
| 879 | let dev = VhostUserNet::new(virtio::base_features(cfg.protected_vm), &opt.socket) |
| 880 | .map_err(Error::VhostUserNetDeviceNew)?; |
| 881 | |
| 882 | Ok(VirtioDeviceStub { |
| 883 | dev: Box::new(dev), |
| 884 | // no sandbox here because virtqueue handling is exported to a different process. |
| 885 | jail: None, |
| 886 | }) |
| 887 | } |
| 888 | |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 889 | fn create_vhost_user_wl_device(cfg: &Config, opt: &VhostUserWlOption) -> DeviceResult { |
| 890 | // The crosvm wl device expects us to connect the tube before it will accept a vhost-user |
| 891 | // connection. |
| 892 | let dev = VhostUserWl::new(virtio::base_features(cfg.protected_vm), &opt.socket) |
| 893 | .map_err(Error::VhostUserWlDeviceNew)?; |
| 894 | |
| 895 | Ok(VirtioDeviceStub { |
| 896 | dev: Box::new(dev), |
| 897 | // no sandbox here because virtqueue handling is exported to a different process. |
| 898 | jail: None, |
| 899 | }) |
| 900 | } |
| 901 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 902 | #[cfg(feature = "gpu")] |
| 903 | fn create_gpu_device( |
| 904 | cfg: &Config, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 905 | exit_evt: &Event, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 906 | gpu_device_tube: Tube, |
| 907 | resource_bridges: Vec<Tube>, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 908 | wayland_socket_path: Option<&PathBuf>, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 909 | x_display: Option<String>, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 910 | event_devices: Vec<EventDevice>, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 911 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Gurchetan Singh | db17478 | 2019-10-01 15:16:15 -0700 | [diff] [blame] | 912 | mem: &GuestMemory, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 913 | ) -> DeviceResult { |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 914 | let mut display_backends = vec![ |
| 915 | virtio::DisplayBackend::X(x_display), |
Jason Macnak | 60eb1fb | 2020-01-09 14:36:29 -0800 | [diff] [blame] | 916 | virtio::DisplayBackend::Stub, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 917 | ]; |
| 918 | |
Gurchetan Singh | 1bbbf1c | 2021-05-19 15:05:56 -0700 | [diff] [blame] | 919 | let wayland_socket_dirs = cfg |
| 920 | .wayland_socket_paths |
| 921 | .iter() |
| 922 | .map(|(_name, path)| path.parent()) |
| 923 | .collect::<Option<Vec<_>>>() |
| 924 | .ok_or(Error::InvalidWaylandPath)?; |
| 925 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 926 | if let Some(socket_path) = wayland_socket_path { |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 927 | display_backends.insert( |
| 928 | 0, |
Gurchetan Singh | 1bbbf1c | 2021-05-19 15:05:56 -0700 | [diff] [blame] | 929 | virtio::DisplayBackend::Wayland(Some(socket_path.to_owned())), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 930 | ); |
| 931 | } |
| 932 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 933 | let dev = virtio::Gpu::new( |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 934 | exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 935 | Some(gpu_device_tube), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 936 | NonZeroU8::new(1).unwrap(), // number of scanouts |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 937 | resource_bridges, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 938 | display_backends, |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 939 | cfg.gpu_parameters.as_ref().unwrap(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 940 | event_devices, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 941 | map_request, |
| 942 | cfg.sandbox, |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 943 | virtio::base_features(cfg.protected_vm), |
Gurchetan Singh | 781d975 | 2021-02-15 17:45:22 -0800 | [diff] [blame] | 944 | cfg.wayland_socket_paths.clone(), |
Gurchetan Singh | db17478 | 2019-10-01 15:16:15 -0700 | [diff] [blame] | 945 | mem.clone(), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 946 | ); |
| 947 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 948 | let jail = match simple_jail(&cfg, "gpu_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 949 | Some(mut jail) => { |
| 950 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 951 | // dri directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 952 | jail.mount_with_data( |
| 953 | Path::new("none"), |
| 954 | Path::new("/"), |
| 955 | "tmpfs", |
| 956 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 957 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 958 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 959 | |
| 960 | // Device nodes required for DRM. |
| 961 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 962 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 963 | let sys_devices_path = Path::new("/sys/devices"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 964 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 965 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 966 | let drm_dri_path = Path::new("/dev/dri"); |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 967 | if drm_dri_path.exists() { |
| 968 | jail.mount_bind(drm_dri_path, drm_dri_path, false)?; |
| 969 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 970 | |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 971 | // Prepare GPU shader disk cache directory. |
| 972 | if let Some(cache_dir) = cfg |
| 973 | .gpu_parameters |
| 974 | .as_ref() |
| 975 | .and_then(|params| params.cache_path.as_ref()) |
| 976 | { |
| 977 | if cfg!(any(target_arch = "arm", target_arch = "aarch64")) && cfg.sandbox { |
| 978 | warn!("shader caching not yet supported on ARM with sandbox enabled"); |
| 979 | env::set_var("MESA_GLSL_CACHE_DISABLE", "true"); |
| 980 | } else { |
John Bates | 0405973 | 2020-10-01 15:58:55 -0700 | [diff] [blame] | 981 | env::set_var("MESA_GLSL_CACHE_DISABLE", "false"); |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 982 | env::set_var("MESA_GLSL_CACHE_DIR", cache_dir); |
| 983 | if let Some(cache_size) = cfg |
| 984 | .gpu_parameters |
| 985 | .as_ref() |
| 986 | .and_then(|params| params.cache_size.as_ref()) |
| 987 | { |
| 988 | env::set_var("MESA_GLSL_CACHE_MAX_SIZE", cache_size); |
| 989 | } |
| 990 | let shadercache_path = Path::new(cache_dir); |
| 991 | jail.mount_bind(shadercache_path, shadercache_path, true)?; |
| 992 | } |
| 993 | } |
| 994 | |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 995 | // If the ARM specific devices exist on the host, bind mount them in. |
| 996 | let mali0_path = Path::new("/dev/mali0"); |
| 997 | if mali0_path.exists() { |
| 998 | jail.mount_bind(mali0_path, mali0_path, true)?; |
| 999 | } |
| 1000 | |
| 1001 | let pvr_sync_path = Path::new("/dev/pvr_sync"); |
| 1002 | if pvr_sync_path.exists() { |
| 1003 | jail.mount_bind(pvr_sync_path, pvr_sync_path, true)?; |
| 1004 | } |
| 1005 | |
Gurchetan Singh | b66d6f6 | 2019-11-08 10:41:29 -0800 | [diff] [blame] | 1006 | // If the udmabuf driver exists on the host, bind mount it in. |
| 1007 | let udmabuf_path = Path::new("/dev/udmabuf"); |
| 1008 | if udmabuf_path.exists() { |
| 1009 | jail.mount_bind(udmabuf_path, udmabuf_path, true)?; |
| 1010 | } |
| 1011 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1012 | // Libraries that are required when mesa drivers are dynamically loaded. |
Chia-I Wu | d562b1a | 2020-12-27 21:08:27 -0800 | [diff] [blame] | 1013 | let lib_dirs = &[ |
| 1014 | "/usr/lib", |
| 1015 | "/usr/lib64", |
| 1016 | "/lib", |
| 1017 | "/lib64", |
| 1018 | "/usr/share/vulkan", |
| 1019 | ]; |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 1020 | for dir in lib_dirs { |
| 1021 | let dir_path = Path::new(dir); |
| 1022 | if dir_path.exists() { |
| 1023 | jail.mount_bind(dir_path, dir_path, false)?; |
| 1024 | } |
| 1025 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1026 | |
Gurchetan Singh | 1bbbf1c | 2021-05-19 15:05:56 -0700 | [diff] [blame] | 1027 | // Bind mount the wayland socket's directory into jail's root. This is necessary since |
| 1028 | // each new wayland context must open() the socket. If the wayland socket is ever |
| 1029 | // destroyed and remade in the same host directory, new connections will be possible |
| 1030 | // without restarting the wayland device. |
| 1031 | for dir in &wayland_socket_dirs { |
| 1032 | jail.mount_bind(dir, dir, true)?; |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1033 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1034 | |
| 1035 | add_crosvm_user_to_jail(&mut jail, "gpu")?; |
| 1036 | |
David Riley | 54e660b | 2019-07-24 17:22:50 -0700 | [diff] [blame] | 1037 | // pvr driver requires read access to /proc/self/task/*/comm. |
| 1038 | let proc_path = Path::new("/proc"); |
| 1039 | jail.mount( |
| 1040 | proc_path, |
| 1041 | proc_path, |
| 1042 | "proc", |
| 1043 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_RDONLY) as usize, |
| 1044 | )?; |
| 1045 | |
John Bates | 0d9d0e3 | 2020-12-03 11:37:33 -0800 | [diff] [blame] | 1046 | // To enable perfetto tracing, we need to give access to the perfetto service IPC |
| 1047 | // endpoints. |
| 1048 | let perfetto_path = Path::new("/run/perfetto"); |
| 1049 | if perfetto_path.exists() { |
| 1050 | jail.mount_bind(perfetto_path, perfetto_path, true)?; |
| 1051 | } |
| 1052 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1053 | Some(jail) |
| 1054 | } |
| 1055 | None => None, |
| 1056 | }; |
| 1057 | |
| 1058 | Ok(VirtioDeviceStub { |
| 1059 | dev: Box::new(dev), |
| 1060 | jail, |
| 1061 | }) |
| 1062 | } |
| 1063 | |
| 1064 | fn create_wayland_device( |
| 1065 | cfg: &Config, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1066 | control_tube: Tube, |
| 1067 | resource_bridge: Option<Tube>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1068 | ) -> DeviceResult { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1069 | let wayland_socket_dirs = cfg |
| 1070 | .wayland_socket_paths |
| 1071 | .iter() |
| 1072 | .map(|(_name, path)| path.parent()) |
| 1073 | .collect::<Option<Vec<_>>>() |
| 1074 | .ok_or(Error::InvalidWaylandPath)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1075 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1076 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 1077 | let dev = virtio::Wl::new( |
| 1078 | features, |
| 1079 | cfg.wayland_socket_paths.clone(), |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1080 | control_tube, |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 1081 | resource_bridge, |
| 1082 | ) |
| 1083 | .map_err(Error::WaylandDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1084 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1085 | let jail = match simple_jail(&cfg, "wl_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1086 | Some(mut jail) => { |
| 1087 | // Create a tmpfs in the device's root directory so that we can bind mount the wayland |
| 1088 | // socket directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 1089 | jail.mount_with_data( |
| 1090 | Path::new("none"), |
| 1091 | Path::new("/"), |
| 1092 | "tmpfs", |
| 1093 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 1094 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1095 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1096 | |
| 1097 | // Bind mount the wayland socket's directory into jail's root. This is necessary since |
| 1098 | // each new wayland context must open() the socket. If the wayland socket is ever |
| 1099 | // destroyed and remade in the same host directory, new connections will be possible |
| 1100 | // without restarting the wayland device. |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1101 | for dir in &wayland_socket_dirs { |
| 1102 | jail.mount_bind(dir, dir, true)?; |
| 1103 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1104 | add_crosvm_user_to_jail(&mut jail, "Wayland")?; |
| 1105 | |
| 1106 | Some(jail) |
| 1107 | } |
| 1108 | None => None, |
| 1109 | }; |
| 1110 | |
| 1111 | Ok(VirtioDeviceStub { |
| 1112 | dev: Box::new(dev), |
| 1113 | jail, |
| 1114 | }) |
| 1115 | } |
| 1116 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1117 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 1118 | fn create_video_device( |
| 1119 | cfg: &Config, |
| 1120 | typ: devices::virtio::VideoDeviceType, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1121 | resource_bridge: Tube, |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1122 | ) -> DeviceResult { |
| 1123 | let jail = match simple_jail(&cfg, "video_device")? { |
| 1124 | Some(mut jail) => { |
| 1125 | match typ { |
| 1126 | devices::virtio::VideoDeviceType::Decoder => { |
| 1127 | add_crosvm_user_to_jail(&mut jail, "video-decoder")? |
| 1128 | } |
| 1129 | devices::virtio::VideoDeviceType::Encoder => { |
| 1130 | add_crosvm_user_to_jail(&mut jail, "video-encoder")? |
| 1131 | } |
| 1132 | }; |
| 1133 | |
| 1134 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 1135 | jail.mount_with_data( |
| 1136 | Path::new("none"), |
| 1137 | Path::new("/"), |
| 1138 | "tmpfs", |
| 1139 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 1140 | "size=67108864", |
| 1141 | )?; |
| 1142 | |
| 1143 | // Render node for libvda. |
| 1144 | let dev_dri_path = Path::new("/dev/dri/renderD128"); |
| 1145 | jail.mount_bind(dev_dri_path, dev_dri_path, false)?; |
| 1146 | |
David Stevens | e341d0a | 2020-10-08 18:02:32 +0900 | [diff] [blame] | 1147 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1148 | { |
| 1149 | // Device nodes used by libdrm through minigbm in libvda on AMD devices. |
| 1150 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
| 1151 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
| 1152 | let sys_devices_path = Path::new("/sys/devices"); |
| 1153 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
| 1154 | |
| 1155 | // Required for loading dri libraries loaded by minigbm on AMD devices. |
| 1156 | let lib_dir = Path::new("/usr/lib64"); |
| 1157 | jail.mount_bind(lib_dir, lib_dir, false)?; |
| 1158 | } |
| 1159 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1160 | // Device nodes required by libchrome which establishes Mojo connection in libvda. |
| 1161 | let dev_urandom_path = Path::new("/dev/urandom"); |
| 1162 | jail.mount_bind(dev_urandom_path, dev_urandom_path, false)?; |
| 1163 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 1164 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 1165 | |
| 1166 | Some(jail) |
| 1167 | } |
| 1168 | None => None, |
| 1169 | }; |
| 1170 | |
| 1171 | Ok(VirtioDeviceStub { |
| 1172 | dev: Box::new(devices::virtio::VideoDevice::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1173 | virtio::base_features(cfg.protected_vm), |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1174 | typ, |
| 1175 | Some(resource_bridge), |
| 1176 | )), |
| 1177 | jail, |
| 1178 | }) |
| 1179 | } |
| 1180 | |
| 1181 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 1182 | fn register_video_device( |
| 1183 | devs: &mut Vec<VirtioDeviceStub>, |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 1184 | video_tube: Tube, |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1185 | cfg: &Config, |
| 1186 | typ: devices::virtio::VideoDeviceType, |
| 1187 | ) -> std::result::Result<(), Error> { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1188 | devs.push(create_video_device(cfg, typ, video_tube)?); |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1189 | Ok(()) |
| 1190 | } |
| 1191 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1192 | fn create_vhost_vsock_device(cfg: &Config, cid: u64, mem: &GuestMemory) -> DeviceResult { |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1193 | let features = virtio::base_features(cfg.protected_vm); |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 1194 | let dev = virtio::vhost::Vsock::new(&cfg.vhost_vsock_device_path, features, cid, mem) |
| 1195 | .map_err(Error::VhostVsockDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1196 | |
| 1197 | Ok(VirtioDeviceStub { |
| 1198 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1199 | jail: simple_jail(&cfg, "vhost_vsock_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1200 | }) |
| 1201 | } |
| 1202 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1203 | fn create_fs_device( |
| 1204 | cfg: &Config, |
| 1205 | uid_map: &str, |
| 1206 | gid_map: &str, |
| 1207 | src: &Path, |
| 1208 | tag: &str, |
| 1209 | fs_cfg: virtio::fs::passthrough::Config, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1210 | device_tube: Tube, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1211 | ) -> DeviceResult { |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1212 | let max_open_files = get_max_open_files()?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 1213 | let j = if cfg.sandbox { |
| 1214 | let seccomp_policy = cfg.seccomp_policy_dir.join("fs_device"); |
| 1215 | let config = SandboxConfig { |
| 1216 | limit_caps: false, |
| 1217 | uid_map: Some(uid_map), |
| 1218 | gid_map: Some(gid_map), |
| 1219 | log_failures: cfg.seccomp_log_failures, |
| 1220 | seccomp_policy: &seccomp_policy, |
| 1221 | }; |
Chirantan Ekbote | 34d45e5 | 2020-04-20 18:15:02 +0900 | [diff] [blame] | 1222 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1223 | // We want bind mounts from the parent namespaces to propagate into the fs device's |
| 1224 | // namespace. |
| 1225 | jail.set_remount_mode(libc::MS_SLAVE); |
| 1226 | |
| 1227 | jail |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 1228 | } else { |
| 1229 | create_base_minijail(src, Some(max_open_files), None)? |
| 1230 | }; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1231 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1232 | let features = virtio::base_features(cfg.protected_vm); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1233 | // TODO(chirantan): Use more than one worker once the kernel driver has been fixed to not panic |
| 1234 | // when num_queues > 1. |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1235 | let dev = |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1236 | virtio::fs::Fs::new(features, tag, 1, fs_cfg, device_tube).map_err(Error::FsDeviceNew)?; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1237 | |
| 1238 | Ok(VirtioDeviceStub { |
| 1239 | dev: Box::new(dev), |
| 1240 | jail: Some(j), |
| 1241 | }) |
| 1242 | } |
| 1243 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1244 | fn create_9p_device( |
| 1245 | cfg: &Config, |
| 1246 | uid_map: &str, |
| 1247 | gid_map: &str, |
| 1248 | src: &Path, |
| 1249 | tag: &str, |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1250 | mut p9_cfg: p9::Config, |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1251 | ) -> DeviceResult { |
| 1252 | let max_open_files = get_max_open_files()?; |
| 1253 | let (jail, root) = if cfg.sandbox { |
| 1254 | let seccomp_policy = cfg.seccomp_policy_dir.join("9p_device"); |
| 1255 | let config = SandboxConfig { |
| 1256 | limit_caps: false, |
| 1257 | uid_map: Some(uid_map), |
| 1258 | gid_map: Some(gid_map), |
| 1259 | log_failures: cfg.seccomp_log_failures, |
| 1260 | seccomp_policy: &seccomp_policy, |
| 1261 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1262 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1263 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1264 | // We want bind mounts from the parent namespaces to propagate into the 9p server's |
| 1265 | // namespace. |
| 1266 | jail.set_remount_mode(libc::MS_SLAVE); |
Chirantan Ekbote | 055de38 | 2020-01-24 12:16:58 +0900 | [diff] [blame] | 1267 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1268 | // The shared directory becomes the root of the device's file system. |
| 1269 | let root = Path::new("/"); |
| 1270 | (Some(jail), root) |
| 1271 | } else { |
| 1272 | // There's no mount namespace so we tell the server to treat the source directory as the |
| 1273 | // root. |
| 1274 | (None, src) |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1275 | }; |
| 1276 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1277 | let features = virtio::base_features(cfg.protected_vm); |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1278 | p9_cfg.root = root.into(); |
| 1279 | let dev = virtio::P9::new(features, tag, p9_cfg).map_err(Error::P9DeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1280 | |
| 1281 | Ok(VirtioDeviceStub { |
| 1282 | dev: Box::new(dev), |
| 1283 | jail, |
| 1284 | }) |
| 1285 | } |
| 1286 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1287 | fn create_pmem_device( |
| 1288 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1289 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1290 | resources: &mut SystemAllocator, |
| 1291 | disk: &DiskOption, |
| 1292 | index: usize, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1293 | pmem_device_tube: Tube, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1294 | ) -> DeviceResult { |
Mike Gerow | ec618a5 | 2021-02-26 20:57:14 +0000 | [diff] [blame] | 1295 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 1296 | let fd: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 1297 | // Safe because we will validate |raw_fd|. |
| 1298 | unsafe { File::from_raw_descriptor(raw_descriptor_from_path(&disk.path)?) } |
| 1299 | } else { |
| 1300 | OpenOptions::new() |
| 1301 | .read(true) |
| 1302 | .write(!disk.read_only) |
| 1303 | .open(&disk.path) |
| 1304 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))? |
| 1305 | }; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1306 | |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1307 | let arena_size = { |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 1308 | let metadata = |
| 1309 | std::fs::metadata(&disk.path).map_err(|e| Error::Disk(disk.path.to_path_buf(), e))?; |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1310 | let disk_len = metadata.len(); |
| 1311 | // Linux requires pmem region sizes to be 2 MiB aligned. Linux will fill any partial page |
| 1312 | // at the end of an mmap'd file and won't write back beyond the actual file length, but if |
| 1313 | // we just align the size of the file to 2 MiB then access beyond the last page of the |
| 1314 | // mapped file will generate SIGBUS. So use a memory mapping arena that will provide |
| 1315 | // padding up to 2 MiB. |
| 1316 | let alignment = 2 * 1024 * 1024; |
| 1317 | let align_adjust = if disk_len % alignment != 0 { |
| 1318 | alignment - (disk_len % alignment) |
| 1319 | } else { |
| 1320 | 0 |
| 1321 | }; |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1322 | disk_len |
| 1323 | .checked_add(align_adjust) |
| 1324 | .ok_or(Error::PmemDeviceImageTooBig)? |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1325 | }; |
| 1326 | |
| 1327 | let protection = { |
| 1328 | if disk.read_only { |
| 1329 | Protection::read() |
| 1330 | } else { |
| 1331 | Protection::read_write() |
| 1332 | } |
| 1333 | }; |
| 1334 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1335 | let arena = { |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1336 | // Conversion from u64 to usize may fail on 32bit system. |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1337 | let arena_size = usize::try_from(arena_size).map_err(|_| Error::PmemDeviceImageTooBig)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1338 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1339 | let mut arena = MemoryMappingArena::new(arena_size).map_err(Error::ReservePmemMemory)?; |
| 1340 | arena |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1341 | .add_fd_offset_protection(0, arena_size, &fd, 0, protection) |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1342 | .map_err(Error::ReservePmemMemory)?; |
| 1343 | arena |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1344 | }; |
| 1345 | |
| 1346 | let mapping_address = resources |
Xiong Zhang | 383b3b5 | 2019-10-30 14:59:26 +0800 | [diff] [blame] | 1347 | .mmio_allocator(MmioType::High) |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1348 | .allocate_with_align( |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1349 | arena_size, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1350 | Alloc::PmemDevice(index), |
| 1351 | format!("pmem_disk_image_{}", index), |
| 1352 | // Linux kernel requires pmem namespaces to be 128 MiB aligned. |
| 1353 | 128 * 1024 * 1024, /* 128 MiB */ |
| 1354 | ) |
| 1355 | .map_err(Error::AllocatePmemDeviceAddress)?; |
| 1356 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1357 | let slot = vm |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1358 | .add_memory_region( |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1359 | GuestAddress(mapping_address), |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1360 | Box::new(arena), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1361 | /* read_only = */ disk.read_only, |
| 1362 | /* log_dirty_pages = */ false, |
| 1363 | ) |
| 1364 | .map_err(Error::AddPmemDeviceMemory)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1365 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1366 | let dev = virtio::Pmem::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1367 | virtio::base_features(cfg.protected_vm), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1368 | fd, |
| 1369 | GuestAddress(mapping_address), |
| 1370 | slot, |
| 1371 | arena_size, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1372 | Some(pmem_device_tube), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1373 | ) |
| 1374 | .map_err(Error::PmemDeviceNew)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1375 | |
| 1376 | Ok(VirtioDeviceStub { |
| 1377 | dev: Box::new(dev) as Box<dyn VirtioDevice>, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1378 | jail: simple_jail(&cfg, "pmem_device")?, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1379 | }) |
| 1380 | } |
| 1381 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1382 | fn create_console_device(cfg: &Config, param: &SerialParameters) -> DeviceResult { |
Michael Hoyle | cd23bc2 | 2020-10-20 22:12:20 -0700 | [diff] [blame] | 1383 | let mut keep_rds = Vec::new(); |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1384 | let evt = Event::new().map_err(Error::CreateEvent)?; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1385 | let dev = param |
Michael Hoyle | cd23bc2 | 2020-10-20 22:12:20 -0700 | [diff] [blame] | 1386 | .create_serial_device::<Console>(cfg.protected_vm, &evt, &mut keep_rds) |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1387 | .map_err(Error::CreateConsole)?; |
| 1388 | |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1389 | let jail = match simple_jail(&cfg, "serial")? { |
| 1390 | Some(mut jail) => { |
| 1391 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 1392 | // log socket directory into it. |
| 1393 | // The size=67108864 is size=64*1024*1024 or size=64MB. |
| 1394 | jail.mount_with_data( |
| 1395 | Path::new("none"), |
| 1396 | Path::new("/"), |
| 1397 | "tmpfs", |
| 1398 | (libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_NOSUID) as usize, |
| 1399 | "size=67108864", |
| 1400 | )?; |
| 1401 | add_crosvm_user_to_jail(&mut jail, "serial")?; |
| 1402 | let res = param.add_bind_mounts(&mut jail); |
| 1403 | if res.is_err() { |
| 1404 | error!("failed to add bind mounts for console device"); |
| 1405 | } |
| 1406 | Some(jail) |
| 1407 | } |
| 1408 | None => None, |
| 1409 | }; |
| 1410 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1411 | Ok(VirtioDeviceStub { |
| 1412 | dev: Box::new(dev), |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1413 | jail, // TODO(dverkamp): use a separate policy for console? |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1414 | }) |
| 1415 | } |
| 1416 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1417 | // gpu_device_tube is not used when GPU support is disabled. |
Dmitry Torokhov | ee42b8c | 2019-05-27 11:14:20 -0700 | [diff] [blame] | 1418 | #[cfg_attr(not(feature = "gpu"), allow(unused_variables))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1419 | fn create_virtio_devices( |
| 1420 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1421 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1422 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1423 | _exit_evt: &Event, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1424 | wayland_device_tube: Tube, |
| 1425 | gpu_device_tube: Tube, |
| 1426 | balloon_device_tube: Tube, |
| 1427 | disk_device_tubes: &mut Vec<Tube>, |
| 1428 | pmem_device_tubes: &mut Vec<Tube>, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1429 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1430 | fs_device_tubes: &mut Vec<Tube>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1431 | ) -> DeviceResult<Vec<VirtioDeviceStub>> { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1432 | let mut devs = Vec::new(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1433 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1434 | for (_, param) in cfg |
| 1435 | .serial_parameters |
| 1436 | .iter() |
| 1437 | .filter(|(_k, v)| v.hardware == SerialHardware::VirtioConsole) |
| 1438 | { |
| 1439 | let dev = create_console_device(cfg, param)?; |
| 1440 | devs.push(dev); |
| 1441 | } |
| 1442 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1443 | for disk in &cfg.disks { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1444 | let disk_device_tube = disk_device_tubes.remove(0); |
| 1445 | devs.push(create_block_device(cfg, disk, disk_device_tube)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 1448 | for blk in &cfg.vhost_user_blk { |
| 1449 | devs.push(create_vhost_user_block_device(cfg, blk)?); |
| 1450 | } |
| 1451 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1452 | for (index, pmem_disk) in cfg.pmem_devices.iter().enumerate() { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1453 | let pmem_device_tube = pmem_device_tubes.remove(0); |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1454 | devs.push(create_pmem_device( |
| 1455 | cfg, |
| 1456 | vm, |
| 1457 | resources, |
| 1458 | pmem_disk, |
| 1459 | index, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1460 | pmem_device_tube, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1461 | )?); |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1464 | devs.push(create_rng_device(cfg)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1465 | |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1466 | #[cfg(feature = "tpm")] |
| 1467 | { |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1468 | if cfg.software_tpm { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1469 | devs.push(create_tpm_device(cfg)?); |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1470 | } |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1473 | for (idx, single_touch_spec) in cfg.virtio_single_touch.iter().enumerate() { |
| 1474 | devs.push(create_single_touch_device( |
| 1475 | cfg, |
| 1476 | single_touch_spec, |
| 1477 | idx as u32, |
| 1478 | )?); |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1481 | for (idx, multi_touch_spec) in cfg.virtio_multi_touch.iter().enumerate() { |
| 1482 | devs.push(create_multi_touch_device( |
| 1483 | cfg, |
| 1484 | multi_touch_spec, |
| 1485 | idx as u32, |
| 1486 | )?); |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1489 | for (idx, trackpad_spec) in cfg.virtio_trackpad.iter().enumerate() { |
| 1490 | devs.push(create_trackpad_device(cfg, trackpad_spec, idx as u32)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1493 | for (idx, mouse_socket) in cfg.virtio_mice.iter().enumerate() { |
| 1494 | devs.push(create_mouse_device(cfg, mouse_socket, idx as u32)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1497 | for (idx, keyboard_socket) in cfg.virtio_keyboard.iter().enumerate() { |
| 1498 | devs.push(create_keyboard_device(cfg, keyboard_socket, idx as u32)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1501 | for (idx, switches_socket) in cfg.virtio_switches.iter().enumerate() { |
| 1502 | devs.push(create_switches_device(cfg, switches_socket, idx as u32)?); |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1505 | for dev_path in &cfg.virtio_input_evdevs { |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1506 | devs.push(create_vinput_device(cfg, &dev_path)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1509 | devs.push(create_balloon_device(cfg, balloon_device_tube)?); |
Dylan Reid | 295ccac | 2017-11-06 14:06:24 -0800 | [diff] [blame] | 1510 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1511 | // We checked above that if the IP is defined, then the netmask is, too. |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1512 | for tap_fd in &cfg.tap_fd { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1513 | devs.push(create_tap_net_device(cfg, *tap_fd)?); |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1516 | if let (Some(host_ip), Some(netmask), Some(mac_address)) = |
| 1517 | (cfg.host_ip, cfg.netmask, cfg.mac_address) |
| 1518 | { |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 1519 | if !cfg.vhost_user_net.is_empty() { |
| 1520 | return Err(Error::VhostUserNetWithNetArgs); |
| 1521 | } |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1522 | devs.push(create_net_device( |
| 1523 | cfg, |
| 1524 | host_ip, |
| 1525 | netmask, |
| 1526 | mac_address, |
| 1527 | vm.get_memory(), |
| 1528 | )?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 1531 | for net in &cfg.vhost_user_net { |
| 1532 | devs.push(create_vhost_user_net_device(cfg, net)?); |
| 1533 | } |
| 1534 | |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 1535 | for opt in &cfg.vhost_user_wl { |
| 1536 | devs.push(create_vhost_user_wl_device(cfg, opt)?); |
| 1537 | } |
| 1538 | |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1539 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1540 | let mut resource_bridges = Vec::<Tube>::new(); |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1541 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1542 | if !cfg.wayland_socket_paths.is_empty() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1543 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1544 | let mut wl_resource_bridge = None::<Tube>; |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1545 | |
| 1546 | #[cfg(feature = "gpu")] |
| 1547 | { |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1548 | if cfg.gpu_parameters.is_some() { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1549 | let (wl_socket, gpu_socket) = Tube::pair().map_err(Error::CreateTube)?; |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1550 | resource_bridges.push(gpu_socket); |
| 1551 | wl_resource_bridge = Some(wl_socket); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | devs.push(create_wayland_device( |
| 1556 | cfg, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1557 | wayland_device_tube, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1558 | wl_resource_bridge, |
| 1559 | )?); |
| 1560 | } |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1561 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1562 | #[cfg(feature = "video-decoder")] |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 1563 | let video_dec_tube = if cfg.video_dec { |
| 1564 | let (video_tube, gpu_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 1565 | resource_bridges.push(gpu_tube); |
| 1566 | Some(video_tube) |
| 1567 | } else { |
| 1568 | None |
| 1569 | }; |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1570 | |
| 1571 | #[cfg(feature = "video-encoder")] |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 1572 | let video_enc_tube = if cfg.video_enc { |
| 1573 | let (video_tube, gpu_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 1574 | resource_bridges.push(gpu_tube); |
| 1575 | Some(video_tube) |
| 1576 | } else { |
| 1577 | None |
| 1578 | }; |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1579 | |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1580 | #[cfg(feature = "gpu")] |
| 1581 | { |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1582 | if let Some(gpu_parameters) = &cfg.gpu_parameters { |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1583 | let mut event_devices = Vec::new(); |
| 1584 | if cfg.display_window_mouse { |
| 1585 | let (event_device_socket, virtio_dev_socket) = |
| 1586 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1587 | let (multi_touch_width, multi_touch_height) = cfg |
| 1588 | .virtio_multi_touch |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1589 | .first() |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1590 | .as_ref() |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1591 | .map(|multi_touch_spec| multi_touch_spec.get_size()) |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1592 | .unwrap_or((gpu_parameters.display_width, gpu_parameters.display_height)); |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1593 | let dev = virtio::new_multi_touch( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1594 | // u32::MAX is the least likely to collide with the indices generated above for |
| 1595 | // the multi_touch options, which begin at 0. |
| 1596 | u32::MAX, |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1597 | virtio_dev_socket, |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1598 | multi_touch_width, |
| 1599 | multi_touch_height, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1600 | virtio::base_features(cfg.protected_vm), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1601 | ) |
| 1602 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1603 | devs.push(VirtioDeviceStub { |
| 1604 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1605 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1606 | }); |
| 1607 | event_devices.push(EventDevice::touchscreen(event_device_socket)); |
| 1608 | } |
| 1609 | if cfg.display_window_keyboard { |
| 1610 | let (event_device_socket, virtio_dev_socket) = |
| 1611 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1612 | let dev = virtio::new_keyboard( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 1613 | // u32::MAX is the least likely to collide with the indices generated above for |
| 1614 | // the multi_touch options, which begin at 0. |
| 1615 | u32::MAX, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1616 | virtio_dev_socket, |
| 1617 | virtio::base_features(cfg.protected_vm), |
| 1618 | ) |
| 1619 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1620 | devs.push(VirtioDeviceStub { |
| 1621 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1622 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1623 | }); |
| 1624 | event_devices.push(EventDevice::keyboard(event_device_socket)); |
| 1625 | } |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1626 | devs.push(create_gpu_device( |
| 1627 | cfg, |
| 1628 | _exit_evt, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1629 | gpu_device_tube, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1630 | resource_bridges, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1631 | // Use the unnamed socket for GPU display screens. |
| 1632 | cfg.wayland_socket_paths.get(""), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1633 | cfg.x_display.clone(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1634 | event_devices, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1635 | map_request, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1636 | vm.get_memory(), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1637 | )?); |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 1641 | #[cfg(feature = "video-decoder")] |
| 1642 | { |
| 1643 | if let Some(video_dec_tube) = video_dec_tube { |
| 1644 | register_video_device( |
| 1645 | &mut devs, |
| 1646 | video_dec_tube, |
| 1647 | cfg, |
| 1648 | devices::virtio::VideoDeviceType::Decoder, |
| 1649 | )?; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | #[cfg(feature = "video-encoder")] |
| 1654 | { |
| 1655 | if let Some(video_enc_tube) = video_enc_tube { |
| 1656 | register_video_device( |
| 1657 | &mut devs, |
| 1658 | video_enc_tube, |
| 1659 | cfg, |
| 1660 | devices::virtio::VideoDeviceType::Encoder, |
| 1661 | )?; |
| 1662 | } |
| 1663 | } |
| 1664 | |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1665 | if let Some(cid) = cfg.cid { |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1666 | devs.push(create_vhost_vsock_device(cfg, cid, vm.get_memory())?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 1669 | for vhost_user_fs in &cfg.vhost_user_fs { |
| 1670 | devs.push(create_vhost_user_fs_device(cfg, &vhost_user_fs)?); |
| 1671 | } |
| 1672 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1673 | for shared_dir in &cfg.shared_dirs { |
| 1674 | let SharedDir { |
| 1675 | src, |
| 1676 | tag, |
| 1677 | kind, |
| 1678 | uid_map, |
| 1679 | gid_map, |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1680 | fs_cfg, |
| 1681 | p9_cfg, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1682 | } = shared_dir; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1683 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1684 | let dev = match kind { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1685 | SharedDirKind::FS => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1686 | let device_tube = fs_device_tubes.remove(0); |
| 1687 | create_fs_device(cfg, uid_map, gid_map, src, tag, fs_cfg.clone(), device_tube)? |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1688 | } |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1689 | SharedDirKind::P9 => create_9p_device(cfg, uid_map, gid_map, src, tag, p9_cfg.clone())?, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1690 | }; |
| 1691 | devs.push(dev); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | Ok(devs) |
| 1695 | } |
| 1696 | |
| 1697 | fn create_devices( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1698 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1699 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1700 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1701 | exit_evt: &Event, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1702 | control_tubes: &mut Vec<TaggedControlTube>, |
| 1703 | wayland_device_tube: Tube, |
| 1704 | gpu_device_tube: Tube, |
| 1705 | balloon_device_tube: Tube, |
| 1706 | disk_device_tubes: &mut Vec<Tube>, |
| 1707 | pmem_device_tubes: &mut Vec<Tube>, |
| 1708 | fs_device_tubes: &mut Vec<Tube>, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1709 | #[cfg(feature = "usb")] usb_provider: HostBackendDeviceProvider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1710 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1711 | ) -> DeviceResult<Vec<(Box<dyn PciDevice>, Option<Minijail>)>> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1712 | let stubs = create_virtio_devices( |
| 1713 | &cfg, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1714 | vm, |
| 1715 | resources, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1716 | exit_evt, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1717 | wayland_device_tube, |
| 1718 | gpu_device_tube, |
| 1719 | balloon_device_tube, |
| 1720 | disk_device_tubes, |
| 1721 | pmem_device_tubes, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1722 | map_request, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1723 | fs_device_tubes, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1724 | )?; |
| 1725 | |
| 1726 | let mut pci_devices = Vec::new(); |
| 1727 | |
| 1728 | for stub in stubs { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1729 | let (msi_host_tube, msi_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 1730 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1731 | let dev = VirtioPciDevice::new(vm.get_memory().clone(), stub.dev, msi_device_tube) |
Daniel Verkamp | bb712d6 | 2019-11-19 09:47:33 -0800 | [diff] [blame] | 1732 | .map_err(Error::VirtioPciDev)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1733 | let dev = Box::new(dev) as Box<dyn PciDevice>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1734 | pci_devices.push((dev, stub.jail)); |
| 1735 | } |
| 1736 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1737 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1738 | for ac97_param in &cfg.ac97_parameters { |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1739 | let dev = Ac97Dev::try_new(vm.get_memory().clone(), ac97_param.clone()) |
| 1740 | .map_err(Error::CreateAc97)?; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 1741 | let jail = simple_jail(&cfg, dev.minijail_policy())?; |
| 1742 | pci_devices.push((Box::new(dev), jail)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1743 | } |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1744 | |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1745 | #[cfg(feature = "usb")] |
| 1746 | { |
| 1747 | // Create xhci controller. |
| 1748 | let usb_controller = Box::new(XhciController::new(vm.get_memory().clone(), usb_provider)); |
| 1749 | pci_devices.push((usb_controller, simple_jail(&cfg, "xhci")?)); |
| 1750 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1751 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1752 | if !cfg.vfio.is_empty() { |
Xiong Zhang | ea6cf66 | 2019-11-11 18:32:02 +0800 | [diff] [blame] | 1753 | let vfio_container = Arc::new(Mutex::new( |
| 1754 | VfioContainer::new().map_err(Error::CreateVfioDevice)?, |
| 1755 | )); |
| 1756 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1757 | for vfio_path in &cfg.vfio { |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1758 | // create MSI, MSI-X, and Mem request sockets for each vfio device |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1759 | let (vfio_host_tube_msi, vfio_device_tube_msi) = |
| 1760 | Tube::pair().map_err(Error::CreateTube)?; |
| 1761 | control_tubes.push(TaggedControlTube::VmIrq(vfio_host_tube_msi)); |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1762 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1763 | let (vfio_host_tube_msix, vfio_device_tube_msix) = |
| 1764 | Tube::pair().map_err(Error::CreateTube)?; |
| 1765 | control_tubes.push(TaggedControlTube::VmIrq(vfio_host_tube_msix)); |
Xiong Zhang | 4b5bb3a | 2019-04-23 17:15:21 +0800 | [diff] [blame] | 1766 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1767 | let (vfio_host_tube_mem, vfio_device_tube_mem) = |
| 1768 | Tube::pair().map_err(Error::CreateTube)?; |
| 1769 | control_tubes.push(TaggedControlTube::VmMemory(vfio_host_tube_mem)); |
Xiong Zhang | 85abeff | 2019-04-23 17:15:24 +0800 | [diff] [blame] | 1770 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1771 | let vfiodevice = VfioDevice::new( |
| 1772 | vfio_path.as_path(), |
| 1773 | vm, |
| 1774 | vm.get_memory(), |
| 1775 | vfio_container.clone(), |
| 1776 | ) |
| 1777 | .map_err(Error::CreateVfioDevice)?; |
Tomasz Jeznach | 502b5de | 2021-02-03 21:45:47 -0800 | [diff] [blame] | 1778 | let mut vfiopcidevice = Box::new(VfioPciDevice::new( |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1779 | vfiodevice, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1780 | vfio_device_tube_msi, |
| 1781 | vfio_device_tube_msix, |
| 1782 | vfio_device_tube_mem, |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1783 | )); |
Tomasz Jeznach | 502b5de | 2021-02-03 21:45:47 -0800 | [diff] [blame] | 1784 | // early reservation for pass-through PCI devices. |
| 1785 | if vfiopcidevice.allocate_address(resources).is_err() { |
| 1786 | warn!( |
| 1787 | "address reservation failed for vfio {}", |
| 1788 | vfiopcidevice.debug_label() |
| 1789 | ); |
| 1790 | } |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1791 | pci_devices.push((vfiopcidevice, simple_jail(&cfg, "vfio_device")?)); |
| 1792 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1793 | } |
| 1794 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1795 | Ok(pci_devices) |
| 1796 | } |
| 1797 | |
| 1798 | #[derive(Copy, Clone)] |
Chirantan Ekbote | 1a2683b | 2019-11-26 16:28:23 +0900 | [diff] [blame] | 1799 | #[cfg_attr(not(feature = "tpm"), allow(dead_code))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1800 | struct Ids { |
| 1801 | uid: uid_t, |
| 1802 | gid: gid_t, |
| 1803 | } |
| 1804 | |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1805 | // Set the uid/gid for the jailed process and give a basic id map. This is |
| 1806 | // required for bind mounts to work. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1807 | fn add_crosvm_user_to_jail(jail: &mut Minijail, feature: &str) -> Result<Ids> { |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1808 | let crosvm_user_group = CStr::from_bytes_with_nul(b"crosvm\0").unwrap(); |
| 1809 | |
| 1810 | let crosvm_uid = match get_user_id(&crosvm_user_group) { |
| 1811 | Ok(u) => u, |
| 1812 | Err(e) => { |
| 1813 | warn!("falling back to current user id for {}: {}", feature, e); |
| 1814 | geteuid() |
| 1815 | } |
| 1816 | }; |
| 1817 | |
| 1818 | let crosvm_gid = match get_group_id(&crosvm_user_group) { |
| 1819 | Ok(u) => u, |
| 1820 | Err(e) => { |
| 1821 | warn!("falling back to current group id for {}: {}", feature, e); |
| 1822 | getegid() |
| 1823 | } |
| 1824 | }; |
| 1825 | |
| 1826 | jail.change_uid(crosvm_uid); |
| 1827 | jail.change_gid(crosvm_gid); |
| 1828 | jail.uidmap(&format!("{0} {0} 1", crosvm_uid)) |
| 1829 | .map_err(Error::SettingUidMap)?; |
| 1830 | jail.gidmap(&format!("{0} {0} 1", crosvm_gid)) |
| 1831 | .map_err(Error::SettingGidMap)?; |
| 1832 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 1833 | Ok(Ids { |
| 1834 | uid: crosvm_uid, |
| 1835 | gid: crosvm_gid, |
| 1836 | }) |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1837 | } |
| 1838 | |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1839 | fn raw_descriptor_from_path(path: &Path) -> Result<RawDescriptor> { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1840 | if !path.is_file() { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1841 | return Err(Error::InvalidFdPath); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1842 | } |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1843 | let raw_descriptor = path |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1844 | .file_name() |
| 1845 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 1846 | .and_then(|fd_str| fd_str.parse::<c_int>().ok()) |
| 1847 | .ok_or(Error::InvalidFdPath)?; |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1848 | validate_raw_descriptor(raw_descriptor).map_err(Error::ValidateRawDescriptor) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1851 | trait IntoUnixStream { |
| 1852 | fn into_unix_stream(self) -> Result<UnixStream>; |
| 1853 | } |
| 1854 | |
| 1855 | impl<'a> IntoUnixStream for &'a Path { |
| 1856 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1857 | if self.parent() == Some(Path::new("/proc/self/fd")) { |
| 1858 | // Safe because we will validate |raw_fd|. |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1859 | unsafe { Ok(UnixStream::from_raw_fd(raw_descriptor_from_path(self)?)) } |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1860 | } else { |
| 1861 | UnixStream::connect(self).map_err(Error::InputEventsOpen) |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | impl<'a> IntoUnixStream for &'a PathBuf { |
| 1866 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1867 | self.as_path().into_unix_stream() |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | impl IntoUnixStream for UnixStream { |
| 1872 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1873 | Ok(self) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1877 | fn setup_vcpu_signal_handler<T: Vcpu>(use_hypervisor_signals: bool) -> Result<()> { |
| 1878 | if use_hypervisor_signals { |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1879 | unsafe { |
Allen Webb | 44c728c | 2021-03-23 15:22:41 -0500 | [diff] [blame] | 1880 | extern "C" fn handle_signal(_: c_int) {} |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1881 | // Our signal handler does nothing and is trivially async signal safe. |
| 1882 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal) |
| 1883 | .map_err(Error::RegisterSignalHandler)?; |
| 1884 | } |
| 1885 | block_signal(SIGRTMIN() + 0).map_err(Error::BlockSignal)?; |
| 1886 | } else { |
| 1887 | unsafe { |
Allen Webb | 44c728c | 2021-03-23 15:22:41 -0500 | [diff] [blame] | 1888 | extern "C" fn handle_signal<T: Vcpu>(_: c_int) { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1889 | T::set_local_immediate_exit(true); |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1890 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1891 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal::<T>) |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1892 | .map_err(Error::RegisterSignalHandler)?; |
| 1893 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1894 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1895 | Ok(()) |
| 1896 | } |
| 1897 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1898 | // Sets up a vcpu and converts it into a runnable vcpu. |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1899 | fn runnable_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1900 | cpu_id: usize, |
| 1901 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1902 | vm: impl VmArch, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1903 | irq_chip: &mut dyn IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1904 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1905 | run_rt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1906 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1907 | no_smt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1908 | has_bios: bool, |
| 1909 | use_hypervisor_signals: bool, |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1910 | ) -> Result<(V, VcpuRunHandle)> |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1911 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1912 | V: VcpuArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1913 | { |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1914 | let mut vcpu = match vcpu { |
| 1915 | Some(v) => v, |
| 1916 | None => { |
| 1917 | // If vcpu is None, it means this arch/hypervisor requires create_vcpu to be called from |
| 1918 | // the vcpu thread. |
| 1919 | match vm |
| 1920 | .create_vcpu(cpu_id) |
| 1921 | .map_err(Error::CreateVcpu)? |
| 1922 | .downcast::<V>() |
| 1923 | { |
| 1924 | Ok(v) => *v, |
| 1925 | Err(_) => panic!("VM created wrong type of VCPU"), |
| 1926 | } |
| 1927 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1928 | }; |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1929 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1930 | irq_chip |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1931 | .add_vcpu(cpu_id, &vcpu) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1932 | .map_err(Error::AddIrqChipVcpu)?; |
| 1933 | |
Daniel Verkamp | caf9ced | 2020-09-29 15:35:02 -0700 | [diff] [blame] | 1934 | if !vcpu_affinity.is_empty() { |
| 1935 | if let Err(e) = set_cpu_affinity(vcpu_affinity) { |
| 1936 | error!("Failed to set CPU affinity: {}", e); |
| 1937 | } |
| 1938 | } |
| 1939 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1940 | Arch::configure_vcpu( |
| 1941 | vm.get_memory(), |
| 1942 | vm.get_hypervisor(), |
| 1943 | irq_chip, |
| 1944 | &mut vcpu, |
| 1945 | cpu_id, |
| 1946 | vcpu_count, |
| 1947 | has_bios, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1948 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1949 | ) |
| 1950 | .map_err(Error::ConfigureVcpu)?; |
| 1951 | |
Zach Reizner | 026f72f | 2021-06-01 14:35:29 -0700 | [diff] [blame] | 1952 | if let Err(e) = enable_core_scheduling() { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1953 | error!("Failed to enable core scheduling: {}", e); |
| 1954 | } |
| 1955 | |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1956 | if run_rt { |
| 1957 | const DEFAULT_VCPU_RT_LEVEL: u16 = 6; |
| 1958 | if let Err(e) = set_rt_prio_limit(u64::from(DEFAULT_VCPU_RT_LEVEL)) |
| 1959 | .and_then(|_| set_rt_round_robin(i32::from(DEFAULT_VCPU_RT_LEVEL))) |
| 1960 | { |
| 1961 | warn!("Failed to set vcpu to real time: {}", e); |
| 1962 | } |
| 1963 | } |
| 1964 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1965 | if use_hypervisor_signals { |
| 1966 | let mut v = get_blocked_signals().map_err(Error::GetSignalMask)?; |
| 1967 | v.retain(|&x| x != SIGRTMIN() + 0); |
| 1968 | vcpu.set_signal_mask(&v).map_err(Error::SettingSignalMask)?; |
| 1969 | } |
| 1970 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1971 | let vcpu_run_handle = vcpu |
| 1972 | .take_run_handle(Some(SIGRTMIN() + 0)) |
| 1973 | .map_err(Error::RunnableVcpu)?; |
| 1974 | |
| 1975 | Ok((vcpu, vcpu_run_handle)) |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1976 | } |
| 1977 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1978 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1979 | fn handle_debug_msg<V>( |
| 1980 | cpu_id: usize, |
| 1981 | vcpu: &V, |
| 1982 | guest_mem: &GuestMemory, |
| 1983 | d: VcpuDebug, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1984 | reply_tube: &mpsc::Sender<VcpuDebugStatusMessage>, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1985 | ) -> Result<()> |
| 1986 | where |
| 1987 | V: VcpuArch + 'static, |
| 1988 | { |
| 1989 | match d { |
| 1990 | VcpuDebug::ReadRegs => { |
| 1991 | let msg = VcpuDebugStatusMessage { |
| 1992 | cpu: cpu_id as usize, |
| 1993 | msg: VcpuDebugStatus::RegValues( |
| 1994 | Arch::debug_read_registers(vcpu as &V).map_err(Error::HandleDebugCommand)?, |
| 1995 | ), |
| 1996 | }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1997 | reply_tube |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1998 | .send(msg) |
| 1999 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2000 | } |
| 2001 | VcpuDebug::WriteRegs(regs) => { |
| 2002 | Arch::debug_write_registers(vcpu as &V, ®s).map_err(Error::HandleDebugCommand)?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2003 | reply_tube |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2004 | .send(VcpuDebugStatusMessage { |
| 2005 | cpu: cpu_id as usize, |
| 2006 | msg: VcpuDebugStatus::CommandComplete, |
| 2007 | }) |
| 2008 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2009 | } |
| 2010 | VcpuDebug::ReadMem(vaddr, len) => { |
| 2011 | let msg = VcpuDebugStatusMessage { |
| 2012 | cpu: cpu_id as usize, |
| 2013 | msg: VcpuDebugStatus::MemoryRegion( |
| 2014 | Arch::debug_read_memory(vcpu as &V, guest_mem, vaddr, len) |
| 2015 | .unwrap_or(Vec::new()), |
| 2016 | ), |
| 2017 | }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2018 | reply_tube |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2019 | .send(msg) |
| 2020 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2021 | } |
| 2022 | VcpuDebug::WriteMem(vaddr, buf) => { |
| 2023 | Arch::debug_write_memory(vcpu as &V, guest_mem, vaddr, &buf) |
| 2024 | .map_err(Error::HandleDebugCommand)?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2025 | reply_tube |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2026 | .send(VcpuDebugStatusMessage { |
| 2027 | cpu: cpu_id as usize, |
| 2028 | msg: VcpuDebugStatus::CommandComplete, |
| 2029 | }) |
| 2030 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2031 | } |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2032 | VcpuDebug::EnableSinglestep => { |
| 2033 | Arch::debug_enable_singlestep(vcpu as &V).map_err(Error::HandleDebugCommand)?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2034 | reply_tube |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2035 | .send(VcpuDebugStatusMessage { |
| 2036 | cpu: cpu_id as usize, |
| 2037 | msg: VcpuDebugStatus::CommandComplete, |
| 2038 | }) |
| 2039 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2040 | } |
| 2041 | VcpuDebug::SetHwBreakPoint(addrs) => { |
| 2042 | Arch::debug_set_hw_breakpoints(vcpu as &V, &addrs) |
| 2043 | .map_err(Error::HandleDebugCommand)?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2044 | reply_tube |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2045 | .send(VcpuDebugStatusMessage { |
| 2046 | cpu: cpu_id as usize, |
| 2047 | msg: VcpuDebugStatus::CommandComplete, |
| 2048 | }) |
| 2049 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 2050 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2054 | fn run_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2055 | cpu_id: usize, |
| 2056 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2057 | vm: impl VmArch + 'static, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2058 | mut irq_chip: Box<dyn IrqChipArch + 'static>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2059 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2060 | run_rt: bool, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2061 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2062 | no_smt: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2063 | start_barrier: Arc<Barrier>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2064 | has_bios: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2065 | io_bus: devices::Bus, |
| 2066 | mmio_bus: devices::Bus, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2067 | exit_evt: Event, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2068 | requires_pvclock_ctrl: bool, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2069 | from_main_tube: mpsc::Receiver<VcpuControl>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2070 | use_hypervisor_signals: bool, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2071 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] to_gdb_tube: Option< |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2072 | mpsc::Sender<VcpuDebugStatusMessage>, |
| 2073 | >, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2074 | ) -> Result<JoinHandle<()>> |
| 2075 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2076 | V: VcpuArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2077 | { |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 2078 | thread::Builder::new() |
| 2079 | .name(format!("crosvm_vcpu{}", cpu_id)) |
| 2080 | .spawn(move || { |
Zach Reizner | 9588531 | 2020-01-29 18:06:01 -0800 | [diff] [blame] | 2081 | // The VCPU thread must trigger the `exit_evt` in all paths, and a `ScopedEvent`'s Drop |
| 2082 | // implementation accomplishes that. |
| 2083 | let _scoped_exit_evt = ScopedEvent::from(exit_evt); |
| 2084 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2085 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2086 | let guest_mem = vm.get_memory().clone(); |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2087 | let runnable_vcpu = runnable_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2088 | cpu_id, |
| 2089 | vcpu, |
| 2090 | vm, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2091 | irq_chip.as_mut(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2092 | vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2093 | run_rt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2094 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2095 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2096 | has_bios, |
| 2097 | use_hypervisor_signals, |
| 2098 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2099 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 2100 | start_barrier.wait(); |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 2101 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2102 | let (vcpu, vcpu_run_handle) = match runnable_vcpu { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2103 | Ok(v) => v, |
| 2104 | Err(e) => { |
| 2105 | error!("failed to start vcpu {}: {}", cpu_id, e); |
| 2106 | return; |
| 2107 | } |
| 2108 | }; |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 2109 | |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2110 | let mut run_mode = VmRunMode::Running; |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2111 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2112 | if to_gdb_tube.is_some() { |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2113 | // Wait until a GDB client attaches |
| 2114 | run_mode = VmRunMode::Breakpoint; |
| 2115 | } |
| 2116 | |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2117 | let mut interrupted_by_signal = false; |
| 2118 | |
| 2119 | 'vcpu_loop: loop { |
| 2120 | // Start by checking for messages to process and the run state of the CPU. |
| 2121 | // An extra check here for Running so there isn't a need to call recv unless a |
| 2122 | // message is likely to be ready because a signal was sent. |
| 2123 | if interrupted_by_signal || run_mode != VmRunMode::Running { |
| 2124 | 'state_loop: loop { |
| 2125 | // Tries to get a pending message without blocking first. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2126 | let msg = match from_main_tube.try_recv() { |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2127 | Ok(m) => m, |
| 2128 | Err(mpsc::TryRecvError::Empty) if run_mode == VmRunMode::Running => { |
| 2129 | // If the VM is running and no message is pending, the state won't |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2130 | // change. |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2131 | break 'state_loop; |
| 2132 | } |
| 2133 | Err(mpsc::TryRecvError::Empty) => { |
| 2134 | // If the VM is not running, wait until a message is ready. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2135 | match from_main_tube.recv() { |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2136 | Ok(m) => m, |
| 2137 | Err(mpsc::RecvError) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2138 | error!("Failed to read from main tube in vcpu"); |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2139 | break 'vcpu_loop; |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | Err(mpsc::TryRecvError::Disconnected) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2144 | error!("Failed to read from main tube in vcpu"); |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2145 | break 'vcpu_loop; |
| 2146 | } |
| 2147 | }; |
| 2148 | |
| 2149 | // Collect all pending messages. |
| 2150 | let mut messages = vec![msg]; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2151 | messages.append(&mut from_main_tube.try_iter().collect()); |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2152 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2153 | for msg in messages { |
| 2154 | match msg { |
| 2155 | VcpuControl::RunState(new_mode) => { |
| 2156 | run_mode = new_mode; |
| 2157 | match run_mode { |
| 2158 | VmRunMode::Running => break 'state_loop, |
| 2159 | VmRunMode::Suspending => { |
| 2160 | // On KVM implementations that use a paravirtualized |
| 2161 | // clock (e.g. x86), a flag must be set to indicate to |
| 2162 | // the guest kernel that a vCPU was suspended. The guest |
| 2163 | // kernel will use this flag to prevent the soft lockup |
| 2164 | // detection from triggering when this vCPU resumes, |
| 2165 | // which could happen days later in realtime. |
| 2166 | if requires_pvclock_ctrl { |
| 2167 | if let Err(e) = vcpu.pvclock_ctrl() { |
| 2168 | error!( |
| 2169 | "failed to tell hypervisor vcpu {} is suspending: {}", |
| 2170 | cpu_id, e |
| 2171 | ); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | VmRunMode::Breakpoint => {} |
| 2176 | VmRunMode::Exiting => break 'vcpu_loop, |
| 2177 | } |
| 2178 | } |
| 2179 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2180 | VcpuControl::Debug(d) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2181 | match &to_gdb_tube { |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2182 | Some(ref ch) => { |
| 2183 | if let Err(e) = handle_debug_msg( |
| 2184 | cpu_id, &vcpu, &guest_mem, d, &ch, |
| 2185 | ) { |
| 2186 | error!("Failed to handle gdb message: {}", e); |
| 2187 | } |
| 2188 | }, |
| 2189 | None => { |
| 2190 | error!("VcpuControl::Debug received while GDB feature is disabled: {:?}", d); |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | } |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | interrupted_by_signal = false; |
| 2200 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2201 | // Vcpus may have run a HLT instruction, which puts them into a state other than |
| 2202 | // VcpuRunState::Runnable. In that case, this call to wait_until_runnable blocks |
| 2203 | // until either the irqchip receives an interrupt for this vcpu, or until the main |
| 2204 | // thread kicks this vcpu as a result of some VmControl operation. In most IrqChip |
| 2205 | // implementations HLT instructions do not make it to crosvm, and thus this is a |
| 2206 | // no-op that always returns VcpuRunState::Runnable. |
| 2207 | match irq_chip.wait_until_runnable(&vcpu) { |
| 2208 | Ok(VcpuRunState::Runnable) => {} |
| 2209 | Ok(VcpuRunState::Interrupted) => interrupted_by_signal = true, |
| 2210 | Err(e) => error!( |
| 2211 | "error waiting for vcpu {} to become runnable: {}", |
| 2212 | cpu_id, e |
| 2213 | ), |
| 2214 | } |
| 2215 | |
| 2216 | if !interrupted_by_signal { |
| 2217 | match vcpu.run(&vcpu_run_handle) { |
| 2218 | Ok(VcpuExit::IoIn { port, mut size }) => { |
| 2219 | let mut data = [0; 8]; |
| 2220 | if size > data.len() { |
| 2221 | error!("unsupported IoIn size of {} bytes", size); |
| 2222 | size = data.len(); |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2223 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2224 | io_bus.read(port as u64, &mut data[..size]); |
| 2225 | if let Err(e) = vcpu.set_data(&data[..size]) { |
| 2226 | error!("failed to set return data for IoIn: {}", e); |
| 2227 | } |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2228 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2229 | Ok(VcpuExit::IoOut { |
| 2230 | port, |
| 2231 | mut size, |
| 2232 | data, |
| 2233 | }) => { |
| 2234 | if size > data.len() { |
| 2235 | error!("unsupported IoOut size of {} bytes", size); |
| 2236 | size = data.len(); |
| 2237 | } |
| 2238 | io_bus.write(port as u64, &data[..size]); |
| 2239 | } |
| 2240 | Ok(VcpuExit::MmioRead { address, size }) => { |
| 2241 | let mut data = [0; 8]; |
| 2242 | mmio_bus.read(address, &mut data[..size]); |
| 2243 | // Setting data for mmio can not fail. |
| 2244 | let _ = vcpu.set_data(&data[..size]); |
| 2245 | } |
| 2246 | Ok(VcpuExit::MmioWrite { |
| 2247 | address, |
| 2248 | size, |
| 2249 | data, |
| 2250 | }) => { |
| 2251 | mmio_bus.write(address, &data[..size]); |
| 2252 | } |
| 2253 | Ok(VcpuExit::IoapicEoi { vector }) => { |
| 2254 | if let Err(e) = irq_chip.broadcast_eoi(vector) { |
| 2255 | error!( |
| 2256 | "failed to broadcast eoi {} on vcpu {}: {}", |
| 2257 | vector, cpu_id, e |
| 2258 | ); |
| 2259 | } |
| 2260 | } |
| 2261 | Ok(VcpuExit::IrqWindowOpen) => {} |
| 2262 | Ok(VcpuExit::Hlt) => irq_chip.halted(cpu_id), |
| 2263 | Ok(VcpuExit::Shutdown) => break, |
| 2264 | Ok(VcpuExit::FailEntry { |
| 2265 | hardware_entry_failure_reason, |
| 2266 | }) => { |
| 2267 | error!("vcpu hw run failure: {:#x}", hardware_entry_failure_reason); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2268 | break; |
| 2269 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2270 | Ok(VcpuExit::SystemEvent(_, _)) => break, |
| 2271 | Ok(VcpuExit::Debug { .. }) => { |
| 2272 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2273 | { |
| 2274 | let msg = VcpuDebugStatusMessage { |
| 2275 | cpu: cpu_id as usize, |
| 2276 | msg: VcpuDebugStatus::HitBreakPoint, |
| 2277 | }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2278 | if let Some(ref ch) = to_gdb_tube { |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2279 | if let Err(e) = ch.send(msg) { |
| 2280 | error!("failed to notify breakpoint to GDB thread: {}", e); |
| 2281 | break; |
| 2282 | } |
| 2283 | } |
| 2284 | run_mode = VmRunMode::Breakpoint; |
| 2285 | } |
| 2286 | } |
| 2287 | Ok(r) => warn!("unexpected vcpu exit: {:?}", r), |
| 2288 | Err(e) => match e.errno() { |
| 2289 | libc::EINTR => interrupted_by_signal = true, |
| 2290 | libc::EAGAIN => {} |
| 2291 | _ => { |
| 2292 | error!("vcpu hit unknown error: {}", e); |
| 2293 | break; |
| 2294 | } |
| 2295 | }, |
| 2296 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | if interrupted_by_signal { |
| 2300 | if use_hypervisor_signals { |
| 2301 | // Try to clear the signal that we use to kick VCPU if it is pending before |
| 2302 | // attempting to handle pause requests. |
| 2303 | if let Err(e) = clear_signal(SIGRTMIN() + 0) { |
| 2304 | error!("failed to clear pending signal: {}", e); |
| 2305 | break; |
| 2306 | } |
| 2307 | } else { |
| 2308 | vcpu.set_immediate_exit(false); |
| 2309 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 2310 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2311 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2312 | if let Err(e) = irq_chip.inject_interrupts(&vcpu) { |
| 2313 | error!("failed to inject interrupts for vcpu {}: {}", cpu_id, e); |
| 2314 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2315 | } |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 2316 | }) |
| 2317 | .map_err(Error::SpawnVcpu) |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2320 | // Reads the contents of a file and converts the space-separated fields into a Vec of i64s. |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2321 | // Returns an error if any of the fields fail to parse. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2322 | fn file_fields_to_i64<P: AsRef<Path>>(path: P) -> io::Result<Vec<i64>> { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2323 | let mut file = File::open(path)?; |
| 2324 | |
| 2325 | let mut buf = [0u8; 32]; |
| 2326 | let count = file.read(&mut buf)?; |
| 2327 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2328 | let content = |
| 2329 | str::from_utf8(&buf[..count]).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 2330 | content |
| 2331 | .trim() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2332 | .split_whitespace() |
| 2333 | .map(|x| { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2334 | x.parse::<i64>() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2335 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 2336 | }) |
| 2337 | .collect() |
| 2338 | } |
| 2339 | |
| 2340 | // Reads the contents of a file and converts them into a u64, and if there |
| 2341 | // are multiple fields it only returns the first one. |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2342 | fn file_to_i64<P: AsRef<Path>>(path: P, nth: usize) -> io::Result<i64> { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2343 | file_fields_to_i64(path)? |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2344 | .into_iter() |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2345 | .nth(nth) |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2346 | .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty file")) |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2349 | fn setup_vm_components(cfg: &Config) -> Result<VmComponents> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 2350 | let initrd_image = if let Some(initrd_path) = &cfg.initrd_path { |
| 2351 | Some(File::open(initrd_path).map_err(|e| Error::OpenInitrd(initrd_path.clone(), e))?) |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 2352 | } else { |
| 2353 | None |
| 2354 | }; |
| 2355 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 2356 | let vm_image = match cfg.executable_path { |
| 2357 | Some(Executable::Kernel(ref kernel_path)) => VmImage::Kernel( |
| 2358 | File::open(kernel_path).map_err(|e| Error::OpenKernel(kernel_path.to_path_buf(), e))?, |
| 2359 | ), |
| 2360 | Some(Executable::Bios(ref bios_path)) => VmImage::Bios( |
| 2361 | File::open(bios_path).map_err(|e| Error::OpenBios(bios_path.to_path_buf(), e))?, |
| 2362 | ), |
| 2363 | _ => panic!("Did not receive a bios or kernel, should be impossible."), |
| 2364 | }; |
| 2365 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2366 | Ok(VmComponents { |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 2367 | memory_size: cfg |
| 2368 | .memory |
| 2369 | .unwrap_or(256) |
| 2370 | .checked_mul(1024 * 1024) |
| 2371 | .ok_or(Error::MemoryTooLarge)?, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2372 | vcpu_count: cfg.vcpu_count.unwrap_or(1), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2373 | vcpu_affinity: cfg.vcpu_affinity.clone(), |
Daniel Verkamp | 8a72afc | 2021-03-15 17:55:52 -0700 | [diff] [blame] | 2374 | cpu_clusters: cfg.cpu_clusters.clone(), |
| 2375 | cpu_capacity: cfg.cpu_capacity.clone(), |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2376 | no_smt: cfg.no_smt, |
Sergey Senozhatsky | 1e369c5 | 2021-04-13 20:23:51 +0900 | [diff] [blame] | 2377 | hugepages: cfg.hugepages, |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 2378 | vm_image, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 2379 | android_fstab: cfg |
| 2380 | .android_fstab |
| 2381 | .as_ref() |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 2382 | .map(|x| File::open(x).map_err(|e| Error::OpenAndroidFstab(x.to_path_buf(), e))) |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 2383 | .map_or(Ok(None), |v| v.map(Some))?, |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 2384 | pstore: cfg.pstore.clone(), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 2385 | initrd_image, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 2386 | extra_kernel_params: cfg.params.clone(), |
| 2387 | wayland_dmabuf: cfg.wayland_dmabuf, |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 2388 | acpi_sdts: cfg |
| 2389 | .acpi_tables |
| 2390 | .iter() |
| 2391 | .map(|path| SDT::from_file(path).map_err(|e| Error::OpenAcpiTable(path.clone(), e))) |
| 2392 | .collect::<Result<Vec<SDT>>>()?, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2393 | rt_cpus: cfg.rt_cpus.clone(), |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 2394 | protected_vm: cfg.protected_vm, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2395 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2396 | gdb: None, |
Tomasz Jeznach | ccb2694 | 2021-03-30 22:44:11 -0700 | [diff] [blame] | 2397 | dmi_path: cfg.dmi_path.clone(), |
Tomasz Jeznach | d93c29f | 2021-04-12 11:00:24 -0700 | [diff] [blame] | 2398 | no_legacy: cfg.no_legacy, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2399 | }) |
| 2400 | } |
| 2401 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2402 | pub fn run_config(cfg: Config) -> Result<()> { |
| 2403 | let components = setup_vm_components(&cfg)?; |
| 2404 | |
| 2405 | let guest_mem_layout = |
| 2406 | Arch::guest_memory_layout(&components).map_err(Error::GuestMemoryLayout)?; |
| 2407 | let guest_mem = GuestMemory::new(&guest_mem_layout).map_err(Error::CreateGuestMemory)?; |
| 2408 | let mut mem_policy = MemoryPolicy::empty(); |
| 2409 | if components.hugepages { |
| 2410 | mem_policy |= MemoryPolicy::USE_HUGEPAGES; |
| 2411 | } |
| 2412 | guest_mem.set_memory_policy(mem_policy); |
| 2413 | let kvm = Kvm::new_with_path(&cfg.kvm_device_path).map_err(Error::CreateKvm)?; |
| 2414 | let vm = KvmVm::new(&kvm, guest_mem).map_err(Error::CreateVm)?; |
| 2415 | let vm_clone = vm.try_clone().map_err(Error::CreateVm)?; |
| 2416 | |
| 2417 | enum KvmIrqChip { |
| 2418 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2419 | Split(KvmSplitIrqChip), |
| 2420 | Kernel(KvmKernelIrqChip), |
| 2421 | } |
| 2422 | |
| 2423 | impl KvmIrqChip { |
| 2424 | fn as_mut(&mut self) -> &mut dyn IrqChipArch { |
| 2425 | match self { |
| 2426 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2427 | KvmIrqChip::Split(i) => i, |
| 2428 | KvmIrqChip::Kernel(i) => i, |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | let ioapic_host_tube; |
| 2434 | let mut irq_chip = if cfg.split_irqchip { |
| 2435 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 2436 | unimplemented!("KVM split irqchip mode only supported on x86 processors"); |
| 2437 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2438 | { |
| 2439 | let (host_tube, ioapic_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2440 | ioapic_host_tube = Some(host_tube); |
| 2441 | KvmIrqChip::Split( |
| 2442 | KvmSplitIrqChip::new( |
| 2443 | vm_clone, |
| 2444 | components.vcpu_count, |
| 2445 | ioapic_device_tube, |
| 2446 | Some(120), |
| 2447 | ) |
| 2448 | .map_err(Error::CreateIrqChip)?, |
| 2449 | ) |
| 2450 | } |
| 2451 | } else { |
| 2452 | ioapic_host_tube = None; |
| 2453 | KvmIrqChip::Kernel( |
| 2454 | KvmKernelIrqChip::new(vm_clone, components.vcpu_count).map_err(Error::CreateIrqChip)?, |
| 2455 | ) |
| 2456 | }; |
| 2457 | |
| 2458 | run_vm::<KvmVcpu, KvmVm>(cfg, components, vm, irq_chip.as_mut(), ioapic_host_tube) |
| 2459 | } |
| 2460 | |
| 2461 | fn run_vm<Vcpu, V>( |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2462 | cfg: Config, |
| 2463 | #[allow(unused_mut)] mut components: VmComponents, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2464 | mut vm: V, |
| 2465 | irq_chip: &mut dyn IrqChipArch, |
| 2466 | ioapic_host_tube: Option<Tube>, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2467 | ) -> Result<()> |
| 2468 | where |
| 2469 | Vcpu: VcpuArch + 'static, |
| 2470 | V: VmArch + 'static, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2471 | { |
| 2472 | if cfg.sandbox { |
| 2473 | // Printing something to the syslog before entering minijail so that libc's syslogger has a |
| 2474 | // chance to open files necessary for its operation, like `/etc/localtime`. After jailing, |
| 2475 | // access to those files will not be possible. |
| 2476 | info!("crosvm entering multiprocess mode"); |
| 2477 | } |
| 2478 | |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 2479 | #[cfg(feature = "usb")] |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2480 | let (usb_control_tube, usb_provider) = |
| 2481 | HostBackendDeviceProvider::new().map_err(Error::CreateUsbProvider)?; |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 2482 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2483 | // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this |
| 2484 | // before any jailed devices have been spawned, so that we can catch any of them that fail very |
| 2485 | // quickly. |
| 2486 | let sigchld_fd = SignalFd::new(libc::SIGCHLD).map_err(Error::CreateSignalFd)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2487 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2488 | let control_server_socket = match &cfg.socket_path { |
| 2489 | Some(path) => Some(UnlinkUnixSeqpacketListener( |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2490 | UnixSeqpacketListener::bind(path).map_err(Error::CreateControlServer)?, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2491 | )), |
| 2492 | None => None, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2493 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2494 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2495 | let mut control_tubes = Vec::new(); |
| 2496 | |
| 2497 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2498 | if let Some(port) = cfg.gdb { |
| 2499 | // GDB needs a control socket to interrupt vcpus. |
| 2500 | let (gdb_host_tube, gdb_control_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2501 | control_tubes.push(TaggedControlTube::Vm(gdb_host_tube)); |
| 2502 | components.gdb = Some((port, gdb_control_tube)); |
| 2503 | } |
| 2504 | |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame^] | 2505 | for wl_cfg in &cfg.vhost_user_wl { |
| 2506 | let wayland_host_tube = UnixSeqpacket::connect(&wl_cfg.vm_tube) |
| 2507 | .map(Tube::new) |
| 2508 | .map_err(Error::ConnectTube)?; |
| 2509 | control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube)); |
| 2510 | } |
| 2511 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2512 | let (wayland_host_tube, wayland_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2513 | control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2514 | // Balloon gets a special socket so balloon requests can be forwarded from the main process. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2515 | let (balloon_host_tube, balloon_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2516 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2517 | // Create one control socket per disk. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2518 | let mut disk_device_tubes = Vec::new(); |
| 2519 | let mut disk_host_tubes = Vec::new(); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2520 | let disk_count = cfg.disks.len(); |
| 2521 | for _ in 0..disk_count { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2522 | let (disk_host_tub, disk_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2523 | disk_host_tubes.push(disk_host_tub); |
| 2524 | disk_device_tubes.push(disk_device_tube); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2525 | } |
| 2526 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2527 | let mut pmem_device_tubes = Vec::new(); |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2528 | let pmem_count = cfg.pmem_devices.len(); |
| 2529 | for _ in 0..pmem_count { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2530 | let (pmem_host_tube, pmem_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2531 | pmem_device_tubes.push(pmem_device_tube); |
| 2532 | control_tubes.push(TaggedControlTube::VmMsync(pmem_host_tube)); |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2535 | let (gpu_host_tube, gpu_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2536 | control_tubes.push(TaggedControlTube::VmMemory(gpu_host_tube)); |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 2537 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2538 | if let Some(ioapic_host_tube) = ioapic_host_tube { |
| 2539 | control_tubes.push(TaggedControlTube::VmIrq(ioapic_host_tube)); |
| 2540 | } |
Zhuocheng Ding | f2e90bf | 2019-12-02 15:50:20 +0800 | [diff] [blame] | 2541 | |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2542 | let battery = if cfg.battery_type.is_some() { |
Alex Lau | f408c73 | 2020-11-10 18:24:04 +0900 | [diff] [blame] | 2543 | let jail = match simple_jail(&cfg, "battery")? { |
| 2544 | #[cfg_attr(not(feature = "powerd-monitor-powerd"), allow(unused_mut))] |
| 2545 | Some(mut jail) => { |
| 2546 | // Setup a bind mount to the system D-Bus socket if the powerd monitor is used. |
| 2547 | #[cfg(feature = "power-monitor-powerd")] |
| 2548 | { |
| 2549 | add_crosvm_user_to_jail(&mut jail, "battery")?; |
| 2550 | |
| 2551 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 2552 | jail.mount_with_data( |
| 2553 | Path::new("none"), |
| 2554 | Path::new("/"), |
| 2555 | "tmpfs", |
| 2556 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 2557 | "size=67108864", |
| 2558 | )?; |
| 2559 | |
| 2560 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 2561 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 2562 | } |
| 2563 | Some(jail) |
| 2564 | } |
| 2565 | None => None, |
| 2566 | }; |
| 2567 | (&cfg.battery_type, jail) |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2568 | } else { |
| 2569 | (&cfg.battery_type, None) |
| 2570 | }; |
| 2571 | |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2572 | let gralloc = RutabagaGralloc::new().map_err(Error::CreateGrallocError)?; |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2573 | let map_request: Arc<Mutex<Option<ExternalMapping>>> = Arc::new(Mutex::new(None)); |
| 2574 | |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2575 | let fs_count = cfg |
| 2576 | .shared_dirs |
| 2577 | .iter() |
| 2578 | .filter(|sd| sd.kind == SharedDirKind::FS) |
| 2579 | .count(); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2580 | let mut fs_device_tubes = Vec::with_capacity(fs_count); |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2581 | for _ in 0..fs_count { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2582 | let (fs_host_tube, fs_device_tube) = Tube::pair().map_err(Error::CreateTube)?; |
| 2583 | control_tubes.push(TaggedControlTube::Fs(fs_host_tube)); |
| 2584 | fs_device_tubes.push(fs_device_tube); |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2585 | } |
| 2586 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2587 | let exit_evt = Event::new().map_err(Error::CreateEvent)?; |
| 2588 | let mut sys_allocator = Arch::create_system_allocator(vm.get_memory()); |
| 2589 | let pci_devices = create_devices( |
| 2590 | &cfg, |
| 2591 | &mut vm, |
| 2592 | &mut sys_allocator, |
| 2593 | &exit_evt, |
| 2594 | &mut control_tubes, |
| 2595 | wayland_device_tube, |
| 2596 | gpu_device_tube, |
| 2597 | balloon_device_tube, |
| 2598 | &mut disk_device_tubes, |
| 2599 | &mut pmem_device_tubes, |
| 2600 | &mut fs_device_tubes, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 2601 | #[cfg(feature = "usb")] |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2602 | usb_provider, |
| 2603 | Arc::clone(&map_request), |
| 2604 | )?; |
| 2605 | |
Kuo-Hsin Yang | 6139da6 | 2021-04-14 16:55:24 +0800 | [diff] [blame] | 2606 | #[cfg_attr(not(feature = "direct"), allow(unused_mut))] |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2607 | let mut linux = Arch::build_vm::<V, Vcpu>( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2608 | components, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2609 | &exit_evt, |
| 2610 | &mut sys_allocator, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2611 | &cfg.serial_parameters, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 2612 | simple_jail(&cfg, "serial")?, |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2613 | battery, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 2614 | vm, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2615 | pci_devices, |
| 2616 | irq_chip, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2617 | ) |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 2618 | .map_err(Error::BuildVm)?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 2619 | |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 2620 | #[cfg(feature = "direct")] |
| 2621 | if let Some(pmio) = &cfg.direct_pmio { |
| 2622 | let direct_io = |
| 2623 | Arc::new(devices::DirectIo::new(&pmio.path, false).map_err(Error::DirectIo)?); |
| 2624 | for range in pmio.ranges.iter() { |
| 2625 | linux |
| 2626 | .io_bus |
| 2627 | .insert_sync(direct_io.clone(), range.0, range.1) |
| 2628 | .unwrap(); |
| 2629 | } |
| 2630 | }; |
| 2631 | |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 2632 | #[cfg(feature = "direct")] |
| 2633 | let mut irqs = Vec::new(); |
| 2634 | |
| 2635 | #[cfg(feature = "direct")] |
| 2636 | for irq in &cfg.direct_level_irq { |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2637 | if !sys_allocator.reserve_irq(*irq) { |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 2638 | warn!("irq {} already reserved.", irq); |
| 2639 | } |
| 2640 | let trigger = Event::new().map_err(Error::CreateEvent)?; |
| 2641 | let resample = Event::new().map_err(Error::CreateEvent)?; |
| 2642 | linux |
| 2643 | .irq_chip |
| 2644 | .register_irq_event(*irq, &trigger, Some(&resample)) |
| 2645 | .unwrap(); |
| 2646 | let direct_irq = |
| 2647 | devices::DirectIrq::new(trigger, Some(resample)).map_err(Error::DirectIrq)?; |
| 2648 | direct_irq.irq_enable(*irq).map_err(Error::DirectIrq)?; |
| 2649 | irqs.push(direct_irq); |
| 2650 | } |
| 2651 | |
| 2652 | #[cfg(feature = "direct")] |
| 2653 | for irq in &cfg.direct_edge_irq { |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2654 | if !sys_allocator.reserve_irq(*irq) { |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 2655 | warn!("irq {} already reserved.", irq); |
| 2656 | } |
| 2657 | let trigger = Event::new().map_err(Error::CreateEvent)?; |
| 2658 | linux |
| 2659 | .irq_chip |
| 2660 | .register_irq_event(*irq, &trigger, None) |
| 2661 | .unwrap(); |
| 2662 | let direct_irq = devices::DirectIrq::new(trigger, None).map_err(Error::DirectIrq)?; |
| 2663 | direct_irq.irq_enable(*irq).map_err(Error::DirectIrq)?; |
| 2664 | irqs.push(direct_irq); |
| 2665 | } |
| 2666 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2667 | run_control( |
| 2668 | linux, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2669 | sys_allocator, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2670 | control_server_socket, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2671 | control_tubes, |
| 2672 | balloon_host_tube, |
| 2673 | &disk_host_tubes, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 2674 | #[cfg(feature = "usb")] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2675 | usb_control_tube, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2676 | exit_evt, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2677 | sigchld_fd, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2678 | cfg.sandbox, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2679 | Arc::clone(&map_request), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2680 | cfg.balloon_bias, |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2681 | gralloc, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2682 | ) |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 2685 | /// Signals all running VCPUs to vmexit, sends VcpuControl message to each VCPU tube, and tells |
| 2686 | /// `irq_chip` to stop blocking halted VCPUs. The channel message is set first because both the |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2687 | /// signal and the irq_chip kick could cause the VCPU thread to continue through the VCPU run |
| 2688 | /// loop. |
| 2689 | fn kick_all_vcpus( |
| 2690 | vcpu_handles: &[(JoinHandle<()>, mpsc::Sender<vm_control::VcpuControl>)], |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2691 | irq_chip: &dyn IrqChip, |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 2692 | message: VcpuControl, |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2693 | ) { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2694 | for (handle, tube) in vcpu_handles { |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 2695 | if let Err(e) = tube.send(message.clone()) { |
| 2696 | error!("failed to send VcpuControl: {}", e); |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2697 | } |
| 2698 | let _ = handle.kill(SIGRTMIN() + 0); |
| 2699 | } |
| 2700 | irq_chip.kick_halted_vcpus(); |
| 2701 | } |
| 2702 | |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2703 | // BalloonPolicy determines the size to set the balloon. |
| 2704 | struct BalloonPolicy { |
| 2705 | // Estimate for when the guest starts aggressivly freeing memory. |
| 2706 | critical_guest_available: i64, |
| 2707 | critical_host_available: i64, // ChromeOS critical margin. |
| 2708 | guest_available_bias: i64, |
| 2709 | max_balloon_actual: i64, // The largest the balloon has ever been observed. |
| 2710 | prev_balloon_full_percent: i64, // How full was the balloon at the previous timestep. |
| 2711 | prev_guest_available: i64, // Available memory in the guest at the previous timestep. |
| 2712 | } |
| 2713 | |
| 2714 | const ONE_KB: i64 = 1024; |
| 2715 | const ONE_MB: i64 = 1024 * ONE_KB; |
| 2716 | |
| 2717 | const LOWMEM_AVAILABLE: &str = "/sys/kernel/mm/chromeos-low_mem/available"; |
| 2718 | const LOWMEM_MARGIN: &str = "/sys/kernel/mm/chromeos-low_mem/margin"; |
| 2719 | |
| 2720 | // BalloonPolicy implements the virtio balloon sizing logic. |
| 2721 | // The balloon is sized with the following heuristics: |
| 2722 | // Balance Available |
| 2723 | // The balloon is sized to balance the amount of available memory above a |
| 2724 | // critical margin. The critical margin is the level at which memory is |
| 2725 | // freed. In the host, this is the ChromeOS available critical margin, which |
| 2726 | // is the trigger to kill tabs. In the guest, we estimate this level by |
| 2727 | // tracking the minimum amount of available memory, discounting sharp |
| 2728 | // 'valleys'. If the guest manages to keep available memory above a given |
| 2729 | // level even with some pressure, then we determine that this is the |
| 2730 | // 'critical' level for the guest. We don't update this critical value if |
| 2731 | // the balloon is fully inflated because in that case, the guest may be out |
| 2732 | // of memory to free. |
| 2733 | // guest_available_bias |
| 2734 | // Even if available memory is perfectly balanced between host and guest, |
| 2735 | // The size of the balloon will still drift randomly depending on whether |
| 2736 | // those host or guest reclaims memory first/faster every time memory is |
| 2737 | // low. To encourage large balloons to shrink and small balloons to grow, |
| 2738 | // the following bias is added to the guest critical margin: |
| 2739 | // (guest_available_bias * balloon_full_percent) / 100 |
| 2740 | // This give the guest more memory when the balloon is full. |
| 2741 | impl BalloonPolicy { |
| 2742 | fn new( |
| 2743 | memory_size: i64, |
| 2744 | critical_host_available: i64, |
| 2745 | guest_available_bias: i64, |
| 2746 | ) -> BalloonPolicy { |
| 2747 | // Estimate some reasonable initial maximum for balloon size. |
| 2748 | let max_balloon_actual = (memory_size * 3) / 4; |
| 2749 | // 400MB is above the zone min margin even for Crostini VMs on 16GB |
| 2750 | // devices (~85MB), and is above when Android Low Memory Killer kills |
| 2751 | // apps (~250MB). |
| 2752 | let critical_guest_available = 400 * ONE_MB; |
| 2753 | |
| 2754 | BalloonPolicy { |
| 2755 | critical_guest_available, |
| 2756 | critical_host_available, |
| 2757 | guest_available_bias, |
| 2758 | max_balloon_actual, |
| 2759 | prev_balloon_full_percent: 0, |
| 2760 | prev_guest_available: 0, |
| 2761 | } |
| 2762 | } |
| 2763 | fn delta(&mut self, stats: BalloonStats, balloon_actual_u: u64) -> Result<i64> { |
| 2764 | let guest_free = stats |
| 2765 | .free_memory |
| 2766 | .map(i64::try_from) |
| 2767 | .ok_or(Error::GuestFreeMissing())? |
| 2768 | .map_err(Error::GuestFreeTooLarge)?; |
| 2769 | let guest_cached = stats |
| 2770 | .disk_caches |
| 2771 | .map(i64::try_from) |
| 2772 | .ok_or(Error::GuestFreeMissing())? |
| 2773 | .map_err(Error::GuestFreeTooLarge)?; |
| 2774 | let balloon_actual = match balloon_actual_u { |
| 2775 | size if size < i64::max_value() as u64 => size as i64, |
| 2776 | _ => return Err(Error::BalloonActualTooLarge), |
| 2777 | }; |
| 2778 | let guest_available = guest_free + guest_cached; |
| 2779 | // Available memory is reported in MB, and we need bytes. |
| 2780 | let host_available = |
| 2781 | file_to_i64(LOWMEM_AVAILABLE, 0).map_err(Error::ReadMemAvailable)? * ONE_MB; |
| 2782 | if self.max_balloon_actual < balloon_actual { |
| 2783 | self.max_balloon_actual = balloon_actual; |
| 2784 | info!( |
| 2785 | "balloon updated max_balloon_actual to {} MiB", |
| 2786 | self.max_balloon_actual / ONE_MB, |
| 2787 | ); |
| 2788 | } |
| 2789 | let balloon_full_percent = balloon_actual * 100 / self.max_balloon_actual; |
| 2790 | // Update critical_guest_available if we see a lower available with the |
| 2791 | // balloon not fully inflated. If the balloon is completely inflated |
| 2792 | // there is a risk that the low available level we see comes at the cost |
| 2793 | // of stability. The Linux OOM Killer might have been forced to kill |
| 2794 | // something important, or page reclaim was so aggressive that there are |
| 2795 | // long UI hangs. |
| 2796 | if guest_available < self.critical_guest_available && balloon_full_percent < 95 { |
| 2797 | // To ignore temporary low memory states, we require that two guest |
| 2798 | // available measurements in a row are low. |
| 2799 | if self.prev_guest_available < self.critical_guest_available |
| 2800 | && self.prev_balloon_full_percent < 95 |
| 2801 | { |
| 2802 | self.critical_guest_available = self.prev_guest_available; |
| 2803 | info!( |
| 2804 | "balloon updated critical_guest_available to {} MiB", |
| 2805 | self.critical_guest_available / ONE_MB, |
| 2806 | ); |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | // Compute the difference in available memory above the host and guest |
| 2811 | // critical thresholds. |
| 2812 | let bias = (self.guest_available_bias * balloon_full_percent) / 100; |
| 2813 | let guest_above_critical = guest_available - self.critical_guest_available - bias; |
| 2814 | let host_above_critical = host_available - self.critical_host_available; |
| 2815 | let balloon_delta = guest_above_critical - host_above_critical; |
| 2816 | // Only let the balloon take up MAX_CRITICAL_DELTA of available memory |
| 2817 | // below the critical level in host or guest. |
| 2818 | const MAX_CRITICAL_DELTA: i64 = 10 * ONE_MB; |
| 2819 | let balloon_delta_capped = if balloon_delta < 0 { |
| 2820 | // The balloon is deflating, taking memory from the host. Don't let |
| 2821 | // it take more than the amount of available memory above the |
| 2822 | // critical margin, plus MAX_CRITICAL_DELTA. |
| 2823 | max( |
| 2824 | balloon_delta, |
| 2825 | -(host_available - self.critical_host_available + MAX_CRITICAL_DELTA), |
| 2826 | ) |
| 2827 | } else { |
| 2828 | // The balloon is inflating, taking memory from the guest. Don't let |
| 2829 | // it take more than the amount of available memory above the |
| 2830 | // critical margin, plus MAX_CRITICAL_DELTA. |
| 2831 | min( |
| 2832 | balloon_delta, |
| 2833 | guest_available - self.critical_guest_available + MAX_CRITICAL_DELTA, |
| 2834 | ) |
| 2835 | }; |
| 2836 | |
| 2837 | self.prev_balloon_full_percent = balloon_full_percent; |
| 2838 | self.prev_guest_available = guest_available; |
| 2839 | |
| 2840 | // Only return a value if target would change available above critical |
| 2841 | // by more than 1%, or we are within 1 MB of critical in host or guest. |
| 2842 | if guest_above_critical < ONE_MB |
| 2843 | || host_above_critical < ONE_MB |
| 2844 | || (balloon_delta.abs() * 100) / guest_above_critical > 1 |
| 2845 | || (balloon_delta.abs() * 100) / host_above_critical > 1 |
| 2846 | { |
| 2847 | // Finally, make sure the balloon delta won't cause a negative size. |
| 2848 | let result = max(balloon_delta_capped, -balloon_actual); |
| 2849 | if result != 0 { |
| 2850 | info!( |
| 2851 | "balloon delta={:<6} ha={:<6} hc={:<6} ga={:<6} gc={:<6} bias={:<6} full={:>3}%", |
| 2852 | result / ONE_MB, |
| 2853 | host_available / ONE_MB, |
| 2854 | self.critical_host_available / ONE_MB, |
| 2855 | guest_available / ONE_MB, |
| 2856 | self.critical_guest_available / ONE_MB, |
| 2857 | bias / ONE_MB, |
| 2858 | balloon_full_percent, |
| 2859 | ); |
| 2860 | } |
| 2861 | return Ok(result); |
| 2862 | } |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 2863 | Ok(0) |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2867 | fn run_control<V: VmArch + 'static, Vcpu: VcpuArch + 'static>( |
| 2868 | mut linux: RunnableLinuxVm<V, Vcpu>, |
| 2869 | mut sys_allocator: SystemAllocator, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2870 | control_server_socket: Option<UnlinkUnixSeqpacketListener>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2871 | mut control_tubes: Vec<TaggedControlTube>, |
| 2872 | balloon_host_tube: Tube, |
| 2873 | disk_host_tubes: &[Tube], |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 2874 | #[cfg(feature = "usb")] usb_control_tube: Tube, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2875 | exit_evt: Event, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2876 | sigchld_fd: SignalFd, |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2877 | sandbox: bool, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2878 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2879 | balloon_bias: i64, |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2880 | mut gralloc: RutabagaGralloc, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2881 | ) -> Result<()> { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2882 | #[derive(PollToken)] |
| 2883 | enum Token { |
| 2884 | Exit, |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2885 | Suspend, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2886 | ChildSignal, |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2887 | IrqFd { index: IrqEventIndex }, |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2888 | BalanceMemory, |
| 2889 | BalloonResult, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2890 | VmControlServer, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2891 | VmControl { index: usize }, |
| 2892 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2893 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 2894 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2895 | .set_raw_mode() |
| 2896 | .expect("failed to set terminal raw mode"); |
| 2897 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2898 | let wait_ctx = WaitContext::build_with(&[ |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2899 | (&exit_evt, Token::Exit), |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2900 | (&linux.suspend_evt, Token::Suspend), |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2901 | (&sigchld_fd, Token::ChildSignal), |
| 2902 | ]) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2903 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2904 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2905 | if let Some(socket_server) = &control_server_socket { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2906 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2907 | .add(socket_server, Token::VmControlServer) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2908 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2909 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2910 | for (index, socket) in control_tubes.iter().enumerate() { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2911 | wait_ctx |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2912 | .add(socket.as_ref(), Token::VmControl { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2913 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2916 | let events = linux |
| 2917 | .irq_chip |
| 2918 | .irq_event_tokens() |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2919 | .map_err(Error::WaitContextAdd)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2920 | |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2921 | for (index, _gsi, evt) in events { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2922 | wait_ctx |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2923 | .add(&evt, Token::IrqFd { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2924 | .map_err(Error::WaitContextAdd)?; |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2925 | } |
| 2926 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2927 | // Balance available memory between guest and host every second. |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2928 | let mut balancemem_timer = Timer::new().map_err(Error::CreateTimer)?; |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2929 | let mut balloon_policy = if let Ok(critical_margin) = file_to_i64(LOWMEM_MARGIN, 0) { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2930 | // Create timer request balloon stats every 1s. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2931 | wait_ctx |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2932 | .add(&balancemem_timer, Token::BalanceMemory) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2933 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2934 | let balancemem_dur = Duration::from_secs(1); |
| 2935 | let balancemem_int = Duration::from_secs(1); |
| 2936 | balancemem_timer |
| 2937 | .reset(balancemem_dur, Some(balancemem_int)) |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2938 | .map_err(Error::ResetTimer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2939 | |
| 2940 | // Listen for balloon statistics from the guest so we can balance. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2941 | wait_ctx |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2942 | .add(&balloon_host_tube, Token::BalloonResult) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2943 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2944 | Some(BalloonPolicy::new( |
| 2945 | linux.vm.get_memory().memory_size() as i64, |
| 2946 | critical_margin * ONE_MB, |
| 2947 | balloon_bias, |
| 2948 | )) |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2949 | } else { |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2950 | warn!("Unable to open low mem margin, maybe not a chrome os kernel"); |
| 2951 | None |
| 2952 | }; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2953 | |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2954 | if sandbox { |
| 2955 | // Before starting VCPUs, in case we started with some capabilities, drop them all. |
| 2956 | drop_capabilities().map_err(Error::DropCapabilities)?; |
| 2957 | } |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 2958 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2959 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2960 | // Create a channel for GDB thread. |
| 2961 | let (to_gdb_channel, from_vcpu_channel) = if linux.gdb.is_some() { |
| 2962 | let (s, r) = mpsc::channel(); |
| 2963 | (Some(s), Some(r)) |
| 2964 | } else { |
| 2965 | (None, None) |
| 2966 | }; |
| 2967 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2968 | let mut vcpu_handles = Vec::with_capacity(linux.vcpu_count); |
| 2969 | let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpu_count + 1)); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2970 | let use_hypervisor_signals = !linux |
| 2971 | .vm |
| 2972 | .get_hypervisor() |
| 2973 | .check_capability(&HypervisorCap::ImmediateExit); |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2974 | setup_vcpu_signal_handler::<Vcpu>(use_hypervisor_signals)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2975 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2976 | let vcpus: Vec<Option<_>> = match linux.vcpus.take() { |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 2977 | Some(vec) => vec.into_iter().map(Some).collect(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2978 | None => iter::repeat_with(|| None).take(linux.vcpu_count).collect(), |
| 2979 | }; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 2980 | for (cpu_id, vcpu) in vcpus.into_iter().enumerate() { |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2981 | let (to_vcpu_channel, from_main_channel) = mpsc::channel(); |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2982 | let vcpu_affinity = match linux.vcpu_affinity.clone() { |
| 2983 | Some(VcpuAffinity::Global(v)) => v, |
| 2984 | Some(VcpuAffinity::PerVcpu(mut m)) => m.remove(&cpu_id).unwrap_or_default(), |
| 2985 | None => Default::default(), |
| 2986 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2987 | let handle = run_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2988 | cpu_id, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2989 | vcpu, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2990 | linux.vm.try_clone().map_err(Error::CloneEvent)?, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2991 | linux.irq_chip.try_box_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2992 | linux.vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2993 | linux.rt_cpus.contains(&cpu_id), |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2994 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2995 | linux.no_smt, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2996 | vcpu_thread_barrier.clone(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2997 | linux.has_bios, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2998 | linux.io_bus.clone(), |
| 2999 | linux.mmio_bus.clone(), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3000 | exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3001 | linux.vm.check_capability(VmCap::PvClockSuspend), |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 3002 | from_main_channel, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3003 | use_hypervisor_signals, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3004 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 3005 | to_gdb_channel.clone(), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 3006 | )?; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 3007 | vcpu_handles.push((handle, to_vcpu_channel)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 3008 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3009 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3010 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 3011 | // Spawn GDB thread. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3012 | if let Some((gdb_port_num, gdb_control_tube)) = linux.gdb.take() { |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3013 | let to_vcpu_channels = vcpu_handles |
| 3014 | .iter() |
| 3015 | .map(|(_handle, channel)| channel.clone()) |
| 3016 | .collect(); |
| 3017 | let target = GdbStub::new( |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3018 | gdb_control_tube, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3019 | to_vcpu_channels, |
| 3020 | from_vcpu_channel.unwrap(), // Must succeed to unwrap() |
| 3021 | ); |
| 3022 | thread::Builder::new() |
| 3023 | .name("gdb".to_owned()) |
| 3024 | .spawn(move || gdb_thread(target, gdb_port_num)) |
| 3025 | .map_err(Error::SpawnGdbServer)?; |
| 3026 | }; |
| 3027 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 3028 | vcpu_thread_barrier.wait(); |
| 3029 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3030 | 'wait: loop { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3031 | let events = { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3032 | match wait_ctx.wait() { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3033 | Ok(v) => v, |
| 3034 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 3035 | error!("failed to poll: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3036 | break; |
| 3037 | } |
| 3038 | } |
| 3039 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3040 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3041 | if let Err(e) = linux.irq_chip.process_delayed_irq_events() { |
| 3042 | warn!("can't deliver delayed irqs: {}", e); |
| 3043 | } |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 3044 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3045 | let mut vm_control_indices_to_remove = Vec::new(); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3046 | for event in events.iter().filter(|e| e.is_readable) { |
| 3047 | match event.token { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3048 | Token::Exit => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3049 | info!("vcpu requested shutdown"); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3050 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3051 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3052 | Token::Suspend => { |
| 3053 | info!("VM requested suspend"); |
| 3054 | linux.suspend_evt.read().unwrap(); |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3055 | kick_all_vcpus( |
| 3056 | &vcpu_handles, |
| 3057 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 3058 | VcpuControl::RunState(VmRunMode::Suspending), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3059 | ); |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3060 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3061 | Token::ChildSignal => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3062 | // Print all available siginfo structs, then exit the loop. |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 3063 | while let Some(siginfo) = sigchld_fd.read().map_err(Error::SignalFd)? { |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 3064 | let pid = siginfo.ssi_pid; |
| 3065 | let pid_label = match linux.pid_debug_label_map.get(&pid) { |
| 3066 | Some(label) => format!("{} (pid {})", label, pid), |
| 3067 | None => format!("pid {}", pid), |
| 3068 | }; |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 3069 | error!( |
| 3070 | "child {} died: signo {}, status {}, code {}", |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 3071 | pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 3072 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3073 | } |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3074 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3075 | } |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 3076 | Token::IrqFd { index } => { |
| 3077 | if let Err(e) = linux.irq_chip.service_irq_event(index) { |
| 3078 | error!("failed to signal irq {}: {}", index, e); |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 3079 | } |
| 3080 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3081 | Token::BalanceMemory => { |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 3082 | balancemem_timer.wait().map_err(Error::Timer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3083 | let command = BalloonControlCommand::Stats {}; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3084 | if let Err(e) = balloon_host_tube.send(&command) { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3085 | warn!("failed to send stats request to balloon device: {}", e); |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 3086 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 3087 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3088 | Token::BalloonResult => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3089 | match balloon_host_tube.recv() { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3090 | Ok(BalloonControlResult::Stats { |
| 3091 | stats, |
| 3092 | balloon_actual: balloon_actual_u, |
| 3093 | }) => { |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 3094 | match balloon_policy |
| 3095 | .as_mut() |
| 3096 | .map(|p| p.delta(stats, balloon_actual_u)) |
| 3097 | { |
| 3098 | None => { |
| 3099 | error!( |
| 3100 | "got result from balloon stats, but no policy is running" |
| 3101 | ); |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3102 | } |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 3103 | Some(Err(e)) => { |
| 3104 | warn!("failed to run balloon policy {}", e); |
| 3105 | } |
| 3106 | Some(Ok(delta)) if delta != 0 => { |
| 3107 | let target = max((balloon_actual_u as i64) + delta, 0) as u64; |
| 3108 | let command = |
| 3109 | BalloonControlCommand::Adjust { num_bytes: target }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3110 | if let Err(e) = balloon_host_tube.send(&command) { |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 3111 | warn!( |
| 3112 | "failed to send memory value to balloon device: {}", |
| 3113 | e |
| 3114 | ); |
| 3115 | } |
| 3116 | } |
| 3117 | Some(Ok(_)) => {} |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3118 | } |
| 3119 | } |
| 3120 | Err(e) => { |
| 3121 | error!("failed to recv BalloonControlResult: {}", e); |
| 3122 | } |
| 3123 | }; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 3124 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3125 | Token::VmControlServer => { |
| 3126 | if let Some(socket_server) = &control_server_socket { |
| 3127 | match socket_server.accept() { |
| 3128 | Ok(socket) => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3129 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3130 | .add( |
| 3131 | &socket, |
| 3132 | Token::VmControl { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3133 | index: control_tubes.len(), |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3134 | }, |
| 3135 | ) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3136 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3137 | control_tubes.push(TaggedControlTube::Vm(Tube::new(socket))); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3138 | } |
| 3139 | Err(e) => error!("failed to accept socket: {}", e), |
| 3140 | } |
| 3141 | } |
| 3142 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3143 | Token::VmControl { index } => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3144 | if let Some(socket) = control_tubes.get(index) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3145 | match socket { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3146 | TaggedControlTube::Vm(tube) => match tube.recv::<VmRequest>() { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3147 | Ok(request) => { |
| 3148 | let mut run_mode_opt = None; |
| 3149 | let response = request.execute( |
| 3150 | &mut run_mode_opt, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3151 | &balloon_host_tube, |
| 3152 | disk_host_tubes, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 3153 | #[cfg(feature = "usb")] |
| 3154 | Some(&usb_control_tube), |
| 3155 | #[cfg(not(feature = "usb"))] |
| 3156 | None, |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 3157 | &mut linux.bat_control, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3158 | ); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3159 | if let Err(e) = tube.send(&response) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3160 | error!("failed to send VmResponse: {}", e); |
| 3161 | } |
| 3162 | if let Some(run_mode) = run_mode_opt { |
| 3163 | info!("control socket changed run mode to {}", run_mode); |
| 3164 | match run_mode { |
| 3165 | VmRunMode::Exiting => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3166 | break 'wait; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3167 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3168 | other => { |
Chuanxiao Dong | 2bbe85c | 2020-11-12 17:18:07 +0800 | [diff] [blame] | 3169 | if other == VmRunMode::Running { |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3170 | linux.io_bus.notify_resume(); |
| 3171 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 3172 | kick_all_vcpus( |
| 3173 | &vcpu_handles, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3174 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 3175 | VcpuControl::RunState(other), |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 3176 | ); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3180 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3181 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3182 | if let TubeError::Disconnected = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3183 | vm_control_indices_to_remove.push(index); |
| 3184 | } else { |
| 3185 | error!("failed to recv VmRequest: {}", e); |
| 3186 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3187 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3188 | }, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3189 | TaggedControlTube::VmMemory(tube) => { |
| 3190 | match tube.recv::<VmMemoryRequest>() { |
| 3191 | Ok(request) => { |
| 3192 | let response = request.execute( |
| 3193 | &mut linux.vm, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3194 | &mut sys_allocator, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3195 | Arc::clone(&map_request), |
| 3196 | &mut gralloc, |
| 3197 | ); |
| 3198 | if let Err(e) = tube.send(&response) { |
| 3199 | error!("failed to send VmMemoryControlResponse: {}", e); |
| 3200 | } |
| 3201 | } |
| 3202 | Err(e) => { |
| 3203 | if let TubeError::Disconnected = e { |
| 3204 | vm_control_indices_to_remove.push(index); |
| 3205 | } else { |
| 3206 | error!("failed to recv VmMemoryControlRequest: {}", e); |
| 3207 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3208 | } |
| 3209 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3210 | } |
| 3211 | TaggedControlTube::VmIrq(tube) => match tube.recv::<VmIrqRequest>() { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3212 | Ok(request) => { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3213 | let response = { |
| 3214 | let irq_chip = &mut linux.irq_chip; |
| 3215 | request.execute( |
| 3216 | |setup| match setup { |
| 3217 | IrqSetup::Event(irq, ev) => { |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 3218 | if let Some(event_index) = irq_chip |
| 3219 | .register_irq_event(irq, ev, None)? |
| 3220 | { |
| 3221 | match wait_ctx.add( |
| 3222 | ev, |
| 3223 | Token::IrqFd { |
| 3224 | index: event_index |
| 3225 | }, |
| 3226 | ) { |
| 3227 | Err(e) => { |
| 3228 | warn!("failed to add IrqFd to poll context: {}", e); |
| 3229 | Err(e) |
| 3230 | }, |
| 3231 | Ok(_) => { |
| 3232 | Ok(()) |
| 3233 | } |
| 3234 | } |
| 3235 | } else { |
| 3236 | Ok(()) |
| 3237 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3238 | } |
| 3239 | IrqSetup::Route(route) => irq_chip.route_irq(route), |
| 3240 | }, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3241 | &mut sys_allocator, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3242 | ) |
| 3243 | }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3244 | if let Err(e) = tube.send(&response) { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3245 | error!("failed to send VmIrqResponse: {}", e); |
| 3246 | } |
| 3247 | } |
| 3248 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3249 | if let TubeError::Disconnected = e { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3250 | vm_control_indices_to_remove.push(index); |
| 3251 | } else { |
| 3252 | error!("failed to recv VmIrqRequest: {}", e); |
| 3253 | } |
| 3254 | } |
| 3255 | }, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3256 | TaggedControlTube::VmMsync(tube) => { |
| 3257 | match tube.recv::<VmMsyncRequest>() { |
| 3258 | Ok(request) => { |
| 3259 | let response = request.execute(&mut linux.vm); |
| 3260 | if let Err(e) = tube.send(&response) { |
| 3261 | error!("failed to send VmMsyncResponse: {}", e); |
| 3262 | } |
| 3263 | } |
| 3264 | Err(e) => { |
| 3265 | if let TubeError::Disconnected = e { |
| 3266 | vm_control_indices_to_remove.push(index); |
| 3267 | } else { |
| 3268 | error!("failed to recv VmMsyncRequest: {}", e); |
| 3269 | } |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 3270 | } |
| 3271 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3272 | } |
| 3273 | TaggedControlTube::Fs(tube) => match tube.recv::<FsMappingRequest>() { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 3274 | Ok(request) => { |
| 3275 | let response = |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3276 | request.execute(&mut linux.vm, &mut sys_allocator); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3277 | if let Err(e) = tube.send(&response) { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 3278 | error!("failed to send VmResponse: {}", e); |
| 3279 | } |
| 3280 | } |
| 3281 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3282 | if let TubeError::Disconnected = e { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 3283 | vm_control_indices_to_remove.push(index); |
| 3284 | } else { |
| 3285 | error!("failed to recv VmResponse: {}", e); |
| 3286 | } |
| 3287 | } |
| 3288 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3289 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3290 | } |
| 3291 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3292 | } |
| 3293 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3294 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3295 | for event in events.iter().filter(|e| e.is_hungup) { |
| 3296 | match event.token { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3297 | Token::Exit => {} |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3298 | Token::Suspend => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3299 | Token::ChildSignal => {} |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 3300 | Token::IrqFd { index: _ } => {} |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3301 | Token::BalanceMemory => {} |
| 3302 | Token::BalloonResult => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3303 | Token::VmControlServer => {} |
| 3304 | Token::VmControl { index } => { |
| 3305 | // It's possible more data is readable and buffered while the socket is hungup, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3306 | // so don't delete the tube from the poll context until we're sure all the |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3307 | // data is read. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3308 | if control_tubes |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3309 | .get(index) |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3310 | .map(|s| !s.as_ref().is_packet_ready()) |
| 3311 | .unwrap_or(false) |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3312 | { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3313 | vm_control_indices_to_remove.push(index); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 3314 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3315 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3316 | } |
| 3317 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3318 | |
| 3319 | // Sort in reverse so the highest indexes are removed first. This removal algorithm |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 3320 | // preserves correct indexes as each element is removed. |
Daniel Verkamp | 8c2f000 | 2020-08-31 15:13:35 -0700 | [diff] [blame] | 3321 | vm_control_indices_to_remove.sort_unstable_by_key(|&k| Reverse(k)); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3322 | vm_control_indices_to_remove.dedup(); |
| 3323 | for index in vm_control_indices_to_remove { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3324 | // Delete the socket from the `wait_ctx` synchronously. Otherwise, the kernel will do |
| 3325 | // this automatically when the FD inserted into the `wait_ctx` is closed after this |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 3326 | // if-block, but this removal can be deferred unpredictably. In some instances where the |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3327 | // system is under heavy load, we can even get events returned by `wait_ctx` for an FD |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 3328 | // that has already been closed. Because the token associated with that spurious event |
| 3329 | // now belongs to a different socket, the control loop will start to interact with |
| 3330 | // sockets that might not be ready to use. This can cause incorrect hangup detection or |
| 3331 | // blocking on a socket that will never be ready. See also: crbug.com/1019986 |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3332 | if let Some(socket) = control_tubes.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3333 | wait_ctx.delete(socket).map_err(Error::WaitContextDelete)?; |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | // This line implicitly drops the socket at `index` when it gets returned by |
| 3337 | // `swap_remove`. After this line, the socket at `index` is not the one from |
| 3338 | // `vm_control_indices_to_remove`. Because of this socket's change in index, we need to |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3339 | // use `wait_ctx.modify` to change the associated index in its `Token::VmControl`. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3340 | control_tubes.swap_remove(index); |
| 3341 | if let Some(tube) = control_tubes.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3342 | wait_ctx |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 3343 | .modify(tube, EventType::Read, Token::VmControl { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3344 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3345 | } |
| 3346 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3347 | } |
| 3348 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3349 | kick_all_vcpus( |
| 3350 | &vcpu_handles, |
| 3351 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 3352 | VcpuControl::RunState(VmRunMode::Exiting), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 3353 | ); |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 3354 | for (handle, _) in vcpu_handles { |
| 3355 | if let Err(e) = handle.join() { |
| 3356 | error!("failed to join vcpu thread: {:?}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 3360 | // Explicitly drop the VM structure here to allow the devices to clean up before the |
| 3361 | // control sockets are closed when this function exits. |
| 3362 | mem::drop(linux); |
| 3363 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 3364 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3365 | .set_canon_mode() |
| 3366 | .expect("failed to restore canonical mode for terminal"); |
| 3367 | |
| 3368 | Ok(()) |
| 3369 | } |