Andrey Smetanin | 50efe82 | 2015-11-10 15:52:43 +0300 | [diff] [blame] | 1 | /* |
| 2 | * QEMU KVM Hyper-V support |
| 3 | * |
| 4 | * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> |
| 5 | * |
| 6 | * Authors: |
| 7 | * Andrey Smetanin <asmetanin@virtuozzo.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "hyperv.h" |
| 15 | #include "standard-headers/asm-x86/hyperv.h" |
| 16 | |
| 17 | int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit) |
| 18 | { |
| 19 | CPUX86State *env = &cpu->env; |
| 20 | |
| 21 | switch (exit->type) { |
| 22 | case KVM_EXIT_HYPERV_SYNIC: |
| 23 | if (!cpu->hyperv_synic) { |
| 24 | return -1; |
| 25 | } |
| 26 | |
| 27 | /* |
| 28 | * For now just track changes in SynIC control and msg/evt pages msr's. |
| 29 | * When SynIC messaging/events processing will be added in future |
| 30 | * here we will do messages queues flushing and pages remapping. |
| 31 | */ |
| 32 | switch (exit->u.synic.msr) { |
| 33 | case HV_X64_MSR_SCONTROL: |
| 34 | env->msr_hv_synic_control = exit->u.synic.control; |
| 35 | break; |
| 36 | case HV_X64_MSR_SIMP: |
| 37 | env->msr_hv_synic_msg_page = exit->u.synic.msg_page; |
| 38 | break; |
| 39 | case HV_X64_MSR_SIEFP: |
| 40 | env->msr_hv_synic_evt_page = exit->u.synic.evt_page; |
| 41 | break; |
| 42 | default: |
| 43 | return -1; |
| 44 | } |
| 45 | return 0; |
| 46 | default: |
| 47 | return -1; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void kvm_hv_sint_ack_handler(EventNotifier *notifier) |
| 52 | { |
| 53 | HvSintRoute *sint_route = container_of(notifier, HvSintRoute, |
| 54 | sint_ack_notifier); |
| 55 | event_notifier_test_and_clear(notifier); |
| 56 | if (sint_route->sint_ack_clb) { |
| 57 | sint_route->sint_ack_clb(sint_route); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, |
| 62 | HvSintAckClb sint_ack_clb) |
| 63 | { |
| 64 | HvSintRoute *sint_route; |
| 65 | int r, gsi; |
| 66 | |
| 67 | sint_route = g_malloc0(sizeof(*sint_route)); |
| 68 | r = event_notifier_init(&sint_route->sint_set_notifier, false); |
| 69 | if (r) { |
| 70 | goto err; |
| 71 | } |
| 72 | |
| 73 | r = event_notifier_init(&sint_route->sint_ack_notifier, false); |
| 74 | if (r) { |
| 75 | goto err_sint_set_notifier; |
| 76 | } |
| 77 | |
| 78 | event_notifier_set_handler(&sint_route->sint_ack_notifier, |
| 79 | kvm_hv_sint_ack_handler); |
| 80 | |
| 81 | gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vcpu_id, sint); |
| 82 | if (gsi < 0) { |
| 83 | goto err_gsi; |
| 84 | } |
| 85 | |
| 86 | r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, |
| 87 | &sint_route->sint_set_notifier, |
| 88 | &sint_route->sint_ack_notifier, gsi); |
| 89 | if (r) { |
| 90 | goto err_irqfd; |
| 91 | } |
| 92 | sint_route->gsi = gsi; |
| 93 | sint_route->sint_ack_clb = sint_ack_clb; |
| 94 | sint_route->vcpu_id = vcpu_id; |
| 95 | sint_route->sint = sint; |
| 96 | |
| 97 | return sint_route; |
| 98 | |
| 99 | err_irqfd: |
| 100 | kvm_irqchip_release_virq(kvm_state, gsi); |
| 101 | err_gsi: |
| 102 | event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL); |
| 103 | event_notifier_cleanup(&sint_route->sint_ack_notifier); |
| 104 | err_sint_set_notifier: |
| 105 | event_notifier_cleanup(&sint_route->sint_set_notifier); |
| 106 | err: |
| 107 | g_free(sint_route); |
| 108 | |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | void kvm_hv_sint_route_destroy(HvSintRoute *sint_route) |
| 113 | { |
| 114 | kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, |
| 115 | &sint_route->sint_set_notifier, |
| 116 | sint_route->gsi); |
| 117 | kvm_irqchip_release_virq(kvm_state, sint_route->gsi); |
| 118 | event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL); |
| 119 | event_notifier_cleanup(&sint_route->sint_ack_notifier); |
| 120 | event_notifier_cleanup(&sint_route->sint_set_notifier); |
| 121 | g_free(sint_route); |
| 122 | } |
| 123 | |
| 124 | int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route) |
| 125 | { |
| 126 | return event_notifier_set(&sint_route->sint_set_notifier); |
| 127 | } |