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 | |
| 15 | //! Implementation of the AIDL interface of the Virt Manager. |
| 16 | |
| 17 | use crate::config::load_vm_config; |
| 18 | use crate::crosvm::VmInstance; |
| 19 | use crate::{Cid, FIRST_GUEST_CID}; |
| 20 | use android_system_virtmanager::aidl::android::system::virtmanager::IVirtManager::IVirtManager; |
| 21 | use android_system_virtmanager::aidl::android::system::virtmanager::IVirtualMachine::{ |
| 22 | BnVirtualMachine, IVirtualMachine, |
| 23 | }; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 24 | use android_system_virtmanager::aidl::android::system::virtmanager::VirtualMachineDebugInfo::VirtualMachineDebugInfo; |
| 25 | use android_system_virtmanager::binder::{self, Interface, StatusCode, Strong, ThreadState}; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 26 | use log::error; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 27 | use std::sync::{Arc, Mutex, Weak}; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 28 | |
| 29 | pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtmanager"; |
| 30 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 31 | // TODO(qwandor): Use PermissionController once it is available to Rust. |
| 32 | /// Only processes running with one of these UIDs are allowed to call debug methods. |
| 33 | const DEBUG_ALLOWED_UIDS: [u32; 2] = [0, 2000]; |
| 34 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 35 | /// Implementation of `IVirtManager`, the entry point of the AIDL service. |
| 36 | #[derive(Debug, Default)] |
| 37 | pub struct VirtManager { |
Andrew Walbran | 9c01baa | 2021-03-08 18:23:50 +0000 | [diff] [blame] | 38 | state: Mutex<State>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | impl Interface for VirtManager {} |
| 42 | |
| 43 | impl IVirtManager for VirtManager { |
| 44 | /// Create and start a new VM with the given configuration, assigning it the next available CID. |
| 45 | /// |
| 46 | /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client. |
| 47 | fn startVm(&self, config_path: &str) -> binder::Result<Strong<dyn IVirtualMachine>> { |
| 48 | let state = &mut *self.state.lock().unwrap(); |
| 49 | let cid = state.next_cid; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 50 | let instance = Arc::new(start_vm(config_path, cid)?); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 51 | // TODO(qwandor): keep track of which CIDs are currently in use so that we can reuse them. |
| 52 | state.next_cid = state.next_cid.checked_add(1).ok_or(StatusCode::UNKNOWN_ERROR)?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 53 | state.add_vm(Arc::downgrade(&instance)); |
| 54 | Ok(VirtualMachine::create(instance)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 55 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 56 | |
| 57 | /// Get a list of all currently running VMs. This method is only intended for debug purposes, |
| 58 | /// and as such is only permitted from the shell user. |
| 59 | fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> { |
| 60 | if !debug_access_allowed() { |
| 61 | return Err(StatusCode::PERMISSION_DENIED.into()); |
| 62 | } |
| 63 | |
| 64 | let state = &mut *self.state.lock().unwrap(); |
| 65 | let vms = state.vms(); |
| 66 | let cids = vms |
| 67 | .into_iter() |
| 68 | .map(|vm| VirtualMachineDebugInfo { |
| 69 | cid: vm.cid as i32, |
| 70 | configPath: vm.config_path.clone(), |
| 71 | }) |
| 72 | .collect(); |
| 73 | Ok(cids) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Check whether the caller of the current Binder method is allowed to call debug methods. |
| 78 | fn debug_access_allowed() -> bool { |
| 79 | let uid = ThreadState::get_calling_uid(); |
| 80 | log::trace!("Debug method call from UID {}.", uid); |
| 81 | DEBUG_ALLOWED_UIDS.contains(&uid) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM. |
| 85 | #[derive(Debug)] |
| 86 | struct VirtualMachine { |
| 87 | instance: Arc<VmInstance>, |
| 88 | } |
| 89 | |
| 90 | impl VirtualMachine { |
| 91 | fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> { |
| 92 | let binder = VirtualMachine { instance }; |
| 93 | BnVirtualMachine::new_binder(binder) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Interface for VirtualMachine {} |
| 98 | |
| 99 | impl IVirtualMachine for VirtualMachine { |
| 100 | fn getCid(&self) -> binder::Result<i32> { |
| 101 | Ok(self.instance.cid as i32) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// The mutable state of the Virt Manager. There should only be one instance of this struct. |
| 106 | #[derive(Debug)] |
| 107 | struct State { |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 108 | /// The next available unused CID. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 109 | next_cid: Cid, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 110 | |
| 111 | /// The VMs which have been started. When VMs are started a weak reference is added to this list |
| 112 | /// while a strong reference is returned to the caller over Binder. Once all copies of the |
| 113 | /// Binder client are dropped the weak reference here will become invalid, and will be removed |
| 114 | /// from the list opportunistically the next time `add_vm` is called. |
| 115 | vms: Vec<Weak<VmInstance>>, |
| 116 | } |
| 117 | |
| 118 | impl State { |
| 119 | /// Get a list of VMs which are currently running. |
| 120 | fn vms(&self) -> Vec<Arc<VmInstance>> { |
| 121 | // Attempt to upgrade the weak pointers to strong pointers. |
| 122 | self.vms.iter().filter_map(Weak::upgrade).collect() |
| 123 | } |
| 124 | |
| 125 | /// Add a new VM to the list. |
| 126 | fn add_vm(&mut self, vm: Weak<VmInstance>) { |
| 127 | // Garbage collect any entries from the stored list which no longer exist. |
| 128 | self.vms.retain(|vm| vm.strong_count() > 0); |
| 129 | |
| 130 | // Actually add the new VM. |
| 131 | self.vms.push(vm); |
| 132 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | impl Default for State { |
| 136 | fn default() -> Self { |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 137 | State { next_cid: FIRST_GUEST_CID, vms: vec![] } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Start a new VM instance from the given VM config filename. This assumes the VM is not already |
| 142 | /// running. |
| 143 | fn start_vm(config_path: &str, cid: Cid) -> binder::Result<VmInstance> { |
| 144 | let config = load_vm_config(config_path).map_err(|e| { |
| 145 | error!("Failed to load VM config {}: {:?}", config_path, e); |
| 146 | StatusCode::BAD_VALUE |
| 147 | })?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame^] | 148 | Ok(VmInstance::start(&config, cid, config_path).map_err(|e| { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 149 | error!("Failed to start VM {}: {:?}", config_path, e); |
| 150 | StatusCode::UNKNOWN_ERROR |
| 151 | })?) |
| 152 | } |