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