blob: e3099c642fecfbbb6cd7b4df4a0df2a9b8091d73 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Eddie Dong97222cc2007-09-12 10:58:04 +03002
3/*
4 * Local APIC virtualization
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2007 Novell
8 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +030010 *
11 * Authors:
12 * Dor Laor <dor.laor@qumranet.com>
13 * Gregory Haskins <ghaskins@novell.com>
14 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
15 *
16 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
Eddie Dong97222cc2007-09-12 10:58:04 +030017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030020#include <linux/kvm.h>
21#include <linux/mm.h>
22#include <linux/highmem.h>
23#include <linux/smp.h>
24#include <linux/hrtimer.h>
25#include <linux/io.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040026#include <linux/export.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070027#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030029#include <asm/processor.h>
30#include <asm/msr.h>
31#include <asm/page.h>
32#include <asm/current.h>
33#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050034#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030036#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030037#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030038#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030039#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030040#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020041#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030042#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030043
Marcelo Tosattib682b812009-02-10 20:41:41 -020044#ifndef CONFIG_X86_64
45#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
46#else
47#define mod_64(x, y) ((x) % (y))
48#endif
49
Eddie Dong97222cc2007-09-12 10:58:04 +030050#define PRId64 "d"
51#define PRIx64 "llx"
52#define PRIu64 "u"
53#define PRIo64 "o"
54
Eddie Dong97222cc2007-09-12 10:58:04 +030055/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050056#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030057#define LAPIC_MMIO_LENGTH (1 << 12)
58/* followed define is not in apicdef.h */
Eddie Dong97222cc2007-09-12 10:58:04 +030059#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090060#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030061
Nadav Amit394457a2014-10-03 00:30:52 +030062#define APIC_BROADCAST 0xFF
63#define X2APIC_BROADCAST 0xFFFFFFFFul
64
Wanpeng Lid0f5a862019-09-17 16:16:26 +080065static bool lapic_timer_advance_dynamic __read_mostly;
Wanpeng Lia0f00372019-09-26 08:54:03 +080066#define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
67#define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
68#define LAPIC_TIMER_ADVANCE_NS_INIT 1000
69#define LAPIC_TIMER_ADVANCE_NS_MAX 5000
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080070/* step-by-step approximation to mitigate fluctuation */
71#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
72
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030073static inline int apic_test_vector(int vec, void *bitmap)
74{
75 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
76}
77
Yang Zhang10606912013-04-11 19:21:38 +080078bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
79{
80 struct kvm_lapic *apic = vcpu->arch.apic;
81
82 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
83 apic_test_vector(vector, apic->regs + APIC_IRR);
84}
85
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030086static inline int __apic_test_and_set_vector(int vec, void *bitmap)
87{
88 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
89}
90
91static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
92{
93 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
94}
95
Gleb Natapovc5cc4212012-08-05 15:58:30 +030096struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +030097struct static_key_deferred apic_sw_disabled __read_mostly;
98
Eddie Dong97222cc2007-09-12 10:58:04 +030099static inline int apic_enabled(struct kvm_lapic *apic)
100{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300101 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300102}
103
Eddie Dong97222cc2007-09-12 10:58:04 +0300104#define LVT_MASK \
105 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
106
107#define LINT_MASK \
108 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
109 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
110
Radim Krčmář6e500432016-12-15 18:06:46 +0100111static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
112{
113 return apic->vcpu->vcpu_id;
114}
115
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800116bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
117{
118 return pi_inject_timer && kvm_vcpu_apicv_active(vcpu);
119}
120EXPORT_SYMBOL_GPL(kvm_can_post_timer_interrupt);
121
122static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
123{
124 return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
125}
126
Radim Krčmáře45115b2016-07-12 22:09:19 +0200127static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
128 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
129 switch (map->mode) {
130 case KVM_APIC_MODE_X2APIC: {
131 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200132 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100133
Radim Krčmáře45115b2016-07-12 22:09:19 +0200134 if (offset <= max_apic_id) {
135 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100136
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200137 offset = array_index_nospec(offset, map->max_apic_id + 1);
Radim Krčmáře45115b2016-07-12 22:09:19 +0200138 *cluster = &map->phys_map[offset];
139 *mask = dest_id & (0xffff >> (16 - cluster_size));
140 } else {
141 *mask = 0;
142 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100143
Radim Krčmáře45115b2016-07-12 22:09:19 +0200144 return true;
145 }
146 case KVM_APIC_MODE_XAPIC_FLAT:
147 *cluster = map->xapic_flat_map;
148 *mask = dest_id & 0xff;
149 return true;
150 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100151 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200152 *mask = dest_id & 0xf;
153 return true;
154 default:
155 /* Not optimized. */
156 return false;
157 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300158}
159
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200160static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100161{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200162 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100163
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200164 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100165}
166
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300167static void recalculate_apic_map(struct kvm *kvm)
168{
169 struct kvm_apic_map *new, *old = NULL;
170 struct kvm_vcpu *vcpu;
171 int i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100172 u32 max_id = 255; /* enough space for any xAPIC ID */
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300173
174 mutex_lock(&kvm->arch.apic_map_lock);
175
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200176 kvm_for_each_vcpu(i, vcpu, kvm)
177 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100178 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200179
Michal Hockoa7c3e902017-05-08 15:57:09 -0700180 new = kvzalloc(sizeof(struct kvm_apic_map) +
Ben Gardon254272c2019-02-11 11:02:50 -0800181 sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
182 GFP_KERNEL_ACCOUNT);
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200183
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300184 if (!new)
185 goto out;
186
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200187 new->max_apic_id = max_id;
188
Nadav Amit173beed2014-11-02 11:54:54 +0200189 kvm_for_each_vcpu(i, vcpu, kvm) {
190 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200191 struct kvm_lapic **cluster;
192 u16 mask;
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100193 u32 ldr;
194 u8 xapic_id;
195 u32 x2apic_id;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300196
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100197 if (!kvm_apic_present(vcpu))
198 continue;
199
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100200 xapic_id = kvm_xapic_id(apic);
201 x2apic_id = kvm_x2apic_id(apic);
202
203 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
204 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
205 x2apic_id <= new->max_apic_id)
206 new->phys_map[x2apic_id] = apic;
207 /*
208 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
209 * prevent them from masking VCPUs with APIC ID <= 0xff.
210 */
211 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
212 new->phys_map[xapic_id] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100213
Radim Krcmarb14c8762019-08-13 23:37:37 -0400214 if (!kvm_apic_sw_enabled(apic))
215 continue;
216
Radim Krčmář6e500432016-12-15 18:06:46 +0100217 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
218
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100219 if (apic_x2apic_mode(apic)) {
220 new->mode |= KVM_APIC_MODE_X2APIC;
221 } else if (ldr) {
222 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500223 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100224 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
225 else
226 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
227 }
228
Radim Krčmáře45115b2016-07-12 22:09:19 +0200229 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100230 continue;
231
Radim Krčmáře45115b2016-07-12 22:09:19 +0200232 if (mask)
233 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300234 }
235out:
236 old = rcu_dereference_protected(kvm->arch.apic_map,
237 lockdep_is_held(&kvm->arch.apic_map_lock));
238 rcu_assign_pointer(kvm->arch.apic_map, new);
239 mutex_unlock(&kvm->arch.apic_map_lock);
240
241 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200242 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800243
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700244 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300245}
246
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300247static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
248{
Radim Krčmáře4627552014-10-30 15:06:45 +0100249 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300250
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500251 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100252
253 if (enabled != apic->sw_enabled) {
254 apic->sw_enabled = enabled;
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800255 if (enabled)
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300256 static_key_slow_dec_deferred(&apic_sw_disabled);
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800257 else
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300258 static_key_slow_inc(&apic_sw_disabled.key);
Radim Krcmarb14c8762019-08-13 23:37:37 -0400259
260 recalculate_apic_map(apic->vcpu->kvm);
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300261 }
262}
263
Radim Krčmářa92e2542016-07-12 22:09:22 +0200264static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300265{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500266 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300267 recalculate_apic_map(apic->vcpu->kvm);
268}
269
270static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
271{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500272 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300273 recalculate_apic_map(apic->vcpu->kvm);
274}
275
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000276static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
277{
278 return ((id >> 4) << 16) | (1 << (id & 0xf));
279}
280
Radim Krčmářa92e2542016-07-12 22:09:22 +0200281static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200282{
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000283 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200284
Radim Krčmář6e500432016-12-15 18:06:46 +0100285 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
286
Radim Krčmářa92e2542016-07-12 22:09:22 +0200287 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500288 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200289 recalculate_apic_map(apic->vcpu->kvm);
290}
291
Eddie Dong97222cc2007-09-12 10:58:04 +0300292static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
293{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500294 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300295}
296
297static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
298{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500299 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300300}
301
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800302static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
303{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100304 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800305}
306
Eddie Dong97222cc2007-09-12 10:58:04 +0300307static inline int apic_lvtt_period(struct kvm_lapic *apic)
308{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100309 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800310}
311
312static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
313{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100314 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300315}
316
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200317static inline int apic_lvt_nmi_mode(u32 lvt_val)
318{
319 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
320}
321
Gleb Natapovfc61b802009-07-05 17:39:35 +0300322void kvm_apic_set_version(struct kvm_vcpu *vcpu)
323{
324 struct kvm_lapic *apic = vcpu->arch.apic;
325 struct kvm_cpuid_entry2 *feat;
326 u32 v = APIC_VERSION;
327
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100328 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300329 return;
330
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100331 /*
332 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
333 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
334 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
335 * version first and level-triggered interrupts never get EOIed in
336 * IOAPIC.
337 */
Gleb Natapovfc61b802009-07-05 17:39:35 +0300338 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100339 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
340 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300341 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500342 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300343}
344
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500345static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800346 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300347 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
348 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
349 LINT_MASK, LINT_MASK, /* LVT0-1 */
350 LVT_MASK /* LVTERR */
351};
352
353static int find_highest_vector(void *bitmap)
354{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900355 int vec;
356 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300357
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900358 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
359 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
360 reg = bitmap + REG_POS(vec);
361 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100362 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900363 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300364
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900365 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300366}
367
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300368static u8 count_vectors(void *bitmap)
369{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900370 int vec;
371 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300372 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900373
374 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
375 reg = bitmap + REG_POS(vec);
376 count += hweight32(*reg);
377 }
378
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300379 return count;
380}
381
Liran Alone7387b02017-12-24 18:12:54 +0200382bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Yang Zhanga20ed542013-04-11 19:25:15 +0800383{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100384 u32 i, vec;
Liran Alone7387b02017-12-24 18:12:54 +0200385 u32 pir_val, irr_val, prev_irr_val;
386 int max_updated_irr;
387
388 max_updated_irr = -1;
389 *max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800390
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100391 for (i = vec = 0; i <= 7; i++, vec += 32) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200392 pir_val = READ_ONCE(pir[i]);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100393 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
Paolo Bonziniad361092016-09-20 16:15:05 +0200394 if (pir_val) {
Liran Alone7387b02017-12-24 18:12:54 +0200395 prev_irr_val = irr_val;
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100396 irr_val |= xchg(&pir[i], 0);
397 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
Liran Alone7387b02017-12-24 18:12:54 +0200398 if (prev_irr_val != irr_val) {
399 max_updated_irr =
400 __fls(irr_val ^ prev_irr_val) + vec;
401 }
Paolo Bonziniad361092016-09-20 16:15:05 +0200402 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100403 if (irr_val)
Liran Alone7387b02017-12-24 18:12:54 +0200404 *max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800405 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100406
Liran Alone7387b02017-12-24 18:12:54 +0200407 return ((max_updated_irr != -1) &&
408 (max_updated_irr == *max_irr));
Yang Zhanga20ed542013-04-11 19:25:15 +0800409}
Wincy Van705699a2015-02-03 23:58:17 +0800410EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
411
Liran Alone7387b02017-12-24 18:12:54 +0200412bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
Wincy Van705699a2015-02-03 23:58:17 +0800413{
414 struct kvm_lapic *apic = vcpu->arch.apic;
415
Liran Alone7387b02017-12-24 18:12:54 +0200416 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
Wincy Van705699a2015-02-03 23:58:17 +0800417}
Yang Zhanga20ed542013-04-11 19:25:15 +0800418EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
419
Gleb Natapov33e4c682009-06-11 11:06:51 +0300420static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300421{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300422 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300423}
424
425static inline int apic_find_highest_irr(struct kvm_lapic *apic)
426{
427 int result;
428
Yang Zhangc7c9c562013-01-25 10:18:51 +0800429 /*
430 * Note that irr_pending is just a hint. It will be always
431 * true with virtual interrupt delivery enabled.
432 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300433 if (!apic->irr_pending)
434 return -1;
435
436 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300437 ASSERT(result == -1 || result >= 16);
438
439 return result;
440}
441
Gleb Natapov33e4c682009-06-11 11:06:51 +0300442static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
443{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800444 struct kvm_vcpu *vcpu;
445
446 vcpu = apic->vcpu;
447
Andrey Smetanind62caab2015-11-10 15:36:33 +0300448 if (unlikely(vcpu->arch.apicv_active)) {
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100449 /* need to update RVI */
Wei Yangee171d22019-03-31 19:17:22 -0700450 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100451 kvm_x86_ops->hwapic_irr_update(vcpu,
452 apic_find_highest_irr(apic));
Nadav Amitf210f752014-11-16 23:49:07 +0200453 } else {
454 apic->irr_pending = false;
Wei Yangee171d22019-03-31 19:17:22 -0700455 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Nadav Amitf210f752014-11-16 23:49:07 +0200456 if (apic_search_irr(apic) != -1)
457 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800458 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300459}
460
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300461static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
462{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800463 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200464
Wanpeng Li56cc2402014-08-05 12:42:24 +0800465 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
466 return;
467
468 vcpu = apic->vcpu;
469
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300470 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800471 * With APIC virtualization enabled, all caching is disabled
472 * because the processor can modify ISR under the hood. Instead
473 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300474 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300475 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200476 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800477 else {
478 ++apic->isr_count;
479 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
480 /*
481 * ISR (in service register) bit is set when injecting an interrupt.
482 * The highest vector is injected. Thus the latest bit set matches
483 * the highest bit in ISR.
484 */
485 apic->highest_isr_cache = vec;
486 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300487}
488
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200489static inline int apic_find_highest_isr(struct kvm_lapic *apic)
490{
491 int result;
492
493 /*
494 * Note that isr_count is always 1, and highest_isr_cache
495 * is always -1, with APIC virtualization enabled.
496 */
497 if (!apic->isr_count)
498 return -1;
499 if (likely(apic->highest_isr_cache != -1))
500 return apic->highest_isr_cache;
501
502 result = find_highest_vector(apic->regs + APIC_ISR);
503 ASSERT(result == -1 || result >= 16);
504
505 return result;
506}
507
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300508static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
509{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200510 struct kvm_vcpu *vcpu;
511 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
512 return;
513
514 vcpu = apic->vcpu;
515
516 /*
517 * We do get here for APIC virtualization enabled if the guest
518 * uses the Hyper-V APIC enlightenment. In this case we may need
519 * to trigger a new interrupt delivery by writing the SVI field;
520 * on the other hand isr_count and highest_isr_cache are unused
521 * and must be left alone.
522 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300523 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200524 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200525 apic_find_highest_isr(apic));
526 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300527 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200528 BUG_ON(apic->isr_count < 0);
529 apic->highest_isr_cache = -1;
530 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300531}
532
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800533int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
534{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300535 /* This may race with setting of irr in __apic_accept_irq() and
536 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
537 * will cause vmexit immediately and the value will be recalculated
538 * on the next vmentry.
539 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100540 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800541}
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100542EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800543
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200544static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800545 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100546 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200547
Yang Zhangb4f22252013-04-11 19:21:37 +0800548int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100549 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300550{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800551 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800552
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200553 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800554 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300555}
556
Miaohe Lin1a686232019-11-09 17:46:49 +0800557static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
558 struct kvm_lapic_irq *irq, u32 min)
559{
560 int i, count = 0;
561 struct kvm_vcpu *vcpu;
562
563 if (min > map->max_apic_id)
564 return 0;
565
566 for_each_set_bit(i, ipi_bitmap,
567 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
568 if (map->phys_map[min + i]) {
569 vcpu = map->phys_map[min + i]->vcpu;
570 count += kvm_apic_set_irq(vcpu, irq, NULL);
571 }
572 }
573
574 return count;
575}
576
Wanpeng Li4180bf12018-07-23 14:39:54 +0800577int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800578 unsigned long ipi_bitmap_high, u32 min,
Wanpeng Li4180bf12018-07-23 14:39:54 +0800579 unsigned long icr, int op_64_bit)
580{
Wanpeng Li4180bf12018-07-23 14:39:54 +0800581 struct kvm_apic_map *map;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800582 struct kvm_lapic_irq irq = {0};
583 int cluster_size = op_64_bit ? 64 : 32;
Miaohe Lin1a686232019-11-09 17:46:49 +0800584 int count;
585
586 if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK))
587 return -KVM_EINVAL;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800588
589 irq.vector = icr & APIC_VECTOR_MASK;
590 irq.delivery_mode = icr & APIC_MODE_MASK;
591 irq.level = (icr & APIC_INT_ASSERT) != 0;
592 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
593
Wanpeng Li4180bf12018-07-23 14:39:54 +0800594 rcu_read_lock();
595 map = rcu_dereference(kvm->arch.apic_map);
596
Miaohe Lin1a686232019-11-09 17:46:49 +0800597 count = -EOPNOTSUPP;
598 if (likely(map)) {
599 count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min);
600 min += cluster_size;
601 count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min);
Wanpeng Li38ab0122018-11-20 09:39:30 +0800602 }
603
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;
Miaohe Lin23520b22020-02-21 22:04:46 +0800630 if (pv_eoi_get_user(vcpu, &val) < 0) {
Yi Wang0d888002019-07-06 01:08:48 +0800631 printk(KERN_WARNING "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);
Miaohe Lin23520b22020-02-21 22:04:46 +0800633 return false;
634 }
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300635 return val & 0x1;
636}
637
638static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
639{
640 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
Yi Wang0d888002019-07-06 01:08:48 +0800641 printk(KERN_WARNING "Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800642 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300643 return;
644 }
645 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
646}
647
648static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
649{
650 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
Yi Wang0d888002019-07-06 01:08:48 +0800651 printk(KERN_WARNING "Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800652 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300653 return;
654 }
655 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
656}
657
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100658static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
659{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100660 int highest_irr;
Liran Alonfa59cc02017-12-24 18:12:53 +0200661 if (apic->vcpu->arch.apicv_active)
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100662 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
663 else
664 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100665 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
666 return -1;
667 return highest_irr;
668}
669
670static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300671{
Avi Kivity3842d132010-07-27 12:30:24 +0300672 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300673 int isr;
674
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500675 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
676 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300677 isr = apic_find_highest_isr(apic);
678 isrv = (isr != -1) ? isr : 0;
679
680 if ((tpr & 0xf0) >= (isrv & 0xf0))
681 ppr = tpr & 0xff;
682 else
683 ppr = isrv & 0xf0;
684
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100685 *new_ppr = ppr;
686 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500687 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100688
689 return ppr < old_ppr;
690}
691
692static void apic_update_ppr(struct kvm_lapic *apic)
693{
694 u32 ppr;
695
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100696 if (__apic_update_ppr(apic, &ppr) &&
697 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100698 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300699}
700
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100701void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
702{
703 apic_update_ppr(vcpu->arch.apic);
704}
705EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
706
Eddie Dong97222cc2007-09-12 10:58:04 +0300707static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
708{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500709 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300710 apic_update_ppr(apic);
711}
712
Radim Krčmář03d22492015-02-12 19:41:31 +0100713static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300714{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100715 return mda == (apic_x2apic_mode(apic) ?
716 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300717}
718
Radim Krčmář03d22492015-02-12 19:41:31 +0100719static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300720{
Radim Krčmář03d22492015-02-12 19:41:31 +0100721 if (kvm_apic_broadcast(apic, mda))
722 return true;
723
724 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100725 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100726
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100727 /*
728 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
729 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
730 * this allows unique addressing of VCPUs with APIC ID over 0xff.
731 * The 0xff condition is needed because writeable xAPIC ID.
732 */
733 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
734 return true;
735
Radim Krčmářb4535b52016-12-15 18:06:47 +0100736 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300737}
738
Radim Krčmář52c233a2015-01-29 22:48:48 +0100739static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300740{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300741 u32 logical_id;
742
Nadav Amit394457a2014-10-03 00:30:52 +0300743 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100744 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300745
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500746 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300747
Radim Krčmář9368b562015-01-29 22:48:49 +0100748 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100749 return ((logical_id >> 16) == (mda >> 16))
750 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100751
752 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300753
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500754 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300755 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100756 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300757 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100758 return ((logical_id >> 4) == (mda >> 4))
759 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300760 default:
Radim Krčmář9368b562015-01-29 22:48:49 +0100761 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300762 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300763}
764
Radim Krčmářc5192652016-07-12 22:09:28 +0200765/* The KVM local APIC implementation has two quirks:
766 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100767 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
768 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
769 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200770 *
771 * - in-kernel IOAPIC messages have to be delivered directly to
772 * x2APIC, because the kernel does not support interrupt remapping.
773 * In order to support broadcast without interrupt remapping, x2APIC
774 * rewrites the destination of non-IPI messages from APIC_BROADCAST
775 * to X2APIC_BROADCAST.
776 *
777 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
778 * important when userspace wants to use x2APIC-format MSIs, because
779 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100780 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200781static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
782 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100783{
784 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100785
Radim Krčmářc5192652016-07-12 22:09:28 +0200786 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100787 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100788 return X2APIC_BROADCAST;
789
Radim Krčmářb4535b52016-12-15 18:06:47 +0100790 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100791}
792
Radim Krčmář52c233a2015-01-29 22:48:48 +0100793bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Peter Xu5c69d5c2019-12-04 20:07:20 +0100794 int shorthand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300795{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800796 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200797 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300798
Zachary Amsdenbd371392010-06-14 11:42:15 -1000799 ASSERT(target);
Peter Xu5c69d5c2019-12-04 20:07:20 +0100800 switch (shorthand) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300801 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100802 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100803 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200804 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100805 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300806 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100807 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300808 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100809 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300810 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100811 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300812 default:
Radim Krčmář9368b562015-01-29 22:48:49 +0100813 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300814 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300815}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500816EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300817
Feng Wu520040142016-01-25 16:53:33 +0800818int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
819 const unsigned long *bitmap, u32 bitmap_size)
820{
821 u32 mod;
822 int i, idx = -1;
823
824 mod = vector % dest_vcpus;
825
826 for (i = 0; i <= mod; i++) {
827 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
828 BUG_ON(idx == bitmap_size);
829 }
830
831 return idx;
832}
833
Radim Krčmář4efd8052016-02-12 15:00:15 +0100834static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
835{
836 if (!kvm->arch.disabled_lapic_found) {
837 kvm->arch.disabled_lapic_found = true;
838 printk(KERN_INFO
839 "Disabled LAPIC found during irq injection\n");
840 }
841}
842
Radim Krčmářc5192652016-07-12 22:09:28 +0200843static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
844 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
845{
846 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
847 if ((irq->dest_id == APIC_BROADCAST &&
848 map->mode != KVM_APIC_MODE_X2APIC))
849 return true;
850 if (irq->dest_id == X2APIC_BROADCAST)
851 return true;
852 } else {
853 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
854 if (irq->dest_id == (x2apic_ipi ?
855 X2APIC_BROADCAST : APIC_BROADCAST))
856 return true;
857 }
858
859 return false;
860}
861
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200862/* Return true if the interrupt can be handled by using *bitmap as index mask
863 * for valid destinations in *dst array.
864 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
865 * Note: we may have zero kvm_lapic destinations when we return true, which
866 * means that the interrupt should be dropped. In this case, *bitmap would be
867 * zero and *dst undefined.
868 */
869static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
870 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
871 struct kvm_apic_map *map, struct kvm_lapic ***dst,
872 unsigned long *bitmap)
873{
874 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200875
876 if (irq->shorthand == APIC_DEST_SELF && src) {
877 *dst = src;
878 *bitmap = 1;
879 return true;
880 } else if (irq->shorthand)
881 return false;
882
Radim Krčmářc5192652016-07-12 22:09:28 +0200883 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200884 return false;
885
886 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200887 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200888 *bitmap = 0;
889 } else {
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200890 u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
891 *dst = &map->phys_map[dest_id];
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200892 *bitmap = 1;
893 }
894 return true;
895 }
896
Radim Krčmáře45115b2016-07-12 22:09:19 +0200897 *bitmap = 0;
898 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
899 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200900 return false;
901
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200902 if (!kvm_lowest_prio_delivery(irq))
903 return true;
904
905 if (!kvm_vector_hashing_enabled()) {
906 lowest = -1;
907 for_each_set_bit(i, bitmap, 16) {
908 if (!(*dst)[i])
909 continue;
910 if (lowest < 0)
911 lowest = i;
912 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
913 (*dst)[lowest]->vcpu) < 0)
914 lowest = i;
915 }
916 } else {
917 if (!*bitmap)
918 return true;
919
920 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
921 bitmap, 16);
922
923 if (!(*dst)[lowest]) {
924 kvm_apic_disabled_lapic_found(kvm);
925 *bitmap = 0;
926 return true;
927 }
928 }
929
930 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
931
932 return true;
933}
934
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300935bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100936 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300937{
938 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200939 unsigned long bitmap;
940 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300941 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200942 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300943
944 *r = -1;
945
946 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800947 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300948 return true;
949 }
950
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300951 rcu_read_lock();
952 map = rcu_dereference(kvm->arch.apic_map);
953
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200954 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200955 if (ret) {
956 *r = 0;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200957 for_each_set_bit(i, &bitmap, 16) {
958 if (!dst[i])
959 continue;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200960 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100961 }
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200962 }
Radim Krčmář3548a252015-02-12 19:41:33 +0100963
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300964 rcu_read_unlock();
965 return ret;
966}
967
Feng Wu6228a0d2016-01-25 16:53:34 +0800968/*
Miaohe Lin00116792019-12-11 14:26:23 +0800969 * This routine tries to handle interrupts in posted mode, here is how
Feng Wu6228a0d2016-01-25 16:53:34 +0800970 * it deals with different cases:
971 * - For single-destination interrupts, handle it in posted mode
972 * - Else if vector hashing is enabled and it is a lowest-priority
973 * interrupt, handle it in posted mode and use the following mechanism
Miaohe Lin67b0ae42019-12-11 14:26:22 +0800974 * to find the destination vCPU.
Feng Wu6228a0d2016-01-25 16:53:34 +0800975 * 1. For lowest-priority interrupts, store all the possible
976 * destination vCPUs in an array.
977 * 2. Use "guest vector % max number of destination vCPUs" to find
978 * the right destination vCPU in the array for the lowest-priority
979 * interrupt.
980 * - Otherwise, use remapped mode to inject the interrupt.
981 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800982bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
983 struct kvm_vcpu **dest_vcpu)
984{
985 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200986 unsigned long bitmap;
987 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800988 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800989
990 if (irq->shorthand)
991 return false;
992
993 rcu_read_lock();
994 map = rcu_dereference(kvm->arch.apic_map);
995
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200996 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
997 hweight16(bitmap) == 1) {
998 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800999
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001000 if (dst[i]) {
1001 *dest_vcpu = dst[i]->vcpu;
1002 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +08001003 }
Feng Wu8feb4a02015-09-18 22:29:47 +08001004 }
1005
Feng Wu8feb4a02015-09-18 22:29:47 +08001006 rcu_read_unlock();
1007 return ret;
1008}
1009
Eddie Dong97222cc2007-09-12 10:58:04 +03001010/*
1011 * Add a pending IRQ into lapic.
1012 * Return 1 if successfully added and 0 if discarded.
1013 */
1014static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +08001015 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001016 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +03001017{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001018 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +03001019 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +03001020
Paolo Bonzinia183b632014-09-11 11:51:02 +02001021 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1022 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001023 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001024 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +02001025 vcpu->arch.apic_arb_prio++;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001026 /* fall through */
Gleb Natapove1035712009-03-05 16:34:59 +02001027 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001028 if (unlikely(trig_mode && !level))
1029 break;
1030
Eddie Dong97222cc2007-09-12 10:58:04 +03001031 /* FIXME add logic for vcpu on reset */
1032 if (unlikely(!apic_enabled(apic)))
1033 break;
1034
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001035 result = 1;
1036
Joerg Roedel9daa5002016-02-29 16:04:44 +01001037 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001038 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +01001039 dest_map->vectors[vcpu->vcpu_id] = vector;
1040 }
Avi Kivitya5d36f82009-12-29 12:42:16 +02001041
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001042 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1043 if (trig_mode)
Wei Yangee171d22019-03-31 19:17:22 -07001044 kvm_lapic_set_vector(vector,
1045 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001046 else
Wei Yangee171d22019-03-31 19:17:22 -07001047 kvm_lapic_clear_vector(vector,
1048 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001049 }
1050
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01001051 if (kvm_x86_ops->deliver_posted_interrupt(vcpu, vector)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001052 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +08001053 kvm_make_request(KVM_REQ_EVENT, vcpu);
1054 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001055 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001056 break;
1057
1058 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +05301059 result = 1;
1060 vcpu->arch.pv.pv_unhalted = 1;
1061 kvm_make_request(KVM_REQ_EVENT, vcpu);
1062 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001063 break;
1064
1065 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +02001066 result = 1;
1067 kvm_make_request(KVM_REQ_SMI, vcpu);
1068 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001069 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001070
Eddie Dong97222cc2007-09-12 10:58:04 +03001071 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001072 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001073 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001074 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001075 break;
1076
1077 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001078 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001079 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001080 /* assumes that there are only KVM_APIC_INIT/SIPI */
1081 apic->pending_events = (1UL << KVM_APIC_INIT);
Avi Kivity3842d132010-07-27 12:30:24 +03001082 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001083 kvm_vcpu_kick(vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001084 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001085 break;
1086
1087 case APIC_DM_STARTUP:
Jan Kiszka66450a22013-03-13 12:42:34 +01001088 result = 1;
1089 apic->sipi_vector = vector;
1090 /* make sure sipi_vector is visible for the receiver */
1091 smp_wmb();
1092 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1093 kvm_make_request(KVM_REQ_EVENT, vcpu);
1094 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001095 break;
1096
Jan Kiszka23930f92008-09-26 09:30:52 +02001097 case APIC_DM_EXTINT:
1098 /*
1099 * Should only be called by kvm_apic_local_deliver() with LVT0,
1100 * before NMI watchdog was enabled. Already handled by
1101 * kvm_apic_accept_pic_intr().
1102 */
1103 break;
1104
Eddie Dong97222cc2007-09-12 10:58:04 +03001105 default:
1106 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1107 delivery_mode);
1108 break;
1109 }
1110 return result;
1111}
1112
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05001113/*
1114 * This routine identifies the destination vcpus mask meant to receive the
1115 * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find
1116 * out the destination vcpus array and set the bitmap or it traverses to
1117 * each available vcpu to identify the same.
1118 */
1119void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
1120 unsigned long *vcpu_bitmap)
1121{
1122 struct kvm_lapic **dest_vcpu = NULL;
1123 struct kvm_lapic *src = NULL;
1124 struct kvm_apic_map *map;
1125 struct kvm_vcpu *vcpu;
1126 unsigned long bitmap;
1127 int i, vcpu_idx;
1128 bool ret;
1129
1130 rcu_read_lock();
1131 map = rcu_dereference(kvm->arch.apic_map);
1132
1133 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu,
1134 &bitmap);
1135 if (ret) {
1136 for_each_set_bit(i, &bitmap, 16) {
1137 if (!dest_vcpu[i])
1138 continue;
1139 vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx;
1140 __set_bit(vcpu_idx, vcpu_bitmap);
1141 }
1142 } else {
1143 kvm_for_each_vcpu(i, vcpu, kvm) {
1144 if (!kvm_apic_present(vcpu))
1145 continue;
1146 if (!kvm_apic_match_dest(vcpu, NULL,
Peter Xub4b29632019-12-04 20:07:16 +01001147 irq->shorthand,
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05001148 irq->dest_id,
1149 irq->dest_mode))
1150 continue;
1151 __set_bit(i, vcpu_bitmap);
1152 }
1153 }
1154 rcu_read_unlock();
1155}
1156
Gleb Natapove1035712009-03-05 16:34:59 +02001157int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001158{
Gleb Natapove1035712009-03-05 16:34:59 +02001159 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001160}
1161
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001162static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1163{
Andrey Smetanin63086302015-11-10 15:36:32 +03001164 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001165}
1166
Yang Zhangc7c9c562013-01-25 10:18:51 +08001167static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1168{
Steve Rutherford7543a632015-07-29 23:21:41 -07001169 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001170
Steve Rutherford7543a632015-07-29 23:21:41 -07001171 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1172 if (!kvm_ioapic_handles_vector(apic, vector))
1173 return;
1174
1175 /* Request a KVM exit to inform the userspace IOAPIC. */
1176 if (irqchip_split(apic->vcpu->kvm)) {
1177 apic->vcpu->arch.pending_ioapic_eoi = vector;
1178 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1179 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001180 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001181
1182 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1183 trigger_mode = IOAPIC_LEVEL_TRIG;
1184 else
1185 trigger_mode = IOAPIC_EDGE_TRIG;
1186
1187 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001188}
1189
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001190static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001191{
1192 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001193
1194 trace_kvm_eoi(apic, vector);
1195
Eddie Dong97222cc2007-09-12 10:58:04 +03001196 /*
1197 * Not every write EOI will has corresponding ISR,
1198 * one example is when Kernel check timer on setup_IO_APIC
1199 */
1200 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001201 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001202
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001203 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001204 apic_update_ppr(apic);
1205
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001206 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1207 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1208
Yang Zhangc7c9c562013-01-25 10:18:51 +08001209 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001210 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001211 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001212}
1213
Yang Zhangc7c9c562013-01-25 10:18:51 +08001214/*
1215 * this interface assumes a trap-like exit, which has already finished
1216 * desired side effect including vISR and vPPR update.
1217 */
1218void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1219{
1220 struct kvm_lapic *apic = vcpu->arch.apic;
1221
1222 trace_kvm_eoi(apic, vector);
1223
1224 kvm_ioapic_send_eoi(apic, vector);
1225 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1226}
1227EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1228
Wanpeng Li2b0911d2019-09-05 14:26:27 +08001229static void apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
Eddie Dong97222cc2007-09-12 10:58:04 +03001230{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001231 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001232
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001233 irq.vector = icr_low & APIC_VECTOR_MASK;
1234 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1235 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001236 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001237 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1238 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001239 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001240 if (apic_x2apic_mode(apic))
1241 irq.dest_id = icr_high;
1242 else
1243 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001244
Gleb Natapov1000ff82009-07-07 16:00:57 +03001245 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1246
Yang Zhangb4f22252013-04-11 19:21:37 +08001247 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001248}
1249
1250static u32 apic_get_tmcct(struct kvm_lapic *apic)
1251{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001252 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001253 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001254 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001255
1256 ASSERT(apic != NULL);
1257
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001258 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001259 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001260 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001261 return 0;
1262
Paolo Bonzini55878592016-10-25 15:23:49 +02001263 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001264 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001265 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001266 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001267
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001268 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1269 tmcct = div64_u64(ns,
1270 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001271
1272 return tmcct;
1273}
1274
Avi Kivityb209749f2007-10-22 16:50:39 +02001275static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1276{
1277 struct kvm_vcpu *vcpu = apic->vcpu;
1278 struct kvm_run *run = vcpu->run;
1279
Avi Kivitya8eeb042010-05-10 12:34:53 +03001280 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001281 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001282 run->tpr_access.is_write = write;
1283}
1284
1285static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1286{
1287 if (apic->vcpu->arch.tpr_access_reporting)
1288 __report_tpr_access(apic, write);
1289}
1290
Eddie Dong97222cc2007-09-12 10:58:04 +03001291static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1292{
1293 u32 val = 0;
1294
1295 if (offset >= LAPIC_MMIO_LENGTH)
1296 return 0;
1297
1298 switch (offset) {
1299 case APIC_ARBPRI:
Eddie Dong97222cc2007-09-12 10:58:04 +03001300 break;
1301
1302 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001303 if (apic_lvtt_tscdeadline(apic))
1304 return 0;
1305
Eddie Dong97222cc2007-09-12 10:58:04 +03001306 val = apic_get_tmcct(apic);
1307 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001308 case APIC_PROCPRI:
1309 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001310 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001311 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001312 case APIC_TASKPRI:
1313 report_tpr_access(apic, false);
1314 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001315 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001316 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001317 break;
1318 }
1319
1320 return val;
1321}
1322
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001323static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1324{
1325 return container_of(dev, struct kvm_lapic, dev);
1326}
1327
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001328#define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1329#define APIC_REGS_MASK(first, count) \
1330 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1331
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001332int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001333 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001334{
Eddie Dong97222cc2007-09-12 10:58:04 +03001335 unsigned char alignment = offset & 0xf;
1336 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001337 /* this bitmask has a bit cleared for each reserved register */
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001338 u64 valid_reg_mask =
1339 APIC_REG_MASK(APIC_ID) |
1340 APIC_REG_MASK(APIC_LVR) |
1341 APIC_REG_MASK(APIC_TASKPRI) |
1342 APIC_REG_MASK(APIC_PROCPRI) |
1343 APIC_REG_MASK(APIC_LDR) |
1344 APIC_REG_MASK(APIC_DFR) |
1345 APIC_REG_MASK(APIC_SPIV) |
1346 APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1347 APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1348 APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1349 APIC_REG_MASK(APIC_ESR) |
1350 APIC_REG_MASK(APIC_ICR) |
1351 APIC_REG_MASK(APIC_ICR2) |
1352 APIC_REG_MASK(APIC_LVTT) |
1353 APIC_REG_MASK(APIC_LVTTHMR) |
1354 APIC_REG_MASK(APIC_LVTPC) |
1355 APIC_REG_MASK(APIC_LVT0) |
1356 APIC_REG_MASK(APIC_LVT1) |
1357 APIC_REG_MASK(APIC_LVTERR) |
1358 APIC_REG_MASK(APIC_TMICT) |
1359 APIC_REG_MASK(APIC_TMCCT) |
1360 APIC_REG_MASK(APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001361
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001362 /* ARBPRI is not valid on x2APIC */
1363 if (!apic_x2apic_mode(apic))
1364 valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001365
Yi Wang0d888002019-07-06 01:08:48 +08001366 if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001367 return 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001368
Eddie Dong97222cc2007-09-12 10:58:04 +03001369 result = __apic_read(apic, offset & ~0xf);
1370
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001371 trace_kvm_apic_read(offset, result);
1372
Eddie Dong97222cc2007-09-12 10:58:04 +03001373 switch (len) {
1374 case 1:
1375 case 2:
1376 case 4:
1377 memcpy(data, (char *)&result + alignment, len);
1378 break;
1379 default:
1380 printk(KERN_ERR "Local APIC read with len = %x, "
1381 "should be 1,2, or 4 instead\n", len);
1382 break;
1383 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001384 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001385}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001386EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001387
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001388static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1389{
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001390 return addr >= apic->base_address &&
1391 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001392}
1393
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001394static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001395 gpa_t address, int len, void *data)
1396{
1397 struct kvm_lapic *apic = to_lapic(this);
1398 u32 offset = address - apic->base_address;
1399
1400 if (!apic_mmio_in_range(apic, address))
1401 return -EOPNOTSUPP;
1402
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001403 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1404 if (!kvm_check_has_quirk(vcpu->kvm,
1405 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1406 return -EOPNOTSUPP;
1407
1408 memset(data, 0xff, len);
1409 return 0;
1410 }
1411
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001412 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001413
1414 return 0;
1415}
1416
Eddie Dong97222cc2007-09-12 10:58:04 +03001417static void update_divide_count(struct kvm_lapic *apic)
1418{
1419 u32 tmp1, tmp2, tdcr;
1420
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001421 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001422 tmp1 = tdcr & 0xf;
1423 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001424 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001425}
1426
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001427static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1428{
1429 /*
1430 * Do not allow the guest to program periodic timers with small
1431 * interval, since the hrtimers are not throttled by the host
1432 * scheduler.
1433 */
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001434 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001435 s64 min_period = min_timer_period_us * 1000LL;
1436
1437 if (apic->lapic_timer.period < min_period) {
1438 pr_info_ratelimited(
1439 "kvm: vcpu %i: requested %lld ns "
1440 "lapic timer period limited to %lld ns\n",
1441 apic->vcpu->vcpu_id,
1442 apic->lapic_timer.period, min_period);
1443 apic->lapic_timer.period = min_period;
1444 }
1445 }
1446}
1447
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001448static void apic_update_lvtt(struct kvm_lapic *apic)
1449{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001450 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001451 apic->lapic_timer.timer_mode_mask;
1452
1453 if (apic->lapic_timer.timer_mode != timer_mode) {
Wanpeng Lic69518c2017-10-05 03:53:51 -07001454 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001455 APIC_LVT_TIMER_TSCDEADLINE)) {
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001456 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmář44275932017-10-06 19:25:55 +02001457 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1458 apic->lapic_timer.period = 0;
1459 apic->lapic_timer.tscdeadline = 0;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001460 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001461 apic->lapic_timer.timer_mode = timer_mode;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001462 limit_periodic_timer_frequency(apic);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001463 }
1464}
1465
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001466/*
1467 * On APICv, this test will cause a busy wait
1468 * during a higher-priority task.
1469 */
1470
1471static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1472{
1473 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001474 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001475
1476 if (kvm_apic_hw_enabled(apic)) {
1477 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001478 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001479
Andrey Smetanind62caab2015-11-10 15:36:33 +03001480 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001481 bitmap = apic->regs + APIC_IRR;
1482
1483 if (apic_test_vector(vec, bitmap))
1484 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001485 }
1486 return false;
1487}
1488
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001489static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1490{
1491 u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1492
1493 /*
1494 * If the guest TSC is running at a different ratio than the host, then
1495 * convert the delay to nanoseconds to achieve an accurate delay. Note
1496 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1497 * always for VMX enabled hardware.
1498 */
1499 if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) {
1500 __delay(min(guest_cycles,
1501 nsec_to_cycles(vcpu, timer_advance_ns)));
1502 } else {
1503 u64 delay_ns = guest_cycles * 1000000ULL;
1504 do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1505 ndelay(min_t(u32, delay_ns, timer_advance_ns));
1506 }
1507}
1508
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001509static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
Wanpeng Liec0671d2019-05-20 16:18:08 +08001510 s64 advance_expire_delta)
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001511{
1512 struct kvm_lapic *apic = vcpu->arch.apic;
Sean Christopherson39497d72019-04-17 10:15:32 -07001513 u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001514 u64 ns;
1515
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001516 /* Do not adjust for tiny fluctuations or large random spikes. */
1517 if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
1518 abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
1519 return;
1520
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001521 /* too early */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001522 if (advance_expire_delta < 0) {
1523 ns = -advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001524 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001525 timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001526 } else {
1527 /* too late */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001528 ns = advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001529 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001530 timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001531 }
1532
Wanpeng Lia0f00372019-09-26 08:54:03 +08001533 if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
1534 timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001535 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1536}
1537
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001538static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001539{
1540 struct kvm_lapic *apic = vcpu->arch.apic;
1541 u64 guest_tsc, tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001542
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001543 if (apic->lapic_timer.expired_tscdeadline == 0)
1544 return;
1545
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001546 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1547 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001548 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Wanpeng Liec0671d2019-05-20 16:18:08 +08001549 apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001550
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001551 if (guest_tsc < tsc_deadline)
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001552 __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
Wanpeng Li3b8a5df2018-10-09 09:02:08 +08001553
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001554 if (lapic_timer_advance_dynamic)
Wanpeng Liec0671d2019-05-20 16:18:08 +08001555 adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001556}
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001557
1558void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
1559{
1560 if (lapic_timer_int_injected(vcpu))
1561 __kvm_wait_lapic_expire(vcpu);
1562}
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08001563EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001564
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001565static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
1566{
1567 struct kvm_timer *ktimer = &apic->lapic_timer;
1568
1569 kvm_apic_local_deliver(apic, APIC_LVTT);
Haiwei Li17ac43a2020-01-16 16:50:21 +08001570 if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001571 ktimer->tscdeadline = 0;
Haiwei Li17ac43a2020-01-16 16:50:21 +08001572 } else if (apic_lvtt_oneshot(apic)) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001573 ktimer->tscdeadline = 0;
1574 ktimer->target_expiration = 0;
1575 }
1576}
1577
1578static void apic_timer_expired(struct kvm_lapic *apic)
1579{
1580 struct kvm_vcpu *vcpu = apic->vcpu;
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001581 struct kvm_timer *ktimer = &apic->lapic_timer;
1582
1583 if (atomic_read(&apic->lapic_timer.pending))
1584 return;
1585
1586 if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
1587 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1588
1589 if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
1590 if (apic->lapic_timer.timer_advance_ns)
1591 __kvm_wait_lapic_expire(vcpu);
1592 kvm_apic_inject_pending_timer_irqs(apic);
1593 return;
1594 }
1595
1596 atomic_inc(&apic->lapic_timer.pending);
1597 kvm_set_pending_timer(vcpu);
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001598}
1599
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001600static void start_sw_tscdeadline(struct kvm_lapic *apic)
1601{
Sean Christopherson39497d72019-04-17 10:15:32 -07001602 struct kvm_timer *ktimer = &apic->lapic_timer;
1603 u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001604 u64 ns = 0;
1605 ktime_t expire;
1606 struct kvm_vcpu *vcpu = apic->vcpu;
1607 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1608 unsigned long flags;
1609 ktime_t now;
1610
1611 if (unlikely(!tscdeadline || !this_tsc_khz))
1612 return;
1613
1614 local_irq_save(flags);
1615
Paolo Bonzini55878592016-10-25 15:23:49 +02001616 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001617 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Liran Alonc09d65d2019-04-16 20:36:34 +03001618
1619 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1620 do_div(ns, this_tsc_khz);
1621
1622 if (likely(tscdeadline > guest_tsc) &&
Sean Christopherson39497d72019-04-17 10:15:32 -07001623 likely(ns > apic->lapic_timer.timer_advance_ns)) {
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001624 expire = ktime_add_ns(now, ns);
Sean Christopherson39497d72019-04-17 10:15:32 -07001625 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02001626 hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001627 } else
1628 apic_timer_expired(apic);
1629
1630 local_irq_restore(flags);
1631}
1632
Wanpeng Lic301b902017-10-06 07:38:32 -07001633static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1634{
1635 ktime_t now, remaining;
1636 u64 ns_remaining_old, ns_remaining_new;
1637
1638 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1639 * APIC_BUS_CYCLE_NS * apic->divide_count;
1640 limit_periodic_timer_frequency(apic);
1641
1642 now = ktime_get();
1643 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1644 if (ktime_to_ns(remaining) < 0)
1645 remaining = 0;
1646
1647 ns_remaining_old = ktime_to_ns(remaining);
1648 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1649 apic->divide_count, old_divisor);
1650
1651 apic->lapic_timer.tscdeadline +=
1652 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1653 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1654 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1655}
1656
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001657static bool set_target_expiration(struct kvm_lapic *apic)
1658{
1659 ktime_t now;
1660 u64 tscl = rdtsc();
1661
Paolo Bonzini55878592016-10-25 15:23:49 +02001662 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001663 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1664 * APIC_BUS_CYCLE_NS * apic->divide_count;
1665
Radim Krčmář5d74a692017-10-06 19:25:54 +02001666 if (!apic->lapic_timer.period) {
1667 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001668 return false;
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001669 }
1670
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001671 limit_periodic_timer_frequency(apic);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001672
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001673 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1674 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1675 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1676
1677 return true;
1678}
1679
1680static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1681{
David Vrabeld8f2f492018-05-18 16:55:46 +01001682 ktime_t now = ktime_get();
1683 u64 tscl = rdtsc();
1684 ktime_t delta;
1685
1686 /*
1687 * Synchronize both deadlines to the same time source or
1688 * differences in the periods (caused by differences in the
1689 * underlying clocks or numerical approximation errors) will
1690 * cause the two to drift apart over time as the errors
1691 * accumulate.
1692 */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001693 apic->lapic_timer.target_expiration =
1694 ktime_add_ns(apic->lapic_timer.target_expiration,
1695 apic->lapic_timer.period);
David Vrabeld8f2f492018-05-18 16:55:46 +01001696 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1697 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1698 nsec_to_cycles(apic->vcpu, delta);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001699}
1700
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001701static void start_sw_period(struct kvm_lapic *apic)
1702{
1703 if (!apic->lapic_timer.period)
1704 return;
1705
1706 if (ktime_after(ktime_get(),
1707 apic->lapic_timer.target_expiration)) {
1708 apic_timer_expired(apic);
1709
1710 if (apic_lvtt_oneshot(apic))
1711 return;
1712
1713 advance_periodic_target_expiration(apic);
1714 }
1715
1716 hrtimer_start(&apic->lapic_timer.timer,
1717 apic->lapic_timer.target_expiration,
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001718 HRTIMER_MODE_ABS);
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001719}
1720
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001721bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1722{
Wanpeng Li91005302016-08-03 12:04:12 +08001723 if (!lapic_in_kernel(vcpu))
1724 return false;
1725
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001726 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1727}
1728EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1729
Wanpeng Li7e810a32016-10-24 18:23:12 +08001730static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001731{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001732 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001733 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
Wanpeng Libd97ad02016-06-30 08:52:49 +08001734 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1735 apic->lapic_timer.hv_timer_in_use = false;
1736}
1737
Paolo Bonzinia749e242017-06-29 17:14:50 +02001738static bool start_hv_timer(struct kvm_lapic *apic)
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001739{
1740 struct kvm_timer *ktimer = &apic->lapic_timer;
Sean Christophersonf9927982019-04-16 13:32:46 -07001741 struct kvm_vcpu *vcpu = apic->vcpu;
1742 bool expired;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001743
Wanpeng Li1d518c62017-07-25 00:43:15 -07001744 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001745 if (!kvm_x86_ops->set_hv_timer)
1746 return false;
1747
Radim Krčmář86bbc1e2017-10-06 19:25:53 +02001748 if (!ktimer->tscdeadline)
1749 return false;
1750
Sean Christophersonf9927982019-04-16 13:32:46 -07001751 if (kvm_x86_ops->set_hv_timer(vcpu, ktimer->tscdeadline, &expired))
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001752 return false;
1753
1754 ktimer->hv_timer_in_use = true;
1755 hrtimer_cancel(&ktimer->timer);
1756
1757 /*
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001758 * To simplify handling the periodic timer, leave the hv timer running
1759 * even if the deadline timer has expired, i.e. rely on the resulting
1760 * VM-Exit to recompute the periodic timer's target expiration.
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001761 */
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001762 if (!apic_lvtt_period(apic)) {
1763 /*
1764 * Cancel the hv timer if the sw timer fired while the hv timer
1765 * was being programmed, or if the hv timer itself expired.
1766 */
1767 if (atomic_read(&ktimer->pending)) {
1768 cancel_hv_timer(apic);
Sean Christophersonf9927982019-04-16 13:32:46 -07001769 } else if (expired) {
Wanpeng Lic8533542017-06-29 06:28:09 -07001770 apic_timer_expired(apic);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001771 cancel_hv_timer(apic);
1772 }
Wanpeng Lic8533542017-06-29 06:28:09 -07001773 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02001774
Sean Christophersonf9927982019-04-16 13:32:46 -07001775 trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001776
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001777 return true;
1778}
1779
Paolo Bonzinia749e242017-06-29 17:14:50 +02001780static void start_sw_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001781{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001782 struct kvm_timer *ktimer = &apic->lapic_timer;
Wanpeng Li1d518c62017-07-25 00:43:15 -07001783
1784 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001785 if (apic->lapic_timer.hv_timer_in_use)
1786 cancel_hv_timer(apic);
1787 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1788 return;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001789
Paolo Bonzinia749e242017-06-29 17:14:50 +02001790 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1791 start_sw_period(apic);
1792 else if (apic_lvtt_tscdeadline(apic))
1793 start_sw_tscdeadline(apic);
1794 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1795}
1796
1797static void restart_apic_timer(struct kvm_lapic *apic)
1798{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001799 preempt_disable();
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001800
1801 if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
1802 goto out;
1803
Paolo Bonzinia749e242017-06-29 17:14:50 +02001804 if (!start_hv_timer(apic))
1805 start_sw_timer(apic);
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001806out:
Wanpeng Li1d518c62017-07-25 00:43:15 -07001807 preempt_enable();
Wanpeng Li196f20c2016-06-28 14:54:19 +08001808}
1809
Eddie Dong97222cc2007-09-12 10:58:04 +03001810void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1811{
1812 struct kvm_lapic *apic = vcpu->arch.apic;
1813
Wanpeng Li1d518c62017-07-25 00:43:15 -07001814 preempt_disable();
1815 /* If the preempt notifier has already run, it also called apic_timer_expired */
1816 if (!apic->lapic_timer.hv_timer_in_use)
1817 goto out;
Eddie Dong97222cc2007-09-12 10:58:04 +03001818 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001819 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001820 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001821
1822 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1823 advance_periodic_target_expiration(apic);
Paolo Bonzinia749e242017-06-29 17:14:50 +02001824 restart_apic_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001825 }
Wanpeng Li1d518c62017-07-25 00:43:15 -07001826out:
1827 preempt_enable();
Eddie Dong97222cc2007-09-12 10:58:04 +03001828}
1829EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1830
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001831void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1832{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001833 restart_apic_timer(vcpu->arch.apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001834}
1835EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1836
1837void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1838{
1839 struct kvm_lapic *apic = vcpu->arch.apic;
1840
Wanpeng Li1d518c62017-07-25 00:43:15 -07001841 preempt_disable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001842 /* Possibly the TSC deadline timer is not enabled yet */
Paolo Bonzinia749e242017-06-29 17:14:50 +02001843 if (apic->lapic_timer.hv_timer_in_use)
1844 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07001845 preempt_enable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001846}
1847EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1848
Paolo Bonzinia749e242017-06-29 17:14:50 +02001849void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1850{
1851 struct kvm_lapic *apic = vcpu->arch.apic;
1852
1853 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1854 restart_apic_timer(apic);
1855}
1856
Eddie Dong97222cc2007-09-12 10:58:04 +03001857static void start_apic_timer(struct kvm_lapic *apic)
1858{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001859 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001860
Paolo Bonzinia749e242017-06-29 17:14:50 +02001861 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1862 && !set_target_expiration(apic))
1863 return;
1864
1865 restart_apic_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001866}
1867
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001868static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1869{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001870 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001871
Radim Krčmář59fd1322015-06-30 22:19:16 +02001872 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1873 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1874 if (lvt0_in_nmi_mode) {
Radim Krčmář42720132015-07-01 15:31:49 +02001875 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001876 } else
1877 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1878 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001879}
1880
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001881int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001882{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001883 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001884
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001885 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001886
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001887 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001888 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001889 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001890 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001891 else
1892 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001893 break;
1894
1895 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001896 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001897 apic_set_tpr(apic, val & 0xff);
1898 break;
1899
1900 case APIC_EOI:
1901 apic_set_eoi(apic);
1902 break;
1903
1904 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001905 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001906 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001907 else
1908 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001909 break;
1910
1911 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001912 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001913 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001914 recalculate_apic_map(apic->vcpu->kvm);
1915 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001916 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001917 break;
1918
Gleb Natapovfc61b802009-07-05 17:39:35 +03001919 case APIC_SPIV: {
1920 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001921 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001922 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001923 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001924 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1925 int i;
1926 u32 lvt_val;
1927
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001928 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001929 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001930 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001931 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001932 lvt_val | APIC_LVT_MASKED);
1933 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001934 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001935 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001936
1937 }
1938 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001939 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001940 case APIC_ICR:
1941 /* No delay here, so we always clear the pending bit */
Wanpeng Li2b0911d2019-09-05 14:26:27 +08001942 val &= ~(1 << 12);
1943 apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
1944 kvm_lapic_set_reg(apic, APIC_ICR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001945 break;
1946
1947 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001948 if (!apic_x2apic_mode(apic))
1949 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001950 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001951 break;
1952
Jan Kiszka23930f92008-09-26 09:30:52 +02001953 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001954 apic_manage_nmi_watchdog(apic, val);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001955 /* fall through */
Eddie Dong97222cc2007-09-12 10:58:04 +03001956 case APIC_LVTTHMR:
1957 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001958 case APIC_LVT1:
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08001959 case APIC_LVTERR: {
Eddie Dong97222cc2007-09-12 10:58:04 +03001960 /* TODO: Check vector */
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08001961 size_t size;
1962 u32 index;
1963
Gleb Natapovc48f1492012-08-05 15:58:33 +03001964 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001965 val |= APIC_LVT_MASKED;
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08001966 size = ARRAY_SIZE(apic_lvt_mask);
1967 index = array_index_nospec(
1968 (reg - APIC_LVTT) >> 4, size);
1969 val &= apic_lvt_mask[index];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001970 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001971 break;
Marios Pomonis4bf79cb2019-12-11 12:47:46 -08001972 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001973
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001974 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001975 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001976 val |= APIC_LVT_MASKED;
1977 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001978 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001979 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001980 break;
1981
Eddie Dong97222cc2007-09-12 10:58:04 +03001982 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001983 if (apic_lvtt_tscdeadline(apic))
1984 break;
1985
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001986 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001987 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001988 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001989 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001990
Wanpeng Lic301b902017-10-06 07:38:32 -07001991 case APIC_TDCR: {
1992 uint32_t old_divisor = apic->divide_count;
1993
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001994 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001995 update_divide_count(apic);
Wanpeng Lic301b902017-10-06 07:38:32 -07001996 if (apic->divide_count != old_divisor &&
1997 apic->lapic_timer.period) {
1998 hrtimer_cancel(&apic->lapic_timer.timer);
1999 update_target_expiration(apic, old_divisor);
2000 restart_apic_timer(apic);
2001 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002002 break;
Wanpeng Lic301b902017-10-06 07:38:32 -07002003 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002004 case APIC_ESR:
Yi Wang0d888002019-07-06 01:08:48 +08002005 if (apic_x2apic_mode(apic) && val != 0)
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002006 ret = 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002007 break;
2008
2009 case APIC_SELF_IPI:
2010 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002011 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002012 } else
2013 ret = 1;
2014 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03002015 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002016 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03002017 break;
2018 }
Yi Wang0d888002019-07-06 01:08:48 +08002019
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002020 return ret;
2021}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002022EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002023
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00002024static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002025 gpa_t address, int len, const void *data)
2026{
2027 struct kvm_lapic *apic = to_lapic(this);
2028 unsigned int offset = address - apic->base_address;
2029 u32 val;
2030
2031 if (!apic_mmio_in_range(apic, address))
2032 return -EOPNOTSUPP;
2033
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02002034 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2035 if (!kvm_check_has_quirk(vcpu->kvm,
2036 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2037 return -EOPNOTSUPP;
2038
2039 return 0;
2040 }
2041
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002042 /*
2043 * APIC register must be aligned on 128-bits boundary.
2044 * 32/64/128 bits registers must be accessed thru 32 bits.
2045 * Refer SDM 8.4.1
2046 */
Yi Wang0d888002019-07-06 01:08:48 +08002047 if (len != 4 || (offset & 0xf))
Sheng Yang756975b2009-07-06 11:05:39 +08002048 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002049
2050 val = *(u32*)data;
2051
Yi Wang0d888002019-07-06 01:08:48 +08002052 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002053
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002054 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002055}
2056
Kevin Tian58fbbf22011-08-30 13:56:17 +03002057void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
2058{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002059 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03002060}
2061EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
2062
Yang Zhang83d4c282013-01-25 10:18:49 +08002063/* emulate APIC access in a trap manner */
2064void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
2065{
2066 u32 val = 0;
2067
2068 /* hw has done the conditional check and inst decode */
2069 offset &= 0xff0;
2070
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002071 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002072
2073 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002074 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002075}
2076EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2077
Rusty Russelld5894442007-10-08 10:48:30 +10002078void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002079{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002080 struct kvm_lapic *apic = vcpu->arch.apic;
2081
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002082 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002083 return;
2084
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002085 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002086
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002087 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2088 static_key_slow_dec_deferred(&apic_hw_disabled);
2089
Radim Krčmáře4627552014-10-30 15:06:45 +01002090 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002091 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03002092
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002093 if (apic->regs)
2094 free_page((unsigned long)apic->regs);
2095
2096 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002097}
2098
2099/*
2100 *----------------------------------------------------------------------
2101 * LAPIC interface
2102 *----------------------------------------------------------------------
2103 */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002104u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2105{
2106 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002107
Wanpeng Lia10388e2016-10-24 18:23:10 +08002108 if (!lapic_in_kernel(vcpu) ||
2109 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002110 return 0;
2111
2112 return apic->lapic_timer.tscdeadline;
2113}
2114
2115void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2116{
2117 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002118
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002119 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03002120 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002121 return;
2122
2123 hrtimer_cancel(&apic->lapic_timer.timer);
2124 apic->lapic_timer.tscdeadline = data;
2125 start_apic_timer(apic);
2126}
2127
Eddie Dong97222cc2007-09-12 10:58:04 +03002128void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2129{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002130 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002131
Avi Kivityb93463a2007-10-25 16:52:32 +02002132 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002133 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03002134}
2135
2136u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2137{
Eddie Dong97222cc2007-09-12 10:58:04 +03002138 u64 tpr;
2139
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002140 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03002141
2142 return (tpr & 0xf0) >> 4;
2143}
2144
2145void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2146{
Yang Zhang8d146952013-01-25 10:18:50 +08002147 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002149
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002150 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002151 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002152
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01002153 vcpu->arch.apic_base = value;
2154
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002155 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2156 kvm_update_cpuid(vcpu);
2157
2158 if (!apic)
2159 return;
2160
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002161 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01002162 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02002163 if (value & MSR_IA32_APICBASE_ENABLE) {
2164 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002165 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08002166 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002167 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08002168 recalculate_apic_map(vcpu->kvm);
2169 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002170 }
2171
Jim Mattson8d860bb2018-05-09 16:56:05 -04002172 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2173 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2174
2175 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2176 kvm_x86_ops->set_virtual_apic_mode(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08002177
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002178 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03002179 MSR_IA32_APICBASE_BASE;
2180
Nadav Amitdb324fe2014-11-02 11:54:59 +02002181 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2182 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2183 pr_warn_once("APIC base relocation is unsupported by KVM");
Eddie Dong97222cc2007-09-12 10:58:04 +03002184}
2185
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002186void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
2187{
2188 struct kvm_lapic *apic = vcpu->arch.apic;
2189
2190 if (vcpu->arch.apicv_active) {
2191 /* irr_pending is always true when apicv is activated. */
2192 apic->irr_pending = true;
2193 apic->isr_count = 1;
2194 } else {
2195 apic->irr_pending = (apic_search_irr(apic) != -1);
2196 apic->isr_count = count_vectors(apic->regs + APIC_ISR);
2197 }
2198}
2199EXPORT_SYMBOL_GPL(kvm_apic_update_apicv);
2200
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002201void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03002202{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002203 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002204 int i;
2205
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002206 if (!apic)
2207 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002208
Eddie Dong97222cc2007-09-12 10:58:04 +03002209 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002210 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002211
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002212 if (!init_event) {
2213 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2214 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002215 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002216 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03002217 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002218
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002219 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2220 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002221 apic_update_lvtt(apic);
Jan H. Schönherr52b54192017-05-20 13:24:32 +02002222 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2223 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002224 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03002225 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002226 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03002227
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002228 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002229 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002230 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02002231 if (!apic_x2apic_mode(apic))
2232 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002233 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2234 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2235 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2236 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2237 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002238 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002239 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2240 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2241 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002242 }
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002243 kvm_apic_update_apicv(vcpu);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002244 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02002245 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002246 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002247 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002248 kvm_lapic_set_base(vcpu,
2249 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002250 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002251 apic_update_ppr(apic);
Jan H. Schönherr4191db22017-10-25 16:43:27 +02002252 if (vcpu->arch.apicv_active) {
2253 kvm_x86_ops->apicv_post_state_restore(vcpu);
2254 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2255 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2256 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002257
Gleb Natapove1035712009-03-05 16:34:59 +02002258 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03002259 vcpu->arch.apic_attention = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002260}
2261
Eddie Dong97222cc2007-09-12 10:58:04 +03002262/*
2263 *----------------------------------------------------------------------
2264 * timer interface
2265 *----------------------------------------------------------------------
2266 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03002267
Avi Kivity2a6eac92012-07-26 18:01:51 +03002268static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002269{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002270 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002271}
2272
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002273int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2274{
Gleb Natapov54e98182012-08-05 15:58:32 +03002275 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002276
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01002277 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03002278 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002279
2280 return 0;
2281}
2282
Avi Kivity89342082011-11-10 14:57:21 +02002283int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002284{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002285 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002286 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002287
Gleb Natapovc48f1492012-08-05 15:58:33 +03002288 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002289 vector = reg & APIC_VECTOR_MASK;
2290 mode = reg & APIC_MODE_MASK;
2291 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08002292 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2293 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02002294 }
2295 return 0;
2296}
2297
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002298void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002299{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002300 struct kvm_lapic *apic = vcpu->arch.apic;
2301
2302 if (apic)
2303 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002304}
2305
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002306static const struct kvm_io_device_ops apic_mmio_ops = {
2307 .read = apic_mmio_read,
2308 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002309};
2310
Avi Kivitye9d90d42012-07-26 18:01:50 +03002311static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2312{
2313 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002314 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002315
Radim Krčmář5d87db72014-10-10 19:15:08 +02002316 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002317
Avi Kivity2a6eac92012-07-26 18:01:51 +03002318 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002319 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002320 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2321 return HRTIMER_RESTART;
2322 } else
2323 return HRTIMER_NORESTART;
2324}
2325
Sean Christophersonc3941d92019-04-17 10:15:33 -07002326int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
Eddie Dong97222cc2007-09-12 10:58:04 +03002327{
2328 struct kvm_lapic *apic;
2329
2330 ASSERT(vcpu != NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03002331
Ben Gardon254272c2019-02-11 11:02:50 -08002332 apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
Eddie Dong97222cc2007-09-12 10:58:04 +03002333 if (!apic)
2334 goto nomem;
2335
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002336 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002337
Ben Gardon254272c2019-02-11 11:02:50 -08002338 apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002339 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002340 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2341 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002342 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002343 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002344 apic->vcpu = vcpu;
2345
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002346 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02002347 HRTIMER_MODE_ABS_HARD);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002348 apic->lapic_timer.timer.function = apic_timer_fn;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002349 if (timer_advance_ns == -1) {
Wanpeng Lia0f00372019-09-26 08:54:03 +08002350 apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Wanpeng Lid0f5a862019-09-17 16:16:26 +08002351 lapic_timer_advance_dynamic = true;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002352 } else {
2353 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
Wanpeng Lid0f5a862019-09-17 16:16:26 +08002354 lapic_timer_advance_dynamic = false;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002355 }
2356
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002357 /*
2358 * APIC is created enabled. This will prevent kvm_lapic_set_base from
Wei Yangee171d22019-03-31 19:17:22 -07002359 * thinking that APIC state has changed.
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002360 */
2361 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002362 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002363 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002364
2365 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002366nomem_free_apic:
2367 kfree(apic);
Saar Amara251fb902019-05-06 11:29:16 +03002368 vcpu->arch.apic = NULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002369nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002370 return -ENOMEM;
2371}
Eddie Dong97222cc2007-09-12 10:58:04 +03002372
2373int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2374{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002375 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002376 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002377
Wanpeng Libb34e692019-07-02 17:25:02 +08002378 if (!kvm_apic_hw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002379 return -1;
2380
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002381 __apic_update_ppr(apic, &ppr);
2382 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002383}
2384
Qing He40487c62007-09-17 14:47:13 +08002385int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2386{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002387 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002388
Gleb Natapovc48f1492012-08-05 15:58:33 +03002389 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Miaohe Lin3ce4dc12020-01-18 10:50:37 +08002390 return 1;
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002391 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2392 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
Miaohe Lin3ce4dc12020-01-18 10:50:37 +08002393 return 1;
2394 return 0;
Qing He40487c62007-09-17 14:47:13 +08002395}
2396
Eddie Dong1b9778d2007-09-03 16:56:58 +03002397void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2398{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002399 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002400
Gleb Natapov54e98182012-08-05 15:58:32 +03002401 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08002402 kvm_apic_inject_pending_timer_irqs(apic);
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002403 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002404 }
2405}
2406
Eddie Dong97222cc2007-09-12 10:58:04 +03002407int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2408{
2409 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002410 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002411 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002412
2413 if (vector == -1)
2414 return -1;
2415
Wanpeng Li56cc2402014-08-05 12:42:24 +08002416 /*
2417 * We get here even with APIC virtualization enabled, if doing
2418 * nested virtualization and L1 runs with the "acknowledge interrupt
2419 * on exit" mode. Then we cannot inject the interrupt via RVI,
2420 * because the process would deliver it through the IDT.
2421 */
2422
Eddie Dong97222cc2007-09-12 10:58:04 +03002423 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002424 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002425 /*
2426 * For auto-EOI interrupts, there might be another pending
2427 * interrupt above PPR, so check whether to raise another
2428 * KVM_REQ_EVENT.
2429 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002430 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002431 } else {
2432 /*
2433 * For normal interrupts, PPR has been raised and there cannot
2434 * be a higher-priority pending interrupt---except if there was
2435 * a concurrent interrupt injection, but that would have
2436 * triggered KVM_REQ_EVENT already.
2437 */
2438 apic_set_isr(vector, apic);
2439 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002440 }
2441
Eddie Dong97222cc2007-09-12 10:58:04 +03002442 return vector;
2443}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002444
Radim Krčmářa92e2542016-07-12 22:09:22 +02002445static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2446 struct kvm_lapic_state *s, bool set)
2447{
2448 if (apic_x2apic_mode(vcpu->arch.apic)) {
2449 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002450 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002451
Radim Krčmář371313132016-07-12 22:09:27 +02002452 if (vcpu->kvm->arch.x2apic_format) {
2453 if (*id != vcpu->vcpu_id)
2454 return -EINVAL;
2455 } else {
2456 if (set)
2457 *id >>= 24;
2458 else
2459 *id <<= 24;
2460 }
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002461
2462 /* In x2APIC mode, the LDR is fixed and based on the id */
2463 if (set)
2464 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002465 }
2466
2467 return 0;
2468}
2469
2470int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2471{
2472 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2473 return kvm_apic_state_fixup(vcpu, s, false);
2474}
2475
2476int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002477{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002478 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002479 int r;
2480
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002481
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002482 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002483 /* set SPIV separately to get count of SW disabled APICs right */
2484 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002485
2486 r = kvm_apic_state_fixup(vcpu, s, true);
2487 if (r)
2488 return r;
Jordan Borgner0e96f312018-10-28 12:58:28 +00002489 memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002490
2491 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002492 kvm_apic_set_version(vcpu);
2493
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002494 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002495 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002496 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002497 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002498 update_divide_count(apic);
2499 start_apic_timer(apic);
Suravee Suthikulpanitb26a6952019-11-14 14:15:04 -06002500 kvm_apic_update_apicv(vcpu);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002501 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002502 if (vcpu->arch.apicv_active) {
Paolo Bonzini967235d2016-12-19 14:03:45 +01002503 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002504 kvm_x86_ops->hwapic_irr_update(vcpu,
2505 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002506 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002507 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002508 }
Avi Kivity3842d132010-07-27 12:30:24 +03002509 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002510 if (ioapic_in_kernel(vcpu->kvm))
2511 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002512
2513 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002514
2515 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002516}
Eddie Donga3d7f852007-09-03 16:15:12 +03002517
Avi Kivity2f52d582008-01-16 12:49:30 +02002518void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002519{
Eddie Donga3d7f852007-09-03 16:15:12 +03002520 struct hrtimer *timer;
2521
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08002522 if (!lapic_in_kernel(vcpu) ||
2523 kvm_can_post_timer_interrupt(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002524 return;
2525
Gleb Natapov54e98182012-08-05 15:58:32 +03002526 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002527 if (hrtimer_cancel(timer))
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02002528 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
Eddie Donga3d7f852007-09-03 16:15:12 +03002529}
Avi Kivityb93463a2007-10-25 16:52:32 +02002530
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002531/*
2532 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2533 *
2534 * Detect whether guest triggered PV EOI since the
2535 * last entry. If yes, set EOI on guests's behalf.
2536 * Clear PV EOI in guest memory in any case.
2537 */
2538static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2539 struct kvm_lapic *apic)
2540{
2541 bool pending;
2542 int vector;
2543 /*
2544 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2545 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2546 *
2547 * KVM_APIC_PV_EOI_PENDING is unset:
2548 * -> host disabled PV EOI.
2549 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2550 * -> host enabled PV EOI, guest did not execute EOI yet.
2551 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2552 * -> host enabled PV EOI, guest executed EOI.
2553 */
2554 BUG_ON(!pv_eoi_enabled(vcpu));
2555 pending = pv_eoi_get_pending(vcpu);
2556 /*
2557 * Clear pending bit in any case: it will be set again on vmentry.
2558 * While this might not be ideal from performance point of view,
2559 * this makes sure pv eoi is only enabled when we know it's safe.
2560 */
2561 pv_eoi_clr_pending(vcpu);
2562 if (pending)
2563 return;
2564 vector = apic_set_eoi(apic);
2565 trace_kvm_pv_eoi(apic, vector);
2566}
2567
Avi Kivityb93463a2007-10-25 16:52:32 +02002568void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2569{
2570 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002571
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002572 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2573 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2574
Gleb Natapov41383772012-04-19 14:06:29 +03002575 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002576 return;
2577
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002578 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2579 sizeof(u32)))
Nicholas Krause603242a2015-08-05 10:44:40 -04002580 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002581
2582 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2583}
2584
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002585/*
2586 * apic_sync_pv_eoi_to_guest - called before vmentry
2587 *
2588 * Detect whether it's safe to enable PV EOI and
2589 * if yes do so.
2590 */
2591static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2592 struct kvm_lapic *apic)
2593{
2594 if (!pv_eoi_enabled(vcpu) ||
2595 /* IRR set or many bits in ISR: could be nested. */
2596 apic->irr_pending ||
2597 /* Cache not set: could be safe but we don't bother. */
2598 apic->highest_isr_cache == -1 ||
2599 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002600 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002601 /*
2602 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2603 * so we need not do anything here.
2604 */
2605 return;
2606 }
2607
2608 pv_eoi_set_pending(apic->vcpu);
2609}
2610
Avi Kivityb93463a2007-10-25 16:52:32 +02002611void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2612{
2613 u32 data, tpr;
2614 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002615 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002616
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002617 apic_sync_pv_eoi_to_guest(vcpu, apic);
2618
Gleb Natapov41383772012-04-19 14:06:29 +03002619 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002620 return;
2621
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002622 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002623 max_irr = apic_find_highest_irr(apic);
2624 if (max_irr < 0)
2625 max_irr = 0;
2626 max_isr = apic_find_highest_isr(apic);
2627 if (max_isr < 0)
2628 max_isr = 0;
2629 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2630
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002631 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2632 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002633}
2634
Andy Honigfda4e2e2013-11-20 10:23:22 -08002635int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002636{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002637 if (vapic_addr) {
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002638 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andy Honigfda4e2e2013-11-20 10:23:22 -08002639 &vcpu->arch.apic->vapic_cache,
2640 vapic_addr, sizeof(u32)))
2641 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002642 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002643 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002644 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002645 }
2646
2647 vcpu->arch.apic->vapic_addr = vapic_addr;
2648 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002649}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002650
2651int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2652{
2653 struct kvm_lapic *apic = vcpu->arch.apic;
2654 u32 reg = (msr - APIC_BASE_MSR) << 4;
2655
Paolo Bonzini35754c92015-07-29 12:05:37 +02002656 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002657 return 1;
2658
Nadav Amitc69d3d92014-11-26 17:56:25 +02002659 if (reg == APIC_ICR2)
2660 return 1;
2661
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002662 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002663 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002664 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2665 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002666}
2667
2668int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2669{
2670 struct kvm_lapic *apic = vcpu->arch.apic;
2671 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2672
Paolo Bonzini35754c92015-07-29 12:05:37 +02002673 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002674 return 1;
2675
Yi Wang0d888002019-07-06 01:08:48 +08002676 if (reg == APIC_DFR || reg == APIC_ICR2)
Nadav Amitc69d3d92014-11-26 17:56:25 +02002677 return 1;
Nadav Amitc69d3d92014-11-26 17:56:25 +02002678
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002679 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002680 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002681 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002682 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002683
2684 *data = (((u64)high) << 32) | low;
2685
2686 return 0;
2687}
Gleb Natapov10388a02010-01-17 15:51:23 +02002688
2689int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2690{
2691 struct kvm_lapic *apic = vcpu->arch.apic;
2692
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002693 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002694 return 1;
2695
2696 /* if this is ICR write vector before command */
2697 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002698 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2699 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002700}
2701
2702int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2703{
2704 struct kvm_lapic *apic = vcpu->arch.apic;
2705 u32 low, high = 0;
2706
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002707 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002708 return 1;
2709
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002710 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002711 return 1;
2712 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002713 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002714
2715 *data = (((u64)high) << 32) | low;
2716
2717 return 0;
2718}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002719
Ladi Prosek72bbf932018-10-16 18:49:59 +02002720int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002721{
2722 u64 addr = data & ~KVM_MSR_ENABLED;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002723 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
2724 unsigned long new_len;
2725
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002726 if (!IS_ALIGNED(addr, 4))
2727 return 1;
2728
2729 vcpu->arch.pv_eoi.msr_val = data;
2730 if (!pv_eoi_enabled(vcpu))
2731 return 0;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002732
2733 if (addr == ghc->gpa && len <= ghc->len)
2734 new_len = ghc->len;
2735 else
2736 new_len = len;
2737
2738 return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002739}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002740
Jan Kiszka66450a22013-03-13 12:42:34 +01002741void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2742{
2743 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002744 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002745 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002746
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002747 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002748 return;
2749
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002750 /*
Liran Alon4b9852f2019-08-26 13:24:49 +03002751 * INITs are latched while CPU is in specific states
2752 * (SMM, VMX non-root mode, SVM with GIF=0).
2753 * Because a CPU cannot be in these states immediately
2754 * after it has processed an INIT signal (and thus in
2755 * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs
2756 * and leave the INIT pending.
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002757 */
Liran Alon27cbe7d2019-11-11 11:16:40 +02002758 if (kvm_vcpu_latch_init(vcpu)) {
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002759 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2760 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2761 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2762 return;
2763 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002764
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002765 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002766 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002767 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002768 if (kvm_vcpu_is_bsp(apic->vcpu))
2769 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2770 else
2771 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2772 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002773 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002774 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2775 /* evaluate pending_events before reading the vector */
2776 smp_rmb();
2777 sipi_vector = apic->sipi_vector;
Jan Kiszka66450a22013-03-13 12:42:34 +01002778 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2779 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2780 }
2781}
2782
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002783void kvm_lapic_init(void)
2784{
2785 /* do not patch jump label more than once per second */
2786 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002787 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002788}
David Matlackcef84c32016-12-16 14:30:36 -08002789
2790void kvm_lapic_exit(void)
2791{
2792 static_key_deferred_flush(&apic_hw_disabled);
2793 static_key_deferred_flush(&apic_sw_disabled);
2794}