VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp@ 28501

Last change on this file since 28501 was 26642, checked in by vboxsync, 15 years ago

Additions/VBoxGuestLib: failure case logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Revision: 26642 $ */
2/** @file
3 * VBoxGuestLibR0 - Physical memory heap.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#include "VBGLInternal.h"
32
33#include <iprt/assert.h>
34#include <iprt/semaphore.h>
35#include <iprt/alloc.h>
36
37/* Physical memory heap consists of double linked list
38 * of chunks. Memory blocks are allocated inside these chunks
39 * and are members of Allocated and Free double linked lists.
40 *
41 * When allocating a block, we search in Free linked
42 * list for a suitable free block. If there is no such block,
43 * a new chunk is allocated and the new block is taken from
44 * the new chunk as the only chunk-sized free block.
45 * Allocated block is excluded from the Free list and goes to
46 * Alloc list.
47 *
48 * When freeing block, we check the pointer and then
49 * exclude block from Alloc list and move it to free list.
50 *
51 * For each chunk we maintain the allocated blocks counter.
52 * if 2 (or more) entire chunks are free they are immediately
53 * deallocated, so we always have at most 1 free chunk.
54 *
55 * When freeing blocks, two subsequent free blocks are always
56 * merged together. Current implementation merges blocks only
57 * when there is a block after the just freed one.
58 *
59 */
60
61#define VBGL_PH_ASSERT Assert
62#define VBGL_PH_ASSERTMsg AssertMsg
63
64// #define DUMPHEAP
65
66#ifdef DUMPHEAP
67# define VBGL_PH_dprintf(a) RTAssertMsg2Weak a
68#else
69# define VBGL_PH_dprintf(a)
70#endif
71
72/* Heap block signature */
73#define VBGL_PH_BLOCKSIGNATURE (0xADDBBBBB)
74
75
76/* Heap chunk signarure */
77#define VBGL_PH_CHUNKSIGNATURE (0xADDCCCCC)
78/* Heap chunk allocation unit */
79#define VBGL_PH_CHUNKSIZE (0x10000)
80
81/* Heap block bit flags */
82#define VBGL_PH_BF_ALLOCATED (0x1)
83
84struct _VBGLPHYSHEAPBLOCK
85{
86 uint32_t u32Signature;
87
88 /* Size of user data in the block. Does not include the block header. */
89 uint32_t cbDataSize;
90
91 uint32_t fu32Flags;
92
93 struct _VBGLPHYSHEAPBLOCK *pNext;
94 struct _VBGLPHYSHEAPBLOCK *pPrev;
95
96 struct _VBGLPHYSHEAPCHUNK *pChunk;
97};
98
99struct _VBGLPHYSHEAPCHUNK
100{
101 uint32_t u32Signature;
102
103 /* Size of the chunk. Includes the chunk header. */
104 uint32_t cbSize;
105
106 /* Physical address of the chunk */
107 RTCCPHYS physAddr;
108
109 /* Number of allocated blocks in the chunk */
110 int32_t cAllocatedBlocks;
111
112 struct _VBGLPHYSHEAPCHUNK *pNext;
113 struct _VBGLPHYSHEAPCHUNK *pPrev;
114};
115
116
117#ifndef DUMPHEAP
118#define dumpheap(a)
119#else
120void dumpheap (char *point)
121{
122 VBGL_PH_dprintf(("VBGL_PH dump at '%s'\n", point));
123
124 VBGL_PH_dprintf(("Chunks:\n"));
125
126 VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead;
127
128 while (pChunk)
129 {
130 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, allocated = %8d, phys = %08X\n",
131 pChunk, pChunk->pNext, pChunk->pPrev, pChunk->u32Signature, pChunk->cbSize, pChunk->cAllocatedBlocks, pChunk->physAddr));
132
133 pChunk = pChunk->pNext;
134 }
135
136 VBGL_PH_dprintf(("Allocated blocks:\n"));
137
138 VBGLPHYSHEAPBLOCK *pBlock = g_vbgldata.pAllocBlocksHead;
139
140 while (pBlock)
141 {
142 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
143 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->u32Signature, pBlock->cbDataSize, pBlock->fu32Flags, pBlock->pChunk));
144
145 pBlock = pBlock->pNext;
146 }
147
148 VBGL_PH_dprintf(("Free blocks:\n"));
149
150 pBlock = g_vbgldata.pFreeBlocksHead;
151
152 while (pBlock)
153 {
154 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
155 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->u32Signature, pBlock->cbDataSize, pBlock->fu32Flags, pBlock->pChunk));
156
157 pBlock = pBlock->pNext;
158 }
159
160 VBGL_PH_dprintf(("VBGL_PH dump at '%s' done\n", point));
161}
162#endif
163
164
165DECLINLINE(void *) vbglPhysHeapBlock2Data (VBGLPHYSHEAPBLOCK *pBlock)
166{
167 return (void *)(pBlock? (char *)pBlock + sizeof (VBGLPHYSHEAPBLOCK): NULL);
168}
169
170DECLINLINE(VBGLPHYSHEAPBLOCK *) vbglPhysHeapData2Block (void *p)
171{
172 VBGLPHYSHEAPBLOCK *pBlock = (VBGLPHYSHEAPBLOCK *)(p? (char *)p - sizeof (VBGLPHYSHEAPBLOCK): NULL);
173
174 VBGL_PH_ASSERTMsg(pBlock == NULL || pBlock->u32Signature == VBGL_PH_BLOCKSIGNATURE,
175 ("pBlock->u32Signature = %08X\n", pBlock->u32Signature));
176
177 return pBlock;
178}
179
180DECLINLINE(int) vbglPhysHeapEnter (void)
181{
182 int rc = RTSemFastMutexRequest(g_vbgldata.mutexHeap);
183
184 VBGL_PH_ASSERTMsg(RT_SUCCESS(rc),
185 ("Failed to request heap mutex, rc = %Rrc\n", rc));
186
187 return rc;
188}
189
190DECLINLINE(void) vbglPhysHeapLeave (void)
191{
192 RTSemFastMutexRelease(g_vbgldata.mutexHeap);
193}
194
195
196static void vbglPhysHeapInitBlock (VBGLPHYSHEAPBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cbDataSize)
197{
198 VBGL_PH_ASSERT(pBlock != NULL);
199 VBGL_PH_ASSERT(pChunk != NULL);
200
201 pBlock->u32Signature = VBGL_PH_BLOCKSIGNATURE;
202 pBlock->cbDataSize = cbDataSize;
203 pBlock->fu32Flags = 0;
204 pBlock->pNext = NULL;
205 pBlock->pPrev = NULL;
206 pBlock->pChunk = pChunk;
207}
208
209
210static void vbglPhysHeapInsertBlock (VBGLPHYSHEAPBLOCK *pInsertAfter, VBGLPHYSHEAPBLOCK *pBlock)
211{
212 VBGL_PH_ASSERTMsg(pBlock->pNext == NULL,
213 ("pBlock->pNext = %p\n", pBlock->pNext));
214 VBGL_PH_ASSERTMsg(pBlock->pPrev == NULL,
215 ("pBlock->pPrev = %p\n", pBlock->pPrev));
216
217 if (pInsertAfter)
218 {
219 pBlock->pNext = pInsertAfter->pNext;
220 pBlock->pPrev = pInsertAfter;
221
222 if (pInsertAfter->pNext)
223 {
224 pInsertAfter->pNext->pPrev = pBlock;
225 }
226
227 pInsertAfter->pNext = pBlock;
228 }
229 else
230 {
231 /* inserting to head of list */
232 pBlock->pPrev = NULL;
233
234 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
235 {
236 pBlock->pNext = g_vbgldata.pAllocBlocksHead;
237
238 if (g_vbgldata.pAllocBlocksHead)
239 {
240 g_vbgldata.pAllocBlocksHead->pPrev = pBlock;
241 }
242
243 g_vbgldata.pAllocBlocksHead = pBlock;
244 }
245 else
246 {
247 pBlock->pNext = g_vbgldata.pFreeBlocksHead;
248
249 if (g_vbgldata.pFreeBlocksHead)
250 {
251 g_vbgldata.pFreeBlocksHead->pPrev = pBlock;
252 }
253
254 g_vbgldata.pFreeBlocksHead = pBlock;
255 }
256 }
257}
258
259static void vbglPhysHeapExcludeBlock (VBGLPHYSHEAPBLOCK *pBlock)
260{
261 if (pBlock->pNext)
262 {
263 pBlock->pNext->pPrev = pBlock->pPrev;
264 }
265 else
266 {
267 /* this is tail of list but we do not maintain tails of block lists.
268 * so do nothing.
269 */
270 ;
271 }
272
273 if (pBlock->pPrev)
274 {
275 pBlock->pPrev->pNext = pBlock->pNext;
276 }
277 else
278 {
279 /* this is head of list but we do not maintain tails of block lists. */
280 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
281 {
282 g_vbgldata.pAllocBlocksHead = pBlock->pNext;
283 }
284 else
285 {
286 g_vbgldata.pFreeBlocksHead = pBlock->pNext;
287 }
288 }
289
290 pBlock->pNext = NULL;
291 pBlock->pPrev = NULL;
292}
293
294static VBGLPHYSHEAPBLOCK *vbglPhysHeapChunkAlloc (uint32_t cbSize)
295{
296 RTCCPHYS physAddr;
297 VBGLPHYSHEAPCHUNK *pChunk;
298 VBGLPHYSHEAPBLOCK *pBlock;
299 VBGL_PH_dprintf(("Allocating new chunk of size %d\n", cbSize));
300
301 /* Compute chunk size to allocate */
302 if (cbSize < VBGL_PH_CHUNKSIZE)
303 {
304 /* Includes case of block size 0 during initialization */
305 cbSize = VBGL_PH_CHUNKSIZE;
306 }
307 else
308 {
309 /* Round up to next chunk size, which must be power of 2 */
310 cbSize = (cbSize + (VBGL_PH_CHUNKSIZE - 1)) & ~(VBGL_PH_CHUNKSIZE - 1);
311 }
312
313 physAddr = 0;
314 /* This function allocates physical contiguous memory (below 4GB) according to the IPRT docs.
315 * Address < 4G is required for the port IO.
316 */
317 pChunk = (VBGLPHYSHEAPCHUNK *)RTMemContAlloc (&physAddr, cbSize);
318
319 if (!pChunk)
320 {
321 LogRel(("vbglPhysHeapChunkAlloc: failed to alloc %u contiguous bytes.\n", cbSize));
322 return NULL;
323 }
324
325 pChunk->u32Signature = VBGL_PH_CHUNKSIGNATURE;
326 pChunk->cbSize = cbSize;
327 pChunk->physAddr = physAddr;
328 pChunk->cAllocatedBlocks = 0;
329 pChunk->pNext = g_vbgldata.pChunkHead;
330 pChunk->pPrev = NULL;
331
332 /* Initialize the free block, which now occupies entire chunk. */
333 pBlock = (VBGLPHYSHEAPBLOCK *)((char *)pChunk + sizeof (VBGLPHYSHEAPCHUNK));
334
335 vbglPhysHeapInitBlock (pBlock, pChunk, cbSize - sizeof (VBGLPHYSHEAPCHUNK) - sizeof (VBGLPHYSHEAPBLOCK));
336
337 vbglPhysHeapInsertBlock (NULL, pBlock);
338
339 g_vbgldata.pChunkHead = pChunk;
340
341 VBGL_PH_dprintf(("Allocated chunk %p, block = %p size=%x\n", pChunk, pBlock, cbSize));
342
343 return pBlock;
344}
345
346
347void vbglPhysHeapChunkDelete (VBGLPHYSHEAPCHUNK *pChunk)
348{
349 char *p;
350 VBGL_PH_ASSERT(pChunk != NULL);
351 VBGL_PH_ASSERTMsg(pChunk->u32Signature == VBGL_PH_CHUNKSIGNATURE,
352 ("pChunk->u32Signature = %08X\n", pChunk->u32Signature));
353
354 VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cbSize));
355
356 /* first scan the chunk and exclude all blocks from lists */
357
358 p = (char *)pChunk + sizeof (VBGLPHYSHEAPCHUNK);
359
360 while (p < (char *)pChunk + pChunk->cbSize)
361 {
362 VBGLPHYSHEAPBLOCK *pBlock = (VBGLPHYSHEAPBLOCK *)p;
363
364 p += pBlock->cbDataSize + sizeof (VBGLPHYSHEAPBLOCK);
365
366 vbglPhysHeapExcludeBlock (pBlock);
367 }
368
369 VBGL_PH_ASSERTMsg(p == (char *)pChunk + pChunk->cbSize,
370 ("p = %p, (char *)pChunk + pChunk->cbSize = %p, pChunk->cbSize = %08X\n",
371 p, (char *)pChunk + pChunk->cbSize, pChunk->cbSize));
372
373 /* Exclude chunk from the chunk list */
374 if (pChunk->pNext)
375 {
376 pChunk->pNext->pPrev = pChunk->pPrev;
377 }
378 else
379 {
380 /* we do not maintain tail */
381 ;
382 }
383
384 if (pChunk->pPrev)
385 {
386 pChunk->pPrev->pNext = pChunk->pNext;
387 }
388 else
389 {
390 /* the chunk was head */
391 g_vbgldata.pChunkHead = pChunk->pNext;
392 }
393
394 RTMemContFree (pChunk, pChunk->cbSize);
395}
396
397
398DECLVBGL(void *) VbglPhysHeapAlloc (uint32_t cbSize)
399{
400 VBGLPHYSHEAPBLOCK *pBlock, *iter;
401 int rc = vbglPhysHeapEnter ();
402
403 if (RT_FAILURE(rc))
404 return NULL;
405
406 dumpheap ("pre alloc");
407
408 pBlock = NULL;
409
410 /* If there are free blocks in the heap, look at them. */
411 iter = g_vbgldata.pFreeBlocksHead;
412
413 /* There will be not many blocks in the heap, so
414 * linear search would be fast enough.
415 */
416
417 while (iter)
418 {
419 if (iter->cbDataSize == cbSize)
420 {
421 /* exact match */
422 pBlock = iter;
423 break;
424 }
425
426 /* Looking for a free block with nearest size */
427 if (iter->cbDataSize > cbSize)
428 {
429 if (pBlock)
430 {
431 if (iter->cbDataSize < pBlock->cbDataSize)
432 {
433 pBlock = iter;
434 }
435 }
436 else
437 {
438 pBlock = iter;
439 }
440 }
441
442 iter = iter->pNext;
443 }
444
445 if (!pBlock)
446 {
447 /* No free blocks, allocate a new chunk,
448 * the only free block of the chunk will
449 * be returned.
450 */
451 pBlock = vbglPhysHeapChunkAlloc (cbSize);
452 }
453
454 if (pBlock)
455 {
456 VBGL_PH_ASSERTMsg(pBlock->u32Signature == VBGL_PH_BLOCKSIGNATURE,
457 ("pBlock = %p, pBlock->u32Signature = %08X\n", pBlock, pBlock->u32Signature));
458 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) == 0,
459 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
460
461 /* We have a free block, either found or allocated. */
462
463 if (pBlock->cbDataSize > 2*(cbSize + sizeof (VBGLPHYSHEAPBLOCK)))
464 {
465 /* Data will occupy less than a half of the block,
466 * the block should be split.
467 */
468 iter = (VBGLPHYSHEAPBLOCK *)((char *)pBlock + sizeof (VBGLPHYSHEAPBLOCK) + cbSize);
469
470 /* Init the new 'iter' block, initialized blocks are always marked as free. */
471 vbglPhysHeapInitBlock (iter, pBlock->pChunk, pBlock->cbDataSize - cbSize - sizeof (VBGLPHYSHEAPBLOCK));
472
473 pBlock->cbDataSize = cbSize;
474
475 /* Insert the new 'iter' block after the 'pBlock' in the free list */
476 vbglPhysHeapInsertBlock (pBlock, iter);
477 }
478
479 /* Exclude pBlock from free list */
480 vbglPhysHeapExcludeBlock (pBlock);
481
482 /* Mark as allocated */
483 pBlock->fu32Flags |= VBGL_PH_BF_ALLOCATED;
484
485 /* Insert to allocated list */
486 vbglPhysHeapInsertBlock (NULL, pBlock);
487
488 /* Adjust the chunk allocated blocks counter */
489 pBlock->pChunk->cAllocatedBlocks++;
490 }
491
492 dumpheap ("post alloc");
493
494 vbglPhysHeapLeave ();
495 VBGL_PH_dprintf(("VbglPhysHeapAlloc %x size %x\n", vbglPhysHeapBlock2Data (pBlock), pBlock->cbDataSize));
496
497 return vbglPhysHeapBlock2Data (pBlock);
498}
499
500DECLVBGL(RTCCPHYS) VbglPhysHeapGetPhysAddr (void *p)
501{
502 RTCCPHYS physAddr = 0;
503 VBGLPHYSHEAPBLOCK *pBlock = vbglPhysHeapData2Block (p);
504
505 if (pBlock)
506 {
507 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) != 0,
508 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
509
510 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
511 physAddr = pBlock->pChunk->physAddr + ((char *)p - (char *)pBlock->pChunk);
512 }
513
514 return physAddr;
515}
516
517DECLVBGL(void) VbglPhysHeapFree (void *p)
518{
519 VBGLPHYSHEAPBLOCK *pBlock;
520 VBGLPHYSHEAPBLOCK *pNeighbour;
521
522 int rc = vbglPhysHeapEnter ();
523 if (RT_FAILURE(rc))
524 return;
525
526 dumpheap ("pre free");
527
528 pBlock = vbglPhysHeapData2Block (p);
529
530 if (!pBlock)
531 {
532 vbglPhysHeapLeave ();
533 return;
534 }
535
536 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) != 0,
537 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
538
539 /* Exclude from allocated list */
540 vbglPhysHeapExcludeBlock (pBlock);
541
542 dumpheap ("post exclude");
543
544 VBGL_PH_dprintf(("VbglPhysHeapFree %x size %x\n", p, pBlock->cbDataSize));
545
546 /* Mark as free */
547 pBlock->fu32Flags &= ~VBGL_PH_BF_ALLOCATED;
548
549 /* Insert to free list */
550 vbglPhysHeapInsertBlock (NULL, pBlock);
551
552 dumpheap ("post insert");
553
554 /* Adjust the chunk allocated blocks counter */
555 pBlock->pChunk->cAllocatedBlocks--;
556
557 VBGL_PH_ASSERT(pBlock->pChunk->cAllocatedBlocks >= 0);
558
559 /* Check if we can merge 2 free blocks. To simplify heap maintenance,
560 * we will look at block after the just freed one.
561 * This will not prevent us from detecting free memory chunks.
562 * Also in most cases blocks are deallocated in reverse allocation order
563 * and in that case the merging will work.
564 */
565
566 pNeighbour = (VBGLPHYSHEAPBLOCK *)((char *)p + pBlock->cbDataSize);
567
568 if ((char *)pNeighbour < (char *)pBlock->pChunk + pBlock->pChunk->cbSize
569 && (pNeighbour->fu32Flags & VBGL_PH_BF_ALLOCATED) == 0)
570 {
571 /* The next block is free as well. */
572
573 /* Adjust size of current memory block */
574 pBlock->cbDataSize += pNeighbour->cbDataSize + sizeof (VBGLPHYSHEAPBLOCK);
575
576 /* Exclude the next neighbour */
577 vbglPhysHeapExcludeBlock (pNeighbour);
578 }
579
580 dumpheap ("post merge");
581
582 /* now check if there are 2 or more free chunks */
583 if (pBlock->pChunk->cAllocatedBlocks == 0)
584 {
585 VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead;
586
587 uint32_t u32FreeChunks = 0;
588
589 while (pChunk)
590 {
591 if (pChunk->cAllocatedBlocks == 0)
592 {
593 u32FreeChunks++;
594 }
595
596 pChunk = pChunk->pNext;
597 }
598
599 if (u32FreeChunks > 1)
600 {
601 /* Delete current chunk, it will also exclude all free blocks
602 * remaining in the chunk from the free list, so the pBlock
603 * will also be invalid after this.
604 */
605 vbglPhysHeapChunkDelete (pBlock->pChunk);
606 }
607 }
608
609 dumpheap ("post free");
610
611 vbglPhysHeapLeave ();
612}
613
614DECLVBGL(int) VbglPhysHeapInit (void)
615{
616 int rc = VINF_SUCCESS;
617
618 /* Allocate the first chunk of the heap. */
619 VBGLPHYSHEAPBLOCK *pBlock = vbglPhysHeapChunkAlloc (0);
620
621 if (!pBlock)
622 rc = VERR_NO_MEMORY;
623
624 RTSemFastMutexCreate(&g_vbgldata.mutexHeap);
625
626 return rc;
627}
628
629DECLVBGL(void) VbglPhysHeapTerminate (void)
630{
631 while (g_vbgldata.pChunkHead)
632 {
633 vbglPhysHeapChunkDelete (g_vbgldata.pChunkHead);
634 }
635
636 RTSemFastMutexDestroy(g_vbgldata.mutexHeap);
637}
638
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