blob: 444d8c9b487f80f9192fd83b58702be98a56b1ef [file] [log] [blame]
Zach Reizner39aa26b2017-12-12 18:03:23 -08001// 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 Dongcb03ec62022-01-20 08:25:38 +08005use std::cmp::{max, Reverse};
Daniel Verkamp5586ff52022-02-24 16:34:55 -08006use std::collections::{BTreeMap, BTreeSet};
Anton Romanov5acc0f52022-01-28 00:18:11 +00007use std::convert::TryInto;
Dylan Reid059a1882018-07-23 17:58:09 -07008use std::fs::{File, OpenOptions};
Vineeth Pillai2b6855e2022-01-12 16:57:22 +00009use std::io::prelude::*;
Federico 'Morg' Pareschia1184822021-09-09 10:52:58 +090010use std::io::stdin;
Steven Richmanf32d0b42020-06-20 21:45:32 -070011use std::iter;
Daniel Verkamp94c35272019-09-12 13:31:30 -070012use std::mem;
Haiwei Li09b7b8e2022-02-18 18:16:05 +080013use std::ops::RangeInclusive;
Anton Romanovd43ae3c2022-01-31 17:32:54 +000014#[cfg(feature = "gpu")]
15use std::os::unix::net::UnixStream;
16use std::os::unix::prelude::OpenOptionsExt;
Xiong Zhang626f0142022-03-12 16:05:17 +080017use std::path::Path;
Daniel Verkampaebda742022-04-07 16:37:27 -070018use std::str::FromStr;
Dylan Reidb0492662019-05-17 14:50:13 -070019use std::sync::{mpsc, Arc, Barrier};
Hikaru Nishida584e52c2021-04-27 17:37:08 +090020use std::time::Duration;
Dylan Reidb0492662019-05-17 14:50:13 -070021
Vineeth Pillai2b6855e2022-01-12 16:57:22 +000022use std::process;
Anton Romanov5acc0f52022-01-28 00:18:11 +000023#[cfg(all(target_arch = "x86_64", feature = "gdb"))]
Zach Reizner39aa26b2017-12-12 18:03:23 -080024use std::thread;
Zach Reizner39aa26b2017-12-12 18:03:23 -080025
Alexandre Courbotc6ad83f2022-02-07 19:45:31 +090026use devices::virtio::vhost::vsock::{VhostVsockConfig, VhostVsockDeviceParameter};
Anton Romanov5acc0f52022-01-28 00:18:11 +000027use libc;
Zach Reizner39aa26b2017-12-12 18:03:23 -080028
Tomasz Jeznach42644642020-05-20 23:27:59 -070029use acpi_tables::sdt::SDT;
30
Daniel Verkamp6b298582021-08-16 15:37:11 -070031use anyhow::{anyhow, bail, Context, Result};
Zach Reiznerd49bcdb2021-01-07 08:30:28 -080032use base::*;
Daniel Verkamp578e7cc2022-03-01 22:34:52 -080033use base::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener};
Anton Romanov5acc0f52022-01-28 00:18:11 +000034use devices::serial_device::SerialHardware;
Zide Chenafdb9382021-06-17 12:04:43 -070035use devices::vfio::{VfioCommonSetup, VfioCommonTrait};
Woody Chow055b81b2022-01-25 18:34:29 +090036use devices::virtio::memory_mapper::MemoryMapperTrait;
Anton Romanovd43ae3c2022-01-31 17:32:54 +000037#[cfg(feature = "gpu")]
Anton Romanov5acc0f52022-01-28 00:18:11 +000038use devices::virtio::{self, EventDevice};
paulhsiace17e6e2020-08-28 18:37:45 +080039#[cfg(feature = "audio")]
40use devices::Ac97Dev;
Xiong Zhang17b0daf2019-04-23 17:14:50 +080041use devices::{
Anton Romanov5acc0f52022-01-28 00:18:11 +000042 self, BusDeviceObj, HostHotPlugKey, HotPlugBus, IrqEventIndex, KvmKernelIrqChip, PciAddress,
Xiong Zhang626f0142022-03-12 16:05:17 +080043 PciDevice, PvPanicCode, PvPanicPciDevice, StubPciDevice, VirtioPciDevice,
Xiong Zhang17b0daf2019-04-23 17:14:50 +080044};
Chuanxiao Donga8d427b2022-01-07 10:26:24 +080045use devices::{CoIommuDev, IommuDevType};
Daniel Verkampf1439d42021-05-21 13:55:10 -070046#[cfg(feature = "usb")]
47use devices::{HostBackendDeviceProvider, XhciController};
Steven Richmanf32d0b42020-06-20 21:45:32 -070048use hypervisor::kvm::{Kvm, KvmVcpu, KvmVm};
Anton Romanov5acc0f52022-01-28 00:18:11 +000049use hypervisor::{HypervisorCap, ProtectionType, Vm, VmCap};
Allen Webbf3024c82020-06-19 07:19:48 -070050use minijail::{self, Minijail};
Anton Romanov5acc0f52022-01-28 00:18:11 +000051use resources::{Alloc, SystemAllocator};
Gurchetan Singh293913c2020-12-09 10:44:13 -080052use rutabaga_gfx::RutabagaGralloc;
Dylan Reidb0492662019-05-17 14:50:13 -070053use sync::Mutex;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -080054use vm_control::*;
Sergey Senozhatskyd78d05b2021-04-13 20:59:58 +090055use vm_memory::{GuestAddress, GuestMemory, MemoryPolicy};
Zach Reizner39aa26b2017-12-12 18:03:23 -080056
Keiichi Watanabec5262e92020-10-21 15:57:33 +090057#[cfg(all(target_arch = "x86_64", feature = "gdb"))]
58use crate::gdb::{gdb_thread, GdbStub};
Daniel Verkamp5586ff52022-02-24 16:34:55 -080059use crate::{Config, Executable, FileBackedMappingParameters, SharedDir, SharedDirKind, VfioType};
Daniel Verkampa7b6a1c2020-03-09 13:16:46 -070060use arch::{
Keiichi Watanabe553d2192021-08-16 16:42:27 +090061 self, LinuxArch, RunnableLinuxVm, VcpuAffinity, VirtioDeviceStub, VmComponents, VmImage,
Daniel Verkampa7b6a1c2020-03-09 13:16:46 -070062};
Sonny Raoed517d12018-02-13 22:09:43 -080063
Xiong Zhang626f0142022-03-12 16:05:17 +080064#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
65use {
66 crate::HostPcieRootPortParameters,
67 devices::{
68 IrqChipX86_64 as IrqChipArch, KvmSplitIrqChip, PciBridge, PcieHostRootPort, PcieRootPort,
69 },
70 hypervisor::{VcpuX86_64 as VcpuArch, VmX86_64 as VmArch},
71 x86_64::X8664arch as Arch,
72};
Sonny Rao2ffa0cb2018-02-26 17:27:40 -080073#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
Steven Richmanf32d0b42020-06-20 21:45:32 -070074use {
75 aarch64::AArch64 as Arch,
Steven Richman11dc6712020-09-02 15:39:14 -070076 devices::IrqChipAArch64 as IrqChipArch,
Steven Richmanf32d0b42020-06-20 21:45:32 -070077 hypervisor::{VcpuAArch64 as VcpuArch, VmAArch64 as VmArch},
78};
Zach Reizner39aa26b2017-12-12 18:03:23 -080079
Anton Romanov5acc0f52022-01-28 00:18:11 +000080mod device_helpers;
81use device_helpers::*;
Anton Romanovdb0f4d62022-03-23 21:24:29 +000082pub(crate) mod jail_helpers;
Anton Romanov5acc0f52022-01-28 00:18:11 +000083use jail_helpers::*;
84mod vcpu;
Chirantan Ekbote2ee9dcd2021-05-26 18:21:44 +090085
David Tolnay2b089fc2019-03-04 15:33:22 -080086#[cfg(feature = "gpu")]
Anton Romanovdb0f4d62022-03-23 21:24:29 +000087pub(crate) mod gpu;
Chirantan Ekbote44292f52021-06-25 18:31:41 +090088#[cfg(feature = "gpu")]
Dmitry Torokhove464a7a2022-01-26 13:29:36 -080089pub use gpu::GpuRenderServerParameters;
90#[cfg(feature = "gpu")]
Anton Romanov5acc0f52022-01-28 00:18:11 +000091use gpu::*;
Jorge E. Moreirad4562d02021-06-28 16:21:12 -070092
Jiyong Park09140202022-04-01 13:04:40 +090093#[cfg(target_os = "android")]
94mod android;
95
Zach Reiznerd49bcdb2021-01-07 08:30:28 -080096// gpu_device_tube is not used when GPU support is disabled.
Dmitry Torokhovee42b8c2019-05-27 11:14:20 -070097#[cfg_attr(not(feature = "gpu"), allow(unused_variables))]
David Tolnay2b089fc2019-03-04 15:33:22 -080098fn create_virtio_devices(
99 cfg: &Config,
Steven Richmanf32d0b42020-06-20 21:45:32 -0700100 vm: &mut impl Vm,
Jakub Starona3411ea2019-04-24 10:55:25 -0700101 resources: &mut SystemAllocator,
Michael Hoyle685316f2020-09-16 15:29:20 -0700102 _exit_evt: &Event,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800103 wayland_device_tube: Tube,
104 gpu_device_tube: Tube,
Alexandre Courbote55b7912022-03-04 16:54:38 +0900105 vhost_user_gpu_tubes: Vec<(Tube, Tube, Tube)>,
Andrew Walbran3cd93602022-01-25 13:59:23 +0000106 balloon_device_tube: Option<Tube>,
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800107 balloon_inflate_tube: Option<Tube>,
David Stevens06d157a2022-01-13 23:44:48 +0900108 init_balloon_size: u64,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800109 disk_device_tubes: &mut Vec<Tube>,
110 pmem_device_tubes: &mut Vec<Tube>,
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -0800111 map_request: Arc<Mutex<Option<ExternalMapping>>>,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800112 fs_device_tubes: &mut Vec<Tube>,
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -0800113 #[cfg(feature = "gpu")] render_server_fd: Option<SafeDescriptor>,
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -0800114 vvu_proxy_device_tubes: &mut Vec<Tube>,
David Stevense123e392022-04-07 09:59:25 +0900115 vvu_proxy_max_sibling_mem_size: u64,
David Tolnay2b089fc2019-03-04 15:33:22 -0800116) -> DeviceResult<Vec<VirtioDeviceStub>> {
Dylan Reid059a1882018-07-23 17:58:09 -0700117 let mut devs = Vec::new();
Zach Reizner39aa26b2017-12-12 18:03:23 -0800118
Chirantan Ekbote44292f52021-06-25 18:31:41 +0900119 #[cfg(feature = "gpu")]
Alexandre Courbote55b7912022-03-04 16:54:38 +0900120 for (opt, (host_gpu_tube, device_gpu_tube, device_control_tube)) in
121 cfg.vhost_user_gpu.iter().zip(vhost_user_gpu_tubes)
122 {
Chirantan Ekbote44292f52021-06-25 18:31:41 +0900123 devs.push(create_vhost_user_gpu_device(
124 cfg,
125 opt,
Alexandre Courbote55b7912022-03-04 16:54:38 +0900126 (host_gpu_tube, device_gpu_tube),
127 device_control_tube,
Chirantan Ekbote44292f52021-06-25 18:31:41 +0900128 )?);
129 }
130
Abhishek Bhardwaj103c1b72021-11-01 15:52:23 -0700131 for opt in &cfg.vvu_proxy {
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -0800132 devs.push(create_vvu_proxy_device(
133 cfg,
134 opt,
135 vvu_proxy_device_tubes.remove(0),
David Stevense123e392022-04-07 09:59:25 +0900136 vvu_proxy_max_sibling_mem_size,
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -0800137 )?);
Abhishek Bhardwaj103c1b72021-11-01 15:52:23 -0700138 }
139
David Tolnayfa701712019-02-13 16:42:54 -0800140 #[cfg_attr(not(feature = "gpu"), allow(unused_mut))]
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800141 let mut resource_bridges = Vec::<Tube>::new();
Chirantan Ekbotedd11d432019-06-11 21:50:46 +0900142
Ryo Hashimoto0b788de2019-12-10 17:14:13 +0900143 if !cfg.wayland_socket_paths.is_empty() {
Chirantan Ekbotedd11d432019-06-11 21:50:46 +0900144 #[cfg_attr(not(feature = "gpu"), allow(unused_mut))]
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800145 let mut wl_resource_bridge = None::<Tube>;
Chirantan Ekbotedd11d432019-06-11 21:50:46 +0900146
147 #[cfg(feature = "gpu")]
148 {
Jason Macnakcc7070b2019-11-06 14:48:12 -0800149 if cfg.gpu_parameters.is_some() {
Daniel Verkamp6b298582021-08-16 15:37:11 -0700150 let (wl_socket, gpu_socket) = Tube::pair().context("failed to create tube")?;
Chirantan Ekbotedd11d432019-06-11 21:50:46 +0900151 resource_bridges.push(gpu_socket);
152 wl_resource_bridge = Some(wl_socket);
153 }
154 }
155
156 devs.push(create_wayland_device(
157 cfg,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800158 wayland_device_tube,
Chirantan Ekbotedd11d432019-06-11 21:50:46 +0900159 wl_resource_bridge,
160 )?);
161 }
David Tolnayfa701712019-02-13 16:42:54 -0800162
Keiichi Watanabe57df6a02019-12-06 22:24:40 +0900163 #[cfg(feature = "video-decoder")]
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900164 let video_dec_cfg = if let Some(backend) = cfg.video_dec {
Daniel Verkamp6b298582021-08-16 15:37:11 -0700165 let (video_tube, gpu_tube) = Tube::pair().context("failed to create tube")?;
Daniel Verkampffb59122021-03-18 14:06:15 -0700166 resource_bridges.push(gpu_tube);
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900167 Some((video_tube, backend))
Daniel Verkampffb59122021-03-18 14:06:15 -0700168 } else {
169 None
170 };
Keiichi Watanabe57df6a02019-12-06 22:24:40 +0900171
172 #[cfg(feature = "video-encoder")]
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900173 let video_enc_cfg = if let Some(backend) = cfg.video_enc {
Daniel Verkamp6b298582021-08-16 15:37:11 -0700174 let (video_tube, gpu_tube) = Tube::pair().context("failed to create tube")?;
Daniel Verkampffb59122021-03-18 14:06:15 -0700175 resource_bridges.push(gpu_tube);
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900176 Some((video_tube, backend))
Daniel Verkampffb59122021-03-18 14:06:15 -0700177 } else {
178 None
179 };
Keiichi Watanabe57df6a02019-12-06 22:24:40 +0900180
Zach Reizner3a8100a2017-09-13 19:15:43 -0700181 #[cfg(feature = "gpu")]
182 {
Noah Golddc7f52b2020-02-01 13:01:58 -0800183 if let Some(gpu_parameters) = &cfg.gpu_parameters {
Anton Romanov5acc0f52022-01-28 00:18:11 +0000184 let mut gpu_display_w = virtio::DEFAULT_DISPLAY_WIDTH;
185 let mut gpu_display_h = virtio::DEFAULT_DISPLAY_HEIGHT;
Jason Macnakd659a0d2021-03-15 15:33:01 -0700186 if !gpu_parameters.displays.is_empty() {
187 gpu_display_w = gpu_parameters.displays[0].width;
188 gpu_display_h = gpu_parameters.displays[0].height;
189 }
190
Zach Reizner65b98f12019-11-22 17:34:58 -0800191 let mut event_devices = Vec::new();
192 if cfg.display_window_mouse {
193 let (event_device_socket, virtio_dev_socket) =
Daniel Verkamp6b298582021-08-16 15:37:11 -0700194 UnixStream::pair().context("failed to create socket")?;
Tristan Muntsinger486cffc2020-09-29 22:05:41 +0000195 let (multi_touch_width, multi_touch_height) = cfg
196 .virtio_multi_touch
Jorge E. Moreira6635ca42021-04-28 13:11:41 -0700197 .first()
Kaiyi Libccb4eb2020-02-06 17:53:11 -0800198 .as_ref()
Tristan Muntsinger486cffc2020-09-29 22:05:41 +0000199 .map(|multi_touch_spec| multi_touch_spec.get_size())
Jason Macnakd659a0d2021-03-15 15:33:01 -0700200 .unwrap_or((gpu_display_w, gpu_display_h));
Tristan Muntsinger486cffc2020-09-29 22:05:41 +0000201 let dev = virtio::new_multi_touch(
Jorge E. Moreira6635ca42021-04-28 13:11:41 -0700202 // u32::MAX is the least likely to collide with the indices generated above for
203 // the multi_touch options, which begin at 0.
204 u32::MAX,
Kaiyi Libccb4eb2020-02-06 17:53:11 -0800205 virtio_dev_socket,
Tristan Muntsinger486cffc2020-09-29 22:05:41 +0000206 multi_touch_width,
207 multi_touch_height,
Noah Goldd4ca29b2020-10-27 12:21:52 -0700208 virtio::base_features(cfg.protected_vm),
Kaiyi Libccb4eb2020-02-06 17:53:11 -0800209 )
Daniel Verkamp6b298582021-08-16 15:37:11 -0700210 .context("failed to set up mouse device")?;
Zach Reizner65b98f12019-11-22 17:34:58 -0800211 devs.push(VirtioDeviceStub {
212 dev: Box::new(dev),
Alexandre Courbot6a8f6562022-03-24 14:43:48 +0900213 jail: simple_jail(&cfg.jail_config, "input_device")?,
Zach Reizner65b98f12019-11-22 17:34:58 -0800214 });
215 event_devices.push(EventDevice::touchscreen(event_device_socket));
216 }
217 if cfg.display_window_keyboard {
218 let (event_device_socket, virtio_dev_socket) =
Daniel Verkamp6b298582021-08-16 15:37:11 -0700219 UnixStream::pair().context("failed to create socket")?;
Noah Goldd4ca29b2020-10-27 12:21:52 -0700220 let dev = virtio::new_keyboard(
Jorge E. Moreira6635ca42021-04-28 13:11:41 -0700221 // u32::MAX is the least likely to collide with the indices generated above for
222 // the multi_touch options, which begin at 0.
223 u32::MAX,
Noah Goldd4ca29b2020-10-27 12:21:52 -0700224 virtio_dev_socket,
225 virtio::base_features(cfg.protected_vm),
226 )
Daniel Verkamp6b298582021-08-16 15:37:11 -0700227 .context("failed to set up keyboard device")?;
Zach Reizner65b98f12019-11-22 17:34:58 -0800228 devs.push(VirtioDeviceStub {
229 dev: Box::new(dev),
Alexandre Courbot6a8f6562022-03-24 14:43:48 +0900230 jail: simple_jail(&cfg.jail_config, "input_device")?,
Zach Reizner65b98f12019-11-22 17:34:58 -0800231 });
232 event_devices.push(EventDevice::keyboard(event_device_socket));
233 }
Chia-I Wu16fb6592021-11-10 11:45:32 -0800234
Zach Reizner0f2cfb02019-06-19 17:46:03 -0700235 devs.push(create_gpu_device(
236 cfg,
237 _exit_evt,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800238 gpu_device_tube,
Zach Reizner0f2cfb02019-06-19 17:46:03 -0700239 resource_bridges,
Ryo Hashimoto0b788de2019-12-10 17:14:13 +0900240 // Use the unnamed socket for GPU display screens.
241 cfg.wayland_socket_paths.get(""),
Zach Reizner0f2cfb02019-06-19 17:46:03 -0700242 cfg.x_display.clone(),
Chia-I Wu16fb6592021-11-10 11:45:32 -0800243 render_server_fd,
Zach Reizner65b98f12019-11-22 17:34:58 -0800244 event_devices,
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -0800245 map_request,
Zach Reizner0f2cfb02019-06-19 17:46:03 -0700246 )?);
Zach Reizner3a8100a2017-09-13 19:15:43 -0700247 }
248 }
249
Richard Fung08289b12022-02-02 20:46:19 +0000250 for (_, param) in cfg
251 .serial_parameters
252 .iter()
253 .filter(|(_k, v)| v.hardware == SerialHardware::VirtioConsole)
254 {
255 let dev = create_console_device(cfg, param)?;
256 devs.push(dev);
257 }
258
259 for disk in &cfg.disks {
260 let disk_device_tube = disk_device_tubes.remove(0);
261 devs.push(create_block_device(cfg, disk, disk_device_tube)?);
262 }
263
264 for blk in &cfg.vhost_user_blk {
265 devs.push(create_vhost_user_block_device(cfg, blk)?);
266 }
267
268 for console in &cfg.vhost_user_console {
269 devs.push(create_vhost_user_console_device(cfg, console)?);
270 }
271
272 for (index, pmem_disk) in cfg.pmem_devices.iter().enumerate() {
273 let pmem_device_tube = pmem_device_tubes.remove(0);
274 devs.push(create_pmem_device(
275 cfg,
276 vm,
277 resources,
278 pmem_disk,
279 index,
280 pmem_device_tube,
281 )?);
282 }
283
Andrew Walbrana24a7522022-02-09 18:23:00 +0000284 if cfg.rng {
285 devs.push(create_rng_device(cfg)?);
286 }
Richard Fung08289b12022-02-02 20:46:19 +0000287
288 #[cfg(feature = "tpm")]
289 {
290 if cfg.software_tpm {
Daniel Verkamp29950ef2021-10-07 14:56:45 -0700291 devs.push(create_software_tpm_device(cfg)?);
Richard Fung08289b12022-02-02 20:46:19 +0000292 }
293 }
294
295 for (idx, single_touch_spec) in cfg.virtio_single_touch.iter().enumerate() {
296 devs.push(create_single_touch_device(
297 cfg,
298 single_touch_spec,
299 idx as u32,
300 )?);
301 }
302
303 for (idx, multi_touch_spec) in cfg.virtio_multi_touch.iter().enumerate() {
304 devs.push(create_multi_touch_device(
305 cfg,
306 multi_touch_spec,
307 idx as u32,
308 )?);
309 }
310
311 for (idx, trackpad_spec) in cfg.virtio_trackpad.iter().enumerate() {
312 devs.push(create_trackpad_device(cfg, trackpad_spec, idx as u32)?);
313 }
314
315 for (idx, mouse_socket) in cfg.virtio_mice.iter().enumerate() {
316 devs.push(create_mouse_device(cfg, mouse_socket, idx as u32)?);
317 }
318
319 for (idx, keyboard_socket) in cfg.virtio_keyboard.iter().enumerate() {
320 devs.push(create_keyboard_device(cfg, keyboard_socket, idx as u32)?);
321 }
322
323 for (idx, switches_socket) in cfg.virtio_switches.iter().enumerate() {
324 devs.push(create_switches_device(cfg, switches_socket, idx as u32)?);
325 }
326
327 for dev_path in &cfg.virtio_input_evdevs {
328 devs.push(create_vinput_device(cfg, dev_path)?);
329 }
330
331 if let Some(balloon_device_tube) = balloon_device_tube {
332 devs.push(create_balloon_device(
333 cfg,
334 balloon_device_tube,
335 balloon_inflate_tube,
336 init_balloon_size,
337 )?);
338 }
339
340 // We checked above that if the IP is defined, then the netmask is, too.
341 for tap_fd in &cfg.tap_fd {
342 devs.push(create_tap_net_device_from_fd(cfg, *tap_fd)?);
343 }
344
345 if let (Some(host_ip), Some(netmask), Some(mac_address)) =
346 (cfg.host_ip, cfg.netmask, cfg.mac_address)
347 {
348 if !cfg.vhost_user_net.is_empty() {
349 bail!("vhost-user-net cannot be used with any of --host_ip, --netmask or --mac");
350 }
351 devs.push(create_net_device_from_config(
352 cfg,
353 host_ip,
354 netmask,
355 mac_address,
356 )?);
357 }
358
359 for tap_name in &cfg.tap_name {
360 devs.push(create_tap_net_device_from_name(cfg, tap_name.as_bytes())?);
361 }
362
363 for net in &cfg.vhost_user_net {
364 devs.push(create_vhost_user_net_device(cfg, net)?);
365 }
366
367 for vsock in &cfg.vhost_user_vsock {
368 devs.push(create_vhost_user_vsock_device(cfg, vsock)?);
369 }
370
371 for opt in &cfg.vhost_user_wl {
372 devs.push(create_vhost_user_wl_device(cfg, opt)?);
373 }
374
Chih-Yang Hsiae31731c2022-01-05 17:30:28 +0800375 #[cfg(feature = "audio_cras")]
376 {
377 for cras_snd in &cfg.cras_snds {
378 devs.push(create_cras_snd_device(cfg, cras_snd.clone())?);
379 }
380 }
381
Daniel Verkampffb59122021-03-18 14:06:15 -0700382 #[cfg(feature = "video-decoder")]
383 {
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900384 if let Some((video_dec_tube, video_dec_backend)) = video_dec_cfg {
Daniel Verkampffb59122021-03-18 14:06:15 -0700385 register_video_device(
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900386 video_dec_backend,
Daniel Verkampffb59122021-03-18 14:06:15 -0700387 &mut devs,
388 video_dec_tube,
389 cfg,
390 devices::virtio::VideoDeviceType::Decoder,
391 )?;
392 }
393 }
394
395 #[cfg(feature = "video-encoder")]
396 {
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900397 if let Some((video_enc_tube, video_enc_backend)) = video_enc_cfg {
Daniel Verkampffb59122021-03-18 14:06:15 -0700398 register_video_device(
Alexandre Courbotb42b3e52021-07-09 23:38:57 +0900399 video_enc_backend,
Daniel Verkampffb59122021-03-18 14:06:15 -0700400 &mut devs,
401 video_enc_tube,
402 cfg,
403 devices::virtio::VideoDeviceType::Encoder,
404 )?;
405 }
406 }
407
Zach Reizneraa575662018-08-15 10:46:32 -0700408 if let Some(cid) = cfg.cid {
Alexandre Courbotc6ad83f2022-02-07 19:45:31 +0900409 let vhost_config = VhostVsockConfig {
410 device: cfg
411 .vhost_vsock_device
412 .clone()
413 .unwrap_or(VhostVsockDeviceParameter::default()),
414 cid,
415 };
416 devs.push(create_vhost_vsock_device(cfg, &vhost_config)?);
Zach Reizneraa575662018-08-15 10:46:32 -0700417 }
418
Woody Chow5890b702021-02-12 14:57:02 +0900419 for vhost_user_fs in &cfg.vhost_user_fs {
Daniel Verkamp166d1dd2021-08-19 17:05:29 -0700420 devs.push(create_vhost_user_fs_device(cfg, vhost_user_fs)?);
Woody Chow5890b702021-02-12 14:57:02 +0900421 }
422
Woody Chow1b16db12021-04-02 16:59:59 +0900423 #[cfg(feature = "audio")]
424 for vhost_user_snd in &cfg.vhost_user_snd {
425 devs.push(create_vhost_user_snd_device(cfg, vhost_user_snd)?);
426 }
427
Chirantan Ekbotebd4723b2019-07-17 10:50:30 +0900428 for shared_dir in &cfg.shared_dirs {
429 let SharedDir {
430 src,
431 tag,
432 kind,
433 uid_map,
434 gid_map,
Chirantan Ekbote75ba8752020-10-27 18:33:02 +0900435 fs_cfg,
436 p9_cfg,
Chirantan Ekbotebd4723b2019-07-17 10:50:30 +0900437 } = shared_dir;
David Tolnay2b089fc2019-03-04 15:33:22 -0800438
Chirantan Ekbotebd4723b2019-07-17 10:50:30 +0900439 let dev = match kind {
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +0900440 SharedDirKind::FS => {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800441 let device_tube = fs_device_tubes.remove(0);
442 create_fs_device(cfg, uid_map, gid_map, src, tag, fs_cfg.clone(), device_tube)?
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +0900443 }
Chirantan Ekbote75ba8752020-10-27 18:33:02 +0900444 SharedDirKind::P9 => create_9p_device(cfg, uid_map, gid_map, src, tag, p9_cfg.clone())?,
Chirantan Ekbotebd4723b2019-07-17 10:50:30 +0900445 };
446 devs.push(dev);
David Tolnay2b089fc2019-03-04 15:33:22 -0800447 }
448
JaeMan Parkeb9cc532021-07-02 15:02:59 +0900449 if let Some(vhost_user_mac80211_hwsim) = &cfg.vhost_user_mac80211_hwsim {
450 devs.push(create_vhost_user_mac80211_hwsim_device(
451 cfg,
Daniel Verkamp166d1dd2021-08-19 17:05:29 -0700452 vhost_user_mac80211_hwsim,
JaeMan Parkeb9cc532021-07-02 15:02:59 +0900453 )?);
454 }
455
Jorge E. Moreirad4562d02021-06-28 16:21:12 -0700456 #[cfg(feature = "audio")]
457 if let Some(path) = &cfg.sound {
Daniel Verkamp166d1dd2021-08-19 17:05:29 -0700458 devs.push(create_sound_device(path, cfg)?);
Jorge E. Moreirad4562d02021-06-28 16:21:12 -0700459 }
460
David Tolnay2b089fc2019-03-04 15:33:22 -0800461 Ok(devs)
462}
463
464fn create_devices(
Trent Begin17ccaad2019-04-17 13:51:25 -0600465 cfg: &Config,
Steven Richmanf32d0b42020-06-20 21:45:32 -0700466 vm: &mut impl Vm,
Jakub Starona3411ea2019-04-24 10:55:25 -0700467 resources: &mut SystemAllocator,
Michael Hoyle685316f2020-09-16 15:29:20 -0700468 exit_evt: &Event,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +0000469 panic_wrtube: Tube,
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800470 iommu_attached_endpoints: &mut BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>>,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800471 control_tubes: &mut Vec<TaggedControlTube>,
472 wayland_device_tube: Tube,
473 gpu_device_tube: Tube,
Alexandre Courbote55b7912022-03-04 16:54:38 +0900474 // Tuple content: (host-side GPU tube, device-side GPU tube, device-side control tube).
475 vhost_user_gpu_tubes: Vec<(Tube, Tube, Tube)>,
Andrew Walbran3cd93602022-01-25 13:59:23 +0000476 balloon_device_tube: Option<Tube>,
David Stevens06d157a2022-01-13 23:44:48 +0900477 init_balloon_size: u64,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -0800478 disk_device_tubes: &mut Vec<Tube>,
479 pmem_device_tubes: &mut Vec<Tube>,
480 fs_device_tubes: &mut Vec<Tube>,
Daniel Verkampf1439d42021-05-21 13:55:10 -0700481 #[cfg(feature = "usb")] usb_provider: HostBackendDeviceProvider,
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -0800482 map_request: Arc<Mutex<Option<ExternalMapping>>>,
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -0800483 #[cfg(feature = "gpu")] render_server_fd: Option<SafeDescriptor>,
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -0800484 vvu_proxy_device_tubes: &mut Vec<Tube>,
David Stevense123e392022-04-07 09:59:25 +0900485 vvu_proxy_max_sibling_mem_size: u64,
Tomasz Nowickiab86d522021-09-22 05:50:46 +0000486) -> DeviceResult<Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)>> {
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800487 let mut devices: Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)> = Vec::new();
488 let mut balloon_inflate_tube: Option<Tube> = None;
Zide Chen5deee482021-04-19 11:06:01 -0700489 if !cfg.vfio.is_empty() {
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800490 let mut coiommu_attached_endpoints = Vec::new();
Zide Chendfc4b882021-03-10 16:35:37 -0800491
Tomasz Nowicki71aca792021-06-09 18:53:49 +0000492 for vfio_dev in cfg
493 .vfio
494 .iter()
495 .filter(|dev| dev.get_type() == VfioType::Pci)
496 {
497 let vfio_path = &vfio_dev.vfio_path;
Zide Chen5deee482021-04-19 11:06:01 -0700498 let (vfio_pci_device, jail) = create_vfio_device(
499 cfg,
500 vm,
501 resources,
502 control_tubes,
503 vfio_path.as_path(),
Xiong Zhangf82f2dc2021-05-21 16:54:12 +0800504 None,
Victor Ding3f749592022-03-18 05:44:20 +0000505 vfio_dev.guest_address(),
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800506 iommu_attached_endpoints,
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800507 Some(&mut coiommu_attached_endpoints),
508 vfio_dev.iommu_dev_type(),
Zide Chen5deee482021-04-19 11:06:01 -0700509 )?;
Zide Chendfc4b882021-03-10 16:35:37 -0800510
Tomasz Nowickiab86d522021-09-22 05:50:46 +0000511 devices.push((vfio_pci_device, jail));
Zide Chen5deee482021-04-19 11:06:01 -0700512 }
Zide Chendfc4b882021-03-10 16:35:37 -0800513
Tomasz Nowicki344eb142021-09-22 05:51:58 +0000514 for vfio_dev in cfg
515 .vfio
516 .iter()
517 .filter(|dev| dev.get_type() == VfioType::Platform)
518 {
519 let vfio_path = &vfio_dev.vfio_path;
520 let (vfio_plat_dev, jail) = create_vfio_platform_device(
521 cfg,
522 vm,
523 resources,
524 control_tubes,
525 vfio_path.as_path(),
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800526 iommu_attached_endpoints,
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800527 IommuDevType::NoIommu, // Virtio IOMMU is not supported yet
Tomasz Nowicki344eb142021-09-22 05:51:58 +0000528 )?;
529
530 devices.push((Box::new(vfio_plat_dev), jail));
531 }
532
Chuanxiao Dongcb03ec62022-01-20 08:25:38 +0800533 if !coiommu_attached_endpoints.is_empty() || !iommu_attached_endpoints.is_empty() {
534 let mut buf = mem::MaybeUninit::<libc::rlimit>::zeroed();
535 let res = unsafe { libc::getrlimit(libc::RLIMIT_MEMLOCK, buf.as_mut_ptr()) };
536 if res == 0 {
537 let limit = unsafe { buf.assume_init() };
538 let rlim_new = limit
539 .rlim_cur
540 .saturating_add(vm.get_memory().memory_size() as libc::rlim_t);
541 let rlim_max = max(limit.rlim_max, rlim_new);
542 if limit.rlim_cur < rlim_new {
543 let limit_arg = libc::rlimit {
544 rlim_cur: rlim_new as libc::rlim_t,
545 rlim_max: rlim_max as libc::rlim_t,
546 };
547 let res = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &limit_arg) };
548 if res != 0 {
549 bail!("Set rlimit failed");
550 }
551 }
552 } else {
553 bail!("Get rlimit failed");
554 }
555 }
556
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800557 if !coiommu_attached_endpoints.is_empty() {
558 let vfio_container =
559 VfioCommonSetup::vfio_get_container(IommuDevType::CoIommu, None as Option<&Path>)
560 .context("failed to get vfio container")?;
561 let (coiommu_host_tube, coiommu_device_tube) =
562 Tube::pair().context("failed to create coiommu tube")?;
563 control_tubes.push(TaggedControlTube::VmMemory(coiommu_host_tube));
564 let vcpu_count = cfg.vcpu_count.unwrap_or(1) as u64;
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800565 let (coiommu_tube, balloon_tube) =
566 Tube::pair().context("failed to create coiommu tube")?;
567 balloon_inflate_tube = Some(balloon_tube);
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800568 let dev = CoIommuDev::new(
569 vm.get_memory().clone(),
570 vfio_container,
571 coiommu_device_tube,
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800572 coiommu_tube,
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800573 coiommu_attached_endpoints,
574 vcpu_count,
Chuanxiao Dongd4468612022-01-14 14:21:17 +0800575 cfg.coiommu_param.unwrap_or_default(),
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800576 )
577 .context("failed to create coiommu device")?;
578
Alexandre Courbot6a8f6562022-03-24 14:43:48 +0900579 devices.push((Box::new(dev), simple_jail(&cfg.jail_config, "coiommu")?));
Chuanxiao Donga8d427b2022-01-07 10:26:24 +0800580 }
Xiong Zhang17b0daf2019-04-23 17:14:50 +0800581 }
582
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800583 let stubs = create_virtio_devices(
584 cfg,
585 vm,
586 resources,
587 exit_evt,
588 wayland_device_tube,
589 gpu_device_tube,
590 vhost_user_gpu_tubes,
591 balloon_device_tube,
592 balloon_inflate_tube,
David Stevens06d157a2022-01-13 23:44:48 +0900593 init_balloon_size,
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800594 disk_device_tubes,
595 pmem_device_tubes,
596 map_request,
597 fs_device_tubes,
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -0800598 #[cfg(feature = "gpu")]
599 render_server_fd,
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -0800600 vvu_proxy_device_tubes,
David Stevense123e392022-04-07 09:59:25 +0900601 vvu_proxy_max_sibling_mem_size,
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800602 )?;
603
604 for stub in stubs {
605 let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?;
606 control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube));
607 let dev = VirtioPciDevice::new(vm.get_memory().clone(), stub.dev, msi_device_tube)
608 .context("failed to create virtio pci dev")?;
609 let dev = Box::new(dev) as Box<dyn BusDeviceObj>;
610 devices.push((dev, stub.jail));
611 }
612
613 #[cfg(feature = "audio")]
614 for ac97_param in &cfg.ac97_parameters {
615 let dev = Ac97Dev::try_new(vm.get_memory().clone(), ac97_param.clone())
616 .context("failed to create ac97 device")?;
Alexandre Courbot6a8f6562022-03-24 14:43:48 +0900617 let jail = simple_jail(&cfg.jail_config, dev.minijail_policy())?;
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800618 devices.push((Box::new(dev), jail));
619 }
620
621 #[cfg(feature = "usb")]
Sebastian Ene0440d352022-02-04 12:23:56 +0000622 if cfg.usb {
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800623 // Create xhci controller.
624 let usb_controller = Box::new(XhciController::new(vm.get_memory().clone(), usb_provider));
Alexandre Courbot6a8f6562022-03-24 14:43:48 +0900625 devices.push((usb_controller, simple_jail(&cfg.jail_config, "xhci")?));
Chuanxiao Dong146a13b2021-12-09 12:59:54 +0800626 }
627
Mattias Nisslerde2c6402021-10-21 12:05:29 +0000628 for params in &cfg.stub_pci_devices {
629 // Stub devices don't need jailing since they don't do anything.
630 devices.push((Box::new(StubPciDevice::new(params)), None));
631 }
632
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +0000633 devices.push((Box::new(PvPanicPciDevice::new(panic_wrtube)), None));
Tomasz Nowickiab86d522021-09-22 05:50:46 +0000634 Ok(devices)
David Tolnay2b089fc2019-03-04 15:33:22 -0800635}
636
Mattias Nisslerbbd91d02021-12-07 08:57:45 +0000637fn create_file_backed_mappings(
638 cfg: &Config,
639 vm: &mut impl Vm,
640 resources: &mut SystemAllocator,
641) -> Result<()> {
642 for mapping in &cfg.file_backed_mappings {
643 let file = OpenOptions::new()
644 .read(true)
645 .write(mapping.writable)
646 .custom_flags(if mapping.sync { libc::O_SYNC } else { 0 })
647 .open(&mapping.path)
648 .context("failed to open file for file-backed mapping")?;
649 let prot = if mapping.writable {
650 Protection::read_write()
651 } else {
652 Protection::read()
653 };
654 let size = mapping
655 .size
656 .try_into()
657 .context("Invalid size for file-backed mapping")?;
658 let memory_mapping = MemoryMappingBuilder::new(size)
659 .from_file(&file)
660 .offset(mapping.offset)
661 .protection(prot)
662 .build()
663 .context("failed to map backing file for file-backed mapping")?;
664
Daniel Verkampde4d7292022-03-01 15:22:38 -0800665 match resources.mmio_allocator_any().allocate_at(
666 mapping.address,
667 mapping.size,
668 Alloc::FileBacked(mapping.address),
669 "file-backed mapping".to_owned(),
670 ) {
671 // OutOfSpace just means that this mapping is not in the MMIO regions at all, so don't
672 // consider it an error.
673 // TODO(b/222769529): Reserve this region in a global memory address space allocator once
674 // we have that so nothing else can accidentally overlap with it.
675 Ok(()) | Err(resources::Error::OutOfSpace) => {}
676 e => e.context("failed to allocate guest address for file-backed mapping")?,
677 }
Mattias Nisslerbbd91d02021-12-07 08:57:45 +0000678
679 vm.add_memory_region(
680 GuestAddress(mapping.address),
681 Box::new(memory_mapping),
682 !mapping.writable,
683 /* log_dirty_pages = */ false,
684 )
685 .context("failed to configure file-backed mapping")?;
686 }
687
688 Ok(())
689}
690
Anton Romanov33334412022-03-22 17:48:18 +0000691#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Xiong Zhangf7874712021-12-24 10:53:59 +0800692fn create_pcie_root_port(
Xiong Zhang626f0142022-03-12 16:05:17 +0800693 host_pcie_rp: Vec<HostPcieRootPortParameters>,
Xiong Zhangf7874712021-12-24 10:53:59 +0800694 sys_allocator: &mut SystemAllocator,
695 control_tubes: &mut Vec<TaggedControlTube>,
696 devices: &mut Vec<(Box<dyn BusDeviceObj>, Option<Minijail>)>,
697 hp_vec: &mut Vec<Arc<Mutex<dyn HotPlugBus>>>,
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800698 hp_endpoints_ranges: &mut Vec<RangeInclusive<u32>>,
Daniel Verkampf35f6362022-04-07 17:24:21 -0700699 // TODO(b/228627457): clippy is incorrectly warning about this Vec, which needs to be a Vec so
700 // we can push into it
701 #[allow(clippy::ptr_arg)] gpe_notify_devs: &mut Vec<(u32, Arc<Mutex<dyn GpeNotify>>)>,
Xiong Zhangf7874712021-12-24 10:53:59 +0800702) -> Result<()> {
703 if host_pcie_rp.is_empty() {
704 // user doesn't specify host pcie root port which link to this virtual pcie rp,
705 // find the empty bus and create a total virtual pcie rp
Haiwei Lie35d4652022-02-10 15:39:33 +0800706 let mut hp_sec_bus = 0u8;
707 // Create Pcie Root Port for non-root buses, each non-root bus device will be
708 // connected behind a virtual pcie root port.
709 for i in 1..255 {
710 if sys_allocator.pci_bus_empty(i) {
711 if hp_sec_bus == 0 {
712 hp_sec_bus = i;
713 }
714 continue;
715 }
716 let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new(i, false)));
717 let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?;
718 control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube));
719 let pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube));
720 // no ipc is used if the root port disables hotplug
721 devices.push((pci_bridge, None));
722 }
723
724 // Create Pcie Root Port for hot-plug
725 if hp_sec_bus == 0 {
726 return Err(anyhow!("no more addresses are available"));
727 }
728 let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new(hp_sec_bus, true)));
Xiong Zhangf7874712021-12-24 10:53:59 +0800729 let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?;
730 control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube));
731 let pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube));
732
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800733 hp_endpoints_ranges.push(RangeInclusive::new(
734 PciAddress {
735 bus: pci_bridge.get_secondary_num(),
736 dev: 0,
737 func: 0,
738 }
739 .to_u32(),
740 PciAddress {
741 bus: pci_bridge.get_subordinate_num(),
742 dev: 32,
743 func: 8,
744 }
745 .to_u32(),
746 ));
747
Xiong Zhangf7874712021-12-24 10:53:59 +0800748 devices.push((pci_bridge, None));
749 hp_vec.push(pcie_root_port as Arc<Mutex<dyn HotPlugBus>>);
750 } else {
751 // user specify host pcie root port which link to this virtual pcie rp,
752 // reserve the host pci BDF and create a virtual pcie RP with some attrs same as host
Xiong Zhang626f0142022-03-12 16:05:17 +0800753 for host_pcie in host_pcie_rp.iter() {
Xiong Zhangcdffe492021-12-24 15:13:30 +0800754 let (vm_host_tube, vm_device_tube) = Tube::pair().context("failed to create tube")?;
Xiong Zhang626f0142022-03-12 16:05:17 +0800755 let pcie_host = PcieHostRootPort::new(host_pcie.host_path.as_path(), vm_device_tube)?;
Xiong Zhangd6de3192022-02-16 13:24:06 +0800756 let bus_range = pcie_host.get_bus_range();
757 let mut slot_implemented = true;
758 for i in bus_range.secondary..=bus_range.subordinate {
759 // if this bus is occupied by one vfio-pci device, this vfio-pci device is
760 // connected to a pci bridge on host statically, then it should be connected
761 // to a virtual pci bridge in guest statically, this bridge won't have
762 // hotplug capability and won't use slot.
763 if !sys_allocator.pci_bus_empty(i) {
764 slot_implemented = false;
Haiwei Lie4a9e822022-03-24 14:18:32 +0800765 break;
Xiong Zhangd6de3192022-02-16 13:24:06 +0800766 }
767 }
Xiong Zhang626f0142022-03-12 16:05:17 +0800768
Xiong Zhangd6de3192022-02-16 13:24:06 +0800769 let pcie_root_port = Arc::new(Mutex::new(PcieRootPort::new_from_host(
770 pcie_host,
771 slot_implemented,
772 )?));
Xiong Zhangcdffe492021-12-24 15:13:30 +0800773 control_tubes.push(TaggedControlTube::Vm(vm_host_tube));
Xiong Zhangf7874712021-12-24 10:53:59 +0800774
775 let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?;
776 control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube));
777 let mut pci_bridge = Box::new(PciBridge::new(pcie_root_port.clone(), msi_device_tube));
778 // early reservation for host pcie root port devices.
779 let rootport_addr = pci_bridge.allocate_address(sys_allocator);
780 if rootport_addr.is_err() {
781 warn!(
782 "address reservation failed for hot pcie root port {}",
783 pci_bridge.debug_label()
784 );
785 }
786
Haiwei Lie4a9e822022-03-24 14:18:32 +0800787 // Only append the sub pci range of a hot-pluggable root port to virtio-iommu
788 if slot_implemented {
789 hp_endpoints_ranges.push(RangeInclusive::new(
790 PciAddress {
791 bus: pci_bridge.get_secondary_num(),
792 dev: 0,
793 func: 0,
794 }
795 .to_u32(),
796 PciAddress {
797 bus: pci_bridge.get_subordinate_num(),
798 dev: 32,
799 func: 8,
800 }
801 .to_u32(),
802 ));
803 }
Haiwei Li09b7b8e2022-02-18 18:16:05 +0800804
Xiong Zhangf7874712021-12-24 10:53:59 +0800805 devices.push((pci_bridge, None));
Xiong Zhang626f0142022-03-12 16:05:17 +0800806 if slot_implemented {
807 if let Some(gpe) = host_pcie.hp_gpe {
808 gpe_notify_devs
809 .push((gpe, pcie_root_port.clone() as Arc<Mutex<dyn GpeNotify>>));
810 }
811 hp_vec.push(pcie_root_port as Arc<Mutex<dyn HotPlugBus>>);
812 }
Xiong Zhangf7874712021-12-24 10:53:59 +0800813 }
814 }
815
816 Ok(())
817}
818
Zach Reiznera90649a2021-03-31 12:56:08 -0700819fn setup_vm_components(cfg: &Config) -> Result<VmComponents> {
David Tolnay2b089fc2019-03-04 15:33:22 -0800820 let initrd_image = if let Some(initrd_path) = &cfg.initrd_path {
Andrew Walbranbc55e302021-07-13 17:35:10 +0100821 Some(
Daniel Verkamped6b27a2022-03-25 14:06:05 -0700822 open_file(initrd_path, OpenOptions::new().read(true))
823 .with_context(|| format!("failed to open initrd {}", initrd_path.display()))?,
Andrew Walbranbc55e302021-07-13 17:35:10 +0100824 )
Daniel Verkampe403f5c2018-12-11 16:29:26 -0800825 } else {
826 None
827 };
828
Cody Schuffelen6d1ab502019-05-21 12:12:38 -0700829 let vm_image = match cfg.executable_path {
Andrew Walbranbc55e302021-07-13 17:35:10 +0100830 Some(Executable::Kernel(ref kernel_path)) => VmImage::Kernel(
Daniel Verkamped6b27a2022-03-25 14:06:05 -0700831 open_file(kernel_path, OpenOptions::new().read(true)).with_context(|| {
832 format!("failed to open kernel image {}", kernel_path.display())
833 })?,
Andrew Walbranbc55e302021-07-13 17:35:10 +0100834 ),
835 Some(Executable::Bios(ref bios_path)) => VmImage::Bios(
Daniel Verkamped6b27a2022-03-25 14:06:05 -0700836 open_file(bios_path, OpenOptions::new().read(true))
Daniel Verkamp6b298582021-08-16 15:37:11 -0700837 .with_context(|| format!("failed to open bios {}", bios_path.display()))?,
Andrew Walbranbc55e302021-07-13 17:35:10 +0100838 ),
Cody Schuffelen6d1ab502019-05-21 12:12:38 -0700839 _ => panic!("Did not receive a bios or kernel, should be impossible."),
840 };
841
Will Deaconc48e7832021-07-30 19:03:06 +0100842 let swiotlb = if let Some(size) = cfg.swiotlb {
843 Some(
844 size.checked_mul(1024 * 1024)
Daniel Verkamp6b298582021-08-16 15:37:11 -0700845 .ok_or_else(|| anyhow!("requested swiotlb size too large"))?,
Will Deaconc48e7832021-07-30 19:03:06 +0100846 )
847 } else {
848 match cfg.protected_vm {
Andrew Walbran0bbbb682021-12-13 13:42:07 +0000849 ProtectionType::Protected | ProtectionType::ProtectedWithoutFirmware => {
850 Some(64 * 1024 * 1024)
851 }
Will Deaconc48e7832021-07-30 19:03:06 +0100852 ProtectionType::Unprotected => None,
853 }
854 };
855
Zach Reiznera90649a2021-03-31 12:56:08 -0700856 Ok(VmComponents {
Daniel Verkamp6a847062019-11-26 13:16:35 -0800857 memory_size: cfg
858 .memory
859 .unwrap_or(256)
860 .checked_mul(1024 * 1024)
Daniel Verkamp6b298582021-08-16 15:37:11 -0700861 .ok_or_else(|| anyhow!("requested memory size too large"))?,
Will Deaconc48e7832021-07-30 19:03:06 +0100862 swiotlb,
Dylan Reid059a1882018-07-23 17:58:09 -0700863 vcpu_count: cfg.vcpu_count.unwrap_or(1),
Daniel Verkamp107edb32019-04-05 09:58:48 -0700864 vcpu_affinity: cfg.vcpu_affinity.clone(),
Daniel Verkamp8a72afc2021-03-15 17:55:52 -0700865 cpu_clusters: cfg.cpu_clusters.clone(),
866 cpu_capacity: cfg.cpu_capacity.clone(),
Dmytro Maluka74031b42022-02-25 18:00:17 +0000867 #[cfg(feature = "direct")]
Dmytro Maluka6cea2c72022-02-25 18:22:17 +0000868 direct_gpe: cfg.direct_gpe.clone(),
Suleiman Souhlal015c3c12020-10-07 14:15:41 +0900869 no_smt: cfg.no_smt,
Sergey Senozhatsky1e369c52021-04-13 20:23:51 +0900870 hugepages: cfg.hugepages,
Cody Schuffelen6d1ab502019-05-21 12:12:38 -0700871 vm_image,
Tristan Muntsinger4133b012018-12-21 16:01:56 -0800872 android_fstab: cfg
873 .android_fstab
874 .as_ref()
Daniel Verkamp6b298582021-08-16 15:37:11 -0700875 .map(|x| {
876 File::open(x)
877 .with_context(|| format!("failed to open android fstab file {}", x.display()))
878 })
Tristan Muntsinger4133b012018-12-21 16:01:56 -0800879 .map_or(Ok(None), |v| v.map(Some))?,
Kansho Nishida282115b2019-12-18 13:13:14 +0900880 pstore: cfg.pstore.clone(),
Daniel Verkampe403f5c2018-12-11 16:29:26 -0800881 initrd_image,
Daniel Verkampaac28132018-10-15 14:58:48 -0700882 extra_kernel_params: cfg.params.clone(),
Tomasz Jeznach42644642020-05-20 23:27:59 -0700883 acpi_sdts: cfg
884 .acpi_tables
885 .iter()
Daniel Verkamp6b298582021-08-16 15:37:11 -0700886 .map(|path| {
887 SDT::from_file(path)
888 .with_context(|| format!("failed to open ACPI file {}", path.display()))
889 })
Tomasz Jeznach42644642020-05-20 23:27:59 -0700890 .collect::<Result<Vec<SDT>>>()?,
Kansho Nishidaab205af2020-08-13 18:17:50 +0900891 rt_cpus: cfg.rt_cpus.clone(),
Suleiman Souhlal63630e82021-02-18 11:53:11 +0900892 delay_rt: cfg.delay_rt,
Will Deacon7d2b8ac2020-10-06 18:51:12 +0100893 protected_vm: cfg.protected_vm,
Keiichi Watanabec5262e92020-10-21 15:57:33 +0900894 #[cfg(all(target_arch = "x86_64", feature = "gdb"))]
Zach Reiznera90649a2021-03-31 12:56:08 -0700895 gdb: None,
Tomasz Jeznachccb26942021-03-30 22:44:11 -0700896 dmi_path: cfg.dmi_path.clone(),
Tomasz Jeznachd93c29f2021-04-12 11:00:24 -0700897 no_legacy: cfg.no_legacy,
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +0800898 host_cpu_topology: cfg.host_cpu_topology,
Grzegorz Jaszczykd33874e2022-02-11 18:27:29 +0000899 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
900 force_s2idle: cfg.force_s2idle,
Zach Reiznera90649a2021-03-31 12:56:08 -0700901 })
902}
903
Andrew Walbranb28ae8e2022-01-17 14:33:10 +0000904#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Dmitry Torokhovf75699f2021-12-03 11:19:13 -0800905pub enum ExitState {
906 Reset,
907 Stop,
Andrew Walbran1a19c672022-01-24 17:24:10 +0000908 Crash,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +0000909 GuestPanic,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -0800910}
911
Daniel Verkamp5586ff52022-02-24 16:34:55 -0800912// Remove ranges in `guest_mem_layout` that overlap with ranges in `file_backed_mappings`.
913// Returns the updated guest memory layout.
914fn punch_holes_in_guest_mem_layout_for_mappings(
915 guest_mem_layout: Vec<(GuestAddress, u64)>,
916 file_backed_mappings: &[FileBackedMappingParameters],
917) -> Vec<(GuestAddress, u64)> {
918 // Create a set containing (start, end) pairs with exclusive end (end = start + size; the byte
919 // at end is not included in the range).
920 let mut layout_set = BTreeSet::new();
921 for (addr, size) in &guest_mem_layout {
922 layout_set.insert((addr.offset(), addr.offset() + size));
923 }
924
925 for mapping in file_backed_mappings {
926 let mapping_start = mapping.address;
927 let mapping_end = mapping_start + mapping.size;
928
929 // Repeatedly split overlapping guest memory regions until no overlaps remain.
930 while let Some((range_start, range_end)) = layout_set
931 .iter()
932 .find(|&&(range_start, range_end)| {
933 mapping_start < range_end && mapping_end > range_start
934 })
935 .cloned()
936 {
937 layout_set.remove(&(range_start, range_end));
938
939 if range_start < mapping_start {
940 layout_set.insert((range_start, mapping_start));
941 }
942 if range_end > mapping_end {
943 layout_set.insert((mapping_end, range_end));
944 }
945 }
946 }
947
948 // Build the final guest memory layout from the modified layout_set.
949 layout_set
950 .iter()
951 .map(|(start, end)| (GuestAddress(*start), end - start))
952 .collect()
953}
954
Dmitry Torokhovf75699f2021-12-03 11:19:13 -0800955pub fn run_config(cfg: Config) -> Result<ExitState> {
Zach Reiznerdc748482021-04-14 13:59:30 -0700956 let components = setup_vm_components(&cfg)?;
957
958 let guest_mem_layout =
Daniel Verkamp6b298582021-08-16 15:37:11 -0700959 Arch::guest_memory_layout(&components).context("failed to create guest memory layout")?;
Daniel Verkamp5586ff52022-02-24 16:34:55 -0800960
961 let guest_mem_layout =
962 punch_holes_in_guest_mem_layout_for_mappings(guest_mem_layout, &cfg.file_backed_mappings);
963
Daniel Verkamp6b298582021-08-16 15:37:11 -0700964 let guest_mem = GuestMemory::new(&guest_mem_layout).context("failed to create guest memory")?;
Zach Reiznerdc748482021-04-14 13:59:30 -0700965 let mut mem_policy = MemoryPolicy::empty();
966 if components.hugepages {
967 mem_policy |= MemoryPolicy::USE_HUGEPAGES;
968 }
Quentin Perret26203802021-12-02 09:48:43 +0000969 guest_mem.set_memory_policy(mem_policy);
Daniel Verkamp6b298582021-08-16 15:37:11 -0700970 let kvm = Kvm::new_with_path(&cfg.kvm_device_path).context("failed to create kvm")?;
Andrew Walbran00f1c9f2021-12-10 17:13:08 +0000971 let vm = KvmVm::new(&kvm, guest_mem, components.protected_vm).context("failed to create vm")?;
Junichi Uekawab3a094e2022-03-29 15:41:47 +0900972
973 if !cfg.userspace_msr.is_empty() {
974 vm.enable_userspace_msr()
975 .context("failed to enable userspace MSR handling, do you have kernel 5.10 or later")?;
976 }
977
Andrew Walbrane79aba12022-01-27 14:12:35 +0000978 // Check that the VM was actually created in protected mode as expected.
979 if cfg.protected_vm != ProtectionType::Unprotected && !vm.check_capability(VmCap::Protected) {
980 bail!("Failed to create protected VM");
981 }
Daniel Verkamp6b298582021-08-16 15:37:11 -0700982 let vm_clone = vm.try_clone().context("failed to clone vm")?;
Zach Reiznerdc748482021-04-14 13:59:30 -0700983
984 enum KvmIrqChip {
985 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
986 Split(KvmSplitIrqChip),
987 Kernel(KvmKernelIrqChip),
988 }
989
990 impl KvmIrqChip {
991 fn as_mut(&mut self) -> &mut dyn IrqChipArch {
992 match self {
993 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
994 KvmIrqChip::Split(i) => i,
995 KvmIrqChip::Kernel(i) => i,
996 }
997 }
998 }
999
1000 let ioapic_host_tube;
1001 let mut irq_chip = if cfg.split_irqchip {
1002 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
1003 unimplemented!("KVM split irqchip mode only supported on x86 processors");
1004 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1005 {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001006 let (host_tube, ioapic_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerdc748482021-04-14 13:59:30 -07001007 ioapic_host_tube = Some(host_tube);
1008 KvmIrqChip::Split(
1009 KvmSplitIrqChip::new(
1010 vm_clone,
1011 components.vcpu_count,
1012 ioapic_device_tube,
1013 Some(120),
1014 )
Daniel Verkamp6b298582021-08-16 15:37:11 -07001015 .context("failed to create IRQ chip")?,
Zach Reiznerdc748482021-04-14 13:59:30 -07001016 )
1017 }
1018 } else {
1019 ioapic_host_tube = None;
1020 KvmIrqChip::Kernel(
Daniel Verkamp6b298582021-08-16 15:37:11 -07001021 KvmKernelIrqChip::new(vm_clone, components.vcpu_count)
1022 .context("failed to create IRQ chip")?,
Zach Reiznerdc748482021-04-14 13:59:30 -07001023 )
1024 };
1025
1026 run_vm::<KvmVcpu, KvmVm>(cfg, components, vm, irq_chip.as_mut(), ioapic_host_tube)
1027}
1028
1029fn run_vm<Vcpu, V>(
Zach Reiznera90649a2021-03-31 12:56:08 -07001030 cfg: Config,
1031 #[allow(unused_mut)] mut components: VmComponents,
Zach Reiznerdc748482021-04-14 13:59:30 -07001032 mut vm: V,
1033 irq_chip: &mut dyn IrqChipArch,
1034 ioapic_host_tube: Option<Tube>,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001035) -> Result<ExitState>
Zach Reiznera90649a2021-03-31 12:56:08 -07001036where
1037 Vcpu: VcpuArch + 'static,
1038 V: VmArch + 'static,
Zach Reiznera90649a2021-03-31 12:56:08 -07001039{
Alexandre Courbot6a8f6562022-03-24 14:43:48 +09001040 if cfg.jail_config.is_some() {
Zach Reiznera90649a2021-03-31 12:56:08 -07001041 // Printing something to the syslog before entering minijail so that libc's syslogger has a
1042 // chance to open files necessary for its operation, like `/etc/localtime`. After jailing,
1043 // access to those files will not be possible.
1044 info!("crosvm entering multiprocess mode");
1045 }
1046
Daniel Verkampf1439d42021-05-21 13:55:10 -07001047 #[cfg(feature = "usb")]
Zach Reiznera90649a2021-03-31 12:56:08 -07001048 let (usb_control_tube, usb_provider) =
Daniel Verkamp6b298582021-08-16 15:37:11 -07001049 HostBackendDeviceProvider::new().context("failed to create usb provider")?;
Daniel Verkampf1439d42021-05-21 13:55:10 -07001050
Zach Reiznera90649a2021-03-31 12:56:08 -07001051 // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this
1052 // before any jailed devices have been spawned, so that we can catch any of them that fail very
1053 // quickly.
Daniel Verkamp6b298582021-08-16 15:37:11 -07001054 let sigchld_fd = SignalFd::new(libc::SIGCHLD).context("failed to create signalfd")?;
Dylan Reid059a1882018-07-23 17:58:09 -07001055
Zach Reiznera60744b2019-02-13 17:33:32 -08001056 let control_server_socket = match &cfg.socket_path {
1057 Some(path) => Some(UnlinkUnixSeqpacketListener(
Daniel Verkamp6b298582021-08-16 15:37:11 -07001058 UnixSeqpacketListener::bind(path).context("failed to create control server")?,
Zach Reiznera60744b2019-02-13 17:33:32 -08001059 )),
1060 None => None,
Dylan Reid059a1882018-07-23 17:58:09 -07001061 };
Zach Reiznera60744b2019-02-13 17:33:32 -08001062
Zach Reiznera90649a2021-03-31 12:56:08 -07001063 let mut control_tubes = Vec::new();
1064
1065 #[cfg(all(target_arch = "x86_64", feature = "gdb"))]
1066 if let Some(port) = cfg.gdb {
1067 // GDB needs a control socket to interrupt vcpus.
Daniel Verkamp6b298582021-08-16 15:37:11 -07001068 let (gdb_host_tube, gdb_control_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznera90649a2021-03-31 12:56:08 -07001069 control_tubes.push(TaggedControlTube::Vm(gdb_host_tube));
1070 components.gdb = Some((port, gdb_control_tube));
1071 }
1072
Chirantan Ekbote2ee9dcd2021-05-26 18:21:44 +09001073 for wl_cfg in &cfg.vhost_user_wl {
1074 let wayland_host_tube = UnixSeqpacket::connect(&wl_cfg.vm_tube)
1075 .map(Tube::new)
Daniel Verkamp6b298582021-08-16 15:37:11 -07001076 .context("failed to connect to wayland tube")?;
Chirantan Ekbote2ee9dcd2021-05-26 18:21:44 +09001077 control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube));
1078 }
1079
Chirantan Ekbote44292f52021-06-25 18:31:41 +09001080 let mut vhost_user_gpu_tubes = Vec::with_capacity(cfg.vhost_user_gpu.len());
1081 for _ in 0..cfg.vhost_user_gpu.len() {
Alexandre Courbote55b7912022-03-04 16:54:38 +09001082 let (host_control_tube, device_control_tube) =
1083 Tube::pair().context("failed to create tube")?;
1084 let (host_gpu_tube, device_gpu_tube) = Tube::pair().context("failed to create tube")?;
1085 vhost_user_gpu_tubes.push((host_gpu_tube, device_gpu_tube, device_control_tube));
1086 control_tubes.push(TaggedControlTube::VmMemory(host_control_tube));
Chirantan Ekbote44292f52021-06-25 18:31:41 +09001087 }
1088
Daniel Verkamp6b298582021-08-16 15:37:11 -07001089 let (wayland_host_tube, wayland_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001090 control_tubes.push(TaggedControlTube::VmMemory(wayland_host_tube));
Andrew Walbran3cd93602022-01-25 13:59:23 +00001091
1092 let (balloon_host_tube, balloon_device_tube) = if cfg.balloon {
David Stevens8be9ef02022-01-13 22:50:24 +09001093 if let Some(ref path) = cfg.balloon_control {
1094 (
1095 None,
1096 Some(Tube::new(
1097 UnixSeqpacket::connect(path).context("failed to create balloon control")?,
1098 )),
1099 )
1100 } else {
1101 // Balloon gets a special socket so balloon requests can be forwarded
1102 // from the main process.
1103 let (host, device) = Tube::pair().context("failed to create tube")?;
1104 // Set recv timeout to avoid deadlock on sending BalloonControlCommand
1105 // before the guest is ready.
1106 host.set_recv_timeout(Some(Duration::from_millis(100)))
1107 .context("failed to set timeout")?;
1108 (Some(host), Some(device))
1109 }
Andrew Walbran3cd93602022-01-25 13:59:23 +00001110 } else {
1111 (None, None)
1112 };
Dylan Reid059a1882018-07-23 17:58:09 -07001113
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001114 // Create one control socket per disk.
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001115 let mut disk_device_tubes = Vec::new();
1116 let mut disk_host_tubes = Vec::new();
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001117 let disk_count = cfg.disks.len();
1118 for _ in 0..disk_count {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001119 let (disk_host_tub, disk_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001120 disk_host_tubes.push(disk_host_tub);
1121 disk_device_tubes.push(disk_device_tube);
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001122 }
1123
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001124 let mut pmem_device_tubes = Vec::new();
Daniel Verkampe1980a92020-02-07 11:00:55 -08001125 let pmem_count = cfg.pmem_devices.len();
1126 for _ in 0..pmem_count {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001127 let (pmem_host_tube, pmem_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001128 pmem_device_tubes.push(pmem_device_tube);
1129 control_tubes.push(TaggedControlTube::VmMsync(pmem_host_tube));
Daniel Verkampe1980a92020-02-07 11:00:55 -08001130 }
1131
Daniel Verkamp6b298582021-08-16 15:37:11 -07001132 let (gpu_host_tube, gpu_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001133 control_tubes.push(TaggedControlTube::VmMemory(gpu_host_tube));
Gurchetan Singh96beafc2019-05-15 09:46:52 -07001134
Zach Reiznerdc748482021-04-14 13:59:30 -07001135 if let Some(ioapic_host_tube) = ioapic_host_tube {
1136 control_tubes.push(TaggedControlTube::VmIrq(ioapic_host_tube));
1137 }
Zhuocheng Dingf2e90bf2019-12-02 15:50:20 +08001138
Chuanxiao Dongbbb32af2020-04-27 16:37:10 +08001139 let battery = if cfg.battery_type.is_some() {
Daniel Verkampcfe49462021-08-19 17:11:05 -07001140 #[cfg_attr(not(feature = "power-monitor-powerd"), allow(clippy::manual_map))]
Alexandre Courbot6a8f6562022-03-24 14:43:48 +09001141 let jail = match simple_jail(&cfg.jail_config, "battery")? {
Daniel Verkampcfe49462021-08-19 17:11:05 -07001142 #[cfg_attr(not(feature = "power-monitor-powerd"), allow(unused_mut))]
Alex Lauf408c732020-11-10 18:24:04 +09001143 Some(mut jail) => {
1144 // Setup a bind mount to the system D-Bus socket if the powerd monitor is used.
1145 #[cfg(feature = "power-monitor-powerd")]
1146 {
Fergus Dall51200512021-08-19 12:54:26 +10001147 add_current_user_to_jail(&mut jail)?;
Alex Lauf408c732020-11-10 18:24:04 +09001148
1149 // Create a tmpfs in the device's root directory so that we can bind mount files.
1150 jail.mount_with_data(
1151 Path::new("none"),
1152 Path::new("/"),
1153 "tmpfs",
1154 (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize,
1155 "size=67108864",
1156 )?;
1157
1158 let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket");
1159 jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?;
1160 }
1161 Some(jail)
1162 }
1163 None => None,
1164 };
1165 (&cfg.battery_type, jail)
Chuanxiao Dongbbb32af2020-04-27 16:37:10 +08001166 } else {
1167 (&cfg.battery_type, None)
1168 };
1169
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -08001170 let map_request: Arc<Mutex<Option<ExternalMapping>>> = Arc::new(Mutex::new(None));
1171
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09001172 let fs_count = cfg
1173 .shared_dirs
1174 .iter()
1175 .filter(|sd| sd.kind == SharedDirKind::FS)
1176 .count();
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001177 let mut fs_device_tubes = Vec::with_capacity(fs_count);
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09001178 for _ in 0..fs_count {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001179 let (fs_host_tube, fs_device_tube) = Tube::pair().context("failed to create tube")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001180 control_tubes.push(TaggedControlTube::Fs(fs_host_tube));
1181 fs_device_tubes.push(fs_device_tube);
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09001182 }
1183
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -08001184 let mut vvu_proxy_device_tubes = Vec::new();
1185 for _ in 0..cfg.vvu_proxy.len() {
1186 let (vvu_proxy_host_tube, vvu_proxy_device_tube) =
1187 Tube::pair().context("failed to create VVU proxy tube")?;
1188 control_tubes.push(TaggedControlTube::VmMemory(vvu_proxy_host_tube));
1189 vvu_proxy_device_tubes.push(vvu_proxy_device_tube);
1190 }
1191
Daniel Verkamp6b298582021-08-16 15:37:11 -07001192 let exit_evt = Event::new().context("failed to create event")?;
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001193 let reset_evt = Event::new().context("failed to create event")?;
Andrew Walbran1a19c672022-01-24 17:24:10 +00001194 let crash_evt = Event::new().context("failed to create event")?;
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001195 let (panic_rdtube, panic_wrtube) = Tube::pair().context("failed to create tube")?;
Ryo Hashimoto8f9dc1d2021-08-18 19:07:29 +09001196
David Stevense4db4172022-03-10 13:26:04 +09001197 let pstore_size = components.pstore.as_ref().map(|pstore| pstore.size as u64);
David Stevensdbd24182022-03-10 10:53:56 +09001198 let mut sys_allocator = SystemAllocator::new(
1199 Arch::get_system_allocator_config(&vm),
1200 pstore_size,
1201 &cfg.mmio_address_ranges,
1202 )
1203 .context("failed to create system allocator")?;
David Stevense4db4172022-03-10 13:26:04 +09001204
Ryo Hashimoto8f9dc1d2021-08-18 19:07:29 +09001205 let ramoops_region = match &components.pstore {
1206 Some(pstore) => Some(
David Stevense4db4172022-03-10 13:26:04 +09001207 arch::pstore::create_memory_region(
1208 &mut vm,
1209 sys_allocator.reserved_region().unwrap(),
1210 pstore,
1211 )
1212 .context("failed to allocate pstore region")?,
Ryo Hashimoto8f9dc1d2021-08-18 19:07:29 +09001213 ),
1214 None => None,
1215 };
1216
Mattias Nisslerbbd91d02021-12-07 08:57:45 +00001217 create_file_backed_mappings(&cfg, &mut vm, &mut sys_allocator)?;
1218
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -08001219 #[cfg(feature = "gpu")]
1220 // Hold on to the render server jail so it keeps running until we exit run_vm()
Dmitry Torokhove464a7a2022-01-26 13:29:36 -08001221 let (_render_server_jail, render_server_fd) =
1222 if let Some(parameters) = &cfg.gpu_render_server_parameters {
1223 let (jail, fd) = start_gpu_render_server(&cfg, parameters)?;
1224 (Some(ScopedMinijail(jail)), Some(fd))
1225 } else {
1226 (None, None)
1227 };
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -08001228
David Stevens06d157a2022-01-13 23:44:48 +09001229 let init_balloon_size = components
1230 .memory_size
1231 .checked_sub(cfg.init_memory.map_or(components.memory_size, |m| {
1232 m.checked_mul(1024 * 1024).unwrap_or(u64::MAX)
1233 }))
1234 .context("failed to calculate init balloon size")?;
1235
Tomasz Nowicki64f43552022-02-22 14:14:45 +00001236 #[cfg(feature = "direct")]
1237 let mut irqs = Vec::new();
1238
1239 #[cfg(feature = "direct")]
1240 for irq in &cfg.direct_level_irq {
1241 if !sys_allocator.reserve_irq(*irq) {
1242 warn!("irq {} already reserved.", irq);
1243 }
Dmitry Torokhov7b7a2062022-03-19 23:01:33 -07001244 let irq_evt = devices::IrqLevelEvent::new().context("failed to create event")?;
Dmitry Torokhovf021a622022-03-21 11:00:29 -07001245 irq_chip.register_level_irq_event(*irq, &irq_evt).unwrap();
Dmitry Torokhov7b7a2062022-03-19 23:01:33 -07001246 let direct_irq = devices::DirectIrq::new_level(&irq_evt)
Tomasz Nowicki64f43552022-02-22 14:14:45 +00001247 .context("failed to enable interrupt forwarding")?;
1248 direct_irq
1249 .irq_enable(*irq)
1250 .context("failed to enable interrupt forwarding")?;
Dmytro Maluka0ac97972022-04-05 15:25:46 +00001251
1252 if cfg.direct_wake_irq.contains(&irq) {
1253 direct_irq
1254 .irq_wake_enable(*irq)
1255 .context("failed to enable interrupt wake")?;
1256 }
1257
Tomasz Nowicki64f43552022-02-22 14:14:45 +00001258 irqs.push(direct_irq);
1259 }
1260
1261 #[cfg(feature = "direct")]
1262 for irq in &cfg.direct_edge_irq {
1263 if !sys_allocator.reserve_irq(*irq) {
1264 warn!("irq {} already reserved.", irq);
1265 }
Dmitry Torokhov7b7a2062022-03-19 23:01:33 -07001266 let irq_evt = devices::IrqEdgeEvent::new().context("failed to create event")?;
Dmitry Torokhovf021a622022-03-21 11:00:29 -07001267 irq_chip.register_edge_irq_event(*irq, &irq_evt).unwrap();
Dmitry Torokhov7b7a2062022-03-19 23:01:33 -07001268 let direct_irq = devices::DirectIrq::new_edge(&irq_evt)
Tomasz Nowicki64f43552022-02-22 14:14:45 +00001269 .context("failed to enable interrupt forwarding")?;
1270 direct_irq
1271 .irq_enable(*irq)
1272 .context("failed to enable interrupt forwarding")?;
Dmytro Maluka0ac97972022-04-05 15:25:46 +00001273
1274 if cfg.direct_wake_irq.contains(&irq) {
1275 direct_irq
1276 .irq_wake_enable(*irq)
1277 .context("failed to enable interrupt wake")?;
1278 }
1279
Tomasz Nowicki64f43552022-02-22 14:14:45 +00001280 irqs.push(direct_irq);
1281 }
1282
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001283 let mut iommu_attached_endpoints: BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>> =
1284 BTreeMap::new();
Tomasz Nowickiab86d522021-09-22 05:50:46 +00001285 let mut devices = create_devices(
Zach Reiznerdc748482021-04-14 13:59:30 -07001286 &cfg,
1287 &mut vm,
1288 &mut sys_allocator,
1289 &exit_evt,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001290 panic_wrtube,
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001291 &mut iommu_attached_endpoints,
Zach Reiznerdc748482021-04-14 13:59:30 -07001292 &mut control_tubes,
1293 wayland_device_tube,
1294 gpu_device_tube,
Chirantan Ekbote44292f52021-06-25 18:31:41 +09001295 vhost_user_gpu_tubes,
Zach Reiznerdc748482021-04-14 13:59:30 -07001296 balloon_device_tube,
David Stevens06d157a2022-01-13 23:44:48 +09001297 init_balloon_size,
Zach Reiznerdc748482021-04-14 13:59:30 -07001298 &mut disk_device_tubes,
1299 &mut pmem_device_tubes,
1300 &mut fs_device_tubes,
Daniel Verkampf1439d42021-05-21 13:55:10 -07001301 #[cfg(feature = "usb")]
Zach Reiznerdc748482021-04-14 13:59:30 -07001302 usb_provider,
1303 Arc::clone(&map_request),
Dmitry Torokhov9cbe5432022-01-25 19:17:07 -08001304 #[cfg(feature = "gpu")]
1305 render_server_fd,
Abhishek Bhardwaj90fd1642021-11-24 18:26:37 -08001306 &mut vvu_proxy_device_tubes,
David Stevense123e392022-04-07 09:59:25 +09001307 components.memory_size,
Zach Reiznerdc748482021-04-14 13:59:30 -07001308 )?;
1309
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001310 let mut hp_endpoints_ranges: Vec<RangeInclusive<u32>> = Vec::new();
Anton Romanov33334412022-03-22 17:48:18 +00001311 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Xiong Zhangf7874712021-12-24 10:53:59 +08001312 let mut hotplug_buses: Vec<Arc<Mutex<dyn HotPlugBus>>> = Vec::new();
Xiong Zhang1b6e0112022-03-15 11:34:16 +08001313 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1314 let mut gpe_notify_devs: Vec<(u32, Arc<Mutex<dyn GpeNotify>>)> = Vec::new();
Xiong Zhangf7874712021-12-24 10:53:59 +08001315 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1316 {
1317 #[cfg(feature = "direct")]
1318 let rp_host = cfg.pcie_rp.clone();
1319 #[cfg(not(feature = "direct"))]
Xiong Zhang626f0142022-03-12 16:05:17 +08001320 let rp_host: Vec<HostPcieRootPortParameters> = Vec::new();
Xiong Zhangf7874712021-12-24 10:53:59 +08001321
1322 // Create Pcie Root Port
1323 create_pcie_root_port(
1324 rp_host,
1325 &mut sys_allocator,
1326 &mut control_tubes,
1327 &mut devices,
1328 &mut hotplug_buses,
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001329 &mut hp_endpoints_ranges,
Xiong Zhang1b6e0112022-03-15 11:34:16 +08001330 &mut gpe_notify_devs,
Xiong Zhangf7874712021-12-24 10:53:59 +08001331 )?;
1332 }
1333
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001334 let (translate_response_senders, request_rx) = setup_virtio_access_platform(
1335 &mut sys_allocator,
1336 &mut iommu_attached_endpoints,
1337 &mut devices,
1338 )?;
1339
Haiwei Li87bc2fc2022-02-18 14:37:40 +08001340 let iommu_host_tube = if !iommu_attached_endpoints.is_empty() || cfg.virtio_iommu {
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001341 let (iommu_host_tube, iommu_device_tube) = Tube::pair().context("failed to create tube")?;
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001342 let iommu_dev = create_iommu_device(
1343 &cfg,
1344 (1u64 << vm.get_guest_phys_addr_bits()) - 1,
1345 iommu_attached_endpoints,
1346 hp_endpoints_ranges,
1347 translate_response_senders,
1348 request_rx,
1349 iommu_device_tube,
1350 )?;
1351
1352 let (msi_host_tube, msi_device_tube) = Tube::pair().context("failed to create tube")?;
1353 control_tubes.push(TaggedControlTube::VmIrq(msi_host_tube));
1354 let mut dev = VirtioPciDevice::new(vm.get_memory().clone(), iommu_dev.dev, msi_device_tube)
1355 .context("failed to create virtio pci dev")?;
1356 // early reservation for viommu.
1357 dev.allocate_address(&mut sys_allocator)
1358 .context("failed to allocate resources early for virtio pci dev")?;
1359 let dev = Box::new(dev);
1360 devices.push((dev, iommu_dev.jail));
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001361 Some(iommu_host_tube)
1362 } else {
1363 None
1364 };
Haiwei Li09b7b8e2022-02-18 18:16:05 +08001365
Peter Fangc2bba082021-04-19 18:40:24 -07001366 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Tomasz Nowickiab86d522021-09-22 05:50:46 +00001367 for device in devices
1368 .iter_mut()
1369 .filter_map(|(dev, _)| dev.as_pci_device_mut())
1370 {
Peter Fangc2bba082021-04-19 18:40:24 -07001371 let sdts = device
1372 .generate_acpi(components.acpi_sdts)
1373 .or_else(|| {
1374 error!("ACPI table generation error");
1375 None
1376 })
Daniel Verkamp6b298582021-08-16 15:37:11 -07001377 .ok_or_else(|| anyhow!("failed to generate ACPI table"))?;
Peter Fangc2bba082021-04-19 18:40:24 -07001378 components.acpi_sdts = sdts;
1379 }
1380
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +08001381 // KVM_CREATE_VCPU uses apic id for x86 and uses cpu id for others.
1382 let mut kvm_vcpu_ids = Vec::new();
1383
Kuo-Hsin Yang6139da62021-04-14 16:55:24 +08001384 #[cfg_attr(not(feature = "direct"), allow(unused_mut))]
Zach Reiznerdc748482021-04-14 13:59:30 -07001385 let mut linux = Arch::build_vm::<V, Vcpu>(
Trent Begin17ccaad2019-04-17 13:51:25 -06001386 components,
Zach Reiznerdc748482021-04-14 13:59:30 -07001387 &exit_evt,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001388 &reset_evt,
Zach Reiznerdc748482021-04-14 13:59:30 -07001389 &mut sys_allocator,
Trent Begin17ccaad2019-04-17 13:51:25 -06001390 &cfg.serial_parameters,
Alexandre Courbot6a8f6562022-03-24 14:43:48 +09001391 simple_jail(&cfg.jail_config, "serial")?,
Chuanxiao Dongbbb32af2020-04-27 16:37:10 +08001392 battery,
Zach Reiznera90649a2021-03-31 12:56:08 -07001393 vm,
Ryo Hashimoto8f9dc1d2021-08-18 19:07:29 +09001394 ramoops_region,
Tomasz Nowickiab86d522021-09-22 05:50:46 +00001395 devices,
Zach Reiznerdc748482021-04-14 13:59:30 -07001396 irq_chip,
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +08001397 &mut kvm_vcpu_ids,
Trent Begin17ccaad2019-04-17 13:51:25 -06001398 )
Daniel Verkamp6b298582021-08-16 15:37:11 -07001399 .context("the architecture failed to build the vm")?;
Lepton Wu60893882018-11-21 11:06:18 -08001400
Daniel Verkamp1286b482021-11-30 15:14:16 -08001401 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1402 {
Xiong Zhangf7874712021-12-24 10:53:59 +08001403 for hotplug_bus in hotplug_buses.iter() {
1404 linux.hotplug_bus.push(hotplug_bus.clone());
1405 }
Xiong Zhang1b6e0112022-03-15 11:34:16 +08001406
1407 if let Some(pm) = &linux.pm {
1408 while let Some((gpe, notify_dev)) = gpe_notify_devs.pop() {
1409 pm.lock().register_gpe_notify_dev(gpe, notify_dev);
1410 }
1411 }
Daniel Verkamp1286b482021-11-30 15:14:16 -08001412 }
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001413
Tomasz Jeznach3ce74762021-02-26 01:01:53 -08001414 #[cfg(feature = "direct")]
1415 if let Some(pmio) = &cfg.direct_pmio {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001416 let direct_io = Arc::new(
1417 devices::DirectIo::new(&pmio.path, false).context("failed to open direct io device")?,
1418 );
Tomasz Jeznach3ce74762021-02-26 01:01:53 -08001419 for range in pmio.ranges.iter() {
1420 linux
1421 .io_bus
Junichi Uekawab180f9c2021-12-07 09:21:36 +09001422 .insert_sync(direct_io.clone(), range.base, range.len)
Tomasz Jeznach3ce74762021-02-26 01:01:53 -08001423 .unwrap();
1424 }
1425 };
1426
Tomasz Jeznach7271f752021-03-04 01:44:06 -08001427 #[cfg(feature = "direct")]
Tomasz Jeznach9e6c6332021-05-27 21:49:14 -07001428 if let Some(mmio) = &cfg.direct_mmio {
Xiong Zhang46471a02021-11-12 00:34:42 +08001429 let direct_mmio = Arc::new(
Junichi Uekawab180f9c2021-12-07 09:21:36 +09001430 devices::DirectMmio::new(&mmio.path, false, &mmio.ranges)
Xiong Zhang46471a02021-11-12 00:34:42 +08001431 .context("failed to open direct mmio device")?,
Daniel Verkamp6b298582021-08-16 15:37:11 -07001432 );
Xiong Zhang46471a02021-11-12 00:34:42 +08001433
Tomasz Jeznach9e6c6332021-05-27 21:49:14 -07001434 for range in mmio.ranges.iter() {
1435 linux
1436 .mmio_bus
Junichi Uekawab180f9c2021-12-07 09:21:36 +09001437 .insert_sync(direct_mmio.clone(), range.base, range.len)
Tomasz Jeznach9e6c6332021-05-27 21:49:14 -07001438 .unwrap();
1439 }
1440 };
1441
Daniel Verkamp6b298582021-08-16 15:37:11 -07001442 let gralloc = RutabagaGralloc::new().context("failed to create gralloc")?;
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001443 run_control(
1444 linux,
Zach Reiznerdc748482021-04-14 13:59:30 -07001445 sys_allocator,
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001446 cfg,
Zach Reiznera60744b2019-02-13 17:33:32 -08001447 control_server_socket,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001448 control_tubes,
1449 balloon_host_tube,
1450 &disk_host_tubes,
Daniel Verkampf1439d42021-05-21 13:55:10 -07001451 #[cfg(feature = "usb")]
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001452 usb_control_tube,
Zach Reiznerdc748482021-04-14 13:59:30 -07001453 exit_evt,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001454 reset_evt,
Andrew Walbran1a19c672022-01-24 17:24:10 +00001455 crash_evt,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001456 panic_rdtube,
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001457 sigchld_fd,
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -08001458 Arc::clone(&map_request),
Gurchetan Singh293913c2020-12-09 10:44:13 -08001459 gralloc,
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +08001460 kvm_vcpu_ids,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001461 iommu_host_tube,
Daniel Verkamp92f73d72018-12-04 13:17:46 -08001462 )
Dylan Reid0ed91ab2018-05-31 15:42:18 -07001463}
1464
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001465fn get_hp_bus<V: VmArch, Vcpu: VcpuArch>(
1466 linux: &RunnableLinuxVm<V, Vcpu>,
1467 host_addr: PciAddress,
1468) -> Result<(Arc<Mutex<dyn HotPlugBus>>, u8)> {
1469 for hp_bus in linux.hotplug_bus.iter() {
1470 if let Some(number) = hp_bus.lock().is_match(host_addr) {
1471 return Ok((hp_bus.clone(), number));
1472 }
1473 }
1474 Err(anyhow!("Failed to find a suitable hotplug bus"))
1475}
1476
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001477fn add_vfio_device<V: VmArch, Vcpu: VcpuArch>(
1478 linux: &mut RunnableLinuxVm<V, Vcpu>,
1479 sys_allocator: &mut SystemAllocator,
1480 cfg: &Config,
1481 control_tubes: &mut Vec<TaggedControlTube>,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001482 iommu_host_tube: &Option<Tube>,
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001483 vfio_path: &Path,
1484) -> Result<()> {
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001485 let host_os_str = vfio_path
1486 .file_name()
1487 .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?;
1488 let host_str = host_os_str
1489 .to_str()
1490 .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?;
Daniel Verkampaebda742022-04-07 16:37:27 -07001491 let host_addr = PciAddress::from_str(host_str).context("failed to parse vfio pci address")?;
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001492
1493 let (hp_bus, bus_num) = get_hp_bus(linux, host_addr)?;
1494
Woody Chow055b81b2022-01-25 18:34:29 +09001495 let mut endpoints: BTreeMap<u32, Arc<Mutex<Box<dyn MemoryMapperTrait>>>> = BTreeMap::new();
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001496 let (vfio_pci_device, jail) = create_vfio_device(
1497 cfg,
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001498 &linux.vm,
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001499 sys_allocator,
1500 control_tubes,
1501 vfio_path,
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001502 Some(bus_num),
Victor Ding3f749592022-03-18 05:44:20 +00001503 None,
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001504 &mut endpoints,
Chuanxiao Donga8d427b2022-01-07 10:26:24 +08001505 None,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001506 if iommu_host_tube.is_some() {
1507 IommuDevType::VirtioIommu
1508 } else {
1509 IommuDevType::NoIommu
1510 },
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001511 )?;
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001512
1513 let pci_address = Arch::register_pci_device(linux, vfio_pci_device, jail, sys_allocator)
Daniel Verkamp6b298582021-08-16 15:37:11 -07001514 .context("Failed to configure pci hotplug device")?;
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001515
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001516 if let Some(iommu_host_tube) = iommu_host_tube {
1517 let &endpoint_addr = endpoints.iter().next().unwrap().0;
1518 let mapper = endpoints.remove(&endpoint_addr).unwrap();
1519 if let Some(vfio_wrapper) = mapper.lock().as_vfio_wrapper() {
1520 let vfio_container = vfio_wrapper.as_vfio_container();
1521 let descriptor = vfio_container.lock().into_raw_descriptor()?;
1522 let request = VirtioIOMMURequest::VfioCommand(VirtioIOMMUVfioCommand::VfioDeviceAdd {
1523 endpoint_addr,
1524 container: {
1525 // Safe because the descriptor is uniquely owned by `descriptor`.
1526 unsafe { File::from_raw_descriptor(descriptor) }
1527 },
1528 });
1529
1530 match virtio_iommu_request(iommu_host_tube, &request)
1531 .map_err(|_| VirtioIOMMUVfioError::SocketFailed)?
1532 {
1533 VirtioIOMMUResponse::VfioResponse(VirtioIOMMUVfioResult::Ok) => (),
1534 resp => bail!("Unexpected message response: {:?}", resp),
1535 }
1536 };
1537 }
1538
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001539 let host_key = HostHotPlugKey::Vfio { host_addr };
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001540 let mut hp_bus = hp_bus.lock();
1541 hp_bus.add_hotplug_device(host_key, pci_address);
1542 hp_bus.hot_plug(pci_address);
1543 Ok(())
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001544}
1545
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001546fn remove_vfio_device<V: VmArch, Vcpu: VcpuArch>(
1547 linux: &RunnableLinuxVm<V, Vcpu>,
Xiong Zhang2d45b912021-05-13 16:22:25 +08001548 sys_allocator: &mut SystemAllocator,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001549 iommu_host_tube: &Option<Tube>,
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001550 vfio_path: &Path,
1551) -> Result<()> {
Daniel Verkamp6b298582021-08-16 15:37:11 -07001552 let host_os_str = vfio_path
1553 .file_name()
1554 .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?;
1555 let host_str = host_os_str
1556 .to_str()
1557 .ok_or_else(|| anyhow!("failed to parse or find vfio path"))?;
Daniel Verkampaebda742022-04-07 16:37:27 -07001558 let host_addr = PciAddress::from_str(host_str).context("failed to parse vfio pci address")?;
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001559 let host_key = HostHotPlugKey::Vfio { host_addr };
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001560 for hp_bus in linux.hotplug_bus.iter() {
1561 let mut hp_bus_lock = hp_bus.lock();
1562 if let Some(pci_addr) = hp_bus_lock.get_hotplug_device(host_key) {
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001563 if let Some(iommu_host_tube) = iommu_host_tube {
1564 let request =
1565 VirtioIOMMURequest::VfioCommand(VirtioIOMMUVfioCommand::VfioDeviceDel {
1566 endpoint_addr: pci_addr.to_u32(),
1567 });
1568 match virtio_iommu_request(iommu_host_tube, &request)
1569 .map_err(|_| VirtioIOMMUVfioError::SocketFailed)?
1570 {
1571 VirtioIOMMUResponse::VfioResponse(VirtioIOMMUVfioResult::Ok) => (),
1572 resp => bail!("Unexpected message response: {:?}", resp),
1573 }
1574 }
1575
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001576 hp_bus_lock.hot_unplug(pci_addr);
Xiong Zhang2d45b912021-05-13 16:22:25 +08001577 sys_allocator.release_pci(pci_addr.bus, pci_addr.dev, pci_addr.func);
Xiong Zhangf82f2dc2021-05-21 16:54:12 +08001578 return Ok(());
1579 }
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001580 }
1581
Daniel Verkamp6b298582021-08-16 15:37:11 -07001582 Err(anyhow!("HotPlugBus hasn't been implemented"))
Xiong Zhang9fadc3f2021-06-07 14:16:45 +08001583}
Xiong Zhang8c9fe3e2021-04-12 15:07:17 +08001584
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001585fn handle_vfio_command<V: VmArch, Vcpu: VcpuArch>(
1586 linux: &mut RunnableLinuxVm<V, Vcpu>,
1587 sys_allocator: &mut SystemAllocator,
1588 cfg: &Config,
1589 add_tubes: &mut Vec<TaggedControlTube>,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001590 iommu_host_tube: &Option<Tube>,
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001591 vfio_path: &Path,
1592 add: bool,
1593) -> VmResponse {
1594 let ret = if add {
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001595 add_vfio_device(
1596 linux,
1597 sys_allocator,
1598 cfg,
1599 add_tubes,
1600 iommu_host_tube,
1601 vfio_path,
1602 )
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001603 } else {
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001604 remove_vfio_device(linux, sys_allocator, iommu_host_tube, vfio_path)
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001605 };
1606
1607 match ret {
1608 Ok(()) => VmResponse::Ok,
1609 Err(e) => {
1610 error!("hanlde_vfio_command failure: {}", e);
1611 add_tubes.clear();
1612 VmResponse::Err(base::Error::new(libc::EINVAL))
1613 }
1614 }
1615}
1616
Zach Reiznerdc748482021-04-14 13:59:30 -07001617fn run_control<V: VmArch + 'static, Vcpu: VcpuArch + 'static>(
1618 mut linux: RunnableLinuxVm<V, Vcpu>,
1619 mut sys_allocator: SystemAllocator,
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001620 cfg: Config,
Zach Reiznera60744b2019-02-13 17:33:32 -08001621 control_server_socket: Option<UnlinkUnixSeqpacketListener>,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001622 mut control_tubes: Vec<TaggedControlTube>,
Andrew Walbran3cd93602022-01-25 13:59:23 +00001623 balloon_host_tube: Option<Tube>,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001624 disk_host_tubes: &[Tube],
Daniel Verkampf1439d42021-05-21 13:55:10 -07001625 #[cfg(feature = "usb")] usb_control_tube: Tube,
Zach Reiznerdc748482021-04-14 13:59:30 -07001626 exit_evt: Event,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001627 reset_evt: Event,
Andrew Walbran1a19c672022-01-24 17:24:10 +00001628 crash_evt: Event,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001629 panic_rdtube: Tube,
Zach Reizner55a9e502018-10-03 10:22:32 -07001630 sigchld_fd: SignalFd,
Lingfeng Yangd6ac1ab2020-01-31 13:55:35 -08001631 map_request: Arc<Mutex<Option<ExternalMapping>>>,
Gurchetan Singh293913c2020-12-09 10:44:13 -08001632 mut gralloc: RutabagaGralloc,
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +08001633 kvm_vcpu_ids: Vec<usize>,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001634 iommu_host_tube: Option<Tube>,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001635) -> Result<ExitState> {
Zach Reizner5bed0d22018-03-28 02:31:11 -07001636 #[derive(PollToken)]
1637 enum Token {
1638 Exit,
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001639 Reset,
Andrew Walbran1a19c672022-01-24 17:24:10 +00001640 Crash,
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001641 Panic,
Chuanxiao Dong546f01c2020-02-12 21:58:47 +08001642 Suspend,
Zach Reizner5bed0d22018-03-28 02:31:11 -07001643 ChildSignal,
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07001644 IrqFd { index: IrqEventIndex },
Zach Reiznera60744b2019-02-13 17:33:32 -08001645 VmControlServer,
Zach Reizner5bed0d22018-03-28 02:31:11 -07001646 VmControl { index: usize },
Tomasz Nowicki98801002022-02-23 21:00:00 +00001647 DelayedIrqFd,
Zach Reizner5bed0d22018-03-28 02:31:11 -07001648 }
Zach Reizner39aa26b2017-12-12 18:03:23 -08001649
Zach Reizner19ad1f32019-12-12 18:58:50 -08001650 stdin()
Zach Reizner39aa26b2017-12-12 18:03:23 -08001651 .set_raw_mode()
1652 .expect("failed to set terminal raw mode");
1653
Michael Hoylee392c462020-10-07 03:29:24 -07001654 let wait_ctx = WaitContext::build_with(&[
Zach Reiznerdc748482021-04-14 13:59:30 -07001655 (&exit_evt, Token::Exit),
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001656 (&reset_evt, Token::Reset),
Andrew Walbran1a19c672022-01-24 17:24:10 +00001657 (&crash_evt, Token::Crash),
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001658 (&panic_rdtube, Token::Panic),
Chuanxiao Dong546f01c2020-02-12 21:58:47 +08001659 (&linux.suspend_evt, Token::Suspend),
Zach Reiznerb2110be2019-07-23 15:55:03 -07001660 (&sigchld_fd, Token::ChildSignal),
1661 ])
Daniel Verkamp6b298582021-08-16 15:37:11 -07001662 .context("failed to add descriptor to wait context")?;
Zach Reiznerb2110be2019-07-23 15:55:03 -07001663
Zach Reiznera60744b2019-02-13 17:33:32 -08001664 if let Some(socket_server) = &control_server_socket {
Michael Hoylee392c462020-10-07 03:29:24 -07001665 wait_ctx
Zach Reiznera60744b2019-02-13 17:33:32 -08001666 .add(socket_server, Token::VmControlServer)
Daniel Verkamp6b298582021-08-16 15:37:11 -07001667 .context("failed to add descriptor to wait context")?;
Zach Reiznera60744b2019-02-13 17:33:32 -08001668 }
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001669 for (index, socket) in control_tubes.iter().enumerate() {
Michael Hoylee392c462020-10-07 03:29:24 -07001670 wait_ctx
Zach Reizner55a9e502018-10-03 10:22:32 -07001671 .add(socket.as_ref(), Token::VmControl { index })
Daniel Verkamp6b298582021-08-16 15:37:11 -07001672 .context("failed to add descriptor to wait context")?;
Zach Reizner39aa26b2017-12-12 18:03:23 -08001673 }
1674
Steven Richmanf32d0b42020-06-20 21:45:32 -07001675 let events = linux
1676 .irq_chip
1677 .irq_event_tokens()
Daniel Verkamp6b298582021-08-16 15:37:11 -07001678 .context("failed to add descriptor to wait context")?;
Steven Richmanf32d0b42020-06-20 21:45:32 -07001679
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07001680 for (index, _gsi, evt) in events {
Michael Hoylee392c462020-10-07 03:29:24 -07001681 wait_ctx
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07001682 .add(&evt, Token::IrqFd { index })
Daniel Verkamp6b298582021-08-16 15:37:11 -07001683 .context("failed to add descriptor to wait context")?;
Zhuocheng Dingb9f4c9b2019-12-02 15:50:28 +08001684 }
1685
Tomasz Nowicki98801002022-02-23 21:00:00 +00001686 if let Some(delayed_ioapic_irq_trigger) = linux.irq_chip.irq_delayed_event_token()? {
1687 wait_ctx
1688 .add(&delayed_ioapic_irq_trigger, Token::DelayedIrqFd)
1689 .context("failed to add descriptor to wait context")?;
1690 }
1691
Alexandre Courbot6a8f6562022-03-24 14:43:48 +09001692 if cfg.jail_config.is_some() {
Lepton Wu20333e42019-03-14 10:48:03 -07001693 // Before starting VCPUs, in case we started with some capabilities, drop them all.
Daniel Verkamp6b298582021-08-16 15:37:11 -07001694 drop_capabilities().context("failed to drop process capabilities")?;
Lepton Wu20333e42019-03-14 10:48:03 -07001695 }
Dmitry Torokhov71006072019-03-06 10:56:51 -08001696
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001697 #[cfg(all(target_arch = "x86_64", feature = "gdb"))]
1698 // Create a channel for GDB thread.
1699 let (to_gdb_channel, from_vcpu_channel) = if linux.gdb.is_some() {
1700 let (s, r) = mpsc::channel();
1701 (Some(s), Some(r))
1702 } else {
1703 (None, None)
1704 };
1705
Steven Richmanf32d0b42020-06-20 21:45:32 -07001706 let mut vcpu_handles = Vec::with_capacity(linux.vcpu_count);
1707 let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpu_count + 1));
Steven Richmanf32d0b42020-06-20 21:45:32 -07001708 let use_hypervisor_signals = !linux
1709 .vm
1710 .get_hypervisor()
Andrew Walbran985491a2022-01-27 13:47:40 +00001711 .check_capability(HypervisorCap::ImmediateExit);
Anton Romanov5acc0f52022-01-28 00:18:11 +00001712 vcpu::setup_vcpu_signal_handler::<Vcpu>(use_hypervisor_signals)?;
Steven Richmanf32d0b42020-06-20 21:45:32 -07001713
Zach Reizner304e7312020-09-29 16:00:24 -07001714 let vcpus: Vec<Option<_>> = match linux.vcpus.take() {
Andrew Walbran9cfdbd92021-01-11 17:40:34 +00001715 Some(vec) => vec.into_iter().map(Some).collect(),
Steven Richmanf32d0b42020-06-20 21:45:32 -07001716 None => iter::repeat_with(|| None).take(linux.vcpu_count).collect(),
1717 };
Yusuke Sato31e136a2021-08-18 11:51:38 -07001718 // Enable core scheduling before creating vCPUs so that the cookie will be
1719 // shared by all vCPU threads.
1720 // TODO(b/199312402): Avoid enabling core scheduling for the crosvm process
1721 // itself for even better performance. Only vCPUs need the feature.
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001722 if cfg.per_vm_core_scheduling {
Yusuke Sato31e136a2021-08-18 11:51:38 -07001723 if let Err(e) = enable_core_scheduling() {
1724 error!("Failed to enable core scheduling: {}", e);
1725 }
1726 }
Vineeth Pillai2b6855e2022-01-12 16:57:22 +00001727 let vcpu_cgroup_tasks_file = match &cfg.vcpu_cgroup_path {
1728 None => None,
1729 Some(cgroup_path) => {
1730 // Move main process to cgroup_path
1731 let mut f = File::create(&cgroup_path.join("tasks"))?;
1732 f.write_all(process::id().to_string().as_bytes())?;
1733 Some(f)
1734 }
1735 };
Jiyong Park09140202022-04-01 13:04:40 +09001736
1737 #[cfg(target_os = "android")]
1738 android::set_process_profiles(&cfg.task_profiles)?;
1739
Daniel Verkamp94c35272019-09-12 13:31:30 -07001740 for (cpu_id, vcpu) in vcpus.into_iter().enumerate() {
Dylan Reidb0492662019-05-17 14:50:13 -07001741 let (to_vcpu_channel, from_main_channel) = mpsc::channel();
Daniel Verkampc677fb42020-09-08 13:47:49 -07001742 let vcpu_affinity = match linux.vcpu_affinity.clone() {
1743 Some(VcpuAffinity::Global(v)) => v,
1744 Some(VcpuAffinity::PerVcpu(mut m)) => m.remove(&cpu_id).unwrap_or_default(),
1745 None => Default::default(),
1746 };
Anton Romanov5acc0f52022-01-28 00:18:11 +00001747 let handle = vcpu::run_vcpu(
Steven Richmanf32d0b42020-06-20 21:45:32 -07001748 cpu_id,
ZhaoLiu2aaf7ad2021-10-10 18:22:29 +08001749 kvm_vcpu_ids[cpu_id],
Zach Reizner55a9e502018-10-03 10:22:32 -07001750 vcpu,
Daniel Verkamp6b298582021-08-16 15:37:11 -07001751 linux.vm.try_clone().context("failed to clone vm")?,
1752 linux
1753 .irq_chip
1754 .try_box_clone()
1755 .context("failed to clone irqchip")?,
Steven Richmanf32d0b42020-06-20 21:45:32 -07001756 linux.vcpu_count,
Kansho Nishidaab205af2020-08-13 18:17:50 +09001757 linux.rt_cpus.contains(&cpu_id),
Daniel Verkampc677fb42020-09-08 13:47:49 -07001758 vcpu_affinity,
Suleiman Souhlal63630e82021-02-18 11:53:11 +09001759 linux.delay_rt,
Suleiman Souhlal015c3c12020-10-07 14:15:41 +09001760 linux.no_smt,
Zach Reizner55a9e502018-10-03 10:22:32 -07001761 vcpu_thread_barrier.clone(),
Steven Richmanf32d0b42020-06-20 21:45:32 -07001762 linux.has_bios,
Colin Downs-Razouk11bed5e2021-11-02 09:33:14 -07001763 (*linux.io_bus).clone(),
1764 (*linux.mmio_bus).clone(),
Daniel Verkamp6b298582021-08-16 15:37:11 -07001765 exit_evt.try_clone().context("failed to clone event")?,
Andrew Walbranb28ae8e2022-01-17 14:33:10 +00001766 reset_evt.try_clone().context("failed to clone event")?,
Andrew Walbran1a19c672022-01-24 17:24:10 +00001767 crash_evt.try_clone().context("failed to clone event")?,
Steven Richmanf32d0b42020-06-20 21:45:32 -07001768 linux.vm.check_capability(VmCap::PvClockSuspend),
Dylan Reidb0492662019-05-17 14:50:13 -07001769 from_main_channel,
Steven Richmanf32d0b42020-06-20 21:45:32 -07001770 use_hypervisor_signals,
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001771 #[cfg(all(target_arch = "x86_64", feature = "gdb"))]
1772 to_gdb_channel.clone(),
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001773 cfg.per_vm_core_scheduling,
1774 cfg.host_cpu_topology,
Zide Chen344e2432022-01-28 14:58:53 -08001775 cfg.privileged_vm,
Vineeth Pillai2b6855e2022-01-12 16:57:22 +00001776 match vcpu_cgroup_tasks_file {
1777 None => None,
1778 Some(ref f) => Some(
1779 f.try_clone()
1780 .context("failed to clone vcpu cgroup tasks file")?,
1781 ),
1782 },
Junichi Uekawab3a094e2022-03-29 15:41:47 +09001783 cfg.userspace_msr.clone(),
Zach Reizner55a9e502018-10-03 10:22:32 -07001784 )?;
Dylan Reidb0492662019-05-17 14:50:13 -07001785 vcpu_handles.push((handle, to_vcpu_channel));
Dylan Reid059a1882018-07-23 17:58:09 -07001786 }
Steven Richmanf32d0b42020-06-20 21:45:32 -07001787
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001788 #[cfg(all(target_arch = "x86_64", feature = "gdb"))]
1789 // Spawn GDB thread.
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001790 if let Some((gdb_port_num, gdb_control_tube)) = linux.gdb.take() {
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001791 let to_vcpu_channels = vcpu_handles
1792 .iter()
1793 .map(|(_handle, channel)| channel.clone())
1794 .collect();
1795 let target = GdbStub::new(
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001796 gdb_control_tube,
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001797 to_vcpu_channels,
1798 from_vcpu_channel.unwrap(), // Must succeed to unwrap()
1799 );
1800 thread::Builder::new()
1801 .name("gdb".to_owned())
1802 .spawn(move || gdb_thread(target, gdb_port_num))
Daniel Verkamp6b298582021-08-16 15:37:11 -07001803 .context("failed to spawn GDB thread")?;
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001804 };
1805
Dylan Reid059a1882018-07-23 17:58:09 -07001806 vcpu_thread_barrier.wait();
1807
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001808 let mut exit_state = ExitState::Stop;
Charles William Dick54045012021-07-27 19:11:53 +09001809 let mut balloon_stats_id: u64 = 0;
1810
Michael Hoylee392c462020-10-07 03:29:24 -07001811 'wait: loop {
Zach Reizner5bed0d22018-03-28 02:31:11 -07001812 let events = {
Michael Hoylee392c462020-10-07 03:29:24 -07001813 match wait_ctx.wait() {
Zach Reizner39aa26b2017-12-12 18:03:23 -08001814 Ok(v) => v,
1815 Err(e) => {
David Tolnayb4bd00f2019-02-12 17:51:26 -08001816 error!("failed to poll: {}", e);
Zach Reizner39aa26b2017-12-12 18:03:23 -08001817 break;
1818 }
1819 }
1820 };
Zach Reiznera60744b2019-02-13 17:33:32 -08001821
1822 let mut vm_control_indices_to_remove = Vec::new();
Michael Hoylee392c462020-10-07 03:29:24 -07001823 for event in events.iter().filter(|e| e.is_readable) {
1824 match event.token {
Zach Reizner5bed0d22018-03-28 02:31:11 -07001825 Token::Exit => {
Zach Reizner39aa26b2017-12-12 18:03:23 -08001826 info!("vcpu requested shutdown");
Michael Hoylee392c462020-10-07 03:29:24 -07001827 break 'wait;
Zach Reizner39aa26b2017-12-12 18:03:23 -08001828 }
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08001829 Token::Reset => {
1830 info!("vcpu requested reset");
1831 exit_state = ExitState::Reset;
1832 break 'wait;
1833 }
Andrew Walbran1a19c672022-01-24 17:24:10 +00001834 Token::Crash => {
1835 info!("vcpu crashed");
1836 exit_state = ExitState::Crash;
1837 break 'wait;
1838 }
Vineeth Pillai9a3d2dc2022-02-18 14:10:16 +00001839 Token::Panic => {
1840 let mut break_to_wait: bool = true;
1841 match panic_rdtube.recv::<u8>() {
1842 Ok(panic_code) => {
1843 let panic_code = PvPanicCode::from_u8(panic_code);
1844 info!("Guest reported panic [Code: {}]", panic_code);
1845 if panic_code == PvPanicCode::CrashLoaded {
1846 // VM is booting to crash kernel.
1847 break_to_wait = false;
1848 }
1849 }
1850 Err(e) => {
1851 warn!("failed to recv panic event: {} ", e);
1852 }
1853 }
1854 if break_to_wait {
1855 exit_state = ExitState::GuestPanic;
1856 break 'wait;
1857 }
1858 }
Chuanxiao Dong546f01c2020-02-12 21:58:47 +08001859 Token::Suspend => {
1860 info!("VM requested suspend");
1861 linux.suspend_evt.read().unwrap();
Anton Romanov5acc0f52022-01-28 00:18:11 +00001862 vcpu::kick_all_vcpus(
Zach Reiznerdc748482021-04-14 13:59:30 -07001863 &vcpu_handles,
1864 linux.irq_chip.as_irq_chip(),
Daniel Verkamp29409802021-02-24 14:46:19 -08001865 VcpuControl::RunState(VmRunMode::Suspending),
Zach Reiznerdc748482021-04-14 13:59:30 -07001866 );
Chuanxiao Dong546f01c2020-02-12 21:58:47 +08001867 }
Zach Reizner5bed0d22018-03-28 02:31:11 -07001868 Token::ChildSignal => {
Zach Reizner39aa26b2017-12-12 18:03:23 -08001869 // Print all available siginfo structs, then exit the loop.
Daniel Verkamp6b298582021-08-16 15:37:11 -07001870 while let Some(siginfo) =
1871 sigchld_fd.read().context("failed to create signalfd")?
1872 {
Zach Reizner3ba00982019-01-23 19:04:43 -08001873 let pid = siginfo.ssi_pid;
1874 let pid_label = match linux.pid_debug_label_map.get(&pid) {
1875 Some(label) => format!("{} (pid {})", label, pid),
1876 None => format!("pid {}", pid),
1877 };
David Tolnayf5032762018-12-03 10:46:45 -08001878 error!(
1879 "child {} died: signo {}, status {}, code {}",
Zach Reizner3ba00982019-01-23 19:04:43 -08001880 pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code
David Tolnayf5032762018-12-03 10:46:45 -08001881 );
Zach Reizner39aa26b2017-12-12 18:03:23 -08001882 }
Michael Hoylee392c462020-10-07 03:29:24 -07001883 break 'wait;
Zach Reizner39aa26b2017-12-12 18:03:23 -08001884 }
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07001885 Token::IrqFd { index } => {
1886 if let Err(e) = linux.irq_chip.service_irq_event(index) {
1887 error!("failed to signal irq {}: {}", index, e);
Zhuocheng Dingb9f4c9b2019-12-02 15:50:28 +08001888 }
1889 }
Tomasz Nowicki98801002022-02-23 21:00:00 +00001890 Token::DelayedIrqFd => {
1891 if let Err(e) = linux.irq_chip.process_delayed_irq_events() {
1892 warn!("can't deliver delayed irqs: {}", e);
1893 }
1894 }
Zach Reiznera60744b2019-02-13 17:33:32 -08001895 Token::VmControlServer => {
1896 if let Some(socket_server) = &control_server_socket {
1897 match socket_server.accept() {
1898 Ok(socket) => {
Michael Hoylee392c462020-10-07 03:29:24 -07001899 wait_ctx
Zach Reiznera60744b2019-02-13 17:33:32 -08001900 .add(
1901 &socket,
1902 Token::VmControl {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001903 index: control_tubes.len(),
Zach Reiznera60744b2019-02-13 17:33:32 -08001904 },
1905 )
Daniel Verkamp6b298582021-08-16 15:37:11 -07001906 .context("failed to add descriptor to wait context")?;
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001907 control_tubes.push(TaggedControlTube::Vm(Tube::new(socket)));
Zach Reiznera60744b2019-02-13 17:33:32 -08001908 }
1909 Err(e) => error!("failed to accept socket: {}", e),
1910 }
1911 }
1912 }
Zach Reizner5bed0d22018-03-28 02:31:11 -07001913 Token::VmControl { index } => {
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001914 let mut add_tubes = Vec::new();
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001915 if let Some(socket) = control_tubes.get(index) {
Jakub Starond99cd0a2019-04-11 14:09:39 -07001916 match socket {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001917 TaggedControlTube::Vm(tube) => match tube.recv::<VmRequest>() {
Jakub Starond99cd0a2019-04-11 14:09:39 -07001918 Ok(request) => {
1919 let mut run_mode_opt = None;
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001920 let response = match request {
1921 VmRequest::VfioCommand { vfio_path, add } => {
1922 handle_vfio_command(
1923 &mut linux,
1924 &mut sys_allocator,
1925 &cfg,
1926 &mut add_tubes,
Haiwei Lie2dffbf2022-02-18 14:30:56 +08001927 &iommu_host_tube,
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001928 &vfio_path,
1929 add,
1930 )
1931 }
1932 _ => request.execute(
1933 &mut run_mode_opt,
Andrew Walbran3cd93602022-01-25 13:59:23 +00001934 balloon_host_tube.as_ref(),
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001935 &mut balloon_stats_id,
1936 disk_host_tubes,
Peter Fang6ca03232021-12-20 02:17:21 -08001937 &mut linux.pm,
Xiong Zhangc78e72b2021-04-08 11:31:41 +08001938 #[cfg(feature = "usb")]
1939 Some(&usb_control_tube),
1940 #[cfg(not(feature = "usb"))]
1941 None,
1942 &mut linux.bat_control,
1943 &vcpu_handles,
1944 ),
1945 };
1946
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001947 if let Err(e) = tube.send(&response) {
Jakub Starond99cd0a2019-04-11 14:09:39 -07001948 error!("failed to send VmResponse: {}", e);
1949 }
1950 if let Some(run_mode) = run_mode_opt {
1951 info!("control socket changed run mode to {}", run_mode);
1952 match run_mode {
1953 VmRunMode::Exiting => {
Michael Hoylee392c462020-10-07 03:29:24 -07001954 break 'wait;
Jakub Starond99cd0a2019-04-11 14:09:39 -07001955 }
Keiichi Watanabec5262e92020-10-21 15:57:33 +09001956 other => {
Chuanxiao Dong2bbe85c2020-11-12 17:18:07 +08001957 if other == VmRunMode::Running {
Daniel Verkampda4e8a92021-07-21 13:49:02 -07001958 for dev in &linux.resume_notify_devices {
1959 dev.lock().resume_imminent();
1960 }
Chuanxiao Dong546f01c2020-02-12 21:58:47 +08001961 }
Anton Romanov5acc0f52022-01-28 00:18:11 +00001962 vcpu::kick_all_vcpus(
Steven Richman11dc6712020-09-02 15:39:14 -07001963 &vcpu_handles,
Zach Reiznerdc748482021-04-14 13:59:30 -07001964 linux.irq_chip.as_irq_chip(),
Daniel Verkamp29409802021-02-24 14:46:19 -08001965 VcpuControl::RunState(other),
Steven Richman11dc6712020-09-02 15:39:14 -07001966 );
Zach Reizner6a8fdd92019-01-16 14:38:41 -08001967 }
1968 }
1969 }
Zach Reizner5bed0d22018-03-28 02:31:11 -07001970 }
Jakub Starond99cd0a2019-04-11 14:09:39 -07001971 Err(e) => {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001972 if let TubeError::Disconnected = e {
Jakub Starond99cd0a2019-04-11 14:09:39 -07001973 vm_control_indices_to_remove.push(index);
1974 } else {
1975 error!("failed to recv VmRequest: {}", e);
1976 }
Zach Reiznera60744b2019-02-13 17:33:32 -08001977 }
Jakub Starond99cd0a2019-04-11 14:09:39 -07001978 },
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001979 TaggedControlTube::VmMemory(tube) => {
1980 match tube.recv::<VmMemoryRequest>() {
1981 Ok(request) => {
1982 let response = request.execute(
1983 &mut linux.vm,
Zach Reiznerdc748482021-04-14 13:59:30 -07001984 &mut sys_allocator,
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08001985 Arc::clone(&map_request),
1986 &mut gralloc,
1987 );
1988 if let Err(e) = tube.send(&response) {
1989 error!("failed to send VmMemoryControlResponse: {}", e);
1990 }
1991 }
1992 Err(e) => {
1993 if let TubeError::Disconnected = e {
1994 vm_control_indices_to_remove.push(index);
1995 } else {
1996 error!("failed to recv VmMemoryControlRequest: {}", e);
1997 }
Jakub Starond99cd0a2019-04-11 14:09:39 -07001998 }
1999 }
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002000 }
2001 TaggedControlTube::VmIrq(tube) => match tube.recv::<VmIrqRequest>() {
Xiong Zhang2515b752019-09-19 10:29:02 +08002002 Ok(request) => {
Steven Richmanf32d0b42020-06-20 21:45:32 -07002003 let response = {
2004 let irq_chip = &mut linux.irq_chip;
2005 request.execute(
2006 |setup| match setup {
Vikram Auradkar0953c582022-03-21 17:33:54 -07002007 IrqSetup::Event(irq, ev, _, _, _) => {
Dmitry Torokhovf021a622022-03-21 11:00:29 -07002008 let irq_evt = devices::IrqEdgeEvent::from_event(ev.try_clone()?);
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07002009 if let Some(event_index) = irq_chip
Dmitry Torokhovf021a622022-03-21 11:00:29 -07002010 .register_edge_irq_event(irq, &irq_evt)?
Colin Downs-Razoukbd532762020-09-08 15:49:35 -07002011 {
2012 match wait_ctx.add(
2013 ev,
2014 Token::IrqFd {
2015 index: event_index
2016 },
2017 ) {
2018 Err(e) => {
2019 warn!("failed to add IrqFd to poll context: {}", e);
2020 Err(e)
2021 },
2022 Ok(_) => {
2023 Ok(())
2024 }
2025 }
2026 } else {
2027 Ok(())
2028 }
Steven Richmanf32d0b42020-06-20 21:45:32 -07002029 }
2030 IrqSetup::Route(route) => irq_chip.route_irq(route),
Dmitry Torokhovf021a622022-03-21 11:00:29 -07002031 IrqSetup::UnRegister(irq, ev) => {
2032 let irq_evt = devices::IrqEdgeEvent::from_event(ev.try_clone()?);
2033 irq_chip.unregister_edge_irq_event(irq, &irq_evt)
2034 }
Steven Richmanf32d0b42020-06-20 21:45:32 -07002035 },
Zach Reiznerdc748482021-04-14 13:59:30 -07002036 &mut sys_allocator,
Steven Richmanf32d0b42020-06-20 21:45:32 -07002037 )
2038 };
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002039 if let Err(e) = tube.send(&response) {
Xiong Zhang2515b752019-09-19 10:29:02 +08002040 error!("failed to send VmIrqResponse: {}", e);
2041 }
2042 }
2043 Err(e) => {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002044 if let TubeError::Disconnected = e {
Xiong Zhang2515b752019-09-19 10:29:02 +08002045 vm_control_indices_to_remove.push(index);
2046 } else {
2047 error!("failed to recv VmIrqRequest: {}", e);
2048 }
2049 }
2050 },
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002051 TaggedControlTube::VmMsync(tube) => {
2052 match tube.recv::<VmMsyncRequest>() {
2053 Ok(request) => {
2054 let response = request.execute(&mut linux.vm);
2055 if let Err(e) = tube.send(&response) {
2056 error!("failed to send VmMsyncResponse: {}", e);
2057 }
2058 }
2059 Err(e) => {
2060 if let TubeError::Disconnected = e {
2061 vm_control_indices_to_remove.push(index);
2062 } else {
2063 error!("failed to recv VmMsyncRequest: {}", e);
2064 }
Daniel Verkampe1980a92020-02-07 11:00:55 -08002065 }
2066 }
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002067 }
2068 TaggedControlTube::Fs(tube) => match tube.recv::<FsMappingRequest>() {
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09002069 Ok(request) => {
2070 let response =
Zach Reiznerdc748482021-04-14 13:59:30 -07002071 request.execute(&mut linux.vm, &mut sys_allocator);
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002072 if let Err(e) = tube.send(&response) {
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09002073 error!("failed to send VmResponse: {}", e);
2074 }
2075 }
2076 Err(e) => {
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002077 if let TubeError::Disconnected = e {
Keiichi Watanabeeefe7fb2020-11-17 17:58:35 +09002078 vm_control_indices_to_remove.push(index);
2079 } else {
2080 error!("failed to recv VmResponse: {}", e);
2081 }
2082 }
2083 },
Zach Reizner39aa26b2017-12-12 18:03:23 -08002084 }
Zach Reizner39aa26b2017-12-12 18:03:23 -08002085 }
Xiong Zhangc78e72b2021-04-08 11:31:41 +08002086 if !add_tubes.is_empty() {
2087 for (idx, socket) in add_tubes.iter().enumerate() {
2088 wait_ctx
2089 .add(
2090 socket.as_ref(),
2091 Token::VmControl {
2092 index: idx + control_tubes.len(),
2093 },
2094 )
2095 .context(
2096 "failed to add hotplug vfio-pci descriptor ot wait context",
2097 )?;
2098 }
2099 control_tubes.append(&mut add_tubes);
2100 }
Zach Reizner39aa26b2017-12-12 18:03:23 -08002101 }
Zach Reizner5bed0d22018-03-28 02:31:11 -07002102 }
2103 }
Zach Reiznera60744b2019-02-13 17:33:32 -08002104
Vikram Auradkarede68c72021-07-01 14:33:54 -07002105 // It's possible more data is readable and buffered while the socket is hungup,
2106 // so don't delete the tube from the poll context until we're sure all the
2107 // data is read.
2108 // Below case covers a condition where we have received a hungup event and the tube is not
2109 // readable.
2110 // In case of readable tube, once all data is read, any attempt to read more data on hungup
2111 // tube should fail. On such failure, we get Disconnected error and index gets added to
2112 // vm_control_indices_to_remove by the time we reach here.
2113 for event in events.iter().filter(|e| e.is_hungup && !e.is_readable) {
2114 if let Token::VmControl { index } = event.token {
2115 vm_control_indices_to_remove.push(index);
Zach Reizner39aa26b2017-12-12 18:03:23 -08002116 }
2117 }
Zach Reiznera60744b2019-02-13 17:33:32 -08002118
2119 // Sort in reverse so the highest indexes are removed first. This removal algorithm
Zide Chen89584072019-11-14 10:33:51 -08002120 // preserves correct indexes as each element is removed.
Daniel Verkamp8c2f0002020-08-31 15:13:35 -07002121 vm_control_indices_to_remove.sort_unstable_by_key(|&k| Reverse(k));
Zach Reiznera60744b2019-02-13 17:33:32 -08002122 vm_control_indices_to_remove.dedup();
2123 for index in vm_control_indices_to_remove {
Michael Hoylee392c462020-10-07 03:29:24 -07002124 // Delete the socket from the `wait_ctx` synchronously. Otherwise, the kernel will do
2125 // this automatically when the FD inserted into the `wait_ctx` is closed after this
Zide Chen89584072019-11-14 10:33:51 -08002126 // if-block, but this removal can be deferred unpredictably. In some instances where the
Michael Hoylee392c462020-10-07 03:29:24 -07002127 // system is under heavy load, we can even get events returned by `wait_ctx` for an FD
Zide Chen89584072019-11-14 10:33:51 -08002128 // that has already been closed. Because the token associated with that spurious event
2129 // now belongs to a different socket, the control loop will start to interact with
2130 // sockets that might not be ready to use. This can cause incorrect hangup detection or
2131 // blocking on a socket that will never be ready. See also: crbug.com/1019986
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002132 if let Some(socket) = control_tubes.get(index) {
Daniel Verkamp6b298582021-08-16 15:37:11 -07002133 wait_ctx
2134 .delete(socket)
2135 .context("failed to remove descriptor from wait context")?;
Zide Chen89584072019-11-14 10:33:51 -08002136 }
2137
2138 // This line implicitly drops the socket at `index` when it gets returned by
2139 // `swap_remove`. After this line, the socket at `index` is not the one from
2140 // `vm_control_indices_to_remove`. Because of this socket's change in index, we need to
Michael Hoylee392c462020-10-07 03:29:24 -07002141 // use `wait_ctx.modify` to change the associated index in its `Token::VmControl`.
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002142 control_tubes.swap_remove(index);
2143 if let Some(tube) = control_tubes.get(index) {
Michael Hoylee392c462020-10-07 03:29:24 -07002144 wait_ctx
Zach Reiznerd49bcdb2021-01-07 08:30:28 -08002145 .modify(tube, EventType::Read, Token::VmControl { index })
Daniel Verkamp6b298582021-08-16 15:37:11 -07002146 .context("failed to add descriptor to wait context")?;
Zach Reiznera60744b2019-02-13 17:33:32 -08002147 }
2148 }
Zach Reizner39aa26b2017-12-12 18:03:23 -08002149 }
2150
Anton Romanov5acc0f52022-01-28 00:18:11 +00002151 vcpu::kick_all_vcpus(
Zach Reiznerdc748482021-04-14 13:59:30 -07002152 &vcpu_handles,
2153 linux.irq_chip.as_irq_chip(),
Daniel Verkamp29409802021-02-24 14:46:19 -08002154 VcpuControl::RunState(VmRunMode::Exiting),
Zach Reiznerdc748482021-04-14 13:59:30 -07002155 );
Steven Richman11dc6712020-09-02 15:39:14 -07002156 for (handle, _) in vcpu_handles {
2157 if let Err(e) = handle.join() {
2158 error!("failed to join vcpu thread: {:?}", e);
Zach Reizner39aa26b2017-12-12 18:03:23 -08002159 }
2160 }
2161
Daniel Verkamp94c35272019-09-12 13:31:30 -07002162 // Explicitly drop the VM structure here to allow the devices to clean up before the
2163 // control sockets are closed when this function exits.
2164 mem::drop(linux);
2165
Zach Reizner19ad1f32019-12-12 18:58:50 -08002166 stdin()
Zach Reizner39aa26b2017-12-12 18:03:23 -08002167 .set_canon_mode()
2168 .expect("failed to restore canonical mode for terminal");
2169
Dmitry Torokhovf75699f2021-12-03 11:19:13 -08002170 Ok(exit_state)
Zach Reizner39aa26b2017-12-12 18:03:23 -08002171}
Daniel Verkamp5586ff52022-02-24 16:34:55 -08002172
2173#[cfg(test)]
2174mod tests {
2175 use super::*;
Xiong Zhang626f0142022-03-12 16:05:17 +08002176 use std::path::PathBuf;
Daniel Verkamp5586ff52022-02-24 16:34:55 -08002177
2178 // Create a file-backed mapping parameters struct with the given `address` and `size` and other
2179 // parameters set to default values.
2180 fn test_file_backed_mapping(address: u64, size: u64) -> FileBackedMappingParameters {
2181 FileBackedMappingParameters {
2182 address,
2183 size,
2184 path: PathBuf::new(),
2185 offset: 0,
2186 writable: false,
2187 sync: false,
2188 }
2189 }
2190
2191 #[test]
2192 fn guest_mem_file_backed_mappings_overlap() {
2193 // Base case: no file mappings; output layout should be identical.
2194 assert_eq!(
2195 punch_holes_in_guest_mem_layout_for_mappings(
2196 vec![
2197 (GuestAddress(0), 0xD000_0000),
2198 (GuestAddress(0x1_0000_0000), 0x8_0000),
2199 ],
2200 &[]
2201 ),
2202 vec![
2203 (GuestAddress(0), 0xD000_0000),
2204 (GuestAddress(0x1_0000_0000), 0x8_0000),
2205 ]
2206 );
2207
2208 // File mapping that does not overlap guest memory.
2209 assert_eq!(
2210 punch_holes_in_guest_mem_layout_for_mappings(
2211 vec![
2212 (GuestAddress(0), 0xD000_0000),
2213 (GuestAddress(0x1_0000_0000), 0x8_0000),
2214 ],
2215 &[test_file_backed_mapping(0xD000_0000, 0x1000)]
2216 ),
2217 vec![
2218 (GuestAddress(0), 0xD000_0000),
2219 (GuestAddress(0x1_0000_0000), 0x8_0000),
2220 ]
2221 );
2222
2223 // File mapping at the start of the low address space region.
2224 assert_eq!(
2225 punch_holes_in_guest_mem_layout_for_mappings(
2226 vec![
2227 (GuestAddress(0), 0xD000_0000),
2228 (GuestAddress(0x1_0000_0000), 0x8_0000),
2229 ],
2230 &[test_file_backed_mapping(0, 0x2000)]
2231 ),
2232 vec![
2233 (GuestAddress(0x2000), 0xD000_0000 - 0x2000),
2234 (GuestAddress(0x1_0000_0000), 0x8_0000),
2235 ]
2236 );
2237
2238 // File mapping at the end of the low address space region.
2239 assert_eq!(
2240 punch_holes_in_guest_mem_layout_for_mappings(
2241 vec![
2242 (GuestAddress(0), 0xD000_0000),
2243 (GuestAddress(0x1_0000_0000), 0x8_0000),
2244 ],
2245 &[test_file_backed_mapping(0xD000_0000 - 0x2000, 0x2000)]
2246 ),
2247 vec![
2248 (GuestAddress(0), 0xD000_0000 - 0x2000),
2249 (GuestAddress(0x1_0000_0000), 0x8_0000),
2250 ]
2251 );
2252
2253 // File mapping fully contained within the middle of the low address space region.
2254 assert_eq!(
2255 punch_holes_in_guest_mem_layout_for_mappings(
2256 vec![
2257 (GuestAddress(0), 0xD000_0000),
2258 (GuestAddress(0x1_0000_0000), 0x8_0000),
2259 ],
2260 &[test_file_backed_mapping(0x1000, 0x2000)]
2261 ),
2262 vec![
2263 (GuestAddress(0), 0x1000),
2264 (GuestAddress(0x3000), 0xD000_0000 - 0x3000),
2265 (GuestAddress(0x1_0000_0000), 0x8_0000),
2266 ]
2267 );
2268
2269 // File mapping at the start of the high address space region.
2270 assert_eq!(
2271 punch_holes_in_guest_mem_layout_for_mappings(
2272 vec![
2273 (GuestAddress(0), 0xD000_0000),
2274 (GuestAddress(0x1_0000_0000), 0x8_0000),
2275 ],
2276 &[test_file_backed_mapping(0x1_0000_0000, 0x2000)]
2277 ),
2278 vec![
2279 (GuestAddress(0), 0xD000_0000),
2280 (GuestAddress(0x1_0000_2000), 0x8_0000 - 0x2000),
2281 ]
2282 );
2283
2284 // File mapping at the end of the high address space region.
2285 assert_eq!(
2286 punch_holes_in_guest_mem_layout_for_mappings(
2287 vec![
2288 (GuestAddress(0), 0xD000_0000),
2289 (GuestAddress(0x1_0000_0000), 0x8_0000),
2290 ],
2291 &[test_file_backed_mapping(0x1_0008_0000 - 0x2000, 0x2000)]
2292 ),
2293 vec![
2294 (GuestAddress(0), 0xD000_0000),
2295 (GuestAddress(0x1_0000_0000), 0x8_0000 - 0x2000),
2296 ]
2297 );
2298
2299 // File mapping fully contained within the middle of the high address space region.
2300 assert_eq!(
2301 punch_holes_in_guest_mem_layout_for_mappings(
2302 vec![
2303 (GuestAddress(0), 0xD000_0000),
2304 (GuestAddress(0x1_0000_0000), 0x8_0000),
2305 ],
2306 &[test_file_backed_mapping(0x1_0000_1000, 0x2000)]
2307 ),
2308 vec![
2309 (GuestAddress(0), 0xD000_0000),
2310 (GuestAddress(0x1_0000_0000), 0x1000),
2311 (GuestAddress(0x1_0000_3000), 0x8_0000 - 0x3000),
2312 ]
2313 );
2314
2315 // File mapping overlapping two guest memory regions.
2316 assert_eq!(
2317 punch_holes_in_guest_mem_layout_for_mappings(
2318 vec![
2319 (GuestAddress(0), 0xD000_0000),
2320 (GuestAddress(0x1_0000_0000), 0x8_0000),
2321 ],
2322 &[test_file_backed_mapping(0xA000_0000, 0x60002000)]
2323 ),
2324 vec![
2325 (GuestAddress(0), 0xA000_0000),
2326 (GuestAddress(0x1_0000_2000), 0x8_0000 - 0x2000),
2327 ]
2328 );
2329 }
2330}