qemu/target/i386/kvm/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 "hw/hyperv/hyperv.h"
  18#include "hyperv-proto.h"
  19
  20int hyperv_x86_synic_add(X86CPU *cpu)
  21{
  22    hyperv_synic_add(CPU(cpu));
  23    return 0;
  24}
  25
  26void hyperv_x86_synic_reset(X86CPU *cpu)
  27{
  28    hyperv_synic_reset(CPU(cpu));
  29}
  30
  31void hyperv_x86_synic_update(X86CPU *cpu)
  32{
  33    CPUX86State *env = &cpu->env;
  34    bool enable = env->msr_hv_synic_control & HV_SYNIC_ENABLE;
  35    hwaddr msg_page_addr = (env->msr_hv_synic_msg_page & HV_SIMP_ENABLE) ?
  36        (env->msr_hv_synic_msg_page & TARGET_PAGE_MASK) : 0;
  37    hwaddr event_page_addr = (env->msr_hv_synic_evt_page & HV_SIEFP_ENABLE) ?
  38        (env->msr_hv_synic_evt_page & TARGET_PAGE_MASK) : 0;
  39    hyperv_synic_update(CPU(cpu), enable, msg_page_addr, event_page_addr);
  40}
  41
  42static void async_synic_update(CPUState *cs, run_on_cpu_data data)
  43{
  44    qemu_mutex_lock_iothread();
  45    hyperv_x86_synic_update(X86_CPU(cs));
  46    qemu_mutex_unlock_iothread();
  47}
  48
  49int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
  50{
  51    CPUX86State *env = &cpu->env;
  52
  53    switch (exit->type) {
  54    case KVM_EXIT_HYPERV_SYNIC:
  55        if (!hyperv_feat_enabled(cpu, HYPERV_FEAT_SYNIC)) {
  56            return -1;
  57        }
  58
  59        switch (exit->u.synic.msr) {
  60        case HV_X64_MSR_SCONTROL:
  61            env->msr_hv_synic_control = exit->u.synic.control;
  62            break;
  63        case HV_X64_MSR_SIMP:
  64            env->msr_hv_synic_msg_page = exit->u.synic.msg_page;
  65            break;
  66        case HV_X64_MSR_SIEFP:
  67            env->msr_hv_synic_evt_page = exit->u.synic.evt_page;
  68            break;
  69        default:
  70            return -1;
  71        }
  72
  73        /*
  74         * this will run in this cpu thread before it returns to KVM, but in a
  75         * safe environment (i.e. when all cpus are quiescent) -- this is
  76         * necessary because memory hierarchy is being changed
  77         */
  78        async_safe_run_on_cpu(CPU(cpu), async_synic_update, RUN_ON_CPU_NULL);
  79
  80        return 0;
  81    case KVM_EXIT_HYPERV_HCALL: {
  82        uint16_t code = exit->u.hcall.input & 0xffff;
  83        bool fast = exit->u.hcall.input & HV_HYPERCALL_FAST;
  84        uint64_t in_param = exit->u.hcall.params[0];
  85        uint64_t out_param = exit->u.hcall.params[1];
  86
  87        switch (code) {
  88        case HV_POST_MESSAGE:
  89            exit->u.hcall.result = hyperv_hcall_post_message(in_param, fast);
  90            break;
  91        case HV_SIGNAL_EVENT:
  92            exit->u.hcall.result = hyperv_hcall_signal_event(in_param, fast);
  93            break;
  94        case HV_POST_DEBUG_DATA:
  95            exit->u.hcall.result =
  96                hyperv_hcall_post_dbg_data(in_param, out_param, fast);
  97            break;
  98        case HV_RETRIEVE_DEBUG_DATA:
  99            exit->u.hcall.result =
 100                hyperv_hcall_retreive_dbg_data(in_param, out_param, fast);
 101            break;
 102        case HV_RESET_DEBUG_SESSION:
 103            exit->u.hcall.result =
 104                hyperv_hcall_reset_dbg_session(out_param);
 105            break;
 106        default:
 107            exit->u.hcall.result = HV_STATUS_INVALID_HYPERCALL_CODE;
 108        }
 109        return 0;
 110    }
 111
 112    case KVM_EXIT_HYPERV_SYNDBG:
 113        if (!hyperv_feat_enabled(cpu, HYPERV_FEAT_SYNDBG)) {
 114            return -1;
 115        }
 116
 117        switch (exit->u.syndbg.msr) {
 118        case HV_X64_MSR_SYNDBG_CONTROL: {
 119            uint64_t control = exit->u.syndbg.control;
 120            env->msr_hv_syndbg_control = control;
 121            env->msr_hv_syndbg_send_page = exit->u.syndbg.send_page;
 122            env->msr_hv_syndbg_recv_page = exit->u.syndbg.recv_page;
 123            exit->u.syndbg.status = HV_STATUS_SUCCESS;
 124            if (control & HV_SYNDBG_CONTROL_SEND) {
 125                exit->u.syndbg.status =
 126                    hyperv_syndbg_send(env->msr_hv_syndbg_send_page,
 127                            HV_SYNDBG_CONTROL_SEND_SIZE(control));
 128            } else if (control & HV_SYNDBG_CONTROL_RECV) {
 129                exit->u.syndbg.status =
 130                    hyperv_syndbg_recv(env->msr_hv_syndbg_recv_page,
 131                            TARGET_PAGE_SIZE);
 132            }
 133            break;
 134        }
 135        case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
 136            env->msr_hv_syndbg_pending_page = exit->u.syndbg.pending_page;
 137            hyperv_syndbg_set_pending_page(env->msr_hv_syndbg_pending_page);
 138            break;
 139        default:
 140            return -1;
 141        }
 142
 143        return 0;
 144    default:
 145        return -1;
 146    }
 147}
 148