blob: 2968ff912f8027bd0a3c254f83c0a5d6b7b0da6f [file] [log] [blame]
Inseob Kimbdca0472023-07-28 19:20:56 +09001// 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//! Implementation of the AIDL interface of the VirtualizationService.
16
Jiyong Park2227eaa2023-08-04 11:59:18 +090017use anyhow::{anyhow, Context};
Inseob Kimbdca0472023-07-28 19:20:56 +090018use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVfioHandler::IVfioHandler;
19use android_system_virtualizationservice_internal::binder::ParcelFileDescriptor;
Jiyong Park2227eaa2023-08-04 11:59:18 +090020use binder::{self, ExceptionCode, Interface, IntoBinderResult};
Inseob Kimbdca0472023-07-28 19:20:56 +090021use lazy_static::lazy_static;
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090022use std::fs::{read_link, write, File};
23use std::io::{Read, Seek, SeekFrom, Write};
24use std::mem::size_of;
25use std::path::{Path, PathBuf};
26use rustutils::system_properties;
27use zerocopy::{
28 byteorder::{BigEndian, U32},
Frederick Mayle2e779942023-10-15 18:27:31 +000029 FromZeroes,
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090030 FromBytes,
31};
Inseob Kimbdca0472023-07-28 19:20:56 +090032
33#[derive(Debug, Default)]
34pub struct VfioHandler {}
35
36impl VfioHandler {
37 pub fn init() -> VfioHandler {
38 VfioHandler::default()
39 }
40}
41
42impl Interface for VfioHandler {}
43
44impl IVfioHandler for VfioHandler {
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090045 fn bindDevicesToVfioDriver(&self, devices: &[String]) -> binder::Result<()> {
Inseob Kimbdca0472023-07-28 19:20:56 +090046 // permission check is already done by IVirtualizationServiceInternal.
47 if !*IS_VFIO_SUPPORTED {
Jiyong Park2227eaa2023-08-04 11:59:18 +090048 return Err(anyhow!("VFIO-platform not supported"))
49 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION);
Inseob Kimbdca0472023-07-28 19:20:56 +090050 }
Inseob Kimbdca0472023-07-28 19:20:56 +090051 devices.iter().try_for_each(|x| bind_device(Path::new(x)))?;
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090052 Ok(())
53 }
Inseob Kimbdca0472023-07-28 19:20:56 +090054
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090055 fn writeVmDtbo(&self, dtbo_fd: &ParcelFileDescriptor) -> binder::Result<()> {
56 let dtbo_path = get_dtbo_img_path()?;
57 let mut dtbo_img = File::open(dtbo_path)
58 .context("Failed to open DTBO partition")
59 .or_service_specific_exception(-1)?;
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090060
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090061 let dt_table_header = get_dt_table_header(&mut dtbo_img)?;
62 let vm_dtbo_idx = system_properties::read("ro.boot.hypervisor.vm_dtbo_idx")
63 .context("Failed to read vm_dtbo_idx")
64 .or_service_specific_exception(-1)?
65 .ok_or_else(|| anyhow!("vm_dtbo_idx is none"))
66 .or_service_specific_exception(-1)?;
67 let vm_dtbo_idx = vm_dtbo_idx
68 .parse()
69 .context("vm_dtbo_idx is not an integer")
70 .or_service_specific_exception(-1)?;
71 let dt_table_entry = get_dt_table_entry(&mut dtbo_img, &dt_table_header, vm_dtbo_idx)?;
72 write_vm_full_dtbo_from_img(&mut dtbo_img, &dt_table_entry, dtbo_fd)?;
Inseob Kimf36347b2023-08-03 12:52:48 +090073 Ok(())
Inseob Kimbdca0472023-07-28 19:20:56 +090074 }
75}
76
77const DEV_VFIO_PATH: &str = "/dev/vfio/vfio";
78const SYSFS_PLATFORM_DEVICES_PATH: &str = "/sys/devices/platform/";
79const VFIO_PLATFORM_DRIVER_PATH: &str = "/sys/bus/platform/drivers/vfio-platform";
80const SYSFS_PLATFORM_DRIVERS_PROBE_PATH: &str = "/sys/bus/platform/drivers_probe";
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090081const DT_TABLE_MAGIC: u32 = 0xd7b7ab1e;
Inseob Kimbdca0472023-07-28 19:20:56 +090082
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090083/// The structure of DT table header in dtbo.img.
84/// https://source.android.com/docs/core/architecture/dto/partitions
85#[repr(C)]
Frederick Mayle2e779942023-10-15 18:27:31 +000086#[derive(Debug, FromZeroes, FromBytes)]
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +090087struct DtTableHeader {
88 /// DT_TABLE_MAGIC
89 magic: U32<BigEndian>,
90 /// includes dt_table_header + all dt_table_entry and all dtb/dtbo
91 _total_size: U32<BigEndian>,
92 /// sizeof(dt_table_header)
93 header_size: U32<BigEndian>,
94 /// sizeof(dt_table_entry)
95 dt_entry_size: U32<BigEndian>,
96 /// number of dt_table_entry
97 dt_entry_count: U32<BigEndian>,
98 /// offset to the first dt_table_entry from head of dt_table_header
99 dt_entries_offset: U32<BigEndian>,
100 /// flash page size we assume
101 _page_size: U32<BigEndian>,
102 /// DTBO image version, the current version is 0. The version will be
103 /// incremented when the dt_table_header struct is updated.
104 _version: U32<BigEndian>,
Inseob Kimbdca0472023-07-28 19:20:56 +0900105}
106
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900107/// The structure of each DT table entry (v0) in dtbo.img.
108/// https://source.android.com/docs/core/architecture/dto/partitions
109#[repr(C)]
Frederick Mayle2e779942023-10-15 18:27:31 +0000110#[derive(Debug, FromZeroes, FromBytes)]
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900111struct DtTableEntry {
112 /// size of each DT
113 dt_size: U32<BigEndian>,
114 /// offset from head of dt_table_header
115 dt_offset: U32<BigEndian>,
116 /// optional, must be zero if unused
117 _id: U32<BigEndian>,
118 /// optional, must be zero if unused
119 _rev: U32<BigEndian>,
120 /// optional, must be zero if unused
121 _custom: [U32<BigEndian>; 4],
122}
123
124lazy_static! {
125 static ref IS_VFIO_SUPPORTED: bool =
126 Path::new(DEV_VFIO_PATH).exists() && Path::new(VFIO_PLATFORM_DRIVER_PATH).exists();
Inseob Kimbdca0472023-07-28 19:20:56 +0900127}
128
129fn check_platform_device(path: &Path) -> binder::Result<()> {
130 if !path.exists() {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900131 return Err(anyhow!("no such device {path:?}"))
132 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Inseob Kimbdca0472023-07-28 19:20:56 +0900133 }
134
135 if !path.starts_with(SYSFS_PLATFORM_DEVICES_PATH) {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900136 return Err(anyhow!("{path:?} is not a platform device"))
137 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Inseob Kimbdca0472023-07-28 19:20:56 +0900138 }
139
140 Ok(())
141}
142
143fn get_device_iommu_group(path: &Path) -> Option<u64> {
144 let group_path = read_link(path.join("iommu_group")).ok()?;
145 let group = group_path.file_name()?;
146 group.to_str()?.parse().ok()
147}
148
149fn is_bound_to_vfio_driver(path: &Path) -> bool {
150 let Ok(driver_path) = read_link(path.join("driver")) else {
151 return false;
152 };
153 let Some(driver) = driver_path.file_name() else {
154 return false;
155 };
156 driver.to_str().unwrap_or("") == "vfio-platform"
157}
158
159fn bind_vfio_driver(path: &Path) -> binder::Result<()> {
160 if is_bound_to_vfio_driver(path) {
161 // already bound
162 return Ok(());
163 }
164
165 // unbind
166 let Some(device) = path.file_name() else {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900167 return Err(anyhow!("can't get device name from {path:?}"))
168 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Inseob Kimbdca0472023-07-28 19:20:56 +0900169 };
170 let Some(device_str) = device.to_str() else {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900171 return Err(anyhow!("invalid filename {device:?}"))
172 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Inseob Kimbdca0472023-07-28 19:20:56 +0900173 };
Inseob Kim55438b22023-08-09 20:16:01 +0900174 let unbind_path = path.join("driver/unbind");
175 if unbind_path.exists() {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900176 write(&unbind_path, device_str.as_bytes())
177 .with_context(|| format!("could not unbind {device_str}"))
178 .or_service_specific_exception(-1)?;
Inseob Kim55438b22023-08-09 20:16:01 +0900179 }
Inseob Kimbdca0472023-07-28 19:20:56 +0900180
181 // bind to VFIO
Jiyong Park2227eaa2023-08-04 11:59:18 +0900182 write(path.join("driver_override"), b"vfio-platform")
183 .with_context(|| format!("could not bind {device_str} to vfio-platform"))
184 .or_service_specific_exception(-1)?;
Inseob Kimbdca0472023-07-28 19:20:56 +0900185
Jiyong Park2227eaa2023-08-04 11:59:18 +0900186 write(SYSFS_PLATFORM_DRIVERS_PROBE_PATH, device_str.as_bytes())
187 .with_context(|| format!("could not write {device_str} to drivers-probe"))
188 .or_service_specific_exception(-1)?;
Inseob Kimbdca0472023-07-28 19:20:56 +0900189
190 // final check
191 if !is_bound_to_vfio_driver(path) {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900192 return Err(anyhow!("{path:?} still not bound to vfio driver"))
193 .or_service_specific_exception(-1);
Inseob Kimbdca0472023-07-28 19:20:56 +0900194 }
195
196 if get_device_iommu_group(path).is_none() {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900197 return Err(anyhow!("can't get iommu group for {path:?}"))
198 .or_service_specific_exception(-1);
Inseob Kimbdca0472023-07-28 19:20:56 +0900199 }
200
201 Ok(())
202}
203
204fn bind_device(path: &Path) -> binder::Result<()> {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900205 let path = path
206 .canonicalize()
207 .with_context(|| format!("can't canonicalize {path:?}"))
208 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?;
Inseob Kimbdca0472023-07-28 19:20:56 +0900209
210 check_platform_device(&path)?;
211 bind_vfio_driver(&path)
212}
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900213
214fn get_dtbo_img_path() -> binder::Result<PathBuf> {
215 let slot_suffix = system_properties::read("ro.boot.slot_suffix")
216 .context("Failed to read ro.boot.slot_suffix")
217 .or_service_specific_exception(-1)?
218 .ok_or_else(|| anyhow!("slot_suffix is none"))
219 .or_service_specific_exception(-1)?;
220 Ok(PathBuf::from(format!("/dev/block/by-name/dtbo{slot_suffix}")))
221}
222
223fn read_values(file: &mut File, size: usize, offset: u64) -> binder::Result<Vec<u8>> {
224 file.seek(SeekFrom::Start(offset))
225 .context("Cannot seek the offset")
226 .or_service_specific_exception(-1)?;
227 let mut buffer = vec![0_u8; size];
228 file.read_exact(&mut buffer)
229 .context("Failed to read buffer")
230 .or_service_specific_exception(-1)?;
231 Ok(buffer)
232}
233
234fn get_dt_table_header(file: &mut File) -> binder::Result<DtTableHeader> {
235 let values = read_values(file, size_of::<DtTableHeader>(), 0)?;
236 let dt_table_header = DtTableHeader::read_from(values.as_slice())
237 .context("DtTableHeader is invalid")
238 .or_service_specific_exception(-1)?;
239 if dt_table_header.magic.get() != DT_TABLE_MAGIC
240 || dt_table_header.header_size.get() as usize != size_of::<DtTableHeader>()
241 {
Chariseee031edc2023-10-16 21:52:10 +0000242 return Err(anyhow!("DtTableHeader is invalid")).or_service_specific_exception(-1);
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900243 }
244 Ok(dt_table_header)
245}
246
247fn get_dt_table_entry(
248 file: &mut File,
249 header: &DtTableHeader,
250 index: u32,
251) -> binder::Result<DtTableEntry> {
252 if index >= header.dt_entry_count.get() {
Chariseee031edc2023-10-16 21:52:10 +0000253 return Err(anyhow!("Invalid dtbo index {index}")).or_service_specific_exception(-1);
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900254 }
255 let Some(prev_dt_entry_total_size) = header.dt_entry_size.get().checked_mul(index) else {
256 return Err(anyhow!("Unexpected arithmetic result"))
257 .or_binder_exception(ExceptionCode::ILLEGAL_STATE);
258 };
259 let Some(dt_entry_offset) =
260 prev_dt_entry_total_size.checked_add(header.dt_entries_offset.get())
261 else {
262 return Err(anyhow!("Unexpected arithmetic result"))
263 .or_binder_exception(ExceptionCode::ILLEGAL_STATE);
264 };
265 let values = read_values(file, size_of::<DtTableEntry>(), dt_entry_offset.into())?;
266 let dt_table_entry = DtTableEntry::read_from(values.as_slice())
267 .with_context(|| format!("DtTableEntry at index {index} is invalid."))
268 .or_service_specific_exception(-1)?;
269 Ok(dt_table_entry)
270}
271
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +0900272fn write_vm_full_dtbo_from_img(
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900273 dtbo_img_file: &mut File,
274 entry: &DtTableEntry,
275 dtbo_fd: &ParcelFileDescriptor,
276) -> binder::Result<()> {
277 let dt_size = entry
278 .dt_size
279 .get()
280 .try_into()
281 .context("Failed to convert type")
282 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)?;
283 let buffer = read_values(dtbo_img_file, dt_size, entry.dt_offset.get().into())?;
284
285 let mut dtbo_fd = dtbo_fd
286 .as_ref()
287 .try_clone()
288 .context("Failed to clone File from ParcelFileDescriptor")
289 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?;
290
Seungjae Yoodb2d9cb2023-08-14 09:11:29 +0900291 dtbo_fd
292 .write_all(&buffer)
293 .context("Failed to write dtbo file")
294 .or_service_specific_exception(-1)?;
295 Ok(())
296}