annotate mem.c @ 2496:187bc857a76a default tip

Fix bug in MED mapper protection bit implementation
author Michael Pavone <pavone@retrodev.com>
date Sun, 28 Apr 2024 23:33:11 -0700
parents 7d851046f035
children
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
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
3 This file is part of BlastEm.
467
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"
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
15 #include "arena.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
16 #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
17 #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
18 #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
19
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
20 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
21 {
2406
8e86cd581620 Better implementation of alloc_code for ASLR/libretro cases that also hopefully works on modern Mac OS
Michael Pavone <pavone@retrodev.com>
parents: 1340
diff changeset
22 //start at 1GB above compiled code to allow plenty of room for sbrk based malloc implementations
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
23 //while still keeping well within 32-bit displacement range for calling code compiled into the executable
2406
8e86cd581620 Better implementation of alloc_code for ASLR/libretro cases that also hopefully works on modern Mac OS
Michael Pavone <pavone@retrodev.com>
parents: 1340
diff changeset
24 static uint8_t *next = ((uint8_t *)alloc_code) + 0x40000000;
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
25 uint8_t *ret = try_alloc_arena();
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
26 if (ret) {
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
27 return ret;
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
28 }
1340
101b5ce682fe Fix some inconsequential issues in code for executable memory allocation noticed while tracking down a different issue
Michael Pavone <pavone@retrodev.com>
parents: 904
diff changeset
29 if (*size & (PAGE_SIZE -1)) {
101b5ce682fe Fix some inconsequential issues in code for executable memory allocation noticed while tracking down a different issue
Michael Pavone <pavone@retrodev.com>
parents: 904
diff changeset
30 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1));
101b5ce682fe Fix some inconsequential issues in code for executable memory allocation noticed while tracking down a different issue
Michael Pavone <pavone@retrodev.com>
parents: 904
diff changeset
31 }
2406
8e86cd581620 Better implementation of alloc_code for ASLR/libretro cases that also hopefully works on modern Mac OS
Michael Pavone <pavone@retrodev.com>
parents: 1340
diff changeset
32 ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
784
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
33 if (ret == MAP_FAILED) {
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
34 perror("alloc_code");
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
35 return NULL;
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
36 }
904
6bafe1988e8c Actually call track_block in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 883
diff changeset
37 track_block(ret);
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
38 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
39 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
40 }
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
41