Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 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 | //! Implementation of the AIDL interface of the VirtualizationService. |
| 16 | |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 17 | use anyhow::{anyhow, Context}; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 18 | use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVfioHandler::IVfioHandler; |
| 19 | use android_system_virtualizationservice_internal::binder::ParcelFileDescriptor; |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 20 | use binder::{self, ExceptionCode, Interface, IntoBinderResult}; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 21 | use lazy_static::lazy_static; |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 22 | use std::fs::{read_link, write, File}; |
| 23 | use std::io::{Read, Seek, SeekFrom, Write}; |
| 24 | use std::mem::size_of; |
| 25 | use std::path::{Path, PathBuf}; |
| 26 | use rustutils::system_properties; |
| 27 | use zerocopy::{ |
| 28 | byteorder::{BigEndian, U32}, |
Frederick Mayle | 2e77994 | 2023-10-15 18:27:31 +0000 | [diff] [blame] | 29 | FromZeroes, |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 30 | FromBytes, |
| 31 | }; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 32 | |
| 33 | #[derive(Debug, Default)] |
| 34 | pub struct VfioHandler {} |
| 35 | |
| 36 | impl VfioHandler { |
| 37 | pub fn init() -> VfioHandler { |
| 38 | VfioHandler::default() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Interface for VfioHandler {} |
| 43 | |
| 44 | impl IVfioHandler for VfioHandler { |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 45 | fn bindDevicesToVfioDriver(&self, devices: &[String]) -> binder::Result<()> { |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 46 | // permission check is already done by IVirtualizationServiceInternal. |
| 47 | if !*IS_VFIO_SUPPORTED { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 48 | return Err(anyhow!("VFIO-platform not supported")) |
| 49 | .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 50 | } |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 51 | devices.iter().try_for_each(|x| bind_device(Path::new(x)))?; |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 52 | Ok(()) |
| 53 | } |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 54 | |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 55 | 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 Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 60 | |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 61 | 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 Kim | f36347b | 2023-08-03 12:52:48 +0900 | [diff] [blame] | 73 | Ok(()) |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | const DEV_VFIO_PATH: &str = "/dev/vfio/vfio"; |
| 78 | const SYSFS_PLATFORM_DEVICES_PATH: &str = "/sys/devices/platform/"; |
| 79 | const VFIO_PLATFORM_DRIVER_PATH: &str = "/sys/bus/platform/drivers/vfio-platform"; |
| 80 | const SYSFS_PLATFORM_DRIVERS_PROBE_PATH: &str = "/sys/bus/platform/drivers_probe"; |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 81 | const DT_TABLE_MAGIC: u32 = 0xd7b7ab1e; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 82 | |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 83 | /// The structure of DT table header in dtbo.img. |
| 84 | /// https://source.android.com/docs/core/architecture/dto/partitions |
| 85 | #[repr(C)] |
Frederick Mayle | 2e77994 | 2023-10-15 18:27:31 +0000 | [diff] [blame] | 86 | #[derive(Debug, FromZeroes, FromBytes)] |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 87 | struct 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 Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 107 | /// 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 Mayle | 2e77994 | 2023-10-15 18:27:31 +0000 | [diff] [blame] | 110 | #[derive(Debug, FromZeroes, FromBytes)] |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 111 | struct 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 | |
| 124 | lazy_static! { |
| 125 | static ref IS_VFIO_SUPPORTED: bool = |
| 126 | Path::new(DEV_VFIO_PATH).exists() && Path::new(VFIO_PLATFORM_DRIVER_PATH).exists(); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | fn check_platform_device(path: &Path) -> binder::Result<()> { |
| 130 | if !path.exists() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 131 | return Err(anyhow!("no such device {path:?}")) |
| 132 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if !path.starts_with(SYSFS_PLATFORM_DEVICES_PATH) { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 136 | return Err(anyhow!("{path:?} is not a platform device")) |
| 137 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Ok(()) |
| 141 | } |
| 142 | |
| 143 | fn 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 | |
| 149 | fn 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 | |
| 159 | fn 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 Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 167 | return Err(anyhow!("can't get device name from {path:?}")) |
| 168 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 169 | }; |
| 170 | let Some(device_str) = device.to_str() else { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 171 | return Err(anyhow!("invalid filename {device:?}")) |
| 172 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 173 | }; |
Inseob Kim | 55438b2 | 2023-08-09 20:16:01 +0900 | [diff] [blame] | 174 | let unbind_path = path.join("driver/unbind"); |
| 175 | if unbind_path.exists() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 176 | write(&unbind_path, device_str.as_bytes()) |
| 177 | .with_context(|| format!("could not unbind {device_str}")) |
| 178 | .or_service_specific_exception(-1)?; |
Inseob Kim | 55438b2 | 2023-08-09 20:16:01 +0900 | [diff] [blame] | 179 | } |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 180 | |
| 181 | // bind to VFIO |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 182 | 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 Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 185 | |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 186 | 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 Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 189 | |
| 190 | // final check |
| 191 | if !is_bound_to_vfio_driver(path) { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 192 | return Err(anyhow!("{path:?} still not bound to vfio driver")) |
| 193 | .or_service_specific_exception(-1); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if get_device_iommu_group(path).is_none() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 197 | return Err(anyhow!("can't get iommu group for {path:?}")) |
| 198 | .or_service_specific_exception(-1); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | Ok(()) |
| 202 | } |
| 203 | |
| 204 | fn bind_device(path: &Path) -> binder::Result<()> { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 205 | let path = path |
| 206 | .canonicalize() |
| 207 | .with_context(|| format!("can't canonicalize {path:?}")) |
| 208 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 209 | |
| 210 | check_platform_device(&path)?; |
| 211 | bind_vfio_driver(&path) |
| 212 | } |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 213 | |
| 214 | fn 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 | |
| 223 | fn 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 | |
| 234 | fn 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 | { |
Charisee | e031edc | 2023-10-16 21:52:10 +0000 | [diff] [blame^] | 242 | return Err(anyhow!("DtTableHeader is invalid")).or_service_specific_exception(-1); |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 243 | } |
| 244 | Ok(dt_table_header) |
| 245 | } |
| 246 | |
| 247 | fn 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() { |
Charisee | e031edc | 2023-10-16 21:52:10 +0000 | [diff] [blame^] | 253 | return Err(anyhow!("Invalid dtbo index {index}")).or_service_specific_exception(-1); |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 254 | } |
| 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 Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 272 | fn write_vm_full_dtbo_from_img( |
Seungjae Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 273 | 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 Yoo | db2d9cb | 2023-08-14 09:11:29 +0900 | [diff] [blame] | 291 | dtbo_fd |
| 292 | .write_all(&buffer) |
| 293 | .context("Failed to write dtbo file") |
| 294 | .or_service_specific_exception(-1)?; |
| 295 | Ok(()) |
| 296 | } |