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; |
Inseob Kim | f36347b | 2023-08-03 12:52:48 +0900 | [diff] [blame] | 22 | use std::fs::{read_link, write}; |
| 23 | use std::io::Write; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 24 | use std::path::Path; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 25 | |
| 26 | #[derive(Debug, Default)] |
| 27 | pub struct VfioHandler {} |
| 28 | |
| 29 | impl VfioHandler { |
| 30 | pub fn init() -> VfioHandler { |
| 31 | VfioHandler::default() |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl Interface for VfioHandler {} |
| 36 | |
| 37 | impl IVfioHandler for VfioHandler { |
Inseob Kim | f36347b | 2023-08-03 12:52:48 +0900 | [diff] [blame] | 38 | fn bindDevicesToVfioDriver( |
| 39 | &self, |
| 40 | devices: &[String], |
| 41 | dtbo: &ParcelFileDescriptor, |
| 42 | ) -> binder::Result<()> { |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 43 | // permission check is already done by IVirtualizationServiceInternal. |
| 44 | if !*IS_VFIO_SUPPORTED { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 45 | return Err(anyhow!("VFIO-platform not supported")) |
| 46 | .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | devices.iter().try_for_each(|x| bind_device(Path::new(x)))?; |
| 50 | |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 51 | let mut dtbo = dtbo |
| 52 | .as_ref() |
| 53 | .try_clone() |
| 54 | .context("Failed to clone File from ParcelFileDescriptor") |
| 55 | .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?; |
Inseob Kim | f36347b | 2023-08-03 12:52:48 +0900 | [diff] [blame] | 56 | // TODO(b/291191362): write DTBO for devices to dtbo. |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 57 | dtbo.write(b"\n") |
| 58 | .context("Can't write to ParcelFileDescriptor") |
| 59 | .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?; |
Inseob Kim | f36347b | 2023-08-03 12:52:48 +0900 | [diff] [blame] | 60 | Ok(()) |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | const DEV_VFIO_PATH: &str = "/dev/vfio/vfio"; |
| 65 | const SYSFS_PLATFORM_DEVICES_PATH: &str = "/sys/devices/platform/"; |
| 66 | const VFIO_PLATFORM_DRIVER_PATH: &str = "/sys/bus/platform/drivers/vfio-platform"; |
| 67 | const SYSFS_PLATFORM_DRIVERS_PROBE_PATH: &str = "/sys/bus/platform/drivers_probe"; |
| 68 | |
| 69 | lazy_static! { |
| 70 | static ref IS_VFIO_SUPPORTED: bool = is_vfio_supported(); |
| 71 | } |
| 72 | |
| 73 | fn is_vfio_supported() -> bool { |
| 74 | Path::new(DEV_VFIO_PATH).exists() && Path::new(VFIO_PLATFORM_DRIVER_PATH).exists() |
| 75 | } |
| 76 | |
| 77 | fn check_platform_device(path: &Path) -> binder::Result<()> { |
| 78 | if !path.exists() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 79 | return Err(anyhow!("no such device {path:?}")) |
| 80 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if !path.starts_with(SYSFS_PLATFORM_DEVICES_PATH) { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 84 | return Err(anyhow!("{path:?} is not a platform device")) |
| 85 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
| 91 | fn get_device_iommu_group(path: &Path) -> Option<u64> { |
| 92 | let group_path = read_link(path.join("iommu_group")).ok()?; |
| 93 | let group = group_path.file_name()?; |
| 94 | group.to_str()?.parse().ok() |
| 95 | } |
| 96 | |
| 97 | fn is_bound_to_vfio_driver(path: &Path) -> bool { |
| 98 | let Ok(driver_path) = read_link(path.join("driver")) else { |
| 99 | return false; |
| 100 | }; |
| 101 | let Some(driver) = driver_path.file_name() else { |
| 102 | return false; |
| 103 | }; |
| 104 | driver.to_str().unwrap_or("") == "vfio-platform" |
| 105 | } |
| 106 | |
| 107 | fn bind_vfio_driver(path: &Path) -> binder::Result<()> { |
| 108 | if is_bound_to_vfio_driver(path) { |
| 109 | // already bound |
| 110 | return Ok(()); |
| 111 | } |
| 112 | |
| 113 | // unbind |
| 114 | let Some(device) = path.file_name() else { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 115 | return Err(anyhow!("can't get device name from {path:?}")) |
| 116 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 117 | }; |
| 118 | let Some(device_str) = device.to_str() else { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 119 | return Err(anyhow!("invalid filename {device:?}")) |
| 120 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 121 | }; |
Inseob Kim | 55438b2 | 2023-08-09 20:16:01 +0900 | [diff] [blame] | 122 | let unbind_path = path.join("driver/unbind"); |
| 123 | if unbind_path.exists() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 124 | write(&unbind_path, device_str.as_bytes()) |
| 125 | .with_context(|| format!("could not unbind {device_str}")) |
| 126 | .or_service_specific_exception(-1)?; |
Inseob Kim | 55438b2 | 2023-08-09 20:16:01 +0900 | [diff] [blame] | 127 | } |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 128 | |
| 129 | // bind to VFIO |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 130 | write(path.join("driver_override"), b"vfio-platform") |
| 131 | .with_context(|| format!("could not bind {device_str} to vfio-platform")) |
| 132 | .or_service_specific_exception(-1)?; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 133 | |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 134 | write(SYSFS_PLATFORM_DRIVERS_PROBE_PATH, device_str.as_bytes()) |
| 135 | .with_context(|| format!("could not write {device_str} to drivers-probe")) |
| 136 | .or_service_specific_exception(-1)?; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 137 | |
| 138 | // final check |
| 139 | if !is_bound_to_vfio_driver(path) { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 140 | return Err(anyhow!("{path:?} still not bound to vfio driver")) |
| 141 | .or_service_specific_exception(-1); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if get_device_iommu_group(path).is_none() { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 145 | return Err(anyhow!("can't get iommu group for {path:?}")) |
| 146 | .or_service_specific_exception(-1); |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | fn bind_device(path: &Path) -> binder::Result<()> { |
Jiyong Park | 2227eaa | 2023-08-04 11:59:18 +0900 | [diff] [blame] | 153 | let path = path |
| 154 | .canonicalize() |
| 155 | .with_context(|| format!("can't canonicalize {path:?}")) |
| 156 | .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?; |
Inseob Kim | bdca047 | 2023-07-28 19:20:56 +0900 | [diff] [blame] | 157 | |
| 158 | check_platform_device(&path)?; |
| 159 | bind_vfio_driver(&path) |
| 160 | } |