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