about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2022-11-11 18:08:19 +0100
committerptitSeb <sebastien.chev@gmail.com>2022-11-11 18:08:19 +0100
commit987a66309445dc720a551dade7f52beda28af0c5 (patch)
tree81a369a3b37db1eaf4e977dd97f9d0e79d8ef8a1 /src
parent107dbd56459c39334f9b0d3611c04d561571737e (diff)
downloadbox64-987a66309445dc720a551dade7f52beda28af0c5.tar.gz
box64-987a66309445dc720a551dade7f52beda28af0c5.zip
Improved cpu cores detection and reporting
Diffstat (limited to 'src')
-rwxr-xr-xsrc/main.c5
-rw-r--r--src/tools/my_cpuid.c1
-rwxr-xr-xsrc/wrapped/wrappedlibc.c43
3 files changed, 41 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index a8f3eb9f..12c57ee5 100755
--- a/src/main.c
+++ b/src/main.c
@@ -163,6 +163,7 @@ void my_child_fork()
 }
 
 #ifdef DYNAREC
+int getNCpu();
 void GatherDynarecExtensions()
 {
     if(box64_dynarec==0)    // no need to check if no dynarec
@@ -304,7 +305,9 @@ HWCAP2_ECV
         printf_log(LOG_INFO, " PMULL");
     if(arm64_atomics)
         printf_log(LOG_INFO, " ATOMICS");
-    printf_log(LOG_INFO, " PageSize:%d\n", box64_pagesize);
+    printf_log(LOG_INFO, " PageSize:%d", box64_pagesize);
+    int ncpu = getNCpu();
+    printf_log(LOG_INFO, " Cores:%d\n", ncpu);
 #elif defined(LA464)
     printf_log(LOG_INFO, "Dynarec for LoongArch");
     printf_log(LOG_INFO, " PageSize:%d\n", box64_pagesize);
diff --git a/src/tools/my_cpuid.c b/src/tools/my_cpuid.c
index 7e330bec..b08c214f 100644
--- a/src/tools/my_cpuid.c
+++ b/src/tools/my_cpuid.c
@@ -128,6 +128,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
         case 0xB:   // Extended Topology Enumeration Leaf
             //TODO!
             R_EAX = 0;
+            R_EBX = 0;
             break;
         case 0xC:   //?
             R_EAX = 0;
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index 8f8f3881..37ebe664 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -1335,16 +1335,19 @@ void grabNCpu() {
     size_t dummy;
     if(f) {
         nCPU = 0;
+        int bogo = 0;
         size_t len = 500;
-        char line[500] = {0};
-        while ((dummy = getline((char**)&line, &len, f)) != -1) {
+        char* line = alloca(len);
+        while ((dummy = getline(&line, &len, f)) != -1) {
             if(!strncmp(line, "processor\t", strlen("processor\t")))
                 ++nCPU;
-            if(!nCPU && !strncmp(line, "BogoMIPS\t", strlen("BogoMIPS\t"))) {
+            if(!bogo && !strncmp(line, "BogoMIPS\t", strlen("BogoMIPS\t"))) {
                 // grab 1st BogoMIPS
                 float tmp;
-                if(sscanf(line, "BogoMIPS\t: %g", &tmp)==1)
+                if(sscanf(line, "BogoMIPS\t: %g", &tmp)==1) {
                     bogoMips = tmp;
+                    bogo = 1;
+                }
             }
         }
         fclose(f);
@@ -1397,7 +1400,7 @@ void CreateCPUInfoFile(int fd)
         P;
         sprintf(buff, "physical id\t: %d\nsiblings\t: %d\n", i, n);
         P;
-        sprintf(buff, "core id\t\t:%d\ncpu cores\t: %d\n", i, 1);
+        sprintf(buff, "core id\t\t: %d\ncpu cores\t: %d\n", i, 1);
         P;
         sprintf(buff, "bogomips\t: %g\n", bogoMips);
         P;
@@ -1783,8 +1786,9 @@ EXPORT int32_t my_execve(x64emu_t* emu, const char* path, char* const argv[], ch
     }
     #ifndef NOALIGN
     if(!strcmp(path + strlen(path) - strlen("/grep"), "/grep")
-    && argv[1] && argv[2] && !strcmp(argv[2], "/proc/cpuinfo")) {
+    && argv[1] && argv[2] && (!strcmp(argv[2], "/proc/cpuinfo") || (argv[1][1]=='-' && argv[3] && !strcmp(argv[3], "/proc/cpuinfo")))) {
         // special case of a bash script shell running grep on cpuinfo to extract capacities...
+        int cpuinfo = strcmp(argv[2], "/proc/cpuinfo")?3:2;
         int n=0;
         while(argv[n]) ++n;
         const char** newargv = (const char**)alloca((n+1)*sizeof(char*));
@@ -1801,7 +1805,32 @@ EXPORT int32_t my_execve(x64emu_t* emu, const char* path, char* const argv[], ch
         int rl = readlink(template, cpuinfo_file, sizeof(cpuinfo_file));
         close(fd);
         chmod(cpuinfo_file, 0666);
-        newargv[2] = cpuinfo_file;
+        newargv[cpuinfo] = cpuinfo_file;
+        printf_log(LOG_DEBUG, " => execve(\"%s\", %p [\"%s\", \"%s\", \"%s\"...:%d], %p)\n", path, newargv, newargv[0], newargv[1], newargv[2],n, envp);
+        int ret = execve(path, (char* const*)newargv, envp);
+        return ret;
+    }
+    if(!strcmp(path + strlen(path) - strlen("/cat"), "/cat")
+    && argv[1] && !strcmp(argv[1], "/proc/cpuinfo")) {
+        // special case of a bash script shell running grep on cpuinfo to extract capacities...
+        int cpuinfo = 1;
+        int n=0;
+        while(argv[n]) ++n;
+        const char** newargv = (const char**)alloca((n+1)*sizeof(char*));
+        memcpy(newargv, argv, sizeof(char*)*(n+1));
+        // create a dummy cpuinfo in temp (that will stay there, sorry)
+        const char* tmpdir = GetTmpDir();
+        char template[100] = {0};
+        sprintf(template, "%s/box64cpuinfoXXXXXX", tmpdir);
+        int fd = mkstemp(template);
+        CreateCPUInfoFile(fd);
+        // get back the name
+        char cpuinfo_file[100] = {0};
+        sprintf(template, "/proc/self/fd/%d", fd);
+        int rl = readlink(template, cpuinfo_file, sizeof(cpuinfo_file));
+        close(fd);
+        chmod(cpuinfo_file, 0666);
+        newargv[cpuinfo] = cpuinfo_file;
         printf_log(LOG_DEBUG, " => execve(\"%s\", %p [\"%s\", \"%s\", \"%s\"...:%d], %p)\n", path, newargv, newargv[0], newargv[1], newargv[2],n, envp);
         int ret = execve(path, (char* const*)newargv, envp);
         return ret;