about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2025-04-11 18:26:51 +0800
committerGitHub <noreply@github.com>2025-04-11 12:26:51 +0200
commit6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2 (patch)
treebf7552c358c240e44e53c176db44bad11a415de3 /src/tools
parentfa85d4d900c3e03b69bdea65204b51151fc62114 (diff)
downloadbox64-6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2.tar.gz
box64-6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2.zip
[WOW64] Splitted freq and cleanup functions from x64emu (#2521)
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/cleanup.c68
-rw-r--r--src/tools/my_cpuid.c33
2 files changed, 85 insertions, 16 deletions
diff --git a/src/tools/cleanup.c b/src/tools/cleanup.c
new file mode 100644
index 00000000..79ac3451
--- /dev/null
+++ b/src/tools/cleanup.c
@@ -0,0 +1,68 @@
+#include <string.h>
+
+#include "cleanup.h"
+#include "elfs/elfloader_private.h"
+#include "box64context.h"
+#include "debug.h"
+#include "callback.h"
+
+typedef struct cleanup_s {
+    void*       f;
+    int         arg;
+    void*       a;
+} cleanup_t;
+
+void AddCleanup(x64emu_t *emu, void *p)
+{
+    (void)emu;
+
+    if(my_context->clean_sz == my_context->clean_cap) {
+        my_context->clean_cap += 32;
+        my_context->cleanups = (cleanup_t*)box_realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
+    }
+    my_context->cleanups[my_context->clean_sz].arg = 0;
+    my_context->cleanups[my_context->clean_sz].a = NULL;
+    my_context->cleanups[my_context->clean_sz++].f = p;
+}
+
+void AddCleanup1Arg(x64emu_t *emu, void *p, void* a, elfheader_t* h)
+{
+    (void)emu;
+    if(!h)
+        return;
+
+    if(h->clean_sz == h->clean_cap) {
+        h->clean_cap += 32;
+        h->cleanups = (cleanup_t*)box_realloc(h->cleanups, sizeof(cleanup_t)*h->clean_cap);
+    }
+    h->cleanups[h->clean_sz].arg = 1;
+    h->cleanups[h->clean_sz].a = a;
+    h->cleanups[h->clean_sz++].f = p;
+}
+
+void CallCleanup(x64emu_t *emu, elfheader_t* h)
+{
+    printf_log(LOG_DEBUG, "Calling atexit registered functions for elf: %p/%s\n", h, h?h->name:"(nil)");
+    if(!h)
+        return;
+    for(int i=h->clean_sz-1; i>=0; --i) {
+        printf_log(LOG_DEBUG, "Call cleanup #%d (args:%d, arg:%p)\n", i, h->cleanups[i].arg, h->cleanups[i].a);
+        RunFunctionWithEmu(emu, 0, (uintptr_t)(h->cleanups[i].f), h->cleanups[i].arg, h->cleanups[i].a );
+        // now remove the cleanup
+        if(i!=h->clean_sz-1)
+            memmove(h->cleanups+i, h->cleanups+i+1, (h->clean_sz-i-1)*sizeof(cleanup_t));
+        --h->clean_sz;
+    }
+}
+
+void CallAllCleanup(x64emu_t *emu)
+{
+    printf_log(LOG_DEBUG, "Calling atexit registered functions\n");
+    for(int i=my_context->clean_sz-1; i>=0; --i) {
+        printf_log(LOG_DEBUG, "Call cleanup #%d\n", i);
+        --my_context->clean_sz;
+        RunFunctionWithEmu(emu, 0, (uintptr_t)(my_context->cleanups[i].f), my_context->cleanups[i].arg, my_context->cleanups[i].a );
+    }
+    box_free(my_context->cleanups);
+    my_context->cleanups = NULL;
+}
\ No newline at end of file
diff --git a/src/tools/my_cpuid.c b/src/tools/my_cpuid.c
index 5c97c7bc..df505ac0 100644
--- a/src/tools/my_cpuid.c
+++ b/src/tools/my_cpuid.c
@@ -1,4 +1,4 @@
-#define _GNU_SOURCE 
+#define _GNU_SOURCE
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -8,6 +8,7 @@
 #include "../emu/x64emu_private.h"
 #include "debug.h"
 #include "x64emu.h"
+#include "freq.h"
 
 int get_cpuMhz()
 {
@@ -36,7 +37,7 @@ int get_cpuMhz()
             fclose(f);
             ++cpucore;
         }
-        else 
+        else
             cpucore = -1;
     }
     #ifndef STATICBUILD
@@ -268,7 +269,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
                 if(cpu<0) cpu=0;
                 R_EAX |= cpu<<24;
             }*/
-            R_EDX =   1         // fpu 
+            R_EDX =   1         // fpu
                     | 1<<1      // vme
                     | 1<<2      // debugging extension
                     | 1<<3      // pse
@@ -311,7 +312,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
                     | BOX64ENV(avx)<<29 // F16C
                     | BOX64ENV(avx2)<<30     // RDRAND
                     | 0<<31     // Hypervisor guest running
-                    ; 
+                    ;
             break;
         case 0x2:
             if(BOX64ENV(cputype)) {
@@ -325,7 +326,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
                 R_EDX = 0x007A7000;
             }
             break;
-        
+
         case 0x4:
             if(BOX64ENV(cputype)) {
                 // reserved
@@ -381,8 +382,8 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             // extended bits...
             if(R_ECX==0) {
                 R_EAX = 0;
-                R_EBX = 
-                        BOX64ENV(avx)<<3 |  // BMI1 
+                R_EBX =
+                        BOX64ENV(avx)<<3 |  // BMI1
                         BOX64ENV(avx2)<<5 |  //AVX2
                         (BOX64ENV(cputype)?0:1)<<6 | // FDP_EXCPTN_ONLY
                         1<<7 | // SMEP
@@ -396,7 +397,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
                         1<<24 | // CLWB
                         BOX64ENV(shaext)<<29|  // SHA extension
                         0;
-                R_RCX = 
+                R_RCX =
                         BOX64ENV(avx)<<9   | //VAES
                         BOX64ENV(avx2)<<10 | //VPCLMULQDQ.
                         1<<22 | // RDPID
@@ -460,7 +461,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             } else {
                 //L3 Cache
                 switch(R_ECX) {
-                    case 0: 
+                    case 0:
                         R_EAX = 0;
                         R_EBX = 0; // maximum range of RMID of physical processor
                         R_ECX = 0;
@@ -543,8 +544,8 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
                         //| 1<<10     // IBS
                         //| 1<<11     // XOP
                         //| 1<<16     // FMA4
-                        ; 
-                R_EDX =   1         // fpu 
+                        ;
+                R_EDX =   1         // fpu
                         | 1<<2      // debugging extension
                         | 1<<3      // pse
                         | 1<<4      // rdtsc
@@ -573,11 +574,11 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             } else {
                 R_EAX = 0;  // reserved
                 R_EBX = 0;  // reserved
-                R_ECX = (1<<0)  // LAHF_LM 
+                R_ECX = (1<<0)  // LAHF_LM
                     | (1<<5)    // LZCNT
                     | (1<<8)   // PREFETCHW
                     ;
-                R_EDX = 1       // x87 FPU 
+                R_EDX = 1       // x87 FPU
                     | (1<<8)    // cx8: cmpxchg8b opcode
                     | (1<<11)   // syscall
                     | (1<<15)   // cmov: FCMOV opcodes
@@ -605,7 +606,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             R_EBX = ((uint32_t*)branding)[9];
             R_ECX = ((uint32_t*)branding)[10];
             R_EDX = ((uint32_t*)branding)[11];
-            break;  
+            break;
         case 0x80000005:
             if(BOX64ENV(cputype)) {
                 //L1 cache and TLB
@@ -665,7 +666,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             break;
         case 0x8000000a:
             if(BOX64ENV(cputype)) {
-                // SVM Revision and Feature Identification 
+                // SVM Revision and Feature Identification
                 R_EAX = 0;
                 R_EBX = 0;
                 R_ECX = 0;
@@ -706,7 +707,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
             R_EBX = 0;
             R_ECX = 0;
             R_EDX = 0;
-    }   
+    }
 }
 
 uint32_t helper_getcpu(x64emu_t* emu) {