VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/alloc-r0drv-nt.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.1 KB
Line 
1/* $Id: alloc-r0drv-nt.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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 */
54DECLHIDDEN(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 = !(fFlags & RTMEMHDR_FLAG_EXEC) && g_uRtNtVersion >= RTNT_MAKE_VERSION(8,0)
60 ? NonPagedPoolNx : NonPagedPool;
61 if (g_pfnrtExAllocatePoolWithTag)
62 pHdr = (PRTMEMHDR)g_pfnrtExAllocatePoolWithTag(enmPoolType, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
63 else
64 {
65 fFlags |= RTMEMHDR_FLAG_UNTAGGED;
66 pHdr = (PRTMEMHDR)ExAllocatePool(enmPoolType, cb + sizeof(*pHdr));
67 }
68 if (pHdr)
69 {
70 pHdr->u32Magic = RTMEMHDR_MAGIC;
71 pHdr->fFlags = fFlags;
72 pHdr->cb = (uint32_t)cb; Assert(pHdr->cb == cb);
73 pHdr->cbReq = (uint32_t)cb;
74 *ppHdr = pHdr;
75 return VINF_SUCCESS;
76 }
77 return VERR_NO_MEMORY;
78 }
79 return VERR_NOT_SUPPORTED;
80}
81
82
83/**
84 * OS specific free function.
85 */
86DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
87{
88 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
89 if (g_pfnrtExFreePoolWithTag && !(pHdr->fFlags & RTMEMHDR_FLAG_UNTAGGED))
90 g_pfnrtExFreePoolWithTag(pHdr, IPRT_NT_POOL_TAG);
91 else
92 ExFreePool(pHdr);
93}
94
95
96/**
97 * Allocates physical contiguous memory (below 4GB).
98 * The allocation is page aligned and its contents is undefined.
99 *
100 * @returns Pointer to the memory block. This is page aligned.
101 * @param pPhys Where to store the physical address.
102 * @param cb The allocation size in bytes. This is always
103 * rounded up to PAGE_SIZE.
104 */
105RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
106{
107 /*
108 * validate input.
109 */
110 AssertPtr(pPhys);
111 Assert(cb > 0);
112
113 /*
114 * Allocate and get physical address.
115 * Make sure the return is page aligned.
116 */
117 PHYSICAL_ADDRESS MaxPhysAddr;
118 MaxPhysAddr.HighPart = 0;
119 MaxPhysAddr.LowPart = 0xffffffff;
120 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
121 void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
122 if (pv)
123 {
124 if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
125 {
126 PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
127 if (!PhysAddr.HighPart) /* paranoia */
128 {
129 *pPhys = (RTCCPHYS)PhysAddr.LowPart;
130 return pv;
131 }
132
133 /* failure */
134 AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
135 }
136 else
137 AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
138
139 MmFreeContiguousMemory(pv);
140 }
141
142 return NULL;
143}
144
145
146/**
147 * Frees memory allocated ysing RTMemContAlloc().
148 *
149 * @param pv Pointer to return from RTMemContAlloc().
150 * @param cb The cb parameter passed to RTMemContAlloc().
151 */
152RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
153{
154 if (pv)
155 {
156 Assert(cb > 0); NOREF(cb);
157 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
158 MmFreeContiguousMemory(pv);
159 }
160}
161
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette