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