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 | |
Daniel Verkamp | 8c2f000 | 2020-08-31 15:13:35 -0700 | [diff] [blame] | 5 | use std::cmp::{max, 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; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 20 | use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; |
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 | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 25 | use std::sync::{Arc, Barrier}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 26 | use std::thread; |
| 27 | use std::thread::JoinHandle; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 28 | use std::time::Duration; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 29 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 30 | use libc::{self, c_int, gid_t, uid_t}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 31 | |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 32 | use acpi_tables::sdt::SDT; |
| 33 | |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 34 | use base::net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener}; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 35 | #[cfg(feature = "gpu")] |
| 36 | use devices::virtio::EventDevice; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 37 | use devices::virtio::{self, Console, VirtioDevice}; |
paulhsia | ce17e6e | 2020-08-28 18:37:45 +0800 | [diff] [blame] | 38 | #[cfg(feature = "audio")] |
| 39 | use devices::Ac97Dev; |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 40 | use devices::{ |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 41 | self, HostBackendDeviceProvider, IrqEventIndex, KvmKernelIrqChip, PciDevice, VfioContainer, |
| 42 | VfioDevice, VfioPciDevice, VirtioPciDevice, XhciController, |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 43 | }; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 44 | use hypervisor::kvm::{Kvm, KvmVcpu, KvmVm}; |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 45 | use hypervisor::{HypervisorCap, Vcpu, VcpuExit, VcpuRunHandle, Vm, VmCap}; |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 46 | use minijail::{self, Minijail}; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 47 | use msg_socket::{MsgError, MsgReceiver, MsgSender, MsgSocket}; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 48 | use net_util::{Error as NetError, MacAddress, Tap}; |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 49 | use remain::sorted; |
Xiong Zhang | 87a3b44 | 2019-10-29 17:32:44 +0800 | [diff] [blame] | 50 | use resources::{Alloc, MmioType, SystemAllocator}; |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 51 | use sync::{Condvar, Mutex}; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 52 | |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 53 | use base::{ |
David Tolnay | 633426a | 2019-04-12 12:18:35 -0700 | [diff] [blame] | 54 | self, block_signal, clear_signal, drop_capabilities, error, flock, get_blocked_signals, |
Fletcher Woodruff | 82ff397 | 2019-10-02 13:11:34 -0600 | [diff] [blame] | 55 | get_group_id, get_user_id, getegid, geteuid, info, register_rt_signal_handler, |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 56 | set_cpu_affinity, set_rt_prio_limit, set_rt_round_robin, signal, validate_raw_fd, warn, |
| 57 | AsRawDescriptor, Event, EventType, ExternalMapping, FlockOperation, Killable, |
| 58 | MemoryMappingArena, PollToken, Protection, RawDescriptor, ScopedEvent, SignalFd, Terminal, |
| 59 | Timer, WaitContext, SIGRTMIN, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 60 | }; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 61 | use vm_control::{ |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 62 | BalloonControlCommand, BalloonControlRequestSocket, BalloonControlResponseSocket, |
Charles William Dick | 664cc3c | 2020-01-10 14:31:52 +0900 | [diff] [blame] | 63 | BalloonControlResult, DiskControlCommand, DiskControlRequestSocket, DiskControlResponseSocket, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 64 | DiskControlResult, IrqSetup, UsbControlSocket, VmControlResponseSocket, VmIrqRequest, |
| 65 | VmIrqRequestSocket, VmIrqResponse, VmIrqResponseSocket, VmMemoryControlRequestSocket, |
| 66 | VmMemoryControlResponseSocket, VmMemoryRequest, VmMemoryResponse, VmMsyncRequest, |
| 67 | VmMsyncRequestSocket, VmMsyncResponse, VmMsyncResponseSocket, VmRunMode, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 68 | }; |
Dylan Reid | ec058d6 | 2020-07-20 20:21:11 -0700 | [diff] [blame] | 69 | use vm_memory::{GuestAddress, GuestMemory}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 70 | |
Daniel Verkamp | 50740ce | 2020-02-28 12:36:56 -0800 | [diff] [blame] | 71 | use crate::{Config, DiskOption, Executable, SharedDir, SharedDirKind, TouchDeviceOption}; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 72 | use arch::{ |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 73 | self, LinuxArch, RunnableLinuxVm, SerialHardware, SerialParameters, VcpuAffinity, |
| 74 | VirtioDeviceStub, VmComponents, VmImage, |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 75 | }; |
Sonny Rao | ed517d1 | 2018-02-13 22:09:43 -0800 | [diff] [blame] | 76 | |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 77 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 78 | use { |
| 79 | aarch64::AArch64 as Arch, |
| 80 | devices::{IrqChip, IrqChipAArch64 as IrqChipArch}, |
| 81 | hypervisor::{VcpuAArch64 as VcpuArch, VmAArch64 as VmArch}, |
| 82 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 83 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 84 | use { |
| 85 | devices::{IrqChipX86_64, IrqChipX86_64 as IrqChipArch, KvmSplitIrqChip}, |
| 86 | hypervisor::{VcpuX86_64, VcpuX86_64 as VcpuArch, VmX86_64 as VmArch}, |
| 87 | x86_64::X8664arch as Arch, |
| 88 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 89 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 90 | #[sorted] |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 91 | #[derive(Debug)] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 92 | pub enum Error { |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 93 | AddGpuDeviceMemory(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 94 | AddIrqChipVcpu(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 95 | AddPmemDeviceMemory(base::Error), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 96 | AllocateGpuDeviceAddress, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 97 | AllocatePmemDeviceAddress(resources::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 98 | BalloonDeviceNew(virtio::BalloonError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 99 | BlockDeviceNew(base::Error), |
| 100 | BlockSignal(base::signal::Error), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 101 | BuildVm(<Arch as LinuxArch>::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 102 | ChownTpmStorage(base::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 103 | CloneEvent(base::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 104 | CloneVcpu(base::Error), |
| 105 | ConfigureVcpu(<Arch as LinuxArch>::Error), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 106 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 107 | CreateAc97(devices::PciDeviceError), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 108 | CreateConsole(arch::serial::Error), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 109 | CreateDiskError(disk::Error), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 110 | CreateEvent(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 111 | CreateSignalFd(base::SignalFdError), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 112 | CreateSocket(io::Error), |
Chirantan Ekbote | 49fa08f | 2018-11-16 13:26:53 -0800 | [diff] [blame] | 113 | CreateTapDevice(NetError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 114 | CreateTimer(base::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 115 | CreateTpmStorage(PathBuf, io::Error), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 116 | CreateUsbProvider(devices::usb::host_backend::error::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 117 | CreateVcpu(base::Error), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 118 | CreateVfioDevice(devices::vfio::VfioError), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 119 | CreateWaitContext(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 120 | DeviceJail(minijail::Error), |
| 121 | DevicePivotRoot(minijail::Error), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 122 | Disk(PathBuf, io::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 123 | DiskImageLock(base::Error), |
| 124 | DropCapabilities(base::Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 125 | FsDeviceNew(virtio::fs::Error), |
| 126 | GetMaxOpenFiles(io::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 127 | GetSignalMask(signal::Error), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 128 | InputDeviceNew(virtio::InputError), |
| 129 | InputEventsOpen(std::io::Error), |
Dylan Reid | 2056644 | 2018-04-02 15:06:15 -0700 | [diff] [blame] | 130 | InvalidFdPath, |
Zach Reizner | 579bd2c | 2018-09-14 15:43:33 -0700 | [diff] [blame] | 131 | InvalidWaylandPath, |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 132 | IoJail(minijail::Error), |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 133 | LoadKernel(Box<dyn StdError>), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 134 | MemoryTooLarge, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 135 | NetDeviceNew(virtio::NetError), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 136 | OpenAcpiTable(PathBuf, io::Error), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 137 | OpenAndroidFstab(PathBuf, io::Error), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 138 | OpenBios(PathBuf, io::Error), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 139 | OpenInitrd(PathBuf, io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 140 | OpenKernel(PathBuf, io::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 141 | OpenVinput(PathBuf, io::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 142 | P9DeviceNew(virtio::P9Error), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 143 | ParseMaxOpenFiles(ParseIntError), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 144 | PivotRootDoesntExist(&'static str), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 145 | PmemDeviceImageTooBig, |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 146 | PmemDeviceNew(base::Error), |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 147 | ReadMemAvailable(io::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 148 | RegisterBalloon(arch::DeviceRegistrationError), |
| 149 | RegisterBlock(arch::DeviceRegistrationError), |
| 150 | RegisterGpu(arch::DeviceRegistrationError), |
| 151 | RegisterNet(arch::DeviceRegistrationError), |
| 152 | RegisterP9(arch::DeviceRegistrationError), |
| 153 | RegisterRng(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 154 | RegisterSignalHandler(base::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 155 | RegisterWayland(arch::DeviceRegistrationError), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 156 | ReserveGpuMemory(base::MmapError), |
| 157 | ReserveMemory(base::Error), |
| 158 | ReservePmemMemory(base::MmapError), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 159 | ResetTimer(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 160 | RngDeviceNew(virtio::RngError), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 161 | RunnableVcpu(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 162 | SettingGidMap(minijail::Error), |
| 163 | SettingMaxOpenFiles(minijail::Error), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 164 | SettingSignalMask(base::Error), |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 165 | SettingUidMap(minijail::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 166 | SignalFd(base::SignalFdError), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 167 | SpawnVcpu(io::Error), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 168 | Timer(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 169 | ValidateRawFd(base::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 170 | VhostNetDeviceNew(virtio::vhost::Error), |
| 171 | VhostVsockDeviceNew(virtio::vhost::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 172 | VirtioPciDev(base::Error), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 173 | WaitContextAdd(base::Error), |
| 174 | WaitContextDelete(base::Error), |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 175 | WaylandDeviceNew(base::Error), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 176 | } |
| 177 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 178 | impl Display for Error { |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 179 | #[remain::check] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 180 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 181 | use self::Error::*; |
| 182 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 183 | #[sorted] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 184 | match self { |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 185 | AddGpuDeviceMemory(e) => write!(f, "failed to add gpu device memory: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 186 | AddIrqChipVcpu(e) => write!(f, "failed to add vcpu to irq chip: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 187 | AddPmemDeviceMemory(e) => write!(f, "failed to add pmem device memory: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 188 | AllocateGpuDeviceAddress => write!(f, "failed to allocate gpu device guest address"), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 189 | AllocatePmemDeviceAddress(e) => { |
| 190 | write!(f, "failed to allocate memory for pmem device: {}", e) |
| 191 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 192 | BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), |
| 193 | BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), |
| 194 | BlockSignal(e) => write!(f, "failed to block signal: {}", e), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 195 | BuildVm(e) => write!(f, "The architecture failed to build the vm: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 196 | ChownTpmStorage(e) => write!(f, "failed to chown tpm storage: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 197 | CloneEvent(e) => write!(f, "failed to clone event: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 198 | CloneVcpu(e) => write!(f, "failed to clone vcpu: {}", e), |
| 199 | ConfigureVcpu(e) => write!(f, "failed to configure vcpu: {}", e), |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 200 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 201 | CreateAc97(e) => write!(f, "failed to create ac97 device: {}", e), |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 202 | CreateConsole(e) => write!(f, "failed to create console device: {}", e), |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 203 | CreateDiskError(e) => write!(f, "failed to create virtual disk: {}", e), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 204 | CreateEvent(e) => write!(f, "failed to create event: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 205 | CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), |
| 206 | CreateSocket(e) => write!(f, "failed to create socket: {}", e), |
| 207 | CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 208 | CreateTimer(e) => write!(f, "failed to create Timer: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 209 | CreateTpmStorage(p, e) => { |
| 210 | write!(f, "failed to create tpm storage dir {}: {}", p.display(), e) |
| 211 | } |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 212 | CreateUsbProvider(e) => write!(f, "failed to create usb provider: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 213 | CreateVcpu(e) => write!(f, "failed to create vcpu: {}", e), |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 214 | CreateVfioDevice(e) => write!(f, "Failed to create vfio device {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 215 | CreateWaitContext(e) => write!(f, "failed to create wait context: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 216 | DeviceJail(e) => write!(f, "failed to jail device: {}", e), |
| 217 | DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 218 | 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] | 219 | DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 220 | DropCapabilities(e) => write!(f, "failed to drop process capabilities: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 221 | FsDeviceNew(e) => write!(f, "failed to create fs device: {}", e), |
| 222 | 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] | 223 | GetSignalMask(e) => write!(f, "failed to retrieve signal mask for vcpu: {}", e), |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 224 | InputDeviceNew(e) => write!(f, "failed to set up input device: {}", e), |
| 225 | InputEventsOpen(e) => write!(f, "failed to open event device: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 226 | InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), |
| 227 | InvalidWaylandPath => write!(f, "wayland socket path has no parent or file name"), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 228 | IoJail(e) => write!(f, "{}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 229 | LoadKernel(e) => write!(f, "failed to load kernel: {}", e), |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 230 | MemoryTooLarge => write!(f, "requested memory size too large"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 231 | NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 232 | 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] | 233 | OpenAndroidFstab(p, e) => write!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 234 | f, |
| 235 | "failed to open android fstab file {}: {}", |
| 236 | p.display(), |
| 237 | e |
| 238 | ), |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 239 | OpenBios(p, e) => write!(f, "failed to open bios {}: {}", p.display(), e), |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 240 | OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), |
| 241 | 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] | 242 | 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] | 243 | P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 244 | 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] | 245 | PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 246 | PmemDeviceImageTooBig => { |
| 247 | write!(f, "failed to create pmem device: pmem device image too big") |
| 248 | } |
| 249 | PmemDeviceNew(e) => write!(f, "failed to create pmem device: {}", e), |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 250 | ReadMemAvailable(e) => write!(f, "failed to read /proc/meminfo: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 251 | RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), |
| 252 | RegisterBlock(e) => write!(f, "error registering block device: {}", e), |
| 253 | RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), |
| 254 | RegisterNet(e) => write!(f, "error registering net device: {}", e), |
| 255 | RegisterP9(e) => write!(f, "error registering 9p device: {}", e), |
| 256 | RegisterRng(e) => write!(f, "error registering rng device: {}", e), |
| 257 | RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), |
| 258 | RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 259 | ReserveGpuMemory(e) => write!(f, "failed to reserve gpu memory: {}", e), |
| 260 | ReserveMemory(e) => write!(f, "failed to reserve memory: {}", e), |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 261 | ReservePmemMemory(e) => write!(f, "failed to reserve pmem memory: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 262 | ResetTimer(e) => write!(f, "failed to reset Timer: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 263 | RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 264 | RunnableVcpu(e) => write!(f, "failed to set thread id for vcpu: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 265 | SettingGidMap(e) => write!(f, "error setting GID map: {}", e), |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 266 | SettingMaxOpenFiles(e) => write!(f, "error setting max open files: {}", e), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 267 | 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] | 268 | SettingUidMap(e) => write!(f, "error setting UID map: {}", e), |
| 269 | SignalFd(e) => write!(f, "failed to read signal fd: {}", e), |
| 270 | SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 271 | Timer(e) => write!(f, "failed to read timer fd: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 272 | ValidateRawFd(e) => write!(f, "failed to validate raw fd: {}", e), |
| 273 | VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), |
| 274 | VhostVsockDeviceNew(e) => write!(f, "failed to set up virtual socket device: {}", e), |
| 275 | VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 276 | WaitContextAdd(e) => write!(f, "failed to add descriptor to wait context: {}", e), |
| 277 | WaitContextDelete(e) => { |
| 278 | write!(f, "failed to remove descriptor from wait context: {}", e) |
| 279 | } |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 280 | WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Allen Webb | f3024c8 | 2020-06-19 07:19:48 -0700 | [diff] [blame] | 285 | impl From<minijail::Error> for Error { |
| 286 | fn from(err: minijail::Error) -> Self { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 287 | Error::IoJail(err) |
| 288 | } |
| 289 | } |
| 290 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 291 | impl std::error::Error for Error {} |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 292 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 293 | type Result<T> = std::result::Result<T, Error>; |
| 294 | |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 295 | enum TaggedControlSocket { |
| 296 | Vm(VmControlResponseSocket), |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 297 | VmMemory(VmMemoryControlResponseSocket), |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 298 | VmIrq(VmIrqResponseSocket), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 299 | VmMsync(VmMsyncResponseSocket), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | impl AsRef<UnixSeqpacket> for TaggedControlSocket { |
| 303 | fn as_ref(&self) -> &UnixSeqpacket { |
| 304 | use self::TaggedControlSocket::*; |
| 305 | match &self { |
Chirantan Ekbote | 5058253 | 2020-01-16 16:49:14 +0900 | [diff] [blame] | 306 | Vm(ref socket) => socket.as_ref(), |
| 307 | VmMemory(ref socket) => socket.as_ref(), |
| 308 | VmIrq(ref socket) => socket.as_ref(), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 309 | VmMsync(ref socket) => socket.as_ref(), |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 314 | impl AsRawDescriptor for TaggedControlSocket { |
| 315 | fn as_raw_descriptor(&self) -> RawDescriptor { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 316 | self.as_ref().as_raw_fd() |
| 317 | } |
| 318 | } |
| 319 | |
Andrew Walbran | f50bab6 | 2020-07-07 13:22:53 +0100 | [diff] [blame] | 320 | fn get_max_open_files() -> Result<u64> { |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 321 | let mut buf = mem::MaybeUninit::<libc::rlimit64>::zeroed(); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 322 | |
Chirantan Ekbote | aa77ea4 | 2019-12-09 14:58:54 +0900 | [diff] [blame] | 323 | // Safe because this will only modify `buf` and we check the return value. |
| 324 | let res = unsafe { libc::prlimit64(0, libc::RLIMIT_NOFILE, ptr::null(), buf.as_mut_ptr()) }; |
| 325 | if res == 0 { |
| 326 | // Safe because the kernel guarantees that the struct is fully initialized. |
| 327 | let limit = unsafe { buf.assume_init() }; |
| 328 | Ok(limit.rlim_max) |
| 329 | } else { |
| 330 | Err(Error::GetMaxOpenFiles(io::Error::last_os_error())) |
| 331 | } |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 334 | struct SandboxConfig<'a> { |
| 335 | limit_caps: bool, |
| 336 | log_failures: bool, |
| 337 | seccomp_policy: &'a Path, |
| 338 | uid_map: Option<&'a str>, |
| 339 | gid_map: Option<&'a str>, |
| 340 | } |
| 341 | |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 342 | fn create_base_minijail( |
| 343 | root: &Path, |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 344 | r_limit: Option<u64>, |
| 345 | config: Option<&SandboxConfig>, |
Zach Reizner | 4486379 | 2019-06-26 14:22:08 -0700 | [diff] [blame] | 346 | ) -> Result<Minijail> { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 347 | // All child jails run in a new user namespace without any users mapped, |
| 348 | // they run as nobody unless otherwise configured. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 349 | let mut j = Minijail::new().map_err(Error::DeviceJail)?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 350 | |
| 351 | if let Some(config) = config { |
| 352 | j.namespace_pids(); |
| 353 | j.namespace_user(); |
| 354 | j.namespace_user_disable_setgroups(); |
| 355 | if config.limit_caps { |
| 356 | // Don't need any capabilities. |
| 357 | j.use_caps(0); |
| 358 | } |
| 359 | if let Some(uid_map) = config.uid_map { |
| 360 | j.uidmap(uid_map).map_err(Error::SettingUidMap)?; |
| 361 | } |
| 362 | if let Some(gid_map) = config.gid_map { |
| 363 | j.gidmap(gid_map).map_err(Error::SettingGidMap)?; |
| 364 | } |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 365 | // Run in a new mount namespace. |
| 366 | j.namespace_vfs(); |
| 367 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 368 | // Run in an empty network namespace. |
| 369 | j.namespace_net(); |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 370 | |
| 371 | // Don't allow the device to gain new privileges. |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 372 | j.no_new_privs(); |
| 373 | |
| 374 | // By default we'll prioritize using the pre-compiled .bpf over the .policy |
| 375 | // file (the .bpf is expected to be compiled using "trap" as the failure |
| 376 | // behavior instead of the default "kill" behavior). |
| 377 | // Refer to the code comment for the "seccomp-log-failures" |
| 378 | // command-line parameter for an explanation about why the |log_failures| |
| 379 | // flag forces the use of .policy files (and the build-time alternative to |
| 380 | // this run-time flag). |
| 381 | let bpf_policy_file = config.seccomp_policy.with_extension("bpf"); |
| 382 | if bpf_policy_file.exists() && !config.log_failures { |
| 383 | j.parse_seccomp_program(&bpf_policy_file) |
| 384 | .map_err(Error::DeviceJail)?; |
| 385 | } else { |
| 386 | // Use TSYNC only for the side effect of it using SECCOMP_RET_TRAP, |
| 387 | // which will correctly kill the entire device process if a worker |
| 388 | // thread commits a seccomp violation. |
| 389 | j.set_seccomp_filter_tsync(); |
| 390 | if config.log_failures { |
| 391 | j.log_seccomp_filter_failures(); |
| 392 | } |
| 393 | j.parse_seccomp_filters(&config.seccomp_policy.with_extension("policy")) |
| 394 | .map_err(Error::DeviceJail)?; |
| 395 | } |
| 396 | j.use_seccomp_filter(); |
| 397 | // Don't do init setup. |
| 398 | j.run_as_init(); |
| 399 | } |
| 400 | |
Chirantan Ekbote | f84c229 | 2020-02-21 16:37:27 +0900 | [diff] [blame] | 401 | // Only pivot_root if we are not re-using the current root directory. |
| 402 | if root != Path::new("/") { |
| 403 | // It's safe to call `namespace_vfs` multiple times. |
| 404 | j.namespace_vfs(); |
| 405 | j.enter_pivot_root(root).map_err(Error::DevicePivotRoot)?; |
| 406 | } |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 407 | |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 408 | // Most devices don't need to open many fds. |
| 409 | let limit = if let Some(r) = r_limit { r } else { 1024u64 }; |
| 410 | j.set_rlimit(libc::RLIMIT_NOFILE as i32, limit, limit) |
| 411 | .map_err(Error::SettingMaxOpenFiles)?; |
| 412 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 413 | Ok(j) |
| 414 | } |
| 415 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 416 | fn simple_jail(cfg: &Config, policy: &str) -> Result<Option<Minijail>> { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 417 | if cfg.sandbox { |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 418 | let pivot_root: &str = option_env!("DEFAULT_PIVOT_ROOT").unwrap_or("/var/empty"); |
| 419 | // A directory for a jailed device's pivot root. |
| 420 | let root_path = Path::new(pivot_root); |
| 421 | if !root_path.exists() { |
| 422 | return Err(Error::PivotRootDoesntExist(pivot_root)); |
| 423 | } |
| 424 | let policy_path: PathBuf = cfg.seccomp_policy_dir.join(policy); |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 425 | let config = SandboxConfig { |
| 426 | limit_caps: true, |
| 427 | log_failures: cfg.seccomp_log_failures, |
| 428 | seccomp_policy: &policy_path, |
| 429 | uid_map: None, |
| 430 | gid_map: None, |
| 431 | }; |
| 432 | Ok(Some(create_base_minijail(root_path, None, Some(&config))?)) |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 433 | } else { |
| 434 | Ok(None) |
| 435 | } |
| 436 | } |
| 437 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 438 | type DeviceResult<T = VirtioDeviceStub> = std::result::Result<T, Error>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 439 | |
| 440 | fn create_block_device( |
| 441 | cfg: &Config, |
| 442 | disk: &DiskOption, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 443 | disk_device_socket: DiskControlResponseSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 444 | ) -> DeviceResult { |
| 445 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 446 | let raw_image: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 447 | // Safe because we will validate |raw_fd|. |
| 448 | unsafe { File::from_raw_fd(raw_fd_from_path(&disk.path)?) } |
| 449 | } else { |
| 450 | OpenOptions::new() |
| 451 | .read(true) |
| 452 | .write(!disk.read_only) |
| 453 | .open(&disk.path) |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 454 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))? |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 455 | }; |
| 456 | // Lock the disk image to prevent other crosvm instances from using it. |
| 457 | let lock_op = if disk.read_only { |
| 458 | FlockOperation::LockShared |
| 459 | } else { |
| 460 | FlockOperation::LockExclusive |
| 461 | }; |
| 462 | flock(&raw_image, lock_op, true).map_err(Error::DiskImageLock)?; |
| 463 | |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 464 | let disk_file = disk::create_disk_file(raw_image).map_err(Error::CreateDiskError)?; |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 465 | let dev = virtio::Block::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 466 | virtio::base_features(cfg.protected_vm), |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 467 | disk_file, |
| 468 | disk.read_only, |
| 469 | disk.sparse, |
Daniel Verkamp | 2767223 | 2019-12-06 17:26:55 +1100 | [diff] [blame] | 470 | disk.block_size, |
Daniel Verkamp | e73c80f | 2019-11-08 10:11:16 -0800 | [diff] [blame] | 471 | Some(disk_device_socket), |
| 472 | ) |
| 473 | .map_err(Error::BlockDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 474 | |
| 475 | Ok(VirtioDeviceStub { |
Cody Schuffelen | 7d533e5 | 2019-07-02 16:54:05 -0700 | [diff] [blame] | 476 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 477 | jail: simple_jail(&cfg, "block_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 478 | }) |
| 479 | } |
| 480 | |
| 481 | fn create_rng_device(cfg: &Config) -> DeviceResult { |
| 482 | let dev = virtio::Rng::new().map_err(Error::RngDeviceNew)?; |
| 483 | |
| 484 | Ok(VirtioDeviceStub { |
| 485 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 486 | jail: simple_jail(&cfg, "rng_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 487 | }) |
| 488 | } |
| 489 | |
| 490 | #[cfg(feature = "tpm")] |
| 491 | fn create_tpm_device(cfg: &Config) -> DeviceResult { |
Michael Hoyle | 6b19695 | 2020-08-02 20:09:41 -0700 | [diff] [blame] | 492 | use base::chown; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 493 | use std::ffi::CString; |
| 494 | use std::fs; |
| 495 | use std::process; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 496 | |
| 497 | let tpm_storage: PathBuf; |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 498 | let mut tpm_jail = simple_jail(&cfg, "tpm_device")?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 499 | |
| 500 | match &mut tpm_jail { |
| 501 | Some(jail) => { |
| 502 | // Create a tmpfs in the device's root directory for tpm |
| 503 | // simulator storage. The size is 20*1024, or 20 KB. |
| 504 | jail.mount_with_data( |
| 505 | Path::new("none"), |
| 506 | Path::new("/"), |
| 507 | "tmpfs", |
| 508 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 509 | "size=20480", |
| 510 | )?; |
| 511 | |
| 512 | let crosvm_ids = add_crosvm_user_to_jail(jail, "tpm")?; |
| 513 | |
| 514 | let pid = process::id(); |
| 515 | let tpm_pid_dir = format!("/run/vm/tpm.{}", pid); |
| 516 | tpm_storage = Path::new(&tpm_pid_dir).to_owned(); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 517 | fs::create_dir_all(&tpm_storage) |
| 518 | .map_err(|e| Error::CreateTpmStorage(tpm_storage.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 519 | 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] | 520 | chown(&tpm_pid_dir_c, crosvm_ids.uid, crosvm_ids.gid) |
| 521 | .map_err(Error::ChownTpmStorage)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 522 | |
| 523 | jail.mount_bind(&tpm_storage, &tpm_storage, true)?; |
| 524 | } |
| 525 | None => { |
| 526 | // Path used inside cros_sdk which does not have /run/vm. |
| 527 | tpm_storage = Path::new("/tmp/tpm-simulator").to_owned(); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | let dev = virtio::Tpm::new(tpm_storage); |
| 532 | |
| 533 | Ok(VirtioDeviceStub { |
| 534 | dev: Box::new(dev), |
| 535 | jail: tpm_jail, |
| 536 | }) |
| 537 | } |
| 538 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 539 | fn create_single_touch_device(cfg: &Config, single_touch_spec: &TouchDeviceOption) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 540 | let socket = single_touch_spec |
| 541 | .get_path() |
| 542 | .into_unix_stream() |
| 543 | .map_err(|e| { |
| 544 | error!("failed configuring virtio single touch: {:?}", e); |
| 545 | e |
| 546 | })?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 547 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 548 | let (width, height) = single_touch_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 549 | let dev = virtio::new_single_touch( |
| 550 | socket, |
| 551 | width, |
| 552 | height, |
| 553 | virtio::base_features(cfg.protected_vm), |
| 554 | ) |
| 555 | .map_err(Error::InputDeviceNew)?; |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 556 | Ok(VirtioDeviceStub { |
| 557 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 558 | jail: simple_jail(&cfg, "input_device")?, |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 559 | }) |
| 560 | } |
| 561 | |
| 562 | fn create_trackpad_device(cfg: &Config, trackpad_spec: &TouchDeviceOption) -> DeviceResult { |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 563 | let socket = trackpad_spec.get_path().into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 564 | error!("failed configuring virtio trackpad: {}", e); |
| 565 | e |
| 566 | })?; |
| 567 | |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 568 | let (width, height) = trackpad_spec.get_size(); |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 569 | let dev = virtio::new_trackpad( |
| 570 | socket, |
| 571 | width, |
| 572 | height, |
| 573 | virtio::base_features(cfg.protected_vm), |
| 574 | ) |
| 575 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 576 | |
| 577 | Ok(VirtioDeviceStub { |
| 578 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 579 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 580 | }) |
| 581 | } |
| 582 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 583 | fn create_mouse_device<T: IntoUnixStream>(cfg: &Config, mouse_socket: T) -> DeviceResult { |
| 584 | let socket = mouse_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 585 | error!("failed configuring virtio mouse: {}", e); |
| 586 | e |
| 587 | })?; |
| 588 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 589 | let dev = virtio::new_mouse(socket, virtio::base_features(cfg.protected_vm)) |
| 590 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 591 | |
| 592 | Ok(VirtioDeviceStub { |
| 593 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 594 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 595 | }) |
| 596 | } |
| 597 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 598 | fn create_keyboard_device<T: IntoUnixStream>(cfg: &Config, keyboard_socket: T) -> DeviceResult { |
| 599 | let socket = keyboard_socket.into_unix_stream().map_err(|e| { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 600 | error!("failed configuring virtio keyboard: {}", e); |
| 601 | e |
| 602 | })?; |
| 603 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 604 | let dev = virtio::new_keyboard(socket, virtio::base_features(cfg.protected_vm)) |
| 605 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 606 | |
| 607 | Ok(VirtioDeviceStub { |
| 608 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 609 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 610 | }) |
| 611 | } |
| 612 | |
| 613 | fn create_vinput_device(cfg: &Config, dev_path: &Path) -> DeviceResult { |
| 614 | let dev_file = OpenOptions::new() |
| 615 | .read(true) |
| 616 | .write(true) |
| 617 | .open(dev_path) |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 618 | .map_err(|e| Error::OpenVinput(dev_path.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 619 | |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 620 | let dev = virtio::new_evdev(dev_file, virtio::base_features(cfg.protected_vm)) |
| 621 | .map_err(Error::InputDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 622 | |
| 623 | Ok(VirtioDeviceStub { |
| 624 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 625 | jail: simple_jail(&cfg, "input_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 626 | }) |
| 627 | } |
| 628 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 629 | fn create_balloon_device(cfg: &Config, socket: BalloonControlResponseSocket) -> DeviceResult { |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 630 | let dev = virtio::Balloon::new(virtio::base_features(cfg.protected_vm), socket) |
| 631 | .map_err(Error::BalloonDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 632 | |
| 633 | Ok(VirtioDeviceStub { |
| 634 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 635 | jail: simple_jail(&cfg, "balloon_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 636 | }) |
| 637 | } |
| 638 | |
| 639 | fn create_tap_net_device(cfg: &Config, tap_fd: RawFd) -> DeviceResult { |
| 640 | // Safe because we ensure that we get a unique handle to the fd. |
| 641 | let tap = unsafe { |
| 642 | Tap::from_raw_fd(validate_raw_fd(tap_fd).map_err(Error::ValidateRawFd)?) |
| 643 | .map_err(Error::CreateTapDevice)? |
| 644 | }; |
| 645 | |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 646 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 647 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 648 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 649 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 650 | vq_pairs = 1; |
| 651 | } |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 652 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 653 | 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] | 654 | |
| 655 | Ok(VirtioDeviceStub { |
| 656 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 657 | jail: simple_jail(&cfg, "net_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 658 | }) |
| 659 | } |
| 660 | |
| 661 | fn create_net_device( |
| 662 | cfg: &Config, |
| 663 | host_ip: Ipv4Addr, |
| 664 | netmask: Ipv4Addr, |
| 665 | mac_address: MacAddress, |
| 666 | mem: &GuestMemory, |
| 667 | ) -> DeviceResult { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 668 | let mut vq_pairs = cfg.net_vq_pairs.unwrap_or(1); |
| 669 | let vcpu_count = cfg.vcpu_count.unwrap_or(1); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 670 | if vcpu_count < vq_pairs as usize { |
Xiong Zhang | 773c707 | 2020-03-20 10:39:55 +0800 | [diff] [blame] | 671 | error!("net vq pairs must be smaller than vcpu count, fall back to single queue mode"); |
| 672 | vq_pairs = 1; |
| 673 | } |
| 674 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 675 | let features = virtio::base_features(cfg.protected_vm); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 676 | let dev = if cfg.vhost_net { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 677 | let dev = virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new( |
| 678 | features, |
| 679 | host_ip, |
| 680 | netmask, |
| 681 | mac_address, |
| 682 | mem, |
| 683 | ) |
| 684 | .map_err(Error::VhostNetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 685 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 686 | } else { |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 687 | 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] | 688 | .map_err(Error::NetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 689 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 690 | }; |
| 691 | |
| 692 | let policy = if cfg.vhost_net { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 693 | "vhost_net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 694 | } else { |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 695 | "net_device" |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | Ok(VirtioDeviceStub { |
| 699 | dev, |
| 700 | jail: simple_jail(&cfg, policy)?, |
| 701 | }) |
| 702 | } |
| 703 | |
| 704 | #[cfg(feature = "gpu")] |
| 705 | fn create_gpu_device( |
| 706 | cfg: &Config, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 707 | exit_evt: &Event, |
Gurchetan Singh | 7ec58fa | 2019-05-15 15:30:38 -0700 | [diff] [blame] | 708 | gpu_device_socket: VmMemoryControlRequestSocket, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 709 | gpu_sockets: Vec<virtio::resource_bridge::ResourceResponseSocket>, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 710 | wayland_socket_path: Option<&PathBuf>, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 711 | x_display: Option<String>, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 712 | event_devices: Vec<EventDevice>, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 713 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 714 | ) -> DeviceResult { |
| 715 | let jailed_wayland_path = Path::new("/wayland-0"); |
| 716 | |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 717 | let mut display_backends = vec![ |
| 718 | virtio::DisplayBackend::X(x_display), |
Jason Macnak | 60eb1fb | 2020-01-09 14:36:29 -0800 | [diff] [blame] | 719 | virtio::DisplayBackend::Stub, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 720 | ]; |
| 721 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 722 | if let Some(socket_path) = wayland_socket_path { |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 723 | display_backends.insert( |
| 724 | 0, |
| 725 | virtio::DisplayBackend::Wayland(if cfg.sandbox { |
| 726 | Some(jailed_wayland_path.to_owned()) |
| 727 | } else { |
| 728 | Some(socket_path.to_owned()) |
| 729 | }), |
| 730 | ); |
| 731 | } |
| 732 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 733 | let dev = virtio::Gpu::new( |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 734 | exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Gurchetan Singh | 7ec58fa | 2019-05-15 15:30:38 -0700 | [diff] [blame] | 735 | Some(gpu_device_socket), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 736 | NonZeroU8::new(1).unwrap(), // number of scanouts |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 737 | gpu_sockets, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 738 | display_backends, |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 739 | cfg.gpu_parameters.as_ref().unwrap(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 740 | event_devices, |
Lingfeng Yang | 5572c8d | 2020-05-05 08:40:36 -0700 | [diff] [blame] | 741 | map_request, |
| 742 | cfg.sandbox, |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 743 | virtio::base_features(cfg.protected_vm), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 744 | ); |
| 745 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 746 | let jail = match simple_jail(&cfg, "gpu_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 747 | Some(mut jail) => { |
| 748 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 749 | // dri directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 750 | jail.mount_with_data( |
| 751 | Path::new("none"), |
| 752 | Path::new("/"), |
| 753 | "tmpfs", |
| 754 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 755 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 756 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 757 | |
| 758 | // Device nodes required for DRM. |
| 759 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 760 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 761 | let sys_devices_path = Path::new("/sys/devices"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 762 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 763 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 764 | let drm_dri_path = Path::new("/dev/dri"); |
Jason Macnak | 2340052 | 2020-08-28 09:10:46 -0700 | [diff] [blame] | 765 | if drm_dri_path.exists() { |
| 766 | jail.mount_bind(drm_dri_path, drm_dri_path, false)?; |
| 767 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 768 | |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 769 | // Prepare GPU shader disk cache directory. |
| 770 | if let Some(cache_dir) = cfg |
| 771 | .gpu_parameters |
| 772 | .as_ref() |
| 773 | .and_then(|params| params.cache_path.as_ref()) |
| 774 | { |
| 775 | if cfg!(any(target_arch = "arm", target_arch = "aarch64")) && cfg.sandbox { |
| 776 | warn!("shader caching not yet supported on ARM with sandbox enabled"); |
| 777 | env::set_var("MESA_GLSL_CACHE_DISABLE", "true"); |
| 778 | } else { |
John Bates | 0405973 | 2020-10-01 15:58:55 -0700 | [diff] [blame] | 779 | env::set_var("MESA_GLSL_CACHE_DISABLE", "false"); |
John Bates | b220eac | 2020-09-14 17:03:02 -0700 | [diff] [blame] | 780 | env::set_var("MESA_GLSL_CACHE_DIR", cache_dir); |
| 781 | if let Some(cache_size) = cfg |
| 782 | .gpu_parameters |
| 783 | .as_ref() |
| 784 | .and_then(|params| params.cache_size.as_ref()) |
| 785 | { |
| 786 | env::set_var("MESA_GLSL_CACHE_MAX_SIZE", cache_size); |
| 787 | } |
| 788 | let shadercache_path = Path::new(cache_dir); |
| 789 | jail.mount_bind(shadercache_path, shadercache_path, true)?; |
| 790 | } |
| 791 | } |
| 792 | |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 793 | // If the ARM specific devices exist on the host, bind mount them in. |
| 794 | let mali0_path = Path::new("/dev/mali0"); |
| 795 | if mali0_path.exists() { |
| 796 | jail.mount_bind(mali0_path, mali0_path, true)?; |
| 797 | } |
| 798 | |
| 799 | let pvr_sync_path = Path::new("/dev/pvr_sync"); |
| 800 | if pvr_sync_path.exists() { |
| 801 | jail.mount_bind(pvr_sync_path, pvr_sync_path, true)?; |
| 802 | } |
| 803 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 804 | // Libraries that are required when mesa drivers are dynamically loaded. |
David Riley | 06787c5 | 2019-07-24 12:09:07 -0700 | [diff] [blame] | 805 | let lib_dirs = &["/usr/lib", "/usr/lib64", "/lib", "/lib64"]; |
| 806 | for dir in lib_dirs { |
| 807 | let dir_path = Path::new(dir); |
| 808 | if dir_path.exists() { |
| 809 | jail.mount_bind(dir_path, dir_path, false)?; |
| 810 | } |
| 811 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 812 | |
| 813 | // Bind mount the wayland socket into jail's root. This is necessary since each |
| 814 | // new wayland context must open() the socket. |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 815 | if let Some(path) = wayland_socket_path { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 816 | jail.mount_bind(path, jailed_wayland_path, true)?; |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 817 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 818 | |
| 819 | add_crosvm_user_to_jail(&mut jail, "gpu")?; |
| 820 | |
David Riley | 54e660b | 2019-07-24 17:22:50 -0700 | [diff] [blame] | 821 | // pvr driver requires read access to /proc/self/task/*/comm. |
| 822 | let proc_path = Path::new("/proc"); |
| 823 | jail.mount( |
| 824 | proc_path, |
| 825 | proc_path, |
| 826 | "proc", |
| 827 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_RDONLY) as usize, |
| 828 | )?; |
| 829 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 830 | Some(jail) |
| 831 | } |
| 832 | None => None, |
| 833 | }; |
| 834 | |
| 835 | Ok(VirtioDeviceStub { |
| 836 | dev: Box::new(dev), |
| 837 | jail, |
| 838 | }) |
| 839 | } |
| 840 | |
| 841 | fn create_wayland_device( |
| 842 | cfg: &Config, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 843 | socket: VmMemoryControlRequestSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 844 | resource_bridge: Option<virtio::resource_bridge::ResourceRequestSocket>, |
| 845 | ) -> DeviceResult { |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 846 | let wayland_socket_dirs = cfg |
| 847 | .wayland_socket_paths |
| 848 | .iter() |
| 849 | .map(|(_name, path)| path.parent()) |
| 850 | .collect::<Option<Vec<_>>>() |
| 851 | .ok_or(Error::InvalidWaylandPath)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 852 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 853 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 854 | let dev = virtio::Wl::new( |
| 855 | features, |
| 856 | cfg.wayland_socket_paths.clone(), |
| 857 | socket, |
| 858 | resource_bridge, |
| 859 | ) |
| 860 | .map_err(Error::WaylandDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 861 | |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 862 | let jail = match simple_jail(&cfg, "wl_device")? { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 863 | Some(mut jail) => { |
| 864 | // Create a tmpfs in the device's root directory so that we can bind mount the wayland |
| 865 | // socket directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 866 | jail.mount_with_data( |
| 867 | Path::new("none"), |
| 868 | Path::new("/"), |
| 869 | "tmpfs", |
| 870 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 871 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 872 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 873 | |
| 874 | // Bind mount the wayland socket's directory into jail's root. This is necessary since |
| 875 | // each new wayland context must open() the socket. If the wayland socket is ever |
| 876 | // destroyed and remade in the same host directory, new connections will be possible |
| 877 | // without restarting the wayland device. |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 878 | for dir in &wayland_socket_dirs { |
| 879 | jail.mount_bind(dir, dir, true)?; |
| 880 | } |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 881 | add_crosvm_user_to_jail(&mut jail, "Wayland")?; |
| 882 | |
| 883 | Some(jail) |
| 884 | } |
| 885 | None => None, |
| 886 | }; |
| 887 | |
| 888 | Ok(VirtioDeviceStub { |
| 889 | dev: Box::new(dev), |
| 890 | jail, |
| 891 | }) |
| 892 | } |
| 893 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 894 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 895 | fn create_video_device( |
| 896 | cfg: &Config, |
| 897 | typ: devices::virtio::VideoDeviceType, |
| 898 | resource_bridge: virtio::resource_bridge::ResourceRequestSocket, |
| 899 | ) -> DeviceResult { |
| 900 | let jail = match simple_jail(&cfg, "video_device")? { |
| 901 | Some(mut jail) => { |
| 902 | match typ { |
| 903 | devices::virtio::VideoDeviceType::Decoder => { |
| 904 | add_crosvm_user_to_jail(&mut jail, "video-decoder")? |
| 905 | } |
| 906 | devices::virtio::VideoDeviceType::Encoder => { |
| 907 | add_crosvm_user_to_jail(&mut jail, "video-encoder")? |
| 908 | } |
| 909 | }; |
| 910 | |
| 911 | // Create a tmpfs in the device's root directory so that we can bind mount files. |
| 912 | jail.mount_with_data( |
| 913 | Path::new("none"), |
| 914 | Path::new("/"), |
| 915 | "tmpfs", |
| 916 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 917 | "size=67108864", |
| 918 | )?; |
| 919 | |
| 920 | // Render node for libvda. |
| 921 | let dev_dri_path = Path::new("/dev/dri/renderD128"); |
| 922 | jail.mount_bind(dev_dri_path, dev_dri_path, false)?; |
| 923 | |
David Stevens | e341d0a | 2020-10-08 18:02:32 +0900 | [diff] [blame] | 924 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 925 | { |
| 926 | // Device nodes used by libdrm through minigbm in libvda on AMD devices. |
| 927 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
| 928 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
| 929 | let sys_devices_path = Path::new("/sys/devices"); |
| 930 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
| 931 | |
| 932 | // Required for loading dri libraries loaded by minigbm on AMD devices. |
| 933 | let lib_dir = Path::new("/usr/lib64"); |
| 934 | jail.mount_bind(lib_dir, lib_dir, false)?; |
| 935 | } |
| 936 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 937 | // Device nodes required by libchrome which establishes Mojo connection in libvda. |
| 938 | let dev_urandom_path = Path::new("/dev/urandom"); |
| 939 | jail.mount_bind(dev_urandom_path, dev_urandom_path, false)?; |
| 940 | let system_bus_socket_path = Path::new("/run/dbus/system_bus_socket"); |
| 941 | jail.mount_bind(system_bus_socket_path, system_bus_socket_path, true)?; |
| 942 | |
| 943 | Some(jail) |
| 944 | } |
| 945 | None => None, |
| 946 | }; |
| 947 | |
| 948 | Ok(VirtioDeviceStub { |
| 949 | dev: Box::new(devices::virtio::VideoDevice::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 950 | virtio::base_features(cfg.protected_vm), |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 951 | typ, |
| 952 | Some(resource_bridge), |
| 953 | )), |
| 954 | jail, |
| 955 | }) |
| 956 | } |
| 957 | |
| 958 | #[cfg(any(feature = "video-decoder", feature = "video-encoder"))] |
| 959 | fn register_video_device( |
| 960 | devs: &mut Vec<VirtioDeviceStub>, |
| 961 | resource_bridges: &mut Vec<virtio::resource_bridge::ResourceResponseSocket>, |
| 962 | cfg: &Config, |
| 963 | typ: devices::virtio::VideoDeviceType, |
| 964 | ) -> std::result::Result<(), Error> { |
| 965 | let (video_socket, gpu_socket) = |
| 966 | virtio::resource_bridge::pair().map_err(Error::CreateSocket)?; |
| 967 | resource_bridges.push(gpu_socket); |
| 968 | devs.push(create_video_device(cfg, typ, video_socket)?); |
| 969 | Ok(()) |
| 970 | } |
| 971 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 972 | fn create_vhost_vsock_device(cfg: &Config, cid: u64, mem: &GuestMemory) -> DeviceResult { |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 973 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 974 | let dev = virtio::vhost::Vsock::new(features, cid, mem).map_err(Error::VhostVsockDeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 975 | |
| 976 | Ok(VirtioDeviceStub { |
| 977 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 978 | jail: simple_jail(&cfg, "vhost_vsock_device")?, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 979 | }) |
| 980 | } |
| 981 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 982 | fn create_fs_device( |
| 983 | cfg: &Config, |
| 984 | uid_map: &str, |
| 985 | gid_map: &str, |
| 986 | src: &Path, |
| 987 | tag: &str, |
| 988 | fs_cfg: virtio::fs::passthrough::Config, |
| 989 | ) -> DeviceResult { |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 990 | let max_open_files = get_max_open_files()?; |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 991 | let j = if cfg.sandbox { |
| 992 | let seccomp_policy = cfg.seccomp_policy_dir.join("fs_device"); |
| 993 | let config = SandboxConfig { |
| 994 | limit_caps: false, |
| 995 | uid_map: Some(uid_map), |
| 996 | gid_map: Some(gid_map), |
| 997 | log_failures: cfg.seccomp_log_failures, |
| 998 | seccomp_policy: &seccomp_policy, |
| 999 | }; |
Chirantan Ekbote | 34d45e5 | 2020-04-20 18:15:02 +0900 | [diff] [blame] | 1000 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1001 | // We want bind mounts from the parent namespaces to propagate into the fs device's |
| 1002 | // namespace. |
| 1003 | jail.set_remount_mode(libc::MS_SLAVE); |
| 1004 | |
| 1005 | jail |
Matt Delco | c24ad78 | 2020-02-14 13:24:36 -0800 | [diff] [blame] | 1006 | } else { |
| 1007 | create_base_minijail(src, Some(max_open_files), None)? |
| 1008 | }; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1009 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1010 | let features = virtio::base_features(cfg.protected_vm); |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1011 | // TODO(chirantan): Use more than one worker once the kernel driver has been fixed to not panic |
| 1012 | // when num_queues > 1. |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 1013 | let dev = virtio::fs::Fs::new(features, tag, 1, fs_cfg).map_err(Error::FsDeviceNew)?; |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1014 | |
| 1015 | Ok(VirtioDeviceStub { |
| 1016 | dev: Box::new(dev), |
| 1017 | jail: Some(j), |
| 1018 | }) |
| 1019 | } |
| 1020 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1021 | fn create_9p_device( |
| 1022 | cfg: &Config, |
| 1023 | uid_map: &str, |
| 1024 | gid_map: &str, |
| 1025 | src: &Path, |
| 1026 | tag: &str, |
| 1027 | ) -> DeviceResult { |
| 1028 | let max_open_files = get_max_open_files()?; |
| 1029 | let (jail, root) = if cfg.sandbox { |
| 1030 | let seccomp_policy = cfg.seccomp_policy_dir.join("9p_device"); |
| 1031 | let config = SandboxConfig { |
| 1032 | limit_caps: false, |
| 1033 | uid_map: Some(uid_map), |
| 1034 | gid_map: Some(gid_map), |
| 1035 | log_failures: cfg.seccomp_log_failures, |
| 1036 | seccomp_policy: &seccomp_policy, |
| 1037 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1038 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1039 | let mut jail = create_base_minijail(src, Some(max_open_files), Some(&config))?; |
| 1040 | // We want bind mounts from the parent namespaces to propagate into the 9p server's |
| 1041 | // namespace. |
| 1042 | jail.set_remount_mode(libc::MS_SLAVE); |
Chirantan Ekbote | 055de38 | 2020-01-24 12:16:58 +0900 | [diff] [blame] | 1043 | |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1044 | // The shared directory becomes the root of the device's file system. |
| 1045 | let root = Path::new("/"); |
| 1046 | (Some(jail), root) |
| 1047 | } else { |
| 1048 | // There's no mount namespace so we tell the server to treat the source directory as the |
| 1049 | // root. |
| 1050 | (None, src) |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1051 | }; |
| 1052 | |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1053 | let features = virtio::base_features(cfg.protected_vm); |
Will Deacon | 81d5adb | 2020-10-06 18:37:48 +0100 | [diff] [blame] | 1054 | let dev = virtio::P9::new(features, root, tag).map_err(Error::P9DeviceNew)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1055 | |
| 1056 | Ok(VirtioDeviceStub { |
| 1057 | dev: Box::new(dev), |
| 1058 | jail, |
| 1059 | }) |
| 1060 | } |
| 1061 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1062 | fn create_pmem_device( |
| 1063 | cfg: &Config, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1064 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1065 | resources: &mut SystemAllocator, |
| 1066 | disk: &DiskOption, |
| 1067 | index: usize, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1068 | pmem_device_socket: VmMsyncRequestSocket, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1069 | ) -> DeviceResult { |
| 1070 | let fd = OpenOptions::new() |
| 1071 | .read(true) |
| 1072 | .write(!disk.read_only) |
| 1073 | .open(&disk.path) |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 1074 | .map_err(|e| Error::Disk(disk.path.to_path_buf(), e))?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1075 | |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1076 | let arena_size = { |
Daniel Verkamp | 46d61ba | 2020-02-25 10:17:50 -0800 | [diff] [blame] | 1077 | let metadata = |
| 1078 | 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] | 1079 | let disk_len = metadata.len(); |
| 1080 | // Linux requires pmem region sizes to be 2 MiB aligned. Linux will fill any partial page |
| 1081 | // at the end of an mmap'd file and won't write back beyond the actual file length, but if |
| 1082 | // we just align the size of the file to 2 MiB then access beyond the last page of the |
| 1083 | // mapped file will generate SIGBUS. So use a memory mapping arena that will provide |
| 1084 | // padding up to 2 MiB. |
| 1085 | let alignment = 2 * 1024 * 1024; |
| 1086 | let align_adjust = if disk_len % alignment != 0 { |
| 1087 | alignment - (disk_len % alignment) |
| 1088 | } else { |
| 1089 | 0 |
| 1090 | }; |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1091 | disk_len |
| 1092 | .checked_add(align_adjust) |
| 1093 | .ok_or(Error::PmemDeviceImageTooBig)? |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | let protection = { |
| 1097 | if disk.read_only { |
| 1098 | Protection::read() |
| 1099 | } else { |
| 1100 | Protection::read_write() |
| 1101 | } |
| 1102 | }; |
| 1103 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1104 | let arena = { |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1105 | // Conversion from u64 to usize may fail on 32bit system. |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1106 | let arena_size = usize::try_from(arena_size).map_err(|_| Error::PmemDeviceImageTooBig)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1107 | |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1108 | let mut arena = MemoryMappingArena::new(arena_size).map_err(Error::ReservePmemMemory)?; |
| 1109 | arena |
Iliyan Malchev | ed14986 | 2020-04-17 23:57:47 +0000 | [diff] [blame] | 1110 | .add_fd_offset_protection(0, arena_size, &fd, 0, protection) |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1111 | .map_err(Error::ReservePmemMemory)?; |
| 1112 | arena |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1113 | }; |
| 1114 | |
| 1115 | let mapping_address = resources |
Xiong Zhang | 383b3b5 | 2019-10-30 14:59:26 +0800 | [diff] [blame] | 1116 | .mmio_allocator(MmioType::High) |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1117 | .allocate_with_align( |
Stephen Barber | dc7c07b | 2019-12-20 12:43:35 -0800 | [diff] [blame] | 1118 | arena_size, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1119 | Alloc::PmemDevice(index), |
| 1120 | format!("pmem_disk_image_{}", index), |
| 1121 | // Linux kernel requires pmem namespaces to be 128 MiB aligned. |
| 1122 | 128 * 1024 * 1024, /* 128 MiB */ |
| 1123 | ) |
| 1124 | .map_err(Error::AllocatePmemDeviceAddress)?; |
| 1125 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1126 | let slot = vm |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1127 | .add_memory_region( |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1128 | GuestAddress(mapping_address), |
Gurchetan Singh | 173fe62 | 2020-05-21 18:05:06 -0700 | [diff] [blame] | 1129 | Box::new(arena), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1130 | /* read_only = */ disk.read_only, |
| 1131 | /* log_dirty_pages = */ false, |
| 1132 | ) |
| 1133 | .map_err(Error::AddPmemDeviceMemory)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1134 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1135 | let dev = virtio::Pmem::new( |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1136 | virtio::base_features(cfg.protected_vm), |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1137 | fd, |
| 1138 | GuestAddress(mapping_address), |
| 1139 | slot, |
| 1140 | arena_size, |
| 1141 | Some(pmem_device_socket), |
| 1142 | ) |
| 1143 | .map_err(Error::PmemDeviceNew)?; |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1144 | |
| 1145 | Ok(VirtioDeviceStub { |
| 1146 | dev: Box::new(dev) as Box<dyn VirtioDevice>, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1147 | jail: simple_jail(&cfg, "pmem_device")?, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1148 | }) |
| 1149 | } |
| 1150 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1151 | fn create_console_device(cfg: &Config, param: &SerialParameters) -> DeviceResult { |
| 1152 | let mut keep_fds = Vec::new(); |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1153 | let evt = Event::new().map_err(Error::CreateEvent)?; |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1154 | let dev = param |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 1155 | .create_serial_device::<Console>(cfg.protected_vm, &evt, &mut keep_fds) |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1156 | .map_err(Error::CreateConsole)?; |
| 1157 | |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1158 | let jail = match simple_jail(&cfg, "serial")? { |
| 1159 | Some(mut jail) => { |
| 1160 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 1161 | // log socket directory into it. |
| 1162 | // The size=67108864 is size=64*1024*1024 or size=64MB. |
| 1163 | jail.mount_with_data( |
| 1164 | Path::new("none"), |
| 1165 | Path::new("/"), |
| 1166 | "tmpfs", |
| 1167 | (libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_NOSUID) as usize, |
| 1168 | "size=67108864", |
| 1169 | )?; |
| 1170 | add_crosvm_user_to_jail(&mut jail, "serial")?; |
| 1171 | let res = param.add_bind_mounts(&mut jail); |
| 1172 | if res.is_err() { |
| 1173 | error!("failed to add bind mounts for console device"); |
| 1174 | } |
| 1175 | Some(jail) |
| 1176 | } |
| 1177 | None => None, |
| 1178 | }; |
| 1179 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1180 | Ok(VirtioDeviceStub { |
| 1181 | dev: Box::new(dev), |
Nicholas Verne | 71e73d8 | 2020-07-08 17:19:55 +1000 | [diff] [blame] | 1182 | jail, // TODO(dverkamp): use a separate policy for console? |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1183 | }) |
| 1184 | } |
| 1185 | |
Dmitry Torokhov | ee42b8c | 2019-05-27 11:14:20 -0700 | [diff] [blame] | 1186 | // gpu_device_socket is not used when GPU support is disabled. |
| 1187 | #[cfg_attr(not(feature = "gpu"), allow(unused_variables))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1188 | fn create_virtio_devices( |
| 1189 | cfg: &Config, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1190 | mem: &GuestMemory, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1191 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1192 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1193 | _exit_evt: &Event, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 1194 | wayland_device_socket: VmMemoryControlRequestSocket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1195 | gpu_device_socket: VmMemoryControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1196 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1197 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1198 | pmem_device_sockets: &mut Vec<VmMsyncRequestSocket>, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1199 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1200 | ) -> DeviceResult<Vec<VirtioDeviceStub>> { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1201 | let mut devs = Vec::new(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1202 | |
Daniel Verkamp | a7b6a1c | 2020-03-09 13:16:46 -0700 | [diff] [blame] | 1203 | for (_, param) in cfg |
| 1204 | .serial_parameters |
| 1205 | .iter() |
| 1206 | .filter(|(_k, v)| v.hardware == SerialHardware::VirtioConsole) |
| 1207 | { |
| 1208 | let dev = create_console_device(cfg, param)?; |
| 1209 | devs.push(dev); |
| 1210 | } |
| 1211 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1212 | for disk in &cfg.disks { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1213 | let disk_device_socket = disk_device_sockets.remove(0); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1214 | devs.push(create_block_device(cfg, disk, disk_device_socket)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1217 | for (index, pmem_disk) in cfg.pmem_devices.iter().enumerate() { |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1218 | let pmem_device_socket = pmem_device_sockets.remove(0); |
| 1219 | devs.push(create_pmem_device( |
| 1220 | cfg, |
| 1221 | vm, |
| 1222 | resources, |
| 1223 | pmem_disk, |
| 1224 | index, |
| 1225 | pmem_device_socket, |
| 1226 | )?); |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1229 | devs.push(create_rng_device(cfg)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1230 | |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1231 | #[cfg(feature = "tpm")] |
| 1232 | { |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1233 | if cfg.software_tpm { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1234 | devs.push(create_tpm_device(cfg)?); |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 1235 | } |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 1238 | if let Some(single_touch_spec) = &cfg.virtio_single_touch { |
| 1239 | devs.push(create_single_touch_device(cfg, single_touch_spec)?); |
| 1240 | } |
| 1241 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1242 | if let Some(trackpad_spec) = &cfg.virtio_trackpad { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1243 | devs.push(create_trackpad_device(cfg, trackpad_spec)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1246 | if let Some(mouse_socket) = &cfg.virtio_mouse { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1247 | devs.push(create_mouse_device(cfg, mouse_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1250 | if let Some(keyboard_socket) = &cfg.virtio_keyboard { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1251 | devs.push(create_keyboard_device(cfg, keyboard_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 1254 | for dev_path in &cfg.virtio_input_evdevs { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1255 | devs.push(create_vinput_device(cfg, dev_path)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1258 | devs.push(create_balloon_device(cfg, balloon_device_socket)?); |
Dylan Reid | 295ccac | 2017-11-06 14:06:24 -0800 | [diff] [blame] | 1259 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1260 | // 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] | 1261 | for tap_fd in &cfg.tap_fd { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1262 | devs.push(create_tap_net_device(cfg, *tap_fd)?); |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1265 | if let (Some(host_ip), Some(netmask), Some(mac_address)) = |
| 1266 | (cfg.host_ip, cfg.netmask, cfg.mac_address) |
| 1267 | { |
| 1268 | devs.push(create_net_device(cfg, host_ip, netmask, mac_address, mem)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1271 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1272 | let mut resource_bridges = Vec::<virtio::resource_bridge::ResourceResponseSocket>::new(); |
| 1273 | |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1274 | if !cfg.wayland_socket_paths.is_empty() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1275 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
| 1276 | let mut wl_resource_bridge = None::<virtio::resource_bridge::ResourceRequestSocket>; |
| 1277 | |
| 1278 | #[cfg(feature = "gpu")] |
| 1279 | { |
Jason Macnak | cc7070b | 2019-11-06 14:48:12 -0800 | [diff] [blame] | 1280 | if cfg.gpu_parameters.is_some() { |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1281 | let (wl_socket, gpu_socket) = |
| 1282 | virtio::resource_bridge::pair().map_err(Error::CreateSocket)?; |
| 1283 | resource_bridges.push(gpu_socket); |
| 1284 | wl_resource_bridge = Some(wl_socket); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | devs.push(create_wayland_device( |
| 1289 | cfg, |
Chirantan Ekbote | dd11d43 | 2019-06-11 21:50:46 +0900 | [diff] [blame] | 1290 | wayland_device_socket, |
| 1291 | wl_resource_bridge, |
| 1292 | )?); |
| 1293 | } |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 1294 | |
Keiichi Watanabe | 57df6a0 | 2019-12-06 22:24:40 +0900 | [diff] [blame] | 1295 | #[cfg(feature = "video-decoder")] |
| 1296 | { |
| 1297 | if cfg.video_dec { |
| 1298 | register_video_device( |
| 1299 | &mut devs, |
| 1300 | &mut resource_bridges, |
| 1301 | cfg, |
| 1302 | devices::virtio::VideoDeviceType::Decoder, |
| 1303 | )?; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | #[cfg(feature = "video-encoder")] |
| 1308 | { |
| 1309 | if cfg.video_enc { |
| 1310 | register_video_device( |
| 1311 | &mut devs, |
| 1312 | &mut resource_bridges, |
| 1313 | cfg, |
| 1314 | devices::virtio::VideoDeviceType::Encoder, |
| 1315 | )?; |
| 1316 | } |
| 1317 | } |
| 1318 | |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1319 | #[cfg(feature = "gpu")] |
| 1320 | { |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1321 | if let Some(gpu_parameters) = &cfg.gpu_parameters { |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1322 | let mut event_devices = Vec::new(); |
| 1323 | if cfg.display_window_mouse { |
| 1324 | let (event_device_socket, virtio_dev_socket) = |
| 1325 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1326 | let (single_touch_width, single_touch_height) = cfg |
| 1327 | .virtio_single_touch |
| 1328 | .as_ref() |
| 1329 | .map(|single_touch_spec| single_touch_spec.get_size()) |
Noah Gold | dc7f52b | 2020-02-01 13:01:58 -0800 | [diff] [blame] | 1330 | .unwrap_or((gpu_parameters.display_width, gpu_parameters.display_height)); |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1331 | let dev = virtio::new_single_touch( |
| 1332 | virtio_dev_socket, |
| 1333 | single_touch_width, |
| 1334 | single_touch_height, |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1335 | virtio::base_features(cfg.protected_vm), |
Kaiyi Li | bccb4eb | 2020-02-06 17:53:11 -0800 | [diff] [blame] | 1336 | ) |
| 1337 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1338 | devs.push(VirtioDeviceStub { |
| 1339 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1340 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1341 | }); |
| 1342 | event_devices.push(EventDevice::touchscreen(event_device_socket)); |
| 1343 | } |
| 1344 | if cfg.display_window_keyboard { |
| 1345 | let (event_device_socket, virtio_dev_socket) = |
| 1346 | UnixStream::pair().map_err(Error::CreateSocket)?; |
Noah Gold | d4ca29b | 2020-10-27 12:21:52 -0700 | [diff] [blame] | 1347 | let dev = virtio::new_keyboard( |
| 1348 | virtio_dev_socket, |
| 1349 | virtio::base_features(cfg.protected_vm), |
| 1350 | ) |
| 1351 | .map_err(Error::InputDeviceNew)?; |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1352 | devs.push(VirtioDeviceStub { |
| 1353 | dev: Box::new(dev), |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1354 | jail: simple_jail(&cfg, "input_device")?, |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1355 | }); |
| 1356 | event_devices.push(EventDevice::keyboard(event_device_socket)); |
| 1357 | } |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1358 | devs.push(create_gpu_device( |
| 1359 | cfg, |
| 1360 | _exit_evt, |
| 1361 | gpu_device_socket, |
| 1362 | resource_bridges, |
Ryo Hashimoto | 0b788de | 2019-12-10 17:14:13 +0900 | [diff] [blame] | 1363 | // Use the unnamed socket for GPU display screens. |
| 1364 | cfg.wayland_socket_paths.get(""), |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1365 | cfg.x_display.clone(), |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1366 | event_devices, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1367 | map_request, |
Zach Reizner | 0f2cfb0 | 2019-06-19 17:46:03 -0700 | [diff] [blame] | 1368 | )?); |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1372 | if let Some(cid) = cfg.cid { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1373 | devs.push(create_vhost_vsock_device(cfg, cid, mem)?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1376 | for shared_dir in &cfg.shared_dirs { |
| 1377 | let SharedDir { |
| 1378 | src, |
| 1379 | tag, |
| 1380 | kind, |
| 1381 | uid_map, |
| 1382 | gid_map, |
| 1383 | cfg: fs_cfg, |
| 1384 | } = shared_dir; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1385 | |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1386 | let dev = match kind { |
| 1387 | SharedDirKind::FS => create_fs_device(cfg, uid_map, gid_map, src, tag, fs_cfg.clone())?, |
Chirantan Ekbote | c6b73e3 | 2020-02-20 15:53:06 +0900 | [diff] [blame] | 1388 | SharedDirKind::P9 => create_9p_device(cfg, uid_map, gid_map, src, tag)?, |
Chirantan Ekbote | bd4723b | 2019-07-17 10:50:30 +0900 | [diff] [blame] | 1389 | }; |
| 1390 | devs.push(dev); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | Ok(devs) |
| 1394 | } |
| 1395 | |
| 1396 | fn create_devices( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 1397 | cfg: &Config, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1398 | mem: &GuestMemory, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1399 | vm: &mut impl Vm, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1400 | resources: &mut SystemAllocator, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1401 | exit_evt: &Event, |
Xiong Zhang | a5d248c | 2019-09-17 14:17:19 -0700 | [diff] [blame] | 1402 | control_sockets: &mut Vec<TaggedControlSocket>, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 1403 | wayland_device_socket: VmMemoryControlRequestSocket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1404 | gpu_device_socket: VmMemoryControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1405 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1406 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1407 | pmem_device_sockets: &mut Vec<VmMsyncRequestSocket>, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1408 | usb_provider: HostBackendDeviceProvider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1409 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1410 | ) -> DeviceResult<Vec<(Box<dyn PciDevice>, Option<Minijail>)>> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1411 | let stubs = create_virtio_devices( |
| 1412 | &cfg, |
| 1413 | mem, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 1414 | vm, |
| 1415 | resources, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1416 | exit_evt, |
| 1417 | wayland_device_socket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 1418 | gpu_device_socket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1419 | balloon_device_socket, |
| 1420 | disk_device_sockets, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 1421 | pmem_device_sockets, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 1422 | map_request, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1423 | )?; |
| 1424 | |
| 1425 | let mut pci_devices = Vec::new(); |
| 1426 | |
| 1427 | for stub in stubs { |
Daniel Verkamp | bb712d6 | 2019-11-19 09:47:33 -0800 | [diff] [blame] | 1428 | let (msi_host_socket, msi_device_socket) = |
| 1429 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 1430 | control_sockets.push(TaggedControlSocket::VmIrq(msi_host_socket)); |
| 1431 | let dev = VirtioPciDevice::new(mem.clone(), stub.dev, msi_device_socket) |
| 1432 | .map_err(Error::VirtioPciDev)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 1433 | let dev = Box::new(dev) as Box<dyn PciDevice>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1434 | pci_devices.push((dev, stub.jail)); |
| 1435 | } |
| 1436 | |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1437 | #[cfg(feature = "audio")] |
Judy Hsiao | d5c1e96 | 2020-02-04 12:30:01 +0800 | [diff] [blame] | 1438 | for ac97_param in &cfg.ac97_parameters { |
| 1439 | 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] | 1440 | let jail = simple_jail(&cfg, dev.minijail_policy())?; |
| 1441 | pci_devices.push((Box::new(dev), jail)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1442 | } |
Andrew Scull | 1590e6f | 2020-03-18 18:00:47 +0000 | [diff] [blame] | 1443 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1444 | // Create xhci controller. |
| 1445 | let usb_controller = Box::new(XhciController::new(mem.clone(), usb_provider)); |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 1446 | pci_devices.push((usb_controller, simple_jail(&cfg, "xhci")?)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1447 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1448 | if !cfg.vfio.is_empty() { |
Xiong Zhang | ea6cf66 | 2019-11-11 18:32:02 +0800 | [diff] [blame] | 1449 | let vfio_container = Arc::new(Mutex::new( |
| 1450 | VfioContainer::new().map_err(Error::CreateVfioDevice)?, |
| 1451 | )); |
| 1452 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1453 | for vfio_path in &cfg.vfio { |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1454 | // create MSI, MSI-X, and Mem request sockets for each vfio device |
| 1455 | let (vfio_host_socket_msi, vfio_device_socket_msi) = |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1456 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1457 | control_sockets.push(TaggedControlSocket::VmIrq(vfio_host_socket_msi)); |
| 1458 | |
| 1459 | let (vfio_host_socket_msix, vfio_device_socket_msix) = |
| 1460 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 1461 | control_sockets.push(TaggedControlSocket::VmIrq(vfio_host_socket_msix)); |
Xiong Zhang | 4b5bb3a | 2019-04-23 17:15:21 +0800 | [diff] [blame] | 1462 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1463 | let (vfio_host_socket_mem, vfio_device_socket_mem) = |
| 1464 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>() |
| 1465 | .map_err(Error::CreateSocket)?; |
| 1466 | control_sockets.push(TaggedControlSocket::VmMemory(vfio_host_socket_mem)); |
Xiong Zhang | 85abeff | 2019-04-23 17:15:24 +0800 | [diff] [blame] | 1467 | |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1468 | let vfiodevice = VfioDevice::new(vfio_path.as_path(), vm, mem, vfio_container.clone()) |
| 1469 | .map_err(Error::CreateVfioDevice)?; |
| 1470 | let vfiopcidevice = Box::new(VfioPciDevice::new( |
| 1471 | vfiodevice, |
Daniel Verkamp | 10154a9 | 2020-09-28 17:44:40 -0700 | [diff] [blame] | 1472 | vfio_device_socket_msi, |
| 1473 | vfio_device_socket_msix, |
Xiong Zhang | 8bb4faa | 2019-11-12 10:06:13 +0800 | [diff] [blame] | 1474 | vfio_device_socket_mem, |
| 1475 | )); |
| 1476 | pci_devices.push((vfiopcidevice, simple_jail(&cfg, "vfio_device")?)); |
| 1477 | } |
Xiong Zhang | 17b0daf | 2019-04-23 17:14:50 +0800 | [diff] [blame] | 1478 | } |
| 1479 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1480 | Ok(pci_devices) |
| 1481 | } |
| 1482 | |
| 1483 | #[derive(Copy, Clone)] |
Chirantan Ekbote | 1a2683b | 2019-11-26 16:28:23 +0900 | [diff] [blame] | 1484 | #[cfg_attr(not(feature = "tpm"), allow(dead_code))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1485 | struct Ids { |
| 1486 | uid: uid_t, |
| 1487 | gid: gid_t, |
| 1488 | } |
| 1489 | |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1490 | // Set the uid/gid for the jailed process and give a basic id map. This is |
| 1491 | // required for bind mounts to work. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1492 | 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] | 1493 | let crosvm_user_group = CStr::from_bytes_with_nul(b"crosvm\0").unwrap(); |
| 1494 | |
| 1495 | let crosvm_uid = match get_user_id(&crosvm_user_group) { |
| 1496 | Ok(u) => u, |
| 1497 | Err(e) => { |
| 1498 | warn!("falling back to current user id for {}: {}", feature, e); |
| 1499 | geteuid() |
| 1500 | } |
| 1501 | }; |
| 1502 | |
| 1503 | let crosvm_gid = match get_group_id(&crosvm_user_group) { |
| 1504 | Ok(u) => u, |
| 1505 | Err(e) => { |
| 1506 | warn!("falling back to current group id for {}: {}", feature, e); |
| 1507 | getegid() |
| 1508 | } |
| 1509 | }; |
| 1510 | |
| 1511 | jail.change_uid(crosvm_uid); |
| 1512 | jail.change_gid(crosvm_gid); |
| 1513 | jail.uidmap(&format!("{0} {0} 1", crosvm_uid)) |
| 1514 | .map_err(Error::SettingUidMap)?; |
| 1515 | jail.gidmap(&format!("{0} {0} 1", crosvm_gid)) |
| 1516 | .map_err(Error::SettingGidMap)?; |
| 1517 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 1518 | Ok(Ids { |
| 1519 | uid: crosvm_uid, |
| 1520 | gid: crosvm_gid, |
| 1521 | }) |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1524 | fn raw_fd_from_path(path: &Path) -> Result<RawFd> { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1525 | if !path.is_file() { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1526 | return Err(Error::InvalidFdPath); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1527 | } |
| 1528 | let raw_fd = path |
| 1529 | .file_name() |
| 1530 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 1531 | .and_then(|fd_str| fd_str.parse::<c_int>().ok()) |
| 1532 | .ok_or(Error::InvalidFdPath)?; |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 1533 | validate_raw_fd(raw_fd).map_err(Error::ValidateRawFd) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Zach Reizner | 65b98f1 | 2019-11-22 17:34:58 -0800 | [diff] [blame] | 1536 | trait IntoUnixStream { |
| 1537 | fn into_unix_stream(self) -> Result<UnixStream>; |
| 1538 | } |
| 1539 | |
| 1540 | impl<'a> IntoUnixStream for &'a Path { |
| 1541 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1542 | if self.parent() == Some(Path::new("/proc/self/fd")) { |
| 1543 | // Safe because we will validate |raw_fd|. |
| 1544 | unsafe { Ok(UnixStream::from_raw_fd(raw_fd_from_path(self)?)) } |
| 1545 | } else { |
| 1546 | UnixStream::connect(self).map_err(Error::InputEventsOpen) |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | impl<'a> IntoUnixStream for &'a PathBuf { |
| 1551 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1552 | self.as_path().into_unix_stream() |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | impl IntoUnixStream for UnixStream { |
| 1557 | fn into_unix_stream(self) -> Result<UnixStream> { |
| 1558 | Ok(self) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1562 | fn setup_vcpu_signal_handler<T: Vcpu>(use_hypervisor_signals: bool) -> Result<()> { |
| 1563 | if use_hypervisor_signals { |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1564 | unsafe { |
| 1565 | extern "C" fn handle_signal() {} |
| 1566 | // Our signal handler does nothing and is trivially async signal safe. |
| 1567 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal) |
| 1568 | .map_err(Error::RegisterSignalHandler)?; |
| 1569 | } |
| 1570 | block_signal(SIGRTMIN() + 0).map_err(Error::BlockSignal)?; |
| 1571 | } else { |
| 1572 | unsafe { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1573 | extern "C" fn handle_signal<T: Vcpu>() { |
| 1574 | T::set_local_immediate_exit(true); |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1575 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1576 | register_rt_signal_handler(SIGRTMIN() + 0, handle_signal::<T>) |
Matt Delco | 84cf9c0 | 2019-10-07 22:38:13 -0700 | [diff] [blame] | 1577 | .map_err(Error::RegisterSignalHandler)?; |
| 1578 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1579 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1580 | Ok(()) |
| 1581 | } |
| 1582 | |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1583 | #[derive(Default)] |
| 1584 | struct VcpuRunMode { |
| 1585 | mtx: Mutex<VmRunMode>, |
| 1586 | cvar: Condvar, |
| 1587 | } |
| 1588 | |
| 1589 | impl VcpuRunMode { |
| 1590 | fn set_and_notify(&self, new_mode: VmRunMode) { |
| 1591 | *self.mtx.lock() = new_mode; |
| 1592 | self.cvar.notify_all(); |
| 1593 | } |
| 1594 | } |
| 1595 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1596 | // Sets up a vcpu and converts it into a runnable vcpu. |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1597 | fn runnable_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1598 | cpu_id: usize, |
| 1599 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1600 | vm: impl VmArch, |
| 1601 | irq_chip: &mut impl IrqChipArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1602 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1603 | run_rt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1604 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1605 | no_smt: bool, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1606 | has_bios: bool, |
| 1607 | use_hypervisor_signals: bool, |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1608 | ) -> Result<(V, VcpuRunHandle)> |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1609 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1610 | V: VcpuArch, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1611 | { |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1612 | let mut vcpu = match vcpu { |
| 1613 | Some(v) => v, |
| 1614 | None => { |
| 1615 | // If vcpu is None, it means this arch/hypervisor requires create_vcpu to be called from |
| 1616 | // the vcpu thread. |
| 1617 | match vm |
| 1618 | .create_vcpu(cpu_id) |
| 1619 | .map_err(Error::CreateVcpu)? |
| 1620 | .downcast::<V>() |
| 1621 | { |
| 1622 | Ok(v) => *v, |
| 1623 | Err(_) => panic!("VM created wrong type of VCPU"), |
| 1624 | } |
| 1625 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1626 | }; |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1627 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1628 | irq_chip |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1629 | .add_vcpu(cpu_id, &vcpu) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1630 | .map_err(Error::AddIrqChipVcpu)?; |
| 1631 | |
Daniel Verkamp | caf9ced | 2020-09-29 15:35:02 -0700 | [diff] [blame] | 1632 | if !vcpu_affinity.is_empty() { |
| 1633 | if let Err(e) = set_cpu_affinity(vcpu_affinity) { |
| 1634 | error!("Failed to set CPU affinity: {}", e); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1638 | Arch::configure_vcpu( |
| 1639 | vm.get_memory(), |
| 1640 | vm.get_hypervisor(), |
| 1641 | irq_chip, |
| 1642 | &mut vcpu, |
| 1643 | cpu_id, |
| 1644 | vcpu_count, |
| 1645 | has_bios, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1646 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1647 | ) |
| 1648 | .map_err(Error::ConfigureVcpu)?; |
| 1649 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1650 | #[cfg(feature = "chromeos")] |
| 1651 | if let Err(e) = base::sched::enable_core_scheduling() { |
| 1652 | error!("Failed to enable core scheduling: {}", e); |
| 1653 | } |
| 1654 | |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1655 | if run_rt { |
| 1656 | const DEFAULT_VCPU_RT_LEVEL: u16 = 6; |
| 1657 | if let Err(e) = set_rt_prio_limit(u64::from(DEFAULT_VCPU_RT_LEVEL)) |
| 1658 | .and_then(|_| set_rt_round_robin(i32::from(DEFAULT_VCPU_RT_LEVEL))) |
| 1659 | { |
| 1660 | warn!("Failed to set vcpu to real time: {}", e); |
| 1661 | } |
| 1662 | } |
| 1663 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1664 | if use_hypervisor_signals { |
| 1665 | let mut v = get_blocked_signals().map_err(Error::GetSignalMask)?; |
| 1666 | v.retain(|&x| x != SIGRTMIN() + 0); |
| 1667 | vcpu.set_signal_mask(&v).map_err(Error::SettingSignalMask)?; |
| 1668 | } |
| 1669 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1670 | let vcpu_run_handle = vcpu |
| 1671 | .take_run_handle(Some(SIGRTMIN() + 0)) |
| 1672 | .map_err(Error::RunnableVcpu)?; |
| 1673 | |
| 1674 | Ok((vcpu, vcpu_run_handle)) |
Dylan Reid | bb30b2f | 2019-10-22 18:30:36 +0300 | [diff] [blame] | 1675 | } |
| 1676 | |
Zhuocheng Ding | db4c70d | 2019-12-02 15:50:24 +0800 | [diff] [blame] | 1677 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1678 | fn inject_interrupt(irq_chip: &mut dyn IrqChipX86_64, vcpu: &dyn VcpuX86_64, vcpu_id: usize) { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1679 | if !irq_chip.interrupt_requested(vcpu_id) || !vcpu.ready_for_interrupt() { |
| 1680 | return; |
| 1681 | } |
| 1682 | |
| 1683 | let vector = irq_chip |
| 1684 | .get_external_interrupt(vcpu_id) |
| 1685 | .unwrap_or_else(|e| { |
| 1686 | error!("get_external_interrupt failed on vcpu {}: {}", vcpu_id, e); |
| 1687 | None |
| 1688 | }); |
| 1689 | if let Some(vector) = vector { |
| 1690 | if let Err(e) = vcpu.interrupt(vector as u32) { |
| 1691 | error!( |
| 1692 | "Failed to inject interrupt {} to vcpu {}: {}", |
| 1693 | vector, vcpu_id, e |
| 1694 | ); |
Zhuocheng Ding | db4c70d | 2019-12-02 15:50:24 +0800 | [diff] [blame] | 1695 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | // The second interrupt request should be handled immediately, so ask vCPU to exit as soon as |
| 1699 | // possible. |
| 1700 | if irq_chip.interrupt_requested(vcpu_id) { |
| 1701 | vcpu.request_interrupt_window(); |
Zhuocheng Ding | db4c70d | 2019-12-02 15:50:24 +0800 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1706 | fn inject_interrupt(_irq_chip: &mut dyn IrqChip, _vcpu: &dyn Vcpu, _vcpu_id: usize) {} |
Zhuocheng Ding | db4c70d | 2019-12-02 15:50:24 +0800 | [diff] [blame] | 1707 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1708 | fn run_vcpu<V>( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1709 | cpu_id: usize, |
| 1710 | vcpu: Option<V>, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1711 | vm: impl VmArch + 'static, |
| 1712 | mut irq_chip: impl IrqChipArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1713 | vcpu_count: usize, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1714 | run_rt: bool, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1715 | vcpu_affinity: Vec<usize>, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1716 | no_smt: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1717 | start_barrier: Arc<Barrier>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1718 | has_bios: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1719 | io_bus: devices::Bus, |
| 1720 | mmio_bus: devices::Bus, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 1721 | exit_evt: Event, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1722 | requires_pvclock_ctrl: bool, |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1723 | run_mode_arc: Arc<VcpuRunMode>, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1724 | use_hypervisor_signals: bool, |
| 1725 | ) -> Result<JoinHandle<()>> |
| 1726 | where |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1727 | V: VcpuArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1728 | { |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1729 | thread::Builder::new() |
| 1730 | .name(format!("crosvm_vcpu{}", cpu_id)) |
| 1731 | .spawn(move || { |
Zach Reizner | 9588531 | 2020-01-29 18:06:01 -0800 | [diff] [blame] | 1732 | // The VCPU thread must trigger the `exit_evt` in all paths, and a `ScopedEvent`'s Drop |
| 1733 | // implementation accomplishes that. |
| 1734 | let _scoped_exit_evt = ScopedEvent::from(exit_evt); |
| 1735 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1736 | let runnable_vcpu = runnable_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1737 | cpu_id, |
| 1738 | vcpu, |
| 1739 | vm, |
| 1740 | &mut irq_chip, |
| 1741 | vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 1742 | run_rt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1743 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1744 | no_smt, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1745 | has_bios, |
| 1746 | use_hypervisor_signals, |
| 1747 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1748 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1749 | start_barrier.wait(); |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1750 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1751 | let (vcpu, vcpu_run_handle) = match runnable_vcpu { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1752 | Ok(v) => v, |
| 1753 | Err(e) => { |
| 1754 | error!("failed to start vcpu {}: {}", cpu_id, e); |
| 1755 | return; |
| 1756 | } |
| 1757 | }; |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1758 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1759 | loop { |
| 1760 | let mut interrupted_by_signal = false; |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1761 | match vcpu.run(&vcpu_run_handle) { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1762 | Ok(VcpuExit::IoIn { port, mut size }) => { |
| 1763 | let mut data = [0; 8]; |
| 1764 | if size > data.len() { |
| 1765 | error!("unsupported IoIn size of {} bytes", size); |
| 1766 | size = data.len(); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1767 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1768 | io_bus.read(port as u64, &mut data[..size]); |
| 1769 | if let Err(e) = vcpu.set_data(&data[..size]) { |
| 1770 | error!("failed to set return data for IoIn: {}", e); |
| 1771 | } |
| 1772 | } |
| 1773 | Ok(VcpuExit::IoOut { |
| 1774 | port, |
| 1775 | mut size, |
| 1776 | data, |
| 1777 | }) => { |
| 1778 | if size > data.len() { |
| 1779 | error!("unsupported IoOut size of {} bytes", size); |
| 1780 | size = data.len(); |
| 1781 | } |
| 1782 | io_bus.write(port as u64, &data[..size]); |
| 1783 | } |
| 1784 | Ok(VcpuExit::MmioRead { address, size }) => { |
| 1785 | let mut data = [0; 8]; |
| 1786 | mmio_bus.read(address, &mut data[..size]); |
| 1787 | // Setting data for mmio can not fail. |
| 1788 | let _ = vcpu.set_data(&data[..size]); |
| 1789 | } |
| 1790 | Ok(VcpuExit::MmioWrite { |
| 1791 | address, |
| 1792 | size, |
| 1793 | data, |
| 1794 | }) => { |
| 1795 | mmio_bus.write(address, &data[..size]); |
| 1796 | } |
| 1797 | Ok(VcpuExit::IoapicEoi { vector }) => { |
| 1798 | if let Err(e) = irq_chip.broadcast_eoi(vector) { |
| 1799 | error!( |
| 1800 | "failed to broadcast eoi {} on vcpu {}: {}", |
| 1801 | vector, cpu_id, e |
| 1802 | ); |
| 1803 | } |
| 1804 | } |
| 1805 | Ok(VcpuExit::Hlt) => break, |
| 1806 | Ok(VcpuExit::Shutdown) => break, |
| 1807 | Ok(VcpuExit::FailEntry { |
| 1808 | hardware_entry_failure_reason, |
| 1809 | }) => { |
| 1810 | error!("vcpu hw run failure: {:#x}", hardware_entry_failure_reason); |
| 1811 | break; |
| 1812 | } |
| 1813 | Ok(VcpuExit::SystemEvent(_, _)) => break, |
| 1814 | Ok(r) => warn!("unexpected vcpu exit: {:?}", r), |
| 1815 | Err(e) => match e.errno() { |
| 1816 | libc::EINTR => interrupted_by_signal = true, |
| 1817 | libc::EAGAIN => {} |
| 1818 | _ => { |
| 1819 | error!("vcpu hit unknown error: {}", e); |
| 1820 | break; |
| 1821 | } |
| 1822 | }, |
| 1823 | } |
| 1824 | |
| 1825 | if interrupted_by_signal { |
| 1826 | if use_hypervisor_signals { |
| 1827 | // Try to clear the signal that we use to kick VCPU if it is pending before |
| 1828 | // attempting to handle pause requests. |
| 1829 | if let Err(e) = clear_signal(SIGRTMIN() + 0) { |
| 1830 | error!("failed to clear pending signal: {}", e); |
| 1831 | break; |
| 1832 | } |
| 1833 | } else { |
| 1834 | vcpu.set_immediate_exit(false); |
| 1835 | } |
| 1836 | let mut run_mode_lock = run_mode_arc.mtx.lock(); |
| 1837 | loop { |
| 1838 | match *run_mode_lock { |
| 1839 | VmRunMode::Running => break, |
| 1840 | VmRunMode::Suspending => { |
| 1841 | // On KVM implementations that use a paravirtualized clock (e.g. |
| 1842 | // x86), a flag must be set to indicate to the guest kernel that a |
| 1843 | // VCPU was suspended. The guest kernel will use this flag to |
| 1844 | // prevent the soft lockup detection from triggering when this VCPU |
| 1845 | // resumes, which could happen days later in realtime. |
| 1846 | if requires_pvclock_ctrl { |
| 1847 | if let Err(e) = vcpu.pvclock_ctrl() { |
| 1848 | error!( |
| 1849 | "failed to tell hypervisor vcpu {} is suspending: {}", |
| 1850 | cpu_id, e |
| 1851 | ); |
Zach Reizner | 795355a | 2019-01-16 17:37:57 -0800 | [diff] [blame] | 1852 | } |
| 1853 | } |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1854 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1855 | VmRunMode::Exiting => return, |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1856 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1857 | // Give ownership of our exclusive lock to the condition variable that will |
| 1858 | // block. When the condition variable is notified, `wait` will unblock and |
| 1859 | // return a new exclusive lock. |
| 1860 | run_mode_lock = run_mode_arc.cvar.wait(run_mode_lock); |
Zhuocheng Ding | db4c70d | 2019-12-02 15:50:24 +0800 | [diff] [blame] | 1861 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1862 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1863 | |
Zach Reizner | 2c770e6 | 2020-09-30 16:49:59 -0700 | [diff] [blame] | 1864 | inject_interrupt(&mut irq_chip, &vcpu, cpu_id); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1865 | } |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 1866 | }) |
| 1867 | .map_err(Error::SpawnVcpu) |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1868 | } |
| 1869 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 1870 | // 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] | 1871 | // Returns an error if any of the fields fail to parse. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 1872 | 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] | 1873 | let mut file = File::open(path)?; |
| 1874 | |
| 1875 | let mut buf = [0u8; 32]; |
| 1876 | let count = file.read(&mut buf)?; |
| 1877 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1878 | let content = |
| 1879 | str::from_utf8(&buf[..count]).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 1880 | content |
| 1881 | .trim() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 1882 | .split_whitespace() |
| 1883 | .map(|x| { |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 1884 | x.parse::<i64>() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 1885 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 1886 | }) |
| 1887 | .collect() |
| 1888 | } |
| 1889 | |
| 1890 | // Reads the contents of a file and converts them into a u64, and if there |
| 1891 | // are multiple fields it only returns the first one. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 1892 | fn file_to_i64<P: AsRef<Path>>(path: P) -> io::Result<i64> { |
| 1893 | file_fields_to_i64(path)? |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame] | 1894 | .into_iter() |
| 1895 | .next() |
| 1896 | .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty file")) |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1899 | fn create_kvm(mem: GuestMemory) -> base::Result<KvmVm> { |
| 1900 | let kvm = Kvm::new()?; |
| 1901 | let vm = KvmVm::new(&kvm, mem)?; |
| 1902 | Ok(vm) |
| 1903 | } |
| 1904 | |
| 1905 | fn create_kvm_kernel_irq_chip( |
| 1906 | vm: &KvmVm, |
| 1907 | vcpu_count: usize, |
| 1908 | _ioapic_device_socket: VmIrqRequestSocket, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1909 | ) -> base::Result<impl IrqChipArch> { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1910 | let irq_chip = KvmKernelIrqChip::new(vm.try_clone()?, vcpu_count)?; |
| 1911 | Ok(irq_chip) |
| 1912 | } |
| 1913 | |
| 1914 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1915 | fn create_kvm_split_irq_chip( |
| 1916 | vm: &KvmVm, |
| 1917 | vcpu_count: usize, |
| 1918 | ioapic_device_socket: VmIrqRequestSocket, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1919 | ) -> base::Result<impl IrqChipArch> { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1920 | let irq_chip = KvmSplitIrqChip::new(vm.try_clone()?, vcpu_count, ioapic_device_socket)?; |
| 1921 | Ok(irq_chip) |
| 1922 | } |
| 1923 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1924 | pub fn run_config(cfg: Config) -> Result<()> { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1925 | if cfg.split_irqchip { |
| 1926 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
| 1927 | { |
| 1928 | unimplemented!("KVM split irqchip mode only supported on x86 processors") |
| 1929 | } |
| 1930 | |
| 1931 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1932 | { |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1933 | run_vm::<_, KvmVcpu, _, _, _>(cfg, create_kvm, create_kvm_split_irq_chip) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } else { |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1936 | run_vm::<_, KvmVcpu, _, _, _>(cfg, create_kvm, create_kvm_kernel_irq_chip) |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1940 | 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] | 1941 | where |
| 1942 | V: VmArch + 'static, |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 1943 | Vcpu: VcpuArch + 'static, |
| 1944 | I: IrqChipArch + 'static, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 1945 | FV: FnOnce(GuestMemory) -> base::Result<V>, |
| 1946 | FI: FnOnce( |
| 1947 | &V, |
| 1948 | usize, // vcpu_count |
| 1949 | VmIrqRequestSocket, // ioapic_device_socket |
| 1950 | ) -> base::Result<I>, |
| 1951 | { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 1952 | if cfg.sandbox { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1953 | // Printing something to the syslog before entering minijail so that libc's syslogger has a |
| 1954 | // chance to open files necessary for its operation, like `/etc/localtime`. After jailing, |
| 1955 | // access to those files will not be possible. |
| 1956 | info!("crosvm entering multiprocess mode"); |
| 1957 | } |
| 1958 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1959 | let (usb_control_socket, usb_provider) = |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1960 | HostBackendDeviceProvider::new().map_err(Error::CreateUsbProvider)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1961 | // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this |
| 1962 | // before any jailed devices have been spawned, so that we can catch any of them that fail very |
| 1963 | // quickly. |
| 1964 | let sigchld_fd = SignalFd::new(libc::SIGCHLD).map_err(Error::CreateSignalFd)?; |
| 1965 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1966 | let initrd_image = if let Some(initrd_path) = &cfg.initrd_path { |
| 1967 | 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] | 1968 | } else { |
| 1969 | None |
| 1970 | }; |
| 1971 | |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1972 | let vm_image = match cfg.executable_path { |
| 1973 | Some(Executable::Kernel(ref kernel_path)) => VmImage::Kernel( |
| 1974 | File::open(kernel_path).map_err(|e| Error::OpenKernel(kernel_path.to_path_buf(), e))?, |
| 1975 | ), |
| 1976 | Some(Executable::Bios(ref bios_path)) => VmImage::Bios( |
| 1977 | File::open(bios_path).map_err(|e| Error::OpenBios(bios_path.to_path_buf(), e))?, |
| 1978 | ), |
| 1979 | _ => panic!("Did not receive a bios or kernel, should be impossible."), |
| 1980 | }; |
| 1981 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1982 | let components = VmComponents { |
Daniel Verkamp | 6a84706 | 2019-11-26 13:16:35 -0800 | [diff] [blame] | 1983 | memory_size: cfg |
| 1984 | .memory |
| 1985 | .unwrap_or(256) |
| 1986 | .checked_mul(1024 * 1024) |
| 1987 | .ok_or(Error::MemoryTooLarge)?, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1988 | vcpu_count: cfg.vcpu_count.unwrap_or(1), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1989 | vcpu_affinity: cfg.vcpu_affinity.clone(), |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 1990 | no_smt: cfg.no_smt, |
Cody Schuffelen | 6d1ab50 | 2019-05-21 12:12:38 -0700 | [diff] [blame] | 1991 | vm_image, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 1992 | android_fstab: cfg |
| 1993 | .android_fstab |
| 1994 | .as_ref() |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1995 | .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] | 1996 | .map_or(Ok(None), |v| v.map(Some))?, |
Kansho Nishida | 282115b | 2019-12-18 13:13:14 +0900 | [diff] [blame] | 1997 | pstore: cfg.pstore.clone(), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1998 | initrd_image, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1999 | extra_kernel_params: cfg.params.clone(), |
| 2000 | wayland_dmabuf: cfg.wayland_dmabuf, |
Tomasz Jeznach | 4264464 | 2020-05-20 23:27:59 -0700 | [diff] [blame] | 2001 | acpi_sdts: cfg |
| 2002 | .acpi_tables |
| 2003 | .iter() |
| 2004 | .map(|path| SDT::from_file(path).map_err(|e| Error::OpenAcpiTable(path.clone(), e))) |
| 2005 | .collect::<Result<Vec<SDT>>>()?, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2006 | rt_cpus: cfg.rt_cpus.clone(), |
Will Deacon | 7d2b8ac | 2020-10-06 18:51:12 +0100 | [diff] [blame] | 2007 | protected_vm: cfg.protected_vm, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2008 | }; |
| 2009 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2010 | let control_server_socket = match &cfg.socket_path { |
| 2011 | Some(path) => Some(UnlinkUnixSeqpacketListener( |
| 2012 | UnixSeqpacketListener::bind(path).map_err(Error::CreateSocket)?, |
| 2013 | )), |
| 2014 | None => None, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2015 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2016 | |
| 2017 | let mut control_sockets = Vec::new(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2018 | let (wayland_host_socket, wayland_device_socket) = |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 2019 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>().map_err(Error::CreateSocket)?; |
| 2020 | control_sockets.push(TaggedControlSocket::VmMemory(wayland_host_socket)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2021 | // 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] | 2022 | let (balloon_host_socket, balloon_device_socket) = |
Charles William Dick | 664cc3c | 2020-01-10 14:31:52 +0900 | [diff] [blame] | 2023 | msg_socket::pair::<BalloonControlCommand, BalloonControlResult>() |
| 2024 | .map_err(Error::CreateSocket)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2025 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2026 | // Create one control socket per disk. |
| 2027 | let mut disk_device_sockets = Vec::new(); |
| 2028 | let mut disk_host_sockets = Vec::new(); |
| 2029 | let disk_count = cfg.disks.len(); |
| 2030 | for _ in 0..disk_count { |
| 2031 | let (disk_host_socket, disk_device_socket) = |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2032 | msg_socket::pair::<DiskControlCommand, DiskControlResult>() |
| 2033 | .map_err(Error::CreateSocket)?; |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2034 | disk_host_sockets.push(disk_host_socket); |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 2035 | disk_device_sockets.push(disk_device_socket); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2038 | let mut pmem_device_sockets = Vec::new(); |
| 2039 | let pmem_count = cfg.pmem_devices.len(); |
| 2040 | for _ in 0..pmem_count { |
| 2041 | let (pmem_host_socket, pmem_device_socket) = |
| 2042 | msg_socket::pair::<VmMsyncResponse, VmMsyncRequest>().map_err(Error::CreateSocket)?; |
| 2043 | pmem_device_sockets.push(pmem_device_socket); |
| 2044 | control_sockets.push(TaggedControlSocket::VmMsync(pmem_host_socket)); |
| 2045 | } |
| 2046 | |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 2047 | let (gpu_host_socket, gpu_device_socket) = |
| 2048 | msg_socket::pair::<VmMemoryResponse, VmMemoryRequest>().map_err(Error::CreateSocket)?; |
| 2049 | control_sockets.push(TaggedControlSocket::VmMemory(gpu_host_socket)); |
| 2050 | |
Zhuocheng Ding | f2e90bf | 2019-12-02 15:50:20 +0800 | [diff] [blame] | 2051 | let (ioapic_host_socket, ioapic_device_socket) = |
| 2052 | msg_socket::pair::<VmIrqResponse, VmIrqRequest>().map_err(Error::CreateSocket)?; |
| 2053 | control_sockets.push(TaggedControlSocket::VmIrq(ioapic_host_socket)); |
| 2054 | |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2055 | let map_request: Arc<Mutex<Option<ExternalMapping>>> = Arc::new(Mutex::new(None)); |
| 2056 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2057 | let linux: RunnableLinuxVm<_, Vcpu, _> = Arch::build_vm( |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2058 | components, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2059 | &cfg.serial_parameters, |
Matt Delco | 45caf91 | 2019-11-13 08:11:09 -0800 | [diff] [blame] | 2060 | simple_jail(&cfg, "serial")?, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 2061 | |mem, vm, sys_allocator, exit_evt| { |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2062 | create_devices( |
| 2063 | &cfg, |
Jakub Staron | a3411ea | 2019-04-24 10:55:25 -0700 | [diff] [blame] | 2064 | mem, |
| 2065 | vm, |
| 2066 | sys_allocator, |
| 2067 | exit_evt, |
Xiong Zhang | a5d248c | 2019-09-17 14:17:19 -0700 | [diff] [blame] | 2068 | &mut control_sockets, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2069 | wayland_device_socket, |
Gurchetan Singh | 96beafc | 2019-05-15 09:46:52 -0700 | [diff] [blame] | 2070 | gpu_device_socket, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2071 | balloon_device_socket, |
| 2072 | &mut disk_device_sockets, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2073 | &mut pmem_device_sockets, |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2074 | usb_provider, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2075 | Arc::clone(&map_request), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2076 | ) |
| 2077 | }, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2078 | create_vm, |
| 2079 | |vm, vcpu_count| create_irq_chip(vm, vcpu_count, ioapic_device_socket), |
Trent Begin | 17ccaad | 2019-04-17 13:51:25 -0600 | [diff] [blame] | 2080 | ) |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 2081 | .map_err(Error::BuildVm)?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 2082 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2083 | run_control( |
| 2084 | linux, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2085 | control_server_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2086 | control_sockets, |
| 2087 | balloon_host_socket, |
| 2088 | &disk_host_sockets, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2089 | usb_control_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2090 | sigchld_fd, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2091 | cfg.sandbox, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2092 | Arc::clone(&map_request), |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 2093 | ) |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2096 | fn run_control<V: VmArch + 'static, Vcpu: VcpuArch + 'static, I: IrqChipArch + 'static>( |
| 2097 | mut linux: RunnableLinuxVm<V, Vcpu, I>, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2098 | control_server_socket: Option<UnlinkUnixSeqpacketListener>, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2099 | mut control_sockets: Vec<TaggedControlSocket>, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 2100 | balloon_host_socket: BalloonControlRequestSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 2101 | disk_host_sockets: &[DiskControlRequestSocket], |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 2102 | usb_control_socket: UsbControlSocket, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2103 | sigchld_fd: SignalFd, |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2104 | sandbox: bool, |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2105 | map_request: Arc<Mutex<Option<ExternalMapping>>>, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2106 | ) -> Result<()> { |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 2107 | const LOWMEM_AVAILABLE: &str = "/sys/kernel/mm/chromeos-low_mem/available"; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2108 | |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2109 | #[derive(PollToken)] |
| 2110 | enum Token { |
| 2111 | Exit, |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2112 | Suspend, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2113 | ChildSignal, |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2114 | IrqFd { index: IrqEventIndex }, |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2115 | BalanceMemory, |
| 2116 | BalloonResult, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2117 | VmControlServer, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2118 | VmControl { index: usize }, |
| 2119 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2120 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 2121 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2122 | .set_raw_mode() |
| 2123 | .expect("failed to set terminal raw mode"); |
| 2124 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2125 | let wait_ctx = WaitContext::build_with(&[ |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2126 | (&linux.exit_evt, Token::Exit), |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2127 | (&linux.suspend_evt, Token::Suspend), |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2128 | (&sigchld_fd, Token::ChildSignal), |
| 2129 | ]) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2130 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | b2110be | 2019-07-23 15:55:03 -0700 | [diff] [blame] | 2131 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2132 | if let Some(socket_server) = &control_server_socket { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2133 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2134 | .add(socket_server, Token::VmControlServer) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2135 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2136 | } |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2137 | for (index, socket) in control_sockets.iter().enumerate() { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2138 | wait_ctx |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2139 | .add(socket.as_ref(), Token::VmControl { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2140 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2141 | } |
| 2142 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2143 | let events = linux |
| 2144 | .irq_chip |
| 2145 | .irq_event_tokens() |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2146 | .map_err(Error::WaitContextAdd)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2147 | |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2148 | for (index, _gsi, evt) in events { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2149 | wait_ctx |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2150 | .add(&evt, Token::IrqFd { index }) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2151 | .map_err(Error::WaitContextAdd)?; |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2152 | } |
| 2153 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2154 | // Balance available memory between guest and host every second. |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2155 | let mut balancemem_timer = Timer::new().map_err(Error::CreateTimer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2156 | if Path::new(LOWMEM_AVAILABLE).exists() { |
| 2157 | // Create timer request balloon stats every 1s. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2158 | wait_ctx |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2159 | .add(&balancemem_timer, Token::BalanceMemory) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2160 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2161 | let balancemem_dur = Duration::from_secs(1); |
| 2162 | let balancemem_int = Duration::from_secs(1); |
| 2163 | balancemem_timer |
| 2164 | .reset(balancemem_dur, Some(balancemem_int)) |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2165 | .map_err(Error::ResetTimer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2166 | |
| 2167 | // Listen for balloon statistics from the guest so we can balance. |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2168 | wait_ctx |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2169 | .add(&balloon_host_socket, Token::BalloonResult) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2170 | .map_err(Error::WaitContextAdd)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2171 | } else { |
| 2172 | warn!("Unable to open low mem available, maybe not a chrome os kernel"); |
| 2173 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2174 | |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 2175 | if sandbox { |
| 2176 | // Before starting VCPUs, in case we started with some capabilities, drop them all. |
| 2177 | drop_capabilities().map_err(Error::DropCapabilities)?; |
| 2178 | } |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 2179 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2180 | let mut vcpu_handles = Vec::with_capacity(linux.vcpu_count); |
| 2181 | let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpu_count + 1)); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 2182 | let run_mode_arc = Arc::new(VcpuRunMode::default()); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2183 | let use_hypervisor_signals = !linux |
| 2184 | .vm |
| 2185 | .get_hypervisor() |
| 2186 | .check_capability(&HypervisorCap::ImmediateExit); |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2187 | setup_vcpu_signal_handler::<Vcpu>(use_hypervisor_signals)?; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2188 | |
Zach Reizner | 304e731 | 2020-09-29 16:00:24 -0700 | [diff] [blame] | 2189 | let vcpus: Vec<Option<_>> = match linux.vcpus.take() { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2190 | Some(vec) => vec.into_iter().map(|vcpu| Some(vcpu)).collect(), |
| 2191 | None => iter::repeat_with(|| None).take(linux.vcpu_count).collect(), |
| 2192 | }; |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 2193 | for (cpu_id, vcpu) in vcpus.into_iter().enumerate() { |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2194 | let vcpu_affinity = match linux.vcpu_affinity.clone() { |
| 2195 | Some(VcpuAffinity::Global(v)) => v, |
| 2196 | Some(VcpuAffinity::PerVcpu(mut m)) => m.remove(&cpu_id).unwrap_or_default(), |
| 2197 | None => Default::default(), |
| 2198 | }; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2199 | let handle = run_vcpu( |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2200 | cpu_id, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2201 | vcpu, |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2202 | linux.vm.try_clone().map_err(Error::CloneEvent)?, |
| 2203 | linux.irq_chip.try_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2204 | linux.vcpu_count, |
Kansho Nishida | ab205af | 2020-08-13 18:17:50 +0900 | [diff] [blame] | 2205 | linux.rt_cpus.contains(&cpu_id), |
Daniel Verkamp | c677fb4 | 2020-09-08 13:47:49 -0700 | [diff] [blame] | 2206 | vcpu_affinity, |
Suleiman Souhlal | 015c3c1 | 2020-10-07 14:15:41 +0900 | [diff] [blame] | 2207 | linux.no_smt, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2208 | vcpu_thread_barrier.clone(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2209 | linux.has_bios, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2210 | linux.io_bus.clone(), |
| 2211 | linux.mmio_bus.clone(), |
Michael Hoyle | 685316f | 2020-09-16 15:29:20 -0700 | [diff] [blame] | 2212 | linux.exit_evt.try_clone().map_err(Error::CloneEvent)?, |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2213 | linux.vm.check_capability(VmCap::PvClockSuspend), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 2214 | run_mode_arc.clone(), |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2215 | use_hypervisor_signals, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2216 | )?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2217 | vcpu_handles.push(handle); |
| 2218 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2219 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2220 | vcpu_thread_barrier.wait(); |
| 2221 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2222 | 'wait: loop { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2223 | let events = { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2224 | match wait_ctx.wait() { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2225 | Ok(v) => v, |
| 2226 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 2227 | error!("failed to poll: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2232 | |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2233 | if let Err(e) = linux.irq_chip.process_delayed_irq_events() { |
| 2234 | warn!("can't deliver delayed irqs: {}", e); |
| 2235 | } |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2236 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2237 | let mut vm_control_indices_to_remove = Vec::new(); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2238 | for event in events.iter().filter(|e| e.is_readable) { |
| 2239 | match event.token { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2240 | Token::Exit => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2241 | info!("vcpu requested shutdown"); |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2242 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2243 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2244 | Token::Suspend => { |
| 2245 | info!("VM requested suspend"); |
| 2246 | linux.suspend_evt.read().unwrap(); |
| 2247 | run_mode_arc.set_and_notify(VmRunMode::Suspending); |
| 2248 | for handle in &vcpu_handles { |
| 2249 | let _ = handle.kill(SIGRTMIN() + 0); |
| 2250 | } |
| 2251 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2252 | Token::ChildSignal => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2253 | // Print all available siginfo structs, then exit the loop. |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2254 | while let Some(siginfo) = sigchld_fd.read().map_err(Error::SignalFd)? { |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 2255 | let pid = siginfo.ssi_pid; |
| 2256 | let pid_label = match linux.pid_debug_label_map.get(&pid) { |
| 2257 | Some(label) => format!("{} (pid {})", label, pid), |
| 2258 | None => format!("pid {}", pid), |
| 2259 | }; |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2260 | error!( |
| 2261 | "child {} died: signo {}, status {}, code {}", |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 2262 | pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 2263 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2264 | } |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2265 | break 'wait; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2266 | } |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2267 | Token::IrqFd { index } => { |
| 2268 | if let Err(e) = linux.irq_chip.service_irq_event(index) { |
| 2269 | error!("failed to signal irq {}: {}", index, e); |
Zhuocheng Ding | b9f4c9b | 2019-12-02 15:50:28 +0800 | [diff] [blame] | 2270 | } |
| 2271 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2272 | Token::BalanceMemory => { |
Michael Hoyle | 08d86a4 | 2020-08-19 14:45:21 -0700 | [diff] [blame] | 2273 | balancemem_timer.wait().map_err(Error::Timer)?; |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2274 | let command = BalloonControlCommand::Stats {}; |
| 2275 | if let Err(e) = balloon_host_socket.send(&command) { |
| 2276 | warn!("failed to send stats request to balloon device: {}", e); |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2277 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2278 | } |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2279 | Token::BalloonResult => { |
| 2280 | match balloon_host_socket.recv() { |
| 2281 | Ok(BalloonControlResult::Stats { |
| 2282 | stats, |
| 2283 | balloon_actual: balloon_actual_u, |
| 2284 | }) => { |
| 2285 | // Available memory is reported in MB, and we need bytes. |
| 2286 | let host_available = file_to_i64(LOWMEM_AVAILABLE) |
| 2287 | .map_err(Error::ReadMemAvailable)? |
| 2288 | << 20; |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2289 | let guest_free_u = if let Some(free) = stats.free_memory { |
| 2290 | free |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2291 | } else { |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2292 | warn!("guest free_memory stat is missing"); |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2293 | continue; |
| 2294 | }; |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2295 | let guest_cached_u = if let Some(cached) = stats.disk_caches { |
| 2296 | cached |
| 2297 | } else { |
| 2298 | warn!("guest disk_caches stat is missing"); |
| 2299 | continue; |
| 2300 | }; |
| 2301 | if guest_free_u > i64::max_value() as u64 { |
| 2302 | warn!("guest free memory is too large"); |
| 2303 | continue; |
| 2304 | } |
| 2305 | if guest_cached_u > i64::max_value() as u64 { |
| 2306 | warn!("guest cached memory is too large"); |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2307 | continue; |
| 2308 | } |
| 2309 | if balloon_actual_u > i64::max_value() as u64 { |
| 2310 | warn!("actual balloon size is too large"); |
| 2311 | continue; |
| 2312 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2313 | // Tell the guest to change the balloon size if the target balloon size |
| 2314 | // is more than 5% different from the current balloon size. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2315 | const RESIZE_PERCENT: i64 = 5; |
| 2316 | let balloon_actual = balloon_actual_u as i64; |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2317 | let guest_free = guest_free_u as i64; |
| 2318 | let guest_cached = guest_cached_u as i64; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2319 | // Compute how much memory the guest should have available after we |
| 2320 | // rebalance. |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2321 | let guest_available_target = host_available; |
| 2322 | let guest_available_delta = |
| 2323 | guest_available_target - guest_free - guest_cached; |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2324 | // How much do we have to change the balloon to balance. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2325 | let balloon_target = max(balloon_actual - guest_available_delta, 0); |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2326 | // Compute the change in balloon size in percent. If the balloon size |
| 2327 | // is 0, use 1 so we don't overflow from the infinity % increase. |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2328 | let balloon_change_percent = (balloon_actual - balloon_target).abs() |
| 2329 | * 100 |
| 2330 | / max(balloon_actual, 1); |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2331 | |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2332 | if balloon_change_percent >= RESIZE_PERCENT { |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2333 | info!("resizing balloon: host avail {}, guest free {} cached {} (target {}), balloon actual {} (target {})", |
Daniel Verkamp | 1cd8099 | 2020-07-27 12:41:50 -0700 | [diff] [blame] | 2334 | host_available, |
Suleiman Souhlal | 14fa6bd | 2020-08-18 13:01:15 +0900 | [diff] [blame] | 2335 | guest_free, |
| 2336 | guest_cached, |
Daniel Verkamp | 1cd8099 | 2020-07-27 12:41:50 -0700 | [diff] [blame] | 2337 | guest_available_target, |
| 2338 | balloon_actual, |
| 2339 | balloon_target, |
| 2340 | ); |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2341 | let command = BalloonControlCommand::Adjust { |
| 2342 | num_bytes: balloon_target as u64, |
| 2343 | }; |
| 2344 | if let Err(e) = balloon_host_socket.send(&command) { |
| 2345 | warn!("failed to send memory value to balloon device: {}", e); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | Err(e) => { |
| 2350 | error!("failed to recv BalloonControlResult: {}", e); |
| 2351 | } |
| 2352 | }; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 2353 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2354 | Token::VmControlServer => { |
| 2355 | if let Some(socket_server) = &control_server_socket { |
| 2356 | match socket_server.accept() { |
| 2357 | Ok(socket) => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2358 | wait_ctx |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2359 | .add( |
| 2360 | &socket, |
| 2361 | Token::VmControl { |
| 2362 | index: control_sockets.len(), |
| 2363 | }, |
| 2364 | ) |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2365 | .map_err(Error::WaitContextAdd)?; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2366 | control_sockets |
| 2367 | .push(TaggedControlSocket::Vm(MsgSocket::new(socket))); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2368 | } |
| 2369 | Err(e) => error!("failed to accept socket: {}", e), |
| 2370 | } |
| 2371 | } |
| 2372 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2373 | Token::VmControl { index } => { |
Daniel Verkamp | 37c4a78 | 2019-01-04 10:44:17 -0800 | [diff] [blame] | 2374 | if let Some(socket) = control_sockets.get(index) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2375 | match socket { |
| 2376 | TaggedControlSocket::Vm(socket) => match socket.recv() { |
| 2377 | Ok(request) => { |
| 2378 | let mut run_mode_opt = None; |
| 2379 | let response = request.execute( |
| 2380 | &mut run_mode_opt, |
| 2381 | &balloon_host_socket, |
| 2382 | disk_host_sockets, |
| 2383 | &usb_control_socket, |
| 2384 | ); |
| 2385 | if let Err(e) = socket.send(&response) { |
| 2386 | error!("failed to send VmResponse: {}", e); |
| 2387 | } |
| 2388 | if let Some(run_mode) = run_mode_opt { |
| 2389 | info!("control socket changed run mode to {}", run_mode); |
| 2390 | match run_mode { |
| 2391 | VmRunMode::Exiting => { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2392 | break 'wait; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2393 | } |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2394 | VmRunMode::Running => { |
| 2395 | if let VmRunMode::Suspending = |
| 2396 | *run_mode_arc.mtx.lock() |
| 2397 | { |
| 2398 | linux.io_bus.notify_resume(); |
| 2399 | } |
| 2400 | run_mode_arc.set_and_notify(VmRunMode::Running); |
| 2401 | for handle in &vcpu_handles { |
| 2402 | let _ = handle.kill(SIGRTMIN() + 0); |
| 2403 | } |
| 2404 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2405 | other => { |
| 2406 | run_mode_arc.set_and_notify(other); |
| 2407 | for handle in &vcpu_handles { |
| 2408 | let _ = handle.kill(SIGRTMIN() + 0); |
| 2409 | } |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2413 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2414 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 2415 | if let MsgError::RecvZero = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2416 | vm_control_indices_to_remove.push(index); |
| 2417 | } else { |
| 2418 | error!("failed to recv VmRequest: {}", e); |
| 2419 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2420 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2421 | }, |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 2422 | TaggedControlSocket::VmMemory(socket) => match socket.recv() { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2423 | Ok(request) => { |
Lingfeng Yang | d6ac1ab | 2020-01-31 13:55:35 -0800 | [diff] [blame] | 2424 | let response = request.execute( |
| 2425 | &mut linux.vm, |
| 2426 | &mut linux.resources, |
| 2427 | Arc::clone(&map_request), |
| 2428 | ); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2429 | if let Err(e) = socket.send(&response) { |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 2430 | error!("failed to send VmMemoryControlResponse: {}", e); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 2434 | if let MsgError::RecvZero = e { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2435 | vm_control_indices_to_remove.push(index); |
| 2436 | } else { |
Gurchetan Singh | 53edb81 | 2019-05-22 08:57:16 -0700 | [diff] [blame] | 2437 | error!("failed to recv VmMemoryControlRequest: {}", e); |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | }, |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2441 | TaggedControlSocket::VmIrq(socket) => match socket.recv() { |
| 2442 | Ok(request) => { |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2443 | let response = { |
| 2444 | let irq_chip = &mut linux.irq_chip; |
| 2445 | request.execute( |
| 2446 | |setup| match setup { |
| 2447 | IrqSetup::Event(irq, ev) => { |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2448 | if let Some(event_index) = irq_chip |
| 2449 | .register_irq_event(irq, ev, None)? |
| 2450 | { |
| 2451 | match wait_ctx.add( |
| 2452 | ev, |
| 2453 | Token::IrqFd { |
| 2454 | index: event_index |
| 2455 | }, |
| 2456 | ) { |
| 2457 | Err(e) => { |
| 2458 | warn!("failed to add IrqFd to poll context: {}", e); |
| 2459 | Err(e) |
| 2460 | }, |
| 2461 | Ok(_) => { |
| 2462 | Ok(()) |
| 2463 | } |
| 2464 | } |
| 2465 | } else { |
| 2466 | Ok(()) |
| 2467 | } |
Steven Richman | f32d0b4 | 2020-06-20 21:45:32 -0700 | [diff] [blame] | 2468 | } |
| 2469 | IrqSetup::Route(route) => irq_chip.route_irq(route), |
| 2470 | }, |
| 2471 | &mut linux.resources, |
| 2472 | ) |
| 2473 | }; |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2474 | if let Err(e) = socket.send(&response) { |
| 2475 | error!("failed to send VmIrqResponse: {}", e); |
| 2476 | } |
| 2477 | } |
| 2478 | Err(e) => { |
Zach Reizner | 297ae77 | 2020-02-21 14:45:14 -0800 | [diff] [blame] | 2479 | if let MsgError::RecvZero = e { |
Xiong Zhang | 2515b75 | 2019-09-19 10:29:02 +0800 | [diff] [blame] | 2480 | vm_control_indices_to_remove.push(index); |
| 2481 | } else { |
| 2482 | error!("failed to recv VmIrqRequest: {}", e); |
| 2483 | } |
| 2484 | } |
| 2485 | }, |
Daniel Verkamp | e1980a9 | 2020-02-07 11:00:55 -0800 | [diff] [blame] | 2486 | TaggedControlSocket::VmMsync(socket) => match socket.recv() { |
| 2487 | Ok(request) => { |
| 2488 | let response = request.execute(&mut linux.vm); |
| 2489 | if let Err(e) = socket.send(&response) { |
| 2490 | error!("failed to send VmMsyncResponse: {}", e); |
| 2491 | } |
| 2492 | } |
| 2493 | Err(e) => { |
| 2494 | if let MsgError::BadRecvSize { actual: 0, .. } = e { |
| 2495 | vm_control_indices_to_remove.push(index); |
| 2496 | } else { |
| 2497 | error!("failed to recv VmMsyncRequest: {}", e); |
| 2498 | } |
| 2499 | } |
| 2500 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2501 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2502 | } |
| 2503 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2504 | } |
| 2505 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2506 | |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2507 | for event in events.iter().filter(|e| e.is_hungup) { |
| 2508 | match event.token { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2509 | Token::Exit => {} |
Chuanxiao Dong | 546f01c | 2020-02-12 21:58:47 +0800 | [diff] [blame] | 2510 | Token::Suspend => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2511 | Token::ChildSignal => {} |
Colin Downs-Razouk | bd53276 | 2020-09-08 15:49:35 -0700 | [diff] [blame^] | 2512 | Token::IrqFd { index: _ } => {} |
Charles William Dick | 0bf8a55 | 2019-10-29 15:36:01 +0900 | [diff] [blame] | 2513 | Token::BalanceMemory => {} |
| 2514 | Token::BalloonResult => {} |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2515 | Token::VmControlServer => {} |
| 2516 | Token::VmControl { index } => { |
| 2517 | // It's possible more data is readable and buffered while the socket is hungup, |
| 2518 | // so don't delete the socket from the poll context until we're sure all the |
| 2519 | // data is read. |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 2520 | match control_sockets |
| 2521 | .get(index) |
| 2522 | .map(|s| s.as_ref().get_readable_bytes()) |
| 2523 | { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2524 | Some(Ok(0)) | Some(Err(_)) => vm_control_indices_to_remove.push(index), |
| 2525 | Some(Ok(x)) => info!("control index {} has {} bytes readable", index, x), |
| 2526 | _ => {} |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 2527 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 2528 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2529 | } |
| 2530 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2531 | |
| 2532 | // 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] | 2533 | // preserves correct indexes as each element is removed. |
Daniel Verkamp | 8c2f000 | 2020-08-31 15:13:35 -0700 | [diff] [blame] | 2534 | vm_control_indices_to_remove.sort_unstable_by_key(|&k| Reverse(k)); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2535 | vm_control_indices_to_remove.dedup(); |
| 2536 | for index in vm_control_indices_to_remove { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2537 | // Delete the socket from the `wait_ctx` synchronously. Otherwise, the kernel will do |
| 2538 | // 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] | 2539 | // 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] | 2540 | // 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] | 2541 | // that has already been closed. Because the token associated with that spurious event |
| 2542 | // now belongs to a different socket, the control loop will start to interact with |
| 2543 | // sockets that might not be ready to use. This can cause incorrect hangup detection or |
| 2544 | // blocking on a socket that will never be ready. See also: crbug.com/1019986 |
| 2545 | if let Some(socket) = control_sockets.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2546 | wait_ctx.delete(socket).map_err(Error::WaitContextDelete)?; |
Zide Chen | 8958407 | 2019-11-14 10:33:51 -0800 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | // This line implicitly drops the socket at `index` when it gets returned by |
| 2550 | // `swap_remove`. After this line, the socket at `index` is not the one from |
| 2551 | // `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] | 2552 | // 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] | 2553 | control_sockets.swap_remove(index); |
| 2554 | if let Some(socket) = control_sockets.get(index) { |
Michael Hoyle | e392c46 | 2020-10-07 03:29:24 -0700 | [diff] [blame] | 2555 | wait_ctx |
| 2556 | .modify(socket, EventType::Read, Token::VmControl { index }) |
| 2557 | .map_err(Error::WaitContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 2558 | } |
| 2559 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 2562 | // VCPU threads MUST see the VmRunMode flag, otherwise they may re-enter the VM. |
| 2563 | run_mode_arc.set_and_notify(VmRunMode::Exiting); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 2564 | for handle in vcpu_handles { |
Dmitry Torokhov | cd40533 | 2018-02-16 16:25:54 -0800 | [diff] [blame] | 2565 | match handle.kill(SIGRTMIN() + 0) { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2566 | Ok(_) => { |
| 2567 | if let Err(e) = handle.join() { |
| 2568 | error!("failed to join vcpu thread: {:?}", e); |
| 2569 | } |
| 2570 | } |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 2571 | Err(e) => error!("failed to kill vcpu thread: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
Daniel Verkamp | 94c3527 | 2019-09-12 13:31:30 -0700 | [diff] [blame] | 2575 | // Explicitly drop the VM structure here to allow the devices to clean up before the |
| 2576 | // control sockets are closed when this function exits. |
| 2577 | mem::drop(linux); |
| 2578 | |
Zach Reizner | 19ad1f3 | 2019-12-12 18:58:50 -0800 | [diff] [blame] | 2579 | stdin() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 2580 | .set_canon_mode() |
| 2581 | .expect("failed to restore canonical mode for terminal"); |
| 2582 | |
| 2583 | Ok(()) |
| 2584 | } |