1 | /* $Id: alloc-r0drv-nt.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-nt-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/mem.h>
|
---|
44 |
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include "r0drv/alloc-r0drv.h"
|
---|
48 | #include "internal-r0drv-nt.h"
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * OS specific allocation function.
|
---|
53 | */
|
---|
54 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
55 | {
|
---|
56 | if (!(fFlags & RTMEMHDR_FLAG_ANY_CTX))
|
---|
57 | {
|
---|
58 | PRTMEMHDR pHdr;
|
---|
59 | POOL_TYPE const enmPoolType = g_uRtNtVersion >= RTNT_MAKE_VERSION(8,0) ? NonPagedPoolNx : NonPagedPool;
|
---|
60 | if (g_pfnrtExAllocatePoolWithTag)
|
---|
61 | pHdr = (PRTMEMHDR)g_pfnrtExAllocatePoolWithTag(enmPoolType, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
|
---|
62 | else
|
---|
63 | {
|
---|
64 | fFlags |= RTMEMHDR_FLAG_UNTAGGED;
|
---|
65 | pHdr = (PRTMEMHDR)ExAllocatePool(enmPoolType, cb + sizeof(*pHdr));
|
---|
66 | }
|
---|
67 | if (RT_LIKELY(pHdr))
|
---|
68 | {
|
---|
69 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
70 | pHdr->fFlags = fFlags;
|
---|
71 | pHdr->cb = (uint32_t)cb; Assert(pHdr->cb == cb);
|
---|
72 | pHdr->cbReq = (uint32_t)cb;
|
---|
73 | *ppHdr = pHdr;
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | }
|
---|
76 | return VERR_NO_MEMORY;
|
---|
77 | }
|
---|
78 | return VERR_NOT_SUPPORTED;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * OS specific free function.
|
---|
84 | */
|
---|
85 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
86 | {
|
---|
87 | pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
|
---|
88 | if (g_pfnrtExFreePoolWithTag && !(pHdr->fFlags & RTMEMHDR_FLAG_UNTAGGED))
|
---|
89 | g_pfnrtExFreePoolWithTag(pHdr, IPRT_NT_POOL_TAG);
|
---|
90 | else
|
---|
91 | ExFreePool(pHdr);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
96 | {
|
---|
97 | /*
|
---|
98 | * validate input.
|
---|
99 | */
|
---|
100 | AssertPtr(pPhys);
|
---|
101 | Assert(cb > 0);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Allocate and get physical address.
|
---|
105 | * Make sure the return is page aligned.
|
---|
106 | */
|
---|
107 | PHYSICAL_ADDRESS MaxPhysAddr;
|
---|
108 | MaxPhysAddr.HighPart = 0;
|
---|
109 | MaxPhysAddr.LowPart = 0xffffffff;
|
---|
110 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
111 | void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
|
---|
112 | if (pv)
|
---|
113 | {
|
---|
114 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
|
---|
115 | {
|
---|
116 | PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
|
---|
117 | if (!PhysAddr.HighPart) /* paranoia */
|
---|
118 | {
|
---|
119 | *pPhys = (RTCCPHYS)PhysAddr.LowPart;
|
---|
120 | return pv;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* failure */
|
---|
124 | AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
|
---|
125 | }
|
---|
126 | else
|
---|
127 | AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
|
---|
128 |
|
---|
129 | MmFreeContiguousMemory(pv);
|
---|
130 | }
|
---|
131 |
|
---|
132 | return NULL;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
137 | {
|
---|
138 | if (pv)
|
---|
139 | {
|
---|
140 | Assert(cb > 0); NOREF(cb);
|
---|
141 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
142 | MmFreeContiguousMemory(pv);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|