1 | /* $Id: alloc-r0drv-nt.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, NT.
|
---|
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 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-nt-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include "r0drv/alloc-r0drv.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * OS specific allocation function.
|
---|
44 | */
|
---|
45 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
46 | {
|
---|
47 | PRTMEMHDR pHdr = (PRTMEMHDR)ExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
|
---|
48 | if (pHdr)
|
---|
49 | {
|
---|
50 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
51 | pHdr->fFlags = fFlags;
|
---|
52 | pHdr->cb = cb;
|
---|
53 | pHdr->cbReq = cb;
|
---|
54 | }
|
---|
55 | return pHdr;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * OS specific free function.
|
---|
61 | */
|
---|
62 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
63 | {
|
---|
64 | pHdr->u32Magic += 1;
|
---|
65 | ExFreePool(pHdr);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Allocates physical contiguous memory (below 4GB).
|
---|
71 | * The allocation is page aligned and its contents is undefined.
|
---|
72 | *
|
---|
73 | * @returns Pointer to the memory block. This is page aligned.
|
---|
74 | * @param pPhys Where to store the physical address.
|
---|
75 | * @param cb The allocation size in bytes. This is always
|
---|
76 | * rounded up to PAGE_SIZE.
|
---|
77 | */
|
---|
78 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
79 | {
|
---|
80 | /*
|
---|
81 | * validate input.
|
---|
82 | */
|
---|
83 | Assert(VALID_PTR(pPhys));
|
---|
84 | Assert(cb > 0);
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Allocate and get physical address.
|
---|
88 | * Make sure the return is page aligned.
|
---|
89 | */
|
---|
90 | PHYSICAL_ADDRESS MaxPhysAddr;
|
---|
91 | MaxPhysAddr.HighPart = 0;
|
---|
92 | MaxPhysAddr.LowPart = 0xffffffff;
|
---|
93 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
94 | void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
|
---|
95 | if (pv)
|
---|
96 | {
|
---|
97 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
|
---|
98 | {
|
---|
99 | PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
|
---|
100 | if (!PhysAddr.HighPart) /* paranoia */
|
---|
101 | {
|
---|
102 | *pPhys = (RTCCPHYS)PhysAddr.LowPart;
|
---|
103 | return pv;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* failure */
|
---|
107 | AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
|
---|
108 | }
|
---|
109 | else
|
---|
110 | AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
|
---|
111 |
|
---|
112 | MmFreeContiguousMemory(pv);
|
---|
113 | }
|
---|
114 |
|
---|
115 | return NULL;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
121 | *
|
---|
122 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
123 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
124 | */
|
---|
125 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
126 | {
|
---|
127 | if (pv)
|
---|
128 | {
|
---|
129 | Assert(cb > 0); NOREF(cb);
|
---|
130 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
131 | MmFreeContiguousMemory(pv);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|