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