1 | /* $Id: alloc-freebsd.cpp 8245 2008-04-21 17:24:28Z 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)
|
---|
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)
|
---|
85 | {
|
---|
86 | return RTMemPageFree(pv);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Allocate page aligned memory.
|
---|
92 | *
|
---|
93 | * @returns Pointer to the allocated memory.
|
---|
94 | * @returns NULL if we're out of memory.
|
---|
95 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
96 | */
|
---|
97 | RTDECL(void *) RTMemPageAlloc(size_t cb)
|
---|
98 | {
|
---|
99 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
100 | void *pv = malloc(cb);
|
---|
101 | AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) == pv,
|
---|
102 | ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
|
---|
103 | NULL);
|
---|
104 | return pv;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Allocate zero'ed page aligned memory.
|
---|
110 | *
|
---|
111 | * @returns Pointer to the allocated memory.
|
---|
112 | * @returns NULL if we're out of memory.
|
---|
113 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
114 | */
|
---|
115 | RTDECL(void *) RTMemPageAllocZ(size_t cb)
|
---|
116 | {
|
---|
117 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
118 | void *pv = RTMemPageAlloc(cb);
|
---|
119 | if (pv)
|
---|
120 | bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
121 | return pv;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
127 | *
|
---|
128 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
129 | * NULL will be ignored.
|
---|
130 | */
|
---|
131 | RTDECL(void) RTMemPageFree(void *pv)
|
---|
132 | {
|
---|
133 | if (pv)
|
---|
134 | free(pv);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Change the page level protection of a memory region.
|
---|
140 | *
|
---|
141 | * @returns iprt status code.
|
---|
142 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
143 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
144 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
145 | */
|
---|
146 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | * Validate input.
|
---|
150 | */
|
---|
151 | if (cb == 0)
|
---|
152 | {
|
---|
153 | AssertMsgFailed(("!cb\n"));
|
---|
154 | return VERR_INVALID_PARAMETER;
|
---|
155 | }
|
---|
156 | if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
|
---|
157 | {
|
---|
158 | AssertMsgFailed(("fProtect=%#x\n", fProtect));
|
---|
159 | return VERR_INVALID_PARAMETER;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Convert the flags.
|
---|
164 | */
|
---|
165 | int fProt;
|
---|
166 | #if RTMEM_PROT_NONE == PROT_NONE \
|
---|
167 | && RTMEM_PROT_READ == PROT_READ \
|
---|
168 | && RTMEM_PROT_WRITE == PROT_WRITE \
|
---|
169 | && RTMEM_PROT_EXEC == PROT_EXEC
|
---|
170 | fProt = fProtect;
|
---|
171 | #else
|
---|
172 | Assert(!RTMEM_PROT_NONE);
|
---|
173 | if (!fProtect)
|
---|
174 | fProt = PROT_NONE;
|
---|
175 | else
|
---|
176 | {
|
---|
177 | fProt = 0;
|
---|
178 | if (fProtect & RTMEM_PROT_READ)
|
---|
179 | fProt |= PROT_READ;
|
---|
180 | if (fProtect & RTMEM_PROT_WRITE)
|
---|
181 | fProt |= PROT_WRITE;
|
---|
182 | if (fProtect & RTMEM_PROT_EXEC)
|
---|
183 | fProt |= PROT_EXEC;
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Align the request.
|
---|
189 | */
|
---|
190 | cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
|
---|
191 | pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Change the page attributes.
|
---|
195 | */
|
---|
196 | int rc = mprotect(pv, cb, fProt);
|
---|
197 | if (!rc)
|
---|
198 | return rc;
|
---|
199 | return RTErrConvertFromErrno(errno);
|
---|
200 | }
|
---|