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