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