Port plat_m(un)map from the VFIO branch

This commit is contained in:
RichardG867
2021-10-16 20:12:49 -03:00
parent e25482db11
commit 5bf8aa2d58
6 changed files with 66 additions and 69 deletions

View File

@@ -105,6 +105,8 @@ extern void plat_path_slash(char *path);
extern int plat_path_abs(char *path); extern int plat_path_abs(char *path);
extern int plat_dir_check(char *path); extern int plat_dir_check(char *path);
extern int plat_dir_create(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 uint64_t plat_timer_read(void);
extern uint32_t plat_get_ticks(void); extern uint32_t plat_get_ticks(void);
extern uint32_t plat_get_micro_ticks(void); extern uint32_t plat_get_micro_ticks(void);

View File

@@ -5,16 +5,6 @@
fbzColorPath fbzColorPath
*/ */
#if defined(__linux__) || defined(__APPLE__)
#include <sys/mman.h>
#include <unistd.h>
#endif
#if _WIN64
#define BITMAP windows_BITMAP
#include <windows.h>
#undef BITMAP
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
#include <intrin.h> #include <intrin.h>
#else #else
@@ -3432,11 +3422,7 @@ void voodoo_codegen_init(voodoo_t *voodoo)
{ {
int c; int c;
#if _WIN64 voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, 1);
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
for (c = 0; c < 256; c++) for (c = 0; c < 256; c++)
{ {
@@ -3462,10 +3448,5 @@ void voodoo_codegen_init(voodoo_t *voodoo)
void voodoo_codegen_close(voodoo_t *voodoo) void voodoo_codegen_close(voodoo_t *voodoo)
{ {
#if _WIN64 plat_munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4);
VirtualFree(voodoo->codegen_data, 0, MEM_RELEASE);
#else
munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4);
#endif
} }

View File

@@ -5,16 +5,6 @@
fbzColorPath fbzColorPath
*/ */
#if defined(__linux__) || defined(__APPLE__)
#include <sys/mman.h>
#include <unistd.h>
#endif
#if defined WIN32 || defined _WIN32 || defined _WIN32
#define BITMAP windows_BITMAP
#include <windows.h>
#undef BITMAP
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
#include <intrin.h> #include <intrin.h>
#else #else
@@ -3378,11 +3368,7 @@ void voodoo_codegen_init(voodoo_t *voodoo)
long pagemask = ~(pagesize - 1); long pagemask = ~(pagesize - 1);
#endif #endif
#if defined WIN32 || defined _WIN32 || defined _WIN32 voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM*4, 1);
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
for (c = 0; c < 256; c++) for (c = 0; c < 256; c++)
{ {
@@ -3408,9 +3394,5 @@ void voodoo_codegen_init(voodoo_t *voodoo)
void voodoo_codegen_close(voodoo_t *voodoo) void voodoo_codegen_close(voodoo_t *voodoo)
{ {
#if defined WIN32 || defined _WIN32 || defined _WIN32 plat_munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4);
VirtualFree(voodoo->codegen_data, 0, MEM_RELEASE);
#else
munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4);
#endif
} }

View File

@@ -34,6 +34,7 @@
#include <86box/config.h> #include <86box/config.h>
#include <86box/io.h> #include <86box/io.h>
#include <86box/mem.h> #include <86box/mem.h>
#include <86box/plat.h>
#include <86box/rom.h> #include <86box/rom.h>
#ifdef USE_DYNAREC #ifdef USE_DYNAREC
# include "codegen_public.h" # 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 uint8_t ff_pccache[4] = { 0xff, 0xff, 0xff, 0xff };
static mem_state_t _mem_state[MEM_MAPPINGS_NO]; static mem_state_t _mem_state[MEM_MAPPINGS_NO];
static uint32_t remap_start_addr; 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 #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; a64[i] = (uint64_t) addr;
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (cr0 >> 31) { if (cr0 >> 31) {
if (write && ((i == 0) || !(addr & 0xfff))) if (write && ((i == 0) || !(addr & 0xfff)))
cond = (!page_lookup[addr >> 12] || !page_lookup[addr >> 12]->write_b); 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]); p = (uint16_t *) &(map->exec[addr - map->base]);
ret = *p; ret = *p;
} else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_HBOUND) && (map && map->read_w)) } 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 { else {
ret = mem_readb_phys(addr + 1) << 8; ret = mem_readb_phys(addr + 1) << 8;
ret |= mem_readb_phys(addr); ret |= mem_readb_phys(addr);
@@ -1682,7 +1688,7 @@ mem_readl_phys(uint32_t addr)
p = (uint32_t *) &(map->exec[addr - map->base]); p = (uint32_t *) &(map->exec[addr - map->base]);
ret = *p; ret = *p;
} else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_QBOUND) && (map && map->read_l)) } 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 { else {
ret = mem_readw_phys(addr + 2) << 16; ret = mem_readw_phys(addr + 2) << 16;
ret |= mem_readw_phys(addr); 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 = (uint16_t *) &(map->exec[addr - map->base]);
*p = val; *p = val;
} else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_HBOUND) && (map && map->write_w)) } 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 { else {
mem_writeb_phys(addr, val & 0xff); mem_writeb_phys(addr, val & 0xff);
mem_writeb_phys(addr + 1, (val >> 8) & 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 = (uint32_t *) &(map->exec[addr - map->base]);
*p = val; *p = val;
} else if (((addr & MEM_GRANULARITY_MASK) <= MEM_GRANULARITY_QBOUND) && (map && map->write_l)) } 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 { else {
mem_writew_phys(addr, val & 0xffff); mem_writew_phys(addr, val & 0xffff);
mem_writew_phys(addr + 2, (val >> 16) & 0xffff); mem_writew_phys(addr + 2, (val >> 16) & 0xffff);
@@ -2606,30 +2612,34 @@ mem_reset(void)
} }
if (ram != NULL) { if (ram != NULL) {
free(ram); plat_munmap(ram, ram_size);
ram = NULL; ram = NULL;
ram_size = 0;
} }
#if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64)) #if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64))
if (ram2 != NULL) { if (ram2 != NULL) {
free(ram2); plat_munmap(ram2, ram2_size);
ram2 = NULL; ram2 = NULL;
ram2_size = 0;
} }
#endif
if (mem_size > 2097152) if (mem_size > 2097152)
fatal("Attempting to use more than 2 GB of emulated RAM\n"); mem_size = 2097152;
#endif
m = 1024UL * mem_size; m = 1024UL * mem_size;
#if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64)) #if (!(defined __amd64__ || defined _M_X64 || defined __aarch64__ || defined _M_ARM64))
if (mem_size > 1048576) { 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) { if (ram == NULL) {
fatal("Failed to allocate primary RAM block. Make sure you have enough RAM available.\n"); fatal("Failed to allocate primary RAM block. Make sure you have enough RAM available.\n");
return; return;
} }
memset(ram, 0x00, (1 << 30)); memset(ram, 0x00, ram_size);
ram2 = (uint8_t *)malloc(m - (1 << 30)); /* allocate and clear the RAM block above 1 GB */ 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 (ram2 == NULL) {
if (config_changed == 2) if (config_changed == 2)
fatal(EMU_NAME " must be restarted for the memory amount change to be applied.\n"); 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"); fatal("Failed to allocate secondary RAM block. Make sure you have enough RAM available.\n");
return; return;
} }
memset(ram2, 0x00, m - (1 << 30)); memset(ram2, 0x00, ram2_size);
} else { } else
ram = (uint8_t *)malloc(m); /* allocate and clear the RAM block */ #endif
{
ram_size = m;
ram = (uint8_t *) plat_mmap(ram_size, 0); /* allocate and clear the RAM block */
if (ram == NULL) { if (ram == NULL) {
fatal("Failed to allocate RAM block. Make sure you have enough RAM available.\n"); fatal("Failed to allocate RAM block. Make sure you have enough RAM available.\n");
return; 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. * Allocate the page table based on how much RAM we have.

View File

@@ -302,7 +302,7 @@ plat_get_basename(const char *path)
while (c > 0) { while (c > 0) {
if (path[c] == '/') if (path[c] == '/')
return((char *)&path[c]); return((char *)&path[c + 1]);
c--; c--;
} }
@@ -366,6 +366,19 @@ plat_dir_create(char *path)
return mkdir(path, S_IRWXU); 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 uint64_t
plat_timer_read(void) plat_timer_read(void)
{ {

View File

@@ -728,7 +728,7 @@ plat_get_basename(const char *path)
while (c > 0) { while (c > 0) {
if (path[c] == '/' || path[c] == '\\') if (path[c] == '/' || path[c] == '\\')
return((char *)&path[c]); return((char *)&path[c + 1]);
c--; 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 uint64_t
plat_timer_read(void) plat_timer_read(void)
{ {