blob: 3cd227ff807fadd27284e6bc81ef2c6f8a60f3d1 [file] [log] [blame]
Eddie Dong97222cc2007-09-12 10:58:04 +03001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +03009 *
10 * Authors:
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 *
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030022#include <linux/kvm.h>
23#include <linux/mm.h>
24#include <linux/highmem.h>
25#include <linux/smp.h>
26#include <linux/hrtimer.h>
27#include <linux/io.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040028#include <linux/export.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070029#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030031#include <asm/processor.h>
32#include <asm/msr.h>
33#include <asm/page.h>
34#include <asm/current.h>
35#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050036#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030038#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030040#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030042#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020043#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030044#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030045
Marcelo Tosattib682b812009-02-10 20:41:41 -020046#ifndef CONFIG_X86_64
47#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48#else
49#define mod_64(x, y) ((x) % (y))
50#endif
51
Eddie Dong97222cc2007-09-12 10:58:04 +030052#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
Eddie Dong97222cc2007-09-12 10:58:04 +030057/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
58#define apic_debug(fmt, arg...)
59
Eddie Dong97222cc2007-09-12 10:58:04 +030060/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050061#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030062#define LAPIC_MMIO_LENGTH (1 << 12)
63/* followed define is not in apicdef.h */
64#define APIC_SHORT_MASK 0xc0000
65#define APIC_DEST_NOSHORT 0x0
66#define APIC_DEST_MASK 0x800
67#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090068#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030069
Nadav Amit394457a2014-10-03 00:30:52 +030070#define APIC_BROADCAST 0xFF
71#define X2APIC_BROADCAST 0xFFFFFFFFul
72
Wanpeng Li3b8a5df2018-10-09 09:02:08 +080073static bool lapic_timer_advance_adjust_done = false;
74#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
75/* step-by-step approximation to mitigate fluctuation */
76#define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
77
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030078static inline int apic_test_vector(int vec, void *bitmap)
79{
80 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
81}
82
Yang Zhang10606912013-04-11 19:21:38 +080083bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
84{
85 struct kvm_lapic *apic = vcpu->arch.apic;
86
87 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
88 apic_test_vector(vector, apic->regs + APIC_IRR);
89}
90
Eddie Dong97222cc2007-09-12 10:58:04 +030091static inline void apic_clear_vector(int vec, void *bitmap)
92{
93 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
94}
95
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030096static inline int __apic_test_and_set_vector(int vec, void *bitmap)
97{
98 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
99}
100
101static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
102{
103 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
104}
105
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300106struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300107struct static_key_deferred apic_sw_disabled __read_mostly;
108
Eddie Dong97222cc2007-09-12 10:58:04 +0300109static inline int apic_enabled(struct kvm_lapic *apic)
110{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300111 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300112}
113
Eddie Dong97222cc2007-09-12 10:58:04 +0300114#define LVT_MASK \
115 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
116
117#define LINT_MASK \
118 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
119 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
120
Radim Krčmář6e500432016-12-15 18:06:46 +0100121static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
122{
123 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
124}
125
126static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
127{
128 return apic->vcpu->vcpu_id;
129}
130
Radim Krčmáře45115b2016-07-12 22:09:19 +0200131static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
132 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
133 switch (map->mode) {
134 case KVM_APIC_MODE_X2APIC: {
135 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200136 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100137
Radim Krčmáře45115b2016-07-12 22:09:19 +0200138 if (offset <= max_apic_id) {
139 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100140
Radim Krčmáře45115b2016-07-12 22:09:19 +0200141 *cluster = &map->phys_map[offset];
142 *mask = dest_id & (0xffff >> (16 - cluster_size));
143 } else {
144 *mask = 0;
145 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100146
Radim Krčmáře45115b2016-07-12 22:09:19 +0200147 return true;
148 }
149 case KVM_APIC_MODE_XAPIC_FLAT:
150 *cluster = map->xapic_flat_map;
151 *mask = dest_id & 0xff;
152 return true;
153 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100154 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200155 *mask = dest_id & 0xf;
156 return true;
157 default:
158 /* Not optimized. */
159 return false;
160 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300161}
162
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200163static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100164{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200165 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100166
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200167 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100168}
169
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300170static void recalculate_apic_map(struct kvm *kvm)
171{
172 struct kvm_apic_map *new, *old = NULL;
173 struct kvm_vcpu *vcpu;
174 int i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100175 u32 max_id = 255; /* enough space for any xAPIC ID */
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300176
177 mutex_lock(&kvm->arch.apic_map_lock);
178
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200179 kvm_for_each_vcpu(i, vcpu, kvm)
180 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100181 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200182
Michal Hockoa7c3e902017-05-08 15:57:09 -0700183 new = kvzalloc(sizeof(struct kvm_apic_map) +
184 sizeof(struct kvm_lapic *) * ((u64)max_id + 1), GFP_KERNEL);
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200185
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300186 if (!new)
187 goto out;
188
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200189 new->max_apic_id = max_id;
190
Nadav Amit173beed2014-11-02 11:54:54 +0200191 kvm_for_each_vcpu(i, vcpu, kvm) {
192 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200193 struct kvm_lapic **cluster;
194 u16 mask;
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100195 u32 ldr;
196 u8 xapic_id;
197 u32 x2apic_id;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300198
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100199 if (!kvm_apic_present(vcpu))
200 continue;
201
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100202 xapic_id = kvm_xapic_id(apic);
203 x2apic_id = kvm_x2apic_id(apic);
204
205 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
206 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
207 x2apic_id <= new->max_apic_id)
208 new->phys_map[x2apic_id] = apic;
209 /*
210 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
211 * prevent them from masking VCPUs with APIC ID <= 0xff.
212 */
213 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
214 new->phys_map[xapic_id] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100215
Radim Krčmář6e500432016-12-15 18:06:46 +0100216 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
217
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100218 if (apic_x2apic_mode(apic)) {
219 new->mode |= KVM_APIC_MODE_X2APIC;
220 } else if (ldr) {
221 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500222 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100223 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
224 else
225 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
226 }
227
Radim Krčmáře45115b2016-07-12 22:09:19 +0200228 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100229 continue;
230
Radim Krčmáře45115b2016-07-12 22:09:19 +0200231 if (mask)
232 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300233 }
234out:
235 old = rcu_dereference_protected(kvm->arch.apic_map,
236 lockdep_is_held(&kvm->arch.apic_map_lock));
237 rcu_assign_pointer(kvm->arch.apic_map, new);
238 mutex_unlock(&kvm->arch.apic_map_lock);
239
240 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200241 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800242
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700243 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300244}
245
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300246static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
247{
Radim Krčmáře4627552014-10-30 15:06:45 +0100248 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300249
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500250 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100251
252 if (enabled != apic->sw_enabled) {
253 apic->sw_enabled = enabled;
254 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300255 static_key_slow_dec_deferred(&apic_sw_disabled);
256 recalculate_apic_map(apic->vcpu->kvm);
257 } else
258 static_key_slow_inc(&apic_sw_disabled.key);
259 }
260}
261
Radim Krčmářa92e2542016-07-12 22:09:22 +0200262static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300263{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500264 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300265 recalculate_apic_map(apic->vcpu->kvm);
266}
267
268static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
269{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500270 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300271 recalculate_apic_map(apic->vcpu->kvm);
272}
273
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000274static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
275{
276 return ((id >> 4) << 16) | (1 << (id & 0xf));
277}
278
Radim Krčmářa92e2542016-07-12 22:09:22 +0200279static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200280{
Dr. David Alan Gilberte872fa92017-11-17 11:52:49 +0000281 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200282
Radim Krčmář6e500432016-12-15 18:06:46 +0100283 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
284
Radim Krčmářa92e2542016-07-12 22:09:22 +0200285 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500286 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200287 recalculate_apic_map(apic->vcpu->kvm);
288}
289
Eddie Dong97222cc2007-09-12 10:58:04 +0300290static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
291{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500292 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300293}
294
295static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
296{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500297 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300298}
299
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800300static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
301{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100302 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800303}
304
Eddie Dong97222cc2007-09-12 10:58:04 +0300305static inline int apic_lvtt_period(struct kvm_lapic *apic)
306{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100307 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800308}
309
310static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
311{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100312 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300313}
314
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200315static inline int apic_lvt_nmi_mode(u32 lvt_val)
316{
317 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
318}
319
Gleb Natapovfc61b802009-07-05 17:39:35 +0300320void kvm_apic_set_version(struct kvm_vcpu *vcpu)
321{
322 struct kvm_lapic *apic = vcpu->arch.apic;
323 struct kvm_cpuid_entry2 *feat;
324 u32 v = APIC_VERSION;
325
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100326 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300327 return;
328
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100329 /*
330 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
331 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
332 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
333 * version first and level-triggered interrupts never get EOIed in
334 * IOAPIC.
335 */
Gleb Natapovfc61b802009-07-05 17:39:35 +0300336 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
Vitaly Kuznetsov0bcc3fb2018-02-09 14:01:33 +0100337 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
338 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300339 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500340 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300341}
342
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500343static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800344 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300345 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
346 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
347 LINT_MASK, LINT_MASK, /* LVT0-1 */
348 LVT_MASK /* LVTERR */
349};
350
351static int find_highest_vector(void *bitmap)
352{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900353 int vec;
354 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300355
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900356 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
357 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
358 reg = bitmap + REG_POS(vec);
359 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100360 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900361 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300362
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900363 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300364}
365
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300366static u8 count_vectors(void *bitmap)
367{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900368 int vec;
369 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300370 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900371
372 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
373 reg = bitmap + REG_POS(vec);
374 count += hweight32(*reg);
375 }
376
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300377 return count;
378}
379
Liran Alone7387b02017-12-24 18:12:54 +0200380bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Yang Zhanga20ed542013-04-11 19:25:15 +0800381{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100382 u32 i, vec;
Liran Alone7387b02017-12-24 18:12:54 +0200383 u32 pir_val, irr_val, prev_irr_val;
384 int max_updated_irr;
385
386 max_updated_irr = -1;
387 *max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800388
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100389 for (i = vec = 0; i <= 7; i++, vec += 32) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200390 pir_val = READ_ONCE(pir[i]);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100391 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
Paolo Bonziniad361092016-09-20 16:15:05 +0200392 if (pir_val) {
Liran Alone7387b02017-12-24 18:12:54 +0200393 prev_irr_val = irr_val;
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100394 irr_val |= xchg(&pir[i], 0);
395 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
Liran Alone7387b02017-12-24 18:12:54 +0200396 if (prev_irr_val != irr_val) {
397 max_updated_irr =
398 __fls(irr_val ^ prev_irr_val) + vec;
399 }
Paolo Bonziniad361092016-09-20 16:15:05 +0200400 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100401 if (irr_val)
Liran Alone7387b02017-12-24 18:12:54 +0200402 *max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800403 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100404
Liran Alone7387b02017-12-24 18:12:54 +0200405 return ((max_updated_irr != -1) &&
406 (max_updated_irr == *max_irr));
Yang Zhanga20ed542013-04-11 19:25:15 +0800407}
Wincy Van705699a2015-02-03 23:58:17 +0800408EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
409
Liran Alone7387b02017-12-24 18:12:54 +0200410bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
Wincy Van705699a2015-02-03 23:58:17 +0800411{
412 struct kvm_lapic *apic = vcpu->arch.apic;
413
Liran Alone7387b02017-12-24 18:12:54 +0200414 return __kvm_apic_update_irr(pir, apic->regs, max_irr);
Wincy Van705699a2015-02-03 23:58:17 +0800415}
Yang Zhanga20ed542013-04-11 19:25:15 +0800416EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
417
Gleb Natapov33e4c682009-06-11 11:06:51 +0300418static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300419{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300420 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300421}
422
423static inline int apic_find_highest_irr(struct kvm_lapic *apic)
424{
425 int result;
426
Yang Zhangc7c9c562013-01-25 10:18:51 +0800427 /*
428 * Note that irr_pending is just a hint. It will be always
429 * true with virtual interrupt delivery enabled.
430 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300431 if (!apic->irr_pending)
432 return -1;
433
434 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300435 ASSERT(result == -1 || result >= 16);
436
437 return result;
438}
439
Gleb Natapov33e4c682009-06-11 11:06:51 +0300440static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
441{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800442 struct kvm_vcpu *vcpu;
443
444 vcpu = apic->vcpu;
445
Andrey Smetanind62caab2015-11-10 15:36:33 +0300446 if (unlikely(vcpu->arch.apicv_active)) {
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100447 /* need to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200448 apic_clear_vector(vec, apic->regs + APIC_IRR);
Paolo Bonzinib95234c2016-12-19 13:57:33 +0100449 kvm_x86_ops->hwapic_irr_update(vcpu,
450 apic_find_highest_irr(apic));
Nadav Amitf210f752014-11-16 23:49:07 +0200451 } else {
452 apic->irr_pending = false;
453 apic_clear_vector(vec, apic->regs + APIC_IRR);
454 if (apic_search_irr(apic) != -1)
455 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800456 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300457}
458
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300459static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
460{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800461 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200462
Wanpeng Li56cc2402014-08-05 12:42:24 +0800463 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
464 return;
465
466 vcpu = apic->vcpu;
467
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300468 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800469 * With APIC virtualization enabled, all caching is disabled
470 * because the processor can modify ISR under the hood. Instead
471 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300472 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300473 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200474 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800475 else {
476 ++apic->isr_count;
477 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
478 /*
479 * ISR (in service register) bit is set when injecting an interrupt.
480 * The highest vector is injected. Thus the latest bit set matches
481 * the highest bit in ISR.
482 */
483 apic->highest_isr_cache = vec;
484 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300485}
486
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200487static inline int apic_find_highest_isr(struct kvm_lapic *apic)
488{
489 int result;
490
491 /*
492 * Note that isr_count is always 1, and highest_isr_cache
493 * is always -1, with APIC virtualization enabled.
494 */
495 if (!apic->isr_count)
496 return -1;
497 if (likely(apic->highest_isr_cache != -1))
498 return apic->highest_isr_cache;
499
500 result = find_highest_vector(apic->regs + APIC_ISR);
501 ASSERT(result == -1 || result >= 16);
502
503 return result;
504}
505
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300506static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
507{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200508 struct kvm_vcpu *vcpu;
509 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
510 return;
511
512 vcpu = apic->vcpu;
513
514 /*
515 * We do get here for APIC virtualization enabled if the guest
516 * uses the Hyper-V APIC enlightenment. In this case we may need
517 * to trigger a new interrupt delivery by writing the SVI field;
518 * on the other hand isr_count and highest_isr_cache are unused
519 * and must be left alone.
520 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300521 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200522 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200523 apic_find_highest_isr(apic));
524 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300525 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200526 BUG_ON(apic->isr_count < 0);
527 apic->highest_isr_cache = -1;
528 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300529}
530
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800531int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
532{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300533 /* This may race with setting of irr in __apic_accept_irq() and
534 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
535 * will cause vmexit immediately and the value will be recalculated
536 * on the next vmentry.
537 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100538 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800539}
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100540EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800541
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200542static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800543 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100544 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200545
Yang Zhangb4f22252013-04-11 19:21:37 +0800546int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100547 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300548{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800549 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800550
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200551 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800552 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300553}
554
Wanpeng Li4180bf12018-07-23 14:39:54 +0800555int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800556 unsigned long ipi_bitmap_high, u32 min,
Wanpeng Li4180bf12018-07-23 14:39:54 +0800557 unsigned long icr, int op_64_bit)
558{
559 int i;
560 struct kvm_apic_map *map;
561 struct kvm_vcpu *vcpu;
562 struct kvm_lapic_irq irq = {0};
563 int cluster_size = op_64_bit ? 64 : 32;
564 int count = 0;
565
566 irq.vector = icr & APIC_VECTOR_MASK;
567 irq.delivery_mode = icr & APIC_MODE_MASK;
568 irq.level = (icr & APIC_INT_ASSERT) != 0;
569 irq.trig_mode = icr & APIC_INT_LEVELTRIG;
570
571 if (icr & APIC_DEST_MASK)
572 return -KVM_EINVAL;
573 if (icr & APIC_SHORT_MASK)
574 return -KVM_EINVAL;
575
576 rcu_read_lock();
577 map = rcu_dereference(kvm->arch.apic_map);
578
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800579 if (min > map->max_apic_id)
580 goto out;
Wanpeng Li4180bf12018-07-23 14:39:54 +0800581 /* Bits above cluster_size are masked in the caller. */
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800582 for_each_set_bit(i, &ipi_bitmap_low,
583 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
584 if (map->phys_map[min + i]) {
585 vcpu = map->phys_map[min + i]->vcpu;
586 count += kvm_apic_set_irq(vcpu, &irq, NULL);
587 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800588 }
589
590 min += cluster_size;
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800591
592 if (min > map->max_apic_id)
593 goto out;
594
595 for_each_set_bit(i, &ipi_bitmap_high,
596 min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
597 if (map->phys_map[min + i]) {
598 vcpu = map->phys_map[min + i]->vcpu;
599 count += kvm_apic_set_irq(vcpu, &irq, NULL);
600 }
Wanpeng Li4180bf12018-07-23 14:39:54 +0800601 }
602
Wanpeng Libdf7ffc2018-08-30 10:03:30 +0800603out:
Wanpeng Li4180bf12018-07-23 14:39:54 +0800604 rcu_read_unlock();
605 return count;
606}
607
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300608static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
609{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200610
611 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
612 sizeof(val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300613}
614
615static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
616{
Paolo Bonzini4e335d92017-05-02 16:20:18 +0200617
618 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
619 sizeof(*val));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300620}
621
622static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
623{
624 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
625}
626
627static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
628{
629 u8 val;
630 if (pv_eoi_get_user(vcpu, &val) < 0)
631 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800632 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300633 return val & 0x1;
634}
635
636static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
637{
638 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
639 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800640 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300641 return;
642 }
643 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
644}
645
646static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
647{
648 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
649 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800650 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300651 return;
652 }
653 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
654}
655
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100656static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
657{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100658 int highest_irr;
Liran Alonfa59cc02017-12-24 18:12:53 +0200659 if (apic->vcpu->arch.apicv_active)
Paolo Bonzini76dfafd52016-12-19 17:17:11 +0100660 highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
661 else
662 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100663 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
664 return -1;
665 return highest_irr;
666}
667
668static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300669{
Avi Kivity3842d132010-07-27 12:30:24 +0300670 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300671 int isr;
672
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500673 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
674 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300675 isr = apic_find_highest_isr(apic);
676 isrv = (isr != -1) ? isr : 0;
677
678 if ((tpr & 0xf0) >= (isrv & 0xf0))
679 ppr = tpr & 0xff;
680 else
681 ppr = isrv & 0xf0;
682
683 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
684 apic, ppr, isr, isrv);
685
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100686 *new_ppr = ppr;
687 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500688 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100689
690 return ppr < old_ppr;
691}
692
693static void apic_update_ppr(struct kvm_lapic *apic)
694{
695 u32 ppr;
696
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100697 if (__apic_update_ppr(apic, &ppr) &&
698 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100699 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300700}
701
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100702void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
703{
704 apic_update_ppr(vcpu->arch.apic);
705}
706EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
707
Eddie Dong97222cc2007-09-12 10:58:04 +0300708static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
709{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500710 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300711 apic_update_ppr(apic);
712}
713
Radim Krčmář03d22492015-02-12 19:41:31 +0100714static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300715{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100716 return mda == (apic_x2apic_mode(apic) ?
717 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300718}
719
Radim Krčmář03d22492015-02-12 19:41:31 +0100720static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300721{
Radim Krčmář03d22492015-02-12 19:41:31 +0100722 if (kvm_apic_broadcast(apic, mda))
723 return true;
724
725 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100726 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100727
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100728 /*
729 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
730 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
731 * this allows unique addressing of VCPUs with APIC ID over 0xff.
732 * The 0xff condition is needed because writeable xAPIC ID.
733 */
734 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
735 return true;
736
Radim Krčmářb4535b52016-12-15 18:06:47 +0100737 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300738}
739
Radim Krčmář52c233a2015-01-29 22:48:48 +0100740static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300741{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300742 u32 logical_id;
743
Nadav Amit394457a2014-10-03 00:30:52 +0300744 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100745 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300746
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500747 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300748
Radim Krčmář9368b562015-01-29 22:48:49 +0100749 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100750 return ((logical_id >> 16) == (mda >> 16))
751 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100752
753 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300754
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500755 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300756 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100757 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300758 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100759 return ((logical_id >> 4) == (mda >> 4))
760 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300761 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200762 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500763 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100764 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300765 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300766}
767
Radim Krčmářc5192652016-07-12 22:09:28 +0200768/* The KVM local APIC implementation has two quirks:
769 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100770 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
771 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
772 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200773 *
774 * - in-kernel IOAPIC messages have to be delivered directly to
775 * x2APIC, because the kernel does not support interrupt remapping.
776 * In order to support broadcast without interrupt remapping, x2APIC
777 * rewrites the destination of non-IPI messages from APIC_BROADCAST
778 * to X2APIC_BROADCAST.
779 *
780 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
781 * important when userspace wants to use x2APIC-format MSIs, because
782 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100783 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200784static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
785 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100786{
787 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100788
Radim Krčmářc5192652016-07-12 22:09:28 +0200789 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100790 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100791 return X2APIC_BROADCAST;
792
Radim Krčmářb4535b52016-12-15 18:06:47 +0100793 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100794}
795
Radim Krčmář52c233a2015-01-29 22:48:48 +0100796bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300797 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300798{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800799 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200800 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300801
802 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200803 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300804 target, source, dest, dest_mode, short_hand);
805
Zachary Amsdenbd371392010-06-14 11:42:15 -1000806 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300807 switch (short_hand) {
808 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100809 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100810 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200811 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100812 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300813 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100814 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300815 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100816 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300817 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100818 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300819 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200820 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
821 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100822 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300823 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300824}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500825EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300826
Feng Wu520040142016-01-25 16:53:33 +0800827int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
828 const unsigned long *bitmap, u32 bitmap_size)
829{
830 u32 mod;
831 int i, idx = -1;
832
833 mod = vector % dest_vcpus;
834
835 for (i = 0; i <= mod; i++) {
836 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
837 BUG_ON(idx == bitmap_size);
838 }
839
840 return idx;
841}
842
Radim Krčmář4efd8052016-02-12 15:00:15 +0100843static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
844{
845 if (!kvm->arch.disabled_lapic_found) {
846 kvm->arch.disabled_lapic_found = true;
847 printk(KERN_INFO
848 "Disabled LAPIC found during irq injection\n");
849 }
850}
851
Radim Krčmářc5192652016-07-12 22:09:28 +0200852static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
853 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
854{
855 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
856 if ((irq->dest_id == APIC_BROADCAST &&
857 map->mode != KVM_APIC_MODE_X2APIC))
858 return true;
859 if (irq->dest_id == X2APIC_BROADCAST)
860 return true;
861 } else {
862 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
863 if (irq->dest_id == (x2apic_ipi ?
864 X2APIC_BROADCAST : APIC_BROADCAST))
865 return true;
866 }
867
868 return false;
869}
870
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200871/* Return true if the interrupt can be handled by using *bitmap as index mask
872 * for valid destinations in *dst array.
873 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
874 * Note: we may have zero kvm_lapic destinations when we return true, which
875 * means that the interrupt should be dropped. In this case, *bitmap would be
876 * zero and *dst undefined.
877 */
878static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
879 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
880 struct kvm_apic_map *map, struct kvm_lapic ***dst,
881 unsigned long *bitmap)
882{
883 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200884
885 if (irq->shorthand == APIC_DEST_SELF && src) {
886 *dst = src;
887 *bitmap = 1;
888 return true;
889 } else if (irq->shorthand)
890 return false;
891
Radim Krčmářc5192652016-07-12 22:09:28 +0200892 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200893 return false;
894
895 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200896 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200897 *bitmap = 0;
898 } else {
899 *dst = &map->phys_map[irq->dest_id];
900 *bitmap = 1;
901 }
902 return true;
903 }
904
Radim Krčmáře45115b2016-07-12 22:09:19 +0200905 *bitmap = 0;
906 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
907 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200908 return false;
909
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200910 if (!kvm_lowest_prio_delivery(irq))
911 return true;
912
913 if (!kvm_vector_hashing_enabled()) {
914 lowest = -1;
915 for_each_set_bit(i, bitmap, 16) {
916 if (!(*dst)[i])
917 continue;
918 if (lowest < 0)
919 lowest = i;
920 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
921 (*dst)[lowest]->vcpu) < 0)
922 lowest = i;
923 }
924 } else {
925 if (!*bitmap)
926 return true;
927
928 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
929 bitmap, 16);
930
931 if (!(*dst)[lowest]) {
932 kvm_apic_disabled_lapic_found(kvm);
933 *bitmap = 0;
934 return true;
935 }
936 }
937
938 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
939
940 return true;
941}
942
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300943bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100944 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300945{
946 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200947 unsigned long bitmap;
948 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300949 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200950 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300951
952 *r = -1;
953
954 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800955 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300956 return true;
957 }
958
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300959 rcu_read_lock();
960 map = rcu_dereference(kvm->arch.apic_map);
961
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200962 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200963 if (ret) {
964 *r = 0;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200965 for_each_set_bit(i, &bitmap, 16) {
966 if (!dst[i])
967 continue;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200968 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100969 }
Paolo Bonzini0624fca2018-10-01 16:07:18 +0200970 }
Radim Krčmář3548a252015-02-12 19:41:33 +0100971
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300972 rcu_read_unlock();
973 return ret;
974}
975
Feng Wu6228a0d2016-01-25 16:53:34 +0800976/*
977 * This routine tries to handler interrupts in posted mode, here is how
978 * it deals with different cases:
979 * - For single-destination interrupts, handle it in posted mode
980 * - Else if vector hashing is enabled and it is a lowest-priority
981 * interrupt, handle it in posted mode and use the following mechanism
982 * to find the destinaiton vCPU.
983 * 1. For lowest-priority interrupts, store all the possible
984 * destination vCPUs in an array.
985 * 2. Use "guest vector % max number of destination vCPUs" to find
986 * the right destination vCPU in the array for the lowest-priority
987 * interrupt.
988 * - Otherwise, use remapped mode to inject the interrupt.
989 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800990bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
991 struct kvm_vcpu **dest_vcpu)
992{
993 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200994 unsigned long bitmap;
995 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800996 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800997
998 if (irq->shorthand)
999 return false;
1000
1001 rcu_read_lock();
1002 map = rcu_dereference(kvm->arch.apic_map);
1003
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001004 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1005 hweight16(bitmap) == 1) {
1006 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +08001007
Radim Krčmář64aa47b2016-07-12 22:09:18 +02001008 if (dst[i]) {
1009 *dest_vcpu = dst[i]->vcpu;
1010 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +08001011 }
Feng Wu8feb4a02015-09-18 22:29:47 +08001012 }
1013
Feng Wu8feb4a02015-09-18 22:29:47 +08001014 rcu_read_unlock();
1015 return ret;
1016}
1017
Eddie Dong97222cc2007-09-12 10:58:04 +03001018/*
1019 * Add a pending IRQ into lapic.
1020 * Return 1 if successfully added and 0 if discarded.
1021 */
1022static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +08001023 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001024 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +03001025{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001026 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +03001027 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +03001028
Paolo Bonzinia183b632014-09-11 11:51:02 +02001029 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1030 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +03001031 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001032 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +02001033 vcpu->arch.apic_arb_prio++;
1034 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001035 if (unlikely(trig_mode && !level))
1036 break;
1037
Eddie Dong97222cc2007-09-12 10:58:04 +03001038 /* FIXME add logic for vcpu on reset */
1039 if (unlikely(!apic_enabled(apic)))
1040 break;
1041
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001042 result = 1;
1043
Joerg Roedel9daa5002016-02-29 16:04:44 +01001044 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +01001045 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +01001046 dest_map->vectors[vcpu->vcpu_id] = vector;
1047 }
Avi Kivitya5d36f82009-12-29 12:42:16 +02001048
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001049 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1050 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001051 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +02001052 else
1053 apic_clear_vector(vector, apic->regs + APIC_TMR);
1054 }
1055
Andrey Smetanind62caab2015-11-10 15:36:33 +03001056 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +08001057 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +02001058 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001059 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +08001060
1061 kvm_make_request(KVM_REQ_EVENT, vcpu);
1062 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001063 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001064 break;
1065
1066 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +05301067 result = 1;
1068 vcpu->arch.pv.pv_unhalted = 1;
1069 kvm_make_request(KVM_REQ_EVENT, vcpu);
1070 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001071 break;
1072
1073 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +02001074 result = 1;
1075 kvm_make_request(KVM_REQ_SMI, vcpu);
1076 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001077 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001078
Eddie Dong97222cc2007-09-12 10:58:04 +03001079 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001080 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001081 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001082 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001083 break;
1084
1085 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001086 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001087 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001088 /* assumes that there are only KVM_APIC_INIT/SIPI */
1089 apic->pending_events = (1UL << KVM_APIC_INIT);
1090 /* make sure pending_events is visible before sending
1091 * the request */
1092 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +03001093 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001094 kvm_vcpu_kick(vcpu);
1095 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001096 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
1097 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +03001098 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001099 break;
1100
1101 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001102 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
1103 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +01001104 result = 1;
1105 apic->sipi_vector = vector;
1106 /* make sure sipi_vector is visible for the receiver */
1107 smp_wmb();
1108 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1109 kvm_make_request(KVM_REQ_EVENT, vcpu);
1110 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001111 break;
1112
Jan Kiszka23930f92008-09-26 09:30:52 +02001113 case APIC_DM_EXTINT:
1114 /*
1115 * Should only be called by kvm_apic_local_deliver() with LVT0,
1116 * before NMI watchdog was enabled. Already handled by
1117 * kvm_apic_accept_pic_intr().
1118 */
1119 break;
1120
Eddie Dong97222cc2007-09-12 10:58:04 +03001121 default:
1122 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1123 delivery_mode);
1124 break;
1125 }
1126 return result;
1127}
1128
Gleb Natapove1035712009-03-05 16:34:59 +02001129int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001130{
Gleb Natapove1035712009-03-05 16:34:59 +02001131 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001132}
1133
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001134static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1135{
Andrey Smetanin63086302015-11-10 15:36:32 +03001136 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001137}
1138
Yang Zhangc7c9c562013-01-25 10:18:51 +08001139static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1140{
Steve Rutherford7543a632015-07-29 23:21:41 -07001141 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001142
Steve Rutherford7543a632015-07-29 23:21:41 -07001143 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1144 if (!kvm_ioapic_handles_vector(apic, vector))
1145 return;
1146
1147 /* Request a KVM exit to inform the userspace IOAPIC. */
1148 if (irqchip_split(apic->vcpu->kvm)) {
1149 apic->vcpu->arch.pending_ioapic_eoi = vector;
1150 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1151 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001152 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001153
1154 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1155 trigger_mode = IOAPIC_LEVEL_TRIG;
1156 else
1157 trigger_mode = IOAPIC_EDGE_TRIG;
1158
1159 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001160}
1161
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001162static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001163{
1164 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001165
1166 trace_kvm_eoi(apic, vector);
1167
Eddie Dong97222cc2007-09-12 10:58:04 +03001168 /*
1169 * Not every write EOI will has corresponding ISR,
1170 * one example is when Kernel check timer on setup_IO_APIC
1171 */
1172 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001173 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001174
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001175 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001176 apic_update_ppr(apic);
1177
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001178 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1179 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1180
Yang Zhangc7c9c562013-01-25 10:18:51 +08001181 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001182 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001183 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001184}
1185
Yang Zhangc7c9c562013-01-25 10:18:51 +08001186/*
1187 * this interface assumes a trap-like exit, which has already finished
1188 * desired side effect including vISR and vPPR update.
1189 */
1190void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1191{
1192 struct kvm_lapic *apic = vcpu->arch.apic;
1193
1194 trace_kvm_eoi(apic, vector);
1195
1196 kvm_ioapic_send_eoi(apic, vector);
1197 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1198}
1199EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1200
Eddie Dong97222cc2007-09-12 10:58:04 +03001201static void apic_send_ipi(struct kvm_lapic *apic)
1202{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001203 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1204 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001205 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001206
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001207 irq.vector = icr_low & APIC_VECTOR_MASK;
1208 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1209 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001210 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001211 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1212 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001213 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001214 if (apic_x2apic_mode(apic))
1215 irq.dest_id = icr_high;
1216 else
1217 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001218
Gleb Natapov1000ff82009-07-07 16:00:57 +03001219 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1220
Eddie Dong97222cc2007-09-12 10:58:04 +03001221 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1222 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001223 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1224 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001225 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001226 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001227 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001228
Yang Zhangb4f22252013-04-11 19:21:37 +08001229 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001230}
1231
1232static u32 apic_get_tmcct(struct kvm_lapic *apic)
1233{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001234 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001235 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001236 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001237
1238 ASSERT(apic != NULL);
1239
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001240 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001241 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001242 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001243 return 0;
1244
Paolo Bonzini55878592016-10-25 15:23:49 +02001245 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001246 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001247 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001248 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001249
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001250 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1251 tmcct = div64_u64(ns,
1252 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001253
1254 return tmcct;
1255}
1256
Avi Kivityb209749f2007-10-22 16:50:39 +02001257static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1258{
1259 struct kvm_vcpu *vcpu = apic->vcpu;
1260 struct kvm_run *run = vcpu->run;
1261
Avi Kivitya8eeb042010-05-10 12:34:53 +03001262 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001263 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001264 run->tpr_access.is_write = write;
1265}
1266
1267static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1268{
1269 if (apic->vcpu->arch.tpr_access_reporting)
1270 __report_tpr_access(apic, write);
1271}
1272
Eddie Dong97222cc2007-09-12 10:58:04 +03001273static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1274{
1275 u32 val = 0;
1276
1277 if (offset >= LAPIC_MMIO_LENGTH)
1278 return 0;
1279
1280 switch (offset) {
1281 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001282 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001283 break;
1284
1285 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001286 if (apic_lvtt_tscdeadline(apic))
1287 return 0;
1288
Eddie Dong97222cc2007-09-12 10:58:04 +03001289 val = apic_get_tmcct(apic);
1290 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001291 case APIC_PROCPRI:
1292 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001293 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001294 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001295 case APIC_TASKPRI:
1296 report_tpr_access(apic, false);
1297 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001298 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001299 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001300 break;
1301 }
1302
1303 return val;
1304}
1305
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001306static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1307{
1308 return container_of(dev, struct kvm_lapic, dev);
1309}
1310
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001311int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001312 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001313{
Eddie Dong97222cc2007-09-12 10:58:04 +03001314 unsigned char alignment = offset & 0xf;
1315 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001316 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001317 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001318
1319 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001320 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1321 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001322 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001323 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001324
1325 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001326 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1327 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001328 return 1;
1329 }
1330
Eddie Dong97222cc2007-09-12 10:58:04 +03001331 result = __apic_read(apic, offset & ~0xf);
1332
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001333 trace_kvm_apic_read(offset, result);
1334
Eddie Dong97222cc2007-09-12 10:58:04 +03001335 switch (len) {
1336 case 1:
1337 case 2:
1338 case 4:
1339 memcpy(data, (char *)&result + alignment, len);
1340 break;
1341 default:
1342 printk(KERN_ERR "Local APIC read with len = %x, "
1343 "should be 1,2, or 4 instead\n", len);
1344 break;
1345 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001346 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001347}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001348EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001349
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001350static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1351{
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001352 return addr >= apic->base_address &&
1353 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001354}
1355
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001356static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001357 gpa_t address, int len, void *data)
1358{
1359 struct kvm_lapic *apic = to_lapic(this);
1360 u32 offset = address - apic->base_address;
1361
1362 if (!apic_mmio_in_range(apic, address))
1363 return -EOPNOTSUPP;
1364
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001365 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1366 if (!kvm_check_has_quirk(vcpu->kvm,
1367 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1368 return -EOPNOTSUPP;
1369
1370 memset(data, 0xff, len);
1371 return 0;
1372 }
1373
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001374 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001375
1376 return 0;
1377}
1378
Eddie Dong97222cc2007-09-12 10:58:04 +03001379static void update_divide_count(struct kvm_lapic *apic)
1380{
1381 u32 tmp1, tmp2, tdcr;
1382
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001383 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001384 tmp1 = tdcr & 0xf;
1385 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001386 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001387
1388 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001389 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001390}
1391
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001392static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
1393{
1394 /*
1395 * Do not allow the guest to program periodic timers with small
1396 * interval, since the hrtimers are not throttled by the host
1397 * scheduler.
1398 */
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001399 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001400 s64 min_period = min_timer_period_us * 1000LL;
1401
1402 if (apic->lapic_timer.period < min_period) {
1403 pr_info_ratelimited(
1404 "kvm: vcpu %i: requested %lld ns "
1405 "lapic timer period limited to %lld ns\n",
1406 apic->vcpu->vcpu_id,
1407 apic->lapic_timer.period, min_period);
1408 apic->lapic_timer.period = min_period;
1409 }
1410 }
1411}
1412
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001413static void apic_update_lvtt(struct kvm_lapic *apic)
1414{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001415 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001416 apic->lapic_timer.timer_mode_mask;
1417
1418 if (apic->lapic_timer.timer_mode != timer_mode) {
Wanpeng Lic69518c2017-10-05 03:53:51 -07001419 if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001420 APIC_LVT_TIMER_TSCDEADLINE)) {
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001421 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmář44275932017-10-06 19:25:55 +02001422 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1423 apic->lapic_timer.period = 0;
1424 apic->lapic_timer.tscdeadline = 0;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001425 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001426 apic->lapic_timer.timer_mode = timer_mode;
Wanpeng Lidedf9c52017-10-05 18:54:25 -07001427 limit_periodic_timer_frequency(apic);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001428 }
1429}
1430
Radim Krčmář5d87db72014-10-10 19:15:08 +02001431static void apic_timer_expired(struct kvm_lapic *apic)
1432{
1433 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001434 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001435 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001436
Radim Krčmář5d87db72014-10-10 19:15:08 +02001437 if (atomic_read(&apic->lapic_timer.pending))
1438 return;
1439
1440 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001441 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001442
Davidlohr Buesocc1b4682017-09-13 13:08:20 -07001443 /*
1444 * For x86, the atomic_inc() is serialized, thus
1445 * using swait_active() is safe.
1446 */
Marcelo Tosatti85773702016-02-19 09:46:39 +01001447 if (swait_active(q))
Peter Zijlstrab3dae102018-06-12 10:34:52 +02001448 swake_up_one(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001449
1450 if (apic_lvtt_tscdeadline(apic))
1451 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1452}
1453
1454/*
1455 * On APICv, this test will cause a busy wait
1456 * during a higher-priority task.
1457 */
1458
1459static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1460{
1461 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001462 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001463
1464 if (kvm_apic_hw_enabled(apic)) {
1465 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001466 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001467
Andrey Smetanind62caab2015-11-10 15:36:33 +03001468 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001469 bitmap = apic->regs + APIC_IRR;
1470
1471 if (apic_test_vector(vec, bitmap))
1472 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001473 }
1474 return false;
1475}
1476
1477void wait_lapic_expire(struct kvm_vcpu *vcpu)
1478{
1479 struct kvm_lapic *apic = vcpu->arch.apic;
Wanpeng Li3b8a5df2018-10-09 09:02:08 +08001480 u64 guest_tsc, tsc_deadline, ns;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001481
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001482 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001483 return;
1484
1485 if (apic->lapic_timer.expired_tscdeadline == 0)
1486 return;
1487
1488 if (!lapic_timer_int_injected(vcpu))
1489 return;
1490
1491 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1492 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001493 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001494 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001495
1496 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1497 if (guest_tsc < tsc_deadline)
Marcelo Tosattib606f182016-06-20 22:33:48 -03001498 __delay(min(tsc_deadline - guest_tsc,
1499 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
Wanpeng Li3b8a5df2018-10-09 09:02:08 +08001500
1501 if (!lapic_timer_advance_adjust_done) {
1502 /* too early */
1503 if (guest_tsc < tsc_deadline) {
1504 ns = (tsc_deadline - guest_tsc) * 1000000ULL;
1505 do_div(ns, vcpu->arch.virtual_tsc_khz);
1506 lapic_timer_advance_ns -= min((unsigned int)ns,
1507 lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1508 } else {
1509 /* too late */
1510 ns = (guest_tsc - tsc_deadline) * 1000000ULL;
1511 do_div(ns, vcpu->arch.virtual_tsc_khz);
1512 lapic_timer_advance_ns += min((unsigned int)ns,
1513 lapic_timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP);
1514 }
1515 if (abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
1516 lapic_timer_advance_adjust_done = true;
1517 }
Radim Krčmář5d87db72014-10-10 19:15:08 +02001518}
1519
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001520static void start_sw_tscdeadline(struct kvm_lapic *apic)
1521{
1522 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1523 u64 ns = 0;
1524 ktime_t expire;
1525 struct kvm_vcpu *vcpu = apic->vcpu;
1526 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1527 unsigned long flags;
1528 ktime_t now;
1529
1530 if (unlikely(!tscdeadline || !this_tsc_khz))
1531 return;
1532
1533 local_irq_save(flags);
1534
Paolo Bonzini55878592016-10-25 15:23:49 +02001535 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001536 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1537 if (likely(tscdeadline > guest_tsc)) {
1538 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1539 do_div(ns, this_tsc_khz);
1540 expire = ktime_add_ns(now, ns);
1541 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1542 hrtimer_start(&apic->lapic_timer.timer,
1543 expire, HRTIMER_MODE_ABS_PINNED);
1544 } else
1545 apic_timer_expired(apic);
1546
1547 local_irq_restore(flags);
1548}
1549
Wanpeng Lic301b902017-10-06 07:38:32 -07001550static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
1551{
1552 ktime_t now, remaining;
1553 u64 ns_remaining_old, ns_remaining_new;
1554
1555 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1556 * APIC_BUS_CYCLE_NS * apic->divide_count;
1557 limit_periodic_timer_frequency(apic);
1558
1559 now = ktime_get();
1560 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1561 if (ktime_to_ns(remaining) < 0)
1562 remaining = 0;
1563
1564 ns_remaining_old = ktime_to_ns(remaining);
1565 ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1566 apic->divide_count, old_divisor);
1567
1568 apic->lapic_timer.tscdeadline +=
1569 nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1570 nsec_to_cycles(apic->vcpu, ns_remaining_old);
1571 apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1572}
1573
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001574static bool set_target_expiration(struct kvm_lapic *apic)
1575{
1576 ktime_t now;
1577 u64 tscl = rdtsc();
1578
Paolo Bonzini55878592016-10-25 15:23:49 +02001579 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001580 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1581 * APIC_BUS_CYCLE_NS * apic->divide_count;
1582
Radim Krčmář5d74a692017-10-06 19:25:54 +02001583 if (!apic->lapic_timer.period) {
1584 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001585 return false;
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001586 }
1587
Wanpeng Liccbfa1d2017-10-05 18:54:24 -07001588 limit_periodic_timer_frequency(apic);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001589
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001590 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1591 PRIx64 ", "
1592 "timer initial count 0x%x, period %lldns, "
1593 "expire @ 0x%016" PRIx64 ".\n", __func__,
1594 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1595 kvm_lapic_get_reg(apic, APIC_TMICT),
1596 apic->lapic_timer.period,
1597 ktime_to_ns(ktime_add_ns(now,
1598 apic->lapic_timer.period)));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001599
1600 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1601 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1602 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1603
1604 return true;
1605}
1606
1607static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1608{
David Vrabeld8f2f492018-05-18 16:55:46 +01001609 ktime_t now = ktime_get();
1610 u64 tscl = rdtsc();
1611 ktime_t delta;
1612
1613 /*
1614 * Synchronize both deadlines to the same time source or
1615 * differences in the periods (caused by differences in the
1616 * underlying clocks or numerical approximation errors) will
1617 * cause the two to drift apart over time as the errors
1618 * accumulate.
1619 */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001620 apic->lapic_timer.target_expiration =
1621 ktime_add_ns(apic->lapic_timer.target_expiration,
1622 apic->lapic_timer.period);
David Vrabeld8f2f492018-05-18 16:55:46 +01001623 delta = ktime_sub(apic->lapic_timer.target_expiration, now);
1624 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1625 nsec_to_cycles(apic->vcpu, delta);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001626}
1627
Anthoine Bourgeoisecf08da2018-04-29 22:05:58 +00001628static void start_sw_period(struct kvm_lapic *apic)
1629{
1630 if (!apic->lapic_timer.period)
1631 return;
1632
1633 if (ktime_after(ktime_get(),
1634 apic->lapic_timer.target_expiration)) {
1635 apic_timer_expired(apic);
1636
1637 if (apic_lvtt_oneshot(apic))
1638 return;
1639
1640 advance_periodic_target_expiration(apic);
1641 }
1642
1643 hrtimer_start(&apic->lapic_timer.timer,
1644 apic->lapic_timer.target_expiration,
1645 HRTIMER_MODE_ABS_PINNED);
1646}
1647
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001648bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1649{
Wanpeng Li91005302016-08-03 12:04:12 +08001650 if (!lapic_in_kernel(vcpu))
1651 return false;
1652
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001653 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1654}
1655EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1656
Wanpeng Li7e810a32016-10-24 18:23:12 +08001657static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001658{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001659 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001660 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
Wanpeng Libd97ad02016-06-30 08:52:49 +08001661 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1662 apic->lapic_timer.hv_timer_in_use = false;
1663}
1664
Paolo Bonzinia749e242017-06-29 17:14:50 +02001665static bool start_hv_timer(struct kvm_lapic *apic)
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001666{
1667 struct kvm_timer *ktimer = &apic->lapic_timer;
1668 int r;
1669
Wanpeng Li1d518c62017-07-25 00:43:15 -07001670 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001671 if (!kvm_x86_ops->set_hv_timer)
1672 return false;
1673
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001674 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1675 return false;
1676
Radim Krčmář86bbc1e2017-10-06 19:25:53 +02001677 if (!ktimer->tscdeadline)
1678 return false;
1679
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001680 r = kvm_x86_ops->set_hv_timer(apic->vcpu, ktimer->tscdeadline);
1681 if (r < 0)
1682 return false;
1683
1684 ktimer->hv_timer_in_use = true;
1685 hrtimer_cancel(&ktimer->timer);
1686
1687 /*
1688 * Also recheck ktimer->pending, in case the sw timer triggered in
1689 * the window. For periodic timer, leave the hv timer running for
1690 * simplicity, and the deadline will be recomputed on the next vmexit.
1691 */
Wanpeng Lic8533542017-06-29 06:28:09 -07001692 if (!apic_lvtt_period(apic) && (r || atomic_read(&ktimer->pending))) {
1693 if (r)
1694 apic_timer_expired(apic);
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001695 return false;
Wanpeng Lic8533542017-06-29 06:28:09 -07001696 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02001697
1698 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true);
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001699 return true;
1700}
1701
Paolo Bonzinia749e242017-06-29 17:14:50 +02001702static void start_sw_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001703{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001704 struct kvm_timer *ktimer = &apic->lapic_timer;
Wanpeng Li1d518c62017-07-25 00:43:15 -07001705
1706 WARN_ON(preemptible());
Paolo Bonzinia749e242017-06-29 17:14:50 +02001707 if (apic->lapic_timer.hv_timer_in_use)
1708 cancel_hv_timer(apic);
1709 if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
1710 return;
Paolo Bonzini35ee9e42017-06-29 17:14:50 +02001711
Paolo Bonzinia749e242017-06-29 17:14:50 +02001712 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1713 start_sw_period(apic);
1714 else if (apic_lvtt_tscdeadline(apic))
1715 start_sw_tscdeadline(apic);
1716 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
1717}
1718
1719static void restart_apic_timer(struct kvm_lapic *apic)
1720{
Wanpeng Li1d518c62017-07-25 00:43:15 -07001721 preempt_disable();
Paolo Bonzinia749e242017-06-29 17:14:50 +02001722 if (!start_hv_timer(apic))
1723 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07001724 preempt_enable();
Wanpeng Li196f20c2016-06-28 14:54:19 +08001725}
1726
Eddie Dong97222cc2007-09-12 10:58:04 +03001727void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1728{
1729 struct kvm_lapic *apic = vcpu->arch.apic;
1730
Wanpeng Li1d518c62017-07-25 00:43:15 -07001731 preempt_disable();
1732 /* If the preempt notifier has already run, it also called apic_timer_expired */
1733 if (!apic->lapic_timer.hv_timer_in_use)
1734 goto out;
Eddie Dong97222cc2007-09-12 10:58:04 +03001735 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001736 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001737 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001738
1739 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1740 advance_periodic_target_expiration(apic);
Paolo Bonzinia749e242017-06-29 17:14:50 +02001741 restart_apic_timer(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001742 }
Wanpeng Li1d518c62017-07-25 00:43:15 -07001743out:
1744 preempt_enable();
Eddie Dong97222cc2007-09-12 10:58:04 +03001745}
1746EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1747
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001748void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1749{
Paolo Bonzinia749e242017-06-29 17:14:50 +02001750 restart_apic_timer(vcpu->arch.apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001751}
1752EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1753
1754void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1755{
1756 struct kvm_lapic *apic = vcpu->arch.apic;
1757
Wanpeng Li1d518c62017-07-25 00:43:15 -07001758 preempt_disable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001759 /* Possibly the TSC deadline timer is not enabled yet */
Paolo Bonzinia749e242017-06-29 17:14:50 +02001760 if (apic->lapic_timer.hv_timer_in_use)
1761 start_sw_timer(apic);
Wanpeng Li1d518c62017-07-25 00:43:15 -07001762 preempt_enable();
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001763}
1764EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1765
Paolo Bonzinia749e242017-06-29 17:14:50 +02001766void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
1767{
1768 struct kvm_lapic *apic = vcpu->arch.apic;
1769
1770 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1771 restart_apic_timer(apic);
1772}
1773
Eddie Dong97222cc2007-09-12 10:58:04 +03001774static void start_apic_timer(struct kvm_lapic *apic)
1775{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001776 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001777
Paolo Bonzinia749e242017-06-29 17:14:50 +02001778 if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1779 && !set_target_expiration(apic))
1780 return;
1781
1782 restart_apic_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001783}
1784
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001785static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1786{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001787 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001788
Radim Krčmář59fd1322015-06-30 22:19:16 +02001789 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1790 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1791 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001792 apic_debug("Receive NMI setting on APIC_LVT0 "
1793 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001794 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001795 } else
1796 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1797 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001798}
1799
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001800int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001801{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001802 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001803
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001804 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001805
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001806 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001807 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001808 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001809 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001810 else
1811 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001812 break;
1813
1814 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001815 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001816 apic_set_tpr(apic, val & 0xff);
1817 break;
1818
1819 case APIC_EOI:
1820 apic_set_eoi(apic);
1821 break;
1822
1823 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001824 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001825 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001826 else
1827 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001828 break;
1829
1830 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001831 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001832 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001833 recalculate_apic_map(apic->vcpu->kvm);
1834 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001835 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001836 break;
1837
Gleb Natapovfc61b802009-07-05 17:39:35 +03001838 case APIC_SPIV: {
1839 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001840 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001841 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001842 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001843 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1844 int i;
1845 u32 lvt_val;
1846
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001847 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001848 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001849 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001850 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001851 lvt_val | APIC_LVT_MASKED);
1852 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001853 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001854 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001855
1856 }
1857 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001858 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001859 case APIC_ICR:
1860 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001861 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001862 apic_send_ipi(apic);
1863 break;
1864
1865 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001866 if (!apic_x2apic_mode(apic))
1867 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001868 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001869 break;
1870
Jan Kiszka23930f92008-09-26 09:30:52 +02001871 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001872 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001873 case APIC_LVTTHMR:
1874 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001875 case APIC_LVT1:
1876 case APIC_LVTERR:
1877 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001878 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001879 val |= APIC_LVT_MASKED;
1880
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001881 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001882 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001883
1884 break;
1885
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001886 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001887 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001888 val |= APIC_LVT_MASKED;
1889 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001890 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001891 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001892 break;
1893
Eddie Dong97222cc2007-09-12 10:58:04 +03001894 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001895 if (apic_lvtt_tscdeadline(apic))
1896 break;
1897
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001898 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001899 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001900 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001901 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001902
Wanpeng Lic301b902017-10-06 07:38:32 -07001903 case APIC_TDCR: {
1904 uint32_t old_divisor = apic->divide_count;
1905
Eddie Dong97222cc2007-09-12 10:58:04 +03001906 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001907 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001908 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001909 update_divide_count(apic);
Wanpeng Lic301b902017-10-06 07:38:32 -07001910 if (apic->divide_count != old_divisor &&
1911 apic->lapic_timer.period) {
1912 hrtimer_cancel(&apic->lapic_timer.timer);
1913 update_target_expiration(apic, old_divisor);
1914 restart_apic_timer(apic);
1915 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001916 break;
Wanpeng Lic301b902017-10-06 07:38:32 -07001917 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001918 case APIC_ESR:
1919 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001920 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001921 ret = 1;
1922 }
1923 break;
1924
1925 case APIC_SELF_IPI:
1926 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001927 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001928 } else
1929 ret = 1;
1930 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001931 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001932 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001933 break;
1934 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001935 if (ret)
1936 apic_debug("Local APIC Write to read-only register %x\n", reg);
1937 return ret;
1938}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001939EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001940
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001941static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001942 gpa_t address, int len, const void *data)
1943{
1944 struct kvm_lapic *apic = to_lapic(this);
1945 unsigned int offset = address - apic->base_address;
1946 u32 val;
1947
1948 if (!apic_mmio_in_range(apic, address))
1949 return -EOPNOTSUPP;
1950
Vitaly Kuznetsovd1766202018-08-02 17:08:16 +02001951 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1952 if (!kvm_check_has_quirk(vcpu->kvm,
1953 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1954 return -EOPNOTSUPP;
1955
1956 return 0;
1957 }
1958
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001959 /*
1960 * APIC register must be aligned on 128-bits boundary.
1961 * 32/64/128 bits registers must be accessed thru 32 bits.
1962 * Refer SDM 8.4.1
1963 */
1964 if (len != 4 || (offset & 0xf)) {
1965 /* Don't shout loud, $infamous_os would cause only noise. */
1966 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001967 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001968 }
1969
1970 val = *(u32*)data;
1971
1972 /* too common printing */
1973 if (offset != APIC_EOI)
1974 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1975 "0x%x\n", __func__, offset, len, val);
1976
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001977 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001978
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001979 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001980}
1981
Kevin Tian58fbbf22011-08-30 13:56:17 +03001982void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1983{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001984 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001985}
1986EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1987
Yang Zhang83d4c282013-01-25 10:18:49 +08001988/* emulate APIC access in a trap manner */
1989void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1990{
1991 u32 val = 0;
1992
1993 /* hw has done the conditional check and inst decode */
1994 offset &= 0xff0;
1995
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001996 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001997
1998 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001999 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08002000}
2001EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
2002
Rusty Russelld5894442007-10-08 10:48:30 +10002003void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03002004{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002005 struct kvm_lapic *apic = vcpu->arch.apic;
2006
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002007 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002008 return;
2009
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002010 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002011
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002012 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2013 static_key_slow_dec_deferred(&apic_hw_disabled);
2014
Radim Krčmáře4627552014-10-30 15:06:45 +01002015 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002016 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03002017
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002018 if (apic->regs)
2019 free_page((unsigned long)apic->regs);
2020
2021 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002022}
2023
2024/*
2025 *----------------------------------------------------------------------
2026 * LAPIC interface
2027 *----------------------------------------------------------------------
2028 */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002029u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
2030{
2031 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002032
Wanpeng Lia10388e2016-10-24 18:23:10 +08002033 if (!lapic_in_kernel(vcpu) ||
2034 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002035 return 0;
2036
2037 return apic->lapic_timer.tscdeadline;
2038}
2039
2040void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
2041{
2042 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002043
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002044 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03002045 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002046 return;
2047
2048 hrtimer_cancel(&apic->lapic_timer.timer);
2049 apic->lapic_timer.tscdeadline = data;
2050 start_apic_timer(apic);
2051}
2052
Eddie Dong97222cc2007-09-12 10:58:04 +03002053void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
2054{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002055 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002056
Avi Kivityb93463a2007-10-25 16:52:32 +02002057 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002058 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03002059}
2060
2061u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
2062{
Eddie Dong97222cc2007-09-12 10:58:04 +03002063 u64 tpr;
2064
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002065 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03002066
2067 return (tpr & 0xf0) >> 4;
2068}
2069
2070void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
2071{
Yang Zhang8d146952013-01-25 10:18:50 +08002072 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002073 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002074
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002075 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002076 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002077
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01002078 vcpu->arch.apic_base = value;
2079
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08002080 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2081 kvm_update_cpuid(vcpu);
2082
2083 if (!apic)
2084 return;
2085
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002086 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01002087 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02002088 if (value & MSR_IA32_APICBASE_ENABLE) {
2089 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002090 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08002091 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002092 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08002093 recalculate_apic_map(vcpu->kvm);
2094 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002095 }
2096
Jim Mattson8d860bb2018-05-09 16:56:05 -04002097 if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
2098 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2099
2100 if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE))
2101 kvm_x86_ops->set_virtual_apic_mode(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08002102
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002103 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03002104 MSR_IA32_APICBASE_BASE;
2105
Nadav Amitdb324fe2014-11-02 11:54:59 +02002106 if ((value & MSR_IA32_APICBASE_ENABLE) &&
2107 apic->base_address != APIC_DEFAULT_PHYS_BASE)
2108 pr_warn_once("APIC base relocation is unsupported by KVM");
2109
Eddie Dong97222cc2007-09-12 10:58:04 +03002110 /* with FSB delivery interrupt, we can restart APIC functionality */
2111 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03002113
2114}
2115
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002116void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03002117{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002118 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002119 int i;
2120
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002121 if (!apic)
2122 return;
Eddie Dong97222cc2007-09-12 10:58:04 +03002123
Radim Krčmářb7e31be2018-03-01 15:24:25 +01002124 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03002125
2126 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002127 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03002128
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002129 if (!init_event) {
2130 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
2131 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002132 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02002133 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03002134 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03002135
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002136 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
2137 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002138 apic_update_lvtt(apic);
Jan H. Schönherr52b54192017-05-20 13:24:32 +02002139 if (kvm_vcpu_is_reset_bsp(vcpu) &&
2140 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002141 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03002142 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002143 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03002144
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002145 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002146 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002147 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02002148 if (!apic_x2apic_mode(apic))
2149 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002150 kvm_lapic_set_reg(apic, APIC_ESR, 0);
2151 kvm_lapic_set_reg(apic, APIC_ICR, 0);
2152 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2153 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2154 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002155 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002156 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2157 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2158 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03002159 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03002160 apic->irr_pending = vcpu->arch.apicv_active;
2161 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002162 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02002163 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002164 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002165 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002166 kvm_lapic_set_base(vcpu,
2167 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002168 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03002169 apic_update_ppr(apic);
Jan H. Schönherr4191db22017-10-25 16:43:27 +02002170 if (vcpu->arch.apicv_active) {
2171 kvm_x86_ops->apicv_post_state_restore(vcpu);
2172 kvm_x86_ops->hwapic_irr_update(vcpu, -1);
2173 kvm_x86_ops->hwapic_isr_update(vcpu, -1);
2174 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002175
Gleb Natapove1035712009-03-05 16:34:59 +02002176 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03002177 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02002178
Radim Krčmář6e500432016-12-15 18:06:46 +01002179 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002180 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Radim Krčmář6e500432016-12-15 18:06:46 +01002181 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002182 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03002183}
2184
Eddie Dong97222cc2007-09-12 10:58:04 +03002185/*
2186 *----------------------------------------------------------------------
2187 * timer interface
2188 *----------------------------------------------------------------------
2189 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03002190
Avi Kivity2a6eac92012-07-26 18:01:51 +03002191static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03002192{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002193 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002194}
2195
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002196int apic_has_pending_timer(struct kvm_vcpu *vcpu)
2197{
Gleb Natapov54e98182012-08-05 15:58:32 +03002198 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002199
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01002200 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03002201 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03002202
2203 return 0;
2204}
2205
Avi Kivity89342082011-11-10 14:57:21 +02002206int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002207{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002208 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002209 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002210
Gleb Natapovc48f1492012-08-05 15:58:33 +03002211 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002212 vector = reg & APIC_VECTOR_MASK;
2213 mode = reg & APIC_MODE_MASK;
2214 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08002215 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2216 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02002217 }
2218 return 0;
2219}
2220
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002221void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002222{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002223 struct kvm_lapic *apic = vcpu->arch.apic;
2224
2225 if (apic)
2226 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002227}
2228
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002229static const struct kvm_io_device_ops apic_mmio_ops = {
2230 .read = apic_mmio_read,
2231 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002232};
2233
Avi Kivitye9d90d42012-07-26 18:01:50 +03002234static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2235{
2236 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002237 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002238
Radim Krčmář5d87db72014-10-10 19:15:08 +02002239 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002240
Avi Kivity2a6eac92012-07-26 18:01:51 +03002241 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002242 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002243 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2244 return HRTIMER_RESTART;
2245 } else
2246 return HRTIMER_NORESTART;
2247}
2248
Eddie Dong97222cc2007-09-12 10:58:04 +03002249int kvm_create_lapic(struct kvm_vcpu *vcpu)
2250{
2251 struct kvm_lapic *apic;
2252
2253 ASSERT(vcpu != NULL);
2254 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2255
2256 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
2257 if (!apic)
2258 goto nomem;
2259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002260 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002261
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002262 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
2263 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002264 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2265 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002266 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002267 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002268 apic->vcpu = vcpu;
2269
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002270 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002271 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002272 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002273
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002274 /*
2275 * APIC is created enabled. This will prevent kvm_lapic_set_base from
2276 * thinking that APIC satet has changed.
2277 */
2278 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002279 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002280 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002281
2282 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002283nomem_free_apic:
2284 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002285nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002286 return -ENOMEM;
2287}
Eddie Dong97222cc2007-09-12 10:58:04 +03002288
2289int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2290{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002291 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002292 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002293
Paolo Bonzinif8543d62016-01-08 13:42:24 +01002294 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002295 return -1;
2296
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002297 __apic_update_ppr(apic, &ppr);
2298 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002299}
2300
Qing He40487c62007-09-17 14:47:13 +08002301int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2302{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002303 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002304 int r = 0;
2305
Gleb Natapovc48f1492012-08-05 15:58:33 +03002306 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002307 r = 1;
2308 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2309 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2310 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002311 return r;
2312}
2313
Eddie Dong1b9778d2007-09-03 16:56:58 +03002314void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2315{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002316 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002317
Gleb Natapov54e98182012-08-05 15:58:32 +03002318 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002319 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002320 if (apic_lvtt_tscdeadline(apic))
2321 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002322 if (apic_lvtt_oneshot(apic)) {
2323 apic->lapic_timer.tscdeadline = 0;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002324 apic->lapic_timer.target_expiration = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002325 }
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002326 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002327 }
2328}
2329
Eddie Dong97222cc2007-09-12 10:58:04 +03002330int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2331{
2332 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002333 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002334 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002335
2336 if (vector == -1)
2337 return -1;
2338
Wanpeng Li56cc2402014-08-05 12:42:24 +08002339 /*
2340 * We get here even with APIC virtualization enabled, if doing
2341 * nested virtualization and L1 runs with the "acknowledge interrupt
2342 * on exit" mode. Then we cannot inject the interrupt via RVI,
2343 * because the process would deliver it through the IDT.
2344 */
2345
Eddie Dong97222cc2007-09-12 10:58:04 +03002346 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002347 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002348 /*
2349 * For auto-EOI interrupts, there might be another pending
2350 * interrupt above PPR, so check whether to raise another
2351 * KVM_REQ_EVENT.
2352 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002353 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002354 } else {
2355 /*
2356 * For normal interrupts, PPR has been raised and there cannot
2357 * be a higher-priority pending interrupt---except if there was
2358 * a concurrent interrupt injection, but that would have
2359 * triggered KVM_REQ_EVENT already.
2360 */
2361 apic_set_isr(vector, apic);
2362 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002363 }
2364
Eddie Dong97222cc2007-09-12 10:58:04 +03002365 return vector;
2366}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002367
Radim Krčmářa92e2542016-07-12 22:09:22 +02002368static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2369 struct kvm_lapic_state *s, bool set)
2370{
2371 if (apic_x2apic_mode(vcpu->arch.apic)) {
2372 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002373 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002374
Radim Krčmář371313132016-07-12 22:09:27 +02002375 if (vcpu->kvm->arch.x2apic_format) {
2376 if (*id != vcpu->vcpu_id)
2377 return -EINVAL;
2378 } else {
2379 if (set)
2380 *id >>= 24;
2381 else
2382 *id <<= 24;
2383 }
Dr. David Alan Gilbert12806ba2017-11-17 11:52:50 +00002384
2385 /* In x2APIC mode, the LDR is fixed and based on the id */
2386 if (set)
2387 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002388 }
2389
2390 return 0;
2391}
2392
2393int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2394{
2395 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2396 return kvm_apic_state_fixup(vcpu, s, false);
2397}
2398
2399int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002400{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002402 int r;
2403
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002404
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002405 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002406 /* set SPIV separately to get count of SW disabled APICs right */
2407 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002408
2409 r = kvm_apic_state_fixup(vcpu, s, true);
2410 if (r)
2411 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002412 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002413
2414 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002415 kvm_apic_set_version(vcpu);
2416
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002417 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002418 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002419 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002420 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002421 update_divide_count(apic);
2422 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002423 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002424 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002425 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002426 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002427 if (vcpu->arch.apicv_active) {
Paolo Bonzini967235d2016-12-19 14:03:45 +01002428 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002429 kvm_x86_ops->hwapic_irr_update(vcpu,
2430 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002431 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002432 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002433 }
Avi Kivity3842d132010-07-27 12:30:24 +03002434 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002435 if (ioapic_in_kernel(vcpu->kvm))
2436 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002437
2438 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002439
2440 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002441}
Eddie Donga3d7f852007-09-03 16:15:12 +03002442
Avi Kivity2f52d582008-01-16 12:49:30 +02002443void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002444{
Eddie Donga3d7f852007-09-03 16:15:12 +03002445 struct hrtimer *timer;
2446
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002447 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002448 return;
2449
Gleb Natapov54e98182012-08-05 15:58:32 +03002450 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002451 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002452 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002453}
Avi Kivityb93463a2007-10-25 16:52:32 +02002454
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002455/*
2456 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2457 *
2458 * Detect whether guest triggered PV EOI since the
2459 * last entry. If yes, set EOI on guests's behalf.
2460 * Clear PV EOI in guest memory in any case.
2461 */
2462static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2463 struct kvm_lapic *apic)
2464{
2465 bool pending;
2466 int vector;
2467 /*
2468 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2469 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2470 *
2471 * KVM_APIC_PV_EOI_PENDING is unset:
2472 * -> host disabled PV EOI.
2473 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2474 * -> host enabled PV EOI, guest did not execute EOI yet.
2475 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2476 * -> host enabled PV EOI, guest executed EOI.
2477 */
2478 BUG_ON(!pv_eoi_enabled(vcpu));
2479 pending = pv_eoi_get_pending(vcpu);
2480 /*
2481 * Clear pending bit in any case: it will be set again on vmentry.
2482 * While this might not be ideal from performance point of view,
2483 * this makes sure pv eoi is only enabled when we know it's safe.
2484 */
2485 pv_eoi_clr_pending(vcpu);
2486 if (pending)
2487 return;
2488 vector = apic_set_eoi(apic);
2489 trace_kvm_pv_eoi(apic, vector);
2490}
2491
Avi Kivityb93463a2007-10-25 16:52:32 +02002492void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2493{
2494 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002495
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002496 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2497 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2498
Gleb Natapov41383772012-04-19 14:06:29 +03002499 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002500 return;
2501
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002502 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2503 sizeof(u32)))
Nicholas Krause603242a2015-08-05 10:44:40 -04002504 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002505
2506 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2507}
2508
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002509/*
2510 * apic_sync_pv_eoi_to_guest - called before vmentry
2511 *
2512 * Detect whether it's safe to enable PV EOI and
2513 * if yes do so.
2514 */
2515static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2516 struct kvm_lapic *apic)
2517{
2518 if (!pv_eoi_enabled(vcpu) ||
2519 /* IRR set or many bits in ISR: could be nested. */
2520 apic->irr_pending ||
2521 /* Cache not set: could be safe but we don't bother. */
2522 apic->highest_isr_cache == -1 ||
2523 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002524 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002525 /*
2526 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2527 * so we need not do anything here.
2528 */
2529 return;
2530 }
2531
2532 pv_eoi_set_pending(apic->vcpu);
2533}
2534
Avi Kivityb93463a2007-10-25 16:52:32 +02002535void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2536{
2537 u32 data, tpr;
2538 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002539 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002540
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002541 apic_sync_pv_eoi_to_guest(vcpu, apic);
2542
Gleb Natapov41383772012-04-19 14:06:29 +03002543 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002544 return;
2545
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002546 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002547 max_irr = apic_find_highest_irr(apic);
2548 if (max_irr < 0)
2549 max_irr = 0;
2550 max_isr = apic_find_highest_isr(apic);
2551 if (max_isr < 0)
2552 max_isr = 0;
2553 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2554
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002555 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2556 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002557}
2558
Andy Honigfda4e2e2013-11-20 10:23:22 -08002559int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002560{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002561 if (vapic_addr) {
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002562 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andy Honigfda4e2e2013-11-20 10:23:22 -08002563 &vcpu->arch.apic->vapic_cache,
2564 vapic_addr, sizeof(u32)))
2565 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002566 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002567 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002568 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002569 }
2570
2571 vcpu->arch.apic->vapic_addr = vapic_addr;
2572 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002573}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002574
2575int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2576{
2577 struct kvm_lapic *apic = vcpu->arch.apic;
2578 u32 reg = (msr - APIC_BASE_MSR) << 4;
2579
Paolo Bonzini35754c92015-07-29 12:05:37 +02002580 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002581 return 1;
2582
Nadav Amitc69d3d92014-11-26 17:56:25 +02002583 if (reg == APIC_ICR2)
2584 return 1;
2585
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002586 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002587 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002588 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2589 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002590}
2591
2592int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2593{
2594 struct kvm_lapic *apic = vcpu->arch.apic;
2595 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2596
Paolo Bonzini35754c92015-07-29 12:05:37 +02002597 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002598 return 1;
2599
Nadav Amitc69d3d92014-11-26 17:56:25 +02002600 if (reg == APIC_DFR || reg == APIC_ICR2) {
2601 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2602 reg);
2603 return 1;
2604 }
2605
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002606 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002607 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002608 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002609 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002610
2611 *data = (((u64)high) << 32) | low;
2612
2613 return 0;
2614}
Gleb Natapov10388a02010-01-17 15:51:23 +02002615
2616int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2617{
2618 struct kvm_lapic *apic = vcpu->arch.apic;
2619
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002620 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002621 return 1;
2622
2623 /* if this is ICR write vector before command */
2624 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002625 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2626 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002627}
2628
2629int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2630{
2631 struct kvm_lapic *apic = vcpu->arch.apic;
2632 u32 low, high = 0;
2633
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002634 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002635 return 1;
2636
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002637 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002638 return 1;
2639 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002640 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002641
2642 *data = (((u64)high) << 32) | low;
2643
2644 return 0;
2645}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002646
Ladi Prosek72bbf932018-10-16 18:49:59 +02002647int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002648{
2649 u64 addr = data & ~KVM_MSR_ENABLED;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002650 struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
2651 unsigned long new_len;
2652
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002653 if (!IS_ALIGNED(addr, 4))
2654 return 1;
2655
2656 vcpu->arch.pv_eoi.msr_val = data;
2657 if (!pv_eoi_enabled(vcpu))
2658 return 0;
Vitaly Kuznetsova7c42bb2018-10-16 18:50:06 +02002659
2660 if (addr == ghc->gpa && len <= ghc->len)
2661 new_len = ghc->len;
2662 else
2663 new_len = len;
2664
2665 return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002666}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002667
Jan Kiszka66450a22013-03-13 12:42:34 +01002668void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2669{
2670 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002671 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002672 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002673
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002674 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002675 return;
2676
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002677 /*
2678 * INITs are latched while in SMM. Because an SMM CPU cannot
2679 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2680 * and delay processing of INIT until the next RSM.
2681 */
2682 if (is_smm(vcpu)) {
2683 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2684 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2685 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2686 return;
2687 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002688
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002689 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002690 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002691 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002692 if (kvm_vcpu_is_bsp(apic->vcpu))
2693 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2694 else
2695 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2696 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002697 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002698 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2699 /* evaluate pending_events before reading the vector */
2700 smp_rmb();
2701 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002702 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002703 vcpu->vcpu_id, sipi_vector);
2704 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2705 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2706 }
2707}
2708
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002709void kvm_lapic_init(void)
2710{
2711 /* do not patch jump label more than once per second */
2712 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002713 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002714}
David Matlackcef84c32016-12-16 14:30:36 -08002715
2716void kvm_lapic_exit(void)
2717{
2718 static_key_deferred_flush(&apic_hw_disabled);
2719 static_key_deferred_flush(&apic_sw_disabled);
2720}