1 |
/* |
2 |
* vm_alloc.cpp - Wrapper to various virtual memory allocation schemes |
3 |
* (supports mmap, vm_allocate or fallbacks to malloc) |
4 |
* |
5 |
* Basilisk II (C) 1997-2008 Christian Bauer |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#ifdef HAVE_FCNTL_H |
27 |
#include <fcntl.h> |
28 |
#endif |
29 |
|
30 |
#ifdef HAVE_WIN32_VM |
31 |
#define WIN32_LEAN_AND_MEAN /* avoid including junk */ |
32 |
#include <windows.h> |
33 |
#endif |
34 |
|
35 |
#include <stdio.h> |
36 |
#include <stdlib.h> |
37 |
#include <string.h> |
38 |
#include <limits.h> |
39 |
#include "vm_alloc.h" |
40 |
|
41 |
#ifdef HAVE_MACH_VM |
42 |
#ifndef HAVE_MACH_TASK_SELF |
43 |
#ifdef HAVE_TASK_SELF |
44 |
#define mach_task_self task_self |
45 |
#else |
46 |
#error "No task_self(), you lose." |
47 |
#endif |
48 |
#endif |
49 |
#endif |
50 |
|
51 |
#ifdef HAVE_WIN32_VM |
52 |
/* Windows is either ILP32 or LLP64 */ |
53 |
typedef UINT_PTR vm_uintptr_t; |
54 |
#else |
55 |
/* Other systems are sane as they are either ILP32 or LP64 */ |
56 |
typedef unsigned long vm_uintptr_t; |
57 |
#endif |
58 |
|
59 |
/* We want MAP_32BIT, if available, for SheepShaver and BasiliskII |
60 |
because the emulated target is 32-bit and this helps to allocate |
61 |
memory so that branches could be resolved more easily (32-bit |
62 |
displacement to code in .text), on AMD64 for example. */ |
63 |
#ifndef MAP_32BIT |
64 |
#define MAP_32BIT 0 |
65 |
#endif |
66 |
#ifndef MAP_ANON |
67 |
#define MAP_ANON 0 |
68 |
#endif |
69 |
#ifndef MAP_ANONYMOUS |
70 |
#define MAP_ANONYMOUS 0 |
71 |
#endif |
72 |
|
73 |
#define MAP_EXTRA_FLAGS (MAP_32BIT) |
74 |
|
75 |
#ifdef HAVE_MMAP_VM |
76 |
#if (defined(__linux__) && defined(__i386__)) || HAVE_LINKER_SCRIPT |
77 |
/* Force a reasonnable address below 0x80000000 on x86 so that we |
78 |
don't get addresses above when the program is run on AMD64. |
79 |
NOTE: this is empirically determined on Linux/x86. */ |
80 |
#define MAP_BASE 0x10000000 |
81 |
#else |
82 |
#define MAP_BASE 0x00000000 |
83 |
#endif |
84 |
static char * next_address = (char *)MAP_BASE; |
85 |
#ifdef HAVE_MMAP_ANON |
86 |
#define map_flags (MAP_ANON | MAP_EXTRA_FLAGS) |
87 |
#define zero_fd -1 |
88 |
#else |
89 |
#ifdef HAVE_MMAP_ANONYMOUS |
90 |
#define map_flags (MAP_ANONYMOUS | MAP_EXTRA_FLAGS) |
91 |
#define zero_fd -1 |
92 |
#else |
93 |
#define map_flags (MAP_EXTRA_FLAGS) |
94 |
static int zero_fd = -1; |
95 |
#endif |
96 |
#endif |
97 |
#endif |
98 |
|
99 |
/* Translate generic VM map flags to host values. */ |
100 |
|
101 |
#ifdef HAVE_MMAP_VM |
102 |
static int translate_map_flags(int vm_flags) |
103 |
{ |
104 |
int flags = 0; |
105 |
if (vm_flags & VM_MAP_SHARED) |
106 |
flags |= MAP_SHARED; |
107 |
if (vm_flags & VM_MAP_PRIVATE) |
108 |
flags |= MAP_PRIVATE; |
109 |
if (vm_flags & VM_MAP_FIXED) |
110 |
flags |= MAP_FIXED; |
111 |
if (vm_flags & VM_MAP_32BIT) |
112 |
flags |= MAP_32BIT; |
113 |
return flags; |
114 |
} |
115 |
#endif |
116 |
|
117 |
/* Align ADDR and SIZE to 64K boundaries. */ |
118 |
|
119 |
#ifdef HAVE_WIN32_VM |
120 |
static inline LPVOID align_addr_segment(LPVOID addr) |
121 |
{ |
122 |
return (LPVOID)(((vm_uintptr_t)addr) & -((vm_uintptr_t)65536)); |
123 |
} |
124 |
|
125 |
static inline DWORD align_size_segment(LPVOID addr, DWORD size) |
126 |
{ |
127 |
return size + ((vm_uintptr_t)addr - (vm_uintptr_t)align_addr_segment(addr)); |
128 |
} |
129 |
#endif |
130 |
|
131 |
/* Translate generic VM prot flags to host values. */ |
132 |
|
133 |
#ifdef HAVE_WIN32_VM |
134 |
static int translate_prot_flags(int prot_flags) |
135 |
{ |
136 |
int prot = PAGE_READWRITE; |
137 |
if (prot_flags == (VM_PAGE_EXECUTE | VM_PAGE_READ | VM_PAGE_WRITE)) |
138 |
prot = PAGE_EXECUTE_READWRITE; |
139 |
else if (prot_flags == (VM_PAGE_EXECUTE | VM_PAGE_READ)) |
140 |
prot = PAGE_EXECUTE_READ; |
141 |
else if (prot_flags == (VM_PAGE_READ | VM_PAGE_WRITE)) |
142 |
prot = PAGE_READWRITE; |
143 |
else if (prot_flags == VM_PAGE_READ) |
144 |
prot = PAGE_READONLY; |
145 |
else if (prot_flags == 0) |
146 |
prot = PAGE_NOACCESS; |
147 |
return prot; |
148 |
} |
149 |
#endif |
150 |
|
151 |
/* Initialize the VM system. Returns 0 if successful, -1 for errors. */ |
152 |
|
153 |
int vm_init(void) |
154 |
{ |
155 |
#ifdef HAVE_MMAP_VM |
156 |
#ifndef zero_fd |
157 |
zero_fd = open("/dev/zero", O_RDWR); |
158 |
if (zero_fd < 0) |
159 |
return -1; |
160 |
#endif |
161 |
#endif |
162 |
return 0; |
163 |
} |
164 |
|
165 |
/* Deallocate all internal data used to wrap virtual memory allocators. */ |
166 |
|
167 |
void vm_exit(void) |
168 |
{ |
169 |
#ifdef HAVE_MMAP_VM |
170 |
#ifndef zero_fd |
171 |
if (zero_fd != -1) { |
172 |
close(zero_fd); |
173 |
zero_fd = -1; |
174 |
} |
175 |
#endif |
176 |
#endif |
177 |
} |
178 |
|
179 |
/* Allocate zero-filled memory of SIZE bytes. The mapping is private |
180 |
and default protection bits are read / write. The return value |
181 |
is the actual mapping address chosen or VM_MAP_FAILED for errors. */ |
182 |
|
183 |
void * vm_acquire(size_t size, int options) |
184 |
{ |
185 |
void * addr; |
186 |
|
187 |
// VM_MAP_FIXED are to be used with vm_acquire_fixed() only |
188 |
if (options & VM_MAP_FIXED) |
189 |
return VM_MAP_FAILED; |
190 |
|
191 |
#ifdef HAVE_MACH_VM |
192 |
// vm_allocate() returns a zero-filled memory region |
193 |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, TRUE) != KERN_SUCCESS) |
194 |
return VM_MAP_FAILED; |
195 |
#else |
196 |
#ifdef HAVE_MMAP_VM |
197 |
int fd = zero_fd; |
198 |
int the_map_flags = translate_map_flags(options) | map_flags; |
199 |
|
200 |
if ((addr = mmap((caddr_t)next_address, size, VM_PAGE_DEFAULT, the_map_flags, fd, 0)) == (void *)MAP_FAILED) |
201 |
return VM_MAP_FAILED; |
202 |
|
203 |
// Sanity checks for 64-bit platforms |
204 |
if (sizeof(void *) == 8 && (options & VM_MAP_32BIT) && !((char *)addr <= (char *)0xffffffff)) |
205 |
return VM_MAP_FAILED; |
206 |
|
207 |
next_address = (char *)addr + size; |
208 |
#else |
209 |
#ifdef HAVE_WIN32_VM |
210 |
if ((addr = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == NULL) |
211 |
return VM_MAP_FAILED; |
212 |
#else |
213 |
if ((addr = calloc(size, 1)) == 0) |
214 |
return VM_MAP_FAILED; |
215 |
|
216 |
// Omit changes for protections because they are not supported in this mode |
217 |
return addr; |
218 |
#endif |
219 |
#endif |
220 |
#endif |
221 |
|
222 |
// Explicitely protect the newly mapped region here because on some systems, |
223 |
// say MacOS X, mmap() doesn't honour the requested protection flags. |
224 |
if (vm_protect(addr, size, VM_PAGE_DEFAULT) != 0) |
225 |
return VM_MAP_FAILED; |
226 |
|
227 |
return addr; |
228 |
} |
229 |
|
230 |
/* Allocate zero-filled memory at exactly ADDR (which must be page-aligned). |
231 |
Retuns 0 if successful, -1 on errors. */ |
232 |
|
233 |
int vm_acquire_fixed(void * addr, size_t size, int options) |
234 |
{ |
235 |
// Fixed mappings are required to be private |
236 |
if (options & VM_MAP_SHARED) |
237 |
return -1; |
238 |
|
239 |
#ifdef HAVE_MACH_VM |
240 |
// vm_allocate() returns a zero-filled memory region |
241 |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, 0) != KERN_SUCCESS) |
242 |
return -1; |
243 |
#else |
244 |
#ifdef HAVE_MMAP_VM |
245 |
int fd = zero_fd; |
246 |
int the_map_flags = translate_map_flags(options) | map_flags | MAP_FIXED; |
247 |
|
248 |
if (mmap((caddr_t)addr, size, VM_PAGE_DEFAULT, the_map_flags, fd, 0) == (void *)MAP_FAILED) |
249 |
return -1; |
250 |
#else |
251 |
#ifdef HAVE_WIN32_VM |
252 |
// Windows cannot allocate Low Memory |
253 |
if (addr == NULL) |
254 |
return -1; |
255 |
|
256 |
// Allocate a possibly offset region to align on 64K boundaries |
257 |
LPVOID req_addr = align_addr_segment(addr); |
258 |
DWORD req_size = align_size_segment(addr, size); |
259 |
LPVOID ret_addr = VirtualAlloc(req_addr, req_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); |
260 |
if (ret_addr != req_addr) |
261 |
return -1; |
262 |
#else |
263 |
// Unsupported |
264 |
return -1; |
265 |
#endif |
266 |
#endif |
267 |
#endif |
268 |
|
269 |
// Explicitely protect the newly mapped region here because on some systems, |
270 |
// say MacOS X, mmap() doesn't honour the requested protection flags. |
271 |
if (vm_protect(addr, size, VM_PAGE_DEFAULT) != 0) |
272 |
return -1; |
273 |
|
274 |
return 0; |
275 |
} |
276 |
|
277 |
/* Deallocate any mapping for the region starting at ADDR and extending |
278 |
LEN bytes. Returns 0 if successful, -1 on errors. */ |
279 |
|
280 |
int vm_release(void * addr, size_t size) |
281 |
{ |
282 |
// Safety check: don't try to release memory that was not allocated |
283 |
if (addr == VM_MAP_FAILED) |
284 |
return 0; |
285 |
|
286 |
#ifdef HAVE_MACH_VM |
287 |
if (vm_deallocate(mach_task_self(), (vm_address_t)addr, size) != KERN_SUCCESS) |
288 |
return -1; |
289 |
#else |
290 |
#ifdef HAVE_MMAP_VM |
291 |
if (munmap((caddr_t)addr, size) != 0) |
292 |
return -1; |
293 |
#else |
294 |
#ifdef HAVE_WIN32_VM |
295 |
if (VirtualFree(align_addr_segment(addr), 0, MEM_RELEASE) == 0) |
296 |
return -1; |
297 |
#else |
298 |
free(addr); |
299 |
#endif |
300 |
#endif |
301 |
#endif |
302 |
|
303 |
return 0; |
304 |
} |
305 |
|
306 |
/* Change the memory protection of the region starting at ADDR and |
307 |
extending LEN bytes to PROT. Returns 0 if successful, -1 for errors. */ |
308 |
|
309 |
int vm_protect(void * addr, size_t size, int prot) |
310 |
{ |
311 |
#ifdef HAVE_MACH_VM |
312 |
int ret_code = vm_protect(mach_task_self(), (vm_address_t)addr, size, 0, prot); |
313 |
return ret_code == KERN_SUCCESS ? 0 : -1; |
314 |
#else |
315 |
#ifdef HAVE_MMAP_VM |
316 |
int ret_code = mprotect((caddr_t)addr, size, prot); |
317 |
return ret_code == 0 ? 0 : -1; |
318 |
#else |
319 |
#ifdef HAVE_WIN32_VM |
320 |
DWORD old_prot; |
321 |
int ret_code = VirtualProtect(addr, size, translate_prot_flags(prot), &old_prot); |
322 |
return ret_code != 0 ? 0 : -1; |
323 |
#else |
324 |
// Unsupported |
325 |
return -1; |
326 |
#endif |
327 |
#endif |
328 |
#endif |
329 |
} |
330 |
|
331 |
/* Returns the size of a page. */ |
332 |
|
333 |
int vm_get_page_size(void) |
334 |
{ |
335 |
#ifdef HAVE_WIN32_VM |
336 |
static vm_uintptr_t page_size = 0; |
337 |
if (page_size == 0) { |
338 |
SYSTEM_INFO si; |
339 |
GetSystemInfo(&si); |
340 |
page_size = si.dwAllocationGranularity; |
341 |
} |
342 |
return page_size; |
343 |
#else |
344 |
return getpagesize(); |
345 |
#endif |
346 |
} |
347 |
|
348 |
#ifdef CONFIGURE_TEST_VM_MAP |
349 |
#include <stdlib.h> |
350 |
#include <signal.h> |
351 |
|
352 |
static void fault_handler(int sig) |
353 |
{ |
354 |
exit(1); |
355 |
} |
356 |
|
357 |
/* Tests covered here: |
358 |
- TEST_VM_PROT_* program slices actually succeeds when a crash occurs |
359 |
- TEST_VM_MAP_ANON* program slices succeeds when it could be compiled |
360 |
*/ |
361 |
int main(void) |
362 |
{ |
363 |
vm_init(); |
364 |
|
365 |
signal(SIGSEGV, fault_handler); |
366 |
#ifdef SIGBUS |
367 |
signal(SIGBUS, fault_handler); |
368 |
#endif |
369 |
|
370 |
#define page_align(address) ((char *)((vm_uintptr_t)(address) & -page_size)) |
371 |
vm_uintptr_t page_size = vm_get_page_size(); |
372 |
|
373 |
const int area_size = 6 * page_size; |
374 |
volatile char * area = (volatile char *) vm_acquire(area_size); |
375 |
volatile char * fault_address = area + (page_size * 7) / 2; |
376 |
|
377 |
#if defined(TEST_VM_MMAP_ANON) || defined(TEST_VM_MMAP_ANONYMOUS) |
378 |
if (area == VM_MAP_FAILED) |
379 |
return 1; |
380 |
|
381 |
if (vm_release((char *)area, area_size) < 0) |
382 |
return 1; |
383 |
|
384 |
return 0; |
385 |
#endif |
386 |
|
387 |
#if defined(TEST_VM_PROT_NONE_READ) || defined(TEST_VM_PROT_NONE_WRITE) |
388 |
if (area == VM_MAP_FAILED) |
389 |
return 0; |
390 |
|
391 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_NOACCESS) < 0) |
392 |
return 0; |
393 |
#endif |
394 |
|
395 |
#if defined(TEST_VM_PROT_RDWR_WRITE) |
396 |
if (area == VM_MAP_FAILED) |
397 |
return 1; |
398 |
|
399 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) |
400 |
return 1; |
401 |
|
402 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
403 |
return 1; |
404 |
#endif |
405 |
|
406 |
#if defined(TEST_VM_PROT_READ_WRITE) |
407 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) |
408 |
return 0; |
409 |
#endif |
410 |
|
411 |
#if defined(TEST_VM_PROT_NONE_READ) |
412 |
// this should cause a core dump |
413 |
char foo = *fault_address; |
414 |
return 0; |
415 |
#endif |
416 |
|
417 |
#if defined(TEST_VM_PROT_NONE_WRITE) || defined(TEST_VM_PROT_READ_WRITE) |
418 |
// this should cause a core dump |
419 |
*fault_address = 'z'; |
420 |
return 0; |
421 |
#endif |
422 |
|
423 |
#if defined(TEST_VM_PROT_RDWR_WRITE) |
424 |
// this should not cause a core dump |
425 |
*fault_address = 'z'; |
426 |
return 0; |
427 |
#endif |
428 |
} |
429 |
#endif |