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