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