blob: 1c6716610cea6ebe72c1bd22155fb98bbd5af94a [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"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010014#include "hf/ffa.h"
Andrew Scull3c257452019-11-26 13:32:50 +000015#include "hf/layout.h"
16#include "hf/plat/iommu.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010017#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull19503262018-09-20 14:48:39 +010019#include "vmapi/hf/call.h"
20
21static struct vm vms[MAX_VMS];
Olivier Deprez96a2a262020-06-11 17:21:38 +020022static struct vm other_world;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023static ffa_vm_count_t vm_count;
J-Alvesb37fd082020-10-22 12:29:21 +010024static struct vm *first_boot_vm;
Andrew Scull19503262018-09-20 14:48:39 +010025
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080026static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
27{
28 if (vm->el0_partition) {
29 return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
30 ppool);
31 }
32 return mm_vm_init(&vm->ptable, vm->id, ppool);
33}
34
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010035struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080036 struct mpool *ppool, bool el0_partition)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010038 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010039 struct vm *vm;
40
Olivier Deprez96a2a262020-06-11 17:21:38 +020041 if (id == HF_OTHER_WORLD_ID) {
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080042 CHECK(el0_partition == false);
Olivier Deprez96a2a262020-06-11 17:21:38 +020043 vm = &other_world;
Andrew Walbran9daa57e2019-09-27 13:33:20 +010044 } else {
45 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010046
Andrew Walbran9daa57e2019-09-27 13:33:20 +010047 CHECK(id >= HF_VM_ID_OFFSET);
48 CHECK(vm_index < ARRAY_SIZE(vms));
49 vm = &vms[vm_index];
50 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010051
Andrew Scull2b5fbad2019-04-05 13:55:56 +010052 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010053
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000054 list_init(&vm->mailbox.waiter_list);
55 list_init(&vm->mailbox.ready_list);
56 sl_init(&vm->lock);
57
Andrew Walbran9daa57e2019-09-27 13:33:20 +010058 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010059 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010060 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000061 atomic_init(&vm->aborting, false);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080062 vm->el0_partition = el0_partition;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080064 if (!vm_init_mm(vm, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010065 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000066 }
67
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000068 /* Initialise waiter entries. */
69 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000070 vm->wait_entries[i].waiting_vm = vm;
71 list_init(&vm->wait_entries[i].wait_links);
72 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000073 }
74
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000075 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010076 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010077 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010078 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010079
Andrew Walbran9daa57e2019-09-27 13:33:20 +010080 return vm;
81}
82
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010083bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080084 struct vm **new_vm, bool el0_partition)
Andrew Walbran9daa57e2019-09-27 13:33:20 +010085{
86 if (vm_count >= MAX_VMS) {
87 return false;
88 }
89
90 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080091 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool,
92 el0_partition);
Andrew Walbran9daa57e2019-09-27 13:33:20 +010093 if (*new_vm == NULL) {
94 return false;
95 }
Andrew Scull19503262018-09-20 14:48:39 +010096 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010097
Wedson Almeida Filho03306112018-11-26 00:08:03 +000098 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010099}
100
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100101ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +0100102{
103 return vm_count;
104}
105
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100106/**
107 * Returns a pointer to the VM with the corresponding id.
108 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100109struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +0100110{
David Brazdilbc501192019-09-27 13:20:56 +0100111 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +0100112
Olivier Deprez96a2a262020-06-11 17:21:38 +0200113 if (id == HF_OTHER_WORLD_ID) {
114 if (other_world.id == HF_OTHER_WORLD_ID) {
115 return &other_world;
116 }
Andrew Scull19503262018-09-20 14:48:39 +0100117 return NULL;
118 }
119
Olivier Deprez96a2a262020-06-11 17:21:38 +0200120 /* Check that this is not a reserved ID. */
121 if (id < HF_VM_ID_OFFSET) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100122 return NULL;
123 }
124
David Brazdilbc501192019-09-27 13:20:56 +0100125 index = id - HF_VM_ID_OFFSET;
126
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100127 return vm_find_index(index);
128}
129
130/**
131 * Returns a pointer to the VM at the specified index.
132 */
133struct vm *vm_find_index(uint16_t index)
134{
David Brazdilbc501192019-09-27 13:20:56 +0100135 /* Ensure the VM is initialized. */
136 if (index >= vm_count) {
137 return NULL;
138 }
139
140 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100141}
142
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000143/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000144 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000145 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100146struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000147{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100148 struct vm_locked locked = {
149 .vm = vm,
150 };
151
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000152 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100153
154 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000155}
156
157/**
Jose Marinho75509b42019-04-09 09:34:59 +0100158 * Locks two VMs ensuring that the locking order is according to the locks'
159 * addresses.
160 */
161struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
162{
163 struct two_vm_locked dual_lock;
164
165 sl_lock_both(&vm1->lock, &vm2->lock);
166 dual_lock.vm1.vm = vm1;
167 dual_lock.vm2.vm = vm2;
168
169 return dual_lock;
170}
171
172/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000173 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
174 * the fact that the VM is no longer locked.
175 */
176void vm_unlock(struct vm_locked *locked)
177{
178 sl_unlock(&locked->vm->lock);
179 locked->vm = NULL;
180}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100181
182/**
183 * Get the vCPU with the given index from the given VM.
184 * This assumes the index is valid, i.e. less than vm->vcpu_count.
185 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100186struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100187{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100188 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100189 return &vm->vcpus[vcpu_index];
190}
Andrew Scull3c257452019-11-26 13:32:50 +0000191
192/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000193 * Gets `vm`'s wait entry for waiting on the `for_vm`.
194 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100195struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000196{
197 uint16_t index;
198
199 CHECK(for_vm >= HF_VM_ID_OFFSET);
200 index = for_vm - HF_VM_ID_OFFSET;
201 CHECK(index < MAX_VMS);
202
203 return &vm->wait_entries[index];
204}
205
206/**
207 * Gets the ID of the VM which the given VM's wait entry is for.
208 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100209ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000210{
211 uint16_t index = entry - vm->wait_entries;
212
213 return index + HF_VM_ID_OFFSET;
214}
215
216/**
Andrew Walbran45633dd2020-10-07 17:59:54 +0100217 * Return whether the given VM ID represents an entity in the current world:
218 * i.e. the hypervisor or a normal world VM when running in the normal world, or
219 * the SPM or an SP when running in the secure world.
220 */
221bool vm_id_is_current_world(ffa_vm_id_t vm_id)
222{
223 return (vm_id & HF_VM_ID_WORLD_MASK) !=
224 (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK);
225}
226
227/**
Andrew Scull3c257452019-11-26 13:32:50 +0000228 * Map a range of addresses to the VM in both the MMU and the IOMMU.
229 *
230 * mm_vm_defrag should always be called after a series of page table updates,
231 * whether they succeed or fail. This is because on failure extra page table
232 * entries may have been allocated and then not used, while on success it may be
233 * possible to compact the page table by merging several entries into a block.
234 *
235 * Returns true on success, or false if the update failed and no changes were
236 * made.
237 *
238 */
239bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
240 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
241{
242 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
243 return false;
244 }
245
246 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
247
248 return true;
249}
250
251/**
252 * Prepares the given VM for the given address mapping such that it will be able
253 * to commit the change without failure.
254 *
255 * In particular, multiple calls to this function will result in the
256 * corresponding calls to commit the changes to succeed.
257 *
258 * Returns true on success, or false if the update failed and no changes were
259 * made.
260 */
261bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
262 uint32_t mode, struct mpool *ppool)
263{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800264 if (vm_locked.vm->el0_partition) {
265 return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
266 mode, ppool);
267 }
Andrew Scull3c257452019-11-26 13:32:50 +0000268 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
269 ppool);
270}
271
272/**
273 * Commits the given address mapping to the VM assuming the operation cannot
274 * fail. `vm_identity_prepare` must used correctly before this to ensure
275 * this condition.
276 */
277void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
278 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
279{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800280 if (vm_locked.vm->el0_partition) {
281 mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
282 ppool);
283 if (ipa != NULL) {
284 /*
285 * EL0 partitions are modeled as lightweight VM's, to
286 * promote code reuse. The below statement returns the
287 * mapped PA as an IPA, however, for an EL0 partition,
288 * this is really a VA.
289 */
290 *ipa = ipa_from_pa(begin);
291 }
292 } else {
293 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
294 ppool, ipa);
295 }
Andrew Scull3c257452019-11-26 13:32:50 +0000296 plat_iommu_identity_map(vm_locked, begin, end, mode);
297}
298
299/**
300 * Unmap a range of addresses from the VM.
301 *
302 * Returns true on success, or false if the update failed and no changes were
303 * made.
304 */
305bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
306 struct mpool *ppool)
307{
308 uint32_t mode = MM_MODE_UNMAPPED_MASK;
309
310 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
311}
312
313/**
314 * Unmaps the hypervisor pages from the given page table.
315 */
316bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
317{
318 /* TODO: If we add pages dynamically, they must be included here too. */
319 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
320 ppool) &&
321 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
322 ppool) &&
323 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
324 ppool);
325}
J-Alvesb37fd082020-10-22 12:29:21 +0100326
327/**
328 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
329 */
330struct vm *vm_get_first_boot(void)
331{
332 return first_boot_vm;
333}
334
335/**
336 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
337 * and rooted in `first_boot_vm`.
338 */
339void vm_update_boot(struct vm *vm)
340{
341 struct vm *current = NULL;
342 struct vm *previous = NULL;
343
344 if (first_boot_vm == NULL) {
345 first_boot_vm = vm;
346 return;
347 }
348
349 current = first_boot_vm;
350
351 while (current != NULL && current->boot_order >= vm->boot_order) {
352 previous = current;
353 current = current->next_boot;
354 }
355
356 if (previous != NULL) {
357 previous->next_boot = vm;
358 } else {
359 first_boot_vm = vm;
360 }
361
362 vm->next_boot = current;
363}