blob: b24b3c6d686ea6426dcef858167ae9ec0090aa88 [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
57#define APIC_BUS_CYCLE_NS 1
58
59/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
60#define apic_debug(fmt, arg...)
61
Eddie Dong97222cc2007-09-12 10:58:04 +030062/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050063#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030064#define LAPIC_MMIO_LENGTH (1 << 12)
65/* followed define is not in apicdef.h */
66#define APIC_SHORT_MASK 0xc0000
67#define APIC_DEST_NOSHORT 0x0
68#define APIC_DEST_MASK 0x800
69#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090070#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030071
Nadav Amit394457a2014-10-03 00:30:52 +030072#define APIC_BROADCAST 0xFF
73#define X2APIC_BROADCAST 0xFFFFFFFFul
74
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030075static inline int apic_test_vector(int vec, void *bitmap)
76{
77 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
78}
79
Yang Zhang10606912013-04-11 19:21:38 +080080bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
81{
82 struct kvm_lapic *apic = vcpu->arch.apic;
83
84 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
85 apic_test_vector(vector, apic->regs + APIC_IRR);
86}
87
Eddie Dong97222cc2007-09-12 10:58:04 +030088static inline void apic_clear_vector(int vec, void *bitmap)
89{
90 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
91}
92
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030093static inline int __apic_test_and_set_vector(int vec, void *bitmap)
94{
95 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
96}
97
98static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
99{
100 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
101}
102
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300103struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300104struct static_key_deferred apic_sw_disabled __read_mostly;
105
Eddie Dong97222cc2007-09-12 10:58:04 +0300106static inline int apic_enabled(struct kvm_lapic *apic)
107{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300108 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300109}
110
Eddie Dong97222cc2007-09-12 10:58:04 +0300111#define LVT_MASK \
112 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
113
114#define LINT_MASK \
115 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
116 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
117
Radim Krčmáře45115b2016-07-12 22:09:19 +0200118static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
119 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
120 switch (map->mode) {
121 case KVM_APIC_MODE_X2APIC: {
122 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200123 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100124
Radim Krčmáře45115b2016-07-12 22:09:19 +0200125 if (offset <= max_apic_id) {
126 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100127
Radim Krčmáře45115b2016-07-12 22:09:19 +0200128 *cluster = &map->phys_map[offset];
129 *mask = dest_id & (0xffff >> (16 - cluster_size));
130 } else {
131 *mask = 0;
132 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100133
Radim Krčmáře45115b2016-07-12 22:09:19 +0200134 return true;
135 }
136 case KVM_APIC_MODE_XAPIC_FLAT:
137 *cluster = map->xapic_flat_map;
138 *mask = dest_id & 0xff;
139 return true;
140 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100141 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200142 *mask = dest_id & 0xf;
143 return true;
144 default:
145 /* Not optimized. */
146 return false;
147 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300148}
149
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200150static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100151{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200152 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100153
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200154 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100155}
156
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300157static void recalculate_apic_map(struct kvm *kvm)
158{
159 struct kvm_apic_map *new, *old = NULL;
160 struct kvm_vcpu *vcpu;
161 int i;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200162 u32 max_id = 255;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300163
164 mutex_lock(&kvm->arch.apic_map_lock);
165
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200166 kvm_for_each_vcpu(i, vcpu, kvm)
167 if (kvm_apic_present(vcpu))
168 max_id = max(max_id, kvm_apic_id(vcpu->arch.apic));
169
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200170 new = kvm_kvzalloc(sizeof(struct kvm_apic_map) +
171 sizeof(struct kvm_lapic *) * ((u64)max_id + 1));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200172
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300173 if (!new)
174 goto out;
175
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200176 new->max_apic_id = max_id;
177
Nadav Amit173beed2014-11-02 11:54:54 +0200178 kvm_for_each_vcpu(i, vcpu, kvm) {
179 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200180 struct kvm_lapic **cluster;
181 u16 mask;
Radim Krčmář25995e52014-11-27 23:30:19 +0100182 u32 ldr, aid;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300183
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100184 if (!kvm_apic_present(vcpu))
185 continue;
186
Radim Krčmář25995e52014-11-27 23:30:19 +0100187 aid = kvm_apic_id(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500188 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300189
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200190 if (aid <= new->max_apic_id)
Radim Krčmář25995e52014-11-27 23:30:19 +0100191 new->phys_map[aid] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100192
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100193 if (apic_x2apic_mode(apic)) {
194 new->mode |= KVM_APIC_MODE_X2APIC;
195 } else if (ldr) {
196 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500197 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100198 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
199 else
200 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
201 }
202
Radim Krčmáře45115b2016-07-12 22:09:19 +0200203 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100204 continue;
205
Radim Krčmáře45115b2016-07-12 22:09:19 +0200206 if (mask)
207 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300208 }
209out:
210 old = rcu_dereference_protected(kvm->arch.apic_map,
211 lockdep_is_held(&kvm->arch.apic_map_lock));
212 rcu_assign_pointer(kvm->arch.apic_map, new);
213 mutex_unlock(&kvm->arch.apic_map_lock);
214
215 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200216 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800217
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700218 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300219}
220
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300221static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
222{
Radim Krčmáře4627552014-10-30 15:06:45 +0100223 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300224
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500225 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100226
227 if (enabled != apic->sw_enabled) {
228 apic->sw_enabled = enabled;
229 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300230 static_key_slow_dec_deferred(&apic_sw_disabled);
231 recalculate_apic_map(apic->vcpu->kvm);
232 } else
233 static_key_slow_inc(&apic_sw_disabled.key);
234 }
235}
236
Radim Krčmářa92e2542016-07-12 22:09:22 +0200237static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300238{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500239 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300240 recalculate_apic_map(apic->vcpu->kvm);
241}
242
243static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
244{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500245 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300246 recalculate_apic_map(apic->vcpu->kvm);
247}
248
Dr. David Alan Gilberte02746e22017-11-17 11:52:49 +0000249static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
250{
251 return ((id >> 4) << 16) | (1 << (id & 0xf));
252}
253
Radim Krčmářa92e2542016-07-12 22:09:22 +0200254static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200255{
Dr. David Alan Gilberte02746e22017-11-17 11:52:49 +0000256 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200257
Radim Krčmářa92e2542016-07-12 22:09:22 +0200258 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500259 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200260 recalculate_apic_map(apic->vcpu->kvm);
261}
262
Eddie Dong97222cc2007-09-12 10:58:04 +0300263static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
264{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500265 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300266}
267
268static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
269{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500270 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300271}
272
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800273static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
274{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100275 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800276}
277
Eddie Dong97222cc2007-09-12 10:58:04 +0300278static inline int apic_lvtt_period(struct kvm_lapic *apic)
279{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100280 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800281}
282
283static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
284{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100285 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300286}
287
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200288static inline int apic_lvt_nmi_mode(u32 lvt_val)
289{
290 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
291}
292
Gleb Natapovfc61b802009-07-05 17:39:35 +0300293void kvm_apic_set_version(struct kvm_vcpu *vcpu)
294{
295 struct kvm_lapic *apic = vcpu->arch.apic;
296 struct kvm_cpuid_entry2 *feat;
297 u32 v = APIC_VERSION;
298
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100299 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300300 return;
301
302 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
303 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
304 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500305 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300306}
307
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500308static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800309 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300310 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
311 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
312 LINT_MASK, LINT_MASK, /* LVT0-1 */
313 LVT_MASK /* LVTERR */
314};
315
316static int find_highest_vector(void *bitmap)
317{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900318 int vec;
319 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300320
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900321 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
322 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
323 reg = bitmap + REG_POS(vec);
324 if (*reg)
325 return fls(*reg) - 1 + vec;
326 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300327
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900328 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300329}
330
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300331static u8 count_vectors(void *bitmap)
332{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900333 int vec;
334 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300335 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900336
337 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
338 reg = bitmap + REG_POS(vec);
339 count += hweight32(*reg);
340 }
341
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300342 return count;
343}
344
Wincy Van705699a2015-02-03 23:58:17 +0800345void __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800346{
347 u32 i, pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800348
349 for (i = 0; i <= 7; i++) {
350 pir_val = xchg(&pir[i], 0);
351 if (pir_val)
Wincy Van705699a2015-02-03 23:58:17 +0800352 *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800353 }
354}
Wincy Van705699a2015-02-03 23:58:17 +0800355EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
356
357void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
358{
359 struct kvm_lapic *apic = vcpu->arch.apic;
360
361 __kvm_apic_update_irr(pir, apic->regs);
Radim Krčmářc77f3fa2015-10-08 20:23:33 +0200362
363 kvm_make_request(KVM_REQ_EVENT, vcpu);
Wincy Van705699a2015-02-03 23:58:17 +0800364}
Yang Zhanga20ed542013-04-11 19:25:15 +0800365EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
366
Gleb Natapov33e4c682009-06-11 11:06:51 +0300367static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300368{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300369 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300370}
371
372static inline int apic_find_highest_irr(struct kvm_lapic *apic)
373{
374 int result;
375
Yang Zhangc7c9c562013-01-25 10:18:51 +0800376 /*
377 * Note that irr_pending is just a hint. It will be always
378 * true with virtual interrupt delivery enabled.
379 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300380 if (!apic->irr_pending)
381 return -1;
382
Andrey Smetanind62caab2015-11-10 15:36:33 +0300383 if (apic->vcpu->arch.apicv_active)
384 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
Gleb Natapov33e4c682009-06-11 11:06:51 +0300385 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300386 ASSERT(result == -1 || result >= 16);
387
388 return result;
389}
390
Gleb Natapov33e4c682009-06-11 11:06:51 +0300391static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
392{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800393 struct kvm_vcpu *vcpu;
394
395 vcpu = apic->vcpu;
396
Andrey Smetanind62caab2015-11-10 15:36:33 +0300397 if (unlikely(vcpu->arch.apicv_active)) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800398 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200399 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800400 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200401 } else {
402 apic->irr_pending = false;
403 apic_clear_vector(vec, apic->regs + APIC_IRR);
404 if (apic_search_irr(apic) != -1)
405 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800406 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300407}
408
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300409static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
410{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800411 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200412
Wanpeng Li56cc2402014-08-05 12:42:24 +0800413 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
414 return;
415
416 vcpu = apic->vcpu;
417
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300418 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800419 * With APIC virtualization enabled, all caching is disabled
420 * because the processor can modify ISR under the hood. Instead
421 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300422 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300423 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200424 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800425 else {
426 ++apic->isr_count;
427 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
428 /*
429 * ISR (in service register) bit is set when injecting an interrupt.
430 * The highest vector is injected. Thus the latest bit set matches
431 * the highest bit in ISR.
432 */
433 apic->highest_isr_cache = vec;
434 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300435}
436
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200437static inline int apic_find_highest_isr(struct kvm_lapic *apic)
438{
439 int result;
440
441 /*
442 * Note that isr_count is always 1, and highest_isr_cache
443 * is always -1, with APIC virtualization enabled.
444 */
445 if (!apic->isr_count)
446 return -1;
447 if (likely(apic->highest_isr_cache != -1))
448 return apic->highest_isr_cache;
449
450 result = find_highest_vector(apic->regs + APIC_ISR);
451 ASSERT(result == -1 || result >= 16);
452
453 return result;
454}
455
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300456static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
457{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200458 struct kvm_vcpu *vcpu;
459 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
460 return;
461
462 vcpu = apic->vcpu;
463
464 /*
465 * We do get here for APIC virtualization enabled if the guest
466 * uses the Hyper-V APIC enlightenment. In this case we may need
467 * to trigger a new interrupt delivery by writing the SVI field;
468 * on the other hand isr_count and highest_isr_cache are unused
469 * and must be left alone.
470 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300471 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200472 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200473 apic_find_highest_isr(apic));
474 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300475 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200476 BUG_ON(apic->isr_count < 0);
477 apic->highest_isr_cache = -1;
478 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300479}
480
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800481int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
482{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300483 /* This may race with setting of irr in __apic_accept_irq() and
484 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
485 * will cause vmexit immediately and the value will be recalculated
486 * on the next vmentry.
487 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100488 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800489}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800490
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200491static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800492 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100493 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200494
Yang Zhangb4f22252013-04-11 19:21:37 +0800495int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100496 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300497{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800498 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800499
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200500 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800501 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300502}
503
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300504static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
505{
506
507 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
508 sizeof(val));
509}
510
511static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
512{
513
514 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
515 sizeof(*val));
516}
517
518static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
519{
520 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
521}
522
523static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
524{
525 u8 val;
526 if (pv_eoi_get_user(vcpu, &val) < 0)
527 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800528 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300529 return val & 0x1;
530}
531
532static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
533{
534 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
535 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800536 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300537 return;
538 }
539 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
540}
541
542static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
543{
544 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
545 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800546 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300547 return;
548 }
549 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
550}
551
Eddie Dong97222cc2007-09-12 10:58:04 +0300552static void apic_update_ppr(struct kvm_lapic *apic)
553{
Avi Kivity3842d132010-07-27 12:30:24 +0300554 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300555 int isr;
556
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500557 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
558 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300559 isr = apic_find_highest_isr(apic);
560 isrv = (isr != -1) ? isr : 0;
561
562 if ((tpr & 0xf0) >= (isrv & 0xf0))
563 ppr = tpr & 0xff;
564 else
565 ppr = isrv & 0xf0;
566
567 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
568 apic, ppr, isr, isrv);
569
Avi Kivity3842d132010-07-27 12:30:24 +0300570 if (old_ppr != ppr) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500571 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200572 if (ppr < old_ppr)
573 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300574 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300575}
576
577static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
578{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500579 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300580 apic_update_ppr(apic);
581}
582
Radim Krčmář03d22492015-02-12 19:41:31 +0100583static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300584{
Radim Krčmář03d22492015-02-12 19:41:31 +0100585 if (apic_x2apic_mode(apic))
586 return mda == X2APIC_BROADCAST;
587
588 return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST;
Eddie Dong97222cc2007-09-12 10:58:04 +0300589}
590
Radim Krčmář03d22492015-02-12 19:41:31 +0100591static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300592{
Radim Krčmář03d22492015-02-12 19:41:31 +0100593 if (kvm_apic_broadcast(apic, mda))
594 return true;
595
596 if (apic_x2apic_mode(apic))
597 return mda == kvm_apic_id(apic);
598
599 return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic));
Nadav Amit394457a2014-10-03 00:30:52 +0300600}
601
Radim Krčmář52c233a2015-01-29 22:48:48 +0100602static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300603{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300604 u32 logical_id;
605
Nadav Amit394457a2014-10-03 00:30:52 +0300606 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100607 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300608
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500609 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300610
Radim Krčmář9368b562015-01-29 22:48:49 +0100611 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100612 return ((logical_id >> 16) == (mda >> 16))
613 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100614
615 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Radim Krčmář03d22492015-02-12 19:41:31 +0100616 mda = GET_APIC_DEST_FIELD(mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300617
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500618 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300619 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100620 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300621 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100622 return ((logical_id >> 4) == (mda >> 4))
623 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300624 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200625 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500626 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100627 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300628 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300629}
630
Radim Krčmářc5192652016-07-12 22:09:28 +0200631/* The KVM local APIC implementation has two quirks:
632 *
633 * - the xAPIC MDA stores the destination at bits 24-31, while this
634 * is not true of struct kvm_lapic_irq's dest_id field. This is
635 * just a quirk in the API and is not problematic.
636 *
637 * - in-kernel IOAPIC messages have to be delivered directly to
638 * x2APIC, because the kernel does not support interrupt remapping.
639 * In order to support broadcast without interrupt remapping, x2APIC
640 * rewrites the destination of non-IPI messages from APIC_BROADCAST
641 * to X2APIC_BROADCAST.
642 *
643 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
644 * important when userspace wants to use x2APIC-format MSIs, because
645 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100646 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200647static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
648 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100649{
650 bool ipi = source != NULL;
651 bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
652
Radim Krčmářc5192652016-07-12 22:09:28 +0200653 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
654 !ipi && dest_id == APIC_BROADCAST && x2apic_mda)
Radim Krčmář03d22492015-02-12 19:41:31 +0100655 return X2APIC_BROADCAST;
656
657 return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
658}
659
Radim Krčmář52c233a2015-01-29 22:48:48 +0100660bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300661 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300662{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800663 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200664 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300665
666 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200667 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300668 target, source, dest, dest_mode, short_hand);
669
Zachary Amsdenbd371392010-06-14 11:42:15 -1000670 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300671 switch (short_hand) {
672 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100673 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100674 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200675 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100676 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300677 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100678 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300679 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100680 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300681 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100682 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300683 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200684 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
685 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100686 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300687 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300688}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500689EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300690
Feng Wu520040142016-01-25 16:53:33 +0800691int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
692 const unsigned long *bitmap, u32 bitmap_size)
693{
694 u32 mod;
695 int i, idx = -1;
696
697 mod = vector % dest_vcpus;
698
699 for (i = 0; i <= mod; i++) {
700 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
701 BUG_ON(idx == bitmap_size);
702 }
703
704 return idx;
705}
706
Radim Krčmář4efd8052016-02-12 15:00:15 +0100707static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
708{
709 if (!kvm->arch.disabled_lapic_found) {
710 kvm->arch.disabled_lapic_found = true;
711 printk(KERN_INFO
712 "Disabled LAPIC found during irq injection\n");
713 }
714}
715
Radim Krčmářc5192652016-07-12 22:09:28 +0200716static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
717 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
718{
719 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
720 if ((irq->dest_id == APIC_BROADCAST &&
721 map->mode != KVM_APIC_MODE_X2APIC))
722 return true;
723 if (irq->dest_id == X2APIC_BROADCAST)
724 return true;
725 } else {
726 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
727 if (irq->dest_id == (x2apic_ipi ?
728 X2APIC_BROADCAST : APIC_BROADCAST))
729 return true;
730 }
731
732 return false;
733}
734
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200735/* Return true if the interrupt can be handled by using *bitmap as index mask
736 * for valid destinations in *dst array.
737 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
738 * Note: we may have zero kvm_lapic destinations when we return true, which
739 * means that the interrupt should be dropped. In this case, *bitmap would be
740 * zero and *dst undefined.
741 */
742static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
743 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
744 struct kvm_apic_map *map, struct kvm_lapic ***dst,
745 unsigned long *bitmap)
746{
747 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200748
749 if (irq->shorthand == APIC_DEST_SELF && src) {
750 *dst = src;
751 *bitmap = 1;
752 return true;
753 } else if (irq->shorthand)
754 return false;
755
Radim Krčmářc5192652016-07-12 22:09:28 +0200756 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200757 return false;
758
759 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200760 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200761 *bitmap = 0;
762 } else {
763 *dst = &map->phys_map[irq->dest_id];
764 *bitmap = 1;
765 }
766 return true;
767 }
768
Radim Krčmáře45115b2016-07-12 22:09:19 +0200769 *bitmap = 0;
770 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
771 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200772 return false;
773
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200774 if (!kvm_lowest_prio_delivery(irq))
775 return true;
776
777 if (!kvm_vector_hashing_enabled()) {
778 lowest = -1;
779 for_each_set_bit(i, bitmap, 16) {
780 if (!(*dst)[i])
781 continue;
782 if (lowest < 0)
783 lowest = i;
784 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
785 (*dst)[lowest]->vcpu) < 0)
786 lowest = i;
787 }
788 } else {
789 if (!*bitmap)
790 return true;
791
792 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
793 bitmap, 16);
794
795 if (!(*dst)[lowest]) {
796 kvm_apic_disabled_lapic_found(kvm);
797 *bitmap = 0;
798 return true;
799 }
800 }
801
802 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
803
804 return true;
805}
806
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300807bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100808 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300809{
810 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200811 unsigned long bitmap;
812 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300813 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200814 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300815
816 *r = -1;
817
818 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800819 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300820 return true;
821 }
822
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300823 rcu_read_lock();
824 map = rcu_dereference(kvm->arch.apic_map);
825
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200826 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
827 if (ret)
828 for_each_set_bit(i, &bitmap, 16) {
829 if (!dst[i])
830 continue;
831 if (*r < 0)
832 *r = 0;
833 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100834 }
835
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300836 rcu_read_unlock();
837 return ret;
838}
839
Feng Wu6228a0d2016-01-25 16:53:34 +0800840/*
841 * This routine tries to handler interrupts in posted mode, here is how
842 * it deals with different cases:
843 * - For single-destination interrupts, handle it in posted mode
844 * - Else if vector hashing is enabled and it is a lowest-priority
845 * interrupt, handle it in posted mode and use the following mechanism
846 * to find the destinaiton vCPU.
847 * 1. For lowest-priority interrupts, store all the possible
848 * destination vCPUs in an array.
849 * 2. Use "guest vector % max number of destination vCPUs" to find
850 * the right destination vCPU in the array for the lowest-priority
851 * interrupt.
852 * - Otherwise, use remapped mode to inject the interrupt.
853 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800854bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
855 struct kvm_vcpu **dest_vcpu)
856{
857 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200858 unsigned long bitmap;
859 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800860 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800861
862 if (irq->shorthand)
863 return false;
864
865 rcu_read_lock();
866 map = rcu_dereference(kvm->arch.apic_map);
867
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200868 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
869 hweight16(bitmap) == 1) {
870 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800871
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200872 if (dst[i]) {
873 *dest_vcpu = dst[i]->vcpu;
874 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +0800875 }
Feng Wu8feb4a02015-09-18 22:29:47 +0800876 }
877
Feng Wu8feb4a02015-09-18 22:29:47 +0800878 rcu_read_unlock();
879 return ret;
880}
881
Eddie Dong97222cc2007-09-12 10:58:04 +0300882/*
883 * Add a pending IRQ into lapic.
884 * Return 1 if successfully added and 0 if discarded.
885 */
886static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800887 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100888 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300889{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200890 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300891 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300892
Paolo Bonzinia183b632014-09-11 11:51:02 +0200893 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
894 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300895 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300896 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200897 vcpu->arch.apic_arb_prio++;
898 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200899 if (unlikely(trig_mode && !level))
900 break;
901
Eddie Dong97222cc2007-09-12 10:58:04 +0300902 /* FIXME add logic for vcpu on reset */
903 if (unlikely(!apic_enabled(apic)))
904 break;
905
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200906 result = 1;
907
Joerg Roedel9daa5002016-02-29 16:04:44 +0100908 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100909 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +0100910 dest_map->vectors[vcpu->vcpu_id] = vector;
911 }
Avi Kivitya5d36f82009-12-29 12:42:16 +0200912
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200913 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
914 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500915 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200916 else
917 apic_clear_vector(vector, apic->regs + APIC_TMR);
918 }
919
Andrey Smetanind62caab2015-11-10 15:36:33 +0300920 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +0800921 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200922 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500923 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800924
925 kvm_make_request(KVM_REQ_EVENT, vcpu);
926 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300927 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300928 break;
929
930 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530931 result = 1;
932 vcpu->arch.pv.pv_unhalted = 1;
933 kvm_make_request(KVM_REQ_EVENT, vcpu);
934 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300935 break;
936
937 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200938 result = 1;
939 kvm_make_request(KVM_REQ_SMI, vcpu);
940 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300941 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800942
Eddie Dong97222cc2007-09-12 10:58:04 +0300943 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200944 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800945 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200946 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300947 break;
948
949 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100950 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200951 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100952 /* assumes that there are only KVM_APIC_INIT/SIPI */
953 apic->pending_events = (1UL << KVM_APIC_INIT);
954 /* make sure pending_events is visible before sending
955 * the request */
956 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300957 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300958 kvm_vcpu_kick(vcpu);
959 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200960 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
961 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300962 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300963 break;
964
965 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200966 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
967 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100968 result = 1;
969 apic->sipi_vector = vector;
970 /* make sure sipi_vector is visible for the receiver */
971 smp_wmb();
972 set_bit(KVM_APIC_SIPI, &apic->pending_events);
973 kvm_make_request(KVM_REQ_EVENT, vcpu);
974 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300975 break;
976
Jan Kiszka23930f92008-09-26 09:30:52 +0200977 case APIC_DM_EXTINT:
978 /*
979 * Should only be called by kvm_apic_local_deliver() with LVT0,
980 * before NMI watchdog was enabled. Already handled by
981 * kvm_apic_accept_pic_intr().
982 */
983 break;
984
Eddie Dong97222cc2007-09-12 10:58:04 +0300985 default:
986 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
987 delivery_mode);
988 break;
989 }
990 return result;
991}
992
Gleb Natapove1035712009-03-05 16:34:59 +0200993int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +0300994{
Gleb Natapove1035712009-03-05 16:34:59 +0200995 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800996}
997
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200998static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
999{
Andrey Smetanin63086302015-11-10 15:36:32 +03001000 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001001}
1002
Yang Zhangc7c9c562013-01-25 10:18:51 +08001003static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1004{
Steve Rutherford7543a632015-07-29 23:21:41 -07001005 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001006
Steve Rutherford7543a632015-07-29 23:21:41 -07001007 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1008 if (!kvm_ioapic_handles_vector(apic, vector))
1009 return;
1010
1011 /* Request a KVM exit to inform the userspace IOAPIC. */
1012 if (irqchip_split(apic->vcpu->kvm)) {
1013 apic->vcpu->arch.pending_ioapic_eoi = vector;
1014 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1015 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001016 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001017
1018 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1019 trigger_mode = IOAPIC_LEVEL_TRIG;
1020 else
1021 trigger_mode = IOAPIC_EDGE_TRIG;
1022
1023 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001024}
1025
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001026static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001027{
1028 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001029
1030 trace_kvm_eoi(apic, vector);
1031
Eddie Dong97222cc2007-09-12 10:58:04 +03001032 /*
1033 * Not every write EOI will has corresponding ISR,
1034 * one example is when Kernel check timer on setup_IO_APIC
1035 */
1036 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001037 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001038
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001039 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001040 apic_update_ppr(apic);
1041
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001042 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1043 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1044
Yang Zhangc7c9c562013-01-25 10:18:51 +08001045 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001046 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001047 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001048}
1049
Yang Zhangc7c9c562013-01-25 10:18:51 +08001050/*
1051 * this interface assumes a trap-like exit, which has already finished
1052 * desired side effect including vISR and vPPR update.
1053 */
1054void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1055{
1056 struct kvm_lapic *apic = vcpu->arch.apic;
1057
1058 trace_kvm_eoi(apic, vector);
1059
1060 kvm_ioapic_send_eoi(apic, vector);
1061 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1062}
1063EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1064
Eddie Dong97222cc2007-09-12 10:58:04 +03001065static void apic_send_ipi(struct kvm_lapic *apic)
1066{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001067 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1068 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001069 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001070
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001071 irq.vector = icr_low & APIC_VECTOR_MASK;
1072 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1073 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001074 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001075 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1076 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001077 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001078 if (apic_x2apic_mode(apic))
1079 irq.dest_id = icr_high;
1080 else
1081 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001082
Gleb Natapov1000ff82009-07-07 16:00:57 +03001083 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1084
Eddie Dong97222cc2007-09-12 10:58:04 +03001085 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1086 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001087 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1088 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001089 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001090 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001091 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001092
Yang Zhangb4f22252013-04-11 19:21:37 +08001093 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001094}
1095
1096static u32 apic_get_tmcct(struct kvm_lapic *apic)
1097{
Marcelo Tosattib682b812009-02-10 20:41:41 -02001098 ktime_t remaining;
1099 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001100 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001101
1102 ASSERT(apic != NULL);
1103
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001104 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001105 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001106 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001107 return 0;
1108
Marcelo Tosattiace15462009-10-08 10:55:03 -03001109 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001110 if (ktime_to_ns(remaining) < 0)
1111 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001112
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001113 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1114 tmcct = div64_u64(ns,
1115 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001116
1117 return tmcct;
1118}
1119
Avi Kivityb209749f2007-10-22 16:50:39 +02001120static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1121{
1122 struct kvm_vcpu *vcpu = apic->vcpu;
1123 struct kvm_run *run = vcpu->run;
1124
Avi Kivitya8eeb042010-05-10 12:34:53 +03001125 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001126 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001127 run->tpr_access.is_write = write;
1128}
1129
1130static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1131{
1132 if (apic->vcpu->arch.tpr_access_reporting)
1133 __report_tpr_access(apic, write);
1134}
1135
Eddie Dong97222cc2007-09-12 10:58:04 +03001136static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1137{
1138 u32 val = 0;
1139
1140 if (offset >= LAPIC_MMIO_LENGTH)
1141 return 0;
1142
1143 switch (offset) {
1144 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001145 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001146 break;
1147
1148 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001149 if (apic_lvtt_tscdeadline(apic))
1150 return 0;
1151
Eddie Dong97222cc2007-09-12 10:58:04 +03001152 val = apic_get_tmcct(apic);
1153 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001154 case APIC_PROCPRI:
1155 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001156 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001157 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001158 case APIC_TASKPRI:
1159 report_tpr_access(apic, false);
1160 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001161 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001162 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001163 break;
1164 }
1165
1166 return val;
1167}
1168
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001169static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1170{
1171 return container_of(dev, struct kvm_lapic, dev);
1172}
1173
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001174int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001175 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001176{
Eddie Dong97222cc2007-09-12 10:58:04 +03001177 unsigned char alignment = offset & 0xf;
1178 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001179 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001180 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001181
1182 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001183 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1184 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001185 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001186 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001187
1188 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001189 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1190 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001191 return 1;
1192 }
1193
Eddie Dong97222cc2007-09-12 10:58:04 +03001194 result = __apic_read(apic, offset & ~0xf);
1195
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001196 trace_kvm_apic_read(offset, result);
1197
Eddie Dong97222cc2007-09-12 10:58:04 +03001198 switch (len) {
1199 case 1:
1200 case 2:
1201 case 4:
1202 memcpy(data, (char *)&result + alignment, len);
1203 break;
1204 default:
1205 printk(KERN_ERR "Local APIC read with len = %x, "
1206 "should be 1,2, or 4 instead\n", len);
1207 break;
1208 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001209 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001210}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001211EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001212
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001213static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1214{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001215 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001216 addr >= apic->base_address &&
1217 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1218}
1219
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001220static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001221 gpa_t address, int len, void *data)
1222{
1223 struct kvm_lapic *apic = to_lapic(this);
1224 u32 offset = address - apic->base_address;
1225
1226 if (!apic_mmio_in_range(apic, address))
1227 return -EOPNOTSUPP;
1228
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001229 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001230
1231 return 0;
1232}
1233
Eddie Dong97222cc2007-09-12 10:58:04 +03001234static void update_divide_count(struct kvm_lapic *apic)
1235{
1236 u32 tmp1, tmp2, tdcr;
1237
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001238 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001239 tmp1 = tdcr & 0xf;
1240 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001241 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001242
1243 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001244 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001245}
1246
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001247static void apic_update_lvtt(struct kvm_lapic *apic)
1248{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001249 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001250 apic->lapic_timer.timer_mode_mask;
1251
1252 if (apic->lapic_timer.timer_mode != timer_mode) {
1253 apic->lapic_timer.timer_mode = timer_mode;
1254 hrtimer_cancel(&apic->lapic_timer.timer);
1255 }
1256}
1257
Radim Krčmář5d87db72014-10-10 19:15:08 +02001258static void apic_timer_expired(struct kvm_lapic *apic)
1259{
1260 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001261 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001262 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001263
Radim Krčmář5d87db72014-10-10 19:15:08 +02001264 if (atomic_read(&apic->lapic_timer.pending))
1265 return;
1266
1267 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001268 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001269
Marcelo Tosatti85773702016-02-19 09:46:39 +01001270 if (swait_active(q))
1271 swake_up(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001272
1273 if (apic_lvtt_tscdeadline(apic))
1274 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1275}
1276
1277/*
1278 * On APICv, this test will cause a busy wait
1279 * during a higher-priority task.
1280 */
1281
1282static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1283{
1284 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001285 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001286
1287 if (kvm_apic_hw_enabled(apic)) {
1288 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001289 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001290
Andrey Smetanind62caab2015-11-10 15:36:33 +03001291 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001292 bitmap = apic->regs + APIC_IRR;
1293
1294 if (apic_test_vector(vec, bitmap))
1295 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001296 }
1297 return false;
1298}
1299
1300void wait_lapic_expire(struct kvm_vcpu *vcpu)
1301{
1302 struct kvm_lapic *apic = vcpu->arch.apic;
1303 u64 guest_tsc, tsc_deadline;
1304
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001305 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001306 return;
1307
1308 if (apic->lapic_timer.expired_tscdeadline == 0)
1309 return;
1310
1311 if (!lapic_timer_int_injected(vcpu))
1312 return;
1313
1314 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1315 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001316 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001317 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001318
1319 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1320 if (guest_tsc < tsc_deadline)
Marcelo Tosattib606f182016-06-20 22:33:48 -03001321 __delay(min(tsc_deadline - guest_tsc,
1322 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
Radim Krčmář5d87db72014-10-10 19:15:08 +02001323}
1324
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001325static void start_sw_tscdeadline(struct kvm_lapic *apic)
1326{
1327 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1328 u64 ns = 0;
1329 ktime_t expire;
1330 struct kvm_vcpu *vcpu = apic->vcpu;
1331 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1332 unsigned long flags;
1333 ktime_t now;
1334
1335 if (unlikely(!tscdeadline || !this_tsc_khz))
1336 return;
1337
1338 local_irq_save(flags);
1339
1340 now = apic->lapic_timer.timer.base->get_time();
1341 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1342 if (likely(tscdeadline > guest_tsc)) {
1343 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1344 do_div(ns, this_tsc_khz);
1345 expire = ktime_add_ns(now, ns);
1346 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1347 hrtimer_start(&apic->lapic_timer.timer,
1348 expire, HRTIMER_MODE_ABS_PINNED);
1349 } else
1350 apic_timer_expired(apic);
1351
1352 local_irq_restore(flags);
1353}
1354
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001355bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1356{
Wanpeng Li91005302016-08-03 12:04:12 +08001357 if (!lapic_in_kernel(vcpu))
1358 return false;
1359
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001360 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1361}
1362EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1363
Wanpeng Libd97ad02016-06-30 08:52:49 +08001364static void cancel_hv_tscdeadline(struct kvm_lapic *apic)
1365{
1366 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1367 apic->lapic_timer.hv_timer_in_use = false;
1368}
1369
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001370void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1371{
1372 struct kvm_lapic *apic = vcpu->arch.apic;
1373
1374 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1375 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Libd97ad02016-06-30 08:52:49 +08001376 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001377 apic_timer_expired(apic);
1378}
1379EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1380
Wanpeng Li196f20c2016-06-28 14:54:19 +08001381static bool start_hv_tscdeadline(struct kvm_lapic *apic)
1382{
1383 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1384
1385 if (atomic_read(&apic->lapic_timer.pending) ||
1386 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1387 if (apic->lapic_timer.hv_timer_in_use)
1388 cancel_hv_tscdeadline(apic);
1389 } else {
1390 apic->lapic_timer.hv_timer_in_use = true;
1391 hrtimer_cancel(&apic->lapic_timer.timer);
1392
1393 /* In case the sw timer triggered in the window */
1394 if (atomic_read(&apic->lapic_timer.pending))
1395 cancel_hv_tscdeadline(apic);
1396 }
1397 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1398 apic->lapic_timer.hv_timer_in_use);
1399 return apic->lapic_timer.hv_timer_in_use;
1400}
1401
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001402void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1403{
1404 struct kvm_lapic *apic = vcpu->arch.apic;
1405
1406 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1407
Wanpeng Li196f20c2016-06-28 14:54:19 +08001408 if (apic_lvtt_tscdeadline(apic))
1409 start_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001410}
1411EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1412
1413void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1414{
1415 struct kvm_lapic *apic = vcpu->arch.apic;
1416
1417 /* Possibly the TSC deadline timer is not enabled yet */
1418 if (!apic->lapic_timer.hv_timer_in_use)
1419 return;
1420
Wanpeng Libd97ad02016-06-30 08:52:49 +08001421 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001422
1423 if (atomic_read(&apic->lapic_timer.pending))
1424 return;
1425
1426 start_sw_tscdeadline(apic);
1427}
1428EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1429
Eddie Dong97222cc2007-09-12 10:58:04 +03001430static void start_apic_timer(struct kvm_lapic *apic)
1431{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001432 ktime_t now;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001433
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001434 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001435
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001436 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001437 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001438 now = apic->lapic_timer.timer.base->get_time();
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001439 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001440 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +02001441
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001442 if (!apic->lapic_timer.period)
1443 return;
1444 /*
1445 * Do not allow the guest to program periodic timers with small
1446 * interval, since the hrtimers are not throttled by the host
1447 * scheduler.
1448 */
1449 if (apic_lvtt_period(apic)) {
1450 s64 min_period = min_timer_period_us * 1000LL;
1451
1452 if (apic->lapic_timer.period < min_period) {
1453 pr_info_ratelimited(
1454 "kvm: vcpu %i: requested %lld ns "
1455 "lapic timer period limited to %lld ns\n",
1456 apic->vcpu->vcpu_id,
1457 apic->lapic_timer.period, min_period);
1458 apic->lapic_timer.period = min_period;
1459 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001460 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001461
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001462 hrtimer_start(&apic->lapic_timer.timer,
1463 ktime_add_ns(now, apic->lapic_timer.period),
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001464 HRTIMER_MODE_ABS_PINNED);
Eddie Dong97222cc2007-09-12 10:58:04 +03001465
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001466 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001467 PRIx64 ", "
1468 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001469 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001470 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001471 kvm_lapic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001472 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001473 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001474 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001475 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li196f20c2016-06-28 14:54:19 +08001476 if (!(kvm_x86_ops->set_hv_timer && start_hv_tscdeadline(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001477 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001478 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001479}
1480
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001481static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1482{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001483 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001484
Radim Krčmář59fd1322015-06-30 22:19:16 +02001485 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1486 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1487 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001488 apic_debug("Receive NMI setting on APIC_LVT0 "
1489 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001490 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001491 } else
1492 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1493 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001494}
1495
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001496int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001497{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001498 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001499
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001500 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001501
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001502 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001503 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001504 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001505 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001506 else
1507 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001508 break;
1509
1510 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001511 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001512 apic_set_tpr(apic, val & 0xff);
1513 break;
1514
1515 case APIC_EOI:
1516 apic_set_eoi(apic);
1517 break;
1518
1519 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001520 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001521 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001522 else
1523 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001524 break;
1525
1526 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001527 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001528 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001529 recalculate_apic_map(apic->vcpu->kvm);
1530 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001531 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001532 break;
1533
Gleb Natapovfc61b802009-07-05 17:39:35 +03001534 case APIC_SPIV: {
1535 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001536 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001537 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001538 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001539 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1540 int i;
1541 u32 lvt_val;
1542
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001543 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001544 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001545 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001546 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001547 lvt_val | APIC_LVT_MASKED);
1548 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001549 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001550 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001551
1552 }
1553 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001554 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001555 case APIC_ICR:
1556 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001557 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001558 apic_send_ipi(apic);
1559 break;
1560
1561 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001562 if (!apic_x2apic_mode(apic))
1563 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001564 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001565 break;
1566
Jan Kiszka23930f92008-09-26 09:30:52 +02001567 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001568 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001569 case APIC_LVTTHMR:
1570 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001571 case APIC_LVT1:
1572 case APIC_LVTERR:
1573 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001574 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001575 val |= APIC_LVT_MASKED;
1576
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001577 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001578 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001579
1580 break;
1581
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001582 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001583 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001584 val |= APIC_LVT_MASKED;
1585 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001586 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001587 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001588 break;
1589
Eddie Dong97222cc2007-09-12 10:58:04 +03001590 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001591 if (apic_lvtt_tscdeadline(apic))
1592 break;
1593
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001594 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001595 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001596 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001597 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001598
1599 case APIC_TDCR:
1600 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001601 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001602 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001603 update_divide_count(apic);
1604 break;
1605
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001606 case APIC_ESR:
1607 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001608 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001609 ret = 1;
1610 }
1611 break;
1612
1613 case APIC_SELF_IPI:
1614 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001615 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001616 } else
1617 ret = 1;
1618 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001619 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001620 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001621 break;
1622 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001623 if (ret)
1624 apic_debug("Local APIC Write to read-only register %x\n", reg);
1625 return ret;
1626}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001627EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001628
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001629static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001630 gpa_t address, int len, const void *data)
1631{
1632 struct kvm_lapic *apic = to_lapic(this);
1633 unsigned int offset = address - apic->base_address;
1634 u32 val;
1635
1636 if (!apic_mmio_in_range(apic, address))
1637 return -EOPNOTSUPP;
1638
1639 /*
1640 * APIC register must be aligned on 128-bits boundary.
1641 * 32/64/128 bits registers must be accessed thru 32 bits.
1642 * Refer SDM 8.4.1
1643 */
1644 if (len != 4 || (offset & 0xf)) {
1645 /* Don't shout loud, $infamous_os would cause only noise. */
1646 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001647 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001648 }
1649
1650 val = *(u32*)data;
1651
1652 /* too common printing */
1653 if (offset != APIC_EOI)
1654 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1655 "0x%x\n", __func__, offset, len, val);
1656
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001657 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001658
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001659 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001660}
1661
Kevin Tian58fbbf22011-08-30 13:56:17 +03001662void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1663{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001664 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001665}
1666EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1667
Yang Zhang83d4c282013-01-25 10:18:49 +08001668/* emulate APIC access in a trap manner */
1669void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1670{
1671 u32 val = 0;
1672
1673 /* hw has done the conditional check and inst decode */
1674 offset &= 0xff0;
1675
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001676 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001677
1678 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001679 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001680}
1681EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1682
Rusty Russelld5894442007-10-08 10:48:30 +10001683void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001684{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001685 struct kvm_lapic *apic = vcpu->arch.apic;
1686
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001687 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001688 return;
1689
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001690 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001691
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001692 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1693 static_key_slow_dec_deferred(&apic_hw_disabled);
1694
Radim Krčmáře4627552014-10-30 15:06:45 +01001695 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001696 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001697
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001698 if (apic->regs)
1699 free_page((unsigned long)apic->regs);
1700
1701 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001702}
1703
1704/*
1705 *----------------------------------------------------------------------
1706 * LAPIC interface
1707 *----------------------------------------------------------------------
1708 */
1709
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001710u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1711{
1712 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001713
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001714 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001715 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001716 return 0;
1717
1718 return apic->lapic_timer.tscdeadline;
1719}
1720
1721void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1722{
1723 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001724
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001725 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001726 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001727 return;
1728
1729 hrtimer_cancel(&apic->lapic_timer.timer);
1730 apic->lapic_timer.tscdeadline = data;
1731 start_apic_timer(apic);
1732}
1733
Eddie Dong97222cc2007-09-12 10:58:04 +03001734void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1735{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001736 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001737
Avi Kivityb93463a2007-10-25 16:52:32 +02001738 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001739 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001740}
1741
1742u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1743{
Eddie Dong97222cc2007-09-12 10:58:04 +03001744 u64 tpr;
1745
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001746 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001747
1748 return (tpr & 0xf0) >> 4;
1749}
1750
1751void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1752{
Yang Zhang8d146952013-01-25 10:18:50 +08001753 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001754 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001755
1756 if (!apic) {
1757 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001758 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001759 return;
1760 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001761
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001762 vcpu->arch.apic_base = value;
1763
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001764 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001765 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001766 if (value & MSR_IA32_APICBASE_ENABLE) {
1767 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001768 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08001769 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001770 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08001771 recalculate_apic_map(vcpu->kvm);
1772 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001773 }
1774
Yang Zhang8d146952013-01-25 10:18:50 +08001775 if ((old_value ^ value) & X2APIC_ENABLE) {
1776 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001777 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001778 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1779 } else
1780 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001781 }
Yang Zhang8d146952013-01-25 10:18:50 +08001782
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001783 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001784 MSR_IA32_APICBASE_BASE;
1785
Nadav Amitdb324fe2014-11-02 11:54:59 +02001786 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1787 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1788 pr_warn_once("APIC base relocation is unsupported by KVM");
1789
Eddie Dong97222cc2007-09-12 10:58:04 +03001790 /* with FSB delivery interrupt, we can restart APIC functionality */
1791 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001792 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001793
1794}
1795
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001796void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001797{
1798 struct kvm_lapic *apic;
1799 int i;
1800
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001801 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001802
1803 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001804 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001805 ASSERT(apic != NULL);
1806
1807 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001808 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001809
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001810 if (!init_event) {
1811 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1812 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001813 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001814 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001815 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001816
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001817 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1818 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001819 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001820 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001821 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001822 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001823 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001824
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001825 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001826 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001827 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001828 if (!apic_x2apic_mode(apic))
1829 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001830 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1831 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1832 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1833 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1834 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001835 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001836 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1837 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1838 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001839 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001840 apic->irr_pending = vcpu->arch.apicv_active;
1841 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001842 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001843 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001844 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001845 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001846 kvm_lapic_set_base(vcpu,
1847 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001848 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001849 apic_update_ppr(apic);
1850
Gleb Natapove1035712009-03-05 16:34:59 +02001851 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001852 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001853
Nadav Amit98eff522014-06-29 12:28:51 +03001854 apic_debug("%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001855 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001856 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001857 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001858}
1859
Eddie Dong97222cc2007-09-12 10:58:04 +03001860/*
1861 *----------------------------------------------------------------------
1862 * timer interface
1863 *----------------------------------------------------------------------
1864 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001865
Avi Kivity2a6eac92012-07-26 18:01:51 +03001866static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001867{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001868 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001869}
1870
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001871int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1872{
Gleb Natapov54e98182012-08-05 15:58:32 +03001873 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001874
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001875 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001876 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001877
1878 return 0;
1879}
1880
Avi Kivity89342082011-11-10 14:57:21 +02001881int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001882{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001883 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001884 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001885
Gleb Natapovc48f1492012-08-05 15:58:33 +03001886 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001887 vector = reg & APIC_VECTOR_MASK;
1888 mode = reg & APIC_MODE_MASK;
1889 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001890 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1891 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001892 }
1893 return 0;
1894}
1895
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001896void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001897{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001898 struct kvm_lapic *apic = vcpu->arch.apic;
1899
1900 if (apic)
1901 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001902}
1903
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001904static const struct kvm_io_device_ops apic_mmio_ops = {
1905 .read = apic_mmio_read,
1906 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001907};
1908
Avi Kivitye9d90d42012-07-26 18:01:50 +03001909static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1910{
1911 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001912 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001913
Radim Krčmář5d87db72014-10-10 19:15:08 +02001914 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001915
Avi Kivity2a6eac92012-07-26 18:01:51 +03001916 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001917 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1918 return HRTIMER_RESTART;
1919 } else
1920 return HRTIMER_NORESTART;
1921}
1922
Eddie Dong97222cc2007-09-12 10:58:04 +03001923int kvm_create_lapic(struct kvm_vcpu *vcpu)
1924{
1925 struct kvm_lapic *apic;
1926
1927 ASSERT(vcpu != NULL);
1928 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1929
1930 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1931 if (!apic)
1932 goto nomem;
1933
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001934 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001935
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001936 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1937 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001938 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1939 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001940 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001941 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001942 apic->vcpu = vcpu;
1943
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001944 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001945 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001946 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001947
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001948 /*
1949 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1950 * thinking that APIC satet has changed.
1951 */
1952 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001953 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001954 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001955 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001956
1957 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001958nomem_free_apic:
1959 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001960nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001961 return -ENOMEM;
1962}
Eddie Dong97222cc2007-09-12 10:58:04 +03001963
1964int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1965{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001966 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001967 int highest_irr;
1968
Paolo Bonzinif8543d62016-01-08 13:42:24 +01001969 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001970 return -1;
1971
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001972 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001973 highest_irr = apic_find_highest_irr(apic);
1974 if ((highest_irr == -1) ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001975 ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03001976 return -1;
1977 return highest_irr;
1978}
1979
Qing He40487c62007-09-17 14:47:13 +08001980int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1981{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001982 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001983 int r = 0;
1984
Gleb Natapovc48f1492012-08-05 15:58:33 +03001985 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001986 r = 1;
1987 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1988 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1989 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001990 return r;
1991}
1992
Eddie Dong1b9778d2007-09-03 16:56:58 +03001993void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1994{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001996
Gleb Natapov54e98182012-08-05 15:58:32 +03001997 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02001998 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03001999 if (apic_lvtt_tscdeadline(apic))
2000 apic->lapic_timer.tscdeadline = 0;
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002001 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002002 }
2003}
2004
Eddie Dong97222cc2007-09-12 10:58:04 +03002005int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2006{
2007 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002009
2010 if (vector == -1)
2011 return -1;
2012
Wanpeng Li56cc2402014-08-05 12:42:24 +08002013 /*
2014 * We get here even with APIC virtualization enabled, if doing
2015 * nested virtualization and L1 runs with the "acknowledge interrupt
2016 * on exit" mode. Then we cannot inject the interrupt via RVI,
2017 * because the process would deliver it through the IDT.
2018 */
2019
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002020 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002021 apic_update_ppr(apic);
2022 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002023
2024 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
2025 apic_clear_isr(vector, apic);
2026 apic_update_ppr(apic);
2027 }
2028
Eddie Dong97222cc2007-09-12 10:58:04 +03002029 return vector;
2030}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002031
Radim Krčmářa92e2542016-07-12 22:09:22 +02002032static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2033 struct kvm_lapic_state *s, bool set)
2034{
2035 if (apic_x2apic_mode(vcpu->arch.apic)) {
2036 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002037 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002038
Radim Krčmář371313132016-07-12 22:09:27 +02002039 if (vcpu->kvm->arch.x2apic_format) {
2040 if (*id != vcpu->vcpu_id)
2041 return -EINVAL;
2042 } else {
2043 if (set)
2044 *id >>= 24;
2045 else
2046 *id <<= 24;
2047 }
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002048
2049 /* In x2APIC mode, the LDR is fixed and based on the id */
2050 if (set)
2051 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002052 }
2053
2054 return 0;
2055}
2056
2057int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2058{
2059 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2060 return kvm_apic_state_fixup(vcpu, s, false);
2061}
2062
2063int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002064{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002066 int r;
2067
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002068
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002069 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002070 /* set SPIV separately to get count of SW disabled APICs right */
2071 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002072
2073 r = kvm_apic_state_fixup(vcpu, s, true);
2074 if (r)
2075 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002076 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002077
2078 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002079 kvm_apic_set_version(vcpu);
2080
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002081 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002082 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002083 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002084 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002085 update_divide_count(apic);
2086 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002087 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002088 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002089 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002090 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002091 if (vcpu->arch.apicv_active) {
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05002092 if (kvm_x86_ops->apicv_post_state_restore)
2093 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002094 kvm_x86_ops->hwapic_irr_update(vcpu,
2095 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002096 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002097 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002098 }
Avi Kivity3842d132010-07-27 12:30:24 +03002099 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002100 if (ioapic_in_kernel(vcpu->kvm))
2101 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002102
2103 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002104
2105 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002106}
Eddie Donga3d7f852007-09-03 16:15:12 +03002107
Avi Kivity2f52d582008-01-16 12:49:30 +02002108void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002109{
Eddie Donga3d7f852007-09-03 16:15:12 +03002110 struct hrtimer *timer;
2111
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002112 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002113 return;
2114
Gleb Natapov54e98182012-08-05 15:58:32 +03002115 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002116 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002117 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002118}
Avi Kivityb93463a2007-10-25 16:52:32 +02002119
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002120/*
2121 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2122 *
2123 * Detect whether guest triggered PV EOI since the
2124 * last entry. If yes, set EOI on guests's behalf.
2125 * Clear PV EOI in guest memory in any case.
2126 */
2127static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2128 struct kvm_lapic *apic)
2129{
2130 bool pending;
2131 int vector;
2132 /*
2133 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2134 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2135 *
2136 * KVM_APIC_PV_EOI_PENDING is unset:
2137 * -> host disabled PV EOI.
2138 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2139 * -> host enabled PV EOI, guest did not execute EOI yet.
2140 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2141 * -> host enabled PV EOI, guest executed EOI.
2142 */
2143 BUG_ON(!pv_eoi_enabled(vcpu));
2144 pending = pv_eoi_get_pending(vcpu);
2145 /*
2146 * Clear pending bit in any case: it will be set again on vmentry.
2147 * While this might not be ideal from performance point of view,
2148 * this makes sure pv eoi is only enabled when we know it's safe.
2149 */
2150 pv_eoi_clr_pending(vcpu);
2151 if (pending)
2152 return;
2153 vector = apic_set_eoi(apic);
2154 trace_kvm_pv_eoi(apic, vector);
2155}
2156
Avi Kivityb93463a2007-10-25 16:52:32 +02002157void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2158{
2159 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002160
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002161 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2162 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2163
Gleb Natapov41383772012-04-19 14:06:29 +03002164 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002165 return;
2166
Nicholas Krause603242a2015-08-05 10:44:40 -04002167 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2168 sizeof(u32)))
2169 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002170
2171 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2172}
2173
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002174/*
2175 * apic_sync_pv_eoi_to_guest - called before vmentry
2176 *
2177 * Detect whether it's safe to enable PV EOI and
2178 * if yes do so.
2179 */
2180static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2181 struct kvm_lapic *apic)
2182{
2183 if (!pv_eoi_enabled(vcpu) ||
2184 /* IRR set or many bits in ISR: could be nested. */
2185 apic->irr_pending ||
2186 /* Cache not set: could be safe but we don't bother. */
2187 apic->highest_isr_cache == -1 ||
2188 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002189 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002190 /*
2191 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2192 * so we need not do anything here.
2193 */
2194 return;
2195 }
2196
2197 pv_eoi_set_pending(apic->vcpu);
2198}
2199
Avi Kivityb93463a2007-10-25 16:52:32 +02002200void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2201{
2202 u32 data, tpr;
2203 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002204 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002205
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002206 apic_sync_pv_eoi_to_guest(vcpu, apic);
2207
Gleb Natapov41383772012-04-19 14:06:29 +03002208 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002209 return;
2210
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002211 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002212 max_irr = apic_find_highest_irr(apic);
2213 if (max_irr < 0)
2214 max_irr = 0;
2215 max_isr = apic_find_highest_isr(apic);
2216 if (max_isr < 0)
2217 max_isr = 0;
2218 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2219
Andy Honigfda4e2e82013-11-20 10:23:22 -08002220 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2221 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002222}
2223
Andy Honigfda4e2e82013-11-20 10:23:22 -08002224int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002225{
Andy Honigfda4e2e82013-11-20 10:23:22 -08002226 if (vapic_addr) {
2227 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2228 &vcpu->arch.apic->vapic_cache,
2229 vapic_addr, sizeof(u32)))
2230 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002231 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002232 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002233 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002234 }
2235
2236 vcpu->arch.apic->vapic_addr = vapic_addr;
2237 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002238}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002239
2240int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2241{
2242 struct kvm_lapic *apic = vcpu->arch.apic;
2243 u32 reg = (msr - APIC_BASE_MSR) << 4;
2244
Paolo Bonzini35754c92015-07-29 12:05:37 +02002245 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002246 return 1;
2247
Nadav Amitc69d3d92014-11-26 17:56:25 +02002248 if (reg == APIC_ICR2)
2249 return 1;
2250
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002251 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002252 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002253 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2254 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002255}
2256
2257int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2258{
2259 struct kvm_lapic *apic = vcpu->arch.apic;
2260 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2261
Paolo Bonzini35754c92015-07-29 12:05:37 +02002262 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002263 return 1;
2264
Nadav Amitc69d3d92014-11-26 17:56:25 +02002265 if (reg == APIC_DFR || reg == APIC_ICR2) {
2266 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2267 reg);
2268 return 1;
2269 }
2270
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002271 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002272 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002273 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002274 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002275
2276 *data = (((u64)high) << 32) | low;
2277
2278 return 0;
2279}
Gleb Natapov10388a02010-01-17 15:51:23 +02002280
2281int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2282{
2283 struct kvm_lapic *apic = vcpu->arch.apic;
2284
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002285 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002286 return 1;
2287
2288 /* if this is ICR write vector before command */
2289 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002290 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2291 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002292}
2293
2294int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2295{
2296 struct kvm_lapic *apic = vcpu->arch.apic;
2297 u32 low, high = 0;
2298
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002299 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002300 return 1;
2301
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002302 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002303 return 1;
2304 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002305 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002306
2307 *data = (((u64)high) << 32) | low;
2308
2309 return 0;
2310}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002311
2312int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2313{
2314 u64 addr = data & ~KVM_MSR_ENABLED;
2315 if (!IS_ALIGNED(addr, 4))
2316 return 1;
2317
2318 vcpu->arch.pv_eoi.msr_val = data;
2319 if (!pv_eoi_enabled(vcpu))
2320 return 0;
2321 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002322 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002323}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002324
Jan Kiszka66450a22013-03-13 12:42:34 +01002325void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2326{
2327 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002328 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002329 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002330
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002331 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002332 return;
2333
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002334 /*
2335 * INITs are latched while in SMM. Because an SMM CPU cannot
2336 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2337 * and delay processing of INIT until the next RSM.
2338 */
2339 if (is_smm(vcpu)) {
2340 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2341 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2342 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2343 return;
2344 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002345
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002346 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002347 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002348 kvm_lapic_reset(vcpu, true);
2349 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002350 if (kvm_vcpu_is_bsp(apic->vcpu))
2351 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2352 else
2353 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2354 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002355 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002356 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2357 /* evaluate pending_events before reading the vector */
2358 smp_rmb();
2359 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002360 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002361 vcpu->vcpu_id, sipi_vector);
2362 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2363 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2364 }
2365}
2366
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002367void kvm_lapic_init(void)
2368{
2369 /* do not patch jump label more than once per second */
2370 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002371 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002372}
David Matlack5ed21cc2016-12-16 14:30:36 -08002373
2374void kvm_lapic_exit(void)
2375{
2376 static_key_deferred_flush(&apic_hw_disabled);
2377 static_key_deferred_flush(&apic_sw_disabled);
2378}