1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * KVM Microsoft Hyper-V emulation 4 * 5 * derived from arch/x86/kvm/x86.c 6 * 7 * Copyright (C) 2006 Qumranet, Inc. 8 * Copyright (C) 2008 Qumranet, Inc. 9 * Copyright IBM Corporation, 2008 10 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 11 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> 12 * 13 * Authors: 14 * Avi Kivity <avi@qumranet.com> 15 * Yaniv Kamay <yaniv@qumranet.com> 16 * Amit Shah <amit.shah@qumranet.com> 17 * Ben-Ami Yassour <benami@il.ibm.com> 18 * Andrey Smetanin <asmetanin@virtuozzo.com> 19 */ 20 21 #include "x86.h" 22 #include "lapic.h" 23 #include "ioapic.h" 24 #include "cpuid.h" 25 #include "hyperv.h" 26 #include "xen.h" 27 28 #include <linux/cpu.h> 29 #include <linux/kvm_host.h> 30 #include <linux/highmem.h> 31 #include <linux/sched/cputime.h> 32 #include <linux/eventfd.h> 33 34 #include <asm/apicdef.h> 35 #include <trace/events/kvm.h> 36 37 #include "trace.h" 38 #include "irq.h" 39 40 /* "Hv#1" signature */ 41 #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 42 43 #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) 44 45 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, 46 bool vcpu_kick); 47 48 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) 49 { 50 return atomic64_read(&synic->sint[sint]); 51 } 52 53 static inline int synic_get_sint_vector(u64 sint_value) 54 { 55 if (sint_value & HV_SYNIC_SINT_MASKED) 56 return -1; 57 return sint_value & HV_SYNIC_SINT_VECTOR_MASK; 58 } 59 60 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, 61 int vector) 62 { 63 int i; 64 65 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 66 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 67 return true; 68 } 69 return false; 70 } 71 72 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, 73 int vector) 74 { 75 int i; 76 u64 sint_value; 77 78 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 79 sint_value = synic_read_sint(synic, i); 80 if (synic_get_sint_vector(sint_value) == vector && 81 sint_value & HV_SYNIC_SINT_AUTO_EOI) 82 return true; 83 } 84 return false; 85 } 86 87 static void synic_update_vector(struct kvm_vcpu_hv_synic *synic, 88 int vector) 89 { 90 if (vector < HV_SYNIC_FIRST_VALID_VECTOR) 91 return; 92 93 if (synic_has_vector_connected(synic, vector)) 94 __set_bit(vector, synic->vec_bitmap); 95 else 96 __clear_bit(vector, synic->vec_bitmap); 97 98 if (synic_has_vector_auto_eoi(synic, vector)) 99 __set_bit(vector, synic->auto_eoi_bitmap); 100 else 101 __clear_bit(vector, synic->auto_eoi_bitmap); 102 } 103 104 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, 105 u64 data, bool host) 106 { 107 int vector, old_vector; 108 bool masked; 109 110 vector = data & HV_SYNIC_SINT_VECTOR_MASK; 111 masked = data & HV_SYNIC_SINT_MASKED; 112 113 /* 114 * Valid vectors are 16-255, however, nested Hyper-V attempts to write 115 * default '0x10000' value on boot and this should not #GP. We need to 116 * allow zero-initing the register from host as well. 117 */ 118 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) 119 return 1; 120 /* 121 * Guest may configure multiple SINTs to use the same vector, so 122 * we maintain a bitmap of vectors handled by synic, and a 123 * bitmap of vectors with auto-eoi behavior. The bitmaps are 124 * updated here, and atomically queried on fast paths. 125 */ 126 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK; 127 128 atomic64_set(&synic->sint[sint], data); 129 130 synic_update_vector(synic, old_vector); 131 132 synic_update_vector(synic, vector); 133 134 /* Load SynIC vectors into EOI exit bitmap */ 135 kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic)); 136 return 0; 137 } 138 139 static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) 140 { 141 struct kvm_vcpu *vcpu = NULL; 142 int i; 143 144 if (vpidx >= KVM_MAX_VCPUS) 145 return NULL; 146 147 vcpu = kvm_get_vcpu(kvm, vpidx); 148 if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx) 149 return vcpu; 150 kvm_for_each_vcpu(i, vcpu, kvm) 151 if (kvm_hv_get_vpindex(vcpu) == vpidx) 152 return vcpu; 153 return NULL; 154 } 155 156 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx) 157 { 158 struct kvm_vcpu *vcpu; 159 struct kvm_vcpu_hv_synic *synic; 160 161 vcpu = get_vcpu_by_vpidx(kvm, vpidx); 162 if (!vcpu || !to_hv_vcpu(vcpu)) 163 return NULL; 164 synic = to_hv_synic(vcpu); 165 return (synic->active) ? synic : NULL; 166 } 167 168 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) 169 { 170 struct kvm *kvm = vcpu->kvm; 171 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 172 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 173 struct kvm_vcpu_hv_stimer *stimer; 174 int gsi, idx; 175 176 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); 177 178 /* Try to deliver pending Hyper-V SynIC timers messages */ 179 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { 180 stimer = &hv_vcpu->stimer[idx]; 181 if (stimer->msg_pending && stimer->config.enable && 182 !stimer->config.direct_mode && 183 stimer->config.sintx == sint) 184 stimer_mark_pending(stimer, false); 185 } 186 187 idx = srcu_read_lock(&kvm->irq_srcu); 188 gsi = atomic_read(&synic->sint_to_gsi[sint]); 189 if (gsi != -1) 190 kvm_notify_acked_gsi(kvm, gsi); 191 srcu_read_unlock(&kvm->irq_srcu, idx); 192 } 193 194 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) 195 { 196 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 197 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 198 199 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; 200 hv_vcpu->exit.u.synic.msr = msr; 201 hv_vcpu->exit.u.synic.control = synic->control; 202 hv_vcpu->exit.u.synic.evt_page = synic->evt_page; 203 hv_vcpu->exit.u.synic.msg_page = synic->msg_page; 204 205 kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 206 } 207 208 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, 209 u32 msr, u64 data, bool host) 210 { 211 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 212 int ret; 213 214 if (!synic->active && !host) 215 return 1; 216 217 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); 218 219 ret = 0; 220 switch (msr) { 221 case HV_X64_MSR_SCONTROL: 222 synic->control = data; 223 if (!host) 224 synic_exit(synic, msr); 225 break; 226 case HV_X64_MSR_SVERSION: 227 if (!host) { 228 ret = 1; 229 break; 230 } 231 synic->version = data; 232 break; 233 case HV_X64_MSR_SIEFP: 234 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && 235 !synic->dont_zero_synic_pages) 236 if (kvm_clear_guest(vcpu->kvm, 237 data & PAGE_MASK, PAGE_SIZE)) { 238 ret = 1; 239 break; 240 } 241 synic->evt_page = data; 242 if (!host) 243 synic_exit(synic, msr); 244 break; 245 case HV_X64_MSR_SIMP: 246 if ((data & HV_SYNIC_SIMP_ENABLE) && !host && 247 !synic->dont_zero_synic_pages) 248 if (kvm_clear_guest(vcpu->kvm, 249 data & PAGE_MASK, PAGE_SIZE)) { 250 ret = 1; 251 break; 252 } 253 synic->msg_page = data; 254 if (!host) 255 synic_exit(synic, msr); 256 break; 257 case HV_X64_MSR_EOM: { 258 int i; 259 260 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 261 kvm_hv_notify_acked_sint(vcpu, i); 262 break; 263 } 264 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 265 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); 266 break; 267 default: 268 ret = 1; 269 break; 270 } 271 return ret; 272 } 273 274 static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu) 275 { 276 struct kvm_cpuid_entry2 *entry; 277 278 entry = kvm_find_cpuid_entry(vcpu, 279 HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 280 0); 281 if (!entry) 282 return false; 283 284 return entry->eax & HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; 285 } 286 287 static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu) 288 { 289 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 290 291 if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL) 292 hv->hv_syndbg.control.status = 293 vcpu->run->hyperv.u.syndbg.status; 294 return 1; 295 } 296 297 static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr) 298 { 299 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 300 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 301 302 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG; 303 hv_vcpu->exit.u.syndbg.msr = msr; 304 hv_vcpu->exit.u.syndbg.control = syndbg->control.control; 305 hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page; 306 hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page; 307 hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page; 308 vcpu->arch.complete_userspace_io = 309 kvm_hv_syndbg_complete_userspace; 310 311 kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 312 } 313 314 static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 315 { 316 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 317 318 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) 319 return 1; 320 321 trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id, 322 to_hv_vcpu(vcpu)->vp_index, msr, data); 323 switch (msr) { 324 case HV_X64_MSR_SYNDBG_CONTROL: 325 syndbg->control.control = data; 326 if (!host) 327 syndbg_exit(vcpu, msr); 328 break; 329 case HV_X64_MSR_SYNDBG_STATUS: 330 syndbg->control.status = data; 331 break; 332 case HV_X64_MSR_SYNDBG_SEND_BUFFER: 333 syndbg->control.send_page = data; 334 break; 335 case HV_X64_MSR_SYNDBG_RECV_BUFFER: 336 syndbg->control.recv_page = data; 337 break; 338 case HV_X64_MSR_SYNDBG_PENDING_BUFFER: 339 syndbg->control.pending_page = data; 340 if (!host) 341 syndbg_exit(vcpu, msr); 342 break; 343 case HV_X64_MSR_SYNDBG_OPTIONS: 344 syndbg->options = data; 345 break; 346 default: 347 break; 348 } 349 350 return 0; 351 } 352 353 static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) 354 { 355 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 356 357 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) 358 return 1; 359 360 switch (msr) { 361 case HV_X64_MSR_SYNDBG_CONTROL: 362 *pdata = syndbg->control.control; 363 break; 364 case HV_X64_MSR_SYNDBG_STATUS: 365 *pdata = syndbg->control.status; 366 break; 367 case HV_X64_MSR_SYNDBG_SEND_BUFFER: 368 *pdata = syndbg->control.send_page; 369 break; 370 case HV_X64_MSR_SYNDBG_RECV_BUFFER: 371 *pdata = syndbg->control.recv_page; 372 break; 373 case HV_X64_MSR_SYNDBG_PENDING_BUFFER: 374 *pdata = syndbg->control.pending_page; 375 break; 376 case HV_X64_MSR_SYNDBG_OPTIONS: 377 *pdata = syndbg->options; 378 break; 379 default: 380 break; 381 } 382 383 trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata); 384 385 return 0; 386 } 387 388 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata, 389 bool host) 390 { 391 int ret; 392 393 if (!synic->active && !host) 394 return 1; 395 396 ret = 0; 397 switch (msr) { 398 case HV_X64_MSR_SCONTROL: 399 *pdata = synic->control; 400 break; 401 case HV_X64_MSR_SVERSION: 402 *pdata = synic->version; 403 break; 404 case HV_X64_MSR_SIEFP: 405 *pdata = synic->evt_page; 406 break; 407 case HV_X64_MSR_SIMP: 408 *pdata = synic->msg_page; 409 break; 410 case HV_X64_MSR_EOM: 411 *pdata = 0; 412 break; 413 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 414 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); 415 break; 416 default: 417 ret = 1; 418 break; 419 } 420 return ret; 421 } 422 423 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) 424 { 425 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 426 struct kvm_lapic_irq irq; 427 int ret, vector; 428 429 if (sint >= ARRAY_SIZE(synic->sint)) 430 return -EINVAL; 431 432 vector = synic_get_sint_vector(synic_read_sint(synic, sint)); 433 if (vector < 0) 434 return -ENOENT; 435 436 memset(&irq, 0, sizeof(irq)); 437 irq.shorthand = APIC_DEST_SELF; 438 irq.dest_mode = APIC_DEST_PHYSICAL; 439 irq.delivery_mode = APIC_DM_FIXED; 440 irq.vector = vector; 441 irq.level = 1; 442 443 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL); 444 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret); 445 return ret; 446 } 447 448 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint) 449 { 450 struct kvm_vcpu_hv_synic *synic; 451 452 synic = synic_get(kvm, vpidx); 453 if (!synic) 454 return -EINVAL; 455 456 return synic_set_irq(synic, sint); 457 } 458 459 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) 460 { 461 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 462 int i; 463 464 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); 465 466 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 467 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 468 kvm_hv_notify_acked_sint(vcpu, i); 469 } 470 471 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi) 472 { 473 struct kvm_vcpu_hv_synic *synic; 474 475 synic = synic_get(kvm, vpidx); 476 if (!synic) 477 return -EINVAL; 478 479 if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) 480 return -EINVAL; 481 482 atomic_set(&synic->sint_to_gsi[sint], gsi); 483 return 0; 484 } 485 486 void kvm_hv_irq_routing_update(struct kvm *kvm) 487 { 488 struct kvm_irq_routing_table *irq_rt; 489 struct kvm_kernel_irq_routing_entry *e; 490 u32 gsi; 491 492 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, 493 lockdep_is_held(&kvm->irq_lock)); 494 495 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { 496 hlist_for_each_entry(e, &irq_rt->map[gsi], link) { 497 if (e->type == KVM_IRQ_ROUTING_HV_SINT) 498 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, 499 e->hv_sint.sint, gsi); 500 } 501 } 502 } 503 504 static void synic_init(struct kvm_vcpu_hv_synic *synic) 505 { 506 int i; 507 508 memset(synic, 0, sizeof(*synic)); 509 synic->version = HV_SYNIC_VERSION_1; 510 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 511 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); 512 atomic_set(&synic->sint_to_gsi[i], -1); 513 } 514 } 515 516 static u64 get_time_ref_counter(struct kvm *kvm) 517 { 518 struct kvm_hv *hv = to_kvm_hv(kvm); 519 struct kvm_vcpu *vcpu; 520 u64 tsc; 521 522 /* 523 * The guest has not set up the TSC page or the clock isn't 524 * stable, fall back to get_kvmclock_ns. 525 */ 526 if (!hv->tsc_ref.tsc_sequence) 527 return div_u64(get_kvmclock_ns(kvm), 100); 528 529 vcpu = kvm_get_vcpu(kvm, 0); 530 tsc = kvm_read_l1_tsc(vcpu, rdtsc()); 531 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) 532 + hv->tsc_ref.tsc_offset; 533 } 534 535 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, 536 bool vcpu_kick) 537 { 538 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 539 540 set_bit(stimer->index, 541 to_hv_vcpu(vcpu)->stimer_pending_bitmap); 542 kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 543 if (vcpu_kick) 544 kvm_vcpu_kick(vcpu); 545 } 546 547 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) 548 { 549 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 550 551 trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id, 552 stimer->index); 553 554 hrtimer_cancel(&stimer->timer); 555 clear_bit(stimer->index, 556 to_hv_vcpu(vcpu)->stimer_pending_bitmap); 557 stimer->msg_pending = false; 558 stimer->exp_time = 0; 559 } 560 561 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) 562 { 563 struct kvm_vcpu_hv_stimer *stimer; 564 565 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); 566 trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id, 567 stimer->index); 568 stimer_mark_pending(stimer, true); 569 570 return HRTIMER_NORESTART; 571 } 572 573 /* 574 * stimer_start() assumptions: 575 * a) stimer->count is not equal to 0 576 * b) stimer->config has HV_STIMER_ENABLE flag 577 */ 578 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) 579 { 580 u64 time_now; 581 ktime_t ktime_now; 582 583 time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm); 584 ktime_now = ktime_get(); 585 586 if (stimer->config.periodic) { 587 if (stimer->exp_time) { 588 if (time_now >= stimer->exp_time) { 589 u64 remainder; 590 591 div64_u64_rem(time_now - stimer->exp_time, 592 stimer->count, &remainder); 593 stimer->exp_time = 594 time_now + (stimer->count - remainder); 595 } 596 } else 597 stimer->exp_time = time_now + stimer->count; 598 599 trace_kvm_hv_stimer_start_periodic( 600 hv_stimer_to_vcpu(stimer)->vcpu_id, 601 stimer->index, 602 time_now, stimer->exp_time); 603 604 hrtimer_start(&stimer->timer, 605 ktime_add_ns(ktime_now, 606 100 * (stimer->exp_time - time_now)), 607 HRTIMER_MODE_ABS); 608 return 0; 609 } 610 stimer->exp_time = stimer->count; 611 if (time_now >= stimer->count) { 612 /* 613 * Expire timer according to Hypervisor Top-Level Functional 614 * specification v4(15.3.1): 615 * "If a one shot is enabled and the specified count is in 616 * the past, it will expire immediately." 617 */ 618 stimer_mark_pending(stimer, false); 619 return 0; 620 } 621 622 trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id, 623 stimer->index, 624 time_now, stimer->count); 625 626 hrtimer_start(&stimer->timer, 627 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), 628 HRTIMER_MODE_ABS); 629 return 0; 630 } 631 632 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, 633 bool host) 634 { 635 union hv_stimer_config new_config = {.as_uint64 = config}, 636 old_config = {.as_uint64 = stimer->config.as_uint64}; 637 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 638 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 639 640 if (!synic->active && !host) 641 return 1; 642 643 trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id, 644 stimer->index, config, host); 645 646 stimer_cleanup(stimer); 647 if (old_config.enable && 648 !new_config.direct_mode && new_config.sintx == 0) 649 new_config.enable = 0; 650 stimer->config.as_uint64 = new_config.as_uint64; 651 652 if (stimer->config.enable) 653 stimer_mark_pending(stimer, false); 654 655 return 0; 656 } 657 658 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, 659 bool host) 660 { 661 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 662 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 663 664 if (!synic->active && !host) 665 return 1; 666 667 trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id, 668 stimer->index, count, host); 669 670 stimer_cleanup(stimer); 671 stimer->count = count; 672 if (stimer->count == 0) 673 stimer->config.enable = 0; 674 else if (stimer->config.auto_enable) 675 stimer->config.enable = 1; 676 677 if (stimer->config.enable) 678 stimer_mark_pending(stimer, false); 679 680 return 0; 681 } 682 683 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) 684 { 685 *pconfig = stimer->config.as_uint64; 686 return 0; 687 } 688 689 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) 690 { 691 *pcount = stimer->count; 692 return 0; 693 } 694 695 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, 696 struct hv_message *src_msg, bool no_retry) 697 { 698 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 699 int msg_off = offsetof(struct hv_message_page, sint_message[sint]); 700 gfn_t msg_page_gfn; 701 struct hv_message_header hv_hdr; 702 int r; 703 704 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) 705 return -ENOENT; 706 707 msg_page_gfn = synic->msg_page >> PAGE_SHIFT; 708 709 /* 710 * Strictly following the spec-mandated ordering would assume setting 711 * .msg_pending before checking .message_type. However, this function 712 * is only called in vcpu context so the entire update is atomic from 713 * guest POV and thus the exact order here doesn't matter. 714 */ 715 r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type, 716 msg_off + offsetof(struct hv_message, 717 header.message_type), 718 sizeof(hv_hdr.message_type)); 719 if (r < 0) 720 return r; 721 722 if (hv_hdr.message_type != HVMSG_NONE) { 723 if (no_retry) 724 return 0; 725 726 hv_hdr.message_flags.msg_pending = 1; 727 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, 728 &hv_hdr.message_flags, 729 msg_off + 730 offsetof(struct hv_message, 731 header.message_flags), 732 sizeof(hv_hdr.message_flags)); 733 if (r < 0) 734 return r; 735 return -EAGAIN; 736 } 737 738 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off, 739 sizeof(src_msg->header) + 740 src_msg->header.payload_size); 741 if (r < 0) 742 return r; 743 744 r = synic_set_irq(synic, sint); 745 if (r < 0) 746 return r; 747 if (r == 0) 748 return -EFAULT; 749 return 0; 750 } 751 752 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) 753 { 754 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 755 struct hv_message *msg = &stimer->msg; 756 struct hv_timer_message_payload *payload = 757 (struct hv_timer_message_payload *)&msg->u.payload; 758 759 /* 760 * To avoid piling up periodic ticks, don't retry message 761 * delivery for them (within "lazy" lost ticks policy). 762 */ 763 bool no_retry = stimer->config.periodic; 764 765 payload->expiration_time = stimer->exp_time; 766 payload->delivery_time = get_time_ref_counter(vcpu->kvm); 767 return synic_deliver_msg(to_hv_synic(vcpu), 768 stimer->config.sintx, msg, 769 no_retry); 770 } 771 772 static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer) 773 { 774 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 775 struct kvm_lapic_irq irq = { 776 .delivery_mode = APIC_DM_FIXED, 777 .vector = stimer->config.apic_vector 778 }; 779 780 if (lapic_in_kernel(vcpu)) 781 return !kvm_apic_set_irq(vcpu, &irq, NULL); 782 return 0; 783 } 784 785 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) 786 { 787 int r, direct = stimer->config.direct_mode; 788 789 stimer->msg_pending = true; 790 if (!direct) 791 r = stimer_send_msg(stimer); 792 else 793 r = stimer_notify_direct(stimer); 794 trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id, 795 stimer->index, direct, r); 796 if (!r) { 797 stimer->msg_pending = false; 798 if (!(stimer->config.periodic)) 799 stimer->config.enable = 0; 800 } 801 } 802 803 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) 804 { 805 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 806 struct kvm_vcpu_hv_stimer *stimer; 807 u64 time_now, exp_time; 808 int i; 809 810 if (!hv_vcpu) 811 return; 812 813 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 814 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { 815 stimer = &hv_vcpu->stimer[i]; 816 if (stimer->config.enable) { 817 exp_time = stimer->exp_time; 818 819 if (exp_time) { 820 time_now = 821 get_time_ref_counter(vcpu->kvm); 822 if (time_now >= exp_time) 823 stimer_expiration(stimer); 824 } 825 826 if ((stimer->config.enable) && 827 stimer->count) { 828 if (!stimer->msg_pending) 829 stimer_start(stimer); 830 } else 831 stimer_cleanup(stimer); 832 } 833 } 834 } 835 836 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) 837 { 838 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 839 int i; 840 841 if (!hv_vcpu) 842 return; 843 844 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 845 stimer_cleanup(&hv_vcpu->stimer[i]); 846 847 kfree(hv_vcpu); 848 vcpu->arch.hyperv = NULL; 849 } 850 851 bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu) 852 { 853 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 854 855 if (!hv_vcpu) 856 return false; 857 858 if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) 859 return false; 860 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; 861 } 862 EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled); 863 864 bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu, 865 struct hv_vp_assist_page *assist_page) 866 { 867 if (!kvm_hv_assist_page_enabled(vcpu)) 868 return false; 869 return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, 870 assist_page, sizeof(*assist_page)); 871 } 872 EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page); 873 874 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) 875 { 876 struct hv_message *msg = &stimer->msg; 877 struct hv_timer_message_payload *payload = 878 (struct hv_timer_message_payload *)&msg->u.payload; 879 880 memset(&msg->header, 0, sizeof(msg->header)); 881 msg->header.message_type = HVMSG_TIMER_EXPIRED; 882 msg->header.payload_size = sizeof(*payload); 883 884 payload->timer_index = stimer->index; 885 payload->expiration_time = 0; 886 payload->delivery_time = 0; 887 } 888 889 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) 890 { 891 memset(stimer, 0, sizeof(*stimer)); 892 stimer->index = timer_index; 893 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 894 stimer->timer.function = stimer_timer_callback; 895 stimer_prepare_msg(stimer); 896 } 897 898 static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) 899 { 900 struct kvm_vcpu_hv *hv_vcpu; 901 int i; 902 903 hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT); 904 if (!hv_vcpu) 905 return -ENOMEM; 906 907 vcpu->arch.hyperv = hv_vcpu; 908 hv_vcpu->vcpu = vcpu; 909 910 synic_init(&hv_vcpu->synic); 911 912 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); 913 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 914 stimer_init(&hv_vcpu->stimer[i], i); 915 916 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu); 917 918 return 0; 919 } 920 921 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) 922 { 923 struct kvm_vcpu_hv_synic *synic; 924 int r; 925 926 if (!to_hv_vcpu(vcpu)) { 927 r = kvm_hv_vcpu_init(vcpu); 928 if (r) 929 return r; 930 } 931 932 synic = to_hv_synic(vcpu); 933 934 /* 935 * Hyper-V SynIC auto EOI SINT's are 936 * not compatible with APICV, so request 937 * to deactivate APICV permanently. 938 */ 939 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV); 940 synic->active = true; 941 synic->dont_zero_synic_pages = dont_zero_synic_pages; 942 synic->control = HV_SYNIC_CONTROL_ENABLE; 943 return 0; 944 } 945 946 static bool kvm_hv_msr_partition_wide(u32 msr) 947 { 948 bool r = false; 949 950 switch (msr) { 951 case HV_X64_MSR_GUEST_OS_ID: 952 case HV_X64_MSR_HYPERCALL: 953 case HV_X64_MSR_REFERENCE_TSC: 954 case HV_X64_MSR_TIME_REF_COUNT: 955 case HV_X64_MSR_CRASH_CTL: 956 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 957 case HV_X64_MSR_RESET: 958 case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 959 case HV_X64_MSR_TSC_EMULATION_CONTROL: 960 case HV_X64_MSR_TSC_EMULATION_STATUS: 961 case HV_X64_MSR_SYNDBG_OPTIONS: 962 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 963 r = true; 964 break; 965 } 966 967 return r; 968 } 969 970 static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata) 971 { 972 struct kvm_hv *hv = to_kvm_hv(kvm); 973 size_t size = ARRAY_SIZE(hv->hv_crash_param); 974 975 if (WARN_ON_ONCE(index >= size)) 976 return -EINVAL; 977 978 *pdata = hv->hv_crash_param[array_index_nospec(index, size)]; 979 return 0; 980 } 981 982 static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata) 983 { 984 struct kvm_hv *hv = to_kvm_hv(kvm); 985 986 *pdata = hv->hv_crash_ctl; 987 return 0; 988 } 989 990 static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data) 991 { 992 struct kvm_hv *hv = to_kvm_hv(kvm); 993 994 hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY; 995 996 return 0; 997 } 998 999 static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data) 1000 { 1001 struct kvm_hv *hv = to_kvm_hv(kvm); 1002 size_t size = ARRAY_SIZE(hv->hv_crash_param); 1003 1004 if (WARN_ON_ONCE(index >= size)) 1005 return -EINVAL; 1006 1007 hv->hv_crash_param[array_index_nospec(index, size)] = data; 1008 return 0; 1009 } 1010 1011 /* 1012 * The kvmclock and Hyper-V TSC page use similar formulas, and converting 1013 * between them is possible: 1014 * 1015 * kvmclock formula: 1016 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) 1017 * + system_time 1018 * 1019 * Hyper-V formula: 1020 * nsec/100 = ticks * scale / 2^64 + offset 1021 * 1022 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. 1023 * By dividing the kvmclock formula by 100 and equating what's left we get: 1024 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1025 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 1026 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 1027 * 1028 * Now expand the kvmclock formula and divide by 100: 1029 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) 1030 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) 1031 * + system_time 1032 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1033 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1034 * + system_time / 100 1035 * 1036 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: 1037 * nsec/100 = ticks * scale / 2^64 1038 * - tsc_timestamp * scale / 2^64 1039 * + system_time / 100 1040 * 1041 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: 1042 * offset = system_time / 100 - tsc_timestamp * scale / 2^64 1043 * 1044 * These two equivalencies are implemented in this function. 1045 */ 1046 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, 1047 struct ms_hyperv_tsc_page *tsc_ref) 1048 { 1049 u64 max_mul; 1050 1051 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) 1052 return false; 1053 1054 /* 1055 * check if scale would overflow, if so we use the time ref counter 1056 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 1057 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) 1058 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) 1059 */ 1060 max_mul = 100ull << (32 - hv_clock->tsc_shift); 1061 if (hv_clock->tsc_to_system_mul >= max_mul) 1062 return false; 1063 1064 /* 1065 * Otherwise compute the scale and offset according to the formulas 1066 * derived above. 1067 */ 1068 tsc_ref->tsc_scale = 1069 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), 1070 hv_clock->tsc_to_system_mul, 1071 100); 1072 1073 tsc_ref->tsc_offset = hv_clock->system_time; 1074 do_div(tsc_ref->tsc_offset, 100); 1075 tsc_ref->tsc_offset -= 1076 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); 1077 return true; 1078 } 1079 1080 void kvm_hv_setup_tsc_page(struct kvm *kvm, 1081 struct pvclock_vcpu_time_info *hv_clock) 1082 { 1083 struct kvm_hv *hv = to_kvm_hv(kvm); 1084 u32 tsc_seq; 1085 u64 gfn; 1086 1087 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); 1088 BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0); 1089 1090 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 1091 return; 1092 1093 mutex_lock(&hv->hv_lock); 1094 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 1095 goto out_unlock; 1096 1097 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; 1098 /* 1099 * Because the TSC parameters only vary when there is a 1100 * change in the master clock, do not bother with caching. 1101 */ 1102 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), 1103 &tsc_seq, sizeof(tsc_seq)))) 1104 goto out_unlock; 1105 1106 /* 1107 * While we're computing and writing the parameters, force the 1108 * guest to use the time reference count MSR. 1109 */ 1110 hv->tsc_ref.tsc_sequence = 0; 1111 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), 1112 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) 1113 goto out_unlock; 1114 1115 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) 1116 goto out_unlock; 1117 1118 /* Ensure sequence is zero before writing the rest of the struct. */ 1119 smp_wmb(); 1120 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) 1121 goto out_unlock; 1122 1123 /* 1124 * Now switch to the TSC page mechanism by writing the sequence. 1125 */ 1126 tsc_seq++; 1127 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) 1128 tsc_seq = 1; 1129 1130 /* Write the struct entirely before the non-zero sequence. */ 1131 smp_wmb(); 1132 1133 hv->tsc_ref.tsc_sequence = tsc_seq; 1134 kvm_write_guest(kvm, gfn_to_gpa(gfn), 1135 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)); 1136 out_unlock: 1137 mutex_unlock(&hv->hv_lock); 1138 } 1139 1140 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, 1141 bool host) 1142 { 1143 struct kvm *kvm = vcpu->kvm; 1144 struct kvm_hv *hv = to_kvm_hv(kvm); 1145 1146 switch (msr) { 1147 case HV_X64_MSR_GUEST_OS_ID: 1148 hv->hv_guest_os_id = data; 1149 /* setting guest os id to zero disables hypercall page */ 1150 if (!hv->hv_guest_os_id) 1151 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; 1152 break; 1153 case HV_X64_MSR_HYPERCALL: { 1154 u8 instructions[9]; 1155 int i = 0; 1156 u64 addr; 1157 1158 /* if guest os id is not set hypercall should remain disabled */ 1159 if (!hv->hv_guest_os_id) 1160 break; 1161 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { 1162 hv->hv_hypercall = data; 1163 break; 1164 } 1165 1166 /* 1167 * If Xen and Hyper-V hypercalls are both enabled, disambiguate 1168 * the same way Xen itself does, by setting the bit 31 of EAX 1169 * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just 1170 * going to be clobbered on 64-bit. 1171 */ 1172 if (kvm_xen_hypercall_enabled(kvm)) { 1173 /* orl $0x80000000, %eax */ 1174 instructions[i++] = 0x0d; 1175 instructions[i++] = 0x00; 1176 instructions[i++] = 0x00; 1177 instructions[i++] = 0x00; 1178 instructions[i++] = 0x80; 1179 } 1180 1181 /* vmcall/vmmcall */ 1182 static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); 1183 i += 3; 1184 1185 /* ret */ 1186 ((unsigned char *)instructions)[i++] = 0xc3; 1187 1188 addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; 1189 if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) 1190 return 1; 1191 hv->hv_hypercall = data; 1192 break; 1193 } 1194 case HV_X64_MSR_REFERENCE_TSC: 1195 hv->hv_tsc_page = data; 1196 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) 1197 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); 1198 break; 1199 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 1200 return kvm_hv_msr_set_crash_data(kvm, 1201 msr - HV_X64_MSR_CRASH_P0, 1202 data); 1203 case HV_X64_MSR_CRASH_CTL: 1204 if (host) 1205 return kvm_hv_msr_set_crash_ctl(kvm, data); 1206 1207 if (data & HV_CRASH_CTL_CRASH_NOTIFY) { 1208 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", 1209 hv->hv_crash_param[0], 1210 hv->hv_crash_param[1], 1211 hv->hv_crash_param[2], 1212 hv->hv_crash_param[3], 1213 hv->hv_crash_param[4]); 1214 1215 /* Send notification about crash to user space */ 1216 kvm_make_request(KVM_REQ_HV_CRASH, vcpu); 1217 } 1218 break; 1219 case HV_X64_MSR_RESET: 1220 if (data == 1) { 1221 vcpu_debug(vcpu, "hyper-v reset requested\n"); 1222 kvm_make_request(KVM_REQ_HV_RESET, vcpu); 1223 } 1224 break; 1225 case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 1226 hv->hv_reenlightenment_control = data; 1227 break; 1228 case HV_X64_MSR_TSC_EMULATION_CONTROL: 1229 hv->hv_tsc_emulation_control = data; 1230 break; 1231 case HV_X64_MSR_TSC_EMULATION_STATUS: 1232 hv->hv_tsc_emulation_status = data; 1233 break; 1234 case HV_X64_MSR_TIME_REF_COUNT: 1235 /* read-only, but still ignore it if host-initiated */ 1236 if (!host) 1237 return 1; 1238 break; 1239 case HV_X64_MSR_SYNDBG_OPTIONS: 1240 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 1241 return syndbg_set_msr(vcpu, msr, data, host); 1242 default: 1243 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", 1244 msr, data); 1245 return 1; 1246 } 1247 return 0; 1248 } 1249 1250 /* Calculate cpu time spent by current task in 100ns units */ 1251 static u64 current_task_runtime_100ns(void) 1252 { 1253 u64 utime, stime; 1254 1255 task_cputime_adjusted(current, &utime, &stime); 1256 1257 return div_u64(utime + stime, 100); 1258 } 1259 1260 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1261 { 1262 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1263 1264 switch (msr) { 1265 case HV_X64_MSR_VP_INDEX: { 1266 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 1267 int vcpu_idx = kvm_vcpu_get_idx(vcpu); 1268 u32 new_vp_index = (u32)data; 1269 1270 if (!host || new_vp_index >= KVM_MAX_VCPUS) 1271 return 1; 1272 1273 if (new_vp_index == hv_vcpu->vp_index) 1274 return 0; 1275 1276 /* 1277 * The VP index is initialized to vcpu_index by 1278 * kvm_hv_vcpu_postcreate so they initially match. Now the 1279 * VP index is changing, adjust num_mismatched_vp_indexes if 1280 * it now matches or no longer matches vcpu_idx. 1281 */ 1282 if (hv_vcpu->vp_index == vcpu_idx) 1283 atomic_inc(&hv->num_mismatched_vp_indexes); 1284 else if (new_vp_index == vcpu_idx) 1285 atomic_dec(&hv->num_mismatched_vp_indexes); 1286 1287 hv_vcpu->vp_index = new_vp_index; 1288 break; 1289 } 1290 case HV_X64_MSR_VP_ASSIST_PAGE: { 1291 u64 gfn; 1292 unsigned long addr; 1293 1294 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) { 1295 hv_vcpu->hv_vapic = data; 1296 if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0)) 1297 return 1; 1298 break; 1299 } 1300 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT; 1301 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); 1302 if (kvm_is_error_hva(addr)) 1303 return 1; 1304 1305 /* 1306 * Clear apic_assist portion of struct hv_vp_assist_page 1307 * only, there can be valuable data in the rest which needs 1308 * to be preserved e.g. on migration. 1309 */ 1310 if (__put_user(0, (u32 __user *)addr)) 1311 return 1; 1312 hv_vcpu->hv_vapic = data; 1313 kvm_vcpu_mark_page_dirty(vcpu, gfn); 1314 if (kvm_lapic_enable_pv_eoi(vcpu, 1315 gfn_to_gpa(gfn) | KVM_MSR_ENABLED, 1316 sizeof(struct hv_vp_assist_page))) 1317 return 1; 1318 break; 1319 } 1320 case HV_X64_MSR_EOI: 1321 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); 1322 case HV_X64_MSR_ICR: 1323 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); 1324 case HV_X64_MSR_TPR: 1325 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); 1326 case HV_X64_MSR_VP_RUNTIME: 1327 if (!host) 1328 return 1; 1329 hv_vcpu->runtime_offset = data - current_task_runtime_100ns(); 1330 break; 1331 case HV_X64_MSR_SCONTROL: 1332 case HV_X64_MSR_SVERSION: 1333 case HV_X64_MSR_SIEFP: 1334 case HV_X64_MSR_SIMP: 1335 case HV_X64_MSR_EOM: 1336 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 1337 return synic_set_msr(to_hv_synic(vcpu), msr, data, host); 1338 case HV_X64_MSR_STIMER0_CONFIG: 1339 case HV_X64_MSR_STIMER1_CONFIG: 1340 case HV_X64_MSR_STIMER2_CONFIG: 1341 case HV_X64_MSR_STIMER3_CONFIG: { 1342 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 1343 1344 return stimer_set_config(to_hv_stimer(vcpu, timer_index), 1345 data, host); 1346 } 1347 case HV_X64_MSR_STIMER0_COUNT: 1348 case HV_X64_MSR_STIMER1_COUNT: 1349 case HV_X64_MSR_STIMER2_COUNT: 1350 case HV_X64_MSR_STIMER3_COUNT: { 1351 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 1352 1353 return stimer_set_count(to_hv_stimer(vcpu, timer_index), 1354 data, host); 1355 } 1356 case HV_X64_MSR_TSC_FREQUENCY: 1357 case HV_X64_MSR_APIC_FREQUENCY: 1358 /* read-only, but still ignore it if host-initiated */ 1359 if (!host) 1360 return 1; 1361 break; 1362 default: 1363 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", 1364 msr, data); 1365 return 1; 1366 } 1367 1368 return 0; 1369 } 1370 1371 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, 1372 bool host) 1373 { 1374 u64 data = 0; 1375 struct kvm *kvm = vcpu->kvm; 1376 struct kvm_hv *hv = to_kvm_hv(kvm); 1377 1378 switch (msr) { 1379 case HV_X64_MSR_GUEST_OS_ID: 1380 data = hv->hv_guest_os_id; 1381 break; 1382 case HV_X64_MSR_HYPERCALL: 1383 data = hv->hv_hypercall; 1384 break; 1385 case HV_X64_MSR_TIME_REF_COUNT: 1386 data = get_time_ref_counter(kvm); 1387 break; 1388 case HV_X64_MSR_REFERENCE_TSC: 1389 data = hv->hv_tsc_page; 1390 break; 1391 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 1392 return kvm_hv_msr_get_crash_data(kvm, 1393 msr - HV_X64_MSR_CRASH_P0, 1394 pdata); 1395 case HV_X64_MSR_CRASH_CTL: 1396 return kvm_hv_msr_get_crash_ctl(kvm, pdata); 1397 case HV_X64_MSR_RESET: 1398 data = 0; 1399 break; 1400 case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 1401 data = hv->hv_reenlightenment_control; 1402 break; 1403 case HV_X64_MSR_TSC_EMULATION_CONTROL: 1404 data = hv->hv_tsc_emulation_control; 1405 break; 1406 case HV_X64_MSR_TSC_EMULATION_STATUS: 1407 data = hv->hv_tsc_emulation_status; 1408 break; 1409 case HV_X64_MSR_SYNDBG_OPTIONS: 1410 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 1411 return syndbg_get_msr(vcpu, msr, pdata, host); 1412 default: 1413 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1414 return 1; 1415 } 1416 1417 *pdata = data; 1418 return 0; 1419 } 1420 1421 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, 1422 bool host) 1423 { 1424 u64 data = 0; 1425 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1426 1427 switch (msr) { 1428 case HV_X64_MSR_VP_INDEX: 1429 data = hv_vcpu->vp_index; 1430 break; 1431 case HV_X64_MSR_EOI: 1432 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); 1433 case HV_X64_MSR_ICR: 1434 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); 1435 case HV_X64_MSR_TPR: 1436 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); 1437 case HV_X64_MSR_VP_ASSIST_PAGE: 1438 data = hv_vcpu->hv_vapic; 1439 break; 1440 case HV_X64_MSR_VP_RUNTIME: 1441 data = current_task_runtime_100ns() + hv_vcpu->runtime_offset; 1442 break; 1443 case HV_X64_MSR_SCONTROL: 1444 case HV_X64_MSR_SVERSION: 1445 case HV_X64_MSR_SIEFP: 1446 case HV_X64_MSR_SIMP: 1447 case HV_X64_MSR_EOM: 1448 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 1449 return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host); 1450 case HV_X64_MSR_STIMER0_CONFIG: 1451 case HV_X64_MSR_STIMER1_CONFIG: 1452 case HV_X64_MSR_STIMER2_CONFIG: 1453 case HV_X64_MSR_STIMER3_CONFIG: { 1454 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 1455 1456 return stimer_get_config(to_hv_stimer(vcpu, timer_index), 1457 pdata); 1458 } 1459 case HV_X64_MSR_STIMER0_COUNT: 1460 case HV_X64_MSR_STIMER1_COUNT: 1461 case HV_X64_MSR_STIMER2_COUNT: 1462 case HV_X64_MSR_STIMER3_COUNT: { 1463 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 1464 1465 return stimer_get_count(to_hv_stimer(vcpu, timer_index), 1466 pdata); 1467 } 1468 case HV_X64_MSR_TSC_FREQUENCY: 1469 data = (u64)vcpu->arch.virtual_tsc_khz * 1000; 1470 break; 1471 case HV_X64_MSR_APIC_FREQUENCY: 1472 data = APIC_BUS_FREQUENCY; 1473 break; 1474 default: 1475 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1476 return 1; 1477 } 1478 *pdata = data; 1479 return 0; 1480 } 1481 1482 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1483 { 1484 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 1485 1486 if (!host && !vcpu->arch.hyperv_enabled) 1487 return 1; 1488 1489 if (!to_hv_vcpu(vcpu)) { 1490 if (kvm_hv_vcpu_init(vcpu)) 1491 return 1; 1492 } 1493 1494 if (kvm_hv_msr_partition_wide(msr)) { 1495 int r; 1496 1497 mutex_lock(&hv->hv_lock); 1498 r = kvm_hv_set_msr_pw(vcpu, msr, data, host); 1499 mutex_unlock(&hv->hv_lock); 1500 return r; 1501 } else 1502 return kvm_hv_set_msr(vcpu, msr, data, host); 1503 } 1504 1505 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) 1506 { 1507 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 1508 1509 if (!host && !vcpu->arch.hyperv_enabled) 1510 return 1; 1511 1512 if (!to_hv_vcpu(vcpu)) { 1513 if (kvm_hv_vcpu_init(vcpu)) 1514 return 1; 1515 } 1516 1517 if (kvm_hv_msr_partition_wide(msr)) { 1518 int r; 1519 1520 mutex_lock(&hv->hv_lock); 1521 r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host); 1522 mutex_unlock(&hv->hv_lock); 1523 return r; 1524 } else 1525 return kvm_hv_get_msr(vcpu, msr, pdata, host); 1526 } 1527 1528 static __always_inline unsigned long *sparse_set_to_vcpu_mask( 1529 struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask, 1530 u64 *vp_bitmap, unsigned long *vcpu_bitmap) 1531 { 1532 struct kvm_hv *hv = to_kvm_hv(kvm); 1533 struct kvm_vcpu *vcpu; 1534 int i, bank, sbank = 0; 1535 1536 memset(vp_bitmap, 0, 1537 KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap)); 1538 for_each_set_bit(bank, (unsigned long *)&valid_bank_mask, 1539 KVM_HV_MAX_SPARSE_VCPU_SET_BITS) 1540 vp_bitmap[bank] = sparse_banks[sbank++]; 1541 1542 if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) { 1543 /* for all vcpus vp_index == vcpu_idx */ 1544 return (unsigned long *)vp_bitmap; 1545 } 1546 1547 bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS); 1548 kvm_for_each_vcpu(i, vcpu, kvm) { 1549 if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap)) 1550 __set_bit(i, vcpu_bitmap); 1551 } 1552 return vcpu_bitmap; 1553 } 1554 1555 static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, u64 ingpa, u16 rep_cnt, bool ex) 1556 { 1557 struct kvm *kvm = vcpu->kvm; 1558 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1559 struct hv_tlb_flush_ex flush_ex; 1560 struct hv_tlb_flush flush; 1561 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; 1562 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); 1563 unsigned long *vcpu_mask; 1564 u64 valid_bank_mask; 1565 u64 sparse_banks[64]; 1566 int sparse_banks_len; 1567 bool all_cpus; 1568 1569 if (!ex) { 1570 if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush)))) 1571 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1572 1573 trace_kvm_hv_flush_tlb(flush.processor_mask, 1574 flush.address_space, flush.flags); 1575 1576 valid_bank_mask = BIT_ULL(0); 1577 sparse_banks[0] = flush.processor_mask; 1578 1579 /* 1580 * Work around possible WS2012 bug: it sends hypercalls 1581 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear, 1582 * while also expecting us to flush something and crashing if 1583 * we don't. Let's treat processor_mask == 0 same as 1584 * HV_FLUSH_ALL_PROCESSORS. 1585 */ 1586 all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) || 1587 flush.processor_mask == 0; 1588 } else { 1589 if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex, 1590 sizeof(flush_ex)))) 1591 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1592 1593 trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask, 1594 flush_ex.hv_vp_set.format, 1595 flush_ex.address_space, 1596 flush_ex.flags); 1597 1598 valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask; 1599 all_cpus = flush_ex.hv_vp_set.format != 1600 HV_GENERIC_SET_SPARSE_4K; 1601 1602 sparse_banks_len = 1603 bitmap_weight((unsigned long *)&valid_bank_mask, 64) * 1604 sizeof(sparse_banks[0]); 1605 1606 if (!sparse_banks_len && !all_cpus) 1607 goto ret_success; 1608 1609 if (!all_cpus && 1610 kvm_read_guest(kvm, 1611 ingpa + offsetof(struct hv_tlb_flush_ex, 1612 hv_vp_set.bank_contents), 1613 sparse_banks, 1614 sparse_banks_len)) 1615 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1616 } 1617 1618 cpumask_clear(&hv_vcpu->tlb_flush); 1619 1620 vcpu_mask = all_cpus ? NULL : 1621 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, 1622 vp_bitmap, vcpu_bitmap); 1623 1624 /* 1625 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't 1626 * analyze it here, flush TLB regardless of the specified address space. 1627 */ 1628 kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, 1629 NULL, vcpu_mask, &hv_vcpu->tlb_flush); 1630 1631 ret_success: 1632 /* We always do full TLB flush, set rep_done = rep_cnt. */ 1633 return (u64)HV_STATUS_SUCCESS | 1634 ((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); 1635 } 1636 1637 static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector, 1638 unsigned long *vcpu_bitmap) 1639 { 1640 struct kvm_lapic_irq irq = { 1641 .delivery_mode = APIC_DM_FIXED, 1642 .vector = vector 1643 }; 1644 struct kvm_vcpu *vcpu; 1645 int i; 1646 1647 kvm_for_each_vcpu(i, vcpu, kvm) { 1648 if (vcpu_bitmap && !test_bit(i, vcpu_bitmap)) 1649 continue; 1650 1651 /* We fail only when APIC is disabled */ 1652 kvm_apic_set_irq(vcpu, &irq, NULL); 1653 } 1654 } 1655 1656 static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, u64 ingpa, u64 outgpa, 1657 bool ex, bool fast) 1658 { 1659 struct kvm *kvm = vcpu->kvm; 1660 struct hv_send_ipi_ex send_ipi_ex; 1661 struct hv_send_ipi send_ipi; 1662 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; 1663 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); 1664 unsigned long *vcpu_mask; 1665 unsigned long valid_bank_mask; 1666 u64 sparse_banks[64]; 1667 int sparse_banks_len; 1668 u32 vector; 1669 bool all_cpus; 1670 1671 if (!ex) { 1672 if (!fast) { 1673 if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi, 1674 sizeof(send_ipi)))) 1675 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1676 sparse_banks[0] = send_ipi.cpu_mask; 1677 vector = send_ipi.vector; 1678 } else { 1679 /* 'reserved' part of hv_send_ipi should be 0 */ 1680 if (unlikely(ingpa >> 32 != 0)) 1681 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1682 sparse_banks[0] = outgpa; 1683 vector = (u32)ingpa; 1684 } 1685 all_cpus = false; 1686 valid_bank_mask = BIT_ULL(0); 1687 1688 trace_kvm_hv_send_ipi(vector, sparse_banks[0]); 1689 } else { 1690 if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex, 1691 sizeof(send_ipi_ex)))) 1692 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1693 1694 trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector, 1695 send_ipi_ex.vp_set.format, 1696 send_ipi_ex.vp_set.valid_bank_mask); 1697 1698 vector = send_ipi_ex.vector; 1699 valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask; 1700 sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) * 1701 sizeof(sparse_banks[0]); 1702 1703 all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL; 1704 1705 if (!sparse_banks_len) 1706 goto ret_success; 1707 1708 if (!all_cpus && 1709 kvm_read_guest(kvm, 1710 ingpa + offsetof(struct hv_send_ipi_ex, 1711 vp_set.bank_contents), 1712 sparse_banks, 1713 sparse_banks_len)) 1714 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1715 } 1716 1717 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR)) 1718 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1719 1720 vcpu_mask = all_cpus ? NULL : 1721 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, 1722 vp_bitmap, vcpu_bitmap); 1723 1724 kvm_send_ipi_to_many(kvm, vector, vcpu_mask); 1725 1726 ret_success: 1727 return HV_STATUS_SUCCESS; 1728 } 1729 1730 void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu) 1731 { 1732 struct kvm_cpuid_entry2 *entry; 1733 1734 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE, 0); 1735 if (entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX) 1736 vcpu->arch.hyperv_enabled = true; 1737 else 1738 vcpu->arch.hyperv_enabled = false; 1739 } 1740 1741 bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu) 1742 { 1743 return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id; 1744 } 1745 1746 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) 1747 { 1748 bool longmode; 1749 1750 longmode = is_64_bit_mode(vcpu); 1751 if (longmode) 1752 kvm_rax_write(vcpu, result); 1753 else { 1754 kvm_rdx_write(vcpu, result >> 32); 1755 kvm_rax_write(vcpu, result & 0xffffffff); 1756 } 1757 } 1758 1759 static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) 1760 { 1761 kvm_hv_hypercall_set_result(vcpu, result); 1762 ++vcpu->stat.hypercalls; 1763 return kvm_skip_emulated_instruction(vcpu); 1764 } 1765 1766 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) 1767 { 1768 return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result); 1769 } 1770 1771 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param) 1772 { 1773 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 1774 struct eventfd_ctx *eventfd; 1775 1776 if (unlikely(!fast)) { 1777 int ret; 1778 gpa_t gpa = param; 1779 1780 if ((gpa & (__alignof__(param) - 1)) || 1781 offset_in_page(gpa) + sizeof(param) > PAGE_SIZE) 1782 return HV_STATUS_INVALID_ALIGNMENT; 1783 1784 ret = kvm_vcpu_read_guest(vcpu, gpa, ¶m, sizeof(param)); 1785 if (ret < 0) 1786 return HV_STATUS_INVALID_ALIGNMENT; 1787 } 1788 1789 /* 1790 * Per spec, bits 32-47 contain the extra "flag number". However, we 1791 * have no use for it, and in all known usecases it is zero, so just 1792 * report lookup failure if it isn't. 1793 */ 1794 if (param & 0xffff00000000ULL) 1795 return HV_STATUS_INVALID_PORT_ID; 1796 /* remaining bits are reserved-zero */ 1797 if (param & ~KVM_HYPERV_CONN_ID_MASK) 1798 return HV_STATUS_INVALID_HYPERCALL_INPUT; 1799 1800 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */ 1801 rcu_read_lock(); 1802 eventfd = idr_find(&hv->conn_to_evt, param); 1803 rcu_read_unlock(); 1804 if (!eventfd) 1805 return HV_STATUS_INVALID_PORT_ID; 1806 1807 eventfd_signal(eventfd, 1); 1808 return HV_STATUS_SUCCESS; 1809 } 1810 1811 int kvm_hv_hypercall(struct kvm_vcpu *vcpu) 1812 { 1813 u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS; 1814 uint16_t code, rep_idx, rep_cnt; 1815 bool fast, rep; 1816 1817 /* 1818 * hypercall generates UD from non zero cpl and real mode 1819 * per HYPER-V spec 1820 */ 1821 if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) { 1822 kvm_queue_exception(vcpu, UD_VECTOR); 1823 return 1; 1824 } 1825 1826 #ifdef CONFIG_X86_64 1827 if (is_64_bit_mode(vcpu)) { 1828 param = kvm_rcx_read(vcpu); 1829 ingpa = kvm_rdx_read(vcpu); 1830 outgpa = kvm_r8_read(vcpu); 1831 } else 1832 #endif 1833 { 1834 param = ((u64)kvm_rdx_read(vcpu) << 32) | 1835 (kvm_rax_read(vcpu) & 0xffffffff); 1836 ingpa = ((u64)kvm_rbx_read(vcpu) << 32) | 1837 (kvm_rcx_read(vcpu) & 0xffffffff); 1838 outgpa = ((u64)kvm_rdi_read(vcpu) << 32) | 1839 (kvm_rsi_read(vcpu) & 0xffffffff); 1840 } 1841 1842 code = param & 0xffff; 1843 fast = !!(param & HV_HYPERCALL_FAST_BIT); 1844 rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff; 1845 rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff; 1846 rep = !!(rep_cnt || rep_idx); 1847 1848 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); 1849 1850 switch (code) { 1851 case HVCALL_NOTIFY_LONG_SPIN_WAIT: 1852 if (unlikely(rep)) { 1853 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1854 break; 1855 } 1856 kvm_vcpu_on_spin(vcpu, true); 1857 break; 1858 case HVCALL_SIGNAL_EVENT: 1859 if (unlikely(rep)) { 1860 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1861 break; 1862 } 1863 ret = kvm_hvcall_signal_event(vcpu, fast, ingpa); 1864 if (ret != HV_STATUS_INVALID_PORT_ID) 1865 break; 1866 fallthrough; /* maybe userspace knows this conn_id */ 1867 case HVCALL_POST_MESSAGE: 1868 /* don't bother userspace if it has no way to handle it */ 1869 if (unlikely(rep || !to_hv_synic(vcpu)->active)) { 1870 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1871 break; 1872 } 1873 vcpu->run->exit_reason = KVM_EXIT_HYPERV; 1874 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; 1875 vcpu->run->hyperv.u.hcall.input = param; 1876 vcpu->run->hyperv.u.hcall.params[0] = ingpa; 1877 vcpu->run->hyperv.u.hcall.params[1] = outgpa; 1878 vcpu->arch.complete_userspace_io = 1879 kvm_hv_hypercall_complete_userspace; 1880 return 0; 1881 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: 1882 if (unlikely(fast || !rep_cnt || rep_idx)) { 1883 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1884 break; 1885 } 1886 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false); 1887 break; 1888 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: 1889 if (unlikely(fast || rep)) { 1890 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1891 break; 1892 } 1893 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false); 1894 break; 1895 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: 1896 if (unlikely(fast || !rep_cnt || rep_idx)) { 1897 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1898 break; 1899 } 1900 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true); 1901 break; 1902 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: 1903 if (unlikely(fast || rep)) { 1904 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1905 break; 1906 } 1907 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true); 1908 break; 1909 case HVCALL_SEND_IPI: 1910 if (unlikely(rep)) { 1911 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1912 break; 1913 } 1914 ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast); 1915 break; 1916 case HVCALL_SEND_IPI_EX: 1917 if (unlikely(fast || rep)) { 1918 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 1919 break; 1920 } 1921 ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false); 1922 break; 1923 case HVCALL_POST_DEBUG_DATA: 1924 case HVCALL_RETRIEVE_DEBUG_DATA: 1925 if (unlikely(fast)) { 1926 ret = HV_STATUS_INVALID_PARAMETER; 1927 break; 1928 } 1929 fallthrough; 1930 case HVCALL_RESET_DEBUG_SESSION: { 1931 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 1932 1933 if (!kvm_hv_is_syndbg_enabled(vcpu)) { 1934 ret = HV_STATUS_INVALID_HYPERCALL_CODE; 1935 break; 1936 } 1937 1938 if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) { 1939 ret = HV_STATUS_OPERATION_DENIED; 1940 break; 1941 } 1942 vcpu->run->exit_reason = KVM_EXIT_HYPERV; 1943 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; 1944 vcpu->run->hyperv.u.hcall.input = param; 1945 vcpu->run->hyperv.u.hcall.params[0] = ingpa; 1946 vcpu->run->hyperv.u.hcall.params[1] = outgpa; 1947 vcpu->arch.complete_userspace_io = 1948 kvm_hv_hypercall_complete_userspace; 1949 return 0; 1950 } 1951 default: 1952 ret = HV_STATUS_INVALID_HYPERCALL_CODE; 1953 break; 1954 } 1955 1956 return kvm_hv_hypercall_complete(vcpu, ret); 1957 } 1958 1959 void kvm_hv_init_vm(struct kvm *kvm) 1960 { 1961 struct kvm_hv *hv = to_kvm_hv(kvm); 1962 1963 mutex_init(&hv->hv_lock); 1964 idr_init(&hv->conn_to_evt); 1965 } 1966 1967 void kvm_hv_destroy_vm(struct kvm *kvm) 1968 { 1969 struct kvm_hv *hv = to_kvm_hv(kvm); 1970 struct eventfd_ctx *eventfd; 1971 int i; 1972 1973 idr_for_each_entry(&hv->conn_to_evt, eventfd, i) 1974 eventfd_ctx_put(eventfd); 1975 idr_destroy(&hv->conn_to_evt); 1976 } 1977 1978 static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd) 1979 { 1980 struct kvm_hv *hv = to_kvm_hv(kvm); 1981 struct eventfd_ctx *eventfd; 1982 int ret; 1983 1984 eventfd = eventfd_ctx_fdget(fd); 1985 if (IS_ERR(eventfd)) 1986 return PTR_ERR(eventfd); 1987 1988 mutex_lock(&hv->hv_lock); 1989 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1, 1990 GFP_KERNEL_ACCOUNT); 1991 mutex_unlock(&hv->hv_lock); 1992 1993 if (ret >= 0) 1994 return 0; 1995 1996 if (ret == -ENOSPC) 1997 ret = -EEXIST; 1998 eventfd_ctx_put(eventfd); 1999 return ret; 2000 } 2001 2002 static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id) 2003 { 2004 struct kvm_hv *hv = to_kvm_hv(kvm); 2005 struct eventfd_ctx *eventfd; 2006 2007 mutex_lock(&hv->hv_lock); 2008 eventfd = idr_remove(&hv->conn_to_evt, conn_id); 2009 mutex_unlock(&hv->hv_lock); 2010 2011 if (!eventfd) 2012 return -ENOENT; 2013 2014 synchronize_srcu(&kvm->srcu); 2015 eventfd_ctx_put(eventfd); 2016 return 0; 2017 } 2018 2019 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args) 2020 { 2021 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) || 2022 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK)) 2023 return -EINVAL; 2024 2025 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN) 2026 return kvm_hv_eventfd_deassign(kvm, args->conn_id); 2027 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd); 2028 } 2029 2030 int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, 2031 struct kvm_cpuid_entry2 __user *entries) 2032 { 2033 uint16_t evmcs_ver = 0; 2034 struct kvm_cpuid_entry2 cpuid_entries[] = { 2035 { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS }, 2036 { .function = HYPERV_CPUID_INTERFACE }, 2037 { .function = HYPERV_CPUID_VERSION }, 2038 { .function = HYPERV_CPUID_FEATURES }, 2039 { .function = HYPERV_CPUID_ENLIGHTMENT_INFO }, 2040 { .function = HYPERV_CPUID_IMPLEMENT_LIMITS }, 2041 { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS }, 2042 { .function = HYPERV_CPUID_SYNDBG_INTERFACE }, 2043 { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES }, 2044 { .function = HYPERV_CPUID_NESTED_FEATURES }, 2045 }; 2046 int i, nent = ARRAY_SIZE(cpuid_entries); 2047 2048 if (kvm_x86_ops.nested_ops->get_evmcs_version) 2049 evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu); 2050 2051 /* Skip NESTED_FEATURES if eVMCS is not supported */ 2052 if (!evmcs_ver) 2053 --nent; 2054 2055 if (cpuid->nent < nent) 2056 return -E2BIG; 2057 2058 if (cpuid->nent > nent) 2059 cpuid->nent = nent; 2060 2061 for (i = 0; i < nent; i++) { 2062 struct kvm_cpuid_entry2 *ent = &cpuid_entries[i]; 2063 u32 signature[3]; 2064 2065 switch (ent->function) { 2066 case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS: 2067 memcpy(signature, "Linux KVM Hv", 12); 2068 2069 ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES; 2070 ent->ebx = signature[0]; 2071 ent->ecx = signature[1]; 2072 ent->edx = signature[2]; 2073 break; 2074 2075 case HYPERV_CPUID_INTERFACE: 2076 ent->eax = HYPERV_CPUID_SIGNATURE_EAX; 2077 break; 2078 2079 case HYPERV_CPUID_VERSION: 2080 /* 2081 * We implement some Hyper-V 2016 functions so let's use 2082 * this version. 2083 */ 2084 ent->eax = 0x00003839; 2085 ent->ebx = 0x000A0000; 2086 break; 2087 2088 case HYPERV_CPUID_FEATURES: 2089 ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE; 2090 ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE; 2091 ent->eax |= HV_MSR_SYNIC_AVAILABLE; 2092 ent->eax |= HV_MSR_SYNTIMER_AVAILABLE; 2093 ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE; 2094 ent->eax |= HV_MSR_HYPERCALL_AVAILABLE; 2095 ent->eax |= HV_MSR_VP_INDEX_AVAILABLE; 2096 ent->eax |= HV_MSR_RESET_AVAILABLE; 2097 ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE; 2098 ent->eax |= HV_ACCESS_FREQUENCY_MSRS; 2099 ent->eax |= HV_ACCESS_REENLIGHTENMENT; 2100 2101 ent->ebx |= HV_POST_MESSAGES; 2102 ent->ebx |= HV_SIGNAL_EVENTS; 2103 2104 ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; 2105 ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; 2106 2107 ent->ebx |= HV_DEBUGGING; 2108 ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE; 2109 ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE; 2110 2111 /* 2112 * Direct Synthetic timers only make sense with in-kernel 2113 * LAPIC 2114 */ 2115 if (!vcpu || lapic_in_kernel(vcpu)) 2116 ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; 2117 2118 break; 2119 2120 case HYPERV_CPUID_ENLIGHTMENT_INFO: 2121 ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; 2122 ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; 2123 ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; 2124 ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; 2125 ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; 2126 if (evmcs_ver) 2127 ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 2128 if (!cpu_smt_possible()) 2129 ent->eax |= HV_X64_NO_NONARCH_CORESHARING; 2130 /* 2131 * Default number of spinlock retry attempts, matches 2132 * HyperV 2016. 2133 */ 2134 ent->ebx = 0x00000FFF; 2135 2136 break; 2137 2138 case HYPERV_CPUID_IMPLEMENT_LIMITS: 2139 /* Maximum number of virtual processors */ 2140 ent->eax = KVM_MAX_VCPUS; 2141 /* 2142 * Maximum number of logical processors, matches 2143 * HyperV 2016. 2144 */ 2145 ent->ebx = 64; 2146 2147 break; 2148 2149 case HYPERV_CPUID_NESTED_FEATURES: 2150 ent->eax = evmcs_ver; 2151 2152 break; 2153 2154 case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS: 2155 memcpy(signature, "Linux KVM Hv", 12); 2156 2157 ent->eax = 0; 2158 ent->ebx = signature[0]; 2159 ent->ecx = signature[1]; 2160 ent->edx = signature[2]; 2161 break; 2162 2163 case HYPERV_CPUID_SYNDBG_INTERFACE: 2164 memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12); 2165 ent->eax = signature[0]; 2166 break; 2167 2168 case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES: 2169 ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; 2170 break; 2171 2172 default: 2173 break; 2174 } 2175 } 2176 2177 if (copy_to_user(entries, cpuid_entries, 2178 nent * sizeof(struct kvm_cpuid_entry2))) 2179 return -EFAULT; 2180 2181 return 0; 2182 } 2183