26 #include <linux/hrtimer.h>
28 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <asm/processor.h>
34 #include <asm/current.h>
45 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
47 #define mod_64(x, y) ((x) % (y))
55 #define APIC_BUS_CYCLE_NS 1
58 #define apic_debug(fmt, arg...)
60 #define APIC_LVT_NUM 6
62 #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
63 #define LAPIC_MMIO_LENGTH (1 << 12)
65 #define APIC_SHORT_MASK 0xc0000
66 #define APIC_DEST_NOSHORT 0x0
67 #define APIC_DEST_MASK 0x800
68 #define MAX_APIC_VECTOR 256
69 #define APIC_VECTORS_PER_REG 32
71 #define VEC_POS(v) ((v) & (32 - 1))
72 #define REG_POS(v) (((v) >> 5) << 4)
74 static unsigned int min_timer_period_us = 500;
79 *((
u32 *) (apic->
regs + reg_off)) = val;
82 static inline int apic_test_and_set_vector(
int vec,
void *
bitmap)
87 static inline int apic_test_and_clear_vector(
int vec,
void *bitmap)
92 static inline int apic_test_vector(
int vec,
void *bitmap)
97 static inline void apic_set_vector(
int vec,
void *bitmap)
102 static inline void apic_clear_vector(
int vec,
void *bitmap)
107 static inline int __apic_test_and_set_vector(
int vec,
void *bitmap)
112 static inline int __apic_test_and_clear_vector(
int vec,
void *bitmap)
120 static inline void apic_set_spiv(
struct kvm_lapic *apic,
u32 val)
123 if (val & APIC_SPIV_APIC_ENABLED)
124 static_key_slow_dec_deferred(&apic_sw_disabled);
133 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
137 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
140 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
141 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
143 static inline int apic_x2apic_mode(
struct kvm_lapic *apic)
148 static inline int kvm_apic_id(
struct kvm_lapic *apic)
150 return (kvm_apic_get_reg(apic,
APIC_ID) >> 24) & 0xff;
170 static void recalculate_apic_map(
struct kvm *
kvm)
187 new->lid_mask = 0xff;
204 if (apic_x2apic_mode(apic)) {
207 new->cid_mask =
new->lid_mask = 0xffff;
208 }
else if (kvm_apic_sw_enabled(apic) &&
216 new->phys_map[kvm_apic_id(apic)] =
apic;
218 ldr = kvm_apic_get_reg(apic,
APIC_LDR);
219 cid = apic_cluster_id(
new, ldr);
220 lid = apic_logical_id(
new, ldr);
227 lockdep_is_held(&kvm->
arch.apic_map_lock));
235 static inline void kvm_apic_set_id(
struct kvm_lapic *apic,
u8 id)
237 apic_set_reg(apic,
APIC_ID,
id << 24);
238 recalculate_apic_map(apic->
vcpu->kvm);
241 static inline void kvm_apic_set_ldr(
struct kvm_lapic *apic,
u32 id)
244 recalculate_apic_map(apic->
vcpu->kvm);
247 static inline int apic_lvt_enabled(
struct kvm_lapic *apic,
int lvt_type)
252 static inline int apic_lvt_vector(
struct kvm_lapic *apic,
int lvt_type)
257 static inline int apic_lvtt_oneshot(
struct kvm_lapic *apic)
259 return ((kvm_apic_get_reg(apic,
APIC_LVTT) &
263 static inline int apic_lvtt_period(
struct kvm_lapic *apic)
265 return ((kvm_apic_get_reg(apic,
APIC_LVTT) &
269 static inline int apic_lvtt_tscdeadline(
struct kvm_lapic *apic)
271 return ((kvm_apic_get_reg(apic,
APIC_LVTT) &
276 static inline int apic_lvt_nmi_mode(
u32 lvt_val)
287 if (!kvm_vcpu_has_lapic(vcpu))
296 static const unsigned int apic_lvt_mask[
APIC_LVT_NUM] = {
304 static int find_highest_vector(
void *bitmap)
313 return fls(*reg) - 1 + vec;
319 static u8 count_vectors(
void *bitmap)
333 static inline int apic_test_and_set_irr(
int vec,
struct kvm_lapic *apic)
336 return apic_test_and_set_vector(vec, apic->
regs +
APIC_IRR);
339 static inline int apic_search_irr(
struct kvm_lapic *apic)
344 static inline int apic_find_highest_irr(
struct kvm_lapic *apic)
351 result = apic_search_irr(apic);
352 ASSERT(result == -1 || result >= 16);
357 static inline void apic_clear_irr(
int vec,
struct kvm_lapic *apic)
361 if (apic_search_irr(apic) != -1)
365 static inline void apic_set_isr(
int vec,
struct kvm_lapic *apic)
367 if (!__apic_test_and_set_vector(vec, apic->
regs +
APIC_ISR))
378 static inline void apic_clear_isr(
int vec,
struct kvm_lapic *apic)
380 if (__apic_test_and_clear_vector(vec, apic->
regs +
APIC_ISR))
395 if (!kvm_vcpu_has_lapic(vcpu))
397 highest_irr = apic_find_highest_irr(vcpu->
arch.apic);
413 static int pv_eoi_put_user(
struct kvm_vcpu *vcpu,
u8 val)
420 static int pv_eoi_get_user(
struct kvm_vcpu *vcpu,
u8 *val)
427 static inline bool pv_eoi_enabled(
struct kvm_vcpu *vcpu)
432 static bool pv_eoi_get_pending(
struct kvm_vcpu *vcpu)
435 if (pv_eoi_get_user(vcpu, &val) < 0)
436 apic_debug(
"Can't read EOI MSR value: 0x%llx\n",
437 (
unsigned long long)vcpi->arch.pv_eoi.msr_val);
441 static void pv_eoi_set_pending(
struct kvm_vcpu *vcpu)
444 apic_debug(
"Can't set EOI MSR value: 0x%llx\n",
445 (
unsigned long long)vcpi->arch.pv_eoi.msr_val);
451 static void pv_eoi_clr_pending(
struct kvm_vcpu *vcpu)
454 apic_debug(
"Can't clear EOI MSR value: 0x%llx\n",
455 (
unsigned long long)vcpi->arch.pv_eoi.msr_val);
461 static inline int apic_find_highest_isr(
struct kvm_lapic *apic)
470 ASSERT(result == -1 || result >= 16);
475 static void apic_update_ppr(
struct kvm_lapic *apic)
482 isr = apic_find_highest_isr(apic);
483 isrv = (isr != -1) ? isr : 0;
485 if ((tpr & 0xf0) >= (isrv & 0xf0))
490 apic_debug(
"vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
491 apic, ppr, isr, isrv);
493 if (old_ppr != ppr) {
500 static void apic_set_tpr(
struct kvm_lapic *apic,
u32 tpr)
503 apic_update_ppr(apic);
508 return dest == 0xff || kvm_apic_id(apic) ==
dest;
516 if (apic_x2apic_mode(apic)) {
517 logical_id = kvm_apic_get_reg(apic,
APIC_LDR);
518 return logical_id &
mda;
523 switch (kvm_apic_get_reg(apic,
APIC_DFR)) {
525 if (logical_id & mda)
529 if (((logical_id >> 4) == (mda >> 0x4))
530 && (logical_id & mda & 0xf))
548 apic_debug(
"target %p, source %p, dest 0x%x, "
549 "dest_mode 0x%x, short_hand 0x%x\n",
550 target, source, dest, dest_mode, short_hand);
553 switch (short_hand) {
563 result = (target ==
source);
569 result = (target !=
source);
572 apic_debug(
"kvm: apic: Bad dest shorthand value %x\n",
584 unsigned long bitmap = 1;
615 bitmap = apic_logical_id(map, mda);
628 bitmap = (l >= 0) ? 1 << l : 0;
656 switch (delivery_mode) {
658 vcpu->
arch.apic_arb_prio++;
665 apic_debug(
"level trig mode for vector %d", vector);
670 result = !apic_test_and_set_irr(vector, apic);
671 trace_kvm_apic_accept_irq(vcpu->
vcpu_id, delivery_mode,
672 trig_mode, vector, !result);
676 "vector %d", vector);
699 if (!trig_mode || level) {
705 apic_debug(
"Ignoring de-assert INIT to vcpu %d\n",
740 return vcpu1->
arch.apic_arb_prio - vcpu2->
arch.apic_arb_prio;
743 static int apic_set_eoi(
struct kvm_lapic *apic)
745 int vector = apic_find_highest_isr(apic);
747 trace_kvm_eoi(apic, vector);
756 apic_clear_isr(vector, apic);
757 apic_update_ppr(apic);
772 static void apic_send_ipi(
struct kvm_lapic *apic)
784 if (apic_x2apic_mode(apic))
785 irq.dest_id = icr_high;
789 trace_kvm_apic_ipi(icr_low,
irq.dest_id);
792 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
793 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
794 icr_high, icr_low,
irq.shorthand,
irq.dest_id,
795 irq.trig_mode,
irq.level,
irq.dest_mode,
irq.delivery_mode,
814 if (ktime_to_ns(remaining) < 0)
815 remaining = ktime_set(0, 0);
818 tmcct = div64_u64(ns,
824 static void __report_tpr_access(
struct kvm_lapic *apic,
bool write)
834 static inline void report_tpr_access(
struct kvm_lapic *apic,
bool write)
836 if (apic->
vcpu->arch.tpr_access_reporting)
837 __report_tpr_access(apic, write);
849 if (apic_x2apic_mode(apic))
850 val = kvm_apic_id(apic);
852 val = kvm_apic_id(apic) << 24;
855 apic_debug(
"Access APIC ARBPRI register which is for P6\n");
859 if (apic_lvtt_tscdeadline(apic))
862 val = apic_get_tmcct(apic);
865 apic_update_ppr(apic);
866 val = kvm_apic_get_reg(apic, offset);
869 report_tpr_access(apic,
false);
872 val = kvm_apic_get_reg(apic, offset);
884 static int apic_reg_read(
struct kvm_lapic *apic,
u32 offset,
int len,
890 static const u64 rmask = 0x43ff01ffffffe70cULL;
892 if ((alignment + len) > 4) {
893 apic_debug(
"KVM_APIC_READ: alignment error %x %d\n",
898 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
899 apic_debug(
"KVM_APIC_READ: read reserved register %x\n",
904 result = __apic_read(apic, offset & ~0xf);
912 memcpy(data, (
char *)&result + alignment, len);
916 "should be 1,2, or 4 instead\n", len);
924 return kvm_apic_hw_enabled(apic) &&
935 if (!apic_mmio_in_range(apic, address))
938 apic_reg_read(apic, offset, len, data);
943 static void update_divide_count(
struct kvm_lapic *apic)
947 tdcr = kvm_apic_get_reg(apic,
APIC_TDCR);
949 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
956 static void start_apic_timer(
struct kvm_lapic *apic)
961 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
974 if (apic_lvtt_period(apic)) {
975 s64 min_period = min_timer_period_us * 1000
LL;
979 "kvm: vcpu %i: requested %lld ns "
980 "lapic timer period limited to %lld ns\n",
993 "timer initial count 0x%x, period %lldns, "
994 "expire @ 0x%016" PRIx64 ".\n", __func__,
1000 }
else if (apic_lvtt_tscdeadline(apic)) {
1005 unsigned long this_tsc_khz = vcpu->
arch.virtual_tsc_khz;
1006 unsigned long flags;
1008 if (
unlikely(!tscdeadline || !this_tsc_khz))
1015 if (
likely(tscdeadline > guest_tsc)) {
1016 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1017 do_div(ns, this_tsc_khz);
1026 static void apic_manage_nmi_watchdog(
struct kvm_lapic *apic,
u32 lvt0_val)
1028 int nmi_wd_enabled = apic_lvt_nmi_mode(kvm_apic_get_reg(apic,
APIC_LVT0));
1030 if (apic_lvt_nmi_mode(lvt0_val)) {
1031 if (!nmi_wd_enabled) {
1032 apic_debug(
"Receive NMI setting on APIC_LVT0 "
1033 "for cpu %d\n", apic->
vcpu->vcpu_id);
1034 apic->
vcpu->kvm->arch.vapics_in_nmi_mode++;
1036 }
else if (nmi_wd_enabled)
1037 apic->
vcpu->kvm->arch.vapics_in_nmi_mode--;
1048 if (!apic_x2apic_mode(apic))
1049 kvm_apic_set_id(apic, val >> 24);
1055 report_tpr_access(apic,
true);
1056 apic_set_tpr(apic, val & 0xff);
1064 if (!apic_x2apic_mode(apic))
1071 if (!apic_x2apic_mode(apic)) {
1072 apic_set_reg(apic,
APIC_DFR, val | 0x0FFFFFFF);
1073 recalculate_apic_map(apic->
vcpu->kvm);
1082 apic_set_spiv(apic, val & mask);
1083 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1088 lvt_val = kvm_apic_get_reg(apic,
1090 apic_set_reg(apic,
APIC_LVTT + 0x10 * i,
1100 apic_set_reg(apic,
APIC_ICR, val & ~(1 << 12));
1101 apic_send_ipi(apic);
1105 if (!apic_x2apic_mode(apic))
1111 apic_manage_nmi_watchdog(apic, val);
1117 if (!kvm_apic_sw_enabled(apic))
1120 val &= apic_lvt_mask[(reg -
APIC_LVTT) >> 4];
1121 apic_set_reg(apic, reg, val);
1126 if ((kvm_apic_get_reg(apic,
APIC_LVTT) &
1131 if (!kvm_apic_sw_enabled(apic))
1133 val &= (apic_lvt_mask[0] | apic->
lapic_timer.timer_mode_mask);
1138 if (apic_lvtt_tscdeadline(apic))
1143 start_apic_timer(apic);
1150 update_divide_count(apic);
1154 if (apic_x2apic_mode(apic) && val != 0) {
1155 apic_debug(
"KVM_WRITE:ESR not zero %x\n", val);
1161 if (apic_x2apic_mode(apic)) {
1162 apic_reg_write(apic,
APIC_ICR, 0x40000 | (val & 0xff));
1171 apic_debug(
"Local APIC Write to read-only register %x\n", reg);
1176 gpa_t address,
int len,
const void *data)
1178 struct kvm_lapic *apic = to_lapic(
this);
1182 if (!apic_mmio_in_range(apic, address))
1190 if (len != 4 || (offset & 0xf)) {
1192 apic_debug(
"apic write: bad size=%d %lx\n", len, (
long)address);
1200 apic_debug(
"%s: offset 0x%x with length 0x%x, and value is "
1201 "0x%x\n", __func__, offset, len, val);
1203 apic_reg_write(apic, offset & 0xff0, val);
1210 if (kvm_vcpu_has_lapic(vcpu))
1219 if (!vcpu->
arch.apic)
1227 if (!(kvm_apic_get_reg(apic,
APIC_SPIV) & APIC_SPIV_APIC_ENABLED))
1246 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
1247 apic_lvtt_period(apic))
1257 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
1258 apic_lvtt_period(apic))
1263 start_apic_timer(apic);
1270 if (!kvm_vcpu_has_lapic(vcpu))
1273 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
1281 if (!kvm_vcpu_has_lapic(vcpu))
1286 return (tpr & 0xf0) >> 4;
1301 if (value & MSR_IA32_APICBASE_ENABLE)
1305 recalculate_apic_map(vcpu->
kvm);
1308 if (!kvm_vcpu_is_bsp(apic->
vcpu))
1312 if (apic_x2apic_mode(apic)) {
1313 u32 id = kvm_apic_id(apic);
1314 u32 ldr = ((
id >> 4) << 16) | (1 << (
id & 0xf));
1315 kvm_apic_set_ldr(apic, ldr);
1321 apic_debug(
"apic base msr is 0x%016" PRIx64
", and base address is "
1334 apic = vcpu->
arch.apic;
1340 kvm_apic_set_id(apic, vcpu->
vcpu_id);
1348 apic_set_reg(apic,
APIC_DFR, 0xffffffffU);
1349 apic_set_spiv(apic, 0xff);
1351 kvm_apic_set_ldr(apic, 0);
1357 for (i = 0; i < 8; i++) {
1358 apic_set_reg(apic,
APIC_IRR + 0x10 * i, 0);
1359 apic_set_reg(apic,
APIC_ISR + 0x10 * i, 0);
1360 apic_set_reg(apic,
APIC_TMR + 0x10 * i, 0);
1365 update_divide_count(apic);
1367 if (kvm_vcpu_is_bsp(vcpu))
1370 vcpu->
arch.pv_eoi.msr_val = 0;
1371 apic_update_ppr(apic);
1373 vcpu->
arch.apic_arb_prio = 0;
1374 vcpu->
arch.apic_attention = 0;
1377 "0x%016" PRIx64
", base_address=0x%0lx.\n", __func__,
1378 vcpu, kvm_apic_id(apic),
1388 static bool lapic_is_periodic(
struct kvm_lapic *apic)
1390 return apic_lvtt_period(apic);
1406 u32 reg = kvm_apic_get_reg(apic, lvt_type);
1413 return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
1427 .read = apic_mmio_read,
1428 .write = apic_mmio_write,
1450 if (waitqueue_active(q))
1453 if (lapic_is_periodic(apic)) {
1454 hrtimer_add_expires_ns(&ktimer->
timer, ktimer->
period);
1477 goto nomem_free_apic;
1495 kvm_iodevice_init(&apic->
dev, &apic_mmio_ops);
1512 apic_update_ppr(apic);
1513 highest_irr = apic_find_highest_irr(apic);
1514 if ((highest_irr == -1) ||
1515 ((highest_irr & 0xF0) <= kvm_apic_get_reg(apic,
APIC_PROCPRI)))
1525 if (!kvm_apic_hw_enabled(vcpu->
arch.apic))
1537 if (!kvm_vcpu_has_lapic(vcpu))
1554 apic_set_isr(vector, apic);
1555 apic_update_ppr(apic);
1556 apic_clear_irr(vector, apic);
1570 kvm_apic_set_id(apic, kvm_apic_id(apic));
1573 apic_update_ppr(apic);
1575 update_divide_count(apic);
1576 start_apic_timer(apic);
1587 if (!kvm_vcpu_has_lapic(vcpu))
1590 timer = &vcpu->
arch.apic->lapic_timer.timer;
1602 static void apic_sync_pv_eoi_from_guest(
struct kvm_vcpu *vcpu,
1618 BUG_ON(!pv_eoi_enabled(vcpu));
1619 pending = pv_eoi_get_pending(vcpu);
1625 pv_eoi_clr_pending(vcpu);
1628 vector = apic_set_eoi(apic);
1629 trace_kvm_pv_eoi(apic, vector);
1638 apic_sync_pv_eoi_from_guest(vcpu, vcpu->
arch.apic);
1647 apic_set_tpr(vcpu->
arch.apic, data & 0xff);
1656 static void apic_sync_pv_eoi_to_guest(
struct kvm_vcpu *vcpu,
1659 if (!pv_eoi_enabled(vcpu) ||
1673 pv_eoi_set_pending(apic->
vcpu);
1679 int max_irr, max_isr;
1683 apic_sync_pv_eoi_to_guest(vcpu, apic);
1689 max_irr = apic_find_highest_irr(apic);
1692 max_isr = apic_find_highest_isr(apic);
1695 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1716 if (!irqchip_in_kernel(vcpu->
kvm) || !apic_x2apic_mode(apic))
1722 return apic_reg_write(apic, reg, (
u32)data);
1730 if (!irqchip_in_kernel(vcpu->
kvm) || !apic_x2apic_mode(apic))
1733 if (apic_reg_read(apic, reg, 4, &
low))
1747 if (!kvm_vcpu_has_lapic(vcpu))
1753 return apic_reg_write(apic, reg, (
u32)data);
1761 if (!kvm_vcpu_has_lapic(vcpu))
1764 if (apic_reg_read(apic, reg, 4, &low))
1767 apic_reg_read(apic,
APIC_ICR2, 4, &high);
1769 *data = (((
u64)high) << 32) | low;
1781 if (!pv_eoi_enabled(vcpu))