VirtualBox

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

Last change on this file since 40257 was 38736, checked in by vboxsync, 13 years ago

*: Please don NOT redefine logger macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 KB
Line 
1/* $Id: HGSMICommon.cpp 38736 2011-09-13 13:58:47Z 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-2011 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;
306 if (!pHeap->fOffsetBased)
307 pHeader = (HGSMIBUFFERHEADER *)RTHeapSimpleAlloc (pHeap->u.hPtr, cbAlloc, 0);
308 else
309 pHeader = (HGSMIBUFFERHEADER *)RTHeapOffsetAlloc (pHeap->u.hOff, cbAlloc, 0);
310
311 if (!pHeader)
312 {
313 return NULL;
314 }
315
316 ++pHeap->cRefs;
317
318 hgsmiBufferInitializeSingle (&pHeap->area, pHeader, cbData, u8Channel, u16ChannelInfo);
319
320 return HGSMIBufferData (pHeader);
321}
322
323HGSMIOFFSET HGSMIHeapBufferOffset (HGSMIHEAP *pHeap,
324 void *pvData)
325{
326 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData);
327
328 HGSMIOFFSET offBuffer = HGSMIPointerToOffset (&pHeap->area, pHeader);
329
330 return offBuffer;
331}
332
333void HGSMIHeapFree (HGSMIHEAP *pHeap,
334 void *pvData)
335{
336 if ( pvData
337 && pHeap->u.hPtr != NIL_RTHEAPSIMPLE)
338 {
339 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData);
340
341 if (!pHeap->fOffsetBased)
342 RTHeapSimpleFree (pHeap->u.hPtr, pHeader);
343 else
344 RTHeapOffsetFree (pHeap->u.hOff, pHeader);
345
346 --pHeap->cRefs;
347 }
348}
349
350/* Verify that the given offBuffer points to a valid buffer, which is within the area.
351 */
352static const HGSMIBUFFERHEADER *hgsmiVerifyBuffer (const HGSMIAREA *pArea,
353 HGSMIOFFSET offBuffer)
354{
355 AssertPtr(pArea);
356
357 LogFlowFunc(("buffer 0x%x, area %p %x [0x%x;0x%x]\n", offBuffer, pArea->pu8Base, pArea->cbArea, pArea->offBase, pArea->offLast));
358
359 if ( offBuffer < pArea->offBase
360 || offBuffer > pArea->offLast)
361 {
362 LogFunc(("offset 0x%x is outside the area [0x%x;0x%x]!!!\n", offBuffer, pArea->offBase, pArea->offLast));
363 HGSMI_STRICT_ASSERT_FAILED();
364 return NULL;
365 }
366
367 const HGSMIBUFFERHEADER *pHeader = HGSMIOffsetToPointer (pArea, offBuffer);
368
369 /* Quick check of the data size, it should be less than the maximum
370 * data size for the buffer at this offset.
371 */
372 LogFlowFunc(("datasize check: pHeader->u32DataSize = 0x%x pArea->offLast - offBuffer = 0x%x\n", pHeader->u32DataSize, pArea->offLast - offBuffer));
373 if (pHeader->u32DataSize <= pArea->offLast - offBuffer)
374 {
375 HGSMIBUFFERTAIL *pTail = HGSMIBufferTail (pHeader);
376
377 /* At least both pHeader and pTail structures are in the area. Check the checksum. */
378 uint32_t u32Checksum = HGSMIChecksum (offBuffer, pHeader, pTail);
379
380 LogFlowFunc(("checksum check: u32Checksum = 0x%x pTail->u32Checksum = 0x%x\n", u32Checksum, pTail->u32Checksum));
381 if (u32Checksum == pTail->u32Checksum)
382 {
383 LogFlowFunc(("returning %p\n", pHeader));
384 return pHeader;
385 }
386 else
387 {
388 LogFunc(("invalid checksum 0x%x, expected 0x%x!!!\n", u32Checksum, pTail->u32Checksum));
389 HGSMI_STRICT_ASSERT_FAILED();
390 }
391 }
392 else
393 {
394 LogFunc(("invalid data size 0x%x, maximum is 0x%x!!!\n", pHeader->u32DataSize, pArea->offLast - offBuffer));
395 HGSMI_STRICT_ASSERT_FAILED();
396 }
397
398 LogFlowFunc(("returning NULL\n"));
399 return NULL;
400}
401
402/* A wrapper to safely call the handler.
403 */
404int HGSMIChannelHandlerCall (const HGSMICHANNELHANDLER *pHandler,
405 const HGSMIBUFFERHEADER *pHeader)
406{
407 LogFlowFunc(("pHandler %p, pHeader %p\n", pHandler, pHeader));
408
409 int rc;
410
411 Assert(pHandler && pHandler->pfnHandler);
412
413 if ( pHandler
414 && pHandler->pfnHandler)
415 {
416 void *pvBuffer = HGSMIBufferData (pHeader);
417 HGSMISIZE cbBuffer = pHeader->u32DataSize;
418
419 rc = pHandler->pfnHandler (pHandler->pvHandler, pHeader->u16ChannelInfo, pvBuffer, cbBuffer);
420 }
421 else
422 {
423 /* It is a NOOP case here. */
424 rc = VINF_SUCCESS;
425 }
426
427 LogFlowFunc(("leave rc = %Rrc\n", rc));
428
429 return rc;
430}
431
432/*
433 * Process a guest buffer.
434 * @thread EMT
435 */
436static int hgsmiBufferProcess (const HGSMICHANNEL *pChannel,
437 const HGSMIBUFFERHEADER *pHeader)
438{
439 LogFlowFunc(("pChannel %p, pHeader %p\n", pChannel, pHeader));
440
441 int rc = HGSMIChannelHandlerCall (&pChannel->handler,
442 pHeader);
443
444 return rc;
445}
446
447HGSMICHANNEL *HGSMIChannelFindById (HGSMICHANNELINFO * pChannelInfo,
448 uint8_t u8Channel)
449{
450 HGSMICHANNEL *pChannel = &pChannelInfo->Channels[u8Channel];
451
452 if (pChannel->u8Flags & HGSMI_CH_F_REGISTERED)
453 {
454 return pChannel;
455 }
456
457 return NULL;
458}
459
460int HGSMIBufferProcess (HGSMIAREA *pArea,
461 HGSMICHANNELINFO * pChannelInfo,
462 HGSMIOFFSET offBuffer)
463{
464 LogFlowFunc(("pArea %p, offBuffer 0x%x\n", pArea, offBuffer));
465
466 AssertPtr(pArea);
467 AssertPtr(pChannelInfo);
468
469 int rc = VERR_GENERAL_FAILURE;
470
471// VM_ASSERT_EMT(pIns->pVM);
472
473 /* Guest has prepared a command description at 'offBuffer'. */
474 const HGSMIBUFFERHEADER *pHeader = hgsmiVerifyBuffer (pArea, offBuffer);
475 Assert(pHeader);
476 if (pHeader)
477 {
478 /* Pass the command to the appropriate handler registered with this instance.
479 * Start with the handler list head, which is the preallocated HGSMI setup channel.
480 */
481 HGSMICHANNEL *pChannel = HGSMIChannelFindById (pChannelInfo, pHeader->u8Channel);
482 Assert(pChannel);
483 if (pChannel)
484 {
485 hgsmiBufferProcess (pChannel, pHeader);
486 HGSMI_STRICT_ASSERT(hgsmiVerifyBuffer (pArea, offBuffer) != NULL);
487 rc = VINF_SUCCESS;
488 }
489 else
490 {
491 rc = VERR_INVALID_FUNCTION;
492 }
493 }
494 else
495 {
496 rc = VERR_INVALID_HANDLE;
497// LogRel(("HGSMI[%s]: ignored invalid guest buffer 0x%08X!!!\n", pIns->pszName, offBuffer));
498 }
499 return rc;
500}
501
502/* Register a new VBVA channel by index.
503 *
504 */
505int HGSMIChannelRegister (HGSMICHANNELINFO * pChannelInfo,
506 uint8_t u8Channel,
507 const char *pszName,
508 PFNHGSMICHANNELHANDLER pfnChannelHandler,
509 void *pvChannelHandler,
510 HGSMICHANNELHANDLER *pOldHandler)
511{
512 AssertPtrReturn(pOldHandler, VERR_INVALID_PARAMETER);
513
514 /* Check whether the channel is already registered. */
515 HGSMICHANNEL *pChannel = HGSMIChannelFindById (pChannelInfo, u8Channel);
516
517 if (!pChannel)
518 {
519 /* Channel is not yet registered. */
520 pChannel = &pChannelInfo->Channels[u8Channel];
521
522 pChannel->u8Flags = HGSMI_CH_F_REGISTERED;
523 pChannel->u8Channel = u8Channel;
524
525 pChannel->handler.pfnHandler = NULL;
526 pChannel->handler.pvHandler = NULL;
527
528 pChannel->pszName = pszName;
529 }
530
531 *pOldHandler = pChannel->handler;
532
533 pChannel->handler.pfnHandler = pfnChannelHandler;
534 pChannel->handler.pvHandler = pvChannelHandler;
535
536 return VINF_SUCCESS;
537}
538
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