qemu/target/i386/hyperv.c
<<
>>
Prefs
   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 "qemu/osdep.h"
  15#include "qemu/main-loop.h"
  16#include "hyperv.h"
  17#include "hyperv-proto.h"
  18
  19uint32_t hyperv_vp_index(X86CPU *cpu)
  20{
  21    return CPU(cpu)->cpu_index;
  22}
  23
  24X86CPU *hyperv_find_vcpu(uint32_t vp_index)
  25{
  26    return X86_CPU(qemu_get_cpu(vp_index));
  27}
  28
  29int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
  30{
  31    CPUX86State *env = &cpu->env;
  32
  33    switch (exit->type) {
  34    case KVM_EXIT_HYPERV_SYNIC:
  35        if (!cpu->hyperv_synic) {
  36            return -1;
  37        }
  38
  39        /*
  40         * For now just track changes in SynIC control and msg/evt pages msr's.
  41         * When SynIC messaging/events processing will be added in future
  42         * here we will do messages queues flushing and pages remapping.
  43         */
  44        switch (exit->u.synic.msr) {
  45        case HV_X64_MSR_SCONTROL:
  46            env->msr_hv_synic_control = exit->u.synic.control;
  47            break;
  48        case HV_X64_MSR_SIMP:
  49            env->msr_hv_synic_msg_page = exit->u.synic.msg_page;
  50            break;
  51        case HV_X64_MSR_SIEFP:
  52            env->msr_hv_synic_evt_page = exit->u.synic.evt_page;
  53            break;
  54        default:
  55            return -1;
  56        }
  57        return 0;
  58    case KVM_EXIT_HYPERV_HCALL: {
  59        uint16_t code;
  60
  61        code  = exit->u.hcall.input & 0xffff;
  62        switch (code) {
  63        case HV_POST_MESSAGE:
  64        case HV_SIGNAL_EVENT:
  65        default:
  66            exit->u.hcall.result = HV_STATUS_INVALID_HYPERCALL_CODE;
  67            return 0;
  68        }
  69    }
  70    default:
  71        return -1;
  72    }
  73}
  74
  75static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
  76{
  77    HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
  78                                           sint_ack_notifier);
  79    event_notifier_test_and_clear(notifier);
  80    if (sint_route->sint_ack_clb) {
  81        sint_route->sint_ack_clb(sint_route);
  82    }
  83}
  84
  85HvSintRoute *kvm_hv_sint_route_create(uint32_t vp_index, uint32_t sint,
  86                                      HvSintAckClb sint_ack_clb)
  87{
  88    HvSintRoute *sint_route;
  89    int r, gsi;
  90
  91    sint_route = g_malloc0(sizeof(*sint_route));
  92    r = event_notifier_init(&sint_route->sint_set_notifier, false);
  93    if (r) {
  94        goto err;
  95    }
  96
  97    r = event_notifier_init(&sint_route->sint_ack_notifier, false);
  98    if (r) {
  99        goto err_sint_set_notifier;
 100    }
 101
 102    event_notifier_set_handler(&sint_route->sint_ack_notifier,
 103                               kvm_hv_sint_ack_handler);
 104
 105    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
 106    if (gsi < 0) {
 107        goto err_gsi;
 108    }
 109
 110    r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
 111                                           &sint_route->sint_set_notifier,
 112                                           &sint_route->sint_ack_notifier, gsi);
 113    if (r) {
 114        goto err_irqfd;
 115    }
 116    sint_route->gsi = gsi;
 117    sint_route->sint_ack_clb = sint_ack_clb;
 118    sint_route->vp_index = vp_index;
 119    sint_route->sint = sint;
 120
 121    return sint_route;
 122
 123err_irqfd:
 124    kvm_irqchip_release_virq(kvm_state, gsi);
 125err_gsi:
 126    event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
 127    event_notifier_cleanup(&sint_route->sint_ack_notifier);
 128err_sint_set_notifier:
 129    event_notifier_cleanup(&sint_route->sint_set_notifier);
 130err:
 131    g_free(sint_route);
 132
 133    return NULL;
 134}
 135
 136void kvm_hv_sint_route_destroy(HvSintRoute *sint_route)
 137{
 138    kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
 139                                          &sint_route->sint_set_notifier,
 140                                          sint_route->gsi);
 141    kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
 142    event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
 143    event_notifier_cleanup(&sint_route->sint_ack_notifier);
 144    event_notifier_cleanup(&sint_route->sint_set_notifier);
 145    g_free(sint_route);
 146}
 147
 148int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route)
 149{
 150    return event_notifier_set(&sint_route->sint_set_notifier);
 151}
 152