aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mvp/mvpkm/qp_host_kernel.c
blob: c53f315b8f281d1fa68bc033c7a43f38986afd24 (plain)
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
/*
 * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
 *
 * Copyright (C) 2010-2012 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 version 2 as published by
 * the Free Software Foundation.
 *
 * 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; see the file COPYING.  If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#line 5

/**
 *  @file
 *
 *  @brief MVP host kernel implementation of the queue pairs API
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/highmem.h>
#include <linux/slab.h>

#include "mvp.h"
#include "mvpkm_kernel.h"
#include "qp.h"
#include "qp_host_kernel.h"

static QPHandle   queuePairs[QP_MAX_QUEUE_PAIRS];
static QPListener listeners[QP_MAX_LISTENERS];

/*
 * Protect listeners and queuePairs.
 */
static DEFINE_MUTEX(qpLock);

#define QPLock()    mutex_lock(&qpLock)
#define QPUnlock()  mutex_unlock(&qpLock)

/**
 * @brief Map a vector of pages into virtually contiguous kernel space
 * @param vm this vm's vm struct
 * @param base base machine page number that lists pages to map
 * @param nrPages number of pages to map
 * @param[out] qp handle to qp to set up
 * @param[out] hkva virtual address mapping
 * @return QP_SUCCESS on success, error code otherwise. Mapped address
 *      is returned in hkva
 */

static int32
MapPages(MvpkmVM *vm,
         MPN base,
         uint32 nrPages,
         QPHandle *qp,
         HKVA *hkva)
{
   HKVA *va;
   uint32 i;
   uint32 rc;
   struct page *basepfn = pfn_to_page(base);
   struct page **pages;

   BUG_ON(!vm); // this would be very bad.

   if (!hkva) {
      return QP_ERROR_INVALID_ARGS;
   }

   pages = kmalloc(nrPages * sizeof (MPN), GFP_KERNEL);
   if (!pages) {
      return QP_ERROR_NO_MEM;
   }

   /*
    * Map in the first page, read out the MPN vector
    */
   down_write(&vm->lockedSem);
   va = kmap(basepfn);
   if (!va) {
      rc = QP_ERROR_INVALID_ARGS;
      kfree(pages);
      qp->pages = NULL;
      goto out;
   }

   /*
    * Grab references and translate MPNs->PFNs
    */
   for (i = 0; i < nrPages; i++) {
      pages[i] = pfn_to_page(((MPN*)va)[i]);
      get_page(pages[i]);
   }

   /*
    * Clean up the first mapping and remap the entire vector
    */
   kunmap(basepfn);
   va = vmap(pages, nrPages, VM_MAP, PAGE_KERNEL);
   if (!va) {
      rc = QP_ERROR_NO_MEM;
      for (i = 0; i < nrPages; i++) {
         put_page(pages[i]);
      }
      kfree(pages);
      qp->pages = NULL;
      goto out;
   } else {
      *hkva = (HKVA)va;
      qp->pages = pages;
   }

   /*
    * Let's not leak mpns..
    */
   memset(va, 0x0, nrPages * PAGE_SIZE);

   rc = QP_SUCCESS;

out:
   up_write(&vm->lockedSem);
   return rc;
}

/**
 * @brief Initialize all free queue pair entries and listeners
 */

void
QP_HostInit(void)
{
   uint32 i;

   for (i = 0; i < QP_MAX_QUEUE_PAIRS; i++) {
      QP_MakeInvalidQPHandle(&queuePairs[i]);
   }

   for (i = 0; i < QP_MAX_LISTENERS; i++) {
      listeners[i] = NULL;
   }
}


/**
 * @brief Detaches a guest from a queue pair and notifies
 *      any registered listeners through the detach callback
 * @param id id that guest requested a detach from, detaches all
 *      queue pairs associated with a VM if the resource id == QP_INVALID_ID
 * @return QP_SUCCESS on success, appropriate error code otherwise
 */

int32
QP_GuestDetachRequest(QPId id)
{
   QPHandle *qp;
   uint32 i;

   if (id.resource >= QP_MAX_ID && id.resource != QP_INVALID_ID) {
      return QP_ERROR_INVALID_ARGS;
   }

   QPLock();

   /*
    * Invalidate all queue pairs associated with this VM if
    * resource == QP_INVALID_ID
    */
   if (id.resource == QP_INVALID_ID) {
      for (i = 0; i < QP_MAX_QUEUE_PAIRS; i++) {
         qp = &queuePairs[i];
         if (qp->id.context == id.context && qp->peerDetachCB) {
            qp->peerDetachCB(qp->detachData);
         }
      }
   } else {
      qp = &queuePairs[id.resource];
      if (qp->peerDetachCB) {
         qp->peerDetachCB(qp->detachData);
      }
  }

   QPUnlock();

   return QP_SUCCESS;
}


/**
 * @brief Attaches a guest to shared memory region
 * @param vm guest to attach
 * @param args queue pair args structure:
 *      - args->id: id of the region to attach to, if id.resource == QP_INVALID_ID, then
 *              an id is assigned
 *      - args->capacity: total size of the region in bytes
 *      - args->type: type of queue pair (e.g PVTCP)
 * @param base base machine page number that lists pages to map
 * @param nrPages number of pages to map
 * @return QP_SUCCESS on success, appropriate error code otherwise.
 */

int32
QP_GuestAttachRequest(MvpkmVM *vm,
                      QPInitArgs *args,
                      MPN base,
                      uint32 nrPages)
{
   int32 rc;
   HKVA hkva = 0;
   QPHandle *qp;
   uint32 i;

   if ((!QP_CheckArgs(args))                              ||
       (vm->wsp->guestId != (Mksck_VmId)args->id.context) ||
       (args->capacity != (nrPages * PAGE_SIZE))) {
      return QP_ERROR_INVALID_ARGS;
   }

   QP_DBG("%s: Guest requested attach to [%u:%u] capacity: %u type: %x base: %x nrPages: %u\n",
          __FUNCTION__,
          args->id.context,
          args->id.resource,
          args->capacity,
          args->type,
          base,
          nrPages);

   QPLock();

   /*
    * Assign a resource id if id == QP_INVALID_ID
    */
   if (args->id.resource == QP_INVALID_ID) {
      for (i = 0; i < QP_MAX_QUEUE_PAIRS; i++) {
         if (queuePairs[i].state == QP_STATE_FREE) {
            args->id.resource = i;
            QP_DBG("%s: Guest requested anonymous region, assigning resource id %u\n",
                   __FUNCTION__, args->id.resource);
            goto found;
         }
      }

      rc = QP_ERROR_NO_MEM;
      goto out;
   }

found:
   qp = queuePairs + args->id.resource;

   if (qp->state != QP_STATE_FREE) {
      rc = QP_ERROR_ALREADY_ATTACHED;
      goto out;
   }

   /*
    * Brand new queue pair, allocate some memory to back it and
    * initialize the entry
    */
   rc = MapPages(vm, base, nrPages, qp, &hkva);
   if (rc != QP_SUCCESS) {
      goto out;
   }

   /* NB: reversed from the guest  */
   qp->id              = args->id;
   qp->capacity        = args->capacity;
   qp->produceQ        = (QHandle*)hkva;
   qp->consumeQ        = (QHandle*)(hkva + args->capacity/2);
   qp->queueSize       = args->capacity/2 - sizeof(QHandle);
   qp->type            = args->type;
   qp->state           = QP_STATE_GUEST_ATTACHED;

   /*
    * The qp is now assumed to be well-formed
    */
   QP_DBG("%s: Guest attached to region [%u:%u] capacity: %u HKVA: %x\n",
          __FUNCTION__,
          args->id.context,
          args->id.resource,
          args->capacity,
          (uint32)hkva);
   rc = QP_SUCCESS;

out:
   QPUnlock();
   if (rc != QP_SUCCESS) {
      QP_DBG("%s: Failed to attach: %u\n", __FUNCTION__, rc);
   }
   return rc;
}


/**
 * @brief Attaches the host to the shared memory region. The guest
 * MUST have allocated the shmem region already or else this will fail.
 * @param args structure with the shared memory region id to attach to,
 *      total size of the region in bytes, and type of queue pair (e.g PVTCP)
 * @param[in, out] qp handle to the queue pair to return
 * @return QP_SUCCESS on success, appropriate error code otherwise
 */

int32
QP_Attach(QPInitArgs *args,
          QPHandle** qp)
{
   uint32 rc;

   if (!qp || !QP_CheckArgs(args)) {
      return QP_ERROR_INVALID_ARGS;
   }

   QP_DBG("%s: Attaching to id: [%u:%u] capacity: %u\n",
          __FUNCTION__,
          args->id.context,
          args->id.resource,
          args->capacity);

   QPLock();
   *qp = queuePairs + args->id.resource;

   if (!QP_CheckHandle(*qp)) {
      *qp = NULL;
      rc = QP_ERROR_INVALID_HANDLE;
      goto out;
   }

   if ((*qp)->state == QP_STATE_CONNECTED) {
      rc = QP_ERROR_ALREADY_ATTACHED;
      goto out;
   }

   if ((*qp)->state != QP_STATE_GUEST_ATTACHED) {
      rc = QP_ERROR_INVALID_HANDLE;
      goto out;
   }

   (*qp)->state = QP_STATE_CONNECTED;

   QP_DBG("%s: Attached!\n", __FUNCTION__);
   rc = QP_SUCCESS;

out:
   QPUnlock();
   return rc;
}

/**
 * @brief Detaches the host to the shared memory region.
 * @param[in, out] qp handle to the queue pair
 * @return QP_SUCCESS on success, appropriate error code otherwise
 * @sideeffects Frees memory
 */

int32
QP_Detach(QPHandle* qp)
{
   uint32 rc;
   uint32 i;

   QPLock();
   if (!QP_CheckHandle(qp)) {
      rc = QP_ERROR_INVALID_HANDLE;
      goto out;
   }

   QP_DBG("%s: Freeing queue pair [%u:%u]\n",
          __FUNCTION__,
          qp->id.context,
          qp->id.resource);

   BUG_ON(!qp->produceQ);
   BUG_ON(!qp->pages);
   BUG_ON((qp->state != QP_STATE_CONNECTED) &&
          (qp->state != QP_STATE_GUEST_ATTACHED));

   vunmap(qp->produceQ);

   for (i = 0; i < qp->capacity/PAGE_SIZE; i++) {
      put_page(qp->pages[i]);
   }
   kfree(qp->pages);

   QP_DBG("%s: Host detached from [%u:%u]\n",
          __FUNCTION__,
          qp->id.context,
          qp->id.resource);

   QP_MakeInvalidQPHandle(qp);
   rc = QP_SUCCESS;

out:
   QPUnlock();
   return rc;
}


/**
 * @brief Detaches and destroys all queue pairs associated with a given guest
 * @param vmID which VM to clean up
 * @sideeffects Destroys all queue pairs for guest vmID
 */

void QP_DetachAll(Mksck_VmId vmID) {
   QPId id = {
      .context = (uint32)vmID,
      .resource = QP_INVALID_ID
   };

   QP_DBG("%s: Detaching all queue pairs from vmId context %u\n", __FUNCTION__, vmID);
   QP_GuestDetachRequest(id);
}

/**
 * @brief Registers a listener into the queue pair system. Callbacks are
 *      called with interrupts disabled and must not sleep.
 * @param listener listener to be called
 * @return QP_SUCCESS on success, QP_ERROR_NO_MEM if no more
 *         listeners can be registered
 */

int32
QP_RegisterListener(const QPListener listener)
{
   uint32 i;
   int32 rc = QP_ERROR_NO_MEM;

   QPLock();
   for (i = 0; i < QP_MAX_LISTENERS; i++) {
      if (!listeners[i]) {
         listeners[i] = listener;
         QP_DBG("%s: Registered listener\n", __FUNCTION__);
         rc = QP_SUCCESS;
         break;
      }
   }
   QPUnlock();

   return rc;
}


/**
 * @brief Unregister a listener service from the queue pair system.
 * @param listener listener to unregister
 * @return QP_SUCCESS on success, appropriate error code otherwise
 */

int32
QP_UnregisterListener(const QPListener listener)
{
   uint32 i;
   int32 rc = QP_ERROR_INVALID_HANDLE;

   QPLock();
   for (i = 0; i < QP_MAX_LISTENERS; i++) {
      if (listeners[i] == listener) {
         listeners[i] = NULL;
         QP_DBG("%s: Unregistered listener\n", __FUNCTION__);
         rc = QP_SUCCESS;
         break;
      }
   }
   QPUnlock();

   return rc;
}


/**
 * @brief Registers a callback to be called when the guest detaches
 *      from a queue pair. Callbacks are called with interrupts and
 *      must not sleep.
 * @param qp handle to the queue pair
 * @param callback callback to be called
 * @param data data to deliver to the callback
 * @return QP_SUCCESS on success, appropriate error code otherwise
 */

int32
QP_RegisterDetachCB(QPHandle *qp,
                    void (*callback)(void*),
                    void *data)
{
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   if (!callback) {
      return QP_ERROR_INVALID_ARGS;
   }

   qp->peerDetachCB   = callback;
   qp->detachData = data;
   QP_DBG("%s: Registered detach callback\n", __FUNCTION__);
   return QP_SUCCESS;
}


/**
 * @brief Noop on the host, only guests can initiate a notify
 * @param args noop
 * @return QP_SUCCESS
 */


int32 QP_Notify(QPInitArgs *args) {
   return QP_SUCCESS;
}


/**
 * @brief Notify any registered listeners for the given queue pair
 * @param args initialization arguments used by the guest
 * @return QP_SUCCESS on success, error otherwise
 */

int32 QP_NotifyListener(QPInitArgs *args) {
   int32 i;
   QPHandle *qp = NULL;

   if (!QP_CheckArgs(args)) {
      return QP_ERROR_INVALID_ARGS;
   }

   /*
    * Iterate over listeners until one of them reports they handled it
    */
   QPLock();
   for (i = 0; i < QP_MAX_LISTENERS; i++) {
      if (listeners[i]) {
         QP_DBG("Delivering attach event to listener...\n");
         if (listeners[i](args) == QP_SUCCESS) {
            break;
         }
      }
   }

   if (i == QP_MAX_LISTENERS) {
      /*
       * No listener successfully probed this QP.
       * The guest DETACH HVC isn't implemented; we need compensate for it
       * by deallocating the QP here.
       * This is a workaround which assumes, more-or-less correctly, that
       * unsuccessful QP probes never lead to subsequent host-attaching.
       */

      qp = &queuePairs[args->id.resource];
   }

   QPUnlock();

   if (qp) {
      QP_Detach(qp);
   }
   return QP_SUCCESS;
}


EXPORT_SYMBOL(QP_Attach);
EXPORT_SYMBOL(QP_Detach);
EXPORT_SYMBOL(QP_RegisterListener);
EXPORT_SYMBOL(QP_UnregisterListener);
EXPORT_SYMBOL(QP_RegisterDetachCB);
EXPORT_SYMBOL(QP_Notify);