blob: 3692de84c42019c116ab47e71a5861eca25396b5 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
8 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
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
21#include <linux/kvm_host.h>
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>
28#include <linux/export.h>
29#include <linux/math64.h>
30#include <linux/slab.h>
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>
36#include <asm/delay.h>
37#include <linux/atomic.h>
38#include <linux/jump_label.h>
39#include "kvm_cache_regs.h"
40#include "irq.h"
41#include "trace.h"
42#include "x86.h"
43#include "cpuid.h"
44#include "hyperv.h"
45
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
52#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
57/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
58#define apic_debug(fmt, arg...) do {} while (0)
59
60/* 14 is the version for Xeon and Pentium 8.4.8*/
61#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
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
68#define APIC_VECTORS_PER_REG 32
69
70#define APIC_BROADCAST 0xFF
71#define X2APIC_BROADCAST 0xFFFFFFFFul
72
73static inline int apic_test_vector(int vec, void *bitmap)
74{
75 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
76}
77
78bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
79{
80 struct kvm_lapic *apic = vcpu->arch.apic;
81
82 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
83 apic_test_vector(vector, apic->regs + APIC_IRR);
84}
85
86static inline void apic_clear_vector(int vec, void *bitmap)
87{
88 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
89}
90
91static inline int __apic_test_and_set_vector(int vec, void *bitmap)
92{
93 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
94}
95
96static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
97{
98 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
99}
100
101struct static_key_deferred apic_hw_disabled __read_mostly;
102struct static_key_deferred apic_sw_disabled __read_mostly;
103
104static inline int apic_enabled(struct kvm_lapic *apic)
105{
106 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
107}
108
109#define LVT_MASK \
110 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
111
112#define LINT_MASK \
113 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
114 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
115
116static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
117{
118 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
119}
120
121static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
122{
123 return apic->vcpu->vcpu_id;
124}
125
126static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
127 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
128 switch (map->mode) {
129 case KVM_APIC_MODE_X2APIC: {
130 u32 offset = (dest_id >> 16) * 16;
131 u32 max_apic_id = map->max_apic_id;
132
133 if (offset <= max_apic_id) {
134 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
135
136 *cluster = &map->phys_map[offset];
137 *mask = dest_id & (0xffff >> (16 - cluster_size));
138 } else {
139 *mask = 0;
140 }
141
142 return true;
143 }
144 case KVM_APIC_MODE_XAPIC_FLAT:
145 *cluster = map->xapic_flat_map;
146 *mask = dest_id & 0xff;
147 return true;
148 case KVM_APIC_MODE_XAPIC_CLUSTER:
149 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
150 *mask = dest_id & 0xf;
151 return true;
152 default:
153 /* Not optimized. */
154 return false;
155 }
156}
157
158static void kvm_apic_map_free(struct rcu_head *rcu)
159{
160 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
161
162 kvfree(map);
163}
164
165static void recalculate_apic_map(struct kvm *kvm)
166{
167 struct kvm_apic_map *new, *old = NULL;
168 struct kvm_vcpu *vcpu;
169 int i;
170 u32 max_id = 255; /* enough space for any xAPIC ID */
171
172 mutex_lock(&kvm->arch.apic_map_lock);
173
174 kvm_for_each_vcpu(i, vcpu, kvm)
175 if (kvm_apic_present(vcpu))
176 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
177
178 new = kvzalloc(sizeof(struct kvm_apic_map) +
179 sizeof(struct kvm_lapic *) * ((u64)max_id + 1), GFP_KERNEL);
180
181 if (!new)
182 goto out;
183
184 new->max_apic_id = max_id;
185
186 kvm_for_each_vcpu(i, vcpu, kvm) {
187 struct kvm_lapic *apic = vcpu->arch.apic;
188 struct kvm_lapic **cluster;
189 u16 mask;
190 u32 ldr;
191 u8 xapic_id;
192 u32 x2apic_id;
193
194 if (!kvm_apic_present(vcpu))
195 continue;
196
197 xapic_id = kvm_xapic_id(apic);
198 x2apic_id = kvm_x2apic_id(apic);
199
200 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
201 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
202 x2apic_id <= new->max_apic_id)
203 new->phys_map[x2apic_id] = apic;
204 /*
205 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
206 * prevent them from masking VCPUs with APIC ID <= 0xff.
207 */
208 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
209 new->phys_map[xapic_id] = apic;
210
211 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
212
213 if (apic_x2apic_mode(apic)) {
214 new->mode |= KVM_APIC_MODE_X2APIC;
215 } else if (ldr) {
216 ldr = GET_APIC_LOGICAL_ID(ldr);
217 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
218 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
219 else
220 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
221 }
222
223 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
224 continue;
225
226 if (mask)
227 cluster[ffs(mask) - 1] = apic;
228 }
229out:
230 old = rcu_dereference_protected(kvm->arch.apic_map,
231 lockdep_is_held(&kvm->arch.apic_map_lock));
232 rcu_assign_pointer(kvm->arch.apic_map, new);
233 mutex_unlock(&kvm->arch.apic_map_lock);
234
235 if (old)
236 call_rcu(&old->rcu, kvm_apic_map_free);
237
238 kvm_make_scan_ioapic_request(kvm);
239}
240
241static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
242{
243 bool enabled = val & APIC_SPIV_APIC_ENABLED;
244
245 kvm_lapic_set_reg(apic, APIC_SPIV, val);
246
247 if (enabled != apic->sw_enabled) {
248 apic->sw_enabled = enabled;
249 if (enabled) {
250 static_key_slow_dec_deferred(&apic_sw_disabled);
251 recalculate_apic_map(apic->vcpu->kvm);
252 } else
253 static_key_slow_inc(&apic_sw_disabled.key);
254 }
255}
256
257static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
258{
259 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
260 recalculate_apic_map(apic->vcpu->kvm);
261}
262
263static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
264{
265 kvm_lapic_set_reg(apic, APIC_LDR, id);
266 recalculate_apic_map(apic->vcpu->kvm);
267}
268
269static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
270{
271 return ((id >> 4) << 16) | (1 << (id & 0xf));
272}
273
274static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
275{
276 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
277
278 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
279
280 kvm_lapic_set_reg(apic, APIC_ID, id);
281 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
282 recalculate_apic_map(apic->vcpu->kvm);
283}
284
285static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
286{
287 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
288}
289
290static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
291{
292 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
293}
294
295static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
296{
297 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
298}
299
300static inline int apic_lvtt_period(struct kvm_lapic *apic)
301{
302 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
303}
304
305static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
306{
307 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
308}
309
310static inline int apic_lvt_nmi_mode(u32 lvt_val)
311{
312 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
313}
314
315void kvm_apic_set_version(struct kvm_vcpu *vcpu)
316{
317 struct kvm_lapic *apic = vcpu->arch.apic;
318 struct kvm_cpuid_entry2 *feat;
319 u32 v = APIC_VERSION;
320
321 if (!lapic_in_kernel(vcpu))
322 return;
323
324 /*
325 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
326 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
327 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
328 * version first and level-triggered interrupts never get EOIed in
329 * IOAPIC.
330 */
331 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
332 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
333 !ioapic_in_kernel(vcpu->kvm))
334 v |= APIC_LVR_DIRECTED_EOI;
335 kvm_lapic_set_reg(apic, APIC_LVR, v);
336}
337
338static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
339 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
340 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
341 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
342 LINT_MASK, LINT_MASK, /* LVT0-1 */
343 LVT_MASK /* LVTERR */
344};
345
346static int find_highest_vector(void *bitmap)
347{
348 int vec;
349 u32 *reg;
350
351 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
352 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
353 reg = bitmap + REG_POS(vec);
354 if (*reg)
355 return __fls(*reg) + vec;
356 }
357
358 return -1;
359}
360
361static u8 count_vectors(void *bitmap)
362{
363 int vec;
364 u32 *reg;
365 u8 count = 0;
366
367 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
368 reg = bitmap + REG_POS(vec);
369 count += hweight32(*reg);
370 }
371
372 return count;
373}
374
375bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
376{
377 u32 i, vec;
378 u32 pir_val, irr_val, prev_irr_val;
379 int max_updated_irr;
380
381 max_updated_irr = -1;
382 *max_irr = -1;
383
384 for (i = vec = 0; i <= 7; i++, vec += 32) {
385 pir_val = READ_ONCE(pir[i]);
386 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
387 if (pir_val) {
388 prev_irr_val = irr_val;
389 irr_val |= xchg(&pir[i], 0);
390 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
391 if (prev_irr_val != irr_val) {
392 max_updated_irr =
393 __fls(irr_val ^ prev_irr_val) + vec;
394 }
395 }
396 if (irr_val)
397 *max_irr = __fls(irr_val) + vec;
398 }
399
400 return ((max_updated_irr != -1) &&
401 (max_updated_irr == *max_irr));
402}
403EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
404
405bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
406{
407 struct kvm_lapic *apic = vcpu->arch.apic;
408
409 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
410}
411EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
412
413static inline int apic_search_irr(struct kvm_lapic *apic)
414{
415 return find_highest_vector(apic->regs + APIC_IRR);
416}
417
418static inline int apic_find_highest_irr(struct kvm_lapic *apic)
419{
420 int result;
421
422 /*
423 * Note that irr_pending is just a hint. It will be always
424 * true with virtual interrupt delivery enabled.
425 */
426 if (!apic->irr_pending)
427 return -1;
428
429 result = apic_search_irr(apic);
430 ASSERT(result == -1 || result >= 16);
431
432 return result;
433}
434
435static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
436{
437 struct kvm_vcpu *vcpu;
438
439 vcpu = apic->vcpu;
440
441 if (unlikely(vcpu->arch.apicv_active)) {
442 /* need to update RVI */
443 apic_clear_vector(vec, apic->regs + APIC_IRR);
444 kvm_x86_ops->hwapic_irr_update(vcpu,
445 apic_find_highest_irr(apic));
446 } else {
447 apic->irr_pending = false;
448 apic_clear_vector(vec, apic->regs + APIC_IRR);
449 if (apic_search_irr(apic) != -1)
450 apic->irr_pending = true;
451 }
452}
453
454static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
455{
456 struct kvm_vcpu *vcpu;
457
458 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
459 return;
460
461 vcpu = apic->vcpu;
462
463 /*
464 * With APIC virtualization enabled, all caching is disabled
465 * because the processor can modify ISR under the hood. Instead
466 * just set SVI.
467 */
468 if (unlikely(vcpu->arch.apicv_active))
469 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
470 else {
471 ++apic->isr_count;
472 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
473 /*
474 * ISR (in service register) bit is set when injecting an interrupt.
475 * The highest vector is injected. Thus the latest bit set matches
476 * the highest bit in ISR.
477 */
478 apic->highest_isr_cache = vec;
479 }
480}
481
482static inline int apic_find_highest_isr(struct kvm_lapic *apic)
483{
484 int result;
485
486 /*
487 * Note that isr_count is always 1, and highest_isr_cache
488 * is always -1, with APIC virtualization enabled.
489 */
490 if (!apic->isr_count)
491 return -1;
492 if (likely(apic->highest_isr_cache != -1))
493 return apic->highest_isr_cache;
494
495 result = find_highest_vector(apic->regs + APIC_ISR);
496 ASSERT(result == -1 || result >= 16);
497
498 return result;
499}
500
501static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
502{
503 struct kvm_vcpu *vcpu;
504 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
505 return;
506
507 vcpu = apic->vcpu;
508
509 /*
510 * We do get here for APIC virtualization enabled if the guest
511 * uses the Hyper-V APIC enlightenment. In this case we may need
512 * to trigger a new interrupt delivery by writing the SVI field;
513 * on the other hand isr_count and highest_isr_cache are unused
514 * and must be left alone.
515 */
516 if (unlikely(vcpu->arch.apicv_active))
517 kvm_x86_ops->hwapic_isr_update(vcpu,
518 apic_find_highest_isr(apic));
519 else {
520 --apic->isr_count;
521 BUG_ON(apic->isr_count < 0);
522 apic->highest_isr_cache = -1;
523 }
524}
525
526int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
527{
528 /* This may race with setting of irr in __apic_accept_irq() and
529 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
530 * will cause vmexit immediately and the value will be recalculated
531 * on the next vmentry.
532 */
533 return apic_find_highest_irr(vcpu->arch.apic);
534}
535EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
536
537static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
538 int vector, int level, int trig_mode,
539 struct dest_map *dest_map);
540
541int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
542 struct dest_map *dest_map)
543{
544 struct kvm_lapic *apic = vcpu->arch.apic;
545
546 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
547 irq->level, irq->trig_mode, dest_map);
548}
549
550int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
551 unsigned long ipi_bitmap_high, u32 min,
552 unsigned long icr, int op_64_bit)
553{
554 int i;
555 struct kvm_apic_map *map;
556 struct kvm_vcpu *vcpu;
557 struct kvm_lapic_irq irq = {0};
558 int cluster_size = op_64_bit ? 64 : 32;
559 int count = 0;
560
561 irq.vector = icr & APIC_VECTOR_MASK;
562 irq.delivery_mode = icr & APIC_MODE_MASK;
563 irq.level = (icr & APIC_INT_ASSERT) != 0;
564 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
565
566 if (icr & APIC_DEST_MASK)
567 return -KVM_EINVAL;
568 if (icr & APIC_SHORT_MASK)
569 return -KVM_EINVAL;
570
571 rcu_read_lock();
572 map = rcu_dereference(kvm->arch.apic_map);
573
574 if (unlikely(!map)) {
575 count = -EOPNOTSUPP;
576 goto out;
577 }
578
579 if (min > map->max_apic_id)
580 goto out;
581 /* Bits above cluster_size are masked in the caller. */
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 }
588 }
589
590 min += cluster_size;
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 }
601 }
602
603out:
604 rcu_read_unlock();
605 return count;
606}
607
608static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
609{
610
611 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
612 sizeof(val));
613}
614
615static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
616{
617
618 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
619 sizeof(*val));
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",
632 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
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",
640 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
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",
650 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
651 return;
652 }
653 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
654}
655
656static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
657{
658 int highest_irr;
659 if (apic->vcpu->arch.apicv_active)
660 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
661 else
662 highest_irr = apic_find_highest_irr(apic);
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)
669{
670 u32 tpr, isrv, ppr, old_ppr;
671 int isr;
672
673 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
674 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
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
686 *new_ppr = ppr;
687 if (old_ppr != ppr)
688 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
689
690 return ppr < old_ppr;
691}
692
693static void apic_update_ppr(struct kvm_lapic *apic)
694{
695 u32 ppr;
696
697 if (__apic_update_ppr(apic, &ppr) &&
698 apic_has_interrupt_for_ppr(apic, ppr) != -1)
699 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
700}
701
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
708static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
709{
710 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
711 apic_update_ppr(apic);
712}
713
714static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
715{
716 return mda == (apic_x2apic_mode(apic) ?
717 X2APIC_BROADCAST : APIC_BROADCAST);
718}
719
720static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
721{
722 if (kvm_apic_broadcast(apic, mda))
723 return true;
724
725 if (apic_x2apic_mode(apic))
726 return mda == kvm_x2apic_id(apic);
727
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
737 return mda == kvm_xapic_id(apic);
738}
739
740static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
741{
742 u32 logical_id;
743
744 if (kvm_apic_broadcast(apic, mda))
745 return true;
746
747 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
748
749 if (apic_x2apic_mode(apic))
750 return ((logical_id >> 16) == (mda >> 16))
751 && (logical_id & mda & 0xffff) != 0;
752
753 logical_id = GET_APIC_LOGICAL_ID(logical_id);
754
755 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
756 case APIC_DFR_FLAT:
757 return (logical_id & mda) != 0;
758 case APIC_DFR_CLUSTER:
759 return ((logical_id >> 4) == (mda >> 4))
760 && (logical_id & mda & 0xf) != 0;
761 default:
762 apic_debug("Bad DFR vcpu %d: %08x\n",
763 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
764 return false;
765 }
766}
767
768/* The KVM local APIC implementation has two quirks:
769 *
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.
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".
783 */
784static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
785 struct kvm_lapic *source, struct kvm_lapic *target)
786{
787 bool ipi = source != NULL;
788
789 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
790 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
791 return X2APIC_BROADCAST;
792
793 return dest_id;
794}
795
796bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
797 int short_hand, unsigned int dest, int dest_mode)
798{
799 struct kvm_lapic *target = vcpu->arch.apic;
800 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
801
802 apic_debug("target %p, source %p, dest 0x%x, "
803 "dest_mode 0x%x, short_hand 0x%x\n",
804 target, source, dest, dest_mode, short_hand);
805
806 ASSERT(target);
807 switch (short_hand) {
808 case APIC_DEST_NOSHORT:
809 if (dest_mode == APIC_DEST_PHYSICAL)
810 return kvm_apic_match_physical_addr(target, mda);
811 else
812 return kvm_apic_match_logical_addr(target, mda);
813 case APIC_DEST_SELF:
814 return target == source;
815 case APIC_DEST_ALLINC:
816 return true;
817 case APIC_DEST_ALLBUT:
818 return target != source;
819 default:
820 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
821 short_hand);
822 return false;
823 }
824}
825EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
826
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
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
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)
854{
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 }
867
868 return false;
869}
870
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;
884
885 if (irq->shorthand == APIC_DEST_SELF && src) {
886 *dst = src;
887 *bitmap = 1;
888 return true;
889 } else if (irq->shorthand)
890 return false;
891
892 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
893 return false;
894
895 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
896 if (irq->dest_id > map->max_apic_id) {
897 *bitmap = 0;
898 } else {
899 *dst = &map->phys_map[irq->dest_id];
900 *bitmap = 1;
901 }
902 return true;
903 }
904
905 *bitmap = 0;
906 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
907 (u16 *)bitmap))
908 return false;
909
910 if (!kvm_lowest_prio_delivery(irq))
911 return true;
912
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;
923 }
924 } else {
925 if (!*bitmap)
926 return true;
927
928 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
929 bitmap, 16);
930
931 if (!(*dst)[lowest]) {
932 kvm_apic_disabled_lapic_found(kvm);
933 *bitmap = 0;
934 return true;
935 }
936 }
937
938 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
939
940 return true;
941}
942
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;
951
952 *r = -1;
953
954 if (irq->shorthand == APIC_DEST_SELF) {
955 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
956 return true;
957 }
958
959 rcu_read_lock();
960 map = rcu_dereference(kvm->arch.apic_map);
961
962 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
963 if (ret)
964 for_each_set_bit(i, &bitmap, 16) {
965 if (!dst[i])
966 continue;
967 if (*r < 0)
968 *r = 0;
969 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
970 }
971
972 rcu_read_unlock();
973 return ret;
974}
975
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 */
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;
994 unsigned long bitmap;
995 struct kvm_lapic **dst = NULL;
996 bool ret = false;
997
998 if (irq->shorthand)
999 return false;
1000
1001 rcu_read_lock();
1002 map = rcu_dereference(kvm->arch.apic_map);
1003
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);
1007
1008 if (dst[i]) {
1009 *dest_vcpu = dst[i]->vcpu;
1010 ret = true;
1011 }
1012 }
1013
1014 rcu_read_unlock();
1015 return ret;
1016}
1017
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,
1023 int vector, int level, int trig_mode,
1024 struct dest_map *dest_map)
1025{
1026 int result = 0;
1027 struct kvm_vcpu *vcpu = apic->vcpu;
1028
1029 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1030 trig_mode, vector);
1031 switch (delivery_mode) {
1032 case APIC_DM_LOWEST:
1033 vcpu->arch.apic_arb_prio++;
1034 case APIC_DM_FIXED:
1035 if (unlikely(trig_mode && !level))
1036 break;
1037
1038 /* FIXME add logic for vcpu on reset */
1039 if (unlikely(!apic_enabled(apic)))
1040 break;
1041
1042 result = 1;
1043
1044 if (dest_map) {
1045 __set_bit(vcpu->vcpu_id, dest_map->map);
1046 dest_map->vectors[vcpu->vcpu_id] = vector;
1047 }
1048
1049 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1050 if (trig_mode)
1051 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
1052 else
1053 apic_clear_vector(vector, apic->regs + APIC_TMR);
1054 }
1055
1056 if (vcpu->arch.apicv_active)
1057 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
1058 else {
1059 kvm_lapic_set_irr(vector, apic);
1060
1061 kvm_make_request(KVM_REQ_EVENT, vcpu);
1062 kvm_vcpu_kick(vcpu);
1063 }
1064 break;
1065
1066 case APIC_DM_REMRD:
1067 result = 1;
1068 vcpu->arch.pv.pv_unhalted = 1;
1069 kvm_make_request(KVM_REQ_EVENT, vcpu);
1070 kvm_vcpu_kick(vcpu);
1071 break;
1072
1073 case APIC_DM_SMI:
1074 result = 1;
1075 kvm_make_request(KVM_REQ_SMI, vcpu);
1076 kvm_vcpu_kick(vcpu);
1077 break;
1078
1079 case APIC_DM_NMI:
1080 result = 1;
1081 kvm_inject_nmi(vcpu);
1082 kvm_vcpu_kick(vcpu);
1083 break;
1084
1085 case APIC_DM_INIT:
1086 if (!trig_mode || level) {
1087 result = 1;
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();
1093 kvm_make_request(KVM_REQ_EVENT, vcpu);
1094 kvm_vcpu_kick(vcpu);
1095 } else {
1096 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
1097 vcpu->vcpu_id);
1098 }
1099 break;
1100
1101 case APIC_DM_STARTUP:
1102 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
1103 vcpu->vcpu_id, vector);
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);
1111 break;
1112
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
1121 default:
1122 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1123 delivery_mode);
1124 break;
1125 }
1126 return result;
1127}
1128
1129int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
1130{
1131 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
1132}
1133
1134static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1135{
1136 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
1137}
1138
1139static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1140{
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;
1146
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;
1152 }
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);
1160}
1161
1162static int apic_set_eoi(struct kvm_lapic *apic)
1163{
1164 int vector = apic_find_highest_isr(apic);
1165
1166 trace_kvm_eoi(apic, vector);
1167
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)
1173 return vector;
1174
1175 apic_clear_isr(vector, apic);
1176 apic_update_ppr(apic);
1177
1178 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1179 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1180
1181 kvm_ioapic_send_eoi(apic, vector);
1182 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1183 return vector;
1184}
1185
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
1201static void apic_send_ipi(struct kvm_lapic *apic)
1202{
1203 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1204 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
1205 struct kvm_lapic_irq irq;
1206
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;
1210 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
1211 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1212 irq.shorthand = icr_low & APIC_SHORT_MASK;
1213 irq.msi_redir_hint = false;
1214 if (apic_x2apic_mode(apic))
1215 irq.dest_id = icr_high;
1216 else
1217 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
1218
1219 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1220
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, "
1223 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1224 "msi_redir_hint 0x%x\n",
1225 icr_high, icr_low, irq.shorthand, irq.dest_id,
1226 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
1227 irq.vector, irq.msi_redir_hint);
1228
1229 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
1230}
1231
1232static u32 apic_get_tmcct(struct kvm_lapic *apic)
1233{
1234 ktime_t remaining, now;
1235 s64 ns;
1236 u32 tmcct;
1237
1238 ASSERT(apic != NULL);
1239
1240 /* if initial count is 0, current count should also be 0 */
1241 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
1242 apic->lapic_timer.period == 0)
1243 return 0;
1244
1245 now = ktime_get();
1246 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1247 if (ktime_to_ns(remaining) < 0)
1248 remaining = 0;
1249
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));
1253
1254 return tmcct;
1255}
1256
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
1262 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
1263 run->tpr_access.rip = kvm_rip_read(vcpu);
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
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:
1282 apic_debug("Access APIC ARBPRI register which is for P6\n");
1283 break;
1284
1285 case APIC_TMCCT: /* Timer CCR */
1286 if (apic_lvtt_tscdeadline(apic))
1287 return 0;
1288
1289 val = apic_get_tmcct(apic);
1290 break;
1291 case APIC_PROCPRI:
1292 apic_update_ppr(apic);
1293 val = kvm_lapic_get_reg(apic, offset);
1294 break;
1295 case APIC_TASKPRI:
1296 report_tpr_access(apic, false);
1297 /* fall thru */
1298 default:
1299 val = kvm_lapic_get_reg(apic, offset);
1300 break;
1301 }
1302
1303 return val;
1304}
1305
1306static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1307{
1308 return container_of(dev, struct kvm_lapic, dev);
1309}
1310
1311int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
1312 void *data)
1313{
1314 unsigned char alignment = offset & 0xf;
1315 u32 result;
1316 /* this bitmask has a bit cleared for each reserved register */
1317 static const u64 rmask = 0x43ff01ffffffe70cULL;
1318
1319 if ((alignment + len) > 4) {
1320 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1321 offset, len);
1322 return 1;
1323 }
1324
1325 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
1326 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1327 offset);
1328 return 1;
1329 }
1330
1331 result = __apic_read(apic, offset & ~0xf);
1332
1333 trace_kvm_apic_read(offset, result);
1334
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 }
1346 return 0;
1347}
1348EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
1349
1350static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1351{
1352 return addr >= apic->base_address &&
1353 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1354}
1355
1356static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
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
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
1374 kvm_lapic_reg_read(apic, offset, len, data);
1375
1376 return 0;
1377}
1378
1379static void update_divide_count(struct kvm_lapic *apic)
1380{
1381 u32 tmp1, tmp2, tdcr;
1382
1383 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
1384 tmp1 = tdcr & 0xf;
1385 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
1386 apic->divide_count = 0x1 << (tmp2 & 0x7);
1387
1388 apic_debug("timer divide count is 0x%x\n",
1389 apic->divide_count);
1390}
1391
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 */
1399 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
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
1413static void apic_update_lvtt(struct kvm_lapic *apic)
1414{
1415 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
1416 apic->lapic_timer.timer_mode_mask;
1417
1418 if (apic->lapic_timer.timer_mode != timer_mode) {
1419 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
1420 APIC_LVT_TIMER_TSCDEADLINE)) {
1421 hrtimer_cancel(&apic->lapic_timer.timer);
1422 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1423 apic->lapic_timer.period = 0;
1424 apic->lapic_timer.tscdeadline = 0;
1425 }
1426 apic->lapic_timer.timer_mode = timer_mode;
1427 limit_periodic_timer_frequency(apic);
1428 }
1429}
1430
1431static void apic_timer_expired(struct kvm_lapic *apic)
1432{
1433 struct kvm_vcpu *vcpu = apic->vcpu;
1434 struct swait_queue_head *q = &vcpu->wq;
1435 struct kvm_timer *ktimer = &apic->lapic_timer;
1436
1437 if (atomic_read(&apic->lapic_timer.pending))
1438 return;
1439
1440 atomic_inc(&apic->lapic_timer.pending);
1441 kvm_set_pending_timer(vcpu);
1442
1443 /*
1444 * For x86, the atomic_inc() is serialized, thus
1445 * using swait_active() is safe.
1446 */
1447 if (swait_active(q))
1448 swake_up_one(q);
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;
1462 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
1463
1464 if (kvm_apic_hw_enabled(apic)) {
1465 int vec = reg & APIC_VECTOR_MASK;
1466 void *bitmap = apic->regs + APIC_ISR;
1467
1468 if (vcpu->arch.apicv_active)
1469 bitmap = apic->regs + APIC_IRR;
1470
1471 if (apic_test_vector(vec, bitmap))
1472 return true;
1473 }
1474 return false;
1475}
1476
1477void wait_lapic_expire(struct kvm_vcpu *vcpu)
1478{
1479 struct kvm_lapic *apic = vcpu->arch.apic;
1480 u64 guest_tsc, tsc_deadline;
1481
1482 if (!lapic_in_kernel(vcpu))
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;
1493 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1494 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
1495
1496 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1497 if (guest_tsc < tsc_deadline)
1498 __delay(min(tsc_deadline - guest_tsc,
1499 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
1500}
1501
1502static void start_sw_tscdeadline(struct kvm_lapic *apic)
1503{
1504 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1505 u64 ns = 0;
1506 ktime_t expire;
1507 struct kvm_vcpu *vcpu = apic->vcpu;
1508 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1509 unsigned long flags;
1510 ktime_t now;
1511
1512 if (unlikely(!tscdeadline || !this_tsc_khz))
1513 return;
1514
1515 local_irq_save(flags);
1516
1517 now = ktime_get();
1518 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1519 if (likely(tscdeadline > guest_tsc)) {
1520 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1521 do_div(ns, this_tsc_khz);
1522 expire = ktime_add_ns(now, ns);
1523 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1524 hrtimer_start(&apic->lapic_timer.timer,
1525 expire, HRTIMER_MODE_ABS_PINNED);
1526 } else
1527 apic_timer_expired(apic);
1528
1529 local_irq_restore(flags);
1530}
1531
1532static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1533{
1534 ktime_t now, remaining;
1535 u64 ns_remaining_old, ns_remaining_new;
1536
1537 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1538 * APIC_BUS_CYCLE_NS * apic->divide_count;
1539 limit_periodic_timer_frequency(apic);
1540
1541 now = ktime_get();
1542 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1543 if (ktime_to_ns(remaining) < 0)
1544 remaining = 0;
1545
1546 ns_remaining_old = ktime_to_ns(remaining);
1547 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1548 apic->divide_count, old_divisor);
1549
1550 apic->lapic_timer.tscdeadline +=
1551 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1552 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1553 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1554}
1555
1556static bool set_target_expiration(struct kvm_lapic *apic)
1557{
1558 ktime_t now;
1559 u64 tscl = rdtsc();
1560
1561 now = ktime_get();
1562 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1563 * APIC_BUS_CYCLE_NS * apic->divide_count;
1564
1565 if (!apic->lapic_timer.period) {
1566 apic->lapic_timer.tscdeadline = 0;
1567 return false;
1568 }
1569
1570 limit_periodic_timer_frequency(apic);
1571
1572 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1573 PRIx64 ", "
1574 "timer initial count 0x%x, period %lldns, "
1575 "expire @ 0x%016" PRIx64 ".\n", __func__,
1576 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1577 kvm_lapic_get_reg(apic, APIC_TMICT),
1578 apic->lapic_timer.period,
1579 ktime_to_ns(ktime_add_ns(now,
1580 apic->lapic_timer.period)));
1581
1582 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1583 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1584 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1585
1586 return true;
1587}
1588
1589static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1590{
1591 ktime_t now = ktime_get();
1592 u64 tscl = rdtsc();
1593 ktime_t delta;
1594
1595 /*
1596 * Synchronize both deadlines to the same time source or
1597 * differences in the periods (caused by differences in the
1598 * underlying clocks or numerical approximation errors) will
1599 * cause the two to drift apart over time as the errors
1600 * accumulate.
1601 */
1602 apic->lapic_timer.target_expiration =
1603 ktime_add_ns(apic->lapic_timer.target_expiration,
1604 apic->lapic_timer.period);
1605 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1606 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1607 nsec_to_cycles(apic->vcpu, delta);
1608}
1609
1610static void start_sw_period(struct kvm_lapic *apic)
1611{
1612 if (!apic->lapic_timer.period)
1613 return;
1614
1615 if (ktime_after(ktime_get(),
1616 apic->lapic_timer.target_expiration)) {
1617 apic_timer_expired(apic);
1618
1619 if (apic_lvtt_oneshot(apic))
1620 return;
1621
1622 advance_periodic_target_expiration(apic);
1623 }
1624
1625 hrtimer_start(&apic->lapic_timer.timer,
1626 apic->lapic_timer.target_expiration,
1627 HRTIMER_MODE_ABS_PINNED);
1628}
1629
1630bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1631{
1632 if (!lapic_in_kernel(vcpu))
1633 return false;
1634
1635 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1636}
1637EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1638
1639static void cancel_hv_timer(struct kvm_lapic *apic)
1640{
1641 WARN_ON(preemptible());
1642 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1643 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1644 apic->lapic_timer.hv_timer_in_use = false;
1645}
1646
1647static bool start_hv_timer(struct kvm_lapic *apic)
1648{
1649 struct kvm_timer *ktimer = &apic->lapic_timer;
1650 int r;
1651
1652 WARN_ON(preemptible());
1653 if (!kvm_x86_ops->set_hv_timer)
1654 return false;
1655
1656 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1657 return false;
1658
1659 if (!ktimer->tscdeadline)
1660 return false;
1661
1662 r = kvm_x86_ops->set_hv_timer(apic->vcpu, ktimer->tscdeadline);
1663 if (r < 0)
1664 return false;
1665
1666 ktimer->hv_timer_in_use = true;
1667 hrtimer_cancel(&ktimer->timer);
1668
1669 /*
1670 * Also recheck ktimer->pending, in case the sw timer triggered in
1671 * the window. For periodic timer, leave the hv timer running for
1672 * simplicity, and the deadline will be recomputed on the next vmexit.
1673 */
1674 if (!apic_lvtt_period(apic) && (r || atomic_read(&ktimer->pending))) {
1675 if (r)
1676 apic_timer_expired(apic);
1677 return false;
1678 }
1679
1680 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true);
1681 return true;
1682}
1683
1684static void start_sw_timer(struct kvm_lapic *apic)
1685{
1686 struct kvm_timer *ktimer = &apic->lapic_timer;
1687
1688 WARN_ON(preemptible());
1689 if (apic->lapic_timer.hv_timer_in_use)
1690 cancel_hv_timer(apic);
1691 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1692 return;
1693
1694 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1695 start_sw_period(apic);
1696 else if (apic_lvtt_tscdeadline(apic))
1697 start_sw_tscdeadline(apic);
1698 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1699}
1700
1701static void restart_apic_timer(struct kvm_lapic *apic)
1702{
1703 preempt_disable();
1704 if (!start_hv_timer(apic))
1705 start_sw_timer(apic);
1706 preempt_enable();
1707}
1708
1709void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1710{
1711 struct kvm_lapic *apic = vcpu->arch.apic;
1712
1713 preempt_disable();
1714 /* If the preempt notifier has already run, it also called apic_timer_expired */
1715 if (!apic->lapic_timer.hv_timer_in_use)
1716 goto out;
1717 WARN_ON(swait_active(&vcpu->wq));
1718 cancel_hv_timer(apic);
1719 apic_timer_expired(apic);
1720
1721 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1722 advance_periodic_target_expiration(apic);
1723 restart_apic_timer(apic);
1724 }
1725out:
1726 preempt_enable();
1727}
1728EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1729
1730void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1731{
1732 restart_apic_timer(vcpu->arch.apic);
1733}
1734EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1735
1736void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1737{
1738 struct kvm_lapic *apic = vcpu->arch.apic;
1739
1740 preempt_disable();
1741 /* Possibly the TSC deadline timer is not enabled yet */
1742 if (apic->lapic_timer.hv_timer_in_use)
1743 start_sw_timer(apic);
1744 preempt_enable();
1745}
1746EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1747
1748void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1749{
1750 struct kvm_lapic *apic = vcpu->arch.apic;
1751
1752 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1753 restart_apic_timer(apic);
1754}
1755
1756static void start_apic_timer(struct kvm_lapic *apic)
1757{
1758 atomic_set(&apic->lapic_timer.pending, 0);
1759
1760 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1761 && !set_target_expiration(apic))
1762 return;
1763
1764 restart_apic_timer(apic);
1765}
1766
1767static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1768{
1769 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
1770
1771 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1772 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1773 if (lvt0_in_nmi_mode) {
1774 apic_debug("Receive NMI setting on APIC_LVT0 "
1775 "for cpu %d\n", apic->vcpu->vcpu_id);
1776 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1777 } else
1778 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1779 }
1780}
1781
1782int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
1783{
1784 int ret = 0;
1785
1786 trace_kvm_apic_write(reg, val);
1787
1788 switch (reg) {
1789 case APIC_ID: /* Local APIC ID */
1790 if (!apic_x2apic_mode(apic))
1791 kvm_apic_set_xapic_id(apic, val >> 24);
1792 else
1793 ret = 1;
1794 break;
1795
1796 case APIC_TASKPRI:
1797 report_tpr_access(apic, true);
1798 apic_set_tpr(apic, val & 0xff);
1799 break;
1800
1801 case APIC_EOI:
1802 apic_set_eoi(apic);
1803 break;
1804
1805 case APIC_LDR:
1806 if (!apic_x2apic_mode(apic))
1807 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
1808 else
1809 ret = 1;
1810 break;
1811
1812 case APIC_DFR:
1813 if (!apic_x2apic_mode(apic)) {
1814 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
1815 recalculate_apic_map(apic->vcpu->kvm);
1816 } else
1817 ret = 1;
1818 break;
1819
1820 case APIC_SPIV: {
1821 u32 mask = 0x3ff;
1822 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
1823 mask |= APIC_SPIV_DIRECTED_EOI;
1824 apic_set_spiv(apic, val & mask);
1825 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1826 int i;
1827 u32 lvt_val;
1828
1829 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
1830 lvt_val = kvm_lapic_get_reg(apic,
1831 APIC_LVTT + 0x10 * i);
1832 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
1833 lvt_val | APIC_LVT_MASKED);
1834 }
1835 apic_update_lvtt(apic);
1836 atomic_set(&apic->lapic_timer.pending, 0);
1837
1838 }
1839 break;
1840 }
1841 case APIC_ICR:
1842 /* No delay here, so we always clear the pending bit */
1843 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
1844 apic_send_ipi(apic);
1845 break;
1846
1847 case APIC_ICR2:
1848 if (!apic_x2apic_mode(apic))
1849 val &= 0xff000000;
1850 kvm_lapic_set_reg(apic, APIC_ICR2, val);
1851 break;
1852
1853 case APIC_LVT0:
1854 apic_manage_nmi_watchdog(apic, val);
1855 case APIC_LVTTHMR:
1856 case APIC_LVTPC:
1857 case APIC_LVT1:
1858 case APIC_LVTERR:
1859 /* TODO: Check vector */
1860 if (!kvm_apic_sw_enabled(apic))
1861 val |= APIC_LVT_MASKED;
1862
1863 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
1864 kvm_lapic_set_reg(apic, reg, val);
1865
1866 break;
1867
1868 case APIC_LVTT:
1869 if (!kvm_apic_sw_enabled(apic))
1870 val |= APIC_LVT_MASKED;
1871 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
1872 kvm_lapic_set_reg(apic, APIC_LVTT, val);
1873 apic_update_lvtt(apic);
1874 break;
1875
1876 case APIC_TMICT:
1877 if (apic_lvtt_tscdeadline(apic))
1878 break;
1879
1880 hrtimer_cancel(&apic->lapic_timer.timer);
1881 kvm_lapic_set_reg(apic, APIC_TMICT, val);
1882 start_apic_timer(apic);
1883 break;
1884
1885 case APIC_TDCR: {
1886 uint32_t old_divisor = apic->divide_count;
1887
1888 if (val & 4)
1889 apic_debug("KVM_WRITE:TDCR %x\n", val);
1890 kvm_lapic_set_reg(apic, APIC_TDCR, val);
1891 update_divide_count(apic);
1892 if (apic->divide_count != old_divisor &&
1893 apic->lapic_timer.period) {
1894 hrtimer_cancel(&apic->lapic_timer.timer);
1895 update_target_expiration(apic, old_divisor);
1896 restart_apic_timer(apic);
1897 }
1898 break;
1899 }
1900 case APIC_ESR:
1901 if (apic_x2apic_mode(apic) && val != 0) {
1902 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
1903 ret = 1;
1904 }
1905 break;
1906
1907 case APIC_SELF_IPI:
1908 if (apic_x2apic_mode(apic)) {
1909 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
1910 } else
1911 ret = 1;
1912 break;
1913 default:
1914 ret = 1;
1915 break;
1916 }
1917 if (ret)
1918 apic_debug("Local APIC Write to read-only register %x\n", reg);
1919 return ret;
1920}
1921EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
1922
1923static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
1924 gpa_t address, int len, const void *data)
1925{
1926 struct kvm_lapic *apic = to_lapic(this);
1927 unsigned int offset = address - apic->base_address;
1928 u32 val;
1929
1930 if (!apic_mmio_in_range(apic, address))
1931 return -EOPNOTSUPP;
1932
1933 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1934 if (!kvm_check_has_quirk(vcpu->kvm,
1935 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1936 return -EOPNOTSUPP;
1937
1938 return 0;
1939 }
1940
1941 /*
1942 * APIC register must be aligned on 128-bits boundary.
1943 * 32/64/128 bits registers must be accessed thru 32 bits.
1944 * Refer SDM 8.4.1
1945 */
1946 if (len != 4 || (offset & 0xf)) {
1947 /* Don't shout loud, $infamous_os would cause only noise. */
1948 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
1949 return 0;
1950 }
1951
1952 val = *(u32*)data;
1953
1954 /* too common printing */
1955 if (offset != APIC_EOI)
1956 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1957 "0x%x\n", __func__, offset, len, val);
1958
1959 kvm_lapic_reg_write(apic, offset & 0xff0, val);
1960
1961 return 0;
1962}
1963
1964void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1965{
1966 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
1967}
1968EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1969
1970/* emulate APIC access in a trap manner */
1971void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1972{
1973 u32 val = 0;
1974
1975 /* hw has done the conditional check and inst decode */
1976 offset &= 0xff0;
1977
1978 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
1979
1980 /* TODO: optimize to just emulate side effect w/o one more write */
1981 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
1982}
1983EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1984
1985void kvm_free_lapic(struct kvm_vcpu *vcpu)
1986{
1987 struct kvm_lapic *apic = vcpu->arch.apic;
1988
1989 if (!vcpu->arch.apic)
1990 return;
1991
1992 hrtimer_cancel(&apic->lapic_timer.timer);
1993
1994 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1995 static_key_slow_dec_deferred(&apic_hw_disabled);
1996
1997 if (!apic->sw_enabled)
1998 static_key_slow_dec_deferred(&apic_sw_disabled);
1999
2000 if (apic->regs)
2001 free_page((unsigned long)apic->regs);
2002
2003 kfree(apic);
2004}
2005
2006/*
2007 *----------------------------------------------------------------------
2008 * LAPIC interface
2009 *----------------------------------------------------------------------
2010 */
2011u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2012{
2013 struct kvm_lapic *apic = vcpu->arch.apic;
2014
2015 if (!lapic_in_kernel(vcpu) ||
2016 !apic_lvtt_tscdeadline(apic))
2017 return 0;
2018
2019 return apic->lapic_timer.tscdeadline;
2020}
2021
2022void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2023{
2024 struct kvm_lapic *apic = vcpu->arch.apic;
2025
2026 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
2027 apic_lvtt_period(apic))
2028 return;
2029
2030 hrtimer_cancel(&apic->lapic_timer.timer);
2031 apic->lapic_timer.tscdeadline = data;
2032 start_apic_timer(apic);
2033}
2034
2035void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2036{
2037 struct kvm_lapic *apic = vcpu->arch.apic;
2038
2039 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
2040 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
2041}
2042
2043u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2044{
2045 u64 tpr;
2046
2047 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
2048
2049 return (tpr & 0xf0) >> 4;
2050}
2051
2052void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2053{
2054 u64 old_value = vcpu->arch.apic_base;
2055 struct kvm_lapic *apic = vcpu->arch.apic;
2056
2057 if (!apic)
2058 value |= MSR_IA32_APICBASE_BSP;
2059
2060 vcpu->arch.apic_base = value;
2061
2062 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2063 kvm_update_cpuid(vcpu);
2064
2065 if (!apic)
2066 return;
2067
2068 /* update jump label if enable bit changes */
2069 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
2070 if (value & MSR_IA32_APICBASE_ENABLE) {
2071 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2072 static_key_slow_dec_deferred(&apic_hw_disabled);
2073 } else {
2074 static_key_slow_inc(&apic_hw_disabled.key);
2075 recalculate_apic_map(vcpu->kvm);
2076 }
2077 }
2078
2079 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2080 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2081
2082 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2083 kvm_x86_ops->set_virtual_apic_mode(vcpu);
2084
2085 apic->base_address = apic->vcpu->arch.apic_base &
2086 MSR_IA32_APICBASE_BASE;
2087
2088 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2089 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2090 pr_warn_once("APIC base relocation is unsupported by KVM");
2091
2092 /* with FSB delivery interrupt, we can restart APIC functionality */
2093 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
2094 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
2095
2096}
2097
2098void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
2099{
2100 struct kvm_lapic *apic = vcpu->arch.apic;
2101 int i;
2102
2103 if (!apic)
2104 return;
2105
2106 apic_debug("%s\n", __func__);
2107
2108 /* Stop the timer in case it's a reset to an active apic */
2109 hrtimer_cancel(&apic->lapic_timer.timer);
2110
2111 if (!init_event) {
2112 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2113 MSR_IA32_APICBASE_ENABLE);
2114 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2115 }
2116 kvm_apic_set_version(apic->vcpu);
2117
2118 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2119 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
2120 apic_update_lvtt(apic);
2121 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2122 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
2123 kvm_lapic_set_reg(apic, APIC_LVT0,
2124 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
2125 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
2126
2127 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
2128 apic_set_spiv(apic, 0xff);
2129 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
2130 if (!apic_x2apic_mode(apic))
2131 kvm_apic_set_ldr(apic, 0);
2132 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2133 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2134 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2135 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2136 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
2137 for (i = 0; i < 8; i++) {
2138 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2139 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2140 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
2141 }
2142 apic->irr_pending = vcpu->arch.apicv_active;
2143 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
2144 apic->highest_isr_cache = -1;
2145 update_divide_count(apic);
2146 atomic_set(&apic->lapic_timer.pending, 0);
2147 if (kvm_vcpu_is_bsp(vcpu))
2148 kvm_lapic_set_base(vcpu,
2149 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
2150 vcpu->arch.pv_eoi.msr_val = 0;
2151 apic_update_ppr(apic);
2152 if (vcpu->arch.apicv_active) {
2153 kvm_x86_ops->apicv_post_state_restore(vcpu);
2154 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2155 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2156 }
2157
2158 vcpu->arch.apic_arb_prio = 0;
2159 vcpu->arch.apic_attention = 0;
2160
2161 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
2162 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
2163 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
2164 vcpu->arch.apic_base, apic->base_address);
2165}
2166
2167/*
2168 *----------------------------------------------------------------------
2169 * timer interface
2170 *----------------------------------------------------------------------
2171 */
2172
2173static bool lapic_is_periodic(struct kvm_lapic *apic)
2174{
2175 return apic_lvtt_period(apic);
2176}
2177
2178int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2179{
2180 struct kvm_lapic *apic = vcpu->arch.apic;
2181
2182 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
2183 return atomic_read(&apic->lapic_timer.pending);
2184
2185 return 0;
2186}
2187
2188int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
2189{
2190 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
2191 int vector, mode, trig_mode;
2192
2193 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
2194 vector = reg & APIC_VECTOR_MASK;
2195 mode = reg & APIC_MODE_MASK;
2196 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
2197 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2198 NULL);
2199 }
2200 return 0;
2201}
2202
2203void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
2204{
2205 struct kvm_lapic *apic = vcpu->arch.apic;
2206
2207 if (apic)
2208 kvm_apic_local_deliver(apic, APIC_LVT0);
2209}
2210
2211static const struct kvm_io_device_ops apic_mmio_ops = {
2212 .read = apic_mmio_read,
2213 .write = apic_mmio_write,
2214};
2215
2216static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2217{
2218 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
2219 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
2220
2221 apic_timer_expired(apic);
2222
2223 if (lapic_is_periodic(apic)) {
2224 advance_periodic_target_expiration(apic);
2225 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2226 return HRTIMER_RESTART;
2227 } else
2228 return HRTIMER_NORESTART;
2229}
2230
2231int kvm_create_lapic(struct kvm_vcpu *vcpu)
2232{
2233 struct kvm_lapic *apic;
2234
2235 ASSERT(vcpu != NULL);
2236 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2237
2238 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
2239 if (!apic)
2240 goto nomem;
2241
2242 vcpu->arch.apic = apic;
2243
2244 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
2245 if (!apic->regs) {
2246 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2247 vcpu->vcpu_id);
2248 goto nomem_free_apic;
2249 }
2250 apic->vcpu = vcpu;
2251
2252 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
2253 HRTIMER_MODE_ABS_PINNED);
2254 apic->lapic_timer.timer.function = apic_timer_fn;
2255
2256 /*
2257 * APIC is created enabled. This will prevent kvm_lapic_set_base from
2258 * thinking that APIC satet has changed.
2259 */
2260 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
2261 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
2262 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
2263
2264 return 0;
2265nomem_free_apic:
2266 kfree(apic);
2267nomem:
2268 return -ENOMEM;
2269}
2270
2271int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2272{
2273 struct kvm_lapic *apic = vcpu->arch.apic;
2274 u32 ppr;
2275
2276 if (!apic_enabled(apic))
2277 return -1;
2278
2279 __apic_update_ppr(apic, &ppr);
2280 return apic_has_interrupt_for_ppr(apic, ppr);
2281}
2282
2283int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2284{
2285 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
2286 int r = 0;
2287
2288 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
2289 r = 1;
2290 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2291 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2292 r = 1;
2293 return r;
2294}
2295
2296void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2297{
2298 struct kvm_lapic *apic = vcpu->arch.apic;
2299
2300 if (atomic_read(&apic->lapic_timer.pending) > 0) {
2301 kvm_apic_local_deliver(apic, APIC_LVTT);
2302 if (apic_lvtt_tscdeadline(apic))
2303 apic->lapic_timer.tscdeadline = 0;
2304 if (apic_lvtt_oneshot(apic)) {
2305 apic->lapic_timer.tscdeadline = 0;
2306 apic->lapic_timer.target_expiration = 0;
2307 }
2308 atomic_set(&apic->lapic_timer.pending, 0);
2309 }
2310}
2311
2312int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2313{
2314 int vector = kvm_apic_has_interrupt(vcpu);
2315 struct kvm_lapic *apic = vcpu->arch.apic;
2316 u32 ppr;
2317
2318 if (vector == -1)
2319 return -1;
2320
2321 /*
2322 * We get here even with APIC virtualization enabled, if doing
2323 * nested virtualization and L1 runs with the "acknowledge interrupt
2324 * on exit" mode. Then we cannot inject the interrupt via RVI,
2325 * because the process would deliver it through the IDT.
2326 */
2327
2328 apic_clear_irr(vector, apic);
2329 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
2330 /*
2331 * For auto-EOI interrupts, there might be another pending
2332 * interrupt above PPR, so check whether to raise another
2333 * KVM_REQ_EVENT.
2334 */
2335 apic_update_ppr(apic);
2336 } else {
2337 /*
2338 * For normal interrupts, PPR has been raised and there cannot
2339 * be a higher-priority pending interrupt---except if there was
2340 * a concurrent interrupt injection, but that would have
2341 * triggered KVM_REQ_EVENT already.
2342 */
2343 apic_set_isr(vector, apic);
2344 __apic_update_ppr(apic, &ppr);
2345 }
2346
2347 return vector;
2348}
2349
2350static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2351 struct kvm_lapic_state *s, bool set)
2352{
2353 if (apic_x2apic_mode(vcpu->arch.apic)) {
2354 u32 *id = (u32 *)(s->regs + APIC_ID);
2355 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
2356
2357 if (vcpu->kvm->arch.x2apic_format) {
2358 if (*id != vcpu->vcpu_id)
2359 return -EINVAL;
2360 } else {
2361 if (set)
2362 *id >>= 24;
2363 else
2364 *id <<= 24;
2365 }
2366
2367 /* In x2APIC mode, the LDR is fixed and based on the id */
2368 if (set)
2369 *ldr = kvm_apic_calc_x2apic_ldr(*id);
2370 }
2371
2372 return 0;
2373}
2374
2375int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2376{
2377 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2378 return kvm_apic_state_fixup(vcpu, s, false);
2379}
2380
2381int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2382{
2383 struct kvm_lapic *apic = vcpu->arch.apic;
2384 int r;
2385
2386
2387 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
2388 /* set SPIV separately to get count of SW disabled APICs right */
2389 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
2390
2391 r = kvm_apic_state_fixup(vcpu, s, true);
2392 if (r)
2393 return r;
2394 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
2395
2396 recalculate_apic_map(vcpu->kvm);
2397 kvm_apic_set_version(vcpu);
2398
2399 apic_update_ppr(apic);
2400 hrtimer_cancel(&apic->lapic_timer.timer);
2401 apic_update_lvtt(apic);
2402 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
2403 update_divide_count(apic);
2404 start_apic_timer(apic);
2405 apic->irr_pending = true;
2406 apic->isr_count = vcpu->arch.apicv_active ?
2407 1 : count_vectors(apic->regs + APIC_ISR);
2408 apic->highest_isr_cache = -1;
2409 if (vcpu->arch.apicv_active) {
2410 kvm_x86_ops->apicv_post_state_restore(vcpu);
2411 kvm_x86_ops->hwapic_irr_update(vcpu,
2412 apic_find_highest_irr(apic));
2413 kvm_x86_ops->hwapic_isr_update(vcpu,
2414 apic_find_highest_isr(apic));
2415 }
2416 kvm_make_request(KVM_REQ_EVENT, vcpu);
2417 if (ioapic_in_kernel(vcpu->kvm))
2418 kvm_rtc_eoi_tracking_restore_one(vcpu);
2419
2420 vcpu->arch.apic_arb_prio = 0;
2421
2422 return 0;
2423}
2424
2425void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
2426{
2427 struct hrtimer *timer;
2428
2429 if (!lapic_in_kernel(vcpu))
2430 return;
2431
2432 timer = &vcpu->arch.apic->lapic_timer.timer;
2433 if (hrtimer_cancel(timer))
2434 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
2435}
2436
2437/*
2438 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2439 *
2440 * Detect whether guest triggered PV EOI since the
2441 * last entry. If yes, set EOI on guests's behalf.
2442 * Clear PV EOI in guest memory in any case.
2443 */
2444static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2445 struct kvm_lapic *apic)
2446{
2447 bool pending;
2448 int vector;
2449 /*
2450 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2451 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2452 *
2453 * KVM_APIC_PV_EOI_PENDING is unset:
2454 * -> host disabled PV EOI.
2455 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2456 * -> host enabled PV EOI, guest did not execute EOI yet.
2457 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2458 * -> host enabled PV EOI, guest executed EOI.
2459 */
2460 BUG_ON(!pv_eoi_enabled(vcpu));
2461 pending = pv_eoi_get_pending(vcpu);
2462 /*
2463 * Clear pending bit in any case: it will be set again on vmentry.
2464 * While this might not be ideal from performance point of view,
2465 * this makes sure pv eoi is only enabled when we know it's safe.
2466 */
2467 pv_eoi_clr_pending(vcpu);
2468 if (pending)
2469 return;
2470 vector = apic_set_eoi(apic);
2471 trace_kvm_pv_eoi(apic, vector);
2472}
2473
2474void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2475{
2476 u32 data;
2477
2478 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2479 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2480
2481 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
2482 return;
2483
2484 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2485 sizeof(u32)))
2486 return;
2487
2488 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2489}
2490
2491/*
2492 * apic_sync_pv_eoi_to_guest - called before vmentry
2493 *
2494 * Detect whether it's safe to enable PV EOI and
2495 * if yes do so.
2496 */
2497static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2498 struct kvm_lapic *apic)
2499{
2500 if (!pv_eoi_enabled(vcpu) ||
2501 /* IRR set or many bits in ISR: could be nested. */
2502 apic->irr_pending ||
2503 /* Cache not set: could be safe but we don't bother. */
2504 apic->highest_isr_cache == -1 ||
2505 /* Need EOI to update ioapic. */
2506 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
2507 /*
2508 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2509 * so we need not do anything here.
2510 */
2511 return;
2512 }
2513
2514 pv_eoi_set_pending(apic->vcpu);
2515}
2516
2517void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2518{
2519 u32 data, tpr;
2520 int max_irr, max_isr;
2521 struct kvm_lapic *apic = vcpu->arch.apic;
2522
2523 apic_sync_pv_eoi_to_guest(vcpu, apic);
2524
2525 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
2526 return;
2527
2528 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
2529 max_irr = apic_find_highest_irr(apic);
2530 if (max_irr < 0)
2531 max_irr = 0;
2532 max_isr = apic_find_highest_isr(apic);
2533 if (max_isr < 0)
2534 max_isr = 0;
2535 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2536
2537 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2538 sizeof(u32));
2539}
2540
2541int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
2542{
2543 if (vapic_addr) {
2544 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2545 &vcpu->arch.apic->vapic_cache,
2546 vapic_addr, sizeof(u32)))
2547 return -EINVAL;
2548 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
2549 } else {
2550 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
2551 }
2552
2553 vcpu->arch.apic->vapic_addr = vapic_addr;
2554 return 0;
2555}
2556
2557int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2558{
2559 struct kvm_lapic *apic = vcpu->arch.apic;
2560 u32 reg = (msr - APIC_BASE_MSR) << 4;
2561
2562 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
2563 return 1;
2564
2565 if (reg == APIC_ICR2)
2566 return 1;
2567
2568 /* if this is ICR write vector before command */
2569 if (reg == APIC_ICR)
2570 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2571 return kvm_lapic_reg_write(apic, reg, (u32)data);
2572}
2573
2574int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2575{
2576 struct kvm_lapic *apic = vcpu->arch.apic;
2577 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2578
2579 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
2580 return 1;
2581
2582 if (reg == APIC_DFR || reg == APIC_ICR2) {
2583 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2584 reg);
2585 return 1;
2586 }
2587
2588 if (kvm_lapic_reg_read(apic, reg, 4, &low))
2589 return 1;
2590 if (reg == APIC_ICR)
2591 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
2592
2593 *data = (((u64)high) << 32) | low;
2594
2595 return 0;
2596}
2597
2598int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2599{
2600 struct kvm_lapic *apic = vcpu->arch.apic;
2601
2602 if (!lapic_in_kernel(vcpu))
2603 return 1;
2604
2605 /* if this is ICR write vector before command */
2606 if (reg == APIC_ICR)
2607 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2608 return kvm_lapic_reg_write(apic, reg, (u32)data);
2609}
2610
2611int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2612{
2613 struct kvm_lapic *apic = vcpu->arch.apic;
2614 u32 low, high = 0;
2615
2616 if (!lapic_in_kernel(vcpu))
2617 return 1;
2618
2619 if (kvm_lapic_reg_read(apic, reg, 4, &low))
2620 return 1;
2621 if (reg == APIC_ICR)
2622 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
2623
2624 *data = (((u64)high) << 32) | low;
2625
2626 return 0;
2627}
2628
2629int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2630{
2631 u64 addr = data & ~KVM_MSR_ENABLED;
2632 if (!IS_ALIGNED(addr, 4))
2633 return 1;
2634
2635 vcpu->arch.pv_eoi.msr_val = data;
2636 if (!pv_eoi_enabled(vcpu))
2637 return 0;
2638 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
2639 addr, sizeof(u8));
2640}
2641
2642void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2643{
2644 struct kvm_lapic *apic = vcpu->arch.apic;
2645 u8 sipi_vector;
2646 unsigned long pe;
2647
2648 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
2649 return;
2650
2651 /*
2652 * INITs are latched while in SMM. Because an SMM CPU cannot
2653 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2654 * and delay processing of INIT until the next RSM.
2655 */
2656 if (is_smm(vcpu)) {
2657 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2658 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2659 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2660 return;
2661 }
2662
2663 pe = xchg(&apic->pending_events, 0);
2664 if (test_bit(KVM_APIC_INIT, &pe)) {
2665 kvm_vcpu_reset(vcpu, true);
2666 if (kvm_vcpu_is_bsp(apic->vcpu))
2667 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2668 else
2669 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2670 }
2671 if (test_bit(KVM_APIC_SIPI, &pe) &&
2672 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2673 /* evaluate pending_events before reading the vector */
2674 smp_rmb();
2675 sipi_vector = apic->sipi_vector;
2676 apic_debug("vcpu %d received sipi with vector # %x\n",
2677 vcpu->vcpu_id, sipi_vector);
2678 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2679 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2680 }
2681}
2682
2683void kvm_lapic_init(void)
2684{
2685 /* do not patch jump label more than once per second */
2686 jump_label_rate_limit(&apic_hw_disabled, HZ);
2687 jump_label_rate_limit(&apic_sw_disabled, HZ);
2688}
2689
2690void kvm_lapic_exit(void)
2691{
2692 static_key_deferred_flush(&apic_hw_disabled);
2693 static_key_deferred_flush(&apic_sw_disabled);
2694}