blob: a85812973cc04a83738cdb5658e3feb02f74da73 [file] [log] [blame]
Zach Reizner39aa26b2017-12-12 18:03:23 -08001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std;
Chirantan Ekbote448516e2018-07-24 16:07:42 -07006use std::cmp::min;
Dylan Reid059a1882018-07-23 17:58:09 -07007use std::ffi::CStr;
Zach Reizner39aa26b2017-12-12 18:03:23 -08008use std::fmt;
Sonny Raoed517d12018-02-13 22:09:43 -08009use std::error;
Dylan Reid059a1882018-07-23 17:58:09 -070010use std::fs::{File, OpenOptions};
Chirantan Ekbote448516e2018-07-24 16:07:42 -070011use std::io::{self, Read, stdin};
12use std::mem;
Dylan Reid059a1882018-07-23 17:58:09 -070013use std::os::unix::io::{FromRawFd, RawFd};
Zach Reizner39aa26b2017-12-12 18:03:23 -080014use std::os::unix::net::UnixDatagram;
15use std::path::{Path, PathBuf};
Chirantan Ekbote448516e2018-07-24 16:07:42 -070016use std::str;
Zach Reizner39aa26b2017-12-12 18:03:23 -080017use std::sync::atomic::{AtomicBool, Ordering};
Dylan Reid059a1882018-07-23 17:58:09 -070018use std::sync::{Arc, Barrier};
Zach Reizner39aa26b2017-12-12 18:03:23 -080019use std::thread;
Chirantan Ekbote448516e2018-07-24 16:07:42 -070020use std::time::Duration;
Zach Reizner39aa26b2017-12-12 18:03:23 -080021use std::thread::JoinHandle;
22
Chirantan Ekbote448516e2018-07-24 16:07:42 -070023use libc::{self, c_int};
24use rand::thread_rng;
25use rand::distributions::{IndependentSample, Range};
Zach Reizner39aa26b2017-12-12 18:03:23 -080026
Chirantan Ekbote448516e2018-07-24 16:07:42 -070027use byteorder::{ByteOrder, LittleEndian};
Zach Reizner39aa26b2017-12-12 18:03:23 -080028use devices;
29use io_jail::{self, Minijail};
Zach Reizner39aa26b2017-12-12 18:03:23 -080030use kvm::*;
Jason D. Clinton865323d2017-09-27 22:04:03 -060031use net_util::Tap;
Dylan Reid88624f82018-01-11 09:20:16 -080032use qcow::{self, QcowFile};
Zach Reizner39aa26b2017-12-12 18:03:23 -080033use sys_util::*;
34use sys_util;
Jason D. Clinton865323d2017-09-27 22:04:03 -060035use vhost;
Dylan Reid228e4a62018-06-07 15:42:41 -070036use vm_control::VmRequest;
Zach Reizner39aa26b2017-12-12 18:03:23 -080037
38use Config;
Dylan Reid88624f82018-01-11 09:20:16 -080039use DiskType;
Dylan Reid059a1882018-07-23 17:58:09 -070040use VirtIoDeviceInfo;
Zach Reizner39aa26b2017-12-12 18:03:23 -080041
Dylan Reid059a1882018-07-23 17:58:09 -070042use arch::{self, LinuxArch, RunnableLinuxVm, VirtioDeviceStub, VmComponents};
Sonny Raoed517d12018-02-13 22:09:43 -080043
44#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
45use x86_64::X8664arch as Arch;
Sonny Rao2ffa0cb2018-02-26 17:27:40 -080046#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
47use aarch64::AArch64 as Arch;
Zach Reizner39aa26b2017-12-12 18:03:23 -080048
Dylan Reid059a1882018-07-23 17:58:09 -070049#[derive(Debug)]
Zach Reizner39aa26b2017-12-12 18:03:23 -080050pub enum Error {
Dylan Reid295ccac2017-11-06 14:06:24 -080051 BalloonDeviceNew(devices::virtio::BalloonError),
Zach Reizner39aa26b2017-12-12 18:03:23 -080052 BlockDeviceNew(sys_util::Error),
Mark Ryan6ed5aea2018-04-20 13:52:35 +010053 BlockSignal(sys_util::signal::Error),
Dylan Reid059a1882018-07-23 17:58:09 -070054 BuildingVm(Box<error::Error>),
Zach Reizner8fb52112017-12-13 16:04:39 -080055 CloneEventFd(sys_util::Error),
Zach Reizner8fb52112017-12-13 16:04:39 -080056 CreateEventFd(sys_util::Error),
Zach Reizner5bed0d22018-03-28 02:31:11 -070057 CreatePollContext(sys_util::Error),
Zach Reizner8fb52112017-12-13 16:04:39 -080058 CreateSignalFd(sys_util::SignalFdError),
59 CreateSocket(io::Error),
Chirantan Ekbote448516e2018-07-24 16:07:42 -070060 CreateTimerFd(sys_util::Error),
Zach Reizner39aa26b2017-12-12 18:03:23 -080061 DeviceJail(io_jail::Error),
62 DevicePivotRoot(io_jail::Error),
Zach Reizner8fb52112017-12-13 16:04:39 -080063 Disk(io::Error),
Stephen Barberc79de2d2018-02-21 14:17:27 -080064 DiskImageLock(sys_util::Error),
Dylan Reid20566442018-04-02 15:06:15 -070065 FailedCLOEXECCheck,
66 FailedToDupFd,
Dylan Reid20566442018-04-02 15:06:15 -070067 InvalidFdPath,
Zach Reizner8fb52112017-12-13 16:04:39 -080068 NetDeviceNew(devices::virtio::NetError),
69 NoVarEmpty,
70 OpenKernel(PathBuf, io::Error),
Chirantan Ekboteebd56812018-04-16 19:32:04 -070071 P9DeviceNew(devices::virtio::P9Error),
Zach Reizner5bed0d22018-03-28 02:31:11 -070072 PollContextAdd(sys_util::Error),
Chirantan Ekbote448516e2018-07-24 16:07:42 -070073 PollContextDelete(sys_util::Error),
Dylan Reid88624f82018-01-11 09:20:16 -080074 QcowDeviceCreate(qcow::Error),
Chirantan Ekbote448516e2018-07-24 16:07:42 -070075 ReadLowmemAvailable(io::Error),
76 ReadLowmemMargin(io::Error),
Dylan Reid059a1882018-07-23 17:58:09 -070077 RegisterBalloon(arch::MmioRegisterError),
78 RegisterBlock(arch::MmioRegisterError),
79 RegisterGpu(arch::MmioRegisterError),
80 RegisterNet(arch::MmioRegisterError),
81 RegisterP9(arch::MmioRegisterError),
82 RegisterRng(arch::MmioRegisterError),
Mark Ryan6ed5aea2018-04-20 13:52:35 +010083 RegisterSignalHandler(sys_util::Error),
Dylan Reid059a1882018-07-23 17:58:09 -070084 RegisterWayland(arch::MmioRegisterError),
Chirantan Ekbote448516e2018-07-24 16:07:42 -070085 ResetTimerFd(sys_util::Error),
Zach Reizner39aa26b2017-12-12 18:03:23 -080086 RngDeviceNew(devices::virtio::RngError),
Zach Reizner8fb52112017-12-13 16:04:39 -080087 SettingGidMap(io_jail::Error),
88 SettingUidMap(io_jail::Error),
Zach Reizner8fb52112017-12-13 16:04:39 -080089 SignalFd(sys_util::SignalFdError),
90 SpawnVcpu(io::Error),
Chirantan Ekbote448516e2018-07-24 16:07:42 -070091 TimerFd(sys_util::Error),
Zach Reizner8fb52112017-12-13 16:04:39 -080092 VhostNetDeviceNew(devices::virtio::vhost::Error),
93 VhostVsockDeviceNew(devices::virtio::vhost::Error),
94 WaylandDeviceNew(sys_util::Error),
Sonny Raoed517d12018-02-13 22:09:43 -080095 LoadKernel(Box<error::Error>),
Zach Reizner39aa26b2017-12-12 18:03:23 -080096}
97
98impl fmt::Display for Error {
99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100 match self {
Dylan Reid295ccac2017-11-06 14:06:24 -0800101 &Error::BalloonDeviceNew(ref e) => write!(f, "failed to create balloon: {:?}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800102 &Error::BlockDeviceNew(ref e) => write!(f, "failed to create block device: {:?}", e),
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100103 &Error::BlockSignal(ref e) => write!(f, "failed to block signal: {:?}", e),
Dylan Reid059a1882018-07-23 17:58:09 -0700104 &Error::BuildingVm(ref e) => {
105 write!(f, "The architecture failed to build the vm: {:?}", e)
Zach Reizner8fb52112017-12-13 16:04:39 -0800106 }
Dylan Reid059a1882018-07-23 17:58:09 -0700107 &Error::CloneEventFd(ref e) => write!(f, "failed to clone eventfd: {:?}", e),
108 &Error::CreateEventFd(ref e) => write!(f, "failed to create eventfd: {:?}", e),
Zach Reizner5bed0d22018-03-28 02:31:11 -0700109 &Error::CreatePollContext(ref e) => write!(f, "failed to create poll context: {:?}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800110 &Error::CreateSignalFd(ref e) => write!(f, "failed to create signalfd: {:?}", e),
111 &Error::CreateSocket(ref e) => write!(f, "failed to create socket: {}", e),
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700112 &Error::CreateTimerFd(ref e) => write!(f, "failed to create timerfd: {}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800113 &Error::DeviceJail(ref e) => write!(f, "failed to jail device: {}", e),
114 &Error::DevicePivotRoot(ref e) => write!(f, "failed to pivot root device: {}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800115 &Error::Disk(ref e) => write!(f, "failed to load disk image: {}", e),
Stephen Barberc79de2d2018-02-21 14:17:27 -0800116 &Error::DiskImageLock(ref e) => write!(f, "failed to lock disk image: {:?}", e),
Dylan Reid20566442018-04-02 15:06:15 -0700117 &Error::FailedCLOEXECCheck => {
118 write!(f, "/proc/self/fd argument failed check for CLOEXEC")
119 }
120 &Error::FailedToDupFd => write!(f, "failed to dup fd from /proc/self/fd"),
Dylan Reid20566442018-04-02 15:06:15 -0700121 &Error::InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"),
Zach Reizner8fb52112017-12-13 16:04:39 -0800122 &Error::NetDeviceNew(ref e) => write!(f, "failed to set up virtio networking: {:?}", e),
123 &Error::NoVarEmpty => write!(f, "/var/empty doesn't exist, can't jail devices."),
124 &Error::OpenKernel(ref p, ref e) => {
125 write!(f, "failed to open kernel image {:?}: {}", p, e)
126 }
Chirantan Ekboteebd56812018-04-16 19:32:04 -0700127 &Error::P9DeviceNew(ref e) => write!(f, "failed to create 9p device: {}", e),
Zach Reizner5bed0d22018-03-28 02:31:11 -0700128 &Error::PollContextAdd(ref e) => write!(f, "failed to add fd to poll context: {:?}", e),
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700129 &Error::PollContextDelete(ref e) => {
130 write!(f, "failed to remove fd from poll context: {:?}", e)
131 }
Dylan Reid88624f82018-01-11 09:20:16 -0800132 &Error::QcowDeviceCreate(ref e) => {
133 write!(f, "failed to read qcow formatted file {:?}", e)
134 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700135 &Error::ReadLowmemAvailable(ref e) => {
136 write!(f, "failed to read /sys/kernel/mm/chromeos-low_mem/available: {}", e)
137 }
138 &Error::ReadLowmemMargin(ref e) => {
139 write!(f, "failed to read /sys/kernel/mm/chromeos-low_mem/margin: {}", e)
140 }
Dylan Reid295ccac2017-11-06 14:06:24 -0800141 &Error::RegisterBalloon(ref e) => {
142 write!(f, "error registering balloon device: {:?}", e)
143 },
Zach Reizner8fb52112017-12-13 16:04:39 -0800144 &Error::RegisterBlock(ref e) => write!(f, "error registering block device: {:?}", e),
Zach Reizner3a8100a2017-09-13 19:15:43 -0700145 &Error::RegisterGpu(ref e) => write!(f, "error registering gpu device: {:?}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800146 &Error::RegisterNet(ref e) => write!(f, "error registering net device: {:?}", e),
Chirantan Ekboteebd56812018-04-16 19:32:04 -0700147 &Error::RegisterP9(ref e) => write!(f, "error registering 9p device: {:?}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800148 &Error::RegisterRng(ref e) => write!(f, "error registering rng device: {:?}", e),
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100149 &Error::RegisterSignalHandler(ref e) => {
150 write!(f, "error registering signal handler: {:?}", e)
151 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800152 &Error::RegisterWayland(ref e) => write!(f, "error registering wayland device: {}", e),
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700153 &Error::ResetTimerFd(ref e) => write!(f, "failed to reset timerfd: {}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800154 &Error::RngDeviceNew(ref e) => write!(f, "failed to set up rng: {:?}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800155 &Error::SettingGidMap(ref e) => write!(f, "error setting GID map: {}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800156 &Error::SettingUidMap(ref e) => write!(f, "error setting UID map: {}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800157 &Error::SignalFd(ref e) => write!(f, "failed to read signal fd: {:?}", e),
158 &Error::SpawnVcpu(ref e) => write!(f, "failed to spawn VCPU thread: {:?}", e),
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700159 &Error::TimerFd(ref e) => write!(f, "failed to read timer fd: {:?}", e),
Zach Reizner8fb52112017-12-13 16:04:39 -0800160 &Error::VhostNetDeviceNew(ref e) => {
161 write!(f, "failed to set up vhost networking: {:?}", e)
162 }
163 &Error::VhostVsockDeviceNew(ref e) => {
164 write!(f, "failed to set up virtual socket device: {:?}", e)
165 }
166 &Error::WaylandDeviceNew(ref e) => {
167 write!(f, "failed to create wayland device: {:?}", e)
168 }
Sonny Raoed517d12018-02-13 22:09:43 -0800169 &Error::LoadKernel(ref e) => write!(f, "failed to load kernel: {}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800170 }
171 }
172}
173
Dylan Reid059a1882018-07-23 17:58:09 -0700174impl std::error::Error for Error {
175 fn description(&self) -> &str {
176 "Some device failure"
177 }
178}
179
Zach Reizner39aa26b2017-12-12 18:03:23 -0800180type Result<T> = std::result::Result<T, Error>;
181
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700182// Verifies that |raw_fd| is actually owned by this process and duplicates it to ensure that
183// we have a unique handle to it.
Dylan Reid059a1882018-07-23 17:58:09 -0700184fn validate_raw_fd(raw_fd: RawFd) -> std::result::Result<RawFd, Box<error::Error>> {
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700185 // Checking that close-on-exec isn't set helps filter out FDs that were opened by
186 // crosvm as all crosvm FDs are close on exec.
187 // Safe because this doesn't modify any memory and we check the return value.
188 let flags = unsafe { libc::fcntl(raw_fd, libc::F_GETFD) };
189 if flags < 0 || (flags & libc::FD_CLOEXEC) != 0 {
Dylan Reid059a1882018-07-23 17:58:09 -0700190 return Err(Box::new(Error::FailedCLOEXECCheck));
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700191 }
192
193 // Duplicate the fd to ensure that we don't accidentally close an fd previously
194 // opened by another subsystem. Safe because this doesn't modify any memory and
195 // we check the return value.
196 let dup_fd = unsafe { libc::fcntl(raw_fd, libc::F_DUPFD_CLOEXEC, 0) };
197 if dup_fd < 0 {
Dylan Reid059a1882018-07-23 17:58:09 -0700198 return Err(Box::new(Error::FailedToDupFd));
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700199 }
200 Ok(dup_fd as RawFd)
201}
202
Zach Reizner39aa26b2017-12-12 18:03:23 -0800203fn create_base_minijail(root: &Path, seccomp_policy: &Path) -> Result<Minijail> {
204 // All child jails run in a new user namespace without any users mapped,
205 // they run as nobody unless otherwise configured.
206 let mut j = Minijail::new().map_err(|e| Error::DeviceJail(e))?;
207 j.namespace_pids();
208 j.namespace_user();
209 j.namespace_user_disable_setgroups();
210 // Don't need any capabilities.
211 j.use_caps(0);
212 // Create a new mount namespace with an empty root FS.
213 j.namespace_vfs();
214 j.enter_pivot_root(root)
215 .map_err(|e| Error::DevicePivotRoot(e))?;
216 // Run in an empty network namespace.
217 j.namespace_net();
218 // Apply the block device seccomp policy.
219 j.no_new_privs();
Stephen Barber3b1d8a52018-01-06 17:34:51 -0800220 // Use TSYNC only for the side effect of it using SECCOMP_RET_TRAP, which will correctly kill
221 // the entire device process if a worker thread commits a seccomp violation.
222 j.set_seccomp_filter_tsync();
Zach Reizner043ddc52018-04-03 20:47:21 -0700223 #[cfg(debug_assertions)]
224 j.log_seccomp_filter_failures();
Zach Reizner39aa26b2017-12-12 18:03:23 -0800225 j.parse_seccomp_filters(seccomp_policy)
226 .map_err(|e| Error::DeviceJail(e))?;
227 j.use_seccomp_filter();
228 // Don't do init setup.
229 j.run_as_init();
230 Ok(j)
231}
232
Dylan Reid059a1882018-07-23 17:58:09 -0700233fn create_virtio_devs(cfg: VirtIoDeviceInfo,
234 mem: &GuestMemory,
235 _exit_evt: &EventFd,
236 wayland_device_socket: UnixDatagram,
237 balloon_device_socket: UnixDatagram)
238 -> std::result::Result<Vec<VirtioDeviceStub>, Box<error::Error>> {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800239 static DEFAULT_PIVOT_ROOT: &'static str = "/var/empty";
Dylan Reidef7352f2018-05-17 18:47:11 -0700240
Dylan Reid059a1882018-07-23 17:58:09 -0700241 let mut devs = Vec::new();
Zach Reizner39aa26b2017-12-12 18:03:23 -0800242
243 // An empty directory for jailed device's pivot root.
244 let empty_root_path = Path::new(DEFAULT_PIVOT_ROOT);
245 if cfg.multiprocess && !empty_root_path.exists() {
Dylan Reid059a1882018-07-23 17:58:09 -0700246 return Err(Box::new(Error::NoVarEmpty));
Zach Reizner39aa26b2017-12-12 18:03:23 -0800247 }
248
Zach Reizner8fb52112017-12-13 16:04:39 -0800249 for disk in &cfg.disks {
Dylan Reid20566442018-04-02 15:06:15 -0700250 // Special case '/proc/self/fd/*' paths. The FD is already open, just use it.
251 let mut raw_image: File = if disk.path.parent() == Some(Path::new("/proc/self/fd")) {
252 if !disk.path.is_file() {
Dylan Reid059a1882018-07-23 17:58:09 -0700253 return Err(Box::new(Error::InvalidFdPath));
Dylan Reid20566442018-04-02 15:06:15 -0700254 }
255 let raw_fd = disk.path.file_name()
256 .and_then(|fd_osstr| fd_osstr.to_str())
257 .and_then(|fd_str| fd_str.parse::<c_int>().ok())
258 .ok_or(Error::InvalidFdPath)?;
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700259 // Safe because we will validate |raw_fd|.
260 unsafe { File::from_raw_fd(validate_raw_fd(raw_fd)?) }
Dylan Reid20566442018-04-02 15:06:15 -0700261 } else {
262 OpenOptions::new()
263 .read(true)
Daniel Verkampde9ae032018-08-09 16:26:59 -0700264 .write(!disk.read_only)
Dylan Reid20566442018-04-02 15:06:15 -0700265 .open(&disk.path)
266 .map_err(|e| Error::Disk(e))?
267 };
Stephen Barberc79de2d2018-02-21 14:17:27 -0800268 // Lock the disk image to prevent other crosvm instances from using it.
Daniel Verkampde9ae032018-08-09 16:26:59 -0700269 let lock_op = if disk.read_only {
Stephen Barberc79de2d2018-02-21 14:17:27 -0800270 FlockOperation::LockShared
Daniel Verkampde9ae032018-08-09 16:26:59 -0700271 } else {
272 FlockOperation::LockExclusive
Stephen Barberc79de2d2018-02-21 14:17:27 -0800273 };
274 flock(&raw_image, lock_op, true).map_err(Error::DiskImageLock)?;
275
Dylan Reid88624f82018-01-11 09:20:16 -0800276 let block_box: Box<devices::virtio::VirtioDevice> = match disk.disk_type {
277 DiskType::FlatFile => { // Access as a raw block device.
Daniel Verkamp70589a02018-08-09 16:27:44 -0700278 Box::new(devices::virtio::Block::new(raw_image, disk.read_only)
Dylan Reid88624f82018-01-11 09:20:16 -0800279 .map_err(|e| Error::BlockDeviceNew(e))?)
280 }
281 DiskType::Qcow => { // Valid qcow header present
282 let qcow_image = QcowFile::from(raw_image)
283 .map_err(|e| Error::QcowDeviceCreate(e))?;
Daniel Verkamp70589a02018-08-09 16:27:44 -0700284 Box::new(devices::virtio::Block::new(qcow_image, disk.read_only)
Dylan Reid88624f82018-01-11 09:20:16 -0800285 .map_err(|e| Error::BlockDeviceNew(e))?)
286 }
287 };
Zach Reizner39aa26b2017-12-12 18:03:23 -0800288 let jail = if cfg.multiprocess {
289 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("block_device.policy");
290 Some(create_base_minijail(empty_root_path, &policy_path)?)
291 }
292 else {
293 None
294 };
295
Dylan Reid059a1882018-07-23 17:58:09 -0700296 devs.push(VirtioDeviceStub {dev: block_box, jail});
Zach Reizner39aa26b2017-12-12 18:03:23 -0800297 }
298
299 let rng_box = Box::new(devices::virtio::Rng::new().map_err(Error::RngDeviceNew)?);
300 let rng_jail = if cfg.multiprocess {
301 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("rng_device.policy");
302 Some(create_base_minijail(empty_root_path, &policy_path)?)
303 } else {
304 None
305 };
Dylan Reid059a1882018-07-23 17:58:09 -0700306 devs.push(VirtioDeviceStub {dev: rng_box, jail: rng_jail});
Zach Reizner39aa26b2017-12-12 18:03:23 -0800307
Dylan Reid295ccac2017-11-06 14:06:24 -0800308 let balloon_box = Box::new(devices::virtio::Balloon::new(balloon_device_socket)
309 .map_err(Error::BalloonDeviceNew)?);
310 let balloon_jail = if cfg.multiprocess {
311 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("balloon_device.policy");
312 Some(create_base_minijail(empty_root_path, &policy_path)?)
313 } else {
314 None
315 };
Dylan Reid059a1882018-07-23 17:58:09 -0700316 devs.push(VirtioDeviceStub {dev: balloon_box, jail: balloon_jail});
Dylan Reid295ccac2017-11-06 14:06:24 -0800317
Zach Reizner39aa26b2017-12-12 18:03:23 -0800318 // We checked above that if the IP is defined, then the netmask is, too.
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700319 if let Some(tap_fd) = cfg.tap_fd {
320 // Safe because we ensure that we get a unique handle to the fd.
321 let tap = unsafe { Tap::from_raw_fd(validate_raw_fd(tap_fd)?) };
322 let net_box = Box::new(devices::virtio::Net::from(tap)
323 .map_err(|e| Error::NetDeviceNew(e))?);
324
325 let jail = if cfg.multiprocess {
326 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("net_device.policy");
327
328 Some(create_base_minijail(empty_root_path, &policy_path)?)
329 } else {
330 None
331 };
332
Dylan Reid059a1882018-07-23 17:58:09 -0700333 devs.push(VirtioDeviceStub {dev: net_box, jail});
Chirantan Ekbote5f787212018-05-31 15:31:31 -0700334 } else if let Some(host_ip) = cfg.host_ip {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800335 if let Some(netmask) = cfg.netmask {
Stephen Barber308ff602018-02-13 22:47:07 -0800336 if let Some(mac_address) = cfg.mac_address {
337 let net_box: Box<devices::virtio::VirtioDevice> = if cfg.vhost_net {
338 Box::new(devices::virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new(host_ip,
339 netmask,
340 mac_address,
341 &mem)
342 .map_err(|e| Error::VhostNetDeviceNew(e))?)
Zach Reizner39aa26b2017-12-12 18:03:23 -0800343 } else {
Stephen Barber308ff602018-02-13 22:47:07 -0800344 Box::new(devices::virtio::Net::<Tap>::new(host_ip, netmask, mac_address)
345 .map_err(|e| Error::NetDeviceNew(e))?)
Zach Reizner39aa26b2017-12-12 18:03:23 -0800346 };
347
Stephen Barber308ff602018-02-13 22:47:07 -0800348 let jail = if cfg.multiprocess {
349 let policy_path: PathBuf = if cfg.vhost_net {
350 cfg.seccomp_policy_dir.join("vhost_net_device.policy")
351 } else {
352 cfg.seccomp_policy_dir.join("net_device.policy")
353 };
Zach Reizner39aa26b2017-12-12 18:03:23 -0800354
Stephen Barber308ff602018-02-13 22:47:07 -0800355 Some(create_base_minijail(empty_root_path, &policy_path)?)
356 } else {
357 None
358 };
359
Dylan Reid059a1882018-07-23 17:58:09 -0700360 devs.push(VirtioDeviceStub {dev: net_box, jail});
Stephen Barber308ff602018-02-13 22:47:07 -0800361 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800362 }
363 }
364
Zach Reizner8fb52112017-12-13 16:04:39 -0800365 if let Some(wayland_socket_path) = cfg.wayland_socket_path.as_ref() {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800366 let jailed_wayland_path = Path::new("/wayland-0");
367
Zach Reizner39aa26b2017-12-12 18:03:23 -0800368 let wl_box = Box::new(devices::virtio::Wl::new(if cfg.multiprocess {
Zach Reizner8fb52112017-12-13 16:04:39 -0800369 &jailed_wayland_path
370 } else {
371 wayland_socket_path.as_path()
372 },
Dylan Reid059a1882018-07-23 17:58:09 -0700373 wayland_device_socket)
Zach Reizner8fb52112017-12-13 16:04:39 -0800374 .map_err(Error::WaylandDeviceNew)?);
Zach Reizner39aa26b2017-12-12 18:03:23 -0800375
376 let jail = if cfg.multiprocess {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800377 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("wl_device.policy");
Chirantan Ekbote293c61c2018-01-04 16:19:17 -0800378 let mut jail = create_base_minijail(empty_root_path, &policy_path)?;
379
380 // Create a tmpfs in the device's root directory so that we can bind mount the
381 // wayland socket into it. The size=67108864 is size=64*1024*1024 or size=64MB.
382 jail.mount_with_data(Path::new("none"), Path::new("/"), "tmpfs",
383 (libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize,
384 "size=67108864")
385 .unwrap();
Zach Reizner39aa26b2017-12-12 18:03:23 -0800386
387 // Bind mount the wayland socket into jail's root. This is necessary since each
388 // new wayland context must open() the socket.
389 jail.mount_bind(wayland_socket_path.as_path(), jailed_wayland_path, true)
390 .unwrap();
391
392 // Set the uid/gid for the jailed process, and give a basic id map. This
393 // is required for the above bind mount to work.
Zach Reizner39aa26b2017-12-12 18:03:23 -0800394 let crosvm_user_group = CStr::from_bytes_with_nul(b"crosvm\0").unwrap();
395 let crosvm_uid = match get_user_id(&crosvm_user_group) {
396 Ok(u) => u,
397 Err(e) => {
398 warn!("falling back to current user id for Wayland: {:?}", e);
399 geteuid()
400 }
401 };
Chirantan Ekbote0ba70d82018-01-24 13:47:58 -0800402 let crosvm_gid = match get_group_id(&crosvm_user_group) {
403 Ok(u) => u,
404 Err(e) => {
405 warn!("falling back to current group id for Wayland: {:?}", e);
406 getegid()
407 }
408 };
Zach Reizner39aa26b2017-12-12 18:03:23 -0800409 jail.change_uid(crosvm_uid);
Chirantan Ekbote0ba70d82018-01-24 13:47:58 -0800410 jail.change_gid(crosvm_gid);
Zach Reizner39aa26b2017-12-12 18:03:23 -0800411 jail.uidmap(&format!("{0} {0} 1", crosvm_uid))
412 .map_err(Error::SettingUidMap)?;
Chirantan Ekbote0ba70d82018-01-24 13:47:58 -0800413 jail.gidmap(&format!("{0} {0} 1", crosvm_gid))
Zach Reizner39aa26b2017-12-12 18:03:23 -0800414 .map_err(Error::SettingGidMap)?;
415
Zach Reizner39aa26b2017-12-12 18:03:23 -0800416 Some(jail)
417 } else {
418 None
419 };
Dylan Reid059a1882018-07-23 17:58:09 -0700420 devs.push(VirtioDeviceStub {
421 dev: wl_box,
422 jail,
423 });
Zach Reizner39aa26b2017-12-12 18:03:23 -0800424 }
425
426 if let Some(cid) = cfg.cid {
Zach Reizner8fb52112017-12-13 16:04:39 -0800427 let vsock_box = Box::new(devices::virtio::vhost::Vsock::new(cid, &mem)
428 .map_err(Error::VhostVsockDeviceNew)?);
Zach Reizner39aa26b2017-12-12 18:03:23 -0800429
430 let jail = if cfg.multiprocess {
431 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("vhost_vsock_device.policy");
432
433 Some(create_base_minijail(empty_root_path, &policy_path)?)
434 } else {
435 None
436 };
437
Dylan Reid059a1882018-07-23 17:58:09 -0700438 devs.push(VirtioDeviceStub {dev: vsock_box, jail});
Zach Reizner39aa26b2017-12-12 18:03:23 -0800439 }
440
Zach Reizner3a8100a2017-09-13 19:15:43 -0700441 #[cfg(feature = "gpu")]
442 {
443 if cfg.gpu {
444 let gpu_box =
445 Box::new(devices::virtio::Gpu::new(_exit_evt
446 .try_clone()
447 .map_err(Error::CloneEventFd)?));
Dylan Reid059a1882018-07-23 17:58:09 -0700448 let jail = if cfg.multiprocess {
Zach Reizner3a8100a2017-09-13 19:15:43 -0700449 error!("jail for virtio-gpu is unimplemented");
450 unimplemented!();
451 } else {
452 None
453 };
Dylan Reid059a1882018-07-23 17:58:09 -0700454 devs.push(VirtioDeviceStub {dev: gpu_box, jail});
Zach Reizner3a8100a2017-09-13 19:15:43 -0700455 }
456 }
457
Chirantan Ekboteebd56812018-04-16 19:32:04 -0700458 let chronos_user_group = CStr::from_bytes_with_nul(b"chronos\0").unwrap();
459 let chronos_uid = match get_user_id(&chronos_user_group) {
460 Ok(u) => u,
461 Err(e) => {
462 warn!("falling back to current user id for 9p: {:?}", e);
463 geteuid()
464 }
465 };
466 let chronos_gid = match get_group_id(&chronos_user_group) {
467 Ok(u) => u,
468 Err(e) => {
469 warn!("falling back to current group id for 9p: {:?}", e);
470 getegid()
471 }
472 };
473
474 for &(ref src, ref tag) in &cfg.shared_dirs {
475 let (jail, root) = if cfg.multiprocess {
476 let policy_path: PathBuf = cfg.seccomp_policy_dir.join("9p_device.policy");
477 let mut jail = create_base_minijail(empty_root_path, &policy_path)?;
478
479 // The shared directory becomes the root of the device's file system.
480 let root = Path::new("/");
481 jail.mount_bind(&src, root, true).unwrap();
482
483 // Set the uid/gid for the jailed process, and give a basic id map. This
484 // is required for the above bind mount to work.
485 jail.change_uid(chronos_uid);
486 jail.change_gid(chronos_gid);
487 jail.uidmap(&format!("{0} {0} 1", chronos_uid))
488 .map_err(Error::SettingUidMap)?;
489 jail.gidmap(&format!("{0} {0} 1", chronos_gid))
490 .map_err(Error::SettingGidMap)?;
491
492 (Some(jail), root)
493 } else {
494 // There's no bind mount so we tell the server to treat the source directory as the
495 // root. The double deref here converts |src| from a &PathBuf into a &Path.
496 (None, &**src)
497 };
498
499 let p9_box = Box::new(devices::virtio::P9::new(root, tag).map_err(Error::P9DeviceNew)?);
500
Dylan Reid059a1882018-07-23 17:58:09 -0700501 devs.push(VirtioDeviceStub {dev: p9_box, jail});
Chirantan Ekboteebd56812018-04-16 19:32:04 -0700502 }
503
Dylan Reid059a1882018-07-23 17:58:09 -0700504 Ok(devs)
Sonny Raobb7da422018-02-13 20:37:48 -0800505}
506
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100507fn setup_vcpu_signal_handler() -> Result<()> {
508 unsafe {
509 extern "C" fn handle_signal() {}
510 // Our signal handler does nothing and is trivially async signal safe.
511 register_signal_handler(SIGRTMIN() + 0, handle_signal)
512 .map_err(Error::RegisterSignalHandler)?;
513 }
514 block_signal(SIGRTMIN() + 0).map_err(Error::BlockSignal)?;
515 Ok(())
516}
517
Sonny Raobb7da422018-02-13 20:37:48 -0800518fn run_vcpu(vcpu: Vcpu,
519 cpu_id: u32,
520 start_barrier: Arc<Barrier>,
521 io_bus: devices::Bus,
522 mmio_bus: devices::Bus,
523 exit_evt: EventFd,
524 kill_signaled: Arc<AtomicBool>) -> Result<JoinHandle<()>> {
Zach Reizner8fb52112017-12-13 16:04:39 -0800525 thread::Builder::new()
526 .name(format!("crosvm_vcpu{}", cpu_id))
527 .spawn(move || {
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100528 let mut sig_ok = true;
529 match get_blocked_signals() {
530 Ok(mut v) => {
531 v.retain(|&x| x != SIGRTMIN() + 0);
532 if let Err(e) = vcpu.set_signal_mask(&v) {
533 error!(
534 "Failed to set the KVM_SIGNAL_MASK for vcpu {} : {:?}",
535 cpu_id, e
536 );
537 sig_ok = false;
538 }
539 }
540 Err(e) => {
541 error!(
542 "Failed to retrieve signal mask for vcpu {} : {:?}",
543 cpu_id, e
544 );
545 sig_ok = false;
546 }
547 };
Zach Reizner39aa26b2017-12-12 18:03:23 -0800548
Zach Reizner8fb52112017-12-13 16:04:39 -0800549 start_barrier.wait();
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100550
551 while sig_ok {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800552 let run_res = vcpu.run();
553 match run_res {
554 Ok(run) => {
555 match run {
556 VcpuExit::IoIn(addr, data) => {
557 io_bus.read(addr as u64, data);
558 }
559 VcpuExit::IoOut(addr, data) => {
560 io_bus.write(addr as u64, data);
561 }
562 VcpuExit::MmioRead(addr, data) => {
563 mmio_bus.read(addr, data);
564 }
565 VcpuExit::MmioWrite(addr, data) => {
566 mmio_bus.write(addr, data);
567 }
568 VcpuExit::Hlt => break,
569 VcpuExit::Shutdown => break,
Sonny Rao6ce158f2018-03-27 17:12:58 -0700570 VcpuExit::SystemEvent(_, _) =>
571 //TODO handle reboot and crash events
572 kill_signaled.store(true, Ordering::SeqCst),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800573 r => warn!("unexpected vcpu exit: {:?}", r),
574 }
575 }
576 Err(e) => {
577 match e.errno() {
578 libc::EAGAIN | libc::EINTR => {},
579 _ => {
580 error!("vcpu hit unknown error: {:?}", e);
581 break;
582 }
583 }
584 }
585 }
586 if kill_signaled.load(Ordering::SeqCst) {
587 break;
588 }
Mark Ryan6ed5aea2018-04-20 13:52:35 +0100589
590 // Try to clear the signal that we use to kick VCPU if it is
591 // pending before attempting to handle pause requests.
592 clear_signal(SIGRTMIN() + 0).expect("failed to clear pending signal");
Zach Reizner39aa26b2017-12-12 18:03:23 -0800593 }
Zach Reizner8fb52112017-12-13 16:04:39 -0800594 exit_evt
Zach Reizner39aa26b2017-12-12 18:03:23 -0800595 .write(1)
596 .expect("failed to signal vcpu exit eventfd");
Zach Reizner8fb52112017-12-13 16:04:39 -0800597 })
598 .map_err(Error::SpawnVcpu)
Zach Reizner39aa26b2017-12-12 18:03:23 -0800599}
600
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700601// Reads the contents of a file and converts them into a u64.
602fn file_to_u64<P: AsRef<Path>>(path: P) -> io::Result<u64> {
603 let mut file = File::open(path)?;
604
605 let mut buf = [0u8; 32];
606 let count = file.read(&mut buf)?;
607
608 let content = str::from_utf8(&buf[..count])
609 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
610 content.trim().parse().map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
611}
612
Dylan Reid059a1882018-07-23 17:58:09 -0700613pub fn run_config(cfg: Config) -> Result<()> {
614 if cfg.virtio_dev_info.multiprocess {
615 // Printing something to the syslog before entering minijail so that libc's syslogger has a
616 // chance to open files necessary for its operation, like `/etc/localtime`. After jailing,
617 // access to those files will not be possible.
618 info!("crosvm entering multiprocess mode");
619 }
620
621 let pci_devices = devices::PciDeviceList::new();
622
623 // Masking signals is inherently dangerous, since this can persist across clones/execs. Do this
624 // before any jailed devices have been spawned, so that we can catch any of them that fail very
625 // quickly.
626 let sigchld_fd = SignalFd::new(libc::SIGCHLD).map_err(Error::CreateSignalFd)?;
627
628 let components = VmComponents {
629 pci_devices,
630 memory_mb: (cfg.memory.unwrap_or(256) << 20) as u64,
631 vcpu_count: cfg.vcpu_count.unwrap_or(1),
632 kernel_image: File::open(cfg.kernel_path.as_path())
633 .map_err(|e| Error::OpenKernel(cfg.kernel_path.clone(), e))?,
634 extra_kernel_params: cfg.params,
635 wayland_dmabuf: cfg.virtio_dev_info.wayland_dmabuf,
636 };
637
638 let mut control_sockets = Vec::new();
639 if let Some(ref path_string) = cfg.socket_path {
640 let path = Path::new(path_string);
641 let dgram = UnixDatagram::bind(path).map_err(Error::CreateSocket)?;
642 control_sockets.push(UnlinkUnixDatagram(dgram));
643 };
644 let (wayland_host_socket, wayland_device_socket) = UnixDatagram::pair()
645 .map_err(Error::CreateSocket)?;
646 control_sockets.push(UnlinkUnixDatagram(wayland_host_socket));
647 // Balloon gets a special socket so balloon requests can be forwarded from the main process.
648 let (balloon_host_socket, balloon_device_socket) = UnixDatagram::pair()
649 .map_err(Error::CreateSocket)?;
650
651 let virtio_dev_info = cfg.virtio_dev_info;
652 let linux = Arch::build_vm(components,
653 |m, e| create_virtio_devs(virtio_dev_info, m, e,
654 wayland_device_socket,
655 balloon_device_socket))
656 .map_err(Error::BuildingVm)?;
657 run_control(linux, control_sockets, balloon_host_socket, sigchld_fd)
Dylan Reid0ed91ab2018-05-31 15:42:18 -0700658}
659
Dylan Reid059a1882018-07-23 17:58:09 -0700660fn run_control(mut linux: RunnableLinuxVm,
661 control_sockets: Vec<UnlinkUnixDatagram>,
662 balloon_host_socket: UnixDatagram,
663 sigchld_fd: SignalFd) -> Result<()> {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800664 const MAX_VM_FD_RECV: usize = 1;
665
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700666 // Paths to get the currently available memory and the low memory threshold.
667 const LOWMEM_MARGIN: &'static str = "/sys/kernel/mm/chromeos-low_mem/margin";
668 const LOWMEM_AVAILABLE: &'static str = "/sys/kernel/mm/chromeos-low_mem/available";
669
670 // The amount of additional memory to claim back from the VM whenever the system is
671 // low on memory.
672 const ONE_GB: u64 = (1 << 30);
673
Dylan Reid0ed91ab2018-05-31 15:42:18 -0700674 let max_balloon_memory = match linux.vm.get_memory().memory_size() {
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700675 // If the VM has at least 1.5 GB, the balloon driver can consume all but the last 1 GB.
676 n if n >= (ONE_GB / 2) * 3 => n - ONE_GB,
677 // Otherwise, if the VM has at least 500MB the balloon driver will consume at most
678 // half of it.
679 n if n >= (ONE_GB / 2) => n / 2,
680 // Otherwise, the VM is too small for us to take memory away from it.
681 _ => 0,
682 };
683 let mut current_balloon_memory: u64 = 0;
684 let balloon_memory_increment: u64 = max_balloon_memory / 16;
685
Zach Reizner5bed0d22018-03-28 02:31:11 -0700686 #[derive(PollToken)]
687 enum Token {
688 Exit,
689 Stdin,
690 ChildSignal,
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700691 CheckAvailableMemory,
692 LowMemory,
693 LowmemTimer,
Zach Reizner5bed0d22018-03-28 02:31:11 -0700694 VmControl { index: usize },
695 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800696
697 let stdin_handle = stdin();
698 let stdin_lock = stdin_handle.lock();
699 stdin_lock
700 .set_raw_mode()
701 .expect("failed to set terminal raw mode");
702
Zach Reizner5bed0d22018-03-28 02:31:11 -0700703 let poll_ctx = PollContext::new().map_err(Error::CreatePollContext)?;
Dylan Reid0ed91ab2018-05-31 15:42:18 -0700704 poll_ctx.add(&linux.exit_evt, Token::Exit).map_err(Error::PollContextAdd)?;
Zach Reizner5bed0d22018-03-28 02:31:11 -0700705 if let Err(e) = poll_ctx.add(&stdin_handle, Token::Stdin) {
706 warn!("failed to add stdin to poll context: {:?}", e);
707 }
Dylan Reid059a1882018-07-23 17:58:09 -0700708 poll_ctx.add(&sigchld_fd, Token::ChildSignal).map_err(Error::PollContextAdd)?;
709 for (index, socket) in control_sockets.iter().enumerate() {
Zach Reizner5bed0d22018-03-28 02:31:11 -0700710 poll_ctx.add(socket.as_ref(), Token::VmControl{ index }).map_err(Error::PollContextAdd)?;
Zach Reizner39aa26b2017-12-12 18:03:23 -0800711 }
712
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700713 // Watch for low memory notifications and take memory back from the VM.
Dylan Reidf11e6ed2018-07-31 10:24:06 -0700714 let low_mem = File::open("/dev/chromeos-low-mem").ok();
715 if let Some(ref low_mem) = low_mem {
716 poll_ctx.add(low_mem, Token::LowMemory).map_err(Error::PollContextAdd)?;
717 } else {
718 warn!("Unable to open low mem indicator, maybe not a chrome os kernel");
719 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700720
721 // Used to rate limit balloon requests.
722 let mut lowmem_timer = TimerFd::new().map_err(Error::CreateTimerFd)?;
723 poll_ctx.add(&lowmem_timer, Token::LowmemTimer).map_err(Error::PollContextAdd)?;
724
725 // Used to check whether it's ok to start giving memory back to the VM.
726 let mut freemem_timer = TimerFd::new().map_err(Error::CreateTimerFd)?;
727 poll_ctx.add(&freemem_timer, Token::CheckAvailableMemory).map_err(Error::PollContextAdd)?;
728
729 // Used to add jitter to timer values so that we don't have a thundering herd problem when
730 // multiple VMs are running.
731 let mut rng = thread_rng();
732 let lowmem_jitter_ms = Range::new(0, 200);
733 let freemem_jitter_secs = Range::new(0, 12);
734 let interval_jitter_secs = Range::new(0, 6);
735
Dylan Reid059a1882018-07-23 17:58:09 -0700736 let mut vcpu_handles = Vec::with_capacity(linux.vcpus.len() as usize);
737 let vcpu_thread_barrier = Arc::new(Barrier::new((linux.vcpus.len() + 1) as usize));
738 let kill_signaled = Arc::new(AtomicBool::new(false));
739 setup_vcpu_signal_handler()?;
740 for (cpu_id, vcpu) in linux.vcpus.into_iter().enumerate() {
741 let handle = run_vcpu(vcpu,
742 cpu_id as u32,
743 vcpu_thread_barrier.clone(),
744 linux.io_bus.clone(),
745 linux.mmio_bus.clone(),
746 linux.exit_evt.try_clone().map_err(Error::CloneEventFd)?,
747 kill_signaled.clone())?;
748 vcpu_handles.push(handle);
749 }
750 vcpu_thread_barrier.wait();
751
Zach Reizner39aa26b2017-12-12 18:03:23 -0800752 let mut scm = Scm::new(MAX_VM_FD_RECV);
753
754 'poll: loop {
Zach Reizner5bed0d22018-03-28 02:31:11 -0700755 let events = {
756 match poll_ctx.wait() {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800757 Ok(v) => v,
758 Err(e) => {
759 error!("failed to poll: {:?}", e);
760 break;
761 }
762 }
763 };
Zach Reizner5bed0d22018-03-28 02:31:11 -0700764 for event in events.iter_readable() {
765 match event.token() {
766 Token::Exit => {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800767 info!("vcpu requested shutdown");
768 break 'poll;
769 }
Zach Reizner5bed0d22018-03-28 02:31:11 -0700770 Token::Stdin => {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800771 let mut out = [0u8; 64];
772 match stdin_lock.read_raw(&mut out[..]) {
773 Ok(0) => {
774 // Zero-length read indicates EOF. Remove from pollables.
Zach Reizner5bed0d22018-03-28 02:31:11 -0700775 let _ = poll_ctx.delete(&stdin_handle);
Zach Reizner39aa26b2017-12-12 18:03:23 -0800776 },
777 Err(e) => {
778 warn!("error while reading stdin: {:?}", e);
Zach Reizner5bed0d22018-03-28 02:31:11 -0700779 let _ = poll_ctx.delete(&stdin_handle);
Zach Reizner39aa26b2017-12-12 18:03:23 -0800780 },
781 Ok(count) => {
Dylan Reid0ed91ab2018-05-31 15:42:18 -0700782 linux.stdio_serial
Zach Reizner39aa26b2017-12-12 18:03:23 -0800783 .lock()
784 .unwrap()
785 .queue_input_bytes(&out[..count])
786 .expect("failed to queue bytes into serial port");
787 },
788 }
789 }
Zach Reizner5bed0d22018-03-28 02:31:11 -0700790 Token::ChildSignal => {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800791 // Print all available siginfo structs, then exit the loop.
792 loop {
Dylan Reid059a1882018-07-23 17:58:09 -0700793 let result = sigchld_fd.read().map_err(Error::SignalFd)?;
Zach Reizner39aa26b2017-12-12 18:03:23 -0800794 if let Some(siginfo) = result {
795 error!("child {} died: signo {}, status {}, code {}",
796 siginfo.ssi_pid,
797 siginfo.ssi_signo,
798 siginfo.ssi_status,
799 siginfo.ssi_code);
800 }
801 break 'poll;
802 }
803 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700804 Token::CheckAvailableMemory => {
805 // Acknowledge the timer.
806 freemem_timer.wait().map_err(Error::TimerFd)?;
807 if current_balloon_memory == 0 {
808 // Nothing to see here.
809 if let Err(e) = freemem_timer.clear() {
810 warn!("unable to clear available memory check timer: {}", e);
811 }
812 continue;
813 }
814
815 // Otherwise see if we can free up some memory.
816 let margin = file_to_u64(LOWMEM_MARGIN).map_err(Error::ReadLowmemMargin)?;
817 let available = file_to_u64(LOWMEM_AVAILABLE).map_err(Error::ReadLowmemAvailable)?;
818
819 // `available` and `margin` are specified in MB while `balloon_memory_increment` is in
820 // bytes. So to correctly compare them we need to turn the increment value into MB.
821 if available >= margin + 2*(balloon_memory_increment >> 20) {
822 current_balloon_memory = if current_balloon_memory >= balloon_memory_increment {
823 current_balloon_memory - balloon_memory_increment
824 } else {
825 0
826 };
827 let mut buf = [0u8; mem::size_of::<u64>()];
828 LittleEndian::write_u64(&mut buf, current_balloon_memory);
Dylan Reid059a1882018-07-23 17:58:09 -0700829 if let Err(e) = balloon_host_socket.send(&buf) {
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700830 warn!("failed to send memory value to balloon device: {}", e);
831 }
832 }
833 }
834 Token::LowMemory => {
Dylan Reidf11e6ed2018-07-31 10:24:06 -0700835 if let Some(ref low_mem) = low_mem {
836 let old_balloon_memory = current_balloon_memory;
837 current_balloon_memory =
838 min(current_balloon_memory + balloon_memory_increment,
839 max_balloon_memory);
840 if current_balloon_memory != old_balloon_memory {
841 let mut buf = [0u8; mem::size_of::<u64>()];
842 LittleEndian::write_u64(&mut buf, current_balloon_memory);
Dylan Reid059a1882018-07-23 17:58:09 -0700843 if let Err(e) = balloon_host_socket.send(&buf) {
Dylan Reidf11e6ed2018-07-31 10:24:06 -0700844 warn!("failed to send memory value to balloon device: {}", e);
845 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700846 }
Dylan Reidf11e6ed2018-07-31 10:24:06 -0700847
848 // Stop polling the lowmem device until the timer fires.
849 poll_ctx.delete(low_mem).map_err(Error::PollContextDelete)?;
850
851 // Add some jitter to the timer so that if there are multiple VMs running
852 // they don't all start ballooning at exactly the same time.
853 let lowmem_dur =
854 Duration::from_millis(1000 + lowmem_jitter_ms.ind_sample(&mut rng));
855 lowmem_timer.reset(lowmem_dur, None).map_err(Error::ResetTimerFd)?;
856
857 // Also start a timer to check when we can start giving memory back. Do the
858 // first check after a minute (with jitter) and subsequent checks after
859 // every 30 seconds (with jitter).
860 let freemem_dur =
861 Duration::from_secs(60 + freemem_jitter_secs.ind_sample(&mut rng));
862 let freemem_int =
863 Duration::from_secs(30 + interval_jitter_secs.ind_sample(&mut rng));
864 freemem_timer
865 .reset(freemem_dur, Some(freemem_int))
866 .map_err(Error::ResetTimerFd)?;
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700867 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700868 }
869 Token::LowmemTimer => {
870 // Acknowledge the timer.
871 lowmem_timer.wait().map_err(Error::TimerFd)?;
872
Dylan Reidf11e6ed2018-07-31 10:24:06 -0700873 if let Some(ref low_mem) = low_mem {
874 // Start polling the lowmem device again.
875 poll_ctx.add(low_mem, Token::LowMemory).map_err(Error::PollContextAdd)?;
876 }
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700877 }
Zach Reizner5bed0d22018-03-28 02:31:11 -0700878 Token::VmControl { index } => {
Dylan Reid059a1882018-07-23 17:58:09 -0700879 if let Some(socket) = control_sockets.get(index as usize) {
Zach Reizner5bed0d22018-03-28 02:31:11 -0700880 match VmRequest::recv(&mut scm, socket.as_ref()) {
881 Ok(request) => {
882 let mut running = true;
883 let response =
Dylan Reid0ed91ab2018-05-31 15:42:18 -0700884 request.execute(&mut linux.vm,
885 &mut linux.resources,
David Reveman52ba4e52018-04-22 21:42:09 -0400886 &mut running,
Dylan Reid059a1882018-07-23 17:58:09 -0700887 &balloon_host_socket);
Zach Reizner5bed0d22018-03-28 02:31:11 -0700888 if let Err(e) = response.send(&mut scm, socket.as_ref()) {
889 error!("failed to send VmResponse: {:?}", e);
890 }
891 if !running {
892 info!("control socket requested exit");
893 break 'poll;
894 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800895 }
Zach Reizner5bed0d22018-03-28 02:31:11 -0700896 Err(e) => error!("failed to recv VmRequest: {:?}", e),
Zach Reizner39aa26b2017-12-12 18:03:23 -0800897 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800898 }
899 }
Zach Reizner5bed0d22018-03-28 02:31:11 -0700900 }
901 }
902 for event in events.iter_hungup() {
903 // It's possible more data is readable and buffered while the socket is hungup, so
904 // don't delete the socket from the poll context until we're sure all the data is
905 // read.
906 if !event.readable() {
907 match event.token() {
908 Token::Exit => {},
909 Token::Stdin => {
910 let _ = poll_ctx.delete(&stdin_handle);
911 },
912 Token::ChildSignal => {},
Chirantan Ekbote448516e2018-07-24 16:07:42 -0700913 Token::CheckAvailableMemory => {},
914 Token::LowMemory => {},
915 Token::LowmemTimer => {},
Zach Reizner5bed0d22018-03-28 02:31:11 -0700916 Token::VmControl { index } => {
Dylan Reid059a1882018-07-23 17:58:09 -0700917 if let Some(socket) = control_sockets.get(index as usize) {
Zach Reizner5bed0d22018-03-28 02:31:11 -0700918 let _ = poll_ctx.delete(socket.as_ref());
919 }
920 },
921 }
Zach Reizner39aa26b2017-12-12 18:03:23 -0800922 }
923 }
924 }
925
926 // vcpu threads MUST see the kill signaled flag, otherwise they may
927 // re-enter the VM.
Dylan Reid059a1882018-07-23 17:58:09 -0700928 kill_signaled.store(true, Ordering::SeqCst);
929 for handle in vcpu_handles {
Dmitry Torokhovcd405332018-02-16 16:25:54 -0800930 match handle.kill(SIGRTMIN() + 0) {
Zach Reizner39aa26b2017-12-12 18:03:23 -0800931 Ok(_) => {
932 if let Err(e) = handle.join() {
933 error!("failed to join vcpu thread: {:?}", e);
934 }
935 }
936 Err(e) => error!("failed to kill vcpu thread: {:?}", e),
937 }
938 }
939
940 stdin_lock
941 .set_canon_mode()
942 .expect("failed to restore canonical mode for terminal");
943
944 Ok(())
945}