Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Local APIC virtualization |
| 4 | * |
| 5 | * Copyright (C) 2006 Qumranet, Inc. |
| 6 | * Copyright (C) 2007 Novell |
| 7 | * Copyright (C) 2007 Intel |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2009 Red Hat, Inc. and/or its affiliates. |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 9 | * |
| 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 Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 21 | #include <linux/kvm_host.h> |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 22 | #include <linux/kvm.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/highmem.h> |
| 25 | #include <linux/smp.h> |
| 26 | #include <linux/hrtimer.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/module.h> |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 29 | #include <linux/math64.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 31 | #include <asm/processor.h> |
| 32 | #include <asm/msr.h> |
| 33 | #include <asm/page.h> |
| 34 | #include <asm/current.h> |
| 35 | #include <asm/apicdef.h> |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 36 | #include <asm/delay.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 37 | #include <linux/atomic.h> |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 38 | #include <linux/jump_label.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 39 | #include "kvm_cache_regs.h" |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 40 | #include "irq.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 41 | #include "trace.h" |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 42 | #include "x86.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 43 | #include "cpuid.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 44 | #include "hyperv.h" |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 45 | |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 46 | #ifndef CONFIG_X86_64 |
| 47 | #define mod_64(x, y) ((x) - (y) * div64_u64(x, y)) |
| 48 | #else |
| 49 | #define mod_64(x, y) ((x) % (y)) |
| 50 | #endif |
| 51 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 52 | #define PRId64 "d" |
| 53 | #define PRIx64 "llx" |
| 54 | #define PRIu64 "u" |
| 55 | #define PRIo64 "o" |
| 56 | |
| 57 | #define APIC_BUS_CYCLE_NS 1 |
| 58 | |
| 59 | /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */ |
| 60 | #define apic_debug(fmt, arg...) |
| 61 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 62 | /* 14 is the version for Xeon and Pentium 8.4.8*/ |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 63 | #define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 64 | #define LAPIC_MMIO_LENGTH (1 << 12) |
| 65 | /* followed define is not in apicdef.h */ |
| 66 | #define APIC_SHORT_MASK 0xc0000 |
| 67 | #define APIC_DEST_NOSHORT 0x0 |
| 68 | #define APIC_DEST_MASK 0x800 |
| 69 | #define MAX_APIC_VECTOR 256 |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 70 | #define APIC_VECTORS_PER_REG 32 |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 71 | |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 72 | #define APIC_BROADCAST 0xFF |
| 73 | #define X2APIC_BROADCAST 0xFFFFFFFFul |
| 74 | |
Michael S. Tsirkin | a0c9a822 | 2012-04-11 18:49:55 +0300 | [diff] [blame] | 75 | static inline int apic_test_vector(int vec, void *bitmap) |
| 76 | { |
| 77 | return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 78 | } |
| 79 | |
Yang Zhang | 1060691 | 2013-04-11 19:21:38 +0800 | [diff] [blame] | 80 | bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector) |
| 81 | { |
| 82 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 83 | |
| 84 | return apic_test_vector(vector, apic->regs + APIC_ISR) || |
| 85 | apic_test_vector(vector, apic->regs + APIC_IRR); |
| 86 | } |
| 87 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 88 | static inline void apic_clear_vector(int vec, void *bitmap) |
| 89 | { |
| 90 | clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 91 | } |
| 92 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 93 | static inline int __apic_test_and_set_vector(int vec, void *bitmap) |
| 94 | { |
| 95 | return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 96 | } |
| 97 | |
| 98 | static inline int __apic_test_and_clear_vector(int vec, void *bitmap) |
| 99 | { |
| 100 | return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 101 | } |
| 102 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 103 | struct static_key_deferred apic_hw_disabled __read_mostly; |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 104 | struct static_key_deferred apic_sw_disabled __read_mostly; |
| 105 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 106 | static inline int apic_enabled(struct kvm_lapic *apic) |
| 107 | { |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 108 | return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic); |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 109 | } |
| 110 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 111 | #define LVT_MASK \ |
| 112 | (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK) |
| 113 | |
| 114 | #define LINT_MASK \ |
| 115 | (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \ |
| 116 | APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER) |
| 117 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 118 | static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, |
| 119 | u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { |
| 120 | switch (map->mode) { |
| 121 | case KVM_APIC_MODE_X2APIC: { |
| 122 | u32 offset = (dest_id >> 16) * 16; |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 123 | u32 max_apic_id = map->max_apic_id; |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 124 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 125 | if (offset <= max_apic_id) { |
| 126 | u8 cluster_size = min(max_apic_id - offset + 1, 16U); |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 127 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 128 | *cluster = &map->phys_map[offset]; |
| 129 | *mask = dest_id & (0xffff >> (16 - cluster_size)); |
| 130 | } else { |
| 131 | *mask = 0; |
| 132 | } |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 133 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | case KVM_APIC_MODE_XAPIC_FLAT: |
| 137 | *cluster = map->xapic_flat_map; |
| 138 | *mask = dest_id & 0xff; |
| 139 | return true; |
| 140 | case KVM_APIC_MODE_XAPIC_CLUSTER: |
| 141 | *cluster = map->xapic_cluster_map[dest_id >> 4]; |
| 142 | *mask = dest_id & 0xf; |
| 143 | return true; |
| 144 | default: |
| 145 | /* Not optimized. */ |
| 146 | return false; |
| 147 | } |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 150 | static void recalculate_apic_map(struct kvm *kvm) |
| 151 | { |
| 152 | struct kvm_apic_map *new, *old = NULL; |
| 153 | struct kvm_vcpu *vcpu; |
| 154 | int i; |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 155 | u32 max_id = 255; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 156 | |
| 157 | mutex_lock(&kvm->arch.apic_map_lock); |
| 158 | |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 159 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 160 | if (kvm_apic_present(vcpu)) |
| 161 | max_id = max(max_id, kvm_apic_id(vcpu->arch.apic)); |
| 162 | |
| 163 | new = kzalloc(sizeof(struct kvm_apic_map) + |
| 164 | sizeof(struct kvm_lapic *) * (max_id + 1), GFP_KERNEL); |
| 165 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 166 | if (!new) |
| 167 | goto out; |
| 168 | |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 169 | new->max_apic_id = max_id; |
| 170 | |
Nadav Amit | 173beed | 2014-11-02 11:54:54 +0200 | [diff] [blame] | 171 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 172 | struct kvm_lapic *apic = vcpu->arch.apic; |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 173 | struct kvm_lapic **cluster; |
| 174 | u16 mask; |
Radim Krčmář | 25995e5 | 2014-11-27 23:30:19 +0100 | [diff] [blame] | 175 | u32 ldr, aid; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 176 | |
Radim Krčmář | df04d1d | 2015-01-29 22:33:35 +0100 | [diff] [blame] | 177 | if (!kvm_apic_present(vcpu)) |
| 178 | continue; |
| 179 | |
Radim Krčmář | 25995e5 | 2014-11-27 23:30:19 +0100 | [diff] [blame] | 180 | aid = kvm_apic_id(apic); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 181 | ldr = kvm_lapic_get_reg(apic, APIC_LDR); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 182 | |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 183 | if (aid <= new->max_apic_id) |
Radim Krčmář | 25995e5 | 2014-11-27 23:30:19 +0100 | [diff] [blame] | 184 | new->phys_map[aid] = apic; |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 185 | |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 186 | if (apic_x2apic_mode(apic)) { |
| 187 | new->mode |= KVM_APIC_MODE_X2APIC; |
| 188 | } else if (ldr) { |
| 189 | ldr = GET_APIC_LOGICAL_ID(ldr); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 190 | if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT) |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 191 | new->mode |= KVM_APIC_MODE_XAPIC_FLAT; |
| 192 | else |
| 193 | new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER; |
| 194 | } |
| 195 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 196 | if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask)) |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 197 | continue; |
| 198 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 199 | if (mask) |
| 200 | cluster[ffs(mask) - 1] = apic; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 201 | } |
| 202 | out: |
| 203 | old = rcu_dereference_protected(kvm->arch.apic_map, |
| 204 | lockdep_is_held(&kvm->arch.apic_map_lock)); |
| 205 | rcu_assign_pointer(kvm->arch.apic_map, new); |
| 206 | mutex_unlock(&kvm->arch.apic_map_lock); |
| 207 | |
| 208 | if (old) |
| 209 | kfree_rcu(old, rcu); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 210 | |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 211 | kvm_make_scan_ioapic_request(kvm); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 214 | static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val) |
| 215 | { |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 216 | bool enabled = val & APIC_SPIV_APIC_ENABLED; |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 217 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 218 | kvm_lapic_set_reg(apic, APIC_SPIV, val); |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 219 | |
| 220 | if (enabled != apic->sw_enabled) { |
| 221 | apic->sw_enabled = enabled; |
| 222 | if (enabled) { |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 223 | static_key_slow_dec_deferred(&apic_sw_disabled); |
| 224 | recalculate_apic_map(apic->vcpu->kvm); |
| 225 | } else |
| 226 | static_key_slow_inc(&apic_sw_disabled.key); |
| 227 | } |
| 228 | } |
| 229 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 230 | static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 231 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 232 | kvm_lapic_set_reg(apic, APIC_ID, id << 24); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 233 | recalculate_apic_map(apic->vcpu->kvm); |
| 234 | } |
| 235 | |
| 236 | static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id) |
| 237 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 238 | kvm_lapic_set_reg(apic, APIC_LDR, id); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 239 | recalculate_apic_map(apic->vcpu->kvm); |
| 240 | } |
| 241 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 242 | static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id) |
Radim Krčmář | 257b9a5 | 2015-05-22 18:45:11 +0200 | [diff] [blame] | 243 | { |
| 244 | u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf)); |
| 245 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 246 | kvm_lapic_set_reg(apic, APIC_ID, id); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 247 | kvm_lapic_set_reg(apic, APIC_LDR, ldr); |
Radim Krčmář | 257b9a5 | 2015-05-22 18:45:11 +0200 | [diff] [blame] | 248 | recalculate_apic_map(apic->vcpu->kvm); |
| 249 | } |
| 250 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 251 | static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type) |
| 252 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 253 | return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type) |
| 257 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 258 | return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 259 | } |
| 260 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 261 | static inline int apic_lvtt_oneshot(struct kvm_lapic *apic) |
| 262 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 263 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 266 | static inline int apic_lvtt_period(struct kvm_lapic *apic) |
| 267 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 268 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic) |
| 272 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 273 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 274 | } |
| 275 | |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 276 | static inline int apic_lvt_nmi_mode(u32 lvt_val) |
| 277 | { |
| 278 | return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI; |
| 279 | } |
| 280 | |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 281 | void kvm_apic_set_version(struct kvm_vcpu *vcpu) |
| 282 | { |
| 283 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 284 | struct kvm_cpuid_entry2 *feat; |
| 285 | u32 v = APIC_VERSION; |
| 286 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 287 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 288 | return; |
| 289 | |
| 290 | feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0); |
| 291 | if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31)))) |
| 292 | v |= APIC_LVR_DIRECTED_EOI; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 293 | kvm_lapic_set_reg(apic, APIC_LVR, v); |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 294 | } |
| 295 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 296 | static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = { |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 297 | LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 298 | LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */ |
| 299 | LVT_MASK | APIC_MODE_MASK, /* LVTPC */ |
| 300 | LINT_MASK, LINT_MASK, /* LVT0-1 */ |
| 301 | LVT_MASK /* LVTERR */ |
| 302 | }; |
| 303 | |
| 304 | static int find_highest_vector(void *bitmap) |
| 305 | { |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 306 | int vec; |
| 307 | u32 *reg; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 308 | |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 309 | for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG; |
| 310 | vec >= 0; vec -= APIC_VECTORS_PER_REG) { |
| 311 | reg = bitmap + REG_POS(vec); |
| 312 | if (*reg) |
| 313 | return fls(*reg) - 1 + vec; |
| 314 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 315 | |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 316 | return -1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 317 | } |
| 318 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 319 | static u8 count_vectors(void *bitmap) |
| 320 | { |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 321 | int vec; |
| 322 | u32 *reg; |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 323 | u8 count = 0; |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 324 | |
| 325 | for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) { |
| 326 | reg = bitmap + REG_POS(vec); |
| 327 | count += hweight32(*reg); |
| 328 | } |
| 329 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 330 | return count; |
| 331 | } |
| 332 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 333 | void __kvm_apic_update_irr(u32 *pir, void *regs) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 334 | { |
| 335 | u32 i, pir_val; |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 336 | |
| 337 | for (i = 0; i <= 7; i++) { |
| 338 | pir_val = xchg(&pir[i], 0); |
| 339 | if (pir_val) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 340 | *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val; |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 341 | } |
| 342 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(__kvm_apic_update_irr); |
| 344 | |
| 345 | void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir) |
| 346 | { |
| 347 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 348 | |
| 349 | __kvm_apic_update_irr(pir, apic->regs); |
Radim Krčmář | c77f3fa | 2015-10-08 20:23:33 +0200 | [diff] [blame] | 350 | |
| 351 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 352 | } |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 353 | EXPORT_SYMBOL_GPL(kvm_apic_update_irr); |
| 354 | |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 355 | static inline int apic_search_irr(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 356 | { |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 357 | return find_highest_vector(apic->regs + APIC_IRR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static inline int apic_find_highest_irr(struct kvm_lapic *apic) |
| 361 | { |
| 362 | int result; |
| 363 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 364 | /* |
| 365 | * Note that irr_pending is just a hint. It will be always |
| 366 | * true with virtual interrupt delivery enabled. |
| 367 | */ |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 368 | if (!apic->irr_pending) |
| 369 | return -1; |
| 370 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 371 | if (apic->vcpu->arch.apicv_active) |
| 372 | kvm_x86_ops->sync_pir_to_irr(apic->vcpu); |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 373 | result = apic_search_irr(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 374 | ASSERT(result == -1 || result >= 16); |
| 375 | |
| 376 | return result; |
| 377 | } |
| 378 | |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 379 | static inline void apic_clear_irr(int vec, struct kvm_lapic *apic) |
| 380 | { |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 381 | struct kvm_vcpu *vcpu; |
| 382 | |
| 383 | vcpu = apic->vcpu; |
| 384 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 385 | if (unlikely(vcpu->arch.apicv_active)) { |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 386 | /* try to update RVI */ |
Nadav Amit | f210f75 | 2014-11-16 23:49:07 +0200 | [diff] [blame] | 387 | apic_clear_vector(vec, apic->regs + APIC_IRR); |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 388 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Nadav Amit | f210f75 | 2014-11-16 23:49:07 +0200 | [diff] [blame] | 389 | } else { |
| 390 | apic->irr_pending = false; |
| 391 | apic_clear_vector(vec, apic->regs + APIC_IRR); |
| 392 | if (apic_search_irr(apic) != -1) |
| 393 | apic->irr_pending = true; |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 394 | } |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 395 | } |
| 396 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 397 | static inline void apic_set_isr(int vec, struct kvm_lapic *apic) |
| 398 | { |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 399 | struct kvm_vcpu *vcpu; |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 400 | |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 401 | if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR)) |
| 402 | return; |
| 403 | |
| 404 | vcpu = apic->vcpu; |
| 405 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 406 | /* |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 407 | * With APIC virtualization enabled, all caching is disabled |
| 408 | * because the processor can modify ISR under the hood. Instead |
| 409 | * just set SVI. |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 410 | */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 411 | if (unlikely(vcpu->arch.apicv_active)) |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 412 | kvm_x86_ops->hwapic_isr_update(vcpu, vec); |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 413 | else { |
| 414 | ++apic->isr_count; |
| 415 | BUG_ON(apic->isr_count > MAX_APIC_VECTOR); |
| 416 | /* |
| 417 | * ISR (in service register) bit is set when injecting an interrupt. |
| 418 | * The highest vector is injected. Thus the latest bit set matches |
| 419 | * the highest bit in ISR. |
| 420 | */ |
| 421 | apic->highest_isr_cache = vec; |
| 422 | } |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 423 | } |
| 424 | |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 425 | static inline int apic_find_highest_isr(struct kvm_lapic *apic) |
| 426 | { |
| 427 | int result; |
| 428 | |
| 429 | /* |
| 430 | * Note that isr_count is always 1, and highest_isr_cache |
| 431 | * is always -1, with APIC virtualization enabled. |
| 432 | */ |
| 433 | if (!apic->isr_count) |
| 434 | return -1; |
| 435 | if (likely(apic->highest_isr_cache != -1)) |
| 436 | return apic->highest_isr_cache; |
| 437 | |
| 438 | result = find_highest_vector(apic->regs + APIC_ISR); |
| 439 | ASSERT(result == -1 || result >= 16); |
| 440 | |
| 441 | return result; |
| 442 | } |
| 443 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 444 | static inline void apic_clear_isr(int vec, struct kvm_lapic *apic) |
| 445 | { |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 446 | struct kvm_vcpu *vcpu; |
| 447 | if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR)) |
| 448 | return; |
| 449 | |
| 450 | vcpu = apic->vcpu; |
| 451 | |
| 452 | /* |
| 453 | * We do get here for APIC virtualization enabled if the guest |
| 454 | * uses the Hyper-V APIC enlightenment. In this case we may need |
| 455 | * to trigger a new interrupt delivery by writing the SVI field; |
| 456 | * on the other hand isr_count and highest_isr_cache are unused |
| 457 | * and must be left alone. |
| 458 | */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 459 | if (unlikely(vcpu->arch.apicv_active)) |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 460 | kvm_x86_ops->hwapic_isr_update(vcpu, |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 461 | apic_find_highest_isr(apic)); |
| 462 | else { |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 463 | --apic->isr_count; |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 464 | BUG_ON(apic->isr_count < 0); |
| 465 | apic->highest_isr_cache = -1; |
| 466 | } |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 467 | } |
| 468 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 469 | int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu) |
| 470 | { |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 471 | /* This may race with setting of irr in __apic_accept_irq() and |
| 472 | * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq |
| 473 | * will cause vmexit immediately and the value will be recalculated |
| 474 | * on the next vmentry. |
| 475 | */ |
Paolo Bonzini | f8543d6 | 2016-01-08 13:42:24 +0100 | [diff] [blame] | 476 | return apic_find_highest_irr(vcpu->arch.apic); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 477 | } |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 478 | |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 479 | static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 480 | int vector, int level, int trig_mode, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 481 | struct dest_map *dest_map); |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 482 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 483 | int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 484 | struct dest_map *dest_map) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 485 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 486 | struct kvm_lapic *apic = vcpu->arch.apic; |
Zhang Xiantao | 8be5453 | 2007-12-02 22:35:57 +0800 | [diff] [blame] | 487 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 488 | return __apic_accept_irq(apic, irq->delivery_mode, irq->vector, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 489 | irq->level, irq->trig_mode, dest_map); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 490 | } |
| 491 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 492 | static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val) |
| 493 | { |
| 494 | |
| 495 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val, |
| 496 | sizeof(val)); |
| 497 | } |
| 498 | |
| 499 | static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val) |
| 500 | { |
| 501 | |
| 502 | return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val, |
| 503 | sizeof(*val)); |
| 504 | } |
| 505 | |
| 506 | static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu) |
| 507 | { |
| 508 | return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; |
| 509 | } |
| 510 | |
| 511 | static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu) |
| 512 | { |
| 513 | u8 val; |
| 514 | if (pv_eoi_get_user(vcpu, &val) < 0) |
| 515 | apic_debug("Can't read EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 516 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 517 | return val & 0x1; |
| 518 | } |
| 519 | |
| 520 | static void pv_eoi_set_pending(struct kvm_vcpu *vcpu) |
| 521 | { |
| 522 | if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) { |
| 523 | apic_debug("Can't set EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 524 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention); |
| 528 | } |
| 529 | |
| 530 | static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu) |
| 531 | { |
| 532 | if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) { |
| 533 | apic_debug("Can't clear EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 534 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention); |
| 538 | } |
| 539 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 540 | static void apic_update_ppr(struct kvm_lapic *apic) |
| 541 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 542 | u32 tpr, isrv, ppr, old_ppr; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 543 | int isr; |
| 544 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 545 | old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI); |
| 546 | tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 547 | isr = apic_find_highest_isr(apic); |
| 548 | isrv = (isr != -1) ? isr : 0; |
| 549 | |
| 550 | if ((tpr & 0xf0) >= (isrv & 0xf0)) |
| 551 | ppr = tpr & 0xff; |
| 552 | else |
| 553 | ppr = isrv & 0xf0; |
| 554 | |
| 555 | apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x", |
| 556 | apic, ppr, isr, isrv); |
| 557 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 558 | if (old_ppr != ppr) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 559 | kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr); |
Avi Kivity | 83bcacb | 2010-10-25 15:23:55 +0200 | [diff] [blame] | 560 | if (ppr < old_ppr) |
| 561 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 562 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr) |
| 566 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 567 | kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 568 | apic_update_ppr(apic); |
| 569 | } |
| 570 | |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 571 | static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 572 | { |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 573 | if (apic_x2apic_mode(apic)) |
| 574 | return mda == X2APIC_BROADCAST; |
| 575 | |
| 576 | return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 577 | } |
| 578 | |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 579 | static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda) |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 580 | { |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 581 | if (kvm_apic_broadcast(apic, mda)) |
| 582 | return true; |
| 583 | |
| 584 | if (apic_x2apic_mode(apic)) |
| 585 | return mda == kvm_apic_id(apic); |
| 586 | |
| 587 | return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic)); |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 588 | } |
| 589 | |
Radim Krčmář | 52c233a | 2015-01-29 22:48:48 +0100 | [diff] [blame] | 590 | static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 591 | { |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 592 | u32 logical_id; |
| 593 | |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 594 | if (kvm_apic_broadcast(apic, mda)) |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 595 | return true; |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 596 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 597 | logical_id = kvm_lapic_get_reg(apic, APIC_LDR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 598 | |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 599 | if (apic_x2apic_mode(apic)) |
Radim Krčmář | 8a39536 | 2015-01-29 22:48:51 +0100 | [diff] [blame] | 600 | return ((logical_id >> 16) == (mda >> 16)) |
| 601 | && (logical_id & mda & 0xffff) != 0; |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 602 | |
| 603 | logical_id = GET_APIC_LOGICAL_ID(logical_id); |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 604 | mda = GET_APIC_DEST_FIELD(mda); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 605 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 606 | switch (kvm_lapic_get_reg(apic, APIC_DFR)) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 607 | case APIC_DFR_FLAT: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 608 | return (logical_id & mda) != 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 609 | case APIC_DFR_CLUSTER: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 610 | return ((logical_id >> 4) == (mda >> 4)) |
| 611 | && (logical_id & mda & 0xf) != 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 612 | default: |
Jan Kiszka | 7712de8 | 2011-09-12 11:25:51 +0200 | [diff] [blame] | 613 | apic_debug("Bad DFR vcpu %d: %08x\n", |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 614 | apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR)); |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 615 | return false; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 616 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 617 | } |
| 618 | |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 619 | /* KVM APIC implementation has two quirks |
| 620 | * - dest always begins at 0 while xAPIC MDA has offset 24, |
| 621 | * - IOxAPIC messages have to be delivered (directly) to x2APIC. |
| 622 | */ |
| 623 | static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source, |
| 624 | struct kvm_lapic *target) |
| 625 | { |
| 626 | bool ipi = source != NULL; |
| 627 | bool x2apic_mda = apic_x2apic_mode(ipi ? source : target); |
| 628 | |
| 629 | if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda) |
| 630 | return X2APIC_BROADCAST; |
| 631 | |
| 632 | return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id); |
| 633 | } |
| 634 | |
Radim Krčmář | 52c233a | 2015-01-29 22:48:48 +0100 | [diff] [blame] | 635 | bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 636 | int short_hand, unsigned int dest, int dest_mode) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 637 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 638 | struct kvm_lapic *target = vcpu->arch.apic; |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 639 | u32 mda = kvm_apic_mda(dest, source, target); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 640 | |
| 641 | apic_debug("target %p, source %p, dest 0x%x, " |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 642 | "dest_mode 0x%x, short_hand 0x%x\n", |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 643 | target, source, dest, dest_mode, short_hand); |
| 644 | |
Zachary Amsden | bd37139 | 2010-06-14 11:42:15 -1000 | [diff] [blame] | 645 | ASSERT(target); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 646 | switch (short_hand) { |
| 647 | case APIC_DEST_NOSHORT: |
Radim Krčmář | 3697f30 | 2015-01-29 22:48:50 +0100 | [diff] [blame] | 648 | if (dest_mode == APIC_DEST_PHYSICAL) |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 649 | return kvm_apic_match_physical_addr(target, mda); |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 650 | else |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 651 | return kvm_apic_match_logical_addr(target, mda); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 652 | case APIC_DEST_SELF: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 653 | return target == source; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 654 | case APIC_DEST_ALLINC: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 655 | return true; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 656 | case APIC_DEST_ALLBUT: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 657 | return target != source; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 658 | default: |
Jan Kiszka | 7712de8 | 2011-09-12 11:25:51 +0200 | [diff] [blame] | 659 | apic_debug("kvm: apic: Bad dest shorthand value %x\n", |
| 660 | short_hand); |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 661 | return false; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 662 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 663 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 664 | EXPORT_SYMBOL_GPL(kvm_apic_match_dest); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 665 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 666 | int kvm_vector_to_index(u32 vector, u32 dest_vcpus, |
| 667 | const unsigned long *bitmap, u32 bitmap_size) |
| 668 | { |
| 669 | u32 mod; |
| 670 | int i, idx = -1; |
| 671 | |
| 672 | mod = vector % dest_vcpus; |
| 673 | |
| 674 | for (i = 0; i <= mod; i++) { |
| 675 | idx = find_next_bit(bitmap, bitmap_size, idx + 1); |
| 676 | BUG_ON(idx == bitmap_size); |
| 677 | } |
| 678 | |
| 679 | return idx; |
| 680 | } |
| 681 | |
Radim Krčmář | 4efd805 | 2016-02-12 15:00:15 +0100 | [diff] [blame] | 682 | static void kvm_apic_disabled_lapic_found(struct kvm *kvm) |
| 683 | { |
| 684 | if (!kvm->arch.disabled_lapic_found) { |
| 685 | kvm->arch.disabled_lapic_found = true; |
| 686 | printk(KERN_INFO |
| 687 | "Disabled LAPIC found during irq injection\n"); |
| 688 | } |
| 689 | } |
| 690 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 691 | /* Return true if the interrupt can be handled by using *bitmap as index mask |
| 692 | * for valid destinations in *dst array. |
| 693 | * Return false if kvm_apic_map_get_dest_lapic did nothing useful. |
| 694 | * Note: we may have zero kvm_lapic destinations when we return true, which |
| 695 | * means that the interrupt should be dropped. In this case, *bitmap would be |
| 696 | * zero and *dst undefined. |
| 697 | */ |
| 698 | static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm, |
| 699 | struct kvm_lapic **src, struct kvm_lapic_irq *irq, |
| 700 | struct kvm_apic_map *map, struct kvm_lapic ***dst, |
| 701 | unsigned long *bitmap) |
| 702 | { |
| 703 | int i, lowest; |
| 704 | bool x2apic_ipi; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 705 | |
| 706 | if (irq->shorthand == APIC_DEST_SELF && src) { |
| 707 | *dst = src; |
| 708 | *bitmap = 1; |
| 709 | return true; |
| 710 | } else if (irq->shorthand) |
| 711 | return false; |
| 712 | |
| 713 | x2apic_ipi = src && *src && apic_x2apic_mode(*src); |
| 714 | if (irq->dest_id == (x2apic_ipi ? X2APIC_BROADCAST : APIC_BROADCAST)) |
| 715 | return false; |
| 716 | |
| 717 | if (!map) |
| 718 | return false; |
| 719 | |
| 720 | if (irq->dest_mode == APIC_DEST_PHYSICAL) { |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 721 | if (irq->dest_id > map->max_apic_id) { |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 722 | *bitmap = 0; |
| 723 | } else { |
| 724 | *dst = &map->phys_map[irq->dest_id]; |
| 725 | *bitmap = 1; |
| 726 | } |
| 727 | return true; |
| 728 | } |
| 729 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 730 | *bitmap = 0; |
| 731 | if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst, |
| 732 | (u16 *)bitmap)) |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 733 | return false; |
| 734 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 735 | if (!kvm_lowest_prio_delivery(irq)) |
| 736 | return true; |
| 737 | |
| 738 | if (!kvm_vector_hashing_enabled()) { |
| 739 | lowest = -1; |
| 740 | for_each_set_bit(i, bitmap, 16) { |
| 741 | if (!(*dst)[i]) |
| 742 | continue; |
| 743 | if (lowest < 0) |
| 744 | lowest = i; |
| 745 | else if (kvm_apic_compare_prio((*dst)[i]->vcpu, |
| 746 | (*dst)[lowest]->vcpu) < 0) |
| 747 | lowest = i; |
| 748 | } |
| 749 | } else { |
| 750 | if (!*bitmap) |
| 751 | return true; |
| 752 | |
| 753 | lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap), |
| 754 | bitmap, 16); |
| 755 | |
| 756 | if (!(*dst)[lowest]) { |
| 757 | kvm_apic_disabled_lapic_found(kvm); |
| 758 | *bitmap = 0; |
| 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | *bitmap = (lowest >= 0) ? 1 << lowest : 0; |
| 764 | |
| 765 | return true; |
| 766 | } |
| 767 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 768 | bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 769 | struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 770 | { |
| 771 | struct kvm_apic_map *map; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 772 | unsigned long bitmap; |
| 773 | struct kvm_lapic **dst = NULL; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 774 | int i; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 775 | bool ret; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 776 | |
| 777 | *r = -1; |
| 778 | |
| 779 | if (irq->shorthand == APIC_DEST_SELF) { |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 780 | *r = kvm_apic_set_irq(src->vcpu, irq, dest_map); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 781 | return true; |
| 782 | } |
| 783 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 784 | rcu_read_lock(); |
| 785 | map = rcu_dereference(kvm->arch.apic_map); |
| 786 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 787 | ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap); |
| 788 | if (ret) |
| 789 | for_each_set_bit(i, &bitmap, 16) { |
| 790 | if (!dst[i]) |
| 791 | continue; |
| 792 | if (*r < 0) |
| 793 | *r = 0; |
| 794 | *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map); |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 795 | } |
| 796 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 797 | rcu_read_unlock(); |
| 798 | return ret; |
| 799 | } |
| 800 | |
Feng Wu | 6228a0d | 2016-01-25 16:53:34 +0800 | [diff] [blame] | 801 | /* |
| 802 | * This routine tries to handler interrupts in posted mode, here is how |
| 803 | * it deals with different cases: |
| 804 | * - For single-destination interrupts, handle it in posted mode |
| 805 | * - Else if vector hashing is enabled and it is a lowest-priority |
| 806 | * interrupt, handle it in posted mode and use the following mechanism |
| 807 | * to find the destinaiton vCPU. |
| 808 | * 1. For lowest-priority interrupts, store all the possible |
| 809 | * destination vCPUs in an array. |
| 810 | * 2. Use "guest vector % max number of destination vCPUs" to find |
| 811 | * the right destination vCPU in the array for the lowest-priority |
| 812 | * interrupt. |
| 813 | * - Otherwise, use remapped mode to inject the interrupt. |
| 814 | */ |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 815 | bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, |
| 816 | struct kvm_vcpu **dest_vcpu) |
| 817 | { |
| 818 | struct kvm_apic_map *map; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 819 | unsigned long bitmap; |
| 820 | struct kvm_lapic **dst = NULL; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 821 | bool ret = false; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 822 | |
| 823 | if (irq->shorthand) |
| 824 | return false; |
| 825 | |
| 826 | rcu_read_lock(); |
| 827 | map = rcu_dereference(kvm->arch.apic_map); |
| 828 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 829 | if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) && |
| 830 | hweight16(bitmap) == 1) { |
| 831 | unsigned long i = find_first_bit(&bitmap, 16); |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 832 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 833 | if (dst[i]) { |
| 834 | *dest_vcpu = dst[i]->vcpu; |
| 835 | ret = true; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 836 | } |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 837 | } |
| 838 | |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 839 | rcu_read_unlock(); |
| 840 | return ret; |
| 841 | } |
| 842 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 843 | /* |
| 844 | * Add a pending IRQ into lapic. |
| 845 | * Return 1 if successfully added and 0 if discarded. |
| 846 | */ |
| 847 | static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 848 | int vector, int level, int trig_mode, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 849 | struct dest_map *dest_map) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 850 | { |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 851 | int result = 0; |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 852 | struct kvm_vcpu *vcpu = apic->vcpu; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 853 | |
Paolo Bonzini | a183b63 | 2014-09-11 11:51:02 +0200 | [diff] [blame] | 854 | trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode, |
| 855 | trig_mode, vector); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 856 | switch (delivery_mode) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 857 | case APIC_DM_LOWEST: |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 858 | vcpu->arch.apic_arb_prio++; |
| 859 | case APIC_DM_FIXED: |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 860 | if (unlikely(trig_mode && !level)) |
| 861 | break; |
| 862 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 863 | /* FIXME add logic for vcpu on reset */ |
| 864 | if (unlikely(!apic_enabled(apic))) |
| 865 | break; |
| 866 | |
Jan Kiszka | 11f5cc0 | 2013-07-25 09:58:45 +0200 | [diff] [blame] | 867 | result = 1; |
| 868 | |
Joerg Roedel | 9daa500 | 2016-02-29 16:04:44 +0100 | [diff] [blame] | 869 | if (dest_map) { |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 870 | __set_bit(vcpu->vcpu_id, dest_map->map); |
Joerg Roedel | 9daa500 | 2016-02-29 16:04:44 +0100 | [diff] [blame] | 871 | dest_map->vectors[vcpu->vcpu_id] = vector; |
| 872 | } |
Avi Kivity | a5d36f8 | 2009-12-29 12:42:16 +0200 | [diff] [blame] | 873 | |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 874 | if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) { |
| 875 | if (trig_mode) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 876 | kvm_lapic_set_vector(vector, apic->regs + APIC_TMR); |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 877 | else |
| 878 | apic_clear_vector(vector, apic->regs + APIC_TMR); |
| 879 | } |
| 880 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 881 | if (vcpu->arch.apicv_active) |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 882 | kvm_x86_ops->deliver_posted_interrupt(vcpu, vector); |
Jan Kiszka | 11f5cc0 | 2013-07-25 09:58:45 +0200 | [diff] [blame] | 883 | else { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 884 | kvm_lapic_set_irr(vector, apic); |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 885 | |
| 886 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 887 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 888 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 889 | break; |
| 890 | |
| 891 | case APIC_DM_REMRD: |
Raghavendra K T | 24d2166 | 2013-08-26 14:18:35 +0530 | [diff] [blame] | 892 | result = 1; |
| 893 | vcpu->arch.pv.pv_unhalted = 1; |
| 894 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 895 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 896 | break; |
| 897 | |
| 898 | case APIC_DM_SMI: |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 899 | result = 1; |
| 900 | kvm_make_request(KVM_REQ_SMI, vcpu); |
| 901 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 902 | break; |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 903 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 904 | case APIC_DM_NMI: |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 905 | result = 1; |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 906 | kvm_inject_nmi(vcpu); |
Jan Kiszka | 26df99c | 2008-09-26 09:30:54 +0200 | [diff] [blame] | 907 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 908 | break; |
| 909 | |
| 910 | case APIC_DM_INIT: |
Julian Stecklina | a52315e | 2012-01-16 14:02:20 +0100 | [diff] [blame] | 911 | if (!trig_mode || level) { |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 912 | result = 1; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 913 | /* assumes that there are only KVM_APIC_INIT/SIPI */ |
| 914 | apic->pending_events = (1UL << KVM_APIC_INIT); |
| 915 | /* make sure pending_events is visible before sending |
| 916 | * the request */ |
| 917 | smp_wmb(); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 918 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 919 | kvm_vcpu_kick(vcpu); |
| 920 | } else { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 921 | apic_debug("Ignoring de-assert INIT to vcpu %d\n", |
| 922 | vcpu->vcpu_id); |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 923 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 924 | break; |
| 925 | |
| 926 | case APIC_DM_STARTUP: |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 927 | apic_debug("SIPI to vcpu %d vector 0x%02x\n", |
| 928 | vcpu->vcpu_id, vector); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 929 | result = 1; |
| 930 | apic->sipi_vector = vector; |
| 931 | /* make sure sipi_vector is visible for the receiver */ |
| 932 | smp_wmb(); |
| 933 | set_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 934 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 935 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 936 | break; |
| 937 | |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 938 | case APIC_DM_EXTINT: |
| 939 | /* |
| 940 | * Should only be called by kvm_apic_local_deliver() with LVT0, |
| 941 | * before NMI watchdog was enabled. Already handled by |
| 942 | * kvm_apic_accept_pic_intr(). |
| 943 | */ |
| 944 | break; |
| 945 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 946 | default: |
| 947 | printk(KERN_ERR "TODO: unsupported delivery mode %x\n", |
| 948 | delivery_mode); |
| 949 | break; |
| 950 | } |
| 951 | return result; |
| 952 | } |
| 953 | |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 954 | int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 955 | { |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 956 | return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio; |
Zhang Xiantao | 8be5453 | 2007-12-02 22:35:57 +0800 | [diff] [blame] | 957 | } |
| 958 | |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 959 | static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector) |
| 960 | { |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 961 | return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors); |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 964 | static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector) |
| 965 | { |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 966 | int trigger_mode; |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 967 | |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 968 | /* Eoi the ioapic only if the ioapic doesn't own the vector. */ |
| 969 | if (!kvm_ioapic_handles_vector(apic, vector)) |
| 970 | return; |
| 971 | |
| 972 | /* Request a KVM exit to inform the userspace IOAPIC. */ |
| 973 | if (irqchip_split(apic->vcpu->kvm)) { |
| 974 | apic->vcpu->arch.pending_ioapic_eoi = vector; |
| 975 | kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu); |
| 976 | return; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 977 | } |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 978 | |
| 979 | if (apic_test_vector(vector, apic->regs + APIC_TMR)) |
| 980 | trigger_mode = IOAPIC_LEVEL_TRIG; |
| 981 | else |
| 982 | trigger_mode = IOAPIC_EDGE_TRIG; |
| 983 | |
| 984 | kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 985 | } |
| 986 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 987 | static int apic_set_eoi(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 988 | { |
| 989 | int vector = apic_find_highest_isr(apic); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 990 | |
| 991 | trace_kvm_eoi(apic, vector); |
| 992 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 993 | /* |
| 994 | * Not every write EOI will has corresponding ISR, |
| 995 | * one example is when Kernel check timer on setup_IO_APIC |
| 996 | */ |
| 997 | if (vector == -1) |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 998 | return vector; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 999 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 1000 | apic_clear_isr(vector, apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1001 | apic_update_ppr(apic); |
| 1002 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1003 | if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap)) |
| 1004 | kvm_hv_synic_send_eoi(apic->vcpu, vector); |
| 1005 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1006 | kvm_ioapic_send_eoi(apic, vector); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 1007 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1008 | return vector; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1009 | } |
| 1010 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1011 | /* |
| 1012 | * this interface assumes a trap-like exit, which has already finished |
| 1013 | * desired side effect including vISR and vPPR update. |
| 1014 | */ |
| 1015 | void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector) |
| 1016 | { |
| 1017 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1018 | |
| 1019 | trace_kvm_eoi(apic, vector); |
| 1020 | |
| 1021 | kvm_ioapic_send_eoi(apic, vector); |
| 1022 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated); |
| 1025 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1026 | static void apic_send_ipi(struct kvm_lapic *apic) |
| 1027 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1028 | u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR); |
| 1029 | u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2); |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1030 | struct kvm_lapic_irq irq; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1031 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1032 | irq.vector = icr_low & APIC_VECTOR_MASK; |
| 1033 | irq.delivery_mode = icr_low & APIC_MODE_MASK; |
| 1034 | irq.dest_mode = icr_low & APIC_DEST_MASK; |
Paolo Bonzini | b7cb223 | 2015-04-21 14:57:05 +0200 | [diff] [blame] | 1035 | irq.level = (icr_low & APIC_INT_ASSERT) != 0; |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1036 | irq.trig_mode = icr_low & APIC_INT_LEVELTRIG; |
| 1037 | irq.shorthand = icr_low & APIC_SHORT_MASK; |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 1038 | irq.msi_redir_hint = false; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1039 | if (apic_x2apic_mode(apic)) |
| 1040 | irq.dest_id = icr_high; |
| 1041 | else |
| 1042 | irq.dest_id = GET_APIC_DEST_FIELD(icr_high); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1043 | |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 1044 | trace_kvm_apic_ipi(icr_low, irq.dest_id); |
| 1045 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1046 | apic_debug("icr_high 0x%x, icr_low 0x%x, " |
| 1047 | "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, " |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 1048 | "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, " |
| 1049 | "msi_redir_hint 0x%x\n", |
Glauber Costa | 9b5843d | 2009-04-29 17:29:09 -0400 | [diff] [blame] | 1050 | icr_high, icr_low, irq.shorthand, irq.dest_id, |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1051 | irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode, |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 1052 | irq.vector, irq.msi_redir_hint); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1053 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 1054 | kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static u32 apic_get_tmcct(struct kvm_lapic *apic) |
| 1058 | { |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 1059 | ktime_t remaining; |
| 1060 | s64 ns; |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1061 | u32 tmcct; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1062 | |
| 1063 | ASSERT(apic != NULL); |
| 1064 | |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1065 | /* if initial count is 0, current count should also be 0 */ |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1066 | if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 || |
Andy Honig | b963a22 | 2013-11-19 14:12:18 -0800 | [diff] [blame] | 1067 | apic->lapic_timer.period == 0) |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1068 | return 0; |
| 1069 | |
Marcelo Tosatti | ace1546 | 2009-10-08 10:55:03 -0300 | [diff] [blame] | 1070 | remaining = hrtimer_get_remaining(&apic->lapic_timer.timer); |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 1071 | if (ktime_to_ns(remaining) < 0) |
| 1072 | remaining = ktime_set(0, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1073 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1074 | ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period); |
| 1075 | tmcct = div64_u64(ns, |
| 1076 | (APIC_BUS_CYCLE_NS * apic->divide_count)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1077 | |
| 1078 | return tmcct; |
| 1079 | } |
| 1080 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1081 | static void __report_tpr_access(struct kvm_lapic *apic, bool write) |
| 1082 | { |
| 1083 | struct kvm_vcpu *vcpu = apic->vcpu; |
| 1084 | struct kvm_run *run = vcpu->run; |
| 1085 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1086 | kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1087 | run->tpr_access.rip = kvm_rip_read(vcpu); |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1088 | run->tpr_access.is_write = write; |
| 1089 | } |
| 1090 | |
| 1091 | static inline void report_tpr_access(struct kvm_lapic *apic, bool write) |
| 1092 | { |
| 1093 | if (apic->vcpu->arch.tpr_access_reporting) |
| 1094 | __report_tpr_access(apic, write); |
| 1095 | } |
| 1096 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1097 | static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset) |
| 1098 | { |
| 1099 | u32 val = 0; |
| 1100 | |
| 1101 | if (offset >= LAPIC_MMIO_LENGTH) |
| 1102 | return 0; |
| 1103 | |
| 1104 | switch (offset) { |
| 1105 | case APIC_ARBPRI: |
Jan Kiszka | 7712de8 | 2011-09-12 11:25:51 +0200 | [diff] [blame] | 1106 | apic_debug("Access APIC ARBPRI register which is for P6\n"); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1107 | break; |
| 1108 | |
| 1109 | case APIC_TMCCT: /* Timer CCR */ |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1110 | if (apic_lvtt_tscdeadline(apic)) |
| 1111 | return 0; |
| 1112 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1113 | val = apic_get_tmcct(apic); |
| 1114 | break; |
Avi Kivity | 4a4541a | 2012-07-22 17:41:00 +0300 | [diff] [blame] | 1115 | case APIC_PROCPRI: |
| 1116 | apic_update_ppr(apic); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1117 | val = kvm_lapic_get_reg(apic, offset); |
Avi Kivity | 4a4541a | 2012-07-22 17:41:00 +0300 | [diff] [blame] | 1118 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1119 | case APIC_TASKPRI: |
| 1120 | report_tpr_access(apic, false); |
| 1121 | /* fall thru */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1122 | default: |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1123 | val = kvm_lapic_get_reg(apic, offset); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | return val; |
| 1128 | } |
| 1129 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 1130 | static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev) |
| 1131 | { |
| 1132 | return container_of(dev, struct kvm_lapic, dev); |
| 1133 | } |
| 1134 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1135 | int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1136 | void *data) |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 1137 | { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1138 | unsigned char alignment = offset & 0xf; |
| 1139 | u32 result; |
Guo Chao | d5b0b5b | 2012-06-28 15:22:57 +0800 | [diff] [blame] | 1140 | /* this bitmask has a bit cleared for each reserved register */ |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1141 | static const u64 rmask = 0x43ff01ffffffe70cULL; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1142 | |
| 1143 | if ((alignment + len) > 4) { |
Gleb Natapov | 4088bb3 | 2009-07-08 11:26:54 +0300 | [diff] [blame] | 1144 | apic_debug("KVM_APIC_READ: alignment error %x %d\n", |
| 1145 | offset, len); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1146 | return 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1147 | } |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1148 | |
| 1149 | if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) { |
Gleb Natapov | 4088bb3 | 2009-07-08 11:26:54 +0300 | [diff] [blame] | 1150 | apic_debug("KVM_APIC_READ: read reserved register %x\n", |
| 1151 | offset); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1152 | return 1; |
| 1153 | } |
| 1154 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1155 | result = __apic_read(apic, offset & ~0xf); |
| 1156 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 1157 | trace_kvm_apic_read(offset, result); |
| 1158 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1159 | switch (len) { |
| 1160 | case 1: |
| 1161 | case 2: |
| 1162 | case 4: |
| 1163 | memcpy(data, (char *)&result + alignment, len); |
| 1164 | break; |
| 1165 | default: |
| 1166 | printk(KERN_ERR "Local APIC read with len = %x, " |
| 1167 | "should be 1,2, or 4 instead\n", len); |
| 1168 | break; |
| 1169 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 1170 | return 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1171 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1172 | EXPORT_SYMBOL_GPL(kvm_lapic_reg_read); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1173 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1174 | static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr) |
| 1175 | { |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1176 | return kvm_apic_hw_enabled(apic) && |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1177 | addr >= apic->base_address && |
| 1178 | addr < apic->base_address + LAPIC_MMIO_LENGTH; |
| 1179 | } |
| 1180 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 1181 | static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this, |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1182 | gpa_t address, int len, void *data) |
| 1183 | { |
| 1184 | struct kvm_lapic *apic = to_lapic(this); |
| 1185 | u32 offset = address - apic->base_address; |
| 1186 | |
| 1187 | if (!apic_mmio_in_range(apic, address)) |
| 1188 | return -EOPNOTSUPP; |
| 1189 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1190 | kvm_lapic_reg_read(apic, offset, len, data); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1195 | static void update_divide_count(struct kvm_lapic *apic) |
| 1196 | { |
| 1197 | u32 tmp1, tmp2, tdcr; |
| 1198 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1199 | tdcr = kvm_lapic_get_reg(apic, APIC_TDCR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1200 | tmp1 = tdcr & 0xf; |
| 1201 | tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1202 | apic->divide_count = 0x1 << (tmp2 & 0x7); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1203 | |
| 1204 | apic_debug("timer divide count is 0x%x\n", |
Glauber Costa | 9b5843d | 2009-04-29 17:29:09 -0400 | [diff] [blame] | 1205 | apic->divide_count); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1206 | } |
| 1207 | |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1208 | static void apic_update_lvtt(struct kvm_lapic *apic) |
| 1209 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1210 | u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) & |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1211 | apic->lapic_timer.timer_mode_mask; |
| 1212 | |
| 1213 | if (apic->lapic_timer.timer_mode != timer_mode) { |
| 1214 | apic->lapic_timer.timer_mode = timer_mode; |
| 1215 | hrtimer_cancel(&apic->lapic_timer.timer); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1219 | static void apic_timer_expired(struct kvm_lapic *apic) |
| 1220 | { |
| 1221 | struct kvm_vcpu *vcpu = apic->vcpu; |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 1222 | struct swait_queue_head *q = &vcpu->wq; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1223 | struct kvm_timer *ktimer = &apic->lapic_timer; |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1224 | |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1225 | if (atomic_read(&apic->lapic_timer.pending)) |
| 1226 | return; |
| 1227 | |
| 1228 | atomic_inc(&apic->lapic_timer.pending); |
Nicholas Krause | bab5bb3 | 2015-01-01 22:05:18 -0500 | [diff] [blame] | 1229 | kvm_set_pending_timer(vcpu); |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1230 | |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 1231 | if (swait_active(q)) |
| 1232 | swake_up(q); |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1233 | |
| 1234 | if (apic_lvtt_tscdeadline(apic)) |
| 1235 | ktimer->expired_tscdeadline = ktimer->tscdeadline; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * On APICv, this test will cause a busy wait |
| 1240 | * during a higher-priority task. |
| 1241 | */ |
| 1242 | |
| 1243 | static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu) |
| 1244 | { |
| 1245 | struct kvm_lapic *apic = vcpu->arch.apic; |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1246 | u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT); |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1247 | |
| 1248 | if (kvm_apic_hw_enabled(apic)) { |
| 1249 | int vec = reg & APIC_VECTOR_MASK; |
Marcelo Tosatti | f933986 | 2015-02-02 15:26:08 -0200 | [diff] [blame] | 1250 | void *bitmap = apic->regs + APIC_ISR; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1251 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 1252 | if (vcpu->arch.apicv_active) |
Marcelo Tosatti | f933986 | 2015-02-02 15:26:08 -0200 | [diff] [blame] | 1253 | bitmap = apic->regs + APIC_IRR; |
| 1254 | |
| 1255 | if (apic_test_vector(vec, bitmap)) |
| 1256 | return true; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1257 | } |
| 1258 | return false; |
| 1259 | } |
| 1260 | |
| 1261 | void wait_lapic_expire(struct kvm_vcpu *vcpu) |
| 1262 | { |
| 1263 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1264 | u64 guest_tsc, tsc_deadline; |
| 1265 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 1266 | if (!lapic_in_kernel(vcpu)) |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1267 | return; |
| 1268 | |
| 1269 | if (apic->lapic_timer.expired_tscdeadline == 0) |
| 1270 | return; |
| 1271 | |
| 1272 | if (!lapic_timer_int_injected(vcpu)) |
| 1273 | return; |
| 1274 | |
| 1275 | tsc_deadline = apic->lapic_timer.expired_tscdeadline; |
| 1276 | apic->lapic_timer.expired_tscdeadline = 0; |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 1277 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
Marcelo Tosatti | 6c19b75 | 2014-12-16 09:08:16 -0500 | [diff] [blame] | 1278 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline); |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1279 | |
| 1280 | /* __delay is delay_tsc whenever the hardware has TSC, thus always. */ |
| 1281 | if (guest_tsc < tsc_deadline) |
| 1282 | __delay(tsc_deadline - guest_tsc); |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1285 | static void start_sw_tscdeadline(struct kvm_lapic *apic) |
| 1286 | { |
| 1287 | u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline; |
| 1288 | u64 ns = 0; |
| 1289 | ktime_t expire; |
| 1290 | struct kvm_vcpu *vcpu = apic->vcpu; |
| 1291 | unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz; |
| 1292 | unsigned long flags; |
| 1293 | ktime_t now; |
| 1294 | |
| 1295 | if (unlikely(!tscdeadline || !this_tsc_khz)) |
| 1296 | return; |
| 1297 | |
| 1298 | local_irq_save(flags); |
| 1299 | |
| 1300 | now = apic->lapic_timer.timer.base->get_time(); |
| 1301 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 1302 | if (likely(tscdeadline > guest_tsc)) { |
| 1303 | ns = (tscdeadline - guest_tsc) * 1000000ULL; |
| 1304 | do_div(ns, this_tsc_khz); |
| 1305 | expire = ktime_add_ns(now, ns); |
| 1306 | expire = ktime_sub_ns(expire, lapic_timer_advance_ns); |
| 1307 | hrtimer_start(&apic->lapic_timer.timer, |
| 1308 | expire, HRTIMER_MODE_ABS_PINNED); |
| 1309 | } else |
| 1310 | apic_timer_expired(apic); |
| 1311 | |
| 1312 | local_irq_restore(flags); |
| 1313 | } |
| 1314 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1315 | bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu) |
| 1316 | { |
| 1317 | return vcpu->arch.apic->lapic_timer.hv_timer_in_use; |
| 1318 | } |
| 1319 | EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use); |
| 1320 | |
Wanpeng Li | bd97ad0 | 2016-06-30 08:52:49 +0800 | [diff] [blame] | 1321 | static void cancel_hv_tscdeadline(struct kvm_lapic *apic) |
| 1322 | { |
| 1323 | kvm_x86_ops->cancel_hv_timer(apic->vcpu); |
| 1324 | apic->lapic_timer.hv_timer_in_use = false; |
| 1325 | } |
| 1326 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1327 | void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu) |
| 1328 | { |
| 1329 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1330 | |
| 1331 | WARN_ON(!apic->lapic_timer.hv_timer_in_use); |
| 1332 | WARN_ON(swait_active(&vcpu->wq)); |
Wanpeng Li | bd97ad0 | 2016-06-30 08:52:49 +0800 | [diff] [blame] | 1333 | cancel_hv_tscdeadline(apic); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1334 | apic_timer_expired(apic); |
| 1335 | } |
| 1336 | EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer); |
| 1337 | |
Wanpeng Li | 196f20c | 2016-06-28 14:54:19 +0800 | [diff] [blame] | 1338 | static bool start_hv_tscdeadline(struct kvm_lapic *apic) |
| 1339 | { |
| 1340 | u64 tscdeadline = apic->lapic_timer.tscdeadline; |
| 1341 | |
| 1342 | if (atomic_read(&apic->lapic_timer.pending) || |
| 1343 | kvm_x86_ops->set_hv_timer(apic->vcpu, tscdeadline)) { |
| 1344 | if (apic->lapic_timer.hv_timer_in_use) |
| 1345 | cancel_hv_tscdeadline(apic); |
| 1346 | } else { |
| 1347 | apic->lapic_timer.hv_timer_in_use = true; |
| 1348 | hrtimer_cancel(&apic->lapic_timer.timer); |
| 1349 | |
| 1350 | /* In case the sw timer triggered in the window */ |
| 1351 | if (atomic_read(&apic->lapic_timer.pending)) |
| 1352 | cancel_hv_tscdeadline(apic); |
| 1353 | } |
| 1354 | trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, |
| 1355 | apic->lapic_timer.hv_timer_in_use); |
| 1356 | return apic->lapic_timer.hv_timer_in_use; |
| 1357 | } |
| 1358 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1359 | void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu) |
| 1360 | { |
| 1361 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1362 | |
| 1363 | WARN_ON(apic->lapic_timer.hv_timer_in_use); |
| 1364 | |
Wanpeng Li | 196f20c | 2016-06-28 14:54:19 +0800 | [diff] [blame] | 1365 | if (apic_lvtt_tscdeadline(apic)) |
| 1366 | start_hv_tscdeadline(apic); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1367 | } |
| 1368 | EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer); |
| 1369 | |
| 1370 | void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu) |
| 1371 | { |
| 1372 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1373 | |
| 1374 | /* Possibly the TSC deadline timer is not enabled yet */ |
| 1375 | if (!apic->lapic_timer.hv_timer_in_use) |
| 1376 | return; |
| 1377 | |
Wanpeng Li | bd97ad0 | 2016-06-30 08:52:49 +0800 | [diff] [blame] | 1378 | cancel_hv_tscdeadline(apic); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1379 | |
| 1380 | if (atomic_read(&apic->lapic_timer.pending)) |
| 1381 | return; |
| 1382 | |
| 1383 | start_sw_tscdeadline(apic); |
| 1384 | } |
| 1385 | EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer); |
| 1386 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1387 | static void start_apic_timer(struct kvm_lapic *apic) |
| 1388 | { |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1389 | ktime_t now; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1390 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1391 | atomic_set(&apic->lapic_timer.pending, 0); |
Avi Kivity | 0b975a3 | 2008-02-24 14:37:50 +0200 | [diff] [blame] | 1392 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1393 | if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) { |
Guo Chao | d5b0b5b | 2012-06-28 15:22:57 +0800 | [diff] [blame] | 1394 | /* lapic timer in oneshot or periodic mode */ |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1395 | now = apic->lapic_timer.timer.base->get_time(); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1396 | apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1397 | * APIC_BUS_CYCLE_NS * apic->divide_count; |
Jan Kiszka | 9bc5791 | 2011-09-12 14:10:22 +0200 | [diff] [blame] | 1398 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1399 | if (!apic->lapic_timer.period) |
| 1400 | return; |
| 1401 | /* |
| 1402 | * Do not allow the guest to program periodic timers with small |
| 1403 | * interval, since the hrtimers are not throttled by the host |
| 1404 | * scheduler. |
| 1405 | */ |
| 1406 | if (apic_lvtt_period(apic)) { |
| 1407 | s64 min_period = min_timer_period_us * 1000LL; |
| 1408 | |
| 1409 | if (apic->lapic_timer.period < min_period) { |
| 1410 | pr_info_ratelimited( |
| 1411 | "kvm: vcpu %i: requested %lld ns " |
| 1412 | "lapic timer period limited to %lld ns\n", |
| 1413 | apic->vcpu->vcpu_id, |
| 1414 | apic->lapic_timer.period, min_period); |
| 1415 | apic->lapic_timer.period = min_period; |
| 1416 | } |
Jan Kiszka | 9bc5791 | 2011-09-12 14:10:22 +0200 | [diff] [blame] | 1417 | } |
Avi Kivity | 0b975a3 | 2008-02-24 14:37:50 +0200 | [diff] [blame] | 1418 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1419 | hrtimer_start(&apic->lapic_timer.timer, |
| 1420 | ktime_add_ns(now, apic->lapic_timer.period), |
Luiz Capitulino | 61abdbe | 2016-04-04 16:46:07 -0400 | [diff] [blame] | 1421 | HRTIMER_MODE_ABS_PINNED); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1422 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1423 | apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016" |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1424 | PRIx64 ", " |
| 1425 | "timer initial count 0x%x, period %lldns, " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1426 | "expire @ 0x%016" PRIx64 ".\n", __func__, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1427 | APIC_BUS_CYCLE_NS, ktime_to_ns(now), |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1428 | kvm_lapic_get_reg(apic, APIC_TMICT), |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1429 | apic->lapic_timer.period, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1430 | ktime_to_ns(ktime_add_ns(now, |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1431 | apic->lapic_timer.period))); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1432 | } else if (apic_lvtt_tscdeadline(apic)) { |
Wanpeng Li | 196f20c | 2016-06-28 14:54:19 +0800 | [diff] [blame] | 1433 | if (!(kvm_x86_ops->set_hv_timer && start_hv_tscdeadline(apic))) |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1434 | start_sw_tscdeadline(apic); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1435 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1436 | } |
| 1437 | |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1438 | static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val) |
| 1439 | { |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1440 | bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val); |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1441 | |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1442 | if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) { |
| 1443 | apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode; |
| 1444 | if (lvt0_in_nmi_mode) { |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1445 | apic_debug("Receive NMI setting on APIC_LVT0 " |
| 1446 | "for cpu %d\n", apic->vcpu->vcpu_id); |
Radim Krčmář | 4272013 | 2015-07-01 15:31:49 +0200 | [diff] [blame] | 1447 | atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode); |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1448 | } else |
| 1449 | atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode); |
| 1450 | } |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1453 | int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1454 | { |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1455 | int ret = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1456 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1457 | trace_kvm_apic_write(reg, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1458 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1459 | switch (reg) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1460 | case APIC_ID: /* Local APIC ID */ |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1461 | if (!apic_x2apic_mode(apic)) |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 1462 | kvm_apic_set_xapic_id(apic, val >> 24); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1463 | else |
| 1464 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1465 | break; |
| 1466 | |
| 1467 | case APIC_TASKPRI: |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1468 | report_tpr_access(apic, true); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1469 | apic_set_tpr(apic, val & 0xff); |
| 1470 | break; |
| 1471 | |
| 1472 | case APIC_EOI: |
| 1473 | apic_set_eoi(apic); |
| 1474 | break; |
| 1475 | |
| 1476 | case APIC_LDR: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1477 | if (!apic_x2apic_mode(apic)) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1478 | kvm_apic_set_ldr(apic, val & APIC_LDR_MASK); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1479 | else |
| 1480 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1481 | break; |
| 1482 | |
| 1483 | case APIC_DFR: |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1484 | if (!apic_x2apic_mode(apic)) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1485 | kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1486 | recalculate_apic_map(apic->vcpu->kvm); |
| 1487 | } else |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1488 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1489 | break; |
| 1490 | |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1491 | case APIC_SPIV: { |
| 1492 | u32 mask = 0x3ff; |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1493 | if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI) |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1494 | mask |= APIC_SPIV_DIRECTED_EOI; |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1495 | apic_set_spiv(apic, val & mask); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1496 | if (!(val & APIC_SPIV_APIC_ENABLED)) { |
| 1497 | int i; |
| 1498 | u32 lvt_val; |
| 1499 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1500 | for (i = 0; i < KVM_APIC_LVT_NUM; i++) { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1501 | lvt_val = kvm_lapic_get_reg(apic, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1502 | APIC_LVTT + 0x10 * i); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1503 | kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1504 | lvt_val | APIC_LVT_MASKED); |
| 1505 | } |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1506 | apic_update_lvtt(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1507 | atomic_set(&apic->lapic_timer.pending, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1508 | |
| 1509 | } |
| 1510 | break; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1511 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1512 | case APIC_ICR: |
| 1513 | /* No delay here, so we always clear the pending bit */ |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1514 | kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1515 | apic_send_ipi(apic); |
| 1516 | break; |
| 1517 | |
| 1518 | case APIC_ICR2: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1519 | if (!apic_x2apic_mode(apic)) |
| 1520 | val &= 0xff000000; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1521 | kvm_lapic_set_reg(apic, APIC_ICR2, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1522 | break; |
| 1523 | |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1524 | case APIC_LVT0: |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1525 | apic_manage_nmi_watchdog(apic, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1526 | case APIC_LVTTHMR: |
| 1527 | case APIC_LVTPC: |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1528 | case APIC_LVT1: |
| 1529 | case APIC_LVTERR: |
| 1530 | /* TODO: Check vector */ |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1531 | if (!kvm_apic_sw_enabled(apic)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1532 | val |= APIC_LVT_MASKED; |
| 1533 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1534 | val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4]; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1535 | kvm_lapic_set_reg(apic, reg, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1536 | |
| 1537 | break; |
| 1538 | |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1539 | case APIC_LVTT: |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1540 | if (!kvm_apic_sw_enabled(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1541 | val |= APIC_LVT_MASKED; |
| 1542 | val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1543 | kvm_lapic_set_reg(apic, APIC_LVTT, val); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1544 | apic_update_lvtt(apic); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1545 | break; |
| 1546 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1547 | case APIC_TMICT: |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1548 | if (apic_lvtt_tscdeadline(apic)) |
| 1549 | break; |
| 1550 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1551 | hrtimer_cancel(&apic->lapic_timer.timer); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1552 | kvm_lapic_set_reg(apic, APIC_TMICT, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1553 | start_apic_timer(apic); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1554 | break; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1555 | |
| 1556 | case APIC_TDCR: |
| 1557 | if (val & 4) |
Jan Kiszka | 7712de8 | 2011-09-12 11:25:51 +0200 | [diff] [blame] | 1558 | apic_debug("KVM_WRITE:TDCR %x\n", val); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1559 | kvm_lapic_set_reg(apic, APIC_TDCR, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1560 | update_divide_count(apic); |
| 1561 | break; |
| 1562 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1563 | case APIC_ESR: |
| 1564 | if (apic_x2apic_mode(apic) && val != 0) { |
Jan Kiszka | 7712de8 | 2011-09-12 11:25:51 +0200 | [diff] [blame] | 1565 | apic_debug("KVM_WRITE:ESR not zero %x\n", val); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1566 | ret = 1; |
| 1567 | } |
| 1568 | break; |
| 1569 | |
| 1570 | case APIC_SELF_IPI: |
| 1571 | if (apic_x2apic_mode(apic)) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1572 | kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff)); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1573 | } else |
| 1574 | ret = 1; |
| 1575 | break; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1576 | default: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1577 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1578 | break; |
| 1579 | } |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1580 | if (ret) |
| 1581 | apic_debug("Local APIC Write to read-only register %x\n", reg); |
| 1582 | return ret; |
| 1583 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1584 | EXPORT_SYMBOL_GPL(kvm_lapic_reg_write); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1585 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 1586 | static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1587 | gpa_t address, int len, const void *data) |
| 1588 | { |
| 1589 | struct kvm_lapic *apic = to_lapic(this); |
| 1590 | unsigned int offset = address - apic->base_address; |
| 1591 | u32 val; |
| 1592 | |
| 1593 | if (!apic_mmio_in_range(apic, address)) |
| 1594 | return -EOPNOTSUPP; |
| 1595 | |
| 1596 | /* |
| 1597 | * APIC register must be aligned on 128-bits boundary. |
| 1598 | * 32/64/128 bits registers must be accessed thru 32 bits. |
| 1599 | * Refer SDM 8.4.1 |
| 1600 | */ |
| 1601 | if (len != 4 || (offset & 0xf)) { |
| 1602 | /* Don't shout loud, $infamous_os would cause only noise. */ |
| 1603 | apic_debug("apic write: bad size=%d %lx\n", len, (long)address); |
Sheng Yang | 756975b | 2009-07-06 11:05:39 +0800 | [diff] [blame] | 1604 | return 0; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | val = *(u32*)data; |
| 1608 | |
| 1609 | /* too common printing */ |
| 1610 | if (offset != APIC_EOI) |
| 1611 | apic_debug("%s: offset 0x%x with length 0x%x, and value is " |
| 1612 | "0x%x\n", __func__, offset, len, val); |
| 1613 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1614 | kvm_lapic_reg_write(apic, offset & 0xff0, val); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1615 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 1616 | return 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1617 | } |
| 1618 | |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 1619 | void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) |
| 1620 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1621 | kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 1622 | } |
| 1623 | EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi); |
| 1624 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1625 | /* emulate APIC access in a trap manner */ |
| 1626 | void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) |
| 1627 | { |
| 1628 | u32 val = 0; |
| 1629 | |
| 1630 | /* hw has done the conditional check and inst decode */ |
| 1631 | offset &= 0xff0; |
| 1632 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1633 | kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1634 | |
| 1635 | /* TODO: optimize to just emulate side effect w/o one more write */ |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1636 | kvm_lapic_reg_write(vcpu->arch.apic, offset, val); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1637 | } |
| 1638 | EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode); |
| 1639 | |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 1640 | void kvm_free_lapic(struct kvm_vcpu *vcpu) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1641 | { |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1642 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1643 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1644 | if (!vcpu->arch.apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1645 | return; |
| 1646 | |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1647 | hrtimer_cancel(&apic->lapic_timer.timer); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1648 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 1649 | if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE)) |
| 1650 | static_key_slow_dec_deferred(&apic_hw_disabled); |
| 1651 | |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 1652 | if (!apic->sw_enabled) |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1653 | static_key_slow_dec_deferred(&apic_sw_disabled); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1654 | |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1655 | if (apic->regs) |
| 1656 | free_page((unsigned long)apic->regs); |
| 1657 | |
| 1658 | kfree(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | *---------------------------------------------------------------------- |
| 1663 | * LAPIC interface |
| 1664 | *---------------------------------------------------------------------- |
| 1665 | */ |
| 1666 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1667 | u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu) |
| 1668 | { |
| 1669 | struct kvm_lapic *apic = vcpu->arch.apic; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1670 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 1671 | if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) || |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 1672 | apic_lvtt_period(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1673 | return 0; |
| 1674 | |
| 1675 | return apic->lapic_timer.tscdeadline; |
| 1676 | } |
| 1677 | |
| 1678 | void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data) |
| 1679 | { |
| 1680 | struct kvm_lapic *apic = vcpu->arch.apic; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1681 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 1682 | if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) || |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 1683 | apic_lvtt_period(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1684 | return; |
| 1685 | |
| 1686 | hrtimer_cancel(&apic->lapic_timer.timer); |
| 1687 | apic->lapic_timer.tscdeadline = data; |
| 1688 | start_apic_timer(apic); |
| 1689 | } |
| 1690 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1691 | void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 1692 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1693 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1694 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 1695 | apic_set_tpr(apic, ((cr8 & 0x0f) << 4) |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1696 | | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu) |
| 1700 | { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1701 | u64 tpr; |
| 1702 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1703 | tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1704 | |
| 1705 | return (tpr & 0xf0) >> 4; |
| 1706 | } |
| 1707 | |
| 1708 | void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) |
| 1709 | { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1710 | u64 old_value = vcpu->arch.apic_base; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1711 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1712 | |
| 1713 | if (!apic) { |
| 1714 | value |= MSR_IA32_APICBASE_BSP; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1715 | vcpu->arch.apic_base = value; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1716 | return; |
| 1717 | } |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 1718 | |
Jan Kiszka | e66d2ae | 2013-12-29 02:29:30 +0100 | [diff] [blame] | 1719 | vcpu->arch.apic_base = value; |
| 1720 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 1721 | /* update jump label if enable bit changes */ |
Andrew Jones | 0dce7cd | 2014-01-15 13:39:59 +0100 | [diff] [blame] | 1722 | if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) { |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 1723 | if (value & MSR_IA32_APICBASE_ENABLE) |
| 1724 | static_key_slow_dec_deferred(&apic_hw_disabled); |
| 1725 | else |
| 1726 | static_key_slow_inc(&apic_hw_disabled.key); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1727 | recalculate_apic_map(vcpu->kvm); |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 1728 | } |
| 1729 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1730 | if ((old_value ^ value) & X2APIC_ENABLE) { |
| 1731 | if (value & X2APIC_ENABLE) { |
Radim Krčmář | 257b9a5 | 2015-05-22 18:45:11 +0200 | [diff] [blame] | 1732 | kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1733 | kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true); |
| 1734 | } else |
| 1735 | kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1736 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1737 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1738 | apic->base_address = apic->vcpu->arch.apic_base & |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1739 | MSR_IA32_APICBASE_BASE; |
| 1740 | |
Nadav Amit | db324fe | 2014-11-02 11:54:59 +0200 | [diff] [blame] | 1741 | if ((value & MSR_IA32_APICBASE_ENABLE) && |
| 1742 | apic->base_address != APIC_DEFAULT_PHYS_BASE) |
| 1743 | pr_warn_once("APIC base relocation is unsupported by KVM"); |
| 1744 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1745 | /* with FSB delivery interrupt, we can restart APIC functionality */ |
| 1746 | apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is " |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1747 | "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1748 | |
| 1749 | } |
| 1750 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1751 | void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1752 | { |
| 1753 | struct kvm_lapic *apic; |
| 1754 | int i; |
| 1755 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1756 | apic_debug("%s\n", __func__); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1757 | |
| 1758 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1759 | apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1760 | ASSERT(apic != NULL); |
| 1761 | |
| 1762 | /* Stop the timer in case it's a reset to an active apic */ |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1763 | hrtimer_cancel(&apic->lapic_timer.timer); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1764 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1765 | if (!init_event) |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 1766 | kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1767 | kvm_apic_set_version(apic->vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1768 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1769 | for (i = 0; i < KVM_APIC_LVT_NUM; i++) |
| 1770 | kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1771 | apic_update_lvtt(apic); |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 1772 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED)) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1773 | kvm_lapic_set_reg(apic, APIC_LVT0, |
Nadav Amit | 90de4a1 | 2015-04-13 01:53:41 +0300 | [diff] [blame] | 1774 | SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT)); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1775 | apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1776 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1777 | kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU); |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1778 | apic_set_spiv(apic, 0xff); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1779 | kvm_lapic_set_reg(apic, APIC_TASKPRI, 0); |
Radim Krčmář | c028dd6 | 2015-05-22 19:22:10 +0200 | [diff] [blame] | 1780 | if (!apic_x2apic_mode(apic)) |
| 1781 | kvm_apic_set_ldr(apic, 0); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1782 | kvm_lapic_set_reg(apic, APIC_ESR, 0); |
| 1783 | kvm_lapic_set_reg(apic, APIC_ICR, 0); |
| 1784 | kvm_lapic_set_reg(apic, APIC_ICR2, 0); |
| 1785 | kvm_lapic_set_reg(apic, APIC_TDCR, 0); |
| 1786 | kvm_lapic_set_reg(apic, APIC_TMICT, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1787 | for (i = 0; i < 8; i++) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1788 | kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0); |
| 1789 | kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0); |
| 1790 | kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1791 | } |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 1792 | apic->irr_pending = vcpu->arch.apicv_active; |
| 1793 | apic->isr_count = vcpu->arch.apicv_active ? 1 : 0; |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 1794 | apic->highest_isr_cache = -1; |
Kevin Pedretti | b33ac88 | 2007-10-21 08:54:53 +0200 | [diff] [blame] | 1795 | update_divide_count(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1796 | atomic_set(&apic->lapic_timer.pending, 0); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 1797 | if (kvm_vcpu_is_bsp(vcpu)) |
Gleb Natapov | 5dbc8f3 | 2012-08-05 15:58:27 +0300 | [diff] [blame] | 1798 | kvm_lapic_set_base(vcpu, |
| 1799 | vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1800 | vcpu->arch.pv_eoi.msr_val = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1801 | apic_update_ppr(apic); |
| 1802 | |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1803 | vcpu->arch.apic_arb_prio = 0; |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 1804 | vcpu->arch.apic_attention = 0; |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1805 | |
Nadav Amit | 98eff52 | 2014-06-29 12:28:51 +0300 | [diff] [blame] | 1806 | apic_debug("%s: vcpu=%p, id=%d, base_msr=" |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1807 | "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1808 | vcpu, kvm_apic_id(apic), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1809 | vcpu->arch.apic_base, apic->base_address); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1810 | } |
| 1811 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1812 | /* |
| 1813 | *---------------------------------------------------------------------- |
| 1814 | * timer interface |
| 1815 | *---------------------------------------------------------------------- |
| 1816 | */ |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1817 | |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 1818 | static bool lapic_is_periodic(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1819 | { |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1820 | return apic_lvtt_period(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1821 | } |
| 1822 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 1823 | int apic_has_pending_timer(struct kvm_vcpu *vcpu) |
| 1824 | { |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 1825 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 1826 | |
Paolo Bonzini | 1e3161b4 | 2016-01-08 13:41:16 +0100 | [diff] [blame] | 1827 | if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT)) |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 1828 | return atomic_read(&apic->lapic_timer.pending); |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 1829 | |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
Avi Kivity | 8934208 | 2011-11-10 14:57:21 +0200 | [diff] [blame] | 1833 | int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type) |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1834 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1835 | u32 reg = kvm_lapic_get_reg(apic, lvt_type); |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1836 | int vector, mode, trig_mode; |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1837 | |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1838 | if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) { |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1839 | vector = reg & APIC_VECTOR_MASK; |
| 1840 | mode = reg & APIC_MODE_MASK; |
| 1841 | trig_mode = reg & APIC_LVT_LEVEL_TRIGGER; |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 1842 | return __apic_accept_irq(apic, mode, vector, 1, trig_mode, |
| 1843 | NULL); |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1844 | } |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Jan Kiszka | 8fdb235 | 2008-10-20 10:20:02 +0200 | [diff] [blame] | 1848 | void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu) |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1849 | { |
Jan Kiszka | 8fdb235 | 2008-10-20 10:20:02 +0200 | [diff] [blame] | 1850 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1851 | |
| 1852 | if (apic) |
| 1853 | kvm_apic_local_deliver(apic, APIC_LVT0); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1854 | } |
| 1855 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 1856 | static const struct kvm_io_device_ops apic_mmio_ops = { |
| 1857 | .read = apic_mmio_read, |
| 1858 | .write = apic_mmio_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 1859 | }; |
| 1860 | |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 1861 | static enum hrtimer_restart apic_timer_fn(struct hrtimer *data) |
| 1862 | { |
| 1863 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 1864 | struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 1865 | |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1866 | apic_timer_expired(apic); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 1867 | |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 1868 | if (lapic_is_periodic(apic)) { |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 1869 | hrtimer_add_expires_ns(&ktimer->timer, ktimer->period); |
| 1870 | return HRTIMER_RESTART; |
| 1871 | } else |
| 1872 | return HRTIMER_NORESTART; |
| 1873 | } |
| 1874 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1875 | int kvm_create_lapic(struct kvm_vcpu *vcpu) |
| 1876 | { |
| 1877 | struct kvm_lapic *apic; |
| 1878 | |
| 1879 | ASSERT(vcpu != NULL); |
| 1880 | apic_debug("apic_init %d\n", vcpu->vcpu_id); |
| 1881 | |
| 1882 | apic = kzalloc(sizeof(*apic), GFP_KERNEL); |
| 1883 | if (!apic) |
| 1884 | goto nomem; |
| 1885 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1886 | vcpu->arch.apic = apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1887 | |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 1888 | apic->regs = (void *)get_zeroed_page(GFP_KERNEL); |
| 1889 | if (!apic->regs) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1890 | printk(KERN_ERR "malloc apic regs error for vcpu %x\n", |
| 1891 | vcpu->vcpu_id); |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 1892 | goto nomem_free_apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1893 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1894 | apic->vcpu = vcpu; |
| 1895 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1896 | hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC, |
Luiz Capitulino | 61abdbe | 2016-04-04 16:46:07 -0400 | [diff] [blame] | 1897 | HRTIMER_MODE_ABS_PINNED); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 1898 | apic->lapic_timer.timer.function = apic_timer_fn; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1899 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 1900 | /* |
| 1901 | * APIC is created enabled. This will prevent kvm_lapic_set_base from |
| 1902 | * thinking that APIC satet has changed. |
| 1903 | */ |
| 1904 | vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | 6aed64a | 2012-08-05 15:58:28 +0300 | [diff] [blame] | 1905 | kvm_lapic_set_base(vcpu, |
| 1906 | APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1907 | |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1908 | static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1909 | kvm_lapic_reset(vcpu, false); |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 1910 | kvm_iodevice_init(&apic->dev, &apic_mmio_ops); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1911 | |
| 1912 | return 0; |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 1913 | nomem_free_apic: |
| 1914 | kfree(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1915 | nomem: |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1916 | return -ENOMEM; |
| 1917 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1918 | |
| 1919 | int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 1920 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1921 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1922 | int highest_irr; |
| 1923 | |
Paolo Bonzini | f8543d6 | 2016-01-08 13:42:24 +0100 | [diff] [blame] | 1924 | if (!apic_enabled(apic)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1925 | return -1; |
| 1926 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1927 | apic_update_ppr(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1928 | highest_irr = apic_find_highest_irr(apic); |
| 1929 | if ((highest_irr == -1) || |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1930 | ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI))) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1931 | return -1; |
| 1932 | return highest_irr; |
| 1933 | } |
| 1934 | |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 1935 | int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu) |
| 1936 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1937 | u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0); |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 1938 | int r = 0; |
| 1939 | |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1940 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) |
Chris Lalancette | e7dca5c | 2010-06-16 17:11:12 -0400 | [diff] [blame] | 1941 | r = 1; |
| 1942 | if ((lvt0 & APIC_LVT_MASKED) == 0 && |
| 1943 | GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT) |
| 1944 | r = 1; |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 1945 | return r; |
| 1946 | } |
| 1947 | |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1948 | void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu) |
| 1949 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1950 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1951 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 1952 | if (atomic_read(&apic->lapic_timer.pending) > 0) { |
Jan Kiszka | f1ed045 | 2013-04-28 14:00:41 +0200 | [diff] [blame] | 1953 | kvm_apic_local_deliver(apic, APIC_LVTT); |
Nadav Amit | fae0ba2 | 2014-08-18 22:42:13 +0300 | [diff] [blame] | 1954 | if (apic_lvtt_tscdeadline(apic)) |
| 1955 | apic->lapic_timer.tscdeadline = 0; |
Jan Kiszka | f1ed045 | 2013-04-28 14:00:41 +0200 | [diff] [blame] | 1956 | atomic_set(&apic->lapic_timer.pending, 0); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1960 | int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu) |
| 1961 | { |
| 1962 | int vector = kvm_apic_has_interrupt(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1963 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1964 | |
| 1965 | if (vector == -1) |
| 1966 | return -1; |
| 1967 | |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 1968 | /* |
| 1969 | * We get here even with APIC virtualization enabled, if doing |
| 1970 | * nested virtualization and L1 runs with the "acknowledge interrupt |
| 1971 | * on exit" mode. Then we cannot inject the interrupt via RVI, |
| 1972 | * because the process would deliver it through the IDT. |
| 1973 | */ |
| 1974 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 1975 | apic_set_isr(vector, apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1976 | apic_update_ppr(apic); |
| 1977 | apic_clear_irr(vector, apic); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1978 | |
| 1979 | if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) { |
| 1980 | apic_clear_isr(vector, apic); |
| 1981 | apic_update_ppr(apic); |
| 1982 | } |
| 1983 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1984 | return vector; |
| 1985 | } |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 1986 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 1987 | static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, |
| 1988 | struct kvm_lapic_state *s, bool set) |
| 1989 | { |
| 1990 | if (apic_x2apic_mode(vcpu->arch.apic)) { |
| 1991 | u32 *id = (u32 *)(s->regs + APIC_ID); |
| 1992 | |
| 1993 | if (set) |
| 1994 | *id >>= 24; |
| 1995 | else |
| 1996 | *id <<= 24; |
| 1997 | } |
| 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s) |
| 2003 | { |
| 2004 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s)); |
| 2005 | return kvm_apic_state_fixup(vcpu, s, false); |
| 2006 | } |
| 2007 | |
| 2008 | int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s) |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2009 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2010 | struct kvm_lapic *apic = vcpu->arch.apic; |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 2011 | int r; |
| 2012 | |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2013 | |
Gleb Natapov | 5dbc8f3 | 2012-08-05 15:58:27 +0300 | [diff] [blame] | 2014 | kvm_lapic_set_base(vcpu, vcpu->arch.apic_base); |
Gleb Natapov | 64eb062 | 2012-08-08 15:24:36 +0300 | [diff] [blame] | 2015 | /* set SPIV separately to get count of SW disabled APICs right */ |
| 2016 | apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV))); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 2017 | |
| 2018 | r = kvm_apic_state_fixup(vcpu, s, true); |
| 2019 | if (r) |
| 2020 | return r; |
Gleb Natapov | 64eb062 | 2012-08-08 15:24:36 +0300 | [diff] [blame] | 2021 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 2022 | |
| 2023 | recalculate_apic_map(vcpu->kvm); |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 2024 | kvm_apic_set_version(vcpu); |
| 2025 | |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2026 | apic_update_ppr(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 2027 | hrtimer_cancel(&apic->lapic_timer.timer); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 2028 | apic_update_lvtt(apic); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2029 | apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0)); |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2030 | update_divide_count(apic); |
| 2031 | start_apic_timer(apic); |
Marcelo Tosatti | 6e24a6e | 2009-12-14 17:37:35 -0200 | [diff] [blame] | 2032 | apic->irr_pending = true; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2033 | apic->isr_count = vcpu->arch.apicv_active ? |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2034 | 1 : count_vectors(apic->regs + APIC_ISR); |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 2035 | apic->highest_isr_cache = -1; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2036 | if (vcpu->arch.apicv_active) { |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 2037 | if (kvm_x86_ops->apicv_post_state_restore) |
| 2038 | kvm_x86_ops->apicv_post_state_restore(vcpu); |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 2039 | kvm_x86_ops->hwapic_irr_update(vcpu, |
| 2040 | apic_find_highest_irr(apic)); |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 2041 | kvm_x86_ops->hwapic_isr_update(vcpu, |
Tiejun Chen | b4eef9b | 2014-12-22 10:32:57 +0100 | [diff] [blame] | 2042 | apic_find_highest_isr(apic)); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2043 | } |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 2044 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 2045 | if (ioapic_in_kernel(vcpu->kvm)) |
| 2046 | kvm_rtc_eoi_tracking_restore_one(vcpu); |
Radim Krčmář | 0669a51 | 2015-10-30 15:48:20 +0100 | [diff] [blame] | 2047 | |
| 2048 | vcpu->arch.apic_arb_prio = 0; |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame^] | 2049 | |
| 2050 | return 0; |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2051 | } |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2052 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 2053 | void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu) |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2054 | { |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2055 | struct hrtimer *timer; |
| 2056 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2057 | if (!lapic_in_kernel(vcpu)) |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2058 | return; |
| 2059 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2060 | timer = &vcpu->arch.apic->lapic_timer.timer; |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2061 | if (hrtimer_cancel(timer)) |
Luiz Capitulino | 61abdbe | 2016-04-04 16:46:07 -0400 | [diff] [blame] | 2062 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2063 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2064 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2065 | /* |
| 2066 | * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt |
| 2067 | * |
| 2068 | * Detect whether guest triggered PV EOI since the |
| 2069 | * last entry. If yes, set EOI on guests's behalf. |
| 2070 | * Clear PV EOI in guest memory in any case. |
| 2071 | */ |
| 2072 | static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu, |
| 2073 | struct kvm_lapic *apic) |
| 2074 | { |
| 2075 | bool pending; |
| 2076 | int vector; |
| 2077 | /* |
| 2078 | * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host |
| 2079 | * and KVM_PV_EOI_ENABLED in guest memory as follows: |
| 2080 | * |
| 2081 | * KVM_APIC_PV_EOI_PENDING is unset: |
| 2082 | * -> host disabled PV EOI. |
| 2083 | * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set: |
| 2084 | * -> host enabled PV EOI, guest did not execute EOI yet. |
| 2085 | * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset: |
| 2086 | * -> host enabled PV EOI, guest executed EOI. |
| 2087 | */ |
| 2088 | BUG_ON(!pv_eoi_enabled(vcpu)); |
| 2089 | pending = pv_eoi_get_pending(vcpu); |
| 2090 | /* |
| 2091 | * Clear pending bit in any case: it will be set again on vmentry. |
| 2092 | * While this might not be ideal from performance point of view, |
| 2093 | * this makes sure pv eoi is only enabled when we know it's safe. |
| 2094 | */ |
| 2095 | pv_eoi_clr_pending(vcpu); |
| 2096 | if (pending) |
| 2097 | return; |
| 2098 | vector = apic_set_eoi(apic); |
| 2099 | trace_kvm_pv_eoi(apic, vector); |
| 2100 | } |
| 2101 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2102 | void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu) |
| 2103 | { |
| 2104 | u32 data; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2105 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2106 | if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention)) |
| 2107 | apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic); |
| 2108 | |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2109 | if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention)) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2110 | return; |
| 2111 | |
Nicholas Krause | 603242a | 2015-08-05 10:44:40 -0400 | [diff] [blame] | 2112 | if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data, |
| 2113 | sizeof(u32))) |
| 2114 | return; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2115 | |
| 2116 | apic_set_tpr(vcpu->arch.apic, data & 0xff); |
| 2117 | } |
| 2118 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2119 | /* |
| 2120 | * apic_sync_pv_eoi_to_guest - called before vmentry |
| 2121 | * |
| 2122 | * Detect whether it's safe to enable PV EOI and |
| 2123 | * if yes do so. |
| 2124 | */ |
| 2125 | static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu, |
| 2126 | struct kvm_lapic *apic) |
| 2127 | { |
| 2128 | if (!pv_eoi_enabled(vcpu) || |
| 2129 | /* IRR set or many bits in ISR: could be nested. */ |
| 2130 | apic->irr_pending || |
| 2131 | /* Cache not set: could be safe but we don't bother. */ |
| 2132 | apic->highest_isr_cache == -1 || |
| 2133 | /* Need EOI to update ioapic. */ |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 2134 | kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) { |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2135 | /* |
| 2136 | * PV EOI was disabled by apic_sync_pv_eoi_from_guest |
| 2137 | * so we need not do anything here. |
| 2138 | */ |
| 2139 | return; |
| 2140 | } |
| 2141 | |
| 2142 | pv_eoi_set_pending(apic->vcpu); |
| 2143 | } |
| 2144 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2145 | void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu) |
| 2146 | { |
| 2147 | u32 data, tpr; |
| 2148 | int max_irr, max_isr; |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2149 | struct kvm_lapic *apic = vcpu->arch.apic; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2150 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2151 | apic_sync_pv_eoi_to_guest(vcpu, apic); |
| 2152 | |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2153 | if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention)) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2154 | return; |
| 2155 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2156 | tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2157 | max_irr = apic_find_highest_irr(apic); |
| 2158 | if (max_irr < 0) |
| 2159 | max_irr = 0; |
| 2160 | max_isr = apic_find_highest_isr(apic); |
| 2161 | if (max_isr < 0) |
| 2162 | max_isr = 0; |
| 2163 | data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24); |
| 2164 | |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2165 | kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data, |
| 2166 | sizeof(u32)); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2167 | } |
| 2168 | |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2169 | int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2170 | { |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2171 | if (vapic_addr) { |
| 2172 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, |
| 2173 | &vcpu->arch.apic->vapic_cache, |
| 2174 | vapic_addr, sizeof(u32))) |
| 2175 | return -EINVAL; |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2176 | __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention); |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2177 | } else { |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2178 | __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention); |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | vcpu->arch.apic->vapic_addr = vapic_addr; |
| 2182 | return 0; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2183 | } |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2184 | |
| 2185 | int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 2186 | { |
| 2187 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2188 | u32 reg = (msr - APIC_BASE_MSR) << 4; |
| 2189 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 2190 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2191 | return 1; |
| 2192 | |
Nadav Amit | c69d3d9 | 2014-11-26 17:56:25 +0200 | [diff] [blame] | 2193 | if (reg == APIC_ICR2) |
| 2194 | return 1; |
| 2195 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2196 | /* if this is ICR write vector before command */ |
Radim Krčmář | decdc28 | 2014-11-26 17:07:05 +0100 | [diff] [blame] | 2197 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2198 | kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32)); |
| 2199 | return kvm_lapic_reg_write(apic, reg, (u32)data); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data) |
| 2203 | { |
| 2204 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2205 | u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0; |
| 2206 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 2207 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2208 | return 1; |
| 2209 | |
Nadav Amit | c69d3d9 | 2014-11-26 17:56:25 +0200 | [diff] [blame] | 2210 | if (reg == APIC_DFR || reg == APIC_ICR2) { |
| 2211 | apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n", |
| 2212 | reg); |
| 2213 | return 1; |
| 2214 | } |
| 2215 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2216 | if (kvm_lapic_reg_read(apic, reg, 4, &low)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2217 | return 1; |
Radim Krčmář | decdc28 | 2014-11-26 17:07:05 +0100 | [diff] [blame] | 2218 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2219 | kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2220 | |
| 2221 | *data = (((u64)high) << 32) | low; |
| 2222 | |
| 2223 | return 0; |
| 2224 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2225 | |
| 2226 | int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data) |
| 2227 | { |
| 2228 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2229 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2230 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2231 | return 1; |
| 2232 | |
| 2233 | /* if this is ICR write vector before command */ |
| 2234 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2235 | kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32)); |
| 2236 | return kvm_lapic_reg_write(apic, reg, (u32)data); |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data) |
| 2240 | { |
| 2241 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2242 | u32 low, high = 0; |
| 2243 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2244 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2245 | return 1; |
| 2246 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2247 | if (kvm_lapic_reg_read(apic, reg, 4, &low)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2248 | return 1; |
| 2249 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2250 | kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high); |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2251 | |
| 2252 | *data = (((u64)high) << 32) | low; |
| 2253 | |
| 2254 | return 0; |
| 2255 | } |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2256 | |
| 2257 | int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data) |
| 2258 | { |
| 2259 | u64 addr = data & ~KVM_MSR_ENABLED; |
| 2260 | if (!IS_ALIGNED(addr, 4)) |
| 2261 | return 1; |
| 2262 | |
| 2263 | vcpu->arch.pv_eoi.msr_val = data; |
| 2264 | if (!pv_eoi_enabled(vcpu)) |
| 2265 | return 0; |
| 2266 | return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data, |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2267 | addr, sizeof(u8)); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2268 | } |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2269 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2270 | void kvm_apic_accept_events(struct kvm_vcpu *vcpu) |
| 2271 | { |
| 2272 | struct kvm_lapic *apic = vcpu->arch.apic; |
Paolo Bonzini | 2b4a273 | 2014-11-24 14:35:24 +0100 | [diff] [blame] | 2273 | u8 sipi_vector; |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2274 | unsigned long pe; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2275 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2276 | if (!lapic_in_kernel(vcpu) || !apic->pending_events) |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2277 | return; |
| 2278 | |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2279 | /* |
| 2280 | * INITs are latched while in SMM. Because an SMM CPU cannot |
| 2281 | * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs |
| 2282 | * and delay processing of INIT until the next RSM. |
| 2283 | */ |
| 2284 | if (is_smm(vcpu)) { |
| 2285 | WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED); |
| 2286 | if (test_bit(KVM_APIC_SIPI, &apic->pending_events)) |
| 2287 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 2288 | return; |
| 2289 | } |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2290 | |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2291 | pe = xchg(&apic->pending_events, 0); |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2292 | if (test_bit(KVM_APIC_INIT, &pe)) { |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 2293 | kvm_lapic_reset(vcpu, true); |
| 2294 | kvm_vcpu_reset(vcpu, true); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2295 | if (kvm_vcpu_is_bsp(apic->vcpu)) |
| 2296 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 2297 | else |
| 2298 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 2299 | } |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2300 | if (test_bit(KVM_APIC_SIPI, &pe) && |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2301 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { |
| 2302 | /* evaluate pending_events before reading the vector */ |
| 2303 | smp_rmb(); |
| 2304 | sipi_vector = apic->sipi_vector; |
Nadav Amit | 98eff52 | 2014-06-29 12:28:51 +0300 | [diff] [blame] | 2305 | apic_debug("vcpu %d received sipi with vector # %x\n", |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2306 | vcpu->vcpu_id, sipi_vector); |
| 2307 | kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector); |
| 2308 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 2309 | } |
| 2310 | } |
| 2311 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2312 | void kvm_lapic_init(void) |
| 2313 | { |
| 2314 | /* do not patch jump label more than once per second */ |
| 2315 | jump_label_rate_limit(&apic_hw_disabled, HZ); |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2316 | jump_label_rate_limit(&apic_sw_disabled, HZ); |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2317 | } |