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