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