aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/vmware/vmci/vmci_event.c
blob: 9a932340a7b693439e360fc90ebed313c96258f8 (plain) (blame)
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*-
 * Copyright (c) 2018 VMware, Inc.
 *
 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
 */

/* This file implements VMCI Event code. */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "vmci.h"
#include "vmci_driver.h"
#include "vmci_event.h"
#include "vmci_kernel_api.h"
#include "vmci_kernel_defs.h"
#include "vmci_kernel_if.h"

#define LGPFX		"vmci_event: "
#define EVENT_MAGIC	0xEABE0000

struct vmci_subscription {
	vmci_id		id;
	int		ref_count;
	bool		run_delayed;
	vmci_event	destroy_event;
	vmci_event_type	event;
	vmci_event_cb	callback;
	void		*callback_data;
	vmci_list_item(vmci_subscription) subscriber_list_item;
};

static struct	vmci_subscription *vmci_event_find(vmci_id sub_id);
static int	vmci_event_deliver(struct vmci_event_msg *event_msg);
static int	vmci_event_register_subscription(struct vmci_subscription *sub,
		    vmci_event_type event, uint32_t flags,
		    vmci_event_cb callback, void *callback_data);
static struct	vmci_subscription *vmci_event_unregister_subscription(
		    vmci_id sub_id);

static vmci_list(vmci_subscription) subscriber_array[VMCI_EVENT_MAX];
static vmci_lock subscriber_lock;

struct vmci_delayed_event_info {
	struct vmci_subscription *sub;
	uint8_t event_payload[sizeof(struct vmci_event_data_max)];
};

struct vmci_event_ref {
	struct vmci_subscription	*sub;
	vmci_list_item(vmci_event_ref)	list_item;
};

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_init --
 *
 *     General init code.
 *
 * Results:
 *     VMCI_SUCCESS on success, appropriate error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_event_init(void)
{
	int i;

	for (i = 0; i < VMCI_EVENT_MAX; i++)
		vmci_list_init(&subscriber_array[i]);

	return (vmci_init_lock(&subscriber_lock, "VMCI Event subscriber lock"));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_exit --
 *
 *     General exit code.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_event_exit(void)
{
	struct vmci_subscription *iter, *iter_2;
	vmci_event_type e;

	/* We free all memory at exit. */
	for (e = 0; e < VMCI_EVENT_MAX; e++) {
		vmci_list_scan_safe(iter, &subscriber_array[e],
		    subscriber_list_item, iter_2) {

			/*
			 * We should never get here because all events should
			 * have been unregistered before we try to unload the
			 * driver module. Also, delayed callbacks could still
			 * be firing so this cleanup would not be safe. Still
			 * it is better to free the memory than not ... so we
			 * leave this code in just in case....
			 */
			ASSERT(false);

			vmci_free_kernel_mem(iter, sizeof(*iter));
		}
	}
	vmci_cleanup_lock(&subscriber_lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_sync --
 *
 *     Use this as a synchronization point when setting globals, for example,
 *     during device shutdown.
 *
 * Results:
 *     true.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_event_sync(void)
{

	vmci_grab_lock_bh(&subscriber_lock);
	vmci_release_lock_bh(&subscriber_lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_check_host_capabilities --
 *
 *     Verify that the host supports the hypercalls we need. If it does not,
 *     try to find fallback hypercalls and use those instead.
 *
 * Results:
 *     true if required hypercalls (or fallback hypercalls) are
 *     supported by the host, false otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

bool
vmci_event_check_host_capabilities(void)
{

	/* vmci_event does not require any hypercalls. */
	return (true);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_get --
 *
 *     Gets a reference to the given struct vmci_subscription.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static void
vmci_event_get(struct vmci_subscription *entry)
{

	ASSERT(entry);

	entry->ref_count++;
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_release --
 *
 *     Releases the given struct vmci_subscription.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     Fires the destroy event if the reference count has gone to zero.
 *
 *------------------------------------------------------------------------------
 */

static void
vmci_event_release(struct vmci_subscription *entry)
{

	ASSERT(entry);
	ASSERT(entry->ref_count > 0);

	entry->ref_count--;
	if (entry->ref_count == 0)
		vmci_signal_event(&entry->destroy_event);
}

 /*
 *------------------------------------------------------------------------------
 *
 * event_release_cb --
 *
 *     Callback to release the event entry reference. It is called by the
 *     vmci_wait_on_event function before it blocks.
 *
 * Result:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static int
event_release_cb(void *client_data)
{
	struct vmci_subscription *sub = (struct vmci_subscription *)client_data;

	ASSERT(sub);

	vmci_grab_lock_bh(&subscriber_lock);
	vmci_event_release(sub);
	vmci_release_lock_bh(&subscriber_lock);

	return (0);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_find --
 *
 *     Find entry. Assumes lock is held.
 *
 * Results:
 *     Entry if found, NULL if not.
 *
 * Side effects:
 *     Increments the struct vmci_subscription refcount if an entry is found.
 *
 *------------------------------------------------------------------------------
 */

static struct vmci_subscription *
vmci_event_find(vmci_id sub_id)
{
	struct vmci_subscription *iter;
	vmci_event_type e;

	for (e = 0; e < VMCI_EVENT_MAX; e++) {
		vmci_list_scan(iter, &subscriber_array[e],
		    subscriber_list_item) {
			if (iter->id == sub_id) {
				vmci_event_get(iter);
				return (iter);
			}
		}
	}
	return (NULL);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_delayed_dispatch_cb --
 *
 *     Calls the specified callback in a delayed context.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static void
vmci_event_delayed_dispatch_cb(void *data)
{
	struct vmci_delayed_event_info *event_info;
	struct vmci_subscription *sub;
	struct vmci_event_data *ed;

	event_info = (struct vmci_delayed_event_info *)data;

	ASSERT(event_info);
	ASSERT(event_info->sub);

	sub = event_info->sub;
	ed = (struct vmci_event_data *)event_info->event_payload;

	sub->callback(sub->id, ed, sub->callback_data);

	vmci_grab_lock_bh(&subscriber_lock);
	vmci_event_release(sub);
	vmci_release_lock_bh(&subscriber_lock);

	vmci_free_kernel_mem(event_info, sizeof(*event_info));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_deliver --
 *
 *     Actually delivers the events to the subscribers.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     The callback function for each subscriber is invoked.
 *
 *------------------------------------------------------------------------------
 */

static int
vmci_event_deliver(struct vmci_event_msg *event_msg)
{
	struct vmci_subscription *iter;
	int err = VMCI_SUCCESS;

	vmci_list(vmci_event_ref) no_delay_list;
	vmci_list_init(&no_delay_list);

	ASSERT(event_msg);

	vmci_grab_lock_bh(&subscriber_lock);
	vmci_list_scan(iter, &subscriber_array[event_msg->event_data.event],
	    subscriber_list_item) {
		if (iter->run_delayed) {
			struct vmci_delayed_event_info *event_info;
			if ((event_info =
			    vmci_alloc_kernel_mem(sizeof(*event_info),
			    VMCI_MEMORY_ATOMIC)) == NULL) {
				err = VMCI_ERROR_NO_MEM;
				goto out;
			}

			vmci_event_get(iter);

			memset(event_info, 0, sizeof(*event_info));
			memcpy(event_info->event_payload,
			    VMCI_DG_PAYLOAD(event_msg),
			    (size_t)event_msg->hdr.payload_size);
			event_info->sub = iter;
			err =
			    vmci_schedule_delayed_work(
			    vmci_event_delayed_dispatch_cb, event_info);
			if (err != VMCI_SUCCESS) {
				vmci_event_release(iter);
				vmci_free_kernel_mem(
				    event_info, sizeof(*event_info));
				goto out;
			}

		} else {
			struct vmci_event_ref *event_ref;

			/*
			 * We construct a local list of subscribers and release
			 * subscriber_lock before invoking the callbacks. This
			 * is similar to delayed callbacks, but callbacks are
			 * invoked right away here.
			 */
			if ((event_ref = vmci_alloc_kernel_mem(
			    sizeof(*event_ref), VMCI_MEMORY_ATOMIC)) == NULL) {
				err = VMCI_ERROR_NO_MEM;
				goto out;
			}

			vmci_event_get(iter);
			event_ref->sub = iter;
			vmci_list_insert(&no_delay_list, event_ref, list_item);
		}
	}

out:
	vmci_release_lock_bh(&subscriber_lock);

	if (!vmci_list_empty(&no_delay_list)) {
		struct vmci_event_data *ed;
		struct vmci_event_ref *iter;
		struct vmci_event_ref *iter_2;

		vmci_list_scan_safe(iter, &no_delay_list, list_item, iter_2) {
			struct vmci_subscription *cur;
			uint8_t event_payload[sizeof(
			    struct vmci_event_data_max)];

			cur = iter->sub;

			/*
			 * We set event data before each callback to ensure
			 * isolation.
			 */
			memset(event_payload, 0, sizeof(event_payload));
			memcpy(event_payload, VMCI_DG_PAYLOAD(event_msg),
			    (size_t)event_msg->hdr.payload_size);
			ed = (struct vmci_event_data *)event_payload;
			cur->callback(cur->id, ed, cur->callback_data);

			vmci_grab_lock_bh(&subscriber_lock);
			vmci_event_release(cur);
			vmci_release_lock_bh(&subscriber_lock);
			vmci_free_kernel_mem(iter, sizeof(*iter));
		}
	}

	return (err);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_dispatch --
 *
 *     Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all
 *     subscribers for given event.
 *
 * Results:
 *     VMCI_SUCCESS on success, error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_event_dispatch(struct vmci_datagram *msg)
{
	struct vmci_event_msg *event_msg = (struct vmci_event_msg *)msg;

	ASSERT(msg &&
	    msg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
	    msg->dst.resource == VMCI_EVENT_HANDLER);

	if (msg->payload_size < sizeof(vmci_event_type) ||
	    msg->payload_size > sizeof(struct vmci_event_data_max))
		return (VMCI_ERROR_INVALID_ARGS);

	if (!VMCI_EVENT_VALID(event_msg->event_data.event))
		return (VMCI_ERROR_EVENT_UNKNOWN);

	vmci_event_deliver(event_msg);

	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_register_subscription --
 *
 *     Initialize and add subscription to subscriber list.
 *
 * Results:
 *     VMCI_SUCCESS on success, error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static int
vmci_event_register_subscription(struct vmci_subscription *sub,
    vmci_event_type event, uint32_t flags, vmci_event_cb callback,
    void *callback_data)
{
#define VMCI_EVENT_MAX_ATTEMPTS	10
	static vmci_id subscription_id = 0;
	int result;
	uint32_t attempts = 0;
	bool success;

	ASSERT(sub);

	if (!VMCI_EVENT_VALID(event) || callback == NULL) {
		VMCI_LOG_DEBUG(LGPFX"Failed to subscribe to event"
		    " (type=%d) (callback=%p) (data=%p).\n",
		    event, callback, callback_data);
		return (VMCI_ERROR_INVALID_ARGS);
	}

	if (!vmci_can_schedule_delayed_work()) {
		/*
		 * If the platform doesn't support delayed work callbacks then
		 * don't allow registration for them.
		 */
		if (flags & VMCI_FLAG_EVENT_DELAYED_CB)
			return (VMCI_ERROR_INVALID_ARGS);
		sub->run_delayed = false;
	} else {
		/*
		 * The platform supports delayed work callbacks. Honor the
		 * requested flags
		 */
		sub->run_delayed = (flags & VMCI_FLAG_EVENT_DELAYED_CB) ?
		    true : false;
	}

	sub->ref_count = 1;
	sub->event = event;
	sub->callback = callback;
	sub->callback_data = callback_data;

	vmci_grab_lock_bh(&subscriber_lock);

	for (success = false, attempts = 0;
	    success == false && attempts < VMCI_EVENT_MAX_ATTEMPTS;
	    attempts++) {
		struct vmci_subscription *existing_sub = NULL;

		/*
		 * We try to get an id a couple of time before claiming we are
		 * out of resources.
		 */
		sub->id = ++subscription_id;

		/* Test for duplicate id. */
		existing_sub = vmci_event_find(sub->id);
		if (existing_sub == NULL) {
			/* We succeeded if we didn't find a duplicate. */
			success = true;
		} else
			vmci_event_release(existing_sub);
	}

	if (success) {
		vmci_create_event(&sub->destroy_event);
		vmci_list_insert(&subscriber_array[event], sub,
		    subscriber_list_item);
		result = VMCI_SUCCESS;
	} else
		result = VMCI_ERROR_NO_RESOURCES;

	vmci_release_lock_bh(&subscriber_lock);
	return (result);
#undef VMCI_EVENT_MAX_ATTEMPTS
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_unregister_subscription --
 *
 *     Remove subscription from subscriber list.
 *
 * Results:
 *     struct vmci_subscription when found, NULL otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static struct vmci_subscription *
vmci_event_unregister_subscription(vmci_id sub_id)
{
	struct vmci_subscription *s;

	vmci_grab_lock_bh(&subscriber_lock);
	s = vmci_event_find(sub_id);
	if (s != NULL) {
		vmci_event_release(s);
		vmci_list_remove(s, subscriber_list_item);
	}
	vmci_release_lock_bh(&subscriber_lock);

	if (s != NULL) {
		vmci_wait_on_event(&s->destroy_event, event_release_cb, s);
		vmci_destroy_event(&s->destroy_event);
	}

	return (s);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_subscribe --
 *
 *     Subscribe to given event. The callback specified can be fired in
 *     different contexts depending on what flag is specified while registering.
 *     If flags contains VMCI_FLAG_EVENT_NONE then the callback is fired with
 *     the subscriber lock held (and BH context on the guest). If flags contain
 *     VMCI_FLAG_EVENT_DELAYED_CB then the callback is fired with no locks held
 *     in thread context. This is useful because other vmci_event functions can
 *     be called, but it also increases the chances that an event will be
 *     dropped.
 *
 * Results:
 *     VMCI_SUCCESS on success, error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_event_subscribe(vmci_event_type event, vmci_event_cb callback,
    void *callback_data, vmci_id *subscription_id)
{
	int retval;
	uint32_t flags = VMCI_FLAG_EVENT_NONE;
	struct vmci_subscription *s = NULL;

	if (subscription_id == NULL) {
		VMCI_LOG_DEBUG(LGPFX"Invalid subscription (NULL).\n");
		return (VMCI_ERROR_INVALID_ARGS);
	}

	s = vmci_alloc_kernel_mem(sizeof(*s), VMCI_MEMORY_NORMAL);
	if (s == NULL)
		return (VMCI_ERROR_NO_MEM);

	retval = vmci_event_register_subscription(s, event, flags,
	    callback, callback_data);
	if (retval < VMCI_SUCCESS) {
		vmci_free_kernel_mem(s, sizeof(*s));
		return (retval);
	}

	*subscription_id = s->id;
	return (retval);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_event_unsubscribe --
 *
 *     Unsubscribe to given event. Removes it from list and frees it.
 *     Will return callback_data if requested by caller.
 *
 * Results:
 *     VMCI_SUCCESS on success, error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_event_unsubscribe(vmci_id sub_id)
{
	struct vmci_subscription *s;

	/*
	 * Return subscription. At this point we know noone else is accessing
	 * the subscription so we can free it.
	 */
	s = vmci_event_unregister_subscription(sub_id);
	if (s == NULL)
		return (VMCI_ERROR_NOT_FOUND);
	vmci_free_kernel_mem(s, sizeof(*s));

	return (VMCI_SUCCESS);
}