blob: 8af6db9b64aa1f06c7711aebca33193a08f7a8f7 [file] [log] [blame]
Eddie Dong97222cc2007-09-12 10:58:04 +03001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Eddie Dong97222cc2007-09-12 10:58:04 +03009 *
10 * Authors:
11 * Dor Laor <dor.laor@qumranet.com>
12 * Gregory Haskins <ghaskins@novell.com>
13 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 *
15 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030022#include <linux/kvm.h>
23#include <linux/mm.h>
24#include <linux/highmem.h>
25#include <linux/smp.h>
26#include <linux/hrtimer.h>
27#include <linux/io.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040028#include <linux/export.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070029#include <linux/math64.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030031#include <asm/processor.h>
32#include <asm/msr.h>
33#include <asm/page.h>
34#include <asm/current.h>
35#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050036#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030038#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030040#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030042#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020043#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030044#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030045
Marcelo Tosattib682b812009-02-10 20:41:41 -020046#ifndef CONFIG_X86_64
47#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
48#else
49#define mod_64(x, y) ((x) % (y))
50#endif
51
Eddie Dong97222cc2007-09-12 10:58:04 +030052#define PRId64 "d"
53#define PRIx64 "llx"
54#define PRIu64 "u"
55#define PRIo64 "o"
56
57#define APIC_BUS_CYCLE_NS 1
58
59/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
60#define apic_debug(fmt, arg...)
61
Eddie Dong97222cc2007-09-12 10:58:04 +030062/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050063#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030064#define LAPIC_MMIO_LENGTH (1 << 12)
65/* followed define is not in apicdef.h */
66#define APIC_SHORT_MASK 0xc0000
67#define APIC_DEST_NOSHORT 0x0
68#define APIC_DEST_MASK 0x800
69#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090070#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030071
Nadav Amit394457a2014-10-03 00:30:52 +030072#define APIC_BROADCAST 0xFF
73#define X2APIC_BROADCAST 0xFFFFFFFFul
74
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030075static inline int apic_test_vector(int vec, void *bitmap)
76{
77 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
78}
79
Yang Zhang10606912013-04-11 19:21:38 +080080bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
81{
82 struct kvm_lapic *apic = vcpu->arch.apic;
83
84 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
85 apic_test_vector(vector, apic->regs + APIC_IRR);
86}
87
Eddie Dong97222cc2007-09-12 10:58:04 +030088static inline void apic_clear_vector(int vec, void *bitmap)
89{
90 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
91}
92
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030093static inline int __apic_test_and_set_vector(int vec, void *bitmap)
94{
95 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
96}
97
98static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
99{
100 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
101}
102
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300103struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300104struct static_key_deferred apic_sw_disabled __read_mostly;
105
Eddie Dong97222cc2007-09-12 10:58:04 +0300106static inline int apic_enabled(struct kvm_lapic *apic)
107{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300108 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300109}
110
Eddie Dong97222cc2007-09-12 10:58:04 +0300111#define LVT_MASK \
112 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
113
114#define LINT_MASK \
115 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
116 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
117
Radim Krčmář6e500432016-12-15 18:06:46 +0100118static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
119{
120 return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
121}
122
123static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
124{
125 return apic->vcpu->vcpu_id;
126}
127
Radim Krčmáře45115b2016-07-12 22:09:19 +0200128static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
129 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
130 switch (map->mode) {
131 case KVM_APIC_MODE_X2APIC: {
132 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200133 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100134
Radim Krčmáře45115b2016-07-12 22:09:19 +0200135 if (offset <= max_apic_id) {
136 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100137
Radim Krčmáře45115b2016-07-12 22:09:19 +0200138 *cluster = &map->phys_map[offset];
139 *mask = dest_id & (0xffff >> (16 - cluster_size));
140 } else {
141 *mask = 0;
142 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100143
Radim Krčmáře45115b2016-07-12 22:09:19 +0200144 return true;
145 }
146 case KVM_APIC_MODE_XAPIC_FLAT:
147 *cluster = map->xapic_flat_map;
148 *mask = dest_id & 0xff;
149 return true;
150 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100151 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200152 *mask = dest_id & 0xf;
153 return true;
154 default:
155 /* Not optimized. */
156 return false;
157 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300158}
159
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200160static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100161{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200162 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100163
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200164 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100165}
166
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300167static void recalculate_apic_map(struct kvm *kvm)
168{
169 struct kvm_apic_map *new, *old = NULL;
170 struct kvm_vcpu *vcpu;
171 int i;
Radim Krčmář6e500432016-12-15 18:06:46 +0100172 u32 max_id = 255; /* enough space for any xAPIC ID */
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300173
174 mutex_lock(&kvm->arch.apic_map_lock);
175
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200176 kvm_for_each_vcpu(i, vcpu, kvm)
177 if (kvm_apic_present(vcpu))
Radim Krčmář6e500432016-12-15 18:06:46 +0100178 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200179
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200180 new = kvm_kvzalloc(sizeof(struct kvm_apic_map) +
181 sizeof(struct kvm_lapic *) * ((u64)max_id + 1));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200182
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300183 if (!new)
184 goto out;
185
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200186 new->max_apic_id = max_id;
187
Nadav Amit173beed2014-11-02 11:54:54 +0200188 kvm_for_each_vcpu(i, vcpu, kvm) {
189 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200190 struct kvm_lapic **cluster;
191 u16 mask;
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100192 u32 ldr;
193 u8 xapic_id;
194 u32 x2apic_id;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300195
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100196 if (!kvm_apic_present(vcpu))
197 continue;
198
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100199 xapic_id = kvm_xapic_id(apic);
200 x2apic_id = kvm_x2apic_id(apic);
201
202 /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
203 if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
204 x2apic_id <= new->max_apic_id)
205 new->phys_map[x2apic_id] = apic;
206 /*
207 * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
208 * prevent them from masking VCPUs with APIC ID <= 0xff.
209 */
210 if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
211 new->phys_map[xapic_id] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100212
Radim Krčmář6e500432016-12-15 18:06:46 +0100213 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
214
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100215 if (apic_x2apic_mode(apic)) {
216 new->mode |= KVM_APIC_MODE_X2APIC;
217 } else if (ldr) {
218 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500219 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100220 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
221 else
222 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
223 }
224
Radim Krčmáře45115b2016-07-12 22:09:19 +0200225 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100226 continue;
227
Radim Krčmáře45115b2016-07-12 22:09:19 +0200228 if (mask)
229 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300230 }
231out:
232 old = rcu_dereference_protected(kvm->arch.apic_map,
233 lockdep_is_held(&kvm->arch.apic_map_lock));
234 rcu_assign_pointer(kvm->arch.apic_map, new);
235 mutex_unlock(&kvm->arch.apic_map_lock);
236
237 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200238 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800239
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700240 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300241}
242
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300243static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
244{
Radim Krčmáře4627552014-10-30 15:06:45 +0100245 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300246
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500247 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100248
249 if (enabled != apic->sw_enabled) {
250 apic->sw_enabled = enabled;
251 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300252 static_key_slow_dec_deferred(&apic_sw_disabled);
253 recalculate_apic_map(apic->vcpu->kvm);
254 } else
255 static_key_slow_inc(&apic_sw_disabled.key);
256 }
257}
258
Radim Krčmářa92e2542016-07-12 22:09:22 +0200259static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300260{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500261 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300262 recalculate_apic_map(apic->vcpu->kvm);
263}
264
265static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
266{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500267 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300268 recalculate_apic_map(apic->vcpu->kvm);
269}
270
Radim Krčmářa92e2542016-07-12 22:09:22 +0200271static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200272{
273 u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
274
Radim Krčmář6e500432016-12-15 18:06:46 +0100275 WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
276
Radim Krčmářa92e2542016-07-12 22:09:22 +0200277 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500278 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200279 recalculate_apic_map(apic->vcpu->kvm);
280}
281
Eddie Dong97222cc2007-09-12 10:58:04 +0300282static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
283{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500284 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300285}
286
287static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
288{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500289 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300290}
291
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800292static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
293{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100294 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800295}
296
Eddie Dong97222cc2007-09-12 10:58:04 +0300297static inline int apic_lvtt_period(struct kvm_lapic *apic)
298{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100299 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800300}
301
302static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
303{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100304 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300305}
306
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200307static inline int apic_lvt_nmi_mode(u32 lvt_val)
308{
309 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
310}
311
Gleb Natapovfc61b802009-07-05 17:39:35 +0300312void kvm_apic_set_version(struct kvm_vcpu *vcpu)
313{
314 struct kvm_lapic *apic = vcpu->arch.apic;
315 struct kvm_cpuid_entry2 *feat;
316 u32 v = APIC_VERSION;
317
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100318 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300319 return;
320
321 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
322 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
323 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500324 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300325}
326
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500327static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800328 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300329 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
330 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
331 LINT_MASK, LINT_MASK, /* LVT0-1 */
332 LVT_MASK /* LVTERR */
333};
334
335static int find_highest_vector(void *bitmap)
336{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900337 int vec;
338 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300339
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900340 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
341 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
342 reg = bitmap + REG_POS(vec);
343 if (*reg)
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100344 return __fls(*reg) + vec;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900345 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300346
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900347 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300348}
349
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300350static u8 count_vectors(void *bitmap)
351{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900352 int vec;
353 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300354 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900355
356 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
357 reg = bitmap + REG_POS(vec);
358 count += hweight32(*reg);
359 }
360
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300361 return count;
362}
363
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100364int __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800365{
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100366 u32 i, vec;
367 u32 pir_val, irr_val;
368 int max_irr = -1;
Yang Zhanga20ed542013-04-11 19:25:15 +0800369
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100370 for (i = vec = 0; i <= 7; i++, vec += 32) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200371 pir_val = READ_ONCE(pir[i]);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100372 irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
Paolo Bonziniad361092016-09-20 16:15:05 +0200373 if (pir_val) {
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100374 irr_val |= xchg(&pir[i], 0);
375 *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
Paolo Bonziniad361092016-09-20 16:15:05 +0200376 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100377 if (irr_val)
378 max_irr = __fls(irr_val) + vec;
Yang Zhanga20ed542013-04-11 19:25:15 +0800379 }
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100380
381 return max_irr;
Yang Zhanga20ed542013-04-11 19:25:15 +0800382}
Wincy Van705699a2015-02-03 23:58:17 +0800383EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
384
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100385int kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
Wincy Van705699a2015-02-03 23:58:17 +0800386{
387 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100388 int max_irr;
Wincy Van705699a2015-02-03 23:58:17 +0800389
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100390 max_irr = __kvm_apic_update_irr(pir, apic->regs);
Radim Krčmářc77f3fa2015-10-08 20:23:33 +0200391
392 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini810e6de2016-12-19 13:05:46 +0100393 return max_irr;
Wincy Van705699a2015-02-03 23:58:17 +0800394}
Yang Zhanga20ed542013-04-11 19:25:15 +0800395EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
396
Gleb Natapov33e4c682009-06-11 11:06:51 +0300397static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300398{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300399 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300400}
401
402static inline int apic_find_highest_irr(struct kvm_lapic *apic)
403{
404 int result;
405
Yang Zhangc7c9c562013-01-25 10:18:51 +0800406 /*
407 * Note that irr_pending is just a hint. It will be always
408 * true with virtual interrupt delivery enabled.
409 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300410 if (!apic->irr_pending)
411 return -1;
412
413 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300414 ASSERT(result == -1 || result >= 16);
415
416 return result;
417}
418
Gleb Natapov33e4c682009-06-11 11:06:51 +0300419static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
420{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800421 struct kvm_vcpu *vcpu;
422
423 vcpu = apic->vcpu;
424
Andrey Smetanind62caab2015-11-10 15:36:33 +0300425 if (unlikely(vcpu->arch.apicv_active)) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800426 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200427 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800428 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200429 } else {
430 apic->irr_pending = false;
431 apic_clear_vector(vec, apic->regs + APIC_IRR);
432 if (apic_search_irr(apic) != -1)
433 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800434 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300435}
436
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300437static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
438{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800439 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200440
Wanpeng Li56cc2402014-08-05 12:42:24 +0800441 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
442 return;
443
444 vcpu = apic->vcpu;
445
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300446 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800447 * With APIC virtualization enabled, all caching is disabled
448 * because the processor can modify ISR under the hood. Instead
449 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300450 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300451 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200452 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800453 else {
454 ++apic->isr_count;
455 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
456 /*
457 * ISR (in service register) bit is set when injecting an interrupt.
458 * The highest vector is injected. Thus the latest bit set matches
459 * the highest bit in ISR.
460 */
461 apic->highest_isr_cache = vec;
462 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300463}
464
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200465static inline int apic_find_highest_isr(struct kvm_lapic *apic)
466{
467 int result;
468
469 /*
470 * Note that isr_count is always 1, and highest_isr_cache
471 * is always -1, with APIC virtualization enabled.
472 */
473 if (!apic->isr_count)
474 return -1;
475 if (likely(apic->highest_isr_cache != -1))
476 return apic->highest_isr_cache;
477
478 result = find_highest_vector(apic->regs + APIC_ISR);
479 ASSERT(result == -1 || result >= 16);
480
481 return result;
482}
483
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300484static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
485{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200486 struct kvm_vcpu *vcpu;
487 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
488 return;
489
490 vcpu = apic->vcpu;
491
492 /*
493 * We do get here for APIC virtualization enabled if the guest
494 * uses the Hyper-V APIC enlightenment. In this case we may need
495 * to trigger a new interrupt delivery by writing the SVI field;
496 * on the other hand isr_count and highest_isr_cache are unused
497 * and must be left alone.
498 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300499 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200500 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200501 apic_find_highest_isr(apic));
502 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300503 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200504 BUG_ON(apic->isr_count < 0);
505 apic->highest_isr_cache = -1;
506 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300507}
508
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800509int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
510{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300511 /* This may race with setting of irr in __apic_accept_irq() and
512 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
513 * will cause vmexit immediately and the value will be recalculated
514 * on the next vmentry.
515 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100516 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800517}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800518
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200519static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800520 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100521 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200522
Yang Zhangb4f22252013-04-11 19:21:37 +0800523int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100524 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300525{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800526 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800527
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200528 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800529 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300530}
531
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300532static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
533{
534
535 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
536 sizeof(val));
537}
538
539static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
540{
541
542 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
543 sizeof(*val));
544}
545
546static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
547{
548 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
549}
550
551static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
552{
553 u8 val;
554 if (pv_eoi_get_user(vcpu, &val) < 0)
555 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800556 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300557 return val & 0x1;
558}
559
560static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
561{
562 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
563 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800564 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300565 return;
566 }
567 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
568}
569
570static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
571{
572 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
573 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800574 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300575 return;
576 }
577 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
578}
579
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100580static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
581{
Paolo Bonzini3d927892016-12-19 13:29:03 +0100582 int highest_irr;
583 if (apic->vcpu->arch.apicv_active)
584 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
585 highest_irr = apic_find_highest_irr(apic);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100586 if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
587 return -1;
588 return highest_irr;
589}
590
591static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Eddie Dong97222cc2007-09-12 10:58:04 +0300592{
Avi Kivity3842d132010-07-27 12:30:24 +0300593 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 int isr;
595
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500596 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
597 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300598 isr = apic_find_highest_isr(apic);
599 isrv = (isr != -1) ? isr : 0;
600
601 if ((tpr & 0xf0) >= (isrv & 0xf0))
602 ppr = tpr & 0xff;
603 else
604 ppr = isrv & 0xf0;
605
606 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
607 apic, ppr, isr, isrv);
608
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100609 *new_ppr = ppr;
610 if (old_ppr != ppr)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500611 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100612
613 return ppr < old_ppr;
614}
615
616static void apic_update_ppr(struct kvm_lapic *apic)
617{
618 u32 ppr;
619
Paolo Bonzini26fbbee2016-12-18 13:54:58 +0100620 if (__apic_update_ppr(apic, &ppr) &&
621 apic_has_interrupt_for_ppr(apic, ppr) != -1)
Paolo Bonzinib3c045d2016-12-18 21:47:54 +0100622 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300623}
624
Paolo Bonzinieb90f342016-12-18 14:02:21 +0100625void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
626{
627 apic_update_ppr(vcpu->arch.apic);
628}
629EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
630
Eddie Dong97222cc2007-09-12 10:58:04 +0300631static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
632{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500633 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300634 apic_update_ppr(apic);
635}
636
Radim Krčmář03d22492015-02-12 19:41:31 +0100637static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300638{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100639 return mda == (apic_x2apic_mode(apic) ?
640 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300641}
642
Radim Krčmář03d22492015-02-12 19:41:31 +0100643static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300644{
Radim Krčmář03d22492015-02-12 19:41:31 +0100645 if (kvm_apic_broadcast(apic, mda))
646 return true;
647
648 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100649 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100650
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100651 /*
652 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
653 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
654 * this allows unique addressing of VCPUs with APIC ID over 0xff.
655 * The 0xff condition is needed because writeable xAPIC ID.
656 */
657 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
658 return true;
659
Radim Krčmářb4535b52016-12-15 18:06:47 +0100660 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300661}
662
Radim Krčmář52c233a2015-01-29 22:48:48 +0100663static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300664{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300665 u32 logical_id;
666
Nadav Amit394457a2014-10-03 00:30:52 +0300667 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100668 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300669
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500670 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300671
Radim Krčmář9368b562015-01-29 22:48:49 +0100672 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100673 return ((logical_id >> 16) == (mda >> 16))
674 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100675
676 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300677
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500678 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300679 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100680 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300681 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100682 return ((logical_id >> 4) == (mda >> 4))
683 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300684 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200685 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500686 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100687 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300688 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300689}
690
Radim Krčmářc5192652016-07-12 22:09:28 +0200691/* The KVM local APIC implementation has two quirks:
692 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100693 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
694 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
695 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200696 *
697 * - in-kernel IOAPIC messages have to be delivered directly to
698 * x2APIC, because the kernel does not support interrupt remapping.
699 * In order to support broadcast without interrupt remapping, x2APIC
700 * rewrites the destination of non-IPI messages from APIC_BROADCAST
701 * to X2APIC_BROADCAST.
702 *
703 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
704 * important when userspace wants to use x2APIC-format MSIs, because
705 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100706 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200707static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
708 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100709{
710 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100711
Radim Krčmářc5192652016-07-12 22:09:28 +0200712 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100713 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100714 return X2APIC_BROADCAST;
715
Radim Krčmářb4535b52016-12-15 18:06:47 +0100716 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100717}
718
Radim Krčmář52c233a2015-01-29 22:48:48 +0100719bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300720 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300721{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800722 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200723 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300724
725 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200726 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300727 target, source, dest, dest_mode, short_hand);
728
Zachary Amsdenbd371392010-06-14 11:42:15 -1000729 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300730 switch (short_hand) {
731 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100732 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100733 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200734 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100735 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300736 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100737 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300738 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100739 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300740 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100741 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300742 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200743 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
744 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100745 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300746 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300747}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500748EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300749
Feng Wu520040142016-01-25 16:53:33 +0800750int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
751 const unsigned long *bitmap, u32 bitmap_size)
752{
753 u32 mod;
754 int i, idx = -1;
755
756 mod = vector % dest_vcpus;
757
758 for (i = 0; i <= mod; i++) {
759 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
760 BUG_ON(idx == bitmap_size);
761 }
762
763 return idx;
764}
765
Radim Krčmář4efd8052016-02-12 15:00:15 +0100766static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
767{
768 if (!kvm->arch.disabled_lapic_found) {
769 kvm->arch.disabled_lapic_found = true;
770 printk(KERN_INFO
771 "Disabled LAPIC found during irq injection\n");
772 }
773}
774
Radim Krčmářc5192652016-07-12 22:09:28 +0200775static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
776 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
777{
778 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
779 if ((irq->dest_id == APIC_BROADCAST &&
780 map->mode != KVM_APIC_MODE_X2APIC))
781 return true;
782 if (irq->dest_id == X2APIC_BROADCAST)
783 return true;
784 } else {
785 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
786 if (irq->dest_id == (x2apic_ipi ?
787 X2APIC_BROADCAST : APIC_BROADCAST))
788 return true;
789 }
790
791 return false;
792}
793
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200794/* Return true if the interrupt can be handled by using *bitmap as index mask
795 * for valid destinations in *dst array.
796 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
797 * Note: we may have zero kvm_lapic destinations when we return true, which
798 * means that the interrupt should be dropped. In this case, *bitmap would be
799 * zero and *dst undefined.
800 */
801static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
802 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
803 struct kvm_apic_map *map, struct kvm_lapic ***dst,
804 unsigned long *bitmap)
805{
806 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200807
808 if (irq->shorthand == APIC_DEST_SELF && src) {
809 *dst = src;
810 *bitmap = 1;
811 return true;
812 } else if (irq->shorthand)
813 return false;
814
Radim Krčmářc5192652016-07-12 22:09:28 +0200815 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200816 return false;
817
818 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200819 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200820 *bitmap = 0;
821 } else {
822 *dst = &map->phys_map[irq->dest_id];
823 *bitmap = 1;
824 }
825 return true;
826 }
827
Radim Krčmáře45115b2016-07-12 22:09:19 +0200828 *bitmap = 0;
829 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
830 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200831 return false;
832
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200833 if (!kvm_lowest_prio_delivery(irq))
834 return true;
835
836 if (!kvm_vector_hashing_enabled()) {
837 lowest = -1;
838 for_each_set_bit(i, bitmap, 16) {
839 if (!(*dst)[i])
840 continue;
841 if (lowest < 0)
842 lowest = i;
843 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
844 (*dst)[lowest]->vcpu) < 0)
845 lowest = i;
846 }
847 } else {
848 if (!*bitmap)
849 return true;
850
851 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
852 bitmap, 16);
853
854 if (!(*dst)[lowest]) {
855 kvm_apic_disabled_lapic_found(kvm);
856 *bitmap = 0;
857 return true;
858 }
859 }
860
861 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
862
863 return true;
864}
865
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300866bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100867 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300868{
869 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200870 unsigned long bitmap;
871 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300872 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200873 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300874
875 *r = -1;
876
877 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800878 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300879 return true;
880 }
881
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300882 rcu_read_lock();
883 map = rcu_dereference(kvm->arch.apic_map);
884
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200885 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
886 if (ret)
887 for_each_set_bit(i, &bitmap, 16) {
888 if (!dst[i])
889 continue;
890 if (*r < 0)
891 *r = 0;
892 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100893 }
894
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300895 rcu_read_unlock();
896 return ret;
897}
898
Feng Wu6228a0d2016-01-25 16:53:34 +0800899/*
900 * This routine tries to handler interrupts in posted mode, here is how
901 * it deals with different cases:
902 * - For single-destination interrupts, handle it in posted mode
903 * - Else if vector hashing is enabled and it is a lowest-priority
904 * interrupt, handle it in posted mode and use the following mechanism
905 * to find the destinaiton vCPU.
906 * 1. For lowest-priority interrupts, store all the possible
907 * destination vCPUs in an array.
908 * 2. Use "guest vector % max number of destination vCPUs" to find
909 * the right destination vCPU in the array for the lowest-priority
910 * interrupt.
911 * - Otherwise, use remapped mode to inject the interrupt.
912 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800913bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
914 struct kvm_vcpu **dest_vcpu)
915{
916 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200917 unsigned long bitmap;
918 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800919 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800920
921 if (irq->shorthand)
922 return false;
923
924 rcu_read_lock();
925 map = rcu_dereference(kvm->arch.apic_map);
926
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200927 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
928 hweight16(bitmap) == 1) {
929 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800930
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200931 if (dst[i]) {
932 *dest_vcpu = dst[i]->vcpu;
933 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +0800934 }
Feng Wu8feb4a02015-09-18 22:29:47 +0800935 }
936
Feng Wu8feb4a02015-09-18 22:29:47 +0800937 rcu_read_unlock();
938 return ret;
939}
940
Eddie Dong97222cc2007-09-12 10:58:04 +0300941/*
942 * Add a pending IRQ into lapic.
943 * Return 1 if successfully added and 0 if discarded.
944 */
945static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800946 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100947 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300948{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200949 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300950 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300951
Paolo Bonzinia183b632014-09-11 11:51:02 +0200952 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
953 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300954 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300955 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200956 vcpu->arch.apic_arb_prio++;
957 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200958 if (unlikely(trig_mode && !level))
959 break;
960
Eddie Dong97222cc2007-09-12 10:58:04 +0300961 /* FIXME add logic for vcpu on reset */
962 if (unlikely(!apic_enabled(apic)))
963 break;
964
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200965 result = 1;
966
Joerg Roedel9daa5002016-02-29 16:04:44 +0100967 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100968 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +0100969 dest_map->vectors[vcpu->vcpu_id] = vector;
970 }
Avi Kivitya5d36f82009-12-29 12:42:16 +0200971
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200972 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
973 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500974 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200975 else
976 apic_clear_vector(vector, apic->regs + APIC_TMR);
977 }
978
Andrey Smetanind62caab2015-11-10 15:36:33 +0300979 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +0800980 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200981 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500982 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800983
984 kvm_make_request(KVM_REQ_EVENT, vcpu);
985 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300986 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300987 break;
988
989 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530990 result = 1;
991 vcpu->arch.pv.pv_unhalted = 1;
992 kvm_make_request(KVM_REQ_EVENT, vcpu);
993 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300994 break;
995
996 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200997 result = 1;
998 kvm_make_request(KVM_REQ_SMI, vcpu);
999 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001000 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001001
Eddie Dong97222cc2007-09-12 10:58:04 +03001002 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001003 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +08001004 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +02001005 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001006 break;
1007
1008 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +01001009 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +02001010 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +01001011 /* assumes that there are only KVM_APIC_INIT/SIPI */
1012 apic->pending_events = (1UL << KVM_APIC_INIT);
1013 /* make sure pending_events is visible before sending
1014 * the request */
1015 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +03001016 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +03001017 kvm_vcpu_kick(vcpu);
1018 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001019 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
1020 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +03001021 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001022 break;
1023
1024 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +02001025 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
1026 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +01001027 result = 1;
1028 apic->sipi_vector = vector;
1029 /* make sure sipi_vector is visible for the receiver */
1030 smp_wmb();
1031 set_bit(KVM_APIC_SIPI, &apic->pending_events);
1032 kvm_make_request(KVM_REQ_EVENT, vcpu);
1033 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001034 break;
1035
Jan Kiszka23930f92008-09-26 09:30:52 +02001036 case APIC_DM_EXTINT:
1037 /*
1038 * Should only be called by kvm_apic_local_deliver() with LVT0,
1039 * before NMI watchdog was enabled. Already handled by
1040 * kvm_apic_accept_pic_intr().
1041 */
1042 break;
1043
Eddie Dong97222cc2007-09-12 10:58:04 +03001044 default:
1045 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1046 delivery_mode);
1047 break;
1048 }
1049 return result;
1050}
1051
Gleb Natapove1035712009-03-05 16:34:59 +02001052int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001053{
Gleb Natapove1035712009-03-05 16:34:59 +02001054 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001055}
1056
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001057static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1058{
Andrey Smetanin63086302015-11-10 15:36:32 +03001059 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001060}
1061
Yang Zhangc7c9c562013-01-25 10:18:51 +08001062static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1063{
Steve Rutherford7543a632015-07-29 23:21:41 -07001064 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001065
Steve Rutherford7543a632015-07-29 23:21:41 -07001066 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1067 if (!kvm_ioapic_handles_vector(apic, vector))
1068 return;
1069
1070 /* Request a KVM exit to inform the userspace IOAPIC. */
1071 if (irqchip_split(apic->vcpu->kvm)) {
1072 apic->vcpu->arch.pending_ioapic_eoi = vector;
1073 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1074 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001075 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001076
1077 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1078 trigger_mode = IOAPIC_LEVEL_TRIG;
1079 else
1080 trigger_mode = IOAPIC_EDGE_TRIG;
1081
1082 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001083}
1084
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001085static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001086{
1087 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001088
1089 trace_kvm_eoi(apic, vector);
1090
Eddie Dong97222cc2007-09-12 10:58:04 +03001091 /*
1092 * Not every write EOI will has corresponding ISR,
1093 * one example is when Kernel check timer on setup_IO_APIC
1094 */
1095 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001096 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001097
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001098 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001099 apic_update_ppr(apic);
1100
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001101 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1102 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1103
Yang Zhangc7c9c562013-01-25 10:18:51 +08001104 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001105 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001106 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001107}
1108
Yang Zhangc7c9c562013-01-25 10:18:51 +08001109/*
1110 * this interface assumes a trap-like exit, which has already finished
1111 * desired side effect including vISR and vPPR update.
1112 */
1113void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1114{
1115 struct kvm_lapic *apic = vcpu->arch.apic;
1116
1117 trace_kvm_eoi(apic, vector);
1118
1119 kvm_ioapic_send_eoi(apic, vector);
1120 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1121}
1122EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1123
Eddie Dong97222cc2007-09-12 10:58:04 +03001124static void apic_send_ipi(struct kvm_lapic *apic)
1125{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001126 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1127 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001128 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001129
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001130 irq.vector = icr_low & APIC_VECTOR_MASK;
1131 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1132 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001133 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001134 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1135 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001136 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001137 if (apic_x2apic_mode(apic))
1138 irq.dest_id = icr_high;
1139 else
1140 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001141
Gleb Natapov1000ff82009-07-07 16:00:57 +03001142 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1143
Eddie Dong97222cc2007-09-12 10:58:04 +03001144 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1145 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001146 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1147 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001148 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001149 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001150 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001151
Yang Zhangb4f22252013-04-11 19:21:37 +08001152 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001153}
1154
1155static u32 apic_get_tmcct(struct kvm_lapic *apic)
1156{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001157 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001158 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001159 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001160
1161 ASSERT(apic != NULL);
1162
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001163 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001164 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001165 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001166 return 0;
1167
Paolo Bonzini55878592016-10-25 15:23:49 +02001168 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001169 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001170 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001171 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001172
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001173 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1174 tmcct = div64_u64(ns,
1175 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001176
1177 return tmcct;
1178}
1179
Avi Kivityb209749f2007-10-22 16:50:39 +02001180static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1181{
1182 struct kvm_vcpu *vcpu = apic->vcpu;
1183 struct kvm_run *run = vcpu->run;
1184
Avi Kivitya8eeb042010-05-10 12:34:53 +03001185 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001186 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001187 run->tpr_access.is_write = write;
1188}
1189
1190static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1191{
1192 if (apic->vcpu->arch.tpr_access_reporting)
1193 __report_tpr_access(apic, write);
1194}
1195
Eddie Dong97222cc2007-09-12 10:58:04 +03001196static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1197{
1198 u32 val = 0;
1199
1200 if (offset >= LAPIC_MMIO_LENGTH)
1201 return 0;
1202
1203 switch (offset) {
1204 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001205 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001206 break;
1207
1208 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001209 if (apic_lvtt_tscdeadline(apic))
1210 return 0;
1211
Eddie Dong97222cc2007-09-12 10:58:04 +03001212 val = apic_get_tmcct(apic);
1213 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001214 case APIC_PROCPRI:
1215 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001216 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001217 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001218 case APIC_TASKPRI:
1219 report_tpr_access(apic, false);
1220 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001221 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001222 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001223 break;
1224 }
1225
1226 return val;
1227}
1228
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001229static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1230{
1231 return container_of(dev, struct kvm_lapic, dev);
1232}
1233
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001234int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001235 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001236{
Eddie Dong97222cc2007-09-12 10:58:04 +03001237 unsigned char alignment = offset & 0xf;
1238 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001239 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001240 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001241
1242 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001243 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1244 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001245 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001246 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001247
1248 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001249 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1250 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001251 return 1;
1252 }
1253
Eddie Dong97222cc2007-09-12 10:58:04 +03001254 result = __apic_read(apic, offset & ~0xf);
1255
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001256 trace_kvm_apic_read(offset, result);
1257
Eddie Dong97222cc2007-09-12 10:58:04 +03001258 switch (len) {
1259 case 1:
1260 case 2:
1261 case 4:
1262 memcpy(data, (char *)&result + alignment, len);
1263 break;
1264 default:
1265 printk(KERN_ERR "Local APIC read with len = %x, "
1266 "should be 1,2, or 4 instead\n", len);
1267 break;
1268 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001269 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001270}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001271EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001272
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001273static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1274{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001275 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001276 addr >= apic->base_address &&
1277 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1278}
1279
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001280static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001281 gpa_t address, int len, void *data)
1282{
1283 struct kvm_lapic *apic = to_lapic(this);
1284 u32 offset = address - apic->base_address;
1285
1286 if (!apic_mmio_in_range(apic, address))
1287 return -EOPNOTSUPP;
1288
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001289 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001290
1291 return 0;
1292}
1293
Eddie Dong97222cc2007-09-12 10:58:04 +03001294static void update_divide_count(struct kvm_lapic *apic)
1295{
1296 u32 tmp1, tmp2, tdcr;
1297
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001298 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001299 tmp1 = tdcr & 0xf;
1300 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001301 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001302
1303 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001304 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001305}
1306
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001307static void apic_update_lvtt(struct kvm_lapic *apic)
1308{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001309 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001310 apic->lapic_timer.timer_mode_mask;
1311
1312 if (apic->lapic_timer.timer_mode != timer_mode) {
1313 apic->lapic_timer.timer_mode = timer_mode;
1314 hrtimer_cancel(&apic->lapic_timer.timer);
1315 }
1316}
1317
Radim Krčmář5d87db72014-10-10 19:15:08 +02001318static void apic_timer_expired(struct kvm_lapic *apic)
1319{
1320 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001321 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001322 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001323
Radim Krčmář5d87db72014-10-10 19:15:08 +02001324 if (atomic_read(&apic->lapic_timer.pending))
1325 return;
1326
1327 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001328 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001329
Marcelo Tosatti85773702016-02-19 09:46:39 +01001330 if (swait_active(q))
1331 swake_up(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001332
1333 if (apic_lvtt_tscdeadline(apic))
1334 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1335}
1336
1337/*
1338 * On APICv, this test will cause a busy wait
1339 * during a higher-priority task.
1340 */
1341
1342static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1343{
1344 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001345 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001346
1347 if (kvm_apic_hw_enabled(apic)) {
1348 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001349 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001350
Andrey Smetanind62caab2015-11-10 15:36:33 +03001351 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001352 bitmap = apic->regs + APIC_IRR;
1353
1354 if (apic_test_vector(vec, bitmap))
1355 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001356 }
1357 return false;
1358}
1359
1360void wait_lapic_expire(struct kvm_vcpu *vcpu)
1361{
1362 struct kvm_lapic *apic = vcpu->arch.apic;
1363 u64 guest_tsc, tsc_deadline;
1364
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001365 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001366 return;
1367
1368 if (apic->lapic_timer.expired_tscdeadline == 0)
1369 return;
1370
1371 if (!lapic_timer_int_injected(vcpu))
1372 return;
1373
1374 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1375 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001376 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001377 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001378
1379 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1380 if (guest_tsc < tsc_deadline)
Marcelo Tosattib606f182016-06-20 22:33:48 -03001381 __delay(min(tsc_deadline - guest_tsc,
1382 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
Radim Krčmář5d87db72014-10-10 19:15:08 +02001383}
1384
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001385static void start_sw_tscdeadline(struct kvm_lapic *apic)
1386{
1387 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1388 u64 ns = 0;
1389 ktime_t expire;
1390 struct kvm_vcpu *vcpu = apic->vcpu;
1391 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1392 unsigned long flags;
1393 ktime_t now;
1394
1395 if (unlikely(!tscdeadline || !this_tsc_khz))
1396 return;
1397
1398 local_irq_save(flags);
1399
Paolo Bonzini55878592016-10-25 15:23:49 +02001400 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001401 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1402 if (likely(tscdeadline > guest_tsc)) {
1403 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1404 do_div(ns, this_tsc_khz);
1405 expire = ktime_add_ns(now, ns);
1406 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1407 hrtimer_start(&apic->lapic_timer.timer,
1408 expire, HRTIMER_MODE_ABS_PINNED);
1409 } else
1410 apic_timer_expired(apic);
1411
1412 local_irq_restore(flags);
1413}
1414
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001415static void start_sw_period(struct kvm_lapic *apic)
1416{
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001417 if (!apic->lapic_timer.period)
1418 return;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001419
1420 if (apic_lvtt_oneshot(apic) &&
Paolo Bonzini55878592016-10-25 15:23:49 +02001421 ktime_after(ktime_get(),
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001422 apic->lapic_timer.target_expiration)) {
1423 apic_timer_expired(apic);
1424 return;
1425 }
1426
1427 hrtimer_start(&apic->lapic_timer.timer,
1428 apic->lapic_timer.target_expiration,
1429 HRTIMER_MODE_ABS_PINNED);
1430}
1431
1432static bool set_target_expiration(struct kvm_lapic *apic)
1433{
1434 ktime_t now;
1435 u64 tscl = rdtsc();
1436
Paolo Bonzini55878592016-10-25 15:23:49 +02001437 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001438 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1439 * APIC_BUS_CYCLE_NS * apic->divide_count;
1440
1441 if (!apic->lapic_timer.period)
1442 return false;
1443
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001444 /*
1445 * Do not allow the guest to program periodic timers with small
1446 * interval, since the hrtimers are not throttled by the host
1447 * scheduler.
1448 */
1449 if (apic_lvtt_period(apic)) {
1450 s64 min_period = min_timer_period_us * 1000LL;
1451
1452 if (apic->lapic_timer.period < min_period) {
1453 pr_info_ratelimited(
1454 "kvm: vcpu %i: requested %lld ns "
1455 "lapic timer period limited to %lld ns\n",
1456 apic->vcpu->vcpu_id,
1457 apic->lapic_timer.period, min_period);
1458 apic->lapic_timer.period = min_period;
1459 }
1460 }
1461
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001462 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1463 PRIx64 ", "
1464 "timer initial count 0x%x, period %lldns, "
1465 "expire @ 0x%016" PRIx64 ".\n", __func__,
1466 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1467 kvm_lapic_get_reg(apic, APIC_TMICT),
1468 apic->lapic_timer.period,
1469 ktime_to_ns(ktime_add_ns(now,
1470 apic->lapic_timer.period)));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001471
1472 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1473 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1474 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1475
1476 return true;
1477}
1478
1479static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1480{
1481 apic->lapic_timer.tscdeadline +=
1482 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1483 apic->lapic_timer.target_expiration =
1484 ktime_add_ns(apic->lapic_timer.target_expiration,
1485 apic->lapic_timer.period);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001486}
1487
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001488bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1489{
Wanpeng Li91005302016-08-03 12:04:12 +08001490 if (!lapic_in_kernel(vcpu))
1491 return false;
1492
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001493 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1494}
1495EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1496
Wanpeng Li7e810a32016-10-24 18:23:12 +08001497static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001498{
1499 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1500 apic->lapic_timer.hv_timer_in_use = false;
1501}
1502
Wanpeng Li7e810a32016-10-24 18:23:12 +08001503static bool start_hv_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001504{
1505 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1506
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001507 if ((atomic_read(&apic->lapic_timer.pending) &&
1508 !apic_lvtt_period(apic)) ||
Wanpeng Li196f20c2016-06-28 14:54:19 +08001509 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1510 if (apic->lapic_timer.hv_timer_in_use)
Wanpeng Li7e810a32016-10-24 18:23:12 +08001511 cancel_hv_timer(apic);
Wanpeng Li196f20c2016-06-28 14:54:19 +08001512 } else {
1513 apic->lapic_timer.hv_timer_in_use = true;
1514 hrtimer_cancel(&apic->lapic_timer.timer);
1515
1516 /* In case the sw timer triggered in the window */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001517 if (atomic_read(&apic->lapic_timer.pending) &&
1518 !apic_lvtt_period(apic))
Wanpeng Li7e810a32016-10-24 18:23:12 +08001519 cancel_hv_timer(apic);
Wanpeng Li196f20c2016-06-28 14:54:19 +08001520 }
1521 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1522 apic->lapic_timer.hv_timer_in_use);
1523 return apic->lapic_timer.hv_timer_in_use;
1524}
1525
Eddie Dong97222cc2007-09-12 10:58:04 +03001526void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1527{
1528 struct kvm_lapic *apic = vcpu->arch.apic;
1529
1530 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1531 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001532 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001533 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001534
1535 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1536 advance_periodic_target_expiration(apic);
1537 if (!start_hv_timer(apic))
1538 start_sw_period(apic);
1539 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001540}
1541EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1542
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001543void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1544{
1545 struct kvm_lapic *apic = vcpu->arch.apic;
1546
1547 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1548
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001549 start_hv_timer(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001550}
1551EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1552
1553void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1554{
1555 struct kvm_lapic *apic = vcpu->arch.apic;
1556
1557 /* Possibly the TSC deadline timer is not enabled yet */
1558 if (!apic->lapic_timer.hv_timer_in_use)
1559 return;
1560
Wanpeng Li7e810a32016-10-24 18:23:12 +08001561 cancel_hv_timer(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001562
1563 if (atomic_read(&apic->lapic_timer.pending))
1564 return;
1565
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001566 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1567 start_sw_period(apic);
1568 else if (apic_lvtt_tscdeadline(apic))
1569 start_sw_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001570}
1571EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1572
Eddie Dong97222cc2007-09-12 10:58:04 +03001573static void start_apic_timer(struct kvm_lapic *apic)
1574{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001575 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001576
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001577 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001578 if (set_target_expiration(apic) &&
1579 !(kvm_x86_ops->set_hv_timer && start_hv_timer(apic)))
1580 start_sw_period(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001581 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li7e810a32016-10-24 18:23:12 +08001582 if (!(kvm_x86_ops->set_hv_timer && start_hv_timer(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001583 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001584 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001585}
1586
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001587static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1588{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001589 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001590
Radim Krčmář59fd1322015-06-30 22:19:16 +02001591 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1592 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1593 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001594 apic_debug("Receive NMI setting on APIC_LVT0 "
1595 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001596 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001597 } else
1598 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1599 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001600}
1601
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001602int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001603{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001604 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001605
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001606 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001607
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001608 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001609 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001610 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001611 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001612 else
1613 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001614 break;
1615
1616 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001617 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001618 apic_set_tpr(apic, val & 0xff);
1619 break;
1620
1621 case APIC_EOI:
1622 apic_set_eoi(apic);
1623 break;
1624
1625 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001626 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001627 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001628 else
1629 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001630 break;
1631
1632 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001633 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001634 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001635 recalculate_apic_map(apic->vcpu->kvm);
1636 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001637 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001638 break;
1639
Gleb Natapovfc61b802009-07-05 17:39:35 +03001640 case APIC_SPIV: {
1641 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001642 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001643 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001644 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001645 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1646 int i;
1647 u32 lvt_val;
1648
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001649 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001650 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001651 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001652 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001653 lvt_val | APIC_LVT_MASKED);
1654 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001655 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001656 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001657
1658 }
1659 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001660 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001661 case APIC_ICR:
1662 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001663 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001664 apic_send_ipi(apic);
1665 break;
1666
1667 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001668 if (!apic_x2apic_mode(apic))
1669 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001670 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001671 break;
1672
Jan Kiszka23930f92008-09-26 09:30:52 +02001673 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001674 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001675 case APIC_LVTTHMR:
1676 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001677 case APIC_LVT1:
1678 case APIC_LVTERR:
1679 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001680 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001681 val |= APIC_LVT_MASKED;
1682
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001683 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001684 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001685
1686 break;
1687
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001688 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001689 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001690 val |= APIC_LVT_MASKED;
1691 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001692 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001693 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001694 break;
1695
Eddie Dong97222cc2007-09-12 10:58:04 +03001696 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001697 if (apic_lvtt_tscdeadline(apic))
1698 break;
1699
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001700 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001701 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001702 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001703 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001704
1705 case APIC_TDCR:
1706 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001707 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001708 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001709 update_divide_count(apic);
1710 break;
1711
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001712 case APIC_ESR:
1713 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001714 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001715 ret = 1;
1716 }
1717 break;
1718
1719 case APIC_SELF_IPI:
1720 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001721 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001722 } else
1723 ret = 1;
1724 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001725 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001726 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001727 break;
1728 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001729 if (ret)
1730 apic_debug("Local APIC Write to read-only register %x\n", reg);
1731 return ret;
1732}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001733EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001734
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001735static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001736 gpa_t address, int len, const void *data)
1737{
1738 struct kvm_lapic *apic = to_lapic(this);
1739 unsigned int offset = address - apic->base_address;
1740 u32 val;
1741
1742 if (!apic_mmio_in_range(apic, address))
1743 return -EOPNOTSUPP;
1744
1745 /*
1746 * APIC register must be aligned on 128-bits boundary.
1747 * 32/64/128 bits registers must be accessed thru 32 bits.
1748 * Refer SDM 8.4.1
1749 */
1750 if (len != 4 || (offset & 0xf)) {
1751 /* Don't shout loud, $infamous_os would cause only noise. */
1752 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001753 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001754 }
1755
1756 val = *(u32*)data;
1757
1758 /* too common printing */
1759 if (offset != APIC_EOI)
1760 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1761 "0x%x\n", __func__, offset, len, val);
1762
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001763 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001764
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001765 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001766}
1767
Kevin Tian58fbbf22011-08-30 13:56:17 +03001768void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1769{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001770 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001771}
1772EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1773
Yang Zhang83d4c282013-01-25 10:18:49 +08001774/* emulate APIC access in a trap manner */
1775void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1776{
1777 u32 val = 0;
1778
1779 /* hw has done the conditional check and inst decode */
1780 offset &= 0xff0;
1781
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001782 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001783
1784 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001785 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001786}
1787EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1788
Rusty Russelld5894442007-10-08 10:48:30 +10001789void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001790{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001791 struct kvm_lapic *apic = vcpu->arch.apic;
1792
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001793 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001794 return;
1795
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001796 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001797
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001798 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1799 static_key_slow_dec_deferred(&apic_hw_disabled);
1800
Radim Krčmáře4627552014-10-30 15:06:45 +01001801 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001802 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001803
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001804 if (apic->regs)
1805 free_page((unsigned long)apic->regs);
1806
1807 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001808}
1809
1810/*
1811 *----------------------------------------------------------------------
1812 * LAPIC interface
1813 *----------------------------------------------------------------------
1814 */
Wanpeng Li498f8162016-10-24 18:23:11 +08001815u64 kvm_get_lapic_target_expiration_tsc(struct kvm_vcpu *vcpu)
1816{
1817 struct kvm_lapic *apic = vcpu->arch.apic;
1818
1819 if (!lapic_in_kernel(vcpu))
1820 return 0;
1821
1822 return apic->lapic_timer.tscdeadline;
1823}
Eddie Dong97222cc2007-09-12 10:58:04 +03001824
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001825u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1826{
1827 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001828
Wanpeng Lia10388e2016-10-24 18:23:10 +08001829 if (!lapic_in_kernel(vcpu) ||
1830 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001831 return 0;
1832
1833 return apic->lapic_timer.tscdeadline;
1834}
1835
1836void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1837{
1838 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001839
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001840 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001841 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001842 return;
1843
1844 hrtimer_cancel(&apic->lapic_timer.timer);
1845 apic->lapic_timer.tscdeadline = data;
1846 start_apic_timer(apic);
1847}
1848
Eddie Dong97222cc2007-09-12 10:58:04 +03001849void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1850{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001851 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001852
Avi Kivityb93463a2007-10-25 16:52:32 +02001853 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001854 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001855}
1856
1857u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1858{
Eddie Dong97222cc2007-09-12 10:58:04 +03001859 u64 tpr;
1860
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001861 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001862
1863 return (tpr & 0xf0) >> 4;
1864}
1865
1866void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1867{
Yang Zhang8d146952013-01-25 10:18:50 +08001868 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001869 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001870
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08001871 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001872 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001873
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001874 vcpu->arch.apic_base = value;
1875
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08001876 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
1877 kvm_update_cpuid(vcpu);
1878
1879 if (!apic)
1880 return;
1881
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001882 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001883 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001884 if (value & MSR_IA32_APICBASE_ENABLE) {
1885 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001886 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08001887 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001888 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08001889 recalculate_apic_map(vcpu->kvm);
1890 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001891 }
1892
Yang Zhang8d146952013-01-25 10:18:50 +08001893 if ((old_value ^ value) & X2APIC_ENABLE) {
1894 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001895 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001896 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1897 } else
1898 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001899 }
Yang Zhang8d146952013-01-25 10:18:50 +08001900
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001901 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001902 MSR_IA32_APICBASE_BASE;
1903
Nadav Amitdb324fe2014-11-02 11:54:59 +02001904 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1905 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1906 pr_warn_once("APIC base relocation is unsupported by KVM");
1907
Eddie Dong97222cc2007-09-12 10:58:04 +03001908 /* with FSB delivery interrupt, we can restart APIC functionality */
1909 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001910 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001911
1912}
1913
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001914void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001915{
1916 struct kvm_lapic *apic;
1917 int i;
1918
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001919 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001920
1921 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001922 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001923 ASSERT(apic != NULL);
1924
1925 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001926 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001927
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001928 if (!init_event) {
1929 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1930 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001931 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001932 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001933 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001934
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001935 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1936 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001937 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001938 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001939 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001940 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001941 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001942
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001943 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001944 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001945 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001946 if (!apic_x2apic_mode(apic))
1947 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001948 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1949 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1950 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1951 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1952 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001953 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001954 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1955 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1956 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001957 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001958 apic->irr_pending = vcpu->arch.apicv_active;
1959 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001960 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001961 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001962 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001963 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001964 kvm_lapic_set_base(vcpu,
1965 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001966 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001967 apic_update_ppr(apic);
1968
Gleb Natapove1035712009-03-05 16:34:59 +02001969 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001970 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001971
Radim Krčmář6e500432016-12-15 18:06:46 +01001972 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001973 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Radim Krčmář6e500432016-12-15 18:06:46 +01001974 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001975 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001976}
1977
Eddie Dong97222cc2007-09-12 10:58:04 +03001978/*
1979 *----------------------------------------------------------------------
1980 * timer interface
1981 *----------------------------------------------------------------------
1982 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001983
Avi Kivity2a6eac92012-07-26 18:01:51 +03001984static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001985{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001986 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001987}
1988
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001989int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1990{
Gleb Natapov54e98182012-08-05 15:58:32 +03001991 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001992
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001993 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001994 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001995
1996 return 0;
1997}
1998
Avi Kivity89342082011-11-10 14:57:21 +02001999int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03002000{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002001 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02002002 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002003
Gleb Natapovc48f1492012-08-05 15:58:33 +03002004 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02002005 vector = reg & APIC_VECTOR_MASK;
2006 mode = reg & APIC_MODE_MASK;
2007 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08002008 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
2009 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02002010 }
2011 return 0;
2012}
2013
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002014void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02002015{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02002016 struct kvm_lapic *apic = vcpu->arch.apic;
2017
2018 if (apic)
2019 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002020}
2021
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002022static const struct kvm_io_device_ops apic_mmio_ops = {
2023 .read = apic_mmio_read,
2024 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002025};
2026
Avi Kivitye9d90d42012-07-26 18:01:50 +03002027static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
2028{
2029 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03002030 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002031
Radim Krčmář5d87db72014-10-10 19:15:08 +02002032 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002033
Avi Kivity2a6eac92012-07-26 18:01:51 +03002034 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002035 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002036 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2037 return HRTIMER_RESTART;
2038 } else
2039 return HRTIMER_NORESTART;
2040}
2041
Eddie Dong97222cc2007-09-12 10:58:04 +03002042int kvm_create_lapic(struct kvm_vcpu *vcpu)
2043{
2044 struct kvm_lapic *apic;
2045
2046 ASSERT(vcpu != NULL);
2047 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2048
2049 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
2050 if (!apic)
2051 goto nomem;
2052
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002053 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002054
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002055 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
2056 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002057 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2058 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002059 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002060 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002061 apic->vcpu = vcpu;
2062
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002063 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002064 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002065 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002066
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002067 /*
2068 * APIC is created enabled. This will prevent kvm_lapic_set_base from
2069 * thinking that APIC satet has changed.
2070 */
2071 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002072 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002073 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002074 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002075
2076 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002077nomem_free_apic:
2078 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002079nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002080 return -ENOMEM;
2081}
Eddie Dong97222cc2007-09-12 10:58:04 +03002082
2083int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2084{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002086 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002087
Paolo Bonzinif8543d62016-01-08 13:42:24 +01002088 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002089 return -1;
2090
Paolo Bonzinib3c045d2016-12-18 21:47:54 +01002091 __apic_update_ppr(apic, &ppr);
2092 return apic_has_interrupt_for_ppr(apic, ppr);
Eddie Dong97222cc2007-09-12 10:58:04 +03002093}
2094
Qing He40487c62007-09-17 14:47:13 +08002095int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2096{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002097 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002098 int r = 0;
2099
Gleb Natapovc48f1492012-08-05 15:58:33 +03002100 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002101 r = 1;
2102 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2103 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2104 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002105 return r;
2106}
2107
Eddie Dong1b9778d2007-09-03 16:56:58 +03002108void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2109{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002111
Gleb Natapov54e98182012-08-05 15:58:32 +03002112 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002113 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002114 if (apic_lvtt_tscdeadline(apic))
2115 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002116 if (apic_lvtt_oneshot(apic)) {
2117 apic->lapic_timer.tscdeadline = 0;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002118 apic->lapic_timer.target_expiration = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002119 }
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002120 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002121 }
2122}
2123
Eddie Dong97222cc2007-09-12 10:58:04 +03002124int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2125{
2126 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002127 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002128 u32 ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +03002129
2130 if (vector == -1)
2131 return -1;
2132
Wanpeng Li56cc2402014-08-05 12:42:24 +08002133 /*
2134 * We get here even with APIC virtualization enabled, if doing
2135 * nested virtualization and L1 runs with the "acknowledge interrupt
2136 * on exit" mode. Then we cannot inject the interrupt via RVI,
2137 * because the process would deliver it through the IDT.
2138 */
2139
Eddie Dong97222cc2007-09-12 10:58:04 +03002140 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002141 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002142 /*
2143 * For auto-EOI interrupts, there might be another pending
2144 * interrupt above PPR, so check whether to raise another
2145 * KVM_REQ_EVENT.
2146 */
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002147 apic_update_ppr(apic);
Paolo Bonzini4d82d122016-12-18 21:43:41 +01002148 } else {
2149 /*
2150 * For normal interrupts, PPR has been raised and there cannot
2151 * be a higher-priority pending interrupt---except if there was
2152 * a concurrent interrupt injection, but that would have
2153 * triggered KVM_REQ_EVENT already.
2154 */
2155 apic_set_isr(vector, apic);
2156 __apic_update_ppr(apic, &ppr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002157 }
2158
Eddie Dong97222cc2007-09-12 10:58:04 +03002159 return vector;
2160}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002161
Radim Krčmářa92e2542016-07-12 22:09:22 +02002162static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2163 struct kvm_lapic_state *s, bool set)
2164{
2165 if (apic_x2apic_mode(vcpu->arch.apic)) {
2166 u32 *id = (u32 *)(s->regs + APIC_ID);
2167
Radim Krčmář371313132016-07-12 22:09:27 +02002168 if (vcpu->kvm->arch.x2apic_format) {
2169 if (*id != vcpu->vcpu_id)
2170 return -EINVAL;
2171 } else {
2172 if (set)
2173 *id >>= 24;
2174 else
2175 *id <<= 24;
2176 }
Radim Krčmářa92e2542016-07-12 22:09:22 +02002177 }
2178
2179 return 0;
2180}
2181
2182int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2183{
2184 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2185 return kvm_apic_state_fixup(vcpu, s, false);
2186}
2187
2188int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002189{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002190 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002191 int r;
2192
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002193
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002194 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002195 /* set SPIV separately to get count of SW disabled APICs right */
2196 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002197
2198 r = kvm_apic_state_fixup(vcpu, s, true);
2199 if (r)
2200 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002201 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002202
2203 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002204 kvm_apic_set_version(vcpu);
2205
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002206 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002207 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002208 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002209 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002210 update_divide_count(apic);
2211 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002212 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002213 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002214 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002215 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002216 if (vcpu->arch.apicv_active) {
Paolo Bonzini967235d2016-12-19 14:03:45 +01002217 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002218 kvm_x86_ops->hwapic_irr_update(vcpu,
2219 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002220 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002221 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002222 }
Avi Kivity3842d132010-07-27 12:30:24 +03002223 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df63972015-07-29 23:21:40 -07002224 if (ioapic_in_kernel(vcpu->kvm))
2225 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002226
2227 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002228
2229 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002230}
Eddie Donga3d7f852007-09-03 16:15:12 +03002231
Avi Kivity2f52d582008-01-16 12:49:30 +02002232void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002233{
Eddie Donga3d7f852007-09-03 16:15:12 +03002234 struct hrtimer *timer;
2235
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002236 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002237 return;
2238
Gleb Natapov54e98182012-08-05 15:58:32 +03002239 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002240 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002241 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002242}
Avi Kivityb93463a2007-10-25 16:52:32 +02002243
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002244/*
2245 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2246 *
2247 * Detect whether guest triggered PV EOI since the
2248 * last entry. If yes, set EOI on guests's behalf.
2249 * Clear PV EOI in guest memory in any case.
2250 */
2251static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2252 struct kvm_lapic *apic)
2253{
2254 bool pending;
2255 int vector;
2256 /*
2257 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2258 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2259 *
2260 * KVM_APIC_PV_EOI_PENDING is unset:
2261 * -> host disabled PV EOI.
2262 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2263 * -> host enabled PV EOI, guest did not execute EOI yet.
2264 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2265 * -> host enabled PV EOI, guest executed EOI.
2266 */
2267 BUG_ON(!pv_eoi_enabled(vcpu));
2268 pending = pv_eoi_get_pending(vcpu);
2269 /*
2270 * Clear pending bit in any case: it will be set again on vmentry.
2271 * While this might not be ideal from performance point of view,
2272 * this makes sure pv eoi is only enabled when we know it's safe.
2273 */
2274 pv_eoi_clr_pending(vcpu);
2275 if (pending)
2276 return;
2277 vector = apic_set_eoi(apic);
2278 trace_kvm_pv_eoi(apic, vector);
2279}
2280
Avi Kivityb93463a2007-10-25 16:52:32 +02002281void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2282{
2283 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002284
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002285 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2286 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2287
Gleb Natapov41383772012-04-19 14:06:29 +03002288 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002289 return;
2290
Nicholas Krause603242a2015-08-05 10:44:40 -04002291 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2292 sizeof(u32)))
2293 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002294
2295 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2296}
2297
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002298/*
2299 * apic_sync_pv_eoi_to_guest - called before vmentry
2300 *
2301 * Detect whether it's safe to enable PV EOI and
2302 * if yes do so.
2303 */
2304static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2305 struct kvm_lapic *apic)
2306{
2307 if (!pv_eoi_enabled(vcpu) ||
2308 /* IRR set or many bits in ISR: could be nested. */
2309 apic->irr_pending ||
2310 /* Cache not set: could be safe but we don't bother. */
2311 apic->highest_isr_cache == -1 ||
2312 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002313 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002314 /*
2315 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2316 * so we need not do anything here.
2317 */
2318 return;
2319 }
2320
2321 pv_eoi_set_pending(apic->vcpu);
2322}
2323
Avi Kivityb93463a2007-10-25 16:52:32 +02002324void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2325{
2326 u32 data, tpr;
2327 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002328 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002329
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002330 apic_sync_pv_eoi_to_guest(vcpu, apic);
2331
Gleb Natapov41383772012-04-19 14:06:29 +03002332 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002333 return;
2334
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002335 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002336 max_irr = apic_find_highest_irr(apic);
2337 if (max_irr < 0)
2338 max_irr = 0;
2339 max_isr = apic_find_highest_isr(apic);
2340 if (max_isr < 0)
2341 max_isr = 0;
2342 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2343
Andy Honigfda4e2e2013-11-20 10:23:22 -08002344 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2345 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002346}
2347
Andy Honigfda4e2e2013-11-20 10:23:22 -08002348int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002349{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002350 if (vapic_addr) {
2351 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2352 &vcpu->arch.apic->vapic_cache,
2353 vapic_addr, sizeof(u32)))
2354 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002355 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002356 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002357 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002358 }
2359
2360 vcpu->arch.apic->vapic_addr = vapic_addr;
2361 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002362}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002363
2364int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2365{
2366 struct kvm_lapic *apic = vcpu->arch.apic;
2367 u32 reg = (msr - APIC_BASE_MSR) << 4;
2368
Paolo Bonzini35754c92015-07-29 12:05:37 +02002369 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002370 return 1;
2371
Nadav Amitc69d3d92014-11-26 17:56:25 +02002372 if (reg == APIC_ICR2)
2373 return 1;
2374
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002375 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002376 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002377 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2378 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002379}
2380
2381int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2382{
2383 struct kvm_lapic *apic = vcpu->arch.apic;
2384 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2385
Paolo Bonzini35754c92015-07-29 12:05:37 +02002386 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002387 return 1;
2388
Nadav Amitc69d3d92014-11-26 17:56:25 +02002389 if (reg == APIC_DFR || reg == APIC_ICR2) {
2390 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2391 reg);
2392 return 1;
2393 }
2394
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002395 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002396 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002397 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002398 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002399
2400 *data = (((u64)high) << 32) | low;
2401
2402 return 0;
2403}
Gleb Natapov10388a02010-01-17 15:51:23 +02002404
2405int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2406{
2407 struct kvm_lapic *apic = vcpu->arch.apic;
2408
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002409 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002410 return 1;
2411
2412 /* if this is ICR write vector before command */
2413 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002414 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2415 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002416}
2417
2418int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2419{
2420 struct kvm_lapic *apic = vcpu->arch.apic;
2421 u32 low, high = 0;
2422
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002423 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002424 return 1;
2425
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002426 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002427 return 1;
2428 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002429 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002430
2431 *data = (((u64)high) << 32) | low;
2432
2433 return 0;
2434}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002435
2436int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2437{
2438 u64 addr = data & ~KVM_MSR_ENABLED;
2439 if (!IS_ALIGNED(addr, 4))
2440 return 1;
2441
2442 vcpu->arch.pv_eoi.msr_val = data;
2443 if (!pv_eoi_enabled(vcpu))
2444 return 0;
2445 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002446 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002447}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002448
Jan Kiszka66450a22013-03-13 12:42:34 +01002449void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2450{
2451 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002452 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002453 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002454
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002455 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002456 return;
2457
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002458 /*
2459 * INITs are latched while in SMM. Because an SMM CPU cannot
2460 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2461 * and delay processing of INIT until the next RSM.
2462 */
2463 if (is_smm(vcpu)) {
2464 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2465 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2466 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2467 return;
2468 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002469
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002470 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002471 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002472 kvm_lapic_reset(vcpu, true);
2473 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002474 if (kvm_vcpu_is_bsp(apic->vcpu))
2475 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2476 else
2477 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2478 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002479 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002480 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2481 /* evaluate pending_events before reading the vector */
2482 smp_rmb();
2483 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002484 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002485 vcpu->vcpu_id, sipi_vector);
2486 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2487 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2488 }
2489}
2490
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002491void kvm_lapic_init(void)
2492{
2493 /* do not patch jump label more than once per second */
2494 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002495 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002496}
David Matlackcef84c32016-12-16 14:30:36 -08002497
2498void kvm_lapic_exit(void)
2499{
2500 static_key_deferred_flush(&apic_hw_disabled);
2501 static_key_deferred_flush(&apic_sw_disabled);
2502}