blob: 3988e26af3b588aed1a19aae7c20158ce67ffa7a [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>
Marios Pomonis19fe4b12019-12-11 12:47:46 -080031#include <linux/nospec.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030032#include <asm/processor.h>
33#include <asm/msr.h>
34#include <asm/page.h>
35#include <asm/current.h>
36#include <asm/apicdef.h>
Marcelo Tosattid0659d92014-12-16 09:08:15 -050037#include <asm/delay.h>
Arun Sharma600634972011-07-26 16:09:06 -070038#include <linux/atomic.h>
Gleb Natapovc5cc4212012-08-05 15:58:30 +030039#include <linux/jump_label.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030040#include "kvm_cache_regs.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030041#include "irq.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030042#include "trace.h"
Gleb Natapovfc61b802009-07-05 17:39:35 +030043#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020044#include "cpuid.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030045#include "hyperv.h"
Eddie Dong97222cc2007-09-12 10:58:04 +030046
Marcelo Tosattib682b812009-02-10 20:41:41 -020047#ifndef CONFIG_X86_64
48#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
49#else
50#define mod_64(x, y) ((x) % (y))
51#endif
52
Eddie Dong97222cc2007-09-12 10:58:04 +030053#define PRId64 "d"
54#define PRIx64 "llx"
55#define PRIu64 "u"
56#define PRIo64 "o"
57
58#define APIC_BUS_CYCLE_NS 1
59
60/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
Yi Wangeadd5352018-11-08 16:48:36 +080061#define apic_debug(fmt, arg...) do {} while (0)
Eddie Dong97222cc2007-09-12 10:58:04 +030062
Eddie Dong97222cc2007-09-12 10:58:04 +030063/* 14 is the version for Xeon and Pentium 8.4.8*/
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -050064#define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
Eddie Dong97222cc2007-09-12 10:58:04 +030065#define LAPIC_MMIO_LENGTH (1 << 12)
66/* followed define is not in apicdef.h */
67#define APIC_SHORT_MASK 0xc0000
68#define APIC_DEST_NOSHORT 0x0
69#define APIC_DEST_MASK 0x800
70#define MAX_APIC_VECTOR 256
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +090071#define APIC_VECTORS_PER_REG 32
Eddie Dong97222cc2007-09-12 10:58:04 +030072
Nadav Amit394457a2014-10-03 00:30:52 +030073#define APIC_BROADCAST 0xFF
74#define X2APIC_BROADCAST 0xFFFFFFFFul
75
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +030076static inline int apic_test_vector(int vec, void *bitmap)
77{
78 return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
79}
80
Yang Zhang10606912013-04-11 19:21:38 +080081bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
82{
83 struct kvm_lapic *apic = vcpu->arch.apic;
84
85 return apic_test_vector(vector, apic->regs + APIC_ISR) ||
86 apic_test_vector(vector, apic->regs + APIC_IRR);
87}
88
Eddie Dong97222cc2007-09-12 10:58:04 +030089static inline void apic_clear_vector(int vec, void *bitmap)
90{
91 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
92}
93
Michael S. Tsirkin8680b942012-06-24 19:24:26 +030094static inline int __apic_test_and_set_vector(int vec, void *bitmap)
95{
96 return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
97}
98
99static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
100{
101 return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
102}
103
Gleb Natapovc5cc4212012-08-05 15:58:30 +0300104struct static_key_deferred apic_hw_disabled __read_mostly;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +0300105struct static_key_deferred apic_sw_disabled __read_mostly;
106
Eddie Dong97222cc2007-09-12 10:58:04 +0300107static inline int apic_enabled(struct kvm_lapic *apic)
108{
Gleb Natapovc48f1492012-08-05 15:58:33 +0300109 return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
Gleb Natapov54e98182012-08-05 15:58:32 +0300110}
111
Eddie Dong97222cc2007-09-12 10:58:04 +0300112#define LVT_MASK \
113 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
114
115#define LINT_MASK \
116 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
117 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
118
Radim Krčmáře45115b2016-07-12 22:09:19 +0200119static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
120 u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
121 switch (map->mode) {
122 case KVM_APIC_MODE_X2APIC: {
123 u32 offset = (dest_id >> 16) * 16;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200124 u32 max_apic_id = map->max_apic_id;
Radim Krčmář3548a252015-02-12 19:41:33 +0100125
Radim Krčmáře45115b2016-07-12 22:09:19 +0200126 if (offset <= max_apic_id) {
127 u8 cluster_size = min(max_apic_id - offset + 1, 16U);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100128
Radim Krčmáře45115b2016-07-12 22:09:19 +0200129 *cluster = &map->phys_map[offset];
130 *mask = dest_id & (0xffff >> (16 - cluster_size));
131 } else {
132 *mask = 0;
133 }
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100134
Radim Krčmáře45115b2016-07-12 22:09:19 +0200135 return true;
136 }
137 case KVM_APIC_MODE_XAPIC_FLAT:
138 *cluster = map->xapic_flat_map;
139 *mask = dest_id & 0xff;
140 return true;
141 case KVM_APIC_MODE_XAPIC_CLUSTER:
Radim Krčmář444fdad2016-11-22 20:20:14 +0100142 *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
Radim Krčmáře45115b2016-07-12 22:09:19 +0200143 *mask = dest_id & 0xf;
144 return true;
145 default:
146 /* Not optimized. */
147 return false;
148 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300149}
150
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200151static void kvm_apic_map_free(struct rcu_head *rcu)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100152{
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200153 struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100154
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200155 kvfree(map);
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100156}
157
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300158static void recalculate_apic_map(struct kvm *kvm)
159{
160 struct kvm_apic_map *new, *old = NULL;
161 struct kvm_vcpu *vcpu;
162 int i;
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200163 u32 max_id = 255;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300164
165 mutex_lock(&kvm->arch.apic_map_lock);
166
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200167 kvm_for_each_vcpu(i, vcpu, kvm)
168 if (kvm_apic_present(vcpu))
169 max_id = max(max_id, kvm_apic_id(vcpu->arch.apic));
170
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200171 new = kvm_kvzalloc(sizeof(struct kvm_apic_map) +
172 sizeof(struct kvm_lapic *) * ((u64)max_id + 1));
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200173
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300174 if (!new)
175 goto out;
176
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200177 new->max_apic_id = max_id;
178
Nadav Amit173beed2014-11-02 11:54:54 +0200179 kvm_for_each_vcpu(i, vcpu, kvm) {
180 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmáře45115b2016-07-12 22:09:19 +0200181 struct kvm_lapic **cluster;
182 u16 mask;
Radim Krčmář25995e52014-11-27 23:30:19 +0100183 u32 ldr, aid;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300184
Radim Krčmářdf04d1d2015-01-29 22:33:35 +0100185 if (!kvm_apic_present(vcpu))
186 continue;
187
Radim Krčmář25995e52014-11-27 23:30:19 +0100188 aid = kvm_apic_id(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500189 ldr = kvm_lapic_get_reg(apic, APIC_LDR);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300190
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200191 if (aid <= new->max_apic_id)
Radim Krčmář25995e52014-11-27 23:30:19 +0100192 new->phys_map[aid] = apic;
Radim Krčmář3548a252015-02-12 19:41:33 +0100193
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100194 if (apic_x2apic_mode(apic)) {
195 new->mode |= KVM_APIC_MODE_X2APIC;
196 } else if (ldr) {
197 ldr = GET_APIC_LOGICAL_ID(ldr);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500198 if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
Radim Krčmář3b5a5ff2015-02-12 19:41:34 +0100199 new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
200 else
201 new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
202 }
203
Radim Krčmáře45115b2016-07-12 22:09:19 +0200204 if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
Radim Krčmář3548a252015-02-12 19:41:33 +0100205 continue;
206
Radim Krčmáře45115b2016-07-12 22:09:19 +0200207 if (mask)
208 cluster[ffs(mask) - 1] = apic;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300209 }
210out:
211 old = rcu_dereference_protected(kvm->arch.apic_map,
212 lockdep_is_held(&kvm->arch.apic_map_lock));
213 rcu_assign_pointer(kvm->arch.apic_map, new);
214 mutex_unlock(&kvm->arch.apic_map_lock);
215
216 if (old)
Radim Krčmářaf1bae52016-07-12 22:09:30 +0200217 call_rcu(&old->rcu, kvm_apic_map_free);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800218
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700219 kvm_make_scan_ioapic_request(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300220}
221
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300222static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
223{
Radim Krčmáře4627552014-10-30 15:06:45 +0100224 bool enabled = val & APIC_SPIV_APIC_ENABLED;
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300225
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500226 kvm_lapic_set_reg(apic, APIC_SPIV, val);
Radim Krčmáře4627552014-10-30 15:06:45 +0100227
228 if (enabled != apic->sw_enabled) {
229 apic->sw_enabled = enabled;
230 if (enabled) {
Nadav Amit1e1b6c22014-08-19 00:03:00 +0300231 static_key_slow_dec_deferred(&apic_sw_disabled);
232 recalculate_apic_map(apic->vcpu->kvm);
233 } else
234 static_key_slow_inc(&apic_sw_disabled.key);
235 }
236}
237
Radim Krčmářa92e2542016-07-12 22:09:22 +0200238static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300239{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500240 kvm_lapic_set_reg(apic, APIC_ID, id << 24);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300241 recalculate_apic_map(apic->vcpu->kvm);
242}
243
244static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
245{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500246 kvm_lapic_set_reg(apic, APIC_LDR, id);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300247 recalculate_apic_map(apic->vcpu->kvm);
248}
249
Dr. David Alan Gilberte02746e22017-11-17 11:52:49 +0000250static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
251{
252 return ((id >> 4) << 16) | (1 << (id & 0xf));
253}
254
Radim Krčmářa92e2542016-07-12 22:09:22 +0200255static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Radim Krčmář257b9a52015-05-22 18:45:11 +0200256{
Dr. David Alan Gilberte02746e22017-11-17 11:52:49 +0000257 u32 ldr = kvm_apic_calc_x2apic_ldr(id);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200258
Radim Krčmářa92e2542016-07-12 22:09:22 +0200259 kvm_lapic_set_reg(apic, APIC_ID, id);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500260 kvm_lapic_set_reg(apic, APIC_LDR, ldr);
Radim Krčmář257b9a52015-05-22 18:45:11 +0200261 recalculate_apic_map(apic->vcpu->kvm);
262}
263
Eddie Dong97222cc2007-09-12 10:58:04 +0300264static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
265{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500266 return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
Eddie Dong97222cc2007-09-12 10:58:04 +0300267}
268
269static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
270{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500271 return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
Eddie Dong97222cc2007-09-12 10:58:04 +0300272}
273
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800274static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
275{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100276 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800277}
278
Eddie Dong97222cc2007-09-12 10:58:04 +0300279static inline int apic_lvtt_period(struct kvm_lapic *apic)
280{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100281 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800282}
283
284static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
285{
Radim Krčmářf30ebc32014-10-30 15:06:47 +0100286 return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
Eddie Dong97222cc2007-09-12 10:58:04 +0300287}
288
Jan Kiszkacc6e4622008-10-20 10:20:03 +0200289static inline int apic_lvt_nmi_mode(u32 lvt_val)
290{
291 return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
292}
293
Gleb Natapovfc61b802009-07-05 17:39:35 +0300294void kvm_apic_set_version(struct kvm_vcpu *vcpu)
295{
296 struct kvm_lapic *apic = vcpu->arch.apic;
297 struct kvm_cpuid_entry2 *feat;
298 u32 v = APIC_VERSION;
299
Paolo Bonzinibce87cc2016-01-08 13:48:51 +0100300 if (!lapic_in_kernel(vcpu))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300301 return;
302
Vitaly Kuznetsove1d32f92018-02-09 14:01:33 +0100303 /*
304 * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
305 * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
306 * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
307 * version first and level-triggered interrupts never get EOIed in
308 * IOAPIC.
309 */
Gleb Natapovfc61b802009-07-05 17:39:35 +0300310 feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
Vitaly Kuznetsove1d32f92018-02-09 14:01:33 +0100311 if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) &&
312 !ioapic_in_kernel(vcpu->kvm))
Gleb Natapovfc61b802009-07-05 17:39:35 +0300313 v |= APIC_LVR_DIRECTED_EOI;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500314 kvm_lapic_set_reg(apic, APIC_LVR, v);
Gleb Natapovfc61b802009-07-05 17:39:35 +0300315}
316
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500317static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800318 LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
Eddie Dong97222cc2007-09-12 10:58:04 +0300319 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
320 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
321 LINT_MASK, LINT_MASK, /* LVT0-1 */
322 LVT_MASK /* LVTERR */
323};
324
325static int find_highest_vector(void *bitmap)
326{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900327 int vec;
328 u32 *reg;
Eddie Dong97222cc2007-09-12 10:58:04 +0300329
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900330 for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
331 vec >= 0; vec -= APIC_VECTORS_PER_REG) {
332 reg = bitmap + REG_POS(vec);
333 if (*reg)
334 return fls(*reg) - 1 + vec;
335 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300336
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900337 return -1;
Eddie Dong97222cc2007-09-12 10:58:04 +0300338}
339
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300340static u8 count_vectors(void *bitmap)
341{
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900342 int vec;
343 u32 *reg;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300344 u8 count = 0;
Takuya Yoshikawaecba9a52012-09-05 19:30:01 +0900345
346 for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
347 reg = bitmap + REG_POS(vec);
348 count += hweight32(*reg);
349 }
350
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300351 return count;
352}
353
Wincy Van705699a2015-02-03 23:58:17 +0800354void __kvm_apic_update_irr(u32 *pir, void *regs)
Yang Zhanga20ed542013-04-11 19:25:15 +0800355{
356 u32 i, pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800357
358 for (i = 0; i <= 7; i++) {
359 pir_val = xchg(&pir[i], 0);
360 if (pir_val)
Wincy Van705699a2015-02-03 23:58:17 +0800361 *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
Yang Zhanga20ed542013-04-11 19:25:15 +0800362 }
363}
Wincy Van705699a2015-02-03 23:58:17 +0800364EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
365
366void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
367{
368 struct kvm_lapic *apic = vcpu->arch.apic;
369
370 __kvm_apic_update_irr(pir, apic->regs);
Radim Krčmářc77f3fa2015-10-08 20:23:33 +0200371
372 kvm_make_request(KVM_REQ_EVENT, vcpu);
Wincy Van705699a2015-02-03 23:58:17 +0800373}
Yang Zhanga20ed542013-04-11 19:25:15 +0800374EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
375
Gleb Natapov33e4c682009-06-11 11:06:51 +0300376static inline int apic_search_irr(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300377{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300378 return find_highest_vector(apic->regs + APIC_IRR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300379}
380
381static inline int apic_find_highest_irr(struct kvm_lapic *apic)
382{
383 int result;
384
Yang Zhangc7c9c562013-01-25 10:18:51 +0800385 /*
386 * Note that irr_pending is just a hint. It will be always
387 * true with virtual interrupt delivery enabled.
388 */
Gleb Natapov33e4c682009-06-11 11:06:51 +0300389 if (!apic->irr_pending)
390 return -1;
391
Andrey Smetanind62caab2015-11-10 15:36:33 +0300392 if (apic->vcpu->arch.apicv_active)
393 kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
Gleb Natapov33e4c682009-06-11 11:06:51 +0300394 result = apic_search_irr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300395 ASSERT(result == -1 || result >= 16);
396
397 return result;
398}
399
Gleb Natapov33e4c682009-06-11 11:06:51 +0300400static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
401{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800402 struct kvm_vcpu *vcpu;
403
404 vcpu = apic->vcpu;
405
Andrey Smetanind62caab2015-11-10 15:36:33 +0300406 if (unlikely(vcpu->arch.apicv_active)) {
Wanpeng Li56cc2402014-08-05 12:42:24 +0800407 /* try to update RVI */
Nadav Amitf210f752014-11-16 23:49:07 +0200408 apic_clear_vector(vec, apic->regs + APIC_IRR);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800409 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Amitf210f752014-11-16 23:49:07 +0200410 } else {
411 apic->irr_pending = false;
412 apic_clear_vector(vec, apic->regs + APIC_IRR);
413 if (apic_search_irr(apic) != -1)
414 apic->irr_pending = true;
Wanpeng Li56cc2402014-08-05 12:42:24 +0800415 }
Gleb Natapov33e4c682009-06-11 11:06:51 +0300416}
417
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300418static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
419{
Wanpeng Li56cc2402014-08-05 12:42:24 +0800420 struct kvm_vcpu *vcpu;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200421
Wanpeng Li56cc2402014-08-05 12:42:24 +0800422 if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
423 return;
424
425 vcpu = apic->vcpu;
426
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300427 /*
Wanpeng Li56cc2402014-08-05 12:42:24 +0800428 * With APIC virtualization enabled, all caching is disabled
429 * because the processor can modify ISR under the hood. Instead
430 * just set SVI.
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300431 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300432 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200433 kvm_x86_ops->hwapic_isr_update(vcpu, vec);
Wanpeng Li56cc2402014-08-05 12:42:24 +0800434 else {
435 ++apic->isr_count;
436 BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
437 /*
438 * ISR (in service register) bit is set when injecting an interrupt.
439 * The highest vector is injected. Thus the latest bit set matches
440 * the highest bit in ISR.
441 */
442 apic->highest_isr_cache = vec;
443 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300444}
445
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200446static inline int apic_find_highest_isr(struct kvm_lapic *apic)
447{
448 int result;
449
450 /*
451 * Note that isr_count is always 1, and highest_isr_cache
452 * is always -1, with APIC virtualization enabled.
453 */
454 if (!apic->isr_count)
455 return -1;
456 if (likely(apic->highest_isr_cache != -1))
457 return apic->highest_isr_cache;
458
459 result = find_highest_vector(apic->regs + APIC_ISR);
460 ASSERT(result == -1 || result >= 16);
461
462 return result;
463}
464
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300465static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
466{
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200467 struct kvm_vcpu *vcpu;
468 if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
469 return;
470
471 vcpu = apic->vcpu;
472
473 /*
474 * We do get here for APIC virtualization enabled if the guest
475 * uses the Hyper-V APIC enlightenment. In this case we may need
476 * to trigger a new interrupt delivery by writing the SVI field;
477 * on the other hand isr_count and highest_isr_cache are unused
478 * and must be left alone.
479 */
Andrey Smetanind62caab2015-11-10 15:36:33 +0300480 if (unlikely(vcpu->arch.apicv_active))
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +0200481 kvm_x86_ops->hwapic_isr_update(vcpu,
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200482 apic_find_highest_isr(apic));
483 else {
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300484 --apic->isr_count;
Paolo Bonzinifc57ac22014-05-14 17:40:58 +0200485 BUG_ON(apic->isr_count < 0);
486 apic->highest_isr_cache = -1;
487 }
Michael S. Tsirkin8680b942012-06-24 19:24:26 +0300488}
489
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800490int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
491{
Gleb Natapov33e4c682009-06-11 11:06:51 +0300492 /* This may race with setting of irr in __apic_accept_irq() and
493 * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
494 * will cause vmexit immediately and the value will be recalculated
495 * on the next vmentry.
496 */
Paolo Bonzinif8543d62016-01-08 13:42:24 +0100497 return apic_find_highest_irr(vcpu->arch.apic);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800498}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800499
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200500static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800501 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100502 struct dest_map *dest_map);
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200503
Yang Zhangb4f22252013-04-11 19:21:37 +0800504int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100505 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300506{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800507 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800508
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200509 return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
Yang Zhangb4f22252013-04-11 19:21:37 +0800510 irq->level, irq->trig_mode, dest_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300511}
512
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300513static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
514{
515
516 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
517 sizeof(val));
518}
519
520static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
521{
522
523 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
524 sizeof(*val));
525}
526
527static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
528{
529 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
530}
531
532static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
533{
534 u8 val;
Miaohe Linbd017372020-02-21 22:04:46 +0800535 if (pv_eoi_get_user(vcpu, &val) < 0) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300536 apic_debug("Can't read EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800537 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Miaohe Linbd017372020-02-21 22:04:46 +0800538 return false;
539 }
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300540 return val & 0x1;
541}
542
543static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
544{
545 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
546 apic_debug("Can't set EOI MSR value: 0x%llx\n",
Chen Fan96893972014-01-02 17:14:11 +0800547 (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300548 return;
549 }
550 __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
551}
552
553static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
554{
555 if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
556 apic_debug("Can't clear 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 __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
561}
562
Eddie Dong97222cc2007-09-12 10:58:04 +0300563static void apic_update_ppr(struct kvm_lapic *apic)
564{
Avi Kivity3842d132010-07-27 12:30:24 +0300565 u32 tpr, isrv, ppr, old_ppr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300566 int isr;
567
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500568 old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
569 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +0300570 isr = apic_find_highest_isr(apic);
571 isrv = (isr != -1) ? isr : 0;
572
573 if ((tpr & 0xf0) >= (isrv & 0xf0))
574 ppr = tpr & 0xff;
575 else
576 ppr = isrv & 0xf0;
577
578 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
579 apic, ppr, isr, isrv);
580
Avi Kivity3842d132010-07-27 12:30:24 +0300581 if (old_ppr != ppr) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500582 kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
Avi Kivity83bcacb2010-10-25 15:23:55 +0200583 if (ppr < old_ppr)
584 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +0300585 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300586}
587
588static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
589{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500590 kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300591 apic_update_ppr(apic);
592}
593
Radim Krčmář03d22492015-02-12 19:41:31 +0100594static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300595{
Radim Krčmář03d22492015-02-12 19:41:31 +0100596 if (apic_x2apic_mode(apic))
597 return mda == X2APIC_BROADCAST;
598
599 return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST;
Eddie Dong97222cc2007-09-12 10:58:04 +0300600}
601
Radim Krčmář03d22492015-02-12 19:41:31 +0100602static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Nadav Amit394457a2014-10-03 00:30:52 +0300603{
Radim Krčmář03d22492015-02-12 19:41:31 +0100604 if (kvm_apic_broadcast(apic, mda))
605 return true;
606
607 if (apic_x2apic_mode(apic))
608 return mda == kvm_apic_id(apic);
609
610 return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic));
Nadav Amit394457a2014-10-03 00:30:52 +0300611}
612
Radim Krčmář52c233a2015-01-29 22:48:48 +0100613static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Eddie Dong97222cc2007-09-12 10:58:04 +0300614{
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300615 u32 logical_id;
616
Nadav Amit394457a2014-10-03 00:30:52 +0300617 if (kvm_apic_broadcast(apic, mda))
Radim Krčmář9368b562015-01-29 22:48:49 +0100618 return true;
Nadav Amit394457a2014-10-03 00:30:52 +0300619
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500620 logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
Eddie Dong97222cc2007-09-12 10:58:04 +0300621
Radim Krčmář9368b562015-01-29 22:48:49 +0100622 if (apic_x2apic_mode(apic))
Radim Krčmář8a395362015-01-29 22:48:51 +0100623 return ((logical_id >> 16) == (mda >> 16))
624 && (logical_id & mda & 0xffff) != 0;
Radim Krčmář9368b562015-01-29 22:48:49 +0100625
626 logical_id = GET_APIC_LOGICAL_ID(logical_id);
Radim Krčmář03d22492015-02-12 19:41:31 +0100627 mda = GET_APIC_DEST_FIELD(mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300628
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500629 switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300630 case APIC_DFR_FLAT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100631 return (logical_id & mda) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300632 case APIC_DFR_CLUSTER:
Radim Krčmář9368b562015-01-29 22:48:49 +0100633 return ((logical_id >> 4) == (mda >> 4))
634 && (logical_id & mda & 0xf) != 0;
Eddie Dong97222cc2007-09-12 10:58:04 +0300635 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200636 apic_debug("Bad DFR vcpu %d: %08x\n",
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500637 apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
Radim Krčmář9368b562015-01-29 22:48:49 +0100638 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300639 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300640}
641
Radim Krčmářc5192652016-07-12 22:09:28 +0200642/* The KVM local APIC implementation has two quirks:
643 *
644 * - the xAPIC MDA stores the destination at bits 24-31, while this
645 * is not true of struct kvm_lapic_irq's dest_id field. This is
646 * just a quirk in the API and is not problematic.
647 *
648 * - in-kernel IOAPIC messages have to be delivered directly to
649 * x2APIC, because the kernel does not support interrupt remapping.
650 * In order to support broadcast without interrupt remapping, x2APIC
651 * rewrites the destination of non-IPI messages from APIC_BROADCAST
652 * to X2APIC_BROADCAST.
653 *
654 * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
655 * important when userspace wants to use x2APIC-format MSIs, because
656 * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
Radim Krčmář03d22492015-02-12 19:41:31 +0100657 */
Radim Krčmářc5192652016-07-12 22:09:28 +0200658static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
659 struct kvm_lapic *source, struct kvm_lapic *target)
Radim Krčmář03d22492015-02-12 19:41:31 +0100660{
661 bool ipi = source != NULL;
662 bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
663
Radim Krčmářc5192652016-07-12 22:09:28 +0200664 if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
665 !ipi && dest_id == APIC_BROADCAST && x2apic_mda)
Radim Krčmář03d22492015-02-12 19:41:31 +0100666 return X2APIC_BROADCAST;
667
668 return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
669}
670
Radim Krčmář52c233a2015-01-29 22:48:48 +0100671bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
Nadav Amit394457a2014-10-03 00:30:52 +0300672 int short_hand, unsigned int dest, int dest_mode)
Eddie Dong97222cc2007-09-12 10:58:04 +0300673{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800674 struct kvm_lapic *target = vcpu->arch.apic;
Radim Krčmářc5192652016-07-12 22:09:28 +0200675 u32 mda = kvm_apic_mda(vcpu, dest, source, target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300676
677 apic_debug("target %p, source %p, dest 0x%x, "
Gleb Natapov343f94f2009-03-05 16:34:54 +0200678 "dest_mode 0x%x, short_hand 0x%x\n",
Eddie Dong97222cc2007-09-12 10:58:04 +0300679 target, source, dest, dest_mode, short_hand);
680
Zachary Amsdenbd371392010-06-14 11:42:15 -1000681 ASSERT(target);
Eddie Dong97222cc2007-09-12 10:58:04 +0300682 switch (short_hand) {
683 case APIC_DEST_NOSHORT:
Radim Krčmář3697f302015-01-29 22:48:50 +0100684 if (dest_mode == APIC_DEST_PHYSICAL)
Radim Krčmář03d22492015-02-12 19:41:31 +0100685 return kvm_apic_match_physical_addr(target, mda);
Gleb Natapov343f94f2009-03-05 16:34:54 +0200686 else
Radim Krčmář03d22492015-02-12 19:41:31 +0100687 return kvm_apic_match_logical_addr(target, mda);
Eddie Dong97222cc2007-09-12 10:58:04 +0300688 case APIC_DEST_SELF:
Radim Krčmář9368b562015-01-29 22:48:49 +0100689 return target == source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300690 case APIC_DEST_ALLINC:
Radim Krčmář9368b562015-01-29 22:48:49 +0100691 return true;
Eddie Dong97222cc2007-09-12 10:58:04 +0300692 case APIC_DEST_ALLBUT:
Radim Krčmář9368b562015-01-29 22:48:49 +0100693 return target != source;
Eddie Dong97222cc2007-09-12 10:58:04 +0300694 default:
Jan Kiszka7712de82011-09-12 11:25:51 +0200695 apic_debug("kvm: apic: Bad dest shorthand value %x\n",
696 short_hand);
Radim Krčmář9368b562015-01-29 22:48:49 +0100697 return false;
Eddie Dong97222cc2007-09-12 10:58:04 +0300698 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300699}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500700EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
Eddie Dong97222cc2007-09-12 10:58:04 +0300701
Feng Wu520040142016-01-25 16:53:33 +0800702int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
703 const unsigned long *bitmap, u32 bitmap_size)
704{
705 u32 mod;
706 int i, idx = -1;
707
708 mod = vector % dest_vcpus;
709
710 for (i = 0; i <= mod; i++) {
711 idx = find_next_bit(bitmap, bitmap_size, idx + 1);
712 BUG_ON(idx == bitmap_size);
713 }
714
715 return idx;
716}
717
Radim Krčmář4efd8052016-02-12 15:00:15 +0100718static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
719{
720 if (!kvm->arch.disabled_lapic_found) {
721 kvm->arch.disabled_lapic_found = true;
722 printk(KERN_INFO
723 "Disabled LAPIC found during irq injection\n");
724 }
725}
726
Radim Krčmářc5192652016-07-12 22:09:28 +0200727static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
728 struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
729{
730 if (kvm->arch.x2apic_broadcast_quirk_disabled) {
731 if ((irq->dest_id == APIC_BROADCAST &&
732 map->mode != KVM_APIC_MODE_X2APIC))
733 return true;
734 if (irq->dest_id == X2APIC_BROADCAST)
735 return true;
736 } else {
737 bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
738 if (irq->dest_id == (x2apic_ipi ?
739 X2APIC_BROADCAST : APIC_BROADCAST))
740 return true;
741 }
742
743 return false;
744}
745
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200746/* Return true if the interrupt can be handled by using *bitmap as index mask
747 * for valid destinations in *dst array.
748 * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
749 * Note: we may have zero kvm_lapic destinations when we return true, which
750 * means that the interrupt should be dropped. In this case, *bitmap would be
751 * zero and *dst undefined.
752 */
753static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
754 struct kvm_lapic **src, struct kvm_lapic_irq *irq,
755 struct kvm_apic_map *map, struct kvm_lapic ***dst,
756 unsigned long *bitmap)
757{
758 int i, lowest;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200759
760 if (irq->shorthand == APIC_DEST_SELF && src) {
761 *dst = src;
762 *bitmap = 1;
763 return true;
764 } else if (irq->shorthand)
765 return false;
766
Radim Krčmářc5192652016-07-12 22:09:28 +0200767 if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200768 return false;
769
770 if (irq->dest_mode == APIC_DEST_PHYSICAL) {
Radim Krčmář0ca52e72016-07-12 22:09:20 +0200771 if (irq->dest_id > map->max_apic_id) {
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200772 *bitmap = 0;
773 } else {
774 *dst = &map->phys_map[irq->dest_id];
775 *bitmap = 1;
776 }
777 return true;
778 }
779
Radim Krčmáře45115b2016-07-12 22:09:19 +0200780 *bitmap = 0;
781 if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
782 (u16 *)bitmap))
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200783 return false;
784
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200785 if (!kvm_lowest_prio_delivery(irq))
786 return true;
787
788 if (!kvm_vector_hashing_enabled()) {
789 lowest = -1;
790 for_each_set_bit(i, bitmap, 16) {
791 if (!(*dst)[i])
792 continue;
793 if (lowest < 0)
794 lowest = i;
795 else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
796 (*dst)[lowest]->vcpu) < 0)
797 lowest = i;
798 }
799 } else {
800 if (!*bitmap)
801 return true;
802
803 lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
804 bitmap, 16);
805
806 if (!(*dst)[lowest]) {
807 kvm_apic_disabled_lapic_found(kvm);
808 *bitmap = 0;
809 return true;
810 }
811 }
812
813 *bitmap = (lowest >= 0) ? 1 << lowest : 0;
814
815 return true;
816}
817
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300818bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100819 struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300820{
821 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200822 unsigned long bitmap;
823 struct kvm_lapic **dst = NULL;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300824 int i;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200825 bool ret;
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300826
827 *r = -1;
828
829 if (irq->shorthand == APIC_DEST_SELF) {
Yang Zhangb4f22252013-04-11 19:21:37 +0800830 *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300831 return true;
832 }
833
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300834 rcu_read_lock();
835 map = rcu_dereference(kvm->arch.apic_map);
836
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200837 ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
838 if (ret)
839 for_each_set_bit(i, &bitmap, 16) {
840 if (!dst[i])
841 continue;
842 if (*r < 0)
843 *r = 0;
844 *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
Radim Krčmář3548a252015-02-12 19:41:33 +0100845 }
846
Gleb Natapov1e08ec42012-09-13 17:19:24 +0300847 rcu_read_unlock();
848 return ret;
849}
850
Feng Wu6228a0d2016-01-25 16:53:34 +0800851/*
852 * This routine tries to handler interrupts in posted mode, here is how
853 * it deals with different cases:
854 * - For single-destination interrupts, handle it in posted mode
855 * - Else if vector hashing is enabled and it is a lowest-priority
856 * interrupt, handle it in posted mode and use the following mechanism
857 * to find the destinaiton vCPU.
858 * 1. For lowest-priority interrupts, store all the possible
859 * destination vCPUs in an array.
860 * 2. Use "guest vector % max number of destination vCPUs" to find
861 * the right destination vCPU in the array for the lowest-priority
862 * interrupt.
863 * - Otherwise, use remapped mode to inject the interrupt.
864 */
Feng Wu8feb4a02015-09-18 22:29:47 +0800865bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
866 struct kvm_vcpu **dest_vcpu)
867{
868 struct kvm_apic_map *map;
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200869 unsigned long bitmap;
870 struct kvm_lapic **dst = NULL;
Feng Wu8feb4a02015-09-18 22:29:47 +0800871 bool ret = false;
Feng Wu8feb4a02015-09-18 22:29:47 +0800872
873 if (irq->shorthand)
874 return false;
875
876 rcu_read_lock();
877 map = rcu_dereference(kvm->arch.apic_map);
878
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200879 if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
880 hweight16(bitmap) == 1) {
881 unsigned long i = find_first_bit(&bitmap, 16);
Feng Wu8feb4a02015-09-18 22:29:47 +0800882
Radim Krčmář64aa47b2016-07-12 22:09:18 +0200883 if (dst[i]) {
884 *dest_vcpu = dst[i]->vcpu;
885 ret = true;
Feng Wu8feb4a02015-09-18 22:29:47 +0800886 }
Feng Wu8feb4a02015-09-18 22:29:47 +0800887 }
888
Feng Wu8feb4a02015-09-18 22:29:47 +0800889 rcu_read_unlock();
890 return ret;
891}
892
Eddie Dong97222cc2007-09-12 10:58:04 +0300893/*
894 * Add a pending IRQ into lapic.
895 * Return 1 if successfully added and 0 if discarded.
896 */
897static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
Yang Zhangb4f22252013-04-11 19:21:37 +0800898 int vector, int level, int trig_mode,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100899 struct dest_map *dest_map)
Eddie Dong97222cc2007-09-12 10:58:04 +0300900{
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200901 int result = 0;
He, Qingc5ec1532007-09-03 17:07:41 +0300902 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300903
Paolo Bonzinia183b632014-09-11 11:51:02 +0200904 trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
905 trig_mode, vector);
Eddie Dong97222cc2007-09-12 10:58:04 +0300906 switch (delivery_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300907 case APIC_DM_LOWEST:
Gleb Natapove1035712009-03-05 16:34:59 +0200908 vcpu->arch.apic_arb_prio++;
909 case APIC_DM_FIXED:
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200910 if (unlikely(trig_mode && !level))
911 break;
912
Eddie Dong97222cc2007-09-12 10:58:04 +0300913 /* FIXME add logic for vcpu on reset */
914 if (unlikely(!apic_enabled(apic)))
915 break;
916
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200917 result = 1;
918
Joerg Roedel9daa5002016-02-29 16:04:44 +0100919 if (dest_map) {
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100920 __set_bit(vcpu->vcpu_id, dest_map->map);
Joerg Roedel9daa5002016-02-29 16:04:44 +0100921 dest_map->vectors[vcpu->vcpu_id] = vector;
922 }
Avi Kivitya5d36f82009-12-29 12:42:16 +0200923
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200924 if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
925 if (trig_mode)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500926 kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
Paolo Bonzinibdaffe12015-07-29 15:03:06 +0200927 else
928 apic_clear_vector(vector, apic->regs + APIC_TMR);
929 }
930
Andrey Smetanind62caab2015-11-10 15:36:33 +0300931 if (vcpu->arch.apicv_active)
Yang Zhang5a717852013-04-11 19:25:16 +0800932 kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
Jan Kiszka11f5cc02013-07-25 09:58:45 +0200933 else {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -0500934 kvm_lapic_set_irr(vector, apic);
Yang Zhang5a717852013-04-11 19:25:16 +0800935
936 kvm_make_request(KVM_REQ_EVENT, vcpu);
937 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300938 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300939 break;
940
941 case APIC_DM_REMRD:
Raghavendra K T24d21662013-08-26 14:18:35 +0530942 result = 1;
943 vcpu->arch.pv.pv_unhalted = 1;
944 kvm_make_request(KVM_REQ_EVENT, vcpu);
945 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300946 break;
947
948 case APIC_DM_SMI:
Paolo Bonzini64d60672015-05-07 11:36:11 +0200949 result = 1;
950 kvm_make_request(KVM_REQ_SMI, vcpu);
951 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300952 break;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800953
Eddie Dong97222cc2007-09-12 10:58:04 +0300954 case APIC_DM_NMI:
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200955 result = 1;
Sheng Yang3419ffc2008-05-15 09:52:48 +0800956 kvm_inject_nmi(vcpu);
Jan Kiszka26df99c2008-09-26 09:30:54 +0200957 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300958 break;
959
960 case APIC_DM_INIT:
Julian Stecklinaa52315e2012-01-16 14:02:20 +0100961 if (!trig_mode || level) {
Gleb Natapov6da7e3f2009-03-05 16:34:44 +0200962 result = 1;
Jan Kiszka66450a22013-03-13 12:42:34 +0100963 /* assumes that there are only KVM_APIC_INIT/SIPI */
964 apic->pending_events = (1UL << KVM_APIC_INIT);
965 /* make sure pending_events is visible before sending
966 * the request */
967 smp_wmb();
Avi Kivity3842d132010-07-27 12:30:24 +0300968 kvm_make_request(KVM_REQ_EVENT, vcpu);
He, Qingc5ec1532007-09-03 17:07:41 +0300969 kvm_vcpu_kick(vcpu);
970 } else {
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200971 apic_debug("Ignoring de-assert INIT to vcpu %d\n",
972 vcpu->vcpu_id);
He, Qingc5ec1532007-09-03 17:07:41 +0300973 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300974 break;
975
976 case APIC_DM_STARTUP:
Jan Kiszka1b10bf32008-09-30 10:41:06 +0200977 apic_debug("SIPI to vcpu %d vector 0x%02x\n",
978 vcpu->vcpu_id, vector);
Jan Kiszka66450a22013-03-13 12:42:34 +0100979 result = 1;
980 apic->sipi_vector = vector;
981 /* make sure sipi_vector is visible for the receiver */
982 smp_wmb();
983 set_bit(KVM_APIC_SIPI, &apic->pending_events);
984 kvm_make_request(KVM_REQ_EVENT, vcpu);
985 kvm_vcpu_kick(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +0300986 break;
987
Jan Kiszka23930f92008-09-26 09:30:52 +0200988 case APIC_DM_EXTINT:
989 /*
990 * Should only be called by kvm_apic_local_deliver() with LVT0,
991 * before NMI watchdog was enabled. Already handled by
992 * kvm_apic_accept_pic_intr().
993 */
994 break;
995
Eddie Dong97222cc2007-09-12 10:58:04 +0300996 default:
997 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
998 delivery_mode);
999 break;
1000 }
1001 return result;
1002}
1003
Gleb Natapove1035712009-03-05 16:34:59 +02001004int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Eddie Dong97222cc2007-09-12 10:58:04 +03001005{
Gleb Natapove1035712009-03-05 16:34:59 +02001006 return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
Zhang Xiantao8be54532007-12-02 22:35:57 +08001007}
1008
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001009static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
1010{
Andrey Smetanin63086302015-11-10 15:36:32 +03001011 return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001012}
1013
Yang Zhangc7c9c562013-01-25 10:18:51 +08001014static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
1015{
Steve Rutherford7543a632015-07-29 23:21:41 -07001016 int trigger_mode;
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02001017
Steve Rutherford7543a632015-07-29 23:21:41 -07001018 /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1019 if (!kvm_ioapic_handles_vector(apic, vector))
1020 return;
1021
1022 /* Request a KVM exit to inform the userspace IOAPIC. */
1023 if (irqchip_split(apic->vcpu->kvm)) {
1024 apic->vcpu->arch.pending_ioapic_eoi = vector;
1025 kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1026 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08001027 }
Steve Rutherford7543a632015-07-29 23:21:41 -07001028
1029 if (apic_test_vector(vector, apic->regs + APIC_TMR))
1030 trigger_mode = IOAPIC_LEVEL_TRIG;
1031 else
1032 trigger_mode = IOAPIC_EDGE_TRIG;
1033
1034 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
Yang Zhangc7c9c562013-01-25 10:18:51 +08001035}
1036
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001037static int apic_set_eoi(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001038{
1039 int vector = apic_find_highest_isr(apic);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001040
1041 trace_kvm_eoi(apic, vector);
1042
Eddie Dong97222cc2007-09-12 10:58:04 +03001043 /*
1044 * Not every write EOI will has corresponding ISR,
1045 * one example is when Kernel check timer on setup_IO_APIC
1046 */
1047 if (vector == -1)
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001048 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001049
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001050 apic_clear_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001051 apic_update_ppr(apic);
1052
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001053 if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
1054 kvm_hv_synic_send_eoi(apic->vcpu, vector);
1055
Yang Zhangc7c9c562013-01-25 10:18:51 +08001056 kvm_ioapic_send_eoi(apic, vector);
Avi Kivity3842d132010-07-27 12:30:24 +03001057 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001058 return vector;
Eddie Dong97222cc2007-09-12 10:58:04 +03001059}
1060
Yang Zhangc7c9c562013-01-25 10:18:51 +08001061/*
1062 * this interface assumes a trap-like exit, which has already finished
1063 * desired side effect including vISR and vPPR update.
1064 */
1065void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
1066{
1067 struct kvm_lapic *apic = vcpu->arch.apic;
1068
1069 trace_kvm_eoi(apic, vector);
1070
1071 kvm_ioapic_send_eoi(apic, vector);
1072 kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1073}
1074EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
1075
Eddie Dong97222cc2007-09-12 10:58:04 +03001076static void apic_send_ipi(struct kvm_lapic *apic)
1077{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001078 u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
1079 u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001080 struct kvm_lapic_irq irq;
Eddie Dong97222cc2007-09-12 10:58:04 +03001081
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001082 irq.vector = icr_low & APIC_VECTOR_MASK;
1083 irq.delivery_mode = icr_low & APIC_MODE_MASK;
1084 irq.dest_mode = icr_low & APIC_DEST_MASK;
Paolo Bonzinib7cb2232015-04-21 14:57:05 +02001085 irq.level = (icr_low & APIC_INT_ASSERT) != 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001086 irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1087 irq.shorthand = icr_low & APIC_SHORT_MASK;
James Sullivan93bbf0b2015-03-18 19:26:03 -06001088 irq.msi_redir_hint = false;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001089 if (apic_x2apic_mode(apic))
1090 irq.dest_id = icr_high;
1091 else
1092 irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
Eddie Dong97222cc2007-09-12 10:58:04 +03001093
Gleb Natapov1000ff82009-07-07 16:00:57 +03001094 trace_kvm_apic_ipi(icr_low, irq.dest_id);
1095
Eddie Dong97222cc2007-09-12 10:58:04 +03001096 apic_debug("icr_high 0x%x, icr_low 0x%x, "
1097 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
James Sullivan93bbf0b2015-03-18 19:26:03 -06001098 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
1099 "msi_redir_hint 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001100 icr_high, icr_low, irq.shorthand, irq.dest_id,
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001101 irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
James Sullivan93bbf0b2015-03-18 19:26:03 -06001102 irq.vector, irq.msi_redir_hint);
Eddie Dong97222cc2007-09-12 10:58:04 +03001103
Yang Zhangb4f22252013-04-11 19:21:37 +08001104 kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
Eddie Dong97222cc2007-09-12 10:58:04 +03001105}
1106
1107static u32 apic_get_tmcct(struct kvm_lapic *apic)
1108{
Marcelo Tosattib682b812009-02-10 20:41:41 -02001109 ktime_t remaining;
1110 s64 ns;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001111 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +03001112
1113 ASSERT(apic != NULL);
1114
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001115 /* if initial count is 0, current count should also be 0 */
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001116 if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
Andy Honigb963a222013-11-19 14:12:18 -08001117 apic->lapic_timer.period == 0)
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +02001118 return 0;
1119
Marcelo Tosattiace15462009-10-08 10:55:03 -03001120 remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
Marcelo Tosattib682b812009-02-10 20:41:41 -02001121 if (ktime_to_ns(remaining) < 0)
1122 remaining = ktime_set(0, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001123
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001124 ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1125 tmcct = div64_u64(ns,
1126 (APIC_BUS_CYCLE_NS * apic->divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +03001127
1128 return tmcct;
1129}
1130
Avi Kivityb209749f2007-10-22 16:50:39 +02001131static void __report_tpr_access(struct kvm_lapic *apic, bool write)
1132{
1133 struct kvm_vcpu *vcpu = apic->vcpu;
1134 struct kvm_run *run = vcpu->run;
1135
Avi Kivitya8eeb042010-05-10 12:34:53 +03001136 kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001137 run->tpr_access.rip = kvm_rip_read(vcpu);
Avi Kivityb209749f2007-10-22 16:50:39 +02001138 run->tpr_access.is_write = write;
1139}
1140
1141static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
1142{
1143 if (apic->vcpu->arch.tpr_access_reporting)
1144 __report_tpr_access(apic, write);
1145}
1146
Eddie Dong97222cc2007-09-12 10:58:04 +03001147static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
1148{
1149 u32 val = 0;
1150
1151 if (offset >= LAPIC_MMIO_LENGTH)
1152 return 0;
1153
1154 switch (offset) {
1155 case APIC_ARBPRI:
Jan Kiszka7712de82011-09-12 11:25:51 +02001156 apic_debug("Access APIC ARBPRI register which is for P6\n");
Eddie Dong97222cc2007-09-12 10:58:04 +03001157 break;
1158
1159 case APIC_TMCCT: /* Timer CCR */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001160 if (apic_lvtt_tscdeadline(apic))
1161 return 0;
1162
Eddie Dong97222cc2007-09-12 10:58:04 +03001163 val = apic_get_tmcct(apic);
1164 break;
Avi Kivity4a4541a2012-07-22 17:41:00 +03001165 case APIC_PROCPRI:
1166 apic_update_ppr(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001167 val = kvm_lapic_get_reg(apic, offset);
Avi Kivity4a4541a2012-07-22 17:41:00 +03001168 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001169 case APIC_TASKPRI:
1170 report_tpr_access(apic, false);
1171 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +03001172 default:
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001173 val = kvm_lapic_get_reg(apic, offset);
Eddie Dong97222cc2007-09-12 10:58:04 +03001174 break;
1175 }
1176
1177 return val;
1178}
1179
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001180static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
1181{
1182 return container_of(dev, struct kvm_lapic, dev);
1183}
1184
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001185int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001186 void *data)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001187{
Eddie Dong97222cc2007-09-12 10:58:04 +03001188 unsigned char alignment = offset & 0xf;
1189 u32 result;
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001190 /* this bitmask has a bit cleared for each reserved register */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001191 static const u64 rmask = 0x43ff01ffffffe70cULL;
Eddie Dong97222cc2007-09-12 10:58:04 +03001192
1193 if ((alignment + len) > 4) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001194 apic_debug("KVM_APIC_READ: alignment error %x %d\n",
1195 offset, len);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001196 return 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001197 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001198
1199 if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
Gleb Natapov4088bb32009-07-08 11:26:54 +03001200 apic_debug("KVM_APIC_READ: read reserved register %x\n",
1201 offset);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001202 return 1;
1203 }
1204
Eddie Dong97222cc2007-09-12 10:58:04 +03001205 result = __apic_read(apic, offset & ~0xf);
1206
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001207 trace_kvm_apic_read(offset, result);
1208
Eddie Dong97222cc2007-09-12 10:58:04 +03001209 switch (len) {
1210 case 1:
1211 case 2:
1212 case 4:
1213 memcpy(data, (char *)&result + alignment, len);
1214 break;
1215 default:
1216 printk(KERN_ERR "Local APIC read with len = %x, "
1217 "should be 1,2, or 4 instead\n", len);
1218 break;
1219 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001220 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001221}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001222EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
Eddie Dong97222cc2007-09-12 10:58:04 +03001223
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001224static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
1225{
Vitaly Kuznetsov08d2bf52018-08-02 17:08:16 +02001226 return addr >= apic->base_address &&
1227 addr < apic->base_address + LAPIC_MMIO_LENGTH;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001228}
1229
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001230static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001231 gpa_t address, int len, void *data)
1232{
1233 struct kvm_lapic *apic = to_lapic(this);
1234 u32 offset = address - apic->base_address;
1235
1236 if (!apic_mmio_in_range(apic, address))
1237 return -EOPNOTSUPP;
1238
Vitaly Kuznetsov08d2bf52018-08-02 17:08:16 +02001239 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1240 if (!kvm_check_has_quirk(vcpu->kvm,
1241 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1242 return -EOPNOTSUPP;
1243
1244 memset(data, 0xff, len);
1245 return 0;
1246 }
1247
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001248 kvm_lapic_reg_read(apic, offset, len, data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001249
1250 return 0;
1251}
1252
Eddie Dong97222cc2007-09-12 10:58:04 +03001253static void update_divide_count(struct kvm_lapic *apic)
1254{
1255 u32 tmp1, tmp2, tdcr;
1256
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001257 tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
Eddie Dong97222cc2007-09-12 10:58:04 +03001258 tmp1 = tdcr & 0xf;
1259 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001260 apic->divide_count = 0x1 << (tmp2 & 0x7);
Eddie Dong97222cc2007-09-12 10:58:04 +03001261
1262 apic_debug("timer divide count is 0x%x\n",
Glauber Costa9b5843d2009-04-29 17:29:09 -04001263 apic->divide_count);
Eddie Dong97222cc2007-09-12 10:58:04 +03001264}
1265
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001266static void apic_update_lvtt(struct kvm_lapic *apic)
1267{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001268 u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001269 apic->lapic_timer.timer_mode_mask;
1270
1271 if (apic->lapic_timer.timer_mode != timer_mode) {
1272 apic->lapic_timer.timer_mode = timer_mode;
1273 hrtimer_cancel(&apic->lapic_timer.timer);
1274 }
1275}
1276
Radim Krčmář5d87db72014-10-10 19:15:08 +02001277static void apic_timer_expired(struct kvm_lapic *apic)
1278{
1279 struct kvm_vcpu *vcpu = apic->vcpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001280 struct swait_queue_head *q = &vcpu->wq;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001281 struct kvm_timer *ktimer = &apic->lapic_timer;
Radim Krčmář5d87db72014-10-10 19:15:08 +02001282
Radim Krčmář5d87db72014-10-10 19:15:08 +02001283 if (atomic_read(&apic->lapic_timer.pending))
1284 return;
1285
1286 atomic_inc(&apic->lapic_timer.pending);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001287 kvm_set_pending_timer(vcpu);
Radim Krčmář5d87db72014-10-10 19:15:08 +02001288
Marcelo Tosatti85773702016-02-19 09:46:39 +01001289 if (swait_active(q))
1290 swake_up(q);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001291
1292 if (apic_lvtt_tscdeadline(apic))
1293 ktimer->expired_tscdeadline = ktimer->tscdeadline;
1294}
1295
1296/*
1297 * On APICv, this test will cause a busy wait
1298 * during a higher-priority task.
1299 */
1300
1301static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
1302{
1303 struct kvm_lapic *apic = vcpu->arch.apic;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001304 u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001305
1306 if (kvm_apic_hw_enabled(apic)) {
1307 int vec = reg & APIC_VECTOR_MASK;
Marcelo Tosattif9339862015-02-02 15:26:08 -02001308 void *bitmap = apic->regs + APIC_ISR;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001309
Andrey Smetanind62caab2015-11-10 15:36:33 +03001310 if (vcpu->arch.apicv_active)
Marcelo Tosattif9339862015-02-02 15:26:08 -02001311 bitmap = apic->regs + APIC_IRR;
1312
1313 if (apic_test_vector(vec, bitmap))
1314 return true;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001315 }
1316 return false;
1317}
1318
1319void wait_lapic_expire(struct kvm_vcpu *vcpu)
1320{
1321 struct kvm_lapic *apic = vcpu->arch.apic;
1322 u64 guest_tsc, tsc_deadline;
1323
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001324 if (!lapic_in_kernel(vcpu))
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001325 return;
1326
1327 if (apic->lapic_timer.expired_tscdeadline == 0)
1328 return;
1329
1330 if (!lapic_timer_int_injected(vcpu))
1331 return;
1332
1333 tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1334 apic->lapic_timer.expired_tscdeadline = 0;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001335 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Marcelo Tosatti6c19b752014-12-16 09:08:16 -05001336 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001337
1338 /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
1339 if (guest_tsc < tsc_deadline)
Marcelo Tosattib606f182016-06-20 22:33:48 -03001340 __delay(min(tsc_deadline - guest_tsc,
1341 nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
Radim Krčmář5d87db72014-10-10 19:15:08 +02001342}
1343
Yunhong Jiang53f9eed2016-06-13 14:20:00 -07001344static void start_sw_tscdeadline(struct kvm_lapic *apic)
1345{
1346 u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
1347 u64 ns = 0;
1348 ktime_t expire;
1349 struct kvm_vcpu *vcpu = apic->vcpu;
1350 unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1351 unsigned long flags;
1352 ktime_t now;
1353
1354 if (unlikely(!tscdeadline || !this_tsc_khz))
1355 return;
1356
1357 local_irq_save(flags);
1358
1359 now = apic->lapic_timer.timer.base->get_time();
1360 guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1361 if (likely(tscdeadline > guest_tsc)) {
1362 ns = (tscdeadline - guest_tsc) * 1000000ULL;
1363 do_div(ns, this_tsc_khz);
1364 expire = ktime_add_ns(now, ns);
1365 expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
1366 hrtimer_start(&apic->lapic_timer.timer,
1367 expire, HRTIMER_MODE_ABS_PINNED);
1368 } else
1369 apic_timer_expired(apic);
1370
1371 local_irq_restore(flags);
1372}
1373
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001374bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
1375{
Wanpeng Li91005302016-08-03 12:04:12 +08001376 if (!lapic_in_kernel(vcpu))
1377 return false;
1378
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001379 return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
1380}
1381EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
1382
Wanpeng Libd97ad02016-06-30 08:52:49 +08001383static void cancel_hv_tscdeadline(struct kvm_lapic *apic)
1384{
Wanpeng Li5ea05192017-05-20 20:32:32 -07001385 preempt_disable();
Wanpeng Libd97ad02016-06-30 08:52:49 +08001386 kvm_x86_ops->cancel_hv_timer(apic->vcpu);
1387 apic->lapic_timer.hv_timer_in_use = false;
Wanpeng Li5ea05192017-05-20 20:32:32 -07001388 preempt_enable();
Wanpeng Libd97ad02016-06-30 08:52:49 +08001389}
1390
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001391void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
1392{
1393 struct kvm_lapic *apic = vcpu->arch.apic;
1394
1395 WARN_ON(!apic->lapic_timer.hv_timer_in_use);
1396 WARN_ON(swait_active(&vcpu->wq));
Wanpeng Libd97ad02016-06-30 08:52:49 +08001397 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001398 apic_timer_expired(apic);
1399}
1400EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
1401
Wanpeng Li196f20c2016-06-28 14:54:19 +08001402static bool start_hv_tscdeadline(struct kvm_lapic *apic)
1403{
1404 u64 tscdeadline = apic->lapic_timer.tscdeadline;
1405
1406 if (atomic_read(&apic->lapic_timer.pending) ||
1407 kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) {
1408 if (apic->lapic_timer.hv_timer_in_use)
1409 cancel_hv_tscdeadline(apic);
1410 } else {
1411 apic->lapic_timer.hv_timer_in_use = true;
1412 hrtimer_cancel(&apic->lapic_timer.timer);
1413
1414 /* In case the sw timer triggered in the window */
1415 if (atomic_read(&apic->lapic_timer.pending))
1416 cancel_hv_tscdeadline(apic);
1417 }
1418 trace_kvm_hv_timer_state(apic->vcpu->vcpu_id,
1419 apic->lapic_timer.hv_timer_in_use);
1420 return apic->lapic_timer.hv_timer_in_use;
1421}
1422
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001423void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
1424{
1425 struct kvm_lapic *apic = vcpu->arch.apic;
1426
1427 WARN_ON(apic->lapic_timer.hv_timer_in_use);
1428
Wanpeng Li196f20c2016-06-28 14:54:19 +08001429 if (apic_lvtt_tscdeadline(apic))
1430 start_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001431}
1432EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer);
1433
1434void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
1435{
1436 struct kvm_lapic *apic = vcpu->arch.apic;
1437
1438 /* Possibly the TSC deadline timer is not enabled yet */
1439 if (!apic->lapic_timer.hv_timer_in_use)
1440 return;
1441
Wanpeng Libd97ad02016-06-30 08:52:49 +08001442 cancel_hv_tscdeadline(apic);
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001443
1444 if (atomic_read(&apic->lapic_timer.pending))
1445 return;
1446
1447 start_sw_tscdeadline(apic);
1448}
1449EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer);
1450
Eddie Dong97222cc2007-09-12 10:58:04 +03001451static void start_apic_timer(struct kvm_lapic *apic)
1452{
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001453 ktime_t now;
Marcelo Tosattid0659d92014-12-16 09:08:15 -05001454
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001455 atomic_set(&apic->lapic_timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +02001456
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001457 if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
Guo Chaod5b0b5b2012-06-28 15:22:57 +08001458 /* lapic timer in oneshot or periodic mode */
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001459 now = apic->lapic_timer.timer.base->get_time();
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001460 apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001461 * APIC_BUS_CYCLE_NS * apic->divide_count;
Jan Kiszka9bc57912011-09-12 14:10:22 +02001462
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001463 if (!apic->lapic_timer.period)
1464 return;
1465 /*
1466 * Do not allow the guest to program periodic timers with small
1467 * interval, since the hrtimers are not throttled by the host
1468 * scheduler.
1469 */
1470 if (apic_lvtt_period(apic)) {
1471 s64 min_period = min_timer_period_us * 1000LL;
1472
1473 if (apic->lapic_timer.period < min_period) {
1474 pr_info_ratelimited(
1475 "kvm: vcpu %i: requested %lld ns "
1476 "lapic timer period limited to %lld ns\n",
1477 apic->vcpu->vcpu_id,
1478 apic->lapic_timer.period, min_period);
1479 apic->lapic_timer.period = min_period;
1480 }
Jan Kiszka9bc57912011-09-12 14:10:22 +02001481 }
Avi Kivity0b975a32008-02-24 14:37:50 +02001482
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001483 hrtimer_start(&apic->lapic_timer.timer,
1484 ktime_add_ns(now, apic->lapic_timer.period),
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001485 HRTIMER_MODE_ABS_PINNED);
Eddie Dong97222cc2007-09-12 10:58:04 +03001486
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001487 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
Eddie Dong97222cc2007-09-12 10:58:04 +03001488 PRIx64 ", "
1489 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001490 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001491 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001492 kvm_lapic_get_reg(apic, APIC_TMICT),
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001493 apic->lapic_timer.period,
Eddie Dong97222cc2007-09-12 10:58:04 +03001494 ktime_to_ns(ktime_add_ns(now,
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001495 apic->lapic_timer.period)));
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001496 } else if (apic_lvtt_tscdeadline(apic)) {
Wanpeng Li196f20c2016-06-28 14:54:19 +08001497 if (!(kvm_x86_ops->set_hv_timer && start_hv_tscdeadline(apic)))
Yunhong Jiangce7a0582016-06-13 14:20:01 -07001498 start_sw_tscdeadline(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001499 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001500}
1501
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001502static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
1503{
Radim Krčmář59fd1322015-06-30 22:19:16 +02001504 bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001505
Radim Krčmář59fd1322015-06-30 22:19:16 +02001506 if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
1507 apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
1508 if (lvt0_in_nmi_mode) {
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001509 apic_debug("Receive NMI setting on APIC_LVT0 "
1510 "for cpu %d\n", apic->vcpu->vcpu_id);
Radim Krčmář42720132015-07-01 15:31:49 +02001511 atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
Radim Krčmář59fd1322015-06-30 22:19:16 +02001512 } else
1513 atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
1514 }
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001515}
1516
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001517int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Eddie Dong97222cc2007-09-12 10:58:04 +03001518{
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001519 int ret = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001520
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001521 trace_kvm_apic_write(reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001522
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001523 switch (reg) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001524 case APIC_ID: /* Local APIC ID */
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001525 if (!apic_x2apic_mode(apic))
Radim Krčmářa92e2542016-07-12 22:09:22 +02001526 kvm_apic_set_xapic_id(apic, val >> 24);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001527 else
1528 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001529 break;
1530
1531 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +02001532 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +03001533 apic_set_tpr(apic, val & 0xff);
1534 break;
1535
1536 case APIC_EOI:
1537 apic_set_eoi(apic);
1538 break;
1539
1540 case APIC_LDR:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001541 if (!apic_x2apic_mode(apic))
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001542 kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001543 else
1544 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001545 break;
1546
1547 case APIC_DFR:
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001548 if (!apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001549 kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03001550 recalculate_apic_map(apic->vcpu->kvm);
1551 } else
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001552 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001553 break;
1554
Gleb Natapovfc61b802009-07-05 17:39:35 +03001555 case APIC_SPIV: {
1556 u32 mask = 0x3ff;
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001557 if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
Gleb Natapovfc61b802009-07-05 17:39:35 +03001558 mask |= APIC_SPIV_DIRECTED_EOI;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001559 apic_set_spiv(apic, val & mask);
Eddie Dong97222cc2007-09-12 10:58:04 +03001560 if (!(val & APIC_SPIV_APIC_ENABLED)) {
1561 int i;
1562 u32 lvt_val;
1563
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001564 for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001565 lvt_val = kvm_lapic_get_reg(apic,
Eddie Dong97222cc2007-09-12 10:58:04 +03001566 APIC_LVTT + 0x10 * i);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001567 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
Eddie Dong97222cc2007-09-12 10:58:04 +03001568 lvt_val | APIC_LVT_MASKED);
1569 }
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001570 apic_update_lvtt(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001571 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001572
1573 }
1574 break;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001575 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001576 case APIC_ICR:
1577 /* No delay here, so we always clear the pending bit */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001578 kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
Eddie Dong97222cc2007-09-12 10:58:04 +03001579 apic_send_ipi(apic);
1580 break;
1581
1582 case APIC_ICR2:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001583 if (!apic_x2apic_mode(apic))
1584 val &= 0xff000000;
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001585 kvm_lapic_set_reg(apic, APIC_ICR2, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001586 break;
1587
Jan Kiszka23930f92008-09-26 09:30:52 +02001588 case APIC_LVT0:
Jan Kiszkacc6e4622008-10-20 10:20:03 +02001589 apic_manage_nmi_watchdog(apic, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001590 case APIC_LVTTHMR:
1591 case APIC_LVTPC:
Eddie Dong97222cc2007-09-12 10:58:04 +03001592 case APIC_LVT1:
Marios Pomonis19fe4b12019-12-11 12:47:46 -08001593 case APIC_LVTERR: {
Eddie Dong97222cc2007-09-12 10:58:04 +03001594 /* TODO: Check vector */
Marios Pomonis19fe4b12019-12-11 12:47:46 -08001595 size_t size;
1596 u32 index;
1597
Gleb Natapovc48f1492012-08-05 15:58:33 +03001598 if (!kvm_apic_sw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03001599 val |= APIC_LVT_MASKED;
Marios Pomonis19fe4b12019-12-11 12:47:46 -08001600 size = ARRAY_SIZE(apic_lvt_mask);
1601 index = array_index_nospec(
1602 (reg - APIC_LVTT) >> 4, size);
1603 val &= apic_lvt_mask[index];
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001604 kvm_lapic_set_reg(apic, reg, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001605 break;
Marios Pomonis19fe4b12019-12-11 12:47:46 -08001606 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001607
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001608 case APIC_LVTT:
Gleb Natapovc48f1492012-08-05 15:58:33 +03001609 if (!kvm_apic_sw_enabled(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001610 val |= APIC_LVT_MASKED;
1611 val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001612 kvm_lapic_set_reg(apic, APIC_LVTT, val);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001613 apic_update_lvtt(apic);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001614 break;
1615
Eddie Dong97222cc2007-09-12 10:58:04 +03001616 case APIC_TMICT:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001617 if (apic_lvtt_tscdeadline(apic))
1618 break;
1619
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001620 hrtimer_cancel(&apic->lapic_timer.timer);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001621 kvm_lapic_set_reg(apic, APIC_TMICT, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001622 start_apic_timer(apic);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001623 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001624
1625 case APIC_TDCR:
1626 if (val & 4)
Jan Kiszka7712de82011-09-12 11:25:51 +02001627 apic_debug("KVM_WRITE:TDCR %x\n", val);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001628 kvm_lapic_set_reg(apic, APIC_TDCR, val);
Eddie Dong97222cc2007-09-12 10:58:04 +03001629 update_divide_count(apic);
1630 break;
1631
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001632 case APIC_ESR:
1633 if (apic_x2apic_mode(apic) && val != 0) {
Jan Kiszka7712de82011-09-12 11:25:51 +02001634 apic_debug("KVM_WRITE:ESR not zero %x\n", val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001635 ret = 1;
1636 }
1637 break;
1638
1639 case APIC_SELF_IPI:
1640 if (apic_x2apic_mode(apic)) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001641 kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001642 } else
1643 ret = 1;
1644 break;
Eddie Dong97222cc2007-09-12 10:58:04 +03001645 default:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001646 ret = 1;
Eddie Dong97222cc2007-09-12 10:58:04 +03001647 break;
1648 }
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001649 if (ret)
1650 apic_debug("Local APIC Write to read-only register %x\n", reg);
1651 return ret;
1652}
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001653EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001654
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00001655static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001656 gpa_t address, int len, const void *data)
1657{
1658 struct kvm_lapic *apic = to_lapic(this);
1659 unsigned int offset = address - apic->base_address;
1660 u32 val;
1661
1662 if (!apic_mmio_in_range(apic, address))
1663 return -EOPNOTSUPP;
1664
Vitaly Kuznetsov08d2bf52018-08-02 17:08:16 +02001665 if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1666 if (!kvm_check_has_quirk(vcpu->kvm,
1667 KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1668 return -EOPNOTSUPP;
1669
1670 return 0;
1671 }
1672
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001673 /*
1674 * APIC register must be aligned on 128-bits boundary.
1675 * 32/64/128 bits registers must be accessed thru 32 bits.
1676 * Refer SDM 8.4.1
1677 */
1678 if (len != 4 || (offset & 0xf)) {
1679 /* Don't shout loud, $infamous_os would cause only noise. */
1680 apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
Sheng Yang756975b2009-07-06 11:05:39 +08001681 return 0;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001682 }
1683
1684 val = *(u32*)data;
1685
1686 /* too common printing */
1687 if (offset != APIC_EOI)
1688 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
1689 "0x%x\n", __func__, offset, len, val);
1690
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001691 kvm_lapic_reg_write(apic, offset & 0xff0, val);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001692
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001693 return 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001694}
1695
Kevin Tian58fbbf22011-08-30 13:56:17 +03001696void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
1697{
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001698 kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
Kevin Tian58fbbf22011-08-30 13:56:17 +03001699}
1700EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
1701
Yang Zhang83d4c282013-01-25 10:18:49 +08001702/* emulate APIC access in a trap manner */
1703void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
1704{
1705 u32 val = 0;
1706
1707 /* hw has done the conditional check and inst decode */
1708 offset &= 0xff0;
1709
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001710 kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001711
1712 /* TODO: optimize to just emulate side effect w/o one more write */
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001713 kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
Yang Zhang83d4c282013-01-25 10:18:49 +08001714}
1715EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
1716
Rusty Russelld5894442007-10-08 10:48:30 +10001717void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +03001718{
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001719 struct kvm_lapic *apic = vcpu->arch.apic;
1720
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001721 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001722 return;
1723
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001724 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001725
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001726 if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
1727 static_key_slow_dec_deferred(&apic_hw_disabled);
1728
Radim Krčmáře4627552014-10-30 15:06:45 +01001729 if (!apic->sw_enabled)
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001730 static_key_slow_dec_deferred(&apic_sw_disabled);
Eddie Dong97222cc2007-09-12 10:58:04 +03001731
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001732 if (apic->regs)
1733 free_page((unsigned long)apic->regs);
1734
1735 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001736}
1737
1738/*
1739 *----------------------------------------------------------------------
1740 * LAPIC interface
1741 *----------------------------------------------------------------------
1742 */
1743
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001744u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
1745{
1746 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001747
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001748 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001749 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001750 return 0;
1751
1752 return apic->lapic_timer.tscdeadline;
1753}
1754
1755void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
1756{
1757 struct kvm_lapic *apic = vcpu->arch.apic;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001758
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01001759 if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
Gleb Natapov54e98182012-08-05 15:58:32 +03001760 apic_lvtt_period(apic))
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001761 return;
1762
1763 hrtimer_cancel(&apic->lapic_timer.timer);
1764 apic->lapic_timer.tscdeadline = data;
1765 start_apic_timer(apic);
1766}
1767
Eddie Dong97222cc2007-09-12 10:58:04 +03001768void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
1769{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001771
Avi Kivityb93463a2007-10-25 16:52:32 +02001772 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001773 | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +03001774}
1775
1776u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
1777{
Eddie Dong97222cc2007-09-12 10:58:04 +03001778 u64 tpr;
1779
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001780 tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
Eddie Dong97222cc2007-09-12 10:58:04 +03001781
1782 return (tpr & 0xf0) >> 4;
1783}
1784
1785void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
1786{
Yang Zhang8d146952013-01-25 10:18:50 +08001787 u64 old_value = vcpu->arch.apic_base;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001788 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001789
1790 if (!apic) {
1791 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001792 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +03001793 return;
1794 }
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001795
Jan Kiszkae66d2ae2013-12-29 02:29:30 +01001796 vcpu->arch.apic_base = value;
1797
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001798 /* update jump label if enable bit changes */
Andrew Jones0dce7cd2014-01-15 13:39:59 +01001799 if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
Radim Krčmář49bd29b2016-07-12 22:09:23 +02001800 if (value & MSR_IA32_APICBASE_ENABLE) {
1801 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001802 static_key_slow_dec_deferred(&apic_hw_disabled);
Wanpeng Li187ca842016-08-03 12:04:13 +08001803 } else {
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001804 static_key_slow_inc(&apic_hw_disabled.key);
Wanpeng Li187ca842016-08-03 12:04:13 +08001805 recalculate_apic_map(vcpu->kvm);
1806 }
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001807 }
1808
Yang Zhang8d146952013-01-25 10:18:50 +08001809 if ((old_value ^ value) & X2APIC_ENABLE) {
1810 if (value & X2APIC_ENABLE) {
Radim Krčmář257b9a52015-05-22 18:45:11 +02001811 kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
Yang Zhang8d146952013-01-25 10:18:50 +08001812 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
1813 } else
1814 kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001815 }
Yang Zhang8d146952013-01-25 10:18:50 +08001816
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001817 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +03001818 MSR_IA32_APICBASE_BASE;
1819
Nadav Amitdb324fe2014-11-02 11:54:59 +02001820 if ((value & MSR_IA32_APICBASE_ENABLE) &&
1821 apic->base_address != APIC_DEFAULT_PHYS_BASE)
1822 pr_warn_once("APIC base relocation is unsupported by KVM");
1823
Eddie Dong97222cc2007-09-12 10:58:04 +03001824 /* with FSB delivery interrupt, we can restart APIC functionality */
1825 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001826 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001827
1828}
1829
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001830void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
Eddie Dong97222cc2007-09-12 10:58:04 +03001831{
1832 struct kvm_lapic *apic;
1833 int i;
1834
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001835 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +03001836
1837 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001838 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001839 ASSERT(apic != NULL);
1840
1841 /* Stop the timer in case it's a reset to an active apic */
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001842 hrtimer_cancel(&apic->lapic_timer.timer);
Eddie Dong97222cc2007-09-12 10:58:04 +03001843
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001844 if (!init_event) {
1845 kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE |
1846 MSR_IA32_APICBASE_ENABLE);
Radim Krčmářa92e2542016-07-12 22:09:22 +02001847 kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
Radim Krčmář4d8e7722016-07-12 22:09:25 +02001848 }
Gleb Natapovfc61b802009-07-05 17:39:35 +03001849 kvm_apic_set_version(apic->vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001850
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001851 for (i = 0; i < KVM_APIC_LVT_NUM; i++)
1852 kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02001853 apic_update_lvtt(apic);
Paolo Bonzini0da029e2015-07-23 08:24:42 +02001854 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001855 kvm_lapic_set_reg(apic, APIC_LVT0,
Nadav Amit90de4a12015-04-13 01:53:41 +03001856 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001857 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong97222cc2007-09-12 10:58:04 +03001858
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001859 kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001860 apic_set_spiv(apic, 0xff);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001861 kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
Radim Krčmářc028dd62015-05-22 19:22:10 +02001862 if (!apic_x2apic_mode(apic))
1863 kvm_apic_set_ldr(apic, 0);
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001864 kvm_lapic_set_reg(apic, APIC_ESR, 0);
1865 kvm_lapic_set_reg(apic, APIC_ICR, 0);
1866 kvm_lapic_set_reg(apic, APIC_ICR2, 0);
1867 kvm_lapic_set_reg(apic, APIC_TDCR, 0);
1868 kvm_lapic_set_reg(apic, APIC_TMICT, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001869 for (i = 0; i < 8; i++) {
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05001870 kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
1871 kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
1872 kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
Eddie Dong97222cc2007-09-12 10:58:04 +03001873 }
Andrey Smetanind62caab2015-11-10 15:36:33 +03001874 apic->irr_pending = vcpu->arch.apicv_active;
1875 apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03001876 apic->highest_isr_cache = -1;
Kevin Pedrettib33ac882007-10-21 08:54:53 +02001877 update_divide_count(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001878 atomic_set(&apic->lapic_timer.pending, 0);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001879 if (kvm_vcpu_is_bsp(vcpu))
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03001880 kvm_lapic_set_base(vcpu,
1881 vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03001882 vcpu->arch.pv_eoi.msr_val = 0;
Eddie Dong97222cc2007-09-12 10:58:04 +03001883 apic_update_ppr(apic);
1884
Gleb Natapove1035712009-03-05 16:34:59 +02001885 vcpu->arch.apic_arb_prio = 0;
Gleb Natapov41383772012-04-19 14:06:29 +03001886 vcpu->arch.apic_attention = 0;
Gleb Natapove1035712009-03-05 16:34:59 +02001887
Nadav Amit98eff522014-06-29 12:28:51 +03001888 apic_debug("%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001889 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +03001890 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001891 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +03001892}
1893
Eddie Dong97222cc2007-09-12 10:58:04 +03001894/*
1895 *----------------------------------------------------------------------
1896 * timer interface
1897 *----------------------------------------------------------------------
1898 */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001899
Avi Kivity2a6eac92012-07-26 18:01:51 +03001900static bool lapic_is_periodic(struct kvm_lapic *apic)
Eddie Dong97222cc2007-09-12 10:58:04 +03001901{
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001902 return apic_lvtt_period(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001903}
1904
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001905int apic_has_pending_timer(struct kvm_vcpu *vcpu)
1906{
Gleb Natapov54e98182012-08-05 15:58:32 +03001907 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001908
Paolo Bonzini1e3161b42016-01-08 13:41:16 +01001909 if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
Gleb Natapov54e98182012-08-05 15:58:32 +03001910 return atomic_read(&apic->lapic_timer.pending);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001911
1912 return 0;
1913}
1914
Avi Kivity89342082011-11-10 14:57:21 +02001915int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Eddie Dong1b9778d2007-09-03 16:56:58 +03001916{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05001917 u32 reg = kvm_lapic_get_reg(apic, lvt_type);
Jan Kiszka23930f92008-09-26 09:30:52 +02001918 int vector, mode, trig_mode;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001919
Gleb Natapovc48f1492012-08-05 15:58:33 +03001920 if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
Jan Kiszka23930f92008-09-26 09:30:52 +02001921 vector = reg & APIC_VECTOR_MASK;
1922 mode = reg & APIC_MODE_MASK;
1923 trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
Yang Zhangb4f22252013-04-11 19:21:37 +08001924 return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
1925 NULL);
Jan Kiszka23930f92008-09-26 09:30:52 +02001926 }
1927 return 0;
1928}
1929
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001930void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
Jan Kiszka23930f92008-09-26 09:30:52 +02001931{
Jan Kiszka8fdb2352008-10-20 10:20:02 +02001932 struct kvm_lapic *apic = vcpu->arch.apic;
1933
1934 if (apic)
1935 kvm_apic_local_deliver(apic, APIC_LVT0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001936}
1937
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001938static const struct kvm_io_device_ops apic_mmio_ops = {
1939 .read = apic_mmio_read,
1940 .write = apic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001941};
1942
Avi Kivitye9d90d42012-07-26 18:01:50 +03001943static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
1944{
1945 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
Avi Kivity2a6eac92012-07-26 18:01:51 +03001946 struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001947
Radim Krčmář5d87db72014-10-10 19:15:08 +02001948 apic_timer_expired(apic);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001949
Avi Kivity2a6eac92012-07-26 18:01:51 +03001950 if (lapic_is_periodic(apic)) {
Avi Kivitye9d90d42012-07-26 18:01:50 +03001951 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
1952 return HRTIMER_RESTART;
1953 } else
1954 return HRTIMER_NORESTART;
1955}
1956
Eddie Dong97222cc2007-09-12 10:58:04 +03001957int kvm_create_lapic(struct kvm_vcpu *vcpu)
1958{
1959 struct kvm_lapic *apic;
1960
1961 ASSERT(vcpu != NULL);
1962 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1963
1964 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1965 if (!apic)
1966 goto nomem;
1967
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001968 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001969
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09001970 apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
1971 if (!apic->regs) {
Eddie Dong97222cc2007-09-12 10:58:04 +03001972 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1973 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001974 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001975 }
Eddie Dong97222cc2007-09-12 10:58:04 +03001976 apic->vcpu = vcpu;
1977
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001978 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04001979 HRTIMER_MODE_ABS_PINNED);
Avi Kivitye9d90d42012-07-26 18:01:50 +03001980 apic->lapic_timer.timer.function = apic_timer_fn;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001981
Gleb Natapovc5cc4212012-08-05 15:58:30 +03001982 /*
1983 * APIC is created enabled. This will prevent kvm_lapic_set_base from
1984 * thinking that APIC satet has changed.
1985 */
1986 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03001987 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001988 kvm_lapic_reset(vcpu, false);
Gregory Haskinsd76685c2009-06-01 12:54:50 -04001989 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
Eddie Dong97222cc2007-09-12 10:58:04 +03001990
1991 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001992nomem_free_apic:
1993 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001994nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001995 return -ENOMEM;
1996}
Eddie Dong97222cc2007-09-12 10:58:04 +03001997
1998int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1999{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002000 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002001 int highest_irr;
2002
Wanpeng Li997ef642019-07-02 17:25:02 +08002003 if (!kvm_apic_hw_enabled(apic))
Eddie Dong97222cc2007-09-12 10:58:04 +03002004 return -1;
2005
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002006 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002007 highest_irr = apic_find_highest_irr(apic);
2008 if ((highest_irr == -1) ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002009 ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
Eddie Dong97222cc2007-09-12 10:58:04 +03002010 return -1;
2011 return highest_irr;
2012}
2013
Qing He40487c62007-09-17 14:47:13 +08002014int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
2015{
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002016 u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08002017 int r = 0;
2018
Gleb Natapovc48f1492012-08-05 15:58:33 +03002019 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
Chris Lalancettee7dca5c2010-06-16 17:11:12 -04002020 r = 1;
2021 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2022 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2023 r = 1;
Qing He40487c62007-09-17 14:47:13 +08002024 return r;
2025}
2026
Eddie Dong1b9778d2007-09-03 16:56:58 +03002027void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
2028{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002029 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002030
Gleb Natapov54e98182012-08-05 15:58:32 +03002031 if (atomic_read(&apic->lapic_timer.pending) > 0) {
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002032 kvm_apic_local_deliver(apic, APIC_LVTT);
Nadav Amitfae0ba22014-08-18 22:42:13 +03002033 if (apic_lvtt_tscdeadline(apic))
2034 apic->lapic_timer.tscdeadline = 0;
Jan Kiszkaf1ed0452013-04-28 14:00:41 +02002035 atomic_set(&apic->lapic_timer.pending, 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002036 }
2037}
2038
Eddie Dong97222cc2007-09-12 10:58:04 +03002039int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
2040{
2041 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002042 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03002043
2044 if (vector == -1)
2045 return -1;
2046
Wanpeng Li56cc2402014-08-05 12:42:24 +08002047 /*
2048 * We get here even with APIC virtualization enabled, if doing
2049 * nested virtualization and L1 runs with the "acknowledge interrupt
2050 * on exit" mode. Then we cannot inject the interrupt via RVI,
2051 * because the process would deliver it through the IDT.
2052 */
2053
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002054 apic_set_isr(vector, apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03002055 apic_update_ppr(apic);
2056 apic_clear_irr(vector, apic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002057
2058 if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
2059 apic_clear_isr(vector, apic);
2060 apic_update_ppr(apic);
2061 }
2062
Eddie Dong97222cc2007-09-12 10:58:04 +03002063 return vector;
2064}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002065
Radim Krčmářa92e2542016-07-12 22:09:22 +02002066static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
2067 struct kvm_lapic_state *s, bool set)
2068{
2069 if (apic_x2apic_mode(vcpu->arch.apic)) {
2070 u32 *id = (u32 *)(s->regs + APIC_ID);
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002071 u32 *ldr = (u32 *)(s->regs + APIC_LDR);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002072
Radim Krčmář371313132016-07-12 22:09:27 +02002073 if (vcpu->kvm->arch.x2apic_format) {
2074 if (*id != vcpu->vcpu_id)
2075 return -EINVAL;
2076 } else {
2077 if (set)
2078 *id >>= 24;
2079 else
2080 *id <<= 24;
2081 }
Dr. David Alan Gilbert9aad7572017-11-17 11:52:50 +00002082
2083 /* In x2APIC mode, the LDR is fixed and based on the id */
2084 if (set)
2085 *ldr = kvm_apic_calc_x2apic_ldr(*id);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002086 }
2087
2088 return 0;
2089}
2090
2091int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
2092{
2093 memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2094 return kvm_apic_state_fixup(vcpu, s, false);
2095}
2096
2097int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002098{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002099 struct kvm_lapic *apic = vcpu->arch.apic;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002100 int r;
2101
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002102
Gleb Natapov5dbc8f32012-08-05 15:58:27 +03002103 kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
Gleb Natapov64eb0622012-08-08 15:24:36 +03002104 /* set SPIV separately to get count of SW disabled APICs right */
2105 apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
Radim Krčmářa92e2542016-07-12 22:09:22 +02002106
2107 r = kvm_apic_state_fixup(vcpu, s, true);
2108 if (r)
2109 return r;
Gleb Natapov64eb0622012-08-08 15:24:36 +03002110 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Radim Krčmářa92e2542016-07-12 22:09:22 +02002111
2112 recalculate_apic_map(vcpu->kvm);
Gleb Natapovfc61b802009-07-05 17:39:35 +03002113 kvm_apic_set_version(vcpu);
2114
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002115 apic_update_ppr(apic);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03002116 hrtimer_cancel(&apic->lapic_timer.timer);
Radim Krčmářb6ac0692015-06-05 20:57:41 +02002117 apic_update_lvtt(apic);
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002118 apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002119 update_divide_count(apic);
2120 start_apic_timer(apic);
Marcelo Tosatti6e24a6e2009-12-14 17:37:35 -02002121 apic->irr_pending = true;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002122 apic->isr_count = vcpu->arch.apicv_active ?
Yang Zhangc7c9c562013-01-25 10:18:51 +08002123 1 : count_vectors(apic->regs + APIC_ISR);
Michael S. Tsirkin8680b942012-06-24 19:24:26 +03002124 apic->highest_isr_cache = -1;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002125 if (vcpu->arch.apicv_active) {
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05002126 if (kvm_x86_ops->apicv_post_state_restore)
2127 kvm_x86_ops->apicv_post_state_restore(vcpu);
Wei Wang4114c272014-11-05 10:53:43 +08002128 kvm_x86_ops->hwapic_irr_update(vcpu,
2129 apic_find_highest_irr(apic));
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02002130 kvm_x86_ops->hwapic_isr_update(vcpu,
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01002131 apic_find_highest_isr(apic));
Andrey Smetanind62caab2015-11-10 15:36:33 +03002132 }
Avi Kivity3842d132010-07-27 12:30:24 +03002133 kvm_make_request(KVM_REQ_EVENT, vcpu);
Steve Rutherford49df6392015-07-29 23:21:40 -07002134 if (ioapic_in_kernel(vcpu->kvm))
2135 kvm_rtc_eoi_tracking_restore_one(vcpu);
Radim Krčmář0669a512015-10-30 15:48:20 +01002136
2137 vcpu->arch.apic_arb_prio = 0;
Radim Krčmářa92e2542016-07-12 22:09:22 +02002138
2139 return 0;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002140}
Eddie Donga3d7f852007-09-03 16:15:12 +03002141
Avi Kivity2f52d582008-01-16 12:49:30 +02002142void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03002143{
Eddie Donga3d7f852007-09-03 16:15:12 +03002144 struct hrtimer *timer;
2145
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002146 if (!lapic_in_kernel(vcpu))
Eddie Donga3d7f852007-09-03 16:15:12 +03002147 return;
2148
Gleb Natapov54e98182012-08-05 15:58:32 +03002149 timer = &vcpu->arch.apic->lapic_timer.timer;
Eddie Donga3d7f852007-09-03 16:15:12 +03002150 if (hrtimer_cancel(timer))
Luiz Capitulino61abdbe2016-04-04 16:46:07 -04002151 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Eddie Donga3d7f852007-09-03 16:15:12 +03002152}
Avi Kivityb93463a2007-10-25 16:52:32 +02002153
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002154/*
2155 * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
2156 *
2157 * Detect whether guest triggered PV EOI since the
2158 * last entry. If yes, set EOI on guests's behalf.
2159 * Clear PV EOI in guest memory in any case.
2160 */
2161static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
2162 struct kvm_lapic *apic)
2163{
2164 bool pending;
2165 int vector;
2166 /*
2167 * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
2168 * and KVM_PV_EOI_ENABLED in guest memory as follows:
2169 *
2170 * KVM_APIC_PV_EOI_PENDING is unset:
2171 * -> host disabled PV EOI.
2172 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
2173 * -> host enabled PV EOI, guest did not execute EOI yet.
2174 * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
2175 * -> host enabled PV EOI, guest executed EOI.
2176 */
2177 BUG_ON(!pv_eoi_enabled(vcpu));
2178 pending = pv_eoi_get_pending(vcpu);
2179 /*
2180 * Clear pending bit in any case: it will be set again on vmentry.
2181 * While this might not be ideal from performance point of view,
2182 * this makes sure pv eoi is only enabled when we know it's safe.
2183 */
2184 pv_eoi_clr_pending(vcpu);
2185 if (pending)
2186 return;
2187 vector = apic_set_eoi(apic);
2188 trace_kvm_pv_eoi(apic, vector);
2189}
2190
Avi Kivityb93463a2007-10-25 16:52:32 +02002191void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
2192{
2193 u32 data;
Avi Kivityb93463a2007-10-25 16:52:32 +02002194
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002195 if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
2196 apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
2197
Gleb Natapov41383772012-04-19 14:06:29 +03002198 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002199 return;
2200
Nicholas Krause603242a2015-08-05 10:44:40 -04002201 if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2202 sizeof(u32)))
2203 return;
Avi Kivityb93463a2007-10-25 16:52:32 +02002204
2205 apic_set_tpr(vcpu->arch.apic, data & 0xff);
2206}
2207
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002208/*
2209 * apic_sync_pv_eoi_to_guest - called before vmentry
2210 *
2211 * Detect whether it's safe to enable PV EOI and
2212 * if yes do so.
2213 */
2214static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
2215 struct kvm_lapic *apic)
2216{
2217 if (!pv_eoi_enabled(vcpu) ||
2218 /* IRR set or many bits in ISR: could be nested. */
2219 apic->irr_pending ||
2220 /* Cache not set: could be safe but we don't bother. */
2221 apic->highest_isr_cache == -1 ||
2222 /* Need EOI to update ioapic. */
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02002223 kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002224 /*
2225 * PV EOI was disabled by apic_sync_pv_eoi_from_guest
2226 * so we need not do anything here.
2227 */
2228 return;
2229 }
2230
2231 pv_eoi_set_pending(apic->vcpu);
2232}
2233
Avi Kivityb93463a2007-10-25 16:52:32 +02002234void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
2235{
2236 u32 data, tpr;
2237 int max_irr, max_isr;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002238 struct kvm_lapic *apic = vcpu->arch.apic;
Avi Kivityb93463a2007-10-25 16:52:32 +02002239
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002240 apic_sync_pv_eoi_to_guest(vcpu, apic);
2241
Gleb Natapov41383772012-04-19 14:06:29 +03002242 if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
Avi Kivityb93463a2007-10-25 16:52:32 +02002243 return;
2244
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -05002245 tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
Avi Kivityb93463a2007-10-25 16:52:32 +02002246 max_irr = apic_find_highest_irr(apic);
2247 if (max_irr < 0)
2248 max_irr = 0;
2249 max_isr = apic_find_highest_isr(apic);
2250 if (max_isr < 0)
2251 max_isr = 0;
2252 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
2253
Andy Honigfda4e2e82013-11-20 10:23:22 -08002254 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
2255 sizeof(u32));
Avi Kivityb93463a2007-10-25 16:52:32 +02002256}
2257
Andy Honigfda4e2e82013-11-20 10:23:22 -08002258int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
Avi Kivityb93463a2007-10-25 16:52:32 +02002259{
Andy Honigfda4e2e82013-11-20 10:23:22 -08002260 if (vapic_addr) {
2261 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
2262 &vcpu->arch.apic->vapic_cache,
2263 vapic_addr, sizeof(u32)))
2264 return -EINVAL;
Gleb Natapov41383772012-04-19 14:06:29 +03002265 __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002266 } else {
Gleb Natapov41383772012-04-19 14:06:29 +03002267 __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
Andy Honigfda4e2e82013-11-20 10:23:22 -08002268 }
2269
2270 vcpu->arch.apic->vapic_addr = vapic_addr;
2271 return 0;
Avi Kivityb93463a2007-10-25 16:52:32 +02002272}
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002273
2274int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
2275{
2276 struct kvm_lapic *apic = vcpu->arch.apic;
2277 u32 reg = (msr - APIC_BASE_MSR) << 4;
2278
Paolo Bonzini35754c92015-07-29 12:05:37 +02002279 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002280 return 1;
2281
Nadav Amitc69d3d92014-11-26 17:56:25 +02002282 if (reg == APIC_ICR2)
2283 return 1;
2284
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002285 /* if this is ICR write vector before command */
Radim Krčmářdecdc282014-11-26 17:07:05 +01002286 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002287 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2288 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002289}
2290
2291int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
2292{
2293 struct kvm_lapic *apic = vcpu->arch.apic;
2294 u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
2295
Paolo Bonzini35754c92015-07-29 12:05:37 +02002296 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002297 return 1;
2298
Nadav Amitc69d3d92014-11-26 17:56:25 +02002299 if (reg == APIC_DFR || reg == APIC_ICR2) {
2300 apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
2301 reg);
2302 return 1;
2303 }
2304
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002305 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002306 return 1;
Radim Krčmářdecdc282014-11-26 17:07:05 +01002307 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002308 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002309
2310 *data = (((u64)high) << 32) | low;
2311
2312 return 0;
2313}
Gleb Natapov10388a02010-01-17 15:51:23 +02002314
2315int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
2316{
2317 struct kvm_lapic *apic = vcpu->arch.apic;
2318
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002319 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002320 return 1;
2321
2322 /* if this is ICR write vector before command */
2323 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002324 kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
2325 return kvm_lapic_reg_write(apic, reg, (u32)data);
Gleb Natapov10388a02010-01-17 15:51:23 +02002326}
2327
2328int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
2329{
2330 struct kvm_lapic *apic = vcpu->arch.apic;
2331 u32 low, high = 0;
2332
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002333 if (!lapic_in_kernel(vcpu))
Gleb Natapov10388a02010-01-17 15:51:23 +02002334 return 1;
2335
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002336 if (kvm_lapic_reg_read(apic, reg, 4, &low))
Gleb Natapov10388a02010-01-17 15:51:23 +02002337 return 1;
2338 if (reg == APIC_ICR)
Suravee Suthikulpanit1e6e2752016-05-04 14:09:40 -05002339 kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
Gleb Natapov10388a02010-01-17 15:51:23 +02002340
2341 *data = (((u64)high) << 32) | low;
2342
2343 return 0;
2344}
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002345
2346int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
2347{
2348 u64 addr = data & ~KVM_MSR_ENABLED;
2349 if (!IS_ALIGNED(addr, 4))
2350 return 1;
2351
2352 vcpu->arch.pv_eoi.msr_val = data;
2353 if (!pv_eoi_enabled(vcpu))
2354 return 0;
2355 return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
Andrew Honig8f964522013-03-29 09:35:21 -07002356 addr, sizeof(u8));
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002357}
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002358
Jan Kiszka66450a22013-03-13 12:42:34 +01002359void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
2360{
2361 struct kvm_lapic *apic = vcpu->arch.apic;
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01002362 u8 sipi_vector;
Gleb Natapov299018f2013-06-03 11:30:02 +03002363 unsigned long pe;
Jan Kiszka66450a22013-03-13 12:42:34 +01002364
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01002365 if (!lapic_in_kernel(vcpu) || !apic->pending_events)
Jan Kiszka66450a22013-03-13 12:42:34 +01002366 return;
2367
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002368 /*
2369 * INITs are latched while in SMM. Because an SMM CPU cannot
2370 * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
2371 * and delay processing of INIT until the next RSM.
2372 */
2373 if (is_smm(vcpu)) {
2374 WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
2375 if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
2376 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
2377 return;
2378 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002379
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02002380 pe = xchg(&apic->pending_events, 0);
Gleb Natapov299018f2013-06-03 11:30:02 +03002381 if (test_bit(KVM_APIC_INIT, &pe)) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002382 kvm_lapic_reset(vcpu, true);
2383 kvm_vcpu_reset(vcpu, true);
Jan Kiszka66450a22013-03-13 12:42:34 +01002384 if (kvm_vcpu_is_bsp(apic->vcpu))
2385 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2386 else
2387 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
2388 }
Gleb Natapov299018f2013-06-03 11:30:02 +03002389 if (test_bit(KVM_APIC_SIPI, &pe) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01002390 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
2391 /* evaluate pending_events before reading the vector */
2392 smp_rmb();
2393 sipi_vector = apic->sipi_vector;
Nadav Amit98eff522014-06-29 12:28:51 +03002394 apic_debug("vcpu %d received sipi with vector # %x\n",
Jan Kiszka66450a22013-03-13 12:42:34 +01002395 vcpu->vcpu_id, sipi_vector);
2396 kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
2397 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2398 }
2399}
2400
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002401void kvm_lapic_init(void)
2402{
2403 /* do not patch jump label more than once per second */
2404 jump_label_rate_limit(&apic_hw_disabled, HZ);
Gleb Natapovf8c1ea12012-08-05 15:58:31 +03002405 jump_label_rate_limit(&apic_sw_disabled, HZ);
Gleb Natapovc5cc4212012-08-05 15:58:30 +03002406}
David Matlack5ed21cc2016-12-16 14:30:36 -08002407
2408void kvm_lapic_exit(void)
2409{
2410 static_key_deferred_flush(&apic_hw_disabled);
2411 static_key_deferred_flush(&apic_sw_disabled);
2412}