diff --git a/src/include/86box/plat.h b/src/include/86box/plat.h index 95a74f65f..d413add7d 100644 --- a/src/include/86box/plat.h +++ b/src/include/86box/plat.h @@ -105,6 +105,8 @@ extern void plat_path_slash(char *path); extern int plat_path_abs(char *path); extern int plat_dir_check(char *path); extern int plat_dir_create(char *path); +extern void *plat_mmap(size_t size, uint8_t executable); +extern void plat_munmap(void *ptr, size_t size); extern uint64_t plat_timer_read(void); extern uint32_t plat_get_ticks(void); extern uint32_t plat_get_micro_ticks(void); diff --git a/src/include/86box/vid_voodoo_codegen_x86-64.h b/src/include/86box/vid_voodoo_codegen_x86-64.h index 4e6cbcaf2..443edc2cd 100644 --- a/src/include/86box/vid_voodoo_codegen_x86-64.h +++ b/src/include/86box/vid_voodoo_codegen_x86-64.h @@ -5,16 +5,6 @@ fbzColorPath */ -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#endif -#if _WIN64 -#define BITMAP windows_BITMAP -#include -#undef BITMAP -#endif - #ifdef _MSC_VER #include #else @@ -3432,11 +3422,7 @@ void voodoo_codegen_init(voodoo_t *voodoo) { int c; -#if _WIN64 - voodoo->codegen_data = VirtualAlloc(NULL, sizeof(voodoo_x86_data_t) * BLOCK_NUM * 4, MEM_COMMIT, PAGE_EXECUTE_READWRITE); -#else - voodoo->codegen_data = mmap(0, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0); -#endif + voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, 1); for (c = 0; c < 256; c++) { @@ -3462,10 +3448,5 @@ void voodoo_codegen_init(voodoo_t *voodoo) void voodoo_codegen_close(voodoo_t *voodoo) { -#if _WIN64 - VirtualFree(voodoo->codegen_data, 0, MEM_RELEASE); -#else - munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4); -#endif + plat_munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4); } - diff --git a/src/include/86box/vid_voodoo_codegen_x86.h b/src/include/86box/vid_voodoo_codegen_x86.h index f84ee00aa..d54a7e683 100644 --- a/src/include/86box/vid_voodoo_codegen_x86.h +++ b/src/include/86box/vid_voodoo_codegen_x86.h @@ -5,16 +5,6 @@ fbzColorPath */ -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#endif -#if defined WIN32 || defined _WIN32 || defined _WIN32 -#define BITMAP windows_BITMAP -#include -#undef BITMAP -#endif - #ifdef _MSC_VER #include #else @@ -3378,11 +3368,7 @@ void voodoo_codegen_init(voodoo_t *voodoo) long pagemask = ~(pagesize - 1); #endif -#if defined WIN32 || defined _WIN32 || defined _WIN32 - voodoo->codegen_data = VirtualAlloc(NULL, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, MEM_COMMIT, PAGE_EXECUTE_READWRITE); -#else - voodoo->codegen_data = mmap(0, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0); -#endif + voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, 1); for (c = 0; c < 256; c++) { @@ -3408,9 +3394,5 @@ void voodoo_codegen_init(voodoo_t *voodoo) void voodoo_codegen_close(voodoo_t *voodoo) { -#if defined WIN32 || defined _WIN32 || defined _WIN32 - VirtualFree(voodoo->codegen_data, 0, MEM_RELEASE); -#else - munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4); -#endif + plat_munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4); } diff --git a/src/mem/mem.c b/src/mem/mem.c index 4a3a9145f..75ddb8c53 100644 --- a/src/mem/mem.c +++ b/src/mem/mem.c @@ -34,6 +34,7 @@ #include <86box/config.h> #include <86box/io.h> #include <86box/mem.h> +#include <86box/plat.h> #include <86box/rom.h> #ifdef USE_DYNAREC # include "codegen_public.h" @@ -128,6 +129,11 @@ static uint8_t *_mem_exec[MEM_MAPPINGS_NO]; static uint8_t ff_pccache[4] = { 0xff, 0xff, 0xff, 0xff }; static mem_state_t _mem_state[MEM_MAPPINGS_NO]; static uint32_t remap_start_addr; +#if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64)) +static size_t ram_size = 0, ram2_size = 0; +#else +static size_t ram_size = 0; +#endif #ifdef ENABLE_MEM_LOG @@ -1590,7 +1596,7 @@ do_mmutranslate(uint32_t addr, uint32_t *a64, int num, int write) a64[i] = (uint64_t) addr; for (i = 0; i < num; i++) { - if (cr0 >> 31) { + if (cr0 >> 31) { if (write && ((i == 0) || !(addr & 0xfff))) cond = (!page_lookup[addr >> 12] || !page_lookup[addr >> 12]->write_b); @@ -1660,7 +1666,7 @@ mem_readw_phys(uint32_t addr) p = (uint16_t *) &(map->exec[addr - map->base]); ret = *p; } else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_HBOUND) && (map && map->read_w)) - ret = map->read_w(addr, map->p); + ret = map->read_w(addr, map->p); else { ret = mem_readb_phys(addr + 1) << 8; ret |= mem_readb_phys(addr); @@ -1682,7 +1688,7 @@ mem_readl_phys(uint32_t addr) p = (uint32_t *) &(map->exec[addr - map->base]); ret = *p; } else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_QBOUND) && (map && map->read_l)) - ret = map->read_l(addr, map->p); + ret = map->read_l(addr, map->p); else { ret = mem_readw_phys(addr + 2) << 16; ret |= mem_readw_phys(addr); @@ -1740,7 +1746,7 @@ mem_writew_phys(uint32_t addr, uint16_t val) p = (uint16_t *) &(map->exec[addr - map->base]); *p = val; } else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_HBOUND) && (map && map->write_w)) - map->write_w(addr, val, map->p); + map->write_w(addr, val, map->p); else { mem_writeb_phys(addr, val & 0xff); mem_writeb_phys(addr + 1, (val >> 8) & 0xff); @@ -1760,7 +1766,7 @@ mem_writel_phys(uint32_t addr, uint32_t val) p = (uint32_t *) &(map->exec[addr - map->base]); *p = val; } else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_QBOUND) && (map && map->write_l)) - map->write_l(addr, val, map->p); + map->write_l(addr, val, map->p); else { mem_writew_phys(addr, val & 0xffff); mem_writew_phys(addr + 2, (val >> 16) & 0xffff); @@ -2606,30 +2612,34 @@ mem_reset(void) } if (ram != NULL) { - free(ram); + plat_munmap(ram, ram_size); ram = NULL; + ram_size = 0; } #if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64)) if (ram2 != NULL) { - free(ram2); + plat_munmap(ram2, ram2_size); ram2 = NULL; + ram2_size = 0; } -#endif if (mem_size > 2097152) - fatal("Attempting to use more than 2 GB of emulated RAM\n"); + mem_size = 2097152; +#endif m = 1024UL * mem_size; #if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64)) if (mem_size > 1048576) { - ram = (uint8_t *)malloc(1 << 30); /* allocate and clear the RAM block of the first 1 GB */ + ram_size = 1 << 30; + ram = (uint8_t *) plat_mmap(ram_size, 0); /* allocate and clear the RAM block of the first 1 GB */ if (ram == NULL) { fatal("Failed to allocate primary RAM block. Make sure you have enough RAM available.\n"); return; } - memset(ram, 0x00, (1 << 30)); - ram2 = (uint8_t *)malloc(m - (1 << 30)); /* allocate and clear the RAM block above 1 GB */ + memset(ram, 0x00, ram_size); + ram2_size = m - (1 << 30); + ram2 = (uint8_t *) plat_mmap(ram2_size, 0); /* allocate and clear the RAM block above 1 GB */ if (ram2 == NULL) { if (config_changed == 2) fatal(EMU_NAME " must be restarted for the memory amount change to be applied.\n"); @@ -2637,25 +2647,20 @@ mem_reset(void) fatal("Failed to allocate secondary RAM block. Make sure you have enough RAM available.\n"); return; } - memset(ram2, 0x00, m - (1 << 30)); - } else { - ram = (uint8_t *)malloc(m); /* allocate and clear the RAM block */ + memset(ram2, 0x00, ram2_size); + } else +#endif + { + ram_size = m; + ram = (uint8_t *) plat_mmap(ram_size, 0); /* allocate and clear the RAM block */ if (ram == NULL) { fatal("Failed to allocate RAM block. Make sure you have enough RAM available.\n"); return; } - memset(ram, 0x00, m); + memset(ram, 0x00, ram_size); + if (mem_size > 1048576) + ram2 = &(ram[1 << 30]); } -#else - ram = (uint8_t *)malloc(m); /* allocate and clear the RAM block */ - if (ram == NULL) { - fatal("Failed to allocate RAM block. Make sure you have enough RAM available.\n"); - return; - } - memset(ram, 0x00, m); - if (mem_size > 1048576) - ram2 = &(ram[1 << 30]); -#endif /* * Allocate the page table based on how much RAM we have. diff --git a/src/unix/unix.c b/src/unix/unix.c index 0dc966802..d0c3dfa73 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -302,7 +302,7 @@ plat_get_basename(const char *path) while (c > 0) { if (path[c] == '/') - return((char *)&path[c]); + return((char *)&path[c + 1]); c--; } @@ -366,6 +366,19 @@ plat_dir_create(char *path) return mkdir(path, S_IRWXU); } +void * +plat_mmap(size_t size, uint8_t executable) +{ + void *ret = mmap(0, size, PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0), MAP_ANON | MAP_PRIVATE, 0, 0); + return (ret < 0) ? NULL : ret; +} + +void +plat_munmap(void *ptr, size_t size) +{ + munmap(ptr, size); +} + uint64_t plat_timer_read(void) { diff --git a/src/win/win.c b/src/win/win.c index 45d32bd8e..6e397b98f 100644 --- a/src/win/win.c +++ b/src/win/win.c @@ -728,7 +728,7 @@ plat_get_basename(const char *path) while (c > 0) { if (path[c] == '/' || path[c] == '\\') - return((char *)&path[c]); + return((char *)&path[c + 1]); c--; } @@ -858,6 +858,20 @@ plat_dir_create(char *path) } +void * +plat_mmap(size_t size, uint8_t executable) +{ + return VirtualAlloc(NULL, size, MEM_COMMIT, executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE); +} + + +void +plat_munmap(void *ptr, size_t size) +{ + VirtualFree(ptr, 0, MEM_RELEASE); +} + + uint64_t plat_timer_read(void) {