Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | #![cfg_attr(not(test), no_std)] |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 5 | #![deny(clippy::undocumented_unsafe_blocks)] |
| 6 | #![deny(unsafe_op_in_unsafe_fn)] |
| 7 | #![doc = include_str!("../README.md")] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 8 | |
Andrew Walbran | 19970ba | 2024-11-25 15:35:00 +0000 | [diff] [blame] | 9 | use core::fmt::{self, Debug, Display, Formatter}; |
Andrew Walbran | 44029a0 | 2024-11-25 15:34:31 +0000 | [diff] [blame] | 10 | use num_enum::{IntoPrimitive, TryFromPrimitive}; |
| 11 | use thiserror::Error; |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 12 | use uuid::Uuid; |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 13 | use zerocopy::transmute; |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 14 | |
| 15 | pub mod boot_info; |
Balint Dobszay | b2ff2bc | 2024-12-19 18:59:38 +0100 | [diff] [blame] | 16 | mod ffa_v1_1; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 17 | mod ffa_v1_2; |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 18 | pub mod memory_management; |
| 19 | pub mod partition_info; |
| 20 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 21 | /// Constant for 4K page size. On many occasions the FF-A spec defines memory size as count of 4K |
| 22 | /// pages, regardless of the current translation granule. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 23 | pub const FFA_PAGE_SIZE_4K: usize = 4096; |
| 24 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 25 | /// Rich error types returned by this module. Should be converted to [`crate::FfaError`] when used |
| 26 | /// with the `FFA_ERROR` interface. |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 27 | #[derive(Debug, Error, PartialEq)] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 28 | pub enum Error { |
| 29 | #[error("Unrecognised FF-A function ID {0}")] |
| 30 | UnrecognisedFunctionId(u32), |
| 31 | #[error("Unrecognised FF-A feature ID {0}")] |
| 32 | UnrecognisedFeatureId(u8), |
| 33 | #[error("Unrecognised FF-A error code {0}")] |
| 34 | UnrecognisedErrorCode(i32), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 35 | #[error("Unrecognised FF-A Framework Message {0}")] |
| 36 | UnrecognisedFwkMsg(u32), |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 37 | #[error("Invalid FF-A Msg Wait Flag {0}")] |
| 38 | InvalidMsgWaitFlag(u32), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 39 | #[error("Unrecognised VM availability status {0}")] |
| 40 | UnrecognisedVmAvailabilityStatus(i32), |
| 41 | #[error("Unrecognised FF-A Warm Boot Type {0}")] |
| 42 | UnrecognisedWarmBootType(u32), |
| 43 | #[error("Invalid version {0}")] |
| 44 | InvalidVersion(u32), |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 45 | #[error("Invalid Information Tag {0}")] |
| 46 | InvalidInformationTag(u16), |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 47 | #[error("Invalid Flag for Notification Set")] |
| 48 | InvalidNotificationSetFlag(u32), |
| 49 | #[error("Invalid Vm ID")] |
| 50 | InvalidVmId(u32), |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 51 | #[error("Invalid FF-A Partition Info Get Flag {0}")] |
| 52 | InvalidPartitionInfoGetFlag(u32), |
Imre Kis | 839eaef | 2025-04-11 17:38:36 +0200 | [diff] [blame] | 53 | #[error("Invalid success argument variant")] |
| 54 | InvalidSuccessArgsVariant, |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 55 | #[error("Invalid notification count")] |
| 56 | InvalidNotificationCount, |
Imre Kis | 92b663e | 2025-04-10 14:15:05 +0200 | [diff] [blame] | 57 | #[error("Invalid Partition Info Get Regs response")] |
| 58 | InvalidPartitionInfoGetRegsResponse, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | impl From<Error> for FfaError { |
| 62 | fn from(value: Error) -> Self { |
| 63 | match value { |
| 64 | Error::UnrecognisedFunctionId(_) | Error::UnrecognisedFeatureId(_) => { |
| 65 | Self::NotSupported |
| 66 | } |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 67 | Error::InvalidInformationTag(_) => Self::Retry, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 68 | Error::UnrecognisedErrorCode(_) |
| 69 | | Error::UnrecognisedFwkMsg(_) |
| 70 | | Error::InvalidVersion(_) |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 71 | | Error::InvalidMsgWaitFlag(_) |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 72 | | Error::UnrecognisedVmAvailabilityStatus(_) |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 73 | | Error::InvalidNotificationSetFlag(_) |
| 74 | | Error::InvalidVmId(_) |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 75 | | Error::UnrecognisedWarmBootType(_) |
Imre Kis | 839eaef | 2025-04-11 17:38:36 +0200 | [diff] [blame] | 76 | | Error::InvalidPartitionInfoGetFlag(_) |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 77 | | Error::InvalidSuccessArgsVariant |
Imre Kis | 92b663e | 2025-04-10 14:15:05 +0200 | [diff] [blame] | 78 | | Error::InvalidNotificationCount |
| 79 | | Error::InvalidPartitionInfoGetRegsResponse => Self::InvalidParameters, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 83 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 84 | /// An FF-A instance is a valid combination of two FF-A components at an exception level boundary. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 85 | #[derive(PartialEq, Clone, Copy)] |
| 86 | pub enum Instance { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 87 | /// The instance between the SPMC and SPMD. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 88 | SecurePhysical, |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 89 | /// The instance between the SPMC and a physical SP (contains the SP's endpoint ID). |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 90 | SecureVirtual(u16), |
| 91 | } |
| 92 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 93 | /// Function IDs of the various FF-A interfaces. |
Andrew Walbran | 969b925 | 2024-11-25 15:35:42 +0000 | [diff] [blame] | 94 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 95 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFunctionId))] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 96 | #[repr(u32)] |
| 97 | pub enum FuncId { |
| 98 | Error = 0x84000060, |
| 99 | Success32 = 0x84000061, |
| 100 | Success64 = 0xc4000061, |
| 101 | Interrupt = 0x84000062, |
| 102 | Version = 0x84000063, |
| 103 | Features = 0x84000064, |
| 104 | RxAcquire = 0x84000084, |
| 105 | RxRelease = 0x84000065, |
| 106 | RxTxMap32 = 0x84000066, |
| 107 | RxTxMap64 = 0xc4000066, |
| 108 | RxTxUnmap = 0x84000067, |
| 109 | PartitionInfoGet = 0x84000068, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 110 | PartitionInfoGetRegs = 0xc400008b, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 111 | IdGet = 0x84000069, |
| 112 | SpmIdGet = 0x84000085, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 113 | ConsoleLog32 = 0x8400008a, |
| 114 | ConsoleLog64 = 0xc400008a, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 115 | MsgWait = 0x8400006b, |
| 116 | Yield = 0x8400006c, |
| 117 | Run = 0x8400006d, |
| 118 | NormalWorldResume = 0x8400007c, |
| 119 | MsgSend2 = 0x84000086, |
| 120 | MsgSendDirectReq32 = 0x8400006f, |
| 121 | MsgSendDirectReq64 = 0xc400006f, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 122 | MsgSendDirectReq64_2 = 0xc400008d, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 123 | MsgSendDirectResp32 = 0x84000070, |
| 124 | MsgSendDirectResp64 = 0xc4000070, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 125 | MsgSendDirectResp64_2 = 0xc400008e, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 126 | NotificationBitmapCreate = 0x8400007d, |
| 127 | NotificationBitmapDestroy = 0x8400007e, |
| 128 | NotificationBind = 0x8400007f, |
| 129 | NotificationUnbind = 0x84000080, |
| 130 | NotificationSet = 0x84000081, |
| 131 | NotificationGet = 0x84000082, |
| 132 | NotificationInfoGet32 = 0x84000083, |
| 133 | NotificationInfoGet64 = 0xc4000083, |
| 134 | El3IntrHandle = 0x8400008c, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 135 | SecondaryEpRegister32 = 0x84000087, |
| 136 | SecondaryEpRegister64 = 0xc4000087, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 137 | MemDonate32 = 0x84000071, |
| 138 | MemDonate64 = 0xc4000071, |
| 139 | MemLend32 = 0x84000072, |
| 140 | MemLend64 = 0xc4000072, |
| 141 | MemShare32 = 0x84000073, |
| 142 | MemShare64 = 0xc4000073, |
| 143 | MemRetrieveReq32 = 0x84000074, |
| 144 | MemRetrieveReq64 = 0xc4000074, |
| 145 | MemRetrieveResp = 0x84000075, |
| 146 | MemRelinquish = 0x84000076, |
| 147 | MemReclaim = 0x84000077, |
| 148 | MemPermGet32 = 0x84000088, |
| 149 | MemPermGet64 = 0xc4000088, |
| 150 | MemPermSet32 = 0x84000089, |
| 151 | MemPermSet64 = 0xc4000089, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 154 | impl FuncId { |
| 155 | /// Returns true if this is a 32-bit call, or false if it is a 64-bit call. |
| 156 | pub fn is_32bit(&self) -> bool { |
Tomás González | 6ccba0a | 2025-04-09 13:31:29 +0100 | [diff] [blame] | 157 | u32::from(*self) & (1 << 30) == 0 |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 161 | /// Error status codes used by the `FFA_ERROR` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 162 | #[derive(Clone, Copy, Debug, Eq, Error, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 163 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedErrorCode))] |
| 164 | #[repr(i32)] |
| 165 | pub enum FfaError { |
| 166 | #[error("Not supported")] |
| 167 | NotSupported = -1, |
| 168 | #[error("Invalid parameters")] |
| 169 | InvalidParameters = -2, |
| 170 | #[error("No memory")] |
| 171 | NoMemory = -3, |
| 172 | #[error("Busy")] |
| 173 | Busy = -4, |
| 174 | #[error("Interrupted")] |
| 175 | Interrupted = -5, |
| 176 | #[error("Denied")] |
| 177 | Denied = -6, |
| 178 | #[error("Retry")] |
| 179 | Retry = -7, |
| 180 | #[error("Aborted")] |
| 181 | Aborted = -8, |
| 182 | #[error("No data")] |
| 183 | NoData = -9, |
| 184 | } |
| 185 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 186 | /// Endpoint ID and vCPU ID pair, used by `FFA_ERROR`, `FFA_INTERRUPT` and `FFA_RUN` interfaces. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 187 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 188 | pub struct TargetInfo { |
| 189 | pub endpoint_id: u16, |
| 190 | pub vcpu_id: u16, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 193 | impl From<u32> for TargetInfo { |
| 194 | fn from(value: u32) -> Self { |
| 195 | Self { |
| 196 | endpoint_id: (value >> 16) as u16, |
| 197 | vcpu_id: value as u16, |
| 198 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 202 | impl From<TargetInfo> for u32 { |
| 203 | fn from(value: TargetInfo) -> Self { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 204 | ((value.endpoint_id as u32) << 16) | value.vcpu_id as u32 |
Andrew Walbran | 0d31581 | 2024-11-25 15:36:36 +0000 | [diff] [blame] | 205 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 206 | } |
Andrew Walbran | 0d31581 | 2024-11-25 15:36:36 +0000 | [diff] [blame] | 207 | |
Imre Kis | 839eaef | 2025-04-11 17:38:36 +0200 | [diff] [blame] | 208 | /// Generic arguments of the `FFA_SUCCESS` interface. The interpretation of the arguments depends on |
| 209 | /// the interface that initiated the request. The application code has knowledge of the request, so |
| 210 | /// it has to convert `SuccessArgs` into/from a specific success args structure that matches the |
| 211 | /// request. |
Imre Kis | 4e9d8bc | 2025-04-10 13:48:26 +0200 | [diff] [blame] | 212 | /// |
| 213 | /// The current specialized success arguments types are: |
| 214 | /// * `FFA_FEATURES` - [`SuccessArgsFeatures`] |
Imre Kis | bbef287 | 2025-04-10 14:11:29 +0200 | [diff] [blame] | 215 | /// * `FFA_ID_GET` - [`SuccessArgsIdGet`] |
| 216 | /// * `FFA_SPM_ID_GET` - [`SuccessArgsSpmIdGet`] |
Imre Kis | 61c3409 | 2025-04-10 14:14:38 +0200 | [diff] [blame] | 217 | /// * `FFA_PARTITION_INFO_GET` - [`partition_info::SuccessArgsPartitionInfoGet`] |
Imre Kis | 92b663e | 2025-04-10 14:15:05 +0200 | [diff] [blame] | 218 | /// * `FFA_PARTITION_INFO_GET_REGS` - [`partition_info::SuccessArgsPartitionInfoGetRegs`] |
Imre Kis | 9959e06 | 2025-04-10 14:16:10 +0200 | [diff] [blame] | 219 | /// * `FFA_NOTIFICATION_GET` - [`SuccessArgsNotificationGet`] |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 220 | /// * `FFA_NOTIFICATION_INFO_GET_32` - [`SuccessArgsNotificationInfoGet32`] |
| 221 | /// * `FFA_NOTIFICATION_INFO_GET_64` - [`SuccessArgsNotificationInfoGet64`] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 222 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 223 | pub enum SuccessArgs { |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 224 | Args32([u32; 6]), |
| 225 | Args64([u64; 6]), |
| 226 | Args64_2([u64; 16]), |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Imre Kis | 839eaef | 2025-04-11 17:38:36 +0200 | [diff] [blame] | 229 | impl SuccessArgs { |
| 230 | fn try_get_args32(self) -> Result<[u32; 6], Error> { |
| 231 | match self { |
| 232 | SuccessArgs::Args32(args) => Ok(args), |
| 233 | SuccessArgs::Args64(_) | SuccessArgs::Args64_2(_) => { |
| 234 | Err(Error::InvalidSuccessArgsVariant) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | fn try_get_args64(self) -> Result<[u64; 6], Error> { |
| 240 | match self { |
| 241 | SuccessArgs::Args64(args) => Ok(args), |
| 242 | SuccessArgs::Args32(_) | SuccessArgs::Args64_2(_) => { |
| 243 | Err(Error::InvalidSuccessArgsVariant) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn try_get_args64_2(self) -> Result<[u64; 16], Error> { |
| 249 | match self { |
| 250 | SuccessArgs::Args64_2(args) => Ok(args), |
| 251 | SuccessArgs::Args32(_) | SuccessArgs::Args64(_) => { |
| 252 | Err(Error::InvalidSuccessArgsVariant) |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 258 | /// Entrypoint address argument for `FFA_SECONDARY_EP_REGISTER` interface. |
| 259 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 260 | pub enum SecondaryEpRegisterAddr { |
| 261 | Addr32(u32), |
| 262 | Addr64(u64), |
| 263 | } |
| 264 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 265 | /// Version number of the FF-A implementation, `.0` is the major, `.1` is minor the version. |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 266 | #[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 267 | pub struct Version(pub u16, pub u16); |
| 268 | |
Tomás González | 1f79435 | 2025-03-03 16:47:06 +0000 | [diff] [blame] | 269 | impl Version { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame] | 270 | // The FF-A spec mandates that bit[31] of a version number must be 0 |
| 271 | const MBZ_BITS: u32 = 1 << 31; |
| 272 | |
Tomás González | 1f79435 | 2025-03-03 16:47:06 +0000 | [diff] [blame] | 273 | /// Returns whether the caller's version (self) is compatible with the callee's version (input |
| 274 | /// parameter) |
| 275 | pub fn is_compatible_to(&self, callee_version: &Version) -> bool { |
| 276 | self.0 == callee_version.0 && self.1 <= callee_version.1 |
| 277 | } |
| 278 | } |
| 279 | |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame] | 280 | impl TryFrom<u32> for Version { |
| 281 | type Error = Error; |
| 282 | |
| 283 | fn try_from(val: u32) -> Result<Self, Self::Error> { |
| 284 | if (val & Self::MBZ_BITS) != 0 { |
| 285 | Err(Error::InvalidVersion(val)) |
| 286 | } else { |
| 287 | Ok(Self((val >> 16) as u16, val as u16)) |
| 288 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | impl From<Version> for u32 { |
| 293 | fn from(v: Version) -> Self { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame] | 294 | let v_u32 = ((v.0 as u32) << 16) | v.1 as u32; |
| 295 | assert!(v_u32 & Version::MBZ_BITS == 0); |
| 296 | v_u32 |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Andrew Walbran | 19970ba | 2024-11-25 15:35:00 +0000 | [diff] [blame] | 300 | impl Display for Version { |
| 301 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 302 | write!(f, "{}.{}", self.0, self.1) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | impl Debug for Version { |
| 307 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 308 | Display::fmt(self, f) |
| 309 | } |
| 310 | } |
| 311 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 312 | /// Feature IDs used by the `FFA_FEATURES` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 313 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 314 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFeatureId))] |
| 315 | #[repr(u8)] |
| 316 | pub enum FeatureId { |
| 317 | NotificationPendingInterrupt = 0x1, |
| 318 | ScheduleReceiverInterrupt = 0x2, |
| 319 | ManagedExitInterrupt = 0x3, |
| 320 | } |
Balint Dobszay | c880249 | 2025-01-15 18:11:39 +0100 | [diff] [blame] | 321 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 322 | /// Arguments for the `FFA_FEATURES` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 323 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 324 | pub enum Feature { |
| 325 | FuncId(FuncId), |
| 326 | FeatureId(FeatureId), |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 327 | Unknown(u32), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 328 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 329 | |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 330 | impl From<u32> for Feature { |
| 331 | fn from(value: u32) -> Self { |
| 332 | // Bit[31] is set for all valid FF-A function IDs so we don't have to check it separately |
| 333 | if let Ok(func_id) = value.try_into() { |
| 334 | Self::FuncId(func_id) |
| 335 | } else if let Ok(feat_id) = (value as u8).try_into() { |
| 336 | Self::FeatureId(feat_id) |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 337 | } else { |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 338 | Self::Unknown(value) |
| 339 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | impl From<Feature> for u32 { |
| 344 | fn from(value: Feature) -> Self { |
| 345 | match value { |
| 346 | Feature::FuncId(func_id) => (1 << 31) | func_id as u32, |
| 347 | Feature::FeatureId(feature_id) => feature_id as u32, |
Imre Kis | 29c8ace | 2025-04-11 13:49:58 +0200 | [diff] [blame^] | 348 | Feature::Unknown(id) => id, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Imre Kis | 4e9d8bc | 2025-04-10 13:48:26 +0200 | [diff] [blame] | 353 | /// `FFA_FEATURES` specific success argument structure. This type needs further specialization based |
| 354 | /// on 'FF-A function ID or Feature ID' field of the preceeding `FFA_FEATURES` request. |
| 355 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 356 | pub struct SuccessArgsFeatures { |
| 357 | pub properties: [u32; 2], |
| 358 | } |
| 359 | |
| 360 | impl From<SuccessArgsFeatures> for SuccessArgs { |
| 361 | fn from(value: SuccessArgsFeatures) -> Self { |
| 362 | Self::Args32([value.properties[0], value.properties[1], 0, 0, 0, 0]) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | impl TryFrom<SuccessArgs> for SuccessArgsFeatures { |
| 367 | type Error = Error; |
| 368 | |
| 369 | fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> { |
| 370 | let args = value.try_get_args32()?; |
| 371 | |
| 372 | Ok(Self { |
| 373 | properties: [args[0], args[1]], |
| 374 | }) |
| 375 | } |
| 376 | } |
| 377 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 378 | /// RXTX buffer descriptor, used by `FFA_RXTX_MAP`. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 379 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 380 | pub enum RxTxAddr { |
| 381 | Addr32 { rx: u32, tx: u32 }, |
| 382 | Addr64 { rx: u64, tx: u64 }, |
| 383 | } |
| 384 | |
Imre Kis | bbef287 | 2025-04-10 14:11:29 +0200 | [diff] [blame] | 385 | /// `FFA_ID_GET` specific success argument structure. |
| 386 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 387 | pub struct SuccessArgsIdGet { |
| 388 | pub id: u16, |
| 389 | } |
| 390 | |
| 391 | impl From<SuccessArgsIdGet> for SuccessArgs { |
| 392 | fn from(value: SuccessArgsIdGet) -> Self { |
| 393 | SuccessArgs::Args32([value.id as u32, 0, 0, 0, 0, 0]) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl TryFrom<SuccessArgs> for SuccessArgsIdGet { |
| 398 | type Error = Error; |
| 399 | |
| 400 | fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> { |
| 401 | let args = value.try_get_args32()?; |
| 402 | Ok(Self { id: args[0] as u16 }) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /// `FFA_SPM_ID_GET` specific success argument structure. |
| 407 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 408 | pub struct SuccessArgsSpmIdGet { |
| 409 | pub id: u16, |
| 410 | } |
| 411 | |
| 412 | impl From<SuccessArgsSpmIdGet> for SuccessArgs { |
| 413 | fn from(value: SuccessArgsSpmIdGet) -> Self { |
| 414 | SuccessArgs::Args32([value.id as u32, 0, 0, 0, 0, 0]) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | impl TryFrom<SuccessArgs> for SuccessArgsSpmIdGet { |
| 419 | type Error = Error; |
| 420 | |
| 421 | fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> { |
| 422 | let args = value.try_get_args32()?; |
| 423 | Ok(Self { id: args[0] as u16 }) |
| 424 | } |
| 425 | } |
| 426 | |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 427 | /// Flags of the `FFA_PARTITION_INFO_GET` interface. |
| 428 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 429 | pub struct PartitionInfoGetFlags { |
| 430 | pub count_only: bool, |
| 431 | } |
| 432 | |
| 433 | impl PartitionInfoGetFlags { |
| 434 | const RETURN_INFORMATION_TYPE_FLAG: u32 = 1 << 0; |
| 435 | const MBZ_BITS: u32 = 0xffff_fffe; |
| 436 | } |
| 437 | |
| 438 | impl TryFrom<u32> for PartitionInfoGetFlags { |
| 439 | type Error = Error; |
| 440 | |
| 441 | fn try_from(val: u32) -> Result<Self, Self::Error> { |
| 442 | if (val & Self::MBZ_BITS) != 0 { |
| 443 | Err(Error::InvalidPartitionInfoGetFlag(val)) |
| 444 | } else { |
| 445 | Ok(Self { |
| 446 | count_only: val & Self::RETURN_INFORMATION_TYPE_FLAG != 0, |
| 447 | }) |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | impl From<PartitionInfoGetFlags> for u32 { |
| 453 | fn from(flags: PartitionInfoGetFlags) -> Self { |
| 454 | let mut bits: u32 = 0; |
| 455 | if flags.count_only { |
| 456 | bits |= PartitionInfoGetFlags::RETURN_INFORMATION_TYPE_FLAG; |
| 457 | } |
| 458 | bits |
| 459 | } |
| 460 | } |
| 461 | |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 462 | /// Composite type for capturing success and error return codes for the VM availability messages. |
| 463 | /// |
| 464 | /// Error codes are handled by the `FfaError` type. Having a separate type for errors helps using |
| 465 | /// `Result<(), FfaError>`. If a single type would include both success and error values, |
| 466 | /// then `Err(FfaError::Success)` would be incomprehensible. |
| 467 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 468 | pub enum VmAvailabilityStatus { |
| 469 | Success, |
| 470 | Error(FfaError), |
| 471 | } |
| 472 | |
| 473 | impl TryFrom<i32> for VmAvailabilityStatus { |
| 474 | type Error = Error; |
| 475 | fn try_from(value: i32) -> Result<Self, <Self as TryFrom<i32>>::Error> { |
| 476 | Ok(match value { |
| 477 | 0 => Self::Success, |
| 478 | error_code => Self::Error(FfaError::try_from(error_code)?), |
| 479 | }) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | impl From<VmAvailabilityStatus> for i32 { |
| 484 | fn from(value: VmAvailabilityStatus) -> Self { |
| 485 | match value { |
| 486 | VmAvailabilityStatus::Success => 0, |
| 487 | VmAvailabilityStatus::Error(error_code) => error_code.into(), |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /// Arguments for the Power Warm Boot `FFA_MSG_SEND_DIRECT_REQ` interface. |
| 493 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 494 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedWarmBootType))] |
| 495 | #[repr(u32)] |
| 496 | pub enum WarmBootType { |
| 497 | ExitFromSuspend = 0, |
| 498 | ExitFromLowPower = 1, |
| 499 | } |
| 500 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 501 | /// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 502 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 503 | pub enum DirectMsgArgs { |
| 504 | Args32([u32; 5]), |
| 505 | Args64([u64; 5]), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 506 | /// Message for forwarding FFA_VERSION call from Normal world to the SPMC |
| 507 | VersionReq { |
| 508 | version: Version, |
| 509 | }, |
| 510 | /// Response message to forwarded FFA_VERSION call from the Normal world |
| 511 | /// Contains the version returned by the SPMC or None |
| 512 | VersionResp { |
| 513 | version: Option<Version>, |
| 514 | }, |
| 515 | /// Message for a power management operation initiated by a PSCI function |
| 516 | PowerPsciReq32 { |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 517 | // params[i]: Input parameter in w[i] in PSCI function invocation at EL3. |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 518 | // params[0]: Function ID. |
| 519 | params: [u32; 4], |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 520 | }, |
| 521 | /// Message for a power management operation initiated by a PSCI function |
| 522 | PowerPsciReq64 { |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 523 | // params[i]: Input parameter in x[i] in PSCI function invocation at EL3. |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 524 | // params[0]: Function ID. |
| 525 | params: [u64; 4], |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 526 | }, |
| 527 | /// Message for a warm boot |
| 528 | PowerWarmBootReq { |
| 529 | boot_type: WarmBootType, |
| 530 | }, |
| 531 | /// Response message to indicate return status of the last power management request message |
| 532 | /// Return error code SUCCESS or DENIED as defined in PSCI spec. Caller is left to do the |
| 533 | /// parsing of the return status. |
| 534 | PowerPsciResp { |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 535 | psci_status: i32, |
| 536 | }, |
| 537 | /// Message to signal creation of a VM |
| 538 | VmCreated { |
| 539 | // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED |
| 540 | // information associated with the created VM. |
| 541 | // The invalid memory region handle must be specified by the Hypervisor if this field is not |
| 542 | // used. |
| 543 | handle: memory_management::Handle, |
| 544 | vm_id: u16, |
| 545 | }, |
| 546 | /// Message to acknowledge creation of a VM |
| 547 | VmCreatedAck { |
| 548 | sp_status: VmAvailabilityStatus, |
| 549 | }, |
| 550 | /// Message to signal destruction of a VM |
| 551 | VmDestructed { |
| 552 | // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED |
| 553 | // information associated with the created VM. |
| 554 | // The invalid memory region handle must be specified by the Hypervisor if this field is not |
| 555 | // used. |
| 556 | handle: memory_management::Handle, |
| 557 | vm_id: u16, |
| 558 | }, |
| 559 | /// Message to acknowledge destruction of a VM |
| 560 | VmDestructedAck { |
| 561 | sp_status: VmAvailabilityStatus, |
| 562 | }, |
| 563 | } |
| 564 | |
| 565 | impl DirectMsgArgs { |
| 566 | // Flags for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces. |
| 567 | |
| 568 | const FWK_MSG_BITS: u32 = 1 << 31; |
| 569 | const VERSION_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1000; |
| 570 | const VERSION_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1001; |
| 571 | const POWER_PSCI_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS; |
| 572 | const POWER_WARM_BOOT_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0001; |
| 573 | const POWER_PSCI_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0010; |
| 574 | const VM_CREATED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0100; |
| 575 | const VM_CREATED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0101; |
| 576 | const VM_DESTRUCTED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0110; |
| 577 | const VM_DESTRUCTED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0111; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 580 | /// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}2` interfaces. |
| 581 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 582 | pub struct DirectMsg2Args([u64; 14]); |
| 583 | |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 584 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 585 | pub struct MsgWaitFlags { |
| 586 | retain_rx_buffer: bool, |
| 587 | } |
| 588 | |
| 589 | impl MsgWaitFlags { |
| 590 | const RETAIN_RX_BUFFER: u32 = 0x01; |
| 591 | const MBZ_BITS: u32 = 0xfffe; |
| 592 | } |
| 593 | |
| 594 | impl TryFrom<u32> for MsgWaitFlags { |
| 595 | type Error = Error; |
| 596 | |
| 597 | fn try_from(val: u32) -> Result<Self, Self::Error> { |
| 598 | if (val & Self::MBZ_BITS) != 0 { |
| 599 | Err(Error::InvalidMsgWaitFlag(val)) |
| 600 | } else { |
| 601 | Ok(MsgWaitFlags { |
| 602 | retain_rx_buffer: val & Self::RETAIN_RX_BUFFER != 0, |
| 603 | }) |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | impl From<MsgWaitFlags> for u32 { |
| 609 | fn from(flags: MsgWaitFlags) -> Self { |
| 610 | let mut bits: u32 = 0; |
| 611 | if flags.retain_rx_buffer { |
| 612 | bits |= MsgWaitFlags::RETAIN_RX_BUFFER; |
| 613 | } |
| 614 | bits |
| 615 | } |
| 616 | } |
| 617 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 618 | /// Descriptor for a dynamically allocated memory buffer that contains the memory transaction |
Tomás González | f268e32 | 2025-03-05 11:18:11 +0000 | [diff] [blame] | 619 | /// descriptor. |
| 620 | /// |
| 621 | /// Used by `FFA_MEM_{DONATE,LEND,SHARE,RETRIEVE_REQ}` interfaces, only when the TX buffer is not |
| 622 | /// used to transmit the transaction descriptor. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 623 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 624 | pub enum MemOpBuf { |
| 625 | Buf32 { addr: u32, page_cnt: u32 }, |
| 626 | Buf64 { addr: u64, page_cnt: u32 }, |
| 627 | } |
| 628 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 629 | /// Memory address argument for `FFA_MEM_PERM_{GET,SET}` interfaces. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 630 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 631 | pub enum MemAddr { |
| 632 | Addr32(u32), |
| 633 | Addr64(u64), |
| 634 | } |
| 635 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 636 | /// Argument for the `FFA_CONSOLE_LOG` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 637 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 638 | pub enum ConsoleLogChars { |
| 639 | Reg32([u32; 6]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 640 | Reg64([u64; 16]), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 643 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 644 | pub struct NotificationBindFlags { |
| 645 | per_vcpu_notification: bool, |
| 646 | } |
| 647 | |
| 648 | impl NotificationBindFlags { |
| 649 | const PER_VCPU_NOTIFICATION: u32 = 1; |
| 650 | } |
| 651 | |
| 652 | impl From<NotificationBindFlags> for u32 { |
| 653 | fn from(flags: NotificationBindFlags) -> Self { |
| 654 | let mut bits: u32 = 0; |
| 655 | if flags.per_vcpu_notification { |
| 656 | bits |= NotificationBindFlags::PER_VCPU_NOTIFICATION; |
| 657 | } |
| 658 | bits |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | impl From<u32> for NotificationBindFlags { |
| 663 | fn from(flags: u32) -> Self { |
| 664 | Self { |
| 665 | per_vcpu_notification: flags & Self::PER_VCPU_NOTIFICATION != 0, |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 671 | pub struct NotificationSetFlags { |
| 672 | delay_schedule_receiver: bool, |
| 673 | vcpu_id: Option<u16>, |
| 674 | } |
| 675 | |
| 676 | impl NotificationSetFlags { |
| 677 | const PER_VCP_NOTIFICATION: u32 = 1 << 0; |
| 678 | const DELAY_SCHEDULE_RECEIVER: u32 = 1 << 1; |
| 679 | const VCPU_ID_SHIFT: u32 = 16; |
| 680 | |
| 681 | const MBZ_BITS: u32 = 0xfffc; |
| 682 | } |
| 683 | |
| 684 | impl From<NotificationSetFlags> for u32 { |
| 685 | fn from(flags: NotificationSetFlags) -> Self { |
| 686 | let mut bits: u32 = 0; |
| 687 | |
| 688 | if flags.delay_schedule_receiver { |
| 689 | bits |= NotificationSetFlags::DELAY_SCHEDULE_RECEIVER; |
| 690 | } |
| 691 | if let Some(vcpu_id) = flags.vcpu_id { |
| 692 | bits |= NotificationSetFlags::PER_VCP_NOTIFICATION; |
| 693 | bits |= u32::from(vcpu_id) << NotificationSetFlags::VCPU_ID_SHIFT; |
| 694 | } |
| 695 | |
| 696 | bits |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | impl TryFrom<u32> for NotificationSetFlags { |
| 701 | type Error = Error; |
| 702 | |
| 703 | fn try_from(flags: u32) -> Result<Self, Self::Error> { |
| 704 | if (flags & Self::MBZ_BITS) != 0 { |
| 705 | return Err(Error::InvalidNotificationSetFlag(flags)); |
| 706 | } |
| 707 | |
| 708 | let tentative_vcpu_id = (flags >> Self::VCPU_ID_SHIFT) as u16; |
| 709 | |
| 710 | let vcpu_id = if (flags & Self::PER_VCP_NOTIFICATION) != 0 { |
| 711 | Some(tentative_vcpu_id) |
| 712 | } else { |
| 713 | if tentative_vcpu_id != 0 { |
| 714 | return Err(Error::InvalidNotificationSetFlag(flags)); |
| 715 | } |
| 716 | None |
| 717 | }; |
| 718 | |
| 719 | Ok(Self { |
| 720 | delay_schedule_receiver: (flags & Self::DELAY_SCHEDULE_RECEIVER) != 0, |
| 721 | vcpu_id, |
| 722 | }) |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 727 | pub struct NotificationGetFlags { |
| 728 | sp_bitmap_id: bool, |
| 729 | vm_bitmap_id: bool, |
| 730 | spm_bitmap_id: bool, |
| 731 | hyp_bitmap_id: bool, |
| 732 | } |
| 733 | |
| 734 | impl NotificationGetFlags { |
| 735 | const SP_BITMAP_ID: u32 = 1; |
| 736 | const VM_BITMAP_ID: u32 = 1 << 1; |
| 737 | const SPM_BITMAP_ID: u32 = 1 << 2; |
| 738 | const HYP_BITMAP_ID: u32 = 1 << 3; |
| 739 | } |
| 740 | |
| 741 | impl From<NotificationGetFlags> for u32 { |
| 742 | fn from(flags: NotificationGetFlags) -> Self { |
| 743 | let mut bits: u32 = 0; |
| 744 | if flags.sp_bitmap_id { |
| 745 | bits |= NotificationGetFlags::SP_BITMAP_ID; |
| 746 | } |
| 747 | if flags.vm_bitmap_id { |
| 748 | bits |= NotificationGetFlags::VM_BITMAP_ID; |
| 749 | } |
| 750 | if flags.spm_bitmap_id { |
| 751 | bits |= NotificationGetFlags::SPM_BITMAP_ID; |
| 752 | } |
| 753 | if flags.hyp_bitmap_id { |
| 754 | bits |= NotificationGetFlags::HYP_BITMAP_ID; |
| 755 | } |
| 756 | bits |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | impl From<u32> for NotificationGetFlags { |
| 761 | // This is a "from" instead of a "try_from" because Reserved Bits are SBZ, *not* MBZ. |
| 762 | fn from(flags: u32) -> Self { |
| 763 | Self { |
| 764 | sp_bitmap_id: (flags & Self::SP_BITMAP_ID) != 0, |
| 765 | vm_bitmap_id: (flags & Self::VM_BITMAP_ID) != 0, |
| 766 | spm_bitmap_id: (flags & Self::SPM_BITMAP_ID) != 0, |
| 767 | hyp_bitmap_id: (flags & Self::HYP_BITMAP_ID) != 0, |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
Imre Kis | 9959e06 | 2025-04-10 14:16:10 +0200 | [diff] [blame] | 772 | /// `FFA_NOTIFICATION_GET` specific success argument structure. |
| 773 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 774 | pub struct SuccessArgsNotificationGet { |
| 775 | pub sp_notifications: Option<u64>, |
| 776 | pub vm_notifications: Option<u64>, |
| 777 | pub spm_notifications: Option<u32>, |
| 778 | pub hypervisor_notifications: Option<u32>, |
| 779 | } |
| 780 | |
| 781 | impl From<SuccessArgsNotificationGet> for SuccessArgs { |
| 782 | fn from(value: SuccessArgsNotificationGet) -> Self { |
| 783 | let mut args = [0; 6]; |
| 784 | |
| 785 | if let Some(bitmap) = value.sp_notifications { |
| 786 | args[0] = bitmap as u32; |
| 787 | args[1] = (bitmap >> 32) as u32; |
| 788 | } |
| 789 | |
| 790 | if let Some(bitmap) = value.vm_notifications { |
| 791 | args[2] = bitmap as u32; |
| 792 | args[3] = (bitmap >> 32) as u32; |
| 793 | } |
| 794 | |
| 795 | if let Some(bitmap) = value.spm_notifications { |
| 796 | args[4] = bitmap; |
| 797 | } |
| 798 | |
| 799 | if let Some(bitmap) = value.hypervisor_notifications { |
| 800 | args[5] = bitmap; |
| 801 | } |
| 802 | |
| 803 | Self::Args32(args) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | impl TryFrom<(NotificationGetFlags, SuccessArgs)> for SuccessArgsNotificationGet { |
| 808 | type Error = Error; |
| 809 | |
| 810 | fn try_from(value: (NotificationGetFlags, SuccessArgs)) -> Result<Self, Self::Error> { |
| 811 | let (flags, value) = value; |
| 812 | let args = value.try_get_args32()?; |
| 813 | |
| 814 | let sp_notifications = if flags.sp_bitmap_id { |
| 815 | Some(u64::from(args[0]) | (u64::from(args[1]) << 32)) |
| 816 | } else { |
| 817 | None |
| 818 | }; |
| 819 | |
| 820 | let vm_notifications = if flags.vm_bitmap_id { |
| 821 | Some(u64::from(args[2]) | (u64::from(args[3]) << 32)) |
| 822 | } else { |
| 823 | None |
| 824 | }; |
| 825 | |
| 826 | let spm_notifications = if flags.spm_bitmap_id { |
| 827 | Some(args[4]) |
| 828 | } else { |
| 829 | None |
| 830 | }; |
| 831 | |
| 832 | let hypervisor_notifications = if flags.hyp_bitmap_id { |
| 833 | Some(args[5]) |
| 834 | } else { |
| 835 | None |
| 836 | }; |
| 837 | |
| 838 | Ok(Self { |
| 839 | sp_notifications, |
| 840 | vm_notifications, |
| 841 | spm_notifications, |
| 842 | hypervisor_notifications, |
| 843 | }) |
| 844 | } |
| 845 | } |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 846 | |
| 847 | /// `FFA_NOTIFICATION_INFO_GET` specific success argument structure. The `MAX_COUNT` parameter |
| 848 | /// depends on the 32-bit or 64-bit packing. |
| 849 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 850 | pub struct SuccessArgsNotificationInfoGet<const MAX_COUNT: usize> { |
| 851 | pub more_pending_notifications: bool, |
| 852 | list_count: usize, |
| 853 | id_counts: [u8; MAX_COUNT], |
| 854 | ids: [u16; MAX_COUNT], |
| 855 | } |
| 856 | |
| 857 | impl<const MAX_COUNT: usize> Default for SuccessArgsNotificationInfoGet<MAX_COUNT> { |
| 858 | fn default() -> Self { |
| 859 | Self { |
| 860 | more_pending_notifications: false, |
| 861 | list_count: 0, |
| 862 | id_counts: [0; MAX_COUNT], |
| 863 | ids: [0; MAX_COUNT], |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | impl<const MAX_COUNT: usize> SuccessArgsNotificationInfoGet<MAX_COUNT> { |
| 869 | const MORE_PENDING_NOTIFICATIONS_FLAG: u64 = 1 << 0; |
| 870 | const LIST_COUNT_SHIFT: usize = 7; |
| 871 | const LIST_COUNT_MASK: u64 = 0x1f; |
| 872 | const ID_COUNT_SHIFT: usize = 12; |
| 873 | const ID_COUNT_MASK: u64 = 0x03; |
| 874 | const ID_COUNT_BITS: usize = 2; |
| 875 | |
| 876 | pub fn add_list(&mut self, endpoint: u16, vcpu_ids: &[u16]) -> Result<(), Error> { |
| 877 | if self.list_count >= MAX_COUNT || vcpu_ids.len() > Self::ID_COUNT_MASK as usize { |
| 878 | return Err(Error::InvalidNotificationCount); |
| 879 | } |
| 880 | |
| 881 | // Each list contains at least one ID: the partition ID, followed by vCPU IDs. The number |
| 882 | // of vCPU IDs is recorded in `id_counts`. |
| 883 | let mut current_id_index = self.list_count + self.id_counts.iter().sum::<u8>() as usize; |
| 884 | if current_id_index + 1 + vcpu_ids.len() > MAX_COUNT { |
| 885 | // The new list does not fit into the available space for IDs. |
| 886 | return Err(Error::InvalidNotificationCount); |
| 887 | } |
| 888 | |
| 889 | self.id_counts[self.list_count] = vcpu_ids.len() as u8; |
| 890 | self.list_count += 1; |
| 891 | |
| 892 | // The first ID is the endpoint ID. |
| 893 | self.ids[current_id_index] = endpoint; |
| 894 | current_id_index += 1; |
| 895 | |
| 896 | // Insert the vCPU IDs. |
| 897 | self.ids[current_id_index..current_id_index + vcpu_ids.len()].copy_from_slice(vcpu_ids); |
| 898 | |
| 899 | Ok(()) |
| 900 | } |
| 901 | |
| 902 | pub fn iter(&self) -> NotificationInfoGetIterator<'_> { |
| 903 | NotificationInfoGetIterator { |
| 904 | list_index: 0, |
| 905 | id_index: 0, |
| 906 | id_count: &self.id_counts[0..self.list_count], |
| 907 | ids: &self.ids, |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /// Pack flags field and IDs. |
| 912 | fn pack(self) -> (u64, [u16; MAX_COUNT]) { |
| 913 | let mut flags = if self.more_pending_notifications { |
| 914 | Self::MORE_PENDING_NOTIFICATIONS_FLAG |
| 915 | } else { |
| 916 | 0 |
| 917 | }; |
| 918 | |
| 919 | flags |= (self.list_count as u64) << Self::LIST_COUNT_SHIFT; |
| 920 | for (count, shift) in self.id_counts.iter().take(self.list_count).zip( |
| 921 | (Self::ID_COUNT_SHIFT..Self::ID_COUNT_SHIFT + Self::ID_COUNT_BITS * MAX_COUNT) |
| 922 | .step_by(Self::ID_COUNT_BITS), |
| 923 | ) { |
| 924 | flags |= u64::from(*count) << shift; |
| 925 | } |
| 926 | |
| 927 | (flags, self.ids) |
| 928 | } |
| 929 | |
| 930 | /// Unpack flags field and IDs. |
| 931 | fn unpack(flags: u64, ids: [u16; MAX_COUNT]) -> Result<Self, Error> { |
| 932 | let count_of_lists = ((flags >> Self::LIST_COUNT_SHIFT) & Self::LIST_COUNT_MASK) as usize; |
| 933 | |
| 934 | if count_of_lists > MAX_COUNT { |
| 935 | return Err(Error::InvalidNotificationCount); |
| 936 | } |
| 937 | |
| 938 | let mut count_of_ids = [0; MAX_COUNT]; |
| 939 | let mut count_of_ids_bits = flags >> Self::ID_COUNT_SHIFT; |
| 940 | |
| 941 | for id in count_of_ids.iter_mut().take(count_of_lists) { |
| 942 | *id = (count_of_ids_bits & Self::ID_COUNT_MASK) as u8; |
| 943 | count_of_ids_bits >>= Self::ID_COUNT_BITS; |
| 944 | } |
| 945 | |
| 946 | Ok(Self { |
| 947 | more_pending_notifications: (flags & Self::MORE_PENDING_NOTIFICATIONS_FLAG) != 0, |
| 948 | list_count: count_of_lists, |
| 949 | id_counts: count_of_ids, |
| 950 | ids, |
| 951 | }) |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /// `FFA_NOTIFICATION_INFO_GET_32` specific success argument structure. |
| 956 | pub type SuccessArgsNotificationInfoGet32 = SuccessArgsNotificationInfoGet<10>; |
| 957 | |
| 958 | impl From<SuccessArgsNotificationInfoGet32> for SuccessArgs { |
| 959 | fn from(value: SuccessArgsNotificationInfoGet32) -> Self { |
| 960 | let (flags, ids) = value.pack(); |
| 961 | let id_regs: [u32; 5] = transmute!(ids); |
| 962 | |
| 963 | let mut args = [0; 6]; |
| 964 | args[0] = flags as u32; |
| 965 | args[1..6].copy_from_slice(&id_regs); |
| 966 | |
| 967 | SuccessArgs::Args32(args) |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | impl TryFrom<SuccessArgs> for SuccessArgsNotificationInfoGet32 { |
| 972 | type Error = Error; |
| 973 | |
| 974 | fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> { |
| 975 | let args = value.try_get_args32()?; |
| 976 | let flags = args[0].into(); |
| 977 | let id_regs: [u32; 5] = args[1..6].try_into().unwrap(); |
| 978 | Self::unpack(flags, transmute!(id_regs)) |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /// `FFA_NOTIFICATION_INFO_GET_64` specific success argument structure. |
| 983 | pub type SuccessArgsNotificationInfoGet64 = SuccessArgsNotificationInfoGet<20>; |
| 984 | |
| 985 | impl From<SuccessArgsNotificationInfoGet64> for SuccessArgs { |
| 986 | fn from(value: SuccessArgsNotificationInfoGet64) -> Self { |
| 987 | let (flags, ids) = value.pack(); |
| 988 | let id_regs: [u64; 5] = transmute!(ids); |
| 989 | |
| 990 | let mut args = [0; 6]; |
| 991 | args[0] = flags; |
| 992 | args[1..6].copy_from_slice(&id_regs); |
| 993 | |
| 994 | SuccessArgs::Args64(args) |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | impl TryFrom<SuccessArgs> for SuccessArgsNotificationInfoGet64 { |
| 999 | type Error = Error; |
| 1000 | |
| 1001 | fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> { |
| 1002 | let args = value.try_get_args64()?; |
| 1003 | let flags = args[0]; |
| 1004 | let id_regs: [u64; 5] = args[1..6].try_into().unwrap(); |
| 1005 | Self::unpack(flags, transmute!(id_regs)) |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | pub struct NotificationInfoGetIterator<'a> { |
| 1010 | list_index: usize, |
| 1011 | id_index: usize, |
| 1012 | id_count: &'a [u8], |
| 1013 | ids: &'a [u16], |
| 1014 | } |
| 1015 | |
| 1016 | impl<'a> Iterator for NotificationInfoGetIterator<'a> { |
| 1017 | type Item = (u16, &'a [u16]); |
| 1018 | |
| 1019 | fn next(&mut self) -> Option<Self::Item> { |
| 1020 | if self.list_index < self.id_count.len() { |
| 1021 | let partition_id = self.ids[self.id_index]; |
| 1022 | let id_range = |
| 1023 | (self.id_index + 1)..=(self.id_index + self.id_count[self.list_index] as usize); |
| 1024 | |
| 1025 | self.id_index += 1 + self.id_count[self.list_index] as usize; |
| 1026 | self.list_index += 1; |
| 1027 | |
| 1028 | Some((partition_id, &self.ids[id_range])) |
| 1029 | } else { |
| 1030 | None |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Tomás González | f268e32 | 2025-03-05 11:18:11 +0000 | [diff] [blame] | 1035 | /// FF-A "message types", the terminology used by the spec is "interfaces". |
| 1036 | /// |
| 1037 | /// The interfaces are used by FF-A components for communication at an FF-A instance. The spec also |
| 1038 | /// describes the valid FF-A instances and conduits for each interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1039 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 1040 | pub enum Interface { |
| 1041 | Error { |
| 1042 | target_info: TargetInfo, |
| 1043 | error_code: FfaError, |
Balint Dobszay | b727aab | 2025-04-07 10:24:59 +0200 | [diff] [blame] | 1044 | error_arg: u32, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1045 | }, |
| 1046 | Success { |
| 1047 | target_info: u32, |
| 1048 | args: SuccessArgs, |
| 1049 | }, |
| 1050 | Interrupt { |
| 1051 | target_info: TargetInfo, |
| 1052 | interrupt_id: u32, |
| 1053 | }, |
| 1054 | Version { |
| 1055 | input_version: Version, |
| 1056 | }, |
| 1057 | VersionOut { |
| 1058 | output_version: Version, |
| 1059 | }, |
| 1060 | Features { |
| 1061 | feat_id: Feature, |
| 1062 | input_properties: u32, |
| 1063 | }, |
| 1064 | RxAcquire { |
| 1065 | vm_id: u16, |
| 1066 | }, |
| 1067 | RxRelease { |
| 1068 | vm_id: u16, |
| 1069 | }, |
| 1070 | RxTxMap { |
| 1071 | addr: RxTxAddr, |
| 1072 | page_cnt: u32, |
| 1073 | }, |
| 1074 | RxTxUnmap { |
| 1075 | id: u16, |
| 1076 | }, |
| 1077 | PartitionInfoGet { |
| 1078 | uuid: Uuid, |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 1079 | flags: PartitionInfoGetFlags, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1080 | }, |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 1081 | PartitionInfoGetRegs { |
| 1082 | uuid: Uuid, |
| 1083 | start_index: u16, |
| 1084 | info_tag: u16, |
| 1085 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1086 | IdGet, |
| 1087 | SpmIdGet, |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 1088 | MsgWait { |
| 1089 | flags: Option<MsgWaitFlags>, |
| 1090 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1091 | Yield, |
| 1092 | Run { |
| 1093 | target_info: TargetInfo, |
| 1094 | }, |
| 1095 | NormalWorldResume, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 1096 | SecondaryEpRegister { |
| 1097 | entrypoint: SecondaryEpRegisterAddr, |
| 1098 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1099 | MsgSend2 { |
| 1100 | sender_vm_id: u16, |
| 1101 | flags: u32, |
| 1102 | }, |
| 1103 | MsgSendDirectReq { |
| 1104 | src_id: u16, |
| 1105 | dst_id: u16, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1106 | args: DirectMsgArgs, |
| 1107 | }, |
| 1108 | MsgSendDirectResp { |
| 1109 | src_id: u16, |
| 1110 | dst_id: u16, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1111 | args: DirectMsgArgs, |
| 1112 | }, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1113 | MsgSendDirectReq2 { |
| 1114 | src_id: u16, |
| 1115 | dst_id: u16, |
| 1116 | uuid: Uuid, |
| 1117 | args: DirectMsg2Args, |
| 1118 | }, |
| 1119 | MsgSendDirectResp2 { |
| 1120 | src_id: u16, |
| 1121 | dst_id: u16, |
| 1122 | args: DirectMsg2Args, |
| 1123 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1124 | MemDonate { |
| 1125 | total_len: u32, |
| 1126 | frag_len: u32, |
| 1127 | buf: Option<MemOpBuf>, |
| 1128 | }, |
| 1129 | MemLend { |
| 1130 | total_len: u32, |
| 1131 | frag_len: u32, |
| 1132 | buf: Option<MemOpBuf>, |
| 1133 | }, |
| 1134 | MemShare { |
| 1135 | total_len: u32, |
| 1136 | frag_len: u32, |
| 1137 | buf: Option<MemOpBuf>, |
| 1138 | }, |
| 1139 | MemRetrieveReq { |
| 1140 | total_len: u32, |
| 1141 | frag_len: u32, |
| 1142 | buf: Option<MemOpBuf>, |
| 1143 | }, |
| 1144 | MemRetrieveResp { |
| 1145 | total_len: u32, |
| 1146 | frag_len: u32, |
| 1147 | }, |
| 1148 | MemRelinquish, |
| 1149 | MemReclaim { |
| 1150 | handle: memory_management::Handle, |
| 1151 | flags: u32, |
| 1152 | }, |
| 1153 | MemPermGet { |
| 1154 | addr: MemAddr, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1155 | page_cnt: Option<u32>, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1156 | }, |
| 1157 | MemPermSet { |
| 1158 | addr: MemAddr, |
| 1159 | page_cnt: u32, |
| 1160 | mem_perm: u32, |
| 1161 | }, |
| 1162 | ConsoleLog { |
| 1163 | char_cnt: u8, |
| 1164 | char_lists: ConsoleLogChars, |
| 1165 | }, |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 1166 | NotificationBitmapCreate { |
| 1167 | vm_id: u16, |
| 1168 | vcpu_cnt: u32, |
| 1169 | }, |
| 1170 | NotificationBitmapDestroy { |
| 1171 | vm_id: u16, |
| 1172 | }, |
| 1173 | NotificationBind { |
| 1174 | sender_id: u16, |
| 1175 | receiver_id: u16, |
| 1176 | flags: NotificationBindFlags, |
| 1177 | bitmap: u64, |
| 1178 | }, |
| 1179 | NotificationUnBind { |
| 1180 | sender_id: u16, |
| 1181 | receiver_id: u16, |
| 1182 | bitmap: u64, |
| 1183 | }, |
| 1184 | NotificationSet { |
| 1185 | sender_id: u16, |
| 1186 | receiver_id: u16, |
| 1187 | flags: NotificationSetFlags, |
| 1188 | bitmap: u64, |
| 1189 | }, |
| 1190 | NotificationGet { |
| 1191 | vcpu_id: u16, |
| 1192 | endpoint_id: u16, |
| 1193 | flags: NotificationGetFlags, |
| 1194 | }, |
| 1195 | NotificationInfoGet { |
| 1196 | is_32bit: bool, |
| 1197 | }, |
Tomás González | e6fe75f | 2025-04-04 09:46:50 +0100 | [diff] [blame] | 1198 | El3IntrHandle, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1201 | impl Interface { |
| 1202 | /// Returns the function ID for the call, if it has one. |
| 1203 | pub fn function_id(&self) -> Option<FuncId> { |
| 1204 | match self { |
| 1205 | Interface::Error { .. } => Some(FuncId::Error), |
| 1206 | Interface::Success { args, .. } => match args { |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1207 | SuccessArgs::Args32(..) => Some(FuncId::Success32), |
| 1208 | SuccessArgs::Args64(..) | SuccessArgs::Args64_2(..) => Some(FuncId::Success64), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1209 | }, |
| 1210 | Interface::Interrupt { .. } => Some(FuncId::Interrupt), |
| 1211 | Interface::Version { .. } => Some(FuncId::Version), |
| 1212 | Interface::VersionOut { .. } => None, |
| 1213 | Interface::Features { .. } => Some(FuncId::Features), |
| 1214 | Interface::RxAcquire { .. } => Some(FuncId::RxAcquire), |
| 1215 | Interface::RxRelease { .. } => Some(FuncId::RxRelease), |
| 1216 | Interface::RxTxMap { addr, .. } => match addr { |
| 1217 | RxTxAddr::Addr32 { .. } => Some(FuncId::RxTxMap32), |
| 1218 | RxTxAddr::Addr64 { .. } => Some(FuncId::RxTxMap64), |
| 1219 | }, |
| 1220 | Interface::RxTxUnmap { .. } => Some(FuncId::RxTxUnmap), |
| 1221 | Interface::PartitionInfoGet { .. } => Some(FuncId::PartitionInfoGet), |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 1222 | Interface::PartitionInfoGetRegs { .. } => Some(FuncId::PartitionInfoGetRegs), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1223 | Interface::IdGet => Some(FuncId::IdGet), |
| 1224 | Interface::SpmIdGet => Some(FuncId::SpmIdGet), |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 1225 | Interface::MsgWait { .. } => Some(FuncId::MsgWait), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1226 | Interface::Yield => Some(FuncId::Yield), |
| 1227 | Interface::Run { .. } => Some(FuncId::Run), |
| 1228 | Interface::NormalWorldResume => Some(FuncId::NormalWorldResume), |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 1229 | Interface::SecondaryEpRegister { entrypoint } => match entrypoint { |
| 1230 | SecondaryEpRegisterAddr::Addr32 { .. } => Some(FuncId::SecondaryEpRegister32), |
| 1231 | SecondaryEpRegisterAddr::Addr64 { .. } => Some(FuncId::SecondaryEpRegister64), |
| 1232 | }, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1233 | Interface::MsgSend2 { .. } => Some(FuncId::MsgSend2), |
| 1234 | Interface::MsgSendDirectReq { args, .. } => match args { |
| 1235 | DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectReq32), |
| 1236 | DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectReq64), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1237 | DirectMsgArgs::VersionReq { .. } => Some(FuncId::MsgSendDirectReq32), |
| 1238 | DirectMsgArgs::PowerPsciReq32 { .. } => Some(FuncId::MsgSendDirectReq32), |
| 1239 | DirectMsgArgs::PowerPsciReq64 { .. } => Some(FuncId::MsgSendDirectReq64), |
| 1240 | DirectMsgArgs::PowerWarmBootReq { .. } => Some(FuncId::MsgSendDirectReq32), |
| 1241 | DirectMsgArgs::VmCreated { .. } => Some(FuncId::MsgSendDirectReq32), |
| 1242 | DirectMsgArgs::VmDestructed { .. } => Some(FuncId::MsgSendDirectReq32), |
| 1243 | _ => None, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1244 | }, |
| 1245 | Interface::MsgSendDirectResp { args, .. } => match args { |
| 1246 | DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectResp32), |
| 1247 | DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectResp64), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1248 | DirectMsgArgs::VersionResp { .. } => Some(FuncId::MsgSendDirectResp32), |
| 1249 | DirectMsgArgs::PowerPsciResp { .. } => Some(FuncId::MsgSendDirectResp32), |
| 1250 | DirectMsgArgs::VmCreatedAck { .. } => Some(FuncId::MsgSendDirectResp32), |
| 1251 | DirectMsgArgs::VmDestructedAck { .. } => Some(FuncId::MsgSendDirectResp32), |
| 1252 | _ => None, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1253 | }, |
| 1254 | Interface::MsgSendDirectReq2 { .. } => Some(FuncId::MsgSendDirectReq64_2), |
| 1255 | Interface::MsgSendDirectResp2 { .. } => Some(FuncId::MsgSendDirectResp64_2), |
| 1256 | Interface::MemDonate { buf, .. } => match buf { |
| 1257 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemDonate64), |
| 1258 | _ => Some(FuncId::MemDonate32), |
| 1259 | }, |
| 1260 | Interface::MemLend { buf, .. } => match buf { |
| 1261 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemLend64), |
| 1262 | _ => Some(FuncId::MemLend32), |
| 1263 | }, |
| 1264 | Interface::MemShare { buf, .. } => match buf { |
| 1265 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemShare64), |
| 1266 | _ => Some(FuncId::MemShare32), |
| 1267 | }, |
| 1268 | Interface::MemRetrieveReq { buf, .. } => match buf { |
| 1269 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemRetrieveReq64), |
| 1270 | _ => Some(FuncId::MemRetrieveReq32), |
| 1271 | }, |
| 1272 | Interface::MemRetrieveResp { .. } => Some(FuncId::MemRetrieveResp), |
| 1273 | Interface::MemRelinquish => Some(FuncId::MemRelinquish), |
| 1274 | Interface::MemReclaim { .. } => Some(FuncId::MemReclaim), |
| 1275 | Interface::MemPermGet { addr, .. } => match addr { |
| 1276 | MemAddr::Addr32(_) => Some(FuncId::MemPermGet32), |
| 1277 | MemAddr::Addr64(_) => Some(FuncId::MemPermGet64), |
| 1278 | }, |
| 1279 | Interface::MemPermSet { addr, .. } => match addr { |
| 1280 | MemAddr::Addr32(_) => Some(FuncId::MemPermSet32), |
| 1281 | MemAddr::Addr64(_) => Some(FuncId::MemPermSet64), |
| 1282 | }, |
| 1283 | Interface::ConsoleLog { char_lists, .. } => match char_lists { |
| 1284 | ConsoleLogChars::Reg32(_) => Some(FuncId::ConsoleLog32), |
| 1285 | ConsoleLogChars::Reg64(_) => Some(FuncId::ConsoleLog64), |
| 1286 | }, |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 1287 | Interface::NotificationBitmapCreate { .. } => Some(FuncId::NotificationBitmapCreate), |
| 1288 | Interface::NotificationBitmapDestroy { .. } => Some(FuncId::NotificationBitmapDestroy), |
| 1289 | Interface::NotificationBind { .. } => Some(FuncId::NotificationBind), |
| 1290 | Interface::NotificationUnBind { .. } => Some(FuncId::NotificationUnbind), |
| 1291 | Interface::NotificationSet { .. } => Some(FuncId::NotificationSet), |
| 1292 | Interface::NotificationGet { .. } => Some(FuncId::NotificationGet), |
| 1293 | Interface::NotificationInfoGet { is_32bit } => match is_32bit { |
| 1294 | true => Some(FuncId::NotificationInfoGet32), |
| 1295 | false => Some(FuncId::NotificationInfoGet64), |
| 1296 | }, |
Tomás González | e6fe75f | 2025-04-04 09:46:50 +0100 | [diff] [blame] | 1297 | Interface::El3IntrHandle => Some(FuncId::El3IntrHandle), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1298 | } |
| 1299 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1300 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1301 | /// Returns true if this is a 32-bit call, or false if it is a 64-bit call. |
| 1302 | pub fn is_32bit(&self) -> bool { |
| 1303 | // TODO: self should always have a function ID? |
| 1304 | self.function_id().unwrap().is_32bit() |
| 1305 | } |
| 1306 | |
| 1307 | /// Parse interface from register contents. The caller must ensure that the `regs` argument has |
| 1308 | /// the correct length: 8 registers for FF-A v1.1 and lower, 18 registers for v1.2 and higher. |
| 1309 | pub fn from_regs(version: Version, regs: &[u64]) -> Result<Self, Error> { |
| 1310 | let reg_cnt = regs.len(); |
| 1311 | |
| 1312 | let msg = match reg_cnt { |
| 1313 | 8 => { |
| 1314 | assert!(version <= Version(1, 1)); |
| 1315 | Interface::unpack_regs8(version, regs.try_into().unwrap())? |
| 1316 | } |
| 1317 | 18 => { |
| 1318 | assert!(version >= Version(1, 2)); |
| 1319 | match FuncId::try_from(regs[0] as u32)? { |
| 1320 | FuncId::ConsoleLog64 |
| 1321 | | FuncId::Success64 |
| 1322 | | FuncId::MsgSendDirectReq64_2 |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 1323 | | FuncId::MsgSendDirectResp64_2 |
| 1324 | | FuncId::PartitionInfoGetRegs => { |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1325 | Interface::unpack_regs18(version, regs.try_into().unwrap())? |
| 1326 | } |
| 1327 | _ => Interface::unpack_regs8(version, regs[..8].try_into().unwrap())?, |
| 1328 | } |
| 1329 | } |
| 1330 | _ => panic!( |
| 1331 | "Invalid number of registers ({}) for FF-A version {}", |
| 1332 | reg_cnt, version |
| 1333 | ), |
| 1334 | }; |
| 1335 | |
| 1336 | Ok(msg) |
| 1337 | } |
| 1338 | |
| 1339 | fn unpack_regs8(version: Version, regs: &[u64; 8]) -> Result<Self, Error> { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1340 | let fid = FuncId::try_from(regs[0] as u32)?; |
| 1341 | |
| 1342 | let msg = match fid { |
| 1343 | FuncId::Error => Self::Error { |
| 1344 | target_info: (regs[1] as u32).into(), |
| 1345 | error_code: FfaError::try_from(regs[2] as i32)?, |
Balint Dobszay | b727aab | 2025-04-07 10:24:59 +0200 | [diff] [blame] | 1346 | error_arg: regs[3] as u32, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1347 | }, |
| 1348 | FuncId::Success32 => Self::Success { |
| 1349 | target_info: regs[1] as u32, |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1350 | args: SuccessArgs::Args32([ |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1351 | regs[2] as u32, |
| 1352 | regs[3] as u32, |
| 1353 | regs[4] as u32, |
| 1354 | regs[5] as u32, |
| 1355 | regs[6] as u32, |
| 1356 | regs[7] as u32, |
| 1357 | ]), |
| 1358 | }, |
| 1359 | FuncId::Success64 => Self::Success { |
| 1360 | target_info: regs[1] as u32, |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1361 | args: SuccessArgs::Args64([regs[2], regs[3], regs[4], regs[5], regs[6], regs[7]]), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1362 | }, |
| 1363 | FuncId::Interrupt => Self::Interrupt { |
| 1364 | target_info: (regs[1] as u32).into(), |
| 1365 | interrupt_id: regs[2] as u32, |
| 1366 | }, |
| 1367 | FuncId::Version => Self::Version { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame] | 1368 | input_version: (regs[1] as u32).try_into()?, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1369 | }, |
| 1370 | FuncId::Features => Self::Features { |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 1371 | feat_id: (regs[1] as u32).into(), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1372 | input_properties: regs[2] as u32, |
| 1373 | }, |
| 1374 | FuncId::RxAcquire => Self::RxAcquire { |
| 1375 | vm_id: regs[1] as u16, |
| 1376 | }, |
| 1377 | FuncId::RxRelease => Self::RxRelease { |
| 1378 | vm_id: regs[1] as u16, |
| 1379 | }, |
| 1380 | FuncId::RxTxMap32 => { |
| 1381 | let addr = RxTxAddr::Addr32 { |
| 1382 | rx: regs[2] as u32, |
| 1383 | tx: regs[1] as u32, |
| 1384 | }; |
| 1385 | let page_cnt = regs[3] as u32; |
| 1386 | |
| 1387 | Self::RxTxMap { addr, page_cnt } |
| 1388 | } |
| 1389 | FuncId::RxTxMap64 => { |
| 1390 | let addr = RxTxAddr::Addr64 { |
| 1391 | rx: regs[2], |
| 1392 | tx: regs[1], |
| 1393 | }; |
| 1394 | let page_cnt = regs[3] as u32; |
| 1395 | |
| 1396 | Self::RxTxMap { addr, page_cnt } |
| 1397 | } |
| 1398 | FuncId::RxTxUnmap => Self::RxTxUnmap { id: regs[1] as u16 }, |
| 1399 | FuncId::PartitionInfoGet => { |
| 1400 | let uuid_words = [ |
| 1401 | regs[1] as u32, |
| 1402 | regs[2] as u32, |
| 1403 | regs[3] as u32, |
| 1404 | regs[4] as u32, |
| 1405 | ]; |
| 1406 | let mut bytes: [u8; 16] = [0; 16]; |
| 1407 | for (i, b) in uuid_words.iter().flat_map(|w| w.to_le_bytes()).enumerate() { |
| 1408 | bytes[i] = b; |
| 1409 | } |
| 1410 | Self::PartitionInfoGet { |
| 1411 | uuid: Uuid::from_bytes(bytes), |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 1412 | flags: PartitionInfoGetFlags::try_from(regs[5] as u32)?, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | FuncId::IdGet => Self::IdGet, |
| 1416 | FuncId::SpmIdGet => Self::SpmIdGet, |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 1417 | FuncId::MsgWait => Self::MsgWait { |
| 1418 | flags: if version >= Version(1, 2) { |
| 1419 | Some(MsgWaitFlags::try_from(regs[2] as u32)?) |
| 1420 | } else { |
| 1421 | None |
| 1422 | }, |
| 1423 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1424 | FuncId::Yield => Self::Yield, |
| 1425 | FuncId::Run => Self::Run { |
| 1426 | target_info: (regs[1] as u32).into(), |
| 1427 | }, |
| 1428 | FuncId::NormalWorldResume => Self::NormalWorldResume, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 1429 | FuncId::SecondaryEpRegister32 => Self::SecondaryEpRegister { |
| 1430 | entrypoint: SecondaryEpRegisterAddr::Addr32(regs[1] as u32), |
| 1431 | }, |
| 1432 | FuncId::SecondaryEpRegister64 => Self::SecondaryEpRegister { |
| 1433 | entrypoint: SecondaryEpRegisterAddr::Addr64(regs[1]), |
| 1434 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1435 | FuncId::MsgSend2 => Self::MsgSend2 { |
| 1436 | sender_vm_id: regs[1] as u16, |
| 1437 | flags: regs[2] as u32, |
| 1438 | }, |
| 1439 | FuncId::MsgSendDirectReq32 => Self::MsgSendDirectReq { |
| 1440 | src_id: (regs[1] >> 16) as u16, |
| 1441 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1442 | args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 { |
| 1443 | match regs[2] as u32 { |
| 1444 | DirectMsgArgs::VERSION_REQ => DirectMsgArgs::VersionReq { |
| 1445 | version: Version::try_from(regs[3] as u32)?, |
| 1446 | }, |
| 1447 | DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq32 { |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1448 | params: [ |
| 1449 | regs[3] as u32, |
| 1450 | regs[4] as u32, |
| 1451 | regs[5] as u32, |
| 1452 | regs[6] as u32, |
| 1453 | ], |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1454 | }, |
| 1455 | DirectMsgArgs::POWER_WARM_BOOT_REQ => DirectMsgArgs::PowerWarmBootReq { |
| 1456 | boot_type: WarmBootType::try_from(regs[3] as u32)?, |
| 1457 | }, |
| 1458 | DirectMsgArgs::VM_CREATED => DirectMsgArgs::VmCreated { |
| 1459 | handle: memory_management::Handle::from([ |
| 1460 | regs[3] as u32, |
| 1461 | regs[4] as u32, |
| 1462 | ]), |
| 1463 | vm_id: regs[5] as u16, |
| 1464 | }, |
| 1465 | DirectMsgArgs::VM_DESTRUCTED => DirectMsgArgs::VmDestructed { |
| 1466 | handle: memory_management::Handle::from([ |
| 1467 | regs[3] as u32, |
| 1468 | regs[4] as u32, |
| 1469 | ]), |
| 1470 | vm_id: regs[5] as u16, |
| 1471 | }, |
| 1472 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 1473 | } |
| 1474 | } else { |
| 1475 | DirectMsgArgs::Args32([ |
| 1476 | regs[3] as u32, |
| 1477 | regs[4] as u32, |
| 1478 | regs[5] as u32, |
| 1479 | regs[6] as u32, |
| 1480 | regs[7] as u32, |
| 1481 | ]) |
| 1482 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1483 | }, |
| 1484 | FuncId::MsgSendDirectReq64 => Self::MsgSendDirectReq { |
| 1485 | src_id: (regs[1] >> 16) as u16, |
| 1486 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1487 | args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 { |
| 1488 | match regs[2] as u32 { |
| 1489 | DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq64 { |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1490 | params: [regs[3], regs[4], regs[5], regs[6]], |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1491 | }, |
| 1492 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 1493 | } |
| 1494 | } else { |
| 1495 | DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]]) |
| 1496 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1497 | }, |
| 1498 | FuncId::MsgSendDirectResp32 => Self::MsgSendDirectResp { |
| 1499 | src_id: (regs[1] >> 16) as u16, |
| 1500 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1501 | args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 { |
| 1502 | match regs[2] as u32 { |
| 1503 | DirectMsgArgs::VERSION_RESP => { |
| 1504 | if regs[3] as i32 == FfaError::NotSupported.into() { |
| 1505 | DirectMsgArgs::VersionResp { version: None } |
| 1506 | } else { |
| 1507 | DirectMsgArgs::VersionResp { |
| 1508 | version: Some(Version::try_from(regs[3] as u32)?), |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | DirectMsgArgs::POWER_PSCI_RESP => DirectMsgArgs::PowerPsciResp { |
| 1513 | psci_status: regs[3] as i32, |
| 1514 | }, |
| 1515 | DirectMsgArgs::VM_CREATED_ACK => DirectMsgArgs::VmCreatedAck { |
| 1516 | sp_status: (regs[3] as i32).try_into()?, |
| 1517 | }, |
| 1518 | DirectMsgArgs::VM_DESTRUCTED_ACK => DirectMsgArgs::VmDestructedAck { |
| 1519 | sp_status: (regs[3] as i32).try_into()?, |
| 1520 | }, |
| 1521 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 1522 | } |
| 1523 | } else { |
| 1524 | DirectMsgArgs::Args32([ |
| 1525 | regs[3] as u32, |
| 1526 | regs[4] as u32, |
| 1527 | regs[5] as u32, |
| 1528 | regs[6] as u32, |
| 1529 | regs[7] as u32, |
| 1530 | ]) |
| 1531 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1532 | }, |
| 1533 | FuncId::MsgSendDirectResp64 => Self::MsgSendDirectResp { |
| 1534 | src_id: (regs[1] >> 16) as u16, |
| 1535 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1536 | args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 { |
| 1537 | return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)); |
| 1538 | } else { |
| 1539 | DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]]) |
| 1540 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1541 | }, |
| 1542 | FuncId::MemDonate32 => Self::MemDonate { |
| 1543 | total_len: regs[1] as u32, |
| 1544 | frag_len: regs[2] as u32, |
| 1545 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1546 | Some(MemOpBuf::Buf32 { |
| 1547 | addr: regs[3] as u32, |
| 1548 | page_cnt: regs[4] as u32, |
| 1549 | }) |
| 1550 | } else { |
| 1551 | None |
| 1552 | }, |
| 1553 | }, |
| 1554 | FuncId::MemDonate64 => Self::MemDonate { |
| 1555 | total_len: regs[1] as u32, |
| 1556 | frag_len: regs[2] as u32, |
| 1557 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1558 | Some(MemOpBuf::Buf64 { |
| 1559 | addr: regs[3], |
| 1560 | page_cnt: regs[4] as u32, |
| 1561 | }) |
| 1562 | } else { |
| 1563 | None |
| 1564 | }, |
| 1565 | }, |
| 1566 | FuncId::MemLend32 => Self::MemLend { |
| 1567 | total_len: regs[1] as u32, |
| 1568 | frag_len: regs[2] as u32, |
| 1569 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1570 | Some(MemOpBuf::Buf32 { |
| 1571 | addr: regs[3] as u32, |
| 1572 | page_cnt: regs[4] as u32, |
| 1573 | }) |
| 1574 | } else { |
| 1575 | None |
| 1576 | }, |
| 1577 | }, |
| 1578 | FuncId::MemLend64 => Self::MemLend { |
| 1579 | total_len: regs[1] as u32, |
| 1580 | frag_len: regs[2] as u32, |
| 1581 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1582 | Some(MemOpBuf::Buf64 { |
| 1583 | addr: regs[3], |
| 1584 | page_cnt: regs[4] as u32, |
| 1585 | }) |
| 1586 | } else { |
| 1587 | None |
| 1588 | }, |
| 1589 | }, |
| 1590 | FuncId::MemShare32 => Self::MemShare { |
| 1591 | total_len: regs[1] as u32, |
| 1592 | frag_len: regs[2] as u32, |
| 1593 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1594 | Some(MemOpBuf::Buf32 { |
| 1595 | addr: regs[3] as u32, |
| 1596 | page_cnt: regs[4] as u32, |
| 1597 | }) |
| 1598 | } else { |
| 1599 | None |
| 1600 | }, |
| 1601 | }, |
| 1602 | FuncId::MemShare64 => Self::MemShare { |
| 1603 | total_len: regs[1] as u32, |
| 1604 | frag_len: regs[2] as u32, |
| 1605 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1606 | Some(MemOpBuf::Buf64 { |
| 1607 | addr: regs[3], |
| 1608 | page_cnt: regs[4] as u32, |
| 1609 | }) |
| 1610 | } else { |
| 1611 | None |
| 1612 | }, |
| 1613 | }, |
| 1614 | FuncId::MemRetrieveReq32 => Self::MemRetrieveReq { |
| 1615 | total_len: regs[1] as u32, |
| 1616 | frag_len: regs[2] as u32, |
| 1617 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1618 | Some(MemOpBuf::Buf32 { |
| 1619 | addr: regs[3] as u32, |
| 1620 | page_cnt: regs[4] as u32, |
| 1621 | }) |
| 1622 | } else { |
| 1623 | None |
| 1624 | }, |
| 1625 | }, |
| 1626 | FuncId::MemRetrieveReq64 => Self::MemRetrieveReq { |
| 1627 | total_len: regs[1] as u32, |
| 1628 | frag_len: regs[2] as u32, |
| 1629 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 1630 | Some(MemOpBuf::Buf64 { |
| 1631 | addr: regs[3], |
| 1632 | page_cnt: regs[4] as u32, |
| 1633 | }) |
| 1634 | } else { |
| 1635 | None |
| 1636 | }, |
| 1637 | }, |
| 1638 | FuncId::MemRetrieveResp => Self::MemRetrieveResp { |
| 1639 | total_len: regs[1] as u32, |
| 1640 | frag_len: regs[2] as u32, |
| 1641 | }, |
| 1642 | FuncId::MemRelinquish => Self::MemRelinquish, |
| 1643 | FuncId::MemReclaim => Self::MemReclaim { |
| 1644 | handle: memory_management::Handle::from([regs[1] as u32, regs[2] as u32]), |
| 1645 | flags: regs[3] as u32, |
| 1646 | }, |
| 1647 | FuncId::MemPermGet32 => Self::MemPermGet { |
| 1648 | addr: MemAddr::Addr32(regs[1] as u32), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1649 | page_cnt: if version >= Version(1, 3) { |
| 1650 | Some(regs[2] as u32) |
| 1651 | } else { |
| 1652 | None |
| 1653 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1654 | }, |
| 1655 | FuncId::MemPermGet64 => Self::MemPermGet { |
| 1656 | addr: MemAddr::Addr64(regs[1]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1657 | page_cnt: if version >= Version(1, 3) { |
| 1658 | Some(regs[2] as u32) |
| 1659 | } else { |
| 1660 | None |
| 1661 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1662 | }, |
| 1663 | FuncId::MemPermSet32 => Self::MemPermSet { |
| 1664 | addr: MemAddr::Addr32(regs[1] as u32), |
| 1665 | page_cnt: regs[2] as u32, |
| 1666 | mem_perm: regs[3] as u32, |
| 1667 | }, |
| 1668 | FuncId::MemPermSet64 => Self::MemPermSet { |
| 1669 | addr: MemAddr::Addr64(regs[1]), |
| 1670 | page_cnt: regs[2] as u32, |
| 1671 | mem_perm: regs[3] as u32, |
| 1672 | }, |
| 1673 | FuncId::ConsoleLog32 => Self::ConsoleLog { |
| 1674 | char_cnt: regs[1] as u8, |
| 1675 | char_lists: ConsoleLogChars::Reg32([ |
| 1676 | regs[2] as u32, |
| 1677 | regs[3] as u32, |
| 1678 | regs[4] as u32, |
| 1679 | regs[5] as u32, |
| 1680 | regs[6] as u32, |
| 1681 | regs[7] as u32, |
| 1682 | ]), |
| 1683 | }, |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 1684 | FuncId::NotificationBitmapCreate => { |
| 1685 | let tentative_vm_id = regs[1] as u32; |
| 1686 | if (tentative_vm_id >> 16) != 0 { |
| 1687 | return Err(Error::InvalidVmId(tentative_vm_id)); |
| 1688 | } |
| 1689 | Self::NotificationBitmapCreate { |
| 1690 | vm_id: tentative_vm_id as u16, |
| 1691 | vcpu_cnt: regs[2] as u32, |
| 1692 | } |
| 1693 | } |
| 1694 | FuncId::NotificationBitmapDestroy => { |
| 1695 | let tentative_vm_id = regs[1] as u32; |
| 1696 | if (tentative_vm_id >> 16) != 0 { |
| 1697 | return Err(Error::InvalidVmId(tentative_vm_id)); |
| 1698 | } |
| 1699 | Self::NotificationBitmapDestroy { |
| 1700 | vm_id: tentative_vm_id as u16, |
| 1701 | } |
| 1702 | } |
| 1703 | FuncId::NotificationBind => Self::NotificationBind { |
| 1704 | sender_id: (regs[1] >> 16) as u16, |
| 1705 | receiver_id: regs[1] as u16, |
| 1706 | flags: (regs[2] as u32).into(), |
| 1707 | bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff), |
| 1708 | }, |
| 1709 | FuncId::NotificationUnbind => Self::NotificationUnBind { |
| 1710 | sender_id: (regs[1] >> 16) as u16, |
| 1711 | receiver_id: regs[1] as u16, |
| 1712 | bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff), |
| 1713 | }, |
| 1714 | FuncId::NotificationSet => Self::NotificationSet { |
| 1715 | sender_id: (regs[1] >> 16) as u16, |
| 1716 | receiver_id: regs[1] as u16, |
| 1717 | flags: (regs[2] as u32).try_into()?, |
| 1718 | bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff), |
| 1719 | }, |
| 1720 | FuncId::NotificationGet => Self::NotificationGet { |
| 1721 | vcpu_id: (regs[1] >> 16) as u16, |
| 1722 | endpoint_id: regs[1] as u16, |
| 1723 | flags: (regs[2] as u32).into(), |
| 1724 | }, |
| 1725 | FuncId::NotificationInfoGet32 => Self::NotificationInfoGet { is_32bit: true }, |
| 1726 | FuncId::NotificationInfoGet64 => Self::NotificationInfoGet { is_32bit: false }, |
Tomás González | e6fe75f | 2025-04-04 09:46:50 +0100 | [diff] [blame] | 1727 | FuncId::El3IntrHandle => Self::El3IntrHandle, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1728 | _ => panic!("Invalid number of registers (8) for function {:#x?}", fid), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1729 | }; |
| 1730 | |
| 1731 | Ok(msg) |
| 1732 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1733 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1734 | fn unpack_regs18(version: Version, regs: &[u64; 18]) -> Result<Self, Error> { |
| 1735 | assert!(version >= Version(1, 2)); |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1736 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1737 | let fid = FuncId::try_from(regs[0] as u32)?; |
| 1738 | |
| 1739 | let msg = match fid { |
| 1740 | FuncId::Success64 => Self::Success { |
| 1741 | target_info: regs[1] as u32, |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1742 | args: SuccessArgs::Args64_2(regs[2..18].try_into().unwrap()), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1743 | }, |
| 1744 | FuncId::MsgSendDirectReq64_2 => Self::MsgSendDirectReq2 { |
| 1745 | src_id: (regs[1] >> 16) as u16, |
| 1746 | dst_id: regs[1] as u16, |
Tomás González | ce3bc22 | 2025-03-25 14:30:42 +0000 | [diff] [blame] | 1747 | uuid: Uuid::from_u64_pair(regs[2].swap_bytes(), regs[3].swap_bytes()), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1748 | args: DirectMsg2Args(regs[4..18].try_into().unwrap()), |
| 1749 | }, |
| 1750 | FuncId::MsgSendDirectResp64_2 => Self::MsgSendDirectResp2 { |
| 1751 | src_id: (regs[1] >> 16) as u16, |
| 1752 | dst_id: regs[1] as u16, |
| 1753 | args: DirectMsg2Args(regs[4..18].try_into().unwrap()), |
| 1754 | }, |
| 1755 | FuncId::ConsoleLog64 => Self::ConsoleLog { |
| 1756 | char_cnt: regs[1] as u8, |
| 1757 | char_lists: ConsoleLogChars::Reg64(regs[2..18].try_into().unwrap()), |
| 1758 | }, |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 1759 | FuncId::PartitionInfoGetRegs => { |
| 1760 | // Bits[15:0]: Start index |
| 1761 | let start_index = (regs[3] & 0xffff) as u16; |
| 1762 | let info_tag = ((regs[3] >> 16) & 0xffff) as u16; |
| 1763 | Self::PartitionInfoGetRegs { |
| 1764 | uuid: Uuid::from_u64_pair(regs[1].swap_bytes(), regs[2].swap_bytes()), |
| 1765 | start_index, |
| 1766 | info_tag: if start_index == 0 && info_tag != 0 { |
| 1767 | return Err(Error::InvalidInformationTag(info_tag)); |
| 1768 | } else { |
| 1769 | info_tag |
| 1770 | }, |
| 1771 | } |
| 1772 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1773 | _ => panic!("Invalid number of registers (18) for function {:#x?}", fid), |
| 1774 | }; |
| 1775 | |
| 1776 | Ok(msg) |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1779 | /// Create register contents for an interface. |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1780 | pub fn to_regs(&self, version: Version, regs: &mut [u64]) { |
| 1781 | let reg_cnt = regs.len(); |
| 1782 | |
| 1783 | match reg_cnt { |
| 1784 | 8 => { |
| 1785 | assert!(version <= Version(1, 1)); |
Balint Dobszay | 91bea9b | 2025-04-09 13:16:06 +0200 | [diff] [blame] | 1786 | regs.fill(0); |
| 1787 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1788 | self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap()); |
| 1789 | } |
| 1790 | 18 => { |
| 1791 | assert!(version >= Version(1, 2)); |
Balint Dobszay | 91bea9b | 2025-04-09 13:16:06 +0200 | [diff] [blame] | 1792 | regs.fill(0); |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1793 | |
| 1794 | match self { |
| 1795 | Interface::ConsoleLog { |
| 1796 | char_lists: ConsoleLogChars::Reg64(_), |
| 1797 | .. |
| 1798 | } |
| 1799 | | Interface::Success { |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1800 | args: SuccessArgs::Args64_2(_), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1801 | .. |
| 1802 | } |
| 1803 | | Interface::MsgSendDirectReq2 { .. } |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 1804 | | Interface::MsgSendDirectResp2 { .. } |
| 1805 | | Interface::PartitionInfoGetRegs { .. } => { |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1806 | self.pack_regs18(version, regs.try_into().unwrap()); |
| 1807 | } |
| 1808 | _ => { |
| 1809 | self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap()); |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | _ => panic!("Invalid number of registers {}", reg_cnt), |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | fn pack_regs8(&self, version: Version, a: &mut [u64; 8]) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1818 | if let Some(function_id) = self.function_id() { |
| 1819 | a[0] = function_id as u64; |
| 1820 | } |
| 1821 | |
| 1822 | match *self { |
| 1823 | Interface::Error { |
| 1824 | target_info, |
| 1825 | error_code, |
Balint Dobszay | b727aab | 2025-04-07 10:24:59 +0200 | [diff] [blame] | 1826 | error_arg, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1827 | } => { |
| 1828 | a[1] = u32::from(target_info).into(); |
| 1829 | a[2] = (error_code as u32).into(); |
Balint Dobszay | b727aab | 2025-04-07 10:24:59 +0200 | [diff] [blame] | 1830 | a[3] = error_arg.into(); |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1831 | } |
| 1832 | Interface::Success { target_info, args } => { |
| 1833 | a[1] = target_info.into(); |
| 1834 | match args { |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1835 | SuccessArgs::Args32(regs) => { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1836 | a[2] = regs[0].into(); |
| 1837 | a[3] = regs[1].into(); |
| 1838 | a[4] = regs[2].into(); |
| 1839 | a[5] = regs[3].into(); |
| 1840 | a[6] = regs[4].into(); |
| 1841 | a[7] = regs[5].into(); |
| 1842 | } |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 1843 | SuccessArgs::Args64(regs) => { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1844 | a[2] = regs[0]; |
| 1845 | a[3] = regs[1]; |
| 1846 | a[4] = regs[2]; |
| 1847 | a[5] = regs[3]; |
| 1848 | a[6] = regs[4]; |
| 1849 | a[7] = regs[5]; |
| 1850 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1851 | _ => panic!("{:#x?} requires 18 registers", args), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | Interface::Interrupt { |
| 1855 | target_info, |
| 1856 | interrupt_id, |
| 1857 | } => { |
| 1858 | a[1] = u32::from(target_info).into(); |
| 1859 | a[2] = interrupt_id.into(); |
| 1860 | } |
| 1861 | Interface::Version { input_version } => { |
| 1862 | a[1] = u32::from(input_version).into(); |
| 1863 | } |
| 1864 | Interface::VersionOut { output_version } => { |
| 1865 | a[0] = u32::from(output_version).into(); |
| 1866 | } |
| 1867 | Interface::Features { |
| 1868 | feat_id, |
| 1869 | input_properties, |
| 1870 | } => { |
| 1871 | a[1] = u32::from(feat_id).into(); |
| 1872 | a[2] = input_properties.into(); |
| 1873 | } |
| 1874 | Interface::RxAcquire { vm_id } => { |
| 1875 | a[1] = vm_id.into(); |
| 1876 | } |
| 1877 | Interface::RxRelease { vm_id } => { |
| 1878 | a[1] = vm_id.into(); |
| 1879 | } |
| 1880 | Interface::RxTxMap { addr, page_cnt } => { |
| 1881 | match addr { |
| 1882 | RxTxAddr::Addr32 { rx, tx } => { |
| 1883 | a[1] = tx.into(); |
| 1884 | a[2] = rx.into(); |
| 1885 | } |
| 1886 | RxTxAddr::Addr64 { rx, tx } => { |
| 1887 | a[1] = tx; |
| 1888 | a[2] = rx; |
| 1889 | } |
| 1890 | } |
| 1891 | a[3] = page_cnt.into(); |
| 1892 | } |
| 1893 | Interface::RxTxUnmap { id } => { |
| 1894 | a[1] = id.into(); |
| 1895 | } |
| 1896 | Interface::PartitionInfoGet { uuid, flags } => { |
| 1897 | let bytes = uuid.into_bytes(); |
| 1898 | a[1] = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]).into(); |
| 1899 | a[2] = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]).into(); |
| 1900 | a[3] = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]).into(); |
| 1901 | a[4] = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]).into(); |
Imre Kis | e295adb | 2025-04-10 13:26:28 +0200 | [diff] [blame] | 1902 | a[5] = u32::from(flags).into(); |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1903 | } |
Tomás González | 092202a | 2025-03-05 11:56:45 +0000 | [diff] [blame] | 1904 | Interface::MsgWait { flags } => { |
| 1905 | if version >= Version(1, 2) { |
| 1906 | if let Some(flags) = flags { |
| 1907 | a[2] = u32::from(flags).into(); |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | Interface::IdGet | Interface::SpmIdGet | Interface::Yield => {} |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1912 | Interface::Run { target_info } => { |
| 1913 | a[1] = u32::from(target_info).into(); |
| 1914 | } |
| 1915 | Interface::NormalWorldResume => {} |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 1916 | Interface::SecondaryEpRegister { entrypoint } => match entrypoint { |
| 1917 | SecondaryEpRegisterAddr::Addr32(addr) => a[1] = addr as u64, |
| 1918 | SecondaryEpRegisterAddr::Addr64(addr) => a[1] = addr, |
| 1919 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1920 | Interface::MsgSend2 { |
| 1921 | sender_vm_id, |
| 1922 | flags, |
| 1923 | } => { |
| 1924 | a[1] = sender_vm_id.into(); |
| 1925 | a[2] = flags.into(); |
| 1926 | } |
| 1927 | Interface::MsgSendDirectReq { |
| 1928 | src_id, |
| 1929 | dst_id, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1930 | args, |
| 1931 | } => { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 1932 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1933 | match args { |
| 1934 | DirectMsgArgs::Args32(args) => { |
| 1935 | a[3] = args[0].into(); |
| 1936 | a[4] = args[1].into(); |
| 1937 | a[5] = args[2].into(); |
| 1938 | a[6] = args[3].into(); |
| 1939 | a[7] = args[4].into(); |
| 1940 | } |
| 1941 | DirectMsgArgs::Args64(args) => { |
| 1942 | a[3] = args[0]; |
| 1943 | a[4] = args[1]; |
| 1944 | a[5] = args[2]; |
| 1945 | a[6] = args[3]; |
| 1946 | a[7] = args[4]; |
| 1947 | } |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1948 | DirectMsgArgs::VersionReq { version } => { |
| 1949 | a[2] = DirectMsgArgs::VERSION_REQ.into(); |
| 1950 | a[3] = u32::from(version).into(); |
| 1951 | } |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1952 | DirectMsgArgs::PowerPsciReq32 { params } => { |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1953 | a[2] = DirectMsgArgs::POWER_PSCI_REQ.into(); |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1954 | a[3] = params[0].into(); |
| 1955 | a[4] = params[1].into(); |
| 1956 | a[5] = params[2].into(); |
| 1957 | a[6] = params[3].into(); |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1958 | } |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1959 | DirectMsgArgs::PowerPsciReq64 { params } => { |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1960 | a[2] = DirectMsgArgs::POWER_PSCI_REQ.into(); |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 1961 | a[3] = params[0]; |
| 1962 | a[4] = params[1]; |
| 1963 | a[5] = params[2]; |
| 1964 | a[6] = params[3]; |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1965 | } |
| 1966 | DirectMsgArgs::PowerWarmBootReq { boot_type } => { |
| 1967 | a[2] = DirectMsgArgs::POWER_WARM_BOOT_REQ.into(); |
| 1968 | a[3] = u32::from(boot_type).into(); |
| 1969 | } |
| 1970 | DirectMsgArgs::VmCreated { handle, vm_id } => { |
| 1971 | a[2] = DirectMsgArgs::VM_CREATED.into(); |
| 1972 | let handle_regs: [u32; 2] = handle.into(); |
| 1973 | a[3] = handle_regs[0].into(); |
| 1974 | a[4] = handle_regs[1].into(); |
| 1975 | a[5] = vm_id.into(); |
| 1976 | } |
| 1977 | DirectMsgArgs::VmDestructed { handle, vm_id } => { |
| 1978 | a[2] = DirectMsgArgs::VM_DESTRUCTED.into(); |
| 1979 | let handle_regs: [u32; 2] = handle.into(); |
| 1980 | a[3] = handle_regs[0].into(); |
| 1981 | a[4] = handle_regs[1].into(); |
| 1982 | a[5] = vm_id.into(); |
| 1983 | } |
| 1984 | _ => panic!("Malformed MsgSendDirectReq interface"), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | Interface::MsgSendDirectResp { |
| 1988 | src_id, |
| 1989 | dst_id, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1990 | args, |
| 1991 | } => { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 1992 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1993 | match args { |
| 1994 | DirectMsgArgs::Args32(args) => { |
| 1995 | a[3] = args[0].into(); |
| 1996 | a[4] = args[1].into(); |
| 1997 | a[5] = args[2].into(); |
| 1998 | a[6] = args[3].into(); |
| 1999 | a[7] = args[4].into(); |
| 2000 | } |
| 2001 | DirectMsgArgs::Args64(args) => { |
| 2002 | a[3] = args[0]; |
| 2003 | a[4] = args[1]; |
| 2004 | a[5] = args[2]; |
| 2005 | a[6] = args[3]; |
| 2006 | a[7] = args[4]; |
| 2007 | } |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 2008 | DirectMsgArgs::VersionResp { version } => { |
| 2009 | a[2] = DirectMsgArgs::VERSION_RESP.into(); |
| 2010 | match version { |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 2011 | None => a[3] = (i32::from(FfaError::NotSupported) as u32).into(), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 2012 | Some(ver) => a[3] = u32::from(ver).into(), |
| 2013 | } |
| 2014 | } |
| 2015 | DirectMsgArgs::PowerPsciResp { psci_status } => { |
| 2016 | a[2] = DirectMsgArgs::POWER_PSCI_RESP.into(); |
| 2017 | a[3] = psci_status as u64; |
| 2018 | } |
| 2019 | DirectMsgArgs::VmCreatedAck { sp_status } => { |
| 2020 | a[2] = DirectMsgArgs::VM_CREATED_ACK.into(); |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 2021 | a[3] = (i32::from(sp_status) as u32).into(); |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 2022 | } |
| 2023 | DirectMsgArgs::VmDestructedAck { sp_status } => { |
| 2024 | a[2] = DirectMsgArgs::VM_DESTRUCTED_ACK.into(); |
Tomás González | 67f92c7 | 2025-03-20 16:50:42 +0000 | [diff] [blame] | 2025 | a[3] = (i32::from(sp_status) as u32).into(); |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 2026 | } |
| 2027 | _ => panic!("Malformed MsgSendDirectResp interface"), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | Interface::MemDonate { |
| 2031 | total_len, |
| 2032 | frag_len, |
| 2033 | buf, |
| 2034 | } => { |
| 2035 | a[1] = total_len.into(); |
| 2036 | a[2] = frag_len.into(); |
| 2037 | (a[3], a[4]) = match buf { |
| 2038 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 2039 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 2040 | None => (0, 0), |
| 2041 | }; |
| 2042 | } |
| 2043 | Interface::MemLend { |
| 2044 | total_len, |
| 2045 | frag_len, |
| 2046 | buf, |
| 2047 | } => { |
| 2048 | a[1] = total_len.into(); |
| 2049 | a[2] = frag_len.into(); |
| 2050 | (a[3], a[4]) = match buf { |
| 2051 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 2052 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 2053 | None => (0, 0), |
| 2054 | }; |
| 2055 | } |
| 2056 | Interface::MemShare { |
| 2057 | total_len, |
| 2058 | frag_len, |
| 2059 | buf, |
| 2060 | } => { |
| 2061 | a[1] = total_len.into(); |
| 2062 | a[2] = frag_len.into(); |
| 2063 | (a[3], a[4]) = match buf { |
| 2064 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 2065 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 2066 | None => (0, 0), |
| 2067 | }; |
| 2068 | } |
| 2069 | Interface::MemRetrieveReq { |
| 2070 | total_len, |
| 2071 | frag_len, |
| 2072 | buf, |
| 2073 | } => { |
| 2074 | a[1] = total_len.into(); |
| 2075 | a[2] = frag_len.into(); |
| 2076 | (a[3], a[4]) = match buf { |
| 2077 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 2078 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 2079 | None => (0, 0), |
| 2080 | }; |
| 2081 | } |
| 2082 | Interface::MemRetrieveResp { |
| 2083 | total_len, |
| 2084 | frag_len, |
| 2085 | } => { |
| 2086 | a[1] = total_len.into(); |
| 2087 | a[2] = frag_len.into(); |
| 2088 | } |
| 2089 | Interface::MemRelinquish => {} |
| 2090 | Interface::MemReclaim { handle, flags } => { |
| 2091 | let handle_regs: [u32; 2] = handle.into(); |
| 2092 | a[1] = handle_regs[0].into(); |
| 2093 | a[2] = handle_regs[1].into(); |
| 2094 | a[3] = flags.into(); |
| 2095 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2096 | Interface::MemPermGet { addr, page_cnt } => { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2097 | a[1] = match addr { |
| 2098 | MemAddr::Addr32(addr) => addr.into(), |
| 2099 | MemAddr::Addr64(addr) => addr, |
| 2100 | }; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2101 | a[2] = if version >= Version(1, 3) { |
| 2102 | page_cnt.unwrap().into() |
| 2103 | } else { |
| 2104 | assert!(page_cnt.is_none()); |
| 2105 | 0 |
| 2106 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2107 | } |
| 2108 | Interface::MemPermSet { |
| 2109 | addr, |
| 2110 | page_cnt, |
| 2111 | mem_perm, |
| 2112 | } => { |
| 2113 | a[1] = match addr { |
| 2114 | MemAddr::Addr32(addr) => addr.into(), |
| 2115 | MemAddr::Addr64(addr) => addr, |
| 2116 | }; |
| 2117 | a[2] = page_cnt.into(); |
| 2118 | a[3] = mem_perm.into(); |
| 2119 | } |
| 2120 | Interface::ConsoleLog { |
| 2121 | char_cnt, |
| 2122 | char_lists, |
| 2123 | } => { |
| 2124 | a[1] = char_cnt.into(); |
| 2125 | match char_lists { |
| 2126 | ConsoleLogChars::Reg32(regs) => { |
| 2127 | a[2] = regs[0].into(); |
| 2128 | a[3] = regs[1].into(); |
| 2129 | a[4] = regs[2].into(); |
| 2130 | a[5] = regs[3].into(); |
| 2131 | a[6] = regs[4].into(); |
| 2132 | a[7] = regs[5].into(); |
| 2133 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2134 | _ => panic!("{:#x?} requires 18 registers", char_lists), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2135 | } |
| 2136 | } |
Tomás González | 7ffb613 | 2025-04-03 12:28:58 +0100 | [diff] [blame] | 2137 | Interface::NotificationBitmapCreate { vm_id, vcpu_cnt } => { |
| 2138 | a[1] = vm_id.into(); |
| 2139 | a[2] = vcpu_cnt.into(); |
| 2140 | } |
| 2141 | Interface::NotificationBitmapDestroy { vm_id } => { |
| 2142 | a[1] = vm_id.into(); |
| 2143 | } |
| 2144 | Interface::NotificationBind { |
| 2145 | sender_id, |
| 2146 | receiver_id, |
| 2147 | flags, |
| 2148 | bitmap, |
| 2149 | } => { |
| 2150 | a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id); |
| 2151 | a[2] = u32::from(flags).into(); |
| 2152 | a[3] = bitmap & 0xffff_ffff; |
| 2153 | a[4] = bitmap >> 32; |
| 2154 | } |
| 2155 | Interface::NotificationUnBind { |
| 2156 | sender_id, |
| 2157 | receiver_id, |
| 2158 | bitmap, |
| 2159 | } => { |
| 2160 | a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id); |
| 2161 | a[3] = bitmap & 0xffff_ffff; |
| 2162 | a[4] = bitmap >> 32; |
| 2163 | } |
| 2164 | Interface::NotificationSet { |
| 2165 | sender_id, |
| 2166 | receiver_id, |
| 2167 | flags, |
| 2168 | bitmap, |
| 2169 | } => { |
| 2170 | a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id); |
| 2171 | a[2] = u32::from(flags).into(); |
| 2172 | a[3] = bitmap & 0xffff_ffff; |
| 2173 | a[4] = bitmap >> 32; |
| 2174 | } |
| 2175 | Interface::NotificationGet { |
| 2176 | vcpu_id, |
| 2177 | endpoint_id, |
| 2178 | flags, |
| 2179 | } => { |
| 2180 | a[1] = (u64::from(vcpu_id) << 16) | u64::from(endpoint_id); |
| 2181 | a[2] = u32::from(flags).into(); |
| 2182 | } |
| 2183 | Interface::NotificationInfoGet { .. } => {} |
Tomás González | e6fe75f | 2025-04-04 09:46:50 +0100 | [diff] [blame] | 2184 | Interface::El3IntrHandle => {} |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2185 | _ => panic!("{:#x?} requires 18 registers", self), |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | fn pack_regs18(&self, version: Version, a: &mut [u64; 18]) { |
| 2190 | assert!(version >= Version(1, 2)); |
| 2191 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2192 | if let Some(function_id) = self.function_id() { |
| 2193 | a[0] = function_id as u64; |
| 2194 | } |
| 2195 | |
| 2196 | match *self { |
| 2197 | Interface::Success { target_info, args } => { |
| 2198 | a[1] = target_info.into(); |
| 2199 | match args { |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 2200 | SuccessArgs::Args64_2(regs) => a[2..18].copy_from_slice(®s[..16]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2201 | _ => panic!("{:#x?} requires 8 registers", args), |
| 2202 | } |
| 2203 | } |
| 2204 | Interface::MsgSendDirectReq2 { |
| 2205 | src_id, |
| 2206 | dst_id, |
| 2207 | uuid, |
| 2208 | args, |
| 2209 | } => { |
| 2210 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
Tomás González | ce3bc22 | 2025-03-25 14:30:42 +0000 | [diff] [blame] | 2211 | let (uuid_msb, uuid_lsb) = uuid.as_u64_pair(); |
| 2212 | (a[2], a[3]) = (uuid_msb.swap_bytes(), uuid_lsb.swap_bytes()); |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2213 | a[4..18].copy_from_slice(&args.0[..14]); |
| 2214 | } |
| 2215 | Interface::MsgSendDirectResp2 { |
| 2216 | src_id, |
| 2217 | dst_id, |
| 2218 | args, |
| 2219 | } => { |
| 2220 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
| 2221 | a[2] = 0; |
| 2222 | a[3] = 0; |
| 2223 | a[4..18].copy_from_slice(&args.0[..14]); |
| 2224 | } |
| 2225 | Interface::ConsoleLog { |
| 2226 | char_cnt, |
| 2227 | char_lists, |
| 2228 | } => { |
| 2229 | a[1] = char_cnt.into(); |
| 2230 | match char_lists { |
| 2231 | ConsoleLogChars::Reg64(regs) => a[2..18].copy_from_slice(®s[..16]), |
| 2232 | _ => panic!("{:#x?} requires 8 registers", char_lists), |
| 2233 | } |
| 2234 | } |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 2235 | Interface::PartitionInfoGetRegs { |
| 2236 | uuid, |
| 2237 | start_index, |
| 2238 | info_tag, |
| 2239 | } => { |
| 2240 | if start_index == 0 && info_tag != 0 { |
| 2241 | panic!("Information Tag MBZ if start index is 0: {:#x?}", self); |
| 2242 | } |
| 2243 | let (uuid_msb, uuid_lsb) = uuid.as_u64_pair(); |
| 2244 | (a[1], a[2]) = (uuid_msb.swap_bytes(), uuid_lsb.swap_bytes()); |
| 2245 | a[3] = (u64::from(info_tag) << 16) | u64::from(start_index); |
| 2246 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2247 | _ => panic!("{:#x?} requires 8 registers", self), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2251 | /// Helper function to create an `FFA_SUCCESS` interface without any arguments. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2252 | pub fn success32_noargs() -> Self { |
| 2253 | Self::Success { |
| 2254 | target_info: 0, |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 2255 | args: SuccessArgs::Args32([0; 6]), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Tomás González | 4c8c7d2 | 2025-03-10 17:14:57 +0000 | [diff] [blame] | 2259 | /// Helper function to create an `FFA_SUCCESS` interface without any arguments. |
| 2260 | pub fn success64_noargs() -> Self { |
| 2261 | Self::Success { |
| 2262 | target_info: 0, |
Imre Kis | 54773b6 | 2025-04-10 13:47:39 +0200 | [diff] [blame] | 2263 | args: SuccessArgs::Args64([0; 6]), |
Tomás González | 4c8c7d2 | 2025-03-10 17:14:57 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2267 | /// Helper function to create an `FFA_ERROR` interface with an error code. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2268 | pub fn error(error_code: FfaError) -> Self { |
| 2269 | Self::Error { |
| 2270 | target_info: TargetInfo { |
| 2271 | endpoint_id: 0, |
| 2272 | vcpu_id: 0, |
| 2273 | }, |
| 2274 | error_code, |
Balint Dobszay | b727aab | 2025-04-07 10:24:59 +0200 | [diff] [blame] | 2275 | error_arg: 0, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2280 | /// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG32` message. |
| 2281 | pub const CONSOLE_LOG_32_MAX_CHAR_CNT: u8 = 24; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 2282 | /// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG64` message. |
| 2283 | pub const CONSOLE_LOG_64_MAX_CHAR_CNT: u8 = 128; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2284 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2285 | /// Helper function to convert the "Tightly packed list of characters" format used by the |
| 2286 | /// `FFA_CONSOLE_LOG` interface into a byte slice. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2287 | pub fn parse_console_log( |
| 2288 | char_cnt: u8, |
| 2289 | char_lists: &ConsoleLogChars, |
| 2290 | log_bytes: &mut [u8], |
| 2291 | ) -> Result<(), FfaError> { |
| 2292 | match char_lists { |
| 2293 | ConsoleLogChars::Reg32(regs) => { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2294 | if !(1..=CONSOLE_LOG_32_MAX_CHAR_CNT).contains(&char_cnt) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2295 | return Err(FfaError::InvalidParameters); |
| 2296 | } |
| 2297 | for (i, reg) in regs.iter().enumerate() { |
| 2298 | log_bytes[4 * i..4 * (i + 1)].copy_from_slice(®.to_le_bytes()); |
| 2299 | } |
| 2300 | } |
| 2301 | ConsoleLogChars::Reg64(regs) => { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 2302 | if !(1..=CONSOLE_LOG_64_MAX_CHAR_CNT).contains(&char_cnt) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 2303 | return Err(FfaError::InvalidParameters); |
| 2304 | } |
| 2305 | for (i, reg) in regs.iter().enumerate() { |
| 2306 | log_bytes[8 * i..8 * (i + 1)].copy_from_slice(®.to_le_bytes()); |
| 2307 | } |
| 2308 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Balint Dobszay | c880249 | 2025-01-15 18:11:39 +0100 | [diff] [blame] | 2311 | Ok(()) |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 2312 | } |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 2313 | |
| 2314 | #[cfg(test)] |
| 2315 | mod tests { |
| 2316 | use super::*; |
| 2317 | |
| 2318 | #[test] |
| 2319 | fn part_info_get_regs() { |
| 2320 | let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap(); |
| 2321 | let uuid_bytes = uuid.as_bytes(); |
| 2322 | let test_info_tag = 0b1101_1101; |
| 2323 | let test_start_index = 0b1101; |
| 2324 | let start_index_and_tag = (test_info_tag << 16) | test_start_index; |
| 2325 | let version = Version(1, 2); |
| 2326 | |
| 2327 | // From spec: |
| 2328 | // Bytes[0...7] of UUID with byte 0 in the low-order bits. |
| 2329 | let reg_x1 = (uuid_bytes[7] as u64) << 56 |
| 2330 | | (uuid_bytes[6] as u64) << 48 |
| 2331 | | (uuid_bytes[5] as u64) << 40 |
| 2332 | | (uuid_bytes[4] as u64) << 32 |
| 2333 | | (uuid_bytes[3] as u64) << 24 |
| 2334 | | (uuid_bytes[2] as u64) << 16 |
| 2335 | | (uuid_bytes[1] as u64) << 8 |
| 2336 | | (uuid_bytes[0] as u64); |
| 2337 | |
| 2338 | // From spec: |
| 2339 | // Bytes[8...15] of UUID with byte 8 in the low-order bits. |
| 2340 | let reg_x2 = (uuid_bytes[15] as u64) << 56 |
| 2341 | | (uuid_bytes[14] as u64) << 48 |
| 2342 | | (uuid_bytes[13] as u64) << 40 |
| 2343 | | (uuid_bytes[12] as u64) << 32 |
| 2344 | | (uuid_bytes[11] as u64) << 24 |
| 2345 | | (uuid_bytes[10] as u64) << 16 |
| 2346 | | (uuid_bytes[9] as u64) << 8 |
| 2347 | | (uuid_bytes[8] as u64); |
| 2348 | |
| 2349 | // First, test for wrong tag: |
| 2350 | { |
| 2351 | let mut regs = [0u64; 18]; |
| 2352 | regs[0] = FuncId::PartitionInfoGetRegs as u64; |
| 2353 | regs[1] = reg_x1; |
| 2354 | regs[2] = reg_x2; |
| 2355 | regs[3] = test_info_tag << 16; |
| 2356 | |
| 2357 | assert!(Interface::from_regs(version, ®s).is_err_and( |
| 2358 | |e| e == Error::InvalidInformationTag(test_info_tag.try_into().unwrap()) |
| 2359 | )); |
| 2360 | } |
| 2361 | |
| 2362 | // Test for regs -> Interface -> regs |
| 2363 | { |
| 2364 | let mut orig_regs = [0u64; 18]; |
| 2365 | orig_regs[0] = FuncId::PartitionInfoGetRegs as u64; |
| 2366 | orig_regs[1] = reg_x1; |
| 2367 | orig_regs[2] = reg_x2; |
| 2368 | orig_regs[3] = start_index_and_tag; |
| 2369 | |
| 2370 | let mut test_regs = orig_regs.clone(); |
| 2371 | let interface = Interface::from_regs(version, &mut test_regs).unwrap(); |
| 2372 | match &interface { |
| 2373 | Interface::PartitionInfoGetRegs { |
| 2374 | info_tag, |
| 2375 | start_index, |
| 2376 | uuid: int_uuid, |
| 2377 | } => { |
| 2378 | assert_eq!(u64::from(*info_tag), test_info_tag); |
| 2379 | assert_eq!(u64::from(*start_index), test_start_index); |
| 2380 | assert_eq!(*int_uuid, uuid); |
| 2381 | } |
| 2382 | _ => panic!("Expecting Interface::PartitionInfoGetRegs!"), |
| 2383 | } |
| 2384 | test_regs.fill(0); |
| 2385 | interface.to_regs(version, &mut test_regs); |
| 2386 | assert_eq!(orig_regs, test_regs); |
| 2387 | } |
| 2388 | |
| 2389 | // Test for Interface -> regs -> Interface |
| 2390 | { |
| 2391 | let interface = Interface::PartitionInfoGetRegs { |
| 2392 | info_tag: test_info_tag.try_into().unwrap(), |
| 2393 | start_index: test_start_index.try_into().unwrap(), |
| 2394 | uuid, |
| 2395 | }; |
| 2396 | |
| 2397 | let mut regs: [u64; 18] = [0; 18]; |
| 2398 | interface.to_regs(version, &mut regs); |
| 2399 | |
| 2400 | assert_eq!(Some(FuncId::PartitionInfoGetRegs), interface.function_id()); |
| 2401 | assert_eq!(regs[0], interface.function_id().unwrap() as u64); |
| 2402 | assert_eq!(regs[1], reg_x1); |
| 2403 | assert_eq!(regs[2], reg_x2); |
| 2404 | assert_eq!(regs[3], (test_info_tag << 16) | test_start_index); |
| 2405 | |
| 2406 | assert_eq!(Interface::from_regs(version, ®s).unwrap(), interface); |
| 2407 | } |
| 2408 | } |
Tomás González | ce3bc22 | 2025-03-25 14:30:42 +0000 | [diff] [blame] | 2409 | |
| 2410 | #[test] |
| 2411 | fn msg_send_direct_req2() { |
| 2412 | let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap(); |
| 2413 | let uuid_bytes = uuid.as_bytes(); |
| 2414 | |
| 2415 | // From spec: |
| 2416 | // Bytes[0...7] of UUID with byte 0 in the low-order bits. |
| 2417 | let reg_x2 = (uuid_bytes[7] as u64) << 56 |
| 2418 | | (uuid_bytes[6] as u64) << 48 |
| 2419 | | (uuid_bytes[5] as u64) << 40 |
| 2420 | | (uuid_bytes[4] as u64) << 32 |
| 2421 | | (uuid_bytes[3] as u64) << 24 |
| 2422 | | (uuid_bytes[2] as u64) << 16 |
| 2423 | | (uuid_bytes[1] as u64) << 8 |
| 2424 | | (uuid_bytes[0] as u64); |
| 2425 | |
| 2426 | // From spec: |
| 2427 | // Bytes[8...15] of UUID with byte 8 in the low-order bits. |
| 2428 | let reg_x3 = (uuid_bytes[15] as u64) << 56 |
| 2429 | | (uuid_bytes[14] as u64) << 48 |
| 2430 | | (uuid_bytes[13] as u64) << 40 |
| 2431 | | (uuid_bytes[12] as u64) << 32 |
| 2432 | | (uuid_bytes[11] as u64) << 24 |
| 2433 | | (uuid_bytes[10] as u64) << 16 |
| 2434 | | (uuid_bytes[9] as u64) << 8 |
| 2435 | | (uuid_bytes[8] as u64); |
| 2436 | |
| 2437 | let test_sender = 0b1101_1101; |
| 2438 | let test_receiver = 0b1101; |
| 2439 | let test_sender_receiver = (test_sender << 16) | test_receiver; |
| 2440 | let version = Version(1, 2); |
| 2441 | |
| 2442 | // Test for regs -> Interface -> regs |
| 2443 | { |
| 2444 | let mut orig_regs = [0u64; 18]; |
| 2445 | orig_regs[0] = FuncId::MsgSendDirectReq64_2 as u64; |
| 2446 | orig_regs[1] = test_sender_receiver; |
| 2447 | orig_regs[2] = reg_x2; |
| 2448 | orig_regs[3] = reg_x3; |
| 2449 | |
| 2450 | let mut test_regs = orig_regs.clone(); |
| 2451 | let interface = Interface::from_regs(version, &mut test_regs).unwrap(); |
| 2452 | match &interface { |
| 2453 | Interface::MsgSendDirectReq2 { |
| 2454 | dst_id, |
| 2455 | src_id, |
| 2456 | args: _, |
| 2457 | uuid: int_uuid, |
| 2458 | } => { |
| 2459 | assert_eq!(u64::from(*src_id), test_sender); |
| 2460 | assert_eq!(u64::from(*dst_id), test_receiver); |
| 2461 | assert_eq!(*int_uuid, uuid); |
| 2462 | } |
| 2463 | _ => panic!("Expecting Interface::MsgSendDirectReq2!"), |
| 2464 | } |
| 2465 | test_regs.fill(0); |
| 2466 | interface.to_regs(version, &mut test_regs); |
| 2467 | assert_eq!(orig_regs, test_regs); |
| 2468 | } |
| 2469 | |
| 2470 | // Test for Interface -> regs -> Interface |
| 2471 | { |
| 2472 | let rest_of_regs: [u64; 14] = [0; 14]; |
| 2473 | |
| 2474 | let interface = Interface::MsgSendDirectReq2 { |
| 2475 | src_id: test_sender.try_into().unwrap(), |
| 2476 | dst_id: test_receiver.try_into().unwrap(), |
| 2477 | uuid, |
| 2478 | args: DirectMsg2Args(rest_of_regs), |
| 2479 | }; |
| 2480 | |
| 2481 | let mut regs: [u64; 18] = [0; 18]; |
| 2482 | interface.to_regs(version, &mut regs); |
| 2483 | |
| 2484 | assert_eq!(Some(FuncId::MsgSendDirectReq64_2), interface.function_id()); |
| 2485 | assert_eq!(regs[0], interface.function_id().unwrap() as u64); |
| 2486 | assert_eq!(regs[1], test_sender_receiver); |
| 2487 | assert_eq!(regs[2], reg_x2); |
| 2488 | assert_eq!(regs[3], reg_x3); |
| 2489 | assert_eq!(regs[4], 0); |
| 2490 | |
| 2491 | assert_eq!(Interface::from_regs(version, ®s).unwrap(), interface); |
| 2492 | } |
| 2493 | } |
Tomás González | 6ccba0a | 2025-04-09 13:31:29 +0100 | [diff] [blame] | 2494 | |
| 2495 | #[test] |
| 2496 | fn is_32bit() { |
| 2497 | let interface_64 = Interface::MsgSendDirectReq { |
| 2498 | src_id: 0, |
| 2499 | dst_id: 1, |
| 2500 | args: DirectMsgArgs::Args64([0, 0, 0, 0, 0]), |
| 2501 | }; |
| 2502 | assert!(!interface_64.is_32bit()); |
| 2503 | |
| 2504 | let interface_32 = Interface::MsgSendDirectReq { |
| 2505 | src_id: 0, |
| 2506 | dst_id: 1, |
| 2507 | args: DirectMsgArgs::Args32([0, 0, 0, 0, 0]), |
| 2508 | }; |
| 2509 | assert!(interface_32.is_32bit()); |
| 2510 | } |
Imre Kis | 787c500 | 2025-04-10 14:25:51 +0200 | [diff] [blame] | 2511 | |
| 2512 | #[test] |
| 2513 | fn success_args_notification_info_get32() { |
| 2514 | let mut notifications = SuccessArgsNotificationInfoGet32::default(); |
| 2515 | |
| 2516 | // 16.7.1.1 Example usage |
| 2517 | notifications.add_list(0x0000, &[0, 2, 3]).unwrap(); |
| 2518 | notifications.add_list(0x0000, &[4, 6]).unwrap(); |
| 2519 | notifications.add_list(0x0002, &[]).unwrap(); |
| 2520 | notifications.add_list(0x0003, &[1]).unwrap(); |
| 2521 | |
| 2522 | let args: SuccessArgs = notifications.into(); |
| 2523 | assert_eq!( |
| 2524 | SuccessArgs::Args32([ |
| 2525 | 0x0004_b200, |
| 2526 | 0x0000_0000, |
| 2527 | 0x0003_0002, |
| 2528 | 0x0004_0000, |
| 2529 | 0x0002_0006, |
| 2530 | 0x0001_0003 |
| 2531 | ]), |
| 2532 | args |
| 2533 | ); |
| 2534 | |
| 2535 | let notifications = SuccessArgsNotificationInfoGet32::try_from(args).unwrap(); |
| 2536 | let mut iter = notifications.iter(); |
| 2537 | assert_eq!(Some((0x0000, &[0, 2, 3][..])), iter.next()); |
| 2538 | assert_eq!(Some((0x0000, &[4, 6][..])), iter.next()); |
| 2539 | assert_eq!(Some((0x0002, &[][..])), iter.next()); |
| 2540 | assert_eq!(Some((0x0003, &[1][..])), iter.next()); |
| 2541 | } |
| 2542 | |
| 2543 | #[test] |
| 2544 | fn success_args_notification_info_get64() { |
| 2545 | let mut notifications = SuccessArgsNotificationInfoGet64::default(); |
| 2546 | |
| 2547 | // 16.7.1.1 Example usage |
| 2548 | notifications.add_list(0x0000, &[0, 2, 3]).unwrap(); |
| 2549 | notifications.add_list(0x0000, &[4, 6]).unwrap(); |
| 2550 | notifications.add_list(0x0002, &[]).unwrap(); |
| 2551 | notifications.add_list(0x0003, &[1]).unwrap(); |
| 2552 | |
| 2553 | let args: SuccessArgs = notifications.into(); |
| 2554 | assert_eq!( |
| 2555 | SuccessArgs::Args64([ |
| 2556 | 0x0004_b200, |
| 2557 | 0x0003_0002_0000_0000, |
| 2558 | 0x0002_0006_0004_0000, |
| 2559 | 0x0000_0000_0001_0003, |
| 2560 | 0x0000_0000_0000_0000, |
| 2561 | 0x0000_0000_0000_0000, |
| 2562 | ]), |
| 2563 | args |
| 2564 | ); |
| 2565 | |
| 2566 | let notifications = SuccessArgsNotificationInfoGet64::try_from(args).unwrap(); |
| 2567 | let mut iter = notifications.iter(); |
| 2568 | assert_eq!(Some((0x0000, &[0, 2, 3][..])), iter.next()); |
| 2569 | assert_eq!(Some((0x0000, &[4, 6][..])), iter.next()); |
| 2570 | assert_eq!(Some((0x0002, &[][..])), iter.next()); |
| 2571 | assert_eq!(Some((0x0003, &[1][..])), iter.next()); |
| 2572 | } |
Tomás González | 0a058bc | 2025-03-11 11:20:55 +0000 | [diff] [blame] | 2573 | } |