1
/* $Id: alloc-posix.cpp $ */
3
* IPRT - Memory Allocation, POSIX.
7
* Copyright (C) 2006-2007 Oracle Corporation
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.
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.
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.
28
/*******************************************************************************
30
*******************************************************************************/
31
#include <iprt/alloc.h>
32
#include <iprt/assert.h>
33
#include <iprt/param.h>
35
#include <iprt/string.h>
38
#ifndef RT_OS_FREEBSD /* Deprecated on FreeBSD */
45
/*******************************************************************************
46
* Defined Constants And Macros *
47
*******************************************************************************/
48
#if !defined(RT_USE_MMAP_EXEC) && (defined(RT_OS_LINUX))
49
# define RT_USE_MMAP_EXEC
52
#if !defined(RT_USE_MMAP_PAGE) && 0 /** @todo mmap is too slow for full scale EF setup. */
53
# define RT_USE_MMAP_PAGE
57
/*******************************************************************************
58
* Structures and Typedefs *
59
*******************************************************************************/
60
#ifdef RT_USE_MMAP_EXEC
62
* RTMemExecAlloc() header used when using mmap for allocating the memory.
64
typedef struct RTMEMEXECHDR
66
/** Magic number (RTMEMEXECHDR_MAGIC). */
68
/** The size we requested from mmap. */
71
uint32_t Alignment[2];
73
} RTMEMEXECHDR, *PRTMEMEXECHDR;
75
/** Magic for RTMEMEXECHDR. */
76
# define RTMEMEXECHDR_MAGIC (~(size_t)0xfeedbabe)
78
#endif /* RT_USE_MMAP_EXEC */
83
* Allocates memory which may contain code.
85
* @returns Pointer to the allocated memory.
86
* @returns NULL on failure.
87
* @param cb Size in bytes of the memory block to allocate.
89
RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
91
AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
93
#ifdef RT_USE_MMAP_EXEC
95
* Use mmap to get low memory.
97
size_t cbAlloc = RT_ALIGN_Z(cb + sizeof(RTMEMEXECHDR), PAGE_SIZE);
98
void *pv = mmap(NULL, cbAlloc, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS
99
# if defined(RT_ARCH_AMD64) && defined(MAP_32BIT)
103
AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
104
PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv;
105
pHdr->uMagic = RTMEMEXECHDR_MAGIC;
113
cb = RT_ALIGN_Z(cb, 32);
115
int rc = posix_memalign(&pv, 32, cb);
116
AssertMsg(!rc && pv, ("posix_memalign(%zd) failed!!! rc=%d\n", cb, rc));
120
* Add PROT_EXEC flag to the page.
122
* This is in violation of the SuS where I think it saith that mprotect() shall
123
* only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
125
memset(pv, 0xcc, cb);
126
void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
127
size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
128
cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
129
rc = mprotect(pvProt, cbProt, PROT_READ | PROT_WRITE | PROT_EXEC);
132
AssertMsgFailed(("mprotect(%p, %#zx,,) -> rc=%d, errno=%d\n", pvProt, cbProt, rc, errno));
143
* Free executable/read/write memory allocated by RTMemExecAlloc().
145
* @param pv Pointer to memory block.
147
RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
151
#ifdef RT_USE_MMAP_EXEC
152
PRTMEMEXECHDR pHdr = (PRTMEMEXECHDR)pv - 1;
153
AssertMsgReturnVoid(RT_ALIGN_P(pHdr, PAGE_SIZE) == pHdr, ("pHdr=%p pv=%p\n", pHdr, pv));
154
AssertMsgReturnVoid(pHdr->uMagic == RTMEMEXECHDR_MAGIC, ("pHdr=%p(uMagic=%#zx) pv=%p\n", pHdr, pHdr->uMagic, pv));
155
int rc = munmap(pHdr, pHdr->cb);
156
AssertMsg(!rc, ("munmap -> %d errno=%d\n", rc, errno)); NOREF(rc);
165
* Allocate page aligned memory.
167
* @returns Pointer to the allocated memory.
168
* @returns NULL if we're out of memory.
169
* @param cb Size of the memory block. Will be rounded up to page size.
171
RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
173
#ifdef RT_USE_MMAP_PAGE
174
size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
175
void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
176
AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
180
# if defined(RT_OS_FREEBSD) /** @todo huh? we're using posix_memalign in the next function... */
182
int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
186
# else /* !RT_OS_FREEBSD */
187
return memalign(PAGE_SIZE, cb);
194
* Allocate zero'ed page aligned memory.
196
* @returns Pointer to the allocated memory.
197
* @returns NULL if we're out of memory.
198
* @param cb Size of the memory block. Will be rounded up to page size.
200
RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
202
#ifdef RT_USE_MMAP_PAGE
203
size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
204
void *pv = mmap(NULL, cbAligned, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
205
AssertMsgReturn(pv != MAP_FAILED, ("errno=%d cb=%#zx\n", errno, cb), NULL);
210
int rc = posix_memalign(&pv, PAGE_SIZE, RT_ALIGN_Z(cb, PAGE_SIZE));
213
RT_BZERO(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
222
* Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
224
* @param pv Pointer to the block as it was returned by the allocation function.
225
* NULL will be ignored.
227
RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
231
Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
233
#ifdef RT_USE_MMAP_PAGE
234
size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
235
int rc = munmap(pv, cbAligned);
236
AssertMsg(!rc, ("munmap(%p, %#zx) -> %d errno=%d\n", pv, cbAligned, rc, errno)); NOREF(rc);
245
* Change the page level protection of a memory region.
247
* @returns iprt status code.
248
* @param pv Start of the region. Will be rounded down to nearest page boundary.
249
* @param cb Size of the region. Will be rounded up to the nearest page boundary.
250
* @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
252
RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
259
AssertMsgFailed(("!cb\n"));
260
return VERR_INVALID_PARAMETER;
262
if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
264
AssertMsgFailed(("fProtect=%#x\n", fProtect));
265
return VERR_INVALID_PARAMETER;
272
#if RTMEM_PROT_NONE == PROT_NONE \
273
&& RTMEM_PROT_READ == PROT_READ \
274
&& RTMEM_PROT_WRITE == PROT_WRITE \
275
&& RTMEM_PROT_EXEC == PROT_EXEC
278
Assert(!RTMEM_PROT_NONE);
284
if (fProtect & RTMEM_PROT_READ)
286
if (fProtect & RTMEM_PROT_WRITE)
288
if (fProtect & RTMEM_PROT_EXEC)
296
cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
297
pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
300
* Change the page attributes.
302
int rc = mprotect(pv, cb, fProt);
305
return RTErrConvertFromErrno(errno);