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