blob: 128d850333f0346d58ce8f2ab9e404265f5d67ea [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
Andrew Scull18c78fc2018-08-20 12:57:41 +010011#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000012#include "hf/assert.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010013#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010014#include "hf/cpu.h"
J-Alves4ef6e842021-03-18 12:47:01 +000015#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010016#include "hf/ffa.h"
Andrew Scull3c257452019-11-26 13:32:50 +000017#include "hf/layout.h"
18#include "hf/plat/iommu.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010019#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010020
Andrew Scull19503262018-09-20 14:48:39 +010021#include "vmapi/hf/call.h"
22
23static struct vm vms[MAX_VMS];
Olivier Deprez96a2a262020-06-11 17:21:38 +020024static struct vm other_world;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010025static ffa_vm_count_t vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010026
J-Alvesfe23ebe2021-10-13 16:07:07 +010027/**
28 * Counters on the status of notifications in the system. It helps to improve
29 * the information retrieved by the receiver scheduler.
30 */
31static struct {
32 /** Counts notifications pending. */
33 uint32_t pending_count;
34 /**
35 * Counts notifications pending, that have been retrieved by the
36 * receiver scheduler.
37 */
38 uint32_t info_get_retrieved_count;
39 struct spinlock lock;
40} all_notifications_state;
41
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080042static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
43{
44 if (vm->el0_partition) {
45 return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
46 ppool);
47 }
48 return mm_vm_init(&vm->ptable, vm->id, ppool);
49}
50
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010051struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080052 struct mpool *ppool, bool el0_partition)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010053{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010054 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010055 struct vm *vm;
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -070056 size_t vcpu_ppool_entries = (align_up(sizeof(struct vcpu) * vcpu_count,
57 MM_PPOOL_ENTRY_SIZE) /
58 MM_PPOOL_ENTRY_SIZE);
Andrew Scull19503262018-09-20 14:48:39 +010059
Olivier Deprez96a2a262020-06-11 17:21:38 +020060 if (id == HF_OTHER_WORLD_ID) {
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080061 CHECK(el0_partition == false);
Olivier Deprez96a2a262020-06-11 17:21:38 +020062 vm = &other_world;
Andrew Walbran9daa57e2019-09-27 13:33:20 +010063 } else {
64 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010065
Andrew Walbran9daa57e2019-09-27 13:33:20 +010066 CHECK(id >= HF_VM_ID_OFFSET);
67 CHECK(vm_index < ARRAY_SIZE(vms));
68 vm = &vms[vm_index];
69 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010070
Andrew Scull2b5fbad2019-04-05 13:55:56 +010071 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010072
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000073 list_init(&vm->mailbox.waiter_list);
74 list_init(&vm->mailbox.ready_list);
75 sl_init(&vm->lock);
76
Andrew Walbran9daa57e2019-09-27 13:33:20 +010077 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010078 vm->vcpu_count = vcpu_count;
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -070079
80 vm->vcpus = (struct vcpu *)mpool_alloc_contiguous(
81 ppool, vcpu_ppool_entries, 1);
82 CHECK(vm->vcpus != NULL);
Raghu Krishnamurthyf5fec202022-09-30 07:25:10 -070083
Andrew Sculld6ee1102019-04-05 22:12:42 +010084 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000085 atomic_init(&vm->aborting, false);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080086 vm->el0_partition = el0_partition;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010087
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080088 if (!vm_init_mm(vm, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010089 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000090 }
91
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000092 /* Initialise waiter entries. */
93 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000094 vm->wait_entries[i].waiting_vm = vm;
95 list_init(&vm->wait_entries[i].wait_links);
96 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000097 }
98
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000099 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +0100100 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +0100101 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100102 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100103
Raghu Krishnamurthyf5fec202022-09-30 07:25:10 -0700104 vm_notifications_init(vm, vcpu_count, ppool);
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100105 return vm;
106}
107
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100108bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800109 struct vm **new_vm, bool el0_partition)
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100110{
111 if (vm_count >= MAX_VMS) {
112 return false;
113 }
114
115 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800116 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool,
117 el0_partition);
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100118 if (*new_vm == NULL) {
119 return false;
120 }
Andrew Scull19503262018-09-20 14:48:39 +0100121 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +0100122
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000123 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100124}
125
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100126ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +0100127{
128 return vm_count;
129}
130
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100131/**
132 * Returns a pointer to the VM with the corresponding id.
133 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100134struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +0100135{
David Brazdilbc501192019-09-27 13:20:56 +0100136 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +0100137
Olivier Deprez96a2a262020-06-11 17:21:38 +0200138 if (id == HF_OTHER_WORLD_ID) {
139 if (other_world.id == HF_OTHER_WORLD_ID) {
140 return &other_world;
141 }
Andrew Scull19503262018-09-20 14:48:39 +0100142 return NULL;
143 }
144
Olivier Deprez96a2a262020-06-11 17:21:38 +0200145 /* Check that this is not a reserved ID. */
146 if (id < HF_VM_ID_OFFSET) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100147 return NULL;
148 }
149
David Brazdilbc501192019-09-27 13:20:56 +0100150 index = id - HF_VM_ID_OFFSET;
151
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100152 return vm_find_index(index);
153}
154
155/**
J-Alves46ee0682021-07-26 15:17:53 +0100156 * Returns a locked instance of the VM with the corresponding id.
157 */
158struct vm_locked vm_find_locked(ffa_vm_id_t id)
159{
160 struct vm *vm = vm_find(id);
161
162 if (vm != NULL) {
163 return vm_lock(vm);
164 }
165
166 return (struct vm_locked){.vm = NULL};
167}
168
169/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100170 * Returns a pointer to the VM at the specified index.
171 */
172struct vm *vm_find_index(uint16_t index)
173{
David Brazdilbc501192019-09-27 13:20:56 +0100174 /* Ensure the VM is initialized. */
175 if (index >= vm_count) {
176 return NULL;
177 }
178
179 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100180}
181
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000182/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000183 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000184 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100185struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000186{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100187 struct vm_locked locked = {
188 .vm = vm,
189 };
190
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000191 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100192
193 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000194}
195
196/**
Jose Marinho75509b42019-04-09 09:34:59 +0100197 * Locks two VMs ensuring that the locking order is according to the locks'
198 * addresses.
199 */
200struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
201{
202 struct two_vm_locked dual_lock;
203
204 sl_lock_both(&vm1->lock, &vm2->lock);
205 dual_lock.vm1.vm = vm1;
206 dual_lock.vm2.vm = vm2;
207
208 return dual_lock;
209}
210
211/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000212 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
213 * the fact that the VM is no longer locked.
214 */
215void vm_unlock(struct vm_locked *locked)
216{
217 sl_unlock(&locked->vm->lock);
218 locked->vm = NULL;
219}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100220
221/**
222 * Get the vCPU with the given index from the given VM.
223 * This assumes the index is valid, i.e. less than vm->vcpu_count.
224 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100225struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100226{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100227 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100228 return &vm->vcpus[vcpu_index];
229}
Andrew Scull3c257452019-11-26 13:32:50 +0000230
231/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000232 * Gets `vm`'s wait entry for waiting on the `for_vm`.
233 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100234struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000235{
236 uint16_t index;
237
238 CHECK(for_vm >= HF_VM_ID_OFFSET);
239 index = for_vm - HF_VM_ID_OFFSET;
240 CHECK(index < MAX_VMS);
241
242 return &vm->wait_entries[index];
243}
244
245/**
J-Alves122f1a12022-12-12 15:55:42 +0000246 * Checks whether the given `to` VM's mailbox is currently busy.
247 */
248bool vm_is_mailbox_busy(struct vm_locked to)
249{
250 return to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
251 to.vm->mailbox.recv == NULL;
252}
253
254/**
J-Alvese8c8c2b2022-12-16 15:34:48 +0000255 * Checks if mailbox is currently owned by the other world.
256 */
257bool vm_is_mailbox_other_world_owned(struct vm_locked to)
258{
259 return to.vm->mailbox.state == MAILBOX_STATE_OTHER_WORLD_OWNED;
260}
261
262/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000263 * Gets the ID of the VM which the given VM's wait entry is for.
264 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100265ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000266{
267 uint16_t index = entry - vm->wait_entries;
268
269 return index + HF_VM_ID_OFFSET;
270}
271
272/**
Andrew Walbran45633dd2020-10-07 17:59:54 +0100273 * Return whether the given VM ID represents an entity in the current world:
274 * i.e. the hypervisor or a normal world VM when running in the normal world, or
275 * the SPM or an SP when running in the secure world.
276 */
277bool vm_id_is_current_world(ffa_vm_id_t vm_id)
278{
279 return (vm_id & HF_VM_ID_WORLD_MASK) !=
280 (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK);
281}
282
283/**
Andrew Scull3c257452019-11-26 13:32:50 +0000284 * Map a range of addresses to the VM in both the MMU and the IOMMU.
285 *
286 * mm_vm_defrag should always be called after a series of page table updates,
287 * whether they succeed or fail. This is because on failure extra page table
288 * entries may have been allocated and then not used, while on success it may be
289 * possible to compact the page table by merging several entries into a block.
290 *
291 * Returns true on success, or false if the update failed and no changes were
292 * made.
293 *
294 */
295bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
296 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
297{
298 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
299 return false;
300 }
301
302 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
303
304 return true;
305}
306
307/**
308 * Prepares the given VM for the given address mapping such that it will be able
309 * to commit the change without failure.
310 *
311 * In particular, multiple calls to this function will result in the
312 * corresponding calls to commit the changes to succeed.
313 *
314 * Returns true on success, or false if the update failed and no changes were
315 * made.
316 */
317bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
318 uint32_t mode, struct mpool *ppool)
319{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800320 if (vm_locked.vm->el0_partition) {
321 return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
322 mode, ppool);
323 }
Andrew Scull3c257452019-11-26 13:32:50 +0000324 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
325 ppool);
326}
327
328/**
329 * Commits the given address mapping to the VM assuming the operation cannot
330 * fail. `vm_identity_prepare` must used correctly before this to ensure
331 * this condition.
332 */
333void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
334 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
335{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800336 if (vm_locked.vm->el0_partition) {
337 mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
338 ppool);
339 if (ipa != NULL) {
340 /*
341 * EL0 partitions are modeled as lightweight VM's, to
342 * promote code reuse. The below statement returns the
343 * mapped PA as an IPA, however, for an EL0 partition,
344 * this is really a VA.
345 */
346 *ipa = ipa_from_pa(begin);
347 }
348 } else {
349 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
350 ppool, ipa);
351 }
Andrew Scull3c257452019-11-26 13:32:50 +0000352 plat_iommu_identity_map(vm_locked, begin, end, mode);
353}
354
355/**
356 * Unmap a range of addresses from the VM.
357 *
358 * Returns true on success, or false if the update failed and no changes were
359 * made.
360 */
361bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
362 struct mpool *ppool)
363{
364 uint32_t mode = MM_MODE_UNMAPPED_MASK;
365
366 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
367}
368
369/**
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700370 * Defrag page tables for an EL0 partition or for a VM.
371 */
372void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool)
373{
374 if (vm_locked.vm->el0_partition) {
375 mm_stage1_defrag(&vm_locked.vm->ptable, ppool);
376 } else {
377 mm_vm_defrag(&vm_locked.vm->ptable, ppool);
378 }
379}
380
381/**
Andrew Scull3c257452019-11-26 13:32:50 +0000382 * Unmaps the hypervisor pages from the given page table.
383 */
384bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
385{
386 /* TODO: If we add pages dynamically, they must be included here too. */
387 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
388 ppool) &&
389 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
390 ppool) &&
391 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
Maksims Svecovs134b8f92022-03-04 15:14:09 +0000392 ppool) &&
393 vm_unmap(vm_locked, layout_stacks_begin(), layout_stacks_end(),
Andrew Scull3c257452019-11-26 13:32:50 +0000394 ppool);
395}
J-Alvesb37fd082020-10-22 12:29:21 +0100396
397/**
Raghu Krishnamurthyea195fa2021-02-12 23:29:00 -0800398 * Gets the mode of the given range of ipa or va if they are mapped with the
399 * same mode.
400 *
401 * Returns true if the range is mapped with the same mode and false otherwise.
402 * The wrapper calls the appropriate mm function depending on if the partition
403 * is a vm or a el0 partition.
404 */
405bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end,
406 uint32_t *mode)
407{
408 if (vm_locked.vm->el0_partition) {
409 return mm_get_mode(&vm_locked.vm->ptable,
410 va_from_pa(pa_from_ipa(begin)),
411 va_from_pa(pa_from_ipa(end)), mode);
412 }
413 return mm_vm_get_mode(&vm_locked.vm->ptable, begin, end, mode);
414}
J-Alvesa0f317d2021-06-09 13:31:59 +0100415
J-Alves66652252022-07-06 09:49:51 +0100416bool vm_mailbox_state_busy(struct vm_locked vm_locked)
417{
418 return vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
419 vm_locked.vm->mailbox.recv == NULL;
420}
421
J-Alves7461ef22021-10-18 17:21:33 +0100422static struct notifications *vm_get_notifications(struct vm_locked vm_locked,
423 bool is_from_vm)
424{
425 return is_from_vm ? &vm_locked.vm->notifications.from_vm
426 : &vm_locked.vm->notifications.from_sp;
427}
428
J-Alvesa0f317d2021-06-09 13:31:59 +0100429/*
Raghu Krishnamurthyf5fec202022-09-30 07:25:10 -0700430 * Dynamically allocate per_vcpu_notifications structure for a given VM.
431 */
432static void vm_notifications_init_per_vcpu_notifications(
433 struct vm *vm, ffa_vcpu_count_t vcpu_count, struct mpool *ppool)
434{
435 size_t notif_ppool_entries =
436 (align_up(sizeof(struct notifications_state) * vcpu_count,
437 MM_PPOOL_ENTRY_SIZE) /
438 MM_PPOOL_ENTRY_SIZE);
439
440 /*
441 * Allow for function to be called on already initialized VMs but those
442 * that require notification structure to be cleared.
443 */
444 if (vm->notifications.from_sp.per_vcpu == NULL) {
445 assert(vm->notifications.from_vm.per_vcpu == NULL);
446 assert(vcpu_count != 0);
447 CHECK(ppool != NULL);
448 vm->notifications.from_sp.per_vcpu =
449 (struct notifications_state *)mpool_alloc_contiguous(
450 ppool, notif_ppool_entries, 1);
451 CHECK(vm->notifications.from_sp.per_vcpu != NULL);
452
453 vm->notifications.from_vm.per_vcpu =
454 (struct notifications_state *)mpool_alloc_contiguous(
455 ppool, notif_ppool_entries, 1);
456 CHECK(vm->notifications.from_vm.per_vcpu != NULL);
457 } else {
458 assert(vm->notifications.from_vm.per_vcpu != NULL);
459 }
460
461 memset_s(vm->notifications.from_sp.per_vcpu,
462 sizeof(*(vm->notifications.from_sp.per_vcpu)) * vcpu_count, 0,
463 sizeof(*(vm->notifications.from_sp.per_vcpu)) * vcpu_count);
464 memset_s(vm->notifications.from_vm.per_vcpu,
465 sizeof(*(vm->notifications.from_vm.per_vcpu)) * vcpu_count, 0,
466 sizeof(*(vm->notifications.from_vm.per_vcpu)) * vcpu_count);
467}
468
469/*
J-Alvesa0f317d2021-06-09 13:31:59 +0100470 * Initializes the notifications structure.
471 */
Raghu Krishnamurthyf5fec202022-09-30 07:25:10 -0700472static void vm_notifications_init_bindings(struct notifications *notifications)
J-Alvesa0f317d2021-06-09 13:31:59 +0100473{
474 for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) {
475 notifications->bindings_sender_id[i] = HF_INVALID_VM_ID;
476 }
477}
478
Raghu Krishnamurthyf5fec202022-09-30 07:25:10 -0700479/*
480 * Initialize notification related structures for a VM.
481 */
482void vm_notifications_init(struct vm *vm, ffa_vcpu_count_t vcpu_count,
483 struct mpool *ppool)
484{
485 vm_notifications_init_per_vcpu_notifications(vm, vcpu_count, ppool);
486
487 /* Basic initialization of the notifications structure. */
488 vm_notifications_init_bindings(&vm->notifications.from_sp);
489 vm_notifications_init_bindings(&vm->notifications.from_vm);
490}
491
J-Alvesa0f317d2021-06-09 13:31:59 +0100492/**
493 * Checks if there are pending notifications.
494 */
495bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm,
496 ffa_notifications_bitmap_t notifications)
497{
498 struct notifications *to_check;
499
500 CHECK(vm_locked.vm != NULL);
501
J-Alves7461ef22021-10-18 17:21:33 +0100502 to_check = vm_get_notifications(vm_locked, from_vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100503
504 /* Check if there are pending per vcpu notifications */
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -0700505 for (uint32_t i = 0U; i < vm_locked.vm->vcpu_count; i++) {
J-Alvesa0f317d2021-06-09 13:31:59 +0100506 if ((to_check->per_vcpu[i].pending & notifications) != 0U) {
507 return true;
508 }
509 }
510
511 /* Check if there are global pending notifications */
512 return (to_check->global.pending & notifications) != 0U;
513}
J-Alvesc003a7a2021-03-18 13:06:53 +0000514
J-Alves7461ef22021-10-18 17:21:33 +0100515/**
516 * Checks if there are pending global notifications, either from SPs or from
517 * VMs.
518 */
519bool vm_are_global_notifications_pending(struct vm_locked vm_locked)
520{
521 return vm_get_notifications(vm_locked, true)->global.pending != 0ULL ||
J-Alves52578f82022-03-25 12:30:47 +0000522 vm_get_notifications(vm_locked, false)->global.pending != 0ULL ||
J-Alvese8c8c2b2022-12-16 15:34:48 +0000523 vm_are_fwk_notifications_pending(vm_locked);
524}
525
526/**
527 * Currently only RX full notification is supported as framework notification.
528 * Returns true if there is one pending, either from Hypervisor or SPMC.
529 */
530bool vm_are_fwk_notifications_pending(struct vm_locked vm_locked)
531{
532 return vm_locked.vm->notifications.framework.pending != 0ULL;
J-Alves7461ef22021-10-18 17:21:33 +0100533}
534
535/**
536 * Checks if there are pending per-vCPU notifications, in a specific vCPU either
537 * from SPs or from VMs.
538 */
539bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked,
540 ffa_vcpu_index_t vcpu_id)
541{
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -0700542 CHECK(vcpu_id < vm_locked.vm->vcpu_count);
J-Alves7461ef22021-10-18 17:21:33 +0100543
544 return vm_get_notifications(vm_locked, true)
545 ->per_vcpu[vcpu_id]
546 .pending != 0ULL ||
547 vm_get_notifications(vm_locked, false)
548 ->per_vcpu[vcpu_id]
549 .pending != 0ULL;
550}
551
J-Alves09ff9d82021-11-02 11:55:20 +0000552bool vm_are_notifications_enabled(struct vm *vm)
J-Alvesc003a7a2021-03-18 13:06:53 +0000553{
J-Alves09ff9d82021-11-02 11:55:20 +0000554 return vm->notifications.enabled == true;
555}
556
557bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked)
558{
559 return vm_are_notifications_enabled(vm_locked.vm);
J-Alvesc003a7a2021-03-18 13:06:53 +0000560}
561
562static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications,
563 uint32_t i)
564{
565 return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U;
566}
567
J-Alvesfe23ebe2021-10-13 16:07:07 +0100568static void vm_notifications_global_state_count_update(
569 ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc)
570{
571 /*
572 * Helper to increment counters from global notifications
573 * state. Count update by increments or decrements of 1 or -1,
574 * respectively.
575 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000576 assert(inc == 1 || inc == -1);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100577
578 sl_lock(&all_notifications_state.lock);
579
580 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
581 if (vm_is_notification_bit_set(bitmap, i)) {
582 CHECK((inc > 0 && *counter < UINT32_MAX) ||
583 (inc < 0 && *counter > 0));
584 *counter += inc;
585 }
586 }
587
588 sl_unlock(&all_notifications_state.lock);
589}
590
591/**
592 * Helper function to increment the pending notifications based on a bitmap
593 * passed as argument.
594 * Function to be used at setting notifications for a given VM.
595 */
596static void vm_notifications_pending_count_add(
597 ffa_notifications_bitmap_t to_add)
598{
599 vm_notifications_global_state_count_update(
600 to_add, &all_notifications_state.pending_count, 1);
601}
602
603/**
604 * Helper function to decrement the pending notifications count.
605 * Function to be used when getting the receiver's pending notifications.
606 */
607static void vm_notifications_pending_count_sub(
608 ffa_notifications_bitmap_t to_sub)
609{
610 vm_notifications_global_state_count_update(
611 to_sub, &all_notifications_state.pending_count, -1);
612}
613
614/**
615 * Helper function to count the notifications whose information has been
616 * retrieved by the scheduler of the system, and are still pending.
617 */
618static void vm_notifications_info_get_retrieved_count_add(
619 ffa_notifications_bitmap_t to_add)
620{
621 vm_notifications_global_state_count_update(
622 to_add, &all_notifications_state.info_get_retrieved_count, 1);
623}
624
625/**
626 * Helper function to subtract the notifications that the receiver is getting
627 * and whose information has been retrieved by the receiver scheduler.
628 */
629static void vm_notifications_info_get_retrieved_count_sub(
630 ffa_notifications_bitmap_t to_sub)
631{
632 vm_notifications_global_state_count_update(
633 to_sub, &all_notifications_state.info_get_retrieved_count, -1);
634}
635
636/**
637 * Helper function to determine if there are notifications pending whose info
638 * hasn't been retrieved by the receiver scheduler.
639 */
640bool vm_notifications_pending_not_retrieved_by_scheduler(void)
641{
642 bool ret;
643
644 sl_lock(&all_notifications_state.lock);
645 ret = all_notifications_state.pending_count >
646 all_notifications_state.info_get_retrieved_count;
647 sl_unlock(&all_notifications_state.lock);
648
649 return ret;
650}
651
652bool vm_is_notifications_pending_count_zero(void)
653{
654 bool ret;
655
656 sl_lock(&all_notifications_state.lock);
657 ret = all_notifications_state.pending_count == 0;
658 sl_unlock(&all_notifications_state.lock);
659
660 return ret;
661}
662
J-Alvesc003a7a2021-03-18 13:06:53 +0000663/**
664 * Checks that all provided notifications are bound to the specified sender, and
665 * are per VCPU or global, as specified.
666 */
667bool vm_notifications_validate_binding(struct vm_locked vm_locked,
668 bool is_from_vm, ffa_vm_id_t sender_id,
669 ffa_notifications_bitmap_t notifications,
670 bool is_per_vcpu)
671{
672 return vm_notifications_validate_bound_sender(
673 vm_locked, is_from_vm, sender_id, notifications) &&
674 vm_notifications_validate_per_vcpu(vm_locked, is_from_vm,
675 is_per_vcpu, notifications);
676}
677
678/**
679 * Update binds information in notification structure for the specified
680 * notifications.
681 */
682void vm_notifications_update_bindings(struct vm_locked vm_locked,
683 bool is_from_vm, ffa_vm_id_t sender_id,
684 ffa_notifications_bitmap_t notifications,
685 bool is_per_vcpu)
686{
687 CHECK(vm_locked.vm != NULL);
688 struct notifications *to_update =
689 vm_get_notifications(vm_locked, is_from_vm);
690
691 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
692 if (vm_is_notification_bit_set(notifications, i)) {
693 to_update->bindings_sender_id[i] = sender_id;
694 }
695 }
696
697 /*
698 * Set notifications if they are per VCPU, else clear them as they are
699 * global.
700 */
701 if (is_per_vcpu) {
702 to_update->bindings_per_vcpu |= notifications;
703 } else {
704 to_update->bindings_per_vcpu &= ~notifications;
705 }
706}
707
708bool vm_notifications_validate_bound_sender(
709 struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id,
710 ffa_notifications_bitmap_t notifications)
711{
712 CHECK(vm_locked.vm != NULL);
713 struct notifications *to_check =
714 vm_get_notifications(vm_locked, is_from_vm);
715
716 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
717 if (vm_is_notification_bit_set(notifications, i) &&
718 to_check->bindings_sender_id[i] != sender_id) {
719 return false;
720 }
721 }
722
723 return true;
724}
725
726bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked,
727 bool is_from_vm, bool is_per_vcpu,
728 ffa_notifications_bitmap_t notif)
729{
730 CHECK(vm_locked.vm != NULL);
731 struct notifications *to_check =
732 vm_get_notifications(vm_locked, is_from_vm);
733
734 return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U
735 : (to_check->bindings_per_vcpu & notif) == 0U;
736}
J-Alvesaa79c012021-07-09 14:29:45 +0100737
J-Alves14163a72022-03-25 14:01:34 +0000738static void vm_notifications_state_set(struct notifications_state *state,
739 ffa_notifications_bitmap_t notifications)
740{
741 state->pending |= notifications;
742 vm_notifications_pending_count_add(notifications);
743}
744
J-Alves5a16c962022-03-25 12:32:51 +0000745void vm_notifications_partition_set_pending(
746 struct vm_locked vm_locked, bool is_from_vm,
747 ffa_notifications_bitmap_t notifications, ffa_vcpu_index_t vcpu_id,
748 bool is_per_vcpu)
J-Alvesaa79c012021-07-09 14:29:45 +0100749{
J-Alves14163a72022-03-25 14:01:34 +0000750 struct notifications *to_set;
751 struct notifications_state *state;
752
J-Alvesaa79c012021-07-09 14:29:45 +0100753 CHECK(vm_locked.vm != NULL);
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -0700754 CHECK(vcpu_id < vm_locked.vm->vcpu_count);
J-Alvesaa79c012021-07-09 14:29:45 +0100755
J-Alves14163a72022-03-25 14:01:34 +0000756 to_set = vm_get_notifications(vm_locked, is_from_vm);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100757
J-Alves14163a72022-03-25 14:01:34 +0000758 state = is_per_vcpu ? &to_set->per_vcpu[vcpu_id] : &to_set->global;
759
760 vm_notifications_state_set(state, notifications);
761}
762
763/**
764 * Set pending framework notifications.
765 */
766void vm_notifications_framework_set_pending(
767 struct vm_locked vm_locked, ffa_notifications_bitmap_t notifications)
768{
769 CHECK(vm_locked.vm != NULL);
Federico Recanatie73d2832022-04-20 11:10:52 +0200770 assert(is_ffa_spm_buffer_full_notification(notifications) ||
771 is_ffa_hyp_buffer_full_notification(notifications));
J-Alves14163a72022-03-25 14:01:34 +0000772 vm_notifications_state_set(&vm_locked.vm->notifications.framework,
773 notifications);
J-Alvesaa79c012021-07-09 14:29:45 +0100774}
775
J-Alves5136dda2022-03-25 12:26:38 +0000776static ffa_notifications_bitmap_t vm_notifications_state_get_pending(
777 struct notifications_state *state)
J-Alvesaa79c012021-07-09 14:29:45 +0100778{
J-Alves5136dda2022-03-25 12:26:38 +0000779 ffa_notifications_bitmap_t to_ret;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100780 ffa_notifications_bitmap_t pending_and_info_get_retrieved;
J-Alvesaa79c012021-07-09 14:29:45 +0100781
J-Alves5136dda2022-03-25 12:26:38 +0000782 assert(state != NULL);
J-Alvesaa79c012021-07-09 14:29:45 +0100783
J-Alves5136dda2022-03-25 12:26:38 +0000784 to_ret = state->pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100785
786 /* Update count of currently pending notifications in the system. */
J-Alves5136dda2022-03-25 12:26:38 +0000787 vm_notifications_pending_count_sub(state->pending);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100788
789 /*
790 * If notifications receiver is getting have been retrieved by the
791 * receiver scheduler, decrement those from respective count.
792 */
793 pending_and_info_get_retrieved =
J-Alves5136dda2022-03-25 12:26:38 +0000794 state->pending & state->info_get_retrieved;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100795
796 if (pending_and_info_get_retrieved != 0) {
797 vm_notifications_info_get_retrieved_count_sub(
798 pending_and_info_get_retrieved);
799 }
800
J-Alves5136dda2022-03-25 12:26:38 +0000801 state->pending = 0U;
802 state->info_get_retrieved = 0U;
J-Alvesaa79c012021-07-09 14:29:45 +0100803
J-Alves5136dda2022-03-25 12:26:38 +0000804 return to_ret;
805}
J-Alvesfe23ebe2021-10-13 16:07:07 +0100806
J-Alves5136dda2022-03-25 12:26:38 +0000807/**
808 * Get global and per-vCPU notifications for the given vCPU ID.
809 */
810ffa_notifications_bitmap_t vm_notifications_partition_get_pending(
811 struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id)
812{
813 ffa_notifications_bitmap_t to_ret;
814 struct notifications *to_get;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100815
J-Alves5136dda2022-03-25 12:26:38 +0000816 assert(vm_locked.vm != NULL);
817 to_get = vm_get_notifications(vm_locked, is_from_vm);
Raghu Krishnamurthy30aabd62022-09-17 21:41:00 -0700818 assert(vcpu_id < vm_locked.vm->vcpu_count);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100819
J-Alves5136dda2022-03-25 12:26:38 +0000820 to_ret = vm_notifications_state_get_pending(&to_get->global);
821 to_ret |=
822 vm_notifications_state_get_pending(&to_get->per_vcpu[vcpu_id]);
J-Alvesaa79c012021-07-09 14:29:45 +0100823
824 return to_ret;
825}
J-Alvesc8e8a222021-06-08 17:33:52 +0100826
827/**
J-Alves663682a2022-03-25 13:56:51 +0000828 * Get pending framework notifications.
829 */
830ffa_notifications_bitmap_t vm_notifications_framework_get_pending(
831 struct vm_locked vm_locked)
832{
Federico Recanati6c1e05c2022-04-20 11:37:26 +0200833 struct vm *vm = vm_locked.vm;
834 ffa_notifications_bitmap_t framework;
Federico Recanati6c1e05c2022-04-20 11:37:26 +0200835
836 assert(vm != NULL);
837
838 framework = vm_notifications_state_get_pending(
839 &vm->notifications.framework);
840
Federico Recanati6c1e05c2022-04-20 11:37:26 +0200841 return framework;
J-Alves663682a2022-03-25 13:56:51 +0000842}
843
J-Alves17c9b6d2022-03-25 14:39:05 +0000844static void vm_notifications_state_info_get(
845 struct notifications_state *state, ffa_vm_id_t vm_id, bool is_per_vcpu,
846 ffa_vcpu_index_t vcpu_id, uint16_t *ids, uint32_t *ids_count,
847 uint32_t *lists_sizes, uint32_t *lists_count,
848 const uint32_t ids_max_count,
849 enum notifications_info_get_state *info_get_state)
850{
851 ffa_notifications_bitmap_t pending_not_retrieved;
852
853 CHECK(*ids_count <= ids_max_count);
854 CHECK(*lists_count <= ids_max_count);
855
856 if (*info_get_state == FULL) {
857 return;
858 }
859
860 pending_not_retrieved = state->pending & ~state->info_get_retrieved;
861
862 /* No notifications pending that haven't been retrieved. */
863 if (pending_not_retrieved == 0U) {
864 return;
865 }
866
867 if (*ids_count == ids_max_count) {
868 *info_get_state = FULL;
869 return;
870 }
871
872 switch (*info_get_state) {
873 case INIT:
874 case STARTING_NEW:
875 /*
876 * At this iteration two ids are to be added: the VM ID
877 * and vCPU ID. If there is no space, change state and
878 * terminate function.
879 */
880 if (is_per_vcpu && ids_max_count - *ids_count < 2) {
881 *info_get_state = FULL;
882 return;
883 }
884
885 *info_get_state = INSERTING;
886 ids[*ids_count] = vm_id;
887 ++(*ids_count);
888
889 if (is_per_vcpu) {
890 /* Insert vCPU ID. */
891 ids[*ids_count] = vcpu_id;
892 ++(*ids_count);
893 ++lists_sizes[*lists_count];
894 }
895
896 ++(*lists_count);
897 break;
898 case INSERTING:
899 /* For per-vCPU notifications only. */
900 if (!is_per_vcpu) {
901 break;
902 }
903
904 /* Insert vCPU ID */
905 ids[*ids_count] = vcpu_id;
906 (*ids_count)++;
907 /* Increment respective list size */
908 ++lists_sizes[*lists_count - 1];
909
910 if (lists_sizes[*lists_count - 1] == 3) {
911 *info_get_state = STARTING_NEW;
912 }
913 break;
914 default:
915 panic("Notification info get action error!!\n");
916 }
917
918 state->info_get_retrieved |= pending_not_retrieved;
919
920 vm_notifications_info_get_retrieved_count_add(pending_not_retrieved);
921}
922
J-Alves663682a2022-03-25 13:56:51 +0000923/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100924 * Get pending notification's information to return to the receiver scheduler.
925 */
926void vm_notifications_info_get_pending(
927 struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids,
928 uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count,
929 const uint32_t ids_max_count,
930 enum notifications_info_get_state *info_get_state)
931{
J-Alves17c9b6d2022-03-25 14:39:05 +0000932 struct notifications *notifications;
J-Alvesc8e8a222021-06-08 17:33:52 +0100933
934 CHECK(vm_locked.vm != NULL);
J-Alvesc8e8a222021-06-08 17:33:52 +0100935
J-Alves17c9b6d2022-03-25 14:39:05 +0000936 notifications = vm_get_notifications(vm_locked, is_from_vm);
J-Alvesc8e8a222021-06-08 17:33:52 +0100937
J-Alves17c9b6d2022-03-25 14:39:05 +0000938 /*
939 * Perform info get for global notifications, before doing it for
940 * per-vCPU.
941 */
942 vm_notifications_state_info_get(&notifications->global,
943 vm_locked.vm->id, false, 0, ids,
944 ids_count, lists_sizes, lists_count,
945 ids_max_count, info_get_state);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100946
J-Alvesc8e8a222021-06-08 17:33:52 +0100947 for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) {
J-Alves17c9b6d2022-03-25 14:39:05 +0000948 vm_notifications_state_info_get(
949 &notifications->per_vcpu[i], vm_locked.vm->id, true, i,
950 ids, ids_count, lists_sizes, lists_count, ids_max_count,
951 info_get_state);
J-Alvesc8e8a222021-06-08 17:33:52 +0100952 }
953}
954
955/**
956 * Gets all info from VM's pending notifications.
957 * Returns true if the list is full, and there is more pending.
958 */
959bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids,
960 uint32_t *ids_count, uint32_t *lists_sizes,
961 uint32_t *lists_count,
962 const uint32_t ids_max_count)
963{
964 enum notifications_info_get_state current_state = INIT;
965
J-Alvesf31940e2022-03-25 17:24:00 +0000966 /* Get info of pending notifications from the framework. */
967 vm_notifications_state_info_get(&vm_locked.vm->notifications.framework,
968 vm_locked.vm->id, false, 0, ids,
969 ids_count, lists_sizes, lists_count,
970 ids_max_count, &current_state);
971
972 /* Get info of pending notifications from SPs. */
J-Alvesc8e8a222021-06-08 17:33:52 +0100973 vm_notifications_info_get_pending(vm_locked, false, ids, ids_count,
974 lists_sizes, lists_count,
975 ids_max_count, &current_state);
976
J-Alvesf31940e2022-03-25 17:24:00 +0000977 /* Get info of pending notifications from VMs. */
J-Alvesc8e8a222021-06-08 17:33:52 +0100978 vm_notifications_info_get_pending(vm_locked, true, ids, ids_count,
979 lists_sizes, lists_count,
980 ids_max_count, &current_state);
981
982 /*
983 * State transitions to FULL when trying to insert a new ID in the
984 * list and there is not more space. This means there are notifications
985 * pending, whose info is not retrieved.
986 */
987 return current_state == FULL;
988}
J-Alves439ac972021-11-18 17:32:03 +0000989
990/**
991 * Checks VM's messaging method support.
992 */
993bool vm_supports_messaging_method(struct vm *vm, uint8_t msg_method)
994{
995 return (vm->messaging_method & msg_method) != 0;
996}
J-Alves6e2abc62021-12-02 14:58:56 +0000997
998void vm_notifications_set_npi_injected(struct vm_locked vm_locked,
999 bool npi_injected)
1000{
1001 vm_locked.vm->notifications.npi_injected = npi_injected;
1002}
1003
1004bool vm_notifications_is_npi_injected(struct vm_locked vm_locked)
1005{
1006 return vm_locked.vm->notifications.npi_injected;
1007}
J-Alves7e67d102022-04-13 13:22:39 +01001008
1009/**
1010 * Sets the designated GP register that the VM expects to receive the boot
1011 * info's address.
1012 */
1013void vm_set_boot_info_gp_reg(struct vm *vm, struct vcpu *vcpu)
1014{
Olivier Deprezb2808332023-02-02 15:25:40 +01001015 if (vm->boot_info.blob_addr.ipa != 0U) {
J-Alves7e67d102022-04-13 13:22:39 +01001016 arch_regs_set_gp_reg(&vcpu->regs,
1017 ipa_addr(vm->boot_info.blob_addr),
1018 vm->boot_info.gp_register_num);
1019 }
1020}