blob: e05946c36b8738e6488d0570c69c2c7b7d5b5041 [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>
28#include <linux/module.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"
Eddie Dong97222cc2007-09-12 10:58:04 +030044
Marcelo Tosattib682b812009-02-10 20:41:41 -020045#ifndef CONFIG_X86_64
46#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
47#else
48#define mod_64(x, y) ((x) % (y))
49#endif
50
Eddie Dong97222cc2007-09-12 10:58:04 +030051#define PRId64 "d"
52#define PRIx64 "llx"
53#define PRIu64 "u"
54#define PRIo64 "o"
55
56#define APIC_BUS_CYCLE_NS 1
57
58/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
59#define apic_debug(fmt, arg...)
60
61#define APIC_LVT_NUM 6
62/* 14 is the version for Xeon and Pentium 8.4.8*/
63#define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
64#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
Eddie Dong97222cc2007-09-12 10:58:04 +030075#define VEC_POS(v) ((v) & (32 - 1))
76#define REG_POS(v) (((v) >> 5) << 4)
Zhang Xiantaoad312c72007-12-13 23:50:52 +080077
Eddie Dong97222cc2007-09-12 10:58:04 +030078static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
79{
80 *((u32 *) (apic->regs + reg_off)) = val;
81}
82
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030083static inline int apic_test_vector(int vec, void *bitmap)
84{
85 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
86}
87
Yang Zhang10606912013-04-11 19:21:38 +080088bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
89{
90 struct kvm_lapic *apic = vcpu->arch.apic;
91
92 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
93 apic_test_vector(vector, apic->regs + APIC_IRR);
94}
95
Eddie Dong97222cc2007-09-12 10:58:04 +030096static inline void apic_set_vector(int vec, void *bitmap)
97{
98 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
99}
100
101static inline void apic_clear_vector(int vec, void *bitmap)
102{
103 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
104}
105
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300106static inline int __apic_test_and_set_vector(int vec, void *bitmap)
107{
108 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
109}
110
111static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
112{
113 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
114}
115
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300116struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300117struct static_key_deferred apic_sw_disabled __read_mostly;
118
Eddie Dong97222cc2007-09-12 10:58:04 +0300119static inline int apic_enabled(struct kvm_lapic *apic)
120{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300121 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300122}
123
Eddie Dong97222cc2007-09-12 10:58:04 +0300124#define LVT_MASK \
125 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
126
127#define LINT_MASK \
128 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
129 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
130
131static inline int kvm_apic_id(struct kvm_lapic *apic)
132{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300133 return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
Eddie Dong97222cc2007-09-12 10:58:04 +0300134}
135
Radim Krčmář3548a252015-02-12 19:41:33 +0100136/* The logical map is definitely wrong if we have multiple
137 * modes at the same time. (Physical map is always right.)
138 */
139static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
140{
141 return !(map->mode & (map->mode - 1));
142}
143
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100144static inline void
145apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
146{
147 unsigned lid_bits;
148
149 BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_CLUSTER != 4);
150 BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_FLAT != 8);
151 BUILD_BUG_ON(KVM_APIC_MODE_X2APIC != 16);
152 lid_bits = map->mode;
153
154 *cid = dest_id >> lid_bits;
155 *lid = dest_id & ((1 << lid_bits) - 1);
156}
157
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300158static void recalculate_apic_map(struct kvm *kvm)
159{
160 struct kvm_apic_map *new, *old = NULL;
161 struct kvm_vcpu *vcpu;
162 int i;
163
164 new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
165
166 mutex_lock(&kvm->arch.apic_map_lock);
167
168 if (!new)
169 goto out;
170
Nadav Amit173beed2014-11-02 11:54:54 +0200171 kvm_for_each_vcpu(i, vcpu, kvm) {
172 struct kvm_lapic *apic = vcpu->arch.apic;
173 u16 cid, lid;
Radim Krčmář25995e52014-11-27 23:30:19 +0100174 u32 ldr, aid;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300175
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100176 if (!kvm_apic_present(vcpu))
177 continue;
178
Radim Krčmář25995e52014-11-27 23:30:19 +0100179 aid = kvm_apic_id(apic);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300180 ldr = kvm_apic_get_reg(apic, APIC_LDR);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300181
Radim Krčmář25995e52014-11-27 23:30:19 +0100182 if (aid < ARRAY_SIZE(new->phys_map))
183 new->phys_map[aid] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100184
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100185 if (apic_x2apic_mode(apic)) {
186 new->mode |= KVM_APIC_MODE_X2APIC;
187 } else if (ldr) {
188 ldr = GET_APIC_LOGICAL_ID(ldr);
189 if (kvm_apic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
190 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
191 else
192 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
193 }
194
195 if (!kvm_apic_logical_map_valid(new))
Radim Krčmář3548a252015-02-12 19:41:33 +0100196 continue;
197
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100198 apic_logical_id(new, ldr, &cid, &lid);
199
Radim Krčmář25995e52014-11-27 23:30:19 +0100200 if (lid && cid < ARRAY_SIZE(new->logical_map))
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300201 new->logical_map[cid][ffs(lid) - 1] = apic;
202 }
203out:
204 old = rcu_dereference_protected(kvm->arch.apic_map,
205 lockdep_is_held(&kvm->arch.apic_map_lock));
206 rcu_assign_pointer(kvm->arch.apic_map, new);
207 mutex_unlock(&kvm->arch.apic_map_lock);
208
209 if (old)
210 kfree_rcu(old, rcu);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800211
Steve Rutherford49df6392015-07-29 23:21:40 -0700212 if (ioapic_in_kernel(kvm))
213 kvm_vcpu_request_scan_ioapic(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300214}
215
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300216static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
217{
Radim Krčmáře4627552014-10-30 15:06:45 +0100218 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300219
220 apic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100221
222 if (enabled != apic->sw_enabled) {
223 apic->sw_enabled = enabled;
224 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300225 static_key_slow_dec_deferred(&apic_sw_disabled);
226 recalculate_apic_map(apic->vcpu->kvm);
227 } else
228 static_key_slow_inc(&apic_sw_disabled.key);
229 }
230}
231
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300232static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
233{
234 apic_set_reg(apic, APIC_ID, id << 24);
235 recalculate_apic_map(apic->vcpu->kvm);
236}
237
238static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
239{
240 apic_set_reg(apic, APIC_LDR, id);
241 recalculate_apic_map(apic->vcpu->kvm);
242}
243
Radim Krčmář257b9a52015-05-22 18:45:11 +0200244static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u8 id)
245{
246 u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
247
248 apic_set_reg(apic, APIC_ID, id << 24);
249 apic_set_reg(apic, APIC_LDR, ldr);
250 recalculate_apic_map(apic->vcpu->kvm);
251}
252
Eddie Dong97222cc2007-09-12 10:58:04 +0300253static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
254{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300255 return !(kvm_apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300256}
257
258static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
259{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300260 return kvm_apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300261}
262
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800263static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
264{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100265 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800266}
267
Eddie Dong97222cc2007-09-12 10:58:04 +0300268static inline int apic_lvtt_period(struct kvm_lapic *apic)
269{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100270 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800271}
272
273static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
274{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100275 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300276}
277
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200278static inline int apic_lvt_nmi_mode(u32 lvt_val)
279{
280 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
281}
282
Gleb Natapovfc61b802009-07-05 17:39:35 +0300283void kvm_apic_set_version(struct kvm_vcpu *vcpu)
284{
285 struct kvm_lapic *apic = vcpu->arch.apic;
286 struct kvm_cpuid_entry2 *feat;
287 u32 v = APIC_VERSION;
288
Gleb Natapovc48f1492012-08-05 15:58:33 +0300289 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300290 return;
291
292 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
293 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
294 v |= APIC_LVR_DIRECTED_EOI;
295 apic_set_reg(apic, APIC_LVR, v);
296}
297
Mathias Krausef1d24832012-08-30 01:30:18 +0200298static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800299 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300300 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
301 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
302 LINT_MASK, LINT_MASK, /* LVT0-1 */
303 LVT_MASK /* LVTERR */
304};
305
306static int find_highest_vector(void *bitmap)
307{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900308 int vec;
309 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300310
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900311 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
312 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
313 reg = bitmap + REG_POS(vec);
314 if (*reg)
315 return fls(*reg) - 1 + vec;
316 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300317
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900318 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300319}
320
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300321static u8 count_vectors(void *bitmap)
322{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900323 int vec;
324 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300325 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900326
327 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
328 reg = bitmap + REG_POS(vec);
329 count += hweight32(*reg);
330 }
331
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300332 return count;
333}
334
Wincy Van705699a2015-02-03 23:58:17 +0800335void __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800336{
337 u32 i, pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800338
339 for (i = 0; i <= 7; i++) {
340 pir_val = xchg(&pir[i], 0);
341 if (pir_val)
Wincy Van705699a2015-02-03 23:58:17 +0800342 *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800343 }
344}
Wincy Van705699a2015-02-03 23:58:17 +0800345EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
346
347void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
348{
349 struct kvm_lapic *apic = vcpu->arch.apic;
350
351 __kvm_apic_update_irr(pir, apic->regs);
352}
Yang Zhanga20ed542013-04-11 19:25:15 +0800353EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
354
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200355static inline void apic_set_irr(int vec, struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300356{
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200357 apic_set_vector(vec, apic->regs + APIC_IRR);
Nadav Amitf210f752014-11-16 23:49:07 +0200358 /*
359 * irr_pending must be true if any interrupt is pending; set it after
360 * APIC_IRR to avoid race with apic_clear_irr
361 */
362 apic->irr_pending = true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300363}
364
Gleb Natapov33e4c682009-06-11 11:06:51 +0300365static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300366{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300367 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300368}
369
370static inline int apic_find_highest_irr(struct kvm_lapic *apic)
371{
372 int result;
373
Yang Zhangc7c9c562013-01-25 10:18:51 +0800374 /*
375 * Note that irr_pending is just a hint. It will be always
376 * true with virtual interrupt delivery enabled.
377 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300378 if (!apic->irr_pending)
379 return -1;
380
Yang Zhang5a717852013-04-11 19:25:16 +0800381 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
Gleb Natapov33e4c682009-06-11 11:06:51 +0300382 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300383 ASSERT(result == -1 || result >= 16);
384
385 return result;
386}
387
Gleb Natapov33e4c682009-06-11 11:06:51 +0300388static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
389{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800390 struct kvm_vcpu *vcpu;
391
392 vcpu = apic->vcpu;
393
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +0200394 if (unlikely(kvm_vcpu_apic_vid_enabled(vcpu))) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800395 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200396 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800397 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200398 } else {
399 apic->irr_pending = false;
400 apic_clear_vector(vec, apic->regs + APIC_IRR);
401 if (apic_search_irr(apic) != -1)
402 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800403 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300404}
405
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300406static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
407{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800408 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200409
Wanpeng Li56cc2402014-08-05 12:42:24 +0800410 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
411 return;
412
413 vcpu = apic->vcpu;
414
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300415 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800416 * With APIC virtualization enabled, all caching is disabled
417 * because the processor can modify ISR under the hood. Instead
418 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300419 */
Tiejun Chenb4eef9b2014-12-22 10:32:57 +0100420 if (unlikely(kvm_x86_ops->hwapic_isr_update))
Wanpeng Li56cc2402014-08-05 12:42:24 +0800421 kvm_x86_ops->hwapic_isr_update(vcpu->kvm, vec);
422 else {
423 ++apic->isr_count;
424 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
425 /*
426 * ISR (in service register) bit is set when injecting an interrupt.
427 * The highest vector is injected. Thus the latest bit set matches
428 * the highest bit in ISR.
429 */
430 apic->highest_isr_cache = vec;
431 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300432}
433
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200434static inline int apic_find_highest_isr(struct kvm_lapic *apic)
435{
436 int result;
437
438 /*
439 * Note that isr_count is always 1, and highest_isr_cache
440 * is always -1, with APIC virtualization enabled.
441 */
442 if (!apic->isr_count)
443 return -1;
444 if (likely(apic->highest_isr_cache != -1))
445 return apic->highest_isr_cache;
446
447 result = find_highest_vector(apic->regs + APIC_ISR);
448 ASSERT(result == -1 || result >= 16);
449
450 return result;
451}
452
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300453static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
454{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200455 struct kvm_vcpu *vcpu;
456 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
457 return;
458
459 vcpu = apic->vcpu;
460
461 /*
462 * We do get here for APIC virtualization enabled if the guest
463 * uses the Hyper-V APIC enlightenment. In this case we may need
464 * to trigger a new interrupt delivery by writing the SVI field;
465 * on the other hand isr_count and highest_isr_cache are unused
466 * and must be left alone.
467 */
Tiejun Chenb4eef9b2014-12-22 10:32:57 +0100468 if (unlikely(kvm_x86_ops->hwapic_isr_update))
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200469 kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
470 apic_find_highest_isr(apic));
471 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300472 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200473 BUG_ON(apic->isr_count < 0);
474 apic->highest_isr_cache = -1;
475 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300476}
477
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800478int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
479{
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800480 int highest_irr;
481
Gleb Natapov33e4c682009-06-11 11:06:51 +0300482 /* This may race with setting of irr in __apic_accept_irq() and
483 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
484 * will cause vmexit immediately and the value will be recalculated
485 * on the next vmentry.
486 */
Gleb Natapovc48f1492012-08-05 15:58:33 +0300487 if (!kvm_vcpu_has_lapic(vcpu))
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800488 return 0;
Gleb Natapov54e98182012-08-05 15:58:32 +0300489 highest_irr = apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800490
491 return highest_irr;
492}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800493
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200494static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800495 int vector, int level, int trig_mode,
496 unsigned long *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200497
Yang Zhangb4f22252013-04-11 19:21:37 +0800498int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
499 unsigned long *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300500{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800501 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800502
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200503 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800504 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300505}
506
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300507static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
508{
509
510 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
511 sizeof(val));
512}
513
514static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
515{
516
517 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
518 sizeof(*val));
519}
520
521static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
522{
523 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
524}
525
526static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
527{
528 u8 val;
529 if (pv_eoi_get_user(vcpu, &val) < 0)
530 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800531 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300532 return val & 0x1;
533}
534
535static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
536{
537 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
538 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800539 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300540 return;
541 }
542 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
543}
544
545static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
546{
547 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
548 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800549 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300550 return;
551 }
552 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
553}
554
Eddie Dong97222cc2007-09-12 10:58:04 +0300555static void apic_update_ppr(struct kvm_lapic *apic)
556{
Avi Kivity3842d132010-07-27 12:30:24 +0300557 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300558 int isr;
559
Gleb Natapovc48f1492012-08-05 15:58:33 +0300560 old_ppr = kvm_apic_get_reg(apic, APIC_PROCPRI);
561 tpr = kvm_apic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300562 isr = apic_find_highest_isr(apic);
563 isrv = (isr != -1) ? isr : 0;
564
565 if ((tpr & 0xf0) >= (isrv & 0xf0))
566 ppr = tpr & 0xff;
567 else
568 ppr = isrv & 0xf0;
569
570 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
571 apic, ppr, isr, isrv);
572
Avi Kivity3842d132010-07-27 12:30:24 +0300573 if (old_ppr != ppr) {
574 apic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200575 if (ppr < old_ppr)
576 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300577 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300578}
579
580static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
581{
582 apic_set_reg(apic, APIC_TASKPRI, tpr);
583 apic_update_ppr(apic);
584}
585
Radim Krčmář03d22492015-02-12 19:41:31 +0100586static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300587{
Radim Krčmář03d22492015-02-12 19:41:31 +0100588 if (apic_x2apic_mode(apic))
589 return mda == X2APIC_BROADCAST;
590
591 return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST;
Eddie Dong97222cc2007-09-12 10:58:04 +0300592}
593
Radim Krčmář03d22492015-02-12 19:41:31 +0100594static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300595{
Radim Krčmář03d22492015-02-12 19:41:31 +0100596 if (kvm_apic_broadcast(apic, mda))
597 return true;
598
599 if (apic_x2apic_mode(apic))
600 return mda == kvm_apic_id(apic);
601
602 return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic));
Nadav Amit394457a2014-10-03 00:30:52 +0300603}
604
Radim Krčmář52c233a2015-01-29 22:48:48 +0100605static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300606{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300607 u32 logical_id;
608
Nadav Amit394457a2014-10-03 00:30:52 +0300609 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100610 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300611
Radim Krčmář9368b562015-01-29 22:48:49 +0100612 logical_id = kvm_apic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300613
Radim Krčmář9368b562015-01-29 22:48:49 +0100614 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100615 return ((logical_id >> 16) == (mda >> 16))
616 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100617
618 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Radim Krčmář03d22492015-02-12 19:41:31 +0100619 mda = GET_APIC_DEST_FIELD(mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300620
Gleb Natapovc48f1492012-08-05 15:58:33 +0300621 switch (kvm_apic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300622 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100623 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300624 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100625 return ((logical_id >> 4) == (mda >> 4))
626 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300627 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200628 apic_debug("Bad DFR vcpu %d: %08x\n",
Gleb Natapovc48f1492012-08-05 15:58:33 +0300629 apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100630 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300631 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300632}
633
Radim Krčmář03d22492015-02-12 19:41:31 +0100634/* KVM APIC implementation has two quirks
635 * - dest always begins at 0 while xAPIC MDA has offset 24,
636 * - IOxAPIC messages have to be delivered (directly) to x2APIC.
637 */
638static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
639 struct kvm_lapic *target)
640{
641 bool ipi = source != NULL;
642 bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
643
644 if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
645 return X2APIC_BROADCAST;
646
647 return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
648}
649
Radim Krčmář52c233a2015-01-29 22:48:48 +0100650bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300651 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300652{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800653 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmář03d22492015-02-12 19:41:31 +0100654 u32 mda = kvm_apic_mda(dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300655
656 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200657 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300658 target, source, dest, dest_mode, short_hand);
659
Zachary Amsdenbd371392010-06-14 11:42:15 -1000660 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300661 switch (short_hand) {
662 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100663 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100664 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200665 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100666 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300667 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100668 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300669 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100670 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300671 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100672 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300673 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200674 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
675 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100676 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300677 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300678}
679
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300680bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Yang Zhangb4f22252013-04-11 19:21:37 +0800681 struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300682{
683 struct kvm_apic_map *map;
684 unsigned long bitmap = 1;
685 struct kvm_lapic **dst;
686 int i;
Paolo Bonzinibea15422015-04-13 15:40:02 +0200687 bool ret, x2apic_ipi;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300688
689 *r = -1;
690
691 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800692 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300693 return true;
694 }
695
696 if (irq->shorthand)
697 return false;
698
Paolo Bonzinibea15422015-04-13 15:40:02 +0200699 x2apic_ipi = src && apic_x2apic_mode(src);
Radim Krčmář9ea369b2015-02-12 19:41:32 +0100700 if (irq->dest_id == (x2apic_ipi ? X2APIC_BROADCAST : APIC_BROADCAST))
701 return false;
702
Paolo Bonzinibea15422015-04-13 15:40:02 +0200703 ret = true;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300704 rcu_read_lock();
705 map = rcu_dereference(kvm->arch.apic_map);
706
Paolo Bonzinibea15422015-04-13 15:40:02 +0200707 if (!map) {
708 ret = false;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300709 goto out;
Paolo Bonzinibea15422015-04-13 15:40:02 +0200710 }
Radim Krčmář698f9752014-11-27 20:03:14 +0100711
Radim Krčmář3697f302015-01-29 22:48:50 +0100712 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmářfa834e92014-11-27 20:03:12 +0100713 if (irq->dest_id >= ARRAY_SIZE(map->phys_map))
714 goto out;
715
716 dst = &map->phys_map[irq->dest_id];
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300717 } else {
Radim Krčmář3548a252015-02-12 19:41:33 +0100718 u16 cid;
719
720 if (!kvm_apic_logical_map_valid(map)) {
721 ret = false;
722 goto out;
723 }
724
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100725 apic_logical_id(map, irq->dest_id, &cid, (u16 *)&bitmap);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300726
Radim Krčmář45c30942014-11-27 20:03:13 +0100727 if (cid >= ARRAY_SIZE(map->logical_map))
728 goto out;
729
730 dst = map->logical_map[cid];
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300731
James Sullivand1ebdbf2015-03-18 19:26:04 -0600732 if (kvm_lowest_prio_delivery(irq)) {
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300733 int l = -1;
734 for_each_set_bit(i, &bitmap, 16) {
735 if (!dst[i])
736 continue;
737 if (l < 0)
738 l = i;
739 else if (kvm_apic_compare_prio(dst[i]->vcpu, dst[l]->vcpu) < 0)
740 l = i;
741 }
742
743 bitmap = (l >= 0) ? 1 << l : 0;
744 }
745 }
746
747 for_each_set_bit(i, &bitmap, 16) {
748 if (!dst[i])
749 continue;
750 if (*r < 0)
751 *r = 0;
Yang Zhangb4f22252013-04-11 19:21:37 +0800752 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300753 }
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300754out:
755 rcu_read_unlock();
756 return ret;
757}
758
Eddie Dong97222cc2007-09-12 10:58:04 +0300759/*
760 * Add a pending IRQ into lapic.
761 * Return 1 if successfully added and 0 if discarded.
762 */
763static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800764 int vector, int level, int trig_mode,
765 unsigned long *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300766{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200767 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300768 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300769
Paolo Bonzinia183b632014-09-11 11:51:02 +0200770 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
771 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300772 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300773 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200774 vcpu->arch.apic_arb_prio++;
775 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200776 if (unlikely(trig_mode && !level))
777 break;
778
Eddie Dong97222cc2007-09-12 10:58:04 +0300779 /* FIXME add logic for vcpu on reset */
780 if (unlikely(!apic_enabled(apic)))
781 break;
782
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200783 result = 1;
784
Yang Zhangb4f22252013-04-11 19:21:37 +0800785 if (dest_map)
786 __set_bit(vcpu->vcpu_id, dest_map);
Avi Kivitya5d36f82009-12-29 12:42:16 +0200787
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200788 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
789 if (trig_mode)
790 apic_set_vector(vector, apic->regs + APIC_TMR);
791 else
792 apic_clear_vector(vector, apic->regs + APIC_TMR);
793 }
794
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200795 if (kvm_x86_ops->deliver_posted_interrupt)
Yang Zhang5a717852013-04-11 19:25:16 +0800796 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200797 else {
798 apic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800799
800 kvm_make_request(KVM_REQ_EVENT, vcpu);
801 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300802 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300803 break;
804
805 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530806 result = 1;
807 vcpu->arch.pv.pv_unhalted = 1;
808 kvm_make_request(KVM_REQ_EVENT, vcpu);
809 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300810 break;
811
812 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200813 result = 1;
814 kvm_make_request(KVM_REQ_SMI, vcpu);
815 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300816 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800817
Eddie Dong97222cc2007-09-12 10:58:04 +0300818 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200819 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800820 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200821 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300822 break;
823
824 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100825 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200826 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100827 /* assumes that there are only KVM_APIC_INIT/SIPI */
828 apic->pending_events = (1UL << KVM_APIC_INIT);
829 /* make sure pending_events is visible before sending
830 * the request */
831 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300832 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300833 kvm_vcpu_kick(vcpu);
834 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200835 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
836 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300837 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300838 break;
839
840 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200841 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
842 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100843 result = 1;
844 apic->sipi_vector = vector;
845 /* make sure sipi_vector is visible for the receiver */
846 smp_wmb();
847 set_bit(KVM_APIC_SIPI, &apic->pending_events);
848 kvm_make_request(KVM_REQ_EVENT, vcpu);
849 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300850 break;
851
Jan Kiszka23930f92008-09-26 09:30:52 +0200852 case APIC_DM_EXTINT:
853 /*
854 * Should only be called by kvm_apic_local_deliver() with LVT0,
855 * before NMI watchdog was enabled. Already handled by
856 * kvm_apic_accept_pic_intr().
857 */
858 break;
859
Eddie Dong97222cc2007-09-12 10:58:04 +0300860 default:
861 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
862 delivery_mode);
863 break;
864 }
865 return result;
866}
867
Gleb Natapove1035712009-03-05 16:34:59 +0200868int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +0300869{
Gleb Natapove1035712009-03-05 16:34:59 +0200870 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800871}
872
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200873static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
874{
875 return test_bit(vector, (ulong *)apic->vcpu->arch.eoi_exit_bitmap);
876}
877
Yang Zhangc7c9c562013-01-25 10:18:51 +0800878static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
879{
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200880 if (kvm_ioapic_handles_vector(apic, vector)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +0800881 int trigger_mode;
882 if (apic_test_vector(vector, apic->regs + APIC_TMR))
883 trigger_mode = IOAPIC_LEVEL_TRIG;
884 else
885 trigger_mode = IOAPIC_EDGE_TRIG;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200886
Yang Zhang1fcc7892013-04-11 19:21:35 +0800887 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800888 }
889}
890
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300891static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300892{
893 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300894
895 trace_kvm_eoi(apic, vector);
896
Eddie Dong97222cc2007-09-12 10:58:04 +0300897 /*
898 * Not every write EOI will has corresponding ISR,
899 * one example is when Kernel check timer on setup_IO_APIC
900 */
901 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300902 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +0300903
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300904 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300905 apic_update_ppr(apic);
906
Yang Zhangc7c9c562013-01-25 10:18:51 +0800907 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +0300908 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300909 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +0300910}
911
Yang Zhangc7c9c562013-01-25 10:18:51 +0800912/*
913 * this interface assumes a trap-like exit, which has already finished
914 * desired side effect including vISR and vPPR update.
915 */
916void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
917{
918 struct kvm_lapic *apic = vcpu->arch.apic;
919
920 trace_kvm_eoi(apic, vector);
921
922 kvm_ioapic_send_eoi(apic, vector);
923 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
924}
925EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
926
Eddie Dong97222cc2007-09-12 10:58:04 +0300927static void apic_send_ipi(struct kvm_lapic *apic)
928{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300929 u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
930 u32 icr_high = kvm_apic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200931 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +0300932
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200933 irq.vector = icr_low & APIC_VECTOR_MASK;
934 irq.delivery_mode = icr_low & APIC_MODE_MASK;
935 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +0200936 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200937 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
938 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -0600939 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300940 if (apic_x2apic_mode(apic))
941 irq.dest_id = icr_high;
942 else
943 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +0300944
Gleb Natapov1000ff82009-07-07 16:00:57 +0300945 trace_kvm_apic_ipi(icr_low, irq.dest_id);
946
Eddie Dong97222cc2007-09-12 10:58:04 +0300947 apic_debug("icr_high 0x%x, icr_low 0x%x, "
948 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -0600949 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
950 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -0400951 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200952 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -0600953 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +0300954
Yang Zhangb4f22252013-04-11 19:21:37 +0800955 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +0300956}
957
958static u32 apic_get_tmcct(struct kvm_lapic *apic)
959{
Marcelo Tosattib682b812009-02-10 20:41:41 -0200960 ktime_t remaining;
961 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200962 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +0300963
964 ASSERT(apic != NULL);
965
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200966 /* if initial count is 0, current count should also be 0 */
Andy Honigb963a222013-11-19 14:12:18 -0800967 if (kvm_apic_get_reg(apic, APIC_TMICT) == 0 ||
968 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200969 return 0;
970
Marcelo Tosattiace15462009-10-08 10:55:03 -0300971 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -0200972 if (ktime_to_ns(remaining) < 0)
973 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300974
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300975 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
976 tmcct = div64_u64(ns,
977 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +0300978
979 return tmcct;
980}
981
Avi Kivityb209749f2007-10-22 16:50:39 +0200982static void __report_tpr_access(struct kvm_lapic *apic, bool write)
983{
984 struct kvm_vcpu *vcpu = apic->vcpu;
985 struct kvm_run *run = vcpu->run;
986
Avi Kivitya8eeb042010-05-10 12:34:53 +0300987 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300988 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +0200989 run->tpr_access.is_write = write;
990}
991
992static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
993{
994 if (apic->vcpu->arch.tpr_access_reporting)
995 __report_tpr_access(apic, write);
996}
997
Eddie Dong97222cc2007-09-12 10:58:04 +0300998static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
999{
1000 u32 val = 0;
1001
1002 if (offset >= LAPIC_MMIO_LENGTH)
1003 return 0;
1004
1005 switch (offset) {
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001006 case APIC_ID:
1007 if (apic_x2apic_mode(apic))
1008 val = kvm_apic_id(apic);
1009 else
1010 val = kvm_apic_id(apic) << 24;
1011 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001012 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001013 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001014 break;
1015
1016 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001017 if (apic_lvtt_tscdeadline(apic))
1018 return 0;
1019
Eddie Dong97222cc2007-09-12 10:58:04 +03001020 val = apic_get_tmcct(apic);
1021 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001022 case APIC_PROCPRI:
1023 apic_update_ppr(apic);
Gleb Natapovc48f1492012-08-05 15:58:33 +03001024 val = kvm_apic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001025 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001026 case APIC_TASKPRI:
1027 report_tpr_access(apic, false);
1028 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001029 default:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001030 val = kvm_apic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001031 break;
1032 }
1033
1034 return val;
1035}
1036
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001037static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1038{
1039 return container_of(dev, struct kvm_lapic, dev);
1040}
1041
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001042static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
1043 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001044{
Eddie Dong97222cc2007-09-12 10:58:04 +03001045 unsigned char alignment = offset & 0xf;
1046 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001047 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001048 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001049
1050 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001051 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1052 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001053 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001054 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001055
1056 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001057 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1058 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001059 return 1;
1060 }
1061
Eddie Dong97222cc2007-09-12 10:58:04 +03001062 result = __apic_read(apic, offset & ~0xf);
1063
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001064 trace_kvm_apic_read(offset, result);
1065
Eddie Dong97222cc2007-09-12 10:58:04 +03001066 switch (len) {
1067 case 1:
1068 case 2:
1069 case 4:
1070 memcpy(data, (char *)&result + alignment, len);
1071 break;
1072 default:
1073 printk(KERN_ERR "Local APIC read with len = %x, "
1074 "should be 1,2, or 4 instead\n", len);
1075 break;
1076 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001077 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001078}
1079
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001080static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1081{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001082 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001083 addr >= apic->base_address &&
1084 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1085}
1086
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001087static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001088 gpa_t address, int len, void *data)
1089{
1090 struct kvm_lapic *apic = to_lapic(this);
1091 u32 offset = address - apic->base_address;
1092
1093 if (!apic_mmio_in_range(apic, address))
1094 return -EOPNOTSUPP;
1095
1096 apic_reg_read(apic, offset, len, data);
1097
1098 return 0;
1099}
1100
Eddie Dong97222cc2007-09-12 10:58:04 +03001101static void update_divide_count(struct kvm_lapic *apic)
1102{
1103 u32 tmp1, tmp2, tdcr;
1104
Gleb Natapovc48f1492012-08-05 15:58:33 +03001105 tdcr = kvm_apic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001106 tmp1 = tdcr & 0xf;
1107 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001108 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001109
1110 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001111 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001112}
1113
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001114static void apic_update_lvtt(struct kvm_lapic *apic)
1115{
1116 u32 timer_mode = kvm_apic_get_reg(apic, APIC_LVTT) &
1117 apic->lapic_timer.timer_mode_mask;
1118
1119 if (apic->lapic_timer.timer_mode != timer_mode) {
1120 apic->lapic_timer.timer_mode = timer_mode;
1121 hrtimer_cancel(&apic->lapic_timer.timer);
1122 }
1123}
1124
Radim Krčmář5d87db72014-10-10 19:15:08 +02001125static void apic_timer_expired(struct kvm_lapic *apic)
1126{
1127 struct kvm_vcpu *vcpu = apic->vcpu;
1128 wait_queue_head_t *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001129 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001130
Radim Krčmář5d87db72014-10-10 19:15:08 +02001131 if (atomic_read(&apic->lapic_timer.pending))
1132 return;
1133
1134 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001135 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001136
1137 if (waitqueue_active(q))
1138 wake_up_interruptible(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001139
1140 if (apic_lvtt_tscdeadline(apic))
1141 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1142}
1143
1144/*
1145 * On APICv, this test will cause a busy wait
1146 * during a higher-priority task.
1147 */
1148
1149static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1150{
1151 struct kvm_lapic *apic = vcpu->arch.apic;
1152 u32 reg = kvm_apic_get_reg(apic, APIC_LVTT);
1153
1154 if (kvm_apic_hw_enabled(apic)) {
1155 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001156 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001157
Marcelo Tosattif9339862015-02-02 15:26:08 -02001158 if (kvm_x86_ops->deliver_posted_interrupt)
1159 bitmap = apic->regs + APIC_IRR;
1160
1161 if (apic_test_vector(vec, bitmap))
1162 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001163 }
1164 return false;
1165}
1166
1167void wait_lapic_expire(struct kvm_vcpu *vcpu)
1168{
1169 struct kvm_lapic *apic = vcpu->arch.apic;
1170 u64 guest_tsc, tsc_deadline;
1171
1172 if (!kvm_vcpu_has_lapic(vcpu))
1173 return;
1174
1175 if (apic->lapic_timer.expired_tscdeadline == 0)
1176 return;
1177
1178 if (!lapic_timer_int_injected(vcpu))
1179 return;
1180
1181 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1182 apic->lapic_timer.expired_tscdeadline = 0;
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001183 guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001184 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001185
1186 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1187 if (guest_tsc < tsc_deadline)
1188 __delay(tsc_deadline - guest_tsc);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001189}
1190
Eddie Dong97222cc2007-09-12 10:58:04 +03001191static void start_apic_timer(struct kvm_lapic *apic)
1192{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001193 ktime_t now;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001194
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001195 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001196
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001197 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001198 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001199 now = apic->lapic_timer.timer.base->get_time();
Gleb Natapovc48f1492012-08-05 15:58:33 +03001200 apic->lapic_timer.period = (u64)kvm_apic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001201 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +02001202
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001203 if (!apic->lapic_timer.period)
1204 return;
1205 /*
1206 * Do not allow the guest to program periodic timers with small
1207 * interval, since the hrtimers are not throttled by the host
1208 * scheduler.
1209 */
1210 if (apic_lvtt_period(apic)) {
1211 s64 min_period = min_timer_period_us * 1000LL;
1212
1213 if (apic->lapic_timer.period < min_period) {
1214 pr_info_ratelimited(
1215 "kvm: vcpu %i: requested %lld ns "
1216 "lapic timer period limited to %lld ns\n",
1217 apic->vcpu->vcpu_id,
1218 apic->lapic_timer.period, min_period);
1219 apic->lapic_timer.period = min_period;
1220 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001221 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001222
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001223 hrtimer_start(&apic->lapic_timer.timer,
1224 ktime_add_ns(now, apic->lapic_timer.period),
1225 HRTIMER_MODE_ABS);
Eddie Dong97222cc2007-09-12 10:58:04 +03001226
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001227 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001228 PRIx64 ", "
1229 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001230 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001231 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Gleb Natapovc48f1492012-08-05 15:58:33 +03001232 kvm_apic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001233 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001234 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001235 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001236 } else if (apic_lvtt_tscdeadline(apic)) {
1237 /* lapic timer in tsc deadline mode */
1238 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1239 u64 ns = 0;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001240 ktime_t expire;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001241 struct kvm_vcpu *vcpu = apic->vcpu;
Zachary Amsdencc578282012-02-03 15:43:50 -02001242 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001243 unsigned long flags;
1244
1245 if (unlikely(!tscdeadline || !this_tsc_khz))
1246 return;
1247
1248 local_irq_save(flags);
1249
1250 now = apic->lapic_timer.timer.base->get_time();
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001251 guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, rdtsc());
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001252 if (likely(tscdeadline > guest_tsc)) {
1253 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1254 do_div(ns, this_tsc_khz);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001255 expire = ktime_add_ns(now, ns);
1256 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
Radim Krčmář1e0ad702014-10-10 19:15:09 +02001257 hrtimer_start(&apic->lapic_timer.timer,
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001258 expire, HRTIMER_MODE_ABS);
Radim Krčmář1e0ad702014-10-10 19:15:09 +02001259 } else
1260 apic_timer_expired(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001261
1262 local_irq_restore(flags);
1263 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001264}
1265
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001266static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1267{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001268 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001269
Radim Krčmář59fd1322015-06-30 22:19:16 +02001270 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1271 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1272 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001273 apic_debug("Receive NMI setting on APIC_LVT0 "
1274 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001275 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001276 } else
1277 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1278 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001279}
1280
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001281static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001282{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001283 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001284
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001285 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001286
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001287 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001288 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001289 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001290 kvm_apic_set_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001291 else
1292 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001293 break;
1294
1295 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001296 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001297 apic_set_tpr(apic, val & 0xff);
1298 break;
1299
1300 case APIC_EOI:
1301 apic_set_eoi(apic);
1302 break;
1303
1304 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001305 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001306 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001307 else
1308 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001309 break;
1310
1311 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001312 if (!apic_x2apic_mode(apic)) {
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001313 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001314 recalculate_apic_map(apic->vcpu->kvm);
1315 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001316 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001317 break;
1318
Gleb Natapovfc61b802009-07-05 17:39:35 +03001319 case APIC_SPIV: {
1320 u32 mask = 0x3ff;
Gleb Natapovc48f1492012-08-05 15:58:33 +03001321 if (kvm_apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001322 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001323 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001324 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1325 int i;
1326 u32 lvt_val;
1327
1328 for (i = 0; i < APIC_LVT_NUM; i++) {
Gleb Natapovc48f1492012-08-05 15:58:33 +03001329 lvt_val = kvm_apic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001330 APIC_LVTT + 0x10 * i);
1331 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
1332 lvt_val | APIC_LVT_MASKED);
1333 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001334 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001335 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001336
1337 }
1338 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001339 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001340 case APIC_ICR:
1341 /* No delay here, so we always clear the pending bit */
1342 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
1343 apic_send_ipi(apic);
1344 break;
1345
1346 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001347 if (!apic_x2apic_mode(apic))
1348 val &= 0xff000000;
1349 apic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001350 break;
1351
Jan Kiszka23930f92008-09-26 09:30:52 +02001352 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001353 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001354 case APIC_LVTTHMR:
1355 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001356 case APIC_LVT1:
1357 case APIC_LVTERR:
1358 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001359 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001360 val |= APIC_LVT_MASKED;
1361
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001362 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
1363 apic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001364
1365 break;
1366
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001367 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001368 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001369 val |= APIC_LVT_MASKED;
1370 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
1371 apic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001372 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001373 break;
1374
Eddie Dong97222cc2007-09-12 10:58:04 +03001375 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001376 if (apic_lvtt_tscdeadline(apic))
1377 break;
1378
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001379 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001380 apic_set_reg(apic, APIC_TMICT, val);
1381 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001382 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001383
1384 case APIC_TDCR:
1385 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001386 apic_debug("KVM_WRITE:TDCR %x\n", val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001387 apic_set_reg(apic, APIC_TDCR, val);
1388 update_divide_count(apic);
1389 break;
1390
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001391 case APIC_ESR:
1392 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001393 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001394 ret = 1;
1395 }
1396 break;
1397
1398 case APIC_SELF_IPI:
1399 if (apic_x2apic_mode(apic)) {
1400 apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
1401 } else
1402 ret = 1;
1403 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001404 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001405 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001406 break;
1407 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001408 if (ret)
1409 apic_debug("Local APIC Write to read-only register %x\n", reg);
1410 return ret;
1411}
1412
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001413static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001414 gpa_t address, int len, const void *data)
1415{
1416 struct kvm_lapic *apic = to_lapic(this);
1417 unsigned int offset = address - apic->base_address;
1418 u32 val;
1419
1420 if (!apic_mmio_in_range(apic, address))
1421 return -EOPNOTSUPP;
1422
1423 /*
1424 * APIC register must be aligned on 128-bits boundary.
1425 * 32/64/128 bits registers must be accessed thru 32 bits.
1426 * Refer SDM 8.4.1
1427 */
1428 if (len != 4 || (offset & 0xf)) {
1429 /* Don't shout loud, $infamous_os would cause only noise. */
1430 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001431 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001432 }
1433
1434 val = *(u32*)data;
1435
1436 /* too common printing */
1437 if (offset != APIC_EOI)
1438 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1439 "0x%x\n", __func__, offset, len, val);
1440
1441 apic_reg_write(apic, offset & 0xff0, val);
1442
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001443 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001444}
1445
Kevin Tian58fbbf22011-08-30 13:56:17 +03001446void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1447{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001448 if (kvm_vcpu_has_lapic(vcpu))
Kevin Tian58fbbf22011-08-30 13:56:17 +03001449 apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
1450}
1451EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1452
Yang Zhang83d4c282013-01-25 10:18:49 +08001453/* emulate APIC access in a trap manner */
1454void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1455{
1456 u32 val = 0;
1457
1458 /* hw has done the conditional check and inst decode */
1459 offset &= 0xff0;
1460
1461 apic_reg_read(vcpu->arch.apic, offset, 4, &val);
1462
1463 /* TODO: optimize to just emulate side effect w/o one more write */
1464 apic_reg_write(vcpu->arch.apic, offset, val);
1465}
1466EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1467
Rusty Russelld5894442007-10-08 10:48:30 +10001468void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001469{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001470 struct kvm_lapic *apic = vcpu->arch.apic;
1471
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001472 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001473 return;
1474
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001475 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001476
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001477 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1478 static_key_slow_dec_deferred(&apic_hw_disabled);
1479
Radim Krčmáře4627552014-10-30 15:06:45 +01001480 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001481 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001482
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001483 if (apic->regs)
1484 free_page((unsigned long)apic->regs);
1485
1486 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001487}
1488
1489/*
1490 *----------------------------------------------------------------------
1491 * LAPIC interface
1492 *----------------------------------------------------------------------
1493 */
1494
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001495u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1496{
1497 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001498
Gleb Natapovc48f1492012-08-05 15:58:33 +03001499 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001500 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001501 return 0;
1502
1503 return apic->lapic_timer.tscdeadline;
1504}
1505
1506void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1507{
1508 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001509
Gleb Natapovc48f1492012-08-05 15:58:33 +03001510 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001511 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001512 return;
1513
1514 hrtimer_cancel(&apic->lapic_timer.timer);
1515 apic->lapic_timer.tscdeadline = data;
1516 start_apic_timer(apic);
1517}
1518
Eddie Dong97222cc2007-09-12 10:58:04 +03001519void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1520{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001521 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001522
Gleb Natapovc48f1492012-08-05 15:58:33 +03001523 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001524 return;
Gleb Natapov54e98182012-08-05 15:58:32 +03001525
Avi Kivityb93463a2007-10-25 16:52:32 +02001526 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Gleb Natapovc48f1492012-08-05 15:58:33 +03001527 | (kvm_apic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001528}
1529
1530u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1531{
Eddie Dong97222cc2007-09-12 10:58:04 +03001532 u64 tpr;
1533
Gleb Natapovc48f1492012-08-05 15:58:33 +03001534 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001535 return 0;
Gleb Natapov54e98182012-08-05 15:58:32 +03001536
Gleb Natapovc48f1492012-08-05 15:58:33 +03001537 tpr = (u64) kvm_apic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001538
1539 return (tpr & 0xf0) >> 4;
1540}
1541
1542void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1543{
Yang Zhang8d146952013-01-25 10:18:50 +08001544 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001545 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001546
1547 if (!apic) {
1548 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001549 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001550 return;
1551 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001552
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001553 vcpu->arch.apic_base = value;
1554
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001555 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001556 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001557 if (value & MSR_IA32_APICBASE_ENABLE)
1558 static_key_slow_dec_deferred(&apic_hw_disabled);
1559 else
1560 static_key_slow_inc(&apic_hw_disabled.key);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001561 recalculate_apic_map(vcpu->kvm);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001562 }
1563
Yang Zhang8d146952013-01-25 10:18:50 +08001564 if ((old_value ^ value) & X2APIC_ENABLE) {
1565 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001566 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001567 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1568 } else
1569 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001570 }
Yang Zhang8d146952013-01-25 10:18:50 +08001571
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001572 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001573 MSR_IA32_APICBASE_BASE;
1574
Nadav Amitdb324fe2014-11-02 11:54:59 +02001575 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1576 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1577 pr_warn_once("APIC base relocation is unsupported by KVM");
1578
Eddie Dong97222cc2007-09-12 10:58:04 +03001579 /* with FSB delivery interrupt, we can restart APIC functionality */
1580 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001581 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001582
1583}
1584
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001585void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001586{
1587 struct kvm_lapic *apic;
1588 int i;
1589
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001590 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001591
1592 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001593 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001594 ASSERT(apic != NULL);
1595
1596 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001597 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001598
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001599 if (!init_event)
1600 kvm_apic_set_id(apic, vcpu->vcpu_id);
Gleb Natapovfc61b802009-07-05 17:39:35 +03001601 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001602
1603 for (i = 0; i < APIC_LVT_NUM; i++)
1604 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001605 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001606 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Nadav Amit90de4a12015-04-13 01:53:41 +03001607 apic_set_reg(apic, APIC_LVT0,
1608 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Radim Krčmář59fd1322015-06-30 22:19:16 +02001609 apic_manage_nmi_watchdog(apic, kvm_apic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001610
1611 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001612 apic_set_spiv(apic, 0xff);
Eddie Dong97222cc2007-09-12 10:58:04 +03001613 apic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001614 if (!apic_x2apic_mode(apic))
1615 kvm_apic_set_ldr(apic, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001616 apic_set_reg(apic, APIC_ESR, 0);
1617 apic_set_reg(apic, APIC_ICR, 0);
1618 apic_set_reg(apic, APIC_ICR2, 0);
1619 apic_set_reg(apic, APIC_TDCR, 0);
1620 apic_set_reg(apic, APIC_TMICT, 0);
1621 for (i = 0; i < 8; i++) {
1622 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1623 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1624 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
1625 }
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02001626 apic->irr_pending = kvm_vcpu_apic_vid_enabled(vcpu);
Radim Krčmářf563db42015-02-27 16:32:38 +01001627 apic->isr_count = kvm_x86_ops->hwapic_isr_update ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001628 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001629 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001630 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001631 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001632 kvm_lapic_set_base(vcpu,
1633 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001634 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001635 apic_update_ppr(apic);
1636
Gleb Natapove1035712009-03-05 16:34:59 +02001637 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001638 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001639
Nadav Amit98eff522014-06-29 12:28:51 +03001640 apic_debug("%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001641 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001642 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001643 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001644}
1645
Eddie Dong97222cc2007-09-12 10:58:04 +03001646/*
1647 *----------------------------------------------------------------------
1648 * timer interface
1649 *----------------------------------------------------------------------
1650 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001651
Avi Kivity2a6eac92012-07-26 18:01:51 +03001652static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001653{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001654 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001655}
1656
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001657int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1658{
Gleb Natapov54e98182012-08-05 15:58:32 +03001659 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001660
Gleb Natapovc48f1492012-08-05 15:58:33 +03001661 if (kvm_vcpu_has_lapic(vcpu) && apic_enabled(apic) &&
Gleb Natapov54e98182012-08-05 15:58:32 +03001662 apic_lvt_enabled(apic, APIC_LVTT))
1663 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001664
1665 return 0;
1666}
1667
Avi Kivity89342082011-11-10 14:57:21 +02001668int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001669{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001670 u32 reg = kvm_apic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001671 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001672
Gleb Natapovc48f1492012-08-05 15:58:33 +03001673 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001674 vector = reg & APIC_VECTOR_MASK;
1675 mode = reg & APIC_MODE_MASK;
1676 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001677 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1678 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001679 }
1680 return 0;
1681}
1682
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001683void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001684{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001685 struct kvm_lapic *apic = vcpu->arch.apic;
1686
1687 if (apic)
1688 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001689}
1690
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001691static const struct kvm_io_device_ops apic_mmio_ops = {
1692 .read = apic_mmio_read,
1693 .write = apic_mmio_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001694};
1695
Avi Kivitye9d90d42012-07-26 18:01:50 +03001696static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1697{
1698 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001699 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001700
Radim Krčmář5d87db72014-10-10 19:15:08 +02001701 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001702
Avi Kivity2a6eac92012-07-26 18:01:51 +03001703 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001704 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1705 return HRTIMER_RESTART;
1706 } else
1707 return HRTIMER_NORESTART;
1708}
1709
Eddie Dong97222cc2007-09-12 10:58:04 +03001710int kvm_create_lapic(struct kvm_vcpu *vcpu)
1711{
1712 struct kvm_lapic *apic;
1713
1714 ASSERT(vcpu != NULL);
1715 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1716
1717 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1718 if (!apic)
1719 goto nomem;
1720
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001721 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001722
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001723 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1724 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001725 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1726 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001727 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001728 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001729 apic->vcpu = vcpu;
1730
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001731 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
1732 HRTIMER_MODE_ABS);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001733 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001734
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001735 /*
1736 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1737 * thinking that APIC satet has changed.
1738 */
1739 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapov6aed64a2012-08-05 15:58:28 +03001740 kvm_lapic_set_base(vcpu,
1741 APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE);
Eddie Dong97222cc2007-09-12 10:58:04 +03001742
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001743 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001744 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c42009-06-01 12:54:50 -04001745 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001746
1747 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001748nomem_free_apic:
1749 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001750nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001751 return -ENOMEM;
1752}
Eddie Dong97222cc2007-09-12 10:58:04 +03001753
1754int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1755{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001756 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001757 int highest_irr;
1758
Gleb Natapovc48f1492012-08-05 15:58:33 +03001759 if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001760 return -1;
1761
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001762 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001763 highest_irr = apic_find_highest_irr(apic);
1764 if ((highest_irr == -1) ||
Gleb Natapovc48f1492012-08-05 15:58:33 +03001765 ((highest_irr & 0xF0) <= kvm_apic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03001766 return -1;
1767 return highest_irr;
1768}
1769
Qing He40487c62007-09-17 14:47:13 +08001770int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1771{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001772 u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001773 int r = 0;
1774
Gleb Natapovc48f1492012-08-05 15:58:33 +03001775 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001776 r = 1;
1777 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1778 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1779 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001780 return r;
1781}
1782
Eddie Dong1b9778d2007-09-03 16:56:58 +03001783void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1784{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001785 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001786
Gleb Natapovc48f1492012-08-05 15:58:33 +03001787 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03001788 return;
1789
1790 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02001791 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03001792 if (apic_lvtt_tscdeadline(apic))
1793 apic->lapic_timer.tscdeadline = 0;
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02001794 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001795 }
1796}
1797
Eddie Dong97222cc2007-09-12 10:58:04 +03001798int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1799{
1800 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001801 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001802
1803 if (vector == -1)
1804 return -1;
1805
Wanpeng Li56cc2402014-08-05 12:42:24 +08001806 /*
1807 * We get here even with APIC virtualization enabled, if doing
1808 * nested virtualization and L1 runs with the "acknowledge interrupt
1809 * on exit" mode. Then we cannot inject the interrupt via RVI,
1810 * because the process would deliver it through the IDT.
1811 */
1812
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001813 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001814 apic_update_ppr(apic);
1815 apic_clear_irr(vector, apic);
1816 return vector;
1817}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001818
Gleb Natapov64eb0622012-08-08 15:24:36 +03001819void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
1820 struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001821{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001822 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001823
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001824 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03001825 /* set SPIV separately to get count of SW disabled APICs right */
1826 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
1827 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001828 /* call kvm_apic_set_id() to put apic into apic_map */
1829 kvm_apic_set_id(apic, kvm_apic_id(apic));
Gleb Natapovfc61b802009-07-05 17:39:35 +03001830 kvm_apic_set_version(vcpu);
1831
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001832 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001833 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001834 apic_update_lvtt(apic);
Radim Krčmářdb138562015-06-30 22:19:17 +02001835 apic_manage_nmi_watchdog(apic, kvm_apic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001836 update_divide_count(apic);
1837 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02001838 apic->irr_pending = true;
Radim Krčmářf563db42015-02-27 16:32:38 +01001839 apic->isr_count = kvm_x86_ops->hwapic_isr_update ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08001840 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001841 apic->highest_isr_cache = -1;
Wei Wang4114c272014-11-05 10:53:43 +08001842 if (kvm_x86_ops->hwapic_irr_update)
1843 kvm_x86_ops->hwapic_irr_update(vcpu,
1844 apic_find_highest_irr(apic));
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01001845 if (unlikely(kvm_x86_ops->hwapic_isr_update))
1846 kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
1847 apic_find_highest_isr(apic));
Avi Kivity3842d132010-07-27 12:30:24 +03001848 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07001849 if (ioapic_in_kernel(vcpu->kvm))
1850 kvm_rtc_eoi_tracking_restore_one(vcpu);
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001851}
Eddie Donga3d7f852007-09-03 16:15:12 +03001852
Avi Kivity2f52d582008-01-16 12:49:30 +02001853void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03001854{
Eddie Donga3d7f852007-09-03 16:15:12 +03001855 struct hrtimer *timer;
1856
Gleb Natapovc48f1492012-08-05 15:58:33 +03001857 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03001858 return;
1859
Gleb Natapov54e98182012-08-05 15:58:32 +03001860 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03001861 if (hrtimer_cancel(timer))
Arjan van de Venbeb20d522008-09-01 14:55:57 -07001862 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
Eddie Donga3d7f852007-09-03 16:15:12 +03001863}
Avi Kivityb93463a2007-10-25 16:52:32 +02001864
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001865/*
1866 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
1867 *
1868 * Detect whether guest triggered PV EOI since the
1869 * last entry. If yes, set EOI on guests's behalf.
1870 * Clear PV EOI in guest memory in any case.
1871 */
1872static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
1873 struct kvm_lapic *apic)
1874{
1875 bool pending;
1876 int vector;
1877 /*
1878 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
1879 * and KVM_PV_EOI_ENABLED in guest memory as follows:
1880 *
1881 * KVM_APIC_PV_EOI_PENDING is unset:
1882 * -> host disabled PV EOI.
1883 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
1884 * -> host enabled PV EOI, guest did not execute EOI yet.
1885 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
1886 * -> host enabled PV EOI, guest executed EOI.
1887 */
1888 BUG_ON(!pv_eoi_enabled(vcpu));
1889 pending = pv_eoi_get_pending(vcpu);
1890 /*
1891 * Clear pending bit in any case: it will be set again on vmentry.
1892 * While this might not be ideal from performance point of view,
1893 * this makes sure pv eoi is only enabled when we know it's safe.
1894 */
1895 pv_eoi_clr_pending(vcpu);
1896 if (pending)
1897 return;
1898 vector = apic_set_eoi(apic);
1899 trace_kvm_pv_eoi(apic, vector);
1900}
1901
Avi Kivityb93463a2007-10-25 16:52:32 +02001902void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
1903{
1904 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02001905
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001906 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
1907 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
1908
Gleb Natapov41383772012-04-19 14:06:29 +03001909 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02001910 return;
1911
Nicholas Krause603242a2015-08-05 10:44:40 -04001912 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
1913 sizeof(u32)))
1914 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02001915
1916 apic_set_tpr(vcpu->arch.apic, data & 0xff);
1917}
1918
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001919/*
1920 * apic_sync_pv_eoi_to_guest - called before vmentry
1921 *
1922 * Detect whether it's safe to enable PV EOI and
1923 * if yes do so.
1924 */
1925static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
1926 struct kvm_lapic *apic)
1927{
1928 if (!pv_eoi_enabled(vcpu) ||
1929 /* IRR set or many bits in ISR: could be nested. */
1930 apic->irr_pending ||
1931 /* Cache not set: could be safe but we don't bother. */
1932 apic->highest_isr_cache == -1 ||
1933 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001934 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001935 /*
1936 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
1937 * so we need not do anything here.
1938 */
1939 return;
1940 }
1941
1942 pv_eoi_set_pending(apic->vcpu);
1943}
1944
Avi Kivityb93463a2007-10-25 16:52:32 +02001945void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
1946{
1947 u32 data, tpr;
1948 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001949 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02001950
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001951 apic_sync_pv_eoi_to_guest(vcpu, apic);
1952
Gleb Natapov41383772012-04-19 14:06:29 +03001953 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02001954 return;
1955
Gleb Natapovc48f1492012-08-05 15:58:33 +03001956 tpr = kvm_apic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02001957 max_irr = apic_find_highest_irr(apic);
1958 if (max_irr < 0)
1959 max_irr = 0;
1960 max_isr = apic_find_highest_isr(apic);
1961 if (max_isr < 0)
1962 max_isr = 0;
1963 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1964
Andy Honigfda4e2e2013-11-20 10:23:22 -08001965 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
1966 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02001967}
1968
Andy Honigfda4e2e2013-11-20 10:23:22 -08001969int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02001970{
Andy Honigfda4e2e2013-11-20 10:23:22 -08001971 if (vapic_addr) {
1972 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
1973 &vcpu->arch.apic->vapic_cache,
1974 vapic_addr, sizeof(u32)))
1975 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03001976 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08001977 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03001978 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08001979 }
1980
1981 vcpu->arch.apic->vapic_addr = vapic_addr;
1982 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02001983}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001984
1985int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1986{
1987 struct kvm_lapic *apic = vcpu->arch.apic;
1988 u32 reg = (msr - APIC_BASE_MSR) << 4;
1989
Paolo Bonzini35754c92015-07-29 12:05:37 +02001990 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001991 return 1;
1992
Nadav Amitc69d3d92014-11-26 17:56:25 +02001993 if (reg == APIC_ICR2)
1994 return 1;
1995
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001996 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01001997 if (reg == APIC_ICR)
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001998 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
1999 return apic_reg_write(apic, reg, (u32)data);
2000}
2001
2002int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2003{
2004 struct kvm_lapic *apic = vcpu->arch.apic;
2005 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2006
Paolo Bonzini35754c92015-07-29 12:05:37 +02002007 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002008 return 1;
2009
Nadav Amitc69d3d92014-11-26 17:56:25 +02002010 if (reg == APIC_DFR || reg == APIC_ICR2) {
2011 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2012 reg);
2013 return 1;
2014 }
2015
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002016 if (apic_reg_read(apic, reg, 4, &low))
2017 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002018 if (reg == APIC_ICR)
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002019 apic_reg_read(apic, APIC_ICR2, 4, &high);
2020
2021 *data = (((u64)high) << 32) | low;
2022
2023 return 0;
2024}
Gleb Natapov10388a02010-01-17 15:51:23 +02002025
2026int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2027{
2028 struct kvm_lapic *apic = vcpu->arch.apic;
2029
Gleb Natapovc48f1492012-08-05 15:58:33 +03002030 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002031 return 1;
2032
2033 /* if this is ICR write vector before command */
2034 if (reg == APIC_ICR)
2035 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2036 return apic_reg_write(apic, reg, (u32)data);
2037}
2038
2039int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2040{
2041 struct kvm_lapic *apic = vcpu->arch.apic;
2042 u32 low, high = 0;
2043
Gleb Natapovc48f1492012-08-05 15:58:33 +03002044 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002045 return 1;
2046
2047 if (apic_reg_read(apic, reg, 4, &low))
2048 return 1;
2049 if (reg == APIC_ICR)
2050 apic_reg_read(apic, APIC_ICR2, 4, &high);
2051
2052 *data = (((u64)high) << 32) | low;
2053
2054 return 0;
2055}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002056
2057int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2058{
2059 u64 addr = data & ~KVM_MSR_ENABLED;
2060 if (!IS_ALIGNED(addr, 4))
2061 return 1;
2062
2063 vcpu->arch.pv_eoi.msr_val = data;
2064 if (!pv_eoi_enabled(vcpu))
2065 return 0;
2066 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002067 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002068}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002069
Jan Kiszka66450a22013-03-13 12:42:34 +01002070void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2071{
2072 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002073 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002074 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002075
Gleb Natapov299018f2013-06-03 11:30:02 +03002076 if (!kvm_vcpu_has_lapic(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002077 return;
2078
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002079 /*
2080 * INITs are latched while in SMM. Because an SMM CPU cannot
2081 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2082 * and delay processing of INIT until the next RSM.
2083 */
2084 if (is_smm(vcpu)) {
2085 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2086 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2087 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2088 return;
2089 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002090
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002091 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002092 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002093 kvm_lapic_reset(vcpu, true);
2094 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002095 if (kvm_vcpu_is_bsp(apic->vcpu))
2096 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2097 else
2098 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2099 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002100 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002101 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2102 /* evaluate pending_events before reading the vector */
2103 smp_rmb();
2104 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002105 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002106 vcpu->vcpu_id, sipi_vector);
2107 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2108 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2109 }
2110}
2111
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002112void kvm_lapic_init(void)
2113{
2114 /* do not patch jump label more than once per second */
2115 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002116 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002117}