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