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 | |
| 5 | use std; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 6 | use std::cmp::min; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 7 | use std::error::Error as StdError; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 8 | use std::ffi::CStr; |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 9 | use std::fmt::{self, Display}; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 10 | use std::fs::{File, OpenOptions}; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 11 | use std::io::{self, stdin, Read}; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 12 | use std::net::Ipv4Addr; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 13 | use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 14 | use std::os::unix::net::UnixStream; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 15 | use std::path::{Path, PathBuf}; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 16 | use std::str; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 17 | use std::sync::{Arc, Barrier}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 18 | use std::thread; |
| 19 | use std::thread::JoinHandle; |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 20 | use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 21 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 22 | use libc::{self, c_int, gid_t, uid_t}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 23 | |
Dylan Reid | 3082e8e | 2019-01-07 10:33:48 -0800 | [diff] [blame] | 24 | use audio_streams::DummyStreamSource; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 25 | use devices::virtio::{self, VirtioDevice}; |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 26 | use devices::{self, HostBackendDeviceProvider, PciDevice, VirtioPciDevice, XhciController}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 27 | use io_jail::{self, Minijail}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 28 | use kvm::*; |
paulhsia | f052cfe | 2019-01-22 15:22:25 +0800 | [diff] [blame] | 29 | use libcras::CrasClient; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 30 | use msg_socket::{MsgError, MsgReceiver, MsgSender, MsgSocket}; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 31 | use net_util::{Error as NetError, MacAddress, Tap}; |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 32 | use qcow::{self, ImageType, QcowFile}; |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 33 | use rand_ish::SimpleRng; |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 34 | use remain::sorted; |
Daniel Prilik | d92f81a | 2019-03-26 14:28:19 -0700 | [diff] [blame] | 35 | #[cfg(feature = "gpu-forward")] |
| 36 | use resources::Alloc; |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 37 | use sync::{Condvar, Mutex}; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 38 | use sys_util::net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener}; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 39 | use sys_util::{ |
David Tolnay | 633426a | 2019-04-12 12:18:35 -0700 | [diff] [blame] | 40 | self, block_signal, clear_signal, drop_capabilities, error, flock, get_blocked_signals, |
| 41 | get_group_id, get_user_id, getegid, geteuid, info, register_signal_handler, set_cpu_affinity, |
| 42 | validate_raw_fd, warn, EventFd, FlockOperation, GuestMemory, Killable, PollContext, PollToken, |
| 43 | SignalFd, Terminal, TimerFd, SIGRTMIN, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 44 | }; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 45 | #[cfg(feature = "gpu-forward")] |
| 46 | use sys_util::{GuestAddress, MemoryMapping, Protection}; |
Jason D. Clinton | 865323d | 2017-09-27 22:04:03 -0600 | [diff] [blame] | 47 | use vhost; |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 48 | use vm_control::{ |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 49 | BalloonControlCommand, BalloonControlRequestSocket, BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 50 | DiskControlCommand, DiskControlRequestSocket, DiskControlResponseSocket, DiskControlResult, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 51 | UsbControlSocket, VmControlResponseSocket, VmRunMode, WlControlRequestSocket, |
| 52 | WlControlResponseSocket, WlDriverRequest, WlDriverResponse, |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 53 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 54 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 55 | use crate::{Config, DiskOption, TouchDeviceOption}; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 56 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 57 | use arch::{self, LinuxArch, RunnableLinuxVm, VirtioDeviceStub, VmComponents}; |
Sonny Rao | ed517d1 | 2018-02-13 22:09:43 -0800 | [diff] [blame] | 58 | |
Sonny Rao | 2ffa0cb | 2018-02-26 17:27:40 -0800 | [diff] [blame] | 59 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
| 60 | use aarch64::AArch64 as Arch; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 61 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 62 | use x86_64::X8664arch as Arch; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 63 | |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 64 | #[cfg(feature = "gpu-forward")] |
David Tolnay | aecf9a4 | 2019-04-11 14:30:00 -0700 | [diff] [blame] | 65 | use render_node_forward::*; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 66 | #[cfg(not(feature = "gpu-forward"))] |
| 67 | type RenderNodeHost = (); |
| 68 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 69 | #[sorted] |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 70 | #[derive(Debug)] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 71 | pub enum Error { |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 72 | AddGpuDeviceMemory(sys_util::Error), |
| 73 | AllocateGpuDeviceAddress, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 74 | BalloonDeviceNew(virtio::BalloonError), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 75 | BlockDeviceNew(sys_util::Error), |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 76 | BlockSignal(sys_util::signal::Error), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 77 | BuildVm(<Arch as LinuxArch>::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 78 | ChownTpmStorage(sys_util::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 79 | CloneEventFd(sys_util::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 80 | CreateCrasClient(libcras::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 81 | CreateEventFd(sys_util::Error), |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 82 | CreatePollContext(sys_util::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 83 | CreateSignalFd(sys_util::SignalFdError), |
| 84 | CreateSocket(io::Error), |
Chirantan Ekbote | 49fa08f | 2018-11-16 13:26:53 -0800 | [diff] [blame] | 85 | CreateTapDevice(NetError), |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 86 | CreateTimerFd(sys_util::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 87 | CreateTpmStorage(PathBuf, io::Error), |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 88 | CreateUsbProvider(devices::usb::host_backend::error::Error), |
Daniel Verkamp | f02fdd1 | 2018-10-10 17:25:14 -0700 | [diff] [blame] | 89 | DetectImageType(qcow::Error), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 90 | DeviceJail(io_jail::Error), |
| 91 | DevicePivotRoot(io_jail::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 92 | Disk(io::Error), |
Stephen Barber | c79de2d | 2018-02-21 14:17:27 -0800 | [diff] [blame] | 93 | DiskImageLock(sys_util::Error), |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 94 | DropCapabilities(sys_util::Error), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 95 | InputDeviceNew(virtio::InputError), |
| 96 | InputEventsOpen(std::io::Error), |
Dylan Reid | 2056644 | 2018-04-02 15:06:15 -0700 | [diff] [blame] | 97 | InvalidFdPath, |
Zach Reizner | 579bd2c | 2018-09-14 15:43:33 -0700 | [diff] [blame] | 98 | InvalidWaylandPath, |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 99 | IoJail(io_jail::Error), |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 100 | LoadKernel(Box<dyn StdError>), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 101 | NetDeviceNew(virtio::NetError), |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 102 | OpenAndroidFstab(PathBuf, io::Error), |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 103 | OpenInitrd(PathBuf, io::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 104 | OpenKernel(PathBuf, io::Error), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 105 | OpenVinput(PathBuf, io::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 106 | P9DeviceNew(virtio::P9Error), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 107 | PivotRootDoesntExist(&'static str), |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 108 | PollContextAdd(sys_util::Error), |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 109 | PollContextDelete(sys_util::Error), |
Dylan Reid | 88624f8 | 2018-01-11 09:20:16 -0800 | [diff] [blame] | 110 | QcowDeviceCreate(qcow::Error), |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 111 | ReadLowmemAvailable(io::Error), |
| 112 | ReadLowmemMargin(io::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 113 | RegisterBalloon(arch::DeviceRegistrationError), |
| 114 | RegisterBlock(arch::DeviceRegistrationError), |
| 115 | RegisterGpu(arch::DeviceRegistrationError), |
| 116 | RegisterNet(arch::DeviceRegistrationError), |
| 117 | RegisterP9(arch::DeviceRegistrationError), |
| 118 | RegisterRng(arch::DeviceRegistrationError), |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 119 | RegisterSignalHandler(sys_util::Error), |
Dylan Reid | 0f579cb | 2018-07-09 15:39:34 -0700 | [diff] [blame] | 120 | RegisterWayland(arch::DeviceRegistrationError), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 121 | ReserveGpuMemory(sys_util::MmapError), |
| 122 | ReserveMemory(sys_util::Error), |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 123 | ResetTimerFd(sys_util::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 124 | RngDeviceNew(virtio::RngError), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 125 | SettingGidMap(io_jail::Error), |
| 126 | SettingUidMap(io_jail::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 127 | SignalFd(sys_util::SignalFdError), |
| 128 | SpawnVcpu(io::Error), |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 129 | TimerFd(sys_util::Error), |
Chirantan Ekbote | 2d29233 | 2018-11-16 11:35:24 -0800 | [diff] [blame] | 130 | ValidateRawFd(sys_util::Error), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 131 | VhostNetDeviceNew(virtio::vhost::Error), |
| 132 | VhostVsockDeviceNew(virtio::vhost::Error), |
Daniel Verkamp | 56f283b | 2018-10-05 11:40:59 -0700 | [diff] [blame] | 133 | VirtioPciDev(sys_util::Error), |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 134 | WaylandDeviceNew(sys_util::Error), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 135 | } |
| 136 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 137 | impl Display for Error { |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 138 | #[remain::check] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 139 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 140 | use self::Error::*; |
| 141 | |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 142 | #[sorted] |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 143 | match self { |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 144 | AddGpuDeviceMemory(e) => write!(f, "failed to add gpu device memory: {}", e), |
| 145 | AllocateGpuDeviceAddress => write!(f, "failed to allocate gpu device guest address"), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 146 | BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), |
| 147 | BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), |
| 148 | BlockSignal(e) => write!(f, "failed to block signal: {}", e), |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 149 | BuildVm(e) => write!(f, "The architecture failed to build the vm: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 150 | ChownTpmStorage(e) => write!(f, "failed to chown tpm storage: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 151 | CloneEventFd(e) => write!(f, "failed to clone eventfd: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 152 | CreateCrasClient(e) => write!(f, "failed to create cras client: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 153 | CreateEventFd(e) => write!(f, "failed to create eventfd: {}", e), |
| 154 | CreatePollContext(e) => write!(f, "failed to create poll context: {}", e), |
| 155 | CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), |
| 156 | CreateSocket(e) => write!(f, "failed to create socket: {}", e), |
| 157 | CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), |
| 158 | CreateTimerFd(e) => write!(f, "failed to create timerfd: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 159 | CreateTpmStorage(p, e) => { |
| 160 | write!(f, "failed to create tpm storage dir {}: {}", p.display(), e) |
| 161 | } |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 162 | CreateUsbProvider(e) => write!(f, "failed to create usb provider: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 163 | DetectImageType(e) => write!(f, "failed to detect disk image type: {}", e), |
| 164 | DeviceJail(e) => write!(f, "failed to jail device: {}", e), |
| 165 | DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), |
| 166 | Disk(e) => write!(f, "failed to load disk image: {}", e), |
| 167 | DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 168 | DropCapabilities(e) => write!(f, "failed to drop process capabilities: {}", e), |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 169 | InputDeviceNew(e) => write!(f, "failed to set up input device: {}", e), |
| 170 | InputEventsOpen(e) => write!(f, "failed to open event device: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 171 | InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), |
| 172 | InvalidWaylandPath => write!(f, "wayland socket path has no parent or file name"), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 173 | IoJail(e) => write!(f, "{}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 174 | LoadKernel(e) => write!(f, "failed to load kernel: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 175 | NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 176 | OpenAndroidFstab(p, e) => write!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 177 | f, |
| 178 | "failed to open android fstab file {}: {}", |
| 179 | p.display(), |
| 180 | e |
| 181 | ), |
David Tolnay | 3df3552 | 2019-03-11 12:36:30 -0700 | [diff] [blame] | 182 | OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), |
| 183 | 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] | 184 | 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] | 185 | P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), |
Lepton Wu | 39133a0 | 2019-02-27 12:42:29 -0800 | [diff] [blame] | 186 | PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 187 | PollContextAdd(e) => write!(f, "failed to add fd to poll context: {}", e), |
| 188 | PollContextDelete(e) => write!(f, "failed to remove fd from poll context: {}", e), |
| 189 | QcowDeviceCreate(e) => write!(f, "failed to read qcow formatted file {}", e), |
| 190 | ReadLowmemAvailable(e) => write!( |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 191 | f, |
| 192 | "failed to read /sys/kernel/mm/chromeos-low_mem/available: {}", |
| 193 | e |
| 194 | ), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 195 | ReadLowmemMargin(e) => write!( |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 196 | f, |
| 197 | "failed to read /sys/kernel/mm/chromeos-low_mem/margin: {}", |
| 198 | e |
| 199 | ), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 200 | RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), |
| 201 | RegisterBlock(e) => write!(f, "error registering block device: {}", e), |
| 202 | RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), |
| 203 | RegisterNet(e) => write!(f, "error registering net device: {}", e), |
| 204 | RegisterP9(e) => write!(f, "error registering 9p device: {}", e), |
| 205 | RegisterRng(e) => write!(f, "error registering rng device: {}", e), |
| 206 | RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), |
| 207 | RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 208 | ReserveGpuMemory(e) => write!(f, "failed to reserve gpu memory: {}", e), |
| 209 | ReserveMemory(e) => write!(f, "failed to reserve memory: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 210 | ResetTimerFd(e) => write!(f, "failed to reset timerfd: {}", e), |
| 211 | RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 212 | SettingGidMap(e) => write!(f, "error setting GID map: {}", e), |
| 213 | SettingUidMap(e) => write!(f, "error setting UID map: {}", e), |
| 214 | SignalFd(e) => write!(f, "failed to read signal fd: {}", e), |
| 215 | SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), |
| 216 | TimerFd(e) => write!(f, "failed to read timer fd: {}", e), |
| 217 | ValidateRawFd(e) => write!(f, "failed to validate raw fd: {}", e), |
| 218 | VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), |
| 219 | VhostVsockDeviceNew(e) => write!(f, "failed to set up virtual socket device: {}", e), |
| 220 | VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), |
| 221 | WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 226 | impl From<io_jail::Error> for Error { |
| 227 | fn from(err: io_jail::Error) -> Self { |
| 228 | Error::IoJail(err) |
| 229 | } |
| 230 | } |
| 231 | |
David Tolnay | c69f975 | 2019-03-01 18:07:56 -0800 | [diff] [blame] | 232 | impl std::error::Error for Error {} |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 233 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 234 | type Result<T> = std::result::Result<T, Error>; |
| 235 | |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 236 | enum TaggedControlSocket { |
| 237 | Vm(VmControlResponseSocket), |
| 238 | Wayland(WlControlResponseSocket), |
| 239 | } |
| 240 | |
| 241 | impl AsRef<UnixSeqpacket> for TaggedControlSocket { |
| 242 | fn as_ref(&self) -> &UnixSeqpacket { |
| 243 | use self::TaggedControlSocket::*; |
| 244 | match &self { |
| 245 | Vm(ref socket) => socket, |
| 246 | Wayland(ref socket) => socket, |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | impl AsRawFd for TaggedControlSocket { |
| 252 | fn as_raw_fd(&self) -> RawFd { |
| 253 | self.as_ref().as_raw_fd() |
| 254 | } |
| 255 | } |
| 256 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 257 | fn create_base_minijail(root: &Path, seccomp_policy: &Path) -> Result<Minijail> { |
| 258 | // All child jails run in a new user namespace without any users mapped, |
| 259 | // they run as nobody unless otherwise configured. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 260 | let mut j = Minijail::new().map_err(Error::DeviceJail)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 261 | j.namespace_pids(); |
| 262 | j.namespace_user(); |
| 263 | j.namespace_user_disable_setgroups(); |
| 264 | // Don't need any capabilities. |
| 265 | j.use_caps(0); |
| 266 | // Create a new mount namespace with an empty root FS. |
| 267 | j.namespace_vfs(); |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 268 | j.enter_pivot_root(root).map_err(Error::DevicePivotRoot)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 269 | // Run in an empty network namespace. |
| 270 | j.namespace_net(); |
| 271 | // Apply the block device seccomp policy. |
| 272 | j.no_new_privs(); |
Stephen Barber | 3b1d8a5 | 2018-01-06 17:34:51 -0800 | [diff] [blame] | 273 | // Use TSYNC only for the side effect of it using SECCOMP_RET_TRAP, which will correctly kill |
| 274 | // the entire device process if a worker thread commits a seccomp violation. |
| 275 | j.set_seccomp_filter_tsync(); |
Zach Reizner | 043ddc5 | 2018-04-03 20:47:21 -0700 | [diff] [blame] | 276 | #[cfg(debug_assertions)] |
| 277 | j.log_seccomp_filter_failures(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 278 | j.parse_seccomp_filters(seccomp_policy) |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 279 | .map_err(Error::DeviceJail)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 280 | j.use_seccomp_filter(); |
| 281 | // Don't do init setup. |
| 282 | j.run_as_init(); |
| 283 | Ok(j) |
| 284 | } |
| 285 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 286 | fn simple_jail(cfg: &Config, policy: &str) -> Result<Option<Minijail>> { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 287 | if cfg.sandbox { |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 288 | let pivot_root: &str = option_env!("DEFAULT_PIVOT_ROOT").unwrap_or("/var/empty"); |
| 289 | // A directory for a jailed device's pivot root. |
| 290 | let root_path = Path::new(pivot_root); |
| 291 | if !root_path.exists() { |
| 292 | return Err(Error::PivotRootDoesntExist(pivot_root)); |
| 293 | } |
| 294 | let policy_path: PathBuf = cfg.seccomp_policy_dir.join(policy); |
| 295 | Ok(Some(create_base_minijail(root_path, &policy_path)?)) |
| 296 | } else { |
| 297 | Ok(None) |
| 298 | } |
| 299 | } |
| 300 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 301 | type DeviceResult<T = VirtioDeviceStub> = std::result::Result<T, Error>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 302 | |
| 303 | fn create_block_device( |
| 304 | cfg: &Config, |
| 305 | disk: &DiskOption, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 306 | disk_device_socket: DiskControlResponseSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 307 | ) -> DeviceResult { |
| 308 | // Special case '/proc/self/fd/*' paths. The FD is already open, just use it. |
| 309 | let raw_image: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) { |
| 310 | // Safe because we will validate |raw_fd|. |
| 311 | unsafe { File::from_raw_fd(raw_fd_from_path(&disk.path)?) } |
| 312 | } else { |
| 313 | OpenOptions::new() |
| 314 | .read(true) |
| 315 | .write(!disk.read_only) |
| 316 | .open(&disk.path) |
| 317 | .map_err(Error::Disk)? |
| 318 | }; |
| 319 | // Lock the disk image to prevent other crosvm instances from using it. |
| 320 | let lock_op = if disk.read_only { |
| 321 | FlockOperation::LockShared |
| 322 | } else { |
| 323 | FlockOperation::LockExclusive |
| 324 | }; |
| 325 | flock(&raw_image, lock_op, true).map_err(Error::DiskImageLock)?; |
| 326 | |
| 327 | let image_type = qcow::detect_image_type(&raw_image).map_err(Error::DetectImageType)?; |
| 328 | let dev = match image_type { |
| 329 | ImageType::Raw => { |
| 330 | // Access as a raw block device. |
| 331 | let dev = virtio::Block::new(raw_image, disk.read_only, Some(disk_device_socket)) |
| 332 | .map_err(Error::BlockDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 333 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 334 | } |
| 335 | ImageType::Qcow2 => { |
| 336 | // Valid qcow header present |
| 337 | let qcow_image = QcowFile::from(raw_image).map_err(Error::QcowDeviceCreate)?; |
| 338 | let dev = virtio::Block::new(qcow_image, disk.read_only, Some(disk_device_socket)) |
| 339 | .map_err(Error::BlockDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 340 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 341 | } |
| 342 | }; |
| 343 | |
| 344 | Ok(VirtioDeviceStub { |
| 345 | dev, |
| 346 | jail: simple_jail(&cfg, "block_device.policy")?, |
| 347 | }) |
| 348 | } |
| 349 | |
| 350 | fn create_rng_device(cfg: &Config) -> DeviceResult { |
| 351 | let dev = virtio::Rng::new().map_err(Error::RngDeviceNew)?; |
| 352 | |
| 353 | Ok(VirtioDeviceStub { |
| 354 | dev: Box::new(dev), |
| 355 | jail: simple_jail(&cfg, "rng_device.policy")?, |
| 356 | }) |
| 357 | } |
| 358 | |
| 359 | #[cfg(feature = "tpm")] |
| 360 | fn create_tpm_device(cfg: &Config) -> DeviceResult { |
| 361 | use std::ffi::CString; |
| 362 | use std::fs; |
| 363 | use std::process; |
| 364 | use sys_util::chown; |
| 365 | |
| 366 | let tpm_storage: PathBuf; |
| 367 | let mut tpm_jail = simple_jail(&cfg, "tpm_device.policy")?; |
| 368 | |
| 369 | match &mut tpm_jail { |
| 370 | Some(jail) => { |
| 371 | // Create a tmpfs in the device's root directory for tpm |
| 372 | // simulator storage. The size is 20*1024, or 20 KB. |
| 373 | jail.mount_with_data( |
| 374 | Path::new("none"), |
| 375 | Path::new("/"), |
| 376 | "tmpfs", |
| 377 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 378 | "size=20480", |
| 379 | )?; |
| 380 | |
| 381 | let crosvm_ids = add_crosvm_user_to_jail(jail, "tpm")?; |
| 382 | |
| 383 | let pid = process::id(); |
| 384 | let tpm_pid_dir = format!("/run/vm/tpm.{}", pid); |
| 385 | tpm_storage = Path::new(&tpm_pid_dir).to_owned(); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 386 | fs::create_dir_all(&tpm_storage) |
| 387 | .map_err(|e| Error::CreateTpmStorage(tpm_storage.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 388 | 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] | 389 | chown(&tpm_pid_dir_c, crosvm_ids.uid, crosvm_ids.gid) |
| 390 | .map_err(Error::ChownTpmStorage)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 391 | |
| 392 | jail.mount_bind(&tpm_storage, &tpm_storage, true)?; |
| 393 | } |
| 394 | None => { |
| 395 | // Path used inside cros_sdk which does not have /run/vm. |
| 396 | tpm_storage = Path::new("/tmp/tpm-simulator").to_owned(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | let dev = virtio::Tpm::new(tpm_storage); |
| 401 | |
| 402 | Ok(VirtioDeviceStub { |
| 403 | dev: Box::new(dev), |
| 404 | jail: tpm_jail, |
| 405 | }) |
| 406 | } |
| 407 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 408 | fn create_single_touch_device(cfg: &Config, single_touch_spec: &TouchDeviceOption) -> DeviceResult { |
| 409 | let socket = create_input_socket(&single_touch_spec.path).map_err(|e| { |
| 410 | error!("failed configuring virtio single touch: {:?}", e); |
| 411 | e |
| 412 | })?; |
| 413 | |
| 414 | let dev = virtio::new_single_touch(socket, single_touch_spec.width, single_touch_spec.height) |
| 415 | .map_err(Error::InputDeviceNew)?; |
| 416 | Ok(VirtioDeviceStub { |
| 417 | dev: Box::new(dev), |
| 418 | jail: simple_jail(&cfg, "input_device.policy")?, |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | fn create_trackpad_device(cfg: &Config, trackpad_spec: &TouchDeviceOption) -> DeviceResult { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 423 | let socket = create_input_socket(&trackpad_spec.path).map_err(|e| { |
| 424 | error!("failed configuring virtio trackpad: {}", e); |
| 425 | e |
| 426 | })?; |
| 427 | |
| 428 | let dev = virtio::new_trackpad(socket, trackpad_spec.width, trackpad_spec.height) |
| 429 | .map_err(Error::InputDeviceNew)?; |
| 430 | |
| 431 | Ok(VirtioDeviceStub { |
| 432 | dev: Box::new(dev), |
| 433 | jail: simple_jail(&cfg, "input_device.policy")?, |
| 434 | }) |
| 435 | } |
| 436 | |
| 437 | fn create_mouse_device(cfg: &Config, mouse_socket: &Path) -> DeviceResult { |
| 438 | let socket = create_input_socket(&mouse_socket).map_err(|e| { |
| 439 | error!("failed configuring virtio mouse: {}", e); |
| 440 | e |
| 441 | })?; |
| 442 | |
| 443 | let dev = virtio::new_mouse(socket).map_err(Error::InputDeviceNew)?; |
| 444 | |
| 445 | Ok(VirtioDeviceStub { |
| 446 | dev: Box::new(dev), |
| 447 | jail: simple_jail(&cfg, "input_device.policy")?, |
| 448 | }) |
| 449 | } |
| 450 | |
| 451 | fn create_keyboard_device(cfg: &Config, keyboard_socket: &Path) -> DeviceResult { |
| 452 | let socket = create_input_socket(&keyboard_socket).map_err(|e| { |
| 453 | error!("failed configuring virtio keyboard: {}", e); |
| 454 | e |
| 455 | })?; |
| 456 | |
| 457 | let dev = virtio::new_keyboard(socket).map_err(Error::InputDeviceNew)?; |
| 458 | |
| 459 | Ok(VirtioDeviceStub { |
| 460 | dev: Box::new(dev), |
| 461 | jail: simple_jail(&cfg, "input_device.policy")?, |
| 462 | }) |
| 463 | } |
| 464 | |
| 465 | fn create_vinput_device(cfg: &Config, dev_path: &Path) -> DeviceResult { |
| 466 | let dev_file = OpenOptions::new() |
| 467 | .read(true) |
| 468 | .write(true) |
| 469 | .open(dev_path) |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 470 | .map_err(|e| Error::OpenVinput(dev_path.to_owned(), e))?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 471 | |
| 472 | let dev = virtio::new_evdev(dev_file).map_err(Error::InputDeviceNew)?; |
| 473 | |
| 474 | Ok(VirtioDeviceStub { |
| 475 | dev: Box::new(dev), |
| 476 | jail: simple_jail(&cfg, "input_device.policy")?, |
| 477 | }) |
| 478 | } |
| 479 | |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 480 | fn create_balloon_device(cfg: &Config, socket: BalloonControlResponseSocket) -> DeviceResult { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 481 | let dev = virtio::Balloon::new(socket).map_err(Error::BalloonDeviceNew)?; |
| 482 | |
| 483 | Ok(VirtioDeviceStub { |
| 484 | dev: Box::new(dev), |
| 485 | jail: simple_jail(&cfg, "balloon_device.policy")?, |
| 486 | }) |
| 487 | } |
| 488 | |
| 489 | fn create_tap_net_device(cfg: &Config, tap_fd: RawFd) -> DeviceResult { |
| 490 | // Safe because we ensure that we get a unique handle to the fd. |
| 491 | let tap = unsafe { |
| 492 | Tap::from_raw_fd(validate_raw_fd(tap_fd).map_err(Error::ValidateRawFd)?) |
| 493 | .map_err(Error::CreateTapDevice)? |
| 494 | }; |
| 495 | |
| 496 | let dev = virtio::Net::from(tap).map_err(Error::NetDeviceNew)?; |
| 497 | |
| 498 | Ok(VirtioDeviceStub { |
| 499 | dev: Box::new(dev), |
| 500 | jail: simple_jail(&cfg, "net_device.policy")?, |
| 501 | }) |
| 502 | } |
| 503 | |
| 504 | fn create_net_device( |
| 505 | cfg: &Config, |
| 506 | host_ip: Ipv4Addr, |
| 507 | netmask: Ipv4Addr, |
| 508 | mac_address: MacAddress, |
| 509 | mem: &GuestMemory, |
| 510 | ) -> DeviceResult { |
| 511 | let dev = if cfg.vhost_net { |
| 512 | let dev = |
| 513 | virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new(host_ip, netmask, mac_address, mem) |
| 514 | .map_err(Error::VhostNetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 515 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 516 | } else { |
| 517 | let dev = |
| 518 | virtio::Net::<Tap>::new(host_ip, netmask, mac_address).map_err(Error::NetDeviceNew)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 519 | Box::new(dev) as Box<dyn VirtioDevice> |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | let policy = if cfg.vhost_net { |
| 523 | "vhost_net_device.policy" |
| 524 | } else { |
| 525 | "net_device.policy" |
| 526 | }; |
| 527 | |
| 528 | Ok(VirtioDeviceStub { |
| 529 | dev, |
| 530 | jail: simple_jail(&cfg, policy)?, |
| 531 | }) |
| 532 | } |
| 533 | |
| 534 | #[cfg(feature = "gpu")] |
| 535 | fn create_gpu_device( |
| 536 | cfg: &Config, |
| 537 | exit_evt: &EventFd, |
| 538 | gpu_socket: virtio::resource_bridge::ResourceResponseSocket, |
| 539 | wayland_socket_path: &Path, |
| 540 | ) -> DeviceResult { |
| 541 | let jailed_wayland_path = Path::new("/wayland-0"); |
| 542 | |
| 543 | let dev = virtio::Gpu::new( |
| 544 | exit_evt.try_clone().map_err(Error::CloneEventFd)?, |
| 545 | Some(gpu_socket), |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 546 | if cfg.sandbox { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 547 | &jailed_wayland_path |
| 548 | } else { |
| 549 | wayland_socket_path |
| 550 | }, |
| 551 | ); |
| 552 | |
| 553 | let jail = match simple_jail(&cfg, "gpu_device.policy")? { |
| 554 | Some(mut jail) => { |
| 555 | // Create a tmpfs in the device's root directory so that we can bind mount the |
| 556 | // dri directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 557 | jail.mount_with_data( |
| 558 | Path::new("none"), |
| 559 | Path::new("/"), |
| 560 | "tmpfs", |
| 561 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 562 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 563 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 564 | |
| 565 | // Device nodes required for DRM. |
| 566 | let sys_dev_char_path = Path::new("/sys/dev/char"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 567 | jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 568 | let sys_devices_path = Path::new("/sys/devices"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 569 | jail.mount_bind(sys_devices_path, sys_devices_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 570 | let drm_dri_path = Path::new("/dev/dri"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 571 | jail.mount_bind(drm_dri_path, drm_dri_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 572 | |
| 573 | // Libraries that are required when mesa drivers are dynamically loaded. |
| 574 | let lib_path = Path::new("/lib64"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 575 | jail.mount_bind(lib_path, lib_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 576 | let usr_lib_path = Path::new("/usr/lib64"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 577 | jail.mount_bind(usr_lib_path, usr_lib_path, false)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 578 | |
| 579 | // Bind mount the wayland socket into jail's root. This is necessary since each |
| 580 | // new wayland context must open() the socket. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 581 | jail.mount_bind(wayland_socket_path, jailed_wayland_path, true)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 582 | |
| 583 | add_crosvm_user_to_jail(&mut jail, "gpu")?; |
| 584 | |
| 585 | Some(jail) |
| 586 | } |
| 587 | None => None, |
| 588 | }; |
| 589 | |
| 590 | Ok(VirtioDeviceStub { |
| 591 | dev: Box::new(dev), |
| 592 | jail, |
| 593 | }) |
| 594 | } |
| 595 | |
| 596 | fn create_wayland_device( |
| 597 | cfg: &Config, |
| 598 | socket_path: &Path, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 599 | socket: WlControlRequestSocket, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 600 | resource_bridge: Option<virtio::resource_bridge::ResourceRequestSocket>, |
| 601 | ) -> DeviceResult { |
| 602 | let wayland_socket_dir = socket_path.parent().ok_or(Error::InvalidWaylandPath)?; |
| 603 | let wayland_socket_name = socket_path.file_name().ok_or(Error::InvalidWaylandPath)?; |
| 604 | let jailed_wayland_dir = Path::new("/wayland"); |
| 605 | let jailed_wayland_path = jailed_wayland_dir.join(wayland_socket_name); |
| 606 | |
| 607 | let dev = virtio::Wl::new( |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 608 | if cfg.sandbox { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 609 | &jailed_wayland_path |
| 610 | } else { |
| 611 | socket_path |
| 612 | }, |
| 613 | socket, |
| 614 | resource_bridge, |
| 615 | ) |
| 616 | .map_err(Error::WaylandDeviceNew)?; |
| 617 | |
| 618 | let jail = match simple_jail(&cfg, "wl_device.policy")? { |
| 619 | Some(mut jail) => { |
| 620 | // Create a tmpfs in the device's root directory so that we can bind mount the wayland |
| 621 | // socket directory into it. The size=67108864 is size=64*1024*1024 or size=64MB. |
| 622 | jail.mount_with_data( |
| 623 | Path::new("none"), |
| 624 | Path::new("/"), |
| 625 | "tmpfs", |
| 626 | (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize, |
| 627 | "size=67108864", |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 628 | )?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 629 | |
| 630 | // Bind mount the wayland socket's directory into jail's root. This is necessary since |
| 631 | // each new wayland context must open() the socket. If the wayland socket is ever |
| 632 | // destroyed and remade in the same host directory, new connections will be possible |
| 633 | // without restarting the wayland device. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 634 | jail.mount_bind(wayland_socket_dir, jailed_wayland_dir, true)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 635 | |
| 636 | add_crosvm_user_to_jail(&mut jail, "Wayland")?; |
| 637 | |
| 638 | Some(jail) |
| 639 | } |
| 640 | None => None, |
| 641 | }; |
| 642 | |
| 643 | Ok(VirtioDeviceStub { |
| 644 | dev: Box::new(dev), |
| 645 | jail, |
| 646 | }) |
| 647 | } |
| 648 | |
| 649 | fn create_vhost_vsock_device(cfg: &Config, cid: u64, mem: &GuestMemory) -> DeviceResult { |
| 650 | let dev = virtio::vhost::Vsock::new(cid, mem).map_err(Error::VhostVsockDeviceNew)?; |
| 651 | |
| 652 | Ok(VirtioDeviceStub { |
| 653 | dev: Box::new(dev), |
| 654 | jail: simple_jail(&cfg, "vhost_vsock_device.policy")?, |
| 655 | }) |
| 656 | } |
| 657 | |
| 658 | fn create_9p_device(cfg: &Config, chronos: Ids, src: &Path, tag: &str) -> DeviceResult { |
| 659 | let (jail, root) = match simple_jail(&cfg, "9p_device.policy")? { |
| 660 | Some(mut jail) => { |
| 661 | // The shared directory becomes the root of the device's file system. |
| 662 | let root = Path::new("/"); |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 663 | jail.mount_bind(src, root, true)?; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 664 | |
| 665 | // Set the uid/gid for the jailed process, and give a basic id map. This |
| 666 | // is required for the above bind mount to work. |
| 667 | jail.change_uid(chronos.uid); |
| 668 | jail.change_gid(chronos.gid); |
| 669 | jail.uidmap(&format!("{0} {0} 1", chronos.uid)) |
| 670 | .map_err(Error::SettingUidMap)?; |
| 671 | jail.gidmap(&format!("{0} {0} 1", chronos.gid)) |
| 672 | .map_err(Error::SettingGidMap)?; |
| 673 | |
| 674 | (Some(jail), root) |
| 675 | } |
| 676 | None => { |
| 677 | // There's no bind mount so we tell the server to treat the source directory as the |
David Tolnay | 9deb7d7 | 2019-03-05 18:25:44 -0800 | [diff] [blame] | 678 | // root. |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 679 | (None, src) |
| 680 | } |
| 681 | }; |
| 682 | |
| 683 | let dev = virtio::P9::new(root, tag).map_err(Error::P9DeviceNew)?; |
| 684 | |
| 685 | Ok(VirtioDeviceStub { |
| 686 | dev: Box::new(dev), |
| 687 | jail, |
| 688 | }) |
| 689 | } |
| 690 | |
| 691 | fn create_virtio_devices( |
| 692 | cfg: &Config, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 693 | mem: &GuestMemory, |
| 694 | _exit_evt: &EventFd, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 695 | wayland_device_socket: WlControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 696 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 697 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 698 | ) -> DeviceResult<Vec<VirtioDeviceStub>> { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 699 | let mut devs = Vec::new(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 700 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 701 | for disk in &cfg.disks { |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 702 | let disk_device_socket = disk_device_sockets.remove(0); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 703 | devs.push(create_block_device(cfg, disk, disk_device_socket)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 704 | } |
| 705 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 706 | devs.push(create_rng_device(cfg)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 707 | |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 708 | #[cfg(feature = "tpm")] |
| 709 | { |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 710 | if cfg.software_tpm { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 711 | devs.push(create_tpm_device(cfg)?); |
David Tolnay | 43f8e21 | 2019-02-13 17:28:16 -0800 | [diff] [blame] | 712 | } |
David Tolnay | de6b29a | 2018-12-20 11:49:46 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Jorge E. Moreira | 99d3f08 | 2019-03-07 10:59:54 -0800 | [diff] [blame] | 715 | if let Some(single_touch_spec) = &cfg.virtio_single_touch { |
| 716 | devs.push(create_single_touch_device(cfg, single_touch_spec)?); |
| 717 | } |
| 718 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 719 | if let Some(trackpad_spec) = &cfg.virtio_trackpad { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 720 | devs.push(create_trackpad_device(cfg, trackpad_spec)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 723 | if let Some(mouse_socket) = &cfg.virtio_mouse { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 724 | devs.push(create_mouse_device(cfg, mouse_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 727 | if let Some(keyboard_socket) = &cfg.virtio_keyboard { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 728 | devs.push(create_keyboard_device(cfg, keyboard_socket)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Jianxun Zhang | 8f4d768 | 2019-02-21 12:55:31 -0800 | [diff] [blame] | 731 | for dev_path in &cfg.virtio_input_evdevs { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 732 | devs.push(create_vinput_device(cfg, dev_path)?); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 733 | } |
| 734 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 735 | devs.push(create_balloon_device(cfg, balloon_device_socket)?); |
Dylan Reid | 295ccac | 2017-11-06 14:06:24 -0800 | [diff] [blame] | 736 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 737 | // 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] | 738 | for tap_fd in &cfg.tap_fd { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 739 | devs.push(create_tap_net_device(cfg, *tap_fd)?); |
Jorge E. Moreira | b795280 | 2019-02-12 16:43:05 -0800 | [diff] [blame] | 740 | } |
| 741 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 742 | if let (Some(host_ip), Some(netmask), Some(mac_address)) = |
| 743 | (cfg.host_ip, cfg.netmask, cfg.mac_address) |
| 744 | { |
| 745 | devs.push(create_net_device(cfg, host_ip, netmask, mac_address, mem)?); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 746 | } |
| 747 | |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 748 | #[cfg_attr(not(feature = "gpu"), allow(unused_mut))] |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 749 | let mut resource_bridge_wl_socket = None::<virtio::resource_bridge::ResourceRequestSocket>; |
David Tolnay | fa70171 | 2019-02-13 16:42:54 -0800 | [diff] [blame] | 750 | |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 751 | #[cfg(feature = "gpu")] |
| 752 | { |
| 753 | if cfg.gpu { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 754 | if let Some(wayland_socket_path) = &cfg.wayland_socket_path { |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 755 | let (wl_socket, gpu_socket) = |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 756 | virtio::resource_bridge::pair().map_err(Error::CreateSocket)?; |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 757 | resource_bridge_wl_socket = Some(wl_socket); |
| 758 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 759 | devs.push(create_gpu_device( |
| 760 | cfg, |
| 761 | _exit_evt, |
| 762 | gpu_socket, |
| 763 | wayland_socket_path, |
| 764 | )?); |
David Riley | b22b613 | 2018-08-20 08:11:42 -0700 | [diff] [blame] | 765 | } |
Zach Reizner | 3a8100a | 2017-09-13 19:15:43 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 769 | if let Some(wayland_socket_path) = cfg.wayland_socket_path.as_ref() { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 770 | devs.push(create_wayland_device( |
| 771 | cfg, |
| 772 | wayland_socket_path, |
| 773 | wayland_device_socket, |
| 774 | resource_bridge_wl_socket, |
| 775 | )?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if let Some(cid) = cfg.cid { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 779 | devs.push(create_vhost_vsock_device(cfg, cid, mem)?); |
Zach Reizner | aa57566 | 2018-08-15 10:46:32 -0700 | [diff] [blame] | 780 | } |
| 781 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 782 | let chronos = get_chronos_ids(); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 783 | |
| 784 | for (src, tag) in &cfg.shared_dirs { |
| 785 | devs.push(create_9p_device(cfg, chronos, src, tag)?); |
| 786 | } |
| 787 | |
| 788 | Ok(devs) |
| 789 | } |
| 790 | |
| 791 | fn create_devices( |
| 792 | cfg: Config, |
| 793 | mem: &GuestMemory, |
| 794 | exit_evt: &EventFd, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 795 | wayland_device_socket: WlControlRequestSocket, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 796 | balloon_device_socket: BalloonControlResponseSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 797 | disk_device_sockets: &mut Vec<DiskControlResponseSocket>, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 798 | usb_provider: HostBackendDeviceProvider, |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 799 | ) -> DeviceResult<Vec<(Box<dyn PciDevice>, Option<Minijail>)>> { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 800 | let stubs = create_virtio_devices( |
| 801 | &cfg, |
| 802 | mem, |
| 803 | exit_evt, |
| 804 | wayland_device_socket, |
| 805 | balloon_device_socket, |
| 806 | disk_device_sockets, |
| 807 | )?; |
| 808 | |
| 809 | let mut pci_devices = Vec::new(); |
| 810 | |
| 811 | for stub in stubs { |
| 812 | let dev = VirtioPciDevice::new(mem.clone(), stub.dev).map_err(Error::VirtioPciDev)?; |
David Tolnay | fdac5ed | 2019-03-08 16:56:14 -0800 | [diff] [blame] | 813 | let dev = Box::new(dev) as Box<dyn PciDevice>; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 814 | pci_devices.push((dev, stub.jail)); |
| 815 | } |
| 816 | |
| 817 | if cfg.cras_audio { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 818 | let server = Box::new(CrasClient::new().map_err(Error::CreateCrasClient)?); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 819 | let cras_audio = devices::Ac97Dev::new(mem.clone(), server); |
| 820 | |
| 821 | pci_devices.push(( |
| 822 | Box::new(cras_audio), |
| 823 | simple_jail(&cfg, "cras_audio_device.policy")?, |
| 824 | )); |
| 825 | } |
| 826 | |
| 827 | if cfg.null_audio { |
| 828 | let server = Box::new(DummyStreamSource::new()); |
| 829 | let null_audio = devices::Ac97Dev::new(mem.clone(), server); |
| 830 | |
| 831 | pci_devices.push(( |
| 832 | Box::new(null_audio), |
| 833 | simple_jail(&cfg, "null_audio_device.policy")?, |
| 834 | )); |
| 835 | } |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 836 | // Create xhci controller. |
| 837 | let usb_controller = Box::new(XhciController::new(mem.clone(), usb_provider)); |
| 838 | pci_devices.push((usb_controller, simple_jail(&cfg, "xhci.policy")?)); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 839 | |
| 840 | Ok(pci_devices) |
| 841 | } |
| 842 | |
| 843 | #[derive(Copy, Clone)] |
| 844 | struct Ids { |
| 845 | uid: uid_t, |
| 846 | gid: gid_t, |
| 847 | } |
| 848 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 849 | fn get_chronos_ids() -> Ids { |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 850 | let chronos_user_group = CStr::from_bytes_with_nul(b"chronos\0").unwrap(); |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 851 | |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 852 | let chronos_uid = match get_user_id(&chronos_user_group) { |
| 853 | Ok(u) => u, |
| 854 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 855 | warn!("falling back to current user id for 9p: {}", e); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 856 | geteuid() |
| 857 | } |
| 858 | }; |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 859 | |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 860 | let chronos_gid = match get_group_id(&chronos_user_group) { |
| 861 | Ok(u) => u, |
| 862 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 863 | warn!("falling back to current group id for 9p: {}", e); |
Chirantan Ekbote | ebd5681 | 2018-04-16 19:32:04 -0700 | [diff] [blame] | 864 | getegid() |
| 865 | } |
| 866 | }; |
| 867 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 868 | Ids { |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 869 | uid: chronos_uid, |
| 870 | gid: chronos_gid, |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 871 | } |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 872 | } |
| 873 | |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 874 | // Set the uid/gid for the jailed process and give a basic id map. This is |
| 875 | // required for bind mounts to work. |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 876 | 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] | 877 | let crosvm_user_group = CStr::from_bytes_with_nul(b"crosvm\0").unwrap(); |
| 878 | |
| 879 | let crosvm_uid = match get_user_id(&crosvm_user_group) { |
| 880 | Ok(u) => u, |
| 881 | Err(e) => { |
| 882 | warn!("falling back to current user id for {}: {}", feature, e); |
| 883 | geteuid() |
| 884 | } |
| 885 | }; |
| 886 | |
| 887 | let crosvm_gid = match get_group_id(&crosvm_user_group) { |
| 888 | Ok(u) => u, |
| 889 | Err(e) => { |
| 890 | warn!("falling back to current group id for {}: {}", feature, e); |
| 891 | getegid() |
| 892 | } |
| 893 | }; |
| 894 | |
| 895 | jail.change_uid(crosvm_uid); |
| 896 | jail.change_gid(crosvm_gid); |
| 897 | jail.uidmap(&format!("{0} {0} 1", crosvm_uid)) |
| 898 | .map_err(Error::SettingUidMap)?; |
| 899 | jail.gidmap(&format!("{0} {0} 1", crosvm_gid)) |
| 900 | .map_err(Error::SettingGidMap)?; |
| 901 | |
David Tolnay | 41a6f84 | 2019-03-01 16:18:44 -0800 | [diff] [blame] | 902 | Ok(Ids { |
| 903 | uid: crosvm_uid, |
| 904 | gid: crosvm_gid, |
| 905 | }) |
David Tolnay | 48c4829 | 2019-03-01 16:54:25 -0800 | [diff] [blame] | 906 | } |
| 907 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 908 | fn raw_fd_from_path(path: &Path) -> Result<RawFd> { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 909 | if !path.is_file() { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 910 | return Err(Error::InvalidFdPath); |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 911 | } |
| 912 | let raw_fd = path |
| 913 | .file_name() |
| 914 | .and_then(|fd_osstr| fd_osstr.to_str()) |
| 915 | .and_then(|fd_str| fd_str.parse::<c_int>().ok()) |
| 916 | .ok_or(Error::InvalidFdPath)?; |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 917 | validate_raw_fd(raw_fd).map_err(Error::ValidateRawFd) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 918 | } |
| 919 | |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 920 | fn create_input_socket(path: &Path) -> Result<UnixStream> { |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 921 | if path.parent() == Some(Path::new("/proc/self/fd")) { |
| 922 | // Safe because we will validate |raw_fd|. |
| 923 | unsafe { Ok(UnixStream::from_raw_fd(raw_fd_from_path(path)?)) } |
| 924 | } else { |
David Tolnay | fd0971d | 2019-03-04 17:15:57 -0800 | [diff] [blame] | 925 | UnixStream::connect(path).map_err(Error::InputEventsOpen) |
Jorge E. Moreira | dffec50 | 2019-01-14 18:44:49 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 929 | fn setup_vcpu_signal_handler() -> Result<()> { |
| 930 | unsafe { |
| 931 | extern "C" fn handle_signal() {} |
| 932 | // Our signal handler does nothing and is trivially async signal safe. |
| 933 | register_signal_handler(SIGRTMIN() + 0, handle_signal) |
| 934 | .map_err(Error::RegisterSignalHandler)?; |
| 935 | } |
| 936 | block_signal(SIGRTMIN() + 0).map_err(Error::BlockSignal)?; |
| 937 | Ok(()) |
| 938 | } |
| 939 | |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 940 | #[derive(Default)] |
| 941 | struct VcpuRunMode { |
| 942 | mtx: Mutex<VmRunMode>, |
| 943 | cvar: Condvar, |
| 944 | } |
| 945 | |
| 946 | impl VcpuRunMode { |
| 947 | fn set_and_notify(&self, new_mode: VmRunMode) { |
| 948 | *self.mtx.lock() = new_mode; |
| 949 | self.cvar.notify_all(); |
| 950 | } |
| 951 | } |
| 952 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 953 | fn run_vcpu( |
| 954 | vcpu: Vcpu, |
| 955 | cpu_id: u32, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 956 | vcpu_affinity: Vec<usize>, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 957 | start_barrier: Arc<Barrier>, |
| 958 | io_bus: devices::Bus, |
| 959 | mmio_bus: devices::Bus, |
| 960 | exit_evt: EventFd, |
Zach Reizner | 795355a | 2019-01-16 17:37:57 -0800 | [diff] [blame] | 961 | requires_kvmclock_ctrl: bool, |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 962 | run_mode_arc: Arc<VcpuRunMode>, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 963 | ) -> Result<JoinHandle<()>> { |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 964 | thread::Builder::new() |
| 965 | .name(format!("crosvm_vcpu{}", cpu_id)) |
| 966 | .spawn(move || { |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 967 | if vcpu_affinity.len() != 0 { |
| 968 | if let Err(e) = set_cpu_affinity(vcpu_affinity) { |
| 969 | error!("Failed to set CPU affinity: {}", e); |
| 970 | } |
| 971 | } |
| 972 | |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 973 | let mut sig_ok = true; |
| 974 | match get_blocked_signals() { |
| 975 | Ok(mut v) => { |
| 976 | v.retain(|&x| x != SIGRTMIN() + 0); |
| 977 | if let Err(e) = vcpu.set_signal_mask(&v) { |
| 978 | error!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 979 | "Failed to set the KVM_SIGNAL_MASK for vcpu {} : {}", |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 980 | cpu_id, e |
| 981 | ); |
| 982 | sig_ok = false; |
| 983 | } |
| 984 | } |
| 985 | Err(e) => { |
| 986 | error!( |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 987 | "Failed to retrieve signal mask for vcpu {} : {}", |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 988 | cpu_id, e |
| 989 | ); |
| 990 | sig_ok = false; |
| 991 | } |
| 992 | }; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 993 | |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 994 | start_barrier.wait(); |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 995 | |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 996 | if sig_ok { |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 997 | 'vcpu_loop: loop { |
| 998 | let mut interrupted_by_signal = false; |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 999 | match vcpu.run() { |
| 1000 | Ok(VcpuExit::IoIn { port, mut size }) => { |
| 1001 | let mut data = [0; 8]; |
| 1002 | if size > data.len() { |
| 1003 | error!("unsupported IoIn size of {} bytes", size); |
| 1004 | size = data.len(); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1005 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1006 | io_bus.read(port as u64, &mut data[..size]); |
| 1007 | if let Err(e) = vcpu.set_data(&data[..size]) { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1008 | error!("failed to set return data for IoIn: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1009 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1010 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1011 | Ok(VcpuExit::IoOut { |
| 1012 | port, |
| 1013 | mut size, |
| 1014 | data, |
| 1015 | }) => { |
| 1016 | if size > data.len() { |
| 1017 | error!("unsupported IoOut size of {} bytes", size); |
| 1018 | size = data.len(); |
| 1019 | } |
| 1020 | io_bus.write(port as u64, &data[..size]); |
| 1021 | } |
| 1022 | Ok(VcpuExit::MmioRead { address, size }) => { |
| 1023 | let mut data = [0; 8]; |
| 1024 | mmio_bus.read(address, &mut data[..size]); |
| 1025 | // Setting data for mmio can not fail. |
| 1026 | let _ = vcpu.set_data(&data[..size]); |
| 1027 | } |
| 1028 | Ok(VcpuExit::MmioWrite { |
| 1029 | address, |
| 1030 | size, |
| 1031 | data, |
| 1032 | }) => { |
| 1033 | mmio_bus.write(address, &data[..size]); |
| 1034 | } |
| 1035 | Ok(VcpuExit::Hlt) => break, |
| 1036 | Ok(VcpuExit::Shutdown) => break, |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1037 | Ok(VcpuExit::SystemEvent(_, _)) => break, |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1038 | Ok(r) => warn!("unexpected vcpu exit: {:?}", r), |
| 1039 | Err(e) => match e.errno() { |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1040 | libc::EINTR => interrupted_by_signal = true, |
| 1041 | libc::EAGAIN => {} |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1042 | _ => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1043 | error!("vcpu hit unknown error: {}", e); |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1044 | break; |
| 1045 | } |
| 1046 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1047 | } |
Mark Ryan | 6ed5aea | 2018-04-20 13:52:35 +0100 | [diff] [blame] | 1048 | |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1049 | if interrupted_by_signal { |
| 1050 | // Try to clear the signal that we use to kick VCPU if it is pending before |
| 1051 | // attempting to handle pause requests. |
| 1052 | if let Err(e) = clear_signal(SIGRTMIN() + 0) { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1053 | error!("failed to clear pending signal: {}", e); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | let mut run_mode_lock = run_mode_arc.mtx.lock(); |
| 1057 | loop { |
| 1058 | match *run_mode_lock { |
| 1059 | VmRunMode::Running => break, |
Zach Reizner | 795355a | 2019-01-16 17:37:57 -0800 | [diff] [blame] | 1060 | VmRunMode::Suspending => { |
| 1061 | // On KVM implementations that use a paravirtualized clock (e.g. |
| 1062 | // x86), a flag must be set to indicate to the guest kernel that |
| 1063 | // a VCPU was suspended. The guest kernel will use this flag to |
| 1064 | // prevent the soft lockup detection from triggering when this |
| 1065 | // VCPU resumes, which could happen days later in realtime. |
| 1066 | if requires_kvmclock_ctrl { |
| 1067 | if let Err(e) = vcpu.kvmclock_ctrl() { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1068 | error!("failed to signal to kvm that vcpu {} is being suspended: {}", cpu_id, e); |
Zach Reizner | 795355a | 2019-01-16 17:37:57 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1072 | VmRunMode::Exiting => break 'vcpu_loop, |
| 1073 | } |
| 1074 | // Give ownership of our exclusive lock to the condition variable that |
| 1075 | // will block. When the condition variable is notified, `wait` will |
| 1076 | // unblock and return a new exclusive lock. |
| 1077 | run_mode_lock = run_mode_arc.cvar.wait(run_mode_lock); |
| 1078 | } |
| 1079 | } |
David Tolnay | 8f3a232 | 2018-11-30 17:11:35 -0800 | [diff] [blame] | 1080 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1081 | } |
Zach Reizner | 8fb5211 | 2017-12-13 16:04:39 -0800 | [diff] [blame] | 1082 | exit_evt |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1083 | .write(1) |
| 1084 | .expect("failed to signal vcpu exit eventfd"); |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 1085 | }) |
| 1086 | .map_err(Error::SpawnVcpu) |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame^] | 1089 | // Reads the contents of a file and converts the space-separated fields into a Vec of u64s. |
| 1090 | // Returns an error if any of the fields fail to parse. |
| 1091 | fn file_fields_to_u64<P: AsRef<Path>>(path: P) -> io::Result<Vec<u64>> { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1092 | let mut file = File::open(path)?; |
| 1093 | |
| 1094 | let mut buf = [0u8; 32]; |
| 1095 | let count = file.read(&mut buf)?; |
| 1096 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1097 | let content = |
| 1098 | str::from_utf8(&buf[..count]).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 1099 | content |
| 1100 | .trim() |
Sonny Rao | d5f6608 | 2019-04-24 12:24:38 -0700 | [diff] [blame^] | 1101 | .split_whitespace() |
| 1102 | .map(|x| { |
| 1103 | x.parse::<u64>() |
| 1104 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 1105 | }) |
| 1106 | .collect() |
| 1107 | } |
| 1108 | |
| 1109 | // Reads the contents of a file and converts them into a u64, and if there |
| 1110 | // are multiple fields it only returns the first one. |
| 1111 | fn file_to_u64<P: AsRef<Path>>(path: P) -> io::Result<u64> { |
| 1112 | file_fields_to_u64(path)? |
| 1113 | .into_iter() |
| 1114 | .next() |
| 1115 | .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty file")) |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1118 | pub fn run_config(cfg: Config) -> Result<()> { |
Lepton Wu | 9105e9f | 2019-03-14 11:38:31 -0700 | [diff] [blame] | 1119 | if cfg.sandbox { |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1120 | // Printing something to the syslog before entering minijail so that libc's syslogger has a |
| 1121 | // chance to open files necessary for its operation, like `/etc/localtime`. After jailing, |
| 1122 | // access to those files will not be possible. |
| 1123 | info!("crosvm entering multiprocess mode"); |
| 1124 | } |
| 1125 | |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1126 | let (usb_control_socket, usb_provider) = |
David Tolnay | 5fb3f51 | 2019-04-12 19:22:33 -0700 | [diff] [blame] | 1127 | HostBackendDeviceProvider::new().map_err(Error::CreateUsbProvider)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1128 | // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this |
| 1129 | // before any jailed devices have been spawned, so that we can catch any of them that fail very |
| 1130 | // quickly. |
| 1131 | let sigchld_fd = SignalFd::new(libc::SIGCHLD).map_err(Error::CreateSignalFd)?; |
| 1132 | |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1133 | let initrd_image = if let Some(initrd_path) = &cfg.initrd_path { |
| 1134 | 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] | 1135 | } else { |
| 1136 | None |
| 1137 | }; |
| 1138 | |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1139 | let components = VmComponents { |
Jakub Staron | f55f75d | 2019-04-26 11:22:51 -0700 | [diff] [blame] | 1140 | memory_size: (cfg.memory.unwrap_or(256) << 20) as u64, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1141 | vcpu_count: cfg.vcpu_count.unwrap_or(1), |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1142 | vcpu_affinity: cfg.vcpu_affinity.clone(), |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1143 | kernel_image: File::open(&cfg.kernel_path) |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1144 | .map_err(|e| Error::OpenKernel(cfg.kernel_path.clone(), e))?, |
Tristan Muntsinger | 4133b01 | 2018-12-21 16:01:56 -0800 | [diff] [blame] | 1145 | android_fstab: cfg |
| 1146 | .android_fstab |
| 1147 | .as_ref() |
David Tolnay | 2b089fc | 2019-03-04 15:33:22 -0800 | [diff] [blame] | 1148 | .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] | 1149 | .map_or(Ok(None), |v| v.map(Some))?, |
Daniel Verkamp | e403f5c | 2018-12-11 16:29:26 -0800 | [diff] [blame] | 1150 | initrd_image, |
Daniel Verkamp | aac2813 | 2018-10-15 14:58:48 -0700 | [diff] [blame] | 1151 | extra_kernel_params: cfg.params.clone(), |
| 1152 | wayland_dmabuf: cfg.wayland_dmabuf, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1153 | }; |
| 1154 | |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1155 | let control_server_socket = match &cfg.socket_path { |
| 1156 | Some(path) => Some(UnlinkUnixSeqpacketListener( |
| 1157 | UnixSeqpacketListener::bind(path).map_err(Error::CreateSocket)?, |
| 1158 | )), |
| 1159 | None => None, |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1160 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1161 | |
| 1162 | let mut control_sockets = Vec::new(); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1163 | let (wayland_host_socket, wayland_device_socket) = |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1164 | msg_socket::pair::<WlDriverResponse, WlDriverRequest>().map_err(Error::CreateSocket)?; |
| 1165 | control_sockets.push(TaggedControlSocket::Wayland(wayland_host_socket)); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1166 | // 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] | 1167 | let (balloon_host_socket, balloon_device_socket) = |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1168 | msg_socket::pair::<BalloonControlCommand, ()>().map_err(Error::CreateSocket)?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1169 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1170 | // Create one control socket per disk. |
| 1171 | let mut disk_device_sockets = Vec::new(); |
| 1172 | let mut disk_host_sockets = Vec::new(); |
| 1173 | let disk_count = cfg.disks.len(); |
| 1174 | for _ in 0..disk_count { |
| 1175 | let (disk_host_socket, disk_device_socket) = |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1176 | msg_socket::pair::<DiskControlCommand, DiskControlResult>() |
| 1177 | .map_err(Error::CreateSocket)?; |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1178 | disk_host_sockets.push(disk_host_socket); |
Jakub Staron | e7c5905 | 2019-04-09 12:31:14 -0700 | [diff] [blame] | 1179 | disk_device_sockets.push(disk_device_socket); |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1182 | let sandbox = cfg.sandbox; |
Miriam Zimmerman | 26ac928 | 2019-01-29 21:21:48 -0800 | [diff] [blame] | 1183 | let linux = Arch::build_vm(components, cfg.split_irqchip, |m, e| { |
Jianxun Zhang | 96f2d8e | 2019-02-20 13:50:42 -0800 | [diff] [blame] | 1184 | create_devices( |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1185 | cfg, |
| 1186 | m, |
| 1187 | e, |
| 1188 | wayland_device_socket, |
| 1189 | balloon_device_socket, |
| 1190 | &mut disk_device_sockets, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1191 | usb_provider, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1192 | ) |
David Tolnay | 2bac1e7 | 2018-12-12 14:33:42 -0800 | [diff] [blame] | 1193 | }) |
David Tolnay | be03426 | 2019-03-04 17:48:36 -0800 | [diff] [blame] | 1194 | .map_err(Error::BuildVm)?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 1195 | |
| 1196 | let _render_node_host = (); |
| 1197 | #[cfg(feature = "gpu-forward")] |
| 1198 | let (_render_node_host, linux) = { |
| 1199 | // Rebinds linux as mutable. |
| 1200 | let mut linux = linux; |
| 1201 | |
| 1202 | // Reserve memory range for GPU buffer allocation in advance to bypass region count |
| 1203 | // limitation. We use mremap/MAP_FIXED later to make sure GPU buffers fall into this range. |
| 1204 | let gpu_mmap = |
| 1205 | MemoryMapping::new_protection(RENDER_NODE_HOST_SIZE as usize, Protection::none()) |
| 1206 | .map_err(Error::ReserveGpuMemory)?; |
| 1207 | |
| 1208 | // Put the non-accessible memory map into device memory so that no other devices use that |
| 1209 | // guest address space. |
| 1210 | let gpu_addr = linux |
| 1211 | .resources |
Daniel Prilik | d92f81a | 2019-03-26 14:28:19 -0700 | [diff] [blame] | 1212 | .device_allocator() |
| 1213 | .allocate( |
| 1214 | RENDER_NODE_HOST_SIZE, |
| 1215 | Alloc::GpuRenderNode, |
| 1216 | "gpu_render_node".to_string(), |
| 1217 | ) |
| 1218 | .map_err(|_| Error::AllocateGpuDeviceAddress)?; |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 1219 | |
| 1220 | let host = RenderNodeHost::start(&gpu_mmap, gpu_addr, linux.vm.get_memory().clone()); |
| 1221 | |
| 1222 | // Makes the gpu memory accessible at allocated address. |
| 1223 | linux |
| 1224 | .vm |
| 1225 | .add_device_memory( |
| 1226 | GuestAddress(gpu_addr), |
| 1227 | gpu_mmap, |
| 1228 | /* read_only = */ false, |
| 1229 | /* log_dirty_pages = */ false, |
| 1230 | ) |
| 1231 | .map_err(Error::AddGpuDeviceMemory)?; |
| 1232 | (host, linux) |
| 1233 | }; |
| 1234 | |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1235 | run_control( |
| 1236 | linux, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1237 | control_server_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1238 | control_sockets, |
| 1239 | balloon_host_socket, |
| 1240 | &disk_host_sockets, |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1241 | usb_control_socket, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1242 | sigchld_fd, |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 1243 | _render_node_host, |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1244 | sandbox, |
Daniel Verkamp | 92f73d7 | 2018-12-04 13:17:46 -0800 | [diff] [blame] | 1245 | ) |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1248 | fn run_control( |
| 1249 | mut linux: RunnableLinuxVm, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1250 | control_server_socket: Option<UnlinkUnixSeqpacketListener>, |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1251 | mut control_sockets: Vec<TaggedControlSocket>, |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1252 | balloon_host_socket: BalloonControlRequestSocket, |
Jakub Staron | ecf81e0 | 2019-04-11 11:43:39 -0700 | [diff] [blame] | 1253 | disk_host_sockets: &[DiskControlRequestSocket], |
Jingkui Wang | 100e6e4 | 2019-03-08 20:41:57 -0800 | [diff] [blame] | 1254 | usb_control_socket: UsbControlSocket, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1255 | sigchld_fd: SignalFd, |
Lepton Wu | 6089388 | 2018-11-21 11:06:18 -0800 | [diff] [blame] | 1256 | _render_node_host: RenderNodeHost, |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1257 | sandbox: bool, |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1258 | ) -> Result<()> { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1259 | // Paths to get the currently available memory and the low memory threshold. |
David Tolnay | 5bbbf61 | 2018-12-01 17:49:30 -0800 | [diff] [blame] | 1260 | const LOWMEM_MARGIN: &str = "/sys/kernel/mm/chromeos-low_mem/margin"; |
| 1261 | const LOWMEM_AVAILABLE: &str = "/sys/kernel/mm/chromeos-low_mem/available"; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1262 | |
| 1263 | // The amount of additional memory to claim back from the VM whenever the system is |
| 1264 | // low on memory. |
| 1265 | const ONE_GB: u64 = (1 << 30); |
| 1266 | |
Dylan Reid | 0ed91ab | 2018-05-31 15:42:18 -0700 | [diff] [blame] | 1267 | let max_balloon_memory = match linux.vm.get_memory().memory_size() { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1268 | // If the VM has at least 1.5 GB, the balloon driver can consume all but the last 1 GB. |
| 1269 | n if n >= (ONE_GB / 2) * 3 => n - ONE_GB, |
| 1270 | // Otherwise, if the VM has at least 500MB the balloon driver will consume at most |
| 1271 | // half of it. |
| 1272 | n if n >= (ONE_GB / 2) => n / 2, |
| 1273 | // Otherwise, the VM is too small for us to take memory away from it. |
| 1274 | _ => 0, |
| 1275 | }; |
| 1276 | let mut current_balloon_memory: u64 = 0; |
| 1277 | let balloon_memory_increment: u64 = max_balloon_memory / 16; |
| 1278 | |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1279 | #[derive(PollToken)] |
| 1280 | enum Token { |
| 1281 | Exit, |
| 1282 | Stdin, |
| 1283 | ChildSignal, |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1284 | CheckAvailableMemory, |
| 1285 | LowMemory, |
| 1286 | LowmemTimer, |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1287 | VmControlServer, |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1288 | VmControl { index: usize }, |
| 1289 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1290 | |
| 1291 | let stdin_handle = stdin(); |
| 1292 | let stdin_lock = stdin_handle.lock(); |
| 1293 | stdin_lock |
| 1294 | .set_raw_mode() |
| 1295 | .expect("failed to set terminal raw mode"); |
| 1296 | |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1297 | let poll_ctx = PollContext::new().map_err(Error::CreatePollContext)?; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1298 | poll_ctx |
| 1299 | .add(&linux.exit_evt, Token::Exit) |
| 1300 | .map_err(Error::PollContextAdd)?; |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1301 | if let Err(e) = poll_ctx.add(&stdin_handle, Token::Stdin) { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1302 | warn!("failed to add stdin to poll context: {}", e); |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1303 | } |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1304 | poll_ctx |
| 1305 | .add(&sigchld_fd, Token::ChildSignal) |
| 1306 | .map_err(Error::PollContextAdd)?; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1307 | |
| 1308 | if let Some(socket_server) = &control_server_socket { |
| 1309 | poll_ctx |
| 1310 | .add(socket_server, Token::VmControlServer) |
| 1311 | .map_err(Error::PollContextAdd)?; |
| 1312 | } |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1313 | for (index, socket) in control_sockets.iter().enumerate() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1314 | poll_ctx |
| 1315 | .add(socket.as_ref(), Token::VmControl { index }) |
| 1316 | .map_err(Error::PollContextAdd)?; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1319 | // Watch for low memory notifications and take memory back from the VM. |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1320 | let low_mem = File::open("/dev/chromeos-low-mem").ok(); |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 1321 | if let Some(low_mem) = &low_mem { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1322 | poll_ctx |
| 1323 | .add(low_mem, Token::LowMemory) |
| 1324 | .map_err(Error::PollContextAdd)?; |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1325 | } else { |
| 1326 | warn!("Unable to open low mem indicator, maybe not a chrome os kernel"); |
| 1327 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1328 | |
| 1329 | // Used to rate limit balloon requests. |
| 1330 | let mut lowmem_timer = TimerFd::new().map_err(Error::CreateTimerFd)?; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1331 | poll_ctx |
| 1332 | .add(&lowmem_timer, Token::LowmemTimer) |
| 1333 | .map_err(Error::PollContextAdd)?; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1334 | |
| 1335 | // Used to check whether it's ok to start giving memory back to the VM. |
| 1336 | let mut freemem_timer = TimerFd::new().map_err(Error::CreateTimerFd)?; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1337 | poll_ctx |
| 1338 | .add(&freemem_timer, Token::CheckAvailableMemory) |
| 1339 | .map_err(Error::PollContextAdd)?; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1340 | |
| 1341 | // Used to add jitter to timer values so that we don't have a thundering herd problem when |
| 1342 | // multiple VMs are running. |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 1343 | let mut simple_rng = SimpleRng::new( |
| 1344 | SystemTime::now() |
| 1345 | .duration_since(UNIX_EPOCH) |
| 1346 | .expect("time went backwards") |
| 1347 | .subsec_nanos() as u64, |
| 1348 | ); |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1349 | |
Lepton Wu | 20333e4 | 2019-03-14 10:48:03 -0700 | [diff] [blame] | 1350 | if sandbox { |
| 1351 | // Before starting VCPUs, in case we started with some capabilities, drop them all. |
| 1352 | drop_capabilities().map_err(Error::DropCapabilities)?; |
| 1353 | } |
Dmitry Torokhov | 7100607 | 2019-03-06 10:56:51 -0800 | [diff] [blame] | 1354 | |
Daniel Verkamp | 37c4a78 | 2019-01-04 10:44:17 -0800 | [diff] [blame] | 1355 | let mut vcpu_handles = Vec::with_capacity(linux.vcpus.len()); |
| 1356 | let vcpu_thread_barrier = Arc::new(Barrier::new(linux.vcpus.len() + 1)); |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1357 | let run_mode_arc = Arc::new(VcpuRunMode::default()); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1358 | setup_vcpu_signal_handler()?; |
| 1359 | for (cpu_id, vcpu) in linux.vcpus.into_iter().enumerate() { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1360 | let handle = run_vcpu( |
| 1361 | vcpu, |
| 1362 | cpu_id as u32, |
Daniel Verkamp | 107edb3 | 2019-04-05 09:58:48 -0700 | [diff] [blame] | 1363 | linux.vcpu_affinity.clone(), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1364 | vcpu_thread_barrier.clone(), |
| 1365 | linux.io_bus.clone(), |
| 1366 | linux.mmio_bus.clone(), |
| 1367 | linux.exit_evt.try_clone().map_err(Error::CloneEventFd)?, |
Zach Reizner | 795355a | 2019-01-16 17:37:57 -0800 | [diff] [blame] | 1368 | linux.vm.check_extension(Cap::KvmclockCtrl), |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1369 | run_mode_arc.clone(), |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1370 | )?; |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1371 | vcpu_handles.push(handle); |
| 1372 | } |
| 1373 | vcpu_thread_barrier.wait(); |
| 1374 | |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1375 | 'poll: loop { |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1376 | let events = { |
| 1377 | match poll_ctx.wait() { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1378 | Ok(v) => v, |
| 1379 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1380 | error!("failed to poll: {}", e); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | }; |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1385 | |
| 1386 | let mut vm_control_indices_to_remove = Vec::new(); |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1387 | for event in events.iter_readable() { |
| 1388 | match event.token() { |
| 1389 | Token::Exit => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1390 | info!("vcpu requested shutdown"); |
| 1391 | break 'poll; |
| 1392 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1393 | Token::Stdin => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1394 | let mut out = [0u8; 64]; |
| 1395 | match stdin_lock.read_raw(&mut out[..]) { |
| 1396 | Ok(0) => { |
| 1397 | // Zero-length read indicates EOF. Remove from pollables. |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1398 | let _ = poll_ctx.delete(&stdin_handle); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1399 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1400 | Err(e) => { |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1401 | warn!("error while reading stdin: {}", e); |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1402 | let _ = poll_ctx.delete(&stdin_handle); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1403 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1404 | Ok(count) => { |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1405 | linux |
| 1406 | .stdio_serial |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1407 | .lock() |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1408 | .queue_input_bytes(&out[..count]) |
| 1409 | .expect("failed to queue bytes into serial port"); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1410 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1411 | } |
| 1412 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1413 | Token::ChildSignal => { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1414 | // Print all available siginfo structs, then exit the loop. |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1415 | while let Some(siginfo) = sigchld_fd.read().map_err(Error::SignalFd)? { |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 1416 | let pid = siginfo.ssi_pid; |
| 1417 | let pid_label = match linux.pid_debug_label_map.get(&pid) { |
| 1418 | Some(label) => format!("{} (pid {})", label, pid), |
| 1419 | None => format!("pid {}", pid), |
| 1420 | }; |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1421 | error!( |
| 1422 | "child {} died: signo {}, status {}, code {}", |
Zach Reizner | 3ba0098 | 2019-01-23 19:04:43 -0800 | [diff] [blame] | 1423 | pid_label, siginfo.ssi_signo, siginfo.ssi_status, siginfo.ssi_code |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1424 | ); |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1425 | } |
David Tolnay | f503276 | 2018-12-03 10:46:45 -0800 | [diff] [blame] | 1426 | break 'poll; |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1427 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1428 | Token::CheckAvailableMemory => { |
| 1429 | // Acknowledge the timer. |
| 1430 | freemem_timer.wait().map_err(Error::TimerFd)?; |
| 1431 | if current_balloon_memory == 0 { |
| 1432 | // Nothing to see here. |
| 1433 | if let Err(e) = freemem_timer.clear() { |
| 1434 | warn!("unable to clear available memory check timer: {}", e); |
| 1435 | } |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | // Otherwise see if we can free up some memory. |
| 1440 | let margin = file_to_u64(LOWMEM_MARGIN).map_err(Error::ReadLowmemMargin)?; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1441 | let available = |
| 1442 | file_to_u64(LOWMEM_AVAILABLE).map_err(Error::ReadLowmemAvailable)?; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1443 | |
| 1444 | // `available` and `margin` are specified in MB while `balloon_memory_increment` is in |
| 1445 | // bytes. So to correctly compare them we need to turn the increment value into MB. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1446 | if available >= margin + 2 * (balloon_memory_increment >> 20) { |
| 1447 | current_balloon_memory = |
| 1448 | if current_balloon_memory >= balloon_memory_increment { |
| 1449 | current_balloon_memory - balloon_memory_increment |
| 1450 | } else { |
| 1451 | 0 |
| 1452 | }; |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1453 | let command = BalloonControlCommand::Adjust { |
| 1454 | num_bytes: current_balloon_memory, |
| 1455 | }; |
| 1456 | if let Err(e) = balloon_host_socket.send(&command) { |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1457 | warn!("failed to send memory value to balloon device: {}", e); |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | Token::LowMemory => { |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 1462 | if let Some(low_mem) = &low_mem { |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1463 | let old_balloon_memory = current_balloon_memory; |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1464 | current_balloon_memory = min( |
| 1465 | current_balloon_memory + balloon_memory_increment, |
| 1466 | max_balloon_memory, |
| 1467 | ); |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1468 | if current_balloon_memory != old_balloon_memory { |
Jakub Staron | 1f828d7 | 2019-04-11 12:49:29 -0700 | [diff] [blame] | 1469 | let command = BalloonControlCommand::Adjust { |
| 1470 | num_bytes: current_balloon_memory, |
| 1471 | }; |
| 1472 | if let Err(e) = balloon_host_socket.send(&command) { |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1473 | warn!("failed to send memory value to balloon device: {}", e); |
| 1474 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1475 | } |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1476 | |
| 1477 | // Stop polling the lowmem device until the timer fires. |
| 1478 | poll_ctx.delete(low_mem).map_err(Error::PollContextDelete)?; |
| 1479 | |
| 1480 | // Add some jitter to the timer so that if there are multiple VMs running |
| 1481 | // they don't all start ballooning at exactly the same time. |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 1482 | let lowmem_dur = Duration::from_millis(1000 + simple_rng.rng() % 200); |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1483 | lowmem_timer |
| 1484 | .reset(lowmem_dur, None) |
| 1485 | .map_err(Error::ResetTimerFd)?; |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1486 | |
| 1487 | // Also start a timer to check when we can start giving memory back. Do the |
| 1488 | // first check after a minute (with jitter) and subsequent checks after |
| 1489 | // every 30 seconds (with jitter). |
Daniel Prilik | 2200604 | 2019-01-14 14:19:04 -0800 | [diff] [blame] | 1490 | let freemem_dur = Duration::from_secs(60 + simple_rng.rng() % 12); |
| 1491 | let freemem_int = Duration::from_secs(30 + simple_rng.rng() % 6); |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1492 | freemem_timer |
| 1493 | .reset(freemem_dur, Some(freemem_int)) |
| 1494 | .map_err(Error::ResetTimerFd)?; |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1495 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1496 | } |
| 1497 | Token::LowmemTimer => { |
| 1498 | // Acknowledge the timer. |
| 1499 | lowmem_timer.wait().map_err(Error::TimerFd)?; |
| 1500 | |
David Tolnay | 64cd5ea | 2019-04-15 15:56:35 -0700 | [diff] [blame] | 1501 | if let Some(low_mem) = &low_mem { |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1502 | // Start polling the lowmem device again. |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1503 | poll_ctx |
| 1504 | .add(low_mem, Token::LowMemory) |
| 1505 | .map_err(Error::PollContextAdd)?; |
Dylan Reid | f11e6ed | 2018-07-31 10:24:06 -0700 | [diff] [blame] | 1506 | } |
Chirantan Ekbote | 448516e | 2018-07-24 16:07:42 -0700 | [diff] [blame] | 1507 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1508 | Token::VmControlServer => { |
| 1509 | if let Some(socket_server) = &control_server_socket { |
| 1510 | match socket_server.accept() { |
| 1511 | Ok(socket) => { |
| 1512 | poll_ctx |
| 1513 | .add( |
| 1514 | &socket, |
| 1515 | Token::VmControl { |
| 1516 | index: control_sockets.len(), |
| 1517 | }, |
| 1518 | ) |
| 1519 | .map_err(Error::PollContextAdd)?; |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1520 | control_sockets |
| 1521 | .push(TaggedControlSocket::Vm(MsgSocket::new(socket))); |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1522 | } |
| 1523 | Err(e) => error!("failed to accept socket: {}", e), |
| 1524 | } |
| 1525 | } |
| 1526 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1527 | Token::VmControl { index } => { |
Daniel Verkamp | 37c4a78 | 2019-01-04 10:44:17 -0800 | [diff] [blame] | 1528 | if let Some(socket) = control_sockets.get(index) { |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1529 | match socket { |
| 1530 | TaggedControlSocket::Vm(socket) => match socket.recv() { |
| 1531 | Ok(request) => { |
| 1532 | let mut run_mode_opt = None; |
| 1533 | let response = request.execute( |
| 1534 | &mut run_mode_opt, |
| 1535 | &balloon_host_socket, |
| 1536 | disk_host_sockets, |
| 1537 | &usb_control_socket, |
| 1538 | ); |
| 1539 | if let Err(e) = socket.send(&response) { |
| 1540 | error!("failed to send VmResponse: {}", e); |
| 1541 | } |
| 1542 | if let Some(run_mode) = run_mode_opt { |
| 1543 | info!("control socket changed run mode to {}", run_mode); |
| 1544 | match run_mode { |
| 1545 | VmRunMode::Exiting => { |
| 1546 | break 'poll; |
| 1547 | } |
| 1548 | other => { |
| 1549 | run_mode_arc.set_and_notify(other); |
| 1550 | for handle in &vcpu_handles { |
| 1551 | let _ = handle.kill(SIGRTMIN() + 0); |
| 1552 | } |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1556 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1557 | Err(e) => { |
| 1558 | if let MsgError::BadRecvSize { actual: 0, .. } = e { |
| 1559 | vm_control_indices_to_remove.push(index); |
| 1560 | } else { |
| 1561 | error!("failed to recv VmRequest: {}", e); |
| 1562 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1563 | } |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1564 | }, |
| 1565 | TaggedControlSocket::Wayland(socket) => match socket.recv() { |
| 1566 | Ok(request) => { |
| 1567 | let response = |
| 1568 | request.execute(&mut linux.vm, &mut linux.resources); |
| 1569 | if let Err(e) = socket.send(&response) { |
| 1570 | error!("failed to send WlControlResponse: {}", e); |
| 1571 | } |
| 1572 | } |
| 1573 | Err(e) => { |
| 1574 | if let MsgError::BadRecvSize { actual: 0, .. } = e { |
| 1575 | vm_control_indices_to_remove.push(index); |
| 1576 | } else { |
| 1577 | error!("failed to recv WlControlRequest: {}", e); |
| 1578 | } |
| 1579 | } |
| 1580 | }, |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1581 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1582 | } |
| 1583 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1584 | } |
| 1585 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1586 | |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1587 | for event in events.iter_hungup() { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1588 | match event.token() { |
| 1589 | Token::Exit => {} |
| 1590 | Token::Stdin => { |
| 1591 | let _ = poll_ctx.delete(&stdin_handle); |
| 1592 | } |
| 1593 | Token::ChildSignal => {} |
| 1594 | Token::CheckAvailableMemory => {} |
| 1595 | Token::LowMemory => {} |
| 1596 | Token::LowmemTimer => {} |
| 1597 | Token::VmControlServer => {} |
| 1598 | Token::VmControl { index } => { |
| 1599 | // It's possible more data is readable and buffered while the socket is hungup, |
| 1600 | // so don't delete the socket from the poll context until we're sure all the |
| 1601 | // data is read. |
Jakub Staron | d99cd0a | 2019-04-11 14:09:39 -0700 | [diff] [blame] | 1602 | match control_sockets |
| 1603 | .get(index) |
| 1604 | .map(|s| s.as_ref().get_readable_bytes()) |
| 1605 | { |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1606 | Some(Ok(0)) | Some(Err(_)) => vm_control_indices_to_remove.push(index), |
| 1607 | Some(Ok(x)) => info!("control index {} has {} bytes readable", index, x), |
| 1608 | _ => {} |
Zach Reizner | 55a9e50 | 2018-10-03 10:22:32 -0700 | [diff] [blame] | 1609 | } |
Zach Reizner | 5bed0d2 | 2018-03-28 02:31:11 -0700 | [diff] [blame] | 1610 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1611 | } |
| 1612 | } |
Zach Reizner | a60744b | 2019-02-13 17:33:32 -0800 | [diff] [blame] | 1613 | |
| 1614 | // Sort in reverse so the highest indexes are removed first. This removal algorithm |
| 1615 | // preserved correct indexes as each element is removed. |
| 1616 | vm_control_indices_to_remove.sort_unstable_by(|a, b| b.cmp(a)); |
| 1617 | vm_control_indices_to_remove.dedup(); |
| 1618 | for index in vm_control_indices_to_remove { |
| 1619 | control_sockets.swap_remove(index); |
| 1620 | if let Some(socket) = control_sockets.get(index) { |
| 1621 | poll_ctx |
| 1622 | .add(socket, Token::VmControl { index }) |
| 1623 | .map_err(Error::PollContextAdd)?; |
| 1624 | } |
| 1625 | } |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Zach Reizner | 6a8fdd9 | 2019-01-16 14:38:41 -0800 | [diff] [blame] | 1628 | // VCPU threads MUST see the VmRunMode flag, otherwise they may re-enter the VM. |
| 1629 | run_mode_arc.set_and_notify(VmRunMode::Exiting); |
Dylan Reid | 059a188 | 2018-07-23 17:58:09 -0700 | [diff] [blame] | 1630 | for handle in vcpu_handles { |
Dmitry Torokhov | cd40533 | 2018-02-16 16:25:54 -0800 | [diff] [blame] | 1631 | match handle.kill(SIGRTMIN() + 0) { |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1632 | Ok(_) => { |
| 1633 | if let Err(e) = handle.join() { |
| 1634 | error!("failed to join vcpu thread: {:?}", e); |
| 1635 | } |
| 1636 | } |
David Tolnay | b4bd00f | 2019-02-12 17:51:26 -0800 | [diff] [blame] | 1637 | Err(e) => error!("failed to kill vcpu thread: {}", e), |
Zach Reizner | 39aa26b | 2017-12-12 18:03:23 -0800 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | stdin_lock |
| 1642 | .set_canon_mode() |
| 1643 | .expect("failed to restore canonical mode for terminal"); |
| 1644 | |
| 1645 | Ok(()) |
| 1646 | } |