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 | |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 5 | use std::cmp::{max, min, Reverse}; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 6 | use std::convert::TryFrom; |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 7 | #[cfg(feature = "gpu")] |
| 8 | use std::env; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 9 | use std::error::Error as StdError; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 10 | use std::ffi::CStr; |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 11 | use std::fmt::{self, Display}; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 12 | use std::fs::{File, OpenOptions}; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 13 | use std::io::{self, stdin, Read}; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 14 | use std::iter; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 15 | use std::mem; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 16 | use std::net::Ipv4Addr; |
Daniel Verkamp | 6f9215c | 2019-08-20 09:41:22 -0700 | [diff] [blame] | 17 | #[cfg(feature = "gpu")] |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 18 | use std::num::NonZeroU8; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 19 | use std::num::ParseIntError; |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 20 | use std::os::unix::io::FromRawFd; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 21 | use std::os::unix::net::UnixStream; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 22 | use std::path::{Path, PathBuf}; |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 23 | use std::ptr; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 24 | use std::str; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 25 | use std::sync::{mpsc, Arc, Barrier}; |
| 26 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 27 | use std::thread; |
| 28 | use std::thread::JoinHandle; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 29 | use std::time::Duration; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 30 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 31 | use libc::{self, c_int, gid_t, uid_t}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 32 | |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 33 | use acpi_tables::sdt::SDT; |
| 34 | |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 35 | use base::net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener}; |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 36 | use devices::virtio::vhost::user::{ |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 37 | Block as VhostUserBlock, Error as VhostUserError, Fs as VhostUserFs, Net as VhostUserNet, |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 38 | }; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 39 | #[cfg(feature = "gpu")] |
| 40 | use devices::virtio::EventDevice; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 41 | use devices::virtio::{self, Console, VirtioDevice}; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 42 | #[cfg(feature = "audio")] |
| 43 | use devices::Ac97Dev; |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 44 | use devices::{ |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 45 | self, HostBackendDeviceProvider, IrqChip, IrqEventIndex, KvmKernelIrqChip, PciDevice, |
| 46 | VcpuRunState, VfioContainer, VfioDevice, VfioPciDevice, VirtioPciDevice, XhciController, |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 47 | }; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 48 | use hypervisor::kvm::{Kvm, KvmVcpu, KvmVm}; |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 49 | use hypervisor::{HypervisorCap, Vcpu, VcpuExit, VcpuRunHandle, Vm, VmCap}; |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 50 | use minijail::{self, Minijail}; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 51 | use msg_socket::{MsgError, MsgReceiver, MsgSender, MsgSocket}; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 52 | use net_util::{Error as NetError, MacAddress, Tap}; |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 53 | use remain::sorted; |
Xiong Zhang | 87a3b44 | 2019-10-29 17:32:44 +0800 | [diff] [blame] | 54 | use resources::{Alloc, MmioType, SystemAllocator}; |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 55 | use rutabaga_gfx::RutabagaGralloc; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 56 | use sync::Mutex; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 57 | |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 58 | use base::{ |
David Tolnay | 633426a | 2019-04-12 12:18:35 -0700 | [diff] [blame] | 59 | self, block_signal, clear_signal, drop_capabilities, error, flock, get_blocked_signals, |
Fletcher Woodruff | 82ff397 | 2019-10-02 13:11:34 -0600 | [diff] [blame] | 60 | get_group_id, get_user_id, getegid, geteuid, info, register_rt_signal_handler, |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 61 | set_cpu_affinity, set_rt_prio_limit, set_rt_round_robin, signal, validate_raw_descriptor, warn, |
| 62 | AsRawDescriptor, Event, EventType, ExternalMapping, FlockOperation, FromRawDescriptor, |
| 63 | Killable, MemoryMappingArena, PollToken, Protection, RawDescriptor, ScopedEvent, SignalFd, |
| 64 | Terminal, Timer, WaitContext, SIGRTMIN, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 65 | }; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 66 | use vm_control::{ |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 67 | BalloonControlCommand, BalloonControlRequestSocket, BalloonControlResponseSocket, |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 68 | BalloonControlResult, BalloonStats, DiskControlCommand, DiskControlRequestSocket, |
| 69 | DiskControlResponseSocket, DiskControlResult, FsMappingRequest, FsMappingRequestSocket, |
| 70 | FsMappingResponseSocket, IrqSetup, UsbControlSocket, VcpuControl, VmControlResponseSocket, |
| 71 | VmIrqRequest, VmIrqRequestSocket, VmIrqResponse, VmIrqResponseSocket, |
| 72 | VmMemoryControlRequestSocket, VmMemoryControlResponseSocket, VmMemoryRequest, VmMemoryResponse, |
| 73 | VmMsyncRequest, VmMsyncRequestSocket, VmMsyncResponse, VmMsyncResponseSocket, VmResponse, |
| 74 | VmRunMode, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 75 | }; |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 76 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 77 | use vm_control::{VcpuDebug, VcpuDebugStatus, VcpuDebugStatusMessage, VmRequest}; |
Dylan Reid | ec058d6 | 2020-07-20 20:21:11 -0700 | [diff] [blame] | 78 | use vm_memory::{GuestAddress, GuestMemory}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 79 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 80 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 81 | use crate::gdb::{gdb_thread, GdbStub}; |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 82 | use crate::{ |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 83 | Config, DiskOption, Executable, SharedDir, SharedDirKind, TouchDeviceOption, VhostUserFsOption, |
| 84 | VhostUserOption, |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 85 | }; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 86 | use arch::{ |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 87 | self, LinuxArch, RunnableLinuxVm, SerialHardware, SerialParameters, VcpuAffinity, |
| 88 | VirtioDeviceStub, VmComponents, VmImage, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 89 | }; |
Sonny Rao | ed517d1 | 2018-02-13 22:09:43 -0800 | [diff] [blame] | 90 | |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 91 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 92 | use { |
| 93 | aarch64::AArch64 as Arch, |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 94 | devices::IrqChipAArch64 as IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 95 | hypervisor::{VcpuAArch64 as VcpuArch, VmAArch64 as VmArch}, |
| 96 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 97 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 98 | use { |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 99 | devices::{IrqChipX86_64 as IrqChipArch, KvmSplitIrqChip}, |
| 100 | hypervisor::{VcpuX86_64 as VcpuArch, VmX86_64 as VmArch}, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 101 | x86_64::X8664arch as Arch, |
| 102 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 103 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 104 | #[sorted] |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 105 | #[derive(Debug)] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 106 | pub enum Error { |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 107 | AddGpuDeviceMemory(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 108 | AddIrqChipVcpu(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 109 | AddPmemDeviceMemory(base::Error), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 110 | AllocateGpuDeviceAddress, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 111 | AllocatePmemDeviceAddress(resources::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 112 | BalloonActualTooLarge, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 113 | BalloonDeviceNew(virtio::BalloonError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 114 | BlockDeviceNew(base::Error), |
| 115 | BlockSignal(base::signal::Error), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 116 | BuildVm(<Arch as LinuxArch>::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 117 | ChownTpmStorage(base::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 118 | CloneEvent(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 119 | CloneVcpu(base::Error), |
| 120 | ConfigureVcpu(<Arch as LinuxArch>::Error), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 121 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 122 | CreateAc97(devices::PciDeviceError), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 123 | CreateConsole(arch::serial::Error), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 124 | CreateDiskError(disk::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 125 | CreateEvent(base::Error), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 126 | CreateGrallocError(rutabaga_gfx::RutabagaError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 127 | CreateSignalFd(base::SignalFdError), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 128 | CreateSocket(io::Error), |
Chirantan Ekbote | 49fa08f | 2018-11-16 13:26:53 -0800 | [diff] [blame] | 129 | CreateTapDevice(NetError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 130 | CreateTimer(base::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 131 | CreateTpmStorage(PathBuf, io::Error), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 132 | CreateUsbProvider(devices::usb::host_backend::error::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 133 | CreateVcpu(base::Error), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 134 | CreateVfioDevice(devices::vfio::VfioError), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 135 | CreateWaitContext(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 136 | DeviceJail(minijail::Error), |
| 137 | DevicePivotRoot(minijail::Error), |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame^] | 138 | DirectIo(io::Error), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 139 | Disk(PathBuf, io::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 140 | DiskImageLock(base::Error), |
| 141 | DropCapabilities(base::Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 142 | FsDeviceNew(virtio::fs::Error), |
| 143 | GetMaxOpenFiles(io::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 144 | GetSignalMask(signal::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 145 | GuestCachedMissing(), |
| 146 | GuestCachedTooLarge(std::num::TryFromIntError), |
| 147 | GuestFreeMissing(), |
| 148 | GuestFreeTooLarge(std::num::TryFromIntError), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 149 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 150 | HandleDebugCommand(<Arch as LinuxArch>::Error), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 151 | InputDeviceNew(virtio::InputError), |
| 152 | InputEventsOpen(std::io::Error), |
Dylan Reid | 2056644 | 2018-04-02 15:06:15 -0700 | [diff] [blame] | 153 | InvalidFdPath, |
Zach Reizner | 579bd2c | 2018-09-14 15:43:33 -0700 | [diff] [blame] | 154 | InvalidWaylandPath, |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 155 | IoJail(minijail::Error), |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 156 | LoadKernel(Box<dyn StdError>), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 157 | MemoryTooLarge, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 158 | NetDeviceNew(virtio::NetError), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 159 | OpenAcpiTable(PathBuf, io::Error), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 160 | OpenAndroidFstab(PathBuf, io::Error), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 161 | OpenBios(PathBuf, io::Error), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 162 | OpenInitrd(PathBuf, io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 163 | OpenKernel(PathBuf, io::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 164 | OpenVinput(PathBuf, io::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 165 | P9DeviceNew(virtio::P9Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 166 | ParseMaxOpenFiles(ParseIntError), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 167 | PivotRootDoesntExist(&'static str), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 168 | PmemDeviceImageTooBig, |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 169 | PmemDeviceNew(base::Error), |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 170 | ReadMemAvailable(io::Error), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 171 | ReadStatm(io::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 172 | RegisterBalloon(arch::DeviceRegistrationError), |
| 173 | RegisterBlock(arch::DeviceRegistrationError), |
| 174 | RegisterGpu(arch::DeviceRegistrationError), |
| 175 | RegisterNet(arch::DeviceRegistrationError), |
| 176 | RegisterP9(arch::DeviceRegistrationError), |
| 177 | RegisterRng(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 178 | RegisterSignalHandler(base::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 179 | RegisterWayland(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 180 | ReserveGpuMemory(base::MmapError), |
| 181 | ReserveMemory(base::Error), |
| 182 | ReservePmemMemory(base::MmapError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 183 | ResetTimer(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 184 | RngDeviceNew(virtio::RngError), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 185 | RunnableVcpu(base::Error), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 186 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 187 | SendDebugStatus(Box<mpsc::SendError<VcpuDebugStatusMessage>>), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 188 | SettingGidMap(minijail::Error), |
| 189 | SettingMaxOpenFiles(minijail::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 190 | SettingSignalMask(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 191 | SettingUidMap(minijail::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 192 | SignalFd(base::SignalFdError), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 193 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 194 | SpawnGdbServer(io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 195 | SpawnVcpu(io::Error), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 196 | Timer(base::Error), |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 197 | ValidateRawDescriptor(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 198 | VhostNetDeviceNew(virtio::vhost::Error), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 199 | VhostUserBlockDeviceNew(VhostUserError), |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 200 | VhostUserFsDeviceNew(VhostUserError), |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 201 | VhostUserNetDeviceNew(VhostUserError), |
| 202 | VhostUserNetWithNetArgs, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 203 | VhostVsockDeviceNew(virtio::vhost::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 204 | VirtioPciDev(base::Error), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 205 | WaitContextAdd(base::Error), |
| 206 | WaitContextDelete(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 207 | WaylandDeviceNew(base::Error), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 208 | } |
| 209 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 210 | impl Display for Error { |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 211 | #[remain::check] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 212 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 213 | use self::Error::*; |
| 214 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 215 | #[sorted] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 216 | match self { |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 217 | AddGpuDeviceMemory(e) => write!(f, "failed to add gpu device memory: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 218 | AddIrqChipVcpu(e) => write!(f, "failed to add vcpu to irq chip: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 219 | AddPmemDeviceMemory(e) => write!(f, "failed to add pmem device memory: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 220 | AllocateGpuDeviceAddress => write!(f, "failed to allocate gpu device guest address"), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 221 | AllocatePmemDeviceAddress(e) => { |
| 222 | write!(f, "failed to allocate memory for pmem device: {}", e) |
| 223 | } |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 224 | BalloonActualTooLarge => write!(f, "balloon actual size is too large"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 225 | BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), |
| 226 | BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), |
| 227 | BlockSignal(e) => write!(f, "failed to block signal: {}", e), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 228 | BuildVm(e) => write!(f, "The architecture failed to build the vm: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 229 | ChownTpmStorage(e) => write!(f, "failed to chown tpm storage: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 230 | CloneEvent(e) => write!(f, "failed to clone event: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 231 | CloneVcpu(e) => write!(f, "failed to clone vcpu: {}", e), |
| 232 | ConfigureVcpu(e) => write!(f, "failed to configure vcpu: {}", e), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 233 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 234 | CreateAc97(e) => write!(f, "failed to create ac97 device: {}", e), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 235 | CreateConsole(e) => write!(f, "failed to create console device: {}", e), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 236 | CreateDiskError(e) => write!(f, "failed to create virtual disk: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 237 | CreateEvent(e) => write!(f, "failed to create event: {}", e), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 238 | CreateGrallocError(e) => write!(f, "failed to create gralloc: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 239 | CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), |
| 240 | CreateSocket(e) => write!(f, "failed to create socket: {}", e), |
| 241 | CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 242 | CreateTimer(e) => write!(f, "failed to create Timer: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 243 | CreateTpmStorage(p, e) => { |
| 244 | write!(f, "failed to create tpm storage dir {}: {}", p.display(), e) |
| 245 | } |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 246 | CreateUsbProvider(e) => write!(f, "failed to create usb provider: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 247 | CreateVcpu(e) => write!(f, "failed to create vcpu: {}", e), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 248 | CreateVfioDevice(e) => write!(f, "Failed to create vfio device {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 249 | CreateWaitContext(e) => write!(f, "failed to create wait context: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 250 | DeviceJail(e) => write!(f, "failed to jail device: {}", e), |
| 251 | DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame^] | 252 | DirectIo(e) => write!(f, "failed to open direct io device: {}", e), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 253 | Disk(p, e) => write!(f, "failed to load disk image {}: {}", p.display(), e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 254 | DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 255 | DropCapabilities(e) => write!(f, "failed to drop process capabilities: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 256 | FsDeviceNew(e) => write!(f, "failed to create fs device: {}", e), |
| 257 | GetMaxOpenFiles(e) => write!(f, "failed to get max number of open files: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 258 | GetSignalMask(e) => write!(f, "failed to retrieve signal mask for vcpu: {}", e), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 259 | GuestCachedMissing() => write!(f, "guest cached is missing from balloon stats"), |
| 260 | GuestCachedTooLarge(e) => write!(f, "guest cached is too large: {}", e), |
| 261 | GuestFreeMissing() => write!(f, "guest free is missing from balloon stats"), |
| 262 | GuestFreeTooLarge(e) => write!(f, "guest free is too large: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 263 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 264 | HandleDebugCommand(e) => write!(f, "failed to handle a gdb command: {}", e), |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 265 | InputDeviceNew(e) => write!(f, "failed to set up input device: {}", e), |
| 266 | InputEventsOpen(e) => write!(f, "failed to open event device: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 267 | InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), |
| 268 | InvalidWaylandPath => write!(f, "wayland socket path has no parent or file name"), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 269 | IoJail(e) => write!(f, "{}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 270 | LoadKernel(e) => write!(f, "failed to load kernel: {}", e), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 271 | MemoryTooLarge => write!(f, "requested memory size too large"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 272 | NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 273 | OpenAcpiTable(p, e) => write!(f, "failed to open ACPI file {}: {}", p.display(), e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 274 | OpenAndroidFstab(p, e) => write!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 275 | f, |
| 276 | "failed to open android fstab file {}: {}", |
| 277 | p.display(), |
| 278 | e |
| 279 | ), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 280 | OpenBios(p, e) => write!(f, "failed to open bios {}: {}", p.display(), e), |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 281 | OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), |
| 282 | OpenKernel(p, e) => write!(f, "failed to open kernel image {}: {}", p.display(), e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 283 | OpenVinput(p, e) => write!(f, "failed to open vinput device {}: {}", p.display(), e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 284 | P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 285 | ParseMaxOpenFiles(e) => write!(f, "failed to parse max number of open files: {}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 286 | PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 287 | PmemDeviceImageTooBig => { |
| 288 | write!(f, "failed to create pmem device: pmem device image too big") |
| 289 | } |
| 290 | PmemDeviceNew(e) => write!(f, "failed to create pmem device: {}", e), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 291 | ReadMemAvailable(e) => write!( |
| 292 | f, |
| 293 | "failed to read /sys/kernel/mm/chromeos-low_mem/available: {}", |
| 294 | e |
| 295 | ), |
| 296 | ReadStatm(e) => write!(f, "failed to read /proc/self/statm: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 297 | RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), |
| 298 | RegisterBlock(e) => write!(f, "error registering block device: {}", e), |
| 299 | RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), |
| 300 | RegisterNet(e) => write!(f, "error registering net device: {}", e), |
| 301 | RegisterP9(e) => write!(f, "error registering 9p device: {}", e), |
| 302 | RegisterRng(e) => write!(f, "error registering rng device: {}", e), |
| 303 | RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), |
| 304 | RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 305 | ReserveGpuMemory(e) => write!(f, "failed to reserve gpu memory: {}", e), |
| 306 | ReserveMemory(e) => write!(f, "failed to reserve memory: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 307 | ReservePmemMemory(e) => write!(f, "failed to reserve pmem memory: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 308 | ResetTimer(e) => write!(f, "failed to reset Timer: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 309 | RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 310 | RunnableVcpu(e) => write!(f, "failed to set thread id for vcpu: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 311 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 312 | SendDebugStatus(e) => write!(f, "failed to send a debug status to GDB thread: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 313 | SettingGidMap(e) => write!(f, "error setting GID map: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 314 | SettingMaxOpenFiles(e) => write!(f, "error setting max open files: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 315 | SettingSignalMask(e) => write!(f, "failed to set the signal mask for vcpu: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 316 | SettingUidMap(e) => write!(f, "error setting UID map: {}", e), |
| 317 | SignalFd(e) => write!(f, "failed to read signal fd: {}", e), |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 318 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 319 | SpawnGdbServer(e) => write!(f, "failed to spawn GDB thread: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 320 | SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 321 | Timer(e) => write!(f, "failed to read timer fd: {}", e), |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 322 | ValidateRawDescriptor(e) => write!(f, "failed to validate raw descriptor: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 323 | VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 324 | VhostUserBlockDeviceNew(e) => { |
| 325 | write!(f, "failed to set up vhost-user block device: {}", e) |
| 326 | } |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 327 | VhostUserFsDeviceNew(e) => { |
| 328 | write!(f, "failed to set up vhost-user fs device: {}", e) |
| 329 | } |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 330 | VhostUserNetDeviceNew(e) => { |
| 331 | write!(f, "failed to set up vhost-user net device: {}", e) |
| 332 | } |
| 333 | VhostUserNetWithNetArgs => { |
| 334 | write!( |
| 335 | f, |
| 336 | "vhost-user-net cannot be used with any of --host_ip, --netmask or --mac" |
| 337 | ) |
| 338 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 339 | VhostVsockDeviceNew(e) => write!(f, "failed to set up virtual socket device: {}", e), |
| 340 | VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 341 | WaitContextAdd(e) => write!(f, "failed to add descriptor to wait context: {}", e), |
| 342 | WaitContextDelete(e) => { |
| 343 | write!(f, "failed to remove descriptor from wait context: {}", e) |
| 344 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 345 | WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 350 | impl From<minijail::Error> for Error { |
| 351 | fn from(err: minijail::Error) -> Self { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 352 | Error::IoJail(err) |
| 353 | } |
| 354 | } |
| 355 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 356 | impl std::error::Error for Error {} |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 357 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 358 | type Result<T> = std::result::Result<T, Error>; |
| 359 | |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 360 | enum TaggedControlSocket { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 361 | Fs(FsMappingResponseSocket), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 362 | Vm(VmControlResponseSocket), |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 363 | VmMemory(VmMemoryControlResponseSocket), |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 364 | VmIrq(VmIrqResponseSocket), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 365 | VmMsync(VmMsyncResponseSocket), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | impl AsRef<UnixSeqpacket> for TaggedControlSocket { |
| 369 | fn as_ref(&self) -> &UnixSeqpacket { |
| 370 | use self::TaggedControlSocket::*; |
| 371 | match &self { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 372 | Fs(ref socket) => socket.as_ref(), |
Chirantan Ekbote | 5058253 | 2020-01-16 16:49:14 +0900 | [diff] [blame] | 373 | Vm(ref socket) => socket.as_ref(), |
| 374 | VmMemory(ref socket) => socket.as_ref(), |
| 375 | VmIrq(ref socket) => socket.as_ref(), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 376 | VmMsync(ref socket) => socket.as_ref(), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 381 | impl AsRawDescriptor for TaggedControlSocket { |
| 382 | fn as_raw_descriptor(&self) -> RawDescriptor { |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 383 | self.as_ref().as_raw_descriptor() |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Andrew Walbran | f50bab6 | 2020-07-07 13:22:53 +0100 | [diff] [blame] | 387 | fn get_max_open_files() -> Result<u64> { |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 388 | let mut buf = mem::MaybeUninit::<libc::rlimit64>::zeroed(); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 389 | |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 390 | // Safe because this will only modify `buf` and we check the return value. |
| 391 | let res = unsafe { libc::prlimit64(0, libc::RLIMIT_NOFILE, ptr::null(), buf.as_mut_ptr()) }; |
| 392 | if res == 0 { |
| 393 | // Safe because the kernel guarantees that the struct is fully initialized. |
| 394 | let limit = unsafe { buf.assume_init() }; |
| 395 | Ok(limit.rlim_max) |
| 396 | } else { |
| 397 | Err(Error::GetMaxOpenFiles(io::Error::last_os_error())) |
| 398 | } |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 401 | struct SandboxConfig<'a> { |
| 402 | limit_caps: bool, |
| 403 | log_failures: bool, |
| 404 | seccomp_policy: &'a Path, |
| 405 | uid_map: Option<&'a str>, |
| 406 | gid_map: Option<&'a str>, |
| 407 | } |
| 408 | |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 409 | fn create_base_minijail( |
| 410 | root: &Path, |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 411 | r_limit: Option<u64>, |
| 412 | config: Option<&SandboxConfig>, |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 413 | ) -> Result<Minijail> { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 414 | // All child jails run in a new user namespace without any users mapped, |
| 415 | // they run as nobody unless otherwise configured. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 416 | let mut j = Minijail::new().map_err(Error::DeviceJail)?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 417 | |
| 418 | if let Some(config) = config { |
| 419 | j.namespace_pids(); |
| 420 | j.namespace_user(); |
| 421 | j.namespace_user_disable_setgroups(); |
| 422 | if config.limit_caps { |
| 423 | // Don't need any capabilities. |
| 424 | j.use_caps(0); |
| 425 | } |
| 426 | if let Some(uid_map) = config.uid_map { |
| 427 | j.uidmap(uid_map).map_err(Error::SettingUidMap)?; |
| 428 | } |
| 429 | if let Some(gid_map) = config.gid_map { |
| 430 | j.gidmap(gid_map).map_err(Error::SettingGidMap)?; |
| 431 | } |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 432 | // Run in a new mount namespace. |
| 433 | j.namespace_vfs(); |
| 434 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 435 | // Run in an empty network namespace. |
| 436 | j.namespace_net(); |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 437 | |
| 438 | // Don't allow the device to gain new privileges. |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 439 | j.no_new_privs(); |
| 440 | |
| 441 | // By default we'll prioritize using the pre-compiled .bpf over the .policy |
| 442 | // file (the .bpf is expected to be compiled using "trap" as the failure |
| 443 | // behavior instead of the default "kill" behavior). |
| 444 | // Refer to the code comment for the "seccomp-log-failures" |
| 445 | // command-line parameter for an explanation about why the |log_failures| |
| 446 | // flag forces the use of .policy files (and the build-time alternative to |
| 447 | // this run-time flag). |
| 448 | let bpf_policy_file = config.seccomp_policy.with_extension("bpf"); |
| 449 | if bpf_policy_file.exists() && !config.log_failures { |
| 450 | j.parse_seccomp_program(&bpf_policy_file) |
| 451 | .map_err(Error::DeviceJail)?; |
| 452 | } else { |
| 453 | // Use TSYNC only for the side effect of it using SECCOMP_RET_TRAP, |
| 454 | // which will correctly kill the entire device process if a worker |
| 455 | // thread commits a seccomp violation. |
| 456 | j.set_seccomp_filter_tsync(); |
| 457 | if config.log_failures { |
| 458 | j.log_seccomp_filter_failures(); |
| 459 | } |
| 460 | j.parse_seccomp_filters(&config.seccomp_policy.with_extension("policy")) |
| 461 | .map_err(Error::DeviceJail)?; |
| 462 | } |
| 463 | j.use_seccomp_filter(); |
| 464 | // Don't do init setup. |
| 465 | j.run_as_init(); |
| 466 | } |
| 467 | |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 468 | // Only pivot_root if we are not re-using the current root directory. |
| 469 | if root != Path::new("/") { |
| 470 | // It's safe to call `namespace_vfs` multiple times. |
| 471 | j.namespace_vfs(); |
| 472 | j.enter_pivot_root(root).map_err(Error::DevicePivotRoot)?; |
| 473 | } |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 474 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 475 | // Most devices don't need to open many fds. |
| 476 | let limit = if let Some(r) = r_limit { r } else { 1024u64 }; |
| 477 | j.set_rlimit(libc::RLIMIT_NOFILE as i32, limit, limit) |
| 478 | .map_err(Error::SettingMaxOpenFiles)?; |
| 479 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 480 | Ok(j) |
| 481 | } |
| 482 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 483 | fn simple_jail(cfg: &Config, policy: &str) -> Result<Option<Minijail>> { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 484 | if cfg.sandbox { |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 485 | let pivot_root: &str = option_env!("DEFAULT_PIVOT_ROOT").unwrap_or("/var/empty"); |
| 486 | // A directory for a jailed device's pivot root. |
| 487 | let root_path = Path::new(pivot_root); |
| 488 | if !root_path.exists() { |
| 489 | return Err(Error::PivotRootDoesntExist(pivot_root)); |
| 490 | } |
| 491 | let policy_path: PathBuf = cfg.seccomp_policy_dir.join(policy); |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 492 | let config = SandboxConfig { |
| 493 | limit_caps: true, |
| 494 | log_failures: cfg.seccomp_log_failures, |
| 495 | seccomp_policy: &policy_path, |
| 496 | uid_map: None, |
| 497 | gid_map: None, |
| 498 | }; |
| 499 | Ok(Some(create_base_minijail(root_path, None, Some(&config))?)) |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 500 | } else { |
| 501 | Ok(None) |
| 502 | } |
| 503 | } |
| 504 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 505 | type DeviceResult<T = VirtioDeviceStub> = std::result::Result<T, Error>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 506 | |
| 507 | fn create_block_device( |
| 508 | cfg: &Config, |
| 509 | disk: &DiskOption, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 510 | disk_device_socket: DiskControlResponseSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 511 | ) -> DeviceResult { |
| 512 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 513 | let raw_image: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 514 | // Safe because we will validate |raw_fd|. |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 515 | unsafe { File::from_raw_descriptor(raw_descriptor_from_path(&disk.path)?) } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 516 | } else { |
| 517 | OpenOptions::new() |
| 518 | .read(true) |
| 519 | .write(!disk.read_only) |
| 520 | .open(&disk.path) |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 521 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))? |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 522 | }; |
| 523 | // Lock the disk image to prevent other crosvm instances from using it. |
| 524 | let lock_op = if disk.read_only { |
| 525 | FlockOperation::LockShared |
| 526 | } else { |
| 527 | FlockOperation::LockExclusive |
| 528 | }; |
| 529 | flock(&raw_image, lock_op, true).map_err(Error::DiskImageLock)?; |
| 530 | |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 531 | let dev = if disk::async_ok(&raw_image).map_err(Error::CreateDiskError)? { |
| 532 | let async_file = disk::create_async_disk_file(raw_image).map_err(Error::CreateDiskError)?; |
| 533 | Box::new( |
| 534 | virtio::BlockAsync::new( |
| 535 | virtio::base_features(cfg.protected_vm), |
| 536 | async_file, |
| 537 | disk.read_only, |
| 538 | disk.sparse, |
| 539 | disk.block_size, |
Daniel Verkamp | dd0ee59 | 2021-03-29 13:05:22 -0700 | [diff] [blame] | 540 | disk.id, |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 541 | Some(disk_device_socket), |
| 542 | ) |
| 543 | .map_err(Error::BlockDeviceNew)?, |
| 544 | ) as Box<dyn VirtioDevice> |
| 545 | } else { |
| 546 | let disk_file = disk::create_disk_file(raw_image).map_err(Error::CreateDiskError)?; |
| 547 | Box::new( |
| 548 | virtio::Block::new( |
| 549 | virtio::base_features(cfg.protected_vm), |
| 550 | disk_file, |
| 551 | disk.read_only, |
| 552 | disk.sparse, |
| 553 | disk.block_size, |
| 554 | disk.id, |
| 555 | Some(disk_device_socket), |
| 556 | ) |
| 557 | .map_err(Error::BlockDeviceNew)?, |
| 558 | ) as Box<dyn VirtioDevice> |
| 559 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 560 | |
| 561 | Ok(VirtioDeviceStub { |
Dylan Reid | 503c5ab | 2020-07-17 11:20:07 -0700 | [diff] [blame] | 562 | dev, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 563 | jail: simple_jail(&cfg, "block_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 564 | }) |
| 565 | } |
| 566 | |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 567 | fn create_vhost_user_block_device(cfg: &Config, opt: &VhostUserOption) -> DeviceResult { |
| 568 | let dev = VhostUserBlock::new(virtio::base_features(cfg.protected_vm), &opt.socket) |
| 569 | .map_err(Error::VhostUserBlockDeviceNew)?; |
| 570 | |
| 571 | Ok(VirtioDeviceStub { |
| 572 | dev: Box::new(dev), |
| 573 | // no sandbox here because virtqueue handling is exported to a different process. |
| 574 | jail: None, |
| 575 | }) |
| 576 | } |
| 577 | |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 578 | fn create_vhost_user_fs_device(cfg: &Config, option: &VhostUserFsOption) -> DeviceResult { |
| 579 | let dev = VhostUserFs::new( |
| 580 | virtio::base_features(cfg.protected_vm), |
| 581 | &option.socket, |
| 582 | &option.tag, |
| 583 | ) |
| 584 | .map_err(Error::VhostUserFsDeviceNew)?; |
| 585 | |
| 586 | Ok(VirtioDeviceStub { |
| 587 | dev: Box::new(dev), |
| 588 | // no sandbox here because virtqueue handling is exported to a different process. |
| 589 | jail: None, |
| 590 | }) |
| 591 | } |
| 592 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 593 | fn create_rng_device(cfg: &Config) -> DeviceResult { |
Keiichi Watanabe | f70350b | 2020-11-24 21:57:53 +0900 | [diff] [blame] | 594 | let dev = |
| 595 | virtio::Rng::new(virtio::base_features(cfg.protected_vm)).map_err(Error::RngDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 596 | |
| 597 | Ok(VirtioDeviceStub { |
| 598 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 599 | jail: simple_jail(&cfg, "rng_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 600 | }) |
| 601 | } |
| 602 | |
| 603 | #[cfg(feature = "tpm")] |
| 604 | fn create_tpm_device(cfg: &Config) -> DeviceResult { |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 605 | use base::chown; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 606 | use std::ffi::CString; |
| 607 | use std::fs; |
| 608 | use std::process; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 609 | |
| 610 | let tpm_storage: PathBuf; |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 611 | let mut tpm_jail = simple_jail(&cfg, "tpm_device")?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 612 | |
| 613 | match &mut tpm_jail { |
| 614 | Some(jail) => { |
| 615 | // Create a tmpfs in the device's root directory for tpm |
| 616 | // simulator storage. The size is 20*1024, or 20 KB. |
| 617 | jail.mount_with_data( |
| 618 | Path::new("none"), |
| 619 | Path::new("/"), |
| 620 | "tmpfs", |
| 621 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 622 | "size=20480", |
| 623 | )?; |
| 624 | |
| 625 | let crosvm_ids = add_crosvm_user_to_jail(jail, "tpm")?; |
| 626 | |
| 627 | let pid = process::id(); |
| 628 | let tpm_pid_dir = format!("/run/vm/tpm.{}", pid); |
| 629 | tpm_storage = Path::new(&tpm_pid_dir).to_owned(); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 630 | fs::create_dir_all(&tpm_storage) |
| 631 | .map_err(|e| Error::CreateTpmStorage(tpm_storage.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 632 | let tpm_pid_dir_c = CString::new(tpm_pid_dir).expect("no nul bytes"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 633 | chown(&tpm_pid_dir_c, crosvm_ids.uid, crosvm_ids.gid) |
| 634 | .map_err(Error::ChownTpmStorage)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 635 | |
| 636 | jail.mount_bind(&tpm_storage, &tpm_storage, true)?; |
| 637 | } |
| 638 | None => { |
| 639 | // Path used inside cros_sdk which does not have /run/vm. |
| 640 | tpm_storage = Path::new("/tmp/tpm-simulator").to_owned(); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | let dev = virtio::Tpm::new(tpm_storage); |
| 645 | |
| 646 | Ok(VirtioDeviceStub { |
| 647 | dev: Box::new(dev), |
| 648 | jail: tpm_jail, |
| 649 | }) |
| 650 | } |
| 651 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 652 | fn create_single_touch_device(cfg: &Config, single_touch_spec: &TouchDeviceOption) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 653 | let socket = single_touch_spec |
| 654 | .get_path() |
| 655 | .into_unix_stream() |
| 656 | .map_err(|e| { |
| 657 | error!("failed configuring virtio single touch: {:?}", e); |
| 658 | e |
| 659 | })?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 660 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 661 | let (width, height) = single_touch_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 662 | let dev = virtio::new_single_touch( |
| 663 | socket, |
| 664 | width, |
| 665 | height, |
| 666 | virtio::base_features(cfg.protected_vm), |
| 667 | ) |
| 668 | .map_err(Error::InputDeviceNew)?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 669 | Ok(VirtioDeviceStub { |
| 670 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 671 | jail: simple_jail(&cfg, "input_device")?, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 672 | }) |
| 673 | } |
| 674 | |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 675 | fn create_multi_touch_device(cfg: &Config, multi_touch_spec: &TouchDeviceOption) -> DeviceResult { |
| 676 | let socket = multi_touch_spec |
| 677 | .get_path() |
| 678 | .into_unix_stream() |
| 679 | .map_err(|e| { |
| 680 | error!("failed configuring virtio multi touch: {:?}", e); |
| 681 | e |
| 682 | })?; |
| 683 | |
| 684 | let (width, height) = multi_touch_spec.get_size(); |
| 685 | let dev = virtio::new_multi_touch( |
| 686 | socket, |
| 687 | width, |
| 688 | height, |
| 689 | virtio::base_features(cfg.protected_vm), |
| 690 | ) |
| 691 | .map_err(Error::InputDeviceNew)?; |
| 692 | |
| 693 | Ok(VirtioDeviceStub { |
| 694 | dev: Box::new(dev), |
| 695 | jail: simple_jail(&cfg, "input_device")?, |
| 696 | }) |
| 697 | } |
| 698 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 699 | fn create_trackpad_device(cfg: &Config, trackpad_spec: &TouchDeviceOption) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 700 | let socket = trackpad_spec.get_path().into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 701 | error!("failed configuring virtio trackpad: {}", e); |
| 702 | e |
| 703 | })?; |
| 704 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 705 | let (width, height) = trackpad_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 706 | let dev = virtio::new_trackpad( |
| 707 | socket, |
| 708 | width, |
| 709 | height, |
| 710 | virtio::base_features(cfg.protected_vm), |
| 711 | ) |
| 712 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 713 | |
| 714 | Ok(VirtioDeviceStub { |
| 715 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 716 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 717 | }) |
| 718 | } |
| 719 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 720 | fn create_mouse_device<T: IntoUnixStream>(cfg: &Config, mouse_socket: T) -> DeviceResult { |
| 721 | let socket = mouse_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 722 | error!("failed configuring virtio mouse: {}", e); |
| 723 | e |
| 724 | })?; |
| 725 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 726 | let dev = virtio::new_mouse(socket, virtio::base_features(cfg.protected_vm)) |
| 727 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 728 | |
| 729 | Ok(VirtioDeviceStub { |
| 730 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 731 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 732 | }) |
| 733 | } |
| 734 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 735 | fn create_keyboard_device<T: IntoUnixStream>(cfg: &Config, keyboard_socket: T) -> DeviceResult { |
| 736 | let socket = keyboard_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 737 | error!("failed configuring virtio keyboard: {}", e); |
| 738 | e |
| 739 | })?; |
| 740 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 741 | let dev = virtio::new_keyboard(socket, virtio::base_features(cfg.protected_vm)) |
| 742 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 743 | |
| 744 | Ok(VirtioDeviceStub { |
| 745 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 746 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 747 | }) |
| 748 | } |
| 749 | |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 750 | fn create_switches_device<T: IntoUnixStream>(cfg: &Config, switches_socket: T) -> DeviceResult { |
| 751 | let socket = switches_socket.into_unix_stream().map_err(|e| { |
| 752 | error!("failed configuring virtio switches: {}", e); |
| 753 | e |
| 754 | })?; |
| 755 | |
| 756 | let dev = virtio::new_switches(socket, virtio::base_features(cfg.protected_vm)) |
| 757 | .map_err(Error::InputDeviceNew)?; |
| 758 | |
| 759 | Ok(VirtioDeviceStub { |
| 760 | dev: Box::new(dev), |
| 761 | jail: simple_jail(&cfg, "input_device")?, |
| 762 | }) |
| 763 | } |
| 764 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 765 | fn create_vinput_device(cfg: &Config, dev_path: &Path) -> DeviceResult { |
| 766 | let dev_file = OpenOptions::new() |
| 767 | .read(true) |
| 768 | .write(true) |
| 769 | .open(dev_path) |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 770 | .map_err(|e| Error::OpenVinput(dev_path.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 771 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 772 | let dev = virtio::new_evdev(dev_file, virtio::base_features(cfg.protected_vm)) |
| 773 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 774 | |
| 775 | Ok(VirtioDeviceStub { |
| 776 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 777 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 778 | }) |
| 779 | } |
| 780 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 781 | fn create_balloon_device(cfg: &Config, socket: BalloonControlResponseSocket) -> DeviceResult { |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 782 | let dev = virtio::Balloon::new(virtio::base_features(cfg.protected_vm), socket) |
| 783 | .map_err(Error::BalloonDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 784 | |
| 785 | Ok(VirtioDeviceStub { |
| 786 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 787 | jail: simple_jail(&cfg, "balloon_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 788 | }) |
| 789 | } |
| 790 | |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 791 | fn create_tap_net_device(cfg: &Config, tap_fd: RawDescriptor) -> DeviceResult { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 792 | // Safe because we ensure that we get a unique handle to the fd. |
| 793 | let tap = unsafe { |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 794 | Tap::from_raw_descriptor( |
| 795 | validate_raw_descriptor(tap_fd).map_err(Error::ValidateRawDescriptor)?, |
| 796 | ) |
| 797 | .map_err(Error::CreateTapDevice)? |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 798 | }; |
| 799 | |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 800 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 801 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 802 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 803 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 804 | vq_pairs = 1; |
| 805 | } |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 806 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 807 | let dev = virtio::Net::from(features, tap, vq_pairs).map_err(Error::NetDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 808 | |
| 809 | Ok(VirtioDeviceStub { |
| 810 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 811 | jail: simple_jail(&cfg, "net_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 812 | }) |
| 813 | } |
| 814 | |
| 815 | fn create_net_device( |
| 816 | cfg: &Config, |
| 817 | host_ip: Ipv4Addr, |
| 818 | netmask: Ipv4Addr, |
| 819 | mac_address: MacAddress, |
| 820 | mem: &GuestMemory, |
| 821 | ) -> DeviceResult { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 822 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 823 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 824 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 825 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 826 | vq_pairs = 1; |
| 827 | } |
| 828 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 829 | let features = virtio::base_features(cfg.protected_vm); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 830 | let dev = if cfg.vhost_net { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 831 | let dev = virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new( |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 832 | &cfg.vhost_net_device_path, |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 833 | features, |
| 834 | host_ip, |
| 835 | netmask, |
| 836 | mac_address, |
| 837 | mem, |
| 838 | ) |
| 839 | .map_err(Error::VhostNetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 840 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 841 | } else { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 842 | let dev = virtio::Net::<Tap>::new(features, host_ip, netmask, mac_address, vq_pairs) |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 843 | .map_err(Error::NetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 844 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 845 | }; |
| 846 | |
| 847 | let policy = if cfg.vhost_net { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 848 | "vhost_net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 849 | } else { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 850 | "net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | Ok(VirtioDeviceStub { |
| 854 | dev, |
| 855 | jail: simple_jail(&cfg, policy)?, |
| 856 | }) |
| 857 | } |
| 858 | |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 859 | fn create_vhost_user_net_device(cfg: &Config, opt: &VhostUserOption) -> DeviceResult { |
| 860 | let dev = VhostUserNet::new(virtio::base_features(cfg.protected_vm), &opt.socket) |
| 861 | .map_err(Error::VhostUserNetDeviceNew)?; |
| 862 | |
| 863 | Ok(VirtioDeviceStub { |
| 864 | dev: Box::new(dev), |
| 865 | // no sandbox here because virtqueue handling is exported to a different process. |
| 866 | jail: None, |
| 867 | }) |
| 868 | } |
| 869 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 870 | #[cfg(feature = "gpu")] |
| 871 | fn create_gpu_device( |
| 872 | cfg: &Config, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 873 | exit_evt: &Event, |
Gurchetan Singh | 7ec58fa | 2019-05-15 15:30:38 -0700 | [diff] [blame] | 874 | gpu_device_socket: VmMemoryControlRequestSocket, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 875 | gpu_sockets: Vec<virtio::resource_bridge::ResourceResponseSocket>, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 876 | wayland_socket_path: Option<&PathBuf>, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 877 | x_display: Option<String>, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 878 | event_devices: Vec<EventDevice>, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 879 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Gurchetan Singh | db17478 | 2019-10-01 15:16:15 -0700 | [diff] [blame] | 880 | mem: &GuestMemory, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 881 | ) -> DeviceResult { |
| 882 | let jailed_wayland_path = Path::new("/wayland-0"); |
| 883 | |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 884 | let mut display_backends = vec![ |
| 885 | virtio::DisplayBackend::X(x_display), |
Jason Macnak | 60eb1fb | 2020-01-09 14:36:29 -0800 | [diff] [blame] | 886 | virtio::DisplayBackend::Stub, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 887 | ]; |
| 888 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 889 | if let Some(socket_path) = wayland_socket_path { |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 890 | display_backends.insert( |
| 891 | 0, |
| 892 | virtio::DisplayBackend::Wayland(if cfg.sandbox { |
| 893 | Some(jailed_wayland_path.to_owned()) |
| 894 | } else { |
| 895 | Some(socket_path.to_owned()) |
| 896 | }), |
| 897 | ); |
| 898 | } |
| 899 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 900 | let dev = virtio::Gpu::new( |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 901 | exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Gurchetan Singh | 7ec58fa | 2019-05-15 15:30:38 -0700 | [diff] [blame] | 902 | Some(gpu_device_socket), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 903 | NonZeroU8::new(1).unwrap(), // number of scanouts |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 904 | gpu_sockets, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 905 | display_backends, |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 906 | cfg.gpu_parameters.as_ref().unwrap(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 907 | event_devices, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 908 | map_request, |
| 909 | cfg.sandbox, |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 910 | virtio::base_features(cfg.protected_vm), |
Gurchetan Singh | 781d975 | 2021-02-15 17:45:22 -0800 | [diff] [blame] | 911 | cfg.wayland_socket_paths.clone(), |
Gurchetan Singh | db17478 | 2019-10-01 15:16:15 -0700 | [diff] [blame] | 912 | mem.clone(), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 913 | ); |
| 914 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 915 | let jail = match simple_jail(&cfg, "gpu_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 916 | Some(mut jail) => { |
| 917 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 918 | // dri directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 919 | jail.mount_with_data( |
| 920 | Path::new("none"), |
| 921 | Path::new("/"), |
| 922 | "tmpfs", |
| 923 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 924 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 925 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 926 | |
| 927 | // Device nodes required for DRM. |
| 928 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 929 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 930 | let sys_devices_path = Path::new("/sys/devices"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 931 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 932 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 933 | let drm_dri_path = Path::new("/dev/dri"); |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 934 | if drm_dri_path.exists() { |
| 935 | jail.mount_bind(drm_dri_path, drm_dri_path, false)?; |
| 936 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 937 | |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 938 | // Prepare GPU shader disk cache directory. |
| 939 | if let Some(cache_dir) = cfg |
| 940 | .gpu_parameters |
| 941 | .as_ref() |
| 942 | .and_then(|params| params.cache_path.as_ref()) |
| 943 | { |
| 944 | if cfg!(any(target_arch = "arm", target_arch = "aarch64")) && cfg.sandbox { |
| 945 | warn!("shader caching not yet supported on ARM with sandbox enabled"); |
| 946 | env::set_var("MESA_GLSL_CACHE_DISABLE", "true"); |
| 947 | } else { |
John Bates | 0405973 | 2020-10-01 15:58:55 -0700 | [diff] [blame] | 948 | env::set_var("MESA_GLSL_CACHE_DISABLE", "false"); |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 949 | env::set_var("MESA_GLSL_CACHE_DIR", cache_dir); |
| 950 | if let Some(cache_size) = cfg |
| 951 | .gpu_parameters |
| 952 | .as_ref() |
| 953 | .and_then(|params| params.cache_size.as_ref()) |
| 954 | { |
| 955 | env::set_var("MESA_GLSL_CACHE_MAX_SIZE", cache_size); |
| 956 | } |
| 957 | let shadercache_path = Path::new(cache_dir); |
| 958 | jail.mount_bind(shadercache_path, shadercache_path, true)?; |
| 959 | } |
| 960 | } |
| 961 | |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 962 | // If the ARM specific devices exist on the host, bind mount them in. |
| 963 | let mali0_path = Path::new("/dev/mali0"); |
| 964 | if mali0_path.exists() { |
| 965 | jail.mount_bind(mali0_path, mali0_path, true)?; |
| 966 | } |
| 967 | |
| 968 | let pvr_sync_path = Path::new("/dev/pvr_sync"); |
| 969 | if pvr_sync_path.exists() { |
| 970 | jail.mount_bind(pvr_sync_path, pvr_sync_path, true)?; |
| 971 | } |
| 972 | |
Gurchetan Singh | b66d6f6 | 2019-11-08 10:41:29 -0800 | [diff] [blame] | 973 | // If the udmabuf driver exists on the host, bind mount it in. |
| 974 | let udmabuf_path = Path::new("/dev/udmabuf"); |
| 975 | if udmabuf_path.exists() { |
| 976 | jail.mount_bind(udmabuf_path, udmabuf_path, true)?; |
| 977 | } |
| 978 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 979 | // Libraries that are required when mesa drivers are dynamically loaded. |
Chia-I Wu | d562b1a | 2020-12-27 21:08:27 -0800 | [diff] [blame] | 980 | let lib_dirs = &[ |
| 981 | "/usr/lib", |
| 982 | "/usr/lib64", |
| 983 | "/lib", |
| 984 | "/lib64", |
| 985 | "/usr/share/vulkan", |
| 986 | ]; |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 987 | for dir in lib_dirs { |
| 988 | let dir_path = Path::new(dir); |
| 989 | if dir_path.exists() { |
| 990 | jail.mount_bind(dir_path, dir_path, false)?; |
| 991 | } |
| 992 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 993 | |
Hiroki Sato | 942b8fc | 2021-02-15 06:30:21 +0000 | [diff] [blame] | 994 | // Bind mount the wayland socket into jail's root. This is necessary since each |
Gurchetan Singh | 781d975 | 2021-02-15 17:45:22 -0800 | [diff] [blame] | 995 | // new wayland context must open() the socket. Don't bind mount the camera socket |
| 996 | // since it seems to cause problems on ARCVM (b/180126126) + Mali. It's unclear if |
| 997 | // camera team will opt for virtio-camera or continue using virtio-wl, so this should |
| 998 | // be fine for now. |
Hiroki Sato | 942b8fc | 2021-02-15 06:30:21 +0000 | [diff] [blame] | 999 | if let Some(path) = wayland_socket_path { |
| 1000 | jail.mount_bind(path, jailed_wayland_path, true)?; |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1001 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1002 | |
| 1003 | add_crosvm_user_to_jail(&mut jail, "gpu")?; |
| 1004 | |
David Riley | 54e660b | 2019-07-24 17:22:50 -0700 | [diff] [blame] | 1005 | // pvr driver requires read access to /proc/self/task/*/comm. |
| 1006 | let proc_path = Path::new("/proc"); |
| 1007 | jail.mount( |
| 1008 | proc_path, |
| 1009 | proc_path, |
| 1010 | "proc", |
| 1011 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_RDONLY) as usize, |
| 1012 | )?; |
| 1013 | |
John Bates | 0d9d0e3 | 2020-12-03 11:37:33 -0800 | [diff] [blame] | 1014 | // To enable perfetto tracing, we need to give access to the perfetto service IPC |
| 1015 | // endpoints. |
| 1016 | let perfetto_path = Path::new("/run/perfetto"); |
| 1017 | if perfetto_path.exists() { |
| 1018 | jail.mount_bind(perfetto_path, perfetto_path, true)?; |
| 1019 | } |
| 1020 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1021 | Some(jail) |
| 1022 | } |
| 1023 | None => None, |
| 1024 | }; |
| 1025 | |
| 1026 | Ok(VirtioDeviceStub { |
| 1027 | dev: Box::new(dev), |
| 1028 | jail, |
| 1029 | }) |
| 1030 | } |
| 1031 | |
| 1032 | fn create_wayland_device( |
| 1033 | cfg: &Config, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 1034 | socket: VmMemoryControlRequestSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1035 | resource_bridge: Option<virtio::resource_bridge::ResourceRequestSocket>, |
| 1036 | ) -> DeviceResult { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1037 | let wayland_socket_dirs = cfg |
| 1038 | .wayland_socket_paths |
| 1039 | .iter() |
| 1040 | .map(|(_name, path)| path.parent()) |
| 1041 | .collect::<Option<Vec<_>>>() |
| 1042 | .ok_or(Error::InvalidWaylandPath)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1043 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1044 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 1045 | let dev = virtio::Wl::new( |
| 1046 | features, |
| 1047 | cfg.wayland_socket_paths.clone(), |
| 1048 | socket, |
| 1049 | resource_bridge, |
| 1050 | ) |
| 1051 | .map_err(Error::WaylandDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1052 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1053 | let jail = match simple_jail(&cfg, "wl_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1054 | Some(mut jail) => { |
| 1055 | // Create a tmpfs in the device's root directory so that we can bind mount the wayland |
| 1056 | // socket directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 1057 | jail.mount_with_data( |
| 1058 | Path::new("none"), |
| 1059 | Path::new("/"), |
| 1060 | "tmpfs", |
| 1061 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 1062 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1063 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1064 | |
| 1065 | // Bind mount the wayland socket's directory into jail's root. This is necessary since |
| 1066 | // each new wayland context must open() the socket. If the wayland socket is ever |
| 1067 | // destroyed and remade in the same host directory, new connections will be possible |
| 1068 | // without restarting the wayland device. |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1069 | for dir in &wayland_socket_dirs { |
| 1070 | jail.mount_bind(dir, dir, true)?; |
| 1071 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1072 | add_crosvm_user_to_jail(&mut jail, "Wayland")?; |
| 1073 | |
| 1074 | Some(jail) |
| 1075 | } |
| 1076 | None => None, |
| 1077 | }; |
| 1078 | |
| 1079 | Ok(VirtioDeviceStub { |
| 1080 | dev: Box::new(dev), |
| 1081 | jail, |
| 1082 | }) |
| 1083 | } |
| 1084 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1085 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 1086 | fn create_video_device( |
| 1087 | cfg: &Config, |
| 1088 | typ: devices::virtio::VideoDeviceType, |
| 1089 | resource_bridge: virtio::resource_bridge::ResourceRequestSocket, |
| 1090 | ) -> DeviceResult { |
| 1091 | let jail = match simple_jail(&cfg, "video_device")? { |
| 1092 | Some(mut jail) => { |
| 1093 | match typ { |
| 1094 | devices::virtio::VideoDeviceType::Decoder => { |
| 1095 | add_crosvm_user_to_jail(&mut jail, "video-decoder")? |
| 1096 | } |
| 1097 | devices::virtio::VideoDeviceType::Encoder => { |
| 1098 | add_crosvm_user_to_jail(&mut jail, "video-encoder")? |
| 1099 | } |
| 1100 | }; |
| 1101 | |
| 1102 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 1103 | jail.mount_with_data( |
| 1104 | Path::new("none"), |
| 1105 | Path::new("/"), |
| 1106 | "tmpfs", |
| 1107 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 1108 | "size=67108864", |
| 1109 | )?; |
| 1110 | |
| 1111 | // Render node for libvda. |
| 1112 | let dev_dri_path = Path::new("/dev/dri/renderD128"); |
| 1113 | jail.mount_bind(dev_dri_path, dev_dri_path, false)?; |
| 1114 | |
David Stevens | e341d0a | 2020-10-08 18:02:32 +0900 | [diff] [blame] | 1115 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1116 | { |
| 1117 | // Device nodes used by libdrm through minigbm in libvda on AMD devices. |
| 1118 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
| 1119 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
| 1120 | let sys_devices_path = Path::new("/sys/devices"); |
| 1121 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
| 1122 | |
| 1123 | // Required for loading dri libraries loaded by minigbm on AMD devices. |
| 1124 | let lib_dir = Path::new("/usr/lib64"); |
| 1125 | jail.mount_bind(lib_dir, lib_dir, false)?; |
| 1126 | } |
| 1127 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1128 | // Device nodes required by libchrome which establishes Mojo connection in libvda. |
| 1129 | let dev_urandom_path = Path::new("/dev/urandom"); |
| 1130 | jail.mount_bind(dev_urandom_path, dev_urandom_path, false)?; |
| 1131 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 1132 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 1133 | |
| 1134 | Some(jail) |
| 1135 | } |
| 1136 | None => None, |
| 1137 | }; |
| 1138 | |
| 1139 | Ok(VirtioDeviceStub { |
| 1140 | dev: Box::new(devices::virtio::VideoDevice::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1141 | virtio::base_features(cfg.protected_vm), |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1142 | typ, |
| 1143 | Some(resource_bridge), |
| 1144 | )), |
| 1145 | jail, |
| 1146 | }) |
| 1147 | } |
| 1148 | |
| 1149 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 1150 | fn register_video_device( |
| 1151 | devs: &mut Vec<VirtioDeviceStub>, |
| 1152 | resource_bridges: &mut Vec<virtio::resource_bridge::ResourceResponseSocket>, |
| 1153 | cfg: &Config, |
| 1154 | typ: devices::virtio::VideoDeviceType, |
| 1155 | ) -> std::result::Result<(), Error> { |
| 1156 | let (video_socket, gpu_socket) = |
| 1157 | virtio::resource_bridge::pair().map_err(Error::CreateSocket)?; |
| 1158 | resource_bridges.push(gpu_socket); |
| 1159 | devs.push(create_video_device(cfg, typ, video_socket)?); |
| 1160 | Ok(()) |
| 1161 | } |
| 1162 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1163 | fn create_vhost_vsock_device(cfg: &Config, cid: u64, mem: &GuestMemory) -> DeviceResult { |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1164 | let features = virtio::base_features(cfg.protected_vm); |
Christian Blichmann | 2f5d4b6 | 2021-03-10 18:08:08 +0100 | [diff] [blame] | 1165 | let dev = virtio::vhost::Vsock::new(&cfg.vhost_vsock_device_path, features, cid, mem) |
| 1166 | .map_err(Error::VhostVsockDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1167 | |
| 1168 | Ok(VirtioDeviceStub { |
| 1169 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1170 | jail: simple_jail(&cfg, "vhost_vsock_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1171 | }) |
| 1172 | } |
| 1173 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1174 | fn create_fs_device( |
| 1175 | cfg: &Config, |
| 1176 | uid_map: &str, |
| 1177 | gid_map: &str, |
| 1178 | src: &Path, |
| 1179 | tag: &str, |
| 1180 | fs_cfg: virtio::fs::passthrough::Config, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1181 | device_socket: FsMappingRequestSocket, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1182 | ) -> DeviceResult { |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1183 | let max_open_files = get_max_open_files()?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 1184 | let j = if cfg.sandbox { |
| 1185 | let seccomp_policy = cfg.seccomp_policy_dir.join("fs_device"); |
| 1186 | let config = SandboxConfig { |
| 1187 | limit_caps: false, |
| 1188 | uid_map: Some(uid_map), |
| 1189 | gid_map: Some(gid_map), |
| 1190 | log_failures: cfg.seccomp_log_failures, |
| 1191 | seccomp_policy: &seccomp_policy, |
| 1192 | }; |
Chirantan Ekbote | 34d45e5 | 2020-04-20 18:15:02 +0900 | [diff] [blame] | 1193 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1194 | // We want bind mounts from the parent namespaces to propagate into the fs device's |
| 1195 | // namespace. |
| 1196 | jail.set_remount_mode(libc::MS_SLAVE); |
| 1197 | |
| 1198 | jail |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 1199 | } else { |
| 1200 | create_base_minijail(src, Some(max_open_files), None)? |
| 1201 | }; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1202 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1203 | let features = virtio::base_features(cfg.protected_vm); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1204 | // TODO(chirantan): Use more than one worker once the kernel driver has been fixed to not panic |
| 1205 | // when num_queues > 1. |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1206 | let dev = |
| 1207 | virtio::fs::Fs::new(features, tag, 1, fs_cfg, device_socket).map_err(Error::FsDeviceNew)?; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1208 | |
| 1209 | Ok(VirtioDeviceStub { |
| 1210 | dev: Box::new(dev), |
| 1211 | jail: Some(j), |
| 1212 | }) |
| 1213 | } |
| 1214 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1215 | fn create_9p_device( |
| 1216 | cfg: &Config, |
| 1217 | uid_map: &str, |
| 1218 | gid_map: &str, |
| 1219 | src: &Path, |
| 1220 | tag: &str, |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1221 | mut p9_cfg: p9::Config, |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1222 | ) -> DeviceResult { |
| 1223 | let max_open_files = get_max_open_files()?; |
| 1224 | let (jail, root) = if cfg.sandbox { |
| 1225 | let seccomp_policy = cfg.seccomp_policy_dir.join("9p_device"); |
| 1226 | let config = SandboxConfig { |
| 1227 | limit_caps: false, |
| 1228 | uid_map: Some(uid_map), |
| 1229 | gid_map: Some(gid_map), |
| 1230 | log_failures: cfg.seccomp_log_failures, |
| 1231 | seccomp_policy: &seccomp_policy, |
| 1232 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1233 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1234 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1235 | // We want bind mounts from the parent namespaces to propagate into the 9p server's |
| 1236 | // namespace. |
| 1237 | jail.set_remount_mode(libc::MS_SLAVE); |
Chirantan Ekbote | 055de38 | 2020-01-24 12:16:58 +0900 | [diff] [blame] | 1238 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1239 | // The shared directory becomes the root of the device's file system. |
| 1240 | let root = Path::new("/"); |
| 1241 | (Some(jail), root) |
| 1242 | } else { |
| 1243 | // There's no mount namespace so we tell the server to treat the source directory as the |
| 1244 | // root. |
| 1245 | (None, src) |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1246 | }; |
| 1247 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1248 | let features = virtio::base_features(cfg.protected_vm); |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1249 | p9_cfg.root = root.into(); |
| 1250 | let dev = virtio::P9::new(features, tag, p9_cfg).map_err(Error::P9DeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1251 | |
| 1252 | Ok(VirtioDeviceStub { |
| 1253 | dev: Box::new(dev), |
| 1254 | jail, |
| 1255 | }) |
| 1256 | } |
| 1257 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1258 | fn create_pmem_device( |
| 1259 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1260 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1261 | resources: &mut SystemAllocator, |
| 1262 | disk: &DiskOption, |
| 1263 | index: usize, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1264 | pmem_device_socket: VmMsyncRequestSocket, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1265 | ) -> DeviceResult { |
Mike Gerow | ec618a5 | 2021-02-26 20:57:14 +0000 | [diff] [blame] | 1266 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 1267 | let fd: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 1268 | // Safe because we will validate |raw_fd|. |
| 1269 | unsafe { File::from_raw_descriptor(raw_descriptor_from_path(&disk.path)?) } |
| 1270 | } else { |
| 1271 | OpenOptions::new() |
| 1272 | .read(true) |
| 1273 | .write(!disk.read_only) |
| 1274 | .open(&disk.path) |
| 1275 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))? |
| 1276 | }; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1277 | |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1278 | let arena_size = { |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 1279 | let metadata = |
| 1280 | std::fs::metadata(&disk.path).map_err(|e| Error::Disk(disk.path.to_path_buf(), e))?; |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1281 | let disk_len = metadata.len(); |
| 1282 | // Linux requires pmem region sizes to be 2 MiB aligned. Linux will fill any partial page |
| 1283 | // at the end of an mmap'd file and won't write back beyond the actual file length, but if |
| 1284 | // we just align the size of the file to 2 MiB then access beyond the last page of the |
| 1285 | // mapped file will generate SIGBUS. So use a memory mapping arena that will provide |
| 1286 | // padding up to 2 MiB. |
| 1287 | let alignment = 2 * 1024 * 1024; |
| 1288 | let align_adjust = if disk_len % alignment != 0 { |
| 1289 | alignment - (disk_len % alignment) |
| 1290 | } else { |
| 1291 | 0 |
| 1292 | }; |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1293 | disk_len |
| 1294 | .checked_add(align_adjust) |
| 1295 | .ok_or(Error::PmemDeviceImageTooBig)? |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1296 | }; |
| 1297 | |
| 1298 | let protection = { |
| 1299 | if disk.read_only { |
| 1300 | Protection::read() |
| 1301 | } else { |
| 1302 | Protection::read_write() |
| 1303 | } |
| 1304 | }; |
| 1305 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1306 | let arena = { |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1307 | // Conversion from u64 to usize may fail on 32bit system. |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1308 | let arena_size = usize::try_from(arena_size).map_err(|_| Error::PmemDeviceImageTooBig)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1309 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1310 | let mut arena = MemoryMappingArena::new(arena_size).map_err(Error::ReservePmemMemory)?; |
| 1311 | arena |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1312 | .add_fd_offset_protection(0, arena_size, &fd, 0, protection) |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1313 | .map_err(Error::ReservePmemMemory)?; |
| 1314 | arena |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1315 | }; |
| 1316 | |
| 1317 | let mapping_address = resources |
Xiong Zhang | 383b3b5 | 2019-10-30 14:59:26 +0800 | [diff] [blame] | 1318 | .mmio_allocator(MmioType::High) |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1319 | .allocate_with_align( |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1320 | arena_size, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1321 | Alloc::PmemDevice(index), |
| 1322 | format!("pmem_disk_image_{}", index), |
| 1323 | // Linux kernel requires pmem namespaces to be 128 MiB aligned. |
| 1324 | 128 * 1024 * 1024, /* 128 MiB */ |
| 1325 | ) |
| 1326 | .map_err(Error::AllocatePmemDeviceAddress)?; |
| 1327 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1328 | let slot = vm |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1329 | .add_memory_region( |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1330 | GuestAddress(mapping_address), |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1331 | Box::new(arena), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1332 | /* read_only = */ disk.read_only, |
| 1333 | /* log_dirty_pages = */ false, |
| 1334 | ) |
| 1335 | .map_err(Error::AddPmemDeviceMemory)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1336 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1337 | let dev = virtio::Pmem::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1338 | virtio::base_features(cfg.protected_vm), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1339 | fd, |
| 1340 | GuestAddress(mapping_address), |
| 1341 | slot, |
| 1342 | arena_size, |
| 1343 | Some(pmem_device_socket), |
| 1344 | ) |
| 1345 | .map_err(Error::PmemDeviceNew)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1346 | |
| 1347 | Ok(VirtioDeviceStub { |
| 1348 | dev: Box::new(dev) as Box<dyn VirtioDevice>, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1349 | jail: simple_jail(&cfg, "pmem_device")?, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1350 | }) |
| 1351 | } |
| 1352 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1353 | fn create_console_device(cfg: &Config, param: &SerialParameters) -> DeviceResult { |
Michael Hoyle | cd23bc2 | 2020-10-20 22:12:20 -0700 | [diff] [blame] | 1354 | let mut keep_rds = Vec::new(); |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1355 | let evt = Event::new().map_err(Error::CreateEvent)?; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1356 | let dev = param |
Michael Hoyle | cd23bc2 | 2020-10-20 22:12:20 -0700 | [diff] [blame] | 1357 | .create_serial_device::<Console>(cfg.protected_vm, &evt, &mut keep_rds) |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1358 | .map_err(Error::CreateConsole)?; |
| 1359 | |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1360 | let jail = match simple_jail(&cfg, "serial")? { |
| 1361 | Some(mut jail) => { |
| 1362 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 1363 | // log socket directory into it. |
| 1364 | // The size=67108864 is size=64*1024*1024 or size=64MB. |
| 1365 | jail.mount_with_data( |
| 1366 | Path::new("none"), |
| 1367 | Path::new("/"), |
| 1368 | "tmpfs", |
| 1369 | (libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_NOSUID) as usize, |
| 1370 | "size=67108864", |
| 1371 | )?; |
| 1372 | add_crosvm_user_to_jail(&mut jail, "serial")?; |
| 1373 | let res = param.add_bind_mounts(&mut jail); |
| 1374 | if res.is_err() { |
| 1375 | error!("failed to add bind mounts for console device"); |
| 1376 | } |
| 1377 | Some(jail) |
| 1378 | } |
| 1379 | None => None, |
| 1380 | }; |
| 1381 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1382 | Ok(VirtioDeviceStub { |
| 1383 | dev: Box::new(dev), |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1384 | jail, // TODO(dverkamp): use a separate policy for console? |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1385 | }) |
| 1386 | } |
| 1387 | |
Dmitry Torokhov | ee42b8c | 2019-05-27 11:14:20 -0700 | [diff] [blame] | 1388 | // gpu_device_socket is not used when GPU support is disabled. |
| 1389 | #[cfg_attr(not(feature = "gpu"), allow(unused_variables))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1390 | fn create_virtio_devices( |
| 1391 | cfg: &Config, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1392 | mem: &GuestMemory, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1393 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1394 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1395 | _exit_evt: &Event, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 1396 | wayland_device_socket: VmMemoryControlRequestSocket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1397 | gpu_device_socket: VmMemoryControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1398 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1399 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1400 | pmem_device_sockets: &mut Vec<VmMsyncRequestSocket>, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1401 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1402 | fs_device_sockets: &mut Vec<FsMappingRequestSocket>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1403 | ) -> DeviceResult<Vec<VirtioDeviceStub>> { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1404 | let mut devs = Vec::new(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1405 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1406 | for (_, param) in cfg |
| 1407 | .serial_parameters |
| 1408 | .iter() |
| 1409 | .filter(|(_k, v)| v.hardware == SerialHardware::VirtioConsole) |
| 1410 | { |
| 1411 | let dev = create_console_device(cfg, param)?; |
| 1412 | devs.push(dev); |
| 1413 | } |
| 1414 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1415 | for disk in &cfg.disks { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1416 | let disk_device_socket = disk_device_sockets.remove(0); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1417 | devs.push(create_block_device(cfg, disk, disk_device_socket)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Keiichi Watanabe | f3a37f4 | 2021-01-21 15:41:11 +0900 | [diff] [blame] | 1420 | for blk in &cfg.vhost_user_blk { |
| 1421 | devs.push(create_vhost_user_block_device(cfg, blk)?); |
| 1422 | } |
| 1423 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1424 | for (index, pmem_disk) in cfg.pmem_devices.iter().enumerate() { |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1425 | let pmem_device_socket = pmem_device_sockets.remove(0); |
| 1426 | devs.push(create_pmem_device( |
| 1427 | cfg, |
| 1428 | vm, |
| 1429 | resources, |
| 1430 | pmem_disk, |
| 1431 | index, |
| 1432 | pmem_device_socket, |
| 1433 | )?); |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1436 | devs.push(create_rng_device(cfg)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1437 | |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1438 | #[cfg(feature = "tpm")] |
| 1439 | { |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1440 | if cfg.software_tpm { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1441 | devs.push(create_tpm_device(cfg)?); |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1442 | } |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1445 | if let Some(single_touch_spec) = &cfg.virtio_single_touch { |
| 1446 | devs.push(create_single_touch_device(cfg, single_touch_spec)?); |
| 1447 | } |
| 1448 | |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1449 | if let Some(multi_touch_spec) = &cfg.virtio_multi_touch { |
| 1450 | devs.push(create_multi_touch_device(cfg, multi_touch_spec)?); |
| 1451 | } |
| 1452 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1453 | if let Some(trackpad_spec) = &cfg.virtio_trackpad { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1454 | devs.push(create_trackpad_device(cfg, trackpad_spec)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1457 | if let Some(mouse_socket) = &cfg.virtio_mouse { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1458 | devs.push(create_mouse_device(cfg, mouse_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1459 | } |
| 1460 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1461 | if let Some(keyboard_socket) = &cfg.virtio_keyboard { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1462 | devs.push(create_keyboard_device(cfg, keyboard_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Daniel Norman | 5e23df7 | 2021-03-11 10:11:02 -0800 | [diff] [blame] | 1465 | if let Some(switches_socket) = &cfg.virtio_switches { |
| 1466 | devs.push(create_switches_device(cfg, switches_socket)?); |
| 1467 | } |
| 1468 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1469 | for dev_path in &cfg.virtio_input_evdevs { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1470 | devs.push(create_vinput_device(cfg, dev_path)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1473 | devs.push(create_balloon_device(cfg, balloon_device_socket)?); |
Dylan Reid | 295ccac | 2017-11-06 14:06:24 -0800 | [diff] [blame] | 1474 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1475 | // 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] | 1476 | for tap_fd in &cfg.tap_fd { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1477 | devs.push(create_tap_net_device(cfg, *tap_fd)?); |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1480 | if let (Some(host_ip), Some(netmask), Some(mac_address)) = |
| 1481 | (cfg.host_ip, cfg.netmask, cfg.mac_address) |
| 1482 | { |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 1483 | if !cfg.vhost_user_net.is_empty() { |
| 1484 | return Err(Error::VhostUserNetWithNetArgs); |
| 1485 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1486 | devs.push(create_net_device(cfg, host_ip, netmask, mac_address, mem)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Keiichi Watanabe | 6068658 | 2021-03-12 04:53:51 +0900 | [diff] [blame] | 1489 | for net in &cfg.vhost_user_net { |
| 1490 | devs.push(create_vhost_user_net_device(cfg, net)?); |
| 1491 | } |
| 1492 | |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1493 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1494 | let mut resource_bridges = Vec::<virtio::resource_bridge::ResourceResponseSocket>::new(); |
| 1495 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1496 | if !cfg.wayland_socket_paths.is_empty() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1497 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
| 1498 | let mut wl_resource_bridge = None::<virtio::resource_bridge::ResourceRequestSocket>; |
| 1499 | |
| 1500 | #[cfg(feature = "gpu")] |
| 1501 | { |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1502 | if cfg.gpu_parameters.is_some() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1503 | let (wl_socket, gpu_socket) = |
| 1504 | virtio::resource_bridge::pair().map_err(Error::CreateSocket)?; |
| 1505 | resource_bridges.push(gpu_socket); |
| 1506 | wl_resource_bridge = Some(wl_socket); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | devs.push(create_wayland_device( |
| 1511 | cfg, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1512 | wayland_device_socket, |
| 1513 | wl_resource_bridge, |
| 1514 | )?); |
| 1515 | } |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1516 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1517 | #[cfg(feature = "video-decoder")] |
| 1518 | { |
| 1519 | if cfg.video_dec { |
| 1520 | register_video_device( |
| 1521 | &mut devs, |
| 1522 | &mut resource_bridges, |
| 1523 | cfg, |
| 1524 | devices::virtio::VideoDeviceType::Decoder, |
| 1525 | )?; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | #[cfg(feature = "video-encoder")] |
| 1530 | { |
| 1531 | if cfg.video_enc { |
| 1532 | register_video_device( |
| 1533 | &mut devs, |
| 1534 | &mut resource_bridges, |
| 1535 | cfg, |
| 1536 | devices::virtio::VideoDeviceType::Encoder, |
| 1537 | )?; |
| 1538 | } |
| 1539 | } |
| 1540 | |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1541 | #[cfg(feature = "gpu")] |
| 1542 | { |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1543 | if let Some(gpu_parameters) = &cfg.gpu_parameters { |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1544 | let mut event_devices = Vec::new(); |
| 1545 | if cfg.display_window_mouse { |
| 1546 | let (event_device_socket, virtio_dev_socket) = |
| 1547 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1548 | let (multi_touch_width, multi_touch_height) = cfg |
| 1549 | .virtio_multi_touch |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1550 | .as_ref() |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1551 | .map(|multi_touch_spec| multi_touch_spec.get_size()) |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1552 | .unwrap_or((gpu_parameters.display_width, gpu_parameters.display_height)); |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1553 | let dev = virtio::new_multi_touch( |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1554 | virtio_dev_socket, |
Tristan Muntsinger | 486cffc | 2020-09-29 22:05:41 +0000 | [diff] [blame] | 1555 | multi_touch_width, |
| 1556 | multi_touch_height, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1557 | virtio::base_features(cfg.protected_vm), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1558 | ) |
| 1559 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1560 | devs.push(VirtioDeviceStub { |
| 1561 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1562 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1563 | }); |
| 1564 | event_devices.push(EventDevice::touchscreen(event_device_socket)); |
| 1565 | } |
| 1566 | if cfg.display_window_keyboard { |
| 1567 | let (event_device_socket, virtio_dev_socket) = |
| 1568 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1569 | let dev = virtio::new_keyboard( |
| 1570 | virtio_dev_socket, |
| 1571 | virtio::base_features(cfg.protected_vm), |
| 1572 | ) |
| 1573 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1574 | devs.push(VirtioDeviceStub { |
| 1575 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1576 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1577 | }); |
| 1578 | event_devices.push(EventDevice::keyboard(event_device_socket)); |
| 1579 | } |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1580 | devs.push(create_gpu_device( |
| 1581 | cfg, |
| 1582 | _exit_evt, |
| 1583 | gpu_device_socket, |
| 1584 | resource_bridges, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1585 | // Use the unnamed socket for GPU display screens. |
| 1586 | cfg.wayland_socket_paths.get(""), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1587 | cfg.x_display.clone(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1588 | event_devices, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1589 | map_request, |
Gurchetan Singh | db17478 | 2019-10-01 15:16:15 -0700 | [diff] [blame] | 1590 | mem, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1591 | )?); |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1595 | if let Some(cid) = cfg.cid { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1596 | devs.push(create_vhost_vsock_device(cfg, cid, mem)?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Woody Chow | 5890b70 | 2021-02-12 14:57:02 +0900 | [diff] [blame] | 1599 | for vhost_user_fs in &cfg.vhost_user_fs { |
| 1600 | devs.push(create_vhost_user_fs_device(cfg, &vhost_user_fs)?); |
| 1601 | } |
| 1602 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1603 | for shared_dir in &cfg.shared_dirs { |
| 1604 | let SharedDir { |
| 1605 | src, |
| 1606 | tag, |
| 1607 | kind, |
| 1608 | uid_map, |
| 1609 | gid_map, |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1610 | fs_cfg, |
| 1611 | p9_cfg, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1612 | } = shared_dir; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1613 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1614 | let dev = match kind { |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1615 | SharedDirKind::FS => { |
| 1616 | let device_socket = fs_device_sockets.remove(0); |
| 1617 | create_fs_device( |
| 1618 | cfg, |
| 1619 | uid_map, |
| 1620 | gid_map, |
| 1621 | src, |
| 1622 | tag, |
| 1623 | fs_cfg.clone(), |
| 1624 | device_socket, |
| 1625 | )? |
| 1626 | } |
Chirantan Ekbote | 75ba875 | 2020-10-27 18:33:02 +0900 | [diff] [blame] | 1627 | 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] | 1628 | }; |
| 1629 | devs.push(dev); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | Ok(devs) |
| 1633 | } |
| 1634 | |
| 1635 | fn create_devices( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1636 | cfg: &Config, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1637 | mem: &GuestMemory, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1638 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1639 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1640 | exit_evt: &Event, |
Xiong Zhang | a5d248c | 2019-09-17 14:17:19 -0700 | [diff] [blame] | 1641 | control_sockets: &mut Vec<TaggedControlSocket>, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 1642 | wayland_device_socket: VmMemoryControlRequestSocket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1643 | gpu_device_socket: VmMemoryControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1644 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1645 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1646 | pmem_device_sockets: &mut Vec<VmMsyncRequestSocket>, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1647 | fs_device_sockets: &mut Vec<FsMappingRequestSocket>, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1648 | usb_provider: HostBackendDeviceProvider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1649 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1650 | ) -> DeviceResult<Vec<(Box<dyn PciDevice>, Option<Minijail>)>> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1651 | let stubs = create_virtio_devices( |
| 1652 | &cfg, |
| 1653 | mem, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1654 | vm, |
| 1655 | resources, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1656 | exit_evt, |
| 1657 | wayland_device_socket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1658 | gpu_device_socket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1659 | balloon_device_socket, |
| 1660 | disk_device_sockets, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1661 | pmem_device_sockets, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1662 | map_request, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 1663 | fs_device_sockets, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1664 | )?; |
| 1665 | |
| 1666 | let mut pci_devices = Vec::new(); |
| 1667 | |
| 1668 | for stub in stubs { |
Daniel Verkamp | bb712d6 | 2019-11-19 09:47:33 -0800 | [diff] [blame] | 1669 | let (msi_host_socket, msi_device_socket) = |
| 1670 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 1671 | control_sockets.push(TaggedControlSocket::VmIrq(msi_host_socket)); |
| 1672 | let dev = VirtioPciDevice::new(mem.clone(), stub.dev, msi_device_socket) |
| 1673 | .map_err(Error::VirtioPciDev)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1674 | let dev = Box::new(dev) as Box<dyn PciDevice>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1675 | pci_devices.push((dev, stub.jail)); |
| 1676 | } |
| 1677 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1678 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1679 | for ac97_param in &cfg.ac97_parameters { |
| 1680 | let dev = Ac97Dev::try_new(mem.clone(), ac97_param.clone()).map_err(Error::CreateAc97)?; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 1681 | let jail = simple_jail(&cfg, dev.minijail_policy())?; |
| 1682 | pci_devices.push((Box::new(dev), jail)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1683 | } |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1684 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1685 | // Create xhci controller. |
| 1686 | let usb_controller = Box::new(XhciController::new(mem.clone(), usb_provider)); |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1687 | pci_devices.push((usb_controller, simple_jail(&cfg, "xhci")?)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1688 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1689 | if !cfg.vfio.is_empty() { |
Xiong Zhang | ea6cf66 | 2019-11-11 18:32:02 +0800 | [diff] [blame] | 1690 | let vfio_container = Arc::new(Mutex::new( |
| 1691 | VfioContainer::new().map_err(Error::CreateVfioDevice)?, |
| 1692 | )); |
| 1693 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1694 | for vfio_path in &cfg.vfio { |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1695 | // create MSI, MSI-X, and Mem request sockets for each vfio device |
| 1696 | let (vfio_host_socket_msi, vfio_device_socket_msi) = |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1697 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1698 | control_sockets.push(TaggedControlSocket::VmIrq(vfio_host_socket_msi)); |
| 1699 | |
| 1700 | let (vfio_host_socket_msix, vfio_device_socket_msix) = |
| 1701 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 1702 | control_sockets.push(TaggedControlSocket::VmIrq(vfio_host_socket_msix)); |
Xiong Zhang | 4b5bb3a | 2019-04-23 17:15:21 +0800 | [diff] [blame] | 1703 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1704 | let (vfio_host_socket_mem, vfio_device_socket_mem) = |
| 1705 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>() |
| 1706 | .map_err(Error::CreateSocket)?; |
| 1707 | control_sockets.push(TaggedControlSocket::VmMemory(vfio_host_socket_mem)); |
Xiong Zhang | 85abeff | 2019-04-23 17:15:24 +0800 | [diff] [blame] | 1708 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1709 | let vfiodevice = VfioDevice::new(vfio_path.as_path(), vm, mem, vfio_container.clone()) |
| 1710 | .map_err(Error::CreateVfioDevice)?; |
Tomasz Jeznach | 502b5de | 2021-02-03 21:45:47 -0800 | [diff] [blame] | 1711 | let mut vfiopcidevice = Box::new(VfioPciDevice::new( |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1712 | vfiodevice, |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1713 | vfio_device_socket_msi, |
| 1714 | vfio_device_socket_msix, |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1715 | vfio_device_socket_mem, |
| 1716 | )); |
Tomasz Jeznach | 502b5de | 2021-02-03 21:45:47 -0800 | [diff] [blame] | 1717 | // early reservation for pass-through PCI devices. |
| 1718 | if vfiopcidevice.allocate_address(resources).is_err() { |
| 1719 | warn!( |
| 1720 | "address reservation failed for vfio {}", |
| 1721 | vfiopcidevice.debug_label() |
| 1722 | ); |
| 1723 | } |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1724 | pci_devices.push((vfiopcidevice, simple_jail(&cfg, "vfio_device")?)); |
| 1725 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1726 | } |
| 1727 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1728 | Ok(pci_devices) |
| 1729 | } |
| 1730 | |
| 1731 | #[derive(Copy, Clone)] |
Chirantan Ekbote | 1a2683b | 2019-11-26 16:28:23 +0900 | [diff] [blame] | 1732 | #[cfg_attr(not(feature = "tpm"), allow(dead_code))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1733 | struct Ids { |
| 1734 | uid: uid_t, |
| 1735 | gid: gid_t, |
| 1736 | } |
| 1737 | |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1738 | // Set the uid/gid for the jailed process and give a basic id map. This is |
| 1739 | // required for bind mounts to work. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1740 | fn add_crosvm_user_to_jail(jail: &mut Minijail, feature: &str) -> Result<Ids> { |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1741 | let crosvm_user_group = CStr::from_bytes_with_nul(b"crosvm\0").unwrap(); |
| 1742 | |
| 1743 | let crosvm_uid = match get_user_id(&crosvm_user_group) { |
| 1744 | Ok(u) => u, |
| 1745 | Err(e) => { |
| 1746 | warn!("falling back to current user id for {}: {}", feature, e); |
| 1747 | geteuid() |
| 1748 | } |
| 1749 | }; |
| 1750 | |
| 1751 | let crosvm_gid = match get_group_id(&crosvm_user_group) { |
| 1752 | Ok(u) => u, |
| 1753 | Err(e) => { |
| 1754 | warn!("falling back to current group id for {}: {}", feature, e); |
| 1755 | getegid() |
| 1756 | } |
| 1757 | }; |
| 1758 | |
| 1759 | jail.change_uid(crosvm_uid); |
| 1760 | jail.change_gid(crosvm_gid); |
| 1761 | jail.uidmap(&format!("{0} {0} 1", crosvm_uid)) |
| 1762 | .map_err(Error::SettingUidMap)?; |
| 1763 | jail.gidmap(&format!("{0} {0} 1", crosvm_gid)) |
| 1764 | .map_err(Error::SettingGidMap)?; |
| 1765 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 1766 | Ok(Ids { |
| 1767 | uid: crosvm_uid, |
| 1768 | gid: crosvm_gid, |
| 1769 | }) |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1772 | fn raw_descriptor_from_path(path: &Path) -> Result<RawDescriptor> { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1773 | if !path.is_file() { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1774 | return Err(Error::InvalidFdPath); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1775 | } |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1776 | let raw_descriptor = path |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1777 | .file_name() |
| 1778 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 1779 | .and_then(|fd_str| fd_str.parse::<c_int>().ok()) |
| 1780 | .ok_or(Error::InvalidFdPath)?; |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1781 | validate_raw_descriptor(raw_descriptor).map_err(Error::ValidateRawDescriptor) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1782 | } |
| 1783 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1784 | trait IntoUnixStream { |
| 1785 | fn into_unix_stream(self) -> Result<UnixStream>; |
| 1786 | } |
| 1787 | |
| 1788 | impl<'a> IntoUnixStream for &'a Path { |
| 1789 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1790 | if self.parent() == Some(Path::new("/proc/self/fd")) { |
| 1791 | // Safe because we will validate |raw_fd|. |
Michael Hoyle | a596a07 | 2020-11-10 19:32:45 -0800 | [diff] [blame] | 1792 | unsafe { Ok(UnixStream::from_raw_fd(raw_descriptor_from_path(self)?)) } |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1793 | } else { |
| 1794 | UnixStream::connect(self).map_err(Error::InputEventsOpen) |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | impl<'a> IntoUnixStream for &'a PathBuf { |
| 1799 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1800 | self.as_path().into_unix_stream() |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | impl IntoUnixStream for UnixStream { |
| 1805 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1806 | Ok(self) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1810 | fn setup_vcpu_signal_handler<T: Vcpu>(use_hypervisor_signals: bool) -> Result<()> { |
| 1811 | if use_hypervisor_signals { |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1812 | unsafe { |
Allen Webb | 44c728c | 2021-03-23 15:22:41 -0500 | [diff] [blame] | 1813 | extern "C" fn handle_signal(_: c_int) {} |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1814 | // Our signal handler does nothing and is trivially async signal safe. |
| 1815 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal) |
| 1816 | .map_err(Error::RegisterSignalHandler)?; |
| 1817 | } |
| 1818 | block_signal(SIGRTMIN() + 0).map_err(Error::BlockSignal)?; |
| 1819 | } else { |
| 1820 | unsafe { |
Allen Webb | 44c728c | 2021-03-23 15:22:41 -0500 | [diff] [blame] | 1821 | extern "C" fn handle_signal<T: Vcpu>(_: c_int) { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1822 | T::set_local_immediate_exit(true); |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1823 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1824 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal::<T>) |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1825 | .map_err(Error::RegisterSignalHandler)?; |
| 1826 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1827 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1828 | Ok(()) |
| 1829 | } |
| 1830 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1831 | // Sets up a vcpu and converts it into a runnable vcpu. |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1832 | fn runnable_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1833 | cpu_id: usize, |
| 1834 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1835 | vm: impl VmArch, |
| 1836 | irq_chip: &mut impl IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1837 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1838 | run_rt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1839 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1840 | no_smt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1841 | has_bios: bool, |
| 1842 | use_hypervisor_signals: bool, |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1843 | ) -> Result<(V, VcpuRunHandle)> |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1844 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1845 | V: VcpuArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1846 | { |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1847 | let mut vcpu = match vcpu { |
| 1848 | Some(v) => v, |
| 1849 | None => { |
| 1850 | // If vcpu is None, it means this arch/hypervisor requires create_vcpu to be called from |
| 1851 | // the vcpu thread. |
| 1852 | match vm |
| 1853 | .create_vcpu(cpu_id) |
| 1854 | .map_err(Error::CreateVcpu)? |
| 1855 | .downcast::<V>() |
| 1856 | { |
| 1857 | Ok(v) => *v, |
| 1858 | Err(_) => panic!("VM created wrong type of VCPU"), |
| 1859 | } |
| 1860 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1861 | }; |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1862 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1863 | irq_chip |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1864 | .add_vcpu(cpu_id, &vcpu) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1865 | .map_err(Error::AddIrqChipVcpu)?; |
| 1866 | |
Daniel Verkamp | caf9ced | 2020-09-29 15:35:02 -0700 | [diff] [blame] | 1867 | if !vcpu_affinity.is_empty() { |
| 1868 | if let Err(e) = set_cpu_affinity(vcpu_affinity) { |
| 1869 | error!("Failed to set CPU affinity: {}", e); |
| 1870 | } |
| 1871 | } |
| 1872 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1873 | Arch::configure_vcpu( |
| 1874 | vm.get_memory(), |
| 1875 | vm.get_hypervisor(), |
| 1876 | irq_chip, |
| 1877 | &mut vcpu, |
| 1878 | cpu_id, |
| 1879 | vcpu_count, |
| 1880 | has_bios, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1881 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1882 | ) |
| 1883 | .map_err(Error::ConfigureVcpu)?; |
| 1884 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1885 | #[cfg(feature = "chromeos")] |
| 1886 | if let Err(e) = base::sched::enable_core_scheduling() { |
| 1887 | error!("Failed to enable core scheduling: {}", e); |
| 1888 | } |
| 1889 | |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1890 | if run_rt { |
| 1891 | const DEFAULT_VCPU_RT_LEVEL: u16 = 6; |
| 1892 | if let Err(e) = set_rt_prio_limit(u64::from(DEFAULT_VCPU_RT_LEVEL)) |
| 1893 | .and_then(|_| set_rt_round_robin(i32::from(DEFAULT_VCPU_RT_LEVEL))) |
| 1894 | { |
| 1895 | warn!("Failed to set vcpu to real time: {}", e); |
| 1896 | } |
| 1897 | } |
| 1898 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1899 | if use_hypervisor_signals { |
| 1900 | let mut v = get_blocked_signals().map_err(Error::GetSignalMask)?; |
| 1901 | v.retain(|&x| x != SIGRTMIN() + 0); |
| 1902 | vcpu.set_signal_mask(&v).map_err(Error::SettingSignalMask)?; |
| 1903 | } |
| 1904 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1905 | let vcpu_run_handle = vcpu |
| 1906 | .take_run_handle(Some(SIGRTMIN() + 0)) |
| 1907 | .map_err(Error::RunnableVcpu)?; |
| 1908 | |
| 1909 | Ok((vcpu, vcpu_run_handle)) |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1910 | } |
| 1911 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1912 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 1913 | fn handle_debug_msg<V>( |
| 1914 | cpu_id: usize, |
| 1915 | vcpu: &V, |
| 1916 | guest_mem: &GuestMemory, |
| 1917 | d: VcpuDebug, |
| 1918 | reply_channel: &mpsc::Sender<VcpuDebugStatusMessage>, |
| 1919 | ) -> Result<()> |
| 1920 | where |
| 1921 | V: VcpuArch + 'static, |
| 1922 | { |
| 1923 | match d { |
| 1924 | VcpuDebug::ReadRegs => { |
| 1925 | let msg = VcpuDebugStatusMessage { |
| 1926 | cpu: cpu_id as usize, |
| 1927 | msg: VcpuDebugStatus::RegValues( |
| 1928 | Arch::debug_read_registers(vcpu as &V).map_err(Error::HandleDebugCommand)?, |
| 1929 | ), |
| 1930 | }; |
| 1931 | reply_channel |
| 1932 | .send(msg) |
| 1933 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1934 | } |
| 1935 | VcpuDebug::WriteRegs(regs) => { |
| 1936 | Arch::debug_write_registers(vcpu as &V, ®s).map_err(Error::HandleDebugCommand)?; |
| 1937 | reply_channel |
| 1938 | .send(VcpuDebugStatusMessage { |
| 1939 | cpu: cpu_id as usize, |
| 1940 | msg: VcpuDebugStatus::CommandComplete, |
| 1941 | }) |
| 1942 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1943 | } |
| 1944 | VcpuDebug::ReadMem(vaddr, len) => { |
| 1945 | let msg = VcpuDebugStatusMessage { |
| 1946 | cpu: cpu_id as usize, |
| 1947 | msg: VcpuDebugStatus::MemoryRegion( |
| 1948 | Arch::debug_read_memory(vcpu as &V, guest_mem, vaddr, len) |
| 1949 | .unwrap_or(Vec::new()), |
| 1950 | ), |
| 1951 | }; |
| 1952 | reply_channel |
| 1953 | .send(msg) |
| 1954 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1955 | } |
| 1956 | VcpuDebug::WriteMem(vaddr, buf) => { |
| 1957 | Arch::debug_write_memory(vcpu as &V, guest_mem, vaddr, &buf) |
| 1958 | .map_err(Error::HandleDebugCommand)?; |
| 1959 | reply_channel |
| 1960 | .send(VcpuDebugStatusMessage { |
| 1961 | cpu: cpu_id as usize, |
| 1962 | msg: VcpuDebugStatus::CommandComplete, |
| 1963 | }) |
| 1964 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1965 | } |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 1966 | VcpuDebug::EnableSinglestep => { |
| 1967 | Arch::debug_enable_singlestep(vcpu as &V).map_err(Error::HandleDebugCommand)?; |
| 1968 | reply_channel |
| 1969 | .send(VcpuDebugStatusMessage { |
| 1970 | cpu: cpu_id as usize, |
| 1971 | msg: VcpuDebugStatus::CommandComplete, |
| 1972 | }) |
| 1973 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1974 | } |
| 1975 | VcpuDebug::SetHwBreakPoint(addrs) => { |
| 1976 | Arch::debug_set_hw_breakpoints(vcpu as &V, &addrs) |
| 1977 | .map_err(Error::HandleDebugCommand)?; |
| 1978 | reply_channel |
| 1979 | .send(VcpuDebugStatusMessage { |
| 1980 | cpu: cpu_id as usize, |
| 1981 | msg: VcpuDebugStatus::CommandComplete, |
| 1982 | }) |
| 1983 | .map_err(|e| Error::SendDebugStatus(Box::new(e))) |
| 1984 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1988 | fn run_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1989 | cpu_id: usize, |
| 1990 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1991 | vm: impl VmArch + 'static, |
| 1992 | mut irq_chip: impl IrqChipArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1993 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1994 | run_rt: bool, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1995 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1996 | no_smt: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1997 | start_barrier: Arc<Barrier>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1998 | has_bios: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1999 | io_bus: devices::Bus, |
| 2000 | mmio_bus: devices::Bus, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2001 | exit_evt: Event, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2002 | requires_pvclock_ctrl: bool, |
Dylan Reid | 3d63706 | 2019-05-19 15:06:26 -0700 | [diff] [blame] | 2003 | from_main_channel: mpsc::Receiver<VcpuControl>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2004 | use_hypervisor_signals: bool, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2005 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] to_gdb_channel: Option< |
| 2006 | mpsc::Sender<VcpuDebugStatusMessage>, |
| 2007 | >, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2008 | ) -> Result<JoinHandle<()>> |
| 2009 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2010 | V: VcpuArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2011 | { |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 2012 | thread::Builder::new() |
| 2013 | .name(format!("crosvm_vcpu{}", cpu_id)) |
| 2014 | .spawn(move || { |
Zach Reizner | 9588531 | 2020-01-29 18:06:01 -0800 | [diff] [blame] | 2015 | // The VCPU thread must trigger the `exit_evt` in all paths, and a `ScopedEvent`'s Drop |
| 2016 | // implementation accomplishes that. |
| 2017 | let _scoped_exit_evt = ScopedEvent::from(exit_evt); |
| 2018 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2019 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2020 | let guest_mem = vm.get_memory().clone(); |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2021 | let runnable_vcpu = runnable_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2022 | cpu_id, |
| 2023 | vcpu, |
| 2024 | vm, |
| 2025 | &mut irq_chip, |
| 2026 | vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2027 | run_rt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2028 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2029 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2030 | has_bios, |
| 2031 | use_hypervisor_signals, |
| 2032 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2033 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 2034 | start_barrier.wait(); |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 2035 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 2036 | let (vcpu, vcpu_run_handle) = match runnable_vcpu { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2037 | Ok(v) => v, |
| 2038 | Err(e) => { |
| 2039 | error!("failed to start vcpu {}: {}", cpu_id, e); |
| 2040 | return; |
| 2041 | } |
| 2042 | }; |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 2043 | |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2044 | let mut run_mode = VmRunMode::Running; |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2045 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2046 | if to_gdb_channel.is_some() { |
| 2047 | // Wait until a GDB client attaches |
| 2048 | run_mode = VmRunMode::Breakpoint; |
| 2049 | } |
| 2050 | |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2051 | let mut interrupted_by_signal = false; |
| 2052 | |
| 2053 | 'vcpu_loop: loop { |
| 2054 | // Start by checking for messages to process and the run state of the CPU. |
| 2055 | // An extra check here for Running so there isn't a need to call recv unless a |
| 2056 | // message is likely to be ready because a signal was sent. |
| 2057 | if interrupted_by_signal || run_mode != VmRunMode::Running { |
| 2058 | 'state_loop: loop { |
| 2059 | // Tries to get a pending message without blocking first. |
| 2060 | let msg = match from_main_channel.try_recv() { |
| 2061 | Ok(m) => m, |
| 2062 | Err(mpsc::TryRecvError::Empty) if run_mode == VmRunMode::Running => { |
| 2063 | // If the VM is running and no message is pending, the state won't |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2064 | // change. |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2065 | break 'state_loop; |
| 2066 | } |
| 2067 | Err(mpsc::TryRecvError::Empty) => { |
| 2068 | // If the VM is not running, wait until a message is ready. |
| 2069 | match from_main_channel.recv() { |
| 2070 | Ok(m) => m, |
| 2071 | Err(mpsc::RecvError) => { |
| 2072 | error!("Failed to read from main channel in vcpu"); |
| 2073 | break 'vcpu_loop; |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | Err(mpsc::TryRecvError::Disconnected) => { |
| 2078 | error!("Failed to read from main channel in vcpu"); |
| 2079 | break 'vcpu_loop; |
| 2080 | } |
| 2081 | }; |
| 2082 | |
| 2083 | // Collect all pending messages. |
| 2084 | let mut messages = vec![msg]; |
| 2085 | messages.append(&mut from_main_channel.try_iter().collect()); |
| 2086 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2087 | for msg in messages { |
| 2088 | match msg { |
| 2089 | VcpuControl::RunState(new_mode) => { |
| 2090 | run_mode = new_mode; |
| 2091 | match run_mode { |
| 2092 | VmRunMode::Running => break 'state_loop, |
| 2093 | VmRunMode::Suspending => { |
| 2094 | // On KVM implementations that use a paravirtualized |
| 2095 | // clock (e.g. x86), a flag must be set to indicate to |
| 2096 | // the guest kernel that a vCPU was suspended. The guest |
| 2097 | // kernel will use this flag to prevent the soft lockup |
| 2098 | // detection from triggering when this vCPU resumes, |
| 2099 | // which could happen days later in realtime. |
| 2100 | if requires_pvclock_ctrl { |
| 2101 | if let Err(e) = vcpu.pvclock_ctrl() { |
| 2102 | error!( |
| 2103 | "failed to tell hypervisor vcpu {} is suspending: {}", |
| 2104 | cpu_id, e |
| 2105 | ); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | VmRunMode::Breakpoint => {} |
| 2110 | VmRunMode::Exiting => break 'vcpu_loop, |
| 2111 | } |
| 2112 | } |
| 2113 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2114 | VcpuControl::Debug(d) => { |
| 2115 | match &to_gdb_channel { |
| 2116 | Some(ref ch) => { |
| 2117 | if let Err(e) = handle_debug_msg( |
| 2118 | cpu_id, &vcpu, &guest_mem, d, &ch, |
| 2119 | ) { |
| 2120 | error!("Failed to handle gdb message: {}", e); |
| 2121 | } |
| 2122 | }, |
| 2123 | None => { |
| 2124 | error!("VcpuControl::Debug received while GDB feature is disabled: {:?}", d); |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | } |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | interrupted_by_signal = false; |
| 2134 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2135 | // Vcpus may have run a HLT instruction, which puts them into a state other than |
| 2136 | // VcpuRunState::Runnable. In that case, this call to wait_until_runnable blocks |
| 2137 | // until either the irqchip receives an interrupt for this vcpu, or until the main |
| 2138 | // thread kicks this vcpu as a result of some VmControl operation. In most IrqChip |
| 2139 | // implementations HLT instructions do not make it to crosvm, and thus this is a |
| 2140 | // no-op that always returns VcpuRunState::Runnable. |
| 2141 | match irq_chip.wait_until_runnable(&vcpu) { |
| 2142 | Ok(VcpuRunState::Runnable) => {} |
| 2143 | Ok(VcpuRunState::Interrupted) => interrupted_by_signal = true, |
| 2144 | Err(e) => error!( |
| 2145 | "error waiting for vcpu {} to become runnable: {}", |
| 2146 | cpu_id, e |
| 2147 | ), |
| 2148 | } |
| 2149 | |
| 2150 | if !interrupted_by_signal { |
| 2151 | match vcpu.run(&vcpu_run_handle) { |
| 2152 | Ok(VcpuExit::IoIn { port, mut size }) => { |
| 2153 | let mut data = [0; 8]; |
| 2154 | if size > data.len() { |
| 2155 | error!("unsupported IoIn size of {} bytes", size); |
| 2156 | size = data.len(); |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2157 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2158 | io_bus.read(port as u64, &mut data[..size]); |
| 2159 | if let Err(e) = vcpu.set_data(&data[..size]) { |
| 2160 | error!("failed to set return data for IoIn: {}", e); |
| 2161 | } |
Keiichi Watanabe | 23f9471 | 2020-10-22 17:43:06 +0900 | [diff] [blame] | 2162 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2163 | Ok(VcpuExit::IoOut { |
| 2164 | port, |
| 2165 | mut size, |
| 2166 | data, |
| 2167 | }) => { |
| 2168 | if size > data.len() { |
| 2169 | error!("unsupported IoOut size of {} bytes", size); |
| 2170 | size = data.len(); |
| 2171 | } |
| 2172 | io_bus.write(port as u64, &data[..size]); |
| 2173 | } |
| 2174 | Ok(VcpuExit::MmioRead { address, size }) => { |
| 2175 | let mut data = [0; 8]; |
| 2176 | mmio_bus.read(address, &mut data[..size]); |
| 2177 | // Setting data for mmio can not fail. |
| 2178 | let _ = vcpu.set_data(&data[..size]); |
| 2179 | } |
| 2180 | Ok(VcpuExit::MmioWrite { |
| 2181 | address, |
| 2182 | size, |
| 2183 | data, |
| 2184 | }) => { |
| 2185 | mmio_bus.write(address, &data[..size]); |
| 2186 | } |
| 2187 | Ok(VcpuExit::IoapicEoi { vector }) => { |
| 2188 | if let Err(e) = irq_chip.broadcast_eoi(vector) { |
| 2189 | error!( |
| 2190 | "failed to broadcast eoi {} on vcpu {}: {}", |
| 2191 | vector, cpu_id, e |
| 2192 | ); |
| 2193 | } |
| 2194 | } |
| 2195 | Ok(VcpuExit::IrqWindowOpen) => {} |
| 2196 | Ok(VcpuExit::Hlt) => irq_chip.halted(cpu_id), |
| 2197 | Ok(VcpuExit::Shutdown) => break, |
| 2198 | Ok(VcpuExit::FailEntry { |
| 2199 | hardware_entry_failure_reason, |
| 2200 | }) => { |
| 2201 | error!("vcpu hw run failure: {:#x}", hardware_entry_failure_reason); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2202 | break; |
| 2203 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2204 | Ok(VcpuExit::SystemEvent(_, _)) => break, |
| 2205 | Ok(VcpuExit::Debug { .. }) => { |
| 2206 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2207 | { |
| 2208 | let msg = VcpuDebugStatusMessage { |
| 2209 | cpu: cpu_id as usize, |
| 2210 | msg: VcpuDebugStatus::HitBreakPoint, |
| 2211 | }; |
| 2212 | if let Some(ref ch) = to_gdb_channel { |
| 2213 | if let Err(e) = ch.send(msg) { |
| 2214 | error!("failed to notify breakpoint to GDB thread: {}", e); |
| 2215 | break; |
| 2216 | } |
| 2217 | } |
| 2218 | run_mode = VmRunMode::Breakpoint; |
| 2219 | } |
| 2220 | } |
| 2221 | Ok(r) => warn!("unexpected vcpu exit: {:?}", r), |
| 2222 | Err(e) => match e.errno() { |
| 2223 | libc::EINTR => interrupted_by_signal = true, |
| 2224 | libc::EAGAIN => {} |
| 2225 | _ => { |
| 2226 | error!("vcpu hit unknown error: {}", e); |
| 2227 | break; |
| 2228 | } |
| 2229 | }, |
| 2230 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | if interrupted_by_signal { |
| 2234 | if use_hypervisor_signals { |
| 2235 | // Try to clear the signal that we use to kick VCPU if it is pending before |
| 2236 | // attempting to handle pause requests. |
| 2237 | if let Err(e) = clear_signal(SIGRTMIN() + 0) { |
| 2238 | error!("failed to clear pending signal: {}", e); |
| 2239 | break; |
| 2240 | } |
| 2241 | } else { |
| 2242 | vcpu.set_immediate_exit(false); |
| 2243 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 2244 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2245 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2246 | if let Err(e) = irq_chip.inject_interrupts(&vcpu) { |
| 2247 | error!("failed to inject interrupts for vcpu {}: {}", cpu_id, e); |
| 2248 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2249 | } |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 2250 | }) |
| 2251 | .map_err(Error::SpawnVcpu) |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2254 | // Reads the contents of a file and converts the space-separated fields into a Vec of i64s. |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2255 | // Returns an error if any of the fields fail to parse. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2256 | fn file_fields_to_i64<P: AsRef<Path>>(path: P) -> io::Result<Vec<i64>> { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2257 | let mut file = File::open(path)?; |
| 2258 | |
| 2259 | let mut buf = [0u8; 32]; |
| 2260 | let count = file.read(&mut buf)?; |
| 2261 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2262 | let content = |
| 2263 | str::from_utf8(&buf[..count]).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 2264 | content |
| 2265 | .trim() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2266 | .split_whitespace() |
| 2267 | .map(|x| { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2268 | x.parse::<i64>() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2269 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 2270 | }) |
| 2271 | .collect() |
| 2272 | } |
| 2273 | |
| 2274 | // Reads the contents of a file and converts them into a u64, and if there |
| 2275 | // are multiple fields it only returns the first one. |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2276 | fn file_to_i64<P: AsRef<Path>>(path: P, nth: usize) -> io::Result<i64> { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2277 | file_fields_to_i64(path)? |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2278 | .into_iter() |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2279 | .nth(nth) |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 2280 | .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty file")) |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 2283 | fn create_kvm(device_path: &Path, mem: GuestMemory) -> base::Result<KvmVm> { |
| 2284 | let kvm = Kvm::new_with_path(device_path)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2285 | let vm = KvmVm::new(&kvm, mem)?; |
| 2286 | Ok(vm) |
| 2287 | } |
| 2288 | |
| 2289 | fn create_kvm_kernel_irq_chip( |
| 2290 | vm: &KvmVm, |
| 2291 | vcpu_count: usize, |
| 2292 | _ioapic_device_socket: VmIrqRequestSocket, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2293 | ) -> base::Result<impl IrqChipArch> { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2294 | let irq_chip = KvmKernelIrqChip::new(vm.try_clone()?, vcpu_count)?; |
| 2295 | Ok(irq_chip) |
| 2296 | } |
| 2297 | |
| 2298 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2299 | fn create_kvm_split_irq_chip( |
| 2300 | vm: &KvmVm, |
| 2301 | vcpu_count: usize, |
| 2302 | ioapic_device_socket: VmIrqRequestSocket, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2303 | ) -> base::Result<impl IrqChipArch> { |
Tomasz Jeznach | eb1114c | 2021-02-26 20:33:11 -0800 | [diff] [blame] | 2304 | let irq_chip = |
| 2305 | KvmSplitIrqChip::new(vm.try_clone()?, vcpu_count, ioapic_device_socket, Some(120))?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2306 | Ok(irq_chip) |
| 2307 | } |
| 2308 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2309 | pub fn run_config(cfg: Config) -> Result<()> { |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 2310 | let kvm_device_path = cfg.kvm_device_path.clone(); |
| 2311 | let create_kvm_with_path = |mem| create_kvm(&kvm_device_path, mem); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2312 | if cfg.split_irqchip { |
| 2313 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
| 2314 | { |
| 2315 | unimplemented!("KVM split irqchip mode only supported on x86 processors") |
| 2316 | } |
| 2317 | |
| 2318 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2319 | { |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 2320 | run_vm::<_, KvmVcpu, _, _, _>(cfg, create_kvm_with_path, create_kvm_split_irq_chip) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2321 | } |
| 2322 | } else { |
Christian Blichmann | 33d5677 | 2021-03-04 19:03:54 +0100 | [diff] [blame] | 2323 | run_vm::<_, KvmVcpu, _, _, _>(cfg, create_kvm_with_path, create_kvm_kernel_irq_chip) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2324 | } |
| 2325 | } |
| 2326 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2327 | fn run_vm<V, Vcpu, I, FV, FI>(cfg: Config, create_vm: FV, create_irq_chip: FI) -> Result<()> |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2328 | where |
| 2329 | V: VmArch + 'static, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2330 | Vcpu: VcpuArch + 'static, |
| 2331 | I: IrqChipArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2332 | FV: FnOnce(GuestMemory) -> base::Result<V>, |
| 2333 | FI: FnOnce( |
| 2334 | &V, |
| 2335 | usize, // vcpu_count |
| 2336 | VmIrqRequestSocket, // ioapic_device_socket |
| 2337 | ) -> base::Result<I>, |
| 2338 | { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 2339 | if cfg.sandbox { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2340 | // Printing something to the syslog before entering minijail so that libc's syslogger has a |
| 2341 | // chance to open files necessary for its operation, like `/etc/localtime`. After jailing, |
| 2342 | // access to those files will not be possible. |
| 2343 | info!("crosvm entering multiprocess mode"); |
| 2344 | } |
| 2345 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2346 | let (usb_control_socket, usb_provider) = |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 2347 | HostBackendDeviceProvider::new().map_err(Error::CreateUsbProvider)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2348 | // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this |
| 2349 | // before any jailed devices have been spawned, so that we can catch any of them that fail very |
| 2350 | // quickly. |
| 2351 | let sigchld_fd = SignalFd::new(libc::SIGCHLD).map_err(Error::CreateSignalFd)?; |
| 2352 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 2353 | let initrd_image = if let Some(initrd_path) = &cfg.initrd_path { |
| 2354 | Some(File::open(initrd_path).map_err(|e| Error::OpenInitrd(initrd_path.clone(), e))?) |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 2355 | } else { |
| 2356 | None |
| 2357 | }; |
| 2358 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 2359 | let vm_image = match cfg.executable_path { |
| 2360 | Some(Executable::Kernel(ref kernel_path)) => VmImage::Kernel( |
| 2361 | File::open(kernel_path).map_err(|e| Error::OpenKernel(kernel_path.to_path_buf(), e))?, |
| 2362 | ), |
| 2363 | Some(Executable::Bios(ref bios_path)) => VmImage::Bios( |
| 2364 | File::open(bios_path).map_err(|e| Error::OpenBios(bios_path.to_path_buf(), e))?, |
| 2365 | ), |
| 2366 | _ => panic!("Did not receive a bios or kernel, should be impossible."), |
| 2367 | }; |
| 2368 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2369 | let mut control_sockets = Vec::new(); |
| 2370 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2371 | let gdb_socket = if let Some(port) = cfg.gdb { |
| 2372 | // GDB needs a control socket to interrupt vcpus. |
| 2373 | let (gdb_host_socket, gdb_control_socket) = |
| 2374 | msg_socket::pair::<VmResponse, VmRequest>().map_err(Error::CreateSocket)?; |
| 2375 | control_sockets.push(TaggedControlSocket::Vm(gdb_host_socket)); |
| 2376 | Some((port, gdb_control_socket)) |
| 2377 | } else { |
| 2378 | None |
| 2379 | }; |
| 2380 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2381 | let components = VmComponents { |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 2382 | memory_size: cfg |
| 2383 | .memory |
| 2384 | .unwrap_or(256) |
| 2385 | .checked_mul(1024 * 1024) |
| 2386 | .ok_or(Error::MemoryTooLarge)?, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2387 | vcpu_count: cfg.vcpu_count.unwrap_or(1), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 2388 | vcpu_affinity: cfg.vcpu_affinity.clone(), |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2389 | no_smt: cfg.no_smt, |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 2390 | vm_image, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 2391 | android_fstab: cfg |
| 2392 | .android_fstab |
| 2393 | .as_ref() |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 2394 | .map(|x| File::open(x).map_err(|e| Error::OpenAndroidFstab(x.to_path_buf(), e))) |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 2395 | .map_or(Ok(None), |v| v.map(Some))?, |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 2396 | pstore: cfg.pstore.clone(), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 2397 | initrd_image, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 2398 | extra_kernel_params: cfg.params.clone(), |
| 2399 | wayland_dmabuf: cfg.wayland_dmabuf, |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 2400 | acpi_sdts: cfg |
| 2401 | .acpi_tables |
| 2402 | .iter() |
| 2403 | .map(|path| SDT::from_file(path).map_err(|e| Error::OpenAcpiTable(path.clone(), e))) |
| 2404 | .collect::<Result<Vec<SDT>>>()?, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2405 | rt_cpus: cfg.rt_cpus.clone(), |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 2406 | protected_vm: cfg.protected_vm, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2407 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2408 | gdb: gdb_socket, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2409 | }; |
| 2410 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2411 | let control_server_socket = match &cfg.socket_path { |
| 2412 | Some(path) => Some(UnlinkUnixSeqpacketListener( |
| 2413 | UnixSeqpacketListener::bind(path).map_err(Error::CreateSocket)?, |
| 2414 | )), |
| 2415 | None => None, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2416 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2417 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2418 | let (wayland_host_socket, wayland_device_socket) = |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 2419 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>().map_err(Error::CreateSocket)?; |
| 2420 | control_sockets.push(TaggedControlSocket::VmMemory(wayland_host_socket)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2421 | // Balloon gets a special socket so balloon requests can be forwarded from the main process. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2422 | let (balloon_host_socket, balloon_device_socket) = |
Charles William Dick | 664cc3c | 2020-01-10 14:31:52 +0900 | [diff] [blame] | 2423 | msg_socket::pair::<BalloonControlCommand, BalloonControlResult>() |
| 2424 | .map_err(Error::CreateSocket)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2425 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2426 | // Create one control socket per disk. |
| 2427 | let mut disk_device_sockets = Vec::new(); |
| 2428 | let mut disk_host_sockets = Vec::new(); |
| 2429 | let disk_count = cfg.disks.len(); |
| 2430 | for _ in 0..disk_count { |
| 2431 | let (disk_host_socket, disk_device_socket) = |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2432 | msg_socket::pair::<DiskControlCommand, DiskControlResult>() |
| 2433 | .map_err(Error::CreateSocket)?; |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2434 | disk_host_sockets.push(disk_host_socket); |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 2435 | disk_device_sockets.push(disk_device_socket); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2438 | let mut pmem_device_sockets = Vec::new(); |
| 2439 | let pmem_count = cfg.pmem_devices.len(); |
| 2440 | for _ in 0..pmem_count { |
| 2441 | let (pmem_host_socket, pmem_device_socket) = |
| 2442 | msg_socket::pair::<VmMsyncResponse, VmMsyncRequest>().map_err(Error::CreateSocket)?; |
| 2443 | pmem_device_sockets.push(pmem_device_socket); |
| 2444 | control_sockets.push(TaggedControlSocket::VmMsync(pmem_host_socket)); |
| 2445 | } |
| 2446 | |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 2447 | let (gpu_host_socket, gpu_device_socket) = |
| 2448 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>().map_err(Error::CreateSocket)?; |
| 2449 | control_sockets.push(TaggedControlSocket::VmMemory(gpu_host_socket)); |
| 2450 | |
Zhuocheng Ding | f2e90bf | 2019-12-02 15:50:20 +0800 | [diff] [blame] | 2451 | let (ioapic_host_socket, ioapic_device_socket) = |
| 2452 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 2453 | control_sockets.push(TaggedControlSocket::VmIrq(ioapic_host_socket)); |
| 2454 | |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2455 | let battery = if cfg.battery_type.is_some() { |
Alex Lau | f408c73 | 2020-11-10 18:24:04 +0900 | [diff] [blame] | 2456 | let jail = match simple_jail(&cfg, "battery")? { |
| 2457 | #[cfg_attr(not(feature = "powerd-monitor-powerd"), allow(unused_mut))] |
| 2458 | Some(mut jail) => { |
| 2459 | // Setup a bind mount to the system D-Bus socket if the powerd monitor is used. |
| 2460 | #[cfg(feature = "power-monitor-powerd")] |
| 2461 | { |
| 2462 | add_crosvm_user_to_jail(&mut jail, "battery")?; |
| 2463 | |
| 2464 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 2465 | jail.mount_with_data( |
| 2466 | Path::new("none"), |
| 2467 | Path::new("/"), |
| 2468 | "tmpfs", |
| 2469 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 2470 | "size=67108864", |
| 2471 | )?; |
| 2472 | |
| 2473 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 2474 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 2475 | } |
| 2476 | Some(jail) |
| 2477 | } |
| 2478 | None => None, |
| 2479 | }; |
| 2480 | (&cfg.battery_type, jail) |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2481 | } else { |
| 2482 | (&cfg.battery_type, None) |
| 2483 | }; |
| 2484 | |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2485 | let gralloc = RutabagaGralloc::new().map_err(Error::CreateGrallocError)?; |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2486 | let map_request: Arc<Mutex<Option<ExternalMapping>>> = Arc::new(Mutex::new(None)); |
| 2487 | |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2488 | let fs_count = cfg |
| 2489 | .shared_dirs |
| 2490 | .iter() |
| 2491 | .filter(|sd| sd.kind == SharedDirKind::FS) |
| 2492 | .count(); |
| 2493 | let mut fs_device_sockets = Vec::with_capacity(fs_count); |
| 2494 | for _ in 0..fs_count { |
| 2495 | let (fs_host_socket, fs_device_socket) = |
| 2496 | msg_socket::pair::<VmResponse, FsMappingRequest>().map_err(Error::CreateSocket)?; |
| 2497 | control_sockets.push(TaggedControlSocket::Fs(fs_host_socket)); |
| 2498 | fs_device_sockets.push(fs_device_socket); |
| 2499 | } |
| 2500 | |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame^] | 2501 | let mut linux: RunnableLinuxVm<_, Vcpu, _> = Arch::build_vm( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2502 | components, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2503 | &cfg.serial_parameters, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 2504 | simple_jail(&cfg, "serial")?, |
Chuanxiao Dong | bbb32af | 2020-04-27 16:37:10 +0800 | [diff] [blame] | 2505 | battery, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 2506 | |mem, vm, sys_allocator, exit_evt| { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2507 | create_devices( |
| 2508 | &cfg, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 2509 | mem, |
| 2510 | vm, |
| 2511 | sys_allocator, |
| 2512 | exit_evt, |
Xiong Zhang | a5d248c | 2019-09-17 14:17:19 -0700 | [diff] [blame] | 2513 | &mut control_sockets, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2514 | wayland_device_socket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 2515 | gpu_device_socket, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2516 | balloon_device_socket, |
| 2517 | &mut disk_device_sockets, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2518 | &mut pmem_device_sockets, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 2519 | &mut fs_device_sockets, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2520 | usb_provider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2521 | Arc::clone(&map_request), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2522 | ) |
| 2523 | }, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2524 | create_vm, |
| 2525 | |vm, vcpu_count| create_irq_chip(vm, vcpu_count, ioapic_device_socket), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2526 | ) |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 2527 | .map_err(Error::BuildVm)?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 2528 | |
Tomasz Jeznach | 3ce7476 | 2021-02-26 01:01:53 -0800 | [diff] [blame^] | 2529 | #[cfg(feature = "direct")] |
| 2530 | if let Some(pmio) = &cfg.direct_pmio { |
| 2531 | let direct_io = |
| 2532 | Arc::new(devices::DirectIo::new(&pmio.path, false).map_err(Error::DirectIo)?); |
| 2533 | for range in pmio.ranges.iter() { |
| 2534 | linux |
| 2535 | .io_bus |
| 2536 | .insert_sync(direct_io.clone(), range.0, range.1) |
| 2537 | .unwrap(); |
| 2538 | } |
| 2539 | }; |
| 2540 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2541 | run_control( |
| 2542 | linux, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2543 | control_server_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2544 | control_sockets, |
| 2545 | balloon_host_socket, |
| 2546 | &disk_host_sockets, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2547 | usb_control_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2548 | sigchld_fd, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2549 | cfg.sandbox, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2550 | Arc::clone(&map_request), |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2551 | cfg.balloon_bias, |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2552 | gralloc, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2553 | ) |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 2554 | } |
| 2555 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2556 | /// Signals all running VCPUs to vmexit, sends VmRunMode message to each VCPU channel, and tells |
| 2557 | /// `irq_chip` to stop blocking halted VCPUs. The channel message is set first because both the |
| 2558 | /// signal and the irq_chip kick could cause the VCPU thread to continue through the VCPU run |
| 2559 | /// loop. |
| 2560 | fn kick_all_vcpus( |
| 2561 | vcpu_handles: &[(JoinHandle<()>, mpsc::Sender<vm_control::VcpuControl>)], |
| 2562 | irq_chip: &impl IrqChip, |
| 2563 | run_mode: &VmRunMode, |
| 2564 | ) { |
| 2565 | for (handle, channel) in vcpu_handles { |
| 2566 | if let Err(e) = channel.send(VcpuControl::RunState(run_mode.clone())) { |
| 2567 | error!("failed to send VmRunMode: {}", e); |
| 2568 | } |
| 2569 | let _ = handle.kill(SIGRTMIN() + 0); |
| 2570 | } |
| 2571 | irq_chip.kick_halted_vcpus(); |
| 2572 | } |
| 2573 | |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2574 | // BalloonPolicy determines the size to set the balloon. |
| 2575 | struct BalloonPolicy { |
| 2576 | // Estimate for when the guest starts aggressivly freeing memory. |
| 2577 | critical_guest_available: i64, |
| 2578 | critical_host_available: i64, // ChromeOS critical margin. |
| 2579 | guest_available_bias: i64, |
| 2580 | max_balloon_actual: i64, // The largest the balloon has ever been observed. |
| 2581 | prev_balloon_full_percent: i64, // How full was the balloon at the previous timestep. |
| 2582 | prev_guest_available: i64, // Available memory in the guest at the previous timestep. |
| 2583 | } |
| 2584 | |
| 2585 | const ONE_KB: i64 = 1024; |
| 2586 | const ONE_MB: i64 = 1024 * ONE_KB; |
| 2587 | |
| 2588 | const LOWMEM_AVAILABLE: &str = "/sys/kernel/mm/chromeos-low_mem/available"; |
| 2589 | const LOWMEM_MARGIN: &str = "/sys/kernel/mm/chromeos-low_mem/margin"; |
| 2590 | |
| 2591 | // BalloonPolicy implements the virtio balloon sizing logic. |
| 2592 | // The balloon is sized with the following heuristics: |
| 2593 | // Balance Available |
| 2594 | // The balloon is sized to balance the amount of available memory above a |
| 2595 | // critical margin. The critical margin is the level at which memory is |
| 2596 | // freed. In the host, this is the ChromeOS available critical margin, which |
| 2597 | // is the trigger to kill tabs. In the guest, we estimate this level by |
| 2598 | // tracking the minimum amount of available memory, discounting sharp |
| 2599 | // 'valleys'. If the guest manages to keep available memory above a given |
| 2600 | // level even with some pressure, then we determine that this is the |
| 2601 | // 'critical' level for the guest. We don't update this critical value if |
| 2602 | // the balloon is fully inflated because in that case, the guest may be out |
| 2603 | // of memory to free. |
| 2604 | // guest_available_bias |
| 2605 | // Even if available memory is perfectly balanced between host and guest, |
| 2606 | // The size of the balloon will still drift randomly depending on whether |
| 2607 | // those host or guest reclaims memory first/faster every time memory is |
| 2608 | // low. To encourage large balloons to shrink and small balloons to grow, |
| 2609 | // the following bias is added to the guest critical margin: |
| 2610 | // (guest_available_bias * balloon_full_percent) / 100 |
| 2611 | // This give the guest more memory when the balloon is full. |
| 2612 | impl BalloonPolicy { |
| 2613 | fn new( |
| 2614 | memory_size: i64, |
| 2615 | critical_host_available: i64, |
| 2616 | guest_available_bias: i64, |
| 2617 | ) -> BalloonPolicy { |
| 2618 | // Estimate some reasonable initial maximum for balloon size. |
| 2619 | let max_balloon_actual = (memory_size * 3) / 4; |
| 2620 | // 400MB is above the zone min margin even for Crostini VMs on 16GB |
| 2621 | // devices (~85MB), and is above when Android Low Memory Killer kills |
| 2622 | // apps (~250MB). |
| 2623 | let critical_guest_available = 400 * ONE_MB; |
| 2624 | |
| 2625 | BalloonPolicy { |
| 2626 | critical_guest_available, |
| 2627 | critical_host_available, |
| 2628 | guest_available_bias, |
| 2629 | max_balloon_actual, |
| 2630 | prev_balloon_full_percent: 0, |
| 2631 | prev_guest_available: 0, |
| 2632 | } |
| 2633 | } |
| 2634 | fn delta(&mut self, stats: BalloonStats, balloon_actual_u: u64) -> Result<i64> { |
| 2635 | let guest_free = stats |
| 2636 | .free_memory |
| 2637 | .map(i64::try_from) |
| 2638 | .ok_or(Error::GuestFreeMissing())? |
| 2639 | .map_err(Error::GuestFreeTooLarge)?; |
| 2640 | let guest_cached = stats |
| 2641 | .disk_caches |
| 2642 | .map(i64::try_from) |
| 2643 | .ok_or(Error::GuestFreeMissing())? |
| 2644 | .map_err(Error::GuestFreeTooLarge)?; |
| 2645 | let balloon_actual = match balloon_actual_u { |
| 2646 | size if size < i64::max_value() as u64 => size as i64, |
| 2647 | _ => return Err(Error::BalloonActualTooLarge), |
| 2648 | }; |
| 2649 | let guest_available = guest_free + guest_cached; |
| 2650 | // Available memory is reported in MB, and we need bytes. |
| 2651 | let host_available = |
| 2652 | file_to_i64(LOWMEM_AVAILABLE, 0).map_err(Error::ReadMemAvailable)? * ONE_MB; |
| 2653 | if self.max_balloon_actual < balloon_actual { |
| 2654 | self.max_balloon_actual = balloon_actual; |
| 2655 | info!( |
| 2656 | "balloon updated max_balloon_actual to {} MiB", |
| 2657 | self.max_balloon_actual / ONE_MB, |
| 2658 | ); |
| 2659 | } |
| 2660 | let balloon_full_percent = balloon_actual * 100 / self.max_balloon_actual; |
| 2661 | // Update critical_guest_available if we see a lower available with the |
| 2662 | // balloon not fully inflated. If the balloon is completely inflated |
| 2663 | // there is a risk that the low available level we see comes at the cost |
| 2664 | // of stability. The Linux OOM Killer might have been forced to kill |
| 2665 | // something important, or page reclaim was so aggressive that there are |
| 2666 | // long UI hangs. |
| 2667 | if guest_available < self.critical_guest_available && balloon_full_percent < 95 { |
| 2668 | // To ignore temporary low memory states, we require that two guest |
| 2669 | // available measurements in a row are low. |
| 2670 | if self.prev_guest_available < self.critical_guest_available |
| 2671 | && self.prev_balloon_full_percent < 95 |
| 2672 | { |
| 2673 | self.critical_guest_available = self.prev_guest_available; |
| 2674 | info!( |
| 2675 | "balloon updated critical_guest_available to {} MiB", |
| 2676 | self.critical_guest_available / ONE_MB, |
| 2677 | ); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | // Compute the difference in available memory above the host and guest |
| 2682 | // critical thresholds. |
| 2683 | let bias = (self.guest_available_bias * balloon_full_percent) / 100; |
| 2684 | let guest_above_critical = guest_available - self.critical_guest_available - bias; |
| 2685 | let host_above_critical = host_available - self.critical_host_available; |
| 2686 | let balloon_delta = guest_above_critical - host_above_critical; |
| 2687 | // Only let the balloon take up MAX_CRITICAL_DELTA of available memory |
| 2688 | // below the critical level in host or guest. |
| 2689 | const MAX_CRITICAL_DELTA: i64 = 10 * ONE_MB; |
| 2690 | let balloon_delta_capped = if balloon_delta < 0 { |
| 2691 | // The balloon is deflating, taking memory from the host. Don't let |
| 2692 | // it take more than the amount of available memory above the |
| 2693 | // critical margin, plus MAX_CRITICAL_DELTA. |
| 2694 | max( |
| 2695 | balloon_delta, |
| 2696 | -(host_available - self.critical_host_available + MAX_CRITICAL_DELTA), |
| 2697 | ) |
| 2698 | } else { |
| 2699 | // The balloon is inflating, taking memory from the guest. Don't let |
| 2700 | // it take more than the amount of available memory above the |
| 2701 | // critical margin, plus MAX_CRITICAL_DELTA. |
| 2702 | min( |
| 2703 | balloon_delta, |
| 2704 | guest_available - self.critical_guest_available + MAX_CRITICAL_DELTA, |
| 2705 | ) |
| 2706 | }; |
| 2707 | |
| 2708 | self.prev_balloon_full_percent = balloon_full_percent; |
| 2709 | self.prev_guest_available = guest_available; |
| 2710 | |
| 2711 | // Only return a value if target would change available above critical |
| 2712 | // by more than 1%, or we are within 1 MB of critical in host or guest. |
| 2713 | if guest_above_critical < ONE_MB |
| 2714 | || host_above_critical < ONE_MB |
| 2715 | || (balloon_delta.abs() * 100) / guest_above_critical > 1 |
| 2716 | || (balloon_delta.abs() * 100) / host_above_critical > 1 |
| 2717 | { |
| 2718 | // Finally, make sure the balloon delta won't cause a negative size. |
| 2719 | let result = max(balloon_delta_capped, -balloon_actual); |
| 2720 | if result != 0 { |
| 2721 | info!( |
| 2722 | "balloon delta={:<6} ha={:<6} hc={:<6} ga={:<6} gc={:<6} bias={:<6} full={:>3}%", |
| 2723 | result / ONE_MB, |
| 2724 | host_available / ONE_MB, |
| 2725 | self.critical_host_available / ONE_MB, |
| 2726 | guest_available / ONE_MB, |
| 2727 | self.critical_guest_available / ONE_MB, |
| 2728 | bias / ONE_MB, |
| 2729 | balloon_full_percent, |
| 2730 | ); |
| 2731 | } |
| 2732 | return Ok(result); |
| 2733 | } |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 2734 | Ok(0) |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2738 | fn run_control<V: VmArch + 'static, Vcpu: VcpuArch + 'static, I: IrqChipArch + 'static>( |
| 2739 | mut linux: RunnableLinuxVm<V, Vcpu, I>, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2740 | control_server_socket: Option<UnlinkUnixSeqpacketListener>, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2741 | mut control_sockets: Vec<TaggedControlSocket>, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 2742 | balloon_host_socket: BalloonControlRequestSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2743 | disk_host_sockets: &[DiskControlRequestSocket], |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2744 | usb_control_socket: UsbControlSocket, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2745 | sigchld_fd: SignalFd, |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2746 | sandbox: bool, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2747 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2748 | balloon_bias: i64, |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 2749 | mut gralloc: RutabagaGralloc, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2750 | ) -> Result<()> { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2751 | #[derive(PollToken)] |
| 2752 | enum Token { |
| 2753 | Exit, |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2754 | Suspend, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2755 | ChildSignal, |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2756 | IrqFd { index: IrqEventIndex }, |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2757 | BalanceMemory, |
| 2758 | BalloonResult, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2759 | VmControlServer, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2760 | VmControl { index: usize }, |
| 2761 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2762 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 2763 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2764 | .set_raw_mode() |
| 2765 | .expect("failed to set terminal raw mode"); |
| 2766 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2767 | let wait_ctx = WaitContext::build_with(&[ |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2768 | (&linux.exit_evt, Token::Exit), |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2769 | (&linux.suspend_evt, Token::Suspend), |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2770 | (&sigchld_fd, Token::ChildSignal), |
| 2771 | ]) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2772 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2773 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2774 | if let Some(socket_server) = &control_server_socket { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2775 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2776 | .add(socket_server, Token::VmControlServer) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2777 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2778 | } |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2779 | for (index, socket) in control_sockets.iter().enumerate() { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2780 | wait_ctx |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2781 | .add(socket.as_ref(), Token::VmControl { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2782 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2785 | let events = linux |
| 2786 | .irq_chip |
| 2787 | .irq_event_tokens() |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2788 | .map_err(Error::WaitContextAdd)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2789 | |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2790 | for (index, _gsi, evt) in events { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2791 | wait_ctx |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2792 | .add(&evt, Token::IrqFd { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2793 | .map_err(Error::WaitContextAdd)?; |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2794 | } |
| 2795 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2796 | // Balance available memory between guest and host every second. |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2797 | let mut balancemem_timer = Timer::new().map_err(Error::CreateTimer)?; |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2798 | let mut balloon_policy = if let Ok(critical_margin) = file_to_i64(LOWMEM_MARGIN, 0) { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2799 | // Create timer request balloon stats every 1s. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2800 | wait_ctx |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2801 | .add(&balancemem_timer, Token::BalanceMemory) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2802 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2803 | let balancemem_dur = Duration::from_secs(1); |
| 2804 | let balancemem_int = Duration::from_secs(1); |
| 2805 | balancemem_timer |
| 2806 | .reset(balancemem_dur, Some(balancemem_int)) |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2807 | .map_err(Error::ResetTimer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2808 | |
| 2809 | // Listen for balloon statistics from the guest so we can balance. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2810 | wait_ctx |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2811 | .add(&balloon_host_socket, Token::BalloonResult) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2812 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2813 | Some(BalloonPolicy::new( |
| 2814 | linux.vm.get_memory().memory_size() as i64, |
| 2815 | critical_margin * ONE_MB, |
| 2816 | balloon_bias, |
| 2817 | )) |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2818 | } else { |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2819 | warn!("Unable to open low mem margin, maybe not a chrome os kernel"); |
| 2820 | None |
| 2821 | }; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2822 | |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2823 | if sandbox { |
| 2824 | // Before starting VCPUs, in case we started with some capabilities, drop them all. |
| 2825 | drop_capabilities().map_err(Error::DropCapabilities)?; |
| 2826 | } |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 2827 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2828 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2829 | // Create a channel for GDB thread. |
| 2830 | let (to_gdb_channel, from_vcpu_channel) = if linux.gdb.is_some() { |
| 2831 | let (s, r) = mpsc::channel(); |
| 2832 | (Some(s), Some(r)) |
| 2833 | } else { |
| 2834 | (None, None) |
| 2835 | }; |
| 2836 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2837 | let mut vcpu_handles = Vec::with_capacity(linux.vcpu_count); |
| 2838 | let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpu_count + 1)); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2839 | let use_hypervisor_signals = !linux |
| 2840 | .vm |
| 2841 | .get_hypervisor() |
| 2842 | .check_capability(&HypervisorCap::ImmediateExit); |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2843 | setup_vcpu_signal_handler::<Vcpu>(use_hypervisor_signals)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2844 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2845 | let vcpus: Vec<Option<_>> = match linux.vcpus.take() { |
Andrew Walbran | 9cfdbd9 | 2021-01-11 17:40:34 +0000 | [diff] [blame] | 2846 | Some(vec) => vec.into_iter().map(Some).collect(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2847 | None => iter::repeat_with(|| None).take(linux.vcpu_count).collect(), |
| 2848 | }; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 2849 | for (cpu_id, vcpu) in vcpus.into_iter().enumerate() { |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2850 | let (to_vcpu_channel, from_main_channel) = mpsc::channel(); |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2851 | let vcpu_affinity = match linux.vcpu_affinity.clone() { |
| 2852 | Some(VcpuAffinity::Global(v)) => v, |
| 2853 | Some(VcpuAffinity::PerVcpu(mut m)) => m.remove(&cpu_id).unwrap_or_default(), |
| 2854 | None => Default::default(), |
| 2855 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2856 | let handle = run_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2857 | cpu_id, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2858 | vcpu, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2859 | linux.vm.try_clone().map_err(Error::CloneEvent)?, |
| 2860 | linux.irq_chip.try_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2861 | linux.vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2862 | linux.rt_cpus.contains(&cpu_id), |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2863 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2864 | linux.no_smt, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2865 | vcpu_thread_barrier.clone(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2866 | linux.has_bios, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2867 | linux.io_bus.clone(), |
| 2868 | linux.mmio_bus.clone(), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2869 | linux.exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2870 | linux.vm.check_capability(VmCap::PvClockSuspend), |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2871 | from_main_channel, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2872 | use_hypervisor_signals, |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2873 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2874 | to_gdb_channel.clone(), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2875 | )?; |
Dylan Reid | b049266 | 2019-05-17 14:50:13 -0700 | [diff] [blame] | 2876 | vcpu_handles.push((handle, to_vcpu_channel)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2877 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2878 | |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 2879 | #[cfg(all(target_arch = "x86_64", feature = "gdb"))] |
| 2880 | // Spawn GDB thread. |
| 2881 | if let Some((gdb_port_num, gdb_control_socket)) = linux.gdb.take() { |
| 2882 | let to_vcpu_channels = vcpu_handles |
| 2883 | .iter() |
| 2884 | .map(|(_handle, channel)| channel.clone()) |
| 2885 | .collect(); |
| 2886 | let target = GdbStub::new( |
| 2887 | gdb_control_socket, |
| 2888 | to_vcpu_channels, |
| 2889 | from_vcpu_channel.unwrap(), // Must succeed to unwrap() |
| 2890 | ); |
| 2891 | thread::Builder::new() |
| 2892 | .name("gdb".to_owned()) |
| 2893 | .spawn(move || gdb_thread(target, gdb_port_num)) |
| 2894 | .map_err(Error::SpawnGdbServer)?; |
| 2895 | }; |
| 2896 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2897 | vcpu_thread_barrier.wait(); |
| 2898 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2899 | 'wait: loop { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2900 | let events = { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2901 | match wait_ctx.wait() { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2902 | Ok(v) => v, |
| 2903 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 2904 | error!("failed to poll: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | } |
| 2908 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2909 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2910 | if let Err(e) = linux.irq_chip.process_delayed_irq_events() { |
| 2911 | warn!("can't deliver delayed irqs: {}", e); |
| 2912 | } |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2913 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2914 | let mut vm_control_indices_to_remove = Vec::new(); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2915 | for event in events.iter().filter(|e| e.is_readable) { |
| 2916 | match event.token { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2917 | Token::Exit => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2918 | info!("vcpu requested shutdown"); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2919 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2920 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2921 | Token::Suspend => { |
| 2922 | info!("VM requested suspend"); |
| 2923 | linux.suspend_evt.read().unwrap(); |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 2924 | kick_all_vcpus(&vcpu_handles, &linux.irq_chip, &VmRunMode::Suspending); |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2925 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2926 | Token::ChildSignal => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2927 | // Print all available siginfo structs, then exit the loop. |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2928 | while let Some(siginfo) = sigchld_fd.read().map_err(Error::SignalFd)? { |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 2929 | let pid = siginfo.ssi_pid; |
| 2930 | let pid_label = match linux.pid_debug_label_map.get(&pid) { |
| 2931 | Some(label) => format!("{} (pid {})", label, pid), |
| 2932 | None => format!("pid {}", pid), |
| 2933 | }; |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2934 | error!( |
| 2935 | "child {} died: signo {}, status {}, code {}", |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 2936 | pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2937 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2938 | } |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2939 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2940 | } |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 2941 | Token::IrqFd { index } => { |
| 2942 | if let Err(e) = linux.irq_chip.service_irq_event(index) { |
| 2943 | error!("failed to signal irq {}: {}", index, e); |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2944 | } |
| 2945 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2946 | Token::BalanceMemory => { |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2947 | balancemem_timer.wait().map_err(Error::Timer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2948 | let command = BalloonControlCommand::Stats {}; |
| 2949 | if let Err(e) = balloon_host_socket.send(&command) { |
| 2950 | warn!("failed to send stats request to balloon device: {}", e); |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2951 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2952 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2953 | Token::BalloonResult => { |
| 2954 | match balloon_host_socket.recv() { |
| 2955 | Ok(BalloonControlResult::Stats { |
| 2956 | stats, |
| 2957 | balloon_actual: balloon_actual_u, |
| 2958 | }) => { |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2959 | match balloon_policy |
| 2960 | .as_mut() |
| 2961 | .map(|p| p.delta(stats, balloon_actual_u)) |
| 2962 | { |
| 2963 | None => { |
| 2964 | error!( |
| 2965 | "got result from balloon stats, but no policy is running" |
| 2966 | ); |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2967 | } |
Charles William Dick | 0e3d4b6 | 2020-12-14 12:16:46 +0900 | [diff] [blame] | 2968 | Some(Err(e)) => { |
| 2969 | warn!("failed to run balloon policy {}", e); |
| 2970 | } |
| 2971 | Some(Ok(delta)) if delta != 0 => { |
| 2972 | let target = max((balloon_actual_u as i64) + delta, 0) as u64; |
| 2973 | let command = |
| 2974 | BalloonControlCommand::Adjust { num_bytes: target }; |
| 2975 | if let Err(e) = balloon_host_socket.send(&command) { |
| 2976 | warn!( |
| 2977 | "failed to send memory value to balloon device: {}", |
| 2978 | e |
| 2979 | ); |
| 2980 | } |
| 2981 | } |
| 2982 | Some(Ok(_)) => {} |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | Err(e) => { |
| 2986 | error!("failed to recv BalloonControlResult: {}", e); |
| 2987 | } |
| 2988 | }; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2989 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2990 | Token::VmControlServer => { |
| 2991 | if let Some(socket_server) = &control_server_socket { |
| 2992 | match socket_server.accept() { |
| 2993 | Ok(socket) => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2994 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2995 | .add( |
| 2996 | &socket, |
| 2997 | Token::VmControl { |
| 2998 | index: control_sockets.len(), |
| 2999 | }, |
| 3000 | ) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3001 | .map_err(Error::WaitContextAdd)?; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3002 | control_sockets |
| 3003 | .push(TaggedControlSocket::Vm(MsgSocket::new(socket))); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3004 | } |
| 3005 | Err(e) => error!("failed to accept socket: {}", e), |
| 3006 | } |
| 3007 | } |
| 3008 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3009 | Token::VmControl { index } => { |
Daniel Verkamp | 37c4a78 | 2019-01-04 10:44:17 -0800 | [diff] [blame] | 3010 | if let Some(socket) = control_sockets.get(index) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3011 | match socket { |
| 3012 | TaggedControlSocket::Vm(socket) => match socket.recv() { |
| 3013 | Ok(request) => { |
| 3014 | let mut run_mode_opt = None; |
| 3015 | let response = request.execute( |
| 3016 | &mut run_mode_opt, |
| 3017 | &balloon_host_socket, |
| 3018 | disk_host_sockets, |
| 3019 | &usb_control_socket, |
Chuanxiao Dong | 256be3a | 2020-04-27 16:39:33 +0800 | [diff] [blame] | 3020 | &mut linux.bat_control, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3021 | ); |
| 3022 | if let Err(e) = socket.send(&response) { |
| 3023 | error!("failed to send VmResponse: {}", e); |
| 3024 | } |
| 3025 | if let Some(run_mode) = run_mode_opt { |
| 3026 | info!("control socket changed run mode to {}", run_mode); |
| 3027 | match run_mode { |
| 3028 | VmRunMode::Exiting => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3029 | break 'wait; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3030 | } |
Keiichi Watanabe | c5262e9 | 2020-10-21 15:57:33 +0900 | [diff] [blame] | 3031 | other => { |
Chuanxiao Dong | 2bbe85c | 2020-11-12 17:18:07 +0800 | [diff] [blame] | 3032 | if other == VmRunMode::Running { |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3033 | linux.io_bus.notify_resume(); |
| 3034 | } |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 3035 | kick_all_vcpus( |
| 3036 | &vcpu_handles, |
| 3037 | &linux.irq_chip, |
| 3038 | &other, |
| 3039 | ); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3043 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3044 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 3045 | if let MsgError::RecvZero = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3046 | vm_control_indices_to_remove.push(index); |
| 3047 | } else { |
| 3048 | error!("failed to recv VmRequest: {}", e); |
| 3049 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3050 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3051 | }, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 3052 | TaggedControlSocket::VmMemory(socket) => match socket.recv() { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3053 | Ok(request) => { |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 3054 | let response = request.execute( |
| 3055 | &mut linux.vm, |
| 3056 | &mut linux.resources, |
| 3057 | Arc::clone(&map_request), |
Gurchetan Singh | 293913c | 2020-12-09 10:44:13 -0800 | [diff] [blame] | 3058 | &mut gralloc, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 3059 | ); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3060 | if let Err(e) = socket.send(&response) { |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 3061 | error!("failed to send VmMemoryControlResponse: {}", e); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3062 | } |
| 3063 | } |
| 3064 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 3065 | if let MsgError::RecvZero = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3066 | vm_control_indices_to_remove.push(index); |
| 3067 | } else { |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 3068 | error!("failed to recv VmMemoryControlRequest: {}", e); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3069 | } |
| 3070 | } |
| 3071 | }, |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3072 | TaggedControlSocket::VmIrq(socket) => match socket.recv() { |
| 3073 | Ok(request) => { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3074 | let response = { |
| 3075 | let irq_chip = &mut linux.irq_chip; |
| 3076 | request.execute( |
| 3077 | |setup| match setup { |
| 3078 | IrqSetup::Event(irq, ev) => { |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 3079 | if let Some(event_index) = irq_chip |
| 3080 | .register_irq_event(irq, ev, None)? |
| 3081 | { |
| 3082 | match wait_ctx.add( |
| 3083 | ev, |
| 3084 | Token::IrqFd { |
| 3085 | index: event_index |
| 3086 | }, |
| 3087 | ) { |
| 3088 | Err(e) => { |
| 3089 | warn!("failed to add IrqFd to poll context: {}", e); |
| 3090 | Err(e) |
| 3091 | }, |
| 3092 | Ok(_) => { |
| 3093 | Ok(()) |
| 3094 | } |
| 3095 | } |
| 3096 | } else { |
| 3097 | Ok(()) |
| 3098 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 3099 | } |
| 3100 | IrqSetup::Route(route) => irq_chip.route_irq(route), |
| 3101 | }, |
| 3102 | &mut linux.resources, |
| 3103 | ) |
| 3104 | }; |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3105 | if let Err(e) = socket.send(&response) { |
| 3106 | error!("failed to send VmIrqResponse: {}", e); |
| 3107 | } |
| 3108 | } |
| 3109 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 3110 | if let MsgError::RecvZero = e { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 3111 | vm_control_indices_to_remove.push(index); |
| 3112 | } else { |
| 3113 | error!("failed to recv VmIrqRequest: {}", e); |
| 3114 | } |
| 3115 | } |
| 3116 | }, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 3117 | TaggedControlSocket::VmMsync(socket) => match socket.recv() { |
| 3118 | Ok(request) => { |
| 3119 | let response = request.execute(&mut linux.vm); |
| 3120 | if let Err(e) = socket.send(&response) { |
| 3121 | error!("failed to send VmMsyncResponse: {}", e); |
| 3122 | } |
| 3123 | } |
| 3124 | Err(e) => { |
| 3125 | if let MsgError::BadRecvSize { actual: 0, .. } = e { |
| 3126 | vm_control_indices_to_remove.push(index); |
| 3127 | } else { |
| 3128 | error!("failed to recv VmMsyncRequest: {}", e); |
| 3129 | } |
| 3130 | } |
| 3131 | }, |
Keiichi Watanabe | eefe7fb | 2020-11-17 17:58:35 +0900 | [diff] [blame] | 3132 | TaggedControlSocket::Fs(socket) => match socket.recv() { |
| 3133 | Ok(request) => { |
| 3134 | let response = |
| 3135 | request.execute(&mut linux.vm, &mut linux.resources); |
| 3136 | if let Err(e) = socket.send(&response) { |
| 3137 | error!("failed to send VmResponse: {}", e); |
| 3138 | } |
| 3139 | } |
| 3140 | Err(e) => { |
| 3141 | if let MsgError::BadRecvSize { actual: 0, .. } = e { |
| 3142 | vm_control_indices_to_remove.push(index); |
| 3143 | } else { |
| 3144 | error!("failed to recv VmResponse: {}", e); |
| 3145 | } |
| 3146 | } |
| 3147 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3148 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3149 | } |
| 3150 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3151 | } |
| 3152 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3153 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3154 | for event in events.iter().filter(|e| e.is_hungup) { |
| 3155 | match event.token { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3156 | Token::Exit => {} |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 3157 | Token::Suspend => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3158 | Token::ChildSignal => {} |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame] | 3159 | Token::IrqFd { index: _ } => {} |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 3160 | Token::BalanceMemory => {} |
| 3161 | Token::BalloonResult => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3162 | Token::VmControlServer => {} |
| 3163 | Token::VmControl { index } => { |
| 3164 | // It's possible more data is readable and buffered while the socket is hungup, |
| 3165 | // so don't delete the socket from the poll context until we're sure all the |
| 3166 | // data is read. |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 3167 | match control_sockets |
| 3168 | .get(index) |
| 3169 | .map(|s| s.as_ref().get_readable_bytes()) |
| 3170 | { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3171 | Some(Ok(0)) | Some(Err(_)) => vm_control_indices_to_remove.push(index), |
| 3172 | Some(Ok(x)) => info!("control index {} has {} bytes readable", index, x), |
| 3173 | _ => {} |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 3174 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 3175 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3176 | } |
| 3177 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3178 | |
| 3179 | // 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] | 3180 | // preserves correct indexes as each element is removed. |
Daniel Verkamp | 8c2f000 | 2020-08-31 15:13:35 -0700 | [diff] [blame] | 3181 | vm_control_indices_to_remove.sort_unstable_by_key(|&k| Reverse(k)); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3182 | vm_control_indices_to_remove.dedup(); |
| 3183 | for index in vm_control_indices_to_remove { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3184 | // Delete the socket from the `wait_ctx` synchronously. Otherwise, the kernel will do |
| 3185 | // 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] | 3186 | // 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] | 3187 | // 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] | 3188 | // that has already been closed. Because the token associated with that spurious event |
| 3189 | // now belongs to a different socket, the control loop will start to interact with |
| 3190 | // sockets that might not be ready to use. This can cause incorrect hangup detection or |
| 3191 | // blocking on a socket that will never be ready. See also: crbug.com/1019986 |
| 3192 | if let Some(socket) = control_sockets.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3193 | wait_ctx.delete(socket).map_err(Error::WaitContextDelete)?; |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | // This line implicitly drops the socket at `index` when it gets returned by |
| 3197 | // `swap_remove`. After this line, the socket at `index` is not the one from |
| 3198 | // `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] | 3199 | // use `wait_ctx.modify` to change the associated index in its `Token::VmControl`. |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3200 | control_sockets.swap_remove(index); |
| 3201 | if let Some(socket) = control_sockets.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 3202 | wait_ctx |
| 3203 | .modify(socket, EventType::Read, Token::VmControl { index }) |
| 3204 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 3205 | } |
| 3206 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3207 | } |
| 3208 | |
Steven Richman | 11dc671 | 2020-09-02 15:39:14 -0700 | [diff] [blame] | 3209 | kick_all_vcpus(&vcpu_handles, &linux.irq_chip, &VmRunMode::Exiting); |
| 3210 | for (handle, _) in vcpu_handles { |
| 3211 | if let Err(e) = handle.join() { |
| 3212 | error!("failed to join vcpu thread: {:?}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 3216 | // Explicitly drop the VM structure here to allow the devices to clean up before the |
| 3217 | // control sockets are closed when this function exits. |
| 3218 | mem::drop(linux); |
| 3219 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 3220 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 3221 | .set_canon_mode() |
| 3222 | .expect("failed to restore canonical mode for terminal"); |
| 3223 | |
| 3224 | Ok(()) |
| 3225 | } |