1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2023, Microsoft Corporation.
*
* mshv_root module's main interrupt handler and associated functionality.
*
* Authors: Microsoft Linux virtualization team
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <linux/random.h>
#include <asm/mshyperv.h>
#include "mshv_eventfd.h"
#include "mshv.h"
static u32 synic_event_ring_get_queued_port(u32 sint_index)
{
struct hv_synic_event_ring_page **event_ring_page;
volatile struct hv_synic_event_ring *ring;
struct hv_synic_pages *spages;
u8 **synic_eventring_tail;
u32 message;
u8 tail;
spages = this_cpu_ptr(mshv_root.synic_pages);
event_ring_page = &spages->synic_event_ring_page;
synic_eventring_tail = (u8 **)this_cpu_ptr(hv_synic_eventring_tail);
if (unlikely(!*synic_eventring_tail)) {
pr_debug("Missing synic event ring tail!\n");
return 0;
}
tail = (*synic_eventring_tail)[sint_index];
if (unlikely(!*event_ring_page)) {
pr_debug("Missing synic event ring page!\n");
return 0;
}
ring = &(*event_ring_page)->sint_event_ring[sint_index];
/*
* Get the message.
*/
message = ring->data[tail];
if (!message) {
if (ring->ring_full) {
/*
* Ring is marked full, but we would have consumed all
* the messages. Notify the hypervisor that ring is now
* empty and check again.
*/
ring->ring_full = 0;
hv_call_notify_port_ring_empty(sint_index);
message = ring->data[tail];
}
if (!message) {
ring->signal_masked = 0;
/*
* Unmask the signal and sync with hypervisor
* before one last check for any message.
*/
mb();
message = ring->data[tail];
/*
* Ok, lets bail out.
*/
if (!message)
return 0;
}
ring->signal_masked = 1;
}
/*
* Clear the message in the ring buffer.
*/
ring->data[tail] = 0;
if (++tail == HV_SYNIC_EVENT_RING_MESSAGE_COUNT)
tail = 0;
(*synic_eventring_tail)[sint_index] = tail;
return message;
}
static bool
mshv_doorbell_isr(struct hv_message *msg)
{
struct hv_notification_message_payload *notification;
u32 port;
if (msg->header.message_type != HVMSG_SYNIC_SINT_INTERCEPT)
return false;
notification = (struct hv_notification_message_payload *)msg->u.payload;
if (notification->sint_index != HV_SYNIC_DOORBELL_SINT_INDEX)
return false;
while ((port = synic_event_ring_get_queued_port(HV_SYNIC_DOORBELL_SINT_INDEX))) {
struct port_table_info ptinfo = { 0 };
if (mshv_portid_lookup(port, &ptinfo)) {
pr_debug("Failed to get port info from port_table!\n");
continue;
}
if (ptinfo.hv_port_type != HV_PORT_TYPE_DOORBELL) {
pr_debug("Not a doorbell port!, port: %d, port_type: %d\n",
port, ptinfo.hv_port_type);
continue;
}
/* Invoke the callback */
ptinfo.hv_port_doorbell.doorbell_cb(port,
ptinfo.hv_port_doorbell.data);
}
return true;
}
static bool mshv_async_call_completion_isr(struct hv_message *msg)
{
bool handled = false;
struct hv_async_completion_message_payload *async_msg;
struct mshv_partition *partition;
u64 partition_id;
if (msg->header.message_type != HVMSG_ASYNC_CALL_COMPLETION)
goto out;
async_msg =
(struct hv_async_completion_message_payload *)msg->u.payload;
partition_id = async_msg->partition_id;
/*
* Hold this lock for the rest of the isr, because the partition could
* be released anytime.
* e.g. the MSHV_RUN_VP thread could wake on another cpu; it could
* release the partition unless we hold this!
*/
rcu_read_lock();
partition = mshv_partition_find(partition_id);
if (unlikely(!partition)) {
pr_debug("failed to find partition %llu\n", partition_id);
goto unlock_out;
}
partition->async_hypercall_status = async_msg->status;
complete(&partition->async_hypercall);
handled = true;
unlock_out:
rcu_read_unlock();
out:
return handled;
}
static void kick_vp(struct mshv_vp *vp)
{
atomic64_inc(&vp->run.vp_signaled_count);
vp->run.kicked_by_hv = 1;
wake_up(&vp->run.vp_suspend_queue);
}
static void
handle_bitset_message(const struct hv_vp_signal_bitset_scheduler_message *msg)
{
int bank_idx, vps_signaled = 0, bank_mask_size;
struct mshv_partition *partition;
const struct hv_vpset *vpset;
const u64 *bank_contents;
u64 partition_id = msg->partition_id;
if (msg->vp_bitset.bitset.format != HV_GENERIC_SET_SPARSE_4K) {
pr_debug("scheduler message format is not HV_GENERIC_SET_SPARSE_4K");
return;
}
if (msg->vp_count == 0) {
pr_debug("scheduler message with no VP specified");
return;
}
rcu_read_lock();
partition = mshv_partition_find(partition_id);
if (unlikely(!partition)) {
pr_debug("failed to find partition %llu\n", partition_id);
goto unlock_out;
}
vpset = &msg->vp_bitset.bitset;
bank_idx = -1;
bank_contents = vpset->bank_contents;
bank_mask_size = sizeof(vpset->valid_bank_mask) * BITS_PER_BYTE;
while (true) {
int vp_bank_idx = -1;
int vp_bank_size = sizeof(*bank_contents) * BITS_PER_BYTE;
int vp_index;
bank_idx = find_next_bit((unsigned long *)&vpset->valid_bank_mask,
bank_mask_size, bank_idx + 1);
if (bank_idx == bank_mask_size)
break;
while (true) {
struct mshv_vp *vp;
vp_bank_idx = find_next_bit((unsigned long *)bank_contents,
vp_bank_size, vp_bank_idx + 1);
if (vp_bank_idx == vp_bank_size)
break;
vp_index = (bank_idx * vp_bank_size) + vp_bank_idx;
/* This shouldn't happen, but just in case. */
if (unlikely(vp_index >= MSHV_MAX_VPS)) {
pr_debug("VP index %u out of bounds\n",
vp_index);
goto unlock_out;
}
vp = partition->pt_vp_array[vp_index];
if (unlikely(!vp)) {
pr_debug("failed to find VP %u\n", vp_index);
goto unlock_out;
}
kick_vp(vp);
vps_signaled++;
}
bank_contents++;
}
unlock_out:
rcu_read_unlock();
if (vps_signaled != msg->vp_count)
pr_debug("asked to signal %u VPs but only did %u\n",
msg->vp_count, vps_signaled);
}
static void
handle_pair_message(const struct hv_vp_signal_pair_scheduler_message *msg)
{
struct mshv_partition *partition = NULL;
struct mshv_vp *vp;
int idx;
rcu_read_lock();
for (idx = 0; idx < msg->vp_count; idx++) {
u64 partition_id = msg->partition_ids[idx];
u32 vp_index = msg->vp_indexes[idx];
if (idx == 0 || partition->pt_id != partition_id) {
partition = mshv_partition_find(partition_id);
if (unlikely(!partition)) {
pr_debug("failed to find partition %llu\n",
partition_id);
break;
}
}
/* This shouldn't happen, but just in case. */
if (unlikely(vp_index >= MSHV_MAX_VPS)) {
pr_debug("VP index %u out of bounds\n", vp_index);
break;
}
vp = partition->pt_vp_array[vp_index];
if (!vp) {
pr_debug("failed to find VP %u\n", vp_index);
break;
}
kick_vp(vp);
}
rcu_read_unlock();
}
static bool
mshv_scheduler_isr(struct hv_message *msg)
{
if (msg->header.message_type != HVMSG_SCHEDULER_VP_SIGNAL_BITSET &&
msg->header.message_type != HVMSG_SCHEDULER_VP_SIGNAL_PAIR)
return false;
if (msg->header.message_type == HVMSG_SCHEDULER_VP_SIGNAL_BITSET)
handle_bitset_message((struct hv_vp_signal_bitset_scheduler_message *)
msg->u.payload);
else
handle_pair_message((struct hv_vp_signal_pair_scheduler_message *)
msg->u.payload);
return true;
}
static bool
mshv_intercept_isr(struct hv_message *msg)
{
struct mshv_partition *partition;
bool handled = false;
struct mshv_vp *vp;
u64 partition_id;
u32 vp_index;
partition_id = msg->header.sender;
rcu_read_lock();
partition = mshv_partition_find(partition_id);
if (unlikely(!partition)) {
pr_debug("failed to find partition %llu\n",
partition_id);
goto unlock_out;
}
if (msg->header.message_type == HVMSG_X64_APIC_EOI) {
/*
* Check if this gsi is registered in the
* ack_notifier list and invoke the callback
* if registered.
*/
/*
* If there is a notifier, the ack callback is supposed
* to handle the VMEXIT. So we need not pass this message
* to vcpu thread.
*/
struct hv_x64_apic_eoi_message *eoi_msg =
(struct hv_x64_apic_eoi_message *)&msg->u.payload[0];
if (mshv_notify_acked_gsi(partition, eoi_msg->interrupt_vector)) {
handled = true;
goto unlock_out;
}
}
/*
* We should get an opaque intercept message here for all intercept
* messages, since we're using the mapped VP intercept message page.
*
* The intercept message will have been placed in intercept message
* page at this point.
*
* Make sure the message type matches our expectation.
*/
if (msg->header.message_type != HVMSG_OPAQUE_INTERCEPT) {
pr_debug("wrong message type %d", msg->header.message_type);
goto unlock_out;
}
/*
* Since we directly index the vp, and it has to exist for us to be here
* (because the vp is only deleted when the partition is), no additional
* locking is needed here
*/
vp_index =
((struct hv_opaque_intercept_message *)msg->u.payload)->vp_index;
vp = partition->pt_vp_array[vp_index];
if (unlikely(!vp)) {
pr_debug("failed to find VP %u\n", vp_index);
goto unlock_out;
}
kick_vp(vp);
handled = true;
unlock_out:
rcu_read_unlock();
return handled;
}
void mshv_isr(void)
{
struct hv_synic_pages *spages = this_cpu_ptr(mshv_root.synic_pages);
struct hv_message_page **msg_page = &spages->synic_message_page;
struct hv_message *msg;
bool handled;
if (unlikely(!(*msg_page))) {
pr_debug("Missing synic page!\n");
return;
}
msg = &((*msg_page)->sint_message[HV_SYNIC_INTERCEPTION_SINT_INDEX]);
/*
* If the type isn't set, there isn't really a message;
* it may be some other hyperv interrupt
*/
if (msg->header.message_type == HVMSG_NONE)
return;
handled = mshv_doorbell_isr(msg);
if (!handled)
handled = mshv_scheduler_isr(msg);
if (!handled)
handled = mshv_async_call_completion_isr(msg);
if (!handled)
handled = mshv_intercept_isr(msg);
if (handled) {
/*
* Acknowledge message with hypervisor if another message is
* pending.
*/
msg->header.message_type = HVMSG_NONE;
/*
* Ensure the write is complete so the hypervisor will deliver
* the next message if available.
*/
mb();
if (msg->header.message_flags.msg_pending)
hv_set_non_nested_msr(HV_MSR_EOM, 0);
#ifdef HYPERVISOR_CALLBACK_VECTOR
add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR);
#endif
} else {
pr_warn_once("%s: unknown message type 0x%x\n", __func__,
msg->header.message_type);
}
}
int mshv_synic_init(unsigned int cpu)
{
union hv_synic_simp simp;
union hv_synic_siefp siefp;
union hv_synic_sirbp sirbp;
#ifdef HYPERVISOR_CALLBACK_VECTOR
union hv_synic_sint sint;
#endif
union hv_synic_scontrol sctrl;
struct hv_synic_pages *spages = this_cpu_ptr(mshv_root.synic_pages);
struct hv_message_page **msg_page = &spages->synic_message_page;
struct hv_synic_event_flags_page **event_flags_page =
&spages->synic_event_flags_page;
struct hv_synic_event_ring_page **event_ring_page =
&spages->synic_event_ring_page;
/* Setup the Synic's message page */
simp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIMP);
simp.simp_enabled = true;
*msg_page = memremap(simp.base_simp_gpa << HV_HYP_PAGE_SHIFT,
HV_HYP_PAGE_SIZE,
MEMREMAP_WB);
if (!(*msg_page))
return -EFAULT;
hv_set_non_nested_msr(HV_MSR_SIMP, simp.as_uint64);
/* Setup the Synic's event flags page */
siefp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIEFP);
siefp.siefp_enabled = true;
*event_flags_page = memremap(siefp.base_siefp_gpa << PAGE_SHIFT,
PAGE_SIZE, MEMREMAP_WB);
if (!(*event_flags_page))
goto cleanup;
hv_set_non_nested_msr(HV_MSR_SIEFP, siefp.as_uint64);
/* Setup the Synic's event ring page */
sirbp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIRBP);
sirbp.sirbp_enabled = true;
*event_ring_page = memremap(sirbp.base_sirbp_gpa << PAGE_SHIFT,
PAGE_SIZE, MEMREMAP_WB);
if (!(*event_ring_page))
goto cleanup;
hv_set_non_nested_msr(HV_MSR_SIRBP, sirbp.as_uint64);
#ifdef HYPERVISOR_CALLBACK_VECTOR
/* Enable intercepts */
sint.as_uint64 = 0;
sint.vector = HYPERVISOR_CALLBACK_VECTOR;
sint.masked = false;
sint.auto_eoi = hv_recommend_using_aeoi();
hv_set_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX,
sint.as_uint64);
/* Doorbell SINT */
sint.as_uint64 = 0;
sint.vector = HYPERVISOR_CALLBACK_VECTOR;
sint.masked = false;
sint.as_intercept = 1;
sint.auto_eoi = hv_recommend_using_aeoi();
hv_set_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_DOORBELL_SINT_INDEX,
sint.as_uint64);
#endif
/* Enable global synic bit */
sctrl.as_uint64 = hv_get_non_nested_msr(HV_MSR_SCONTROL);
sctrl.enable = 1;
hv_set_non_nested_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
return 0;
cleanup:
if (*event_ring_page) {
sirbp.sirbp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIRBP, sirbp.as_uint64);
memunmap(*event_ring_page);
}
if (*event_flags_page) {
siefp.siefp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIEFP, siefp.as_uint64);
memunmap(*event_flags_page);
}
if (*msg_page) {
simp.simp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIMP, simp.as_uint64);
memunmap(*msg_page);
}
return -EFAULT;
}
int mshv_synic_cleanup(unsigned int cpu)
{
union hv_synic_sint sint;
union hv_synic_simp simp;
union hv_synic_siefp siefp;
union hv_synic_sirbp sirbp;
union hv_synic_scontrol sctrl;
struct hv_synic_pages *spages = this_cpu_ptr(mshv_root.synic_pages);
struct hv_message_page **msg_page = &spages->synic_message_page;
struct hv_synic_event_flags_page **event_flags_page =
&spages->synic_event_flags_page;
struct hv_synic_event_ring_page **event_ring_page =
&spages->synic_event_ring_page;
/* Disable the interrupt */
sint.as_uint64 = hv_get_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX);
sint.masked = true;
hv_set_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX,
sint.as_uint64);
/* Disable Doorbell SINT */
sint.as_uint64 = hv_get_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_DOORBELL_SINT_INDEX);
sint.masked = true;
hv_set_non_nested_msr(HV_MSR_SINT0 + HV_SYNIC_DOORBELL_SINT_INDEX,
sint.as_uint64);
/* Disable Synic's event ring page */
sirbp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIRBP);
sirbp.sirbp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIRBP, sirbp.as_uint64);
memunmap(*event_ring_page);
/* Disable Synic's event flags page */
siefp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIEFP);
siefp.siefp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIEFP, siefp.as_uint64);
memunmap(*event_flags_page);
/* Disable Synic's message page */
simp.as_uint64 = hv_get_non_nested_msr(HV_MSR_SIMP);
simp.simp_enabled = false;
hv_set_non_nested_msr(HV_MSR_SIMP, simp.as_uint64);
memunmap(*msg_page);
/* Disable global synic bit */
sctrl.as_uint64 = hv_get_non_nested_msr(HV_MSR_SCONTROL);
sctrl.enable = 0;
hv_set_non_nested_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
return 0;
}
int
mshv_register_doorbell(u64 partition_id, doorbell_cb_t doorbell_cb, void *data,
u64 gpa, u64 val, u64 flags)
{
struct hv_connection_info connection_info = { 0 };
union hv_connection_id connection_id = { 0 };
struct port_table_info *port_table_info;
struct hv_port_info port_info = { 0 };
union hv_port_id port_id = { 0 };
int ret;
port_table_info = kmalloc(sizeof(*port_table_info), GFP_KERNEL);
if (!port_table_info)
return -ENOMEM;
port_table_info->hv_port_type = HV_PORT_TYPE_DOORBELL;
port_table_info->hv_port_doorbell.doorbell_cb = doorbell_cb;
port_table_info->hv_port_doorbell.data = data;
ret = mshv_portid_alloc(port_table_info);
if (ret < 0) {
kfree(port_table_info);
return ret;
}
port_id.u.id = ret;
port_info.port_type = HV_PORT_TYPE_DOORBELL;
port_info.doorbell_port_info.target_sint = HV_SYNIC_DOORBELL_SINT_INDEX;
port_info.doorbell_port_info.target_vp = HV_ANY_VP;
ret = hv_call_create_port(hv_current_partition_id, port_id, partition_id,
&port_info,
0, 0, NUMA_NO_NODE);
if (ret < 0) {
mshv_portid_free(port_id.u.id);
return ret;
}
connection_id.u.id = port_id.u.id;
connection_info.port_type = HV_PORT_TYPE_DOORBELL;
connection_info.doorbell_connection_info.gpa = gpa;
connection_info.doorbell_connection_info.trigger_value = val;
connection_info.doorbell_connection_info.flags = flags;
ret = hv_call_connect_port(hv_current_partition_id, port_id, partition_id,
connection_id, &connection_info, 0, NUMA_NO_NODE);
if (ret < 0) {
hv_call_delete_port(hv_current_partition_id, port_id);
mshv_portid_free(port_id.u.id);
return ret;
}
// lets use the port_id as the doorbell_id
return port_id.u.id;
}
void
mshv_unregister_doorbell(u64 partition_id, int doorbell_portid)
{
union hv_port_id port_id = { 0 };
union hv_connection_id connection_id = { 0 };
connection_id.u.id = doorbell_portid;
hv_call_disconnect_port(partition_id, connection_id);
port_id.u.id = doorbell_portid;
hv_call_delete_port(hv_current_partition_id, port_id);
mshv_portid_free(doorbell_portid);
}
|