Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1 | /* |
| 2 | * KVM Microsoft Hyper-V emulation |
| 3 | * |
| 4 | * derived from arch/x86/kvm/x86.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
| 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 10 | * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> |
| 11 | * |
| 12 | * Authors: |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * Amit Shah <amit.shah@qumranet.com> |
| 16 | * Ben-Ami Yassour <benami@il.ibm.com> |
| 17 | * Andrey Smetanin <asmetanin@virtuozzo.com> |
| 18 | * |
| 19 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 20 | * the COPYING file in the top-level directory. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "x86.h" |
| 25 | #include "lapic.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 26 | #include "ioapic.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 27 | #include "hyperv.h" |
| 28 | |
| 29 | #include <linux/kvm_host.h> |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 30 | #include <linux/highmem.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 31 | #include <linux/sched/cputime.h> |
| 32 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 33 | #include <asm/apicdef.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 34 | #include <trace/events/kvm.h> |
| 35 | |
| 36 | #include "trace.h" |
| 37 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 38 | static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) |
| 39 | { |
| 40 | return atomic64_read(&synic->sint[sint]); |
| 41 | } |
| 42 | |
| 43 | static inline int synic_get_sint_vector(u64 sint_value) |
| 44 | { |
| 45 | if (sint_value & HV_SYNIC_SINT_MASKED) |
| 46 | return -1; |
| 47 | return sint_value & HV_SYNIC_SINT_VECTOR_MASK; |
| 48 | } |
| 49 | |
| 50 | static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, |
| 51 | int vector) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 56 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, |
| 63 | int vector) |
| 64 | { |
| 65 | int i; |
| 66 | u64 sint_value; |
| 67 | |
| 68 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 69 | sint_value = synic_read_sint(synic, i); |
| 70 | if (synic_get_sint_vector(sint_value) == vector && |
| 71 | sint_value & HV_SYNIC_SINT_AUTO_EOI) |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
Andrey Smetanin | 7be58a6 | 2015-12-28 18:27:23 +0300 | [diff] [blame] | 77 | static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, |
| 78 | u64 data, bool host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 79 | { |
| 80 | int vector; |
| 81 | |
| 82 | vector = data & HV_SYNIC_SINT_VECTOR_MASK; |
Andrey Smetanin | 7be58a6 | 2015-12-28 18:27:23 +0300 | [diff] [blame] | 83 | if (vector < 16 && !host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 84 | return 1; |
| 85 | /* |
| 86 | * Guest may configure multiple SINTs to use the same vector, so |
| 87 | * we maintain a bitmap of vectors handled by synic, and a |
| 88 | * bitmap of vectors with auto-eoi behavior. The bitmaps are |
| 89 | * updated here, and atomically queried on fast paths. |
| 90 | */ |
| 91 | |
| 92 | atomic64_set(&synic->sint[sint], data); |
| 93 | |
| 94 | if (synic_has_vector_connected(synic, vector)) |
| 95 | __set_bit(vector, synic->vec_bitmap); |
| 96 | else |
| 97 | __clear_bit(vector, synic->vec_bitmap); |
| 98 | |
| 99 | if (synic_has_vector_auto_eoi(synic, vector)) |
| 100 | __set_bit(vector, synic->auto_eoi_bitmap); |
| 101 | else |
| 102 | __clear_bit(vector, synic->auto_eoi_bitmap); |
| 103 | |
| 104 | /* Load SynIC vectors into EOI exit bitmap */ |
| 105 | kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id) |
| 110 | { |
| 111 | struct kvm_vcpu *vcpu; |
| 112 | struct kvm_vcpu_hv_synic *synic; |
| 113 | |
| 114 | if (vcpu_id >= atomic_read(&kvm->online_vcpus)) |
| 115 | return NULL; |
| 116 | vcpu = kvm_get_vcpu(kvm, vcpu_id); |
| 117 | if (!vcpu) |
| 118 | return NULL; |
| 119 | synic = vcpu_to_synic(vcpu); |
| 120 | return (synic->active) ? synic : NULL; |
| 121 | } |
| 122 | |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 123 | static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic, |
| 124 | u32 sint) |
| 125 | { |
| 126 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 127 | struct page *page; |
| 128 | gpa_t gpa; |
| 129 | struct hv_message *msg; |
| 130 | struct hv_message_page *msg_page; |
| 131 | |
| 132 | gpa = synic->msg_page & PAGE_MASK; |
| 133 | page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); |
| 134 | if (is_error_page(page)) { |
| 135 | vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n", |
| 136 | gpa); |
| 137 | return; |
| 138 | } |
| 139 | msg_page = kmap_atomic(page); |
| 140 | |
| 141 | msg = &msg_page->sint_message[sint]; |
| 142 | msg->header.message_flags.msg_pending = 0; |
| 143 | |
| 144 | kunmap_atomic(msg_page); |
| 145 | kvm_release_page_dirty(page); |
| 146 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
| 147 | } |
| 148 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 149 | static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) |
| 150 | { |
| 151 | struct kvm *kvm = vcpu->kvm; |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 152 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 153 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 154 | struct kvm_vcpu_hv_stimer *stimer; |
| 155 | int gsi, idx, stimers_pending; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 156 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 157 | trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 158 | |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 159 | if (synic->msg_page & HV_SYNIC_SIMP_ENABLE) |
| 160 | synic_clear_sint_msg_pending(synic, sint); |
| 161 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 162 | /* Try to deliver pending Hyper-V SynIC timers messages */ |
| 163 | stimers_pending = 0; |
| 164 | for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { |
| 165 | stimer = &hv_vcpu->stimer[idx]; |
| 166 | if (stimer->msg_pending && |
| 167 | (stimer->config & HV_STIMER_ENABLE) && |
| 168 | HV_STIMER_SINT(stimer->config) == sint) { |
| 169 | set_bit(stimer->index, |
| 170 | hv_vcpu->stimer_pending_bitmap); |
| 171 | stimers_pending++; |
| 172 | } |
| 173 | } |
| 174 | if (stimers_pending) |
| 175 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 176 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 177 | idx = srcu_read_lock(&kvm->irq_srcu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 178 | gsi = atomic_read(&synic->sint_to_gsi[sint]); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 179 | if (gsi != -1) |
| 180 | kvm_notify_acked_gsi(kvm, gsi); |
| 181 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 182 | } |
| 183 | |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 184 | static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) |
| 185 | { |
| 186 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 187 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
| 188 | |
| 189 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; |
| 190 | hv_vcpu->exit.u.synic.msr = msr; |
| 191 | hv_vcpu->exit.u.synic.control = synic->control; |
| 192 | hv_vcpu->exit.u.synic.evt_page = synic->evt_page; |
| 193 | hv_vcpu->exit.u.synic.msg_page = synic->msg_page; |
| 194 | |
| 195 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 196 | } |
| 197 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 198 | static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, |
| 199 | u32 msr, u64 data, bool host) |
| 200 | { |
| 201 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 202 | int ret; |
| 203 | |
| 204 | if (!synic->active) |
| 205 | return 1; |
| 206 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 207 | trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); |
| 208 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 209 | ret = 0; |
| 210 | switch (msr) { |
| 211 | case HV_X64_MSR_SCONTROL: |
| 212 | synic->control = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 213 | if (!host) |
| 214 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 215 | break; |
| 216 | case HV_X64_MSR_SVERSION: |
| 217 | if (!host) { |
| 218 | ret = 1; |
| 219 | break; |
| 220 | } |
| 221 | synic->version = data; |
| 222 | break; |
| 223 | case HV_X64_MSR_SIEFP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame^] | 224 | if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && |
| 225 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 226 | if (kvm_clear_guest(vcpu->kvm, |
| 227 | data & PAGE_MASK, PAGE_SIZE)) { |
| 228 | ret = 1; |
| 229 | break; |
| 230 | } |
| 231 | synic->evt_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 232 | if (!host) |
| 233 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 234 | break; |
| 235 | case HV_X64_MSR_SIMP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame^] | 236 | if ((data & HV_SYNIC_SIMP_ENABLE) && !host && |
| 237 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 238 | if (kvm_clear_guest(vcpu->kvm, |
| 239 | data & PAGE_MASK, PAGE_SIZE)) { |
| 240 | ret = 1; |
| 241 | break; |
| 242 | } |
| 243 | synic->msg_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 244 | if (!host) |
| 245 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 246 | break; |
| 247 | case HV_X64_MSR_EOM: { |
| 248 | int i; |
| 249 | |
| 250 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 251 | kvm_hv_notify_acked_sint(vcpu, i); |
| 252 | break; |
| 253 | } |
| 254 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Andrey Smetanin | 7be58a6 | 2015-12-28 18:27:23 +0300 | [diff] [blame] | 255 | ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 256 | break; |
| 257 | default: |
| 258 | ret = 1; |
| 259 | break; |
| 260 | } |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata) |
| 265 | { |
| 266 | int ret; |
| 267 | |
| 268 | if (!synic->active) |
| 269 | return 1; |
| 270 | |
| 271 | ret = 0; |
| 272 | switch (msr) { |
| 273 | case HV_X64_MSR_SCONTROL: |
| 274 | *pdata = synic->control; |
| 275 | break; |
| 276 | case HV_X64_MSR_SVERSION: |
| 277 | *pdata = synic->version; |
| 278 | break; |
| 279 | case HV_X64_MSR_SIEFP: |
| 280 | *pdata = synic->evt_page; |
| 281 | break; |
| 282 | case HV_X64_MSR_SIMP: |
| 283 | *pdata = synic->msg_page; |
| 284 | break; |
| 285 | case HV_X64_MSR_EOM: |
| 286 | *pdata = 0; |
| 287 | break; |
| 288 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 289 | *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); |
| 290 | break; |
| 291 | default: |
| 292 | ret = 1; |
| 293 | break; |
| 294 | } |
| 295 | return ret; |
| 296 | } |
| 297 | |
Jiang Biao | ecd8a8c | 2016-11-07 08:56:33 +0800 | [diff] [blame] | 298 | static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 299 | { |
| 300 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 301 | struct kvm_lapic_irq irq; |
| 302 | int ret, vector; |
| 303 | |
| 304 | if (sint >= ARRAY_SIZE(synic->sint)) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | vector = synic_get_sint_vector(synic_read_sint(synic, sint)); |
| 308 | if (vector < 0) |
| 309 | return -ENOENT; |
| 310 | |
| 311 | memset(&irq, 0, sizeof(irq)); |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 312 | irq.shorthand = APIC_DEST_SELF; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 313 | irq.dest_mode = APIC_DEST_PHYSICAL; |
| 314 | irq.delivery_mode = APIC_DM_FIXED; |
| 315 | irq.vector = vector; |
| 316 | irq.level = 1; |
| 317 | |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 318 | ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL); |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 319 | trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint) |
| 324 | { |
| 325 | struct kvm_vcpu_hv_synic *synic; |
| 326 | |
| 327 | synic = synic_get(kvm, vcpu_id); |
| 328 | if (!synic) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | return synic_set_irq(synic, sint); |
| 332 | } |
| 333 | |
| 334 | void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) |
| 335 | { |
| 336 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 337 | int i; |
| 338 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 339 | trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 340 | |
| 341 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 342 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 343 | kvm_hv_notify_acked_sint(vcpu, i); |
| 344 | } |
| 345 | |
| 346 | static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi) |
| 347 | { |
| 348 | struct kvm_vcpu_hv_synic *synic; |
| 349 | |
| 350 | synic = synic_get(kvm, vcpu_id); |
| 351 | if (!synic) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) |
| 355 | return -EINVAL; |
| 356 | |
| 357 | atomic_set(&synic->sint_to_gsi[sint], gsi); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | void kvm_hv_irq_routing_update(struct kvm *kvm) |
| 362 | { |
| 363 | struct kvm_irq_routing_table *irq_rt; |
| 364 | struct kvm_kernel_irq_routing_entry *e; |
| 365 | u32 gsi; |
| 366 | |
| 367 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 368 | lockdep_is_held(&kvm->irq_lock)); |
| 369 | |
| 370 | for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { |
| 371 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 372 | if (e->type == KVM_IRQ_ROUTING_HV_SINT) |
| 373 | kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, |
| 374 | e->hv_sint.sint, gsi); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | static void synic_init(struct kvm_vcpu_hv_synic *synic) |
| 380 | { |
| 381 | int i; |
| 382 | |
| 383 | memset(synic, 0, sizeof(*synic)); |
| 384 | synic->version = HV_SYNIC_VERSION_1; |
| 385 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 386 | atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); |
| 387 | atomic_set(&synic->sint_to_gsi[i], -1); |
| 388 | } |
| 389 | } |
| 390 | |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 391 | static u64 get_time_ref_counter(struct kvm *kvm) |
| 392 | { |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 393 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 394 | struct kvm_vcpu *vcpu; |
| 395 | u64 tsc; |
| 396 | |
| 397 | /* |
| 398 | * The guest has not set up the TSC page or the clock isn't |
| 399 | * stable, fall back to get_kvmclock_ns. |
| 400 | */ |
| 401 | if (!hv->tsc_ref.tsc_sequence) |
| 402 | return div_u64(get_kvmclock_ns(kvm), 100); |
| 403 | |
| 404 | vcpu = kvm_get_vcpu(kvm, 0); |
| 405 | tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 406 | return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) |
| 407 | + hv->tsc_ref.tsc_offset; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 408 | } |
| 409 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 410 | static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 411 | bool vcpu_kick) |
| 412 | { |
| 413 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 414 | |
| 415 | set_bit(stimer->index, |
| 416 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 417 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 418 | if (vcpu_kick) |
| 419 | kvm_vcpu_kick(vcpu); |
| 420 | } |
| 421 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 422 | static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) |
| 423 | { |
| 424 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 425 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 426 | trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id, |
| 427 | stimer->index); |
| 428 | |
Andrey Smetanin | 019b978 | 2015-12-28 18:27:19 +0300 | [diff] [blame] | 429 | hrtimer_cancel(&stimer->timer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 430 | clear_bit(stimer->index, |
| 431 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 432 | stimer->msg_pending = false; |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 433 | stimer->exp_time = 0; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) |
| 437 | { |
| 438 | struct kvm_vcpu_hv_stimer *stimer; |
| 439 | |
| 440 | stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 441 | trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id, |
| 442 | stimer->index); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 443 | stimer_mark_pending(stimer, true); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 444 | |
| 445 | return HRTIMER_NORESTART; |
| 446 | } |
| 447 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 448 | /* |
| 449 | * stimer_start() assumptions: |
| 450 | * a) stimer->count is not equal to 0 |
| 451 | * b) stimer->config has HV_STIMER_ENABLE flag |
| 452 | */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 453 | static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) |
| 454 | { |
| 455 | u64 time_now; |
| 456 | ktime_t ktime_now; |
| 457 | |
| 458 | time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); |
| 459 | ktime_now = ktime_get(); |
| 460 | |
| 461 | if (stimer->config & HV_STIMER_PERIODIC) { |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 462 | if (stimer->exp_time) { |
| 463 | if (time_now >= stimer->exp_time) { |
| 464 | u64 remainder; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 465 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 466 | div64_u64_rem(time_now - stimer->exp_time, |
| 467 | stimer->count, &remainder); |
| 468 | stimer->exp_time = |
| 469 | time_now + (stimer->count - remainder); |
| 470 | } |
| 471 | } else |
| 472 | stimer->exp_time = time_now + stimer->count; |
| 473 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 474 | trace_kvm_hv_stimer_start_periodic( |
| 475 | stimer_to_vcpu(stimer)->vcpu_id, |
| 476 | stimer->index, |
| 477 | time_now, stimer->exp_time); |
| 478 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 479 | hrtimer_start(&stimer->timer, |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 480 | ktime_add_ns(ktime_now, |
| 481 | 100 * (stimer->exp_time - time_now)), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 482 | HRTIMER_MODE_ABS); |
| 483 | return 0; |
| 484 | } |
| 485 | stimer->exp_time = stimer->count; |
| 486 | if (time_now >= stimer->count) { |
| 487 | /* |
| 488 | * Expire timer according to Hypervisor Top-Level Functional |
| 489 | * specification v4(15.3.1): |
| 490 | * "If a one shot is enabled and the specified count is in |
| 491 | * the past, it will expire immediately." |
| 492 | */ |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 493 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 497 | trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id, |
| 498 | stimer->index, |
| 499 | time_now, stimer->count); |
| 500 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 501 | hrtimer_start(&stimer->timer, |
| 502 | ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), |
| 503 | HRTIMER_MODE_ABS); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, |
| 508 | bool host) |
| 509 | { |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 510 | trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id, |
| 511 | stimer->index, config, host); |
| 512 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 513 | stimer_cleanup(stimer); |
Andrey Smetanin | 23a3b20 | 2015-12-28 18:27:22 +0300 | [diff] [blame] | 514 | if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 515 | config &= ~HV_STIMER_ENABLE; |
| 516 | stimer->config = config; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 517 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, |
| 522 | bool host) |
| 523 | { |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 524 | trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id, |
| 525 | stimer->index, count, host); |
| 526 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 527 | stimer_cleanup(stimer); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 528 | stimer->count = count; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 529 | if (stimer->count == 0) |
| 530 | stimer->config &= ~HV_STIMER_ENABLE; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 531 | else if (stimer->config & HV_STIMER_AUTOENABLE) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 532 | stimer->config |= HV_STIMER_ENABLE; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 533 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) |
| 538 | { |
| 539 | *pconfig = stimer->config; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) |
| 544 | { |
| 545 | *pcount = stimer->count; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, |
| 550 | struct hv_message *src_msg) |
| 551 | { |
| 552 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 553 | struct page *page; |
| 554 | gpa_t gpa; |
| 555 | struct hv_message *dst_msg; |
| 556 | int r; |
| 557 | struct hv_message_page *msg_page; |
| 558 | |
| 559 | if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) |
| 560 | return -ENOENT; |
| 561 | |
| 562 | gpa = synic->msg_page & PAGE_MASK; |
| 563 | page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); |
| 564 | if (is_error_page(page)) |
| 565 | return -EFAULT; |
| 566 | |
| 567 | msg_page = kmap_atomic(page); |
| 568 | dst_msg = &msg_page->sint_message[sint]; |
| 569 | if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE, |
| 570 | src_msg->header.message_type) != HVMSG_NONE) { |
| 571 | dst_msg->header.message_flags.msg_pending = 1; |
| 572 | r = -EAGAIN; |
| 573 | } else { |
| 574 | memcpy(&dst_msg->u.payload, &src_msg->u.payload, |
| 575 | src_msg->header.payload_size); |
| 576 | dst_msg->header.message_type = src_msg->header.message_type; |
| 577 | dst_msg->header.payload_size = src_msg->header.payload_size; |
| 578 | r = synic_set_irq(synic, sint); |
| 579 | if (r >= 1) |
| 580 | r = 0; |
| 581 | else if (r == 0) |
| 582 | r = -EFAULT; |
| 583 | } |
| 584 | kunmap_atomic(msg_page); |
| 585 | kvm_release_page_dirty(page); |
| 586 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
| 587 | return r; |
| 588 | } |
| 589 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 590 | static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 591 | { |
| 592 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 593 | struct hv_message *msg = &stimer->msg; |
| 594 | struct hv_timer_message_payload *payload = |
| 595 | (struct hv_timer_message_payload *)&msg->u.payload; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 596 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 597 | payload->expiration_time = stimer->exp_time; |
| 598 | payload->delivery_time = get_time_ref_counter(vcpu->kvm); |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 599 | return synic_deliver_msg(vcpu_to_synic(vcpu), |
| 600 | HV_STIMER_SINT(stimer->config), msg); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) |
| 604 | { |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 605 | int r; |
| 606 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 607 | stimer->msg_pending = true; |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 608 | r = stimer_send_msg(stimer); |
| 609 | trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id, |
| 610 | stimer->index, r); |
| 611 | if (!r) { |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 612 | stimer->msg_pending = false; |
| 613 | if (!(stimer->config & HV_STIMER_PERIODIC)) |
| 614 | stimer->config &= ~HV_STIMER_ENABLE; |
| 615 | } |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) |
| 619 | { |
| 620 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 621 | struct kvm_vcpu_hv_stimer *stimer; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 622 | u64 time_now, exp_time; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 623 | int i; |
| 624 | |
| 625 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 626 | if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { |
| 627 | stimer = &hv_vcpu->stimer[i]; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 628 | if (stimer->config & HV_STIMER_ENABLE) { |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 629 | exp_time = stimer->exp_time; |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 630 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 631 | if (exp_time) { |
| 632 | time_now = |
| 633 | get_time_ref_counter(vcpu->kvm); |
| 634 | if (time_now >= exp_time) |
| 635 | stimer_expiration(stimer); |
| 636 | } |
| 637 | |
| 638 | if ((stimer->config & HV_STIMER_ENABLE) && |
| 639 | stimer->count) |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 640 | stimer_start(stimer); |
| 641 | else |
| 642 | stimer_cleanup(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 648 | { |
| 649 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 650 | int i; |
| 651 | |
| 652 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 653 | stimer_cleanup(&hv_vcpu->stimer[i]); |
| 654 | } |
| 655 | |
| 656 | static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) |
| 657 | { |
| 658 | struct hv_message *msg = &stimer->msg; |
| 659 | struct hv_timer_message_payload *payload = |
| 660 | (struct hv_timer_message_payload *)&msg->u.payload; |
| 661 | |
| 662 | memset(&msg->header, 0, sizeof(msg->header)); |
| 663 | msg->header.message_type = HVMSG_TIMER_EXPIRED; |
| 664 | msg->header.payload_size = sizeof(*payload); |
| 665 | |
| 666 | payload->timer_index = stimer->index; |
| 667 | payload->expiration_time = 0; |
| 668 | payload->delivery_time = 0; |
| 669 | } |
| 670 | |
| 671 | static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) |
| 672 | { |
| 673 | memset(stimer, 0, sizeof(*stimer)); |
| 674 | stimer->index = timer_index; |
| 675 | hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 676 | stimer->timer.function = stimer_timer_callback; |
| 677 | stimer_prepare_msg(stimer); |
| 678 | } |
| 679 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 680 | void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) |
| 681 | { |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 682 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 683 | int i; |
| 684 | |
| 685 | synic_init(&hv_vcpu->synic); |
| 686 | |
| 687 | bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); |
| 688 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 689 | stimer_init(&hv_vcpu->stimer[i], i); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 690 | } |
| 691 | |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame^] | 692 | int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 693 | { |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame^] | 694 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 695 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 696 | /* |
| 697 | * Hyper-V SynIC auto EOI SINT's are |
| 698 | * not compatible with APICV, so deactivate APICV |
| 699 | */ |
| 700 | kvm_vcpu_deactivate_apicv(vcpu); |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame^] | 701 | synic->active = true; |
| 702 | synic->dont_zero_synic_pages = dont_zero_synic_pages; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 706 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 707 | { |
| 708 | bool r = false; |
| 709 | |
| 710 | switch (msr) { |
| 711 | case HV_X64_MSR_GUEST_OS_ID: |
| 712 | case HV_X64_MSR_HYPERCALL: |
| 713 | case HV_X64_MSR_REFERENCE_TSC: |
| 714 | case HV_X64_MSR_TIME_REF_COUNT: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 715 | case HV_X64_MSR_CRASH_CTL: |
| 716 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 717 | case HV_X64_MSR_RESET: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 718 | r = true; |
| 719 | break; |
| 720 | } |
| 721 | |
| 722 | return r; |
| 723 | } |
| 724 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 725 | static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, |
| 726 | u32 index, u64 *pdata) |
| 727 | { |
| 728 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 729 | |
| 730 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 731 | return -EINVAL; |
| 732 | |
| 733 | *pdata = hv->hv_crash_param[index]; |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) |
| 738 | { |
| 739 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 740 | |
| 741 | *pdata = hv->hv_crash_ctl; |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) |
| 746 | { |
| 747 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 748 | |
| 749 | if (host) |
| 750 | hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY; |
| 751 | |
| 752 | if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) { |
| 753 | |
| 754 | vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", |
| 755 | hv->hv_crash_param[0], |
| 756 | hv->hv_crash_param[1], |
| 757 | hv->hv_crash_param[2], |
| 758 | hv->hv_crash_param[3], |
| 759 | hv->hv_crash_param[4]); |
| 760 | |
| 761 | /* Send notification about crash to user space */ |
| 762 | kvm_make_request(KVM_REQ_HV_CRASH, vcpu); |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, |
| 769 | u32 index, u64 data) |
| 770 | { |
| 771 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 772 | |
| 773 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | hv->hv_crash_param[index] = data; |
| 777 | return 0; |
| 778 | } |
| 779 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 780 | /* |
| 781 | * The kvmclock and Hyper-V TSC page use similar formulas, and converting |
| 782 | * between them is possible: |
| 783 | * |
| 784 | * kvmclock formula: |
| 785 | * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) |
| 786 | * + system_time |
| 787 | * |
| 788 | * Hyper-V formula: |
| 789 | * nsec/100 = ticks * scale / 2^64 + offset |
| 790 | * |
| 791 | * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. |
| 792 | * By dividing the kvmclock formula by 100 and equating what's left we get: |
| 793 | * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 794 | * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 795 | * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 |
| 796 | * |
| 797 | * Now expand the kvmclock formula and divide by 100: |
| 798 | * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) |
| 799 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) |
| 800 | * + system_time |
| 801 | * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 802 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 803 | * + system_time / 100 |
| 804 | * |
| 805 | * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: |
| 806 | * nsec/100 = ticks * scale / 2^64 |
| 807 | * - tsc_timestamp * scale / 2^64 |
| 808 | * + system_time / 100 |
| 809 | * |
| 810 | * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: |
| 811 | * offset = system_time / 100 - tsc_timestamp * scale / 2^64 |
| 812 | * |
| 813 | * These two equivalencies are implemented in this function. |
| 814 | */ |
| 815 | static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, |
| 816 | HV_REFERENCE_TSC_PAGE *tsc_ref) |
| 817 | { |
| 818 | u64 max_mul; |
| 819 | |
| 820 | if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) |
| 821 | return false; |
| 822 | |
| 823 | /* |
| 824 | * check if scale would overflow, if so we use the time ref counter |
| 825 | * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 |
| 826 | * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) |
| 827 | * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) |
| 828 | */ |
| 829 | max_mul = 100ull << (32 - hv_clock->tsc_shift); |
| 830 | if (hv_clock->tsc_to_system_mul >= max_mul) |
| 831 | return false; |
| 832 | |
| 833 | /* |
| 834 | * Otherwise compute the scale and offset according to the formulas |
| 835 | * derived above. |
| 836 | */ |
| 837 | tsc_ref->tsc_scale = |
| 838 | mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), |
| 839 | hv_clock->tsc_to_system_mul, |
| 840 | 100); |
| 841 | |
| 842 | tsc_ref->tsc_offset = hv_clock->system_time; |
| 843 | do_div(tsc_ref->tsc_offset, 100); |
| 844 | tsc_ref->tsc_offset -= |
| 845 | mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | void kvm_hv_setup_tsc_page(struct kvm *kvm, |
| 850 | struct pvclock_vcpu_time_info *hv_clock) |
| 851 | { |
| 852 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 853 | u32 tsc_seq; |
| 854 | u64 gfn; |
| 855 | |
| 856 | BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); |
| 857 | BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0); |
| 858 | |
| 859 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 860 | return; |
| 861 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 862 | mutex_lock(&kvm->arch.hyperv.hv_lock); |
| 863 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 864 | goto out_unlock; |
| 865 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 866 | gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 867 | /* |
| 868 | * Because the TSC parameters only vary when there is a |
| 869 | * change in the master clock, do not bother with caching. |
| 870 | */ |
| 871 | if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), |
| 872 | &tsc_seq, sizeof(tsc_seq)))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 873 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 874 | |
| 875 | /* |
| 876 | * While we're computing and writing the parameters, force the |
| 877 | * guest to use the time reference count MSR. |
| 878 | */ |
| 879 | hv->tsc_ref.tsc_sequence = 0; |
| 880 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 881 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 882 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 883 | |
| 884 | if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 885 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 886 | |
| 887 | /* Ensure sequence is zero before writing the rest of the struct. */ |
| 888 | smp_wmb(); |
| 889 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 890 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 891 | |
| 892 | /* |
| 893 | * Now switch to the TSC page mechanism by writing the sequence. |
| 894 | */ |
| 895 | tsc_seq++; |
| 896 | if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) |
| 897 | tsc_seq = 1; |
| 898 | |
| 899 | /* Write the struct entirely before the non-zero sequence. */ |
| 900 | smp_wmb(); |
| 901 | |
| 902 | hv->tsc_ref.tsc_sequence = tsc_seq; |
| 903 | kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 904 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 905 | out_unlock: |
| 906 | mutex_unlock(&kvm->arch.hyperv.hv_lock); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 909 | static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, |
| 910 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 911 | { |
| 912 | struct kvm *kvm = vcpu->kvm; |
| 913 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 914 | |
| 915 | switch (msr) { |
| 916 | case HV_X64_MSR_GUEST_OS_ID: |
| 917 | hv->hv_guest_os_id = data; |
| 918 | /* setting guest os id to zero disables hypercall page */ |
| 919 | if (!hv->hv_guest_os_id) |
| 920 | hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 921 | break; |
| 922 | case HV_X64_MSR_HYPERCALL: { |
| 923 | u64 gfn; |
| 924 | unsigned long addr; |
| 925 | u8 instructions[4]; |
| 926 | |
| 927 | /* if guest os id is not set hypercall should remain disabled */ |
| 928 | if (!hv->hv_guest_os_id) |
| 929 | break; |
| 930 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 931 | hv->hv_hypercall = data; |
| 932 | break; |
| 933 | } |
| 934 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 935 | addr = gfn_to_hva(kvm, gfn); |
| 936 | if (kvm_is_error_hva(addr)) |
| 937 | return 1; |
| 938 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 939 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 940 | if (__copy_to_user((void __user *)addr, instructions, 4)) |
| 941 | return 1; |
| 942 | hv->hv_hypercall = data; |
| 943 | mark_page_dirty(kvm, gfn); |
| 944 | break; |
| 945 | } |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 946 | case HV_X64_MSR_REFERENCE_TSC: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 947 | hv->hv_tsc_page = data; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 948 | if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) |
| 949 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 950 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 951 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 952 | return kvm_hv_msr_set_crash_data(vcpu, |
| 953 | msr - HV_X64_MSR_CRASH_P0, |
| 954 | data); |
| 955 | case HV_X64_MSR_CRASH_CTL: |
| 956 | return kvm_hv_msr_set_crash_ctl(vcpu, data, host); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 957 | case HV_X64_MSR_RESET: |
| 958 | if (data == 1) { |
| 959 | vcpu_debug(vcpu, "hyper-v reset requested\n"); |
| 960 | kvm_make_request(KVM_REQ_HV_RESET, vcpu); |
| 961 | } |
| 962 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 963 | default: |
| 964 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 965 | msr, data); |
| 966 | return 1; |
| 967 | } |
| 968 | return 0; |
| 969 | } |
| 970 | |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 971 | /* Calculate cpu time spent by current task in 100ns units */ |
| 972 | static u64 current_task_runtime_100ns(void) |
| 973 | { |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 974 | u64 utime, stime; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 975 | |
| 976 | task_cputime_adjusted(current, &utime, &stime); |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 977 | |
| 978 | return div_u64(utime + stime, 100); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 982 | { |
| 983 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 984 | |
| 985 | switch (msr) { |
| 986 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 987 | u64 gfn; |
| 988 | unsigned long addr; |
| 989 | |
| 990 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 991 | hv->hv_vapic = data; |
| 992 | if (kvm_lapic_enable_pv_eoi(vcpu, 0)) |
| 993 | return 1; |
| 994 | break; |
| 995 | } |
| 996 | gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT; |
| 997 | addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); |
| 998 | if (kvm_is_error_hva(addr)) |
| 999 | return 1; |
| 1000 | if (__clear_user((void __user *)addr, PAGE_SIZE)) |
| 1001 | return 1; |
| 1002 | hv->hv_vapic = data; |
| 1003 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 1004 | if (kvm_lapic_enable_pv_eoi(vcpu, |
| 1005 | gfn_to_gpa(gfn) | KVM_MSR_ENABLED)) |
| 1006 | return 1; |
| 1007 | break; |
| 1008 | } |
| 1009 | case HV_X64_MSR_EOI: |
| 1010 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1011 | case HV_X64_MSR_ICR: |
| 1012 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1013 | case HV_X64_MSR_TPR: |
| 1014 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1015 | case HV_X64_MSR_VP_RUNTIME: |
| 1016 | if (!host) |
| 1017 | return 1; |
| 1018 | hv->runtime_offset = data - current_task_runtime_100ns(); |
| 1019 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1020 | case HV_X64_MSR_SCONTROL: |
| 1021 | case HV_X64_MSR_SVERSION: |
| 1022 | case HV_X64_MSR_SIEFP: |
| 1023 | case HV_X64_MSR_SIMP: |
| 1024 | case HV_X64_MSR_EOM: |
| 1025 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 1026 | return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1027 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1028 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1029 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1030 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1031 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1032 | |
| 1033 | return stimer_set_config(vcpu_to_stimer(vcpu, timer_index), |
| 1034 | data, host); |
| 1035 | } |
| 1036 | case HV_X64_MSR_STIMER0_COUNT: |
| 1037 | case HV_X64_MSR_STIMER1_COUNT: |
| 1038 | case HV_X64_MSR_STIMER2_COUNT: |
| 1039 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1040 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1041 | |
| 1042 | return stimer_set_count(vcpu_to_stimer(vcpu, timer_index), |
| 1043 | data, host); |
| 1044 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1045 | default: |
| 1046 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 1047 | msr, data); |
| 1048 | return 1; |
| 1049 | } |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1055 | { |
| 1056 | u64 data = 0; |
| 1057 | struct kvm *kvm = vcpu->kvm; |
| 1058 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 1059 | |
| 1060 | switch (msr) { |
| 1061 | case HV_X64_MSR_GUEST_OS_ID: |
| 1062 | data = hv->hv_guest_os_id; |
| 1063 | break; |
| 1064 | case HV_X64_MSR_HYPERCALL: |
| 1065 | data = hv->hv_hypercall; |
| 1066 | break; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 1067 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1068 | data = get_time_ref_counter(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1069 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1070 | case HV_X64_MSR_REFERENCE_TSC: |
| 1071 | data = hv->hv_tsc_page; |
| 1072 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1073 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 1074 | return kvm_hv_msr_get_crash_data(vcpu, |
| 1075 | msr - HV_X64_MSR_CRASH_P0, |
| 1076 | pdata); |
| 1077 | case HV_X64_MSR_CRASH_CTL: |
| 1078 | return kvm_hv_msr_get_crash_ctl(vcpu, pdata); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1079 | case HV_X64_MSR_RESET: |
| 1080 | data = 0; |
| 1081 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1082 | default: |
| 1083 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1084 | return 1; |
| 1085 | } |
| 1086 | |
| 1087 | *pdata = data; |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1092 | { |
| 1093 | u64 data = 0; |
| 1094 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 1095 | |
| 1096 | switch (msr) { |
| 1097 | case HV_X64_MSR_VP_INDEX: { |
| 1098 | int r; |
| 1099 | struct kvm_vcpu *v; |
| 1100 | |
| 1101 | kvm_for_each_vcpu(r, v, vcpu->kvm) { |
| 1102 | if (v == vcpu) { |
| 1103 | data = r; |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | break; |
| 1108 | } |
| 1109 | case HV_X64_MSR_EOI: |
| 1110 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1111 | case HV_X64_MSR_ICR: |
| 1112 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1113 | case HV_X64_MSR_TPR: |
| 1114 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
| 1115 | case HV_X64_MSR_APIC_ASSIST_PAGE: |
| 1116 | data = hv->hv_vapic; |
| 1117 | break; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1118 | case HV_X64_MSR_VP_RUNTIME: |
| 1119 | data = current_task_runtime_100ns() + hv->runtime_offset; |
| 1120 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1121 | case HV_X64_MSR_SCONTROL: |
| 1122 | case HV_X64_MSR_SVERSION: |
| 1123 | case HV_X64_MSR_SIEFP: |
| 1124 | case HV_X64_MSR_SIMP: |
| 1125 | case HV_X64_MSR_EOM: |
| 1126 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 1127 | return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1128 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1129 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1130 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1131 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1132 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1133 | |
| 1134 | return stimer_get_config(vcpu_to_stimer(vcpu, timer_index), |
| 1135 | pdata); |
| 1136 | } |
| 1137 | case HV_X64_MSR_STIMER0_COUNT: |
| 1138 | case HV_X64_MSR_STIMER1_COUNT: |
| 1139 | case HV_X64_MSR_STIMER2_COUNT: |
| 1140 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1141 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1142 | |
| 1143 | return stimer_get_count(vcpu_to_stimer(vcpu, timer_index), |
| 1144 | pdata); |
| 1145 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1146 | default: |
| 1147 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1148 | return 1; |
| 1149 | } |
| 1150 | *pdata = data; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1154 | int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1155 | { |
| 1156 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1157 | int r; |
| 1158 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1159 | mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1160 | r = kvm_hv_set_msr_pw(vcpu, msr, data, host); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1161 | mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1162 | return r; |
| 1163 | } else |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1164 | return kvm_hv_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1168 | { |
| 1169 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1170 | int r; |
| 1171 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1172 | mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1173 | r = kvm_hv_get_msr_pw(vcpu, msr, pdata); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1174 | mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1175 | return r; |
| 1176 | } else |
| 1177 | return kvm_hv_get_msr(vcpu, msr, pdata); |
| 1178 | } |
| 1179 | |
| 1180 | bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 1181 | { |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1182 | return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1183 | } |
| 1184 | |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1185 | static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) |
| 1186 | { |
| 1187 | bool longmode; |
| 1188 | |
| 1189 | longmode = is_64_bit_mode(vcpu); |
| 1190 | if (longmode) |
| 1191 | kvm_register_write(vcpu, VCPU_REGS_RAX, result); |
| 1192 | else { |
| 1193 | kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32); |
| 1194 | kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) |
| 1199 | { |
| 1200 | struct kvm_run *run = vcpu->run; |
| 1201 | |
| 1202 | kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result); |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1206 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 1207 | { |
| 1208 | u64 param, ingpa, outgpa, ret; |
| 1209 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 1210 | bool fast, longmode; |
| 1211 | |
| 1212 | /* |
| 1213 | * hypercall generates UD from non zero cpl and real mode |
| 1214 | * per HYPER-V spec |
| 1215 | */ |
| 1216 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
| 1217 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 1218 | return 1; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | longmode = is_64_bit_mode(vcpu); |
| 1222 | |
| 1223 | if (!longmode) { |
| 1224 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 1225 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 1226 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 1227 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 1228 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 1229 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
| 1230 | } |
| 1231 | #ifdef CONFIG_X86_64 |
| 1232 | else { |
| 1233 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 1234 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 1235 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 1236 | } |
| 1237 | #endif |
| 1238 | |
| 1239 | code = param & 0xffff; |
| 1240 | fast = (param >> 16) & 0x1; |
| 1241 | rep_cnt = (param >> 32) & 0xfff; |
| 1242 | rep_idx = (param >> 48) & 0xfff; |
| 1243 | |
| 1244 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 1245 | |
Andrey Smetanin | b2fdc25 | 2016-02-11 16:45:00 +0300 | [diff] [blame] | 1246 | /* Hypercall continuation is not supported yet */ |
| 1247 | if (rep_cnt || rep_idx) { |
| 1248 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 1249 | goto set_result; |
| 1250 | } |
| 1251 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1252 | switch (code) { |
Andrey Smetanin | 8ed6d76 | 2016-02-11 16:44:57 +0300 | [diff] [blame] | 1253 | case HVCALL_NOTIFY_LONG_SPIN_WAIT: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1254 | kvm_vcpu_on_spin(vcpu); |
| 1255 | break; |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1256 | case HVCALL_POST_MESSAGE: |
| 1257 | case HVCALL_SIGNAL_EVENT: |
Paolo Bonzini | a2b5c3c | 2016-03-29 11:23:25 +0200 | [diff] [blame] | 1258 | /* don't bother userspace if it has no way to handle it */ |
| 1259 | if (!vcpu_to_synic(vcpu)->active) { |
| 1260 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 1261 | break; |
| 1262 | } |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1263 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 1264 | vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; |
| 1265 | vcpu->run->hyperv.u.hcall.input = param; |
| 1266 | vcpu->run->hyperv.u.hcall.params[0] = ingpa; |
| 1267 | vcpu->run->hyperv.u.hcall.params[1] = outgpa; |
| 1268 | vcpu->arch.complete_userspace_io = |
| 1269 | kvm_hv_hypercall_complete_userspace; |
| 1270 | return 0; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1271 | default: |
| 1272 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 1273 | break; |
| 1274 | } |
| 1275 | |
Andrey Smetanin | b2fdc25 | 2016-02-11 16:45:00 +0300 | [diff] [blame] | 1276 | set_result: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1277 | ret = res | (((u64)rep_done & 0xfff) << 32); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1278 | kvm_hv_hypercall_set_result(vcpu, ret); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1279 | return 1; |
| 1280 | } |