blob: 856509fad974f62df8241915adf6c0a94fa23589 [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 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/api.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010020#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/cpu.h"
Andrew Scull3c257452019-11-26 13:32:50 +000022#include "hf/layout.h"
23#include "hf/plat/iommu.h"
Andrew Walbranb037d5b2019-06-25 17:19:41 +010024#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010025#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010026
Andrew Scull19503262018-09-20 14:48:39 +010027#include "vmapi/hf/call.h"
28
29static struct vm vms[MAX_VMS];
Andrew Walbran52d99672019-06-25 15:51:11 +010030static spci_vm_count_t vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010031
Andrew Walbranc6d23c42019-06-26 13:30:42 +010032bool vm_init(spci_vcpu_count_t vcpu_count, struct mpool *ppool,
33 struct vm **new_vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010034{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010035 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010036 struct vm *vm;
37
38 if (vm_count >= MAX_VMS) {
39 return false;
40 }
41
42 vm = &vms[vm_count];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010043
Andrew Scull2b5fbad2019-04-05 13:55:56 +010044 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010045
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000046 list_init(&vm->mailbox.waiter_list);
47 list_init(&vm->mailbox.ready_list);
48 sl_init(&vm->lock);
49
Fuad Tabba494376e2019-08-05 12:35:10 +010050 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
51 vm->id = vm_count + HF_VM_ID_OFFSET;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010052 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010053 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000054 atomic_init(&vm->aborting, false);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010055
Andrew Scullda3df7f2019-01-05 17:49:27 +000056 if (!mm_vm_init(&vm->ptable, ppool)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +000057 return false;
58 }
59
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000060 /* Initialise waiter entries. */
61 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000062 vm->wait_entries[i].waiting_vm = vm;
63 list_init(&vm->wait_entries[i].wait_links);
64 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000065 }
66
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000067 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010068 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010069 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010070 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010071
Andrew Scull19503262018-09-20 14:48:39 +010072 ++vm_count;
73 *new_vm = vm;
74
Wedson Almeida Filho03306112018-11-26 00:08:03 +000075 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010076}
77
Andrew Walbran52d99672019-06-25 15:51:11 +010078spci_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +010079{
80 return vm_count;
81}
82
Andrew Walbran42347a92019-05-09 13:59:03 +010083struct vm *vm_find(spci_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +010084{
David Brazdilbc501192019-09-27 13:20:56 +010085 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +010086
David Brazdilbc501192019-09-27 13:20:56 +010087 /* Check that this is not a reserved ID. */
88 if (id < HF_VM_ID_OFFSET) {
Andrew Scull19503262018-09-20 14:48:39 +010089 return NULL;
90 }
91
David Brazdilbc501192019-09-27 13:20:56 +010092 index = id - HF_VM_ID_OFFSET;
93
94 /* Ensure the VM is initialized. */
95 if (index >= vm_count) {
96 return NULL;
97 }
98
99 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100100}
101
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000102/**
103 * Locks the given VM and updates `locked` to hold the newly locked vm.
104 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100105struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000106{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100107 struct vm_locked locked = {
108 .vm = vm,
109 };
110
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000111 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100112
113 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000114}
115
116/**
Jose Marinho75509b42019-04-09 09:34:59 +0100117 * Locks two VMs ensuring that the locking order is according to the locks'
118 * addresses.
119 */
120struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
121{
122 struct two_vm_locked dual_lock;
123
124 sl_lock_both(&vm1->lock, &vm2->lock);
125 dual_lock.vm1.vm = vm1;
126 dual_lock.vm2.vm = vm2;
127
128 return dual_lock;
129}
130
131/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000132 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
133 * the fact that the VM is no longer locked.
134 */
135void vm_unlock(struct vm_locked *locked)
136{
137 sl_unlock(&locked->vm->lock);
138 locked->vm = NULL;
139}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100140
141/**
142 * Get the vCPU with the given index from the given VM.
143 * This assumes the index is valid, i.e. less than vm->vcpu_count.
144 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100145struct vcpu *vm_get_vcpu(struct vm *vm, spci_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100146{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100147 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100148 return &vm->vcpus[vcpu_index];
149}
Andrew Scull3c257452019-11-26 13:32:50 +0000150
151/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000152 * Gets `vm`'s wait entry for waiting on the `for_vm`.
153 */
154struct wait_entry *vm_get_wait_entry(struct vm *vm, spci_vm_id_t for_vm)
155{
156 uint16_t index;
157
158 CHECK(for_vm >= HF_VM_ID_OFFSET);
159 index = for_vm - HF_VM_ID_OFFSET;
160 CHECK(index < MAX_VMS);
161
162 return &vm->wait_entries[index];
163}
164
165/**
166 * Gets the ID of the VM which the given VM's wait entry is for.
167 */
168spci_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
169{
170 uint16_t index = entry - vm->wait_entries;
171
172 return index + HF_VM_ID_OFFSET;
173}
174
175/**
Andrew Scull3c257452019-11-26 13:32:50 +0000176 * Map a range of addresses to the VM in both the MMU and the IOMMU.
177 *
178 * mm_vm_defrag should always be called after a series of page table updates,
179 * whether they succeed or fail. This is because on failure extra page table
180 * entries may have been allocated and then not used, while on success it may be
181 * possible to compact the page table by merging several entries into a block.
182 *
183 * Returns true on success, or false if the update failed and no changes were
184 * made.
185 *
186 */
187bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
188 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
189{
190 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
191 return false;
192 }
193
194 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
195
196 return true;
197}
198
199/**
200 * Prepares the given VM for the given address mapping such that it will be able
201 * to commit the change without failure.
202 *
203 * In particular, multiple calls to this function will result in the
204 * corresponding calls to commit the changes to succeed.
205 *
206 * Returns true on success, or false if the update failed and no changes were
207 * made.
208 */
209bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
210 uint32_t mode, struct mpool *ppool)
211{
212 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
213 ppool);
214}
215
216/**
217 * Commits the given address mapping to the VM assuming the operation cannot
218 * fail. `vm_identity_prepare` must used correctly before this to ensure
219 * this condition.
220 */
221void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
222 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
223{
224 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, ppool,
225 ipa);
226 plat_iommu_identity_map(vm_locked, begin, end, mode);
227}
228
229/**
230 * Unmap a range of addresses from the VM.
231 *
232 * Returns true on success, or false if the update failed and no changes were
233 * made.
234 */
235bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
236 struct mpool *ppool)
237{
238 uint32_t mode = MM_MODE_UNMAPPED_MASK;
239
240 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
241}
242
243/**
244 * Unmaps the hypervisor pages from the given page table.
245 */
246bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
247{
248 /* TODO: If we add pages dynamically, they must be included here too. */
249 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
250 ppool) &&
251 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
252 ppool) &&
253 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
254 ppool);
255}