blob: 0b734025fc87fe64526eb5e22eaca349479ac475 [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>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030037#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030039#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030040#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030041#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020042#include "cpuid.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030043
Marcelo Tosattib682b812009-02-10 20:41:41 -020044#ifndef CONFIG_X86_64
45#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
46#else
47#define mod_64(x, y) ((x) % (y))
48#endif
49
Eddie Dong97222cc2007-09-12 10:58:04 +030050#define PRId64 "d"
51#define PRIx64 "llx"
52#define PRIu64 "u"
53#define PRIo64 "o"
54
55#define APIC_BUS_CYCLE_NS 1
56
57/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
58#define apic_debug(fmt, arg...)
59
60#define APIC_LVT_NUM 6
61/* 14 is the version for Xeon and Pentium 8.4.8*/
62#define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
63#define LAPIC_MMIO_LENGTH (1 << 12)
64/* followed define is not in apicdef.h */
65#define APIC_SHORT_MASK 0xc0000
66#define APIC_DEST_NOSHORT 0x0
67#define APIC_DEST_MASK 0x800
68#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090069#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030070
71#define VEC_POS(v) ((v) & (32 - 1))
72#define REG_POS(v) (((v) >> 5) << 4)
Zhang Xiantaoad312c72007-12-13 23:50:52 +080073
Jan Kiszka9bc57912011-09-12 14:10:22 +020074static unsigned int min_timer_period_us = 500;
75module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
76
Eddie Dong97222cc2007-09-12 10:58:04 +030077static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
78{
79 *((u32 *) (apic->regs + reg_off)) = val;
80}
81
82static inline int apic_test_and_set_vector(int vec, void *bitmap)
83{
84 return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
85}
86
87static inline int apic_test_and_clear_vector(int vec, void *bitmap)
88{
89 return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
90}
91
Michael S. Tsirkina0c9a822012-04-11 18:49:55 +030092static inline int apic_test_vector(int vec, void *bitmap)
93{
94 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
95}
96
Eddie Dong97222cc2007-09-12 10:58:04 +030097static inline void apic_set_vector(int vec, void *bitmap)
98{
99 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
100}
101
102static inline void apic_clear_vector(int vec, void *bitmap)
103{
104 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
105}
106
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300107static inline int __apic_test_and_set_vector(int vec, void *bitmap)
108{
109 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
110}
111
112static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
113{
114 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
115}
116
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300117struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300118struct static_key_deferred apic_sw_disabled __read_mostly;
119
120static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +0300121{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300122 if ((kvm_apic_get_reg(apic, APIC_SPIV) ^ val) & APIC_SPIV_APIC_ENABLED) {
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300123 if (val & APIC_SPIV_APIC_ENABLED)
124 static_key_slow_dec_deferred(&apic_sw_disabled);
125 else
126 static_key_slow_inc(&apic_sw_disabled.key);
127 }
128 apic_set_reg(apic, APIC_SPIV, val);
129}
130
Eddie Dong97222cc2007-09-12 10:58:04 +0300131static inline int apic_enabled(struct kvm_lapic *apic)
132{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300133 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300134}
135
Eddie Dong97222cc2007-09-12 10:58:04 +0300136#define LVT_MASK \
137 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
138
139#define LINT_MASK \
140 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
141 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
142
143static inline int kvm_apic_id(struct kvm_lapic *apic)
144{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300145 return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
Eddie Dong97222cc2007-09-12 10:58:04 +0300146}
147
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300148static void recalculate_apic_map(struct kvm *kvm)
149{
150 struct kvm_apic_map *new, *old = NULL;
151 struct kvm_vcpu *vcpu;
152 int i;
153
154 new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
155
156 mutex_lock(&kvm->arch.apic_map_lock);
157
158 if (!new)
159 goto out;
160
161 new->ldr_bits = 8;
162 /* flat mode is default */
163 new->cid_shift = 8;
164 new->cid_mask = 0;
165 new->lid_mask = 0xff;
166
167 kvm_for_each_vcpu(i, vcpu, kvm) {
168 struct kvm_lapic *apic = vcpu->arch.apic;
169 u16 cid, lid;
170 u32 ldr;
171
172 if (!kvm_apic_present(vcpu))
173 continue;
174
175 /*
176 * All APICs have to be configured in the same mode by an OS.
177 * We take advatage of this while building logical id loockup
178 * table. After reset APICs are in xapic/flat mode, so if we
179 * find apic with different setting we assume this is the mode
180 * OS wants all apics to be in; build lookup table accordingly.
181 */
182 if (apic_x2apic_mode(apic)) {
183 new->ldr_bits = 32;
184 new->cid_shift = 16;
185 new->cid_mask = new->lid_mask = 0xffff;
186 } else if (kvm_apic_sw_enabled(apic) &&
187 !new->cid_mask /* flat mode */ &&
188 kvm_apic_get_reg(apic, APIC_DFR) == APIC_DFR_CLUSTER) {
189 new->cid_shift = 4;
190 new->cid_mask = 0xf;
191 new->lid_mask = 0xf;
192 }
193
194 new->phys_map[kvm_apic_id(apic)] = apic;
195
196 ldr = kvm_apic_get_reg(apic, APIC_LDR);
197 cid = apic_cluster_id(new, ldr);
198 lid = apic_logical_id(new, ldr);
199
200 if (lid)
201 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
212 kvm_ioapic_make_eoibitmap_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300213}
214
215static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
216{
217 apic_set_reg(apic, APIC_ID, id << 24);
218 recalculate_apic_map(apic->vcpu->kvm);
219}
220
221static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
222{
223 apic_set_reg(apic, APIC_LDR, id);
224 recalculate_apic_map(apic->vcpu->kvm);
225}
226
Eddie Dong97222cc2007-09-12 10:58:04 +0300227static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
228{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300229 return !(kvm_apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300230}
231
232static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
233{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300234 return kvm_apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300235}
236
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800237static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
238{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300239 return ((kvm_apic_get_reg(apic, APIC_LVTT) &
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800240 apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_ONESHOT);
241}
242
Eddie Dong97222cc2007-09-12 10:58:04 +0300243static inline int apic_lvtt_period(struct kvm_lapic *apic)
244{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300245 return ((kvm_apic_get_reg(apic, APIC_LVTT) &
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800246 apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_PERIODIC);
247}
248
249static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
250{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300251 return ((kvm_apic_get_reg(apic, APIC_LVTT) &
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800252 apic->lapic_timer.timer_mode_mask) ==
253 APIC_LVT_TIMER_TSCDEADLINE);
Eddie Dong97222cc2007-09-12 10:58:04 +0300254}
255
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200256static inline int apic_lvt_nmi_mode(u32 lvt_val)
257{
258 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
259}
260
Gleb Natapovfc61b802009-07-05 17:39:35 +0300261void kvm_apic_set_version(struct kvm_vcpu *vcpu)
262{
263 struct kvm_lapic *apic = vcpu->arch.apic;
264 struct kvm_cpuid_entry2 *feat;
265 u32 v = APIC_VERSION;
266
Gleb Natapovc48f1492012-08-05 15:58:33 +0300267 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300268 return;
269
270 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
271 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
272 v |= APIC_LVR_DIRECTED_EOI;
273 apic_set_reg(apic, APIC_LVR, v);
274}
275
Mathias Krausef1d24832012-08-30 01:30:18 +0200276static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800277 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300278 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
279 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
280 LINT_MASK, LINT_MASK, /* LVT0-1 */
281 LVT_MASK /* LVTERR */
282};
283
284static int find_highest_vector(void *bitmap)
285{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900286 int vec;
287 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300288
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900289 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
290 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
291 reg = bitmap + REG_POS(vec);
292 if (*reg)
293 return fls(*reg) - 1 + vec;
294 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300295
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900296 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300297}
298
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300299static u8 count_vectors(void *bitmap)
300{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900301 int vec;
302 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300303 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900304
305 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
306 reg = bitmap + REG_POS(vec);
307 count += hweight32(*reg);
308 }
309
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300310 return count;
311}
312
Eddie Dong97222cc2007-09-12 10:58:04 +0300313static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
314{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300315 apic->irr_pending = true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300316 return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
317}
318
Gleb Natapov33e4c682009-06-11 11:06:51 +0300319static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300320{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300321 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300322}
323
324static inline int apic_find_highest_irr(struct kvm_lapic *apic)
325{
326 int result;
327
Yang Zhangc7c9c562013-01-25 10:18:51 +0800328 /*
329 * Note that irr_pending is just a hint. It will be always
330 * true with virtual interrupt delivery enabled.
331 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300332 if (!apic->irr_pending)
333 return -1;
334
335 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300336 ASSERT(result == -1 || result >= 16);
337
338 return result;
339}
340
Gleb Natapov33e4c682009-06-11 11:06:51 +0300341static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
342{
343 apic->irr_pending = false;
344 apic_clear_vector(vec, apic->regs + APIC_IRR);
345 if (apic_search_irr(apic) != -1)
346 apic->irr_pending = true;
347}
348
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300349static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
350{
351 if (!__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
352 ++apic->isr_count;
353 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
354 /*
355 * ISR (in service register) bit is set when injecting an interrupt.
356 * The highest vector is injected. Thus the latest bit set matches
357 * the highest bit in ISR.
358 */
359 apic->highest_isr_cache = vec;
360}
361
362static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
363{
364 if (__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
365 --apic->isr_count;
366 BUG_ON(apic->isr_count < 0);
367 apic->highest_isr_cache = -1;
368}
369
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800370int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
371{
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800372 int highest_irr;
373
Gleb Natapov33e4c682009-06-11 11:06:51 +0300374 /* This may race with setting of irr in __apic_accept_irq() and
375 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
376 * will cause vmexit immediately and the value will be recalculated
377 * on the next vmentry.
378 */
Gleb Natapovc48f1492012-08-05 15:58:33 +0300379 if (!kvm_vcpu_has_lapic(vcpu))
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800380 return 0;
Gleb Natapov54e98182012-08-05 15:58:32 +0300381 highest_irr = apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800382
383 return highest_irr;
384}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800385
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200386static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800387 int vector, int level, int trig_mode,
388 unsigned long *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200389
Yang Zhangb4f22252013-04-11 19:21:37 +0800390int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
391 unsigned long *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300392{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800393 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800394
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200395 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800396 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300397}
398
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300399static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
400{
401
402 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
403 sizeof(val));
404}
405
406static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
407{
408
409 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
410 sizeof(*val));
411}
412
413static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
414{
415 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
416}
417
418static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
419{
420 u8 val;
421 if (pv_eoi_get_user(vcpu, &val) < 0)
422 apic_debug("Can't read EOI MSR value: 0x%llx\n",
423 (unsigned long long)vcpi->arch.pv_eoi.msr_val);
424 return val & 0x1;
425}
426
427static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
428{
429 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
430 apic_debug("Can't set EOI MSR value: 0x%llx\n",
431 (unsigned long long)vcpi->arch.pv_eoi.msr_val);
432 return;
433 }
434 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
435}
436
437static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
438{
439 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
440 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
441 (unsigned long long)vcpi->arch.pv_eoi.msr_val);
442 return;
443 }
444 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
445}
446
Eddie Dong97222cc2007-09-12 10:58:04 +0300447static inline int apic_find_highest_isr(struct kvm_lapic *apic)
448{
449 int result;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800450
451 /* Note that isr_count is always 1 with vid enabled */
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300452 if (!apic->isr_count)
453 return -1;
454 if (likely(apic->highest_isr_cache != -1))
455 return apic->highest_isr_cache;
Eddie Dong97222cc2007-09-12 10:58:04 +0300456
457 result = find_highest_vector(apic->regs + APIC_ISR);
458 ASSERT(result == -1 || result >= 16);
459
460 return result;
461}
462
463static void apic_update_ppr(struct kvm_lapic *apic)
464{
Avi Kivity3842d132010-07-27 12:30:24 +0300465 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300466 int isr;
467
Gleb Natapovc48f1492012-08-05 15:58:33 +0300468 old_ppr = kvm_apic_get_reg(apic, APIC_PROCPRI);
469 tpr = kvm_apic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300470 isr = apic_find_highest_isr(apic);
471 isrv = (isr != -1) ? isr : 0;
472
473 if ((tpr & 0xf0) >= (isrv & 0xf0))
474 ppr = tpr & 0xff;
475 else
476 ppr = isrv & 0xf0;
477
478 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
479 apic, ppr, isr, isrv);
480
Avi Kivity3842d132010-07-27 12:30:24 +0300481 if (old_ppr != ppr) {
482 apic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200483 if (ppr < old_ppr)
484 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300485 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300486}
487
488static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
489{
490 apic_set_reg(apic, APIC_TASKPRI, tpr);
491 apic_update_ppr(apic);
492}
493
494int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
495{
Gleb Natapov343f94f2009-03-05 16:34:54 +0200496 return dest == 0xff || kvm_apic_id(apic) == dest;
Eddie Dong97222cc2007-09-12 10:58:04 +0300497}
498
499int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
500{
501 int result = 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300502 u32 logical_id;
503
504 if (apic_x2apic_mode(apic)) {
Gleb Natapovc48f1492012-08-05 15:58:33 +0300505 logical_id = kvm_apic_get_reg(apic, APIC_LDR);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300506 return logical_id & mda;
507 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300508
Gleb Natapovc48f1492012-08-05 15:58:33 +0300509 logical_id = GET_APIC_LOGICAL_ID(kvm_apic_get_reg(apic, APIC_LDR));
Eddie Dong97222cc2007-09-12 10:58:04 +0300510
Gleb Natapovc48f1492012-08-05 15:58:33 +0300511 switch (kvm_apic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300512 case APIC_DFR_FLAT:
513 if (logical_id & mda)
514 result = 1;
515 break;
516 case APIC_DFR_CLUSTER:
517 if (((logical_id >> 4) == (mda >> 0x4))
518 && (logical_id & mda & 0xf))
519 result = 1;
520 break;
521 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200522 apic_debug("Bad DFR vcpu %d: %08x\n",
Gleb Natapovc48f1492012-08-05 15:58:33 +0300523 apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
Eddie Dong97222cc2007-09-12 10:58:04 +0300524 break;
525 }
526
527 return result;
528}
529
Gleb Natapov343f94f2009-03-05 16:34:54 +0200530int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Eddie Dong97222cc2007-09-12 10:58:04 +0300531 int short_hand, int dest, int dest_mode)
532{
533 int result = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800534 struct kvm_lapic *target = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300535
536 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200537 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300538 target, source, dest, dest_mode, short_hand);
539
Zachary Amsdenbd371392010-06-14 11:42:15 -1000540 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300541 switch (short_hand) {
542 case APIC_DEST_NOSHORT:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200543 if (dest_mode == 0)
Eddie Dong97222cc2007-09-12 10:58:04 +0300544 /* Physical mode. */
Gleb Natapov343f94f2009-03-05 16:34:54 +0200545 result = kvm_apic_match_physical_addr(target, dest);
546 else
Eddie Dong97222cc2007-09-12 10:58:04 +0300547 /* Logical mode. */
548 result = kvm_apic_match_logical_addr(target, dest);
549 break;
550 case APIC_DEST_SELF:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200551 result = (target == source);
Eddie Dong97222cc2007-09-12 10:58:04 +0300552 break;
553 case APIC_DEST_ALLINC:
554 result = 1;
555 break;
556 case APIC_DEST_ALLBUT:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200557 result = (target != source);
Eddie Dong97222cc2007-09-12 10:58:04 +0300558 break;
559 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200560 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
561 short_hand);
Eddie Dong97222cc2007-09-12 10:58:04 +0300562 break;
563 }
564
565 return result;
566}
567
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300568bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Yang Zhangb4f22252013-04-11 19:21:37 +0800569 struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300570{
571 struct kvm_apic_map *map;
572 unsigned long bitmap = 1;
573 struct kvm_lapic **dst;
574 int i;
575 bool ret = false;
576
577 *r = -1;
578
579 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800580 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300581 return true;
582 }
583
584 if (irq->shorthand)
585 return false;
586
587 rcu_read_lock();
588 map = rcu_dereference(kvm->arch.apic_map);
589
590 if (!map)
591 goto out;
592
593 if (irq->dest_mode == 0) { /* physical mode */
594 if (irq->delivery_mode == APIC_DM_LOWEST ||
595 irq->dest_id == 0xff)
596 goto out;
597 dst = &map->phys_map[irq->dest_id & 0xff];
598 } else {
599 u32 mda = irq->dest_id << (32 - map->ldr_bits);
600
601 dst = map->logical_map[apic_cluster_id(map, mda)];
602
603 bitmap = apic_logical_id(map, mda);
604
605 if (irq->delivery_mode == APIC_DM_LOWEST) {
606 int l = -1;
607 for_each_set_bit(i, &bitmap, 16) {
608 if (!dst[i])
609 continue;
610 if (l < 0)
611 l = i;
612 else if (kvm_apic_compare_prio(dst[i]->vcpu, dst[l]->vcpu) < 0)
613 l = i;
614 }
615
616 bitmap = (l >= 0) ? 1 << l : 0;
617 }
618 }
619
620 for_each_set_bit(i, &bitmap, 16) {
621 if (!dst[i])
622 continue;
623 if (*r < 0)
624 *r = 0;
Yang Zhangb4f22252013-04-11 19:21:37 +0800625 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300626 }
627
628 ret = true;
629out:
630 rcu_read_unlock();
631 return ret;
632}
633
Eddie Dong97222cc2007-09-12 10:58:04 +0300634/*
635 * Add a pending IRQ into lapic.
636 * Return 1 if successfully added and 0 if discarded.
637 */
638static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800639 int vector, int level, int trig_mode,
640 unsigned long *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300641{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200642 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300643 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300644
645 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300646 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200647 vcpu->arch.apic_arb_prio++;
648 case APIC_DM_FIXED:
Eddie Dong97222cc2007-09-12 10:58:04 +0300649 /* FIXME add logic for vcpu on reset */
650 if (unlikely(!apic_enabled(apic)))
651 break;
652
Yang Zhangb4f22252013-04-11 19:21:37 +0800653 if (dest_map)
654 __set_bit(vcpu->vcpu_id, dest_map);
655
Avi Kivitya5d36f82009-12-29 12:42:16 +0200656 if (trig_mode) {
657 apic_debug("level trig mode for vector %d", vector);
658 apic_set_vector(vector, apic->regs + APIC_TMR);
659 } else
660 apic_clear_vector(vector, apic->regs + APIC_TMR);
661
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200662 result = !apic_test_and_set_irr(vector, apic);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300663 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
Gleb Natapov4da74892009-08-27 16:25:04 +0300664 trig_mode, vector, !result);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200665 if (!result) {
666 if (trig_mode)
667 apic_debug("level trig mode repeatedly for "
668 "vector %d", vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300669 break;
670 }
671
Avi Kivity3842d132010-07-27 12:30:24 +0300672 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -0300673 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300674 break;
675
676 case APIC_DM_REMRD:
Jan Kiszka7712de82011-09-12 11:25:51 +0200677 apic_debug("Ignoring delivery mode 3\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300678 break;
679
680 case APIC_DM_SMI:
Jan Kiszka7712de82011-09-12 11:25:51 +0200681 apic_debug("Ignoring guest SMI\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300682 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800683
Eddie Dong97222cc2007-09-12 10:58:04 +0300684 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200685 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800686 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200687 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300688 break;
689
690 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100691 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200692 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100693 /* assumes that there are only KVM_APIC_INIT/SIPI */
694 apic->pending_events = (1UL << KVM_APIC_INIT);
695 /* make sure pending_events is visible before sending
696 * the request */
697 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300698 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300699 kvm_vcpu_kick(vcpu);
700 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200701 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
702 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300703 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300704 break;
705
706 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200707 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
708 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100709 result = 1;
710 apic->sipi_vector = vector;
711 /* make sure sipi_vector is visible for the receiver */
712 smp_wmb();
713 set_bit(KVM_APIC_SIPI, &apic->pending_events);
714 kvm_make_request(KVM_REQ_EVENT, vcpu);
715 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300716 break;
717
Jan Kiszka23930f92008-09-26 09:30:52 +0200718 case APIC_DM_EXTINT:
719 /*
720 * Should only be called by kvm_apic_local_deliver() with LVT0,
721 * before NMI watchdog was enabled. Already handled by
722 * kvm_apic_accept_pic_intr().
723 */
724 break;
725
Eddie Dong97222cc2007-09-12 10:58:04 +0300726 default:
727 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
728 delivery_mode);
729 break;
730 }
731 return result;
732}
733
Gleb Natapove1035712009-03-05 16:34:59 +0200734int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +0300735{
Gleb Natapove1035712009-03-05 16:34:59 +0200736 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800737}
738
Yang Zhangc7c9c562013-01-25 10:18:51 +0800739static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
740{
741 if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
742 kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
743 int trigger_mode;
744 if (apic_test_vector(vector, apic->regs + APIC_TMR))
745 trigger_mode = IOAPIC_LEVEL_TRIG;
746 else
747 trigger_mode = IOAPIC_EDGE_TRIG;
Yang Zhang1fcc7892013-04-11 19:21:35 +0800748 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800749 }
750}
751
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300752static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300753{
754 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300755
756 trace_kvm_eoi(apic, vector);
757
Eddie Dong97222cc2007-09-12 10:58:04 +0300758 /*
759 * Not every write EOI will has corresponding ISR,
760 * one example is when Kernel check timer on setup_IO_APIC
761 */
762 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300763 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +0300764
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300765 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300766 apic_update_ppr(apic);
767
Yang Zhangc7c9c562013-01-25 10:18:51 +0800768 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +0300769 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300770 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +0300771}
772
Yang Zhangc7c9c562013-01-25 10:18:51 +0800773/*
774 * this interface assumes a trap-like exit, which has already finished
775 * desired side effect including vISR and vPPR update.
776 */
777void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
778{
779 struct kvm_lapic *apic = vcpu->arch.apic;
780
781 trace_kvm_eoi(apic, vector);
782
783 kvm_ioapic_send_eoi(apic, vector);
784 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
785}
786EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
787
Eddie Dong97222cc2007-09-12 10:58:04 +0300788static void apic_send_ipi(struct kvm_lapic *apic)
789{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300790 u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
791 u32 icr_high = kvm_apic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200792 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +0300793
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200794 irq.vector = icr_low & APIC_VECTOR_MASK;
795 irq.delivery_mode = icr_low & APIC_MODE_MASK;
796 irq.dest_mode = icr_low & APIC_DEST_MASK;
797 irq.level = icr_low & APIC_INT_ASSERT;
798 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
799 irq.shorthand = icr_low & APIC_SHORT_MASK;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300800 if (apic_x2apic_mode(apic))
801 irq.dest_id = icr_high;
802 else
803 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +0300804
Gleb Natapov1000ff82009-07-07 16:00:57 +0300805 trace_kvm_apic_ipi(icr_low, irq.dest_id);
806
Eddie Dong97222cc2007-09-12 10:58:04 +0300807 apic_debug("icr_high 0x%x, icr_low 0x%x, "
808 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
809 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -0400810 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200811 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
812 irq.vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300813
Yang Zhangb4f22252013-04-11 19:21:37 +0800814 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +0300815}
816
817static u32 apic_get_tmcct(struct kvm_lapic *apic)
818{
Marcelo Tosattib682b812009-02-10 20:41:41 -0200819 ktime_t remaining;
820 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200821 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +0300822
823 ASSERT(apic != NULL);
824
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200825 /* if initial count is 0, current count should also be 0 */
Gleb Natapovc48f1492012-08-05 15:58:33 +0300826 if (kvm_apic_get_reg(apic, APIC_TMICT) == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200827 return 0;
828
Marcelo Tosattiace15462009-10-08 10:55:03 -0300829 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -0200830 if (ktime_to_ns(remaining) < 0)
831 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300832
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300833 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
834 tmcct = div64_u64(ns,
835 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +0300836
837 return tmcct;
838}
839
Avi Kivityb209749f2007-10-22 16:50:39 +0200840static void __report_tpr_access(struct kvm_lapic *apic, bool write)
841{
842 struct kvm_vcpu *vcpu = apic->vcpu;
843 struct kvm_run *run = vcpu->run;
844
Avi Kivitya8eeb042010-05-10 12:34:53 +0300845 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300846 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +0200847 run->tpr_access.is_write = write;
848}
849
850static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
851{
852 if (apic->vcpu->arch.tpr_access_reporting)
853 __report_tpr_access(apic, write);
854}
855
Eddie Dong97222cc2007-09-12 10:58:04 +0300856static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
857{
858 u32 val = 0;
859
860 if (offset >= LAPIC_MMIO_LENGTH)
861 return 0;
862
863 switch (offset) {
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300864 case APIC_ID:
865 if (apic_x2apic_mode(apic))
866 val = kvm_apic_id(apic);
867 else
868 val = kvm_apic_id(apic) << 24;
869 break;
Eddie Dong97222cc2007-09-12 10:58:04 +0300870 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +0200871 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300872 break;
873
874 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800875 if (apic_lvtt_tscdeadline(apic))
876 return 0;
877
Eddie Dong97222cc2007-09-12 10:58:04 +0300878 val = apic_get_tmcct(apic);
879 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +0300880 case APIC_PROCPRI:
881 apic_update_ppr(apic);
Gleb Natapovc48f1492012-08-05 15:58:33 +0300882 val = kvm_apic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +0300883 break;
Avi Kivityb209749f2007-10-22 16:50:39 +0200884 case APIC_TASKPRI:
885 report_tpr_access(apic, false);
886 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +0300887 default:
Gleb Natapovc48f1492012-08-05 15:58:33 +0300888 val = kvm_apic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +0300889 break;
890 }
891
892 return val;
893}
894
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400895static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
896{
897 return container_of(dev, struct kvm_lapic, dev);
898}
899
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300900static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
901 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300902{
Eddie Dong97222cc2007-09-12 10:58:04 +0300903 unsigned char alignment = offset & 0xf;
904 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +0800905 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300906 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300907
908 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +0300909 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
910 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300911 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300912 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300913
914 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +0300915 apic_debug("KVM_APIC_READ: read reserved register %x\n",
916 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300917 return 1;
918 }
919
Eddie Dong97222cc2007-09-12 10:58:04 +0300920 result = __apic_read(apic, offset & ~0xf);
921
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300922 trace_kvm_apic_read(offset, result);
923
Eddie Dong97222cc2007-09-12 10:58:04 +0300924 switch (len) {
925 case 1:
926 case 2:
927 case 4:
928 memcpy(data, (char *)&result + alignment, len);
929 break;
930 default:
931 printk(KERN_ERR "Local APIC read with len = %x, "
932 "should be 1,2, or 4 instead\n", len);
933 break;
934 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300935 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300936}
937
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300938static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
939{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300940 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300941 addr >= apic->base_address &&
942 addr < apic->base_address + LAPIC_MMIO_LENGTH;
943}
944
945static int apic_mmio_read(struct kvm_io_device *this,
946 gpa_t address, int len, void *data)
947{
948 struct kvm_lapic *apic = to_lapic(this);
949 u32 offset = address - apic->base_address;
950
951 if (!apic_mmio_in_range(apic, address))
952 return -EOPNOTSUPP;
953
954 apic_reg_read(apic, offset, len, data);
955
956 return 0;
957}
958
Eddie Dong97222cc2007-09-12 10:58:04 +0300959static void update_divide_count(struct kvm_lapic *apic)
960{
961 u32 tmp1, tmp2, tdcr;
962
Gleb Natapovc48f1492012-08-05 15:58:33 +0300963 tdcr = kvm_apic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300964 tmp1 = tdcr & 0xf;
965 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300966 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +0300967
968 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -0400969 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +0300970}
971
972static void start_apic_timer(struct kvm_lapic *apic)
973{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800974 ktime_t now;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300975 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +0200976
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800977 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +0800978 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800979 now = apic->lapic_timer.timer.base->get_time();
Gleb Natapovc48f1492012-08-05 15:58:33 +0300980 apic->lapic_timer.period = (u64)kvm_apic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800981 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +0200982
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800983 if (!apic->lapic_timer.period)
984 return;
985 /*
986 * Do not allow the guest to program periodic timers with small
987 * interval, since the hrtimers are not throttled by the host
988 * scheduler.
989 */
990 if (apic_lvtt_period(apic)) {
991 s64 min_period = min_timer_period_us * 1000LL;
992
993 if (apic->lapic_timer.period < min_period) {
994 pr_info_ratelimited(
995 "kvm: vcpu %i: requested %lld ns "
996 "lapic timer period limited to %lld ns\n",
997 apic->vcpu->vcpu_id,
998 apic->lapic_timer.period, min_period);
999 apic->lapic_timer.period = min_period;
1000 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001001 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001002
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001003 hrtimer_start(&apic->lapic_timer.timer,
1004 ktime_add_ns(now, apic->lapic_timer.period),
1005 HRTIMER_MODE_ABS);
Eddie Dong97222cc2007-09-12 10:58:04 +03001006
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001007 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001008 PRIx64 ", "
1009 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001010 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001011 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Gleb Natapovc48f1492012-08-05 15:58:33 +03001012 kvm_apic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001013 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001014 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001015 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001016 } else if (apic_lvtt_tscdeadline(apic)) {
1017 /* lapic timer in tsc deadline mode */
1018 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1019 u64 ns = 0;
1020 struct kvm_vcpu *vcpu = apic->vcpu;
Zachary Amsdencc578282012-02-03 15:43:50 -02001021 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001022 unsigned long flags;
1023
1024 if (unlikely(!tscdeadline || !this_tsc_khz))
1025 return;
1026
1027 local_irq_save(flags);
1028
1029 now = apic->lapic_timer.timer.base->get_time();
Marcelo Tosatti886b4702012-11-27 23:28:58 -02001030 guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, native_read_tsc());
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001031 if (likely(tscdeadline > guest_tsc)) {
1032 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1033 do_div(ns, this_tsc_khz);
1034 }
1035 hrtimer_start(&apic->lapic_timer.timer,
1036 ktime_add_ns(now, ns), HRTIMER_MODE_ABS);
1037
1038 local_irq_restore(flags);
1039 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001040}
1041
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001042static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1043{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001044 int nmi_wd_enabled = apic_lvt_nmi_mode(kvm_apic_get_reg(apic, APIC_LVT0));
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001045
1046 if (apic_lvt_nmi_mode(lvt0_val)) {
1047 if (!nmi_wd_enabled) {
1048 apic_debug("Receive NMI setting on APIC_LVT0 "
1049 "for cpu %d\n", apic->vcpu->vcpu_id);
1050 apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
1051 }
1052 } else if (nmi_wd_enabled)
1053 apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
1054}
1055
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001056static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001057{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001058 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001059
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001060 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001061
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001062 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001063 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001064 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001065 kvm_apic_set_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001066 else
1067 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001068 break;
1069
1070 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001071 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001072 apic_set_tpr(apic, val & 0xff);
1073 break;
1074
1075 case APIC_EOI:
1076 apic_set_eoi(apic);
1077 break;
1078
1079 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001080 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001081 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001082 else
1083 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001084 break;
1085
1086 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001087 if (!apic_x2apic_mode(apic)) {
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001088 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001089 recalculate_apic_map(apic->vcpu->kvm);
1090 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001091 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001092 break;
1093
Gleb Natapovfc61b802009-07-05 17:39:35 +03001094 case APIC_SPIV: {
1095 u32 mask = 0x3ff;
Gleb Natapovc48f1492012-08-05 15:58:33 +03001096 if (kvm_apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001097 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001098 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001099 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1100 int i;
1101 u32 lvt_val;
1102
1103 for (i = 0; i < APIC_LVT_NUM; i++) {
Gleb Natapovc48f1492012-08-05 15:58:33 +03001104 lvt_val = kvm_apic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001105 APIC_LVTT + 0x10 * i);
1106 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
1107 lvt_val | APIC_LVT_MASKED);
1108 }
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001109 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001110
1111 }
1112 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001113 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001114 case APIC_ICR:
1115 /* No delay here, so we always clear the pending bit */
1116 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
1117 apic_send_ipi(apic);
1118 break;
1119
1120 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001121 if (!apic_x2apic_mode(apic))
1122 val &= 0xff000000;
1123 apic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001124 break;
1125
Jan Kiszka23930f92008-09-26 09:30:52 +02001126 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001127 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001128 case APIC_LVTTHMR:
1129 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001130 case APIC_LVT1:
1131 case APIC_LVTERR:
1132 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001133 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001134 val |= APIC_LVT_MASKED;
1135
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001136 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
1137 apic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001138
1139 break;
1140
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001141 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001142 if ((kvm_apic_get_reg(apic, APIC_LVTT) &
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001143 apic->lapic_timer.timer_mode_mask) !=
1144 (val & apic->lapic_timer.timer_mode_mask))
1145 hrtimer_cancel(&apic->lapic_timer.timer);
1146
Gleb Natapovc48f1492012-08-05 15:58:33 +03001147 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001148 val |= APIC_LVT_MASKED;
1149 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
1150 apic_set_reg(apic, APIC_LVTT, val);
1151 break;
1152
Eddie Dong97222cc2007-09-12 10:58:04 +03001153 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001154 if (apic_lvtt_tscdeadline(apic))
1155 break;
1156
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001157 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001158 apic_set_reg(apic, APIC_TMICT, val);
1159 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001160 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001161
1162 case APIC_TDCR:
1163 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001164 apic_debug("KVM_WRITE:TDCR %x\n", val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001165 apic_set_reg(apic, APIC_TDCR, val);
1166 update_divide_count(apic);
1167 break;
1168
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001169 case APIC_ESR:
1170 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001171 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001172 ret = 1;
1173 }
1174 break;
1175
1176 case APIC_SELF_IPI:
1177 if (apic_x2apic_mode(apic)) {
1178 apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
1179 } else
1180 ret = 1;
1181 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001182 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001183 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001184 break;
1185 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001186 if (ret)
1187 apic_debug("Local APIC Write to read-only register %x\n", reg);
1188 return ret;
1189}
1190
1191static int apic_mmio_write(struct kvm_io_device *this,
1192 gpa_t address, int len, const void *data)
1193{
1194 struct kvm_lapic *apic = to_lapic(this);
1195 unsigned int offset = address - apic->base_address;
1196 u32 val;
1197
1198 if (!apic_mmio_in_range(apic, address))
1199 return -EOPNOTSUPP;
1200
1201 /*
1202 * APIC register must be aligned on 128-bits boundary.
1203 * 32/64/128 bits registers must be accessed thru 32 bits.
1204 * Refer SDM 8.4.1
1205 */
1206 if (len != 4 || (offset & 0xf)) {
1207 /* Don't shout loud, $infamous_os would cause only noise. */
1208 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001209 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001210 }
1211
1212 val = *(u32*)data;
1213
1214 /* too common printing */
1215 if (offset != APIC_EOI)
1216 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1217 "0x%x\n", __func__, offset, len, val);
1218
1219 apic_reg_write(apic, offset & 0xff0, val);
1220
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001221 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001222}
1223
Kevin Tian58fbbf262011-08-30 13:56:17 +03001224void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1225{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001226 if (kvm_vcpu_has_lapic(vcpu))
Kevin Tian58fbbf262011-08-30 13:56:17 +03001227 apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
1228}
1229EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1230
Yang Zhang83d4c282013-01-25 10:18:49 +08001231/* emulate APIC access in a trap manner */
1232void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1233{
1234 u32 val = 0;
1235
1236 /* hw has done the conditional check and inst decode */
1237 offset &= 0xff0;
1238
1239 apic_reg_read(vcpu->arch.apic, offset, 4, &val);
1240
1241 /* TODO: optimize to just emulate side effect w/o one more write */
1242 apic_reg_write(vcpu->arch.apic, offset, val);
1243}
1244EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1245
Rusty Russelld5894442007-10-08 10:48:30 +10001246void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001247{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001248 struct kvm_lapic *apic = vcpu->arch.apic;
1249
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001250 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001251 return;
1252
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001253 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001254
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001255 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1256 static_key_slow_dec_deferred(&apic_hw_disabled);
1257
Gleb Natapovc48f1492012-08-05 15:58:33 +03001258 if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED))
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001259 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001260
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001261 if (apic->regs)
1262 free_page((unsigned long)apic->regs);
1263
1264 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001265}
1266
1267/*
1268 *----------------------------------------------------------------------
1269 * LAPIC interface
1270 *----------------------------------------------------------------------
1271 */
1272
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001273u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1274{
1275 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001276
Gleb Natapovc48f1492012-08-05 15:58:33 +03001277 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001278 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001279 return 0;
1280
1281 return apic->lapic_timer.tscdeadline;
1282}
1283
1284void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1285{
1286 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001287
Gleb Natapovc48f1492012-08-05 15:58:33 +03001288 if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001289 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001290 return;
1291
1292 hrtimer_cancel(&apic->lapic_timer.timer);
1293 apic->lapic_timer.tscdeadline = data;
1294 start_apic_timer(apic);
1295}
1296
Eddie Dong97222cc2007-09-12 10:58:04 +03001297void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1298{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001299 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001300
Gleb Natapovc48f1492012-08-05 15:58:33 +03001301 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001302 return;
Gleb Natapov54e98182012-08-05 15:58:32 +03001303
Avi Kivityb93463a2007-10-25 16:52:32 +02001304 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Gleb Natapovc48f1492012-08-05 15:58:33 +03001305 | (kvm_apic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001306}
1307
1308u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1309{
Eddie Dong97222cc2007-09-12 10:58:04 +03001310 u64 tpr;
1311
Gleb Natapovc48f1492012-08-05 15:58:33 +03001312 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001313 return 0;
Gleb Natapov54e98182012-08-05 15:58:32 +03001314
Gleb Natapovc48f1492012-08-05 15:58:33 +03001315 tpr = (u64) kvm_apic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001316
1317 return (tpr & 0xf0) >> 4;
1318}
1319
1320void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1321{
Yang Zhang8d146952013-01-25 10:18:50 +08001322 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001324
1325 if (!apic) {
1326 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001327 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001328 return;
1329 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001330
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001331 /* update jump label if enable bit changes */
1332 if ((vcpu->arch.apic_base ^ value) & MSR_IA32_APICBASE_ENABLE) {
1333 if (value & MSR_IA32_APICBASE_ENABLE)
1334 static_key_slow_dec_deferred(&apic_hw_disabled);
1335 else
1336 static_key_slow_inc(&apic_hw_disabled.key);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001337 recalculate_apic_map(vcpu->kvm);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001338 }
1339
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001340 if (!kvm_vcpu_is_bsp(apic->vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001341 value &= ~MSR_IA32_APICBASE_BSP;
1342
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 vcpu->arch.apic_base = value;
Yang Zhang8d146952013-01-25 10:18:50 +08001344 if ((old_value ^ value) & X2APIC_ENABLE) {
1345 if (value & X2APIC_ENABLE) {
1346 u32 id = kvm_apic_id(apic);
1347 u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
1348 kvm_apic_set_ldr(apic, ldr);
1349 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1350 } else
1351 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001352 }
Yang Zhang8d146952013-01-25 10:18:50 +08001353
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001354 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001355 MSR_IA32_APICBASE_BASE;
1356
1357 /* with FSB delivery interrupt, we can restart APIC functionality */
1358 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001359 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001360
1361}
1362
He, Qingc5ec1532007-09-03 17:07:41 +03001363void kvm_lapic_reset(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001364{
1365 struct kvm_lapic *apic;
1366 int i;
1367
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001368 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001369
1370 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001371 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001372 ASSERT(apic != NULL);
1373
1374 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001375 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001376
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001377 kvm_apic_set_id(apic, vcpu->vcpu_id);
Gleb Natapovfc61b802009-07-05 17:39:35 +03001378 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001379
1380 for (i = 0; i < APIC_LVT_NUM; i++)
1381 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Qing He40487c62007-09-17 14:47:13 +08001382 apic_set_reg(apic, APIC_LVT0,
1383 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Eddie Dong97222cc2007-09-12 10:58:04 +03001384
1385 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001386 apic_set_spiv(apic, 0xff);
Eddie Dong97222cc2007-09-12 10:58:04 +03001387 apic_set_reg(apic, APIC_TASKPRI, 0);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001388 kvm_apic_set_ldr(apic, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001389 apic_set_reg(apic, APIC_ESR, 0);
1390 apic_set_reg(apic, APIC_ICR, 0);
1391 apic_set_reg(apic, APIC_ICR2, 0);
1392 apic_set_reg(apic, APIC_TDCR, 0);
1393 apic_set_reg(apic, APIC_TMICT, 0);
1394 for (i = 0; i < 8; i++) {
1395 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1396 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1397 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
1398 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08001399 apic->irr_pending = kvm_apic_vid_enabled(vcpu->kvm);
1400 apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001401 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001402 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001403 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001404 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001405 kvm_lapic_set_base(vcpu,
1406 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001407 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001408 apic_update_ppr(apic);
1409
Gleb Natapove1035712009-03-05 16:34:59 +02001410 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001411 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001412
Eddie Dong97222cc2007-09-12 10:58:04 +03001413 apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001414 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001415 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001416 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001417}
1418
Eddie Dong97222cc2007-09-12 10:58:04 +03001419/*
1420 *----------------------------------------------------------------------
1421 * timer interface
1422 *----------------------------------------------------------------------
1423 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001424
Avi Kivity2a6eac92012-07-26 18:01:51 +03001425static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001426{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001427 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001428}
1429
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001430int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1431{
Gleb Natapov54e98182012-08-05 15:58:32 +03001432 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001433
Gleb Natapovc48f1492012-08-05 15:58:33 +03001434 if (kvm_vcpu_has_lapic(vcpu) && apic_enabled(apic) &&
Gleb Natapov54e98182012-08-05 15:58:32 +03001435 apic_lvt_enabled(apic, APIC_LVTT))
1436 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001437
1438 return 0;
1439}
1440
Avi Kivity89342082011-11-10 14:57:21 +02001441int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001442{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001443 u32 reg = kvm_apic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001444 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001445
Gleb Natapovc48f1492012-08-05 15:58:33 +03001446 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001447 vector = reg & APIC_VECTOR_MASK;
1448 mode = reg & APIC_MODE_MASK;
1449 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001450 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1451 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001452 }
1453 return 0;
1454}
1455
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001456void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001457{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001458 struct kvm_lapic *apic = vcpu->arch.apic;
1459
1460 if (apic)
1461 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001462}
1463
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001464static const struct kvm_io_device_ops apic_mmio_ops = {
1465 .read = apic_mmio_read,
1466 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001467};
1468
Avi Kivitye9d90d42012-07-26 18:01:50 +03001469static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1470{
1471 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001472 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
1473 struct kvm_vcpu *vcpu = apic->vcpu;
Avi Kivitye9d90d42012-07-26 18:01:50 +03001474 wait_queue_head_t *q = &vcpu->wq;
1475
1476 /*
1477 * There is a race window between reading and incrementing, but we do
1478 * not care about potentially losing timer events in the !reinject
1479 * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked
1480 * in vcpu_enter_guest.
1481 */
Avi Kivity2a6eac92012-07-26 18:01:51 +03001482 if (!atomic_read(&ktimer->pending)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001483 atomic_inc(&ktimer->pending);
1484 /* FIXME: this code should not know anything about vcpus */
1485 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1486 }
1487
1488 if (waitqueue_active(q))
1489 wake_up_interruptible(q);
1490
Avi Kivity2a6eac92012-07-26 18:01:51 +03001491 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001492 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1493 return HRTIMER_RESTART;
1494 } else
1495 return HRTIMER_NORESTART;
1496}
1497
Eddie Dong97222cc2007-09-12 10:58:04 +03001498int kvm_create_lapic(struct kvm_vcpu *vcpu)
1499{
1500 struct kvm_lapic *apic;
1501
1502 ASSERT(vcpu != NULL);
1503 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1504
1505 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1506 if (!apic)
1507 goto nomem;
1508
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001509 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001510
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001511 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1512 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001513 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1514 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001515 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001516 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001517 apic->vcpu = vcpu;
1518
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001519 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
1520 HRTIMER_MODE_ABS);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001521 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001522
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001523 /*
1524 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1525 * thinking that APIC satet has changed.
1526 */
1527 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapov6aed64a2012-08-05 15:58:28 +03001528 kvm_lapic_set_base(vcpu,
1529 APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE);
Eddie Dong97222cc2007-09-12 10:58:04 +03001530
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001531 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
He, Qingc5ec1532007-09-03 17:07:41 +03001532 kvm_lapic_reset(vcpu);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001533 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001534
1535 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001536nomem_free_apic:
1537 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001538nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001539 return -ENOMEM;
1540}
Eddie Dong97222cc2007-09-12 10:58:04 +03001541
1542int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1543{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001544 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001545 int highest_irr;
1546
Gleb Natapovc48f1492012-08-05 15:58:33 +03001547 if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001548 return -1;
1549
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001550 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001551 highest_irr = apic_find_highest_irr(apic);
1552 if ((highest_irr == -1) ||
Gleb Natapovc48f1492012-08-05 15:58:33 +03001553 ((highest_irr & 0xF0) <= kvm_apic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03001554 return -1;
1555 return highest_irr;
1556}
1557
Qing He40487c62007-09-17 14:47:13 +08001558int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1559{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001560 u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001561 int r = 0;
1562
Gleb Natapovc48f1492012-08-05 15:58:33 +03001563 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001564 r = 1;
1565 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1566 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1567 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001568 return r;
1569}
1570
Eddie Dong1b9778d2007-09-03 16:56:58 +03001571void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1572{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001573 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001574
Gleb Natapovc48f1492012-08-05 15:58:33 +03001575 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03001576 return;
1577
1578 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001579 if (kvm_apic_local_deliver(apic, APIC_LVTT))
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001580 atomic_dec(&apic->lapic_timer.pending);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001581 }
1582}
1583
Eddie Dong97222cc2007-09-12 10:58:04 +03001584int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1585{
1586 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001587 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001588
1589 if (vector == -1)
1590 return -1;
1591
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001592 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001593 apic_update_ppr(apic);
1594 apic_clear_irr(vector, apic);
1595 return vector;
1596}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001597
Gleb Natapov64eb0622012-08-08 15:24:36 +03001598void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
1599 struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001600{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001601 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001602
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001603 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03001604 /* set SPIV separately to get count of SW disabled APICs right */
1605 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
1606 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001607 /* call kvm_apic_set_id() to put apic into apic_map */
1608 kvm_apic_set_id(apic, kvm_apic_id(apic));
Gleb Natapovfc61b802009-07-05 17:39:35 +03001609 kvm_apic_set_version(vcpu);
1610
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001611 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001612 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001613 update_divide_count(apic);
1614 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02001615 apic->irr_pending = true;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001616 apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm) ?
1617 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001618 apic->highest_isr_cache = -1;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001619 kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
Avi Kivity3842d132010-07-27 12:30:24 +03001620 kvm_make_request(KVM_REQ_EVENT, vcpu);
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001621}
Eddie Donga3d7f852007-09-03 16:15:12 +03001622
Avi Kivity2f52d582008-01-16 12:49:30 +02001623void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03001624{
Eddie Donga3d7f852007-09-03 16:15:12 +03001625 struct hrtimer *timer;
1626
Gleb Natapovc48f1492012-08-05 15:58:33 +03001627 if (!kvm_vcpu_has_lapic(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03001628 return;
1629
Gleb Natapov54e98182012-08-05 15:58:32 +03001630 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03001631 if (hrtimer_cancel(timer))
Arjan van de Venbeb20d52008-09-01 14:55:57 -07001632 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
Eddie Donga3d7f852007-09-03 16:15:12 +03001633}
Avi Kivityb93463a2007-10-25 16:52:32 +02001634
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001635/*
1636 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
1637 *
1638 * Detect whether guest triggered PV EOI since the
1639 * last entry. If yes, set EOI on guests's behalf.
1640 * Clear PV EOI in guest memory in any case.
1641 */
1642static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
1643 struct kvm_lapic *apic)
1644{
1645 bool pending;
1646 int vector;
1647 /*
1648 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
1649 * and KVM_PV_EOI_ENABLED in guest memory as follows:
1650 *
1651 * KVM_APIC_PV_EOI_PENDING is unset:
1652 * -> host disabled PV EOI.
1653 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
1654 * -> host enabled PV EOI, guest did not execute EOI yet.
1655 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
1656 * -> host enabled PV EOI, guest executed EOI.
1657 */
1658 BUG_ON(!pv_eoi_enabled(vcpu));
1659 pending = pv_eoi_get_pending(vcpu);
1660 /*
1661 * Clear pending bit in any case: it will be set again on vmentry.
1662 * While this might not be ideal from performance point of view,
1663 * this makes sure pv eoi is only enabled when we know it's safe.
1664 */
1665 pv_eoi_clr_pending(vcpu);
1666 if (pending)
1667 return;
1668 vector = apic_set_eoi(apic);
1669 trace_kvm_pv_eoi(apic, vector);
1670}
1671
Avi Kivityb93463a2007-10-25 16:52:32 +02001672void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
1673{
1674 u32 data;
1675 void *vapic;
1676
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001677 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
1678 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
1679
Gleb Natapov41383772012-04-19 14:06:29 +03001680 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02001681 return;
1682
Cong Wang8fd75e12011-11-25 23:14:17 +08001683 vapic = kmap_atomic(vcpu->arch.apic->vapic_page);
Avi Kivityb93463a2007-10-25 16:52:32 +02001684 data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
Cong Wang8fd75e12011-11-25 23:14:17 +08001685 kunmap_atomic(vapic);
Avi Kivityb93463a2007-10-25 16:52:32 +02001686
1687 apic_set_tpr(vcpu->arch.apic, data & 0xff);
1688}
1689
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001690/*
1691 * apic_sync_pv_eoi_to_guest - called before vmentry
1692 *
1693 * Detect whether it's safe to enable PV EOI and
1694 * if yes do so.
1695 */
1696static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
1697 struct kvm_lapic *apic)
1698{
1699 if (!pv_eoi_enabled(vcpu) ||
1700 /* IRR set or many bits in ISR: could be nested. */
1701 apic->irr_pending ||
1702 /* Cache not set: could be safe but we don't bother. */
1703 apic->highest_isr_cache == -1 ||
1704 /* Need EOI to update ioapic. */
1705 kvm_ioapic_handles_vector(vcpu->kvm, apic->highest_isr_cache)) {
1706 /*
1707 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
1708 * so we need not do anything here.
1709 */
1710 return;
1711 }
1712
1713 pv_eoi_set_pending(apic->vcpu);
1714}
1715
Avi Kivityb93463a2007-10-25 16:52:32 +02001716void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
1717{
1718 u32 data, tpr;
1719 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001720 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02001721 void *vapic;
1722
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001723 apic_sync_pv_eoi_to_guest(vcpu, apic);
1724
Gleb Natapov41383772012-04-19 14:06:29 +03001725 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02001726 return;
1727
Gleb Natapovc48f1492012-08-05 15:58:33 +03001728 tpr = kvm_apic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02001729 max_irr = apic_find_highest_irr(apic);
1730 if (max_irr < 0)
1731 max_irr = 0;
1732 max_isr = apic_find_highest_isr(apic);
1733 if (max_isr < 0)
1734 max_isr = 0;
1735 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1736
Cong Wang8fd75e12011-11-25 23:14:17 +08001737 vapic = kmap_atomic(vcpu->arch.apic->vapic_page);
Avi Kivityb93463a2007-10-25 16:52:32 +02001738 *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
Cong Wang8fd75e12011-11-25 23:14:17 +08001739 kunmap_atomic(vapic);
Avi Kivityb93463a2007-10-25 16:52:32 +02001740}
1741
1742void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
1743{
Avi Kivityb93463a2007-10-25 16:52:32 +02001744 vcpu->arch.apic->vapic_addr = vapic_addr;
Gleb Natapov41383772012-04-19 14:06:29 +03001745 if (vapic_addr)
1746 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
1747 else
1748 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Avi Kivityb93463a2007-10-25 16:52:32 +02001749}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001750
1751int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1752{
1753 struct kvm_lapic *apic = vcpu->arch.apic;
1754 u32 reg = (msr - APIC_BASE_MSR) << 4;
1755
1756 if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
1757 return 1;
1758
1759 /* if this is ICR write vector before command */
1760 if (msr == 0x830)
1761 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
1762 return apic_reg_write(apic, reg, (u32)data);
1763}
1764
1765int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
1766{
1767 struct kvm_lapic *apic = vcpu->arch.apic;
1768 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
1769
1770 if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
1771 return 1;
1772
1773 if (apic_reg_read(apic, reg, 4, &low))
1774 return 1;
1775 if (msr == 0x830)
1776 apic_reg_read(apic, APIC_ICR2, 4, &high);
1777
1778 *data = (((u64)high) << 32) | low;
1779
1780 return 0;
1781}
Gleb Natapov10388a02010-01-17 15:51:23 +02001782
1783int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
1784{
1785 struct kvm_lapic *apic = vcpu->arch.apic;
1786
Gleb Natapovc48f1492012-08-05 15:58:33 +03001787 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02001788 return 1;
1789
1790 /* if this is ICR write vector before command */
1791 if (reg == APIC_ICR)
1792 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
1793 return apic_reg_write(apic, reg, (u32)data);
1794}
1795
1796int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
1797{
1798 struct kvm_lapic *apic = vcpu->arch.apic;
1799 u32 low, high = 0;
1800
Gleb Natapovc48f1492012-08-05 15:58:33 +03001801 if (!kvm_vcpu_has_lapic(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02001802 return 1;
1803
1804 if (apic_reg_read(apic, reg, 4, &low))
1805 return 1;
1806 if (reg == APIC_ICR)
1807 apic_reg_read(apic, APIC_ICR2, 4, &high);
1808
1809 *data = (((u64)high) << 32) | low;
1810
1811 return 0;
1812}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001813
1814int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
1815{
1816 u64 addr = data & ~KVM_MSR_ENABLED;
1817 if (!IS_ALIGNED(addr, 4))
1818 return 1;
1819
1820 vcpu->arch.pv_eoi.msr_val = data;
1821 if (!pv_eoi_enabled(vcpu))
1822 return 0;
1823 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
1824 addr);
1825}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001826
Jan Kiszka66450a22013-03-13 12:42:34 +01001827void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
1828{
1829 struct kvm_lapic *apic = vcpu->arch.apic;
1830 unsigned int sipi_vector;
1831
1832 if (!kvm_vcpu_has_lapic(vcpu))
1833 return;
1834
1835 if (test_and_clear_bit(KVM_APIC_INIT, &apic->pending_events)) {
1836 kvm_lapic_reset(vcpu);
1837 kvm_vcpu_reset(vcpu);
1838 if (kvm_vcpu_is_bsp(apic->vcpu))
1839 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1840 else
1841 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
1842 }
1843 if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events) &&
1844 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
1845 /* evaluate pending_events before reading the vector */
1846 smp_rmb();
1847 sipi_vector = apic->sipi_vector;
1848 pr_debug("vcpu %d received sipi with vector # %x\n",
1849 vcpu->vcpu_id, sipi_vector);
1850 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
1851 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1852 }
1853}
1854
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001855void kvm_lapic_init(void)
1856{
1857 /* do not patch jump label more than once per second */
1858 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001859 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001860}