1 | /* $Id: alloc-r0drv-nt.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Memory Allocation, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include "the-nt-kernel.h"
|
---|
27 |
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include "r0drv/alloc-r0drv.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * OS specific allocation function.
|
---|
35 | */
|
---|
36 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
37 | {
|
---|
38 | Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
|
---|
39 | PRTMEMHDR pHdr = (PRTMEMHDR)ExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), 'iprt');
|
---|
40 | if (pHdr)
|
---|
41 | {
|
---|
42 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
43 | pHdr->fFlags = fFlags;
|
---|
44 | pHdr->cb = cb;
|
---|
45 | pHdr->u32Padding= 0;
|
---|
46 | }
|
---|
47 | return pHdr;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * OS specific free function.
|
---|
53 | */
|
---|
54 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
55 | {
|
---|
56 | pHdr->u32Magic += 1;
|
---|
57 | ExFreePool(pHdr);
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Allocates physical contiguous memory (below 4GB).
|
---|
63 | * The allocation is page aligned and the content is undefined.
|
---|
64 | *
|
---|
65 | * @returns Pointer to the memory block. This is page aligned.
|
---|
66 | * @param pPhys Where to store the physical address.
|
---|
67 | * @param cb The allocation size in bytes. This is always
|
---|
68 | * rounded up to PAGE_SIZE.
|
---|
69 | */
|
---|
70 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * validate input.
|
---|
74 | */
|
---|
75 | Assert(VALID_PTR(pPhys));
|
---|
76 | Assert(cb > 0);
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Allocate and get physical address.
|
---|
80 | * Make sure the return is page aligned.
|
---|
81 | */
|
---|
82 | PHYSICAL_ADDRESS MaxPhysAddr;
|
---|
83 | MaxPhysAddr.HighPart = 0;
|
---|
84 | MaxPhysAddr.LowPart = 0xffffffff;
|
---|
85 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
86 | void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
|
---|
87 | if (pv)
|
---|
88 | {
|
---|
89 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
|
---|
90 | {
|
---|
91 | PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
|
---|
92 | if (!PhysAddr.HighPart) /* paranoia */
|
---|
93 | {
|
---|
94 | *pPhys = (RTCCPHYS)PhysAddr.LowPart;
|
---|
95 | return pv;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* failure */
|
---|
99 | AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
|
---|
100 | }
|
---|
101 | else
|
---|
102 | AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
|
---|
103 |
|
---|
104 | MmFreeContiguousMemory(pv);
|
---|
105 | }
|
---|
106 |
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
113 | *
|
---|
114 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
115 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
116 | */
|
---|
117 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
118 | {
|
---|
119 | if (pv)
|
---|
120 | {
|
---|
121 | Assert(cb > 0); NOREF(cb);
|
---|
122 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
123 | MmFreeContiguousMemory(pv);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|