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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
23 #include <linux/highmem.h>
24 #include <linux/smp.h>
25 #include <linux/hrtimer.h>
27 #include <linux/export.h>
28 #include <linux/math64.h>
29 #include <linux/slab.h>
30 #include <asm/processor.h>
34 #include <asm/current.h>
35 #include <asm/apicdef.h>
36 #include <asm/delay.h>
37 #include <linux/atomic.h>
38 #include <linux/jump_label.h>
39 #include "kvm_cache_regs.h"
50 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
52 #define mod_64(x, y) ((x) % (y))
55 /* 14 is the version for Xeon and Pentium 8.4.8*/
56 #define APIC_VERSION 0x14UL
57 #define LAPIC_MMIO_LENGTH (1 << 12)
58 /* followed define is not in apicdef.h */
59 #define MAX_APIC_VECTOR 256
60 #define APIC_VECTORS_PER_REG 32
63 * Enable local APIC timer advancement (tscdeadline mode only) with adaptive
64 * tuning. When enabled, KVM programs the host timer event to fire early, i.e.
65 * before the deadline expires, to account for the delay between taking the
66 * VM-Exit (to inject the guest event) and the subsequent VM-Enter to resume
67 * the guest, i.e. so that the interrupt arrives in the guest with minimal
68 * latency relative to the deadline programmed by the guest.
70 static bool lapic_timer_advance __read_mostly
= true;
71 module_param(lapic_timer_advance
, bool, 0444);
73 #define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
74 #define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
75 #define LAPIC_TIMER_ADVANCE_NS_INIT 1000
76 #define LAPIC_TIMER_ADVANCE_NS_MAX 5000
77 /* step-by-step approximation to mitigate fluctuation */
78 #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
79 static int kvm_lapic_msr_read(struct kvm_lapic
*apic
, u32 reg
, u64
*data
);
80 static int kvm_lapic_msr_write(struct kvm_lapic
*apic
, u32 reg
, u64 data
);
82 static inline void __kvm_lapic_set_reg(char *regs
, int reg_off
, u32 val
)
84 *((u32
*) (regs
+ reg_off
)) = val
;
87 static inline void kvm_lapic_set_reg(struct kvm_lapic
*apic
, int reg_off
, u32 val
)
89 __kvm_lapic_set_reg(apic
->regs
, reg_off
, val
);
92 static __always_inline u64
__kvm_lapic_get_reg64(char *regs
, int reg
)
94 BUILD_BUG_ON(reg
!= APIC_ICR
);
95 return *((u64
*) (regs
+ reg
));
98 static __always_inline u64
kvm_lapic_get_reg64(struct kvm_lapic
*apic
, int reg
)
100 return __kvm_lapic_get_reg64(apic
->regs
, reg
);
103 static __always_inline
void __kvm_lapic_set_reg64(char *regs
, int reg
, u64 val
)
105 BUILD_BUG_ON(reg
!= APIC_ICR
);
106 *((u64
*) (regs
+ reg
)) = val
;
109 static __always_inline
void kvm_lapic_set_reg64(struct kvm_lapic
*apic
,
112 __kvm_lapic_set_reg64(apic
->regs
, reg
, val
);
115 static inline int apic_test_vector(int vec
, void *bitmap
)
117 return test_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
120 bool kvm_apic_pending_eoi(struct kvm_vcpu
*vcpu
, int vector
)
122 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
124 return apic_test_vector(vector
, apic
->regs
+ APIC_ISR
) ||
125 apic_test_vector(vector
, apic
->regs
+ APIC_IRR
);
128 static inline int __apic_test_and_set_vector(int vec
, void *bitmap
)
130 return __test_and_set_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
133 static inline int __apic_test_and_clear_vector(int vec
, void *bitmap
)
135 return __test_and_clear_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
138 __read_mostly
DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu
);
139 EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu
);
141 __read_mostly
DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_hw_disabled
, HZ
);
142 __read_mostly
DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_sw_disabled
, HZ
);
144 static inline int apic_enabled(struct kvm_lapic
*apic
)
146 return kvm_apic_sw_enabled(apic
) && kvm_apic_hw_enabled(apic
);
150 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
153 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
154 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
156 static inline u32
kvm_x2apic_id(struct kvm_lapic
*apic
)
158 return apic
->vcpu
->vcpu_id
;
161 static bool kvm_can_post_timer_interrupt(struct kvm_vcpu
*vcpu
)
163 return pi_inject_timer
&& kvm_vcpu_apicv_active(vcpu
) &&
164 (kvm_mwait_in_guest(vcpu
->kvm
) || kvm_hlt_in_guest(vcpu
->kvm
));
167 bool kvm_can_use_hv_timer(struct kvm_vcpu
*vcpu
)
169 return kvm_x86_ops
.set_hv_timer
170 && !(kvm_mwait_in_guest(vcpu
->kvm
) ||
171 kvm_can_post_timer_interrupt(vcpu
));
174 static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu
*vcpu
)
176 return kvm_can_post_timer_interrupt(vcpu
) && vcpu
->mode
== IN_GUEST_MODE
;
179 static inline u32
kvm_apic_calc_x2apic_ldr(u32 id
)
181 return ((id
>> 4) << 16) | (1 << (id
& 0xf));
184 static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map
*map
,
185 u32 dest_id
, struct kvm_lapic
***cluster
, u16
*mask
) {
186 switch (map
->logical_mode
) {
187 case KVM_APIC_MODE_SW_DISABLED
:
188 /* Arbitrarily use the flat map so that @cluster isn't NULL. */
189 *cluster
= map
->xapic_flat_map
;
192 case KVM_APIC_MODE_X2APIC
: {
193 u32 offset
= (dest_id
>> 16) * 16;
194 u32 max_apic_id
= map
->max_apic_id
;
196 if (offset
<= max_apic_id
) {
197 u8 cluster_size
= min(max_apic_id
- offset
+ 1, 16U);
199 offset
= array_index_nospec(offset
, map
->max_apic_id
+ 1);
200 *cluster
= &map
->phys_map
[offset
];
201 *mask
= dest_id
& (0xffff >> (16 - cluster_size
));
208 case KVM_APIC_MODE_XAPIC_FLAT
:
209 *cluster
= map
->xapic_flat_map
;
210 *mask
= dest_id
& 0xff;
212 case KVM_APIC_MODE_XAPIC_CLUSTER
:
213 *cluster
= map
->xapic_cluster_map
[(dest_id
>> 4) & 0xf];
214 *mask
= dest_id
& 0xf;
216 case KVM_APIC_MODE_MAP_DISABLED
:
224 static int kvm_recalculate_phys_map(struct kvm_apic_map
*new,
225 struct kvm_vcpu
*vcpu
,
226 bool *xapic_id_mismatch
)
228 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
229 u32 x2apic_id
= kvm_x2apic_id(apic
);
230 u32 xapic_id
= kvm_xapic_id(apic
);
234 * For simplicity, KVM always allocates enough space for all possible
235 * xAPIC IDs. Yell, but don't kill the VM, as KVM can continue on
236 * without the optimized map.
238 if (WARN_ON_ONCE(xapic_id
> new->max_apic_id
))
242 * Bail if a vCPU was added and/or enabled its APIC between allocating
243 * the map and doing the actual calculations for the map. Note, KVM
244 * hardcodes the x2APIC ID to vcpu_id, i.e. there's no TOCTOU bug if
245 * the compiler decides to reload x2apic_id after this check.
247 if (x2apic_id
> new->max_apic_id
)
251 * Deliberately truncate the vCPU ID when detecting a mismatched APIC
252 * ID to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a
253 * 32-bit value. Any unwanted aliasing due to truncation results will
256 if (!apic_x2apic_mode(apic
) && xapic_id
!= (u8
)vcpu
->vcpu_id
)
257 *xapic_id_mismatch
= true;
260 * Apply KVM's hotplug hack if userspace has enable 32-bit APIC IDs.
261 * Allow sending events to vCPUs by their x2APIC ID even if the target
262 * vCPU is in legacy xAPIC mode, and silently ignore aliased xAPIC IDs
263 * (the x2APIC ID is truncated to 8 bits, causing IDs > 0xff to wrap
266 * Honor the architectural (and KVM's non-optimized) behavior if
267 * userspace has not enabled 32-bit x2APIC IDs. Each APIC is supposed
268 * to process messages independently. If multiple vCPUs have the same
269 * effective APIC ID, e.g. due to the x2APIC wrap or because the guest
270 * manually modified its xAPIC IDs, events targeting that ID are
271 * supposed to be recognized by all vCPUs with said ID.
273 if (vcpu
->kvm
->arch
.x2apic_format
) {
274 /* See also kvm_apic_match_physical_addr(). */
275 if (apic_x2apic_mode(apic
) || x2apic_id
> 0xff)
276 new->phys_map
[x2apic_id
] = apic
;
278 if (!apic_x2apic_mode(apic
) && !new->phys_map
[xapic_id
])
279 new->phys_map
[xapic_id
] = apic
;
282 * Disable the optimized map if the physical APIC ID is already
283 * mapped, i.e. is aliased to multiple vCPUs. The optimized
284 * map requires a strict 1:1 mapping between IDs and vCPUs.
286 if (apic_x2apic_mode(apic
))
287 physical_id
= x2apic_id
;
289 physical_id
= xapic_id
;
291 if (new->phys_map
[physical_id
])
294 new->phys_map
[physical_id
] = apic
;
300 static void kvm_recalculate_logical_map(struct kvm_apic_map
*new,
301 struct kvm_vcpu
*vcpu
)
303 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
304 enum kvm_apic_logical_mode logical_mode
;
305 struct kvm_lapic
**cluster
;
309 if (new->logical_mode
== KVM_APIC_MODE_MAP_DISABLED
)
312 if (!kvm_apic_sw_enabled(apic
))
315 ldr
= kvm_lapic_get_reg(apic
, APIC_LDR
);
319 if (apic_x2apic_mode(apic
)) {
320 logical_mode
= KVM_APIC_MODE_X2APIC
;
322 ldr
= GET_APIC_LOGICAL_ID(ldr
);
323 if (kvm_lapic_get_reg(apic
, APIC_DFR
) == APIC_DFR_FLAT
)
324 logical_mode
= KVM_APIC_MODE_XAPIC_FLAT
;
326 logical_mode
= KVM_APIC_MODE_XAPIC_CLUSTER
;
330 * To optimize logical mode delivery, all software-enabled APICs must
331 * be configured for the same mode.
333 if (new->logical_mode
== KVM_APIC_MODE_SW_DISABLED
) {
334 new->logical_mode
= logical_mode
;
335 } else if (new->logical_mode
!= logical_mode
) {
336 new->logical_mode
= KVM_APIC_MODE_MAP_DISABLED
;
341 * In x2APIC mode, the LDR is read-only and derived directly from the
342 * x2APIC ID, thus is guaranteed to be addressable. KVM reuses
343 * kvm_apic_map.phys_map to optimize logical mode x2APIC interrupts by
344 * reversing the LDR calculation to get cluster of APICs, i.e. no
345 * additional work is required.
347 if (apic_x2apic_mode(apic
))
350 if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr
,
352 new->logical_mode
= KVM_APIC_MODE_MAP_DISABLED
;
360 if (!is_power_of_2(mask
) || cluster
[ldr
])
361 new->logical_mode
= KVM_APIC_MODE_MAP_DISABLED
;
367 * CLEAN -> DIRTY and UPDATE_IN_PROGRESS -> DIRTY changes happen without a lock.
369 * DIRTY -> UPDATE_IN_PROGRESS and UPDATE_IN_PROGRESS -> CLEAN happen with
370 * apic_map_lock_held.
378 static void kvm_recalculate_apic_map(struct kvm
*kvm
)
380 struct kvm_apic_map
*new, *old
= NULL
;
381 struct kvm_vcpu
*vcpu
;
383 u32 max_id
= 255; /* enough space for any xAPIC ID */
384 bool xapic_id_mismatch
;
387 /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
388 if (atomic_read_acquire(&kvm
->arch
.apic_map_dirty
) == CLEAN
)
391 WARN_ONCE(!irqchip_in_kernel(kvm
),
392 "Dirty APIC map without an in-kernel local APIC");
394 mutex_lock(&kvm
->arch
.apic_map_lock
);
398 * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean)
399 * or the APIC registers (if dirty). Note, on retry the map may have
400 * not yet been marked dirty by whatever task changed a vCPU's x2APIC
401 * ID, i.e. the map may still show up as in-progress. In that case
402 * this task still needs to retry and complete its calculation.
404 if (atomic_cmpxchg_acquire(&kvm
->arch
.apic_map_dirty
,
405 DIRTY
, UPDATE_IN_PROGRESS
) == CLEAN
) {
406 /* Someone else has updated the map. */
407 mutex_unlock(&kvm
->arch
.apic_map_lock
);
412 * Reset the mismatch flag between attempts so that KVM does the right
413 * thing if a vCPU changes its xAPIC ID, but do NOT reset max_id, i.e.
414 * keep max_id strictly increasing. Disallowing max_id from shrinking
415 * ensures KVM won't get stuck in an infinite loop, e.g. if the vCPU
416 * with the highest x2APIC ID is toggling its APIC on and off.
418 xapic_id_mismatch
= false;
420 kvm_for_each_vcpu(i
, vcpu
, kvm
)
421 if (kvm_apic_present(vcpu
))
422 max_id
= max(max_id
, kvm_x2apic_id(vcpu
->arch
.apic
));
424 new = kvzalloc(sizeof(struct kvm_apic_map
) +
425 sizeof(struct kvm_lapic
*) * ((u64
)max_id
+ 1),
431 new->max_apic_id
= max_id
;
432 new->logical_mode
= KVM_APIC_MODE_SW_DISABLED
;
434 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
435 if (!kvm_apic_present(vcpu
))
438 r
= kvm_recalculate_phys_map(new, vcpu
, &xapic_id_mismatch
);
450 kvm_recalculate_logical_map(new, vcpu
);
454 * The optimized map is effectively KVM's internal version of APICv,
455 * and all unwanted aliasing that results in disabling the optimized
456 * map also applies to APICv.
459 kvm_set_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED
);
461 kvm_clear_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED
);
463 if (!new || new->logical_mode
== KVM_APIC_MODE_MAP_DISABLED
)
464 kvm_set_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED
);
466 kvm_clear_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED
);
468 if (xapic_id_mismatch
)
469 kvm_set_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_APIC_ID_MODIFIED
);
471 kvm_clear_apicv_inhibit(kvm
, APICV_INHIBIT_REASON_APIC_ID_MODIFIED
);
473 old
= rcu_dereference_protected(kvm
->arch
.apic_map
,
474 lockdep_is_held(&kvm
->arch
.apic_map_lock
));
475 rcu_assign_pointer(kvm
->arch
.apic_map
, new);
477 * Write kvm->arch.apic_map before clearing apic->apic_map_dirty.
478 * If another update has come in, leave it DIRTY.
480 atomic_cmpxchg_release(&kvm
->arch
.apic_map_dirty
,
481 UPDATE_IN_PROGRESS
, CLEAN
);
482 mutex_unlock(&kvm
->arch
.apic_map_lock
);
485 kvfree_rcu(old
, rcu
);
487 kvm_make_scan_ioapic_request(kvm
);
490 static inline void apic_set_spiv(struct kvm_lapic
*apic
, u32 val
)
492 bool enabled
= val
& APIC_SPIV_APIC_ENABLED
;
494 kvm_lapic_set_reg(apic
, APIC_SPIV
, val
);
496 if (enabled
!= apic
->sw_enabled
) {
497 apic
->sw_enabled
= enabled
;
499 static_branch_slow_dec_deferred(&apic_sw_disabled
);
501 static_branch_inc(&apic_sw_disabled
.key
);
503 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
506 /* Check if there are APF page ready requests pending */
508 kvm_make_request(KVM_REQ_APF_READY
, apic
->vcpu
);
509 kvm_xen_sw_enable_lapic(apic
->vcpu
);
513 static inline void kvm_apic_set_xapic_id(struct kvm_lapic
*apic
, u8 id
)
515 kvm_lapic_set_reg(apic
, APIC_ID
, id
<< 24);
516 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
519 static inline void kvm_apic_set_ldr(struct kvm_lapic
*apic
, u32 id
)
521 kvm_lapic_set_reg(apic
, APIC_LDR
, id
);
522 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
525 static inline void kvm_apic_set_dfr(struct kvm_lapic
*apic
, u32 val
)
527 kvm_lapic_set_reg(apic
, APIC_DFR
, val
);
528 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
531 static inline void kvm_apic_set_x2apic_id(struct kvm_lapic
*apic
, u32 id
)
533 u32 ldr
= kvm_apic_calc_x2apic_ldr(id
);
535 WARN_ON_ONCE(id
!= apic
->vcpu
->vcpu_id
);
537 kvm_lapic_set_reg(apic
, APIC_ID
, id
);
538 kvm_lapic_set_reg(apic
, APIC_LDR
, ldr
);
539 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
542 static inline int apic_lvt_enabled(struct kvm_lapic
*apic
, int lvt_type
)
544 return !(kvm_lapic_get_reg(apic
, lvt_type
) & APIC_LVT_MASKED
);
547 static inline int apic_lvtt_oneshot(struct kvm_lapic
*apic
)
549 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_ONESHOT
;
552 static inline int apic_lvtt_period(struct kvm_lapic
*apic
)
554 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_PERIODIC
;
557 static inline int apic_lvtt_tscdeadline(struct kvm_lapic
*apic
)
559 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_TSCDEADLINE
;
562 static inline int apic_lvt_nmi_mode(u32 lvt_val
)
564 return (lvt_val
& (APIC_MODE_MASK
| APIC_LVT_MASKED
)) == APIC_DM_NMI
;
567 static inline bool kvm_lapic_lvt_supported(struct kvm_lapic
*apic
, int lvt_index
)
569 return apic
->nr_lvt_entries
> lvt_index
;
572 static inline int kvm_apic_calc_nr_lvt_entries(struct kvm_vcpu
*vcpu
)
574 return KVM_APIC_MAX_NR_LVT_ENTRIES
- !(vcpu
->arch
.mcg_cap
& MCG_CMCI_P
);
577 void kvm_apic_set_version(struct kvm_vcpu
*vcpu
)
579 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
582 if (!lapic_in_kernel(vcpu
))
585 v
= APIC_VERSION
| ((apic
->nr_lvt_entries
- 1) << 16);
588 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
589 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
590 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
591 * version first and level-triggered interrupts never get EOIed in
594 if (guest_cpu_cap_has(vcpu
, X86_FEATURE_X2APIC
) &&
595 !ioapic_in_kernel(vcpu
->kvm
))
596 v
|= APIC_LVR_DIRECTED_EOI
;
597 kvm_lapic_set_reg(apic
, APIC_LVR
, v
);
600 void kvm_apic_after_set_mcg_cap(struct kvm_vcpu
*vcpu
)
602 int nr_lvt_entries
= kvm_apic_calc_nr_lvt_entries(vcpu
);
603 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
606 if (!lapic_in_kernel(vcpu
) || nr_lvt_entries
== apic
->nr_lvt_entries
)
609 /* Initialize/mask any "new" LVT entries. */
610 for (i
= apic
->nr_lvt_entries
; i
< nr_lvt_entries
; i
++)
611 kvm_lapic_set_reg(apic
, APIC_LVTx(i
), APIC_LVT_MASKED
);
613 apic
->nr_lvt_entries
= nr_lvt_entries
;
615 /* The number of LVT entries is reflected in the version register. */
616 kvm_apic_set_version(vcpu
);
619 static const unsigned int apic_lvt_mask
[KVM_APIC_MAX_NR_LVT_ENTRIES
] = {
620 [LVT_TIMER
] = LVT_MASK
, /* timer mode mask added at runtime */
621 [LVT_THERMAL_MONITOR
] = LVT_MASK
| APIC_MODE_MASK
,
622 [LVT_PERFORMANCE_COUNTER
] = LVT_MASK
| APIC_MODE_MASK
,
623 [LVT_LINT0
] = LINT_MASK
,
624 [LVT_LINT1
] = LINT_MASK
,
625 [LVT_ERROR
] = LVT_MASK
,
626 [LVT_CMCI
] = LVT_MASK
| APIC_MODE_MASK
629 static int find_highest_vector(void *bitmap
)
634 for (vec
= MAX_APIC_VECTOR
- APIC_VECTORS_PER_REG
;
635 vec
>= 0; vec
-= APIC_VECTORS_PER_REG
) {
636 reg
= bitmap
+ REG_POS(vec
);
638 return __fls(*reg
) + vec
;
644 static u8
count_vectors(void *bitmap
)
650 for (vec
= 0; vec
< MAX_APIC_VECTOR
; vec
+= APIC_VECTORS_PER_REG
) {
651 reg
= bitmap
+ REG_POS(vec
);
652 count
+= hweight32(*reg
);
658 bool __kvm_apic_update_irr(unsigned long *pir
, void *regs
, int *max_irr
)
660 unsigned long pir_vals
[NR_PIR_WORDS
];
661 u32
*__pir
= (void *)pir_vals
;
663 u32 irr_val
, prev_irr_val
;
666 max_updated_irr
= -1;
669 if (!pi_harvest_pir(pir
, pir_vals
))
672 for (i
= vec
= 0; i
<= 7; i
++, vec
+= 32) {
673 u32
*p_irr
= (u32
*)(regs
+ APIC_IRR
+ i
* 0x10);
675 irr_val
= READ_ONCE(*p_irr
);
678 prev_irr_val
= irr_val
;
680 irr_val
= prev_irr_val
| __pir
[i
];
681 } while (prev_irr_val
!= irr_val
&&
682 !try_cmpxchg(p_irr
, &prev_irr_val
, irr_val
));
684 if (prev_irr_val
!= irr_val
)
685 max_updated_irr
= __fls(irr_val
^ prev_irr_val
) + vec
;
688 *max_irr
= __fls(irr_val
) + vec
;
691 return ((max_updated_irr
!= -1) &&
692 (max_updated_irr
== *max_irr
));
694 EXPORT_SYMBOL_GPL(__kvm_apic_update_irr
);
696 bool kvm_apic_update_irr(struct kvm_vcpu
*vcpu
, unsigned long *pir
, int *max_irr
)
698 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
699 bool irr_updated
= __kvm_apic_update_irr(pir
, apic
->regs
, max_irr
);
701 if (unlikely(!apic
->apicv_active
&& irr_updated
))
702 apic
->irr_pending
= true;
705 EXPORT_SYMBOL_GPL(kvm_apic_update_irr
);
707 static inline int apic_search_irr(struct kvm_lapic
*apic
)
709 return find_highest_vector(apic
->regs
+ APIC_IRR
);
712 static inline int apic_find_highest_irr(struct kvm_lapic
*apic
)
717 * Note that irr_pending is just a hint. It will be always
718 * true with virtual interrupt delivery enabled.
720 if (!apic
->irr_pending
)
723 result
= apic_search_irr(apic
);
724 ASSERT(result
== -1 || result
>= 16);
729 static inline void apic_clear_irr(int vec
, struct kvm_lapic
*apic
)
731 if (unlikely(apic
->apicv_active
)) {
732 kvm_lapic_clear_vector(vec
, apic
->regs
+ APIC_IRR
);
734 apic
->irr_pending
= false;
735 kvm_lapic_clear_vector(vec
, apic
->regs
+ APIC_IRR
);
736 if (apic_search_irr(apic
) != -1)
737 apic
->irr_pending
= true;
741 void kvm_apic_clear_irr(struct kvm_vcpu
*vcpu
, int vec
)
743 apic_clear_irr(vec
, vcpu
->arch
.apic
);
745 EXPORT_SYMBOL_GPL(kvm_apic_clear_irr
);
747 static inline void apic_set_isr(int vec
, struct kvm_lapic
*apic
)
749 if (__apic_test_and_set_vector(vec
, apic
->regs
+ APIC_ISR
))
753 * With APIC virtualization enabled, all caching is disabled
754 * because the processor can modify ISR under the hood. Instead
757 if (unlikely(apic
->apicv_active
))
758 kvm_x86_call(hwapic_isr_update
)(apic
->vcpu
, vec
);
761 BUG_ON(apic
->isr_count
> MAX_APIC_VECTOR
);
763 * ISR (in service register) bit is set when injecting an interrupt.
764 * The highest vector is injected. Thus the latest bit set matches
765 * the highest bit in ISR.
767 apic
->highest_isr_cache
= vec
;
771 static inline int apic_find_highest_isr(struct kvm_lapic
*apic
)
776 * Note that isr_count is always 1, and highest_isr_cache
777 * is always -1, with APIC virtualization enabled.
779 if (!apic
->isr_count
)
781 if (likely(apic
->highest_isr_cache
!= -1))
782 return apic
->highest_isr_cache
;
784 result
= find_highest_vector(apic
->regs
+ APIC_ISR
);
785 ASSERT(result
== -1 || result
>= 16);
790 static inline void apic_clear_isr(int vec
, struct kvm_lapic
*apic
)
792 if (!__apic_test_and_clear_vector(vec
, apic
->regs
+ APIC_ISR
))
796 * We do get here for APIC virtualization enabled if the guest
797 * uses the Hyper-V APIC enlightenment. In this case we may need
798 * to trigger a new interrupt delivery by writing the SVI field;
799 * on the other hand isr_count and highest_isr_cache are unused
800 * and must be left alone.
802 if (unlikely(apic
->apicv_active
))
803 kvm_x86_call(hwapic_isr_update
)(apic
->vcpu
, apic_find_highest_isr(apic
));
806 BUG_ON(apic
->isr_count
< 0);
807 apic
->highest_isr_cache
= -1;
811 void kvm_apic_update_hwapic_isr(struct kvm_vcpu
*vcpu
)
813 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
815 if (WARN_ON_ONCE(!lapic_in_kernel(vcpu
)) || !apic
->apicv_active
)
818 kvm_x86_call(hwapic_isr_update
)(vcpu
, apic_find_highest_isr(apic
));
820 EXPORT_SYMBOL_GPL(kvm_apic_update_hwapic_isr
);
822 int kvm_lapic_find_highest_irr(struct kvm_vcpu
*vcpu
)
824 /* This may race with setting of irr in __apic_accept_irq() and
825 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
826 * will cause vmexit immediately and the value will be recalculated
827 * on the next vmentry.
829 return apic_find_highest_irr(vcpu
->arch
.apic
);
831 EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr
);
833 static int __apic_accept_irq(struct kvm_lapic
*apic
, int delivery_mode
,
834 int vector
, int level
, int trig_mode
,
835 struct dest_map
*dest_map
);
837 int kvm_apic_set_irq(struct kvm_vcpu
*vcpu
, struct kvm_lapic_irq
*irq
,
838 struct dest_map
*dest_map
)
840 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
842 return __apic_accept_irq(apic
, irq
->delivery_mode
, irq
->vector
,
843 irq
->level
, irq
->trig_mode
, dest_map
);
846 static int __pv_send_ipi(unsigned long *ipi_bitmap
, struct kvm_apic_map
*map
,
847 struct kvm_lapic_irq
*irq
, u32 min
)
850 struct kvm_vcpu
*vcpu
;
852 if (min
> map
->max_apic_id
)
855 for_each_set_bit(i
, ipi_bitmap
,
856 min((u32
)BITS_PER_LONG
, (map
->max_apic_id
- min
+ 1))) {
857 if (map
->phys_map
[min
+ i
]) {
858 vcpu
= map
->phys_map
[min
+ i
]->vcpu
;
859 count
+= kvm_apic_set_irq(vcpu
, irq
, NULL
);
866 int kvm_pv_send_ipi(struct kvm
*kvm
, unsigned long ipi_bitmap_low
,
867 unsigned long ipi_bitmap_high
, u32 min
,
868 unsigned long icr
, int op_64_bit
)
870 struct kvm_apic_map
*map
;
871 struct kvm_lapic_irq irq
= {0};
872 int cluster_size
= op_64_bit
? 64 : 32;
875 if (icr
& (APIC_DEST_MASK
| APIC_SHORT_MASK
))
878 irq
.vector
= icr
& APIC_VECTOR_MASK
;
879 irq
.delivery_mode
= icr
& APIC_MODE_MASK
;
880 irq
.level
= (icr
& APIC_INT_ASSERT
) != 0;
881 irq
.trig_mode
= icr
& APIC_INT_LEVELTRIG
;
884 map
= rcu_dereference(kvm
->arch
.apic_map
);
888 count
= __pv_send_ipi(&ipi_bitmap_low
, map
, &irq
, min
);
890 count
+= __pv_send_ipi(&ipi_bitmap_high
, map
, &irq
, min
);
897 static int pv_eoi_put_user(struct kvm_vcpu
*vcpu
, u8 val
)
900 return kvm_write_guest_cached(vcpu
->kvm
, &vcpu
->arch
.pv_eoi
.data
, &val
,
904 static int pv_eoi_get_user(struct kvm_vcpu
*vcpu
, u8
*val
)
907 return kvm_read_guest_cached(vcpu
->kvm
, &vcpu
->arch
.pv_eoi
.data
, val
,
911 static inline bool pv_eoi_enabled(struct kvm_vcpu
*vcpu
)
913 return vcpu
->arch
.pv_eoi
.msr_val
& KVM_MSR_ENABLED
;
916 static void pv_eoi_set_pending(struct kvm_vcpu
*vcpu
)
918 if (pv_eoi_put_user(vcpu
, KVM_PV_EOI_ENABLED
) < 0)
921 __set_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
);
924 static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu
*vcpu
)
928 if (pv_eoi_get_user(vcpu
, &val
) < 0)
931 val
&= KVM_PV_EOI_ENABLED
;
933 if (val
&& pv_eoi_put_user(vcpu
, KVM_PV_EOI_DISABLED
) < 0)
937 * Clear pending bit in any case: it will be set again on vmentry.
938 * While this might not be ideal from performance point of view,
939 * this makes sure pv eoi is only enabled when we know it's safe.
941 __clear_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
);
946 static int apic_has_interrupt_for_ppr(struct kvm_lapic
*apic
, u32 ppr
)
949 if (kvm_x86_ops
.sync_pir_to_irr
)
950 highest_irr
= kvm_x86_call(sync_pir_to_irr
)(apic
->vcpu
);
952 highest_irr
= apic_find_highest_irr(apic
);
953 if (highest_irr
== -1 || (highest_irr
& 0xF0) <= ppr
)
958 static bool __apic_update_ppr(struct kvm_lapic
*apic
, u32
*new_ppr
)
960 u32 tpr
, isrv
, ppr
, old_ppr
;
963 old_ppr
= kvm_lapic_get_reg(apic
, APIC_PROCPRI
);
964 tpr
= kvm_lapic_get_reg(apic
, APIC_TASKPRI
);
965 isr
= apic_find_highest_isr(apic
);
966 isrv
= (isr
!= -1) ? isr
: 0;
968 if ((tpr
& 0xf0) >= (isrv
& 0xf0))
975 kvm_lapic_set_reg(apic
, APIC_PROCPRI
, ppr
);
977 return ppr
< old_ppr
;
980 static void apic_update_ppr(struct kvm_lapic
*apic
)
984 if (__apic_update_ppr(apic
, &ppr
) &&
985 apic_has_interrupt_for_ppr(apic
, ppr
) != -1)
986 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
989 void kvm_apic_update_ppr(struct kvm_vcpu
*vcpu
)
991 apic_update_ppr(vcpu
->arch
.apic
);
993 EXPORT_SYMBOL_GPL(kvm_apic_update_ppr
);
995 static void apic_set_tpr(struct kvm_lapic
*apic
, u32 tpr
)
997 kvm_lapic_set_reg(apic
, APIC_TASKPRI
, tpr
);
998 apic_update_ppr(apic
);
1001 static bool kvm_apic_broadcast(struct kvm_lapic
*apic
, u32 mda
)
1003 return mda
== (apic_x2apic_mode(apic
) ?
1004 X2APIC_BROADCAST
: APIC_BROADCAST
);
1007 static bool kvm_apic_match_physical_addr(struct kvm_lapic
*apic
, u32 mda
)
1009 if (kvm_apic_broadcast(apic
, mda
))
1013 * Hotplug hack: Accept interrupts for vCPUs in xAPIC mode as if they
1014 * were in x2APIC mode if the target APIC ID can't be encoded as an
1015 * xAPIC ID. This allows unique addressing of hotplugged vCPUs (which
1016 * start in xAPIC mode) with an APIC ID that is unaddressable in xAPIC
1017 * mode. Match the x2APIC ID if and only if the target APIC ID can't
1018 * be encoded in xAPIC to avoid spurious matches against a vCPU that
1019 * changed its (addressable) xAPIC ID (which is writable).
1021 if (apic_x2apic_mode(apic
) || mda
> 0xff)
1022 return mda
== kvm_x2apic_id(apic
);
1024 return mda
== kvm_xapic_id(apic
);
1027 static bool kvm_apic_match_logical_addr(struct kvm_lapic
*apic
, u32 mda
)
1031 if (kvm_apic_broadcast(apic
, mda
))
1034 logical_id
= kvm_lapic_get_reg(apic
, APIC_LDR
);
1036 if (apic_x2apic_mode(apic
))
1037 return ((logical_id
>> 16) == (mda
>> 16))
1038 && (logical_id
& mda
& 0xffff) != 0;
1040 logical_id
= GET_APIC_LOGICAL_ID(logical_id
);
1042 switch (kvm_lapic_get_reg(apic
, APIC_DFR
)) {
1044 return (logical_id
& mda
) != 0;
1045 case APIC_DFR_CLUSTER
:
1046 return ((logical_id
>> 4) == (mda
>> 4))
1047 && (logical_id
& mda
& 0xf) != 0;
1053 /* The KVM local APIC implementation has two quirks:
1055 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
1056 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
1057 * KVM doesn't do that aliasing.
1059 * - in-kernel IOAPIC messages have to be delivered directly to
1060 * x2APIC, because the kernel does not support interrupt remapping.
1061 * In order to support broadcast without interrupt remapping, x2APIC
1062 * rewrites the destination of non-IPI messages from APIC_BROADCAST
1063 * to X2APIC_BROADCAST.
1065 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
1066 * important when userspace wants to use x2APIC-format MSIs, because
1067 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
1069 static u32
kvm_apic_mda(struct kvm_vcpu
*vcpu
, unsigned int dest_id
,
1070 struct kvm_lapic
*source
, struct kvm_lapic
*target
)
1072 bool ipi
= source
!= NULL
;
1074 if (!vcpu
->kvm
->arch
.x2apic_broadcast_quirk_disabled
&&
1075 !ipi
&& dest_id
== APIC_BROADCAST
&& apic_x2apic_mode(target
))
1076 return X2APIC_BROADCAST
;
1081 bool kvm_apic_match_dest(struct kvm_vcpu
*vcpu
, struct kvm_lapic
*source
,
1082 int shorthand
, unsigned int dest
, int dest_mode
)
1084 struct kvm_lapic
*target
= vcpu
->arch
.apic
;
1085 u32 mda
= kvm_apic_mda(vcpu
, dest
, source
, target
);
1088 switch (shorthand
) {
1089 case APIC_DEST_NOSHORT
:
1090 if (dest_mode
== APIC_DEST_PHYSICAL
)
1091 return kvm_apic_match_physical_addr(target
, mda
);
1093 return kvm_apic_match_logical_addr(target
, mda
);
1094 case APIC_DEST_SELF
:
1095 return target
== source
;
1096 case APIC_DEST_ALLINC
:
1098 case APIC_DEST_ALLBUT
:
1099 return target
!= source
;
1104 EXPORT_SYMBOL_GPL(kvm_apic_match_dest
);
1106 int kvm_vector_to_index(u32 vector
, u32 dest_vcpus
,
1107 const unsigned long *bitmap
, u32 bitmap_size
)
1112 mod
= vector
% dest_vcpus
;
1114 for (i
= 0; i
<= mod
; i
++) {
1115 idx
= find_next_bit(bitmap
, bitmap_size
, idx
+ 1);
1116 BUG_ON(idx
== bitmap_size
);
1122 static void kvm_apic_disabled_lapic_found(struct kvm
*kvm
)
1124 if (!kvm
->arch
.disabled_lapic_found
) {
1125 kvm
->arch
.disabled_lapic_found
= true;
1126 pr_info("Disabled LAPIC found during irq injection\n");
1130 static bool kvm_apic_is_broadcast_dest(struct kvm
*kvm
, struct kvm_lapic
**src
,
1131 struct kvm_lapic_irq
*irq
, struct kvm_apic_map
*map
)
1133 if (kvm
->arch
.x2apic_broadcast_quirk_disabled
) {
1134 if ((irq
->dest_id
== APIC_BROADCAST
&&
1135 map
->logical_mode
!= KVM_APIC_MODE_X2APIC
))
1137 if (irq
->dest_id
== X2APIC_BROADCAST
)
1140 bool x2apic_ipi
= src
&& *src
&& apic_x2apic_mode(*src
);
1141 if (irq
->dest_id
== (x2apic_ipi
?
1142 X2APIC_BROADCAST
: APIC_BROADCAST
))
1149 /* Return true if the interrupt can be handled by using *bitmap as index mask
1150 * for valid destinations in *dst array.
1151 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
1152 * Note: we may have zero kvm_lapic destinations when we return true, which
1153 * means that the interrupt should be dropped. In this case, *bitmap would be
1154 * zero and *dst undefined.
1156 static inline bool kvm_apic_map_get_dest_lapic(struct kvm
*kvm
,
1157 struct kvm_lapic
**src
, struct kvm_lapic_irq
*irq
,
1158 struct kvm_apic_map
*map
, struct kvm_lapic
***dst
,
1159 unsigned long *bitmap
)
1163 if (irq
->shorthand
== APIC_DEST_SELF
&& src
) {
1167 } else if (irq
->shorthand
)
1170 if (!map
|| kvm_apic_is_broadcast_dest(kvm
, src
, irq
, map
))
1173 if (irq
->dest_mode
== APIC_DEST_PHYSICAL
) {
1174 if (irq
->dest_id
> map
->max_apic_id
) {
1177 u32 dest_id
= array_index_nospec(irq
->dest_id
, map
->max_apic_id
+ 1);
1178 *dst
= &map
->phys_map
[dest_id
];
1185 if (!kvm_apic_map_get_logical_dest(map
, irq
->dest_id
, dst
,
1189 if (!kvm_lowest_prio_delivery(irq
))
1192 if (!kvm_vector_hashing_enabled()) {
1194 for_each_set_bit(i
, bitmap
, 16) {
1199 else if (kvm_apic_compare_prio((*dst
)[i
]->vcpu
,
1200 (*dst
)[lowest
]->vcpu
) < 0)
1207 lowest
= kvm_vector_to_index(irq
->vector
, hweight16(*bitmap
),
1210 if (!(*dst
)[lowest
]) {
1211 kvm_apic_disabled_lapic_found(kvm
);
1217 *bitmap
= (lowest
>= 0) ? 1 << lowest
: 0;
1222 bool kvm_irq_delivery_to_apic_fast(struct kvm
*kvm
, struct kvm_lapic
*src
,
1223 struct kvm_lapic_irq
*irq
, int *r
, struct dest_map
*dest_map
)
1225 struct kvm_apic_map
*map
;
1226 unsigned long bitmap
;
1227 struct kvm_lapic
**dst
= NULL
;
1233 if (irq
->shorthand
== APIC_DEST_SELF
) {
1234 if (KVM_BUG_ON(!src
, kvm
)) {
1238 *r
= kvm_apic_set_irq(src
->vcpu
, irq
, dest_map
);
1243 map
= rcu_dereference(kvm
->arch
.apic_map
);
1245 ret
= kvm_apic_map_get_dest_lapic(kvm
, &src
, irq
, map
, &dst
, &bitmap
);
1248 for_each_set_bit(i
, &bitmap
, 16) {
1251 *r
+= kvm_apic_set_irq(dst
[i
]->vcpu
, irq
, dest_map
);
1260 * This routine tries to handle interrupts in posted mode, here is how
1261 * it deals with different cases:
1262 * - For single-destination interrupts, handle it in posted mode
1263 * - Else if vector hashing is enabled and it is a lowest-priority
1264 * interrupt, handle it in posted mode and use the following mechanism
1265 * to find the destination vCPU.
1266 * 1. For lowest-priority interrupts, store all the possible
1267 * destination vCPUs in an array.
1268 * 2. Use "guest vector % max number of destination vCPUs" to find
1269 * the right destination vCPU in the array for the lowest-priority
1271 * - Otherwise, use remapped mode to inject the interrupt.
1273 bool kvm_intr_is_single_vcpu_fast(struct kvm
*kvm
, struct kvm_lapic_irq
*irq
,
1274 struct kvm_vcpu
**dest_vcpu
)
1276 struct kvm_apic_map
*map
;
1277 unsigned long bitmap
;
1278 struct kvm_lapic
**dst
= NULL
;
1285 map
= rcu_dereference(kvm
->arch
.apic_map
);
1287 if (kvm_apic_map_get_dest_lapic(kvm
, NULL
, irq
, map
, &dst
, &bitmap
) &&
1288 hweight16(bitmap
) == 1) {
1289 unsigned long i
= find_first_bit(&bitmap
, 16);
1292 *dest_vcpu
= dst
[i
]->vcpu
;
1302 * Add a pending IRQ into lapic.
1303 * Return 1 if successfully added and 0 if discarded.
1305 static int __apic_accept_irq(struct kvm_lapic
*apic
, int delivery_mode
,
1306 int vector
, int level
, int trig_mode
,
1307 struct dest_map
*dest_map
)
1310 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1312 trace_kvm_apic_accept_irq(vcpu
->vcpu_id
, delivery_mode
,
1314 switch (delivery_mode
) {
1315 case APIC_DM_LOWEST
:
1316 vcpu
->arch
.apic_arb_prio
++;
1319 if (unlikely(trig_mode
&& !level
))
1322 /* FIXME add logic for vcpu on reset */
1323 if (unlikely(!apic_enabled(apic
)))
1329 __set_bit(vcpu
->vcpu_id
, dest_map
->map
);
1330 dest_map
->vectors
[vcpu
->vcpu_id
] = vector
;
1333 if (apic_test_vector(vector
, apic
->regs
+ APIC_TMR
) != !!trig_mode
) {
1335 kvm_lapic_set_vector(vector
,
1336 apic
->regs
+ APIC_TMR
);
1338 kvm_lapic_clear_vector(vector
,
1339 apic
->regs
+ APIC_TMR
);
1342 kvm_x86_call(deliver_interrupt
)(apic
, delivery_mode
,
1348 vcpu
->arch
.pv
.pv_unhalted
= 1;
1349 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
1350 kvm_vcpu_kick(vcpu
);
1354 if (!kvm_inject_smi(vcpu
)) {
1355 kvm_vcpu_kick(vcpu
);
1362 kvm_inject_nmi(vcpu
);
1363 kvm_vcpu_kick(vcpu
);
1367 if (!trig_mode
|| level
) {
1369 /* assumes that there are only KVM_APIC_INIT/SIPI */
1370 apic
->pending_events
= (1UL << KVM_APIC_INIT
);
1371 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
1372 kvm_vcpu_kick(vcpu
);
1376 case APIC_DM_STARTUP
:
1378 apic
->sipi_vector
= vector
;
1379 /* make sure sipi_vector is visible for the receiver */
1381 set_bit(KVM_APIC_SIPI
, &apic
->pending_events
);
1382 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
1383 kvm_vcpu_kick(vcpu
);
1386 case APIC_DM_EXTINT
:
1388 * Should only be called by kvm_apic_local_deliver() with LVT0,
1389 * before NMI watchdog was enabled. Already handled by
1390 * kvm_apic_accept_pic_intr().
1395 printk(KERN_ERR
"TODO: unsupported delivery mode %x\n",
1403 * This routine identifies the destination vcpus mask meant to receive the
1404 * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find
1405 * out the destination vcpus array and set the bitmap or it traverses to
1406 * each available vcpu to identify the same.
1408 void kvm_bitmap_or_dest_vcpus(struct kvm
*kvm
, struct kvm_lapic_irq
*irq
,
1409 unsigned long *vcpu_bitmap
)
1411 struct kvm_lapic
**dest_vcpu
= NULL
;
1412 struct kvm_lapic
*src
= NULL
;
1413 struct kvm_apic_map
*map
;
1414 struct kvm_vcpu
*vcpu
;
1415 unsigned long bitmap
, i
;
1420 map
= rcu_dereference(kvm
->arch
.apic_map
);
1422 ret
= kvm_apic_map_get_dest_lapic(kvm
, &src
, irq
, map
, &dest_vcpu
,
1425 for_each_set_bit(i
, &bitmap
, 16) {
1428 vcpu_idx
= dest_vcpu
[i
]->vcpu
->vcpu_idx
;
1429 __set_bit(vcpu_idx
, vcpu_bitmap
);
1432 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1433 if (!kvm_apic_present(vcpu
))
1435 if (!kvm_apic_match_dest(vcpu
, NULL
,
1440 __set_bit(i
, vcpu_bitmap
);
1446 int kvm_apic_compare_prio(struct kvm_vcpu
*vcpu1
, struct kvm_vcpu
*vcpu2
)
1448 return vcpu1
->arch
.apic_arb_prio
- vcpu2
->arch
.apic_arb_prio
;
1451 static bool kvm_ioapic_handles_vector(struct kvm_lapic
*apic
, int vector
)
1453 return test_bit(vector
, apic
->vcpu
->arch
.ioapic_handled_vectors
);
1456 static void kvm_ioapic_send_eoi(struct kvm_lapic
*apic
, int vector
)
1460 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1461 if (!kvm_ioapic_handles_vector(apic
, vector
))
1465 * If the intercepted EOI is for an IRQ that was pending from previous
1466 * routing, then re-scan the I/O APIC routes as EOIs for the IRQ likely
1467 * no longer need to be intercepted.
1469 if (apic
->vcpu
->arch
.highest_stale_pending_ioapic_eoi
== vector
)
1470 kvm_make_request(KVM_REQ_SCAN_IOAPIC
, apic
->vcpu
);
1472 /* Request a KVM exit to inform the userspace IOAPIC. */
1473 if (irqchip_split(apic
->vcpu
->kvm
)) {
1474 apic
->vcpu
->arch
.pending_ioapic_eoi
= vector
;
1475 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT
, apic
->vcpu
);
1479 if (apic_test_vector(vector
, apic
->regs
+ APIC_TMR
))
1480 trigger_mode
= IOAPIC_LEVEL_TRIG
;
1482 trigger_mode
= IOAPIC_EDGE_TRIG
;
1484 kvm_ioapic_update_eoi(apic
->vcpu
, vector
, trigger_mode
);
1487 static int apic_set_eoi(struct kvm_lapic
*apic
)
1489 int vector
= apic_find_highest_isr(apic
);
1491 trace_kvm_eoi(apic
, vector
);
1494 * Not every write EOI will has corresponding ISR,
1495 * one example is when Kernel check timer on setup_IO_APIC
1500 apic_clear_isr(vector
, apic
);
1501 apic_update_ppr(apic
);
1503 if (kvm_hv_synic_has_vector(apic
->vcpu
, vector
))
1504 kvm_hv_synic_send_eoi(apic
->vcpu
, vector
);
1506 kvm_ioapic_send_eoi(apic
, vector
);
1507 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
1512 * this interface assumes a trap-like exit, which has already finished
1513 * desired side effect including vISR and vPPR update.
1515 void kvm_apic_set_eoi_accelerated(struct kvm_vcpu
*vcpu
, int vector
)
1517 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1519 trace_kvm_eoi(apic
, vector
);
1521 kvm_ioapic_send_eoi(apic
, vector
);
1522 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
1524 EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated
);
1526 void kvm_apic_send_ipi(struct kvm_lapic
*apic
, u32 icr_low
, u32 icr_high
)
1528 struct kvm_lapic_irq irq
;
1530 /* KVM has no delay and should always clear the BUSY/PENDING flag. */
1531 WARN_ON_ONCE(icr_low
& APIC_ICR_BUSY
);
1533 irq
.vector
= icr_low
& APIC_VECTOR_MASK
;
1534 irq
.delivery_mode
= icr_low
& APIC_MODE_MASK
;
1535 irq
.dest_mode
= icr_low
& APIC_DEST_MASK
;
1536 irq
.level
= (icr_low
& APIC_INT_ASSERT
) != 0;
1537 irq
.trig_mode
= icr_low
& APIC_INT_LEVELTRIG
;
1538 irq
.shorthand
= icr_low
& APIC_SHORT_MASK
;
1539 irq
.msi_redir_hint
= false;
1540 if (apic_x2apic_mode(apic
))
1541 irq
.dest_id
= icr_high
;
1543 irq
.dest_id
= GET_XAPIC_DEST_FIELD(icr_high
);
1545 trace_kvm_apic_ipi(icr_low
, irq
.dest_id
);
1547 kvm_irq_delivery_to_apic(apic
->vcpu
->kvm
, apic
, &irq
, NULL
);
1549 EXPORT_SYMBOL_GPL(kvm_apic_send_ipi
);
1551 static u32
apic_get_tmcct(struct kvm_lapic
*apic
)
1553 ktime_t remaining
, now
;
1556 ASSERT(apic
!= NULL
);
1558 /* if initial count is 0, current count should also be 0 */
1559 if (kvm_lapic_get_reg(apic
, APIC_TMICT
) == 0 ||
1560 apic
->lapic_timer
.period
== 0)
1564 remaining
= ktime_sub(apic
->lapic_timer
.target_expiration
, now
);
1565 if (ktime_to_ns(remaining
) < 0)
1568 ns
= mod_64(ktime_to_ns(remaining
), apic
->lapic_timer
.period
);
1569 return div64_u64(ns
, (apic
->vcpu
->kvm
->arch
.apic_bus_cycle_ns
*
1570 apic
->divide_count
));
1573 static void __report_tpr_access(struct kvm_lapic
*apic
, bool write
)
1575 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1576 struct kvm_run
*run
= vcpu
->run
;
1578 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS
, vcpu
);
1579 run
->tpr_access
.rip
= kvm_rip_read(vcpu
);
1580 run
->tpr_access
.is_write
= write
;
1583 static inline void report_tpr_access(struct kvm_lapic
*apic
, bool write
)
1585 if (apic
->vcpu
->arch
.tpr_access_reporting
)
1586 __report_tpr_access(apic
, write
);
1589 static u32
__apic_read(struct kvm_lapic
*apic
, unsigned int offset
)
1593 if (offset
>= LAPIC_MMIO_LENGTH
)
1600 case APIC_TMCCT
: /* Timer CCR */
1601 if (apic_lvtt_tscdeadline(apic
))
1604 val
= apic_get_tmcct(apic
);
1607 apic_update_ppr(apic
);
1608 val
= kvm_lapic_get_reg(apic
, offset
);
1611 report_tpr_access(apic
, false);
1614 val
= kvm_lapic_get_reg(apic
, offset
);
1621 static inline struct kvm_lapic
*to_lapic(struct kvm_io_device
*dev
)
1623 return container_of(dev
, struct kvm_lapic
, dev
);
1626 #define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1627 #define APIC_REGS_MASK(first, count) \
1628 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1630 u64
kvm_lapic_readable_reg_mask(struct kvm_lapic
*apic
)
1632 /* Leave bits '0' for reserved and write-only registers. */
1633 u64 valid_reg_mask
=
1634 APIC_REG_MASK(APIC_ID
) |
1635 APIC_REG_MASK(APIC_LVR
) |
1636 APIC_REG_MASK(APIC_TASKPRI
) |
1637 APIC_REG_MASK(APIC_PROCPRI
) |
1638 APIC_REG_MASK(APIC_LDR
) |
1639 APIC_REG_MASK(APIC_SPIV
) |
1640 APIC_REGS_MASK(APIC_ISR
, APIC_ISR_NR
) |
1641 APIC_REGS_MASK(APIC_TMR
, APIC_ISR_NR
) |
1642 APIC_REGS_MASK(APIC_IRR
, APIC_ISR_NR
) |
1643 APIC_REG_MASK(APIC_ESR
) |
1644 APIC_REG_MASK(APIC_ICR
) |
1645 APIC_REG_MASK(APIC_LVTT
) |
1646 APIC_REG_MASK(APIC_LVTTHMR
) |
1647 APIC_REG_MASK(APIC_LVTPC
) |
1648 APIC_REG_MASK(APIC_LVT0
) |
1649 APIC_REG_MASK(APIC_LVT1
) |
1650 APIC_REG_MASK(APIC_LVTERR
) |
1651 APIC_REG_MASK(APIC_TMICT
) |
1652 APIC_REG_MASK(APIC_TMCCT
) |
1653 APIC_REG_MASK(APIC_TDCR
);
1655 if (kvm_lapic_lvt_supported(apic
, LVT_CMCI
))
1656 valid_reg_mask
|= APIC_REG_MASK(APIC_LVTCMCI
);
1658 /* ARBPRI, DFR, and ICR2 are not valid in x2APIC mode. */
1659 if (!apic_x2apic_mode(apic
))
1660 valid_reg_mask
|= APIC_REG_MASK(APIC_ARBPRI
) |
1661 APIC_REG_MASK(APIC_DFR
) |
1662 APIC_REG_MASK(APIC_ICR2
);
1664 return valid_reg_mask
;
1666 EXPORT_SYMBOL_GPL(kvm_lapic_readable_reg_mask
);
1668 static int kvm_lapic_reg_read(struct kvm_lapic
*apic
, u32 offset
, int len
,
1671 unsigned char alignment
= offset
& 0xf;
1675 * WARN if KVM reads ICR in x2APIC mode, as it's an 8-byte register in
1676 * x2APIC and needs to be manually handled by the caller.
1678 WARN_ON_ONCE(apic_x2apic_mode(apic
) && offset
== APIC_ICR
);
1680 if (alignment
+ len
> 4)
1683 if (offset
> 0x3f0 ||
1684 !(kvm_lapic_readable_reg_mask(apic
) & APIC_REG_MASK(offset
)))
1687 result
= __apic_read(apic
, offset
& ~0xf);
1689 trace_kvm_apic_read(offset
, result
);
1695 memcpy(data
, (char *)&result
+ alignment
, len
);
1698 printk(KERN_ERR
"Local APIC read with len = %x, "
1699 "should be 1,2, or 4 instead\n", len
);
1705 static int apic_mmio_in_range(struct kvm_lapic
*apic
, gpa_t addr
)
1707 return addr
>= apic
->base_address
&&
1708 addr
< apic
->base_address
+ LAPIC_MMIO_LENGTH
;
1711 static int apic_mmio_read(struct kvm_vcpu
*vcpu
, struct kvm_io_device
*this,
1712 gpa_t address
, int len
, void *data
)
1714 struct kvm_lapic
*apic
= to_lapic(this);
1715 u32 offset
= address
- apic
->base_address
;
1717 if (!apic_mmio_in_range(apic
, address
))
1720 if (!kvm_apic_hw_enabled(apic
) || apic_x2apic_mode(apic
)) {
1721 if (!kvm_check_has_quirk(vcpu
->kvm
,
1722 KVM_X86_QUIRK_LAPIC_MMIO_HOLE
))
1725 memset(data
, 0xff, len
);
1729 kvm_lapic_reg_read(apic
, offset
, len
, data
);
1734 static void update_divide_count(struct kvm_lapic
*apic
)
1736 u32 tmp1
, tmp2
, tdcr
;
1738 tdcr
= kvm_lapic_get_reg(apic
, APIC_TDCR
);
1740 tmp2
= ((tmp1
& 0x3) | ((tmp1
& 0x8) >> 1)) + 1;
1741 apic
->divide_count
= 0x1 << (tmp2
& 0x7);
1744 static void limit_periodic_timer_frequency(struct kvm_lapic
*apic
)
1747 * Do not allow the guest to program periodic timers with small
1748 * interval, since the hrtimers are not throttled by the host
1751 if (apic_lvtt_period(apic
) && apic
->lapic_timer
.period
) {
1752 s64 min_period
= min_timer_period_us
* 1000LL;
1754 if (apic
->lapic_timer
.period
< min_period
) {
1756 "vcpu %i: requested %lld ns "
1757 "lapic timer period limited to %lld ns\n",
1758 apic
->vcpu
->vcpu_id
,
1759 apic
->lapic_timer
.period
, min_period
);
1760 apic
->lapic_timer
.period
= min_period
;
1765 static void cancel_hv_timer(struct kvm_lapic
*apic
);
1767 static void cancel_apic_timer(struct kvm_lapic
*apic
)
1769 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1771 if (apic
->lapic_timer
.hv_timer_in_use
)
1772 cancel_hv_timer(apic
);
1774 atomic_set(&apic
->lapic_timer
.pending
, 0);
1777 static void apic_update_lvtt(struct kvm_lapic
*apic
)
1779 u32 timer_mode
= kvm_lapic_get_reg(apic
, APIC_LVTT
) &
1780 apic
->lapic_timer
.timer_mode_mask
;
1782 if (apic
->lapic_timer
.timer_mode
!= timer_mode
) {
1783 if (apic_lvtt_tscdeadline(apic
) != (timer_mode
==
1784 APIC_LVT_TIMER_TSCDEADLINE
)) {
1785 cancel_apic_timer(apic
);
1786 kvm_lapic_set_reg(apic
, APIC_TMICT
, 0);
1787 apic
->lapic_timer
.period
= 0;
1788 apic
->lapic_timer
.tscdeadline
= 0;
1790 apic
->lapic_timer
.timer_mode
= timer_mode
;
1791 limit_periodic_timer_frequency(apic
);
1796 * On APICv, this test will cause a busy wait
1797 * during a higher-priority task.
1800 static bool lapic_timer_int_injected(struct kvm_vcpu
*vcpu
)
1802 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1806 * Assume a timer IRQ was "injected" if the APIC is protected. KVM's
1807 * copy of the vIRR is bogus, it's the responsibility of the caller to
1808 * precisely check whether or not a timer IRQ is pending.
1810 if (apic
->guest_apic_protected
)
1813 reg
= kvm_lapic_get_reg(apic
, APIC_LVTT
);
1814 if (kvm_apic_hw_enabled(apic
)) {
1815 int vec
= reg
& APIC_VECTOR_MASK
;
1816 void *bitmap
= apic
->regs
+ APIC_ISR
;
1818 if (apic
->apicv_active
)
1819 bitmap
= apic
->regs
+ APIC_IRR
;
1821 if (apic_test_vector(vec
, bitmap
))
1827 static inline void __wait_lapic_expire(struct kvm_vcpu
*vcpu
, u64 guest_cycles
)
1829 u64 timer_advance_ns
= vcpu
->arch
.apic
->lapic_timer
.timer_advance_ns
;
1832 * If the guest TSC is running at a different ratio than the host, then
1833 * convert the delay to nanoseconds to achieve an accurate delay. Note
1834 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1835 * always for VMX enabled hardware.
1837 if (vcpu
->arch
.tsc_scaling_ratio
== kvm_caps
.default_tsc_scaling_ratio
) {
1838 __delay(min(guest_cycles
,
1839 nsec_to_cycles(vcpu
, timer_advance_ns
)));
1841 u64 delay_ns
= guest_cycles
* 1000000ULL;
1842 do_div(delay_ns
, vcpu
->arch
.virtual_tsc_khz
);
1843 ndelay(min_t(u32
, delay_ns
, timer_advance_ns
));
1847 static inline void adjust_lapic_timer_advance(struct kvm_vcpu
*vcpu
,
1848 s64 advance_expire_delta
)
1850 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1851 u32 timer_advance_ns
= apic
->lapic_timer
.timer_advance_ns
;
1854 /* Do not adjust for tiny fluctuations or large random spikes. */
1855 if (abs(advance_expire_delta
) > LAPIC_TIMER_ADVANCE_ADJUST_MAX
||
1856 abs(advance_expire_delta
) < LAPIC_TIMER_ADVANCE_ADJUST_MIN
)
1860 if (advance_expire_delta
< 0) {
1861 ns
= -advance_expire_delta
* 1000000ULL;
1862 do_div(ns
, vcpu
->arch
.virtual_tsc_khz
);
1863 timer_advance_ns
-= ns
/LAPIC_TIMER_ADVANCE_ADJUST_STEP
;
1866 ns
= advance_expire_delta
* 1000000ULL;
1867 do_div(ns
, vcpu
->arch
.virtual_tsc_khz
);
1868 timer_advance_ns
+= ns
/LAPIC_TIMER_ADVANCE_ADJUST_STEP
;
1871 if (unlikely(timer_advance_ns
> LAPIC_TIMER_ADVANCE_NS_MAX
))
1872 timer_advance_ns
= LAPIC_TIMER_ADVANCE_NS_INIT
;
1873 apic
->lapic_timer
.timer_advance_ns
= timer_advance_ns
;
1876 static void __kvm_wait_lapic_expire(struct kvm_vcpu
*vcpu
)
1878 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1879 u64 guest_tsc
, tsc_deadline
;
1881 tsc_deadline
= apic
->lapic_timer
.expired_tscdeadline
;
1882 apic
->lapic_timer
.expired_tscdeadline
= 0;
1883 guest_tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
1884 trace_kvm_wait_lapic_expire(vcpu
->vcpu_id
, guest_tsc
- tsc_deadline
);
1886 adjust_lapic_timer_advance(vcpu
, guest_tsc
- tsc_deadline
);
1889 * If the timer fired early, reread the TSC to account for the overhead
1890 * of the above adjustment to avoid waiting longer than is necessary.
1892 if (guest_tsc
< tsc_deadline
)
1893 guest_tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
1895 if (guest_tsc
< tsc_deadline
)
1896 __wait_lapic_expire(vcpu
, tsc_deadline
- guest_tsc
);
1899 void kvm_wait_lapic_expire(struct kvm_vcpu
*vcpu
)
1901 if (lapic_in_kernel(vcpu
) &&
1902 vcpu
->arch
.apic
->lapic_timer
.expired_tscdeadline
&&
1903 vcpu
->arch
.apic
->lapic_timer
.timer_advance_ns
&&
1904 lapic_timer_int_injected(vcpu
))
1905 __kvm_wait_lapic_expire(vcpu
);
1907 EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire
);
1909 static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic
*apic
)
1911 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
1913 kvm_apic_local_deliver(apic
, APIC_LVTT
);
1914 if (apic_lvtt_tscdeadline(apic
)) {
1915 ktimer
->tscdeadline
= 0;
1916 } else if (apic_lvtt_oneshot(apic
)) {
1917 ktimer
->tscdeadline
= 0;
1918 ktimer
->target_expiration
= 0;
1922 static void apic_timer_expired(struct kvm_lapic
*apic
, bool from_timer_fn
)
1924 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1925 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
1927 if (atomic_read(&apic
->lapic_timer
.pending
))
1930 if (apic_lvtt_tscdeadline(apic
) || ktimer
->hv_timer_in_use
)
1931 ktimer
->expired_tscdeadline
= ktimer
->tscdeadline
;
1933 if (!from_timer_fn
&& apic
->apicv_active
) {
1934 WARN_ON(kvm_get_running_vcpu() != vcpu
);
1935 kvm_apic_inject_pending_timer_irqs(apic
);
1939 if (kvm_use_posted_timer_interrupt(apic
->vcpu
)) {
1941 * Ensure the guest's timer has truly expired before posting an
1942 * interrupt. Open code the relevant checks to avoid querying
1943 * lapic_timer_int_injected(), which will be false since the
1944 * interrupt isn't yet injected. Waiting until after injecting
1945 * is not an option since that won't help a posted interrupt.
1947 if (vcpu
->arch
.apic
->lapic_timer
.expired_tscdeadline
&&
1948 vcpu
->arch
.apic
->lapic_timer
.timer_advance_ns
)
1949 __kvm_wait_lapic_expire(vcpu
);
1950 kvm_apic_inject_pending_timer_irqs(apic
);
1954 atomic_inc(&apic
->lapic_timer
.pending
);
1955 kvm_make_request(KVM_REQ_UNBLOCK
, vcpu
);
1957 kvm_vcpu_kick(vcpu
);
1960 static void start_sw_tscdeadline(struct kvm_lapic
*apic
)
1962 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
1963 u64 guest_tsc
, tscdeadline
= ktimer
->tscdeadline
;
1966 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1967 u32 this_tsc_khz
= vcpu
->arch
.virtual_tsc_khz
;
1968 unsigned long flags
;
1971 if (unlikely(!tscdeadline
|| !this_tsc_khz
))
1974 local_irq_save(flags
);
1977 guest_tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
1979 ns
= (tscdeadline
- guest_tsc
) * 1000000ULL;
1980 do_div(ns
, this_tsc_khz
);
1982 if (likely(tscdeadline
> guest_tsc
) &&
1983 likely(ns
> apic
->lapic_timer
.timer_advance_ns
)) {
1984 expire
= ktime_add_ns(now
, ns
);
1985 expire
= ktime_sub_ns(expire
, ktimer
->timer_advance_ns
);
1986 hrtimer_start(&ktimer
->timer
, expire
, HRTIMER_MODE_ABS_HARD
);
1988 apic_timer_expired(apic
, false);
1990 local_irq_restore(flags
);
1993 static inline u64
tmict_to_ns(struct kvm_lapic
*apic
, u32 tmict
)
1995 return (u64
)tmict
* apic
->vcpu
->kvm
->arch
.apic_bus_cycle_ns
*
1996 (u64
)apic
->divide_count
;
1999 static void update_target_expiration(struct kvm_lapic
*apic
, uint32_t old_divisor
)
2001 ktime_t now
, remaining
;
2002 u64 ns_remaining_old
, ns_remaining_new
;
2004 apic
->lapic_timer
.period
=
2005 tmict_to_ns(apic
, kvm_lapic_get_reg(apic
, APIC_TMICT
));
2006 limit_periodic_timer_frequency(apic
);
2009 remaining
= ktime_sub(apic
->lapic_timer
.target_expiration
, now
);
2010 if (ktime_to_ns(remaining
) < 0)
2013 ns_remaining_old
= ktime_to_ns(remaining
);
2014 ns_remaining_new
= mul_u64_u32_div(ns_remaining_old
,
2015 apic
->divide_count
, old_divisor
);
2017 apic
->lapic_timer
.tscdeadline
+=
2018 nsec_to_cycles(apic
->vcpu
, ns_remaining_new
) -
2019 nsec_to_cycles(apic
->vcpu
, ns_remaining_old
);
2020 apic
->lapic_timer
.target_expiration
= ktime_add_ns(now
, ns_remaining_new
);
2023 static bool set_target_expiration(struct kvm_lapic
*apic
, u32 count_reg
)
2030 apic
->lapic_timer
.period
=
2031 tmict_to_ns(apic
, kvm_lapic_get_reg(apic
, APIC_TMICT
));
2033 if (!apic
->lapic_timer
.period
) {
2034 apic
->lapic_timer
.tscdeadline
= 0;
2038 limit_periodic_timer_frequency(apic
);
2039 deadline
= apic
->lapic_timer
.period
;
2041 if (apic_lvtt_period(apic
) || apic_lvtt_oneshot(apic
)) {
2042 if (unlikely(count_reg
!= APIC_TMICT
)) {
2043 deadline
= tmict_to_ns(apic
,
2044 kvm_lapic_get_reg(apic
, count_reg
));
2045 if (unlikely(deadline
<= 0)) {
2046 if (apic_lvtt_period(apic
))
2047 deadline
= apic
->lapic_timer
.period
;
2051 else if (unlikely(deadline
> apic
->lapic_timer
.period
)) {
2052 pr_info_ratelimited(
2053 "vcpu %i: requested lapic timer restore with "
2054 "starting count register %#x=%u (%lld ns) > initial count (%lld ns). "
2055 "Using initial count to start timer.\n",
2056 apic
->vcpu
->vcpu_id
,
2058 kvm_lapic_get_reg(apic
, count_reg
),
2059 deadline
, apic
->lapic_timer
.period
);
2060 kvm_lapic_set_reg(apic
, count_reg
, 0);
2061 deadline
= apic
->lapic_timer
.period
;
2066 apic
->lapic_timer
.tscdeadline
= kvm_read_l1_tsc(apic
->vcpu
, tscl
) +
2067 nsec_to_cycles(apic
->vcpu
, deadline
);
2068 apic
->lapic_timer
.target_expiration
= ktime_add_ns(now
, deadline
);
2073 static void advance_periodic_target_expiration(struct kvm_lapic
*apic
)
2075 ktime_t now
= ktime_get();
2080 * Synchronize both deadlines to the same time source or
2081 * differences in the periods (caused by differences in the
2082 * underlying clocks or numerical approximation errors) will
2083 * cause the two to drift apart over time as the errors
2086 apic
->lapic_timer
.target_expiration
=
2087 ktime_add_ns(apic
->lapic_timer
.target_expiration
,
2088 apic
->lapic_timer
.period
);
2089 delta
= ktime_sub(apic
->lapic_timer
.target_expiration
, now
);
2090 apic
->lapic_timer
.tscdeadline
= kvm_read_l1_tsc(apic
->vcpu
, tscl
) +
2091 nsec_to_cycles(apic
->vcpu
, delta
);
2094 static void start_sw_period(struct kvm_lapic
*apic
)
2096 if (!apic
->lapic_timer
.period
)
2099 if (ktime_after(ktime_get(),
2100 apic
->lapic_timer
.target_expiration
)) {
2101 apic_timer_expired(apic
, false);
2103 if (apic_lvtt_oneshot(apic
))
2106 advance_periodic_target_expiration(apic
);
2109 hrtimer_start(&apic
->lapic_timer
.timer
,
2110 apic
->lapic_timer
.target_expiration
,
2111 HRTIMER_MODE_ABS_HARD
);
2114 bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu
*vcpu
)
2116 if (!lapic_in_kernel(vcpu
))
2119 return vcpu
->arch
.apic
->lapic_timer
.hv_timer_in_use
;
2122 static void cancel_hv_timer(struct kvm_lapic
*apic
)
2124 WARN_ON(preemptible());
2125 WARN_ON(!apic
->lapic_timer
.hv_timer_in_use
);
2126 kvm_x86_call(cancel_hv_timer
)(apic
->vcpu
);
2127 apic
->lapic_timer
.hv_timer_in_use
= false;
2130 static bool start_hv_timer(struct kvm_lapic
*apic
)
2132 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
2133 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
2136 WARN_ON(preemptible());
2137 if (!kvm_can_use_hv_timer(vcpu
))
2140 if (!ktimer
->tscdeadline
)
2143 if (kvm_x86_call(set_hv_timer
)(vcpu
, ktimer
->tscdeadline
, &expired
))
2146 ktimer
->hv_timer_in_use
= true;
2147 hrtimer_cancel(&ktimer
->timer
);
2150 * To simplify handling the periodic timer, leave the hv timer running
2151 * even if the deadline timer has expired, i.e. rely on the resulting
2152 * VM-Exit to recompute the periodic timer's target expiration.
2154 if (!apic_lvtt_period(apic
)) {
2156 * Cancel the hv timer if the sw timer fired while the hv timer
2157 * was being programmed, or if the hv timer itself expired.
2159 if (atomic_read(&ktimer
->pending
)) {
2160 cancel_hv_timer(apic
);
2161 } else if (expired
) {
2162 apic_timer_expired(apic
, false);
2163 cancel_hv_timer(apic
);
2167 trace_kvm_hv_timer_state(vcpu
->vcpu_id
, ktimer
->hv_timer_in_use
);
2172 static void start_sw_timer(struct kvm_lapic
*apic
)
2174 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
2176 WARN_ON(preemptible());
2177 if (apic
->lapic_timer
.hv_timer_in_use
)
2178 cancel_hv_timer(apic
);
2179 if (!apic_lvtt_period(apic
) && atomic_read(&ktimer
->pending
))
2182 if (apic_lvtt_period(apic
) || apic_lvtt_oneshot(apic
))
2183 start_sw_period(apic
);
2184 else if (apic_lvtt_tscdeadline(apic
))
2185 start_sw_tscdeadline(apic
);
2186 trace_kvm_hv_timer_state(apic
->vcpu
->vcpu_id
, false);
2189 static void restart_apic_timer(struct kvm_lapic
*apic
)
2193 if (!apic_lvtt_period(apic
) && atomic_read(&apic
->lapic_timer
.pending
))
2196 if (!start_hv_timer(apic
))
2197 start_sw_timer(apic
);
2202 void kvm_lapic_expired_hv_timer(struct kvm_vcpu
*vcpu
)
2204 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2207 /* If the preempt notifier has already run, it also called apic_timer_expired */
2208 if (!apic
->lapic_timer
.hv_timer_in_use
)
2210 WARN_ON(kvm_vcpu_is_blocking(vcpu
));
2211 apic_timer_expired(apic
, false);
2212 cancel_hv_timer(apic
);
2214 if (apic_lvtt_period(apic
) && apic
->lapic_timer
.period
) {
2215 advance_periodic_target_expiration(apic
);
2216 restart_apic_timer(apic
);
2221 EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer
);
2223 void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu
*vcpu
)
2225 restart_apic_timer(vcpu
->arch
.apic
);
2228 void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu
*vcpu
)
2230 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2233 /* Possibly the TSC deadline timer is not enabled yet */
2234 if (apic
->lapic_timer
.hv_timer_in_use
)
2235 start_sw_timer(apic
);
2239 void kvm_lapic_restart_hv_timer(struct kvm_vcpu
*vcpu
)
2241 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2243 WARN_ON(!apic
->lapic_timer
.hv_timer_in_use
);
2244 restart_apic_timer(apic
);
2247 static void __start_apic_timer(struct kvm_lapic
*apic
, u32 count_reg
)
2249 atomic_set(&apic
->lapic_timer
.pending
, 0);
2251 if ((apic_lvtt_period(apic
) || apic_lvtt_oneshot(apic
))
2252 && !set_target_expiration(apic
, count_reg
))
2255 restart_apic_timer(apic
);
2258 static void start_apic_timer(struct kvm_lapic
*apic
)
2260 __start_apic_timer(apic
, APIC_TMICT
);
2263 static void apic_manage_nmi_watchdog(struct kvm_lapic
*apic
, u32 lvt0_val
)
2265 bool lvt0_in_nmi_mode
= apic_lvt_nmi_mode(lvt0_val
);
2267 if (apic
->lvt0_in_nmi_mode
!= lvt0_in_nmi_mode
) {
2268 apic
->lvt0_in_nmi_mode
= lvt0_in_nmi_mode
;
2269 if (lvt0_in_nmi_mode
) {
2270 atomic_inc(&apic
->vcpu
->kvm
->arch
.vapics_in_nmi_mode
);
2272 atomic_dec(&apic
->vcpu
->kvm
->arch
.vapics_in_nmi_mode
);
2276 static int get_lvt_index(u32 reg
)
2278 if (reg
== APIC_LVTCMCI
)
2280 if (reg
< APIC_LVTT
|| reg
> APIC_LVTERR
)
2282 return array_index_nospec(
2283 (reg
- APIC_LVTT
) >> 4, KVM_APIC_MAX_NR_LVT_ENTRIES
);
2286 static int kvm_lapic_reg_write(struct kvm_lapic
*apic
, u32 reg
, u32 val
)
2290 trace_kvm_apic_write(reg
, val
);
2293 case APIC_ID
: /* Local APIC ID */
2294 if (!apic_x2apic_mode(apic
)) {
2295 kvm_apic_set_xapic_id(apic
, val
>> 24);
2302 report_tpr_access(apic
, true);
2303 apic_set_tpr(apic
, val
& 0xff);
2311 if (!apic_x2apic_mode(apic
))
2312 kvm_apic_set_ldr(apic
, val
& APIC_LDR_MASK
);
2318 if (!apic_x2apic_mode(apic
))
2319 kvm_apic_set_dfr(apic
, val
| 0x0FFFFFFF);
2326 if (kvm_lapic_get_reg(apic
, APIC_LVR
) & APIC_LVR_DIRECTED_EOI
)
2327 mask
|= APIC_SPIV_DIRECTED_EOI
;
2328 apic_set_spiv(apic
, val
& mask
);
2329 if (!(val
& APIC_SPIV_APIC_ENABLED
)) {
2332 for (i
= 0; i
< apic
->nr_lvt_entries
; i
++) {
2333 kvm_lapic_set_reg(apic
, APIC_LVTx(i
),
2334 kvm_lapic_get_reg(apic
, APIC_LVTx(i
)) | APIC_LVT_MASKED
);
2336 apic_update_lvtt(apic
);
2337 atomic_set(&apic
->lapic_timer
.pending
, 0);
2343 WARN_ON_ONCE(apic_x2apic_mode(apic
));
2345 /* No delay here, so we always clear the pending bit */
2346 val
&= ~APIC_ICR_BUSY
;
2347 kvm_apic_send_ipi(apic
, val
, kvm_lapic_get_reg(apic
, APIC_ICR2
));
2348 kvm_lapic_set_reg(apic
, APIC_ICR
, val
);
2351 if (apic_x2apic_mode(apic
))
2354 kvm_lapic_set_reg(apic
, APIC_ICR2
, val
& 0xff000000);
2358 apic_manage_nmi_watchdog(apic
, val
);
2364 case APIC_LVTCMCI
: {
2365 u32 index
= get_lvt_index(reg
);
2366 if (!kvm_lapic_lvt_supported(apic
, index
)) {
2370 if (!kvm_apic_sw_enabled(apic
))
2371 val
|= APIC_LVT_MASKED
;
2372 val
&= apic_lvt_mask
[index
];
2373 kvm_lapic_set_reg(apic
, reg
, val
);
2378 if (!kvm_apic_sw_enabled(apic
))
2379 val
|= APIC_LVT_MASKED
;
2380 val
&= (apic_lvt_mask
[LVT_TIMER
] | apic
->lapic_timer
.timer_mode_mask
);
2381 kvm_lapic_set_reg(apic
, APIC_LVTT
, val
);
2382 apic_update_lvtt(apic
);
2386 if (apic_lvtt_tscdeadline(apic
))
2389 cancel_apic_timer(apic
);
2390 kvm_lapic_set_reg(apic
, APIC_TMICT
, val
);
2391 start_apic_timer(apic
);
2395 uint32_t old_divisor
= apic
->divide_count
;
2397 kvm_lapic_set_reg(apic
, APIC_TDCR
, val
& 0xb);
2398 update_divide_count(apic
);
2399 if (apic
->divide_count
!= old_divisor
&&
2400 apic
->lapic_timer
.period
) {
2401 hrtimer_cancel(&apic
->lapic_timer
.timer
);
2402 update_target_expiration(apic
, old_divisor
);
2403 restart_apic_timer(apic
);
2408 if (apic_x2apic_mode(apic
) && val
!= 0)
2414 * Self-IPI exists only when x2APIC is enabled. Bits 7:0 hold
2415 * the vector, everything else is reserved.
2417 if (!apic_x2apic_mode(apic
) || (val
& ~APIC_VECTOR_MASK
))
2420 kvm_apic_send_ipi(apic
, APIC_DEST_SELF
| val
, 0);
2428 * Recalculate APIC maps if necessary, e.g. if the software enable bit
2429 * was toggled, the APIC ID changed, etc... The maps are marked dirty
2430 * on relevant changes, i.e. this is a nop for most writes.
2432 kvm_recalculate_apic_map(apic
->vcpu
->kvm
);
2437 static int apic_mmio_write(struct kvm_vcpu
*vcpu
, struct kvm_io_device
*this,
2438 gpa_t address
, int len
, const void *data
)
2440 struct kvm_lapic
*apic
= to_lapic(this);
2441 unsigned int offset
= address
- apic
->base_address
;
2444 if (!apic_mmio_in_range(apic
, address
))
2447 if (!kvm_apic_hw_enabled(apic
) || apic_x2apic_mode(apic
)) {
2448 if (!kvm_check_has_quirk(vcpu
->kvm
,
2449 KVM_X86_QUIRK_LAPIC_MMIO_HOLE
))
2456 * APIC register must be aligned on 128-bits boundary.
2457 * 32/64/128 bits registers must be accessed thru 32 bits.
2460 if (len
!= 4 || (offset
& 0xf))
2465 kvm_lapic_reg_write(apic
, offset
& 0xff0, val
);
2470 void kvm_lapic_set_eoi(struct kvm_vcpu
*vcpu
)
2472 kvm_lapic_reg_write(vcpu
->arch
.apic
, APIC_EOI
, 0);
2474 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi
);
2476 #define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13))
2478 int kvm_x2apic_icr_write(struct kvm_lapic
*apic
, u64 data
)
2480 if (data
& X2APIC_ICR_RESERVED_BITS
)
2484 * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but
2485 * only AMD requires it to be zero, Intel essentially just ignores the
2486 * bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled,
2487 * the CPU performs the reserved bits checks, i.e. the underlying CPU
2488 * behavior will "win". Arbitrarily clear the BUSY bit, as there is no
2489 * sane way to provide consistent behavior with respect to hardware.
2491 data
&= ~APIC_ICR_BUSY
;
2493 kvm_apic_send_ipi(apic
, (u32
)data
, (u32
)(data
>> 32));
2494 if (kvm_x86_ops
.x2apic_icr_is_split
) {
2495 kvm_lapic_set_reg(apic
, APIC_ICR
, data
);
2496 kvm_lapic_set_reg(apic
, APIC_ICR2
, data
>> 32);
2498 kvm_lapic_set_reg64(apic
, APIC_ICR
, data
);
2500 trace_kvm_apic_write(APIC_ICR
, data
);
2504 static u64
kvm_x2apic_icr_read(struct kvm_lapic
*apic
)
2506 if (kvm_x86_ops
.x2apic_icr_is_split
)
2507 return (u64
)kvm_lapic_get_reg(apic
, APIC_ICR
) |
2508 (u64
)kvm_lapic_get_reg(apic
, APIC_ICR2
) << 32;
2510 return kvm_lapic_get_reg64(apic
, APIC_ICR
);
2513 /* emulate APIC access in a trap manner */
2514 void kvm_apic_write_nodecode(struct kvm_vcpu
*vcpu
, u32 offset
)
2516 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2519 * ICR is a single 64-bit register when x2APIC is enabled, all others
2520 * registers hold 32-bit values. For legacy xAPIC, ICR writes need to
2521 * go down the common path to get the upper half from ICR2.
2523 * Note, using the write helpers may incur an unnecessary write to the
2524 * virtual APIC state, but KVM needs to conditionally modify the value
2525 * in certain cases, e.g. to clear the ICR busy bit. The cost of extra
2526 * conditional branches is likely a wash relative to the cost of the
2527 * maybe-unecessary write, and both are in the noise anyways.
2529 if (apic_x2apic_mode(apic
) && offset
== APIC_ICR
)
2530 WARN_ON_ONCE(kvm_x2apic_icr_write(apic
, kvm_x2apic_icr_read(apic
)));
2532 kvm_lapic_reg_write(apic
, offset
, kvm_lapic_get_reg(apic
, offset
));
2534 EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode
);
2536 void kvm_free_lapic(struct kvm_vcpu
*vcpu
)
2538 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2540 if (!vcpu
->arch
.apic
) {
2541 static_branch_dec(&kvm_has_noapic_vcpu
);
2545 hrtimer_cancel(&apic
->lapic_timer
.timer
);
2547 if (!(vcpu
->arch
.apic_base
& MSR_IA32_APICBASE_ENABLE
))
2548 static_branch_slow_dec_deferred(&apic_hw_disabled
);
2550 if (!apic
->sw_enabled
)
2551 static_branch_slow_dec_deferred(&apic_sw_disabled
);
2554 free_page((unsigned long)apic
->regs
);
2560 *----------------------------------------------------------------------
2562 *----------------------------------------------------------------------
2564 u64
kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu
*vcpu
)
2566 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2568 if (!kvm_apic_present(vcpu
) || !apic_lvtt_tscdeadline(apic
))
2571 return apic
->lapic_timer
.tscdeadline
;
2574 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu
*vcpu
, u64 data
)
2576 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2578 if (!kvm_apic_present(vcpu
) || !apic_lvtt_tscdeadline(apic
))
2581 hrtimer_cancel(&apic
->lapic_timer
.timer
);
2582 apic
->lapic_timer
.tscdeadline
= data
;
2583 start_apic_timer(apic
);
2586 void kvm_lapic_set_tpr(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
2588 apic_set_tpr(vcpu
->arch
.apic
, (cr8
& 0x0f) << 4);
2591 u64
kvm_lapic_get_cr8(struct kvm_vcpu
*vcpu
)
2595 tpr
= (u64
) kvm_lapic_get_reg(vcpu
->arch
.apic
, APIC_TASKPRI
);
2597 return (tpr
& 0xf0) >> 4;
2600 static void __kvm_apic_set_base(struct kvm_vcpu
*vcpu
, u64 value
)
2602 u64 old_value
= vcpu
->arch
.apic_base
;
2603 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2605 vcpu
->arch
.apic_base
= value
;
2607 if ((old_value
^ value
) & MSR_IA32_APICBASE_ENABLE
)
2608 vcpu
->arch
.cpuid_dynamic_bits_dirty
= true;
2613 /* update jump label if enable bit changes */
2614 if ((old_value
^ value
) & MSR_IA32_APICBASE_ENABLE
) {
2615 if (value
& MSR_IA32_APICBASE_ENABLE
) {
2616 kvm_apic_set_xapic_id(apic
, vcpu
->vcpu_id
);
2617 static_branch_slow_dec_deferred(&apic_hw_disabled
);
2618 /* Check if there are APF page ready requests pending */
2619 kvm_make_request(KVM_REQ_APF_READY
, vcpu
);
2621 static_branch_inc(&apic_hw_disabled
.key
);
2622 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
2626 if ((old_value
^ value
) & X2APIC_ENABLE
) {
2627 if (value
& X2APIC_ENABLE
)
2628 kvm_apic_set_x2apic_id(apic
, vcpu
->vcpu_id
);
2629 else if (value
& MSR_IA32_APICBASE_ENABLE
)
2630 kvm_apic_set_xapic_id(apic
, vcpu
->vcpu_id
);
2633 if ((old_value
^ value
) & (MSR_IA32_APICBASE_ENABLE
| X2APIC_ENABLE
)) {
2634 kvm_make_request(KVM_REQ_APICV_UPDATE
, vcpu
);
2635 kvm_x86_call(set_virtual_apic_mode
)(vcpu
);
2638 apic
->base_address
= apic
->vcpu
->arch
.apic_base
&
2639 MSR_IA32_APICBASE_BASE
;
2641 if ((value
& MSR_IA32_APICBASE_ENABLE
) &&
2642 apic
->base_address
!= APIC_DEFAULT_PHYS_BASE
) {
2643 kvm_set_apicv_inhibit(apic
->vcpu
->kvm
,
2644 APICV_INHIBIT_REASON_APIC_BASE_MODIFIED
);
2648 int kvm_apic_set_base(struct kvm_vcpu
*vcpu
, u64 value
, bool host_initiated
)
2650 enum lapic_mode old_mode
= kvm_get_apic_mode(vcpu
);
2651 enum lapic_mode new_mode
= kvm_apic_mode(value
);
2653 if (vcpu
->arch
.apic_base
== value
)
2656 u64 reserved_bits
= kvm_vcpu_reserved_gpa_bits_raw(vcpu
) | 0x2ff |
2657 (guest_cpu_cap_has(vcpu
, X86_FEATURE_X2APIC
) ? 0 : X2APIC_ENABLE
);
2659 if ((value
& reserved_bits
) != 0 || new_mode
== LAPIC_MODE_INVALID
)
2661 if (!host_initiated
) {
2662 if (old_mode
== LAPIC_MODE_X2APIC
&& new_mode
== LAPIC_MODE_XAPIC
)
2664 if (old_mode
== LAPIC_MODE_DISABLED
&& new_mode
== LAPIC_MODE_X2APIC
)
2668 __kvm_apic_set_base(vcpu
, value
);
2669 kvm_recalculate_apic_map(vcpu
->kvm
);
2672 EXPORT_SYMBOL_GPL(kvm_apic_set_base
);
2674 void kvm_apic_update_apicv(struct kvm_vcpu
*vcpu
)
2676 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2679 * When APICv is enabled, KVM must always search the IRR for a pending
2680 * IRQ, as other vCPUs and devices can set IRR bits even if the vCPU
2681 * isn't running. If APICv is disabled, KVM _should_ search the IRR
2682 * for a pending IRQ. But KVM currently doesn't ensure *all* hardware,
2683 * e.g. CPUs and IOMMUs, has seen the change in state, i.e. searching
2684 * the IRR at this time could race with IRQ delivery from hardware that
2685 * still sees APICv as being enabled.
2687 * FIXME: Ensure other vCPUs and devices observe the change in APICv
2688 * state prior to updating KVM's metadata caches, so that KVM
2689 * can safely search the IRR and set irr_pending accordingly.
2691 apic
->irr_pending
= true;
2693 if (apic
->apicv_active
)
2694 apic
->isr_count
= 1;
2696 apic
->isr_count
= count_vectors(apic
->regs
+ APIC_ISR
);
2698 apic
->highest_isr_cache
= -1;
2701 int kvm_alloc_apic_access_page(struct kvm
*kvm
)
2706 mutex_lock(&kvm
->slots_lock
);
2707 if (kvm
->arch
.apic_access_memslot_enabled
||
2708 kvm
->arch
.apic_access_memslot_inhibited
)
2711 hva
= __x86_set_memory_region(kvm
, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
,
2712 APIC_DEFAULT_PHYS_BASE
, PAGE_SIZE
);
2718 kvm
->arch
.apic_access_memslot_enabled
= true;
2720 mutex_unlock(&kvm
->slots_lock
);
2723 EXPORT_SYMBOL_GPL(kvm_alloc_apic_access_page
);
2725 void kvm_inhibit_apic_access_page(struct kvm_vcpu
*vcpu
)
2727 struct kvm
*kvm
= vcpu
->kvm
;
2729 if (!kvm
->arch
.apic_access_memslot_enabled
)
2732 kvm_vcpu_srcu_read_unlock(vcpu
);
2734 mutex_lock(&kvm
->slots_lock
);
2736 if (kvm
->arch
.apic_access_memslot_enabled
) {
2737 __x86_set_memory_region(kvm
, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
, 0, 0);
2739 * Clear "enabled" after the memslot is deleted so that a
2740 * different vCPU doesn't get a false negative when checking
2741 * the flag out of slots_lock. No additional memory barrier is
2742 * needed as modifying memslots requires waiting other vCPUs to
2743 * drop SRCU (see above), and false positives are ok as the
2744 * flag is rechecked after acquiring slots_lock.
2746 kvm
->arch
.apic_access_memslot_enabled
= false;
2749 * Mark the memslot as inhibited to prevent reallocating the
2750 * memslot during vCPU creation, e.g. if a vCPU is hotplugged.
2752 kvm
->arch
.apic_access_memslot_inhibited
= true;
2755 mutex_unlock(&kvm
->slots_lock
);
2757 kvm_vcpu_srcu_read_lock(vcpu
);
2760 void kvm_lapic_reset(struct kvm_vcpu
*vcpu
, bool init_event
)
2762 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2766 kvm_x86_call(apicv_pre_state_restore
)(vcpu
);
2769 msr_val
= APIC_DEFAULT_PHYS_BASE
| MSR_IA32_APICBASE_ENABLE
;
2770 if (kvm_vcpu_is_reset_bsp(vcpu
))
2771 msr_val
|= MSR_IA32_APICBASE_BSP
;
2774 * Use the inner helper to avoid an extra recalcuation of the
2775 * optimized APIC map if some other task has dirtied the map.
2776 * The recalculation needed for this vCPU will be done after
2777 * all APIC state has been initialized (see below).
2779 __kvm_apic_set_base(vcpu
, msr_val
);
2785 /* Stop the timer in case it's a reset to an active apic */
2786 hrtimer_cancel(&apic
->lapic_timer
.timer
);
2788 /* The xAPIC ID is set at RESET even if the APIC was already enabled. */
2790 kvm_apic_set_xapic_id(apic
, vcpu
->vcpu_id
);
2791 kvm_apic_set_version(apic
->vcpu
);
2793 for (i
= 0; i
< apic
->nr_lvt_entries
; i
++)
2794 kvm_lapic_set_reg(apic
, APIC_LVTx(i
), APIC_LVT_MASKED
);
2795 apic_update_lvtt(apic
);
2796 if (kvm_vcpu_is_reset_bsp(vcpu
) &&
2797 kvm_check_has_quirk(vcpu
->kvm
, KVM_X86_QUIRK_LINT0_REENABLED
))
2798 kvm_lapic_set_reg(apic
, APIC_LVT0
,
2799 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT
));
2800 apic_manage_nmi_watchdog(apic
, kvm_lapic_get_reg(apic
, APIC_LVT0
));
2802 kvm_apic_set_dfr(apic
, 0xffffffffU
);
2803 apic_set_spiv(apic
, 0xff);
2804 kvm_lapic_set_reg(apic
, APIC_TASKPRI
, 0);
2805 if (!apic_x2apic_mode(apic
))
2806 kvm_apic_set_ldr(apic
, 0);
2807 kvm_lapic_set_reg(apic
, APIC_ESR
, 0);
2808 if (!apic_x2apic_mode(apic
)) {
2809 kvm_lapic_set_reg(apic
, APIC_ICR
, 0);
2810 kvm_lapic_set_reg(apic
, APIC_ICR2
, 0);
2812 kvm_lapic_set_reg64(apic
, APIC_ICR
, 0);
2814 kvm_lapic_set_reg(apic
, APIC_TDCR
, 0);
2815 kvm_lapic_set_reg(apic
, APIC_TMICT
, 0);
2816 for (i
= 0; i
< 8; i
++) {
2817 kvm_lapic_set_reg(apic
, APIC_IRR
+ 0x10 * i
, 0);
2818 kvm_lapic_set_reg(apic
, APIC_ISR
+ 0x10 * i
, 0);
2819 kvm_lapic_set_reg(apic
, APIC_TMR
+ 0x10 * i
, 0);
2821 kvm_apic_update_apicv(vcpu
);
2822 update_divide_count(apic
);
2823 atomic_set(&apic
->lapic_timer
.pending
, 0);
2825 vcpu
->arch
.pv_eoi
.msr_val
= 0;
2826 apic_update_ppr(apic
);
2827 if (apic
->apicv_active
) {
2828 kvm_x86_call(apicv_post_state_restore
)(vcpu
);
2829 kvm_x86_call(hwapic_isr_update
)(vcpu
, -1);
2832 vcpu
->arch
.apic_arb_prio
= 0;
2833 vcpu
->arch
.apic_attention
= 0;
2835 kvm_recalculate_apic_map(vcpu
->kvm
);
2839 *----------------------------------------------------------------------
2841 *----------------------------------------------------------------------
2844 static bool lapic_is_periodic(struct kvm_lapic
*apic
)
2846 return apic_lvtt_period(apic
);
2849 int apic_has_pending_timer(struct kvm_vcpu
*vcpu
)
2851 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2853 if (apic_enabled(apic
) && apic_lvt_enabled(apic
, APIC_LVTT
))
2854 return atomic_read(&apic
->lapic_timer
.pending
);
2859 int kvm_apic_local_deliver(struct kvm_lapic
*apic
, int lvt_type
)
2861 u32 reg
= kvm_lapic_get_reg(apic
, lvt_type
);
2862 int vector
, mode
, trig_mode
;
2865 if (kvm_apic_hw_enabled(apic
) && !(reg
& APIC_LVT_MASKED
)) {
2866 vector
= reg
& APIC_VECTOR_MASK
;
2867 mode
= reg
& APIC_MODE_MASK
;
2868 trig_mode
= reg
& APIC_LVT_LEVEL_TRIGGER
;
2870 r
= __apic_accept_irq(apic
, mode
, vector
, 1, trig_mode
, NULL
);
2871 if (r
&& lvt_type
== APIC_LVTPC
&&
2872 guest_cpuid_is_intel_compatible(apic
->vcpu
))
2873 kvm_lapic_set_reg(apic
, APIC_LVTPC
, reg
| APIC_LVT_MASKED
);
2879 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu
*vcpu
)
2881 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2884 kvm_apic_local_deliver(apic
, APIC_LVT0
);
2887 static const struct kvm_io_device_ops apic_mmio_ops
= {
2888 .read
= apic_mmio_read
,
2889 .write
= apic_mmio_write
,
2892 static enum hrtimer_restart
apic_timer_fn(struct hrtimer
*data
)
2894 struct kvm_timer
*ktimer
= container_of(data
, struct kvm_timer
, timer
);
2895 struct kvm_lapic
*apic
= container_of(ktimer
, struct kvm_lapic
, lapic_timer
);
2897 apic_timer_expired(apic
, true);
2899 if (lapic_is_periodic(apic
)) {
2900 advance_periodic_target_expiration(apic
);
2901 hrtimer_add_expires_ns(&ktimer
->timer
, ktimer
->period
);
2902 return HRTIMER_RESTART
;
2904 return HRTIMER_NORESTART
;
2907 int kvm_create_lapic(struct kvm_vcpu
*vcpu
)
2909 struct kvm_lapic
*apic
;
2911 ASSERT(vcpu
!= NULL
);
2913 if (!irqchip_in_kernel(vcpu
->kvm
)) {
2914 static_branch_inc(&kvm_has_noapic_vcpu
);
2918 apic
= kzalloc(sizeof(*apic
), GFP_KERNEL_ACCOUNT
);
2922 vcpu
->arch
.apic
= apic
;
2924 if (kvm_x86_ops
.alloc_apic_backing_page
)
2925 apic
->regs
= kvm_x86_call(alloc_apic_backing_page
)(vcpu
);
2927 apic
->regs
= (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT
);
2929 printk(KERN_ERR
"malloc apic regs error for vcpu %x\n",
2931 goto nomem_free_apic
;
2935 apic
->nr_lvt_entries
= kvm_apic_calc_nr_lvt_entries(vcpu
);
2937 hrtimer_setup(&apic
->lapic_timer
.timer
, apic_timer_fn
, CLOCK_MONOTONIC
,
2938 HRTIMER_MODE_ABS_HARD
);
2939 if (lapic_timer_advance
)
2940 apic
->lapic_timer
.timer_advance_ns
= LAPIC_TIMER_ADVANCE_NS_INIT
;
2943 * Stuff the APIC ENABLE bit in lieu of temporarily incrementing
2944 * apic_hw_disabled; the full RESET value is set by kvm_lapic_reset().
2946 vcpu
->arch
.apic_base
= MSR_IA32_APICBASE_ENABLE
;
2947 static_branch_inc(&apic_sw_disabled
.key
); /* sw disabled at reset */
2948 kvm_iodevice_init(&apic
->dev
, &apic_mmio_ops
);
2951 * Defer evaluating inhibits until the vCPU is first run, as this vCPU
2952 * will not get notified of any changes until this vCPU is visible to
2953 * other vCPUs (marked online and added to the set of vCPUs).
2955 * Opportunistically mark APICv active as VMX in particularly is highly
2956 * unlikely to have inhibits. Ignore the current per-VM APICv state so
2957 * that vCPU creation is guaranteed to run with a deterministic value,
2958 * the request will ensure the vCPU gets the correct state before VM-Entry.
2961 apic
->apicv_active
= true;
2962 kvm_make_request(KVM_REQ_APICV_UPDATE
, vcpu
);
2968 vcpu
->arch
.apic
= NULL
;
2973 int kvm_apic_has_interrupt(struct kvm_vcpu
*vcpu
)
2975 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2978 if (!kvm_apic_present(vcpu
))
2981 if (apic
->guest_apic_protected
)
2984 __apic_update_ppr(apic
, &ppr
);
2985 return apic_has_interrupt_for_ppr(apic
, ppr
);
2987 EXPORT_SYMBOL_GPL(kvm_apic_has_interrupt
);
2989 int kvm_apic_accept_pic_intr(struct kvm_vcpu
*vcpu
)
2991 u32 lvt0
= kvm_lapic_get_reg(vcpu
->arch
.apic
, APIC_LVT0
);
2993 if (!kvm_apic_hw_enabled(vcpu
->arch
.apic
))
2995 if ((lvt0
& APIC_LVT_MASKED
) == 0 &&
2996 GET_APIC_DELIVERY_MODE(lvt0
) == APIC_MODE_EXTINT
)
3001 void kvm_inject_apic_timer_irqs(struct kvm_vcpu
*vcpu
)
3003 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3005 if (atomic_read(&apic
->lapic_timer
.pending
) > 0) {
3006 kvm_apic_inject_pending_timer_irqs(apic
);
3007 atomic_set(&apic
->lapic_timer
.pending
, 0);
3011 void kvm_apic_ack_interrupt(struct kvm_vcpu
*vcpu
, int vector
)
3013 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3016 if (WARN_ON_ONCE(vector
< 0 || !apic
))
3020 * We get here even with APIC virtualization enabled, if doing
3021 * nested virtualization and L1 runs with the "acknowledge interrupt
3022 * on exit" mode. Then we cannot inject the interrupt via RVI,
3023 * because the process would deliver it through the IDT.
3026 apic_clear_irr(vector
, apic
);
3027 if (kvm_hv_synic_auto_eoi_set(vcpu
, vector
)) {
3029 * For auto-EOI interrupts, there might be another pending
3030 * interrupt above PPR, so check whether to raise another
3033 apic_update_ppr(apic
);
3036 * For normal interrupts, PPR has been raised and there cannot
3037 * be a higher-priority pending interrupt---except if there was
3038 * a concurrent interrupt injection, but that would have
3039 * triggered KVM_REQ_EVENT already.
3041 apic_set_isr(vector
, apic
);
3042 __apic_update_ppr(apic
, &ppr
);
3046 EXPORT_SYMBOL_GPL(kvm_apic_ack_interrupt
);
3048 static int kvm_apic_state_fixup(struct kvm_vcpu
*vcpu
,
3049 struct kvm_lapic_state
*s
, bool set
)
3051 if (apic_x2apic_mode(vcpu
->arch
.apic
)) {
3052 u32 x2apic_id
= kvm_x2apic_id(vcpu
->arch
.apic
);
3053 u32
*id
= (u32
*)(s
->regs
+ APIC_ID
);
3054 u32
*ldr
= (u32
*)(s
->regs
+ APIC_LDR
);
3057 if (vcpu
->kvm
->arch
.x2apic_format
) {
3058 if (*id
!= x2apic_id
)
3062 * Ignore the userspace value when setting APIC state.
3063 * KVM's model is that the x2APIC ID is readonly, e.g.
3064 * KVM only supports delivering interrupts to KVM's
3065 * version of the x2APIC ID. However, for backwards
3066 * compatibility, don't reject attempts to set a
3067 * mismatched ID for userspace that hasn't opted into
3073 *id
= x2apic_id
<< 24;
3077 * In x2APIC mode, the LDR is fixed and based on the id. And
3078 * if the ICR is _not_ split, ICR is internally a single 64-bit
3079 * register, but needs to be split to ICR+ICR2 in userspace for
3080 * backwards compatibility.
3083 *ldr
= kvm_apic_calc_x2apic_ldr(x2apic_id
);
3085 if (!kvm_x86_ops
.x2apic_icr_is_split
) {
3087 icr
= __kvm_lapic_get_reg(s
->regs
, APIC_ICR
) |
3088 (u64
)__kvm_lapic_get_reg(s
->regs
, APIC_ICR2
) << 32;
3089 __kvm_lapic_set_reg64(s
->regs
, APIC_ICR
, icr
);
3091 icr
= __kvm_lapic_get_reg64(s
->regs
, APIC_ICR
);
3092 __kvm_lapic_set_reg(s
->regs
, APIC_ICR2
, icr
>> 32);
3100 int kvm_apic_get_state(struct kvm_vcpu
*vcpu
, struct kvm_lapic_state
*s
)
3102 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof(*s
));
3105 * Get calculated timer current count for remaining timer period (if
3106 * any) and store it in the returned register set.
3108 __kvm_lapic_set_reg(s
->regs
, APIC_TMCCT
,
3109 __apic_read(vcpu
->arch
.apic
, APIC_TMCCT
));
3111 return kvm_apic_state_fixup(vcpu
, s
, false);
3114 int kvm_apic_set_state(struct kvm_vcpu
*vcpu
, struct kvm_lapic_state
*s
)
3116 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3119 kvm_x86_call(apicv_pre_state_restore
)(vcpu
);
3121 /* set SPIV separately to get count of SW disabled APICs right */
3122 apic_set_spiv(apic
, *((u32
*)(s
->regs
+ APIC_SPIV
)));
3124 r
= kvm_apic_state_fixup(vcpu
, s
, true);
3126 kvm_recalculate_apic_map(vcpu
->kvm
);
3129 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof(*s
));
3131 atomic_set_release(&apic
->vcpu
->kvm
->arch
.apic_map_dirty
, DIRTY
);
3132 kvm_recalculate_apic_map(vcpu
->kvm
);
3133 kvm_apic_set_version(vcpu
);
3135 apic_update_ppr(apic
);
3136 cancel_apic_timer(apic
);
3137 apic
->lapic_timer
.expired_tscdeadline
= 0;
3138 apic_update_lvtt(apic
);
3139 apic_manage_nmi_watchdog(apic
, kvm_lapic_get_reg(apic
, APIC_LVT0
));
3140 update_divide_count(apic
);
3141 __start_apic_timer(apic
, APIC_TMCCT
);
3142 kvm_lapic_set_reg(apic
, APIC_TMCCT
, 0);
3143 kvm_apic_update_apicv(vcpu
);
3144 if (apic
->apicv_active
) {
3145 kvm_x86_call(apicv_post_state_restore
)(vcpu
);
3146 kvm_x86_call(hwapic_isr_update
)(vcpu
, apic_find_highest_isr(apic
));
3148 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
3149 if (ioapic_in_kernel(vcpu
->kvm
))
3150 kvm_rtc_eoi_tracking_restore_one(vcpu
);
3152 vcpu
->arch
.apic_arb_prio
= 0;
3157 void __kvm_migrate_apic_timer(struct kvm_vcpu
*vcpu
)
3159 struct hrtimer
*timer
;
3161 if (!lapic_in_kernel(vcpu
) ||
3162 kvm_can_post_timer_interrupt(vcpu
))
3165 timer
= &vcpu
->arch
.apic
->lapic_timer
.timer
;
3166 if (hrtimer_cancel(timer
))
3167 hrtimer_start_expires(timer
, HRTIMER_MODE_ABS_HARD
);
3171 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
3173 * Detect whether guest triggered PV EOI since the
3174 * last entry. If yes, set EOI on guests's behalf.
3175 * Clear PV EOI in guest memory in any case.
3177 static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu
*vcpu
,
3178 struct kvm_lapic
*apic
)
3182 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
3183 * and KVM_PV_EOI_ENABLED in guest memory as follows:
3185 * KVM_APIC_PV_EOI_PENDING is unset:
3186 * -> host disabled PV EOI.
3187 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
3188 * -> host enabled PV EOI, guest did not execute EOI yet.
3189 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
3190 * -> host enabled PV EOI, guest executed EOI.
3192 BUG_ON(!pv_eoi_enabled(vcpu
));
3194 if (pv_eoi_test_and_clr_pending(vcpu
))
3196 vector
= apic_set_eoi(apic
);
3197 trace_kvm_pv_eoi(apic
, vector
);
3200 void kvm_lapic_sync_from_vapic(struct kvm_vcpu
*vcpu
)
3204 if (test_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
))
3205 apic_sync_pv_eoi_from_guest(vcpu
, vcpu
->arch
.apic
);
3207 if (!test_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
))
3210 if (kvm_read_guest_cached(vcpu
->kvm
, &vcpu
->arch
.apic
->vapic_cache
, &data
,
3214 apic_set_tpr(vcpu
->arch
.apic
, data
& 0xff);
3218 * apic_sync_pv_eoi_to_guest - called before vmentry
3220 * Detect whether it's safe to enable PV EOI and
3223 static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu
*vcpu
,
3224 struct kvm_lapic
*apic
)
3226 if (!pv_eoi_enabled(vcpu
) ||
3227 /* IRR set or many bits in ISR: could be nested. */
3228 apic
->irr_pending
||
3229 /* Cache not set: could be safe but we don't bother. */
3230 apic
->highest_isr_cache
== -1 ||
3231 /* Need EOI to update ioapic. */
3232 kvm_ioapic_handles_vector(apic
, apic
->highest_isr_cache
)) {
3234 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
3235 * so we need not do anything here.
3240 pv_eoi_set_pending(apic
->vcpu
);
3243 void kvm_lapic_sync_to_vapic(struct kvm_vcpu
*vcpu
)
3246 int max_irr
, max_isr
;
3247 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3249 apic_sync_pv_eoi_to_guest(vcpu
, apic
);
3251 if (!test_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
))
3254 tpr
= kvm_lapic_get_reg(apic
, APIC_TASKPRI
) & 0xff;
3255 max_irr
= apic_find_highest_irr(apic
);
3258 max_isr
= apic_find_highest_isr(apic
);
3261 data
= (tpr
& 0xff) | ((max_isr
& 0xf0) << 8) | (max_irr
<< 24);
3263 kvm_write_guest_cached(vcpu
->kvm
, &vcpu
->arch
.apic
->vapic_cache
, &data
,
3267 int kvm_lapic_set_vapic_addr(struct kvm_vcpu
*vcpu
, gpa_t vapic_addr
)
3270 if (kvm_gfn_to_hva_cache_init(vcpu
->kvm
,
3271 &vcpu
->arch
.apic
->vapic_cache
,
3272 vapic_addr
, sizeof(u32
)))
3274 __set_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
);
3276 __clear_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
);
3279 vcpu
->arch
.apic
->vapic_addr
= vapic_addr
;
3283 static int kvm_lapic_msr_read(struct kvm_lapic
*apic
, u32 reg
, u64
*data
)
3287 if (reg
== APIC_ICR
) {
3288 *data
= kvm_x2apic_icr_read(apic
);
3292 if (kvm_lapic_reg_read(apic
, reg
, 4, &low
))
3300 static int kvm_lapic_msr_write(struct kvm_lapic
*apic
, u32 reg
, u64 data
)
3303 * ICR is a 64-bit register in x2APIC mode (and Hyper-V PV vAPIC) and
3304 * can be written as such, all other registers remain accessible only
3305 * through 32-bit reads/writes.
3307 if (reg
== APIC_ICR
)
3308 return kvm_x2apic_icr_write(apic
, data
);
3310 /* Bits 63:32 are reserved in all other registers. */
3314 return kvm_lapic_reg_write(apic
, reg
, (u32
)data
);
3317 int kvm_x2apic_msr_write(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
3319 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3320 u32 reg
= (msr
- APIC_BASE_MSR
) << 4;
3322 if (!lapic_in_kernel(vcpu
) || !apic_x2apic_mode(apic
))
3325 return kvm_lapic_msr_write(apic
, reg
, data
);
3328 int kvm_x2apic_msr_read(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*data
)
3330 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3331 u32 reg
= (msr
- APIC_BASE_MSR
) << 4;
3333 if (!lapic_in_kernel(vcpu
) || !apic_x2apic_mode(apic
))
3336 return kvm_lapic_msr_read(apic
, reg
, data
);
3339 int kvm_hv_vapic_msr_write(struct kvm_vcpu
*vcpu
, u32 reg
, u64 data
)
3341 if (!lapic_in_kernel(vcpu
))
3344 return kvm_lapic_msr_write(vcpu
->arch
.apic
, reg
, data
);
3347 int kvm_hv_vapic_msr_read(struct kvm_vcpu
*vcpu
, u32 reg
, u64
*data
)
3349 if (!lapic_in_kernel(vcpu
))
3352 return kvm_lapic_msr_read(vcpu
->arch
.apic
, reg
, data
);
3355 int kvm_lapic_set_pv_eoi(struct kvm_vcpu
*vcpu
, u64 data
, unsigned long len
)
3357 u64 addr
= data
& ~KVM_MSR_ENABLED
;
3358 struct gfn_to_hva_cache
*ghc
= &vcpu
->arch
.pv_eoi
.data
;
3359 unsigned long new_len
;
3362 if (!IS_ALIGNED(addr
, 4))
3365 if (data
& KVM_MSR_ENABLED
) {
3366 if (addr
== ghc
->gpa
&& len
<= ghc
->len
)
3371 ret
= kvm_gfn_to_hva_cache_init(vcpu
->kvm
, ghc
, addr
, new_len
);
3376 vcpu
->arch
.pv_eoi
.msr_val
= data
;
3381 int kvm_apic_accept_events(struct kvm_vcpu
*vcpu
)
3383 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3387 if (!kvm_apic_has_pending_init_or_sipi(vcpu
))
3390 if (is_guest_mode(vcpu
)) {
3391 r
= kvm_check_nested_events(vcpu
);
3393 return r
== -EBUSY
? 0 : r
;
3395 * Continue processing INIT/SIPI even if a nested VM-Exit
3396 * occurred, e.g. pending SIPIs should be dropped if INIT+SIPI
3397 * are blocked as a result of transitioning to VMX root mode.
3402 * INITs are blocked while CPU is in specific states (SMM, VMX root
3403 * mode, SVM with GIF=0), while SIPIs are dropped if the CPU isn't in
3404 * wait-for-SIPI (WFS).
3406 if (!kvm_apic_init_sipi_allowed(vcpu
)) {
3407 WARN_ON_ONCE(vcpu
->arch
.mp_state
== KVM_MP_STATE_INIT_RECEIVED
);
3408 clear_bit(KVM_APIC_SIPI
, &apic
->pending_events
);
3412 if (test_and_clear_bit(KVM_APIC_INIT
, &apic
->pending_events
)) {
3413 kvm_vcpu_reset(vcpu
, true);
3414 if (kvm_vcpu_is_bsp(apic
->vcpu
))
3415 kvm_set_mp_state(vcpu
, KVM_MP_STATE_RUNNABLE
);
3417 kvm_set_mp_state(vcpu
, KVM_MP_STATE_INIT_RECEIVED
);
3419 if (test_and_clear_bit(KVM_APIC_SIPI
, &apic
->pending_events
)) {
3420 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_INIT_RECEIVED
) {
3421 /* evaluate pending_events before reading the vector */
3423 sipi_vector
= apic
->sipi_vector
;
3424 kvm_x86_call(vcpu_deliver_sipi_vector
)(vcpu
,
3426 kvm_set_mp_state(vcpu
, KVM_MP_STATE_RUNNABLE
);
3432 void kvm_lapic_exit(void)
3434 static_key_deferred_flush(&apic_hw_disabled
);
3435 WARN_ON(static_branch_unlikely(&apic_hw_disabled
.key
));
3436 static_key_deferred_flush(&apic_sw_disabled
);
3437 WARN_ON(static_branch_unlikely(&apic_sw_disabled
.key
));