8000 Moved more OS-dependent functions to os.h by ksco · Pull Request #2491 · ptitSeb/box64 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Moved more OS-dependent functions to os.h #2491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/box64context.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/mman.h>
#include <pthread.h>

#include "os.h"
#include "box64context.h"
#include "debug.h"
#include "elfloader.h"
Expand Down
1 change: 1 addition & 0 deletions src/custommem.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <syscall.h>
#include <sys/personality.h>

#include "os.h"
#include "box64context.h"
#include "elfloader.h"
#include "debug.h"
Expand Down
20 changes: 5 additions & 15 deletions src/dynarec/dynablock.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/mman.h>

#include "os.h"
#include "debug.h"
Expand Down Expand Up @@ -183,17 +181,11 @@ dynablock_t *AddNewDynablock(uintptr_t addr)
return block;
}

//TODO: move this to dynrec_arm.c and track allocated structure to avoid memory leak
static __thread JUMPBUFF dynarec_jmpbuf;
#ifdef ANDROID
#define DYN_JMPBUF dynarec_jmpbuf
#else
#define DYN_JMPBUF &dynarec_jmpbuf
#endif
NEW_JUMPBUFF(dynarec_jmpbuf);

void cancelFillBlock()
{
longjmp(DYN_JMPBUF, 1);
LongJmp(GET_JUMPBUFF(dynarec_jmpbuf), 1);
}

/*
Expand Down Expand Up @@ -236,7 +228,7 @@ static dynablock_t* internalDBGetBlock(x64emu_t* emu, uintptr_t addr, uintptr_t

// fill the block
block->x64_addr = (void*)addr;
if(sigsetjmp(DYN_JMPBUF, 1)) {
if (SigSetJmp(GET_JUMPBUFF(dynarec_jmpbuf), 1)) {
printf_log(LOG_INFO, "FillBlock at %p triggered a segfault, canceling\n", (void*)addr);
FreeDynablock(block, 0);
if(need_lock)
Expand Down Expand Up @@ -281,8 +273,7 @@ dynablock_t* DBGetBlock(x64emu_t* emu, uintptr_t addr, int create, int is32bits)
return NULL;
dynablock_t *db = internalDBGetBlock(emu, addr, addr, create, 1, is32bits);
if(db && db->done && db->block && getNeedTest(addr)) {
if(db->always_test)
sched_yield(); // just calm down...
if (db->always_test) SchedYield(); // just calm down...
uint32_t hash = X31_hash_code(db->x64_addr, db->x64_size);
int need_lock = mutex_trylock(&my_context->mutex_dyndump);
if(hash!=db->hash) {
Expand Down Expand Up @@ -319,8 +310,7 @@ dynablock_t* DBAlternateBlock(x64emu_t* emu, uintptr_t addr, uintptr_t filladdr,
int create = 1;
dynablock_t *db = internalDBGetBlock(emu, addr, filladdr, create, 1, is32bits);
if(db && db->done && db->block && getNeedTest(filladdr)) {
if(db->always_test)
sched_yield(); // just calm down...
if (db->always_test) SchedYield(); // just calm down...
int need_lock = mutex_trylock(&my_context->mutex_dyndump);
uint32_t hash = X31_hash_code(db->x64_addr, db->x64_size);
if(hash!=db->hash) {
Expand Down
5 changes: 2 additions & 3 deletions src/dynarec/dynarec.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <setjmp.h>

#include "os.h"
#include "debug.h"
Expand Down Expand Up @@ -168,9 +167,9 @@ void DynaRun(x64emu_t* emu)
#endif
emu->flags.jmpbuf_ready = 1;
#ifdef ANDROID
if((skip=sigsetjmp(*(JUMPBUFF*)emu->jmpbuf, 1)))
if ((skip = SigSetJmp(*(JUMPBUFF*)emu->jmpbuf, 1)))
#else
if((skip=sigsetjmp(emu->jmpbuf, 1)))
if ((skip = SigSetJmp(emu->jmpbuf, 1)))
#endif
{
printf_log(LOG_DEBUG, "Setjmp DynaRun, fs=0x%x\n", emu->segs[_FS]);
Expand Down
8 changes: 1 addition & 7 deletions src/emu/x64emu_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __X86EMU_PRIVATE_H_

#include "regs.h"
#include "os.h"

typedef struct box64context_s box64context_t;
typedef struct x64_ucontext_s x64_ucontext_t;
Expand Down Expand Up @@ -51,13 +52,6 @@ typedef struct emu_flags_s {
uint32_t jmpbuf_ready:1; // the jmpbuf in the emu is ok and don't need refresh
} emu_flags_t;

#ifdef ANDROID
#include <setjmp.h>
#define JUMPBUFF sigjmp_buf
#else
#define JUMPBUFF struct __jmp_buf_tag
#endif

#define N_SCRATCH 200

typedef struct x64emu_s {
Expand Down
1 change: 1 addition & 0 deletions src/emu/x64run_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sys/syscall.h>
#endif

#include "os.h"
#include "debug.h"
#include "box64stack.h"
#include "x64emu.h"
Expand Down
1 change: 1 addition & 0 deletions src/emu/x64tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>

#include "os.h"
#include "debug.h"
#include "box64context.h"
#include "x64emu.h"
Expand Down
10 changes: 0 additions & 10 deletions src/include/box64context.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,6 @@ typedef struct box64context_s {

} box64context_t;

#ifndef USE_CUSTOM_MUTEX
#define mutex_lock(A) pthread_mutex_lock(A)
#define mutex_trylock(A) pthread_mutex_trylock(A)
#define mutex_unlock(A) pthread_mutex_unlock(A)
#else
#define mutex_lock(A) {uint32_t tid = (uint32_t)GetTID(); while(native_lock_storeifnull_d(A, tid)) sched_yield();}
#define mutex_trylock(A) native_lock_storeifnull_d(A, (uint32_t)GetTID())
#define mutex_unlock(A) native_lock_storeifref_d(A, 0, (uint32_t)GetTID())
#endif

extern box64context_t *my_context; // global context

box64context_t *NewBox64Context(int argc);
Expand Down
46 changes: 46 additions & 0 deletions src/include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,51 @@
#define __OS_H_

int GetTID(void);
int SchedYield(void);

#ifndef _WIN32
#include <setjmp.h>
#define LongJmp longjmp
#define SigSetJmp sigsetjmp
#else
#define LongJmp(a, b)
#define SigSetJmp(a, b) 0
#endif

#ifndef USE_CUSTOM_MUTEX
#define mutex_lock(A) pthread_mutex_lock(A)
#define mutex_trylock(A) pthread_mutex_trylock(A)
#define mutex_unlock(A) pthread_mutex_unlock(A)
#else
#define mutex_lock(A) \
do { \
uint32_t tid = (uint32_t)GetTID(); \
while (native_lock_storeifnull_d(A, tid)) \
sched_yield(); \
} while (0)
#define mutex_trylock(A) native_lock_storeifnull_d(A, (uint32_t)GetTID())
#define mutex_unlock(A) native_lock_storeifref_d(A, 0, (uint32_t)GetTID())
#endif

#ifndef _WIN32
#include <setjmp.h>
#define NEW_JUMPBUFF(name) \
static __thread JUMPBUFF name
#ifdef ANDROID
#define JUMPBUFF sigjmp_buf
#define GET_JUMPBUFF(name) name
#else
#define JUMPBUFF struct __jmp_buf_tag
#define GET_JUMPBUFF(name) &name
#endif
#else
#define JUMPBUFF int
#define NEW_JUMPBUFF(name)
#define GET_JUMPBUFF(name) NULL
#endif

#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define PROT_EXEC 0x4

#endif //__OS_H_
10 changes: 9 additions & 1 deletion src/os/os_linux.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include <sys/syscall.h>
#include <sched.h>
#include <unistd.h>

int GetTID()
#include "os.h"

int GetTID(void)
{
return syscall(SYS_gettid);
}

int SchedYield(void)
{
return sched_yield();
}
3 changes: 2 additions & 1 deletion src/tools/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <sys/mman.h>
#include <errno.h>

#include <wrappedlibs.h>
#include "os.h"
#include "wrappedlibs.h"
#include "custommem.h"
#include "bridge.h"
#include "bridge_private.h"
Expand Down
0