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 | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 17 | use crate::crosvm::VmInstance; |
| 18 | use crate::{Cid, FIRST_GUEST_CID}; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 19 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService; |
| 20 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualMachine::{ |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 21 | BnVirtualMachine, IVirtualMachine, |
| 22 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 23 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualMachineCallback::IVirtualMachineCallback; |
| 24 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig; |
| 25 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::VirtualMachineDebugInfo::VirtualMachineDebugInfo; |
| 26 | use android_system_virtualizationservice::binder::{ |
Andrew Walbran | 4de2878 | 2021-04-13 14:51:43 +0000 | [diff] [blame] | 27 | self, BinderFeatures, Interface, ParcelFileDescriptor, StatusCode, Strong, ThreadState, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 28 | }; |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 29 | use log::{debug, error}; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 30 | use std::sync::{Arc, Mutex, Weak}; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 31 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 32 | pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice"; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 33 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 34 | // TODO(qwandor): Use PermissionController once it is available to Rust. |
| 35 | /// Only processes running with one of these UIDs are allowed to call debug methods. |
| 36 | const DEBUG_ALLOWED_UIDS: [u32; 2] = [0, 2000]; |
| 37 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 38 | /// Implementation of `IVirtualizationService`, the entry point of the AIDL service. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 39 | #[derive(Debug, Default)] |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 40 | pub struct VirtualizationService { |
Andrew Walbran | 9c01baa | 2021-03-08 18:23:50 +0000 | [diff] [blame] | 41 | state: Mutex<State>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 44 | impl Interface for VirtualizationService {} |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 45 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 46 | impl IVirtualizationService for VirtualizationService { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 47 | /// Create and start a new VM with the given configuration, assigning it the next available CID. |
| 48 | /// |
| 49 | /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client. |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 50 | fn startVm( |
| 51 | &self, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 52 | config: &VirtualMachineConfig, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 53 | log_fd: Option<&ParcelFileDescriptor>, |
| 54 | ) -> binder::Result<Strong<dyn IVirtualMachine>> { |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 55 | let state = &mut *self.state.lock().unwrap(); |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 56 | let log_fd = log_fd |
| 57 | .map(|fd| fd.as_ref().try_clone().map_err(|_| StatusCode::UNKNOWN_ERROR)) |
| 58 | .transpose()?; |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 59 | let requester_uid = ThreadState::get_calling_uid(); |
| 60 | let requester_sid = ThreadState::with_calling_sid(|sid| { |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 61 | if let Some(sid) = sid { |
| 62 | match sid.to_str() { |
| 63 | Ok(sid) => Ok(sid.to_owned()), |
| 64 | Err(e) => { |
| 65 | error!("SID was not valid UTF-8: {:?}", e); |
| 66 | Err(StatusCode::BAD_VALUE) |
| 67 | } |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 68 | } |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 69 | } else { |
| 70 | error!("Missing SID on startVm"); |
| 71 | Err(StatusCode::UNKNOWN_ERROR) |
| 72 | } |
| 73 | })?; |
| 74 | let requester_debug_pid = ThreadState::get_calling_pid(); |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 75 | let cid = state.allocate_cid()?; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 76 | let instance = VmInstance::start( |
| 77 | config, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 78 | cid, |
| 79 | log_fd, |
| 80 | requester_uid, |
| 81 | requester_sid, |
| 82 | requester_debug_pid, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 83 | ) |
| 84 | .map_err(|e| { |
| 85 | error!("Failed to start VM with config {:?}: {:?}", config, e); |
| 86 | StatusCode::UNKNOWN_ERROR |
| 87 | })?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 88 | state.add_vm(Arc::downgrade(&instance)); |
| 89 | Ok(VirtualMachine::create(instance)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 90 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 91 | |
| 92 | /// Get a list of all currently running VMs. This method is only intended for debug purposes, |
| 93 | /// and as such is only permitted from the shell user. |
| 94 | fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> { |
| 95 | if !debug_access_allowed() { |
| 96 | return Err(StatusCode::PERMISSION_DENIED.into()); |
| 97 | } |
| 98 | |
| 99 | let state = &mut *self.state.lock().unwrap(); |
| 100 | let vms = state.vms(); |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 101 | let cids = vms |
| 102 | .into_iter() |
| 103 | .map(|vm| VirtualMachineDebugInfo { |
| 104 | cid: vm.cid as i32, |
Andrew Walbran | 1ef19ae | 2021-04-07 11:31:57 +0000 | [diff] [blame] | 105 | requesterUid: vm.requester_uid as i32, |
| 106 | requesterSid: vm.requester_sid.clone(), |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 107 | requesterPid: vm.requester_debug_pid, |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 108 | running: vm.running(), |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 109 | }) |
| 110 | .collect(); |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 111 | Ok(cids) |
| 112 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 113 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 114 | /// Hold a strong reference to a VM in VirtualizationService. This method is only intended for |
| 115 | /// debug purposes, and as such is only permitted from the shell user. |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 116 | fn debugHoldVmRef(&self, vmref: &Strong<dyn IVirtualMachine>) -> binder::Result<()> { |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 117 | if !debug_access_allowed() { |
| 118 | return Err(StatusCode::PERMISSION_DENIED.into()); |
| 119 | } |
| 120 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 121 | let state = &mut *self.state.lock().unwrap(); |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 122 | state.debug_hold_vm(vmref.clone()); |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 123 | Ok(()) |
| 124 | } |
| 125 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 126 | /// Drop reference to a VM that is being held by VirtualizationService. Returns the reference if |
| 127 | /// the VM was found and None otherwise. This method is only intended for debug purposes, and as |
| 128 | /// such is only permitted from the shell user. |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 129 | fn debugDropVmRef(&self, cid: i32) -> binder::Result<Option<Strong<dyn IVirtualMachine>>> { |
| 130 | if !debug_access_allowed() { |
| 131 | return Err(StatusCode::PERMISSION_DENIED.into()); |
| 132 | } |
| 133 | |
| 134 | let state = &mut *self.state.lock().unwrap(); |
| 135 | Ok(state.debug_drop_vm(cid)) |
| 136 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /// Check whether the caller of the current Binder method is allowed to call debug methods. |
| 140 | fn debug_access_allowed() -> bool { |
| 141 | let uid = ThreadState::get_calling_uid(); |
| 142 | log::trace!("Debug method call from UID {}.", uid); |
| 143 | DEBUG_ALLOWED_UIDS.contains(&uid) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM. |
| 147 | #[derive(Debug)] |
| 148 | struct VirtualMachine { |
| 149 | instance: Arc<VmInstance>, |
| 150 | } |
| 151 | |
| 152 | impl VirtualMachine { |
| 153 | fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> { |
| 154 | let binder = VirtualMachine { instance }; |
Andrew Walbran | 4de2878 | 2021-04-13 14:51:43 +0000 | [diff] [blame] | 155 | BnVirtualMachine::new_binder(binder, BinderFeatures::default()) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | impl Interface for VirtualMachine {} |
| 160 | |
| 161 | impl IVirtualMachine for VirtualMachine { |
| 162 | fn getCid(&self) -> binder::Result<i32> { |
| 163 | Ok(self.instance.cid as i32) |
| 164 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 165 | |
| 166 | fn isRunning(&self) -> binder::Result<bool> { |
| 167 | Ok(self.instance.running()) |
| 168 | } |
| 169 | |
| 170 | fn registerCallback( |
| 171 | &self, |
| 172 | callback: &Strong<dyn IVirtualMachineCallback>, |
| 173 | ) -> binder::Result<()> { |
| 174 | // TODO: Should this give an error if the VM is already dead? |
| 175 | self.instance.callbacks.add(callback.clone()); |
| 176 | Ok(()) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl Drop for VirtualMachine { |
| 181 | fn drop(&mut self) { |
| 182 | debug!("Dropping {:?}", self); |
| 183 | self.instance.kill(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// A set of Binders to be called back in response to various events on the VM, such as when it |
| 188 | /// dies. |
| 189 | #[derive(Debug, Default)] |
| 190 | pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>); |
| 191 | |
| 192 | impl VirtualMachineCallbacks { |
| 193 | /// Call all registered callbacks to say that the VM has died. |
| 194 | pub fn callback_on_died(&self, cid: Cid) { |
| 195 | let callbacks = &*self.0.lock().unwrap(); |
| 196 | for callback in callbacks { |
| 197 | if let Err(e) = callback.onDied(cid as i32) { |
| 198 | error!("Error calling callback: {}", e); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// Add a new callback to the set. |
| 204 | fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) { |
| 205 | self.0.lock().unwrap().push(callback); |
| 206 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame^] | 209 | /// The mutable state of the VirtualizationService. There should only be one instance of this |
| 210 | /// struct. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 211 | #[derive(Debug)] |
| 212 | struct State { |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 213 | /// The next available unused CID. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 214 | next_cid: Cid, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 215 | |
| 216 | /// The VMs which have been started. When VMs are started a weak reference is added to this list |
| 217 | /// while a strong reference is returned to the caller over Binder. Once all copies of the |
| 218 | /// Binder client are dropped the weak reference here will become invalid, and will be removed |
| 219 | /// from the list opportunistically the next time `add_vm` is called. |
| 220 | vms: Vec<Weak<VmInstance>>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 221 | |
| 222 | /// Vector of strong VM references held on behalf of users that cannot hold them themselves. |
| 223 | /// This is only used for debugging purposes. |
| 224 | debug_held_vms: Vec<Strong<dyn IVirtualMachine>>, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | impl State { |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 228 | /// Get a list of VMs which still have Binder references to them. |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 229 | fn vms(&self) -> Vec<Arc<VmInstance>> { |
| 230 | // Attempt to upgrade the weak pointers to strong pointers. |
| 231 | self.vms.iter().filter_map(Weak::upgrade).collect() |
| 232 | } |
| 233 | |
| 234 | /// Add a new VM to the list. |
| 235 | fn add_vm(&mut self, vm: Weak<VmInstance>) { |
| 236 | // Garbage collect any entries from the stored list which no longer exist. |
| 237 | self.vms.retain(|vm| vm.strong_count() > 0); |
| 238 | |
| 239 | // Actually add the new VM. |
| 240 | self.vms.push(vm); |
| 241 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 242 | |
| 243 | /// Store a strong VM reference. |
| 244 | fn debug_hold_vm(&mut self, vm: Strong<dyn IVirtualMachine>) { |
| 245 | self.debug_held_vms.push(vm); |
| 246 | } |
| 247 | |
| 248 | /// Retrieve and remove a strong VM reference. |
| 249 | fn debug_drop_vm(&mut self, cid: i32) -> Option<Strong<dyn IVirtualMachine>> { |
| 250 | let pos = self.debug_held_vms.iter().position(|vm| vm.getCid() == Ok(cid))?; |
| 251 | Some(self.debug_held_vms.swap_remove(pos)) |
| 252 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 253 | |
| 254 | /// Get the next available CID, or an error if we have run out. |
| 255 | fn allocate_cid(&mut self) -> binder::Result<Cid> { |
| 256 | // TODO(qwandor): keep track of which CIDs are currently in use so that we can reuse them. |
| 257 | let cid = self.next_cid; |
| 258 | self.next_cid = self.next_cid.checked_add(1).ok_or(StatusCode::UNKNOWN_ERROR)?; |
| 259 | Ok(cid) |
| 260 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | impl Default for State { |
| 264 | fn default() -> Self { |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 265 | State { next_cid: FIRST_GUEST_CID, vms: vec![], debug_held_vms: vec![] } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 266 | } |
| 267 | } |