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 | |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 18 | static struct vcpu *boot_vcpu; |
| 19 | |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 20 | /** GP register to be used to pass the current vCPU ID, at core bring up. */ |
| 21 | #define PHYS_CORE_IDX_GP_REG 4 |
| 22 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Locks the given vCPU and updates `locked` to hold the newly locked vCPU. |
| 25 | */ |
| 26 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu) |
| 27 | { |
| 28 | struct vcpu_locked locked = { |
| 29 | .vcpu = vcpu, |
| 30 | }; |
| 31 | |
| 32 | sl_lock(&vcpu->lock); |
| 33 | |
| 34 | return locked; |
| 35 | } |
| 36 | |
| 37 | /** |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 38 | * Locks two vCPUs ensuring that the locking order is according to the locks' |
| 39 | * addresses. |
| 40 | */ |
| 41 | struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2) |
| 42 | { |
| 43 | struct two_vcpu_locked dual_lock; |
| 44 | |
| 45 | sl_lock_both(&vcpu1->lock, &vcpu2->lock); |
| 46 | dual_lock.vcpu1.vcpu = vcpu1; |
| 47 | dual_lock.vcpu2.vcpu = vcpu2; |
| 48 | |
| 49 | return dual_lock; |
| 50 | } |
| 51 | |
| 52 | /** |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 53 | * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to |
| 54 | * reflect the fact that the vCPU is no longer locked. |
| 55 | */ |
| 56 | void vcpu_unlock(struct vcpu_locked *locked) |
| 57 | { |
| 58 | sl_unlock(&locked->vcpu->lock); |
| 59 | locked->vcpu = NULL; |
| 60 | } |
| 61 | |
| 62 | void vcpu_init(struct vcpu *vcpu, struct vm *vm) |
| 63 | { |
| 64 | memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu)); |
| 65 | sl_init(&vcpu->lock); |
| 66 | vcpu->regs_available = true; |
| 67 | vcpu->vm = vm; |
| 68 | vcpu->state = VCPU_STATE_OFF; |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 69 | vcpu->direct_request_origin.is_ffa_req2 = false; |
| 70 | vcpu->direct_request_origin.vm_id = HF_INVALID_VM_ID; |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 71 | vcpu->rt_model = RTM_SP_INIT; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 72 | vcpu->next_boot = NULL; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Initialise the registers for the given vCPU and set the state to |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 77 | * 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] | 78 | */ |
| 79 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg) |
| 80 | { |
| 81 | arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg); |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 82 | vcpu.vcpu->state = VCPU_STATE_WAITING; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 85 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu) |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 86 | { |
| 87 | size_t index = vcpu - vcpu->vm->vcpus; |
| 88 | |
| 89 | CHECK(index < UINT16_MAX); |
| 90 | return index; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * 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] | 95 | * turning vCPUs on and off. Note that Aborted still counts as ON for the |
| 96 | * purposes of PSCI, because according to the PSCI specification (section |
Olivier Deprez | e7eb168 | 2022-03-16 17:09:03 +0100 | [diff] [blame] | 97 | * 5.7.1) a core is only considered to be off if it has been turned off |
| 98 | * 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] | 99 | */ |
| 100 | bool vcpu_is_off(struct vcpu_locked vcpu) |
| 101 | { |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 102 | return (vcpu.vcpu->state == VCPU_STATE_OFF); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Starts a vCPU of a secondary VM. |
| 107 | * |
| 108 | * Returns true if the secondary was reset and started, or false if it was |
| 109 | * already on and so nothing was done. |
| 110 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 111 | bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked, |
| 112 | ipaddr_t entry, uintreg_t arg) |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 113 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 114 | struct vm *vm = vcpu_locked.vcpu->vm; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 115 | bool vcpu_was_off; |
| 116 | |
| 117 | CHECK(vm->id != HF_PRIMARY_VM_ID); |
| 118 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 119 | vcpu_was_off = vcpu_is_off(vcpu_locked); |
| 120 | if (vcpu_was_off) { |
| 121 | /* |
| 122 | * Set vCPU registers to a clean state ready for boot. As this |
| 123 | * is a secondary which can migrate between pCPUs, the ID of the |
| 124 | * vCPU is defined as the index and does not match the ID of the |
| 125 | * pCPU it is running on. |
| 126 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 127 | arch_regs_reset(vcpu_locked.vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 128 | vcpu_on(vcpu_locked, entry, arg); |
| 129 | } |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 130 | |
| 131 | return vcpu_was_off; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Handles a page fault. It does so by determining if it's a legitimate or |
| 136 | * spurious fault, and recovering from the latter. |
| 137 | * |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 138 | * 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] | 139 | * should be aborted. |
| 140 | */ |
| 141 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 142 | struct vcpu_fault_info *f) |
| 143 | { |
| 144 | struct vm *vm = current->vm; |
| 145 | uint32_t mode; |
| 146 | uint32_t mask = f->mode | MM_MODE_INVALID; |
| 147 | bool resume; |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 148 | struct vm_locked locked_vm; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 149 | |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 150 | locked_vm = vm_lock(vm); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Check if this is a legitimate fault, i.e., if the page table doesn't |
| 153 | * allow the access attempted by the VM. |
| 154 | * |
| 155 | * Otherwise, this is a spurious fault, likely because another CPU is |
| 156 | * updating the page table. It is responsible for issuing global TLB |
| 157 | * invalidations while holding the VM lock, so we don't need to do |
| 158 | * anything else to recover from it. (Acquiring/releasing the lock |
| 159 | * ensured that the invalidations have completed.) |
| 160 | */ |
Raghu Krishnamurthy | b5775d2 | 2021-02-26 18:54:40 -0800 | [diff] [blame] | 161 | if (!locked_vm.vm->el0_partition) { |
| 162 | resume = vm_mem_get_mode(locked_vm, f->ipaddr, |
| 163 | ipa_add(f->ipaddr, 1), &mode) && |
| 164 | (mode & mask) == f->mode; |
| 165 | } else { |
| 166 | /* |
| 167 | * For EL0 partitions we need to get the mode for the faulting |
| 168 | * vaddr. |
| 169 | */ |
| 170 | resume = |
| 171 | vm_mem_get_mode(locked_vm, ipa_init(va_addr(f->vaddr)), |
| 172 | ipa_add(ipa_init(va_addr(f->vaddr)), 1), |
| 173 | &mode) && |
| 174 | (mode & mask) == f->mode; |
Raghu Krishnamurthy | f16b2ce | 2021-11-02 07:48:38 -0700 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * For EL0 partitions, if there is an instruction abort and the |
| 178 | * mode of the page is RWX, we don't resume since Hafnium does |
| 179 | * not allow write and executable pages. |
| 180 | */ |
| 181 | if ((f->mode == MM_MODE_X) && |
| 182 | ((mode & MM_MODE_W) == MM_MODE_W)) { |
| 183 | resume = false; |
| 184 | } |
Raghu Krishnamurthy | b5775d2 | 2021-02-26 18:54:40 -0800 | [diff] [blame] | 185 | } |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 186 | |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 187 | vm_unlock(&locked_vm); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 188 | |
| 189 | if (!resume) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 190 | dlog_warning( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 191 | "Stage-%d page fault: pc=%#lx, vmid=%#x, vcpu=%u, " |
| 192 | "vaddr=%#lx, ipaddr=%#lx, mode=%#x %#x\n", |
| 193 | current->vm->el0_partition ? 1 : 2, va_addr(f->pc), |
| 194 | vm->id, vcpu_index(current), va_addr(f->vaddr), |
| 195 | ipa_addr(f->ipaddr), f->mode, mode); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return resume; |
| 199 | } |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 200 | |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 201 | void vcpu_set_phys_core_idx(struct vcpu *vcpu) |
| 202 | { |
| 203 | arch_regs_set_gp_reg(&vcpu->regs, cpu_index(vcpu->cpu), |
| 204 | PHYS_CORE_IDX_GP_REG); |
| 205 | } |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 206 | |
| 207 | /** |
Olivier Deprez | 632249e | 2022-09-26 09:18:31 +0200 | [diff] [blame] | 208 | * Sets the designated GP register through which the vCPU expects to receive the |
| 209 | * boot info's address. |
| 210 | */ |
| 211 | void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu) |
| 212 | { |
| 213 | struct vm *vm = vcpu->vm; |
| 214 | uint32_t gp_register_num = vm->boot_info.gp_register_num; |
| 215 | |
| 216 | if (vm->boot_info.blob_addr.ipa != 0U) { |
| 217 | arch_regs_set_gp_reg(&vcpu->regs, |
| 218 | ipa_addr(vm->boot_info.blob_addr), |
| 219 | gp_register_num); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 224 | * Gets the first partition to boot, according to Boot Protocol from FFA spec. |
| 225 | */ |
| 226 | struct vcpu *vcpu_get_boot_vcpu(void) |
| 227 | { |
| 228 | return boot_vcpu; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Insert in boot list, sorted by `boot_order` parameter in the vm structure |
| 233 | * and rooted in `first_boot_vm`. |
| 234 | */ |
| 235 | void vcpu_update_boot(struct vcpu *vcpu) |
| 236 | { |
| 237 | struct vcpu *current = NULL; |
| 238 | struct vcpu *previous = NULL; |
| 239 | |
| 240 | if (boot_vcpu == NULL) { |
| 241 | boot_vcpu = vcpu; |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | current = boot_vcpu; |
| 246 | |
| 247 | while (current != NULL && |
| 248 | current->vm->boot_order <= vcpu->vm->boot_order) { |
| 249 | previous = current; |
| 250 | current = current->next_boot; |
| 251 | } |
| 252 | |
| 253 | if (previous != NULL) { |
| 254 | previous->next_boot = vcpu; |
| 255 | } else { |
| 256 | boot_vcpu = vcpu; |
| 257 | } |
| 258 | |
| 259 | vcpu->next_boot = current; |
| 260 | } |
J-Alves | 12cedae | 2023-08-04 14:37:37 +0100 | [diff] [blame] | 261 | |
J-Alves | b8730e9 | 2024-08-07 18:28:55 +0100 | [diff] [blame^] | 262 | void vcpu_interrupt_clear_decrement(struct vcpu_locked vcpu_locked, |
| 263 | uint32_t intid) |
| 264 | { |
| 265 | struct interrupts *interrupts = &(vcpu_locked.vcpu->interrupts); |
| 266 | |
| 267 | vcpu_virt_interrupt_clear_pending(interrupts, intid); |
| 268 | vcpu_interrupt_count_decrement(vcpu_locked, interrupts, intid); |
| 269 | } |
| 270 | |
J-Alves | 0247fe6 | 2024-02-23 10:21:46 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Sets the vcpu in the VCPU_STATE_RUNNING. |
J-Alves | 67a7926 | 2024-07-17 12:01:39 +0100 | [diff] [blame] | 273 | * With that, its register are set as "not available". |
| 274 | * If there are registers to be written to vCPU's context, do so. |
| 275 | * However, this action is restricted to WAITING and BLOCKED states, |
| 276 | * as such, assert accordingly. |
J-Alves | 0247fe6 | 2024-02-23 10:21:46 +0000 | [diff] [blame] | 277 | */ |
| 278 | void vcpu_set_running(struct vcpu_locked target_locked, struct ffa_value *args) |
J-Alves | 12cedae | 2023-08-04 14:37:37 +0100 | [diff] [blame] | 279 | { |
| 280 | struct vcpu *target_vcpu = target_locked.vcpu; |
| 281 | |
J-Alves | 67a7926 | 2024-07-17 12:01:39 +0100 | [diff] [blame] | 282 | if (args != NULL) { |
J-Alves | 0247fe6 | 2024-02-23 10:21:46 +0000 | [diff] [blame] | 283 | CHECK(target_vcpu->regs_available); |
J-Alves | 67a7926 | 2024-07-17 12:01:39 +0100 | [diff] [blame] | 284 | assert(target_vcpu->state == VCPU_STATE_WAITING || |
| 285 | target_vcpu->state == VCPU_STATE_BLOCKED); |
J-Alves | 0247fe6 | 2024-02-23 10:21:46 +0000 | [diff] [blame] | 286 | |
| 287 | arch_regs_set_retval(&target_vcpu->regs, *args); |
| 288 | } |
J-Alves | 12cedae | 2023-08-04 14:37:37 +0100 | [diff] [blame] | 289 | |
| 290 | /* Mark the registers as unavailable now. */ |
| 291 | target_vcpu->regs_available = false; |
| 292 | |
| 293 | /* We are about to resume target vCPU. */ |
| 294 | target_vcpu->state = VCPU_STATE_RUNNING; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Saves the current interrupt priority. |
| 299 | */ |
| 300 | void vcpu_save_interrupt_priority(struct vcpu_locked vcpu_locked, |
| 301 | uint8_t priority) |
| 302 | { |
| 303 | vcpu_locked.vcpu->priority_mask = priority; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * It injects a virtual interrupt in the vcpu if is enabled and is not pending. |
| 308 | */ |
| 309 | void vcpu_interrupt_inject(struct vcpu_locked target_locked, uint32_t intid) |
| 310 | { |
| 311 | struct vcpu *target_vcpu = target_locked.vcpu; |
| 312 | struct interrupts *interrupts = &target_vcpu->interrupts; |
| 313 | |
| 314 | /* |
| 315 | * We only need to change state and (maybe) trigger a virtual interrupt |
| 316 | * if it is enabled and was not previously pending. Otherwise we can |
| 317 | * skip everything except setting the pending bit. |
| 318 | */ |
| 319 | if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) && |
| 320 | !vcpu_is_virt_interrupt_pending(interrupts, intid))) { |
| 321 | goto out; |
| 322 | } |
| 323 | |
| 324 | /* Increment the count. */ |
| 325 | vcpu_interrupt_count_increment(target_locked, interrupts, intid); |
| 326 | |
| 327 | /* |
| 328 | * Only need to update state if there was not already an |
| 329 | * interrupt enabled and pending. |
| 330 | */ |
| 331 | if (vcpu_interrupt_count_get(target_locked) != 1) { |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | out: |
| 336 | /* Either way, make it pending. */ |
| 337 | vcpu_virt_interrupt_set_pending(interrupts, intid); |
| 338 | } |
| 339 | |
J-Alves | 12cedae | 2023-08-04 14:37:37 +0100 | [diff] [blame] | 340 | void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked) |
| 341 | { |
| 342 | struct vcpu *target_vcpu = vcpu_locked.vcpu; |
| 343 | |
| 344 | assert(target_vcpu->scheduling_mode == NONE); |
| 345 | assert(target_vcpu->call_chain.prev_node == NULL); |
| 346 | assert(target_vcpu->call_chain.next_node == NULL); |
| 347 | assert(target_vcpu->rt_model == RTM_NONE); |
| 348 | |
| 349 | target_vcpu->scheduling_mode = SPMC_MODE; |
| 350 | target_vcpu->rt_model = RTM_SEC_INTERRUPT; |
| 351 | } |
Madhukar Pappireddy | 32913cb | 2024-07-19 13:04:05 -0500 | [diff] [blame] | 352 | |
| 353 | static uint16_t queue_increment_index(uint16_t current_idx) |
| 354 | { |
| 355 | /* Look at the next index. Wrap around if necessary. */ |
| 356 | if (current_idx == VINT_QUEUE_MAX - 1) { |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | return current_idx + 1; |
| 361 | } |
| 362 | |
| 363 | static bool is_queue_empty(struct interrupt_queue *q) |
| 364 | { |
| 365 | if (q->head == q->tail) { |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Queue the pending virtual interrupt for target vCPU. |
| 374 | * |
| 375 | * Returns true if successful in pushing a new entry to the queue, or false |
| 376 | * otherwise. |
| 377 | */ |
| 378 | bool vcpu_interrupt_queue_push(struct vcpu_locked vcpu_locked, uint32_t vint_id) |
| 379 | { |
| 380 | struct interrupt_queue *q; |
| 381 | uint16_t new_tail; |
| 382 | |
| 383 | assert(vint_id != HF_INVALID_INTID); |
| 384 | |
| 385 | q = &vcpu_locked.vcpu->interrupts.vint_q; |
| 386 | |
| 387 | /* |
| 388 | * A new entry is pushed at the tail of the queue. Upon successful |
| 389 | * push operation, the tail increments or wraps around. |
| 390 | */ |
| 391 | new_tail = queue_increment_index(q->tail); |
| 392 | |
| 393 | /* If new_tail reaches head of the queue, then the queue is full. */ |
| 394 | if (new_tail == q->head) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /* Add the virtual interrupt to the queue. */ |
| 399 | q->vint_buffer[q->tail] = vint_id; |
| 400 | q->tail = new_tail; |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Remove an entry from the specified vCPU's queue at the head. |
| 407 | * |
| 408 | * Returns true if successful in removing the entry, or false otherwise. |
| 409 | */ |
| 410 | bool vcpu_interrupt_queue_pop(struct vcpu_locked vcpu_locked, uint32_t *vint_id) |
| 411 | { |
| 412 | struct interrupt_queue *q; |
| 413 | uint16_t new_head; |
| 414 | |
| 415 | assert(vint_id != NULL); |
| 416 | |
| 417 | q = &vcpu_locked.vcpu->interrupts.vint_q; |
| 418 | |
| 419 | /* Check if queue is empty. */ |
| 420 | if (is_queue_empty(q)) { |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * An entry is removed from the head of the queue. Once successful, the |
| 426 | * head is incremented or wrapped around if needed. |
| 427 | */ |
| 428 | new_head = queue_increment_index(q->head); |
| 429 | *vint_id = q->vint_buffer[q->head]; |
| 430 | q->head = new_head; |
| 431 | |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Look for the first pending virtual interrupt from the vcpu's queue. Note |
| 437 | * that the entry is not removed from the queue. |
| 438 | * |
| 439 | * Returns true if a valid entry exists in the queue, or false otherwise. |
| 440 | */ |
| 441 | bool vcpu_interrupt_queue_peek(struct vcpu_locked vcpu_locked, |
| 442 | uint32_t *vint_id) |
| 443 | { |
| 444 | struct interrupt_queue *q; |
| 445 | uint32_t queued_vint; |
| 446 | |
| 447 | assert(vint_id != NULL); |
| 448 | |
| 449 | q = &vcpu_locked.vcpu->interrupts.vint_q; |
| 450 | |
| 451 | /* Check if queue is empty. */ |
| 452 | if (is_queue_empty(q)) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | queued_vint = q->vint_buffer[q->head]; |
| 457 | assert(queued_vint != HF_INVALID_INTID); |
| 458 | |
| 459 | *vint_id = queued_vint; |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Find if a specific virtual interrupt exists in the specified vCPU's queue. |
| 465 | * |
| 466 | * Returns true if such an entry exists in the queue, or false otherwise. |
| 467 | */ |
| 468 | bool vcpu_is_interrupt_in_queue(struct vcpu_locked vcpu_locked, |
| 469 | uint32_t vint_id) |
| 470 | { |
| 471 | struct interrupt_queue *q; |
| 472 | uint16_t next; |
| 473 | |
| 474 | assert(vint_id != HF_INVALID_INTID); |
| 475 | |
| 476 | q = &vcpu_locked.vcpu->interrupts.vint_q; |
| 477 | |
| 478 | /* Check if the queue is empty. */ |
| 479 | if (is_queue_empty(q)) { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | next = q->head; |
| 484 | while (true) { |
| 485 | /* Match found. */ |
| 486 | if (q->vint_buffer[next] == vint_id) { |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | next = queue_increment_index(next); |
| 491 | |
| 492 | /* Reached the end of queue. */ |
| 493 | if (next == q->tail) { |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Check if there are any entries in the interrupt queue. |
| 503 | * |
| 504 | * Returns true if queue is empty, or false otherwise. |
| 505 | */ |
| 506 | bool vcpu_is_interrupt_queue_empty(struct vcpu_locked vcpu_locked) |
| 507 | { |
| 508 | struct interrupt_queue *q; |
| 509 | |
| 510 | q = &vcpu_locked.vcpu->interrupts.vint_q; |
| 511 | |
| 512 | if (is_queue_empty(q)) { |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | return false; |
| 517 | } |
J-Alves | 3b31f09 | 2024-08-07 13:26:29 +0100 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * When interrupt handling is complete the preempted_vcpu field should go back |
| 521 | * to NULL. |
| 522 | */ |
| 523 | void vcpu_secure_interrupt_complete(struct vcpu_locked vcpu_locked) |
| 524 | { |
| 525 | struct vcpu *vcpu; |
| 526 | |
| 527 | vcpu = vcpu_locked.vcpu; |
| 528 | vcpu->preempted_vcpu = NULL; |
J-Alves | ac94075 | 2024-08-07 14:02:51 +0100 | [diff] [blame] | 529 | vcpu->requires_deactivate_call = false; |
J-Alves | 3b31f09 | 2024-08-07 13:26:29 +0100 | [diff] [blame] | 530 | } |