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