Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/hyperv/hyperv.c
Line
Count
Source
1
/*
2
 * Hyper-V guest/hypervisor interaction
3
 *
4
 * Copyright (c) 2015-2018 Virtuozzo International GmbH.
5
 *
6
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7
 * See the COPYING file in the top-level directory.
8
 */
9
10
#include "qemu/osdep.h"
11
#include "qemu/main-loop.h"
12
#include "qemu/module.h"
13
#include "qapi/error.h"
14
#include "system/address-spaces.h"
15
#include "system/memory.h"
16
#include "exec/target_page.h"
17
#include "exec/cpu-common.h"
18
#include "linux/kvm.h"
19
#include "system/kvm.h"
20
#include "system/physmem.h"
21
#include "qemu/bitops.h"
22
#include "qemu/error-report.h"
23
#include "qemu/lockable.h"
24
#include "qemu/queue.h"
25
#include "qemu/rcu.h"
26
#include "qemu/rcu_queue.h"
27
#include "hw/hyperv/hyperv.h"
28
#include "qom/object.h"
29
#include "target/i386/kvm/hyperv-proto.h"
30
31
struct SynICState {
32
    DeviceState parent_obj;
33
34
    CPUState *cs;
35
36
    bool sctl_enabled;
37
    hwaddr msg_page_addr;
38
    hwaddr event_page_addr;
39
    MemoryRegion msg_page_mr;
40
    MemoryRegion event_page_mr;
41
    struct hyperv_message_page *msg_page;
42
    struct hyperv_event_flags_page *event_page;
43
44
    QemuMutex sint_routes_mutex;
45
    QLIST_HEAD(, HvSintRoute) sint_routes;
46
};
47
48
0
#define TYPE_SYNIC "hyperv-synic"
49
OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC)
50
51
static bool synic_enabled;
52
53
bool hyperv_is_synic_enabled(void)
54
0
{
55
0
    return synic_enabled;
56
0
}
57
58
static SynICState *get_synic(CPUState *cs)
59
0
{
60
0
    return SYNIC(object_resolve_path_component(OBJECT(cs), "synic"));
61
0
}
62
63
bool hyperv_is_synic_present(CPUState *cs)
64
0
{
65
0
    return get_synic(cs);
66
0
}
67
68
static void synic_update(SynICState *synic, bool sctl_enable,
69
                         hwaddr msg_page_addr, hwaddr event_page_addr)
70
0
{
71
72
0
    synic->sctl_enabled = sctl_enable;
73
0
    if (synic->msg_page_addr != msg_page_addr) {
74
0
        if (synic->msg_page_addr) {
75
0
            memory_region_del_subregion(get_system_memory(),
76
0
                                        &synic->msg_page_mr);
77
0
        }
78
0
        if (msg_page_addr) {
79
0
            memory_region_add_subregion(get_system_memory(), msg_page_addr,
80
0
                                        &synic->msg_page_mr);
81
0
        }
82
0
        synic->msg_page_addr = msg_page_addr;
83
0
    }
84
0
    if (synic->event_page_addr != event_page_addr) {
85
0
        if (synic->event_page_addr) {
86
0
            memory_region_del_subregion(get_system_memory(),
87
0
                                        &synic->event_page_mr);
88
0
        }
89
0
        if (event_page_addr) {
90
0
            memory_region_add_subregion(get_system_memory(), event_page_addr,
91
0
                                        &synic->event_page_mr);
92
0
        }
93
0
        synic->event_page_addr = event_page_addr;
94
0
    }
95
0
}
96
97
void hyperv_synic_update(CPUState *cs, bool sctl_enable,
98
                         hwaddr msg_page_addr, hwaddr event_page_addr)
99
0
{
100
0
    SynICState *synic = get_synic(cs);
101
102
0
    if (!synic) {
103
0
        return;
104
0
    }
105
106
0
    synic_update(synic, sctl_enable, msg_page_addr, event_page_addr);
107
0
}
108
109
static void synic_realize(DeviceState *dev, Error **errp)
110
0
{
111
0
    Object *obj = OBJECT(dev);
112
0
    SynICState *synic = SYNIC(dev);
113
0
    char *msgp_name, *eventp_name;
114
0
    uint32_t vp_index;
115
116
    /* memory region names have to be globally unique */
117
0
    vp_index = hyperv_vp_index(synic->cs);
118
0
    msgp_name = g_strdup_printf("synic-%u-msg-page", vp_index);
119
0
    eventp_name = g_strdup_printf("synic-%u-event-page", vp_index);
120
121
0
    memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name,
122
0
                           sizeof(*synic->msg_page), &error_abort);
123
0
    memory_region_init_ram(&synic->event_page_mr, obj, eventp_name,
124
0
                           sizeof(*synic->event_page), &error_abort);
125
0
    synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
126
0
    synic->event_page = memory_region_get_ram_ptr(&synic->event_page_mr);
127
0
    qemu_mutex_init(&synic->sint_routes_mutex);
128
0
    QLIST_INIT(&synic->sint_routes);
129
130
0
    g_free(msgp_name);
131
0
    g_free(eventp_name);
132
0
}
133
134
static void synic_reset(DeviceState *dev)
135
0
{
136
0
    SynICState *synic = SYNIC(dev);
137
0
    memset(synic->msg_page, 0, sizeof(*synic->msg_page));
138
0
    memset(synic->event_page, 0, sizeof(*synic->event_page));
139
0
    synic_update(synic, false, 0, 0);
140
0
    assert(QLIST_EMPTY(&synic->sint_routes));
141
0
}
142
143
static void synic_class_init(ObjectClass *klass, const void *data)
144
63
{
145
63
    DeviceClass *dc = DEVICE_CLASS(klass);
146
147
63
    dc->realize = synic_realize;
148
63
    device_class_set_legacy_reset(dc, synic_reset);
149
63
    dc->user_creatable = false;
150
63
}
151
152
void hyperv_synic_add(CPUState *cs)
153
0
{
154
0
    Object *obj;
155
0
    SynICState *synic;
156
157
0
    obj = object_new(TYPE_SYNIC);
158
0
    synic = SYNIC(obj);
159
0
    synic->cs = cs;
160
0
    object_property_add_child(OBJECT(cs), "synic", obj);
161
0
    object_unref(obj);
162
0
    qdev_realize(DEVICE(obj), NULL, &error_abort);
163
0
    synic_enabled = true;
164
0
}
165
166
void hyperv_synic_reset(CPUState *cs)
167
0
{
168
0
    SynICState *synic = get_synic(cs);
169
170
0
    if (synic) {
171
0
        device_cold_reset(DEVICE(synic));
172
0
    }
173
0
}
174
175
static const TypeInfo synic_type_info = {
176
    .name = TYPE_SYNIC,
177
    .parent = TYPE_DEVICE,
178
    .instance_size = sizeof(SynICState),
179
    .class_init = synic_class_init,
180
};
181
182
static void synic_register_types(void)
183
63
{
184
63
    type_register_static(&synic_type_info);
185
63
}
186
187
type_init(synic_register_types)
188
189
/*
190
 * KVM has its own message producers (SynIC timers).  To guarantee
191
 * serialization with both KVM vcpu and the guest cpu, the messages are first
192
 * staged in an intermediate area and then posted to the SynIC message page in
193
 * the vcpu thread.
194
 */
195
typedef struct HvSintStagedMessage {
196
    /* message content staged by hyperv_post_msg */
197
    struct hyperv_message msg;
198
    /* callback + data (r/o) to complete the processing in a BH */
199
    HvSintMsgCb cb;
200
    void *cb_data;
201
    /* message posting status filled by cpu_post_msg */
202
    int status;
203
    /* passing the buck: */
204
    enum {
205
        /* initial state */
206
        HV_STAGED_MSG_FREE,
207
        /*
208
         * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE ->
209
         * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu
210
         */
211
        HV_STAGED_MSG_BUSY,
212
        /*
213
         * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot,
214
         * notify the guest, records the status, marks the posting done (BUSY
215
         * -> POSTED), and schedules sint_msg_bh BH
216
         */
217
        HV_STAGED_MSG_POSTED,
218
        /*
219
         * sint_msg_bh (BH) verifies that the posting is done, runs the
220
         * callback, and starts over (POSTED -> FREE)
221
         */
222
    } state;
223
} HvSintStagedMessage;
224
225
struct HvSintRoute {
226
    uint32_t sint;
227
    SynICState *synic;
228
    int gsi;
229
    EventNotifier sint_set_notifier;
230
    EventNotifier sint_ack_notifier;
231
232
    HvSintStagedMessage *staged_msg;
233
234
    unsigned refcount;
235
    QLIST_ENTRY(HvSintRoute) link;
236
};
237
238
static CPUState *hyperv_find_vcpu(uint32_t vp_index)
239
0
{
240
0
    return qemu_get_cpu(vp_index);
241
0
}
242
243
/*
244
 * BH to complete the processing of a staged message.
245
 */
246
static void sint_msg_bh(void *opaque)
247
0
{
248
0
    HvSintRoute *sint_route = opaque;
249
0
    HvSintStagedMessage *staged_msg = sint_route->staged_msg;
250
251
0
    if (qatomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
252
        /* status nor ready yet (spurious ack from guest?), ignore */
253
0
        return;
254
0
    }
255
256
0
    staged_msg->cb(staged_msg->cb_data, staged_msg->status);
257
0
    staged_msg->status = 0;
258
259
    /* staged message processing finished, ready to start over */
260
0
    qatomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
261
    /* drop the reference taken in hyperv_post_msg */
262
0
    hyperv_sint_route_unref(sint_route);
263
0
}
264
265
/*
266
 * Worker to transfer the message from the staging area into the SynIC message
267
 * page in vcpu context.
268
 */
269
static void cpu_post_msg(CPUState *cs, run_on_cpu_data data)
270
0
{
271
0
    HvSintRoute *sint_route = data.host_ptr;
272
0
    HvSintStagedMessage *staged_msg = sint_route->staged_msg;
273
0
    SynICState *synic = sint_route->synic;
274
0
    struct hyperv_message *dst_msg;
275
0
    bool wait_for_sint_ack = false;
276
277
0
    assert(staged_msg->state == HV_STAGED_MSG_BUSY);
278
279
0
    if (!synic->msg_page_addr) {
280
0
        staged_msg->status = -ENXIO;
281
0
        goto posted;
282
0
    }
283
284
0
    dst_msg = &synic->msg_page->slot[sint_route->sint];
285
286
0
    if (dst_msg->header.message_type != HV_MESSAGE_NONE) {
287
0
        dst_msg->header.message_flags |= HV_MESSAGE_FLAG_PENDING;
288
0
        staged_msg->status = -EAGAIN;
289
0
        wait_for_sint_ack = true;
290
0
    } else {
291
0
        memcpy(dst_msg, &staged_msg->msg, sizeof(*dst_msg));
292
0
        staged_msg->status = hyperv_sint_route_set_sint(sint_route);
293
0
    }
294
295
0
    memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page));
296
297
0
posted:
298
0
    qatomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
299
    /*
300
     * Notify the msg originator of the progress made; if the slot was busy we
301
     * set msg_pending flag in it so it will be the guest who will do EOM and
302
     * trigger the notification from KVM via sint_ack_notifier
303
     */
304
0
    if (!wait_for_sint_ack) {
305
0
        aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh,
306
0
                                sint_route);
307
0
    }
308
0
}
309
310
/*
311
 * Post a Hyper-V message to the staging area, for delivery to guest in the
312
 * vcpu thread.
313
 */
314
int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg)
315
0
{
316
0
    HvSintStagedMessage *staged_msg = sint_route->staged_msg;
317
318
0
    assert(staged_msg);
319
320
    /* grab the staging area */
321
0
    if (qatomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
322
0
                       HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) {
323
0
        return -EAGAIN;
324
0
    }
325
326
0
    memcpy(&staged_msg->msg, src_msg, sizeof(*src_msg));
327
328
    /* hold a reference on sint_route until the callback is finished */
329
0
    hyperv_sint_route_ref(sint_route);
330
331
    /* schedule message posting attempt in vcpu thread */
332
0
    async_run_on_cpu(sint_route->synic->cs, cpu_post_msg,
333
0
                     RUN_ON_CPU_HOST_PTR(sint_route));
334
0
    return 0;
335
0
}
336
337
static void sint_ack_handler(EventNotifier *notifier)
338
0
{
339
0
    HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
340
0
                                           sint_ack_notifier);
341
0
    event_notifier_test_and_clear(notifier);
342
343
    /*
344
     * the guest consumed the previous message so complete the current one with
345
     * -EAGAIN and let the msg originator retry
346
     */
347
0
    aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, sint_route);
348
0
}
349
350
/*
351
 * Set given event flag for a given sint on a given vcpu, and signal the sint.
352
 */
353
int hyperv_set_event_flag(HvSintRoute *sint_route, unsigned eventno)
354
0
{
355
0
    int ret;
356
0
    SynICState *synic = sint_route->synic;
357
0
    unsigned long *flags, set_mask;
358
0
    unsigned set_idx;
359
360
0
    if (eventno > HV_EVENT_FLAGS_COUNT) {
361
0
        return -EINVAL;
362
0
    }
363
0
    if (!synic->sctl_enabled || !synic->event_page_addr) {
364
0
        return -ENXIO;
365
0
    }
366
367
0
    set_idx = BIT_WORD(eventno);
368
0
    set_mask = BIT_MASK(eventno);
369
0
    flags = synic->event_page->slot[sint_route->sint].flags;
370
371
0
    if ((qatomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
372
0
        memory_region_set_dirty(&synic->event_page_mr, 0,
373
0
                                sizeof(*synic->event_page));
374
0
        ret = hyperv_sint_route_set_sint(sint_route);
375
0
    } else {
376
0
        ret = 0;
377
0
    }
378
0
    return ret;
379
0
}
380
381
static int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint)
382
0
{
383
0
    struct kvm_irq_routing_entry kroute = {};
384
0
    int virq;
385
386
0
    if (!kvm_gsi_routing_enabled()) {
387
0
        return -ENOSYS;
388
0
    }
389
0
    virq = kvm_irqchip_get_virq(s);
390
0
    if (virq < 0) {
391
0
        return virq;
392
0
    }
393
394
0
    kroute.gsi = virq;
395
0
    kroute.type = KVM_IRQ_ROUTING_HV_SINT;
396
0
    kroute.flags = 0;
397
0
    kroute.u.hv_sint.vcpu = vcpu;
398
0
    kroute.u.hv_sint.sint = sint;
399
400
0
    kvm_add_routing_entry(s, &kroute);
401
0
    kvm_irqchip_commit_routes(s);
402
403
0
    return virq;
404
0
}
405
406
HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
407
                                   HvSintMsgCb cb, void *cb_data)
408
0
{
409
0
    HvSintRoute *sint_route = NULL;
410
0
    EventNotifier *ack_notifier = NULL;
411
0
    int r, gsi;
412
0
    CPUState *cs;
413
0
    SynICState *synic;
414
0
    bool ack_event_initialized = false;
415
416
0
    cs = hyperv_find_vcpu(vp_index);
417
0
    if (!cs) {
418
0
        return NULL;
419
0
    }
420
421
0
    synic = get_synic(cs);
422
0
    if (!synic) {
423
0
        return NULL;
424
0
    }
425
426
0
    sint_route = g_new0(HvSintRoute, 1);
427
0
    if (!sint_route) {
428
0
        return NULL;
429
0
    }
430
431
0
    sint_route->synic = synic;
432
0
    sint_route->sint = sint;
433
0
    sint_route->refcount = 1;
434
435
0
    ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL;
436
0
    if (ack_notifier) {
437
0
        sint_route->staged_msg = g_new0(HvSintStagedMessage, 1);
438
0
        if (!sint_route->staged_msg) {
439
0
            goto cleanup_err_sint;
440
0
        }
441
0
        sint_route->staged_msg->cb = cb;
442
0
        sint_route->staged_msg->cb_data = cb_data;
443
444
0
        r = event_notifier_init(ack_notifier, false);
445
0
        if (r < 0) {
446
0
            goto cleanup_err_sint;
447
0
        }
448
0
        event_notifier_set_handler(ack_notifier, sint_ack_handler);
449
0
        ack_event_initialized = true;
450
0
    }
451
452
    /* See if we are done or we need to setup a GSI for this SintRoute */
453
0
    if (!synic->sctl_enabled) {
454
0
        goto cleanup;
455
0
    }
456
457
    /* We need to setup a GSI for this SintRoute */
458
0
    r = event_notifier_init(&sint_route->sint_set_notifier, false);
459
0
    if (r < 0) {
460
0
        goto cleanup_err_sint;
461
0
    }
462
463
0
    gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
464
0
    if (gsi < 0) {
465
0
        goto cleanup_err_sint_notifier;
466
0
    }
467
468
0
    r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
469
0
                                           &sint_route->sint_set_notifier,
470
0
                                           ack_notifier, gsi);
471
0
    if (r) {
472
0
        goto cleanup_err_irqfd;
473
0
    }
474
0
    sint_route->gsi = gsi;
475
0
cleanup:
476
0
    qemu_mutex_lock(&synic->sint_routes_mutex);
477
0
    QLIST_INSERT_HEAD(&synic->sint_routes, sint_route, link);
478
0
    qemu_mutex_unlock(&synic->sint_routes_mutex);
479
0
    return sint_route;
480
481
0
cleanup_err_irqfd:
482
0
    kvm_irqchip_release_virq(kvm_state, gsi);
483
484
0
cleanup_err_sint_notifier:
485
0
    event_notifier_cleanup(&sint_route->sint_set_notifier);
486
487
0
cleanup_err_sint:
488
0
    if (ack_notifier) {
489
0
        if (ack_event_initialized) {
490
0
            event_notifier_set_handler(ack_notifier, NULL);
491
0
            event_notifier_cleanup(ack_notifier);
492
0
        }
493
494
0
        g_free(sint_route->staged_msg);
495
0
    }
496
497
0
    g_free(sint_route);
498
0
    return NULL;
499
0
}
500
501
void hyperv_sint_route_ref(HvSintRoute *sint_route)
502
0
{
503
0
    sint_route->refcount++;
504
0
}
505
506
void hyperv_sint_route_unref(HvSintRoute *sint_route)
507
0
{
508
0
    SynICState *synic;
509
510
0
    if (!sint_route) {
511
0
        return;
512
0
    }
513
514
0
    assert(sint_route->refcount > 0);
515
516
0
    if (--sint_route->refcount) {
517
0
        return;
518
0
    }
519
520
0
    synic = sint_route->synic;
521
0
    qemu_mutex_lock(&synic->sint_routes_mutex);
522
0
    QLIST_REMOVE(sint_route, link);
523
0
    qemu_mutex_unlock(&synic->sint_routes_mutex);
524
525
0
    if (sint_route->gsi) {
526
0
        kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
527
0
                                              &sint_route->sint_set_notifier,
528
0
                                              sint_route->gsi);
529
0
        kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
530
0
        event_notifier_cleanup(&sint_route->sint_set_notifier);
531
0
    }
532
533
0
    if (sint_route->staged_msg) {
534
0
        event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
535
0
        event_notifier_cleanup(&sint_route->sint_ack_notifier);
536
0
        g_free(sint_route->staged_msg);
537
0
    }
538
0
    g_free(sint_route);
539
0
}
540
541
int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
542
0
{
543
0
    if (!sint_route->gsi) {
544
0
        return 0;
545
0
    }
546
547
0
    return event_notifier_set(&sint_route->sint_set_notifier);
548
0
}
549
550
typedef struct MsgHandler {
551
    struct rcu_head rcu;
552
    QLIST_ENTRY(MsgHandler) link;
553
    uint32_t conn_id;
554
    HvMsgHandler handler;
555
    void *data;
556
} MsgHandler;
557
558
typedef struct EventFlagHandler {
559
    struct rcu_head rcu;
560
    QLIST_ENTRY(EventFlagHandler) link;
561
    uint32_t conn_id;
562
    EventNotifier *notifier;
563
} EventFlagHandler;
564
565
static QLIST_HEAD(, MsgHandler) msg_handlers;
566
static QLIST_HEAD(, EventFlagHandler) event_flag_handlers;
567
static QemuMutex handlers_mutex;
568
569
static void __attribute__((constructor)) hv_init(void)
570
63
{
571
63
    QLIST_INIT(&msg_handlers);
572
63
    QLIST_INIT(&event_flag_handlers);
573
63
    qemu_mutex_init(&handlers_mutex);
574
63
}
575
576
int hyperv_set_msg_handler(uint32_t conn_id, HvMsgHandler handler, void *data)
577
0
{
578
0
    int ret;
579
0
    MsgHandler *mh;
580
581
0
    QEMU_LOCK_GUARD(&handlers_mutex);
582
0
    QLIST_FOREACH(mh, &msg_handlers, link) {
583
0
        if (mh->conn_id == conn_id) {
584
0
            if (handler) {
585
0
                ret = -EEXIST;
586
0
            } else {
587
0
                QLIST_REMOVE_RCU(mh, link);
588
0
                g_free_rcu(mh, rcu);
589
0
                ret = 0;
590
0
            }
591
0
            return ret;
592
0
        }
593
0
    }
594
595
0
    if (handler) {
596
0
        mh = g_new(MsgHandler, 1);
597
0
        mh->conn_id = conn_id;
598
0
        mh->handler = handler;
599
0
        mh->data = data;
600
0
        QLIST_INSERT_HEAD_RCU(&msg_handlers, mh, link);
601
0
        ret = 0;
602
0
    } else {
603
0
        ret = -ENOENT;
604
0
    }
605
606
0
    return ret;
607
0
}
608
609
uint16_t hyperv_hcall_post_message(uint64_t param, bool fast)
610
0
{
611
0
    uint16_t ret;
612
0
    hwaddr len;
613
0
    struct hyperv_post_message_input *msg;
614
0
    MsgHandler *mh;
615
616
0
    if (fast) {
617
0
        return HV_STATUS_INVALID_HYPERCALL_CODE;
618
0
    }
619
0
    if (param & (__alignof__(*msg) - 1)) {
620
0
        return HV_STATUS_INVALID_ALIGNMENT;
621
0
    }
622
623
0
    len = sizeof(*msg);
624
0
    msg = physical_memory_map(param, &len, 0);
625
0
    if (len < sizeof(*msg)) {
626
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
627
0
        goto unmap;
628
0
    }
629
0
    if (msg->payload_size > sizeof(msg->payload)) {
630
0
        ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
631
0
        goto unmap;
632
0
    }
633
634
0
    ret = HV_STATUS_INVALID_CONNECTION_ID;
635
0
    WITH_RCU_READ_LOCK_GUARD() {
636
0
        QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
637
0
            if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
638
0
                ret = mh->handler(msg, mh->data);
639
0
                break;
640
0
            }
641
0
        }
642
0
    }
643
644
0
unmap:
645
0
    physical_memory_unmap(msg, len, 0, 0);
646
0
    return ret;
647
0
}
648
649
static int set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
650
0
{
651
0
    int ret;
652
0
    EventFlagHandler *handler;
653
654
0
    QEMU_LOCK_GUARD(&handlers_mutex);
655
0
    QLIST_FOREACH(handler, &event_flag_handlers, link) {
656
0
        if (handler->conn_id == conn_id) {
657
0
            if (notifier) {
658
0
                ret = -EEXIST;
659
0
            } else {
660
0
                QLIST_REMOVE_RCU(handler, link);
661
0
                g_free_rcu(handler, rcu);
662
0
                ret = 0;
663
0
            }
664
0
            return ret;
665
0
        }
666
0
    }
667
668
0
    if (notifier) {
669
0
        handler = g_new(EventFlagHandler, 1);
670
0
        handler->conn_id = conn_id;
671
0
        handler->notifier = notifier;
672
0
        QLIST_INSERT_HEAD_RCU(&event_flag_handlers, handler, link);
673
0
        ret = 0;
674
0
    } else {
675
0
        ret = -ENOENT;
676
0
    }
677
678
0
    return ret;
679
0
}
680
681
static bool process_event_flags_userspace;
682
683
int hyperv_set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
684
0
{
685
0
    if (!process_event_flags_userspace &&
686
0
        !kvm_check_extension(kvm_state, KVM_CAP_HYPERV_EVENTFD)) {
687
0
        process_event_flags_userspace = true;
688
689
0
        warn_report("Hyper-V event signaling is not supported by this kernel; "
690
0
                    "using slower userspace hypercall processing");
691
0
    }
692
693
0
    if (!process_event_flags_userspace) {
694
0
        struct kvm_hyperv_eventfd hvevfd = {
695
0
            .conn_id = conn_id,
696
0
            .fd = notifier ? event_notifier_get_fd(notifier) : -1,
697
0
            .flags = notifier ? 0 : KVM_HYPERV_EVENTFD_DEASSIGN,
698
0
        };
699
700
0
        return kvm_vm_ioctl(kvm_state, KVM_HYPERV_EVENTFD, &hvevfd);
701
0
    }
702
0
    return set_event_flag_handler(conn_id, notifier);
703
0
}
704
705
uint16_t hyperv_hcall_signal_event(uint64_t param, bool fast)
706
0
{
707
0
    EventFlagHandler *handler;
708
709
0
    if (unlikely(!fast)) {
710
0
        MemTxResult result;
711
0
        hwaddr addr = param;
712
713
0
        if (addr & (__alignof__(addr) - 1)) {
714
0
            return HV_STATUS_INVALID_ALIGNMENT;
715
0
        }
716
717
0
        param = address_space_ldq_le(&address_space_memory, addr,
718
0
                                     MEMTXATTRS_UNSPECIFIED, &result);
719
0
        assert(result == MEMTX_OK);
720
0
    }
721
722
    /*
723
     * Per spec, bits 32-47 contain the extra "flag number".  However, we
724
     * have no use for it, and in all known usecases it is zero, so just
725
     * report lookup failure if it isn't.
726
     */
727
0
    if (param & 0xffff00000000ULL) {
728
0
        return HV_STATUS_INVALID_PORT_ID;
729
0
    }
730
    /* remaining bits are reserved-zero */
731
0
    if (param & ~HV_CONNECTION_ID_MASK) {
732
0
        return HV_STATUS_INVALID_HYPERCALL_INPUT;
733
0
    }
734
735
0
    RCU_READ_LOCK_GUARD();
736
0
    QLIST_FOREACH_RCU(handler, &event_flag_handlers, link) {
737
0
        if (handler->conn_id == param) {
738
0
            event_notifier_set(handler->notifier);
739
0
            return 0;
740
0
        }
741
0
    }
742
0
    return HV_STATUS_INVALID_CONNECTION_ID;
743
0
}
744
745
static HvSynDbgHandler hv_syndbg_handler;
746
static void *hv_syndbg_context;
747
748
void hyperv_set_syndbg_handler(HvSynDbgHandler handler, void *context)
749
0
{
750
0
    assert(!hv_syndbg_handler);
751
0
    hv_syndbg_handler = handler;
752
0
    hv_syndbg_context = context;
753
0
}
754
755
uint16_t hyperv_hcall_reset_dbg_session(uint64_t outgpa)
756
0
{
757
0
    uint16_t ret;
758
0
    HvSynDbgMsg msg;
759
0
    struct hyperv_reset_debug_session_output *reset_dbg_session = NULL;
760
0
    hwaddr len;
761
762
0
    if (!hv_syndbg_handler) {
763
0
        ret = HV_STATUS_INVALID_HYPERCALL_CODE;
764
0
        goto cleanup;
765
0
    }
766
767
0
    len = sizeof(*reset_dbg_session);
768
0
    reset_dbg_session = physical_memory_map(outgpa, &len, 1);
769
0
    if (!reset_dbg_session || len < sizeof(*reset_dbg_session)) {
770
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
771
0
        goto cleanup;
772
0
    }
773
774
0
    msg.type = HV_SYNDBG_MSG_CONNECTION_INFO;
775
0
    ret = hv_syndbg_handler(hv_syndbg_context, &msg);
776
0
    if (ret) {
777
0
        goto cleanup;
778
0
    }
779
780
0
    reset_dbg_session->host_ip = msg.u.connection_info.host_ip;
781
0
    reset_dbg_session->host_port = msg.u.connection_info.host_port;
782
    /* The following fields are only used as validation for KDVM */
783
0
    memset(&reset_dbg_session->host_mac, 0,
784
0
           sizeof(reset_dbg_session->host_mac));
785
0
    reset_dbg_session->target_ip = msg.u.connection_info.host_ip;
786
0
    reset_dbg_session->target_port = msg.u.connection_info.host_port;
787
0
    memset(&reset_dbg_session->target_mac, 0,
788
0
           sizeof(reset_dbg_session->target_mac));
789
0
cleanup:
790
0
    if (reset_dbg_session) {
791
0
        physical_memory_unmap(reset_dbg_session,
792
0
                                  sizeof(*reset_dbg_session), 1, len);
793
0
    }
794
795
0
    return ret;
796
0
}
797
798
uint16_t hyperv_hcall_retreive_dbg_data(uint64_t ingpa, uint64_t outgpa,
799
                                        bool fast)
800
0
{
801
0
    uint16_t ret;
802
0
    struct hyperv_retrieve_debug_data_input *debug_data_in = NULL;
803
0
    struct hyperv_retrieve_debug_data_output *debug_data_out = NULL;
804
0
    hwaddr in_len, out_len;
805
0
    HvSynDbgMsg msg;
806
807
0
    if (fast || !hv_syndbg_handler) {
808
0
        ret = HV_STATUS_INVALID_HYPERCALL_CODE;
809
0
        goto cleanup;
810
0
    }
811
812
0
    in_len = sizeof(*debug_data_in);
813
0
    debug_data_in = physical_memory_map(ingpa, &in_len, 0);
814
0
    if (!debug_data_in || in_len < sizeof(*debug_data_in)) {
815
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
816
0
        goto cleanup;
817
0
    }
818
819
0
    out_len = sizeof(*debug_data_out);
820
0
    debug_data_out = physical_memory_map(outgpa, &out_len, 1);
821
0
    if (!debug_data_out || out_len < sizeof(*debug_data_out)) {
822
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
823
0
        goto cleanup;
824
0
    }
825
826
0
    msg.type = HV_SYNDBG_MSG_RECV;
827
0
    msg.u.recv.buf_gpa = outgpa + sizeof(*debug_data_out);
828
0
    msg.u.recv.count = TARGET_PAGE_SIZE - sizeof(*debug_data_out);
829
0
    msg.u.recv.options = debug_data_in->options;
830
0
    msg.u.recv.timeout = debug_data_in->timeout;
831
0
    msg.u.recv.is_raw = true;
832
0
    ret = hv_syndbg_handler(hv_syndbg_context, &msg);
833
0
    if (ret == HV_STATUS_NO_DATA) {
834
0
        debug_data_out->retrieved_count = 0;
835
0
        debug_data_out->remaining_count = debug_data_in->count;
836
0
        goto cleanup;
837
0
    } else if (ret != HV_STATUS_SUCCESS) {
838
0
        goto cleanup;
839
0
    }
840
841
0
    debug_data_out->retrieved_count = msg.u.recv.retrieved_count;
842
0
    debug_data_out->remaining_count =
843
0
        debug_data_in->count - msg.u.recv.retrieved_count;
844
0
cleanup:
845
0
    if (debug_data_out) {
846
0
        physical_memory_unmap(debug_data_out, sizeof(*debug_data_out), 1,
847
0
                                  out_len);
848
0
    }
849
850
0
    if (debug_data_in) {
851
0
        physical_memory_unmap(debug_data_in, sizeof(*debug_data_in), 0,
852
0
                                  in_len);
853
0
    }
854
855
0
    return ret;
856
0
}
857
858
uint16_t hyperv_hcall_post_dbg_data(uint64_t ingpa, uint64_t outgpa, bool fast)
859
0
{
860
0
    uint16_t ret;
861
0
    struct hyperv_post_debug_data_input *post_data_in = NULL;
862
0
    struct hyperv_post_debug_data_output *post_data_out = NULL;
863
0
    hwaddr in_len, out_len;
864
0
    HvSynDbgMsg msg;
865
866
0
    if (fast || !hv_syndbg_handler) {
867
0
        ret = HV_STATUS_INVALID_HYPERCALL_CODE;
868
0
        goto cleanup;
869
0
    }
870
871
0
    in_len = sizeof(*post_data_in);
872
0
    post_data_in = physical_memory_map(ingpa, &in_len, 0);
873
0
    if (!post_data_in || in_len < sizeof(*post_data_in)) {
874
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
875
0
        goto cleanup;
876
0
    }
877
878
0
    if (post_data_in->count > TARGET_PAGE_SIZE - sizeof(*post_data_in)) {
879
0
        ret = HV_STATUS_INVALID_PARAMETER;
880
0
        goto cleanup;
881
0
    }
882
883
0
    out_len = sizeof(*post_data_out);
884
0
    post_data_out = physical_memory_map(outgpa, &out_len, 1);
885
0
    if (!post_data_out || out_len < sizeof(*post_data_out)) {
886
0
        ret = HV_STATUS_INSUFFICIENT_MEMORY;
887
0
        goto cleanup;
888
0
    }
889
890
0
    msg.type = HV_SYNDBG_MSG_SEND;
891
0
    msg.u.send.buf_gpa = ingpa + sizeof(*post_data_in);
892
0
    msg.u.send.count = post_data_in->count;
893
0
    msg.u.send.is_raw = true;
894
0
    ret = hv_syndbg_handler(hv_syndbg_context, &msg);
895
0
    if (ret != HV_STATUS_SUCCESS) {
896
0
        goto cleanup;
897
0
    }
898
899
0
    post_data_out->pending_count = msg.u.send.pending_count;
900
0
    ret = post_data_out->pending_count ? HV_STATUS_INSUFFICIENT_BUFFERS :
901
0
                                         HV_STATUS_SUCCESS;
902
0
cleanup:
903
0
    if (post_data_out) {
904
0
        physical_memory_unmap(post_data_out,
905
0
                                  sizeof(*post_data_out), 1, out_len);
906
0
    }
907
908
0
    if (post_data_in) {
909
0
        physical_memory_unmap(post_data_in,
910
0
                                  sizeof(*post_data_in), 0, in_len);
911
0
    }
912
913
0
    return ret;
914
0
}
915
916
uint32_t hyperv_syndbg_send(uint64_t ingpa, uint32_t count)
917
0
{
918
0
    HvSynDbgMsg msg;
919
920
0
    if (!hv_syndbg_handler) {
921
0
        return HV_SYNDBG_STATUS_INVALID;
922
0
    }
923
924
0
    msg.type = HV_SYNDBG_MSG_SEND;
925
0
    msg.u.send.buf_gpa = ingpa;
926
0
    msg.u.send.count = count;
927
0
    msg.u.send.is_raw = false;
928
0
    if (hv_syndbg_handler(hv_syndbg_context, &msg)) {
929
0
        return HV_SYNDBG_STATUS_INVALID;
930
0
    }
931
932
0
    return HV_SYNDBG_STATUS_SEND_SUCCESS;
933
0
}
934
935
uint32_t hyperv_syndbg_recv(uint64_t ingpa, uint32_t count)
936
0
{
937
0
    uint16_t ret;
938
0
    HvSynDbgMsg msg;
939
940
0
    if (!hv_syndbg_handler) {
941
0
        return HV_SYNDBG_STATUS_INVALID;
942
0
    }
943
944
0
    msg.type = HV_SYNDBG_MSG_RECV;
945
0
    msg.u.recv.buf_gpa = ingpa;
946
0
    msg.u.recv.count = count;
947
0
    msg.u.recv.options = 0;
948
0
    msg.u.recv.timeout = 0;
949
0
    msg.u.recv.is_raw = false;
950
0
    ret = hv_syndbg_handler(hv_syndbg_context, &msg);
951
0
    if (ret != HV_STATUS_SUCCESS) {
952
0
        return 0;
953
0
    }
954
955
0
    return HV_SYNDBG_STATUS_SET_SIZE(HV_SYNDBG_STATUS_RECV_SUCCESS,
956
0
                                     msg.u.recv.retrieved_count);
957
0
}
958
959
void hyperv_syndbg_set_pending_page(uint64_t ingpa)
960
0
{
961
0
    HvSynDbgMsg msg;
962
963
0
    if (!hv_syndbg_handler) {
964
0
        return;
965
0
    }
966
967
0
    msg.type = HV_SYNDBG_MSG_SET_PENDING_PAGE;
968
0
    msg.u.pending_page.buf_gpa = ingpa;
969
0
    hv_syndbg_handler(hv_syndbg_context, &msg);
970
0
}
971
972
uint64_t hyperv_syndbg_query_options(void)
973
0
{
974
0
    HvSynDbgMsg msg;
975
976
0
    if (!hv_syndbg_handler) {
977
0
        return 0;
978
0
    }
979
980
0
    msg.type = HV_SYNDBG_MSG_QUERY_OPTIONS;
981
0
    if (hv_syndbg_handler(hv_syndbg_context, &msg) != HV_STATUS_SUCCESS) {
982
0
        return 0;
983
0
    }
984
985
0
    return msg.u.query_options.options;
986
0
}
987
988
static bool vmbus_recommended_features_enabled;
989
990
bool hyperv_are_vmbus_recommended_features_enabled(void)
991
0
{
992
0
    return vmbus_recommended_features_enabled;
993
0
}
994
995
void hyperv_set_vmbus_recommended_features_enabled(void)
996
0
{
997
    vmbus_recommended_features_enabled = true;
998
0
}