VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/alloc-posix.cpp@ 28317

Last change on this file since 28317 was 28317, checked in by vboxsync, 15 years ago

RTMemPageFree + all users: Added size parameter to RTMemPageFree so we can avoid tracking structures when using mmap/munmap.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1/* $Id: alloc-posix.cpp 28317 2010-04-14 18:06:05Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/param.h>
38#include <iprt/err.h>
39#include <iprt/string.h>
40
41#include <stdlib.h>
42#include <malloc.h>
43#include <errno.h>
44#include <sys/mman.h>
45
46
47/*******************************************************************************
48* Defined Constants And Macros *
49*******************************************************************************/
50#if !defined(RT_USE_MMAP_EXEC) && (defined(RT_OS_LINUX))
51# define RT_USE_MMAP_EXEC
52#endif
53
54#if !defined(RT_USE_MMAP_PAGE) && 0 /** @todo mmap is too slow for full scale EF setup. */
55# define RT_USE_MMAP_PAGE
56#endif
57
58
59/*******************************************************************************
60* Structures and Typedefs *
61*******************************************************************************/
62#ifdef RT_USE_MMAP_EXEC
63/**
64 * RTMemExecAlloc() header used when using mmap for allocating the memory.
65 */
66typedef struct RTMEMEXECHDR
67{
68 /** Magic number (RTMEMEXECHDR_MAGIC). */
69 size_t uMagic;
70 /** The size we requested from mmap. */
71 size_t cb;
72# if ARCH_BITS == 32
73 uint32_t Alignment[2];
74# endif
75} RTMEMEXECHDR, *PRTMEMEXECHDR;
76
77/** Magic for RTMEMEXECHDR. */
78# define RTMEMEXECHDR_MAGIC (~(size_t)0xfeedbabe)
79
80#endif /* RT_USE_MMAP_EXEC */
81
82
83
84/**
85 * Allocates memory which may contain code.
86 *
87 * @returns Pointer to the allocated memory.
88 * @returns NULL on failure.
89 * @param cb Size in bytes of the memory block to allocate.
90 */
91RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
92{
93 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
94
95#ifdef RT_USE_MMAP_EXEC
96 /*
97 * Use mmap to get low memory.
98 */
99 size_t cbAlloc = RT_ALIGN_Z(cb + sizeof(RTMEMEXECHDR), PAGE_SIZE);
100 void *pv = mmap(NULL, cbAlloc, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS
101# if defined(RT_ARCH_AMD64) && defined(MAP_32BIT)
102 | MAP_32BIT
103# endif
104 , -1, 0);
105 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
106 PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv;
107 pHdr->uMagic = RTMEMEXECHDR_MAGIC;
108 pHdr->cb = cbAlloc;
109 pv = pHdr + 1;
110
111#else
112 /*
113 * Allocate first.
114 */
115 cb = RT_ALIGN_Z(cb, 32);
116 void *pv = NULL;
117 int rc = posix_memalign(&pv, 32, cb);
118 AssertMsg(!rc && pv, ("posix_memalign(%zd) failed!!! rc=%d\n", cb, rc));
119 if (pv && !rc)
120 {
121 /*
122 * Add PROT_EXEC flag to the page.
123 *
124 * This is in violation of the SuS where I think it saith that mprotect() shall
125 * only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
126 */
127 memset(pv, 0xcc, cb);
128 void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
129 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
130 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
131 rc = mprotect(pvProt, cbProt, PROT_READ | PROT_WRITE | PROT_EXEC);
132 if (rc)
133 {
134 AssertMsgFailed(("mprotect(%p, %#zx,,) -> rc=%d, errno=%d\n", pvProt, cbProt, rc, errno));
135 free(pv);
136 pv = NULL;
137 }
138 }
139#endif
140 return pv;
141}
142
143
144/**
145 * Free executable/read/write memory allocated by RTMemExecAlloc().
146 *
147 * @param pv Pointer to memory block.
148 */
149RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
150{
151 if (pv)
152 {
153#ifdef RT_USE_MMAP_EXEC
154 PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv - 1;
155 AssertMsgReturnVoid(RT_ALIGN_P(pHdr, PAGE_SIZE) == pHdr, ("pHdr=%p pv=%p\n", pHdr, pv));
156 AssertMsgReturnVoid(pHdr->uMagic == RTMEMEXECHDR_MAGIC, ("pHdr=%p(uMagic=%#zx) pv=%p\n", pHdr, pHdr->uMagic, pv));
157 int rc = munmap(pHdr, pHdr->cb);
158 AssertMsg(!rc, ("munmap -> %d errno=%d\n", rc, errno)); NOREF(rc);
159#else
160 free(pv);
161#endif
162 }
163}
164
165
166/**
167 * Allocate page aligned memory.
168 *
169 * @returns Pointer to the allocated memory.
170 * @returns NULL if we're out of memory.
171 * @param cb Size of the memory block. Will be rounded up to page size.
172 */
173RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
174{
175#ifdef RT_USE_MMAP_PAGE
176 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
177 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
178 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
179 return pv;
180
181#else
182# if 0 /** @todo huh? we're using posix_memalign in the next function... */
183 void *pv;
184 int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
185 if (!rc)
186 return pv;
187 return NULL;
188# else
189 return memalign(PAGE_SIZE, cb);
190# endif
191#endif
192}
193
194
195/**
196 * Allocate zero'ed page aligned memory.
197 *
198 * @returns Pointer to the allocated memory.
199 * @returns NULL if we're out of memory.
200 * @param cb Size of the memory block. Will be rounded up to page size.
201 */
202RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
203{
204#ifdef RT_USE_MMAP_PAGE
205 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
206 void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
207 AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
208 return pv;
209
210#else
211 void *pv;
212 int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
213 if (!rc)
214 {
215 RT_BZERO(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
216 return pv;
217 }
218 return NULL;
219#endif
220}
221
222
223/**
224 * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
225 *
226 * @param pv Pointer to the block as it was returned by the allocation function.
227 * NULL will be ignored.
228 */
229RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
230{
231 if (pv)
232 {
233 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
234
235#ifdef RT_USE_MMAP_PAGE
236 size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
237 int rc = munmap(pv, cbAligned);
238 AssertMsg(!rc, ("munmap(%p, %#zx) -> %d errno=%d\n", pv, cbAligned, rc, errno)); NOREF(rc);
239#else
240 free(pv);
241#endif
242 }
243}
244
245
246/**
247 * Change the page level protection of a memory region.
248 *
249 * @returns iprt status code.
250 * @param pv Start of the region. Will be rounded down to nearest page boundary.
251 * @param cb Size of the region. Will be rounded up to the nearest page boundary.
252 * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
253 */
254RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
255{
256 /*
257 * Validate input.
258 */
259 if (cb == 0)
260 {
261 AssertMsgFailed(("!cb\n"));
262 return VERR_INVALID_PARAMETER;
263 }
264 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
265 {
266 AssertMsgFailed(("fProtect=%#x\n", fProtect));
267 return VERR_INVALID_PARAMETER;
268 }
269
270 /*
271 * Convert the flags.
272 */
273 int fProt;
274#if RTMEM_PROT_NONE == PROT_NONE \
275 && RTMEM_PROT_READ == PROT_READ \
276 && RTMEM_PROT_WRITE == PROT_WRITE \
277 && RTMEM_PROT_EXEC == PROT_EXEC
278 fProt = fProtect;
279#else
280 Assert(!RTMEM_PROT_NONE);
281 if (!fProtect)
282 fProt = PROT_NONE;
283 else
284 {
285 fProt = 0;
286 if (fProtect & RTMEM_PROT_READ)
287 fProt |= PROT_READ;
288 if (fProtect & RTMEM_PROT_WRITE)
289 fProt |= PROT_WRITE;
290 if (fProtect & RTMEM_PROT_EXEC)
291 fProt |= PROT_EXEC;
292 }
293#endif
294
295 /*
296 * Align the request.
297 */
298 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
299 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
300
301 /*
302 * Change the page attributes.
303 */
304 int rc = mprotect(pv, cb, fProt);
305 if (!rc)
306 return rc;
307 return RTErrConvertFromErrno(errno);
308}
Note: See TracBrowser for help on using the repository browser.

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