about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-05-05 13:49:06 +0200
committerptitSeb <sebastien.chev@gmail.com>2023-05-05 13:49:06 +0200
commit49dfbee2119ce52600f7acbf61af690d6c9ef16c (patch)
tree5cc28c28631fe8ac7736eea8a95223a0814ebcae
parent3fc3875e82402f788c85db9de16851570d69346f (diff)
downloadbox64-49dfbee2119ce52600f7acbf61af690d6c9ef16c.tar.gz
box64-49dfbee2119ce52600f7acbf61af690d6c9ef16c.zip
Improved handling of x86_64 47bits memory (should help #763)
-rw-r--r--src/custommem.c25
-rwxr-xr-xsrc/elfs/elfloader.c2
-rwxr-xr-xsrc/wrapped/wrappedlibc.c6
3 files changed, 29 insertions, 4 deletions
diff --git a/src/custommem.c b/src/custommem.c
index f77b005c..7214aa8c 100644
--- a/src/custommem.c
+++ b/src/custommem.c
@@ -1364,6 +1364,30 @@ static void atfork_child_custommem(void)
     init_mutexes();
 }
 
+void reserveHighMem()
+{
+    intptr_t cur = 1LL<<47;
+    mapmem_t* m = mapmem;
+    while(m && (m->end<cur)) {
+        m = m->next;
+    }
+    while (m) {
+        uintptr_t addr = 0, end = 0;
+        if(m->begin>cur) {
+            void* ret = mmap64((void*)cur, m->begin-cur, 0, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
+            printf_log(LOG_DEBUG, "Reserve %p(0x%zx) => %p (%s)\n", (void*)cur, m->begin-cur, ret, strerror(errno));
+            if(ret!=(void*)-1) {
+                addr = cur;
+                end = m->begin;
+            }
+        }
+        cur = m->end + 1;
+        m = m->next;
+        if(addr)
+            addMapMem(addr, end);
+    }
+}
+
 void init_custommem_helper(box64context_t* ctx)
 {
     (void)ctx;
@@ -1392,6 +1416,7 @@ void init_custommem_helper(box64context_t* ctx)
     mapmem->begin = 0x0;
     mapmem->end = (uintptr_t)LOWEST - 1;
     loadProtectionFromMap();
+    reserveHighMem();
     // check if PageSize is correctly defined
     if(box64_pagesize != (1<<MEMPROT_SHIFT)) {
         printf_log(LOG_NONE, "Error: PageSize configuration is wrong: configured with %d, but got %zd\n", 1<<MEMPROT_SHIFT, box64_pagesize);
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index 388372bf..310bb2f7 100755
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -258,7 +258,7 @@ int AllocElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
     } else {
         // vaddr is 0, load everything has a One block
         uintptr_t old_offs = offs;
-        if(!offs && box64_wine)
+        if(!offs /*&& box64_wine*/)
             offs = (uintptr_t)find47bitBlock(head->memsz); // limit to 47bits...
         printf_log(log_level, "Allocating 0x%zx memory @%p for Elf \"%s\"\n", head->memsz, (void*)offs, head->name);
         void* p = mmap((void*)offs, head->memsz
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index cc68c216..3f4e7a09 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -2590,7 +2590,7 @@ EXPORT void* my_mremap(x64emu_t* emu, void* old_addr, size_t old_size, size_t ne
             freeProtection((uintptr_t)ret+new_size, old_size-new_size);
             #ifdef DYNAREC
             if(box64_dynarec)
-                cleanDBFromAddressRange((uintptr_t)ret+new_size, new_size-old_size, 1);
+                cleanDBFromAddressRange((uintptr_t)ret+new_size, old_size-new_size, 1);
             #endif
         } else if(!old_size) {
             setProtection((uintptr_t)ret, new_size, prot);
@@ -2644,14 +2644,14 @@ EXPORT int my_mprotect(x64emu_t* emu, void *addr, unsigned long len, int prot)
         prot|=PROT_READ;    // PROT_READ is implicit with PROT_WRITE on x86_64
     int ret = mprotect(addr, len, prot);
     #ifdef DYNAREC
-    if(box64_dynarec && !ret) {
+    if(box64_dynarec && !ret && len) {
         if(prot& PROT_EXEC)
             addDBFromAddressRange((uintptr_t)addr, len);
         else
             cleanDBFromAddressRange((uintptr_t)addr, len, 0);
     }
     #endif
-    if(!ret)
+    if(!ret && len)
         updateProtection((uintptr_t)addr, len, prot);
     return ret;
 }