mirror of
https://github.com/ptitSeb/box64.git
synced 2026-11-07 09:26:11 -10:00
[SYSINFO] Refactored sysinfo module (#3410)
* [SYSINFO] Refactored sysinfo module * fix wowbox64 build * fix * fixfixfix * review * this is SO annoying * ???? * woops * build, please * fix * review
This commit is contained in:
1 parent
5c7b90a52a
commit
f06933aad7
23 files changed
+339
-404
No files matched your search
+3
-2
@@ -380,8 +380,9 @@ set(OS_LINUX_SRC
|
||||
"${BOX64_ROOT}/src/os/os_linux.c"
|
||||
"${BOX64_ROOT}/src/os/perfmap.c"
|
||||
"${BOX64_ROOT}/src/os/symbolfuncs_linux.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid_linux.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid_common.c"
|
||||
"${BOX64_ROOT}/src/os/random_linux.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid.c"
|
||||
"${BOX64_ROOT}/src/os/sysinfo.c"
|
||||
)
|
||||
|
||||
set(ELFLOADER_SRC
|
||||
|
||||
+10
-12
@@ -49,6 +49,7 @@
|
||||
#include "cleanup.h"
|
||||
#include "freq.h"
|
||||
#include "hostext.h"
|
||||
#include "sysinfo.h"
|
||||
|
||||
box64context_t *my_context = NULL;
|
||||
extern box64env_t box64env;
|
||||
@@ -76,6 +77,7 @@ uint32_t default_gs = 0x53;
|
||||
uint32_t default_fs = 0;
|
||||
int box64_isglibc234 = 0;
|
||||
int box64_unittest_mode = 0;
|
||||
sysinfo_t box64_sysinfo = { 0 };
|
||||
|
||||
#ifdef DYNAREC
|
||||
cpu_ext_t cpuext = {0};
|
||||
@@ -159,9 +161,6 @@ static void openFTrace(void)
|
||||
}
|
||||
}
|
||||
|
||||
const char* getCpuName();
|
||||
int getNCpuUnmasked();
|
||||
|
||||
void computeRDTSC()
|
||||
{
|
||||
int hardware = 0;
|
||||
@@ -224,13 +223,12 @@ static void displayMiscInfo()
|
||||
}
|
||||
#endif
|
||||
|
||||
// grab ncpu and cpu name
|
||||
int ncpu = getNCpuUnmasked();
|
||||
const char* cpuname = getCpuName();
|
||||
|
||||
printf_log(LOG_INFO, "Running on %s with %d core%s, pagesize: %zd\n", cpuname, ncpu, ncpu > 1 ? "s" : "", box64_pagesize);
|
||||
|
||||
// grab and calibrate hardware counter
|
||||
InitializeSystemInfo();
|
||||
printf_log(LOG_INFO, "Running on %s with %d core%s, pagesize: %zd", box64_sysinfo.cpuname, box64_sysinfo.ncpu, box64_sysinfo.ncpu > 1 ? "s" : "", box64_pagesize);
|
||||
if (BOX64ENV(maxcpu))
|
||||
printf_log_prefix(0, LOG_INFO, ", emulating %d core%s\n", BOX64ENV(maxcpu), BOX64ENV(maxcpu) > 1 ? "s" : "");
|
||||
else
|
||||
printf_log_prefix(0, LOG_INFO, "\n");
|
||||
computeRDTSC();
|
||||
}
|
||||
|
||||
@@ -716,9 +714,9 @@ extern char** environ;
|
||||
|
||||
int initialize(int argc, const char **argv, char** env, x64emu_t** emulator, elfheader_t** elfheader, int exec)
|
||||
{
|
||||
#ifndef STATICBUILD
|
||||
#ifndef STATICBUILD
|
||||
init_malloc_hook();
|
||||
#endif
|
||||
#endif
|
||||
init_auxval(argc, argv, environ?environ:env);
|
||||
// analogue to QEMU_VERSION in qemu-user-mode emulation
|
||||
if(getenv("BOX64_VERSION")) {
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "emu/x64compstrings.h"
|
||||
#include "x64test.h"
|
||||
#include "dynarec/dynarec_next.h"
|
||||
#include "random.h"
|
||||
|
||||
static const int8_t mask_shift8[] = { -7, -6, -5, -4, -3, -2, -1, 0 };
|
||||
static const int8_t mask_string8[] = { 7, 6, 5, 4, 3, 2, 1, 0 };
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "x64test.h"
|
||||
#include "dynarec/dynarec_next.h"
|
||||
#include "bitutils.h"
|
||||
#include "random.h"
|
||||
|
||||
#ifndef HAVE_TRACE
|
||||
void PrintTrace() {}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "x64test.h"
|
||||
#include "dynarec/dynarec_next.h"
|
||||
#include "bitutils.h"
|
||||
#include "random.h"
|
||||
|
||||
#ifndef HAVE_TRACE
|
||||
void PrintTrace() {}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "emit_signals.h"
|
||||
#include "x64shaext.h"
|
||||
#include "freq.h"
|
||||
#include "random.h"
|
||||
#ifdef DYNAREC
|
||||
#include "custommem.h"
|
||||
#include "../dynarec/native_lock.h"
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "os.h"
|
||||
#include "hostext.h"
|
||||
#include "sysinfo.h"
|
||||
|
||||
typedef struct box64context_s box64context_t;
|
||||
extern box64env_t box64env;
|
||||
@@ -33,6 +34,7 @@ extern int box64_unittest_mode;
|
||||
extern uintptr_t fmod_smc_start, fmod_smc_end; // to handle libfmod (from Unreal) SMC (self modifying code)
|
||||
extern uint32_t default_gs, default_fs;
|
||||
extern int box64_tcmalloc_minimal; // when using tcmalloc_minimal
|
||||
extern sysinfo_t box64_sysinfo;
|
||||
#define LOG_NONE 0
|
||||
#define LOG_INFO 1
|
||||
#define LOG_DEBUG 2
|
||||
@@ -96,7 +98,7 @@ void init_malloc_hook(void);
|
||||
#define box_strdup strdup
|
||||
#define box_realpath realpath
|
||||
#else
|
||||
extern size_t(*box_malloc_usable_size)(void*);
|
||||
extern size_t (*box_malloc_usable_size)(void*);
|
||||
extern void* __libc_malloc(size_t);
|
||||
extern void* __libc_realloc(void*, size_t);
|
||||
extern void* __libc_calloc(size_t, size_t);
|
||||
|
||||
@@ -4,12 +4,4 @@
|
||||
typedef struct x64emu_s x64emu_t;
|
||||
|
||||
void my_cpuid(x64emu_t* emu, uint32_t tmp32u);
|
||||
uint32_t helper_getcpu(x64emu_t* emu); // get the numa/cpu id actually running
|
||||
uint32_t get_random32();
|
||||
uint64_t get_random64();
|
||||
int get_cpuMhz();
|
||||
int getNCpu();
|
||||
int getNCpuUnmasked();
|
||||
int canNCpuBeChanged();
|
||||
const char* getBoxCpuName();
|
||||
#endif //__MY_CPUID_H__
|
||||
@@ -0,0 +1,5 @@
|
||||
#ifndef __RANDOM_H_
|
||||
#define __RANDOM_H_
|
||||
uint32_t get_random32(void);
|
||||
uint64_t get_random64(void);
|
||||
#endif //__RANDOM_H_
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __SYSINFO_H_
|
||||
#define __SYSINFO_H_
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct x64emu_s x64emu_t;
|
||||
typedef struct sysinfo_s {
|
||||
uint64_t frequency;
|
||||
uint64_t ncpu;
|
||||
uint64_t bogomips;
|
||||
char* cpuname;
|
||||
|
||||
uint64_t box64_ncpu;
|
||||
char* box64_cpuname;
|
||||
|
||||
uint32_t emulated_frequency : 1;
|
||||
uint32_t read_frequency : 1;
|
||||
uint32_t read_ncpu : 1;
|
||||
uint32_t read_bogomips : 1;
|
||||
uint32_t read_cpuname : 1;
|
||||
} sysinfo_t;
|
||||
|
||||
void InitializeSystemInfo(void);
|
||||
uint32_t helper_getcpu(x64emu_t* emu);
|
||||
#endif //__SYSINFO_H_
|
||||
+1
-8
@@ -81,15 +81,8 @@ static inline uint64_t readFreq()
|
||||
{
|
||||
static size_t val = -1;
|
||||
|
||||
FILE* f = popen("cat /proc/cpuinfo | grep -i \"CPU MHz\" | head -n 1 | sed -r 's/CPU MHz.+:\\s{1,}//g'", "r");
|
||||
if(f) {
|
||||
char tmp[200] = "";
|
||||
ssize_t s = fread(tmp, 1, 200, f);
|
||||
pclose(f);
|
||||
if (s > 0) return (uint64_t)atof(tmp) * 1e6;
|
||||
}
|
||||
if (box64_sysinfo.read_frequency) return box64_sysinfo.frequency;
|
||||
|
||||
// fallback to rdtime + sleep
|
||||
struct timespec ts;
|
||||
ts.tv_sec = 0;
|
||||
ts.tv_nsec = 50000000; // 50 milliseconds
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
|
||||
{
|
||||
emu->regs[_AX].dword[1] = emu->regs[_DX].dword[1] = emu->regs[_CX].dword[1] = emu->regs[_BX].dword[1] = 0;
|
||||
int ncpu = getNCpu();
|
||||
if(!ncpu) ncpu = 1;
|
||||
int ncpu = box64_sysinfo.box64_ncpu;
|
||||
if (!ncpu) ncpu = 1;
|
||||
int ncluster = 1;
|
||||
static int warned = 10;
|
||||
if(BOX64ENV(cputype)) {
|
||||
@@ -33,7 +33,7 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
|
||||
}
|
||||
static char branding[3*4*4+1] = "";
|
||||
if(!branding[0]) {
|
||||
strcpy(branding, getBoxCpuName());
|
||||
strncpy(branding, box64_sysinfo.box64_cpuname, 3*4*4);
|
||||
while(strlen(branding)<3*4*4) {
|
||||
memmove(branding+1, branding, strlen(branding)+1);
|
||||
branding[0] = ' ';
|
||||
@@ -356,8 +356,8 @@ void my_cpuid(x64emu_t* emu, uint32_t tmp32u)
|
||||
if(BOX64ENV(cputype)) {
|
||||
R_RAX = R_RBX = R_RCX = R_RDX = 0;
|
||||
} else {
|
||||
R_RAX = get_cpuMhz();
|
||||
R_RBX = get_cpuMhz();
|
||||
R_RAX = box64_sysinfo.frequency / 1000000;
|
||||
R_RBX = box64_sysinfo.frequency / 1000000;
|
||||
R_RCX = 100;
|
||||
R_RDX = 0;
|
||||
|
||||
@@ -1,274 +0,0 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sched.h>
|
||||
|
||||
#include "my_cpuid.h"
|
||||
#include "build_info.h"
|
||||
#include "debug.h"
|
||||
#include "x64emu.h"
|
||||
|
||||
int get_cpuMhz()
|
||||
{
|
||||
static int MHz = 0;
|
||||
if(MHz) return MHz;
|
||||
char *p = NULL;
|
||||
if((p=getenv("BOX64_CPUMHZ"))) {
|
||||
MHz = atoi(p);
|
||||
return MHz;
|
||||
}
|
||||
char cpumhz[200];
|
||||
sprintf(cpumhz, "%d", MHz?:1000);
|
||||
setenv("BOX64_CPUMHZ", cpumhz, 1); // set temp value incase box64 gets recursively called
|
||||
|
||||
int cpucore = 0;
|
||||
while(cpucore!=-1) {
|
||||
char cpufreq[4096];
|
||||
sprintf(cpufreq, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpucore);
|
||||
FILE *f = fopen(cpufreq, "r");
|
||||
if(f) {
|
||||
int r;
|
||||
if(1==fscanf(f, "%d", &r)) {
|
||||
r /= 1000;
|
||||
if(MHz<r)
|
||||
MHz = r;
|
||||
}
|
||||
fclose(f);
|
||||
++cpucore;
|
||||
}
|
||||
else
|
||||
cpucore = -1;
|
||||
}
|
||||
// try with /procs/cpuinfo
|
||||
if (!MHz) {
|
||||
FILE* f = fopen("/proc/cpuinfo", "r");
|
||||
if (f) {
|
||||
char line[256];
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
if (strcasestr(line, "cpu mhz") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
float mhz;
|
||||
if (sscanf(colon + 1, "%f", &mhz) == 1) {
|
||||
MHz = (int)mhz;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
#ifndef STATICBUILD
|
||||
if(!MHz) {
|
||||
// try with lscpu, grabbing the max frequency
|
||||
FILE* f = popen("lscpu | grep \"CPU max MHz:\" | sed -r 's/CPU max MHz:\\s{1,}//g'", "r");
|
||||
if(f) {
|
||||
char tmp[200] = "";
|
||||
ssize_t s = fread(tmp, 1, 200, f);
|
||||
pclose(f);
|
||||
if(s>0) {
|
||||
// worked! (unless it's saying "lscpu: command not found" or something like that)
|
||||
if(!strstr(tmp, "lscpu")) {
|
||||
// trim ending
|
||||
while(strlen(tmp) && tmp[strlen(tmp)-1]=='\n')
|
||||
tmp[strlen(tmp)-1] = 0;
|
||||
// incase multiple cpu type are present, there will be multiple lines
|
||||
while(strchr(tmp, '\n'))
|
||||
*strchr(tmp,'\n') = ' ';
|
||||
// cut the float part (so '.' or ','), it's not needed
|
||||
if(strchr(tmp, '.'))
|
||||
*strchr(tmp, '.')= '\0';
|
||||
if(strchr(tmp, ','))
|
||||
*strchr(tmp, ',')= '\0';
|
||||
int mhz;
|
||||
if(sscanf(tmp, "%d", &mhz)==1)
|
||||
MHz = mhz;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if(!MHz)
|
||||
MHz = 1500; // default to 1.5Ghz...
|
||||
sprintf(cpumhz, "%d", MHz);
|
||||
setenv("BOX64_CPUMHZ", cpumhz, 1); // set actual value
|
||||
return MHz;
|
||||
}
|
||||
static int nCPU = 0;
|
||||
static double bogoMips = 100.;
|
||||
static int read_ncpu = 0;
|
||||
|
||||
void grabNCpu() {
|
||||
nCPU = 1; // default number of CPU to 1
|
||||
FILE *f = fopen("/proc/cpuinfo", "r");
|
||||
ssize_t dummy;
|
||||
if(f) {
|
||||
nCPU = 0;
|
||||
int bogo = 0;
|
||||
size_t len = 500;
|
||||
char* line = malloc(len);
|
||||
while ((dummy = getline(&line, &len, f)) != (ssize_t)-1) {
|
||||
if(!strncmp(line, "processor\t", strlen("processor\t")))
|
||||
++nCPU;
|
||||
if(!bogo && !strncmp(line, "BogoMIPS\t", strlen("BogoMIPS\t"))) {
|
||||
// grab 1st BogoMIPS
|
||||
float tmp;
|
||||
if(sscanf(line, "BogoMIPS\t: %g", &tmp)==1) {
|
||||
bogoMips = tmp;
|
||||
bogo = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(line);
|
||||
fclose(f);
|
||||
if(!nCPU) nCPU=1;
|
||||
}
|
||||
}
|
||||
int getNCpu()
|
||||
{
|
||||
if(!nCPU)
|
||||
grabNCpu();
|
||||
read_ncpu = 1;
|
||||
if(BOX64ENV(maxcpu) && nCPU>BOX64ENV(maxcpu))
|
||||
return BOX64ENV(maxcpu);
|
||||
return nCPU;
|
||||
}
|
||||
int getNCpuUnmasked()
|
||||
{
|
||||
if(!nCPU)
|
||||
grabNCpu();
|
||||
return nCPU;
|
||||
}
|
||||
int canNCpuBeChanged()
|
||||
{
|
||||
return read_ncpu?0:1;
|
||||
}
|
||||
|
||||
double getBogoMips()
|
||||
{
|
||||
if(!nCPU)
|
||||
grabNCpu();
|
||||
return bogoMips;
|
||||
}
|
||||
|
||||
const char* getCpuName()
|
||||
{
|
||||
static char name[200] = "Unknown CPU";
|
||||
static int done = 0;
|
||||
if(done)
|
||||
return name;
|
||||
done = 1;
|
||||
char *p = NULL;
|
||||
if((p=getenv("BOX64_CPUNAME"))) {
|
||||
strcpy(name, p);
|
||||
return name;
|
||||
}
|
||||
setenv("BOX64_CPUNAME", name, 1); // temporary set
|
||||
#ifndef STATICBUILD
|
||||
FILE* f = popen("LC_ALL=C lscpu | grep -i \"model name:\" | head -n 1 | sed -r 's/(model name:)\\s{1,}//gi'", "r");
|
||||
if(f) {
|
||||
char tmp[200] = "";
|
||||
ssize_t s = fread(tmp, 1, 200, f);
|
||||
pclose(f);
|
||||
if(s>0) {
|
||||
// worked! (unless it's saying "lscpu: command not found" or something like that)
|
||||
if(!strstr(tmp, "lscpu")) {
|
||||
// trim ending
|
||||
while(strlen(tmp) && tmp[strlen(tmp)-1]=='\n')
|
||||
tmp[strlen(tmp)-1] = 0;
|
||||
strncpy(name, tmp, 199);
|
||||
}
|
||||
setenv("BOX64_CPUNAME", name, 1);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
// failed, try to get architecture at least
|
||||
f = popen("uname -m", "r");
|
||||
if(f) {
|
||||
char tmp[200] = "";
|
||||
ssize_t s = fread(tmp, 1, 200, f);
|
||||
pclose(f);
|
||||
if(s>0) {
|
||||
// worked!
|
||||
// trim ending
|
||||
while(strlen(tmp) && tmp[strlen(tmp)-1]=='\n')
|
||||
tmp[strlen(tmp)-1] = 0;
|
||||
snprintf(name, 199, "unknown %s cpu", tmp);
|
||||
setenv("BOX64_CPUNAME", name, 1);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// Nope, bye
|
||||
return name;
|
||||
}
|
||||
|
||||
const char* getBoxCpuName()
|
||||
{
|
||||
static char branding[3*4*4+1] = "";
|
||||
static int done = 0;
|
||||
if(!done) {
|
||||
done = 1;
|
||||
const char* name = getCpuName();
|
||||
if(strstr(name, "MHz") || strstr(name, "GHz")) {
|
||||
// name already have the speed in it
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s", 39, name);
|
||||
} else {
|
||||
unsigned int MHz = get_cpuMhz();
|
||||
if(MHz>1500) { // swiches to GHz display...
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s @%1.2f GHz", 28, name, MHz / 1000.);
|
||||
} else {
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s @%04d MHz", 28, name, MHz);
|
||||
}
|
||||
}
|
||||
}
|
||||
return branding;
|
||||
}
|
||||
|
||||
uint32_t helper_getcpu(x64emu_t* emu) {
|
||||
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && !defined(ANDROID)
|
||||
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 28)
|
||||
uint32_t cpu, node;
|
||||
if(!getcpu(&cpu, &node))
|
||||
return (node&0xff)<<12 | (cpu&0xff);
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t fallback_random32()
|
||||
{
|
||||
return random() ^ (random()<<1);
|
||||
}
|
||||
|
||||
uint32_t get_random32()
|
||||
{
|
||||
uint32_t ret;
|
||||
FILE* f = fopen("/dev/urandom", "rb");
|
||||
if(f) {
|
||||
if(fread(&ret, sizeof(ret), 1, f)!=1)
|
||||
ret = fallback_random32();
|
||||
fclose(f);
|
||||
} else
|
||||
ret = fallback_random32();
|
||||
return ret;
|
||||
}
|
||||
uint64_t fallback_random64()
|
||||
{
|
||||
return random() ^ (((uint64_t)random())<<18) ^ (((uint64_t)random())<<41);
|
||||
}
|
||||
|
||||
uint64_t get_random64()
|
||||
{
|
||||
uint64_t ret;
|
||||
FILE* f = fopen("/dev/urandom", "rb");
|
||||
if(f) {
|
||||
if(fread(&ret, sizeof(ret), 1, f)!=1)
|
||||
ret = fallback_random64();
|
||||
fclose(f);
|
||||
} else
|
||||
ret = fallback_random64();
|
||||
return ret;
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include "my_cpuid.h"
|
||||
#include "debug.h"
|
||||
#include "build_info.h"
|
||||
|
||||
NTSYSAPI ULONG WINAPI NtGetTickCount(VOID);
|
||||
NTSYSAPI ULONG NTAPI RtlRandom(ULONG *seed);
|
||||
|
||||
static int nCPU = 0;
|
||||
static int read_ncpu = 0;
|
||||
|
||||
void grabNCpu() {
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
nCPU = sysinfo.dwNumberOfProcessors;
|
||||
}
|
||||
int get_cpuMhz() {
|
||||
return 2000; //TODO: find a way to get realy value instead of arbitrary default to 2.0GHz
|
||||
}
|
||||
int getNCpu()
|
||||
{
|
||||
if(!nCPU)
|
||||
grabNCpu();
|
||||
if(BOX64ENV(maxcpu) && nCPU>BOX64ENV(maxcpu))
|
||||
return BOX64ENV(maxcpu);
|
||||
return nCPU;
|
||||
}
|
||||
int getNCpuUnmasked()
|
||||
{
|
||||
if(!nCPU)
|
||||
grabNCpu();
|
||||
return nCPU;
|
||||
}
|
||||
int canNCpuBeChanged()
|
||||
{
|
||||
return read_ncpu?0:1;
|
||||
}
|
||||
|
||||
const char* getBoxCpuName()
|
||||
{
|
||||
return BOX64_STR;
|
||||
}
|
||||
|
||||
uint32_t helper_getcpu(x64emu_t* emu) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t get_random32(void)
|
||||
{
|
||||
ULONG seed = NtGetTickCount();
|
||||
return RtlRandom(&seed);
|
||||
}
|
||||
|
||||
uint64_t get_random64(void)
|
||||
{
|
||||
ULONG seed = NtGetTickCount();
|
||||
uint64_t tmp = RtlRandom(&seed);
|
||||
return RtlRandom(&seed) | (tmp << 32);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "x64emu.h"
|
||||
|
||||
uint32_t fallback_random32(void)
|
||||
{
|
||||
return random() ^ (random()<<1);
|
||||
}
|
||||
|
||||
uint32_t get_random32(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
FILE* f = fopen("/dev/urandom", "rb");
|
||||
if(f) {
|
||||
if(fread(&ret, sizeof(ret), 1, f)!=1)
|
||||
ret = fallback_random32();
|
||||
fclose(f);
|
||||
} else
|
||||
ret = fallback_random32();
|
||||
return ret;
|
||||
}
|
||||
uint64_t fallback_random64(void)
|
||||
{
|
||||
return random() ^ (((uint64_t)random())<<18) ^ (((uint64_t)random())<<41);
|
||||
}
|
||||
|
||||
uint64_t get_random64(void)
|
||||
{
|
||||
uint64_t ret;
|
||||
FILE* f = fopen("/dev/urandom", "rb");
|
||||
if(f) {
|
||||
if(fread(&ret, sizeof(ret), 1, f)!=1)
|
||||
ret = fallback_random64();
|
||||
fclose(f);
|
||||
} else
|
||||
ret = fallback_random64();
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include "my_cpuid.h"
|
||||
#include "debug.h"
|
||||
#include "build_info.h"
|
||||
|
||||
NTSYSAPI ULONG WINAPI NtGetTickCount(VOID);
|
||||
NTSYSAPI ULONG NTAPI RtlRandom(ULONG *seed);
|
||||
|
||||
uint32_t get_random32(void)
|
||||
{
|
||||
ULONG seed = NtGetTickCount();
|
||||
return RtlRandom(&seed);
|
||||
}
|
||||
|
||||
uint64_t get_random64(void)
|
||||
{
|
||||
ULONG seed = NtGetTickCount();
|
||||
uint64_t tmp = RtlRandom(&seed);
|
||||
return RtlRandom(&seed) | (tmp << 32);
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sched.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "sysinfo.h"
|
||||
#include "build_info.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include "freq.h"
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
static void readCpuinfo(sysinfo_t* info)
|
||||
{
|
||||
info->emulated_frequency = 1;
|
||||
int cpucore = 0;
|
||||
while (1) {
|
||||
char cpufreq[4096] = { 0 };
|
||||
sprintf(cpufreq, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpucore);
|
||||
FILE* f = fopen(cpufreq, "r");
|
||||
if (!f) break;
|
||||
int r;
|
||||
if (1 == fscanf(f, "%d", &r)) {
|
||||
info->frequency = (uint64_t)r * 1000;
|
||||
info->emulated_frequency = 0;
|
||||
info->read_frequency = 1;
|
||||
}
|
||||
fclose(f);
|
||||
++cpucore;
|
||||
}
|
||||
|
||||
char line[4096] = { 0 };
|
||||
FILE* f = fopen("/proc/cpuinfo", "r");
|
||||
if (!f) goto lscpu;
|
||||
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
if (info->ncpu <= 1 && !info->read_frequency && strstr(line, "CPU MHz") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
float mhz;
|
||||
if (sscanf(colon + 1, "%f", &mhz) == 1) {
|
||||
info->frequency = (uint64_t)(mhz * 1e6);
|
||||
info->emulated_frequency = 0;
|
||||
info->read_frequency = 1;
|
||||
}
|
||||
}
|
||||
} else if (info->ncpu <= 1 && strstr(line, "Model Name") != NULL) { // TODO: big.LITTLE handling?
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
size_t len = strlen(colon + 2);
|
||||
info->cpuname = (char*)malloc(len + 1);
|
||||
strcpy(info->cpuname, colon + 2);
|
||||
char* newline = strchr(info->cpuname, '\n');
|
||||
if (newline) *newline = '\0';
|
||||
info->read_cpuname = 1;
|
||||
}
|
||||
} else if (!info->read_bogomips && strstr(line, "BogoMIPS") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
float bm;
|
||||
if (sscanf(colon + 1, "%f", &bm) == 1) {
|
||||
info->bogomips = (uint64_t)(bm * 1e6);
|
||||
info->read_bogomips = 1;
|
||||
}
|
||||
}
|
||||
} else if (strstr(line, "processor") != NULL) {
|
||||
info->ncpu++;
|
||||
}
|
||||
}
|
||||
if (info->ncpu) info->read_ncpu = 1;
|
||||
fclose(f);
|
||||
|
||||
lscpu:
|
||||
if (getenv("BOX64_LSCPU_EXECUTED")) {
|
||||
// in case it's the x86_64 lscpu, prevent infinite loop
|
||||
goto fallback;
|
||||
}
|
||||
setenv("BOX64_LSCPU_EXECUTED", "1", 1);
|
||||
if (!info->read_cpuname || !info->read_ncpu || !info->read_frequency) {
|
||||
FILE* f = popen("lscpu", "r");
|
||||
if (!f) goto fallback;
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
if (!info->read_ncpu && strstr(line, "CPU(s):") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
int ncpu;
|
||||
if (sscanf(colon + 1, "%d", &ncpu) == 1) {
|
||||
info->ncpu = (uint64_t)ncpu;
|
||||
info->read_ncpu = 1;
|
||||
}
|
||||
}
|
||||
} else if (!info->read_cpuname && strstr(line, "Model name:") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
size_t len = strlen(colon + 2);
|
||||
info->cpuname = (char*)malloc(len + 1);
|
||||
strcpy(info->cpuname, colon + 2);
|
||||
char* newline = strchr(info->cpuname, '\n');
|
||||
if (newline) *newline = '\0';
|
||||
info->read_cpuname = 1;
|
||||
}
|
||||
} else if (!info->read_frequency && strstr(line, "CPU max MHz:") != NULL) {
|
||||
char* colon = strchr(line, ':');
|
||||
if (colon != NULL) {
|
||||
float mhz;
|
||||
if (sscanf(colon + 1, "%f", &mhz) == 1) {
|
||||
info->frequency = (uint64_t)(mhz * 1e6);
|
||||
info->emulated_frequency = 0;
|
||||
info->read_frequency = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pclose(f);
|
||||
}
|
||||
|
||||
fallback:
|
||||
if (!info->read_ncpu && !getenv("BOX64_NPROC_EXECUTED") /* in case it's the x86 version, prevent infinite loop */) {
|
||||
setenv("BOX64_NPROC_EXECUTED", "1", 1);
|
||||
FILE* f = popen("nproc", "r");
|
||||
if (f) {
|
||||
int ncpu;
|
||||
if (fscanf(f, "%d", &ncpu) == 1) {
|
||||
info->ncpu = (uint64_t)ncpu;
|
||||
}
|
||||
pclose(f);
|
||||
} else {
|
||||
info->ncpu = 1;
|
||||
}
|
||||
}
|
||||
if (!info->read_cpuname) {
|
||||
info->cpuname = (char*)malloc(20);
|
||||
strcpy(info->cpuname, "Unknown CPU");
|
||||
}
|
||||
if (!info->read_frequency) {
|
||||
info->frequency = 1000000000; // 1GHz default
|
||||
info->emulated_frequency = 1;
|
||||
}
|
||||
|
||||
if (!info->read_bogomips) {
|
||||
info->bogomips = info->frequency;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
void InitializeSystemInfo(void)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
sysinfo_t info = { 0 };
|
||||
readCpuinfo(&box64_sysinfo);
|
||||
|
||||
char branding[3 * 4 * 4 + 1];
|
||||
if (strstr(box64_sysinfo.cpuname, "MHz") || strstr(box64_sysinfo.cpuname, "GHz")) {
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s", 39, box64_sysinfo.cpuname);
|
||||
} else {
|
||||
if (box64_sysinfo.frequency > 1500000) {
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s @%1.2f GHz", 28, box64_sysinfo.cpuname, box64_sysinfo.frequency / 1000000000.);
|
||||
} else {
|
||||
snprintf(branding, sizeof(branding), BOX64_BUILD_INFO_STRING_SHORT " on %.*s @%04d MHz", 28, box64_sysinfo.cpuname, box64_sysinfo.frequency / 1000000);
|
||||
}
|
||||
}
|
||||
box64_sysinfo.box64_cpuname = (char*)calloc(strlen(branding) + 1, 1);
|
||||
strcpy(box64_sysinfo.box64_cpuname, branding);
|
||||
|
||||
#else
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
box64_sysinfo.ncpu = sysinfo.dwNumberOfProcessors;
|
||||
box64_sysinfo.frequency = ReadTSCFrequency(NULL);
|
||||
box64_sysinfo.bogomips = box64_sysinfo.frequency;
|
||||
box64_sysinfo.cpuname = (char*)WinMalloc(64);
|
||||
strcpy(box64_sysinfo.cpuname, "Box64 Virtual CPU");
|
||||
box64_sysinfo.box64_cpuname = (char*)WinCalloc(strlen(BOX64_STR) + 1, 1);
|
||||
strcpy(box64_sysinfo.box64_cpuname, BOX64_STR);
|
||||
|
||||
box64_sysinfo.emulated_frequency = 0;
|
||||
#endif
|
||||
|
||||
box64_sysinfo.box64_ncpu = box64_sysinfo.ncpu;
|
||||
if (BOX64ENV(maxcpu) && box64_sysinfo.ncpu > (uint64_t)BOX64ENV(maxcpu)) {
|
||||
box64_sysinfo.box64_ncpu = (uint64_t)BOX64ENV(maxcpu);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t helper_getcpu(x64emu_t* emu)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && !defined(ANDROID)
|
||||
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 28)
|
||||
uint32_t cpu, node;
|
||||
if (!getcpu(&cpu, &node))
|
||||
return (node & 0xff) << 12 | (cpu & 0xff);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
return box64_sysinfo.ncpu;
|
||||
}
|
||||
+2
-4
@@ -142,7 +142,6 @@ static void parseRange(const char* s, uintptr_t* start, uintptr_t* end)
|
||||
}
|
||||
|
||||
void AddNewLibs(const char* list);
|
||||
int canNCpuBeChanged();
|
||||
|
||||
extern int box64_cycle_log_initialized;
|
||||
|
||||
@@ -255,8 +254,7 @@ static void applyCustomRules()
|
||||
}
|
||||
|
||||
if (box64env.maxcpu == 0 || (box64env.new_maxcpu < box64env.maxcpu)) {
|
||||
if(canNCpuBeChanged())
|
||||
box64env.maxcpu = box64env.new_maxcpu;
|
||||
box64env.maxcpu = box64env.new_maxcpu;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
@@ -278,7 +276,7 @@ static void applyCustomRules()
|
||||
box64env.avx = 1;
|
||||
box64env.avx2 = 1;
|
||||
}
|
||||
#ifdef LA64
|
||||
#if defined(LA64) && defined(DYNAREC)
|
||||
if (box64env.avx && !cpuext.lasx) {
|
||||
box64env.avx = 0;
|
||||
box64env.avx2 = 0;
|
||||
|
||||
+10
-24
@@ -70,6 +70,7 @@
|
||||
#include "wine_tools.h"
|
||||
#include "pe_tools.h"
|
||||
#include "cleanup.h"
|
||||
#include "random.h"
|
||||
#ifndef LOG_INFO
|
||||
#define LOG_INFO 1
|
||||
#endif
|
||||
@@ -1716,26 +1717,12 @@ EXPORT ssize_t my___readlink_chk(x64emu_t* emu, void* path, void* buf, size_t sz
|
||||
return my_readlink(emu, path, buf, sz);
|
||||
}
|
||||
|
||||
int getNCpu(); // defined in my_cpuid.c
|
||||
const char* getBoxCpuName(); // defined in my_cpuid.c
|
||||
const char* getCpuName(); // defined in my_cpu_id.c
|
||||
double getBogoMips(); // defined in my_cpu_id.c
|
||||
|
||||
#ifndef NOALIGN
|
||||
void CreateCPUInfoFile(int fd)
|
||||
{
|
||||
size_t dummy;
|
||||
char buff[600];
|
||||
double freq = 600.0; // default to 600 MHz
|
||||
// try to get actual ARM max speed:
|
||||
FILE *f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
|
||||
if(f) {
|
||||
int r;
|
||||
if(1==fscanf(f, "%d", &r))
|
||||
freq = r/1000.;
|
||||
fclose(f);
|
||||
}
|
||||
int n = getNCpu();
|
||||
int n = box64_sysinfo.box64_ncpu;
|
||||
// generate fake CPUINFO
|
||||
#define P \
|
||||
dummy = write(fd, buff, strlen(buff))
|
||||
@@ -1748,11 +1735,11 @@ void CreateCPUInfoFile(int fd)
|
||||
P;
|
||||
sprintf(buff, "model\t\t: 1\n");
|
||||
P;
|
||||
sprintf(buff, "model name\t: %s\n", getBoxCpuName());
|
||||
sprintf(buff, "model name\t: %s\n", box64_sysinfo.cpuname);
|
||||
P;
|
||||
sprintf(buff, "stepping\t: 1\nmicrocode\t: 0x10\n");
|
||||
P;
|
||||
sprintf(buff, "cpu MHz\t\t: %g\n", freq);
|
||||
sprintf(buff, "cpu MHz\t\t: %g\n", box64_sysinfo.frequency / 1000000.);
|
||||
P;
|
||||
sprintf(buff, "cache size\t: %d\n", 4096);
|
||||
P;
|
||||
@@ -1760,7 +1747,7 @@ void CreateCPUInfoFile(int fd)
|
||||
P;
|
||||
sprintf(buff, "core id\t\t: %d\ncpu cores\t: %d\n", i, n);
|
||||
P;
|
||||
sprintf(buff, "bogomips\t: %g\n", getBogoMips());
|
||||
sprintf(buff, "bogomips\t: %g\n", box64_sysinfo.bogomips / 1000000.);
|
||||
P;
|
||||
sprintf(buff, "flags\t\t: fpu cx8 sep ht cmov clflush mmx sse sse2 syscall tsc lahf_lm ssse3 ht tm lm fxsr cpuid"\
|
||||
"%s cx16%s movbe pni "\
|
||||
@@ -1786,7 +1773,7 @@ void CreateCPUPresentFile(int fd)
|
||||
{
|
||||
size_t dummy;
|
||||
char buff[600];
|
||||
int n = getNCpu();
|
||||
int n = box64_sysinfo.box64_ncpu;
|
||||
// generate fake CPUINFO
|
||||
sprintf(buff, "0-%d\n", n-1);
|
||||
dummy = write(fd, buff, strlen(buff));
|
||||
@@ -2032,7 +2019,7 @@ EXPORT int32_t my_open64(x64emu_t* emu, void* pathname, int32_t flags, uint32_t
|
||||
lseek(tmp, 0, SEEK_SET);
|
||||
return tmp;
|
||||
}
|
||||
if(BOX64ENV(maxcpu) && (!strcmp(pathname, "/sys/devices/system/cpu/present") || !strcmp(pathname, "/sys/devices/system/cpu/online")) && (getNCpu()>=BOX64ENV(maxcpu))) {
|
||||
if (BOX64ENV(maxcpu) && (!strcmp(pathname, "/sys/devices/system/cpu/present") || !strcmp(pathname, "/sys/devices/system/cpu/online")) && (box64_sysinfo.ncpu >= BOX64ENV(maxcpu))) {
|
||||
// special case for cpu present (to limit to 64 cores)
|
||||
int tmp = shm_open(TMP_CPUPRESENT, O_RDWR | O_CREAT, S_IRWXU);
|
||||
if(tmp<0) return open64(pathname, mode); // error fallback
|
||||
@@ -2103,7 +2090,7 @@ EXPORT FILE* my_fopen64(x64emu_t* emu, const char* path, const char* mode)
|
||||
lseek(tmp, 0, SEEK_SET);
|
||||
return fdopen(tmp, mode);
|
||||
}
|
||||
if(BOX64ENV(maxcpu) && (!strcmp(path, "/sys/devices/system/cpu/present") || !strcmp(path, "/sys/devices/system/cpu/online")) && (getNCpu()>=BOX64ENV(maxcpu))) {
|
||||
if (BOX64ENV(maxcpu) && (!strcmp(path, "/sys/devices/system/cpu/present") || !strcmp(path, "/sys/devices/system/cpu/online")) && (box64_sysinfo.ncpu >= BOX64ENV(maxcpu))) {
|
||||
// special case for cpu present (to limit to 64 cores)
|
||||
int tmp = shm_open(TMP_CPUPRESENT, O_RDWR | O_CREAT, S_IRWXU);
|
||||
if(tmp<0) return fopen64(path, mode); // error fallback
|
||||
@@ -4004,7 +3991,6 @@ size_t __attribute__((weak)) __strlcpy_chk(char* dest, const char* src, size_t l
|
||||
return strlcpy(dest, src, len);
|
||||
}
|
||||
|
||||
uint32_t get_random32();
|
||||
__attribute__((weak)) uint32_t arc4random(void)
|
||||
{
|
||||
return get_random32();
|
||||
@@ -4042,10 +4028,10 @@ __attribute__((weak)) int dn_skipname(const unsigned char* ptr, const unsigned c
|
||||
#endif
|
||||
EXPORT long my_sysconf(x64emu_t* emu, int what) {
|
||||
if(what==_SC_NPROCESSORS_ONLN) {
|
||||
return getNCpu();
|
||||
return box64_sysinfo.box64_ncpu;
|
||||
}
|
||||
if(what==_SC_NPROCESSORS_CONF) {
|
||||
return getNCpu();
|
||||
return box64_sysinfo.box64_ncpu;
|
||||
}
|
||||
return sysconf(what);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "wrappedlibs.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "wrapper.h"
|
||||
#include "bridge.h"
|
||||
#include "librarian/library_private.h"
|
||||
@@ -37,10 +38,9 @@ EXPORT void my___TBB_internal_deallocate_binding_handler(void* handler)
|
||||
EXPORT void my___TBB_internal_destroy_system_topology()
|
||||
{ }
|
||||
|
||||
int getNCpu();
|
||||
EXPORT int my___TBB_internal_get_default_concurrency(int numa_id, int core_type, int max_threads_per_core)
|
||||
{
|
||||
return getNCpu();
|
||||
return box64_sysinfo.box64_ncpu;
|
||||
}
|
||||
|
||||
EXPORT void my___TBB_internal_restore_affinity(void* handler, int slot)
|
||||
|
||||
@@ -1586,8 +1586,6 @@ static int isProcSelf(const char *path, const char* w)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getNCpu();
|
||||
|
||||
#ifdef ANDROID
|
||||
static int shm_open(const char *name, int oflag, mode_t mode) {
|
||||
return -1;
|
||||
|
||||
@@ -94,8 +94,9 @@ set(WOW64_BOX64CPU_SRC
|
||||
"${BOX64_ROOT}/src/os/symbolfuncs_wine.c"
|
||||
"${BOX64_ROOT}/src/os/emit_signal_wine.c"
|
||||
"${BOX64_ROOT}/src/os/perfmap.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid_wine.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid_common.c"
|
||||
"${BOX64_ROOT}/src/os/random_wine.c"
|
||||
"${BOX64_ROOT}/src/os/my_cpuid.c"
|
||||
"${BOX64_ROOT}/src/os/sysinfo.c"
|
||||
"${BOX64_ROOT}/src/tools/alternate.c"
|
||||
"${BOX64_ROOT}/src/tools/bitutils.c"
|
||||
"${BOX64_ROOT}/src/tools/env.c"
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "wine/compiler.h"
|
||||
#include "wine/debug.h"
|
||||
#include "hostext.h"
|
||||
#include "sysinfo.h"
|
||||
|
||||
uintptr_t box64_pagesize = 4096;
|
||||
|
||||
@@ -36,6 +37,7 @@ uint8_t box64_rdtsc_shift = 0;
|
||||
int box64_is32bits = 0;
|
||||
int box64_unittest_mode = 0;
|
||||
int box64_wine = 0; // this is for the emulated x86 Wine.
|
||||
sysinfo_t box64_sysinfo = {0};
|
||||
|
||||
static uint32_t x86emu_parity_tab[8] = {
|
||||
0x96696996,
|
||||
@@ -244,6 +246,7 @@ NTSTATUS WINAPI BTCpuProcessInit(void)
|
||||
|
||||
LoadEnvVariables();
|
||||
InitializeEnvFiles();
|
||||
InitializeSystemInfo();
|
||||
|
||||
if (!BOX64ENV(nobanner)) PrintBox64Version(1);
|
||||
if (DetectHostCpuFeatures())
|
||||
|
||||
Reference in new issue
Block a user