]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - ubuntu/vbox/vboxguest/VBoxGuestR0LibPhysHeap.c
1 /* $Id: VBoxGuestR0LibPhysHeap.cpp $ */
3 * VBoxGuestLibR0 - Physical memory heap.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
27 /*********************************************************************************************************************************
29 *********************************************************************************************************************************/
30 #include "VBoxGuestR0LibInternal.h"
32 #include <iprt/assert.h>
33 #include <iprt/semaphore.h>
34 #include <iprt/alloc.h>
36 /* Physical memory heap consists of double linked list
37 * of chunks. Memory blocks are allocated inside these chunks
38 * and are members of Allocated and Free double linked lists.
40 * When allocating a block, we search in Free linked
41 * list for a suitable free block. If there is no such block,
42 * a new chunk is allocated and the new block is taken from
43 * the new chunk as the only chunk-sized free block.
44 * Allocated block is excluded from the Free list and goes to
47 * When freeing block, we check the pointer and then
48 * exclude block from Alloc list and move it to free list.
50 * For each chunk we maintain the allocated blocks counter.
51 * if 2 (or more) entire chunks are free they are immediately
52 * deallocated, so we always have at most 1 free chunk.
54 * When freeing blocks, two subsequent free blocks are always
55 * merged together. Current implementation merges blocks only
56 * when there is a block after the just freed one.
60 #define VBGL_PH_ASSERT Assert
61 #define VBGL_PH_ASSERTMsg AssertMsg
66 # define VBGL_PH_dprintf(a) RTAssertMsg2Weak a
68 # define VBGL_PH_dprintf(a)
71 /* Heap block signature */
72 #define VBGL_PH_BLOCKSIGNATURE (0xADDBBBBB)
75 /* Heap chunk signature */
76 #define VBGL_PH_CHUNKSIGNATURE (0xADDCCCCC)
77 /* Heap chunk allocation unit */
78 #define VBGL_PH_CHUNKSIZE (0x10000)
80 /* Heap block bit flags */
81 #define VBGL_PH_BF_ALLOCATED (0x1)
83 struct _VBGLPHYSHEAPBLOCK
85 uint32_t u32Signature
;
87 /* Size of user data in the block. Does not include the block header. */
92 struct _VBGLPHYSHEAPBLOCK
*pNext
;
93 struct _VBGLPHYSHEAPBLOCK
*pPrev
;
95 struct _VBGLPHYSHEAPCHUNK
*pChunk
;
98 struct _VBGLPHYSHEAPCHUNK
100 uint32_t u32Signature
;
102 /* Size of the chunk. Includes the chunk header. */
105 /* Physical address of the chunk */
108 /* Number of allocated blocks in the chunk */
109 int32_t cAllocatedBlocks
;
111 struct _VBGLPHYSHEAPCHUNK
*pNext
;
112 struct _VBGLPHYSHEAPCHUNK
*pPrev
;
119 void dumpheap (char *point
)
121 VBGL_PH_dprintf(("VBGL_PH dump at '%s'\n", point
));
123 VBGL_PH_dprintf(("Chunks:\n"));
125 VBGLPHYSHEAPCHUNK
*pChunk
= g_vbgldata
.pChunkHead
;
129 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, allocated = %8d, phys = %08X\n",
130 pChunk
, pChunk
->pNext
, pChunk
->pPrev
, pChunk
->u32Signature
, pChunk
->cbSize
, pChunk
->cAllocatedBlocks
, pChunk
->physAddr
));
132 pChunk
= pChunk
->pNext
;
135 VBGL_PH_dprintf(("Allocated blocks:\n"));
137 VBGLPHYSHEAPBLOCK
*pBlock
= g_vbgldata
.pAllocBlocksHead
;
141 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
142 pBlock
, pBlock
->pNext
, pBlock
->pPrev
, pBlock
->u32Signature
, pBlock
->cbDataSize
, pBlock
->fu32Flags
, pBlock
->pChunk
));
144 pBlock
= pBlock
->pNext
;
147 VBGL_PH_dprintf(("Free blocks:\n"));
149 pBlock
= g_vbgldata
.pFreeBlocksHead
;
153 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
154 pBlock
, pBlock
->pNext
, pBlock
->pPrev
, pBlock
->u32Signature
, pBlock
->cbDataSize
, pBlock
->fu32Flags
, pBlock
->pChunk
));
156 pBlock
= pBlock
->pNext
;
159 VBGL_PH_dprintf(("VBGL_PH dump at '%s' done\n", point
));
164 DECLINLINE(void *) vbglPhysHeapBlock2Data (VBGLPHYSHEAPBLOCK
*pBlock
)
166 return (void *)(pBlock
? (char *)pBlock
+ sizeof (VBGLPHYSHEAPBLOCK
): NULL
);
169 DECLINLINE(VBGLPHYSHEAPBLOCK
*) vbglPhysHeapData2Block (void *p
)
171 VBGLPHYSHEAPBLOCK
*pBlock
= (VBGLPHYSHEAPBLOCK
*)(p
? (char *)p
- sizeof (VBGLPHYSHEAPBLOCK
): NULL
);
173 VBGL_PH_ASSERTMsg(pBlock
== NULL
|| pBlock
->u32Signature
== VBGL_PH_BLOCKSIGNATURE
,
174 ("pBlock->u32Signature = %08X\n", pBlock
->u32Signature
));
179 DECLINLINE(int) vbglPhysHeapEnter (void)
181 int rc
= RTSemFastMutexRequest(g_vbgldata
.mutexHeap
);
183 VBGL_PH_ASSERTMsg(RT_SUCCESS(rc
),
184 ("Failed to request heap mutex, rc = %Rrc\n", rc
));
189 DECLINLINE(void) vbglPhysHeapLeave (void)
191 RTSemFastMutexRelease(g_vbgldata
.mutexHeap
);
195 static void vbglPhysHeapInitBlock (VBGLPHYSHEAPBLOCK
*pBlock
, VBGLPHYSHEAPCHUNK
*pChunk
, uint32_t cbDataSize
)
197 VBGL_PH_ASSERT(pBlock
!= NULL
);
198 VBGL_PH_ASSERT(pChunk
!= NULL
);
200 pBlock
->u32Signature
= VBGL_PH_BLOCKSIGNATURE
;
201 pBlock
->cbDataSize
= cbDataSize
;
202 pBlock
->fu32Flags
= 0;
203 pBlock
->pNext
= NULL
;
204 pBlock
->pPrev
= NULL
;
205 pBlock
->pChunk
= pChunk
;
209 static void vbglPhysHeapInsertBlock (VBGLPHYSHEAPBLOCK
*pInsertAfter
, VBGLPHYSHEAPBLOCK
*pBlock
)
211 VBGL_PH_ASSERTMsg(pBlock
->pNext
== NULL
,
212 ("pBlock->pNext = %p\n", pBlock
->pNext
));
213 VBGL_PH_ASSERTMsg(pBlock
->pPrev
== NULL
,
214 ("pBlock->pPrev = %p\n", pBlock
->pPrev
));
218 pBlock
->pNext
= pInsertAfter
->pNext
;
219 pBlock
->pPrev
= pInsertAfter
;
221 if (pInsertAfter
->pNext
)
223 pInsertAfter
->pNext
->pPrev
= pBlock
;
226 pInsertAfter
->pNext
= pBlock
;
230 /* inserting to head of list */
231 pBlock
->pPrev
= NULL
;
233 if (pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
)
235 pBlock
->pNext
= g_vbgldata
.pAllocBlocksHead
;
237 if (g_vbgldata
.pAllocBlocksHead
)
239 g_vbgldata
.pAllocBlocksHead
->pPrev
= pBlock
;
242 g_vbgldata
.pAllocBlocksHead
= pBlock
;
246 pBlock
->pNext
= g_vbgldata
.pFreeBlocksHead
;
248 if (g_vbgldata
.pFreeBlocksHead
)
250 g_vbgldata
.pFreeBlocksHead
->pPrev
= pBlock
;
253 g_vbgldata
.pFreeBlocksHead
= pBlock
;
258 static void vbglPhysHeapExcludeBlock (VBGLPHYSHEAPBLOCK
*pBlock
)
262 pBlock
->pNext
->pPrev
= pBlock
->pPrev
;
266 /* this is tail of list but we do not maintain tails of block lists.
274 pBlock
->pPrev
->pNext
= pBlock
->pNext
;
278 /* this is head of list but we do not maintain tails of block lists. */
279 if (pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
)
281 g_vbgldata
.pAllocBlocksHead
= pBlock
->pNext
;
285 g_vbgldata
.pFreeBlocksHead
= pBlock
->pNext
;
289 pBlock
->pNext
= NULL
;
290 pBlock
->pPrev
= NULL
;
293 static VBGLPHYSHEAPBLOCK
*vbglPhysHeapChunkAlloc (uint32_t cbSize
)
296 VBGLPHYSHEAPCHUNK
*pChunk
;
297 VBGLPHYSHEAPBLOCK
*pBlock
;
298 VBGL_PH_dprintf(("Allocating new chunk of size %d\n", cbSize
));
300 /* Compute chunk size to allocate */
301 if (cbSize
< VBGL_PH_CHUNKSIZE
)
303 /* Includes case of block size 0 during initialization */
304 cbSize
= VBGL_PH_CHUNKSIZE
;
308 /* Round up to next chunk size, which must be power of 2 */
309 cbSize
= (cbSize
+ (VBGL_PH_CHUNKSIZE
- 1)) & ~(VBGL_PH_CHUNKSIZE
- 1);
313 /* This function allocates physical contiguous memory (below 4GB) according to the IPRT docs.
314 * Address < 4G is required for the port IO.
316 pChunk
= (VBGLPHYSHEAPCHUNK
*)RTMemContAlloc (&physAddr
, cbSize
);
320 LogRel(("vbglPhysHeapChunkAlloc: failed to alloc %u contiguous bytes.\n", cbSize
));
324 AssertRelease(physAddr
< _4G
&& physAddr
+ cbSize
<= _4G
);
326 pChunk
->u32Signature
= VBGL_PH_CHUNKSIGNATURE
;
327 pChunk
->cbSize
= cbSize
;
328 pChunk
->physAddr
= (uint32_t)physAddr
;
329 pChunk
->cAllocatedBlocks
= 0;
330 pChunk
->pNext
= g_vbgldata
.pChunkHead
;
331 pChunk
->pPrev
= NULL
;
333 /* Initialize the free block, which now occupies entire chunk. */
334 pBlock
= (VBGLPHYSHEAPBLOCK
*)((char *)pChunk
+ sizeof (VBGLPHYSHEAPCHUNK
));
336 vbglPhysHeapInitBlock (pBlock
, pChunk
, cbSize
- sizeof (VBGLPHYSHEAPCHUNK
) - sizeof (VBGLPHYSHEAPBLOCK
));
338 vbglPhysHeapInsertBlock (NULL
, pBlock
);
340 g_vbgldata
.pChunkHead
= pChunk
;
342 VBGL_PH_dprintf(("Allocated chunk %p, block = %p size=%x\n", pChunk
, pBlock
, cbSize
));
348 void vbglPhysHeapChunkDelete (VBGLPHYSHEAPCHUNK
*pChunk
)
351 VBGL_PH_ASSERT(pChunk
!= NULL
);
352 VBGL_PH_ASSERTMsg(pChunk
->u32Signature
== VBGL_PH_CHUNKSIGNATURE
,
353 ("pChunk->u32Signature = %08X\n", pChunk
->u32Signature
));
355 VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk
, pChunk
->cbSize
));
357 /* first scan the chunk and exclude all blocks from lists */
359 p
= (char *)pChunk
+ sizeof (VBGLPHYSHEAPCHUNK
);
361 while (p
< (char *)pChunk
+ pChunk
->cbSize
)
363 VBGLPHYSHEAPBLOCK
*pBlock
= (VBGLPHYSHEAPBLOCK
*)p
;
365 p
+= pBlock
->cbDataSize
+ sizeof (VBGLPHYSHEAPBLOCK
);
367 vbglPhysHeapExcludeBlock (pBlock
);
370 VBGL_PH_ASSERTMsg(p
== (char *)pChunk
+ pChunk
->cbSize
,
371 ("p = %p, (char *)pChunk + pChunk->cbSize = %p, pChunk->cbSize = %08X\n",
372 p
, (char *)pChunk
+ pChunk
->cbSize
, pChunk
->cbSize
));
374 /* Exclude chunk from the chunk list */
377 pChunk
->pNext
->pPrev
= pChunk
->pPrev
;
381 /* we do not maintain tail */
387 pChunk
->pPrev
->pNext
= pChunk
->pNext
;
391 /* the chunk was head */
392 g_vbgldata
.pChunkHead
= pChunk
->pNext
;
395 RTMemContFree (pChunk
, pChunk
->cbSize
);
399 DECLR0VBGL(void *) VbglR0PhysHeapAlloc (uint32_t cbSize
)
401 VBGLPHYSHEAPBLOCK
*pBlock
, *iter
;
402 int rc
= vbglPhysHeapEnter ();
407 dumpheap ("pre alloc");
411 /* If there are free blocks in the heap, look at them. */
412 iter
= g_vbgldata
.pFreeBlocksHead
;
414 /* There will be not many blocks in the heap, so
415 * linear search would be fast enough.
420 if (iter
->cbDataSize
== cbSize
)
427 /* Looking for a free block with nearest size */
428 if (iter
->cbDataSize
> cbSize
)
432 if (iter
->cbDataSize
< pBlock
->cbDataSize
)
448 /* No free blocks, allocate a new chunk,
449 * the only free block of the chunk will
452 pBlock
= vbglPhysHeapChunkAlloc (cbSize
);
457 VBGL_PH_ASSERTMsg(pBlock
->u32Signature
== VBGL_PH_BLOCKSIGNATURE
,
458 ("pBlock = %p, pBlock->u32Signature = %08X\n", pBlock
, pBlock
->u32Signature
));
459 VBGL_PH_ASSERTMsg((pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
) == 0,
460 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock
, pBlock
->fu32Flags
));
462 /* We have a free block, either found or allocated. */
464 if (pBlock
->cbDataSize
> 2*(cbSize
+ sizeof (VBGLPHYSHEAPBLOCK
)))
466 /* Data will occupy less than a half of the block,
467 * the block should be split.
469 iter
= (VBGLPHYSHEAPBLOCK
*)((char *)pBlock
+ sizeof (VBGLPHYSHEAPBLOCK
) + cbSize
);
471 /* Init the new 'iter' block, initialized blocks are always marked as free. */
472 vbglPhysHeapInitBlock (iter
, pBlock
->pChunk
, pBlock
->cbDataSize
- cbSize
- sizeof (VBGLPHYSHEAPBLOCK
));
474 pBlock
->cbDataSize
= cbSize
;
476 /* Insert the new 'iter' block after the 'pBlock' in the free list */
477 vbglPhysHeapInsertBlock (pBlock
, iter
);
480 /* Exclude pBlock from free list */
481 vbglPhysHeapExcludeBlock (pBlock
);
483 /* Mark as allocated */
484 pBlock
->fu32Flags
|= VBGL_PH_BF_ALLOCATED
;
486 /* Insert to allocated list */
487 vbglPhysHeapInsertBlock (NULL
, pBlock
);
489 /* Adjust the chunk allocated blocks counter */
490 pBlock
->pChunk
->cAllocatedBlocks
++;
493 dumpheap ("post alloc");
495 vbglPhysHeapLeave ();
496 VBGL_PH_dprintf(("VbglR0PhysHeapAlloc %x size %x\n", vbglPhysHeapBlock2Data (pBlock
), pBlock
->cbDataSize
));
498 return vbglPhysHeapBlock2Data (pBlock
);
501 DECLR0VBGL(uint32_t) VbglR0PhysHeapGetPhysAddr (void *p
)
503 uint32_t physAddr
= 0;
504 VBGLPHYSHEAPBLOCK
*pBlock
= vbglPhysHeapData2Block (p
);
508 VBGL_PH_ASSERTMsg((pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
) != 0,
509 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock
, pBlock
->fu32Flags
));
511 if (pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
)
512 physAddr
= pBlock
->pChunk
->physAddr
+ (uint32_t)((uintptr_t)p
- (uintptr_t)pBlock
->pChunk
);
518 DECLR0VBGL(void) VbglR0PhysHeapFree(void *p
)
520 VBGLPHYSHEAPBLOCK
*pBlock
;
521 VBGLPHYSHEAPBLOCK
*pNeighbour
;
523 int rc
= vbglPhysHeapEnter ();
527 dumpheap ("pre free");
529 pBlock
= vbglPhysHeapData2Block (p
);
533 vbglPhysHeapLeave ();
537 VBGL_PH_ASSERTMsg((pBlock
->fu32Flags
& VBGL_PH_BF_ALLOCATED
) != 0,
538 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock
, pBlock
->fu32Flags
));
540 /* Exclude from allocated list */
541 vbglPhysHeapExcludeBlock (pBlock
);
543 dumpheap ("post exclude");
545 VBGL_PH_dprintf(("VbglR0PhysHeapFree %x size %x\n", p
, pBlock
->cbDataSize
));
548 pBlock
->fu32Flags
&= ~VBGL_PH_BF_ALLOCATED
;
550 /* Insert to free list */
551 vbglPhysHeapInsertBlock (NULL
, pBlock
);
553 dumpheap ("post insert");
555 /* Adjust the chunk allocated blocks counter */
556 pBlock
->pChunk
->cAllocatedBlocks
--;
558 VBGL_PH_ASSERT(pBlock
->pChunk
->cAllocatedBlocks
>= 0);
560 /* Check if we can merge 2 free blocks. To simplify heap maintenance,
561 * we will look at block after the just freed one.
562 * This will not prevent us from detecting free memory chunks.
563 * Also in most cases blocks are deallocated in reverse allocation order
564 * and in that case the merging will work.
567 pNeighbour
= (VBGLPHYSHEAPBLOCK
*)((char *)p
+ pBlock
->cbDataSize
);
569 if ((char *)pNeighbour
< (char *)pBlock
->pChunk
+ pBlock
->pChunk
->cbSize
570 && (pNeighbour
->fu32Flags
& VBGL_PH_BF_ALLOCATED
) == 0)
572 /* The next block is free as well. */
574 /* Adjust size of current memory block */
575 pBlock
->cbDataSize
+= pNeighbour
->cbDataSize
+ sizeof (VBGLPHYSHEAPBLOCK
);
577 /* Exclude the next neighbour */
578 vbglPhysHeapExcludeBlock (pNeighbour
);
581 dumpheap ("post merge");
583 /* now check if there are 2 or more free chunks */
584 if (pBlock
->pChunk
->cAllocatedBlocks
== 0)
586 VBGLPHYSHEAPCHUNK
*pChunk
= g_vbgldata
.pChunkHead
;
588 uint32_t u32FreeChunks
= 0;
592 if (pChunk
->cAllocatedBlocks
== 0)
597 pChunk
= pChunk
->pNext
;
600 if (u32FreeChunks
> 1)
602 /* Delete current chunk, it will also exclude all free blocks
603 * remaining in the chunk from the free list, so the pBlock
604 * will also be invalid after this.
606 vbglPhysHeapChunkDelete (pBlock
->pChunk
);
610 dumpheap ("post free");
612 vbglPhysHeapLeave ();
615 DECLR0VBGL(int) VbglR0PhysHeapInit (void)
617 int rc
= VINF_SUCCESS
;
619 /* Allocate the first chunk of the heap. */
620 VBGLPHYSHEAPBLOCK
*pBlock
= vbglPhysHeapChunkAlloc (0);
625 RTSemFastMutexCreate(&g_vbgldata
.mutexHeap
);
630 DECLR0VBGL(void) VbglR0PhysHeapTerminate (void)
632 while (g_vbgldata
.pChunkHead
)
634 vbglPhysHeapChunkDelete (g_vbgldata
.pChunkHead
);
637 RTSemFastMutexDestroy(g_vbgldata
.mutexHeap
);