]> git.ipfire.org Git - thirdparty/linux.git/blame - arch/x86/kvm/lapic.c
KVM: refine the comment of function gfn_to_hva_memslot_prot()
[thirdparty/linux.git] / arch / x86 / kvm / lapic.c
CommitLineData
97222cc8
ED
1
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
9611c187 8 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
97222cc8
ED
9 *
10 * Authors:
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 *
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 */
20
edf88417 21#include <linux/kvm_host.h>
97222cc8
ED
22#include <linux/kvm.h>
23#include <linux/mm.h>
24#include <linux/highmem.h>
25#include <linux/smp.h>
26#include <linux/hrtimer.h>
27#include <linux/io.h>
1767e931 28#include <linux/export.h>
6f6d6a1a 29#include <linux/math64.h>
5a0e3ad6 30#include <linux/slab.h>
97222cc8
ED
31#include <asm/processor.h>
32#include <asm/msr.h>
33#include <asm/page.h>
34#include <asm/current.h>
35#include <asm/apicdef.h>
d0659d94 36#include <asm/delay.h>
60063497 37#include <linux/atomic.h>
c5cc421b 38#include <linux/jump_label.h>
5fdbf976 39#include "kvm_cache_regs.h"
97222cc8 40#include "irq.h"
229456fc 41#include "trace.h"
fc61b800 42#include "x86.h"
00b27a3e 43#include "cpuid.h"
5c919412 44#include "hyperv.h"
97222cc8 45
b682b814
MT
46#ifndef CONFIG_X86_64
47#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48#else
49#define mod_64(x, y) ((x) % (y))
50#endif
51
97222cc8
ED
52#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
97222cc8
ED
57/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
58#define apic_debug(fmt, arg...)
59
97222cc8 60/* 14 is the version for Xeon and Pentium 8.4.8*/
1e6e2755 61#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
97222cc8
ED
62#define LAPIC_MMIO_LENGTH (1 << 12)
63/* followed define is not in apicdef.h */
64#define APIC_SHORT_MASK 0xc0000
65#define APIC_DEST_NOSHORT 0x0
66#define APIC_DEST_MASK 0x800
67#define MAX_APIC_VECTOR 256
ecba9a52 68#define APIC_VECTORS_PER_REG 32
97222cc8 69
394457a9
NA
70#define APIC_BROADCAST 0xFF
71#define X2APIC_BROADCAST 0xFFFFFFFFul
72
3b8a5df6
WL
73static bool lapic_timer_advance_adjust_done = false;
74#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
75/* step-by-step approximation to mitigate fluctuation */
76#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
77
a0c9a822
MT
78static inline int apic_test_vector(int vec, void *bitmap)
79{
80 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
81}
82
10606919
YZ
83bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
84{
85 struct kvm_lapic *apic = vcpu->arch.apic;
86
87 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
88 apic_test_vector(vector, apic->regs + APIC_IRR);
89}
90
97222cc8
ED
91static inline void apic_clear_vector(int vec, void *bitmap)
92{
93 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
94}
95
8680b94b
MT
96static inline int __apic_test_and_set_vector(int vec, void *bitmap)
97{
98 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
99}
100
101static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
102{
103 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
104}
105
c5cc421b 106struct static_key_deferred apic_hw_disabled __read_mostly;
f8c1ea10
GN
107struct static_key_deferred apic_sw_disabled __read_mostly;
108
97222cc8
ED
109static inline int apic_enabled(struct kvm_lapic *apic)
110{
c48f1496 111 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
54e9818f
GN
112}
113
97222cc8
ED
114#define LVT_MASK \
115 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
116
117#define LINT_MASK \
118 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
119 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
120
6e500439
RK
121static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
122{
123 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
124}
125
126static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
127{
128 return apic->vcpu->vcpu_id;
129}
130
e45115b6
RK
131static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
132 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
133 switch (map->mode) {
134 case KVM_APIC_MODE_X2APIC: {
135 u32 offset = (dest_id >> 16) * 16;
0ca52e7b 136 u32 max_apic_id = map->max_apic_id;
e45115b6
RK
137
138 if (offset <= max_apic_id) {
139 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
140
141 *cluster = &map->phys_map[offset];
142 *mask = dest_id & (0xffff >> (16 - cluster_size));
143 } else {
144 *mask = 0;
145 }
3b5a5ffa 146
e45115b6
RK
147 return true;
148 }
149 case KVM_APIC_MODE_XAPIC_FLAT:
150 *cluster = map->xapic_flat_map;
151 *mask = dest_id & 0xff;
152 return true;
153 case KVM_APIC_MODE_XAPIC_CLUSTER:
444fdad8 154 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
e45115b6
RK
155 *mask = dest_id & 0xf;
156 return true;
157 default:
158 /* Not optimized. */
159 return false;
160 }
3548a259
RK
161}
162
af1bae54 163static void kvm_apic_map_free(struct rcu_head *rcu)
3b5a5ffa 164{
af1bae54 165 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
3b5a5ffa 166
af1bae54 167 kvfree(map);
3b5a5ffa
RK
168}
169
1e08ec4a
GN
170static void recalculate_apic_map(struct kvm *kvm)
171{
172 struct kvm_apic_map *new, *old = NULL;
173 struct kvm_vcpu *vcpu;
174 int i;
6e500439 175 u32 max_id = 255; /* enough space for any xAPIC ID */
1e08ec4a
GN
176
177 mutex_lock(&kvm->arch.apic_map_lock);
178
0ca52e7b
RK
179 kvm_for_each_vcpu(i, vcpu, kvm)
180 if (kvm_apic_present(vcpu))
6e500439 181 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
0ca52e7b 182
a7c3e901
MH
183 new = kvzalloc(sizeof(struct kvm_apic_map) +
184 sizeof(struct kvm_lapic *) * ((u64)max_id + 1), GFP_KERNEL);
0ca52e7b 185
1e08ec4a
GN
186 if (!new)
187 goto out;
188
0ca52e7b
RK
189 new->max_apic_id = max_id;
190
173beedc
NA
191 kvm_for_each_vcpu(i, vcpu, kvm) {
192 struct kvm_lapic *apic = vcpu->arch.apic;
e45115b6
RK
193 struct kvm_lapic **cluster;
194 u16 mask;
5bd5db38
RK
195 u32 ldr;
196 u8 xapic_id;
197 u32 x2apic_id;
1e08ec4a 198
df04d1d1
RK
199 if (!kvm_apic_present(vcpu))
200 continue;
201
5bd5db38
RK
202 xapic_id = kvm_xapic_id(apic);
203 x2apic_id = kvm_x2apic_id(apic);
204
205 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
206 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
207 x2apic_id <= new->max_apic_id)
208 new->phys_map[x2apic_id] = apic;
209 /*
210 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
211 * prevent them from masking VCPUs with APIC ID <= 0xff.
212 */
213 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
214 new->phys_map[xapic_id] = apic;
3548a259 215
6e500439
RK
216 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
217
3b5a5ffa
RK
218 if (apic_x2apic_mode(apic)) {
219 new->mode |= KVM_APIC_MODE_X2APIC;
220 } else if (ldr) {
221 ldr = GET_APIC_LOGICAL_ID(ldr);
dfb95954 222 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
3b5a5ffa
RK
223 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
224 else
225 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
226 }
227
e45115b6 228 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
3548a259
RK
229 continue;
230
e45115b6
RK
231 if (mask)
232 cluster[ffs(mask) - 1] = apic;
1e08ec4a
GN
233 }
234out:
235 old = rcu_dereference_protected(kvm->arch.apic_map,
236 lockdep_is_held(&kvm->arch.apic_map_lock));
237 rcu_assign_pointer(kvm->arch.apic_map, new);
238 mutex_unlock(&kvm->arch.apic_map_lock);
239
240 if (old)
af1bae54 241 call_rcu(&old->rcu, kvm_apic_map_free);
c7c9c56c 242
b053b2ae 243 kvm_make_scan_ioapic_request(kvm);
1e08ec4a
GN
244}
245
1e1b6c26
NA
246static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
247{
e462755c 248 bool enabled = val & APIC_SPIV_APIC_ENABLED;
1e1b6c26 249
1e6e2755 250 kvm_lapic_set_reg(apic, APIC_SPIV, val);
e462755c
RK
251
252 if (enabled != apic->sw_enabled) {
253 apic->sw_enabled = enabled;
254 if (enabled) {
1e1b6c26
NA
255 static_key_slow_dec_deferred(&apic_sw_disabled);
256 recalculate_apic_map(apic->vcpu->kvm);
257 } else
258 static_key_slow_inc(&apic_sw_disabled.key);
259 }
260}
261
a92e2543 262static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
1e08ec4a 263{
1e6e2755 264 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
1e08ec4a
GN
265 recalculate_apic_map(apic->vcpu->kvm);
266}
267
268static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
269{
1e6e2755 270 kvm_lapic_set_reg(apic, APIC_LDR, id);
1e08ec4a
GN
271 recalculate_apic_map(apic->vcpu->kvm);
272}
273
e872fa94
DDAG
274static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
275{
276 return ((id >> 4) << 16) | (1 << (id & 0xf));
277}
278
a92e2543 279static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
257b9a5f 280{
e872fa94 281 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
257b9a5f 282
6e500439
RK
283 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
284
a92e2543 285 kvm_lapic_set_reg(apic, APIC_ID, id);
1e6e2755 286 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
257b9a5f
RK
287 recalculate_apic_map(apic->vcpu->kvm);
288}
289
97222cc8
ED
290static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
291{
dfb95954 292 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
97222cc8
ED
293}
294
295static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
296{
dfb95954 297 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
97222cc8
ED
298}
299
a3e06bbe
LJ
300static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
301{
f30ebc31 302 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
a3e06bbe
LJ
303}
304
97222cc8
ED
305static inline int apic_lvtt_period(struct kvm_lapic *apic)
306{
f30ebc31 307 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
a3e06bbe
LJ
308}
309
310static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
311{
f30ebc31 312 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
97222cc8
ED
313}
314
cc6e462c
JK
315static inline int apic_lvt_nmi_mode(u32 lvt_val)
316{
317 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
318}
319
fc61b800
GN
320void kvm_apic_set_version(struct kvm_vcpu *vcpu)
321{
322 struct kvm_lapic *apic = vcpu->arch.apic;
323 struct kvm_cpuid_entry2 *feat;
324 u32 v = APIC_VERSION;
325
bce87cce 326 if (!lapic_in_kernel(vcpu))
fc61b800
GN
327 return;
328
0bcc3fb9
VK
329 /*
330 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
331 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
332 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
333 * version first and level-triggered interrupts never get EOIed in
334 * IOAPIC.
335 */
fc61b800 336 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
0bcc3fb9
VK
337 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
338 !ioapic_in_kernel(vcpu->kvm))
fc61b800 339 v |= APIC_LVR_DIRECTED_EOI;
1e6e2755 340 kvm_lapic_set_reg(apic, APIC_LVR, v);
fc61b800
GN
341}
342
1e6e2755 343static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
a3e06bbe 344 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
97222cc8
ED
345 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
346 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
347 LINT_MASK, LINT_MASK, /* LVT0-1 */
348 LVT_MASK /* LVTERR */
349};
350
351static int find_highest_vector(void *bitmap)
352{
ecba9a52
TY
353 int vec;
354 u32 *reg;
97222cc8 355
ecba9a52
TY
356 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
357 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
358 reg = bitmap + REG_POS(vec);
359 if (*reg)
810e6def 360 return __fls(*reg) + vec;
ecba9a52 361 }
97222cc8 362
ecba9a52 363 return -1;
97222cc8
ED
364}
365
8680b94b
MT
366static u8 count_vectors(void *bitmap)
367{
ecba9a52
TY
368 int vec;
369 u32 *reg;
8680b94b 370 u8 count = 0;
ecba9a52
TY
371
372 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
373 reg = bitmap + REG_POS(vec);
374 count += hweight32(*reg);
375 }
376
8680b94b
MT
377 return count;
378}
379
e7387b0e 380bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
a20ed54d 381{
810e6def 382 u32 i, vec;
e7387b0e
LA
383 u32 pir_val, irr_val, prev_irr_val;
384 int max_updated_irr;
385
386 max_updated_irr = -1;
387 *max_irr = -1;
a20ed54d 388
810e6def 389 for (i = vec = 0; i <= 7; i++, vec += 32) {
ad361091 390 pir_val = READ_ONCE(pir[i]);
810e6def 391 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
ad361091 392 if (pir_val) {
e7387b0e 393 prev_irr_val = irr_val;
810e6def
PB
394 irr_val |= xchg(&pir[i], 0);
395 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
e7387b0e
LA
396 if (prev_irr_val != irr_val) {
397 max_updated_irr =
398 __fls(irr_val ^ prev_irr_val) + vec;
399 }
ad361091 400 }
810e6def 401 if (irr_val)
e7387b0e 402 *max_irr = __fls(irr_val) + vec;
a20ed54d 403 }
810e6def 404
e7387b0e
LA
405 return ((max_updated_irr != -1) &&
406 (max_updated_irr == *max_irr));
a20ed54d 407}
705699a1
WV
408EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
409
e7387b0e 410bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
705699a1
WV
411{
412 struct kvm_lapic *apic = vcpu->arch.apic;
413
e7387b0e 414 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
705699a1 415}
a20ed54d
YZ
416EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
417
33e4c686 418static inline int apic_search_irr(struct kvm_lapic *apic)
97222cc8 419{
33e4c686 420 return find_highest_vector(apic->regs + APIC_IRR);
97222cc8
ED
421}
422
423static inline int apic_find_highest_irr(struct kvm_lapic *apic)
424{
425 int result;
426
c7c9c56c
YZ
427 /*
428 * Note that irr_pending is just a hint. It will be always
429 * true with virtual interrupt delivery enabled.
430 */
33e4c686
GN
431 if (!apic->irr_pending)
432 return -1;
433
434 result = apic_search_irr(apic);
97222cc8
ED
435 ASSERT(result == -1 || result >= 16);
436
437 return result;
438}
439
33e4c686
GN
440static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
441{
56cc2406
WL
442 struct kvm_vcpu *vcpu;
443
444 vcpu = apic->vcpu;
445
d62caabb 446 if (unlikely(vcpu->arch.apicv_active)) {
b95234c8 447 /* need to update RVI */
f210f757 448 apic_clear_vector(vec, apic->regs + APIC_IRR);
b95234c8
PB
449 kvm_x86_ops->hwapic_irr_update(vcpu,
450 apic_find_highest_irr(apic));
f210f757
NA
451 } else {
452 apic->irr_pending = false;
453 apic_clear_vector(vec, apic->regs + APIC_IRR);
454 if (apic_search_irr(apic) != -1)
455 apic->irr_pending = true;
56cc2406 456 }
33e4c686
GN
457}
458
8680b94b
MT
459static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
460{
56cc2406
WL
461 struct kvm_vcpu *vcpu;
462
463 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
464 return;
465
466 vcpu = apic->vcpu;
fc57ac2c 467
8680b94b 468 /*
56cc2406
WL
469 * With APIC virtualization enabled, all caching is disabled
470 * because the processor can modify ISR under the hood. Instead
471 * just set SVI.
8680b94b 472 */
d62caabb 473 if (unlikely(vcpu->arch.apicv_active))
67c9dddc 474 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
56cc2406
WL
475 else {
476 ++apic->isr_count;
477 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
478 /*
479 * ISR (in service register) bit is set when injecting an interrupt.
480 * The highest vector is injected. Thus the latest bit set matches
481 * the highest bit in ISR.
482 */
483 apic->highest_isr_cache = vec;
484 }
8680b94b
MT
485}
486
fc57ac2c
PB
487static inline int apic_find_highest_isr(struct kvm_lapic *apic)
488{
489 int result;
490
491 /*
492 * Note that isr_count is always 1, and highest_isr_cache
493 * is always -1, with APIC virtualization enabled.
494 */
495 if (!apic->isr_count)
496 return -1;
497 if (likely(apic->highest_isr_cache != -1))
498 return apic->highest_isr_cache;
499
500 result = find_highest_vector(apic->regs + APIC_ISR);
501 ASSERT(result == -1 || result >= 16);
502
503 return result;
504}
505
8680b94b
MT
506static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
507{
fc57ac2c
PB
508 struct kvm_vcpu *vcpu;
509 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
510 return;
511
512 vcpu = apic->vcpu;
513
514 /*
515 * We do get here for APIC virtualization enabled if the guest
516 * uses the Hyper-V APIC enlightenment. In this case we may need
517 * to trigger a new interrupt delivery by writing the SVI field;
518 * on the other hand isr_count and highest_isr_cache are unused
519 * and must be left alone.
520 */
d62caabb 521 if (unlikely(vcpu->arch.apicv_active))
67c9dddc 522 kvm_x86_ops->hwapic_isr_update(vcpu,
fc57ac2c
PB
523 apic_find_highest_isr(apic));
524 else {
8680b94b 525 --apic->isr_count;
fc57ac2c
PB
526 BUG_ON(apic->isr_count < 0);
527 apic->highest_isr_cache = -1;
528 }
8680b94b
MT
529}
530
6e5d865c
YS
531int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
532{
33e4c686
GN
533 /* This may race with setting of irr in __apic_accept_irq() and
534 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
535 * will cause vmexit immediately and the value will be recalculated
536 * on the next vmentry.
537 */
f8543d6a 538 return apic_find_highest_irr(vcpu->arch.apic);
6e5d865c 539}
76dfafd5 540EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
6e5d865c 541
6da7e3f6 542static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
b4f2225c 543 int vector, int level, int trig_mode,
9e4aabe2 544 struct dest_map *dest_map);
6da7e3f6 545
b4f2225c 546int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
9e4aabe2 547 struct dest_map *dest_map)
97222cc8 548{
ad312c7c 549 struct kvm_lapic *apic = vcpu->arch.apic;
8be5453f 550
58c2dde1 551 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
b4f2225c 552 irq->level, irq->trig_mode, dest_map);
97222cc8
ED
553}
554
4180bf1b 555int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
bdf7ffc8 556 unsigned long ipi_bitmap_high, u32 min,
4180bf1b
WL
557 unsigned long icr, int op_64_bit)
558{
559 int i;
560 struct kvm_apic_map *map;
561 struct kvm_vcpu *vcpu;
562 struct kvm_lapic_irq irq = {0};
563 int cluster_size = op_64_bit ? 64 : 32;
564 int count = 0;
565
566 irq.vector = icr & APIC_VECTOR_MASK;
567 irq.delivery_mode = icr & APIC_MODE_MASK;
568 irq.level = (icr & APIC_INT_ASSERT) != 0;
569 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
570
571 if (icr & APIC_DEST_MASK)
572 return -KVM_EINVAL;
573 if (icr & APIC_SHORT_MASK)
574 return -KVM_EINVAL;
575
576 rcu_read_lock();
577 map = rcu_dereference(kvm->arch.apic_map);
578
bdf7ffc8
WL
579 if (min > map->max_apic_id)
580 goto out;
4180bf1b 581 /* Bits above cluster_size are masked in the caller. */
bdf7ffc8
WL
582 for_each_set_bit(i, &ipi_bitmap_low,
583 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
584 if (map->phys_map[min + i]) {
585 vcpu = map->phys_map[min + i]->vcpu;
586 count += kvm_apic_set_irq(vcpu, &irq, NULL);
587 }
4180bf1b
WL
588 }
589
590 min += cluster_size;
bdf7ffc8
WL
591
592 if (min > map->max_apic_id)
593 goto out;
594
595 for_each_set_bit(i, &ipi_bitmap_high,
596 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
597 if (map->phys_map[min + i]) {
598 vcpu = map->phys_map[min + i]->vcpu;
599 count += kvm_apic_set_irq(vcpu, &irq, NULL);
600 }
4180bf1b
WL
601 }
602
bdf7ffc8 603out:
4180bf1b
WL
604 rcu_read_unlock();
605 return count;
606}
607
ae7a2a3f
MT
608static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
609{
4e335d9e
PB
610
611 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
612 sizeof(val));
ae7a2a3f
MT
613}
614
615static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
616{
4e335d9e
PB
617
618 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
619 sizeof(*val));
ae7a2a3f
MT
620}
621
622static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
623{
624 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
625}
626
627static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
628{
629 u8 val;
630 if (pv_eoi_get_user(vcpu, &val) < 0)
631 apic_debug("Can't read EOI MSR value: 0x%llx\n",
96893977 632 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
ae7a2a3f
MT
633 return val & 0x1;
634}
635
636static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
637{
638 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
639 apic_debug("Can't set EOI MSR value: 0x%llx\n",
96893977 640 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
ae7a2a3f
MT
641 return;
642 }
643 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
644}
645
646static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
647{
648 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
649 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
96893977 650 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
ae7a2a3f
MT
651 return;
652 }
653 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
654}
655
b3c045d3
PB
656static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
657{
3d92789f 658 int highest_irr;
fa59cc00 659 if (apic->vcpu->arch.apicv_active)
76dfafd5
PB
660 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
661 else
662 highest_irr = apic_find_highest_irr(apic);
b3c045d3
PB
663 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
664 return -1;
665 return highest_irr;
666}
667
668static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
97222cc8 669{
3842d135 670 u32 tpr, isrv, ppr, old_ppr;
97222cc8
ED
671 int isr;
672
dfb95954
SS
673 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
674 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
97222cc8
ED
675 isr = apic_find_highest_isr(apic);
676 isrv = (isr != -1) ? isr : 0;
677
678 if ((tpr & 0xf0) >= (isrv & 0xf0))
679 ppr = tpr & 0xff;
680 else
681 ppr = isrv & 0xf0;
682
683 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
684 apic, ppr, isr, isrv);
685
b3c045d3
PB
686 *new_ppr = ppr;
687 if (old_ppr != ppr)
1e6e2755 688 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
b3c045d3
PB
689
690 return ppr < old_ppr;
691}
692
693static void apic_update_ppr(struct kvm_lapic *apic)
694{
695 u32 ppr;
696
26fbbee5
PB
697 if (__apic_update_ppr(apic, &ppr) &&
698 apic_has_interrupt_for_ppr(apic, ppr) != -1)
b3c045d3 699 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
97222cc8
ED
700}
701
eb90f341
PB
702void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
703{
704 apic_update_ppr(vcpu->arch.apic);
705}
706EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
707
97222cc8
ED
708static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
709{
1e6e2755 710 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
97222cc8
ED
711 apic_update_ppr(apic);
712}
713
03d2249e 714static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
394457a9 715{
b4535b58
RK
716 return mda == (apic_x2apic_mode(apic) ?
717 X2APIC_BROADCAST : APIC_BROADCAST);
394457a9
NA
718}
719
03d2249e 720static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
97222cc8 721{
03d2249e
RK
722 if (kvm_apic_broadcast(apic, mda))
723 return true;
724
725 if (apic_x2apic_mode(apic))
6e500439 726 return mda == kvm_x2apic_id(apic);
03d2249e 727
5bd5db38
RK
728 /*
729 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
730 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
731 * this allows unique addressing of VCPUs with APIC ID over 0xff.
732 * The 0xff condition is needed because writeable xAPIC ID.
733 */
734 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
735 return true;
736
b4535b58 737 return mda == kvm_xapic_id(apic);
97222cc8
ED
738}
739
52c233a4 740static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
97222cc8 741{
0105d1a5
GN
742 u32 logical_id;
743
394457a9 744 if (kvm_apic_broadcast(apic, mda))
9368b567 745 return true;
394457a9 746
dfb95954 747 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
97222cc8 748
9368b567 749 if (apic_x2apic_mode(apic))
8a395363
RK
750 return ((logical_id >> 16) == (mda >> 16))
751 && (logical_id & mda & 0xffff) != 0;
97222cc8 752
9368b567 753 logical_id = GET_APIC_LOGICAL_ID(logical_id);
97222cc8 754
dfb95954 755 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
97222cc8 756 case APIC_DFR_FLAT:
9368b567 757 return (logical_id & mda) != 0;
97222cc8 758 case APIC_DFR_CLUSTER:
9368b567
RK
759 return ((logical_id >> 4) == (mda >> 4))
760 && (logical_id & mda & 0xf) != 0;
97222cc8 761 default:
7712de87 762 apic_debug("Bad DFR vcpu %d: %08x\n",
dfb95954 763 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
9368b567 764 return false;
97222cc8 765 }
97222cc8
ED
766}
767
c519265f
RK
768/* The KVM local APIC implementation has two quirks:
769 *
b4535b58
RK
770 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
771 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
772 * KVM doesn't do that aliasing.
c519265f
RK
773 *
774 * - in-kernel IOAPIC messages have to be delivered directly to
775 * x2APIC, because the kernel does not support interrupt remapping.
776 * In order to support broadcast without interrupt remapping, x2APIC
777 * rewrites the destination of non-IPI messages from APIC_BROADCAST
778 * to X2APIC_BROADCAST.
779 *
780 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
781 * important when userspace wants to use x2APIC-format MSIs, because
782 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
03d2249e 783 */
c519265f
RK
784static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
785 struct kvm_lapic *source, struct kvm_lapic *target)
03d2249e
RK
786{
787 bool ipi = source != NULL;
03d2249e 788
c519265f 789 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
b4535b58 790 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
03d2249e
RK
791 return X2APIC_BROADCAST;
792
b4535b58 793 return dest_id;
03d2249e
RK
794}
795
52c233a4 796bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
394457a9 797 int short_hand, unsigned int dest, int dest_mode)
97222cc8 798{
ad312c7c 799 struct kvm_lapic *target = vcpu->arch.apic;
c519265f 800 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
97222cc8
ED
801
802 apic_debug("target %p, source %p, dest 0x%x, "
343f94fe 803 "dest_mode 0x%x, short_hand 0x%x\n",
97222cc8
ED
804 target, source, dest, dest_mode, short_hand);
805
bd371396 806 ASSERT(target);
97222cc8
ED
807 switch (short_hand) {
808 case APIC_DEST_NOSHORT:
3697f302 809 if (dest_mode == APIC_DEST_PHYSICAL)
03d2249e 810 return kvm_apic_match_physical_addr(target, mda);
343f94fe 811 else
03d2249e 812 return kvm_apic_match_logical_addr(target, mda);
97222cc8 813 case APIC_DEST_SELF:
9368b567 814 return target == source;
97222cc8 815 case APIC_DEST_ALLINC:
9368b567 816 return true;
97222cc8 817 case APIC_DEST_ALLBUT:
9368b567 818 return target != source;
97222cc8 819 default:
7712de87
JK
820 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
821 short_hand);
9368b567 822 return false;
97222cc8 823 }
97222cc8 824}
1e6e2755 825EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
97222cc8 826
52004014
FW
827int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
828 const unsigned long *bitmap, u32 bitmap_size)
829{
830 u32 mod;
831 int i, idx = -1;
832
833 mod = vector % dest_vcpus;
834
835 for (i = 0; i <= mod; i++) {
836 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
837 BUG_ON(idx == bitmap_size);
838 }
839
840 return idx;
841}
842
4efd805f
RK
843static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
844{
845 if (!kvm->arch.disabled_lapic_found) {
846 kvm->arch.disabled_lapic_found = true;
847 printk(KERN_INFO
848 "Disabled LAPIC found during irq injection\n");
849 }
850}
851
c519265f
RK
852static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
853 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
1e08ec4a 854{
c519265f
RK
855 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
856 if ((irq->dest_id == APIC_BROADCAST &&
857 map->mode != KVM_APIC_MODE_X2APIC))
858 return true;
859 if (irq->dest_id == X2APIC_BROADCAST)
860 return true;
861 } else {
862 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
863 if (irq->dest_id == (x2apic_ipi ?
864 X2APIC_BROADCAST : APIC_BROADCAST))
865 return true;
866 }
1e08ec4a 867
c519265f
RK
868 return false;
869}
1e08ec4a 870
64aa47bf
RK
871/* Return true if the interrupt can be handled by using *bitmap as index mask
872 * for valid destinations in *dst array.
873 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
874 * Note: we may have zero kvm_lapic destinations when we return true, which
875 * means that the interrupt should be dropped. In this case, *bitmap would be
876 * zero and *dst undefined.
877 */
878static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
879 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
880 struct kvm_apic_map *map, struct kvm_lapic ***dst,
881 unsigned long *bitmap)
882{
883 int i, lowest;
1e08ec4a 884
64aa47bf
RK
885 if (irq->shorthand == APIC_DEST_SELF && src) {
886 *dst = src;
887 *bitmap = 1;
888 return true;
889 } else if (irq->shorthand)
1e08ec4a
GN
890 return false;
891
c519265f 892 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
9ea369b0
RK
893 return false;
894
64aa47bf 895 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
0ca52e7b 896 if (irq->dest_id > map->max_apic_id) {
64aa47bf
RK
897 *bitmap = 0;
898 } else {
899 *dst = &map->phys_map[irq->dest_id];
900 *bitmap = 1;
901 }
1e08ec4a 902 return true;
bea15428 903 }
698f9755 904
e45115b6
RK
905 *bitmap = 0;
906 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
907 (u16 *)bitmap))
1e08ec4a 908 return false;
fa834e91 909
64aa47bf
RK
910 if (!kvm_lowest_prio_delivery(irq))
911 return true;
3548a259 912
64aa47bf
RK
913 if (!kvm_vector_hashing_enabled()) {
914 lowest = -1;
915 for_each_set_bit(i, bitmap, 16) {
916 if (!(*dst)[i])
917 continue;
918 if (lowest < 0)
919 lowest = i;
920 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
921 (*dst)[lowest]->vcpu) < 0)
922 lowest = i;
3548a259 923 }
64aa47bf
RK
924 } else {
925 if (!*bitmap)
926 return true;
3548a259 927
64aa47bf
RK
928 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
929 bitmap, 16);
45c3094a 930
64aa47bf
RK
931 if (!(*dst)[lowest]) {
932 kvm_apic_disabled_lapic_found(kvm);
933 *bitmap = 0;
934 return true;
935 }
936 }
1e08ec4a 937
64aa47bf 938 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
1e08ec4a 939
64aa47bf
RK
940 return true;
941}
52004014 942
64aa47bf
RK
943bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
944 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
945{
946 struct kvm_apic_map *map;
947 unsigned long bitmap;
948 struct kvm_lapic **dst = NULL;
949 int i;
950 bool ret;
52004014 951
64aa47bf 952 *r = -1;
52004014 953
64aa47bf
RK
954 if (irq->shorthand == APIC_DEST_SELF) {
955 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
956 return true;
957 }
52004014 958
64aa47bf
RK
959 rcu_read_lock();
960 map = rcu_dereference(kvm->arch.apic_map);
52004014 961
64aa47bf 962 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
0624fca9
PB
963 if (ret) {
964 *r = 0;
64aa47bf
RK
965 for_each_set_bit(i, &bitmap, 16) {
966 if (!dst[i])
967 continue;
64aa47bf 968 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
1e08ec4a 969 }
0624fca9 970 }
1e08ec4a 971
1e08ec4a
GN
972 rcu_read_unlock();
973 return ret;
974}
975
6228a0da
FW
976/*
977 * This routine tries to handler interrupts in posted mode, here is how
978 * it deals with different cases:
979 * - For single-destination interrupts, handle it in posted mode
980 * - Else if vector hashing is enabled and it is a lowest-priority
981 * interrupt, handle it in posted mode and use the following mechanism
982 * to find the destinaiton vCPU.
983 * 1. For lowest-priority interrupts, store all the possible
984 * destination vCPUs in an array.
985 * 2. Use "guest vector % max number of destination vCPUs" to find
986 * the right destination vCPU in the array for the lowest-priority
987 * interrupt.
988 * - Otherwise, use remapped mode to inject the interrupt.
989 */
8feb4a04
FW
990bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
991 struct kvm_vcpu **dest_vcpu)
992{
993 struct kvm_apic_map *map;
64aa47bf
RK
994 unsigned long bitmap;
995 struct kvm_lapic **dst = NULL;
8feb4a04 996 bool ret = false;
8feb4a04
FW
997
998 if (irq->shorthand)
999 return false;
1000
1001 rcu_read_lock();
1002 map = rcu_dereference(kvm->arch.apic_map);
1003
64aa47bf
RK
1004 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1005 hweight16(bitmap) == 1) {
1006 unsigned long i = find_first_bit(&bitmap, 16);
6228a0da 1007
64aa47bf
RK
1008 if (dst[i]) {
1009 *dest_vcpu = dst[i]->vcpu;
1010 ret = true;
6228a0da 1011 }
8feb4a04
FW
1012 }
1013
8feb4a04
FW
1014 rcu_read_unlock();
1015 return ret;
1016}
1017
97222cc8
ED
1018/*
1019 * Add a pending IRQ into lapic.
1020 * Return 1 if successfully added and 0 if discarded.
1021 */
1022static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
b4f2225c 1023 int vector, int level, int trig_mode,
9e4aabe2 1024 struct dest_map *dest_map)
97222cc8 1025{
6da7e3f6 1026 int result = 0;
c5ec1534 1027 struct kvm_vcpu *vcpu = apic->vcpu;
97222cc8 1028
a183b638
PB
1029 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1030 trig_mode, vector);
97222cc8 1031 switch (delivery_mode) {
97222cc8 1032 case APIC_DM_LOWEST:
e1035715
GN
1033 vcpu->arch.apic_arb_prio++;
1034 case APIC_DM_FIXED:
bdaffe1d
PB
1035 if (unlikely(trig_mode && !level))
1036 break;
1037
97222cc8
ED
1038 /* FIXME add logic for vcpu on reset */
1039 if (unlikely(!apic_enabled(apic)))
1040 break;
1041
11f5cc05
JK
1042 result = 1;
1043
9daa5007 1044 if (dest_map) {
9e4aabe2 1045 __set_bit(vcpu->vcpu_id, dest_map->map);
9daa5007
JR
1046 dest_map->vectors[vcpu->vcpu_id] = vector;
1047 }
a5d36f82 1048
bdaffe1d
PB
1049 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1050 if (trig_mode)
1e6e2755 1051 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
bdaffe1d
PB
1052 else
1053 apic_clear_vector(vector, apic->regs + APIC_TMR);
1054 }
1055
d62caabb 1056 if (vcpu->arch.apicv_active)
5a71785d 1057 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
11f5cc05 1058 else {
1e6e2755 1059 kvm_lapic_set_irr(vector, apic);
5a71785d
YZ
1060
1061 kvm_make_request(KVM_REQ_EVENT, vcpu);
1062 kvm_vcpu_kick(vcpu);
1063 }
97222cc8
ED
1064 break;
1065
1066 case APIC_DM_REMRD:
24d2166b
R
1067 result = 1;
1068 vcpu->arch.pv.pv_unhalted = 1;
1069 kvm_make_request(KVM_REQ_EVENT, vcpu);
1070 kvm_vcpu_kick(vcpu);
97222cc8
ED
1071 break;
1072
1073 case APIC_DM_SMI:
64d60670
PB
1074 result = 1;
1075 kvm_make_request(KVM_REQ_SMI, vcpu);
1076 kvm_vcpu_kick(vcpu);
97222cc8 1077 break;
3419ffc8 1078
97222cc8 1079 case APIC_DM_NMI:
6da7e3f6 1080 result = 1;
3419ffc8 1081 kvm_inject_nmi(vcpu);
26df99c6 1082 kvm_vcpu_kick(vcpu);
97222cc8
ED
1083 break;
1084
1085 case APIC_DM_INIT:
a52315e1 1086 if (!trig_mode || level) {
6da7e3f6 1087 result = 1;
66450a21
JK
1088 /* assumes that there are only KVM_APIC_INIT/SIPI */
1089 apic->pending_events = (1UL << KVM_APIC_INIT);
1090 /* make sure pending_events is visible before sending
1091 * the request */
1092 smp_wmb();
3842d135 1093 kvm_make_request(KVM_REQ_EVENT, vcpu);
c5ec1534
HQ
1094 kvm_vcpu_kick(vcpu);
1095 } else {
1b10bf31
JK
1096 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
1097 vcpu->vcpu_id);
c5ec1534 1098 }
97222cc8
ED
1099 break;
1100
1101 case APIC_DM_STARTUP:
1b10bf31
JK
1102 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
1103 vcpu->vcpu_id, vector);
66450a21
JK
1104 result = 1;
1105 apic->sipi_vector = vector;
1106 /* make sure sipi_vector is visible for the receiver */
1107 smp_wmb();
1108 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1109 kvm_make_request(KVM_REQ_EVENT, vcpu);
1110 kvm_vcpu_kick(vcpu);
97222cc8
ED
1111 break;
1112
23930f95
JK
1113 case APIC_DM_EXTINT:
1114 /*
1115 * Should only be called by kvm_apic_local_deliver() with LVT0,
1116 * before NMI watchdog was enabled. Already handled by
1117 * kvm_apic_accept_pic_intr().
1118 */
1119 break;
1120
97222cc8
ED
1121 default:
1122 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1123 delivery_mode);
1124 break;
1125 }
1126 return result;
1127}
1128
e1035715 1129int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
8be5453f 1130{
e1035715 1131 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
8be5453f
ZX
1132}
1133
3bb345f3
PB
1134static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1135{
6308630b 1136 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
3bb345f3
PB
1137}
1138
c7c9c56c
YZ
1139static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1140{
7543a635
SR
1141 int trigger_mode;
1142
1143 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1144 if (!kvm_ioapic_handles_vector(apic, vector))
1145 return;
3bb345f3 1146
7543a635
SR
1147 /* Request a KVM exit to inform the userspace IOAPIC. */
1148 if (irqchip_split(apic->vcpu->kvm)) {
1149 apic->vcpu->arch.pending_ioapic_eoi = vector;
1150 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1151 return;
c7c9c56c 1152 }
7543a635
SR
1153
1154 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1155 trigger_mode = IOAPIC_LEVEL_TRIG;
1156 else
1157 trigger_mode = IOAPIC_EDGE_TRIG;
1158
1159 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
c7c9c56c
YZ
1160}
1161
ae7a2a3f 1162static int apic_set_eoi(struct kvm_lapic *apic)
97222cc8
ED
1163{
1164 int vector = apic_find_highest_isr(apic);
ae7a2a3f
MT
1165
1166 trace_kvm_eoi(apic, vector);
1167
97222cc8
ED
1168 /*
1169 * Not every write EOI will has corresponding ISR,
1170 * one example is when Kernel check timer on setup_IO_APIC
1171 */
1172 if (vector == -1)
ae7a2a3f 1173 return vector;
97222cc8 1174
8680b94b 1175 apic_clear_isr(vector, apic);
97222cc8
ED
1176 apic_update_ppr(apic);
1177
5c919412
AS
1178 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1179 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1180
c7c9c56c 1181 kvm_ioapic_send_eoi(apic, vector);
3842d135 1182 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
ae7a2a3f 1183 return vector;
97222cc8
ED
1184}
1185
c7c9c56c
YZ
1186/*
1187 * this interface assumes a trap-like exit, which has already finished
1188 * desired side effect including vISR and vPPR update.
1189 */
1190void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1191{
1192 struct kvm_lapic *apic = vcpu->arch.apic;
1193
1194 trace_kvm_eoi(apic, vector);
1195
1196 kvm_ioapic_send_eoi(apic, vector);
1197 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1198}
1199EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1200
97222cc8
ED
1201static void apic_send_ipi(struct kvm_lapic *apic)
1202{
dfb95954
SS
1203 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1204 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
58c2dde1 1205 struct kvm_lapic_irq irq;
97222cc8 1206
58c2dde1
GN
1207 irq.vector = icr_low & APIC_VECTOR_MASK;
1208 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1209 irq.dest_mode = icr_low & APIC_DEST_MASK;
b7cb2231 1210 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
58c2dde1
GN
1211 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1212 irq.shorthand = icr_low & APIC_SHORT_MASK;
93bbf0b8 1213 irq.msi_redir_hint = false;
0105d1a5
GN
1214 if (apic_x2apic_mode(apic))
1215 irq.dest_id = icr_high;
1216 else
1217 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
97222cc8 1218
1000ff8d
GN
1219 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1220
97222cc8
ED
1221 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1222 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
93bbf0b8
JS
1223 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1224 "msi_redir_hint 0x%x\n",
9b5843dd 1225 icr_high, icr_low, irq.shorthand, irq.dest_id,
58c2dde1 1226 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
93bbf0b8 1227 irq.vector, irq.msi_redir_hint);
58c2dde1 1228
b4f2225c 1229 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
97222cc8
ED
1230}
1231
1232static u32 apic_get_tmcct(struct kvm_lapic *apic)
1233{
8003c9ae 1234 ktime_t remaining, now;
b682b814 1235 s64 ns;
9da8f4e8 1236 u32 tmcct;
97222cc8
ED
1237
1238 ASSERT(apic != NULL);
1239
9da8f4e8 1240 /* if initial count is 0, current count should also be 0 */
dfb95954 1241 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
b963a22e 1242 apic->lapic_timer.period == 0)
9da8f4e8
KP
1243 return 0;
1244
5587859f 1245 now = ktime_get();
8003c9ae 1246 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
b682b814 1247 if (ktime_to_ns(remaining) < 0)
8b0e1953 1248 remaining = 0;
b682b814 1249
d3c7b77d
MT
1250 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1251 tmcct = div64_u64(ns,
1252 (APIC_BUS_CYCLE_NS * apic->divide_count));
97222cc8
ED
1253
1254 return tmcct;
1255}
1256
b209749f
AK
1257static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1258{
1259 struct kvm_vcpu *vcpu = apic->vcpu;
1260 struct kvm_run *run = vcpu->run;
1261
a8eeb04a 1262 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
5fdbf976 1263 run->tpr_access.rip = kvm_rip_read(vcpu);
b209749f
AK
1264 run->tpr_access.is_write = write;
1265}
1266
1267static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1268{
1269 if (apic->vcpu->arch.tpr_access_reporting)
1270 __report_tpr_access(apic, write);
1271}
1272
97222cc8
ED
1273static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1274{
1275 u32 val = 0;
1276
1277 if (offset >= LAPIC_MMIO_LENGTH)
1278 return 0;
1279
1280 switch (offset) {
1281 case APIC_ARBPRI:
7712de87 1282 apic_debug("Access APIC ARBPRI register which is for P6\n");
97222cc8
ED
1283 break;
1284
1285 case APIC_TMCCT: /* Timer CCR */
a3e06bbe
LJ
1286 if (apic_lvtt_tscdeadline(apic))
1287 return 0;
1288
97222cc8
ED
1289 val = apic_get_tmcct(apic);
1290 break;
4a4541a4
AK
1291 case APIC_PROCPRI:
1292 apic_update_ppr(apic);
dfb95954 1293 val = kvm_lapic_get_reg(apic, offset);
4a4541a4 1294 break;
b209749f
AK
1295 case APIC_TASKPRI:
1296 report_tpr_access(apic, false);
1297 /* fall thru */
97222cc8 1298 default:
dfb95954 1299 val = kvm_lapic_get_reg(apic, offset);
97222cc8
ED
1300 break;
1301 }
1302
1303 return val;
1304}
1305
d76685c4
GH
1306static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1307{
1308 return container_of(dev, struct kvm_lapic, dev);
1309}
1310
1e6e2755 1311int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
0105d1a5 1312 void *data)
97222cc8 1313{
97222cc8
ED
1314 unsigned char alignment = offset & 0xf;
1315 u32 result;
d5b0b5b1 1316 /* this bitmask has a bit cleared for each reserved register */
0105d1a5 1317 static const u64 rmask = 0x43ff01ffffffe70cULL;
97222cc8
ED
1318
1319 if ((alignment + len) > 4) {
4088bb3c
GN
1320 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1321 offset, len);
0105d1a5 1322 return 1;
97222cc8 1323 }
0105d1a5
GN
1324
1325 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
4088bb3c
GN
1326 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1327 offset);
0105d1a5
GN
1328 return 1;
1329 }
1330
97222cc8
ED
1331 result = __apic_read(apic, offset & ~0xf);
1332
229456fc
MT
1333 trace_kvm_apic_read(offset, result);
1334
97222cc8
ED
1335 switch (len) {
1336 case 1:
1337 case 2:
1338 case 4:
1339 memcpy(data, (char *)&result + alignment, len);
1340 break;
1341 default:
1342 printk(KERN_ERR "Local APIC read with len = %x, "
1343 "should be 1,2, or 4 instead\n", len);
1344 break;
1345 }
bda9020e 1346 return 0;
97222cc8 1347}
1e6e2755 1348EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
97222cc8 1349
0105d1a5
GN
1350static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1351{
d1766202
VK
1352 return addr >= apic->base_address &&
1353 addr < apic->base_address + LAPIC_MMIO_LENGTH;
0105d1a5
GN
1354}
1355
e32edf4f 1356static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
0105d1a5
GN
1357 gpa_t address, int len, void *data)
1358{
1359 struct kvm_lapic *apic = to_lapic(this);
1360 u32 offset = address - apic->base_address;
1361
1362 if (!apic_mmio_in_range(apic, address))
1363 return -EOPNOTSUPP;
1364
d1766202
VK
1365 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1366 if (!kvm_check_has_quirk(vcpu->kvm,
1367 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1368 return -EOPNOTSUPP;
1369
1370 memset(data, 0xff, len);
1371 return 0;
1372 }
1373
1e6e2755 1374 kvm_lapic_reg_read(apic, offset, len, data);
0105d1a5
GN
1375
1376 return 0;
1377}
1378
97222cc8
ED
1379static void update_divide_count(struct kvm_lapic *apic)
1380{
1381 u32 tmp1, tmp2, tdcr;
1382
dfb95954 1383 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
97222cc8
ED
1384 tmp1 = tdcr & 0xf;
1385 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
d3c7b77d 1386 apic->divide_count = 0x1 << (tmp2 & 0x7);
97222cc8
ED
1387
1388 apic_debug("timer divide count is 0x%x\n",
9b5843dd 1389 apic->divide_count);
97222cc8
ED
1390}
1391
ccbfa1d3
WL
1392static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1393{
1394 /*
1395 * Do not allow the guest to program periodic timers with small
1396 * interval, since the hrtimers are not throttled by the host
1397 * scheduler.
1398 */
dedf9c5e 1399 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
ccbfa1d3
WL
1400 s64 min_period = min_timer_period_us * 1000LL;
1401
1402 if (apic->lapic_timer.period < min_period) {
1403 pr_info_ratelimited(
1404 "kvm: vcpu %i: requested %lld ns "
1405 "lapic timer period limited to %lld ns\n",
1406 apic->vcpu->vcpu_id,
1407 apic->lapic_timer.period, min_period);
1408 apic->lapic_timer.period = min_period;
1409 }
1410 }
1411}
1412
b6ac0695
RK
1413static void apic_update_lvtt(struct kvm_lapic *apic)
1414{
dfb95954 1415 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
b6ac0695
RK
1416 apic->lapic_timer.timer_mode_mask;
1417
1418 if (apic->lapic_timer.timer_mode != timer_mode) {
c69518c8 1419 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
dedf9c5e 1420 APIC_LVT_TIMER_TSCDEADLINE)) {
dedf9c5e 1421 hrtimer_cancel(&apic->lapic_timer.timer);
44275932
RK
1422 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1423 apic->lapic_timer.period = 0;
1424 apic->lapic_timer.tscdeadline = 0;
dedf9c5e 1425 }
b6ac0695 1426 apic->lapic_timer.timer_mode = timer_mode;
dedf9c5e 1427 limit_periodic_timer_frequency(apic);
b6ac0695
RK
1428 }
1429}
1430
5d87db71
RK
1431static void apic_timer_expired(struct kvm_lapic *apic)
1432{
1433 struct kvm_vcpu *vcpu = apic->vcpu;
8577370f 1434 struct swait_queue_head *q = &vcpu->wq;
d0659d94 1435 struct kvm_timer *ktimer = &apic->lapic_timer;
5d87db71 1436
5d87db71
RK
1437 if (atomic_read(&apic->lapic_timer.pending))
1438 return;
1439
1440 atomic_inc(&apic->lapic_timer.pending);
bab5bb39 1441 kvm_set_pending_timer(vcpu);
5d87db71 1442
cc1b4680
DB
1443 /*
1444 * For x86, the atomic_inc() is serialized, thus
1445 * using swait_active() is safe.
1446 */
8577370f 1447 if (swait_active(q))
b3dae109 1448 swake_up_one(q);
d0659d94
MT
1449
1450 if (apic_lvtt_tscdeadline(apic))
1451 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1452}
1453
1454/*
1455 * On APICv, this test will cause a busy wait
1456 * during a higher-priority task.
1457 */
1458
1459static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1460{
1461 struct kvm_lapic *apic = vcpu->arch.apic;
dfb95954 1462 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
d0659d94
MT
1463
1464 if (kvm_apic_hw_enabled(apic)) {
1465 int vec = reg & APIC_VECTOR_MASK;
f9339860 1466 void *bitmap = apic->regs + APIC_ISR;
d0659d94 1467
d62caabb 1468 if (vcpu->arch.apicv_active)
f9339860
MT
1469 bitmap = apic->regs + APIC_IRR;
1470
1471 if (apic_test_vector(vec, bitmap))
1472 return true;
d0659d94
MT
1473 }
1474 return false;
1475}
1476
1477void wait_lapic_expire(struct kvm_vcpu *vcpu)
1478{
1479 struct kvm_lapic *apic = vcpu->arch.apic;
3b8a5df6 1480 u64 guest_tsc, tsc_deadline, ns;
d0659d94 1481
bce87cce 1482 if (!lapic_in_kernel(vcpu))
d0659d94
MT
1483 return;
1484
1485 if (apic->lapic_timer.expired_tscdeadline == 0)
1486 return;
1487
1488 if (!lapic_timer_int_injected(vcpu))
1489 return;
1490
1491 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1492 apic->lapic_timer.expired_tscdeadline = 0;
4ba76538 1493 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
6c19b753 1494 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
d0659d94
MT
1495
1496 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1497 if (guest_tsc < tsc_deadline)
b606f189
MT
1498 __delay(min(tsc_deadline - guest_tsc,
1499 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
3b8a5df6
WL
1500
1501 if (!lapic_timer_advance_adjust_done) {
1502 /* too early */
1503 if (guest_tsc < tsc_deadline) {
1504 ns = (tsc_deadline - guest_tsc) * 1000000ULL;
1505 do_div(ns, vcpu->arch.virtual_tsc_khz);
1506 lapic_timer_advance_ns -= min((unsigned int)ns,
1507 lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1508 } else {
1509 /* too late */
1510 ns = (guest_tsc - tsc_deadline) * 1000000ULL;
1511 do_div(ns, vcpu->arch.virtual_tsc_khz);
1512 lapic_timer_advance_ns += min((unsigned int)ns,
1513 lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1514 }
1515 if (abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
1516 lapic_timer_advance_adjust_done = true;
1517 }
5d87db71
RK
1518}
1519
53f9eedf
YJ
1520static void start_sw_tscdeadline(struct kvm_lapic *apic)
1521{
1522 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1523 u64 ns = 0;
1524 ktime_t expire;
1525 struct kvm_vcpu *vcpu = apic->vcpu;
1526 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1527 unsigned long flags;
1528 ktime_t now;
1529
1530 if (unlikely(!tscdeadline || !this_tsc_khz))
1531 return;
1532
1533 local_irq_save(flags);
1534
5587859f 1535 now = ktime_get();
53f9eedf
YJ
1536 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1537 if (likely(tscdeadline > guest_tsc)) {
1538 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1539 do_div(ns, this_tsc_khz);
1540 expire = ktime_add_ns(now, ns);
1541 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1542 hrtimer_start(&apic->lapic_timer.timer,
1543 expire, HRTIMER_MODE_ABS_PINNED);
1544 } else
1545 apic_timer_expired(apic);
1546
1547 local_irq_restore(flags);
1548}
1549
c301b909
WL
1550static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1551{
1552 ktime_t now, remaining;
1553 u64 ns_remaining_old, ns_remaining_new;
1554
1555 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1556 * APIC_BUS_CYCLE_NS * apic->divide_count;
1557 limit_periodic_timer_frequency(apic);
1558
1559 now = ktime_get();
1560 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1561 if (ktime_to_ns(remaining) < 0)
1562 remaining = 0;
1563
1564 ns_remaining_old = ktime_to_ns(remaining);
1565 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1566 apic->divide_count, old_divisor);
1567
1568 apic->lapic_timer.tscdeadline +=
1569 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1570 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1571 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1572}
1573
8003c9ae 1574static bool set_target_expiration(struct kvm_lapic *apic)
7d7f7da2
WL
1575{
1576 ktime_t now;
8003c9ae 1577 u64 tscl = rdtsc();
7d7f7da2 1578
5587859f 1579 now = ktime_get();
7d7f7da2 1580 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
8003c9ae 1581 * APIC_BUS_CYCLE_NS * apic->divide_count;
7d7f7da2 1582
5d74a699
RK
1583 if (!apic->lapic_timer.period) {
1584 apic->lapic_timer.tscdeadline = 0;
8003c9ae 1585 return false;
7d7f7da2
WL
1586 }
1587
ccbfa1d3 1588 limit_periodic_timer_frequency(apic);
7d7f7da2 1589
7d7f7da2
WL
1590 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1591 PRIx64 ", "
1592 "timer initial count 0x%x, period %lldns, "
1593 "expire @ 0x%016" PRIx64 ".\n", __func__,
1594 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1595 kvm_lapic_get_reg(apic, APIC_TMICT),
1596 apic->lapic_timer.period,
1597 ktime_to_ns(ktime_add_ns(now,
1598 apic->lapic_timer.period)));
8003c9ae
WL
1599
1600 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1601 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1602 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1603
1604 return true;
1605}
1606
1607static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1608{
d8f2f498
DV
1609 ktime_t now = ktime_get();
1610 u64 tscl = rdtsc();
1611 ktime_t delta;
1612
1613 /*
1614 * Synchronize both deadlines to the same time source or
1615 * differences in the periods (caused by differences in the
1616 * underlying clocks or numerical approximation errors) will
1617 * cause the two to drift apart over time as the errors
1618 * accumulate.
1619 */
8003c9ae
WL
1620 apic->lapic_timer.target_expiration =
1621 ktime_add_ns(apic->lapic_timer.target_expiration,
1622 apic->lapic_timer.period);
d8f2f498
DV
1623 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1624 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1625 nsec_to_cycles(apic->vcpu, delta);
7d7f7da2
WL
1626}
1627
ecf08dad
AB
1628static void start_sw_period(struct kvm_lapic *apic)
1629{
1630 if (!apic->lapic_timer.period)
1631 return;
1632
1633 if (ktime_after(ktime_get(),
1634 apic->lapic_timer.target_expiration)) {
1635 apic_timer_expired(apic);
1636
1637 if (apic_lvtt_oneshot(apic))
1638 return;
1639
1640 advance_periodic_target_expiration(apic);
1641 }
1642
1643 hrtimer_start(&apic->lapic_timer.timer,
1644 apic->lapic_timer.target_expiration,
1645 HRTIMER_MODE_ABS_PINNED);
1646}
1647
ce7a058a
YJ
1648bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1649{
91005300
WL
1650 if (!lapic_in_kernel(vcpu))
1651 return false;
1652
ce7a058a
YJ
1653 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1654}
1655EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1656
7e810a38 1657static void cancel_hv_timer(struct kvm_lapic *apic)
bd97ad0e 1658{
1d518c68 1659 WARN_ON(preemptible());
a749e247 1660 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
bd97ad0e
WL
1661 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1662 apic->lapic_timer.hv_timer_in_use = false;
1663}
1664
a749e247 1665static bool start_hv_timer(struct kvm_lapic *apic)
196f20ca 1666{
35ee9e48
PB
1667 struct kvm_timer *ktimer = &apic->lapic_timer;
1668 int r;
196f20ca 1669
1d518c68 1670 WARN_ON(preemptible());
a749e247
PB
1671 if (!kvm_x86_ops->set_hv_timer)
1672 return false;
1673
35ee9e48
PB
1674 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1675 return false;
1676
86bbc1e6
RK
1677 if (!ktimer->tscdeadline)
1678 return false;
1679
35ee9e48
PB
1680 r = kvm_x86_ops->set_hv_timer(apic->vcpu, ktimer->tscdeadline);
1681 if (r < 0)
1682 return false;
1683
1684 ktimer->hv_timer_in_use = true;
1685 hrtimer_cancel(&ktimer->timer);
196f20ca 1686
35ee9e48
PB
1687 /*
1688 * Also recheck ktimer->pending, in case the sw timer triggered in
1689 * the window. For periodic timer, leave the hv timer running for
1690 * simplicity, and the deadline will be recomputed on the next vmexit.
1691 */
c8533544
WL
1692 if (!apic_lvtt_period(apic) && (r || atomic_read(&ktimer->pending))) {
1693 if (r)
1694 apic_timer_expired(apic);
35ee9e48 1695 return false;
c8533544 1696 }
a749e247
PB
1697
1698 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true);
35ee9e48
PB
1699 return true;
1700}
1701
a749e247 1702static void start_sw_timer(struct kvm_lapic *apic)
35ee9e48 1703{
a749e247 1704 struct kvm_timer *ktimer = &apic->lapic_timer;
1d518c68
WL
1705
1706 WARN_ON(preemptible());
a749e247
PB
1707 if (apic->lapic_timer.hv_timer_in_use)
1708 cancel_hv_timer(apic);
1709 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1710 return;
1711
1712 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1713 start_sw_period(apic);
1714 else if (apic_lvtt_tscdeadline(apic))
1715 start_sw_tscdeadline(apic);
1716 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1717}
35ee9e48 1718
a749e247
PB
1719static void restart_apic_timer(struct kvm_lapic *apic)
1720{
1d518c68 1721 preempt_disable();
a749e247
PB
1722 if (!start_hv_timer(apic))
1723 start_sw_timer(apic);
1d518c68 1724 preempt_enable();
196f20ca
WL
1725}
1726
8003c9ae
WL
1727void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1728{
1729 struct kvm_lapic *apic = vcpu->arch.apic;
1730
1d518c68
WL
1731 preempt_disable();
1732 /* If the preempt notifier has already run, it also called apic_timer_expired */
1733 if (!apic->lapic_timer.hv_timer_in_use)
1734 goto out;
8003c9ae
WL
1735 WARN_ON(swait_active(&vcpu->wq));
1736 cancel_hv_timer(apic);
1737 apic_timer_expired(apic);
1738
1739 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1740 advance_periodic_target_expiration(apic);
a749e247 1741 restart_apic_timer(apic);
8003c9ae 1742 }
1d518c68
WL
1743out:
1744 preempt_enable();
8003c9ae
WL
1745}
1746EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1747
ce7a058a
YJ
1748void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1749{
a749e247 1750 restart_apic_timer(vcpu->arch.apic);
ce7a058a
YJ
1751}
1752EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1753
1754void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1755{
1756 struct kvm_lapic *apic = vcpu->arch.apic;
1757
1d518c68 1758 preempt_disable();
ce7a058a 1759 /* Possibly the TSC deadline timer is not enabled yet */
a749e247
PB
1760 if (apic->lapic_timer.hv_timer_in_use)
1761 start_sw_timer(apic);
1d518c68 1762 preempt_enable();
a749e247
PB
1763}
1764EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
ce7a058a 1765
a749e247
PB
1766void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1767{
1768 struct kvm_lapic *apic = vcpu->arch.apic;
ce7a058a 1769
a749e247
PB
1770 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1771 restart_apic_timer(apic);
ce7a058a 1772}
ce7a058a 1773
97222cc8
ED
1774static void start_apic_timer(struct kvm_lapic *apic)
1775{
d3c7b77d 1776 atomic_set(&apic->lapic_timer.pending, 0);
0b975a3c 1777
a749e247
PB
1778 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1779 && !set_target_expiration(apic))
1780 return;
1781
1782 restart_apic_timer(apic);
97222cc8
ED
1783}
1784
cc6e462c
JK
1785static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1786{
59fd1323 1787 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
cc6e462c 1788
59fd1323
RK
1789 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1790 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1791 if (lvt0_in_nmi_mode) {
cc6e462c
JK
1792 apic_debug("Receive NMI setting on APIC_LVT0 "
1793 "for cpu %d\n", apic->vcpu->vcpu_id);
42720138 1794 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
59fd1323
RK
1795 } else
1796 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1797 }
cc6e462c
JK
1798}
1799
1e6e2755 1800int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
97222cc8 1801{
0105d1a5 1802 int ret = 0;
97222cc8 1803
0105d1a5 1804 trace_kvm_apic_write(reg, val);
97222cc8 1805
0105d1a5 1806 switch (reg) {
97222cc8 1807 case APIC_ID: /* Local APIC ID */
0105d1a5 1808 if (!apic_x2apic_mode(apic))
a92e2543 1809 kvm_apic_set_xapic_id(apic, val >> 24);
0105d1a5
GN
1810 else
1811 ret = 1;
97222cc8
ED
1812 break;
1813
1814 case APIC_TASKPRI:
b209749f 1815 report_tpr_access(apic, true);
97222cc8
ED
1816 apic_set_tpr(apic, val & 0xff);
1817 break;
1818
1819 case APIC_EOI:
1820 apic_set_eoi(apic);
1821 break;
1822
1823 case APIC_LDR:
0105d1a5 1824 if (!apic_x2apic_mode(apic))
1e08ec4a 1825 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
0105d1a5
GN
1826 else
1827 ret = 1;
97222cc8
ED
1828 break;
1829
1830 case APIC_DFR:
1e08ec4a 1831 if (!apic_x2apic_mode(apic)) {
1e6e2755 1832 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
1e08ec4a
GN
1833 recalculate_apic_map(apic->vcpu->kvm);
1834 } else
0105d1a5 1835 ret = 1;
97222cc8
ED
1836 break;
1837
fc61b800
GN
1838 case APIC_SPIV: {
1839 u32 mask = 0x3ff;
dfb95954 1840 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
fc61b800 1841 mask |= APIC_SPIV_DIRECTED_EOI;
f8c1ea10 1842 apic_set_spiv(apic, val & mask);
97222cc8
ED
1843 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1844 int i;
1845 u32 lvt_val;
1846
1e6e2755 1847 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
dfb95954 1848 lvt_val = kvm_lapic_get_reg(apic,
97222cc8 1849 APIC_LVTT + 0x10 * i);
1e6e2755 1850 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
97222cc8
ED
1851 lvt_val | APIC_LVT_MASKED);
1852 }
b6ac0695 1853 apic_update_lvtt(apic);
d3c7b77d 1854 atomic_set(&apic->lapic_timer.pending, 0);
97222cc8
ED
1855
1856 }
1857 break;
fc61b800 1858 }
97222cc8
ED
1859 case APIC_ICR:
1860 /* No delay here, so we always clear the pending bit */
1e6e2755 1861 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
97222cc8
ED
1862 apic_send_ipi(apic);
1863 break;
1864
1865 case APIC_ICR2:
0105d1a5
GN
1866 if (!apic_x2apic_mode(apic))
1867 val &= 0xff000000;
1e6e2755 1868 kvm_lapic_set_reg(apic, APIC_ICR2, val);
97222cc8
ED
1869 break;
1870
23930f95 1871 case APIC_LVT0:
cc6e462c 1872 apic_manage_nmi_watchdog(apic, val);
97222cc8
ED
1873 case APIC_LVTTHMR:
1874 case APIC_LVTPC:
97222cc8
ED
1875 case APIC_LVT1:
1876 case APIC_LVTERR:
1877 /* TODO: Check vector */
c48f1496 1878 if (!kvm_apic_sw_enabled(apic))
97222cc8
ED
1879 val |= APIC_LVT_MASKED;
1880
0105d1a5 1881 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
1e6e2755 1882 kvm_lapic_set_reg(apic, reg, val);
97222cc8
ED
1883
1884 break;
1885
b6ac0695 1886 case APIC_LVTT:
c48f1496 1887 if (!kvm_apic_sw_enabled(apic))
a3e06bbe
LJ
1888 val |= APIC_LVT_MASKED;
1889 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
1e6e2755 1890 kvm_lapic_set_reg(apic, APIC_LVTT, val);
b6ac0695 1891 apic_update_lvtt(apic);
a3e06bbe
LJ
1892 break;
1893
97222cc8 1894 case APIC_TMICT:
a3e06bbe
LJ
1895 if (apic_lvtt_tscdeadline(apic))
1896 break;
1897
d3c7b77d 1898 hrtimer_cancel(&apic->lapic_timer.timer);
1e6e2755 1899 kvm_lapic_set_reg(apic, APIC_TMICT, val);
97222cc8 1900 start_apic_timer(apic);
0105d1a5 1901 break;
97222cc8 1902
c301b909
WL
1903 case APIC_TDCR: {
1904 uint32_t old_divisor = apic->divide_count;
1905
97222cc8 1906 if (val & 4)
7712de87 1907 apic_debug("KVM_WRITE:TDCR %x\n", val);
1e6e2755 1908 kvm_lapic_set_reg(apic, APIC_TDCR, val);
97222cc8 1909 update_divide_count(apic);
c301b909
WL
1910 if (apic->divide_count != old_divisor &&
1911 apic->lapic_timer.period) {
1912 hrtimer_cancel(&apic->lapic_timer.timer);
1913 update_target_expiration(apic, old_divisor);
1914 restart_apic_timer(apic);
1915 }
97222cc8 1916 break;
c301b909 1917 }
0105d1a5
GN
1918 case APIC_ESR:
1919 if (apic_x2apic_mode(apic) && val != 0) {
7712de87 1920 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
0105d1a5
GN
1921 ret = 1;
1922 }
1923 break;
1924
1925 case APIC_SELF_IPI:
1926 if (apic_x2apic_mode(apic)) {
1e6e2755 1927 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
0105d1a5
GN
1928 } else
1929 ret = 1;
1930 break;
97222cc8 1931 default:
0105d1a5 1932 ret = 1;
97222cc8
ED
1933 break;
1934 }
0105d1a5
GN
1935 if (ret)
1936 apic_debug("Local APIC Write to read-only register %x\n", reg);
1937 return ret;
1938}
1e6e2755 1939EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
0105d1a5 1940
e32edf4f 1941static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
0105d1a5
GN
1942 gpa_t address, int len, const void *data)
1943{
1944 struct kvm_lapic *apic = to_lapic(this);
1945 unsigned int offset = address - apic->base_address;
1946 u32 val;
1947
1948 if (!apic_mmio_in_range(apic, address))
1949 return -EOPNOTSUPP;
1950
d1766202
VK
1951 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1952 if (!kvm_check_has_quirk(vcpu->kvm,
1953 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1954 return -EOPNOTSUPP;
1955
1956 return 0;
1957 }
1958
0105d1a5
GN
1959 /*
1960 * APIC register must be aligned on 128-bits boundary.
1961 * 32/64/128 bits registers must be accessed thru 32 bits.
1962 * Refer SDM 8.4.1
1963 */
1964 if (len != 4 || (offset & 0xf)) {
1965 /* Don't shout loud, $infamous_os would cause only noise. */
1966 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
756975bb 1967 return 0;
0105d1a5
GN
1968 }
1969
1970 val = *(u32*)data;
1971
1972 /* too common printing */
1973 if (offset != APIC_EOI)
1974 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1975 "0x%x\n", __func__, offset, len, val);
1976
1e6e2755 1977 kvm_lapic_reg_write(apic, offset & 0xff0, val);
0105d1a5 1978
bda9020e 1979 return 0;
97222cc8
ED
1980}
1981
58fbbf26
KT
1982void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1983{
1e6e2755 1984 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
58fbbf26
KT
1985}
1986EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1987
83d4c286
YZ
1988/* emulate APIC access in a trap manner */
1989void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1990{
1991 u32 val = 0;
1992
1993 /* hw has done the conditional check and inst decode */
1994 offset &= 0xff0;
1995
1e6e2755 1996 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
83d4c286
YZ
1997
1998 /* TODO: optimize to just emulate side effect w/o one more write */
1e6e2755 1999 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
83d4c286
YZ
2000}
2001EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2002
d589444e 2003void kvm_free_lapic(struct kvm_vcpu *vcpu)
97222cc8 2004{
f8c1ea10
GN
2005 struct kvm_lapic *apic = vcpu->arch.apic;
2006
ad312c7c 2007 if (!vcpu->arch.apic)
97222cc8
ED
2008 return;
2009
f8c1ea10 2010 hrtimer_cancel(&apic->lapic_timer.timer);
97222cc8 2011
c5cc421b
GN
2012 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2013 static_key_slow_dec_deferred(&apic_hw_disabled);
2014
e462755c 2015 if (!apic->sw_enabled)
f8c1ea10 2016 static_key_slow_dec_deferred(&apic_sw_disabled);
97222cc8 2017
f8c1ea10
GN
2018 if (apic->regs)
2019 free_page((unsigned long)apic->regs);
2020
2021 kfree(apic);
97222cc8
ED
2022}
2023
2024/*
2025 *----------------------------------------------------------------------
2026 * LAPIC interface
2027 *----------------------------------------------------------------------
2028 */
a3e06bbe
LJ
2029u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2030{
2031 struct kvm_lapic *apic = vcpu->arch.apic;
a3e06bbe 2032
a10388e1
WL
2033 if (!lapic_in_kernel(vcpu) ||
2034 !apic_lvtt_tscdeadline(apic))
a3e06bbe
LJ
2035 return 0;
2036
2037 return apic->lapic_timer.tscdeadline;
2038}
2039
2040void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2041{
2042 struct kvm_lapic *apic = vcpu->arch.apic;
a3e06bbe 2043
bce87cce 2044 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
54e9818f 2045 apic_lvtt_period(apic))
a3e06bbe
LJ
2046 return;
2047
2048 hrtimer_cancel(&apic->lapic_timer.timer);
2049 apic->lapic_timer.tscdeadline = data;
2050 start_apic_timer(apic);
2051}
2052
97222cc8
ED
2053void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2054{
ad312c7c 2055 struct kvm_lapic *apic = vcpu->arch.apic;
97222cc8 2056
b93463aa 2057 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
dfb95954 2058 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
97222cc8
ED
2059}
2060
2061u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2062{
97222cc8
ED
2063 u64 tpr;
2064
dfb95954 2065 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
97222cc8
ED
2066
2067 return (tpr & 0xf0) >> 4;
2068}
2069
2070void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2071{
8d14695f 2072 u64 old_value = vcpu->arch.apic_base;
ad312c7c 2073 struct kvm_lapic *apic = vcpu->arch.apic;
97222cc8 2074
c7dd15b3 2075 if (!apic)
97222cc8 2076 value |= MSR_IA32_APICBASE_BSP;
c5af89b6 2077
e66d2ae7
JK
2078 vcpu->arch.apic_base = value;
2079
c7dd15b3
JM
2080 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2081 kvm_update_cpuid(vcpu);
2082
2083 if (!apic)
2084 return;
2085
c5cc421b 2086 /* update jump label if enable bit changes */
0dce7cd6 2087 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
49bd29ba
RK
2088 if (value & MSR_IA32_APICBASE_ENABLE) {
2089 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
c5cc421b 2090 static_key_slow_dec_deferred(&apic_hw_disabled);
187ca84b 2091 } else {
c5cc421b 2092 static_key_slow_inc(&apic_hw_disabled.key);
187ca84b
WL
2093 recalculate_apic_map(vcpu->kvm);
2094 }
c5cc421b
GN
2095 }
2096
8d860bbe
JM
2097 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2098 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2099
2100 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2101 kvm_x86_ops->set_virtual_apic_mode(vcpu);
8d14695f 2102
ad312c7c 2103 apic->base_address = apic->vcpu->arch.apic_base &
97222cc8
ED
2104 MSR_IA32_APICBASE_BASE;
2105
db324fe6
NA
2106 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2107 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2108 pr_warn_once("APIC base relocation is unsupported by KVM");
2109
97222cc8
ED
2110 /* with FSB delivery interrupt, we can restart APIC functionality */
2111 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
ad312c7c 2112 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
97222cc8
ED
2113
2114}
2115
d28bc9dd 2116void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
97222cc8 2117{
b7e31be3 2118 struct kvm_lapic *apic = vcpu->arch.apic;
97222cc8
ED
2119 int i;
2120
b7e31be3
RK
2121 if (!apic)
2122 return;
97222cc8 2123
b7e31be3 2124 apic_debug("%s\n", __func__);
97222cc8
ED
2125
2126 /* Stop the timer in case it's a reset to an active apic */
d3c7b77d 2127 hrtimer_cancel(&apic->lapic_timer.timer);
97222cc8 2128
4d8e772b
RK
2129 if (!init_event) {
2130 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2131 MSR_IA32_APICBASE_ENABLE);
a92e2543 2132 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
4d8e772b 2133 }
fc61b800 2134 kvm_apic_set_version(apic->vcpu);
97222cc8 2135
1e6e2755
SS
2136 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2137 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
b6ac0695 2138 apic_update_lvtt(apic);
52b54190
JS
2139 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2140 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
1e6e2755 2141 kvm_lapic_set_reg(apic, APIC_LVT0,
90de4a18 2142 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
dfb95954 2143 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
97222cc8 2144
1e6e2755 2145 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
f8c1ea10 2146 apic_set_spiv(apic, 0xff);
1e6e2755 2147 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
c028dd6b
RK
2148 if (!apic_x2apic_mode(apic))
2149 kvm_apic_set_ldr(apic, 0);
1e6e2755
SS
2150 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2151 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2152 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2153 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2154 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
97222cc8 2155 for (i = 0; i < 8; i++) {
1e6e2755
SS
2156 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2157 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2158 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
97222cc8 2159 }
d62caabb
AS
2160 apic->irr_pending = vcpu->arch.apicv_active;
2161 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
8680b94b 2162 apic->highest_isr_cache = -1;
b33ac88b 2163 update_divide_count(apic);
d3c7b77d 2164 atomic_set(&apic->lapic_timer.pending, 0);
c5af89b6 2165 if (kvm_vcpu_is_bsp(vcpu))
5dbc8f3f
GN
2166 kvm_lapic_set_base(vcpu,
2167 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
ae7a2a3f 2168 vcpu->arch.pv_eoi.msr_val = 0;
97222cc8 2169 apic_update_ppr(apic);
4191db26
JS
2170 if (vcpu->arch.apicv_active) {
2171 kvm_x86_ops->apicv_post_state_restore(vcpu);
2172 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2173 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2174 }
97222cc8 2175
e1035715 2176 vcpu->arch.apic_arb_prio = 0;
41383771 2177 vcpu->arch.apic_attention = 0;
e1035715 2178
6e500439 2179 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
b8688d51 2180 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
6e500439 2181 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
ad312c7c 2182 vcpu->arch.apic_base, apic->base_address);
97222cc8
ED
2183}
2184
97222cc8
ED
2185/*
2186 *----------------------------------------------------------------------
2187 * timer interface
2188 *----------------------------------------------------------------------
2189 */
1b9778da 2190
2a6eac96 2191static bool lapic_is_periodic(struct kvm_lapic *apic)
97222cc8 2192{
d3c7b77d 2193 return apic_lvtt_period(apic);
97222cc8
ED
2194}
2195
3d80840d
MT
2196int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2197{
54e9818f 2198 struct kvm_lapic *apic = vcpu->arch.apic;
3d80840d 2199
1e3161b4 2200 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
54e9818f 2201 return atomic_read(&apic->lapic_timer.pending);
3d80840d
MT
2202
2203 return 0;
2204}
2205
89342082 2206int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
1b9778da 2207{
dfb95954 2208 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
23930f95 2209 int vector, mode, trig_mode;
23930f95 2210
c48f1496 2211 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
23930f95
JK
2212 vector = reg & APIC_VECTOR_MASK;
2213 mode = reg & APIC_MODE_MASK;
2214 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
b4f2225c
YZ
2215 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2216 NULL);
23930f95
JK
2217 }
2218 return 0;
2219}
1b9778da 2220
8fdb2351 2221void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
23930f95 2222{
8fdb2351
JK
2223 struct kvm_lapic *apic = vcpu->arch.apic;
2224
2225 if (apic)
2226 kvm_apic_local_deliver(apic, APIC_LVT0);
1b9778da
ED
2227}
2228
d76685c4
GH
2229static const struct kvm_io_device_ops apic_mmio_ops = {
2230 .read = apic_mmio_read,
2231 .write = apic_mmio_write,
d76685c4
GH
2232};
2233
e9d90d47
AK
2234static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2235{
2236 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
2a6eac96 2237 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
e9d90d47 2238
5d87db71 2239 apic_timer_expired(apic);
e9d90d47 2240
2a6eac96 2241 if (lapic_is_periodic(apic)) {
8003c9ae 2242 advance_periodic_target_expiration(apic);
e9d90d47
AK
2243 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2244 return HRTIMER_RESTART;
2245 } else
2246 return HRTIMER_NORESTART;
2247}
2248
97222cc8
ED
2249int kvm_create_lapic(struct kvm_vcpu *vcpu)
2250{
2251 struct kvm_lapic *apic;
2252
2253 ASSERT(vcpu != NULL);
2254 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2255
2256 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
2257 if (!apic)
2258 goto nomem;
2259
ad312c7c 2260 vcpu->arch.apic = apic;
97222cc8 2261
afc20184
TY
2262 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
2263 if (!apic->regs) {
97222cc8
ED
2264 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2265 vcpu->vcpu_id);
d589444e 2266 goto nomem_free_apic;
97222cc8 2267 }
97222cc8
ED
2268 apic->vcpu = vcpu;
2269
d3c7b77d 2270 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
61abdbe0 2271 HRTIMER_MODE_ABS_PINNED);
e9d90d47 2272 apic->lapic_timer.timer.function = apic_timer_fn;
d3c7b77d 2273
c5cc421b
GN
2274 /*
2275 * APIC is created enabled. This will prevent kvm_lapic_set_base from
2276 * thinking that APIC satet has changed.
2277 */
2278 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
f8c1ea10 2279 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
d76685c4 2280 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
97222cc8
ED
2281
2282 return 0;
d589444e
RR
2283nomem_free_apic:
2284 kfree(apic);
97222cc8 2285nomem:
97222cc8
ED
2286 return -ENOMEM;
2287}
97222cc8
ED
2288
2289int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2290{
ad312c7c 2291 struct kvm_lapic *apic = vcpu->arch.apic;
b3c045d3 2292 u32 ppr;
97222cc8 2293
f8543d6a 2294 if (!apic_enabled(apic))
97222cc8
ED
2295 return -1;
2296
b3c045d3
PB
2297 __apic_update_ppr(apic, &ppr);
2298 return apic_has_interrupt_for_ppr(apic, ppr);
97222cc8
ED
2299}
2300
40487c68
QH
2301int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2302{
dfb95954 2303 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
40487c68
QH
2304 int r = 0;
2305
c48f1496 2306 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
e7dca5c0
CL
2307 r = 1;
2308 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2309 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2310 r = 1;
40487c68
QH
2311 return r;
2312}
2313
1b9778da
ED
2314void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2315{
ad312c7c 2316 struct kvm_lapic *apic = vcpu->arch.apic;
1b9778da 2317
54e9818f 2318 if (atomic_read(&apic->lapic_timer.pending) > 0) {
f1ed0450 2319 kvm_apic_local_deliver(apic, APIC_LVTT);
fae0ba21
NA
2320 if (apic_lvtt_tscdeadline(apic))
2321 apic->lapic_timer.tscdeadline = 0;
8003c9ae
WL
2322 if (apic_lvtt_oneshot(apic)) {
2323 apic->lapic_timer.tscdeadline = 0;
8b0e1953 2324 apic->lapic_timer.target_expiration = 0;
8003c9ae 2325 }
f1ed0450 2326 atomic_set(&apic->lapic_timer.pending, 0);
1b9778da
ED
2327 }
2328}
2329
97222cc8
ED
2330int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2331{
2332 int vector = kvm_apic_has_interrupt(vcpu);
ad312c7c 2333 struct kvm_lapic *apic = vcpu->arch.apic;
4d82d12b 2334 u32 ppr;
97222cc8
ED
2335
2336 if (vector == -1)
2337 return -1;
2338
56cc2406
WL
2339 /*
2340 * We get here even with APIC virtualization enabled, if doing
2341 * nested virtualization and L1 runs with the "acknowledge interrupt
2342 * on exit" mode. Then we cannot inject the interrupt via RVI,
2343 * because the process would deliver it through the IDT.
2344 */
2345
97222cc8 2346 apic_clear_irr(vector, apic);
5c919412 2347 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
4d82d12b
PB
2348 /*
2349 * For auto-EOI interrupts, there might be another pending
2350 * interrupt above PPR, so check whether to raise another
2351 * KVM_REQ_EVENT.
2352 */
5c919412 2353 apic_update_ppr(apic);
4d82d12b
PB
2354 } else {
2355 /*
2356 * For normal interrupts, PPR has been raised and there cannot
2357 * be a higher-priority pending interrupt---except if there was
2358 * a concurrent interrupt injection, but that would have
2359 * triggered KVM_REQ_EVENT already.
2360 */
2361 apic_set_isr(vector, apic);
2362 __apic_update_ppr(apic, &ppr);
5c919412
AS
2363 }
2364
97222cc8
ED
2365 return vector;
2366}
96ad2cc6 2367
a92e2543
RK
2368static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2369 struct kvm_lapic_state *s, bool set)
2370{
2371 if (apic_x2apic_mode(vcpu->arch.apic)) {
2372 u32 *id = (u32 *)(s->regs + APIC_ID);
12806ba9 2373 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
a92e2543 2374
37131313
RK
2375 if (vcpu->kvm->arch.x2apic_format) {
2376 if (*id != vcpu->vcpu_id)
2377 return -EINVAL;
2378 } else {
2379 if (set)
2380 *id >>= 24;
2381 else
2382 *id <<= 24;
2383 }
12806ba9
DDAG
2384
2385 /* In x2APIC mode, the LDR is fixed and based on the id */
2386 if (set)
2387 *ldr = kvm_apic_calc_x2apic_ldr(*id);
a92e2543
RK
2388 }
2389
2390 return 0;
2391}
2392
2393int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2394{
2395 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2396 return kvm_apic_state_fixup(vcpu, s, false);
2397}
2398
2399int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
96ad2cc6 2400{
ad312c7c 2401 struct kvm_lapic *apic = vcpu->arch.apic;
a92e2543
RK
2402 int r;
2403
96ad2cc6 2404
5dbc8f3f 2405 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
64eb0620
GN
2406 /* set SPIV separately to get count of SW disabled APICs right */
2407 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
a92e2543
RK
2408
2409 r = kvm_apic_state_fixup(vcpu, s, true);
2410 if (r)
2411 return r;
64eb0620 2412 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
a92e2543
RK
2413
2414 recalculate_apic_map(vcpu->kvm);
fc61b800
GN
2415 kvm_apic_set_version(vcpu);
2416
96ad2cc6 2417 apic_update_ppr(apic);
d3c7b77d 2418 hrtimer_cancel(&apic->lapic_timer.timer);
b6ac0695 2419 apic_update_lvtt(apic);
dfb95954 2420 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
96ad2cc6
ED
2421 update_divide_count(apic);
2422 start_apic_timer(apic);
6e24a6ef 2423 apic->irr_pending = true;
d62caabb 2424 apic->isr_count = vcpu->arch.apicv_active ?
c7c9c56c 2425 1 : count_vectors(apic->regs + APIC_ISR);
8680b94b 2426 apic->highest_isr_cache = -1;
d62caabb 2427 if (vcpu->arch.apicv_active) {
967235d3 2428 kvm_x86_ops->apicv_post_state_restore(vcpu);
4114c27d
WW
2429 kvm_x86_ops->hwapic_irr_update(vcpu,
2430 apic_find_highest_irr(apic));
67c9dddc 2431 kvm_x86_ops->hwapic_isr_update(vcpu,
b4eef9b3 2432 apic_find_highest_isr(apic));
d62caabb 2433 }
3842d135 2434 kvm_make_request(KVM_REQ_EVENT, vcpu);
49df6397
SR
2435 if (ioapic_in_kernel(vcpu->kvm))
2436 kvm_rtc_eoi_tracking_restore_one(vcpu);
0669a510
RK
2437
2438 vcpu->arch.apic_arb_prio = 0;
a92e2543
RK
2439
2440 return 0;
96ad2cc6 2441}
a3d7f85f 2442
2f52d58c 2443void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
a3d7f85f 2444{
a3d7f85f
ED
2445 struct hrtimer *timer;
2446
bce87cce 2447 if (!lapic_in_kernel(vcpu))
a3d7f85f
ED
2448 return;
2449
54e9818f 2450 timer = &vcpu->arch.apic->lapic_timer.timer;
a3d7f85f 2451 if (hrtimer_cancel(timer))
61abdbe0 2452 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
a3d7f85f 2453}
b93463aa 2454
ae7a2a3f
MT
2455/*
2456 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2457 *
2458 * Detect whether guest triggered PV EOI since the
2459 * last entry. If yes, set EOI on guests's behalf.
2460 * Clear PV EOI in guest memory in any case.
2461 */
2462static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2463 struct kvm_lapic *apic)
2464{
2465 bool pending;
2466 int vector;
2467 /*
2468 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2469 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2470 *
2471 * KVM_APIC_PV_EOI_PENDING is unset:
2472 * -> host disabled PV EOI.
2473 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2474 * -> host enabled PV EOI, guest did not execute EOI yet.
2475 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2476 * -> host enabled PV EOI, guest executed EOI.
2477 */
2478 BUG_ON(!pv_eoi_enabled(vcpu));
2479 pending = pv_eoi_get_pending(vcpu);
2480 /*
2481 * Clear pending bit in any case: it will be set again on vmentry.
2482 * While this might not be ideal from performance point of view,
2483 * this makes sure pv eoi is only enabled when we know it's safe.
2484 */
2485 pv_eoi_clr_pending(vcpu);
2486 if (pending)
2487 return;
2488 vector = apic_set_eoi(apic);
2489 trace_kvm_pv_eoi(apic, vector);
2490}
2491
b93463aa
AK
2492void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2493{
2494 u32 data;
b93463aa 2495
ae7a2a3f
MT
2496 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2497 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2498
41383771 2499 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
b93463aa
AK
2500 return;
2501
4e335d9e
PB
2502 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2503 sizeof(u32)))
603242a8 2504 return;
b93463aa
AK
2505
2506 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2507}
2508
ae7a2a3f
MT
2509/*
2510 * apic_sync_pv_eoi_to_guest - called before vmentry
2511 *
2512 * Detect whether it's safe to enable PV EOI and
2513 * if yes do so.
2514 */
2515static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2516 struct kvm_lapic *apic)
2517{
2518 if (!pv_eoi_enabled(vcpu) ||
2519 /* IRR set or many bits in ISR: could be nested. */
2520 apic->irr_pending ||
2521 /* Cache not set: could be safe but we don't bother. */
2522 apic->highest_isr_cache == -1 ||
2523 /* Need EOI to update ioapic. */
3bb345f3 2524 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
ae7a2a3f
MT
2525 /*
2526 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2527 * so we need not do anything here.
2528 */
2529 return;
2530 }
2531
2532 pv_eoi_set_pending(apic->vcpu);
2533}
2534
b93463aa
AK
2535void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2536{
2537 u32 data, tpr;
2538 int max_irr, max_isr;
ae7a2a3f 2539 struct kvm_lapic *apic = vcpu->arch.apic;
b93463aa 2540
ae7a2a3f
MT
2541 apic_sync_pv_eoi_to_guest(vcpu, apic);
2542
41383771 2543 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
b93463aa
AK
2544 return;
2545
dfb95954 2546 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
b93463aa
AK
2547 max_irr = apic_find_highest_irr(apic);
2548 if (max_irr < 0)
2549 max_irr = 0;
2550 max_isr = apic_find_highest_isr(apic);
2551 if (max_isr < 0)
2552 max_isr = 0;
2553 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2554
4e335d9e
PB
2555 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2556 sizeof(u32));
b93463aa
AK
2557}
2558
fda4e2e8 2559int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
b93463aa 2560{
fda4e2e8 2561 if (vapic_addr) {
4e335d9e 2562 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
fda4e2e8
AH
2563 &vcpu->arch.apic->vapic_cache,
2564 vapic_addr, sizeof(u32)))
2565 return -EINVAL;
41383771 2566 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
fda4e2e8 2567 } else {
41383771 2568 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
fda4e2e8
AH
2569 }
2570
2571 vcpu->arch.apic->vapic_addr = vapic_addr;
2572 return 0;
b93463aa 2573}
0105d1a5
GN
2574
2575int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2576{
2577 struct kvm_lapic *apic = vcpu->arch.apic;
2578 u32 reg = (msr - APIC_BASE_MSR) << 4;
2579
35754c98 2580 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
0105d1a5
GN
2581 return 1;
2582
c69d3d9b
NA
2583 if (reg == APIC_ICR2)
2584 return 1;
2585
0105d1a5 2586 /* if this is ICR write vector before command */
decdc283 2587 if (reg == APIC_ICR)
1e6e2755
SS
2588 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2589 return kvm_lapic_reg_write(apic, reg, (u32)data);
0105d1a5
GN
2590}
2591
2592int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2593{
2594 struct kvm_lapic *apic = vcpu->arch.apic;
2595 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2596
35754c98 2597 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
0105d1a5
GN
2598 return 1;
2599
c69d3d9b
NA
2600 if (reg == APIC_DFR || reg == APIC_ICR2) {
2601 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2602 reg);
2603 return 1;
2604 }
2605
1e6e2755 2606 if (kvm_lapic_reg_read(apic, reg, 4, &low))
0105d1a5 2607 return 1;
decdc283 2608 if (reg == APIC_ICR)
1e6e2755 2609 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
0105d1a5
GN
2610
2611 *data = (((u64)high) << 32) | low;
2612
2613 return 0;
2614}
10388a07
GN
2615
2616int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2617{
2618 struct kvm_lapic *apic = vcpu->arch.apic;
2619
bce87cce 2620 if (!lapic_in_kernel(vcpu))
10388a07
GN
2621 return 1;
2622
2623 /* if this is ICR write vector before command */
2624 if (reg == APIC_ICR)
1e6e2755
SS
2625 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2626 return kvm_lapic_reg_write(apic, reg, (u32)data);
10388a07
GN
2627}
2628
2629int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2630{
2631 struct kvm_lapic *apic = vcpu->arch.apic;
2632 u32 low, high = 0;
2633
bce87cce 2634 if (!lapic_in_kernel(vcpu))
10388a07
GN
2635 return 1;
2636
1e6e2755 2637 if (kvm_lapic_reg_read(apic, reg, 4, &low))
10388a07
GN
2638 return 1;
2639 if (reg == APIC_ICR)
1e6e2755 2640 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
10388a07
GN
2641
2642 *data = (((u64)high) << 32) | low;
2643
2644 return 0;
2645}
ae7a2a3f
MT
2646
2647int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2648{
2649 u64 addr = data & ~KVM_MSR_ENABLED;
2650 if (!IS_ALIGNED(addr, 4))
2651 return 1;
2652
2653 vcpu->arch.pv_eoi.msr_val = data;
2654 if (!pv_eoi_enabled(vcpu))
2655 return 0;
4e335d9e 2656 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
8f964525 2657 addr, sizeof(u8));
ae7a2a3f 2658}
c5cc421b 2659
66450a21
JK
2660void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2661{
2662 struct kvm_lapic *apic = vcpu->arch.apic;
2b4a273b 2663 u8 sipi_vector;
299018f4 2664 unsigned long pe;
66450a21 2665
bce87cce 2666 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
66450a21
JK
2667 return;
2668
cd7764fe
PB
2669 /*
2670 * INITs are latched while in SMM. Because an SMM CPU cannot
2671 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2672 * and delay processing of INIT until the next RSM.
2673 */
2674 if (is_smm(vcpu)) {
2675 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2676 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2677 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2678 return;
2679 }
299018f4 2680
cd7764fe 2681 pe = xchg(&apic->pending_events, 0);
299018f4 2682 if (test_bit(KVM_APIC_INIT, &pe)) {
d28bc9dd 2683 kvm_vcpu_reset(vcpu, true);
66450a21
JK
2684 if (kvm_vcpu_is_bsp(apic->vcpu))
2685 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2686 else
2687 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2688 }
299018f4 2689 if (test_bit(KVM_APIC_SIPI, &pe) &&
66450a21
JK
2690 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2691 /* evaluate pending_events before reading the vector */
2692 smp_rmb();
2693 sipi_vector = apic->sipi_vector;
98eff52a 2694 apic_debug("vcpu %d received sipi with vector # %x\n",
66450a21
JK
2695 vcpu->vcpu_id, sipi_vector);
2696 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2697 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2698 }
2699}
2700
c5cc421b
GN
2701void kvm_lapic_init(void)
2702{
2703 /* do not patch jump label more than once per second */
2704 jump_label_rate_limit(&apic_hw_disabled, HZ);
f8c1ea10 2705 jump_label_rate_limit(&apic_sw_disabled, HZ);
c5cc421b 2706}
cef84c30
DM
2707
2708void kvm_lapic_exit(void)
2709{
2710 static_key_deferred_flush(&apic_hw_disabled);
2711 static_key_deferred_flush(&apic_sw_disabled);
2712}