VirtualBox

source: vbox/trunk/src/VBox/GuestHost/HGSMI/HGSMICommon.cpp@ 48418

Last change on this file since 48418 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.4 KB
Line 
1/* $Id: HGSMICommon.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * VBox Host Guest Shared Memory Interface (HGSMI) - Functions common to both host and guest.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#define LOG_DISABLED /* Maybe we can enabled it all the time now? */
19#define LOG_GROUP LOG_GROUP_HGSMI
20#include <iprt/heap.h>
21#include <iprt/string.h>
22
23#include <VBox/HGSMI/HGSMI.h>
24#include <VBox/log.h>
25
26
27/* Channel flags. */
28#define HGSMI_CH_F_REGISTERED 0x01
29
30/* Assertions for situations which could happen and normally must be processed properly
31 * but must be investigated during development: guest misbehaving, etc.
32 */
33#ifdef HGSMI_STRICT
34#define HGSMI_STRICT_ASSERT_FAILED() AssertFailed()
35#define HGSMI_STRICT_ASSERT(expr) Assert(expr)
36#else
37#define HGSMI_STRICT_ASSERT_FAILED() do {} while (0)
38#define HGSMI_STRICT_ASSERT(expr) do {} while (0)
39#endif /* !HGSMI_STRICT */
40
41/* One-at-a-Time Hash from
42 * http://www.burtleburtle.net/bob/hash/doobs.html
43 *
44 * ub4 one_at_a_time(char *key, ub4 len)
45 * {
46 * ub4 hash, i;
47 * for (hash=0, i=0; i<len; ++i)
48 * {
49 * hash += key[i];
50 * hash += (hash << 10);
51 * hash ^= (hash >> 6);
52 * }
53 * hash += (hash << 3);
54 * hash ^= (hash >> 11);
55 * hash += (hash << 15);
56 * return hash;
57 * }
58 */
59
60static uint32_t hgsmiHashBegin (void)
61{
62 return 0;
63}
64
65static uint32_t hgsmiHashProcess (uint32_t hash,
66 const void *pvData,
67 size_t cbData)
68{
69 const uint8_t *pu8Data = (const uint8_t *)pvData;
70
71 while (cbData--)
72 {
73 hash += *pu8Data++;
74 hash += (hash << 10);
75 hash ^= (hash >> 6);
76 }
77
78 return hash;
79}
80
81static uint32_t hgsmiHashEnd (uint32_t hash)
82{
83 hash += (hash << 3);
84 hash ^= (hash >> 11);
85 hash += (hash << 15);
86
87 return hash;
88}
89
90uint32_t HGSMIChecksum (HGSMIOFFSET offBuffer,
91 const HGSMIBUFFERHEADER *pHeader,
92 const HGSMIBUFFERTAIL *pTail)
93{
94 uint32_t u32Checksum = hgsmiHashBegin ();
95
96 u32Checksum = hgsmiHashProcess (u32Checksum, &offBuffer, sizeof (offBuffer));
97 u32Checksum = hgsmiHashProcess (u32Checksum, pHeader, sizeof (HGSMIBUFFERHEADER));
98 u32Checksum = hgsmiHashProcess (u32Checksum, pTail, RT_OFFSETOF(HGSMIBUFFERTAIL, u32Checksum));
99
100 return hgsmiHashEnd (u32Checksum);
101}
102
103static HGSMIOFFSET hgsmiBufferInitializeSingle (const HGSMIAREA *pArea,
104 HGSMIBUFFERHEADER *pHeader,
105 uint32_t u32DataSize,
106 uint8_t u8Channel,
107 uint16_t u16ChannelInfo)
108{
109 if ( !pArea
110 || !pHeader)
111 {
112 return HGSMIOFFSET_VOID;
113 }
114
115 /* Buffer must be within the area:
116 * * header data size do not exceed the maximum data size;
117 * * buffer address is greater than the area base address;
118 * * buffer address is lower than the maximum allowed for the given data size.
119 */
120 HGSMISIZE cbMaximumDataSize = pArea->offLast - pArea->offBase;
121
122 if ( u32DataSize > cbMaximumDataSize
123 || (uint8_t *)pHeader < pArea->pu8Base
124 || (uint8_t *)pHeader > pArea->pu8Base + cbMaximumDataSize - u32DataSize)
125 {
126 return HGSMIOFFSET_VOID;
127 }
128
129 HGSMIOFFSET offBuffer = HGSMIPointerToOffset (pArea, pHeader);
130
131 pHeader->u8Flags = HGSMI_BUFFER_HEADER_F_SEQ_SINGLE;
132 pHeader->u32DataSize = u32DataSize;
133 pHeader->u8Channel = u8Channel;
134 pHeader->u16ChannelInfo = u16ChannelInfo;
135 memset (pHeader->u.au8Union, 0, sizeof (pHeader->u.au8Union));
136
137 HGSMIBUFFERTAIL *pTail = HGSMIBufferTail (pHeader);
138
139 pTail->u32Reserved = 0;
140 pTail->u32Checksum = HGSMIChecksum (offBuffer, pHeader, pTail);
141
142 return offBuffer;
143}
144
145int HGSMIAreaInitialize (HGSMIAREA *pArea, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase)
146{
147 uint8_t *pu8Base = (uint8_t *)pvBase;
148
149 if ( !pArea /* Check that the area: */
150 || cbArea < HGSMIBufferMinimumSize () /* Large enough. */
151 || pu8Base + cbArea < pu8Base /* No address space wrap. */
152 || offBase > UINT32_C(0xFFFFFFFF) - cbArea /* Area within the 32 bit space: offBase + cbMem <= 0xFFFFFFFF */
153 )
154 {
155 return VERR_INVALID_PARAMETER;
156 }
157
158 pArea->pu8Base = pu8Base;
159 pArea->offBase = offBase;
160 pArea->offLast = cbArea - HGSMIBufferMinimumSize () + offBase;
161 pArea->cbArea = cbArea;
162
163 return VINF_SUCCESS;
164}
165
166void HGSMIAreaClear (HGSMIAREA *pArea)
167{
168 if (pArea)
169 {
170 memset (pArea, 0, sizeof (HGSMIAREA));
171 }
172}
173
174/* Initialize the memory buffer including its checksum.
175 * No changes alloed to the header and the tail after that.
176 */
177HGSMIOFFSET HGSMIBufferInitializeSingle (const HGSMIAREA *pArea,
178 HGSMIBUFFERHEADER *pHeader,
179 HGSMISIZE cbBuffer,
180 uint8_t u8Channel,
181 uint16_t u16ChannelInfo)
182{
183 if (cbBuffer < HGSMIBufferMinimumSize ())
184 {
185 return HGSMIOFFSET_VOID;
186 }
187
188 return hgsmiBufferInitializeSingle (pArea, pHeader, cbBuffer - HGSMIBufferMinimumSize (), u8Channel, u16ChannelInfo);
189}
190
191void HGSMIHeapSetupUnitialized (HGSMIHEAP *pHeap)
192{
193 pHeap->u.hPtr = NIL_RTHEAPSIMPLE;
194 pHeap->cRefs = 0;
195 pHeap->area.cbArea = 0;
196 pHeap->area.offBase = HGSMIOFFSET_VOID;
197 pHeap->area.offLast = HGSMIOFFSET_VOID;
198 pHeap->area.pu8Base = 0;
199 pHeap->fOffsetBased = false;
200}
201
202bool HGSMIHeapIsItialized (HGSMIHEAP *pHeap)
203{
204 return pHeap->u.hPtr != NIL_RTHEAPSIMPLE;
205}
206
207int HGSMIHeapRelocate (HGSMIHEAP *pHeap,
208 void *pvBase,
209 uint32_t offHeapHandle,
210 uintptr_t offDelta,
211 HGSMISIZE cbArea,
212 HGSMIOFFSET offBase,
213 bool fOffsetBased
214 )
215{
216 if ( !pHeap
217 || !pvBase)
218 {
219 return VERR_INVALID_PARAMETER;
220 }
221
222 int rc = HGSMIAreaInitialize (&pHeap->area, pvBase, cbArea, offBase);
223
224 if (RT_SUCCESS (rc))
225 {
226 if (fOffsetBased)
227 pHeap->u.hOff = (RTHEAPOFFSET)((uint8_t *)pvBase + offHeapHandle);
228 else
229 {
230 pHeap->u.hPtr = (RTHEAPSIMPLE)((uint8_t *)pvBase + offHeapHandle);
231 rc = RTHeapSimpleRelocate (pHeap->u.hPtr, offDelta); AssertRC(rc);
232 }
233 if (RT_SUCCESS (rc))
234 {
235 pHeap->cRefs = 0;
236 pHeap->fOffsetBased = fOffsetBased;
237 }
238 else
239 {
240 HGSMIAreaClear (&pHeap->area);
241 }
242 }
243
244 return rc;
245}
246
247int HGSMIHeapSetup (HGSMIHEAP *pHeap,
248 void *pvBase,
249 HGSMISIZE cbArea,
250 HGSMIOFFSET offBase,
251 bool fOffsetBased)
252{
253 if ( !pHeap
254 || !pvBase)
255 {
256 return VERR_INVALID_PARAMETER;
257 }
258
259 int rc = HGSMIAreaInitialize (&pHeap->area, pvBase, cbArea, offBase);
260
261 if (RT_SUCCESS (rc))
262 {
263 if (!fOffsetBased)
264 rc = RTHeapSimpleInit (&pHeap->u.hPtr, pvBase, cbArea);
265 else
266 rc = RTHeapOffsetInit (&pHeap->u.hOff, pvBase, cbArea);
267
268 if (RT_SUCCESS (rc))
269 {
270 pHeap->cRefs = 0;
271 pHeap->fOffsetBased = fOffsetBased;
272 }
273 else
274 {
275 HGSMIAreaClear (&pHeap->area);
276 }
277 }
278
279 return rc;
280}
281
282void HGSMIHeapDestroy (HGSMIHEAP *pHeap)
283{
284 if (pHeap)
285 {
286 Assert(!pHeap->cRefs);
287 pHeap->u.hPtr = NIL_RTHEAPSIMPLE;
288 HGSMIAreaClear (&pHeap->area);
289 pHeap->cRefs = 0;
290 }
291}
292
293void *HGSMIHeapAlloc (HGSMIHEAP *pHeap,
294 HGSMISIZE cbData,
295 uint8_t u8Channel,
296 uint16_t u16ChannelInfo)
297{
298 if (pHeap->u.hPtr == NIL_RTHEAPSIMPLE)
299 {
300 return NULL;
301 }
302
303 size_t cbAlloc = HGSMIBufferRequiredSize (cbData);
304
305 HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIHeapBufferAlloc (pHeap, cbAlloc);
306 if (!pHeader)
307 return NULL;
308
309 hgsmiBufferInitializeSingle (&pHeap->area, pHeader, cbData, u8Channel, u16ChannelInfo);
310
311 return HGSMIBufferData (pHeader);
312}
313
314HGSMIOFFSET HGSMIHeapBufferOffset (HGSMIHEAP *pHeap,
315 void *pvData)
316{
317 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData);
318
319 HGSMIOFFSET offBuffer = HGSMIPointerToOffset (&pHeap->area, pHeader);
320
321 return offBuffer;
322}
323
324void HGSMIHeapFree (HGSMIHEAP *pHeap,
325 void *pvData)
326{
327 if ( pvData
328 && pHeap->u.hPtr != NIL_RTHEAPSIMPLE)
329 {
330 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData);
331
332 HGSMIHeapBufferFree (pHeap, pHeader);
333 }
334}
335
336void* HGSMIHeapBufferAlloc (HGSMIHEAP *pHeap, HGSMISIZE cbBuffer)
337{
338 void* pvBuf;
339 if (!pHeap->fOffsetBased)
340 pvBuf = RTHeapSimpleAlloc (pHeap->u.hPtr, cbBuffer, 0);
341 else
342 pvBuf = RTHeapOffsetAlloc (pHeap->u.hOff, cbBuffer, 0);
343
344 if (!pvBuf)
345 return NULL;
346
347 ++pHeap->cRefs;
348 return pvBuf;
349}
350
351void HGSMIHeapBufferFree(HGSMIHEAP *pHeap,
352 void *pvBuf)
353{
354 if (!pHeap->fOffsetBased)
355 RTHeapSimpleFree (pHeap->u.hPtr, pvBuf);
356 else
357 RTHeapOffsetFree (pHeap->u.hOff, pvBuf);
358
359 --pHeap->cRefs;
360}
361
362/* Verify that the given offBuffer points to a valid buffer, which is within the area.
363 */
364static const HGSMIBUFFERHEADER *hgsmiVerifyBuffer (const HGSMIAREA *pArea,
365 HGSMIOFFSET offBuffer)
366{
367 AssertPtr(pArea);
368
369 LogFlowFunc(("buffer 0x%x, area %p %x [0x%x;0x%x]\n", offBuffer, pArea->pu8Base, pArea->cbArea, pArea->offBase, pArea->offLast));
370
371 if ( offBuffer < pArea->offBase
372 || offBuffer > pArea->offLast)
373 {
374 LogFunc(("offset 0x%x is outside the area [0x%x;0x%x]!!!\n", offBuffer, pArea->offBase, pArea->offLast));
375 HGSMI_STRICT_ASSERT_FAILED();
376 return NULL;
377 }
378
379 const HGSMIBUFFERHEADER *pHeader = HGSMIOffsetToPointer (pArea, offBuffer);
380
381 /* Quick check of the data size, it should be less than the maximum
382 * data size for the buffer at this offset.
383 */
384 LogFlowFunc(("datasize check: pHeader->u32DataSize = 0x%x pArea->offLast - offBuffer = 0x%x\n", pHeader->u32DataSize, pArea->offLast - offBuffer));
385 if (pHeader->u32DataSize <= pArea->offLast - offBuffer)
386 {
387 HGSMIBUFFERTAIL *pTail = HGSMIBufferTail (pHeader);
388
389 /* At least both pHeader and pTail structures are in the area. Check the checksum. */
390 uint32_t u32Checksum = HGSMIChecksum (offBuffer, pHeader, pTail);
391
392 LogFlowFunc(("checksum check: u32Checksum = 0x%x pTail->u32Checksum = 0x%x\n", u32Checksum, pTail->u32Checksum));
393 if (u32Checksum == pTail->u32Checksum)
394 {
395 LogFlowFunc(("returning %p\n", pHeader));
396 return pHeader;
397 }
398 else
399 {
400 LogFunc(("invalid checksum 0x%x, expected 0x%x!!!\n", u32Checksum, pTail->u32Checksum));
401 HGSMI_STRICT_ASSERT_FAILED();
402 }
403 }
404 else
405 {
406 LogFunc(("invalid data size 0x%x, maximum is 0x%x!!!\n", pHeader->u32DataSize, pArea->offLast - offBuffer));
407 HGSMI_STRICT_ASSERT_FAILED();
408 }
409
410 LogFlowFunc(("returning NULL\n"));
411 return NULL;
412}
413
414/* A wrapper to safely call the handler.
415 */
416int HGSMIChannelHandlerCall (const HGSMICHANNELHANDLER *pHandler,
417 const HGSMIBUFFERHEADER *pHeader)
418{
419 LogFlowFunc(("pHandler %p, pHeader %p\n", pHandler, pHeader));
420
421 int rc;
422
423 Assert(pHandler && pHandler->pfnHandler);
424
425 if ( pHandler
426 && pHandler->pfnHandler)
427 {
428 void *pvBuffer = HGSMIBufferData (pHeader);
429 HGSMISIZE cbBuffer = pHeader->u32DataSize;
430
431 rc = pHandler->pfnHandler (pHandler->pvHandler, pHeader->u16ChannelInfo, pvBuffer, cbBuffer);
432 }
433 else
434 {
435 /* It is a NOOP case here. */
436 rc = VINF_SUCCESS;
437 }
438
439 LogFlowFunc(("leave rc = %Rrc\n", rc));
440
441 return rc;
442}
443
444/*
445 * Process a guest buffer.
446 * @thread EMT
447 */
448static int hgsmiBufferProcess (const HGSMICHANNEL *pChannel,
449 const HGSMIBUFFERHEADER *pHeader)
450{
451 LogFlowFunc(("pChannel %p, pHeader %p\n", pChannel, pHeader));
452
453 int rc = HGSMIChannelHandlerCall (&pChannel->handler,
454 pHeader);
455
456 return rc;
457}
458
459HGSMICHANNEL *HGSMIChannelFindById (HGSMICHANNELINFO * pChannelInfo,
460 uint8_t u8Channel)
461{
462 HGSMICHANNEL *pChannel = &pChannelInfo->Channels[u8Channel];
463
464 if (pChannel->u8Flags & HGSMI_CH_F_REGISTERED)
465 {
466 return pChannel;
467 }
468
469 return NULL;
470}
471
472int HGSMIBufferProcess (HGSMIAREA *pArea,
473 HGSMICHANNELINFO * pChannelInfo,
474 HGSMIOFFSET offBuffer)
475{
476 LogFlowFunc(("pArea %p, offBuffer 0x%x\n", pArea, offBuffer));
477
478 AssertPtr(pArea);
479 AssertPtr(pChannelInfo);
480
481 int rc = VERR_GENERAL_FAILURE;
482
483// VM_ASSERT_EMT(pIns->pVM);
484
485 /* Guest has prepared a command description at 'offBuffer'. */
486 const HGSMIBUFFERHEADER *pHeader = hgsmiVerifyBuffer (pArea, offBuffer);
487 Assert(pHeader);
488 if (pHeader)
489 {
490 /* Pass the command to the appropriate handler registered with this instance.
491 * Start with the handler list head, which is the preallocated HGSMI setup channel.
492 */
493 HGSMICHANNEL *pChannel = HGSMIChannelFindById (pChannelInfo, pHeader->u8Channel);
494 Assert(pChannel);
495 if (pChannel)
496 {
497 hgsmiBufferProcess (pChannel, pHeader);
498 HGSMI_STRICT_ASSERT(hgsmiVerifyBuffer (pArea, offBuffer) != NULL);
499 rc = VINF_SUCCESS;
500 }
501 else
502 {
503 rc = VERR_INVALID_FUNCTION;
504 }
505 }
506 else
507 {
508 rc = VERR_INVALID_HANDLE;
509// LogRel(("HGSMI[%s]: ignored invalid guest buffer 0x%08X!!!\n", pIns->pszName, offBuffer));
510 }
511 return rc;
512}
513
514/* Register a new VBVA channel by index.
515 *
516 */
517int HGSMIChannelRegister (HGSMICHANNELINFO * pChannelInfo,
518 uint8_t u8Channel,
519 const char *pszName,
520 PFNHGSMICHANNELHANDLER pfnChannelHandler,
521 void *pvChannelHandler,
522 HGSMICHANNELHANDLER *pOldHandler)
523{
524 AssertPtrReturn(pOldHandler, VERR_INVALID_PARAMETER);
525
526 /* Check whether the channel is already registered. */
527 HGSMICHANNEL *pChannel = HGSMIChannelFindById (pChannelInfo, u8Channel);
528
529 if (!pChannel)
530 {
531 /* Channel is not yet registered. */
532 pChannel = &pChannelInfo->Channels[u8Channel];
533
534 pChannel->u8Flags = HGSMI_CH_F_REGISTERED;
535 pChannel->u8Channel = u8Channel;
536
537 pChannel->handler.pfnHandler = NULL;
538 pChannel->handler.pvHandler = NULL;
539
540 pChannel->pszName = pszName;
541 }
542
543 *pOldHandler = pChannel->handler;
544
545 pChannel->handler.pfnHandler = pfnChannelHandler;
546 pChannel->handler.pvHandler = pvChannelHandler;
547
548 return VINF_SUCCESS;
549}
550
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette