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-2005 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 |
/* We want MAP_32BIT, if available, for SheepShaver and BasiliskII |
52 |
because the emulated target is 32-bit and this helps to allocate |
53 |
memory so that branches could be resolved more easily (32-bit |
54 |
displacement to code in .text), on AMD64 for example. */ |
55 |
#ifndef MAP_32BIT |
56 |
#define MAP_32BIT 0 |
57 |
#endif |
58 |
#ifndef MAP_ANON |
59 |
#define MAP_ANON 0 |
60 |
#endif |
61 |
#ifndef MAP_ANONYMOUS |
62 |
#define MAP_ANONYMOUS 0 |
63 |
#endif |
64 |
|
65 |
#define MAP_EXTRA_FLAGS (MAP_32BIT) |
66 |
|
67 |
#ifdef HAVE_MMAP_VM |
68 |
#if (defined(__linux__) && defined(__i386__)) || HAVE_LINKER_SCRIPT |
69 |
/* Force a reasonnable address below 0x80000000 on x86 so that we |
70 |
don't get addresses above when the program is run on AMD64. |
71 |
NOTE: this is empirically determined on Linux/x86. */ |
72 |
#define MAP_BASE 0x10000000 |
73 |
#else |
74 |
#define MAP_BASE 0x00000000 |
75 |
#endif |
76 |
static char * next_address = (char *)MAP_BASE; |
77 |
#ifdef HAVE_MMAP_ANON |
78 |
#define map_flags (MAP_ANON | MAP_EXTRA_FLAGS) |
79 |
#define zero_fd -1 |
80 |
#else |
81 |
#ifdef HAVE_MMAP_ANONYMOUS |
82 |
#define map_flags (MAP_ANONYMOUS | MAP_EXTRA_FLAGS) |
83 |
#define zero_fd -1 |
84 |
#else |
85 |
#define map_flags (MAP_EXTRA_FLAGS) |
86 |
static int zero_fd = -1; |
87 |
#endif |
88 |
#endif |
89 |
#endif |
90 |
|
91 |
/* Utility functions for POSIX SHM handling. */ |
92 |
|
93 |
#ifdef USE_33BIT_ADDRESSING |
94 |
struct shm_range_t { |
95 |
const char *file; |
96 |
void *base; |
97 |
unsigned int size; |
98 |
shm_range_t *next; |
99 |
}; |
100 |
|
101 |
static shm_range_t *shm_ranges = NULL; |
102 |
|
103 |
static bool add_shm_range(const char *file, void *base, unsigned int size) |
104 |
{ |
105 |
shm_range_t *r = (shm_range_t *)malloc(sizeof(shm_range_t)); |
106 |
if (r) { |
107 |
r->file = file; |
108 |
r->base = base; |
109 |
r->size = size; |
110 |
r->next = shm_ranges ? shm_ranges : NULL; |
111 |
shm_ranges = r; |
112 |
return true; |
113 |
} |
114 |
return false; |
115 |
} |
116 |
|
117 |
static shm_range_t *find_shm_range(void *base, unsigned int size) |
118 |
{ |
119 |
for (shm_range_t *r = shm_ranges; r != NULL; r = r->next) |
120 |
if (r->base == base && r->size == size) |
121 |
return r; |
122 |
return NULL; |
123 |
} |
124 |
|
125 |
static bool remove_shm_range(shm_range_t *r) |
126 |
{ |
127 |
if (r) { |
128 |
for (shm_range_t *p = shm_ranges; p != NULL; p = p->next) { |
129 |
if (p->next == r) { |
130 |
p->next = r->next; |
131 |
free(r); |
132 |
return true; |
133 |
} |
134 |
} |
135 |
} |
136 |
return false; |
137 |
} |
138 |
|
139 |
static bool remove_shm_range(void *base, unsigned int size) |
140 |
{ |
141 |
return remove_shm_range(find_shm_range(base, size)); |
142 |
} |
143 |
#endif |
144 |
|
145 |
/* Build a POSIX SHM memory segment file descriptor name. */ |
146 |
|
147 |
#ifdef USE_33BIT_ADDRESSING |
148 |
static const char *build_shm_filename(void) |
149 |
{ |
150 |
static int id = 0; |
151 |
static char filename[PATH_MAX]; |
152 |
|
153 |
int ret = snprintf(filename, sizeof(filename), "/BasiliskII-%d-shm-%d", getpid(), id); |
154 |
if (ret == -1 || ret >= sizeof(filename)) |
155 |
return NULL; |
156 |
|
157 |
id++; |
158 |
return filename; |
159 |
} |
160 |
#endif |
161 |
|
162 |
/* Translate generic VM map flags to host values. */ |
163 |
|
164 |
#ifdef HAVE_MMAP_VM |
165 |
static int translate_map_flags(int vm_flags) |
166 |
{ |
167 |
int flags = 0; |
168 |
if (vm_flags & VM_MAP_SHARED) |
169 |
flags |= MAP_SHARED; |
170 |
if (vm_flags & VM_MAP_PRIVATE) |
171 |
flags |= MAP_PRIVATE; |
172 |
if (vm_flags & VM_MAP_FIXED) |
173 |
flags |= MAP_FIXED; |
174 |
if (vm_flags & VM_MAP_32BIT) |
175 |
flags |= MAP_32BIT; |
176 |
return flags; |
177 |
} |
178 |
#endif |
179 |
|
180 |
/* Align ADDR and SIZE to 64K boundaries. */ |
181 |
|
182 |
#ifdef HAVE_WIN32_VM |
183 |
static inline LPVOID align_addr_segment(LPVOID addr) |
184 |
{ |
185 |
return (LPVOID)(((DWORD)addr) & -65536); |
186 |
} |
187 |
|
188 |
static inline DWORD align_size_segment(LPVOID addr, DWORD size) |
189 |
{ |
190 |
return size + ((DWORD)addr - (DWORD)align_addr_segment(addr)); |
191 |
} |
192 |
#endif |
193 |
|
194 |
/* Translate generic VM prot flags to host values. */ |
195 |
|
196 |
#ifdef HAVE_WIN32_VM |
197 |
static int translate_prot_flags(int prot_flags) |
198 |
{ |
199 |
int prot = PAGE_READWRITE; |
200 |
if (prot_flags == (VM_PAGE_EXECUTE | VM_PAGE_READ | VM_PAGE_WRITE)) |
201 |
prot = PAGE_EXECUTE_READWRITE; |
202 |
else if (prot_flags == (VM_PAGE_EXECUTE | VM_PAGE_READ)) |
203 |
prot = PAGE_EXECUTE_READ; |
204 |
else if (prot_flags == (VM_PAGE_READ | VM_PAGE_WRITE)) |
205 |
prot = PAGE_READWRITE; |
206 |
else if (prot_flags == VM_PAGE_READ) |
207 |
prot = PAGE_READONLY; |
208 |
else if (prot_flags == 0) |
209 |
prot = PAGE_NOACCESS; |
210 |
return prot; |
211 |
} |
212 |
#endif |
213 |
|
214 |
/* Initialize the VM system. Returns 0 if successful, -1 for errors. */ |
215 |
|
216 |
int vm_init(void) |
217 |
{ |
218 |
#ifdef HAVE_MMAP_VM |
219 |
#ifndef zero_fd |
220 |
zero_fd = open("/dev/zero", O_RDWR); |
221 |
if (zero_fd < 0) |
222 |
return -1; |
223 |
#endif |
224 |
#endif |
225 |
return 0; |
226 |
} |
227 |
|
228 |
/* Deallocate all internal data used to wrap virtual memory allocators. */ |
229 |
|
230 |
void vm_exit(void) |
231 |
{ |
232 |
#ifdef HAVE_MMAP_VM |
233 |
#ifndef zero_fd |
234 |
if (zero_fd != -1) { |
235 |
close(zero_fd); |
236 |
zero_fd = -1; |
237 |
} |
238 |
#endif |
239 |
#endif |
240 |
} |
241 |
|
242 |
/* Allocate zero-filled memory of SIZE bytes. The mapping is private |
243 |
and default protection bits are read / write. The return value |
244 |
is the actual mapping address chosen or VM_MAP_FAILED for errors. */ |
245 |
|
246 |
void * vm_acquire(size_t size, int options) |
247 |
{ |
248 |
void * addr; |
249 |
|
250 |
// VM_MAP_FIXED are to be used with vm_acquire_fixed() only |
251 |
if (options & VM_MAP_FIXED) |
252 |
return VM_MAP_FAILED; |
253 |
|
254 |
#ifdef HAVE_MACH_VM |
255 |
// vm_allocate() returns a zero-filled memory region |
256 |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, TRUE) != KERN_SUCCESS) |
257 |
return VM_MAP_FAILED; |
258 |
#else |
259 |
#ifdef HAVE_MMAP_VM |
260 |
int fd = zero_fd; |
261 |
int the_map_flags = translate_map_flags(options) | map_flags; |
262 |
|
263 |
#ifdef USE_33BIT_ADDRESSING |
264 |
const char *shm_file = NULL; |
265 |
if (sizeof(void *) == 8 && (options & VM_MAP_33BIT)) { |
266 |
the_map_flags &= ~(MAP_PRIVATE | MAP_ANON | MAP_ANONYMOUS); |
267 |
the_map_flags |= MAP_SHARED; |
268 |
|
269 |
if ((shm_file = build_shm_filename()) == NULL) |
270 |
return VM_MAP_FAILED; |
271 |
|
272 |
if ((fd = shm_open(shm_file, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0) |
273 |
return VM_MAP_FAILED; |
274 |
|
275 |
if (ftruncate(fd, size) < 0) |
276 |
return VM_MAP_FAILED; |
277 |
} |
278 |
#endif |
279 |
|
280 |
if ((addr = mmap((caddr_t)next_address, size, VM_PAGE_DEFAULT, the_map_flags, fd, 0)) == (void *)MAP_FAILED) |
281 |
return VM_MAP_FAILED; |
282 |
|
283 |
// Sanity checks for 64-bit platforms |
284 |
if (sizeof(void *) == 8 && (options & VM_MAP_32BIT) && !((char *)addr <= (char *)0xffffffff)) |
285 |
return VM_MAP_FAILED; |
286 |
|
287 |
next_address = (char *)addr + size; |
288 |
|
289 |
// Remap to 33-bit space |
290 |
#ifdef USE_33BIT_ADDRESSING |
291 |
if (sizeof(void *) == 8 && (options & VM_MAP_33BIT)) { |
292 |
if (!add_shm_range(strdup(shm_file), addr, size)) |
293 |
return VM_MAP_FAILED; |
294 |
|
295 |
if (mmap((char *)addr + (1L << 32), size, VM_PAGE_DEFAULT, the_map_flags | MAP_FIXED, fd, 0) == (void *)MAP_FAILED) |
296 |
return VM_MAP_FAILED; |
297 |
close(fd); |
298 |
} |
299 |
#endif |
300 |
#else |
301 |
#ifdef HAVE_WIN32_VM |
302 |
if ((addr = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == NULL) |
303 |
return VM_MAP_FAILED; |
304 |
#else |
305 |
if ((addr = calloc(size, 1)) == 0) |
306 |
return VM_MAP_FAILED; |
307 |
|
308 |
// Omit changes for protections because they are not supported in this mode |
309 |
return addr; |
310 |
#endif |
311 |
#endif |
312 |
#endif |
313 |
|
314 |
// Explicitely protect the newly mapped region here because on some systems, |
315 |
// say MacOS X, mmap() doesn't honour the requested protection flags. |
316 |
if (vm_protect(addr, size, VM_PAGE_DEFAULT) != 0) |
317 |
return VM_MAP_FAILED; |
318 |
|
319 |
return addr; |
320 |
} |
321 |
|
322 |
/* Allocate zero-filled memory at exactly ADDR (which must be page-aligned). |
323 |
Retuns 0 if successful, -1 on errors. */ |
324 |
|
325 |
int vm_acquire_fixed(void * addr, size_t size, int options) |
326 |
{ |
327 |
// Fixed mappings are required to be private |
328 |
if (options & VM_MAP_SHARED) |
329 |
return -1; |
330 |
|
331 |
#ifdef HAVE_MACH_VM |
332 |
// vm_allocate() returns a zero-filled memory region |
333 |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, 0) != KERN_SUCCESS) |
334 |
return -1; |
335 |
#else |
336 |
#ifdef HAVE_MMAP_VM |
337 |
int fd = zero_fd; |
338 |
int the_map_flags = translate_map_flags(options) | map_flags | MAP_FIXED; |
339 |
|
340 |
#ifdef USE_33BIT_ADDRESSING |
341 |
const char *shm_file = NULL; |
342 |
if (sizeof(void *) == 8 && (options & VM_MAP_33BIT)) { |
343 |
the_map_flags &= ~(MAP_PRIVATE | MAP_ANON | MAP_ANONYMOUS); |
344 |
the_map_flags |= MAP_SHARED; |
345 |
|
346 |
if ((shm_file = build_shm_filename()) == NULL) |
347 |
return -1; |
348 |
|
349 |
if ((fd = shm_open(shm_file, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0) |
350 |
return -1; |
351 |
|
352 |
if (ftruncate(fd, size) < 0) |
353 |
return -1; |
354 |
} |
355 |
#endif |
356 |
|
357 |
if (mmap((caddr_t)addr, size, VM_PAGE_DEFAULT, the_map_flags, fd, 0) == (void *)MAP_FAILED) |
358 |
return -1; |
359 |
|
360 |
// Remap to 33-bit space |
361 |
#ifdef USE_33BIT_ADDRESSING |
362 |
if (sizeof(void *) == 8 && (options & VM_MAP_33BIT)) { |
363 |
if (!add_shm_range(strdup(shm_file), addr, size)) |
364 |
return -1; |
365 |
|
366 |
if (mmap((char *)addr + (1L << 32), size, VM_PAGE_DEFAULT, the_map_flags, fd, 0) == (void *)MAP_FAILED) |
367 |
return -1; |
368 |
close(fd); |
369 |
} |
370 |
#endif |
371 |
#else |
372 |
#ifdef HAVE_WIN32_VM |
373 |
// Windows cannot allocate Low Memory |
374 |
if (addr == NULL) |
375 |
return -1; |
376 |
|
377 |
// Allocate a possibly offset region to align on 64K boundaries |
378 |
LPVOID req_addr = align_addr_segment(addr); |
379 |
DWORD req_size = align_size_segment(addr, size); |
380 |
LPVOID ret_addr = VirtualAlloc(req_addr, req_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); |
381 |
if (ret_addr != req_addr) |
382 |
return -1; |
383 |
#else |
384 |
// Unsupported |
385 |
return -1; |
386 |
#endif |
387 |
#endif |
388 |
#endif |
389 |
|
390 |
// Explicitely protect the newly mapped region here because on some systems, |
391 |
// say MacOS X, mmap() doesn't honour the requested protection flags. |
392 |
if (vm_protect(addr, size, VM_PAGE_DEFAULT) != 0) |
393 |
return -1; |
394 |
|
395 |
return 0; |
396 |
} |
397 |
|
398 |
/* Deallocate any mapping for the region starting at ADDR and extending |
399 |
LEN bytes. Returns 0 if successful, -1 on errors. */ |
400 |
|
401 |
int vm_release(void * addr, size_t size) |
402 |
{ |
403 |
// Safety check: don't try to release memory that was not allocated |
404 |
if (addr == VM_MAP_FAILED) |
405 |
return 0; |
406 |
|
407 |
#ifdef HAVE_MACH_VM |
408 |
if (vm_deallocate(mach_task_self(), (vm_address_t)addr, size) != KERN_SUCCESS) |
409 |
return -1; |
410 |
#else |
411 |
#ifdef HAVE_MMAP_VM |
412 |
if (munmap((caddr_t)addr, size) != 0) |
413 |
return -1; |
414 |
|
415 |
#ifdef USE_33BIT_ADDRESSING |
416 |
shm_range_t *r = find_shm_range(addr, size); |
417 |
if (r) { |
418 |
if (munmap((char *)r->base + (1L << 32), size) != 0) |
419 |
return -1; |
420 |
|
421 |
if (shm_unlink(r->file) < 0) |
422 |
return -1; |
423 |
free((char *)r->file); |
424 |
|
425 |
if (!remove_shm_range(r)) |
426 |
return -1; |
427 |
} |
428 |
#endif |
429 |
#else |
430 |
#ifdef HAVE_WIN32_VM |
431 |
if (VirtualFree(align_addr_segment(addr), 0, MEM_RELEASE) == 0) |
432 |
return -1; |
433 |
#else |
434 |
free(addr); |
435 |
#endif |
436 |
#endif |
437 |
#endif |
438 |
|
439 |
return 0; |
440 |
} |
441 |
|
442 |
/* Change the memory protection of the region starting at ADDR and |
443 |
extending LEN bytes to PROT. Returns 0 if successful, -1 for errors. */ |
444 |
|
445 |
int vm_protect(void * addr, size_t size, int prot) |
446 |
{ |
447 |
#ifdef HAVE_MACH_VM |
448 |
int ret_code = vm_protect(mach_task_self(), (vm_address_t)addr, size, 0, prot); |
449 |
return ret_code == KERN_SUCCESS ? 0 : -1; |
450 |
#else |
451 |
#ifdef HAVE_MMAP_VM |
452 |
int ret_code = mprotect((caddr_t)addr, size, prot); |
453 |
return ret_code == 0 ? 0 : -1; |
454 |
#else |
455 |
#ifdef HAVE_WIN32_VM |
456 |
DWORD old_prot; |
457 |
int ret_code = VirtualProtect(addr, size, translate_prot_flags(prot), &old_prot); |
458 |
return ret_code != 0 ? 0 : -1; |
459 |
#else |
460 |
// Unsupported |
461 |
return -1; |
462 |
#endif |
463 |
#endif |
464 |
#endif |
465 |
} |
466 |
|
467 |
/* Returns the size of a page. */ |
468 |
|
469 |
int vm_get_page_size(void) |
470 |
{ |
471 |
#ifdef HAVE_WIN32_VM |
472 |
static unsigned long page_size = 0; |
473 |
if (page_size == 0) { |
474 |
SYSTEM_INFO si; |
475 |
GetSystemInfo(&si); |
476 |
page_size = si.dwAllocationGranularity; |
477 |
} |
478 |
return page_size; |
479 |
#else |
480 |
return getpagesize(); |
481 |
#endif |
482 |
} |
483 |
|
484 |
#ifdef CONFIGURE_TEST_VM_MAP |
485 |
#include <stdlib.h> |
486 |
#include <signal.h> |
487 |
|
488 |
static void fault_handler(int sig) |
489 |
{ |
490 |
exit(1); |
491 |
} |
492 |
|
493 |
/* Tests covered here: |
494 |
- TEST_VM_PROT_* program slices actually succeeds when a crash occurs |
495 |
- TEST_VM_MAP_ANON* program slices succeeds when it could be compiled |
496 |
*/ |
497 |
int main(void) |
498 |
{ |
499 |
vm_init(); |
500 |
|
501 |
signal(SIGSEGV, fault_handler); |
502 |
#ifdef SIGBUS |
503 |
signal(SIGBUS, fault_handler); |
504 |
#endif |
505 |
|
506 |
#define page_align(address) ((char *)((unsigned long)(address) & -page_size)) |
507 |
unsigned long page_size = vm_get_page_size(); |
508 |
|
509 |
const int area_size = 6 * page_size; |
510 |
volatile char * area = (volatile char *) vm_acquire(area_size); |
511 |
volatile char * fault_address = area + (page_size * 7) / 2; |
512 |
|
513 |
#if defined(TEST_VM_MMAP_ANON) || defined(TEST_VM_MMAP_ANONYMOUS) |
514 |
if (area == VM_MAP_FAILED) |
515 |
return 1; |
516 |
|
517 |
if (vm_release((char *)area, area_size) < 0) |
518 |
return 1; |
519 |
|
520 |
return 0; |
521 |
#endif |
522 |
|
523 |
#if defined(TEST_VM_PROT_NONE_READ) || defined(TEST_VM_PROT_NONE_WRITE) |
524 |
if (area == VM_MAP_FAILED) |
525 |
return 0; |
526 |
|
527 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_NOACCESS) < 0) |
528 |
return 0; |
529 |
#endif |
530 |
|
531 |
#if defined(TEST_VM_PROT_RDWR_WRITE) |
532 |
if (area == VM_MAP_FAILED) |
533 |
return 1; |
534 |
|
535 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) |
536 |
return 1; |
537 |
|
538 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) |
539 |
return 1; |
540 |
#endif |
541 |
|
542 |
#if defined(TEST_VM_PROT_READ_WRITE) |
543 |
if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) |
544 |
return 0; |
545 |
#endif |
546 |
|
547 |
#if defined(TEST_VM_PROT_NONE_READ) |
548 |
// this should cause a core dump |
549 |
char foo = *fault_address; |
550 |
return 0; |
551 |
#endif |
552 |
|
553 |
#if defined(TEST_VM_PROT_NONE_WRITE) || defined(TEST_VM_PROT_READ_WRITE) |
554 |
// this should cause a core dump |
555 |
*fault_address = 'z'; |
556 |
return 0; |
557 |
#endif |
558 |
|
559 |
#if defined(TEST_VM_PROT_RDWR_WRITE) |
560 |
// this should not cause a core dump |
561 |
*fault_address = 'z'; |
562 |
return 0; |
563 |
#endif |
564 |
} |
565 |
#endif |