blob: 53454a6775bf8b3e08a54f3772cc056e54ea7355 [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 Sharma60063492011-07-26 16:09:06 -070036#include <linux/atomic.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030037#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030038#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030039#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030040#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020041#include "cpuid.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030042
Marcelo Tosattib682b812009-02-10 20:41:41 -020043#ifndef CONFIG_X86_64
44#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
45#else
46#define mod_64(x, y) ((x) % (y))
47#endif
48
Eddie Dong97222cc2007-09-12 10:58:04 +030049#define PRId64 "d"
50#define PRIx64 "llx"
51#define PRIu64 "u"
52#define PRIo64 "o"
53
54#define APIC_BUS_CYCLE_NS 1
55
56/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
57#define apic_debug(fmt, arg...)
58
59#define APIC_LVT_NUM 6
60/* 14 is the version for Xeon and Pentium 8.4.8*/
61#define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
62#define LAPIC_MMIO_LENGTH (1 << 12)
63/* followed define is not in apicdef.h */
64#define APIC_SHORT_MASK 0xc0000
65#define APIC_DEST_NOSHORT 0x0
66#define APIC_DEST_MASK 0x800
67#define MAX_APIC_VECTOR 256
68
69#define VEC_POS(v) ((v) & (32 - 1))
70#define REG_POS(v) (((v) >> 5) << 4)
Zhang Xiantaoad312c72007-12-13 23:50:52 +080071
Jan Kiszka9bc57912011-09-12 14:10:22 +020072static unsigned int min_timer_period_us = 500;
73module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
74
Eddie Dong97222cc2007-09-12 10:58:04 +030075static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
76{
77 return *((u32 *) (apic->regs + reg_off));
78}
79
80static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
81{
82 *((u32 *) (apic->regs + reg_off)) = val;
83}
84
85static inline int apic_test_and_set_vector(int vec, void *bitmap)
86{
87 return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
88}
89
90static inline int apic_test_and_clear_vector(int vec, void *bitmap)
91{
92 return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
93}
94
95static inline void apic_set_vector(int vec, void *bitmap)
96{
97 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
98}
99
100static inline void apic_clear_vector(int vec, void *bitmap)
101{
102 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
103}
104
105static inline int apic_hw_enabled(struct kvm_lapic *apic)
106{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800107 return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300108}
109
110static inline int apic_sw_enabled(struct kvm_lapic *apic)
111{
112 return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
113}
114
115static inline int apic_enabled(struct kvm_lapic *apic)
116{
117 return apic_sw_enabled(apic) && apic_hw_enabled(apic);
118}
119
120#define LVT_MASK \
121 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
122
123#define LINT_MASK \
124 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
125 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
126
127static inline int kvm_apic_id(struct kvm_lapic *apic)
128{
129 return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
130}
131
132static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
133{
134 return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
135}
136
137static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
138{
139 return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
140}
141
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800142static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
143{
144 return ((apic_get_reg(apic, APIC_LVTT) &
145 apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_ONESHOT);
146}
147
Eddie Dong97222cc2007-09-12 10:58:04 +0300148static inline int apic_lvtt_period(struct kvm_lapic *apic)
149{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800150 return ((apic_get_reg(apic, APIC_LVTT) &
151 apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_PERIODIC);
152}
153
154static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
155{
156 return ((apic_get_reg(apic, APIC_LVTT) &
157 apic->lapic_timer.timer_mode_mask) ==
158 APIC_LVT_TIMER_TSCDEADLINE);
Eddie Dong97222cc2007-09-12 10:58:04 +0300159}
160
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200161static inline int apic_lvt_nmi_mode(u32 lvt_val)
162{
163 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
164}
165
Gleb Natapovfc61b802009-07-05 17:39:35 +0300166void kvm_apic_set_version(struct kvm_vcpu *vcpu)
167{
168 struct kvm_lapic *apic = vcpu->arch.apic;
169 struct kvm_cpuid_entry2 *feat;
170 u32 v = APIC_VERSION;
171
172 if (!irqchip_in_kernel(vcpu->kvm))
173 return;
174
175 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
176 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
177 v |= APIC_LVR_DIRECTED_EOI;
178 apic_set_reg(apic, APIC_LVR, v);
179}
180
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300181static inline int apic_x2apic_mode(struct kvm_lapic *apic)
182{
183 return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
184}
185
Eddie Dong97222cc2007-09-12 10:58:04 +0300186static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800187 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300188 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
189 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
190 LINT_MASK, LINT_MASK, /* LVT0-1 */
191 LVT_MASK /* LVTERR */
192};
193
194static int find_highest_vector(void *bitmap)
195{
196 u32 *word = bitmap;
197 int word_offset = MAX_APIC_VECTOR >> 5;
198
199 while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
200 continue;
201
202 if (likely(!word_offset && !word[0]))
203 return -1;
204 else
205 return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
206}
207
208static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
209{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300210 apic->irr_pending = true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300211 return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
212}
213
Gleb Natapov33e4c682009-06-11 11:06:51 +0300214static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300215{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300216 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300217}
218
219static inline int apic_find_highest_irr(struct kvm_lapic *apic)
220{
221 int result;
222
Gleb Natapov33e4c682009-06-11 11:06:51 +0300223 if (!apic->irr_pending)
224 return -1;
225
226 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300227 ASSERT(result == -1 || result >= 16);
228
229 return result;
230}
231
Gleb Natapov33e4c682009-06-11 11:06:51 +0300232static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
233{
234 apic->irr_pending = false;
235 apic_clear_vector(vec, apic->regs + APIC_IRR);
236 if (apic_search_irr(apic) != -1)
237 apic->irr_pending = true;
238}
239
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800240int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
241{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800242 struct kvm_lapic *apic = vcpu->arch.apic;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800243 int highest_irr;
244
Gleb Natapov33e4c682009-06-11 11:06:51 +0300245 /* This may race with setting of irr in __apic_accept_irq() and
246 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
247 * will cause vmexit immediately and the value will be recalculated
248 * on the next vmentry.
249 */
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800250 if (!apic)
251 return 0;
252 highest_irr = apic_find_highest_irr(apic);
253
254 return highest_irr;
255}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800256
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200257static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
258 int vector, int level, int trig_mode);
259
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200260int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Eddie Dong97222cc2007-09-12 10:58:04 +0300261{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800262 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800263
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200264 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
265 irq->level, irq->trig_mode);
Eddie Dong97222cc2007-09-12 10:58:04 +0300266}
267
268static inline int apic_find_highest_isr(struct kvm_lapic *apic)
269{
270 int result;
271
272 result = find_highest_vector(apic->regs + APIC_ISR);
273 ASSERT(result == -1 || result >= 16);
274
275 return result;
276}
277
278static void apic_update_ppr(struct kvm_lapic *apic)
279{
Avi Kivity3842d132010-07-27 12:30:24 +0300280 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300281 int isr;
282
Avi Kivity3842d132010-07-27 12:30:24 +0300283 old_ppr = apic_get_reg(apic, APIC_PROCPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300284 tpr = apic_get_reg(apic, APIC_TASKPRI);
285 isr = apic_find_highest_isr(apic);
286 isrv = (isr != -1) ? isr : 0;
287
288 if ((tpr & 0xf0) >= (isrv & 0xf0))
289 ppr = tpr & 0xff;
290 else
291 ppr = isrv & 0xf0;
292
293 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
294 apic, ppr, isr, isrv);
295
Avi Kivity3842d132010-07-27 12:30:24 +0300296 if (old_ppr != ppr) {
297 apic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200298 if (ppr < old_ppr)
299 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300300 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300301}
302
303static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
304{
305 apic_set_reg(apic, APIC_TASKPRI, tpr);
306 apic_update_ppr(apic);
307}
308
309int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
310{
Gleb Natapov343f94f2009-03-05 16:34:54 +0200311 return dest == 0xff || kvm_apic_id(apic) == dest;
Eddie Dong97222cc2007-09-12 10:58:04 +0300312}
313
314int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
315{
316 int result = 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300317 u32 logical_id;
318
319 if (apic_x2apic_mode(apic)) {
320 logical_id = apic_get_reg(apic, APIC_LDR);
321 return logical_id & mda;
322 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300323
324 logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
325
326 switch (apic_get_reg(apic, APIC_DFR)) {
327 case APIC_DFR_FLAT:
328 if (logical_id & mda)
329 result = 1;
330 break;
331 case APIC_DFR_CLUSTER:
332 if (((logical_id >> 4) == (mda >> 0x4))
333 && (logical_id & mda & 0xf))
334 result = 1;
335 break;
336 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200337 apic_debug("Bad DFR vcpu %d: %08x\n",
338 apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
Eddie Dong97222cc2007-09-12 10:58:04 +0300339 break;
340 }
341
342 return result;
343}
344
Gleb Natapov343f94f2009-03-05 16:34:54 +0200345int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Eddie Dong97222cc2007-09-12 10:58:04 +0300346 int short_hand, int dest, int dest_mode)
347{
348 int result = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800349 struct kvm_lapic *target = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300350
351 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200352 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300353 target, source, dest, dest_mode, short_hand);
354
Zachary Amsdenbd371392010-06-14 11:42:15 -1000355 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300356 switch (short_hand) {
357 case APIC_DEST_NOSHORT:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200358 if (dest_mode == 0)
Eddie Dong97222cc2007-09-12 10:58:04 +0300359 /* Physical mode. */
Gleb Natapov343f94f2009-03-05 16:34:54 +0200360 result = kvm_apic_match_physical_addr(target, dest);
361 else
Eddie Dong97222cc2007-09-12 10:58:04 +0300362 /* Logical mode. */
363 result = kvm_apic_match_logical_addr(target, dest);
364 break;
365 case APIC_DEST_SELF:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200366 result = (target == source);
Eddie Dong97222cc2007-09-12 10:58:04 +0300367 break;
368 case APIC_DEST_ALLINC:
369 result = 1;
370 break;
371 case APIC_DEST_ALLBUT:
Gleb Natapov343f94f2009-03-05 16:34:54 +0200372 result = (target != source);
Eddie Dong97222cc2007-09-12 10:58:04 +0300373 break;
374 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200375 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
376 short_hand);
Eddie Dong97222cc2007-09-12 10:58:04 +0300377 break;
378 }
379
380 return result;
381}
382
383/*
384 * Add a pending IRQ into lapic.
385 * Return 1 if successfully added and 0 if discarded.
386 */
387static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
388 int vector, int level, int trig_mode)
389{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200390 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300391 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300392
393 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300394 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200395 vcpu->arch.apic_arb_prio++;
396 case APIC_DM_FIXED:
Eddie Dong97222cc2007-09-12 10:58:04 +0300397 /* FIXME add logic for vcpu on reset */
398 if (unlikely(!apic_enabled(apic)))
399 break;
400
Avi Kivitya5d36f82009-12-29 12:42:16 +0200401 if (trig_mode) {
402 apic_debug("level trig mode for vector %d", vector);
403 apic_set_vector(vector, apic->regs + APIC_TMR);
404 } else
405 apic_clear_vector(vector, apic->regs + APIC_TMR);
406
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200407 result = !apic_test_and_set_irr(vector, apic);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300408 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
Gleb Natapov4da74892009-08-27 16:25:04 +0300409 trig_mode, vector, !result);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200410 if (!result) {
411 if (trig_mode)
412 apic_debug("level trig mode repeatedly for "
413 "vector %d", vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300414 break;
415 }
416
Avi Kivity3842d132010-07-27 12:30:24 +0300417 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -0300418 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300419 break;
420
421 case APIC_DM_REMRD:
Jan Kiszka7712de82011-09-12 11:25:51 +0200422 apic_debug("Ignoring delivery mode 3\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300423 break;
424
425 case APIC_DM_SMI:
Jan Kiszka7712de82011-09-12 11:25:51 +0200426 apic_debug("Ignoring guest SMI\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300427 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800428
Eddie Dong97222cc2007-09-12 10:58:04 +0300429 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200430 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800431 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200432 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300433 break;
434
435 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100436 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200437 result = 1;
Avi Kivitya4535292008-04-13 17:54:35 +0300438 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
Avi Kivity3842d132010-07-27 12:30:24 +0300439 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300440 kvm_vcpu_kick(vcpu);
441 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200442 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
443 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300444 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300445 break;
446
447 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200448 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
449 vcpu->vcpu_id, vector);
Avi Kivitya4535292008-04-13 17:54:35 +0300450 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200451 result = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800452 vcpu->arch.sipi_vector = vector;
Avi Kivitya4535292008-04-13 17:54:35 +0300453 vcpu->arch.mp_state = KVM_MP_STATE_SIPI_RECEIVED;
Avi Kivity3842d132010-07-27 12:30:24 +0300454 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -0300455 kvm_vcpu_kick(vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300456 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300457 break;
458
Jan Kiszka23930f92008-09-26 09:30:52 +0200459 case APIC_DM_EXTINT:
460 /*
461 * Should only be called by kvm_apic_local_deliver() with LVT0,
462 * before NMI watchdog was enabled. Already handled by
463 * kvm_apic_accept_pic_intr().
464 */
465 break;
466
Eddie Dong97222cc2007-09-12 10:58:04 +0300467 default:
468 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
469 delivery_mode);
470 break;
471 }
472 return result;
473}
474
Gleb Natapove1035712009-03-05 16:34:59 +0200475int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +0300476{
Gleb Natapove1035712009-03-05 16:34:59 +0200477 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800478}
479
Eddie Dong97222cc2007-09-12 10:58:04 +0300480static void apic_set_eoi(struct kvm_lapic *apic)
481{
482 int vector = apic_find_highest_isr(apic);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300483 int trigger_mode;
Eddie Dong97222cc2007-09-12 10:58:04 +0300484 /*
485 * Not every write EOI will has corresponding ISR,
486 * one example is when Kernel check timer on setup_IO_APIC
487 */
488 if (vector == -1)
489 return;
490
491 apic_clear_vector(vector, apic->regs + APIC_ISR);
492 apic_update_ppr(apic);
493
494 if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
Marcelo Tosattif5244722008-07-26 17:01:00 -0300495 trigger_mode = IOAPIC_LEVEL_TRIG;
496 else
497 trigger_mode = IOAPIC_EDGE_TRIG;
Gleb Natapoveba02262009-08-24 11:54:25 +0300498 if (!(apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300499 kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
Avi Kivity3842d132010-07-27 12:30:24 +0300500 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300501}
502
503static void apic_send_ipi(struct kvm_lapic *apic)
504{
505 u32 icr_low = apic_get_reg(apic, APIC_ICR);
506 u32 icr_high = apic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200507 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +0300508
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200509 irq.vector = icr_low & APIC_VECTOR_MASK;
510 irq.delivery_mode = icr_low & APIC_MODE_MASK;
511 irq.dest_mode = icr_low & APIC_DEST_MASK;
512 irq.level = icr_low & APIC_INT_ASSERT;
513 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
514 irq.shorthand = icr_low & APIC_SHORT_MASK;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300515 if (apic_x2apic_mode(apic))
516 irq.dest_id = icr_high;
517 else
518 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +0300519
Gleb Natapov1000ff82009-07-07 16:00:57 +0300520 trace_kvm_apic_ipi(icr_low, irq.dest_id);
521
Eddie Dong97222cc2007-09-12 10:58:04 +0300522 apic_debug("icr_high 0x%x, icr_low 0x%x, "
523 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
524 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -0400525 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200526 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
527 irq.vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300528
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200529 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq);
Eddie Dong97222cc2007-09-12 10:58:04 +0300530}
531
532static u32 apic_get_tmcct(struct kvm_lapic *apic)
533{
Marcelo Tosattib682b812009-02-10 20:41:41 -0200534 ktime_t remaining;
535 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200536 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +0300537
538 ASSERT(apic != NULL);
539
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200540 /* if initial count is 0, current count should also be 0 */
Andy Honigcd1e0202013-11-19 14:12:18 -0800541 if (apic_get_reg(apic, APIC_TMICT) == 0 ||
542 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200543 return 0;
544
Marcelo Tosattiace15462009-10-08 10:55:03 -0300545 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -0200546 if (ktime_to_ns(remaining) < 0)
547 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300548
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300549 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
550 tmcct = div64_u64(ns,
551 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +0300552
553 return tmcct;
554}
555
Avi Kivityb209749f2007-10-22 16:50:39 +0200556static void __report_tpr_access(struct kvm_lapic *apic, bool write)
557{
558 struct kvm_vcpu *vcpu = apic->vcpu;
559 struct kvm_run *run = vcpu->run;
560
Avi Kivitya8eeb042010-05-10 12:34:53 +0300561 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300562 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +0200563 run->tpr_access.is_write = write;
564}
565
566static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
567{
568 if (apic->vcpu->arch.tpr_access_reporting)
569 __report_tpr_access(apic, write);
570}
571
Eddie Dong97222cc2007-09-12 10:58:04 +0300572static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
573{
574 u32 val = 0;
575
576 if (offset >= LAPIC_MMIO_LENGTH)
577 return 0;
578
579 switch (offset) {
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300580 case APIC_ID:
581 if (apic_x2apic_mode(apic))
582 val = kvm_apic_id(apic);
583 else
584 val = kvm_apic_id(apic) << 24;
585 break;
Eddie Dong97222cc2007-09-12 10:58:04 +0300586 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +0200587 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +0300588 break;
589
590 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800591 if (apic_lvtt_tscdeadline(apic))
592 return 0;
593
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 val = apic_get_tmcct(apic);
595 break;
596
Avi Kivityb209749f2007-10-22 16:50:39 +0200597 case APIC_TASKPRI:
598 report_tpr_access(apic, false);
599 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +0300600 default:
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800601 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300602 val = apic_get_reg(apic, offset);
603 break;
604 }
605
606 return val;
607}
608
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400609static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
610{
611 return container_of(dev, struct kvm_lapic, dev);
612}
613
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300614static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
615 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300616{
Eddie Dong97222cc2007-09-12 10:58:04 +0300617 unsigned char alignment = offset & 0xf;
618 u32 result;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300619 /* this bitmask has a bit cleared for each reserver register */
620 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300621
622 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +0300623 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
624 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300625 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300626 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300627
628 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +0300629 apic_debug("KVM_APIC_READ: read reserved register %x\n",
630 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300631 return 1;
632 }
633
Eddie Dong97222cc2007-09-12 10:58:04 +0300634 result = __apic_read(apic, offset & ~0xf);
635
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300636 trace_kvm_apic_read(offset, result);
637
Eddie Dong97222cc2007-09-12 10:58:04 +0300638 switch (len) {
639 case 1:
640 case 2:
641 case 4:
642 memcpy(data, (char *)&result + alignment, len);
643 break;
644 default:
645 printk(KERN_ERR "Local APIC read with len = %x, "
646 "should be 1,2, or 4 instead\n", len);
647 break;
648 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300649 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300650}
651
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300652static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
653{
654 return apic_hw_enabled(apic) &&
655 addr >= apic->base_address &&
656 addr < apic->base_address + LAPIC_MMIO_LENGTH;
657}
658
659static int apic_mmio_read(struct kvm_io_device *this,
660 gpa_t address, int len, void *data)
661{
662 struct kvm_lapic *apic = to_lapic(this);
663 u32 offset = address - apic->base_address;
664
665 if (!apic_mmio_in_range(apic, address))
666 return -EOPNOTSUPP;
667
668 apic_reg_read(apic, offset, len, data);
669
670 return 0;
671}
672
Eddie Dong97222cc2007-09-12 10:58:04 +0300673static void update_divide_count(struct kvm_lapic *apic)
674{
675 u32 tmp1, tmp2, tdcr;
676
677 tdcr = apic_get_reg(apic, APIC_TDCR);
678 tmp1 = tdcr & 0xf;
679 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300680 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +0300681
682 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -0400683 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +0300684}
685
686static void start_apic_timer(struct kvm_lapic *apic)
687{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800688 ktime_t now;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300689 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +0200690
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800691 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
692 /* lapic timer in oneshot or peroidic mode */
693 now = apic->lapic_timer.timer.base->get_time();
694 apic->lapic_timer.period = (u64)apic_get_reg(apic, APIC_TMICT)
695 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +0200696
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800697 if (!apic->lapic_timer.period)
698 return;
699 /*
700 * Do not allow the guest to program periodic timers with small
701 * interval, since the hrtimers are not throttled by the host
702 * scheduler.
703 */
704 if (apic_lvtt_period(apic)) {
705 s64 min_period = min_timer_period_us * 1000LL;
706
707 if (apic->lapic_timer.period < min_period) {
708 pr_info_ratelimited(
709 "kvm: vcpu %i: requested %lld ns "
710 "lapic timer period limited to %lld ns\n",
711 apic->vcpu->vcpu_id,
712 apic->lapic_timer.period, min_period);
713 apic->lapic_timer.period = min_period;
714 }
Jan Kiszka9bc57912011-09-12 14:10:22 +0200715 }
Avi Kivity0b975a32008-02-24 14:37:50 +0200716
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800717 hrtimer_start(&apic->lapic_timer.timer,
718 ktime_add_ns(now, apic->lapic_timer.period),
719 HRTIMER_MODE_ABS);
Eddie Dong97222cc2007-09-12 10:58:04 +0300720
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800721 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +0300722 PRIx64 ", "
723 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800724 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +0300725 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
726 apic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300727 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +0300728 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300729 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800730 } else if (apic_lvtt_tscdeadline(apic)) {
731 /* lapic timer in tsc deadline mode */
732 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
733 u64 ns = 0;
734 struct kvm_vcpu *vcpu = apic->vcpu;
Zachary Amsdencc578282012-02-03 15:43:50 -0200735 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800736 unsigned long flags;
737
738 if (unlikely(!tscdeadline || !this_tsc_khz))
739 return;
740
741 local_irq_save(flags);
742
743 now = apic->lapic_timer.timer.base->get_time();
744 guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu);
745 if (likely(tscdeadline > guest_tsc)) {
746 ns = (tscdeadline - guest_tsc) * 1000000ULL;
747 do_div(ns, this_tsc_khz);
748 }
749 hrtimer_start(&apic->lapic_timer.timer,
750 ktime_add_ns(now, ns), HRTIMER_MODE_ABS);
751
752 local_irq_restore(flags);
753 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300754}
755
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200756static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
757{
758 int nmi_wd_enabled = apic_lvt_nmi_mode(apic_get_reg(apic, APIC_LVT0));
759
760 if (apic_lvt_nmi_mode(lvt0_val)) {
761 if (!nmi_wd_enabled) {
762 apic_debug("Receive NMI setting on APIC_LVT0 "
763 "for cpu %d\n", apic->vcpu->vcpu_id);
José Adolfo Galdámez9cc712e2015-10-21 21:52:13 -0600764 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200765 }
766 } else if (nmi_wd_enabled)
José Adolfo Galdámez9cc712e2015-10-21 21:52:13 -0600767 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200768}
769
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300770static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +0300771{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300772 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300773
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300774 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300775
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300776 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300777 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300778 if (!apic_x2apic_mode(apic))
779 apic_set_reg(apic, APIC_ID, val);
780 else
781 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300782 break;
783
784 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +0200785 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +0300786 apic_set_tpr(apic, val & 0xff);
787 break;
788
789 case APIC_EOI:
790 apic_set_eoi(apic);
791 break;
792
793 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300794 if (!apic_x2apic_mode(apic))
795 apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
796 else
797 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300798 break;
799
800 case APIC_DFR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300801 if (!apic_x2apic_mode(apic))
802 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
803 else
804 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300805 break;
806
Gleb Natapovfc61b802009-07-05 17:39:35 +0300807 case APIC_SPIV: {
808 u32 mask = 0x3ff;
809 if (apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
810 mask |= APIC_SPIV_DIRECTED_EOI;
811 apic_set_reg(apic, APIC_SPIV, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +0300812 if (!(val & APIC_SPIV_APIC_ENABLED)) {
813 int i;
814 u32 lvt_val;
815
816 for (i = 0; i < APIC_LVT_NUM; i++) {
817 lvt_val = apic_get_reg(apic,
818 APIC_LVTT + 0x10 * i);
819 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
820 lvt_val | APIC_LVT_MASKED);
821 }
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300822 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300823
824 }
825 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +0300826 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300827 case APIC_ICR:
828 /* No delay here, so we always clear the pending bit */
829 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
830 apic_send_ipi(apic);
831 break;
832
833 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300834 if (!apic_x2apic_mode(apic))
835 val &= 0xff000000;
836 apic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300837 break;
838
Jan Kiszka23930f92008-09-26 09:30:52 +0200839 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200840 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300841 case APIC_LVTTHMR:
842 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +0300843 case APIC_LVT1:
844 case APIC_LVTERR:
845 /* TODO: Check vector */
846 if (!apic_sw_enabled(apic))
847 val |= APIC_LVT_MASKED;
848
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300849 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
850 apic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300851
852 break;
853
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800854 case APIC_LVTT:
855 if ((apic_get_reg(apic, APIC_LVTT) &
856 apic->lapic_timer.timer_mode_mask) !=
857 (val & apic->lapic_timer.timer_mode_mask))
858 hrtimer_cancel(&apic->lapic_timer.timer);
859
860 if (!apic_sw_enabled(apic))
861 val |= APIC_LVT_MASKED;
862 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
863 apic_set_reg(apic, APIC_LVTT, val);
864 break;
865
Eddie Dong97222cc2007-09-12 10:58:04 +0300866 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800867 if (apic_lvtt_tscdeadline(apic))
868 break;
869
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300870 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +0300871 apic_set_reg(apic, APIC_TMICT, val);
872 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300873 break;
Eddie Dong97222cc2007-09-12 10:58:04 +0300874
875 case APIC_TDCR:
876 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +0200877 apic_debug("KVM_WRITE:TDCR %x\n", val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300878 apic_set_reg(apic, APIC_TDCR, val);
879 update_divide_count(apic);
880 break;
881
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300882 case APIC_ESR:
883 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +0200884 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300885 ret = 1;
886 }
887 break;
888
889 case APIC_SELF_IPI:
890 if (apic_x2apic_mode(apic)) {
891 apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
892 } else
893 ret = 1;
894 break;
Eddie Dong97222cc2007-09-12 10:58:04 +0300895 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300896 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300897 break;
898 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300899 if (ret)
900 apic_debug("Local APIC Write to read-only register %x\n", reg);
901 return ret;
902}
903
904static int apic_mmio_write(struct kvm_io_device *this,
905 gpa_t address, int len, const void *data)
906{
907 struct kvm_lapic *apic = to_lapic(this);
908 unsigned int offset = address - apic->base_address;
909 u32 val;
910
911 if (!apic_mmio_in_range(apic, address))
912 return -EOPNOTSUPP;
913
914 /*
915 * APIC register must be aligned on 128-bits boundary.
916 * 32/64/128 bits registers must be accessed thru 32 bits.
917 * Refer SDM 8.4.1
918 */
919 if (len != 4 || (offset & 0xf)) {
920 /* Don't shout loud, $infamous_os would cause only noise. */
921 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +0800922 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300923 }
924
925 val = *(u32*)data;
926
927 /* too common printing */
928 if (offset != APIC_EOI)
929 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
930 "0x%x\n", __func__, offset, len, val);
931
932 apic_reg_write(apic, offset & 0xff0, val);
933
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300934 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300935}
936
Kevin Tian58fbbf22011-08-30 13:56:17 +0300937void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
938{
939 struct kvm_lapic *apic = vcpu->arch.apic;
940
941 if (apic)
942 apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
943}
944EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
945
Rusty Russelld5894442007-10-08 10:48:30 +1000946void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +0300947{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800948 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300949 return;
950
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300951 hrtimer_cancel(&vcpu->arch.apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +0300952
Takuya Yoshikawaafc20182011-03-05 12:40:20 +0900953 if (vcpu->arch.apic->regs)
954 free_page((unsigned long)vcpu->arch.apic->regs);
Eddie Dong97222cc2007-09-12 10:58:04 +0300955
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800956 kfree(vcpu->arch.apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300957}
958
959/*
960 *----------------------------------------------------------------------
961 * LAPIC interface
962 *----------------------------------------------------------------------
963 */
964
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800965u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
966{
967 struct kvm_lapic *apic = vcpu->arch.apic;
968 if (!apic)
969 return 0;
970
971 if (apic_lvtt_oneshot(apic) || apic_lvtt_period(apic))
972 return 0;
973
974 return apic->lapic_timer.tscdeadline;
975}
976
977void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
978{
979 struct kvm_lapic *apic = vcpu->arch.apic;
980 if (!apic)
981 return;
982
983 if (apic_lvtt_oneshot(apic) || apic_lvtt_period(apic))
984 return;
985
986 hrtimer_cancel(&apic->lapic_timer.timer);
987 apic->lapic_timer.tscdeadline = data;
988 start_apic_timer(apic);
989}
990
Eddie Dong97222cc2007-09-12 10:58:04 +0300991void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
992{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800993 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300994
995 if (!apic)
996 return;
Avi Kivityb93463a2007-10-25 16:52:32 +0200997 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
998 | (apic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +0300999}
1000
1001u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1002{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001003 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001004 u64 tpr;
1005
1006 if (!apic)
1007 return 0;
1008 tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
1009
1010 return (tpr & 0xf0) >> 4;
1011}
1012
1013void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1014{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001015 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001016
1017 if (!apic) {
1018 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001019 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001020 return;
1021 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001022
1023 if (!kvm_vcpu_is_bsp(apic->vcpu))
Eddie Dong97222cc2007-09-12 10:58:04 +03001024 value &= ~MSR_IA32_APICBASE_BSP;
1025
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001026 vcpu->arch.apic_base = value;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001027 if (apic_x2apic_mode(apic)) {
1028 u32 id = kvm_apic_id(apic);
1029 u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
1030 apic_set_reg(apic, APIC_LDR, ldr);
1031 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001032 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001033 MSR_IA32_APICBASE_BASE;
1034
1035 /* with FSB delivery interrupt, we can restart APIC functionality */
1036 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001037 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001038
1039}
1040
He, Qingc5ec1532007-09-03 17:07:41 +03001041void kvm_lapic_reset(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001042{
1043 struct kvm_lapic *apic;
1044 int i;
1045
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001046 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001047
1048 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001049 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001050 ASSERT(apic != NULL);
1051
1052 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001053 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001054
1055 apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
Gleb Natapovfc61b802009-07-05 17:39:35 +03001056 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001057
1058 for (i = 0; i < APIC_LVT_NUM; i++)
1059 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Qing He40487c62007-09-17 14:47:13 +08001060 apic_set_reg(apic, APIC_LVT0,
1061 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Eddie Dong97222cc2007-09-12 10:58:04 +03001062
1063 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
1064 apic_set_reg(apic, APIC_SPIV, 0xff);
1065 apic_set_reg(apic, APIC_TASKPRI, 0);
1066 apic_set_reg(apic, APIC_LDR, 0);
1067 apic_set_reg(apic, APIC_ESR, 0);
1068 apic_set_reg(apic, APIC_ICR, 0);
1069 apic_set_reg(apic, APIC_ICR2, 0);
1070 apic_set_reg(apic, APIC_TDCR, 0);
1071 apic_set_reg(apic, APIC_TMICT, 0);
1072 for (i = 0; i < 8; i++) {
1073 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1074 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1075 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
1076 }
Gleb Natapov33e4c682009-06-11 11:06:51 +03001077 apic->irr_pending = false;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001078 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001079 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001080 if (kvm_vcpu_is_bsp(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001081 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
Eddie Dong97222cc2007-09-12 10:58:04 +03001082 apic_update_ppr(apic);
1083
Gleb Natapove1035712009-03-05 16:34:59 +02001084 vcpu->arch.apic_arb_prio = 0;
1085
Eddie Dong97222cc2007-09-12 10:58:04 +03001086 apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001087 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001088 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001089 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001090}
1091
Gleb Natapov343f94f2009-03-05 16:34:54 +02001092bool kvm_apic_present(struct kvm_vcpu *vcpu)
1093{
1094 return vcpu->arch.apic && apic_hw_enabled(vcpu->arch.apic);
1095}
1096
Eddie Dong97222cc2007-09-12 10:58:04 +03001097int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
1098{
Gleb Natapov343f94f2009-03-05 16:34:54 +02001099 return kvm_apic_present(vcpu) && apic_sw_enabled(vcpu->arch.apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001100}
1101
1102/*
1103 *----------------------------------------------------------------------
1104 * timer interface
1105 *----------------------------------------------------------------------
1106 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001107
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001108static bool lapic_is_periodic(struct kvm_timer *ktimer)
Eddie Dong97222cc2007-09-12 10:58:04 +03001109{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001110 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic,
1111 lapic_timer);
1112 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001113}
1114
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001115int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1116{
1117 struct kvm_lapic *lapic = vcpu->arch.apic;
1118
Marcelo Tosatti54aaace2008-05-14 02:29:06 -03001119 if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001120 return atomic_read(&lapic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001121
1122 return 0;
1123}
1124
Avi Kivity89342082011-11-10 14:57:21 +02001125int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001126{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001127 u32 reg = apic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001128 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001129
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001130 if (apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001131 vector = reg & APIC_VECTOR_MASK;
1132 mode = reg & APIC_MODE_MASK;
1133 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
1134 return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
1135 }
1136 return 0;
1137}
1138
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001139void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001140{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001141 struct kvm_lapic *apic = vcpu->arch.apic;
1142
1143 if (apic)
1144 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001145}
1146
Hannes Eder386eb6e2009-03-10 22:51:09 +01001147static struct kvm_timer_ops lapic_timer_ops = {
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001148 .is_periodic = lapic_is_periodic,
1149};
Eddie Dong97222cc2007-09-12 10:58:04 +03001150
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001151static const struct kvm_io_device_ops apic_mmio_ops = {
1152 .read = apic_mmio_read,
1153 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001154};
1155
Eddie Dong97222cc2007-09-12 10:58:04 +03001156int kvm_create_lapic(struct kvm_vcpu *vcpu)
1157{
1158 struct kvm_lapic *apic;
1159
1160 ASSERT(vcpu != NULL);
1161 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1162
1163 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1164 if (!apic)
1165 goto nomem;
1166
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001167 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001168
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001169 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1170 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001171 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1172 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001173 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001174 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001175 apic->vcpu = vcpu;
1176
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001177 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
1178 HRTIMER_MODE_ABS);
1179 apic->lapic_timer.timer.function = kvm_timer_fn;
1180 apic->lapic_timer.t_ops = &lapic_timer_ops;
1181 apic->lapic_timer.kvm = vcpu->kvm;
Gleb Natapov1ed0ce02009-06-09 15:56:27 +03001182 apic->lapic_timer.vcpu = vcpu;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001183
Eddie Dong97222cc2007-09-12 10:58:04 +03001184 apic->base_address = APIC_DEFAULT_PHYS_BASE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001185 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
Eddie Dong97222cc2007-09-12 10:58:04 +03001186
He, Qingc5ec1532007-09-03 17:07:41 +03001187 kvm_lapic_reset(vcpu);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001188 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001189
1190 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001191nomem_free_apic:
1192 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001193nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001194 return -ENOMEM;
1195}
Eddie Dong97222cc2007-09-12 10:58:04 +03001196
1197int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001199 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001200 int highest_irr;
1201
1202 if (!apic || !apic_enabled(apic))
1203 return -1;
1204
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001205 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001206 highest_irr = apic_find_highest_irr(apic);
1207 if ((highest_irr == -1) ||
1208 ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
1209 return -1;
1210 return highest_irr;
1211}
1212
Qing He40487c62007-09-17 14:47:13 +08001213int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1214{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001215 u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001216 int r = 0;
1217
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001218 if (!apic_hw_enabled(vcpu->arch.apic))
1219 r = 1;
1220 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1221 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1222 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001223 return r;
1224}
1225
Eddie Dong1b9778d2007-09-03 16:56:58 +03001226void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1227{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001228 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001229
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001230 if (apic && atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001231 if (kvm_apic_local_deliver(apic, APIC_LVTT))
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001232 atomic_dec(&apic->lapic_timer.pending);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001233 }
1234}
1235
Eddie Dong97222cc2007-09-12 10:58:04 +03001236int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1237{
1238 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001239 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001240
1241 if (vector == -1)
1242 return -1;
1243
1244 apic_set_vector(vector, apic->regs + APIC_ISR);
1245 apic_update_ppr(apic);
1246 apic_clear_irr(vector, apic);
1247 return vector;
1248}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001249
1250void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
1251{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001252 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001253
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001254 apic->base_address = vcpu->arch.apic_base &
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001255 MSR_IA32_APICBASE_BASE;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001256 kvm_apic_set_version(vcpu);
1257
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001258 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001259 hrtimer_cancel(&apic->lapic_timer.timer);
José Adolfo Galdámez9cc712e2015-10-21 21:52:13 -06001260 apic_manage_nmi_watchdog(apic, apic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001261 update_divide_count(apic);
1262 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02001263 apic->irr_pending = true;
Avi Kivity3842d132010-07-27 12:30:24 +03001264 kvm_make_request(KVM_REQ_EVENT, vcpu);
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001265}
Eddie Donga3d7f852007-09-03 16:15:12 +03001266
Avi Kivity2f52d582008-01-16 12:49:30 +02001267void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03001268{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001269 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Donga3d7f852007-09-03 16:15:12 +03001270 struct hrtimer *timer;
1271
1272 if (!apic)
1273 return;
1274
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001275 timer = &apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03001276 if (hrtimer_cancel(timer))
Arjan van de Venbeb20d52008-09-01 14:55:57 -07001277 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
Eddie Donga3d7f852007-09-03 16:15:12 +03001278}
Avi Kivityb93463a2007-10-25 16:52:32 +02001279
1280void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
1281{
1282 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02001283
1284 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1285 return;
1286
Andy Honig777f8f32013-11-20 10:23:22 -08001287 kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
1288 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02001289
1290 apic_set_tpr(vcpu->arch.apic, data & 0xff);
1291}
1292
1293void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
1294{
1295 u32 data, tpr;
1296 int max_irr, max_isr;
1297 struct kvm_lapic *apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02001298
1299 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1300 return;
1301
1302 apic = vcpu->arch.apic;
1303 tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
1304 max_irr = apic_find_highest_irr(apic);
1305 if (max_irr < 0)
1306 max_irr = 0;
1307 max_isr = apic_find_highest_isr(apic);
1308 if (max_isr < 0)
1309 max_isr = 0;
1310 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1311
Andy Honig777f8f32013-11-20 10:23:22 -08001312 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
1313 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02001314}
1315
Andy Honig777f8f32013-11-20 10:23:22 -08001316int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02001317{
1318 if (!irqchip_in_kernel(vcpu->kvm))
Andy Honig777f8f32013-11-20 10:23:22 -08001319 return -EINVAL;
1320
1321 if (vapic_addr) {
1322 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
1323 &vcpu->arch.apic->vapic_cache,
1324 vapic_addr, sizeof(u32)))
1325 return -EINVAL;
1326 }
Avi Kivityb93463a2007-10-25 16:52:32 +02001327
1328 vcpu->arch.apic->vapic_addr = vapic_addr;
Andy Honig777f8f32013-11-20 10:23:22 -08001329 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02001330}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001331
1332int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1333{
1334 struct kvm_lapic *apic = vcpu->arch.apic;
1335 u32 reg = (msr - APIC_BASE_MSR) << 4;
1336
1337 if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
1338 return 1;
1339
1340 /* if this is ICR write vector before command */
1341 if (msr == 0x830)
1342 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
1343 return apic_reg_write(apic, reg, (u32)data);
1344}
1345
1346int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
1347{
1348 struct kvm_lapic *apic = vcpu->arch.apic;
1349 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
1350
1351 if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
1352 return 1;
1353
1354 if (apic_reg_read(apic, reg, 4, &low))
1355 return 1;
1356 if (msr == 0x830)
1357 apic_reg_read(apic, APIC_ICR2, 4, &high);
1358
1359 *data = (((u64)high) << 32) | low;
1360
1361 return 0;
1362}
Gleb Natapov10388a02010-01-17 15:51:23 +02001363
1364int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
1365{
1366 struct kvm_lapic *apic = vcpu->arch.apic;
1367
1368 if (!irqchip_in_kernel(vcpu->kvm))
1369 return 1;
1370
1371 /* if this is ICR write vector before command */
1372 if (reg == APIC_ICR)
1373 apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
1374 return apic_reg_write(apic, reg, (u32)data);
1375}
1376
1377int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
1378{
1379 struct kvm_lapic *apic = vcpu->arch.apic;
1380 u32 low, high = 0;
1381
1382 if (!irqchip_in_kernel(vcpu->kvm))
1383 return 1;
1384
1385 if (apic_reg_read(apic, reg, 4, &low))
1386 return 1;
1387 if (reg == APIC_ICR)
1388 apic_reg_read(apic, APIC_ICR2, 4, &high);
1389
1390 *data = (((u64)high) << 32) | low;
1391
1392 return 0;
1393}