Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 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 Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/vm.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 19 | #include "hf/api.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 20 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/cpu.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 22 | #include "hf/ffa.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 23 | #include "hf/layout.h" |
| 24 | #include "hf/plat/iommu.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 25 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 26 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 27 | #include "vmapi/hf/call.h" |
| 28 | |
| 29 | static struct vm vms[MAX_VMS]; |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 30 | static struct vm tee_vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 31 | static ffa_vm_count_t vm_count; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 32 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 33 | struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count, |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 34 | struct mpool *ppool) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 35 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 36 | uint32_t i; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 37 | struct vm *vm; |
| 38 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 39 | if (id == HF_TEE_VM_ID) { |
| 40 | vm = &tee_vm; |
| 41 | } else { |
| 42 | uint16_t vm_index = id - HF_VM_ID_OFFSET; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 43 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 44 | CHECK(id >= HF_VM_ID_OFFSET); |
| 45 | CHECK(vm_index < ARRAY_SIZE(vms)); |
| 46 | vm = &vms[vm_index]; |
| 47 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 48 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 49 | memset_s(vm, sizeof(*vm), 0, sizeof(*vm)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 50 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 51 | list_init(&vm->mailbox.waiter_list); |
| 52 | list_init(&vm->mailbox.ready_list); |
| 53 | sl_init(&vm->lock); |
| 54 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 55 | vm->id = id; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 56 | vm->vcpu_count = vcpu_count; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 57 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 58 | atomic_init(&vm->aborting, false); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 59 | |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 60 | if (!mm_vm_init(&vm->ptable, ppool)) { |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 61 | return NULL; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 64 | /* Initialise waiter entries. */ |
| 65 | for (i = 0; i < MAX_VMS; i++) { |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 66 | vm->wait_entries[i].waiting_vm = vm; |
| 67 | list_init(&vm->wait_entries[i].wait_links); |
| 68 | list_init(&vm->wait_entries[i].ready_links); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 71 | /* Do basic initialization of vCPUs. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 72 | for (i = 0; i < vcpu_count; i++) { |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 73 | vcpu_init(vm_get_vcpu(vm, i), vm); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 74 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 75 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 76 | return vm; |
| 77 | } |
| 78 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 79 | bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool, |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 80 | struct vm **new_vm) |
| 81 | { |
| 82 | if (vm_count >= MAX_VMS) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */ |
| 87 | *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool); |
| 88 | if (*new_vm == NULL) { |
| 89 | return false; |
| 90 | } |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 91 | ++vm_count; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 92 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 93 | return true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 96 | ffa_vm_count_t vm_get_count(void) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 97 | { |
| 98 | return vm_count; |
| 99 | } |
| 100 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 101 | struct vm *vm_find(ffa_vm_id_t id) |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 102 | { |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 103 | uint16_t index; |
Fuad Tabba | 494376e | 2019-08-05 12:35:10 +0100 | [diff] [blame] | 104 | |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 105 | /* Check that this is not a reserved ID. */ |
| 106 | if (id < HF_VM_ID_OFFSET) { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 110 | if (id == HF_TEE_VM_ID) { |
| 111 | if (tee_vm.id == HF_TEE_VM_ID) { |
| 112 | return &tee_vm; |
| 113 | } |
| 114 | return NULL; |
| 115 | } |
| 116 | |
David Brazdil | bc50119 | 2019-09-27 13:20:56 +0100 | [diff] [blame] | 117 | index = id - HF_VM_ID_OFFSET; |
| 118 | |
| 119 | /* Ensure the VM is initialized. */ |
| 120 | if (index >= vm_count) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | return &vms[index]; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 127 | /** |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 128 | * Locks the given VM and updates `locked` to hold the newly locked VM. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 129 | */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 130 | struct vm_locked vm_lock(struct vm *vm) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 131 | { |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 132 | struct vm_locked locked = { |
| 133 | .vm = vm, |
| 134 | }; |
| 135 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 136 | sl_lock(&vm->lock); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 137 | |
| 138 | return locked; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 142 | * Locks two VMs ensuring that the locking order is according to the locks' |
| 143 | * addresses. |
| 144 | */ |
| 145 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2) |
| 146 | { |
| 147 | struct two_vm_locked dual_lock; |
| 148 | |
| 149 | sl_lock_both(&vm1->lock, &vm2->lock); |
| 150 | dual_lock.vm1.vm = vm1; |
| 151 | dual_lock.vm2.vm = vm2; |
| 152 | |
| 153 | return dual_lock; |
| 154 | } |
| 155 | |
| 156 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 157 | * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect |
| 158 | * the fact that the VM is no longer locked. |
| 159 | */ |
| 160 | void vm_unlock(struct vm_locked *locked) |
| 161 | { |
| 162 | sl_unlock(&locked->vm->lock); |
| 163 | locked->vm = NULL; |
| 164 | } |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Get the vCPU with the given index from the given VM. |
| 168 | * This assumes the index is valid, i.e. less than vm->vcpu_count. |
| 169 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 170 | struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index) |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 171 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 172 | CHECK(vcpu_index < vm->vcpu_count); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 173 | return &vm->vcpus[vcpu_index]; |
| 174 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 175 | |
| 176 | /** |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 177 | * Gets `vm`'s wait entry for waiting on the `for_vm`. |
| 178 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 179 | struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm) |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 180 | { |
| 181 | uint16_t index; |
| 182 | |
| 183 | CHECK(for_vm >= HF_VM_ID_OFFSET); |
| 184 | index = for_vm - HF_VM_ID_OFFSET; |
| 185 | CHECK(index < MAX_VMS); |
| 186 | |
| 187 | return &vm->wait_entries[index]; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Gets the ID of the VM which the given VM's wait entry is for. |
| 192 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 193 | ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry) |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 194 | { |
| 195 | uint16_t index = entry - vm->wait_entries; |
| 196 | |
| 197 | return index + HF_VM_ID_OFFSET; |
| 198 | } |
| 199 | |
| 200 | /** |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 201 | * Map a range of addresses to the VM in both the MMU and the IOMMU. |
| 202 | * |
| 203 | * mm_vm_defrag should always be called after a series of page table updates, |
| 204 | * whether they succeed or fail. This is because on failure extra page table |
| 205 | * entries may have been allocated and then not used, while on success it may be |
| 206 | * possible to compact the page table by merging several entries into a block. |
| 207 | * |
| 208 | * Returns true on success, or false if the update failed and no changes were |
| 209 | * made. |
| 210 | * |
| 211 | */ |
| 212 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 213 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa) |
| 214 | { |
| 215 | if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa); |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Prepares the given VM for the given address mapping such that it will be able |
| 226 | * to commit the change without failure. |
| 227 | * |
| 228 | * In particular, multiple calls to this function will result in the |
| 229 | * corresponding calls to commit the changes to succeed. |
| 230 | * |
| 231 | * Returns true on success, or false if the update failed and no changes were |
| 232 | * made. |
| 233 | */ |
| 234 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 235 | uint32_t mode, struct mpool *ppool) |
| 236 | { |
| 237 | return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode, |
| 238 | ppool); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Commits the given address mapping to the VM assuming the operation cannot |
| 243 | * fail. `vm_identity_prepare` must used correctly before this to ensure |
| 244 | * this condition. |
| 245 | */ |
| 246 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 247 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa) |
| 248 | { |
| 249 | mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, ppool, |
| 250 | ipa); |
| 251 | plat_iommu_identity_map(vm_locked, begin, end, mode); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Unmap a range of addresses from the VM. |
| 256 | * |
| 257 | * Returns true on success, or false if the update failed and no changes were |
| 258 | * made. |
| 259 | */ |
| 260 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 261 | struct mpool *ppool) |
| 262 | { |
| 263 | uint32_t mode = MM_MODE_UNMAPPED_MASK; |
| 264 | |
| 265 | return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Unmaps the hypervisor pages from the given page table. |
| 270 | */ |
| 271 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool) |
| 272 | { |
| 273 | /* TODO: If we add pages dynamically, they must be included here too. */ |
| 274 | return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(), |
| 275 | ppool) && |
| 276 | vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(), |
| 277 | ppool) && |
| 278 | vm_unmap(vm_locked, layout_data_begin(), layout_data_end(), |
| 279 | ppool); |
| 280 | } |