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