Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1 | /* |
| 2 | * KVM Microsoft Hyper-V emulation |
| 3 | * |
| 4 | * derived from arch/x86/kvm/x86.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
| 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 10 | * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> |
| 11 | * |
| 12 | * Authors: |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * Amit Shah <amit.shah@qumranet.com> |
| 16 | * Ben-Ami Yassour <benami@il.ibm.com> |
| 17 | * Andrey Smetanin <asmetanin@virtuozzo.com> |
| 18 | * |
| 19 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 20 | * the COPYING file in the top-level directory. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "x86.h" |
| 25 | #include "lapic.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 26 | #include "ioapic.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 27 | #include "hyperv.h" |
| 28 | |
| 29 | #include <linux/kvm_host.h> |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 30 | #include <asm/apicdef.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 31 | #include <trace/events/kvm.h> |
| 32 | |
| 33 | #include "trace.h" |
| 34 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 35 | static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) |
| 36 | { |
| 37 | return atomic64_read(&synic->sint[sint]); |
| 38 | } |
| 39 | |
| 40 | static inline int synic_get_sint_vector(u64 sint_value) |
| 41 | { |
| 42 | if (sint_value & HV_SYNIC_SINT_MASKED) |
| 43 | return -1; |
| 44 | return sint_value & HV_SYNIC_SINT_VECTOR_MASK; |
| 45 | } |
| 46 | |
| 47 | static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, |
| 48 | int vector) |
| 49 | { |
| 50 | int i; |
| 51 | |
| 52 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 53 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, |
| 60 | int vector) |
| 61 | { |
| 62 | int i; |
| 63 | u64 sint_value; |
| 64 | |
| 65 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 66 | sint_value = synic_read_sint(synic, i); |
| 67 | if (synic_get_sint_vector(sint_value) == vector && |
| 68 | sint_value & HV_SYNIC_SINT_AUTO_EOI) |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data) |
| 75 | { |
| 76 | int vector; |
| 77 | |
| 78 | vector = data & HV_SYNIC_SINT_VECTOR_MASK; |
| 79 | if (vector < 16) |
| 80 | return 1; |
| 81 | /* |
| 82 | * Guest may configure multiple SINTs to use the same vector, so |
| 83 | * we maintain a bitmap of vectors handled by synic, and a |
| 84 | * bitmap of vectors with auto-eoi behavior. The bitmaps are |
| 85 | * updated here, and atomically queried on fast paths. |
| 86 | */ |
| 87 | |
| 88 | atomic64_set(&synic->sint[sint], data); |
| 89 | |
| 90 | if (synic_has_vector_connected(synic, vector)) |
| 91 | __set_bit(vector, synic->vec_bitmap); |
| 92 | else |
| 93 | __clear_bit(vector, synic->vec_bitmap); |
| 94 | |
| 95 | if (synic_has_vector_auto_eoi(synic, vector)) |
| 96 | __set_bit(vector, synic->auto_eoi_bitmap); |
| 97 | else |
| 98 | __clear_bit(vector, synic->auto_eoi_bitmap); |
| 99 | |
| 100 | /* Load SynIC vectors into EOI exit bitmap */ |
| 101 | kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id) |
| 106 | { |
| 107 | struct kvm_vcpu *vcpu; |
| 108 | struct kvm_vcpu_hv_synic *synic; |
| 109 | |
| 110 | if (vcpu_id >= atomic_read(&kvm->online_vcpus)) |
| 111 | return NULL; |
| 112 | vcpu = kvm_get_vcpu(kvm, vcpu_id); |
| 113 | if (!vcpu) |
| 114 | return NULL; |
| 115 | synic = vcpu_to_synic(vcpu); |
| 116 | return (synic->active) ? synic : NULL; |
| 117 | } |
| 118 | |
| 119 | static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) |
| 120 | { |
| 121 | struct kvm *kvm = vcpu->kvm; |
| 122 | int gsi, idx; |
| 123 | |
| 124 | vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint); |
| 125 | |
| 126 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 127 | gsi = atomic_read(&vcpu_to_synic(vcpu)->sint_to_gsi[sint]); |
| 128 | if (gsi != -1) |
| 129 | kvm_notify_acked_gsi(kvm, gsi); |
| 130 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 131 | } |
| 132 | |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 133 | static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) |
| 134 | { |
| 135 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 136 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
| 137 | |
| 138 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; |
| 139 | hv_vcpu->exit.u.synic.msr = msr; |
| 140 | hv_vcpu->exit.u.synic.control = synic->control; |
| 141 | hv_vcpu->exit.u.synic.evt_page = synic->evt_page; |
| 142 | hv_vcpu->exit.u.synic.msg_page = synic->msg_page; |
| 143 | |
| 144 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 145 | } |
| 146 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 147 | static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, |
| 148 | u32 msr, u64 data, bool host) |
| 149 | { |
| 150 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 151 | int ret; |
| 152 | |
| 153 | if (!synic->active) |
| 154 | return 1; |
| 155 | |
| 156 | vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n", |
| 157 | msr, data, host); |
| 158 | ret = 0; |
| 159 | switch (msr) { |
| 160 | case HV_X64_MSR_SCONTROL: |
| 161 | synic->control = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 162 | if (!host) |
| 163 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 164 | break; |
| 165 | case HV_X64_MSR_SVERSION: |
| 166 | if (!host) { |
| 167 | ret = 1; |
| 168 | break; |
| 169 | } |
| 170 | synic->version = data; |
| 171 | break; |
| 172 | case HV_X64_MSR_SIEFP: |
| 173 | if (data & HV_SYNIC_SIEFP_ENABLE) |
| 174 | if (kvm_clear_guest(vcpu->kvm, |
| 175 | data & PAGE_MASK, PAGE_SIZE)) { |
| 176 | ret = 1; |
| 177 | break; |
| 178 | } |
| 179 | synic->evt_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 180 | if (!host) |
| 181 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 182 | break; |
| 183 | case HV_X64_MSR_SIMP: |
| 184 | if (data & HV_SYNIC_SIMP_ENABLE) |
| 185 | if (kvm_clear_guest(vcpu->kvm, |
| 186 | data & PAGE_MASK, PAGE_SIZE)) { |
| 187 | ret = 1; |
| 188 | break; |
| 189 | } |
| 190 | synic->msg_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 191 | if (!host) |
| 192 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 193 | break; |
| 194 | case HV_X64_MSR_EOM: { |
| 195 | int i; |
| 196 | |
| 197 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 198 | kvm_hv_notify_acked_sint(vcpu, i); |
| 199 | break; |
| 200 | } |
| 201 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 202 | ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data); |
| 203 | break; |
| 204 | default: |
| 205 | ret = 1; |
| 206 | break; |
| 207 | } |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata) |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | if (!synic->active) |
| 216 | return 1; |
| 217 | |
| 218 | ret = 0; |
| 219 | switch (msr) { |
| 220 | case HV_X64_MSR_SCONTROL: |
| 221 | *pdata = synic->control; |
| 222 | break; |
| 223 | case HV_X64_MSR_SVERSION: |
| 224 | *pdata = synic->version; |
| 225 | break; |
| 226 | case HV_X64_MSR_SIEFP: |
| 227 | *pdata = synic->evt_page; |
| 228 | break; |
| 229 | case HV_X64_MSR_SIMP: |
| 230 | *pdata = synic->msg_page; |
| 231 | break; |
| 232 | case HV_X64_MSR_EOM: |
| 233 | *pdata = 0; |
| 234 | break; |
| 235 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 236 | *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); |
| 237 | break; |
| 238 | default: |
| 239 | ret = 1; |
| 240 | break; |
| 241 | } |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) |
| 246 | { |
| 247 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 248 | struct kvm_lapic_irq irq; |
| 249 | int ret, vector; |
| 250 | |
| 251 | if (sint >= ARRAY_SIZE(synic->sint)) |
| 252 | return -EINVAL; |
| 253 | |
| 254 | vector = synic_get_sint_vector(synic_read_sint(synic, sint)); |
| 255 | if (vector < 0) |
| 256 | return -ENOENT; |
| 257 | |
| 258 | memset(&irq, 0, sizeof(irq)); |
| 259 | irq.dest_id = kvm_apic_id(vcpu->arch.apic); |
| 260 | irq.dest_mode = APIC_DEST_PHYSICAL; |
| 261 | irq.delivery_mode = APIC_DM_FIXED; |
| 262 | irq.vector = vector; |
| 263 | irq.level = 1; |
| 264 | |
| 265 | ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL); |
| 266 | vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint) |
| 271 | { |
| 272 | struct kvm_vcpu_hv_synic *synic; |
| 273 | |
| 274 | synic = synic_get(kvm, vcpu_id); |
| 275 | if (!synic) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | return synic_set_irq(synic, sint); |
| 279 | } |
| 280 | |
| 281 | void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) |
| 282 | { |
| 283 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 284 | int i; |
| 285 | |
| 286 | vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector); |
| 287 | |
| 288 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 289 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 290 | kvm_hv_notify_acked_sint(vcpu, i); |
| 291 | } |
| 292 | |
| 293 | static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi) |
| 294 | { |
| 295 | struct kvm_vcpu_hv_synic *synic; |
| 296 | |
| 297 | synic = synic_get(kvm, vcpu_id); |
| 298 | if (!synic) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | atomic_set(&synic->sint_to_gsi[sint], gsi); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | void kvm_hv_irq_routing_update(struct kvm *kvm) |
| 309 | { |
| 310 | struct kvm_irq_routing_table *irq_rt; |
| 311 | struct kvm_kernel_irq_routing_entry *e; |
| 312 | u32 gsi; |
| 313 | |
| 314 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 315 | lockdep_is_held(&kvm->irq_lock)); |
| 316 | |
| 317 | for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { |
| 318 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 319 | if (e->type == KVM_IRQ_ROUTING_HV_SINT) |
| 320 | kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, |
| 321 | e->hv_sint.sint, gsi); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void synic_init(struct kvm_vcpu_hv_synic *synic) |
| 327 | { |
| 328 | int i; |
| 329 | |
| 330 | memset(synic, 0, sizeof(*synic)); |
| 331 | synic->version = HV_SYNIC_VERSION_1; |
| 332 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 333 | atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); |
| 334 | atomic_set(&synic->sint_to_gsi[i], -1); |
| 335 | } |
| 336 | } |
| 337 | |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame^] | 338 | static u64 get_time_ref_counter(struct kvm *kvm) |
| 339 | { |
| 340 | return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100); |
| 341 | } |
| 342 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 343 | void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) |
| 344 | { |
| 345 | synic_init(vcpu_to_synic(vcpu)); |
| 346 | } |
| 347 | |
| 348 | int kvm_hv_activate_synic(struct kvm_vcpu *vcpu) |
| 349 | { |
| 350 | /* |
| 351 | * Hyper-V SynIC auto EOI SINT's are |
| 352 | * not compatible with APICV, so deactivate APICV |
| 353 | */ |
| 354 | kvm_vcpu_deactivate_apicv(vcpu); |
| 355 | vcpu_to_synic(vcpu)->active = true; |
| 356 | return 0; |
| 357 | } |
| 358 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 359 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 360 | { |
| 361 | bool r = false; |
| 362 | |
| 363 | switch (msr) { |
| 364 | case HV_X64_MSR_GUEST_OS_ID: |
| 365 | case HV_X64_MSR_HYPERCALL: |
| 366 | case HV_X64_MSR_REFERENCE_TSC: |
| 367 | case HV_X64_MSR_TIME_REF_COUNT: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 368 | case HV_X64_MSR_CRASH_CTL: |
| 369 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 370 | case HV_X64_MSR_RESET: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 371 | r = true; |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | return r; |
| 376 | } |
| 377 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 378 | static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, |
| 379 | u32 index, u64 *pdata) |
| 380 | { |
| 381 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 382 | |
| 383 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | *pdata = hv->hv_crash_param[index]; |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) |
| 391 | { |
| 392 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 393 | |
| 394 | *pdata = hv->hv_crash_ctl; |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) |
| 399 | { |
| 400 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 401 | |
| 402 | if (host) |
| 403 | hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY; |
| 404 | |
| 405 | if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) { |
| 406 | |
| 407 | vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", |
| 408 | hv->hv_crash_param[0], |
| 409 | hv->hv_crash_param[1], |
| 410 | hv->hv_crash_param[2], |
| 411 | hv->hv_crash_param[3], |
| 412 | hv->hv_crash_param[4]); |
| 413 | |
| 414 | /* Send notification about crash to user space */ |
| 415 | kvm_make_request(KVM_REQ_HV_CRASH, vcpu); |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, |
| 422 | u32 index, u64 data) |
| 423 | { |
| 424 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 425 | |
| 426 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | hv->hv_crash_param[index] = data; |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, |
| 434 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 435 | { |
| 436 | struct kvm *kvm = vcpu->kvm; |
| 437 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 438 | |
| 439 | switch (msr) { |
| 440 | case HV_X64_MSR_GUEST_OS_ID: |
| 441 | hv->hv_guest_os_id = data; |
| 442 | /* setting guest os id to zero disables hypercall page */ |
| 443 | if (!hv->hv_guest_os_id) |
| 444 | hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 445 | break; |
| 446 | case HV_X64_MSR_HYPERCALL: { |
| 447 | u64 gfn; |
| 448 | unsigned long addr; |
| 449 | u8 instructions[4]; |
| 450 | |
| 451 | /* if guest os id is not set hypercall should remain disabled */ |
| 452 | if (!hv->hv_guest_os_id) |
| 453 | break; |
| 454 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 455 | hv->hv_hypercall = data; |
| 456 | break; |
| 457 | } |
| 458 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 459 | addr = gfn_to_hva(kvm, gfn); |
| 460 | if (kvm_is_error_hva(addr)) |
| 461 | return 1; |
| 462 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 463 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 464 | if (__copy_to_user((void __user *)addr, instructions, 4)) |
| 465 | return 1; |
| 466 | hv->hv_hypercall = data; |
| 467 | mark_page_dirty(kvm, gfn); |
| 468 | break; |
| 469 | } |
| 470 | case HV_X64_MSR_REFERENCE_TSC: { |
| 471 | u64 gfn; |
| 472 | HV_REFERENCE_TSC_PAGE tsc_ref; |
| 473 | |
| 474 | memset(&tsc_ref, 0, sizeof(tsc_ref)); |
| 475 | hv->hv_tsc_page = data; |
| 476 | if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 477 | break; |
| 478 | gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 479 | if (kvm_write_guest( |
| 480 | kvm, |
| 481 | gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT, |
| 482 | &tsc_ref, sizeof(tsc_ref))) |
| 483 | return 1; |
| 484 | mark_page_dirty(kvm, gfn); |
| 485 | break; |
| 486 | } |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 487 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 488 | return kvm_hv_msr_set_crash_data(vcpu, |
| 489 | msr - HV_X64_MSR_CRASH_P0, |
| 490 | data); |
| 491 | case HV_X64_MSR_CRASH_CTL: |
| 492 | return kvm_hv_msr_set_crash_ctl(vcpu, data, host); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 493 | case HV_X64_MSR_RESET: |
| 494 | if (data == 1) { |
| 495 | vcpu_debug(vcpu, "hyper-v reset requested\n"); |
| 496 | kvm_make_request(KVM_REQ_HV_RESET, vcpu); |
| 497 | } |
| 498 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 499 | default: |
| 500 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 501 | msr, data); |
| 502 | return 1; |
| 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 507 | /* Calculate cpu time spent by current task in 100ns units */ |
| 508 | static u64 current_task_runtime_100ns(void) |
| 509 | { |
| 510 | cputime_t utime, stime; |
| 511 | |
| 512 | task_cputime_adjusted(current, &utime, &stime); |
| 513 | return div_u64(cputime_to_nsecs(utime + stime), 100); |
| 514 | } |
| 515 | |
| 516 | static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 517 | { |
| 518 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 519 | |
| 520 | switch (msr) { |
| 521 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 522 | u64 gfn; |
| 523 | unsigned long addr; |
| 524 | |
| 525 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 526 | hv->hv_vapic = data; |
| 527 | if (kvm_lapic_enable_pv_eoi(vcpu, 0)) |
| 528 | return 1; |
| 529 | break; |
| 530 | } |
| 531 | gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT; |
| 532 | addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); |
| 533 | if (kvm_is_error_hva(addr)) |
| 534 | return 1; |
| 535 | if (__clear_user((void __user *)addr, PAGE_SIZE)) |
| 536 | return 1; |
| 537 | hv->hv_vapic = data; |
| 538 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 539 | if (kvm_lapic_enable_pv_eoi(vcpu, |
| 540 | gfn_to_gpa(gfn) | KVM_MSR_ENABLED)) |
| 541 | return 1; |
| 542 | break; |
| 543 | } |
| 544 | case HV_X64_MSR_EOI: |
| 545 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 546 | case HV_X64_MSR_ICR: |
| 547 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 548 | case HV_X64_MSR_TPR: |
| 549 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 550 | case HV_X64_MSR_VP_RUNTIME: |
| 551 | if (!host) |
| 552 | return 1; |
| 553 | hv->runtime_offset = data - current_task_runtime_100ns(); |
| 554 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 555 | case HV_X64_MSR_SCONTROL: |
| 556 | case HV_X64_MSR_SVERSION: |
| 557 | case HV_X64_MSR_SIEFP: |
| 558 | case HV_X64_MSR_SIMP: |
| 559 | case HV_X64_MSR_EOM: |
| 560 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 561 | return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 562 | default: |
| 563 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 564 | msr, data); |
| 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 572 | { |
| 573 | u64 data = 0; |
| 574 | struct kvm *kvm = vcpu->kvm; |
| 575 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 576 | |
| 577 | switch (msr) { |
| 578 | case HV_X64_MSR_GUEST_OS_ID: |
| 579 | data = hv->hv_guest_os_id; |
| 580 | break; |
| 581 | case HV_X64_MSR_HYPERCALL: |
| 582 | data = hv->hv_hypercall; |
| 583 | break; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame^] | 584 | case HV_X64_MSR_TIME_REF_COUNT: |
| 585 | data = get_time_ref_counter(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 586 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 587 | case HV_X64_MSR_REFERENCE_TSC: |
| 588 | data = hv->hv_tsc_page; |
| 589 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 590 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 591 | return kvm_hv_msr_get_crash_data(vcpu, |
| 592 | msr - HV_X64_MSR_CRASH_P0, |
| 593 | pdata); |
| 594 | case HV_X64_MSR_CRASH_CTL: |
| 595 | return kvm_hv_msr_get_crash_ctl(vcpu, pdata); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 596 | case HV_X64_MSR_RESET: |
| 597 | data = 0; |
| 598 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 599 | default: |
| 600 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | *pdata = data; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 609 | { |
| 610 | u64 data = 0; |
| 611 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 612 | |
| 613 | switch (msr) { |
| 614 | case HV_X64_MSR_VP_INDEX: { |
| 615 | int r; |
| 616 | struct kvm_vcpu *v; |
| 617 | |
| 618 | kvm_for_each_vcpu(r, v, vcpu->kvm) { |
| 619 | if (v == vcpu) { |
| 620 | data = r; |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | break; |
| 625 | } |
| 626 | case HV_X64_MSR_EOI: |
| 627 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 628 | case HV_X64_MSR_ICR: |
| 629 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 630 | case HV_X64_MSR_TPR: |
| 631 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
| 632 | case HV_X64_MSR_APIC_ASSIST_PAGE: |
| 633 | data = hv->hv_vapic; |
| 634 | break; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 635 | case HV_X64_MSR_VP_RUNTIME: |
| 636 | data = current_task_runtime_100ns() + hv->runtime_offset; |
| 637 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 638 | case HV_X64_MSR_SCONTROL: |
| 639 | case HV_X64_MSR_SVERSION: |
| 640 | case HV_X64_MSR_SIEFP: |
| 641 | case HV_X64_MSR_SIMP: |
| 642 | case HV_X64_MSR_EOM: |
| 643 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 644 | return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 645 | default: |
| 646 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 647 | return 1; |
| 648 | } |
| 649 | *pdata = data; |
| 650 | return 0; |
| 651 | } |
| 652 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 653 | int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 654 | { |
| 655 | if (kvm_hv_msr_partition_wide(msr)) { |
| 656 | int r; |
| 657 | |
| 658 | mutex_lock(&vcpu->kvm->lock); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 659 | r = kvm_hv_set_msr_pw(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 660 | mutex_unlock(&vcpu->kvm->lock); |
| 661 | return r; |
| 662 | } else |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 663 | return kvm_hv_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 667 | { |
| 668 | if (kvm_hv_msr_partition_wide(msr)) { |
| 669 | int r; |
| 670 | |
| 671 | mutex_lock(&vcpu->kvm->lock); |
| 672 | r = kvm_hv_get_msr_pw(vcpu, msr, pdata); |
| 673 | mutex_unlock(&vcpu->kvm->lock); |
| 674 | return r; |
| 675 | } else |
| 676 | return kvm_hv_get_msr(vcpu, msr, pdata); |
| 677 | } |
| 678 | |
| 679 | bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 680 | { |
| 681 | return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 682 | } |
| 683 | |
| 684 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 685 | { |
| 686 | u64 param, ingpa, outgpa, ret; |
| 687 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 688 | bool fast, longmode; |
| 689 | |
| 690 | /* |
| 691 | * hypercall generates UD from non zero cpl and real mode |
| 692 | * per HYPER-V spec |
| 693 | */ |
| 694 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
| 695 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | longmode = is_64_bit_mode(vcpu); |
| 700 | |
| 701 | if (!longmode) { |
| 702 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 703 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 704 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 705 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 706 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 707 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
| 708 | } |
| 709 | #ifdef CONFIG_X86_64 |
| 710 | else { |
| 711 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 712 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 713 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 714 | } |
| 715 | #endif |
| 716 | |
| 717 | code = param & 0xffff; |
| 718 | fast = (param >> 16) & 0x1; |
| 719 | rep_cnt = (param >> 32) & 0xfff; |
| 720 | rep_idx = (param >> 48) & 0xfff; |
| 721 | |
| 722 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 723 | |
| 724 | switch (code) { |
| 725 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 726 | kvm_vcpu_on_spin(vcpu); |
| 727 | break; |
| 728 | default: |
| 729 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 734 | if (longmode) { |
| 735 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 736 | } else { |
| 737 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 738 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 739 | } |
| 740 | |
| 741 | return 1; |
| 742 | } |