Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 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 | |
| 17 | #include "hf/vcpu.h" |
| 18 | |
| 19 | #include "hf/check.h" |
| 20 | #include "hf/dlog.h" |
| 21 | #include "hf/std.h" |
| 22 | #include "hf/vm.h" |
| 23 | |
| 24 | /** |
| 25 | * Locks the given vCPU and updates `locked` to hold the newly locked vCPU. |
| 26 | */ |
| 27 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu) |
| 28 | { |
| 29 | struct vcpu_locked locked = { |
| 30 | .vcpu = vcpu, |
| 31 | }; |
| 32 | |
| 33 | sl_lock(&vcpu->lock); |
| 34 | |
| 35 | return locked; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to |
| 40 | * reflect the fact that the vCPU is no longer locked. |
| 41 | */ |
| 42 | void vcpu_unlock(struct vcpu_locked *locked) |
| 43 | { |
| 44 | sl_unlock(&locked->vcpu->lock); |
| 45 | locked->vcpu = NULL; |
| 46 | } |
| 47 | |
| 48 | void vcpu_init(struct vcpu *vcpu, struct vm *vm) |
| 49 | { |
| 50 | memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu)); |
| 51 | sl_init(&vcpu->lock); |
| 52 | vcpu->regs_available = true; |
| 53 | vcpu->vm = vm; |
| 54 | vcpu->state = VCPU_STATE_OFF; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Initialise the registers for the given vCPU and set the state to |
| 59 | * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this. |
| 60 | */ |
| 61 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg) |
| 62 | { |
| 63 | arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg); |
| 64 | vcpu.vcpu->state = VCPU_STATE_READY; |
| 65 | } |
| 66 | |
| 67 | spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu) |
| 68 | { |
| 69 | size_t index = vcpu - vcpu->vm->vcpus; |
| 70 | |
| 71 | CHECK(index < UINT16_MAX); |
| 72 | return index; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check whether the given vcpu_state is an off state, for the purpose of |
| 77 | * turning vCPUs on and off. Note that aborted still counts as on in this |
| 78 | * context. |
| 79 | */ |
| 80 | bool vcpu_is_off(struct vcpu_locked vcpu) |
| 81 | { |
| 82 | switch (vcpu.vcpu->state) { |
| 83 | case VCPU_STATE_OFF: |
| 84 | return true; |
| 85 | case VCPU_STATE_READY: |
| 86 | case VCPU_STATE_RUNNING: |
| 87 | case VCPU_STATE_BLOCKED_MAILBOX: |
| 88 | case VCPU_STATE_BLOCKED_INTERRUPT: |
| 89 | case VCPU_STATE_ABORTED: |
| 90 | /* |
| 91 | * Aborted still counts as ON for the purposes of PSCI, |
| 92 | * because according to the PSCI specification (section |
| 93 | * 5.7.1) a core is only considered to be off if it has |
| 94 | * been turned off with a CPU_OFF call or hasn't yet |
| 95 | * been turned on with a CPU_ON call. |
| 96 | */ |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Starts a vCPU of a secondary VM. |
| 103 | * |
| 104 | * Returns true if the secondary was reset and started, or false if it was |
| 105 | * already on and so nothing was done. |
| 106 | */ |
| 107 | bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry, |
| 108 | uintreg_t arg) |
| 109 | { |
| 110 | struct vcpu_locked vcpu_locked; |
| 111 | struct vm *vm = vcpu->vm; |
| 112 | bool vcpu_was_off; |
| 113 | |
| 114 | CHECK(vm->id != HF_PRIMARY_VM_ID); |
| 115 | |
| 116 | vcpu_locked = vcpu_lock(vcpu); |
| 117 | vcpu_was_off = vcpu_is_off(vcpu_locked); |
| 118 | if (vcpu_was_off) { |
| 119 | /* |
| 120 | * Set vCPU registers to a clean state ready for boot. As this |
| 121 | * is a secondary which can migrate between pCPUs, the ID of the |
| 122 | * vCPU is defined as the index and does not match the ID of the |
| 123 | * pCPU it is running on. |
| 124 | */ |
| 125 | arch_regs_reset(vcpu); |
| 126 | vcpu_on(vcpu_locked, entry, arg); |
| 127 | } |
| 128 | vcpu_unlock(&vcpu_locked); |
| 129 | |
| 130 | return vcpu_was_off; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Handles a page fault. It does so by determining if it's a legitimate or |
| 135 | * spurious fault, and recovering from the latter. |
| 136 | * |
| 137 | * Returns true if the caller should resume the current vcpu, or false if its VM |
| 138 | * should be aborted. |
| 139 | */ |
| 140 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 141 | struct vcpu_fault_info *f) |
| 142 | { |
| 143 | struct vm *vm = current->vm; |
| 144 | uint32_t mode; |
| 145 | uint32_t mask = f->mode | MM_MODE_INVALID; |
| 146 | bool resume; |
| 147 | |
| 148 | sl_lock(&vm->lock); |
| 149 | |
| 150 | /* |
| 151 | * Check if this is a legitimate fault, i.e., if the page table doesn't |
| 152 | * allow the access attempted by the VM. |
| 153 | * |
| 154 | * Otherwise, this is a spurious fault, likely because another CPU is |
| 155 | * updating the page table. It is responsible for issuing global TLB |
| 156 | * invalidations while holding the VM lock, so we don't need to do |
| 157 | * anything else to recover from it. (Acquiring/releasing the lock |
| 158 | * ensured that the invalidations have completed.) |
| 159 | */ |
| 160 | resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1), |
| 161 | &mode) && |
| 162 | (mode & mask) == f->mode; |
| 163 | |
| 164 | sl_unlock(&vm->lock); |
| 165 | |
| 166 | if (!resume) { |
| 167 | dlog("Stage-2 page fault: pc=%#x, vmid=%u, vcpu=%u, " |
| 168 | "vaddr=%#x, ipaddr=%#x, mode=%#x\n", |
| 169 | f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr, |
| 170 | f->mode); |
| 171 | } |
| 172 | |
| 173 | return resume; |
| 174 | } |