aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mvp/mvpkm/qp_common.c
blob: 8d121a1b765346ae8b565169bcf159c5b4e9c0c4 (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
/*
 * 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 Queue Pairs common enqueue and dequeue functions.
 *  Does not include Attach(), and Detach(), as this will be specific
 *  to host/guest
 *  implementations.
 */

#include <linux/module.h>

#include "mvp_types.h"
#include "comm_os.h"
#include "qp.h"


/**
 *  @brief Calculate free space in the queue, convenience function
 *  @param head queue head offset
 *  @param tail  queue tail offset
 *  @param queueSize size of queue
 *  @return free space in the queue
 */
static inline int32
FreeSpace(uint32 head, uint32 tail, uint32 queueSize) {
   /* Leave 1 byte free to resolve ambiguity between empty
    * and full conditions */
   return (tail >= head) ? (queueSize - (tail - head) - 1) :
                           (head - tail - 1);
}


/**
 *  @brief Returns available space for enqueue, in bytes
 *  @param qp handle to the queue pair
 *  @return available space in bytes in the queue for enqueue operations,
 *      QP_ERROR_INVALID_HANDLE if the handle is malformed
 */
int32
QP_EnqueueSpace(QPHandle *qp)
{
   uint32 head;
   uint32 phantom;
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   head    = qp->produceQ->head;
   phantom = qp->produceQ->phantom_tail;

   if (head    >= qp->queueSize ||
       phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   return FreeSpace(head, phantom, qp->queueSize);
}


/**
 *  @brief Enqueues a segment of data into the producer queue
 *  @param qp handle to the queue pair
 *  @param buf data to enqueue
 *  @param bufSize size in bytes to enqueue
 *  @return number of bytes enqueued on success, appropriate error
 *      code otherwise
 *  @sideeffects May move phantom tail pointer
 */
int32
QP_EnqueueSegment(QPHandle *qp, const void *buf, size_t bufSize)
{
   int32 freeSpace;
   uint32 head;
   uint32 phantom;

   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   head = qp->produceQ->head;
   phantom = qp->produceQ->phantom_tail;

   /*
    * This check must go after the assignment above,
    * otherwise a malicious guest could write bogus
    * offsets to the queue and cause the memcpy to
    * copy into unpleasant places.
    */
   if (head    >= qp->queueSize ||
       phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   freeSpace = FreeSpace(head, phantom, qp->queueSize);

   if (bufSize <= freeSpace) {
      if (bufSize + phantom < qp->queueSize) {
         memcpy(qp->produceQ->data + phantom, buf, bufSize);
         phantom += bufSize;
      } else {
         uint32 written = qp->queueSize - phantom;
         memcpy(qp->produceQ->data + phantom, buf, written);
         memcpy(qp->produceQ->data, (uint8*)buf + written, bufSize - written);
         phantom = bufSize - written;
      }
   } else {
      return QP_ERROR_NO_MEM;
   }

   qp->produceQ->phantom_tail = phantom;

   return bufSize;
}


/**
 *  @brief Commits any previous EnqueueSegment operations to the queue
 *         pair
 *  @param qp handle to the queue pair.
 *  @return QP_SUCCESS on success, appropriate error code otherwise.
 *  @sideeffects May move tail pointer
 */
int32
QP_EnqueueCommit(QPHandle *qp)
{
   uint32 phantom;
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   phantom = qp->produceQ->phantom_tail;
   if (phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   qp->produceQ->tail = phantom;
   return QP_SUCCESS;
}


/**
 *  @brief Returns any available bytes for dequeue
 *  @param qp handle to the queue pair
 *  @return available bytes for dequeue, appropriate error code
 *      otherwise
 */
int32
QP_DequeueSpace(QPHandle *qp)
{
   uint32 tail;
   uint32 phantom;
   int32 bytesAvailable;

   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   tail    = qp->consumeQ->tail;
   phantom = qp->consumeQ->phantom_head;

   if (tail    >= qp->queueSize ||
       phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   bytesAvailable = (tail - phantom);
   if ((int32)bytesAvailable < 0) {
      bytesAvailable += qp->queueSize;
   }
   return bytesAvailable;
}


/**
 *  @brief Dequeues a segment of data from the consumer queue into
 *      a buffer
 *  @param qp handle to the queue pair
 *  @param[out] buf buffer to copy to
 *  @param bytesDesired number of bytes to dequeue
 *  @return number of bytes dequeued on success, appropriate error
 *      code otherwise
 *  @sideeffects May move phantom head pointer
 */
int32
QP_DequeueSegment(QPHandle *qp, const void *buf, size_t bytesDesired)
{
   uint32 tail;
   uint32 phantom;
   int32 bytesAvailable = 0;

   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   tail = qp->consumeQ->tail;
   phantom = qp->consumeQ->phantom_head;

   /*
    * This check must go after the assignment above,
    * otherwise a malicious guest could write bogus
    * offsets to the queue and cause the memcpy to
    * copy into unpleasant places.
    */
   if (tail    >= qp->queueSize  ||
       phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   bytesAvailable = (tail - phantom);
   if ((int32)bytesAvailable < 0) {
      bytesAvailable += qp->queueSize;
   }

   if (bytesDesired <= bytesAvailable) {
      if (bytesDesired + phantom < qp->queueSize) {
         memcpy((void*)buf, qp->consumeQ->data + phantom, bytesDesired);
         phantom += bytesDesired;
      } else {
         uint32 written = qp->queueSize - phantom;
         memcpy((void*)buf, qp->consumeQ->data + phantom, written);
         memcpy((uint8*)buf + written, qp->consumeQ->data, bytesDesired - written);
         phantom = bytesDesired - written;
      }
   } else {
      return QP_ERROR_NO_MEM;
   }

   qp->consumeQ->phantom_head  = phantom;

   return bytesDesired;
}


/**
 *  @brief Commits any previous DequeueSegment operations to the queue
 *      pair
 *  @param qp handle to the queue pair
 *  @return QP_SUCCESS on success, QP_ERROR_INVALID_HANDLE if the handle
 *      is malformed
 *  @sideeffects Moves the head pointer
 */
int32
QP_DequeueCommit(QPHandle *qp)
{
   uint32 phantom;
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   phantom = qp->consumeQ->phantom_head;
   if (phantom >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   qp->consumeQ->head = phantom;
   return QP_SUCCESS;
}


/**
 *  @brief Resets the phantom tail pointer and discards any pending
 *      enqueues
 *  @param qp handle to the queue pair
 *  @return QP_SUCCESS on success, QP_ERROR_INVALID_HANDLE if the handle
 *      is malformed
 *  @sideeffects Resets the phantom tail pointer
 */
int32
QP_EnqueueReset(QPHandle *qp)
{
   uint32 tail;
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   tail = qp->produceQ->tail;
   if (tail >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   qp->produceQ->phantom_tail = tail;
   return QP_SUCCESS;
}

/**
 *  @brief Resets the phantom head pointer and discards any pending
 *      dequeues
 *  @param qp handle to the queue pair
 *  @return QP_SUCCESS on success, QP_ERROR_INVALID_HANDLE if the handle
 *      is malformed
 *  @sideeffects Resets the phantom head pointer
 */
int32
QP_DequeueReset(QPHandle *qp)
{
   uint32 head;
   if (!QP_CheckHandle(qp)) {
      return QP_ERROR_INVALID_HANDLE;
   }

   head = qp->consumeQ->head;
   if (head >= qp->queueSize) {
      return QP_ERROR_INVALID_HANDLE;
   }

   qp->consumeQ->phantom_head = head;
   return QP_SUCCESS;
}

EXPORT_SYMBOL(QP_EnqueueSpace);
EXPORT_SYMBOL(QP_EnqueueSegment);
EXPORT_SYMBOL(QP_EnqueueCommit);
EXPORT_SYMBOL(QP_DequeueSpace);
EXPORT_SYMBOL(QP_DequeueSegment);
EXPORT_SYMBOL(QP_DequeueCommit);
EXPORT_SYMBOL(QP_EnqueueReset);
EXPORT_SYMBOL(QP_DequeueReset);