annotate mem.c @ 832:0433fdd9ba66

Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
author Michael Pavone <pavone@retrodev.com>
date Mon, 19 Oct 2015 19:16:28 -0700
parents c3e3a0d734e2
children 9f149f0e98b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
2 Copyright 2013 Michael Pavone
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
3 This file is part of BlastEm.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
5 */
14
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include <sys/mman.h>
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <stddef.h>
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
8 #include <stdint.h>
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
9 #include <stdlib.h>
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
10 #include <unistd.h>
784
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
11 #include <errno.h>
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
12 #include <stdio.h>
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
13
14
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 #include "mem.h"
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
15 #ifndef MAP_ANONYMOUS
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
16 #define MAP_ANONYMOUS MAP_ANON
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
17 #endif
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
18
759
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
19 #ifndef MAP_32BIT
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
20 #define MAP_32BIT 0
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
21 #endif
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
22
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
23 void * alloc_code(size_t *size)
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
24 {
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
25 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
26 //while still keeping well within 32-bit displacement range for calling code compiled into the executable
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
27 static uint8_t *next = (uint8_t *)0x40000000;
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
28 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1));
759
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
29 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
784
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
30 if (ret == MAP_FAILED) {
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
31 perror("alloc_code");
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
32 return NULL;
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
33 }
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
34 next = ret + *size;
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
35 return ret;
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
36 }
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
37