1 // Copyright 2023, 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 //! This module contains the functions to start, stop and communicate with the
16 //! Service VM.
17
18 use android_system_virtualizationservice::{
19 aidl::android::system::virtualizationservice::{
20 CpuTopology::CpuTopology, DiskImage::DiskImage,
21 IVirtualizationService::IVirtualizationService, Partition::Partition,
22 PartitionType::PartitionType, VirtualMachineConfig::VirtualMachineConfig,
23 VirtualMachineRawConfig::VirtualMachineRawConfig,
24 },
25 binder::ParcelFileDescriptor,
26 };
27 use anyhow::{anyhow, ensure, Context, Result};
28 use log::{info, warn};
29 use service_vm_comm::{Request, Response, ServiceVmRequest, VmType};
30 use std::fs::{self, File, OpenOptions};
31 use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
32 use std::path::{Path, PathBuf};
33 use std::sync::{Condvar, Mutex};
34 use std::thread;
35 use std::time::Duration;
36 use vmclient::{DeathReason, VmInstance};
37 use vsock::{VsockListener, VsockStream, VMADDR_CID_HOST};
38
39 /// Size of virtual memory allocated to the Service VM.
40 pub const VM_MEMORY_MB: i32 = 6;
41
42 const VIRT_DATA_DIR: &str = "/data/misc/apexdata/com.android.virt";
43 const RIALTO_PATH: &str = "/apex/com.android.virt/etc/rialto.bin";
44 const INSTANCE_IMG_NAME: &str = "service_vm_instance.img";
45 const INSTANCE_ID_FILENAME: &str = "service_vm_instance_id";
46 const INSTANCE_IMG_SIZE_BYTES: i64 = 1 << 20; // 1MB
47 const WRITE_BUFFER_CAPACITY: usize = 512;
48 const READ_TIMEOUT: Duration = Duration::from_secs(10);
49 const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
50
51 static PENDING_REQUESTS: AtomicCounter = AtomicCounter::new();
52 static SERVICE_VM: Mutex<Option<ServiceVm>> = Mutex::new(None);
53 static SERVICE_VM_SHUTDOWN: Condvar = Condvar::new();
54
55 /// Atomic counter with a condition variable that is used to wait for the counter
56 /// to become positive within a timeout.
57 #[derive(Debug, Default)]
58 struct AtomicCounter {
59 num: Mutex<usize>,
60 num_increased: Condvar,
61 }
62
63 impl AtomicCounter {
new() -> Self64 const fn new() -> Self {
65 Self { num: Mutex::new(0), num_increased: Condvar::new() }
66 }
67
68 /// Checks if the counter becomes positive within the given timeout.
is_positive_within_timeout(&self, timeout: Duration) -> bool69 fn is_positive_within_timeout(&self, timeout: Duration) -> bool {
70 let (guard, _wait_result) = self
71 .num_increased
72 .wait_timeout_while(self.num.lock().unwrap(), timeout, |&mut x| x == 0)
73 .unwrap();
74 *guard > 0
75 }
76
increment(&self)77 fn increment(&self) {
78 let mut num = self.num.lock().unwrap();
79 *num = num.checked_add(1).unwrap();
80 self.num_increased.notify_all();
81 }
82
decrement(&self)83 fn decrement(&self) {
84 let mut num = self.num.lock().unwrap();
85 *num = num.checked_sub(1).unwrap();
86 }
87 }
88
89 /// Processes the request in the service VM.
process_request(request: Request) -> Result<Response>90 pub fn process_request(request: Request) -> Result<Response> {
91 PENDING_REQUESTS.increment();
92 let result = process_request_in_service_vm(request);
93 PENDING_REQUESTS.decrement();
94 thread::spawn(stop_service_vm_if_idle);
95 result
96 }
97
process_request_in_service_vm(request: Request) -> Result<Response>98 fn process_request_in_service_vm(request: Request) -> Result<Response> {
99 let mut service_vm = SERVICE_VM.lock().unwrap();
100 if service_vm.is_none() {
101 *service_vm = Some(ServiceVm::start()?);
102 }
103 service_vm.as_mut().unwrap().process_request(request)
104 }
105
stop_service_vm_if_idle()106 fn stop_service_vm_if_idle() {
107 if PENDING_REQUESTS.is_positive_within_timeout(Duration::from_secs(1)) {
108 info!("Service VM has pending requests, keeping it running.");
109 } else {
110 info!("Service VM is idle, shutting it down.");
111 *SERVICE_VM.lock().unwrap() = None;
112 SERVICE_VM_SHUTDOWN.notify_all();
113 }
114 }
115
116 /// Waits until the service VM shuts down.
117 /// This function is only used in tests.
wait_until_service_vm_shuts_down() -> Result<()>118 pub fn wait_until_service_vm_shuts_down() -> Result<()> {
119 const WAIT_FOR_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
120
121 let (_guard, wait_result) = SERVICE_VM_SHUTDOWN
122 .wait_timeout_while(SERVICE_VM.lock().unwrap(), WAIT_FOR_SHUTDOWN_TIMEOUT, |x| x.is_some())
123 .unwrap();
124 ensure!(!wait_result.timed_out(), "Service VM didn't shut down within the timeout");
125 Ok(())
126 }
127
128 /// Service VM.
129 pub struct ServiceVm {
130 vsock_stream: VsockStream,
131 /// VmInstance will be dropped when ServiceVm goes out of scope, which will kill the VM.
132 vm: VmInstance,
133 }
134
135 impl ServiceVm {
136 /// Starts the service VM and returns its instance.
137 /// The same instance image is used for different VMs.
138 /// TODO(b/27593612): Remove instance image usage for Service VM.
start() -> Result<Self>139 pub fn start() -> Result<Self> {
140 let instance_img_path = Path::new(VIRT_DATA_DIR).join(INSTANCE_IMG_NAME);
141 let vm = protected_vm_instance(instance_img_path)?;
142
143 let vm = Self::start_vm(vm, VmType::ProtectedVm)?;
144 Ok(vm)
145 }
146
147 /// Starts the given VM instance and sets up the vsock connection with it.
148 /// Returns a `ServiceVm` instance.
149 /// This function is exposed for testing.
start_vm(vm: VmInstance, vm_type: VmType) -> Result<Self>150 pub fn start_vm(vm: VmInstance, vm_type: VmType) -> Result<Self> {
151 // Sets up the vsock server on the host.
152 let vsock_listener = VsockListener::bind_with_cid_port(VMADDR_CID_HOST, vm_type.port())?;
153
154 // Starts the service VM.
155 vm.start().context("Failed to start service VM")?;
156 info!("Service VM started");
157
158 // Accepts the connection from the service VM.
159 // TODO(b/299427101): Introduce a timeout for the accept.
160 let (vsock_stream, peer_addr) = vsock_listener.accept().context("Failed to accept")?;
161 info!("Accepted connection {:?}", vsock_stream);
162 ensure!(
163 peer_addr.cid() == u32::try_from(vm.cid()).unwrap(),
164 "The CID of the peer address {} doesn't match the service VM CID {}",
165 peer_addr,
166 vm.cid()
167 );
168 vsock_stream.set_read_timeout(Some(READ_TIMEOUT))?;
169 vsock_stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
170
171 Ok(Self { vsock_stream, vm })
172 }
173
174 /// Processes the request in the service VM.
process_request(&mut self, request: Request) -> Result<Response>175 pub fn process_request(&mut self, request: Request) -> Result<Response> {
176 self.write_request(&ServiceVmRequest::Process(request))?;
177 self.read_response()
178 }
179
180 /// Sends the request to the service VM.
write_request(&mut self, request: &ServiceVmRequest) -> Result<()>181 fn write_request(&mut self, request: &ServiceVmRequest) -> Result<()> {
182 let mut buffer = BufWriter::with_capacity(WRITE_BUFFER_CAPACITY, &mut self.vsock_stream);
183 ciborium::into_writer(request, &mut buffer)?;
184 buffer.flush().context("Failed to flush the buffer")?;
185 info!("Sent request to the service VM.");
186 Ok(())
187 }
188
189 /// Reads the response from the service VM.
read_response(&mut self) -> Result<Response>190 fn read_response(&mut self) -> Result<Response> {
191 let response: Response = ciborium::from_reader(&mut self.vsock_stream)
192 .context("Failed to read the response from the service VM")?;
193 info!("Received response from the service VM.");
194 Ok(response)
195 }
196
197 /// Shuts down the service VM.
shutdown(&mut self) -> Result<DeathReason>198 fn shutdown(&mut self) -> Result<DeathReason> {
199 self.write_request(&ServiceVmRequest::Shutdown)?;
200 self.vm
201 .wait_for_death_with_timeout(Duration::from_secs(10))
202 .ok_or_else(|| anyhow!("Timed out to exit the service VM"))
203 }
204 }
205
206 impl Drop for ServiceVm {
drop(&mut self)207 fn drop(&mut self) {
208 // Wait till the service VM finishes releasing all the resources.
209 match self.shutdown() {
210 Ok(reason) => info!("Exit the service VM successfully: {reason:?}"),
211 Err(e) => warn!("Service VM shutdown request failed '{e:?}', killing it."),
212 }
213 }
214 }
215
216 /// Returns a `VmInstance` of a protected VM with the instance image from the given path.
protected_vm_instance(instance_img_path: PathBuf) -> Result<VmInstance>217 pub fn protected_vm_instance(instance_img_path: PathBuf) -> Result<VmInstance> {
218 let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
219 let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
220 info!("Connected to VirtMgr for service VM");
221
222 let instance_img = instance_img(service.as_ref(), instance_img_path)?;
223 let writable_partitions = vec![Partition {
224 label: "vm-instance".to_owned(),
225 image: Some(instance_img),
226 writable: true,
227 guid: None,
228 }];
229 let rialto = File::open(RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
230 let instance_id_file = Path::new(VIRT_DATA_DIR).join(INSTANCE_ID_FILENAME);
231 let instance_id = get_or_allocate_instance_id(service.as_ref(), instance_id_file)?;
232 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
233 name: String::from("Service VM"),
234 kernel: Some(ParcelFileDescriptor::new(rialto)),
235 disks: vec![DiskImage { image: None, partitions: writable_partitions, writable: true }],
236 instanceId: instance_id,
237 protectedVm: true,
238 memoryMib: VM_MEMORY_MB,
239 cpuTopology: CpuTopology::ONE_CPU,
240 platformVersion: "~1.0".to_string(),
241 gdbPort: 0, // No gdb
242 ..Default::default()
243 });
244 let console_out = Some(android_log_fd()?);
245 let console_in = None;
246 let log = Some(android_log_fd()?);
247 let dump_dt = None;
248 let callback = None;
249 VmInstance::create(service.as_ref(), &config, console_out, console_in, log, dump_dt, callback)
250 .context("Failed to create service VM")
251 }
252
253 /// TODO(b/291213394): Reuse this method in other places such as vm and compos.
get_or_allocate_instance_id( service: &dyn IVirtualizationService, instance_id_file: PathBuf, ) -> Result<[u8; 64]>254 fn get_or_allocate_instance_id(
255 service: &dyn IVirtualizationService,
256 instance_id_file: PathBuf,
257 ) -> Result<[u8; 64]> {
258 let mut instance_id = [0; 64];
259 if instance_id_file.exists() {
260 let mut file = File::open(&instance_id_file)?;
261 file.read_exact(&mut instance_id)?;
262 } else {
263 info!("Allocating a new instance ID for the Service VM");
264 instance_id = service.allocateInstanceId()?;
265 fs::write(instance_id_file, instance_id)?;
266 }
267 Ok(instance_id)
268 }
269
270 /// Returns the file descriptor of the instance image at the given path.
instance_img( service: &dyn IVirtualizationService, instance_img_path: PathBuf, ) -> Result<ParcelFileDescriptor>271 fn instance_img(
272 service: &dyn IVirtualizationService,
273 instance_img_path: PathBuf,
274 ) -> Result<ParcelFileDescriptor> {
275 if instance_img_path.exists() {
276 // TODO(b/298174584): Try to recover if the service VM is triggered by rkpd.
277 return Ok(OpenOptions::new()
278 .read(true)
279 .write(true)
280 .open(instance_img_path)
281 .map(ParcelFileDescriptor::new)?);
282 }
283 let instance_img = OpenOptions::new()
284 .create(true)
285 .truncate(true)
286 .read(true)
287 .write(true)
288 .open(instance_img_path)
289 .map(ParcelFileDescriptor::new)?;
290 service.initializeWritablePartition(
291 &instance_img,
292 INSTANCE_IMG_SIZE_BYTES,
293 PartitionType::ANDROID_VM_INSTANCE,
294 )?;
295 Ok(instance_img)
296 }
297
298 /// This function is only exposed for testing.
android_log_fd() -> io::Result<File>299 pub fn android_log_fd() -> io::Result<File> {
300 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
301
302 let reader = File::from(reader_fd);
303 let writer = File::from(writer_fd);
304
305 thread::spawn(|| {
306 for line in BufReader::new(reader).lines() {
307 match line {
308 Ok(l) => info!("{}", l),
309 Err(e) => {
310 warn!("Failed to read line: {e:?}");
311 break;
312 }
313 }
314 }
315 });
316 Ok(writer)
317 }
318