~n-muench/ubuntu/precise/open-vm-tools/open-vm-tools.raring-precise.backport

« back to all changes in this revision

Viewing changes to modules/linux/vmci/common/vmciEvent.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation version 2 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
11
 * for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * vmciEvent.c --
 
21
 *
 
22
 *     VMCI Event code for host and guests.
 
23
 */
 
24
 
 
25
#include "vmci_kernel_if.h"
 
26
#include "vmci_defs.h"
 
27
#include "vmci_infrastructure.h"
 
28
#include "vmciEvent.h"
 
29
#include "vmciKernelAPI.h"
 
30
#if defined(VMKERNEL)
 
31
#  include "vmciVmkInt.h"
 
32
#  include "vm_libc.h"
 
33
#  include "helper_ext.h"
 
34
#  include "vmciDriver.h"
 
35
#else
 
36
#  include "vmciDriver.h"
 
37
#endif
 
38
 
 
39
#define LGPFX "VMCIEvent: "
 
40
 
 
41
#define EVENT_MAGIC 0xEABE0000
 
42
 
 
43
typedef struct VMCISubscription {
 
44
   VMCIId         id;
 
45
   int            refCount;
 
46
   Bool           runDelayed;
 
47
   VMCIEvent      destroyEvent;
 
48
   VMCI_Event     event;
 
49
   VMCI_EventCB   callback;
 
50
   void           *callbackData;
 
51
   VMCIListItem   subscriberListItem;
 
52
} VMCISubscription;
 
53
 
 
54
 
 
55
static VMCISubscription *VMCIEventFind(VMCIId subID);
 
56
static int VMCIEventDeliver(VMCIEventMsg *eventMsg);
 
57
static int VMCIEventRegisterSubscription(VMCISubscription *sub,
 
58
                                         VMCI_Event event,
 
59
                                         uint32 flags,
 
60
                                         VMCI_EventCB callback,
 
61
                                         void *callbackData);
 
62
static VMCISubscription *VMCIEventUnregisterSubscription(VMCIId subID);
 
63
 
 
64
 
 
65
#if defined(VMKERNEL)
 
66
   /* VMK doesn't need BH locks, so use lower ranks. */
 
67
#  define VMCIEventInitLock(_lock, _name) \
 
68
   VMCI_InitLock(_lock, _name, VMCI_LOCK_RANK_HIGHER)
 
69
#  define VMCIEventGrabLock(_lock, _flags)    VMCI_GrabLock(_lock, _flags)
 
70
#  define VMCIEventReleaseLock(_lock, _flags) VMCI_ReleaseLock(_lock, _flags)
 
71
#else // VMKERNEL
 
72
#  define VMCIEventInitLock(_lock, _name) \
 
73
   VMCI_InitLock(_lock, _name, VMCI_LOCK_RANK_HIGHER_BH)
 
74
#  define VMCIEventGrabLock(_lock, _flags)    VMCI_GrabLock_BH(_lock, _flags)
 
75
#  define VMCIEventReleaseLock(_lock, _flags) VMCI_ReleaseLock_BH(_lock, _flags)
 
76
#endif // VMKERNEL
 
77
 
 
78
 
 
79
static VMCIList subscriberArray[VMCI_EVENT_MAX];
 
80
static VMCILock subscriberLock;
 
81
 
 
82
typedef struct VMCIDelayedEventInfo {
 
83
   VMCISubscription *sub;
 
84
   uint8 eventPayload[sizeof(VMCIEventData_Max)];
 
85
} VMCIDelayedEventInfo;
 
86
 
 
87
 
 
88
/*
 
89
 *----------------------------------------------------------------------
 
90
 *
 
91
 * VMCIEvent_Init --
 
92
 *
 
93
 *      General init code.
 
94
 *
 
95
 * Results:
 
96
 *      None.
 
97
 *
 
98
 * Side effects:
 
99
 *      None.
 
100
 *
 
101
 *----------------------------------------------------------------------
 
102
 */
 
103
 
 
104
void
 
105
VMCIEvent_Init(void)
 
106
{
 
107
   int i;
 
108
 
 
109
   for (i = 0; i < VMCI_EVENT_MAX; i++) {
 
110
      VMCIList_Init(&subscriberArray[i]);
 
111
   }
 
112
   VMCIEventInitLock(&subscriberLock, "VMCIEventSubscriberLock");
 
113
}
 
114
 
 
115
 
 
116
/*
 
117
 *----------------------------------------------------------------------
 
118
 *
 
119
 * VMCIEvent_Exit --
 
120
 *
 
121
 *      General exit code.
 
122
 *
 
123
 * Results:
 
124
 *      None.
 
125
 *
 
126
 * Side effects:
 
127
 *      None.
 
128
 *
 
129
 *----------------------------------------------------------------------
 
130
 */
 
131
 
 
132
void
 
133
VMCIEvent_Exit(void)
 
134
{
 
135
   VMCIListItem *iter, *iter2;
 
136
   VMCI_Event e;
 
137
 
 
138
   /* We free all memory at exit. */
 
139
   for (e = 0; e < VMCI_EVENT_MAX; e++) {
 
140
      VMCIList_ScanSafe(iter, iter2, &subscriberArray[e]) {
 
141
         VMCISubscription *cur;
 
142
 
 
143
         /*
 
144
          * We should never get here because all events should have been
 
145
          * unregistered before we try to unload the driver module.
 
146
          * Also, delayed callbacks could still be firing so this cleanup
 
147
          * would not be safe.
 
148
          * Still it is better to free the memory than not ... so we
 
149
          * leave this code in just in case....
 
150
          *
 
151
          */
 
152
         ASSERT(FALSE);
 
153
 
 
154
         cur = VMCIList_Entry(iter, VMCISubscription, subscriberListItem);
 
155
         VMCI_FreeKernelMem(cur, sizeof *cur);
 
156
      }
 
157
   }
 
158
   VMCI_CleanupLock(&subscriberLock);
 
159
}
 
160
 
 
161
 
 
162
/*
 
163
 *-----------------------------------------------------------------------------
 
164
 *
 
165
 * VMCIEvent_Sync --
 
166
 *
 
167
 *      Use this as a synchronization point when setting globals, for example,
 
168
 *      during device shutdown.
 
169
 *
 
170
 * Results:
 
171
 *      TRUE.
 
172
 *
 
173
 * Side effects:
 
174
 *      None.
 
175
 *
 
176
 *-----------------------------------------------------------------------------
 
177
 */
 
178
 
 
179
void
 
180
VMCIEvent_Sync(void)
 
181
{
 
182
   VMCILockFlags lockFlags;
 
183
   VMCIEventGrabLock(&subscriberLock, &lockFlags);
 
184
   VMCIEventReleaseLock(&subscriberLock, lockFlags);
 
185
}
 
186
 
 
187
 
 
188
/*
 
189
 *-----------------------------------------------------------------------------
 
190
 *
 
191
 * VMCIEvent_CheckHostCapabilities --
 
192
 *
 
193
 *      Verify that the host supports the hypercalls we need. If it does not,
 
194
 *      try to find fallback hypercalls and use those instead.
 
195
 *
 
196
 * Results:
 
197
 *      TRUE if required hypercalls (or fallback hypercalls) are
 
198
 *      supported by the host, FALSE otherwise.
 
199
 *
 
200
 * Side effects:
 
201
 *      None.
 
202
 *
 
203
 *-----------------------------------------------------------------------------
 
204
 */
 
205
 
 
206
Bool
 
207
VMCIEvent_CheckHostCapabilities(void)
 
208
{
 
209
   /* VMCIEvent does not require any hypercalls. */
 
210
   return TRUE;
 
211
}
 
212
 
 
213
 
 
214
/*
 
215
 *-----------------------------------------------------------------------------
 
216
 *
 
217
 * VMCIEventGet --
 
218
 *
 
219
 *      Gets a reference to the given VMCISubscription.
 
220
 *
 
221
 * Results:
 
222
 *      None.
 
223
 *
 
224
 * Side effects:
 
225
 *      None.
 
226
 *
 
227
 *-----------------------------------------------------------------------------
 
228
 */
 
229
 
 
230
static void
 
231
VMCIEventGet(VMCISubscription *entry)  // IN
 
232
{
 
233
   ASSERT(entry);
 
234
 
 
235
   entry->refCount++;
 
236
}
 
237
 
 
238
 
 
239
/*
 
240
 *-----------------------------------------------------------------------------
 
241
 *
 
242
 * VMCIEventRelease --
 
243
 *
 
244
 *      Releases the given VMCISubscription.
 
245
 *
 
246
 * Results:
 
247
 *      None.
 
248
 *
 
249
 * Side effects:
 
250
 *      Fires the destroy event if the reference count has gone to zero.
 
251
 *
 
252
 *-----------------------------------------------------------------------------
 
253
 */
 
254
 
 
255
static void
 
256
VMCIEventRelease(VMCISubscription *entry)  // IN
 
257
{
 
258
   ASSERT(entry);
 
259
   ASSERT(entry->refCount > 0);
 
260
 
 
261
   entry->refCount--;
 
262
   if (entry->refCount == 0) {
 
263
      VMCI_SignalEvent(&entry->destroyEvent);
 
264
   }
 
265
}
 
266
 
 
267
 
 
268
 /*
 
269
 *------------------------------------------------------------------------------
 
270
 *
 
271
 *  EventReleaseCB --
 
272
 *
 
273
 *     Callback to release the event entry reference. It is called by the
 
274
 *     VMCI_WaitOnEvent function before it blocks.
 
275
 *
 
276
 *  Result:
 
277
 *     None.
 
278
 *
 
279
 *  Side effects:
 
280
 *     None.
 
281
 *
 
282
 *------------------------------------------------------------------------------
 
283
 */
 
284
 
 
285
static int
 
286
EventReleaseCB(void *clientData) // IN
 
287
{
 
288
   VMCILockFlags flags;
 
289
   VMCISubscription *sub = (VMCISubscription *)clientData;
 
290
 
 
291
   ASSERT(sub);
 
292
 
 
293
   VMCIEventGrabLock(&subscriberLock, &flags);
 
294
   VMCIEventRelease(sub);
 
295
   VMCIEventReleaseLock(&subscriberLock, flags);
 
296
 
 
297
   return 0;
 
298
}
 
299
 
 
300
 
 
301
/*
 
302
 *-----------------------------------------------------------------------------
 
303
 *
 
304
 * VMCIEventFind --
 
305
 *
 
306
 *      Find entry. Assumes lock is held.
 
307
 *
 
308
 * Results:
 
309
 *      Entry if found, NULL if not.
 
310
 *
 
311
 * Side effects:
 
312
 *      Increments the VMCISubscription refcount if an entry is found.
 
313
 *
 
314
 *-----------------------------------------------------------------------------
 
315
 */
 
316
 
 
317
static VMCISubscription *
 
318
VMCIEventFind(VMCIId subID)  // IN
 
319
{
 
320
   VMCIListItem *iter;
 
321
   VMCI_Event e;
 
322
 
 
323
   for (e = 0; e < VMCI_EVENT_MAX; e++) {
 
324
      VMCIList_Scan(iter, &subscriberArray[e]) {
 
325
         VMCISubscription *cur =
 
326
            VMCIList_Entry(iter, VMCISubscription, subscriberListItem);
 
327
         if (cur->id == subID) {
 
328
            VMCIEventGet(cur);
 
329
            return cur;
 
330
         }
 
331
      }
 
332
   }
 
333
   return NULL;
 
334
}
 
335
 
 
336
 
 
337
/*
 
338
 *----------------------------------------------------------------------
 
339
 *
 
340
 * VMCIEventDelayedDispatchCB --
 
341
 *
 
342
 *      Calls the specified callback in a delayed context.
 
343
 *
 
344
 * Results:
 
345
 *      None.
 
346
 *
 
347
 * Side effects:
 
348
 *      None.
 
349
 *
 
350
 *----------------------------------------------------------------------
 
351
 */
 
352
 
 
353
static void
 
354
VMCIEventDelayedDispatchCB(void *data) // IN
 
355
{
 
356
   VMCIDelayedEventInfo *eventInfo;
 
357
   VMCISubscription *sub;
 
358
   VMCI_EventData *ed;
 
359
   VMCILockFlags flags;
 
360
 
 
361
   eventInfo = (VMCIDelayedEventInfo *)data;
 
362
 
 
363
   ASSERT(eventInfo);
 
364
   ASSERT(eventInfo->sub);
 
365
 
 
366
   sub = eventInfo->sub;
 
367
   ed = (VMCI_EventData *)eventInfo->eventPayload;
 
368
 
 
369
   sub->callback(sub->id, ed, sub->callbackData);
 
370
 
 
371
   VMCIEventGrabLock(&subscriberLock, &flags);
 
372
   VMCIEventRelease(sub);
 
373
   VMCIEventReleaseLock(&subscriberLock, flags);
 
374
 
 
375
   VMCI_FreeKernelMem(eventInfo, sizeof *eventInfo);
 
376
}
 
377
 
 
378
 
 
379
/*
 
380
 *----------------------------------------------------------------------------
 
381
 *
 
382
 * VMCIEventDeliver --
 
383
 *
 
384
 *      Actually delivers the events to the subscribers.
 
385
 *
 
386
 * Results:
 
387
 *      None.
 
388
 *
 
389
 * Side effects:
 
390
 *      The callback function for each subscriber is invoked.
 
391
 *
 
392
 *----------------------------------------------------------------------------
 
393
 */
 
394
 
 
395
static int
 
396
VMCIEventDeliver(VMCIEventMsg *eventMsg)  // IN
 
397
{
 
398
   int err = VMCI_SUCCESS;
 
399
   VMCIListItem *iter;
 
400
   VMCILockFlags flags;
 
401
 
 
402
   ASSERT(eventMsg);
 
403
 
 
404
   VMCIEventGrabLock(&subscriberLock, &flags);
 
405
   VMCIList_Scan(iter, &subscriberArray[eventMsg->eventData.event]) {
 
406
      VMCI_EventData *ed;
 
407
      VMCISubscription *cur = VMCIList_Entry(iter, VMCISubscription,
 
408
                                             subscriberListItem);
 
409
      ASSERT(cur && cur->event == eventMsg->eventData.event);
 
410
 
 
411
      if (cur->runDelayed) {
 
412
         VMCIDelayedEventInfo *eventInfo;
 
413
         if ((eventInfo = VMCI_AllocKernelMem(sizeof *eventInfo,
 
414
                                              (VMCI_MEMORY_ATOMIC |
 
415
                                               VMCI_MEMORY_NONPAGED))) == NULL) {
 
416
            err = VMCI_ERROR_NO_MEM;
 
417
            goto out;
 
418
         }
 
419
 
 
420
         VMCIEventGet(cur);
 
421
 
 
422
         memset(eventInfo, 0, sizeof *eventInfo);
 
423
         memcpy(eventInfo->eventPayload, VMCI_DG_PAYLOAD(eventMsg),
 
424
                (size_t)eventMsg->hdr.payloadSize);
 
425
         eventInfo->sub = cur;
 
426
         err = VMCI_ScheduleDelayedWork(VMCIEventDelayedDispatchCB,
 
427
                                        eventInfo);
 
428
         if (err != VMCI_SUCCESS) {
 
429
            VMCIEventRelease(cur);
 
430
            VMCI_FreeKernelMem(eventInfo, sizeof *eventInfo);
 
431
            goto out;
 
432
         }
 
433
 
 
434
      } else {
 
435
         uint8 eventPayload[sizeof(VMCIEventData_Max)];
 
436
 
 
437
         /* We set event data before each callback to ensure isolation. */
 
438
         memset(eventPayload, 0, sizeof eventPayload);
 
439
         memcpy(eventPayload, VMCI_DG_PAYLOAD(eventMsg),
 
440
                (size_t)eventMsg->hdr.payloadSize);
 
441
         ed = (VMCI_EventData *)eventPayload;
 
442
         cur->callback(cur->id, ed, cur->callbackData);
 
443
      }
 
444
   }
 
445
 
 
446
out:
 
447
   VMCIEventReleaseLock(&subscriberLock, flags);
 
448
 
 
449
   return err;
 
450
}
 
451
 
 
452
 
 
453
/*
 
454
 *----------------------------------------------------------------------
 
455
 *
 
456
 * VMCIEvent_Dispatch --
 
457
 *
 
458
 *      Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all
 
459
 *      subscribers for given event.
 
460
 *
 
461
 * Results:
 
462
 *      VMCI_SUCCESS on success, error code otherwise.
 
463
 *
 
464
 * Side effects:
 
465
 *      None.
 
466
 *
 
467
 *----------------------------------------------------------------------
 
468
 */
 
469
 
 
470
int
 
471
VMCIEvent_Dispatch(VMCIDatagram *msg)  // IN
 
472
{
 
473
   VMCIEventMsg *eventMsg = (VMCIEventMsg *)msg;
 
474
 
 
475
   ASSERT(msg &&
 
476
          msg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
 
477
          msg->dst.resource == VMCI_EVENT_HANDLER);
 
478
 
 
479
   if (msg->payloadSize < sizeof(VMCI_Event) ||
 
480
       msg->payloadSize > sizeof(VMCIEventData_Max)) {
 
481
      return VMCI_ERROR_INVALID_ARGS;
 
482
   }
 
483
 
 
484
   if (!VMCI_EVENT_VALID(eventMsg->eventData.event)) {
 
485
      return VMCI_ERROR_EVENT_UNKNOWN;
 
486
   }
 
487
 
 
488
   VMCIEventDeliver(eventMsg);
 
489
 
 
490
   return VMCI_SUCCESS;
 
491
}
 
492
 
 
493
 
 
494
/*
 
495
 *----------------------------------------------------------------------
 
496
 *
 
497
 * VMCIEventRegisterSubscription --
 
498
 *
 
499
 *      Initialize and add subscription to subscriber list.
 
500
 *
 
501
 * Results:
 
502
 *      VMCI_SUCCESS on success, error code otherwise.
 
503
 *
 
504
 * Side effects:
 
505
 *      None.
 
506
 *
 
507
 *----------------------------------------------------------------------
 
508
 */
 
509
 
 
510
static int
 
511
VMCIEventRegisterSubscription(VMCISubscription *sub,   // IN
 
512
                              VMCI_Event event,        // IN
 
513
                              uint32 flags,            // IN
 
514
                              VMCI_EventCB callback,   // IN
 
515
                              void *callbackData)      // IN
 
516
{
 
517
#  define VMCI_EVENT_MAX_ATTEMPTS 10
 
518
   static VMCIId subscriptionID = 0;
 
519
   VMCILockFlags lockFlags;
 
520
   uint32 attempts = 0;
 
521
   int result;
 
522
   Bool success;
 
523
 
 
524
   ASSERT(sub);
 
525
 
 
526
   if (!VMCI_EVENT_VALID(event) || callback == NULL) {
 
527
      VMCI_DEBUG_LOG(4, (LGPFX"Failed to subscribe to event (type=%d) "
 
528
                         "(callback=%p) (data=%p).\n",
 
529
                         event, callback, callbackData));
 
530
      return VMCI_ERROR_INVALID_ARGS;
 
531
   }
 
532
 
 
533
   if (vmkernel) {
 
534
      /*
 
535
       * In the vmkernel we defer delivery of events to a helper world.  This
 
536
       * makes the event delivery more consistent across hosts and guests with
 
537
       * regard to which locks are held.
 
538
       */
 
539
      sub->runDelayed = TRUE;
 
540
   } else if (!VMCI_CanScheduleDelayedWork()) {
 
541
      /*
 
542
       * If the platform doesn't support delayed work callbacks then don't
 
543
       * allow registration for them.
 
544
       */
 
545
      if (flags & VMCI_FLAG_EVENT_DELAYED_CB) {
 
546
         return VMCI_ERROR_INVALID_ARGS;
 
547
      }
 
548
      sub->runDelayed = FALSE;
 
549
   } else {
 
550
      /*
 
551
       * The platform supports delayed work callbacks. Honor the requested
 
552
       * flags
 
553
       */
 
554
      sub->runDelayed = (flags & VMCI_FLAG_EVENT_DELAYED_CB) ? TRUE : FALSE;
 
555
   }
 
556
 
 
557
   sub->refCount = 1;
 
558
   sub->event = event;
 
559
   sub->callback = callback;
 
560
   sub->callbackData = callbackData;
 
561
   VMCIList_InitEntry(&sub->subscriberListItem);
 
562
 
 
563
   VMCIEventGrabLock(&subscriberLock, &lockFlags);
 
564
 
 
565
   /* Check if creation of a new event is allowed. */
 
566
   if (!VMCI_CanCreate()) {
 
567
      result = VMCI_ERROR_UNAVAILABLE;
 
568
      goto exit;
 
569
   }
 
570
 
 
571
   for (success = FALSE, attempts = 0;
 
572
        success == FALSE && attempts < VMCI_EVENT_MAX_ATTEMPTS;
 
573
        attempts++) {
 
574
      VMCISubscription *existingSub = NULL;
 
575
 
 
576
      /*
 
577
       * We try to get an id a couple of time before claiming we are out of
 
578
       * resources.
 
579
       */
 
580
      sub->id = ++subscriptionID;
 
581
 
 
582
      /* Test for duplicate id. */
 
583
      existingSub = VMCIEventFind(sub->id);
 
584
      if (existingSub == NULL) {
 
585
         /* We succeeded if we didn't find a duplicate. */
 
586
         success = TRUE;
 
587
      } else {
 
588
         VMCIEventRelease(existingSub);
 
589
      }
 
590
   }
 
591
 
 
592
   if (success) {
 
593
      VMCI_CreateEvent(&sub->destroyEvent);
 
594
      VMCIList_Insert(&sub->subscriberListItem, &subscriberArray[event]);
 
595
      result = VMCI_SUCCESS;
 
596
   } else {
 
597
      result = VMCI_ERROR_NO_RESOURCES;
 
598
   }
 
599
 
 
600
exit:
 
601
   VMCIEventReleaseLock(&subscriberLock, lockFlags);
 
602
   return result;
 
603
#  undef VMCI_EVENT_MAX_ATTEMPTS
 
604
}
 
605
 
 
606
 
 
607
 
 
608
/*
 
609
 *----------------------------------------------------------------------
 
610
 *
 
611
 * VMCIEventUnregisterSubscription --
 
612
 *
 
613
 *      Remove subscription from subscriber list.
 
614
 *
 
615
 * Results:
 
616
 *      VMCISubscription when found, NULL otherwise.
 
617
 *
 
618
 * Side effects:
 
619
 *      None.
 
620
 *
 
621
 *----------------------------------------------------------------------
 
622
 */
 
623
 
 
624
static VMCISubscription *
 
625
VMCIEventUnregisterSubscription(VMCIId subID)    // IN
 
626
{
 
627
   VMCILockFlags flags;
 
628
   VMCISubscription *s;
 
629
 
 
630
   VMCIEventGrabLock(&subscriberLock, &flags);
 
631
   s = VMCIEventFind(subID);
 
632
   if (s != NULL) {
 
633
      VMCIEventRelease(s);
 
634
      VMCIList_Remove(&s->subscriberListItem, &subscriberArray[s->event]);
 
635
   }
 
636
   VMCIEventReleaseLock(&subscriberLock, flags);
 
637
 
 
638
   if (s != NULL) {
 
639
      VMCI_WaitOnEvent(&s->destroyEvent, EventReleaseCB, s);
 
640
      VMCI_DestroyEvent(&s->destroyEvent);
 
641
   }
 
642
 
 
643
   return s;
 
644
}
 
645
 
 
646
 
 
647
/*
 
648
 *----------------------------------------------------------------------
 
649
 *
 
650
 * VMCIEvent_Subscribe --
 
651
 *
 
652
 *      Subscribe to given event. The callback specified can be fired
 
653
 *      in different contexts depending on what flag is specified while
 
654
 *      registering. If flags contains VMCI_FLAG_EVENT_NONE then the
 
655
 *      callback is fired with the subscriber lock held (and BH context
 
656
 *      on the guest). If flags contain VMCI_FLAG_EVENT_DELAYED_CB then
 
657
 *      the callback is fired with no locks held in thread context.
 
658
 *      This is useful because other VMCIEvent functions can be called,
 
659
 *      but it also increases the chances that an event will be dropped.
 
660
 *
 
661
 * Results:
 
662
 *      VMCI_SUCCESS on success, error code otherwise.
 
663
 *
 
664
 * Side effects:
 
665
 *      None.
 
666
 *
 
667
 *----------------------------------------------------------------------
 
668
 */
 
669
 
 
670
VMCI_EXPORT_SYMBOL(VMCIEvent_Subscribe)
 
671
int
 
672
VMCIEvent_Subscribe(VMCI_Event event,        // IN
 
673
                    uint32 flags,            // IN
 
674
                    VMCI_EventCB callback,   // IN
 
675
                    void *callbackData,      // IN
 
676
                    VMCIId *subscriptionID)  // OUT
 
677
{
 
678
   int retval;
 
679
   VMCISubscription *s = NULL;
 
680
 
 
681
   if (subscriptionID == NULL) {
 
682
      VMCI_DEBUG_LOG(4, (LGPFX"Invalid subscription (NULL).\n"));
 
683
      return VMCI_ERROR_INVALID_ARGS;
 
684
   }
 
685
 
 
686
   s = VMCI_AllocKernelMem(sizeof *s, VMCI_MEMORY_NONPAGED);
 
687
   if (s == NULL) {
 
688
      return VMCI_ERROR_NO_MEM;
 
689
   }
 
690
 
 
691
   retval = VMCIEventRegisterSubscription(s, event, flags,
 
692
                                          callback, callbackData);
 
693
   if (retval < VMCI_SUCCESS) {
 
694
      VMCI_FreeKernelMem(s, sizeof *s);
 
695
      return retval;
 
696
   }
 
697
 
 
698
   *subscriptionID = s->id;
 
699
   return retval;
 
700
}
 
701
 
 
702
 
 
703
/*
 
704
 *----------------------------------------------------------------------
 
705
 *
 
706
 * VMCIEvent_Unsubscribe --
 
707
 *
 
708
 *      Unsubscribe to given event. Removes it from list and frees it.
 
709
 *      Will return callbackData if requested by caller.
 
710
 *
 
711
 * Results:
 
712
 *      VMCI_SUCCESS on success, error code otherwise.
 
713
 *
 
714
 * Side effects:
 
715
 *      None.
 
716
 *
 
717
 *----------------------------------------------------------------------
 
718
 */
 
719
 
 
720
VMCI_EXPORT_SYMBOL(VMCIEvent_Unsubscribe)
 
721
int
 
722
VMCIEvent_Unsubscribe(VMCIId subID)   // IN
 
723
{
 
724
   VMCISubscription *s;
 
725
 
 
726
   /*
 
727
    * Return subscription. At this point we know noone else is accessing
 
728
    * the subscription so we can free it.
 
729
    */
 
730
   s = VMCIEventUnregisterSubscription(subID);
 
731
   if (s == NULL) {
 
732
      return VMCI_ERROR_NOT_FOUND;
 
733
 
 
734
   }
 
735
   VMCI_FreeKernelMem(s, sizeof *s);
 
736
 
 
737
   return VMCI_SUCCESS;
 
738
}