blob: 7e9ac4606279a913a0f7d7cb8a9118b5f4a96985 [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)
344 return fls(*reg) - 1 + vec;
345 }
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
Wincy Van705699a2015-02-03 23:58:17 +0800364void __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800365{
366 u32 i, pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800367
368 for (i = 0; i <= 7; i++) {
Paolo Bonziniad361092016-09-20 16:15:05 +0200369 pir_val = READ_ONCE(pir[i]);
370 if (pir_val) {
371 pir_val = xchg(&pir[i], 0);
Wincy Van705699a2015-02-03 23:58:17 +0800372 *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
Paolo Bonziniad361092016-09-20 16:15:05 +0200373 }
Yang Zhanga20ed542013-04-11 19:25:15 +0800374 }
375}
Wincy Van705699a2015-02-03 23:58:17 +0800376EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
377
378void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
379{
380 struct kvm_lapic *apic = vcpu->arch.apic;
381
382 __kvm_apic_update_irr(pir, apic->regs);
Radim Krčmářc77f3fa2015-10-08 20:23:33 +0200383
384 kvm_make_request(KVM_REQ_EVENT, vcpu);
Wincy Van705699a2015-02-03 23:58:17 +0800385}
Yang Zhanga20ed542013-04-11 19:25:15 +0800386EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
387
Gleb Natapov33e4c682009-06-11 11:06:51 +0300388static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300389{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300390 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300391}
392
393static inline int apic_find_highest_irr(struct kvm_lapic *apic)
394{
395 int result;
396
Yang Zhangc7c9c562013-01-25 10:18:51 +0800397 /*
398 * Note that irr_pending is just a hint. It will be always
399 * true with virtual interrupt delivery enabled.
400 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300401 if (!apic->irr_pending)
402 return -1;
403
Andrey Smetanind62caab2015-11-10 15:36:33 +0300404 if (apic->vcpu->arch.apicv_active)
405 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
Gleb Natapov33e4c682009-06-11 11:06:51 +0300406 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300407 ASSERT(result == -1 || result >= 16);
408
409 return result;
410}
411
Gleb Natapov33e4c682009-06-11 11:06:51 +0300412static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
413{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800414 struct kvm_vcpu *vcpu;
415
416 vcpu = apic->vcpu;
417
Andrey Smetanind62caab2015-11-10 15:36:33 +0300418 if (unlikely(vcpu->arch.apicv_active)) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800419 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200420 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800421 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200422 } else {
423 apic->irr_pending = false;
424 apic_clear_vector(vec, apic->regs + APIC_IRR);
425 if (apic_search_irr(apic) != -1)
426 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800427 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300428}
429
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300430static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
431{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800432 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200433
Wanpeng Li56cc2402014-08-05 12:42:24 +0800434 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
435 return;
436
437 vcpu = apic->vcpu;
438
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300439 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800440 * With APIC virtualization enabled, all caching is disabled
441 * because the processor can modify ISR under the hood. Instead
442 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300443 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300444 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200445 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800446 else {
447 ++apic->isr_count;
448 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
449 /*
450 * ISR (in service register) bit is set when injecting an interrupt.
451 * The highest vector is injected. Thus the latest bit set matches
452 * the highest bit in ISR.
453 */
454 apic->highest_isr_cache = vec;
455 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300456}
457
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200458static inline int apic_find_highest_isr(struct kvm_lapic *apic)
459{
460 int result;
461
462 /*
463 * Note that isr_count is always 1, and highest_isr_cache
464 * is always -1, with APIC virtualization enabled.
465 */
466 if (!apic->isr_count)
467 return -1;
468 if (likely(apic->highest_isr_cache != -1))
469 return apic->highest_isr_cache;
470
471 result = find_highest_vector(apic->regs + APIC_ISR);
472 ASSERT(result == -1 || result >= 16);
473
474 return result;
475}
476
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300477static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
478{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200479 struct kvm_vcpu *vcpu;
480 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
481 return;
482
483 vcpu = apic->vcpu;
484
485 /*
486 * We do get here for APIC virtualization enabled if the guest
487 * uses the Hyper-V APIC enlightenment. In this case we may need
488 * to trigger a new interrupt delivery by writing the SVI field;
489 * on the other hand isr_count and highest_isr_cache are unused
490 * and must be left alone.
491 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300492 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200493 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200494 apic_find_highest_isr(apic));
495 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300496 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200497 BUG_ON(apic->isr_count < 0);
498 apic->highest_isr_cache = -1;
499 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300500}
501
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800502int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
503{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300504 /* This may race with setting of irr in __apic_accept_irq() and
505 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
506 * will cause vmexit immediately and the value will be recalculated
507 * on the next vmentry.
508 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100509 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800510}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800511
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200512static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800513 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100514 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200515
Yang Zhangb4f22252013-04-11 19:21:37 +0800516int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100517 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300518{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800519 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800520
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200521 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800522 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300523}
524
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300525static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
526{
527
528 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
529 sizeof(val));
530}
531
532static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
533{
534
535 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
536 sizeof(*val));
537}
538
539static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
540{
541 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
542}
543
544static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
545{
546 u8 val;
547 if (pv_eoi_get_user(vcpu, &val) < 0)
548 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800549 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300550 return val & 0x1;
551}
552
553static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
554{
555 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
556 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800557 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300558 return;
559 }
560 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
561}
562
563static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
564{
565 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
566 apic_debug("Can't clear EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800567 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300568 return;
569 }
570 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
571}
572
Eddie Dong97222cc2007-09-12 10:58:04 +0300573static void apic_update_ppr(struct kvm_lapic *apic)
574{
Avi Kivity3842d132010-07-27 12:30:24 +0300575 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300576 int isr;
577
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500578 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
579 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300580 isr = apic_find_highest_isr(apic);
581 isrv = (isr != -1) ? isr : 0;
582
583 if ((tpr & 0xf0) >= (isrv & 0xf0))
584 ppr = tpr & 0xff;
585 else
586 ppr = isrv & 0xf0;
587
588 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
589 apic, ppr, isr, isrv);
590
Avi Kivity3842d132010-07-27 12:30:24 +0300591 if (old_ppr != ppr) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500592 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200593 if (ppr < old_ppr)
594 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300595 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300596}
597
598static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
599{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500600 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300601 apic_update_ppr(apic);
602}
603
Radim Krčmář03d22492015-02-12 19:41:31 +0100604static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300605{
Radim Krčmářb4535b52016-12-15 18:06:47 +0100606 return mda == (apic_x2apic_mode(apic) ?
607 X2APIC_BROADCAST : APIC_BROADCAST);
Eddie Dong97222cc2007-09-12 10:58:04 +0300608}
609
Radim Krčmář03d22492015-02-12 19:41:31 +0100610static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300611{
Radim Krčmář03d22492015-02-12 19:41:31 +0100612 if (kvm_apic_broadcast(apic, mda))
613 return true;
614
615 if (apic_x2apic_mode(apic))
Radim Krčmář6e500432016-12-15 18:06:46 +0100616 return mda == kvm_x2apic_id(apic);
Radim Krčmář03d22492015-02-12 19:41:31 +0100617
Radim Krčmář5bd5db32016-12-15 18:06:48 +0100618 /*
619 * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
620 * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and
621 * this allows unique addressing of VCPUs with APIC ID over 0xff.
622 * The 0xff condition is needed because writeable xAPIC ID.
623 */
624 if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
625 return true;
626
Radim Krčmářb4535b52016-12-15 18:06:47 +0100627 return mda == kvm_xapic_id(apic);
Nadav Amit394457a2014-10-03 00:30:52 +0300628}
629
Radim Krčmář52c233a2015-01-29 22:48:48 +0100630static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300631{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300632 u32 logical_id;
633
Nadav Amit394457a2014-10-03 00:30:52 +0300634 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100635 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300636
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500637 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300638
Radim Krčmář9368b562015-01-29 22:48:49 +0100639 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100640 return ((logical_id >> 16) == (mda >> 16))
641 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100642
643 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Eddie Dong97222cc2007-09-12 10:58:04 +0300644
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500645 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300646 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100647 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300648 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100649 return ((logical_id >> 4) == (mda >> 4))
650 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300651 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200652 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500653 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100654 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300655 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300656}
657
Radim Krčmářc5192652016-07-12 22:09:28 +0200658/* The KVM local APIC implementation has two quirks:
659 *
Radim Krčmářb4535b52016-12-15 18:06:47 +0100660 * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
661 * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
662 * KVM doesn't do that aliasing.
Radim Krčmářc5192652016-07-12 22:09:28 +0200663 *
664 * - in-kernel IOAPIC messages have to be delivered directly to
665 * x2APIC, because the kernel does not support interrupt remapping.
666 * In order to support broadcast without interrupt remapping, x2APIC
667 * rewrites the destination of non-IPI messages from APIC_BROADCAST
668 * to X2APIC_BROADCAST.
669 *
670 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
671 * important when userspace wants to use x2APIC-format MSIs, because
672 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100673 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200674static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
675 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100676{
677 bool ipi = source != NULL;
Radim Krčmář03d22492015-02-12 19:41:31 +0100678
Radim Krčmářc5192652016-07-12 22:09:28 +0200679 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
Radim Krčmářb4535b52016-12-15 18:06:47 +0100680 !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
Radim Krčmář03d22492015-02-12 19:41:31 +0100681 return X2APIC_BROADCAST;
682
Radim Krčmářb4535b52016-12-15 18:06:47 +0100683 return dest_id;
Radim Krčmář03d22492015-02-12 19:41:31 +0100684}
685
Radim Krčmář52c233a2015-01-29 22:48:48 +0100686bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300687 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300688{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800689 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200690 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300691
692 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200693 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300694 target, source, dest, dest_mode, short_hand);
695
Zachary Amsdenbd371392010-06-14 11:42:15 -1000696 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300697 switch (short_hand) {
698 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100699 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100700 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200701 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100702 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300703 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100704 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300705 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100706 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300707 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100708 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300709 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200710 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
711 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100712 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300713 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300714}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500715EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300716
Feng Wu520040142016-01-25 16:53:33 +0800717int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
718 const unsigned long *bitmap, u32 bitmap_size)
719{
720 u32 mod;
721 int i, idx = -1;
722
723 mod = vector % dest_vcpus;
724
725 for (i = 0; i <= mod; i++) {
726 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
727 BUG_ON(idx == bitmap_size);
728 }
729
730 return idx;
731}
732
Radim Krčmář4efd8052016-02-12 15:00:15 +0100733static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
734{
735 if (!kvm->arch.disabled_lapic_found) {
736 kvm->arch.disabled_lapic_found = true;
737 printk(KERN_INFO
738 "Disabled LAPIC found during irq injection\n");
739 }
740}
741
Radim Krčmářc5192652016-07-12 22:09:28 +0200742static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
743 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
744{
745 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
746 if ((irq->dest_id == APIC_BROADCAST &&
747 map->mode != KVM_APIC_MODE_X2APIC))
748 return true;
749 if (irq->dest_id == X2APIC_BROADCAST)
750 return true;
751 } else {
752 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
753 if (irq->dest_id == (x2apic_ipi ?
754 X2APIC_BROADCAST : APIC_BROADCAST))
755 return true;
756 }
757
758 return false;
759}
760
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200761/* Return true if the interrupt can be handled by using *bitmap as index mask
762 * for valid destinations in *dst array.
763 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
764 * Note: we may have zero kvm_lapic destinations when we return true, which
765 * means that the interrupt should be dropped. In this case, *bitmap would be
766 * zero and *dst undefined.
767 */
768static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
769 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
770 struct kvm_apic_map *map, struct kvm_lapic ***dst,
771 unsigned long *bitmap)
772{
773 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200774
775 if (irq->shorthand == APIC_DEST_SELF && src) {
776 *dst = src;
777 *bitmap = 1;
778 return true;
779 } else if (irq->shorthand)
780 return false;
781
Radim Krčmářc5192652016-07-12 22:09:28 +0200782 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200783 return false;
784
785 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200786 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200787 *bitmap = 0;
788 } else {
789 *dst = &map->phys_map[irq->dest_id];
790 *bitmap = 1;
791 }
792 return true;
793 }
794
Radim Krčmáře45115b2016-07-12 22:09:19 +0200795 *bitmap = 0;
796 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
797 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200798 return false;
799
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200800 if (!kvm_lowest_prio_delivery(irq))
801 return true;
802
803 if (!kvm_vector_hashing_enabled()) {
804 lowest = -1;
805 for_each_set_bit(i, bitmap, 16) {
806 if (!(*dst)[i])
807 continue;
808 if (lowest < 0)
809 lowest = i;
810 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
811 (*dst)[lowest]->vcpu) < 0)
812 lowest = i;
813 }
814 } else {
815 if (!*bitmap)
816 return true;
817
818 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
819 bitmap, 16);
820
821 if (!(*dst)[lowest]) {
822 kvm_apic_disabled_lapic_found(kvm);
823 *bitmap = 0;
824 return true;
825 }
826 }
827
828 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
829
830 return true;
831}
832
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300833bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100834 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300835{
836 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200837 unsigned long bitmap;
838 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300839 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200840 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300841
842 *r = -1;
843
844 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800845 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300846 return true;
847 }
848
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300849 rcu_read_lock();
850 map = rcu_dereference(kvm->arch.apic_map);
851
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200852 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
853 if (ret)
854 for_each_set_bit(i, &bitmap, 16) {
855 if (!dst[i])
856 continue;
857 if (*r < 0)
858 *r = 0;
859 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100860 }
861
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300862 rcu_read_unlock();
863 return ret;
864}
865
Feng Wu6228a0d2016-01-25 16:53:34 +0800866/*
867 * This routine tries to handler interrupts in posted mode, here is how
868 * it deals with different cases:
869 * - For single-destination interrupts, handle it in posted mode
870 * - Else if vector hashing is enabled and it is a lowest-priority
871 * interrupt, handle it in posted mode and use the following mechanism
872 * to find the destinaiton vCPU.
873 * 1. For lowest-priority interrupts, store all the possible
874 * destination vCPUs in an array.
875 * 2. Use "guest vector % max number of destination vCPUs" to find
876 * the right destination vCPU in the array for the lowest-priority
877 * interrupt.
878 * - Otherwise, use remapped mode to inject the interrupt.
879 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800880bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
881 struct kvm_vcpu **dest_vcpu)
882{
883 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200884 unsigned long bitmap;
885 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800886 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800887
888 if (irq->shorthand)
889 return false;
890
891 rcu_read_lock();
892 map = rcu_dereference(kvm->arch.apic_map);
893
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200894 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
895 hweight16(bitmap) == 1) {
896 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800897
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200898 if (dst[i]) {
899 *dest_vcpu = dst[i]->vcpu;
900 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +0800901 }
Feng Wu8feb4a02015-09-18 22:29:47 +0800902 }
903
Feng Wu8feb4a02015-09-18 22:29:47 +0800904 rcu_read_unlock();
905 return ret;
906}
907
Eddie Dong97222cc2007-09-12 10:58:04 +0300908/*
909 * Add a pending IRQ into lapic.
910 * Return 1 if successfully added and 0 if discarded.
911 */
912static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800913 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100914 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300915{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200916 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300917 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300918
Paolo Bonzinia183b632014-09-11 11:51:02 +0200919 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
920 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300921 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300922 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200923 vcpu->arch.apic_arb_prio++;
924 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200925 if (unlikely(trig_mode && !level))
926 break;
927
Eddie Dong97222cc2007-09-12 10:58:04 +0300928 /* FIXME add logic for vcpu on reset */
929 if (unlikely(!apic_enabled(apic)))
930 break;
931
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200932 result = 1;
933
Joerg Roedel9daa5002016-02-29 16:04:44 +0100934 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100935 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +0100936 dest_map->vectors[vcpu->vcpu_id] = vector;
937 }
Avi Kivitya5d36f82009-12-29 12:42:16 +0200938
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200939 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
940 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500941 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200942 else
943 apic_clear_vector(vector, apic->regs + APIC_TMR);
944 }
945
Andrey Smetanind62caab2015-11-10 15:36:33 +0300946 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +0800947 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200948 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500949 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800950
951 kvm_make_request(KVM_REQ_EVENT, vcpu);
952 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300953 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300954 break;
955
956 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530957 result = 1;
958 vcpu->arch.pv.pv_unhalted = 1;
959 kvm_make_request(KVM_REQ_EVENT, vcpu);
960 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300961 break;
962
963 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200964 result = 1;
965 kvm_make_request(KVM_REQ_SMI, vcpu);
966 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300967 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800968
Eddie Dong97222cc2007-09-12 10:58:04 +0300969 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200970 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800971 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200972 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300973 break;
974
975 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100976 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200977 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100978 /* assumes that there are only KVM_APIC_INIT/SIPI */
979 apic->pending_events = (1UL << KVM_APIC_INIT);
980 /* make sure pending_events is visible before sending
981 * the request */
982 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300983 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300984 kvm_vcpu_kick(vcpu);
985 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200986 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
987 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300988 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300989 break;
990
991 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200992 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
993 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100994 result = 1;
995 apic->sipi_vector = vector;
996 /* make sure sipi_vector is visible for the receiver */
997 smp_wmb();
998 set_bit(KVM_APIC_SIPI, &apic->pending_events);
999 kvm_make_request(KVM_REQ_EVENT, vcpu);
1000 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001001 break;
1002
Jan Kiszka23930f92008-09-26 09:30:52 +02001003 case APIC_DM_EXTINT:
1004 /*
1005 * Should only be called by kvm_apic_local_deliver() with LVT0,
1006 * before NMI watchdog was enabled. Already handled by
1007 * kvm_apic_accept_pic_intr().
1008 */
1009 break;
1010
Eddie Dong97222cc2007-09-12 10:58:04 +03001011 default:
1012 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1013 delivery_mode);
1014 break;
1015 }
1016 return result;
1017}
1018
Gleb Natapove1035712009-03-05 16:34:59 +02001019int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001020{
Gleb Natapove1035712009-03-05 16:34:59 +02001021 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001022}
1023
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001024static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1025{
Andrey Smetanin63086302015-11-10 15:36:32 +03001026 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001027}
1028
Yang Zhangc7c9c562013-01-25 10:18:51 +08001029static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1030{
Steve Rutherford7543a632015-07-29 23:21:41 -07001031 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001032
Steve Rutherford7543a632015-07-29 23:21:41 -07001033 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1034 if (!kvm_ioapic_handles_vector(apic, vector))
1035 return;
1036
1037 /* Request a KVM exit to inform the userspace IOAPIC. */
1038 if (irqchip_split(apic->vcpu->kvm)) {
1039 apic->vcpu->arch.pending_ioapic_eoi = vector;
1040 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1041 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001042 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001043
1044 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1045 trigger_mode = IOAPIC_LEVEL_TRIG;
1046 else
1047 trigger_mode = IOAPIC_EDGE_TRIG;
1048
1049 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001050}
1051
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001052static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001053{
1054 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001055
1056 trace_kvm_eoi(apic, vector);
1057
Eddie Dong97222cc2007-09-12 10:58:04 +03001058 /*
1059 * Not every write EOI will has corresponding ISR,
1060 * one example is when Kernel check timer on setup_IO_APIC
1061 */
1062 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001063 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001064
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001065 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001066 apic_update_ppr(apic);
1067
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001068 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1069 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1070
Yang Zhangc7c9c562013-01-25 10:18:51 +08001071 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001072 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001073 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001074}
1075
Yang Zhangc7c9c562013-01-25 10:18:51 +08001076/*
1077 * this interface assumes a trap-like exit, which has already finished
1078 * desired side effect including vISR and vPPR update.
1079 */
1080void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1081{
1082 struct kvm_lapic *apic = vcpu->arch.apic;
1083
1084 trace_kvm_eoi(apic, vector);
1085
1086 kvm_ioapic_send_eoi(apic, vector);
1087 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1088}
1089EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1090
Eddie Dong97222cc2007-09-12 10:58:04 +03001091static void apic_send_ipi(struct kvm_lapic *apic)
1092{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001093 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1094 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001095 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001096
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001097 irq.vector = icr_low & APIC_VECTOR_MASK;
1098 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1099 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001100 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001101 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1102 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001103 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001104 if (apic_x2apic_mode(apic))
1105 irq.dest_id = icr_high;
1106 else
1107 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001108
Gleb Natapov1000ff82009-07-07 16:00:57 +03001109 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1110
Eddie Dong97222cc2007-09-12 10:58:04 +03001111 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1112 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001113 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1114 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001115 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001116 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001117 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001118
Yang Zhangb4f22252013-04-11 19:21:37 +08001119 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001120}
1121
1122static u32 apic_get_tmcct(struct kvm_lapic *apic)
1123{
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001124 ktime_t remaining, now;
Marcelo Tosattib682b812009-02-10 20:41:41 -02001125 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001126 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001127
1128 ASSERT(apic != NULL);
1129
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001130 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001131 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001132 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001133 return 0;
1134
Paolo Bonzini55878592016-10-25 15:23:49 +02001135 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001136 remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001137 if (ktime_to_ns(remaining) < 0)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001138 remaining = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001139
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001140 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1141 tmcct = div64_u64(ns,
1142 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001143
1144 return tmcct;
1145}
1146
Avi Kivityb209749f2007-10-22 16:50:39 +02001147static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1148{
1149 struct kvm_vcpu *vcpu = apic->vcpu;
1150 struct kvm_run *run = vcpu->run;
1151
Avi Kivitya8eeb042010-05-10 12:34:53 +03001152 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001153 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001154 run->tpr_access.is_write = write;
1155}
1156
1157static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1158{
1159 if (apic->vcpu->arch.tpr_access_reporting)
1160 __report_tpr_access(apic, write);
1161}
1162
Eddie Dong97222cc2007-09-12 10:58:04 +03001163static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1164{
1165 u32 val = 0;
1166
1167 if (offset >= LAPIC_MMIO_LENGTH)
1168 return 0;
1169
1170 switch (offset) {
1171 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001172 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001173 break;
1174
1175 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001176 if (apic_lvtt_tscdeadline(apic))
1177 return 0;
1178
Eddie Dong97222cc2007-09-12 10:58:04 +03001179 val = apic_get_tmcct(apic);
1180 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001181 case APIC_PROCPRI:
1182 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001183 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001184 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001185 case APIC_TASKPRI:
1186 report_tpr_access(apic, false);
1187 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001188 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001189 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001190 break;
1191 }
1192
1193 return val;
1194}
1195
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001196static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1197{
1198 return container_of(dev, struct kvm_lapic, dev);
1199}
1200
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001201int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001202 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001203{
Eddie Dong97222cc2007-09-12 10:58:04 +03001204 unsigned char alignment = offset & 0xf;
1205 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001206 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001207 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001208
1209 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001210 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1211 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001212 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001213 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001214
1215 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001216 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1217 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001218 return 1;
1219 }
1220
Eddie Dong97222cc2007-09-12 10:58:04 +03001221 result = __apic_read(apic, offset & ~0xf);
1222
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001223 trace_kvm_apic_read(offset, result);
1224
Eddie Dong97222cc2007-09-12 10:58:04 +03001225 switch (len) {
1226 case 1:
1227 case 2:
1228 case 4:
1229 memcpy(data, (char *)&result + alignment, len);
1230 break;
1231 default:
1232 printk(KERN_ERR "Local APIC read with len = %x, "
1233 "should be 1,2, or 4 instead\n", len);
1234 break;
1235 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001236 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001237}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001238EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001239
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001240static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1241{
Gleb Natapovc48f1492012-08-05 15:58:33 +03001242 return kvm_apic_hw_enabled(apic) &&
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001243 addr >= apic->base_address &&
1244 addr < apic->base_address + LAPIC_MMIO_LENGTH;
1245}
1246
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001247static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001248 gpa_t address, int len, void *data)
1249{
1250 struct kvm_lapic *apic = to_lapic(this);
1251 u32 offset = address - apic->base_address;
1252
1253 if (!apic_mmio_in_range(apic, address))
1254 return -EOPNOTSUPP;
1255
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001256 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001257
1258 return 0;
1259}
1260
Eddie Dong97222cc2007-09-12 10:58:04 +03001261static void update_divide_count(struct kvm_lapic *apic)
1262{
1263 u32 tmp1, tmp2, tdcr;
1264
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001265 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001266 tmp1 = tdcr & 0xf;
1267 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001268 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001269
1270 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843dd2009-04-29 17:29:09 -04001271 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001272}
1273
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001274static void apic_update_lvtt(struct kvm_lapic *apic)
1275{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001276 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001277 apic->lapic_timer.timer_mode_mask;
1278
1279 if (apic->lapic_timer.timer_mode != timer_mode) {
1280 apic->lapic_timer.timer_mode = timer_mode;
1281 hrtimer_cancel(&apic->lapic_timer.timer);
1282 }
1283}
1284
Radim Krčmář5d87db72014-10-10 19:15:08 +02001285static void apic_timer_expired(struct kvm_lapic *apic)
1286{
1287 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001288 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001289 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001290
Radim Krčmář5d87db72014-10-10 19:15:08 +02001291 if (atomic_read(&apic->lapic_timer.pending))
1292 return;
1293
1294 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001295 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001296
Marcelo Tosatti85773702016-02-19 09:46:39 +01001297 if (swait_active(q))
1298 swake_up(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001299
1300 if (apic_lvtt_tscdeadline(apic))
1301 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1302}
1303
1304/*
1305 * On APICv, this test will cause a busy wait
1306 * during a higher-priority task.
1307 */
1308
1309static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1310{
1311 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001312 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001313
1314 if (kvm_apic_hw_enabled(apic)) {
1315 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001316 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001317
Andrey Smetanind62caab2015-11-10 15:36:33 +03001318 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001319 bitmap = apic->regs + APIC_IRR;
1320
1321 if (apic_test_vector(vec, bitmap))
1322 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001323 }
1324 return false;
1325}
1326
1327void wait_lapic_expire(struct kvm_vcpu *vcpu)
1328{
1329 struct kvm_lapic *apic = vcpu->arch.apic;
1330 u64 guest_tsc, tsc_deadline;
1331
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001332 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001333 return;
1334
1335 if (apic->lapic_timer.expired_tscdeadline == 0)
1336 return;
1337
1338 if (!lapic_timer_int_injected(vcpu))
1339 return;
1340
1341 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1342 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001343 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001344 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001345
1346 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1347 if (guest_tsc < tsc_deadline)
Marcelo Tosattib606f182016-06-20 22:33:48 -03001348 __delay(min(tsc_deadline - guest_tsc,
1349 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
Radim Krčmář5d87db72014-10-10 19:15:08 +02001350}
1351
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001352static void start_sw_tscdeadline(struct kvm_lapic *apic)
1353{
1354 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1355 u64 ns = 0;
1356 ktime_t expire;
1357 struct kvm_vcpu *vcpu = apic->vcpu;
1358 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1359 unsigned long flags;
1360 ktime_t now;
1361
1362 if (unlikely(!tscdeadline || !this_tsc_khz))
1363 return;
1364
1365 local_irq_save(flags);
1366
Paolo Bonzini55878592016-10-25 15:23:49 +02001367 now = ktime_get();
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001368 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1369 if (likely(tscdeadline > guest_tsc)) {
1370 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1371 do_div(ns, this_tsc_khz);
1372 expire = ktime_add_ns(now, ns);
1373 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1374 hrtimer_start(&apic->lapic_timer.timer,
1375 expire, HRTIMER_MODE_ABS_PINNED);
1376 } else
1377 apic_timer_expired(apic);
1378
1379 local_irq_restore(flags);
1380}
1381
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001382static void start_sw_period(struct kvm_lapic *apic)
1383{
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001384 if (!apic->lapic_timer.period)
1385 return;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001386
1387 if (apic_lvtt_oneshot(apic) &&
Paolo Bonzini55878592016-10-25 15:23:49 +02001388 ktime_after(ktime_get(),
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001389 apic->lapic_timer.target_expiration)) {
1390 apic_timer_expired(apic);
1391 return;
1392 }
1393
1394 hrtimer_start(&apic->lapic_timer.timer,
1395 apic->lapic_timer.target_expiration,
1396 HRTIMER_MODE_ABS_PINNED);
1397}
1398
1399static bool set_target_expiration(struct kvm_lapic *apic)
1400{
1401 ktime_t now;
1402 u64 tscl = rdtsc();
1403
Paolo Bonzini55878592016-10-25 15:23:49 +02001404 now = ktime_get();
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001405 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
1406 * APIC_BUS_CYCLE_NS * apic->divide_count;
1407
1408 if (!apic->lapic_timer.period)
1409 return false;
1410
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001411 /*
1412 * Do not allow the guest to program periodic timers with small
1413 * interval, since the hrtimers are not throttled by the host
1414 * scheduler.
1415 */
1416 if (apic_lvtt_period(apic)) {
1417 s64 min_period = min_timer_period_us * 1000LL;
1418
1419 if (apic->lapic_timer.period < min_period) {
1420 pr_info_ratelimited(
1421 "kvm: vcpu %i: requested %lld ns "
1422 "lapic timer period limited to %lld ns\n",
1423 apic->vcpu->vcpu_id,
1424 apic->lapic_timer.period, min_period);
1425 apic->lapic_timer.period = min_period;
1426 }
1427 }
1428
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001429 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
1430 PRIx64 ", "
1431 "timer initial count 0x%x, period %lldns, "
1432 "expire @ 0x%016" PRIx64 ".\n", __func__,
1433 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
1434 kvm_lapic_get_reg(apic, APIC_TMICT),
1435 apic->lapic_timer.period,
1436 ktime_to_ns(ktime_add_ns(now,
1437 apic->lapic_timer.period)));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001438
1439 apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
1440 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1441 apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period);
1442
1443 return true;
1444}
1445
1446static void advance_periodic_target_expiration(struct kvm_lapic *apic)
1447{
1448 apic->lapic_timer.tscdeadline +=
1449 nsec_to_cycles(apic->vcpu, apic->lapic_timer.period);
1450 apic->lapic_timer.target_expiration =
1451 ktime_add_ns(apic->lapic_timer.target_expiration,
1452 apic->lapic_timer.period);
Wanpeng Li7d7f7da2016-10-24 18:23:09 +08001453}
1454
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001455bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1456{
Wanpeng Li91005302016-08-03 12:04:12 +08001457 if (!lapic_in_kernel(vcpu))
1458 return false;
1459
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001460 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1461}
1462EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1463
Wanpeng Li7e810a32016-10-24 18:23:12 +08001464static void cancel_hv_timer(struct kvm_lapic *apic)
Wanpeng Libd97ad02016-06-30 08:52:49 +08001465{
1466 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1467 apic->lapic_timer.hv_timer_in_use = false;
1468}
1469
Wanpeng Li7e810a32016-10-24 18:23:12 +08001470static bool start_hv_timer(struct kvm_lapic *apic)
Wanpeng Li196f20c2016-06-28 14:54:19 +08001471{
1472 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1473
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001474 if ((atomic_read(&apic->lapic_timer.pending) &&
1475 !apic_lvtt_period(apic)) ||
Wanpeng Li196f20c2016-06-28 14:54:19 +08001476 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1477 if (apic->lapic_timer.hv_timer_in_use)
Wanpeng Li7e810a32016-10-24 18:23:12 +08001478 cancel_hv_timer(apic);
Wanpeng Li196f20c2016-06-28 14:54:19 +08001479 } else {
1480 apic->lapic_timer.hv_timer_in_use = true;
1481 hrtimer_cancel(&apic->lapic_timer.timer);
1482
1483 /* In case the sw timer triggered in the window */
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001484 if (atomic_read(&apic->lapic_timer.pending) &&
1485 !apic_lvtt_period(apic))
Wanpeng Li7e810a32016-10-24 18:23:12 +08001486 cancel_hv_timer(apic);
Wanpeng Li196f20c2016-06-28 14:54:19 +08001487 }
1488 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1489 apic->lapic_timer.hv_timer_in_use);
1490 return apic->lapic_timer.hv_timer_in_use;
1491}
1492
Eddie Dong97222cc2007-09-12 10:58:04 +03001493void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1494{
1495 struct kvm_lapic *apic = vcpu->arch.apic;
1496
1497 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1498 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001499 cancel_hv_timer(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001500 apic_timer_expired(apic);
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001501
1502 if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1503 advance_periodic_target_expiration(apic);
1504 if (!start_hv_timer(apic))
1505 start_sw_period(apic);
1506 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001507}
1508EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1509
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001510void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1511{
1512 struct kvm_lapic *apic = vcpu->arch.apic;
1513
1514 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1515
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001516 start_hv_timer(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001517}
1518EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1519
1520void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1521{
1522 struct kvm_lapic *apic = vcpu->arch.apic;
1523
1524 /* Possibly the TSC deadline timer is not enabled yet */
1525 if (!apic->lapic_timer.hv_timer_in_use)
1526 return;
1527
Wanpeng Li7e810a32016-10-24 18:23:12 +08001528 cancel_hv_timer(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001529
1530 if (atomic_read(&apic->lapic_timer.pending))
1531 return;
1532
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001533 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
1534 start_sw_period(apic);
1535 else if (apic_lvtt_tscdeadline(apic))
1536 start_sw_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001537}
1538EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1539
Eddie Dong97222cc2007-09-12 10:58:04 +03001540static void start_apic_timer(struct kvm_lapic *apic)
1541{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001542 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001543
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001544 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08001545 if (set_target_expiration(apic) &&
1546 !(kvm_x86_ops->set_hv_timer && start_hv_timer(apic)))
1547 start_sw_period(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001548 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li7e810a32016-10-24 18:23:12 +08001549 if (!(kvm_x86_ops->set_hv_timer && start_hv_timer(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001550 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001551 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001552}
1553
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001554static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1555{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001556 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001557
Radim Krčmář59fd1322015-06-30 22:19:16 +02001558 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1559 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1560 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001561 apic_debug("Receive NMI setting on APIC_LVT0 "
1562 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001563 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001564 } else
1565 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1566 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001567}
1568
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001569int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001570{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001571 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001572
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001573 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001574
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001575 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001576 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001577 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001578 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001579 else
1580 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001581 break;
1582
1583 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001584 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001585 apic_set_tpr(apic, val & 0xff);
1586 break;
1587
1588 case APIC_EOI:
1589 apic_set_eoi(apic);
1590 break;
1591
1592 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001593 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001594 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001595 else
1596 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001597 break;
1598
1599 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001600 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001601 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001602 recalculate_apic_map(apic->vcpu->kvm);
1603 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001604 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001605 break;
1606
Gleb Natapovfc61b802009-07-05 17:39:35 +03001607 case APIC_SPIV: {
1608 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001609 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001610 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001611 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001612 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1613 int i;
1614 u32 lvt_val;
1615
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001616 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001617 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001618 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001619 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001620 lvt_val | APIC_LVT_MASKED);
1621 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001622 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001623 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001624
1625 }
1626 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001627 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001628 case APIC_ICR:
1629 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001630 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001631 apic_send_ipi(apic);
1632 break;
1633
1634 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001635 if (!apic_x2apic_mode(apic))
1636 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001637 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001638 break;
1639
Jan Kiszka23930f92008-09-26 09:30:52 +02001640 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001641 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001642 case APIC_LVTTHMR:
1643 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001644 case APIC_LVT1:
1645 case APIC_LVTERR:
1646 /* TODO: Check vector */
Gleb Natapovc48f1492012-08-05 15:58:33 +03001647 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001648 val |= APIC_LVT_MASKED;
1649
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001650 val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001651 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001652
1653 break;
1654
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001655 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001656 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001657 val |= APIC_LVT_MASKED;
1658 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001659 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001660 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001661 break;
1662
Eddie Dong97222cc2007-09-12 10:58:04 +03001663 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001664 if (apic_lvtt_tscdeadline(apic))
1665 break;
1666
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001667 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001668 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001669 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001670 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001671
1672 case APIC_TDCR:
1673 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001674 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001675 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001676 update_divide_count(apic);
1677 break;
1678
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001679 case APIC_ESR:
1680 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001681 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001682 ret = 1;
1683 }
1684 break;
1685
1686 case APIC_SELF_IPI:
1687 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001688 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001689 } else
1690 ret = 1;
1691 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001692 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001693 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001694 break;
1695 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001696 if (ret)
1697 apic_debug("Local APIC Write to read-only register %x\n", reg);
1698 return ret;
1699}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001700EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001701
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001702static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001703 gpa_t address, int len, const void *data)
1704{
1705 struct kvm_lapic *apic = to_lapic(this);
1706 unsigned int offset = address - apic->base_address;
1707 u32 val;
1708
1709 if (!apic_mmio_in_range(apic, address))
1710 return -EOPNOTSUPP;
1711
1712 /*
1713 * APIC register must be aligned on 128-bits boundary.
1714 * 32/64/128 bits registers must be accessed thru 32 bits.
1715 * Refer SDM 8.4.1
1716 */
1717 if (len != 4 || (offset & 0xf)) {
1718 /* Don't shout loud, $infamous_os would cause only noise. */
1719 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001720 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001721 }
1722
1723 val = *(u32*)data;
1724
1725 /* too common printing */
1726 if (offset != APIC_EOI)
1727 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1728 "0x%x\n", __func__, offset, len, val);
1729
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001730 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001731
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001732 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001733}
1734
Kevin Tian58fbbf22011-08-30 13:56:17 +03001735void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1736{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001737 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001738}
1739EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1740
Yang Zhang83d4c282013-01-25 10:18:49 +08001741/* emulate APIC access in a trap manner */
1742void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1743{
1744 u32 val = 0;
1745
1746 /* hw has done the conditional check and inst decode */
1747 offset &= 0xff0;
1748
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001749 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001750
1751 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001752 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001753}
1754EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1755
Rusty Russelld5894442007-10-08 10:48:30 +10001756void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001757{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001758 struct kvm_lapic *apic = vcpu->arch.apic;
1759
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001760 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001761 return;
1762
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001763 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001764
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001765 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1766 static_key_slow_dec_deferred(&apic_hw_disabled);
1767
Radim Krčmáře4627552014-10-30 15:06:45 +01001768 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001769 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001770
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001771 if (apic->regs)
1772 free_page((unsigned long)apic->regs);
1773
1774 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001775}
1776
1777/*
1778 *----------------------------------------------------------------------
1779 * LAPIC interface
1780 *----------------------------------------------------------------------
1781 */
Wanpeng Li498f8162016-10-24 18:23:11 +08001782u64 kvm_get_lapic_target_expiration_tsc(struct kvm_vcpu *vcpu)
1783{
1784 struct kvm_lapic *apic = vcpu->arch.apic;
1785
1786 if (!lapic_in_kernel(vcpu))
1787 return 0;
1788
1789 return apic->lapic_timer.tscdeadline;
1790}
Eddie Dong97222cc2007-09-12 10:58:04 +03001791
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001792u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1793{
1794 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001795
Wanpeng Lia10388e2016-10-24 18:23:10 +08001796 if (!lapic_in_kernel(vcpu) ||
1797 !apic_lvtt_tscdeadline(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001798 return 0;
1799
1800 return apic->lapic_timer.tscdeadline;
1801}
1802
1803void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1804{
1805 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001806
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001807 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001808 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001809 return;
1810
1811 hrtimer_cancel(&apic->lapic_timer.timer);
1812 apic->lapic_timer.tscdeadline = data;
1813 start_apic_timer(apic);
1814}
1815
Eddie Dong97222cc2007-09-12 10:58:04 +03001816void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1817{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001818 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001819
Avi Kivityb93463a2007-10-25 16:52:32 +02001820 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001821 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001822}
1823
1824u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1825{
Eddie Dong97222cc2007-09-12 10:58:04 +03001826 u64 tpr;
1827
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001828 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001829
1830 return (tpr & 0xf0) >> 4;
1831}
1832
1833void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1834{
Yang Zhang8d146952013-01-25 10:18:50 +08001835 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001836 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001837
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08001838 if (!apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001839 value |= MSR_IA32_APICBASE_BSP;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001840
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001841 vcpu->arch.apic_base = value;
1842
Jim Mattsonc7dd15b2016-11-09 09:50:11 -08001843 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
1844 kvm_update_cpuid(vcpu);
1845
1846 if (!apic)
1847 return;
1848
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001849 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001850 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001851 if (value & MSR_IA32_APICBASE_ENABLE) {
1852 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001853 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08001854 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001855 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08001856 recalculate_apic_map(vcpu->kvm);
1857 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001858 }
1859
Yang Zhang8d146952013-01-25 10:18:50 +08001860 if ((old_value ^ value) & X2APIC_ENABLE) {
1861 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001862 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001863 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1864 } else
1865 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001866 }
Yang Zhang8d146952013-01-25 10:18:50 +08001867
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001868 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001869 MSR_IA32_APICBASE_BASE;
1870
Nadav Amitdb324fe2014-11-02 11:54:59 +02001871 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1872 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1873 pr_warn_once("APIC base relocation is unsupported by KVM");
1874
Eddie Dong97222cc2007-09-12 10:58:04 +03001875 /* with FSB delivery interrupt, we can restart APIC functionality */
1876 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001877 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001878
1879}
1880
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001881void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001882{
1883 struct kvm_lapic *apic;
1884 int i;
1885
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001886 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001887
1888 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001889 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001890 ASSERT(apic != NULL);
1891
1892 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001893 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001894
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001895 if (!init_event) {
1896 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1897 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001898 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001899 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001900 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001901
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001902 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1903 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001904 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001905 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001906 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001907 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001908 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001909
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001910 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001911 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001912 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001913 if (!apic_x2apic_mode(apic))
1914 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001915 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1916 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1917 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1918 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1919 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001920 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001921 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1922 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1923 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001924 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001925 apic->irr_pending = vcpu->arch.apicv_active;
1926 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001927 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001928 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001929 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001930 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001931 kvm_lapic_set_base(vcpu,
1932 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001933 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001934 apic_update_ppr(apic);
1935
Gleb Natapove1035712009-03-05 16:34:59 +02001936 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001937 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001938
Radim Krčmář6e500432016-12-15 18:06:46 +01001939 apic_debug("%s: vcpu=%p, id=0x%x, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001940 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Radim Krčmář6e500432016-12-15 18:06:46 +01001941 vcpu, kvm_lapic_get_reg(apic, APIC_ID),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001942 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001943}
1944
Eddie Dong97222cc2007-09-12 10:58:04 +03001945/*
1946 *----------------------------------------------------------------------
1947 * timer interface
1948 *----------------------------------------------------------------------
1949 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001950
Avi Kivity2a6eac92012-07-26 18:01:51 +03001951static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001952{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001953 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001954}
1955
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001956int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1957{
Gleb Natapov54e98182012-08-05 15:58:32 +03001958 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001959
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001960 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001961 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001962
1963 return 0;
1964}
1965
Avi Kivity89342082011-11-10 14:57:21 +02001966int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001967{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001968 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001969 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001970
Gleb Natapovc48f1492012-08-05 15:58:33 +03001971 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001972 vector = reg & APIC_VECTOR_MASK;
1973 mode = reg & APIC_MODE_MASK;
1974 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001975 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1976 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001977 }
1978 return 0;
1979}
1980
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001981void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001982{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001983 struct kvm_lapic *apic = vcpu->arch.apic;
1984
1985 if (apic)
1986 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001987}
1988
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001989static const struct kvm_io_device_ops apic_mmio_ops = {
1990 .read = apic_mmio_read,
1991 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001992};
1993
Avi Kivitye9d90d42012-07-26 18:01:50 +03001994static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1995{
1996 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001997 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001998
Radim Krčmář5d87db72014-10-10 19:15:08 +02001999 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002000
Avi Kivity2a6eac92012-07-26 18:01:51 +03002001 if (lapic_is_periodic(apic)) {
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002002 advance_periodic_target_expiration(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002003 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2004 return HRTIMER_RESTART;
2005 } else
2006 return HRTIMER_NORESTART;
2007}
2008
Eddie Dong97222cc2007-09-12 10:58:04 +03002009int kvm_create_lapic(struct kvm_vcpu *vcpu)
2010{
2011 struct kvm_lapic *apic;
2012
2013 ASSERT(vcpu != NULL);
2014 apic_debug("apic_init %d\n", vcpu->vcpu_id);
2015
2016 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
2017 if (!apic)
2018 goto nomem;
2019
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002020 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002021
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002022 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
2023 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03002024 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2025 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10002026 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002027 }
Eddie Dong97222cc2007-09-12 10:58:04 +03002028 apic->vcpu = vcpu;
2029
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002030 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002031 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03002032 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002033
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002034 /*
2035 * APIC is created enabled. This will prevent kvm_lapic_set_base from
2036 * thinking that APIC satet has changed.
2037 */
2038 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002039 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002040 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002041 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03002042
2043 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10002044nomem_free_apic:
2045 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002046nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03002047 return -ENOMEM;
2048}
Eddie Dong97222cc2007-09-12 10:58:04 +03002049
2050int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
2051{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002053 int highest_irr;
2054
Paolo Bonzinif8543d62016-01-08 13:42:24 +01002055 if (!apic_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002056 return -1;
2057
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002058 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002059 highest_irr = apic_find_highest_irr(apic);
2060 if ((highest_irr == -1) ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002061 ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03002062 return -1;
2063 return highest_irr;
2064}
2065
Qing He40487c62007-09-17 14:47:13 +08002066int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2067{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002068 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002069 int r = 0;
2070
Gleb Natapovc48f1492012-08-05 15:58:33 +03002071 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002072 r = 1;
2073 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2074 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2075 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002076 return r;
2077}
2078
Eddie Dong1b9778d2007-09-03 16:56:58 +03002079void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2080{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002081 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002082
Gleb Natapov54e98182012-08-05 15:58:32 +03002083 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002084 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002085 if (apic_lvtt_tscdeadline(apic))
2086 apic->lapic_timer.tscdeadline = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002087 if (apic_lvtt_oneshot(apic)) {
2088 apic->lapic_timer.tscdeadline = 0;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002089 apic->lapic_timer.target_expiration = 0;
Wanpeng Li8003c9a2016-10-24 18:23:13 +08002090 }
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002091 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002092 }
2093}
2094
Eddie Dong97222cc2007-09-12 10:58:04 +03002095int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2096{
2097 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002098 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002099
2100 if (vector == -1)
2101 return -1;
2102
Wanpeng Li56cc2402014-08-05 12:42:24 +08002103 /*
2104 * We get here even with APIC virtualization enabled, if doing
2105 * nested virtualization and L1 runs with the "acknowledge interrupt
2106 * on exit" mode. Then we cannot inject the interrupt via RVI,
2107 * because the process would deliver it through the IDT.
2108 */
2109
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002110 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002111 apic_update_ppr(apic);
2112 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002113
2114 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
2115 apic_clear_isr(vector, apic);
2116 apic_update_ppr(apic);
2117 }
2118
Eddie Dong97222cc2007-09-12 10:58:04 +03002119 return vector;
2120}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002121
Radim Krčmářa92e2542016-07-12 22:09:22 +02002122static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2123 struct kvm_lapic_state *s, bool set)
2124{
2125 if (apic_x2apic_mode(vcpu->arch.apic)) {
2126 u32 *id = (u32 *)(s->regs + APIC_ID);
2127
Radim Krčmář371313132016-07-12 22:09:27 +02002128 if (vcpu->kvm->arch.x2apic_format) {
2129 if (*id != vcpu->vcpu_id)
2130 return -EINVAL;
2131 } else {
2132 if (set)
2133 *id >>= 24;
2134 else
2135 *id <<= 24;
2136 }
Radim Krčmářa92e2542016-07-12 22:09:22 +02002137 }
2138
2139 return 0;
2140}
2141
2142int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2143{
2144 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2145 return kvm_apic_state_fixup(vcpu, s, false);
2146}
2147
2148int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002149{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002150 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002151 int r;
2152
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002153
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002154 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002155 /* set SPIV separately to get count of SW disabled APICs right */
2156 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002157
2158 r = kvm_apic_state_fixup(vcpu, s, true);
2159 if (r)
2160 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002161 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002162
2163 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002164 kvm_apic_set_version(vcpu);
2165
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002166 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002167 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002168 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002169 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002170 update_divide_count(apic);
2171 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002172 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002173 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002174 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002175 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002176 if (vcpu->arch.apicv_active) {
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05002177 if (kvm_x86_ops->apicv_post_state_restore)
2178 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002179 kvm_x86_ops->hwapic_irr_update(vcpu,
2180 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002181 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002182 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002183 }
Avi Kivity3842d132010-07-27 12:30:24 +03002184 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002185 if (ioapic_in_kernel(vcpu->kvm))
2186 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002187
2188 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002189
2190 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002191}
Eddie Donga3d7f852007-09-03 16:15:12 +03002192
Avi Kivity2f52d582008-01-16 12:49:30 +02002193void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002194{
Eddie Donga3d7f852007-09-03 16:15:12 +03002195 struct hrtimer *timer;
2196
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002197 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002198 return;
2199
Gleb Natapov54e98182012-08-05 15:58:32 +03002200 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002201 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002202 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002203}
Avi Kivityb93463a2007-10-25 16:52:32 +02002204
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002205/*
2206 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2207 *
2208 * Detect whether guest triggered PV EOI since the
2209 * last entry. If yes, set EOI on guests's behalf.
2210 * Clear PV EOI in guest memory in any case.
2211 */
2212static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2213 struct kvm_lapic *apic)
2214{
2215 bool pending;
2216 int vector;
2217 /*
2218 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2219 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2220 *
2221 * KVM_APIC_PV_EOI_PENDING is unset:
2222 * -> host disabled PV EOI.
2223 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2224 * -> host enabled PV EOI, guest did not execute EOI yet.
2225 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2226 * -> host enabled PV EOI, guest executed EOI.
2227 */
2228 BUG_ON(!pv_eoi_enabled(vcpu));
2229 pending = pv_eoi_get_pending(vcpu);
2230 /*
2231 * Clear pending bit in any case: it will be set again on vmentry.
2232 * While this might not be ideal from performance point of view,
2233 * this makes sure pv eoi is only enabled when we know it's safe.
2234 */
2235 pv_eoi_clr_pending(vcpu);
2236 if (pending)
2237 return;
2238 vector = apic_set_eoi(apic);
2239 trace_kvm_pv_eoi(apic, vector);
2240}
2241
Avi Kivityb93463a2007-10-25 16:52:32 +02002242void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2243{
2244 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002245
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002246 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2247 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2248
Gleb Natapov41383772012-04-19 14:06:29 +03002249 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002250 return;
2251
Nicholas Krause603242a2015-08-05 10:44:40 -04002252 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2253 sizeof(u32)))
2254 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002255
2256 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2257}
2258
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002259/*
2260 * apic_sync_pv_eoi_to_guest - called before vmentry
2261 *
2262 * Detect whether it's safe to enable PV EOI and
2263 * if yes do so.
2264 */
2265static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2266 struct kvm_lapic *apic)
2267{
2268 if (!pv_eoi_enabled(vcpu) ||
2269 /* IRR set or many bits in ISR: could be nested. */
2270 apic->irr_pending ||
2271 /* Cache not set: could be safe but we don't bother. */
2272 apic->highest_isr_cache == -1 ||
2273 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002274 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002275 /*
2276 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2277 * so we need not do anything here.
2278 */
2279 return;
2280 }
2281
2282 pv_eoi_set_pending(apic->vcpu);
2283}
2284
Avi Kivityb93463a2007-10-25 16:52:32 +02002285void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2286{
2287 u32 data, tpr;
2288 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002289 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002290
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002291 apic_sync_pv_eoi_to_guest(vcpu, apic);
2292
Gleb Natapov41383772012-04-19 14:06:29 +03002293 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002294 return;
2295
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002296 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002297 max_irr = apic_find_highest_irr(apic);
2298 if (max_irr < 0)
2299 max_irr = 0;
2300 max_isr = apic_find_highest_isr(apic);
2301 if (max_isr < 0)
2302 max_isr = 0;
2303 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2304
Andy Honigfda4e2e2013-11-20 10:23:22 -08002305 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2306 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002307}
2308
Andy Honigfda4e2e2013-11-20 10:23:22 -08002309int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002310{
Andy Honigfda4e2e2013-11-20 10:23:22 -08002311 if (vapic_addr) {
2312 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2313 &vcpu->arch.apic->vapic_cache,
2314 vapic_addr, sizeof(u32)))
2315 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002316 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002317 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002318 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e2013-11-20 10:23:22 -08002319 }
2320
2321 vcpu->arch.apic->vapic_addr = vapic_addr;
2322 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002323}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002324
2325int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2326{
2327 struct kvm_lapic *apic = vcpu->arch.apic;
2328 u32 reg = (msr - APIC_BASE_MSR) << 4;
2329
Paolo Bonzini35754c92015-07-29 12:05:37 +02002330 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002331 return 1;
2332
Nadav Amitc69d3d92014-11-26 17:56:25 +02002333 if (reg == APIC_ICR2)
2334 return 1;
2335
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002336 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002337 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002338 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2339 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002340}
2341
2342int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2343{
2344 struct kvm_lapic *apic = vcpu->arch.apic;
2345 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2346
Paolo Bonzini35754c92015-07-29 12:05:37 +02002347 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002348 return 1;
2349
Nadav Amitc69d3d92014-11-26 17:56:25 +02002350 if (reg == APIC_DFR || reg == APIC_ICR2) {
2351 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2352 reg);
2353 return 1;
2354 }
2355
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002356 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002357 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002358 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002359 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002360
2361 *data = (((u64)high) << 32) | low;
2362
2363 return 0;
2364}
Gleb Natapov10388a02010-01-17 15:51:23 +02002365
2366int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2367{
2368 struct kvm_lapic *apic = vcpu->arch.apic;
2369
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002370 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002371 return 1;
2372
2373 /* if this is ICR write vector before command */
2374 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002375 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2376 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002377}
2378
2379int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2380{
2381 struct kvm_lapic *apic = vcpu->arch.apic;
2382 u32 low, high = 0;
2383
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002384 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002385 return 1;
2386
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002387 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002388 return 1;
2389 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002390 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002391
2392 *data = (((u64)high) << 32) | low;
2393
2394 return 0;
2395}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002396
2397int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2398{
2399 u64 addr = data & ~KVM_MSR_ENABLED;
2400 if (!IS_ALIGNED(addr, 4))
2401 return 1;
2402
2403 vcpu->arch.pv_eoi.msr_val = data;
2404 if (!pv_eoi_enabled(vcpu))
2405 return 0;
2406 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002407 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002408}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002409
Jan Kiszka66450a22013-03-13 12:42:34 +01002410void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2411{
2412 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002413 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002414 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002415
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002416 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002417 return;
2418
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002419 /*
2420 * INITs are latched while in SMM. Because an SMM CPU cannot
2421 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2422 * and delay processing of INIT until the next RSM.
2423 */
2424 if (is_smm(vcpu)) {
2425 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2426 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2427 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2428 return;
2429 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002430
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002431 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002432 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002433 kvm_lapic_reset(vcpu, true);
2434 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002435 if (kvm_vcpu_is_bsp(apic->vcpu))
2436 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2437 else
2438 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2439 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002440 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002441 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2442 /* evaluate pending_events before reading the vector */
2443 smp_rmb();
2444 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002445 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002446 vcpu->vcpu_id, sipi_vector);
2447 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2448 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2449 }
2450}
2451
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002452void kvm_lapic_init(void)
2453{
2454 /* do not patch jump label more than once per second */
2455 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002456 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002457}