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 | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 9 | #include "hf/vm.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 10 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 11 | #include "hf/api.h" |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 12 | #include "hf/assert.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 13 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 14 | #include "hf/cpu.h" |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 15 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 16 | #include "hf/ffa.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 17 | #include "hf/layout.h" |
| 18 | #include "hf/plat/iommu.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 19 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 20 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 21 | #include "vmapi/hf/call.h" |
| 22 | |
| 23 | static struct vm vms[MAX_VMS]; |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 24 | static struct vm other_world; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 25 | static ffa_vm_count_t vm_count; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 26 | static struct vm *first_boot_vm; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 27 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 28 | /** |
| 29 | * Counters on the status of notifications in the system. It helps to improve |
| 30 | * the information retrieved by the receiver scheduler. |
| 31 | */ |
| 32 | static struct { |
| 33 | /** Counts notifications pending. */ |
| 34 | uint32_t pending_count; |
| 35 | /** |
| 36 | * Counts notifications pending, that have been retrieved by the |
| 37 | * receiver scheduler. |
| 38 | */ |
| 39 | uint32_t info_get_retrieved_count; |
| 40 | struct spinlock lock; |
| 41 | } all_notifications_state; |
| 42 | |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 43 | static bool vm_init_mm(struct vm *vm, struct mpool *ppool) |
| 44 | { |
| 45 | if (vm->el0_partition) { |
| 46 | return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1, |
| 47 | ppool); |
| 48 | } |
| 49 | return mm_vm_init(&vm->ptable, vm->id, ppool); |
| 50 | } |
| 51 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 52 | 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] | 53 | struct mpool *ppool, bool el0_partition) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 54 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 55 | uint32_t i; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 56 | struct vm *vm; |
| 57 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 58 | if (id == HF_OTHER_WORLD_ID) { |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 59 | CHECK(el0_partition == false); |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 60 | vm = &other_world; |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 61 | } else { |
| 62 | uint16_t vm_index = id - HF_VM_ID_OFFSET; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 63 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 64 | CHECK(id >= HF_VM_ID_OFFSET); |
| 65 | CHECK(vm_index < ARRAY_SIZE(vms)); |
| 66 | vm = &vms[vm_index]; |
| 67 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 68 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 69 | memset_s(vm, sizeof(*vm), 0, sizeof(*vm)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 70 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 71 | list_init(&vm->mailbox.waiter_list); |
| 72 | list_init(&vm->mailbox.ready_list); |
| 73 | sl_init(&vm->lock); |
| 74 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 75 | vm->id = id; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 76 | vm->vcpu_count = vcpu_count; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 77 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 78 | atomic_init(&vm->aborting, false); |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 79 | vm->el0_partition = el0_partition; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 80 | |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 81 | if (!vm_init_mm(vm, ppool)) { |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 82 | return NULL; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 85 | /* Initialise waiter entries. */ |
| 86 | for (i = 0; i < MAX_VMS; i++) { |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 87 | vm->wait_entries[i].waiting_vm = vm; |
| 88 | list_init(&vm->wait_entries[i].wait_links); |
| 89 | list_init(&vm->wait_entries[i].ready_links); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 92 | /* Do basic initialization of vCPUs. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 93 | for (i = 0; i < vcpu_count; i++) { |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 94 | vcpu_init(vm_get_vcpu(vm, i), vm); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 95 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 96 | |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 97 | /* Basic initialization of the notifications structure. */ |
| 98 | vm_notifications_init_bindings(&vm->notifications.from_sp); |
| 99 | vm_notifications_init_bindings(&vm->notifications.from_vm); |
| 100 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 101 | return vm; |
| 102 | } |
| 103 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 104 | 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] | 105 | struct vm **new_vm, bool el0_partition) |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 106 | { |
| 107 | if (vm_count >= MAX_VMS) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */ |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 112 | *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool, |
| 113 | el0_partition); |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 114 | if (*new_vm == NULL) { |
| 115 | return false; |
| 116 | } |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 117 | ++vm_count; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 118 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 119 | return true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 122 | ffa_vm_count_t vm_get_count(void) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 123 | { |
| 124 | return vm_count; |
| 125 | } |
| 126 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 127 | /** |
| 128 | * Returns a pointer to the VM with the corresponding id. |
| 129 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 130 | struct vm *vm_find(ffa_vm_id_t id) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 131 | { |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 132 | uint16_t index; |
Fuad Tabba | 494376e | 2019-08-05 12:35:10 +0100 | [diff] [blame] | 133 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 134 | if (id == HF_OTHER_WORLD_ID) { |
| 135 | if (other_world.id == HF_OTHER_WORLD_ID) { |
| 136 | return &other_world; |
| 137 | } |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 141 | /* Check that this is not a reserved ID. */ |
| 142 | if (id < HF_VM_ID_OFFSET) { |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 146 | index = id - HF_VM_ID_OFFSET; |
| 147 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 148 | return vm_find_index(index); |
| 149 | } |
| 150 | |
| 151 | /** |
J-Alves | 46ee068 | 2021-07-26 15:17:53 +0100 | [diff] [blame] | 152 | * Returns a locked instance of the VM with the corresponding id. |
| 153 | */ |
| 154 | struct vm_locked vm_find_locked(ffa_vm_id_t id) |
| 155 | { |
| 156 | struct vm *vm = vm_find(id); |
| 157 | |
| 158 | if (vm != NULL) { |
| 159 | return vm_lock(vm); |
| 160 | } |
| 161 | |
| 162 | return (struct vm_locked){.vm = NULL}; |
| 163 | } |
| 164 | |
| 165 | /** |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 166 | * Returns a pointer to the VM at the specified index. |
| 167 | */ |
| 168 | struct vm *vm_find_index(uint16_t index) |
| 169 | { |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 170 | /* Ensure the VM is initialized. */ |
| 171 | if (index >= vm_count) { |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | return &vms[index]; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 178 | /** |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 179 | * Locks the given VM and updates `locked` to hold the newly locked VM. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 180 | */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 181 | struct vm_locked vm_lock(struct vm *vm) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 182 | { |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 183 | struct vm_locked locked = { |
| 184 | .vm = vm, |
| 185 | }; |
| 186 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 187 | sl_lock(&vm->lock); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 188 | |
| 189 | return locked; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 193 | * Locks two VMs ensuring that the locking order is according to the locks' |
| 194 | * addresses. |
| 195 | */ |
| 196 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2) |
| 197 | { |
| 198 | struct two_vm_locked dual_lock; |
| 199 | |
| 200 | sl_lock_both(&vm1->lock, &vm2->lock); |
| 201 | dual_lock.vm1.vm = vm1; |
| 202 | dual_lock.vm2.vm = vm2; |
| 203 | |
| 204 | return dual_lock; |
| 205 | } |
| 206 | |
| 207 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 208 | * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect |
| 209 | * the fact that the VM is no longer locked. |
| 210 | */ |
| 211 | void vm_unlock(struct vm_locked *locked) |
| 212 | { |
| 213 | sl_unlock(&locked->vm->lock); |
| 214 | locked->vm = NULL; |
| 215 | } |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Get the vCPU with the given index from the given VM. |
| 219 | * This assumes the index is valid, i.e. less than vm->vcpu_count. |
| 220 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 221 | struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index) |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 222 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 223 | CHECK(vcpu_index < vm->vcpu_count); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 224 | return &vm->vcpus[vcpu_index]; |
| 225 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 226 | |
| 227 | /** |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 228 | * Gets `vm`'s wait entry for waiting on the `for_vm`. |
| 229 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 230 | struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm) |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 231 | { |
| 232 | uint16_t index; |
| 233 | |
| 234 | CHECK(for_vm >= HF_VM_ID_OFFSET); |
| 235 | index = for_vm - HF_VM_ID_OFFSET; |
| 236 | CHECK(index < MAX_VMS); |
| 237 | |
| 238 | return &vm->wait_entries[index]; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Gets the ID of the VM which the given VM's wait entry is for. |
| 243 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 244 | ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry) |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 245 | { |
| 246 | uint16_t index = entry - vm->wait_entries; |
| 247 | |
| 248 | return index + HF_VM_ID_OFFSET; |
| 249 | } |
| 250 | |
| 251 | /** |
Andrew Walbran | 45633dd | 2020-10-07 17:59:54 +0100 | [diff] [blame] | 252 | * Return whether the given VM ID represents an entity in the current world: |
| 253 | * i.e. the hypervisor or a normal world VM when running in the normal world, or |
| 254 | * the SPM or an SP when running in the secure world. |
| 255 | */ |
| 256 | bool vm_id_is_current_world(ffa_vm_id_t vm_id) |
| 257 | { |
| 258 | return (vm_id & HF_VM_ID_WORLD_MASK) != |
| 259 | (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK); |
| 260 | } |
| 261 | |
| 262 | /** |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 263 | * Map a range of addresses to the VM in both the MMU and the IOMMU. |
| 264 | * |
| 265 | * mm_vm_defrag should always be called after a series of page table updates, |
| 266 | * whether they succeed or fail. This is because on failure extra page table |
| 267 | * entries may have been allocated and then not used, while on success it may be |
| 268 | * possible to compact the page table by merging several entries into a block. |
| 269 | * |
| 270 | * Returns true on success, or false if the update failed and no changes were |
| 271 | * made. |
| 272 | * |
| 273 | */ |
| 274 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 275 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa) |
| 276 | { |
| 277 | if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa); |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Prepares the given VM for the given address mapping such that it will be able |
| 288 | * to commit the change without failure. |
| 289 | * |
| 290 | * In particular, multiple calls to this function will result in the |
| 291 | * corresponding calls to commit the changes to succeed. |
| 292 | * |
| 293 | * Returns true on success, or false if the update failed and no changes were |
| 294 | * made. |
| 295 | */ |
| 296 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 297 | uint32_t mode, struct mpool *ppool) |
| 298 | { |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 299 | if (vm_locked.vm->el0_partition) { |
| 300 | return mm_identity_prepare(&vm_locked.vm->ptable, begin, end, |
| 301 | mode, ppool); |
| 302 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 303 | return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode, |
| 304 | ppool); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Commits the given address mapping to the VM assuming the operation cannot |
| 309 | * fail. `vm_identity_prepare` must used correctly before this to ensure |
| 310 | * this condition. |
| 311 | */ |
| 312 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 313 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa) |
| 314 | { |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 315 | if (vm_locked.vm->el0_partition) { |
| 316 | mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, |
| 317 | ppool); |
| 318 | if (ipa != NULL) { |
| 319 | /* |
| 320 | * EL0 partitions are modeled as lightweight VM's, to |
| 321 | * promote code reuse. The below statement returns the |
| 322 | * mapped PA as an IPA, however, for an EL0 partition, |
| 323 | * this is really a VA. |
| 324 | */ |
| 325 | *ipa = ipa_from_pa(begin); |
| 326 | } |
| 327 | } else { |
| 328 | mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, |
| 329 | ppool, ipa); |
| 330 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 331 | plat_iommu_identity_map(vm_locked, begin, end, mode); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Unmap a range of addresses from the VM. |
| 336 | * |
| 337 | * Returns true on success, or false if the update failed and no changes were |
| 338 | * made. |
| 339 | */ |
| 340 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 341 | struct mpool *ppool) |
| 342 | { |
| 343 | uint32_t mode = MM_MODE_UNMAPPED_MASK; |
| 344 | |
| 345 | return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL); |
| 346 | } |
| 347 | |
| 348 | /** |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 349 | * Defrag page tables for an EL0 partition or for a VM. |
| 350 | */ |
| 351 | void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool) |
| 352 | { |
| 353 | if (vm_locked.vm->el0_partition) { |
| 354 | mm_stage1_defrag(&vm_locked.vm->ptable, ppool); |
| 355 | } else { |
| 356 | mm_vm_defrag(&vm_locked.vm->ptable, ppool); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 361 | * Unmaps the hypervisor pages from the given page table. |
| 362 | */ |
| 363 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool) |
| 364 | { |
| 365 | /* TODO: If we add pages dynamically, they must be included here too. */ |
| 366 | return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(), |
| 367 | ppool) && |
| 368 | vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(), |
| 369 | ppool) && |
| 370 | vm_unmap(vm_locked, layout_data_begin(), layout_data_end(), |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 371 | ppool) && |
| 372 | vm_unmap(vm_locked, layout_stacks_begin(), layout_stacks_end(), |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 373 | ppool); |
| 374 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Gets the first partition to boot, according to Boot Protocol from FFA spec. |
| 378 | */ |
| 379 | struct vm *vm_get_first_boot(void) |
| 380 | { |
| 381 | return first_boot_vm; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Insert in boot list, sorted by `boot_order` parameter in the vm structure |
| 386 | * and rooted in `first_boot_vm`. |
| 387 | */ |
| 388 | void vm_update_boot(struct vm *vm) |
| 389 | { |
| 390 | struct vm *current = NULL; |
| 391 | struct vm *previous = NULL; |
| 392 | |
| 393 | if (first_boot_vm == NULL) { |
| 394 | first_boot_vm = vm; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | current = first_boot_vm; |
| 399 | |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 400 | while (current != NULL && current->boot_order <= vm->boot_order) { |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 401 | previous = current; |
| 402 | current = current->next_boot; |
| 403 | } |
| 404 | |
| 405 | if (previous != NULL) { |
| 406 | previous->next_boot = vm; |
| 407 | } else { |
| 408 | first_boot_vm = vm; |
| 409 | } |
| 410 | |
| 411 | vm->next_boot = current; |
| 412 | } |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 413 | |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 414 | /** |
| 415 | * Gets the mode of the given range of ipa or va if they are mapped with the |
| 416 | * same mode. |
| 417 | * |
| 418 | * Returns true if the range is mapped with the same mode and false otherwise. |
| 419 | * The wrapper calls the appropriate mm function depending on if the partition |
| 420 | * is a vm or a el0 partition. |
| 421 | */ |
| 422 | bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end, |
| 423 | uint32_t *mode) |
| 424 | { |
| 425 | if (vm_locked.vm->el0_partition) { |
| 426 | return mm_get_mode(&vm_locked.vm->ptable, |
| 427 | va_from_pa(pa_from_ipa(begin)), |
| 428 | va_from_pa(pa_from_ipa(end)), mode); |
| 429 | } |
| 430 | return mm_vm_get_mode(&vm_locked.vm->ptable, begin, end, mode); |
| 431 | } |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 432 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 433 | static struct notifications *vm_get_notifications(struct vm_locked vm_locked, |
| 434 | bool is_from_vm) |
| 435 | { |
| 436 | return is_from_vm ? &vm_locked.vm->notifications.from_vm |
| 437 | : &vm_locked.vm->notifications.from_sp; |
| 438 | } |
| 439 | |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 440 | /* |
| 441 | * Initializes the notifications structure. |
| 442 | */ |
| 443 | void vm_notifications_init_bindings(struct notifications *notifications) |
| 444 | { |
| 445 | for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 446 | notifications->bindings_sender_id[i] = HF_INVALID_VM_ID; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Checks if there are pending notifications. |
| 452 | */ |
| 453 | bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm, |
| 454 | ffa_notifications_bitmap_t notifications) |
| 455 | { |
| 456 | struct notifications *to_check; |
| 457 | |
| 458 | CHECK(vm_locked.vm != NULL); |
| 459 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 460 | to_check = vm_get_notifications(vm_locked, from_vm); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 461 | |
| 462 | /* Check if there are pending per vcpu notifications */ |
| 463 | for (uint32_t i = 0U; i < MAX_CPUS; i++) { |
| 464 | if ((to_check->per_vcpu[i].pending & notifications) != 0U) { |
| 465 | return true; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* Check if there are global pending notifications */ |
| 470 | return (to_check->global.pending & notifications) != 0U; |
| 471 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 472 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 473 | /** |
| 474 | * Checks if there are pending global notifications, either from SPs or from |
| 475 | * VMs. |
| 476 | */ |
| 477 | bool vm_are_global_notifications_pending(struct vm_locked vm_locked) |
| 478 | { |
| 479 | return vm_get_notifications(vm_locked, true)->global.pending != 0ULL || |
J-Alves | 52578f8 | 2022-03-25 12:30:47 +0000 | [diff] [blame] | 480 | vm_get_notifications(vm_locked, false)->global.pending != 0ULL || |
| 481 | vm_locked.vm->notifications.framework.pending != 0ULL; |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Checks if there are pending per-vCPU notifications, in a specific vCPU either |
| 486 | * from SPs or from VMs. |
| 487 | */ |
| 488 | bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked, |
| 489 | ffa_vcpu_index_t vcpu_id) |
| 490 | { |
| 491 | CHECK(vcpu_id < MAX_CPUS); |
| 492 | |
| 493 | return vm_get_notifications(vm_locked, true) |
| 494 | ->per_vcpu[vcpu_id] |
| 495 | .pending != 0ULL || |
| 496 | vm_get_notifications(vm_locked, false) |
| 497 | ->per_vcpu[vcpu_id] |
| 498 | .pending != 0ULL; |
| 499 | } |
| 500 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 501 | bool vm_are_notifications_enabled(struct vm *vm) |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 502 | { |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 503 | return vm->notifications.enabled == true; |
| 504 | } |
| 505 | |
| 506 | bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked) |
| 507 | { |
| 508 | return vm_are_notifications_enabled(vm_locked.vm); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications, |
| 512 | uint32_t i) |
| 513 | { |
| 514 | return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U; |
| 515 | } |
| 516 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 517 | static void vm_notifications_global_state_count_update( |
| 518 | ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc) |
| 519 | { |
| 520 | /* |
| 521 | * Helper to increment counters from global notifications |
| 522 | * state. Count update by increments or decrements of 1 or -1, |
| 523 | * respectively. |
| 524 | */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 525 | assert(inc == 1 || inc == -1); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 526 | |
| 527 | sl_lock(&all_notifications_state.lock); |
| 528 | |
| 529 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 530 | if (vm_is_notification_bit_set(bitmap, i)) { |
| 531 | CHECK((inc > 0 && *counter < UINT32_MAX) || |
| 532 | (inc < 0 && *counter > 0)); |
| 533 | *counter += inc; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | sl_unlock(&all_notifications_state.lock); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Helper function to increment the pending notifications based on a bitmap |
| 542 | * passed as argument. |
| 543 | * Function to be used at setting notifications for a given VM. |
| 544 | */ |
| 545 | static void vm_notifications_pending_count_add( |
| 546 | ffa_notifications_bitmap_t to_add) |
| 547 | { |
| 548 | vm_notifications_global_state_count_update( |
| 549 | to_add, &all_notifications_state.pending_count, 1); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Helper function to decrement the pending notifications count. |
| 554 | * Function to be used when getting the receiver's pending notifications. |
| 555 | */ |
| 556 | static void vm_notifications_pending_count_sub( |
| 557 | ffa_notifications_bitmap_t to_sub) |
| 558 | { |
| 559 | vm_notifications_global_state_count_update( |
| 560 | to_sub, &all_notifications_state.pending_count, -1); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Helper function to count the notifications whose information has been |
| 565 | * retrieved by the scheduler of the system, and are still pending. |
| 566 | */ |
| 567 | static void vm_notifications_info_get_retrieved_count_add( |
| 568 | ffa_notifications_bitmap_t to_add) |
| 569 | { |
| 570 | vm_notifications_global_state_count_update( |
| 571 | to_add, &all_notifications_state.info_get_retrieved_count, 1); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Helper function to subtract the notifications that the receiver is getting |
| 576 | * and whose information has been retrieved by the receiver scheduler. |
| 577 | */ |
| 578 | static void vm_notifications_info_get_retrieved_count_sub( |
| 579 | ffa_notifications_bitmap_t to_sub) |
| 580 | { |
| 581 | vm_notifications_global_state_count_update( |
| 582 | to_sub, &all_notifications_state.info_get_retrieved_count, -1); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Helper function to determine if there are notifications pending whose info |
| 587 | * hasn't been retrieved by the receiver scheduler. |
| 588 | */ |
| 589 | bool vm_notifications_pending_not_retrieved_by_scheduler(void) |
| 590 | { |
| 591 | bool ret; |
| 592 | |
| 593 | sl_lock(&all_notifications_state.lock); |
| 594 | ret = all_notifications_state.pending_count > |
| 595 | all_notifications_state.info_get_retrieved_count; |
| 596 | sl_unlock(&all_notifications_state.lock); |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | bool vm_is_notifications_pending_count_zero(void) |
| 602 | { |
| 603 | bool ret; |
| 604 | |
| 605 | sl_lock(&all_notifications_state.lock); |
| 606 | ret = all_notifications_state.pending_count == 0; |
| 607 | sl_unlock(&all_notifications_state.lock); |
| 608 | |
| 609 | return ret; |
| 610 | } |
| 611 | |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 612 | /** |
| 613 | * Checks that all provided notifications are bound to the specified sender, and |
| 614 | * are per VCPU or global, as specified. |
| 615 | */ |
| 616 | bool vm_notifications_validate_binding(struct vm_locked vm_locked, |
| 617 | bool is_from_vm, ffa_vm_id_t sender_id, |
| 618 | ffa_notifications_bitmap_t notifications, |
| 619 | bool is_per_vcpu) |
| 620 | { |
| 621 | return vm_notifications_validate_bound_sender( |
| 622 | vm_locked, is_from_vm, sender_id, notifications) && |
| 623 | vm_notifications_validate_per_vcpu(vm_locked, is_from_vm, |
| 624 | is_per_vcpu, notifications); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Update binds information in notification structure for the specified |
| 629 | * notifications. |
| 630 | */ |
| 631 | void vm_notifications_update_bindings(struct vm_locked vm_locked, |
| 632 | bool is_from_vm, ffa_vm_id_t sender_id, |
| 633 | ffa_notifications_bitmap_t notifications, |
| 634 | bool is_per_vcpu) |
| 635 | { |
| 636 | CHECK(vm_locked.vm != NULL); |
| 637 | struct notifications *to_update = |
| 638 | vm_get_notifications(vm_locked, is_from_vm); |
| 639 | |
| 640 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 641 | if (vm_is_notification_bit_set(notifications, i)) { |
| 642 | to_update->bindings_sender_id[i] = sender_id; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Set notifications if they are per VCPU, else clear them as they are |
| 648 | * global. |
| 649 | */ |
| 650 | if (is_per_vcpu) { |
| 651 | to_update->bindings_per_vcpu |= notifications; |
| 652 | } else { |
| 653 | to_update->bindings_per_vcpu &= ~notifications; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | bool vm_notifications_validate_bound_sender( |
| 658 | struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id, |
| 659 | ffa_notifications_bitmap_t notifications) |
| 660 | { |
| 661 | CHECK(vm_locked.vm != NULL); |
| 662 | struct notifications *to_check = |
| 663 | vm_get_notifications(vm_locked, is_from_vm); |
| 664 | |
| 665 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 666 | if (vm_is_notification_bit_set(notifications, i) && |
| 667 | to_check->bindings_sender_id[i] != sender_id) { |
| 668 | return false; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked, |
| 676 | bool is_from_vm, bool is_per_vcpu, |
| 677 | ffa_notifications_bitmap_t notif) |
| 678 | { |
| 679 | CHECK(vm_locked.vm != NULL); |
| 680 | struct notifications *to_check = |
| 681 | vm_get_notifications(vm_locked, is_from_vm); |
| 682 | |
| 683 | return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U |
| 684 | : (to_check->bindings_per_vcpu & notif) == 0U; |
| 685 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 686 | |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 687 | void vm_notifications_partition_set_pending( |
| 688 | struct vm_locked vm_locked, bool is_from_vm, |
| 689 | ffa_notifications_bitmap_t notifications, ffa_vcpu_index_t vcpu_id, |
| 690 | bool is_per_vcpu) |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 691 | { |
| 692 | CHECK(vm_locked.vm != NULL); |
| 693 | struct notifications *to_set = |
| 694 | vm_get_notifications(vm_locked, is_from_vm); |
| 695 | CHECK(vcpu_id < MAX_CPUS); |
| 696 | |
| 697 | if (is_per_vcpu) { |
| 698 | to_set->per_vcpu[vcpu_id].pending |= notifications; |
| 699 | } else { |
| 700 | to_set->global.pending |= notifications; |
| 701 | } |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 702 | |
| 703 | /* Update count of notifications pending. */ |
| 704 | vm_notifications_pending_count_add(notifications); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 705 | } |
| 706 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 707 | static ffa_notifications_bitmap_t vm_notifications_state_get_pending( |
| 708 | struct notifications_state *state) |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 709 | { |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 710 | ffa_notifications_bitmap_t to_ret; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 711 | ffa_notifications_bitmap_t pending_and_info_get_retrieved; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 712 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 713 | assert(state != NULL); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 714 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 715 | to_ret = state->pending; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 716 | |
| 717 | /* Update count of currently pending notifications in the system. */ |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 718 | vm_notifications_pending_count_sub(state->pending); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 719 | |
| 720 | /* |
| 721 | * If notifications receiver is getting have been retrieved by the |
| 722 | * receiver scheduler, decrement those from respective count. |
| 723 | */ |
| 724 | pending_and_info_get_retrieved = |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 725 | state->pending & state->info_get_retrieved; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 726 | |
| 727 | if (pending_and_info_get_retrieved != 0) { |
| 728 | vm_notifications_info_get_retrieved_count_sub( |
| 729 | pending_and_info_get_retrieved); |
| 730 | } |
| 731 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 732 | state->pending = 0U; |
| 733 | state->info_get_retrieved = 0U; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 734 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 735 | return to_ret; |
| 736 | } |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 737 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 738 | /** |
| 739 | * Get global and per-vCPU notifications for the given vCPU ID. |
| 740 | */ |
| 741 | ffa_notifications_bitmap_t vm_notifications_partition_get_pending( |
| 742 | struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id) |
| 743 | { |
| 744 | ffa_notifications_bitmap_t to_ret; |
| 745 | struct notifications *to_get; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 746 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 747 | assert(vm_locked.vm != NULL); |
| 748 | to_get = vm_get_notifications(vm_locked, is_from_vm); |
| 749 | assert(vcpu_id < MAX_CPUS); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 750 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 751 | to_ret = vm_notifications_state_get_pending(&to_get->global); |
| 752 | to_ret |= |
| 753 | vm_notifications_state_get_pending(&to_get->per_vcpu[vcpu_id]); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 754 | |
| 755 | return to_ret; |
| 756 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 757 | |
| 758 | /** |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame^] | 759 | * Get pending framework notifications. |
| 760 | */ |
| 761 | ffa_notifications_bitmap_t vm_notifications_framework_get_pending( |
| 762 | struct vm_locked vm_locked) |
| 763 | { |
| 764 | assert(vm_locked.vm != NULL); |
| 765 | return vm_notifications_state_get_pending( |
| 766 | &vm_locked.vm->notifications.framework); |
| 767 | } |
| 768 | |
| 769 | /** |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 770 | * Get pending notification's information to return to the receiver scheduler. |
| 771 | */ |
| 772 | void vm_notifications_info_get_pending( |
| 773 | struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids, |
| 774 | uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count, |
| 775 | const uint32_t ids_max_count, |
| 776 | enum notifications_info_get_state *info_get_state) |
| 777 | { |
| 778 | ffa_notifications_bitmap_t pending_not_retrieved; |
| 779 | |
| 780 | CHECK(vm_locked.vm != NULL); |
| 781 | struct notifications *notifications = |
| 782 | vm_get_notifications(vm_locked, is_from_vm); |
| 783 | |
| 784 | if (*info_get_state == FULL) { |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | CHECK(*ids_count <= ids_max_count); |
| 789 | CHECK(*lists_count <= ids_max_count); |
| 790 | |
| 791 | pending_not_retrieved = notifications->global.pending & |
| 792 | ~notifications->global.info_get_retrieved; |
| 793 | |
| 794 | if (pending_not_retrieved != 0U && *info_get_state == INIT) { |
| 795 | /* |
| 796 | * If action is to INIT, means that no list has been |
| 797 | * created for the given VM ID, which also means that global |
| 798 | * notifications are not represented in the list yet. |
| 799 | */ |
| 800 | if (*ids_count == ids_max_count) { |
| 801 | *info_get_state = FULL; |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | *info_get_state = INSERTING; |
| 806 | |
| 807 | (*lists_count)++; |
| 808 | ids[*ids_count] = vm_locked.vm->id; |
| 809 | ++(*ids_count); |
| 810 | } |
| 811 | |
| 812 | notifications->global.info_get_retrieved |= pending_not_retrieved; |
| 813 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 814 | vm_notifications_info_get_retrieved_count_add(pending_not_retrieved); |
| 815 | |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 816 | for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) { |
| 817 | /* |
| 818 | * Include VCPU ID of per-VCPU notifications. |
| 819 | */ |
| 820 | pending_not_retrieved = |
| 821 | notifications->per_vcpu[i].pending & |
| 822 | ~notifications->per_vcpu[i].info_get_retrieved; |
| 823 | |
| 824 | if (pending_not_retrieved == 0U) { |
| 825 | continue; |
| 826 | } |
| 827 | |
| 828 | switch (*info_get_state) { |
| 829 | case INIT: |
| 830 | case STARTING_NEW: |
| 831 | /* |
| 832 | * At this iteration two ids need to be added: the VM ID |
| 833 | * and VCPU ID. If there is not space, change state and |
| 834 | * terminate function. |
| 835 | */ |
| 836 | if (ids_max_count - *ids_count < 2) { |
| 837 | *info_get_state = FULL; |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | ids[*ids_count] = vm_locked.vm->id; |
| 842 | ++(*ids_count); |
| 843 | |
| 844 | /* Insert VCPU ID */ |
| 845 | ids[*ids_count] = i; |
| 846 | ++(*ids_count); |
| 847 | |
| 848 | ++lists_sizes[*lists_count]; |
| 849 | ++(*lists_count); |
| 850 | |
| 851 | *info_get_state = INSERTING; |
| 852 | break; |
| 853 | case INSERTING: |
| 854 | if (*ids_count == ids_max_count) { |
| 855 | *info_get_state = FULL; |
| 856 | return; |
| 857 | } |
| 858 | |
| 859 | /* Insert VCPU ID */ |
| 860 | ids[*ids_count] = i; |
| 861 | (*ids_count)++; |
| 862 | |
| 863 | /* Increment respective list size */ |
| 864 | ++lists_sizes[*lists_count - 1]; |
| 865 | |
| 866 | if (lists_sizes[*lists_count - 1] == 3) { |
| 867 | *info_get_state = STARTING_NEW; |
| 868 | } |
| 869 | break; |
| 870 | default: |
| 871 | panic("Notification info get action error!!\n"); |
| 872 | } |
| 873 | |
| 874 | notifications->per_vcpu[i].info_get_retrieved |= |
| 875 | pending_not_retrieved; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 876 | |
| 877 | vm_notifications_info_get_retrieved_count_add( |
| 878 | pending_not_retrieved); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Gets all info from VM's pending notifications. |
| 884 | * Returns true if the list is full, and there is more pending. |
| 885 | */ |
| 886 | bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids, |
| 887 | uint32_t *ids_count, uint32_t *lists_sizes, |
| 888 | uint32_t *lists_count, |
| 889 | const uint32_t ids_max_count) |
| 890 | { |
| 891 | enum notifications_info_get_state current_state = INIT; |
| 892 | |
| 893 | /* Get info of pending notifications from SPs */ |
| 894 | vm_notifications_info_get_pending(vm_locked, false, ids, ids_count, |
| 895 | lists_sizes, lists_count, |
| 896 | ids_max_count, ¤t_state); |
| 897 | |
| 898 | /* Get info of pending notifications from VMs */ |
| 899 | vm_notifications_info_get_pending(vm_locked, true, ids, ids_count, |
| 900 | lists_sizes, lists_count, |
| 901 | ids_max_count, ¤t_state); |
| 902 | |
| 903 | /* |
| 904 | * State transitions to FULL when trying to insert a new ID in the |
| 905 | * list and there is not more space. This means there are notifications |
| 906 | * pending, whose info is not retrieved. |
| 907 | */ |
| 908 | return current_state == FULL; |
| 909 | } |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 910 | |
| 911 | /** |
| 912 | * Checks VM's messaging method support. |
| 913 | */ |
| 914 | bool vm_supports_messaging_method(struct vm *vm, uint8_t msg_method) |
| 915 | { |
| 916 | return (vm->messaging_method & msg_method) != 0; |
| 917 | } |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 918 | |
| 919 | void vm_notifications_set_npi_injected(struct vm_locked vm_locked, |
| 920 | bool npi_injected) |
| 921 | { |
| 922 | vm_locked.vm->notifications.npi_injected = npi_injected; |
| 923 | } |
| 924 | |
| 925 | bool vm_notifications_is_npi_injected(struct vm_locked vm_locked) |
| 926 | { |
| 927 | return vm_locked.vm->notifications.npi_injected; |
| 928 | } |
J-Alves | 7e67d10 | 2022-04-13 13:22:39 +0100 | [diff] [blame] | 929 | |
| 930 | /** |
| 931 | * Sets the designated GP register that the VM expects to receive the boot |
| 932 | * info's address. |
| 933 | */ |
| 934 | void vm_set_boot_info_gp_reg(struct vm *vm, struct vcpu *vcpu) |
| 935 | { |
| 936 | if (!vm->initialized && vm->boot_info.blob_addr.ipa != 0U) { |
| 937 | arch_regs_set_gp_reg(&vcpu->regs, |
| 938 | ipa_addr(vm->boot_info.blob_addr), |
| 939 | vm->boot_info.gp_register_num); |
| 940 | } |
| 941 | } |