blob: 2098dc689088bbd4d53353d68fe6e0fb8a6b47a4 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Eddie Dong97222cc2007-09-12 10:58:04 +03002
3/*
4 * Local APIC virtualization
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2007 Novell
8 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +030010 *
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.
Eddie Dong97222cc2007-09-12 10:58:04 +030017 */
Sean Christopherson8d20bd62022-11-30 23:09:18 +000018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Eddie Dong97222cc2007-09-12 10:58:04 +030019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030021#include <linux/kvm.h>
22#include <linux/mm.h>
23#include <linux/highmem.h>
24#include <linux/smp.h>
25#include <linux/hrtimer.h>
26#include <linux/io.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040027#include <linux/export.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070028#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030030#include <asm/processor.h>
Jue Wang4b903562022-06-10 10:11:30 -070031#include <asm/mce.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030032#include <asm/msr.h>
33#include <asm/page.h>
34#include <asm/current.h>
35#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050036#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030038#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030040#include "irq.h"
彭浩(Richard)88197e62020-05-21 05:57:49 +000041#include "ioapic.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030042#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030043#include "x86.h"
David Woodhouse8e62bf2b2024-02-27 11:49:16 +000044#include "xen.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020045#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030046#include "hyperv.h"
Paolo Bonzinib0b42192022-09-29 13:20:09 -040047#include "smm.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030048
Marcelo Tosattib682b812009-02-10 20:41:41 -020049#ifndef CONFIG_X86_64
50#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
51#else
52#define mod_64(x, y) ((x) % (y))
53#endif
54
Eddie Dong97222cc2007-09-12 10:58:04 +030055/* 14 is the version for Xeon and Pentium 8.4.8*/
Jue Wang951ceb92022-06-10 10:11:27 -070056#define APIC_VERSION 0x14UL
Eddie Dong97222cc2007-09-12 10:58:04 +030057#define LAPIC_MMIO_LENGTH (1 << 12)
58/* followed define is not in apicdef.h */
Eddie Dong97222cc2007-09-12 10:58:04 +030059#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090060#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030061
Sean Christopherson89a58812024-05-21 18:03:04 -070062/*
63 * Enable local APIC timer advancement (tscdeadline mode only) with adaptive
64 * tuning. When enabled, KVM programs the host timer event to fire early, i.e.
65 * before the deadline expires, to account for the delay between taking the
66 * VM-Exit (to inject the guest event) and the subsequent VM-Enter to resume
67 * the guest, i.e. so that the interrupt arrives in the guest with minimal
68 * latency relative to the deadline programmed by the guest.
69 */
70static bool lapic_timer_advance __read_mostly = true;
71module_param(lapic_timer_advance, bool, 0444);
72
Wanpeng Lia0f00372019-09-26 08:54:03 +080073#define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
74#define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
75#define LAPIC_TIMER_ADVANCE_NS_INIT 1000
76#define LAPIC_TIMER_ADVANCE_NS_MAX 5000
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080077/* step-by-step approximation to mitigate fluctuation */
78#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
Zeng Guang5413bcb2022-04-19 23:35:16 +080079static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data);
Suravee Suthikulpanit1bd9dfe2022-07-25 00:33:56 -050080static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data);
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080081
Sean Christophersonb9964ee2022-02-04 21:42:04 +000082static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val)
83{
84 *((u32 *) (regs + reg_off)) = val;
85}
86
87static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
88{
89 __kvm_lapic_set_reg(apic->regs, reg_off, val);
90}
91
Sean Christophersona57a3162022-02-04 21:42:03 +000092static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg)
93{
94 BUILD_BUG_ON(reg != APIC_ICR);
95 return *((u64 *) (regs + reg));
96}
97
98static __always_inline u64 kvm_lapic_get_reg64(struct kvm_lapic *apic, int reg)
99{
100 return __kvm_lapic_get_reg64(apic->regs, reg);
101}
102
103static __always_inline void __kvm_lapic_set_reg64(char *regs, int reg, u64 val)
104{
105 BUILD_BUG_ON(reg != APIC_ICR);
106 *((u64 *) (regs + reg)) = val;
107}
108
109static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,
110 int reg, u64 val)
111{
112 __kvm_lapic_set_reg64(apic->regs, reg, val);
113}
114
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +0300115static inline int apic_test_vector(int vec, void *bitmap)
116{
117 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
118}
119
Yang Zhang10606912013-04-11 19:21:38 +0800120bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
121{
122 struct kvm_lapic *apic = vcpu->arch.apic;
123
124 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
125 apic_test_vector(vector, apic->regs + APIC_IRR);
126}
127
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300128static inline int __apic_test_and_set_vector(int vec, void *bitmap)
129{
130 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
131}
132
133static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
134{
135 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
136}
137
Sean Christophersona78d9042024-02-09 14:20:46 -0800138__read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
139EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu);
140
Cun Li6e4e3b42021-01-11 23:24:35 +0800141__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_hw_disabled, HZ);
142__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_sw_disabled, HZ);
Gleb Natapovf8c1ea102012-08-05 15:58:31 +0300143
Eddie Dong97222cc2007-09-12 10:58:04 +0300144static inline int apic_enabled(struct kvm_lapic *apic)
145{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300146 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300147}
148
Eddie Dong97222cc2007-09-12 10:58:04 +0300149#define LVT_MASK \
150 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
151
152#define LINT_MASK \
153 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
154 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
155
Radim Krčmář6e500432016-12-15 18:06:46 +0100156static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
157{
158 return apic->vcpu->vcpu_id;
159}
160
Paolo Bonzini199a8b82020-05-05 06:45:35 -0400161static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800162{
Wanpeng Li1714a4e2022-01-25 04:08:58 -0800163 return pi_inject_timer && kvm_vcpu_apicv_active(vcpu) &&
164 (kvm_mwait_in_guest(vcpu->kvm) || kvm_hlt_in_guest(vcpu->kvm));
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800165}
Paolo Bonzini199a8b82020-05-05 06:45:35 -0400166
167bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu)
168{
169 return kvm_x86_ops.set_hv_timer
170 && !(kvm_mwait_in_guest(vcpu->kvm) ||
171 kvm_can_post_timer_interrupt(vcpu));
172}
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800173
174static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
175{
176 return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
177}
178
Sean Christopherson76e52752023-01-06 01:12:52 +0000179static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
180{
181 return ((id >> 4) << 16) | (1 << (id & 0xf));
182}
183
Radim Krčmáře45115b2016-07-12 22:09:19 +0200184static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
185 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
Sean Christopherson353669012023-01-06 01:12:51 +0000186 switch (map->logical_mode) {
187 case KVM_APIC_MODE_SW_DISABLED:
188 /* Arbitrarily use the flat map so that @cluster isn't NULL. */
189 *cluster = map->xapic_flat_map;
190 *mask = 0;
191 return true;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200192 case KVM_APIC_MODE_X2APIC: {
193 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200194 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100195
Radim Krčmáře45115b2016-07-12 22:09:19 +0200196 if (offset <= max_apic_id) {
197 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100198
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200199 offset = array_index_nospec(offset, map->max_apic_id + 1);
Radim Krčmáře45115b2016-07-12 22:09:19 +0200200 *cluster = &map->phys_map[offset];
201 *mask = dest_id & (0xffff >> (16 - cluster_size));
202 } else {
203 *mask = 0;
204 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100205
Radim Krčmáře45115b2016-07-12 22:09:19 +0200206 return true;
207 }
208 case KVM_APIC_MODE_XAPIC_FLAT:
209 *cluster = map->xapic_flat_map;
210 *mask = dest_id & 0xff;
211 return true;
212 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100213 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200214 *mask = dest_id & 0xf;
215 return true;
Sean Christopherson353669012023-01-06 01:12:51 +0000216 case KVM_APIC_MODE_MAP_DISABLED:
217 return false;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200218 default:
Sean Christopherson353669012023-01-06 01:12:51 +0000219 WARN_ON_ONCE(1);
Radim Krčmáře45115b2016-07-12 22:09:19 +0200220 return false;
221 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300222}
223
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200224static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100225{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200226 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100227
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200228 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100229}
230
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000231static int kvm_recalculate_phys_map(struct kvm_apic_map *new,
232 struct kvm_vcpu *vcpu,
233 bool *xapic_id_mismatch)
234{
235 struct kvm_lapic *apic = vcpu->arch.apic;
236 u32 x2apic_id = kvm_x2apic_id(apic);
237 u32 xapic_id = kvm_xapic_id(apic);
238 u32 physical_id;
239
240 /*
Sean Christopherson4364b282023-06-02 16:32:48 -0700241 * For simplicity, KVM always allocates enough space for all possible
242 * xAPIC IDs. Yell, but don't kill the VM, as KVM can continue on
243 * without the optimized map.
244 */
245 if (WARN_ON_ONCE(xapic_id > new->max_apic_id))
246 return -EINVAL;
247
248 /*
249 * Bail if a vCPU was added and/or enabled its APIC between allocating
250 * the map and doing the actual calculations for the map. Note, KVM
251 * hardcodes the x2APIC ID to vcpu_id, i.e. there's no TOCTOU bug if
252 * the compiler decides to reload x2apic_id after this check.
253 */
254 if (x2apic_id > new->max_apic_id)
255 return -E2BIG;
256
257 /*
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000258 * Deliberately truncate the vCPU ID when detecting a mismatched APIC
259 * ID to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a
260 * 32-bit value. Any unwanted aliasing due to truncation results will
261 * be detected below.
262 */
263 if (!apic_x2apic_mode(apic) && xapic_id != (u8)vcpu->vcpu_id)
264 *xapic_id_mismatch = true;
265
266 /*
267 * Apply KVM's hotplug hack if userspace has enable 32-bit APIC IDs.
268 * Allow sending events to vCPUs by their x2APIC ID even if the target
269 * vCPU is in legacy xAPIC mode, and silently ignore aliased xAPIC IDs
270 * (the x2APIC ID is truncated to 8 bits, causing IDs > 0xff to wrap
271 * and collide).
272 *
273 * Honor the architectural (and KVM's non-optimized) behavior if
274 * userspace has not enabled 32-bit x2APIC IDs. Each APIC is supposed
275 * to process messages independently. If multiple vCPUs have the same
276 * effective APIC ID, e.g. due to the x2APIC wrap or because the guest
277 * manually modified its xAPIC IDs, events targeting that ID are
278 * supposed to be recognized by all vCPUs with said ID.
279 */
280 if (vcpu->kvm->arch.x2apic_format) {
281 /* See also kvm_apic_match_physical_addr(). */
Sean Christopherson4364b282023-06-02 16:32:48 -0700282 if (apic_x2apic_mode(apic) || x2apic_id > 0xff)
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000283 new->phys_map[x2apic_id] = apic;
284
285 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
286 new->phys_map[xapic_id] = apic;
287 } else {
288 /*
289 * Disable the optimized map if the physical APIC ID is already
290 * mapped, i.e. is aliased to multiple vCPUs. The optimized
291 * map requires a strict 1:1 mapping between IDs and vCPUs.
292 */
293 if (apic_x2apic_mode(apic))
294 physical_id = x2apic_id;
295 else
296 physical_id = xapic_id;
297
298 if (new->phys_map[physical_id])
299 return -EINVAL;
300
301 new->phys_map[physical_id] = apic;
302 }
303
304 return 0;
305}
306
307static void kvm_recalculate_logical_map(struct kvm_apic_map *new,
308 struct kvm_vcpu *vcpu)
309{
310 struct kvm_lapic *apic = vcpu->arch.apic;
311 enum kvm_apic_logical_mode logical_mode;
312 struct kvm_lapic **cluster;
313 u16 mask;
314 u32 ldr;
315
316 if (new->logical_mode == KVM_APIC_MODE_MAP_DISABLED)
317 return;
318
319 if (!kvm_apic_sw_enabled(apic))
320 return;
321
322 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
323 if (!ldr)
324 return;
325
326 if (apic_x2apic_mode(apic)) {
327 logical_mode = KVM_APIC_MODE_X2APIC;
328 } else {
329 ldr = GET_APIC_LOGICAL_ID(ldr);
330 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
331 logical_mode = KVM_APIC_MODE_XAPIC_FLAT;
332 else
333 logical_mode = KVM_APIC_MODE_XAPIC_CLUSTER;
334 }
335
336 /*
337 * To optimize logical mode delivery, all software-enabled APICs must
338 * be configured for the same mode.
339 */
340 if (new->logical_mode == KVM_APIC_MODE_SW_DISABLED) {
341 new->logical_mode = logical_mode;
342 } else if (new->logical_mode != logical_mode) {
343 new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
344 return;
345 }
346
347 /*
348 * In x2APIC mode, the LDR is read-only and derived directly from the
349 * x2APIC ID, thus is guaranteed to be addressable. KVM reuses
350 * kvm_apic_map.phys_map to optimize logical mode x2APIC interrupts by
351 * reversing the LDR calculation to get cluster of APICs, i.e. no
352 * additional work is required.
353 */
Sean Christopherson4b7c3f62024-08-02 13:29:40 -0700354 if (apic_x2apic_mode(apic))
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000355 return;
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000356
357 if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr,
358 &cluster, &mask))) {
359 new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
360 return;
361 }
362
363 if (!mask)
364 return;
365
366 ldr = ffs(mask) - 1;
367 if (!is_power_of_2(mask) || cluster[ldr])
368 new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
369 else
370 cluster[ldr] = apic;
371}
372
Paolo Bonzini44d52712020-06-22 16:37:42 +0200373/*
374 * CLEAN -> DIRTY and UPDATE_IN_PROGRESS -> DIRTY changes happen without a lock.
375 *
376 * DIRTY -> UPDATE_IN_PROGRESS and UPDATE_IN_PROGRESS -> CLEAN happen with
377 * apic_map_lock_held.
378 */
379enum {
380 CLEAN,
381 UPDATE_IN_PROGRESS,
382 DIRTY
383};
384
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800385void kvm_recalculate_apic_map(struct kvm *kvm)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300386{
387 struct kvm_apic_map *new, *old = NULL;
388 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +0000389 unsigned long i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100390 u32 max_id = 255; /* enough space for any xAPIC ID */
Sean Christopherson41e90a62023-06-02 16:32:49 -0700391 bool xapic_id_mismatch;
392 int r;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300393
Paolo Bonzini44d52712020-06-22 16:37:42 +0200394 /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
395 if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN)
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800396 return;
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800397
Sean Christophersonc2f79a62021-07-13 09:32:49 -0700398 WARN_ONCE(!irqchip_in_kernel(kvm),
399 "Dirty APIC map without an in-kernel local APIC");
400
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300401 mutex_lock(&kvm->arch.apic_map_lock);
Sean Christopherson41e90a62023-06-02 16:32:49 -0700402
403retry:
Paolo Bonzini44d52712020-06-22 16:37:42 +0200404 /*
Sean Christopherson41e90a62023-06-02 16:32:49 -0700405 * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean)
406 * or the APIC registers (if dirty). Note, on retry the map may have
407 * not yet been marked dirty by whatever task changed a vCPU's x2APIC
408 * ID, i.e. the map may still show up as in-progress. In that case
409 * this task still needs to retry and complete its calculation.
Paolo Bonzini44d52712020-06-22 16:37:42 +0200410 */
411 if (atomic_cmpxchg_acquire(&kvm->arch.apic_map_dirty,
412 DIRTY, UPDATE_IN_PROGRESS) == CLEAN) {
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800413 /* Someone else has updated the map. */
414 mutex_unlock(&kvm->arch.apic_map_lock);
415 return;
416 }
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300417
Sean Christopherson41e90a62023-06-02 16:32:49 -0700418 /*
419 * Reset the mismatch flag between attempts so that KVM does the right
420 * thing if a vCPU changes its xAPIC ID, but do NOT reset max_id, i.e.
421 * keep max_id strictly increasing. Disallowing max_id from shrinking
422 * ensures KVM won't get stuck in an infinite loop, e.g. if the vCPU
423 * with the highest x2APIC ID is toggling its APIC on and off.
424 */
425 xapic_id_mismatch = false;
426
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200427 kvm_for_each_vcpu(i, vcpu, kvm)
428 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100429 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200430
Michal Hockoa7c3e902017-05-08 15:57:09 -0700431 new = kvzalloc(sizeof(struct kvm_apic_map) +
Ben Gardon254272c2019-02-11 11:02:50 -0800432 sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
433 GFP_KERNEL_ACCOUNT);
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200434
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300435 if (!new)
436 goto out;
437
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200438 new->max_apic_id = max_id;
Sean Christopherson353669012023-01-06 01:12:51 +0000439 new->logical_mode = KVM_APIC_MODE_SW_DISABLED;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200440
Nadav Amit173beed2014-11-02 11:54:54 +0200441 kvm_for_each_vcpu(i, vcpu, kvm) {
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100442 if (!kvm_apic_present(vcpu))
443 continue;
444
Sean Christopherson41e90a62023-06-02 16:32:49 -0700445 r = kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch);
446 if (r) {
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000447 kvfree(new);
448 new = NULL;
Sean Christopherson41e90a62023-06-02 16:32:49 -0700449 if (r == -E2BIG) {
450 cond_resched();
451 goto retry;
452 }
453
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000454 goto out;
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100455 }
456
Sean Christopherson72c70ce2023-01-06 01:13:06 +0000457 kvm_recalculate_logical_map(new, vcpu);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300458 }
459out:
Sean Christopherson5063c412023-01-06 01:12:56 +0000460 /*
461 * The optimized map is effectively KVM's internal version of APICv,
462 * and all unwanted aliasing that results in disabling the optimized
463 * map also applies to APICv.
464 */
465 if (!new)
466 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED);
467 else
468 kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED);
469
Sean Christopherson9a364852023-01-06 01:12:57 +0000470 if (!new || new->logical_mode == KVM_APIC_MODE_MAP_DISABLED)
471 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED);
472 else
473 kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED);
474
Greg Edwardsd471bd82023-01-06 01:13:05 +0000475 if (xapic_id_mismatch)
476 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
477 else
478 kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
479
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300480 old = rcu_dereference_protected(kvm->arch.apic_map,
481 lockdep_is_held(&kvm->arch.apic_map_lock));
482 rcu_assign_pointer(kvm->arch.apic_map, new);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800483 /*
Paolo Bonzini44d52712020-06-22 16:37:42 +0200484 * Write kvm->arch.apic_map before clearing apic->apic_map_dirty.
485 * If another update has come in, leave it DIRTY.
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800486 */
Paolo Bonzini44d52712020-06-22 16:37:42 +0200487 atomic_cmpxchg_release(&kvm->arch.apic_map_dirty,
488 UPDATE_IN_PROGRESS, CLEAN);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300489 mutex_unlock(&kvm->arch.apic_map_lock);
490
491 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200492 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800493
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700494 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300495}
496
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300497static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
498{
Radim Krčmáře4627552014-10-30 15:06:45 +0100499 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300500
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500501 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100502
503 if (enabled != apic->sw_enabled) {
504 apic->sw_enabled = enabled;
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800505 if (enabled)
Cun Li6e4e3b42021-01-11 23:24:35 +0800506 static_branch_slow_dec_deferred(&apic_sw_disabled);
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800507 else
Cun Li6e4e3b42021-01-11 23:24:35 +0800508 static_branch_inc(&apic_sw_disabled.key);
Radim Krcmarb14c8762019-08-13 23:37:37 -0400509
Paolo Bonzini44d52712020-06-22 16:37:42 +0200510 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300511 }
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +0200512
513 /* Check if there are APF page ready requests pending */
David Woodhouse8e62bf2b2024-02-27 11:49:16 +0000514 if (enabled) {
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +0200515 kvm_make_request(KVM_REQ_APF_READY, apic->vcpu);
David Woodhouse8e62bf2b2024-02-27 11:49:16 +0000516 kvm_xen_sw_enable_lapic(apic->vcpu);
517 }
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300518}
519
Radim Krčmářa92e2542016-07-12 22:09:22 +0200520static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300521{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500522 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Paolo Bonzini44d52712020-06-22 16:37:42 +0200523 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300524}
525
526static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
527{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500528 kvm_lapic_set_reg(apic, APIC_LDR, id);
Paolo Bonzini44d52712020-06-22 16:37:42 +0200529 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300530}
531
Wanpeng Liae6f2492020-08-19 16:55:26 +0800532static inline void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val)
533{
534 kvm_lapic_set_reg(apic, APIC_DFR, val);
535 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
536}
537
Radim Krčmářa92e2542016-07-12 22:09:22 +0200538static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200539{
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000540 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200541
Radim Krčmář6e500432016-12-15 18:06:46 +0100542 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
543
Radim Krčmářa92e2542016-07-12 22:09:22 +0200544 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500545 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Paolo Bonzini44d52712020-06-22 16:37:42 +0200546 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200547}
548
Eddie Dong97222cc2007-09-12 10:58:04 +0300549static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
550{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500551 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300552}
553
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800554static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
555{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100556 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800557}
558
Eddie Dong97222cc2007-09-12 10:58:04 +0300559static inline int apic_lvtt_period(struct kvm_lapic *apic)
560{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100561 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800562}
563
564static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
565{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100566 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300567}
568
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200569static inline int apic_lvt_nmi_mode(u32 lvt_val)
570{
571 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
572}
573
Jue Wang4b903562022-06-10 10:11:30 -0700574static inline bool kvm_lapic_lvt_supported(struct kvm_lapic *apic, int lvt_index)
575{
576 return apic->nr_lvt_entries > lvt_index;
577}
578
Sean Christopherson03d84f92022-07-08 15:38:51 -0700579static inline int kvm_apic_calc_nr_lvt_entries(struct kvm_vcpu *vcpu)
580{
581 return KVM_APIC_MAX_NR_LVT_ENTRIES - !(vcpu->arch.mcg_cap & MCG_CMCI_P);
582}
583
Gleb Natapovfc61b802009-07-05 17:39:35 +0300584void kvm_apic_set_version(struct kvm_vcpu *vcpu)
585{
586 struct kvm_lapic *apic = vcpu->arch.apic;
Jue Wang4b903562022-06-10 10:11:30 -0700587 u32 v = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +0300588
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100589 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300590 return;
591
Jue Wang4b903562022-06-10 10:11:30 -0700592 v = APIC_VERSION | ((apic->nr_lvt_entries - 1) << 16);
593
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100594 /*
595 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
596 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
597 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
598 * version first and level-triggered interrupts never get EOIed in
599 * IOAPIC.
600 */
Xiaoyao Li565b7822020-07-08 14:50:53 +0800601 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) &&
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100602 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300603 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500604 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300605}
606
Sean Christophersonf83894b2022-07-08 15:48:10 -0700607void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu)
608{
609 int nr_lvt_entries = kvm_apic_calc_nr_lvt_entries(vcpu);
610 struct kvm_lapic *apic = vcpu->arch.apic;
611 int i;
612
613 if (!lapic_in_kernel(vcpu) || nr_lvt_entries == apic->nr_lvt_entries)
614 return;
615
616 /* Initialize/mask any "new" LVT entries. */
617 for (i = apic->nr_lvt_entries; i < nr_lvt_entries; i++)
618 kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
619
620 apic->nr_lvt_entries = nr_lvt_entries;
621
622 /* The number of LVT entries is reflected in the version register. */
623 kvm_apic_set_version(vcpu);
624}
625
Jue Wang1d8c6812022-06-10 10:11:28 -0700626static const unsigned int apic_lvt_mask[KVM_APIC_MAX_NR_LVT_ENTRIES] = {
627 [LVT_TIMER] = LVT_MASK, /* timer mode mask added at runtime */
628 [LVT_THERMAL_MONITOR] = LVT_MASK | APIC_MODE_MASK,
629 [LVT_PERFORMANCE_COUNTER] = LVT_MASK | APIC_MODE_MASK,
630 [LVT_LINT0] = LINT_MASK,
631 [LVT_LINT1] = LINT_MASK,
Jue Wang4b903562022-06-10 10:11:30 -0700632 [LVT_ERROR] = LVT_MASK,
633 [LVT_CMCI] = LVT_MASK | APIC_MODE_MASK
Eddie Dong97222cc2007-09-12 10:58:04 +0300634};
635
636static int find_highest_vector(void *bitmap)
637{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900638 int vec;
639 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300640
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900641 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
642 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
643 reg = bitmap + REG_POS(vec);
644 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100645 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900646 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300647
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900648 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300649}
650
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300651static u8 count_vectors(void *bitmap)
652{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900653 int vec;
654 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300655 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900656
657 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
658 reg = bitmap + REG_POS(vec);
659 count += hweight32(*reg);
660 }
661
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300662 return count;
663}
664
Liran Alone7387b02017-12-24 18:12:54 +0200665bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Yang Zhanga20ed542013-04-11 19:25:15 +0800666{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100667 u32 i, vec;
Liran Alone7387b02017-12-24 18:12:54 +0200668 u32 pir_val, irr_val, prev_irr_val;
669 int max_updated_irr;
670
671 max_updated_irr = -1;
672 *max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800673
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100674 for (i = vec = 0; i <= 7; i++, vec += 32) {
Maxim Levitsky514946d2023-07-26 16:59:43 +0300675 u32 *p_irr = (u32 *)(regs + APIC_IRR + i * 0x10);
676
677 irr_val = *p_irr;
Paolo Bonziniad361092016-09-20 16:15:05 +0200678 pir_val = READ_ONCE(pir[i]);
Maxim Levitsky514946d2023-07-26 16:59:43 +0300679
Paolo Bonziniad361092016-09-20 16:15:05 +0200680 if (pir_val) {
Maxim Levitsky514946d2023-07-26 16:59:43 +0300681 pir_val = xchg(&pir[i], 0);
682
Liran Alone7387b02017-12-24 18:12:54 +0200683 prev_irr_val = irr_val;
Maxim Levitsky514946d2023-07-26 16:59:43 +0300684 do {
685 irr_val = prev_irr_val | pir_val;
686 } while (prev_irr_val != irr_val &&
687 !try_cmpxchg(p_irr, &prev_irr_val, irr_val));
688
689 if (prev_irr_val != irr_val)
690 max_updated_irr = __fls(irr_val ^ prev_irr_val) + vec;
Paolo Bonziniad361092016-09-20 16:15:05 +0200691 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100692 if (irr_val)
Liran Alone7387b02017-12-24 18:12:54 +0200693 *max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800694 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100695
Liran Alone7387b02017-12-24 18:12:54 +0200696 return ((max_updated_irr != -1) &&
697 (max_updated_irr == *max_irr));
Yang Zhanga20ed542013-04-11 19:25:15 +0800698}
Wincy Van705699a2015-02-03 23:58:17 +0800699EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
700
Liran Alone7387b02017-12-24 18:12:54 +0200701bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
Wincy Van705699a2015-02-03 23:58:17 +0800702{
703 struct kvm_lapic *apic = vcpu->arch.apic;
Maxim Levitskycff540e2023-07-26 16:59:44 +0300704 bool irr_updated = __kvm_apic_update_irr(pir, apic->regs, max_irr);
Wincy Van705699a2015-02-03 23:58:17 +0800705
Maxim Levitskycff540e2023-07-26 16:59:44 +0300706 if (unlikely(!apic->apicv_active && irr_updated))
707 apic->irr_pending = true;
708 return irr_updated;
Wincy Van705699a2015-02-03 23:58:17 +0800709}
Yang Zhanga20ed542013-04-11 19:25:15 +0800710EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
711
Gleb Natapov33e4c682009-06-11 11:06:51 +0300712static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300713{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300714 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300715}
716
717static inline int apic_find_highest_irr(struct kvm_lapic *apic)
718{
719 int result;
720
Yang Zhangc7c9c562013-01-25 10:18:51 +0800721 /*
722 * Note that irr_pending is just a hint. It will be always
723 * true with virtual interrupt delivery enabled.
724 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300725 if (!apic->irr_pending)
726 return -1;
727
728 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300729 ASSERT(result == -1 || result >= 16);
730
731 return result;
732}
733
Gleb Natapov33e4c682009-06-11 11:06:51 +0300734static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
735{
Sean Christophersonce0a58f42022-06-14 23:05:47 +0000736 if (unlikely(apic->apicv_active)) {
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100737 /* need to update RVI */
Wei Yangee171d22019-03-31 19:17:22 -0700738 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Wei Wang89604642024-05-07 21:31:02 +0800739 kvm_x86_call(hwapic_irr_update)(apic->vcpu,
740 apic_find_highest_irr(apic));
Nadav Amitf210f752014-11-16 23:49:07 +0200741 } else {
742 apic->irr_pending = false;
Wei Yangee171d22019-03-31 19:17:22 -0700743 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Nadav Amitf210f752014-11-16 23:49:07 +0200744 if (apic_search_irr(apic) != -1)
745 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800746 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300747}
748
Sean Christopherson25bb2cf2020-08-12 10:51:29 -0700749void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec)
750{
751 apic_clear_irr(vec, vcpu->arch.apic);
752}
753EXPORT_SYMBOL_GPL(kvm_apic_clear_irr);
754
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300755static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
756{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800757 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
758 return;
759
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300760 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800761 * With APIC virtualization enabled, all caching is disabled
762 * because the processor can modify ISR under the hood. Instead
763 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300764 */
Sean Christophersonce0a58f42022-06-14 23:05:47 +0000765 if (unlikely(apic->apicv_active))
Wei Wang89604642024-05-07 21:31:02 +0800766 kvm_x86_call(hwapic_isr_update)(vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800767 else {
768 ++apic->isr_count;
769 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
770 /*
771 * ISR (in service register) bit is set when injecting an interrupt.
772 * The highest vector is injected. Thus the latest bit set matches
773 * the highest bit in ISR.
774 */
775 apic->highest_isr_cache = vec;
776 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300777}
778
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200779static inline int apic_find_highest_isr(struct kvm_lapic *apic)
780{
781 int result;
782
783 /*
784 * Note that isr_count is always 1, and highest_isr_cache
785 * is always -1, with APIC virtualization enabled.
786 */
787 if (!apic->isr_count)
788 return -1;
789 if (likely(apic->highest_isr_cache != -1))
790 return apic->highest_isr_cache;
791
792 result = find_highest_vector(apic->regs + APIC_ISR);
793 ASSERT(result == -1 || result >= 16);
794
795 return result;
796}
797
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300798static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
799{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200800 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
801 return;
802
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200803 /*
804 * We do get here for APIC virtualization enabled if the guest
805 * uses the Hyper-V APIC enlightenment. In this case we may need
806 * to trigger a new interrupt delivery by writing the SVI field;
807 * on the other hand isr_count and highest_isr_cache are unused
808 * and must be left alone.
809 */
Sean Christophersonce0a58f42022-06-14 23:05:47 +0000810 if (unlikely(apic->apicv_active))
Wei Wang89604642024-05-07 21:31:02 +0800811 kvm_x86_call(hwapic_isr_update)(apic_find_highest_isr(apic));
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200812 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300813 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200814 BUG_ON(apic->isr_count < 0);
815 apic->highest_isr_cache = -1;
816 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300817}
818
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800819int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
820{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300821 /* This may race with setting of irr in __apic_accept_irq() and
822 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
823 * will cause vmexit immediately and the value will be recalculated
824 * on the next vmentry.
825 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100826 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800827}
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100828EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800829
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200830static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800831 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100832 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200833
Yang Zhangb4f22252013-04-11 19:21:37 +0800834int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100835 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300836{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800837 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800838
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200839 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800840 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300841}
842
Miaohe Lin1a686232019-11-09 17:46:49 +0800843static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
844 struct kvm_lapic_irq *irq, u32 min)
845{
846 int i, count = 0;
847 struct kvm_vcpu *vcpu;
848
849 if (min > map->max_apic_id)
850 return 0;
851
852 for_each_set_bit(i, ipi_bitmap,
853 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
854 if (map->phys_map[min + i]) {
855 vcpu = map->phys_map[min + i]->vcpu;
856 count += kvm_apic_set_irq(vcpu, irq, NULL);
857 }
858 }
859
860 return count;
861}
862
Wanpeng Li4180bf12018-07-23 14:39:54 +0800863int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800864 unsigned long ipi_bitmap_high, u32 min,
Wanpeng Li4180bf12018-07-23 14:39:54 +0800865 unsigned long icr, int op_64_bit)
866{
Wanpeng Li4180bf12018-07-23 14:39:54 +0800867 struct kvm_apic_map *map;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800868 struct kvm_lapic_irq irq = {0};
869 int cluster_size = op_64_bit ? 64 : 32;
Miaohe Lin1a686232019-11-09 17:46:49 +0800870 int count;
871
872 if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK))
873 return -KVM_EINVAL;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800874
875 irq.vector = icr & APIC_VECTOR_MASK;
876 irq.delivery_mode = icr & APIC_MODE_MASK;
877 irq.level = (icr & APIC_INT_ASSERT) != 0;
878 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
879
Wanpeng Li4180bf12018-07-23 14:39:54 +0800880 rcu_read_lock();
881 map = rcu_dereference(kvm->arch.apic_map);
882
Miaohe Lin1a686232019-11-09 17:46:49 +0800883 count = -EOPNOTSUPP;
884 if (likely(map)) {
885 count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min);
886 min += cluster_size;
887 count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min);
Wanpeng Li38ab0122018-11-20 09:39:30 +0800888 }
889
Wanpeng Li4180bf12018-07-23 14:39:54 +0800890 rcu_read_unlock();
891 return count;
892}
893
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300894static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
895{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200896
897 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
898 sizeof(val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300899}
900
901static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
902{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200903
904 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
905 sizeof(*val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300906}
907
908static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
909{
910 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
911}
912
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300913static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
914{
Li RongQingce5977b2021-11-04 19:56:13 +0800915 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300916 return;
Li RongQingce5977b2021-11-04 19:56:13 +0800917
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300918 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
919}
920
Li RongQing51b12092021-11-04 19:56:14 +0800921static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu *vcpu)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300922{
Li RongQing51b12092021-11-04 19:56:14 +0800923 u8 val;
Li RongQingce5977b2021-11-04 19:56:13 +0800924
Li RongQing51b12092021-11-04 19:56:14 +0800925 if (pv_eoi_get_user(vcpu, &val) < 0)
926 return false;
927
928 val &= KVM_PV_EOI_ENABLED;
929
930 if (val && pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
931 return false;
932
933 /*
934 * Clear pending bit in any case: it will be set again on vmentry.
935 * While this might not be ideal from performance point of view,
936 * this makes sure pv eoi is only enabled when we know it's safe.
937 */
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300938 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
Li RongQing51b12092021-11-04 19:56:14 +0800939
940 return val;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300941}
942
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100943static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
944{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100945 int highest_irr;
Paolo Bonzini37c4dbf2021-11-22 19:43:10 -0500946 if (kvm_x86_ops.sync_pir_to_irr)
Wei Wang89604642024-05-07 21:31:02 +0800947 highest_irr = kvm_x86_call(sync_pir_to_irr)(apic->vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100948 else
949 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100950 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
951 return -1;
952 return highest_irr;
953}
954
955static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300956{
Avi Kivity3842d132010-07-27 12:30:24 +0300957 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300958 int isr;
959
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500960 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
961 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300962 isr = apic_find_highest_isr(apic);
963 isrv = (isr != -1) ? isr : 0;
964
965 if ((tpr & 0xf0) >= (isrv & 0xf0))
966 ppr = tpr & 0xff;
967 else
968 ppr = isrv & 0xf0;
969
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100970 *new_ppr = ppr;
971 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500972 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100973
974 return ppr < old_ppr;
975}
976
977static void apic_update_ppr(struct kvm_lapic *apic)
978{
979 u32 ppr;
980
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100981 if (__apic_update_ppr(apic, &ppr) &&
982 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100983 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300984}
985
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100986void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
987{
988 apic_update_ppr(vcpu->arch.apic);
989}
990EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
991
Eddie Dong97222cc2007-09-12 10:58:04 +0300992static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
993{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500994 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300995 apic_update_ppr(apic);
996}
997
Radim Krčmář03d22492015-02-12 19:41:31 +0100998static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300999{
Radim Krčmářb4535b52016-12-15 18:06:47 +01001000 return mda == (apic_x2apic_mode(apic) ?
1001 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +03001002}
1003
Radim Krčmář03d22492015-02-12 19:41:31 +01001004static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +03001005{
Radim Krčmář03d22492015-02-12 19:41:31 +01001006 if (kvm_apic_broadcast(apic, mda))
1007 return true;
1008
Radim Krčmář5bd5db32016-12-15 18:06:48 +01001009 /*
Sean Christopherson8031d872022-07-12 11:46:53 -07001010 * Hotplug hack: Accept interrupts for vCPUs in xAPIC mode as if they
1011 * were in x2APIC mode if the target APIC ID can't be encoded as an
1012 * xAPIC ID. This allows unique addressing of hotplugged vCPUs (which
1013 * start in xAPIC mode) with an APIC ID that is unaddressable in xAPIC
1014 * mode. Match the x2APIC ID if and only if the target APIC ID can't
1015 * be encoded in xAPIC to avoid spurious matches against a vCPU that
1016 * changed its (addressable) xAPIC ID (which is writable).
Radim Krčmář5bd5db32016-12-15 18:06:48 +01001017 */
Sean Christopherson8031d872022-07-12 11:46:53 -07001018 if (apic_x2apic_mode(apic) || mda > 0xff)
1019 return mda == kvm_x2apic_id(apic);
Radim Krčmář5bd5db32016-12-15 18:06:48 +01001020
Radim Krčmářb4535b52016-12-15 18:06:47 +01001021 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +03001022}
1023
Radim Krčmář52c233a2015-01-29 22:48:48 +01001024static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +03001025{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001026 u32 logical_id;
1027
Nadav Amit394457a2014-10-03 00:30:52 +03001028 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +01001029 return true;
Nadav Amit394457a2014-10-03 00:30:52 +03001030
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001031 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001032
Radim Krčmář9368b562015-01-29 22:48:49 +01001033 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +01001034 return ((logical_id >> 16) == (mda >> 16))
1035 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +01001036
1037 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +03001038
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001039 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001040 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +01001041 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001042 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +01001043 return ((logical_id >> 4) == (mda >> 4))
1044 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001045 default:
Radim Krčmář9368b562015-01-29 22:48:49 +01001046 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +03001047 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001048}
1049
Radim Krčmářc5192652016-07-12 22:09:28 +02001050/* The KVM local APIC implementation has two quirks:
1051 *
Radim Krčmářb4535b52016-12-15 18:06:47 +01001052 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
1053 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
1054 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +02001055 *
1056 * - in-kernel IOAPIC messages have to be delivered directly to
1057 * x2APIC, because the kernel does not support interrupt remapping.
1058 * In order to support broadcast without interrupt remapping, x2APIC
1059 * rewrites the destination of non-IPI messages from APIC_BROADCAST
1060 * to X2APIC_BROADCAST.
1061 *
1062 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
1063 * important when userspace wants to use x2APIC-format MSIs, because
1064 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +01001065 */
Radim Krčmářc5192652016-07-12 22:09:28 +02001066static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
1067 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +01001068{
1069 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +01001070
Radim Krčmářc5192652016-07-12 22:09:28 +02001071 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +01001072 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +01001073 return X2APIC_BROADCAST;
1074
Radim Krčmářb4535b52016-12-15 18:06:47 +01001075 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +01001076}
1077
Radim Krčmář52c233a2015-01-29 22:48:48 +01001078bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Peter Xu5c69d5c2019-12-04 20:07:20 +01001079 int shorthand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +03001080{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001081 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +02001082 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +03001083
Zachary Amsdenbd371392010-06-14 11:42:15 -10001084 ASSERT(target);
Peter Xu5c69d5c2019-12-04 20:07:20 +01001085 switch (shorthand) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001086 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +01001087 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +01001088 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +02001089 else
Radim Krčmář03d22492015-02-12 19:41:31 +01001090 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +03001091 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +01001092 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +03001093 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +01001094 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +03001095 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +01001096 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +03001097 default:
Radim Krčmář9368b562015-01-29 22:48:49 +01001098 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +03001099 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001100}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001101EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +03001102
Feng Wu520040142016-01-25 16:53:33 +08001103int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
1104 const unsigned long *bitmap, u32 bitmap_size)
1105{
1106 u32 mod;
1107 int i, idx = -1;
1108
1109 mod = vector % dest_vcpus;
1110
1111 for (i = 0; i <= mod; i++) {
1112 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
1113 BUG_ON(idx == bitmap_size);
1114 }
1115
1116 return idx;
1117}
1118
Radim Krčmář4efd8052016-02-12 15:00:15 +01001119static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
1120{
1121 if (!kvm->arch.disabled_lapic_found) {
1122 kvm->arch.disabled_lapic_found = true;
Sean Christopherson8d20bd62022-11-30 23:09:18 +00001123 pr_info("Disabled LAPIC found during irq injection\n");
Radim Krčmář4efd8052016-02-12 15:00:15 +01001124 }
1125}
1126
Radim Krčmářc5192652016-07-12 22:09:28 +02001127static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
1128 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
1129{
1130 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
1131 if ((irq->dest_id == APIC_BROADCAST &&
Sean Christopherson353669012023-01-06 01:12:51 +00001132 map->logical_mode != KVM_APIC_MODE_X2APIC))
Radim Krčmářc5192652016-07-12 22:09:28 +02001133 return true;
1134 if (irq->dest_id == X2APIC_BROADCAST)
1135 return true;
1136 } else {
1137 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
1138 if (irq->dest_id == (x2apic_ipi ?
1139 X2APIC_BROADCAST : APIC_BROADCAST))
1140 return true;
1141 }
1142
1143 return false;
1144}
1145
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001146/* Return true if the interrupt can be handled by using *bitmap as index mask
1147 * for valid destinations in *dst array.
1148 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
1149 * Note: we may have zero kvm_lapic destinations when we return true, which
1150 * means that the interrupt should be dropped. In this case, *bitmap would be
1151 * zero and *dst undefined.
1152 */
1153static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
1154 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
1155 struct kvm_apic_map *map, struct kvm_lapic ***dst,
1156 unsigned long *bitmap)
1157{
1158 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001159
1160 if (irq->shorthand == APIC_DEST_SELF && src) {
1161 *dst = src;
1162 *bitmap = 1;
1163 return true;
1164 } else if (irq->shorthand)
1165 return false;
1166
Radim Krčmářc5192652016-07-12 22:09:28 +02001167 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001168 return false;
1169
1170 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +02001171 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001172 *bitmap = 0;
1173 } else {
Paolo Bonzini1d487e92019-04-11 11:16:47 +02001174 u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
1175 *dst = &map->phys_map[dest_id];
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001176 *bitmap = 1;
1177 }
1178 return true;
1179 }
1180
Radim Krčmáře45115b2016-07-12 22:09:19 +02001181 *bitmap = 0;
1182 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
1183 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001184 return false;
1185
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001186 if (!kvm_lowest_prio_delivery(irq))
1187 return true;
1188
1189 if (!kvm_vector_hashing_enabled()) {
1190 lowest = -1;
1191 for_each_set_bit(i, bitmap, 16) {
1192 if (!(*dst)[i])
1193 continue;
1194 if (lowest < 0)
1195 lowest = i;
1196 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
1197 (*dst)[lowest]->vcpu) < 0)
1198 lowest = i;
1199 }
1200 } else {
1201 if (!*bitmap)
1202 return true;
1203
1204 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
1205 bitmap, 16);
1206
1207 if (!(*dst)[lowest]) {
1208 kvm_apic_disabled_lapic_found(kvm);
1209 *bitmap = 0;
1210 return true;
1211 }
1212 }
1213
1214 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
1215
1216 return true;
1217}
1218
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001219bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001220 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001221{
1222 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001223 unsigned long bitmap;
1224 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001225 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001226 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001227
1228 *r = -1;
1229
1230 if (irq->shorthand == APIC_DEST_SELF) {
Vitaly Kuznetsov00b5f372022-03-25 14:21:39 +01001231 if (KVM_BUG_ON(!src, kvm)) {
1232 *r = 0;
1233 return true;
1234 }
Yang Zhangb4f22252013-04-11 19:21:37 +08001235 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001236 return true;
1237 }
1238
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001239 rcu_read_lock();
1240 map = rcu_dereference(kvm->arch.apic_map);
1241
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001242 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
Paolo Bonzini0624fca2018-10-01 16:07:18 +02001243 if (ret) {
1244 *r = 0;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001245 for_each_set_bit(i, &bitmap, 16) {
1246 if (!dst[i])
1247 continue;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001248 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +01001249 }
Paolo Bonzini0624fca2018-10-01 16:07:18 +02001250 }
Radim Krčmář3548a252015-02-12 19:41:33 +01001251
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001252 rcu_read_unlock();
1253 return ret;
1254}
1255
Feng Wu6228a0d2016-01-25 16:53:34 +08001256/*
Miaohe Lin00116792019-12-11 14:26:23 +08001257 * This routine tries to handle interrupts in posted mode, here is how
Feng Wu6228a0d2016-01-25 16:53:34 +08001258 * it deals with different cases:
1259 * - For single-destination interrupts, handle it in posted mode
1260 * - Else if vector hashing is enabled and it is a lowest-priority
1261 * interrupt, handle it in posted mode and use the following mechanism
Miaohe Lin67b0ae42019-12-11 14:26:22 +08001262 * to find the destination vCPU.
Feng Wu6228a0d2016-01-25 16:53:34 +08001263 * 1. For lowest-priority interrupts, store all the possible
1264 * destination vCPUs in an array.
1265 * 2. Use "guest vector % max number of destination vCPUs" to find
1266 * the right destination vCPU in the array for the lowest-priority
1267 * interrupt.
1268 * - Otherwise, use remapped mode to inject the interrupt.
1269 */
Feng Wu8feb4a02015-09-18 22:29:47 +08001270bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
1271 struct kvm_vcpu **dest_vcpu)
1272{
1273 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001274 unsigned long bitmap;
1275 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +08001276 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +08001277
1278 if (irq->shorthand)
1279 return false;
1280
1281 rcu_read_lock();
1282 map = rcu_dereference(kvm->arch.apic_map);
1283
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001284 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1285 hweight16(bitmap) == 1) {
1286 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +08001287
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001288 if (dst[i]) {
1289 *dest_vcpu = dst[i]->vcpu;
1290 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +08001291 }
Feng Wu8feb4a02015-09-18 22:29:47 +08001292 }
1293
Feng Wu8feb4a02015-09-18 22:29:47 +08001294 rcu_read_unlock();
1295 return ret;
1296}
1297
Eddie Dong97222cc2007-09-12 10:58:04 +03001298/*
1299 * Add a pending IRQ into lapic.
1300 * Return 1 if successfully added and 0 if discarded.
1301 */
1302static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +08001303 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001304 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +03001305{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001306 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +03001307 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +03001308
Paolo Bonzinia183b632014-09-11 11:51:02 +02001309 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1310 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001311 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001312 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +02001313 vcpu->arch.apic_arb_prio++;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001314 fallthrough;
Gleb Natapove1035712009-03-05 16:34:59 +02001315 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001316 if (unlikely(trig_mode && !level))
1317 break;
1318
Eddie Dong97222cc2007-09-12 10:58:04 +03001319 /* FIXME add logic for vcpu on reset */
1320 if (unlikely(!apic_enabled(apic)))
1321 break;
1322
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001323 result = 1;
1324
Joerg Roedel9daa5002016-02-29 16:04:44 +01001325 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001326 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +01001327 dest_map->vectors[vcpu->vcpu_id] = vector;
1328 }
Avi Kivitya5d36f82009-12-29 12:42:16 +02001329
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001330 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1331 if (trig_mode)
Wei Yangee171d22019-03-31 19:17:22 -07001332 kvm_lapic_set_vector(vector,
1333 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001334 else
Wei Yangee171d22019-03-31 19:17:22 -07001335 kvm_lapic_clear_vector(vector,
1336 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001337 }
1338
Wei Wang89604642024-05-07 21:31:02 +08001339 kvm_x86_call(deliver_interrupt)(apic, delivery_mode,
1340 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001341 break;
1342
1343 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +05301344 result = 1;
1345 vcpu->arch.pv.pv_unhalted = 1;
1346 kvm_make_request(KVM_REQ_EVENT, vcpu);
1347 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001348 break;
1349
1350 case APIC_DM_SMI:
Paolo Bonzinib0b42192022-09-29 13:20:09 -04001351 if (!kvm_inject_smi(vcpu)) {
1352 kvm_vcpu_kick(vcpu);
1353 result = 1;
1354 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001355 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001356
Eddie Dong97222cc2007-09-12 10:58:04 +03001357 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001358 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001359 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001360 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001361 break;
1362
1363 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001364 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001365 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001366 /* assumes that there are only KVM_APIC_INIT/SIPI */
1367 apic->pending_events = (1UL << KVM_APIC_INIT);
Avi Kivity3842d132010-07-27 12:30:24 +03001368 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001369 kvm_vcpu_kick(vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001370 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001371 break;
1372
1373 case APIC_DM_STARTUP:
Jan Kiszka66450a22013-03-13 12:42:34 +01001374 result = 1;
1375 apic->sipi_vector = vector;
1376 /* make sure sipi_vector is visible for the receiver */
1377 smp_wmb();
1378 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1379 kvm_make_request(KVM_REQ_EVENT, vcpu);
1380 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001381 break;
1382
Jan Kiszka23930f92008-09-26 09:30:52 +02001383 case APIC_DM_EXTINT:
1384 /*
1385 * Should only be called by kvm_apic_local_deliver() with LVT0,
1386 * before NMI watchdog was enabled. Already handled by
1387 * kvm_apic_accept_pic_intr().
1388 */
1389 break;
1390
Eddie Dong97222cc2007-09-12 10:58:04 +03001391 default:
1392 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1393 delivery_mode);
1394 break;
1395 }
1396 return result;
1397}
1398
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05001399/*
1400 * This routine identifies the destination vcpus mask meant to receive the
1401 * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find
1402 * out the destination vcpus array and set the bitmap or it traverses to
1403 * each available vcpu to identify the same.
1404 */
1405void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
1406 unsigned long *vcpu_bitmap)
1407{
1408 struct kvm_lapic **dest_vcpu = NULL;
1409 struct kvm_lapic *src = NULL;
1410 struct kvm_apic_map *map;
1411 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +00001412 unsigned long bitmap, i;
1413 int vcpu_idx;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05001414 bool ret;
1415
1416 rcu_read_lock();
1417 map = rcu_dereference(kvm->arch.apic_map);
1418
1419 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu,
1420 &bitmap);
1421 if (ret) {
1422 for_each_set_bit(i, &bitmap, 16) {
1423 if (!dest_vcpu[i])
1424 continue;
1425 vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx;
1426 __set_bit(vcpu_idx, vcpu_bitmap);
1427 }
1428 } else {
1429 kvm_for_each_vcpu(i, vcpu, kvm) {
1430 if (!kvm_apic_present(vcpu))
1431 continue;
1432 if (!kvm_apic_match_dest(vcpu, NULL,
Peter Xub4b29632019-12-04 20:07:16 +01001433 irq->shorthand,
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05001434 irq->dest_id,
1435 irq->dest_mode))
1436 continue;
1437 __set_bit(i, vcpu_bitmap);
1438 }
1439 }
1440 rcu_read_unlock();
1441}
1442
Gleb Natapove1035712009-03-05 16:34:59 +02001443int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001444{
Gleb Natapove1035712009-03-05 16:34:59 +02001445 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001446}
1447
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001448static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1449{
Andrey Smetanin63086302015-11-10 15:36:32 +03001450 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001451}
1452
Yang Zhangc7c9c562013-01-25 10:18:51 +08001453static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1454{
Steve Rutherford7543a632015-07-29 23:21:41 -07001455 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001456
Steve Rutherford7543a632015-07-29 23:21:41 -07001457 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1458 if (!kvm_ioapic_handles_vector(apic, vector))
1459 return;
1460
1461 /* Request a KVM exit to inform the userspace IOAPIC. */
1462 if (irqchip_split(apic->vcpu->kvm)) {
1463 apic->vcpu->arch.pending_ioapic_eoi = vector;
1464 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1465 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001466 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001467
1468 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1469 trigger_mode = IOAPIC_LEVEL_TRIG;
1470 else
1471 trigger_mode = IOAPIC_EDGE_TRIG;
1472
1473 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001474}
1475
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001476static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001477{
1478 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001479
1480 trace_kvm_eoi(apic, vector);
1481
Eddie Dong97222cc2007-09-12 10:58:04 +03001482 /*
1483 * Not every write EOI will has corresponding ISR,
1484 * one example is when Kernel check timer on setup_IO_APIC
1485 */
1486 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001487 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001488
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001489 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001490 apic_update_ppr(apic);
1491
Vitaly Kuznetsov06592622023-12-05 11:36:19 +01001492 if (kvm_hv_synic_has_vector(apic->vcpu, vector))
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001493 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1494
Yang Zhangc7c9c562013-01-25 10:18:51 +08001495 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001496 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001497 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001498}
1499
Yang Zhangc7c9c562013-01-25 10:18:51 +08001500/*
1501 * this interface assumes a trap-like exit, which has already finished
1502 * desired side effect including vISR and vPPR update.
1503 */
1504void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1505{
1506 struct kvm_lapic *apic = vcpu->arch.apic;
1507
1508 trace_kvm_eoi(apic, vector);
1509
1510 kvm_ioapic_send_eoi(apic, vector);
1511 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1512}
1513EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1514
Wanpeng Lid5361672020-03-26 10:20:02 +08001515void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
Eddie Dong97222cc2007-09-12 10:58:04 +03001516{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001517 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001518
Sean Christophersonbd17f412022-02-04 21:42:00 +00001519 /* KVM has no delay and should always clear the BUSY/PENDING flag. */
1520 WARN_ON_ONCE(icr_low & APIC_ICR_BUSY);
1521
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001522 irq.vector = icr_low & APIC_VECTOR_MASK;
1523 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1524 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001525 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001526 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1527 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001528 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001529 if (apic_x2apic_mode(apic))
1530 irq.dest_id = icr_high;
1531 else
Suravee Suthikulpanitbf348f62022-05-19 05:26:54 -05001532 irq.dest_id = GET_XAPIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001533
Gleb Natapov1000ff82009-07-07 16:00:57 +03001534 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1535
Yang Zhangb4f22252013-04-11 19:21:37 +08001536 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001537}
Sean Christophersonb51818a2022-02-04 21:41:59 +00001538EXPORT_SYMBOL_GPL(kvm_apic_send_ipi);
Eddie Dong97222cc2007-09-12 10:58:04 +03001539
1540static u32 apic_get_tmcct(struct kvm_lapic *apic)
1541{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001542 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001543 s64 ns;
Eddie Dong97222cc2007-09-12 10:58:04 +03001544
1545 ASSERT(apic != NULL);
1546
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001547 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001548 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001549 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001550 return 0;
1551
Paolo Bonzini55878592016-10-25 15:23:49 +02001552 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001553 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001554 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001555 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001556
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001557 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
Isaku Yamahatab4602562024-04-25 15:07:00 -07001558 return div64_u64(ns, (apic->vcpu->kvm->arch.apic_bus_cycle_ns *
1559 apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001560}
1561
Avi Kivityb209749f2007-10-22 16:50:39 +02001562static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1563{
1564 struct kvm_vcpu *vcpu = apic->vcpu;
1565 struct kvm_run *run = vcpu->run;
1566
Avi Kivitya8eeb042010-05-10 12:34:53 +03001567 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001568 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001569 run->tpr_access.is_write = write;
1570}
1571
1572static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1573{
1574 if (apic->vcpu->arch.tpr_access_reporting)
1575 __report_tpr_access(apic, write);
1576}
1577
Eddie Dong97222cc2007-09-12 10:58:04 +03001578static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1579{
1580 u32 val = 0;
1581
1582 if (offset >= LAPIC_MMIO_LENGTH)
1583 return 0;
1584
1585 switch (offset) {
1586 case APIC_ARBPRI:
Eddie Dong97222cc2007-09-12 10:58:04 +03001587 break;
1588
1589 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001590 if (apic_lvtt_tscdeadline(apic))
1591 return 0;
1592
Eddie Dong97222cc2007-09-12 10:58:04 +03001593 val = apic_get_tmcct(apic);
1594 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001595 case APIC_PROCPRI:
1596 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001597 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001598 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001599 case APIC_TASKPRI:
1600 report_tpr_access(apic, false);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001601 fallthrough;
Eddie Dong97222cc2007-09-12 10:58:04 +03001602 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001603 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001604 break;
1605 }
1606
1607 return val;
1608}
1609
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001610static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1611{
1612 return container_of(dev, struct kvm_lapic, dev);
1613}
1614
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001615#define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1616#define APIC_REGS_MASK(first, count) \
1617 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1618
Sean Christophersonb5fcc592023-01-07 01:10:23 +00001619u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001620{
Sean Christophersonb5fcc592023-01-07 01:10:23 +00001621 /* Leave bits '0' for reserved and write-only registers. */
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001622 u64 valid_reg_mask =
1623 APIC_REG_MASK(APIC_ID) |
1624 APIC_REG_MASK(APIC_LVR) |
1625 APIC_REG_MASK(APIC_TASKPRI) |
1626 APIC_REG_MASK(APIC_PROCPRI) |
1627 APIC_REG_MASK(APIC_LDR) |
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001628 APIC_REG_MASK(APIC_SPIV) |
1629 APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1630 APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1631 APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1632 APIC_REG_MASK(APIC_ESR) |
1633 APIC_REG_MASK(APIC_ICR) |
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001634 APIC_REG_MASK(APIC_LVTT) |
1635 APIC_REG_MASK(APIC_LVTTHMR) |
1636 APIC_REG_MASK(APIC_LVTPC) |
1637 APIC_REG_MASK(APIC_LVT0) |
1638 APIC_REG_MASK(APIC_LVT1) |
1639 APIC_REG_MASK(APIC_LVTERR) |
1640 APIC_REG_MASK(APIC_TMICT) |
1641 APIC_REG_MASK(APIC_TMCCT) |
1642 APIC_REG_MASK(APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001643
Jue Wang4b903562022-06-10 10:11:30 -07001644 if (kvm_lapic_lvt_supported(apic, LVT_CMCI))
1645 valid_reg_mask |= APIC_REG_MASK(APIC_LVTCMCI);
1646
Sean Christophersonb5fcc592023-01-07 01:10:23 +00001647 /* ARBPRI, DFR, and ICR2 are not valid in x2APIC mode. */
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001648 if (!apic_x2apic_mode(apic))
Sean Christophersona57a3162022-02-04 21:42:03 +00001649 valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI) |
Sean Christophersonb2236492023-01-07 01:10:22 +00001650 APIC_REG_MASK(APIC_DFR) |
Sean Christophersona57a3162022-02-04 21:42:03 +00001651 APIC_REG_MASK(APIC_ICR2);
Sean Christophersonb5fcc592023-01-07 01:10:23 +00001652
1653 return valid_reg_mask;
1654}
1655EXPORT_SYMBOL_GPL(kvm_lapic_readable_reg_mask);
1656
1657static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
1658 void *data)
1659{
1660 unsigned char alignment = offset & 0xf;
1661 u32 result;
1662
1663 /*
1664 * WARN if KVM reads ICR in x2APIC mode, as it's an 8-byte register in
1665 * x2APIC and needs to be manually handled by the caller.
1666 */
1667 WARN_ON_ONCE(apic_x2apic_mode(apic) && offset == APIC_ICR);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001668
Jim Mattson218bf772021-06-02 13:52:24 -07001669 if (alignment + len > 4)
1670 return 1;
1671
Sean Christophersonb5fcc592023-01-07 01:10:23 +00001672 if (offset > 0x3f0 ||
1673 !(kvm_lapic_readable_reg_mask(apic) & APIC_REG_MASK(offset)))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001674 return 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001675
Eddie Dong97222cc2007-09-12 10:58:04 +03001676 result = __apic_read(apic, offset & ~0xf);
1677
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001678 trace_kvm_apic_read(offset, result);
1679
Eddie Dong97222cc2007-09-12 10:58:04 +03001680 switch (len) {
1681 case 1:
1682 case 2:
1683 case 4:
1684 memcpy(data, (char *)&result + alignment, len);
1685 break;
1686 default:
1687 printk(KERN_ERR "Local APIC read with len = %x, "
1688 "should be 1,2, or 4 instead\n", len);
1689 break;
1690 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001691 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001692}
1693
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001694static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1695{
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001696 return addr >= apic->base_address &&
1697 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001698}
1699
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001700static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001701 gpa_t address, int len, void *data)
1702{
1703 struct kvm_lapic *apic = to_lapic(this);
1704 u32 offset = address - apic->base_address;
1705
1706 if (!apic_mmio_in_range(apic, address))
1707 return -EOPNOTSUPP;
1708
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001709 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1710 if (!kvm_check_has_quirk(vcpu->kvm,
1711 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1712 return -EOPNOTSUPP;
1713
1714 memset(data, 0xff, len);
1715 return 0;
1716 }
1717
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001718 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001719
1720 return 0;
1721}
1722
Eddie Dong97222cc2007-09-12 10:58:04 +03001723static void update_divide_count(struct kvm_lapic *apic)
1724{
1725 u32 tmp1, tmp2, tdcr;
1726
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001727 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001728 tmp1 = tdcr & 0xf;
1729 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001730 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001731}
1732
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001733static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1734{
1735 /*
1736 * Do not allow the guest to program periodic timers with small
1737 * interval, since the hrtimers are not throttled by the host
1738 * scheduler.
1739 */
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001740 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001741 s64 min_period = min_timer_period_us * 1000LL;
1742
1743 if (apic->lapic_timer.period < min_period) {
Jim Mattson0005ca22024-07-24 12:05:09 -07001744 pr_info_once(
Sean Christopherson8d20bd62022-11-30 23:09:18 +00001745 "vcpu %i: requested %lld ns "
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001746 "lapic timer period limited to %lld ns\n",
1747 apic->vcpu->vcpu_id,
1748 apic->lapic_timer.period, min_period);
1749 apic->lapic_timer.period = min_period;
1750 }
1751 }
1752}
1753
Wanpeng Li94be4b82020-03-24 14:32:10 +08001754static void cancel_hv_timer(struct kvm_lapic *apic);
1755
Wanpeng Lie898da72021-06-07 00:19:43 -07001756static void cancel_apic_timer(struct kvm_lapic *apic)
1757{
1758 hrtimer_cancel(&apic->lapic_timer.timer);
1759 preempt_disable();
1760 if (apic->lapic_timer.hv_timer_in_use)
1761 cancel_hv_timer(apic);
1762 preempt_enable();
Wanpeng Li619f51d2022-05-20 10:15:18 -04001763 atomic_set(&apic->lapic_timer.pending, 0);
Wanpeng Lie898da72021-06-07 00:19:43 -07001764}
1765
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001766static void apic_update_lvtt(struct kvm_lapic *apic)
1767{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001768 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001769 apic->lapic_timer.timer_mode_mask;
1770
1771 if (apic->lapic_timer.timer_mode != timer_mode) {
Wanpeng Lic69518c2017-10-05 03:53:51 -07001772 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001773 APIC_LVT_TIMER_TSCDEADLINE)) {
Wanpeng Lie898da72021-06-07 00:19:43 -07001774 cancel_apic_timer(apic);
Radim Krčmář44275932017-10-06 19:25:55 +02001775 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1776 apic->lapic_timer.period = 0;
1777 apic->lapic_timer.tscdeadline = 0;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001778 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001779 apic->lapic_timer.timer_mode = timer_mode;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001780 limit_periodic_timer_frequency(apic);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001781 }
1782}
1783
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001784/*
1785 * On APICv, this test will cause a busy wait
1786 * during a higher-priority task.
1787 */
1788
1789static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1790{
1791 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001792 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001793
1794 if (kvm_apic_hw_enabled(apic)) {
1795 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001796 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001797
Sean Christophersonce0a58f42022-06-14 23:05:47 +00001798 if (apic->apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001799 bitmap = apic->regs + APIC_IRR;
1800
1801 if (apic_test_vector(vec, bitmap))
1802 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001803 }
1804 return false;
1805}
1806
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001807static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1808{
1809 u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1810
1811 /*
1812 * If the guest TSC is running at a different ratio than the host, then
1813 * convert the delay to nanoseconds to achieve an accurate delay. Note
1814 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1815 * always for VMX enabled hardware.
1816 */
Sean Christopherson938c8742022-05-24 21:56:23 +08001817 if (vcpu->arch.tsc_scaling_ratio == kvm_caps.default_tsc_scaling_ratio) {
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001818 __delay(min(guest_cycles,
1819 nsec_to_cycles(vcpu, timer_advance_ns)));
1820 } else {
1821 u64 delay_ns = guest_cycles * 1000000ULL;
1822 do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1823 ndelay(min_t(u32, delay_ns, timer_advance_ns));
1824 }
1825}
1826
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001827static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
Wanpeng Liec0671d2019-05-20 16:18:08 +08001828 s64 advance_expire_delta)
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001829{
1830 struct kvm_lapic *apic = vcpu->arch.apic;
Sean Christopherson39497d72019-04-17 10:15:32 -07001831 u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001832 u64 ns;
1833
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001834 /* Do not adjust for tiny fluctuations or large random spikes. */
1835 if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
1836 abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
1837 return;
1838
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001839 /* too early */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001840 if (advance_expire_delta < 0) {
1841 ns = -advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001842 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001843 timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001844 } else {
1845 /* too late */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001846 ns = advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001847 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001848 timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001849 }
1850
Wanpeng Lia0f00372019-09-26 08:54:03 +08001851 if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
1852 timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001853 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1854}
1855
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001856static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001857{
1858 struct kvm_lapic *apic = vcpu->arch.apic;
1859 u64 guest_tsc, tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001860
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001861 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1862 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001863 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Wanpeng Lie0ac5352022-04-26 01:25:51 -07001864 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001865
Sean Christopherson89a58812024-05-21 18:03:04 -07001866 adjust_lapic_timer_advance(vcpu, guest_tsc - tsc_deadline);
1867
1868 /*
1869 * If the timer fired early, reread the TSC to account for the overhead
1870 * of the above adjustment to avoid waiting longer than is necessary.
1871 */
1872 if (guest_tsc < tsc_deadline)
1873 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Wanpeng Li9805cf02021-05-18 05:00:35 -07001874
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001875 if (guest_tsc < tsc_deadline)
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001876 __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001877}
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001878
1879void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
1880{
Wanpeng Li010fd372020-09-10 17:50:41 +08001881 if (lapic_in_kernel(vcpu) &&
1882 vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1883 vcpu->arch.apic->lapic_timer.timer_advance_ns &&
1884 lapic_timer_int_injected(vcpu))
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001885 __kvm_wait_lapic_expire(vcpu);
1886}
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08001887EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001888
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001889static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
1890{
1891 struct kvm_timer *ktimer = &apic->lapic_timer;
1892
1893 kvm_apic_local_deliver(apic, APIC_LVTT);
Haiwei Li17ac43a2020-01-16 16:50:21 +08001894 if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001895 ktimer->tscdeadline = 0;
Haiwei Li17ac43a2020-01-16 16:50:21 +08001896 } else if (apic_lvtt_oneshot(apic)) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001897 ktimer->tscdeadline = 0;
1898 ktimer->target_expiration = 0;
1899 }
1900}
1901
Wanpeng Liae95f562020-04-28 14:23:28 +08001902static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001903{
1904 struct kvm_vcpu *vcpu = apic->vcpu;
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001905 struct kvm_timer *ktimer = &apic->lapic_timer;
1906
1907 if (atomic_read(&apic->lapic_timer.pending))
1908 return;
1909
1910 if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
1911 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1912
Sean Christophersonce0a58f42022-06-14 23:05:47 +00001913 if (!from_timer_fn && apic->apicv_active) {
Wanpeng Liae95f562020-04-28 14:23:28 +08001914 WARN_ON(kvm_get_running_vcpu() != vcpu);
1915 kvm_apic_inject_pending_timer_irqs(apic);
1916 return;
1917 }
1918
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001919 if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
Sean Christophersonbeda4302021-03-04 18:18:08 -08001920 /*
1921 * Ensure the guest's timer has truly expired before posting an
1922 * interrupt. Open code the relevant checks to avoid querying
1923 * lapic_timer_int_injected(), which will be false since the
1924 * interrupt isn't yet injected. Waiting until after injecting
1925 * is not an option since that won't help a posted interrupt.
1926 */
1927 if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1928 vcpu->arch.apic->lapic_timer.timer_advance_ns)
1929 __kvm_wait_lapic_expire(vcpu);
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001930 kvm_apic_inject_pending_timer_irqs(apic);
1931 return;
1932 }
1933
1934 atomic_inc(&apic->lapic_timer.pending);
Marcelo Tosatti084071d2021-05-25 10:41:17 -03001935 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
Wanpeng Li68ca76632020-09-10 17:50:40 +08001936 if (from_timer_fn)
1937 kvm_vcpu_kick(vcpu);
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001938}
1939
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001940static void start_sw_tscdeadline(struct kvm_lapic *apic)
1941{
Sean Christopherson39497d72019-04-17 10:15:32 -07001942 struct kvm_timer *ktimer = &apic->lapic_timer;
1943 u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001944 u64 ns = 0;
1945 ktime_t expire;
1946 struct kvm_vcpu *vcpu = apic->vcpu;
Thorsten Blum1448d4a2024-08-14 22:33:46 +02001947 u32 this_tsc_khz = vcpu->arch.virtual_tsc_khz;
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001948 unsigned long flags;
1949 ktime_t now;
1950
1951 if (unlikely(!tscdeadline || !this_tsc_khz))
1952 return;
1953
1954 local_irq_save(flags);
1955
Paolo Bonzini55878592016-10-25 15:23:49 +02001956 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001957 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Liran Alonc09d65d2019-04-16 20:36:34 +03001958
1959 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1960 do_div(ns, this_tsc_khz);
1961
1962 if (likely(tscdeadline > guest_tsc) &&
Sean Christopherson39497d72019-04-17 10:15:32 -07001963 likely(ns > apic->lapic_timer.timer_advance_ns)) {
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001964 expire = ktime_add_ns(now, ns);
Sean Christopherson39497d72019-04-17 10:15:32 -07001965 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02001966 hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001967 } else
Wanpeng Liae95f562020-04-28 14:23:28 +08001968 apic_timer_expired(apic, false);
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001969
1970 local_irq_restore(flags);
1971}
1972
Peter Shier24647e02018-10-10 15:56:53 -07001973static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
1974{
Isaku Yamahatab4602562024-04-25 15:07:00 -07001975 return (u64)tmict * apic->vcpu->kvm->arch.apic_bus_cycle_ns *
1976 (u64)apic->divide_count;
Peter Shier24647e02018-10-10 15:56:53 -07001977}
1978
Wanpeng Lic301b902017-10-06 07:38:32 -07001979static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1980{
1981 ktime_t now, remaining;
1982 u64 ns_remaining_old, ns_remaining_new;
1983
Peter Shier24647e02018-10-10 15:56:53 -07001984 apic->lapic_timer.period =
1985 tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
Wanpeng Lic301b902017-10-06 07:38:32 -07001986 limit_periodic_timer_frequency(apic);
1987
1988 now = ktime_get();
1989 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1990 if (ktime_to_ns(remaining) < 0)
1991 remaining = 0;
1992
1993 ns_remaining_old = ktime_to_ns(remaining);
1994 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1995 apic->divide_count, old_divisor);
1996
1997 apic->lapic_timer.tscdeadline +=
1998 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1999 nsec_to_cycles(apic->vcpu, ns_remaining_old);
2000 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
2001}
2002
Peter Shier24647e02018-10-10 15:56:53 -07002003static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002004{
2005 ktime_t now;
2006 u64 tscl = rdtsc();
Peter Shier24647e02018-10-10 15:56:53 -07002007 s64 deadline;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002008
Paolo Bonzini55878592016-10-25 15:23:49 +02002009 now = ktime_get();
Peter Shier24647e02018-10-10 15:56:53 -07002010 apic->lapic_timer.period =
2011 tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002012
Radim Krčmář5d74a692017-10-06 19:25:54 +02002013 if (!apic->lapic_timer.period) {
2014 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002015 return false;
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08002016 }
2017
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07002018 limit_periodic_timer_frequency(apic);
Peter Shier24647e02018-10-10 15:56:53 -07002019 deadline = apic->lapic_timer.period;
2020
2021 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
2022 if (unlikely(count_reg != APIC_TMICT)) {
2023 deadline = tmict_to_ns(apic,
2024 kvm_lapic_get_reg(apic, count_reg));
Li RongQing8e6ed96c2023-01-06 12:06:25 +08002025 if (unlikely(deadline <= 0)) {
2026 if (apic_lvtt_period(apic))
2027 deadline = apic->lapic_timer.period;
2028 else
2029 deadline = 0;
2030 }
Peter Shier24647e02018-10-10 15:56:53 -07002031 else if (unlikely(deadline > apic->lapic_timer.period)) {
2032 pr_info_ratelimited(
Sean Christopherson8d20bd62022-11-30 23:09:18 +00002033 "vcpu %i: requested lapic timer restore with "
Peter Shier24647e02018-10-10 15:56:53 -07002034 "starting count register %#x=%u (%lld ns) > initial count (%lld ns). "
2035 "Using initial count to start timer.\n",
2036 apic->vcpu->vcpu_id,
2037 count_reg,
2038 kvm_lapic_get_reg(apic, count_reg),
2039 deadline, apic->lapic_timer.period);
2040 kvm_lapic_set_reg(apic, count_reg, 0);
2041 deadline = apic->lapic_timer.period;
2042 }
2043 }
2044 }
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08002045
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002046 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
Peter Shier24647e02018-10-10 15:56:53 -07002047 nsec_to_cycles(apic->vcpu, deadline);
2048 apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002049
2050 return true;
2051}
2052
2053static void advance_periodic_target_expiration(struct kvm_lapic *apic)
2054{
David Vrabeld8f2f492018-05-18 16:55:46 +01002055 ktime_t now = ktime_get();
2056 u64 tscl = rdtsc();
2057 ktime_t delta;
2058
2059 /*
2060 * Synchronize both deadlines to the same time source or
2061 * differences in the periods (caused by differences in the
2062 * underlying clocks or numerical approximation errors) will
2063 * cause the two to drift apart over time as the errors
2064 * accumulate.
2065 */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002066 apic->lapic_timer.target_expiration =
2067 ktime_add_ns(apic->lapic_timer.target_expiration,
2068 apic->lapic_timer.period);
David Vrabeld8f2f492018-05-18 16:55:46 +01002069 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
2070 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
2071 nsec_to_cycles(apic->vcpu, delta);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08002072}
2073
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00002074static void start_sw_period(struct kvm_lapic *apic)
2075{
2076 if (!apic->lapic_timer.period)
2077 return;
2078
2079 if (ktime_after(ktime_get(),
2080 apic->lapic_timer.target_expiration)) {
Wanpeng Liae95f562020-04-28 14:23:28 +08002081 apic_timer_expired(apic, false);
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00002082
2083 if (apic_lvtt_oneshot(apic))
2084 return;
2085
2086 advance_periodic_target_expiration(apic);
2087 }
2088
2089 hrtimer_start(&apic->lapic_timer.timer,
2090 apic->lapic_timer.target_expiration,
He Zheedec6e02020-03-20 15:06:07 +08002091 HRTIMER_MODE_ABS_HARD);
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00002092}
2093
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002094bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
2095{
Wanpeng Li91005302016-08-03 12:04:12 +08002096 if (!lapic_in_kernel(vcpu))
2097 return false;
2098
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002099 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
2100}
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002101
Wanpeng Li7e810a32016-10-24 18:23:12 +08002102static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08002103{
Wanpeng Li1d518c62017-07-25 00:43:15 -07002104 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02002105 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
Wei Wang89604642024-05-07 21:31:02 +08002106 kvm_x86_call(cancel_hv_timer)(apic->vcpu);
Wanpeng Libd97ad02016-06-30 08:52:49 +08002107 apic->lapic_timer.hv_timer_in_use = false;
2108}
2109
Paolo Bonzinia749e242017-06-29 17:14:50 +02002110static bool start_hv_timer(struct kvm_lapic *apic)
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002111{
2112 struct kvm_timer *ktimer = &apic->lapic_timer;
Sean Christophersonf9927982019-04-16 13:32:46 -07002113 struct kvm_vcpu *vcpu = apic->vcpu;
2114 bool expired;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002115
Wanpeng Li1d518c62017-07-25 00:43:15 -07002116 WARN_ON(preemptible());
Paolo Bonzini199a8b82020-05-05 06:45:35 -04002117 if (!kvm_can_use_hv_timer(vcpu))
Paolo Bonzinia749e242017-06-29 17:14:50 +02002118 return false;
2119
Radim Krčmář86bbc1e2017-10-06 19:25:53 +02002120 if (!ktimer->tscdeadline)
2121 return false;
2122
Wei Wang89604642024-05-07 21:31:02 +08002123 if (kvm_x86_call(set_hv_timer)(vcpu, ktimer->tscdeadline, &expired))
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002124 return false;
2125
2126 ktimer->hv_timer_in_use = true;
2127 hrtimer_cancel(&ktimer->timer);
2128
2129 /*
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07002130 * To simplify handling the periodic timer, leave the hv timer running
2131 * even if the deadline timer has expired, i.e. rely on the resulting
2132 * VM-Exit to recompute the periodic timer's target expiration.
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002133 */
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07002134 if (!apic_lvtt_period(apic)) {
2135 /*
2136 * Cancel the hv timer if the sw timer fired while the hv timer
2137 * was being programmed, or if the hv timer itself expired.
2138 */
2139 if (atomic_read(&ktimer->pending)) {
2140 cancel_hv_timer(apic);
Sean Christophersonf9927982019-04-16 13:32:46 -07002141 } else if (expired) {
Wanpeng Liae95f562020-04-28 14:23:28 +08002142 apic_timer_expired(apic, false);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07002143 cancel_hv_timer(apic);
2144 }
Wanpeng Lic8533542017-06-29 06:28:09 -07002145 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02002146
Sean Christophersonf9927982019-04-16 13:32:46 -07002147 trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07002148
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002149 return true;
2150}
2151
Paolo Bonzinia749e242017-06-29 17:14:50 +02002152static void start_sw_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08002153{
Paolo Bonzinia749e242017-06-29 17:14:50 +02002154 struct kvm_timer *ktimer = &apic->lapic_timer;
Wanpeng Li1d518c62017-07-25 00:43:15 -07002155
2156 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02002157 if (apic->lapic_timer.hv_timer_in_use)
2158 cancel_hv_timer(apic);
2159 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
2160 return;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02002161
Paolo Bonzinia749e242017-06-29 17:14:50 +02002162 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
2163 start_sw_period(apic);
2164 else if (apic_lvtt_tscdeadline(apic))
2165 start_sw_tscdeadline(apic);
2166 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
2167}
2168
2169static void restart_apic_timer(struct kvm_lapic *apic)
2170{
Wanpeng Li1d518c62017-07-25 00:43:15 -07002171 preempt_disable();
Sean Christopherson4ca88b32019-04-16 13:32:47 -07002172
2173 if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
2174 goto out;
2175
Paolo Bonzinia749e242017-06-29 17:14:50 +02002176 if (!start_hv_timer(apic))
2177 start_sw_timer(apic);
Sean Christopherson4ca88b32019-04-16 13:32:47 -07002178out:
Wanpeng Li1d518c62017-07-25 00:43:15 -07002179 preempt_enable();
Wanpeng Li196f20c2016-06-28 14:54:19 +08002180}
2181
Eddie Dong97222cc2007-09-12 10:58:04 +03002182void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
2183{
2184 struct kvm_lapic *apic = vcpu->arch.apic;
2185
Wanpeng Li1d518c62017-07-25 00:43:15 -07002186 preempt_disable();
2187 /* If the preempt notifier has already run, it also called apic_timer_expired */
2188 if (!apic->lapic_timer.hv_timer_in_use)
2189 goto out;
Sean Christophersond92a5d12021-10-08 19:12:12 -07002190 WARN_ON(kvm_vcpu_is_blocking(vcpu));
Wanpeng Liae95f562020-04-28 14:23:28 +08002191 apic_timer_expired(apic, false);
Wanpeng Lid981dd12021-04-28 19:08:02 +08002192 cancel_hv_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002193
2194 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
2195 advance_periodic_target_expiration(apic);
Paolo Bonzinia749e242017-06-29 17:14:50 +02002196 restart_apic_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002197 }
Wanpeng Li1d518c62017-07-25 00:43:15 -07002198out:
2199 preempt_enable();
Eddie Dong97222cc2007-09-12 10:58:04 +03002200}
2201EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
2202
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002203void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
2204{
Paolo Bonzinia749e242017-06-29 17:14:50 +02002205 restart_apic_timer(vcpu->arch.apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002206}
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002207
2208void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
2209{
2210 struct kvm_lapic *apic = vcpu->arch.apic;
2211
Wanpeng Li1d518c62017-07-25 00:43:15 -07002212 preempt_disable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002213 /* Possibly the TSC deadline timer is not enabled yet */
Paolo Bonzinia749e242017-06-29 17:14:50 +02002214 if (apic->lapic_timer.hv_timer_in_use)
2215 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07002216 preempt_enable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002217}
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002218
Paolo Bonzinia749e242017-06-29 17:14:50 +02002219void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
2220{
2221 struct kvm_lapic *apic = vcpu->arch.apic;
2222
2223 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
2224 restart_apic_timer(apic);
2225}
2226
Peter Shier24647e02018-10-10 15:56:53 -07002227static void __start_apic_timer(struct kvm_lapic *apic, u32 count_reg)
Eddie Dong97222cc2007-09-12 10:58:04 +03002228{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002229 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02002230
Paolo Bonzinia749e242017-06-29 17:14:50 +02002231 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
Peter Shier24647e02018-10-10 15:56:53 -07002232 && !set_target_expiration(apic, count_reg))
Paolo Bonzinia749e242017-06-29 17:14:50 +02002233 return;
2234
2235 restart_apic_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002236}
2237
Peter Shier24647e02018-10-10 15:56:53 -07002238static void start_apic_timer(struct kvm_lapic *apic)
2239{
2240 __start_apic_timer(apic, APIC_TMICT);
2241}
2242
Jan Kiszkacc6e4622008-10-20 10:20:03 +02002243static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
2244{
Radim Krčmář59fd1322015-06-30 22:19:16 +02002245 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02002246
Radim Krčmář59fd1322015-06-30 22:19:16 +02002247 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
2248 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
2249 if (lvt0_in_nmi_mode) {
Radim Krčmář42720132015-07-01 15:31:49 +02002250 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02002251 } else
2252 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
2253 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02002254}
2255
Jue Wang4b903562022-06-10 10:11:30 -07002256static int get_lvt_index(u32 reg)
2257{
2258 if (reg == APIC_LVTCMCI)
2259 return LVT_CMCI;
2260 if (reg < APIC_LVTT || reg > APIC_LVTERR)
2261 return -1;
2262 return array_index_nospec(
2263 (reg - APIC_LVTT) >> 4, KVM_APIC_MAX_NR_LVT_ENTRIES);
2264}
2265
Sean Christopherson70180052022-02-04 21:42:01 +00002266static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03002267{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002268 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002269
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002270 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03002271
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002272 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002273 case APIC_ID: /* Local APIC ID */
Maxim Levitsky3743c2f2022-06-06 21:08:24 +03002274 if (!apic_x2apic_mode(apic)) {
Radim Krčmářa92e2542016-07-12 22:09:22 +02002275 kvm_apic_set_xapic_id(apic, val >> 24);
Maxim Levitsky3743c2f2022-06-06 21:08:24 +03002276 } else {
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002277 ret = 1;
Maxim Levitsky3743c2f2022-06-06 21:08:24 +03002278 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002279 break;
2280
2281 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02002282 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03002283 apic_set_tpr(apic, val & 0xff);
2284 break;
2285
2286 case APIC_EOI:
2287 apic_set_eoi(apic);
2288 break;
2289
2290 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002291 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03002292 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002293 else
2294 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03002295 break;
2296
2297 case APIC_DFR:
Wanpeng Liae6f2492020-08-19 16:55:26 +08002298 if (!apic_x2apic_mode(apic))
2299 kvm_apic_set_dfr(apic, val | 0x0FFFFFFF);
2300 else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002301 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03002302 break;
2303
Gleb Natapovfc61b802009-07-05 17:39:35 +03002304 case APIC_SPIV: {
2305 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002306 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03002307 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea102012-08-05 15:58:31 +03002308 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03002309 if (!(val & APIC_SPIV_APIC_ENABLED)) {
2310 int i;
Eddie Dong97222cc2007-09-12 10:58:04 +03002311
Jue Wang4b903562022-06-10 10:11:30 -07002312 for (i = 0; i < apic->nr_lvt_entries; i++) {
Jue Wang987f6252022-06-10 10:11:29 -07002313 kvm_lapic_set_reg(apic, APIC_LVTx(i),
Jue Wang4b903562022-06-10 10:11:30 -07002314 kvm_lapic_get_reg(apic, APIC_LVTx(i)) | APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +03002315 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002316 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002317 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002318
2319 }
2320 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03002321 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002322 case APIC_ICR:
Sean Christophersona57a3162022-02-04 21:42:03 +00002323 WARN_ON_ONCE(apic_x2apic_mode(apic));
2324
Eddie Dong97222cc2007-09-12 10:58:04 +03002325 /* No delay here, so we always clear the pending bit */
Sean Christophersonbd17f412022-02-04 21:42:00 +00002326 val &= ~APIC_ICR_BUSY;
Wanpeng Lid5361672020-03-26 10:20:02 +08002327 kvm_apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
Wanpeng Li2b0911d2019-09-05 14:26:27 +08002328 kvm_lapic_set_reg(apic, APIC_ICR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03002329 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03002330 case APIC_ICR2:
Sean Christophersona57a3162022-02-04 21:42:03 +00002331 if (apic_x2apic_mode(apic))
2332 ret = 1;
2333 else
2334 kvm_lapic_set_reg(apic, APIC_ICR2, val & 0xff000000);
Eddie Dong97222cc2007-09-12 10:58:04 +03002335 break;
2336
Jan Kiszka23930f92008-09-26 09:30:52 +02002337 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02002338 apic_manage_nmi_watchdog(apic, val);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002339 fallthrough;
Eddie Dong97222cc2007-09-12 10:58:04 +03002340 case APIC_LVTTHMR:
2341 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03002342 case APIC_LVT1:
Jue Wang4b903562022-06-10 10:11:30 -07002343 case APIC_LVTERR:
2344 case APIC_LVTCMCI: {
2345 u32 index = get_lvt_index(reg);
2346 if (!kvm_lapic_lvt_supported(apic, index)) {
2347 ret = 1;
2348 break;
2349 }
Gleb Natapovc48f1492012-08-05 15:58:33 +03002350 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002351 val |= APIC_LVT_MASKED;
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08002352 val &= apic_lvt_mask[index];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002353 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03002354 break;
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08002355 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002356
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002357 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03002358 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002359 val |= APIC_LVT_MASKED;
2360 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002361 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002362 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002363 break;
2364
Eddie Dong97222cc2007-09-12 10:58:04 +03002365 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002366 if (apic_lvtt_tscdeadline(apic))
2367 break;
2368
Wanpeng Lie898da72021-06-07 00:19:43 -07002369 cancel_apic_timer(apic);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002370 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03002371 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002372 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03002373
Wanpeng Lic301b902017-10-06 07:38:32 -07002374 case APIC_TDCR: {
2375 uint32_t old_divisor = apic->divide_count;
2376
Wanpeng Lia445fc42020-07-31 11:12:20 +08002377 kvm_lapic_set_reg(apic, APIC_TDCR, val & 0xb);
Eddie Dong97222cc2007-09-12 10:58:04 +03002378 update_divide_count(apic);
Wanpeng Lic301b902017-10-06 07:38:32 -07002379 if (apic->divide_count != old_divisor &&
2380 apic->lapic_timer.period) {
2381 hrtimer_cancel(&apic->lapic_timer.timer);
2382 update_target_expiration(apic, old_divisor);
2383 restart_apic_timer(apic);
2384 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002385 break;
Wanpeng Lic301b902017-10-06 07:38:32 -07002386 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002387 case APIC_ESR:
Yi Wang0d888002019-07-06 01:08:48 +08002388 if (apic_x2apic_mode(apic) && val != 0)
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002389 ret = 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002390 break;
2391
2392 case APIC_SELF_IPI:
Sean Christophersonba5838a2023-01-07 01:10:20 +00002393 /*
2394 * Self-IPI exists only when x2APIC is enabled. Bits 7:0 hold
2395 * the vector, everything else is reserved.
2396 */
2397 if (!apic_x2apic_mode(apic) || (val & ~APIC_VECTOR_MASK))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002398 ret = 1;
Sean Christophersonba5838a2023-01-07 01:10:20 +00002399 else
2400 kvm_apic_send_ipi(apic, APIC_DEST_SELF | val, 0);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002401 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03002402 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002403 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03002404 break;
2405 }
Yi Wang0d888002019-07-06 01:08:48 +08002406
Sean Christophersonbd17f412022-02-04 21:42:00 +00002407 /*
2408 * Recalculate APIC maps if necessary, e.g. if the software enable bit
2409 * was toggled, the APIC ID changed, etc... The maps are marked dirty
2410 * on relevant changes, i.e. this is a nop for most writes.
2411 */
Wanpeng Li4abaffc2020-02-26 10:41:02 +08002412 kvm_recalculate_apic_map(apic->vcpu->kvm);
2413
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002414 return ret;
2415}
2416
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00002417static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002418 gpa_t address, int len, const void *data)
2419{
2420 struct kvm_lapic *apic = to_lapic(this);
2421 unsigned int offset = address - apic->base_address;
2422 u32 val;
2423
2424 if (!apic_mmio_in_range(apic, address))
2425 return -EOPNOTSUPP;
2426
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02002427 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2428 if (!kvm_check_has_quirk(vcpu->kvm,
2429 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2430 return -EOPNOTSUPP;
2431
2432 return 0;
2433 }
2434
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002435 /*
2436 * APIC register must be aligned on 128-bits boundary.
2437 * 32/64/128 bits registers must be accessed thru 32 bits.
2438 * Refer SDM 8.4.1
2439 */
Yi Wang0d888002019-07-06 01:08:48 +08002440 if (len != 4 || (offset & 0xf))
Sheng Yang756975b2009-07-06 11:05:39 +08002441 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002442
2443 val = *(u32*)data;
2444
Yi Wang0d888002019-07-06 01:08:48 +08002445 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002446
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002447 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002448}
2449
Kevin Tian58fbbf22011-08-30 13:56:17 +03002450void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
2451{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002452 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03002453}
2454EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
2455
Sean Christophersond3323432024-07-19 16:50:59 -07002456#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13))
2457
2458int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data)
2459{
2460 if (data & X2APIC_ICR_RESERVED_BITS)
2461 return 1;
2462
2463 /*
2464 * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but
2465 * only AMD requires it to be zero, Intel essentially just ignores the
2466 * bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled,
2467 * the CPU performs the reserved bits checks, i.e. the underlying CPU
2468 * behavior will "win". Arbitrarily clear the BUSY bit, as there is no
2469 * sane way to provide consistent behavior with respect to hardware.
2470 */
2471 data &= ~APIC_ICR_BUSY;
2472
2473 kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32));
Sean Christopherson73b42dc2024-07-19 16:51:00 -07002474 if (kvm_x86_ops.x2apic_icr_is_split) {
2475 kvm_lapic_set_reg(apic, APIC_ICR, data);
2476 kvm_lapic_set_reg(apic, APIC_ICR2, data >> 32);
2477 } else {
2478 kvm_lapic_set_reg64(apic, APIC_ICR, data);
2479 }
Sean Christophersond3323432024-07-19 16:50:59 -07002480 trace_kvm_apic_write(APIC_ICR, data);
2481 return 0;
2482}
2483
Sean Christopherson73b42dc2024-07-19 16:51:00 -07002484static u64 kvm_x2apic_icr_read(struct kvm_lapic *apic)
2485{
2486 if (kvm_x86_ops.x2apic_icr_is_split)
2487 return (u64)kvm_lapic_get_reg(apic, APIC_ICR) |
2488 (u64)kvm_lapic_get_reg(apic, APIC_ICR2) << 32;
2489
2490 return kvm_lapic_get_reg64(apic, APIC_ICR);
2491}
2492
Yang Zhang83d4c282013-01-25 10:18:49 +08002493/* emulate APIC access in a trap manner */
2494void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
2495{
Zeng Guang5413bcb2022-04-19 23:35:16 +08002496 struct kvm_lapic *apic = vcpu->arch.apic;
Yang Zhang83d4c282013-01-25 10:18:49 +08002497
Suravee Suthikulpanit1bd9dfe2022-07-25 00:33:56 -05002498 /*
Tao Su629d36982023-09-14 13:55:04 +08002499 * ICR is a single 64-bit register when x2APIC is enabled, all others
2500 * registers hold 32-bit values. For legacy xAPIC, ICR writes need to
2501 * go down the common path to get the upper half from ICR2.
2502 *
2503 * Note, using the write helpers may incur an unnecessary write to the
2504 * virtual APIC state, but KVM needs to conditionally modify the value
2505 * in certain cases, e.g. to clear the ICR busy bit. The cost of extra
2506 * conditional branches is likely a wash relative to the cost of the
2507 * maybe-unecessary write, and both are in the noise anyways.
Suravee Suthikulpanit1bd9dfe2022-07-25 00:33:56 -05002508 */
Tao Su629d36982023-09-14 13:55:04 +08002509 if (apic_x2apic_mode(apic) && offset == APIC_ICR)
Sean Christopherson73b42dc2024-07-19 16:51:00 -07002510 WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_x2apic_icr_read(apic)));
Tao Su629d36982023-09-14 13:55:04 +08002511 else
2512 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
Yang Zhang83d4c282013-01-25 10:18:49 +08002513}
2514EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2515
Rusty Russelld5894442007-10-08 10:48:30 +10002516void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002517{
Gleb Natapovf8c1ea102012-08-05 15:58:31 +03002518 struct kvm_lapic *apic = vcpu->arch.apic;
2519
Sean Christophersona78d9042024-02-09 14:20:46 -08002520 if (!vcpu->arch.apic) {
2521 static_branch_dec(&kvm_has_noapic_vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002522 return;
Sean Christophersona78d9042024-02-09 14:20:46 -08002523 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002524
Gleb Natapovf8c1ea102012-08-05 15:58:31 +03002525 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002526
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002527 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
Cun Li6e4e3b42021-01-11 23:24:35 +08002528 static_branch_slow_dec_deferred(&apic_hw_disabled);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002529
Radim Krčmáře4627552014-10-30 15:06:45 +01002530 if (!apic->sw_enabled)
Cun Li6e4e3b42021-01-11 23:24:35 +08002531 static_branch_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03002532
Gleb Natapovf8c1ea102012-08-05 15:58:31 +03002533 if (apic->regs)
2534 free_page((unsigned long)apic->regs);
2535
2536 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002537}
2538
2539/*
2540 *----------------------------------------------------------------------
2541 * LAPIC interface
2542 *----------------------------------------------------------------------
2543 */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002544u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2545{
2546 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002547
Wanpeng Lia970e9b2020-09-10 17:50:36 +08002548 if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002549 return 0;
2550
2551 return apic->lapic_timer.tscdeadline;
2552}
2553
2554void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2555{
2556 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002557
Wanpeng Li27503832020-09-10 17:50:37 +08002558 if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002559 return;
2560
2561 hrtimer_cancel(&apic->lapic_timer.timer);
2562 apic->lapic_timer.tscdeadline = data;
2563 start_apic_timer(apic);
2564}
2565
Eddie Dong97222cc2007-09-12 10:58:04 +03002566void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2567{
Zhenzhong Duanf66af9f2022-02-10 17:45:06 +08002568 apic_set_tpr(vcpu->arch.apic, (cr8 & 0x0f) << 4);
Eddie Dong97222cc2007-09-12 10:58:04 +03002569}
2570
2571u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2572{
Eddie Dong97222cc2007-09-12 10:58:04 +03002573 u64 tpr;
2574
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002575 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03002576
2577 return (tpr & 0xf0) >> 4;
2578}
2579
2580void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2581{
Yang Zhang8d146952013-01-25 10:18:50 +08002582 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002583 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002584
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01002585 vcpu->arch.apic_base = value;
2586
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002587 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08002588 kvm_update_cpuid_runtime(vcpu);
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002589
2590 if (!apic)
2591 return;
2592
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002593 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01002594 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02002595 if (value & MSR_IA32_APICBASE_ENABLE) {
2596 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Cun Li6e4e3b42021-01-11 23:24:35 +08002597 static_branch_slow_dec_deferred(&apic_hw_disabled);
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +02002598 /* Check if there are APF page ready requests pending */
2599 kvm_make_request(KVM_REQ_APF_READY, vcpu);
Wanpeng Li187ca842016-08-03 12:04:13 +08002600 } else {
Cun Li6e4e3b42021-01-11 23:24:35 +08002601 static_branch_inc(&apic_hw_disabled.key);
Paolo Bonzini44d52712020-06-22 16:37:42 +02002602 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Wanpeng Li187ca842016-08-03 12:04:13 +08002603 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002604 }
2605
Emanuele Giuseppe Esposito052c3b992023-01-10 10:40:33 -08002606 if ((old_value ^ value) & X2APIC_ENABLE) {
2607 if (value & X2APIC_ENABLE)
2608 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2609 else if (value & MSR_IA32_APICBASE_ENABLE)
2610 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2611 }
Jim Mattson8d860bb2018-05-09 16:56:05 -04002612
Suravee Suthikulpanit8fc9c7a2022-05-19 05:27:00 -05002613 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) {
Sean Christopherson1459f5c2023-01-06 01:12:41 +00002614 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
Wei Wang89604642024-05-07 21:31:02 +08002615 kvm_x86_call(set_virtual_apic_mode)(vcpu);
Suravee Suthikulpanit8fc9c7a2022-05-19 05:27:00 -05002616 }
Yang Zhang8d146952013-01-25 10:18:50 +08002617
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002618 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03002619 MSR_IA32_APICBASE_BASE;
2620
Nadav Amitdb324fe2014-11-02 11:54:59 +02002621 if ((value & MSR_IA32_APICBASE_ENABLE) &&
Maxim Levitsky3743c2f2022-06-06 21:08:24 +03002622 apic->base_address != APIC_DEFAULT_PHYS_BASE) {
2623 kvm_set_apicv_inhibit(apic->vcpu->kvm,
2624 APICV_INHIBIT_REASON_APIC_BASE_MODIFIED);
2625 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002626}
2627
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002628void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
2629{
2630 struct kvm_lapic *apic = vcpu->arch.apic;
2631
Sean Christophersonce0a58f42022-06-14 23:05:47 +00002632 if (apic->apicv_active) {
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002633 /* irr_pending is always true when apicv is activated. */
2634 apic->irr_pending = true;
2635 apic->isr_count = 1;
2636 } else {
Maxim Levitsky755c2bf2022-02-07 17:54:25 +02002637 /*
2638 * Don't clear irr_pending, searching the IRR can race with
2639 * updates from the CPU as APICv is still active from hardware's
2640 * perspective. The flag will be cleared as appropriate when
2641 * KVM injects the interrupt.
2642 */
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002643 apic->isr_count = count_vectors(apic->regs + APIC_ISR);
2644 }
Sean Christopherson97a71c42023-01-06 01:12:35 +00002645 apic->highest_isr_cache = -1;
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002646}
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002647
Sean Christophersonc482f2c2023-01-06 01:12:42 +00002648int kvm_alloc_apic_access_page(struct kvm *kvm)
2649{
2650 struct page *page;
2651 void __user *hva;
2652 int ret = 0;
2653
2654 mutex_lock(&kvm->slots_lock);
Sean Christopherson2008fab2023-01-06 01:12:43 +00002655 if (kvm->arch.apic_access_memslot_enabled ||
2656 kvm->arch.apic_access_memslot_inhibited)
Sean Christophersonc482f2c2023-01-06 01:12:42 +00002657 goto out;
2658
2659 hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
2660 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
2661 if (IS_ERR(hva)) {
2662 ret = PTR_ERR(hva);
2663 goto out;
2664 }
2665
2666 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
2667 if (is_error_page(page)) {
2668 ret = -EFAULT;
2669 goto out;
2670 }
2671
2672 /*
2673 * Do not pin the page in memory, so that memory hot-unplug
2674 * is able to migrate it.
2675 */
2676 put_page(page);
2677 kvm->arch.apic_access_memslot_enabled = true;
2678out:
2679 mutex_unlock(&kvm->slots_lock);
2680 return ret;
2681}
2682EXPORT_SYMBOL_GPL(kvm_alloc_apic_access_page);
2683
Sean Christopherson2008fab2023-01-06 01:12:43 +00002684void kvm_inhibit_apic_access_page(struct kvm_vcpu *vcpu)
2685{
2686 struct kvm *kvm = vcpu->kvm;
2687
2688 if (!kvm->arch.apic_access_memslot_enabled)
2689 return;
2690
2691 kvm_vcpu_srcu_read_unlock(vcpu);
2692
2693 mutex_lock(&kvm->slots_lock);
2694
2695 if (kvm->arch.apic_access_memslot_enabled) {
2696 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
2697 /*
2698 * Clear "enabled" after the memslot is deleted so that a
2699 * different vCPU doesn't get a false negative when checking
2700 * the flag out of slots_lock. No additional memory barrier is
2701 * needed as modifying memslots requires waiting other vCPUs to
2702 * drop SRCU (see above), and false positives are ok as the
2703 * flag is rechecked after acquiring slots_lock.
2704 */
2705 kvm->arch.apic_access_memslot_enabled = false;
2706
2707 /*
2708 * Mark the memslot as inhibited to prevent reallocating the
2709 * memslot during vCPU creation, e.g. if a vCPU is hotplugged.
2710 */
2711 kvm->arch.apic_access_memslot_inhibited = true;
2712 }
2713
2714 mutex_unlock(&kvm->slots_lock);
2715
2716 kvm_vcpu_srcu_read_lock(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002717}
2718
2719void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
2720{
2721 struct kvm_lapic *apic = vcpu->arch.apic;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002722 u64 msr_val;
Eddie Dong97222cc2007-09-12 10:58:04 +03002723 int i;
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002724
Wei Wang89604642024-05-07 21:31:02 +08002725 kvm_x86_call(apicv_pre_state_restore)(vcpu);
Haitao Shan9cfec6d2023-09-12 16:55:45 -07002726
Sean Christopherson45477002021-07-13 09:32:56 -07002727 if (!init_event) {
Sean Christophersonf7d8a192021-10-12 17:35:53 -07002728 msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
Sean Christopherson45477002021-07-13 09:32:56 -07002729 if (kvm_vcpu_is_reset_bsp(vcpu))
Sean Christophersonf7d8a192021-10-12 17:35:53 -07002730 msr_val |= MSR_IA32_APICBASE_BSP;
2731 kvm_lapic_set_base(vcpu, msr_val);
Sean Christopherson45477002021-07-13 09:32:56 -07002732 }
2733
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002734 if (!apic)
2735 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002736
Eddie Dong97222cc2007-09-12 10:58:04 +03002737 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002738 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002739
Sean Christophersonf7d8a192021-10-12 17:35:53 -07002740 /* The xAPIC ID is set at RESET even if the APIC was already enabled. */
2741 if (!init_event)
Radim Krčmářa92e2542016-07-12 22:09:22 +02002742 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002743 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002744
Jue Wang4b903562022-06-10 10:11:30 -07002745 for (i = 0; i < apic->nr_lvt_entries; i++)
Jue Wang987f6252022-06-10 10:11:29 -07002746 kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002747 apic_update_lvtt(apic);
Jan H. Schönherr52b54192017-05-20 13:24:32 +02002748 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2749 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002750 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03002751 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002752 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03002753
Wanpeng Liae6f2492020-08-19 16:55:26 +08002754 kvm_apic_set_dfr(apic, 0xffffffffU);
Gleb Natapovf8c1ea102012-08-05 15:58:31 +03002755 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002756 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02002757 if (!apic_x2apic_mode(apic))
2758 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002759 kvm_lapic_set_reg(apic, APIC_ESR, 0);
Sean Christophersona57a3162022-02-04 21:42:03 +00002760 if (!apic_x2apic_mode(apic)) {
2761 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2762 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2763 } else {
2764 kvm_lapic_set_reg64(apic, APIC_ICR, 0);
2765 }
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002766 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2767 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002768 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002769 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2770 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2771 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002772 }
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002773 kvm_apic_update_apicv(vcpu);
Kevin Pedrettib33ac882007-10-21 08:54:53 +02002774 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002775 atomic_set(&apic->lapic_timer.pending, 0);
Sean Christopherson549240e2021-07-13 09:32:50 -07002776
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002777 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002778 apic_update_ppr(apic);
Sean Christophersonce0a58f42022-06-14 23:05:47 +00002779 if (apic->apicv_active) {
Wei Wang89604642024-05-07 21:31:02 +08002780 kvm_x86_call(apicv_post_state_restore)(vcpu);
2781 kvm_x86_call(hwapic_irr_update)(vcpu, -1);
2782 kvm_x86_call(hwapic_isr_update)(-1);
Jan H. Schönherr4191db22017-10-25 16:43:27 +02002783 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002784
Gleb Natapove1035712009-03-05 16:34:59 +02002785 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03002786 vcpu->arch.apic_attention = 0;
Wanpeng Li4abaffc2020-02-26 10:41:02 +08002787
2788 kvm_recalculate_apic_map(vcpu->kvm);
Eddie Dong97222cc2007-09-12 10:58:04 +03002789}
2790
Eddie Dong97222cc2007-09-12 10:58:04 +03002791/*
2792 *----------------------------------------------------------------------
2793 * timer interface
2794 *----------------------------------------------------------------------
2795 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03002796
Avi Kivity2a6eac92012-07-26 18:01:51 +03002797static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002798{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002799 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002800}
2801
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002802int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2803{
Gleb Natapov54e98182012-08-05 15:58:32 +03002804 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002805
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01002806 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03002807 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002808
2809 return 0;
2810}
2811
Avi Kivity89342082011-11-10 14:57:21 +02002812int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002813{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002814 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002815 int vector, mode, trig_mode;
Jim Mattsona16eb252023-09-25 17:34:47 +00002816 int r;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002817
Gleb Natapovc48f1492012-08-05 15:58:33 +03002818 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002819 vector = reg & APIC_VECTOR_MASK;
2820 mode = reg & APIC_MODE_MASK;
2821 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Jim Mattsona16eb252023-09-25 17:34:47 +00002822
2823 r = __apic_accept_irq(apic, mode, vector, 1, trig_mode, NULL);
Sandipan Das49ff3b42024-04-05 16:55:55 -07002824 if (r && lvt_type == APIC_LVTPC &&
2825 guest_cpuid_is_intel_compatible(apic->vcpu))
Jim Mattsona16eb252023-09-25 17:34:47 +00002826 kvm_lapic_set_reg(apic, APIC_LVTPC, reg | APIC_LVT_MASKED);
2827 return r;
Jan Kiszka23930f92008-09-26 09:30:52 +02002828 }
2829 return 0;
2830}
2831
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002832void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002833{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002834 struct kvm_lapic *apic = vcpu->arch.apic;
2835
2836 if (apic)
2837 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002838}
2839
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002840static const struct kvm_io_device_ops apic_mmio_ops = {
2841 .read = apic_mmio_read,
2842 .write = apic_mmio_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002843};
2844
Avi Kivitye9d90d42012-07-26 18:01:50 +03002845static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2846{
2847 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002848 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002849
Wanpeng Liae95f562020-04-28 14:23:28 +08002850 apic_timer_expired(apic, true);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002851
Avi Kivity2a6eac92012-07-26 18:01:51 +03002852 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002853 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002854 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2855 return HRTIMER_RESTART;
2856 } else
2857 return HRTIMER_NORESTART;
2858}
2859
Sean Christopherson89a58812024-05-21 18:03:04 -07002860int kvm_create_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002861{
2862 struct kvm_lapic *apic;
2863
2864 ASSERT(vcpu != NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03002865
Sean Christophersona78d9042024-02-09 14:20:46 -08002866 if (!irqchip_in_kernel(vcpu->kvm)) {
2867 static_branch_inc(&kvm_has_noapic_vcpu);
2868 return 0;
2869 }
2870
Ben Gardon254272c2019-02-11 11:02:50 -08002871 apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
Eddie Dong97222cc2007-09-12 10:58:04 +03002872 if (!apic)
2873 goto nomem;
2874
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002875 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002876
Brijesh Singh75253db2024-01-25 22:11:21 -06002877 if (kvm_x86_ops.alloc_apic_backing_page)
Wei Wang89604642024-05-07 21:31:02 +08002878 apic->regs = kvm_x86_call(alloc_apic_backing_page)(vcpu);
Brijesh Singh75253db2024-01-25 22:11:21 -06002879 else
2880 apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002881 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002882 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2883 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002884 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002885 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002886 apic->vcpu = vcpu;
2887
Sean Christopherson03d84f92022-07-08 15:38:51 -07002888 apic->nr_lvt_entries = kvm_apic_calc_nr_lvt_entries(vcpu);
2889
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002890 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02002891 HRTIMER_MODE_ABS_HARD);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002892 apic->lapic_timer.timer.function = apic_timer_fn;
Sean Christopherson89a58812024-05-21 18:03:04 -07002893 if (lapic_timer_advance)
Wanpeng Lia0f00372019-09-26 08:54:03 +08002894 apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002895
Sean Christophersonf7d8a192021-10-12 17:35:53 -07002896 /*
2897 * Stuff the APIC ENABLE bit in lieu of temporarily incrementing
2898 * apic_hw_disabled; the full RESET value is set by kvm_lapic_reset().
2899 */
2900 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Cun Li6e4e3b42021-01-11 23:24:35 +08002901 static_branch_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002902 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002903
Sean Christophersona78d9042024-02-09 14:20:46 -08002904 /*
2905 * Defer evaluating inhibits until the vCPU is first run, as this vCPU
2906 * will not get notified of any changes until this vCPU is visible to
2907 * other vCPUs (marked online and added to the set of vCPUs).
2908 *
2909 * Opportunistically mark APICv active as VMX in particularly is highly
2910 * unlikely to have inhibits. Ignore the current per-VM APICv state so
2911 * that vCPU creation is guaranteed to run with a deterministic value,
2912 * the request will ensure the vCPU gets the correct state before VM-Entry.
2913 */
2914 if (enable_apicv) {
2915 apic->apicv_active = true;
2916 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
2917 }
2918
Eddie Dong97222cc2007-09-12 10:58:04 +03002919 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002920nomem_free_apic:
2921 kfree(apic);
Saar Amara251fb902019-05-06 11:29:16 +03002922 vcpu->arch.apic = NULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002923nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002924 return -ENOMEM;
2925}
Eddie Dong97222cc2007-09-12 10:58:04 +03002926
2927int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2928{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002929 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002930 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002931
Paolo Bonzini72c3bcd2020-11-27 08:53:52 +01002932 if (!kvm_apic_present(vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03002933 return -1;
2934
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002935 __apic_update_ppr(apic, &ppr);
2936 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002937}
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07002938EXPORT_SYMBOL_GPL(kvm_apic_has_interrupt);
Eddie Dong97222cc2007-09-12 10:58:04 +03002939
Qing He40487c62007-09-17 14:47:13 +08002940int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2941{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002942 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002943
Gleb Natapovc48f1492012-08-05 15:58:33 +03002944 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Miaohe Lin3ce4dc12020-01-18 10:50:37 +08002945 return 1;
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002946 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2947 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
Miaohe Lin3ce4dc12020-01-18 10:50:37 +08002948 return 1;
2949 return 0;
Qing He40487c62007-09-17 14:47:13 +08002950}
2951
Eddie Dong1b9778d2007-09-03 16:56:58 +03002952void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2953{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002954 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002955
Gleb Natapov54e98182012-08-05 15:58:32 +03002956 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08002957 kvm_apic_inject_pending_timer_irqs(apic);
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002958 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002959 }
2960}
2961
Sean Christophersona194a3a2024-09-05 21:34:07 -07002962void kvm_apic_ack_interrupt(struct kvm_vcpu *vcpu, int vector)
Eddie Dong97222cc2007-09-12 10:58:04 +03002963{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002964 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002965 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002966
Sean Christophersona194a3a2024-09-05 21:34:07 -07002967 if (WARN_ON_ONCE(vector < 0 || !apic))
2968 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002969
Wanpeng Li56cc2402014-08-05 12:42:24 +08002970 /*
2971 * We get here even with APIC virtualization enabled, if doing
2972 * nested virtualization and L1 runs with the "acknowledge interrupt
2973 * on exit" mode. Then we cannot inject the interrupt via RVI,
2974 * because the process would deliver it through the IDT.
2975 */
2976
Eddie Dong97222cc2007-09-12 10:58:04 +03002977 apic_clear_irr(vector, apic);
Vitaly Kuznetsov16e880b2023-12-05 11:36:18 +01002978 if (kvm_hv_synic_auto_eoi_set(vcpu, vector)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002979 /*
2980 * For auto-EOI interrupts, there might be another pending
2981 * interrupt above PPR, so check whether to raise another
2982 * KVM_REQ_EVENT.
2983 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002984 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002985 } else {
2986 /*
2987 * For normal interrupts, PPR has been raised and there cannot
2988 * be a higher-priority pending interrupt---except if there was
2989 * a concurrent interrupt injection, but that would have
2990 * triggered KVM_REQ_EVENT already.
2991 */
2992 apic_set_isr(vector, apic);
2993 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002994 }
2995
Eddie Dong97222cc2007-09-12 10:58:04 +03002996}
Sean Christophersona194a3a2024-09-05 21:34:07 -07002997EXPORT_SYMBOL_GPL(kvm_apic_ack_interrupt);
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002998
Radim Krčmářa92e2542016-07-12 22:09:22 +02002999static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
3000 struct kvm_lapic_state *s, bool set)
3001{
3002 if (apic_x2apic_mode(vcpu->arch.apic)) {
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003003 u32 x2apic_id = kvm_x2apic_id(vcpu->arch.apic);
Radim Krčmářa92e2542016-07-12 22:09:22 +02003004 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00003005 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Sean Christophersona57a3162022-02-04 21:42:03 +00003006 u64 icr;
Radim Krčmářa92e2542016-07-12 22:09:22 +02003007
Radim Krčmář371313132016-07-12 22:09:27 +02003008 if (vcpu->kvm->arch.x2apic_format) {
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003009 if (*id != x2apic_id)
Radim Krčmář371313132016-07-12 22:09:27 +02003010 return -EINVAL;
3011 } else {
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003012 /*
3013 * Ignore the userspace value when setting APIC state.
3014 * KVM's model is that the x2APIC ID is readonly, e.g.
3015 * KVM only supports delivering interrupts to KVM's
3016 * version of the x2APIC ID. However, for backwards
3017 * compatibility, don't reject attempts to set a
3018 * mismatched ID for userspace that hasn't opted into
3019 * x2apic_format.
3020 */
Radim Krčmář371313132016-07-12 22:09:27 +02003021 if (set)
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003022 *id = x2apic_id;
Radim Krčmář371313132016-07-12 22:09:27 +02003023 else
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003024 *id = x2apic_id << 24;
Radim Krčmář371313132016-07-12 22:09:27 +02003025 }
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00003026
Sean Christophersona57a3162022-02-04 21:42:03 +00003027 /*
3028 * In x2APIC mode, the LDR is fixed and based on the id. And
Sean Christopherson73b42dc2024-07-19 16:51:00 -07003029 * if the ICR is _not_ split, ICR is internally a single 64-bit
3030 * register, but needs to be split to ICR+ICR2 in userspace for
3031 * backwards compatibility.
Sean Christophersona57a3162022-02-04 21:42:03 +00003032 */
Sean Christopherson73b42dc2024-07-19 16:51:00 -07003033 if (set)
Sean Christopherson4b7c3f62024-08-02 13:29:40 -07003034 *ldr = kvm_apic_calc_x2apic_ldr(x2apic_id);
Sean Christophersona57a3162022-02-04 21:42:03 +00003035
Sean Christopherson73b42dc2024-07-19 16:51:00 -07003036 if (!kvm_x86_ops.x2apic_icr_is_split) {
3037 if (set) {
3038 icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) |
3039 (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32;
3040 __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr);
3041 } else {
3042 icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR);
3043 __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32);
3044 }
Sean Christophersona57a3162022-02-04 21:42:03 +00003045 }
Radim Krčmářa92e2542016-07-12 22:09:22 +02003046 }
3047
3048 return 0;
3049}
3050
3051int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
3052{
3053 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
Peter Shier24647e02018-10-10 15:56:53 -07003054
3055 /*
3056 * Get calculated timer current count for remaining timer period (if
3057 * any) and store it in the returned register set.
3058 */
3059 __kvm_lapic_set_reg(s->regs, APIC_TMCCT,
3060 __apic_read(vcpu->arch.apic, APIC_TMCCT));
3061
Radim Krčmářa92e2542016-07-12 22:09:22 +02003062 return kvm_apic_state_fixup(vcpu, s, false);
3063}
3064
3065int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003066{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003067 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02003068 int r;
3069
Wei Wang89604642024-05-07 21:31:02 +08003070 kvm_x86_call(apicv_pre_state_restore)(vcpu);
Haitao Shan9cfec6d2023-09-12 16:55:45 -07003071
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03003072 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03003073 /* set SPIV separately to get count of SW disabled APICs right */
3074 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02003075
3076 r = kvm_apic_state_fixup(vcpu, s, true);
Wanpeng Li4abaffc2020-02-26 10:41:02 +08003077 if (r) {
3078 kvm_recalculate_apic_map(vcpu->kvm);
Radim Krčmářa92e2542016-07-12 22:09:22 +02003079 return r;
Wanpeng Li4abaffc2020-02-26 10:41:02 +08003080 }
Jordan Borgner0e96f312018-10-28 12:58:28 +00003081 memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
Radim Krčmářa92e2542016-07-12 22:09:22 +02003082
Paolo Bonzini44d52712020-06-22 16:37:42 +02003083 atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
Wanpeng Li4abaffc2020-02-26 10:41:02 +08003084 kvm_recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03003085 kvm_apic_set_version(vcpu);
3086
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003087 apic_update_ppr(apic);
Wanpeng Li35fe7cf2022-01-25 01:17:00 -08003088 cancel_apic_timer(apic);
Wanpeng Li35737d22021-03-04 08:35:18 +08003089 apic->lapic_timer.expired_tscdeadline = 0;
Radim Krčmářb6ac0692015-06-05 20:57:41 +02003090 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05003091 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003092 update_divide_count(apic);
Peter Shier24647e02018-10-10 15:56:53 -07003093 __start_apic_timer(apic, APIC_TMCCT);
Wanpeng Li27358862021-06-09 00:16:40 -07003094 kvm_lapic_set_reg(apic, APIC_TMCCT, 0);
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06003095 kvm_apic_update_apicv(vcpu);
Sean Christophersonce0a58f42022-06-14 23:05:47 +00003096 if (apic->apicv_active) {
Wei Wang89604642024-05-07 21:31:02 +08003097 kvm_x86_call(apicv_post_state_restore)(vcpu);
3098 kvm_x86_call(hwapic_irr_update)(vcpu,
3099 apic_find_highest_irr(apic));
3100 kvm_x86_call(hwapic_isr_update)(apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03003101 }
Avi Kivity3842d132010-07-27 12:30:24 +03003102 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07003103 if (ioapic_in_kernel(vcpu->kvm))
3104 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01003105
3106 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02003107
3108 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003109}
Eddie Donga3d7f852007-09-03 16:15:12 +03003110
Avi Kivity2f52d582008-01-16 12:49:30 +02003111void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03003112{
Eddie Donga3d7f852007-09-03 16:15:12 +03003113 struct hrtimer *timer;
3114
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08003115 if (!lapic_in_kernel(vcpu) ||
3116 kvm_can_post_timer_interrupt(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03003117 return;
3118
Gleb Natapov54e98182012-08-05 15:58:32 +03003119 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03003120 if (hrtimer_cancel(timer))
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02003121 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
Eddie Donga3d7f852007-09-03 16:15:12 +03003122}
Avi Kivityb93463a2007-10-25 16:52:32 +02003123
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003124/*
3125 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
3126 *
3127 * Detect whether guest triggered PV EOI since the
3128 * last entry. If yes, set EOI on guests's behalf.
3129 * Clear PV EOI in guest memory in any case.
3130 */
3131static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
3132 struct kvm_lapic *apic)
3133{
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003134 int vector;
3135 /*
3136 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
3137 * and KVM_PV_EOI_ENABLED in guest memory as follows:
3138 *
3139 * KVM_APIC_PV_EOI_PENDING is unset:
3140 * -> host disabled PV EOI.
3141 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
3142 * -> host enabled PV EOI, guest did not execute EOI yet.
3143 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
3144 * -> host enabled PV EOI, guest executed EOI.
3145 */
3146 BUG_ON(!pv_eoi_enabled(vcpu));
Li RongQing51b12092021-11-04 19:56:14 +08003147
3148 if (pv_eoi_test_and_clr_pending(vcpu))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003149 return;
3150 vector = apic_set_eoi(apic);
3151 trace_kvm_pv_eoi(apic, vector);
3152}
3153
Avi Kivityb93463a2007-10-25 16:52:32 +02003154void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
3155{
3156 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02003157
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003158 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
3159 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
3160
Gleb Natapov41383772012-04-19 14:06:29 +03003161 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02003162 return;
3163
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003164 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
3165 sizeof(u32)))
Nicholas Krause603242a2015-08-05 10:44:40 -04003166 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02003167
3168 apic_set_tpr(vcpu->arch.apic, data & 0xff);
3169}
3170
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003171/*
3172 * apic_sync_pv_eoi_to_guest - called before vmentry
3173 *
3174 * Detect whether it's safe to enable PV EOI and
3175 * if yes do so.
3176 */
3177static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
3178 struct kvm_lapic *apic)
3179{
3180 if (!pv_eoi_enabled(vcpu) ||
3181 /* IRR set or many bits in ISR: could be nested. */
3182 apic->irr_pending ||
3183 /* Cache not set: could be safe but we don't bother. */
3184 apic->highest_isr_cache == -1 ||
3185 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02003186 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003187 /*
3188 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
3189 * so we need not do anything here.
3190 */
3191 return;
3192 }
3193
3194 pv_eoi_set_pending(apic->vcpu);
3195}
3196
Avi Kivityb93463a2007-10-25 16:52:32 +02003197void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
3198{
3199 u32 data, tpr;
3200 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003201 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02003202
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003203 apic_sync_pv_eoi_to_guest(vcpu, apic);
3204
Gleb Natapov41383772012-04-19 14:06:29 +03003205 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02003206 return;
3207
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05003208 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02003209 max_irr = apic_find_highest_irr(apic);
3210 if (max_irr < 0)
3211 max_irr = 0;
3212 max_isr = apic_find_highest_isr(apic);
3213 if (max_isr < 0)
3214 max_isr = 0;
3215 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
3216
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003217 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
3218 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02003219}
3220
Andy Honigfda4e2e2013-11-20 10:23:22 -08003221int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02003222{
Andy Honigfda4e2e2013-11-20 10:23:22 -08003223 if (vapic_addr) {
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003224 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andy Honigfda4e2e2013-11-20 10:23:22 -08003225 &vcpu->arch.apic->vapic_cache,
3226 vapic_addr, sizeof(u32)))
3227 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03003228 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08003229 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03003230 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08003231 }
3232
3233 vcpu->arch.apic->vapic_addr = vapic_addr;
3234 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02003235}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003236
Sean Christopherson54294782022-02-04 21:42:02 +00003237static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data)
3238{
Sean Christophersona57a3162022-02-04 21:42:03 +00003239 u32 low;
3240
3241 if (reg == APIC_ICR) {
Sean Christopherson73b42dc2024-07-19 16:51:00 -07003242 *data = kvm_x2apic_icr_read(apic);
Sean Christophersona57a3162022-02-04 21:42:03 +00003243 return 0;
3244 }
Sean Christopherson54294782022-02-04 21:42:02 +00003245
3246 if (kvm_lapic_reg_read(apic, reg, 4, &low))
3247 return 1;
3248
Sean Christophersona57a3162022-02-04 21:42:03 +00003249 *data = low;
Sean Christopherson54294782022-02-04 21:42:02 +00003250
3251 return 0;
3252}
3253
3254static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data)
3255{
Sean Christophersona57a3162022-02-04 21:42:03 +00003256 /*
Sean Christophersonab52be12023-01-07 01:10:21 +00003257 * ICR is a 64-bit register in x2APIC mode (and Hyper-V PV vAPIC) and
Sean Christophersona57a3162022-02-04 21:42:03 +00003258 * can be written as such, all other registers remain accessible only
3259 * through 32-bit reads/writes.
3260 */
Sean Christopherson54294782022-02-04 21:42:02 +00003261 if (reg == APIC_ICR)
Sean Christophersona57a3162022-02-04 21:42:03 +00003262 return kvm_x2apic_icr_write(apic, data);
3263
Sean Christophersonab52be12023-01-07 01:10:21 +00003264 /* Bits 63:32 are reserved in all other registers. */
3265 if (data >> 32)
3266 return 1;
3267
Sean Christopherson54294782022-02-04 21:42:02 +00003268 return kvm_lapic_reg_write(apic, reg, (u32)data);
3269}
3270
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003271int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
3272{
3273 struct kvm_lapic *apic = vcpu->arch.apic;
3274 u32 reg = (msr - APIC_BASE_MSR) << 4;
3275
Paolo Bonzini35754c92015-07-29 12:05:37 +02003276 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003277 return 1;
3278
Sean Christopherson54294782022-02-04 21:42:02 +00003279 return kvm_lapic_msr_write(apic, reg, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003280}
3281
3282int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
3283{
3284 struct kvm_lapic *apic = vcpu->arch.apic;
Sean Christopherson54294782022-02-04 21:42:02 +00003285 u32 reg = (msr - APIC_BASE_MSR) << 4;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003286
Paolo Bonzini35754c92015-07-29 12:05:37 +02003287 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003288 return 1;
3289
Sean Christopherson54294782022-02-04 21:42:02 +00003290 return kvm_lapic_msr_read(apic, reg, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003291}
Gleb Natapov10388a02010-01-17 15:51:23 +02003292
3293int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
3294{
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003295 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02003296 return 1;
3297
Sean Christopherson54294782022-02-04 21:42:02 +00003298 return kvm_lapic_msr_write(vcpu->arch.apic, reg, data);
Gleb Natapov10388a02010-01-17 15:51:23 +02003299}
3300
3301int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
3302{
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003303 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02003304 return 1;
3305
Sean Christopherson54294782022-02-04 21:42:02 +00003306 return kvm_lapic_msr_read(vcpu->arch.apic, reg, data);
Gleb Natapov10388a02010-01-17 15:51:23 +02003307}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003308
Vitaly Kuznetsov77c33232021-11-08 16:28:18 +01003309int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003310{
3311 u64 addr = data & ~KVM_MSR_ENABLED;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02003312 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
3313 unsigned long new_len;
Vitaly Kuznetsovafd67ee2021-11-08 16:28:19 +01003314 int ret;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02003315
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003316 if (!IS_ALIGNED(addr, 4))
3317 return 1;
3318
Vitaly Kuznetsovafd67ee2021-11-08 16:28:19 +01003319 if (data & KVM_MSR_ENABLED) {
3320 if (addr == ghc->gpa && len <= ghc->len)
3321 new_len = ghc->len;
3322 else
3323 new_len = len;
3324
3325 ret = kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
3326 if (ret)
3327 return ret;
3328 }
3329
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003330 vcpu->arch.pv_eoi.msr_val = data;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02003331
Vitaly Kuznetsovafd67ee2021-11-08 16:28:19 +01003332 return 0;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003333}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03003334
Jim Mattson4fe09bc2021-06-04 10:26:04 -07003335int kvm_apic_accept_events(struct kvm_vcpu *vcpu)
Jan Kiszka66450a22013-03-13 12:42:34 +01003336{
3337 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01003338 u8 sipi_vector;
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05003339 int r;
Jan Kiszka66450a22013-03-13 12:42:34 +01003340
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003341 if (!kvm_apic_has_pending_init_or_sipi(vcpu))
Jim Mattson4fe09bc2021-06-04 10:26:04 -07003342 return 0;
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05003343
3344 if (is_guest_mode(vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08003345 r = kvm_check_nested_events(vcpu);
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05003346 if (r < 0)
Jim Mattson4fe09bc2021-06-04 10:26:04 -07003347 return r == -EBUSY ? 0 : r;
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05003348 /*
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003349 * Continue processing INIT/SIPI even if a nested VM-Exit
3350 * occurred, e.g. pending SIPIs should be dropped if INIT+SIPI
3351 * are blocked as a result of transitioning to VMX root mode.
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05003352 */
3353 }
3354
3355 /*
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003356 * INITs are blocked while CPU is in specific states (SMM, VMX root
3357 * mode, SVM with GIF=0), while SIPIs are dropped if the CPU isn't in
3358 * wait-for-SIPI (WFS).
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02003359 */
Sean Christopherson1b7a1b72022-09-21 00:31:52 +00003360 if (!kvm_apic_init_sipi_allowed(vcpu)) {
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02003361 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003362 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
Jim Mattson4fe09bc2021-06-04 10:26:04 -07003363 return 0;
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02003364 }
Gleb Natapov299018f2013-06-03 11:30:02 +03003365
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003366 if (test_and_clear_bit(KVM_APIC_INIT, &apic->pending_events)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003367 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01003368 if (kvm_vcpu_is_bsp(apic->vcpu))
3369 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3370 else
3371 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3372 }
Sean Christopherson1e17a6f2022-09-21 00:31:58 +00003373 if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events)) {
Maxim Levitskyf57ad632020-12-03 16:33:19 +02003374 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
3375 /* evaluate pending_events before reading the vector */
3376 smp_rmb();
3377 sipi_vector = apic->sipi_vector;
Wei Wang89604642024-05-07 21:31:02 +08003378 kvm_x86_call(vcpu_deliver_sipi_vector)(vcpu,
3379 sipi_vector);
Maxim Levitskyf57ad632020-12-03 16:33:19 +02003380 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3381 }
Jan Kiszka66450a22013-03-13 12:42:34 +01003382 }
Jim Mattson4fe09bc2021-06-04 10:26:04 -07003383 return 0;
Jan Kiszka66450a22013-03-13 12:42:34 +01003384}
3385
David Matlackcef84c32016-12-16 14:30:36 -08003386void kvm_lapic_exit(void)
3387{
3388 static_key_deferred_flush(&apic_hw_disabled);
Sean Christopherson9139a7a2021-10-12 17:35:54 -07003389 WARN_ON(static_branch_unlikely(&apic_hw_disabled.key));
David Matlackcef84c32016-12-16 14:30:36 -08003390 static_key_deferred_flush(&apic_sw_disabled);
Sean Christopherson9139a7a2021-10-12 17:35:54 -07003391 WARN_ON(static_branch_unlikely(&apic_sw_disabled.key));
David Matlackcef84c32016-12-16 14:30:36 -08003392}