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