Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 15 | //! Implementation of the AIDL interface of the VirtualizationService. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 17 | use crate::composite::make_composite_image; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 18 | use crate::crosvm::{CrosvmConfig, DiskFile, PayloadState, VmInstance, VmState}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 19 | use crate::payload::add_microdroid_images; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 20 | use crate::{Cid, FIRST_GUEST_CID, SYSPROP_LAST_CID}; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 21 | use ::binder::unstable_api::AsNative; |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 22 | use android_os_permissions_aidl::aidl::android::os::IPermissionController; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 23 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 24 | DiskImage::DiskImage, |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 25 | IVirtualMachine::{BnVirtualMachine, IVirtualMachine}, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 26 | IVirtualMachineCallback::IVirtualMachineCallback, |
| 27 | IVirtualizationService::IVirtualizationService, |
| 28 | PartitionType::PartitionType, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 29 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 30 | VirtualMachineAppConfig::VirtualMachineAppConfig, |
| 31 | VirtualMachineConfig::VirtualMachineConfig, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 32 | VirtualMachineDebugInfo::VirtualMachineDebugInfo, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 33 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 34 | VirtualMachineState::VirtualMachineState, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 35 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 36 | use android_system_virtualizationservice::binder::{ |
Shikha Panwar | d8e3542 | 2021-10-11 13:51:27 +0000 | [diff] [blame] | 37 | self, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor, Status, StatusCode, Strong, |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 38 | ThreadState, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 39 | }; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 40 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::{ |
| 41 | IVirtualMachineService::{ |
| 42 | BnVirtualMachineService, IVirtualMachineService, VM_BINDER_SERVICE_PORT, |
| 43 | VM_STREAM_SERVICE_PORT, |
| 44 | }, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 45 | }; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 46 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 47 | use binder_common::{lazy_service::LazyServiceGuard, new_binder_exception}; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 48 | use disk::QcowFile; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 49 | use idsig::{HashAlgorithm, V4Signature}; |
| 50 | use log::{debug, error, info, warn}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 51 | use microdroid_payload_config::VmPayloadConfig; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 52 | use rustutils::system_properties; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 53 | use std::convert::TryInto; |
Shikha Panwar | d8e3542 | 2021-10-11 13:51:27 +0000 | [diff] [blame] | 54 | use std::ffi::CStr; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 55 | use std::fs::{create_dir, File, OpenOptions}; |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 56 | use std::io::{Error, ErrorKind, Write}; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 57 | use std::num::NonZeroU32; |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 58 | use std::os::unix::io::{FromRawFd, IntoRawFd}; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 59 | use std::path::{Path, PathBuf}; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 60 | use std::sync::{Arc, Mutex, Weak}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 61 | use vmconfig::VmConfig; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 62 | use vsock::{SockAddr, VsockListener, VsockStream}; |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 63 | use zip::ZipArchive; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 64 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 65 | pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice"; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 66 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 67 | /// Directory in which to write disk image files used while running VMs. |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 68 | pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice"; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 69 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 70 | /// The CID representing the host VM |
| 71 | const VMADDR_CID_HOST: u32 = 2; |
| 72 | |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 73 | /// The size of zero.img. |
| 74 | /// Gaps in composite disk images are filled with a shared zero.img. |
| 75 | const ZERO_FILLER_SIZE: u64 = 4096; |
| 76 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 77 | /// Magic string for the instance image |
| 78 | const ANDROID_VM_INSTANCE_MAGIC: &str = "Android-VM-instance"; |
| 79 | |
| 80 | /// Version of the instance image format |
| 81 | const ANDROID_VM_INSTANCE_VERSION: u16 = 1; |
| 82 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 83 | /// Implementation of `IVirtualizationService`, the entry point of the AIDL service. |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 84 | #[derive(Debug, Default)] |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 85 | pub struct VirtualizationService { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 86 | state: Arc<Mutex<State>>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Shikha Panwar | d8e3542 | 2021-10-11 13:51:27 +0000 | [diff] [blame] | 89 | impl Interface for VirtualizationService { |
| 90 | fn dump(&self, mut file: &File, _args: &[&CStr]) -> Result<(), StatusCode> { |
| 91 | check_permission("android.permission.DUMP").or(Err(StatusCode::PERMISSION_DENIED))?; |
| 92 | let state = &mut *self.state.lock().unwrap(); |
| 93 | let vms = state.vms(); |
| 94 | writeln!(file, "Running {0} VMs:", vms.len()).or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 95 | for vm in vms { |
| 96 | writeln!(file, "VM CID: {}", vm.cid).or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 97 | writeln!(file, "\tState: {:?}", vm.vm_state.lock().unwrap()) |
| 98 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 99 | writeln!(file, "\tPayload state {:?}", vm.payload_state()) |
| 100 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 101 | writeln!(file, "\tProtected: {}", vm.protected).or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 102 | writeln!(file, "\ttemporary_directory: {}", vm.temporary_directory.to_string_lossy()) |
| 103 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 104 | writeln!(file, "\trequester_uid: {}", vm.requester_uid) |
| 105 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 106 | writeln!(file, "\trequester_sid: {}", vm.requester_sid) |
| 107 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 108 | writeln!(file, "\trequester_debug_pid: {}", vm.requester_debug_pid) |
| 109 | .or(Err(StatusCode::UNKNOWN_ERROR))?; |
| 110 | } |
| 111 | Ok(()) |
| 112 | } |
| 113 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 114 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 115 | impl IVirtualizationService for VirtualizationService { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 116 | /// Creates (but does not start) a new VM with the given configuration, assigning it the next |
| 117 | /// available CID. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 118 | /// |
| 119 | /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client. |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 120 | fn createVm( |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 121 | &self, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 122 | config: &VirtualMachineConfig, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 123 | log_fd: Option<&ParcelFileDescriptor>, |
| 124 | ) -> binder::Result<Strong<dyn IVirtualMachine>> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 125 | check_manage_access()?; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 126 | let state = &mut *self.state.lock().unwrap(); |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 127 | let mut log_fd = log_fd.map(clone_file).transpose()?; |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 128 | let requester_uid = ThreadState::get_calling_uid(); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 129 | let requester_sid = get_calling_sid()?; |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 130 | let requester_debug_pid = ThreadState::get_calling_pid(); |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 131 | let cid = next_cid().or(Err(ExceptionCode::ILLEGAL_STATE))?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 132 | |
| 133 | // Counter to generate unique IDs for temporary image files. |
| 134 | let mut next_temporary_image_id = 0; |
| 135 | // Files which are referred to from composite images. These must be mapped to the crosvm |
| 136 | // child process, and not closed before it is started. |
| 137 | let mut indirect_files = vec![]; |
| 138 | |
| 139 | // Make directory for temporary files. |
| 140 | let temporary_directory: PathBuf = format!("{}/{}", TEMPORARY_DIRECTORY, cid).into(); |
| 141 | create_dir(&temporary_directory).map_err(|e| { |
| 142 | error!( |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 143 | "Failed to create temporary directory {:?} for VM files: {}", |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 144 | temporary_directory, e |
| 145 | ); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 146 | new_binder_exception( |
| 147 | ExceptionCode::SERVICE_SPECIFIC, |
| 148 | format!( |
| 149 | "Failed to create temporary directory {:?} for VM files: {}", |
| 150 | temporary_directory, e |
| 151 | ), |
| 152 | ) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 153 | })?; |
| 154 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 155 | // Disable console logging if debug level != full. Note that kernel anyway doesn't use the |
| 156 | // console output when debug level != full. So, users won't be able to see the kernel |
| 157 | // output even without this overriding. This is to silence output from the bootloader which |
| 158 | // doesn't understand the bootconfig parameters. |
| 159 | if let VirtualMachineConfig::AppConfig(config) = config { |
| 160 | if config.debugLevel != DebugLevel::FULL { |
| 161 | log_fd = None; |
| 162 | } |
| 163 | } |
| 164 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 165 | let config = match config { |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 166 | VirtualMachineConfig::AppConfig(config) => BorrowedOrOwned::Owned( |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 167 | load_app_config(config, &temporary_directory).map_err(|e| { |
| 168 | error!("Failed to load app config from {}: {}", &config.configPath, e); |
| 169 | new_binder_exception( |
| 170 | ExceptionCode::SERVICE_SPECIFIC, |
| 171 | format!("Failed to load app config from {}: {}", &config.configPath, e), |
| 172 | ) |
| 173 | })?, |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 174 | ), |
| 175 | VirtualMachineConfig::RawConfig(config) => BorrowedOrOwned::Borrowed(config), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 176 | }; |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 177 | let config = config.as_ref(); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 178 | |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 179 | let zero_filler_path = temporary_directory.join("zero.img"); |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 180 | write_zero_filler(&zero_filler_path).map_err(|e| { |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 181 | error!("Failed to make composite image: {}", e); |
| 182 | new_binder_exception( |
| 183 | ExceptionCode::SERVICE_SPECIFIC, |
| 184 | format!("Failed to make composite image: {}", e), |
| 185 | ) |
| 186 | })?; |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 187 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 188 | // Assemble disk images if needed. |
| 189 | let disks = config |
| 190 | .disks |
| 191 | .iter() |
| 192 | .map(|disk| { |
| 193 | assemble_disk_image( |
| 194 | disk, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 195 | &zero_filler_path, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 196 | &temporary_directory, |
| 197 | &mut next_temporary_image_id, |
| 198 | &mut indirect_files, |
| 199 | ) |
| 200 | }) |
| 201 | .collect::<Result<Vec<DiskFile>, _>>()?; |
| 202 | |
| 203 | // Actually start the VM. |
| 204 | let crosvm_config = CrosvmConfig { |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 205 | cid, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 206 | bootloader: maybe_clone_file(&config.bootloader)?, |
| 207 | kernel: maybe_clone_file(&config.kernel)?, |
| 208 | initrd: maybe_clone_file(&config.initrd)?, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 209 | disks, |
| 210 | params: config.params.to_owned(), |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 211 | protected: config.protectedVm, |
| 212 | memory_mib: config.memoryMib.try_into().ok().and_then(NonZeroU32::new), |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 213 | log_fd, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 214 | indirect_files, |
| 215 | }; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 216 | let instance = Arc::new( |
| 217 | VmInstance::new( |
| 218 | crosvm_config, |
| 219 | temporary_directory, |
| 220 | requester_uid, |
| 221 | requester_sid, |
| 222 | requester_debug_pid, |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 223 | ) |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 224 | .map_err(|e| { |
| 225 | error!("Failed to create VM with config {:?}: {}", config, e); |
| 226 | new_binder_exception( |
| 227 | ExceptionCode::SERVICE_SPECIFIC, |
| 228 | format!("Failed to create VM: {}", e), |
| 229 | ) |
| 230 | })?, |
| 231 | ); |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 232 | state.add_vm(Arc::downgrade(&instance)); |
| 233 | Ok(VirtualMachine::create(instance)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 234 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 235 | |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 236 | /// Initialise an empty partition image of the given size to be used as a writable partition. |
| 237 | fn initializeWritablePartition( |
| 238 | &self, |
| 239 | image_fd: &ParcelFileDescriptor, |
| 240 | size: i64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 241 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 242 | ) -> binder::Result<()> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 243 | check_manage_access()?; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 244 | let size = size.try_into().map_err(|e| { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 245 | new_binder_exception( |
| 246 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 247 | format!("Invalid size {}: {}", size, e), |
| 248 | ) |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 249 | })?; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 250 | let image = clone_file(image_fd)?; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 251 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 252 | let mut part = QcowFile::new(image, size).map_err(|e| { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 253 | new_binder_exception( |
| 254 | ExceptionCode::SERVICE_SPECIFIC, |
| 255 | format!("Failed to create QCOW2 image: {}", e), |
| 256 | ) |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 257 | })?; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 258 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 259 | match partition_type { |
| 260 | PartitionType::RAW => Ok(()), |
| 261 | PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut part), |
| 262 | _ => Err(Error::new( |
| 263 | ErrorKind::Unsupported, |
| 264 | format!("Unsupported partition type {:?}", partition_type), |
| 265 | )), |
| 266 | } |
| 267 | .map_err(|e| { |
| 268 | new_binder_exception( |
| 269 | ExceptionCode::SERVICE_SPECIFIC, |
| 270 | format!("Failed to initialize partition as {:?}: {}", partition_type, e), |
| 271 | ) |
| 272 | })?; |
| 273 | |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 274 | Ok(()) |
| 275 | } |
| 276 | |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 277 | /// Creates or update the idsig file by digesting the input APK file. |
| 278 | fn createOrUpdateIdsigFile( |
| 279 | &self, |
| 280 | input_fd: &ParcelFileDescriptor, |
| 281 | idsig_fd: &ParcelFileDescriptor, |
| 282 | ) -> binder::Result<()> { |
| 283 | // TODO(b/193504400): do this only when (1) idsig_fd is empty or (2) the APK digest in |
| 284 | // idsig_fd is different from APK digest in input_fd |
| 285 | |
| 286 | let mut input = clone_file(input_fd)?; |
| 287 | let mut sig = V4Signature::create(&mut input, 4096, &[], HashAlgorithm::SHA256).unwrap(); |
| 288 | |
| 289 | let mut output = clone_file(idsig_fd)?; |
| 290 | output.set_len(0).unwrap(); |
| 291 | sig.write_into(&mut output).unwrap(); |
| 292 | Ok(()) |
| 293 | } |
| 294 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 295 | /// Get a list of all currently running VMs. This method is only intended for debug purposes, |
| 296 | /// and as such is only permitted from the shell user. |
| 297 | fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 298 | check_debug_access()?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 299 | |
| 300 | let state = &mut *self.state.lock().unwrap(); |
| 301 | let vms = state.vms(); |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 302 | let cids = vms |
| 303 | .into_iter() |
| 304 | .map(|vm| VirtualMachineDebugInfo { |
| 305 | cid: vm.cid as i32, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 306 | temporaryDirectory: vm.temporary_directory.to_string_lossy().to_string(), |
Andrew Walbran | 1ef19ae | 2021-04-07 11:31:57 +0000 | [diff] [blame] | 307 | requesterUid: vm.requester_uid as i32, |
| 308 | requesterSid: vm.requester_sid.clone(), |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 309 | requesterPid: vm.requester_debug_pid, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 310 | state: get_state(&vm), |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 311 | }) |
| 312 | .collect(); |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 313 | Ok(cids) |
| 314 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 315 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 316 | /// Hold a strong reference to a VM in VirtualizationService. This method is only intended for |
| 317 | /// debug purposes, and as such is only permitted from the shell user. |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 318 | fn debugHoldVmRef(&self, vmref: &Strong<dyn IVirtualMachine>) -> binder::Result<()> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 319 | check_debug_access()?; |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 320 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 321 | let state = &mut *self.state.lock().unwrap(); |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 322 | state.debug_hold_vm(vmref.clone()); |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 323 | Ok(()) |
| 324 | } |
| 325 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 326 | /// Drop reference to a VM that is being held by VirtualizationService. Returns the reference if |
| 327 | /// the VM was found and None otherwise. This method is only intended for debug purposes, and as |
| 328 | /// such is only permitted from the shell user. |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 329 | fn debugDropVmRef(&self, cid: i32) -> binder::Result<Option<Strong<dyn IVirtualMachine>>> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 330 | check_debug_access()?; |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 331 | |
| 332 | let state = &mut *self.state.lock().unwrap(); |
| 333 | Ok(state.debug_drop_vm(cid)) |
| 334 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 337 | impl VirtualizationService { |
| 338 | pub fn init() -> VirtualizationService { |
| 339 | let service = VirtualizationService::default(); |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 340 | |
| 341 | // server for payload output |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 342 | let state = service.state.clone(); // reference to state (not the state itself) is copied |
| 343 | std::thread::spawn(move || { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 344 | handle_stream_connection_from_vm(state).unwrap(); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 345 | }); |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 346 | |
| 347 | // binder server for vm |
| 348 | let state = service.state.clone(); // reference to state (not the state itself) is copied |
| 349 | std::thread::spawn(move || { |
| 350 | let mut service = VirtualMachineService::new_binder(state).as_binder(); |
| 351 | debug!("virtual machine service is starting as an RPC service."); |
| 352 | // SAFETY: Service ownership is transferring to the server and won't be valid afterward. |
| 353 | // Plus the binder objects are threadsafe. |
| 354 | let retval = unsafe { |
| 355 | binder_rpc_unstable_bindgen::RunRpcServer( |
| 356 | service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 357 | VM_BINDER_SERVICE_PORT as u32, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 358 | ) |
| 359 | }; |
| 360 | if retval { |
| 361 | debug!("RPC server has shut down gracefully"); |
| 362 | } else { |
| 363 | bail!("Premature termination of RPC server"); |
| 364 | } |
| 365 | |
| 366 | Ok(retval) |
| 367 | }); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 368 | service |
| 369 | } |
| 370 | } |
| 371 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 372 | /// Waits for incoming connections from VM. If a new connection is made, stores the stream in the |
| 373 | /// corresponding `VmInstance`. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 374 | fn handle_stream_connection_from_vm(state: Arc<Mutex<State>>) -> Result<()> { |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 375 | let listener = |
| 376 | VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_STREAM_SERVICE_PORT as u32)?; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 377 | for stream in listener.incoming() { |
| 378 | let stream = match stream { |
| 379 | Err(e) => { |
| 380 | warn!("invalid incoming connection: {}", e); |
| 381 | continue; |
| 382 | } |
| 383 | Ok(s) => s, |
| 384 | }; |
| 385 | if let Ok(SockAddr::Vsock(addr)) = stream.peer_addr() { |
| 386 | let cid = addr.cid(); |
| 387 | let port = addr.port(); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 388 | info!("payload stream connected from cid={}, port={}", cid, port); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 389 | if let Some(vm) = state.lock().unwrap().get_vm(cid) { |
Chris Wailes | 8bbb893 | 2021-09-10 14:14:19 -0700 | [diff] [blame] | 390 | *vm.stream.lock().unwrap() = Some(stream); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 391 | } else { |
| 392 | error!("connection from cid={} is not from a guest VM", cid); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | Ok(()) |
| 397 | } |
| 398 | |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 399 | fn write_zero_filler(zero_filler_path: &Path) -> Result<()> { |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 400 | let file = OpenOptions::new() |
| 401 | .create_new(true) |
| 402 | .read(true) |
| 403 | .write(true) |
| 404 | .open(zero_filler_path) |
| 405 | .with_context(|| "Failed to create zero.img")?; |
| 406 | file.set_len(ZERO_FILLER_SIZE)?; |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 407 | Ok(()) |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 408 | } |
| 409 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 410 | fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> { |
| 411 | part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?; |
| 412 | part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?; |
| 413 | part.flush() |
| 414 | } |
| 415 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 416 | /// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm. |
| 417 | /// |
| 418 | /// This may involve assembling a composite disk from a set of partition images. |
| 419 | fn assemble_disk_image( |
| 420 | disk: &DiskImage, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 421 | zero_filler_path: &Path, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 422 | temporary_directory: &Path, |
| 423 | next_temporary_image_id: &mut u64, |
| 424 | indirect_files: &mut Vec<File>, |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 425 | ) -> Result<DiskFile, Status> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 426 | let image = if !disk.partitions.is_empty() { |
| 427 | if disk.image.is_some() { |
| 428 | warn!("DiskImage {:?} contains both image and partitions.", disk); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 429 | return Err(new_binder_exception( |
| 430 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 431 | "DiskImage contains both image and partitions.", |
| 432 | )); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 435 | let composite_image_filenames = |
| 436 | make_composite_image_filenames(temporary_directory, next_temporary_image_id); |
| 437 | let (image, partition_files) = make_composite_image( |
| 438 | &disk.partitions, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 439 | zero_filler_path, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 440 | &composite_image_filenames.composite, |
| 441 | &composite_image_filenames.header, |
| 442 | &composite_image_filenames.footer, |
| 443 | ) |
| 444 | .map_err(|e| { |
| 445 | error!("Failed to make composite image with config {:?}: {}", disk, e); |
| 446 | new_binder_exception( |
| 447 | ExceptionCode::SERVICE_SPECIFIC, |
| 448 | format!("Failed to make composite image: {}", e), |
| 449 | ) |
| 450 | })?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 451 | |
| 452 | // Pass the file descriptors for the various partition files to crosvm when it |
| 453 | // is run. |
| 454 | indirect_files.extend(partition_files); |
| 455 | |
| 456 | image |
| 457 | } else if let Some(image) = &disk.image { |
| 458 | clone_file(image)? |
| 459 | } else { |
| 460 | warn!("DiskImage {:?} didn't contain image or partitions.", disk); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 461 | return Err(new_binder_exception( |
| 462 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 463 | "DiskImage didn't contain image or partitions.", |
| 464 | )); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | Ok(DiskFile { image, writable: disk.writable }) |
| 468 | } |
| 469 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 470 | fn load_app_config( |
| 471 | config: &VirtualMachineAppConfig, |
| 472 | temporary_directory: &Path, |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 473 | ) -> Result<VirtualMachineRawConfig> { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 474 | let apk_file = clone_file(config.apk.as_ref().unwrap())?; |
| 475 | let idsig_file = clone_file(config.idsig.as_ref().unwrap())?; |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 476 | let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 477 | let config_path = &config.configPath; |
| 478 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 479 | let mut apk_zip = ZipArchive::new(&apk_file)?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 480 | let config_file = apk_zip.by_name(config_path)?; |
| 481 | let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 482 | |
| 483 | let os_name = &vm_payload_config.os.name; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 484 | |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 485 | // For now, the only supported "os" value is "microdroid" |
| 486 | if os_name != "microdroid" { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 487 | bail!("Unknown OS \"{}\"", os_name); |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 488 | } |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 489 | |
| 490 | // It is safe to construct a filename based on the os_name because we've already checked that it |
| 491 | // is one of the allowed values. |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 492 | let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name)); |
| 493 | let vm_config_file = File::open(vm_config_path)?; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 494 | let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 495 | |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 496 | if config.memoryMib > 0 { |
| 497 | vm_config.memoryMib = config.memoryMib; |
Andrew Walbran | 45bcb0c | 2021-07-14 15:02:06 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 500 | // Microdroid requires an additional payload disk image and the bootconfig partition. |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 501 | if os_name == "microdroid" { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 502 | add_microdroid_images( |
| 503 | config, |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 504 | temporary_directory, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 505 | apk_file, |
| 506 | idsig_file, |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 507 | instance_file, |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 508 | &vm_payload_config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 509 | &mut vm_config, |
| 510 | )?; |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 511 | } |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 512 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 513 | Ok(vm_config) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 514 | } |
| 515 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 516 | /// Generates a unique filename to use for a composite disk image. |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 517 | fn make_composite_image_filenames( |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 518 | temporary_directory: &Path, |
| 519 | next_temporary_image_id: &mut u64, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 520 | ) -> CompositeImageFilenames { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 521 | let id = *next_temporary_image_id; |
| 522 | *next_temporary_image_id += 1; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 523 | CompositeImageFilenames { |
| 524 | composite: temporary_directory.join(format!("composite-{}.img", id)), |
| 525 | header: temporary_directory.join(format!("composite-{}-header.img", id)), |
| 526 | footer: temporary_directory.join(format!("composite-{}-footer.img", id)), |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /// Filenames for a composite disk image, including header and footer partitions. |
| 531 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 532 | struct CompositeImageFilenames { |
| 533 | /// The composite disk image itself. |
| 534 | composite: PathBuf, |
| 535 | /// The header partition image. |
| 536 | header: PathBuf, |
| 537 | /// The footer partition image. |
| 538 | footer: PathBuf, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /// Gets the calling SID of the current Binder thread. |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 542 | fn get_calling_sid() -> Result<String, Status> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 543 | ThreadState::with_calling_sid(|sid| { |
| 544 | if let Some(sid) = sid { |
| 545 | match sid.to_str() { |
| 546 | Ok(sid) => Ok(sid.to_owned()), |
| 547 | Err(e) => { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 548 | error!("SID was not valid UTF-8: {}", e); |
| 549 | Err(new_binder_exception( |
| 550 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 551 | format!("SID was not valid UTF-8: {}", e), |
| 552 | )) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | } else { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 556 | error!("Missing SID on createVm"); |
| 557 | Err(new_binder_exception(ExceptionCode::SECURITY, "Missing SID on createVm")) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 558 | } |
| 559 | }) |
| 560 | } |
| 561 | |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 562 | /// Checks whether the caller has a specific permission |
| 563 | fn check_permission(perm: &str) -> binder::Result<()> { |
| 564 | let calling_pid = ThreadState::get_calling_pid(); |
| 565 | let calling_uid = ThreadState::get_calling_uid(); |
| 566 | // Root can do anything |
| 567 | if calling_uid == 0 { |
| 568 | return Ok(()); |
| 569 | } |
| 570 | let perm_svc: Strong<dyn IPermissionController::IPermissionController> = |
| 571 | binder::get_interface("permission")?; |
| 572 | if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 573 | Ok(()) |
| 574 | } else { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 575 | Err(new_binder_exception( |
| 576 | ExceptionCode::SECURITY, |
| 577 | format!("does not have the {} permission", perm), |
| 578 | )) |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 579 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 582 | /// Check whether the caller of the current Binder method is allowed to call debug methods. |
| 583 | fn check_debug_access() -> binder::Result<()> { |
| 584 | check_permission("android.permission.DEBUG_VIRTUAL_MACHINE") |
| 585 | } |
| 586 | |
| 587 | /// Check whether the caller of the current Binder method is allowed to manage VMs |
| 588 | fn check_manage_access() -> binder::Result<()> { |
| 589 | check_permission("android.permission.MANAGE_VIRTUAL_MACHINE") |
| 590 | } |
| 591 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 592 | /// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM. |
| 593 | #[derive(Debug)] |
| 594 | struct VirtualMachine { |
| 595 | instance: Arc<VmInstance>, |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 596 | /// Keeps our service process running as long as this VM instance exists. |
| 597 | lazy_service_guard: LazyServiceGuard, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | impl VirtualMachine { |
| 601 | fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> { |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 602 | let binder = VirtualMachine { instance, lazy_service_guard: Default::default() }; |
Andrew Walbran | 4de2878 | 2021-04-13 14:51:43 +0000 | [diff] [blame] | 603 | BnVirtualMachine::new_binder(binder, BinderFeatures::default()) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | impl Interface for VirtualMachine {} |
| 608 | |
| 609 | impl IVirtualMachine for VirtualMachine { |
| 610 | fn getCid(&self) -> binder::Result<i32> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 611 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 612 | // others. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 613 | Ok(self.instance.cid as i32) |
| 614 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 615 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 616 | fn getState(&self) -> binder::Result<VirtualMachineState> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 617 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 618 | // others. |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 619 | Ok(get_state(&self.instance)) |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | fn registerCallback( |
| 623 | &self, |
| 624 | callback: &Strong<dyn IVirtualMachineCallback>, |
| 625 | ) -> binder::Result<()> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 626 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 627 | // others. |
| 628 | // |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 629 | // TODO: Should this give an error if the VM is already dead? |
| 630 | self.instance.callbacks.add(callback.clone()); |
| 631 | Ok(()) |
| 632 | } |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 633 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 634 | fn start(&self) -> binder::Result<()> { |
| 635 | self.instance.start().map_err(|e| { |
| 636 | error!("Error starting VM with CID {}: {:?}", self.instance.cid, e); |
| 637 | new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string()) |
| 638 | }) |
| 639 | } |
| 640 | |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 641 | fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 642 | if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) { |
| 643 | return Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "VM is not running")); |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 644 | } |
| 645 | let stream = |
| 646 | VsockStream::connect_with_cid_port(self.instance.cid, port as u32).map_err(|e| { |
| 647 | new_binder_exception( |
| 648 | ExceptionCode::SERVICE_SPECIFIC, |
| 649 | format!("Failed to connect: {}", e), |
| 650 | ) |
| 651 | })?; |
| 652 | Ok(vsock_stream_to_pfd(stream)) |
| 653 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | impl Drop for VirtualMachine { |
| 657 | fn drop(&mut self) { |
| 658 | debug!("Dropping {:?}", self); |
| 659 | self.instance.kill(); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /// A set of Binders to be called back in response to various events on the VM, such as when it |
| 664 | /// dies. |
| 665 | #[derive(Debug, Default)] |
| 666 | pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>); |
| 667 | |
| 668 | impl VirtualMachineCallbacks { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 669 | /// Call all registered callbacks to notify that the payload has started. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 670 | pub fn notify_payload_started(&self, cid: Cid, stream: Option<VsockStream>) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 671 | let callbacks = &*self.0.lock().unwrap(); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 672 | let pfd = stream.map(vsock_stream_to_pfd); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 673 | for callback in callbacks { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 674 | if let Err(e) = callback.onPayloadStarted(cid as i32, pfd.as_ref()) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 675 | error!("Error notifying payload start event from VM CID {}: {}", cid, e); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 680 | /// Call all registered callbacks to notify that the payload is ready to serve. |
| 681 | pub fn notify_payload_ready(&self, cid: Cid) { |
| 682 | let callbacks = &*self.0.lock().unwrap(); |
| 683 | for callback in callbacks { |
| 684 | if let Err(e) = callback.onPayloadReady(cid as i32) { |
| 685 | error!("Error notifying payload ready event from VM CID {}: {}", cid, e); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 690 | /// Call all registered callbacks to notify that the payload has finished. |
| 691 | pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) { |
| 692 | let callbacks = &*self.0.lock().unwrap(); |
| 693 | for callback in callbacks { |
| 694 | if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) { |
| 695 | error!("Error notifying payload finish event from VM CID {}: {}", cid, e); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 700 | /// Call all registered callbacks to say that the VM has died. |
| 701 | pub fn callback_on_died(&self, cid: Cid) { |
| 702 | let callbacks = &*self.0.lock().unwrap(); |
| 703 | for callback in callbacks { |
| 704 | if let Err(e) = callback.onDied(cid as i32) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 705 | error!("Error notifying exit of VM CID {}: {}", cid, e); |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /// Add a new callback to the set. |
| 711 | fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) { |
| 712 | self.0.lock().unwrap().push(callback); |
| 713 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 716 | /// The mutable state of the VirtualizationService. There should only be one instance of this |
| 717 | /// struct. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 718 | #[derive(Debug)] |
| 719 | struct State { |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 720 | /// The VMs which have been started. When VMs are started a weak reference is added to this list |
| 721 | /// while a strong reference is returned to the caller over Binder. Once all copies of the |
| 722 | /// Binder client are dropped the weak reference here will become invalid, and will be removed |
| 723 | /// from the list opportunistically the next time `add_vm` is called. |
| 724 | vms: Vec<Weak<VmInstance>>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 725 | |
| 726 | /// Vector of strong VM references held on behalf of users that cannot hold them themselves. |
| 727 | /// This is only used for debugging purposes. |
| 728 | debug_held_vms: Vec<Strong<dyn IVirtualMachine>>, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | impl State { |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 732 | /// Get a list of VMs which still have Binder references to them. |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 733 | fn vms(&self) -> Vec<Arc<VmInstance>> { |
| 734 | // Attempt to upgrade the weak pointers to strong pointers. |
| 735 | self.vms.iter().filter_map(Weak::upgrade).collect() |
| 736 | } |
| 737 | |
| 738 | /// Add a new VM to the list. |
| 739 | fn add_vm(&mut self, vm: Weak<VmInstance>) { |
| 740 | // Garbage collect any entries from the stored list which no longer exist. |
| 741 | self.vms.retain(|vm| vm.strong_count() > 0); |
| 742 | |
| 743 | // Actually add the new VM. |
| 744 | self.vms.push(vm); |
| 745 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 746 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 747 | /// Get a VM that corresponds to the given cid |
| 748 | fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> { |
| 749 | self.vms().into_iter().find(|vm| vm.cid == cid) |
| 750 | } |
| 751 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 752 | /// Store a strong VM reference. |
| 753 | fn debug_hold_vm(&mut self, vm: Strong<dyn IVirtualMachine>) { |
| 754 | self.debug_held_vms.push(vm); |
| 755 | } |
| 756 | |
| 757 | /// Retrieve and remove a strong VM reference. |
| 758 | fn debug_drop_vm(&mut self, cid: i32) -> Option<Strong<dyn IVirtualMachine>> { |
| 759 | let pos = self.debug_held_vms.iter().position(|vm| vm.getCid() == Ok(cid))?; |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 760 | let vm = self.debug_held_vms.swap_remove(pos); |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 761 | Some(vm) |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 762 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | impl Default for State { |
| 766 | fn default() -> Self { |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 767 | State { vms: vec![], debug_held_vms: vec![] } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 770 | |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 771 | /// Get the next available CID, or an error if we have run out. The last CID used is stored in |
| 772 | /// a system property so that restart of virtualizationservice doesn't reuse CID while the host |
| 773 | /// Android is up. |
| 774 | fn next_cid() -> Result<Cid> { |
| 775 | let next = if let Ok(val) = system_properties::read(SYSPROP_LAST_CID) { |
| 776 | if let Ok(num) = val.parse::<u32>() { |
| 777 | num.checked_add(1).ok_or_else(|| anyhow!("run out of CID"))? |
| 778 | } else { |
| 779 | error!("Invalid last CID {}. Using {}", &val, FIRST_GUEST_CID); |
| 780 | FIRST_GUEST_CID |
| 781 | } |
| 782 | } else { |
| 783 | // First VM since the boot |
| 784 | FIRST_GUEST_CID |
| 785 | }; |
| 786 | // Persist the last value for next use |
| 787 | let str_val = format!("{}", next); |
| 788 | system_properties::write(SYSPROP_LAST_CID, &str_val)?; |
| 789 | Ok(next) |
| 790 | } |
| 791 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 792 | /// Gets the `VirtualMachineState` of the given `VmInstance`. |
| 793 | fn get_state(instance: &VmInstance) -> VirtualMachineState { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 794 | match &*instance.vm_state.lock().unwrap() { |
| 795 | VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED, |
| 796 | VmState::Running { .. } => match instance.payload_state() { |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 797 | PayloadState::Starting => VirtualMachineState::STARTING, |
| 798 | PayloadState::Started => VirtualMachineState::STARTED, |
| 799 | PayloadState::Ready => VirtualMachineState::READY, |
| 800 | PayloadState::Finished => VirtualMachineState::FINISHED, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 801 | }, |
| 802 | VmState::Dead => VirtualMachineState::DEAD, |
| 803 | VmState::Failed => VirtualMachineState::DEAD, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 807 | /// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file. |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 808 | fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> { |
| 809 | file.as_ref().try_clone().map_err(|e| { |
| 810 | new_binder_exception( |
| 811 | ExceptionCode::BAD_PARCELABLE, |
| 812 | format!("Failed to clone File from ParcelFileDescriptor: {}", e), |
| 813 | ) |
| 814 | }) |
| 815 | } |
| 816 | |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 817 | /// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file. |
| 818 | fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> Result<Option<File>, Status> { |
| 819 | file.as_ref().map(clone_file).transpose() |
| 820 | } |
| 821 | |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 822 | /// Converts a `VsockStream` to a `ParcelFileDescriptor`. |
| 823 | fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor { |
| 824 | // SAFETY: ownership is transferred from stream to f |
| 825 | let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 826 | ParcelFileDescriptor::new(f) |
| 827 | } |
| 828 | |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 829 | /// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but |
| 830 | /// it doesn't require that T implements Clone. |
| 831 | enum BorrowedOrOwned<'a, T> { |
| 832 | Borrowed(&'a T), |
| 833 | Owned(T), |
| 834 | } |
| 835 | |
| 836 | impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> { |
| 837 | fn as_ref(&self) -> &T { |
| 838 | match self { |
| 839 | Self::Borrowed(b) => b, |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 840 | Self::Owned(o) => o, |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 844 | |
| 845 | /// Implementation of `IVirtualMachineService`, the entry point of the AIDL service. |
| 846 | #[derive(Debug, Default)] |
| 847 | struct VirtualMachineService { |
| 848 | state: Arc<Mutex<State>>, |
| 849 | } |
| 850 | |
| 851 | impl Interface for VirtualMachineService {} |
| 852 | |
| 853 | impl IVirtualMachineService for VirtualMachineService { |
| 854 | fn notifyPayloadStarted(&self, cid: i32) -> binder::Result<()> { |
| 855 | let cid = cid as Cid; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 856 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 857 | info!("VM having CID {} started payload", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 858 | vm.update_payload_state(PayloadState::Started) |
| 859 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 860 | let stream = vm.stream.lock().unwrap().take(); |
| 861 | vm.callbacks.notify_payload_started(cid, stream); |
| 862 | Ok(()) |
| 863 | } else { |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 864 | error!("notifyPayloadStarted is called from an unknown cid {}", cid); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 865 | Err(new_binder_exception( |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 866 | ExceptionCode::SERVICE_SPECIFIC, |
| 867 | format!("cannot find a VM with cid {}", cid), |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 868 | )) |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 869 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 870 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 871 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 872 | fn notifyPayloadReady(&self, cid: i32) -> binder::Result<()> { |
| 873 | let cid = cid as Cid; |
| 874 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 875 | info!("VM having CID {} payload is ready", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 876 | vm.update_payload_state(PayloadState::Ready) |
| 877 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 878 | vm.callbacks.notify_payload_ready(cid); |
| 879 | Ok(()) |
| 880 | } else { |
| 881 | error!("notifyPayloadReady is called from an unknown cid {}", cid); |
| 882 | Err(new_binder_exception( |
| 883 | ExceptionCode::SERVICE_SPECIFIC, |
| 884 | format!("cannot find a VM with cid {}", cid), |
| 885 | )) |
| 886 | } |
| 887 | } |
| 888 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 889 | fn notifyPayloadFinished(&self, cid: i32, exit_code: i32) -> binder::Result<()> { |
| 890 | let cid = cid as Cid; |
| 891 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 892 | info!("VM having CID {} finished payload", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 893 | vm.update_payload_state(PayloadState::Finished) |
| 894 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 895 | vm.callbacks.notify_payload_finished(cid, exit_code); |
| 896 | Ok(()) |
| 897 | } else { |
| 898 | error!("notifyPayloadFinished is called from an unknown cid {}", cid); |
| 899 | Err(new_binder_exception( |
| 900 | ExceptionCode::SERVICE_SPECIFIC, |
| 901 | format!("cannot find a VM with cid {}", cid), |
| 902 | )) |
| 903 | } |
| 904 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | impl VirtualMachineService { |
| 908 | fn new_binder(state: Arc<Mutex<State>>) -> Strong<dyn IVirtualMachineService> { |
| 909 | BnVirtualMachineService::new_binder( |
| 910 | VirtualMachineService { state }, |
| 911 | BinderFeatures::default(), |
| 912 | ) |
| 913 | } |
| 914 | } |