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 | |
Karl Meakin | e143080 | 2024-03-06 14:08:11 +0000 | [diff] [blame] | 11 | #include "hf/arch/spinlock.h" |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 12 | #include "hf/arch/vm.h" |
| 13 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 14 | #include "hf/api.h" |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 15 | #include "hf/assert.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 16 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/cpu.h" |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 18 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 19 | #include "hf/ffa.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 20 | #include "hf/layout.h" |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 21 | #include "hf/plat/iommu.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 22 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 23 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 24 | #include "vmapi/hf/call.h" |
| 25 | |
| 26 | static struct vm vms[MAX_VMS]; |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 27 | static struct vm other_world; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 28 | static ffa_vm_count_t vm_count; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 29 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 30 | /** |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 31 | * The `boot_list` is a special entry in the circular linked list maintained by |
| 32 | * the partition manager and serves as both the start and end of the list. |
| 33 | */ |
| 34 | static struct list_entry boot_list = LIST_INIT(boot_list); |
| 35 | |
| 36 | /** |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 37 | * Counters on the status of notifications in the system. It helps to improve |
| 38 | * the information retrieved by the receiver scheduler. |
| 39 | */ |
| 40 | static struct { |
| 41 | /** Counts notifications pending. */ |
| 42 | uint32_t pending_count; |
| 43 | /** |
| 44 | * Counts notifications pending, that have been retrieved by the |
| 45 | * receiver scheduler. |
| 46 | */ |
| 47 | uint32_t info_get_retrieved_count; |
| 48 | struct spinlock lock; |
| 49 | } all_notifications_state; |
| 50 | |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 51 | static bool vm_init_mm(struct vm *vm, struct mpool *ppool) |
| 52 | { |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 53 | return arch_vm_init_mm(vm, ppool) && arch_vm_iommu_init_mm(vm, ppool); |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 54 | } |
| 55 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 56 | struct vm *vm_init(ffa_id_t id, ffa_vcpu_count_t vcpu_count, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 57 | struct mpool *ppool, bool el0_partition, |
| 58 | uint8_t dma_device_count) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 59 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 60 | uint32_t i; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 61 | struct vm *vm; |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 62 | size_t vcpu_ppool_entries = (align_up(sizeof(struct vcpu) * vcpu_count, |
| 63 | MM_PPOOL_ENTRY_SIZE) / |
| 64 | MM_PPOOL_ENTRY_SIZE); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 65 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 66 | if (id == HF_OTHER_WORLD_ID) { |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 67 | CHECK(el0_partition == false); |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 68 | vm = &other_world; |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 69 | } else { |
| 70 | uint16_t vm_index = id - HF_VM_ID_OFFSET; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 71 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 72 | CHECK(id >= HF_VM_ID_OFFSET); |
| 73 | CHECK(vm_index < ARRAY_SIZE(vms)); |
| 74 | vm = &vms[vm_index]; |
| 75 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 76 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 77 | memset_s(vm, sizeof(*vm), 0, sizeof(*vm)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 78 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 79 | sl_init(&vm->lock); |
| 80 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 81 | vm->id = id; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 82 | vm->vcpu_count = vcpu_count; |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 83 | |
| 84 | vm->vcpus = (struct vcpu *)mpool_alloc_contiguous( |
| 85 | ppool, vcpu_ppool_entries, 1); |
| 86 | CHECK(vm->vcpus != NULL); |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 87 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 88 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 89 | atomic_init(&vm->aborting, false); |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 90 | vm->el0_partition = el0_partition; |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 91 | vm->dma_device_count = dma_device_count; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 92 | |
Raghu Krishnamurthy | ec1b491 | 2021-02-10 19:09:06 -0800 | [diff] [blame] | 93 | if (!vm_init_mm(vm, ppool)) { |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 94 | return NULL; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 97 | /* Do basic initialization of vCPUs. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 98 | for (i = 0; i < vcpu_count; i++) { |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 99 | vcpu_init(vm_get_vcpu(vm, i), vm); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 100 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 101 | |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 102 | vm_notifications_init(vm, vcpu_count, ppool); |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 103 | list_init(&vm->boot_list_node); |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 104 | return vm; |
| 105 | } |
| 106 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 107 | bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 108 | struct vm **new_vm, bool el0_partition, |
| 109 | uint8_t dma_device_count) |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 110 | { |
| 111 | if (vm_count >= MAX_VMS) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /* 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] | 116 | *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 117 | el0_partition, dma_device_count); |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 118 | if (*new_vm == NULL) { |
| 119 | return false; |
| 120 | } |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 121 | ++vm_count; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 122 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 123 | return true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 126 | ffa_vm_count_t vm_get_count(void) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 127 | { |
| 128 | return vm_count; |
| 129 | } |
| 130 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 131 | /** |
| 132 | * Returns a pointer to the VM with the corresponding id. |
| 133 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 134 | struct vm *vm_find(ffa_id_t id) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 135 | { |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 136 | uint16_t index; |
Fuad Tabba | 494376e | 2019-08-05 12:35:10 +0100 | [diff] [blame] | 137 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 138 | if (id == HF_OTHER_WORLD_ID) { |
| 139 | if (other_world.id == HF_OTHER_WORLD_ID) { |
| 140 | return &other_world; |
| 141 | } |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
| 144 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 145 | /* Check that this is not a reserved ID. */ |
| 146 | if (id < HF_VM_ID_OFFSET) { |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 147 | return NULL; |
| 148 | } |
| 149 | |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 150 | index = id - HF_VM_ID_OFFSET; |
| 151 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 152 | return vm_find_index(index); |
| 153 | } |
| 154 | |
| 155 | /** |
J-Alves | 46ee068 | 2021-07-26 15:17:53 +0100 | [diff] [blame] | 156 | * Returns a locked instance of the VM with the corresponding id. |
| 157 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 158 | struct vm_locked vm_find_locked(ffa_id_t id) |
J-Alves | 46ee068 | 2021-07-26 15:17:53 +0100 | [diff] [blame] | 159 | { |
| 160 | struct vm *vm = vm_find(id); |
| 161 | |
| 162 | if (vm != NULL) { |
| 163 | return vm_lock(vm); |
| 164 | } |
| 165 | |
| 166 | return (struct vm_locked){.vm = NULL}; |
| 167 | } |
| 168 | |
| 169 | /** |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 170 | * Returns a pointer to the VM at the specified index. |
| 171 | */ |
| 172 | struct vm *vm_find_index(uint16_t index) |
| 173 | { |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 174 | /* Ensure the VM is initialized. */ |
| 175 | if (index >= vm_count) { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | return &vms[index]; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 182 | /** |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 183 | * 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] | 184 | */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 185 | struct vm_locked vm_lock(struct vm *vm) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 186 | { |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 187 | struct vm_locked locked = { |
| 188 | .vm = vm, |
| 189 | }; |
| 190 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 191 | sl_lock(&vm->lock); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 192 | |
| 193 | return locked; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 197 | * Locks two VMs ensuring that the locking order is according to the locks' |
| 198 | * addresses. |
| 199 | */ |
| 200 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2) |
| 201 | { |
| 202 | struct two_vm_locked dual_lock; |
| 203 | |
| 204 | sl_lock_both(&vm1->lock, &vm2->lock); |
| 205 | dual_lock.vm1.vm = vm1; |
| 206 | dual_lock.vm2.vm = vm2; |
| 207 | |
| 208 | return dual_lock; |
| 209 | } |
| 210 | |
| 211 | /** |
Karl Meakin | e143080 | 2024-03-06 14:08:11 +0000 | [diff] [blame] | 212 | * Locks two VMs ensuring that the locking order is according to the locks' |
| 213 | * addresses, given `vm1` is already locked. |
| 214 | */ |
| 215 | struct two_vm_locked vm_lock_both_in_order(struct vm_locked vm1, struct vm *vm2) |
| 216 | { |
| 217 | struct spinlock *sl1 = &vm1.vm->lock; |
| 218 | struct spinlock *sl2 = &vm2->lock; |
| 219 | |
| 220 | /* |
| 221 | * Use `sl_lock`/`sl_unlock` directly rather than |
| 222 | * `vm_lock`/`vm_unlock` because `vm_unlock` sets the vm field |
| 223 | * to NULL. |
| 224 | */ |
| 225 | if (sl1 < sl2) { |
| 226 | sl_lock(sl2); |
| 227 | } else { |
| 228 | sl_unlock(sl1); |
| 229 | sl_lock(sl2); |
| 230 | sl_lock(sl1); |
| 231 | } |
| 232 | |
| 233 | return (struct two_vm_locked){ |
| 234 | .vm1 = vm1, |
| 235 | .vm2 = (struct vm_locked){.vm = vm2}, |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 240 | * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect |
| 241 | * the fact that the VM is no longer locked. |
| 242 | */ |
| 243 | void vm_unlock(struct vm_locked *locked) |
| 244 | { |
| 245 | sl_unlock(&locked->vm->lock); |
| 246 | locked->vm = NULL; |
| 247 | } |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Get the vCPU with the given index from the given VM. |
| 251 | * This assumes the index is valid, i.e. less than vm->vcpu_count. |
| 252 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 253 | 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] | 254 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 255 | CHECK(vcpu_index < vm->vcpu_count); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 256 | return &vm->vcpus[vcpu_index]; |
| 257 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 258 | |
| 259 | /** |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 260 | * Checks whether the given `to` VM's mailbox is currently busy. |
| 261 | */ |
| 262 | bool vm_is_mailbox_busy(struct vm_locked to) |
| 263 | { |
| 264 | return to.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 265 | to.vm->mailbox.recv == NULL; |
| 266 | } |
| 267 | |
| 268 | /** |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 269 | * Checks if mailbox is currently owned by the other world. |
| 270 | */ |
| 271 | bool vm_is_mailbox_other_world_owned(struct vm_locked to) |
| 272 | { |
| 273 | return to.vm->mailbox.state == MAILBOX_STATE_OTHER_WORLD_OWNED; |
| 274 | } |
| 275 | |
| 276 | /** |
Andrew Walbran | 45633dd | 2020-10-07 17:59:54 +0100 | [diff] [blame] | 277 | * Return whether the given VM ID represents an entity in the current world: |
| 278 | * i.e. the hypervisor or a normal world VM when running in the normal world, or |
| 279 | * the SPM or an SP when running in the secure world. |
| 280 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 281 | bool vm_id_is_current_world(ffa_id_t vm_id) |
Andrew Walbran | 45633dd | 2020-10-07 17:59:54 +0100 | [diff] [blame] | 282 | { |
| 283 | return (vm_id & HF_VM_ID_WORLD_MASK) != |
| 284 | (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK); |
| 285 | } |
| 286 | |
| 287 | /** |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 288 | * Map a range of addresses to the VM in both the MMU and the IOMMU. |
| 289 | * |
| 290 | * mm_vm_defrag should always be called after a series of page table updates, |
| 291 | * whether they succeed or fail. This is because on failure extra page table |
| 292 | * entries may have been allocated and then not used, while on success it may be |
| 293 | * possible to compact the page table by merging several entries into a block. |
| 294 | * |
| 295 | * Returns true on success, or false if the update failed and no changes were |
| 296 | * made. |
| 297 | * |
| 298 | */ |
| 299 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame^] | 300 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 301 | { |
| 302 | if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa); |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Prepares the given VM for the given address mapping such that it will be able |
| 313 | * to commit the change without failure. |
| 314 | * |
| 315 | * In particular, multiple calls to this function will result in the |
| 316 | * corresponding calls to commit the changes to succeed. |
| 317 | * |
| 318 | * Returns true on success, or false if the update failed and no changes were |
| 319 | * made. |
| 320 | */ |
| 321 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame^] | 322 | mm_mode_t mode, struct mpool *ppool) |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 323 | { |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 324 | return arch_vm_identity_prepare(vm_locked, begin, end, mode, ppool); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Commits the given address mapping to the VM assuming the operation cannot |
| 329 | * fail. `vm_identity_prepare` must used correctly before this to ensure |
| 330 | * this condition. |
| 331 | */ |
| 332 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame^] | 333 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 334 | { |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 335 | arch_vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Unmap a range of addresses from the VM. |
| 340 | * |
| 341 | * Returns true on success, or false if the update failed and no changes were |
| 342 | * made. |
| 343 | */ |
| 344 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 345 | struct mpool *ppool) |
| 346 | { |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 347 | return arch_vm_unmap(vm_locked, begin, end, ppool); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 351 | * Defrag page tables for an EL0 partition or for a VM. |
| 352 | */ |
| 353 | void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool) |
| 354 | { |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 355 | arch_vm_ptable_defrag(vm_locked, ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 359 | * Unmaps the hypervisor pages from the given page table. |
| 360 | */ |
| 361 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool) |
| 362 | { |
| 363 | /* TODO: If we add pages dynamically, they must be included here too. */ |
| 364 | return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(), |
| 365 | ppool) && |
| 366 | vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(), |
| 367 | ppool) && |
| 368 | vm_unmap(vm_locked, layout_data_begin(), layout_data_end(), |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 369 | ppool) && |
| 370 | vm_unmap(vm_locked, layout_stacks_begin(), layout_stacks_end(), |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 371 | ppool); |
| 372 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 373 | |
| 374 | /** |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 375 | * Gets the mode of the given range of ipa or va if they are mapped with the |
| 376 | * same mode. |
| 377 | * |
| 378 | * Returns true if the range is mapped with the same mode and false otherwise. |
| 379 | * The wrapper calls the appropriate mm function depending on if the partition |
| 380 | * is a vm or a el0 partition. |
| 381 | */ |
| 382 | bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame^] | 383 | mm_mode_t *mode) |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 384 | { |
Olivier Deprez | d9d409f | 2023-03-17 11:47:57 +0100 | [diff] [blame] | 385 | return arch_vm_mem_get_mode(vm_locked, begin, end, mode); |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 386 | } |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 387 | |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 388 | bool vm_iommu_mm_identity_map(struct vm_locked vm_locked, paddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame^] | 389 | paddr_t end, mm_mode_t mode, struct mpool *ppool, |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 390 | ipaddr_t *ipa, uint8_t dma_device_id) |
| 391 | { |
| 392 | return arch_vm_iommu_mm_identity_map(vm_locked, begin, end, mode, ppool, |
| 393 | ipa, dma_device_id); |
| 394 | } |
| 395 | |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 396 | bool vm_mailbox_state_busy(struct vm_locked vm_locked) |
| 397 | { |
| 398 | return vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 399 | vm_locked.vm->mailbox.recv == NULL; |
| 400 | } |
| 401 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 402 | static struct notifications *vm_get_notifications(struct vm_locked vm_locked, |
| 403 | bool is_from_vm) |
| 404 | { |
| 405 | return is_from_vm ? &vm_locked.vm->notifications.from_vm |
| 406 | : &vm_locked.vm->notifications.from_sp; |
| 407 | } |
| 408 | |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 409 | /* |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 410 | * Dynamically allocate per_vcpu_notifications structure for a given VM. |
| 411 | */ |
| 412 | static void vm_notifications_init_per_vcpu_notifications( |
| 413 | struct vm *vm, ffa_vcpu_count_t vcpu_count, struct mpool *ppool) |
| 414 | { |
| 415 | size_t notif_ppool_entries = |
| 416 | (align_up(sizeof(struct notifications_state) * vcpu_count, |
| 417 | MM_PPOOL_ENTRY_SIZE) / |
| 418 | MM_PPOOL_ENTRY_SIZE); |
| 419 | |
| 420 | /* |
| 421 | * Allow for function to be called on already initialized VMs but those |
| 422 | * that require notification structure to be cleared. |
| 423 | */ |
| 424 | if (vm->notifications.from_sp.per_vcpu == NULL) { |
| 425 | assert(vm->notifications.from_vm.per_vcpu == NULL); |
| 426 | assert(vcpu_count != 0); |
| 427 | CHECK(ppool != NULL); |
| 428 | vm->notifications.from_sp.per_vcpu = |
| 429 | (struct notifications_state *)mpool_alloc_contiguous( |
| 430 | ppool, notif_ppool_entries, 1); |
| 431 | CHECK(vm->notifications.from_sp.per_vcpu != NULL); |
| 432 | |
| 433 | vm->notifications.from_vm.per_vcpu = |
| 434 | (struct notifications_state *)mpool_alloc_contiguous( |
| 435 | ppool, notif_ppool_entries, 1); |
| 436 | CHECK(vm->notifications.from_vm.per_vcpu != NULL); |
| 437 | } else { |
| 438 | assert(vm->notifications.from_vm.per_vcpu != NULL); |
| 439 | } |
| 440 | |
| 441 | memset_s(vm->notifications.from_sp.per_vcpu, |
| 442 | sizeof(*(vm->notifications.from_sp.per_vcpu)) * vcpu_count, 0, |
| 443 | sizeof(*(vm->notifications.from_sp.per_vcpu)) * vcpu_count); |
| 444 | memset_s(vm->notifications.from_vm.per_vcpu, |
| 445 | sizeof(*(vm->notifications.from_vm.per_vcpu)) * vcpu_count, 0, |
| 446 | sizeof(*(vm->notifications.from_vm.per_vcpu)) * vcpu_count); |
| 447 | } |
| 448 | |
| 449 | /* |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 450 | * Initializes the notifications structure. |
| 451 | */ |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 452 | static void vm_notifications_init_bindings(struct notifications *notifications) |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 453 | { |
| 454 | for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 455 | notifications->bindings_sender_id[i] = HF_INVALID_VM_ID; |
| 456 | } |
| 457 | } |
| 458 | |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 459 | /* |
| 460 | * Initialize notification related structures for a VM. |
| 461 | */ |
| 462 | void vm_notifications_init(struct vm *vm, ffa_vcpu_count_t vcpu_count, |
| 463 | struct mpool *ppool) |
| 464 | { |
| 465 | vm_notifications_init_per_vcpu_notifications(vm, vcpu_count, ppool); |
| 466 | |
| 467 | /* Basic initialization of the notifications structure. */ |
| 468 | vm_notifications_init_bindings(&vm->notifications.from_sp); |
| 469 | vm_notifications_init_bindings(&vm->notifications.from_vm); |
| 470 | } |
| 471 | |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 472 | /** |
| 473 | * Checks if there are pending notifications. |
| 474 | */ |
| 475 | bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm, |
| 476 | ffa_notifications_bitmap_t notifications) |
| 477 | { |
| 478 | struct notifications *to_check; |
| 479 | |
| 480 | CHECK(vm_locked.vm != NULL); |
| 481 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 482 | to_check = vm_get_notifications(vm_locked, from_vm); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 483 | |
| 484 | /* Check if there are pending per vcpu notifications */ |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 485 | for (uint32_t i = 0U; i < vm_locked.vm->vcpu_count; i++) { |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 486 | if ((to_check->per_vcpu[i].pending & notifications) != 0U) { |
| 487 | return true; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* Check if there are global pending notifications */ |
| 492 | return (to_check->global.pending & notifications) != 0U; |
| 493 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 494 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 495 | /** |
| 496 | * Checks if there are pending global notifications, either from SPs or from |
| 497 | * VMs. |
| 498 | */ |
| 499 | bool vm_are_global_notifications_pending(struct vm_locked vm_locked) |
| 500 | { |
| 501 | return vm_get_notifications(vm_locked, true)->global.pending != 0ULL || |
J-Alves | 52578f8 | 2022-03-25 12:30:47 +0000 | [diff] [blame] | 502 | vm_get_notifications(vm_locked, false)->global.pending != 0ULL || |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 503 | vm_are_fwk_notifications_pending(vm_locked); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Currently only RX full notification is supported as framework notification. |
| 508 | * Returns true if there is one pending, either from Hypervisor or SPMC. |
| 509 | */ |
| 510 | bool vm_are_fwk_notifications_pending(struct vm_locked vm_locked) |
| 511 | { |
| 512 | return vm_locked.vm->notifications.framework.pending != 0ULL; |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Checks if there are pending per-vCPU notifications, in a specific vCPU either |
| 517 | * from SPs or from VMs. |
| 518 | */ |
| 519 | bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked, |
| 520 | ffa_vcpu_index_t vcpu_id) |
| 521 | { |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 522 | CHECK(vcpu_id < vm_locked.vm->vcpu_count); |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 523 | |
| 524 | return vm_get_notifications(vm_locked, true) |
| 525 | ->per_vcpu[vcpu_id] |
| 526 | .pending != 0ULL || |
| 527 | vm_get_notifications(vm_locked, false) |
| 528 | ->per_vcpu[vcpu_id] |
| 529 | .pending != 0ULL; |
| 530 | } |
| 531 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 532 | bool vm_are_notifications_enabled(struct vm *vm) |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 533 | { |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 534 | return vm->notifications.enabled == true; |
| 535 | } |
| 536 | |
| 537 | bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked) |
| 538 | { |
| 539 | return vm_are_notifications_enabled(vm_locked.vm); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications, |
| 543 | uint32_t i) |
| 544 | { |
| 545 | return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U; |
| 546 | } |
| 547 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 548 | static void vm_notifications_global_state_count_update( |
| 549 | ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc) |
| 550 | { |
| 551 | /* |
| 552 | * Helper to increment counters from global notifications |
| 553 | * state. Count update by increments or decrements of 1 or -1, |
| 554 | * respectively. |
| 555 | */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 556 | assert(inc == 1 || inc == -1); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 557 | |
| 558 | sl_lock(&all_notifications_state.lock); |
| 559 | |
| 560 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 561 | if (vm_is_notification_bit_set(bitmap, i)) { |
| 562 | CHECK((inc > 0 && *counter < UINT32_MAX) || |
| 563 | (inc < 0 && *counter > 0)); |
| 564 | *counter += inc; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | sl_unlock(&all_notifications_state.lock); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Helper function to increment the pending notifications based on a bitmap |
| 573 | * passed as argument. |
| 574 | * Function to be used at setting notifications for a given VM. |
| 575 | */ |
| 576 | static void vm_notifications_pending_count_add( |
| 577 | ffa_notifications_bitmap_t to_add) |
| 578 | { |
| 579 | vm_notifications_global_state_count_update( |
| 580 | to_add, &all_notifications_state.pending_count, 1); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Helper function to decrement the pending notifications count. |
| 585 | * Function to be used when getting the receiver's pending notifications. |
| 586 | */ |
| 587 | static void vm_notifications_pending_count_sub( |
| 588 | ffa_notifications_bitmap_t to_sub) |
| 589 | { |
| 590 | vm_notifications_global_state_count_update( |
| 591 | to_sub, &all_notifications_state.pending_count, -1); |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Helper function to count the notifications whose information has been |
| 596 | * retrieved by the scheduler of the system, and are still pending. |
| 597 | */ |
| 598 | static void vm_notifications_info_get_retrieved_count_add( |
| 599 | ffa_notifications_bitmap_t to_add) |
| 600 | { |
| 601 | vm_notifications_global_state_count_update( |
| 602 | to_add, &all_notifications_state.info_get_retrieved_count, 1); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Helper function to subtract the notifications that the receiver is getting |
| 607 | * and whose information has been retrieved by the receiver scheduler. |
| 608 | */ |
| 609 | static void vm_notifications_info_get_retrieved_count_sub( |
| 610 | ffa_notifications_bitmap_t to_sub) |
| 611 | { |
| 612 | vm_notifications_global_state_count_update( |
| 613 | to_sub, &all_notifications_state.info_get_retrieved_count, -1); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Helper function to determine if there are notifications pending whose info |
| 618 | * hasn't been retrieved by the receiver scheduler. |
| 619 | */ |
| 620 | bool vm_notifications_pending_not_retrieved_by_scheduler(void) |
| 621 | { |
| 622 | bool ret; |
| 623 | |
| 624 | sl_lock(&all_notifications_state.lock); |
| 625 | ret = all_notifications_state.pending_count > |
| 626 | all_notifications_state.info_get_retrieved_count; |
| 627 | sl_unlock(&all_notifications_state.lock); |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | bool vm_is_notifications_pending_count_zero(void) |
| 633 | { |
| 634 | bool ret; |
| 635 | |
| 636 | sl_lock(&all_notifications_state.lock); |
| 637 | ret = all_notifications_state.pending_count == 0; |
| 638 | sl_unlock(&all_notifications_state.lock); |
| 639 | |
| 640 | return ret; |
| 641 | } |
| 642 | |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 643 | /** |
| 644 | * Checks that all provided notifications are bound to the specified sender, and |
| 645 | * are per VCPU or global, as specified. |
| 646 | */ |
| 647 | bool vm_notifications_validate_binding(struct vm_locked vm_locked, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 648 | bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 649 | ffa_notifications_bitmap_t notifications, |
| 650 | bool is_per_vcpu) |
| 651 | { |
| 652 | return vm_notifications_validate_bound_sender( |
| 653 | vm_locked, is_from_vm, sender_id, notifications) && |
| 654 | vm_notifications_validate_per_vcpu(vm_locked, is_from_vm, |
| 655 | is_per_vcpu, notifications); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Update binds information in notification structure for the specified |
| 660 | * notifications. |
| 661 | */ |
| 662 | void vm_notifications_update_bindings(struct vm_locked vm_locked, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 663 | bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 664 | ffa_notifications_bitmap_t notifications, |
| 665 | bool is_per_vcpu) |
| 666 | { |
| 667 | CHECK(vm_locked.vm != NULL); |
| 668 | struct notifications *to_update = |
| 669 | vm_get_notifications(vm_locked, is_from_vm); |
| 670 | |
| 671 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 672 | if (vm_is_notification_bit_set(notifications, i)) { |
| 673 | to_update->bindings_sender_id[i] = sender_id; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Set notifications if they are per VCPU, else clear them as they are |
| 679 | * global. |
| 680 | */ |
| 681 | if (is_per_vcpu) { |
| 682 | to_update->bindings_per_vcpu |= notifications; |
| 683 | } else { |
| 684 | to_update->bindings_per_vcpu &= ~notifications; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | bool vm_notifications_validate_bound_sender( |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 689 | struct vm_locked vm_locked, bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 690 | ffa_notifications_bitmap_t notifications) |
| 691 | { |
| 692 | CHECK(vm_locked.vm != NULL); |
| 693 | struct notifications *to_check = |
| 694 | vm_get_notifications(vm_locked, is_from_vm); |
| 695 | |
| 696 | for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) { |
| 697 | if (vm_is_notification_bit_set(notifications, i) && |
| 698 | to_check->bindings_sender_id[i] != sender_id) { |
| 699 | return false; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked, |
| 707 | bool is_from_vm, bool is_per_vcpu, |
| 708 | ffa_notifications_bitmap_t notif) |
| 709 | { |
| 710 | CHECK(vm_locked.vm != NULL); |
| 711 | struct notifications *to_check = |
| 712 | vm_get_notifications(vm_locked, is_from_vm); |
| 713 | |
| 714 | return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U |
| 715 | : (to_check->bindings_per_vcpu & notif) == 0U; |
| 716 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 717 | |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 718 | static void vm_notifications_state_set(struct notifications_state *state, |
| 719 | ffa_notifications_bitmap_t notifications) |
| 720 | { |
J-Alves | fc50ef7 | 2025-02-03 11:57:51 +0000 | [diff] [blame] | 721 | /* |
| 722 | * Exclude notifications which are already pending, to avoid |
| 723 | * leaving the pending counter in a wrongful state. |
| 724 | */ |
| 725 | ffa_notifications_bitmap_t to_set = |
| 726 | (state->pending & notifications) ^ notifications; |
| 727 | |
| 728 | /* Change the state of the pending notifications. */ |
| 729 | state->pending |= to_set; |
| 730 | vm_notifications_pending_count_add(to_set); |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 731 | } |
| 732 | |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 733 | void vm_notifications_partition_set_pending( |
| 734 | struct vm_locked vm_locked, bool is_from_vm, |
| 735 | ffa_notifications_bitmap_t notifications, ffa_vcpu_index_t vcpu_id, |
| 736 | bool is_per_vcpu) |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 737 | { |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 738 | struct notifications *to_set; |
| 739 | struct notifications_state *state; |
| 740 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 741 | CHECK(vm_locked.vm != NULL); |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 742 | CHECK(vcpu_id < vm_locked.vm->vcpu_count); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 743 | |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 744 | to_set = vm_get_notifications(vm_locked, is_from_vm); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 745 | |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 746 | state = is_per_vcpu ? &to_set->per_vcpu[vcpu_id] : &to_set->global; |
| 747 | |
| 748 | vm_notifications_state_set(state, notifications); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Set pending framework notifications. |
| 753 | */ |
| 754 | void vm_notifications_framework_set_pending( |
| 755 | struct vm_locked vm_locked, ffa_notifications_bitmap_t notifications) |
| 756 | { |
| 757 | CHECK(vm_locked.vm != NULL); |
Federico Recanati | e73d283 | 2022-04-20 11:10:52 +0200 | [diff] [blame] | 758 | assert(is_ffa_spm_buffer_full_notification(notifications) || |
| 759 | is_ffa_hyp_buffer_full_notification(notifications)); |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 760 | vm_notifications_state_set(&vm_locked.vm->notifications.framework, |
| 761 | notifications); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 762 | } |
| 763 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 764 | static ffa_notifications_bitmap_t vm_notifications_state_get_pending( |
| 765 | struct notifications_state *state) |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 766 | { |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 767 | ffa_notifications_bitmap_t to_ret; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 768 | ffa_notifications_bitmap_t pending_and_info_get_retrieved; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 769 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 770 | assert(state != NULL); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 771 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 772 | to_ret = state->pending; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 773 | |
| 774 | /* Update count of currently pending notifications in the system. */ |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 775 | vm_notifications_pending_count_sub(state->pending); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * If notifications receiver is getting have been retrieved by the |
| 779 | * receiver scheduler, decrement those from respective count. |
| 780 | */ |
| 781 | pending_and_info_get_retrieved = |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 782 | state->pending & state->info_get_retrieved; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 783 | |
| 784 | if (pending_and_info_get_retrieved != 0) { |
| 785 | vm_notifications_info_get_retrieved_count_sub( |
| 786 | pending_and_info_get_retrieved); |
| 787 | } |
| 788 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 789 | state->pending = 0U; |
| 790 | state->info_get_retrieved = 0U; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 791 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 792 | return to_ret; |
| 793 | } |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 794 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 795 | /** |
| 796 | * Get global and per-vCPU notifications for the given vCPU ID. |
| 797 | */ |
| 798 | ffa_notifications_bitmap_t vm_notifications_partition_get_pending( |
| 799 | struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id) |
| 800 | { |
| 801 | ffa_notifications_bitmap_t to_ret; |
| 802 | struct notifications *to_get; |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 803 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 804 | assert(vm_locked.vm != NULL); |
| 805 | to_get = vm_get_notifications(vm_locked, is_from_vm); |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 806 | assert(vcpu_id < vm_locked.vm->vcpu_count); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 807 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 808 | to_ret = vm_notifications_state_get_pending(&to_get->global); |
| 809 | to_ret |= |
| 810 | vm_notifications_state_get_pending(&to_get->per_vcpu[vcpu_id]); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 811 | |
| 812 | return to_ret; |
| 813 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 814 | |
| 815 | /** |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 816 | * Get pending framework notifications. |
| 817 | */ |
| 818 | ffa_notifications_bitmap_t vm_notifications_framework_get_pending( |
| 819 | struct vm_locked vm_locked) |
| 820 | { |
Federico Recanati | 6c1e05c | 2022-04-20 11:37:26 +0200 | [diff] [blame] | 821 | struct vm *vm = vm_locked.vm; |
| 822 | ffa_notifications_bitmap_t framework; |
Federico Recanati | 6c1e05c | 2022-04-20 11:37:26 +0200 | [diff] [blame] | 823 | |
| 824 | assert(vm != NULL); |
| 825 | |
| 826 | framework = vm_notifications_state_get_pending( |
| 827 | &vm->notifications.framework); |
| 828 | |
Federico Recanati | 6c1e05c | 2022-04-20 11:37:26 +0200 | [diff] [blame] | 829 | return framework; |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 832 | static bool vm_insert_notification_info_list( |
| 833 | ffa_id_t vm_id, bool is_per_vcpu, ffa_vcpu_index_t vcpu_id, |
| 834 | uint16_t *ids, uint32_t *ids_count, uint32_t *lists_sizes, |
| 835 | uint32_t *lists_count, const uint32_t ids_max_count, |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 836 | enum notifications_info_get_state *info_get_state) |
| 837 | { |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 838 | CHECK(*ids_count <= ids_max_count); |
| 839 | CHECK(*lists_count <= ids_max_count); |
| 840 | |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 841 | if (*info_get_state == FULL || *ids_count == ids_max_count) { |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 842 | *info_get_state = FULL; |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 843 | return false; |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | switch (*info_get_state) { |
| 847 | case INIT: |
| 848 | case STARTING_NEW: |
| 849 | /* |
| 850 | * At this iteration two ids are to be added: the VM ID |
| 851 | * and vCPU ID. If there is no space, change state and |
| 852 | * terminate function. |
| 853 | */ |
| 854 | if (is_per_vcpu && ids_max_count - *ids_count < 2) { |
| 855 | *info_get_state = FULL; |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 856 | return false; |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | *info_get_state = INSERTING; |
| 860 | ids[*ids_count] = vm_id; |
| 861 | ++(*ids_count); |
| 862 | |
| 863 | if (is_per_vcpu) { |
| 864 | /* Insert vCPU ID. */ |
| 865 | ids[*ids_count] = vcpu_id; |
| 866 | ++(*ids_count); |
| 867 | ++lists_sizes[*lists_count]; |
| 868 | } |
| 869 | |
| 870 | ++(*lists_count); |
| 871 | break; |
| 872 | case INSERTING: |
| 873 | /* For per-vCPU notifications only. */ |
| 874 | if (!is_per_vcpu) { |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | /* Insert vCPU ID */ |
| 879 | ids[*ids_count] = vcpu_id; |
| 880 | (*ids_count)++; |
| 881 | /* Increment respective list size */ |
| 882 | ++lists_sizes[*lists_count - 1]; |
| 883 | |
| 884 | if (lists_sizes[*lists_count - 1] == 3) { |
| 885 | *info_get_state = STARTING_NEW; |
| 886 | } |
| 887 | break; |
| 888 | default: |
| 889 | panic("Notification info get action error!!\n"); |
| 890 | } |
| 891 | |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 892 | return true; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Check if the notification is pending and hasn't being retrieved. |
| 897 | * If so attempt to add it to the notification info list. |
| 898 | * Returns true if successfully added to the list. |
| 899 | */ |
| 900 | static bool vm_notifications_state_info_get( |
| 901 | struct notifications_state *state, ffa_id_t vm_id, bool is_per_vcpu, |
| 902 | ffa_vcpu_index_t vcpu_id, uint16_t *ids, uint32_t *ids_count, |
| 903 | uint32_t *lists_sizes, uint32_t *lists_count, |
| 904 | const uint32_t ids_max_count, |
| 905 | enum notifications_info_get_state *info_get_state) |
| 906 | { |
| 907 | ffa_notifications_bitmap_t pending_not_retrieved; |
| 908 | |
| 909 | pending_not_retrieved = state->pending & ~state->info_get_retrieved; |
| 910 | |
| 911 | /* No notifications pending that haven't been retrieved. */ |
| 912 | if (pending_not_retrieved == 0U) { |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | if (!vm_insert_notification_info_list( |
| 917 | vm_id, is_per_vcpu, vcpu_id, ids, ids_count, lists_sizes, |
| 918 | lists_count, ids_max_count, info_get_state)) { |
| 919 | return false; |
| 920 | } |
| 921 | |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 922 | state->info_get_retrieved |= pending_not_retrieved; |
| 923 | |
| 924 | vm_notifications_info_get_retrieved_count_add(pending_not_retrieved); |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 925 | |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Check if the vcpu has a pending IPI that hasn't been retrieved. |
| 931 | * If so try add it to the notification info list. |
| 932 | * Returns true if successfully added to the list. |
| 933 | */ |
| 934 | static bool vm_ipi_state_info_get( |
| 935 | struct vcpu *vcpu, ffa_id_t vm_id, ffa_vcpu_index_t vcpu_id, |
| 936 | uint16_t *ids, uint32_t *ids_count, uint32_t *lists_sizes, |
| 937 | uint32_t *lists_count, const uint32_t ids_max_count, |
| 938 | enum notifications_info_get_state *info_get_state, bool per_vcpu_added) |
| 939 | { |
| 940 | bool ret = true; |
| 941 | bool pending_not_retrieved; |
| 942 | struct vcpu_locked vcpu_locked = vcpu_lock(vcpu); |
| 943 | struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts; |
| 944 | |
| 945 | pending_not_retrieved = |
| 946 | vcpu_is_virt_interrupt_pending(interrupts, HF_IPI_INTID) && |
| 947 | !vcpu_ipi_is_info_get_retrieved(vcpu_locked); |
| 948 | |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame] | 949 | /* |
| 950 | * No notifications pending that haven't been retrieved or the vCPU is |
| 951 | * not in the waiting state. Only report waiting vCPUs as this is the |
| 952 | * only state that needs explicit cycles donated from the NWd. |
| 953 | */ |
| 954 | if (!pending_not_retrieved || vcpu->state != VCPU_STATE_WAITING) { |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 955 | ret = false; |
| 956 | goto out; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * If the per vCPU notification was added to the list we do not need |
| 961 | * to add it again for the IPI. |
| 962 | */ |
| 963 | if (!per_vcpu_added && |
| 964 | !vm_insert_notification_info_list( |
| 965 | vm_id, true, vcpu_id, ids, ids_count, lists_sizes, |
| 966 | lists_count, ids_max_count, info_get_state)) { |
| 967 | ret = false; |
| 968 | goto out; |
| 969 | } |
| 970 | |
| 971 | vcpu_ipi_set_info_get_retrieved(vcpu_locked); |
| 972 | |
| 973 | out: |
| 974 | vcpu_unlock(&vcpu_locked); |
| 975 | |
| 976 | return ret; |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 977 | } |
| 978 | |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 979 | /** |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 980 | * Get pending notification's information to return to the receiver scheduler. |
| 981 | */ |
| 982 | void vm_notifications_info_get_pending( |
| 983 | struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids, |
| 984 | uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count, |
| 985 | const uint32_t ids_max_count, |
| 986 | enum notifications_info_get_state *info_get_state) |
| 987 | { |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 988 | struct notifications *notifications; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 989 | |
| 990 | CHECK(vm_locked.vm != NULL); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 991 | |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 992 | notifications = vm_get_notifications(vm_locked, is_from_vm); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 993 | |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 994 | /* |
| 995 | * Perform info get for global notifications, before doing it for |
| 996 | * per-vCPU. |
| 997 | */ |
| 998 | vm_notifications_state_info_get(¬ifications->global, |
| 999 | vm_locked.vm->id, false, 0, ids, |
| 1000 | ids_count, lists_sizes, lists_count, |
| 1001 | ids_max_count, info_get_state); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 1002 | |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 1003 | for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) { |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 1004 | struct vcpu *vcpu = vm_get_vcpu(vm_locked.vm, i); |
| 1005 | bool per_vcpu_added; |
| 1006 | |
| 1007 | per_vcpu_added = vm_notifications_state_info_get( |
J-Alves | 17c9b6d | 2022-03-25 14:39:05 +0000 | [diff] [blame] | 1008 | ¬ifications->per_vcpu[i], vm_locked.vm->id, true, i, |
| 1009 | ids, ids_count, lists_sizes, lists_count, ids_max_count, |
| 1010 | info_get_state); |
Daniel Boulby | 1f2babf | 2024-08-29 16:39:47 +0100 | [diff] [blame] | 1011 | /* |
| 1012 | * IPIs can only be pending for partitions at the |
| 1013 | * current virtual FF-A instance. |
| 1014 | */ |
| 1015 | if (vm_id_is_current_world(vm_locked.vm->id)) { |
| 1016 | vm_ipi_state_info_get(vcpu, vm_locked.vm->id, i, ids, |
| 1017 | ids_count, lists_sizes, |
| 1018 | lists_count, ids_max_count, |
| 1019 | info_get_state, per_vcpu_added); |
| 1020 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * Gets all info from VM's pending notifications. |
| 1026 | * Returns true if the list is full, and there is more pending. |
| 1027 | */ |
| 1028 | bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids, |
| 1029 | uint32_t *ids_count, uint32_t *lists_sizes, |
| 1030 | uint32_t *lists_count, |
| 1031 | const uint32_t ids_max_count) |
| 1032 | { |
| 1033 | enum notifications_info_get_state current_state = INIT; |
| 1034 | |
J-Alves | f31940e | 2022-03-25 17:24:00 +0000 | [diff] [blame] | 1035 | /* Get info of pending notifications from the framework. */ |
| 1036 | vm_notifications_state_info_get(&vm_locked.vm->notifications.framework, |
| 1037 | vm_locked.vm->id, false, 0, ids, |
| 1038 | ids_count, lists_sizes, lists_count, |
| 1039 | ids_max_count, ¤t_state); |
| 1040 | |
| 1041 | /* Get info of pending notifications from SPs. */ |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 1042 | vm_notifications_info_get_pending(vm_locked, false, ids, ids_count, |
| 1043 | lists_sizes, lists_count, |
| 1044 | ids_max_count, ¤t_state); |
| 1045 | |
J-Alves | f31940e | 2022-03-25 17:24:00 +0000 | [diff] [blame] | 1046 | /* Get info of pending notifications from VMs. */ |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 1047 | vm_notifications_info_get_pending(vm_locked, true, ids, ids_count, |
| 1048 | lists_sizes, lists_count, |
| 1049 | ids_max_count, ¤t_state); |
| 1050 | |
| 1051 | /* |
| 1052 | * State transitions to FULL when trying to insert a new ID in the |
| 1053 | * list and there is not more space. This means there are notifications |
| 1054 | * pending, whose info is not retrieved. |
| 1055 | */ |
| 1056 | return current_state == FULL; |
| 1057 | } |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 1058 | |
| 1059 | /** |
| 1060 | * Checks VM's messaging method support. |
| 1061 | */ |
Kathleen Capella | f71dee4 | 2023-08-08 16:24:14 -0400 | [diff] [blame] | 1062 | bool vm_supports_messaging_method(struct vm *vm, uint16_t msg_method) |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 1063 | { |
| 1064 | return (vm->messaging_method & msg_method) != 0; |
| 1065 | } |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1066 | |
J-Alves | 7e67d10 | 2022-04-13 13:22:39 +0100 | [diff] [blame] | 1067 | /** |
| 1068 | * Sets the designated GP register that the VM expects to receive the boot |
| 1069 | * info's address. |
| 1070 | */ |
| 1071 | void vm_set_boot_info_gp_reg(struct vm *vm, struct vcpu *vcpu) |
| 1072 | { |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 1073 | if (vm->boot_info.blob_addr.ipa != 0U) { |
J-Alves | 7e67d10 | 2022-04-13 13:22:39 +0100 | [diff] [blame] | 1074 | arch_regs_set_gp_reg(&vcpu->regs, |
| 1075 | ipa_addr(vm->boot_info.blob_addr), |
| 1076 | vm->boot_info.gp_register_num); |
| 1077 | } |
| 1078 | } |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 1079 | |
| 1080 | /** |
| 1081 | * Obtain the interrupt descriptor entry of the specified vm corresponding |
| 1082 | * to the specific interrupt id. |
| 1083 | */ |
Madhukar Pappireddy | 3221a44 | 2023-07-24 16:10:55 -0500 | [diff] [blame] | 1084 | static struct interrupt_descriptor *vm_find_interrupt_descriptor( |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 1085 | struct vm_locked vm_locked, uint32_t id) |
| 1086 | { |
| 1087 | for (uint32_t i = 0; i < HF_NUM_INTIDS; i++) { |
| 1088 | /* Interrupt descriptors are populated contiguously. */ |
| 1089 | if (!vm_locked.vm->interrupt_desc[i].valid) { |
| 1090 | break; |
| 1091 | } |
| 1092 | |
| 1093 | if (vm_locked.vm->interrupt_desc[i].interrupt_id == id) { |
| 1094 | /* Interrupt descriptor found. */ |
| 1095 | return &vm_locked.vm->interrupt_desc[i]; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * Update the target MPIDR corresponding to the specified interrupt id |
| 1104 | * belonging to the specified vm. |
| 1105 | */ |
| 1106 | struct interrupt_descriptor *vm_interrupt_set_target_mpidr( |
| 1107 | struct vm_locked vm_locked, uint32_t id, uint32_t target_mpidr) |
| 1108 | { |
| 1109 | struct interrupt_descriptor *int_desc; |
| 1110 | |
| 1111 | int_desc = vm_find_interrupt_descriptor(vm_locked, id); |
| 1112 | |
| 1113 | if (int_desc != NULL) { |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 1114 | int_desc->mpidr_valid = true; |
| 1115 | int_desc->mpidr = target_mpidr; |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | return int_desc; |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * Update the security state of the specified interrupt id belonging to the |
| 1123 | * specified vm. |
| 1124 | */ |
| 1125 | struct interrupt_descriptor *vm_interrupt_set_sec_state( |
| 1126 | struct vm_locked vm_locked, uint32_t id, uint32_t sec_state) |
| 1127 | { |
| 1128 | struct interrupt_descriptor *int_desc; |
| 1129 | |
| 1130 | int_desc = vm_find_interrupt_descriptor(vm_locked, id); |
| 1131 | |
| 1132 | if (int_desc != NULL) { |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 1133 | int_desc->sec_state = sec_state; |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | return int_desc; |
| 1137 | } |
Madhukar Pappireddy | 938faaf | 2023-07-31 17:56:55 -0500 | [diff] [blame] | 1138 | |
| 1139 | /** |
| 1140 | * Enable or disable the specified interrupt id belonging to specified vm. |
| 1141 | */ |
| 1142 | struct interrupt_descriptor *vm_interrupt_set_enable(struct vm_locked vm_locked, |
| 1143 | uint32_t id, bool enable) |
| 1144 | { |
| 1145 | struct interrupt_descriptor *int_desc; |
| 1146 | |
| 1147 | int_desc = vm_find_interrupt_descriptor(vm_locked, id); |
| 1148 | |
| 1149 | if (int_desc != NULL) { |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 1150 | int_desc->enabled = enable; |
Madhukar Pappireddy | 938faaf | 2023-07-31 17:56:55 -0500 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | return int_desc; |
| 1154 | } |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 1155 | |
| 1156 | /** |
| 1157 | * The 'boot_list' is used as the start and end of the list. |
| 1158 | * Start: the nodes it points to is the first VM to boot. |
| 1159 | * End: the last node's next points to the entry. |
| 1160 | */ |
| 1161 | static bool vm_is_boot_list_end(struct vm *vm) |
| 1162 | { |
| 1163 | return vm->boot_list_node.next == &boot_list; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * Gets the first partition to boot, according to Boot Protocol from FF-A spec. |
| 1168 | */ |
| 1169 | struct vm *vm_get_boot_vm(void) |
| 1170 | { |
| 1171 | assert(!list_empty(&boot_list)); |
| 1172 | |
| 1173 | return CONTAINER_OF(boot_list.next, struct vm, boot_list_node); |
| 1174 | } |
| 1175 | |
| 1176 | /** |
Madhukar Pappireddy | a81f541 | 2024-11-25 09:46:48 -0600 | [diff] [blame] | 1177 | * Gets the first MP partition to boot on a secondary CPU, as per the boot |
| 1178 | * order from FF-A spec. |
| 1179 | * If every SP in the system is an UP partition, this function returns NULL. |
| 1180 | */ |
| 1181 | struct vm *vm_get_boot_vm_secondary_core(void) |
| 1182 | { |
| 1183 | struct vm *vm = vm_get_boot_vm(); |
| 1184 | |
| 1185 | if (vm_is_up(vm)) { |
| 1186 | return vm_get_next_boot_secondary_core(vm); |
| 1187 | } |
| 1188 | |
| 1189 | return vm; |
| 1190 | } |
| 1191 | |
| 1192 | /** |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 1193 | * Returns the next element in the boot order list, if there is one. |
| 1194 | */ |
| 1195 | struct vm *vm_get_next_boot(struct vm *vm) |
| 1196 | { |
| 1197 | return vm_is_boot_list_end(vm) |
| 1198 | ? NULL |
| 1199 | : CONTAINER_OF(vm->boot_list_node.next, struct vm, |
| 1200 | boot_list_node); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Madhukar Pappireddy | a81f541 | 2024-11-25 09:46:48 -0600 | [diff] [blame] | 1204 | * Returns the next element representing an MP endpoint in the boot order list, |
| 1205 | * if there is one. |
| 1206 | */ |
| 1207 | struct vm *vm_get_next_boot_secondary_core(struct vm *vm) |
| 1208 | { |
| 1209 | struct vm *vm_next; |
| 1210 | |
| 1211 | assert(vm != NULL); |
| 1212 | |
| 1213 | vm_next = vm_get_next_boot(vm); |
| 1214 | |
| 1215 | /* Keep searching until an MP endpoint is found. */ |
| 1216 | while (vm_next != NULL && vm_is_up(vm_next)) { |
| 1217 | vm_next = vm_get_next_boot(vm_next); |
| 1218 | } |
| 1219 | |
| 1220 | return vm_next; |
| 1221 | } |
| 1222 | |
| 1223 | /** |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 1224 | * Insert in boot list, sorted by `boot_order` parameter in the vm structure |
| 1225 | * and rooted in `first_boot_vm`. |
| 1226 | */ |
| 1227 | void vm_update_boot(struct vm *vm) |
| 1228 | { |
| 1229 | struct vm *current_vm = NULL; |
| 1230 | |
| 1231 | if (list_empty(&boot_list)) { |
| 1232 | list_prepend(&boot_list, &vm->boot_list_node); |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * When getting to this point the first insertion should have |
| 1238 | * been done. |
| 1239 | */ |
| 1240 | current_vm = vm_get_boot_vm(); |
| 1241 | assert(current_vm != NULL); |
| 1242 | |
| 1243 | /* |
| 1244 | * Iterate until the position is found according to boot order, or |
| 1245 | * until we reach end of the list. |
| 1246 | */ |
| 1247 | while (!vm_is_boot_list_end(current_vm) && |
| 1248 | current_vm->boot_order <= vm->boot_order) { |
| 1249 | current_vm = vm_get_next_boot(current_vm); |
| 1250 | } |
| 1251 | |
| 1252 | current_vm->boot_order > vm->boot_order |
| 1253 | ? list_prepend(¤t_vm->boot_list_node, &vm->boot_list_node) |
| 1254 | : list_append(¤t_vm->boot_list_node, &vm->boot_list_node); |
| 1255 | } |