3 * Local APIC virtualization
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
8 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
21 #include <linux/kvm_host.h>
22 #include <linux/kvm.h>
24 #include <linux/highmem.h>
25 #include <linux/smp.h>
26 #include <linux/hrtimer.h>
28 #include <linux/module.h>
29 #include <linux/math64.h>
30 #include <linux/slab.h>
31 #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"
47 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
49 #define mod_64(x, y) ((x) % (y))
57 #define APIC_BUS_CYCLE_NS 1
59 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
60 #define apic_debug(fmt, arg...)
62 /* 14 is the version for Xeon and Pentium 8.4.8*/
63 #define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
64 #define LAPIC_MMIO_LENGTH (1 << 12)
65 /* followed define is not in apicdef.h */
66 #define APIC_SHORT_MASK 0xc0000
67 #define APIC_DEST_NOSHORT 0x0
68 #define APIC_DEST_MASK 0x800
69 #define MAX_APIC_VECTOR 256
70 #define APIC_VECTORS_PER_REG 32
72 #define APIC_BROADCAST 0xFF
73 #define X2APIC_BROADCAST 0xFFFFFFFFul
75 static inline int apic_test_vector(int vec
, void *bitmap
)
77 return test_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
80 bool kvm_apic_pending_eoi(struct kvm_vcpu
*vcpu
, int vector
)
82 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
84 return apic_test_vector(vector
, apic
->regs
+ APIC_ISR
) ||
85 apic_test_vector(vector
, apic
->regs
+ APIC_IRR
);
88 static inline void apic_clear_vector(int vec
, void *bitmap
)
90 clear_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
93 static inline int __apic_test_and_set_vector(int vec
, void *bitmap
)
95 return __test_and_set_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
98 static inline int __apic_test_and_clear_vector(int vec
, void *bitmap
)
100 return __test_and_clear_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
103 struct static_key_deferred apic_hw_disabled __read_mostly
;
104 struct static_key_deferred apic_sw_disabled __read_mostly
;
106 static inline int apic_enabled(struct kvm_lapic
*apic
)
108 return kvm_apic_sw_enabled(apic
) && kvm_apic_hw_enabled(apic
);
112 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
115 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
116 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
118 static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map
*map
,
119 u32 dest_id
, struct kvm_lapic
***cluster
, u16
*mask
) {
121 case KVM_APIC_MODE_X2APIC
: {
122 u32 offset
= (dest_id
>> 16) * 16;
123 u32 max_apic_id
= ARRAY_SIZE(map
->phys_map
) - 1;
125 if (offset
<= max_apic_id
) {
126 u8 cluster_size
= min(max_apic_id
- offset
+ 1, 16U);
128 *cluster
= &map
->phys_map
[offset
];
129 *mask
= dest_id
& (0xffff >> (16 - cluster_size
));
136 case KVM_APIC_MODE_XAPIC_FLAT
:
137 *cluster
= map
->xapic_flat_map
;
138 *mask
= dest_id
& 0xff;
140 case KVM_APIC_MODE_XAPIC_CLUSTER
:
141 *cluster
= map
->xapic_cluster_map
[dest_id
>> 4];
142 *mask
= dest_id
& 0xf;
150 static void recalculate_apic_map(struct kvm
*kvm
)
152 struct kvm_apic_map
*new, *old
= NULL
;
153 struct kvm_vcpu
*vcpu
;
156 new = kzalloc(sizeof(struct kvm_apic_map
), GFP_KERNEL
);
158 mutex_lock(&kvm
->arch
.apic_map_lock
);
163 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
164 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
165 struct kvm_lapic
**cluster
;
169 if (!kvm_apic_present(vcpu
))
172 aid
= kvm_apic_id(apic
);
173 ldr
= kvm_lapic_get_reg(apic
, APIC_LDR
);
175 if (aid
< ARRAY_SIZE(new->phys_map
))
176 new->phys_map
[aid
] = apic
;
178 if (apic_x2apic_mode(apic
)) {
179 new->mode
|= KVM_APIC_MODE_X2APIC
;
181 ldr
= GET_APIC_LOGICAL_ID(ldr
);
182 if (kvm_lapic_get_reg(apic
, APIC_DFR
) == APIC_DFR_FLAT
)
183 new->mode
|= KVM_APIC_MODE_XAPIC_FLAT
;
185 new->mode
|= KVM_APIC_MODE_XAPIC_CLUSTER
;
188 if (!kvm_apic_map_get_logical_dest(new, ldr
, &cluster
, &mask
))
192 cluster
[ffs(mask
) - 1] = apic
;
195 old
= rcu_dereference_protected(kvm
->arch
.apic_map
,
196 lockdep_is_held(&kvm
->arch
.apic_map_lock
));
197 rcu_assign_pointer(kvm
->arch
.apic_map
, new);
198 mutex_unlock(&kvm
->arch
.apic_map_lock
);
203 kvm_make_scan_ioapic_request(kvm
);
206 static inline void apic_set_spiv(struct kvm_lapic
*apic
, u32 val
)
208 bool enabled
= val
& APIC_SPIV_APIC_ENABLED
;
210 kvm_lapic_set_reg(apic
, APIC_SPIV
, val
);
212 if (enabled
!= apic
->sw_enabled
) {
213 apic
->sw_enabled
= enabled
;
215 static_key_slow_dec_deferred(&apic_sw_disabled
);
216 recalculate_apic_map(apic
->vcpu
->kvm
);
218 static_key_slow_inc(&apic_sw_disabled
.key
);
222 static inline void kvm_apic_set_id(struct kvm_lapic
*apic
, u8 id
)
224 kvm_lapic_set_reg(apic
, APIC_ID
, id
<< 24);
225 recalculate_apic_map(apic
->vcpu
->kvm
);
228 static inline void kvm_apic_set_ldr(struct kvm_lapic
*apic
, u32 id
)
230 kvm_lapic_set_reg(apic
, APIC_LDR
, id
);
231 recalculate_apic_map(apic
->vcpu
->kvm
);
234 static inline void kvm_apic_set_x2apic_id(struct kvm_lapic
*apic
, u8 id
)
236 u32 ldr
= ((id
>> 4) << 16) | (1 << (id
& 0xf));
238 kvm_lapic_set_reg(apic
, APIC_ID
, id
<< 24);
239 kvm_lapic_set_reg(apic
, APIC_LDR
, ldr
);
240 recalculate_apic_map(apic
->vcpu
->kvm
);
243 static inline int apic_lvt_enabled(struct kvm_lapic
*apic
, int lvt_type
)
245 return !(kvm_lapic_get_reg(apic
, lvt_type
) & APIC_LVT_MASKED
);
248 static inline int apic_lvt_vector(struct kvm_lapic
*apic
, int lvt_type
)
250 return kvm_lapic_get_reg(apic
, lvt_type
) & APIC_VECTOR_MASK
;
253 static inline int apic_lvtt_oneshot(struct kvm_lapic
*apic
)
255 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_ONESHOT
;
258 static inline int apic_lvtt_period(struct kvm_lapic
*apic
)
260 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_PERIODIC
;
263 static inline int apic_lvtt_tscdeadline(struct kvm_lapic
*apic
)
265 return apic
->lapic_timer
.timer_mode
== APIC_LVT_TIMER_TSCDEADLINE
;
268 static inline int apic_lvt_nmi_mode(u32 lvt_val
)
270 return (lvt_val
& (APIC_MODE_MASK
| APIC_LVT_MASKED
)) == APIC_DM_NMI
;
273 void kvm_apic_set_version(struct kvm_vcpu
*vcpu
)
275 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
276 struct kvm_cpuid_entry2
*feat
;
277 u32 v
= APIC_VERSION
;
279 if (!lapic_in_kernel(vcpu
))
282 feat
= kvm_find_cpuid_entry(apic
->vcpu
, 0x1, 0);
283 if (feat
&& (feat
->ecx
& (1 << (X86_FEATURE_X2APIC
& 31))))
284 v
|= APIC_LVR_DIRECTED_EOI
;
285 kvm_lapic_set_reg(apic
, APIC_LVR
, v
);
288 static const unsigned int apic_lvt_mask
[KVM_APIC_LVT_NUM
] = {
289 LVT_MASK
, /* part LVTT mask, timer mode mask added at runtime */
290 LVT_MASK
| APIC_MODE_MASK
, /* LVTTHMR */
291 LVT_MASK
| APIC_MODE_MASK
, /* LVTPC */
292 LINT_MASK
, LINT_MASK
, /* LVT0-1 */
293 LVT_MASK
/* LVTERR */
296 static int find_highest_vector(void *bitmap
)
301 for (vec
= MAX_APIC_VECTOR
- APIC_VECTORS_PER_REG
;
302 vec
>= 0; vec
-= APIC_VECTORS_PER_REG
) {
303 reg
= bitmap
+ REG_POS(vec
);
305 return fls(*reg
) - 1 + vec
;
311 static u8
count_vectors(void *bitmap
)
317 for (vec
= 0; vec
< MAX_APIC_VECTOR
; vec
+= APIC_VECTORS_PER_REG
) {
318 reg
= bitmap
+ REG_POS(vec
);
319 count
+= hweight32(*reg
);
325 void __kvm_apic_update_irr(u32
*pir
, void *regs
)
329 for (i
= 0; i
<= 7; i
++) {
330 pir_val
= xchg(&pir
[i
], 0);
332 *((u32
*)(regs
+ APIC_IRR
+ i
* 0x10)) |= pir_val
;
335 EXPORT_SYMBOL_GPL(__kvm_apic_update_irr
);
337 void kvm_apic_update_irr(struct kvm_vcpu
*vcpu
, u32
*pir
)
339 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
341 __kvm_apic_update_irr(pir
, apic
->regs
);
343 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
345 EXPORT_SYMBOL_GPL(kvm_apic_update_irr
);
347 static inline int apic_search_irr(struct kvm_lapic
*apic
)
349 return find_highest_vector(apic
->regs
+ APIC_IRR
);
352 static inline int apic_find_highest_irr(struct kvm_lapic
*apic
)
357 * Note that irr_pending is just a hint. It will be always
358 * true with virtual interrupt delivery enabled.
360 if (!apic
->irr_pending
)
363 if (apic
->vcpu
->arch
.apicv_active
)
364 kvm_x86_ops
->sync_pir_to_irr(apic
->vcpu
);
365 result
= apic_search_irr(apic
);
366 ASSERT(result
== -1 || result
>= 16);
371 static inline void apic_clear_irr(int vec
, struct kvm_lapic
*apic
)
373 struct kvm_vcpu
*vcpu
;
377 if (unlikely(vcpu
->arch
.apicv_active
)) {
378 /* try to update RVI */
379 apic_clear_vector(vec
, apic
->regs
+ APIC_IRR
);
380 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
382 apic
->irr_pending
= false;
383 apic_clear_vector(vec
, apic
->regs
+ APIC_IRR
);
384 if (apic_search_irr(apic
) != -1)
385 apic
->irr_pending
= true;
389 static inline void apic_set_isr(int vec
, struct kvm_lapic
*apic
)
391 struct kvm_vcpu
*vcpu
;
393 if (__apic_test_and_set_vector(vec
, apic
->regs
+ APIC_ISR
))
399 * With APIC virtualization enabled, all caching is disabled
400 * because the processor can modify ISR under the hood. Instead
403 if (unlikely(vcpu
->arch
.apicv_active
))
404 kvm_x86_ops
->hwapic_isr_update(vcpu
, vec
);
407 BUG_ON(apic
->isr_count
> MAX_APIC_VECTOR
);
409 * ISR (in service register) bit is set when injecting an interrupt.
410 * The highest vector is injected. Thus the latest bit set matches
411 * the highest bit in ISR.
413 apic
->highest_isr_cache
= vec
;
417 static inline int apic_find_highest_isr(struct kvm_lapic
*apic
)
422 * Note that isr_count is always 1, and highest_isr_cache
423 * is always -1, with APIC virtualization enabled.
425 if (!apic
->isr_count
)
427 if (likely(apic
->highest_isr_cache
!= -1))
428 return apic
->highest_isr_cache
;
430 result
= find_highest_vector(apic
->regs
+ APIC_ISR
);
431 ASSERT(result
== -1 || result
>= 16);
436 static inline void apic_clear_isr(int vec
, struct kvm_lapic
*apic
)
438 struct kvm_vcpu
*vcpu
;
439 if (!__apic_test_and_clear_vector(vec
, apic
->regs
+ APIC_ISR
))
445 * We do get here for APIC virtualization enabled if the guest
446 * uses the Hyper-V APIC enlightenment. In this case we may need
447 * to trigger a new interrupt delivery by writing the SVI field;
448 * on the other hand isr_count and highest_isr_cache are unused
449 * and must be left alone.
451 if (unlikely(vcpu
->arch
.apicv_active
))
452 kvm_x86_ops
->hwapic_isr_update(vcpu
,
453 apic_find_highest_isr(apic
));
456 BUG_ON(apic
->isr_count
< 0);
457 apic
->highest_isr_cache
= -1;
461 int kvm_lapic_find_highest_irr(struct kvm_vcpu
*vcpu
)
463 /* This may race with setting of irr in __apic_accept_irq() and
464 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
465 * will cause vmexit immediately and the value will be recalculated
466 * on the next vmentry.
468 return apic_find_highest_irr(vcpu
->arch
.apic
);
471 static int __apic_accept_irq(struct kvm_lapic
*apic
, int delivery_mode
,
472 int vector
, int level
, int trig_mode
,
473 struct dest_map
*dest_map
);
475 int kvm_apic_set_irq(struct kvm_vcpu
*vcpu
, struct kvm_lapic_irq
*irq
,
476 struct dest_map
*dest_map
)
478 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
480 return __apic_accept_irq(apic
, irq
->delivery_mode
, irq
->vector
,
481 irq
->level
, irq
->trig_mode
, dest_map
);
484 static int pv_eoi_put_user(struct kvm_vcpu
*vcpu
, u8 val
)
487 return kvm_write_guest_cached(vcpu
->kvm
, &vcpu
->arch
.pv_eoi
.data
, &val
,
491 static int pv_eoi_get_user(struct kvm_vcpu
*vcpu
, u8
*val
)
494 return kvm_read_guest_cached(vcpu
->kvm
, &vcpu
->arch
.pv_eoi
.data
, val
,
498 static inline bool pv_eoi_enabled(struct kvm_vcpu
*vcpu
)
500 return vcpu
->arch
.pv_eoi
.msr_val
& KVM_MSR_ENABLED
;
503 static bool pv_eoi_get_pending(struct kvm_vcpu
*vcpu
)
506 if (pv_eoi_get_user(vcpu
, &val
) < 0)
507 apic_debug("Can't read EOI MSR value: 0x%llx\n",
508 (unsigned long long)vcpu
->arch
.pv_eoi
.msr_val
);
512 static void pv_eoi_set_pending(struct kvm_vcpu
*vcpu
)
514 if (pv_eoi_put_user(vcpu
, KVM_PV_EOI_ENABLED
) < 0) {
515 apic_debug("Can't set EOI MSR value: 0x%llx\n",
516 (unsigned long long)vcpu
->arch
.pv_eoi
.msr_val
);
519 __set_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
);
522 static void pv_eoi_clr_pending(struct kvm_vcpu
*vcpu
)
524 if (pv_eoi_put_user(vcpu
, KVM_PV_EOI_DISABLED
) < 0) {
525 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
526 (unsigned long long)vcpu
->arch
.pv_eoi
.msr_val
);
529 __clear_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
);
532 static void apic_update_ppr(struct kvm_lapic
*apic
)
534 u32 tpr
, isrv
, ppr
, old_ppr
;
537 old_ppr
= kvm_lapic_get_reg(apic
, APIC_PROCPRI
);
538 tpr
= kvm_lapic_get_reg(apic
, APIC_TASKPRI
);
539 isr
= apic_find_highest_isr(apic
);
540 isrv
= (isr
!= -1) ? isr
: 0;
542 if ((tpr
& 0xf0) >= (isrv
& 0xf0))
547 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
548 apic
, ppr
, isr
, isrv
);
550 if (old_ppr
!= ppr
) {
551 kvm_lapic_set_reg(apic
, APIC_PROCPRI
, ppr
);
553 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
557 static void apic_set_tpr(struct kvm_lapic
*apic
, u32 tpr
)
559 kvm_lapic_set_reg(apic
, APIC_TASKPRI
, tpr
);
560 apic_update_ppr(apic
);
563 static bool kvm_apic_broadcast(struct kvm_lapic
*apic
, u32 mda
)
565 if (apic_x2apic_mode(apic
))
566 return mda
== X2APIC_BROADCAST
;
568 return GET_APIC_DEST_FIELD(mda
) == APIC_BROADCAST
;
571 static bool kvm_apic_match_physical_addr(struct kvm_lapic
*apic
, u32 mda
)
573 if (kvm_apic_broadcast(apic
, mda
))
576 if (apic_x2apic_mode(apic
))
577 return mda
== kvm_apic_id(apic
);
579 return mda
== SET_APIC_DEST_FIELD(kvm_apic_id(apic
));
582 static bool kvm_apic_match_logical_addr(struct kvm_lapic
*apic
, u32 mda
)
586 if (kvm_apic_broadcast(apic
, mda
))
589 logical_id
= kvm_lapic_get_reg(apic
, APIC_LDR
);
591 if (apic_x2apic_mode(apic
))
592 return ((logical_id
>> 16) == (mda
>> 16))
593 && (logical_id
& mda
& 0xffff) != 0;
595 logical_id
= GET_APIC_LOGICAL_ID(logical_id
);
596 mda
= GET_APIC_DEST_FIELD(mda
);
598 switch (kvm_lapic_get_reg(apic
, APIC_DFR
)) {
600 return (logical_id
& mda
) != 0;
601 case APIC_DFR_CLUSTER
:
602 return ((logical_id
>> 4) == (mda
>> 4))
603 && (logical_id
& mda
& 0xf) != 0;
605 apic_debug("Bad DFR vcpu %d: %08x\n",
606 apic
->vcpu
->vcpu_id
, kvm_lapic_get_reg(apic
, APIC_DFR
));
611 /* KVM APIC implementation has two quirks
612 * - dest always begins at 0 while xAPIC MDA has offset 24,
613 * - IOxAPIC messages have to be delivered (directly) to x2APIC.
615 static u32
kvm_apic_mda(unsigned int dest_id
, struct kvm_lapic
*source
,
616 struct kvm_lapic
*target
)
618 bool ipi
= source
!= NULL
;
619 bool x2apic_mda
= apic_x2apic_mode(ipi
? source
: target
);
621 if (!ipi
&& dest_id
== APIC_BROADCAST
&& x2apic_mda
)
622 return X2APIC_BROADCAST
;
624 return x2apic_mda
? dest_id
: SET_APIC_DEST_FIELD(dest_id
);
627 bool kvm_apic_match_dest(struct kvm_vcpu
*vcpu
, struct kvm_lapic
*source
,
628 int short_hand
, unsigned int dest
, int dest_mode
)
630 struct kvm_lapic
*target
= vcpu
->arch
.apic
;
631 u32 mda
= kvm_apic_mda(dest
, source
, target
);
633 apic_debug("target %p, source %p, dest 0x%x, "
634 "dest_mode 0x%x, short_hand 0x%x\n",
635 target
, source
, dest
, dest_mode
, short_hand
);
638 switch (short_hand
) {
639 case APIC_DEST_NOSHORT
:
640 if (dest_mode
== APIC_DEST_PHYSICAL
)
641 return kvm_apic_match_physical_addr(target
, mda
);
643 return kvm_apic_match_logical_addr(target
, mda
);
645 return target
== source
;
646 case APIC_DEST_ALLINC
:
648 case APIC_DEST_ALLBUT
:
649 return target
!= source
;
651 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
656 EXPORT_SYMBOL_GPL(kvm_apic_match_dest
);
658 int kvm_vector_to_index(u32 vector
, u32 dest_vcpus
,
659 const unsigned long *bitmap
, u32 bitmap_size
)
664 mod
= vector
% dest_vcpus
;
666 for (i
= 0; i
<= mod
; i
++) {
667 idx
= find_next_bit(bitmap
, bitmap_size
, idx
+ 1);
668 BUG_ON(idx
== bitmap_size
);
674 static void kvm_apic_disabled_lapic_found(struct kvm
*kvm
)
676 if (!kvm
->arch
.disabled_lapic_found
) {
677 kvm
->arch
.disabled_lapic_found
= true;
679 "Disabled LAPIC found during irq injection\n");
683 /* Return true if the interrupt can be handled by using *bitmap as index mask
684 * for valid destinations in *dst array.
685 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
686 * Note: we may have zero kvm_lapic destinations when we return true, which
687 * means that the interrupt should be dropped. In this case, *bitmap would be
688 * zero and *dst undefined.
690 static inline bool kvm_apic_map_get_dest_lapic(struct kvm
*kvm
,
691 struct kvm_lapic
**src
, struct kvm_lapic_irq
*irq
,
692 struct kvm_apic_map
*map
, struct kvm_lapic
***dst
,
693 unsigned long *bitmap
)
698 if (irq
->shorthand
== APIC_DEST_SELF
&& src
) {
702 } else if (irq
->shorthand
)
705 x2apic_ipi
= src
&& *src
&& apic_x2apic_mode(*src
);
706 if (irq
->dest_id
== (x2apic_ipi
? X2APIC_BROADCAST
: APIC_BROADCAST
))
712 if (irq
->dest_mode
== APIC_DEST_PHYSICAL
) {
713 if (irq
->dest_id
>= ARRAY_SIZE(map
->phys_map
)) {
716 *dst
= &map
->phys_map
[irq
->dest_id
];
723 if (!kvm_apic_map_get_logical_dest(map
, irq
->dest_id
, dst
,
727 if (!kvm_lowest_prio_delivery(irq
))
730 if (!kvm_vector_hashing_enabled()) {
732 for_each_set_bit(i
, bitmap
, 16) {
737 else if (kvm_apic_compare_prio((*dst
)[i
]->vcpu
,
738 (*dst
)[lowest
]->vcpu
) < 0)
745 lowest
= kvm_vector_to_index(irq
->vector
, hweight16(*bitmap
),
748 if (!(*dst
)[lowest
]) {
749 kvm_apic_disabled_lapic_found(kvm
);
755 *bitmap
= (lowest
>= 0) ? 1 << lowest
: 0;
760 bool kvm_irq_delivery_to_apic_fast(struct kvm
*kvm
, struct kvm_lapic
*src
,
761 struct kvm_lapic_irq
*irq
, int *r
, struct dest_map
*dest_map
)
763 struct kvm_apic_map
*map
;
764 unsigned long bitmap
;
765 struct kvm_lapic
**dst
= NULL
;
771 if (irq
->shorthand
== APIC_DEST_SELF
) {
772 *r
= kvm_apic_set_irq(src
->vcpu
, irq
, dest_map
);
777 map
= rcu_dereference(kvm
->arch
.apic_map
);
779 ret
= kvm_apic_map_get_dest_lapic(kvm
, &src
, irq
, map
, &dst
, &bitmap
);
781 for_each_set_bit(i
, &bitmap
, 16) {
786 *r
+= kvm_apic_set_irq(dst
[i
]->vcpu
, irq
, dest_map
);
794 * This routine tries to handler interrupts in posted mode, here is how
795 * it deals with different cases:
796 * - For single-destination interrupts, handle it in posted mode
797 * - Else if vector hashing is enabled and it is a lowest-priority
798 * interrupt, handle it in posted mode and use the following mechanism
799 * to find the destinaiton vCPU.
800 * 1. For lowest-priority interrupts, store all the possible
801 * destination vCPUs in an array.
802 * 2. Use "guest vector % max number of destination vCPUs" to find
803 * the right destination vCPU in the array for the lowest-priority
805 * - Otherwise, use remapped mode to inject the interrupt.
807 bool kvm_intr_is_single_vcpu_fast(struct kvm
*kvm
, struct kvm_lapic_irq
*irq
,
808 struct kvm_vcpu
**dest_vcpu
)
810 struct kvm_apic_map
*map
;
811 unsigned long bitmap
;
812 struct kvm_lapic
**dst
= NULL
;
819 map
= rcu_dereference(kvm
->arch
.apic_map
);
821 if (kvm_apic_map_get_dest_lapic(kvm
, NULL
, irq
, map
, &dst
, &bitmap
) &&
822 hweight16(bitmap
) == 1) {
823 unsigned long i
= find_first_bit(&bitmap
, 16);
826 *dest_vcpu
= dst
[i
]->vcpu
;
836 * Add a pending IRQ into lapic.
837 * Return 1 if successfully added and 0 if discarded.
839 static int __apic_accept_irq(struct kvm_lapic
*apic
, int delivery_mode
,
840 int vector
, int level
, int trig_mode
,
841 struct dest_map
*dest_map
)
844 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
846 trace_kvm_apic_accept_irq(vcpu
->vcpu_id
, delivery_mode
,
848 switch (delivery_mode
) {
850 vcpu
->arch
.apic_arb_prio
++;
852 if (unlikely(trig_mode
&& !level
))
855 /* FIXME add logic for vcpu on reset */
856 if (unlikely(!apic_enabled(apic
)))
862 __set_bit(vcpu
->vcpu_id
, dest_map
->map
);
863 dest_map
->vectors
[vcpu
->vcpu_id
] = vector
;
866 if (apic_test_vector(vector
, apic
->regs
+ APIC_TMR
) != !!trig_mode
) {
868 kvm_lapic_set_vector(vector
, apic
->regs
+ APIC_TMR
);
870 apic_clear_vector(vector
, apic
->regs
+ APIC_TMR
);
873 if (vcpu
->arch
.apicv_active
)
874 kvm_x86_ops
->deliver_posted_interrupt(vcpu
, vector
);
876 kvm_lapic_set_irr(vector
, apic
);
878 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
885 vcpu
->arch
.pv
.pv_unhalted
= 1;
886 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
892 kvm_make_request(KVM_REQ_SMI
, vcpu
);
898 kvm_inject_nmi(vcpu
);
903 if (!trig_mode
|| level
) {
905 /* assumes that there are only KVM_APIC_INIT/SIPI */
906 apic
->pending_events
= (1UL << KVM_APIC_INIT
);
907 /* make sure pending_events is visible before sending
910 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
913 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
918 case APIC_DM_STARTUP
:
919 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
920 vcpu
->vcpu_id
, vector
);
922 apic
->sipi_vector
= vector
;
923 /* make sure sipi_vector is visible for the receiver */
925 set_bit(KVM_APIC_SIPI
, &apic
->pending_events
);
926 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
932 * Should only be called by kvm_apic_local_deliver() with LVT0,
933 * before NMI watchdog was enabled. Already handled by
934 * kvm_apic_accept_pic_intr().
939 printk(KERN_ERR
"TODO: unsupported delivery mode %x\n",
946 int kvm_apic_compare_prio(struct kvm_vcpu
*vcpu1
, struct kvm_vcpu
*vcpu2
)
948 return vcpu1
->arch
.apic_arb_prio
- vcpu2
->arch
.apic_arb_prio
;
951 static bool kvm_ioapic_handles_vector(struct kvm_lapic
*apic
, int vector
)
953 return test_bit(vector
, apic
->vcpu
->arch
.ioapic_handled_vectors
);
956 static void kvm_ioapic_send_eoi(struct kvm_lapic
*apic
, int vector
)
960 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
961 if (!kvm_ioapic_handles_vector(apic
, vector
))
964 /* Request a KVM exit to inform the userspace IOAPIC. */
965 if (irqchip_split(apic
->vcpu
->kvm
)) {
966 apic
->vcpu
->arch
.pending_ioapic_eoi
= vector
;
967 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT
, apic
->vcpu
);
971 if (apic_test_vector(vector
, apic
->regs
+ APIC_TMR
))
972 trigger_mode
= IOAPIC_LEVEL_TRIG
;
974 trigger_mode
= IOAPIC_EDGE_TRIG
;
976 kvm_ioapic_update_eoi(apic
->vcpu
, vector
, trigger_mode
);
979 static int apic_set_eoi(struct kvm_lapic
*apic
)
981 int vector
= apic_find_highest_isr(apic
);
983 trace_kvm_eoi(apic
, vector
);
986 * Not every write EOI will has corresponding ISR,
987 * one example is when Kernel check timer on setup_IO_APIC
992 apic_clear_isr(vector
, apic
);
993 apic_update_ppr(apic
);
995 if (test_bit(vector
, vcpu_to_synic(apic
->vcpu
)->vec_bitmap
))
996 kvm_hv_synic_send_eoi(apic
->vcpu
, vector
);
998 kvm_ioapic_send_eoi(apic
, vector
);
999 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
1004 * this interface assumes a trap-like exit, which has already finished
1005 * desired side effect including vISR and vPPR update.
1007 void kvm_apic_set_eoi_accelerated(struct kvm_vcpu
*vcpu
, int vector
)
1009 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1011 trace_kvm_eoi(apic
, vector
);
1013 kvm_ioapic_send_eoi(apic
, vector
);
1014 kvm_make_request(KVM_REQ_EVENT
, apic
->vcpu
);
1016 EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated
);
1018 static void apic_send_ipi(struct kvm_lapic
*apic
)
1020 u32 icr_low
= kvm_lapic_get_reg(apic
, APIC_ICR
);
1021 u32 icr_high
= kvm_lapic_get_reg(apic
, APIC_ICR2
);
1022 struct kvm_lapic_irq irq
;
1024 irq
.vector
= icr_low
& APIC_VECTOR_MASK
;
1025 irq
.delivery_mode
= icr_low
& APIC_MODE_MASK
;
1026 irq
.dest_mode
= icr_low
& APIC_DEST_MASK
;
1027 irq
.level
= (icr_low
& APIC_INT_ASSERT
) != 0;
1028 irq
.trig_mode
= icr_low
& APIC_INT_LEVELTRIG
;
1029 irq
.shorthand
= icr_low
& APIC_SHORT_MASK
;
1030 irq
.msi_redir_hint
= false;
1031 if (apic_x2apic_mode(apic
))
1032 irq
.dest_id
= icr_high
;
1034 irq
.dest_id
= GET_APIC_DEST_FIELD(icr_high
);
1036 trace_kvm_apic_ipi(icr_low
, irq
.dest_id
);
1038 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1039 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
1040 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1041 "msi_redir_hint 0x%x\n",
1042 icr_high
, icr_low
, irq
.shorthand
, irq
.dest_id
,
1043 irq
.trig_mode
, irq
.level
, irq
.dest_mode
, irq
.delivery_mode
,
1044 irq
.vector
, irq
.msi_redir_hint
);
1046 kvm_irq_delivery_to_apic(apic
->vcpu
->kvm
, apic
, &irq
, NULL
);
1049 static u32
apic_get_tmcct(struct kvm_lapic
*apic
)
1055 ASSERT(apic
!= NULL
);
1057 /* if initial count is 0, current count should also be 0 */
1058 if (kvm_lapic_get_reg(apic
, APIC_TMICT
) == 0 ||
1059 apic
->lapic_timer
.period
== 0)
1062 remaining
= hrtimer_get_remaining(&apic
->lapic_timer
.timer
);
1063 if (ktime_to_ns(remaining
) < 0)
1064 remaining
= ktime_set(0, 0);
1066 ns
= mod_64(ktime_to_ns(remaining
), apic
->lapic_timer
.period
);
1067 tmcct
= div64_u64(ns
,
1068 (APIC_BUS_CYCLE_NS
* apic
->divide_count
));
1073 static void __report_tpr_access(struct kvm_lapic
*apic
, bool write
)
1075 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1076 struct kvm_run
*run
= vcpu
->run
;
1078 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS
, vcpu
);
1079 run
->tpr_access
.rip
= kvm_rip_read(vcpu
);
1080 run
->tpr_access
.is_write
= write
;
1083 static inline void report_tpr_access(struct kvm_lapic
*apic
, bool write
)
1085 if (apic
->vcpu
->arch
.tpr_access_reporting
)
1086 __report_tpr_access(apic
, write
);
1089 static u32
__apic_read(struct kvm_lapic
*apic
, unsigned int offset
)
1093 if (offset
>= LAPIC_MMIO_LENGTH
)
1098 if (apic_x2apic_mode(apic
))
1099 val
= kvm_apic_id(apic
);
1101 val
= kvm_apic_id(apic
) << 24;
1104 apic_debug("Access APIC ARBPRI register which is for P6\n");
1107 case APIC_TMCCT
: /* Timer CCR */
1108 if (apic_lvtt_tscdeadline(apic
))
1111 val
= apic_get_tmcct(apic
);
1114 apic_update_ppr(apic
);
1115 val
= kvm_lapic_get_reg(apic
, offset
);
1118 report_tpr_access(apic
, false);
1121 val
= kvm_lapic_get_reg(apic
, offset
);
1128 static inline struct kvm_lapic
*to_lapic(struct kvm_io_device
*dev
)
1130 return container_of(dev
, struct kvm_lapic
, dev
);
1133 int kvm_lapic_reg_read(struct kvm_lapic
*apic
, u32 offset
, int len
,
1136 unsigned char alignment
= offset
& 0xf;
1138 /* this bitmask has a bit cleared for each reserved register */
1139 static const u64 rmask
= 0x43ff01ffffffe70cULL
;
1141 if ((alignment
+ len
) > 4) {
1142 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1147 if (offset
> 0x3f0 || !(rmask
& (1ULL << (offset
>> 4)))) {
1148 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1153 result
= __apic_read(apic
, offset
& ~0xf);
1155 trace_kvm_apic_read(offset
, result
);
1161 memcpy(data
, (char *)&result
+ alignment
, len
);
1164 printk(KERN_ERR
"Local APIC read with len = %x, "
1165 "should be 1,2, or 4 instead\n", len
);
1170 EXPORT_SYMBOL_GPL(kvm_lapic_reg_read
);
1172 static int apic_mmio_in_range(struct kvm_lapic
*apic
, gpa_t addr
)
1174 return kvm_apic_hw_enabled(apic
) &&
1175 addr
>= apic
->base_address
&&
1176 addr
< apic
->base_address
+ LAPIC_MMIO_LENGTH
;
1179 static int apic_mmio_read(struct kvm_vcpu
*vcpu
, struct kvm_io_device
*this,
1180 gpa_t address
, int len
, void *data
)
1182 struct kvm_lapic
*apic
= to_lapic(this);
1183 u32 offset
= address
- apic
->base_address
;
1185 if (!apic_mmio_in_range(apic
, address
))
1188 kvm_lapic_reg_read(apic
, offset
, len
, data
);
1193 static void update_divide_count(struct kvm_lapic
*apic
)
1195 u32 tmp1
, tmp2
, tdcr
;
1197 tdcr
= kvm_lapic_get_reg(apic
, APIC_TDCR
);
1199 tmp2
= ((tmp1
& 0x3) | ((tmp1
& 0x8) >> 1)) + 1;
1200 apic
->divide_count
= 0x1 << (tmp2
& 0x7);
1202 apic_debug("timer divide count is 0x%x\n",
1203 apic
->divide_count
);
1206 static void apic_update_lvtt(struct kvm_lapic
*apic
)
1208 u32 timer_mode
= kvm_lapic_get_reg(apic
, APIC_LVTT
) &
1209 apic
->lapic_timer
.timer_mode_mask
;
1211 if (apic
->lapic_timer
.timer_mode
!= timer_mode
) {
1212 apic
->lapic_timer
.timer_mode
= timer_mode
;
1213 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1217 static void apic_timer_expired(struct kvm_lapic
*apic
)
1219 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1220 struct swait_queue_head
*q
= &vcpu
->wq
;
1221 struct kvm_timer
*ktimer
= &apic
->lapic_timer
;
1223 if (atomic_read(&apic
->lapic_timer
.pending
))
1226 atomic_inc(&apic
->lapic_timer
.pending
);
1227 kvm_set_pending_timer(vcpu
);
1229 if (swait_active(q
))
1232 if (apic_lvtt_tscdeadline(apic
))
1233 ktimer
->expired_tscdeadline
= ktimer
->tscdeadline
;
1237 * On APICv, this test will cause a busy wait
1238 * during a higher-priority task.
1241 static bool lapic_timer_int_injected(struct kvm_vcpu
*vcpu
)
1243 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1244 u32 reg
= kvm_lapic_get_reg(apic
, APIC_LVTT
);
1246 if (kvm_apic_hw_enabled(apic
)) {
1247 int vec
= reg
& APIC_VECTOR_MASK
;
1248 void *bitmap
= apic
->regs
+ APIC_ISR
;
1250 if (vcpu
->arch
.apicv_active
)
1251 bitmap
= apic
->regs
+ APIC_IRR
;
1253 if (apic_test_vector(vec
, bitmap
))
1259 void wait_lapic_expire(struct kvm_vcpu
*vcpu
)
1261 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1262 u64 guest_tsc
, tsc_deadline
;
1264 if (!lapic_in_kernel(vcpu
))
1267 if (apic
->lapic_timer
.expired_tscdeadline
== 0)
1270 if (!lapic_timer_int_injected(vcpu
))
1273 tsc_deadline
= apic
->lapic_timer
.expired_tscdeadline
;
1274 apic
->lapic_timer
.expired_tscdeadline
= 0;
1275 guest_tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
1276 trace_kvm_wait_lapic_expire(vcpu
->vcpu_id
, guest_tsc
- tsc_deadline
);
1278 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1279 if (guest_tsc
< tsc_deadline
)
1280 __delay(tsc_deadline
- guest_tsc
);
1283 static void start_sw_tscdeadline(struct kvm_lapic
*apic
)
1285 u64 guest_tsc
, tscdeadline
= apic
->lapic_timer
.tscdeadline
;
1288 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
1289 unsigned long this_tsc_khz
= vcpu
->arch
.virtual_tsc_khz
;
1290 unsigned long flags
;
1293 if (unlikely(!tscdeadline
|| !this_tsc_khz
))
1296 local_irq_save(flags
);
1298 now
= apic
->lapic_timer
.timer
.base
->get_time();
1299 guest_tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
1300 if (likely(tscdeadline
> guest_tsc
)) {
1301 ns
= (tscdeadline
- guest_tsc
) * 1000000ULL;
1302 do_div(ns
, this_tsc_khz
);
1303 expire
= ktime_add_ns(now
, ns
);
1304 expire
= ktime_sub_ns(expire
, lapic_timer_advance_ns
);
1305 hrtimer_start(&apic
->lapic_timer
.timer
,
1306 expire
, HRTIMER_MODE_ABS_PINNED
);
1308 apic_timer_expired(apic
);
1310 local_irq_restore(flags
);
1313 bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu
*vcpu
)
1315 return vcpu
->arch
.apic
->lapic_timer
.hv_timer_in_use
;
1317 EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use
);
1319 static void cancel_hv_tscdeadline(struct kvm_lapic
*apic
)
1321 kvm_x86_ops
->cancel_hv_timer(apic
->vcpu
);
1322 apic
->lapic_timer
.hv_timer_in_use
= false;
1325 void kvm_lapic_expired_hv_timer(struct kvm_vcpu
*vcpu
)
1327 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1329 WARN_ON(!apic
->lapic_timer
.hv_timer_in_use
);
1330 WARN_ON(swait_active(&vcpu
->wq
));
1331 cancel_hv_tscdeadline(apic
);
1332 apic_timer_expired(apic
);
1334 EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer
);
1336 static bool start_hv_tscdeadline(struct kvm_lapic
*apic
)
1338 u64 tscdeadline
= apic
->lapic_timer
.tscdeadline
;
1340 if (atomic_read(&apic
->lapic_timer
.pending
) ||
1341 kvm_x86_ops
->set_hv_timer(apic
->vcpu
, tscdeadline
)) {
1342 if (apic
->lapic_timer
.hv_timer_in_use
)
1343 cancel_hv_tscdeadline(apic
);
1345 apic
->lapic_timer
.hv_timer_in_use
= true;
1346 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1348 /* In case the sw timer triggered in the window */
1349 if (atomic_read(&apic
->lapic_timer
.pending
))
1350 cancel_hv_tscdeadline(apic
);
1352 trace_kvm_hv_timer_state(apic
->vcpu
->vcpu_id
,
1353 apic
->lapic_timer
.hv_timer_in_use
);
1354 return apic
->lapic_timer
.hv_timer_in_use
;
1357 void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu
*vcpu
)
1359 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1361 WARN_ON(apic
->lapic_timer
.hv_timer_in_use
);
1363 if (apic_lvtt_tscdeadline(apic
))
1364 start_hv_tscdeadline(apic
);
1366 EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer
);
1368 void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu
*vcpu
)
1370 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1372 /* Possibly the TSC deadline timer is not enabled yet */
1373 if (!apic
->lapic_timer
.hv_timer_in_use
)
1376 cancel_hv_tscdeadline(apic
);
1378 if (atomic_read(&apic
->lapic_timer
.pending
))
1381 start_sw_tscdeadline(apic
);
1383 EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer
);
1385 static void start_apic_timer(struct kvm_lapic
*apic
)
1389 atomic_set(&apic
->lapic_timer
.pending
, 0);
1391 if (apic_lvtt_period(apic
) || apic_lvtt_oneshot(apic
)) {
1392 /* lapic timer in oneshot or periodic mode */
1393 now
= apic
->lapic_timer
.timer
.base
->get_time();
1394 apic
->lapic_timer
.period
= (u64
)kvm_lapic_get_reg(apic
, APIC_TMICT
)
1395 * APIC_BUS_CYCLE_NS
* apic
->divide_count
;
1397 if (!apic
->lapic_timer
.period
)
1400 * Do not allow the guest to program periodic timers with small
1401 * interval, since the hrtimers are not throttled by the host
1404 if (apic_lvtt_period(apic
)) {
1405 s64 min_period
= min_timer_period_us
* 1000LL;
1407 if (apic
->lapic_timer
.period
< min_period
) {
1408 pr_info_ratelimited(
1409 "kvm: vcpu %i: requested %lld ns "
1410 "lapic timer period limited to %lld ns\n",
1411 apic
->vcpu
->vcpu_id
,
1412 apic
->lapic_timer
.period
, min_period
);
1413 apic
->lapic_timer
.period
= min_period
;
1417 hrtimer_start(&apic
->lapic_timer
.timer
,
1418 ktime_add_ns(now
, apic
->lapic_timer
.period
),
1419 HRTIMER_MODE_ABS_PINNED
);
1421 apic_debug("%s: bus cycle is %" PRId64
"ns, now 0x%016"
1423 "timer initial count 0x%x, period %lldns, "
1424 "expire @ 0x%016" PRIx64
".\n", __func__
,
1425 APIC_BUS_CYCLE_NS
, ktime_to_ns(now
),
1426 kvm_lapic_get_reg(apic
, APIC_TMICT
),
1427 apic
->lapic_timer
.period
,
1428 ktime_to_ns(ktime_add_ns(now
,
1429 apic
->lapic_timer
.period
)));
1430 } else if (apic_lvtt_tscdeadline(apic
)) {
1431 if (!(kvm_x86_ops
->set_hv_timer
&& start_hv_tscdeadline(apic
)))
1432 start_sw_tscdeadline(apic
);
1436 static void apic_manage_nmi_watchdog(struct kvm_lapic
*apic
, u32 lvt0_val
)
1438 bool lvt0_in_nmi_mode
= apic_lvt_nmi_mode(lvt0_val
);
1440 if (apic
->lvt0_in_nmi_mode
!= lvt0_in_nmi_mode
) {
1441 apic
->lvt0_in_nmi_mode
= lvt0_in_nmi_mode
;
1442 if (lvt0_in_nmi_mode
) {
1443 apic_debug("Receive NMI setting on APIC_LVT0 "
1444 "for cpu %d\n", apic
->vcpu
->vcpu_id
);
1445 atomic_inc(&apic
->vcpu
->kvm
->arch
.vapics_in_nmi_mode
);
1447 atomic_dec(&apic
->vcpu
->kvm
->arch
.vapics_in_nmi_mode
);
1451 int kvm_lapic_reg_write(struct kvm_lapic
*apic
, u32 reg
, u32 val
)
1455 trace_kvm_apic_write(reg
, val
);
1458 case APIC_ID
: /* Local APIC ID */
1459 if (!apic_x2apic_mode(apic
))
1460 kvm_apic_set_id(apic
, val
>> 24);
1466 report_tpr_access(apic
, true);
1467 apic_set_tpr(apic
, val
& 0xff);
1475 if (!apic_x2apic_mode(apic
))
1476 kvm_apic_set_ldr(apic
, val
& APIC_LDR_MASK
);
1482 if (!apic_x2apic_mode(apic
)) {
1483 kvm_lapic_set_reg(apic
, APIC_DFR
, val
| 0x0FFFFFFF);
1484 recalculate_apic_map(apic
->vcpu
->kvm
);
1491 if (kvm_lapic_get_reg(apic
, APIC_LVR
) & APIC_LVR_DIRECTED_EOI
)
1492 mask
|= APIC_SPIV_DIRECTED_EOI
;
1493 apic_set_spiv(apic
, val
& mask
);
1494 if (!(val
& APIC_SPIV_APIC_ENABLED
)) {
1498 for (i
= 0; i
< KVM_APIC_LVT_NUM
; i
++) {
1499 lvt_val
= kvm_lapic_get_reg(apic
,
1500 APIC_LVTT
+ 0x10 * i
);
1501 kvm_lapic_set_reg(apic
, APIC_LVTT
+ 0x10 * i
,
1502 lvt_val
| APIC_LVT_MASKED
);
1504 apic_update_lvtt(apic
);
1505 atomic_set(&apic
->lapic_timer
.pending
, 0);
1511 /* No delay here, so we always clear the pending bit */
1512 kvm_lapic_set_reg(apic
, APIC_ICR
, val
& ~(1 << 12));
1513 apic_send_ipi(apic
);
1517 if (!apic_x2apic_mode(apic
))
1519 kvm_lapic_set_reg(apic
, APIC_ICR2
, val
);
1523 apic_manage_nmi_watchdog(apic
, val
);
1528 /* TODO: Check vector */
1529 if (!kvm_apic_sw_enabled(apic
))
1530 val
|= APIC_LVT_MASKED
;
1532 val
&= apic_lvt_mask
[(reg
- APIC_LVTT
) >> 4];
1533 kvm_lapic_set_reg(apic
, reg
, val
);
1538 if (!kvm_apic_sw_enabled(apic
))
1539 val
|= APIC_LVT_MASKED
;
1540 val
&= (apic_lvt_mask
[0] | apic
->lapic_timer
.timer_mode_mask
);
1541 kvm_lapic_set_reg(apic
, APIC_LVTT
, val
);
1542 apic_update_lvtt(apic
);
1546 if (apic_lvtt_tscdeadline(apic
))
1549 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1550 kvm_lapic_set_reg(apic
, APIC_TMICT
, val
);
1551 start_apic_timer(apic
);
1556 apic_debug("KVM_WRITE:TDCR %x\n", val
);
1557 kvm_lapic_set_reg(apic
, APIC_TDCR
, val
);
1558 update_divide_count(apic
);
1562 if (apic_x2apic_mode(apic
) && val
!= 0) {
1563 apic_debug("KVM_WRITE:ESR not zero %x\n", val
);
1569 if (apic_x2apic_mode(apic
)) {
1570 kvm_lapic_reg_write(apic
, APIC_ICR
, 0x40000 | (val
& 0xff));
1579 apic_debug("Local APIC Write to read-only register %x\n", reg
);
1582 EXPORT_SYMBOL_GPL(kvm_lapic_reg_write
);
1584 static int apic_mmio_write(struct kvm_vcpu
*vcpu
, struct kvm_io_device
*this,
1585 gpa_t address
, int len
, const void *data
)
1587 struct kvm_lapic
*apic
= to_lapic(this);
1588 unsigned int offset
= address
- apic
->base_address
;
1591 if (!apic_mmio_in_range(apic
, address
))
1595 * APIC register must be aligned on 128-bits boundary.
1596 * 32/64/128 bits registers must be accessed thru 32 bits.
1599 if (len
!= 4 || (offset
& 0xf)) {
1600 /* Don't shout loud, $infamous_os would cause only noise. */
1601 apic_debug("apic write: bad size=%d %lx\n", len
, (long)address
);
1607 /* too common printing */
1608 if (offset
!= APIC_EOI
)
1609 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1610 "0x%x\n", __func__
, offset
, len
, val
);
1612 kvm_lapic_reg_write(apic
, offset
& 0xff0, val
);
1617 void kvm_lapic_set_eoi(struct kvm_vcpu
*vcpu
)
1619 kvm_lapic_reg_write(vcpu
->arch
.apic
, APIC_EOI
, 0);
1621 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi
);
1623 /* emulate APIC access in a trap manner */
1624 void kvm_apic_write_nodecode(struct kvm_vcpu
*vcpu
, u32 offset
)
1628 /* hw has done the conditional check and inst decode */
1631 kvm_lapic_reg_read(vcpu
->arch
.apic
, offset
, 4, &val
);
1633 /* TODO: optimize to just emulate side effect w/o one more write */
1634 kvm_lapic_reg_write(vcpu
->arch
.apic
, offset
, val
);
1636 EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode
);
1638 void kvm_free_lapic(struct kvm_vcpu
*vcpu
)
1640 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1642 if (!vcpu
->arch
.apic
)
1645 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1647 if (!(vcpu
->arch
.apic_base
& MSR_IA32_APICBASE_ENABLE
))
1648 static_key_slow_dec_deferred(&apic_hw_disabled
);
1650 if (!apic
->sw_enabled
)
1651 static_key_slow_dec_deferred(&apic_sw_disabled
);
1654 free_page((unsigned long)apic
->regs
);
1660 *----------------------------------------------------------------------
1662 *----------------------------------------------------------------------
1665 u64
kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu
*vcpu
)
1667 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1669 if (!lapic_in_kernel(vcpu
) || apic_lvtt_oneshot(apic
) ||
1670 apic_lvtt_period(apic
))
1673 return apic
->lapic_timer
.tscdeadline
;
1676 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu
*vcpu
, u64 data
)
1678 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1680 if (!lapic_in_kernel(vcpu
) || apic_lvtt_oneshot(apic
) ||
1681 apic_lvtt_period(apic
))
1684 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1685 apic
->lapic_timer
.tscdeadline
= data
;
1686 start_apic_timer(apic
);
1689 void kvm_lapic_set_tpr(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
1691 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1693 apic_set_tpr(apic
, ((cr8
& 0x0f) << 4)
1694 | (kvm_lapic_get_reg(apic
, APIC_TASKPRI
) & 4));
1697 u64
kvm_lapic_get_cr8(struct kvm_vcpu
*vcpu
)
1701 tpr
= (u64
) kvm_lapic_get_reg(vcpu
->arch
.apic
, APIC_TASKPRI
);
1703 return (tpr
& 0xf0) >> 4;
1706 void kvm_lapic_set_base(struct kvm_vcpu
*vcpu
, u64 value
)
1708 u64 old_value
= vcpu
->arch
.apic_base
;
1709 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1712 value
|= MSR_IA32_APICBASE_BSP
;
1713 vcpu
->arch
.apic_base
= value
;
1717 vcpu
->arch
.apic_base
= value
;
1719 /* update jump label if enable bit changes */
1720 if ((old_value
^ value
) & MSR_IA32_APICBASE_ENABLE
) {
1721 if (value
& MSR_IA32_APICBASE_ENABLE
)
1722 static_key_slow_dec_deferred(&apic_hw_disabled
);
1724 static_key_slow_inc(&apic_hw_disabled
.key
);
1725 recalculate_apic_map(vcpu
->kvm
);
1728 if ((old_value
^ value
) & X2APIC_ENABLE
) {
1729 if (value
& X2APIC_ENABLE
) {
1730 kvm_apic_set_x2apic_id(apic
, vcpu
->vcpu_id
);
1731 kvm_x86_ops
->set_virtual_x2apic_mode(vcpu
, true);
1733 kvm_x86_ops
->set_virtual_x2apic_mode(vcpu
, false);
1736 apic
->base_address
= apic
->vcpu
->arch
.apic_base
&
1737 MSR_IA32_APICBASE_BASE
;
1739 if ((value
& MSR_IA32_APICBASE_ENABLE
) &&
1740 apic
->base_address
!= APIC_DEFAULT_PHYS_BASE
)
1741 pr_warn_once("APIC base relocation is unsupported by KVM");
1743 /* with FSB delivery interrupt, we can restart APIC functionality */
1744 apic_debug("apic base msr is 0x%016" PRIx64
", and base address is "
1745 "0x%lx.\n", apic
->vcpu
->arch
.apic_base
, apic
->base_address
);
1749 void kvm_lapic_reset(struct kvm_vcpu
*vcpu
, bool init_event
)
1751 struct kvm_lapic
*apic
;
1754 apic_debug("%s\n", __func__
);
1757 apic
= vcpu
->arch
.apic
;
1758 ASSERT(apic
!= NULL
);
1760 /* Stop the timer in case it's a reset to an active apic */
1761 hrtimer_cancel(&apic
->lapic_timer
.timer
);
1764 kvm_apic_set_id(apic
, vcpu
->vcpu_id
);
1765 kvm_apic_set_version(apic
->vcpu
);
1767 for (i
= 0; i
< KVM_APIC_LVT_NUM
; i
++)
1768 kvm_lapic_set_reg(apic
, APIC_LVTT
+ 0x10 * i
, APIC_LVT_MASKED
);
1769 apic_update_lvtt(apic
);
1770 if (kvm_check_has_quirk(vcpu
->kvm
, KVM_X86_QUIRK_LINT0_REENABLED
))
1771 kvm_lapic_set_reg(apic
, APIC_LVT0
,
1772 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT
));
1773 apic_manage_nmi_watchdog(apic
, kvm_lapic_get_reg(apic
, APIC_LVT0
));
1775 kvm_lapic_set_reg(apic
, APIC_DFR
, 0xffffffffU
);
1776 apic_set_spiv(apic
, 0xff);
1777 kvm_lapic_set_reg(apic
, APIC_TASKPRI
, 0);
1778 if (!apic_x2apic_mode(apic
))
1779 kvm_apic_set_ldr(apic
, 0);
1780 kvm_lapic_set_reg(apic
, APIC_ESR
, 0);
1781 kvm_lapic_set_reg(apic
, APIC_ICR
, 0);
1782 kvm_lapic_set_reg(apic
, APIC_ICR2
, 0);
1783 kvm_lapic_set_reg(apic
, APIC_TDCR
, 0);
1784 kvm_lapic_set_reg(apic
, APIC_TMICT
, 0);
1785 for (i
= 0; i
< 8; i
++) {
1786 kvm_lapic_set_reg(apic
, APIC_IRR
+ 0x10 * i
, 0);
1787 kvm_lapic_set_reg(apic
, APIC_ISR
+ 0x10 * i
, 0);
1788 kvm_lapic_set_reg(apic
, APIC_TMR
+ 0x10 * i
, 0);
1790 apic
->irr_pending
= vcpu
->arch
.apicv_active
;
1791 apic
->isr_count
= vcpu
->arch
.apicv_active
? 1 : 0;
1792 apic
->highest_isr_cache
= -1;
1793 update_divide_count(apic
);
1794 atomic_set(&apic
->lapic_timer
.pending
, 0);
1795 if (kvm_vcpu_is_bsp(vcpu
))
1796 kvm_lapic_set_base(vcpu
,
1797 vcpu
->arch
.apic_base
| MSR_IA32_APICBASE_BSP
);
1798 vcpu
->arch
.pv_eoi
.msr_val
= 0;
1799 apic_update_ppr(apic
);
1801 vcpu
->arch
.apic_arb_prio
= 0;
1802 vcpu
->arch
.apic_attention
= 0;
1804 apic_debug("%s: vcpu=%p, id=%d, base_msr="
1805 "0x%016" PRIx64
", base_address=0x%0lx.\n", __func__
,
1806 vcpu
, kvm_apic_id(apic
),
1807 vcpu
->arch
.apic_base
, apic
->base_address
);
1811 *----------------------------------------------------------------------
1813 *----------------------------------------------------------------------
1816 static bool lapic_is_periodic(struct kvm_lapic
*apic
)
1818 return apic_lvtt_period(apic
);
1821 int apic_has_pending_timer(struct kvm_vcpu
*vcpu
)
1823 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1825 if (apic_enabled(apic
) && apic_lvt_enabled(apic
, APIC_LVTT
))
1826 return atomic_read(&apic
->lapic_timer
.pending
);
1831 int kvm_apic_local_deliver(struct kvm_lapic
*apic
, int lvt_type
)
1833 u32 reg
= kvm_lapic_get_reg(apic
, lvt_type
);
1834 int vector
, mode
, trig_mode
;
1836 if (kvm_apic_hw_enabled(apic
) && !(reg
& APIC_LVT_MASKED
)) {
1837 vector
= reg
& APIC_VECTOR_MASK
;
1838 mode
= reg
& APIC_MODE_MASK
;
1839 trig_mode
= reg
& APIC_LVT_LEVEL_TRIGGER
;
1840 return __apic_accept_irq(apic
, mode
, vector
, 1, trig_mode
,
1846 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu
*vcpu
)
1848 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1851 kvm_apic_local_deliver(apic
, APIC_LVT0
);
1854 static const struct kvm_io_device_ops apic_mmio_ops
= {
1855 .read
= apic_mmio_read
,
1856 .write
= apic_mmio_write
,
1859 static enum hrtimer_restart
apic_timer_fn(struct hrtimer
*data
)
1861 struct kvm_timer
*ktimer
= container_of(data
, struct kvm_timer
, timer
);
1862 struct kvm_lapic
*apic
= container_of(ktimer
, struct kvm_lapic
, lapic_timer
);
1864 apic_timer_expired(apic
);
1866 if (lapic_is_periodic(apic
)) {
1867 hrtimer_add_expires_ns(&ktimer
->timer
, ktimer
->period
);
1868 return HRTIMER_RESTART
;
1870 return HRTIMER_NORESTART
;
1873 int kvm_create_lapic(struct kvm_vcpu
*vcpu
)
1875 struct kvm_lapic
*apic
;
1877 ASSERT(vcpu
!= NULL
);
1878 apic_debug("apic_init %d\n", vcpu
->vcpu_id
);
1880 apic
= kzalloc(sizeof(*apic
), GFP_KERNEL
);
1884 vcpu
->arch
.apic
= apic
;
1886 apic
->regs
= (void *)get_zeroed_page(GFP_KERNEL
);
1888 printk(KERN_ERR
"malloc apic regs error for vcpu %x\n",
1890 goto nomem_free_apic
;
1894 hrtimer_init(&apic
->lapic_timer
.timer
, CLOCK_MONOTONIC
,
1895 HRTIMER_MODE_ABS_PINNED
);
1896 apic
->lapic_timer
.timer
.function
= apic_timer_fn
;
1899 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1900 * thinking that APIC satet has changed.
1902 vcpu
->arch
.apic_base
= MSR_IA32_APICBASE_ENABLE
;
1903 kvm_lapic_set_base(vcpu
,
1904 APIC_DEFAULT_PHYS_BASE
| MSR_IA32_APICBASE_ENABLE
);
1906 static_key_slow_inc(&apic_sw_disabled
.key
); /* sw disabled at reset */
1907 kvm_lapic_reset(vcpu
, false);
1908 kvm_iodevice_init(&apic
->dev
, &apic_mmio_ops
);
1917 int kvm_apic_has_interrupt(struct kvm_vcpu
*vcpu
)
1919 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1922 if (!apic_enabled(apic
))
1925 apic_update_ppr(apic
);
1926 highest_irr
= apic_find_highest_irr(apic
);
1927 if ((highest_irr
== -1) ||
1928 ((highest_irr
& 0xF0) <= kvm_lapic_get_reg(apic
, APIC_PROCPRI
)))
1933 int kvm_apic_accept_pic_intr(struct kvm_vcpu
*vcpu
)
1935 u32 lvt0
= kvm_lapic_get_reg(vcpu
->arch
.apic
, APIC_LVT0
);
1938 if (!kvm_apic_hw_enabled(vcpu
->arch
.apic
))
1940 if ((lvt0
& APIC_LVT_MASKED
) == 0 &&
1941 GET_APIC_DELIVERY_MODE(lvt0
) == APIC_MODE_EXTINT
)
1946 void kvm_inject_apic_timer_irqs(struct kvm_vcpu
*vcpu
)
1948 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1950 if (atomic_read(&apic
->lapic_timer
.pending
) > 0) {
1951 kvm_apic_local_deliver(apic
, APIC_LVTT
);
1952 if (apic_lvtt_tscdeadline(apic
))
1953 apic
->lapic_timer
.tscdeadline
= 0;
1954 atomic_set(&apic
->lapic_timer
.pending
, 0);
1958 int kvm_get_apic_interrupt(struct kvm_vcpu
*vcpu
)
1960 int vector
= kvm_apic_has_interrupt(vcpu
);
1961 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1967 * We get here even with APIC virtualization enabled, if doing
1968 * nested virtualization and L1 runs with the "acknowledge interrupt
1969 * on exit" mode. Then we cannot inject the interrupt via RVI,
1970 * because the process would deliver it through the IDT.
1973 apic_set_isr(vector
, apic
);
1974 apic_update_ppr(apic
);
1975 apic_clear_irr(vector
, apic
);
1977 if (test_bit(vector
, vcpu_to_synic(vcpu
)->auto_eoi_bitmap
)) {
1978 apic_clear_isr(vector
, apic
);
1979 apic_update_ppr(apic
);
1985 void kvm_apic_post_state_restore(struct kvm_vcpu
*vcpu
,
1986 struct kvm_lapic_state
*s
)
1988 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1990 kvm_lapic_set_base(vcpu
, vcpu
->arch
.apic_base
);
1991 /* set SPIV separately to get count of SW disabled APICs right */
1992 apic_set_spiv(apic
, *((u32
*)(s
->regs
+ APIC_SPIV
)));
1993 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1994 /* call kvm_apic_set_id() to put apic into apic_map */
1995 kvm_apic_set_id(apic
, kvm_apic_id(apic
));
1996 kvm_apic_set_version(vcpu
);
1998 apic_update_ppr(apic
);
1999 hrtimer_cancel(&apic
->lapic_timer
.timer
);
2000 apic_update_lvtt(apic
);
2001 apic_manage_nmi_watchdog(apic
, kvm_lapic_get_reg(apic
, APIC_LVT0
));
2002 update_divide_count(apic
);
2003 start_apic_timer(apic
);
2004 apic
->irr_pending
= true;
2005 apic
->isr_count
= vcpu
->arch
.apicv_active
?
2006 1 : count_vectors(apic
->regs
+ APIC_ISR
);
2007 apic
->highest_isr_cache
= -1;
2008 if (vcpu
->arch
.apicv_active
) {
2009 if (kvm_x86_ops
->apicv_post_state_restore
)
2010 kvm_x86_ops
->apicv_post_state_restore(vcpu
);
2011 kvm_x86_ops
->hwapic_irr_update(vcpu
,
2012 apic_find_highest_irr(apic
));
2013 kvm_x86_ops
->hwapic_isr_update(vcpu
,
2014 apic_find_highest_isr(apic
));
2016 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
2017 if (ioapic_in_kernel(vcpu
->kvm
))
2018 kvm_rtc_eoi_tracking_restore_one(vcpu
);
2020 vcpu
->arch
.apic_arb_prio
= 0;
2023 void __kvm_migrate_apic_timer(struct kvm_vcpu
*vcpu
)
2025 struct hrtimer
*timer
;
2027 if (!lapic_in_kernel(vcpu
))
2030 timer
= &vcpu
->arch
.apic
->lapic_timer
.timer
;
2031 if (hrtimer_cancel(timer
))
2032 hrtimer_start_expires(timer
, HRTIMER_MODE_ABS_PINNED
);
2036 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2038 * Detect whether guest triggered PV EOI since the
2039 * last entry. If yes, set EOI on guests's behalf.
2040 * Clear PV EOI in guest memory in any case.
2042 static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu
*vcpu
,
2043 struct kvm_lapic
*apic
)
2048 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2049 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2051 * KVM_APIC_PV_EOI_PENDING is unset:
2052 * -> host disabled PV EOI.
2053 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2054 * -> host enabled PV EOI, guest did not execute EOI yet.
2055 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2056 * -> host enabled PV EOI, guest executed EOI.
2058 BUG_ON(!pv_eoi_enabled(vcpu
));
2059 pending
= pv_eoi_get_pending(vcpu
);
2061 * Clear pending bit in any case: it will be set again on vmentry.
2062 * While this might not be ideal from performance point of view,
2063 * this makes sure pv eoi is only enabled when we know it's safe.
2065 pv_eoi_clr_pending(vcpu
);
2068 vector
= apic_set_eoi(apic
);
2069 trace_kvm_pv_eoi(apic
, vector
);
2072 void kvm_lapic_sync_from_vapic(struct kvm_vcpu
*vcpu
)
2076 if (test_bit(KVM_APIC_PV_EOI_PENDING
, &vcpu
->arch
.apic_attention
))
2077 apic_sync_pv_eoi_from_guest(vcpu
, vcpu
->arch
.apic
);
2079 if (!test_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
))
2082 if (kvm_read_guest_cached(vcpu
->kvm
, &vcpu
->arch
.apic
->vapic_cache
, &data
,
2086 apic_set_tpr(vcpu
->arch
.apic
, data
& 0xff);
2090 * apic_sync_pv_eoi_to_guest - called before vmentry
2092 * Detect whether it's safe to enable PV EOI and
2095 static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu
*vcpu
,
2096 struct kvm_lapic
*apic
)
2098 if (!pv_eoi_enabled(vcpu
) ||
2099 /* IRR set or many bits in ISR: could be nested. */
2100 apic
->irr_pending
||
2101 /* Cache not set: could be safe but we don't bother. */
2102 apic
->highest_isr_cache
== -1 ||
2103 /* Need EOI to update ioapic. */
2104 kvm_ioapic_handles_vector(apic
, apic
->highest_isr_cache
)) {
2106 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2107 * so we need not do anything here.
2112 pv_eoi_set_pending(apic
->vcpu
);
2115 void kvm_lapic_sync_to_vapic(struct kvm_vcpu
*vcpu
)
2118 int max_irr
, max_isr
;
2119 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2121 apic_sync_pv_eoi_to_guest(vcpu
, apic
);
2123 if (!test_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
))
2126 tpr
= kvm_lapic_get_reg(apic
, APIC_TASKPRI
) & 0xff;
2127 max_irr
= apic_find_highest_irr(apic
);
2130 max_isr
= apic_find_highest_isr(apic
);
2133 data
= (tpr
& 0xff) | ((max_isr
& 0xf0) << 8) | (max_irr
<< 24);
2135 kvm_write_guest_cached(vcpu
->kvm
, &vcpu
->arch
.apic
->vapic_cache
, &data
,
2139 int kvm_lapic_set_vapic_addr(struct kvm_vcpu
*vcpu
, gpa_t vapic_addr
)
2142 if (kvm_gfn_to_hva_cache_init(vcpu
->kvm
,
2143 &vcpu
->arch
.apic
->vapic_cache
,
2144 vapic_addr
, sizeof(u32
)))
2146 __set_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
);
2148 __clear_bit(KVM_APIC_CHECK_VAPIC
, &vcpu
->arch
.apic_attention
);
2151 vcpu
->arch
.apic
->vapic_addr
= vapic_addr
;
2155 int kvm_x2apic_msr_write(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
2157 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2158 u32 reg
= (msr
- APIC_BASE_MSR
) << 4;
2160 if (!lapic_in_kernel(vcpu
) || !apic_x2apic_mode(apic
))
2163 if (reg
== APIC_ICR2
)
2166 /* if this is ICR write vector before command */
2167 if (reg
== APIC_ICR
)
2168 kvm_lapic_reg_write(apic
, APIC_ICR2
, (u32
)(data
>> 32));
2169 return kvm_lapic_reg_write(apic
, reg
, (u32
)data
);
2172 int kvm_x2apic_msr_read(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*data
)
2174 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2175 u32 reg
= (msr
- APIC_BASE_MSR
) << 4, low
, high
= 0;
2177 if (!lapic_in_kernel(vcpu
) || !apic_x2apic_mode(apic
))
2180 if (reg
== APIC_DFR
|| reg
== APIC_ICR2
) {
2181 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2186 if (kvm_lapic_reg_read(apic
, reg
, 4, &low
))
2188 if (reg
== APIC_ICR
)
2189 kvm_lapic_reg_read(apic
, APIC_ICR2
, 4, &high
);
2191 *data
= (((u64
)high
) << 32) | low
;
2196 int kvm_hv_vapic_msr_write(struct kvm_vcpu
*vcpu
, u32 reg
, u64 data
)
2198 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2200 if (!lapic_in_kernel(vcpu
))
2203 /* if this is ICR write vector before command */
2204 if (reg
== APIC_ICR
)
2205 kvm_lapic_reg_write(apic
, APIC_ICR2
, (u32
)(data
>> 32));
2206 return kvm_lapic_reg_write(apic
, reg
, (u32
)data
);
2209 int kvm_hv_vapic_msr_read(struct kvm_vcpu
*vcpu
, u32 reg
, u64
*data
)
2211 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2214 if (!lapic_in_kernel(vcpu
))
2217 if (kvm_lapic_reg_read(apic
, reg
, 4, &low
))
2219 if (reg
== APIC_ICR
)
2220 kvm_lapic_reg_read(apic
, APIC_ICR2
, 4, &high
);
2222 *data
= (((u64
)high
) << 32) | low
;
2227 int kvm_lapic_enable_pv_eoi(struct kvm_vcpu
*vcpu
, u64 data
)
2229 u64 addr
= data
& ~KVM_MSR_ENABLED
;
2230 if (!IS_ALIGNED(addr
, 4))
2233 vcpu
->arch
.pv_eoi
.msr_val
= data
;
2234 if (!pv_eoi_enabled(vcpu
))
2236 return kvm_gfn_to_hva_cache_init(vcpu
->kvm
, &vcpu
->arch
.pv_eoi
.data
,
2240 void kvm_apic_accept_events(struct kvm_vcpu
*vcpu
)
2242 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2246 if (!lapic_in_kernel(vcpu
) || !apic
->pending_events
)
2250 * INITs are latched while in SMM. Because an SMM CPU cannot
2251 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2252 * and delay processing of INIT until the next RSM.
2255 WARN_ON_ONCE(vcpu
->arch
.mp_state
== KVM_MP_STATE_INIT_RECEIVED
);
2256 if (test_bit(KVM_APIC_SIPI
, &apic
->pending_events
))
2257 clear_bit(KVM_APIC_SIPI
, &apic
->pending_events
);
2261 pe
= xchg(&apic
->pending_events
, 0);
2262 if (test_bit(KVM_APIC_INIT
, &pe
)) {
2263 kvm_lapic_reset(vcpu
, true);
2264 kvm_vcpu_reset(vcpu
, true);
2265 if (kvm_vcpu_is_bsp(apic
->vcpu
))
2266 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
2268 vcpu
->arch
.mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
2270 if (test_bit(KVM_APIC_SIPI
, &pe
) &&
2271 vcpu
->arch
.mp_state
== KVM_MP_STATE_INIT_RECEIVED
) {
2272 /* evaluate pending_events before reading the vector */
2274 sipi_vector
= apic
->sipi_vector
;
2275 apic_debug("vcpu %d received sipi with vector # %x\n",
2276 vcpu
->vcpu_id
, sipi_vector
);
2277 kvm_vcpu_deliver_sipi_vector(vcpu
, sipi_vector
);
2278 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
2282 void kvm_lapic_init(void)
2284 /* do not patch jump label more than once per second */
2285 jump_label_rate_limit(&apic_hw_disabled
, HZ
);
2286 jump_label_rate_limit(&apic_sw_disabled
, HZ
);