1 // SPDX-License-Identifier: GPL-2.0-only
4 * Local APIC virtualization
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2007 Novell
8 * Copyright (C) 2007 Intel
9 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
12 * Dor Laor <dor.laor@qumranet.com>
13 * Gregory Haskins <ghaskins@novell.com>
14 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
16 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
19 #include <linux/kvm_host.h>
20 #include <linux/kvm.h>
22 #include <linux/highmem.h>
23 #include <linux/smp.h>
24 #include <linux/hrtimer.h>
26 #include <linux/export.h>
27 #include <linux/math64.h>
28 #include <linux/slab.h>
29 #include <asm/processor.h>
32 #include <asm/current.h>
33 #include <asm/apicdef.h>
34 #include <asm/delay.h>
35 #include <linux/atomic.h>
36 #include <linux/jump_label.h>
37 #include "kvm_cache_regs.h"
46 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48 #define mod_64(x, y) ((x) % (y))
56 /* 14 is the version for Xeon and Pentium 8.4.8*/
57 #define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
58 #define LAPIC_MMIO_LENGTH (1 << 12)
59 /* followed define is not in apicdef.h */
60 #define MAX_APIC_VECTOR 256
61 #define APIC_VECTORS_PER_REG 32
63 static bool lapic_timer_advance_dynamic __read_mostly;
64 #define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
65 #define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
66 #define LAPIC_TIMER_ADVANCE_NS_INIT 1000
67 #define LAPIC_TIMER_ADVANCE_NS_MAX 5000
68 /* step-by-step approximation to mitigate fluctuation */
69 #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
71 static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val)
73 *((u32 *) (regs + reg_off)) = val;
76 static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
78 __kvm_lapic_set_reg(apic->regs, reg_off, val);
81 static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg)
83 BUILD_BUG_ON(reg != APIC_ICR);
84 return *((u64 *) (regs + reg));
87 static __always_inline u64 kvm_lapic_get_reg64(struct kvm_lapic *apic, int reg)
89 return __kvm_lapic_get_reg64(apic->regs, reg);
92 static __always_inline void __kvm_lapic_set_reg64(char *regs, int reg, u64 val)
94 BUILD_BUG_ON(reg != APIC_ICR);
95 *((u64 *) (regs + reg)) = val;
98 static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,
101 __kvm_lapic_set_reg64(apic->regs, reg, val);
104 static inline int apic_test_vector(int vec, void *bitmap)
106 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
109 bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
111 struct kvm_lapic *apic = vcpu->arch.apic;
113 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
114 apic_test_vector(vector, apic->regs + APIC_IRR);
117 static inline int __apic_test_and_set_vector(int vec, void *bitmap)
119 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
122 static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
124 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
127 __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_hw_disabled, HZ);
128 __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_sw_disabled, HZ);
130 static inline int apic_enabled(struct kvm_lapic *apic)
132 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
136 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
139 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
140 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
142 static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
144 return apic->vcpu->vcpu_id;
147 static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
149 return pi_inject_timer && kvm_vcpu_apicv_active(vcpu) &&
150 (kvm_mwait_in_guest(vcpu->kvm) || kvm_hlt_in_guest(vcpu->kvm));
153 bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu)
155 return kvm_x86_ops.set_hv_timer
156 && !(kvm_mwait_in_guest(vcpu->kvm) ||
157 kvm_can_post_timer_interrupt(vcpu));
159 EXPORT_SYMBOL_GPL(kvm_can_use_hv_timer);
161 static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
163 return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
166 static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
167 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
169 case KVM_APIC_MODE_X2APIC: {
170 u32 offset = (dest_id >> 16) * 16;
171 u32 max_apic_id = map->max_apic_id;
173 if (offset <= max_apic_id) {
174 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
176 offset = array_index_nospec(offset, map->max_apic_id + 1);
177 *cluster = &map->phys_map[offset];
178 *mask = dest_id & (0xffff >> (16 - cluster_size));
185 case KVM_APIC_MODE_XAPIC_FLAT:
186 *cluster = map->xapic_flat_map;
187 *mask = dest_id & 0xff;
189 case KVM_APIC_MODE_XAPIC_CLUSTER:
190 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
191 *mask = dest_id & 0xf;
199 static void kvm_apic_map_free(struct rcu_head *rcu)
201 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
207 * CLEAN -> DIRTY and UPDATE_IN_PROGRESS -> DIRTY changes happen without a lock.
209 * DIRTY -> UPDATE_IN_PROGRESS and UPDATE_IN_PROGRESS -> CLEAN happen with
210 * apic_map_lock_held.
218 void kvm_recalculate_apic_map(struct kvm *kvm)
220 struct kvm_apic_map *new, *old = NULL;
221 struct kvm_vcpu *vcpu;
223 u32 max_id = 255; /* enough space for any xAPIC ID */
225 /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
226 if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN)
229 WARN_ONCE(!irqchip_in_kernel(kvm),
230 "Dirty APIC map without an in-kernel local APIC");
232 mutex_lock(&kvm->arch.apic_map_lock);
234 * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map
235 * (if clean) or the APIC registers (if dirty).
237 if (atomic_cmpxchg_acquire(&kvm->arch.apic_map_dirty,
238 DIRTY, UPDATE_IN_PROGRESS) == CLEAN) {
239 /* Someone else has updated the map. */
240 mutex_unlock(&kvm->arch.apic_map_lock);
244 kvm_for_each_vcpu(i, vcpu, kvm)
245 if (kvm_apic_present(vcpu))
246 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
248 new = kvzalloc(sizeof(struct kvm_apic_map) +
249 sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
255 new->max_apic_id = max_id;
257 kvm_for_each_vcpu(i, vcpu, kvm) {
258 struct kvm_lapic *apic = vcpu->arch.apic;
259 struct kvm_lapic **cluster;
265 if (!kvm_apic_present(vcpu))
268 xapic_id = kvm_xapic_id(apic);
269 x2apic_id = kvm_x2apic_id(apic);
271 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
272 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
273 x2apic_id <= new->max_apic_id)
274 new->phys_map[x2apic_id] = apic;
276 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
277 * prevent them from masking VCPUs with APIC ID <= 0xff.
279 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
280 new->phys_map[xapic_id] = apic;
282 if (!kvm_apic_sw_enabled(apic))
285 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
287 if (apic_x2apic_mode(apic)) {
288 new->mode |= KVM_APIC_MODE_X2APIC;
290 ldr = GET_APIC_LOGICAL_ID(ldr);
291 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
292 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
294 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
297 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
301 cluster[ffs(mask) - 1] = apic;
304 old = rcu_dereference_protected(kvm->arch.apic_map,
305 lockdep_is_held(&kvm->arch.apic_map_lock));
306 rcu_assign_pointer(kvm->arch.apic_map, new);
308 * Write kvm->arch.apic_map before clearing apic->apic_map_dirty.
309 * If another update has come in, leave it DIRTY.
311 atomic_cmpxchg_release(&kvm->arch.apic_map_dirty,
312 UPDATE_IN_PROGRESS, CLEAN);
313 mutex_unlock(&kvm->arch.apic_map_lock);
316 call_rcu(&old->rcu, kvm_apic_map_free);
318 kvm_make_scan_ioapic_request(kvm);
321 static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
323 bool enabled = val & APIC_SPIV_APIC_ENABLED;
325 kvm_lapic_set_reg(apic, APIC_SPIV, val);
327 if (enabled != apic->sw_enabled) {
328 apic->sw_enabled = enabled;
330 static_branch_slow_dec_deferred(&apic_sw_disabled);
332 static_branch_inc(&apic_sw_disabled.key);
334 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
337 /* Check if there are APF page ready requests pending */
339 kvm_make_request(KVM_REQ_APF_READY, apic->vcpu);
342 static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
344 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
345 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
348 static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
350 kvm_lapic_set_reg(apic, APIC_LDR, id);
351 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
354 static inline void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val)
356 kvm_lapic_set_reg(apic, APIC_DFR, val);
357 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
360 static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
362 return ((id >> 4) << 16) | (1 << (id & 0xf));
365 static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
367 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
369 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
371 kvm_lapic_set_reg(apic, APIC_ID, id);
372 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
373 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
376 static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
378 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
381 static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
383 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
386 static inline int apic_lvtt_period(struct kvm_lapic *apic)
388 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
391 static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
393 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
396 static inline int apic_lvt_nmi_mode(u32 lvt_val)
398 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
401 void kvm_apic_set_version(struct kvm_vcpu *vcpu)
403 struct kvm_lapic *apic = vcpu->arch.apic;
404 u32 v = APIC_VERSION;
406 if (!lapic_in_kernel(vcpu))
410 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
411 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
412 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
413 * version first and level-triggered interrupts never get EOIed in
416 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) &&
417 !ioapic_in_kernel(vcpu->kvm))
418 v |= APIC_LVR_DIRECTED_EOI;
419 kvm_lapic_set_reg(apic, APIC_LVR, v);
422 static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
423 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
424 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
425 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
426 LINT_MASK, LINT_MASK, /* LVT0-1 */
427 LVT_MASK /* LVTERR */
430 static int find_highest_vector(void *bitmap)
435 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
436 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
437 reg = bitmap + REG_POS(vec);
439 return __fls(*reg) + vec;
445 static u8 count_vectors(void *bitmap)
451 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
452 reg = bitmap + REG_POS(vec);
453 count += hweight32(*reg);
459 bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
462 u32 pir_val, irr_val, prev_irr_val;
465 max_updated_irr = -1;
468 for (i = vec = 0; i <= 7; i++, vec += 32) {
469 pir_val = READ_ONCE(pir[i]);
470 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
472 prev_irr_val = irr_val;
473 irr_val |= xchg(&pir[i], 0);
474 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
475 if (prev_irr_val != irr_val) {
477 __fls(irr_val ^ prev_irr_val) + vec;
481 *max_irr = __fls(irr_val) + vec;
484 return ((max_updated_irr != -1) &&
485 (max_updated_irr == *max_irr));
487 EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
489 bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
491 struct kvm_lapic *apic = vcpu->arch.apic;
493 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
495 EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
497 static inline int apic_search_irr(struct kvm_lapic *apic)
499 return find_highest_vector(apic->regs + APIC_IRR);
502 static inline int apic_find_highest_irr(struct kvm_lapic *apic)
507 * Note that irr_pending is just a hint. It will be always
508 * true with virtual interrupt delivery enabled.
510 if (!apic->irr_pending)
513 result = apic_search_irr(apic);
514 ASSERT(result == -1 || result >= 16);
519 static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
521 struct kvm_vcpu *vcpu;
525 if (unlikely(vcpu->arch.apicv_active)) {
526 /* need to update RVI */
527 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
528 static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, apic_find_highest_irr(apic));
530 apic->irr_pending = false;
531 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
532 if (apic_search_irr(apic) != -1)
533 apic->irr_pending = true;
537 void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec)
539 apic_clear_irr(vec, vcpu->arch.apic);
541 EXPORT_SYMBOL_GPL(kvm_apic_clear_irr);
543 static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
545 struct kvm_vcpu *vcpu;
547 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
553 * With APIC virtualization enabled, all caching is disabled
554 * because the processor can modify ISR under the hood. Instead
557 if (unlikely(vcpu->arch.apicv_active))
558 static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, vec);
561 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
563 * ISR (in service register) bit is set when injecting an interrupt.
564 * The highest vector is injected. Thus the latest bit set matches
565 * the highest bit in ISR.
567 apic->highest_isr_cache = vec;
571 static inline int apic_find_highest_isr(struct kvm_lapic *apic)
576 * Note that isr_count is always 1, and highest_isr_cache
577 * is always -1, with APIC virtualization enabled.
579 if (!apic->isr_count)
581 if (likely(apic->highest_isr_cache != -1))
582 return apic->highest_isr_cache;
584 result = find_highest_vector(apic->regs + APIC_ISR);
585 ASSERT(result == -1 || result >= 16);
590 static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
592 struct kvm_vcpu *vcpu;
593 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
599 * We do get here for APIC virtualization enabled if the guest
600 * uses the Hyper-V APIC enlightenment. In this case we may need
601 * to trigger a new interrupt delivery by writing the SVI field;
602 * on the other hand isr_count and highest_isr_cache are unused
603 * and must be left alone.
605 if (unlikely(vcpu->arch.apicv_active))
606 static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, apic_find_highest_isr(apic));
609 BUG_ON(apic->isr_count < 0);
610 apic->highest_isr_cache = -1;
614 int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
616 /* This may race with setting of irr in __apic_accept_irq() and
617 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
618 * will cause vmexit immediately and the value will be recalculated
619 * on the next vmentry.
621 return apic_find_highest_irr(vcpu->arch.apic);
623 EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
625 static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
626 int vector, int level, int trig_mode,
627 struct dest_map *dest_map);
629 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
630 struct dest_map *dest_map)
632 struct kvm_lapic *apic = vcpu->arch.apic;
634 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
635 irq->level, irq->trig_mode, dest_map);
638 static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
639 struct kvm_lapic_irq *irq, u32 min)
642 struct kvm_vcpu *vcpu;
644 if (min > map->max_apic_id)
647 for_each_set_bit(i, ipi_bitmap,
648 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
649 if (map->phys_map[min + i]) {
650 vcpu = map->phys_map[min + i]->vcpu;
651 count += kvm_apic_set_irq(vcpu, irq, NULL);
658 int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
659 unsigned long ipi_bitmap_high, u32 min,
660 unsigned long icr, int op_64_bit)
662 struct kvm_apic_map *map;
663 struct kvm_lapic_irq irq = {0};
664 int cluster_size = op_64_bit ? 64 : 32;
667 if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK))
670 irq.vector = icr & APIC_VECTOR_MASK;
671 irq.delivery_mode = icr & APIC_MODE_MASK;
672 irq.level = (icr & APIC_INT_ASSERT) != 0;
673 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
676 map = rcu_dereference(kvm->arch.apic_map);
680 count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min);
682 count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min);
689 static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
692 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
696 static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
699 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
703 static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
705 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
708 static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
710 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0)
713 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
716 static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu *vcpu)
720 if (pv_eoi_get_user(vcpu, &val) < 0)
723 val &= KVM_PV_EOI_ENABLED;
725 if (val && pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
729 * Clear pending bit in any case: it will be set again on vmentry.
730 * While this might not be ideal from performance point of view,
731 * this makes sure pv eoi is only enabled when we know it's safe.
733 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
738 static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
741 if (kvm_x86_ops.sync_pir_to_irr)
742 highest_irr = static_call(kvm_x86_sync_pir_to_irr)(apic->vcpu);
744 highest_irr = apic_find_highest_irr(apic);
745 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
750 static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
752 u32 tpr, isrv, ppr, old_ppr;
755 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
756 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
757 isr = apic_find_highest_isr(apic);
758 isrv = (isr != -1) ? isr : 0;
760 if ((tpr & 0xf0) >= (isrv & 0xf0))
767 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
769 return ppr < old_ppr;
772 static void apic_update_ppr(struct kvm_lapic *apic)
776 if (__apic_update_ppr(apic, &ppr) &&
777 apic_has_interrupt_for_ppr(apic, ppr) != -1)
778 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
781 void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
783 apic_update_ppr(vcpu->arch.apic);
785 EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
787 static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
789 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
790 apic_update_ppr(apic);
793 static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
795 return mda == (apic_x2apic_mode(apic) ?
796 X2APIC_BROADCAST : APIC_BROADCAST);
799 static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
801 if (kvm_apic_broadcast(apic, mda))
804 if (apic_x2apic_mode(apic))
805 return mda == kvm_x2apic_id(apic);
808 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
809 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
810 * this allows unique addressing of VCPUs with APIC ID over 0xff.
811 * The 0xff condition is needed because writeable xAPIC ID.
813 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
816 return mda == kvm_xapic_id(apic);
819 static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
823 if (kvm_apic_broadcast(apic, mda))
826 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
828 if (apic_x2apic_mode(apic))
829 return ((logical_id >> 16) == (mda >> 16))
830 && (logical_id & mda & 0xffff) != 0;
832 logical_id = GET_APIC_LOGICAL_ID(logical_id);
834 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
836 return (logical_id & mda) != 0;
837 case APIC_DFR_CLUSTER:
838 return ((logical_id >> 4) == (mda >> 4))
839 && (logical_id & mda & 0xf) != 0;
845 /* The KVM local APIC implementation has two quirks:
847 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
848 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
849 * KVM doesn't do that aliasing.
851 * - in-kernel IOAPIC messages have to be delivered directly to
852 * x2APIC, because the kernel does not support interrupt remapping.
853 * In order to support broadcast without interrupt remapping, x2APIC
854 * rewrites the destination of non-IPI messages from APIC_BROADCAST
855 * to X2APIC_BROADCAST.
857 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
858 * important when userspace wants to use x2APIC-format MSIs, because
859 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
861 static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
862 struct kvm_lapic *source, struct kvm_lapic *target)
864 bool ipi = source != NULL;
866 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
867 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
868 return X2APIC_BROADCAST;
873 bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
874 int shorthand, unsigned int dest, int dest_mode)
876 struct kvm_lapic *target = vcpu->arch.apic;
877 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
881 case APIC_DEST_NOSHORT:
882 if (dest_mode == APIC_DEST_PHYSICAL)
883 return kvm_apic_match_physical_addr(target, mda);
885 return kvm_apic_match_logical_addr(target, mda);
887 return target == source;
888 case APIC_DEST_ALLINC:
890 case APIC_DEST_ALLBUT:
891 return target != source;
896 EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
898 int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
899 const unsigned long *bitmap, u32 bitmap_size)
904 mod = vector % dest_vcpus;
906 for (i = 0; i <= mod; i++) {
907 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
908 BUG_ON(idx == bitmap_size);
914 static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
916 if (!kvm->arch.disabled_lapic_found) {
917 kvm->arch.disabled_lapic_found = true;
919 "Disabled LAPIC found during irq injection\n");
923 static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
924 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
926 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
927 if ((irq->dest_id == APIC_BROADCAST &&
928 map->mode != KVM_APIC_MODE_X2APIC))
930 if (irq->dest_id == X2APIC_BROADCAST)
933 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
934 if (irq->dest_id == (x2apic_ipi ?
935 X2APIC_BROADCAST : APIC_BROADCAST))
942 /* Return true if the interrupt can be handled by using *bitmap as index mask
943 * for valid destinations in *dst array.
944 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
945 * Note: we may have zero kvm_lapic destinations when we return true, which
946 * means that the interrupt should be dropped. In this case, *bitmap would be
947 * zero and *dst undefined.
949 static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
950 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
951 struct kvm_apic_map *map, struct kvm_lapic ***dst,
952 unsigned long *bitmap)
956 if (irq->shorthand == APIC_DEST_SELF && src) {
960 } else if (irq->shorthand)
963 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
966 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
967 if (irq->dest_id > map->max_apic_id) {
970 u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
971 *dst = &map->phys_map[dest_id];
978 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
982 if (!kvm_lowest_prio_delivery(irq))
985 if (!kvm_vector_hashing_enabled()) {
987 for_each_set_bit(i, bitmap, 16) {
992 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
993 (*dst)[lowest]->vcpu) < 0)
1000 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
1003 if (!(*dst)[lowest]) {
1004 kvm_apic_disabled_lapic_found(kvm);
1010 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
1015 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
1016 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
1018 struct kvm_apic_map *map;
1019 unsigned long bitmap;
1020 struct kvm_lapic **dst = NULL;
1026 if (irq->shorthand == APIC_DEST_SELF) {
1027 if (KVM_BUG_ON(!src, kvm)) {
1031 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
1036 map = rcu_dereference(kvm->arch.apic_map);
1038 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
1041 for_each_set_bit(i, &bitmap, 16) {
1044 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
1053 * This routine tries to handle interrupts in posted mode, here is how
1054 * it deals with different cases:
1055 * - For single-destination interrupts, handle it in posted mode
1056 * - Else if vector hashing is enabled and it is a lowest-priority
1057 * interrupt, handle it in posted mode and use the following mechanism
1058 * to find the destination vCPU.
1059 * 1. For lowest-priority interrupts, store all the possible
1060 * destination vCPUs in an array.
1061 * 2. Use "guest vector % max number of destination vCPUs" to find
1062 * the right destination vCPU in the array for the lowest-priority
1064 * - Otherwise, use remapped mode to inject the interrupt.
1066 bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
1067 struct kvm_vcpu **dest_vcpu)
1069 struct kvm_apic_map *map;
1070 unsigned long bitmap;
1071 struct kvm_lapic **dst = NULL;
1078 map = rcu_dereference(kvm->arch.apic_map);
1080 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1081 hweight16(bitmap) == 1) {
1082 unsigned long i = find_first_bit(&bitmap, 16);
1085 *dest_vcpu = dst[i]->vcpu;
1095 * Add a pending IRQ into lapic.
1096 * Return 1 if successfully added and 0 if discarded.
1098 static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
1099 int vector, int level, int trig_mode,
1100 struct dest_map *dest_map)
1103 struct kvm_vcpu *vcpu = apic->vcpu;
1105 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1107 switch (delivery_mode) {
1108 case APIC_DM_LOWEST:
1109 vcpu->arch.apic_arb_prio++;
1112 if (unlikely(trig_mode && !level))
1115 /* FIXME add logic for vcpu on reset */
1116 if (unlikely(!apic_enabled(apic)))
1122 __set_bit(vcpu->vcpu_id, dest_map->map);
1123 dest_map->vectors[vcpu->vcpu_id] = vector;
1126 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1128 kvm_lapic_set_vector(vector,
1129 apic->regs + APIC_TMR);
1131 kvm_lapic_clear_vector(vector,
1132 apic->regs + APIC_TMR);
1135 static_call(kvm_x86_deliver_interrupt)(apic, delivery_mode,
1141 vcpu->arch.pv.pv_unhalted = 1;
1142 kvm_make_request(KVM_REQ_EVENT, vcpu);
1143 kvm_vcpu_kick(vcpu);
1148 kvm_make_request(KVM_REQ_SMI, vcpu);
1149 kvm_vcpu_kick(vcpu);
1154 kvm_inject_nmi(vcpu);
1155 kvm_vcpu_kick(vcpu);
1159 if (!trig_mode || level) {
1161 /* assumes that there are only KVM_APIC_INIT/SIPI */
1162 apic->pending_events = (1UL << KVM_APIC_INIT);
1163 kvm_make_request(KVM_REQ_EVENT, vcpu);
1164 kvm_vcpu_kick(vcpu);
1168 case APIC_DM_STARTUP:
1170 apic->sipi_vector = vector;
1171 /* make sure sipi_vector is visible for the receiver */
1173 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1174 kvm_make_request(KVM_REQ_EVENT, vcpu);
1175 kvm_vcpu_kick(vcpu);
1178 case APIC_DM_EXTINT:
1180 * Should only be called by kvm_apic_local_deliver() with LVT0,
1181 * before NMI watchdog was enabled. Already handled by
1182 * kvm_apic_accept_pic_intr().
1187 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1195 * This routine identifies the destination vcpus mask meant to receive the
1196 * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find
1197 * out the destination vcpus array and set the bitmap or it traverses to
1198 * each available vcpu to identify the same.
1200 void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
1201 unsigned long *vcpu_bitmap)
1203 struct kvm_lapic **dest_vcpu = NULL;
1204 struct kvm_lapic *src = NULL;
1205 struct kvm_apic_map *map;
1206 struct kvm_vcpu *vcpu;
1207 unsigned long bitmap, i;
1212 map = rcu_dereference(kvm->arch.apic_map);
1214 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu,
1217 for_each_set_bit(i, &bitmap, 16) {
1220 vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx;
1221 __set_bit(vcpu_idx, vcpu_bitmap);
1224 kvm_for_each_vcpu(i, vcpu, kvm) {
1225 if (!kvm_apic_present(vcpu))
1227 if (!kvm_apic_match_dest(vcpu, NULL,
1232 __set_bit(i, vcpu_bitmap);
1238 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
1240 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
1243 static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1245 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
1248 static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1252 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1253 if (!kvm_ioapic_handles_vector(apic, vector))
1256 /* Request a KVM exit to inform the userspace IOAPIC. */
1257 if (irqchip_split(apic->vcpu->kvm)) {
1258 apic->vcpu->arch.pending_ioapic_eoi = vector;
1259 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1263 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1264 trigger_mode = IOAPIC_LEVEL_TRIG;
1266 trigger_mode = IOAPIC_EDGE_TRIG;
1268 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
1271 static int apic_set_eoi(struct kvm_lapic *apic)
1273 int vector = apic_find_highest_isr(apic);
1275 trace_kvm_eoi(apic, vector);
1278 * Not every write EOI will has corresponding ISR,
1279 * one example is when Kernel check timer on setup_IO_APIC
1284 apic_clear_isr(vector, apic);
1285 apic_update_ppr(apic);
1287 if (to_hv_vcpu(apic->vcpu) &&
1288 test_bit(vector, to_hv_synic(apic->vcpu)->vec_bitmap))
1289 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1291 kvm_ioapic_send_eoi(apic, vector);
1292 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1297 * this interface assumes a trap-like exit, which has already finished
1298 * desired side effect including vISR and vPPR update.
1300 void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1302 struct kvm_lapic *apic = vcpu->arch.apic;
1304 trace_kvm_eoi(apic, vector);
1306 kvm_ioapic_send_eoi(apic, vector);
1307 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1309 EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1311 void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
1313 struct kvm_lapic_irq irq;
1315 /* KVM has no delay and should always clear the BUSY/PENDING flag. */
1316 WARN_ON_ONCE(icr_low & APIC_ICR_BUSY);
1318 irq.vector = icr_low & APIC_VECTOR_MASK;
1319 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1320 irq.dest_mode = icr_low & APIC_DEST_MASK;
1321 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
1322 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1323 irq.shorthand = icr_low & APIC_SHORT_MASK;
1324 irq.msi_redir_hint = false;
1325 if (apic_x2apic_mode(apic))
1326 irq.dest_id = icr_high;
1328 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
1330 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1332 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
1334 EXPORT_SYMBOL_GPL(kvm_apic_send_ipi);
1336 static u32 apic_get_tmcct(struct kvm_lapic *apic)
1338 ktime_t remaining, now;
1342 ASSERT(apic != NULL);
1344 /* if initial count is 0, current count should also be 0 */
1345 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
1346 apic->lapic_timer.period == 0)
1350 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1351 if (ktime_to_ns(remaining) < 0)
1354 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1355 tmcct = div64_u64(ns,
1356 (APIC_BUS_CYCLE_NS * apic->divide_count));
1361 static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1363 struct kvm_vcpu *vcpu = apic->vcpu;
1364 struct kvm_run *run = vcpu->run;
1366 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
1367 run->tpr_access.rip = kvm_rip_read(vcpu);
1368 run->tpr_access.is_write = write;
1371 static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1373 if (apic->vcpu->arch.tpr_access_reporting)
1374 __report_tpr_access(apic, write);
1377 static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1381 if (offset >= LAPIC_MMIO_LENGTH)
1388 case APIC_TMCCT: /* Timer CCR */
1389 if (apic_lvtt_tscdeadline(apic))
1392 val = apic_get_tmcct(apic);
1395 apic_update_ppr(apic);
1396 val = kvm_lapic_get_reg(apic, offset);
1399 report_tpr_access(apic, false);
1402 val = kvm_lapic_get_reg(apic, offset);
1409 static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1411 return container_of(dev, struct kvm_lapic, dev);
1414 #define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1415 #define APIC_REGS_MASK(first, count) \
1416 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1418 static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
1421 unsigned char alignment = offset & 0xf;
1423 /* this bitmask has a bit cleared for each reserved register */
1424 u64 valid_reg_mask =
1425 APIC_REG_MASK(APIC_ID) |
1426 APIC_REG_MASK(APIC_LVR) |
1427 APIC_REG_MASK(APIC_TASKPRI) |
1428 APIC_REG_MASK(APIC_PROCPRI) |
1429 APIC_REG_MASK(APIC_LDR) |
1430 APIC_REG_MASK(APIC_DFR) |
1431 APIC_REG_MASK(APIC_SPIV) |
1432 APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1433 APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1434 APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1435 APIC_REG_MASK(APIC_ESR) |
1436 APIC_REG_MASK(APIC_ICR) |
1437 APIC_REG_MASK(APIC_LVTT) |
1438 APIC_REG_MASK(APIC_LVTTHMR) |
1439 APIC_REG_MASK(APIC_LVTPC) |
1440 APIC_REG_MASK(APIC_LVT0) |
1441 APIC_REG_MASK(APIC_LVT1) |
1442 APIC_REG_MASK(APIC_LVTERR) |
1443 APIC_REG_MASK(APIC_TMICT) |
1444 APIC_REG_MASK(APIC_TMCCT) |
1445 APIC_REG_MASK(APIC_TDCR);
1448 * ARBPRI and ICR2 are not valid in x2APIC mode. WARN if KVM reads ICR
1449 * in x2APIC mode as it's an 8-byte register in x2APIC and needs to be
1450 * manually handled by the caller.
1452 if (!apic_x2apic_mode(apic))
1453 valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI) |
1454 APIC_REG_MASK(APIC_ICR2);
1456 WARN_ON_ONCE(offset == APIC_ICR);
1458 if (alignment + len > 4)
1461 if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
1464 result = __apic_read(apic, offset & ~0xf);
1466 trace_kvm_apic_read(offset, result);
1472 memcpy(data, (char *)&result + alignment, len);
1475 printk(KERN_ERR "Local APIC read with len = %x, "
1476 "should be 1,2, or 4 instead\n", len);
1482 static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1484 return addr >= apic->base_address &&
1485 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1488 static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
1489 gpa_t address, int len, void *data)
1491 struct kvm_lapic *apic = to_lapic(this);
1492 u32 offset = address - apic->base_address;
1494 if (!apic_mmio_in_range(apic, address))
1497 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1498 if (!kvm_check_has_quirk(vcpu->kvm,
1499 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1502 memset(data, 0xff, len);
1506 kvm_lapic_reg_read(apic, offset, len, data);
1511 static void update_divide_count(struct kvm_lapic *apic)
1513 u32 tmp1, tmp2, tdcr;
1515 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
1517 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
1518 apic->divide_count = 0x1 << (tmp2 & 0x7);
1521 static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1524 * Do not allow the guest to program periodic timers with small
1525 * interval, since the hrtimers are not throttled by the host
1528 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1529 s64 min_period = min_timer_period_us * 1000LL;
1531 if (apic->lapic_timer.period < min_period) {
1532 pr_info_ratelimited(
1533 "kvm: vcpu %i: requested %lld ns "
1534 "lapic timer period limited to %lld ns\n",
1535 apic->vcpu->vcpu_id,
1536 apic->lapic_timer.period, min_period);
1537 apic->lapic_timer.period = min_period;
1542 static void cancel_hv_timer(struct kvm_lapic *apic);
1544 static void cancel_apic_timer(struct kvm_lapic *apic)
1546 hrtimer_cancel(&apic->lapic_timer.timer);
1548 if (apic->lapic_timer.hv_timer_in_use)
1549 cancel_hv_timer(apic);
1551 atomic_set(&apic->lapic_timer.pending, 0);
1554 static void apic_update_lvtt(struct kvm_lapic *apic)
1556 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
1557 apic->lapic_timer.timer_mode_mask;
1559 if (apic->lapic_timer.timer_mode != timer_mode) {
1560 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
1561 APIC_LVT_TIMER_TSCDEADLINE)) {
1562 cancel_apic_timer(apic);
1563 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1564 apic->lapic_timer.period = 0;
1565 apic->lapic_timer.tscdeadline = 0;
1567 apic->lapic_timer.timer_mode = timer_mode;
1568 limit_periodic_timer_frequency(apic);
1573 * On APICv, this test will cause a busy wait
1574 * during a higher-priority task.
1577 static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1579 struct kvm_lapic *apic = vcpu->arch.apic;
1580 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
1582 if (kvm_apic_hw_enabled(apic)) {
1583 int vec = reg & APIC_VECTOR_MASK;
1584 void *bitmap = apic->regs + APIC_ISR;
1586 if (vcpu->arch.apicv_active)
1587 bitmap = apic->regs + APIC_IRR;
1589 if (apic_test_vector(vec, bitmap))
1595 static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1597 u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1600 * If the guest TSC is running at a different ratio than the host, then
1601 * convert the delay to nanoseconds to achieve an accurate delay. Note
1602 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1603 * always for VMX enabled hardware.
1605 if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) {
1606 __delay(min(guest_cycles,
1607 nsec_to_cycles(vcpu, timer_advance_ns)));
1609 u64 delay_ns = guest_cycles * 1000000ULL;
1610 do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1611 ndelay(min_t(u32, delay_ns, timer_advance_ns));
1615 static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
1616 s64 advance_expire_delta)
1618 struct kvm_lapic *apic = vcpu->arch.apic;
1619 u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
1622 /* Do not adjust for tiny fluctuations or large random spikes. */
1623 if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
1624 abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
1628 if (advance_expire_delta < 0) {
1629 ns = -advance_expire_delta * 1000000ULL;
1630 do_div(ns, vcpu->arch.virtual_tsc_khz);
1631 timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
1634 ns = advance_expire_delta * 1000000ULL;
1635 do_div(ns, vcpu->arch.virtual_tsc_khz);
1636 timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
1639 if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
1640 timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
1641 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1644 static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
1646 struct kvm_lapic *apic = vcpu->arch.apic;
1647 u64 guest_tsc, tsc_deadline;
1649 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1650 apic->lapic_timer.expired_tscdeadline = 0;
1651 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1652 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
1654 if (lapic_timer_advance_dynamic) {
1655 adjust_lapic_timer_advance(vcpu, guest_tsc - tsc_deadline);
1657 * If the timer fired early, reread the TSC to account for the
1658 * overhead of the above adjustment to avoid waiting longer
1659 * than is necessary.
1661 if (guest_tsc < tsc_deadline)
1662 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1665 if (guest_tsc < tsc_deadline)
1666 __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
1669 void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
1671 if (lapic_in_kernel(vcpu) &&
1672 vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1673 vcpu->arch.apic->lapic_timer.timer_advance_ns &&
1674 lapic_timer_int_injected(vcpu))
1675 __kvm_wait_lapic_expire(vcpu);
1677 EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
1679 static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
1681 struct kvm_timer *ktimer = &apic->lapic_timer;
1683 kvm_apic_local_deliver(apic, APIC_LVTT);
1684 if (apic_lvtt_tscdeadline(apic)) {
1685 ktimer->tscdeadline = 0;
1686 } else if (apic_lvtt_oneshot(apic)) {
1687 ktimer->tscdeadline = 0;
1688 ktimer->target_expiration = 0;
1692 static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
1694 struct kvm_vcpu *vcpu = apic->vcpu;
1695 struct kvm_timer *ktimer = &apic->lapic_timer;
1697 if (atomic_read(&apic->lapic_timer.pending))
1700 if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
1701 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1703 if (!from_timer_fn && vcpu->arch.apicv_active) {
1704 WARN_ON(kvm_get_running_vcpu() != vcpu);
1705 kvm_apic_inject_pending_timer_irqs(apic);
1709 if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
1711 * Ensure the guest's timer has truly expired before posting an
1712 * interrupt. Open code the relevant checks to avoid querying
1713 * lapic_timer_int_injected(), which will be false since the
1714 * interrupt isn't yet injected. Waiting until after injecting
1715 * is not an option since that won't help a posted interrupt.
1717 if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1718 vcpu->arch.apic->lapic_timer.timer_advance_ns)
1719 __kvm_wait_lapic_expire(vcpu);
1720 kvm_apic_inject_pending_timer_irqs(apic);
1724 atomic_inc(&apic->lapic_timer.pending);
1725 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
1727 kvm_vcpu_kick(vcpu);
1730 static void start_sw_tscdeadline(struct kvm_lapic *apic)
1732 struct kvm_timer *ktimer = &apic->lapic_timer;
1733 u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
1736 struct kvm_vcpu *vcpu = apic->vcpu;
1737 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1738 unsigned long flags;
1741 if (unlikely(!tscdeadline || !this_tsc_khz))
1744 local_irq_save(flags);
1747 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1749 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1750 do_div(ns, this_tsc_khz);
1752 if (likely(tscdeadline > guest_tsc) &&
1753 likely(ns > apic->lapic_timer.timer_advance_ns)) {
1754 expire = ktime_add_ns(now, ns);
1755 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
1756 hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
1758 apic_timer_expired(apic, false);
1760 local_irq_restore(flags);
1763 static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
1765 return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count;
1768 static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1770 ktime_t now, remaining;
1771 u64 ns_remaining_old, ns_remaining_new;
1773 apic->lapic_timer.period =
1774 tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
1775 limit_periodic_timer_frequency(apic);
1778 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1779 if (ktime_to_ns(remaining) < 0)
1782 ns_remaining_old = ktime_to_ns(remaining);
1783 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1784 apic->divide_count, old_divisor);
1786 apic->lapic_timer.tscdeadline +=
1787 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1788 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1789 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1792 static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
1799 apic->lapic_timer.period =
1800 tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
1802 if (!apic->lapic_timer.period) {
1803 apic->lapic_timer.tscdeadline = 0;
1807 limit_periodic_timer_frequency(apic);
1808 deadline = apic->lapic_timer.period;
1810 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
1811 if (unlikely(count_reg != APIC_TMICT)) {
1812 deadline = tmict_to_ns(apic,
1813 kvm_lapic_get_reg(apic, count_reg));
1814 if (unlikely(deadline <= 0))
1815 deadline = apic->lapic_timer.period;
1816 else if (unlikely(deadline > apic->lapic_timer.period)) {
1817 pr_info_ratelimited(
1818 "kvm: vcpu %i: requested lapic timer restore with "
1819 "starting count register %#x=%u (%lld ns) > initial count (%lld ns). "
1820 "Using initial count to start timer.\n",
1821 apic->vcpu->vcpu_id,
1823 kvm_lapic_get_reg(apic, count_reg),
1824 deadline, apic->lapic_timer.period);
1825 kvm_lapic_set_reg(apic, count_reg, 0);
1826 deadline = apic->lapic_timer.period;
1831 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1832 nsec_to_cycles(apic->vcpu, deadline);
1833 apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
1838 static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1840 ktime_t now = ktime_get();
1845 * Synchronize both deadlines to the same time source or
1846 * differences in the periods (caused by differences in the
1847 * underlying clocks or numerical approximation errors) will
1848 * cause the two to drift apart over time as the errors
1851 apic->lapic_timer.target_expiration =
1852 ktime_add_ns(apic->lapic_timer.target_expiration,
1853 apic->lapic_timer.period);
1854 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1855 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1856 nsec_to_cycles(apic->vcpu, delta);
1859 static void start_sw_period(struct kvm_lapic *apic)
1861 if (!apic->lapic_timer.period)
1864 if (ktime_after(ktime_get(),
1865 apic->lapic_timer.target_expiration)) {
1866 apic_timer_expired(apic, false);
1868 if (apic_lvtt_oneshot(apic))
1871 advance_periodic_target_expiration(apic);
1874 hrtimer_start(&apic->lapic_timer.timer,
1875 apic->lapic_timer.target_expiration,
1876 HRTIMER_MODE_ABS_HARD);
1879 bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1881 if (!lapic_in_kernel(vcpu))
1884 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1886 EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1888 static void cancel_hv_timer(struct kvm_lapic *apic)
1890 WARN_ON(preemptible());
1891 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1892 static_call(kvm_x86_cancel_hv_timer)(apic->vcpu);
1893 apic->lapic_timer.hv_timer_in_use = false;
1896 static bool start_hv_timer(struct kvm_lapic *apic)
1898 struct kvm_timer *ktimer = &apic->lapic_timer;
1899 struct kvm_vcpu *vcpu = apic->vcpu;
1902 WARN_ON(preemptible());
1903 if (!kvm_can_use_hv_timer(vcpu))
1906 if (!ktimer->tscdeadline)
1909 if (static_call(kvm_x86_set_hv_timer)(vcpu, ktimer->tscdeadline, &expired))
1912 ktimer->hv_timer_in_use = true;
1913 hrtimer_cancel(&ktimer->timer);
1916 * To simplify handling the periodic timer, leave the hv timer running
1917 * even if the deadline timer has expired, i.e. rely on the resulting
1918 * VM-Exit to recompute the periodic timer's target expiration.
1920 if (!apic_lvtt_period(apic)) {
1922 * Cancel the hv timer if the sw timer fired while the hv timer
1923 * was being programmed, or if the hv timer itself expired.
1925 if (atomic_read(&ktimer->pending)) {
1926 cancel_hv_timer(apic);
1927 } else if (expired) {
1928 apic_timer_expired(apic, false);
1929 cancel_hv_timer(apic);
1933 trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
1938 static void start_sw_timer(struct kvm_lapic *apic)
1940 struct kvm_timer *ktimer = &apic->lapic_timer;
1942 WARN_ON(preemptible());
1943 if (apic->lapic_timer.hv_timer_in_use)
1944 cancel_hv_timer(apic);
1945 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1948 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1949 start_sw_period(apic);
1950 else if (apic_lvtt_tscdeadline(apic))
1951 start_sw_tscdeadline(apic);
1952 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1955 static void restart_apic_timer(struct kvm_lapic *apic)
1959 if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
1962 if (!start_hv_timer(apic))
1963 start_sw_timer(apic);
1968 void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1970 struct kvm_lapic *apic = vcpu->arch.apic;
1973 /* If the preempt notifier has already run, it also called apic_timer_expired */
1974 if (!apic->lapic_timer.hv_timer_in_use)
1976 WARN_ON(kvm_vcpu_is_blocking(vcpu));
1977 apic_timer_expired(apic, false);
1978 cancel_hv_timer(apic);
1980 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1981 advance_periodic_target_expiration(apic);
1982 restart_apic_timer(apic);
1987 EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1989 void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1991 restart_apic_timer(vcpu->arch.apic);
1994 void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1996 struct kvm_lapic *apic = vcpu->arch.apic;
1999 /* Possibly the TSC deadline timer is not enabled yet */
2000 if (apic->lapic_timer.hv_timer_in_use)
2001 start_sw_timer(apic);
2005 void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
2007 struct kvm_lapic *apic = vcpu->arch.apic;
2009 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
2010 restart_apic_timer(apic);
2013 static void __start_apic_timer(struct kvm_lapic *apic, u32 count_reg)
2015 atomic_set(&apic->lapic_timer.pending, 0);
2017 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
2018 && !set_target_expiration(apic, count_reg))
2021 restart_apic_timer(apic);
2024 static void start_apic_timer(struct kvm_lapic *apic)
2026 __start_apic_timer(apic, APIC_TMICT);
2029 static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
2031 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
2033 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
2034 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
2035 if (lvt0_in_nmi_mode) {
2036 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
2038 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
2042 static void kvm_lapic_xapic_id_updated(struct kvm_lapic *apic)
2044 struct kvm *kvm = apic->vcpu->kvm;
2046 if (KVM_BUG_ON(apic_x2apic_mode(apic), kvm))
2049 if (kvm_xapic_id(apic) == apic->vcpu->vcpu_id)
2052 kvm_set_apicv_inhibit(apic->vcpu->kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
2055 static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
2059 trace_kvm_apic_write(reg, val);
2062 case APIC_ID: /* Local APIC ID */
2063 if (!apic_x2apic_mode(apic)) {
2064 kvm_apic_set_xapic_id(apic, val >> 24);
2065 kvm_lapic_xapic_id_updated(apic);
2072 report_tpr_access(apic, true);
2073 apic_set_tpr(apic, val & 0xff);
2081 if (!apic_x2apic_mode(apic))
2082 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
2088 if (!apic_x2apic_mode(apic))
2089 kvm_apic_set_dfr(apic, val | 0x0FFFFFFF);
2096 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
2097 mask |= APIC_SPIV_DIRECTED_EOI;
2098 apic_set_spiv(apic, val & mask);
2099 if (!(val & APIC_SPIV_APIC_ENABLED)) {
2103 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
2104 lvt_val = kvm_lapic_get_reg(apic,
2105 APIC_LVTT + 0x10 * i);
2106 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
2107 lvt_val | APIC_LVT_MASKED);
2109 apic_update_lvtt(apic);
2110 atomic_set(&apic->lapic_timer.pending, 0);
2116 WARN_ON_ONCE(apic_x2apic_mode(apic));
2118 /* No delay here, so we always clear the pending bit */
2119 val &= ~APIC_ICR_BUSY;
2120 kvm_apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
2121 kvm_lapic_set_reg(apic, APIC_ICR, val);
2124 if (apic_x2apic_mode(apic))
2127 kvm_lapic_set_reg(apic, APIC_ICR2, val & 0xff000000);
2131 apic_manage_nmi_watchdog(apic, val);
2137 /* TODO: Check vector */
2141 if (!kvm_apic_sw_enabled(apic))
2142 val |= APIC_LVT_MASKED;
2143 size = ARRAY_SIZE(apic_lvt_mask);
2144 index = array_index_nospec(
2145 (reg - APIC_LVTT) >> 4, size);
2146 val &= apic_lvt_mask[index];
2147 kvm_lapic_set_reg(apic, reg, val);
2152 if (!kvm_apic_sw_enabled(apic))
2153 val |= APIC_LVT_MASKED;
2154 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
2155 kvm_lapic_set_reg(apic, APIC_LVTT, val);
2156 apic_update_lvtt(apic);
2160 if (apic_lvtt_tscdeadline(apic))
2163 cancel_apic_timer(apic);
2164 kvm_lapic_set_reg(apic, APIC_TMICT, val);
2165 start_apic_timer(apic);
2169 uint32_t old_divisor = apic->divide_count;
2171 kvm_lapic_set_reg(apic, APIC_TDCR, val & 0xb);
2172 update_divide_count(apic);
2173 if (apic->divide_count != old_divisor &&
2174 apic->lapic_timer.period) {
2175 hrtimer_cancel(&apic->lapic_timer.timer);
2176 update_target_expiration(apic, old_divisor);
2177 restart_apic_timer(apic);
2182 if (apic_x2apic_mode(apic) && val != 0)
2187 if (apic_x2apic_mode(apic))
2188 kvm_apic_send_ipi(apic, APIC_DEST_SELF | (val & APIC_VECTOR_MASK), 0);
2198 * Recalculate APIC maps if necessary, e.g. if the software enable bit
2199 * was toggled, the APIC ID changed, etc... The maps are marked dirty
2200 * on relevant changes, i.e. this is a nop for most writes.
2202 kvm_recalculate_apic_map(apic->vcpu->kvm);
2207 static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
2208 gpa_t address, int len, const void *data)
2210 struct kvm_lapic *apic = to_lapic(this);
2211 unsigned int offset = address - apic->base_address;
2214 if (!apic_mmio_in_range(apic, address))
2217 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2218 if (!kvm_check_has_quirk(vcpu->kvm,
2219 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2226 * APIC register must be aligned on 128-bits boundary.
2227 * 32/64/128 bits registers must be accessed thru 32 bits.
2230 if (len != 4 || (offset & 0xf))
2235 kvm_lapic_reg_write(apic, offset & 0xff0, val);
2240 void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
2242 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
2244 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
2246 /* emulate APIC access in a trap manner */
2247 void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
2249 u32 val = kvm_lapic_get_reg(vcpu->arch.apic, offset);
2251 /* TODO: optimize to just emulate side effect w/o one more write */
2252 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
2254 EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2256 void kvm_free_lapic(struct kvm_vcpu *vcpu)
2258 struct kvm_lapic *apic = vcpu->arch.apic;
2260 if (!vcpu->arch.apic)
2263 hrtimer_cancel(&apic->lapic_timer.timer);
2265 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2266 static_branch_slow_dec_deferred(&apic_hw_disabled);
2268 if (!apic->sw_enabled)
2269 static_branch_slow_dec_deferred(&apic_sw_disabled);
2272 free_page((unsigned long)apic->regs);
2278 *----------------------------------------------------------------------
2280 *----------------------------------------------------------------------
2282 u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2284 struct kvm_lapic *apic = vcpu->arch.apic;
2286 if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
2289 return apic->lapic_timer.tscdeadline;
2292 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2294 struct kvm_lapic *apic = vcpu->arch.apic;
2296 if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
2299 hrtimer_cancel(&apic->lapic_timer.timer);
2300 apic->lapic_timer.tscdeadline = data;
2301 start_apic_timer(apic);
2304 void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2306 apic_set_tpr(vcpu->arch.apic, (cr8 & 0x0f) << 4);
2309 u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2313 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
2315 return (tpr & 0xf0) >> 4;
2318 void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2320 u64 old_value = vcpu->arch.apic_base;
2321 struct kvm_lapic *apic = vcpu->arch.apic;
2323 vcpu->arch.apic_base = value;
2325 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2326 kvm_update_cpuid_runtime(vcpu);
2331 /* update jump label if enable bit changes */
2332 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
2333 if (value & MSR_IA32_APICBASE_ENABLE) {
2334 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2335 static_branch_slow_dec_deferred(&apic_hw_disabled);
2336 /* Check if there are APF page ready requests pending */
2337 kvm_make_request(KVM_REQ_APF_READY, vcpu);
2339 static_branch_inc(&apic_hw_disabled.key);
2340 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
2344 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2345 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2347 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2348 static_call_cond(kvm_x86_set_virtual_apic_mode)(vcpu);
2350 apic->base_address = apic->vcpu->arch.apic_base &
2351 MSR_IA32_APICBASE_BASE;
2353 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2354 apic->base_address != APIC_DEFAULT_PHYS_BASE) {
2355 kvm_set_apicv_inhibit(apic->vcpu->kvm,
2356 APICV_INHIBIT_REASON_APIC_BASE_MODIFIED);
2360 void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
2362 struct kvm_lapic *apic = vcpu->arch.apic;
2364 if (vcpu->arch.apicv_active) {
2365 /* irr_pending is always true when apicv is activated. */
2366 apic->irr_pending = true;
2367 apic->isr_count = 1;
2370 * Don't clear irr_pending, searching the IRR can race with
2371 * updates from the CPU as APICv is still active from hardware's
2372 * perspective. The flag will be cleared as appropriate when
2373 * KVM injects the interrupt.
2375 apic->isr_count = count_vectors(apic->regs + APIC_ISR);
2378 EXPORT_SYMBOL_GPL(kvm_apic_update_apicv);
2380 void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
2382 struct kvm_lapic *apic = vcpu->arch.apic;
2387 msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
2388 if (kvm_vcpu_is_reset_bsp(vcpu))
2389 msr_val |= MSR_IA32_APICBASE_BSP;
2390 kvm_lapic_set_base(vcpu, msr_val);
2396 /* Stop the timer in case it's a reset to an active apic */
2397 hrtimer_cancel(&apic->lapic_timer.timer);
2399 /* The xAPIC ID is set at RESET even if the APIC was already enabled. */
2401 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2402 kvm_apic_set_version(apic->vcpu);
2404 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2405 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
2406 apic_update_lvtt(apic);
2407 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2408 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
2409 kvm_lapic_set_reg(apic, APIC_LVT0,
2410 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
2411 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
2413 kvm_apic_set_dfr(apic, 0xffffffffU);
2414 apic_set_spiv(apic, 0xff);
2415 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
2416 if (!apic_x2apic_mode(apic))
2417 kvm_apic_set_ldr(apic, 0);
2418 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2419 if (!apic_x2apic_mode(apic)) {
2420 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2421 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2423 kvm_lapic_set_reg64(apic, APIC_ICR, 0);
2425 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2426 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
2427 for (i = 0; i < 8; i++) {
2428 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2429 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2430 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
2432 kvm_apic_update_apicv(vcpu);
2433 apic->highest_isr_cache = -1;
2434 update_divide_count(apic);
2435 atomic_set(&apic->lapic_timer.pending, 0);
2437 vcpu->arch.pv_eoi.msr_val = 0;
2438 apic_update_ppr(apic);
2439 if (vcpu->arch.apicv_active) {
2440 static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
2441 static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, -1);
2442 static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, -1);
2445 vcpu->arch.apic_arb_prio = 0;
2446 vcpu->arch.apic_attention = 0;
2448 kvm_recalculate_apic_map(vcpu->kvm);
2452 *----------------------------------------------------------------------
2454 *----------------------------------------------------------------------
2457 static bool lapic_is_periodic(struct kvm_lapic *apic)
2459 return apic_lvtt_period(apic);
2462 int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2464 struct kvm_lapic *apic = vcpu->arch.apic;
2466 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
2467 return atomic_read(&apic->lapic_timer.pending);
2472 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
2474 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
2475 int vector, mode, trig_mode;
2477 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
2478 vector = reg & APIC_VECTOR_MASK;
2479 mode = reg & APIC_MODE_MASK;
2480 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
2481 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2487 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
2489 struct kvm_lapic *apic = vcpu->arch.apic;
2492 kvm_apic_local_deliver(apic, APIC_LVT0);
2495 static const struct kvm_io_device_ops apic_mmio_ops = {
2496 .read = apic_mmio_read,
2497 .write = apic_mmio_write,
2500 static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2502 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
2503 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
2505 apic_timer_expired(apic, true);
2507 if (lapic_is_periodic(apic)) {
2508 advance_periodic_target_expiration(apic);
2509 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2510 return HRTIMER_RESTART;
2512 return HRTIMER_NORESTART;
2515 int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
2517 struct kvm_lapic *apic;
2519 ASSERT(vcpu != NULL);
2521 apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
2525 vcpu->arch.apic = apic;
2527 apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
2529 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2531 goto nomem_free_apic;
2535 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
2536 HRTIMER_MODE_ABS_HARD);
2537 apic->lapic_timer.timer.function = apic_timer_fn;
2538 if (timer_advance_ns == -1) {
2539 apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
2540 lapic_timer_advance_dynamic = true;
2542 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
2543 lapic_timer_advance_dynamic = false;
2547 * Stuff the APIC ENABLE bit in lieu of temporarily incrementing
2548 * apic_hw_disabled; the full RESET value is set by kvm_lapic_reset().
2550 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
2551 static_branch_inc(&apic_sw_disabled.key); /* sw disabled at reset */
2552 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
2557 vcpu->arch.apic = NULL;
2562 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2564 struct kvm_lapic *apic = vcpu->arch.apic;
2567 if (!kvm_apic_present(vcpu))
2570 __apic_update_ppr(apic, &ppr);
2571 return apic_has_interrupt_for_ppr(apic, ppr);
2573 EXPORT_SYMBOL_GPL(kvm_apic_has_interrupt);
2575 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2577 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
2579 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
2581 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2582 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2587 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2589 struct kvm_lapic *apic = vcpu->arch.apic;
2591 if (atomic_read(&apic->lapic_timer.pending) > 0) {
2592 kvm_apic_inject_pending_timer_irqs(apic);
2593 atomic_set(&apic->lapic_timer.pending, 0);
2597 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2599 int vector = kvm_apic_has_interrupt(vcpu);
2600 struct kvm_lapic *apic = vcpu->arch.apic;
2607 * We get here even with APIC virtualization enabled, if doing
2608 * nested virtualization and L1 runs with the "acknowledge interrupt
2609 * on exit" mode. Then we cannot inject the interrupt via RVI,
2610 * because the process would deliver it through the IDT.
2613 apic_clear_irr(vector, apic);
2614 if (to_hv_vcpu(vcpu) && test_bit(vector, to_hv_synic(vcpu)->auto_eoi_bitmap)) {
2616 * For auto-EOI interrupts, there might be another pending
2617 * interrupt above PPR, so check whether to raise another
2620 apic_update_ppr(apic);
2623 * For normal interrupts, PPR has been raised and there cannot
2624 * be a higher-priority pending interrupt---except if there was
2625 * a concurrent interrupt injection, but that would have
2626 * triggered KVM_REQ_EVENT already.
2628 apic_set_isr(vector, apic);
2629 __apic_update_ppr(apic, &ppr);
2635 static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2636 struct kvm_lapic_state *s, bool set)
2638 if (apic_x2apic_mode(vcpu->arch.apic)) {
2639 u32 *id = (u32 *)(s->regs + APIC_ID);
2640 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
2643 if (vcpu->kvm->arch.x2apic_format) {
2644 if (*id != vcpu->vcpu_id)
2654 * In x2APIC mode, the LDR is fixed and based on the id. And
2655 * ICR is internally a single 64-bit register, but needs to be
2656 * split to ICR+ICR2 in userspace for backwards compatibility.
2659 *ldr = kvm_apic_calc_x2apic_ldr(*id);
2661 icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) |
2662 (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32;
2663 __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr);
2665 icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR);
2666 __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32);
2669 kvm_lapic_xapic_id_updated(vcpu->arch.apic);
2675 int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2677 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2680 * Get calculated timer current count for remaining timer period (if
2681 * any) and store it in the returned register set.
2683 __kvm_lapic_set_reg(s->regs, APIC_TMCCT,
2684 __apic_read(vcpu->arch.apic, APIC_TMCCT));
2686 return kvm_apic_state_fixup(vcpu, s, false);
2689 int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2691 struct kvm_lapic *apic = vcpu->arch.apic;
2694 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
2695 /* set SPIV separately to get count of SW disabled APICs right */
2696 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
2698 r = kvm_apic_state_fixup(vcpu, s, true);
2700 kvm_recalculate_apic_map(vcpu->kvm);
2703 memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
2705 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
2706 kvm_recalculate_apic_map(vcpu->kvm);
2707 kvm_apic_set_version(vcpu);
2709 apic_update_ppr(apic);
2710 cancel_apic_timer(apic);
2711 apic->lapic_timer.expired_tscdeadline = 0;
2712 apic_update_lvtt(apic);
2713 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
2714 update_divide_count(apic);
2715 __start_apic_timer(apic, APIC_TMCCT);
2716 kvm_lapic_set_reg(apic, APIC_TMCCT, 0);
2717 kvm_apic_update_apicv(vcpu);
2718 apic->highest_isr_cache = -1;
2719 if (vcpu->arch.apicv_active) {
2720 static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
2721 static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, apic_find_highest_irr(apic));
2722 static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, apic_find_highest_isr(apic));
2724 kvm_make_request(KVM_REQ_EVENT, vcpu);
2725 if (ioapic_in_kernel(vcpu->kvm))
2726 kvm_rtc_eoi_tracking_restore_one(vcpu);
2728 vcpu->arch.apic_arb_prio = 0;
2733 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
2735 struct hrtimer *timer;
2737 if (!lapic_in_kernel(vcpu) ||
2738 kvm_can_post_timer_interrupt(vcpu))
2741 timer = &vcpu->arch.apic->lapic_timer.timer;
2742 if (hrtimer_cancel(timer))
2743 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
2747 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2749 * Detect whether guest triggered PV EOI since the
2750 * last entry. If yes, set EOI on guests's behalf.
2751 * Clear PV EOI in guest memory in any case.
2753 static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2754 struct kvm_lapic *apic)
2758 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2759 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2761 * KVM_APIC_PV_EOI_PENDING is unset:
2762 * -> host disabled PV EOI.
2763 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2764 * -> host enabled PV EOI, guest did not execute EOI yet.
2765 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2766 * -> host enabled PV EOI, guest executed EOI.
2768 BUG_ON(!pv_eoi_enabled(vcpu));
2770 if (pv_eoi_test_and_clr_pending(vcpu))
2772 vector = apic_set_eoi(apic);
2773 trace_kvm_pv_eoi(apic, vector);
2776 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2780 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2781 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2783 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
2786 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2790 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2794 * apic_sync_pv_eoi_to_guest - called before vmentry
2796 * Detect whether it's safe to enable PV EOI and
2799 static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2800 struct kvm_lapic *apic)
2802 if (!pv_eoi_enabled(vcpu) ||
2803 /* IRR set or many bits in ISR: could be nested. */
2804 apic->irr_pending ||
2805 /* Cache not set: could be safe but we don't bother. */
2806 apic->highest_isr_cache == -1 ||
2807 /* Need EOI to update ioapic. */
2808 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
2810 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2811 * so we need not do anything here.
2816 pv_eoi_set_pending(apic->vcpu);
2819 void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2822 int max_irr, max_isr;
2823 struct kvm_lapic *apic = vcpu->arch.apic;
2825 apic_sync_pv_eoi_to_guest(vcpu, apic);
2827 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
2830 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
2831 max_irr = apic_find_highest_irr(apic);
2834 max_isr = apic_find_highest_isr(apic);
2837 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2839 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2843 int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
2846 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2847 &vcpu->arch.apic->vapic_cache,
2848 vapic_addr, sizeof(u32)))
2850 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
2852 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
2855 vcpu->arch.apic->vapic_addr = vapic_addr;
2859 int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data)
2861 data &= ~APIC_ICR_BUSY;
2863 kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32));
2864 kvm_lapic_set_reg64(apic, APIC_ICR, data);
2865 trace_kvm_apic_write(APIC_ICR, data);
2869 static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data)
2873 if (reg == APIC_ICR) {
2874 *data = kvm_lapic_get_reg64(apic, APIC_ICR);
2878 if (kvm_lapic_reg_read(apic, reg, 4, &low))
2886 static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data)
2889 * ICR is a 64-bit register in x2APIC mode (and Hyper'v PV vAPIC) and
2890 * can be written as such, all other registers remain accessible only
2891 * through 32-bit reads/writes.
2893 if (reg == APIC_ICR)
2894 return kvm_x2apic_icr_write(apic, data);
2896 return kvm_lapic_reg_write(apic, reg, (u32)data);
2899 int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2901 struct kvm_lapic *apic = vcpu->arch.apic;
2902 u32 reg = (msr - APIC_BASE_MSR) << 4;
2904 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
2907 return kvm_lapic_msr_write(apic, reg, data);
2910 int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2912 struct kvm_lapic *apic = vcpu->arch.apic;
2913 u32 reg = (msr - APIC_BASE_MSR) << 4;
2915 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
2918 if (reg == APIC_DFR)
2921 return kvm_lapic_msr_read(apic, reg, data);
2924 int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2926 if (!lapic_in_kernel(vcpu))
2929 return kvm_lapic_msr_write(vcpu->arch.apic, reg, data);
2932 int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2934 if (!lapic_in_kernel(vcpu))
2937 return kvm_lapic_msr_read(vcpu->arch.apic, reg, data);
2940 int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
2942 u64 addr = data & ~KVM_MSR_ENABLED;
2943 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
2944 unsigned long new_len;
2947 if (!IS_ALIGNED(addr, 4))
2950 if (data & KVM_MSR_ENABLED) {
2951 if (addr == ghc->gpa && len <= ghc->len)
2956 ret = kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
2961 vcpu->arch.pv_eoi.msr_val = data;
2966 int kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2968 struct kvm_lapic *apic = vcpu->arch.apic;
2973 if (!lapic_in_kernel(vcpu))
2977 * Read pending events before calling the check_events
2980 pe = smp_load_acquire(&apic->pending_events);
2984 if (is_guest_mode(vcpu)) {
2985 r = kvm_check_nested_events(vcpu);
2987 return r == -EBUSY ? 0 : r;
2989 * If an event has happened and caused a vmexit,
2990 * we know INITs are latched and therefore
2991 * we will not incorrectly deliver an APIC
2992 * event instead of a vmexit.
2997 * INITs are latched while CPU is in specific states
2998 * (SMM, VMX root mode, SVM with GIF=0).
2999 * Because a CPU cannot be in these states immediately
3000 * after it has processed an INIT signal (and thus in
3001 * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs
3002 * and leave the INIT pending.
3004 if (kvm_vcpu_latch_init(vcpu)) {
3005 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
3006 if (test_bit(KVM_APIC_SIPI, &pe))
3007 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3011 if (test_bit(KVM_APIC_INIT, &pe)) {
3012 clear_bit(KVM_APIC_INIT, &apic->pending_events);
3013 kvm_vcpu_reset(vcpu, true);
3014 if (kvm_vcpu_is_bsp(apic->vcpu))
3015 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3017 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3019 if (test_bit(KVM_APIC_SIPI, &pe)) {
3020 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3021 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
3022 /* evaluate pending_events before reading the vector */
3024 sipi_vector = apic->sipi_vector;
3025 static_call(kvm_x86_vcpu_deliver_sipi_vector)(vcpu, sipi_vector);
3026 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3032 void kvm_lapic_exit(void)
3034 static_key_deferred_flush(&apic_hw_disabled);
3035 WARN_ON(static_branch_unlikely(&apic_hw_disabled.key));
3036 static_key_deferred_flush(&apic_sw_disabled);
3037 WARN_ON(static_branch_unlikely(&apic_sw_disabled.key));