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