VirtualBox

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

Last change on this file since 7689 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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