blob: 3c2a8c1130549d27d0b16d774b054902e73a423e [file] [log] [blame]
Eddie Dong97222cc2007-09-12 10:58:04 +03001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +03009 *
10 * Authors:
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 *
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030022#include <linux/kvm.h>
23#include <linux/mm.h>
24#include <linux/highmem.h>
25#include <linux/smp.h>
26#include <linux/hrtimer.h>
27#include <linux/io.h>
28#include <linux/module.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070029#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030031#include <asm/processor.h>
32#include <asm/msr.h>
33#include <asm/page.h>
34#include <asm/current.h>
35#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050036#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030038#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030040#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030042#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020043#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030044#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030045
Marcelo Tosattib682b812009-02-10 20:41:41 -020046#ifndef CONFIG_X86_64
47#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48#else
49#define mod_64(x, y) ((x) % (y))
50#endif
51
Eddie Dong97222cc2007-09-12 10:58:04 +030052#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
57#define APIC_BUS_CYCLE_NS 1
58
59/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
60#define apic_debug(fmt, arg...)
61
Eddie Dong97222cc2007-09-12 10:58:04 +030062/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050063#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030064#define LAPIC_MMIO_LENGTH (1 << 12)
65/* followed define is not in apicdef.h */
66#define APIC_SHORT_MASK 0xc0000
67#define APIC_DEST_NOSHORT 0x0
68#define APIC_DEST_MASK 0x800
69#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090070#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030071
Nadav Amit394457a2014-10-03 00:30:52 +030072#define APIC_BROADCAST 0xFF
73#define X2APIC_BROADCAST 0xFFFFFFFFul
74
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030075static inline int apic_test_vector(int vec, void *bitmap)
76{
77 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
78}
79
Yang Zhang10606912013-04-11 19:21:38 +080080bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
81{
82 struct kvm_lapic *apic = vcpu->arch.apic;
83
84 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
85 apic_test_vector(vector, apic->regs + APIC_IRR);
86}
87
Eddie Dong97222cc2007-09-12 10:58:04 +030088static inline void apic_clear_vector(int vec, void *bitmap)
89{
90 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
91}
92
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030093static inline int __apic_test_and_set_vector(int vec, void *bitmap)
94{
95 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
96}
97
98static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
99{
100 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
101}
102
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300103struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300104struct static_key_deferred apic_sw_disabled __read_mostly;
105
Eddie Dong97222cc2007-09-12 10:58:04 +0300106static inline int apic_enabled(struct kvm_lapic *apic)
107{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300108 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300109}
110
Eddie Dong97222cc2007-09-12 10:58:04 +0300111#define LVT_MASK \
112 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
113
114#define LINT_MASK \
115 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
116 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
117
Radim Krčmáře45115b2016-07-12 22:09:19 +0200118static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
119 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
120 switch (map->mode) {
121 case KVM_APIC_MODE_X2APIC: {
122 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200123 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100124
Radim Krčmáře45115b2016-07-12 22:09:19 +0200125 if (offset <= max_apic_id) {
126 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100127
Radim Krčmáře45115b2016-07-12 22:09:19 +0200128 *cluster = &map->phys_map[offset];
129 *mask = dest_id & (0xffff >> (16 - cluster_size));
130 } else {
131 *mask = 0;
132 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100133
Radim Krčmáře45115b2016-07-12 22:09:19 +0200134 return true;
135 }
136 case KVM_APIC_MODE_XAPIC_FLAT:
137 *cluster = map->xapic_flat_map;
138 *mask = dest_id & 0xff;
139 return true;
140 case KVM_APIC_MODE_XAPIC_CLUSTER:
141 *cluster = map->xapic_cluster_map[dest_id >> 4];
142 *mask = dest_id & 0xf;
143 return true;
144 default:
145 /* Not optimized. */
146 return false;
147 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100148}
149
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300150static void recalculate_apic_map(struct kvm *kvm)
151{
152 struct kvm_apic_map *new, *old = NULL;
153 struct kvm_vcpu *vcpu;
154 int i;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200155 u32 max_id = 255;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300156
157 mutex_lock(&kvm->arch.apic_map_lock);
158
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200159 kvm_for_each_vcpu(i, vcpu, kvm)
160 if (kvm_apic_present(vcpu))
161 max_id = max(max_id, kvm_apic_id(vcpu->arch.apic));
162
163 new = kzalloc(sizeof(struct kvm_apic_map) +
164 sizeof(struct kvm_lapic *) * (max_id + 1), GFP_KERNEL);
165
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300166 if (!new)
167 goto out;
168
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200169 new->max_apic_id = max_id;
170
Nadav Amit173beed2014-11-02 11:54:54 +0200171 kvm_for_each_vcpu(i, vcpu, kvm) {
172 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200173 struct kvm_lapic **cluster;
174 u16 mask;
Radim Krčmář25995e52014-11-27 23:30:19 +0100175 u32 ldr, aid;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300176
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100177 if (!kvm_apic_present(vcpu))
178 continue;
179
Radim Krčmář25995e52014-11-27 23:30:19 +0100180 aid = kvm_apic_id(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500181 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300182
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200183 if (aid <= new->max_apic_id)
Radim Krčmář25995e52014-11-27 23:30:19 +0100184 new->phys_map[aid] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100185
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100186 if (apic_x2apic_mode(apic)) {
187 new->mode |= KVM_APIC_MODE_X2APIC;
188 } else if (ldr) {
189 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500190 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100191 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
192 else
193 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
194 }
195
Radim Krčmáře45115b2016-07-12 22:09:19 +0200196 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100197 continue;
198
Radim Krčmáře45115b2016-07-12 22:09:19 +0200199 if (mask)
200 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300201 }
202out:
203 old = rcu_dereference_protected(kvm->arch.apic_map,
204 lockdep_is_held(&kvm->arch.apic_map_lock));
205 rcu_assign_pointer(kvm->arch.apic_map, new);
206 mutex_unlock(&kvm->arch.apic_map_lock);
207
208 if (old)
209 kfree_rcu(old, rcu);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800210
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700211 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300212}
213
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300214static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
215{
Radim Krčmáře4627552014-10-30 15:06:45 +0100216 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300217
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500218 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100219
220 if (enabled != apic->sw_enabled) {
221 apic->sw_enabled = enabled;
222 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300223 static_key_slow_dec_deferred(&apic_sw_disabled);
224 recalculate_apic_map(apic->vcpu->kvm);
225 } else
226 static_key_slow_inc(&apic_sw_disabled.key);
227 }
228}
229
Radim Krčmářa92e2542016-07-12 22:09:22 +0200230static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300231{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500232 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300233 recalculate_apic_map(apic->vcpu->kvm);
234}
235
236static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
237{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500238 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300239 recalculate_apic_map(apic->vcpu->kvm);
240}
241
Radim Krčmářa92e2542016-07-12 22:09:22 +0200242static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200243{
244 u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
245
Radim Krčmářa92e2542016-07-12 22:09:22 +0200246 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500247 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200248 recalculate_apic_map(apic->vcpu->kvm);
249}
250
Eddie Dong97222cc2007-09-12 10:58:04 +0300251static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
252{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500253 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300254}
255
256static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
257{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500258 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300259}
260
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800261static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
262{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100263 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800264}
265
Eddie Dong97222cc2007-09-12 10:58:04 +0300266static inline int apic_lvtt_period(struct kvm_lapic *apic)
267{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100268 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800269}
270
271static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
272{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100273 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300274}
275
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200276static inline int apic_lvt_nmi_mode(u32 lvt_val)
277{
278 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
279}
280
Gleb Natapovfc61b802009-07-05 17:39:35 +0300281void kvm_apic_set_version(struct kvm_vcpu *vcpu)
282{
283 struct kvm_lapic *apic = vcpu->arch.apic;
284 struct kvm_cpuid_entry2 *feat;
285 u32 v = APIC_VERSION;
286
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100287 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300288 return;
289
290 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
291 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
292 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500293 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300294}
295
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500296static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800297 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300298 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
299 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
300 LINT_MASK, LINT_MASK, /* LVT0-1 */
301 LVT_MASK /* LVTERR */
302};
303
304static int find_highest_vector(void *bitmap)
305{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900306 int vec;
307 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300308
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900309 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
310 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
311 reg = bitmap + REG_POS(vec);
312 if (*reg)
313 return fls(*reg) - 1 + vec;
314 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300315
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900316 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300317}
318
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300319static u8 count_vectors(void *bitmap)
320{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900321 int vec;
322 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300323 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900324
325 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
326 reg = bitmap + REG_POS(vec);
327 count += hweight32(*reg);
328 }
329
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300330 return count;
331}
332
Wincy Van705699a2015-02-03 23:58:17 +0800333void __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800334{
335 u32 i, pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800336
337 for (i = 0; i <= 7; i++) {
338 pir_val = xchg(&pir[i], 0);
339 if (pir_val)
Wincy Van705699a2015-02-03 23:58:17 +0800340 *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800341 }
342}
Wincy Van705699a2015-02-03 23:58:17 +0800343EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
344
345void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
346{
347 struct kvm_lapic *apic = vcpu->arch.apic;
348
349 __kvm_apic_update_irr(pir, apic->regs);
Radim Krčmářc77f3fa2015-10-08 20:23:33 +0200350
351 kvm_make_request(KVM_REQ_EVENT, vcpu);
Wincy Van705699a2015-02-03 23:58:17 +0800352}
Yang Zhanga20ed542013-04-11 19:25:15 +0800353EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
354
Gleb Natapov33e4c682009-06-11 11:06:51 +0300355static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300356{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300357 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300358}
359
360static inline int apic_find_highest_irr(struct kvm_lapic *apic)
361{
362 int result;
363
Yang Zhangc7c9c562013-01-25 10:18:51 +0800364 /*
365 * Note that irr_pending is just a hint. It will be always
366 * true with virtual interrupt delivery enabled.
367 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300368 if (!apic->irr_pending)
369 return -1;
370
Andrey Smetanind62caab2015-11-10 15:36:33 +0300371 if (apic->vcpu->arch.apicv_active)
372 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
Gleb Natapov33e4c682009-06-11 11:06:51 +0300373 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300374 ASSERT(result == -1 || result >= 16);
375
376 return result;
377}
378
Gleb Natapov33e4c682009-06-11 11:06:51 +0300379static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
380{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800381 struct kvm_vcpu *vcpu;
382
383 vcpu = apic->vcpu;
384
Andrey Smetanind62caab2015-11-10 15:36:33 +0300385 if (unlikely(vcpu->arch.apicv_active)) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800386 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200387 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800388 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200389 } else {
390 apic->irr_pending = false;
391 apic_clear_vector(vec, apic->regs + APIC_IRR);
392 if (apic_search_irr(apic) != -1)
393 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800394 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300395}
396
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300397static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
398{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800399 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200400
Wanpeng Li56cc2402014-08-05 12:42:24 +0800401 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
402 return;
403
404 vcpu = apic->vcpu;
405
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300406 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800407 * With APIC virtualization enabled, all caching is disabled
408 * because the processor can modify ISR under the hood. Instead
409 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300410 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300411 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200412 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800413 else {
414 ++apic->isr_count;
415 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
416 /*
417 * ISR (in service register) bit is set when injecting an interrupt.
418 * The highest vector is injected. Thus the latest bit set matches
419 * the highest bit in ISR.
420 */
421 apic->highest_isr_cache = vec;
422 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300423}
424
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200425static inline int apic_find_highest_isr(struct kvm_lapic *apic)
426{
427 int result;
428
429 /*
430 * Note that isr_count is always 1, and highest_isr_cache
431 * is always -1, with APIC virtualization enabled.
432 */
433 if (!apic->isr_count)
434 return -1;
435 if (likely(apic->highest_isr_cache != -1))
436 return apic->highest_isr_cache;
437
438 result = find_highest_vector(apic->regs + APIC_ISR);
439 ASSERT(result == -1 || result >= 16);
440
441 return result;
442}
443
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300444static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
445{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200446 struct kvm_vcpu *vcpu;
447 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
448 return;
449
450 vcpu = apic->vcpu;
451
452 /*
453 * We do get here for APIC virtualization enabled if the guest
454 * uses the Hyper-V APIC enlightenment. In this case we may need
455 * to trigger a new interrupt delivery by writing the SVI field;
456 * on the other hand isr_count and highest_isr_cache are unused
457 * and must be left alone.
458 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300459 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200460 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200461 apic_find_highest_isr(apic));
462 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300463 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200464 BUG_ON(apic->isr_count < 0);
465 apic->highest_isr_cache = -1;
466 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300467}
468
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800469int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
470{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300471 /* This may race with setting of irr in __apic_accept_irq() and
472 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
473 * will cause vmexit immediately and the value will be recalculated
474 * on the next vmentry.
475 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100476 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800477}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800478
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200479static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800480 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100481 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200482
Yang Zhangb4f22252013-04-11 19:21:37 +0800483int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100484 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300485{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800486 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800487
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200488 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800489 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300490}
491
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300492static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
493{
494
495 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
496 sizeof(val));
497}
498
499static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
500{
501
502 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
503 sizeof(*val));
504}
505
506static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
507{
508 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
509}
510
511static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
512{
513 u8 val;
514 if (pv_eoi_get_user(vcpu, &val) < 0)
515 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800516 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300517 return val & 0x1;
518}
519
520static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
521{
522 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
523 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800524 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300525 return;
526 }
527 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
528}
529
530static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
531{
532 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
533 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800534 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300535 return;
536 }
537 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
538}
539
Eddie Dong97222cc2007-09-12 10:58:04 +0300540static void apic_update_ppr(struct kvm_lapic *apic)
541{
Avi Kivity3842d132010-07-27 12:30:24 +0300542 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300543 int isr;
544
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500545 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
546 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300547 isr = apic_find_highest_isr(apic);
548 isrv = (isr != -1) ? isr : 0;
549
550 if ((tpr & 0xf0) >= (isrv & 0xf0))
551 ppr = tpr & 0xff;
552 else
553 ppr = isrv & 0xf0;
554
555 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
556 apic, ppr, isr, isrv);
557
Avi Kivity3842d132010-07-27 12:30:24 +0300558 if (old_ppr != ppr) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500559 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200560 if (ppr < old_ppr)
561 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300562 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300563}
564
565static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
566{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500567 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300568 apic_update_ppr(apic);
569}
570
Radim Krčmář03d22492015-02-12 19:41:31 +0100571static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300572{
Radim Krčmář03d22492015-02-12 19:41:31 +0100573 if (apic_x2apic_mode(apic))
574 return mda == X2APIC_BROADCAST;
575
576 return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST;
Eddie Dong97222cc2007-09-12 10:58:04 +0300577}
578
Radim Krčmář03d22492015-02-12 19:41:31 +0100579static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300580{
Radim Krčmář03d22492015-02-12 19:41:31 +0100581 if (kvm_apic_broadcast(apic, mda))
582 return true;
583
584 if (apic_x2apic_mode(apic))
585 return mda == kvm_apic_id(apic);
586
587 return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic));
Nadav Amit394457a2014-10-03 00:30:52 +0300588}
589
Radim Krčmář52c233a2015-01-29 22:48:48 +0100590static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300591{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300592 u32 logical_id;
593
Nadav Amit394457a2014-10-03 00:30:52 +0300594 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100595 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300596
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500597 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300598
Radim Krčmář9368b562015-01-29 22:48:49 +0100599 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100600 return ((logical_id >> 16) == (mda >> 16))
601 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100602
603 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Radim Krčmář03d22492015-02-12 19:41:31 +0100604 mda = GET_APIC_DEST_FIELD(mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300605
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500606 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300607 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100608 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300609 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100610 return ((logical_id >> 4) == (mda >> 4))
611 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200613 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500614 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100615 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300616 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300617}
618
Radim Krčmář03d22492015-02-12 19:41:31 +0100619/* KVM APIC implementation has two quirks
620 * - dest always begins at 0 while xAPIC MDA has offset 24,
621 * - IOxAPIC messages have to be delivered (directly) to x2APIC.
622 */
623static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
624 struct kvm_lapic *target)
625{
626 bool ipi = source != NULL;
627 bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
628
629 if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
630 return X2APIC_BROADCAST;
631
632 return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
633}
634
Radim Krčmář52c233a2015-01-29 22:48:48 +0100635bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300636 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800638 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmář03d22492015-02-12 19:41:31 +0100639 u32 mda = kvm_apic_mda(dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300640
641 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200642 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300643 target, source, dest, dest_mode, short_hand);
644
Zachary Amsdenbd371392010-06-14 11:42:15 -1000645 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300646 switch (short_hand) {
647 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100648 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100649 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200650 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100651 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300652 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100653 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300654 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100655 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300656 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100657 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300658 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200659 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
660 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100661 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300662 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300663}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500664EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300665
Feng Wu520040142016-01-25 16:53:33 +0800666int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
667 const unsigned long *bitmap, u32 bitmap_size)
668{
669 u32 mod;
670 int i, idx = -1;
671
672 mod = vector % dest_vcpus;
673
674 for (i = 0; i <= mod; i++) {
675 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
676 BUG_ON(idx == bitmap_size);
677 }
678
679 return idx;
680}
681
Radim Krčmář4efd8052016-02-12 15:00:15 +0100682static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
683{
684 if (!kvm->arch.disabled_lapic_found) {
685 kvm->arch.disabled_lapic_found = true;
686 printk(KERN_INFO
687 "Disabled LAPIC found during irq injection\n");
688 }
689}
690
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200691/* Return true if the interrupt can be handled by using *bitmap as index mask
692 * for valid destinations in *dst array.
693 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
694 * Note: we may have zero kvm_lapic destinations when we return true, which
695 * means that the interrupt should be dropped. In this case, *bitmap would be
696 * zero and *dst undefined.
697 */
698static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
699 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
700 struct kvm_apic_map *map, struct kvm_lapic ***dst,
701 unsigned long *bitmap)
702{
703 int i, lowest;
704 bool x2apic_ipi;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200705
706 if (irq->shorthand == APIC_DEST_SELF && src) {
707 *dst = src;
708 *bitmap = 1;
709 return true;
710 } else if (irq->shorthand)
711 return false;
712
713 x2apic_ipi = src && *src && apic_x2apic_mode(*src);
714 if (irq->dest_id == (x2apic_ipi ? X2APIC_BROADCAST : APIC_BROADCAST))
715 return false;
716
717 if (!map)
718 return false;
719
720 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200721 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200722 *bitmap = 0;
723 } else {
724 *dst = &map->phys_map[irq->dest_id];
725 *bitmap = 1;
726 }
727 return true;
728 }
729
Radim Krčmáře45115b2016-07-12 22:09:19 +0200730 *bitmap = 0;
731 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
732 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200733 return false;
734
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200735 if (!kvm_lowest_prio_delivery(irq))
736 return true;
737
738 if (!kvm_vector_hashing_enabled()) {
739 lowest = -1;
740 for_each_set_bit(i, bitmap, 16) {
741 if (!(*dst)[i])
742 continue;
743 if (lowest < 0)
744 lowest = i;
745 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
746 (*dst)[lowest]->vcpu) < 0)
747 lowest = i;
748 }
749 } else {
750 if (!*bitmap)
751 return true;
752
753 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
754 bitmap, 16);
755
756 if (!(*dst)[lowest]) {
757 kvm_apic_disabled_lapic_found(kvm);
758 *bitmap = 0;
759 return true;
760 }
761 }
762
763 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
764
765 return true;
766}
767
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300768bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100769 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300770{
771 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200772 unsigned long bitmap;
773 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300774 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200775 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300776
777 *r = -1;
778
779 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800780 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300781 return true;
782 }
783
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300784 rcu_read_lock();
785 map = rcu_dereference(kvm->arch.apic_map);
786
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200787 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
788 if (ret)
789 for_each_set_bit(i, &bitmap, 16) {
790 if (!dst[i])
791 continue;
792 if (*r < 0)
793 *r = 0;
794 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100795 }
796
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300797 rcu_read_unlock();
798 return ret;
799}
800
Feng Wu6228a0d2016-01-25 16:53:34 +0800801/*
802 * This routine tries to handler interrupts in posted mode, here is how
803 * it deals with different cases:
804 * - For single-destination interrupts, handle it in posted mode
805 * - Else if vector hashing is enabled and it is a lowest-priority
806 * interrupt, handle it in posted mode and use the following mechanism
807 * to find the destinaiton vCPU.
808 * 1. For lowest-priority interrupts, store all the possible
809 * destination vCPUs in an array.
810 * 2. Use "guest vector % max number of destination vCPUs" to find
811 * the right destination vCPU in the array for the lowest-priority
812 * interrupt.
813 * - Otherwise, use remapped mode to inject the interrupt.
814 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800815bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
816 struct kvm_vcpu **dest_vcpu)
817{
818 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200819 unsigned long bitmap;
820 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800821 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800822
823 if (irq->shorthand)
824 return false;
825
826 rcu_read_lock();
827 map = rcu_dereference(kvm->arch.apic_map);
828
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200829 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
830 hweight16(bitmap) == 1) {
831 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800832
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200833 if (dst[i]) {
834 *dest_vcpu = dst[i]->vcpu;
835 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +0800836 }
Feng Wu8feb4a02015-09-18 22:29:47 +0800837 }
838
Feng Wu8feb4a02015-09-18 22:29:47 +0800839 rcu_read_unlock();
840 return ret;
841}
842
Eddie Dong97222cc2007-09-12 10:58:04 +0300843/*
844 * Add a pending IRQ into lapic.
845 * Return 1 if successfully added and 0 if discarded.
846 */
847static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800848 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100849 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300850{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200851 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300852 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300853
Paolo Bonzinia183b632014-09-11 11:51:02 +0200854 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
855 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300856 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300857 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200858 vcpu->arch.apic_arb_prio++;
859 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200860 if (unlikely(trig_mode && !level))
861 break;
862
Eddie Dong97222cc2007-09-12 10:58:04 +0300863 /* FIXME add logic for vcpu on reset */
864 if (unlikely(!apic_enabled(apic)))
865 break;
866
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200867 result = 1;
868
Joerg Roedel9daa5002016-02-29 16:04:44 +0100869 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100870 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +0100871 dest_map->vectors[vcpu->vcpu_id] = vector;
872 }
Avi Kivitya5d36f82009-12-29 12:42:16 +0200873
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200874 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
875 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500876 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200877 else
878 apic_clear_vector(vector, apic->regs + APIC_TMR);
879 }
880
Andrey Smetanind62caab2015-11-10 15:36:33 +0300881 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +0800882 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200883 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500884 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800885
886 kvm_make_request(KVM_REQ_EVENT, vcpu);
887 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300888 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300889 break;
890
891 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530892 result = 1;
893 vcpu->arch.pv.pv_unhalted = 1;
894 kvm_make_request(KVM_REQ_EVENT, vcpu);
895 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300896 break;
897
898 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200899 result = 1;
900 kvm_make_request(KVM_REQ_SMI, vcpu);
901 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300902 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800903
Eddie Dong97222cc2007-09-12 10:58:04 +0300904 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200905 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800906 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200907 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300908 break;
909
910 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100911 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200912 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100913 /* assumes that there are only KVM_APIC_INIT/SIPI */
914 apic->pending_events = (1UL << KVM_APIC_INIT);
915 /* make sure pending_events is visible before sending
916 * the request */
917 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300918 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300919 kvm_vcpu_kick(vcpu);
920 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200921 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
922 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300923 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300924 break;
925
926 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200927 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
928 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100929 result = 1;
930 apic->sipi_vector = vector;
931 /* make sure sipi_vector is visible for the receiver */
932 smp_wmb();
933 set_bit(KVM_APIC_SIPI, &apic->pending_events);
934 kvm_make_request(KVM_REQ_EVENT, vcpu);
935 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300936 break;
937
Jan Kiszka23930f92008-09-26 09:30:52 +0200938 case APIC_DM_EXTINT:
939 /*
940 * Should only be called by kvm_apic_local_deliver() with LVT0,
941 * before NMI watchdog was enabled. Already handled by
942 * kvm_apic_accept_pic_intr().
943 */
944 break;
945
Eddie Dong97222cc2007-09-12 10:58:04 +0300946 default:
947 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
948 delivery_mode);
949 break;
950 }
951 return result;
952}
953
Gleb Natapove1035712009-03-05 16:34:59 +0200954int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +0300955{
Gleb Natapove1035712009-03-05 16:34:59 +0200956 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800957}
958
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200959static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
960{
Andrey Smetanin63086302015-11-10 15:36:32 +0300961 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200962}
963
Yang Zhangc7c9c562013-01-25 10:18:51 +0800964static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
965{
Steve Rutherford7543a632015-07-29 23:21:41 -0700966 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +0200967
Steve Rutherford7543a632015-07-29 23:21:41 -0700968 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
969 if (!kvm_ioapic_handles_vector(apic, vector))
970 return;
971
972 /* Request a KVM exit to inform the userspace IOAPIC. */
973 if (irqchip_split(apic->vcpu->kvm)) {
974 apic->vcpu->arch.pending_ioapic_eoi = vector;
975 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
976 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800977 }
Steve Rutherford7543a632015-07-29 23:21:41 -0700978
979 if (apic_test_vector(vector, apic->regs + APIC_TMR))
980 trigger_mode = IOAPIC_LEVEL_TRIG;
981 else
982 trigger_mode = IOAPIC_EDGE_TRIG;
983
984 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800985}
986
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300987static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300988{
989 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300990
991 trace_kvm_eoi(apic, vector);
992
Eddie Dong97222cc2007-09-12 10:58:04 +0300993 /*
994 * Not every write EOI will has corresponding ISR,
995 * one example is when Kernel check timer on setup_IO_APIC
996 */
997 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300998 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +0300999
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001000 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001001 apic_update_ppr(apic);
1002
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001003 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1004 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1005
Yang Zhangc7c9c562013-01-25 10:18:51 +08001006 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001007 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001008 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001009}
1010
Yang Zhangc7c9c562013-01-25 10:18:51 +08001011/*
1012 * this interface assumes a trap-like exit, which has already finished
1013 * desired side effect including vISR and vPPR update.
1014 */
1015void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1016{
1017 struct kvm_lapic *apic = vcpu->arch.apic;
1018
1019 trace_kvm_eoi(apic, vector);
1020
1021 kvm_ioapic_send_eoi(apic, vector);
1022 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1023}
1024EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1025
Eddie Dong97222cc2007-09-12 10:58:04 +03001026static void apic_send_ipi(struct kvm_lapic *apic)
1027{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001028 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1029 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001030 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001031
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001032 irq.vector = icr_low & APIC_VECTOR_MASK;
1033 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1034 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001035 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001036 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1037 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001038 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001039 if (apic_x2apic_mode(apic))
1040 irq.dest_id = icr_high;
1041 else
1042 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001043
Gleb Natapov1000ff82009-07-07 16:00:57 +03001044 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1045
Eddie Dong97222cc2007-09-12 10:58:04 +03001046 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1047 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001048 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1049 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001050 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001051 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001052 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001053
Yang Zhangb4f22252013-04-11 19:21:37 +08001054 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001055}
1056
1057static u32 apic_get_tmcct(struct kvm_lapic *apic)
1058{
Marcelo Tosattib682b812009-02-10 20:41:41 -02001059 ktime_t remaining;
1060 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001061 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001062
1063 ASSERT(apic != NULL);
1064
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001065 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001066 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001067 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001068 return 0;
1069
Marcelo Tosattiace15462009-10-08 10:55:03 -03001070 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001071 if (ktime_to_ns(remaining) < 0)
1072 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001073
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001074 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1075 tmcct = div64_u64(ns,
1076 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001077
1078 return tmcct;
1079}
1080
Avi Kivityb209749f2007-10-22 16:50:39 +02001081static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1082{
1083 struct kvm_vcpu *vcpu = apic->vcpu;
1084 struct kvm_run *run = vcpu->run;
1085
Avi Kivitya8eeb042010-05-10 12:34:53 +03001086 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001087 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001088 run->tpr_access.is_write = write;
1089}
1090
1091static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1092{
1093 if (apic->vcpu->arch.tpr_access_reporting)
1094 __report_tpr_access(apic, write);
1095}
1096
Eddie Dong97222cc2007-09-12 10:58:04 +03001097static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1098{
1099 u32 val = 0;
1100
1101 if (offset >= LAPIC_MMIO_LENGTH)
1102 return 0;
1103
1104 switch (offset) {
1105 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001106 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001107 break;
1108
1109 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001110 if (apic_lvtt_tscdeadline(apic))
1111 return 0;
1112
Eddie Dong97222cc2007-09-12 10:58:04 +03001113 val = apic_get_tmcct(apic);
1114 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001115 case APIC_PROCPRI:
1116 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001117 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001118 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001119 case APIC_TASKPRI:
1120 report_tpr_access(apic, false);
1121 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001122 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001123 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001124 break;
1125 }
1126
1127 return val;
1128}
1129
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001130static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1131{
1132 return container_of(dev, struct kvm_lapic, dev);
1133}
1134
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001135int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001136 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001137{
Eddie Dong97222cc2007-09-12 10:58:04 +03001138 unsigned char alignment = offset & 0xf;
1139 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001140 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001141 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001142
1143 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001144 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1145 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001146 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001147 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001148
1149 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001150 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1151 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001152 return 1;
1153 }
1154
Eddie Dong97222cc2007-09-12 10:58:04 +03001155 result = __apic_read(apic, offset & ~0xf);
1156
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001157 trace_kvm_apic_read(offset, result);
1158
Eddie Dong97222cc2007-09-12 10:58:04 +03001159 switch (len) {
1160 case 1:
1161 case 2:
1162 case 4:
1163 memcpy(data, (char *)&result + alignment, len);
1164 break;
1165 default:
1166 printk(KERN_ERR "Local APIC read with len = %x, "
1167 "should be 1,2, or 4 instead\n", len);
1168 break;
1169 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001170 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001171}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001172EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001173
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001174static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1175{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001176 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001177 addr >= apic->base_address &&
1178 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1179}
1180
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001181static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001182 gpa_t address, int len, void *data)
1183{
1184 struct kvm_lapic *apic = to_lapic(this);
1185 u32 offset = address - apic->base_address;
1186
1187 if (!apic_mmio_in_range(apic, address))
1188 return -EOPNOTSUPP;
1189
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001190 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001191
1192 return 0;
1193}
1194
Eddie Dong97222cc2007-09-12 10:58:04 +03001195static void update_divide_count(struct kvm_lapic *apic)
1196{
1197 u32 tmp1, tmp2, tdcr;
1198
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001199 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001200 tmp1 = tdcr & 0xf;
1201 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001202 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001203
1204 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001205 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001206}
1207
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001208static void apic_update_lvtt(struct kvm_lapic *apic)
1209{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001210 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001211 apic->lapic_timer.timer_mode_mask;
1212
1213 if (apic->lapic_timer.timer_mode != timer_mode) {
1214 apic->lapic_timer.timer_mode = timer_mode;
1215 hrtimer_cancel(&apic->lapic_timer.timer);
1216 }
1217}
1218
Radim Krčmář5d87db72014-10-10 19:15:08 +02001219static void apic_timer_expired(struct kvm_lapic *apic)
1220{
1221 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001222 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001223 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001224
Radim Krčmář5d87db72014-10-10 19:15:08 +02001225 if (atomic_read(&apic->lapic_timer.pending))
1226 return;
1227
1228 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001229 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001230
Marcelo Tosatti85773702016-02-19 09:46:39 +01001231 if (swait_active(q))
1232 swake_up(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001233
1234 if (apic_lvtt_tscdeadline(apic))
1235 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1236}
1237
1238/*
1239 * On APICv, this test will cause a busy wait
1240 * during a higher-priority task.
1241 */
1242
1243static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1244{
1245 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001246 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001247
1248 if (kvm_apic_hw_enabled(apic)) {
1249 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001250 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001251
Andrey Smetanind62caab2015-11-10 15:36:33 +03001252 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001253 bitmap = apic->regs + APIC_IRR;
1254
1255 if (apic_test_vector(vec, bitmap))
1256 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001257 }
1258 return false;
1259}
1260
1261void wait_lapic_expire(struct kvm_vcpu *vcpu)
1262{
1263 struct kvm_lapic *apic = vcpu->arch.apic;
1264 u64 guest_tsc, tsc_deadline;
1265
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001266 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001267 return;
1268
1269 if (apic->lapic_timer.expired_tscdeadline == 0)
1270 return;
1271
1272 if (!lapic_timer_int_injected(vcpu))
1273 return;
1274
1275 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1276 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001277 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001278 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001279
1280 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1281 if (guest_tsc < tsc_deadline)
1282 __delay(tsc_deadline - guest_tsc);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001283}
1284
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001285static void start_sw_tscdeadline(struct kvm_lapic *apic)
1286{
1287 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1288 u64 ns = 0;
1289 ktime_t expire;
1290 struct kvm_vcpu *vcpu = apic->vcpu;
1291 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1292 unsigned long flags;
1293 ktime_t now;
1294
1295 if (unlikely(!tscdeadline || !this_tsc_khz))
1296 return;
1297
1298 local_irq_save(flags);
1299
1300 now = apic->lapic_timer.timer.base->get_time();
1301 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1302 if (likely(tscdeadline > guest_tsc)) {
1303 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1304 do_div(ns, this_tsc_khz);
1305 expire = ktime_add_ns(now, ns);
1306 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1307 hrtimer_start(&apic->lapic_timer.timer,
1308 expire, HRTIMER_MODE_ABS_PINNED);
1309 } else
1310 apic_timer_expired(apic);
1311
1312 local_irq_restore(flags);
1313}
1314
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001315bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1316{
1317 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1318}
1319EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1320
Wanpeng Libd97ad02016-06-30 08:52:49 +08001321static void cancel_hv_tscdeadline(struct kvm_lapic *apic)
1322{
1323 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1324 apic->lapic_timer.hv_timer_in_use = false;
1325}
1326
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001327void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1328{
1329 struct kvm_lapic *apic = vcpu->arch.apic;
1330
1331 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1332 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Libd97ad02016-06-30 08:52:49 +08001333 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001334 apic_timer_expired(apic);
1335}
1336EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1337
Wanpeng Li196f20c2016-06-28 14:54:19 +08001338static bool start_hv_tscdeadline(struct kvm_lapic *apic)
1339{
1340 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1341
1342 if (atomic_read(&apic->lapic_timer.pending) ||
1343 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1344 if (apic->lapic_timer.hv_timer_in_use)
1345 cancel_hv_tscdeadline(apic);
1346 } else {
1347 apic->lapic_timer.hv_timer_in_use = true;
1348 hrtimer_cancel(&apic->lapic_timer.timer);
1349
1350 /* In case the sw timer triggered in the window */
1351 if (atomic_read(&apic->lapic_timer.pending))
1352 cancel_hv_tscdeadline(apic);
1353 }
1354 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1355 apic->lapic_timer.hv_timer_in_use);
1356 return apic->lapic_timer.hv_timer_in_use;
1357}
1358
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001359void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1360{
1361 struct kvm_lapic *apic = vcpu->arch.apic;
1362
1363 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1364
Wanpeng Li196f20c2016-06-28 14:54:19 +08001365 if (apic_lvtt_tscdeadline(apic))
1366 start_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001367}
1368EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1369
1370void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1371{
1372 struct kvm_lapic *apic = vcpu->arch.apic;
1373
1374 /* Possibly the TSC deadline timer is not enabled yet */
1375 if (!apic->lapic_timer.hv_timer_in_use)
1376 return;
1377
Wanpeng Libd97ad02016-06-30 08:52:49 +08001378 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001379
1380 if (atomic_read(&apic->lapic_timer.pending))
1381 return;
1382
1383 start_sw_tscdeadline(apic);
1384}
1385EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1386
Eddie Dong97222cc2007-09-12 10:58:04 +03001387static void start_apic_timer(struct kvm_lapic *apic)
1388{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001389 ktime_t now;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001390
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001391 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001392
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001393 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001394 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001395 now = apic->lapic_timer.timer.base->get_time();
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001396 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001397 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +02001398
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001399 if (!apic->lapic_timer.period)
1400 return;
1401 /*
1402 * Do not allow the guest to program periodic timers with small
1403 * interval, since the hrtimers are not throttled by the host
1404 * scheduler.
1405 */
1406 if (apic_lvtt_period(apic)) {
1407 s64 min_period = min_timer_period_us * 1000LL;
1408
1409 if (apic->lapic_timer.period < min_period) {
1410 pr_info_ratelimited(
1411 "kvm: vcpu %i: requested %lld ns "
1412 "lapic timer period limited to %lld ns\n",
1413 apic->vcpu->vcpu_id,
1414 apic->lapic_timer.period, min_period);
1415 apic->lapic_timer.period = min_period;
1416 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001417 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001418
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001419 hrtimer_start(&apic->lapic_timer.timer,
1420 ktime_add_ns(now, apic->lapic_timer.period),
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001421 HRTIMER_MODE_ABS_PINNED);
Eddie Dong97222cc2007-09-12 10:58:04 +03001422
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001423 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001424 PRIx64 ", "
1425 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001426 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001427 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001428 kvm_lapic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001429 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001430 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001431 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001432 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li196f20c2016-06-28 14:54:19 +08001433 if (!(kvm_x86_ops->set_hv_timer && start_hv_tscdeadline(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001434 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001435 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001436}
1437
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001438static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1439{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001440 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001441
Radim Krčmář59fd1322015-06-30 22:19:16 +02001442 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1443 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1444 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001445 apic_debug("Receive NMI setting on APIC_LVT0 "
1446 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001447 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001448 } else
1449 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1450 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001451}
1452
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001453int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001454{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001455 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001456
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001457 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001458
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001459 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001460 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001461 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001462 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001463 else
1464 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001465 break;
1466
1467 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001468 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001469 apic_set_tpr(apic, val & 0xff);
1470 break;
1471
1472 case APIC_EOI:
1473 apic_set_eoi(apic);
1474 break;
1475
1476 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001477 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001478 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001479 else
1480 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001481 break;
1482
1483 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001484 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001485 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001486 recalculate_apic_map(apic->vcpu->kvm);
1487 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001488 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001489 break;
1490
Gleb Natapovfc61b802009-07-05 17:39:35 +03001491 case APIC_SPIV: {
1492 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001493 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001494 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001495 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001496 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1497 int i;
1498 u32 lvt_val;
1499
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001500 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001501 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001502 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001503 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001504 lvt_val | APIC_LVT_MASKED);
1505 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001506 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001507 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001508
1509 }
1510 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001511 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001512 case APIC_ICR:
1513 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001514 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001515 apic_send_ipi(apic);
1516 break;
1517
1518 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001519 if (!apic_x2apic_mode(apic))
1520 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001521 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001522 break;
1523
Jan Kiszka23930f92008-09-26 09:30:52 +02001524 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001525 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001526 case APIC_LVTTHMR:
1527 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001528 case APIC_LVT1:
1529 case APIC_LVTERR:
1530 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001531 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001532 val |= APIC_LVT_MASKED;
1533
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001534 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001535 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001536
1537 break;
1538
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001539 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001540 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001541 val |= APIC_LVT_MASKED;
1542 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001543 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001544 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001545 break;
1546
Eddie Dong97222cc2007-09-12 10:58:04 +03001547 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001548 if (apic_lvtt_tscdeadline(apic))
1549 break;
1550
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001551 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001552 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001553 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001554 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001555
1556 case APIC_TDCR:
1557 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001558 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001559 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001560 update_divide_count(apic);
1561 break;
1562
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001563 case APIC_ESR:
1564 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001565 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001566 ret = 1;
1567 }
1568 break;
1569
1570 case APIC_SELF_IPI:
1571 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001572 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001573 } else
1574 ret = 1;
1575 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001576 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001577 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001578 break;
1579 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001580 if (ret)
1581 apic_debug("Local APIC Write to read-only register %x\n", reg);
1582 return ret;
1583}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001584EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001585
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001586static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001587 gpa_t address, int len, const void *data)
1588{
1589 struct kvm_lapic *apic = to_lapic(this);
1590 unsigned int offset = address - apic->base_address;
1591 u32 val;
1592
1593 if (!apic_mmio_in_range(apic, address))
1594 return -EOPNOTSUPP;
1595
1596 /*
1597 * APIC register must be aligned on 128-bits boundary.
1598 * 32/64/128 bits registers must be accessed thru 32 bits.
1599 * Refer SDM 8.4.1
1600 */
1601 if (len != 4 || (offset & 0xf)) {
1602 /* Don't shout loud, $infamous_os would cause only noise. */
1603 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001604 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001605 }
1606
1607 val = *(u32*)data;
1608
1609 /* too common printing */
1610 if (offset != APIC_EOI)
1611 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1612 "0x%x\n", __func__, offset, len, val);
1613
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001614 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001615
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001616 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001617}
1618
Kevin Tian58fbbf22011-08-30 13:56:17 +03001619void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1620{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001621 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001622}
1623EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1624
Yang Zhang83d4c282013-01-25 10:18:49 +08001625/* emulate APIC access in a trap manner */
1626void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1627{
1628 u32 val = 0;
1629
1630 /* hw has done the conditional check and inst decode */
1631 offset &= 0xff0;
1632
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001633 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001634
1635 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001636 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001637}
1638EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1639
Rusty Russelld5894442007-10-08 10:48:30 +10001640void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001641{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001642 struct kvm_lapic *apic = vcpu->arch.apic;
1643
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001644 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001645 return;
1646
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001647 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001648
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001649 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1650 static_key_slow_dec_deferred(&apic_hw_disabled);
1651
Radim Krčmáře4627552014-10-30 15:06:45 +01001652 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001653 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001654
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001655 if (apic->regs)
1656 free_page((unsigned long)apic->regs);
1657
1658 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001659}
1660
1661/*
1662 *----------------------------------------------------------------------
1663 * LAPIC interface
1664 *----------------------------------------------------------------------
1665 */
1666
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001667u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1668{
1669 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001670
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001671 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001672 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001673 return 0;
1674
1675 return apic->lapic_timer.tscdeadline;
1676}
1677
1678void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1679{
1680 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001681
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001682 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001683 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001684 return;
1685
1686 hrtimer_cancel(&apic->lapic_timer.timer);
1687 apic->lapic_timer.tscdeadline = data;
1688 start_apic_timer(apic);
1689}
1690
Eddie Dong97222cc2007-09-12 10:58:04 +03001691void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1692{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001693 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001694
Avi Kivityb93463a2007-10-25 16:52:32 +02001695 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001696 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001697}
1698
1699u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1700{
Eddie Dong97222cc2007-09-12 10:58:04 +03001701 u64 tpr;
1702
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001703 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001704
1705 return (tpr & 0xf0) >> 4;
1706}
1707
1708void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1709{
Yang Zhang8d146952013-01-25 10:18:50 +08001710 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001711 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001712
1713 if (!apic) {
1714 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001715 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001716 return;
1717 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001718
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001719 vcpu->arch.apic_base = value;
1720
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001721 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001722 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001723 if (value & MSR_IA32_APICBASE_ENABLE) {
1724 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001725 static_key_slow_dec_deferred(&apic_hw_disabled);
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001726 } else
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001727 static_key_slow_inc(&apic_hw_disabled.key);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001728 recalculate_apic_map(vcpu->kvm);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001729 }
1730
Yang Zhang8d146952013-01-25 10:18:50 +08001731 if ((old_value ^ value) & X2APIC_ENABLE) {
1732 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001733 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001734 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1735 } else
1736 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001737 }
Yang Zhang8d146952013-01-25 10:18:50 +08001738
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001739 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001740 MSR_IA32_APICBASE_BASE;
1741
Nadav Amitdb324fe2014-11-02 11:54:59 +02001742 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1743 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1744 pr_warn_once("APIC base relocation is unsupported by KVM");
1745
Eddie Dong97222cc2007-09-12 10:58:04 +03001746 /* with FSB delivery interrupt, we can restart APIC functionality */
1747 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001748 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001749
1750}
1751
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001752void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001753{
1754 struct kvm_lapic *apic;
1755 int i;
1756
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001757 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001758
1759 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001760 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001761 ASSERT(apic != NULL);
1762
1763 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001764 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001765
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001766 if (!init_event) {
1767 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1768 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001769 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001770 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001771 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001772
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001773 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1774 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001775 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001776 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001777 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001778 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001779 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001780
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001781 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001782 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001783 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001784 if (!apic_x2apic_mode(apic))
1785 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001786 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1787 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1788 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1789 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1790 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001791 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001792 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1793 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1794 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001795 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001796 apic->irr_pending = vcpu->arch.apicv_active;
1797 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001798 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001799 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001800 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001801 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001802 kvm_lapic_set_base(vcpu,
1803 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001804 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001805 apic_update_ppr(apic);
1806
Gleb Natapove1035712009-03-05 16:34:59 +02001807 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001808 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001809
Nadav Amit98eff522014-06-29 12:28:51 +03001810 apic_debug("%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001811 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001812 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001813 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001814}
1815
Eddie Dong97222cc2007-09-12 10:58:04 +03001816/*
1817 *----------------------------------------------------------------------
1818 * timer interface
1819 *----------------------------------------------------------------------
1820 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001821
Avi Kivity2a6eac92012-07-26 18:01:51 +03001822static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001823{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001824 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001825}
1826
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001827int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1828{
Gleb Natapov54e98182012-08-05 15:58:32 +03001829 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001830
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001831 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001832 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001833
1834 return 0;
1835}
1836
Avi Kivity89342082011-11-10 14:57:21 +02001837int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001838{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001839 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001840 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001841
Gleb Natapovc48f1492012-08-05 15:58:33 +03001842 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001843 vector = reg & APIC_VECTOR_MASK;
1844 mode = reg & APIC_MODE_MASK;
1845 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001846 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1847 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001848 }
1849 return 0;
1850}
1851
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001852void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001853{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001854 struct kvm_lapic *apic = vcpu->arch.apic;
1855
1856 if (apic)
1857 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001858}
1859
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001860static const struct kvm_io_device_ops apic_mmio_ops = {
1861 .read = apic_mmio_read,
1862 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001863};
1864
Avi Kivitye9d90d42012-07-26 18:01:50 +03001865static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1866{
1867 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001868 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001869
Radim Krčmář5d87db72014-10-10 19:15:08 +02001870 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001871
Avi Kivity2a6eac92012-07-26 18:01:51 +03001872 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001873 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1874 return HRTIMER_RESTART;
1875 } else
1876 return HRTIMER_NORESTART;
1877}
1878
Eddie Dong97222cc2007-09-12 10:58:04 +03001879int kvm_create_lapic(struct kvm_vcpu *vcpu)
1880{
1881 struct kvm_lapic *apic;
1882
1883 ASSERT(vcpu != NULL);
1884 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1885
1886 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1887 if (!apic)
1888 goto nomem;
1889
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001890 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001891
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001892 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1893 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001894 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1895 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001896 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001897 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001898 apic->vcpu = vcpu;
1899
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001900 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001901 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001902 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001903
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001904 /*
1905 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1906 * thinking that APIC satet has changed.
1907 */
1908 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001909 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001910 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001911 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001912
1913 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001914nomem_free_apic:
1915 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001916nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001917 return -ENOMEM;
1918}
Eddie Dong97222cc2007-09-12 10:58:04 +03001919
1920int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1921{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001922 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001923 int highest_irr;
1924
Paolo Bonzinif8543d62016-01-08 13:42:24 +01001925 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001926 return -1;
1927
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001928 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001929 highest_irr = apic_find_highest_irr(apic);
1930 if ((highest_irr == -1) ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001931 ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03001932 return -1;
1933 return highest_irr;
1934}
1935
Qing He40487c62007-09-17 14:47:13 +08001936int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1937{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001938 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001939 int r = 0;
1940
Gleb Natapovc48f1492012-08-05 15:58:33 +03001941 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04001942 r = 1;
1943 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1944 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1945 r = 1;
Qing He40487c62007-09-17 14:47:13 +08001946 return r;
1947}
1948
Eddie Dong1b9778d2007-09-03 16:56:58 +03001949void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1950{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001951 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001952
Gleb Natapov54e98182012-08-05 15:58:32 +03001953 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02001954 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03001955 if (apic_lvtt_tscdeadline(apic))
1956 apic->lapic_timer.tscdeadline = 0;
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02001957 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001958 }
1959}
1960
Eddie Dong97222cc2007-09-12 10:58:04 +03001961int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1962{
1963 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001964 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001965
1966 if (vector == -1)
1967 return -1;
1968
Wanpeng Li56cc2402014-08-05 12:42:24 +08001969 /*
1970 * We get here even with APIC virtualization enabled, if doing
1971 * nested virtualization and L1 runs with the "acknowledge interrupt
1972 * on exit" mode. Then we cannot inject the interrupt via RVI,
1973 * because the process would deliver it through the IDT.
1974 */
1975
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001976 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001977 apic_update_ppr(apic);
1978 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001979
1980 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
1981 apic_clear_isr(vector, apic);
1982 apic_update_ppr(apic);
1983 }
1984
Eddie Dong97222cc2007-09-12 10:58:04 +03001985 return vector;
1986}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001987
Radim Krčmářa92e2542016-07-12 22:09:22 +02001988static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
1989 struct kvm_lapic_state *s, bool set)
1990{
1991 if (apic_x2apic_mode(vcpu->arch.apic)) {
1992 u32 *id = (u32 *)(s->regs + APIC_ID);
1993
1994 if (set)
1995 *id >>= 24;
1996 else
1997 *id <<= 24;
1998 }
1999
2000 return 0;
2001}
2002
2003int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2004{
2005 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2006 return kvm_apic_state_fixup(vcpu, s, false);
2007}
2008
2009int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002010{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002012 int r;
2013
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002014
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002015 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002016 /* set SPIV separately to get count of SW disabled APICs right */
2017 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002018
2019 r = kvm_apic_state_fixup(vcpu, s, true);
2020 if (r)
2021 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002022 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002023
2024 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002025 kvm_apic_set_version(vcpu);
2026
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002027 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002028 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002029 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002030 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002031 update_divide_count(apic);
2032 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002033 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002034 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002035 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002036 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002037 if (vcpu->arch.apicv_active) {
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05002038 if (kvm_x86_ops->apicv_post_state_restore)
2039 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002040 kvm_x86_ops->hwapic_irr_update(vcpu,
2041 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002042 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002043 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002044 }
Avi Kivity3842d132010-07-27 12:30:24 +03002045 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002046 if (ioapic_in_kernel(vcpu->kvm))
2047 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002048
2049 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002050
2051 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002052}
Eddie Donga3d7f852007-09-03 16:15:12 +03002053
Avi Kivity2f52d582008-01-16 12:49:30 +02002054void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002055{
Eddie Donga3d7f852007-09-03 16:15:12 +03002056 struct hrtimer *timer;
2057
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002058 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002059 return;
2060
Gleb Natapov54e98182012-08-05 15:58:32 +03002061 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002062 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002063 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002064}
Avi Kivityb93463a2007-10-25 16:52:32 +02002065
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002066/*
2067 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2068 *
2069 * Detect whether guest triggered PV EOI since the
2070 * last entry. If yes, set EOI on guests's behalf.
2071 * Clear PV EOI in guest memory in any case.
2072 */
2073static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2074 struct kvm_lapic *apic)
2075{
2076 bool pending;
2077 int vector;
2078 /*
2079 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2080 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2081 *
2082 * KVM_APIC_PV_EOI_PENDING is unset:
2083 * -> host disabled PV EOI.
2084 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2085 * -> host enabled PV EOI, guest did not execute EOI yet.
2086 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2087 * -> host enabled PV EOI, guest executed EOI.
2088 */
2089 BUG_ON(!pv_eoi_enabled(vcpu));
2090 pending = pv_eoi_get_pending(vcpu);
2091 /*
2092 * Clear pending bit in any case: it will be set again on vmentry.
2093 * While this might not be ideal from performance point of view,
2094 * this makes sure pv eoi is only enabled when we know it's safe.
2095 */
2096 pv_eoi_clr_pending(vcpu);
2097 if (pending)
2098 return;
2099 vector = apic_set_eoi(apic);
2100 trace_kvm_pv_eoi(apic, vector);
2101}
2102
Avi Kivityb93463a2007-10-25 16:52:32 +02002103void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2104{
2105 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002106
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002107 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2108 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2109
Gleb Natapov41383772012-04-19 14:06:29 +03002110 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002111 return;
2112
Nicholas Krause603242a2015-08-05 10:44:40 -04002113 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2114 sizeof(u32)))
2115 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002116
2117 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2118}
2119
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002120/*
2121 * apic_sync_pv_eoi_to_guest - called before vmentry
2122 *
2123 * Detect whether it's safe to enable PV EOI and
2124 * if yes do so.
2125 */
2126static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2127 struct kvm_lapic *apic)
2128{
2129 if (!pv_eoi_enabled(vcpu) ||
2130 /* IRR set or many bits in ISR: could be nested. */
2131 apic->irr_pending ||
2132 /* Cache not set: could be safe but we don't bother. */
2133 apic->highest_isr_cache == -1 ||
2134 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002135 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002136 /*
2137 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2138 * so we need not do anything here.
2139 */
2140 return;
2141 }
2142
2143 pv_eoi_set_pending(apic->vcpu);
2144}
2145
Avi Kivityb93463a2007-10-25 16:52:32 +02002146void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2147{
2148 u32 data, tpr;
2149 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002150 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002151
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002152 apic_sync_pv_eoi_to_guest(vcpu, apic);
2153
Gleb Natapov41383772012-04-19 14:06:29 +03002154 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002155 return;
2156
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002157 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002158 max_irr = apic_find_highest_irr(apic);
2159 if (max_irr < 0)
2160 max_irr = 0;
2161 max_isr = apic_find_highest_isr(apic);
2162 if (max_isr < 0)
2163 max_isr = 0;
2164 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2165
Andy Honigfda4e2e82013-11-20 10:23:22 -08002166 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2167 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002168}
2169
Andy Honigfda4e2e82013-11-20 10:23:22 -08002170int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002171{
Andy Honigfda4e2e82013-11-20 10:23:22 -08002172 if (vapic_addr) {
2173 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2174 &vcpu->arch.apic->vapic_cache,
2175 vapic_addr, sizeof(u32)))
2176 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002177 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002178 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002179 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002180 }
2181
2182 vcpu->arch.apic->vapic_addr = vapic_addr;
2183 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002184}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002185
2186int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2187{
2188 struct kvm_lapic *apic = vcpu->arch.apic;
2189 u32 reg = (msr - APIC_BASE_MSR) << 4;
2190
Paolo Bonzini35754c92015-07-29 12:05:37 +02002191 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002192 return 1;
2193
Nadav Amitc69d3d92014-11-26 17:56:25 +02002194 if (reg == APIC_ICR2)
2195 return 1;
2196
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002197 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002198 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002199 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2200 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002201}
2202
2203int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2204{
2205 struct kvm_lapic *apic = vcpu->arch.apic;
2206 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2207
Paolo Bonzini35754c92015-07-29 12:05:37 +02002208 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002209 return 1;
2210
Nadav Amitc69d3d92014-11-26 17:56:25 +02002211 if (reg == APIC_DFR || reg == APIC_ICR2) {
2212 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2213 reg);
2214 return 1;
2215 }
2216
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002217 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002218 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002219 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002220 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002221
2222 *data = (((u64)high) << 32) | low;
2223
2224 return 0;
2225}
Gleb Natapov10388a02010-01-17 15:51:23 +02002226
2227int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2228{
2229 struct kvm_lapic *apic = vcpu->arch.apic;
2230
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002231 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002232 return 1;
2233
2234 /* if this is ICR write vector before command */
2235 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002236 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2237 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002238}
2239
2240int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2241{
2242 struct kvm_lapic *apic = vcpu->arch.apic;
2243 u32 low, high = 0;
2244
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002245 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002246 return 1;
2247
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002248 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002249 return 1;
2250 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002251 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002252
2253 *data = (((u64)high) << 32) | low;
2254
2255 return 0;
2256}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002257
2258int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2259{
2260 u64 addr = data & ~KVM_MSR_ENABLED;
2261 if (!IS_ALIGNED(addr, 4))
2262 return 1;
2263
2264 vcpu->arch.pv_eoi.msr_val = data;
2265 if (!pv_eoi_enabled(vcpu))
2266 return 0;
2267 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002268 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002269}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002270
Jan Kiszka66450a22013-03-13 12:42:34 +01002271void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2272{
2273 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002274 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002275 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002276
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002277 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002278 return;
2279
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002280 /*
2281 * INITs are latched while in SMM. Because an SMM CPU cannot
2282 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2283 * and delay processing of INIT until the next RSM.
2284 */
2285 if (is_smm(vcpu)) {
2286 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2287 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2288 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2289 return;
2290 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002291
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002292 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002293 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002294 kvm_lapic_reset(vcpu, true);
2295 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002296 if (kvm_vcpu_is_bsp(apic->vcpu))
2297 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2298 else
2299 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2300 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002301 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002302 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2303 /* evaluate pending_events before reading the vector */
2304 smp_rmb();
2305 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002306 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002307 vcpu->vcpu_id, sipi_vector);
2308 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2309 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2310 }
2311}
2312
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002313void kvm_lapic_init(void)
2314{
2315 /* do not patch jump label more than once per second */
2316 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002317 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002318}