VirtualBox

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

最後變更 在這個檔案從41054是 41054,由 vboxsync 提交於 13 年 前

alloc-r0drv-darwin.cpp: Use RTR0MemObjAllocPage to satisfy RTMEMHDR_FLAG_EXEC requests.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 5.0 KB
 
1/* $Id: alloc-r0drv-darwin.cpp 41054 2012-04-25 15:11:33Z 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.alldomusa.eu.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 }
84 else
85 {
86
87 pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
88 if (RT_UNLIKELY(!pHdr))
89 {
90 printf("rtR0MemAllocEx(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
91 return VERR_NO_MEMORY;
92 }
93 }
94
95 pHdr->u32Magic = RTMEMHDR_MAGIC;
96 pHdr->fFlags = fFlags;
97 pHdr->cb = cb;
98 pHdr->cbReq = cb;
99 *ppHdr = pHdr;;
100 return VINF_SUCCESS;
101}
102
103
104/**
105 * OS specific free function.
106 */
107DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
108{
109 pHdr->u32Magic += 1;
110 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
111 {
112 PRTMEMDARWINHDREX pExHdr = RT_FROM_MEMBER(pHdr, RTMEMDARWINHDREX, Hdr);
113 int rc = RTR0MemObjFree(pExHdr->hMemObj, false /*fFreeMappings*/);
114 AssertRC(rc);
115 }
116 else
117 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
118}
119
120
121RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
122{
123 /*
124 * validate input.
125 */
126 AssertPtr(pPhys);
127 Assert(cb > 0);
128 RT_ASSERT_PREEMPTIBLE();
129
130 /*
131 * Allocate the memory and ensure that the API is still providing
132 * memory that's always below 4GB.
133 */
134 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
135 IOPhysicalAddress PhysAddr;
136 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
137 if (pv)
138 {
139 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
140 {
141 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
142 {
143 *pPhys = PhysAddr;
144 return pv;
145 }
146 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
147 }
148 else
149 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
150 IOFreeContiguous(pv, cb);
151 }
152 return NULL;
153}
154
155
156RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
157{
158 RT_ASSERT_PREEMPTIBLE();
159 if (pv)
160 {
161 Assert(cb > 0);
162 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
163
164 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
165 IOFreeContiguous(pv, cb);
166 }
167}
168
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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