blob: 43a52f21a64e948cf24f1ed48fd78f11974394f5 [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;
J-Alvesb37fd082020-10-22 12:29:21 +010026static struct vm *first_boot_vm;
Andrew Scull19503262018-09-20 14:48:39 +010027
J-Alvesfe23ebe2021-10-13 16:07:07 +010028/**
29 * Counters on the status of notifications in the system. It helps to improve
30 * the information retrieved by the receiver scheduler.
31 */
32static struct {
33 /** Counts notifications pending. */
34 uint32_t pending_count;
35 /**
36 * Counts notifications pending, that have been retrieved by the
37 * receiver scheduler.
38 */
39 uint32_t info_get_retrieved_count;
40 struct spinlock lock;
41} all_notifications_state;
42
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080043static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
44{
45 if (vm->el0_partition) {
46 return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
47 ppool);
48 }
49 return mm_vm_init(&vm->ptable, vm->id, ppool);
50}
51
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010052struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080053 struct mpool *ppool, bool el0_partition)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010055 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010056 struct vm *vm;
57
Olivier Deprez96a2a262020-06-11 17:21:38 +020058 if (id == HF_OTHER_WORLD_ID) {
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080059 CHECK(el0_partition == false);
Olivier Deprez96a2a262020-06-11 17:21:38 +020060 vm = &other_world;
Andrew Walbran9daa57e2019-09-27 13:33:20 +010061 } else {
62 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010063
Andrew Walbran9daa57e2019-09-27 13:33:20 +010064 CHECK(id >= HF_VM_ID_OFFSET);
65 CHECK(vm_index < ARRAY_SIZE(vms));
66 vm = &vms[vm_index];
67 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010068
Andrew Scull2b5fbad2019-04-05 13:55:56 +010069 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000071 list_init(&vm->mailbox.waiter_list);
72 list_init(&vm->mailbox.ready_list);
73 sl_init(&vm->lock);
74
Andrew Walbran9daa57e2019-09-27 13:33:20 +010075 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010076 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010077 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000078 atomic_init(&vm->aborting, false);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080079 vm->el0_partition = el0_partition;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080081 if (!vm_init_mm(vm, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010082 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000083 }
84
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000085 /* Initialise waiter entries. */
86 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000087 vm->wait_entries[i].waiting_vm = vm;
88 list_init(&vm->wait_entries[i].wait_links);
89 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000090 }
91
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000092 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010093 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010094 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010095 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096
J-Alves4ef6e842021-03-18 12:47:01 +000097 /* Basic initialization of the notifications structure. */
98 vm_notifications_init_bindings(&vm->notifications.from_sp);
99 vm_notifications_init_bindings(&vm->notifications.from_vm);
100
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100101 return vm;
102}
103
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100104bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800105 struct vm **new_vm, bool el0_partition)
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100106{
107 if (vm_count >= MAX_VMS) {
108 return false;
109 }
110
111 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800112 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool,
113 el0_partition);
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100114 if (*new_vm == NULL) {
115 return false;
116 }
Andrew Scull19503262018-09-20 14:48:39 +0100117 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +0100118
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000119 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100120}
121
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100122ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +0100123{
124 return vm_count;
125}
126
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100127/**
128 * Returns a pointer to the VM with the corresponding id.
129 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100130struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +0100131{
David Brazdilbc501192019-09-27 13:20:56 +0100132 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +0100133
Olivier Deprez96a2a262020-06-11 17:21:38 +0200134 if (id == HF_OTHER_WORLD_ID) {
135 if (other_world.id == HF_OTHER_WORLD_ID) {
136 return &other_world;
137 }
Andrew Scull19503262018-09-20 14:48:39 +0100138 return NULL;
139 }
140
Olivier Deprez96a2a262020-06-11 17:21:38 +0200141 /* Check that this is not a reserved ID. */
142 if (id < HF_VM_ID_OFFSET) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100143 return NULL;
144 }
145
David Brazdilbc501192019-09-27 13:20:56 +0100146 index = id - HF_VM_ID_OFFSET;
147
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100148 return vm_find_index(index);
149}
150
151/**
J-Alves46ee0682021-07-26 15:17:53 +0100152 * Returns a locked instance of the VM with the corresponding id.
153 */
154struct vm_locked vm_find_locked(ffa_vm_id_t id)
155{
156 struct vm *vm = vm_find(id);
157
158 if (vm != NULL) {
159 return vm_lock(vm);
160 }
161
162 return (struct vm_locked){.vm = NULL};
163}
164
165/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100166 * Returns a pointer to the VM at the specified index.
167 */
168struct vm *vm_find_index(uint16_t index)
169{
David Brazdilbc501192019-09-27 13:20:56 +0100170 /* Ensure the VM is initialized. */
171 if (index >= vm_count) {
172 return NULL;
173 }
174
175 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100176}
177
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000178/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000179 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000180 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100181struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000182{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100183 struct vm_locked locked = {
184 .vm = vm,
185 };
186
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000187 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100188
189 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000190}
191
192/**
Jose Marinho75509b42019-04-09 09:34:59 +0100193 * Locks two VMs ensuring that the locking order is according to the locks'
194 * addresses.
195 */
196struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
197{
198 struct two_vm_locked dual_lock;
199
200 sl_lock_both(&vm1->lock, &vm2->lock);
201 dual_lock.vm1.vm = vm1;
202 dual_lock.vm2.vm = vm2;
203
204 return dual_lock;
205}
206
207/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000208 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
209 * the fact that the VM is no longer locked.
210 */
211void vm_unlock(struct vm_locked *locked)
212{
213 sl_unlock(&locked->vm->lock);
214 locked->vm = NULL;
215}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100216
217/**
218 * Get the vCPU with the given index from the given VM.
219 * This assumes the index is valid, i.e. less than vm->vcpu_count.
220 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100221struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100222{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100223 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100224 return &vm->vcpus[vcpu_index];
225}
Andrew Scull3c257452019-11-26 13:32:50 +0000226
227/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000228 * Gets `vm`'s wait entry for waiting on the `for_vm`.
229 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100230struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000231{
232 uint16_t index;
233
234 CHECK(for_vm >= HF_VM_ID_OFFSET);
235 index = for_vm - HF_VM_ID_OFFSET;
236 CHECK(index < MAX_VMS);
237
238 return &vm->wait_entries[index];
239}
240
241/**
242 * Gets the ID of the VM which the given VM's wait entry is for.
243 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100244ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000245{
246 uint16_t index = entry - vm->wait_entries;
247
248 return index + HF_VM_ID_OFFSET;
249}
250
251/**
Andrew Walbran45633dd2020-10-07 17:59:54 +0100252 * Return whether the given VM ID represents an entity in the current world:
253 * i.e. the hypervisor or a normal world VM when running in the normal world, or
254 * the SPM or an SP when running in the secure world.
255 */
256bool vm_id_is_current_world(ffa_vm_id_t vm_id)
257{
258 return (vm_id & HF_VM_ID_WORLD_MASK) !=
259 (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK);
260}
261
262/**
Andrew Scull3c257452019-11-26 13:32:50 +0000263 * Map a range of addresses to the VM in both the MMU and the IOMMU.
264 *
265 * mm_vm_defrag should always be called after a series of page table updates,
266 * whether they succeed or fail. This is because on failure extra page table
267 * entries may have been allocated and then not used, while on success it may be
268 * possible to compact the page table by merging several entries into a block.
269 *
270 * Returns true on success, or false if the update failed and no changes were
271 * made.
272 *
273 */
274bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
275 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
276{
277 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
278 return false;
279 }
280
281 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
282
283 return true;
284}
285
286/**
287 * Prepares the given VM for the given address mapping such that it will be able
288 * to commit the change without failure.
289 *
290 * In particular, multiple calls to this function will result in the
291 * corresponding calls to commit the changes to succeed.
292 *
293 * Returns true on success, or false if the update failed and no changes were
294 * made.
295 */
296bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
297 uint32_t mode, struct mpool *ppool)
298{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800299 if (vm_locked.vm->el0_partition) {
300 return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
301 mode, ppool);
302 }
Andrew Scull3c257452019-11-26 13:32:50 +0000303 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
304 ppool);
305}
306
307/**
308 * Commits the given address mapping to the VM assuming the operation cannot
309 * fail. `vm_identity_prepare` must used correctly before this to ensure
310 * this condition.
311 */
312void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
313 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
314{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800315 if (vm_locked.vm->el0_partition) {
316 mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
317 ppool);
318 if (ipa != NULL) {
319 /*
320 * EL0 partitions are modeled as lightweight VM's, to
321 * promote code reuse. The below statement returns the
322 * mapped PA as an IPA, however, for an EL0 partition,
323 * this is really a VA.
324 */
325 *ipa = ipa_from_pa(begin);
326 }
327 } else {
328 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
329 ppool, ipa);
330 }
Andrew Scull3c257452019-11-26 13:32:50 +0000331 plat_iommu_identity_map(vm_locked, begin, end, mode);
332}
333
334/**
335 * Unmap a range of addresses from the VM.
336 *
337 * Returns true on success, or false if the update failed and no changes were
338 * made.
339 */
340bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
341 struct mpool *ppool)
342{
343 uint32_t mode = MM_MODE_UNMAPPED_MASK;
344
345 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
346}
347
348/**
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700349 * Defrag page tables for an EL0 partition or for a VM.
350 */
351void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool)
352{
353 if (vm_locked.vm->el0_partition) {
354 mm_stage1_defrag(&vm_locked.vm->ptable, ppool);
355 } else {
356 mm_vm_defrag(&vm_locked.vm->ptable, ppool);
357 }
358}
359
360/**
Andrew Scull3c257452019-11-26 13:32:50 +0000361 * Unmaps the hypervisor pages from the given page table.
362 */
363bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
364{
365 /* TODO: If we add pages dynamically, they must be included here too. */
366 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
367 ppool) &&
368 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
369 ppool) &&
370 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
371 ppool);
372}
J-Alvesb37fd082020-10-22 12:29:21 +0100373
374/**
375 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
376 */
377struct vm *vm_get_first_boot(void)
378{
379 return first_boot_vm;
380}
381
382/**
383 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
384 * and rooted in `first_boot_vm`.
385 */
386void vm_update_boot(struct vm *vm)
387{
388 struct vm *current = NULL;
389 struct vm *previous = NULL;
390
391 if (first_boot_vm == NULL) {
392 first_boot_vm = vm;
393 return;
394 }
395
396 current = first_boot_vm;
397
J-Alvesbeeb6dc2021-12-08 18:21:32 +0000398 while (current != NULL && current->boot_order <= vm->boot_order) {
J-Alvesb37fd082020-10-22 12:29:21 +0100399 previous = current;
400 current = current->next_boot;
401 }
402
403 if (previous != NULL) {
404 previous->next_boot = vm;
405 } else {
406 first_boot_vm = vm;
407 }
408
409 vm->next_boot = current;
410}
J-Alves4ef6e842021-03-18 12:47:01 +0000411
Raghu Krishnamurthyea195fa2021-02-12 23:29:00 -0800412/**
413 * Gets the mode of the given range of ipa or va if they are mapped with the
414 * same mode.
415 *
416 * Returns true if the range is mapped with the same mode and false otherwise.
417 * The wrapper calls the appropriate mm function depending on if the partition
418 * is a vm or a el0 partition.
419 */
420bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end,
421 uint32_t *mode)
422{
423 if (vm_locked.vm->el0_partition) {
424 return mm_get_mode(&vm_locked.vm->ptable,
425 va_from_pa(pa_from_ipa(begin)),
426 va_from_pa(pa_from_ipa(end)), mode);
427 }
428 return mm_vm_get_mode(&vm_locked.vm->ptable, begin, end, mode);
429}
J-Alvesa0f317d2021-06-09 13:31:59 +0100430
J-Alves7461ef22021-10-18 17:21:33 +0100431static struct notifications *vm_get_notifications(struct vm_locked vm_locked,
432 bool is_from_vm)
433{
434 return is_from_vm ? &vm_locked.vm->notifications.from_vm
435 : &vm_locked.vm->notifications.from_sp;
436}
437
J-Alvesa0f317d2021-06-09 13:31:59 +0100438/*
439 * Initializes the notifications structure.
440 */
441void vm_notifications_init_bindings(struct notifications *notifications)
442{
443 for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) {
444 notifications->bindings_sender_id[i] = HF_INVALID_VM_ID;
445 }
446}
447
448/**
449 * Checks if there are pending notifications.
450 */
451bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm,
452 ffa_notifications_bitmap_t notifications)
453{
454 struct notifications *to_check;
455
456 CHECK(vm_locked.vm != NULL);
457
J-Alves7461ef22021-10-18 17:21:33 +0100458 to_check = vm_get_notifications(vm_locked, from_vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100459
460 /* Check if there are pending per vcpu notifications */
461 for (uint32_t i = 0U; i < MAX_CPUS; i++) {
462 if ((to_check->per_vcpu[i].pending & notifications) != 0U) {
463 return true;
464 }
465 }
466
467 /* Check if there are global pending notifications */
468 return (to_check->global.pending & notifications) != 0U;
469}
J-Alvesc003a7a2021-03-18 13:06:53 +0000470
J-Alves7461ef22021-10-18 17:21:33 +0100471/**
472 * Checks if there are pending global notifications, either from SPs or from
473 * VMs.
474 */
475bool vm_are_global_notifications_pending(struct vm_locked vm_locked)
476{
477 return vm_get_notifications(vm_locked, true)->global.pending != 0ULL ||
478 vm_get_notifications(vm_locked, false)->global.pending != 0ULL;
479}
480
481/**
482 * Checks if there are pending per-vCPU notifications, in a specific vCPU either
483 * from SPs or from VMs.
484 */
485bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked,
486 ffa_vcpu_index_t vcpu_id)
487{
488 CHECK(vcpu_id < MAX_CPUS);
489
490 return vm_get_notifications(vm_locked, true)
491 ->per_vcpu[vcpu_id]
492 .pending != 0ULL ||
493 vm_get_notifications(vm_locked, false)
494 ->per_vcpu[vcpu_id]
495 .pending != 0ULL;
496}
497
J-Alves09ff9d82021-11-02 11:55:20 +0000498bool vm_are_notifications_enabled(struct vm *vm)
J-Alvesc003a7a2021-03-18 13:06:53 +0000499{
J-Alves09ff9d82021-11-02 11:55:20 +0000500 return vm->notifications.enabled == true;
501}
502
503bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked)
504{
505 return vm_are_notifications_enabled(vm_locked.vm);
J-Alvesc003a7a2021-03-18 13:06:53 +0000506}
507
508static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications,
509 uint32_t i)
510{
511 return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U;
512}
513
J-Alvesfe23ebe2021-10-13 16:07:07 +0100514static void vm_notifications_global_state_count_update(
515 ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc)
516{
517 /*
518 * Helper to increment counters from global notifications
519 * state. Count update by increments or decrements of 1 or -1,
520 * respectively.
521 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000522 assert(inc == 1 || inc == -1);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100523
524 sl_lock(&all_notifications_state.lock);
525
526 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
527 if (vm_is_notification_bit_set(bitmap, i)) {
528 CHECK((inc > 0 && *counter < UINT32_MAX) ||
529 (inc < 0 && *counter > 0));
530 *counter += inc;
531 }
532 }
533
534 sl_unlock(&all_notifications_state.lock);
535}
536
537/**
538 * Helper function to increment the pending notifications based on a bitmap
539 * passed as argument.
540 * Function to be used at setting notifications for a given VM.
541 */
542static void vm_notifications_pending_count_add(
543 ffa_notifications_bitmap_t to_add)
544{
545 vm_notifications_global_state_count_update(
546 to_add, &all_notifications_state.pending_count, 1);
547}
548
549/**
550 * Helper function to decrement the pending notifications count.
551 * Function to be used when getting the receiver's pending notifications.
552 */
553static void vm_notifications_pending_count_sub(
554 ffa_notifications_bitmap_t to_sub)
555{
556 vm_notifications_global_state_count_update(
557 to_sub, &all_notifications_state.pending_count, -1);
558}
559
560/**
561 * Helper function to count the notifications whose information has been
562 * retrieved by the scheduler of the system, and are still pending.
563 */
564static void vm_notifications_info_get_retrieved_count_add(
565 ffa_notifications_bitmap_t to_add)
566{
567 vm_notifications_global_state_count_update(
568 to_add, &all_notifications_state.info_get_retrieved_count, 1);
569}
570
571/**
572 * Helper function to subtract the notifications that the receiver is getting
573 * and whose information has been retrieved by the receiver scheduler.
574 */
575static void vm_notifications_info_get_retrieved_count_sub(
576 ffa_notifications_bitmap_t to_sub)
577{
578 vm_notifications_global_state_count_update(
579 to_sub, &all_notifications_state.info_get_retrieved_count, -1);
580}
581
582/**
583 * Helper function to determine if there are notifications pending whose info
584 * hasn't been retrieved by the receiver scheduler.
585 */
586bool vm_notifications_pending_not_retrieved_by_scheduler(void)
587{
588 bool ret;
589
590 sl_lock(&all_notifications_state.lock);
591 ret = all_notifications_state.pending_count >
592 all_notifications_state.info_get_retrieved_count;
593 sl_unlock(&all_notifications_state.lock);
594
595 return ret;
596}
597
598bool vm_is_notifications_pending_count_zero(void)
599{
600 bool ret;
601
602 sl_lock(&all_notifications_state.lock);
603 ret = all_notifications_state.pending_count == 0;
604 sl_unlock(&all_notifications_state.lock);
605
606 return ret;
607}
608
J-Alvesc003a7a2021-03-18 13:06:53 +0000609/**
610 * Checks that all provided notifications are bound to the specified sender, and
611 * are per VCPU or global, as specified.
612 */
613bool vm_notifications_validate_binding(struct vm_locked vm_locked,
614 bool is_from_vm, ffa_vm_id_t sender_id,
615 ffa_notifications_bitmap_t notifications,
616 bool is_per_vcpu)
617{
618 return vm_notifications_validate_bound_sender(
619 vm_locked, is_from_vm, sender_id, notifications) &&
620 vm_notifications_validate_per_vcpu(vm_locked, is_from_vm,
621 is_per_vcpu, notifications);
622}
623
624/**
625 * Update binds information in notification structure for the specified
626 * notifications.
627 */
628void vm_notifications_update_bindings(struct vm_locked vm_locked,
629 bool is_from_vm, ffa_vm_id_t sender_id,
630 ffa_notifications_bitmap_t notifications,
631 bool is_per_vcpu)
632{
633 CHECK(vm_locked.vm != NULL);
634 struct notifications *to_update =
635 vm_get_notifications(vm_locked, is_from_vm);
636
637 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
638 if (vm_is_notification_bit_set(notifications, i)) {
639 to_update->bindings_sender_id[i] = sender_id;
640 }
641 }
642
643 /*
644 * Set notifications if they are per VCPU, else clear them as they are
645 * global.
646 */
647 if (is_per_vcpu) {
648 to_update->bindings_per_vcpu |= notifications;
649 } else {
650 to_update->bindings_per_vcpu &= ~notifications;
651 }
652}
653
654bool vm_notifications_validate_bound_sender(
655 struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id,
656 ffa_notifications_bitmap_t notifications)
657{
658 CHECK(vm_locked.vm != NULL);
659 struct notifications *to_check =
660 vm_get_notifications(vm_locked, is_from_vm);
661
662 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
663 if (vm_is_notification_bit_set(notifications, i) &&
664 to_check->bindings_sender_id[i] != sender_id) {
665 return false;
666 }
667 }
668
669 return true;
670}
671
672bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked,
673 bool is_from_vm, bool is_per_vcpu,
674 ffa_notifications_bitmap_t notif)
675{
676 CHECK(vm_locked.vm != NULL);
677 struct notifications *to_check =
678 vm_get_notifications(vm_locked, is_from_vm);
679
680 return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U
681 : (to_check->bindings_per_vcpu & notif) == 0U;
682}
J-Alvesaa79c012021-07-09 14:29:45 +0100683
684void vm_notifications_set(struct vm_locked vm_locked, bool is_from_vm,
685 ffa_notifications_bitmap_t notifications,
686 ffa_vcpu_index_t vcpu_id, bool is_per_vcpu)
687{
688 CHECK(vm_locked.vm != NULL);
689 struct notifications *to_set =
690 vm_get_notifications(vm_locked, is_from_vm);
691 CHECK(vcpu_id < MAX_CPUS);
692
693 if (is_per_vcpu) {
694 to_set->per_vcpu[vcpu_id].pending |= notifications;
695 } else {
696 to_set->global.pending |= notifications;
697 }
J-Alvesfe23ebe2021-10-13 16:07:07 +0100698
699 /* Update count of notifications pending. */
700 vm_notifications_pending_count_add(notifications);
J-Alvesaa79c012021-07-09 14:29:45 +0100701}
702
703/**
704 * Get Global notifications and per CPU only of the current VCPU.
705 */
706ffa_notifications_bitmap_t vm_notifications_get_pending_and_clear(
707 struct vm_locked vm_locked, bool is_from_vm,
708 ffa_vcpu_index_t cur_vcpu_id)
709{
710 ffa_notifications_bitmap_t to_ret = 0;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100711 ffa_notifications_bitmap_t pending_and_info_get_retrieved;
J-Alvesaa79c012021-07-09 14:29:45 +0100712
713 CHECK(vm_locked.vm != NULL);
714 struct notifications *to_get =
715 vm_get_notifications(vm_locked, is_from_vm);
716 CHECK(cur_vcpu_id < MAX_CPUS);
717
718 to_ret |= to_get->global.pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100719
720 /* Update count of currently pending notifications in the system. */
721 vm_notifications_pending_count_sub(to_get->global.pending);
722
723 /*
724 * If notifications receiver is getting have been retrieved by the
725 * receiver scheduler, decrement those from respective count.
726 */
727 pending_and_info_get_retrieved =
728 to_get->global.pending & to_get->global.info_get_retrieved;
729
730 if (pending_and_info_get_retrieved != 0) {
731 vm_notifications_info_get_retrieved_count_sub(
732 pending_and_info_get_retrieved);
733 }
734
J-Alvesaa79c012021-07-09 14:29:45 +0100735 to_get->global.pending = 0U;
736 to_get->global.info_get_retrieved = 0U;
737
738 to_ret |= to_get->per_vcpu[cur_vcpu_id].pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100739
740 /*
741 * Update counts of notifications, this time for per-vCPU notifications.
742 */
743 vm_notifications_pending_count_sub(
744 to_get->per_vcpu[cur_vcpu_id].pending);
745
746 pending_and_info_get_retrieved =
747 to_get->per_vcpu[cur_vcpu_id].pending &
748 to_get->per_vcpu[cur_vcpu_id].info_get_retrieved;
749
750 if (pending_and_info_get_retrieved != 0) {
751 vm_notifications_info_get_retrieved_count_sub(
752 pending_and_info_get_retrieved);
753 }
754
J-Alvesaa79c012021-07-09 14:29:45 +0100755 to_get->per_vcpu[cur_vcpu_id].pending = 0U;
756 to_get->per_vcpu[cur_vcpu_id].info_get_retrieved = 0U;
757
758 return to_ret;
759}
J-Alvesc8e8a222021-06-08 17:33:52 +0100760
761/**
762 * Get pending notification's information to return to the receiver scheduler.
763 */
764void vm_notifications_info_get_pending(
765 struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids,
766 uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count,
767 const uint32_t ids_max_count,
768 enum notifications_info_get_state *info_get_state)
769{
770 ffa_notifications_bitmap_t pending_not_retrieved;
771
772 CHECK(vm_locked.vm != NULL);
773 struct notifications *notifications =
774 vm_get_notifications(vm_locked, is_from_vm);
775
776 if (*info_get_state == FULL) {
777 return;
778 }
779
780 CHECK(*ids_count <= ids_max_count);
781 CHECK(*lists_count <= ids_max_count);
782
783 pending_not_retrieved = notifications->global.pending &
784 ~notifications->global.info_get_retrieved;
785
786 if (pending_not_retrieved != 0U && *info_get_state == INIT) {
787 /*
788 * If action is to INIT, means that no list has been
789 * created for the given VM ID, which also means that global
790 * notifications are not represented in the list yet.
791 */
792 if (*ids_count == ids_max_count) {
793 *info_get_state = FULL;
794 return;
795 }
796
797 *info_get_state = INSERTING;
798
799 (*lists_count)++;
800 ids[*ids_count] = vm_locked.vm->id;
801 ++(*ids_count);
802 }
803
804 notifications->global.info_get_retrieved |= pending_not_retrieved;
805
J-Alvesfe23ebe2021-10-13 16:07:07 +0100806 vm_notifications_info_get_retrieved_count_add(pending_not_retrieved);
807
J-Alvesc8e8a222021-06-08 17:33:52 +0100808 for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) {
809 /*
810 * Include VCPU ID of per-VCPU notifications.
811 */
812 pending_not_retrieved =
813 notifications->per_vcpu[i].pending &
814 ~notifications->per_vcpu[i].info_get_retrieved;
815
816 if (pending_not_retrieved == 0U) {
817 continue;
818 }
819
820 switch (*info_get_state) {
821 case INIT:
822 case STARTING_NEW:
823 /*
824 * At this iteration two ids need to be added: the VM ID
825 * and VCPU ID. If there is not space, change state and
826 * terminate function.
827 */
828 if (ids_max_count - *ids_count < 2) {
829 *info_get_state = FULL;
830 return;
831 }
832
833 ids[*ids_count] = vm_locked.vm->id;
834 ++(*ids_count);
835
836 /* Insert VCPU ID */
837 ids[*ids_count] = i;
838 ++(*ids_count);
839
840 ++lists_sizes[*lists_count];
841 ++(*lists_count);
842
843 *info_get_state = INSERTING;
844 break;
845 case INSERTING:
846 if (*ids_count == ids_max_count) {
847 *info_get_state = FULL;
848 return;
849 }
850
851 /* Insert VCPU ID */
852 ids[*ids_count] = i;
853 (*ids_count)++;
854
855 /* Increment respective list size */
856 ++lists_sizes[*lists_count - 1];
857
858 if (lists_sizes[*lists_count - 1] == 3) {
859 *info_get_state = STARTING_NEW;
860 }
861 break;
862 default:
863 panic("Notification info get action error!!\n");
864 }
865
866 notifications->per_vcpu[i].info_get_retrieved |=
867 pending_not_retrieved;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100868
869 vm_notifications_info_get_retrieved_count_add(
870 pending_not_retrieved);
J-Alvesc8e8a222021-06-08 17:33:52 +0100871 }
872}
873
874/**
875 * Gets all info from VM's pending notifications.
876 * Returns true if the list is full, and there is more pending.
877 */
878bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids,
879 uint32_t *ids_count, uint32_t *lists_sizes,
880 uint32_t *lists_count,
881 const uint32_t ids_max_count)
882{
883 enum notifications_info_get_state current_state = INIT;
884
885 /* Get info of pending notifications from SPs */
886 vm_notifications_info_get_pending(vm_locked, false, ids, ids_count,
887 lists_sizes, lists_count,
888 ids_max_count, &current_state);
889
890 /* Get info of pending notifications from VMs */
891 vm_notifications_info_get_pending(vm_locked, true, ids, ids_count,
892 lists_sizes, lists_count,
893 ids_max_count, &current_state);
894
895 /*
896 * State transitions to FULL when trying to insert a new ID in the
897 * list and there is not more space. This means there are notifications
898 * pending, whose info is not retrieved.
899 */
900 return current_state == FULL;
901}