VirtualBox

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

Last change on this file since 77473 was 77172, checked in by vboxsync, 6 years ago

IRPT/alloc-r0drv-nt.cpp: Make sure we free early untagged allocations with the untagged free function, otherwise checked kernels will be unhappy with us. ticketref:18187

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.9 KB
Line 
1/* $Id: alloc-r0drv-nt.cpp 77172 2019-02-06 11:38:04Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
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#include "internal/iprt.h"
33#include <iprt/mem.h>
34
35#include <iprt/assert.h>
36#include <iprt/errcore.h>
37#include "r0drv/alloc-r0drv.h"
38#include "internal-r0drv-nt.h"
39
40
41/**
42 * OS specific allocation function.
43 */
44DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
45{
46 if (!(fFlags & RTMEMHDR_FLAG_ANY_CTX))
47 {
48 PRTMEMHDR pHdr;
49 if (g_pfnrtExAllocatePoolWithTag)
50 {
51 if (!(fFlags & RTMEMHDR_FLAG_EXEC) && g_uRtNtVersion >= RTNT_MAKE_VERSION(8,0))
52 pHdr = (PRTMEMHDR)g_pfnrtExAllocatePoolWithTag(NonPagedPoolNx, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
53 else
54 pHdr = (PRTMEMHDR)g_pfnrtExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
55 }
56 else
57 {
58 fFlags |= RTMEMHDR_FLAG_UNTAGGED;
59 pHdr = (PRTMEMHDR)ExAllocatePool(NonPagedPool, cb + sizeof(*pHdr));
60 }
61 if (pHdr)
62 {
63 pHdr->u32Magic = RTMEMHDR_MAGIC;
64 pHdr->fFlags = fFlags;
65 pHdr->cb = (uint32_t)cb; Assert(pHdr->cb == cb);
66 pHdr->cbReq = (uint32_t)cb;
67 *ppHdr = pHdr;
68 return VINF_SUCCESS;
69 }
70 return VERR_NO_MEMORY;
71 }
72 return VERR_NOT_SUPPORTED;
73}
74
75
76/**
77 * OS specific free function.
78 */
79DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
80{
81 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
82 if (g_pfnrtExFreePoolWithTag && !(pHdr->fFlags & RTMEMHDR_FLAG_UNTAGGED))
83 g_pfnrtExFreePoolWithTag(pHdr, IPRT_NT_POOL_TAG);
84 else
85 ExFreePool(pHdr);
86}
87
88
89/**
90 * Allocates physical contiguous memory (below 4GB).
91 * The allocation is page aligned and its contents is undefined.
92 *
93 * @returns Pointer to the memory block. This is page aligned.
94 * @param pPhys Where to store the physical address.
95 * @param cb The allocation size in bytes. This is always
96 * rounded up to PAGE_SIZE.
97 */
98RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
99{
100 /*
101 * validate input.
102 */
103 Assert(VALID_PTR(pPhys));
104 Assert(cb > 0);
105
106 /*
107 * Allocate and get physical address.
108 * Make sure the return is page aligned.
109 */
110 PHYSICAL_ADDRESS MaxPhysAddr;
111 MaxPhysAddr.HighPart = 0;
112 MaxPhysAddr.LowPart = 0xffffffff;
113 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
114 void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
115 if (pv)
116 {
117 if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
118 {
119 PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
120 if (!PhysAddr.HighPart) /* paranoia */
121 {
122 *pPhys = (RTCCPHYS)PhysAddr.LowPart;
123 return pv;
124 }
125
126 /* failure */
127 AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
128 }
129 else
130 AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
131
132 MmFreeContiguousMemory(pv);
133 }
134
135 return NULL;
136}
137
138
139/**
140 * Frees memory allocated ysing RTMemContAlloc().
141 *
142 * @param pv Pointer to return from RTMemContAlloc().
143 * @param cb The cb parameter passed to RTMemContAlloc().
144 */
145RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
146{
147 if (pv)
148 {
149 Assert(cb > 0); NOREF(cb);
150 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
151 MmFreeContiguousMemory(pv);
152 }
153}
154
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