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}; |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 6 | use std::collections::{BTreeMap, BTreeSet}; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 7 | use std::convert::TryInto; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 8 | use std::fs::{File, OpenOptions}; |
Vineeth Pillai | 2b6855e | 2022-01-12 16:57:22 +0000 | [diff] [blame] | 9 | use std::io::prelude::*; |
Federico 'Morg' Pareschi | a118482 | 2021-09-09 10:52:58 +0900 | [diff] [blame] | 10 | use std::io::stdin; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 11 | use std::iter; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 12 | use std::mem; |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 13 | use std::ops::RangeInclusive; |
Anton Romanov | d43ae3c | 2022-01-31 17:32:54 +0000 | [diff] [blame] | 14 | #[cfg(feature = "gpu")] |
| 15 | use std::os::unix::net::UnixStream; |
| 16 | use std::os::unix::prelude::OpenOptionsExt; |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 17 | use std::path::Path; |
Daniel Verkamp | aebda74 | 2022-04-07 16:37:27 -0700 | [diff] [blame] | 18 | use std::str::FromStr; |
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 | |
Vineeth Pillai | 2b6855e | 2022-01-12 16:57:22 +0000 | [diff] [blame] | 22 | use std::process; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 23 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 24 | use std::thread; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 25 | |
Alexandre Courbot | c6ad83f | 2022-02-07 19:45:31 +0900 | [diff] [blame] | 26 | use devices::virtio::vhost::vsock::{VhostVsockConfig, VhostVsockDeviceParameter}; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 27 | use libc; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 28 | |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 29 | use acpi_tables::sdt::SDT; |
| 30 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 31 | use anyhow::{anyhow, bail, Context, Result}; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 32 | use base::*; |
Daniel Verkamp | 578e7cc | 2022-03-01 22:34:52 -0800 | [diff] [blame] | 33 | use base::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener}; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 34 | use devices::serial_device::SerialHardware; |
Zide Chen | afdb938 | 2021-06-17 12:04:43 -0700 | [diff] [blame] | 35 | use devices::vfio::{VfioCommonSetup, VfioCommonTrait}; |
Woody Chow | 055b81b | 2022-01-25 18:34:29 +0900 | [diff] [blame] | 36 | use devices::virtio::memory_mapper::MemoryMapperTrait; |
Anton Romanov | d43ae3c | 2022-01-31 17:32:54 +0000 | [diff] [blame] | 37 | #[cfg(feature = "gpu")] |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 38 | use devices::virtio::{self, EventDevice}; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 39 | #[cfg(feature = "audio")] |
| 40 | use devices::Ac97Dev; |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 41 | use devices::{ |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 42 | self, BusDeviceObj, HostHotPlugKey, HotPlugBus, IrqEventIndex, KvmKernelIrqChip, PciAddress, |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 43 | PciDevice, PvPanicCode, PvPanicPciDevice, StubPciDevice, VirtioPciDevice, |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 44 | }; |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 45 | use devices::{CoIommuDev, IommuDevType}; |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 46 | #[cfg(feature = "usb")] |
| 47 | use devices::{HostBackendDeviceProvider, XhciController}; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 48 | use hypervisor::kvm::{Kvm, KvmVcpu, KvmVm}; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 49 | use hypervisor::{HypervisorCap, ProtectionType, Vm, VmCap}; |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 50 | use minijail::{self, Minijail}; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 51 | use resources::{Alloc, SystemAllocator}; |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 52 | use rutabaga_gfx::RutabagaGralloc; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 53 | use sync::Mutex; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 54 | use vm_control::*; |
Sergey Senozhatsky | d78d05b | 2021-04-13 20:59:58 +0900 | [diff] [blame] | 55 | use vm_memory::{GuestAddress, GuestMemory, MemoryPolicy}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 56 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 57 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 58 | use crate::gdb::{gdb_thread, GdbStub}; |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 59 | use crate::{Config, Executable, FileBackedMappingParameters, SharedDir, SharedDirKind, VfioType}; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 60 | use arch::{ |
Keiichi Watanabe | 553d219 | 2021-08-16 16:42:27 +0900 | [diff] [blame] | 61 | self, LinuxArch, RunnableLinuxVm, VcpuAffinity, VirtioDeviceStub, VmComponents, VmImage, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 62 | }; |
Sonny Rao | ed517d1 | 2018-02-13 22:09:43 -0800 | [diff] [blame] | 63 | |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 64 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 65 | use { |
| 66 | crate::HostPcieRootPortParameters, |
| 67 | devices::{ |
| 68 | IrqChipX86_64 as IrqChipArch, KvmSplitIrqChip, PciBridge, PcieHostRootPort, PcieRootPort, |
| 69 | }, |
| 70 | hypervisor::{VcpuX86_64 as VcpuArch, VmX86_64 as VmArch}, |
| 71 | x86_64::X8664arch as Arch, |
| 72 | }; |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 73 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 74 | use { |
| 75 | aarch64::AArch64 as Arch, |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 76 | devices::IrqChipAArch64 as IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 77 | hypervisor::{VcpuAArch64 as VcpuArch, VmAArch64 as VmArch}, |
| 78 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 79 | |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 80 | mod device_helpers; |
| 81 | use device_helpers::*; |
Anton Romanov | db0f4d6 | 2022-03-23 21:24:29 +0000 | [diff] [blame] | 82 | pub(crate) mod jail_helpers; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 83 | use jail_helpers::*; |
| 84 | mod vcpu; |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 85 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 86 | #[cfg(feature = "gpu")] |
Anton Romanov | db0f4d6 | 2022-03-23 21:24:29 +0000 | [diff] [blame] | 87 | pub(crate) mod gpu; |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 88 | #[cfg(feature = "gpu")] |
Dmitry Torokhov | e464a7a | 2022-01-26 13:29:36 -0800 | [diff] [blame] | 89 | pub use gpu::GpuRenderServerParameters; |
| 90 | #[cfg(feature = "gpu")] |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 91 | use gpu::*; |
Jorge E. Moreira | d4562d0 | 2021-06-28 16:21:12 -0700 | [diff] [blame] | 92 | |
Jiyong Park | 0914020 | 2022-04-01 13:04:40 +0900 | [diff] [blame] | 93 | #[cfg(target_os = "android")] |
| 94 | mod android; |
| 95 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 96 | // gpu_device_tube is not used when GPU support is disabled. |
Dmitry Torokhov | ee42b8c | 2019-05-27 11:14:20 -0700 | [diff] [blame] | 97 | #[cfg_attr(not(feature = "gpu"), allow(unused_variables))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 98 | fn create_virtio_devices( |
| 99 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 100 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 101 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 102 | _exit_evt: &Event, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 103 | wayland_device_tube: Tube, |
| 104 | gpu_device_tube: Tube, |
Alexandre Courbot | e55b791 | 2022-03-04 16:54:38 +0900 | [diff] [blame] | 105 | vhost_user_gpu_tubes: Vec<(Tube, Tube, Tube)>, |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 106 | balloon_device_tube: Option<Tube>, |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 107 | balloon_inflate_tube: Option<Tube>, |
David Stevens | 06d157a | 2022-01-13 23:44:48 +0900 | [diff] [blame] | 108 | init_balloon_size: u64, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 109 | disk_device_tubes: &mut Vec<Tube>, |
| 110 | pmem_device_tubes: &mut Vec<Tube>, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 111 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 112 | fs_device_tubes: &mut Vec<Tube>, |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 113 | #[cfg(feature = "gpu")] render_server_fd: Option<SafeDescriptor>, |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 114 | vvu_proxy_device_tubes: &mut Vec<Tube>, |
David Stevens | e123e39 | 2022-04-07 09:59:25 +0900 | [diff] [blame] | 115 | vvu_proxy_max_sibling_mem_size: u64, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 116 | ) -> DeviceResult<Vec<VirtioDeviceStub>> { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 117 | let mut devs = Vec::new(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 118 | |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 119 | #[cfg(feature = "gpu")] |
Alexandre Courbot | e55b791 | 2022-03-04 16:54:38 +0900 | [diff] [blame] | 120 | for (opt, (host_gpu_tube, device_gpu_tube, device_control_tube)) in |
| 121 | cfg.vhost_user_gpu.iter().zip(vhost_user_gpu_tubes) |
| 122 | { |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 123 | devs.push(create_vhost_user_gpu_device( |
| 124 | cfg, |
| 125 | opt, |
Alexandre Courbot | e55b791 | 2022-03-04 16:54:38 +0900 | [diff] [blame] | 126 | (host_gpu_tube, device_gpu_tube), |
| 127 | device_control_tube, |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 128 | )?); |
| 129 | } |
| 130 | |
Abhishek Bhardwaj | 103c1b7 | 2021-11-01 15:52:23 -0700 | [diff] [blame] | 131 | for opt in &cfg.vvu_proxy { |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 132 | devs.push(create_vvu_proxy_device( |
| 133 | cfg, |
| 134 | opt, |
| 135 | vvu_proxy_device_tubes.remove(0), |
David Stevens | e123e39 | 2022-04-07 09:59:25 +0900 | [diff] [blame] | 136 | vvu_proxy_max_sibling_mem_size, |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 137 | )?); |
Abhishek Bhardwaj | 103c1b7 | 2021-11-01 15:52:23 -0700 | [diff] [blame] | 138 | } |
| 139 | |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 140 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 141 | let mut resource_bridges = Vec::<Tube>::new(); |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 142 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 143 | if !cfg.wayland_socket_paths.is_empty() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 144 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 145 | let mut wl_resource_bridge = None::<Tube>; |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 146 | |
| 147 | #[cfg(feature = "gpu")] |
| 148 | { |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 149 | if cfg.gpu_parameters.is_some() { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 150 | let (wl_socket, gpu_socket) = Tube::pair().context("failed to create tube")?; |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 151 | resource_bridges.push(gpu_socket); |
| 152 | wl_resource_bridge = Some(wl_socket); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | devs.push(create_wayland_device( |
| 157 | cfg, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 158 | wayland_device_tube, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 159 | wl_resource_bridge, |
| 160 | )?); |
| 161 | } |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 162 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 163 | #[cfg(feature = "video-decoder")] |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 164 | let video_dec_cfg = if let Some(backend) = cfg.video_dec { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 165 | let (video_tube, gpu_tube) = Tube::pair().context("failed to create tube")?; |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 166 | resource_bridges.push(gpu_tube); |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 167 | Some((video_tube, backend)) |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 168 | } else { |
| 169 | None |
| 170 | }; |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 171 | |
| 172 | #[cfg(feature = "video-encoder")] |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 173 | let video_enc_cfg = if let Some(backend) = cfg.video_enc { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 174 | let (video_tube, gpu_tube) = Tube::pair().context("failed to create tube")?; |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 175 | resource_bridges.push(gpu_tube); |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 176 | Some((video_tube, backend)) |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 177 | } else { |
| 178 | None |
| 179 | }; |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 180 | |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 181 | #[cfg(feature = "gpu")] |
| 182 | { |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 183 | if let Some(gpu_parameters) = &cfg.gpu_parameters { |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 184 | let mut gpu_display_w = virtio::DEFAULT_DISPLAY_WIDTH; |
| 185 | let mut gpu_display_h = virtio::DEFAULT_DISPLAY_HEIGHT; |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame] | 186 | if !gpu_parameters.displays.is_empty() { |
| 187 | gpu_display_w = gpu_parameters.displays[0].width; |
| 188 | gpu_display_h = gpu_parameters.displays[0].height; |
| 189 | } |
| 190 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 191 | let mut event_devices = Vec::new(); |
| 192 | if cfg.display_window_mouse { |
| 193 | let (event_device_socket, virtio_dev_socket) = |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 194 | UnixStream::pair().context("failed to create socket")?; |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 195 | let (multi_touch_width, multi_touch_height) = cfg |
| 196 | .virtio_multi_touch |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 197 | .first() |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 198 | .as_ref() |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 199 | .map(|multi_touch_spec| multi_touch_spec.get_size()) |
Jason Macnak | d659a0d | 2021-03-15 15:33:01 -0700 | [diff] [blame] | 200 | .unwrap_or((gpu_display_w, gpu_display_h)); |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 201 | let dev = virtio::new_multi_touch( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 202 | // u32::MAX is the least likely to collide with the indices generated above for |
| 203 | // the multi_touch options, which begin at 0. |
| 204 | u32::MAX, |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 205 | virtio_dev_socket, |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 206 | multi_touch_width, |
| 207 | multi_touch_height, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 208 | virtio::base_features(cfg.protected_vm), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 209 | ) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 210 | .context("failed to set up mouse device")?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 211 | devs.push(VirtioDeviceStub { |
| 212 | dev: Box::new(dev), |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 213 | jail: simple_jail(&cfg.jail_config, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 214 | }); |
| 215 | event_devices.push(EventDevice::touchscreen(event_device_socket)); |
| 216 | } |
| 217 | if cfg.display_window_keyboard { |
| 218 | let (event_device_socket, virtio_dev_socket) = |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 219 | UnixStream::pair().context("failed to create socket")?; |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 220 | let dev = virtio::new_keyboard( |
Jorge E. Moreira | 6635ca4 | 2021-04-28 13:11:41 -0700 | [diff] [blame] | 221 | // u32::MAX is the least likely to collide with the indices generated above for |
| 222 | // the multi_touch options, which begin at 0. |
| 223 | u32::MAX, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 224 | virtio_dev_socket, |
| 225 | virtio::base_features(cfg.protected_vm), |
| 226 | ) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 227 | .context("failed to set up keyboard device")?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 228 | devs.push(VirtioDeviceStub { |
| 229 | dev: Box::new(dev), |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 230 | jail: simple_jail(&cfg.jail_config, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 231 | }); |
| 232 | event_devices.push(EventDevice::keyboard(event_device_socket)); |
| 233 | } |
Chia-I Wu | 16fb659 | 2021-11-10 11:45:32 -0800 | [diff] [blame] | 234 | |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 235 | devs.push(create_gpu_device( |
| 236 | cfg, |
| 237 | _exit_evt, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 238 | gpu_device_tube, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 239 | resource_bridges, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 240 | // Use the unnamed socket for GPU display screens. |
| 241 | cfg.wayland_socket_paths.get(""), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 242 | cfg.x_display.clone(), |
Chia-I Wu | 16fb659 | 2021-11-10 11:45:32 -0800 | [diff] [blame] | 243 | render_server_fd, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 244 | event_devices, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 245 | map_request, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 246 | )?); |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Richard Fung | 08289b1 | 2022-02-02 20:46:19 +0000 | [diff] [blame] | 250 | for (_, param) in cfg |
| 251 | .serial_parameters |
| 252 | .iter() |
| 253 | .filter(|(_k, v)| v.hardware == SerialHardware::VirtioConsole) |
| 254 | { |
| 255 | let dev = create_console_device(cfg, param)?; |
| 256 | devs.push(dev); |
| 257 | } |
| 258 | |
| 259 | for disk in &cfg.disks { |
| 260 | let disk_device_tube = disk_device_tubes.remove(0); |
| 261 | devs.push(create_block_device(cfg, disk, disk_device_tube)?); |
| 262 | } |
| 263 | |
| 264 | for blk in &cfg.vhost_user_blk { |
| 265 | devs.push(create_vhost_user_block_device(cfg, blk)?); |
| 266 | } |
| 267 | |
| 268 | for console in &cfg.vhost_user_console { |
| 269 | devs.push(create_vhost_user_console_device(cfg, console)?); |
| 270 | } |
| 271 | |
| 272 | for (index, pmem_disk) in cfg.pmem_devices.iter().enumerate() { |
| 273 | let pmem_device_tube = pmem_device_tubes.remove(0); |
| 274 | devs.push(create_pmem_device( |
| 275 | cfg, |
| 276 | vm, |
| 277 | resources, |
| 278 | pmem_disk, |
| 279 | index, |
| 280 | pmem_device_tube, |
| 281 | )?); |
| 282 | } |
| 283 | |
Andrew Walbran | a24a752 | 2022-02-09 18:23:00 +0000 | [diff] [blame] | 284 | if cfg.rng { |
| 285 | devs.push(create_rng_device(cfg)?); |
| 286 | } |
Richard Fung | 08289b1 | 2022-02-02 20:46:19 +0000 | [diff] [blame] | 287 | |
| 288 | #[cfg(feature = "tpm")] |
| 289 | { |
| 290 | if cfg.software_tpm { |
Daniel Verkamp | 29950ef | 2021-10-07 14:56:45 -0700 | [diff] [blame] | 291 | devs.push(create_software_tpm_device(cfg)?); |
Richard Fung | 08289b1 | 2022-02-02 20:46:19 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | for (idx, single_touch_spec) in cfg.virtio_single_touch.iter().enumerate() { |
| 296 | devs.push(create_single_touch_device( |
| 297 | cfg, |
| 298 | single_touch_spec, |
| 299 | idx as u32, |
| 300 | )?); |
| 301 | } |
| 302 | |
| 303 | for (idx, multi_touch_spec) in cfg.virtio_multi_touch.iter().enumerate() { |
| 304 | devs.push(create_multi_touch_device( |
| 305 | cfg, |
| 306 | multi_touch_spec, |
| 307 | idx as u32, |
| 308 | )?); |
| 309 | } |
| 310 | |
| 311 | for (idx, trackpad_spec) in cfg.virtio_trackpad.iter().enumerate() { |
| 312 | devs.push(create_trackpad_device(cfg, trackpad_spec, idx as u32)?); |
| 313 | } |
| 314 | |
| 315 | for (idx, mouse_socket) in cfg.virtio_mice.iter().enumerate() { |
| 316 | devs.push(create_mouse_device(cfg, mouse_socket, idx as u32)?); |
| 317 | } |
| 318 | |
| 319 | for (idx, keyboard_socket) in cfg.virtio_keyboard.iter().enumerate() { |
| 320 | devs.push(create_keyboard_device(cfg, keyboard_socket, idx as u32)?); |
| 321 | } |
| 322 | |
| 323 | for (idx, switches_socket) in cfg.virtio_switches.iter().enumerate() { |
| 324 | devs.push(create_switches_device(cfg, switches_socket, idx as u32)?); |
| 325 | } |
| 326 | |
| 327 | for dev_path in &cfg.virtio_input_evdevs { |
| 328 | devs.push(create_vinput_device(cfg, dev_path)?); |
| 329 | } |
| 330 | |
| 331 | if let Some(balloon_device_tube) = balloon_device_tube { |
| 332 | devs.push(create_balloon_device( |
| 333 | cfg, |
| 334 | balloon_device_tube, |
| 335 | balloon_inflate_tube, |
| 336 | init_balloon_size, |
| 337 | )?); |
| 338 | } |
| 339 | |
| 340 | // We checked above that if the IP is defined, then the netmask is, too. |
| 341 | for tap_fd in &cfg.tap_fd { |
| 342 | devs.push(create_tap_net_device_from_fd(cfg, *tap_fd)?); |
| 343 | } |
| 344 | |
| 345 | if let (Some(host_ip), Some(netmask), Some(mac_address)) = |
| 346 | (cfg.host_ip, cfg.netmask, cfg.mac_address) |
| 347 | { |
| 348 | if !cfg.vhost_user_net.is_empty() { |
| 349 | bail!("vhost-user-net cannot be used with any of --host_ip, --netmask or --mac"); |
| 350 | } |
| 351 | devs.push(create_net_device_from_config( |
| 352 | cfg, |
| 353 | host_ip, |
| 354 | netmask, |
| 355 | mac_address, |
| 356 | )?); |
| 357 | } |
| 358 | |
| 359 | for tap_name in &cfg.tap_name { |
| 360 | devs.push(create_tap_net_device_from_name(cfg, tap_name.as_bytes())?); |
| 361 | } |
| 362 | |
| 363 | for net in &cfg.vhost_user_net { |
| 364 | devs.push(create_vhost_user_net_device(cfg, net)?); |
| 365 | } |
| 366 | |
| 367 | for vsock in &cfg.vhost_user_vsock { |
| 368 | devs.push(create_vhost_user_vsock_device(cfg, vsock)?); |
| 369 | } |
| 370 | |
| 371 | for opt in &cfg.vhost_user_wl { |
| 372 | devs.push(create_vhost_user_wl_device(cfg, opt)?); |
| 373 | } |
| 374 | |
Chih-Yang Hsia | e31731c | 2022-01-05 17:30:28 +0800 | [diff] [blame] | 375 | #[cfg(feature = "audio_cras")] |
| 376 | { |
| 377 | for cras_snd in &cfg.cras_snds { |
| 378 | devs.push(create_cras_snd_device(cfg, cras_snd.clone())?); |
| 379 | } |
| 380 | } |
| 381 | |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 382 | #[cfg(feature = "video-decoder")] |
| 383 | { |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 384 | if let Some((video_dec_tube, video_dec_backend)) = video_dec_cfg { |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 385 | register_video_device( |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 386 | video_dec_backend, |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 387 | &mut devs, |
| 388 | video_dec_tube, |
| 389 | cfg, |
| 390 | devices::virtio::VideoDeviceType::Decoder, |
| 391 | )?; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | #[cfg(feature = "video-encoder")] |
| 396 | { |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 397 | if let Some((video_enc_tube, video_enc_backend)) = video_enc_cfg { |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 398 | register_video_device( |
Alexandre Courbot | b42b3e5 | 2021-07-09 23:38:57 +0900 | [diff] [blame] | 399 | video_enc_backend, |
Daniel Verkamp | ffb5912 | 2021-03-18 14:06:15 -0700 | [diff] [blame] | 400 | &mut devs, |
| 401 | video_enc_tube, |
| 402 | cfg, |
| 403 | devices::virtio::VideoDeviceType::Encoder, |
| 404 | )?; |
| 405 | } |
| 406 | } |
| 407 | |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 408 | if let Some(cid) = cfg.cid { |
Alexandre Courbot | c6ad83f | 2022-02-07 19:45:31 +0900 | [diff] [blame] | 409 | let vhost_config = VhostVsockConfig { |
| 410 | device: cfg |
| 411 | .vhost_vsock_device |
| 412 | .clone() |
| 413 | .unwrap_or(VhostVsockDeviceParameter::default()), |
| 414 | cid, |
| 415 | }; |
| 416 | devs.push(create_vhost_vsock_device(cfg, &vhost_config)?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 419 | for vhost_user_fs in &cfg.vhost_user_fs { |
Daniel Verkamp | 166d1dd | 2021-08-19 17:05:29 -0700 | [diff] [blame] | 420 | devs.push(create_vhost_user_fs_device(cfg, vhost_user_fs)?); |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 421 | } |
| 422 | |
Woody Chow | 1b16db1 | 2021-04-02 16:59:59 +0900 | [diff] [blame] | 423 | #[cfg(feature = "audio")] |
| 424 | for vhost_user_snd in &cfg.vhost_user_snd { |
| 425 | devs.push(create_vhost_user_snd_device(cfg, vhost_user_snd)?); |
| 426 | } |
| 427 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 428 | for shared_dir in &cfg.shared_dirs { |
| 429 | let SharedDir { |
| 430 | src, |
| 431 | tag, |
| 432 | kind, |
| 433 | uid_map, |
| 434 | gid_map, |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 435 | fs_cfg, |
| 436 | p9_cfg, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 437 | } = shared_dir; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 438 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 439 | let dev = match kind { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 440 | SharedDirKind::FS => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 441 | let device_tube = fs_device_tubes.remove(0); |
| 442 | 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] | 443 | } |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 444 | 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] | 445 | }; |
| 446 | devs.push(dev); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 447 | } |
| 448 | |
JaeMan Park | eb9cc53 | 2021-07-02 15:02:59 +0900 | [diff] [blame] | 449 | if let Some(vhost_user_mac80211_hwsim) = &cfg.vhost_user_mac80211_hwsim { |
| 450 | devs.push(create_vhost_user_mac80211_hwsim_device( |
| 451 | cfg, |
Daniel Verkamp | 166d1dd | 2021-08-19 17:05:29 -0700 | [diff] [blame] | 452 | vhost_user_mac80211_hwsim, |
JaeMan Park | eb9cc53 | 2021-07-02 15:02:59 +0900 | [diff] [blame] | 453 | )?); |
| 454 | } |
| 455 | |
Jorge E. Moreira | d4562d0 | 2021-06-28 16:21:12 -0700 | [diff] [blame] | 456 | #[cfg(feature = "audio")] |
| 457 | if let Some(path) = &cfg.sound { |
Daniel Verkamp | 166d1dd | 2021-08-19 17:05:29 -0700 | [diff] [blame] | 458 | devs.push(create_sound_device(path, cfg)?); |
Jorge E. Moreira | d4562d0 | 2021-06-28 16:21:12 -0700 | [diff] [blame] | 459 | } |
| 460 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 461 | Ok(devs) |
| 462 | } |
| 463 | |
| 464 | fn create_devices( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 465 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 466 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 467 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 468 | exit_evt: &Event, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 469 | panic_wrtube: Tube, |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 470 | iommu_attached_endpoints: &mut BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 471 | control_tubes: &mut Vec<TaggedControlTube>, |
| 472 | wayland_device_tube: Tube, |
| 473 | gpu_device_tube: Tube, |
Alexandre Courbot | e55b791 | 2022-03-04 16:54:38 +0900 | [diff] [blame] | 474 | // Tuple content: (host-side GPU tube, device-side GPU tube, device-side control tube). |
| 475 | vhost_user_gpu_tubes: Vec<(Tube, Tube, Tube)>, |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 476 | balloon_device_tube: Option<Tube>, |
David Stevens | 06d157a | 2022-01-13 23:44:48 +0900 | [diff] [blame] | 477 | init_balloon_size: u64, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 478 | disk_device_tubes: &mut Vec<Tube>, |
| 479 | pmem_device_tubes: &mut Vec<Tube>, |
| 480 | fs_device_tubes: &mut Vec<Tube>, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 481 | #[cfg(feature = "usb")] usb_provider: HostBackendDeviceProvider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 482 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 483 | #[cfg(feature = "gpu")] render_server_fd: Option<SafeDescriptor>, |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 484 | vvu_proxy_device_tubes: &mut Vec<Tube>, |
David Stevens | e123e39 | 2022-04-07 09:59:25 +0900 | [diff] [blame] | 485 | vvu_proxy_max_sibling_mem_size: u64, |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 486 | ) -> DeviceResult<Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)>> { |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 487 | let mut devices: Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)> = Vec::new(); |
| 488 | let mut balloon_inflate_tube: Option<Tube> = None; |
Zide Chen | 5deee48 | 2021-04-19 11:06:01 -0700 | [diff] [blame] | 489 | if !cfg.vfio.is_empty() { |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 490 | let mut coiommu_attached_endpoints = Vec::new(); |
Zide Chen | dfc4b88 | 2021-03-10 16:35:37 -0800 | [diff] [blame] | 491 | |
Tomasz Nowicki | 71aca79 | 2021-06-09 18:53:49 +0000 | [diff] [blame] | 492 | for vfio_dev in cfg |
| 493 | .vfio |
| 494 | .iter() |
| 495 | .filter(|dev| dev.get_type() == VfioType::Pci) |
| 496 | { |
| 497 | let vfio_path = &vfio_dev.vfio_path; |
Zide Chen | 5deee48 | 2021-04-19 11:06:01 -0700 | [diff] [blame] | 498 | let (vfio_pci_device, jail) = create_vfio_device( |
| 499 | cfg, |
| 500 | vm, |
| 501 | resources, |
| 502 | control_tubes, |
| 503 | vfio_path.as_path(), |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 504 | None, |
Victor Ding | 3f74959 | 2022-03-18 05:44:20 +0000 | [diff] [blame] | 505 | vfio_dev.guest_address(), |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 506 | iommu_attached_endpoints, |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 507 | Some(&mut coiommu_attached_endpoints), |
| 508 | vfio_dev.iommu_dev_type(), |
Zide Chen | 5deee48 | 2021-04-19 11:06:01 -0700 | [diff] [blame] | 509 | )?; |
Zide Chen | dfc4b88 | 2021-03-10 16:35:37 -0800 | [diff] [blame] | 510 | |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 511 | devices.push((vfio_pci_device, jail)); |
Zide Chen | 5deee48 | 2021-04-19 11:06:01 -0700 | [diff] [blame] | 512 | } |
Zide Chen | dfc4b88 | 2021-03-10 16:35:37 -0800 | [diff] [blame] | 513 | |
Tomasz Nowicki | 344eb14 | 2021-09-22 05:51:58 +0000 | [diff] [blame] | 514 | for vfio_dev in cfg |
| 515 | .vfio |
| 516 | .iter() |
| 517 | .filter(|dev| dev.get_type() == VfioType::Platform) |
| 518 | { |
| 519 | let vfio_path = &vfio_dev.vfio_path; |
| 520 | let (vfio_plat_dev, jail) = create_vfio_platform_device( |
| 521 | cfg, |
| 522 | vm, |
| 523 | resources, |
| 524 | control_tubes, |
| 525 | vfio_path.as_path(), |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 526 | iommu_attached_endpoints, |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 527 | IommuDevType::NoIommu, // Virtio IOMMU is not supported yet |
Tomasz Nowicki | 344eb14 | 2021-09-22 05:51:58 +0000 | [diff] [blame] | 528 | )?; |
| 529 | |
| 530 | devices.push((Box::new(vfio_plat_dev), jail)); |
| 531 | } |
| 532 | |
Chuanxiao Dong | cb03ec6 | 2022-01-20 08:25:38 +0800 | [diff] [blame] | 533 | if !coiommu_attached_endpoints.is_empty() || !iommu_attached_endpoints.is_empty() { |
| 534 | let mut buf = mem::MaybeUninit::<libc::rlimit>::zeroed(); |
| 535 | let res = unsafe { libc::getrlimit(libc::RLIMIT_MEMLOCK, buf.as_mut_ptr()) }; |
| 536 | if res == 0 { |
| 537 | let limit = unsafe { buf.assume_init() }; |
| 538 | let rlim_new = limit |
| 539 | .rlim_cur |
| 540 | .saturating_add(vm.get_memory().memory_size() as libc::rlim_t); |
| 541 | let rlim_max = max(limit.rlim_max, rlim_new); |
| 542 | if limit.rlim_cur < rlim_new { |
| 543 | let limit_arg = libc::rlimit { |
| 544 | rlim_cur: rlim_new as libc::rlim_t, |
| 545 | rlim_max: rlim_max as libc::rlim_t, |
| 546 | }; |
| 547 | let res = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &limit_arg) }; |
| 548 | if res != 0 { |
| 549 | bail!("Set rlimit failed"); |
| 550 | } |
| 551 | } |
| 552 | } else { |
| 553 | bail!("Get rlimit failed"); |
| 554 | } |
| 555 | } |
| 556 | |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 557 | if !coiommu_attached_endpoints.is_empty() { |
| 558 | let vfio_container = |
| 559 | VfioCommonSetup::vfio_get_container(IommuDevType::CoIommu, None as Option<&Path>) |
| 560 | .context("failed to get vfio container")?; |
| 561 | let (coiommu_host_tube, coiommu_device_tube) = |
| 562 | Tube::pair().context("failed to create coiommu tube")?; |
| 563 | control_tubes.push(TaggedControlTube::VmMemory(coiommu_host_tube)); |
| 564 | let vcpu_count = cfg.vcpu_count.unwrap_or(1) as u64; |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 565 | let (coiommu_tube, balloon_tube) = |
| 566 | Tube::pair().context("failed to create coiommu tube")?; |
| 567 | balloon_inflate_tube = Some(balloon_tube); |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 568 | let dev = CoIommuDev::new( |
| 569 | vm.get_memory().clone(), |
| 570 | vfio_container, |
| 571 | coiommu_device_tube, |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 572 | coiommu_tube, |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 573 | coiommu_attached_endpoints, |
| 574 | vcpu_count, |
Chuanxiao Dong | d446861 | 2022-01-14 14:21:17 +0800 | [diff] [blame] | 575 | cfg.coiommu_param.unwrap_or_default(), |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 576 | ) |
| 577 | .context("failed to create coiommu device")?; |
| 578 | |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 579 | devices.push((Box::new(dev), simple_jail(&cfg.jail_config, "coiommu")?)); |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 580 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 581 | } |
| 582 | |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 583 | let stubs = create_virtio_devices( |
| 584 | cfg, |
| 585 | vm, |
| 586 | resources, |
| 587 | exit_evt, |
| 588 | wayland_device_tube, |
| 589 | gpu_device_tube, |
| 590 | vhost_user_gpu_tubes, |
| 591 | balloon_device_tube, |
| 592 | balloon_inflate_tube, |
David Stevens | 06d157a | 2022-01-13 23:44:48 +0900 | [diff] [blame] | 593 | init_balloon_size, |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 594 | disk_device_tubes, |
| 595 | pmem_device_tubes, |
| 596 | map_request, |
| 597 | fs_device_tubes, |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 598 | #[cfg(feature = "gpu")] |
| 599 | render_server_fd, |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 600 | vvu_proxy_device_tubes, |
David Stevens | e123e39 | 2022-04-07 09:59:25 +0900 | [diff] [blame] | 601 | vvu_proxy_max_sibling_mem_size, |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 602 | )?; |
| 603 | |
| 604 | for stub in stubs { |
| 605 | let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?; |
| 606 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
| 607 | let dev = VirtioPciDevice::new(vm.get_memory().clone(), stub.dev, msi_device_tube) |
| 608 | .context("failed to create virtio pci dev")?; |
| 609 | let dev = Box::new(dev) as Box<dyn BusDeviceObj>; |
| 610 | devices.push((dev, stub.jail)); |
| 611 | } |
| 612 | |
| 613 | #[cfg(feature = "audio")] |
| 614 | for ac97_param in &cfg.ac97_parameters { |
| 615 | let dev = Ac97Dev::try_new(vm.get_memory().clone(), ac97_param.clone()) |
| 616 | .context("failed to create ac97 device")?; |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 617 | let jail = simple_jail(&cfg.jail_config, dev.minijail_policy())?; |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 618 | devices.push((Box::new(dev), jail)); |
| 619 | } |
| 620 | |
| 621 | #[cfg(feature = "usb")] |
Sebastian Ene | 0440d35 | 2022-02-04 12:23:56 +0000 | [diff] [blame] | 622 | if cfg.usb { |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 623 | // Create xhci controller. |
| 624 | let usb_controller = Box::new(XhciController::new(vm.get_memory().clone(), usb_provider)); |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 625 | devices.push((usb_controller, simple_jail(&cfg.jail_config, "xhci")?)); |
Chuanxiao Dong | 146a13b | 2021-12-09 12:59:54 +0800 | [diff] [blame] | 626 | } |
| 627 | |
Mattias Nissler | de2c640 | 2021-10-21 12:05:29 +0000 | [diff] [blame] | 628 | for params in &cfg.stub_pci_devices { |
| 629 | // Stub devices don't need jailing since they don't do anything. |
| 630 | devices.push((Box::new(StubPciDevice::new(params)), None)); |
| 631 | } |
| 632 | |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 633 | devices.push((Box::new(PvPanicPciDevice::new(panic_wrtube)), None)); |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 634 | Ok(devices) |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Mattias Nissler | bbd91d0 | 2021-12-07 08:57:45 +0000 | [diff] [blame] | 637 | fn create_file_backed_mappings( |
| 638 | cfg: &Config, |
| 639 | vm: &mut impl Vm, |
| 640 | resources: &mut SystemAllocator, |
| 641 | ) -> Result<()> { |
| 642 | for mapping in &cfg.file_backed_mappings { |
| 643 | let file = OpenOptions::new() |
| 644 | .read(true) |
| 645 | .write(mapping.writable) |
| 646 | .custom_flags(if mapping.sync { libc::O_SYNC } else { 0 }) |
| 647 | .open(&mapping.path) |
| 648 | .context("failed to open file for file-backed mapping")?; |
| 649 | let prot = if mapping.writable { |
| 650 | Protection::read_write() |
| 651 | } else { |
| 652 | Protection::read() |
| 653 | }; |
| 654 | let size = mapping |
| 655 | .size |
| 656 | .try_into() |
| 657 | .context("Invalid size for file-backed mapping")?; |
| 658 | let memory_mapping = MemoryMappingBuilder::new(size) |
| 659 | .from_file(&file) |
| 660 | .offset(mapping.offset) |
| 661 | .protection(prot) |
| 662 | .build() |
| 663 | .context("failed to map backing file for file-backed mapping")?; |
| 664 | |
Daniel Verkamp | de4d729 | 2022-03-01 15:22:38 -0800 | [diff] [blame] | 665 | match resources.mmio_allocator_any().allocate_at( |
| 666 | mapping.address, |
| 667 | mapping.size, |
| 668 | Alloc::FileBacked(mapping.address), |
| 669 | "file-backed mapping".to_owned(), |
| 670 | ) { |
| 671 | // OutOfSpace just means that this mapping is not in the MMIO regions at all, so don't |
| 672 | // consider it an error. |
| 673 | // TODO(b/222769529): Reserve this region in a global memory address space allocator once |
| 674 | // we have that so nothing else can accidentally overlap with it. |
| 675 | Ok(()) | Err(resources::Error::OutOfSpace) => {} |
| 676 | e => e.context("failed to allocate guest address for file-backed mapping")?, |
| 677 | } |
Mattias Nissler | bbd91d0 | 2021-12-07 08:57:45 +0000 | [diff] [blame] | 678 | |
| 679 | vm.add_memory_region( |
| 680 | GuestAddress(mapping.address), |
| 681 | Box::new(memory_mapping), |
| 682 | !mapping.writable, |
| 683 | /* log_dirty_pages = */ false, |
| 684 | ) |
| 685 | .context("failed to configure file-backed mapping")?; |
| 686 | } |
| 687 | |
| 688 | Ok(()) |
| 689 | } |
| 690 | |
Anton Romanov | 3333441 | 2022-03-22 17:48:18 +0000 | [diff] [blame] | 691 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 692 | fn create_pcie_root_port( |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 693 | host_pcie_rp: Vec<HostPcieRootPortParameters>, |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 694 | sys_allocator: &mut SystemAllocator, |
| 695 | control_tubes: &mut Vec<TaggedControlTube>, |
| 696 | devices: &mut Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)>, |
| 697 | hp_vec: &mut Vec<Arc<Mutex<dyn HotPlugBus>>>, |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 698 | hp_endpoints_ranges: &mut Vec<RangeInclusive<u32>>, |
Daniel Verkamp | f35f636 | 2022-04-07 17:24:21 -0700 | [diff] [blame] | 699 | // TODO(b/228627457): clippy is incorrectly warning about this Vec, which needs to be a Vec so |
| 700 | // we can push into it |
| 701 | #[allow(clippy::ptr_arg)] gpe_notify_devs: &mut Vec<(u32, Arc<Mutex<dyn GpeNotify>>)>, |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 702 | ) -> Result<()> { |
| 703 | if host_pcie_rp.is_empty() { |
| 704 | // user doesn't specify host pcie root port which link to this virtual pcie rp, |
| 705 | // find the empty bus and create a total virtual pcie rp |
Haiwei Li | e35d465 | 2022-02-10 15:39:33 +0800 | [diff] [blame] | 706 | let mut hp_sec_bus = 0u8; |
| 707 | // Create Pcie Root Port for non-root buses, each non-root bus device will be |
| 708 | // connected behind a virtual pcie root port. |
| 709 | for i in 1..255 { |
| 710 | if sys_allocator.pci_bus_empty(i) { |
| 711 | if hp_sec_bus == 0 { |
| 712 | hp_sec_bus = i; |
| 713 | } |
| 714 | continue; |
| 715 | } |
| 716 | let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new(i, false))); |
| 717 | let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?; |
| 718 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
| 719 | let pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube)); |
| 720 | // no ipc is used if the root port disables hotplug |
| 721 | devices.push((pci_bridge, None)); |
| 722 | } |
| 723 | |
| 724 | // Create Pcie Root Port for hot-plug |
| 725 | if hp_sec_bus == 0 { |
| 726 | return Err(anyhow!("no more addresses are available")); |
| 727 | } |
| 728 | let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new(hp_sec_bus, true))); |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 729 | let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?; |
| 730 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
| 731 | let pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube)); |
| 732 | |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 733 | hp_endpoints_ranges.push(RangeInclusive::new( |
| 734 | PciAddress { |
| 735 | bus: pci_bridge.get_secondary_num(), |
| 736 | dev: 0, |
| 737 | func: 0, |
| 738 | } |
| 739 | .to_u32(), |
| 740 | PciAddress { |
| 741 | bus: pci_bridge.get_subordinate_num(), |
| 742 | dev: 32, |
| 743 | func: 8, |
| 744 | } |
| 745 | .to_u32(), |
| 746 | )); |
| 747 | |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 748 | devices.push((pci_bridge, None)); |
| 749 | hp_vec.push(pcie_root_port as Arc<Mutex<dyn HotPlugBus>>); |
| 750 | } else { |
| 751 | // user specify host pcie root port which link to this virtual pcie rp, |
| 752 | // reserve the host pci BDF and create a virtual pcie RP with some attrs same as host |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 753 | for host_pcie in host_pcie_rp.iter() { |
Xiong Zhang | cdffe49 | 2021-12-24 15:13:30 +0800 | [diff] [blame] | 754 | let (vm_host_tube, vm_device_tube) = Tube::pair().context("failed to create tube")?; |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 755 | let pcie_host = PcieHostRootPort::new(host_pcie.host_path.as_path(), vm_device_tube)?; |
Xiong Zhang | d6de319 | 2022-02-16 13:24:06 +0800 | [diff] [blame] | 756 | let bus_range = pcie_host.get_bus_range(); |
| 757 | let mut slot_implemented = true; |
| 758 | for i in bus_range.secondary..=bus_range.subordinate { |
| 759 | // if this bus is occupied by one vfio-pci device, this vfio-pci device is |
| 760 | // connected to a pci bridge on host statically, then it should be connected |
| 761 | // to a virtual pci bridge in guest statically, this bridge won't have |
| 762 | // hotplug capability and won't use slot. |
| 763 | if !sys_allocator.pci_bus_empty(i) { |
| 764 | slot_implemented = false; |
Haiwei Li | e4a9e82 | 2022-03-24 14:18:32 +0800 | [diff] [blame] | 765 | break; |
Xiong Zhang | d6de319 | 2022-02-16 13:24:06 +0800 | [diff] [blame] | 766 | } |
| 767 | } |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 768 | |
Xiong Zhang | d6de319 | 2022-02-16 13:24:06 +0800 | [diff] [blame] | 769 | let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new_from_host( |
| 770 | pcie_host, |
| 771 | slot_implemented, |
| 772 | )?)); |
Xiong Zhang | cdffe49 | 2021-12-24 15:13:30 +0800 | [diff] [blame] | 773 | control_tubes.push(TaggedControlTube::Vm(vm_host_tube)); |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 774 | |
| 775 | let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?; |
| 776 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
| 777 | let mut pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube)); |
| 778 | // early reservation for host pcie root port devices. |
| 779 | let rootport_addr = pci_bridge.allocate_address(sys_allocator); |
| 780 | if rootport_addr.is_err() { |
| 781 | warn!( |
| 782 | "address reservation failed for hot pcie root port {}", |
| 783 | pci_bridge.debug_label() |
| 784 | ); |
| 785 | } |
| 786 | |
Haiwei Li | e4a9e82 | 2022-03-24 14:18:32 +0800 | [diff] [blame] | 787 | // Only append the sub pci range of a hot-pluggable root port to virtio-iommu |
| 788 | if slot_implemented { |
| 789 | hp_endpoints_ranges.push(RangeInclusive::new( |
| 790 | PciAddress { |
| 791 | bus: pci_bridge.get_secondary_num(), |
| 792 | dev: 0, |
| 793 | func: 0, |
| 794 | } |
| 795 | .to_u32(), |
| 796 | PciAddress { |
| 797 | bus: pci_bridge.get_subordinate_num(), |
| 798 | dev: 32, |
| 799 | func: 8, |
| 800 | } |
| 801 | .to_u32(), |
| 802 | )); |
| 803 | } |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 804 | |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 805 | devices.push((pci_bridge, None)); |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 806 | if slot_implemented { |
| 807 | if let Some(gpe) = host_pcie.hp_gpe { |
| 808 | gpe_notify_devs |
| 809 | .push((gpe, pcie_root_port.clone() as Arc<Mutex<dyn GpeNotify>>)); |
| 810 | } |
| 811 | hp_vec.push(pcie_root_port as Arc<Mutex<dyn HotPlugBus>>); |
| 812 | } |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | Ok(()) |
| 817 | } |
| 818 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 819 | fn setup_vm_components(cfg: &Config) -> Result<VmComponents> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 820 | let initrd_image = if let Some(initrd_path) = &cfg.initrd_path { |
Andrew Walbran | bc55e30 | 2021-07-13 17:35:10 +0100 | [diff] [blame] | 821 | Some( |
Daniel Verkamp | ed6b27a | 2022-03-25 14:06:05 -0700 | [diff] [blame] | 822 | open_file(initrd_path, OpenOptions::new().read(true)) |
| 823 | .with_context(|| format!("failed to open initrd {}", initrd_path.display()))?, |
Andrew Walbran | bc55e30 | 2021-07-13 17:35:10 +0100 | [diff] [blame] | 824 | ) |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 825 | } else { |
| 826 | None |
| 827 | }; |
| 828 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 829 | let vm_image = match cfg.executable_path { |
Andrew Walbran | bc55e30 | 2021-07-13 17:35:10 +0100 | [diff] [blame] | 830 | Some(Executable::Kernel(ref kernel_path)) => VmImage::Kernel( |
Daniel Verkamp | ed6b27a | 2022-03-25 14:06:05 -0700 | [diff] [blame] | 831 | open_file(kernel_path, OpenOptions::new().read(true)).with_context(|| { |
| 832 | format!("failed to open kernel image {}", kernel_path.display()) |
| 833 | })?, |
Andrew Walbran | bc55e30 | 2021-07-13 17:35:10 +0100 | [diff] [blame] | 834 | ), |
| 835 | Some(Executable::Bios(ref bios_path)) => VmImage::Bios( |
Daniel Verkamp | ed6b27a | 2022-03-25 14:06:05 -0700 | [diff] [blame] | 836 | open_file(bios_path, OpenOptions::new().read(true)) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 837 | .with_context(|| format!("failed to open bios {}", bios_path.display()))?, |
Andrew Walbran | bc55e30 | 2021-07-13 17:35:10 +0100 | [diff] [blame] | 838 | ), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 839 | _ => panic!("Did not receive a bios or kernel, should be impossible."), |
| 840 | }; |
| 841 | |
Will Deacon | c48e783 | 2021-07-30 19:03:06 +0100 | [diff] [blame] | 842 | let swiotlb = if let Some(size) = cfg.swiotlb { |
| 843 | Some( |
| 844 | size.checked_mul(1024 * 1024) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 845 | .ok_or_else(|| anyhow!("requested swiotlb size too large"))?, |
Will Deacon | c48e783 | 2021-07-30 19:03:06 +0100 | [diff] [blame] | 846 | ) |
| 847 | } else { |
| 848 | match cfg.protected_vm { |
Andrew Walbran | 0bbbb68 | 2021-12-13 13:42:07 +0000 | [diff] [blame] | 849 | ProtectionType::Protected | ProtectionType::ProtectedWithoutFirmware => { |
| 850 | Some(64 * 1024 * 1024) |
| 851 | } |
Will Deacon | c48e783 | 2021-07-30 19:03:06 +0100 | [diff] [blame] | 852 | ProtectionType::Unprotected => None, |
| 853 | } |
| 854 | }; |
| 855 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 856 | Ok(VmComponents { |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 857 | memory_size: cfg |
| 858 | .memory |
| 859 | .unwrap_or(256) |
| 860 | .checked_mul(1024 * 1024) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 861 | .ok_or_else(|| anyhow!("requested memory size too large"))?, |
Will Deacon | c48e783 | 2021-07-30 19:03:06 +0100 | [diff] [blame] | 862 | swiotlb, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 863 | vcpu_count: cfg.vcpu_count.unwrap_or(1), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 864 | vcpu_affinity: cfg.vcpu_affinity.clone(), |
Daniel Verkamp | 8a72afc | 2021-03-15 17:55:52 -0700 | [diff] [blame] | 865 | cpu_clusters: cfg.cpu_clusters.clone(), |
| 866 | cpu_capacity: cfg.cpu_capacity.clone(), |
Dmytro Maluka | 74031b4 | 2022-02-25 18:00:17 +0000 | [diff] [blame] | 867 | #[cfg(feature = "direct")] |
Dmytro Maluka | 6cea2c7 | 2022-02-25 18:22:17 +0000 | [diff] [blame] | 868 | direct_gpe: cfg.direct_gpe.clone(), |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 869 | no_smt: cfg.no_smt, |
Sergey Senozhatsky | 1e369c5 | 2021-04-13 20:23:51 +0900 | [diff] [blame] | 870 | hugepages: cfg.hugepages, |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 871 | vm_image, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 872 | android_fstab: cfg |
| 873 | .android_fstab |
| 874 | .as_ref() |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 875 | .map(|x| { |
| 876 | File::open(x) |
| 877 | .with_context(|| format!("failed to open android fstab file {}", x.display())) |
| 878 | }) |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 879 | .map_or(Ok(None), |v| v.map(Some))?, |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 880 | pstore: cfg.pstore.clone(), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 881 | initrd_image, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 882 | extra_kernel_params: cfg.params.clone(), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 883 | acpi_sdts: cfg |
| 884 | .acpi_tables |
| 885 | .iter() |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 886 | .map(|path| { |
| 887 | SDT::from_file(path) |
| 888 | .with_context(|| format!("failed to open ACPI file {}", path.display())) |
| 889 | }) |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 890 | .collect::<Result<Vec<SDT>>>()?, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 891 | rt_cpus: cfg.rt_cpus.clone(), |
Suleiman Souhlal | 63630e8 | 2021-02-18 11:53:11 +0900 | [diff] [blame] | 892 | delay_rt: cfg.delay_rt, |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 893 | protected_vm: cfg.protected_vm, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 894 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 895 | gdb: None, |
Tomasz Jeznach | ccb2694 | 2021-03-30 22:44:11 -0700 | [diff] [blame] | 896 | dmi_path: cfg.dmi_path.clone(), |
Tomasz Jeznach | d93c29f | 2021-04-12 11:00:24 -0700 | [diff] [blame] | 897 | no_legacy: cfg.no_legacy, |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 898 | host_cpu_topology: cfg.host_cpu_topology, |
Grzegorz Jaszczyk | d33874e | 2022-02-11 18:27:29 +0000 | [diff] [blame] | 899 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 900 | force_s2idle: cfg.force_s2idle, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 901 | }) |
| 902 | } |
| 903 | |
Andrew Walbran | b28ae8e | 2022-01-17 14:33:10 +0000 | [diff] [blame] | 904 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 905 | pub enum ExitState { |
| 906 | Reset, |
| 907 | Stop, |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 908 | Crash, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 909 | GuestPanic, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 912 | // Remove ranges in `guest_mem_layout` that overlap with ranges in `file_backed_mappings`. |
| 913 | // Returns the updated guest memory layout. |
| 914 | fn punch_holes_in_guest_mem_layout_for_mappings( |
| 915 | guest_mem_layout: Vec<(GuestAddress, u64)>, |
| 916 | file_backed_mappings: &[FileBackedMappingParameters], |
| 917 | ) -> Vec<(GuestAddress, u64)> { |
| 918 | // Create a set containing (start, end) pairs with exclusive end (end = start + size; the byte |
| 919 | // at end is not included in the range). |
| 920 | let mut layout_set = BTreeSet::new(); |
| 921 | for (addr, size) in &guest_mem_layout { |
| 922 | layout_set.insert((addr.offset(), addr.offset() + size)); |
| 923 | } |
| 924 | |
| 925 | for mapping in file_backed_mappings { |
| 926 | let mapping_start = mapping.address; |
| 927 | let mapping_end = mapping_start + mapping.size; |
| 928 | |
| 929 | // Repeatedly split overlapping guest memory regions until no overlaps remain. |
| 930 | while let Some((range_start, range_end)) = layout_set |
| 931 | .iter() |
| 932 | .find(|&&(range_start, range_end)| { |
| 933 | mapping_start < range_end && mapping_end > range_start |
| 934 | }) |
| 935 | .cloned() |
| 936 | { |
| 937 | layout_set.remove(&(range_start, range_end)); |
| 938 | |
| 939 | if range_start < mapping_start { |
| 940 | layout_set.insert((range_start, mapping_start)); |
| 941 | } |
| 942 | if range_end > mapping_end { |
| 943 | layout_set.insert((mapping_end, range_end)); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // Build the final guest memory layout from the modified layout_set. |
| 949 | layout_set |
| 950 | .iter() |
| 951 | .map(|(start, end)| (GuestAddress(*start), end - start)) |
| 952 | .collect() |
| 953 | } |
| 954 | |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 955 | pub fn run_config(cfg: Config) -> Result<ExitState> { |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 956 | let components = setup_vm_components(&cfg)?; |
| 957 | |
| 958 | let guest_mem_layout = |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 959 | Arch::guest_memory_layout(&components).context("failed to create guest memory layout")?; |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 960 | |
| 961 | let guest_mem_layout = |
| 962 | punch_holes_in_guest_mem_layout_for_mappings(guest_mem_layout, &cfg.file_backed_mappings); |
| 963 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 964 | 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] | 965 | let mut mem_policy = MemoryPolicy::empty(); |
| 966 | if components.hugepages { |
| 967 | mem_policy |= MemoryPolicy::USE_HUGEPAGES; |
| 968 | } |
Quentin Perret | 2620380 | 2021-12-02 09:48:43 +0000 | [diff] [blame] | 969 | guest_mem.set_memory_policy(mem_policy); |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 970 | 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] | 971 | let vm = KvmVm::new(&kvm, guest_mem, components.protected_vm).context("failed to create vm")?; |
Junichi Uekawa | b3a094e | 2022-03-29 15:41:47 +0900 | [diff] [blame] | 972 | |
| 973 | if !cfg.userspace_msr.is_empty() { |
| 974 | vm.enable_userspace_msr() |
| 975 | .context("failed to enable userspace MSR handling, do you have kernel 5.10 or later")?; |
| 976 | } |
| 977 | |
Andrew Walbran | e79aba1 | 2022-01-27 14:12:35 +0000 | [diff] [blame] | 978 | // Check that the VM was actually created in protected mode as expected. |
| 979 | if cfg.protected_vm != ProtectionType::Unprotected && !vm.check_capability(VmCap::Protected) { |
| 980 | bail!("Failed to create protected VM"); |
| 981 | } |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 982 | let vm_clone = vm.try_clone().context("failed to clone vm")?; |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 983 | |
| 984 | enum KvmIrqChip { |
| 985 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 986 | Split(KvmSplitIrqChip), |
| 987 | Kernel(KvmKernelIrqChip), |
| 988 | } |
| 989 | |
| 990 | impl KvmIrqChip { |
| 991 | fn as_mut(&mut self) -> &mut dyn IrqChipArch { |
| 992 | match self { |
| 993 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 994 | KvmIrqChip::Split(i) => i, |
| 995 | KvmIrqChip::Kernel(i) => i, |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | let ioapic_host_tube; |
| 1001 | let mut irq_chip = if cfg.split_irqchip { |
| 1002 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 1003 | unimplemented!("KVM split irqchip mode only supported on x86 processors"); |
| 1004 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1005 | { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1006 | 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] | 1007 | ioapic_host_tube = Some(host_tube); |
| 1008 | KvmIrqChip::Split( |
| 1009 | KvmSplitIrqChip::new( |
| 1010 | vm_clone, |
| 1011 | components.vcpu_count, |
| 1012 | ioapic_device_tube, |
| 1013 | Some(120), |
| 1014 | ) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1015 | .context("failed to create IRQ chip")?, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1016 | ) |
| 1017 | } |
| 1018 | } else { |
| 1019 | ioapic_host_tube = None; |
| 1020 | KvmIrqChip::Kernel( |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1021 | KvmKernelIrqChip::new(vm_clone, components.vcpu_count) |
| 1022 | .context("failed to create IRQ chip")?, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1023 | ) |
| 1024 | }; |
| 1025 | |
| 1026 | run_vm::<KvmVcpu, KvmVm>(cfg, components, vm, irq_chip.as_mut(), ioapic_host_tube) |
| 1027 | } |
| 1028 | |
| 1029 | fn run_vm<Vcpu, V>( |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1030 | cfg: Config, |
| 1031 | #[allow(unused_mut)] mut components: VmComponents, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1032 | mut vm: V, |
| 1033 | irq_chip: &mut dyn IrqChipArch, |
| 1034 | ioapic_host_tube: Option<Tube>, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1035 | ) -> Result<ExitState> |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1036 | where |
| 1037 | Vcpu: VcpuArch + 'static, |
| 1038 | V: VmArch + 'static, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1039 | { |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 1040 | if cfg.jail_config.is_some() { |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1041 | // Printing something to the syslog before entering minijail so that libc's syslogger has a |
| 1042 | // chance to open files necessary for its operation, like `/etc/localtime`. After jailing, |
| 1043 | // access to those files will not be possible. |
| 1044 | info!("crosvm entering multiprocess mode"); |
| 1045 | } |
| 1046 | |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1047 | #[cfg(feature = "usb")] |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1048 | let (usb_control_tube, usb_provider) = |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1049 | HostBackendDeviceProvider::new().context("failed to create usb provider")?; |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1050 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1051 | // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this |
| 1052 | // before any jailed devices have been spawned, so that we can catch any of them that fail very |
| 1053 | // quickly. |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1054 | let sigchld_fd = SignalFd::new(libc::SIGCHLD).context("failed to create signalfd")?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1055 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1056 | let control_server_socket = match &cfg.socket_path { |
| 1057 | Some(path) => Some(UnlinkUnixSeqpacketListener( |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1058 | UnixSeqpacketListener::bind(path).context("failed to create control server")?, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1059 | )), |
| 1060 | None => None, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1061 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1062 | |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1063 | let mut control_tubes = Vec::new(); |
| 1064 | |
| 1065 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1066 | if let Some(port) = cfg.gdb { |
| 1067 | // GDB needs a control socket to interrupt vcpus. |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1068 | 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] | 1069 | control_tubes.push(TaggedControlTube::Vm(gdb_host_tube)); |
| 1070 | components.gdb = Some((port, gdb_control_tube)); |
| 1071 | } |
| 1072 | |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 1073 | for wl_cfg in &cfg.vhost_user_wl { |
| 1074 | let wayland_host_tube = UnixSeqpacket::connect(&wl_cfg.vm_tube) |
| 1075 | .map(Tube::new) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1076 | .context("failed to connect to wayland tube")?; |
Chirantan Ekbote | 2ee9dcd | 2021-05-26 18:21:44 +0900 | [diff] [blame] | 1077 | control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube)); |
| 1078 | } |
| 1079 | |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 1080 | let mut vhost_user_gpu_tubes = Vec::with_capacity(cfg.vhost_user_gpu.len()); |
| 1081 | for _ in 0..cfg.vhost_user_gpu.len() { |
Alexandre Courbot | e55b791 | 2022-03-04 16:54:38 +0900 | [diff] [blame] | 1082 | let (host_control_tube, device_control_tube) = |
| 1083 | Tube::pair().context("failed to create tube")?; |
| 1084 | let (host_gpu_tube, device_gpu_tube) = Tube::pair().context("failed to create tube")?; |
| 1085 | vhost_user_gpu_tubes.push((host_gpu_tube, device_gpu_tube, device_control_tube)); |
| 1086 | control_tubes.push(TaggedControlTube::VmMemory(host_control_tube)); |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 1087 | } |
| 1088 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1089 | 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] | 1090 | control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube)); |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 1091 | |
| 1092 | let (balloon_host_tube, balloon_device_tube) = if cfg.balloon { |
David Stevens | 8be9ef0 | 2022-01-13 22:50:24 +0900 | [diff] [blame] | 1093 | if let Some(ref path) = cfg.balloon_control { |
| 1094 | ( |
| 1095 | None, |
| 1096 | Some(Tube::new( |
| 1097 | UnixSeqpacket::connect(path).context("failed to create balloon control")?, |
| 1098 | )), |
| 1099 | ) |
| 1100 | } else { |
| 1101 | // Balloon gets a special socket so balloon requests can be forwarded |
| 1102 | // from the main process. |
| 1103 | let (host, device) = Tube::pair().context("failed to create tube")?; |
| 1104 | // Set recv timeout to avoid deadlock on sending BalloonControlCommand |
| 1105 | // before the guest is ready. |
| 1106 | host.set_recv_timeout(Some(Duration::from_millis(100))) |
| 1107 | .context("failed to set timeout")?; |
| 1108 | (Some(host), Some(device)) |
| 1109 | } |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 1110 | } else { |
| 1111 | (None, None) |
| 1112 | }; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1113 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1114 | // Create one control socket per disk. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1115 | let mut disk_device_tubes = Vec::new(); |
| 1116 | let mut disk_host_tubes = Vec::new(); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1117 | let disk_count = cfg.disks.len(); |
| 1118 | for _ in 0..disk_count { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1119 | 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] | 1120 | disk_host_tubes.push(disk_host_tub); |
| 1121 | disk_device_tubes.push(disk_device_tube); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1124 | let mut pmem_device_tubes = Vec::new(); |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1125 | let pmem_count = cfg.pmem_devices.len(); |
| 1126 | for _ in 0..pmem_count { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1127 | 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] | 1128 | pmem_device_tubes.push(pmem_device_tube); |
| 1129 | control_tubes.push(TaggedControlTube::VmMsync(pmem_host_tube)); |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1132 | 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] | 1133 | control_tubes.push(TaggedControlTube::VmMemory(gpu_host_tube)); |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1134 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1135 | if let Some(ioapic_host_tube) = ioapic_host_tube { |
| 1136 | control_tubes.push(TaggedControlTube::VmIrq(ioapic_host_tube)); |
| 1137 | } |
Zhuocheng Ding | f2e90bf | 2019-12-02 15:50:20 +0800 | [diff] [blame] | 1138 | |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 1139 | let battery = if cfg.battery_type.is_some() { |
Daniel Verkamp | cfe4946 | 2021-08-19 17:11:05 -0700 | [diff] [blame] | 1140 | #[cfg_attr(not(feature = "power-monitor-powerd"), allow(clippy::manual_map))] |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 1141 | let jail = match simple_jail(&cfg.jail_config, "battery")? { |
Daniel Verkamp | cfe4946 | 2021-08-19 17:11:05 -0700 | [diff] [blame] | 1142 | #[cfg_attr(not(feature = "power-monitor-powerd"), allow(unused_mut))] |
Alex Lau | f408c73 | 2020-11-10 18:24:04 +0900 | [diff] [blame] | 1143 | Some(mut jail) => { |
| 1144 | // Setup a bind mount to the system D-Bus socket if the powerd monitor is used. |
| 1145 | #[cfg(feature = "power-monitor-powerd")] |
| 1146 | { |
Fergus Dall | 5120051 | 2021-08-19 12:54:26 +1000 | [diff] [blame] | 1147 | add_current_user_to_jail(&mut jail)?; |
Alex Lau | f408c73 | 2020-11-10 18:24:04 +0900 | [diff] [blame] | 1148 | |
| 1149 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 1150 | jail.mount_with_data( |
| 1151 | Path::new("none"), |
| 1152 | Path::new("/"), |
| 1153 | "tmpfs", |
| 1154 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 1155 | "size=67108864", |
| 1156 | )?; |
| 1157 | |
| 1158 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 1159 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 1160 | } |
| 1161 | Some(jail) |
| 1162 | } |
| 1163 | None => None, |
| 1164 | }; |
| 1165 | (&cfg.battery_type, jail) |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 1166 | } else { |
| 1167 | (&cfg.battery_type, None) |
| 1168 | }; |
| 1169 | |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1170 | let map_request: Arc<Mutex<Option<ExternalMapping>>> = Arc::new(Mutex::new(None)); |
| 1171 | |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1172 | let fs_count = cfg |
| 1173 | .shared_dirs |
| 1174 | .iter() |
| 1175 | .filter(|sd| sd.kind == SharedDirKind::FS) |
| 1176 | .count(); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1177 | let mut fs_device_tubes = Vec::with_capacity(fs_count); |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1178 | for _ in 0..fs_count { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1179 | 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] | 1180 | control_tubes.push(TaggedControlTube::Fs(fs_host_tube)); |
| 1181 | fs_device_tubes.push(fs_device_tube); |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1182 | } |
| 1183 | |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 1184 | let mut vvu_proxy_device_tubes = Vec::new(); |
| 1185 | for _ in 0..cfg.vvu_proxy.len() { |
| 1186 | let (vvu_proxy_host_tube, vvu_proxy_device_tube) = |
| 1187 | Tube::pair().context("failed to create VVU proxy tube")?; |
| 1188 | control_tubes.push(TaggedControlTube::VmMemory(vvu_proxy_host_tube)); |
| 1189 | vvu_proxy_device_tubes.push(vvu_proxy_device_tube); |
| 1190 | } |
| 1191 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1192 | let exit_evt = Event::new().context("failed to create event")?; |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1193 | let reset_evt = Event::new().context("failed to create event")?; |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1194 | let crash_evt = Event::new().context("failed to create event")?; |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1195 | let (panic_rdtube, panic_wrtube) = Tube::pair().context("failed to create tube")?; |
Ryo Hashimoto | 8f9dc1d | 2021-08-18 19:07:29 +0900 | [diff] [blame] | 1196 | |
David Stevens | e4db417 | 2022-03-10 13:26:04 +0900 | [diff] [blame] | 1197 | let pstore_size = components.pstore.as_ref().map(|pstore| pstore.size as u64); |
David Stevens | dbd2418 | 2022-03-10 10:53:56 +0900 | [diff] [blame] | 1198 | let mut sys_allocator = SystemAllocator::new( |
| 1199 | Arch::get_system_allocator_config(&vm), |
| 1200 | pstore_size, |
| 1201 | &cfg.mmio_address_ranges, |
| 1202 | ) |
| 1203 | .context("failed to create system allocator")?; |
David Stevens | e4db417 | 2022-03-10 13:26:04 +0900 | [diff] [blame] | 1204 | |
Ryo Hashimoto | 8f9dc1d | 2021-08-18 19:07:29 +0900 | [diff] [blame] | 1205 | let ramoops_region = match &components.pstore { |
| 1206 | Some(pstore) => Some( |
David Stevens | e4db417 | 2022-03-10 13:26:04 +0900 | [diff] [blame] | 1207 | arch::pstore::create_memory_region( |
| 1208 | &mut vm, |
| 1209 | sys_allocator.reserved_region().unwrap(), |
| 1210 | pstore, |
| 1211 | ) |
| 1212 | .context("failed to allocate pstore region")?, |
Ryo Hashimoto | 8f9dc1d | 2021-08-18 19:07:29 +0900 | [diff] [blame] | 1213 | ), |
| 1214 | None => None, |
| 1215 | }; |
| 1216 | |
Mattias Nissler | bbd91d0 | 2021-12-07 08:57:45 +0000 | [diff] [blame] | 1217 | create_file_backed_mappings(&cfg, &mut vm, &mut sys_allocator)?; |
| 1218 | |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 1219 | #[cfg(feature = "gpu")] |
| 1220 | // Hold on to the render server jail so it keeps running until we exit run_vm() |
Dmitry Torokhov | e464a7a | 2022-01-26 13:29:36 -0800 | [diff] [blame] | 1221 | let (_render_server_jail, render_server_fd) = |
| 1222 | if let Some(parameters) = &cfg.gpu_render_server_parameters { |
| 1223 | let (jail, fd) = start_gpu_render_server(&cfg, parameters)?; |
| 1224 | (Some(ScopedMinijail(jail)), Some(fd)) |
| 1225 | } else { |
| 1226 | (None, None) |
| 1227 | }; |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 1228 | |
David Stevens | 06d157a | 2022-01-13 23:44:48 +0900 | [diff] [blame] | 1229 | let init_balloon_size = components |
| 1230 | .memory_size |
| 1231 | .checked_sub(cfg.init_memory.map_or(components.memory_size, |m| { |
| 1232 | m.checked_mul(1024 * 1024).unwrap_or(u64::MAX) |
| 1233 | })) |
| 1234 | .context("failed to calculate init balloon size")?; |
| 1235 | |
Tomasz Nowicki | 64f4355 | 2022-02-22 14:14:45 +0000 | [diff] [blame] | 1236 | #[cfg(feature = "direct")] |
| 1237 | let mut irqs = Vec::new(); |
| 1238 | |
| 1239 | #[cfg(feature = "direct")] |
| 1240 | for irq in &cfg.direct_level_irq { |
| 1241 | if !sys_allocator.reserve_irq(*irq) { |
| 1242 | warn!("irq {} already reserved.", irq); |
| 1243 | } |
Dmitry Torokhov | 7b7a206 | 2022-03-19 23:01:33 -0700 | [diff] [blame] | 1244 | let irq_evt = devices::IrqLevelEvent::new().context("failed to create event")?; |
Dmitry Torokhov | f021a62 | 2022-03-21 11:00:29 -0700 | [diff] [blame] | 1245 | irq_chip.register_level_irq_event(*irq, &irq_evt).unwrap(); |
Dmitry Torokhov | 7b7a206 | 2022-03-19 23:01:33 -0700 | [diff] [blame] | 1246 | let direct_irq = devices::DirectIrq::new_level(&irq_evt) |
Tomasz Nowicki | 64f4355 | 2022-02-22 14:14:45 +0000 | [diff] [blame] | 1247 | .context("failed to enable interrupt forwarding")?; |
| 1248 | direct_irq |
| 1249 | .irq_enable(*irq) |
| 1250 | .context("failed to enable interrupt forwarding")?; |
Dmytro Maluka | 0ac9797 | 2022-04-05 15:25:46 +0000 | [diff] [blame] | 1251 | |
| 1252 | if cfg.direct_wake_irq.contains(&irq) { |
| 1253 | direct_irq |
| 1254 | .irq_wake_enable(*irq) |
| 1255 | .context("failed to enable interrupt wake")?; |
| 1256 | } |
| 1257 | |
Tomasz Nowicki | 64f4355 | 2022-02-22 14:14:45 +0000 | [diff] [blame] | 1258 | irqs.push(direct_irq); |
| 1259 | } |
| 1260 | |
| 1261 | #[cfg(feature = "direct")] |
| 1262 | for irq in &cfg.direct_edge_irq { |
| 1263 | if !sys_allocator.reserve_irq(*irq) { |
| 1264 | warn!("irq {} already reserved.", irq); |
| 1265 | } |
Dmitry Torokhov | 7b7a206 | 2022-03-19 23:01:33 -0700 | [diff] [blame] | 1266 | let irq_evt = devices::IrqEdgeEvent::new().context("failed to create event")?; |
Dmitry Torokhov | f021a62 | 2022-03-21 11:00:29 -0700 | [diff] [blame] | 1267 | irq_chip.register_edge_irq_event(*irq, &irq_evt).unwrap(); |
Dmitry Torokhov | 7b7a206 | 2022-03-19 23:01:33 -0700 | [diff] [blame] | 1268 | let direct_irq = devices::DirectIrq::new_edge(&irq_evt) |
Tomasz Nowicki | 64f4355 | 2022-02-22 14:14:45 +0000 | [diff] [blame] | 1269 | .context("failed to enable interrupt forwarding")?; |
| 1270 | direct_irq |
| 1271 | .irq_enable(*irq) |
| 1272 | .context("failed to enable interrupt forwarding")?; |
Dmytro Maluka | 0ac9797 | 2022-04-05 15:25:46 +0000 | [diff] [blame] | 1273 | |
| 1274 | if cfg.direct_wake_irq.contains(&irq) { |
| 1275 | direct_irq |
| 1276 | .irq_wake_enable(*irq) |
| 1277 | .context("failed to enable interrupt wake")?; |
| 1278 | } |
| 1279 | |
Tomasz Nowicki | 64f4355 | 2022-02-22 14:14:45 +0000 | [diff] [blame] | 1280 | irqs.push(direct_irq); |
| 1281 | } |
| 1282 | |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1283 | let mut iommu_attached_endpoints: BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>> = |
| 1284 | BTreeMap::new(); |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 1285 | let mut devices = create_devices( |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1286 | &cfg, |
| 1287 | &mut vm, |
| 1288 | &mut sys_allocator, |
| 1289 | &exit_evt, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1290 | panic_wrtube, |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1291 | &mut iommu_attached_endpoints, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1292 | &mut control_tubes, |
| 1293 | wayland_device_tube, |
| 1294 | gpu_device_tube, |
Chirantan Ekbote | 44292f5 | 2021-06-25 18:31:41 +0900 | [diff] [blame] | 1295 | vhost_user_gpu_tubes, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1296 | balloon_device_tube, |
David Stevens | 06d157a | 2022-01-13 23:44:48 +0900 | [diff] [blame] | 1297 | init_balloon_size, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1298 | &mut disk_device_tubes, |
| 1299 | &mut pmem_device_tubes, |
| 1300 | &mut fs_device_tubes, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1301 | #[cfg(feature = "usb")] |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1302 | usb_provider, |
| 1303 | Arc::clone(&map_request), |
Dmitry Torokhov | 9cbe543 | 2022-01-25 19:17:07 -0800 | [diff] [blame] | 1304 | #[cfg(feature = "gpu")] |
| 1305 | render_server_fd, |
Abhishek Bhardwaj | 90fd164 | 2021-11-24 18:26:37 -0800 | [diff] [blame] | 1306 | &mut vvu_proxy_device_tubes, |
David Stevens | e123e39 | 2022-04-07 09:59:25 +0900 | [diff] [blame] | 1307 | components.memory_size, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1308 | )?; |
| 1309 | |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1310 | let mut hp_endpoints_ranges: Vec<RangeInclusive<u32>> = Vec::new(); |
Anton Romanov | 3333441 | 2022-03-22 17:48:18 +0000 | [diff] [blame] | 1311 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 1312 | let mut hotplug_buses: Vec<Arc<Mutex<dyn HotPlugBus>>> = Vec::new(); |
Xiong Zhang | 1b6e011 | 2022-03-15 11:34:16 +0800 | [diff] [blame] | 1313 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1314 | let mut gpe_notify_devs: Vec<(u32, Arc<Mutex<dyn GpeNotify>>)> = Vec::new(); |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 1315 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1316 | { |
| 1317 | #[cfg(feature = "direct")] |
| 1318 | let rp_host = cfg.pcie_rp.clone(); |
| 1319 | #[cfg(not(feature = "direct"))] |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 1320 | let rp_host: Vec<HostPcieRootPortParameters> = Vec::new(); |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 1321 | |
| 1322 | // Create Pcie Root Port |
| 1323 | create_pcie_root_port( |
| 1324 | rp_host, |
| 1325 | &mut sys_allocator, |
| 1326 | &mut control_tubes, |
| 1327 | &mut devices, |
| 1328 | &mut hotplug_buses, |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1329 | &mut hp_endpoints_ranges, |
Xiong Zhang | 1b6e011 | 2022-03-15 11:34:16 +0800 | [diff] [blame] | 1330 | &mut gpe_notify_devs, |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 1331 | )?; |
| 1332 | } |
| 1333 | |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1334 | let (translate_response_senders, request_rx) = setup_virtio_access_platform( |
| 1335 | &mut sys_allocator, |
| 1336 | &mut iommu_attached_endpoints, |
| 1337 | &mut devices, |
| 1338 | )?; |
| 1339 | |
Haiwei Li | 87bc2fc | 2022-02-18 14:37:40 +0800 | [diff] [blame] | 1340 | let iommu_host_tube = if !iommu_attached_endpoints.is_empty() || cfg.virtio_iommu { |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1341 | let (iommu_host_tube, iommu_device_tube) = Tube::pair().context("failed to create tube")?; |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1342 | let iommu_dev = create_iommu_device( |
| 1343 | &cfg, |
| 1344 | (1u64 << vm.get_guest_phys_addr_bits()) - 1, |
| 1345 | iommu_attached_endpoints, |
| 1346 | hp_endpoints_ranges, |
| 1347 | translate_response_senders, |
| 1348 | request_rx, |
| 1349 | iommu_device_tube, |
| 1350 | )?; |
| 1351 | |
| 1352 | let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?; |
| 1353 | control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube)); |
| 1354 | let mut dev = VirtioPciDevice::new(vm.get_memory().clone(), iommu_dev.dev, msi_device_tube) |
| 1355 | .context("failed to create virtio pci dev")?; |
| 1356 | // early reservation for viommu. |
| 1357 | dev.allocate_address(&mut sys_allocator) |
| 1358 | .context("failed to allocate resources early for virtio pci dev")?; |
| 1359 | let dev = Box::new(dev); |
| 1360 | devices.push((dev, iommu_dev.jail)); |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1361 | Some(iommu_host_tube) |
| 1362 | } else { |
| 1363 | None |
| 1364 | }; |
Haiwei Li | 09b7b8e | 2022-02-18 18:16:05 +0800 | [diff] [blame] | 1365 | |
Peter Fang | c2bba08 | 2021-04-19 18:40:24 -0700 | [diff] [blame] | 1366 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 1367 | for device in devices |
| 1368 | .iter_mut() |
| 1369 | .filter_map(|(dev, _)| dev.as_pci_device_mut()) |
| 1370 | { |
Peter Fang | c2bba08 | 2021-04-19 18:40:24 -0700 | [diff] [blame] | 1371 | let sdts = device |
| 1372 | .generate_acpi(components.acpi_sdts) |
| 1373 | .or_else(|| { |
| 1374 | error!("ACPI table generation error"); |
| 1375 | None |
| 1376 | }) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1377 | .ok_or_else(|| anyhow!("failed to generate ACPI table"))?; |
Peter Fang | c2bba08 | 2021-04-19 18:40:24 -0700 | [diff] [blame] | 1378 | components.acpi_sdts = sdts; |
| 1379 | } |
| 1380 | |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 1381 | // KVM_CREATE_VCPU uses apic id for x86 and uses cpu id for others. |
| 1382 | let mut kvm_vcpu_ids = Vec::new(); |
| 1383 | |
Kuo-Hsin Yang | 6139da6 | 2021-04-14 16:55:24 +0800 | [diff] [blame] | 1384 | #[cfg_attr(not(feature = "direct"), allow(unused_mut))] |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1385 | let mut linux = Arch::build_vm::<V, Vcpu>( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1386 | components, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1387 | &exit_evt, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1388 | &reset_evt, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1389 | &mut sys_allocator, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1390 | &cfg.serial_parameters, |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 1391 | simple_jail(&cfg.jail_config, "serial")?, |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 1392 | battery, |
Zach Reizner | a90649a | 2021-03-31 12:56:08 -0700 | [diff] [blame] | 1393 | vm, |
Ryo Hashimoto | 8f9dc1d | 2021-08-18 19:07:29 +0900 | [diff] [blame] | 1394 | ramoops_region, |
Tomasz Nowicki | ab86d52 | 2021-09-22 05:50:46 +0000 | [diff] [blame] | 1395 | devices, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1396 | irq_chip, |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 1397 | &mut kvm_vcpu_ids, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1398 | ) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1399 | .context("the architecture failed to build the vm")?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 1400 | |
Daniel Verkamp | 1286b48 | 2021-11-30 15:14:16 -0800 | [diff] [blame] | 1401 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1402 | { |
Xiong Zhang | f787471 | 2021-12-24 10:53:59 +0800 | [diff] [blame] | 1403 | for hotplug_bus in hotplug_buses.iter() { |
| 1404 | linux.hotplug_bus.push(hotplug_bus.clone()); |
| 1405 | } |
Xiong Zhang | 1b6e011 | 2022-03-15 11:34:16 +0800 | [diff] [blame] | 1406 | |
| 1407 | if let Some(pm) = &linux.pm { |
| 1408 | while let Some((gpe, notify_dev)) = gpe_notify_devs.pop() { |
| 1409 | pm.lock().register_gpe_notify_dev(gpe, notify_dev); |
| 1410 | } |
| 1411 | } |
Daniel Verkamp | 1286b48 | 2021-11-30 15:14:16 -0800 | [diff] [blame] | 1412 | } |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1413 | |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 1414 | #[cfg(feature = "direct")] |
| 1415 | if let Some(pmio) = &cfg.direct_pmio { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1416 | let direct_io = Arc::new( |
| 1417 | devices::DirectIo::new(&pmio.path, false).context("failed to open direct io device")?, |
| 1418 | ); |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 1419 | for range in pmio.ranges.iter() { |
| 1420 | linux |
| 1421 | .io_bus |
Junichi Uekawa | b180f9c | 2021-12-07 09:21:36 +0900 | [diff] [blame] | 1422 | .insert_sync(direct_io.clone(), range.base, range.len) |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame] | 1423 | .unwrap(); |
| 1424 | } |
| 1425 | }; |
| 1426 | |
Tomasz Jeznach | 7271f75 | 2021-03-04 01:44:06 -0800 | [diff] [blame] | 1427 | #[cfg(feature = "direct")] |
Tomasz Jeznach | 9e6c633 | 2021-05-27 21:49:14 -0700 | [diff] [blame] | 1428 | if let Some(mmio) = &cfg.direct_mmio { |
Xiong Zhang | 46471a0 | 2021-11-12 00:34:42 +0800 | [diff] [blame] | 1429 | let direct_mmio = Arc::new( |
Junichi Uekawa | b180f9c | 2021-12-07 09:21:36 +0900 | [diff] [blame] | 1430 | devices::DirectMmio::new(&mmio.path, false, &mmio.ranges) |
Xiong Zhang | 46471a0 | 2021-11-12 00:34:42 +0800 | [diff] [blame] | 1431 | .context("failed to open direct mmio device")?, |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1432 | ); |
Xiong Zhang | 46471a0 | 2021-11-12 00:34:42 +0800 | [diff] [blame] | 1433 | |
Tomasz Jeznach | 9e6c633 | 2021-05-27 21:49:14 -0700 | [diff] [blame] | 1434 | for range in mmio.ranges.iter() { |
| 1435 | linux |
| 1436 | .mmio_bus |
Junichi Uekawa | b180f9c | 2021-12-07 09:21:36 +0900 | [diff] [blame] | 1437 | .insert_sync(direct_mmio.clone(), range.base, range.len) |
Tomasz Jeznach | 9e6c633 | 2021-05-27 21:49:14 -0700 | [diff] [blame] | 1438 | .unwrap(); |
| 1439 | } |
| 1440 | }; |
| 1441 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1442 | let gralloc = RutabagaGralloc::new().context("failed to create gralloc")?; |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1443 | run_control( |
| 1444 | linux, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1445 | sys_allocator, |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1446 | cfg, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1447 | control_server_socket, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1448 | control_tubes, |
| 1449 | balloon_host_tube, |
| 1450 | &disk_host_tubes, |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1451 | #[cfg(feature = "usb")] |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1452 | usb_control_tube, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1453 | exit_evt, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1454 | reset_evt, |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1455 | crash_evt, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1456 | panic_rdtube, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1457 | sigchld_fd, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1458 | Arc::clone(&map_request), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 1459 | gralloc, |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 1460 | kvm_vcpu_ids, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1461 | iommu_host_tube, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1462 | ) |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1465 | fn get_hp_bus<V: VmArch, Vcpu: VcpuArch>( |
| 1466 | linux: &RunnableLinuxVm<V, Vcpu>, |
| 1467 | host_addr: PciAddress, |
| 1468 | ) -> Result<(Arc<Mutex<dyn HotPlugBus>>, u8)> { |
| 1469 | for hp_bus in linux.hotplug_bus.iter() { |
| 1470 | if let Some(number) = hp_bus.lock().is_match(host_addr) { |
| 1471 | return Ok((hp_bus.clone(), number)); |
| 1472 | } |
| 1473 | } |
| 1474 | Err(anyhow!("Failed to find a suitable hotplug bus")) |
| 1475 | } |
| 1476 | |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1477 | fn add_vfio_device<V: VmArch, Vcpu: VcpuArch>( |
| 1478 | linux: &mut RunnableLinuxVm<V, Vcpu>, |
| 1479 | sys_allocator: &mut SystemAllocator, |
| 1480 | cfg: &Config, |
| 1481 | control_tubes: &mut Vec<TaggedControlTube>, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1482 | iommu_host_tube: &Option<Tube>, |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1483 | vfio_path: &Path, |
| 1484 | ) -> Result<()> { |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1485 | let host_os_str = vfio_path |
| 1486 | .file_name() |
| 1487 | .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?; |
| 1488 | let host_str = host_os_str |
| 1489 | .to_str() |
| 1490 | .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?; |
Daniel Verkamp | aebda74 | 2022-04-07 16:37:27 -0700 | [diff] [blame] | 1491 | let host_addr = PciAddress::from_str(host_str).context("failed to parse vfio pci address")?; |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1492 | |
| 1493 | let (hp_bus, bus_num) = get_hp_bus(linux, host_addr)?; |
| 1494 | |
Woody Chow | 055b81b | 2022-01-25 18:34:29 +0900 | [diff] [blame] | 1495 | let mut endpoints: BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>> = BTreeMap::new(); |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1496 | let (vfio_pci_device, jail) = create_vfio_device( |
| 1497 | cfg, |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1498 | &linux.vm, |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1499 | sys_allocator, |
| 1500 | control_tubes, |
| 1501 | vfio_path, |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1502 | Some(bus_num), |
Victor Ding | 3f74959 | 2022-03-18 05:44:20 +0000 | [diff] [blame] | 1503 | None, |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1504 | &mut endpoints, |
Chuanxiao Dong | a8d427b | 2022-01-07 10:26:24 +0800 | [diff] [blame] | 1505 | None, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1506 | if iommu_host_tube.is_some() { |
| 1507 | IommuDevType::VirtioIommu |
| 1508 | } else { |
| 1509 | IommuDevType::NoIommu |
| 1510 | }, |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1511 | )?; |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1512 | |
| 1513 | 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] | 1514 | .context("Failed to configure pci hotplug device")?; |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1515 | |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1516 | if let Some(iommu_host_tube) = iommu_host_tube { |
| 1517 | let &endpoint_addr = endpoints.iter().next().unwrap().0; |
| 1518 | let mapper = endpoints.remove(&endpoint_addr).unwrap(); |
| 1519 | if let Some(vfio_wrapper) = mapper.lock().as_vfio_wrapper() { |
| 1520 | let vfio_container = vfio_wrapper.as_vfio_container(); |
| 1521 | let descriptor = vfio_container.lock().into_raw_descriptor()?; |
| 1522 | let request = VirtioIOMMURequest::VfioCommand(VirtioIOMMUVfioCommand::VfioDeviceAdd { |
| 1523 | endpoint_addr, |
| 1524 | container: { |
| 1525 | // Safe because the descriptor is uniquely owned by `descriptor`. |
| 1526 | unsafe { File::from_raw_descriptor(descriptor) } |
| 1527 | }, |
| 1528 | }); |
| 1529 | |
| 1530 | match virtio_iommu_request(iommu_host_tube, &request) |
| 1531 | .map_err(|_| VirtioIOMMUVfioError::SocketFailed)? |
| 1532 | { |
| 1533 | VirtioIOMMUResponse::VfioResponse(VirtioIOMMUVfioResult::Ok) => (), |
| 1534 | resp => bail!("Unexpected message response: {:?}", resp), |
| 1535 | } |
| 1536 | }; |
| 1537 | } |
| 1538 | |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1539 | let host_key = HostHotPlugKey::Vfio { host_addr }; |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1540 | let mut hp_bus = hp_bus.lock(); |
| 1541 | hp_bus.add_hotplug_device(host_key, pci_address); |
| 1542 | hp_bus.hot_plug(pci_address); |
| 1543 | Ok(()) |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1546 | fn remove_vfio_device<V: VmArch, Vcpu: VcpuArch>( |
| 1547 | linux: &RunnableLinuxVm<V, Vcpu>, |
Xiong Zhang | 2d45b91 | 2021-05-13 16:22:25 +0800 | [diff] [blame] | 1548 | sys_allocator: &mut SystemAllocator, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1549 | iommu_host_tube: &Option<Tube>, |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1550 | vfio_path: &Path, |
| 1551 | ) -> Result<()> { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1552 | let host_os_str = vfio_path |
| 1553 | .file_name() |
| 1554 | .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?; |
| 1555 | let host_str = host_os_str |
| 1556 | .to_str() |
| 1557 | .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?; |
Daniel Verkamp | aebda74 | 2022-04-07 16:37:27 -0700 | [diff] [blame] | 1558 | let host_addr = PciAddress::from_str(host_str).context("failed to parse vfio pci address")?; |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1559 | let host_key = HostHotPlugKey::Vfio { host_addr }; |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1560 | for hp_bus in linux.hotplug_bus.iter() { |
| 1561 | let mut hp_bus_lock = hp_bus.lock(); |
| 1562 | if let Some(pci_addr) = hp_bus_lock.get_hotplug_device(host_key) { |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1563 | if let Some(iommu_host_tube) = iommu_host_tube { |
| 1564 | let request = |
| 1565 | VirtioIOMMURequest::VfioCommand(VirtioIOMMUVfioCommand::VfioDeviceDel { |
| 1566 | endpoint_addr: pci_addr.to_u32(), |
| 1567 | }); |
| 1568 | match virtio_iommu_request(iommu_host_tube, &request) |
| 1569 | .map_err(|_| VirtioIOMMUVfioError::SocketFailed)? |
| 1570 | { |
| 1571 | VirtioIOMMUResponse::VfioResponse(VirtioIOMMUVfioResult::Ok) => (), |
| 1572 | resp => bail!("Unexpected message response: {:?}", resp), |
| 1573 | } |
| 1574 | } |
| 1575 | |
Xiong Zhang | f82f2dc | 2021-05-21 16:54:12 +0800 | [diff] [blame] | 1576 | hp_bus_lock.hot_unplug(pci_addr); |
Xiong Zhang | 2d45b91 | 2021-05-13 16:22:25 +0800 | [diff] [blame] | 1577 | 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] | 1578 | return Ok(()); |
| 1579 | } |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1582 | Err(anyhow!("HotPlugBus hasn't been implemented")) |
Xiong Zhang | 9fadc3f | 2021-06-07 14:16:45 +0800 | [diff] [blame] | 1583 | } |
Xiong Zhang | 8c9fe3e | 2021-04-12 15:07:17 +0800 | [diff] [blame] | 1584 | |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1585 | fn handle_vfio_command<V: VmArch, Vcpu: VcpuArch>( |
| 1586 | linux: &mut RunnableLinuxVm<V, Vcpu>, |
| 1587 | sys_allocator: &mut SystemAllocator, |
| 1588 | cfg: &Config, |
| 1589 | add_tubes: &mut Vec<TaggedControlTube>, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1590 | iommu_host_tube: &Option<Tube>, |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1591 | vfio_path: &Path, |
| 1592 | add: bool, |
| 1593 | ) -> VmResponse { |
| 1594 | let ret = if add { |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1595 | add_vfio_device( |
| 1596 | linux, |
| 1597 | sys_allocator, |
| 1598 | cfg, |
| 1599 | add_tubes, |
| 1600 | iommu_host_tube, |
| 1601 | vfio_path, |
| 1602 | ) |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1603 | } else { |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1604 | remove_vfio_device(linux, sys_allocator, iommu_host_tube, vfio_path) |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1605 | }; |
| 1606 | |
| 1607 | match ret { |
| 1608 | Ok(()) => VmResponse::Ok, |
| 1609 | Err(e) => { |
| 1610 | error!("hanlde_vfio_command failure: {}", e); |
| 1611 | add_tubes.clear(); |
| 1612 | VmResponse::Err(base::Error::new(libc::EINVAL)) |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1617 | fn run_control<V: VmArch + 'static, Vcpu: VcpuArch + 'static>( |
| 1618 | mut linux: RunnableLinuxVm<V, Vcpu>, |
| 1619 | mut sys_allocator: SystemAllocator, |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1620 | cfg: Config, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1621 | control_server_socket: Option<UnlinkUnixSeqpacketListener>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1622 | mut control_tubes: Vec<TaggedControlTube>, |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 1623 | balloon_host_tube: Option<Tube>, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1624 | disk_host_tubes: &[Tube], |
Daniel Verkamp | f1439d4 | 2021-05-21 13:55:10 -0700 | [diff] [blame] | 1625 | #[cfg(feature = "usb")] usb_control_tube: Tube, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1626 | exit_evt: Event, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1627 | reset_evt: Event, |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1628 | crash_evt: Event, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1629 | panic_rdtube: Tube, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1630 | sigchld_fd: SignalFd, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1631 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 1632 | mut gralloc: RutabagaGralloc, |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 1633 | kvm_vcpu_ids: Vec<usize>, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1634 | iommu_host_tube: Option<Tube>, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1635 | ) -> Result<ExitState> { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1636 | #[derive(PollToken)] |
| 1637 | enum Token { |
| 1638 | Exit, |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1639 | Reset, |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1640 | Crash, |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1641 | Panic, |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 1642 | Suspend, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1643 | ChildSignal, |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 1644 | IrqFd { index: IrqEventIndex }, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1645 | VmControlServer, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1646 | VmControl { index: usize }, |
Tomasz Nowicki | 9880100 | 2022-02-23 21:00:00 +0000 | [diff] [blame] | 1647 | DelayedIrqFd, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1648 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1649 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 1650 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1651 | .set_raw_mode() |
| 1652 | .expect("failed to set terminal raw mode"); |
| 1653 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1654 | let wait_ctx = WaitContext::build_with(&[ |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1655 | (&exit_evt, Token::Exit), |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1656 | (&reset_evt, Token::Reset), |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1657 | (&crash_evt, Token::Crash), |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1658 | (&panic_rdtube, Token::Panic), |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 1659 | (&linux.suspend_evt, Token::Suspend), |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 1660 | (&sigchld_fd, Token::ChildSignal), |
| 1661 | ]) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1662 | .context("failed to add descriptor to wait context")?; |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 1663 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1664 | if let Some(socket_server) = &control_server_socket { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1665 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1666 | .add(socket_server, Token::VmControlServer) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1667 | .context("failed to add descriptor to wait context")?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1668 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1669 | for (index, socket) in control_tubes.iter().enumerate() { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1670 | wait_ctx |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1671 | .add(socket.as_ref(), Token::VmControl { index }) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1672 | .context("failed to add descriptor to wait context")?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1675 | let events = linux |
| 1676 | .irq_chip |
| 1677 | .irq_event_tokens() |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1678 | .context("failed to add descriptor to wait context")?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1679 | |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 1680 | for (index, _gsi, evt) in events { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1681 | wait_ctx |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 1682 | .add(&evt, Token::IrqFd { index }) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1683 | .context("failed to add descriptor to wait context")?; |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Tomasz Nowicki | 9880100 | 2022-02-23 21:00:00 +0000 | [diff] [blame] | 1686 | if let Some(delayed_ioapic_irq_trigger) = linux.irq_chip.irq_delayed_event_token()? { |
| 1687 | wait_ctx |
| 1688 | .add(&delayed_ioapic_irq_trigger, Token::DelayedIrqFd) |
| 1689 | .context("failed to add descriptor to wait context")?; |
| 1690 | } |
| 1691 | |
Alexandre Courbot | 6a8f656 | 2022-03-24 14:43:48 +0900 | [diff] [blame] | 1692 | if cfg.jail_config.is_some() { |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1693 | // 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] | 1694 | drop_capabilities().context("failed to drop process capabilities")?; |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1695 | } |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 1696 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1697 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1698 | // Create a channel for GDB thread. |
| 1699 | let (to_gdb_channel, from_vcpu_channel) = if linux.gdb.is_some() { |
| 1700 | let (s, r) = mpsc::channel(); |
| 1701 | (Some(s), Some(r)) |
| 1702 | } else { |
| 1703 | (None, None) |
| 1704 | }; |
| 1705 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1706 | let mut vcpu_handles = Vec::with_capacity(linux.vcpu_count); |
| 1707 | let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpu_count + 1)); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1708 | let use_hypervisor_signals = !linux |
| 1709 | .vm |
| 1710 | .get_hypervisor() |
Andrew Walbran | 985491a | 2022-01-27 13:47:40 +0000 | [diff] [blame] | 1711 | .check_capability(HypervisorCap::ImmediateExit); |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 1712 | vcpu::setup_vcpu_signal_handler::<Vcpu>(use_hypervisor_signals)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1713 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1714 | let vcpus: Vec<Option<_>> = match linux.vcpus.take() { |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 1715 | Some(vec) => vec.into_iter().map(Some).collect(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1716 | None => iter::repeat_with(|| None).take(linux.vcpu_count).collect(), |
| 1717 | }; |
Yusuke Sato | 31e136a | 2021-08-18 11:51:38 -0700 | [diff] [blame] | 1718 | // Enable core scheduling before creating vCPUs so that the cookie will be |
| 1719 | // shared by all vCPU threads. |
| 1720 | // TODO(b/199312402): Avoid enabling core scheduling for the crosvm process |
| 1721 | // itself for even better performance. Only vCPUs need the feature. |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1722 | if cfg.per_vm_core_scheduling { |
Yusuke Sato | 31e136a | 2021-08-18 11:51:38 -0700 | [diff] [blame] | 1723 | if let Err(e) = enable_core_scheduling() { |
| 1724 | error!("Failed to enable core scheduling: {}", e); |
| 1725 | } |
| 1726 | } |
Vineeth Pillai | 2b6855e | 2022-01-12 16:57:22 +0000 | [diff] [blame] | 1727 | let vcpu_cgroup_tasks_file = match &cfg.vcpu_cgroup_path { |
| 1728 | None => None, |
| 1729 | Some(cgroup_path) => { |
| 1730 | // Move main process to cgroup_path |
| 1731 | let mut f = File::create(&cgroup_path.join("tasks"))?; |
| 1732 | f.write_all(process::id().to_string().as_bytes())?; |
| 1733 | Some(f) |
| 1734 | } |
| 1735 | }; |
Jiyong Park | 0914020 | 2022-04-01 13:04:40 +0900 | [diff] [blame] | 1736 | |
| 1737 | #[cfg(target_os = "android")] |
| 1738 | android::set_process_profiles(&cfg.task_profiles)?; |
| 1739 | |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 1740 | for (cpu_id, vcpu) in vcpus.into_iter().enumerate() { |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 1741 | let (to_vcpu_channel, from_main_channel) = mpsc::channel(); |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 1742 | let vcpu_affinity = match linux.vcpu_affinity.clone() { |
| 1743 | Some(VcpuAffinity::Global(v)) => v, |
| 1744 | Some(VcpuAffinity::PerVcpu(mut m)) => m.remove(&cpu_id).unwrap_or_default(), |
| 1745 | None => Default::default(), |
| 1746 | }; |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 1747 | let handle = vcpu::run_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1748 | cpu_id, |
ZhaoLiu | 2aaf7ad | 2021-10-10 18:22:29 +0800 | [diff] [blame] | 1749 | kvm_vcpu_ids[cpu_id], |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1750 | vcpu, |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1751 | linux.vm.try_clone().context("failed to clone vm")?, |
| 1752 | linux |
| 1753 | .irq_chip |
| 1754 | .try_box_clone() |
| 1755 | .context("failed to clone irqchip")?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1756 | linux.vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1757 | linux.rt_cpus.contains(&cpu_id), |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 1758 | vcpu_affinity, |
Suleiman Souhlal | 63630e8 | 2021-02-18 11:53:11 +0900 | [diff] [blame] | 1759 | linux.delay_rt, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1760 | linux.no_smt, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1761 | vcpu_thread_barrier.clone(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1762 | linux.has_bios, |
Colin Downs-Razouk | 11bed5e | 2021-11-02 09:33:14 -0700 | [diff] [blame] | 1763 | (*linux.io_bus).clone(), |
| 1764 | (*linux.mmio_bus).clone(), |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1765 | exit_evt.try_clone().context("failed to clone event")?, |
Andrew Walbran | b28ae8e | 2022-01-17 14:33:10 +0000 | [diff] [blame] | 1766 | reset_evt.try_clone().context("failed to clone event")?, |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1767 | crash_evt.try_clone().context("failed to clone event")?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1768 | linux.vm.check_capability(VmCap::PvClockSuspend), |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 1769 | from_main_channel, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1770 | use_hypervisor_signals, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1771 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1772 | to_gdb_channel.clone(), |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1773 | cfg.per_vm_core_scheduling, |
| 1774 | cfg.host_cpu_topology, |
Zide Chen | 344e243 | 2022-01-28 14:58:53 -0800 | [diff] [blame] | 1775 | cfg.privileged_vm, |
Vineeth Pillai | 2b6855e | 2022-01-12 16:57:22 +0000 | [diff] [blame] | 1776 | match vcpu_cgroup_tasks_file { |
| 1777 | None => None, |
| 1778 | Some(ref f) => Some( |
| 1779 | f.try_clone() |
| 1780 | .context("failed to clone vcpu cgroup tasks file")?, |
| 1781 | ), |
| 1782 | }, |
Junichi Uekawa | b3a094e | 2022-03-29 15:41:47 +0900 | [diff] [blame] | 1783 | cfg.userspace_msr.clone(), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1784 | )?; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 1785 | vcpu_handles.push((handle, to_vcpu_channel)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1786 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1787 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1788 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1789 | // Spawn GDB thread. |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1790 | if let Some((gdb_port_num, gdb_control_tube)) = linux.gdb.take() { |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1791 | let to_vcpu_channels = vcpu_handles |
| 1792 | .iter() |
| 1793 | .map(|(_handle, channel)| channel.clone()) |
| 1794 | .collect(); |
| 1795 | let target = GdbStub::new( |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1796 | gdb_control_tube, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1797 | to_vcpu_channels, |
| 1798 | from_vcpu_channel.unwrap(), // Must succeed to unwrap() |
| 1799 | ); |
| 1800 | thread::Builder::new() |
| 1801 | .name("gdb".to_owned()) |
| 1802 | .spawn(move || gdb_thread(target, gdb_port_num)) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1803 | .context("failed to spawn GDB thread")?; |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1804 | }; |
| 1805 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1806 | vcpu_thread_barrier.wait(); |
| 1807 | |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1808 | let mut exit_state = ExitState::Stop; |
Charles William Dick | 5404501 | 2021-07-27 19:11:53 +0900 | [diff] [blame] | 1809 | let mut balloon_stats_id: u64 = 0; |
| 1810 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1811 | 'wait: loop { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1812 | let events = { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1813 | match wait_ctx.wait() { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1814 | Ok(v) => v, |
| 1815 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1816 | error!("failed to poll: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1817 | break; |
| 1818 | } |
| 1819 | } |
| 1820 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1821 | |
| 1822 | let mut vm_control_indices_to_remove = Vec::new(); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1823 | for event in events.iter().filter(|e| e.is_readable) { |
| 1824 | match event.token { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1825 | Token::Exit => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1826 | info!("vcpu requested shutdown"); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1827 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1828 | } |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 1829 | Token::Reset => { |
| 1830 | info!("vcpu requested reset"); |
| 1831 | exit_state = ExitState::Reset; |
| 1832 | break 'wait; |
| 1833 | } |
Andrew Walbran | 1a19c67 | 2022-01-24 17:24:10 +0000 | [diff] [blame] | 1834 | Token::Crash => { |
| 1835 | info!("vcpu crashed"); |
| 1836 | exit_state = ExitState::Crash; |
| 1837 | break 'wait; |
| 1838 | } |
Vineeth Pillai | 9a3d2dc | 2022-02-18 14:10:16 +0000 | [diff] [blame] | 1839 | Token::Panic => { |
| 1840 | let mut break_to_wait: bool = true; |
| 1841 | match panic_rdtube.recv::<u8>() { |
| 1842 | Ok(panic_code) => { |
| 1843 | let panic_code = PvPanicCode::from_u8(panic_code); |
| 1844 | info!("Guest reported panic [Code: {}]", panic_code); |
| 1845 | if panic_code == PvPanicCode::CrashLoaded { |
| 1846 | // VM is booting to crash kernel. |
| 1847 | break_to_wait = false; |
| 1848 | } |
| 1849 | } |
| 1850 | Err(e) => { |
| 1851 | warn!("failed to recv panic event: {} ", e); |
| 1852 | } |
| 1853 | } |
| 1854 | if break_to_wait { |
| 1855 | exit_state = ExitState::GuestPanic; |
| 1856 | break 'wait; |
| 1857 | } |
| 1858 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 1859 | Token::Suspend => { |
| 1860 | info!("VM requested suspend"); |
| 1861 | linux.suspend_evt.read().unwrap(); |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 1862 | vcpu::kick_all_vcpus( |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1863 | &vcpu_handles, |
| 1864 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 1865 | VcpuControl::RunState(VmRunMode::Suspending), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1866 | ); |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 1867 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1868 | Token::ChildSignal => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1869 | // Print all available siginfo structs, then exit the loop. |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1870 | while let Some(siginfo) = |
| 1871 | sigchld_fd.read().context("failed to create signalfd")? |
| 1872 | { |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 1873 | let pid = siginfo.ssi_pid; |
| 1874 | let pid_label = match linux.pid_debug_label_map.get(&pid) { |
| 1875 | Some(label) => format!("{} (pid {})", label, pid), |
| 1876 | None => format!("pid {}", pid), |
| 1877 | }; |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1878 | error!( |
| 1879 | "child {} died: signo {}, status {}, code {}", |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 1880 | pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1881 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1882 | } |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1883 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1884 | } |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 1885 | Token::IrqFd { index } => { |
| 1886 | if let Err(e) = linux.irq_chip.service_irq_event(index) { |
| 1887 | error!("failed to signal irq {}: {}", index, e); |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 1888 | } |
| 1889 | } |
Tomasz Nowicki | 9880100 | 2022-02-23 21:00:00 +0000 | [diff] [blame] | 1890 | Token::DelayedIrqFd => { |
| 1891 | if let Err(e) = linux.irq_chip.process_delayed_irq_events() { |
| 1892 | warn!("can't deliver delayed irqs: {}", e); |
| 1893 | } |
| 1894 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1895 | Token::VmControlServer => { |
| 1896 | if let Some(socket_server) = &control_server_socket { |
| 1897 | match socket_server.accept() { |
| 1898 | Ok(socket) => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1899 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1900 | .add( |
| 1901 | &socket, |
| 1902 | Token::VmControl { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1903 | index: control_tubes.len(), |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1904 | }, |
| 1905 | ) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 1906 | .context("failed to add descriptor to wait context")?; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1907 | control_tubes.push(TaggedControlTube::Vm(Tube::new(socket))); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1908 | } |
| 1909 | Err(e) => error!("failed to accept socket: {}", e), |
| 1910 | } |
| 1911 | } |
| 1912 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1913 | Token::VmControl { index } => { |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1914 | let mut add_tubes = Vec::new(); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1915 | if let Some(socket) = control_tubes.get(index) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1916 | match socket { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1917 | TaggedControlTube::Vm(tube) => match tube.recv::<VmRequest>() { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1918 | Ok(request) => { |
| 1919 | let mut run_mode_opt = None; |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1920 | let response = match request { |
| 1921 | VmRequest::VfioCommand { vfio_path, add } => { |
| 1922 | handle_vfio_command( |
| 1923 | &mut linux, |
| 1924 | &mut sys_allocator, |
| 1925 | &cfg, |
| 1926 | &mut add_tubes, |
Haiwei Li | e2dffbf | 2022-02-18 14:30:56 +0800 | [diff] [blame] | 1927 | &iommu_host_tube, |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1928 | &vfio_path, |
| 1929 | add, |
| 1930 | ) |
| 1931 | } |
| 1932 | _ => request.execute( |
| 1933 | &mut run_mode_opt, |
Andrew Walbran | 3cd9360 | 2022-01-25 13:59:23 +0000 | [diff] [blame] | 1934 | balloon_host_tube.as_ref(), |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1935 | &mut balloon_stats_id, |
| 1936 | disk_host_tubes, |
Peter Fang | 6ca0323 | 2021-12-20 02:17:21 -0800 | [diff] [blame] | 1937 | &mut linux.pm, |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 1938 | #[cfg(feature = "usb")] |
| 1939 | Some(&usb_control_tube), |
| 1940 | #[cfg(not(feature = "usb"))] |
| 1941 | None, |
| 1942 | &mut linux.bat_control, |
| 1943 | &vcpu_handles, |
| 1944 | ), |
| 1945 | }; |
| 1946 | |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1947 | if let Err(e) = tube.send(&response) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1948 | error!("failed to send VmResponse: {}", e); |
| 1949 | } |
| 1950 | if let Some(run_mode) = run_mode_opt { |
| 1951 | info!("control socket changed run mode to {}", run_mode); |
| 1952 | match run_mode { |
| 1953 | VmRunMode::Exiting => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 1954 | break 'wait; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1955 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1956 | other => { |
Chuanxiao Dong | 2bbe85c | 2020-11-12 17:18:07 +0800 | [diff] [blame] | 1957 | if other == VmRunMode::Running { |
Daniel Verkamp | da4e8a9 | 2021-07-21 13:49:02 -0700 | [diff] [blame] | 1958 | for dev in &linux.resume_notify_devices { |
| 1959 | dev.lock().resume_imminent(); |
| 1960 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 1961 | } |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 1962 | vcpu::kick_all_vcpus( |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 1963 | &vcpu_handles, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1964 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 1965 | VcpuControl::RunState(other), |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 1966 | ); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1970 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1971 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1972 | if let TubeError::Disconnected = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1973 | vm_control_indices_to_remove.push(index); |
| 1974 | } else { |
| 1975 | error!("failed to recv VmRequest: {}", e); |
| 1976 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1977 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1978 | }, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1979 | TaggedControlTube::VmMemory(tube) => { |
| 1980 | match tube.recv::<VmMemoryRequest>() { |
| 1981 | Ok(request) => { |
| 1982 | let response = request.execute( |
| 1983 | &mut linux.vm, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 1984 | &mut sys_allocator, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 1985 | Arc::clone(&map_request), |
| 1986 | &mut gralloc, |
| 1987 | ); |
| 1988 | if let Err(e) = tube.send(&response) { |
| 1989 | error!("failed to send VmMemoryControlResponse: {}", e); |
| 1990 | } |
| 1991 | } |
| 1992 | Err(e) => { |
| 1993 | if let TubeError::Disconnected = e { |
| 1994 | vm_control_indices_to_remove.push(index); |
| 1995 | } else { |
| 1996 | error!("failed to recv VmMemoryControlRequest: {}", e); |
| 1997 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1998 | } |
| 1999 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2000 | } |
| 2001 | TaggedControlTube::VmIrq(tube) => match tube.recv::<VmIrqRequest>() { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2002 | Ok(request) => { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2003 | let response = { |
| 2004 | let irq_chip = &mut linux.irq_chip; |
| 2005 | request.execute( |
| 2006 | |setup| match setup { |
Vikram Auradkar | 0953c58 | 2022-03-21 17:33:54 -0700 | [diff] [blame] | 2007 | IrqSetup::Event(irq, ev, _, _, _) => { |
Dmitry Torokhov | f021a62 | 2022-03-21 11:00:29 -0700 | [diff] [blame] | 2008 | let irq_evt = devices::IrqEdgeEvent::from_event(ev.try_clone()?); |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2009 | if let Some(event_index) = irq_chip |
Dmitry Torokhov | f021a62 | 2022-03-21 11:00:29 -0700 | [diff] [blame] | 2010 | .register_edge_irq_event(irq, &irq_evt)? |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2011 | { |
| 2012 | match wait_ctx.add( |
| 2013 | ev, |
| 2014 | Token::IrqFd { |
| 2015 | index: event_index |
| 2016 | }, |
| 2017 | ) { |
| 2018 | Err(e) => { |
| 2019 | warn!("failed to add IrqFd to poll context: {}", e); |
| 2020 | Err(e) |
| 2021 | }, |
| 2022 | Ok(_) => { |
| 2023 | Ok(()) |
| 2024 | } |
| 2025 | } |
| 2026 | } else { |
| 2027 | Ok(()) |
| 2028 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2029 | } |
| 2030 | IrqSetup::Route(route) => irq_chip.route_irq(route), |
Dmitry Torokhov | f021a62 | 2022-03-21 11:00:29 -0700 | [diff] [blame] | 2031 | IrqSetup::UnRegister(irq, ev) => { |
| 2032 | let irq_evt = devices::IrqEdgeEvent::from_event(ev.try_clone()?); |
| 2033 | irq_chip.unregister_edge_irq_event(irq, &irq_evt) |
| 2034 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2035 | }, |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2036 | &mut sys_allocator, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2037 | ) |
| 2038 | }; |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2039 | if let Err(e) = tube.send(&response) { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2040 | error!("failed to send VmIrqResponse: {}", e); |
| 2041 | } |
| 2042 | } |
| 2043 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2044 | if let TubeError::Disconnected = e { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2045 | vm_control_indices_to_remove.push(index); |
| 2046 | } else { |
| 2047 | error!("failed to recv VmIrqRequest: {}", e); |
| 2048 | } |
| 2049 | } |
| 2050 | }, |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2051 | TaggedControlTube::VmMsync(tube) => { |
| 2052 | match tube.recv::<VmMsyncRequest>() { |
| 2053 | Ok(request) => { |
| 2054 | let response = request.execute(&mut linux.vm); |
| 2055 | if let Err(e) = tube.send(&response) { |
| 2056 | error!("failed to send VmMsyncResponse: {}", e); |
| 2057 | } |
| 2058 | } |
| 2059 | Err(e) => { |
| 2060 | if let TubeError::Disconnected = e { |
| 2061 | vm_control_indices_to_remove.push(index); |
| 2062 | } else { |
| 2063 | error!("failed to recv VmMsyncRequest: {}", e); |
| 2064 | } |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2065 | } |
| 2066 | } |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2067 | } |
| 2068 | TaggedControlTube::Fs(tube) => match tube.recv::<FsMappingRequest>() { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2069 | Ok(request) => { |
| 2070 | let response = |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2071 | request.execute(&mut linux.vm, &mut sys_allocator); |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2072 | if let Err(e) = tube.send(&response) { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2073 | error!("failed to send VmResponse: {}", e); |
| 2074 | } |
| 2075 | } |
| 2076 | Err(e) => { |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2077 | if let TubeError::Disconnected = e { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2078 | vm_control_indices_to_remove.push(index); |
| 2079 | } else { |
| 2080 | error!("failed to recv VmResponse: {}", e); |
| 2081 | } |
| 2082 | } |
| 2083 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2084 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2085 | } |
Xiong Zhang | c78e72b | 2021-04-08 11:31:41 +0800 | [diff] [blame] | 2086 | if !add_tubes.is_empty() { |
| 2087 | for (idx, socket) in add_tubes.iter().enumerate() { |
| 2088 | wait_ctx |
| 2089 | .add( |
| 2090 | socket.as_ref(), |
| 2091 | Token::VmControl { |
| 2092 | index: idx + control_tubes.len(), |
| 2093 | }, |
| 2094 | ) |
| 2095 | .context( |
| 2096 | "failed to add hotplug vfio-pci descriptor ot wait context", |
| 2097 | )?; |
| 2098 | } |
| 2099 | control_tubes.append(&mut add_tubes); |
| 2100 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2101 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2102 | } |
| 2103 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2104 | |
Vikram Auradkar | ede68c7 | 2021-07-01 14:33:54 -0700 | [diff] [blame] | 2105 | // It's possible more data is readable and buffered while the socket is hungup, |
| 2106 | // so don't delete the tube from the poll context until we're sure all the |
| 2107 | // data is read. |
| 2108 | // Below case covers a condition where we have received a hungup event and the tube is not |
| 2109 | // readable. |
| 2110 | // In case of readable tube, once all data is read, any attempt to read more data on hungup |
| 2111 | // tube should fail. On such failure, we get Disconnected error and index gets added to |
| 2112 | // vm_control_indices_to_remove by the time we reach here. |
| 2113 | for event in events.iter().filter(|e| e.is_hungup && !e.is_readable) { |
| 2114 | if let Token::VmControl { index } = event.token { |
| 2115 | vm_control_indices_to_remove.push(index); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2116 | } |
| 2117 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2118 | |
| 2119 | // 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] | 2120 | // preserves correct indexes as each element is removed. |
Daniel Verkamp | 8c2f000 | 2020-08-31 15:13:35 -0700 | [diff] [blame] | 2121 | vm_control_indices_to_remove.sort_unstable_by_key(|&k| Reverse(k)); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2122 | vm_control_indices_to_remove.dedup(); |
| 2123 | for index in vm_control_indices_to_remove { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2124 | // Delete the socket from the `wait_ctx` synchronously. Otherwise, the kernel will do |
| 2125 | // 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] | 2126 | // 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] | 2127 | // 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] | 2128 | // that has already been closed. Because the token associated with that spurious event |
| 2129 | // now belongs to a different socket, the control loop will start to interact with |
| 2130 | // sockets that might not be ready to use. This can cause incorrect hangup detection or |
| 2131 | // 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] | 2132 | if let Some(socket) = control_tubes.get(index) { |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 2133 | wait_ctx |
| 2134 | .delete(socket) |
| 2135 | .context("failed to remove descriptor from wait context")?; |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | // This line implicitly drops the socket at `index` when it gets returned by |
| 2139 | // `swap_remove`. After this line, the socket at `index` is not the one from |
| 2140 | // `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] | 2141 | // 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] | 2142 | control_tubes.swap_remove(index); |
| 2143 | if let Some(tube) = control_tubes.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2144 | wait_ctx |
Zach Reizner | d49bcdb | 2021-01-07 08:30:28 -0800 | [diff] [blame] | 2145 | .modify(tube, EventType::Read, Token::VmControl { index }) |
Daniel Verkamp | 6b29858 | 2021-08-16 15:37:11 -0700 | [diff] [blame] | 2146 | .context("failed to add descriptor to wait context")?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2147 | } |
| 2148 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2149 | } |
| 2150 | |
Anton Romanov | 5acc0f5 | 2022-01-28 00:18:11 +0000 | [diff] [blame] | 2151 | vcpu::kick_all_vcpus( |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2152 | &vcpu_handles, |
| 2153 | linux.irq_chip.as_irq_chip(), |
Daniel Verkamp | 2940980 | 2021-02-24 14:46:19 -0800 | [diff] [blame] | 2154 | VcpuControl::RunState(VmRunMode::Exiting), |
Zach Reizner | dc74848 | 2021-04-14 13:59:30 -0700 | [diff] [blame] | 2155 | ); |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2156 | for (handle, _) in vcpu_handles { |
| 2157 | if let Err(e) = handle.join() { |
| 2158 | error!("failed to join vcpu thread: {:?}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 2162 | // Explicitly drop the VM structure here to allow the devices to clean up before the |
| 2163 | // control sockets are closed when this function exits. |
| 2164 | mem::drop(linux); |
| 2165 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 2166 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2167 | .set_canon_mode() |
| 2168 | .expect("failed to restore canonical mode for terminal"); |
| 2169 | |
Dmitry Torokhov | f75699f | 2021-12-03 11:19:13 -0800 | [diff] [blame] | 2170 | Ok(exit_state) |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2171 | } |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 2172 | |
| 2173 | #[cfg(test)] |
| 2174 | mod tests { |
| 2175 | use super::*; |
Xiong Zhang | 626f014 | 2022-03-12 16:05:17 +0800 | [diff] [blame] | 2176 | use std::path::PathBuf; |
Daniel Verkamp | 5586ff5 | 2022-02-24 16:34:55 -0800 | [diff] [blame] | 2177 | |
| 2178 | // Create a file-backed mapping parameters struct with the given `address` and `size` and other |
| 2179 | // parameters set to default values. |
| 2180 | fn test_file_backed_mapping(address: u64, size: u64) -> FileBackedMappingParameters { |
| 2181 | FileBackedMappingParameters { |
| 2182 | address, |
| 2183 | size, |
| 2184 | path: PathBuf::new(), |
| 2185 | offset: 0, |
| 2186 | writable: false, |
| 2187 | sync: false, |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | #[test] |
| 2192 | fn guest_mem_file_backed_mappings_overlap() { |
| 2193 | // Base case: no file mappings; output layout should be identical. |
| 2194 | assert_eq!( |
| 2195 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2196 | vec![ |
| 2197 | (GuestAddress(0), 0xD000_0000), |
| 2198 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2199 | ], |
| 2200 | &[] |
| 2201 | ), |
| 2202 | vec![ |
| 2203 | (GuestAddress(0), 0xD000_0000), |
| 2204 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2205 | ] |
| 2206 | ); |
| 2207 | |
| 2208 | // File mapping that does not overlap guest memory. |
| 2209 | assert_eq!( |
| 2210 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2211 | vec![ |
| 2212 | (GuestAddress(0), 0xD000_0000), |
| 2213 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2214 | ], |
| 2215 | &[test_file_backed_mapping(0xD000_0000, 0x1000)] |
| 2216 | ), |
| 2217 | vec![ |
| 2218 | (GuestAddress(0), 0xD000_0000), |
| 2219 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2220 | ] |
| 2221 | ); |
| 2222 | |
| 2223 | // File mapping at the start of the low address space region. |
| 2224 | assert_eq!( |
| 2225 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2226 | vec![ |
| 2227 | (GuestAddress(0), 0xD000_0000), |
| 2228 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2229 | ], |
| 2230 | &[test_file_backed_mapping(0, 0x2000)] |
| 2231 | ), |
| 2232 | vec![ |
| 2233 | (GuestAddress(0x2000), 0xD000_0000 - 0x2000), |
| 2234 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2235 | ] |
| 2236 | ); |
| 2237 | |
| 2238 | // File mapping at the end of the low address space region. |
| 2239 | assert_eq!( |
| 2240 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2241 | vec![ |
| 2242 | (GuestAddress(0), 0xD000_0000), |
| 2243 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2244 | ], |
| 2245 | &[test_file_backed_mapping(0xD000_0000 - 0x2000, 0x2000)] |
| 2246 | ), |
| 2247 | vec![ |
| 2248 | (GuestAddress(0), 0xD000_0000 - 0x2000), |
| 2249 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2250 | ] |
| 2251 | ); |
| 2252 | |
| 2253 | // File mapping fully contained within the middle of the low address space region. |
| 2254 | assert_eq!( |
| 2255 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2256 | vec![ |
| 2257 | (GuestAddress(0), 0xD000_0000), |
| 2258 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2259 | ], |
| 2260 | &[test_file_backed_mapping(0x1000, 0x2000)] |
| 2261 | ), |
| 2262 | vec![ |
| 2263 | (GuestAddress(0), 0x1000), |
| 2264 | (GuestAddress(0x3000), 0xD000_0000 - 0x3000), |
| 2265 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2266 | ] |
| 2267 | ); |
| 2268 | |
| 2269 | // File mapping at the start of the high address space region. |
| 2270 | assert_eq!( |
| 2271 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2272 | vec![ |
| 2273 | (GuestAddress(0), 0xD000_0000), |
| 2274 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2275 | ], |
| 2276 | &[test_file_backed_mapping(0x1_0000_0000, 0x2000)] |
| 2277 | ), |
| 2278 | vec![ |
| 2279 | (GuestAddress(0), 0xD000_0000), |
| 2280 | (GuestAddress(0x1_0000_2000), 0x8_0000 - 0x2000), |
| 2281 | ] |
| 2282 | ); |
| 2283 | |
| 2284 | // File mapping at the end of the high address space region. |
| 2285 | assert_eq!( |
| 2286 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2287 | vec![ |
| 2288 | (GuestAddress(0), 0xD000_0000), |
| 2289 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2290 | ], |
| 2291 | &[test_file_backed_mapping(0x1_0008_0000 - 0x2000, 0x2000)] |
| 2292 | ), |
| 2293 | vec![ |
| 2294 | (GuestAddress(0), 0xD000_0000), |
| 2295 | (GuestAddress(0x1_0000_0000), 0x8_0000 - 0x2000), |
| 2296 | ] |
| 2297 | ); |
| 2298 | |
| 2299 | // File mapping fully contained within the middle of the high address space region. |
| 2300 | assert_eq!( |
| 2301 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2302 | vec![ |
| 2303 | (GuestAddress(0), 0xD000_0000), |
| 2304 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2305 | ], |
| 2306 | &[test_file_backed_mapping(0x1_0000_1000, 0x2000)] |
| 2307 | ), |
| 2308 | vec![ |
| 2309 | (GuestAddress(0), 0xD000_0000), |
| 2310 | (GuestAddress(0x1_0000_0000), 0x1000), |
| 2311 | (GuestAddress(0x1_0000_3000), 0x8_0000 - 0x3000), |
| 2312 | ] |
| 2313 | ); |
| 2314 | |
| 2315 | // File mapping overlapping two guest memory regions. |
| 2316 | assert_eq!( |
| 2317 | punch_holes_in_guest_mem_layout_for_mappings( |
| 2318 | vec![ |
| 2319 | (GuestAddress(0), 0xD000_0000), |
| 2320 | (GuestAddress(0x1_0000_0000), 0x8_0000), |
| 2321 | ], |
| 2322 | &[test_file_backed_mapping(0xA000_0000, 0x60002000)] |
| 2323 | ), |
| 2324 | vec![ |
| 2325 | (GuestAddress(0), 0xA000_0000), |
| 2326 | (GuestAddress(0x1_0000_2000), 0x8_0000 - 0x2000), |
| 2327 | ] |
| 2328 | ); |
| 2329 | } |
| 2330 | } |