blob: cacfe14717d665348fd0a3ae9365f2f764cc0b49 [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 */
59#define APIC_SHORT_MASK 0xc0000
60#define APIC_DEST_NOSHORT 0x0
61#define APIC_DEST_MASK 0x800
62#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090063#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030064
Nadav Amit394457a2014-10-03 00:30:52 +030065#define APIC_BROADCAST 0xFF
66#define X2APIC_BROADCAST 0xFFFFFFFFul
67
Wanpeng Lid0f5a862019-09-17 16:16:26 +080068static bool lapic_timer_advance_dynamic __read_mostly;
Wanpeng Lia0f00372019-09-26 08:54:03 +080069#define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
70#define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
71#define LAPIC_TIMER_ADVANCE_NS_INIT 1000
72#define LAPIC_TIMER_ADVANCE_NS_MAX 5000
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080073/* step-by-step approximation to mitigate fluctuation */
74#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
75
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030076static inline int apic_test_vector(int vec, void *bitmap)
77{
78 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
79}
80
Yang Zhang10606912013-04-11 19:21:38 +080081bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
82{
83 struct kvm_lapic *apic = vcpu->arch.apic;
84
85 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
86 apic_test_vector(vector, apic->regs + APIC_IRR);
87}
88
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030089static inline int __apic_test_and_set_vector(int vec, void *bitmap)
90{
91 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
92}
93
94static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
95{
96 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
97}
98
Gleb Natapovc5cc4212012-08-05 15:58:30 +030099struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300100struct static_key_deferred apic_sw_disabled __read_mostly;
101
Eddie Dong97222cc2007-09-12 10:58:04 +0300102static inline int apic_enabled(struct kvm_lapic *apic)
103{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300104 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300105}
106
Eddie Dong97222cc2007-09-12 10:58:04 +0300107#define LVT_MASK \
108 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
109
110#define LINT_MASK \
111 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
112 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
113
Radim Krčmář6e500432016-12-15 18:06:46 +0100114static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
115{
116 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
117}
118
119static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
120{
121 return apic->vcpu->vcpu_id;
122}
123
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800124bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
125{
126 return pi_inject_timer && kvm_vcpu_apicv_active(vcpu);
127}
128EXPORT_SYMBOL_GPL(kvm_can_post_timer_interrupt);
129
130static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
131{
132 return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
133}
134
Radim Krčmáře45115b2016-07-12 22:09:19 +0200135static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
136 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
137 switch (map->mode) {
138 case KVM_APIC_MODE_X2APIC: {
139 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200140 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100141
Radim Krčmáře45115b2016-07-12 22:09:19 +0200142 if (offset <= max_apic_id) {
143 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100144
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200145 offset = array_index_nospec(offset, map->max_apic_id + 1);
Radim Krčmáře45115b2016-07-12 22:09:19 +0200146 *cluster = &map->phys_map[offset];
147 *mask = dest_id & (0xffff >> (16 - cluster_size));
148 } else {
149 *mask = 0;
150 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100151
Radim Krčmáře45115b2016-07-12 22:09:19 +0200152 return true;
153 }
154 case KVM_APIC_MODE_XAPIC_FLAT:
155 *cluster = map->xapic_flat_map;
156 *mask = dest_id & 0xff;
157 return true;
158 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100159 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200160 *mask = dest_id & 0xf;
161 return true;
162 default:
163 /* Not optimized. */
164 return false;
165 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300166}
167
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200168static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100169{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200170 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100171
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200172 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100173}
174
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300175static void recalculate_apic_map(struct kvm *kvm)
176{
177 struct kvm_apic_map *new, *old = NULL;
178 struct kvm_vcpu *vcpu;
179 int i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100180 u32 max_id = 255; /* enough space for any xAPIC ID */
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300181
182 mutex_lock(&kvm->arch.apic_map_lock);
183
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200184 kvm_for_each_vcpu(i, vcpu, kvm)
185 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100186 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200187
Michal Hockoa7c3e902017-05-08 15:57:09 -0700188 new = kvzalloc(sizeof(struct kvm_apic_map) +
Ben Gardon254272c2019-02-11 11:02:50 -0800189 sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
190 GFP_KERNEL_ACCOUNT);
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200191
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300192 if (!new)
193 goto out;
194
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200195 new->max_apic_id = max_id;
196
Nadav Amit173beed2014-11-02 11:54:54 +0200197 kvm_for_each_vcpu(i, vcpu, kvm) {
198 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200199 struct kvm_lapic **cluster;
200 u16 mask;
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100201 u32 ldr;
202 u8 xapic_id;
203 u32 x2apic_id;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300204
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100205 if (!kvm_apic_present(vcpu))
206 continue;
207
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100208 xapic_id = kvm_xapic_id(apic);
209 x2apic_id = kvm_x2apic_id(apic);
210
211 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
212 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
213 x2apic_id <= new->max_apic_id)
214 new->phys_map[x2apic_id] = apic;
215 /*
216 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
217 * prevent them from masking VCPUs with APIC ID <= 0xff.
218 */
219 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
220 new->phys_map[xapic_id] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100221
Radim Krcmarb14c8762019-08-13 23:37:37 -0400222 if (!kvm_apic_sw_enabled(apic))
223 continue;
224
Radim Krčmář6e500432016-12-15 18:06:46 +0100225 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
226
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100227 if (apic_x2apic_mode(apic)) {
228 new->mode |= KVM_APIC_MODE_X2APIC;
229 } else if (ldr) {
230 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500231 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100232 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
233 else
234 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
235 }
236
Radim Krčmáře45115b2016-07-12 22:09:19 +0200237 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100238 continue;
239
Radim Krčmáře45115b2016-07-12 22:09:19 +0200240 if (mask)
241 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300242 }
243out:
244 old = rcu_dereference_protected(kvm->arch.apic_map,
245 lockdep_is_held(&kvm->arch.apic_map_lock));
246 rcu_assign_pointer(kvm->arch.apic_map, new);
247 mutex_unlock(&kvm->arch.apic_map_lock);
248
249 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200250 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800251
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700252 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300253}
254
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300255static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
256{
Radim Krčmáře4627552014-10-30 15:06:45 +0100257 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300258
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500259 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100260
261 if (enabled != apic->sw_enabled) {
262 apic->sw_enabled = enabled;
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800263 if (enabled)
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300264 static_key_slow_dec_deferred(&apic_sw_disabled);
Peng Haoeb1ff0a2018-12-04 17:42:50 +0800265 else
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300266 static_key_slow_inc(&apic_sw_disabled.key);
Radim Krcmarb14c8762019-08-13 23:37:37 -0400267
268 recalculate_apic_map(apic->vcpu->kvm);
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300269 }
270}
271
Radim Krčmářa92e2542016-07-12 22:09:22 +0200272static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300273{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500274 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300275 recalculate_apic_map(apic->vcpu->kvm);
276}
277
278static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
279{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500280 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300281 recalculate_apic_map(apic->vcpu->kvm);
282}
283
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000284static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
285{
286 return ((id >> 4) << 16) | (1 << (id & 0xf));
287}
288
Radim Krčmářa92e2542016-07-12 22:09:22 +0200289static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200290{
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000291 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200292
Radim Krčmář6e500432016-12-15 18:06:46 +0100293 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
294
Radim Krčmářa92e2542016-07-12 22:09:22 +0200295 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500296 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200297 recalculate_apic_map(apic->vcpu->kvm);
298}
299
Eddie Dong97222cc2007-09-12 10:58:04 +0300300static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
301{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500302 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300303}
304
305static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
306{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500307 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300308}
309
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800310static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
311{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100312 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800313}
314
Eddie Dong97222cc2007-09-12 10:58:04 +0300315static inline int apic_lvtt_period(struct kvm_lapic *apic)
316{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100317 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800318}
319
320static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
321{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100322 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300323}
324
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200325static inline int apic_lvt_nmi_mode(u32 lvt_val)
326{
327 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
328}
329
Gleb Natapovfc61b802009-07-05 17:39:35 +0300330void kvm_apic_set_version(struct kvm_vcpu *vcpu)
331{
332 struct kvm_lapic *apic = vcpu->arch.apic;
333 struct kvm_cpuid_entry2 *feat;
334 u32 v = APIC_VERSION;
335
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100336 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300337 return;
338
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100339 /*
340 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
341 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
342 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
343 * version first and level-triggered interrupts never get EOIed in
344 * IOAPIC.
345 */
Gleb Natapovfc61b802009-07-05 17:39:35 +0300346 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100347 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
348 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300349 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500350 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300351}
352
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500353static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800354 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300355 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
356 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
357 LINT_MASK, LINT_MASK, /* LVT0-1 */
358 LVT_MASK /* LVTERR */
359};
360
361static int find_highest_vector(void *bitmap)
362{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900363 int vec;
364 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300365
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900366 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
367 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
368 reg = bitmap + REG_POS(vec);
369 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100370 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900371 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300372
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900373 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300374}
375
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300376static u8 count_vectors(void *bitmap)
377{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900378 int vec;
379 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300380 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900381
382 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
383 reg = bitmap + REG_POS(vec);
384 count += hweight32(*reg);
385 }
386
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300387 return count;
388}
389
Liran Alone7387b02017-12-24 18:12:54 +0200390bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Yang Zhanga20ed542013-04-11 19:25:15 +0800391{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100392 u32 i, vec;
Liran Alone7387b02017-12-24 18:12:54 +0200393 u32 pir_val, irr_val, prev_irr_val;
394 int max_updated_irr;
395
396 max_updated_irr = -1;
397 *max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800398
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100399 for (i = vec = 0; i <= 7; i++, vec += 32) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200400 pir_val = READ_ONCE(pir[i]);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100401 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
Paolo Bonziniad361092016-09-20 16:15:05 +0200402 if (pir_val) {
Liran Alone7387b02017-12-24 18:12:54 +0200403 prev_irr_val = irr_val;
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100404 irr_val |= xchg(&pir[i], 0);
405 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
Liran Alone7387b02017-12-24 18:12:54 +0200406 if (prev_irr_val != irr_val) {
407 max_updated_irr =
408 __fls(irr_val ^ prev_irr_val) + vec;
409 }
Paolo Bonziniad361092016-09-20 16:15:05 +0200410 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100411 if (irr_val)
Liran Alone7387b02017-12-24 18:12:54 +0200412 *max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800413 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100414
Liran Alone7387b02017-12-24 18:12:54 +0200415 return ((max_updated_irr != -1) &&
416 (max_updated_irr == *max_irr));
Yang Zhanga20ed542013-04-11 19:25:15 +0800417}
Wincy Van705699a2015-02-03 23:58:17 +0800418EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
419
Liran Alone7387b02017-12-24 18:12:54 +0200420bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
Wincy Van705699a2015-02-03 23:58:17 +0800421{
422 struct kvm_lapic *apic = vcpu->arch.apic;
423
Liran Alone7387b02017-12-24 18:12:54 +0200424 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
Wincy Van705699a2015-02-03 23:58:17 +0800425}
Yang Zhanga20ed542013-04-11 19:25:15 +0800426EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
427
Gleb Natapov33e4c682009-06-11 11:06:51 +0300428static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300429{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300430 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300431}
432
433static inline int apic_find_highest_irr(struct kvm_lapic *apic)
434{
435 int result;
436
Yang Zhangc7c9c562013-01-25 10:18:51 +0800437 /*
438 * Note that irr_pending is just a hint. It will be always
439 * true with virtual interrupt delivery enabled.
440 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300441 if (!apic->irr_pending)
442 return -1;
443
444 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300445 ASSERT(result == -1 || result >= 16);
446
447 return result;
448}
449
Gleb Natapov33e4c682009-06-11 11:06:51 +0300450static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
451{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800452 struct kvm_vcpu *vcpu;
453
454 vcpu = apic->vcpu;
455
Andrey Smetanind62caab2015-11-10 15:36:33 +0300456 if (unlikely(vcpu->arch.apicv_active)) {
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100457 /* need to update RVI */
Wei Yangee171d22019-03-31 19:17:22 -0700458 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100459 kvm_x86_ops->hwapic_irr_update(vcpu,
460 apic_find_highest_irr(apic));
Nadav Amitf210f752014-11-16 23:49:07 +0200461 } else {
462 apic->irr_pending = false;
Wei Yangee171d22019-03-31 19:17:22 -0700463 kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
Nadav Amitf210f752014-11-16 23:49:07 +0200464 if (apic_search_irr(apic) != -1)
465 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800466 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300467}
468
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300469static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
470{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800471 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200472
Wanpeng Li56cc2402014-08-05 12:42:24 +0800473 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
474 return;
475
476 vcpu = apic->vcpu;
477
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300478 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800479 * With APIC virtualization enabled, all caching is disabled
480 * because the processor can modify ISR under the hood. Instead
481 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300482 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300483 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200484 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800485 else {
486 ++apic->isr_count;
487 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
488 /*
489 * ISR (in service register) bit is set when injecting an interrupt.
490 * The highest vector is injected. Thus the latest bit set matches
491 * the highest bit in ISR.
492 */
493 apic->highest_isr_cache = vec;
494 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300495}
496
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200497static inline int apic_find_highest_isr(struct kvm_lapic *apic)
498{
499 int result;
500
501 /*
502 * Note that isr_count is always 1, and highest_isr_cache
503 * is always -1, with APIC virtualization enabled.
504 */
505 if (!apic->isr_count)
506 return -1;
507 if (likely(apic->highest_isr_cache != -1))
508 return apic->highest_isr_cache;
509
510 result = find_highest_vector(apic->regs + APIC_ISR);
511 ASSERT(result == -1 || result >= 16);
512
513 return result;
514}
515
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300516static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
517{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200518 struct kvm_vcpu *vcpu;
519 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
520 return;
521
522 vcpu = apic->vcpu;
523
524 /*
525 * We do get here for APIC virtualization enabled if the guest
526 * uses the Hyper-V APIC enlightenment. In this case we may need
527 * to trigger a new interrupt delivery by writing the SVI field;
528 * on the other hand isr_count and highest_isr_cache are unused
529 * and must be left alone.
530 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300531 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200532 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200533 apic_find_highest_isr(apic));
534 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300535 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200536 BUG_ON(apic->isr_count < 0);
537 apic->highest_isr_cache = -1;
538 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300539}
540
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800541int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
542{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300543 /* This may race with setting of irr in __apic_accept_irq() and
544 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
545 * will cause vmexit immediately and the value will be recalculated
546 * on the next vmentry.
547 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100548 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800549}
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100550EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800551
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200552static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800553 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100554 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200555
Yang Zhangb4f22252013-04-11 19:21:37 +0800556int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100557 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300558{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800559 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800560
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200561 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800562 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300563}
564
Wanpeng Li4180bf12018-07-23 14:39:54 +0800565int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800566 unsigned long ipi_bitmap_high, u32 min,
Wanpeng Li4180bf12018-07-23 14:39:54 +0800567 unsigned long icr, int op_64_bit)
568{
569 int i;
570 struct kvm_apic_map *map;
571 struct kvm_vcpu *vcpu;
572 struct kvm_lapic_irq irq = {0};
573 int cluster_size = op_64_bit ? 64 : 32;
574 int count = 0;
575
576 irq.vector = icr & APIC_VECTOR_MASK;
577 irq.delivery_mode = icr & APIC_MODE_MASK;
578 irq.level = (icr & APIC_INT_ASSERT) != 0;
579 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
580
581 if (icr & APIC_DEST_MASK)
582 return -KVM_EINVAL;
583 if (icr & APIC_SHORT_MASK)
584 return -KVM_EINVAL;
585
586 rcu_read_lock();
587 map = rcu_dereference(kvm->arch.apic_map);
588
Wanpeng Li38ab0122018-11-20 09:39:30 +0800589 if (unlikely(!map)) {
590 count = -EOPNOTSUPP;
591 goto out;
592 }
593
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800594 if (min > map->max_apic_id)
595 goto out;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800596 /* Bits above cluster_size are masked in the caller. */
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800597 for_each_set_bit(i, &ipi_bitmap_low,
598 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
599 if (map->phys_map[min + i]) {
600 vcpu = map->phys_map[min + i]->vcpu;
601 count += kvm_apic_set_irq(vcpu, &irq, NULL);
602 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800603 }
604
605 min += cluster_size;
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800606
607 if (min > map->max_apic_id)
608 goto out;
609
610 for_each_set_bit(i, &ipi_bitmap_high,
611 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
612 if (map->phys_map[min + i]) {
613 vcpu = map->phys_map[min + i]->vcpu;
614 count += kvm_apic_set_irq(vcpu, &irq, NULL);
615 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800616 }
617
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800618out:
Wanpeng Li4180bf12018-07-23 14:39:54 +0800619 rcu_read_unlock();
620 return count;
621}
622
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300623static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
624{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200625
626 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
627 sizeof(val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300628}
629
630static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
631{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200632
633 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
634 sizeof(*val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300635}
636
637static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
638{
639 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
640}
641
642static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
643{
644 u8 val;
645 if (pv_eoi_get_user(vcpu, &val) < 0)
Yi Wang0d888002019-07-06 01:08:48 +0800646 printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800647 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300648 return val & 0x1;
649}
650
651static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
652{
653 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
Yi Wang0d888002019-07-06 01:08:48 +0800654 printk(KERN_WARNING "Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800655 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300656 return;
657 }
658 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
659}
660
661static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
662{
663 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
Yi Wang0d888002019-07-06 01:08:48 +0800664 printk(KERN_WARNING "Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800665 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300666 return;
667 }
668 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
669}
670
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100671static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
672{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100673 int highest_irr;
Liran Alonfa59cc02017-12-24 18:12:53 +0200674 if (apic->vcpu->arch.apicv_active)
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100675 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
676 else
677 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100678 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
679 return -1;
680 return highest_irr;
681}
682
683static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300684{
Avi Kivity3842d132010-07-27 12:30:24 +0300685 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300686 int isr;
687
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500688 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
689 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300690 isr = apic_find_highest_isr(apic);
691 isrv = (isr != -1) ? isr : 0;
692
693 if ((tpr & 0xf0) >= (isrv & 0xf0))
694 ppr = tpr & 0xff;
695 else
696 ppr = isrv & 0xf0;
697
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100698 *new_ppr = ppr;
699 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500700 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100701
702 return ppr < old_ppr;
703}
704
705static void apic_update_ppr(struct kvm_lapic *apic)
706{
707 u32 ppr;
708
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100709 if (__apic_update_ppr(apic, &ppr) &&
710 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100711 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300712}
713
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100714void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
715{
716 apic_update_ppr(vcpu->arch.apic);
717}
718EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
719
Eddie Dong97222cc2007-09-12 10:58:04 +0300720static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
721{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500722 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300723 apic_update_ppr(apic);
724}
725
Radim Krčmář03d22492015-02-12 19:41:31 +0100726static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300727{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100728 return mda == (apic_x2apic_mode(apic) ?
729 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300730}
731
Radim Krčmář03d22492015-02-12 19:41:31 +0100732static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300733{
Radim Krčmář03d22492015-02-12 19:41:31 +0100734 if (kvm_apic_broadcast(apic, mda))
735 return true;
736
737 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100738 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100739
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100740 /*
741 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
742 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
743 * this allows unique addressing of VCPUs with APIC ID over 0xff.
744 * The 0xff condition is needed because writeable xAPIC ID.
745 */
746 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
747 return true;
748
Radim Krčmářb4535b52016-12-15 18:06:47 +0100749 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300750}
751
Radim Krčmář52c233a2015-01-29 22:48:48 +0100752static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300753{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300754 u32 logical_id;
755
Nadav Amit394457a2014-10-03 00:30:52 +0300756 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100757 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300758
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500759 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300760
Radim Krčmář9368b562015-01-29 22:48:49 +0100761 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100762 return ((logical_id >> 16) == (mda >> 16))
763 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100764
765 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300766
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500767 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300768 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100769 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300770 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100771 return ((logical_id >> 4) == (mda >> 4))
772 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300773 default:
Radim Krčmář9368b562015-01-29 22:48:49 +0100774 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300775 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300776}
777
Radim Krčmářc5192652016-07-12 22:09:28 +0200778/* The KVM local APIC implementation has two quirks:
779 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100780 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
781 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
782 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200783 *
784 * - in-kernel IOAPIC messages have to be delivered directly to
785 * x2APIC, because the kernel does not support interrupt remapping.
786 * In order to support broadcast without interrupt remapping, x2APIC
787 * rewrites the destination of non-IPI messages from APIC_BROADCAST
788 * to X2APIC_BROADCAST.
789 *
790 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
791 * important when userspace wants to use x2APIC-format MSIs, because
792 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100793 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200794static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
795 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100796{
797 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100798
Radim Krčmářc5192652016-07-12 22:09:28 +0200799 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100800 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100801 return X2APIC_BROADCAST;
802
Radim Krčmářb4535b52016-12-15 18:06:47 +0100803 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100804}
805
Radim Krčmář52c233a2015-01-29 22:48:48 +0100806bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300807 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300808{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800809 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200810 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300811
Zachary Amsdenbd371392010-06-14 11:42:15 -1000812 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300813 switch (short_hand) {
814 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100815 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100816 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200817 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100818 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300819 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100820 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300821 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100822 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300823 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100824 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300825 default:
Radim Krčmář9368b562015-01-29 22:48:49 +0100826 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300827 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300828}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500829EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300830
Feng Wu520040142016-01-25 16:53:33 +0800831int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
832 const unsigned long *bitmap, u32 bitmap_size)
833{
834 u32 mod;
835 int i, idx = -1;
836
837 mod = vector % dest_vcpus;
838
839 for (i = 0; i <= mod; i++) {
840 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
841 BUG_ON(idx == bitmap_size);
842 }
843
844 return idx;
845}
846
Radim Krčmář4efd8052016-02-12 15:00:15 +0100847static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
848{
849 if (!kvm->arch.disabled_lapic_found) {
850 kvm->arch.disabled_lapic_found = true;
851 printk(KERN_INFO
852 "Disabled LAPIC found during irq injection\n");
853 }
854}
855
Radim Krčmářc5192652016-07-12 22:09:28 +0200856static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
857 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
858{
859 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
860 if ((irq->dest_id == APIC_BROADCAST &&
861 map->mode != KVM_APIC_MODE_X2APIC))
862 return true;
863 if (irq->dest_id == X2APIC_BROADCAST)
864 return true;
865 } else {
866 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
867 if (irq->dest_id == (x2apic_ipi ?
868 X2APIC_BROADCAST : APIC_BROADCAST))
869 return true;
870 }
871
872 return false;
873}
874
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200875/* Return true if the interrupt can be handled by using *bitmap as index mask
876 * for valid destinations in *dst array.
877 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
878 * Note: we may have zero kvm_lapic destinations when we return true, which
879 * means that the interrupt should be dropped. In this case, *bitmap would be
880 * zero and *dst undefined.
881 */
882static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
883 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
884 struct kvm_apic_map *map, struct kvm_lapic ***dst,
885 unsigned long *bitmap)
886{
887 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200888
889 if (irq->shorthand == APIC_DEST_SELF && src) {
890 *dst = src;
891 *bitmap = 1;
892 return true;
893 } else if (irq->shorthand)
894 return false;
895
Radim Krčmářc5192652016-07-12 22:09:28 +0200896 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200897 return false;
898
899 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200900 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200901 *bitmap = 0;
902 } else {
Paolo Bonzini1d487e92019-04-11 11:16:47 +0200903 u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
904 *dst = &map->phys_map[dest_id];
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200905 *bitmap = 1;
906 }
907 return true;
908 }
909
Radim Krčmáře45115b2016-07-12 22:09:19 +0200910 *bitmap = 0;
911 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
912 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200913 return false;
914
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200915 if (!kvm_lowest_prio_delivery(irq))
916 return true;
917
918 if (!kvm_vector_hashing_enabled()) {
919 lowest = -1;
920 for_each_set_bit(i, bitmap, 16) {
921 if (!(*dst)[i])
922 continue;
923 if (lowest < 0)
924 lowest = i;
925 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
926 (*dst)[lowest]->vcpu) < 0)
927 lowest = i;
928 }
929 } else {
930 if (!*bitmap)
931 return true;
932
933 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
934 bitmap, 16);
935
936 if (!(*dst)[lowest]) {
937 kvm_apic_disabled_lapic_found(kvm);
938 *bitmap = 0;
939 return true;
940 }
941 }
942
943 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
944
945 return true;
946}
947
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300948bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100949 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300950{
951 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200952 unsigned long bitmap;
953 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300954 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200955 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300956
957 *r = -1;
958
959 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800960 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300961 return true;
962 }
963
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300964 rcu_read_lock();
965 map = rcu_dereference(kvm->arch.apic_map);
966
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200967 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200968 if (ret) {
969 *r = 0;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200970 for_each_set_bit(i, &bitmap, 16) {
971 if (!dst[i])
972 continue;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200973 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100974 }
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200975 }
Radim Krčmář3548a252015-02-12 19:41:33 +0100976
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300977 rcu_read_unlock();
978 return ret;
979}
980
Feng Wu6228a0d2016-01-25 16:53:34 +0800981/*
982 * This routine tries to handler interrupts in posted mode, here is how
983 * it deals with different cases:
984 * - For single-destination interrupts, handle it in posted mode
985 * - Else if vector hashing is enabled and it is a lowest-priority
986 * interrupt, handle it in posted mode and use the following mechanism
987 * to find the destinaiton vCPU.
988 * 1. For lowest-priority interrupts, store all the possible
989 * destination vCPUs in an array.
990 * 2. Use "guest vector % max number of destination vCPUs" to find
991 * the right destination vCPU in the array for the lowest-priority
992 * interrupt.
993 * - Otherwise, use remapped mode to inject the interrupt.
994 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800995bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
996 struct kvm_vcpu **dest_vcpu)
997{
998 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200999 unsigned long bitmap;
1000 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +08001001 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +08001002
1003 if (irq->shorthand)
1004 return false;
1005
1006 rcu_read_lock();
1007 map = rcu_dereference(kvm->arch.apic_map);
1008
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001009 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1010 hweight16(bitmap) == 1) {
1011 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +08001012
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001013 if (dst[i]) {
1014 *dest_vcpu = dst[i]->vcpu;
1015 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +08001016 }
Feng Wu8feb4a02015-09-18 22:29:47 +08001017 }
1018
Feng Wu8feb4a02015-09-18 22:29:47 +08001019 rcu_read_unlock();
1020 return ret;
1021}
1022
Eddie Dong97222cc2007-09-12 10:58:04 +03001023/*
1024 * Add a pending IRQ into lapic.
1025 * Return 1 if successfully added and 0 if discarded.
1026 */
1027static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +08001028 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001029 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +03001030{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001031 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +03001032 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +03001033
Paolo Bonzinia183b632014-09-11 11:51:02 +02001034 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1035 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001036 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001037 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +02001038 vcpu->arch.apic_arb_prio++;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001039 /* fall through */
Gleb Natapove1035712009-03-05 16:34:59 +02001040 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001041 if (unlikely(trig_mode && !level))
1042 break;
1043
Eddie Dong97222cc2007-09-12 10:58:04 +03001044 /* FIXME add logic for vcpu on reset */
1045 if (unlikely(!apic_enabled(apic)))
1046 break;
1047
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001048 result = 1;
1049
Joerg Roedel9daa5002016-02-29 16:04:44 +01001050 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001051 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +01001052 dest_map->vectors[vcpu->vcpu_id] = vector;
1053 }
Avi Kivitya5d36f82009-12-29 12:42:16 +02001054
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001055 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1056 if (trig_mode)
Wei Yangee171d22019-03-31 19:17:22 -07001057 kvm_lapic_set_vector(vector,
1058 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001059 else
Wei Yangee171d22019-03-31 19:17:22 -07001060 kvm_lapic_clear_vector(vector,
1061 apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001062 }
1063
Andrey Smetanind62caab2015-11-10 15:36:33 +03001064 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +08001065 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001066 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001067 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +08001068
1069 kvm_make_request(KVM_REQ_EVENT, vcpu);
1070 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001071 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001072 break;
1073
1074 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +05301075 result = 1;
1076 vcpu->arch.pv.pv_unhalted = 1;
1077 kvm_make_request(KVM_REQ_EVENT, vcpu);
1078 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001079 break;
1080
1081 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +02001082 result = 1;
1083 kvm_make_request(KVM_REQ_SMI, vcpu);
1084 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001085 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001086
Eddie Dong97222cc2007-09-12 10:58:04 +03001087 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001088 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001089 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001090 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001091 break;
1092
1093 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001094 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001095 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001096 /* assumes that there are only KVM_APIC_INIT/SIPI */
1097 apic->pending_events = (1UL << KVM_APIC_INIT);
1098 /* make sure pending_events is visible before sending
1099 * the request */
1100 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +03001101 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001102 kvm_vcpu_kick(vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001103 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001104 break;
1105
1106 case APIC_DM_STARTUP:
Jan Kiszka66450a22013-03-13 12:42:34 +01001107 result = 1;
1108 apic->sipi_vector = vector;
1109 /* make sure sipi_vector is visible for the receiver */
1110 smp_wmb();
1111 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1112 kvm_make_request(KVM_REQ_EVENT, vcpu);
1113 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001114 break;
1115
Jan Kiszka23930f92008-09-26 09:30:52 +02001116 case APIC_DM_EXTINT:
1117 /*
1118 * Should only be called by kvm_apic_local_deliver() with LVT0,
1119 * before NMI watchdog was enabled. Already handled by
1120 * kvm_apic_accept_pic_intr().
1121 */
1122 break;
1123
Eddie Dong97222cc2007-09-12 10:58:04 +03001124 default:
1125 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1126 delivery_mode);
1127 break;
1128 }
1129 return result;
1130}
1131
Gleb Natapove1035712009-03-05 16:34:59 +02001132int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001133{
Gleb Natapove1035712009-03-05 16:34:59 +02001134 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001135}
1136
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001137static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1138{
Andrey Smetanin63086302015-11-10 15:36:32 +03001139 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001140}
1141
Yang Zhangc7c9c562013-01-25 10:18:51 +08001142static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1143{
Steve Rutherford7543a632015-07-29 23:21:41 -07001144 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001145
Steve Rutherford7543a632015-07-29 23:21:41 -07001146 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1147 if (!kvm_ioapic_handles_vector(apic, vector))
1148 return;
1149
1150 /* Request a KVM exit to inform the userspace IOAPIC. */
1151 if (irqchip_split(apic->vcpu->kvm)) {
1152 apic->vcpu->arch.pending_ioapic_eoi = vector;
1153 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1154 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001155 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001156
1157 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1158 trigger_mode = IOAPIC_LEVEL_TRIG;
1159 else
1160 trigger_mode = IOAPIC_EDGE_TRIG;
1161
1162 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001163}
1164
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001165static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001166{
1167 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001168
1169 trace_kvm_eoi(apic, vector);
1170
Eddie Dong97222cc2007-09-12 10:58:04 +03001171 /*
1172 * Not every write EOI will has corresponding ISR,
1173 * one example is when Kernel check timer on setup_IO_APIC
1174 */
1175 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001176 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001177
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001178 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001179 apic_update_ppr(apic);
1180
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001181 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1182 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1183
Yang Zhangc7c9c562013-01-25 10:18:51 +08001184 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001185 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001186 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001187}
1188
Yang Zhangc7c9c562013-01-25 10:18:51 +08001189/*
1190 * this interface assumes a trap-like exit, which has already finished
1191 * desired side effect including vISR and vPPR update.
1192 */
1193void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1194{
1195 struct kvm_lapic *apic = vcpu->arch.apic;
1196
1197 trace_kvm_eoi(apic, vector);
1198
1199 kvm_ioapic_send_eoi(apic, vector);
1200 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1201}
1202EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1203
Wanpeng Li2b0911d2019-09-05 14:26:27 +08001204static void apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
Eddie Dong97222cc2007-09-12 10:58:04 +03001205{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001206 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001207
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001208 irq.vector = icr_low & APIC_VECTOR_MASK;
1209 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1210 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001211 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001212 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1213 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001214 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001215 if (apic_x2apic_mode(apic))
1216 irq.dest_id = icr_high;
1217 else
1218 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001219
Gleb Natapov1000ff82009-07-07 16:00:57 +03001220 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1221
Yang Zhangb4f22252013-04-11 19:21:37 +08001222 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001223}
1224
1225static u32 apic_get_tmcct(struct kvm_lapic *apic)
1226{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001227 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001228 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001229 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001230
1231 ASSERT(apic != NULL);
1232
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001233 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001234 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001235 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001236 return 0;
1237
Paolo Bonzini55878592016-10-25 15:23:49 +02001238 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001239 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001240 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001241 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001242
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001243 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1244 tmcct = div64_u64(ns,
1245 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001246
1247 return tmcct;
1248}
1249
Avi Kivityb209749f2007-10-22 16:50:39 +02001250static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1251{
1252 struct kvm_vcpu *vcpu = apic->vcpu;
1253 struct kvm_run *run = vcpu->run;
1254
Avi Kivitya8eeb042010-05-10 12:34:53 +03001255 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001256 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001257 run->tpr_access.is_write = write;
1258}
1259
1260static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1261{
1262 if (apic->vcpu->arch.tpr_access_reporting)
1263 __report_tpr_access(apic, write);
1264}
1265
Eddie Dong97222cc2007-09-12 10:58:04 +03001266static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1267{
1268 u32 val = 0;
1269
1270 if (offset >= LAPIC_MMIO_LENGTH)
1271 return 0;
1272
1273 switch (offset) {
1274 case APIC_ARBPRI:
Eddie Dong97222cc2007-09-12 10:58:04 +03001275 break;
1276
1277 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001278 if (apic_lvtt_tscdeadline(apic))
1279 return 0;
1280
Eddie Dong97222cc2007-09-12 10:58:04 +03001281 val = apic_get_tmcct(apic);
1282 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001283 case APIC_PROCPRI:
1284 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001285 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001286 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001287 case APIC_TASKPRI:
1288 report_tpr_access(apic, false);
1289 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001290 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001291 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001292 break;
1293 }
1294
1295 return val;
1296}
1297
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001298static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1299{
1300 return container_of(dev, struct kvm_lapic, dev);
1301}
1302
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001303#define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
1304#define APIC_REGS_MASK(first, count) \
1305 (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
1306
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001307int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001308 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001309{
Eddie Dong97222cc2007-09-12 10:58:04 +03001310 unsigned char alignment = offset & 0xf;
1311 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001312 /* this bitmask has a bit cleared for each reserved register */
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001313 u64 valid_reg_mask =
1314 APIC_REG_MASK(APIC_ID) |
1315 APIC_REG_MASK(APIC_LVR) |
1316 APIC_REG_MASK(APIC_TASKPRI) |
1317 APIC_REG_MASK(APIC_PROCPRI) |
1318 APIC_REG_MASK(APIC_LDR) |
1319 APIC_REG_MASK(APIC_DFR) |
1320 APIC_REG_MASK(APIC_SPIV) |
1321 APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1322 APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1323 APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1324 APIC_REG_MASK(APIC_ESR) |
1325 APIC_REG_MASK(APIC_ICR) |
1326 APIC_REG_MASK(APIC_ICR2) |
1327 APIC_REG_MASK(APIC_LVTT) |
1328 APIC_REG_MASK(APIC_LVTTHMR) |
1329 APIC_REG_MASK(APIC_LVTPC) |
1330 APIC_REG_MASK(APIC_LVT0) |
1331 APIC_REG_MASK(APIC_LVT1) |
1332 APIC_REG_MASK(APIC_LVTERR) |
1333 APIC_REG_MASK(APIC_TMICT) |
1334 APIC_REG_MASK(APIC_TMCCT) |
1335 APIC_REG_MASK(APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001336
Paolo Bonzini01402cf2019-07-05 14:57:58 +02001337 /* ARBPRI is not valid on x2APIC */
1338 if (!apic_x2apic_mode(apic))
1339 valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001340
Yi Wang0d888002019-07-06 01:08:48 +08001341 if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001342 return 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001343
Eddie Dong97222cc2007-09-12 10:58:04 +03001344 result = __apic_read(apic, offset & ~0xf);
1345
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001346 trace_kvm_apic_read(offset, result);
1347
Eddie Dong97222cc2007-09-12 10:58:04 +03001348 switch (len) {
1349 case 1:
1350 case 2:
1351 case 4:
1352 memcpy(data, (char *)&result + alignment, len);
1353 break;
1354 default:
1355 printk(KERN_ERR "Local APIC read with len = %x, "
1356 "should be 1,2, or 4 instead\n", len);
1357 break;
1358 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001359 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001360}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001361EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001362
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001363static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1364{
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001365 return addr >= apic->base_address &&
1366 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001367}
1368
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001369static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001370 gpa_t address, int len, void *data)
1371{
1372 struct kvm_lapic *apic = to_lapic(this);
1373 u32 offset = address - apic->base_address;
1374
1375 if (!apic_mmio_in_range(apic, address))
1376 return -EOPNOTSUPP;
1377
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001378 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1379 if (!kvm_check_has_quirk(vcpu->kvm,
1380 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1381 return -EOPNOTSUPP;
1382
1383 memset(data, 0xff, len);
1384 return 0;
1385 }
1386
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001387 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001388
1389 return 0;
1390}
1391
Eddie Dong97222cc2007-09-12 10:58:04 +03001392static void update_divide_count(struct kvm_lapic *apic)
1393{
1394 u32 tmp1, tmp2, tdcr;
1395
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001396 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001397 tmp1 = tdcr & 0xf;
1398 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001399 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001400}
1401
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001402static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1403{
1404 /*
1405 * Do not allow the guest to program periodic timers with small
1406 * interval, since the hrtimers are not throttled by the host
1407 * scheduler.
1408 */
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001409 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001410 s64 min_period = min_timer_period_us * 1000LL;
1411
1412 if (apic->lapic_timer.period < min_period) {
1413 pr_info_ratelimited(
1414 "kvm: vcpu %i: requested %lld ns "
1415 "lapic timer period limited to %lld ns\n",
1416 apic->vcpu->vcpu_id,
1417 apic->lapic_timer.period, min_period);
1418 apic->lapic_timer.period = min_period;
1419 }
1420 }
1421}
1422
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001423static void apic_update_lvtt(struct kvm_lapic *apic)
1424{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001425 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001426 apic->lapic_timer.timer_mode_mask;
1427
1428 if (apic->lapic_timer.timer_mode != timer_mode) {
Wanpeng Lic69518c2017-10-05 03:53:51 -07001429 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001430 APIC_LVT_TIMER_TSCDEADLINE)) {
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001431 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmář44275932017-10-06 19:25:55 +02001432 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1433 apic->lapic_timer.period = 0;
1434 apic->lapic_timer.tscdeadline = 0;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001435 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001436 apic->lapic_timer.timer_mode = timer_mode;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001437 limit_periodic_timer_frequency(apic);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001438 }
1439}
1440
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001441/*
1442 * On APICv, this test will cause a busy wait
1443 * during a higher-priority task.
1444 */
1445
1446static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1447{
1448 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001449 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001450
1451 if (kvm_apic_hw_enabled(apic)) {
1452 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001453 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001454
Andrey Smetanind62caab2015-11-10 15:36:33 +03001455 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001456 bitmap = apic->regs + APIC_IRR;
1457
1458 if (apic_test_vector(vec, bitmap))
1459 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001460 }
1461 return false;
1462}
1463
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001464static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
1465{
1466 u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1467
1468 /*
1469 * If the guest TSC is running at a different ratio than the host, then
1470 * convert the delay to nanoseconds to achieve an accurate delay. Note
1471 * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1472 * always for VMX enabled hardware.
1473 */
1474 if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) {
1475 __delay(min(guest_cycles,
1476 nsec_to_cycles(vcpu, timer_advance_ns)));
1477 } else {
1478 u64 delay_ns = guest_cycles * 1000000ULL;
1479 do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1480 ndelay(min_t(u32, delay_ns, timer_advance_ns));
1481 }
1482}
1483
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001484static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
Wanpeng Liec0671d2019-05-20 16:18:08 +08001485 s64 advance_expire_delta)
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001486{
1487 struct kvm_lapic *apic = vcpu->arch.apic;
Sean Christopherson39497d72019-04-17 10:15:32 -07001488 u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001489 u64 ns;
1490
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001491 /* Do not adjust for tiny fluctuations or large random spikes. */
1492 if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
1493 abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
1494 return;
1495
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001496 /* too early */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001497 if (advance_expire_delta < 0) {
1498 ns = -advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001499 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001500 timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001501 } else {
1502 /* too late */
Wanpeng Liec0671d2019-05-20 16:18:08 +08001503 ns = advance_expire_delta * 1000000ULL;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001504 do_div(ns, vcpu->arch.virtual_tsc_khz);
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001505 timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001506 }
1507
Wanpeng Lia0f00372019-09-26 08:54:03 +08001508 if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
1509 timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001510 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1511}
1512
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001513static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
Wanpeng Li84ea3ac2019-05-20 16:18:05 +08001514{
1515 struct kvm_lapic *apic = vcpu->arch.apic;
1516 u64 guest_tsc, tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001517
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001518 if (apic->lapic_timer.expired_tscdeadline == 0)
1519 return;
1520
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001521 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1522 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001523 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Wanpeng Liec0671d2019-05-20 16:18:08 +08001524 apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001525
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001526 if (guest_tsc < tsc_deadline)
Sean Christophersonb6aa57c2019-04-17 10:15:34 -07001527 __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
Wanpeng Li3b8a5df2018-10-09 09:02:08 +08001528
Wanpeng Lid0f5a862019-09-17 16:16:26 +08001529 if (lapic_timer_advance_dynamic)
Wanpeng Liec0671d2019-05-20 16:18:08 +08001530 adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001531}
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001532
1533void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
1534{
1535 if (lapic_timer_int_injected(vcpu))
1536 __kvm_wait_lapic_expire(vcpu);
1537}
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08001538EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001539
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001540static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
1541{
1542 struct kvm_timer *ktimer = &apic->lapic_timer;
1543
1544 kvm_apic_local_deliver(apic, APIC_LVTT);
1545 if (apic_lvtt_tscdeadline(apic))
1546 ktimer->tscdeadline = 0;
1547 if (apic_lvtt_oneshot(apic)) {
1548 ktimer->tscdeadline = 0;
1549 ktimer->target_expiration = 0;
1550 }
1551}
1552
1553static void apic_timer_expired(struct kvm_lapic *apic)
1554{
1555 struct kvm_vcpu *vcpu = apic->vcpu;
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001556 struct kvm_timer *ktimer = &apic->lapic_timer;
1557
1558 if (atomic_read(&apic->lapic_timer.pending))
1559 return;
1560
1561 if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
1562 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1563
1564 if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
1565 if (apic->lapic_timer.timer_advance_ns)
1566 __kvm_wait_lapic_expire(vcpu);
1567 kvm_apic_inject_pending_timer_irqs(apic);
1568 return;
1569 }
1570
1571 atomic_inc(&apic->lapic_timer.pending);
1572 kvm_set_pending_timer(vcpu);
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08001573}
1574
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001575static void start_sw_tscdeadline(struct kvm_lapic *apic)
1576{
Sean Christopherson39497d72019-04-17 10:15:32 -07001577 struct kvm_timer *ktimer = &apic->lapic_timer;
1578 u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001579 u64 ns = 0;
1580 ktime_t expire;
1581 struct kvm_vcpu *vcpu = apic->vcpu;
1582 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1583 unsigned long flags;
1584 ktime_t now;
1585
1586 if (unlikely(!tscdeadline || !this_tsc_khz))
1587 return;
1588
1589 local_irq_save(flags);
1590
Paolo Bonzini55878592016-10-25 15:23:49 +02001591 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001592 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Liran Alonc09d65d2019-04-16 20:36:34 +03001593
1594 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1595 do_div(ns, this_tsc_khz);
1596
1597 if (likely(tscdeadline > guest_tsc) &&
Sean Christopherson39497d72019-04-17 10:15:32 -07001598 likely(ns > apic->lapic_timer.timer_advance_ns)) {
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001599 expire = ktime_add_ns(now, ns);
Sean Christopherson39497d72019-04-17 10:15:32 -07001600 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02001601 hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001602 } else
1603 apic_timer_expired(apic);
1604
1605 local_irq_restore(flags);
1606}
1607
Wanpeng Lic301b902017-10-06 07:38:32 -07001608static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1609{
1610 ktime_t now, remaining;
1611 u64 ns_remaining_old, ns_remaining_new;
1612
1613 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1614 * APIC_BUS_CYCLE_NS * apic->divide_count;
1615 limit_periodic_timer_frequency(apic);
1616
1617 now = ktime_get();
1618 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1619 if (ktime_to_ns(remaining) < 0)
1620 remaining = 0;
1621
1622 ns_remaining_old = ktime_to_ns(remaining);
1623 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1624 apic->divide_count, old_divisor);
1625
1626 apic->lapic_timer.tscdeadline +=
1627 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1628 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1629 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1630}
1631
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001632static bool set_target_expiration(struct kvm_lapic *apic)
1633{
1634 ktime_t now;
1635 u64 tscl = rdtsc();
1636
Paolo Bonzini55878592016-10-25 15:23:49 +02001637 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001638 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1639 * APIC_BUS_CYCLE_NS * apic->divide_count;
1640
Radim Krčmář5d74a692017-10-06 19:25:54 +02001641 if (!apic->lapic_timer.period) {
1642 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001643 return false;
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001644 }
1645
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001646 limit_periodic_timer_frequency(apic);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001647
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001648 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1649 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1650 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1651
1652 return true;
1653}
1654
1655static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1656{
David Vrabeld8f2f492018-05-18 16:55:46 +01001657 ktime_t now = ktime_get();
1658 u64 tscl = rdtsc();
1659 ktime_t delta;
1660
1661 /*
1662 * Synchronize both deadlines to the same time source or
1663 * differences in the periods (caused by differences in the
1664 * underlying clocks or numerical approximation errors) will
1665 * cause the two to drift apart over time as the errors
1666 * accumulate.
1667 */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001668 apic->lapic_timer.target_expiration =
1669 ktime_add_ns(apic->lapic_timer.target_expiration,
1670 apic->lapic_timer.period);
David Vrabeld8f2f492018-05-18 16:55:46 +01001671 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1672 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1673 nsec_to_cycles(apic->vcpu, delta);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001674}
1675
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001676static void start_sw_period(struct kvm_lapic *apic)
1677{
1678 if (!apic->lapic_timer.period)
1679 return;
1680
1681 if (ktime_after(ktime_get(),
1682 apic->lapic_timer.target_expiration)) {
1683 apic_timer_expired(apic);
1684
1685 if (apic_lvtt_oneshot(apic))
1686 return;
1687
1688 advance_periodic_target_expiration(apic);
1689 }
1690
1691 hrtimer_start(&apic->lapic_timer.timer,
1692 apic->lapic_timer.target_expiration,
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001693 HRTIMER_MODE_ABS);
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001694}
1695
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001696bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1697{
Wanpeng Li91005302016-08-03 12:04:12 +08001698 if (!lapic_in_kernel(vcpu))
1699 return false;
1700
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001701 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1702}
1703EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1704
Wanpeng Li7e810a32016-10-24 18:23:12 +08001705static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001706{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001707 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001708 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
Wanpeng Libd97ad02016-06-30 08:52:49 +08001709 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1710 apic->lapic_timer.hv_timer_in_use = false;
1711}
1712
Paolo Bonzinia749e242017-06-29 17:14:50 +02001713static bool start_hv_timer(struct kvm_lapic *apic)
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001714{
1715 struct kvm_timer *ktimer = &apic->lapic_timer;
Sean Christophersonf9927982019-04-16 13:32:46 -07001716 struct kvm_vcpu *vcpu = apic->vcpu;
1717 bool expired;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001718
Wanpeng Li1d518c62017-07-25 00:43:15 -07001719 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001720 if (!kvm_x86_ops->set_hv_timer)
1721 return false;
1722
Radim Krčmář86bbc1e2017-10-06 19:25:53 +02001723 if (!ktimer->tscdeadline)
1724 return false;
1725
Sean Christophersonf9927982019-04-16 13:32:46 -07001726 if (kvm_x86_ops->set_hv_timer(vcpu, ktimer->tscdeadline, &expired))
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001727 return false;
1728
1729 ktimer->hv_timer_in_use = true;
1730 hrtimer_cancel(&ktimer->timer);
1731
1732 /*
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001733 * To simplify handling the periodic timer, leave the hv timer running
1734 * even if the deadline timer has expired, i.e. rely on the resulting
1735 * VM-Exit to recompute the periodic timer's target expiration.
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001736 */
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001737 if (!apic_lvtt_period(apic)) {
1738 /*
1739 * Cancel the hv timer if the sw timer fired while the hv timer
1740 * was being programmed, or if the hv timer itself expired.
1741 */
1742 if (atomic_read(&ktimer->pending)) {
1743 cancel_hv_timer(apic);
Sean Christophersonf9927982019-04-16 13:32:46 -07001744 } else if (expired) {
Wanpeng Lic8533542017-06-29 06:28:09 -07001745 apic_timer_expired(apic);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001746 cancel_hv_timer(apic);
1747 }
Wanpeng Lic8533542017-06-29 06:28:09 -07001748 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02001749
Sean Christophersonf9927982019-04-16 13:32:46 -07001750 trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
Sean Christophersonf1ba5cf2019-04-16 13:32:45 -07001751
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001752 return true;
1753}
1754
Paolo Bonzinia749e242017-06-29 17:14:50 +02001755static void start_sw_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001756{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001757 struct kvm_timer *ktimer = &apic->lapic_timer;
Wanpeng Li1d518c62017-07-25 00:43:15 -07001758
1759 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001760 if (apic->lapic_timer.hv_timer_in_use)
1761 cancel_hv_timer(apic);
1762 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1763 return;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001764
Paolo Bonzinia749e242017-06-29 17:14:50 +02001765 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1766 start_sw_period(apic);
1767 else if (apic_lvtt_tscdeadline(apic))
1768 start_sw_tscdeadline(apic);
1769 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1770}
1771
1772static void restart_apic_timer(struct kvm_lapic *apic)
1773{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001774 preempt_disable();
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001775
1776 if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
1777 goto out;
1778
Paolo Bonzinia749e242017-06-29 17:14:50 +02001779 if (!start_hv_timer(apic))
1780 start_sw_timer(apic);
Sean Christopherson4ca88b32019-04-16 13:32:47 -07001781out:
Wanpeng Li1d518c62017-07-25 00:43:15 -07001782 preempt_enable();
Wanpeng Li196f20c2016-06-28 14:54:19 +08001783}
1784
Eddie Dong97222cc2007-09-12 10:58:04 +03001785void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1786{
1787 struct kvm_lapic *apic = vcpu->arch.apic;
1788
Wanpeng Li1d518c62017-07-25 00:43:15 -07001789 preempt_disable();
1790 /* If the preempt notifier has already run, it also called apic_timer_expired */
1791 if (!apic->lapic_timer.hv_timer_in_use)
1792 goto out;
Eddie Dong97222cc2007-09-12 10:58:04 +03001793 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001794 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001795 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001796
1797 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1798 advance_periodic_target_expiration(apic);
Paolo Bonzinia749e242017-06-29 17:14:50 +02001799 restart_apic_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001800 }
Wanpeng Li1d518c62017-07-25 00:43:15 -07001801out:
1802 preempt_enable();
Eddie Dong97222cc2007-09-12 10:58:04 +03001803}
1804EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1805
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001806void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1807{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001808 restart_apic_timer(vcpu->arch.apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001809}
1810EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1811
1812void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1813{
1814 struct kvm_lapic *apic = vcpu->arch.apic;
1815
Wanpeng Li1d518c62017-07-25 00:43:15 -07001816 preempt_disable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001817 /* Possibly the TSC deadline timer is not enabled yet */
Paolo Bonzinia749e242017-06-29 17:14:50 +02001818 if (apic->lapic_timer.hv_timer_in_use)
1819 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07001820 preempt_enable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001821}
1822EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1823
Paolo Bonzinia749e242017-06-29 17:14:50 +02001824void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1825{
1826 struct kvm_lapic *apic = vcpu->arch.apic;
1827
1828 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1829 restart_apic_timer(apic);
1830}
1831
Eddie Dong97222cc2007-09-12 10:58:04 +03001832static void start_apic_timer(struct kvm_lapic *apic)
1833{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001834 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001835
Paolo Bonzinia749e242017-06-29 17:14:50 +02001836 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1837 && !set_target_expiration(apic))
1838 return;
1839
1840 restart_apic_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001841}
1842
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001843static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1844{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001845 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001846
Radim Krčmář59fd1322015-06-30 22:19:16 +02001847 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1848 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1849 if (lvt0_in_nmi_mode) {
Radim Krčmář42720132015-07-01 15:31:49 +02001850 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001851 } else
1852 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1853 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001854}
1855
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001856int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001857{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001858 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001859
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001860 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001861
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001862 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001863 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001864 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001865 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001866 else
1867 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001868 break;
1869
1870 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001871 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001872 apic_set_tpr(apic, val & 0xff);
1873 break;
1874
1875 case APIC_EOI:
1876 apic_set_eoi(apic);
1877 break;
1878
1879 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001880 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001881 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001882 else
1883 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001884 break;
1885
1886 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001887 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001888 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001889 recalculate_apic_map(apic->vcpu->kvm);
1890 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001891 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001892 break;
1893
Gleb Natapovfc61b802009-07-05 17:39:35 +03001894 case APIC_SPIV: {
1895 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001896 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001897 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001898 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001899 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1900 int i;
1901 u32 lvt_val;
1902
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001903 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001904 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001905 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001906 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001907 lvt_val | APIC_LVT_MASKED);
1908 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001909 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001910 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001911
1912 }
1913 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001914 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001915 case APIC_ICR:
1916 /* No delay here, so we always clear the pending bit */
Wanpeng Li2b0911d2019-09-05 14:26:27 +08001917 val &= ~(1 << 12);
1918 apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
1919 kvm_lapic_set_reg(apic, APIC_ICR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001920 break;
1921
1922 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001923 if (!apic_x2apic_mode(apic))
1924 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001925 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001926 break;
1927
Jan Kiszka23930f92008-09-26 09:30:52 +02001928 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001929 apic_manage_nmi_watchdog(apic, val);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06001930 /* fall through */
Eddie Dong97222cc2007-09-12 10:58:04 +03001931 case APIC_LVTTHMR:
1932 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001933 case APIC_LVT1:
1934 case APIC_LVTERR:
1935 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001936 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001937 val |= APIC_LVT_MASKED;
1938
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001939 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001940 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001941
1942 break;
1943
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001944 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001945 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001946 val |= APIC_LVT_MASKED;
1947 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001948 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001949 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001950 break;
1951
Eddie Dong97222cc2007-09-12 10:58:04 +03001952 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001953 if (apic_lvtt_tscdeadline(apic))
1954 break;
1955
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001956 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001957 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001958 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001959 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001960
Wanpeng Lic301b902017-10-06 07:38:32 -07001961 case APIC_TDCR: {
1962 uint32_t old_divisor = apic->divide_count;
1963
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001964 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001965 update_divide_count(apic);
Wanpeng Lic301b902017-10-06 07:38:32 -07001966 if (apic->divide_count != old_divisor &&
1967 apic->lapic_timer.period) {
1968 hrtimer_cancel(&apic->lapic_timer.timer);
1969 update_target_expiration(apic, old_divisor);
1970 restart_apic_timer(apic);
1971 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001972 break;
Wanpeng Lic301b902017-10-06 07:38:32 -07001973 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001974 case APIC_ESR:
Yi Wang0d888002019-07-06 01:08:48 +08001975 if (apic_x2apic_mode(apic) && val != 0)
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001976 ret = 1;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001977 break;
1978
1979 case APIC_SELF_IPI:
1980 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001981 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001982 } else
1983 ret = 1;
1984 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001985 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001986 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001987 break;
1988 }
Yi Wang0d888002019-07-06 01:08:48 +08001989
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001990 return ret;
1991}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001992EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001993
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001994static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001995 gpa_t address, int len, const void *data)
1996{
1997 struct kvm_lapic *apic = to_lapic(this);
1998 unsigned int offset = address - apic->base_address;
1999 u32 val;
2000
2001 if (!apic_mmio_in_range(apic, address))
2002 return -EOPNOTSUPP;
2003
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02002004 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2005 if (!kvm_check_has_quirk(vcpu->kvm,
2006 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2007 return -EOPNOTSUPP;
2008
2009 return 0;
2010 }
2011
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002012 /*
2013 * APIC register must be aligned on 128-bits boundary.
2014 * 32/64/128 bits registers must be accessed thru 32 bits.
2015 * Refer SDM 8.4.1
2016 */
Yi Wang0d888002019-07-06 01:08:48 +08002017 if (len != 4 || (offset & 0xf))
Sheng Yang756975b2009-07-06 11:05:39 +08002018 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002019
2020 val = *(u32*)data;
2021
Yi Wang0d888002019-07-06 01:08:48 +08002022 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002023
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002024 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002025}
2026
Kevin Tian58fbbf22011-08-30 13:56:17 +03002027void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
2028{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002029 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03002030}
2031EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
2032
Yang Zhang83d4c282013-01-25 10:18:49 +08002033/* emulate APIC access in a trap manner */
2034void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
2035{
2036 u32 val = 0;
2037
2038 /* hw has done the conditional check and inst decode */
2039 offset &= 0xff0;
2040
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002041 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002042
2043 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002044 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002045}
2046EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2047
Rusty Russelld5894442007-10-08 10:48:30 +10002048void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002049{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002050 struct kvm_lapic *apic = vcpu->arch.apic;
2051
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002053 return;
2054
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002055 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002056
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002057 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2058 static_key_slow_dec_deferred(&apic_hw_disabled);
2059
Radim Krčmáře4627552014-10-30 15:06:45 +01002060 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002061 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03002062
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002063 if (apic->regs)
2064 free_page((unsigned long)apic->regs);
2065
2066 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002067}
2068
2069/*
2070 *----------------------------------------------------------------------
2071 * LAPIC interface
2072 *----------------------------------------------------------------------
2073 */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002074u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2075{
2076 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002077
Wanpeng Lia10388e2016-10-24 18:23:10 +08002078 if (!lapic_in_kernel(vcpu) ||
2079 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002080 return 0;
2081
2082 return apic->lapic_timer.tscdeadline;
2083}
2084
2085void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2086{
2087 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002088
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002089 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03002090 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002091 return;
2092
2093 hrtimer_cancel(&apic->lapic_timer.timer);
2094 apic->lapic_timer.tscdeadline = data;
2095 start_apic_timer(apic);
2096}
2097
Eddie Dong97222cc2007-09-12 10:58:04 +03002098void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2099{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002101
Avi Kivityb93463a2007-10-25 16:52:32 +02002102 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002103 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03002104}
2105
2106u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2107{
Eddie Dong97222cc2007-09-12 10:58:04 +03002108 u64 tpr;
2109
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002110 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03002111
2112 return (tpr & 0xf0) >> 4;
2113}
2114
2115void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2116{
Yang Zhang8d146952013-01-25 10:18:50 +08002117 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002118 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002119
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002120 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002121 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002122
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01002123 vcpu->arch.apic_base = value;
2124
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002125 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2126 kvm_update_cpuid(vcpu);
2127
2128 if (!apic)
2129 return;
2130
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002131 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01002132 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02002133 if (value & MSR_IA32_APICBASE_ENABLE) {
2134 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002135 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08002136 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002137 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08002138 recalculate_apic_map(vcpu->kvm);
2139 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002140 }
2141
Jim Mattson8d860bb2018-05-09 16:56:05 -04002142 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2143 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2144
2145 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2146 kvm_x86_ops->set_virtual_apic_mode(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08002147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03002149 MSR_IA32_APICBASE_BASE;
2150
Nadav Amitdb324fe2014-11-02 11:54:59 +02002151 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2152 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2153 pr_warn_once("APIC base relocation is unsupported by KVM");
Eddie Dong97222cc2007-09-12 10:58:04 +03002154}
2155
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002156void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03002157{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002158 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002159 int i;
2160
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002161 if (!apic)
2162 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002163
Eddie Dong97222cc2007-09-12 10:58:04 +03002164 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002165 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002166
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002167 if (!init_event) {
2168 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2169 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002170 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002171 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03002172 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002173
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002174 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2175 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002176 apic_update_lvtt(apic);
Jan H. Schönherr52b54192017-05-20 13:24:32 +02002177 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2178 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002179 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03002180 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002181 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03002182
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002183 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002184 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002185 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02002186 if (!apic_x2apic_mode(apic))
2187 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002188 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2189 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2190 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2191 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2192 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002193 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002194 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2195 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2196 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002197 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03002198 apic->irr_pending = vcpu->arch.apicv_active;
2199 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002200 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02002201 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002202 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002203 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002204 kvm_lapic_set_base(vcpu,
2205 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002206 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002207 apic_update_ppr(apic);
Jan H. Schönherr4191db22017-10-25 16:43:27 +02002208 if (vcpu->arch.apicv_active) {
2209 kvm_x86_ops->apicv_post_state_restore(vcpu);
2210 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2211 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2212 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002213
Gleb Natapove1035712009-03-05 16:34:59 +02002214 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03002215 vcpu->arch.apic_attention = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002216}
2217
Eddie Dong97222cc2007-09-12 10:58:04 +03002218/*
2219 *----------------------------------------------------------------------
2220 * timer interface
2221 *----------------------------------------------------------------------
2222 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03002223
Avi Kivity2a6eac92012-07-26 18:01:51 +03002224static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002225{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002226 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002227}
2228
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002229int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2230{
Gleb Natapov54e98182012-08-05 15:58:32 +03002231 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002232
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01002233 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03002234 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002235
2236 return 0;
2237}
2238
Avi Kivity89342082011-11-10 14:57:21 +02002239int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002240{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002241 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002242 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002243
Gleb Natapovc48f1492012-08-05 15:58:33 +03002244 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002245 vector = reg & APIC_VECTOR_MASK;
2246 mode = reg & APIC_MODE_MASK;
2247 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08002248 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2249 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02002250 }
2251 return 0;
2252}
2253
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002254void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002255{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002256 struct kvm_lapic *apic = vcpu->arch.apic;
2257
2258 if (apic)
2259 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002260}
2261
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002262static const struct kvm_io_device_ops apic_mmio_ops = {
2263 .read = apic_mmio_read,
2264 .write = apic_mmio_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002265};
2266
Avi Kivitye9d90d42012-07-26 18:01:50 +03002267static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2268{
2269 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002270 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002271
Radim Krčmář5d87db72014-10-10 19:15:08 +02002272 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002273
Avi Kivity2a6eac92012-07-26 18:01:51 +03002274 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002275 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002276 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2277 return HRTIMER_RESTART;
2278 } else
2279 return HRTIMER_NORESTART;
2280}
2281
Sean Christophersonc3941d92019-04-17 10:15:33 -07002282int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
Eddie Dong97222cc2007-09-12 10:58:04 +03002283{
2284 struct kvm_lapic *apic;
2285
2286 ASSERT(vcpu != NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03002287
Ben Gardon254272c2019-02-11 11:02:50 -08002288 apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
Eddie Dong97222cc2007-09-12 10:58:04 +03002289 if (!apic)
2290 goto nomem;
2291
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002292 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002293
Ben Gardon254272c2019-02-11 11:02:50 -08002294 apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002295 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002296 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2297 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002298 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002299 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002300 apic->vcpu = vcpu;
2301
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002302 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02002303 HRTIMER_MODE_ABS_HARD);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002304 apic->lapic_timer.timer.function = apic_timer_fn;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002305 if (timer_advance_ns == -1) {
Wanpeng Lia0f00372019-09-26 08:54:03 +08002306 apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
Wanpeng Lid0f5a862019-09-17 16:16:26 +08002307 lapic_timer_advance_dynamic = true;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002308 } else {
2309 apic->lapic_timer.timer_advance_ns = timer_advance_ns;
Wanpeng Lid0f5a862019-09-17 16:16:26 +08002310 lapic_timer_advance_dynamic = false;
Sean Christophersonc3941d92019-04-17 10:15:33 -07002311 }
2312
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002313 /*
2314 * APIC is created enabled. This will prevent kvm_lapic_set_base from
Wei Yangee171d22019-03-31 19:17:22 -07002315 * thinking that APIC state has changed.
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002316 */
2317 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002318 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Gregory Haskinsd76685c42009-06-01 12:54:50 -04002319 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002320
2321 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002322nomem_free_apic:
2323 kfree(apic);
Saar Amara251fb902019-05-06 11:29:16 +03002324 vcpu->arch.apic = NULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002325nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002326 return -ENOMEM;
2327}
Eddie Dong97222cc2007-09-12 10:58:04 +03002328
2329int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2330{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002331 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002332 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002333
Wanpeng Libb34e692019-07-02 17:25:02 +08002334 if (!kvm_apic_hw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002335 return -1;
2336
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002337 __apic_update_ppr(apic, &ppr);
2338 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002339}
2340
Qing He40487c62007-09-17 14:47:13 +08002341int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2342{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002343 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002344 int r = 0;
2345
Gleb Natapovc48f1492012-08-05 15:58:33 +03002346 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002347 r = 1;
2348 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2349 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2350 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002351 return r;
2352}
2353
Eddie Dong1b9778d2007-09-03 16:56:58 +03002354void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2355{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002356 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002357
Gleb Natapov54e98182012-08-05 15:58:32 +03002358 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08002359 kvm_apic_inject_pending_timer_irqs(apic);
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002360 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002361 }
2362}
2363
Eddie Dong97222cc2007-09-12 10:58:04 +03002364int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2365{
2366 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002367 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002368 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002369
2370 if (vector == -1)
2371 return -1;
2372
Wanpeng Li56cc2402014-08-05 12:42:24 +08002373 /*
2374 * We get here even with APIC virtualization enabled, if doing
2375 * nested virtualization and L1 runs with the "acknowledge interrupt
2376 * on exit" mode. Then we cannot inject the interrupt via RVI,
2377 * because the process would deliver it through the IDT.
2378 */
2379
Eddie Dong97222cc2007-09-12 10:58:04 +03002380 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002381 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002382 /*
2383 * For auto-EOI interrupts, there might be another pending
2384 * interrupt above PPR, so check whether to raise another
2385 * KVM_REQ_EVENT.
2386 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002387 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002388 } else {
2389 /*
2390 * For normal interrupts, PPR has been raised and there cannot
2391 * be a higher-priority pending interrupt---except if there was
2392 * a concurrent interrupt injection, but that would have
2393 * triggered KVM_REQ_EVENT already.
2394 */
2395 apic_set_isr(vector, apic);
2396 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002397 }
2398
Eddie Dong97222cc2007-09-12 10:58:04 +03002399 return vector;
2400}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002401
Radim Krčmářa92e2542016-07-12 22:09:22 +02002402static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2403 struct kvm_lapic_state *s, bool set)
2404{
2405 if (apic_x2apic_mode(vcpu->arch.apic)) {
2406 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002407 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002408
Radim Krčmář371313132016-07-12 22:09:27 +02002409 if (vcpu->kvm->arch.x2apic_format) {
2410 if (*id != vcpu->vcpu_id)
2411 return -EINVAL;
2412 } else {
2413 if (set)
2414 *id >>= 24;
2415 else
2416 *id <<= 24;
2417 }
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002418
2419 /* In x2APIC mode, the LDR is fixed and based on the id */
2420 if (set)
2421 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002422 }
2423
2424 return 0;
2425}
2426
2427int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2428{
2429 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2430 return kvm_apic_state_fixup(vcpu, s, false);
2431}
2432
2433int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002434{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002436 int r;
2437
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002438
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002439 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002440 /* set SPIV separately to get count of SW disabled APICs right */
2441 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002442
2443 r = kvm_apic_state_fixup(vcpu, s, true);
2444 if (r)
2445 return r;
Jordan Borgner0e96f312018-10-28 12:58:28 +00002446 memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002447
2448 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002449 kvm_apic_set_version(vcpu);
2450
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002451 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002452 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002453 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002454 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002455 update_divide_count(apic);
2456 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002457 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002458 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002459 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002460 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002461 if (vcpu->arch.apicv_active) {
Paolo Bonzini967235d2016-12-19 14:03:45 +01002462 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002463 kvm_x86_ops->hwapic_irr_update(vcpu,
2464 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002465 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002466 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002467 }
Avi Kivity3842d132010-07-27 12:30:24 +03002468 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002469 if (ioapic_in_kernel(vcpu->kvm))
2470 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002471
2472 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002473
2474 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002475}
Eddie Donga3d7f852007-09-03 16:15:12 +03002476
Avi Kivity2f52d582008-01-16 12:49:30 +02002477void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002478{
Eddie Donga3d7f852007-09-03 16:15:12 +03002479 struct hrtimer *timer;
2480
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08002481 if (!lapic_in_kernel(vcpu) ||
2482 kvm_can_post_timer_interrupt(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002483 return;
2484
Gleb Natapov54e98182012-08-05 15:58:32 +03002485 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002486 if (hrtimer_cancel(timer))
Sebastian Andrzej Siewior2c0d2782019-07-26 20:30:55 +02002487 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
Eddie Donga3d7f852007-09-03 16:15:12 +03002488}
Avi Kivityb93463a2007-10-25 16:52:32 +02002489
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002490/*
2491 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2492 *
2493 * Detect whether guest triggered PV EOI since the
2494 * last entry. If yes, set EOI on guests's behalf.
2495 * Clear PV EOI in guest memory in any case.
2496 */
2497static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2498 struct kvm_lapic *apic)
2499{
2500 bool pending;
2501 int vector;
2502 /*
2503 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2504 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2505 *
2506 * KVM_APIC_PV_EOI_PENDING is unset:
2507 * -> host disabled PV EOI.
2508 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2509 * -> host enabled PV EOI, guest did not execute EOI yet.
2510 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2511 * -> host enabled PV EOI, guest executed EOI.
2512 */
2513 BUG_ON(!pv_eoi_enabled(vcpu));
2514 pending = pv_eoi_get_pending(vcpu);
2515 /*
2516 * Clear pending bit in any case: it will be set again on vmentry.
2517 * While this might not be ideal from performance point of view,
2518 * this makes sure pv eoi is only enabled when we know it's safe.
2519 */
2520 pv_eoi_clr_pending(vcpu);
2521 if (pending)
2522 return;
2523 vector = apic_set_eoi(apic);
2524 trace_kvm_pv_eoi(apic, vector);
2525}
2526
Avi Kivityb93463a2007-10-25 16:52:32 +02002527void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2528{
2529 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002530
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002531 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2532 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2533
Gleb Natapov41383772012-04-19 14:06:29 +03002534 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002535 return;
2536
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002537 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2538 sizeof(u32)))
Nicholas Krause603242a2015-08-05 10:44:40 -04002539 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002540
2541 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2542}
2543
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002544/*
2545 * apic_sync_pv_eoi_to_guest - called before vmentry
2546 *
2547 * Detect whether it's safe to enable PV EOI and
2548 * if yes do so.
2549 */
2550static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2551 struct kvm_lapic *apic)
2552{
2553 if (!pv_eoi_enabled(vcpu) ||
2554 /* IRR set or many bits in ISR: could be nested. */
2555 apic->irr_pending ||
2556 /* Cache not set: could be safe but we don't bother. */
2557 apic->highest_isr_cache == -1 ||
2558 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002559 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002560 /*
2561 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2562 * so we need not do anything here.
2563 */
2564 return;
2565 }
2566
2567 pv_eoi_set_pending(apic->vcpu);
2568}
2569
Avi Kivityb93463a2007-10-25 16:52:32 +02002570void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2571{
2572 u32 data, tpr;
2573 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002574 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002575
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002576 apic_sync_pv_eoi_to_guest(vcpu, apic);
2577
Gleb Natapov41383772012-04-19 14:06:29 +03002578 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002579 return;
2580
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002581 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002582 max_irr = apic_find_highest_irr(apic);
2583 if (max_irr < 0)
2584 max_irr = 0;
2585 max_isr = apic_find_highest_isr(apic);
2586 if (max_isr < 0)
2587 max_isr = 0;
2588 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2589
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002590 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2591 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002592}
2593
Andy Honigfda4e2e2013-11-20 10:23:22 -08002594int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002595{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002596 if (vapic_addr) {
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002597 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andy Honigfda4e2e2013-11-20 10:23:22 -08002598 &vcpu->arch.apic->vapic_cache,
2599 vapic_addr, sizeof(u32)))
2600 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002601 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002602 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002603 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002604 }
2605
2606 vcpu->arch.apic->vapic_addr = vapic_addr;
2607 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002608}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002609
2610int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2611{
2612 struct kvm_lapic *apic = vcpu->arch.apic;
2613 u32 reg = (msr - APIC_BASE_MSR) << 4;
2614
Paolo Bonzini35754c92015-07-29 12:05:37 +02002615 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002616 return 1;
2617
Nadav Amitc69d3d92014-11-26 17:56:25 +02002618 if (reg == APIC_ICR2)
2619 return 1;
2620
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002621 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002622 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002623 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2624 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002625}
2626
2627int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2628{
2629 struct kvm_lapic *apic = vcpu->arch.apic;
2630 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2631
Paolo Bonzini35754c92015-07-29 12:05:37 +02002632 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002633 return 1;
2634
Yi Wang0d888002019-07-06 01:08:48 +08002635 if (reg == APIC_DFR || reg == APIC_ICR2)
Nadav Amitc69d3d92014-11-26 17:56:25 +02002636 return 1;
Nadav Amitc69d3d92014-11-26 17:56:25 +02002637
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002638 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002639 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002640 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002641 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002642
2643 *data = (((u64)high) << 32) | low;
2644
2645 return 0;
2646}
Gleb Natapov10388a02010-01-17 15:51:23 +02002647
2648int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2649{
2650 struct kvm_lapic *apic = vcpu->arch.apic;
2651
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002652 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002653 return 1;
2654
2655 /* if this is ICR write vector before command */
2656 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002657 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2658 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002659}
2660
2661int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2662{
2663 struct kvm_lapic *apic = vcpu->arch.apic;
2664 u32 low, high = 0;
2665
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002666 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002667 return 1;
2668
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002669 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002670 return 1;
2671 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002672 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002673
2674 *data = (((u64)high) << 32) | low;
2675
2676 return 0;
2677}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002678
Ladi Prosek72bbf932018-10-16 18:49:59 +02002679int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002680{
2681 u64 addr = data & ~KVM_MSR_ENABLED;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002682 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
2683 unsigned long new_len;
2684
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002685 if (!IS_ALIGNED(addr, 4))
2686 return 1;
2687
2688 vcpu->arch.pv_eoi.msr_val = data;
2689 if (!pv_eoi_enabled(vcpu))
2690 return 0;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002691
2692 if (addr == ghc->gpa && len <= ghc->len)
2693 new_len = ghc->len;
2694 else
2695 new_len = len;
2696
2697 return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002698}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002699
Jan Kiszka66450a22013-03-13 12:42:34 +01002700void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2701{
2702 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002703 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002704 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002705
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002706 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002707 return;
2708
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002709 /*
Liran Alon4b9852f2019-08-26 13:24:49 +03002710 * INITs are latched while CPU is in specific states
2711 * (SMM, VMX non-root mode, SVM with GIF=0).
2712 * Because a CPU cannot be in these states immediately
2713 * after it has processed an INIT signal (and thus in
2714 * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs
2715 * and leave the INIT pending.
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002716 */
Liran Alon27cbe7d2019-11-11 11:16:40 +02002717 if (kvm_vcpu_latch_init(vcpu)) {
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002718 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2719 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2720 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2721 return;
2722 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002723
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002724 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002725 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002726 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002727 if (kvm_vcpu_is_bsp(apic->vcpu))
2728 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2729 else
2730 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2731 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002732 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002733 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2734 /* evaluate pending_events before reading the vector */
2735 smp_rmb();
2736 sipi_vector = apic->sipi_vector;
Jan Kiszka66450a22013-03-13 12:42:34 +01002737 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2738 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2739 }
2740}
2741
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002742void kvm_lapic_init(void)
2743{
2744 /* do not patch jump label more than once per second */
2745 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002746 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002747}
David Matlackcef84c32016-12-16 14:30:36 -08002748
2749void kvm_lapic_exit(void)
2750{
2751 static_key_deferred_flush(&apic_hw_disabled);
2752 static_key_deferred_flush(&apic_sw_disabled);
2753}