3 * Local APIC virtualization
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
10 * Dor Laor <dor.laor@qumranet.com>
11 * Gregory Haskins <ghaskins@novell.com>
12 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
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/module.h>
28 #include <linux/math64.h>
29 #include <asm/processor.h>
32 #include <asm/current.h>
33 #include <asm/apicdef.h>
34 #include <asm/atomic.h>
35 #include "kvm_cache_regs.h"
39 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
41 #define mod_64(x, y) ((x) % (y))
49 #define APIC_BUS_CYCLE_NS 1
51 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
52 #define apic_debug(fmt, arg...)
54 #define APIC_LVT_NUM 6
55 /* 14 is the version for Xeon and Pentium 8.4.8*/
56 #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
57 #define LAPIC_MMIO_LENGTH (1 << 12)
58 /* followed define is not in apicdef.h */
59 #define APIC_SHORT_MASK 0xc0000
60 #define APIC_DEST_NOSHORT 0x0
61 #define APIC_DEST_MASK 0x800
62 #define MAX_APIC_VECTOR 256
64 #define VEC_POS(v) ((v) & (32 - 1))
65 #define REG_POS(v) (((v) >> 5) << 4)
67 static inline u32
apic_get_reg(struct kvm_lapic
*apic
, int reg_off
)
69 return *((u32
*) (apic
->regs
+ reg_off
));
72 static inline void apic_set_reg(struct kvm_lapic
*apic
, int reg_off
, u32 val
)
74 *((u32
*) (apic
->regs
+ reg_off
)) = val
;
77 static inline int apic_test_and_set_vector(int vec
, void *bitmap
)
79 return test_and_set_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
82 static inline int apic_test_and_clear_vector(int vec
, void *bitmap
)
84 return test_and_clear_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
87 static inline void apic_set_vector(int vec
, void *bitmap
)
89 set_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
92 static inline void apic_clear_vector(int vec
, void *bitmap
)
94 clear_bit(VEC_POS(vec
), (bitmap
) + REG_POS(vec
));
97 static inline int apic_hw_enabled(struct kvm_lapic
*apic
)
99 return (apic
)->vcpu
->arch
.apic_base
& MSR_IA32_APICBASE_ENABLE
;
102 static inline int apic_sw_enabled(struct kvm_lapic
*apic
)
104 return apic_get_reg(apic
, APIC_SPIV
) & APIC_SPIV_APIC_ENABLED
;
107 static inline int apic_enabled(struct kvm_lapic
*apic
)
109 return apic_sw_enabled(apic
) && apic_hw_enabled(apic
);
113 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
116 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
117 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
119 static inline int kvm_apic_id(struct kvm_lapic
*apic
)
121 return (apic_get_reg(apic
, APIC_ID
) >> 24) & 0xff;
124 static inline int apic_lvt_enabled(struct kvm_lapic
*apic
, int lvt_type
)
126 return !(apic_get_reg(apic
, lvt_type
) & APIC_LVT_MASKED
);
129 static inline int apic_lvt_vector(struct kvm_lapic
*apic
, int lvt_type
)
131 return apic_get_reg(apic
, lvt_type
) & APIC_VECTOR_MASK
;
134 static inline int apic_lvtt_period(struct kvm_lapic
*apic
)
136 return apic_get_reg(apic
, APIC_LVTT
) & APIC_LVT_TIMER_PERIODIC
;
139 static unsigned int apic_lvt_mask
[APIC_LVT_NUM
] = {
140 LVT_MASK
| APIC_LVT_TIMER_PERIODIC
, /* LVTT */
141 LVT_MASK
| APIC_MODE_MASK
, /* LVTTHMR */
142 LVT_MASK
| APIC_MODE_MASK
, /* LVTPC */
143 LINT_MASK
, LINT_MASK
, /* LVT0-1 */
144 LVT_MASK
/* LVTERR */
147 static int find_highest_vector(void *bitmap
)
150 int word_offset
= MAX_APIC_VECTOR
>> 5;
152 while ((word_offset
!= 0) && (word
[(--word_offset
) << 2] == 0))
155 if (likely(!word_offset
&& !word
[0]))
158 return fls(word
[word_offset
<< 2]) - 1 + (word_offset
<< 5);
161 static inline int apic_test_and_set_irr(int vec
, struct kvm_lapic
*apic
)
163 return apic_test_and_set_vector(vec
, apic
->regs
+ APIC_IRR
);
166 static inline void apic_clear_irr(int vec
, struct kvm_lapic
*apic
)
168 apic_clear_vector(vec
, apic
->regs
+ APIC_IRR
);
171 static inline int apic_find_highest_irr(struct kvm_lapic
*apic
)
175 result
= find_highest_vector(apic
->regs
+ APIC_IRR
);
176 ASSERT(result
== -1 || result
>= 16);
181 int kvm_lapic_find_highest_irr(struct kvm_vcpu
*vcpu
)
183 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
188 highest_irr
= apic_find_highest_irr(apic
);
192 EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr
);
194 int kvm_apic_set_irq(struct kvm_vcpu
*vcpu
, u8 vec
, u8 trig
)
196 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
198 if (!apic_test_and_set_irr(vec
, apic
)) {
199 /* a new pending irq is set in IRR */
201 apic_set_vector(vec
, apic
->regs
+ APIC_TMR
);
203 apic_clear_vector(vec
, apic
->regs
+ APIC_TMR
);
204 kvm_vcpu_kick(apic
->vcpu
);
210 static inline int apic_find_highest_isr(struct kvm_lapic
*apic
)
214 result
= find_highest_vector(apic
->regs
+ APIC_ISR
);
215 ASSERT(result
== -1 || result
>= 16);
220 static void apic_update_ppr(struct kvm_lapic
*apic
)
225 tpr
= apic_get_reg(apic
, APIC_TASKPRI
);
226 isr
= apic_find_highest_isr(apic
);
227 isrv
= (isr
!= -1) ? isr
: 0;
229 if ((tpr
& 0xf0) >= (isrv
& 0xf0))
234 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
235 apic
, ppr
, isr
, isrv
);
237 apic_set_reg(apic
, APIC_PROCPRI
, ppr
);
240 static void apic_set_tpr(struct kvm_lapic
*apic
, u32 tpr
)
242 apic_set_reg(apic
, APIC_TASKPRI
, tpr
);
243 apic_update_ppr(apic
);
246 int kvm_apic_match_physical_addr(struct kvm_lapic
*apic
, u16 dest
)
248 return kvm_apic_id(apic
) == dest
;
251 int kvm_apic_match_logical_addr(struct kvm_lapic
*apic
, u8 mda
)
256 logical_id
= GET_APIC_LOGICAL_ID(apic_get_reg(apic
, APIC_LDR
));
258 switch (apic_get_reg(apic
, APIC_DFR
)) {
260 if (logical_id
& mda
)
263 case APIC_DFR_CLUSTER
:
264 if (((logical_id
>> 4) == (mda
>> 0x4))
265 && (logical_id
& mda
& 0xf))
269 printk(KERN_WARNING
"Bad DFR vcpu %d: %08x\n",
270 apic
->vcpu
->vcpu_id
, apic_get_reg(apic
, APIC_DFR
));
277 static int apic_match_dest(struct kvm_vcpu
*vcpu
, struct kvm_lapic
*source
,
278 int short_hand
, int dest
, int dest_mode
)
281 struct kvm_lapic
*target
= vcpu
->arch
.apic
;
283 apic_debug("target %p, source %p, dest 0x%x, "
284 "dest_mode 0x%x, short_hand 0x%x",
285 target
, source
, dest
, dest_mode
, short_hand
);
288 switch (short_hand
) {
289 case APIC_DEST_NOSHORT
:
290 if (dest_mode
== 0) {
292 if ((dest
== 0xFF) || (dest
== kvm_apic_id(target
)))
296 result
= kvm_apic_match_logical_addr(target
, dest
);
299 if (target
== source
)
302 case APIC_DEST_ALLINC
:
305 case APIC_DEST_ALLBUT
:
306 if (target
!= source
)
310 printk(KERN_WARNING
"Bad dest shorthand value %x\n",
319 * Add a pending IRQ into lapic.
320 * Return 1 if successfully added and 0 if discarded.
322 static int __apic_accept_irq(struct kvm_lapic
*apic
, int delivery_mode
,
323 int vector
, int level
, int trig_mode
)
325 int orig_irr
, result
= 0;
326 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
328 switch (delivery_mode
) {
331 /* FIXME add logic for vcpu on reset */
332 if (unlikely(!apic_enabled(apic
)))
335 orig_irr
= apic_test_and_set_irr(vector
, apic
);
336 if (orig_irr
&& trig_mode
) {
337 apic_debug("level trig mode repeatedly for vector %d",
343 apic_debug("level trig mode for vector %d", vector
);
344 apic_set_vector(vector
, apic
->regs
+ APIC_TMR
);
346 apic_clear_vector(vector
, apic
->regs
+ APIC_TMR
);
350 result
= (orig_irr
== 0);
354 printk(KERN_DEBUG
"Ignoring delivery mode 3\n");
358 printk(KERN_DEBUG
"Ignoring guest SMI\n");
362 kvm_inject_nmi(vcpu
);
367 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
)
369 "INIT on a runnable vcpu %d\n",
371 vcpu
->arch
.mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
374 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
379 case APIC_DM_STARTUP
:
380 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
381 vcpu
->vcpu_id
, vector
);
382 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_INIT_RECEIVED
) {
383 vcpu
->arch
.sipi_vector
= vector
;
384 vcpu
->arch
.mp_state
= KVM_MP_STATE_SIPI_RECEIVED
;
390 printk(KERN_ERR
"TODO: unsupported delivery mode %x\n",
397 static struct kvm_lapic
*kvm_apic_round_robin(struct kvm
*kvm
, u8 vector
,
398 unsigned long bitmap
)
402 struct kvm_lapic
*apic
= NULL
;
404 last
= kvm
->arch
.round_robin_prev_vcpu
;
408 if (++next
== KVM_MAX_VCPUS
)
410 if (kvm
->vcpus
[next
] == NULL
|| !test_bit(next
, &bitmap
))
412 apic
= kvm
->vcpus
[next
]->arch
.apic
;
413 if (apic
&& apic_enabled(apic
))
416 } while (next
!= last
);
417 kvm
->arch
.round_robin_prev_vcpu
= next
;
420 printk(KERN_DEBUG
"vcpu not ready for apic_round_robin\n");
425 struct kvm_vcpu
*kvm_get_lowest_prio_vcpu(struct kvm
*kvm
, u8 vector
,
426 unsigned long bitmap
)
428 struct kvm_lapic
*apic
;
430 apic
= kvm_apic_round_robin(kvm
, vector
, bitmap
);
436 static void apic_set_eoi(struct kvm_lapic
*apic
)
438 int vector
= apic_find_highest_isr(apic
);
441 * Not every write EOI will has corresponding ISR,
442 * one example is when Kernel check timer on setup_IO_APIC
447 apic_clear_vector(vector
, apic
->regs
+ APIC_ISR
);
448 apic_update_ppr(apic
);
450 if (apic_test_and_clear_vector(vector
, apic
->regs
+ APIC_TMR
))
451 trigger_mode
= IOAPIC_LEVEL_TRIG
;
453 trigger_mode
= IOAPIC_EDGE_TRIG
;
454 kvm_ioapic_update_eoi(apic
->vcpu
->kvm
, vector
, trigger_mode
);
457 static void apic_send_ipi(struct kvm_lapic
*apic
)
459 u32 icr_low
= apic_get_reg(apic
, APIC_ICR
);
460 u32 icr_high
= apic_get_reg(apic
, APIC_ICR2
);
462 unsigned int dest
= GET_APIC_DEST_FIELD(icr_high
);
463 unsigned int short_hand
= icr_low
& APIC_SHORT_MASK
;
464 unsigned int trig_mode
= icr_low
& APIC_INT_LEVELTRIG
;
465 unsigned int level
= icr_low
& APIC_INT_ASSERT
;
466 unsigned int dest_mode
= icr_low
& APIC_DEST_MASK
;
467 unsigned int delivery_mode
= icr_low
& APIC_MODE_MASK
;
468 unsigned int vector
= icr_low
& APIC_VECTOR_MASK
;
470 struct kvm_vcpu
*target
;
471 struct kvm_vcpu
*vcpu
;
472 unsigned long lpr_map
= 0;
475 apic_debug("icr_high 0x%x, icr_low 0x%x, "
476 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
477 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
478 icr_high
, icr_low
, short_hand
, dest
,
479 trig_mode
, level
, dest_mode
, delivery_mode
, vector
);
481 for (i
= 0; i
< KVM_MAX_VCPUS
; i
++) {
482 vcpu
= apic
->vcpu
->kvm
->vcpus
[i
];
486 if (vcpu
->arch
.apic
&&
487 apic_match_dest(vcpu
, apic
, short_hand
, dest
, dest_mode
)) {
488 if (delivery_mode
== APIC_DM_LOWEST
)
489 set_bit(vcpu
->vcpu_id
, &lpr_map
);
491 __apic_accept_irq(vcpu
->arch
.apic
, delivery_mode
,
492 vector
, level
, trig_mode
);
496 if (delivery_mode
== APIC_DM_LOWEST
) {
497 target
= kvm_get_lowest_prio_vcpu(vcpu
->kvm
, vector
, lpr_map
);
499 __apic_accept_irq(target
->arch
.apic
, delivery_mode
,
500 vector
, level
, trig_mode
);
504 static u32
apic_get_tmcct(struct kvm_lapic
*apic
)
510 ASSERT(apic
!= NULL
);
512 /* if initial count is 0, current count should also be 0 */
513 if (apic_get_reg(apic
, APIC_TMICT
) == 0)
516 remaining
= hrtimer_expires_remaining(&apic
->timer
.dev
);
517 if (ktime_to_ns(remaining
) < 0)
518 remaining
= ktime_set(0, 0);
520 ns
= mod_64(ktime_to_ns(remaining
), apic
->timer
.period
);
521 tmcct
= div64_u64(ns
, (APIC_BUS_CYCLE_NS
* apic
->timer
.divide_count
));
526 static void __report_tpr_access(struct kvm_lapic
*apic
, bool write
)
528 struct kvm_vcpu
*vcpu
= apic
->vcpu
;
529 struct kvm_run
*run
= vcpu
->run
;
531 set_bit(KVM_REQ_REPORT_TPR_ACCESS
, &vcpu
->requests
);
532 run
->tpr_access
.rip
= kvm_rip_read(vcpu
);
533 run
->tpr_access
.is_write
= write
;
536 static inline void report_tpr_access(struct kvm_lapic
*apic
, bool write
)
538 if (apic
->vcpu
->arch
.tpr_access_reporting
)
539 __report_tpr_access(apic
, write
);
542 static u32
__apic_read(struct kvm_lapic
*apic
, unsigned int offset
)
546 KVMTRACE_1D(APIC_ACCESS
, apic
->vcpu
, (u32
)offset
, handler
);
548 if (offset
>= LAPIC_MMIO_LENGTH
)
553 printk(KERN_WARNING
"Access APIC ARBPRI register "
554 "which is for P6\n");
557 case APIC_TMCCT
: /* Timer CCR */
558 val
= apic_get_tmcct(apic
);
562 report_tpr_access(apic
, false);
565 apic_update_ppr(apic
);
566 val
= apic_get_reg(apic
, offset
);
573 static void apic_mmio_read(struct kvm_io_device
*this,
574 gpa_t address
, int len
, void *data
)
576 struct kvm_lapic
*apic
= (struct kvm_lapic
*)this->private;
577 unsigned int offset
= address
- apic
->base_address
;
578 unsigned char alignment
= offset
& 0xf;
581 if ((alignment
+ len
) > 4) {
582 printk(KERN_ERR
"KVM_APIC_READ: alignment error %lx %d",
583 (unsigned long)address
, len
);
586 result
= __apic_read(apic
, offset
& ~0xf);
592 memcpy(data
, (char *)&result
+ alignment
, len
);
595 printk(KERN_ERR
"Local APIC read with len = %x, "
596 "should be 1,2, or 4 instead\n", len
);
601 static void update_divide_count(struct kvm_lapic
*apic
)
603 u32 tmp1
, tmp2
, tdcr
;
605 tdcr
= apic_get_reg(apic
, APIC_TDCR
);
607 tmp2
= ((tmp1
& 0x3) | ((tmp1
& 0x8) >> 1)) + 1;
608 apic
->timer
.divide_count
= 0x1 << (tmp2
& 0x7);
610 apic_debug("timer divide count is 0x%x\n",
611 apic
->timer
.divide_count
);
614 static void start_apic_timer(struct kvm_lapic
*apic
)
616 ktime_t now
= apic
->timer
.dev
.base
->get_time();
618 apic
->timer
.period
= apic_get_reg(apic
, APIC_TMICT
) *
619 APIC_BUS_CYCLE_NS
* apic
->timer
.divide_count
;
620 atomic_set(&apic
->timer
.pending
, 0);
622 if (!apic
->timer
.period
)
625 hrtimer_start(&apic
->timer
.dev
,
626 ktime_add_ns(now
, apic
->timer
.period
),
629 apic_debug("%s: bus cycle is %" PRId64
"ns, now 0x%016"
631 "timer initial count 0x%x, period %lldns, "
632 "expire @ 0x%016" PRIx64
".\n", __func__
,
633 APIC_BUS_CYCLE_NS
, ktime_to_ns(now
),
634 apic_get_reg(apic
, APIC_TMICT
),
636 ktime_to_ns(ktime_add_ns(now
,
637 apic
->timer
.period
)));
640 static void apic_mmio_write(struct kvm_io_device
*this,
641 gpa_t address
, int len
, const void *data
)
643 struct kvm_lapic
*apic
= (struct kvm_lapic
*)this->private;
644 unsigned int offset
= address
- apic
->base_address
;
645 unsigned char alignment
= offset
& 0xf;
649 * APIC register must be aligned on 128-bits boundary.
650 * 32/64/128 bits registers must be accessed thru 32 bits.
653 if (len
!= 4 || alignment
) {
654 /* Don't shout loud, $infamous_os would cause only noise. */
655 apic_debug("apic write: bad size=%d %lx\n",
662 /* too common printing */
663 if (offset
!= APIC_EOI
)
664 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
665 "0x%x\n", __func__
, offset
, len
, val
);
669 KVMTRACE_1D(APIC_ACCESS
, apic
->vcpu
, (u32
)offset
, handler
);
672 case APIC_ID
: /* Local APIC ID */
673 apic_set_reg(apic
, APIC_ID
, val
);
677 report_tpr_access(apic
, true);
678 apic_set_tpr(apic
, val
& 0xff);
686 apic_set_reg(apic
, APIC_LDR
, val
& APIC_LDR_MASK
);
690 apic_set_reg(apic
, APIC_DFR
, val
| 0x0FFFFFFF);
694 apic_set_reg(apic
, APIC_SPIV
, val
& 0x3ff);
695 if (!(val
& APIC_SPIV_APIC_ENABLED
)) {
699 for (i
= 0; i
< APIC_LVT_NUM
; i
++) {
700 lvt_val
= apic_get_reg(apic
,
701 APIC_LVTT
+ 0x10 * i
);
702 apic_set_reg(apic
, APIC_LVTT
+ 0x10 * i
,
703 lvt_val
| APIC_LVT_MASKED
);
705 atomic_set(&apic
->timer
.pending
, 0);
711 /* No delay here, so we always clear the pending bit */
712 apic_set_reg(apic
, APIC_ICR
, val
& ~(1 << 12));
717 apic_set_reg(apic
, APIC_ICR2
, val
& 0xff000000);
726 /* TODO: Check vector */
727 if (!apic_sw_enabled(apic
))
728 val
|= APIC_LVT_MASKED
;
730 val
&= apic_lvt_mask
[(offset
- APIC_LVTT
) >> 4];
731 apic_set_reg(apic
, offset
, val
);
736 hrtimer_cancel(&apic
->timer
.dev
);
737 apic_set_reg(apic
, APIC_TMICT
, val
);
738 start_apic_timer(apic
);
743 printk(KERN_ERR
"KVM_WRITE:TDCR %x\n", val
);
744 apic_set_reg(apic
, APIC_TDCR
, val
);
745 update_divide_count(apic
);
749 apic_debug("Local APIC Write to read-only register %x\n",
756 static int apic_mmio_range(struct kvm_io_device
*this, gpa_t addr
,
759 struct kvm_lapic
*apic
= (struct kvm_lapic
*)this->private;
763 if (apic_hw_enabled(apic
) &&
764 (addr
>= apic
->base_address
) &&
765 (addr
< (apic
->base_address
+ LAPIC_MMIO_LENGTH
)))
771 void kvm_free_lapic(struct kvm_vcpu
*vcpu
)
773 if (!vcpu
->arch
.apic
)
776 hrtimer_cancel(&vcpu
->arch
.apic
->timer
.dev
);
778 if (vcpu
->arch
.apic
->regs_page
)
779 __free_page(vcpu
->arch
.apic
->regs_page
);
781 kfree(vcpu
->arch
.apic
);
785 *----------------------------------------------------------------------
787 *----------------------------------------------------------------------
790 void kvm_lapic_set_tpr(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
792 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
796 apic_set_tpr(apic
, ((cr8
& 0x0f) << 4)
797 | (apic_get_reg(apic
, APIC_TASKPRI
) & 4));
799 EXPORT_SYMBOL_GPL(kvm_lapic_set_tpr
);
801 u64
kvm_lapic_get_cr8(struct kvm_vcpu
*vcpu
)
803 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
808 tpr
= (u64
) apic_get_reg(apic
, APIC_TASKPRI
);
810 return (tpr
& 0xf0) >> 4;
812 EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8
);
814 void kvm_lapic_set_base(struct kvm_vcpu
*vcpu
, u64 value
)
816 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
819 value
|= MSR_IA32_APICBASE_BSP
;
820 vcpu
->arch
.apic_base
= value
;
823 if (apic
->vcpu
->vcpu_id
)
824 value
&= ~MSR_IA32_APICBASE_BSP
;
826 vcpu
->arch
.apic_base
= value
;
827 apic
->base_address
= apic
->vcpu
->arch
.apic_base
&
828 MSR_IA32_APICBASE_BASE
;
830 /* with FSB delivery interrupt, we can restart APIC functionality */
831 apic_debug("apic base msr is 0x%016" PRIx64
", and base address is "
832 "0x%lx.\n", apic
->vcpu
->arch
.apic_base
, apic
->base_address
);
836 u64
kvm_lapic_get_base(struct kvm_vcpu
*vcpu
)
838 return vcpu
->arch
.apic_base
;
840 EXPORT_SYMBOL_GPL(kvm_lapic_get_base
);
842 void kvm_lapic_reset(struct kvm_vcpu
*vcpu
)
844 struct kvm_lapic
*apic
;
847 apic_debug("%s\n", __func__
);
850 apic
= vcpu
->arch
.apic
;
851 ASSERT(apic
!= NULL
);
853 /* Stop the timer in case it's a reset to an active apic */
854 hrtimer_cancel(&apic
->timer
.dev
);
856 apic_set_reg(apic
, APIC_ID
, vcpu
->vcpu_id
<< 24);
857 apic_set_reg(apic
, APIC_LVR
, APIC_VERSION
);
859 for (i
= 0; i
< APIC_LVT_NUM
; i
++)
860 apic_set_reg(apic
, APIC_LVTT
+ 0x10 * i
, APIC_LVT_MASKED
);
861 apic_set_reg(apic
, APIC_LVT0
,
862 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT
));
864 apic_set_reg(apic
, APIC_DFR
, 0xffffffffU
);
865 apic_set_reg(apic
, APIC_SPIV
, 0xff);
866 apic_set_reg(apic
, APIC_TASKPRI
, 0);
867 apic_set_reg(apic
, APIC_LDR
, 0);
868 apic_set_reg(apic
, APIC_ESR
, 0);
869 apic_set_reg(apic
, APIC_ICR
, 0);
870 apic_set_reg(apic
, APIC_ICR2
, 0);
871 apic_set_reg(apic
, APIC_TDCR
, 0);
872 apic_set_reg(apic
, APIC_TMICT
, 0);
873 for (i
= 0; i
< 8; i
++) {
874 apic_set_reg(apic
, APIC_IRR
+ 0x10 * i
, 0);
875 apic_set_reg(apic
, APIC_ISR
+ 0x10 * i
, 0);
876 apic_set_reg(apic
, APIC_TMR
+ 0x10 * i
, 0);
878 update_divide_count(apic
);
879 atomic_set(&apic
->timer
.pending
, 0);
880 if (vcpu
->vcpu_id
== 0)
881 vcpu
->arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
882 apic_update_ppr(apic
);
884 apic_debug(KERN_INFO
"%s: vcpu=%p, id=%d, base_msr="
885 "0x%016" PRIx64
", base_address=0x%0lx.\n", __func__
,
886 vcpu
, kvm_apic_id(apic
),
887 vcpu
->arch
.apic_base
, apic
->base_address
);
889 EXPORT_SYMBOL_GPL(kvm_lapic_reset
);
891 int kvm_lapic_enabled(struct kvm_vcpu
*vcpu
)
893 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
898 ret
= apic_enabled(apic
);
902 EXPORT_SYMBOL_GPL(kvm_lapic_enabled
);
905 *----------------------------------------------------------------------
907 *----------------------------------------------------------------------
910 /* TODO: make sure __apic_timer_fn runs in current pCPU */
911 static int __apic_timer_fn(struct kvm_lapic
*apic
)
914 wait_queue_head_t
*q
= &apic
->vcpu
->wq
;
916 if(!atomic_inc_and_test(&apic
->timer
.pending
))
917 set_bit(KVM_REQ_PENDING_TIMER
, &apic
->vcpu
->requests
);
918 if (waitqueue_active(q
))
919 wake_up_interruptible(q
);
921 if (apic_lvtt_period(apic
)) {
923 hrtimer_add_expires_ns(&apic
->timer
.dev
, apic
->timer
.period
);
928 int apic_has_pending_timer(struct kvm_vcpu
*vcpu
)
930 struct kvm_lapic
*lapic
= vcpu
->arch
.apic
;
932 if (lapic
&& apic_enabled(lapic
) && apic_lvt_enabled(lapic
, APIC_LVTT
))
933 return atomic_read(&lapic
->timer
.pending
);
938 static int __inject_apic_timer_irq(struct kvm_lapic
*apic
)
942 vector
= apic_lvt_vector(apic
, APIC_LVTT
);
943 return __apic_accept_irq(apic
, APIC_DM_FIXED
, vector
, 1, 0);
946 static enum hrtimer_restart
apic_timer_fn(struct hrtimer
*data
)
948 struct kvm_lapic
*apic
;
949 int restart_timer
= 0;
951 apic
= container_of(data
, struct kvm_lapic
, timer
.dev
);
953 restart_timer
= __apic_timer_fn(apic
);
956 return HRTIMER_RESTART
;
958 return HRTIMER_NORESTART
;
961 int kvm_create_lapic(struct kvm_vcpu
*vcpu
)
963 struct kvm_lapic
*apic
;
965 ASSERT(vcpu
!= NULL
);
966 apic_debug("apic_init %d\n", vcpu
->vcpu_id
);
968 apic
= kzalloc(sizeof(*apic
), GFP_KERNEL
);
972 vcpu
->arch
.apic
= apic
;
974 apic
->regs_page
= alloc_page(GFP_KERNEL
);
975 if (apic
->regs_page
== NULL
) {
976 printk(KERN_ERR
"malloc apic regs error for vcpu %x\n",
978 goto nomem_free_apic
;
980 apic
->regs
= page_address(apic
->regs_page
);
981 memset(apic
->regs
, 0, PAGE_SIZE
);
984 hrtimer_init(&apic
->timer
.dev
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
985 apic
->timer
.dev
.function
= apic_timer_fn
;
986 apic
->base_address
= APIC_DEFAULT_PHYS_BASE
;
987 vcpu
->arch
.apic_base
= APIC_DEFAULT_PHYS_BASE
;
989 kvm_lapic_reset(vcpu
);
990 apic
->dev
.read
= apic_mmio_read
;
991 apic
->dev
.write
= apic_mmio_write
;
992 apic
->dev
.in_range
= apic_mmio_range
;
993 apic
->dev
.private = apic
;
1001 EXPORT_SYMBOL_GPL(kvm_create_lapic
);
1003 int kvm_apic_has_interrupt(struct kvm_vcpu
*vcpu
)
1005 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1008 if (!apic
|| !apic_enabled(apic
))
1011 apic_update_ppr(apic
);
1012 highest_irr
= apic_find_highest_irr(apic
);
1013 if ((highest_irr
== -1) ||
1014 ((highest_irr
& 0xF0) <= apic_get_reg(apic
, APIC_PROCPRI
)))
1019 int kvm_apic_accept_pic_intr(struct kvm_vcpu
*vcpu
)
1021 u32 lvt0
= apic_get_reg(vcpu
->arch
.apic
, APIC_LVT0
);
1024 if (vcpu
->vcpu_id
== 0) {
1025 if (!apic_hw_enabled(vcpu
->arch
.apic
))
1027 if ((lvt0
& APIC_LVT_MASKED
) == 0 &&
1028 GET_APIC_DELIVERY_MODE(lvt0
) == APIC_MODE_EXTINT
)
1034 void kvm_inject_apic_timer_irqs(struct kvm_vcpu
*vcpu
)
1036 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1038 if (apic
&& apic_lvt_enabled(apic
, APIC_LVTT
) &&
1039 atomic_read(&apic
->timer
.pending
) > 0) {
1040 if (__inject_apic_timer_irq(apic
))
1041 atomic_dec(&apic
->timer
.pending
);
1045 int kvm_get_apic_interrupt(struct kvm_vcpu
*vcpu
)
1047 int vector
= kvm_apic_has_interrupt(vcpu
);
1048 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1053 apic_set_vector(vector
, apic
->regs
+ APIC_ISR
);
1054 apic_update_ppr(apic
);
1055 apic_clear_irr(vector
, apic
);
1059 void kvm_apic_post_state_restore(struct kvm_vcpu
*vcpu
)
1061 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1063 apic
->base_address
= vcpu
->arch
.apic_base
&
1064 MSR_IA32_APICBASE_BASE
;
1065 apic_set_reg(apic
, APIC_LVR
, APIC_VERSION
);
1066 apic_update_ppr(apic
);
1067 hrtimer_cancel(&apic
->timer
.dev
);
1068 update_divide_count(apic
);
1069 start_apic_timer(apic
);
1072 void __kvm_migrate_apic_timer(struct kvm_vcpu
*vcpu
)
1074 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
1075 struct hrtimer
*timer
;
1080 timer
= &apic
->timer
.dev
;
1081 if (hrtimer_cancel(timer
))
1082 hrtimer_start_expires(timer
, HRTIMER_MODE_ABS
);
1085 void kvm_lapic_sync_from_vapic(struct kvm_vcpu
*vcpu
)
1090 if (!irqchip_in_kernel(vcpu
->kvm
) || !vcpu
->arch
.apic
->vapic_addr
)
1093 vapic
= kmap_atomic(vcpu
->arch
.apic
->vapic_page
, KM_USER0
);
1094 data
= *(u32
*)(vapic
+ offset_in_page(vcpu
->arch
.apic
->vapic_addr
));
1095 kunmap_atomic(vapic
, KM_USER0
);
1097 apic_set_tpr(vcpu
->arch
.apic
, data
& 0xff);
1100 void kvm_lapic_sync_to_vapic(struct kvm_vcpu
*vcpu
)
1103 int max_irr
, max_isr
;
1104 struct kvm_lapic
*apic
;
1107 if (!irqchip_in_kernel(vcpu
->kvm
) || !vcpu
->arch
.apic
->vapic_addr
)
1110 apic
= vcpu
->arch
.apic
;
1111 tpr
= apic_get_reg(apic
, APIC_TASKPRI
) & 0xff;
1112 max_irr
= apic_find_highest_irr(apic
);
1115 max_isr
= apic_find_highest_isr(apic
);
1118 data
= (tpr
& 0xff) | ((max_isr
& 0xf0) << 8) | (max_irr
<< 24);
1120 vapic
= kmap_atomic(vcpu
->arch
.apic
->vapic_page
, KM_USER0
);
1121 *(u32
*)(vapic
+ offset_in_page(vcpu
->arch
.apic
->vapic_addr
)) = data
;
1122 kunmap_atomic(vapic
, KM_USER0
);
1125 void kvm_lapic_set_vapic_addr(struct kvm_vcpu
*vcpu
, gpa_t vapic_addr
)
1127 if (!irqchip_in_kernel(vcpu
->kvm
))
1130 vcpu
->arch
.apic
->vapic_addr
= vapic_addr
;