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