blob: 3175ac24d24a0185cb2b5f5b8639b0fd6ca84c92 [file] [log] [blame]
Fuad Tabba5c738432019-12-02 11:02:42 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Tabba5c738432019-12-02 11:02:42 +00007 */
8
9#include "hf/vcpu.h"
10
Olivier Depreze6f7b9d2021-02-01 11:55:48 +010011#include "hf/arch/cpu.h"
12
Fuad Tabba5c738432019-12-02 11:02:42 +000013#include "hf/check.h"
14#include "hf/dlog.h"
15#include "hf/std.h"
16#include "hf/vm.h"
17
J-Alves7ac49052022-02-08 17:20:53 +000018/** 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 Tabba5c738432019-12-02 11:02:42 +000021/**
22 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
23 */
24struct 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 Deprez0b6f10a2020-08-05 18:21:33 +020036 * Locks two vCPUs ensuring that the locking order is according to the locks'
37 * addresses.
38 */
39struct 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 Tabba5c738432019-12-02 11:02:42 +000051 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
52 * reflect the fact that the vCPU is no longer locked.
53 */
54void vcpu_unlock(struct vcpu_locked *locked)
55{
56 sl_unlock(&locked->vcpu->lock);
57 locked->vcpu = NULL;
58}
59
60void 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;
Kathleen Capellae468c112023-12-13 17:56:28 -050067 vcpu->direct_request_origin.is_ffa_req2 = false;
68 vcpu->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezb2808332023-02-02 15:25:40 +010069 vcpu->rt_model = RTM_SP_INIT;
Madhukar Pappireddyeed861e2024-09-25 13:50:54 -050070 list_init(&vcpu->timer_node);
Daniel Boulby7011b5a2024-10-15 18:27:26 +010071 list_init(&vcpu->ipi_list_node);
Fuad Tabba5c738432019-12-02 11:02:42 +000072}
73
74/**
75 * Initialise the registers for the given vCPU and set the state to
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050076 * VCPU_STATE_WAITING. The caller must hold the vCPU lock while calling this.
Fuad Tabba5c738432019-12-02 11:02:42 +000077 */
78void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
79{
80 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050081 vcpu.vcpu->state = VCPU_STATE_WAITING;
Fuad Tabba5c738432019-12-02 11:02:42 +000082}
83
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010084ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
Fuad Tabba5c738432019-12-02 11:02:42 +000085{
86 size_t index = vcpu - vcpu->vm->vcpus;
87
88 CHECK(index < UINT16_MAX);
89 return index;
90}
91
92/**
93 * Check whether the given vcpu_state is an off state, for the purpose of
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050094 * turning vCPUs on and off. Note that Aborted still counts as ON for the
95 * purposes of PSCI, because according to the PSCI specification (section
Olivier Depreze7eb1682022-03-16 17:09:03 +010096 * 5.7.1) a core is only considered to be off if it has been turned off
97 * with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call.
Fuad Tabba5c738432019-12-02 11:02:42 +000098 */
99bool vcpu_is_off(struct vcpu_locked vcpu)
100{
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500101 return (vcpu.vcpu->state == VCPU_STATE_OFF);
Fuad Tabba5c738432019-12-02 11:02:42 +0000102}
103
104/**
105 * Starts a vCPU of a secondary VM.
106 *
107 * Returns true if the secondary was reset and started, or false if it was
108 * already on and so nothing was done.
109 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100110bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
111 ipaddr_t entry, uintreg_t arg)
Fuad Tabba5c738432019-12-02 11:02:42 +0000112{
Max Shvetsov40108e72020-08-27 12:39:50 +0100113 struct vm *vm = vcpu_locked.vcpu->vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000114 bool vcpu_was_off;
115
116 CHECK(vm->id != HF_PRIMARY_VM_ID);
117
Fuad Tabba5c738432019-12-02 11:02:42 +0000118 vcpu_was_off = vcpu_is_off(vcpu_locked);
119 if (vcpu_was_off) {
120 /*
121 * Set vCPU registers to a clean state ready for boot. As this
122 * is a secondary which can migrate between pCPUs, the ID of the
123 * vCPU is defined as the index and does not match the ID of the
124 * pCPU it is running on.
125 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100126 arch_regs_reset(vcpu_locked.vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000127 vcpu_on(vcpu_locked, entry, arg);
128 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000129
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 *
Fuad Tabbaed294af2019-12-20 10:43:01 +0000137 * Returns true if the caller should resume the current vCPU, or false if its VM
Fuad Tabba5c738432019-12-02 11:02:42 +0000138 * should be aborted.
139 */
140bool 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;
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800147 struct vm_locked locked_vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000148
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800149 locked_vm = vm_lock(vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000150 /*
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 */
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800160 if (!locked_vm.vm->el0_partition) {
161 resume = vm_mem_get_mode(locked_vm, f->ipaddr,
162 ipa_add(f->ipaddr, 1), &mode) &&
163 (mode & mask) == f->mode;
164 } else {
165 /*
166 * For EL0 partitions we need to get the mode for the faulting
167 * vaddr.
168 */
169 resume =
170 vm_mem_get_mode(locked_vm, ipa_init(va_addr(f->vaddr)),
171 ipa_add(ipa_init(va_addr(f->vaddr)), 1),
172 &mode) &&
173 (mode & mask) == f->mode;
Raghu Krishnamurthyf16b2ce2021-11-02 07:48:38 -0700174
175 /*
176 * For EL0 partitions, if there is an instruction abort and the
177 * mode of the page is RWX, we don't resume since Hafnium does
178 * not allow write and executable pages.
179 */
180 if ((f->mode == MM_MODE_X) &&
181 ((mode & MM_MODE_W) == MM_MODE_W)) {
182 resume = false;
183 }
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800184 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000185
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800186 vm_unlock(&locked_vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000187
188 if (!resume) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000189 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +0000190 "Stage-%d page fault: pc=%#lx, vmid=%#x, vcpu=%u, "
191 "vaddr=%#lx, ipaddr=%#lx, mode=%#x %#x\n",
192 current->vm->el0_partition ? 1 : 2, va_addr(f->pc),
193 vm->id, vcpu_index(current), va_addr(f->vaddr),
194 ipa_addr(f->ipaddr), f->mode, mode);
Fuad Tabba5c738432019-12-02 11:02:42 +0000195 }
196
197 return resume;
198}
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200199
J-Alves7ac49052022-02-08 17:20:53 +0000200void vcpu_set_phys_core_idx(struct vcpu *vcpu)
201{
202 arch_regs_set_gp_reg(&vcpu->regs, cpu_index(vcpu->cpu),
203 PHYS_CORE_IDX_GP_REG);
204}
Olivier Deprez181074b2023-02-02 14:53:23 +0100205
206/**
Olivier Deprez632249e2022-09-26 09:18:31 +0200207 * Sets the designated GP register through which the vCPU expects to receive the
208 * boot info's address.
209 */
210void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu)
211{
212 struct vm *vm = vcpu->vm;
213 uint32_t gp_register_num = vm->boot_info.gp_register_num;
214
215 if (vm->boot_info.blob_addr.ipa != 0U) {
216 arch_regs_set_gp_reg(&vcpu->regs,
217 ipa_addr(vm->boot_info.blob_addr),
218 gp_register_num);
219 }
220}
221
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000222static void vcpu_virt_interrupt_set_pending(struct interrupts *interrupts,
223 uint32_t intid)
224{
225 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
226}
227
228static void vcpu_virt_interrupt_clear_pending(struct interrupts *interrupts,
229 uint32_t intid)
230{
231 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
232}
233
234static void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
235{
236 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
237}
238
239static void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
240{
241 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
242}
243
244static void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
245{
246 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
247}
248
249static void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
250{
251 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
252}
253
254static void vcpu_interrupt_count_increment(struct vcpu_locked vcpu_locked,
255 uint32_t intid)
256{
257 struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts;
258
259 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
260 INTERRUPT_TYPE_IRQ) {
261 vcpu_irq_count_increment(vcpu_locked);
262 } else {
263 vcpu_fiq_count_increment(vcpu_locked);
264 }
265}
266
267static void vcpu_interrupt_count_decrement(struct vcpu_locked vcpu_locked,
268 uint32_t intid)
269{
270 struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts;
271
272 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
273 INTERRUPT_TYPE_IRQ) {
274 vcpu_irq_count_decrement(vcpu_locked);
275 } else {
276 vcpu_fiq_count_decrement(vcpu_locked);
277 }
278}
279
280uint32_t vcpu_virt_interrupt_irq_count_get(struct vcpu_locked vcpu_locked)
281{
282 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
283}
284
285uint32_t vcpu_virt_interrupt_fiq_count_get(struct vcpu_locked vcpu_locked)
286{
287 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
288}
289
290uint32_t vcpu_virt_interrupt_count_get(struct vcpu_locked vcpu_locked)
291{
292 return vcpu_virt_interrupt_irq_count_get(vcpu_locked) +
293 vcpu_virt_interrupt_fiq_count_get(vcpu_locked);
294}
295
J-Alvesb8730e92024-08-07 18:28:55 +0100296void vcpu_interrupt_clear_decrement(struct vcpu_locked vcpu_locked,
297 uint32_t intid)
298{
299 struct interrupts *interrupts = &(vcpu_locked.vcpu->interrupts);
300
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100301 /* Clear any specifics for the current intid. */
302 switch (intid) {
303 case HF_IPI_INTID:
304 vcpu_ipi_clear_info_get_retrieved(vcpu_locked);
305 break;
306 default:
307 /* Do no additional work. */
308 break;
309 }
310
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000311 /*
312 * Mark the virtual interrupt as no longer pending and decrement
313 * the interrupt count.
314 */
J-Alvesb8730e92024-08-07 18:28:55 +0100315 vcpu_virt_interrupt_clear_pending(interrupts, intid);
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000316 vcpu_interrupt_count_decrement(vcpu_locked, intid);
J-Alvesb8730e92024-08-07 18:28:55 +0100317}
318
J-Alves0247fe62024-02-23 10:21:46 +0000319/**
320 * Sets the vcpu in the VCPU_STATE_RUNNING.
J-Alves67a79262024-07-17 12:01:39 +0100321 * With that, its register are set as "not available".
322 * If there are registers to be written to vCPU's context, do so.
323 * However, this action is restricted to WAITING and BLOCKED states,
324 * as such, assert accordingly.
J-Alves0247fe62024-02-23 10:21:46 +0000325 */
J-Alves478faac2024-10-23 10:35:57 +0100326void vcpu_set_running(struct vcpu_locked target_locked,
327 const struct ffa_value *args)
J-Alves12cedae2023-08-04 14:37:37 +0100328{
329 struct vcpu *target_vcpu = target_locked.vcpu;
330
J-Alves67a79262024-07-17 12:01:39 +0100331 if (args != NULL) {
J-Alves0247fe62024-02-23 10:21:46 +0000332 CHECK(target_vcpu->regs_available);
J-Alves67a79262024-07-17 12:01:39 +0100333 assert(target_vcpu->state == VCPU_STATE_WAITING ||
334 target_vcpu->state == VCPU_STATE_BLOCKED);
J-Alves0247fe62024-02-23 10:21:46 +0000335
336 arch_regs_set_retval(&target_vcpu->regs, *args);
337 }
J-Alves12cedae2023-08-04 14:37:37 +0100338
339 /* Mark the registers as unavailable now. */
340 target_vcpu->regs_available = false;
341
342 /* We are about to resume target vCPU. */
343 target_vcpu->state = VCPU_STATE_RUNNING;
344}
345
J-Alves12cedae2023-08-04 14:37:37 +0100346void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked)
347{
348 struct vcpu *target_vcpu = vcpu_locked.vcpu;
349
350 assert(target_vcpu->scheduling_mode == NONE);
351 assert(target_vcpu->call_chain.prev_node == NULL);
352 assert(target_vcpu->call_chain.next_node == NULL);
353 assert(target_vcpu->rt_model == RTM_NONE);
354
355 target_vcpu->scheduling_mode = SPMC_MODE;
356 target_vcpu->rt_model = RTM_SEC_INTERRUPT;
357}
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500358
359static uint16_t queue_increment_index(uint16_t current_idx)
360{
361 /* Look at the next index. Wrap around if necessary. */
362 if (current_idx == VINT_QUEUE_MAX - 1) {
363 return 0;
364 }
365
366 return current_idx + 1;
367}
368
369static bool is_queue_empty(struct interrupt_queue *q)
370{
371 if (q->head == q->tail) {
372 return true;
373 }
374
375 return false;
376}
377
378/**
379 * Queue the pending virtual interrupt for target vCPU.
380 *
381 * Returns true if successful in pushing a new entry to the queue, or false
382 * otherwise.
383 */
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000384static bool vcpu_interrupt_queue_push(struct vcpu_locked vcpu_locked,
385 uint32_t vint_id)
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500386{
387 struct interrupt_queue *q;
388 uint16_t new_tail;
389
390 assert(vint_id != HF_INVALID_INTID);
391
392 q = &vcpu_locked.vcpu->interrupts.vint_q;
393
394 /*
395 * A new entry is pushed at the tail of the queue. Upon successful
396 * push operation, the tail increments or wraps around.
397 */
398 new_tail = queue_increment_index(q->tail);
399
400 /* If new_tail reaches head of the queue, then the queue is full. */
401 if (new_tail == q->head) {
402 return false;
403 }
404
405 /* Add the virtual interrupt to the queue. */
406 q->vint_buffer[q->tail] = vint_id;
407 q->tail = new_tail;
408
409 return true;
410}
411
412/**
413 * Remove an entry from the specified vCPU's queue at the head.
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500414 * Returns true if successful in removing the entry, or false otherwise.
415 */
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000416static bool vcpu_interrupt_queue_pop(struct vcpu_locked vcpu_locked,
417 uint32_t *vint_id)
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500418{
419 struct interrupt_queue *q;
420 uint16_t new_head;
421
422 assert(vint_id != NULL);
423
424 q = &vcpu_locked.vcpu->interrupts.vint_q;
425
426 /* Check if queue is empty. */
427 if (is_queue_empty(q)) {
428 return false;
429 }
430
431 /*
432 * An entry is removed from the head of the queue. Once successful, the
433 * head is incremented or wrapped around if needed.
434 */
435 new_head = queue_increment_index(q->head);
436 *vint_id = q->vint_buffer[q->head];
437 q->head = new_head;
438
439 return true;
440}
441
442/**
443 * Look for the first pending virtual interrupt from the vcpu's queue. Note
444 * that the entry is not removed from the queue.
445 *
446 * Returns true if a valid entry exists in the queue, or false otherwise.
447 */
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000448static bool vcpu_interrupt_queue_peek(struct vcpu_locked vcpu_locked,
449 uint32_t *vint_id)
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500450{
451 struct interrupt_queue *q;
452 uint32_t queued_vint;
453
454 assert(vint_id != NULL);
455
456 q = &vcpu_locked.vcpu->interrupts.vint_q;
457
458 /* Check if queue is empty. */
459 if (is_queue_empty(q)) {
460 return false;
461 }
462
463 queued_vint = q->vint_buffer[q->head];
464 assert(queued_vint != HF_INVALID_INTID);
465
466 *vint_id = queued_vint;
467 return true;
468}
469
470/**
J-Alves3b31f092024-08-07 13:26:29 +0100471 * When interrupt handling is complete the preempted_vcpu field should go back
472 * to NULL.
473 */
474void vcpu_secure_interrupt_complete(struct vcpu_locked vcpu_locked)
475{
476 struct vcpu *vcpu;
477
478 vcpu = vcpu_locked.vcpu;
479 vcpu->preempted_vcpu = NULL;
J-Alvesac940752024-08-07 14:02:51 +0100480 vcpu->requires_deactivate_call = false;
J-Alves3b31f092024-08-07 13:26:29 +0100481}
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000482
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000483void vcpu_virt_interrupt_enable(struct vcpu_locked vcpu_locked,
484 uint32_t vint_id, bool enable)
485{
486 struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts;
487
488 if (enable) {
489 /*
490 * If it is pending and was not enabled before, increment the
491 * count.
492 */
493 if (vcpu_is_virt_interrupt_pending(interrupts, vint_id) &&
494 !vcpu_is_virt_interrupt_enabled(interrupts, vint_id)) {
495 vcpu_interrupt_count_increment(vcpu_locked, vint_id);
496 }
497 vcpu_virt_interrupt_set_enabled(interrupts, vint_id);
498 } else {
499 /*
500 * If it is pending and was enabled before, decrement the count.
501 */
502 if (vcpu_is_virt_interrupt_pending(interrupts, vint_id) &&
503 vcpu_is_virt_interrupt_enabled(interrupts, vint_id)) {
504 vcpu_interrupt_count_decrement(vcpu_locked, vint_id);
505 }
506 vcpu_virt_interrupt_clear_enabled(interrupts, vint_id);
507 }
508}
509
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000510/*
511 * Find and return the first intid that is pending and enabled, the interrupt
512 * struct for this intid will be at the head of the list so can be popped.
513 * Intid returned in the vint_id argument.
514 * True returned if a pending and enabled interrupt is found. False otherwise.
515 */
516uint32_t vcpu_virt_interrupt_peek_pending_and_enabled(
517 struct vcpu_locked vcpu_locked)
518{
519 uint32_t vint_id = HF_INVALID_INTID;
520 struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts;
521 uint32_t pending_and_enabled_count =
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000522 vcpu_virt_interrupt_count_get(vcpu_locked);
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000523
524 /* First check there is a pending and enabled interrupt to return. */
525 if (pending_and_enabled_count == 0) {
526 return HF_INVALID_INTID;
527 }
528
529 /*
530 * We know here there is a pending and enabled interrupt in
531 * the queue. So push any interrupts that are not enabled to
532 * the back of the queue until we reach the first enabled one.
533 */
534 while (vcpu_interrupt_queue_peek(vcpu_locked, &vint_id) &&
535 !vcpu_is_virt_interrupt_enabled(interrupts, vint_id)) {
536 /* Push disabled interrupt to the back of the queue. */
537 vcpu_interrupt_queue_pop(vcpu_locked, &vint_id);
538 vcpu_interrupt_queue_push(vcpu_locked, vint_id);
539 }
540
541 assert(vint_id != HF_INVALID_INTID);
542
543 return vint_id;
544}
545
546/*
547 * Get the next pending and enabled virtual interrupt ID.
548 * Pops from the queue and clears the bitmap.
549 */
550uint32_t vcpu_virt_interrupt_get_pending_and_enabled(
551 struct vcpu_locked vcpu_locked)
552{
553 uint32_t vint_id =
554 vcpu_virt_interrupt_peek_pending_and_enabled(vcpu_locked);
555
556 if (vint_id != HF_INVALID_INTID) {
557 vcpu_interrupt_queue_pop(vcpu_locked, &vint_id);
558 vcpu_interrupt_clear_decrement(vcpu_locked, vint_id);
559 }
560
561 return vint_id;
562}
563
564/*
565 * Set a virtual interrupt to pending. Add it to the queue and set the bitmap.
566 */
567void vcpu_virt_interrupt_inject(struct vcpu_locked vcpu_locked,
568 uint32_t vint_id)
569{
570 struct interrupts *interrupts = &vcpu_locked.vcpu->interrupts;
571
572 /*
573 * An interrupt can only be pending once so return if it is
574 * already pending.
575 */
576 if (vcpu_is_virt_interrupt_pending(interrupts, vint_id)) {
577 return;
578 }
579
580 /* Push to the queue and set the bitmap. */
581 if (!vcpu_interrupt_queue_push(vcpu_locked, vint_id)) {
582 panic("Exhausted interrupt queue for vCPU %d of SP %#x\n",
583 vcpu_locked.vcpu->cpu->id, vcpu_locked.vcpu->vm->id);
584 }
585 vcpu_virt_interrupt_set_pending(interrupts, vint_id);
586
587 if (vcpu_is_virt_interrupt_enabled(interrupts, vint_id)) {
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000588 vcpu_interrupt_count_increment(vcpu_locked, vint_id);
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000589 }
590}