Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 9 | #pragma once |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 10 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 11 | #include <stdatomic.h> |
| 12 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 13 | #include "hf/arch/types.h" |
| 14 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 15 | #include "hf/cpu.h" |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 16 | #include "hf/list.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/mm.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 18 | #include "hf/mpool.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 19 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 20 | #include "vmapi/hf/ffa.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 21 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 22 | #define MAX_SMCS 32 |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 23 | #define LOG_BUFFER_SIZE 256 |
| 24 | |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 25 | /** |
| 26 | * The state of an RX buffer. |
| 27 | * |
| 28 | * EMPTY is the initial state. The follow state transitions are possible: |
Olivier Deprez | cf6e386 | 2021-01-18 10:24:58 +0100 | [diff] [blame] | 29 | * * EMPTY => RECEIVED: message sent to the VM. |
| 30 | * * RECEIVED => READ: secondary VM returns from FFA_MSG_WAIT or |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 31 | * FFA_MSG_POLL, or primary VM returns from FFA_RUN with an FFA_MSG_SEND |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 32 | * where the receiver is itself. |
Olivier Deprez | cf6e386 | 2021-01-18 10:24:58 +0100 | [diff] [blame] | 33 | * * READ => EMPTY: VM called FFA_RX_RELEASE. |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 34 | */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 35 | enum mailbox_state { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 36 | /** There is no message in the mailbox. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 37 | MAILBOX_STATE_EMPTY, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 38 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 39 | /** There is a message in the mailbox that is waiting for a reader. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 40 | MAILBOX_STATE_RECEIVED, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 41 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 42 | /** There is a message in the mailbox that has been read. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 43 | MAILBOX_STATE_READ, |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 46 | struct wait_entry { |
| 47 | /** The VM that is waiting for a mailbox to become writable. */ |
| 48 | struct vm *waiting_vm; |
| 49 | |
| 50 | /** |
| 51 | * Links used to add entry to a VM's waiter_list. This is protected by |
| 52 | * the notifying VM's lock. |
| 53 | */ |
| 54 | struct list_entry wait_links; |
| 55 | |
| 56 | /** |
| 57 | * Links used to add entry to a VM's ready_list. This is protected by |
| 58 | * the waiting VM's lock. |
| 59 | */ |
| 60 | struct list_entry ready_links; |
| 61 | }; |
| 62 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 63 | struct mailbox { |
| 64 | enum mailbox_state state; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 65 | void *recv; |
| 66 | const void *send; |
| 67 | |
| 68 | /** The ID of the VM which sent the message currently in `recv`. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 69 | ffa_vm_id_t recv_sender; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 70 | |
| 71 | /** The size of the message currently in `recv`. */ |
| 72 | uint32_t recv_size; |
| 73 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 74 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 75 | * The FF-A function ID to use to deliver the message currently in |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 76 | * `recv`. |
| 77 | */ |
| 78 | uint32_t recv_func; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * List of wait_entry structs representing VMs that want to be notified |
| 82 | * when the mailbox becomes writable. Once the mailbox does become |
| 83 | * writable, the entry is removed from this list and added to the |
| 84 | * waiting VM's ready_list. |
| 85 | */ |
| 86 | struct list_entry waiter_list; |
| 87 | |
| 88 | /** |
| 89 | * List of wait_entry structs representing VMs whose mailboxes became |
| 90 | * writable since the owner of the mailbox registers for notification. |
| 91 | */ |
| 92 | struct list_entry ready_list; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 95 | struct notifications_state { |
| 96 | /** |
| 97 | * To keep track of the notifications pending. |
| 98 | * Set on call to FFA_NOTIFICATION_SET, and cleared on call to |
| 99 | * FFA_NOTIFICATION_GET. |
| 100 | */ |
| 101 | ffa_notifications_bitmap_t pending; |
| 102 | |
| 103 | /** |
| 104 | * Set on FFA_NOTIFICATION_INFO_GET to keep track of the notifications |
| 105 | * whose information has been retrieved by the referred ABI. |
| 106 | * Cleared on call to FFA_NOTIFICATION_GET. |
| 107 | */ |
| 108 | ffa_notifications_bitmap_t info_get_retrieved; |
| 109 | }; |
| 110 | |
| 111 | struct notifications { |
| 112 | /** |
| 113 | * The following array maps the notifications to the bound FF-A |
| 114 | * endpoint. |
| 115 | * The index in the bindings array relates to the notification |
| 116 | * ID, and bit position in 'ffa_notifications_bitmap_t'. |
| 117 | */ |
| 118 | ffa_vm_id_t bindings_sender_id[MAX_FFA_NOTIFICATIONS]; |
| 119 | ffa_notifications_bitmap_t bindings_per_vcpu; |
| 120 | |
| 121 | /* The index of the array below relates to the ID of the VCPU. */ |
| 122 | struct notifications_state per_vcpu[MAX_CPUS]; |
| 123 | struct notifications_state global; |
| 124 | }; |
| 125 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 126 | struct smc_whitelist { |
| 127 | uint32_t smcs[MAX_SMCS]; |
| 128 | uint16_t smc_count; |
| 129 | bool permissive; |
| 130 | }; |
| 131 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 132 | struct vm { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 133 | ffa_vm_id_t id; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 134 | struct ffa_uuid uuid; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 135 | struct smc_whitelist smc_whitelist; |
| 136 | |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame] | 137 | /** See api.c for the partial ordering on locks. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 138 | struct spinlock lock; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 139 | ffa_vcpu_count_t vcpu_count; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 140 | struct vcpu vcpus[MAX_CPUS]; |
Andrew Scull | 89a7524 | 2018-08-06 17:04:55 +0100 | [diff] [blame] | 141 | struct mm_ptable ptable; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 142 | struct mailbox mailbox; |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 143 | |
| 144 | struct { |
| 145 | /** |
| 146 | * State structures for notifications coming from VMs or coming |
| 147 | * from SPs. Both fields are maintained by the SPMC. |
| 148 | * The hypervisor ignores the 'from_sp' field, given VM |
| 149 | * notifications from SPs are managed by the SPMC. |
| 150 | */ |
| 151 | struct notifications from_vm; |
| 152 | struct notifications from_sp; |
| 153 | /* TODO: include framework notifications */ |
| 154 | bool enabled; |
| 155 | } notifications; |
| 156 | |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 157 | char log_buffer[LOG_BUFFER_SIZE]; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 158 | uint16_t log_buffer_length; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 159 | |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Wait entries to be used when waiting on other VM mailboxes. See |
| 162 | * comments on `struct wait_entry` for the lock discipline of these. |
| 163 | */ |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 164 | struct wait_entry wait_entries[MAX_VMS]; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 165 | |
| 166 | atomic_bool aborting; |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 167 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 168 | /** |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 169 | * Booting parameters (FF-A SP partitions). |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 170 | */ |
| 171 | bool initialized; |
| 172 | uint16_t boot_order; |
Maksims Svecovs | b596eab | 2021-04-27 00:52:27 +0100 | [diff] [blame] | 173 | uint8_t messaging_method; |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 174 | bool managed_exit; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 175 | struct vm *next_boot; |
| 176 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 177 | /** |
| 178 | * Secondary entry point supplied by FFA_SECONDARY_EP_REGISTER used |
| 179 | * for cold and warm boot of SP execution contexts. |
| 180 | */ |
| 181 | ipaddr_t secondary_ep; |
| 182 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 183 | /** Arch-specific VM information. */ |
| 184 | struct arch_vm arch; |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 185 | bool el0_partition; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /** Encapsulates a VM whose lock is held. */ |
| 189 | struct vm_locked { |
| 190 | struct vm *vm; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 191 | }; |
| 192 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 193 | /** Container for two vm_locked structures */ |
| 194 | struct two_vm_locked { |
| 195 | struct vm_locked vm1; |
| 196 | struct vm_locked vm2; |
| 197 | }; |
| 198 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 199 | struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count, |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 200 | struct mpool *ppool, bool el0_partition); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 201 | bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool, |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 202 | struct vm **new_vm, bool el0_partition); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 203 | ffa_vm_count_t vm_get_count(void); |
| 204 | struct vm *vm_find(ffa_vm_id_t id); |
J-Alves | 46ee068 | 2021-07-26 15:17:53 +0100 | [diff] [blame] | 205 | struct vm_locked vm_find_locked(ffa_vm_id_t id); |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 206 | struct vm *vm_find_index(uint16_t index); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 207 | struct vm_locked vm_lock(struct vm *vm); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 208 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 209 | void vm_unlock(struct vm_locked *locked); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 210 | struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index); |
| 211 | struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm); |
| 212 | ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry); |
Andrew Walbran | 45633dd | 2020-10-07 17:59:54 +0100 | [diff] [blame] | 213 | bool vm_id_is_current_world(ffa_vm_id_t vm_id); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 214 | |
| 215 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 216 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
| 217 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 218 | uint32_t mode, struct mpool *ppool); |
| 219 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 220 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
| 221 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 222 | struct mpool *ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 223 | void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 224 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 225 | |
| 226 | void vm_update_boot(struct vm *vm); |
| 227 | struct vm *vm_get_first_boot(void); |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 228 | |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 229 | bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end, |
| 230 | uint32_t *mode); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 231 | |
| 232 | void vm_notifications_init_bindings(struct notifications *n); |
| 233 | bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm, |
| 234 | ffa_notifications_bitmap_t notifications); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 235 | bool vm_are_notifications_enabled(struct vm_locked vm_locked); |
| 236 | bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked, |
| 237 | bool is_from_vm, bool is_per_vcpu, |
| 238 | ffa_notifications_bitmap_t notif); |
| 239 | bool vm_notifications_validate_bound_sender( |
| 240 | struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id, |
| 241 | ffa_notifications_bitmap_t notifications); |
| 242 | bool vm_notifications_validate_binding(struct vm_locked vm_locked, |
| 243 | bool is_from_vm, ffa_vm_id_t sender_id, |
| 244 | ffa_notifications_bitmap_t notifications, |
| 245 | bool is_per_vcpu); |
| 246 | void vm_notifications_update_bindings(struct vm_locked vm_locked, |
| 247 | bool is_from_vm, ffa_vm_id_t sender_id, |
| 248 | ffa_notifications_bitmap_t notifications, |
| 249 | bool is_per_vcpu); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame^] | 250 | void vm_notifications_set(struct vm_locked vm_locked, bool is_from_vm, |
| 251 | ffa_notifications_bitmap_t notifications, |
| 252 | ffa_vcpu_index_t vcpu_id, bool is_per_vcpu); |
| 253 | ffa_notifications_bitmap_t vm_notifications_get_pending_and_clear( |
| 254 | struct vm_locked vm_locked, bool is_from_vm, |
| 255 | ffa_vcpu_index_t cur_vcpu_id); |