blob: e4227ceab0c6494b6c658961c819798216c5e1a5 [file] [log] [blame]
Eddie Dong97222cc2007-09-12 10:58:04 +03001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +03009 *
10 * Authors:
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 *
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030022#include <linux/kvm.h>
23#include <linux/mm.h>
24#include <linux/highmem.h>
25#include <linux/smp.h>
26#include <linux/hrtimer.h>
27#include <linux/io.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040028#include <linux/export.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070029#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030031#include <asm/processor.h>
32#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"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030042#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020043#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030044#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030045
Marcelo Tosattib682b812009-02-10 20:41:41 -020046#ifndef CONFIG_X86_64
47#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48#else
49#define mod_64(x, y) ((x) % (y))
50#endif
51
Eddie Dong97222cc2007-09-12 10:58:04 +030052#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
Eddie Dong97222cc2007-09-12 10:58:04 +030057/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
Yi Wang354cb412018-11-08 16:48:36 +080058#define apic_debug(fmt, arg...) do {} while (0)
Eddie Dong97222cc2007-09-12 10:58:04 +030059
Eddie Dong97222cc2007-09-12 10:58:04 +030060/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050061#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030062#define LAPIC_MMIO_LENGTH (1 << 12)
63/* followed define is not in apicdef.h */
64#define APIC_SHORT_MASK 0xc0000
65#define APIC_DEST_NOSHORT 0x0
66#define APIC_DEST_MASK 0x800
67#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090068#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030069
Nadav Amit394457a2014-10-03 00:30:52 +030070#define APIC_BROADCAST 0xFF
71#define X2APIC_BROADCAST 0xFFFFFFFFul
72
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080073#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
74/* step-by-step approximation to mitigate fluctuation */
75#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
76
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030077static inline int apic_test_vector(int vec, void *bitmap)
78{
79 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
80}
81
Yang Zhang10606912013-04-11 19:21:38 +080082bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
83{
84 struct kvm_lapic *apic = vcpu->arch.apic;
85
86 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
87 apic_test_vector(vector, apic->regs + APIC_IRR);
88}
89
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030090static inline int __apic_test_and_set_vector(int vec, void *bitmap)
91{
92 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
93}
94
95static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
96{
97 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
98}
99
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300100struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300101struct static_key_deferred apic_sw_disabled __read_mostly;
102
Eddie Dong97222cc2007-09-12 10:58:04 +0300103static inline int apic_enabled(struct kvm_lapic *apic)
104{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300105 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300106}
107
Eddie Dong97222cc2007-09-12 10:58:04 +0300108#define LVT_MASK \
109 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
110
111#define LINT_MASK \
112 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
113 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
114
Radim Krčmář6e500432016-12-15 18:06:46 +0100115static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
116{
117 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
118}
119
120static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
121{
122 return apic->vcpu->vcpu_id;
123}
124
Radim Krčmáře45115b2016-07-12 22:09:19 +0200125static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
126 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
127 switch (map->mode) {
128 case KVM_APIC_MODE_X2APIC: {
129 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200130 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100131
Radim Krčmáře45115b2016-07-12 22:09:19 +0200132 if (offset <= max_apic_id) {
133 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100134
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200135 offset = array_index_nospec(offset, map->max_apic_id + 1);
Radim Krčmáře45115b2016-07-12 22:09:19 +0200136 *cluster = &map->phys_map[offset];
137 *mask = dest_id & (0xffff >> (16 - cluster_size));
138 } else {
139 *mask = 0;
140 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100141
Radim Krčmáře45115b2016-07-12 22:09:19 +0200142 return true;
143 }
144 case KVM_APIC_MODE_XAPIC_FLAT:
145 *cluster = map->xapic_flat_map;
146 *mask = dest_id & 0xff;
147 return true;
148 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100149 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200150 *mask = dest_id & 0xf;
151 return true;
152 default:
153 /* Not optimized. */
154 return false;
155 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300156}
157
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200158static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100159{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200160 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100161
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200162 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100163}
164
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300165static void recalculate_apic_map(struct kvm *kvm)
166{
167 struct kvm_apic_map *new, *old = NULL;
168 struct kvm_vcpu *vcpu;
169 int i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100170 u32 max_id = 255; /* enough space for any xAPIC ID */
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300171
172 mutex_lock(&kvm->arch.apic_map_lock);
173
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200174 kvm_for_each_vcpu(i, vcpu, kvm)
175 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100176 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200177
Michal Hockoa7c3e902017-05-08 15:57:09 -0700178 new = kvzalloc(sizeof(struct kvm_apic_map) +
Ben Gardon254272c2019-02-11 11:02:50 -0800179 sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
180 GFP_KERNEL_ACCOUNT);
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200181
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300182 if (!new)
183 goto out;
184
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200185 new->max_apic_id = max_id;
186
Nadav Amit173beed2014-11-02 11:54:54 +0200187 kvm_for_each_vcpu(i, vcpu, kvm) {
188 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200189 struct kvm_lapic **cluster;
190 u16 mask;
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100191 u32 ldr;
192 u8 xapic_id;
193 u32 x2apic_id;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300194
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100195 if (!kvm_apic_present(vcpu))
196 continue;
197
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100198 xapic_id = kvm_xapic_id(apic);
199 x2apic_id = kvm_x2apic_id(apic);
200
201 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
202 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
203 x2apic_id <= new->max_apic_id)
204 new->phys_map[x2apic_id] = apic;
205 /*
206 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
207 * prevent them from masking VCPUs with APIC ID <= 0xff.
208 */
209 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
210 new->phys_map[xapic_id] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100211
Radim Krčmář6e500432016-12-15 18:06:46 +0100212 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
213
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100214 if (apic_x2apic_mode(apic)) {
215 new->mode |= KVM_APIC_MODE_X2APIC;
216 } else if (ldr) {
217 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500218 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100219 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
220 else
221 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
222 }
223
Radim Krčmáře45115b2016-07-12 22:09:19 +0200224 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100225 continue;
226
Radim Krčmáře45115b2016-07-12 22:09:19 +0200227 if (mask)
228 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300229 }
230out:
231 old = rcu_dereference_protected(kvm->arch.apic_map,
232 lockdep_is_held(&kvm->arch.apic_map_lock));
233 rcu_assign_pointer(kvm->arch.apic_map, new);
234 mutex_unlock(&kvm->arch.apic_map_lock);
235
236 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200237 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800238
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700239 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300240}
241
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300242static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
243{
Radim Krčmáře4627552014-10-30 15:06:45 +0100244 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300245
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500246 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100247
248 if (enabled != apic->sw_enabled) {
249 apic->sw_enabled = enabled;
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800250 if (enabled)
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300251 static_key_slow_dec_deferred(&apic_sw_disabled);
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800252 else
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300253 static_key_slow_inc(&apic_sw_disabled.key);
254 }
255}
256
Radim Krčmářa92e2542016-07-12 22:09:22 +0200257static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300258{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500259 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300260 recalculate_apic_map(apic->vcpu->kvm);
261}
262
263static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
264{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500265 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300266 recalculate_apic_map(apic->vcpu->kvm);
267}
268
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000269static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
270{
271 return ((id >> 4) << 16) | (1 << (id & 0xf));
272}
273
Radim Krčmářa92e2542016-07-12 22:09:22 +0200274static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200275{
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000276 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200277
Radim Krčmář6e500432016-12-15 18:06:46 +0100278 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
279
Radim Krčmářa92e2542016-07-12 22:09:22 +0200280 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500281 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200282 recalculate_apic_map(apic->vcpu->kvm);
283}
284
Eddie Dong97222cc2007-09-12 10:58:04 +0300285static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
286{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500287 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300288}
289
290static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
291{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500292 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300293}
294
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800295static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
296{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100297 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800298}
299
Eddie Dong97222cc2007-09-12 10:58:04 +0300300static inline int apic_lvtt_period(struct kvm_lapic *apic)
301{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100302 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800303}
304
305static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
306{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100307 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300308}
309
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200310static inline int apic_lvt_nmi_mode(u32 lvt_val)
311{
312 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
313}
314
Gleb Natapovfc61b802009-07-05 17:39:35 +0300315void kvm_apic_set_version(struct kvm_vcpu *vcpu)
316{
317 struct kvm_lapic *apic = vcpu->arch.apic;
318 struct kvm_cpuid_entry2 *feat;
319 u32 v = APIC_VERSION;
320
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100321 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300322 return;
323
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100324 /*
325 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
326 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
327 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
328 * version first and level-triggered interrupts never get EOIed in
329 * IOAPIC.
330 */
Gleb Natapovfc61b802009-07-05 17:39:35 +0300331 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100332 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
333 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300334 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500335 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300336}
337
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500338static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800339 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300340 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
341 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
342 LINT_MASK, LINT_MASK, /* LVT0-1 */
343 LVT_MASK /* LVTERR */
344};
345
346static int find_highest_vector(void *bitmap)
347{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900348 int vec;
349 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300350
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900351 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
352 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
353 reg = bitmap + REG_POS(vec);
354 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100355 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900356 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300357
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900358 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300359}
360
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300361static u8 count_vectors(void *bitmap)
362{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900363 int vec;
364 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300365 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900366
367 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
368 reg = bitmap + REG_POS(vec);
369 count += hweight32(*reg);
370 }
371
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300372 return count;
373}
374
Liran Alone7387b02017-12-24 18:12:54 +0200375bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Yang Zhanga20ed542013-04-11 19:25:15 +0800376{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100377 u32 i, vec;
Liran Alone7387b02017-12-24 18:12:54 +0200378 u32 pir_val, irr_val, prev_irr_val;
379 int max_updated_irr;
380
381 max_updated_irr = -1;
382 *max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800383
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100384 for (i = vec = 0; i <= 7; i++, vec += 32) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200385 pir_val = READ_ONCE(pir[i]);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100386 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
Paolo Bonziniad361092016-09-20 16:15:05 +0200387 if (pir_val) {
Liran Alone7387b02017-12-24 18:12:54 +0200388 prev_irr_val = irr_val;
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100389 irr_val |= xchg(&pir[i], 0);
390 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
Liran Alone7387b02017-12-24 18:12:54 +0200391 if (prev_irr_val != irr_val) {
392 max_updated_irr =
393 __fls(irr_val ^ prev_irr_val) + vec;
394 }
Paolo Bonziniad361092016-09-20 16:15:05 +0200395 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100396 if (irr_val)
Liran Alone7387b02017-12-24 18:12:54 +0200397 *max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800398 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100399
Liran Alone7387b02017-12-24 18:12:54 +0200400 return ((max_updated_irr != -1) &&
401 (max_updated_irr == *max_irr));
Yang Zhanga20ed542013-04-11 19:25:15 +0800402}
Wincy Van705699a2015-02-03 23:58:17 +0800403EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
404
Liran Alone7387b02017-12-24 18:12:54 +0200405bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
Wincy Van705699a2015-02-03 23:58:17 +0800406{
407 struct kvm_lapic *apic = vcpu->arch.apic;
408
Liran Alone7387b02017-12-24 18:12:54 +0200409 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
Wincy Van705699a2015-02-03 23:58:17 +0800410}
Yang Zhanga20ed542013-04-11 19:25:15 +0800411EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
412
Gleb Natapov33e4c682009-06-11 11:06:51 +0300413static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300414{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300415 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300416}
417
418static inline int apic_find_highest_irr(struct kvm_lapic *apic)
419{
420 int result;
421
Yang Zhangc7c9c562013-01-25 10:18:51 +0800422 /*
423 * Note that irr_pending is just a hint. It will be always
424 * true with virtual interrupt delivery enabled.
425 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300426 if (!apic->irr_pending)
427 return -1;
428
429 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300430 ASSERT(result == -1 || result >= 16);
431
432 return result;
433}
434
Gleb Natapov33e4c682009-06-11 11:06:51 +0300435static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
436{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800437 struct kvm_vcpu *vcpu;
438
439 vcpu = apic->vcpu;
440
Andrey Smetanind62caab2015-11-10 15:36:33 +0300441 if (unlikely(vcpu->arch.apicv_active)) {
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100442 /* need to update RVI */
Wei Yangee171d22019-03-31 19:17:22 -0700443 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100444 kvm_x86_ops->hwapic_irr_update(vcpu,
445 apic_find_highest_irr(apic));
Nadav Amitf210f752014-11-16 23:49:07 +0200446 } else {
447 apic->irr_pending = false;
Wei Yangee171d22019-03-31 19:17:22 -0700448 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Nadav Amitf210f752014-11-16 23:49:07 +0200449 if (apic_search_irr(apic) != -1)
450 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800451 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300452}
453
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300454static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
455{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800456 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200457
Wanpeng Li56cc2402014-08-05 12:42:24 +0800458 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
459 return;
460
461 vcpu = apic->vcpu;
462
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300463 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800464 * With APIC virtualization enabled, all caching is disabled
465 * because the processor can modify ISR under the hood. Instead
466 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300467 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300468 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200469 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800470 else {
471 ++apic->isr_count;
472 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
473 /*
474 * ISR (in service register) bit is set when injecting an interrupt.
475 * The highest vector is injected. Thus the latest bit set matches
476 * the highest bit in ISR.
477 */
478 apic->highest_isr_cache = vec;
479 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300480}
481
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200482static inline int apic_find_highest_isr(struct kvm_lapic *apic)
483{
484 int result;
485
486 /*
487 * Note that isr_count is always 1, and highest_isr_cache
488 * is always -1, with APIC virtualization enabled.
489 */
490 if (!apic->isr_count)
491 return -1;
492 if (likely(apic->highest_isr_cache != -1))
493 return apic->highest_isr_cache;
494
495 result = find_highest_vector(apic->regs + APIC_ISR);
496 ASSERT(result == -1 || result >= 16);
497
498 return result;
499}
500
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300501static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
502{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200503 struct kvm_vcpu *vcpu;
504 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
505 return;
506
507 vcpu = apic->vcpu;
508
509 /*
510 * We do get here for APIC virtualization enabled if the guest
511 * uses the Hyper-V APIC enlightenment. In this case we may need
512 * to trigger a new interrupt delivery by writing the SVI field;
513 * on the other hand isr_count and highest_isr_cache are unused
514 * and must be left alone.
515 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300516 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200517 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200518 apic_find_highest_isr(apic));
519 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300520 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200521 BUG_ON(apic->isr_count < 0);
522 apic->highest_isr_cache = -1;
523 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300524}
525
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800526int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
527{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300528 /* This may race with setting of irr in __apic_accept_irq() and
529 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
530 * will cause vmexit immediately and the value will be recalculated
531 * on the next vmentry.
532 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100533 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800534}
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100535EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800536
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200537static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800538 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100539 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200540
Yang Zhangb4f22252013-04-11 19:21:37 +0800541int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100542 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300543{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800544 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800545
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200546 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800547 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300548}
549
Wanpeng Li4180bf12018-07-23 14:39:54 +0800550int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800551 unsigned long ipi_bitmap_high, u32 min,
Wanpeng Li4180bf12018-07-23 14:39:54 +0800552 unsigned long icr, int op_64_bit)
553{
554 int i;
555 struct kvm_apic_map *map;
556 struct kvm_vcpu *vcpu;
557 struct kvm_lapic_irq irq = {0};
558 int cluster_size = op_64_bit ? 64 : 32;
559 int count = 0;
560
561 irq.vector = icr & APIC_VECTOR_MASK;
562 irq.delivery_mode = icr & APIC_MODE_MASK;
563 irq.level = (icr & APIC_INT_ASSERT) != 0;
564 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
565
566 if (icr & APIC_DEST_MASK)
567 return -KVM_EINVAL;
568 if (icr & APIC_SHORT_MASK)
569 return -KVM_EINVAL;
570
571 rcu_read_lock();
572 map = rcu_dereference(kvm->arch.apic_map);
573
Wanpeng Li38ab0122018-11-20 09:39:30 +0800574 if (unlikely(!map)) {
575 count = -EOPNOTSUPP;
576 goto out;
577 }
578
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800579 if (min > map->max_apic_id)
580 goto out;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800581 /* Bits above cluster_size are masked in the caller. */
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800582 for_each_set_bit(i, &ipi_bitmap_low,
583 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
584 if (map->phys_map[min + i]) {
585 vcpu = map->phys_map[min + i]->vcpu;
586 count += kvm_apic_set_irq(vcpu, &irq, NULL);
587 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800588 }
589
590 min += cluster_size;
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800591
592 if (min > map->max_apic_id)
593 goto out;
594
595 for_each_set_bit(i, &ipi_bitmap_high,
596 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
597 if (map->phys_map[min + i]) {
598 vcpu = map->phys_map[min + i]->vcpu;
599 count += kvm_apic_set_irq(vcpu, &irq, NULL);
600 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800601 }
602
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800603out:
Wanpeng Li4180bf12018-07-23 14:39:54 +0800604 rcu_read_unlock();
605 return count;
606}
607
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300608static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
609{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200610
611 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
612 sizeof(val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300613}
614
615static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
616{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200617
618 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
619 sizeof(*val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300620}
621
622static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
623{
624 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
625}
626
627static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
628{
629 u8 val;
630 if (pv_eoi_get_user(vcpu, &val) < 0)
631 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800632 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300633 return val & 0x1;
634}
635
636static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
637{
638 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
639 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800640 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300641 return;
642 }
643 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
644}
645
646static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
647{
648 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
649 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800650 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300651 return;
652 }
653 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
654}
655
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100656static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
657{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100658 int highest_irr;
Liran Alonfa59cc02017-12-24 18:12:53 +0200659 if (apic->vcpu->arch.apicv_active)
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100660 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
661 else
662 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100663 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
664 return -1;
665 return highest_irr;
666}
667
668static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300669{
Avi Kivity3842d132010-07-27 12:30:24 +0300670 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300671 int isr;
672
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500673 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
674 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300675 isr = apic_find_highest_isr(apic);
676 isrv = (isr != -1) ? isr : 0;
677
678 if ((tpr & 0xf0) >= (isrv & 0xf0))
679 ppr = tpr & 0xff;
680 else
681 ppr = isrv & 0xf0;
682
683 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
684 apic, ppr, isr, isrv);
685
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100686 *new_ppr = ppr;
687 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500688 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100689
690 return ppr < old_ppr;
691}
692
693static void apic_update_ppr(struct kvm_lapic *apic)
694{
695 u32 ppr;
696
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100697 if (__apic_update_ppr(apic, &ppr) &&
698 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100699 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300700}
701
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100702void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
703{
704 apic_update_ppr(vcpu->arch.apic);
705}
706EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
707
Eddie Dong97222cc2007-09-12 10:58:04 +0300708static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
709{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500710 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300711 apic_update_ppr(apic);
712}
713
Radim Krčmář03d22492015-02-12 19:41:31 +0100714static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300715{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100716 return mda == (apic_x2apic_mode(apic) ?
717 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300718}
719
Radim Krčmář03d22492015-02-12 19:41:31 +0100720static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300721{
Radim Krčmář03d22492015-02-12 19:41:31 +0100722 if (kvm_apic_broadcast(apic, mda))
723 return true;
724
725 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100726 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100727
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100728 /*
729 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
730 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
731 * this allows unique addressing of VCPUs with APIC ID over 0xff.
732 * The 0xff condition is needed because writeable xAPIC ID.
733 */
734 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
735 return true;
736
Radim Krčmářb4535b52016-12-15 18:06:47 +0100737 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300738}
739
Radim Krčmář52c233a2015-01-29 22:48:48 +0100740static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300741{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300742 u32 logical_id;
743
Nadav Amit394457a2014-10-03 00:30:52 +0300744 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100745 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300746
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500747 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300748
Radim Krčmář9368b562015-01-29 22:48:49 +0100749 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100750 return ((logical_id >> 16) == (mda >> 16))
751 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100752
753 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300754
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500755 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300756 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100757 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300758 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100759 return ((logical_id >> 4) == (mda >> 4))
760 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300761 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200762 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500763 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100764 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300765 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300766}
767
Radim Krčmářc5192652016-07-12 22:09:28 +0200768/* The KVM local APIC implementation has two quirks:
769 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100770 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
771 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
772 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200773 *
774 * - in-kernel IOAPIC messages have to be delivered directly to
775 * x2APIC, because the kernel does not support interrupt remapping.
776 * In order to support broadcast without interrupt remapping, x2APIC
777 * rewrites the destination of non-IPI messages from APIC_BROADCAST
778 * to X2APIC_BROADCAST.
779 *
780 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
781 * important when userspace wants to use x2APIC-format MSIs, because
782 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100783 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200784static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
785 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100786{
787 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100788
Radim Krčmářc5192652016-07-12 22:09:28 +0200789 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100790 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100791 return X2APIC_BROADCAST;
792
Radim Krčmářb4535b52016-12-15 18:06:47 +0100793 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100794}
795
Radim Krčmář52c233a2015-01-29 22:48:48 +0100796bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300797 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300798{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800799 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200800 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300801
802 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200803 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300804 target, source, dest, dest_mode, short_hand);
805
Zachary Amsdenbd371392010-06-14 11:42:15 -1000806 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300807 switch (short_hand) {
808 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100809 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100810 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200811 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100812 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300813 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100814 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300815 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100816 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300817 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100818 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300819 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200820 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
821 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100822 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300823 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300824}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500825EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300826
Feng Wu520040142016-01-25 16:53:33 +0800827int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
828 const unsigned long *bitmap, u32 bitmap_size)
829{
830 u32 mod;
831 int i, idx = -1;
832
833 mod = vector % dest_vcpus;
834
835 for (i = 0; i <= mod; i++) {
836 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
837 BUG_ON(idx == bitmap_size);
838 }
839
840 return idx;
841}
842
Radim Krčmář4efd8052016-02-12 15:00:15 +0100843static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
844{
845 if (!kvm->arch.disabled_lapic_found) {
846 kvm->arch.disabled_lapic_found = true;
847 printk(KERN_INFO
848 "Disabled LAPIC found during irq injection\n");
849 }
850}
851
Radim Krčmářc5192652016-07-12 22:09:28 +0200852static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
853 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
854{
855 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
856 if ((irq->dest_id == APIC_BROADCAST &&
857 map->mode != KVM_APIC_MODE_X2APIC))
858 return true;
859 if (irq->dest_id == X2APIC_BROADCAST)
860 return true;
861 } else {
862 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
863 if (irq->dest_id == (x2apic_ipi ?
864 X2APIC_BROADCAST : APIC_BROADCAST))
865 return true;
866 }
867
868 return false;
869}
870
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200871/* Return true if the interrupt can be handled by using *bitmap as index mask
872 * for valid destinations in *dst array.
873 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
874 * Note: we may have zero kvm_lapic destinations when we return true, which
875 * means that the interrupt should be dropped. In this case, *bitmap would be
876 * zero and *dst undefined.
877 */
878static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
879 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
880 struct kvm_apic_map *map, struct kvm_lapic ***dst,
881 unsigned long *bitmap)
882{
883 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200884
885 if (irq->shorthand == APIC_DEST_SELF && src) {
886 *dst = src;
887 *bitmap = 1;
888 return true;
889 } else if (irq->shorthand)
890 return false;
891
Radim Krčmářc5192652016-07-12 22:09:28 +0200892 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200893 return false;
894
895 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200896 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200897 *bitmap = 0;
898 } else {
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200899 u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
900 *dst = &map->phys_map[dest_id];
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200901 *bitmap = 1;
902 }
903 return true;
904 }
905
Radim Krčmáře45115b2016-07-12 22:09:19 +0200906 *bitmap = 0;
907 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
908 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200909 return false;
910
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200911 if (!kvm_lowest_prio_delivery(irq))
912 return true;
913
914 if (!kvm_vector_hashing_enabled()) {
915 lowest = -1;
916 for_each_set_bit(i, bitmap, 16) {
917 if (!(*dst)[i])
918 continue;
919 if (lowest < 0)
920 lowest = i;
921 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
922 (*dst)[lowest]->vcpu) < 0)
923 lowest = i;
924 }
925 } else {
926 if (!*bitmap)
927 return true;
928
929 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
930 bitmap, 16);
931
932 if (!(*dst)[lowest]) {
933 kvm_apic_disabled_lapic_found(kvm);
934 *bitmap = 0;
935 return true;
936 }
937 }
938
939 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
940
941 return true;
942}
943
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300944bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100945 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300946{
947 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200948 unsigned long bitmap;
949 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300950 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200951 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300952
953 *r = -1;
954
955 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800956 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300957 return true;
958 }
959
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300960 rcu_read_lock();
961 map = rcu_dereference(kvm->arch.apic_map);
962
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200963 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200964 if (ret) {
965 *r = 0;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200966 for_each_set_bit(i, &bitmap, 16) {
967 if (!dst[i])
968 continue;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200969 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100970 }
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200971 }
Radim Krčmář3548a252015-02-12 19:41:33 +0100972
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300973 rcu_read_unlock();
974 return ret;
975}
976
Feng Wu6228a0d2016-01-25 16:53:34 +0800977/*
978 * This routine tries to handler interrupts in posted mode, here is how
979 * it deals with different cases:
980 * - For single-destination interrupts, handle it in posted mode
981 * - Else if vector hashing is enabled and it is a lowest-priority
982 * interrupt, handle it in posted mode and use the following mechanism
983 * to find the destinaiton vCPU.
984 * 1. For lowest-priority interrupts, store all the possible
985 * destination vCPUs in an array.
986 * 2. Use "guest vector % max number of destination vCPUs" to find
987 * the right destination vCPU in the array for the lowest-priority
988 * interrupt.
989 * - Otherwise, use remapped mode to inject the interrupt.
990 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800991bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
992 struct kvm_vcpu **dest_vcpu)
993{
994 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200995 unsigned long bitmap;
996 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800997 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800998
999 if (irq->shorthand)
1000 return false;
1001
1002 rcu_read_lock();
1003 map = rcu_dereference(kvm->arch.apic_map);
1004
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001005 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1006 hweight16(bitmap) == 1) {
1007 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +08001008
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001009 if (dst[i]) {
1010 *dest_vcpu = dst[i]->vcpu;
1011 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +08001012 }
Feng Wu8feb4a02015-09-18 22:29:47 +08001013 }
1014
Feng Wu8feb4a02015-09-18 22:29:47 +08001015 rcu_read_unlock();
1016 return ret;
1017}
1018
Eddie Dong97222cc2007-09-12 10:58:04 +03001019/*
1020 * Add a pending IRQ into lapic.
1021 * Return 1 if successfully added and 0 if discarded.
1022 */
1023static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +08001024 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001025 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +03001026{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001027 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +03001028 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +03001029
Paolo Bonzinia183b632014-09-11 11:51:02 +02001030 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1031 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001032 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001033 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +02001034 vcpu->arch.apic_arb_prio++;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001035 /* fall through */
Gleb Natapove1035712009-03-05 16:34:59 +02001036 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001037 if (unlikely(trig_mode && !level))
1038 break;
1039
Eddie Dong97222cc2007-09-12 10:58:04 +03001040 /* FIXME add logic for vcpu on reset */
1041 if (unlikely(!apic_enabled(apic)))
1042 break;
1043
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001044 result = 1;
1045
Joerg Roedel9daa5002016-02-29 16:04:44 +01001046 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001047 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +01001048 dest_map->vectors[vcpu->vcpu_id] = vector;
1049 }
Avi Kivitya5d36f82009-12-29 12:42:16 +02001050
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001051 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1052 if (trig_mode)
Wei Yangee171d22019-03-31 19:17:22 -07001053 kvm_lapic_set_vector(vector,
1054 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001055 else
Wei Yangee171d22019-03-31 19:17:22 -07001056 kvm_lapic_clear_vector(vector,
1057 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001058 }
1059
Andrey Smetanind62caab2015-11-10 15:36:33 +03001060 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +08001061 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001062 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001063 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +08001064
1065 kvm_make_request(KVM_REQ_EVENT, vcpu);
1066 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001067 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001068 break;
1069
1070 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +05301071 result = 1;
1072 vcpu->arch.pv.pv_unhalted = 1;
1073 kvm_make_request(KVM_REQ_EVENT, vcpu);
1074 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001075 break;
1076
1077 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +02001078 result = 1;
1079 kvm_make_request(KVM_REQ_SMI, vcpu);
1080 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001081 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001082
Eddie Dong97222cc2007-09-12 10:58:04 +03001083 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001084 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001085 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001086 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001087 break;
1088
1089 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001090 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001091 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001092 /* assumes that there are only KVM_APIC_INIT/SIPI */
1093 apic->pending_events = (1UL << KVM_APIC_INIT);
1094 /* make sure pending_events is visible before sending
1095 * the request */
1096 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +03001097 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001098 kvm_vcpu_kick(vcpu);
1099 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001100 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
1101 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +03001102 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001103 break;
1104
1105 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001106 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
1107 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +01001108 result = 1;
1109 apic->sipi_vector = vector;
1110 /* make sure sipi_vector is visible for the receiver */
1111 smp_wmb();
1112 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1113 kvm_make_request(KVM_REQ_EVENT, vcpu);
1114 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001115 break;
1116
Jan Kiszka23930f92008-09-26 09:30:52 +02001117 case APIC_DM_EXTINT:
1118 /*
1119 * Should only be called by kvm_apic_local_deliver() with LVT0,
1120 * before NMI watchdog was enabled. Already handled by
1121 * kvm_apic_accept_pic_intr().
1122 */
1123 break;
1124
Eddie Dong97222cc2007-09-12 10:58:04 +03001125 default:
1126 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1127 delivery_mode);
1128 break;
1129 }
1130 return result;
1131}
1132
Gleb Natapove1035712009-03-05 16:34:59 +02001133int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001134{
Gleb Natapove1035712009-03-05 16:34:59 +02001135 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001136}
1137
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001138static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1139{
Andrey Smetanin63086302015-11-10 15:36:32 +03001140 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001141}
1142
Yang Zhangc7c9c562013-01-25 10:18:51 +08001143static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1144{
Steve Rutherford7543a632015-07-29 23:21:41 -07001145 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001146
Steve Rutherford7543a632015-07-29 23:21:41 -07001147 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1148 if (!kvm_ioapic_handles_vector(apic, vector))
1149 return;
1150
1151 /* Request a KVM exit to inform the userspace IOAPIC. */
1152 if (irqchip_split(apic->vcpu->kvm)) {
1153 apic->vcpu->arch.pending_ioapic_eoi = vector;
1154 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1155 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001156 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001157
1158 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1159 trigger_mode = IOAPIC_LEVEL_TRIG;
1160 else
1161 trigger_mode = IOAPIC_EDGE_TRIG;
1162
1163 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001164}
1165
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001166static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001167{
1168 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001169
1170 trace_kvm_eoi(apic, vector);
1171
Eddie Dong97222cc2007-09-12 10:58:04 +03001172 /*
1173 * Not every write EOI will has corresponding ISR,
1174 * one example is when Kernel check timer on setup_IO_APIC
1175 */
1176 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001177 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001178
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001179 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001180 apic_update_ppr(apic);
1181
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001182 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1183 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1184
Yang Zhangc7c9c562013-01-25 10:18:51 +08001185 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001186 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001187 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001188}
1189
Yang Zhangc7c9c562013-01-25 10:18:51 +08001190/*
1191 * this interface assumes a trap-like exit, which has already finished
1192 * desired side effect including vISR and vPPR update.
1193 */
1194void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1195{
1196 struct kvm_lapic *apic = vcpu->arch.apic;
1197
1198 trace_kvm_eoi(apic, vector);
1199
1200 kvm_ioapic_send_eoi(apic, vector);
1201 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1202}
1203EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1204
Eddie Dong97222cc2007-09-12 10:58:04 +03001205static void apic_send_ipi(struct kvm_lapic *apic)
1206{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001207 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1208 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001209 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001210
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001211 irq.vector = icr_low & APIC_VECTOR_MASK;
1212 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1213 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001214 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001215 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1216 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001217 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001218 if (apic_x2apic_mode(apic))
1219 irq.dest_id = icr_high;
1220 else
1221 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001222
Gleb Natapov1000ff82009-07-07 16:00:57 +03001223 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1224
Eddie Dong97222cc2007-09-12 10:58:04 +03001225 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1226 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001227 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1228 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001229 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001230 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001231 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001232
Yang Zhangb4f22252013-04-11 19:21:37 +08001233 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001234}
1235
1236static u32 apic_get_tmcct(struct kvm_lapic *apic)
1237{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001238 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001239 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001240 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001241
1242 ASSERT(apic != NULL);
1243
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001244 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001245 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001246 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001247 return 0;
1248
Paolo Bonzini55878592016-10-25 15:23:49 +02001249 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001250 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001251 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001252 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001253
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001254 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1255 tmcct = div64_u64(ns,
1256 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001257
1258 return tmcct;
1259}
1260
Avi Kivityb209749f2007-10-22 16:50:39 +02001261static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1262{
1263 struct kvm_vcpu *vcpu = apic->vcpu;
1264 struct kvm_run *run = vcpu->run;
1265
Avi Kivitya8eeb042010-05-10 12:34:53 +03001266 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001267 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001268 run->tpr_access.is_write = write;
1269}
1270
1271static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1272{
1273 if (apic->vcpu->arch.tpr_access_reporting)
1274 __report_tpr_access(apic, write);
1275}
1276
Eddie Dong97222cc2007-09-12 10:58:04 +03001277static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1278{
1279 u32 val = 0;
1280
1281 if (offset >= LAPIC_MMIO_LENGTH)
1282 return 0;
1283
1284 switch (offset) {
1285 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001286 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001287 break;
1288
1289 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001290 if (apic_lvtt_tscdeadline(apic))
1291 return 0;
1292
Eddie Dong97222cc2007-09-12 10:58:04 +03001293 val = apic_get_tmcct(apic);
1294 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001295 case APIC_PROCPRI:
1296 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001297 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001298 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001299 case APIC_TASKPRI:
1300 report_tpr_access(apic, false);
1301 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001302 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001303 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001304 break;
1305 }
1306
1307 return val;
1308}
1309
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001310static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1311{
1312 return container_of(dev, struct kvm_lapic, dev);
1313}
1314
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001315#define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1316#define APIC_REGS_MASK(first, count) \
1317 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1318
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001319int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001320 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001321{
Eddie Dong97222cc2007-09-12 10:58:04 +03001322 unsigned char alignment = offset & 0xf;
1323 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001324 /* this bitmask has a bit cleared for each reserved register */
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001325 u64 valid_reg_mask =
1326 APIC_REG_MASK(APIC_ID) |
1327 APIC_REG_MASK(APIC_LVR) |
1328 APIC_REG_MASK(APIC_TASKPRI) |
1329 APIC_REG_MASK(APIC_PROCPRI) |
1330 APIC_REG_MASK(APIC_LDR) |
1331 APIC_REG_MASK(APIC_DFR) |
1332 APIC_REG_MASK(APIC_SPIV) |
1333 APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1334 APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1335 APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1336 APIC_REG_MASK(APIC_ESR) |
1337 APIC_REG_MASK(APIC_ICR) |
1338 APIC_REG_MASK(APIC_ICR2) |
1339 APIC_REG_MASK(APIC_LVTT) |
1340 APIC_REG_MASK(APIC_LVTTHMR) |
1341 APIC_REG_MASK(APIC_LVTPC) |
1342 APIC_REG_MASK(APIC_LVT0) |
1343 APIC_REG_MASK(APIC_LVT1) |
1344 APIC_REG_MASK(APIC_LVTERR) |
1345 APIC_REG_MASK(APIC_TMICT) |
1346 APIC_REG_MASK(APIC_TMCCT) |
1347 APIC_REG_MASK(APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001348
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001349 /* ARBPRI is not valid on x2APIC */
1350 if (!apic_x2apic_mode(apic))
1351 valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001352
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001353 if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001354 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1355 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001356 return 1;
1357 }
1358
Eddie Dong97222cc2007-09-12 10:58:04 +03001359 result = __apic_read(apic, offset & ~0xf);
1360
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001361 trace_kvm_apic_read(offset, result);
1362
Eddie Dong97222cc2007-09-12 10:58:04 +03001363 switch (len) {
1364 case 1:
1365 case 2:
1366 case 4:
1367 memcpy(data, (char *)&result + alignment, len);
1368 break;
1369 default:
1370 printk(KERN_ERR "Local APIC read with len = %x, "
1371 "should be 1,2, or 4 instead\n", len);
1372 break;
1373 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001374 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001375}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001376EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001377
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001378static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1379{
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001380 return addr >= apic->base_address &&
1381 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001382}
1383
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001384static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001385 gpa_t address, int len, void *data)
1386{
1387 struct kvm_lapic *apic = to_lapic(this);
1388 u32 offset = address - apic->base_address;
1389
1390 if (!apic_mmio_in_range(apic, address))
1391 return -EOPNOTSUPP;
1392
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001393 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1394 if (!kvm_check_has_quirk(vcpu->kvm,
1395 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1396 return -EOPNOTSUPP;
1397
1398 memset(data, 0xff, len);
1399 return 0;
1400 }
1401
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001402 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001403
1404 return 0;
1405}
1406
Eddie Dong97222cc2007-09-12 10:58:04 +03001407static void update_divide_count(struct kvm_lapic *apic)
1408{
1409 u32 tmp1, tmp2, tdcr;
1410
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001411 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001412 tmp1 = tdcr & 0xf;
1413 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001414 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001415
1416 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001417 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001418}
1419
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001420static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1421{
1422 /*
1423 * Do not allow the guest to program periodic timers with small
1424 * interval, since the hrtimers are not throttled by the host
1425 * scheduler.
1426 */
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001427 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001428 s64 min_period = min_timer_period_us * 1000LL;
1429
1430 if (apic->lapic_timer.period < min_period) {
1431 pr_info_ratelimited(
1432 "kvm: vcpu %i: requested %lld ns "
1433 "lapic timer period limited to %lld ns\n",
1434 apic->vcpu->vcpu_id,
1435 apic->lapic_timer.period, min_period);
1436 apic->lapic_timer.period = min_period;
1437 }
1438 }
1439}
1440
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001441static void apic_update_lvtt(struct kvm_lapic *apic)
1442{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001443 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001444 apic->lapic_timer.timer_mode_mask;
1445
1446 if (apic->lapic_timer.timer_mode != timer_mode) {
Wanpeng Lic69518c2017-10-05 03:53:51 -07001447 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001448 APIC_LVT_TIMER_TSCDEADLINE)) {
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001449 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmář44275932017-10-06 19:25:55 +02001450 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1451 apic->lapic_timer.period = 0;
1452 apic->lapic_timer.tscdeadline = 0;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001453 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001454 apic->lapic_timer.timer_mode = timer_mode;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001455 limit_periodic_timer_frequency(apic);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001456 }
1457}
1458
Radim Krčmář5d87db72014-10-10 19:15:08 +02001459static void apic_timer_expired(struct kvm_lapic *apic)
1460{
1461 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001462 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001463 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001464
Radim Krčmář5d87db72014-10-10 19:15:08 +02001465 if (atomic_read(&apic->lapic_timer.pending))
1466 return;
1467
1468 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001469 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001470
Davidlohr Buesocc1b4682017-09-13 13:08:20 -07001471 /*
1472 * For x86, the atomic_inc() is serialized, thus
1473 * using swait_active() is safe.
1474 */
Marcelo Tosatti85773702016-02-19 09:46:39 +01001475 if (swait_active(q))
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001476 swake_up_one(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001477
Sean Christophersonee66e452019-04-16 13:32:44 -07001478 if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001479 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1480}
1481
1482/*
1483 * On APICv, this test will cause a busy wait
1484 * during a higher-priority task.
1485 */
1486
1487static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1488{
1489 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001490 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001491
1492 if (kvm_apic_hw_enabled(apic)) {
1493 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001494 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001495
Andrey Smetanind62caab2015-11-10 15:36:33 +03001496 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001497 bitmap = apic->regs + APIC_IRR;
1498
1499 if (apic_test_vector(vec, bitmap))
1500 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001501 }
1502 return false;
1503}
1504
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001505static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1506{
1507 u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1508
1509 /*
1510 * If the guest TSC is running at a different ratio than the host, then
1511 * convert the delay to nanoseconds to achieve an accurate delay. Note
1512 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1513 * always for VMX enabled hardware.
1514 */
1515 if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) {
1516 __delay(min(guest_cycles,
1517 nsec_to_cycles(vcpu, timer_advance_ns)));
1518 } else {
1519 u64 delay_ns = guest_cycles * 1000000ULL;
1520 do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1521 ndelay(min_t(u32, delay_ns, timer_advance_ns));
1522 }
1523}
1524
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001525static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
Wanpeng Liec0671d2019-05-20 16:18:08 +08001526 s64 advance_expire_delta)
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001527{
1528 struct kvm_lapic *apic = vcpu->arch.apic;
Sean Christopherson39497d72019-04-17 10:15:32 -07001529 u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001530 u64 ns;
1531
1532 /* too early */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001533 if (advance_expire_delta < 0) {
1534 ns = -advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001535 do_div(ns, vcpu->arch.virtual_tsc_khz);
1536 timer_advance_ns -= min((u32)ns,
1537 timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1538 } else {
1539 /* too late */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001540 ns = advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001541 do_div(ns, vcpu->arch.virtual_tsc_khz);
1542 timer_advance_ns += min((u32)ns,
1543 timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1544 }
1545
Wanpeng Liec0671d2019-05-20 16:18:08 +08001546 if (abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001547 apic->lapic_timer.timer_advance_adjust_done = true;
1548 if (unlikely(timer_advance_ns > 5000)) {
1549 timer_advance_ns = 0;
1550 apic->lapic_timer.timer_advance_adjust_done = true;
1551 }
1552 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1553}
1554
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08001555void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001556{
1557 struct kvm_lapic *apic = vcpu->arch.apic;
1558 u64 guest_tsc, tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001559
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001560 if (apic->lapic_timer.expired_tscdeadline == 0)
1561 return;
1562
1563 if (!lapic_timer_int_injected(vcpu))
1564 return;
1565
1566 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1567 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001568 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Wanpeng Liec0671d2019-05-20 16:18:08 +08001569 apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001570
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001571 if (guest_tsc < tsc_deadline)
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001572 __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
Wanpeng Li3b8a5df2018-10-09 09:02:08 +08001573
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001574 if (unlikely(!apic->lapic_timer.timer_advance_adjust_done))
Wanpeng Liec0671d2019-05-20 16:18:08 +08001575 adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001576}
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08001577EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001578
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001579static void start_sw_tscdeadline(struct kvm_lapic *apic)
1580{
Sean Christopherson39497d72019-04-17 10:15:32 -07001581 struct kvm_timer *ktimer = &apic->lapic_timer;
1582 u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001583 u64 ns = 0;
1584 ktime_t expire;
1585 struct kvm_vcpu *vcpu = apic->vcpu;
1586 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1587 unsigned long flags;
1588 ktime_t now;
1589
1590 if (unlikely(!tscdeadline || !this_tsc_khz))
1591 return;
1592
1593 local_irq_save(flags);
1594
Paolo Bonzini55878592016-10-25 15:23:49 +02001595 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001596 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Liran Alonc09d65d2019-04-16 20:36:34 +03001597
1598 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1599 do_div(ns, this_tsc_khz);
1600
1601 if (likely(tscdeadline > guest_tsc) &&
Sean Christopherson39497d72019-04-17 10:15:32 -07001602 likely(ns > apic->lapic_timer.timer_advance_ns)) {
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001603 expire = ktime_add_ns(now, ns);
Sean Christopherson39497d72019-04-17 10:15:32 -07001604 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
1605 hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_PINNED);
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001606 } else
1607 apic_timer_expired(apic);
1608
1609 local_irq_restore(flags);
1610}
1611
Wanpeng Lic301b902017-10-06 07:38:32 -07001612static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1613{
1614 ktime_t now, remaining;
1615 u64 ns_remaining_old, ns_remaining_new;
1616
1617 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1618 * APIC_BUS_CYCLE_NS * apic->divide_count;
1619 limit_periodic_timer_frequency(apic);
1620
1621 now = ktime_get();
1622 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1623 if (ktime_to_ns(remaining) < 0)
1624 remaining = 0;
1625
1626 ns_remaining_old = ktime_to_ns(remaining);
1627 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1628 apic->divide_count, old_divisor);
1629
1630 apic->lapic_timer.tscdeadline +=
1631 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1632 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1633 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1634}
1635
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001636static bool set_target_expiration(struct kvm_lapic *apic)
1637{
1638 ktime_t now;
1639 u64 tscl = rdtsc();
1640
Paolo Bonzini55878592016-10-25 15:23:49 +02001641 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001642 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1643 * APIC_BUS_CYCLE_NS * apic->divide_count;
1644
Radim Krčmář5d74a692017-10-06 19:25:54 +02001645 if (!apic->lapic_timer.period) {
1646 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001647 return false;
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001648 }
1649
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001650 limit_periodic_timer_frequency(apic);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001651
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001652 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1653 PRIx64 ", "
1654 "timer initial count 0x%x, period %lldns, "
1655 "expire @ 0x%016" PRIx64 ".\n", __func__,
1656 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1657 kvm_lapic_get_reg(apic, APIC_TMICT),
1658 apic->lapic_timer.period,
1659 ktime_to_ns(ktime_add_ns(now,
1660 apic->lapic_timer.period)));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001661
1662 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1663 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1664 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1665
1666 return true;
1667}
1668
1669static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1670{
David Vrabeld8f2f492018-05-18 16:55:46 +01001671 ktime_t now = ktime_get();
1672 u64 tscl = rdtsc();
1673 ktime_t delta;
1674
1675 /*
1676 * Synchronize both deadlines to the same time source or
1677 * differences in the periods (caused by differences in the
1678 * underlying clocks or numerical approximation errors) will
1679 * cause the two to drift apart over time as the errors
1680 * accumulate.
1681 */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001682 apic->lapic_timer.target_expiration =
1683 ktime_add_ns(apic->lapic_timer.target_expiration,
1684 apic->lapic_timer.period);
David Vrabeld8f2f492018-05-18 16:55:46 +01001685 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1686 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1687 nsec_to_cycles(apic->vcpu, delta);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001688}
1689
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001690static void start_sw_period(struct kvm_lapic *apic)
1691{
1692 if (!apic->lapic_timer.period)
1693 return;
1694
1695 if (ktime_after(ktime_get(),
1696 apic->lapic_timer.target_expiration)) {
1697 apic_timer_expired(apic);
1698
1699 if (apic_lvtt_oneshot(apic))
1700 return;
1701
1702 advance_periodic_target_expiration(apic);
1703 }
1704
1705 hrtimer_start(&apic->lapic_timer.timer,
1706 apic->lapic_timer.target_expiration,
1707 HRTIMER_MODE_ABS_PINNED);
1708}
1709
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001710bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1711{
Wanpeng Li91005302016-08-03 12:04:12 +08001712 if (!lapic_in_kernel(vcpu))
1713 return false;
1714
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001715 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1716}
1717EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1718
Wanpeng Li7e810a32016-10-24 18:23:12 +08001719static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001720{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001721 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001722 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
Wanpeng Libd97ad02016-06-30 08:52:49 +08001723 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1724 apic->lapic_timer.hv_timer_in_use = false;
1725}
1726
Paolo Bonzinia749e242017-06-29 17:14:50 +02001727static bool start_hv_timer(struct kvm_lapic *apic)
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001728{
1729 struct kvm_timer *ktimer = &apic->lapic_timer;
Sean Christophersonf9927982019-04-16 13:32:46 -07001730 struct kvm_vcpu *vcpu = apic->vcpu;
1731 bool expired;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001732
Wanpeng Li1d518c62017-07-25 00:43:15 -07001733 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001734 if (!kvm_x86_ops->set_hv_timer)
1735 return false;
1736
Radim Krčmář86bbc1e2017-10-06 19:25:53 +02001737 if (!ktimer->tscdeadline)
1738 return false;
1739
Sean Christophersonf9927982019-04-16 13:32:46 -07001740 if (kvm_x86_ops->set_hv_timer(vcpu, ktimer->tscdeadline, &expired))
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001741 return false;
1742
1743 ktimer->hv_timer_in_use = true;
1744 hrtimer_cancel(&ktimer->timer);
1745
1746 /*
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001747 * To simplify handling the periodic timer, leave the hv timer running
1748 * even if the deadline timer has expired, i.e. rely on the resulting
1749 * VM-Exit to recompute the periodic timer's target expiration.
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001750 */
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001751 if (!apic_lvtt_period(apic)) {
1752 /*
1753 * Cancel the hv timer if the sw timer fired while the hv timer
1754 * was being programmed, or if the hv timer itself expired.
1755 */
1756 if (atomic_read(&ktimer->pending)) {
1757 cancel_hv_timer(apic);
Sean Christophersonf9927982019-04-16 13:32:46 -07001758 } else if (expired) {
Wanpeng Lic8533542017-06-29 06:28:09 -07001759 apic_timer_expired(apic);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001760 cancel_hv_timer(apic);
1761 }
Wanpeng Lic8533542017-06-29 06:28:09 -07001762 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02001763
Sean Christophersonf9927982019-04-16 13:32:46 -07001764 trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001765
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001766 return true;
1767}
1768
Paolo Bonzinia749e242017-06-29 17:14:50 +02001769static void start_sw_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001770{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001771 struct kvm_timer *ktimer = &apic->lapic_timer;
Wanpeng Li1d518c62017-07-25 00:43:15 -07001772
1773 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001774 if (apic->lapic_timer.hv_timer_in_use)
1775 cancel_hv_timer(apic);
1776 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1777 return;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001778
Paolo Bonzinia749e242017-06-29 17:14:50 +02001779 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1780 start_sw_period(apic);
1781 else if (apic_lvtt_tscdeadline(apic))
1782 start_sw_tscdeadline(apic);
1783 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1784}
1785
1786static void restart_apic_timer(struct kvm_lapic *apic)
1787{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001788 preempt_disable();
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001789
1790 if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
1791 goto out;
1792
Paolo Bonzinia749e242017-06-29 17:14:50 +02001793 if (!start_hv_timer(apic))
1794 start_sw_timer(apic);
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001795out:
Wanpeng Li1d518c62017-07-25 00:43:15 -07001796 preempt_enable();
Wanpeng Li196f20c2016-06-28 14:54:19 +08001797}
1798
Eddie Dong97222cc2007-09-12 10:58:04 +03001799void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1800{
1801 struct kvm_lapic *apic = vcpu->arch.apic;
1802
Wanpeng Li1d518c62017-07-25 00:43:15 -07001803 preempt_disable();
1804 /* If the preempt notifier has already run, it also called apic_timer_expired */
1805 if (!apic->lapic_timer.hv_timer_in_use)
1806 goto out;
Eddie Dong97222cc2007-09-12 10:58:04 +03001807 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001808 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001809 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001810
1811 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1812 advance_periodic_target_expiration(apic);
Paolo Bonzinia749e242017-06-29 17:14:50 +02001813 restart_apic_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001814 }
Wanpeng Li1d518c62017-07-25 00:43:15 -07001815out:
1816 preempt_enable();
Eddie Dong97222cc2007-09-12 10:58:04 +03001817}
1818EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1819
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001820void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1821{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001822 restart_apic_timer(vcpu->arch.apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001823}
1824EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1825
1826void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1827{
1828 struct kvm_lapic *apic = vcpu->arch.apic;
1829
Wanpeng Li1d518c62017-07-25 00:43:15 -07001830 preempt_disable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001831 /* Possibly the TSC deadline timer is not enabled yet */
Paolo Bonzinia749e242017-06-29 17:14:50 +02001832 if (apic->lapic_timer.hv_timer_in_use)
1833 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07001834 preempt_enable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001835}
1836EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1837
Paolo Bonzinia749e242017-06-29 17:14:50 +02001838void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1839{
1840 struct kvm_lapic *apic = vcpu->arch.apic;
1841
1842 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1843 restart_apic_timer(apic);
1844}
1845
Eddie Dong97222cc2007-09-12 10:58:04 +03001846static void start_apic_timer(struct kvm_lapic *apic)
1847{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001848 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001849
Paolo Bonzinia749e242017-06-29 17:14:50 +02001850 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1851 && !set_target_expiration(apic))
1852 return;
1853
1854 restart_apic_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001855}
1856
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001857static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1858{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001859 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001860
Radim Krčmář59fd1322015-06-30 22:19:16 +02001861 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1862 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1863 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001864 apic_debug("Receive NMI setting on APIC_LVT0 "
1865 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001866 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001867 } else
1868 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1869 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001870}
1871
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001872int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001873{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001874 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001875
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001876 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001877
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001878 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001879 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001880 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001881 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001882 else
1883 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001884 break;
1885
1886 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001887 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001888 apic_set_tpr(apic, val & 0xff);
1889 break;
1890
1891 case APIC_EOI:
1892 apic_set_eoi(apic);
1893 break;
1894
1895 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001896 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001897 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001898 else
1899 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001900 break;
1901
1902 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001903 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001904 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001905 recalculate_apic_map(apic->vcpu->kvm);
1906 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001907 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001908 break;
1909
Gleb Natapovfc61b802009-07-05 17:39:35 +03001910 case APIC_SPIV: {
1911 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001912 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001913 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001914 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001915 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1916 int i;
1917 u32 lvt_val;
1918
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001919 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001920 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001921 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001922 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001923 lvt_val | APIC_LVT_MASKED);
1924 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001925 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001926 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001927
1928 }
1929 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001930 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001931 case APIC_ICR:
1932 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001933 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001934 apic_send_ipi(apic);
1935 break;
1936
1937 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001938 if (!apic_x2apic_mode(apic))
1939 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001940 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001941 break;
1942
Jan Kiszka23930f92008-09-26 09:30:52 +02001943 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001944 apic_manage_nmi_watchdog(apic, val);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001945 /* fall through */
Eddie Dong97222cc2007-09-12 10:58:04 +03001946 case APIC_LVTTHMR:
1947 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001948 case APIC_LVT1:
1949 case APIC_LVTERR:
1950 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001951 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001952 val |= APIC_LVT_MASKED;
1953
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001954 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001955 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001956
1957 break;
1958
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001959 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001960 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001961 val |= APIC_LVT_MASKED;
1962 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001963 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001964 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001965 break;
1966
Eddie Dong97222cc2007-09-12 10:58:04 +03001967 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001968 if (apic_lvtt_tscdeadline(apic))
1969 break;
1970
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001971 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001972 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001973 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001974 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001975
Wanpeng Lic301b902017-10-06 07:38:32 -07001976 case APIC_TDCR: {
1977 uint32_t old_divisor = apic->divide_count;
1978
Eddie Dong97222cc2007-09-12 10:58:04 +03001979 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001980 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001981 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001982 update_divide_count(apic);
Wanpeng Lic301b902017-10-06 07:38:32 -07001983 if (apic->divide_count != old_divisor &&
1984 apic->lapic_timer.period) {
1985 hrtimer_cancel(&apic->lapic_timer.timer);
1986 update_target_expiration(apic, old_divisor);
1987 restart_apic_timer(apic);
1988 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001989 break;
Wanpeng Lic301b902017-10-06 07:38:32 -07001990 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001991 case APIC_ESR:
1992 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001993 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001994 ret = 1;
1995 }
1996 break;
1997
1998 case APIC_SELF_IPI:
1999 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002000 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002001 } else
2002 ret = 1;
2003 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03002004 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002005 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03002006 break;
2007 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002008 if (ret)
2009 apic_debug("Local APIC Write to read-only register %x\n", reg);
2010 return ret;
2011}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002012EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002013
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00002014static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002015 gpa_t address, int len, const void *data)
2016{
2017 struct kvm_lapic *apic = to_lapic(this);
2018 unsigned int offset = address - apic->base_address;
2019 u32 val;
2020
2021 if (!apic_mmio_in_range(apic, address))
2022 return -EOPNOTSUPP;
2023
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02002024 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2025 if (!kvm_check_has_quirk(vcpu->kvm,
2026 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2027 return -EOPNOTSUPP;
2028
2029 return 0;
2030 }
2031
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002032 /*
2033 * APIC register must be aligned on 128-bits boundary.
2034 * 32/64/128 bits registers must be accessed thru 32 bits.
2035 * Refer SDM 8.4.1
2036 */
2037 if (len != 4 || (offset & 0xf)) {
2038 /* Don't shout loud, $infamous_os would cause only noise. */
2039 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08002040 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002041 }
2042
2043 val = *(u32*)data;
2044
2045 /* too common printing */
2046 if (offset != APIC_EOI)
2047 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
2048 "0x%x\n", __func__, offset, len, val);
2049
Wei Yang4cb8b112019-03-31 19:17:23 -07002050 kvm_lapic_reg_write(apic, offset, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002051
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002052 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002053}
2054
Kevin Tian58fbbf22011-08-30 13:56:17 +03002055void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
2056{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002057 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03002058}
2059EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
2060
Yang Zhang83d4c282013-01-25 10:18:49 +08002061/* emulate APIC access in a trap manner */
2062void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
2063{
2064 u32 val = 0;
2065
2066 /* hw has done the conditional check and inst decode */
2067 offset &= 0xff0;
2068
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002069 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002070
2071 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002072 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002073}
2074EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2075
Rusty Russelld5894442007-10-08 10:48:30 +10002076void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002077{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002078 struct kvm_lapic *apic = vcpu->arch.apic;
2079
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002081 return;
2082
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002083 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002084
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002085 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2086 static_key_slow_dec_deferred(&apic_hw_disabled);
2087
Radim Krčmáře4627552014-10-30 15:06:45 +01002088 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002089 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03002090
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002091 if (apic->regs)
2092 free_page((unsigned long)apic->regs);
2093
2094 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002095}
2096
2097/*
2098 *----------------------------------------------------------------------
2099 * LAPIC interface
2100 *----------------------------------------------------------------------
2101 */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002102u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2103{
2104 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002105
Wanpeng Lia10388e2016-10-24 18:23:10 +08002106 if (!lapic_in_kernel(vcpu) ||
2107 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002108 return 0;
2109
2110 return apic->lapic_timer.tscdeadline;
2111}
2112
2113void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2114{
2115 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002116
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002117 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03002118 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002119 return;
2120
2121 hrtimer_cancel(&apic->lapic_timer.timer);
2122 apic->lapic_timer.tscdeadline = data;
2123 start_apic_timer(apic);
2124}
2125
Eddie Dong97222cc2007-09-12 10:58:04 +03002126void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2127{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002128 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002129
Avi Kivityb93463a2007-10-25 16:52:32 +02002130 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002131 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03002132}
2133
2134u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2135{
Eddie Dong97222cc2007-09-12 10:58:04 +03002136 u64 tpr;
2137
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002138 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03002139
2140 return (tpr & 0xf0) >> 4;
2141}
2142
2143void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2144{
Yang Zhang8d146952013-01-25 10:18:50 +08002145 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002146 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002147
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002148 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002149 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002150
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01002151 vcpu->arch.apic_base = value;
2152
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002153 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2154 kvm_update_cpuid(vcpu);
2155
2156 if (!apic)
2157 return;
2158
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002159 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01002160 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02002161 if (value & MSR_IA32_APICBASE_ENABLE) {
2162 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002163 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08002164 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002165 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08002166 recalculate_apic_map(vcpu->kvm);
2167 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002168 }
2169
Jim Mattson8d860bb2018-05-09 16:56:05 -04002170 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2171 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2172
2173 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2174 kvm_x86_ops->set_virtual_apic_mode(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08002175
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002176 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03002177 MSR_IA32_APICBASE_BASE;
2178
Nadav Amitdb324fe2014-11-02 11:54:59 +02002179 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2180 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2181 pr_warn_once("APIC base relocation is unsupported by KVM");
2182
Eddie Dong97222cc2007-09-12 10:58:04 +03002183 /* with FSB delivery interrupt, we can restart APIC functionality */
2184 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002185 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03002186
2187}
2188
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002189void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03002190{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002191 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002192 int i;
2193
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002194 if (!apic)
2195 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002196
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002197 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03002198
2199 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002200 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002201
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002202 if (!init_event) {
2203 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2204 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002205 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002206 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03002207 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002208
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002209 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2210 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002211 apic_update_lvtt(apic);
Jan H. Schönherr52b54192017-05-20 13:24:32 +02002212 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2213 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002214 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03002215 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002216 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03002217
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002218 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002219 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002220 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02002221 if (!apic_x2apic_mode(apic))
2222 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002223 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2224 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2225 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2226 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2227 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002228 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002229 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2230 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2231 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002232 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03002233 apic->irr_pending = vcpu->arch.apicv_active;
2234 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002235 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02002236 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002237 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002238 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002239 kvm_lapic_set_base(vcpu,
2240 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002241 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002242 apic_update_ppr(apic);
Jan H. Schönherr4191db22017-10-25 16:43:27 +02002243 if (vcpu->arch.apicv_active) {
2244 kvm_x86_ops->apicv_post_state_restore(vcpu);
2245 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2246 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2247 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002248
Gleb Natapove1035712009-03-05 16:34:59 +02002249 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03002250 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02002251
Radim Krčmář6e500432016-12-15 18:06:46 +01002252 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002253 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Radim Krčmář6e500432016-12-15 18:06:46 +01002254 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002255 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03002256}
2257
Eddie Dong97222cc2007-09-12 10:58:04 +03002258/*
2259 *----------------------------------------------------------------------
2260 * timer interface
2261 *----------------------------------------------------------------------
2262 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03002263
Avi Kivity2a6eac92012-07-26 18:01:51 +03002264static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002265{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002266 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002267}
2268
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002269int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2270{
Gleb Natapov54e98182012-08-05 15:58:32 +03002271 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002272
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01002273 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03002274 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002275
2276 return 0;
2277}
2278
Avi Kivity89342082011-11-10 14:57:21 +02002279int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002280{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002281 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002282 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002283
Gleb Natapovc48f1492012-08-05 15:58:33 +03002284 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002285 vector = reg & APIC_VECTOR_MASK;
2286 mode = reg & APIC_MODE_MASK;
2287 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08002288 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2289 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02002290 }
2291 return 0;
2292}
2293
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002294void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002295{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002296 struct kvm_lapic *apic = vcpu->arch.apic;
2297
2298 if (apic)
2299 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002300}
2301
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002302static const struct kvm_io_device_ops apic_mmio_ops = {
2303 .read = apic_mmio_read,
2304 .write = apic_mmio_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002305};
2306
Avi Kivitye9d90d42012-07-26 18:01:50 +03002307static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2308{
2309 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002310 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002311
Radim Krčmář5d87db72014-10-10 19:15:08 +02002312 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002313
Avi Kivity2a6eac92012-07-26 18:01:51 +03002314 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002315 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002316 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2317 return HRTIMER_RESTART;
2318 } else
2319 return HRTIMER_NORESTART;
2320}
2321
Sean Christophersonc3941d92019-04-17 10:15:33 -07002322int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
Eddie Dong97222cc2007-09-12 10:58:04 +03002323{
2324 struct kvm_lapic *apic;
2325
2326 ASSERT(vcpu != NULL);
2327 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2328
Ben Gardon254272c2019-02-11 11:02:50 -08002329 apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
Eddie Dong97222cc2007-09-12 10:58:04 +03002330 if (!apic)
2331 goto nomem;
2332
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002333 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002334
Ben Gardon254272c2019-02-11 11:02:50 -08002335 apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002336 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002337 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2338 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002339 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002340 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002341 apic->vcpu = vcpu;
2342
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002343 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002344 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002345 apic->lapic_timer.timer.function = apic_timer_fn;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002346 if (timer_advance_ns == -1) {
2347 apic->lapic_timer.timer_advance_ns = 1000;
2348 apic->lapic_timer.timer_advance_adjust_done = false;
2349 } else {
2350 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
2351 apic->lapic_timer.timer_advance_adjust_done = true;
2352 }
2353
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002354
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002355 /*
2356 * APIC is created enabled. This will prevent kvm_lapic_set_base from
Wei Yangee171d22019-03-31 19:17:22 -07002357 * thinking that APIC state has changed.
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002358 */
2359 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002360 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002361 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002362
2363 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002364nomem_free_apic:
2365 kfree(apic);
Saar Amara251fb902019-05-06 11:29:16 +03002366 vcpu->arch.apic = NULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002367nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002368 return -ENOMEM;
2369}
Eddie Dong97222cc2007-09-12 10:58:04 +03002370
2371int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2372{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002373 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002374 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002375
Paolo Bonzinif8543d62016-01-08 13:42:24 +01002376 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002377 return -1;
2378
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002379 __apic_update_ppr(apic, &ppr);
2380 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002381}
2382
Qing He40487c62007-09-17 14:47:13 +08002383int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2384{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002385 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002386 int r = 0;
2387
Gleb Natapovc48f1492012-08-05 15:58:33 +03002388 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002389 r = 1;
2390 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2391 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2392 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002393 return r;
2394}
2395
Eddie Dong1b9778d2007-09-03 16:56:58 +03002396void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2397{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002398 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002399
Gleb Natapov54e98182012-08-05 15:58:32 +03002400 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002401 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002402 if (apic_lvtt_tscdeadline(apic))
2403 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002404 if (apic_lvtt_oneshot(apic)) {
2405 apic->lapic_timer.tscdeadline = 0;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002406 apic->lapic_timer.target_expiration = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002407 }
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002408 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002409 }
2410}
2411
Eddie Dong97222cc2007-09-12 10:58:04 +03002412int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2413{
2414 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002415 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002416 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002417
2418 if (vector == -1)
2419 return -1;
2420
Wanpeng Li56cc2402014-08-05 12:42:24 +08002421 /*
2422 * We get here even with APIC virtualization enabled, if doing
2423 * nested virtualization and L1 runs with the "acknowledge interrupt
2424 * on exit" mode. Then we cannot inject the interrupt via RVI,
2425 * because the process would deliver it through the IDT.
2426 */
2427
Eddie Dong97222cc2007-09-12 10:58:04 +03002428 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002429 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002430 /*
2431 * For auto-EOI interrupts, there might be another pending
2432 * interrupt above PPR, so check whether to raise another
2433 * KVM_REQ_EVENT.
2434 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002435 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002436 } else {
2437 /*
2438 * For normal interrupts, PPR has been raised and there cannot
2439 * be a higher-priority pending interrupt---except if there was
2440 * a concurrent interrupt injection, but that would have
2441 * triggered KVM_REQ_EVENT already.
2442 */
2443 apic_set_isr(vector, apic);
2444 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002445 }
2446
Eddie Dong97222cc2007-09-12 10:58:04 +03002447 return vector;
2448}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002449
Radim Krčmářa92e2542016-07-12 22:09:22 +02002450static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2451 struct kvm_lapic_state *s, bool set)
2452{
2453 if (apic_x2apic_mode(vcpu->arch.apic)) {
2454 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002455 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002456
Radim Krčmář371313132016-07-12 22:09:27 +02002457 if (vcpu->kvm->arch.x2apic_format) {
2458 if (*id != vcpu->vcpu_id)
2459 return -EINVAL;
2460 } else {
2461 if (set)
2462 *id >>= 24;
2463 else
2464 *id <<= 24;
2465 }
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002466
2467 /* In x2APIC mode, the LDR is fixed and based on the id */
2468 if (set)
2469 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002470 }
2471
2472 return 0;
2473}
2474
2475int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2476{
2477 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2478 return kvm_apic_state_fixup(vcpu, s, false);
2479}
2480
2481int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002482{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002483 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002484 int r;
2485
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002486
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002487 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002488 /* set SPIV separately to get count of SW disabled APICs right */
2489 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002490
2491 r = kvm_apic_state_fixup(vcpu, s, true);
2492 if (r)
2493 return r;
Jordan Borgner0e96f312018-10-28 12:58:28 +00002494 memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002495
2496 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002497 kvm_apic_set_version(vcpu);
2498
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002499 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002500 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002501 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002502 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002503 update_divide_count(apic);
2504 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002505 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002506 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002507 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002508 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002509 if (vcpu->arch.apicv_active) {
Paolo Bonzini967235d2016-12-19 14:03:45 +01002510 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002511 kvm_x86_ops->hwapic_irr_update(vcpu,
2512 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002513 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002514 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002515 }
Avi Kivity3842d132010-07-27 12:30:24 +03002516 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002517 if (ioapic_in_kernel(vcpu->kvm))
2518 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002519
2520 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002521
2522 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002523}
Eddie Donga3d7f852007-09-03 16:15:12 +03002524
Avi Kivity2f52d582008-01-16 12:49:30 +02002525void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002526{
Eddie Donga3d7f852007-09-03 16:15:12 +03002527 struct hrtimer *timer;
2528
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002529 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002530 return;
2531
Gleb Natapov54e98182012-08-05 15:58:32 +03002532 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002533 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002534 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002535}
Avi Kivityb93463a2007-10-25 16:52:32 +02002536
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002537/*
2538 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2539 *
2540 * Detect whether guest triggered PV EOI since the
2541 * last entry. If yes, set EOI on guests's behalf.
2542 * Clear PV EOI in guest memory in any case.
2543 */
2544static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2545 struct kvm_lapic *apic)
2546{
2547 bool pending;
2548 int vector;
2549 /*
2550 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2551 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2552 *
2553 * KVM_APIC_PV_EOI_PENDING is unset:
2554 * -> host disabled PV EOI.
2555 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2556 * -> host enabled PV EOI, guest did not execute EOI yet.
2557 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2558 * -> host enabled PV EOI, guest executed EOI.
2559 */
2560 BUG_ON(!pv_eoi_enabled(vcpu));
2561 pending = pv_eoi_get_pending(vcpu);
2562 /*
2563 * Clear pending bit in any case: it will be set again on vmentry.
2564 * While this might not be ideal from performance point of view,
2565 * this makes sure pv eoi is only enabled when we know it's safe.
2566 */
2567 pv_eoi_clr_pending(vcpu);
2568 if (pending)
2569 return;
2570 vector = apic_set_eoi(apic);
2571 trace_kvm_pv_eoi(apic, vector);
2572}
2573
Avi Kivityb93463a2007-10-25 16:52:32 +02002574void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2575{
2576 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002577
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002578 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2579 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2580
Gleb Natapov41383772012-04-19 14:06:29 +03002581 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002582 return;
2583
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002584 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2585 sizeof(u32)))
Nicholas Krause603242a2015-08-05 10:44:40 -04002586 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002587
2588 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2589}
2590
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002591/*
2592 * apic_sync_pv_eoi_to_guest - called before vmentry
2593 *
2594 * Detect whether it's safe to enable PV EOI and
2595 * if yes do so.
2596 */
2597static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2598 struct kvm_lapic *apic)
2599{
2600 if (!pv_eoi_enabled(vcpu) ||
2601 /* IRR set or many bits in ISR: could be nested. */
2602 apic->irr_pending ||
2603 /* Cache not set: could be safe but we don't bother. */
2604 apic->highest_isr_cache == -1 ||
2605 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002606 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002607 /*
2608 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2609 * so we need not do anything here.
2610 */
2611 return;
2612 }
2613
2614 pv_eoi_set_pending(apic->vcpu);
2615}
2616
Avi Kivityb93463a2007-10-25 16:52:32 +02002617void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2618{
2619 u32 data, tpr;
2620 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002621 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002622
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002623 apic_sync_pv_eoi_to_guest(vcpu, apic);
2624
Gleb Natapov41383772012-04-19 14:06:29 +03002625 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002626 return;
2627
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002628 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002629 max_irr = apic_find_highest_irr(apic);
2630 if (max_irr < 0)
2631 max_irr = 0;
2632 max_isr = apic_find_highest_isr(apic);
2633 if (max_isr < 0)
2634 max_isr = 0;
2635 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2636
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002637 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2638 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002639}
2640
Andy Honigfda4e2e2013-11-20 10:23:22 -08002641int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002642{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002643 if (vapic_addr) {
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002644 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andy Honigfda4e2e2013-11-20 10:23:22 -08002645 &vcpu->arch.apic->vapic_cache,
2646 vapic_addr, sizeof(u32)))
2647 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002648 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002649 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002650 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002651 }
2652
2653 vcpu->arch.apic->vapic_addr = vapic_addr;
2654 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002655}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002656
2657int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2658{
2659 struct kvm_lapic *apic = vcpu->arch.apic;
2660 u32 reg = (msr - APIC_BASE_MSR) << 4;
2661
Paolo Bonzini35754c92015-07-29 12:05:37 +02002662 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002663 return 1;
2664
Nadav Amitc69d3d92014-11-26 17:56:25 +02002665 if (reg == APIC_ICR2)
2666 return 1;
2667
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002668 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002669 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002670 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2671 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002672}
2673
2674int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2675{
2676 struct kvm_lapic *apic = vcpu->arch.apic;
2677 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2678
Paolo Bonzini35754c92015-07-29 12:05:37 +02002679 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002680 return 1;
2681
Nadav Amitc69d3d92014-11-26 17:56:25 +02002682 if (reg == APIC_DFR || reg == APIC_ICR2) {
2683 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2684 reg);
2685 return 1;
2686 }
2687
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002688 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002689 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002690 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002691 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002692
2693 *data = (((u64)high) << 32) | low;
2694
2695 return 0;
2696}
Gleb Natapov10388a02010-01-17 15:51:23 +02002697
2698int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2699{
2700 struct kvm_lapic *apic = vcpu->arch.apic;
2701
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002702 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002703 return 1;
2704
2705 /* if this is ICR write vector before command */
2706 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002707 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2708 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002709}
2710
2711int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2712{
2713 struct kvm_lapic *apic = vcpu->arch.apic;
2714 u32 low, high = 0;
2715
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002716 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002717 return 1;
2718
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002719 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002720 return 1;
2721 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002722 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002723
2724 *data = (((u64)high) << 32) | low;
2725
2726 return 0;
2727}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002728
Ladi Prosek72bbf932018-10-16 18:49:59 +02002729int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002730{
2731 u64 addr = data & ~KVM_MSR_ENABLED;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002732 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
2733 unsigned long new_len;
2734
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002735 if (!IS_ALIGNED(addr, 4))
2736 return 1;
2737
2738 vcpu->arch.pv_eoi.msr_val = data;
2739 if (!pv_eoi_enabled(vcpu))
2740 return 0;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002741
2742 if (addr == ghc->gpa && len <= ghc->len)
2743 new_len = ghc->len;
2744 else
2745 new_len = len;
2746
2747 return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002748}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002749
Jan Kiszka66450a22013-03-13 12:42:34 +01002750void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2751{
2752 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002753 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002754 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002755
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002756 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002757 return;
2758
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002759 /*
2760 * INITs are latched while in SMM. Because an SMM CPU cannot
2761 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2762 * and delay processing of INIT until the next RSM.
2763 */
2764 if (is_smm(vcpu)) {
2765 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2766 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2767 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2768 return;
2769 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002770
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002771 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002772 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002773 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002774 if (kvm_vcpu_is_bsp(apic->vcpu))
2775 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2776 else
2777 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2778 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002779 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002780 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2781 /* evaluate pending_events before reading the vector */
2782 smp_rmb();
2783 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002784 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002785 vcpu->vcpu_id, sipi_vector);
2786 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2787 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2788 }
2789}
2790
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002791void kvm_lapic_init(void)
2792{
2793 /* do not patch jump label more than once per second */
2794 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002795 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002796}
David Matlackcef84c32016-12-16 14:30:36 -08002797
2798void kvm_lapic_exit(void)
2799{
2800 static_key_deferred_flush(&apic_hw_disabled);
2801 static_key_deferred_flush(&apic_sw_disabled);
2802}