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