xref: /openbmc/qemu/hw/hyperv/hyperv.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
1701189e3SRoman Kagan /*
2701189e3SRoman Kagan  * Hyper-V guest/hypervisor interaction
3701189e3SRoman Kagan  *
4701189e3SRoman Kagan  * Copyright (c) 2015-2018 Virtuozzo International GmbH.
5701189e3SRoman Kagan  *
6701189e3SRoman Kagan  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7701189e3SRoman Kagan  * See the COPYING file in the top-level directory.
8701189e3SRoman Kagan  */
9701189e3SRoman Kagan 
10701189e3SRoman Kagan #include "qemu/osdep.h"
11701189e3SRoman Kagan #include "qemu/main-loop.h"
120b8fa32fSMarkus Armbruster #include "qemu/module.h"
13606c34bfSRoman Kagan #include "qapi/error.h"
14267e071bSRoman Kagan #include "exec/address-spaces.h"
1554f0058fSPhilippe Mathieu-Daudé #include "exec/memory.h"
16701189e3SRoman Kagan #include "sysemu/kvm.h"
17f5642f8bSRoman Kagan #include "qemu/bitops.h"
188d3bc0b7SRoman Kagan #include "qemu/error-report.h"
1908b689aaSSimran Singhal #include "qemu/lockable.h"
20e6ea9f45SRoman Kagan #include "qemu/queue.h"
21e6ea9f45SRoman Kagan #include "qemu/rcu.h"
22e6ea9f45SRoman Kagan #include "qemu/rcu_queue.h"
23701189e3SRoman Kagan #include "hw/hyperv/hyperv.h"
24db1015e9SEduardo Habkost #include "qom/object.h"
2554f0058fSPhilippe Mathieu-Daudé #include "target/i386/kvm/hyperv-proto.h"
2654f0058fSPhilippe Mathieu-Daudé #include "target/i386/cpu.h"
2754f0058fSPhilippe Mathieu-Daudé #include "exec/cpu-all.h"
28701189e3SRoman Kagan 
29db1015e9SEduardo Habkost struct SynICState {
30606c34bfSRoman Kagan     DeviceState parent_obj;
31606c34bfSRoman Kagan 
32606c34bfSRoman Kagan     CPUState *cs;
33606c34bfSRoman Kagan 
3464ddecc8SJon Doron     bool sctl_enabled;
35606c34bfSRoman Kagan     hwaddr msg_page_addr;
36606c34bfSRoman Kagan     hwaddr event_page_addr;
37267e071bSRoman Kagan     MemoryRegion msg_page_mr;
38267e071bSRoman Kagan     MemoryRegion event_page_mr;
39267e071bSRoman Kagan     struct hyperv_message_page *msg_page;
40267e071bSRoman Kagan     struct hyperv_event_flags_page *event_page;
4164ddecc8SJon Doron 
4264ddecc8SJon Doron     QemuMutex sint_routes_mutex;
4364ddecc8SJon Doron     QLIST_HEAD(, HvSintRoute) sint_routes;
44db1015e9SEduardo Habkost };
45606c34bfSRoman Kagan 
46606c34bfSRoman Kagan #define TYPE_SYNIC "hyperv-synic"
OBJECT_DECLARE_SIMPLE_TYPE(SynICState,SYNIC)478063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC)
48606c34bfSRoman Kagan 
49d42cd961SJon Doron static bool synic_enabled;
50d42cd961SJon Doron 
51d42cd961SJon Doron bool hyperv_is_synic_enabled(void)
52d42cd961SJon Doron {
53d42cd961SJon Doron     return synic_enabled;
54d42cd961SJon Doron }
55d42cd961SJon Doron 
get_synic(CPUState * cs)56606c34bfSRoman Kagan static SynICState *get_synic(CPUState *cs)
57606c34bfSRoman Kagan {
58606c34bfSRoman Kagan     return SYNIC(object_resolve_path_component(OBJECT(cs), "synic"));
59606c34bfSRoman Kagan }
60606c34bfSRoman Kagan 
synic_update(SynICState * synic,bool sctl_enable,hwaddr msg_page_addr,hwaddr event_page_addr)6164ddecc8SJon Doron static void synic_update(SynICState *synic, bool sctl_enable,
62606c34bfSRoman Kagan                          hwaddr msg_page_addr, hwaddr event_page_addr)
63606c34bfSRoman Kagan {
64606c34bfSRoman Kagan 
6564ddecc8SJon Doron     synic->sctl_enabled = sctl_enable;
66267e071bSRoman Kagan     if (synic->msg_page_addr != msg_page_addr) {
67267e071bSRoman Kagan         if (synic->msg_page_addr) {
68267e071bSRoman Kagan             memory_region_del_subregion(get_system_memory(),
69267e071bSRoman Kagan                                         &synic->msg_page_mr);
70267e071bSRoman Kagan         }
71267e071bSRoman Kagan         if (msg_page_addr) {
72267e071bSRoman Kagan             memory_region_add_subregion(get_system_memory(), msg_page_addr,
73267e071bSRoman Kagan                                         &synic->msg_page_mr);
74267e071bSRoman Kagan         }
75606c34bfSRoman Kagan         synic->msg_page_addr = msg_page_addr;
76267e071bSRoman Kagan     }
77267e071bSRoman Kagan     if (synic->event_page_addr != event_page_addr) {
78267e071bSRoman Kagan         if (synic->event_page_addr) {
79267e071bSRoman Kagan             memory_region_del_subregion(get_system_memory(),
80267e071bSRoman Kagan                                         &synic->event_page_mr);
81267e071bSRoman Kagan         }
82267e071bSRoman Kagan         if (event_page_addr) {
83267e071bSRoman Kagan             memory_region_add_subregion(get_system_memory(), event_page_addr,
84267e071bSRoman Kagan                                         &synic->event_page_mr);
85267e071bSRoman Kagan         }
86606c34bfSRoman Kagan         synic->event_page_addr = event_page_addr;
87606c34bfSRoman Kagan     }
88267e071bSRoman Kagan }
89606c34bfSRoman Kagan 
hyperv_synic_update(CPUState * cs,bool sctl_enable,hwaddr msg_page_addr,hwaddr event_page_addr)9064ddecc8SJon Doron void hyperv_synic_update(CPUState *cs, bool sctl_enable,
91606c34bfSRoman Kagan                          hwaddr msg_page_addr, hwaddr event_page_addr)
92606c34bfSRoman Kagan {
93606c34bfSRoman Kagan     SynICState *synic = get_synic(cs);
94606c34bfSRoman Kagan 
95606c34bfSRoman Kagan     if (!synic) {
96606c34bfSRoman Kagan         return;
97606c34bfSRoman Kagan     }
98606c34bfSRoman Kagan 
9964ddecc8SJon Doron     synic_update(synic, sctl_enable, msg_page_addr, event_page_addr);
100606c34bfSRoman Kagan }
101606c34bfSRoman Kagan 
synic_realize(DeviceState * dev,Error ** errp)102606c34bfSRoman Kagan static void synic_realize(DeviceState *dev, Error **errp)
103606c34bfSRoman Kagan {
104267e071bSRoman Kagan     Object *obj = OBJECT(dev);
105267e071bSRoman Kagan     SynICState *synic = SYNIC(dev);
106267e071bSRoman Kagan     char *msgp_name, *eventp_name;
107267e071bSRoman Kagan     uint32_t vp_index;
108606c34bfSRoman Kagan 
109267e071bSRoman Kagan     /* memory region names have to be globally unique */
110267e071bSRoman Kagan     vp_index = hyperv_vp_index(synic->cs);
111267e071bSRoman Kagan     msgp_name = g_strdup_printf("synic-%u-msg-page", vp_index);
112267e071bSRoman Kagan     eventp_name = g_strdup_printf("synic-%u-event-page", vp_index);
113267e071bSRoman Kagan 
114267e071bSRoman Kagan     memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name,
115267e071bSRoman Kagan                            sizeof(*synic->msg_page), &error_abort);
116267e071bSRoman Kagan     memory_region_init_ram(&synic->event_page_mr, obj, eventp_name,
117267e071bSRoman Kagan                            sizeof(*synic->event_page), &error_abort);
118267e071bSRoman Kagan     synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
119267e071bSRoman Kagan     synic->event_page = memory_region_get_ram_ptr(&synic->event_page_mr);
12064ddecc8SJon Doron     qemu_mutex_init(&synic->sint_routes_mutex);
12164ddecc8SJon Doron     QLIST_INIT(&synic->sint_routes);
122267e071bSRoman Kagan 
123267e071bSRoman Kagan     g_free(msgp_name);
124267e071bSRoman Kagan     g_free(eventp_name);
125267e071bSRoman Kagan }
12664ddecc8SJon Doron 
synic_reset(DeviceState * dev)127606c34bfSRoman Kagan static void synic_reset(DeviceState *dev)
128606c34bfSRoman Kagan {
129606c34bfSRoman Kagan     SynICState *synic = SYNIC(dev);
130267e071bSRoman Kagan     memset(synic->msg_page, 0, sizeof(*synic->msg_page));
131267e071bSRoman Kagan     memset(synic->event_page, 0, sizeof(*synic->event_page));
132606c34bfSRoman Kagan     synic_update(synic, false, 0, 0);
13364ddecc8SJon Doron     assert(QLIST_EMPTY(&synic->sint_routes));
134606c34bfSRoman Kagan }
135606c34bfSRoman Kagan 
synic_class_init(ObjectClass * klass,void * data)136606c34bfSRoman Kagan static void synic_class_init(ObjectClass *klass, void *data)
137606c34bfSRoman Kagan {
138606c34bfSRoman Kagan     DeviceClass *dc = DEVICE_CLASS(klass);
139606c34bfSRoman Kagan 
140606c34bfSRoman Kagan     dc->realize = synic_realize;
141*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, synic_reset);
142606c34bfSRoman Kagan     dc->user_creatable = false;
143606c34bfSRoman Kagan }
144606c34bfSRoman Kagan 
hyperv_synic_add(CPUState * cs)145606c34bfSRoman Kagan void hyperv_synic_add(CPUState *cs)
146606c34bfSRoman Kagan {
147606c34bfSRoman Kagan     Object *obj;
148606c34bfSRoman Kagan     SynICState *synic;
149606c34bfSRoman Kagan 
150606c34bfSRoman Kagan     obj = object_new(TYPE_SYNIC);
151606c34bfSRoman Kagan     synic = SYNIC(obj);
152606c34bfSRoman Kagan     synic->cs = cs;
153d2623129SMarkus Armbruster     object_property_add_child(OBJECT(cs), "synic", obj);
154606c34bfSRoman Kagan     object_unref(obj);
155ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(obj), NULL, &error_abort);
156d42cd961SJon Doron     synic_enabled = true;
157606c34bfSRoman Kagan }
158606c34bfSRoman Kagan 
hyperv_synic_reset(CPUState * cs)159606c34bfSRoman Kagan void hyperv_synic_reset(CPUState *cs)
160606c34bfSRoman Kagan {
16130a759b6SRoman Kagan     SynICState *synic = get_synic(cs);
16230a759b6SRoman Kagan 
16330a759b6SRoman Kagan     if (synic) {
1647764963bSPeter Maydell         device_cold_reset(DEVICE(synic));
16530a759b6SRoman Kagan     }
166606c34bfSRoman Kagan }
167606c34bfSRoman Kagan 
168606c34bfSRoman Kagan static const TypeInfo synic_type_info = {
169606c34bfSRoman Kagan     .name = TYPE_SYNIC,
170606c34bfSRoman Kagan     .parent = TYPE_DEVICE,
171606c34bfSRoman Kagan     .instance_size = sizeof(SynICState),
172606c34bfSRoman Kagan     .class_init = synic_class_init,
173606c34bfSRoman Kagan };
174606c34bfSRoman Kagan 
synic_register_types(void)175606c34bfSRoman Kagan static void synic_register_types(void)
176606c34bfSRoman Kagan {
177606c34bfSRoman Kagan     type_register_static(&synic_type_info);
178606c34bfSRoman Kagan }
179606c34bfSRoman Kagan 
180606c34bfSRoman Kagan type_init(synic_register_types)
181606c34bfSRoman Kagan 
1824cbaf3c1SRoman Kagan /*
1834cbaf3c1SRoman Kagan  * KVM has its own message producers (SynIC timers).  To guarantee
1844cbaf3c1SRoman Kagan  * serialization with both KVM vcpu and the guest cpu, the messages are first
1854cbaf3c1SRoman Kagan  * staged in an intermediate area and then posted to the SynIC message page in
1864cbaf3c1SRoman Kagan  * the vcpu thread.
1874cbaf3c1SRoman Kagan  */
1884cbaf3c1SRoman Kagan typedef struct HvSintStagedMessage {
1894cbaf3c1SRoman Kagan     /* message content staged by hyperv_post_msg */
1904cbaf3c1SRoman Kagan     struct hyperv_message msg;
1914cbaf3c1SRoman Kagan     /* callback + data (r/o) to complete the processing in a BH */
1924cbaf3c1SRoman Kagan     HvSintMsgCb cb;
1934cbaf3c1SRoman Kagan     void *cb_data;
1944cbaf3c1SRoman Kagan     /* message posting status filled by cpu_post_msg */
1954cbaf3c1SRoman Kagan     int status;
1964cbaf3c1SRoman Kagan     /* passing the buck: */
1974cbaf3c1SRoman Kagan     enum {
1984cbaf3c1SRoman Kagan         /* initial state */
1994cbaf3c1SRoman Kagan         HV_STAGED_MSG_FREE,
2004cbaf3c1SRoman Kagan         /*
2014cbaf3c1SRoman Kagan          * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE ->
2024cbaf3c1SRoman Kagan          * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu
2034cbaf3c1SRoman Kagan          */
2044cbaf3c1SRoman Kagan         HV_STAGED_MSG_BUSY,
2054cbaf3c1SRoman Kagan         /*
2064cbaf3c1SRoman Kagan          * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot,
2074cbaf3c1SRoman Kagan          * notify the guest, records the status, marks the posting done (BUSY
2084cbaf3c1SRoman Kagan          * -> POSTED), and schedules sint_msg_bh BH
2094cbaf3c1SRoman Kagan          */
2104cbaf3c1SRoman Kagan         HV_STAGED_MSG_POSTED,
2114cbaf3c1SRoman Kagan         /*
2124cbaf3c1SRoman Kagan          * sint_msg_bh (BH) verifies that the posting is done, runs the
2134cbaf3c1SRoman Kagan          * callback, and starts over (POSTED -> FREE)
2144cbaf3c1SRoman Kagan          */
2154cbaf3c1SRoman Kagan     } state;
2164cbaf3c1SRoman Kagan } HvSintStagedMessage;
2174cbaf3c1SRoman Kagan 
218701189e3SRoman Kagan struct HvSintRoute {
219701189e3SRoman Kagan     uint32_t sint;
220606c34bfSRoman Kagan     SynICState *synic;
221701189e3SRoman Kagan     int gsi;
222701189e3SRoman Kagan     EventNotifier sint_set_notifier;
223701189e3SRoman Kagan     EventNotifier sint_ack_notifier;
2244cbaf3c1SRoman Kagan 
2254cbaf3c1SRoman Kagan     HvSintStagedMessage *staged_msg;
2264cbaf3c1SRoman Kagan 
227701189e3SRoman Kagan     unsigned refcount;
22864ddecc8SJon Doron     QLIST_ENTRY(HvSintRoute) link;
229701189e3SRoman Kagan };
230701189e3SRoman Kagan 
hyperv_find_vcpu(uint32_t vp_index)231701189e3SRoman Kagan static CPUState *hyperv_find_vcpu(uint32_t vp_index)
232701189e3SRoman Kagan {
233701189e3SRoman Kagan     CPUState *cs = qemu_get_cpu(vp_index);
234701189e3SRoman Kagan     assert(hyperv_vp_index(cs) == vp_index);
235701189e3SRoman Kagan     return cs;
236701189e3SRoman Kagan }
237701189e3SRoman Kagan 
2384cbaf3c1SRoman Kagan /*
2394cbaf3c1SRoman Kagan  * BH to complete the processing of a staged message.
2404cbaf3c1SRoman Kagan  */
sint_msg_bh(void * opaque)2414cbaf3c1SRoman Kagan static void sint_msg_bh(void *opaque)
2424cbaf3c1SRoman Kagan {
2434cbaf3c1SRoman Kagan     HvSintRoute *sint_route = opaque;
2444cbaf3c1SRoman Kagan     HvSintStagedMessage *staged_msg = sint_route->staged_msg;
2454cbaf3c1SRoman Kagan 
246d73415a3SStefan Hajnoczi     if (qatomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
2474cbaf3c1SRoman Kagan         /* status nor ready yet (spurious ack from guest?), ignore */
2484cbaf3c1SRoman Kagan         return;
2494cbaf3c1SRoman Kagan     }
2504cbaf3c1SRoman Kagan 
2514cbaf3c1SRoman Kagan     staged_msg->cb(staged_msg->cb_data, staged_msg->status);
2524cbaf3c1SRoman Kagan     staged_msg->status = 0;
2534cbaf3c1SRoman Kagan 
2544cbaf3c1SRoman Kagan     /* staged message processing finished, ready to start over */
255d73415a3SStefan Hajnoczi     qatomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
2564cbaf3c1SRoman Kagan     /* drop the reference taken in hyperv_post_msg */
2574cbaf3c1SRoman Kagan     hyperv_sint_route_unref(sint_route);
2584cbaf3c1SRoman Kagan }
2594cbaf3c1SRoman Kagan 
2604cbaf3c1SRoman Kagan /*
2614cbaf3c1SRoman Kagan  * Worker to transfer the message from the staging area into the SynIC message
2624cbaf3c1SRoman Kagan  * page in vcpu context.
2634cbaf3c1SRoman Kagan  */
cpu_post_msg(CPUState * cs,run_on_cpu_data data)2644cbaf3c1SRoman Kagan static void cpu_post_msg(CPUState *cs, run_on_cpu_data data)
2654cbaf3c1SRoman Kagan {
2664cbaf3c1SRoman Kagan     HvSintRoute *sint_route = data.host_ptr;
2674cbaf3c1SRoman Kagan     HvSintStagedMessage *staged_msg = sint_route->staged_msg;
2684cbaf3c1SRoman Kagan     SynICState *synic = sint_route->synic;
2694cbaf3c1SRoman Kagan     struct hyperv_message *dst_msg;
2704cbaf3c1SRoman Kagan     bool wait_for_sint_ack = false;
2714cbaf3c1SRoman Kagan 
2724cbaf3c1SRoman Kagan     assert(staged_msg->state == HV_STAGED_MSG_BUSY);
2734cbaf3c1SRoman Kagan 
27464ddecc8SJon Doron     if (!synic->msg_page_addr) {
2754cbaf3c1SRoman Kagan         staged_msg->status = -ENXIO;
2764cbaf3c1SRoman Kagan         goto posted;
2774cbaf3c1SRoman Kagan     }
2784cbaf3c1SRoman Kagan 
2794cbaf3c1SRoman Kagan     dst_msg = &synic->msg_page->slot[sint_route->sint];
2804cbaf3c1SRoman Kagan 
2814cbaf3c1SRoman Kagan     if (dst_msg->header.message_type != HV_MESSAGE_NONE) {
2824cbaf3c1SRoman Kagan         dst_msg->header.message_flags |= HV_MESSAGE_FLAG_PENDING;
2834cbaf3c1SRoman Kagan         staged_msg->status = -EAGAIN;
2844cbaf3c1SRoman Kagan         wait_for_sint_ack = true;
2854cbaf3c1SRoman Kagan     } else {
2864cbaf3c1SRoman Kagan         memcpy(dst_msg, &staged_msg->msg, sizeof(*dst_msg));
2874cbaf3c1SRoman Kagan         staged_msg->status = hyperv_sint_route_set_sint(sint_route);
2884cbaf3c1SRoman Kagan     }
2894cbaf3c1SRoman Kagan 
2904cbaf3c1SRoman Kagan     memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page));
2914cbaf3c1SRoman Kagan 
2924cbaf3c1SRoman Kagan posted:
293d73415a3SStefan Hajnoczi     qatomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
2944cbaf3c1SRoman Kagan     /*
2954cbaf3c1SRoman Kagan      * Notify the msg originator of the progress made; if the slot was busy we
2964cbaf3c1SRoman Kagan      * set msg_pending flag in it so it will be the guest who will do EOM and
2974cbaf3c1SRoman Kagan      * trigger the notification from KVM via sint_ack_notifier
2984cbaf3c1SRoman Kagan      */
2994cbaf3c1SRoman Kagan     if (!wait_for_sint_ack) {
3004cbaf3c1SRoman Kagan         aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh,
3014cbaf3c1SRoman Kagan                                 sint_route);
3024cbaf3c1SRoman Kagan     }
3034cbaf3c1SRoman Kagan }
3044cbaf3c1SRoman Kagan 
3054cbaf3c1SRoman Kagan /*
3064cbaf3c1SRoman Kagan  * Post a Hyper-V message to the staging area, for delivery to guest in the
3074cbaf3c1SRoman Kagan  * vcpu thread.
3084cbaf3c1SRoman Kagan  */
hyperv_post_msg(HvSintRoute * sint_route,struct hyperv_message * src_msg)3094cbaf3c1SRoman Kagan int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg)
3104cbaf3c1SRoman Kagan {
3114cbaf3c1SRoman Kagan     HvSintStagedMessage *staged_msg = sint_route->staged_msg;
3124cbaf3c1SRoman Kagan 
3134cbaf3c1SRoman Kagan     assert(staged_msg);
3144cbaf3c1SRoman Kagan 
3154cbaf3c1SRoman Kagan     /* grab the staging area */
316d73415a3SStefan Hajnoczi     if (qatomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
3174cbaf3c1SRoman Kagan                        HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) {
3184cbaf3c1SRoman Kagan         return -EAGAIN;
3194cbaf3c1SRoman Kagan     }
3204cbaf3c1SRoman Kagan 
3214cbaf3c1SRoman Kagan     memcpy(&staged_msg->msg, src_msg, sizeof(*src_msg));
3224cbaf3c1SRoman Kagan 
3234cbaf3c1SRoman Kagan     /* hold a reference on sint_route until the callback is finished */
3244cbaf3c1SRoman Kagan     hyperv_sint_route_ref(sint_route);
3254cbaf3c1SRoman Kagan 
3264cbaf3c1SRoman Kagan     /* schedule message posting attempt in vcpu thread */
3274cbaf3c1SRoman Kagan     async_run_on_cpu(sint_route->synic->cs, cpu_post_msg,
3284cbaf3c1SRoman Kagan                      RUN_ON_CPU_HOST_PTR(sint_route));
3294cbaf3c1SRoman Kagan     return 0;
3304cbaf3c1SRoman Kagan }
3314cbaf3c1SRoman Kagan 
sint_ack_handler(EventNotifier * notifier)3324cbaf3c1SRoman Kagan static void sint_ack_handler(EventNotifier *notifier)
333701189e3SRoman Kagan {
334701189e3SRoman Kagan     HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
335701189e3SRoman Kagan                                            sint_ack_notifier);
336701189e3SRoman Kagan     event_notifier_test_and_clear(notifier);
3374cbaf3c1SRoman Kagan 
3384cbaf3c1SRoman Kagan     /*
3394cbaf3c1SRoman Kagan      * the guest consumed the previous message so complete the current one with
3404cbaf3c1SRoman Kagan      * -EAGAIN and let the msg originator retry
3414cbaf3c1SRoman Kagan      */
3424cbaf3c1SRoman Kagan     aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, sint_route);
343701189e3SRoman Kagan }
344701189e3SRoman Kagan 
345f5642f8bSRoman Kagan /*
346f5642f8bSRoman Kagan  * Set given event flag for a given sint on a given vcpu, and signal the sint.
347f5642f8bSRoman Kagan  */
hyperv_set_event_flag(HvSintRoute * sint_route,unsigned eventno)348f5642f8bSRoman Kagan int hyperv_set_event_flag(HvSintRoute *sint_route, unsigned eventno)
349f5642f8bSRoman Kagan {
350f5642f8bSRoman Kagan     int ret;
351f5642f8bSRoman Kagan     SynICState *synic = sint_route->synic;
352f5642f8bSRoman Kagan     unsigned long *flags, set_mask;
353f5642f8bSRoman Kagan     unsigned set_idx;
354f5642f8bSRoman Kagan 
355f5642f8bSRoman Kagan     if (eventno > HV_EVENT_FLAGS_COUNT) {
356f5642f8bSRoman Kagan         return -EINVAL;
357f5642f8bSRoman Kagan     }
35864ddecc8SJon Doron     if (!synic->sctl_enabled || !synic->event_page_addr) {
359f5642f8bSRoman Kagan         return -ENXIO;
360f5642f8bSRoman Kagan     }
361f5642f8bSRoman Kagan 
362f5642f8bSRoman Kagan     set_idx = BIT_WORD(eventno);
363f5642f8bSRoman Kagan     set_mask = BIT_MASK(eventno);
364f5642f8bSRoman Kagan     flags = synic->event_page->slot[sint_route->sint].flags;
365f5642f8bSRoman Kagan 
366d73415a3SStefan Hajnoczi     if ((qatomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
367f5642f8bSRoman Kagan         memory_region_set_dirty(&synic->event_page_mr, 0,
368f5642f8bSRoman Kagan                                 sizeof(*synic->event_page));
369f5642f8bSRoman Kagan         ret = hyperv_sint_route_set_sint(sint_route);
370f5642f8bSRoman Kagan     } else {
371f5642f8bSRoman Kagan         ret = 0;
372f5642f8bSRoman Kagan     }
373f5642f8bSRoman Kagan     return ret;
374f5642f8bSRoman Kagan }
375f5642f8bSRoman Kagan 
kvm_irqchip_add_hv_sint_route(KVMState * s,uint32_t vcpu,uint32_t sint)37648663349SPaolo Bonzini static int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint)
37748663349SPaolo Bonzini {
37848663349SPaolo Bonzini     struct kvm_irq_routing_entry kroute = {};
37948663349SPaolo Bonzini     int virq;
38048663349SPaolo Bonzini 
38148663349SPaolo Bonzini     if (!kvm_gsi_routing_enabled()) {
38248663349SPaolo Bonzini         return -ENOSYS;
38348663349SPaolo Bonzini     }
38448663349SPaolo Bonzini     virq = kvm_irqchip_get_virq(s);
38548663349SPaolo Bonzini     if (virq < 0) {
38648663349SPaolo Bonzini         return virq;
38748663349SPaolo Bonzini     }
38848663349SPaolo Bonzini 
38948663349SPaolo Bonzini     kroute.gsi = virq;
39048663349SPaolo Bonzini     kroute.type = KVM_IRQ_ROUTING_HV_SINT;
39148663349SPaolo Bonzini     kroute.flags = 0;
39248663349SPaolo Bonzini     kroute.u.hv_sint.vcpu = vcpu;
39348663349SPaolo Bonzini     kroute.u.hv_sint.sint = sint;
39448663349SPaolo Bonzini 
39548663349SPaolo Bonzini     kvm_add_routing_entry(s, &kroute);
39648663349SPaolo Bonzini     kvm_irqchip_commit_routes(s);
39748663349SPaolo Bonzini 
39848663349SPaolo Bonzini     return virq;
39948663349SPaolo Bonzini }
40048663349SPaolo Bonzini 
hyperv_sint_route_new(uint32_t vp_index,uint32_t sint,HvSintMsgCb cb,void * cb_data)401701189e3SRoman Kagan HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
4024cbaf3c1SRoman Kagan                                    HvSintMsgCb cb, void *cb_data)
403701189e3SRoman Kagan {
40464ddecc8SJon Doron     HvSintRoute *sint_route = NULL;
40564ddecc8SJon Doron     EventNotifier *ack_notifier = NULL;
406701189e3SRoman Kagan     int r, gsi;
407701189e3SRoman Kagan     CPUState *cs;
408606c34bfSRoman Kagan     SynICState *synic;
40964ddecc8SJon Doron     bool ack_event_initialized = false;
410701189e3SRoman Kagan 
411701189e3SRoman Kagan     cs = hyperv_find_vcpu(vp_index);
412701189e3SRoman Kagan     if (!cs) {
413701189e3SRoman Kagan         return NULL;
414701189e3SRoman Kagan     }
415701189e3SRoman Kagan 
416606c34bfSRoman Kagan     synic = get_synic(cs);
417606c34bfSRoman Kagan     if (!synic) {
418606c34bfSRoman Kagan         return NULL;
419606c34bfSRoman Kagan     }
420606c34bfSRoman Kagan 
421701189e3SRoman Kagan     sint_route = g_new0(HvSintRoute, 1);
42264ddecc8SJon Doron     if (!sint_route) {
42364ddecc8SJon Doron         return NULL;
424701189e3SRoman Kagan     }
425701189e3SRoman Kagan 
42664ddecc8SJon Doron     sint_route->synic = synic;
42764ddecc8SJon Doron     sint_route->sint = sint;
42864ddecc8SJon Doron     sint_route->refcount = 1;
4294cbaf3c1SRoman Kagan 
4304cbaf3c1SRoman Kagan     ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL;
431701189e3SRoman Kagan     if (ack_notifier) {
4324cbaf3c1SRoman Kagan         sint_route->staged_msg = g_new0(HvSintStagedMessage, 1);
43364ddecc8SJon Doron         if (!sint_route->staged_msg) {
43464ddecc8SJon Doron             goto cleanup_err_sint;
43564ddecc8SJon Doron         }
4364cbaf3c1SRoman Kagan         sint_route->staged_msg->cb = cb;
4374cbaf3c1SRoman Kagan         sint_route->staged_msg->cb_data = cb_data;
4384cbaf3c1SRoman Kagan 
439701189e3SRoman Kagan         r = event_notifier_init(ack_notifier, false);
440701189e3SRoman Kagan         if (r) {
44164ddecc8SJon Doron             goto cleanup_err_sint;
44264ddecc8SJon Doron         }
44364ddecc8SJon Doron         event_notifier_set_handler(ack_notifier, sint_ack_handler);
44464ddecc8SJon Doron         ack_event_initialized = true;
445701189e3SRoman Kagan     }
446701189e3SRoman Kagan 
44764ddecc8SJon Doron     /* See if we are done or we need to setup a GSI for this SintRoute */
44864ddecc8SJon Doron     if (!synic->sctl_enabled) {
44964ddecc8SJon Doron         goto cleanup;
45064ddecc8SJon Doron     }
45164ddecc8SJon Doron 
45264ddecc8SJon Doron     /* We need to setup a GSI for this SintRoute */
45364ddecc8SJon Doron     r = event_notifier_init(&sint_route->sint_set_notifier, false);
45464ddecc8SJon Doron     if (r) {
45564ddecc8SJon Doron         goto cleanup_err_sint;
456701189e3SRoman Kagan     }
457701189e3SRoman Kagan 
458701189e3SRoman Kagan     gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
459701189e3SRoman Kagan     if (gsi < 0) {
46064ddecc8SJon Doron         goto cleanup_err_sint_notifier;
461701189e3SRoman Kagan     }
462701189e3SRoman Kagan 
463701189e3SRoman Kagan     r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
464701189e3SRoman Kagan                                            &sint_route->sint_set_notifier,
465701189e3SRoman Kagan                                            ack_notifier, gsi);
466701189e3SRoman Kagan     if (r) {
46764ddecc8SJon Doron         goto cleanup_err_irqfd;
468701189e3SRoman Kagan     }
469701189e3SRoman Kagan     sint_route->gsi = gsi;
47064ddecc8SJon Doron cleanup:
47164ddecc8SJon Doron     qemu_mutex_lock(&synic->sint_routes_mutex);
47264ddecc8SJon Doron     QLIST_INSERT_HEAD(&synic->sint_routes, sint_route, link);
47364ddecc8SJon Doron     qemu_mutex_unlock(&synic->sint_routes_mutex);
474701189e3SRoman Kagan     return sint_route;
475701189e3SRoman Kagan 
47664ddecc8SJon Doron cleanup_err_irqfd:
477701189e3SRoman Kagan     kvm_irqchip_release_virq(kvm_state, gsi);
47864ddecc8SJon Doron 
47964ddecc8SJon Doron cleanup_err_sint_notifier:
48064ddecc8SJon Doron     event_notifier_cleanup(&sint_route->sint_set_notifier);
48164ddecc8SJon Doron 
48264ddecc8SJon Doron cleanup_err_sint:
483701189e3SRoman Kagan     if (ack_notifier) {
48464ddecc8SJon Doron         if (ack_event_initialized) {
485701189e3SRoman Kagan             event_notifier_set_handler(ack_notifier, NULL);
486701189e3SRoman Kagan             event_notifier_cleanup(ack_notifier);
48764ddecc8SJon Doron         }
48864ddecc8SJon Doron 
4894cbaf3c1SRoman Kagan         g_free(sint_route->staged_msg);
490701189e3SRoman Kagan     }
491701189e3SRoman Kagan 
49264ddecc8SJon Doron     g_free(sint_route);
493701189e3SRoman Kagan     return NULL;
494701189e3SRoman Kagan }
495701189e3SRoman Kagan 
hyperv_sint_route_ref(HvSintRoute * sint_route)496701189e3SRoman Kagan void hyperv_sint_route_ref(HvSintRoute *sint_route)
497701189e3SRoman Kagan {
498701189e3SRoman Kagan     sint_route->refcount++;
499701189e3SRoman Kagan }
500701189e3SRoman Kagan 
hyperv_sint_route_unref(HvSintRoute * sint_route)501701189e3SRoman Kagan void hyperv_sint_route_unref(HvSintRoute *sint_route)
502701189e3SRoman Kagan {
50364ddecc8SJon Doron     SynICState *synic;
50464ddecc8SJon Doron 
505701189e3SRoman Kagan     if (!sint_route) {
506701189e3SRoman Kagan         return;
507701189e3SRoman Kagan     }
508701189e3SRoman Kagan 
509701189e3SRoman Kagan     assert(sint_route->refcount > 0);
510701189e3SRoman Kagan 
511701189e3SRoman Kagan     if (--sint_route->refcount) {
512701189e3SRoman Kagan         return;
513701189e3SRoman Kagan     }
514701189e3SRoman Kagan 
51564ddecc8SJon Doron     synic = sint_route->synic;
51664ddecc8SJon Doron     qemu_mutex_lock(&synic->sint_routes_mutex);
51764ddecc8SJon Doron     QLIST_REMOVE(sint_route, link);
51864ddecc8SJon Doron     qemu_mutex_unlock(&synic->sint_routes_mutex);
51964ddecc8SJon Doron 
52064ddecc8SJon Doron     if (sint_route->gsi) {
521701189e3SRoman Kagan         kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
522701189e3SRoman Kagan                                               &sint_route->sint_set_notifier,
523701189e3SRoman Kagan                                               sint_route->gsi);
524701189e3SRoman Kagan         kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
52564ddecc8SJon Doron         event_notifier_cleanup(&sint_route->sint_set_notifier);
52664ddecc8SJon Doron     }
52764ddecc8SJon Doron 
5284cbaf3c1SRoman Kagan     if (sint_route->staged_msg) {
529701189e3SRoman Kagan         event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
530701189e3SRoman Kagan         event_notifier_cleanup(&sint_route->sint_ack_notifier);
5314cbaf3c1SRoman Kagan         g_free(sint_route->staged_msg);
532701189e3SRoman Kagan     }
533701189e3SRoman Kagan     g_free(sint_route);
534701189e3SRoman Kagan }
535701189e3SRoman Kagan 
hyperv_sint_route_set_sint(HvSintRoute * sint_route)536701189e3SRoman Kagan int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
537701189e3SRoman Kagan {
53864ddecc8SJon Doron     if (!sint_route->gsi) {
53964ddecc8SJon Doron         return 0;
54064ddecc8SJon Doron     }
54164ddecc8SJon Doron 
542701189e3SRoman Kagan     return event_notifier_set(&sint_route->sint_set_notifier);
543701189e3SRoman Kagan }
544e6ea9f45SRoman Kagan 
54576036a5fSRoman Kagan typedef struct MsgHandler {
54676036a5fSRoman Kagan     struct rcu_head rcu;
54776036a5fSRoman Kagan     QLIST_ENTRY(MsgHandler) link;
54876036a5fSRoman Kagan     uint32_t conn_id;
54976036a5fSRoman Kagan     HvMsgHandler handler;
55076036a5fSRoman Kagan     void *data;
55176036a5fSRoman Kagan } MsgHandler;
55276036a5fSRoman Kagan 
553e6ea9f45SRoman Kagan typedef struct EventFlagHandler {
554e6ea9f45SRoman Kagan     struct rcu_head rcu;
555e6ea9f45SRoman Kagan     QLIST_ENTRY(EventFlagHandler) link;
556e6ea9f45SRoman Kagan     uint32_t conn_id;
557e6ea9f45SRoman Kagan     EventNotifier *notifier;
558e6ea9f45SRoman Kagan } EventFlagHandler;
559e6ea9f45SRoman Kagan 
56076036a5fSRoman Kagan static QLIST_HEAD(, MsgHandler) msg_handlers;
561e6ea9f45SRoman Kagan static QLIST_HEAD(, EventFlagHandler) event_flag_handlers;
562e6ea9f45SRoman Kagan static QemuMutex handlers_mutex;
563e6ea9f45SRoman Kagan 
hv_init(void)564e6ea9f45SRoman Kagan static void __attribute__((constructor)) hv_init(void)
565e6ea9f45SRoman Kagan {
56676036a5fSRoman Kagan     QLIST_INIT(&msg_handlers);
567e6ea9f45SRoman Kagan     QLIST_INIT(&event_flag_handlers);
568e6ea9f45SRoman Kagan     qemu_mutex_init(&handlers_mutex);
569e6ea9f45SRoman Kagan }
570e6ea9f45SRoman Kagan 
hyperv_set_msg_handler(uint32_t conn_id,HvMsgHandler handler,void * data)57176036a5fSRoman Kagan int hyperv_set_msg_handler(uint32_t conn_id, HvMsgHandler handler, void *data)
57276036a5fSRoman Kagan {
57376036a5fSRoman Kagan     int ret;
57476036a5fSRoman Kagan     MsgHandler *mh;
57576036a5fSRoman Kagan 
57608b689aaSSimran Singhal     QEMU_LOCK_GUARD(&handlers_mutex);
57776036a5fSRoman Kagan     QLIST_FOREACH(mh, &msg_handlers, link) {
57876036a5fSRoman Kagan         if (mh->conn_id == conn_id) {
57976036a5fSRoman Kagan             if (handler) {
58076036a5fSRoman Kagan                 ret = -EEXIST;
58176036a5fSRoman Kagan             } else {
58276036a5fSRoman Kagan                 QLIST_REMOVE_RCU(mh, link);
58376036a5fSRoman Kagan                 g_free_rcu(mh, rcu);
58476036a5fSRoman Kagan                 ret = 0;
58576036a5fSRoman Kagan             }
58608b689aaSSimran Singhal             return ret;
58776036a5fSRoman Kagan         }
58876036a5fSRoman Kagan     }
58976036a5fSRoman Kagan 
59076036a5fSRoman Kagan     if (handler) {
59176036a5fSRoman Kagan         mh = g_new(MsgHandler, 1);
59276036a5fSRoman Kagan         mh->conn_id = conn_id;
59376036a5fSRoman Kagan         mh->handler = handler;
59476036a5fSRoman Kagan         mh->data = data;
59576036a5fSRoman Kagan         QLIST_INSERT_HEAD_RCU(&msg_handlers, mh, link);
59676036a5fSRoman Kagan         ret = 0;
59776036a5fSRoman Kagan     } else {
59876036a5fSRoman Kagan         ret = -ENOENT;
59976036a5fSRoman Kagan     }
60008b689aaSSimran Singhal 
60176036a5fSRoman Kagan     return ret;
60276036a5fSRoman Kagan }
60376036a5fSRoman Kagan 
hyperv_hcall_post_message(uint64_t param,bool fast)60476036a5fSRoman Kagan uint16_t hyperv_hcall_post_message(uint64_t param, bool fast)
60576036a5fSRoman Kagan {
60676036a5fSRoman Kagan     uint16_t ret;
60776036a5fSRoman Kagan     hwaddr len;
60876036a5fSRoman Kagan     struct hyperv_post_message_input *msg;
60976036a5fSRoman Kagan     MsgHandler *mh;
61076036a5fSRoman Kagan 
61176036a5fSRoman Kagan     if (fast) {
61276036a5fSRoman Kagan         return HV_STATUS_INVALID_HYPERCALL_CODE;
61376036a5fSRoman Kagan     }
61476036a5fSRoman Kagan     if (param & (__alignof__(*msg) - 1)) {
61576036a5fSRoman Kagan         return HV_STATUS_INVALID_ALIGNMENT;
61676036a5fSRoman Kagan     }
61776036a5fSRoman Kagan 
61876036a5fSRoman Kagan     len = sizeof(*msg);
61976036a5fSRoman Kagan     msg = cpu_physical_memory_map(param, &len, 0);
62076036a5fSRoman Kagan     if (len < sizeof(*msg)) {
62176036a5fSRoman Kagan         ret = HV_STATUS_INSUFFICIENT_MEMORY;
62276036a5fSRoman Kagan         goto unmap;
62376036a5fSRoman Kagan     }
62476036a5fSRoman Kagan     if (msg->payload_size > sizeof(msg->payload)) {
62576036a5fSRoman Kagan         ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
62676036a5fSRoman Kagan         goto unmap;
62776036a5fSRoman Kagan     }
62876036a5fSRoman Kagan 
62976036a5fSRoman Kagan     ret = HV_STATUS_INVALID_CONNECTION_ID;
630b66173afSDr. David Alan Gilbert     WITH_RCU_READ_LOCK_GUARD() {
63176036a5fSRoman Kagan         QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
63276036a5fSRoman Kagan             if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
63376036a5fSRoman Kagan                 ret = mh->handler(msg, mh->data);
63476036a5fSRoman Kagan                 break;
63576036a5fSRoman Kagan             }
63676036a5fSRoman Kagan         }
637b66173afSDr. David Alan Gilbert     }
63876036a5fSRoman Kagan 
63976036a5fSRoman Kagan unmap:
64076036a5fSRoman Kagan     cpu_physical_memory_unmap(msg, len, 0, 0);
64176036a5fSRoman Kagan     return ret;
64276036a5fSRoman Kagan }
64376036a5fSRoman Kagan 
set_event_flag_handler(uint32_t conn_id,EventNotifier * notifier)6448d3bc0b7SRoman Kagan static int set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
645e6ea9f45SRoman Kagan {
646e6ea9f45SRoman Kagan     int ret;
647e6ea9f45SRoman Kagan     EventFlagHandler *handler;
648e6ea9f45SRoman Kagan 
64908b689aaSSimran Singhal     QEMU_LOCK_GUARD(&handlers_mutex);
650e6ea9f45SRoman Kagan     QLIST_FOREACH(handler, &event_flag_handlers, link) {
651e6ea9f45SRoman Kagan         if (handler->conn_id == conn_id) {
652e6ea9f45SRoman Kagan             if (notifier) {
653e6ea9f45SRoman Kagan                 ret = -EEXIST;
654e6ea9f45SRoman Kagan             } else {
655e6ea9f45SRoman Kagan                 QLIST_REMOVE_RCU(handler, link);
656e6ea9f45SRoman Kagan                 g_free_rcu(handler, rcu);
657e6ea9f45SRoman Kagan                 ret = 0;
658e6ea9f45SRoman Kagan             }
65908b689aaSSimran Singhal             return ret;
660e6ea9f45SRoman Kagan         }
661e6ea9f45SRoman Kagan     }
662e6ea9f45SRoman Kagan 
663e6ea9f45SRoman Kagan     if (notifier) {
664e6ea9f45SRoman Kagan         handler = g_new(EventFlagHandler, 1);
665e6ea9f45SRoman Kagan         handler->conn_id = conn_id;
666e6ea9f45SRoman Kagan         handler->notifier = notifier;
667e6ea9f45SRoman Kagan         QLIST_INSERT_HEAD_RCU(&event_flag_handlers, handler, link);
668e6ea9f45SRoman Kagan         ret = 0;
669e6ea9f45SRoman Kagan     } else {
670e6ea9f45SRoman Kagan         ret = -ENOENT;
671e6ea9f45SRoman Kagan     }
67208b689aaSSimran Singhal 
673e6ea9f45SRoman Kagan     return ret;
674e6ea9f45SRoman Kagan }
675e6ea9f45SRoman Kagan 
6768d3bc0b7SRoman Kagan static bool process_event_flags_userspace;
6778d3bc0b7SRoman Kagan 
hyperv_set_event_flag_handler(uint32_t conn_id,EventNotifier * notifier)6788d3bc0b7SRoman Kagan int hyperv_set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
6798d3bc0b7SRoman Kagan {
6808d3bc0b7SRoman Kagan     if (!process_event_flags_userspace &&
6818d3bc0b7SRoman Kagan         !kvm_check_extension(kvm_state, KVM_CAP_HYPERV_EVENTFD)) {
6828d3bc0b7SRoman Kagan         process_event_flags_userspace = true;
6838d3bc0b7SRoman Kagan 
6848d3bc0b7SRoman Kagan         warn_report("Hyper-V event signaling is not supported by this kernel; "
6858d3bc0b7SRoman Kagan                     "using slower userspace hypercall processing");
6868d3bc0b7SRoman Kagan     }
6878d3bc0b7SRoman Kagan 
6888d3bc0b7SRoman Kagan     if (!process_event_flags_userspace) {
6898d3bc0b7SRoman Kagan         struct kvm_hyperv_eventfd hvevfd = {
6908d3bc0b7SRoman Kagan             .conn_id = conn_id,
6918d3bc0b7SRoman Kagan             .fd = notifier ? event_notifier_get_fd(notifier) : -1,
6928d3bc0b7SRoman Kagan             .flags = notifier ? 0 : KVM_HYPERV_EVENTFD_DEASSIGN,
6938d3bc0b7SRoman Kagan         };
6948d3bc0b7SRoman Kagan 
6958d3bc0b7SRoman Kagan         return kvm_vm_ioctl(kvm_state, KVM_HYPERV_EVENTFD, &hvevfd);
6968d3bc0b7SRoman Kagan     }
6978d3bc0b7SRoman Kagan     return set_event_flag_handler(conn_id, notifier);
6988d3bc0b7SRoman Kagan }
6998d3bc0b7SRoman Kagan 
hyperv_hcall_signal_event(uint64_t param,bool fast)700e6ea9f45SRoman Kagan uint16_t hyperv_hcall_signal_event(uint64_t param, bool fast)
701e6ea9f45SRoman Kagan {
702e6ea9f45SRoman Kagan     EventFlagHandler *handler;
703e6ea9f45SRoman Kagan 
704e6ea9f45SRoman Kagan     if (unlikely(!fast)) {
705e6ea9f45SRoman Kagan         hwaddr addr = param;
706e6ea9f45SRoman Kagan 
707e6ea9f45SRoman Kagan         if (addr & (__alignof__(addr) - 1)) {
708e6ea9f45SRoman Kagan             return HV_STATUS_INVALID_ALIGNMENT;
709e6ea9f45SRoman Kagan         }
710e6ea9f45SRoman Kagan 
711e6ea9f45SRoman Kagan         param = ldq_phys(&address_space_memory, addr);
712e6ea9f45SRoman Kagan     }
713e6ea9f45SRoman Kagan 
714e6ea9f45SRoman Kagan     /*
715e6ea9f45SRoman Kagan      * Per spec, bits 32-47 contain the extra "flag number".  However, we
716e6ea9f45SRoman Kagan      * have no use for it, and in all known usecases it is zero, so just
717e6ea9f45SRoman Kagan      * report lookup failure if it isn't.
718e6ea9f45SRoman Kagan      */
719e6ea9f45SRoman Kagan     if (param & 0xffff00000000ULL) {
720e6ea9f45SRoman Kagan         return HV_STATUS_INVALID_PORT_ID;
721e6ea9f45SRoman Kagan     }
722e6ea9f45SRoman Kagan     /* remaining bits are reserved-zero */
723e6ea9f45SRoman Kagan     if (param & ~HV_CONNECTION_ID_MASK) {
724e6ea9f45SRoman Kagan         return HV_STATUS_INVALID_HYPERCALL_INPUT;
725e6ea9f45SRoman Kagan     }
726e6ea9f45SRoman Kagan 
727b66173afSDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
728e6ea9f45SRoman Kagan     QLIST_FOREACH_RCU(handler, &event_flag_handlers, link) {
729e6ea9f45SRoman Kagan         if (handler->conn_id == param) {
730e6ea9f45SRoman Kagan             event_notifier_set(handler->notifier);
731b66173afSDr. David Alan Gilbert             return 0;
732e6ea9f45SRoman Kagan         }
733e6ea9f45SRoman Kagan     }
734b66173afSDr. David Alan Gilbert     return HV_STATUS_INVALID_CONNECTION_ID;
735e6ea9f45SRoman Kagan }
73673d24074SJon Doron 
73773d24074SJon Doron static HvSynDbgHandler hv_syndbg_handler;
73873d24074SJon Doron static void *hv_syndbg_context;
73973d24074SJon Doron 
hyperv_set_syndbg_handler(HvSynDbgHandler handler,void * context)74073d24074SJon Doron void hyperv_set_syndbg_handler(HvSynDbgHandler handler, void *context)
74173d24074SJon Doron {
74273d24074SJon Doron     assert(!hv_syndbg_handler);
74373d24074SJon Doron     hv_syndbg_handler = handler;
74473d24074SJon Doron     hv_syndbg_context = context;
74573d24074SJon Doron }
74673d24074SJon Doron 
hyperv_hcall_reset_dbg_session(uint64_t outgpa)74773d24074SJon Doron uint16_t hyperv_hcall_reset_dbg_session(uint64_t outgpa)
74873d24074SJon Doron {
74973d24074SJon Doron     uint16_t ret;
75073d24074SJon Doron     HvSynDbgMsg msg;
75173d24074SJon Doron     struct hyperv_reset_debug_session_output *reset_dbg_session = NULL;
75273d24074SJon Doron     hwaddr len;
75373d24074SJon Doron 
75473d24074SJon Doron     if (!hv_syndbg_handler) {
75573d24074SJon Doron         ret = HV_STATUS_INVALID_HYPERCALL_CODE;
75673d24074SJon Doron         goto cleanup;
75773d24074SJon Doron     }
75873d24074SJon Doron 
75973d24074SJon Doron     len = sizeof(*reset_dbg_session);
76073d24074SJon Doron     reset_dbg_session = cpu_physical_memory_map(outgpa, &len, 1);
76173d24074SJon Doron     if (!reset_dbg_session || len < sizeof(*reset_dbg_session)) {
76273d24074SJon Doron         ret = HV_STATUS_INSUFFICIENT_MEMORY;
76373d24074SJon Doron         goto cleanup;
76473d24074SJon Doron     }
76573d24074SJon Doron 
76673d24074SJon Doron     msg.type = HV_SYNDBG_MSG_CONNECTION_INFO;
76773d24074SJon Doron     ret = hv_syndbg_handler(hv_syndbg_context, &msg);
76873d24074SJon Doron     if (ret) {
76973d24074SJon Doron         goto cleanup;
77073d24074SJon Doron     }
77173d24074SJon Doron 
77273d24074SJon Doron     reset_dbg_session->host_ip = msg.u.connection_info.host_ip;
77373d24074SJon Doron     reset_dbg_session->host_port = msg.u.connection_info.host_port;
77473d24074SJon Doron     /* The following fields are only used as validation for KDVM */
77573d24074SJon Doron     memset(&reset_dbg_session->host_mac, 0,
77673d24074SJon Doron            sizeof(reset_dbg_session->host_mac));
77773d24074SJon Doron     reset_dbg_session->target_ip = msg.u.connection_info.host_ip;
77873d24074SJon Doron     reset_dbg_session->target_port = msg.u.connection_info.host_port;
77973d24074SJon Doron     memset(&reset_dbg_session->target_mac, 0,
78073d24074SJon Doron            sizeof(reset_dbg_session->target_mac));
78173d24074SJon Doron cleanup:
78273d24074SJon Doron     if (reset_dbg_session) {
78373d24074SJon Doron         cpu_physical_memory_unmap(reset_dbg_session,
78473d24074SJon Doron                                   sizeof(*reset_dbg_session), 1, len);
78573d24074SJon Doron     }
78673d24074SJon Doron 
78773d24074SJon Doron     return ret;
78873d24074SJon Doron }
78973d24074SJon Doron 
hyperv_hcall_retreive_dbg_data(uint64_t ingpa,uint64_t outgpa,bool fast)79073d24074SJon Doron uint16_t hyperv_hcall_retreive_dbg_data(uint64_t ingpa, uint64_t outgpa,
79173d24074SJon Doron                                         bool fast)
79273d24074SJon Doron {
79373d24074SJon Doron     uint16_t ret;
79473d24074SJon Doron     struct hyperv_retrieve_debug_data_input *debug_data_in = NULL;
79573d24074SJon Doron     struct hyperv_retrieve_debug_data_output *debug_data_out = NULL;
79673d24074SJon Doron     hwaddr in_len, out_len;
79773d24074SJon Doron     HvSynDbgMsg msg;
79873d24074SJon Doron 
79973d24074SJon Doron     if (fast || !hv_syndbg_handler) {
80073d24074SJon Doron         ret = HV_STATUS_INVALID_HYPERCALL_CODE;
80173d24074SJon Doron         goto cleanup;
80273d24074SJon Doron     }
80373d24074SJon Doron 
80473d24074SJon Doron     in_len = sizeof(*debug_data_in);
80573d24074SJon Doron     debug_data_in = cpu_physical_memory_map(ingpa, &in_len, 0);
80673d24074SJon Doron     if (!debug_data_in || in_len < sizeof(*debug_data_in)) {
80773d24074SJon Doron         ret = HV_STATUS_INSUFFICIENT_MEMORY;
80873d24074SJon Doron         goto cleanup;
80973d24074SJon Doron     }
81073d24074SJon Doron 
81173d24074SJon Doron     out_len = sizeof(*debug_data_out);
81273d24074SJon Doron     debug_data_out = cpu_physical_memory_map(outgpa, &out_len, 1);
81373d24074SJon Doron     if (!debug_data_out || out_len < sizeof(*debug_data_out)) {
81473d24074SJon Doron         ret = HV_STATUS_INSUFFICIENT_MEMORY;
81573d24074SJon Doron         goto cleanup;
81673d24074SJon Doron     }
81773d24074SJon Doron 
81873d24074SJon Doron     msg.type = HV_SYNDBG_MSG_RECV;
81973d24074SJon Doron     msg.u.recv.buf_gpa = outgpa + sizeof(*debug_data_out);
82073d24074SJon Doron     msg.u.recv.count = TARGET_PAGE_SIZE - sizeof(*debug_data_out);
82173d24074SJon Doron     msg.u.recv.options = debug_data_in->options;
82273d24074SJon Doron     msg.u.recv.timeout = debug_data_in->timeout;
82373d24074SJon Doron     msg.u.recv.is_raw = true;
82473d24074SJon Doron     ret = hv_syndbg_handler(hv_syndbg_context, &msg);
82573d24074SJon Doron     if (ret == HV_STATUS_NO_DATA) {
82673d24074SJon Doron         debug_data_out->retrieved_count = 0;
82773d24074SJon Doron         debug_data_out->remaining_count = debug_data_in->count;
82873d24074SJon Doron         goto cleanup;
82973d24074SJon Doron     } else if (ret != HV_STATUS_SUCCESS) {
83073d24074SJon Doron         goto cleanup;
83173d24074SJon Doron     }
83273d24074SJon Doron 
83373d24074SJon Doron     debug_data_out->retrieved_count = msg.u.recv.retrieved_count;
83473d24074SJon Doron     debug_data_out->remaining_count =
83573d24074SJon Doron         debug_data_in->count - msg.u.recv.retrieved_count;
83673d24074SJon Doron cleanup:
83773d24074SJon Doron     if (debug_data_out) {
83873d24074SJon Doron         cpu_physical_memory_unmap(debug_data_out, sizeof(*debug_data_out), 1,
83973d24074SJon Doron                                   out_len);
84073d24074SJon Doron     }
84173d24074SJon Doron 
84273d24074SJon Doron     if (debug_data_in) {
84373d24074SJon Doron         cpu_physical_memory_unmap(debug_data_in, sizeof(*debug_data_in), 0,
84473d24074SJon Doron                                   in_len);
84573d24074SJon Doron     }
84673d24074SJon Doron 
84773d24074SJon Doron     return ret;
84873d24074SJon Doron }
84973d24074SJon Doron 
hyperv_hcall_post_dbg_data(uint64_t ingpa,uint64_t outgpa,bool fast)85073d24074SJon Doron uint16_t hyperv_hcall_post_dbg_data(uint64_t ingpa, uint64_t outgpa, bool fast)
85173d24074SJon Doron {
85273d24074SJon Doron     uint16_t ret;
85373d24074SJon Doron     struct hyperv_post_debug_data_input *post_data_in = NULL;
85473d24074SJon Doron     struct hyperv_post_debug_data_output *post_data_out = NULL;
85573d24074SJon Doron     hwaddr in_len, out_len;
85673d24074SJon Doron     HvSynDbgMsg msg;
85773d24074SJon Doron 
85873d24074SJon Doron     if (fast || !hv_syndbg_handler) {
85973d24074SJon Doron         ret = HV_STATUS_INVALID_HYPERCALL_CODE;
86073d24074SJon Doron         goto cleanup;
86173d24074SJon Doron     }
86273d24074SJon Doron 
86373d24074SJon Doron     in_len = sizeof(*post_data_in);
86473d24074SJon Doron     post_data_in = cpu_physical_memory_map(ingpa, &in_len, 0);
86573d24074SJon Doron     if (!post_data_in || in_len < sizeof(*post_data_in)) {
86673d24074SJon Doron         ret = HV_STATUS_INSUFFICIENT_MEMORY;
86773d24074SJon Doron         goto cleanup;
86873d24074SJon Doron     }
86973d24074SJon Doron 
87073d24074SJon Doron     if (post_data_in->count > TARGET_PAGE_SIZE - sizeof(*post_data_in)) {
87173d24074SJon Doron         ret = HV_STATUS_INVALID_PARAMETER;
87273d24074SJon Doron         goto cleanup;
87373d24074SJon Doron     }
87473d24074SJon Doron 
87573d24074SJon Doron     out_len = sizeof(*post_data_out);
87673d24074SJon Doron     post_data_out = cpu_physical_memory_map(outgpa, &out_len, 1);
87773d24074SJon Doron     if (!post_data_out || out_len < sizeof(*post_data_out)) {
87873d24074SJon Doron         ret = HV_STATUS_INSUFFICIENT_MEMORY;
87973d24074SJon Doron         goto cleanup;
88073d24074SJon Doron     }
88173d24074SJon Doron 
88273d24074SJon Doron     msg.type = HV_SYNDBG_MSG_SEND;
88373d24074SJon Doron     msg.u.send.buf_gpa = ingpa + sizeof(*post_data_in);
88473d24074SJon Doron     msg.u.send.count = post_data_in->count;
88573d24074SJon Doron     msg.u.send.is_raw = true;
88673d24074SJon Doron     ret = hv_syndbg_handler(hv_syndbg_context, &msg);
88773d24074SJon Doron     if (ret != HV_STATUS_SUCCESS) {
88873d24074SJon Doron         goto cleanup;
88973d24074SJon Doron     }
89073d24074SJon Doron 
89173d24074SJon Doron     post_data_out->pending_count = msg.u.send.pending_count;
89273d24074SJon Doron     ret = post_data_out->pending_count ? HV_STATUS_INSUFFICIENT_BUFFERS :
89373d24074SJon Doron                                          HV_STATUS_SUCCESS;
89473d24074SJon Doron cleanup:
89573d24074SJon Doron     if (post_data_out) {
89673d24074SJon Doron         cpu_physical_memory_unmap(post_data_out,
89773d24074SJon Doron                                   sizeof(*post_data_out), 1, out_len);
89873d24074SJon Doron     }
89973d24074SJon Doron 
90073d24074SJon Doron     if (post_data_in) {
90173d24074SJon Doron         cpu_physical_memory_unmap(post_data_in,
90273d24074SJon Doron                                   sizeof(*post_data_in), 0, in_len);
90373d24074SJon Doron     }
90473d24074SJon Doron 
90573d24074SJon Doron     return ret;
90673d24074SJon Doron }
90773d24074SJon Doron 
hyperv_syndbg_send(uint64_t ingpa,uint32_t count)90873d24074SJon Doron uint32_t hyperv_syndbg_send(uint64_t ingpa, uint32_t count)
90973d24074SJon Doron {
91073d24074SJon Doron     HvSynDbgMsg msg;
91173d24074SJon Doron 
91273d24074SJon Doron     if (!hv_syndbg_handler) {
91373d24074SJon Doron         return HV_SYNDBG_STATUS_INVALID;
91473d24074SJon Doron     }
91573d24074SJon Doron 
91673d24074SJon Doron     msg.type = HV_SYNDBG_MSG_SEND;
91773d24074SJon Doron     msg.u.send.buf_gpa = ingpa;
91873d24074SJon Doron     msg.u.send.count = count;
91973d24074SJon Doron     msg.u.send.is_raw = false;
92073d24074SJon Doron     if (hv_syndbg_handler(hv_syndbg_context, &msg)) {
92173d24074SJon Doron         return HV_SYNDBG_STATUS_INVALID;
92273d24074SJon Doron     }
92373d24074SJon Doron 
92473d24074SJon Doron     return HV_SYNDBG_STATUS_SEND_SUCCESS;
92573d24074SJon Doron }
92673d24074SJon Doron 
hyperv_syndbg_recv(uint64_t ingpa,uint32_t count)92773d24074SJon Doron uint32_t hyperv_syndbg_recv(uint64_t ingpa, uint32_t count)
92873d24074SJon Doron {
92973d24074SJon Doron     uint16_t ret;
93073d24074SJon Doron     HvSynDbgMsg msg;
93173d24074SJon Doron 
93273d24074SJon Doron     if (!hv_syndbg_handler) {
93373d24074SJon Doron         return HV_SYNDBG_STATUS_INVALID;
93473d24074SJon Doron     }
93573d24074SJon Doron 
93673d24074SJon Doron     msg.type = HV_SYNDBG_MSG_RECV;
93773d24074SJon Doron     msg.u.recv.buf_gpa = ingpa;
93873d24074SJon Doron     msg.u.recv.count = count;
93973d24074SJon Doron     msg.u.recv.options = 0;
94073d24074SJon Doron     msg.u.recv.timeout = 0;
94173d24074SJon Doron     msg.u.recv.is_raw = false;
94273d24074SJon Doron     ret = hv_syndbg_handler(hv_syndbg_context, &msg);
94373d24074SJon Doron     if (ret != HV_STATUS_SUCCESS) {
94473d24074SJon Doron         return 0;
94573d24074SJon Doron     }
94673d24074SJon Doron 
94773d24074SJon Doron     return HV_SYNDBG_STATUS_SET_SIZE(HV_SYNDBG_STATUS_RECV_SUCCESS,
94873d24074SJon Doron                                      msg.u.recv.retrieved_count);
94973d24074SJon Doron }
95073d24074SJon Doron 
hyperv_syndbg_set_pending_page(uint64_t ingpa)95173d24074SJon Doron void hyperv_syndbg_set_pending_page(uint64_t ingpa)
95273d24074SJon Doron {
95373d24074SJon Doron     HvSynDbgMsg msg;
95473d24074SJon Doron 
95573d24074SJon Doron     if (!hv_syndbg_handler) {
95673d24074SJon Doron         return;
95773d24074SJon Doron     }
95873d24074SJon Doron 
95973d24074SJon Doron     msg.type = HV_SYNDBG_MSG_SET_PENDING_PAGE;
96073d24074SJon Doron     msg.u.pending_page.buf_gpa = ingpa;
96173d24074SJon Doron     hv_syndbg_handler(hv_syndbg_context, &msg);
96273d24074SJon Doron }
96373d24074SJon Doron 
hyperv_syndbg_query_options(void)96473d24074SJon Doron uint64_t hyperv_syndbg_query_options(void)
96573d24074SJon Doron {
96673d24074SJon Doron     HvSynDbgMsg msg;
96773d24074SJon Doron 
96873d24074SJon Doron     if (!hv_syndbg_handler) {
96973d24074SJon Doron         return 0;
97073d24074SJon Doron     }
97173d24074SJon Doron 
97273d24074SJon Doron     msg.type = HV_SYNDBG_MSG_QUERY_OPTIONS;
97373d24074SJon Doron     if (hv_syndbg_handler(hv_syndbg_context, &msg) != HV_STATUS_SUCCESS) {
97473d24074SJon Doron         return 0;
97573d24074SJon Doron     }
97673d24074SJon Doron 
97773d24074SJon Doron     return msg.u.query_options.options;
97873d24074SJon Doron }
9796093637bSMaciej S. Szmigiero 
9806093637bSMaciej S. Szmigiero static bool vmbus_recommended_features_enabled;
9816093637bSMaciej S. Szmigiero 
hyperv_are_vmbus_recommended_features_enabled(void)9826093637bSMaciej S. Szmigiero bool hyperv_are_vmbus_recommended_features_enabled(void)
9836093637bSMaciej S. Szmigiero {
9846093637bSMaciej S. Szmigiero     return vmbus_recommended_features_enabled;
9856093637bSMaciej S. Szmigiero }
9866093637bSMaciej S. Szmigiero 
hyperv_set_vmbus_recommended_features_enabled(void)9876093637bSMaciej S. Szmigiero void hyperv_set_vmbus_recommended_features_enabled(void)
9886093637bSMaciej S. Szmigiero {
9896093637bSMaciej S. Szmigiero     vmbus_recommended_features_enabled = true;
9906093637bSMaciej S. Szmigiero }
991