1 | /* $Id: alloc-freebsd.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 <errno.h>
|
---|
43 | #include <sys/mman.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Allocates memory which may contain code.
|
---|
48 | *
|
---|
49 | * @returns Pointer to the allocated memory.
|
---|
50 | * @returns NULL on failure.
|
---|
51 | * @param cb Size in bytes of the memory block to allocate.
|
---|
52 | */
|
---|
53 | RTDECL(void *) RTMemExecAlloc(size_t cb) RT_NO_THROW
|
---|
54 | {
|
---|
55 | /*
|
---|
56 | * Allocate first.
|
---|
57 | */
|
---|
58 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
59 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
60 | void *pv = RTMemPageAlloc(cb);
|
---|
61 | if (pv)
|
---|
62 | {
|
---|
63 | /*
|
---|
64 | * Add PROT_EXEC flag to the page(s).
|
---|
65 | */
|
---|
66 | memset(pv, 0xcc, cb);
|
---|
67 | int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
|
---|
68 | if (rc)
|
---|
69 | {
|
---|
70 | AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
|
---|
71 | free(pv);
|
---|
72 | pv = NULL;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return pv;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
81 | *
|
---|
82 | * @param pv Pointer to memory block.
|
---|
83 | */
|
---|
84 | RTDECL(void) RTMemExecFree(void *pv) RT_NO_THROW
|
---|
85 | {
|
---|
86 | if (pv)
|
---|
87 | free(pv);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Allocate page aligned memory.
|
---|
93 | *
|
---|
94 | * @returns Pointer to the allocated memory.
|
---|
95 | * @returns NULL if we're out of memory.
|
---|
96 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
97 | */
|
---|
98 | RTDECL(void *) RTMemPageAlloc(size_t cb) RT_NO_THROW
|
---|
99 | {
|
---|
100 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
101 | void *pv = malloc(cb);
|
---|
102 | AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) == pv,
|
---|
103 | ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
|
---|
104 | NULL);
|
---|
105 | return pv;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Allocate zero'ed page aligned memory.
|
---|
111 | *
|
---|
112 | * @returns Pointer to the allocated memory.
|
---|
113 | * @returns NULL if we're out of memory.
|
---|
114 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
115 | */
|
---|
116 | RTDECL(void *) RTMemPageAllocZ(size_t cb) RT_NO_THROW
|
---|
117 | {
|
---|
118 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
119 | void *pv = RTMemPageAlloc(cb);
|
---|
120 | if (pv)
|
---|
121 | bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
122 | return pv;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
128 | *
|
---|
129 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
130 | * NULL will be ignored.
|
---|
131 | */
|
---|
132 | RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
|
---|
133 | {
|
---|
134 | if (pv)
|
---|
135 | free(pv);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Change the page level protection of a memory region.
|
---|
141 | *
|
---|
142 | * @returns iprt status code.
|
---|
143 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
144 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
145 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
146 | */
|
---|
147 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
|
---|
148 | {
|
---|
149 | /*
|
---|
150 | * Validate input.
|
---|
151 | */
|
---|
152 | if (cb == 0)
|
---|
153 | {
|
---|
154 | AssertMsgFailed(("!cb\n"));
|
---|
155 | return VERR_INVALID_PARAMETER;
|
---|
156 | }
|
---|
157 | if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
|
---|
158 | {
|
---|
159 | AssertMsgFailed(("fProtect=%#x\n", fProtect));
|
---|
160 | return VERR_INVALID_PARAMETER;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Convert the flags.
|
---|
165 | */
|
---|
166 | int fProt;
|
---|
167 | #if RTMEM_PROT_NONE == PROT_NONE \
|
---|
168 | && RTMEM_PROT_READ == PROT_READ \
|
---|
169 | && RTMEM_PROT_WRITE == PROT_WRITE \
|
---|
170 | && RTMEM_PROT_EXEC == PROT_EXEC
|
---|
171 | fProt = fProtect;
|
---|
172 | #else
|
---|
173 | Assert(!RTMEM_PROT_NONE);
|
---|
174 | if (!fProtect)
|
---|
175 | fProt = PROT_NONE;
|
---|
176 | else
|
---|
177 | {
|
---|
178 | fProt = 0;
|
---|
179 | if (fProtect & RTMEM_PROT_READ)
|
---|
180 | fProt |= PROT_READ;
|
---|
181 | if (fProtect & RTMEM_PROT_WRITE)
|
---|
182 | fProt |= PROT_WRITE;
|
---|
183 | if (fProtect & RTMEM_PROT_EXEC)
|
---|
184 | fProt |= PROT_EXEC;
|
---|
185 | }
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Align the request.
|
---|
190 | */
|
---|
191 | cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
|
---|
192 | pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Change the page attributes.
|
---|
196 | */
|
---|
197 | int rc = mprotect(pv, cb, fProt);
|
---|
198 | if (!rc)
|
---|
199 | return rc;
|
---|
200 | return RTErrConvertFromErrno(errno);
|
---|
201 | }
|
---|