Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Local APIC virtualization |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * Copyright (C) 2007 Novell |
| 8 | * Copyright (C) 2007 Intel |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 9 | * Copyright 2009 Red Hat, Inc. and/or its affiliates. |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 10 | * |
| 11 | * Authors: |
| 12 | * Dor Laor <dor.laor@qumranet.com> |
| 13 | * Gregory Haskins <ghaskins@novell.com> |
| 14 | * Yaozu (Eddie) Dong <eddie.dong@intel.com> |
| 15 | * |
| 16 | * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation. |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 17 | */ |
| 18 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 19 | #include <linux/kvm_host.h> |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 20 | #include <linux/kvm.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/hrtimer.h> |
| 25 | #include <linux/io.h> |
Paul Gortmaker | 1767e93 | 2016-07-13 20:19:00 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 27 | #include <linux/math64.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 29 | #include <asm/processor.h> |
| 30 | #include <asm/msr.h> |
| 31 | #include <asm/page.h> |
| 32 | #include <asm/current.h> |
| 33 | #include <asm/apicdef.h> |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 34 | #include <asm/delay.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 35 | #include <linux/atomic.h> |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 36 | #include <linux/jump_label.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 37 | #include "kvm_cache_regs.h" |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 38 | #include "irq.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 39 | #include "trace.h" |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 40 | #include "x86.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 41 | #include "cpuid.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 42 | #include "hyperv.h" |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 43 | |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 44 | #ifndef CONFIG_X86_64 |
| 45 | #define mod_64(x, y) ((x) - (y) * div64_u64(x, y)) |
| 46 | #else |
| 47 | #define mod_64(x, y) ((x) % (y)) |
| 48 | #endif |
| 49 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 50 | #define PRId64 "d" |
| 51 | #define PRIx64 "llx" |
| 52 | #define PRIu64 "u" |
| 53 | #define PRIo64 "o" |
| 54 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 55 | /* 14 is the version for Xeon and Pentium 8.4.8*/ |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 56 | #define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 57 | #define LAPIC_MMIO_LENGTH (1 << 12) |
| 58 | /* followed define is not in apicdef.h */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 59 | #define MAX_APIC_VECTOR 256 |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 60 | #define APIC_VECTORS_PER_REG 32 |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 61 | |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 62 | #define APIC_BROADCAST 0xFF |
| 63 | #define X2APIC_BROADCAST 0xFFFFFFFFul |
| 64 | |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 65 | static bool lapic_timer_advance_dynamic __read_mostly; |
Wanpeng Li | a0f0037 | 2019-09-26 08:54:03 +0800 | [diff] [blame] | 66 | #define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */ |
| 67 | #define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */ |
| 68 | #define LAPIC_TIMER_ADVANCE_NS_INIT 1000 |
| 69 | #define LAPIC_TIMER_ADVANCE_NS_MAX 5000 |
Wanpeng Li | 3b8a5df | 2018-10-09 09:02:08 +0800 | [diff] [blame] | 70 | /* step-by-step approximation to mitigate fluctuation */ |
| 71 | #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8 |
| 72 | |
Michael S. Tsirkin | a0c9a822 | 2012-04-11 18:49:55 +0300 | [diff] [blame] | 73 | static inline int apic_test_vector(int vec, void *bitmap) |
| 74 | { |
| 75 | return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 76 | } |
| 77 | |
Yang Zhang | 1060691 | 2013-04-11 19:21:38 +0800 | [diff] [blame] | 78 | bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector) |
| 79 | { |
| 80 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 81 | |
| 82 | return apic_test_vector(vector, apic->regs + APIC_ISR) || |
| 83 | apic_test_vector(vector, apic->regs + APIC_IRR); |
| 84 | } |
| 85 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 86 | static inline int __apic_test_and_set_vector(int vec, void *bitmap) |
| 87 | { |
| 88 | return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 89 | } |
| 90 | |
| 91 | static inline int __apic_test_and_clear_vector(int vec, void *bitmap) |
| 92 | { |
| 93 | return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); |
| 94 | } |
| 95 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 96 | struct static_key_deferred apic_hw_disabled __read_mostly; |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 97 | struct static_key_deferred apic_sw_disabled __read_mostly; |
| 98 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 99 | static inline int apic_enabled(struct kvm_lapic *apic) |
| 100 | { |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 101 | return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic); |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 102 | } |
| 103 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 104 | #define LVT_MASK \ |
| 105 | (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK) |
| 106 | |
| 107 | #define LINT_MASK \ |
| 108 | (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \ |
| 109 | APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER) |
| 110 | |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 111 | static inline u32 kvm_x2apic_id(struct kvm_lapic *apic) |
| 112 | { |
| 113 | return apic->vcpu->vcpu_id; |
| 114 | } |
| 115 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 116 | bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu) |
| 117 | { |
| 118 | return pi_inject_timer && kvm_vcpu_apicv_active(vcpu); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(kvm_can_post_timer_interrupt); |
| 121 | |
| 122 | static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu) |
| 123 | { |
| 124 | return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE; |
| 125 | } |
| 126 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 127 | static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, |
| 128 | u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { |
| 129 | switch (map->mode) { |
| 130 | case KVM_APIC_MODE_X2APIC: { |
| 131 | u32 offset = (dest_id >> 16) * 16; |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 132 | u32 max_apic_id = map->max_apic_id; |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 133 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 134 | if (offset <= max_apic_id) { |
| 135 | u8 cluster_size = min(max_apic_id - offset + 1, 16U); |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 136 | |
Paolo Bonzini | 1d487e9 | 2019-04-11 11:16:47 +0200 | [diff] [blame] | 137 | offset = array_index_nospec(offset, map->max_apic_id + 1); |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 138 | *cluster = &map->phys_map[offset]; |
| 139 | *mask = dest_id & (0xffff >> (16 - cluster_size)); |
| 140 | } else { |
| 141 | *mask = 0; |
| 142 | } |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 143 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | case KVM_APIC_MODE_XAPIC_FLAT: |
| 147 | *cluster = map->xapic_flat_map; |
| 148 | *mask = dest_id & 0xff; |
| 149 | return true; |
| 150 | case KVM_APIC_MODE_XAPIC_CLUSTER: |
Radim Krčmář | 444fdad | 2016-11-22 20:20:14 +0100 | [diff] [blame] | 151 | *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf]; |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 152 | *mask = dest_id & 0xf; |
| 153 | return true; |
| 154 | default: |
| 155 | /* Not optimized. */ |
| 156 | return false; |
| 157 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 158 | } |
| 159 | |
Radim Krčmář | af1bae5 | 2016-07-12 22:09:30 +0200 | [diff] [blame] | 160 | static void kvm_apic_map_free(struct rcu_head *rcu) |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 161 | { |
Radim Krčmář | af1bae5 | 2016-07-12 22:09:30 +0200 | [diff] [blame] | 162 | struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu); |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 163 | |
Radim Krčmář | af1bae5 | 2016-07-12 22:09:30 +0200 | [diff] [blame] | 164 | kvfree(map); |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 167 | static void recalculate_apic_map(struct kvm *kvm) |
| 168 | { |
| 169 | struct kvm_apic_map *new, *old = NULL; |
| 170 | struct kvm_vcpu *vcpu; |
| 171 | int i; |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 172 | u32 max_id = 255; /* enough space for any xAPIC ID */ |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 173 | |
| 174 | mutex_lock(&kvm->arch.apic_map_lock); |
| 175 | |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 176 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 177 | if (kvm_apic_present(vcpu)) |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 178 | max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic)); |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 179 | |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 180 | new = kvzalloc(sizeof(struct kvm_apic_map) + |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 181 | sizeof(struct kvm_lapic *) * ((u64)max_id + 1), |
| 182 | GFP_KERNEL_ACCOUNT); |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 183 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 184 | if (!new) |
| 185 | goto out; |
| 186 | |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 187 | new->max_apic_id = max_id; |
| 188 | |
Nadav Amit | 173beed | 2014-11-02 11:54:54 +0200 | [diff] [blame] | 189 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 190 | struct kvm_lapic *apic = vcpu->arch.apic; |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 191 | struct kvm_lapic **cluster; |
| 192 | u16 mask; |
Radim Krčmář | 5bd5db3 | 2016-12-15 18:06:48 +0100 | [diff] [blame] | 193 | u32 ldr; |
| 194 | u8 xapic_id; |
| 195 | u32 x2apic_id; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 196 | |
Radim Krčmář | df04d1d | 2015-01-29 22:33:35 +0100 | [diff] [blame] | 197 | if (!kvm_apic_present(vcpu)) |
| 198 | continue; |
| 199 | |
Radim Krčmář | 5bd5db3 | 2016-12-15 18:06:48 +0100 | [diff] [blame] | 200 | xapic_id = kvm_xapic_id(apic); |
| 201 | x2apic_id = kvm_x2apic_id(apic); |
| 202 | |
| 203 | /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */ |
| 204 | if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) && |
| 205 | x2apic_id <= new->max_apic_id) |
| 206 | new->phys_map[x2apic_id] = apic; |
| 207 | /* |
| 208 | * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around, |
| 209 | * prevent them from masking VCPUs with APIC ID <= 0xff. |
| 210 | */ |
| 211 | if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id]) |
| 212 | new->phys_map[xapic_id] = apic; |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 213 | |
Radim Krcmar | b14c876 | 2019-08-13 23:37:37 -0400 | [diff] [blame] | 214 | if (!kvm_apic_sw_enabled(apic)) |
| 215 | continue; |
| 216 | |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 217 | ldr = kvm_lapic_get_reg(apic, APIC_LDR); |
| 218 | |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 219 | if (apic_x2apic_mode(apic)) { |
| 220 | new->mode |= KVM_APIC_MODE_X2APIC; |
| 221 | } else if (ldr) { |
| 222 | ldr = GET_APIC_LOGICAL_ID(ldr); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 223 | if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT) |
Radim Krčmář | 3b5a5ff | 2015-02-12 19:41:34 +0100 | [diff] [blame] | 224 | new->mode |= KVM_APIC_MODE_XAPIC_FLAT; |
| 225 | else |
| 226 | new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER; |
| 227 | } |
| 228 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 229 | if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask)) |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 230 | continue; |
| 231 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 232 | if (mask) |
| 233 | cluster[ffs(mask) - 1] = apic; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 234 | } |
| 235 | out: |
| 236 | old = rcu_dereference_protected(kvm->arch.apic_map, |
| 237 | lockdep_is_held(&kvm->arch.apic_map_lock)); |
| 238 | rcu_assign_pointer(kvm->arch.apic_map, new); |
| 239 | mutex_unlock(&kvm->arch.apic_map_lock); |
| 240 | |
| 241 | if (old) |
Radim Krčmář | af1bae5 | 2016-07-12 22:09:30 +0200 | [diff] [blame] | 242 | call_rcu(&old->rcu, kvm_apic_map_free); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 243 | |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 244 | kvm_make_scan_ioapic_request(kvm); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 245 | } |
| 246 | |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 247 | static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val) |
| 248 | { |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 249 | bool enabled = val & APIC_SPIV_APIC_ENABLED; |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 250 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 251 | kvm_lapic_set_reg(apic, APIC_SPIV, val); |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 252 | |
| 253 | if (enabled != apic->sw_enabled) { |
| 254 | apic->sw_enabled = enabled; |
Peng Hao | eb1ff0a | 2018-12-04 17:42:50 +0800 | [diff] [blame] | 255 | if (enabled) |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 256 | static_key_slow_dec_deferred(&apic_sw_disabled); |
Peng Hao | eb1ff0a | 2018-12-04 17:42:50 +0800 | [diff] [blame] | 257 | else |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 258 | static_key_slow_inc(&apic_sw_disabled.key); |
Radim Krcmar | b14c876 | 2019-08-13 23:37:37 -0400 | [diff] [blame] | 259 | |
| 260 | recalculate_apic_map(apic->vcpu->kvm); |
Nadav Amit | 1e1b6c2 | 2014-08-19 00:03:00 +0300 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 264 | 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] | 265 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 266 | kvm_lapic_set_reg(apic, APIC_ID, id << 24); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 267 | recalculate_apic_map(apic->vcpu->kvm); |
| 268 | } |
| 269 | |
| 270 | static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id) |
| 271 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 272 | kvm_lapic_set_reg(apic, APIC_LDR, id); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 273 | recalculate_apic_map(apic->vcpu->kvm); |
| 274 | } |
| 275 | |
Dr. David Alan Gilbert | e872fa9 | 2017-11-17 11:52:49 +0000 | [diff] [blame] | 276 | static inline u32 kvm_apic_calc_x2apic_ldr(u32 id) |
| 277 | { |
| 278 | return ((id >> 4) << 16) | (1 << (id & 0xf)); |
| 279 | } |
| 280 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 281 | 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] | 282 | { |
Dr. David Alan Gilbert | e872fa9 | 2017-11-17 11:52:49 +0000 | [diff] [blame] | 283 | u32 ldr = kvm_apic_calc_x2apic_ldr(id); |
Radim Krčmář | 257b9a5 | 2015-05-22 18:45:11 +0200 | [diff] [blame] | 284 | |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 285 | WARN_ON_ONCE(id != apic->vcpu->vcpu_id); |
| 286 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 287 | kvm_lapic_set_reg(apic, APIC_ID, id); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 288 | kvm_lapic_set_reg(apic, APIC_LDR, ldr); |
Radim Krčmář | 257b9a5 | 2015-05-22 18:45:11 +0200 | [diff] [blame] | 289 | recalculate_apic_map(apic->vcpu->kvm); |
| 290 | } |
| 291 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 292 | static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type) |
| 293 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 294 | return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type) |
| 298 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 299 | return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 300 | } |
| 301 | |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 302 | static inline int apic_lvtt_oneshot(struct kvm_lapic *apic) |
| 303 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 304 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 307 | static inline int apic_lvtt_period(struct kvm_lapic *apic) |
| 308 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 309 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic) |
| 313 | { |
Radim Krčmář | f30ebc3 | 2014-10-30 15:06:47 +0100 | [diff] [blame] | 314 | return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 315 | } |
| 316 | |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 317 | static inline int apic_lvt_nmi_mode(u32 lvt_val) |
| 318 | { |
| 319 | return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI; |
| 320 | } |
| 321 | |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 322 | void kvm_apic_set_version(struct kvm_vcpu *vcpu) |
| 323 | { |
| 324 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 325 | struct kvm_cpuid_entry2 *feat; |
| 326 | u32 v = APIC_VERSION; |
| 327 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 328 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 329 | return; |
| 330 | |
Vitaly Kuznetsov | 0bcc3fb | 2018-02-09 14:01:33 +0100 | [diff] [blame] | 331 | /* |
| 332 | * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation) |
| 333 | * which doesn't have EOI register; Some buggy OSes (e.g. Windows with |
| 334 | * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC |
| 335 | * version first and level-triggered interrupts never get EOIed in |
| 336 | * IOAPIC. |
| 337 | */ |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 338 | feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0); |
Vitaly Kuznetsov | 0bcc3fb | 2018-02-09 14:01:33 +0100 | [diff] [blame] | 339 | if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))) && |
| 340 | !ioapic_in_kernel(vcpu->kvm)) |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 341 | v |= APIC_LVR_DIRECTED_EOI; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 342 | kvm_lapic_set_reg(apic, APIC_LVR, v); |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 343 | } |
| 344 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 345 | static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = { |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 346 | LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 347 | LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */ |
| 348 | LVT_MASK | APIC_MODE_MASK, /* LVTPC */ |
| 349 | LINT_MASK, LINT_MASK, /* LVT0-1 */ |
| 350 | LVT_MASK /* LVTERR */ |
| 351 | }; |
| 352 | |
| 353 | static int find_highest_vector(void *bitmap) |
| 354 | { |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 355 | int vec; |
| 356 | u32 *reg; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 357 | |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 358 | for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG; |
| 359 | vec >= 0; vec -= APIC_VECTORS_PER_REG) { |
| 360 | reg = bitmap + REG_POS(vec); |
| 361 | if (*reg) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 362 | return __fls(*reg) + vec; |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 363 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 364 | |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 365 | return -1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 366 | } |
| 367 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 368 | static u8 count_vectors(void *bitmap) |
| 369 | { |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 370 | int vec; |
| 371 | u32 *reg; |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 372 | u8 count = 0; |
Takuya Yoshikawa | ecba9a5 | 2012-09-05 19:30:01 +0900 | [diff] [blame] | 373 | |
| 374 | for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) { |
| 375 | reg = bitmap + REG_POS(vec); |
| 376 | count += hweight32(*reg); |
| 377 | } |
| 378 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 379 | return count; |
| 380 | } |
| 381 | |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 382 | bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 383 | { |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 384 | u32 i, vec; |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 385 | u32 pir_val, irr_val, prev_irr_val; |
| 386 | int max_updated_irr; |
| 387 | |
| 388 | max_updated_irr = -1; |
| 389 | *max_irr = -1; |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 390 | |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 391 | for (i = vec = 0; i <= 7; i++, vec += 32) { |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 392 | pir_val = READ_ONCE(pir[i]); |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 393 | irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10)); |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 394 | if (pir_val) { |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 395 | prev_irr_val = irr_val; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 396 | irr_val |= xchg(&pir[i], 0); |
| 397 | *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val; |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 398 | if (prev_irr_val != irr_val) { |
| 399 | max_updated_irr = |
| 400 | __fls(irr_val ^ prev_irr_val) + vec; |
| 401 | } |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 402 | } |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 403 | if (irr_val) |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 404 | *max_irr = __fls(irr_val) + vec; |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 405 | } |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 406 | |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 407 | return ((max_updated_irr != -1) && |
| 408 | (max_updated_irr == *max_irr)); |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 409 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 410 | EXPORT_SYMBOL_GPL(__kvm_apic_update_irr); |
| 411 | |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 412 | bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 413 | { |
| 414 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 415 | |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 416 | return __kvm_apic_update_irr(pir, apic->regs, max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 417 | } |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(kvm_apic_update_irr); |
| 419 | |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 420 | static inline int apic_search_irr(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 421 | { |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 422 | return find_highest_vector(apic->regs + APIC_IRR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static inline int apic_find_highest_irr(struct kvm_lapic *apic) |
| 426 | { |
| 427 | int result; |
| 428 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 429 | /* |
| 430 | * Note that irr_pending is just a hint. It will be always |
| 431 | * true with virtual interrupt delivery enabled. |
| 432 | */ |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 433 | if (!apic->irr_pending) |
| 434 | return -1; |
| 435 | |
| 436 | result = apic_search_irr(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 437 | ASSERT(result == -1 || result >= 16); |
| 438 | |
| 439 | return result; |
| 440 | } |
| 441 | |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 442 | static inline void apic_clear_irr(int vec, struct kvm_lapic *apic) |
| 443 | { |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 444 | struct kvm_vcpu *vcpu; |
| 445 | |
| 446 | vcpu = apic->vcpu; |
| 447 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 448 | if (unlikely(vcpu->arch.apicv_active)) { |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 449 | /* need to update RVI */ |
Wei Yang | ee171d2 | 2019-03-31 19:17:22 -0700 | [diff] [blame] | 450 | kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR); |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 451 | kvm_x86_ops->hwapic_irr_update(vcpu, |
| 452 | apic_find_highest_irr(apic)); |
Nadav Amit | f210f75 | 2014-11-16 23:49:07 +0200 | [diff] [blame] | 453 | } else { |
| 454 | apic->irr_pending = false; |
Wei Yang | ee171d2 | 2019-03-31 19:17:22 -0700 | [diff] [blame] | 455 | kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR); |
Nadav Amit | f210f75 | 2014-11-16 23:49:07 +0200 | [diff] [blame] | 456 | if (apic_search_irr(apic) != -1) |
| 457 | apic->irr_pending = true; |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 458 | } |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 459 | } |
| 460 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 461 | static inline void apic_set_isr(int vec, struct kvm_lapic *apic) |
| 462 | { |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 463 | struct kvm_vcpu *vcpu; |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 464 | |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 465 | if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR)) |
| 466 | return; |
| 467 | |
| 468 | vcpu = apic->vcpu; |
| 469 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 470 | /* |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 471 | * With APIC virtualization enabled, all caching is disabled |
| 472 | * because the processor can modify ISR under the hood. Instead |
| 473 | * just set SVI. |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 474 | */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 475 | if (unlikely(vcpu->arch.apicv_active)) |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 476 | kvm_x86_ops->hwapic_isr_update(vcpu, vec); |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 477 | else { |
| 478 | ++apic->isr_count; |
| 479 | BUG_ON(apic->isr_count > MAX_APIC_VECTOR); |
| 480 | /* |
| 481 | * ISR (in service register) bit is set when injecting an interrupt. |
| 482 | * The highest vector is injected. Thus the latest bit set matches |
| 483 | * the highest bit in ISR. |
| 484 | */ |
| 485 | apic->highest_isr_cache = vec; |
| 486 | } |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 489 | static inline int apic_find_highest_isr(struct kvm_lapic *apic) |
| 490 | { |
| 491 | int result; |
| 492 | |
| 493 | /* |
| 494 | * Note that isr_count is always 1, and highest_isr_cache |
| 495 | * is always -1, with APIC virtualization enabled. |
| 496 | */ |
| 497 | if (!apic->isr_count) |
| 498 | return -1; |
| 499 | if (likely(apic->highest_isr_cache != -1)) |
| 500 | return apic->highest_isr_cache; |
| 501 | |
| 502 | result = find_highest_vector(apic->regs + APIC_ISR); |
| 503 | ASSERT(result == -1 || result >= 16); |
| 504 | |
| 505 | return result; |
| 506 | } |
| 507 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 508 | static inline void apic_clear_isr(int vec, struct kvm_lapic *apic) |
| 509 | { |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 510 | struct kvm_vcpu *vcpu; |
| 511 | if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR)) |
| 512 | return; |
| 513 | |
| 514 | vcpu = apic->vcpu; |
| 515 | |
| 516 | /* |
| 517 | * We do get here for APIC virtualization enabled if the guest |
| 518 | * uses the Hyper-V APIC enlightenment. In this case we may need |
| 519 | * to trigger a new interrupt delivery by writing the SVI field; |
| 520 | * on the other hand isr_count and highest_isr_cache are unused |
| 521 | * and must be left alone. |
| 522 | */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 523 | if (unlikely(vcpu->arch.apicv_active)) |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 524 | kvm_x86_ops->hwapic_isr_update(vcpu, |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 525 | apic_find_highest_isr(apic)); |
| 526 | else { |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 527 | --apic->isr_count; |
Paolo Bonzini | fc57ac2 | 2014-05-14 17:40:58 +0200 | [diff] [blame] | 528 | BUG_ON(apic->isr_count < 0); |
| 529 | apic->highest_isr_cache = -1; |
| 530 | } |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 531 | } |
| 532 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 533 | int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu) |
| 534 | { |
Gleb Natapov | 33e4c68 | 2009-06-11 11:06:51 +0300 | [diff] [blame] | 535 | /* This may race with setting of irr in __apic_accept_irq() and |
| 536 | * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq |
| 537 | * will cause vmexit immediately and the value will be recalculated |
| 538 | * on the next vmentry. |
| 539 | */ |
Paolo Bonzini | f8543d6 | 2016-01-08 13:42:24 +0100 | [diff] [blame] | 540 | return apic_find_highest_irr(vcpu->arch.apic); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 541 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 542 | EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 543 | |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 544 | static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 545 | int vector, int level, int trig_mode, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 546 | struct dest_map *dest_map); |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 547 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 548 | 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] | 549 | struct dest_map *dest_map) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 550 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 551 | struct kvm_lapic *apic = vcpu->arch.apic; |
Zhang Xiantao | 8be5453 | 2007-12-02 22:35:57 +0800 | [diff] [blame] | 552 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 553 | return __apic_accept_irq(apic, irq->delivery_mode, irq->vector, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 554 | irq->level, irq->trig_mode, dest_map); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 555 | } |
| 556 | |
Miaohe Lin | 1a68623 | 2019-11-09 17:46:49 +0800 | [diff] [blame] | 557 | static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, |
| 558 | struct kvm_lapic_irq *irq, u32 min) |
| 559 | { |
| 560 | int i, count = 0; |
| 561 | struct kvm_vcpu *vcpu; |
| 562 | |
| 563 | if (min > map->max_apic_id) |
| 564 | return 0; |
| 565 | |
| 566 | for_each_set_bit(i, ipi_bitmap, |
| 567 | min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) { |
| 568 | if (map->phys_map[min + i]) { |
| 569 | vcpu = map->phys_map[min + i]->vcpu; |
| 570 | count += kvm_apic_set_irq(vcpu, irq, NULL); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return count; |
| 575 | } |
| 576 | |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 577 | int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, |
Wanpeng Li | bdf7ffc | 2018-08-30 10:03:30 +0800 | [diff] [blame] | 578 | unsigned long ipi_bitmap_high, u32 min, |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 579 | unsigned long icr, int op_64_bit) |
| 580 | { |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 581 | struct kvm_apic_map *map; |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 582 | struct kvm_lapic_irq irq = {0}; |
| 583 | int cluster_size = op_64_bit ? 64 : 32; |
Miaohe Lin | 1a68623 | 2019-11-09 17:46:49 +0800 | [diff] [blame] | 584 | int count; |
| 585 | |
| 586 | if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK)) |
| 587 | return -KVM_EINVAL; |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 588 | |
| 589 | irq.vector = icr & APIC_VECTOR_MASK; |
| 590 | irq.delivery_mode = icr & APIC_MODE_MASK; |
| 591 | irq.level = (icr & APIC_INT_ASSERT) != 0; |
| 592 | irq.trig_mode = icr & APIC_INT_LEVELTRIG; |
| 593 | |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 594 | rcu_read_lock(); |
| 595 | map = rcu_dereference(kvm->arch.apic_map); |
| 596 | |
Miaohe Lin | 1a68623 | 2019-11-09 17:46:49 +0800 | [diff] [blame] | 597 | count = -EOPNOTSUPP; |
| 598 | if (likely(map)) { |
| 599 | count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min); |
| 600 | min += cluster_size; |
| 601 | count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min); |
Wanpeng Li | 38ab012 | 2018-11-20 09:39:30 +0800 | [diff] [blame] | 602 | } |
| 603 | |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 604 | rcu_read_unlock(); |
| 605 | return count; |
| 606 | } |
| 607 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 608 | static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val) |
| 609 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 610 | |
| 611 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val, |
| 612 | sizeof(val)); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val) |
| 616 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 617 | |
| 618 | return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val, |
| 619 | sizeof(*val)); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu) |
| 623 | { |
| 624 | return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; |
| 625 | } |
| 626 | |
| 627 | static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu) |
| 628 | { |
| 629 | u8 val; |
| 630 | if (pv_eoi_get_user(vcpu, &val) < 0) |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 631 | printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 632 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 633 | return val & 0x1; |
| 634 | } |
| 635 | |
| 636 | static void pv_eoi_set_pending(struct kvm_vcpu *vcpu) |
| 637 | { |
| 638 | if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) { |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 639 | printk(KERN_WARNING "Can't set EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 640 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 641 | return; |
| 642 | } |
| 643 | __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention); |
| 644 | } |
| 645 | |
| 646 | static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu) |
| 647 | { |
| 648 | if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) { |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 649 | printk(KERN_WARNING "Can't clear EOI MSR value: 0x%llx\n", |
Chen Fan | 9689397 | 2014-01-02 17:14:11 +0800 | [diff] [blame] | 650 | (unsigned long long)vcpu->arch.pv_eoi.msr_val); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention); |
| 654 | } |
| 655 | |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 656 | static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr) |
| 657 | { |
Paolo Bonzini | 3d92789 | 2016-12-19 13:29:03 +0100 | [diff] [blame] | 658 | int highest_irr; |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 659 | if (apic->vcpu->arch.apicv_active) |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 660 | highest_irr = kvm_x86_ops->sync_pir_to_irr(apic->vcpu); |
| 661 | else |
| 662 | highest_irr = apic_find_highest_irr(apic); |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 663 | if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr) |
| 664 | return -1; |
| 665 | return highest_irr; |
| 666 | } |
| 667 | |
| 668 | static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 669 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 670 | u32 tpr, isrv, ppr, old_ppr; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 671 | int isr; |
| 672 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 673 | old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI); |
| 674 | tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 675 | isr = apic_find_highest_isr(apic); |
| 676 | isrv = (isr != -1) ? isr : 0; |
| 677 | |
| 678 | if ((tpr & 0xf0) >= (isrv & 0xf0)) |
| 679 | ppr = tpr & 0xff; |
| 680 | else |
| 681 | ppr = isrv & 0xf0; |
| 682 | |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 683 | *new_ppr = ppr; |
| 684 | if (old_ppr != ppr) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 685 | kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr); |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 686 | |
| 687 | return ppr < old_ppr; |
| 688 | } |
| 689 | |
| 690 | static void apic_update_ppr(struct kvm_lapic *apic) |
| 691 | { |
| 692 | u32 ppr; |
| 693 | |
Paolo Bonzini | 26fbbee | 2016-12-18 13:54:58 +0100 | [diff] [blame] | 694 | if (__apic_update_ppr(apic, &ppr) && |
| 695 | apic_has_interrupt_for_ppr(apic, ppr) != -1) |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 696 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 697 | } |
| 698 | |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 699 | void kvm_apic_update_ppr(struct kvm_vcpu *vcpu) |
| 700 | { |
| 701 | apic_update_ppr(vcpu->arch.apic); |
| 702 | } |
| 703 | EXPORT_SYMBOL_GPL(kvm_apic_update_ppr); |
| 704 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 705 | static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr) |
| 706 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 707 | kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 708 | apic_update_ppr(apic); |
| 709 | } |
| 710 | |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 711 | static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 712 | { |
Radim Krčmář | b4535b5 | 2016-12-15 18:06:47 +0100 | [diff] [blame] | 713 | return mda == (apic_x2apic_mode(apic) ? |
| 714 | X2APIC_BROADCAST : APIC_BROADCAST); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 715 | } |
| 716 | |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 717 | 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] | 718 | { |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 719 | if (kvm_apic_broadcast(apic, mda)) |
| 720 | return true; |
| 721 | |
| 722 | if (apic_x2apic_mode(apic)) |
Radim Krčmář | 6e50043 | 2016-12-15 18:06:46 +0100 | [diff] [blame] | 723 | return mda == kvm_x2apic_id(apic); |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 724 | |
Radim Krčmář | 5bd5db3 | 2016-12-15 18:06:48 +0100 | [diff] [blame] | 725 | /* |
| 726 | * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if |
| 727 | * it were in x2APIC mode. Hotplugged VCPUs start in xAPIC mode and |
| 728 | * this allows unique addressing of VCPUs with APIC ID over 0xff. |
| 729 | * The 0xff condition is needed because writeable xAPIC ID. |
| 730 | */ |
| 731 | if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic)) |
| 732 | return true; |
| 733 | |
Radim Krčmář | b4535b5 | 2016-12-15 18:06:47 +0100 | [diff] [blame] | 734 | return mda == kvm_xapic_id(apic); |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 735 | } |
| 736 | |
Radim Krčmář | 52c233a | 2015-01-29 22:48:48 +0100 | [diff] [blame] | 737 | 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] | 738 | { |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 739 | u32 logical_id; |
| 740 | |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 741 | if (kvm_apic_broadcast(apic, mda)) |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 742 | return true; |
Nadav Amit | 394457a | 2014-10-03 00:30:52 +0300 | [diff] [blame] | 743 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 744 | logical_id = kvm_lapic_get_reg(apic, APIC_LDR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 745 | |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 746 | if (apic_x2apic_mode(apic)) |
Radim Krčmář | 8a39536 | 2015-01-29 22:48:51 +0100 | [diff] [blame] | 747 | return ((logical_id >> 16) == (mda >> 16)) |
| 748 | && (logical_id & mda & 0xffff) != 0; |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 749 | |
| 750 | logical_id = GET_APIC_LOGICAL_ID(logical_id); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 751 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 752 | switch (kvm_lapic_get_reg(apic, APIC_DFR)) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 753 | case APIC_DFR_FLAT: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 754 | return (logical_id & mda) != 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 755 | case APIC_DFR_CLUSTER: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 756 | return ((logical_id >> 4) == (mda >> 4)) |
| 757 | && (logical_id & mda & 0xf) != 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 758 | default: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 759 | return false; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 760 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 761 | } |
| 762 | |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 763 | /* The KVM local APIC implementation has two quirks: |
| 764 | * |
Radim Krčmář | b4535b5 | 2016-12-15 18:06:47 +0100 | [diff] [blame] | 765 | * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs |
| 766 | * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID. |
| 767 | * KVM doesn't do that aliasing. |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 768 | * |
| 769 | * - in-kernel IOAPIC messages have to be delivered directly to |
| 770 | * x2APIC, because the kernel does not support interrupt remapping. |
| 771 | * In order to support broadcast without interrupt remapping, x2APIC |
| 772 | * rewrites the destination of non-IPI messages from APIC_BROADCAST |
| 773 | * to X2APIC_BROADCAST. |
| 774 | * |
| 775 | * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is |
| 776 | * important when userspace wants to use x2APIC-format MSIs, because |
| 777 | * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7". |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 778 | */ |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 779 | static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id, |
| 780 | struct kvm_lapic *source, struct kvm_lapic *target) |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 781 | { |
| 782 | bool ipi = source != NULL; |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 783 | |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 784 | if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled && |
Radim Krčmář | b4535b5 | 2016-12-15 18:06:47 +0100 | [diff] [blame] | 785 | !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target)) |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 786 | return X2APIC_BROADCAST; |
| 787 | |
Radim Krčmář | b4535b5 | 2016-12-15 18:06:47 +0100 | [diff] [blame] | 788 | return dest_id; |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Radim Krčmář | 52c233a | 2015-01-29 22:48:48 +0100 | [diff] [blame] | 791 | bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, |
Peter Xu | 5c69d5c | 2019-12-04 20:07:20 +0100 | [diff] [blame^] | 792 | int shorthand, unsigned int dest, int dest_mode) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 793 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 794 | struct kvm_lapic *target = vcpu->arch.apic; |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 795 | u32 mda = kvm_apic_mda(vcpu, dest, source, target); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 796 | |
Zachary Amsden | bd37139 | 2010-06-14 11:42:15 -1000 | [diff] [blame] | 797 | ASSERT(target); |
Peter Xu | 5c69d5c | 2019-12-04 20:07:20 +0100 | [diff] [blame^] | 798 | switch (shorthand) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 799 | case APIC_DEST_NOSHORT: |
Radim Krčmář | 3697f30 | 2015-01-29 22:48:50 +0100 | [diff] [blame] | 800 | if (dest_mode == APIC_DEST_PHYSICAL) |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 801 | return kvm_apic_match_physical_addr(target, mda); |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 802 | else |
Radim Krčmář | 03d2249 | 2015-02-12 19:41:31 +0100 | [diff] [blame] | 803 | return kvm_apic_match_logical_addr(target, mda); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 804 | case APIC_DEST_SELF: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 805 | return target == source; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 806 | case APIC_DEST_ALLINC: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 807 | return true; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 808 | case APIC_DEST_ALLBUT: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 809 | return target != source; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 810 | default: |
Radim Krčmář | 9368b56 | 2015-01-29 22:48:49 +0100 | [diff] [blame] | 811 | return false; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 812 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 813 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(kvm_apic_match_dest); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 815 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 816 | int kvm_vector_to_index(u32 vector, u32 dest_vcpus, |
| 817 | const unsigned long *bitmap, u32 bitmap_size) |
| 818 | { |
| 819 | u32 mod; |
| 820 | int i, idx = -1; |
| 821 | |
| 822 | mod = vector % dest_vcpus; |
| 823 | |
| 824 | for (i = 0; i <= mod; i++) { |
| 825 | idx = find_next_bit(bitmap, bitmap_size, idx + 1); |
| 826 | BUG_ON(idx == bitmap_size); |
| 827 | } |
| 828 | |
| 829 | return idx; |
| 830 | } |
| 831 | |
Radim Krčmář | 4efd805 | 2016-02-12 15:00:15 +0100 | [diff] [blame] | 832 | static void kvm_apic_disabled_lapic_found(struct kvm *kvm) |
| 833 | { |
| 834 | if (!kvm->arch.disabled_lapic_found) { |
| 835 | kvm->arch.disabled_lapic_found = true; |
| 836 | printk(KERN_INFO |
| 837 | "Disabled LAPIC found during irq injection\n"); |
| 838 | } |
| 839 | } |
| 840 | |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 841 | static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src, |
| 842 | struct kvm_lapic_irq *irq, struct kvm_apic_map *map) |
| 843 | { |
| 844 | if (kvm->arch.x2apic_broadcast_quirk_disabled) { |
| 845 | if ((irq->dest_id == APIC_BROADCAST && |
| 846 | map->mode != KVM_APIC_MODE_X2APIC)) |
| 847 | return true; |
| 848 | if (irq->dest_id == X2APIC_BROADCAST) |
| 849 | return true; |
| 850 | } else { |
| 851 | bool x2apic_ipi = src && *src && apic_x2apic_mode(*src); |
| 852 | if (irq->dest_id == (x2apic_ipi ? |
| 853 | X2APIC_BROADCAST : APIC_BROADCAST)) |
| 854 | return true; |
| 855 | } |
| 856 | |
| 857 | return false; |
| 858 | } |
| 859 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 860 | /* Return true if the interrupt can be handled by using *bitmap as index mask |
| 861 | * for valid destinations in *dst array. |
| 862 | * Return false if kvm_apic_map_get_dest_lapic did nothing useful. |
| 863 | * Note: we may have zero kvm_lapic destinations when we return true, which |
| 864 | * means that the interrupt should be dropped. In this case, *bitmap would be |
| 865 | * zero and *dst undefined. |
| 866 | */ |
| 867 | static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm, |
| 868 | struct kvm_lapic **src, struct kvm_lapic_irq *irq, |
| 869 | struct kvm_apic_map *map, struct kvm_lapic ***dst, |
| 870 | unsigned long *bitmap) |
| 871 | { |
| 872 | int i, lowest; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 873 | |
| 874 | if (irq->shorthand == APIC_DEST_SELF && src) { |
| 875 | *dst = src; |
| 876 | *bitmap = 1; |
| 877 | return true; |
| 878 | } else if (irq->shorthand) |
| 879 | return false; |
| 880 | |
Radim Krčmář | c519265 | 2016-07-12 22:09:28 +0200 | [diff] [blame] | 881 | if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map)) |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 882 | return false; |
| 883 | |
| 884 | if (irq->dest_mode == APIC_DEST_PHYSICAL) { |
Radim Krčmář | 0ca52e7 | 2016-07-12 22:09:20 +0200 | [diff] [blame] | 885 | if (irq->dest_id > map->max_apic_id) { |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 886 | *bitmap = 0; |
| 887 | } else { |
Paolo Bonzini | 1d487e9 | 2019-04-11 11:16:47 +0200 | [diff] [blame] | 888 | u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1); |
| 889 | *dst = &map->phys_map[dest_id]; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 890 | *bitmap = 1; |
| 891 | } |
| 892 | return true; |
| 893 | } |
| 894 | |
Radim Krčmář | e45115b | 2016-07-12 22:09:19 +0200 | [diff] [blame] | 895 | *bitmap = 0; |
| 896 | if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst, |
| 897 | (u16 *)bitmap)) |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 898 | return false; |
| 899 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 900 | if (!kvm_lowest_prio_delivery(irq)) |
| 901 | return true; |
| 902 | |
| 903 | if (!kvm_vector_hashing_enabled()) { |
| 904 | lowest = -1; |
| 905 | for_each_set_bit(i, bitmap, 16) { |
| 906 | if (!(*dst)[i]) |
| 907 | continue; |
| 908 | if (lowest < 0) |
| 909 | lowest = i; |
| 910 | else if (kvm_apic_compare_prio((*dst)[i]->vcpu, |
| 911 | (*dst)[lowest]->vcpu) < 0) |
| 912 | lowest = i; |
| 913 | } |
| 914 | } else { |
| 915 | if (!*bitmap) |
| 916 | return true; |
| 917 | |
| 918 | lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap), |
| 919 | bitmap, 16); |
| 920 | |
| 921 | if (!(*dst)[lowest]) { |
| 922 | kvm_apic_disabled_lapic_found(kvm); |
| 923 | *bitmap = 0; |
| 924 | return true; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | *bitmap = (lowest >= 0) ? 1 << lowest : 0; |
| 929 | |
| 930 | return true; |
| 931 | } |
| 932 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 933 | 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] | 934 | struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 935 | { |
| 936 | struct kvm_apic_map *map; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 937 | unsigned long bitmap; |
| 938 | struct kvm_lapic **dst = NULL; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 939 | int i; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 940 | bool ret; |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 941 | |
| 942 | *r = -1; |
| 943 | |
| 944 | if (irq->shorthand == APIC_DEST_SELF) { |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 945 | *r = kvm_apic_set_irq(src->vcpu, irq, dest_map); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 946 | return true; |
| 947 | } |
| 948 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 949 | rcu_read_lock(); |
| 950 | map = rcu_dereference(kvm->arch.apic_map); |
| 951 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 952 | ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap); |
Paolo Bonzini | 0624fca | 2018-10-01 16:07:18 +0200 | [diff] [blame] | 953 | if (ret) { |
| 954 | *r = 0; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 955 | for_each_set_bit(i, &bitmap, 16) { |
| 956 | if (!dst[i]) |
| 957 | continue; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 958 | *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map); |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 959 | } |
Paolo Bonzini | 0624fca | 2018-10-01 16:07:18 +0200 | [diff] [blame] | 960 | } |
Radim Krčmář | 3548a25 | 2015-02-12 19:41:33 +0100 | [diff] [blame] | 961 | |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 962 | rcu_read_unlock(); |
| 963 | return ret; |
| 964 | } |
| 965 | |
Feng Wu | 6228a0d | 2016-01-25 16:53:34 +0800 | [diff] [blame] | 966 | /* |
| 967 | * This routine tries to handler interrupts in posted mode, here is how |
| 968 | * it deals with different cases: |
| 969 | * - For single-destination interrupts, handle it in posted mode |
| 970 | * - Else if vector hashing is enabled and it is a lowest-priority |
| 971 | * interrupt, handle it in posted mode and use the following mechanism |
| 972 | * to find the destinaiton vCPU. |
| 973 | * 1. For lowest-priority interrupts, store all the possible |
| 974 | * destination vCPUs in an array. |
| 975 | * 2. Use "guest vector % max number of destination vCPUs" to find |
| 976 | * the right destination vCPU in the array for the lowest-priority |
| 977 | * interrupt. |
| 978 | * - Otherwise, use remapped mode to inject the interrupt. |
| 979 | */ |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 980 | bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, |
| 981 | struct kvm_vcpu **dest_vcpu) |
| 982 | { |
| 983 | struct kvm_apic_map *map; |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 984 | unsigned long bitmap; |
| 985 | struct kvm_lapic **dst = NULL; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 986 | bool ret = false; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 987 | |
| 988 | if (irq->shorthand) |
| 989 | return false; |
| 990 | |
| 991 | rcu_read_lock(); |
| 992 | map = rcu_dereference(kvm->arch.apic_map); |
| 993 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 994 | if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) && |
| 995 | hweight16(bitmap) == 1) { |
| 996 | unsigned long i = find_first_bit(&bitmap, 16); |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 997 | |
Radim Krčmář | 64aa47b | 2016-07-12 22:09:18 +0200 | [diff] [blame] | 998 | if (dst[i]) { |
| 999 | *dest_vcpu = dst[i]->vcpu; |
| 1000 | ret = true; |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 1001 | } |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 1004 | rcu_read_unlock(); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1008 | /* |
| 1009 | * Add a pending IRQ into lapic. |
| 1010 | * Return 1 if successfully added and 0 if discarded. |
| 1011 | */ |
| 1012 | static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 1013 | int vector, int level, int trig_mode, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 1014 | struct dest_map *dest_map) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1015 | { |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 1016 | int result = 0; |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1017 | struct kvm_vcpu *vcpu = apic->vcpu; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1018 | |
Paolo Bonzini | a183b63 | 2014-09-11 11:51:02 +0200 | [diff] [blame] | 1019 | trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode, |
| 1020 | trig_mode, vector); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1021 | switch (delivery_mode) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1022 | case APIC_DM_LOWEST: |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1023 | vcpu->arch.apic_arb_prio++; |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 1024 | /* fall through */ |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1025 | case APIC_DM_FIXED: |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 1026 | if (unlikely(trig_mode && !level)) |
| 1027 | break; |
| 1028 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1029 | /* FIXME add logic for vcpu on reset */ |
| 1030 | if (unlikely(!apic_enabled(apic))) |
| 1031 | break; |
| 1032 | |
Jan Kiszka | 11f5cc0 | 2013-07-25 09:58:45 +0200 | [diff] [blame] | 1033 | result = 1; |
| 1034 | |
Joerg Roedel | 9daa500 | 2016-02-29 16:04:44 +0100 | [diff] [blame] | 1035 | if (dest_map) { |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 1036 | __set_bit(vcpu->vcpu_id, dest_map->map); |
Joerg Roedel | 9daa500 | 2016-02-29 16:04:44 +0100 | [diff] [blame] | 1037 | dest_map->vectors[vcpu->vcpu_id] = vector; |
| 1038 | } |
Avi Kivity | a5d36f8 | 2009-12-29 12:42:16 +0200 | [diff] [blame] | 1039 | |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 1040 | if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) { |
| 1041 | if (trig_mode) |
Wei Yang | ee171d2 | 2019-03-31 19:17:22 -0700 | [diff] [blame] | 1042 | kvm_lapic_set_vector(vector, |
| 1043 | apic->regs + APIC_TMR); |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 1044 | else |
Wei Yang | ee171d2 | 2019-03-31 19:17:22 -0700 | [diff] [blame] | 1045 | kvm_lapic_clear_vector(vector, |
| 1046 | apic->regs + APIC_TMR); |
Paolo Bonzini | bdaffe1 | 2015-07-29 15:03:06 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 1049 | if (vcpu->arch.apicv_active) |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 1050 | kvm_x86_ops->deliver_posted_interrupt(vcpu, vector); |
Jan Kiszka | 11f5cc0 | 2013-07-25 09:58:45 +0200 | [diff] [blame] | 1051 | else { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1052 | kvm_lapic_set_irr(vector, apic); |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 1053 | |
| 1054 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 1055 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1056 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1057 | break; |
| 1058 | |
| 1059 | case APIC_DM_REMRD: |
Raghavendra K T | 24d2166 | 2013-08-26 14:18:35 +0530 | [diff] [blame] | 1060 | result = 1; |
| 1061 | vcpu->arch.pv.pv_unhalted = 1; |
| 1062 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 1063 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1064 | break; |
| 1065 | |
| 1066 | case APIC_DM_SMI: |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 1067 | result = 1; |
| 1068 | kvm_make_request(KVM_REQ_SMI, vcpu); |
| 1069 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1070 | break; |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 1071 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1072 | case APIC_DM_NMI: |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 1073 | result = 1; |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 1074 | kvm_inject_nmi(vcpu); |
Jan Kiszka | 26df99c | 2008-09-26 09:30:54 +0200 | [diff] [blame] | 1075 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | case APIC_DM_INIT: |
Julian Stecklina | a52315e | 2012-01-16 14:02:20 +0100 | [diff] [blame] | 1079 | if (!trig_mode || level) { |
Gleb Natapov | 6da7e3f | 2009-03-05 16:34:44 +0200 | [diff] [blame] | 1080 | result = 1; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 1081 | /* assumes that there are only KVM_APIC_INIT/SIPI */ |
| 1082 | apic->pending_events = (1UL << KVM_APIC_INIT); |
| 1083 | /* make sure pending_events is visible before sending |
| 1084 | * the request */ |
| 1085 | smp_wmb(); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 1086 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1087 | kvm_vcpu_kick(vcpu); |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1088 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1089 | break; |
| 1090 | |
| 1091 | case APIC_DM_STARTUP: |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 1092 | result = 1; |
| 1093 | apic->sipi_vector = vector; |
| 1094 | /* make sure sipi_vector is visible for the receiver */ |
| 1095 | smp_wmb(); |
| 1096 | set_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 1097 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 1098 | kvm_vcpu_kick(vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1099 | break; |
| 1100 | |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1101 | case APIC_DM_EXTINT: |
| 1102 | /* |
| 1103 | * Should only be called by kvm_apic_local_deliver() with LVT0, |
| 1104 | * before NMI watchdog was enabled. Already handled by |
| 1105 | * kvm_apic_accept_pic_intr(). |
| 1106 | */ |
| 1107 | break; |
| 1108 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1109 | default: |
| 1110 | printk(KERN_ERR "TODO: unsupported delivery mode %x\n", |
| 1111 | delivery_mode); |
| 1112 | break; |
| 1113 | } |
| 1114 | return result; |
| 1115 | } |
| 1116 | |
Nitesh Narayan Lal | 7ee30bc | 2019-11-07 07:53:43 -0500 | [diff] [blame] | 1117 | /* |
| 1118 | * This routine identifies the destination vcpus mask meant to receive the |
| 1119 | * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find |
| 1120 | * out the destination vcpus array and set the bitmap or it traverses to |
| 1121 | * each available vcpu to identify the same. |
| 1122 | */ |
| 1123 | void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq, |
| 1124 | unsigned long *vcpu_bitmap) |
| 1125 | { |
| 1126 | struct kvm_lapic **dest_vcpu = NULL; |
| 1127 | struct kvm_lapic *src = NULL; |
| 1128 | struct kvm_apic_map *map; |
| 1129 | struct kvm_vcpu *vcpu; |
| 1130 | unsigned long bitmap; |
| 1131 | int i, vcpu_idx; |
| 1132 | bool ret; |
| 1133 | |
| 1134 | rcu_read_lock(); |
| 1135 | map = rcu_dereference(kvm->arch.apic_map); |
| 1136 | |
| 1137 | ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu, |
| 1138 | &bitmap); |
| 1139 | if (ret) { |
| 1140 | for_each_set_bit(i, &bitmap, 16) { |
| 1141 | if (!dest_vcpu[i]) |
| 1142 | continue; |
| 1143 | vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx; |
| 1144 | __set_bit(vcpu_idx, vcpu_bitmap); |
| 1145 | } |
| 1146 | } else { |
| 1147 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1148 | if (!kvm_apic_present(vcpu)) |
| 1149 | continue; |
| 1150 | if (!kvm_apic_match_dest(vcpu, NULL, |
Peter Xu | b4b2963 | 2019-12-04 20:07:16 +0100 | [diff] [blame] | 1151 | irq->shorthand, |
Nitesh Narayan Lal | 7ee30bc | 2019-11-07 07:53:43 -0500 | [diff] [blame] | 1152 | irq->dest_id, |
| 1153 | irq->dest_mode)) |
| 1154 | continue; |
| 1155 | __set_bit(i, vcpu_bitmap); |
| 1156 | } |
| 1157 | } |
| 1158 | rcu_read_unlock(); |
| 1159 | } |
| 1160 | |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1161 | 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] | 1162 | { |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1163 | return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio; |
Zhang Xiantao | 8be5453 | 2007-12-02 22:35:57 +0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 1166 | static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector) |
| 1167 | { |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 1168 | return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors); |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1171 | static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector) |
| 1172 | { |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 1173 | int trigger_mode; |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 1174 | |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 1175 | /* Eoi the ioapic only if the ioapic doesn't own the vector. */ |
| 1176 | if (!kvm_ioapic_handles_vector(apic, vector)) |
| 1177 | return; |
| 1178 | |
| 1179 | /* Request a KVM exit to inform the userspace IOAPIC. */ |
| 1180 | if (irqchip_split(apic->vcpu->kvm)) { |
| 1181 | apic->vcpu->arch.pending_ioapic_eoi = vector; |
| 1182 | kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu); |
| 1183 | return; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1184 | } |
Steve Rutherford | 7543a63 | 2015-07-29 23:21:41 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (apic_test_vector(vector, apic->regs + APIC_TMR)) |
| 1187 | trigger_mode = IOAPIC_LEVEL_TRIG; |
| 1188 | else |
| 1189 | trigger_mode = IOAPIC_EDGE_TRIG; |
| 1190 | |
| 1191 | kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1194 | static int apic_set_eoi(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1195 | { |
| 1196 | int vector = apic_find_highest_isr(apic); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1197 | |
| 1198 | trace_kvm_eoi(apic, vector); |
| 1199 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1200 | /* |
| 1201 | * Not every write EOI will has corresponding ISR, |
| 1202 | * one example is when Kernel check timer on setup_IO_APIC |
| 1203 | */ |
| 1204 | if (vector == -1) |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1205 | return vector; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1206 | |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 1207 | apic_clear_isr(vector, apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1208 | apic_update_ppr(apic); |
| 1209 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1210 | if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap)) |
| 1211 | kvm_hv_synic_send_eoi(apic->vcpu, vector); |
| 1212 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1213 | kvm_ioapic_send_eoi(apic, vector); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 1214 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1215 | return vector; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1216 | } |
| 1217 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1218 | /* |
| 1219 | * this interface assumes a trap-like exit, which has already finished |
| 1220 | * desired side effect including vISR and vPPR update. |
| 1221 | */ |
| 1222 | void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector) |
| 1223 | { |
| 1224 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1225 | |
| 1226 | trace_kvm_eoi(apic, vector); |
| 1227 | |
| 1228 | kvm_ioapic_send_eoi(apic, vector); |
| 1229 | kvm_make_request(KVM_REQ_EVENT, apic->vcpu); |
| 1230 | } |
| 1231 | EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated); |
| 1232 | |
Wanpeng Li | 2b0911d | 2019-09-05 14:26:27 +0800 | [diff] [blame] | 1233 | static void apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1234 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1235 | struct kvm_lapic_irq irq; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1236 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1237 | irq.vector = icr_low & APIC_VECTOR_MASK; |
| 1238 | irq.delivery_mode = icr_low & APIC_MODE_MASK; |
| 1239 | irq.dest_mode = icr_low & APIC_DEST_MASK; |
Paolo Bonzini | b7cb223 | 2015-04-21 14:57:05 +0200 | [diff] [blame] | 1240 | irq.level = (icr_low & APIC_INT_ASSERT) != 0; |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1241 | irq.trig_mode = icr_low & APIC_INT_LEVELTRIG; |
| 1242 | irq.shorthand = icr_low & APIC_SHORT_MASK; |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 1243 | irq.msi_redir_hint = false; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1244 | if (apic_x2apic_mode(apic)) |
| 1245 | irq.dest_id = icr_high; |
| 1246 | else |
| 1247 | irq.dest_id = GET_APIC_DEST_FIELD(icr_high); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1248 | |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 1249 | trace_kvm_apic_ipi(icr_low, irq.dest_id); |
| 1250 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 1251 | kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static u32 apic_get_tmcct(struct kvm_lapic *apic) |
| 1255 | { |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1256 | ktime_t remaining, now; |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 1257 | s64 ns; |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1258 | u32 tmcct; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1259 | |
| 1260 | ASSERT(apic != NULL); |
| 1261 | |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1262 | /* if initial count is 0, current count should also be 0 */ |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1263 | if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 || |
Andy Honig | b963a22 | 2013-11-19 14:12:18 -0800 | [diff] [blame] | 1264 | apic->lapic_timer.period == 0) |
Kevin Pedretti | 9da8f4e | 2007-10-21 08:55:50 +0200 | [diff] [blame] | 1265 | return 0; |
| 1266 | |
Paolo Bonzini | 5587859 | 2016-10-25 15:23:49 +0200 | [diff] [blame] | 1267 | now = ktime_get(); |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1268 | remaining = ktime_sub(apic->lapic_timer.target_expiration, now); |
Marcelo Tosatti | b682b81 | 2009-02-10 20:41:41 -0200 | [diff] [blame] | 1269 | if (ktime_to_ns(remaining) < 0) |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 1270 | remaining = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1271 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1272 | ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period); |
| 1273 | tmcct = div64_u64(ns, |
| 1274 | (APIC_BUS_CYCLE_NS * apic->divide_count)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1275 | |
| 1276 | return tmcct; |
| 1277 | } |
| 1278 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1279 | static void __report_tpr_access(struct kvm_lapic *apic, bool write) |
| 1280 | { |
| 1281 | struct kvm_vcpu *vcpu = apic->vcpu; |
| 1282 | struct kvm_run *run = vcpu->run; |
| 1283 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1284 | kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1285 | run->tpr_access.rip = kvm_rip_read(vcpu); |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1286 | run->tpr_access.is_write = write; |
| 1287 | } |
| 1288 | |
| 1289 | static inline void report_tpr_access(struct kvm_lapic *apic, bool write) |
| 1290 | { |
| 1291 | if (apic->vcpu->arch.tpr_access_reporting) |
| 1292 | __report_tpr_access(apic, write); |
| 1293 | } |
| 1294 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1295 | static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset) |
| 1296 | { |
| 1297 | u32 val = 0; |
| 1298 | |
| 1299 | if (offset >= LAPIC_MMIO_LENGTH) |
| 1300 | return 0; |
| 1301 | |
| 1302 | switch (offset) { |
| 1303 | case APIC_ARBPRI: |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1304 | break; |
| 1305 | |
| 1306 | case APIC_TMCCT: /* Timer CCR */ |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1307 | if (apic_lvtt_tscdeadline(apic)) |
| 1308 | return 0; |
| 1309 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1310 | val = apic_get_tmcct(apic); |
| 1311 | break; |
Avi Kivity | 4a4541a | 2012-07-22 17:41:00 +0300 | [diff] [blame] | 1312 | case APIC_PROCPRI: |
| 1313 | apic_update_ppr(apic); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1314 | val = kvm_lapic_get_reg(apic, offset); |
Avi Kivity | 4a4541a | 2012-07-22 17:41:00 +0300 | [diff] [blame] | 1315 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1316 | case APIC_TASKPRI: |
| 1317 | report_tpr_access(apic, false); |
| 1318 | /* fall thru */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1319 | default: |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1320 | val = kvm_lapic_get_reg(apic, offset); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1321 | break; |
| 1322 | } |
| 1323 | |
| 1324 | return val; |
| 1325 | } |
| 1326 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 1327 | static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev) |
| 1328 | { |
| 1329 | return container_of(dev, struct kvm_lapic, dev); |
| 1330 | } |
| 1331 | |
Paolo Bonzini | 01402cf | 2019-07-05 14:57:58 +0200 | [diff] [blame] | 1332 | #define APIC_REG_MASK(reg) (1ull << ((reg) >> 4)) |
| 1333 | #define APIC_REGS_MASK(first, count) \ |
| 1334 | (APIC_REG_MASK(first) * ((1ull << (count)) - 1)) |
| 1335 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1336 | 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] | 1337 | void *data) |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 1338 | { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1339 | unsigned char alignment = offset & 0xf; |
| 1340 | u32 result; |
Guo Chao | d5b0b5b | 2012-06-28 15:22:57 +0800 | [diff] [blame] | 1341 | /* this bitmask has a bit cleared for each reserved register */ |
Paolo Bonzini | 01402cf | 2019-07-05 14:57:58 +0200 | [diff] [blame] | 1342 | u64 valid_reg_mask = |
| 1343 | APIC_REG_MASK(APIC_ID) | |
| 1344 | APIC_REG_MASK(APIC_LVR) | |
| 1345 | APIC_REG_MASK(APIC_TASKPRI) | |
| 1346 | APIC_REG_MASK(APIC_PROCPRI) | |
| 1347 | APIC_REG_MASK(APIC_LDR) | |
| 1348 | APIC_REG_MASK(APIC_DFR) | |
| 1349 | APIC_REG_MASK(APIC_SPIV) | |
| 1350 | APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) | |
| 1351 | APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) | |
| 1352 | APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) | |
| 1353 | APIC_REG_MASK(APIC_ESR) | |
| 1354 | APIC_REG_MASK(APIC_ICR) | |
| 1355 | APIC_REG_MASK(APIC_ICR2) | |
| 1356 | APIC_REG_MASK(APIC_LVTT) | |
| 1357 | APIC_REG_MASK(APIC_LVTTHMR) | |
| 1358 | APIC_REG_MASK(APIC_LVTPC) | |
| 1359 | APIC_REG_MASK(APIC_LVT0) | |
| 1360 | APIC_REG_MASK(APIC_LVT1) | |
| 1361 | APIC_REG_MASK(APIC_LVTERR) | |
| 1362 | APIC_REG_MASK(APIC_TMICT) | |
| 1363 | APIC_REG_MASK(APIC_TMCCT) | |
| 1364 | APIC_REG_MASK(APIC_TDCR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1365 | |
Paolo Bonzini | 01402cf | 2019-07-05 14:57:58 +0200 | [diff] [blame] | 1366 | /* ARBPRI is not valid on x2APIC */ |
| 1367 | if (!apic_x2apic_mode(apic)) |
| 1368 | valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1369 | |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 1370 | if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset))) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1371 | return 1; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1372 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1373 | result = __apic_read(apic, offset & ~0xf); |
| 1374 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 1375 | trace_kvm_apic_read(offset, result); |
| 1376 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1377 | switch (len) { |
| 1378 | case 1: |
| 1379 | case 2: |
| 1380 | case 4: |
| 1381 | memcpy(data, (char *)&result + alignment, len); |
| 1382 | break; |
| 1383 | default: |
| 1384 | printk(KERN_ERR "Local APIC read with len = %x, " |
| 1385 | "should be 1,2, or 4 instead\n", len); |
| 1386 | break; |
| 1387 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 1388 | return 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1389 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1390 | EXPORT_SYMBOL_GPL(kvm_lapic_reg_read); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1391 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1392 | static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr) |
| 1393 | { |
Vitaly Kuznetsov | d176620 | 2018-08-02 17:08:16 +0200 | [diff] [blame] | 1394 | return addr >= apic->base_address && |
| 1395 | addr < apic->base_address + LAPIC_MMIO_LENGTH; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1396 | } |
| 1397 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 1398 | 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] | 1399 | gpa_t address, int len, void *data) |
| 1400 | { |
| 1401 | struct kvm_lapic *apic = to_lapic(this); |
| 1402 | u32 offset = address - apic->base_address; |
| 1403 | |
| 1404 | if (!apic_mmio_in_range(apic, address)) |
| 1405 | return -EOPNOTSUPP; |
| 1406 | |
Vitaly Kuznetsov | d176620 | 2018-08-02 17:08:16 +0200 | [diff] [blame] | 1407 | if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) { |
| 1408 | if (!kvm_check_has_quirk(vcpu->kvm, |
| 1409 | KVM_X86_QUIRK_LAPIC_MMIO_HOLE)) |
| 1410 | return -EOPNOTSUPP; |
| 1411 | |
| 1412 | memset(data, 0xff, len); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1416 | kvm_lapic_reg_read(apic, offset, len, data); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1417 | |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1421 | static void update_divide_count(struct kvm_lapic *apic) |
| 1422 | { |
| 1423 | u32 tmp1, tmp2, tdcr; |
| 1424 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1425 | tdcr = kvm_lapic_get_reg(apic, APIC_TDCR); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1426 | tmp1 = tdcr & 0xf; |
| 1427 | tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1428 | apic->divide_count = 0x1 << (tmp2 & 0x7); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1429 | } |
| 1430 | |
Wanpeng Li | ccbfa1d | 2017-10-05 18:54:24 -0700 | [diff] [blame] | 1431 | static void limit_periodic_timer_frequency(struct kvm_lapic *apic) |
| 1432 | { |
| 1433 | /* |
| 1434 | * Do not allow the guest to program periodic timers with small |
| 1435 | * interval, since the hrtimers are not throttled by the host |
| 1436 | * scheduler. |
| 1437 | */ |
Wanpeng Li | dedf9c5 | 2017-10-05 18:54:25 -0700 | [diff] [blame] | 1438 | if (apic_lvtt_period(apic) && apic->lapic_timer.period) { |
Wanpeng Li | ccbfa1d | 2017-10-05 18:54:24 -0700 | [diff] [blame] | 1439 | s64 min_period = min_timer_period_us * 1000LL; |
| 1440 | |
| 1441 | if (apic->lapic_timer.period < min_period) { |
| 1442 | pr_info_ratelimited( |
| 1443 | "kvm: vcpu %i: requested %lld ns " |
| 1444 | "lapic timer period limited to %lld ns\n", |
| 1445 | apic->vcpu->vcpu_id, |
| 1446 | apic->lapic_timer.period, min_period); |
| 1447 | apic->lapic_timer.period = min_period; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1452 | static void apic_update_lvtt(struct kvm_lapic *apic) |
| 1453 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1454 | u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) & |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1455 | apic->lapic_timer.timer_mode_mask; |
| 1456 | |
| 1457 | if (apic->lapic_timer.timer_mode != timer_mode) { |
Wanpeng Li | c69518c | 2017-10-05 03:53:51 -0700 | [diff] [blame] | 1458 | if (apic_lvtt_tscdeadline(apic) != (timer_mode == |
Wanpeng Li | dedf9c5 | 2017-10-05 18:54:25 -0700 | [diff] [blame] | 1459 | APIC_LVT_TIMER_TSCDEADLINE)) { |
Wanpeng Li | dedf9c5 | 2017-10-05 18:54:25 -0700 | [diff] [blame] | 1460 | hrtimer_cancel(&apic->lapic_timer.timer); |
Radim Krčmář | 4427593 | 2017-10-06 19:25:55 +0200 | [diff] [blame] | 1461 | kvm_lapic_set_reg(apic, APIC_TMICT, 0); |
| 1462 | apic->lapic_timer.period = 0; |
| 1463 | apic->lapic_timer.tscdeadline = 0; |
Wanpeng Li | dedf9c5 | 2017-10-05 18:54:25 -0700 | [diff] [blame] | 1464 | } |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1465 | apic->lapic_timer.timer_mode = timer_mode; |
Wanpeng Li | dedf9c5 | 2017-10-05 18:54:25 -0700 | [diff] [blame] | 1466 | limit_periodic_timer_frequency(apic); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1470 | /* |
| 1471 | * On APICv, this test will cause a busy wait |
| 1472 | * during a higher-priority task. |
| 1473 | */ |
| 1474 | |
| 1475 | static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu) |
| 1476 | { |
| 1477 | struct kvm_lapic *apic = vcpu->arch.apic; |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1478 | u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT); |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1479 | |
| 1480 | if (kvm_apic_hw_enabled(apic)) { |
| 1481 | int vec = reg & APIC_VECTOR_MASK; |
Marcelo Tosatti | f933986 | 2015-02-02 15:26:08 -0200 | [diff] [blame] | 1482 | void *bitmap = apic->regs + APIC_ISR; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1483 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 1484 | if (vcpu->arch.apicv_active) |
Marcelo Tosatti | f933986 | 2015-02-02 15:26:08 -0200 | [diff] [blame] | 1485 | bitmap = apic->regs + APIC_IRR; |
| 1486 | |
| 1487 | if (apic_test_vector(vec, bitmap)) |
| 1488 | return true; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1489 | } |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Sean Christopherson | b6aa57c | 2019-04-17 10:15:34 -0700 | [diff] [blame] | 1493 | static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles) |
| 1494 | { |
| 1495 | u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns; |
| 1496 | |
| 1497 | /* |
| 1498 | * If the guest TSC is running at a different ratio than the host, then |
| 1499 | * convert the delay to nanoseconds to achieve an accurate delay. Note |
| 1500 | * that __delay() uses delay_tsc whenever the hardware has TSC, thus |
| 1501 | * always for VMX enabled hardware. |
| 1502 | */ |
| 1503 | if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) { |
| 1504 | __delay(min(guest_cycles, |
| 1505 | nsec_to_cycles(vcpu, timer_advance_ns))); |
| 1506 | } else { |
| 1507 | u64 delay_ns = guest_cycles * 1000000ULL; |
| 1508 | do_div(delay_ns, vcpu->arch.virtual_tsc_khz); |
| 1509 | ndelay(min_t(u32, delay_ns, timer_advance_ns)); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1513 | static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu, |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 1514 | s64 advance_expire_delta) |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1515 | { |
| 1516 | struct kvm_lapic *apic = vcpu->arch.apic; |
Sean Christopherson | 39497d7 | 2019-04-17 10:15:32 -0700 | [diff] [blame] | 1517 | u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1518 | u64 ns; |
| 1519 | |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 1520 | /* Do not adjust for tiny fluctuations or large random spikes. */ |
| 1521 | if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX || |
| 1522 | abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN) |
| 1523 | return; |
| 1524 | |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1525 | /* too early */ |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 1526 | if (advance_expire_delta < 0) { |
| 1527 | ns = -advance_expire_delta * 1000000ULL; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1528 | do_div(ns, vcpu->arch.virtual_tsc_khz); |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 1529 | timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1530 | } else { |
| 1531 | /* too late */ |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 1532 | ns = advance_expire_delta * 1000000ULL; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1533 | do_div(ns, vcpu->arch.virtual_tsc_khz); |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 1534 | timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Wanpeng Li | a0f0037 | 2019-09-26 08:54:03 +0800 | [diff] [blame] | 1537 | if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX)) |
| 1538 | timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT; |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1539 | apic->lapic_timer.timer_advance_ns = timer_advance_ns; |
| 1540 | } |
| 1541 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 1542 | static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) |
Wanpeng Li | 84ea3ac | 2019-05-20 16:18:05 +0800 | [diff] [blame] | 1543 | { |
| 1544 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1545 | u64 guest_tsc, tsc_deadline; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1546 | |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1547 | if (apic->lapic_timer.expired_tscdeadline == 0) |
| 1548 | return; |
| 1549 | |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1550 | tsc_deadline = apic->lapic_timer.expired_tscdeadline; |
| 1551 | apic->lapic_timer.expired_tscdeadline = 0; |
Haozhong Zhang | 4ba7653 | 2015-10-20 15:39:07 +0800 | [diff] [blame] | 1552 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 1553 | apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline; |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1554 | |
Marcelo Tosatti | d0659d9 | 2014-12-16 09:08:15 -0500 | [diff] [blame] | 1555 | if (guest_tsc < tsc_deadline) |
Sean Christopherson | b6aa57c | 2019-04-17 10:15:34 -0700 | [diff] [blame] | 1556 | __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); |
Wanpeng Li | 3b8a5df | 2018-10-09 09:02:08 +0800 | [diff] [blame] | 1557 | |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 1558 | if (lapic_timer_advance_dynamic) |
Wanpeng Li | ec0671d | 2019-05-20 16:18:08 +0800 | [diff] [blame] | 1559 | adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta); |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1560 | } |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 1561 | |
| 1562 | void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) |
| 1563 | { |
| 1564 | if (lapic_timer_int_injected(vcpu)) |
| 1565 | __kvm_wait_lapic_expire(vcpu); |
| 1566 | } |
Wanpeng Li | b6c4bc6 | 2019-05-20 16:18:09 +0800 | [diff] [blame] | 1567 | EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire); |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 1568 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 1569 | static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic) |
| 1570 | { |
| 1571 | struct kvm_timer *ktimer = &apic->lapic_timer; |
| 1572 | |
| 1573 | kvm_apic_local_deliver(apic, APIC_LVTT); |
| 1574 | if (apic_lvtt_tscdeadline(apic)) |
| 1575 | ktimer->tscdeadline = 0; |
| 1576 | if (apic_lvtt_oneshot(apic)) { |
| 1577 | ktimer->tscdeadline = 0; |
| 1578 | ktimer->target_expiration = 0; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | static void apic_timer_expired(struct kvm_lapic *apic) |
| 1583 | { |
| 1584 | struct kvm_vcpu *vcpu = apic->vcpu; |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 1585 | struct kvm_timer *ktimer = &apic->lapic_timer; |
| 1586 | |
| 1587 | if (atomic_read(&apic->lapic_timer.pending)) |
| 1588 | return; |
| 1589 | |
| 1590 | if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use) |
| 1591 | ktimer->expired_tscdeadline = ktimer->tscdeadline; |
| 1592 | |
| 1593 | if (kvm_use_posted_timer_interrupt(apic->vcpu)) { |
| 1594 | if (apic->lapic_timer.timer_advance_ns) |
| 1595 | __kvm_wait_lapic_expire(vcpu); |
| 1596 | kvm_apic_inject_pending_timer_irqs(apic); |
| 1597 | return; |
| 1598 | } |
| 1599 | |
| 1600 | atomic_inc(&apic->lapic_timer.pending); |
| 1601 | kvm_set_pending_timer(vcpu); |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1604 | static void start_sw_tscdeadline(struct kvm_lapic *apic) |
| 1605 | { |
Sean Christopherson | 39497d7 | 2019-04-17 10:15:32 -0700 | [diff] [blame] | 1606 | struct kvm_timer *ktimer = &apic->lapic_timer; |
| 1607 | u64 guest_tsc, tscdeadline = ktimer->tscdeadline; |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1608 | u64 ns = 0; |
| 1609 | ktime_t expire; |
| 1610 | struct kvm_vcpu *vcpu = apic->vcpu; |
| 1611 | unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz; |
| 1612 | unsigned long flags; |
| 1613 | ktime_t now; |
| 1614 | |
| 1615 | if (unlikely(!tscdeadline || !this_tsc_khz)) |
| 1616 | return; |
| 1617 | |
| 1618 | local_irq_save(flags); |
| 1619 | |
Paolo Bonzini | 5587859 | 2016-10-25 15:23:49 +0200 | [diff] [blame] | 1620 | now = ktime_get(); |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1621 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
Liran Alon | c09d65d | 2019-04-16 20:36:34 +0300 | [diff] [blame] | 1622 | |
| 1623 | ns = (tscdeadline - guest_tsc) * 1000000ULL; |
| 1624 | do_div(ns, this_tsc_khz); |
| 1625 | |
| 1626 | if (likely(tscdeadline > guest_tsc) && |
Sean Christopherson | 39497d7 | 2019-04-17 10:15:32 -0700 | [diff] [blame] | 1627 | likely(ns > apic->lapic_timer.timer_advance_ns)) { |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1628 | expire = ktime_add_ns(now, ns); |
Sean Christopherson | 39497d7 | 2019-04-17 10:15:32 -0700 | [diff] [blame] | 1629 | expire = ktime_sub_ns(expire, ktimer->timer_advance_ns); |
Sebastian Andrzej Siewior | 2c0d278 | 2019-07-26 20:30:55 +0200 | [diff] [blame] | 1630 | hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD); |
Yunhong Jiang | 53f9eed | 2016-06-13 14:20:00 -0700 | [diff] [blame] | 1631 | } else |
| 1632 | apic_timer_expired(apic); |
| 1633 | |
| 1634 | local_irq_restore(flags); |
| 1635 | } |
| 1636 | |
Wanpeng Li | c301b90 | 2017-10-06 07:38:32 -0700 | [diff] [blame] | 1637 | static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor) |
| 1638 | { |
| 1639 | ktime_t now, remaining; |
| 1640 | u64 ns_remaining_old, ns_remaining_new; |
| 1641 | |
| 1642 | apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT) |
| 1643 | * APIC_BUS_CYCLE_NS * apic->divide_count; |
| 1644 | limit_periodic_timer_frequency(apic); |
| 1645 | |
| 1646 | now = ktime_get(); |
| 1647 | remaining = ktime_sub(apic->lapic_timer.target_expiration, now); |
| 1648 | if (ktime_to_ns(remaining) < 0) |
| 1649 | remaining = 0; |
| 1650 | |
| 1651 | ns_remaining_old = ktime_to_ns(remaining); |
| 1652 | ns_remaining_new = mul_u64_u32_div(ns_remaining_old, |
| 1653 | apic->divide_count, old_divisor); |
| 1654 | |
| 1655 | apic->lapic_timer.tscdeadline += |
| 1656 | nsec_to_cycles(apic->vcpu, ns_remaining_new) - |
| 1657 | nsec_to_cycles(apic->vcpu, ns_remaining_old); |
| 1658 | apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new); |
| 1659 | } |
| 1660 | |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1661 | static bool set_target_expiration(struct kvm_lapic *apic) |
| 1662 | { |
| 1663 | ktime_t now; |
| 1664 | u64 tscl = rdtsc(); |
| 1665 | |
Paolo Bonzini | 5587859 | 2016-10-25 15:23:49 +0200 | [diff] [blame] | 1666 | now = ktime_get(); |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1667 | apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT) |
| 1668 | * APIC_BUS_CYCLE_NS * apic->divide_count; |
| 1669 | |
Radim Krčmář | 5d74a69 | 2017-10-06 19:25:54 +0200 | [diff] [blame] | 1670 | if (!apic->lapic_timer.period) { |
| 1671 | apic->lapic_timer.tscdeadline = 0; |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1672 | return false; |
Wanpeng Li | 7d7f7da | 2016-10-24 18:23:09 +0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Wanpeng Li | ccbfa1d | 2017-10-05 18:54:24 -0700 | [diff] [blame] | 1675 | limit_periodic_timer_frequency(apic); |
Wanpeng Li | 7d7f7da | 2016-10-24 18:23:09 +0800 | [diff] [blame] | 1676 | |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1677 | apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) + |
| 1678 | nsec_to_cycles(apic->vcpu, apic->lapic_timer.period); |
| 1679 | apic->lapic_timer.target_expiration = ktime_add_ns(now, apic->lapic_timer.period); |
| 1680 | |
| 1681 | return true; |
| 1682 | } |
| 1683 | |
| 1684 | static void advance_periodic_target_expiration(struct kvm_lapic *apic) |
| 1685 | { |
David Vrabel | d8f2f49 | 2018-05-18 16:55:46 +0100 | [diff] [blame] | 1686 | ktime_t now = ktime_get(); |
| 1687 | u64 tscl = rdtsc(); |
| 1688 | ktime_t delta; |
| 1689 | |
| 1690 | /* |
| 1691 | * Synchronize both deadlines to the same time source or |
| 1692 | * differences in the periods (caused by differences in the |
| 1693 | * underlying clocks or numerical approximation errors) will |
| 1694 | * cause the two to drift apart over time as the errors |
| 1695 | * accumulate. |
| 1696 | */ |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1697 | apic->lapic_timer.target_expiration = |
| 1698 | ktime_add_ns(apic->lapic_timer.target_expiration, |
| 1699 | apic->lapic_timer.period); |
David Vrabel | d8f2f49 | 2018-05-18 16:55:46 +0100 | [diff] [blame] | 1700 | delta = ktime_sub(apic->lapic_timer.target_expiration, now); |
| 1701 | apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) + |
| 1702 | nsec_to_cycles(apic->vcpu, delta); |
Wanpeng Li | 7d7f7da | 2016-10-24 18:23:09 +0800 | [diff] [blame] | 1703 | } |
| 1704 | |
Anthoine Bourgeois | ecf08da | 2018-04-29 22:05:58 +0000 | [diff] [blame] | 1705 | static void start_sw_period(struct kvm_lapic *apic) |
| 1706 | { |
| 1707 | if (!apic->lapic_timer.period) |
| 1708 | return; |
| 1709 | |
| 1710 | if (ktime_after(ktime_get(), |
| 1711 | apic->lapic_timer.target_expiration)) { |
| 1712 | apic_timer_expired(apic); |
| 1713 | |
| 1714 | if (apic_lvtt_oneshot(apic)) |
| 1715 | return; |
| 1716 | |
| 1717 | advance_periodic_target_expiration(apic); |
| 1718 | } |
| 1719 | |
| 1720 | hrtimer_start(&apic->lapic_timer.timer, |
| 1721 | apic->lapic_timer.target_expiration, |
Wanpeng Li | 4d151bf | 2019-07-06 09:26:50 +0800 | [diff] [blame] | 1722 | HRTIMER_MODE_ABS); |
Anthoine Bourgeois | ecf08da | 2018-04-29 22:05:58 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1725 | bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu) |
| 1726 | { |
Wanpeng Li | 9100530 | 2016-08-03 12:04:12 +0800 | [diff] [blame] | 1727 | if (!lapic_in_kernel(vcpu)) |
| 1728 | return false; |
| 1729 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1730 | return vcpu->arch.apic->lapic_timer.hv_timer_in_use; |
| 1731 | } |
| 1732 | EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use); |
| 1733 | |
Wanpeng Li | 7e810a3 | 2016-10-24 18:23:12 +0800 | [diff] [blame] | 1734 | static void cancel_hv_timer(struct kvm_lapic *apic) |
Wanpeng Li | bd97ad0 | 2016-06-30 08:52:49 +0800 | [diff] [blame] | 1735 | { |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1736 | WARN_ON(preemptible()); |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1737 | WARN_ON(!apic->lapic_timer.hv_timer_in_use); |
Wanpeng Li | bd97ad0 | 2016-06-30 08:52:49 +0800 | [diff] [blame] | 1738 | kvm_x86_ops->cancel_hv_timer(apic->vcpu); |
| 1739 | apic->lapic_timer.hv_timer_in_use = false; |
| 1740 | } |
| 1741 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1742 | static bool start_hv_timer(struct kvm_lapic *apic) |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1743 | { |
| 1744 | struct kvm_timer *ktimer = &apic->lapic_timer; |
Sean Christopherson | f992798 | 2019-04-16 13:32:46 -0700 | [diff] [blame] | 1745 | struct kvm_vcpu *vcpu = apic->vcpu; |
| 1746 | bool expired; |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1747 | |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1748 | WARN_ON(preemptible()); |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1749 | if (!kvm_x86_ops->set_hv_timer) |
| 1750 | return false; |
| 1751 | |
Radim Krčmář | 86bbc1e | 2017-10-06 19:25:53 +0200 | [diff] [blame] | 1752 | if (!ktimer->tscdeadline) |
| 1753 | return false; |
| 1754 | |
Sean Christopherson | f992798 | 2019-04-16 13:32:46 -0700 | [diff] [blame] | 1755 | if (kvm_x86_ops->set_hv_timer(vcpu, ktimer->tscdeadline, &expired)) |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1756 | return false; |
| 1757 | |
| 1758 | ktimer->hv_timer_in_use = true; |
| 1759 | hrtimer_cancel(&ktimer->timer); |
| 1760 | |
| 1761 | /* |
Sean Christopherson | f1ba5cf | 2019-04-16 13:32:45 -0700 | [diff] [blame] | 1762 | * To simplify handling the periodic timer, leave the hv timer running |
| 1763 | * even if the deadline timer has expired, i.e. rely on the resulting |
| 1764 | * VM-Exit to recompute the periodic timer's target expiration. |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1765 | */ |
Sean Christopherson | f1ba5cf | 2019-04-16 13:32:45 -0700 | [diff] [blame] | 1766 | if (!apic_lvtt_period(apic)) { |
| 1767 | /* |
| 1768 | * Cancel the hv timer if the sw timer fired while the hv timer |
| 1769 | * was being programmed, or if the hv timer itself expired. |
| 1770 | */ |
| 1771 | if (atomic_read(&ktimer->pending)) { |
| 1772 | cancel_hv_timer(apic); |
Sean Christopherson | f992798 | 2019-04-16 13:32:46 -0700 | [diff] [blame] | 1773 | } else if (expired) { |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 1774 | apic_timer_expired(apic); |
Sean Christopherson | f1ba5cf | 2019-04-16 13:32:45 -0700 | [diff] [blame] | 1775 | cancel_hv_timer(apic); |
| 1776 | } |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 1777 | } |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1778 | |
Sean Christopherson | f992798 | 2019-04-16 13:32:46 -0700 | [diff] [blame] | 1779 | trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use); |
Sean Christopherson | f1ba5cf | 2019-04-16 13:32:45 -0700 | [diff] [blame] | 1780 | |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1781 | return true; |
| 1782 | } |
| 1783 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1784 | static void start_sw_timer(struct kvm_lapic *apic) |
Wanpeng Li | 196f20c | 2016-06-28 14:54:19 +0800 | [diff] [blame] | 1785 | { |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1786 | struct kvm_timer *ktimer = &apic->lapic_timer; |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1787 | |
| 1788 | WARN_ON(preemptible()); |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1789 | if (apic->lapic_timer.hv_timer_in_use) |
| 1790 | cancel_hv_timer(apic); |
| 1791 | if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending)) |
| 1792 | return; |
Paolo Bonzini | 35ee9e4 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1793 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1794 | if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) |
| 1795 | start_sw_period(apic); |
| 1796 | else if (apic_lvtt_tscdeadline(apic)) |
| 1797 | start_sw_tscdeadline(apic); |
| 1798 | trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false); |
| 1799 | } |
| 1800 | |
| 1801 | static void restart_apic_timer(struct kvm_lapic *apic) |
| 1802 | { |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1803 | preempt_disable(); |
Sean Christopherson | 4ca88b3 | 2019-04-16 13:32:47 -0700 | [diff] [blame] | 1804 | |
| 1805 | if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending)) |
| 1806 | goto out; |
| 1807 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1808 | if (!start_hv_timer(apic)) |
| 1809 | start_sw_timer(apic); |
Sean Christopherson | 4ca88b3 | 2019-04-16 13:32:47 -0700 | [diff] [blame] | 1810 | out: |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1811 | preempt_enable(); |
Wanpeng Li | 196f20c | 2016-06-28 14:54:19 +0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1814 | void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu) |
| 1815 | { |
| 1816 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1817 | |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1818 | preempt_disable(); |
| 1819 | /* If the preempt notifier has already run, it also called apic_timer_expired */ |
| 1820 | if (!apic->lapic_timer.hv_timer_in_use) |
| 1821 | goto out; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1822 | WARN_ON(swait_active(&vcpu->wq)); |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1823 | cancel_hv_timer(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1824 | apic_timer_expired(apic); |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1825 | |
| 1826 | if (apic_lvtt_period(apic) && apic->lapic_timer.period) { |
| 1827 | advance_periodic_target_expiration(apic); |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1828 | restart_apic_timer(apic); |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 1829 | } |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1830 | out: |
| 1831 | preempt_enable(); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1832 | } |
| 1833 | EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer); |
| 1834 | |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1835 | void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu) |
| 1836 | { |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1837 | restart_apic_timer(vcpu->arch.apic); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1838 | } |
| 1839 | EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_hv_timer); |
| 1840 | |
| 1841 | void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu) |
| 1842 | { |
| 1843 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1844 | |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1845 | preempt_disable(); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1846 | /* Possibly the TSC deadline timer is not enabled yet */ |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1847 | if (apic->lapic_timer.hv_timer_in_use) |
| 1848 | start_sw_timer(apic); |
Wanpeng Li | 1d518c6 | 2017-07-25 00:43:15 -0700 | [diff] [blame] | 1849 | preempt_enable(); |
Yunhong Jiang | ce7a058 | 2016-06-13 14:20:01 -0700 | [diff] [blame] | 1850 | } |
| 1851 | EXPORT_SYMBOL_GPL(kvm_lapic_switch_to_sw_timer); |
| 1852 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1853 | void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu) |
| 1854 | { |
| 1855 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1856 | |
| 1857 | WARN_ON(!apic->lapic_timer.hv_timer_in_use); |
| 1858 | restart_apic_timer(apic); |
| 1859 | } |
| 1860 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1861 | static void start_apic_timer(struct kvm_lapic *apic) |
| 1862 | { |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1863 | atomic_set(&apic->lapic_timer.pending, 0); |
Avi Kivity | 0b975a3 | 2008-02-24 14:37:50 +0200 | [diff] [blame] | 1864 | |
Paolo Bonzini | a749e24 | 2017-06-29 17:14:50 +0200 | [diff] [blame] | 1865 | if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) |
| 1866 | && !set_target_expiration(apic)) |
| 1867 | return; |
| 1868 | |
| 1869 | restart_apic_timer(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1870 | } |
| 1871 | |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1872 | static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val) |
| 1873 | { |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1874 | bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val); |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1875 | |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1876 | if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) { |
| 1877 | apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode; |
| 1878 | if (lvt0_in_nmi_mode) { |
Radim Krčmář | 4272013 | 2015-07-01 15:31:49 +0200 | [diff] [blame] | 1879 | atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode); |
Radim Krčmář | 59fd132 | 2015-06-30 22:19:16 +0200 | [diff] [blame] | 1880 | } else |
| 1881 | atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode); |
| 1882 | } |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1885 | 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] | 1886 | { |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1887 | int ret = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1888 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1889 | trace_kvm_apic_write(reg, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1890 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1891 | switch (reg) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1892 | case APIC_ID: /* Local APIC ID */ |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1893 | if (!apic_x2apic_mode(apic)) |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 1894 | kvm_apic_set_xapic_id(apic, val >> 24); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1895 | else |
| 1896 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1897 | break; |
| 1898 | |
| 1899 | case APIC_TASKPRI: |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1900 | report_tpr_access(apic, true); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1901 | apic_set_tpr(apic, val & 0xff); |
| 1902 | break; |
| 1903 | |
| 1904 | case APIC_EOI: |
| 1905 | apic_set_eoi(apic); |
| 1906 | break; |
| 1907 | |
| 1908 | case APIC_LDR: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1909 | if (!apic_x2apic_mode(apic)) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1910 | kvm_apic_set_ldr(apic, val & APIC_LDR_MASK); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1911 | else |
| 1912 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | case APIC_DFR: |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1916 | if (!apic_x2apic_mode(apic)) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1917 | kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 1918 | recalculate_apic_map(apic->vcpu->kvm); |
| 1919 | } else |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1920 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1921 | break; |
| 1922 | |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1923 | case APIC_SPIV: { |
| 1924 | u32 mask = 0x3ff; |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1925 | if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI) |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1926 | mask |= APIC_SPIV_DIRECTED_EOI; |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 1927 | apic_set_spiv(apic, val & mask); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1928 | if (!(val & APIC_SPIV_APIC_ENABLED)) { |
| 1929 | int i; |
| 1930 | u32 lvt_val; |
| 1931 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1932 | for (i = 0; i < KVM_APIC_LVT_NUM; i++) { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 1933 | lvt_val = kvm_lapic_get_reg(apic, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1934 | APIC_LVTT + 0x10 * i); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1935 | kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1936 | lvt_val | APIC_LVT_MASKED); |
| 1937 | } |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1938 | apic_update_lvtt(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1939 | atomic_set(&apic->lapic_timer.pending, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1940 | |
| 1941 | } |
| 1942 | break; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1943 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1944 | case APIC_ICR: |
| 1945 | /* No delay here, so we always clear the pending bit */ |
Wanpeng Li | 2b0911d | 2019-09-05 14:26:27 +0800 | [diff] [blame] | 1946 | val &= ~(1 << 12); |
| 1947 | apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2)); |
| 1948 | kvm_lapic_set_reg(apic, APIC_ICR, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1949 | break; |
| 1950 | |
| 1951 | case APIC_ICR2: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1952 | if (!apic_x2apic_mode(apic)) |
| 1953 | val &= 0xff000000; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1954 | kvm_lapic_set_reg(apic, APIC_ICR2, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1955 | break; |
| 1956 | |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 1957 | case APIC_LVT0: |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 1958 | apic_manage_nmi_watchdog(apic, val); |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 1959 | /* fall through */ |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1960 | case APIC_LVTTHMR: |
| 1961 | case APIC_LVTPC: |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1962 | case APIC_LVT1: |
| 1963 | case APIC_LVTERR: |
| 1964 | /* TODO: Check vector */ |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1965 | if (!kvm_apic_sw_enabled(apic)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1966 | val |= APIC_LVT_MASKED; |
| 1967 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1968 | val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4]; |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1969 | kvm_lapic_set_reg(apic, reg, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1970 | |
| 1971 | break; |
| 1972 | |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1973 | case APIC_LVTT: |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 1974 | if (!kvm_apic_sw_enabled(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1975 | val |= APIC_LVT_MASKED; |
| 1976 | val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1977 | kvm_lapic_set_reg(apic, APIC_LVTT, val); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 1978 | apic_update_lvtt(apic); |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1979 | break; |
| 1980 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1981 | case APIC_TMICT: |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 1982 | if (apic_lvtt_tscdeadline(apic)) |
| 1983 | break; |
| 1984 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1985 | hrtimer_cancel(&apic->lapic_timer.timer); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1986 | kvm_lapic_set_reg(apic, APIC_TMICT, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1987 | start_apic_timer(apic); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1988 | break; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1989 | |
Wanpeng Li | c301b90 | 2017-10-06 07:38:32 -0700 | [diff] [blame] | 1990 | case APIC_TDCR: { |
| 1991 | uint32_t old_divisor = apic->divide_count; |
| 1992 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 1993 | kvm_lapic_set_reg(apic, APIC_TDCR, val); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 1994 | update_divide_count(apic); |
Wanpeng Li | c301b90 | 2017-10-06 07:38:32 -0700 | [diff] [blame] | 1995 | if (apic->divide_count != old_divisor && |
| 1996 | apic->lapic_timer.period) { |
| 1997 | hrtimer_cancel(&apic->lapic_timer.timer); |
| 1998 | update_target_expiration(apic, old_divisor); |
| 1999 | restart_apic_timer(apic); |
| 2000 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2001 | break; |
Wanpeng Li | c301b90 | 2017-10-06 07:38:32 -0700 | [diff] [blame] | 2002 | } |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2003 | case APIC_ESR: |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 2004 | if (apic_x2apic_mode(apic) && val != 0) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2005 | ret = 1; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2006 | break; |
| 2007 | |
| 2008 | case APIC_SELF_IPI: |
| 2009 | if (apic_x2apic_mode(apic)) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2010 | kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff)); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2011 | } else |
| 2012 | ret = 1; |
| 2013 | break; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2014 | default: |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2015 | ret = 1; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2016 | break; |
| 2017 | } |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 2018 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2019 | return ret; |
| 2020 | } |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2021 | EXPORT_SYMBOL_GPL(kvm_lapic_reg_write); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2022 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 2023 | 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] | 2024 | gpa_t address, int len, const void *data) |
| 2025 | { |
| 2026 | struct kvm_lapic *apic = to_lapic(this); |
| 2027 | unsigned int offset = address - apic->base_address; |
| 2028 | u32 val; |
| 2029 | |
| 2030 | if (!apic_mmio_in_range(apic, address)) |
| 2031 | return -EOPNOTSUPP; |
| 2032 | |
Vitaly Kuznetsov | d176620 | 2018-08-02 17:08:16 +0200 | [diff] [blame] | 2033 | if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) { |
| 2034 | if (!kvm_check_has_quirk(vcpu->kvm, |
| 2035 | KVM_X86_QUIRK_LAPIC_MMIO_HOLE)) |
| 2036 | return -EOPNOTSUPP; |
| 2037 | |
| 2038 | return 0; |
| 2039 | } |
| 2040 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2041 | /* |
| 2042 | * APIC register must be aligned on 128-bits boundary. |
| 2043 | * 32/64/128 bits registers must be accessed thru 32 bits. |
| 2044 | * Refer SDM 8.4.1 |
| 2045 | */ |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 2046 | if (len != 4 || (offset & 0xf)) |
Sheng Yang | 756975b | 2009-07-06 11:05:39 +0800 | [diff] [blame] | 2047 | return 0; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2048 | |
| 2049 | val = *(u32*)data; |
| 2050 | |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 2051 | kvm_lapic_reg_write(apic, offset & 0xff0, val); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2052 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2053 | return 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2054 | } |
| 2055 | |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 2056 | void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) |
| 2057 | { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2058 | kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 2059 | } |
| 2060 | EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi); |
| 2061 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2062 | /* emulate APIC access in a trap manner */ |
| 2063 | void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) |
| 2064 | { |
| 2065 | u32 val = 0; |
| 2066 | |
| 2067 | /* hw has done the conditional check and inst decode */ |
| 2068 | offset &= 0xff0; |
| 2069 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2070 | kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2071 | |
| 2072 | /* TODO: optimize to just emulate side effect w/o one more write */ |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2073 | kvm_lapic_reg_write(vcpu->arch.apic, offset, val); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2074 | } |
| 2075 | EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode); |
| 2076 | |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 2077 | void kvm_free_lapic(struct kvm_vcpu *vcpu) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2078 | { |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2079 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2080 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2081 | if (!vcpu->arch.apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2082 | return; |
| 2083 | |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2084 | hrtimer_cancel(&apic->lapic_timer.timer); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2085 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2086 | if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE)) |
| 2087 | static_key_slow_dec_deferred(&apic_hw_disabled); |
| 2088 | |
Radim Krčmář | e462755 | 2014-10-30 15:06:45 +0100 | [diff] [blame] | 2089 | if (!apic->sw_enabled) |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2090 | static_key_slow_dec_deferred(&apic_sw_disabled); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2091 | |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2092 | if (apic->regs) |
| 2093 | free_page((unsigned long)apic->regs); |
| 2094 | |
| 2095 | kfree(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | *---------------------------------------------------------------------- |
| 2100 | * LAPIC interface |
| 2101 | *---------------------------------------------------------------------- |
| 2102 | */ |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2103 | u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu) |
| 2104 | { |
| 2105 | struct kvm_lapic *apic = vcpu->arch.apic; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2106 | |
Wanpeng Li | a10388e | 2016-10-24 18:23:10 +0800 | [diff] [blame] | 2107 | if (!lapic_in_kernel(vcpu) || |
| 2108 | !apic_lvtt_tscdeadline(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2109 | return 0; |
| 2110 | |
| 2111 | return apic->lapic_timer.tscdeadline; |
| 2112 | } |
| 2113 | |
| 2114 | void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data) |
| 2115 | { |
| 2116 | struct kvm_lapic *apic = vcpu->arch.apic; |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2117 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2118 | if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) || |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2119 | apic_lvtt_period(apic)) |
Liu, Jinsong | a3e06bb | 2011-09-22 16:55:52 +0800 | [diff] [blame] | 2120 | return; |
| 2121 | |
| 2122 | hrtimer_cancel(&apic->lapic_timer.timer); |
| 2123 | apic->lapic_timer.tscdeadline = data; |
| 2124 | start_apic_timer(apic); |
| 2125 | } |
| 2126 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2127 | void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 2128 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2129 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2130 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2131 | apic_set_tpr(apic, ((cr8 & 0x0f) << 4) |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2132 | | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu) |
| 2136 | { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2137 | u64 tpr; |
| 2138 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2139 | tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2140 | |
| 2141 | return (tpr & 0xf0) >> 4; |
| 2142 | } |
| 2143 | |
| 2144 | void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) |
| 2145 | { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2146 | u64 old_value = vcpu->arch.apic_base; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2147 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2148 | |
Jim Mattson | c7dd15b | 2016-11-09 09:50:11 -0800 | [diff] [blame] | 2149 | if (!apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2150 | value |= MSR_IA32_APICBASE_BSP; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 2151 | |
Jan Kiszka | e66d2ae | 2013-12-29 02:29:30 +0100 | [diff] [blame] | 2152 | vcpu->arch.apic_base = value; |
| 2153 | |
Jim Mattson | c7dd15b | 2016-11-09 09:50:11 -0800 | [diff] [blame] | 2154 | if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) |
| 2155 | kvm_update_cpuid(vcpu); |
| 2156 | |
| 2157 | if (!apic) |
| 2158 | return; |
| 2159 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2160 | /* update jump label if enable bit changes */ |
Andrew Jones | 0dce7cd | 2014-01-15 13:39:59 +0100 | [diff] [blame] | 2161 | if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) { |
Radim Krčmář | 49bd29b | 2016-07-12 22:09:23 +0200 | [diff] [blame] | 2162 | if (value & MSR_IA32_APICBASE_ENABLE) { |
| 2163 | kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2164 | static_key_slow_dec_deferred(&apic_hw_disabled); |
Wanpeng Li | 187ca84 | 2016-08-03 12:04:13 +0800 | [diff] [blame] | 2165 | } else { |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2166 | static_key_slow_inc(&apic_hw_disabled.key); |
Wanpeng Li | 187ca84 | 2016-08-03 12:04:13 +0800 | [diff] [blame] | 2167 | recalculate_apic_map(vcpu->kvm); |
| 2168 | } |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2169 | } |
| 2170 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 2171 | if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE)) |
| 2172 | kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id); |
| 2173 | |
| 2174 | if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) |
| 2175 | kvm_x86_ops->set_virtual_apic_mode(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2176 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2177 | apic->base_address = apic->vcpu->arch.apic_base & |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2178 | MSR_IA32_APICBASE_BASE; |
| 2179 | |
Nadav Amit | db324fe | 2014-11-02 11:54:59 +0200 | [diff] [blame] | 2180 | if ((value & MSR_IA32_APICBASE_ENABLE) && |
| 2181 | apic->base_address != APIC_DEFAULT_PHYS_BASE) |
| 2182 | pr_warn_once("APIC base relocation is unsupported by KVM"); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2183 | } |
| 2184 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 2185 | void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2186 | { |
Radim Krčmář | b7e31be | 2018-03-01 15:24:25 +0100 | [diff] [blame] | 2187 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2188 | int i; |
| 2189 | |
Radim Krčmář | b7e31be | 2018-03-01 15:24:25 +0100 | [diff] [blame] | 2190 | if (!apic) |
| 2191 | return; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2192 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2193 | /* 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] | 2194 | hrtimer_cancel(&apic->lapic_timer.timer); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2195 | |
Radim Krčmář | 4d8e772 | 2016-07-12 22:09:25 +0200 | [diff] [blame] | 2196 | if (!init_event) { |
| 2197 | kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE | |
| 2198 | MSR_IA32_APICBASE_ENABLE); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2199 | kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); |
Radim Krčmář | 4d8e772 | 2016-07-12 22:09:25 +0200 | [diff] [blame] | 2200 | } |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 2201 | kvm_apic_set_version(apic->vcpu); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2202 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2203 | for (i = 0; i < KVM_APIC_LVT_NUM; i++) |
| 2204 | 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] | 2205 | apic_update_lvtt(apic); |
Jan H. Schönherr | 52b5419 | 2017-05-20 13:24:32 +0200 | [diff] [blame] | 2206 | if (kvm_vcpu_is_reset_bsp(vcpu) && |
| 2207 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED)) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2208 | kvm_lapic_set_reg(apic, APIC_LVT0, |
Nadav Amit | 90de4a1 | 2015-04-13 01:53:41 +0300 | [diff] [blame] | 2209 | SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT)); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2210 | apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0)); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2211 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2212 | kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU); |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2213 | apic_set_spiv(apic, 0xff); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2214 | kvm_lapic_set_reg(apic, APIC_TASKPRI, 0); |
Radim Krčmář | c028dd6 | 2015-05-22 19:22:10 +0200 | [diff] [blame] | 2215 | if (!apic_x2apic_mode(apic)) |
| 2216 | kvm_apic_set_ldr(apic, 0); |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2217 | kvm_lapic_set_reg(apic, APIC_ESR, 0); |
| 2218 | kvm_lapic_set_reg(apic, APIC_ICR, 0); |
| 2219 | kvm_lapic_set_reg(apic, APIC_ICR2, 0); |
| 2220 | kvm_lapic_set_reg(apic, APIC_TDCR, 0); |
| 2221 | kvm_lapic_set_reg(apic, APIC_TMICT, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2222 | for (i = 0; i < 8; i++) { |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2223 | kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0); |
| 2224 | kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0); |
| 2225 | kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2226 | } |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2227 | apic->irr_pending = vcpu->arch.apicv_active; |
| 2228 | apic->isr_count = vcpu->arch.apicv_active ? 1 : 0; |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 2229 | apic->highest_isr_cache = -1; |
Kevin Pedretti | b33ac88 | 2007-10-21 08:54:53 +0200 | [diff] [blame] | 2230 | update_divide_count(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 2231 | atomic_set(&apic->lapic_timer.pending, 0); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 2232 | if (kvm_vcpu_is_bsp(vcpu)) |
Gleb Natapov | 5dbc8f3 | 2012-08-05 15:58:27 +0300 | [diff] [blame] | 2233 | kvm_lapic_set_base(vcpu, |
| 2234 | vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2235 | vcpu->arch.pv_eoi.msr_val = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2236 | apic_update_ppr(apic); |
Jan H. Schönherr | 4191db2 | 2017-10-25 16:43:27 +0200 | [diff] [blame] | 2237 | if (vcpu->arch.apicv_active) { |
| 2238 | kvm_x86_ops->apicv_post_state_restore(vcpu); |
| 2239 | kvm_x86_ops->hwapic_irr_update(vcpu, -1); |
| 2240 | kvm_x86_ops->hwapic_isr_update(vcpu, -1); |
| 2241 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2242 | |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 2243 | vcpu->arch.apic_arb_prio = 0; |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2244 | vcpu->arch.apic_attention = 0; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2245 | } |
| 2246 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2247 | /* |
| 2248 | *---------------------------------------------------------------------- |
| 2249 | * timer interface |
| 2250 | *---------------------------------------------------------------------- |
| 2251 | */ |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2252 | |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 2253 | static bool lapic_is_periodic(struct kvm_lapic *apic) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2254 | { |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 2255 | return apic_lvtt_period(apic); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2256 | } |
| 2257 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 2258 | int apic_has_pending_timer(struct kvm_vcpu *vcpu) |
| 2259 | { |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2260 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 2261 | |
Paolo Bonzini | 1e3161b4 | 2016-01-08 13:41:16 +0100 | [diff] [blame] | 2262 | if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT)) |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2263 | return atomic_read(&apic->lapic_timer.pending); |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 2264 | |
| 2265 | return 0; |
| 2266 | } |
| 2267 | |
Avi Kivity | 8934208 | 2011-11-10 14:57:21 +0200 | [diff] [blame] | 2268 | int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type) |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2269 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2270 | u32 reg = kvm_lapic_get_reg(apic, lvt_type); |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 2271 | int vector, mode, trig_mode; |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2272 | |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 2273 | if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) { |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 2274 | vector = reg & APIC_VECTOR_MASK; |
| 2275 | mode = reg & APIC_MODE_MASK; |
| 2276 | trig_mode = reg & APIC_LVT_LEVEL_TRIGGER; |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 2277 | return __apic_accept_irq(apic, mode, vector, 1, trig_mode, |
| 2278 | NULL); |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 2279 | } |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
Jan Kiszka | 8fdb235 | 2008-10-20 10:20:02 +0200 | [diff] [blame] | 2283 | void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu) |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 2284 | { |
Jan Kiszka | 8fdb235 | 2008-10-20 10:20:02 +0200 | [diff] [blame] | 2285 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2286 | |
| 2287 | if (apic) |
| 2288 | kvm_apic_local_deliver(apic, APIC_LVT0); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2289 | } |
| 2290 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 2291 | static const struct kvm_io_device_ops apic_mmio_ops = { |
| 2292 | .read = apic_mmio_read, |
| 2293 | .write = apic_mmio_write, |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 2294 | }; |
| 2295 | |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 2296 | static enum hrtimer_restart apic_timer_fn(struct hrtimer *data) |
| 2297 | { |
| 2298 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 2299 | struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 2300 | |
Radim Krčmář | 5d87db7 | 2014-10-10 19:15:08 +0200 | [diff] [blame] | 2301 | apic_timer_expired(apic); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 2302 | |
Avi Kivity | 2a6eac9 | 2012-07-26 18:01:51 +0300 | [diff] [blame] | 2303 | if (lapic_is_periodic(apic)) { |
Wanpeng Li | 8003c9a | 2016-10-24 18:23:13 +0800 | [diff] [blame] | 2304 | advance_periodic_target_expiration(apic); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 2305 | hrtimer_add_expires_ns(&ktimer->timer, ktimer->period); |
| 2306 | return HRTIMER_RESTART; |
| 2307 | } else |
| 2308 | return HRTIMER_NORESTART; |
| 2309 | } |
| 2310 | |
Sean Christopherson | c3941d9 | 2019-04-17 10:15:33 -0700 | [diff] [blame] | 2311 | int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2312 | { |
| 2313 | struct kvm_lapic *apic; |
| 2314 | |
| 2315 | ASSERT(vcpu != NULL); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2316 | |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 2317 | apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2318 | if (!apic) |
| 2319 | goto nomem; |
| 2320 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2321 | vcpu->arch.apic = apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2322 | |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 2323 | apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 2324 | if (!apic->regs) { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2325 | printk(KERN_ERR "malloc apic regs error for vcpu %x\n", |
| 2326 | vcpu->vcpu_id); |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 2327 | goto nomem_free_apic; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2328 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2329 | apic->vcpu = vcpu; |
| 2330 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 2331 | hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC, |
Sebastian Andrzej Siewior | 2c0d278 | 2019-07-26 20:30:55 +0200 | [diff] [blame] | 2332 | HRTIMER_MODE_ABS_HARD); |
Avi Kivity | e9d90d4 | 2012-07-26 18:01:50 +0300 | [diff] [blame] | 2333 | apic->lapic_timer.timer.function = apic_timer_fn; |
Sean Christopherson | c3941d9 | 2019-04-17 10:15:33 -0700 | [diff] [blame] | 2334 | if (timer_advance_ns == -1) { |
Wanpeng Li | a0f0037 | 2019-09-26 08:54:03 +0800 | [diff] [blame] | 2335 | apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT; |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 2336 | lapic_timer_advance_dynamic = true; |
Sean Christopherson | c3941d9 | 2019-04-17 10:15:33 -0700 | [diff] [blame] | 2337 | } else { |
| 2338 | apic->lapic_timer.timer_advance_ns = timer_advance_ns; |
Wanpeng Li | d0f5a86 | 2019-09-17 16:16:26 +0800 | [diff] [blame] | 2339 | lapic_timer_advance_dynamic = false; |
Sean Christopherson | c3941d9 | 2019-04-17 10:15:33 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2342 | /* |
| 2343 | * APIC is created enabled. This will prevent kvm_lapic_set_base from |
Wei Yang | ee171d2 | 2019-03-31 19:17:22 -0700 | [diff] [blame] | 2344 | * thinking that APIC state has changed. |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2345 | */ |
| 2346 | vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2347 | static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */ |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 2348 | kvm_iodevice_init(&apic->dev, &apic_mmio_ops); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2349 | |
| 2350 | return 0; |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 2351 | nomem_free_apic: |
| 2352 | kfree(apic); |
Saar Amar | a251fb90 | 2019-05-06 11:29:16 +0300 | [diff] [blame] | 2353 | vcpu->arch.apic = NULL; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2354 | nomem: |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2355 | return -ENOMEM; |
| 2356 | } |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2357 | |
| 2358 | int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 2359 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2360 | struct kvm_lapic *apic = vcpu->arch.apic; |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 2361 | u32 ppr; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2362 | |
Wanpeng Li | bb34e69 | 2019-07-02 17:25:02 +0800 | [diff] [blame] | 2363 | if (!kvm_apic_hw_enabled(apic)) |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2364 | return -1; |
| 2365 | |
Paolo Bonzini | b3c045d | 2016-12-18 21:47:54 +0100 | [diff] [blame] | 2366 | __apic_update_ppr(apic, &ppr); |
| 2367 | return apic_has_interrupt_for_ppr(apic, ppr); |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2368 | } |
| 2369 | |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 2370 | int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu) |
| 2371 | { |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2372 | u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0); |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 2373 | int r = 0; |
| 2374 | |
Gleb Natapov | c48f149 | 2012-08-05 15:58:33 +0300 | [diff] [blame] | 2375 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) |
Chris Lalancette | e7dca5c | 2010-06-16 17:11:12 -0400 | [diff] [blame] | 2376 | r = 1; |
| 2377 | if ((lvt0 & APIC_LVT_MASKED) == 0 && |
| 2378 | GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT) |
| 2379 | r = 1; |
Qing He | 40487c6 | 2007-09-17 14:47:13 +0800 | [diff] [blame] | 2380 | return r; |
| 2381 | } |
| 2382 | |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2383 | void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu) |
| 2384 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2385 | struct kvm_lapic *apic = vcpu->arch.apic; |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2386 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2387 | if (atomic_read(&apic->lapic_timer.pending) > 0) { |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 2388 | kvm_apic_inject_pending_timer_irqs(apic); |
Jan Kiszka | f1ed045 | 2013-04-28 14:00:41 +0200 | [diff] [blame] | 2389 | atomic_set(&apic->lapic_timer.pending, 0); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2393 | int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu) |
| 2394 | { |
| 2395 | int vector = kvm_apic_has_interrupt(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2396 | struct kvm_lapic *apic = vcpu->arch.apic; |
Paolo Bonzini | 4d82d12 | 2016-12-18 21:43:41 +0100 | [diff] [blame] | 2397 | u32 ppr; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2398 | |
| 2399 | if (vector == -1) |
| 2400 | return -1; |
| 2401 | |
Wanpeng Li | 56cc240 | 2014-08-05 12:42:24 +0800 | [diff] [blame] | 2402 | /* |
| 2403 | * We get here even with APIC virtualization enabled, if doing |
| 2404 | * nested virtualization and L1 runs with the "acknowledge interrupt |
| 2405 | * on exit" mode. Then we cannot inject the interrupt via RVI, |
| 2406 | * because the process would deliver it through the IDT. |
| 2407 | */ |
| 2408 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2409 | apic_clear_irr(vector, apic); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 2410 | if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) { |
Paolo Bonzini | 4d82d12 | 2016-12-18 21:43:41 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * For auto-EOI interrupts, there might be another pending |
| 2413 | * interrupt above PPR, so check whether to raise another |
| 2414 | * KVM_REQ_EVENT. |
| 2415 | */ |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 2416 | apic_update_ppr(apic); |
Paolo Bonzini | 4d82d12 | 2016-12-18 21:43:41 +0100 | [diff] [blame] | 2417 | } else { |
| 2418 | /* |
| 2419 | * For normal interrupts, PPR has been raised and there cannot |
| 2420 | * be a higher-priority pending interrupt---except if there was |
| 2421 | * a concurrent interrupt injection, but that would have |
| 2422 | * triggered KVM_REQ_EVENT already. |
| 2423 | */ |
| 2424 | apic_set_isr(vector, apic); |
| 2425 | __apic_update_ppr(apic, &ppr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 2426 | } |
| 2427 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2428 | return vector; |
| 2429 | } |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2430 | |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2431 | static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, |
| 2432 | struct kvm_lapic_state *s, bool set) |
| 2433 | { |
| 2434 | if (apic_x2apic_mode(vcpu->arch.apic)) { |
| 2435 | u32 *id = (u32 *)(s->regs + APIC_ID); |
Dr. David Alan Gilbert | 12806ba | 2017-11-17 11:52:50 +0000 | [diff] [blame] | 2436 | u32 *ldr = (u32 *)(s->regs + APIC_LDR); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2437 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 2438 | if (vcpu->kvm->arch.x2apic_format) { |
| 2439 | if (*id != vcpu->vcpu_id) |
| 2440 | return -EINVAL; |
| 2441 | } else { |
| 2442 | if (set) |
| 2443 | *id >>= 24; |
| 2444 | else |
| 2445 | *id <<= 24; |
| 2446 | } |
Dr. David Alan Gilbert | 12806ba | 2017-11-17 11:52:50 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* In x2APIC mode, the LDR is fixed and based on the id */ |
| 2449 | if (set) |
| 2450 | *ldr = kvm_apic_calc_x2apic_ldr(*id); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s) |
| 2457 | { |
| 2458 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s)); |
| 2459 | return kvm_apic_state_fixup(vcpu, s, false); |
| 2460 | } |
| 2461 | |
| 2462 | 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] | 2463 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2464 | struct kvm_lapic *apic = vcpu->arch.apic; |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2465 | int r; |
| 2466 | |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2467 | |
Gleb Natapov | 5dbc8f3 | 2012-08-05 15:58:27 +0300 | [diff] [blame] | 2468 | kvm_lapic_set_base(vcpu, vcpu->arch.apic_base); |
Gleb Natapov | 64eb062 | 2012-08-08 15:24:36 +0300 | [diff] [blame] | 2469 | /* set SPIV separately to get count of SW disabled APICs right */ |
| 2470 | apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV))); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2471 | |
| 2472 | r = kvm_apic_state_fixup(vcpu, s, true); |
| 2473 | if (r) |
| 2474 | return r; |
Jordan Borgner | 0e96f31 | 2018-10-28 12:58:28 +0000 | [diff] [blame] | 2475 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s)); |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2476 | |
| 2477 | recalculate_apic_map(vcpu->kvm); |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 2478 | kvm_apic_set_version(vcpu); |
| 2479 | |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2480 | apic_update_ppr(apic); |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 2481 | hrtimer_cancel(&apic->lapic_timer.timer); |
Radim Krčmář | b6ac069 | 2015-06-05 20:57:41 +0200 | [diff] [blame] | 2482 | apic_update_lvtt(apic); |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2483 | apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0)); |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2484 | update_divide_count(apic); |
| 2485 | start_apic_timer(apic); |
Marcelo Tosatti | 6e24a6e | 2009-12-14 17:37:35 -0200 | [diff] [blame] | 2486 | apic->irr_pending = true; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2487 | apic->isr_count = vcpu->arch.apicv_active ? |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2488 | 1 : count_vectors(apic->regs + APIC_ISR); |
Michael S. Tsirkin | 8680b94 | 2012-06-24 19:24:26 +0300 | [diff] [blame] | 2489 | apic->highest_isr_cache = -1; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2490 | if (vcpu->arch.apicv_active) { |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 2491 | kvm_x86_ops->apicv_post_state_restore(vcpu); |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 2492 | kvm_x86_ops->hwapic_irr_update(vcpu, |
| 2493 | apic_find_highest_irr(apic)); |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 2494 | kvm_x86_ops->hwapic_isr_update(vcpu, |
Tiejun Chen | b4eef9b | 2014-12-22 10:32:57 +0100 | [diff] [blame] | 2495 | apic_find_highest_isr(apic)); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2496 | } |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 2497 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 2498 | if (ioapic_in_kernel(vcpu->kvm)) |
| 2499 | kvm_rtc_eoi_tracking_restore_one(vcpu); |
Radim Krčmář | 0669a51 | 2015-10-30 15:48:20 +0100 | [diff] [blame] | 2500 | |
| 2501 | vcpu->arch.apic_arb_prio = 0; |
Radim Krčmář | a92e254 | 2016-07-12 22:09:22 +0200 | [diff] [blame] | 2502 | |
| 2503 | return 0; |
Eddie Dong | 96ad2cc | 2007-09-06 12:22:56 +0300 | [diff] [blame] | 2504 | } |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2505 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 2506 | void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu) |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2507 | { |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2508 | struct hrtimer *timer; |
| 2509 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 2510 | if (!lapic_in_kernel(vcpu) || |
| 2511 | kvm_can_post_timer_interrupt(vcpu)) |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2512 | return; |
| 2513 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 2514 | timer = &vcpu->arch.apic->lapic_timer.timer; |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2515 | if (hrtimer_cancel(timer)) |
Sebastian Andrzej Siewior | 2c0d278 | 2019-07-26 20:30:55 +0200 | [diff] [blame] | 2516 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 2517 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2518 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2519 | /* |
| 2520 | * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt |
| 2521 | * |
| 2522 | * Detect whether guest triggered PV EOI since the |
| 2523 | * last entry. If yes, set EOI on guests's behalf. |
| 2524 | * Clear PV EOI in guest memory in any case. |
| 2525 | */ |
| 2526 | static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu, |
| 2527 | struct kvm_lapic *apic) |
| 2528 | { |
| 2529 | bool pending; |
| 2530 | int vector; |
| 2531 | /* |
| 2532 | * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host |
| 2533 | * and KVM_PV_EOI_ENABLED in guest memory as follows: |
| 2534 | * |
| 2535 | * KVM_APIC_PV_EOI_PENDING is unset: |
| 2536 | * -> host disabled PV EOI. |
| 2537 | * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set: |
| 2538 | * -> host enabled PV EOI, guest did not execute EOI yet. |
| 2539 | * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset: |
| 2540 | * -> host enabled PV EOI, guest executed EOI. |
| 2541 | */ |
| 2542 | BUG_ON(!pv_eoi_enabled(vcpu)); |
| 2543 | pending = pv_eoi_get_pending(vcpu); |
| 2544 | /* |
| 2545 | * Clear pending bit in any case: it will be set again on vmentry. |
| 2546 | * While this might not be ideal from performance point of view, |
| 2547 | * this makes sure pv eoi is only enabled when we know it's safe. |
| 2548 | */ |
| 2549 | pv_eoi_clr_pending(vcpu); |
| 2550 | if (pending) |
| 2551 | return; |
| 2552 | vector = apic_set_eoi(apic); |
| 2553 | trace_kvm_pv_eoi(apic, vector); |
| 2554 | } |
| 2555 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2556 | void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu) |
| 2557 | { |
| 2558 | u32 data; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2559 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2560 | if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention)) |
| 2561 | apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic); |
| 2562 | |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2563 | if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention)) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2564 | return; |
| 2565 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2566 | if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data, |
| 2567 | sizeof(u32))) |
Nicholas Krause | 603242a | 2015-08-05 10:44:40 -0400 | [diff] [blame] | 2568 | return; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2569 | |
| 2570 | apic_set_tpr(vcpu->arch.apic, data & 0xff); |
| 2571 | } |
| 2572 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2573 | /* |
| 2574 | * apic_sync_pv_eoi_to_guest - called before vmentry |
| 2575 | * |
| 2576 | * Detect whether it's safe to enable PV EOI and |
| 2577 | * if yes do so. |
| 2578 | */ |
| 2579 | static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu, |
| 2580 | struct kvm_lapic *apic) |
| 2581 | { |
| 2582 | if (!pv_eoi_enabled(vcpu) || |
| 2583 | /* IRR set or many bits in ISR: could be nested. */ |
| 2584 | apic->irr_pending || |
| 2585 | /* Cache not set: could be safe but we don't bother. */ |
| 2586 | apic->highest_isr_cache == -1 || |
| 2587 | /* Need EOI to update ioapic. */ |
Paolo Bonzini | 3bb345f | 2015-07-29 10:43:18 +0200 | [diff] [blame] | 2588 | kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) { |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2589 | /* |
| 2590 | * PV EOI was disabled by apic_sync_pv_eoi_from_guest |
| 2591 | * so we need not do anything here. |
| 2592 | */ |
| 2593 | return; |
| 2594 | } |
| 2595 | |
| 2596 | pv_eoi_set_pending(apic->vcpu); |
| 2597 | } |
| 2598 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2599 | void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu) |
| 2600 | { |
| 2601 | u32 data, tpr; |
| 2602 | int max_irr, max_isr; |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2603 | struct kvm_lapic *apic = vcpu->arch.apic; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2604 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2605 | apic_sync_pv_eoi_to_guest(vcpu, apic); |
| 2606 | |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2607 | if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention)) |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2608 | return; |
| 2609 | |
Suravee Suthikulpanit | dfb9595 | 2016-05-04 14:09:41 -0500 | [diff] [blame] | 2610 | tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2611 | max_irr = apic_find_highest_irr(apic); |
| 2612 | if (max_irr < 0) |
| 2613 | max_irr = 0; |
| 2614 | max_isr = apic_find_highest_isr(apic); |
| 2615 | if (max_isr < 0) |
| 2616 | max_isr = 0; |
| 2617 | data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24); |
| 2618 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2619 | kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data, |
| 2620 | sizeof(u32)); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2623 | 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] | 2624 | { |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2625 | if (vapic_addr) { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2626 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2627 | &vcpu->arch.apic->vapic_cache, |
| 2628 | vapic_addr, sizeof(u32))) |
| 2629 | return -EINVAL; |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2630 | __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention); |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2631 | } else { |
Gleb Natapov | 4138377 | 2012-04-19 14:06:29 +0300 | [diff] [blame] | 2632 | __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention); |
Andy Honig | fda4e2e | 2013-11-20 10:23:22 -0800 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | vcpu->arch.apic->vapic_addr = vapic_addr; |
| 2636 | return 0; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2637 | } |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2638 | |
| 2639 | int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 2640 | { |
| 2641 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2642 | u32 reg = (msr - APIC_BASE_MSR) << 4; |
| 2643 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 2644 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2645 | return 1; |
| 2646 | |
Nadav Amit | c69d3d9 | 2014-11-26 17:56:25 +0200 | [diff] [blame] | 2647 | if (reg == APIC_ICR2) |
| 2648 | return 1; |
| 2649 | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2650 | /* if this is ICR write vector before command */ |
Radim Krčmář | decdc28 | 2014-11-26 17:07:05 +0100 | [diff] [blame] | 2651 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2652 | kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32)); |
| 2653 | return kvm_lapic_reg_write(apic, reg, (u32)data); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data) |
| 2657 | { |
| 2658 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2659 | u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0; |
| 2660 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 2661 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2662 | return 1; |
| 2663 | |
Yi Wang | 0d88800 | 2019-07-06 01:08:48 +0800 | [diff] [blame] | 2664 | if (reg == APIC_DFR || reg == APIC_ICR2) |
Nadav Amit | c69d3d9 | 2014-11-26 17:56:25 +0200 | [diff] [blame] | 2665 | return 1; |
Nadav Amit | c69d3d9 | 2014-11-26 17:56:25 +0200 | [diff] [blame] | 2666 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2667 | if (kvm_lapic_reg_read(apic, reg, 4, &low)) |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2668 | return 1; |
Radim Krčmář | decdc28 | 2014-11-26 17:07:05 +0100 | [diff] [blame] | 2669 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2670 | kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high); |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2671 | |
| 2672 | *data = (((u64)high) << 32) | low; |
| 2673 | |
| 2674 | return 0; |
| 2675 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2676 | |
| 2677 | int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data) |
| 2678 | { |
| 2679 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2680 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2681 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2682 | return 1; |
| 2683 | |
| 2684 | /* if this is ICR write vector before command */ |
| 2685 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2686 | kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32)); |
| 2687 | return kvm_lapic_reg_write(apic, reg, (u32)data); |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data) |
| 2691 | { |
| 2692 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2693 | u32 low, high = 0; |
| 2694 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2695 | if (!lapic_in_kernel(vcpu)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2696 | return 1; |
| 2697 | |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2698 | if (kvm_lapic_reg_read(apic, reg, 4, &low)) |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2699 | return 1; |
| 2700 | if (reg == APIC_ICR) |
Suravee Suthikulpanit | 1e6e275 | 2016-05-04 14:09:40 -0500 | [diff] [blame] | 2701 | kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high); |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 2702 | |
| 2703 | *data = (((u64)high) << 32) | low; |
| 2704 | |
| 2705 | return 0; |
| 2706 | } |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2707 | |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 2708 | int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len) |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2709 | { |
| 2710 | u64 addr = data & ~KVM_MSR_ENABLED; |
Vitaly Kuznetsov | a7c42bb | 2018-10-16 18:50:06 +0200 | [diff] [blame] | 2711 | struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data; |
| 2712 | unsigned long new_len; |
| 2713 | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2714 | if (!IS_ALIGNED(addr, 4)) |
| 2715 | return 1; |
| 2716 | |
| 2717 | vcpu->arch.pv_eoi.msr_val = data; |
| 2718 | if (!pv_eoi_enabled(vcpu)) |
| 2719 | return 0; |
Vitaly Kuznetsov | a7c42bb | 2018-10-16 18:50:06 +0200 | [diff] [blame] | 2720 | |
| 2721 | if (addr == ghc->gpa && len <= ghc->len) |
| 2722 | new_len = ghc->len; |
| 2723 | else |
| 2724 | new_len = len; |
| 2725 | |
| 2726 | return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len); |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 2727 | } |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2728 | |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2729 | void kvm_apic_accept_events(struct kvm_vcpu *vcpu) |
| 2730 | { |
| 2731 | struct kvm_lapic *apic = vcpu->arch.apic; |
Paolo Bonzini | 2b4a273 | 2014-11-24 14:35:24 +0100 | [diff] [blame] | 2732 | u8 sipi_vector; |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2733 | unsigned long pe; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2734 | |
Paolo Bonzini | bce87cc | 2016-01-08 13:48:51 +0100 | [diff] [blame] | 2735 | if (!lapic_in_kernel(vcpu) || !apic->pending_events) |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2736 | return; |
| 2737 | |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2738 | /* |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 2739 | * INITs are latched while CPU is in specific states |
| 2740 | * (SMM, VMX non-root mode, SVM with GIF=0). |
| 2741 | * Because a CPU cannot be in these states immediately |
| 2742 | * after it has processed an INIT signal (and thus in |
| 2743 | * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs |
| 2744 | * and leave the INIT pending. |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2745 | */ |
Liran Alon | 27cbe7d | 2019-11-11 11:16:40 +0200 | [diff] [blame] | 2746 | if (kvm_vcpu_latch_init(vcpu)) { |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2747 | WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED); |
| 2748 | if (test_bit(KVM_APIC_SIPI, &apic->pending_events)) |
| 2749 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 2750 | return; |
| 2751 | } |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2752 | |
Paolo Bonzini | cd7764f | 2015-06-04 10:41:21 +0200 | [diff] [blame] | 2753 | pe = xchg(&apic->pending_events, 0); |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2754 | if (test_bit(KVM_APIC_INIT, &pe)) { |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 2755 | kvm_vcpu_reset(vcpu, true); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2756 | if (kvm_vcpu_is_bsp(apic->vcpu)) |
| 2757 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 2758 | else |
| 2759 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 2760 | } |
Gleb Natapov | 299018f | 2013-06-03 11:30:02 +0300 | [diff] [blame] | 2761 | if (test_bit(KVM_APIC_SIPI, &pe) && |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2762 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { |
| 2763 | /* evaluate pending_events before reading the vector */ |
| 2764 | smp_rmb(); |
| 2765 | sipi_vector = apic->sipi_vector; |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 2766 | kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector); |
| 2767 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 2768 | } |
| 2769 | } |
| 2770 | |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2771 | void kvm_lapic_init(void) |
| 2772 | { |
| 2773 | /* do not patch jump label more than once per second */ |
| 2774 | jump_label_rate_limit(&apic_hw_disabled, HZ); |
Gleb Natapov | f8c1ea1 | 2012-08-05 15:58:31 +0300 | [diff] [blame] | 2775 | jump_label_rate_limit(&apic_sw_disabled, HZ); |
Gleb Natapov | c5cc421 | 2012-08-05 15:58:30 +0300 | [diff] [blame] | 2776 | } |
David Matlack | cef84c3 | 2016-12-16 14:30:36 -0800 | [diff] [blame] | 2777 | |
| 2778 | void kvm_lapic_exit(void) |
| 2779 | { |
| 2780 | static_key_deferred_flush(&apic_hw_disabled); |
| 2781 | static_key_deferred_flush(&apic_sw_disabled); |
| 2782 | } |