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