blob: a021d77131e6396398a0d00128e8f12b43eef022 [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];
Andrew Walbran9daa57e2019-09-27 13:33:20 +010022static struct vm tee_vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023static ffa_vm_count_t vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010024
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010025struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Andrew Walbran9daa57e2019-09-27 13:33:20 +010026 struct mpool *ppool)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010028 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010029 struct vm *vm;
30
Andrew Walbran9daa57e2019-09-27 13:33:20 +010031 if (id == HF_TEE_VM_ID) {
32 vm = &tee_vm;
33 } else {
34 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010035
Andrew Walbran9daa57e2019-09-27 13:33:20 +010036 CHECK(id >= HF_VM_ID_OFFSET);
37 CHECK(vm_index < ARRAY_SIZE(vms));
38 vm = &vms[vm_index];
39 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010040
Andrew Scull2b5fbad2019-04-05 13:55:56 +010041 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010042
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000043 list_init(&vm->mailbox.waiter_list);
44 list_init(&vm->mailbox.ready_list);
45 sl_init(&vm->lock);
46
Andrew Walbran9daa57e2019-09-27 13:33:20 +010047 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010048 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010049 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000050 atomic_init(&vm->aborting, false);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010051
Andrew Scullda3df7f2019-01-05 17:49:27 +000052 if (!mm_vm_init(&vm->ptable, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010053 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000054 }
55
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000056 /* Initialise waiter entries. */
57 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000058 vm->wait_entries[i].waiting_vm = vm;
59 list_init(&vm->wait_entries[i].wait_links);
60 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000061 }
62
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000063 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010064 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010065 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010066 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010067
Andrew Walbran9daa57e2019-09-27 13:33:20 +010068 return vm;
69}
70
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010071bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Andrew Walbran9daa57e2019-09-27 13:33:20 +010072 struct vm **new_vm)
73{
74 if (vm_count >= MAX_VMS) {
75 return false;
76 }
77
78 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
79 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool);
80 if (*new_vm == NULL) {
81 return false;
82 }
Andrew Scull19503262018-09-20 14:48:39 +010083 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010084
Wedson Almeida Filho03306112018-11-26 00:08:03 +000085 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010086}
87
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010088ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +010089{
90 return vm_count;
91}
92
Fuad Tabbae4efcc32020-07-16 15:37:27 +010093/**
94 * Returns a pointer to the VM with the corresponding id.
95 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +010097{
David Brazdilbc501192019-09-27 13:20:56 +010098 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +010099
David Brazdilbc501192019-09-27 13:20:56 +0100100 /* Check that this is not a reserved ID. */
101 if (id < HF_VM_ID_OFFSET) {
Andrew Scull19503262018-09-20 14:48:39 +0100102 return NULL;
103 }
104
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100105 if (id == HF_TEE_VM_ID) {
106 if (tee_vm.id == HF_TEE_VM_ID) {
107 return &tee_vm;
108 }
109 return NULL;
110 }
111
David Brazdilbc501192019-09-27 13:20:56 +0100112 index = id - HF_VM_ID_OFFSET;
113
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100114 return vm_find_index(index);
115}
116
117/**
118 * Returns a pointer to the VM at the specified index.
119 */
120struct vm *vm_find_index(uint16_t index)
121{
David Brazdilbc501192019-09-27 13:20:56 +0100122 /* Ensure the VM is initialized. */
123 if (index >= vm_count) {
124 return NULL;
125 }
126
127 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100128}
129
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000130/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000131 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000132 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100133struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000134{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100135 struct vm_locked locked = {
136 .vm = vm,
137 };
138
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000139 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100140
141 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000142}
143
144/**
Jose Marinho75509b42019-04-09 09:34:59 +0100145 * Locks two VMs ensuring that the locking order is according to the locks'
146 * addresses.
147 */
148struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
149{
150 struct two_vm_locked dual_lock;
151
152 sl_lock_both(&vm1->lock, &vm2->lock);
153 dual_lock.vm1.vm = vm1;
154 dual_lock.vm2.vm = vm2;
155
156 return dual_lock;
157}
158
159/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000160 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
161 * the fact that the VM is no longer locked.
162 */
163void vm_unlock(struct vm_locked *locked)
164{
165 sl_unlock(&locked->vm->lock);
166 locked->vm = NULL;
167}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100168
169/**
170 * Get the vCPU with the given index from the given VM.
171 * This assumes the index is valid, i.e. less than vm->vcpu_count.
172 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100173struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100174{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100175 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100176 return &vm->vcpus[vcpu_index];
177}
Andrew Scull3c257452019-11-26 13:32:50 +0000178
179/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000180 * Gets `vm`'s wait entry for waiting on the `for_vm`.
181 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100182struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000183{
184 uint16_t index;
185
186 CHECK(for_vm >= HF_VM_ID_OFFSET);
187 index = for_vm - HF_VM_ID_OFFSET;
188 CHECK(index < MAX_VMS);
189
190 return &vm->wait_entries[index];
191}
192
193/**
194 * Gets the ID of the VM which the given VM's wait entry is for.
195 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100196ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000197{
198 uint16_t index = entry - vm->wait_entries;
199
200 return index + HF_VM_ID_OFFSET;
201}
202
203/**
Andrew Scull3c257452019-11-26 13:32:50 +0000204 * Map a range of addresses to the VM in both the MMU and the IOMMU.
205 *
206 * mm_vm_defrag should always be called after a series of page table updates,
207 * whether they succeed or fail. This is because on failure extra page table
208 * entries may have been allocated and then not used, while on success it may be
209 * possible to compact the page table by merging several entries into a block.
210 *
211 * Returns true on success, or false if the update failed and no changes were
212 * made.
213 *
214 */
215bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
216 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
217{
218 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
219 return false;
220 }
221
222 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
223
224 return true;
225}
226
227/**
228 * Prepares the given VM for the given address mapping such that it will be able
229 * to commit the change without failure.
230 *
231 * In particular, multiple calls to this function will result in the
232 * corresponding calls to commit the changes to succeed.
233 *
234 * Returns true on success, or false if the update failed and no changes were
235 * made.
236 */
237bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
238 uint32_t mode, struct mpool *ppool)
239{
240 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
241 ppool);
242}
243
244/**
245 * Commits the given address mapping to the VM assuming the operation cannot
246 * fail. `vm_identity_prepare` must used correctly before this to ensure
247 * this condition.
248 */
249void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
250 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
251{
252 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, ppool,
253 ipa);
254 plat_iommu_identity_map(vm_locked, begin, end, mode);
255}
256
257/**
258 * Unmap a range of addresses from the VM.
259 *
260 * Returns true on success, or false if the update failed and no changes were
261 * made.
262 */
263bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
264 struct mpool *ppool)
265{
266 uint32_t mode = MM_MODE_UNMAPPED_MASK;
267
268 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
269}
270
271/**
272 * Unmaps the hypervisor pages from the given page table.
273 */
274bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
275{
276 /* TODO: If we add pages dynamically, they must be included here too. */
277 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
278 ppool) &&
279 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
280 ppool) &&
281 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
282 ppool);
283}