/usr/src/open-vm-tools-10.0.7/vmci/common/vmciEvent.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | /*********************************************************
* Copyright (C) 2007-2015 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
/*
* vmciEvent.c --
*
* VMCI Event code for host and guests.
*/
#include "vmci_kernel_if.h"
#include "vmci_defs.h"
#include "vmci_infrastructure.h"
#include "vmciEvent.h"
#include "vmciKernelAPI.h"
#if defined(VMKERNEL)
# include "vmciVmkInt.h"
# include "vm_libc.h"
# include "helper_ext.h"
# include "vmciDriver.h"
#else
# include "vmciDriver.h"
#endif
#define LGPFX "VMCIEvent: "
#define EVENT_MAGIC 0xEABE0000
typedef struct VMCISubscription {
VMCIId id;
int refCount;
Bool runDelayed;
VMCIEvent destroyEvent;
VMCI_Event event;
VMCI_EventCB callback;
void *callbackData;
VMCIListItem subscriberListItem;
} VMCISubscription;
static VMCISubscription *VMCIEventFind(VMCIId subID);
static int VMCIEventDeliver(VMCIEventMsg *eventMsg);
static int VMCIEventRegisterSubscription(VMCISubscription *sub,
VMCI_Event event,
uint32 flags,
VMCI_EventCB callback,
void *callbackData);
static VMCISubscription *VMCIEventUnregisterSubscription(VMCIId subID);
static VMCIList subscriberArray[VMCI_EVENT_MAX];
static VMCILock subscriberLock;
typedef struct VMCIDelayedEventInfo {
VMCISubscription *sub;
uint8 eventPayload[sizeof(VMCIEventData_Max)];
} VMCIDelayedEventInfo;
typedef struct VMCIEventRef {
VMCISubscription *sub;
VMCIListItem listItem;
} VMCIEventRef;
/*
*----------------------------------------------------------------------
*
* VMCIEvent_Init --
*
* General init code.
*
* Results:
* VMCI_SUCCESS on success, appropriate error code otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
VMCIEvent_Init(void)
{
int i;
for (i = 0; i < VMCI_EVENT_MAX; i++) {
VMCIList_Init(&subscriberArray[i]);
}
return VMCI_InitLock(&subscriberLock, "VMCIEventSubscriberLock",
VMCI_LOCK_RANK_EVENT);
}
/*
*----------------------------------------------------------------------
*
* VMCIEvent_Exit --
*
* General exit code.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
VMCIEvent_Exit(void)
{
VMCIListItem *iter, *iter2;
VMCI_Event e;
/* We free all memory at exit. */
for (e = 0; e < VMCI_EVENT_MAX; e++) {
VMCIList_ScanSafe(iter, iter2, &subscriberArray[e]) {
VMCISubscription *cur;
/*
* 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);
cur = VMCIList_Entry(iter, VMCISubscription, subscriberListItem);
VMCI_FreeKernelMem(cur, sizeof *cur);
}
}
VMCI_CleanupLock(&subscriberLock);
}
/*
*-----------------------------------------------------------------------------
*
* VMCIEvent_Sync --
*
* Use this as a synchronization point when setting globals, for example,
* during device shutdown.
*
* Results:
* TRUE.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
void
VMCIEvent_Sync(void)
{
VMCILockFlags lockFlags;
VMCI_GrabLock_BH(&subscriberLock, &lockFlags);
VMCI_ReleaseLock_BH(&subscriberLock, lockFlags);
}
/*
*-----------------------------------------------------------------------------
*
* VMCIEvent_CheckHostCapabilities --
*
* 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
VMCIEvent_CheckHostCapabilities(void)
{
/* VMCIEvent does not require any hypercalls. */
return TRUE;
}
/*
*-----------------------------------------------------------------------------
*
* VMCIEventGet --
*
* Gets a reference to the given VMCISubscription.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
VMCIEventGet(VMCISubscription *entry) // IN
{
ASSERT(entry);
entry->refCount++;
}
/*
*-----------------------------------------------------------------------------
*
* VMCIEventRelease --
*
* Releases the given VMCISubscription.
*
* Results:
* None.
*
* Side effects:
* Fires the destroy event if the reference count has gone to zero.
*
*-----------------------------------------------------------------------------
*/
static void
VMCIEventRelease(VMCISubscription *entry) // IN
{
ASSERT(entry);
ASSERT(entry->refCount > 0);
entry->refCount--;
if (entry->refCount == 0) {
VMCI_SignalEvent(&entry->destroyEvent);
}
}
/*
*------------------------------------------------------------------------------
*
* EventReleaseCB --
*
* Callback to release the event entry reference. It is called by the
* VMCI_WaitOnEvent function before it blocks.
*
* Result:
* None.
*
* Side effects:
* None.
*
*------------------------------------------------------------------------------
*/
static int
EventReleaseCB(void *clientData) // IN
{
VMCILockFlags flags;
VMCISubscription *sub = (VMCISubscription *)clientData;
ASSERT(sub);
VMCI_GrabLock_BH(&subscriberLock, &flags);
VMCIEventRelease(sub);
VMCI_ReleaseLock_BH(&subscriberLock, flags);
return 0;
}
/*
*-----------------------------------------------------------------------------
*
* VMCIEventFind --
*
* Find entry. Assumes lock is held.
*
* Results:
* Entry if found, NULL if not.
*
* Side effects:
* Increments the VMCISubscription refcount if an entry is found.
*
*-----------------------------------------------------------------------------
*/
static VMCISubscription *
VMCIEventFind(VMCIId subID) // IN
{
VMCIListItem *iter;
VMCI_Event e;
for (e = 0; e < VMCI_EVENT_MAX; e++) {
VMCIList_Scan(iter, &subscriberArray[e]) {
VMCISubscription *cur =
VMCIList_Entry(iter, VMCISubscription, subscriberListItem);
if (cur->id == subID) {
VMCIEventGet(cur);
return cur;
}
}
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* VMCIEventDelayedDispatchCB --
*
* Calls the specified callback in a delayed context.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
VMCIEventDelayedDispatchCB(void *data) // IN
{
VMCIDelayedEventInfo *eventInfo;
VMCISubscription *sub;
VMCI_EventData *ed;
VMCILockFlags flags;
eventInfo = (VMCIDelayedEventInfo *)data;
ASSERT(eventInfo);
ASSERT(eventInfo->sub);
sub = eventInfo->sub;
ed = (VMCI_EventData *)eventInfo->eventPayload;
sub->callback(sub->id, ed, sub->callbackData);
VMCI_GrabLock_BH(&subscriberLock, &flags);
VMCIEventRelease(sub);
VMCI_ReleaseLock_BH(&subscriberLock, flags);
VMCI_FreeKernelMem(eventInfo, sizeof *eventInfo);
}
/*
*----------------------------------------------------------------------------
*
* VMCIEventDeliver --
*
* Actually delivers the events to the subscribers.
*
* Results:
* None.
*
* Side effects:
* The callback function for each subscriber is invoked.
*
*----------------------------------------------------------------------------
*/
static int
VMCIEventDeliver(VMCIEventMsg *eventMsg) // IN
{
int err = VMCI_SUCCESS;
VMCIListItem *iter;
VMCILockFlags flags;
VMCIList noDelayList;
VMCIList_Init(&noDelayList);
ASSERT(eventMsg);
VMCI_GrabLock_BH(&subscriberLock, &flags);
VMCIList_Scan(iter, &subscriberArray[eventMsg->eventData.event]) {
VMCISubscription *cur = VMCIList_Entry(iter, VMCISubscription,
subscriberListItem);
ASSERT(cur && cur->event == eventMsg->eventData.event);
if (cur->runDelayed) {
VMCIDelayedEventInfo *eventInfo;
if ((eventInfo = VMCI_AllocKernelMem(sizeof *eventInfo,
(VMCI_MEMORY_ATOMIC |
VMCI_MEMORY_NONPAGED))) == NULL) {
err = VMCI_ERROR_NO_MEM;
goto out;
}
VMCIEventGet(cur);
memset(eventInfo, 0, sizeof *eventInfo);
memcpy(eventInfo->eventPayload, VMCI_DG_PAYLOAD(eventMsg),
(size_t)eventMsg->hdr.payloadSize);
eventInfo->sub = cur;
err = VMCI_ScheduleDelayedWork(VMCIEventDelayedDispatchCB,
eventInfo);
if (err != VMCI_SUCCESS) {
VMCIEventRelease(cur);
VMCI_FreeKernelMem(eventInfo, sizeof *eventInfo);
goto out;
}
} else {
VMCIEventRef *eventRef;
/*
* To avoid possible lock rank voilation when holding
* subscriberLock, we construct a local list of
* subscribers and release subscriberLock before
* invokes the callbacks. This is similar to delayed
* callbacks, but callbacks is invoked right away here.
*/
if ((eventRef = VMCI_AllocKernelMem(sizeof *eventRef,
(VMCI_MEMORY_ATOMIC |
VMCI_MEMORY_NONPAGED))) == NULL) {
err = VMCI_ERROR_NO_MEM;
goto out;
}
VMCIEventGet(cur);
eventRef->sub = cur;
VMCIList_InitEntry(&eventRef->listItem);
VMCIList_Insert(&eventRef->listItem, &noDelayList);
}
}
out:
VMCI_ReleaseLock_BH(&subscriberLock, flags);
if (!VMCIList_Empty(&noDelayList)) {
VMCI_EventData *ed;
VMCIListItem *iter2;
VMCIList_ScanSafe(iter, iter2, &noDelayList) {
VMCIEventRef *eventRef = VMCIList_Entry(iter, VMCIEventRef,
listItem);
VMCISubscription *cur = eventRef->sub;
uint8 eventPayload[sizeof(VMCIEventData_Max)];
/* We set event data before each callback to ensure isolation. */
memset(eventPayload, 0, sizeof eventPayload);
memcpy(eventPayload, VMCI_DG_PAYLOAD(eventMsg),
(size_t)eventMsg->hdr.payloadSize);
ed = (VMCI_EventData *)eventPayload;
cur->callback(cur->id, ed, cur->callbackData);
VMCI_GrabLock_BH(&subscriberLock, &flags);
VMCIEventRelease(cur);
VMCI_ReleaseLock_BH(&subscriberLock, flags);
VMCI_FreeKernelMem(eventRef, sizeof *eventRef);
}
}
return err;
}
/*
*----------------------------------------------------------------------
*
* VMCIEvent_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
VMCIEvent_Dispatch(VMCIDatagram *msg) // IN
{
VMCIEventMsg *eventMsg = (VMCIEventMsg *)msg;
ASSERT(msg &&
msg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
msg->dst.resource == VMCI_EVENT_HANDLER);
if (msg->payloadSize < sizeof(VMCI_Event) ||
msg->payloadSize > sizeof(VMCIEventData_Max)) {
return VMCI_ERROR_INVALID_ARGS;
}
if (!VMCI_EVENT_VALID(eventMsg->eventData.event)) {
return VMCI_ERROR_EVENT_UNKNOWN;
}
VMCIEventDeliver(eventMsg);
return VMCI_SUCCESS;
}
/*
*----------------------------------------------------------------------
*
* VMCIEventRegisterSubscription --
*
* Initialize and add subscription to subscriber list.
*
* Results:
* VMCI_SUCCESS on success, error code otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VMCIEventRegisterSubscription(VMCISubscription *sub, // IN
VMCI_Event event, // IN
uint32 flags, // IN
VMCI_EventCB callback, // IN
void *callbackData) // IN
{
# define VMCI_EVENT_MAX_ATTEMPTS 10
static VMCIId subscriptionID = 0;
VMCILockFlags lockFlags;
uint32 attempts = 0;
int result;
Bool success;
ASSERT(sub);
if (!VMCI_EVENT_VALID(event) || callback == NULL) {
VMCI_DEBUG_LOG(4, (LGPFX"Failed to subscribe to event (type=%d) "
"(callback=%p) (data=%p).\n",
event, callback, callbackData));
return VMCI_ERROR_INVALID_ARGS;
}
if (vmkernel) {
/*
* In the vmkernel we defer delivery of events to a helper world. This
* makes the event delivery more consistent across hosts and guests with
* regard to which locks are held. Memory access and guest paused events
* are an exception to this, since clients need to know immediately that
* the device memory is disabled (if we delay such events, then clients
* may be notified too late).
*/
if (VMCI_EVENT_MEM_ACCESS_ON == event ||
VMCI_EVENT_MEM_ACCESS_OFF == event ||
VMCI_EVENT_GUEST_PAUSED == event ||
VMCI_EVENT_GUEST_UNPAUSED == event) {
/*
* Client must expect to get such events synchronously, and should
* perform its locking accordingly. If it can't handle this, then
* fail.
*/
if (flags & VMCI_FLAG_EVENT_DELAYED_CB) {
return VMCI_ERROR_INVALID_ARGS;
}
sub->runDelayed = FALSE;
} else {
sub->runDelayed = TRUE;
}
} else if (!VMCI_CanScheduleDelayedWork()) {
/*
* 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->runDelayed = FALSE;
} else {
/*
* The platform supports delayed work callbacks. Honor the requested
* flags
*/
sub->runDelayed = (flags & VMCI_FLAG_EVENT_DELAYED_CB) ? TRUE : FALSE;
}
sub->refCount = 1;
sub->event = event;
sub->callback = callback;
sub->callbackData = callbackData;
VMCIList_InitEntry(&sub->subscriberListItem);
VMCI_GrabLock_BH(&subscriberLock, &lockFlags);
/* Check if creation of a new event is allowed. */
if (!VMCI_CanCreate()) {
result = VMCI_ERROR_UNAVAILABLE;
goto exit;
}
for (success = FALSE, attempts = 0;
success == FALSE && attempts < VMCI_EVENT_MAX_ATTEMPTS;
attempts++) {
VMCISubscription *existingSub = NULL;
/*
* We try to get an id a couple of time before claiming we are out of
* resources.
*/
sub->id = ++subscriptionID;
/* Test for duplicate id. */
existingSub = VMCIEventFind(sub->id);
if (existingSub == NULL) {
/* We succeeded if we didn't find a duplicate. */
success = TRUE;
} else {
VMCIEventRelease(existingSub);
}
}
if (success) {
VMCI_CreateEvent(&sub->destroyEvent);
VMCIList_Insert(&sub->subscriberListItem, &subscriberArray[event]);
result = VMCI_SUCCESS;
} else {
result = VMCI_ERROR_NO_RESOURCES;
}
exit:
VMCI_ReleaseLock_BH(&subscriberLock, lockFlags);
return result;
# undef VMCI_EVENT_MAX_ATTEMPTS
}
/*
*----------------------------------------------------------------------
*
* VMCIEventUnregisterSubscription --
*
* Remove subscription from subscriber list.
*
* Results:
* VMCISubscription when found, NULL otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static VMCISubscription *
VMCIEventUnregisterSubscription(VMCIId subID) // IN
{
VMCILockFlags flags;
VMCISubscription *s;
VMCI_GrabLock_BH(&subscriberLock, &flags);
s = VMCIEventFind(subID);
if (s != NULL) {
VMCIEventRelease(s);
VMCIList_Remove(&s->subscriberListItem);
}
VMCI_ReleaseLock_BH(&subscriberLock, flags);
if (s != NULL) {
VMCI_WaitOnEvent(&s->destroyEvent, EventReleaseCB, s);
VMCI_DestroyEvent(&s->destroyEvent);
}
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 VMCIEvent 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.
*
*----------------------------------------------------------------------
*/
VMCI_EXPORT_SYMBOL(vmci_event_subscribe)
int
vmci_event_subscribe(VMCI_Event event, // IN
#if !defined(linux) || defined(VMKERNEL)
uint32 flags, // IN
#endif // !linux || VMKERNEL
VMCI_EventCB callback, // IN
void *callbackData, // IN
VMCIId *subscriptionID) // OUT
{
int retval;
#if defined(linux) && !defined(VMKERNEL)
uint32 flags = VMCI_FLAG_EVENT_NONE;
#endif // linux && !VMKERNEL
VMCISubscription *s = NULL;
if (subscriptionID == NULL) {
VMCI_DEBUG_LOG(4, (LGPFX"Invalid subscription (NULL).\n"));
return VMCI_ERROR_INVALID_ARGS;
}
s = VMCI_AllocKernelMem(sizeof *s, VMCI_MEMORY_NONPAGED);
if (s == NULL) {
return VMCI_ERROR_NO_MEM;
}
retval = VMCIEventRegisterSubscription(s, event, flags,
callback, callbackData);
if (retval < VMCI_SUCCESS) {
VMCI_FreeKernelMem(s, sizeof *s);
return retval;
}
*subscriptionID = s->id;
return retval;
}
/*
*----------------------------------------------------------------------
*
* vmci_event_unsubscribe --
*
* Unsubscribe to given event. Removes it from list and frees it.
* Will return callbackData if requested by caller.
*
* Results:
* VMCI_SUCCESS on success, error code otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
VMCI_EXPORT_SYMBOL(vmci_event_unsubscribe)
int
vmci_event_unsubscribe(VMCIId subID) // IN
{
VMCISubscription *s;
/*
* Return subscription. At this point we know noone else is accessing
* the subscription so we can free it.
*/
s = VMCIEventUnregisterSubscription(subID);
if (s == NULL) {
return VMCI_ERROR_NOT_FOUND;
}
VMCI_FreeKernelMem(s, sizeof *s);
return VMCI_SUCCESS;
}
|