1 | /* $Id: alloc-posix.cpp 31158 2010-07-28 03:24:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 <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 | #ifndef RT_OS_FREEBSD /* Deprecated on FreeBSD */
|
---|
39 | # include <malloc.h>
|
---|
40 | #endif
|
---|
41 | #include <errno.h>
|
---|
42 | #include <sys/mman.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | #if !defined(RT_USE_MMAP_EXEC) && (defined(RT_OS_LINUX))
|
---|
49 | # define RT_USE_MMAP_EXEC
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #if !defined(RT_USE_MMAP_PAGE) && 0 /** @todo mmap is too slow for full scale EF setup. */
|
---|
53 | # define RT_USE_MMAP_PAGE
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Structures and Typedefs *
|
---|
59 | *******************************************************************************/
|
---|
60 | #ifdef RT_USE_MMAP_EXEC
|
---|
61 | /**
|
---|
62 | * RTMemExecAlloc() header used when using mmap for allocating the memory.
|
---|
63 | */
|
---|
64 | typedef struct RTMEMEXECHDR
|
---|
65 | {
|
---|
66 | /** Magic number (RTMEMEXECHDR_MAGIC). */
|
---|
67 | size_t uMagic;
|
---|
68 | /** The size we requested from mmap. */
|
---|
69 | size_t cb;
|
---|
70 | # if ARCH_BITS == 32
|
---|
71 | uint32_t Alignment[2];
|
---|
72 | # endif
|
---|
73 | } RTMEMEXECHDR, *PRTMEMEXECHDR;
|
---|
74 |
|
---|
75 | /** Magic for RTMEMEXECHDR. */
|
---|
76 | # define RTMEMEXECHDR_MAGIC (~(size_t)0xfeedbabe)
|
---|
77 |
|
---|
78 | #endif /* RT_USE_MMAP_EXEC */
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) 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_EXEC
|
---|
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 | RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
|
---|
136 | {
|
---|
137 | if (pv)
|
---|
138 | {
|
---|
139 | #ifdef RT_USE_MMAP_EXEC
|
---|
140 | PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv - 1;
|
---|
141 | AssertMsgReturnVoid(RT_ALIGN_P(pHdr, PAGE_SIZE) == pHdr, ("pHdr=%p pv=%p\n", pHdr, pv));
|
---|
142 | AssertMsgReturnVoid(pHdr->uMagic == RTMEMEXECHDR_MAGIC, ("pHdr=%p(uMagic=%#zx) pv=%p\n", pHdr, pHdr->uMagic, pv));
|
---|
143 | int rc = munmap(pHdr, pHdr->cb);
|
---|
144 | AssertMsg(!rc, ("munmap -> %d errno=%d\n", rc, errno)); NOREF(rc);
|
---|
145 | #else
|
---|
146 | free(pv);
|
---|
147 | #endif
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
153 | {
|
---|
154 | #ifdef RT_USE_MMAP_PAGE
|
---|
155 | size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
156 | void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
157 | AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
|
---|
158 | return pv;
|
---|
159 |
|
---|
160 | #else
|
---|
161 | # if defined(RT_OS_FREEBSD) /** @todo huh? we're using posix_memalign in the next function... */
|
---|
162 | void *pv;
|
---|
163 | int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
164 | if (!rc)
|
---|
165 | return pv;
|
---|
166 | return NULL;
|
---|
167 | # else /* !RT_OS_FREEBSD */
|
---|
168 | return memalign(PAGE_SIZE, cb);
|
---|
169 | # endif
|
---|
170 | #endif
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
|
---|
175 | {
|
---|
176 | #ifdef RT_USE_MMAP_PAGE
|
---|
177 | size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
178 | void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
179 | AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
|
---|
180 | return pv;
|
---|
181 |
|
---|
182 | #else
|
---|
183 | void *pv;
|
---|
184 | int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
185 | if (!rc)
|
---|
186 | {
|
---|
187 | RT_BZERO(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
188 | return pv;
|
---|
189 | }
|
---|
190 | return NULL;
|
---|
191 | #endif
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
|
---|
196 | {
|
---|
197 | if (pv)
|
---|
198 | {
|
---|
199 | Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
|
---|
200 |
|
---|
201 | #ifdef RT_USE_MMAP_PAGE
|
---|
202 | size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
203 | int rc = munmap(pv, cbAligned);
|
---|
204 | AssertMsg(!rc, ("munmap(%p, %#zx) -> %d errno=%d\n", pv, cbAligned, rc, errno)); NOREF(rc);
|
---|
205 | #else
|
---|
206 | free(pv);
|
---|
207 | #endif
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
|
---|
213 | {
|
---|
214 | /*
|
---|
215 | * Validate input.
|
---|
216 | */
|
---|
217 | if (cb == 0)
|
---|
218 | {
|
---|
219 | AssertMsgFailed(("!cb\n"));
|
---|
220 | return VERR_INVALID_PARAMETER;
|
---|
221 | }
|
---|
222 | if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
|
---|
223 | {
|
---|
224 | AssertMsgFailed(("fProtect=%#x\n", fProtect));
|
---|
225 | return VERR_INVALID_PARAMETER;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Convert the flags.
|
---|
230 | */
|
---|
231 | int fProt;
|
---|
232 | #if RTMEM_PROT_NONE == PROT_NONE \
|
---|
233 | && RTMEM_PROT_READ == PROT_READ \
|
---|
234 | && RTMEM_PROT_WRITE == PROT_WRITE \
|
---|
235 | && RTMEM_PROT_EXEC == PROT_EXEC
|
---|
236 | fProt = fProtect;
|
---|
237 | #else
|
---|
238 | Assert(!RTMEM_PROT_NONE);
|
---|
239 | if (!fProtect)
|
---|
240 | fProt = PROT_NONE;
|
---|
241 | else
|
---|
242 | {
|
---|
243 | fProt = 0;
|
---|
244 | if (fProtect & RTMEM_PROT_READ)
|
---|
245 | fProt |= PROT_READ;
|
---|
246 | if (fProtect & RTMEM_PROT_WRITE)
|
---|
247 | fProt |= PROT_WRITE;
|
---|
248 | if (fProtect & RTMEM_PROT_EXEC)
|
---|
249 | fProt |= PROT_EXEC;
|
---|
250 | }
|
---|
251 | #endif
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Align the request.
|
---|
255 | */
|
---|
256 | cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
|
---|
257 | pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Change the page attributes.
|
---|
261 | */
|
---|
262 | int rc = mprotect(pv, cb, fProt);
|
---|
263 | if (!rc)
|
---|
264 | return rc;
|
---|
265 | return RTErrConvertFromErrno(errno);
|
---|
266 | }
|
---|