blob: e321121e5c52ac87b32ac040461a2f1bbd7d0b4c [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"
Andrew Scull877ae4b2019-07-02 12:52:33 +010012#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010013#include "hf/cpu.h"
J-Alves4ef6e842021-03-18 12:47:01 +000014#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010015#include "hf/ffa.h"
Andrew Scull3c257452019-11-26 13:32:50 +000016#include "hf/layout.h"
17#include "hf/plat/iommu.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010018#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010019
Andrew Scull19503262018-09-20 14:48:39 +010020#include "vmapi/hf/call.h"
21
22static struct vm vms[MAX_VMS];
Olivier Deprez96a2a262020-06-11 17:21:38 +020023static struct vm other_world;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010024static ffa_vm_count_t vm_count;
J-Alvesb37fd082020-10-22 12:29:21 +010025static struct vm *first_boot_vm;
Andrew Scull19503262018-09-20 14:48:39 +010026
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080027static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
28{
29 if (vm->el0_partition) {
30 return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
31 ppool);
32 }
33 return mm_vm_init(&vm->ptable, vm->id, ppool);
34}
35
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010036struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080037 struct mpool *ppool, bool el0_partition)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010039 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010040 struct vm *vm;
41
Olivier Deprez96a2a262020-06-11 17:21:38 +020042 if (id == HF_OTHER_WORLD_ID) {
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080043 CHECK(el0_partition == false);
Olivier Deprez96a2a262020-06-11 17:21:38 +020044 vm = &other_world;
Andrew Walbran9daa57e2019-09-27 13:33:20 +010045 } else {
46 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010047
Andrew Walbran9daa57e2019-09-27 13:33:20 +010048 CHECK(id >= HF_VM_ID_OFFSET);
49 CHECK(vm_index < ARRAY_SIZE(vms));
50 vm = &vms[vm_index];
51 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010052
Andrew Scull2b5fbad2019-04-05 13:55:56 +010053 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010054
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000055 list_init(&vm->mailbox.waiter_list);
56 list_init(&vm->mailbox.ready_list);
57 sl_init(&vm->lock);
58
Andrew Walbran9daa57e2019-09-27 13:33:20 +010059 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010060 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010061 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000062 atomic_init(&vm->aborting, false);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080063 vm->el0_partition = el0_partition;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080065 if (!vm_init_mm(vm, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010066 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000067 }
68
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000069 /* Initialise waiter entries. */
70 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000071 vm->wait_entries[i].waiting_vm = vm;
72 list_init(&vm->wait_entries[i].wait_links);
73 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000074 }
75
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000076 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010077 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010078 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010079 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080
J-Alves4ef6e842021-03-18 12:47:01 +000081 /* Basic initialization of the notifications structure. */
82 vm_notifications_init_bindings(&vm->notifications.from_sp);
83 vm_notifications_init_bindings(&vm->notifications.from_vm);
84
85 /* TODO: Enable in accordance to VM's manifest. */
86 vm->notifications.enabled = true;
87
Andrew Walbran9daa57e2019-09-27 13:33:20 +010088 return vm;
89}
90
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010091bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080092 struct vm **new_vm, bool el0_partition)
Andrew Walbran9daa57e2019-09-27 13:33:20 +010093{
94 if (vm_count >= MAX_VMS) {
95 return false;
96 }
97
98 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080099 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool,
100 el0_partition);
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100101 if (*new_vm == NULL) {
102 return false;
103 }
Andrew Scull19503262018-09-20 14:48:39 +0100104 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +0100105
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000106 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100107}
108
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100109ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +0100110{
111 return vm_count;
112}
113
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100114/**
115 * Returns a pointer to the VM with the corresponding id.
116 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100117struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +0100118{
David Brazdilbc501192019-09-27 13:20:56 +0100119 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +0100120
Olivier Deprez96a2a262020-06-11 17:21:38 +0200121 if (id == HF_OTHER_WORLD_ID) {
122 if (other_world.id == HF_OTHER_WORLD_ID) {
123 return &other_world;
124 }
Andrew Scull19503262018-09-20 14:48:39 +0100125 return NULL;
126 }
127
Olivier Deprez96a2a262020-06-11 17:21:38 +0200128 /* Check that this is not a reserved ID. */
129 if (id < HF_VM_ID_OFFSET) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100130 return NULL;
131 }
132
David Brazdilbc501192019-09-27 13:20:56 +0100133 index = id - HF_VM_ID_OFFSET;
134
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100135 return vm_find_index(index);
136}
137
138/**
139 * Returns a pointer to the VM at the specified index.
140 */
141struct vm *vm_find_index(uint16_t index)
142{
David Brazdilbc501192019-09-27 13:20:56 +0100143 /* Ensure the VM is initialized. */
144 if (index >= vm_count) {
145 return NULL;
146 }
147
148 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100149}
150
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000151/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000152 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000153 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100154struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000155{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100156 struct vm_locked locked = {
157 .vm = vm,
158 };
159
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000160 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100161
162 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000163}
164
165/**
Jose Marinho75509b42019-04-09 09:34:59 +0100166 * Locks two VMs ensuring that the locking order is according to the locks'
167 * addresses.
168 */
169struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
170{
171 struct two_vm_locked dual_lock;
172
173 sl_lock_both(&vm1->lock, &vm2->lock);
174 dual_lock.vm1.vm = vm1;
175 dual_lock.vm2.vm = vm2;
176
177 return dual_lock;
178}
179
180/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000181 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
182 * the fact that the VM is no longer locked.
183 */
184void vm_unlock(struct vm_locked *locked)
185{
186 sl_unlock(&locked->vm->lock);
187 locked->vm = NULL;
188}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100189
190/**
191 * Get the vCPU with the given index from the given VM.
192 * This assumes the index is valid, i.e. less than vm->vcpu_count.
193 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100194struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100195{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100196 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100197 return &vm->vcpus[vcpu_index];
198}
Andrew Scull3c257452019-11-26 13:32:50 +0000199
200/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000201 * Gets `vm`'s wait entry for waiting on the `for_vm`.
202 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100203struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000204{
205 uint16_t index;
206
207 CHECK(for_vm >= HF_VM_ID_OFFSET);
208 index = for_vm - HF_VM_ID_OFFSET;
209 CHECK(index < MAX_VMS);
210
211 return &vm->wait_entries[index];
212}
213
214/**
215 * Gets the ID of the VM which the given VM's wait entry is for.
216 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100217ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000218{
219 uint16_t index = entry - vm->wait_entries;
220
221 return index + HF_VM_ID_OFFSET;
222}
223
224/**
Andrew Walbran45633dd2020-10-07 17:59:54 +0100225 * Return whether the given VM ID represents an entity in the current world:
226 * i.e. the hypervisor or a normal world VM when running in the normal world, or
227 * the SPM or an SP when running in the secure world.
228 */
229bool vm_id_is_current_world(ffa_vm_id_t vm_id)
230{
231 return (vm_id & HF_VM_ID_WORLD_MASK) !=
232 (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK);
233}
234
235/**
Andrew Scull3c257452019-11-26 13:32:50 +0000236 * Map a range of addresses to the VM in both the MMU and the IOMMU.
237 *
238 * mm_vm_defrag should always be called after a series of page table updates,
239 * whether they succeed or fail. This is because on failure extra page table
240 * entries may have been allocated and then not used, while on success it may be
241 * possible to compact the page table by merging several entries into a block.
242 *
243 * Returns true on success, or false if the update failed and no changes were
244 * made.
245 *
246 */
247bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
248 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
249{
250 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
251 return false;
252 }
253
254 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
255
256 return true;
257}
258
259/**
260 * Prepares the given VM for the given address mapping such that it will be able
261 * to commit the change without failure.
262 *
263 * In particular, multiple calls to this function will result in the
264 * corresponding calls to commit the changes to succeed.
265 *
266 * Returns true on success, or false if the update failed and no changes were
267 * made.
268 */
269bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
270 uint32_t mode, struct mpool *ppool)
271{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800272 if (vm_locked.vm->el0_partition) {
273 return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
274 mode, ppool);
275 }
Andrew Scull3c257452019-11-26 13:32:50 +0000276 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
277 ppool);
278}
279
280/**
281 * Commits the given address mapping to the VM assuming the operation cannot
282 * fail. `vm_identity_prepare` must used correctly before this to ensure
283 * this condition.
284 */
285void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
286 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
287{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800288 if (vm_locked.vm->el0_partition) {
289 mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
290 ppool);
291 if (ipa != NULL) {
292 /*
293 * EL0 partitions are modeled as lightweight VM's, to
294 * promote code reuse. The below statement returns the
295 * mapped PA as an IPA, however, for an EL0 partition,
296 * this is really a VA.
297 */
298 *ipa = ipa_from_pa(begin);
299 }
300 } else {
301 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
302 ppool, ipa);
303 }
Andrew Scull3c257452019-11-26 13:32:50 +0000304 plat_iommu_identity_map(vm_locked, begin, end, mode);
305}
306
307/**
308 * Unmap a range of addresses from the VM.
309 *
310 * Returns true on success, or false if the update failed and no changes were
311 * made.
312 */
313bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
314 struct mpool *ppool)
315{
316 uint32_t mode = MM_MODE_UNMAPPED_MASK;
317
318 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
319}
320
321/**
322 * Unmaps the hypervisor pages from the given page table.
323 */
324bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
325{
326 /* TODO: If we add pages dynamically, they must be included here too. */
327 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
328 ppool) &&
329 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
330 ppool) &&
331 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
332 ppool);
333}
J-Alvesb37fd082020-10-22 12:29:21 +0100334
335/**
336 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
337 */
338struct vm *vm_get_first_boot(void)
339{
340 return first_boot_vm;
341}
342
343/**
344 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
345 * and rooted in `first_boot_vm`.
346 */
347void vm_update_boot(struct vm *vm)
348{
349 struct vm *current = NULL;
350 struct vm *previous = NULL;
351
352 if (first_boot_vm == NULL) {
353 first_boot_vm = vm;
354 return;
355 }
356
357 current = first_boot_vm;
358
359 while (current != NULL && current->boot_order >= vm->boot_order) {
360 previous = current;
361 current = current->next_boot;
362 }
363
364 if (previous != NULL) {
365 previous->next_boot = vm;
366 } else {
367 first_boot_vm = vm;
368 }
369
370 vm->next_boot = current;
371}
J-Alves4ef6e842021-03-18 12:47:01 +0000372
373/*
374 * Initializes the notifications structure.
375 */
376void vm_notifications_init_bindings(struct notifications *notifications)
377{
378 for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) {
379 notifications->bindings_sender_id[i] = HF_INVALID_VM_ID;
380 }
381}