~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to modules/linux/vmci/vmciQueuePair.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
 
 * vmciQueuePair.c --
21
 
 *
22
 
 *     Implements the VMCI QueuePair API.
23
 
 */
24
 
 
25
 
#ifdef __linux__
26
 
#  include "driver-config.h"
27
 
#  include <asm/page.h>
28
 
#  include <linux/module.h>
29
 
#elif defined(_WIN32)
30
 
#  include <wdm.h>
31
 
#elif defined(__APPLE__)
32
 
#  include <IOKit/IOLib.h>
33
 
#endif /* __linux__ */
34
 
 
35
 
#include "vm_assert.h"
36
 
#include "vmci_kernel_if.h"
37
 
#include "vmciQueuePairInt.h"
38
 
#include "vmciUtil.h"
39
 
#include "vmciInt.h"
40
 
#include "vmciEvent.h"
41
 
#include "circList.h"
42
 
 
43
 
#define LGPFX "VMCIQueuePair: "
44
 
 
45
 
typedef struct QueuePairEntry {
46
 
   VMCIHandle handle;
47
 
   VMCIId     peer;
48
 
   uint32     flags;
49
 
   uint64     produceSize;
50
 
   uint64     consumeSize;
51
 
   uint64     numPPNs;
52
 
   PPNSet     ppnSet;
53
 
   void      *produceQ;
54
 
   void      *consumeQ;
55
 
   uint32     refCount;
56
 
   ListItem   listItem;
57
 
} QueuePairEntry;
58
 
 
59
 
typedef struct QueuePairList {
60
 
   ListItem  *head;
61
 
   VMCIMutex mutex;
62
 
} QueuePairList;
63
 
 
64
 
static QueuePairList queuePairList;
65
 
 
66
 
static QueuePairEntry *QueuePairList_FindEntry(VMCIHandle handle);
67
 
static void QueuePairList_AddEntry(QueuePairEntry *entry);
68
 
static void QueuePairList_RemoveEntry(QueuePairEntry *entry);
69
 
static QueuePairEntry *QueuePairList_GetHead(void);
70
 
static QueuePairEntry *QueuePairEntryCreate(VMCIHandle handle,
71
 
                                            VMCIId peer, uint32 flags,
72
 
                                            uint64 produceSize,
73
 
                                            uint64 consumeSize,
74
 
                                            void *produceQ, void *consumeQ);
75
 
static void QueuePairEntryDestroy(QueuePairEntry *entry);
76
 
static int VMCIQueuePairAlloc_HyperCall(const QueuePairEntry *entry);
77
 
static int VMCIQueuePairAllocHelper(VMCIHandle *handle, VMCIQueue **produceQ,
78
 
                                    uint64 produceSize, VMCIQueue **consumeQ,
79
 
                                    uint64 consumeSize,
80
 
                                    VMCIId peer, uint32 flags);
81
 
static int VMCIQueuePairDetachHelper(VMCIHandle handle);
82
 
static int QueuePairNotifyPeerLocal(Bool attach, VMCIHandle handle);
83
 
 
84
 
 
85
 
/*
86
 
 *-----------------------------------------------------------------------------
87
 
 *
88
 
 * QueuePairLock_Init --
89
 
 *
90
 
 *      Creates the lock protecting the QueuePair list.
91
 
 *
92
 
 * Results:
93
 
 *      None.
94
 
 *
95
 
 * Side effects:
96
 
 *      None.
97
 
 *
98
 
 *-----------------------------------------------------------------------------
99
 
 */
100
 
 
101
 
static INLINE void
102
 
QueuePairLock_Init(void)
103
 
{
104
 
   VMCIMutex_Init(&queuePairList.mutex);
105
 
}
106
 
 
107
 
 
108
 
/*
109
 
 *-----------------------------------------------------------------------------
110
 
 *
111
 
 * QueuePairLock_Destroy --
112
 
 *
113
 
 *      Destroys the lock protecting the QueuePair list.
114
 
 *
115
 
 * Results:
116
 
 *      None.
117
 
 *
118
 
 * Side effects:
119
 
 *      None.
120
 
 *
121
 
 *-----------------------------------------------------------------------------
122
 
 */
123
 
 
124
 
static INLINE void
125
 
QueuePairLock_Destroy(void)
126
 
{
127
 
   VMCIMutex_Destroy(&queuePairList.mutex); /* No-op on Linux and Windows. */
128
 
}
129
 
 
130
 
 
131
 
/*
132
 
 *-----------------------------------------------------------------------------
133
 
 *
134
 
 * QueuePairList_Lock --
135
 
 *
136
 
 *      Acquires the lock protecting the QueuePair list.
137
 
 *
138
 
 * Results:
139
 
 *      None.
140
 
 *
141
 
 * Side effects:
142
 
 *      None.
143
 
 *
144
 
 *-----------------------------------------------------------------------------
145
 
 */
146
 
 
147
 
static INLINE void
148
 
QueuePairList_Lock(void)
149
 
{
150
 
   VMCIMutex_Acquire(&queuePairList.mutex);
151
 
}
152
 
 
153
 
 
154
 
/*
155
 
 *-----------------------------------------------------------------------------
156
 
 *
157
 
 * QueuePairList_Unlock --
158
 
 *
159
 
 *      Releases the lock protecting the QueuePair list.
160
 
 *
161
 
 * Results:
162
 
 *      None.
163
 
 *
164
 
 * Side effects:
165
 
 *      None.
166
 
 *
167
 
 *-----------------------------------------------------------------------------
168
 
 */
169
 
 
170
 
static INLINE void
171
 
QueuePairList_Unlock(void)
172
 
{
173
 
   VMCIMutex_Release(&queuePairList.mutex);
174
 
}
175
 
 
176
 
 
177
 
/*
178
 
 *-----------------------------------------------------------------------------
179
 
 *
180
 
 * VMCIQueuePair_Init --
181
 
 *
182
 
 *      Initalizes QueuePair data structure state.
183
 
 *
184
 
 * Results:
185
 
 *      None.
186
 
 *
187
 
 * Side effects:
188
 
 *      None.
189
 
 *
190
 
 *-----------------------------------------------------------------------------
191
 
 */
192
 
 
193
 
void
194
 
VMCIQueuePair_Init(void)
195
 
{
196
 
   queuePairList.head = NULL;
197
 
   QueuePairLock_Init();
198
 
}
199
 
 
200
 
 
201
 
/*
202
 
 *-----------------------------------------------------------------------------
203
 
 *
204
 
 * VMCIQueuePair_Exit --
205
 
 *
206
 
 *      Destroys all QueuePairs. Makes hypercalls to detach from QueuePairs.
207
 
 *
208
 
 * Results:
209
 
 *      None.
210
 
 *
211
 
 * Side effects:
212
 
 *      None.
213
 
 *
214
 
 *-----------------------------------------------------------------------------
215
 
 */
216
 
 
217
 
void
218
 
VMCIQueuePair_Exit(void)
219
 
{
220
 
   QueuePairEntry *entry;
221
 
 
222
 
   QueuePairList_Lock();
223
 
 
224
 
   while ((entry = QueuePairList_GetHead())) {
225
 
      /*
226
 
       * Don't make a hypercall for local QueuePairs.
227
 
       */
228
 
      if (!(entry->flags & VMCI_QPFLAG_LOCAL)) {
229
 
         VMCIQueuePairDetachMsg detachMsg;
230
 
 
231
 
         detachMsg.hdr.dst = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
232
 
                                              VMCI_QUEUEPAIR_DETACH);
233
 
         detachMsg.hdr.src = VMCI_ANON_SRC_HANDLE;
234
 
         detachMsg.hdr.payloadSize = sizeof entry->handle;
235
 
         detachMsg.handle = entry->handle;
236
 
 
237
 
         (void)VMCI_SendDatagram((VMCIDatagram *)&detachMsg);
238
 
      }
239
 
      /*
240
 
       * We cannot fail the exit, so let's reset refCount.
241
 
       */
242
 
      entry->refCount = 0;
243
 
      QueuePairList_RemoveEntry(entry);
244
 
      QueuePairEntryDestroy(entry);
245
 
   }
246
 
 
247
 
   QueuePairList_Unlock();
248
 
   QueuePairLock_Destroy();
249
 
}
250
 
 
251
 
 
252
 
/*
253
 
 *-----------------------------------------------------------------------------
254
 
 *
255
 
 * QueuePairList_FindEntry --
256
 
 *
257
 
 *    Searches the list of QueuePairs to find if an entry already exists.
258
 
 *    Assumes that the lock on the list is held.
259
 
 *
260
 
 * Results:
261
 
 *    Pointer to the entry if it exists, NULL otherwise.
262
 
 *
263
 
 * Side effects:
264
 
 *    None.
265
 
 *
266
 
 *-----------------------------------------------------------------------------
267
 
 */
268
 
 
269
 
static QueuePairEntry *
270
 
QueuePairList_FindEntry(VMCIHandle handle) // IN:
271
 
{
272
 
   ListItem *next;
273
 
 
274
 
   if (VMCI_HANDLE_INVALID(handle)) {
275
 
      return NULL;
276
 
   }
277
 
 
278
 
   LIST_SCAN(next, queuePairList.head) {
279
 
      QueuePairEntry *entry = LIST_CONTAINER(next, QueuePairEntry, listItem);
280
 
 
281
 
      if (VMCI_HANDLE_EQUAL(entry->handle, handle)) {
282
 
         return entry;
283
 
      }
284
 
   }
285
 
 
286
 
   return NULL;
287
 
}
288
 
 
289
 
 
290
 
/*
291
 
 *-----------------------------------------------------------------------------
292
 
 *
293
 
 * QueuePairList_AddEntry --
294
 
 *
295
 
 *    Appends a QueuePair entry to the list. Assumes that the lock on the
296
 
 *    list is held.
297
 
 *
298
 
 * Results:
299
 
 *    None.
300
 
 *
301
 
 * Side effects:
302
 
 *    None.
303
 
 *
304
 
 *-----------------------------------------------------------------------------
305
 
 */
306
 
 
307
 
static void
308
 
QueuePairList_AddEntry(QueuePairEntry *entry) // IN:
309
 
{
310
 
   if (entry) {
311
 
      LIST_QUEUE(&entry->listItem, &queuePairList.head);
312
 
   }
313
 
}
314
 
 
315
 
 
316
 
/*
317
 
 *-----------------------------------------------------------------------------
318
 
 *
319
 
 * QueuePairList_RemoveEntry --
320
 
 *
321
 
 *    Removes a QueuePair entry from the list. Assumes that the lock on the
322
 
 *    list is held.
323
 
 *
324
 
 * Results:
325
 
 *    None.
326
 
 *
327
 
 * Side effects:
328
 
 *    None.
329
 
 *
330
 
 *-----------------------------------------------------------------------------
331
 
 */
332
 
 
333
 
static void
334
 
QueuePairList_RemoveEntry(QueuePairEntry *entry) // IN:
335
 
{
336
 
   if (entry) {
337
 
      LIST_DEL(&entry->listItem, &queuePairList.head);
338
 
   }
339
 
}
340
 
 
341
 
 
342
 
/*
343
 
 *-----------------------------------------------------------------------------
344
 
 *
345
 
 * QueuePairList_GetHead --
346
 
 *
347
 
 *      Returns the entry from the head of the list. Assumes that the list is
348
 
 *      locked.
349
 
 *
350
 
 * Results:
351
 
 *      Pointer to entry.
352
 
 *
353
 
 * Side effects:
354
 
 *      None.
355
 
 *
356
 
 *-----------------------------------------------------------------------------
357
 
 */
358
 
 
359
 
static QueuePairEntry *
360
 
QueuePairList_GetHead(void)
361
 
{
362
 
   ListItem *first = LIST_FIRST(queuePairList.head);
363
 
 
364
 
   if (first) {
365
 
      QueuePairEntry *entry = LIST_CONTAINER(first, QueuePairEntry, listItem);
366
 
      return entry;
367
 
   }
368
 
 
369
 
   return NULL;
370
 
}
371
 
 
372
 
 
373
 
/*
374
 
 *-----------------------------------------------------------------------------
375
 
 *
376
 
 * VMCIQueuePair_Alloc --
377
 
 *
378
 
 *      Allocates a VMCI QueuePair. Only checks validity of input arguments.
379
 
 *      Real work is done in the OS-specific helper routine.
380
 
 *
381
 
 * Results:
382
 
 *      Success or failure.
383
 
 *
384
 
 * Side effects:
385
 
 *      Memory is allocated.
386
 
 *
387
 
 *-----------------------------------------------------------------------------
388
 
 */
389
 
 
390
 
int
391
 
VMCIQueuePair_Alloc(VMCIHandle *handle,     // IN/OUT:
392
 
                    VMCIQueue  **produceQ,  // OUT:
393
 
                    uint64     produceSize, // IN:
394
 
                    VMCIQueue  **consumeQ,  // OUT:
395
 
                    uint64     consumeSize, // IN:
396
 
                    VMCIId     peer,        // IN:
397
 
                    uint32     flags)       // IN:
398
 
{
399
 
   ASSERT_ON_COMPILE(sizeof(VMCIQueueHeader) <= PAGE_SIZE);
400
 
 
401
 
   return VMCIQueuePair_AllocPriv(handle, produceQ, produceSize, consumeQ, consumeSize, peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
402
 
}
403
 
 
404
 
 
405
 
/*
406
 
 *-----------------------------------------------------------------------------
407
 
 *
408
 
 * VMCIQueuePair_AllocPriv --
409
 
 *
410
 
 *      Provided for compatibility with the host API. Always returns an error
411
 
 *      since requesting privileges from the guest is not allowed. Use
412
 
 *      VMCIQueuePair_Alloc instead.
413
 
 *
414
 
 * Results:
415
 
 *      An error.
416
 
 *
417
 
 * Side effects:
418
 
 *      None.
419
 
 *
420
 
 *-----------------------------------------------------------------------------
421
 
 */
422
 
 
423
 
int
424
 
VMCIQueuePair_AllocPriv(VMCIHandle *handle,           // IN/OUT:
425
 
                        VMCIQueue  **produceQ,        // OUT:
426
 
                        uint64     produceSize,       // IN:
427
 
                        VMCIQueue  **consumeQ,        // OUT:
428
 
                        uint64     consumeSize,       // IN:
429
 
                        VMCIId     peer,              // IN:
430
 
                        uint32     flags,             // IN:
431
 
                        VMCIPrivilegeFlags privFlags) // IN:
432
 
{
433
 
   if (privFlags != VMCI_NO_PRIVILEGE_FLAGS) {
434
 
      return VMCI_ERROR_NO_ACCESS;
435
 
   }
436
 
 
437
 
   if (!handle || !produceQ || !consumeQ || (!produceSize && !consumeSize) ||
438
 
       (flags & ~VMCI_QP_ALL_FLAGS)) {
439
 
      return VMCI_ERROR_INVALID_ARGS;
440
 
   }
441
 
 
442
 
   return VMCIQueuePairAllocHelper(handle, produceQ, produceSize, consumeQ,
443
 
                                   consumeSize, peer, flags);
444
 
}
445
 
 
446
 
 
447
 
/*
448
 
 *-----------------------------------------------------------------------------
449
 
 *
450
 
 * VMCIQueuePair_Detach --
451
 
 *
452
 
 *      Detaches from a VMCI QueuePair. Only checks validity of input argument.
453
 
 *      Real work is done in the OS-specific helper routine.
454
 
 *
455
 
 * Results:
456
 
 *      Success or failure.
457
 
 *
458
 
 * Side effects:
459
 
 *      Memory is freed.
460
 
 *
461
 
 *-----------------------------------------------------------------------------
462
 
 */
463
 
 
464
 
int
465
 
VMCIQueuePair_Detach(VMCIHandle handle) // IN:
466
 
{
467
 
   if (VMCI_HANDLE_INVALID(handle)) {
468
 
      return VMCI_ERROR_INVALID_ARGS;
469
 
   }
470
 
   return VMCIQueuePairDetachHelper(handle);
471
 
}
472
 
 
473
 
 
474
 
/*
475
 
 *-----------------------------------------------------------------------------
476
 
 *
477
 
 * QueuePairEntryCreate --
478
 
 *
479
 
 *      Allocates and initializes a QueuePairEntry structure.  Allocates a
480
 
 *      QueuePair rid (and handle) iff the given entry has an invalid handle.
481
 
 *      0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved handles.  Assumes
482
 
 *      that the QP list lock is held by the caller.
483
 
 *
484
 
 * Results:
485
 
 *      Pointer to structure intialized.
486
 
 *
487
 
 * Side effects:
488
 
 *      None.
489
 
 *
490
 
 *-----------------------------------------------------------------------------
491
 
 */
492
 
 
493
 
QueuePairEntry *
494
 
QueuePairEntryCreate(VMCIHandle handle,  // IN:
495
 
                     VMCIId peer,        // IN:
496
 
                     uint32 flags,       // IN:
497
 
                     uint64 produceSize, // IN:
498
 
                     uint64 consumeSize, // IN:
499
 
                     void *produceQ,     // IN:
500
 
                     void *consumeQ)     // IN:
501
 
{
502
 
   static VMCIId queuePairRID = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
503
 
   QueuePairEntry *entry;
504
 
   const uint64 numPPNs = CEILING(produceSize, PAGE_SIZE) +
505
 
                          CEILING(consumeSize, PAGE_SIZE) +
506
 
                          2; /* One page each for the queue headers. */
507
 
 
508
 
   ASSERT((produceSize || consumeSize) && produceQ && consumeQ);
509
 
 
510
 
   if (VMCI_HANDLE_INVALID(handle)) {
511
 
      VMCIId contextID = VMCI_GetContextID();
512
 
      VMCIId oldRID = queuePairRID;
513
 
 
514
 
      /*
515
 
       * Generate a unique QueuePair rid.  Keep on trying until we wrap around
516
 
       * in the RID space.
517
 
       */
518
 
      ASSERT(oldRID > VMCI_RESERVED_RESOURCE_ID_MAX);
519
 
      do {
520
 
         handle = VMCI_MAKE_HANDLE(contextID, queuePairRID);
521
 
         entry = QueuePairList_FindEntry(handle);
522
 
         queuePairRID++;
523
 
         if (UNLIKELY(!queuePairRID)) {
524
 
            /*
525
 
             * Skip the reserved rids.
526
 
             */
527
 
            queuePairRID = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
528
 
         }
529
 
      } while (entry && queuePairRID != oldRID);
530
 
 
531
 
      if (UNLIKELY(entry != NULL)) {
532
 
         ASSERT(queuePairRID == oldRID);
533
 
         /*
534
 
          * We wrapped around --- no rids were free.
535
 
          */
536
 
         return NULL;
537
 
      }
538
 
   }
539
 
 
540
 
   ASSERT(!VMCI_HANDLE_INVALID(handle) &&
541
 
          QueuePairList_FindEntry(handle) == NULL);
542
 
   entry = VMCI_AllocKernelMem(sizeof *entry, VMCI_MEMORY_NORMAL);
543
 
   if (entry) {
544
 
      entry->handle = handle;
545
 
      entry->peer = peer;
546
 
      entry->flags = flags;
547
 
      entry->produceSize = produceSize;
548
 
      entry->consumeSize = consumeSize;
549
 
      entry->numPPNs = numPPNs;
550
 
      memset(&entry->ppnSet, 0, sizeof entry->ppnSet);
551
 
      entry->produceQ = produceQ;
552
 
      entry->consumeQ = consumeQ;
553
 
      entry->refCount = 0;
554
 
      INIT_LIST_ITEM(&entry->listItem);
555
 
   }
556
 
   return entry;
557
 
}
558
 
 
559
 
 
560
 
/*
561
 
 *-----------------------------------------------------------------------------
562
 
 *
563
 
 * QueuePairEntryDestroy --
564
 
 *
565
 
 *      Frees a QueuePairEntry structure.
566
 
 *
567
 
 * Results:
568
 
 *      None.
569
 
 *
570
 
 * Side effects:
571
 
 *      None.
572
 
 *
573
 
 *-----------------------------------------------------------------------------
574
 
 */
575
 
 
576
 
void
577
 
QueuePairEntryDestroy(QueuePairEntry *entry) // IN:
578
 
{
579
 
   ASSERT(entry);
580
 
   ASSERT(entry->refCount == 0);
581
 
 
582
 
   VMCI_FreePPNSet(&entry->ppnSet);
583
 
   VMCI_FreeQueue(entry->produceQ, entry->produceSize);
584
 
   VMCI_FreeQueue(entry->consumeQ, entry->consumeSize);
585
 
   VMCI_FreeKernelMem(entry, sizeof *entry);
586
 
}
587
 
 
588
 
 
589
 
/*
590
 
 *-----------------------------------------------------------------------------
591
 
 *
592
 
 * VMCIQueuePairAlloc_HyperCall --
593
 
 *
594
 
 *      Helper to make a QueuePairAlloc hypercall.
595
 
 *
596
 
 * Results:
597
 
 *      Result of the hypercall.
598
 
 *
599
 
 * Side effects:
600
 
 *      Memory is allocated & freed.
601
 
 *
602
 
 *-----------------------------------------------------------------------------
603
 
 */
604
 
 
605
 
int
606
 
VMCIQueuePairAlloc_HyperCall(const QueuePairEntry *entry) // IN:
607
 
{
608
 
   VMCIQueuePairAllocMsg *allocMsg;
609
 
   size_t msgSize;
610
 
   int result;
611
 
 
612
 
   if (!entry || entry->numPPNs <= 2) {
613
 
      return VMCI_ERROR_INVALID_ARGS;
614
 
   }
615
 
 
616
 
   ASSERT(!(entry->flags & VMCI_QPFLAG_LOCAL));
617
 
 
618
 
   msgSize = sizeof *allocMsg + (size_t)entry->numPPNs * sizeof(PPN);
619
 
   allocMsg = VMCI_AllocKernelMem(msgSize, VMCI_MEMORY_NONPAGED);
620
 
   if (!allocMsg) {
621
 
      return VMCI_ERROR_NO_MEM;
622
 
   }
623
 
 
624
 
   allocMsg->hdr.dst = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
625
 
                                        VMCI_QUEUEPAIR_ALLOC);
626
 
   allocMsg->hdr.src = VMCI_ANON_SRC_HANDLE;
627
 
   allocMsg->hdr.payloadSize = msgSize - VMCI_DG_HEADERSIZE;
628
 
   allocMsg->handle = entry->handle;
629
 
   allocMsg->peer = entry->peer;
630
 
   allocMsg->flags = entry->flags;
631
 
   allocMsg->produceSize = entry->produceSize;
632
 
   allocMsg->consumeSize = entry->consumeSize;
633
 
   allocMsg->numPPNs = entry->numPPNs;
634
 
   result = VMCI_PopulatePPNList((uint8 *)allocMsg + sizeof *allocMsg, &entry->ppnSet);
635
 
   if (result == VMCI_SUCCESS) {
636
 
      result = VMCI_SendDatagram((VMCIDatagram *)allocMsg);
637
 
   }
638
 
   VMCI_FreeKernelMem(allocMsg, msgSize);
639
 
 
640
 
   return result;
641
 
}
642
 
 
643
 
 
644
 
/*
645
 
 *-----------------------------------------------------------------------------
646
 
 *
647
 
 * VMCIQueuePairAllocHelper --
648
 
 *
649
 
 *      Helper for VMCI QueuePairAlloc. Allocates physical pages for the
650
 
 *      QueuePair. Makes OS dependent calls through generic wrappers.
651
 
 *
652
 
 * Results:
653
 
 *      Success or failure.
654
 
 *
655
 
 * Side effects:
656
 
 *      Memory is allocated.
657
 
 *
658
 
 *-----------------------------------------------------------------------------
659
 
 */
660
 
 
661
 
static int
662
 
VMCIQueuePairAllocHelper(VMCIHandle *handle,   // IN/OUT:
663
 
                         VMCIQueue **produceQ, // OUT:
664
 
                         uint64 produceSize,   // IN:
665
 
                         VMCIQueue **consumeQ, // OUT:
666
 
                         uint64 consumeSize,   // IN:
667
 
                         VMCIId peer,          // IN:
668
 
                         uint32 flags)         // IN:
669
 
{
670
 
   const uint64 numProducePages = CEILING(produceSize, PAGE_SIZE) + 1;
671
 
   const uint64 numConsumePages = CEILING(consumeSize, PAGE_SIZE) + 1;
672
 
   void *myProduceQ = NULL;
673
 
   void *myConsumeQ = NULL;
674
 
   int result;
675
 
   QueuePairEntry *queuePairEntry = NULL;
676
 
 
677
 
   /*
678
 
    * XXX Check for possible overflow of 'size' arguments when passed to
679
 
    * compat_get_order (after some arithmetic ops).
680
 
    */
681
 
 
682
 
   ASSERT(handle && produceQ && consumeQ && (produceSize || consumeSize));
683
 
 
684
 
   QueuePairList_Lock();
685
 
 
686
 
   if ((queuePairEntry = QueuePairList_FindEntry(*handle))) {
687
 
      if (queuePairEntry->flags & VMCI_QPFLAG_LOCAL) {
688
 
         /* Local attach case. */
689
 
         if (queuePairEntry->refCount > 1) {
690
 
            VMCI_LOG((LGPFX "Error attempting to attach more than once.\n"));
691
 
            result = VMCI_ERROR_UNAVAILABLE;
692
 
            goto errorKeepEntry;
693
 
         }
694
 
 
695
 
         if (queuePairEntry->produceSize != consumeSize ||
696
 
             queuePairEntry->consumeSize != produceSize ||
697
 
             queuePairEntry->flags != (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
698
 
            VMCI_LOG((LGPFX "Error mismatched queue pair in local attach.\n"));
699
 
            result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
700
 
            goto errorKeepEntry;
701
 
         }
702
 
 
703
 
         /*
704
 
          * Do a local attach.  We swap the consume and produce queues for the
705
 
          * attacher and deliver an attach event.
706
 
          */
707
 
         result = QueuePairNotifyPeerLocal(TRUE, *handle);
708
 
         if (result < VMCI_SUCCESS) {
709
 
            goto errorKeepEntry;
710
 
         }
711
 
         myProduceQ = queuePairEntry->consumeQ;
712
 
         myConsumeQ = queuePairEntry->produceQ;
713
 
         goto out;
714
 
      }
715
 
      result = VMCI_ERROR_ALREADY_EXISTS;
716
 
      goto errorKeepEntry;
717
 
   }
718
 
 
719
 
   myProduceQ = VMCI_AllocQueue(produceSize);
720
 
   if (!myProduceQ) {
721
 
      VMCI_LOG((LGPFX "Error allocating pages for produce queue.\n"));
722
 
      result = VMCI_ERROR_NO_MEM;
723
 
      goto error;
724
 
   }
725
 
 
726
 
   myConsumeQ = VMCI_AllocQueue(consumeSize);
727
 
   if (!myConsumeQ) {
728
 
      VMCI_LOG((LGPFX "Error allocating pages for consume queue.\n"));
729
 
      result = VMCI_ERROR_NO_MEM;
730
 
      goto error;
731
 
   }
732
 
 
733
 
   queuePairEntry = QueuePairEntryCreate(*handle, peer, flags,
734
 
                                         produceSize, consumeSize,
735
 
                                         myProduceQ, myConsumeQ);
736
 
   if (!queuePairEntry) {
737
 
      VMCI_LOG((LGPFX "Error allocating memory in %s.\n", __FUNCTION__));
738
 
      result = VMCI_ERROR_NO_MEM;
739
 
      goto error;
740
 
   }
741
 
 
742
 
   result = VMCI_AllocPPNSet(myProduceQ, numProducePages, myConsumeQ,
743
 
                             numConsumePages, &queuePairEntry->ppnSet);
744
 
   if (result < VMCI_SUCCESS) {
745
 
      VMCI_LOG((LGPFX "VMCI_AllocPPNSet failed.\n"));
746
 
      goto error;
747
 
   }
748
 
 
749
 
   /*
750
 
    * It's only necessary to notify the host if this queue pair will be
751
 
    * attached to from another context.
752
 
    */
753
 
   if (queuePairEntry->flags & VMCI_QPFLAG_LOCAL) {
754
 
      /* Local create case. */
755
 
      VMCIId contextId = VMCI_GetContextID();
756
 
 
757
 
      /*
758
 
       * Enforce similar checks on local queue pairs as we do for regular ones.
759
 
       * The handle's context must match the creator or attacher context id
760
 
       * (here they are both the current context id) and the attach-only flag
761
 
       * cannot exist during create.  We also ensure specified peer is this
762
 
       * context or an invalid one.
763
 
       */
764
 
      if (queuePairEntry->handle.context != contextId ||
765
 
          (queuePairEntry->peer != VMCI_INVALID_ID &&
766
 
           queuePairEntry->peer != contextId)) {
767
 
         result = VMCI_ERROR_NO_ACCESS;
768
 
         goto error;
769
 
      }
770
 
 
771
 
      if (queuePairEntry->flags & VMCI_QPFLAG_ATTACH_ONLY) {
772
 
         result = VMCI_ERROR_NOT_FOUND;
773
 
         goto error;
774
 
      }
775
 
   } else {
776
 
      result = VMCIQueuePairAlloc_HyperCall(queuePairEntry);
777
 
      if (result < VMCI_SUCCESS) {
778
 
         VMCI_LOG((LGPFX "VMCIQueuePairAlloc_HyperCall result = %d.\n",
779
 
                   result));
780
 
         goto error;
781
 
      }
782
 
   }
783
 
 
784
 
   QueuePairList_AddEntry(queuePairEntry);
785
 
 
786
 
out:
787
 
   queuePairEntry->refCount++;
788
 
   *handle = queuePairEntry->handle;
789
 
   *produceQ = (VMCIQueue *)myProduceQ;
790
 
   *consumeQ = (VMCIQueue *)myConsumeQ;
791
 
 
792
 
   /*
793
 
    * We should initialize the queue pair header pages on a local queue pair
794
 
    * create.  For non-local queue pairs, the hypervisor initializes the header
795
 
    * pages in the create step.
796
 
    */
797
 
   if ((queuePairEntry->flags & VMCI_QPFLAG_LOCAL) &&
798
 
       queuePairEntry->refCount == 1) {
799
 
      VMCIQueueHeader_Init((*produceQ)->qHeader, *handle);
800
 
      VMCIQueueHeader_Init((*consumeQ)->qHeader, *handle);
801
 
   }
802
 
 
803
 
   QueuePairList_Unlock();
804
 
 
805
 
   return VMCI_SUCCESS;
806
 
 
807
 
error:
808
 
   QueuePairList_Unlock();
809
 
   if (queuePairEntry) {
810
 
      /* The queues will be freed inside the destroy routine. */
811
 
      QueuePairEntryDestroy(queuePairEntry);
812
 
   } else {
813
 
      if (myProduceQ) {
814
 
         VMCI_FreeQueue(myProduceQ, produceSize);
815
 
      }
816
 
      if (myConsumeQ) {
817
 
         VMCI_FreeQueue(myConsumeQ, consumeSize);
818
 
      }
819
 
   }
820
 
   return result;
821
 
 
822
 
errorKeepEntry:
823
 
   /* This path should only be used when an existing entry was found. */
824
 
   ASSERT(queuePairEntry->refCount > 0);
825
 
   QueuePairList_Unlock();
826
 
   return result;
827
 
}
828
 
 
829
 
 
830
 
/*
831
 
 *-----------------------------------------------------------------------------
832
 
 *
833
 
 * VMCIQueuePairDetachHelper --
834
 
 *
835
 
 *      Helper for VMCI QueuePair detach interface on Linux. Frees the physical
836
 
 *      pages for the QueuePair.
837
 
 *
838
 
 * Results:
839
 
 *      Success or failure.
840
 
 *
841
 
 * Side effects:
842
 
 *      Memory may be freed.
843
 
 *
844
 
 *-----------------------------------------------------------------------------
845
 
 */
846
 
 
847
 
static int
848
 
VMCIQueuePairDetachHelper(VMCIHandle handle)   // IN:
849
 
{
850
 
   int result;
851
 
   QueuePairEntry *entry;
852
 
   uint32 refCount;
853
 
 
854
 
   ASSERT(!VMCI_HANDLE_INVALID(handle));
855
 
 
856
 
   QueuePairList_Lock();
857
 
 
858
 
   entry = QueuePairList_FindEntry(handle);
859
 
   if (!entry) {
860
 
      result = VMCI_ERROR_NOT_FOUND;
861
 
      goto out;
862
 
   }
863
 
 
864
 
   ASSERT(entry->refCount >= 1);
865
 
 
866
 
   if (entry->flags & VMCI_QPFLAG_LOCAL) {
867
 
      result = VMCI_SUCCESS;
868
 
 
869
 
      if (entry->refCount > 1) {
870
 
         result = QueuePairNotifyPeerLocal(FALSE, handle);
871
 
         if (result < VMCI_SUCCESS) {
872
 
            goto out;
873
 
         }
874
 
      }
875
 
   } else {
876
 
      VMCIQueuePairDetachMsg detachMsg;
877
 
 
878
 
      detachMsg.hdr.dst = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
879
 
                                           VMCI_QUEUEPAIR_DETACH);
880
 
      detachMsg.hdr.src = VMCI_ANON_SRC_HANDLE;
881
 
      detachMsg.hdr.payloadSize = sizeof handle;
882
 
      detachMsg.handle = handle;
883
 
 
884
 
      result = VMCI_SendDatagram((VMCIDatagram *)&detachMsg);
885
 
   }
886
 
 
887
 
out:
888
 
   if (result >= VMCI_SUCCESS) {
889
 
      entry->refCount--;
890
 
 
891
 
      if (entry->refCount == 0) {
892
 
         QueuePairList_RemoveEntry(entry);
893
 
      }
894
 
   }
895
 
 
896
 
   /* If we didn't remove the entry, this could change once we unlock. */
897
 
   refCount = entry ? entry->refCount :
898
 
                      0xffffffff; /*
899
 
                                   * Value does not matter, silence the
900
 
                                   * compiler.
901
 
                                   */
902
 
 
903
 
   QueuePairList_Unlock();
904
 
 
905
 
   if (result >= VMCI_SUCCESS && refCount == 0) {
906
 
      QueuePairEntryDestroy(entry);
907
 
   }
908
 
   return result;
909
 
}
910
 
 
911
 
 
912
 
/*
913
 
 *----------------------------------------------------------------------------
914
 
 *
915
 
 * QueuePairNotifyPeerLocal --
916
 
 *
917
 
 *      Dispatches a queue pair event message directly into the local event
918
 
 *      queue.
919
 
 *
920
 
 * Results:
921
 
 *      VMCI_SUCCESS on success, error code otherwise
922
 
 *
923
 
 * Side effects:
924
 
 *      None.
925
 
 *
926
 
 *----------------------------------------------------------------------------
927
 
 */
928
 
 
929
 
static int
930
 
QueuePairNotifyPeerLocal(Bool attach,           // IN: attach or detach?
931
 
                         VMCIHandle handle)     // IN: queue pair handle
932
 
{
933
 
   VMCIEventMsg *eMsg;
934
 
   VMCIEventPayload_QP *ePayload;
935
 
   /* buf is only 48 bytes. */
936
 
   char buf[sizeof *eMsg + sizeof *ePayload];
937
 
   VMCIId contextId;
938
 
 
939
 
   contextId = VMCI_GetContextID();
940
 
 
941
 
   eMsg = (VMCIEventMsg *)buf;
942
 
   ePayload = VMCIEventMsgPayload(eMsg);
943
 
 
944
 
   eMsg->hdr.dst = VMCI_MAKE_HANDLE(contextId, VMCI_EVENT_HANDLER);
945
 
   eMsg->hdr.src = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
946
 
                                    VMCI_CONTEXT_RESOURCE_ID);
947
 
   eMsg->hdr.payloadSize = sizeof *eMsg + sizeof *ePayload - sizeof eMsg->hdr;
948
 
   eMsg->eventData.event = attach ? VMCI_EVENT_QP_PEER_ATTACH :
949
 
                                    VMCI_EVENT_QP_PEER_DETACH;
950
 
   ePayload->peerId = contextId;
951
 
   ePayload->handle = handle;
952
 
 
953
 
   return VMCIEvent_Dispatch((VMCIDatagram *)eMsg);
954
 
}