Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1 | /* |
| 2 | * KVM Microsoft Hyper-V emulation |
| 3 | * |
| 4 | * derived from arch/x86/kvm/x86.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
| 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 10 | * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> |
| 11 | * |
| 12 | * Authors: |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * Amit Shah <amit.shah@qumranet.com> |
| 16 | * Ben-Ami Yassour <benami@il.ibm.com> |
| 17 | * Andrey Smetanin <asmetanin@virtuozzo.com> |
| 18 | * |
| 19 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 20 | * the COPYING file in the top-level directory. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "x86.h" |
| 25 | #include "lapic.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 26 | #include "ioapic.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 27 | #include "hyperv.h" |
| 28 | |
| 29 | #include <linux/kvm_host.h> |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 30 | #include <linux/highmem.h> |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 31 | #include <asm/apicdef.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 32 | #include <trace/events/kvm.h> |
| 33 | |
| 34 | #include "trace.h" |
| 35 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 36 | static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) |
| 37 | { |
| 38 | return atomic64_read(&synic->sint[sint]); |
| 39 | } |
| 40 | |
| 41 | static inline int synic_get_sint_vector(u64 sint_value) |
| 42 | { |
| 43 | if (sint_value & HV_SYNIC_SINT_MASKED) |
| 44 | return -1; |
| 45 | return sint_value & HV_SYNIC_SINT_VECTOR_MASK; |
| 46 | } |
| 47 | |
| 48 | static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, |
| 49 | int vector) |
| 50 | { |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 54 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, |
| 61 | int vector) |
| 62 | { |
| 63 | int i; |
| 64 | u64 sint_value; |
| 65 | |
| 66 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 67 | sint_value = synic_read_sint(synic, i); |
| 68 | if (synic_get_sint_vector(sint_value) == vector && |
| 69 | sint_value & HV_SYNIC_SINT_AUTO_EOI) |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data) |
| 76 | { |
| 77 | int vector; |
| 78 | |
| 79 | vector = data & HV_SYNIC_SINT_VECTOR_MASK; |
| 80 | if (vector < 16) |
| 81 | return 1; |
| 82 | /* |
| 83 | * Guest may configure multiple SINTs to use the same vector, so |
| 84 | * we maintain a bitmap of vectors handled by synic, and a |
| 85 | * bitmap of vectors with auto-eoi behavior. The bitmaps are |
| 86 | * updated here, and atomically queried on fast paths. |
| 87 | */ |
| 88 | |
| 89 | atomic64_set(&synic->sint[sint], data); |
| 90 | |
| 91 | if (synic_has_vector_connected(synic, vector)) |
| 92 | __set_bit(vector, synic->vec_bitmap); |
| 93 | else |
| 94 | __clear_bit(vector, synic->vec_bitmap); |
| 95 | |
| 96 | if (synic_has_vector_auto_eoi(synic, vector)) |
| 97 | __set_bit(vector, synic->auto_eoi_bitmap); |
| 98 | else |
| 99 | __clear_bit(vector, synic->auto_eoi_bitmap); |
| 100 | |
| 101 | /* Load SynIC vectors into EOI exit bitmap */ |
| 102 | kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id) |
| 107 | { |
| 108 | struct kvm_vcpu *vcpu; |
| 109 | struct kvm_vcpu_hv_synic *synic; |
| 110 | |
| 111 | if (vcpu_id >= atomic_read(&kvm->online_vcpus)) |
| 112 | return NULL; |
| 113 | vcpu = kvm_get_vcpu(kvm, vcpu_id); |
| 114 | if (!vcpu) |
| 115 | return NULL; |
| 116 | synic = vcpu_to_synic(vcpu); |
| 117 | return (synic->active) ? synic : NULL; |
| 118 | } |
| 119 | |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 120 | static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic, |
| 121 | u32 sint) |
| 122 | { |
| 123 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 124 | struct page *page; |
| 125 | gpa_t gpa; |
| 126 | struct hv_message *msg; |
| 127 | struct hv_message_page *msg_page; |
| 128 | |
| 129 | gpa = synic->msg_page & PAGE_MASK; |
| 130 | page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); |
| 131 | if (is_error_page(page)) { |
| 132 | vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n", |
| 133 | gpa); |
| 134 | return; |
| 135 | } |
| 136 | msg_page = kmap_atomic(page); |
| 137 | |
| 138 | msg = &msg_page->sint_message[sint]; |
| 139 | msg->header.message_flags.msg_pending = 0; |
| 140 | |
| 141 | kunmap_atomic(msg_page); |
| 142 | kvm_release_page_dirty(page); |
| 143 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
| 144 | } |
| 145 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 146 | static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) |
| 147 | { |
| 148 | struct kvm *kvm = vcpu->kvm; |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 149 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 150 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 151 | struct kvm_vcpu_hv_stimer *stimer; |
| 152 | int gsi, idx, stimers_pending; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 153 | |
| 154 | vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint); |
| 155 | |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 156 | if (synic->msg_page & HV_SYNIC_SIMP_ENABLE) |
| 157 | synic_clear_sint_msg_pending(synic, sint); |
| 158 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 159 | /* Try to deliver pending Hyper-V SynIC timers messages */ |
| 160 | stimers_pending = 0; |
| 161 | for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { |
| 162 | stimer = &hv_vcpu->stimer[idx]; |
| 163 | if (stimer->msg_pending && |
| 164 | (stimer->config & HV_STIMER_ENABLE) && |
| 165 | HV_STIMER_SINT(stimer->config) == sint) { |
| 166 | set_bit(stimer->index, |
| 167 | hv_vcpu->stimer_pending_bitmap); |
| 168 | stimers_pending++; |
| 169 | } |
| 170 | } |
| 171 | if (stimers_pending) |
| 172 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 173 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 174 | idx = srcu_read_lock(&kvm->irq_srcu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 175 | gsi = atomic_read(&synic->sint_to_gsi[sint]); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 176 | if (gsi != -1) |
| 177 | kvm_notify_acked_gsi(kvm, gsi); |
| 178 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 179 | } |
| 180 | |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 181 | static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) |
| 182 | { |
| 183 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 184 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
| 185 | |
| 186 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; |
| 187 | hv_vcpu->exit.u.synic.msr = msr; |
| 188 | hv_vcpu->exit.u.synic.control = synic->control; |
| 189 | hv_vcpu->exit.u.synic.evt_page = synic->evt_page; |
| 190 | hv_vcpu->exit.u.synic.msg_page = synic->msg_page; |
| 191 | |
| 192 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 193 | } |
| 194 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 195 | static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, |
| 196 | u32 msr, u64 data, bool host) |
| 197 | { |
| 198 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 199 | int ret; |
| 200 | |
| 201 | if (!synic->active) |
| 202 | return 1; |
| 203 | |
| 204 | vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n", |
| 205 | msr, data, host); |
| 206 | ret = 0; |
| 207 | switch (msr) { |
| 208 | case HV_X64_MSR_SCONTROL: |
| 209 | synic->control = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 210 | if (!host) |
| 211 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 212 | break; |
| 213 | case HV_X64_MSR_SVERSION: |
| 214 | if (!host) { |
| 215 | ret = 1; |
| 216 | break; |
| 217 | } |
| 218 | synic->version = data; |
| 219 | break; |
| 220 | case HV_X64_MSR_SIEFP: |
| 221 | if (data & HV_SYNIC_SIEFP_ENABLE) |
| 222 | if (kvm_clear_guest(vcpu->kvm, |
| 223 | data & PAGE_MASK, PAGE_SIZE)) { |
| 224 | ret = 1; |
| 225 | break; |
| 226 | } |
| 227 | synic->evt_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 228 | if (!host) |
| 229 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 230 | break; |
| 231 | case HV_X64_MSR_SIMP: |
| 232 | if (data & HV_SYNIC_SIMP_ENABLE) |
| 233 | if (kvm_clear_guest(vcpu->kvm, |
| 234 | data & PAGE_MASK, PAGE_SIZE)) { |
| 235 | ret = 1; |
| 236 | break; |
| 237 | } |
| 238 | synic->msg_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 239 | if (!host) |
| 240 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 241 | break; |
| 242 | case HV_X64_MSR_EOM: { |
| 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 246 | kvm_hv_notify_acked_sint(vcpu, i); |
| 247 | break; |
| 248 | } |
| 249 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 250 | ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data); |
| 251 | break; |
| 252 | default: |
| 253 | ret = 1; |
| 254 | break; |
| 255 | } |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata) |
| 260 | { |
| 261 | int ret; |
| 262 | |
| 263 | if (!synic->active) |
| 264 | return 1; |
| 265 | |
| 266 | ret = 0; |
| 267 | switch (msr) { |
| 268 | case HV_X64_MSR_SCONTROL: |
| 269 | *pdata = synic->control; |
| 270 | break; |
| 271 | case HV_X64_MSR_SVERSION: |
| 272 | *pdata = synic->version; |
| 273 | break; |
| 274 | case HV_X64_MSR_SIEFP: |
| 275 | *pdata = synic->evt_page; |
| 276 | break; |
| 277 | case HV_X64_MSR_SIMP: |
| 278 | *pdata = synic->msg_page; |
| 279 | break; |
| 280 | case HV_X64_MSR_EOM: |
| 281 | *pdata = 0; |
| 282 | break; |
| 283 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 284 | *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); |
| 285 | break; |
| 286 | default: |
| 287 | ret = 1; |
| 288 | break; |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) |
| 294 | { |
| 295 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 296 | struct kvm_lapic_irq irq; |
| 297 | int ret, vector; |
| 298 | |
| 299 | if (sint >= ARRAY_SIZE(synic->sint)) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | vector = synic_get_sint_vector(synic_read_sint(synic, sint)); |
| 303 | if (vector < 0) |
| 304 | return -ENOENT; |
| 305 | |
| 306 | memset(&irq, 0, sizeof(irq)); |
| 307 | irq.dest_id = kvm_apic_id(vcpu->arch.apic); |
| 308 | irq.dest_mode = APIC_DEST_PHYSICAL; |
| 309 | irq.delivery_mode = APIC_DM_FIXED; |
| 310 | irq.vector = vector; |
| 311 | irq.level = 1; |
| 312 | |
| 313 | ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL); |
| 314 | vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint) |
| 319 | { |
| 320 | struct kvm_vcpu_hv_synic *synic; |
| 321 | |
| 322 | synic = synic_get(kvm, vcpu_id); |
| 323 | if (!synic) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | return synic_set_irq(synic, sint); |
| 327 | } |
| 328 | |
| 329 | void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) |
| 330 | { |
| 331 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 332 | int i; |
| 333 | |
| 334 | vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector); |
| 335 | |
| 336 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 337 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 338 | kvm_hv_notify_acked_sint(vcpu, i); |
| 339 | } |
| 340 | |
| 341 | static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi) |
| 342 | { |
| 343 | struct kvm_vcpu_hv_synic *synic; |
| 344 | |
| 345 | synic = synic_get(kvm, vcpu_id); |
| 346 | if (!synic) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) |
| 350 | return -EINVAL; |
| 351 | |
| 352 | atomic_set(&synic->sint_to_gsi[sint], gsi); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | void kvm_hv_irq_routing_update(struct kvm *kvm) |
| 357 | { |
| 358 | struct kvm_irq_routing_table *irq_rt; |
| 359 | struct kvm_kernel_irq_routing_entry *e; |
| 360 | u32 gsi; |
| 361 | |
| 362 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 363 | lockdep_is_held(&kvm->irq_lock)); |
| 364 | |
| 365 | for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { |
| 366 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 367 | if (e->type == KVM_IRQ_ROUTING_HV_SINT) |
| 368 | kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, |
| 369 | e->hv_sint.sint, gsi); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void synic_init(struct kvm_vcpu_hv_synic *synic) |
| 375 | { |
| 376 | int i; |
| 377 | |
| 378 | memset(synic, 0, sizeof(*synic)); |
| 379 | synic->version = HV_SYNIC_VERSION_1; |
| 380 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 381 | atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); |
| 382 | atomic_set(&synic->sint_to_gsi[i], -1); |
| 383 | } |
| 384 | } |
| 385 | |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 386 | static u64 get_time_ref_counter(struct kvm *kvm) |
| 387 | { |
| 388 | return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100); |
| 389 | } |
| 390 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 391 | static void stimer_mark_expired(struct kvm_vcpu_hv_stimer *stimer, |
| 392 | bool vcpu_kick) |
| 393 | { |
| 394 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 395 | |
| 396 | set_bit(stimer->index, |
| 397 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 398 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 399 | if (vcpu_kick) |
| 400 | kvm_vcpu_kick(vcpu); |
| 401 | } |
| 402 | |
| 403 | static void stimer_stop(struct kvm_vcpu_hv_stimer *stimer) |
| 404 | { |
| 405 | hrtimer_cancel(&stimer->timer); |
| 406 | } |
| 407 | |
| 408 | static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) |
| 409 | { |
| 410 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 411 | |
| 412 | stimer_stop(stimer); |
| 413 | clear_bit(stimer->index, |
| 414 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 415 | stimer->msg_pending = false; |
| 416 | } |
| 417 | |
| 418 | static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) |
| 419 | { |
| 420 | struct kvm_vcpu_hv_stimer *stimer; |
| 421 | |
| 422 | stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); |
| 423 | stimer_mark_expired(stimer, true); |
| 424 | |
| 425 | return HRTIMER_NORESTART; |
| 426 | } |
| 427 | |
| 428 | static void stimer_restart(struct kvm_vcpu_hv_stimer *stimer) |
| 429 | { |
| 430 | u64 time_now; |
| 431 | ktime_t ktime_now; |
| 432 | u64 remainder; |
| 433 | |
| 434 | time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); |
| 435 | ktime_now = ktime_get(); |
| 436 | |
| 437 | div64_u64_rem(time_now - stimer->exp_time, stimer->count, &remainder); |
| 438 | stimer->exp_time = time_now + (stimer->count - remainder); |
| 439 | |
| 440 | hrtimer_start(&stimer->timer, |
| 441 | ktime_add_ns(ktime_now, |
| 442 | 100 * (stimer->exp_time - time_now)), |
| 443 | HRTIMER_MODE_ABS); |
| 444 | } |
| 445 | |
| 446 | static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) |
| 447 | { |
| 448 | u64 time_now; |
| 449 | ktime_t ktime_now; |
| 450 | |
| 451 | time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); |
| 452 | ktime_now = ktime_get(); |
| 453 | |
| 454 | if (stimer->config & HV_STIMER_PERIODIC) { |
| 455 | if (stimer->count == 0) |
| 456 | return -EINVAL; |
| 457 | |
| 458 | stimer->exp_time = time_now + stimer->count; |
| 459 | hrtimer_start(&stimer->timer, |
| 460 | ktime_add_ns(ktime_now, 100 * stimer->count), |
| 461 | HRTIMER_MODE_ABS); |
| 462 | return 0; |
| 463 | } |
| 464 | stimer->exp_time = stimer->count; |
| 465 | if (time_now >= stimer->count) { |
| 466 | /* |
| 467 | * Expire timer according to Hypervisor Top-Level Functional |
| 468 | * specification v4(15.3.1): |
| 469 | * "If a one shot is enabled and the specified count is in |
| 470 | * the past, it will expire immediately." |
| 471 | */ |
| 472 | stimer_mark_expired(stimer, false); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | hrtimer_start(&stimer->timer, |
| 477 | ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), |
| 478 | HRTIMER_MODE_ABS); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, |
| 483 | bool host) |
| 484 | { |
| 485 | if (stimer->count == 0 || HV_STIMER_SINT(config) == 0) |
| 486 | config &= ~HV_STIMER_ENABLE; |
| 487 | stimer->config = config; |
| 488 | stimer_cleanup(stimer); |
| 489 | if (stimer->config & HV_STIMER_ENABLE) |
| 490 | if (stimer_start(stimer)) |
| 491 | return 1; |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, |
| 496 | bool host) |
| 497 | { |
| 498 | stimer->count = count; |
| 499 | |
| 500 | stimer_cleanup(stimer); |
| 501 | if (stimer->count == 0) |
| 502 | stimer->config &= ~HV_STIMER_ENABLE; |
| 503 | else if (stimer->config & HV_STIMER_AUTOENABLE) { |
| 504 | stimer->config |= HV_STIMER_ENABLE; |
| 505 | if (stimer_start(stimer)) |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) |
| 513 | { |
| 514 | *pconfig = stimer->config; |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) |
| 519 | { |
| 520 | *pcount = stimer->count; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, |
| 525 | struct hv_message *src_msg) |
| 526 | { |
| 527 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 528 | struct page *page; |
| 529 | gpa_t gpa; |
| 530 | struct hv_message *dst_msg; |
| 531 | int r; |
| 532 | struct hv_message_page *msg_page; |
| 533 | |
| 534 | if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) |
| 535 | return -ENOENT; |
| 536 | |
| 537 | gpa = synic->msg_page & PAGE_MASK; |
| 538 | page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); |
| 539 | if (is_error_page(page)) |
| 540 | return -EFAULT; |
| 541 | |
| 542 | msg_page = kmap_atomic(page); |
| 543 | dst_msg = &msg_page->sint_message[sint]; |
| 544 | if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE, |
| 545 | src_msg->header.message_type) != HVMSG_NONE) { |
| 546 | dst_msg->header.message_flags.msg_pending = 1; |
| 547 | r = -EAGAIN; |
| 548 | } else { |
| 549 | memcpy(&dst_msg->u.payload, &src_msg->u.payload, |
| 550 | src_msg->header.payload_size); |
| 551 | dst_msg->header.message_type = src_msg->header.message_type; |
| 552 | dst_msg->header.payload_size = src_msg->header.payload_size; |
| 553 | r = synic_set_irq(synic, sint); |
| 554 | if (r >= 1) |
| 555 | r = 0; |
| 556 | else if (r == 0) |
| 557 | r = -EFAULT; |
| 558 | } |
| 559 | kunmap_atomic(msg_page); |
| 560 | kvm_release_page_dirty(page); |
| 561 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
| 562 | return r; |
| 563 | } |
| 564 | |
| 565 | static void stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) |
| 566 | { |
| 567 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 568 | struct hv_message *msg = &stimer->msg; |
| 569 | struct hv_timer_message_payload *payload = |
| 570 | (struct hv_timer_message_payload *)&msg->u.payload; |
| 571 | int r; |
| 572 | |
| 573 | stimer->msg_pending = true; |
| 574 | payload->expiration_time = stimer->exp_time; |
| 575 | payload->delivery_time = get_time_ref_counter(vcpu->kvm); |
| 576 | r = synic_deliver_msg(vcpu_to_synic(vcpu), |
| 577 | HV_STIMER_SINT(stimer->config), msg); |
| 578 | if (!r) |
| 579 | stimer->msg_pending = false; |
| 580 | } |
| 581 | |
| 582 | static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) |
| 583 | { |
| 584 | stimer_send_msg(stimer); |
| 585 | if (!(stimer->config & HV_STIMER_PERIODIC)) |
Andrey Smetanin | 1ac1b65 | 2015-12-28 18:27:18 +0300 | [diff] [blame^] | 586 | stimer->config &= ~HV_STIMER_ENABLE; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 587 | else |
| 588 | stimer_restart(stimer); |
| 589 | } |
| 590 | |
| 591 | void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) |
| 592 | { |
| 593 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 594 | struct kvm_vcpu_hv_stimer *stimer; |
| 595 | u64 time_now; |
| 596 | int i; |
| 597 | |
| 598 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 599 | if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { |
| 600 | stimer = &hv_vcpu->stimer[i]; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 601 | if (stimer->config & HV_STIMER_ENABLE) { |
| 602 | time_now = get_time_ref_counter(vcpu->kvm); |
| 603 | if (time_now >= stimer->exp_time) |
| 604 | stimer_expiration(stimer); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 610 | { |
| 611 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 612 | int i; |
| 613 | |
| 614 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 615 | stimer_cleanup(&hv_vcpu->stimer[i]); |
| 616 | } |
| 617 | |
| 618 | static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) |
| 619 | { |
| 620 | struct hv_message *msg = &stimer->msg; |
| 621 | struct hv_timer_message_payload *payload = |
| 622 | (struct hv_timer_message_payload *)&msg->u.payload; |
| 623 | |
| 624 | memset(&msg->header, 0, sizeof(msg->header)); |
| 625 | msg->header.message_type = HVMSG_TIMER_EXPIRED; |
| 626 | msg->header.payload_size = sizeof(*payload); |
| 627 | |
| 628 | payload->timer_index = stimer->index; |
| 629 | payload->expiration_time = 0; |
| 630 | payload->delivery_time = 0; |
| 631 | } |
| 632 | |
| 633 | static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) |
| 634 | { |
| 635 | memset(stimer, 0, sizeof(*stimer)); |
| 636 | stimer->index = timer_index; |
| 637 | hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 638 | stimer->timer.function = stimer_timer_callback; |
| 639 | stimer_prepare_msg(stimer); |
| 640 | } |
| 641 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 642 | void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) |
| 643 | { |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 644 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 645 | int i; |
| 646 | |
| 647 | synic_init(&hv_vcpu->synic); |
| 648 | |
| 649 | bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); |
| 650 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 651 | stimer_init(&hv_vcpu->stimer[i], i); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | int kvm_hv_activate_synic(struct kvm_vcpu *vcpu) |
| 655 | { |
| 656 | /* |
| 657 | * Hyper-V SynIC auto EOI SINT's are |
| 658 | * not compatible with APICV, so deactivate APICV |
| 659 | */ |
| 660 | kvm_vcpu_deactivate_apicv(vcpu); |
| 661 | vcpu_to_synic(vcpu)->active = true; |
| 662 | return 0; |
| 663 | } |
| 664 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 665 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 666 | { |
| 667 | bool r = false; |
| 668 | |
| 669 | switch (msr) { |
| 670 | case HV_X64_MSR_GUEST_OS_ID: |
| 671 | case HV_X64_MSR_HYPERCALL: |
| 672 | case HV_X64_MSR_REFERENCE_TSC: |
| 673 | case HV_X64_MSR_TIME_REF_COUNT: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 674 | case HV_X64_MSR_CRASH_CTL: |
| 675 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 676 | case HV_X64_MSR_RESET: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 677 | r = true; |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | return r; |
| 682 | } |
| 683 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 684 | static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, |
| 685 | u32 index, u64 *pdata) |
| 686 | { |
| 687 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 688 | |
| 689 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 690 | return -EINVAL; |
| 691 | |
| 692 | *pdata = hv->hv_crash_param[index]; |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) |
| 697 | { |
| 698 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 699 | |
| 700 | *pdata = hv->hv_crash_ctl; |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) |
| 705 | { |
| 706 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 707 | |
| 708 | if (host) |
| 709 | hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY; |
| 710 | |
| 711 | if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) { |
| 712 | |
| 713 | vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", |
| 714 | hv->hv_crash_param[0], |
| 715 | hv->hv_crash_param[1], |
| 716 | hv->hv_crash_param[2], |
| 717 | hv->hv_crash_param[3], |
| 718 | hv->hv_crash_param[4]); |
| 719 | |
| 720 | /* Send notification about crash to user space */ |
| 721 | kvm_make_request(KVM_REQ_HV_CRASH, vcpu); |
| 722 | } |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, |
| 728 | u32 index, u64 data) |
| 729 | { |
| 730 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 731 | |
| 732 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | hv->hv_crash_param[index] = data; |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, |
| 740 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 741 | { |
| 742 | struct kvm *kvm = vcpu->kvm; |
| 743 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 744 | |
| 745 | switch (msr) { |
| 746 | case HV_X64_MSR_GUEST_OS_ID: |
| 747 | hv->hv_guest_os_id = data; |
| 748 | /* setting guest os id to zero disables hypercall page */ |
| 749 | if (!hv->hv_guest_os_id) |
| 750 | hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 751 | break; |
| 752 | case HV_X64_MSR_HYPERCALL: { |
| 753 | u64 gfn; |
| 754 | unsigned long addr; |
| 755 | u8 instructions[4]; |
| 756 | |
| 757 | /* if guest os id is not set hypercall should remain disabled */ |
| 758 | if (!hv->hv_guest_os_id) |
| 759 | break; |
| 760 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 761 | hv->hv_hypercall = data; |
| 762 | break; |
| 763 | } |
| 764 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 765 | addr = gfn_to_hva(kvm, gfn); |
| 766 | if (kvm_is_error_hva(addr)) |
| 767 | return 1; |
| 768 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 769 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 770 | if (__copy_to_user((void __user *)addr, instructions, 4)) |
| 771 | return 1; |
| 772 | hv->hv_hypercall = data; |
| 773 | mark_page_dirty(kvm, gfn); |
| 774 | break; |
| 775 | } |
| 776 | case HV_X64_MSR_REFERENCE_TSC: { |
| 777 | u64 gfn; |
| 778 | HV_REFERENCE_TSC_PAGE tsc_ref; |
| 779 | |
| 780 | memset(&tsc_ref, 0, sizeof(tsc_ref)); |
| 781 | hv->hv_tsc_page = data; |
| 782 | if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 783 | break; |
| 784 | gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 785 | if (kvm_write_guest( |
| 786 | kvm, |
| 787 | gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT, |
| 788 | &tsc_ref, sizeof(tsc_ref))) |
| 789 | return 1; |
| 790 | mark_page_dirty(kvm, gfn); |
| 791 | break; |
| 792 | } |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 793 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 794 | return kvm_hv_msr_set_crash_data(vcpu, |
| 795 | msr - HV_X64_MSR_CRASH_P0, |
| 796 | data); |
| 797 | case HV_X64_MSR_CRASH_CTL: |
| 798 | return kvm_hv_msr_set_crash_ctl(vcpu, data, host); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 799 | case HV_X64_MSR_RESET: |
| 800 | if (data == 1) { |
| 801 | vcpu_debug(vcpu, "hyper-v reset requested\n"); |
| 802 | kvm_make_request(KVM_REQ_HV_RESET, vcpu); |
| 803 | } |
| 804 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 805 | default: |
| 806 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 807 | msr, data); |
| 808 | return 1; |
| 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 813 | /* Calculate cpu time spent by current task in 100ns units */ |
| 814 | static u64 current_task_runtime_100ns(void) |
| 815 | { |
| 816 | cputime_t utime, stime; |
| 817 | |
| 818 | task_cputime_adjusted(current, &utime, &stime); |
| 819 | return div_u64(cputime_to_nsecs(utime + stime), 100); |
| 820 | } |
| 821 | |
| 822 | 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] | 823 | { |
| 824 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 825 | |
| 826 | switch (msr) { |
| 827 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 828 | u64 gfn; |
| 829 | unsigned long addr; |
| 830 | |
| 831 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 832 | hv->hv_vapic = data; |
| 833 | if (kvm_lapic_enable_pv_eoi(vcpu, 0)) |
| 834 | return 1; |
| 835 | break; |
| 836 | } |
| 837 | gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT; |
| 838 | addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); |
| 839 | if (kvm_is_error_hva(addr)) |
| 840 | return 1; |
| 841 | if (__clear_user((void __user *)addr, PAGE_SIZE)) |
| 842 | return 1; |
| 843 | hv->hv_vapic = data; |
| 844 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 845 | if (kvm_lapic_enable_pv_eoi(vcpu, |
| 846 | gfn_to_gpa(gfn) | KVM_MSR_ENABLED)) |
| 847 | return 1; |
| 848 | break; |
| 849 | } |
| 850 | case HV_X64_MSR_EOI: |
| 851 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 852 | case HV_X64_MSR_ICR: |
| 853 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 854 | case HV_X64_MSR_TPR: |
| 855 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 856 | case HV_X64_MSR_VP_RUNTIME: |
| 857 | if (!host) |
| 858 | return 1; |
| 859 | hv->runtime_offset = data - current_task_runtime_100ns(); |
| 860 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 861 | case HV_X64_MSR_SCONTROL: |
| 862 | case HV_X64_MSR_SVERSION: |
| 863 | case HV_X64_MSR_SIEFP: |
| 864 | case HV_X64_MSR_SIMP: |
| 865 | case HV_X64_MSR_EOM: |
| 866 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 867 | return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 868 | case HV_X64_MSR_STIMER0_CONFIG: |
| 869 | case HV_X64_MSR_STIMER1_CONFIG: |
| 870 | case HV_X64_MSR_STIMER2_CONFIG: |
| 871 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 872 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 873 | |
| 874 | return stimer_set_config(vcpu_to_stimer(vcpu, timer_index), |
| 875 | data, host); |
| 876 | } |
| 877 | case HV_X64_MSR_STIMER0_COUNT: |
| 878 | case HV_X64_MSR_STIMER1_COUNT: |
| 879 | case HV_X64_MSR_STIMER2_COUNT: |
| 880 | case HV_X64_MSR_STIMER3_COUNT: { |
| 881 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 882 | |
| 883 | return stimer_set_count(vcpu_to_stimer(vcpu, timer_index), |
| 884 | data, host); |
| 885 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 886 | default: |
| 887 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 888 | msr, data); |
| 889 | return 1; |
| 890 | } |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 896 | { |
| 897 | u64 data = 0; |
| 898 | struct kvm *kvm = vcpu->kvm; |
| 899 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 900 | |
| 901 | switch (msr) { |
| 902 | case HV_X64_MSR_GUEST_OS_ID: |
| 903 | data = hv->hv_guest_os_id; |
| 904 | break; |
| 905 | case HV_X64_MSR_HYPERCALL: |
| 906 | data = hv->hv_hypercall; |
| 907 | break; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 908 | case HV_X64_MSR_TIME_REF_COUNT: |
| 909 | data = get_time_ref_counter(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 910 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 911 | case HV_X64_MSR_REFERENCE_TSC: |
| 912 | data = hv->hv_tsc_page; |
| 913 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 914 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 915 | return kvm_hv_msr_get_crash_data(vcpu, |
| 916 | msr - HV_X64_MSR_CRASH_P0, |
| 917 | pdata); |
| 918 | case HV_X64_MSR_CRASH_CTL: |
| 919 | return kvm_hv_msr_get_crash_ctl(vcpu, pdata); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 920 | case HV_X64_MSR_RESET: |
| 921 | data = 0; |
| 922 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 923 | default: |
| 924 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 925 | return 1; |
| 926 | } |
| 927 | |
| 928 | *pdata = data; |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 933 | { |
| 934 | u64 data = 0; |
| 935 | struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; |
| 936 | |
| 937 | switch (msr) { |
| 938 | case HV_X64_MSR_VP_INDEX: { |
| 939 | int r; |
| 940 | struct kvm_vcpu *v; |
| 941 | |
| 942 | kvm_for_each_vcpu(r, v, vcpu->kvm) { |
| 943 | if (v == vcpu) { |
| 944 | data = r; |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | break; |
| 949 | } |
| 950 | case HV_X64_MSR_EOI: |
| 951 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 952 | case HV_X64_MSR_ICR: |
| 953 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 954 | case HV_X64_MSR_TPR: |
| 955 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
| 956 | case HV_X64_MSR_APIC_ASSIST_PAGE: |
| 957 | data = hv->hv_vapic; |
| 958 | break; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 959 | case HV_X64_MSR_VP_RUNTIME: |
| 960 | data = current_task_runtime_100ns() + hv->runtime_offset; |
| 961 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 962 | case HV_X64_MSR_SCONTROL: |
| 963 | case HV_X64_MSR_SVERSION: |
| 964 | case HV_X64_MSR_SIEFP: |
| 965 | case HV_X64_MSR_SIMP: |
| 966 | case HV_X64_MSR_EOM: |
| 967 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 968 | return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 969 | case HV_X64_MSR_STIMER0_CONFIG: |
| 970 | case HV_X64_MSR_STIMER1_CONFIG: |
| 971 | case HV_X64_MSR_STIMER2_CONFIG: |
| 972 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 973 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 974 | |
| 975 | return stimer_get_config(vcpu_to_stimer(vcpu, timer_index), |
| 976 | pdata); |
| 977 | } |
| 978 | case HV_X64_MSR_STIMER0_COUNT: |
| 979 | case HV_X64_MSR_STIMER1_COUNT: |
| 980 | case HV_X64_MSR_STIMER2_COUNT: |
| 981 | case HV_X64_MSR_STIMER3_COUNT: { |
| 982 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 983 | |
| 984 | return stimer_get_count(vcpu_to_stimer(vcpu, timer_index), |
| 985 | pdata); |
| 986 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 987 | default: |
| 988 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 989 | return 1; |
| 990 | } |
| 991 | *pdata = data; |
| 992 | return 0; |
| 993 | } |
| 994 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 995 | 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] | 996 | { |
| 997 | if (kvm_hv_msr_partition_wide(msr)) { |
| 998 | int r; |
| 999 | |
| 1000 | mutex_lock(&vcpu->kvm->lock); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1001 | r = kvm_hv_set_msr_pw(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1002 | mutex_unlock(&vcpu->kvm->lock); |
| 1003 | return r; |
| 1004 | } else |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1005 | return kvm_hv_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1009 | { |
| 1010 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1011 | int r; |
| 1012 | |
| 1013 | mutex_lock(&vcpu->kvm->lock); |
| 1014 | r = kvm_hv_get_msr_pw(vcpu, msr, pdata); |
| 1015 | mutex_unlock(&vcpu->kvm->lock); |
| 1016 | return r; |
| 1017 | } else |
| 1018 | return kvm_hv_get_msr(vcpu, msr, pdata); |
| 1019 | } |
| 1020 | |
| 1021 | bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 1022 | { |
| 1023 | return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 1024 | } |
| 1025 | |
| 1026 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 1027 | { |
| 1028 | u64 param, ingpa, outgpa, ret; |
| 1029 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 1030 | bool fast, longmode; |
| 1031 | |
| 1032 | /* |
| 1033 | * hypercall generates UD from non zero cpl and real mode |
| 1034 | * per HYPER-V spec |
| 1035 | */ |
| 1036 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
| 1037 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | longmode = is_64_bit_mode(vcpu); |
| 1042 | |
| 1043 | if (!longmode) { |
| 1044 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 1045 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 1046 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 1047 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 1048 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 1049 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
| 1050 | } |
| 1051 | #ifdef CONFIG_X86_64 |
| 1052 | else { |
| 1053 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 1054 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 1055 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 1056 | } |
| 1057 | #endif |
| 1058 | |
| 1059 | code = param & 0xffff; |
| 1060 | fast = (param >> 16) & 0x1; |
| 1061 | rep_cnt = (param >> 32) & 0xfff; |
| 1062 | rep_idx = (param >> 48) & 0xfff; |
| 1063 | |
| 1064 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 1065 | |
| 1066 | switch (code) { |
| 1067 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 1068 | kvm_vcpu_on_spin(vcpu); |
| 1069 | break; |
| 1070 | default: |
| 1071 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 1076 | if (longmode) { |
| 1077 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 1078 | } else { |
| 1079 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 1080 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 1081 | } |
| 1082 | |
| 1083 | return 1; |
| 1084 | } |