2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
6 * Copyright (c) 2019 Joyent, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_bhyve_snapshot.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
45 #include <x86/specialreg.h>
46 #include <x86/apicreg.h>
48 #include <machine/clock.h>
49 #include <machine/smp.h>
51 #include <machine/vmm.h>
52 #include <machine/vmm_snapshot.h>
54 #include "vmm_lapic.h"
59 #include "vlapic_priv.h"
62 #define PRIO(x) ((x) >> 4)
64 #define VLAPIC_VERSION (0x14)
66 #define x2apic(vlapic) (((vlapic)->msr_apicbase & APICBASE_X2APIC) ? 1 : 0)
69 * The 'vlapic->timer_mtx' is used to provide mutual exclusion between the
70 * vlapic_callout_handler() and vcpu accesses to:
71 * - timer_freq_bt, timer_period_bt, timer_fire_bt
72 * - timer LVT register
74 #define VLAPIC_TIMER_LOCK(vlapic) mtx_lock_spin(&((vlapic)->timer_mtx))
75 #define VLAPIC_TIMER_UNLOCK(vlapic) mtx_unlock_spin(&((vlapic)->timer_mtx))
76 #define VLAPIC_TIMER_LOCKED(vlapic) mtx_owned(&((vlapic)->timer_mtx))
79 * APIC timer frequency:
80 * - arbitrary but chosen to be in the ballpark of contemporary hardware.
81 * - power-of-two to avoid loss of precision when converted to a bintime.
83 #define VLAPIC_BUS_FREQ (128 * 1024 * 1024)
85 static void vlapic_set_error(struct vlapic *, uint32_t, bool);
86 static void vlapic_callout_handler(void *arg);
87 static void vlapic_reset(struct vlapic *vlapic);
89 static __inline uint32_t
90 vlapic_get_id(struct vlapic *vlapic)
94 return (vlapic->vcpuid);
96 return (vlapic->vcpuid << 24);
100 x2apic_ldr(struct vlapic *vlapic)
105 apicid = vlapic_get_id(vlapic);
106 ldr = 1 << (apicid & 0xf);
107 ldr |= (apicid & 0xffff0) << 12;
112 vlapic_dfr_write_handler(struct vlapic *vlapic)
116 lapic = vlapic->apic_page;
117 if (x2apic(vlapic)) {
118 VM_CTR1(vlapic->vm, "ignoring write to DFR in x2apic mode: %#x",
124 lapic->dfr &= APIC_DFR_MODEL_MASK;
125 lapic->dfr |= APIC_DFR_RESERVED;
127 if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_FLAT)
128 VLAPIC_CTR0(vlapic, "vlapic DFR in Flat Model");
129 else if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_CLUSTER)
130 VLAPIC_CTR0(vlapic, "vlapic DFR in Cluster Model");
132 VLAPIC_CTR1(vlapic, "DFR in Unknown Model %#x", lapic->dfr);
136 vlapic_ldr_write_handler(struct vlapic *vlapic)
140 lapic = vlapic->apic_page;
142 /* LDR is read-only in x2apic mode */
143 if (x2apic(vlapic)) {
144 VLAPIC_CTR1(vlapic, "ignoring write to LDR in x2apic mode: %#x",
146 lapic->ldr = x2apic_ldr(vlapic);
148 lapic->ldr &= ~APIC_LDR_RESERVED;
149 VLAPIC_CTR1(vlapic, "vlapic LDR set to %#x", lapic->ldr);
154 vlapic_id_write_handler(struct vlapic *vlapic)
159 * We don't allow the ID register to be modified so reset it back to
162 lapic = vlapic->apic_page;
163 lapic->id = vlapic_get_id(vlapic);
167 vlapic_timer_divisor(uint32_t dcr)
187 panic("vlapic_timer_divisor: invalid dcr 0x%08x", dcr);
193 vlapic_dump_lvt(uint32_t offset, uint32_t *lvt)
195 printf("Offset %x: lvt %08x (V:%02x DS:%x M:%x)\n", offset,
196 *lvt, *lvt & APIC_LVTT_VECTOR, *lvt & APIC_LVTT_DS,
202 vlapic_get_ccr(struct vlapic *vlapic)
204 struct bintime bt_now, bt_rem;
205 struct LAPIC *lapic __diagused;
209 lapic = vlapic->apic_page;
211 VLAPIC_TIMER_LOCK(vlapic);
212 if (callout_active(&vlapic->callout)) {
214 * If the timer is scheduled to expire in the future then
215 * compute the value of 'ccr' based on the remaining time.
218 if (bintime_cmp(&vlapic->timer_fire_bt, &bt_now, >)) {
219 bt_rem = vlapic->timer_fire_bt;
220 bintime_sub(&bt_rem, &bt_now);
221 ccr += bt_rem.sec * BT2FREQ(&vlapic->timer_freq_bt);
222 ccr += bt_rem.frac / vlapic->timer_freq_bt.frac;
225 KASSERT(ccr <= lapic->icr_timer, ("vlapic_get_ccr: invalid ccr %#x, "
226 "icr_timer is %#x", ccr, lapic->icr_timer));
227 VLAPIC_CTR2(vlapic, "vlapic ccr_timer = %#x, icr_timer = %#x",
228 ccr, lapic->icr_timer);
229 VLAPIC_TIMER_UNLOCK(vlapic);
234 vlapic_dcr_write_handler(struct vlapic *vlapic)
239 lapic = vlapic->apic_page;
240 VLAPIC_TIMER_LOCK(vlapic);
242 divisor = vlapic_timer_divisor(lapic->dcr_timer);
243 VLAPIC_CTR2(vlapic, "vlapic dcr_timer=%#x, divisor=%d",
244 lapic->dcr_timer, divisor);
247 * Update the timer frequency and the timer period.
249 * XXX changes to the frequency divider will not take effect until
250 * the timer is reloaded.
252 FREQ2BT(VLAPIC_BUS_FREQ / divisor, &vlapic->timer_freq_bt);
253 vlapic->timer_period_bt = vlapic->timer_freq_bt;
254 bintime_mul(&vlapic->timer_period_bt, lapic->icr_timer);
256 VLAPIC_TIMER_UNLOCK(vlapic);
260 vlapic_esr_write_handler(struct vlapic *vlapic)
264 lapic = vlapic->apic_page;
265 lapic->esr = vlapic->esr_pending;
266 vlapic->esr_pending = 0;
270 vlapic_set_intr_ready(struct vlapic *vlapic, int vector, bool level)
273 uint32_t *irrptr, *tmrptr, mask;
276 KASSERT(vector >= 0 && vector < 256, ("invalid vector %d", vector));
278 lapic = vlapic->apic_page;
279 if (!(lapic->svr & APIC_SVR_ENABLE)) {
280 VLAPIC_CTR1(vlapic, "vlapic is software disabled, ignoring "
281 "interrupt %d", vector);
286 vlapic_set_error(vlapic, APIC_ESR_RECEIVE_ILLEGAL_VECTOR,
288 VLAPIC_CTR1(vlapic, "vlapic ignoring interrupt to vector %d",
293 if (vlapic->ops.set_intr_ready)
294 return ((*vlapic->ops.set_intr_ready)(vlapic, vector, level));
296 idx = (vector / 32) * 4;
297 mask = 1 << (vector % 32);
299 irrptr = &lapic->irr0;
300 atomic_set_int(&irrptr[idx], mask);
303 * Verify that the trigger-mode of the interrupt matches with
304 * the vlapic TMR registers.
306 tmrptr = &lapic->tmr0;
307 if ((tmrptr[idx] & mask) != (level ? mask : 0)) {
308 VLAPIC_CTR3(vlapic, "vlapic TMR[%d] is 0x%08x but "
309 "interrupt is %s-triggered", idx / 4, tmrptr[idx],
310 level ? "level" : "edge");
313 VLAPIC_CTR_IRR(vlapic, "vlapic_set_intr_ready");
317 static __inline uint32_t *
318 vlapic_get_lvtptr(struct vlapic *vlapic, uint32_t offset)
320 struct LAPIC *lapic = vlapic->apic_page;
324 case APIC_OFFSET_CMCI_LVT:
325 return (&lapic->lvt_cmci);
326 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
327 i = (offset - APIC_OFFSET_TIMER_LVT) >> 2;
328 return ((&lapic->lvt_timer) + i);
330 panic("vlapic_get_lvt: invalid LVT\n");
335 lvt_off_to_idx(uint32_t offset)
340 case APIC_OFFSET_CMCI_LVT:
341 index = APIC_LVT_CMCI;
343 case APIC_OFFSET_TIMER_LVT:
344 index = APIC_LVT_TIMER;
346 case APIC_OFFSET_THERM_LVT:
347 index = APIC_LVT_THERMAL;
349 case APIC_OFFSET_PERF_LVT:
350 index = APIC_LVT_PMC;
352 case APIC_OFFSET_LINT0_LVT:
353 index = APIC_LVT_LINT0;
355 case APIC_OFFSET_LINT1_LVT:
356 index = APIC_LVT_LINT1;
358 case APIC_OFFSET_ERROR_LVT:
359 index = APIC_LVT_ERROR;
365 KASSERT(index >= 0 && index <= VLAPIC_MAXLVT_INDEX, ("lvt_off_to_idx: "
366 "invalid lvt index %d for offset %#x", index, offset));
371 static __inline uint32_t
372 vlapic_get_lvt(struct vlapic *vlapic, uint32_t offset)
377 idx = lvt_off_to_idx(offset);
378 val = atomic_load_acq_32(&vlapic->lvt_last[idx]);
383 vlapic_lvt_write_handler(struct vlapic *vlapic, uint32_t offset)
385 uint32_t *lvtptr, mask, val;
389 lapic = vlapic->apic_page;
390 lvtptr = vlapic_get_lvtptr(vlapic, offset);
392 idx = lvt_off_to_idx(offset);
394 if (!(lapic->svr & APIC_SVR_ENABLE))
396 mask = APIC_LVT_M | APIC_LVT_DS | APIC_LVT_VECTOR;
398 case APIC_OFFSET_TIMER_LVT:
399 mask |= APIC_LVTT_TM;
401 case APIC_OFFSET_ERROR_LVT:
403 case APIC_OFFSET_LINT0_LVT:
404 case APIC_OFFSET_LINT1_LVT:
405 mask |= APIC_LVT_TM | APIC_LVT_RIRR | APIC_LVT_IIPP;
413 atomic_store_rel_32(&vlapic->lvt_last[idx], val);
417 vlapic_mask_lvts(struct vlapic *vlapic)
419 struct LAPIC *lapic = vlapic->apic_page;
421 lapic->lvt_cmci |= APIC_LVT_M;
422 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_CMCI_LVT);
424 lapic->lvt_timer |= APIC_LVT_M;
425 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_TIMER_LVT);
427 lapic->lvt_thermal |= APIC_LVT_M;
428 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_THERM_LVT);
430 lapic->lvt_pcint |= APIC_LVT_M;
431 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_PERF_LVT);
433 lapic->lvt_lint0 |= APIC_LVT_M;
434 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_LINT0_LVT);
436 lapic->lvt_lint1 |= APIC_LVT_M;
437 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_LINT1_LVT);
439 lapic->lvt_error |= APIC_LVT_M;
440 vlapic_lvt_write_handler(vlapic, APIC_OFFSET_ERROR_LVT);
444 vlapic_fire_lvt(struct vlapic *vlapic, u_int lvt)
446 uint32_t mode, reg, vec;
448 reg = atomic_load_acq_32(&vlapic->lvt_last[lvt]);
450 if (reg & APIC_LVT_M)
452 vec = reg & APIC_LVT_VECTOR;
453 mode = reg & APIC_LVT_DM;
456 case APIC_LVT_DM_FIXED:
458 vlapic_set_error(vlapic, APIC_ESR_SEND_ILLEGAL_VECTOR,
459 lvt == APIC_LVT_ERROR);
462 if (vlapic_set_intr_ready(vlapic, vec, false))
463 vcpu_notify_event(vlapic->vm, vlapic->vcpuid, true);
465 case APIC_LVT_DM_NMI:
466 vm_inject_nmi(vlapic->vm, vlapic->vcpuid);
468 case APIC_LVT_DM_EXTINT:
469 vm_inject_extint(vlapic->vm, vlapic->vcpuid);
472 // Other modes ignored
480 dump_isrvec_stk(struct vlapic *vlapic)
485 isrptr = &vlapic->apic_page->isr0;
486 for (i = 0; i < 8; i++)
487 printf("ISR%d 0x%08x\n", i, isrptr[i * 4]);
489 for (i = 0; i <= vlapic->isrvec_stk_top; i++)
490 printf("isrvec_stk[%d] = %d\n", i, vlapic->isrvec_stk[i]);
495 * Algorithm adopted from section "Interrupt, Task and Processor Priority"
496 * in Intel Architecture Manual Vol 3a.
499 vlapic_update_ppr(struct vlapic *vlapic)
501 int isrvec, tpr, ppr;
504 * Note that the value on the stack at index 0 is always 0.
506 * This is a placeholder for the value of ISRV when none of the
507 * bits is set in the ISRx registers.
509 isrvec = vlapic->isrvec_stk[vlapic->isrvec_stk_top];
510 tpr = vlapic->apic_page->tpr;
514 int i, lastprio, curprio, vector, idx;
517 if (vlapic->isrvec_stk_top == 0 && isrvec != 0)
518 panic("isrvec_stk is corrupted: %d", isrvec);
521 * Make sure that the priority of the nested interrupts is
525 for (i = 1; i <= vlapic->isrvec_stk_top; i++) {
526 curprio = PRIO(vlapic->isrvec_stk[i]);
527 if (curprio <= lastprio) {
528 dump_isrvec_stk(vlapic);
529 panic("isrvec_stk does not satisfy invariant");
535 * Make sure that each bit set in the ISRx registers has a
536 * corresponding entry on the isrvec stack.
539 isrptr = &vlapic->apic_page->isr0;
540 for (vector = 0; vector < 256; vector++) {
541 idx = (vector / 32) * 4;
542 if (isrptr[idx] & (1 << (vector % 32))) {
543 if (i > vlapic->isrvec_stk_top ||
544 vlapic->isrvec_stk[i] != vector) {
545 dump_isrvec_stk(vlapic);
546 panic("ISR and isrvec_stk out of sync");
554 if (PRIO(tpr) >= PRIO(isrvec))
559 vlapic->apic_page->ppr = ppr;
560 VLAPIC_CTR1(vlapic, "vlapic_update_ppr 0x%02x", ppr);
564 vlapic_sync_tpr(struct vlapic *vlapic)
566 vlapic_update_ppr(vlapic);
569 static VMM_STAT(VLAPIC_GRATUITOUS_EOI, "EOI without any in-service interrupt");
572 vlapic_process_eoi(struct vlapic *vlapic)
574 struct LAPIC *lapic = vlapic->apic_page;
575 uint32_t *isrptr, *tmrptr;
576 int i, idx, bitpos, vector;
578 isrptr = &lapic->isr0;
579 tmrptr = &lapic->tmr0;
581 for (i = 7; i >= 0; i--) {
583 bitpos = fls(isrptr[idx]);
585 if (vlapic->isrvec_stk_top <= 0) {
586 panic("invalid vlapic isrvec_stk_top %d",
587 vlapic->isrvec_stk_top);
589 isrptr[idx] &= ~(1 << bitpos);
590 vector = i * 32 + bitpos;
591 VLAPIC_CTR1(vlapic, "EOI vector %d", vector);
592 VLAPIC_CTR_ISR(vlapic, "vlapic_process_eoi");
593 vlapic->isrvec_stk_top--;
594 vlapic_update_ppr(vlapic);
595 if ((tmrptr[idx] & (1 << bitpos)) != 0) {
596 vioapic_process_eoi(vlapic->vm, vlapic->vcpuid,
602 VLAPIC_CTR0(vlapic, "Gratuitous EOI");
603 vmm_stat_incr(vlapic->vcpu, VLAPIC_GRATUITOUS_EOI, 1);
607 vlapic_get_lvt_field(uint32_t lvt, uint32_t mask)
614 vlapic_periodic_timer(struct vlapic *vlapic)
618 lvt = vlapic_get_lvt(vlapic, APIC_OFFSET_TIMER_LVT);
620 return (vlapic_get_lvt_field(lvt, APIC_LVTT_TM_PERIODIC));
623 static VMM_STAT(VLAPIC_INTR_ERROR, "error interrupts generated by vlapic");
626 vlapic_set_error(struct vlapic *vlapic, uint32_t mask, bool lvt_error)
629 vlapic->esr_pending |= mask;
632 * Avoid infinite recursion if the error LVT itself is configured with
638 if (vlapic_fire_lvt(vlapic, APIC_LVT_ERROR)) {
639 vmm_stat_incr(vlapic->vcpu, VLAPIC_INTR_ERROR, 1);
643 static VMM_STAT(VLAPIC_INTR_TIMER, "timer interrupts generated by vlapic");
646 vlapic_fire_timer(struct vlapic *vlapic)
649 KASSERT(VLAPIC_TIMER_LOCKED(vlapic), ("vlapic_fire_timer not locked"));
651 if (vlapic_fire_lvt(vlapic, APIC_LVT_TIMER)) {
652 VLAPIC_CTR0(vlapic, "vlapic timer fired");
653 vmm_stat_incr(vlapic->vcpu, VLAPIC_INTR_TIMER, 1);
657 static VMM_STAT(VLAPIC_INTR_CMC,
658 "corrected machine check interrupts generated by vlapic");
661 vlapic_fire_cmci(struct vlapic *vlapic)
664 if (vlapic_fire_lvt(vlapic, APIC_LVT_CMCI)) {
665 vmm_stat_incr(vlapic->vcpu, VLAPIC_INTR_CMC, 1);
669 static VMM_STAT_ARRAY(LVTS_TRIGGERRED, VLAPIC_MAXLVT_INDEX + 1,
673 vlapic_trigger_lvt(struct vlapic *vlapic, int vector)
676 if (vlapic_enabled(vlapic) == false) {
678 * When the local APIC is global/hardware disabled,
679 * LINT[1:0] pins are configured as INTR and NMI pins,
684 vm_inject_extint(vlapic->vm, vlapic->vcpuid);
687 vm_inject_nmi(vlapic->vm, vlapic->vcpuid);
701 case APIC_LVT_THERMAL:
703 if (vlapic_fire_lvt(vlapic, vector)) {
704 vmm_stat_array_incr(vlapic->vcpu, LVTS_TRIGGERRED,
715 vlapic_callout_reset(struct vlapic *vlapic, sbintime_t t)
717 callout_reset_sbt_curcpu(&vlapic->callout, t, 0,
718 vlapic_callout_handler, vlapic, 0);
722 vlapic_callout_handler(void *arg)
724 struct vlapic *vlapic;
725 struct bintime bt, btnow;
730 VLAPIC_TIMER_LOCK(vlapic);
731 if (callout_pending(&vlapic->callout)) /* callout was reset */
734 if (!callout_active(&vlapic->callout)) /* callout was stopped */
737 callout_deactivate(&vlapic->callout);
739 vlapic_fire_timer(vlapic);
741 if (vlapic_periodic_timer(vlapic)) {
743 KASSERT(bintime_cmp(&btnow, &vlapic->timer_fire_bt, >=),
744 ("vlapic callout at %#lx.%#lx, expected at %#lx.#%lx",
745 btnow.sec, btnow.frac, vlapic->timer_fire_bt.sec,
746 vlapic->timer_fire_bt.frac));
749 * Compute the delta between when the timer was supposed to
750 * fire and the present time.
753 bintime_sub(&bt, &vlapic->timer_fire_bt);
755 rem_sbt = bttosbt(vlapic->timer_period_bt);
756 if (bintime_cmp(&bt, &vlapic->timer_period_bt, <)) {
758 * Adjust the time until the next countdown downward
759 * to account for the lost time.
761 rem_sbt -= bttosbt(bt);
764 * If the delta is greater than the timer period then
765 * just reset our time base instead of trying to catch
768 vlapic->timer_fire_bt = btnow;
769 VLAPIC_CTR2(vlapic, "vlapic timer lagging by %lu "
770 "usecs, period is %lu usecs - resetting time base",
771 bttosbt(bt) / SBT_1US,
772 bttosbt(vlapic->timer_period_bt) / SBT_1US);
775 bintime_add(&vlapic->timer_fire_bt, &vlapic->timer_period_bt);
776 vlapic_callout_reset(vlapic, rem_sbt);
779 VLAPIC_TIMER_UNLOCK(vlapic);
783 vlapic_icrtmr_write_handler(struct vlapic *vlapic)
789 VLAPIC_TIMER_LOCK(vlapic);
791 lapic = vlapic->apic_page;
792 icr_timer = lapic->icr_timer;
794 vlapic->timer_period_bt = vlapic->timer_freq_bt;
795 bintime_mul(&vlapic->timer_period_bt, icr_timer);
797 if (icr_timer != 0) {
798 binuptime(&vlapic->timer_fire_bt);
799 bintime_add(&vlapic->timer_fire_bt, &vlapic->timer_period_bt);
801 sbt = bttosbt(vlapic->timer_period_bt);
802 vlapic_callout_reset(vlapic, sbt);
804 callout_stop(&vlapic->callout);
806 VLAPIC_TIMER_UNLOCK(vlapic);
810 * This function populates 'dmask' with the set of vcpus that match the
811 * addressing specified by the (dest, phys, lowprio) tuple.
813 * 'x2apic_dest' specifies whether 'dest' is interpreted as x2APIC (32-bit)
814 * or xAPIC (8-bit) destination field.
817 vlapic_calcdest(struct vm *vm, cpuset_t *dmask, uint32_t dest, bool phys,
818 bool lowprio, bool x2apic_dest)
820 struct vlapic *vlapic;
821 uint32_t dfr, ldr, ldest, cluster;
822 uint32_t mda_flat_ldest, mda_cluster_ldest, mda_ldest, mda_cluster_id;
826 if ((x2apic_dest && dest == 0xffffffff) ||
827 (!x2apic_dest && dest == 0xff)) {
829 * Broadcast in both logical and physical modes.
831 *dmask = vm_active_cpus(vm);
837 * Physical mode: destination is APIC ID.
840 vcpuid = vm_apicid2vcpuid(vm, dest);
841 amask = vm_active_cpus(vm);
842 if (vcpuid < vm_get_maxcpus(vm) && CPU_ISSET(vcpuid, &amask))
843 CPU_SET(vcpuid, dmask);
846 * In the "Flat Model" the MDA is interpreted as an 8-bit wide
847 * bitmask. This model is only available in the xAPIC mode.
849 mda_flat_ldest = dest & 0xff;
852 * In the "Cluster Model" the MDA is used to identify a
853 * specific cluster and a set of APICs in that cluster.
856 mda_cluster_id = dest >> 16;
857 mda_cluster_ldest = dest & 0xffff;
859 mda_cluster_id = (dest >> 4) & 0xf;
860 mda_cluster_ldest = dest & 0xf;
864 * Logical mode: match each APIC that has a bit set
865 * in its LDR that matches a bit in the ldest.
868 amask = vm_active_cpus(vm);
869 CPU_FOREACH_ISSET(vcpuid, &amask) {
870 vlapic = vm_lapic(vm_vcpu(vm, vcpuid));
871 dfr = vlapic->apic_page->dfr;
872 ldr = vlapic->apic_page->ldr;
874 if ((dfr & APIC_DFR_MODEL_MASK) ==
875 APIC_DFR_MODEL_FLAT) {
877 mda_ldest = mda_flat_ldest;
878 } else if ((dfr & APIC_DFR_MODEL_MASK) ==
879 APIC_DFR_MODEL_CLUSTER) {
880 if (x2apic(vlapic)) {
882 ldest = ldr & 0xffff;
885 ldest = (ldr >> 24) & 0xf;
887 if (cluster != mda_cluster_id)
889 mda_ldest = mda_cluster_ldest;
892 * Guest has configured a bad logical
893 * model for this vcpu - skip it.
895 VLAPIC_CTR1(vlapic, "vlapic has bad logical "
896 "model %x - cannot deliver interrupt", dfr);
900 if ((mda_ldest & ldest) != 0) {
901 CPU_SET(vcpuid, dmask);
909 static VMM_STAT_ARRAY(IPIS_SENT, VM_MAXCPU, "ipis sent to vcpu");
912 vlapic_set_tpr(struct vlapic *vlapic, uint8_t val)
914 struct LAPIC *lapic = vlapic->apic_page;
916 if (lapic->tpr != val) {
917 VLAPIC_CTR2(vlapic, "vlapic TPR changed from %#x to %#x",
920 vlapic_update_ppr(vlapic);
925 vlapic_get_tpr(struct vlapic *vlapic)
927 struct LAPIC *lapic = vlapic->apic_page;
933 vlapic_set_cr8(struct vlapic *vlapic, uint64_t val)
938 vm_inject_gp(vlapic->vcpu);
943 vlapic_set_tpr(vlapic, tpr);
947 vlapic_get_cr8(struct vlapic *vlapic)
951 tpr = vlapic_get_tpr(vlapic);
956 vlapic_is_icr_valid(uint64_t icrval)
958 uint32_t mode = icrval & APIC_DELMODE_MASK;
959 uint32_t level = icrval & APIC_LEVEL_MASK;
960 uint32_t trigger = icrval & APIC_TRIGMOD_MASK;
961 uint32_t shorthand = icrval & APIC_DEST_MASK;
964 case APIC_DELMODE_FIXED:
965 if (trigger == APIC_TRIGMOD_EDGE)
968 * AMD allows a level assert IPI and Intel converts a level
969 * assert IPI into an edge IPI.
971 if (trigger == APIC_TRIGMOD_LEVEL && level == APIC_LEVEL_ASSERT)
974 case APIC_DELMODE_LOWPRIO:
975 case APIC_DELMODE_SMI:
976 case APIC_DELMODE_NMI:
977 case APIC_DELMODE_INIT:
978 if (trigger == APIC_TRIGMOD_EDGE &&
979 (shorthand == APIC_DEST_DESTFLD ||
980 shorthand == APIC_DEST_ALLESELF))
983 * AMD allows a level assert IPI and Intel converts a level
984 * assert IPI into an edge IPI.
986 if (trigger == APIC_TRIGMOD_LEVEL &&
987 level == APIC_LEVEL_ASSERT &&
988 (shorthand == APIC_DEST_DESTFLD ||
989 shorthand == APIC_DEST_ALLESELF))
992 * An level triggered deassert INIT is defined in the Intel
993 * Multiprocessor Specification and the Intel Software Developer
994 * Manual. Due to the MPS it's required to send a level assert
995 * INIT to a cpu and then a level deassert INIT. Some operating
996 * systems e.g. FreeBSD or Linux use that algorithm. According
997 * to the SDM a level deassert INIT is only supported by Pentium
998 * and P6 processors. It's always send to all cpus regardless of
999 * the destination or shorthand field. It resets the arbitration
1000 * id register. This register is not software accessible and
1001 * only required for the APIC bus arbitration. So, the level
1002 * deassert INIT doesn't need any emulation and we should ignore
1003 * it. The SDM also defines that newer processors don't support
1004 * the level deassert INIT and it's not valid any more. As it's
1005 * defined for older systems, it can't be invalid per se.
1006 * Otherwise, backward compatibility would be broken. However,
1007 * when returning false here, it'll be ignored which is the
1008 * desired behaviour.
1010 if (mode == APIC_DELMODE_INIT &&
1011 trigger == APIC_TRIGMOD_LEVEL &&
1012 level == APIC_LEVEL_DEASSERT)
1015 case APIC_DELMODE_STARTUP:
1016 if (shorthand == APIC_DEST_DESTFLD ||
1017 shorthand == APIC_DEST_ALLESELF)
1020 case APIC_DELMODE_RR:
1021 /* Only available on AMD! */
1022 if (trigger == APIC_TRIGMOD_EDGE &&
1023 shorthand == APIC_DEST_DESTFLD)
1026 case APIC_DELMODE_RESV:
1029 __assert_unreachable();
1036 vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu)
1040 cpuset_t dmask, ipimask;
1042 uint32_t dest, vec, mode, shorthand;
1043 struct vlapic *vlapic2;
1044 struct vm_exit *vmexit;
1045 struct LAPIC *lapic;
1047 lapic = vlapic->apic_page;
1048 lapic->icr_lo &= ~APIC_DELSTAT_PEND;
1049 icrval = ((uint64_t)lapic->icr_hi << 32) | lapic->icr_lo;
1052 dest = icrval >> 32;
1054 dest = icrval >> (32 + 24);
1055 vec = icrval & APIC_VECTOR_MASK;
1056 mode = icrval & APIC_DELMODE_MASK;
1057 phys = (icrval & APIC_DESTMODE_LOG) == 0;
1058 shorthand = icrval & APIC_DEST_MASK;
1060 VLAPIC_CTR2(vlapic, "icrlo 0x%016lx triggered ipi %d", icrval, vec);
1062 switch (shorthand) {
1063 case APIC_DEST_DESTFLD:
1064 vlapic_calcdest(vlapic->vm, &dmask, dest, phys, false, x2apic(vlapic));
1066 case APIC_DEST_SELF:
1067 CPU_SETOF(vlapic->vcpuid, &dmask);
1069 case APIC_DEST_ALLISELF:
1070 dmask = vm_active_cpus(vlapic->vm);
1072 case APIC_DEST_ALLESELF:
1073 dmask = vm_active_cpus(vlapic->vm);
1074 CPU_CLR(vlapic->vcpuid, &dmask);
1077 __assert_unreachable();
1081 * Ignore invalid combinations of the icr.
1083 if (!vlapic_is_icr_valid(icrval)) {
1084 VLAPIC_CTR1(vlapic, "Ignoring invalid ICR %016lx", icrval);
1089 * ipimask is a set of vCPUs needing userland handling of the current
1095 case APIC_DELMODE_FIXED:
1097 vlapic_set_error(vlapic, APIC_ESR_SEND_ILLEGAL_VECTOR,
1099 VLAPIC_CTR1(vlapic, "Ignoring invalid IPI %d", vec);
1103 CPU_FOREACH_ISSET(i, &dmask) {
1104 lapic_intr_edge(vlapic->vm, i, vec);
1105 vmm_stat_array_incr(vlapic->vcpu, IPIS_SENT, i, 1);
1107 "vlapic sending ipi %d to vcpuid %d", vec, i);
1111 case APIC_DELMODE_NMI:
1112 CPU_FOREACH_ISSET(i, &dmask) {
1113 vm_inject_nmi(vlapic->vm, i);
1115 "vlapic sending ipi nmi to vcpuid %d", i);
1119 case APIC_DELMODE_INIT:
1120 if (!vlapic->ipi_exit) {
1124 i = vm_apicid2vcpuid(vlapic->vm, dest);
1125 if (i >= vm_get_maxcpus(vlapic->vm) ||
1126 i == vlapic->vcpuid)
1130 * Userland which doesn't support the IPI exit
1131 * requires that the boot state is set to SIPI
1134 vlapic2 = vm_lapic(vm_vcpu(vlapic->vm, i));
1135 vlapic2->boot_state = BS_SIPI;
1139 CPU_COPY(&dmask, &ipimask);
1141 case APIC_DELMODE_STARTUP:
1142 if (!vlapic->ipi_exit) {
1147 * Old bhyve versions don't support the IPI
1148 * exit. Translate it into the old style.
1150 i = vm_apicid2vcpuid(vlapic->vm, dest);
1151 if (i >= vm_get_maxcpus(vlapic->vm) ||
1152 i == vlapic->vcpuid)
1156 * Ignore SIPIs in any state other than wait-for-SIPI
1158 vlapic2 = vm_lapic(vm_vcpu(vlapic->vm, i));
1159 if (vlapic2->boot_state != BS_SIPI)
1161 vlapic2->boot_state = BS_RUNNING;
1163 vmexit = vm_exitinfo(vlapic->vcpu);
1164 vmexit->exitcode = VM_EXITCODE_SPINUP_AP;
1165 vmexit->u.spinup_ap.vcpu = i;
1166 vmexit->u.spinup_ap.rip = vec << PAGE_SHIFT;
1172 CPU_FOREACH_ISSET(i, &dmask) {
1173 vlapic2 = vm_lapic(vm_vcpu(vlapic->vm, i));
1176 * Ignore SIPIs in any state other than wait-for-SIPI
1178 if (vlapic2->boot_state != BS_SIPI)
1180 vlapic2->boot_state = BS_RUNNING;
1181 CPU_SET(i, &ipimask);
1189 if (!CPU_EMPTY(&ipimask)) {
1190 vmexit = vm_exitinfo(vlapic->vcpu);
1191 vmexit->exitcode = VM_EXITCODE_IPI;
1192 vmexit->u.ipi.mode = mode;
1193 vmexit->u.ipi.vector = vec;
1194 vmexit->u.ipi.dmask = dmask;
1203 vlapic_handle_init(struct vcpu *vcpu, void *arg)
1205 struct vlapic *vlapic = vm_lapic(vcpu);
1207 vlapic_reset(vlapic);
1209 /* vlapic_reset modifies the boot state. */
1210 vlapic->boot_state = BS_SIPI;
1214 vm_handle_ipi(struct vcpu *vcpu, struct vm_exit *vme, bool *retu)
1217 switch (vme->u.ipi.mode) {
1218 case APIC_DELMODE_INIT:
1219 vm_smp_rendezvous(vcpu, vme->u.ipi.dmask, vlapic_handle_init,
1222 case APIC_DELMODE_STARTUP:
1232 vlapic_self_ipi_handler(struct vlapic *vlapic, uint64_t val)
1236 KASSERT(x2apic(vlapic), ("SELF_IPI does not exist in xAPIC mode"));
1239 lapic_intr_edge(vlapic->vm, vlapic->vcpuid, vec);
1240 vmm_stat_array_incr(vlapic->vcpu, IPIS_SENT, vlapic->vcpuid, 1);
1241 VLAPIC_CTR1(vlapic, "vlapic self-ipi %d", vec);
1245 vlapic_pending_intr(struct vlapic *vlapic, int *vecptr)
1247 struct LAPIC *lapic = vlapic->apic_page;
1248 int idx, i, bitpos, vector;
1249 uint32_t *irrptr, val;
1251 vlapic_update_ppr(vlapic);
1253 if (vlapic->ops.pending_intr)
1254 return ((*vlapic->ops.pending_intr)(vlapic, vecptr));
1256 irrptr = &lapic->irr0;
1258 for (i = 7; i >= 0; i--) {
1260 val = atomic_load_acq_int(&irrptr[idx]);
1263 vector = i * 32 + (bitpos - 1);
1264 if (PRIO(vector) > PRIO(lapic->ppr)) {
1265 VLAPIC_CTR1(vlapic, "pending intr %d", vector);
1277 vlapic_intr_accepted(struct vlapic *vlapic, int vector)
1279 struct LAPIC *lapic = vlapic->apic_page;
1280 uint32_t *irrptr, *isrptr;
1283 if (vlapic->ops.intr_accepted)
1284 return ((*vlapic->ops.intr_accepted)(vlapic, vector));
1287 * clear the ready bit for vector being accepted in irr
1288 * and set the vector as in service in isr.
1290 idx = (vector / 32) * 4;
1292 irrptr = &lapic->irr0;
1293 atomic_clear_int(&irrptr[idx], 1 << (vector % 32));
1294 VLAPIC_CTR_IRR(vlapic, "vlapic_intr_accepted");
1296 isrptr = &lapic->isr0;
1297 isrptr[idx] |= 1 << (vector % 32);
1298 VLAPIC_CTR_ISR(vlapic, "vlapic_intr_accepted");
1303 vlapic->isrvec_stk_top++;
1305 stk_top = vlapic->isrvec_stk_top;
1306 if (stk_top >= ISRVEC_STK_SIZE)
1307 panic("isrvec_stk_top overflow %d", stk_top);
1309 vlapic->isrvec_stk[stk_top] = vector;
1313 vlapic_svr_write_handler(struct vlapic *vlapic)
1315 struct LAPIC *lapic;
1316 uint32_t old, new, changed;
1318 lapic = vlapic->apic_page;
1321 old = vlapic->svr_last;
1322 vlapic->svr_last = new;
1324 changed = old ^ new;
1325 if ((changed & APIC_SVR_ENABLE) != 0) {
1326 if ((new & APIC_SVR_ENABLE) == 0) {
1328 * The apic is now disabled so stop the apic timer
1329 * and mask all the LVT entries.
1331 VLAPIC_CTR0(vlapic, "vlapic is software-disabled");
1332 VLAPIC_TIMER_LOCK(vlapic);
1333 callout_stop(&vlapic->callout);
1334 VLAPIC_TIMER_UNLOCK(vlapic);
1335 vlapic_mask_lvts(vlapic);
1338 * The apic is now enabled so restart the apic timer
1339 * if it is configured in periodic mode.
1341 VLAPIC_CTR0(vlapic, "vlapic is software-enabled");
1342 if (vlapic_periodic_timer(vlapic))
1343 vlapic_icrtmr_write_handler(vlapic);
1349 vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset,
1350 uint64_t *data, bool *retu)
1352 struct LAPIC *lapic = vlapic->apic_page;
1356 /* Ignore MMIO accesses in x2APIC mode */
1357 if (x2apic(vlapic) && mmio_access) {
1358 VLAPIC_CTR1(vlapic, "MMIO read from offset %#lx in x2APIC mode",
1364 if (!x2apic(vlapic) && !mmio_access) {
1366 * XXX Generate GP fault for MSR accesses in xAPIC mode
1368 VLAPIC_CTR1(vlapic, "x2APIC MSR read from offset %#lx in "
1369 "xAPIC mode", offset);
1374 if (offset > sizeof(*lapic)) {
1382 case APIC_OFFSET_ID:
1385 case APIC_OFFSET_VER:
1386 *data = lapic->version;
1388 case APIC_OFFSET_TPR:
1389 *data = vlapic_get_tpr(vlapic);
1391 case APIC_OFFSET_APR:
1394 case APIC_OFFSET_PPR:
1397 case APIC_OFFSET_EOI:
1400 case APIC_OFFSET_LDR:
1403 case APIC_OFFSET_DFR:
1406 case APIC_OFFSET_SVR:
1409 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
1410 i = (offset - APIC_OFFSET_ISR0) >> 2;
1414 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
1415 i = (offset - APIC_OFFSET_TMR0) >> 2;
1419 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
1420 i = (offset - APIC_OFFSET_IRR0) >> 2;
1422 *data = atomic_load_acq_int(reg + i);
1424 case APIC_OFFSET_ESR:
1427 case APIC_OFFSET_ICR_LOW:
1428 *data = lapic->icr_lo;
1430 *data |= (uint64_t)lapic->icr_hi << 32;
1432 case APIC_OFFSET_ICR_HI:
1433 *data = lapic->icr_hi;
1435 case APIC_OFFSET_CMCI_LVT:
1436 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1437 *data = vlapic_get_lvt(vlapic, offset);
1439 reg = vlapic_get_lvtptr(vlapic, offset);
1440 KASSERT(*data == *reg, ("inconsistent lvt value at "
1441 "offset %#lx: %#lx/%#x", offset, *data, *reg));
1444 case APIC_OFFSET_TIMER_ICR:
1445 *data = lapic->icr_timer;
1447 case APIC_OFFSET_TIMER_CCR:
1448 *data = vlapic_get_ccr(vlapic);
1450 case APIC_OFFSET_TIMER_DCR:
1451 *data = lapic->dcr_timer;
1453 case APIC_OFFSET_SELF_IPI:
1455 * XXX generate a GP fault if vlapic is in x2apic mode
1459 case APIC_OFFSET_RRR:
1465 VLAPIC_CTR2(vlapic, "vlapic read offset %#x, data %#lx", offset, *data);
1470 vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset,
1471 uint64_t data, bool *retu)
1473 struct LAPIC *lapic = vlapic->apic_page;
1477 KASSERT((offset & 0xf) == 0 && offset < PAGE_SIZE,
1478 ("vlapic_write: invalid offset %#lx", offset));
1480 VLAPIC_CTR2(vlapic, "vlapic write offset %#lx, data %#lx",
1483 if (offset > sizeof(*lapic))
1486 /* Ignore MMIO accesses in x2APIC mode */
1487 if (x2apic(vlapic) && mmio_access) {
1488 VLAPIC_CTR2(vlapic, "MMIO write of %#lx to offset %#lx "
1489 "in x2APIC mode", data, offset);
1494 * XXX Generate GP fault for MSR accesses in xAPIC mode
1496 if (!x2apic(vlapic) && !mmio_access) {
1497 VLAPIC_CTR2(vlapic, "x2APIC MSR write of %#lx to offset %#lx "
1498 "in xAPIC mode", data, offset);
1505 case APIC_OFFSET_ID:
1507 vlapic_id_write_handler(vlapic);
1509 case APIC_OFFSET_TPR:
1510 vlapic_set_tpr(vlapic, data & 0xff);
1512 case APIC_OFFSET_EOI:
1513 vlapic_process_eoi(vlapic);
1515 case APIC_OFFSET_LDR:
1517 vlapic_ldr_write_handler(vlapic);
1519 case APIC_OFFSET_DFR:
1521 vlapic_dfr_write_handler(vlapic);
1523 case APIC_OFFSET_SVR:
1525 vlapic_svr_write_handler(vlapic);
1527 case APIC_OFFSET_ICR_LOW:
1528 lapic->icr_lo = data;
1530 lapic->icr_hi = data >> 32;
1531 retval = vlapic_icrlo_write_handler(vlapic, retu);
1533 case APIC_OFFSET_ICR_HI:
1534 lapic->icr_hi = data;
1536 case APIC_OFFSET_CMCI_LVT:
1537 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT:
1538 regptr = vlapic_get_lvtptr(vlapic, offset);
1540 vlapic_lvt_write_handler(vlapic, offset);
1542 case APIC_OFFSET_TIMER_ICR:
1543 lapic->icr_timer = data;
1544 vlapic_icrtmr_write_handler(vlapic);
1547 case APIC_OFFSET_TIMER_DCR:
1548 lapic->dcr_timer = data;
1549 vlapic_dcr_write_handler(vlapic);
1552 case APIC_OFFSET_ESR:
1553 vlapic_esr_write_handler(vlapic);
1556 case APIC_OFFSET_SELF_IPI:
1558 vlapic_self_ipi_handler(vlapic, data);
1561 case APIC_OFFSET_VER:
1562 case APIC_OFFSET_APR:
1563 case APIC_OFFSET_PPR:
1564 case APIC_OFFSET_RRR:
1565 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7:
1566 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7:
1567 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7:
1568 case APIC_OFFSET_TIMER_CCR:
1578 vlapic_reset(struct vlapic *vlapic)
1580 struct LAPIC *lapic;
1582 lapic = vlapic->apic_page;
1583 bzero(lapic, sizeof(struct LAPIC));
1585 lapic->id = vlapic_get_id(vlapic);
1586 lapic->version = VLAPIC_VERSION;
1587 lapic->version |= (VLAPIC_MAXLVT_INDEX << MAXLVTSHIFT);
1588 lapic->dfr = 0xffffffff;
1589 lapic->svr = APIC_SVR_VECTOR;
1590 vlapic_mask_lvts(vlapic);
1591 vlapic_reset_tmr(vlapic);
1593 lapic->dcr_timer = 0;
1594 vlapic_dcr_write_handler(vlapic);
1596 if (vlapic->vcpuid == 0)
1597 vlapic->boot_state = BS_RUNNING; /* BSP */
1599 vlapic->boot_state = BS_INIT; /* AP */
1601 vlapic->svr_last = lapic->svr;
1605 vlapic_init(struct vlapic *vlapic)
1607 KASSERT(vlapic->vm != NULL, ("vlapic_init: vm is not initialized"));
1608 KASSERT(vlapic->vcpuid >= 0 &&
1609 vlapic->vcpuid < vm_get_maxcpus(vlapic->vm),
1610 ("vlapic_init: vcpuid is not initialized"));
1611 KASSERT(vlapic->apic_page != NULL, ("vlapic_init: apic_page is not "
1615 * If the vlapic is configured in x2apic mode then it will be
1616 * accessed in the critical section via the MSR emulation code.
1618 * Therefore the timer mutex must be a spinlock because blockable
1619 * mutexes cannot be acquired in a critical section.
1621 mtx_init(&vlapic->timer_mtx, "vlapic timer mtx", NULL, MTX_SPIN);
1622 callout_init(&vlapic->callout, 1);
1624 vlapic->msr_apicbase = DEFAULT_APIC_BASE | APICBASE_ENABLED;
1626 if (vlapic->vcpuid == 0)
1627 vlapic->msr_apicbase |= APICBASE_BSP;
1629 vlapic->ipi_exit = false;
1631 vlapic_reset(vlapic);
1635 vlapic_cleanup(struct vlapic *vlapic)
1638 callout_drain(&vlapic->callout);
1642 vlapic_get_apicbase(struct vlapic *vlapic)
1645 return (vlapic->msr_apicbase);
1649 vlapic_set_apicbase(struct vlapic *vlapic, uint64_t new)
1652 if (vlapic->msr_apicbase != new) {
1653 VLAPIC_CTR2(vlapic, "Changing APIC_BASE MSR from %#lx to %#lx "
1654 "not supported", vlapic->msr_apicbase, new);
1662 vlapic_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state)
1664 struct vlapic *vlapic;
1665 struct LAPIC *lapic;
1667 vlapic = vm_lapic(vcpu);
1669 if (state == X2APIC_DISABLED)
1670 vlapic->msr_apicbase &= ~APICBASE_X2APIC;
1672 vlapic->msr_apicbase |= APICBASE_X2APIC;
1675 * Reset the local APIC registers whose values are mode-dependent.
1677 * XXX this works because the APIC mode can be changed only at vcpu
1678 * initialization time.
1680 lapic = vlapic->apic_page;
1681 lapic->id = vlapic_get_id(vlapic);
1682 if (x2apic(vlapic)) {
1683 lapic->ldr = x2apic_ldr(vlapic);
1687 lapic->dfr = 0xffffffff;
1690 if (state == X2APIC_ENABLED) {
1691 if (vlapic->ops.enable_x2apic_mode)
1692 (*vlapic->ops.enable_x2apic_mode)(vlapic);
1697 vlapic_deliver_intr(struct vm *vm, bool level, uint32_t dest, bool phys,
1698 int delmode, int vec)
1704 if (delmode != IOART_DELFIXED &&
1705 delmode != IOART_DELLOPRI &&
1706 delmode != IOART_DELEXINT) {
1707 VM_CTR1(vm, "vlapic intr invalid delmode %#x", delmode);
1710 lowprio = (delmode == IOART_DELLOPRI);
1713 * We don't provide any virtual interrupt redirection hardware so
1714 * all interrupts originating from the ioapic or MSI specify the
1715 * 'dest' in the legacy xAPIC format.
1717 vlapic_calcdest(vm, &dmask, dest, phys, lowprio, false);
1719 CPU_FOREACH_ISSET(vcpuid, &dmask) {
1720 if (delmode == IOART_DELEXINT) {
1721 vm_inject_extint(vm, vcpuid);
1723 lapic_set_intr(vm, vcpuid, vec, level);
1729 vlapic_post_intr(struct vlapic *vlapic, int hostcpu, int ipinum)
1732 * Post an interrupt to the vcpu currently running on 'hostcpu'.
1734 * This is done by leveraging features like Posted Interrupts (Intel)
1735 * Doorbell MSR (AMD AVIC) that avoid a VM exit.
1737 * If neither of these features are available then fallback to
1738 * sending an IPI to 'hostcpu'.
1740 if (vlapic->ops.post_intr)
1741 (*vlapic->ops.post_intr)(vlapic, hostcpu);
1743 ipi_cpu(hostcpu, ipinum);
1747 vlapic_enabled(struct vlapic *vlapic)
1749 struct LAPIC *lapic = vlapic->apic_page;
1751 if ((vlapic->msr_apicbase & APICBASE_ENABLED) != 0 &&
1752 (lapic->svr & APIC_SVR_ENABLE) != 0)
1759 vlapic_set_tmr(struct vlapic *vlapic, int vector, bool level)
1761 struct LAPIC *lapic;
1762 uint32_t *tmrptr, mask;
1765 lapic = vlapic->apic_page;
1766 tmrptr = &lapic->tmr0;
1767 idx = (vector / 32) * 4;
1768 mask = 1 << (vector % 32);
1770 tmrptr[idx] |= mask;
1772 tmrptr[idx] &= ~mask;
1774 if (vlapic->ops.set_tmr != NULL)
1775 (*vlapic->ops.set_tmr)(vlapic, vector, level);
1779 vlapic_reset_tmr(struct vlapic *vlapic)
1783 VLAPIC_CTR0(vlapic, "vlapic resetting all vectors to edge-triggered");
1785 for (vector = 0; vector <= 255; vector++)
1786 vlapic_set_tmr(vlapic, vector, false);
1790 vlapic_set_tmr_level(struct vlapic *vlapic, uint32_t dest, bool phys,
1791 int delmode, int vector)
1796 KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector));
1799 * A level trigger is valid only for fixed and lowprio delivery modes.
1801 if (delmode != APIC_DELMODE_FIXED && delmode != APIC_DELMODE_LOWPRIO) {
1802 VLAPIC_CTR1(vlapic, "Ignoring level trigger-mode for "
1803 "delivery-mode %d", delmode);
1807 lowprio = (delmode == APIC_DELMODE_LOWPRIO);
1808 vlapic_calcdest(vlapic->vm, &dmask, dest, phys, lowprio, false);
1810 if (!CPU_ISSET(vlapic->vcpuid, &dmask))
1813 VLAPIC_CTR1(vlapic, "vector %d set to level-triggered", vector);
1814 vlapic_set_tmr(vlapic, vector, true);
1817 #ifdef BHYVE_SNAPSHOT
1819 vlapic_reset_callout(struct vlapic *vlapic, uint32_t ccr)
1821 /* The implementation is similar to the one in the
1822 * `vlapic_icrtmr_write_handler` function
1827 VLAPIC_TIMER_LOCK(vlapic);
1829 bt = vlapic->timer_freq_bt;
1830 bintime_mul(&bt, ccr);
1833 binuptime(&vlapic->timer_fire_bt);
1834 bintime_add(&vlapic->timer_fire_bt, &bt);
1837 vlapic_callout_reset(vlapic, sbt);
1839 /* even if the CCR was 0, periodic timers should be reset */
1840 if (vlapic_periodic_timer(vlapic)) {
1841 binuptime(&vlapic->timer_fire_bt);
1842 bintime_add(&vlapic->timer_fire_bt,
1843 &vlapic->timer_period_bt);
1844 sbt = bttosbt(vlapic->timer_period_bt);
1846 callout_stop(&vlapic->callout);
1847 vlapic_callout_reset(vlapic, sbt);
1851 VLAPIC_TIMER_UNLOCK(vlapic);
1855 vlapic_snapshot(struct vm *vm, struct vm_snapshot_meta *meta)
1858 struct vlapic *vlapic;
1859 struct LAPIC *lapic;
1861 uint16_t i, maxcpus;
1863 KASSERT(vm != NULL, ("%s: arg was NULL", __func__));
1867 maxcpus = vm_get_maxcpus(vm);
1868 for (i = 0; i < maxcpus; i++) {
1869 vlapic = vm_lapic(vm_vcpu(vm, i));
1871 /* snapshot the page first; timer period depends on icr_timer */
1872 lapic = vlapic->apic_page;
1873 SNAPSHOT_BUF_OR_LEAVE(lapic, PAGE_SIZE, meta, ret, done);
1875 SNAPSHOT_VAR_OR_LEAVE(vlapic->esr_pending, meta, ret, done);
1877 SNAPSHOT_VAR_OR_LEAVE(vlapic->timer_freq_bt.sec,
1879 SNAPSHOT_VAR_OR_LEAVE(vlapic->timer_freq_bt.frac,
1883 * Timer period is equal to 'icr_timer' ticks at a frequency of
1886 if (meta->op == VM_SNAPSHOT_RESTORE) {
1887 vlapic->timer_period_bt = vlapic->timer_freq_bt;
1888 bintime_mul(&vlapic->timer_period_bt, lapic->icr_timer);
1891 SNAPSHOT_BUF_OR_LEAVE(vlapic->isrvec_stk,
1892 sizeof(vlapic->isrvec_stk),
1894 SNAPSHOT_VAR_OR_LEAVE(vlapic->isrvec_stk_top, meta, ret, done);
1895 SNAPSHOT_VAR_OR_LEAVE(vlapic->boot_state, meta, ret, done);
1897 SNAPSHOT_BUF_OR_LEAVE(vlapic->lvt_last,
1898 sizeof(vlapic->lvt_last),
1901 if (meta->op == VM_SNAPSHOT_SAVE)
1902 ccr = vlapic_get_ccr(vlapic);
1904 SNAPSHOT_VAR_OR_LEAVE(ccr, meta, ret, done);
1906 if (meta->op == VM_SNAPSHOT_RESTORE &&
1907 vlapic_enabled(vlapic) && lapic->icr_timer != 0) {
1908 /* Reset the value of the 'timer_fire_bt' and the vlapic
1909 * callout based on the value of the current count
1910 * register saved when the VM snapshot was created.
1911 * If initial count register is 0, timer is not used.
1912 * Look at "10.5.4 APIC Timer" in Software Developer Manual.
1914 vlapic_reset_callout(vlapic, ccr);