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