blob: 5c3d416fff17d668b5efd1270d10321c95027e4f [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{
Wanpeng Li5ea05192017-05-20 20:32:32 -07001366 preempt_disable();
Wanpeng Libd97ad02016-06-30 08:52:49 +08001367 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1368 apic->lapic_timer.hv_timer_in_use = false;
Wanpeng Li5ea05192017-05-20 20:32:32 -07001369 preempt_enable();
Wanpeng Libd97ad02016-06-30 08:52:49 +08001370}
1371
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001372void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1373{
1374 struct kvm_lapic *apic = vcpu->arch.apic;
1375
1376 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1377 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Libd97ad02016-06-30 08:52:49 +08001378 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001379 apic_timer_expired(apic);
1380}
1381EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1382
Wanpeng Li196f20c2016-06-28 14:54:19 +08001383static bool start_hv_tscdeadline(struct kvm_lapic *apic)
1384{
1385 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1386
1387 if (atomic_read(&apic->lapic_timer.pending) ||
1388 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1389 if (apic->lapic_timer.hv_timer_in_use)
1390 cancel_hv_tscdeadline(apic);
1391 } else {
1392 apic->lapic_timer.hv_timer_in_use = true;
1393 hrtimer_cancel(&apic->lapic_timer.timer);
1394
1395 /* In case the sw timer triggered in the window */
1396 if (atomic_read(&apic->lapic_timer.pending))
1397 cancel_hv_tscdeadline(apic);
1398 }
1399 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1400 apic->lapic_timer.hv_timer_in_use);
1401 return apic->lapic_timer.hv_timer_in_use;
1402}
1403
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001404void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1405{
1406 struct kvm_lapic *apic = vcpu->arch.apic;
1407
1408 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1409
Wanpeng Li196f20c2016-06-28 14:54:19 +08001410 if (apic_lvtt_tscdeadline(apic))
1411 start_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001412}
1413EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1414
1415void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1416{
1417 struct kvm_lapic *apic = vcpu->arch.apic;
1418
1419 /* Possibly the TSC deadline timer is not enabled yet */
1420 if (!apic->lapic_timer.hv_timer_in_use)
1421 return;
1422
Wanpeng Libd97ad02016-06-30 08:52:49 +08001423 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001424
1425 if (atomic_read(&apic->lapic_timer.pending))
1426 return;
1427
1428 start_sw_tscdeadline(apic);
1429}
1430EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1431
Eddie Dong97222cc2007-09-12 10:58:04 +03001432static void start_apic_timer(struct kvm_lapic *apic)
1433{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001434 ktime_t now;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001435
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001436 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001437
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001438 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001439 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001440 now = apic->lapic_timer.timer.base->get_time();
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001441 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001442 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +02001443
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001444 if (!apic->lapic_timer.period)
1445 return;
1446 /*
1447 * Do not allow the guest to program periodic timers with small
1448 * interval, since the hrtimers are not throttled by the host
1449 * scheduler.
1450 */
1451 if (apic_lvtt_period(apic)) {
1452 s64 min_period = min_timer_period_us * 1000LL;
1453
1454 if (apic->lapic_timer.period < min_period) {
1455 pr_info_ratelimited(
1456 "kvm: vcpu %i: requested %lld ns "
1457 "lapic timer period limited to %lld ns\n",
1458 apic->vcpu->vcpu_id,
1459 apic->lapic_timer.period, min_period);
1460 apic->lapic_timer.period = min_period;
1461 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001462 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001463
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001464 hrtimer_start(&apic->lapic_timer.timer,
1465 ktime_add_ns(now, apic->lapic_timer.period),
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001466 HRTIMER_MODE_ABS_PINNED);
Eddie Dong97222cc2007-09-12 10:58:04 +03001467
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001468 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001469 PRIx64 ", "
1470 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001471 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001472 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001473 kvm_lapic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001474 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001475 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001476 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001477 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li196f20c2016-06-28 14:54:19 +08001478 if (!(kvm_x86_ops->set_hv_timer && start_hv_tscdeadline(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001479 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001480 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001481}
1482
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001483static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1484{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001485 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001486
Radim Krčmář59fd1322015-06-30 22:19:16 +02001487 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1488 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1489 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001490 apic_debug("Receive NMI setting on APIC_LVT0 "
1491 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001492 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001493 } else
1494 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1495 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001496}
1497
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001498int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001499{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001500 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001501
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001502 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001503
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001504 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001505 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001506 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001507 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001508 else
1509 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001510 break;
1511
1512 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001513 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001514 apic_set_tpr(apic, val & 0xff);
1515 break;
1516
1517 case APIC_EOI:
1518 apic_set_eoi(apic);
1519 break;
1520
1521 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001522 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001523 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001524 else
1525 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001526 break;
1527
1528 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001529 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001530 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001531 recalculate_apic_map(apic->vcpu->kvm);
1532 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001533 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001534 break;
1535
Gleb Natapovfc61b802009-07-05 17:39:35 +03001536 case APIC_SPIV: {
1537 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001538 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001539 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001540 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001541 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1542 int i;
1543 u32 lvt_val;
1544
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001545 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001546 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001547 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001548 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001549 lvt_val | APIC_LVT_MASKED);
1550 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001551 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001552 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001553
1554 }
1555 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001556 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001557 case APIC_ICR:
1558 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001559 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001560 apic_send_ipi(apic);
1561 break;
1562
1563 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001564 if (!apic_x2apic_mode(apic))
1565 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001566 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001567 break;
1568
Jan Kiszka23930f92008-09-26 09:30:52 +02001569 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001570 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001571 case APIC_LVTTHMR:
1572 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001573 case APIC_LVT1:
1574 case APIC_LVTERR:
1575 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001576 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001577 val |= APIC_LVT_MASKED;
1578
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001579 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001580 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001581
1582 break;
1583
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001584 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001585 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001586 val |= APIC_LVT_MASKED;
1587 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001588 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001589 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001590 break;
1591
Eddie Dong97222cc2007-09-12 10:58:04 +03001592 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001593 if (apic_lvtt_tscdeadline(apic))
1594 break;
1595
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001596 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001597 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001598 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001599 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001600
1601 case APIC_TDCR:
1602 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001603 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001604 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001605 update_divide_count(apic);
1606 break;
1607
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001608 case APIC_ESR:
1609 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001610 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001611 ret = 1;
1612 }
1613 break;
1614
1615 case APIC_SELF_IPI:
1616 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001617 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001618 } else
1619 ret = 1;
1620 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001621 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001622 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001623 break;
1624 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001625 if (ret)
1626 apic_debug("Local APIC Write to read-only register %x\n", reg);
1627 return ret;
1628}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001629EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001630
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001631static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001632 gpa_t address, int len, const void *data)
1633{
1634 struct kvm_lapic *apic = to_lapic(this);
1635 unsigned int offset = address - apic->base_address;
1636 u32 val;
1637
1638 if (!apic_mmio_in_range(apic, address))
1639 return -EOPNOTSUPP;
1640
1641 /*
1642 * APIC register must be aligned on 128-bits boundary.
1643 * 32/64/128 bits registers must be accessed thru 32 bits.
1644 * Refer SDM 8.4.1
1645 */
1646 if (len != 4 || (offset & 0xf)) {
1647 /* Don't shout loud, $infamous_os would cause only noise. */
1648 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001649 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001650 }
1651
1652 val = *(u32*)data;
1653
1654 /* too common printing */
1655 if (offset != APIC_EOI)
1656 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1657 "0x%x\n", __func__, offset, len, val);
1658
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001659 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001660
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001661 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001662}
1663
Kevin Tian58fbbf22011-08-30 13:56:17 +03001664void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1665{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001666 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001667}
1668EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1669
Yang Zhang83d4c282013-01-25 10:18:49 +08001670/* emulate APIC access in a trap manner */
1671void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1672{
1673 u32 val = 0;
1674
1675 /* hw has done the conditional check and inst decode */
1676 offset &= 0xff0;
1677
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001678 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001679
1680 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001681 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001682}
1683EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1684
Rusty Russelld5894442007-10-08 10:48:30 +10001685void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001686{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001687 struct kvm_lapic *apic = vcpu->arch.apic;
1688
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001689 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001690 return;
1691
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001692 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001693
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001694 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1695 static_key_slow_dec_deferred(&apic_hw_disabled);
1696
Radim Krčmáře4627552014-10-30 15:06:45 +01001697 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001698 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001699
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001700 if (apic->regs)
1701 free_page((unsigned long)apic->regs);
1702
1703 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001704}
1705
1706/*
1707 *----------------------------------------------------------------------
1708 * LAPIC interface
1709 *----------------------------------------------------------------------
1710 */
1711
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001712u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1713{
1714 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001715
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001716 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001717 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001718 return 0;
1719
1720 return apic->lapic_timer.tscdeadline;
1721}
1722
1723void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1724{
1725 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001726
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001727 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001728 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001729 return;
1730
1731 hrtimer_cancel(&apic->lapic_timer.timer);
1732 apic->lapic_timer.tscdeadline = data;
1733 start_apic_timer(apic);
1734}
1735
Eddie Dong97222cc2007-09-12 10:58:04 +03001736void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1737{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001738 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001739
Avi Kivityb93463a2007-10-25 16:52:32 +02001740 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001741 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001742}
1743
1744u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1745{
Eddie Dong97222cc2007-09-12 10:58:04 +03001746 u64 tpr;
1747
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001748 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001749
1750 return (tpr & 0xf0) >> 4;
1751}
1752
1753void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1754{
Yang Zhang8d146952013-01-25 10:18:50 +08001755 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001756 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001757
1758 if (!apic) {
1759 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001760 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001761 return;
1762 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001763
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001764 vcpu->arch.apic_base = value;
1765
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001766 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001767 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001768 if (value & MSR_IA32_APICBASE_ENABLE) {
1769 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001770 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08001771 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001772 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08001773 recalculate_apic_map(vcpu->kvm);
1774 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001775 }
1776
Yang Zhang8d146952013-01-25 10:18:50 +08001777 if ((old_value ^ value) & X2APIC_ENABLE) {
1778 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001779 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001780 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1781 } else
1782 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001783 }
Yang Zhang8d146952013-01-25 10:18:50 +08001784
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001785 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001786 MSR_IA32_APICBASE_BASE;
1787
Nadav Amitdb324fe2014-11-02 11:54:59 +02001788 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1789 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1790 pr_warn_once("APIC base relocation is unsupported by KVM");
1791
Eddie Dong97222cc2007-09-12 10:58:04 +03001792 /* with FSB delivery interrupt, we can restart APIC functionality */
1793 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001794 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001795
1796}
1797
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001798void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001799{
1800 struct kvm_lapic *apic;
1801 int i;
1802
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001803 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001804
1805 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001806 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001807 ASSERT(apic != NULL);
1808
1809 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001810 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001811
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001812 if (!init_event) {
1813 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1814 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001815 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001816 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001817 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001818
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001819 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1820 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001821 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001822 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001823 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001824 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001825 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001826
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001827 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001828 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001829 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001830 if (!apic_x2apic_mode(apic))
1831 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001832 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1833 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1834 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1835 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1836 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001837 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001838 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1839 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1840 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001841 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001842 apic->irr_pending = vcpu->arch.apicv_active;
1843 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001844 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001845 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001846 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001847 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001848 kvm_lapic_set_base(vcpu,
1849 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001850 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001851 apic_update_ppr(apic);
1852
Gleb Natapove1035712009-03-05 16:34:59 +02001853 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001854 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001855
Nadav Amit98eff522014-06-29 12:28:51 +03001856 apic_debug("%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001857 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001858 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001859 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001860}
1861
Eddie Dong97222cc2007-09-12 10:58:04 +03001862/*
1863 *----------------------------------------------------------------------
1864 * timer interface
1865 *----------------------------------------------------------------------
1866 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001867
Avi Kivity2a6eac92012-07-26 18:01:51 +03001868static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001869{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001870 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001871}
1872
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001873int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1874{
Gleb Natapov54e98182012-08-05 15:58:32 +03001875 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001876
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001877 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001878 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001879
1880 return 0;
1881}
1882
Avi Kivity89342082011-11-10 14:57:21 +02001883int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001884{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001885 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001886 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001887
Gleb Natapovc48f1492012-08-05 15:58:33 +03001888 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001889 vector = reg & APIC_VECTOR_MASK;
1890 mode = reg & APIC_MODE_MASK;
1891 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001892 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1893 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001894 }
1895 return 0;
1896}
1897
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001898void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001899{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001900 struct kvm_lapic *apic = vcpu->arch.apic;
1901
1902 if (apic)
1903 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001904}
1905
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001906static const struct kvm_io_device_ops apic_mmio_ops = {
1907 .read = apic_mmio_read,
1908 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001909};
1910
Avi Kivitye9d90d42012-07-26 18:01:50 +03001911static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1912{
1913 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001914 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001915
Radim Krčmář5d87db72014-10-10 19:15:08 +02001916 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001917
Avi Kivity2a6eac92012-07-26 18:01:51 +03001918 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001919 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1920 return HRTIMER_RESTART;
1921 } else
1922 return HRTIMER_NORESTART;
1923}
1924
Eddie Dong97222cc2007-09-12 10:58:04 +03001925int kvm_create_lapic(struct kvm_vcpu *vcpu)
1926{
1927 struct kvm_lapic *apic;
1928
1929 ASSERT(vcpu != NULL);
1930 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1931
1932 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1933 if (!apic)
1934 goto nomem;
1935
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001936 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001937
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001938 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1939 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001940 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1941 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001942 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001943 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001944 apic->vcpu = vcpu;
1945
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001946 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001947 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001948 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001949
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001950 /*
1951 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1952 * thinking that APIC satet has changed.
1953 */
1954 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001955 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001956 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001957 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001958
1959 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001960nomem_free_apic:
1961 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001962nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001963 return -ENOMEM;
1964}
Eddie Dong97222cc2007-09-12 10:58:04 +03001965
1966int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1967{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001968 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001969 int highest_irr;
1970
Paolo Bonzinif8543d62016-01-08 13:42:24 +01001971 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001972 return -1;
1973
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001974 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001975 highest_irr = apic_find_highest_irr(apic);
1976 if ((highest_irr == -1) ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001977 ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03001978 return -1;
1979 return highest_irr;
1980}
1981
Qing He40487c62007-09-17 14:47:13 +08001982int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1983{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001984 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001985 int r = 0;
1986
Gleb Natapovc48f1492012-08-05 15:58:33 +03001987 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001988 r = 1;
1989 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1990 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1991 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001992 return r;
1993}
1994
Eddie Dong1b9778d2007-09-03 16:56:58 +03001995void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1996{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001997 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001998
Gleb Natapov54e98182012-08-05 15:58:32 +03001999 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002000 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002001 if (apic_lvtt_tscdeadline(apic))
2002 apic->lapic_timer.tscdeadline = 0;
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002003 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002004 }
2005}
2006
Eddie Dong97222cc2007-09-12 10:58:04 +03002007int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2008{
2009 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002010 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002011
2012 if (vector == -1)
2013 return -1;
2014
Wanpeng Li56cc2402014-08-05 12:42:24 +08002015 /*
2016 * We get here even with APIC virtualization enabled, if doing
2017 * nested virtualization and L1 runs with the "acknowledge interrupt
2018 * on exit" mode. Then we cannot inject the interrupt via RVI,
2019 * because the process would deliver it through the IDT.
2020 */
2021
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002022 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002023 apic_update_ppr(apic);
2024 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002025
2026 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
2027 apic_clear_isr(vector, apic);
2028 apic_update_ppr(apic);
2029 }
2030
Eddie Dong97222cc2007-09-12 10:58:04 +03002031 return vector;
2032}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002033
Radim Krčmářa92e2542016-07-12 22:09:22 +02002034static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2035 struct kvm_lapic_state *s, bool set)
2036{
2037 if (apic_x2apic_mode(vcpu->arch.apic)) {
2038 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002039 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002040
Radim Krčmář371313132016-07-12 22:09:27 +02002041 if (vcpu->kvm->arch.x2apic_format) {
2042 if (*id != vcpu->vcpu_id)
2043 return -EINVAL;
2044 } else {
2045 if (set)
2046 *id >>= 24;
2047 else
2048 *id <<= 24;
2049 }
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002050
2051 /* In x2APIC mode, the LDR is fixed and based on the id */
2052 if (set)
2053 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002054 }
2055
2056 return 0;
2057}
2058
2059int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2060{
2061 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2062 return kvm_apic_state_fixup(vcpu, s, false);
2063}
2064
2065int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002066{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002067 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002068 int r;
2069
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002070
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002071 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002072 /* set SPIV separately to get count of SW disabled APICs right */
2073 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002074
2075 r = kvm_apic_state_fixup(vcpu, s, true);
2076 if (r)
2077 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002078 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002079
2080 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002081 kvm_apic_set_version(vcpu);
2082
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002083 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002084 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002085 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002086 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002087 update_divide_count(apic);
2088 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002089 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002090 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002091 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002092 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002093 if (vcpu->arch.apicv_active) {
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05002094 if (kvm_x86_ops->apicv_post_state_restore)
2095 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002096 kvm_x86_ops->hwapic_irr_update(vcpu,
2097 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002098 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002099 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002100 }
Avi Kivity3842d132010-07-27 12:30:24 +03002101 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002102 if (ioapic_in_kernel(vcpu->kvm))
2103 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002104
2105 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002106
2107 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002108}
Eddie Donga3d7f852007-09-03 16:15:12 +03002109
Avi Kivity2f52d582008-01-16 12:49:30 +02002110void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002111{
Eddie Donga3d7f852007-09-03 16:15:12 +03002112 struct hrtimer *timer;
2113
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002114 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002115 return;
2116
Gleb Natapov54e98182012-08-05 15:58:32 +03002117 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002118 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002119 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002120}
Avi Kivityb93463a2007-10-25 16:52:32 +02002121
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002122/*
2123 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2124 *
2125 * Detect whether guest triggered PV EOI since the
2126 * last entry. If yes, set EOI on guests's behalf.
2127 * Clear PV EOI in guest memory in any case.
2128 */
2129static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2130 struct kvm_lapic *apic)
2131{
2132 bool pending;
2133 int vector;
2134 /*
2135 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2136 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2137 *
2138 * KVM_APIC_PV_EOI_PENDING is unset:
2139 * -> host disabled PV EOI.
2140 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2141 * -> host enabled PV EOI, guest did not execute EOI yet.
2142 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2143 * -> host enabled PV EOI, guest executed EOI.
2144 */
2145 BUG_ON(!pv_eoi_enabled(vcpu));
2146 pending = pv_eoi_get_pending(vcpu);
2147 /*
2148 * Clear pending bit in any case: it will be set again on vmentry.
2149 * While this might not be ideal from performance point of view,
2150 * this makes sure pv eoi is only enabled when we know it's safe.
2151 */
2152 pv_eoi_clr_pending(vcpu);
2153 if (pending)
2154 return;
2155 vector = apic_set_eoi(apic);
2156 trace_kvm_pv_eoi(apic, vector);
2157}
2158
Avi Kivityb93463a2007-10-25 16:52:32 +02002159void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2160{
2161 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002162
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002163 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2164 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2165
Gleb Natapov41383772012-04-19 14:06:29 +03002166 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002167 return;
2168
Nicholas Krause603242a2015-08-05 10:44:40 -04002169 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2170 sizeof(u32)))
2171 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002172
2173 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2174}
2175
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002176/*
2177 * apic_sync_pv_eoi_to_guest - called before vmentry
2178 *
2179 * Detect whether it's safe to enable PV EOI and
2180 * if yes do so.
2181 */
2182static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2183 struct kvm_lapic *apic)
2184{
2185 if (!pv_eoi_enabled(vcpu) ||
2186 /* IRR set or many bits in ISR: could be nested. */
2187 apic->irr_pending ||
2188 /* Cache not set: could be safe but we don't bother. */
2189 apic->highest_isr_cache == -1 ||
2190 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002191 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002192 /*
2193 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2194 * so we need not do anything here.
2195 */
2196 return;
2197 }
2198
2199 pv_eoi_set_pending(apic->vcpu);
2200}
2201
Avi Kivityb93463a2007-10-25 16:52:32 +02002202void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2203{
2204 u32 data, tpr;
2205 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002206 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002207
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002208 apic_sync_pv_eoi_to_guest(vcpu, apic);
2209
Gleb Natapov41383772012-04-19 14:06:29 +03002210 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002211 return;
2212
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002213 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002214 max_irr = apic_find_highest_irr(apic);
2215 if (max_irr < 0)
2216 max_irr = 0;
2217 max_isr = apic_find_highest_isr(apic);
2218 if (max_isr < 0)
2219 max_isr = 0;
2220 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2221
Andy Honigfda4e2e82013-11-20 10:23:22 -08002222 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2223 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002224}
2225
Andy Honigfda4e2e82013-11-20 10:23:22 -08002226int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002227{
Andy Honigfda4e2e82013-11-20 10:23:22 -08002228 if (vapic_addr) {
2229 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2230 &vcpu->arch.apic->vapic_cache,
2231 vapic_addr, sizeof(u32)))
2232 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002233 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002234 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002235 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002236 }
2237
2238 vcpu->arch.apic->vapic_addr = vapic_addr;
2239 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002240}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002241
2242int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2243{
2244 struct kvm_lapic *apic = vcpu->arch.apic;
2245 u32 reg = (msr - APIC_BASE_MSR) << 4;
2246
Paolo Bonzini35754c92015-07-29 12:05:37 +02002247 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002248 return 1;
2249
Nadav Amitc69d3d92014-11-26 17:56:25 +02002250 if (reg == APIC_ICR2)
2251 return 1;
2252
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002253 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002254 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002255 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2256 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002257}
2258
2259int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2260{
2261 struct kvm_lapic *apic = vcpu->arch.apic;
2262 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2263
Paolo Bonzini35754c92015-07-29 12:05:37 +02002264 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002265 return 1;
2266
Nadav Amitc69d3d92014-11-26 17:56:25 +02002267 if (reg == APIC_DFR || reg == APIC_ICR2) {
2268 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2269 reg);
2270 return 1;
2271 }
2272
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002273 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002274 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002275 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002276 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002277
2278 *data = (((u64)high) << 32) | low;
2279
2280 return 0;
2281}
Gleb Natapov10388a02010-01-17 15:51:23 +02002282
2283int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2284{
2285 struct kvm_lapic *apic = vcpu->arch.apic;
2286
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002287 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002288 return 1;
2289
2290 /* if this is ICR write vector before command */
2291 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002292 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2293 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002294}
2295
2296int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2297{
2298 struct kvm_lapic *apic = vcpu->arch.apic;
2299 u32 low, high = 0;
2300
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002301 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002302 return 1;
2303
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002304 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002305 return 1;
2306 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002307 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002308
2309 *data = (((u64)high) << 32) | low;
2310
2311 return 0;
2312}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002313
2314int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2315{
2316 u64 addr = data & ~KVM_MSR_ENABLED;
2317 if (!IS_ALIGNED(addr, 4))
2318 return 1;
2319
2320 vcpu->arch.pv_eoi.msr_val = data;
2321 if (!pv_eoi_enabled(vcpu))
2322 return 0;
2323 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002324 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002325}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002326
Jan Kiszka66450a22013-03-13 12:42:34 +01002327void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2328{
2329 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002330 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002331 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002332
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002333 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002334 return;
2335
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002336 /*
2337 * INITs are latched while in SMM. Because an SMM CPU cannot
2338 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2339 * and delay processing of INIT until the next RSM.
2340 */
2341 if (is_smm(vcpu)) {
2342 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2343 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2344 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2345 return;
2346 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002347
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002348 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002349 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002350 kvm_lapic_reset(vcpu, true);
2351 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002352 if (kvm_vcpu_is_bsp(apic->vcpu))
2353 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2354 else
2355 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2356 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002357 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002358 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2359 /* evaluate pending_events before reading the vector */
2360 smp_rmb();
2361 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002362 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002363 vcpu->vcpu_id, sipi_vector);
2364 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2365 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2366 }
2367}
2368
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002369void kvm_lapic_init(void)
2370{
2371 /* do not patch jump label more than once per second */
2372 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002373 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002374}
David Matlack5ed21cc2016-12-16 14:30:36 -08002375
2376void kvm_lapic_exit(void)
2377{
2378 static_key_deferred_flush(&apic_hw_disabled);
2379 static_key_deferred_flush(&apic_sw_disabled);
2380}