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