blob: 0b3c12483abf3ed0cd6182e3dd89b3b820890300 [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(),
Maksims Svecovs134b8f92022-03-04 15:14:09 +0000371 ppool) &&
372 vm_unmap(vm_locked, layout_stacks_begin(), layout_stacks_end(),
Andrew Scull3c257452019-11-26 13:32:50 +0000373 ppool);
374}
J-Alvesb37fd082020-10-22 12:29:21 +0100375
376/**
377 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
378 */
379struct vm *vm_get_first_boot(void)
380{
381 return first_boot_vm;
382}
383
384/**
385 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
386 * and rooted in `first_boot_vm`.
387 */
388void vm_update_boot(struct vm *vm)
389{
390 struct vm *current = NULL;
391 struct vm *previous = NULL;
392
393 if (first_boot_vm == NULL) {
394 first_boot_vm = vm;
395 return;
396 }
397
398 current = first_boot_vm;
399
J-Alvesbeeb6dc2021-12-08 18:21:32 +0000400 while (current != NULL && current->boot_order <= vm->boot_order) {
J-Alvesb37fd082020-10-22 12:29:21 +0100401 previous = current;
402 current = current->next_boot;
403 }
404
405 if (previous != NULL) {
406 previous->next_boot = vm;
407 } else {
408 first_boot_vm = vm;
409 }
410
411 vm->next_boot = current;
412}
J-Alves4ef6e842021-03-18 12:47:01 +0000413
Raghu Krishnamurthyea195fa2021-02-12 23:29:00 -0800414/**
415 * Gets the mode of the given range of ipa or va if they are mapped with the
416 * same mode.
417 *
418 * Returns true if the range is mapped with the same mode and false otherwise.
419 * The wrapper calls the appropriate mm function depending on if the partition
420 * is a vm or a el0 partition.
421 */
422bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end,
423 uint32_t *mode)
424{
425 if (vm_locked.vm->el0_partition) {
426 return mm_get_mode(&vm_locked.vm->ptable,
427 va_from_pa(pa_from_ipa(begin)),
428 va_from_pa(pa_from_ipa(end)), mode);
429 }
430 return mm_vm_get_mode(&vm_locked.vm->ptable, begin, end, mode);
431}
J-Alvesa0f317d2021-06-09 13:31:59 +0100432
J-Alves7461ef22021-10-18 17:21:33 +0100433static struct notifications *vm_get_notifications(struct vm_locked vm_locked,
434 bool is_from_vm)
435{
436 return is_from_vm ? &vm_locked.vm->notifications.from_vm
437 : &vm_locked.vm->notifications.from_sp;
438}
439
J-Alvesa0f317d2021-06-09 13:31:59 +0100440/*
441 * Initializes the notifications structure.
442 */
443void vm_notifications_init_bindings(struct notifications *notifications)
444{
445 for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) {
446 notifications->bindings_sender_id[i] = HF_INVALID_VM_ID;
447 }
448}
449
450/**
451 * Checks if there are pending notifications.
452 */
453bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm,
454 ffa_notifications_bitmap_t notifications)
455{
456 struct notifications *to_check;
457
458 CHECK(vm_locked.vm != NULL);
459
J-Alves7461ef22021-10-18 17:21:33 +0100460 to_check = vm_get_notifications(vm_locked, from_vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100461
462 /* Check if there are pending per vcpu notifications */
463 for (uint32_t i = 0U; i < MAX_CPUS; i++) {
464 if ((to_check->per_vcpu[i].pending & notifications) != 0U) {
465 return true;
466 }
467 }
468
469 /* Check if there are global pending notifications */
470 return (to_check->global.pending & notifications) != 0U;
471}
J-Alvesc003a7a2021-03-18 13:06:53 +0000472
J-Alves7461ef22021-10-18 17:21:33 +0100473/**
474 * Checks if there are pending global notifications, either from SPs or from
475 * VMs.
476 */
477bool vm_are_global_notifications_pending(struct vm_locked vm_locked)
478{
479 return vm_get_notifications(vm_locked, true)->global.pending != 0ULL ||
480 vm_get_notifications(vm_locked, false)->global.pending != 0ULL;
481}
482
483/**
484 * Checks if there are pending per-vCPU notifications, in a specific vCPU either
485 * from SPs or from VMs.
486 */
487bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked,
488 ffa_vcpu_index_t vcpu_id)
489{
490 CHECK(vcpu_id < MAX_CPUS);
491
492 return vm_get_notifications(vm_locked, true)
493 ->per_vcpu[vcpu_id]
494 .pending != 0ULL ||
495 vm_get_notifications(vm_locked, false)
496 ->per_vcpu[vcpu_id]
497 .pending != 0ULL;
498}
499
J-Alves09ff9d82021-11-02 11:55:20 +0000500bool vm_are_notifications_enabled(struct vm *vm)
J-Alvesc003a7a2021-03-18 13:06:53 +0000501{
J-Alves09ff9d82021-11-02 11:55:20 +0000502 return vm->notifications.enabled == true;
503}
504
505bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked)
506{
507 return vm_are_notifications_enabled(vm_locked.vm);
J-Alvesc003a7a2021-03-18 13:06:53 +0000508}
509
510static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications,
511 uint32_t i)
512{
513 return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U;
514}
515
J-Alvesfe23ebe2021-10-13 16:07:07 +0100516static void vm_notifications_global_state_count_update(
517 ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc)
518{
519 /*
520 * Helper to increment counters from global notifications
521 * state. Count update by increments or decrements of 1 or -1,
522 * respectively.
523 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000524 assert(inc == 1 || inc == -1);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100525
526 sl_lock(&all_notifications_state.lock);
527
528 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
529 if (vm_is_notification_bit_set(bitmap, i)) {
530 CHECK((inc > 0 && *counter < UINT32_MAX) ||
531 (inc < 0 && *counter > 0));
532 *counter += inc;
533 }
534 }
535
536 sl_unlock(&all_notifications_state.lock);
537}
538
539/**
540 * Helper function to increment the pending notifications based on a bitmap
541 * passed as argument.
542 * Function to be used at setting notifications for a given VM.
543 */
544static void vm_notifications_pending_count_add(
545 ffa_notifications_bitmap_t to_add)
546{
547 vm_notifications_global_state_count_update(
548 to_add, &all_notifications_state.pending_count, 1);
549}
550
551/**
552 * Helper function to decrement the pending notifications count.
553 * Function to be used when getting the receiver's pending notifications.
554 */
555static void vm_notifications_pending_count_sub(
556 ffa_notifications_bitmap_t to_sub)
557{
558 vm_notifications_global_state_count_update(
559 to_sub, &all_notifications_state.pending_count, -1);
560}
561
562/**
563 * Helper function to count the notifications whose information has been
564 * retrieved by the scheduler of the system, and are still pending.
565 */
566static void vm_notifications_info_get_retrieved_count_add(
567 ffa_notifications_bitmap_t to_add)
568{
569 vm_notifications_global_state_count_update(
570 to_add, &all_notifications_state.info_get_retrieved_count, 1);
571}
572
573/**
574 * Helper function to subtract the notifications that the receiver is getting
575 * and whose information has been retrieved by the receiver scheduler.
576 */
577static void vm_notifications_info_get_retrieved_count_sub(
578 ffa_notifications_bitmap_t to_sub)
579{
580 vm_notifications_global_state_count_update(
581 to_sub, &all_notifications_state.info_get_retrieved_count, -1);
582}
583
584/**
585 * Helper function to determine if there are notifications pending whose info
586 * hasn't been retrieved by the receiver scheduler.
587 */
588bool vm_notifications_pending_not_retrieved_by_scheduler(void)
589{
590 bool ret;
591
592 sl_lock(&all_notifications_state.lock);
593 ret = all_notifications_state.pending_count >
594 all_notifications_state.info_get_retrieved_count;
595 sl_unlock(&all_notifications_state.lock);
596
597 return ret;
598}
599
600bool vm_is_notifications_pending_count_zero(void)
601{
602 bool ret;
603
604 sl_lock(&all_notifications_state.lock);
605 ret = all_notifications_state.pending_count == 0;
606 sl_unlock(&all_notifications_state.lock);
607
608 return ret;
609}
610
J-Alvesc003a7a2021-03-18 13:06:53 +0000611/**
612 * Checks that all provided notifications are bound to the specified sender, and
613 * are per VCPU or global, as specified.
614 */
615bool vm_notifications_validate_binding(struct vm_locked vm_locked,
616 bool is_from_vm, ffa_vm_id_t sender_id,
617 ffa_notifications_bitmap_t notifications,
618 bool is_per_vcpu)
619{
620 return vm_notifications_validate_bound_sender(
621 vm_locked, is_from_vm, sender_id, notifications) &&
622 vm_notifications_validate_per_vcpu(vm_locked, is_from_vm,
623 is_per_vcpu, notifications);
624}
625
626/**
627 * Update binds information in notification structure for the specified
628 * notifications.
629 */
630void vm_notifications_update_bindings(struct vm_locked vm_locked,
631 bool is_from_vm, ffa_vm_id_t sender_id,
632 ffa_notifications_bitmap_t notifications,
633 bool is_per_vcpu)
634{
635 CHECK(vm_locked.vm != NULL);
636 struct notifications *to_update =
637 vm_get_notifications(vm_locked, is_from_vm);
638
639 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
640 if (vm_is_notification_bit_set(notifications, i)) {
641 to_update->bindings_sender_id[i] = sender_id;
642 }
643 }
644
645 /*
646 * Set notifications if they are per VCPU, else clear them as they are
647 * global.
648 */
649 if (is_per_vcpu) {
650 to_update->bindings_per_vcpu |= notifications;
651 } else {
652 to_update->bindings_per_vcpu &= ~notifications;
653 }
654}
655
656bool vm_notifications_validate_bound_sender(
657 struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id,
658 ffa_notifications_bitmap_t notifications)
659{
660 CHECK(vm_locked.vm != NULL);
661 struct notifications *to_check =
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_check->bindings_sender_id[i] != sender_id) {
667 return false;
668 }
669 }
670
671 return true;
672}
673
674bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked,
675 bool is_from_vm, bool is_per_vcpu,
676 ffa_notifications_bitmap_t notif)
677{
678 CHECK(vm_locked.vm != NULL);
679 struct notifications *to_check =
680 vm_get_notifications(vm_locked, is_from_vm);
681
682 return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U
683 : (to_check->bindings_per_vcpu & notif) == 0U;
684}
J-Alvesaa79c012021-07-09 14:29:45 +0100685
686void vm_notifications_set(struct vm_locked vm_locked, bool is_from_vm,
687 ffa_notifications_bitmap_t notifications,
688 ffa_vcpu_index_t vcpu_id, bool is_per_vcpu)
689{
690 CHECK(vm_locked.vm != NULL);
691 struct notifications *to_set =
692 vm_get_notifications(vm_locked, is_from_vm);
693 CHECK(vcpu_id < MAX_CPUS);
694
695 if (is_per_vcpu) {
696 to_set->per_vcpu[vcpu_id].pending |= notifications;
697 } else {
698 to_set->global.pending |= notifications;
699 }
J-Alvesfe23ebe2021-10-13 16:07:07 +0100700
701 /* Update count of notifications pending. */
702 vm_notifications_pending_count_add(notifications);
J-Alvesaa79c012021-07-09 14:29:45 +0100703}
704
J-Alves5136dda2022-03-25 12:26:38 +0000705static ffa_notifications_bitmap_t vm_notifications_state_get_pending(
706 struct notifications_state *state)
J-Alvesaa79c012021-07-09 14:29:45 +0100707{
J-Alves5136dda2022-03-25 12:26:38 +0000708 ffa_notifications_bitmap_t to_ret;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100709 ffa_notifications_bitmap_t pending_and_info_get_retrieved;
J-Alvesaa79c012021-07-09 14:29:45 +0100710
J-Alves5136dda2022-03-25 12:26:38 +0000711 assert(state != NULL);
J-Alvesaa79c012021-07-09 14:29:45 +0100712
J-Alves5136dda2022-03-25 12:26:38 +0000713 to_ret = state->pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100714
715 /* Update count of currently pending notifications in the system. */
J-Alves5136dda2022-03-25 12:26:38 +0000716 vm_notifications_pending_count_sub(state->pending);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100717
718 /*
719 * If notifications receiver is getting have been retrieved by the
720 * receiver scheduler, decrement those from respective count.
721 */
722 pending_and_info_get_retrieved =
J-Alves5136dda2022-03-25 12:26:38 +0000723 state->pending & state->info_get_retrieved;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100724
725 if (pending_and_info_get_retrieved != 0) {
726 vm_notifications_info_get_retrieved_count_sub(
727 pending_and_info_get_retrieved);
728 }
729
J-Alves5136dda2022-03-25 12:26:38 +0000730 state->pending = 0U;
731 state->info_get_retrieved = 0U;
J-Alvesaa79c012021-07-09 14:29:45 +0100732
J-Alves5136dda2022-03-25 12:26:38 +0000733 return to_ret;
734}
J-Alvesfe23ebe2021-10-13 16:07:07 +0100735
J-Alves5136dda2022-03-25 12:26:38 +0000736/**
737 * Get global and per-vCPU notifications for the given vCPU ID.
738 */
739ffa_notifications_bitmap_t vm_notifications_partition_get_pending(
740 struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id)
741{
742 ffa_notifications_bitmap_t to_ret;
743 struct notifications *to_get;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100744
J-Alves5136dda2022-03-25 12:26:38 +0000745 assert(vm_locked.vm != NULL);
746 to_get = vm_get_notifications(vm_locked, is_from_vm);
747 assert(vcpu_id < MAX_CPUS);
J-Alvesfe23ebe2021-10-13 16:07:07 +0100748
J-Alves5136dda2022-03-25 12:26:38 +0000749 to_ret = vm_notifications_state_get_pending(&to_get->global);
750 to_ret |=
751 vm_notifications_state_get_pending(&to_get->per_vcpu[vcpu_id]);
J-Alvesaa79c012021-07-09 14:29:45 +0100752
753 return to_ret;
754}
J-Alvesc8e8a222021-06-08 17:33:52 +0100755
756/**
757 * Get pending notification's information to return to the receiver scheduler.
758 */
759void vm_notifications_info_get_pending(
760 struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids,
761 uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count,
762 const uint32_t ids_max_count,
763 enum notifications_info_get_state *info_get_state)
764{
765 ffa_notifications_bitmap_t pending_not_retrieved;
766
767 CHECK(vm_locked.vm != NULL);
768 struct notifications *notifications =
769 vm_get_notifications(vm_locked, is_from_vm);
770
771 if (*info_get_state == FULL) {
772 return;
773 }
774
775 CHECK(*ids_count <= ids_max_count);
776 CHECK(*lists_count <= ids_max_count);
777
778 pending_not_retrieved = notifications->global.pending &
779 ~notifications->global.info_get_retrieved;
780
781 if (pending_not_retrieved != 0U && *info_get_state == INIT) {
782 /*
783 * If action is to INIT, means that no list has been
784 * created for the given VM ID, which also means that global
785 * notifications are not represented in the list yet.
786 */
787 if (*ids_count == ids_max_count) {
788 *info_get_state = FULL;
789 return;
790 }
791
792 *info_get_state = INSERTING;
793
794 (*lists_count)++;
795 ids[*ids_count] = vm_locked.vm->id;
796 ++(*ids_count);
797 }
798
799 notifications->global.info_get_retrieved |= pending_not_retrieved;
800
J-Alvesfe23ebe2021-10-13 16:07:07 +0100801 vm_notifications_info_get_retrieved_count_add(pending_not_retrieved);
802
J-Alvesc8e8a222021-06-08 17:33:52 +0100803 for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) {
804 /*
805 * Include VCPU ID of per-VCPU notifications.
806 */
807 pending_not_retrieved =
808 notifications->per_vcpu[i].pending &
809 ~notifications->per_vcpu[i].info_get_retrieved;
810
811 if (pending_not_retrieved == 0U) {
812 continue;
813 }
814
815 switch (*info_get_state) {
816 case INIT:
817 case STARTING_NEW:
818 /*
819 * At this iteration two ids need to be added: the VM ID
820 * and VCPU ID. If there is not space, change state and
821 * terminate function.
822 */
823 if (ids_max_count - *ids_count < 2) {
824 *info_get_state = FULL;
825 return;
826 }
827
828 ids[*ids_count] = vm_locked.vm->id;
829 ++(*ids_count);
830
831 /* Insert VCPU ID */
832 ids[*ids_count] = i;
833 ++(*ids_count);
834
835 ++lists_sizes[*lists_count];
836 ++(*lists_count);
837
838 *info_get_state = INSERTING;
839 break;
840 case INSERTING:
841 if (*ids_count == ids_max_count) {
842 *info_get_state = FULL;
843 return;
844 }
845
846 /* Insert VCPU ID */
847 ids[*ids_count] = i;
848 (*ids_count)++;
849
850 /* Increment respective list size */
851 ++lists_sizes[*lists_count - 1];
852
853 if (lists_sizes[*lists_count - 1] == 3) {
854 *info_get_state = STARTING_NEW;
855 }
856 break;
857 default:
858 panic("Notification info get action error!!\n");
859 }
860
861 notifications->per_vcpu[i].info_get_retrieved |=
862 pending_not_retrieved;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100863
864 vm_notifications_info_get_retrieved_count_add(
865 pending_not_retrieved);
J-Alvesc8e8a222021-06-08 17:33:52 +0100866 }
867}
868
869/**
870 * Gets all info from VM's pending notifications.
871 * Returns true if the list is full, and there is more pending.
872 */
873bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids,
874 uint32_t *ids_count, uint32_t *lists_sizes,
875 uint32_t *lists_count,
876 const uint32_t ids_max_count)
877{
878 enum notifications_info_get_state current_state = INIT;
879
880 /* Get info of pending notifications from SPs */
881 vm_notifications_info_get_pending(vm_locked, false, ids, ids_count,
882 lists_sizes, lists_count,
883 ids_max_count, &current_state);
884
885 /* Get info of pending notifications from VMs */
886 vm_notifications_info_get_pending(vm_locked, true, ids, ids_count,
887 lists_sizes, lists_count,
888 ids_max_count, &current_state);
889
890 /*
891 * State transitions to FULL when trying to insert a new ID in the
892 * list and there is not more space. This means there are notifications
893 * pending, whose info is not retrieved.
894 */
895 return current_state == FULL;
896}
J-Alves439ac972021-11-18 17:32:03 +0000897
898/**
899 * Checks VM's messaging method support.
900 */
901bool vm_supports_messaging_method(struct vm *vm, uint8_t msg_method)
902{
903 return (vm->messaging_method & msg_method) != 0;
904}
J-Alves6e2abc62021-12-02 14:58:56 +0000905
906void vm_notifications_set_npi_injected(struct vm_locked vm_locked,
907 bool npi_injected)
908{
909 vm_locked.vm->notifications.npi_injected = npi_injected;
910}
911
912bool vm_notifications_is_npi_injected(struct vm_locked vm_locked)
913{
914 return vm_locked.vm->notifications.npi_injected;
915}
J-Alves7e67d102022-04-13 13:22:39 +0100916
917/**
918 * Sets the designated GP register that the VM expects to receive the boot
919 * info's address.
920 */
921void vm_set_boot_info_gp_reg(struct vm *vm, struct vcpu *vcpu)
922{
923 if (!vm->initialized && vm->boot_info.blob_addr.ipa != 0U) {
924 arch_regs_set_gp_reg(&vcpu->regs,
925 ipa_addr(vm->boot_info.blob_addr),
926 vm->boot_info.gp_register_num);
927 }
928}