about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2021-03-05 16:58:33 +0100
committerptitSeb <sebastien.chev@gmail.com>2021-03-05 16:58:33 +0100
commita1e3e0d5b12f2e1b39890585dfa658394901e07b (patch)
treeeb779406b126c5d61311fbfc3fb4aedd3a1566a0 /src
parente9fc44761c220d01101a53ec34286450de9f0c5a (diff)
downloadbox64-a1e3e0d5b12f2e1b39890585dfa658394901e07b.tar.gz
box64-a1e3e0d5b12f2e1b39890585dfa658394901e07b.zip
Added some phtread functions
Diffstat (limited to 'src')
-rwxr-xr-xsrc/libtools/threads.c138
-rw-r--r--src/wrapped/generated/functions_list.txt3
-rw-r--r--src/wrapped/generated/wrapper.c6
-rw-r--r--src/wrapped/generated/wrapper.h3
-rwxr-xr-xsrc/wrapped/wrappedlibpthread_private.h8
5 files changed, 86 insertions, 72 deletions
diff --git a/src/libtools/threads.c b/src/libtools/threads.c
index 64cc414a..e23ad0d4 100755
--- a/src/libtools/threads.c
+++ b/src/libtools/threads.c
@@ -16,12 +16,12 @@
 #include "x64run.h"
 #include "x64emu.h"
 #include "box64stack.h"
-//#include "callback.h"
+#include "callback.h"
 #include "custommem.h"
 #include "khash.h"
 #include "emu/x64run_private.h"
 #include "x64trace.h"
-//#include "dynarec.h"
+#include "dynarec.h"
 #include "bridge.h"
 #ifdef DYNAREC
 #include "dynablock.h"
@@ -228,12 +228,10 @@ static void* pthread_routine(void* p)
 	et->emu->type = EMUTYPE_MAIN;
 	// setup callstack and run...
 	x64emu_t* emu = et->emu;
-    R_RSP -= 4;
-	uint64_t *sp = (uint64_t*)R_RSP;
-	*sp = (uintptr_t)et->arg;
-//	PushExit(emu);
+	PushExit(emu);
 	R_RIP = et->fnc;
-//	DynaRun(et->emu);
+	R_RDI = (uintptr_t)et->arg;
+	DynaRun(emu);
 	void* ret = (void*)R_RAX;
 	//void* ret = (void*)RunFunctionWithEmu(et->emu, 0, et->fnc, 1, et->arg);
 	return ret;
@@ -265,47 +263,47 @@ EXPORT int my_pthread_attr_setstack(x64emu_t* emu, void* attr, void* stackaddr,
 	return pthread_attr_setstacksize(attr, stacksize);
 }
 
-//EXPORT int my_pthread_create(x64emu_t *emu, void* t, void* attr, void* start_routine, void* arg)
-//{
-//	int stacksize = 2*1024*1024;	//default stack size is 2Mo
-//	void* attr_stack;
-//	size_t attr_stacksize;
-//	int own;
-//	void* stack;
-//
-//	if(attr) {
-//		size_t stsize;
-//		if(pthread_attr_getstacksize(attr, &stsize)==0)
-//			stacksize = stsize;
-//	}
-//	if(GetStackSize(emu, (uintptr_t)attr, &attr_stack, &attr_stacksize))
-//	{
-//		stack = attr_stack;
-//		stacksize = attr_stacksize;
-//		own = 0;
-//	} else {
-//		stack = malloc(stacksize);
-//		own = 1;
-//	}
-//
-//	emuthread_t *et = (emuthread_t*)calloc(1, sizeof(emuthread_t));
-//    x64emu_t *emuthread = NewX86Emu(my_context, (uintptr_t)start_routine, (uintptr_t)stack, stacksize, own);
-//	SetupX86Emu(emuthread);
-//	SetFS(emuthread, GetFS(emu));
-//	et->emu = emuthread;
-//	et->fnc = (uintptr_t)start_routine;
-//	et->arg = arg;
-//	#ifdef DYNAREC
-//	if(box86_dynarec) {
-//		// pre-creation of the JIT code for the entry point of the thread
-//		dynablock_t *current = NULL;
-//		DBGetBlock(emu, (uintptr_t)start_routine, 1, &current);
-//	}
-//	#endif
-//	// create thread
-//	return pthread_create((pthread_t*)t, (const pthread_attr_t *)attr, 
-//		pthread_routine, et);
-//}
+EXPORT int my_pthread_create(x64emu_t *emu, void* t, void* attr, void* start_routine, void* arg)
+{
+	int stacksize = 2*1024*1024;	//default stack size is 2Mo
+	void* attr_stack;
+	size_t attr_stacksize;
+	int own;
+	void* stack;
+
+	if(attr) {
+		size_t stsize;
+		if(pthread_attr_getstacksize(attr, &stsize)==0)
+			stacksize = stsize;
+	}
+	if(GetStackSize(emu, (uintptr_t)attr, &attr_stack, &attr_stacksize))
+	{
+		stack = attr_stack;
+		stacksize = attr_stacksize;
+		own = 0;
+	} else {
+		stack = malloc(stacksize);
+		own = 1;
+	}
+
+	emuthread_t *et = (emuthread_t*)calloc(1, sizeof(emuthread_t));
+    x64emu_t *emuthread = NewX64Emu(my_context, (uintptr_t)start_routine, (uintptr_t)stack, stacksize, own);
+	SetupX64Emu(emuthread);
+	SetFS(emuthread, GetFS(emu));
+	et->emu = emuthread;
+	et->fnc = (uintptr_t)start_routine;
+	et->arg = arg;
+	#ifdef DYNAREC
+	if(box86_dynarec) {
+		// pre-creation of the JIT code for the entry point of the thread
+		dynablock_t *current = NULL;
+		DBGetBlock(emu, (uintptr_t)start_routine, 1, &current);
+	}
+	#endif
+	// create thread
+	return pthread_create((pthread_t*)t, (const pthread_attr_t *)attr, 
+		pthread_routine, et);
+}
 
 void* my_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** pet)
 {
@@ -383,7 +381,7 @@ void* my_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** pet
 //	// just in case it does return
 //	emu->quit = 1;
 //}
-#if 0
+
 KHASH_MAP_INIT_INT(once, int)
 
 #define SUPER() \
@@ -592,7 +590,7 @@ EXPORT int my_pthread_cond_wait(x64emu_t* emu, void* cond, void* mutex)
 	pthread_cond_t * c = get_cond(cond);
 	return pthread_cond_wait(c, getAlignedMutex((pthread_mutex_t*)mutex));
 }
-
+#if 0
 EXPORT int my_pthread_mutexattr_setkind_np(x64emu_t* emu, void* t, int kind)
 {
     // does "kind" needs some type of translation?
@@ -689,8 +687,12 @@ EXPORT void my_pthread_exit(x64emu_t* emu, void* retval)
 	emu->quit = 1;	// to be safe
 	pthread_exit(retval);
 }
-
-#ifndef NOALIGN
+#endif
+#ifdef NOALIGN
+pthread_mutex_t* getAlignedMutex(pthread_mutex_t* m) {
+	return m;
+}
+#else
 // mutex alignment
 KHASH_MAP_INIT_INT(mutex, pthread_mutex_t*)
 
@@ -755,7 +757,7 @@ EXPORT int my_pthread_mutex_unlock(pthread_mutex_t *m)
 }
 
 #endif
-#endif
+
 static void emujmpbuf_destroy(void* p)
 {
 	emu_jmpbuf_t *ej = (emu_jmpbuf_t*)p;
@@ -779,10 +781,10 @@ emu_jmpbuf_t* GetJmpBuf()
 void init_pthread_helper()
 {
 //	InitCancelThread();
-//	mapcond = kh_init(mapcond);
+	mapcond = kh_init(mapcond);
 	pthread_key_create(&jmpbuf_key, emujmpbuf_destroy);
 #ifndef NOALIGN
-//	unaligned_mutex = kh_init(mutex);
+	unaligned_mutex = kh_init(mutex);
 #endif
 }
 
@@ -790,19 +792,19 @@ void fini_pthread_helper(box64context_t* context)
 {
 //	FreeCancelThread(context);
 	CleanStackSize(context);
-//	pthread_cond_t *cond;
-//	kh_foreach_value(mapcond, cond, 
-//		pthread_cond_destroy(cond);
-//		free(cond);
-//	);
-//	kh_destroy(mapcond, mapcond);
-//	mapcond = NULL;
+	pthread_cond_t *cond;
+	kh_foreach_value(mapcond, cond, 
+		pthread_cond_destroy(cond);
+		free(cond);
+	);
+	kh_destroy(mapcond, mapcond);
+	mapcond = NULL;
 #ifndef NOALIGN
-//	pthread_mutex_t *m;
-//	kh_foreach_value(unaligned_mutex, m, 
-//		pthread_mutex_destroy(m);
-//		free(m);
-//	);
-//	kh_destroy(mutex, unaligned_mutex);
+	pthread_mutex_t *m;
+	kh_foreach_value(unaligned_mutex, m, 
+		pthread_mutex_destroy(m);
+		free(m);
+	);
+	kh_destroy(mutex, unaligned_mutex);
 #endif
 }
diff --git a/src/wrapped/generated/functions_list.txt b/src/wrapped/generated/functions_list.txt
index 1dd76985..fda934ac 100644
--- a/src/wrapped/generated/functions_list.txt
+++ b/src/wrapped/generated/functions_list.txt
@@ -6,6 +6,7 @@
 #() iFu
 #() iFp
 #() IFp
+#() uFv
 #() dFp
 #() lFp
 #() pFE
@@ -14,6 +15,7 @@
 #() vFpp
 #() iFEp
 #() iFip
+#() iFuu
 #() iFup
 #() pFEp
 #() pFLL
@@ -30,6 +32,7 @@
 #() iFuipp
 #() pFEppi
 #() pFEppp
+#() iFEpppp
 #() iFipppi
 #() iFEpippppp
 #() iFEv -> iFE
diff --git a/src/wrapped/generated/wrapper.c b/src/wrapped/generated/wrapper.c
index db22ceed..9627bc6b 100644
--- a/src/wrapped/generated/wrapper.c
+++ b/src/wrapped/generated/wrapper.c
@@ -77,6 +77,7 @@ typedef int32_t (*iFi_t)(int32_t);
 typedef int32_t (*iFu_t)(uint32_t);
 typedef int32_t (*iFp_t)(void*);
 typedef int64_t (*IFp_t)(void*);
+typedef uint32_t (*uFv_t)(void);
 typedef double (*dFp_t)(void*);
 typedef intptr_t (*lFp_t)(void*);
 typedef void* (*pFE_t)(x64emu_t*);
@@ -85,6 +86,7 @@ typedef void (*vFEp_t)(x64emu_t*, void*);
 typedef void (*vFpp_t)(void*, void*);
 typedef int32_t (*iFEp_t)(x64emu_t*, void*);
 typedef int32_t (*iFip_t)(int32_t, void*);
+typedef int32_t (*iFuu_t)(uint32_t, uint32_t);
 typedef int32_t (*iFup_t)(uint32_t, void*);
 typedef void* (*pFEp_t)(x64emu_t*, void*);
 typedef void* (*pFLL_t)(uintptr_t, uintptr_t);
@@ -101,6 +103,7 @@ typedef int32_t (*iFEppp_t)(x64emu_t*, void*, void*, void*);
 typedef int32_t (*iFuipp_t)(uint32_t, int32_t, void*, void*);
 typedef void* (*pFEppi_t)(x64emu_t*, void*, void*, int32_t);
 typedef void* (*pFEppp_t)(x64emu_t*, void*, void*, void*);
+typedef int32_t (*iFEpppp_t)(x64emu_t*, void*, void*, void*, void*);
 typedef int32_t (*iFipppi_t)(int32_t, void*, void*, void*, int32_t);
 typedef int32_t (*iFEpippppp_t)(x64emu_t*, void*, int32_t, void*, void*, void*, void*, void*);
 
@@ -112,6 +115,7 @@ void iFi(x64emu_t *emu, uintptr_t fcn) { iFi_t fn = (iFi_t)fcn; R_RAX=fn((int32_
 void iFu(x64emu_t *emu, uintptr_t fcn) { iFu_t fn = (iFu_t)fcn; R_RAX=fn((uint32_t)R_RDI); }
 void iFp(x64emu_t *emu, uintptr_t fcn) { iFp_t fn = (iFp_t)fcn; R_RAX=fn((void*)R_RDI); }
 void IFp(x64emu_t *emu, uintptr_t fcn) { IFp_t fn = (IFp_t)fcn; R_RAX=(uint64_t)fn((void*)R_RDI); }
+void uFv(x64emu_t *emu, uintptr_t fcn) { uFv_t fn = (uFv_t)fcn; R_RAX=(uint32_t)fn(); }
 void dFp(x64emu_t *emu, uintptr_t fcn) { dFp_t fn = (dFp_t)fcn; emu->xmm[0].d[0]=fn((void*)R_RDI); }
 void lFp(x64emu_t *emu, uintptr_t fcn) { lFp_t fn = (lFp_t)fcn; R_RAX=(intptr_t)fn((void*)R_RDI); }
 void pFE(x64emu_t *emu, uintptr_t fcn) { pFE_t fn = (pFE_t)fcn; R_RAX=(uintptr_t)fn(emu); }
@@ -120,6 +124,7 @@ void vFEp(x64emu_t *emu, uintptr_t fcn) { vFEp_t fn = (vFEp_t)fcn; fn(emu, (void
 void vFpp(x64emu_t *emu, uintptr_t fcn) { vFpp_t fn = (vFpp_t)fcn; fn((void*)R_RDI, (void*)R_RSI); }
 void iFEp(x64emu_t *emu, uintptr_t fcn) { iFEp_t fn = (iFEp_t)fcn; R_RAX=fn(emu, (void*)R_RDI); }
 void iFip(x64emu_t *emu, uintptr_t fcn) { iFip_t fn = (iFip_t)fcn; R_RAX=fn((int32_t)R_RDI, (void*)R_RSI); }
+void iFuu(x64emu_t *emu, uintptr_t fcn) { iFuu_t fn = (iFuu_t)fcn; R_RAX=fn((uint32_t)R_RDI, (uint32_t)R_RSI); }
 void iFup(x64emu_t *emu, uintptr_t fcn) { iFup_t fn = (iFup_t)fcn; R_RAX=fn((uint32_t)R_RDI, (void*)R_RSI); }
 void pFEp(x64emu_t *emu, uintptr_t fcn) { pFEp_t fn = (pFEp_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI); }
 void pFLL(x64emu_t *emu, uintptr_t fcn) { pFLL_t fn = (pFLL_t)fcn; R_RAX=(uintptr_t)fn((uintptr_t)R_RDI, (uintptr_t)R_RSI); }
@@ -136,6 +141,7 @@ void iFEppp(x64emu_t *emu, uintptr_t fcn) { iFEppp_t fn = (iFEppp_t)fcn; R_RAX=f
 void iFuipp(x64emu_t *emu, uintptr_t fcn) { iFuipp_t fn = (iFuipp_t)fcn; R_RAX=fn((uint32_t)R_RDI, (int32_t)R_RSI, (void*)R_RDX, (void*)R_RCX); }
 void pFEppi(x64emu_t *emu, uintptr_t fcn) { pFEppi_t fn = (pFEppi_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (int32_t)R_RDX); }
 void pFEppp(x64emu_t *emu, uintptr_t fcn) { pFEppp_t fn = (pFEppp_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI, (void*)R_RDX); }
+void iFEpppp(x64emu_t *emu, uintptr_t fcn) { iFEpppp_t fn = (iFEpppp_t)fcn; R_RAX=fn(emu, (void*)R_RDI, (void*)R_RSI, (void*)R_RDX, (void*)R_RCX); }
 void iFipppi(x64emu_t *emu, uintptr_t fcn) { iFipppi_t fn = (iFipppi_t)fcn; R_RAX=fn((int32_t)R_RDI, (void*)R_RSI, (void*)R_RDX, (void*)R_RCX, (int32_t)R_R8); }
 void iFEpippppp(x64emu_t *emu, uintptr_t fcn) { iFEpippppp_t fn = (iFEpippppp_t)fcn; R_RAX=fn(emu, (void*)R_RDI, (int32_t)R_RSI, (void*)R_RDX, (void*)R_RCX, (void*)R_R8, (void*)R_R9, *(void**)(R_RSP + 16)); }
 
diff --git a/src/wrapped/generated/wrapper.h b/src/wrapped/generated/wrapper.h
index 2b8bac8a..62820990 100644
--- a/src/wrapped/generated/wrapper.h
+++ b/src/wrapped/generated/wrapper.h
@@ -38,6 +38,7 @@ void iFi(x64emu_t *emu, uintptr_t fnc);
 void iFu(x64emu_t *emu, uintptr_t fnc);
 void iFp(x64emu_t *emu, uintptr_t fnc);
 void IFp(x64emu_t *emu, uintptr_t fnc);
+void uFv(x64emu_t *emu, uintptr_t fnc);
 void dFp(x64emu_t *emu, uintptr_t fnc);
 void lFp(x64emu_t *emu, uintptr_t fnc);
 void pFE(x64emu_t *emu, uintptr_t fnc);
@@ -46,6 +47,7 @@ void vFEp(x64emu_t *emu, uintptr_t fnc);
 void vFpp(x64emu_t *emu, uintptr_t fnc);
 void iFEp(x64emu_t *emu, uintptr_t fnc);
 void iFip(x64emu_t *emu, uintptr_t fnc);
+void iFuu(x64emu_t *emu, uintptr_t fnc);
 void iFup(x64emu_t *emu, uintptr_t fnc);
 void pFEp(x64emu_t *emu, uintptr_t fnc);
 void pFLL(x64emu_t *emu, uintptr_t fnc);
@@ -62,6 +64,7 @@ void iFEppp(x64emu_t *emu, uintptr_t fnc);
 void iFuipp(x64emu_t *emu, uintptr_t fnc);
 void pFEppi(x64emu_t *emu, uintptr_t fnc);
 void pFEppp(x64emu_t *emu, uintptr_t fnc);
+void iFEpppp(x64emu_t *emu, uintptr_t fnc);
 void iFipppi(x64emu_t *emu, uintptr_t fnc);
 void iFEpippppp(x64emu_t *emu, uintptr_t fnc);
 
diff --git a/src/wrapped/wrappedlibpthread_private.h b/src/wrapped/wrappedlibpthread_private.h
index e6f0f7f3..28e769cc 100755
--- a/src/wrapped/wrappedlibpthread_private.h
+++ b/src/wrapped/wrappedlibpthread_private.h
@@ -63,9 +63,9 @@ GOM(pthread_attr_destroy, iFEp)
 //GOM(pthread_cond_signal, iFEp)
 //GOM(pthread_cond_timedwait, iFEppp)
 //GOM(pthread_cond_wait, iFEpp)
-//GOM(pthread_create, iFEpppp)
+GOM(pthread_create, iFEpppp)
 //GO(pthread_detach, iFu)
-//GO(pthread_equal, iFuu)
+GO(pthread_equal, iFuu)
 //GOM(pthread_exit, vFEp)
 //GOM(pthread_getaffinity_np, iFEpup)
 //GO(pthread_getattr_np, iFup)
@@ -77,7 +77,7 @@ GOM(pthread_attr_destroy, iFEp)
 //GOM(pthread_getname_np, iFEppu)  //not present on Pandora
 //GOM(__pthread_initialize, vFv)  // doesn't exist anymore...
 // __pthread_initialize_minimal
-//GO(pthread_join, iFup)
+GO(pthread_join, iFup)
 //GOM(__pthread_key_create, iFEpp)
 //GOM(pthread_key_create, iFEpp)
 //GO(pthread_key_delete, iFu)
@@ -138,7 +138,7 @@ GOM(pthread_attr_destroy, iFEp)
 //GO2(pthread_rwlock_unlock, iFp, __pthread_rwlock_unlock)   // not always defined
 //GO(__pthread_rwlock_wrlock, iFp)
 //GO2(pthread_rwlock_wrlock, iFp, __pthread_rwlock_wrlock)    // not always defined
-//GO(pthread_self, uFv)
+GO(pthread_self, uFv)
 //GOM(pthread_setaffinity_np, iFELup)
 //GO(pthread_setcancelstate, iFip)
 //GO(pthread_setcanceltype, iFip)