Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * 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. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/vcpu.h" |
| 10 | |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 11 | #include "hf/arch/cpu.h" |
| 12 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 13 | #include "hf/check.h" |
| 14 | #include "hf/dlog.h" |
| 15 | #include "hf/std.h" |
| 16 | #include "hf/vm.h" |
| 17 | |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 18 | /** GP register to be used to pass the current vCPU ID, at core bring up. */ |
| 19 | #define PHYS_CORE_IDX_GP_REG 4 |
| 20 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Locks the given vCPU and updates `locked` to hold the newly locked vCPU. |
| 23 | */ |
| 24 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu) |
| 25 | { |
| 26 | struct vcpu_locked locked = { |
| 27 | .vcpu = vcpu, |
| 28 | }; |
| 29 | |
| 30 | sl_lock(&vcpu->lock); |
| 31 | |
| 32 | return locked; |
| 33 | } |
| 34 | |
| 35 | /** |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 36 | * Locks two vCPUs ensuring that the locking order is according to the locks' |
| 37 | * addresses. |
| 38 | */ |
| 39 | struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2) |
| 40 | { |
| 41 | struct two_vcpu_locked dual_lock; |
| 42 | |
| 43 | sl_lock_both(&vcpu1->lock, &vcpu2->lock); |
| 44 | dual_lock.vcpu1.vcpu = vcpu1; |
| 45 | dual_lock.vcpu2.vcpu = vcpu2; |
| 46 | |
| 47 | return dual_lock; |
| 48 | } |
| 49 | |
| 50 | /** |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 51 | * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to |
| 52 | * reflect the fact that the vCPU is no longer locked. |
| 53 | */ |
| 54 | void vcpu_unlock(struct vcpu_locked *locked) |
| 55 | { |
| 56 | sl_unlock(&locked->vcpu->lock); |
| 57 | locked->vcpu = NULL; |
| 58 | } |
| 59 | |
| 60 | void vcpu_init(struct vcpu *vcpu, struct vm *vm) |
| 61 | { |
| 62 | memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu)); |
| 63 | sl_init(&vcpu->lock); |
| 64 | vcpu->regs_available = true; |
| 65 | vcpu->vm = vm; |
| 66 | vcpu->state = VCPU_STATE_OFF; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 67 | vcpu->direct_request_origin_vm_id = HF_INVALID_VM_ID; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Initialise the registers for the given vCPU and set the state to |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 72 | * VCPU_STATE_WAITING. The caller must hold the vCPU lock while calling this. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 73 | */ |
| 74 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg) |
| 75 | { |
| 76 | arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg); |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 77 | vcpu.vcpu->state = VCPU_STATE_WAITING; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 80 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu) |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 81 | { |
| 82 | size_t index = vcpu - vcpu->vm->vcpus; |
| 83 | |
| 84 | CHECK(index < UINT16_MAX); |
| 85 | return index; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check whether the given vcpu_state is an off state, for the purpose of |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 90 | * turning vCPUs on and off. Note that Aborted still counts as ON for the |
| 91 | * purposes of PSCI, because according to the PSCI specification (section |
Olivier Deprez | e7eb168 | 2022-03-16 17:09:03 +0100 | [diff] [blame] | 92 | * 5.7.1) a core is only considered to be off if it has been turned off |
| 93 | * with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 94 | */ |
| 95 | bool vcpu_is_off(struct vcpu_locked vcpu) |
| 96 | { |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 97 | return (vcpu.vcpu->state == VCPU_STATE_OFF); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Starts a vCPU of a secondary VM. |
| 102 | * |
| 103 | * Returns true if the secondary was reset and started, or false if it was |
| 104 | * already on and so nothing was done. |
| 105 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 106 | bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked, |
| 107 | ipaddr_t entry, uintreg_t arg) |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 108 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 109 | struct vm *vm = vcpu_locked.vcpu->vm; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 110 | bool vcpu_was_off; |
| 111 | |
| 112 | CHECK(vm->id != HF_PRIMARY_VM_ID); |
| 113 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 114 | vcpu_was_off = vcpu_is_off(vcpu_locked); |
| 115 | if (vcpu_was_off) { |
| 116 | /* |
| 117 | * Set vCPU registers to a clean state ready for boot. As this |
| 118 | * is a secondary which can migrate between pCPUs, the ID of the |
| 119 | * vCPU is defined as the index and does not match the ID of the |
| 120 | * pCPU it is running on. |
| 121 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 122 | arch_regs_reset(vcpu_locked.vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 123 | vcpu_on(vcpu_locked, entry, arg); |
| 124 | } |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 125 | |
| 126 | return vcpu_was_off; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Handles a page fault. It does so by determining if it's a legitimate or |
| 131 | * spurious fault, and recovering from the latter. |
| 132 | * |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 133 | * Returns true if the caller should resume the current vCPU, or false if its VM |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 134 | * should be aborted. |
| 135 | */ |
| 136 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 137 | struct vcpu_fault_info *f) |
| 138 | { |
| 139 | struct vm *vm = current->vm; |
| 140 | uint32_t mode; |
| 141 | uint32_t mask = f->mode | MM_MODE_INVALID; |
| 142 | bool resume; |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 143 | struct vm_locked locked_vm; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 144 | |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 145 | locked_vm = vm_lock(vm); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Check if this is a legitimate fault, i.e., if the page table doesn't |
| 148 | * allow the access attempted by the VM. |
| 149 | * |
| 150 | * Otherwise, this is a spurious fault, likely because another CPU is |
| 151 | * updating the page table. It is responsible for issuing global TLB |
| 152 | * invalidations while holding the VM lock, so we don't need to do |
| 153 | * anything else to recover from it. (Acquiring/releasing the lock |
| 154 | * ensured that the invalidations have completed.) |
| 155 | */ |
Raghu Krishnamurthy | b5775d2 | 2021-02-26 18:54:40 -0800 | [diff] [blame] | 156 | if (!locked_vm.vm->el0_partition) { |
| 157 | resume = vm_mem_get_mode(locked_vm, f->ipaddr, |
| 158 | ipa_add(f->ipaddr, 1), &mode) && |
| 159 | (mode & mask) == f->mode; |
| 160 | } else { |
| 161 | /* |
| 162 | * For EL0 partitions we need to get the mode for the faulting |
| 163 | * vaddr. |
| 164 | */ |
| 165 | resume = |
| 166 | vm_mem_get_mode(locked_vm, ipa_init(va_addr(f->vaddr)), |
| 167 | ipa_add(ipa_init(va_addr(f->vaddr)), 1), |
| 168 | &mode) && |
| 169 | (mode & mask) == f->mode; |
Raghu Krishnamurthy | f16b2ce | 2021-11-02 07:48:38 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * For EL0 partitions, if there is an instruction abort and the |
| 173 | * mode of the page is RWX, we don't resume since Hafnium does |
| 174 | * not allow write and executable pages. |
| 175 | */ |
| 176 | if ((f->mode == MM_MODE_X) && |
| 177 | ((mode & MM_MODE_W) == MM_MODE_W)) { |
| 178 | resume = false; |
| 179 | } |
Raghu Krishnamurthy | b5775d2 | 2021-02-26 18:54:40 -0800 | [diff] [blame] | 180 | } |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 181 | |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 182 | vm_unlock(&locked_vm); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 183 | |
| 184 | if (!resume) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 185 | dlog_warning( |
Raghu Krishnamurthy | b5775d2 | 2021-02-26 18:54:40 -0800 | [diff] [blame] | 186 | "Stage-%d page fault: pc=%#x, vmid=%#x, vcpu=%u, " |
| 187 | "vaddr=%#x, ipaddr=%#x, mode=%#x %#x\n", |
| 188 | current->vm->el0_partition ? 1 : 2, f->pc, vm->id, |
| 189 | vcpu_index(current), f->vaddr, f->ipaddr, f->mode, |
| 190 | mode); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return resume; |
| 194 | } |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 195 | |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 196 | void vcpu_reset(struct vcpu *vcpu) |
| 197 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 198 | arch_cpu_init(vcpu->cpu, vcpu->vm->secondary_ep); |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 199 | |
| 200 | /* Reset the registers to give a clean start for vCPU. */ |
| 201 | arch_regs_reset(vcpu); |
Madhukar Pappireddy | fe297a3 | 2022-06-21 16:42:13 -0500 | [diff] [blame^] | 202 | vcpu->rt_model = RTM_SP_INIT; |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 203 | } |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 204 | |
| 205 | void vcpu_set_phys_core_idx(struct vcpu *vcpu) |
| 206 | { |
| 207 | arch_regs_set_gp_reg(&vcpu->regs, cpu_index(vcpu->cpu), |
| 208 | PHYS_CORE_IDX_GP_REG); |
| 209 | } |