VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/alloc-r0drv-darwin.cpp@ 78327

Last change on this file since 78327 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.0 KB
Line 
1/* $Id: alloc-r0drv-darwin.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
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#pragma GCC diagnostic ignored "-Wdeprecated-declarations" /* (IOMallocContiguous et al are deprecated) */
32#include "the-darwin-kernel.h"
33#include "internal/iprt.h"
34#include <iprt/mem.h>
35#include <iprt/memobj.h>
36
37#include <iprt/assert.h>
38#include <iprt/errcore.h>
39#include <iprt/thread.h>
40#include "r0drv/alloc-r0drv.h"
41
42
43/*********************************************************************************************************************************
44* Structures and Typedefs *
45*********************************************************************************************************************************/
46/**
47 * Extended header used for headers marked with RTMEMHDR_FLAG_EXEC.
48 *
49 * This is used with allocating executable memory, for things like generated
50 * code and loaded modules.
51 */
52typedef struct RTMEMDARWINHDREX
53{
54 /** The associated memory object. */
55 RTR0MEMOBJ hMemObj;
56 /** Alignment padding. */
57 uint8_t abPadding[ARCH_BITS == 32 ? 12 : 8];
58 /** The header we present to the generic API. */
59 RTMEMHDR Hdr;
60} RTMEMDARWINHDREX;
61AssertCompileSize(RTMEMDARWINHDREX, 32);
62/** Pointer to an extended memory header. */
63typedef RTMEMDARWINHDREX *PRTMEMDARWINHDREX;
64
65
66/**
67 * OS specific allocation function.
68 */
69DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
70{
71 IPRT_DARWIN_SAVE_EFL_AC();
72
73 if (RT_UNLIKELY(fFlags & RTMEMHDR_FLAG_ANY_CTX))
74 return VERR_NOT_SUPPORTED;
75
76 PRTMEMHDR pHdr;
77 if (fFlags & RTMEMHDR_FLAG_EXEC)
78 {
79 RTR0MEMOBJ hMemObj;
80 int rc = RTR0MemObjAllocPage(&hMemObj, cb + sizeof(RTMEMDARWINHDREX), true /*fExecutable*/);
81 if (RT_FAILURE(rc))
82 {
83 IPRT_DARWIN_RESTORE_EFL_AC();
84 return rc;
85 }
86 PRTMEMDARWINHDREX pExHdr = (PRTMEMDARWINHDREX)RTR0MemObjAddress(hMemObj);
87 pExHdr->hMemObj = hMemObj;
88 pHdr = &pExHdr->Hdr;
89#if 1 /*fExecutable isn't currently honored above. */
90 rc = RTR0MemObjProtect(hMemObj, 0, RTR0MemObjSize(hMemObj), RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
91 AssertRC(rc);
92#endif
93 }
94 else
95 {
96 pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
97 if (RT_UNLIKELY(!pHdr))
98 {
99 printf("rtR0MemAllocEx(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
100 IPRT_DARWIN_RESTORE_EFL_AC();
101 return VERR_NO_MEMORY;
102 }
103 }
104
105 pHdr->u32Magic = RTMEMHDR_MAGIC;
106 pHdr->fFlags = fFlags;
107 pHdr->cb = cb;
108 pHdr->cbReq = cb;
109 *ppHdr = pHdr;
110
111 IPRT_DARWIN_RESTORE_EFL_AC();
112 return VINF_SUCCESS;
113}
114
115
116/**
117 * OS specific free function.
118 */
119DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
120{
121 IPRT_DARWIN_SAVE_EFL_AC();
122
123 pHdr->u32Magic += 1;
124 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
125 {
126 PRTMEMDARWINHDREX pExHdr = RT_FROM_MEMBER(pHdr, RTMEMDARWINHDREX, Hdr);
127 int rc = RTR0MemObjFree(pExHdr->hMemObj, false /*fFreeMappings*/);
128 AssertRC(rc);
129 }
130 else
131 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
132
133 IPRT_DARWIN_RESTORE_EFL_AC();
134}
135
136
137RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
138{
139 /*
140 * validate input.
141 */
142 AssertPtr(pPhys);
143 Assert(cb > 0);
144 RT_ASSERT_PREEMPTIBLE();
145 IPRT_DARWIN_SAVE_EFL_AC();
146
147 /*
148 * Allocate the memory and ensure that the API is still providing
149 * memory that's always below 4GB.
150 */
151 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
152 IOPhysicalAddress PhysAddr;
153 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
154 if (pv)
155 {
156 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
157 {
158 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
159 {
160 *pPhys = PhysAddr;
161 IPRT_DARWIN_RESTORE_EFL_AC();
162 return pv;
163 }
164 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
165 }
166 else
167 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
168 IOFreeContiguous(pv, cb);
169 }
170
171 IPRT_DARWIN_RESTORE_EFL_AC();
172 return NULL;
173}
174
175
176RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
177{
178 RT_ASSERT_PREEMPTIBLE();
179 if (pv)
180 {
181 Assert(cb > 0);
182 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
183 IPRT_DARWIN_SAVE_EFL_AC();
184
185 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
186 IOFreeContiguous(pv, cb);
187
188 IPRT_DARWIN_RESTORE_EFL_AC();
189 }
190}
191
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