VirtualBox

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

Last change on this file since 29502 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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