Skip to content

Commit

Permalink
Added some virtual files (if missing) to allow wine to compute caches…
Browse files Browse the repository at this point in the history
…ize and coherency (backported from box64)
  • Loading branch information
ptitSeb committed May 19, 2024
1 parent 148722d commit 13c7029
Showing 1 changed file with 165 additions and 2 deletions.
167 changes: 165 additions & 2 deletions src/wrapped/wrappedlibc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,19 @@ static int isProcSelf(const char *path, const char* w)
return 0;
}

static int isSysCpuCache(const char *path, const char* w, int* _cpu, int* _index)
{
char tmp[128];
int cpu, index;
if(sscanf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/%s", &cpu, &index, tmp)!=3)
return 0;
if(strcmp(tmp, w))
return 0;
if(_cpu) * _cpu = cpu;
if(_index) *_index = index;
return 1;
}

EXPORT int32_t my_readlink(x86emu_t* emu, void* path, void* buf, uint32_t sz)
{
if(isProcSelf((const char*)path, "exe")) {
Expand Down Expand Up @@ -1761,7 +1774,42 @@ void CreateCPUPresentFile(int fd)
dummy = write(fd, buff, strlen(buff));
(void)dummy;
}

void CreateClocksourceFile(int fd)
{
size_t dummy;
dummy = write(fd, "tsc\n", strlen("tsc\n"));
(void)dummy;
}
void CreateCpuCacheAssoc(int fd, int cpu, int index)
{
size_t dummy;
char tmp[64];
sprintf(tmp, "%d\n", (index>=3)?16:8); // Random be coherent values...
dummy = write(fd, tmp, strlen(tmp));
(void)dummy;
}
void CreateCpuCacheCoher(int fd, int cpu, int index)
{
size_t dummy;
char tmp[64];
sprintf(tmp, "%d\n", 64); // Random be coherent values...
dummy = write(fd, tmp, strlen(tmp));
(void)dummy;
}
void CreateCpuCacheSize(int fd, int cpu, int index)
{
size_t dummy;
char tmp[64];
int cachesize = 12288;
switch(index) {
case 0: cachesize = 32; break;
case 1: cachesize = 32; break;
case 2: cachesize = 256; break;
}
sprintf(tmp, "%dK\n", cachesize); // Random be coherent values...
dummy = write(fd, tmp, strlen(tmp));
(void)dummy;
}

#ifdef ANDROID
static int shm_open(const char *name, int oflag, mode_t mode) {
Expand All @@ -1777,7 +1825,10 @@ static int shm_unlink(const char *name) {
#endif
#define TMP_MEMMAP "box86_tmpmemmap"
#define TMP_CMDLINE "box86_tmpcmdline"
#define TMP_CPUPRESENT "box64_cpupresent"
#define TMP_CPUPRESENT "box86_cpupresent"
#define TMP_CPUCACHE_ASSOC "box86_cpucacheassoc"
#define TMP_CPUCACHE_COHER "box86_cpucachecoher"
#define TMP_CPUCACHE_SIZE "box86_cpucachesize"
EXPORT int32_t my_open(x86emu_t* emu, void* pathname, int32_t flags, uint32_t mode)
{
if(isProcSelf((const char*) pathname, "cmdline")) {
Expand Down Expand Up @@ -1830,6 +1881,34 @@ EXPORT int32_t my_open(x86emu_t* emu, void* pathname, int32_t flags, uint32_t mo
lseek(tmp, 0, SEEK_SET);
return tmp;
}
int cpu, index;
if(isSysCpuCache(pathname, "ways_of_associativity", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_ASSOC, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_ASSOC); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
if(isSysCpuCache(pathname, "coherency_line_size", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_COHER, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_COHER); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheCoher(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
if(isSysCpuCache(pathname, "size", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_SIZE, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_SIZE); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
#endif
int ret = open(pathname, flags, mode);
return ret;
Expand Down Expand Up @@ -1919,6 +1998,34 @@ EXPORT int32_t my_open64(x86emu_t* emu, void* pathname, int32_t flags, uint32_t
lseek(tmp, 0, SEEK_SET);
return tmp;
}
int cpu, index;
if(isSysCpuCache(pathname, "ways_of_associativity", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_ASSOC, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_ASSOC); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
if(isSysCpuCache(pathname, "coherency_line_size", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_COHER, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_COHER); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheCoher(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
if(isSysCpuCache(pathname, "size", &cpu, &index) && !FileExist(pathname, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_SIZE, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return open(pathname, flags, mode); // error fallback
shm_unlink(TMP_CPUCACHE_SIZE); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return tmp;
}
#endif
return open64(pathname, flags, mode);
}
Expand Down Expand Up @@ -1964,6 +2071,34 @@ EXPORT FILE* my_fopen(x86emu_t* emu, const char* path, const char* mode)
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
int cpu, index;
if(isSysCpuCache(path, "ways_of_associativity", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_ASSOC, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_ASSOC); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
if(isSysCpuCache(path, "coherency_line_size", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_COHER, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_COHER); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheCoher(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
if(isSysCpuCache(path, "size", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_SIZE, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_SIZE); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
#endif
if(isProcSelf(path, "exe")) {
return fopen(emu->context->fullpath, mode);
Expand Down Expand Up @@ -2012,6 +2147,34 @@ EXPORT FILE* my_fopen64(x86emu_t* emu, const char* path, const char* mode)
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
int cpu, index;
if(isSysCpuCache(path, "ways_of_associativity", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_ASSOC, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_ASSOC); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
if(isSysCpuCache(path, "coherency_line_size", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_COHER, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_COHER); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheCoher(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
if(isSysCpuCache(path, "size", &cpu, &index) && !FileExist(path, IS_FILE)) {
// Create a dummy one
int tmp = shm_open(TMP_CPUCACHE_SIZE, O_RDWR | O_CREAT, S_IRWXU);
if(tmp<0) return fopen64(path, mode); // error fallback
shm_unlink(TMP_CPUCACHE_SIZE); // remove the shm file, but it will still exist because it's currently in use
CreateCpuCacheAssoc(tmp, cpu, index);
lseek(tmp, 0, SEEK_SET);
return fdopen(tmp, mode);
}
#endif
if(isProcSelf(path, "exe")) {
return fopen64(emu->context->fullpath, mode);
Expand Down

0 comments on commit 13c7029

Please sign in to comment.