VirtualBox

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

Last change on this file since 56221 was 48935, checked in by vboxsync, 11 years ago

Runtime: Whitespace and svn:keyword cleanups by scm.

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