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