Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame^] | 19 | #include "hf/arch/std.h" |
| 20 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/api.h" |
| 22 | #include "hf/cpu.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 23 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 24 | #include "vmapi/hf/call.h" |
| 25 | |
| 26 | static struct vm vms[MAX_VMS]; |
| 27 | static uint32_t vm_count; |
| 28 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 29 | bool vm_init(uint32_t vcpu_count, struct mpool *ppool, struct vm **new_vm) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 30 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 31 | uint32_t i; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 32 | struct vm *vm; |
| 33 | |
| 34 | if (vm_count >= MAX_VMS) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | vm = &vms[vm_count]; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 39 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 40 | memset(vm, 0, sizeof(*vm)); |
| 41 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 42 | list_init(&vm->mailbox.waiter_list); |
| 43 | list_init(&vm->mailbox.ready_list); |
| 44 | sl_init(&vm->lock); |
| 45 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 46 | vm->id = vm_count; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 47 | vm->vcpu_count = vcpu_count; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 48 | vm->mailbox.state = mailbox_state_empty; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 49 | atomic_init(&vm->aborting, false); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 50 | |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 51 | if (!mm_vm_init(&vm->ptable, ppool)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 55 | /* Initialise waiter entries. */ |
| 56 | for (i = 0; i < MAX_VMS; i++) { |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 57 | vm->wait_entries[i].waiting_vm = vm; |
| 58 | list_init(&vm->wait_entries[i].wait_links); |
| 59 | list_init(&vm->wait_entries[i].ready_links); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 62 | /* Do basic initialization of vcpus. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 63 | for (i = 0; i < vcpu_count; i++) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 64 | vcpu_init(&vm->vcpus[i], vm); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 65 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 66 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 67 | ++vm_count; |
| 68 | *new_vm = vm; |
| 69 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 70 | return true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 73 | uint32_t vm_get_count(void) |
| 74 | { |
| 75 | return vm_count; |
| 76 | } |
| 77 | |
| 78 | struct vm *vm_get(uint32_t id) |
| 79 | { |
| 80 | /* Ensure the VM is initialized. */ |
| 81 | if (id >= vm_count) { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | return &vms[id]; |
| 86 | } |
| 87 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Locks the given VM and updates `locked` to hold the newly locked vm. |
| 90 | */ |
| 91 | void vm_lock(struct vm *vm, struct vm_locked *locked) |
| 92 | { |
| 93 | sl_lock(&vm->lock); |
| 94 | locked->vm = vm; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect |
| 99 | * the fact that the VM is no longer locked. |
| 100 | */ |
| 101 | void vm_unlock(struct vm_locked *locked) |
| 102 | { |
| 103 | sl_unlock(&locked->vm->lock); |
| 104 | locked->vm = NULL; |
| 105 | } |
| 106 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Starts a vCPU of a secondary VM. |
| 109 | * |
| 110 | * TODO: Shall we use index or id here? |
| 111 | */ |
| 112 | void vm_secondary_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, |
| 113 | uintreg_t arg) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 114 | { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 115 | struct vcpu *vcpu = &vm->vcpus[index]; |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 116 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 117 | if (index >= vm->vcpu_count) { |
| 118 | return; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 119 | } |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Set vCPU registers to a clean state ready for boot. As this is a |
| 123 | * secondary which can migrate between pCPUs, the ID of the vCPU is |
| 124 | * defined as the index and does not match the ID of the pCPU it is |
| 125 | * running on. |
| 126 | */ |
| 127 | arch_regs_reset(&vcpu->regs, vm->id == HF_PRIMARY_VM_ID, vm->id, index, |
| 128 | vm->ptable.root); |
| 129 | vcpu_on(vcpu, entry, arg); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 130 | } |