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