From 597826f00530e8d0c6f4a8ccda2e366f56b65579 Mon Sep 17 00:00:00 2001 From: Cyrus Harmon Date: Sun, 29 Jul 2012 06:33:45 -0700 Subject: [PATCH] Miscellaneous cleanups for threaded darwin platforms * Gather some related declarations in fewer (conditionalised) places * Lay down some infrastructure for mach port different from threads' addresses * Slightly modified by Paul Khuong --- src/compiler/generic/objdef.lisp | 4 +++- src/runtime/darwin-os.c | 24 +++++++++++++++++++++++- src/runtime/thread.c | 15 +-------------- src/runtime/thread.h | 4 ++-- src/runtime/x86-64-bsd-os.c | 8 +------- src/runtime/x86-64-darwin-os.c | 2 -- src/runtime/x86-darwin-os.c | 11 +++++------ 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/compiler/generic/objdef.lisp b/src/compiler/generic/objdef.lisp index dcc1c72cf..938990cae 100644 --- a/src/compiler/generic/objdef.lisp +++ b/src/compiler/generic/objdef.lisp @@ -419,8 +419,10 @@ ;; Same as above for the location of the current control stack ;; pointer. This is also used on threaded x86oids to allow LDB to ;; print an approximation of the CSP as needed. - #!+(and sb-thread) + #!+sb-thread (control-stack-pointer :c-type "lispobj *") + #!+mach-exception-handler + (mach-port-name :c-type "mach_port_name_t") ;; KLUDGE: On alpha, until STEPPING we have been lucky and the 32 ;; bit slots came in pairs. However the C compiler will align ;; interrupt_contexts on a double word boundary. This logic should diff --git a/src/runtime/darwin-os.c b/src/runtime/darwin-os.c index fbbae266e..9cbf12872 100644 --- a/src/runtime/darwin-os.c +++ b/src/runtime/darwin-os.c @@ -148,10 +148,32 @@ mach_thread_init(mach_port_t thread_exception_port) return ret; } +kern_return_t +mach_lisp_thread_init(struct thread *thread) { + mach_port_t port = (mach_port_t) thread; + kern_return_t ret; + ret = mach_thread_init(port); + thread->mach_port_name = port; + + return ret; +} + +kern_return_t +mach_lisp_thread_destroy(struct thread *thread) { + mach_port_t port = (mach_port_t) thread; + + FSHOW((stderr, "Deallocating mach port %x\n", port)); + mach_port_move_member(current_mach_task, port, MACH_PORT_NULL); + mach_port_deallocate(current_mach_task, port); + + return mach_port_destroy(current_mach_task, port); +} + void setup_mach_exceptions() { + mach_port_t port = (mach_port_t) all_threads; setup_mach_exception_handling_thread(); - mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads)); + mach_thread_init(port); } pid_t diff --git a/src/runtime/thread.c b/src/runtime/thread.c index 7a18c5a42..4b42d1d29 100644 --- a/src/runtime/thread.c +++ b/src/runtime/thread.c @@ -392,14 +392,7 @@ new_thread_trampoline(struct thread *th) (sizeof (struct interrupt_data))); #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER - FSHOW((stderr, "Deallocating mach port %x\n", THREAD_STRUCT_TO_EXCEPTION_PORT(th))); - mach_port_move_member(current_mach_task, - THREAD_STRUCT_TO_EXCEPTION_PORT(th), - MACH_PORT_NULL); - mach_port_deallocate(current_mach_task, - THREAD_STRUCT_TO_EXCEPTION_PORT(th)); - mach_port_destroy(current_mach_task, - THREAD_STRUCT_TO_EXCEPTION_PORT(th)); + mach_lisp_thread_destroy(th); #endif schedule_thread_post_mortem(th); @@ -590,12 +583,6 @@ create_thread_struct(lispobj initial_function) { return th; } -#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER -mach_port_t setup_mach_exception_handling_thread(); -kern_return_t mach_thread_init(mach_port_t thread_exception_port); - -#endif - void create_initial_thread(lispobj initial_function) { struct thread *th=create_thread_struct(initial_function); #ifdef LISP_FEATURE_SB_THREAD diff --git a/src/runtime/thread.h b/src/runtime/thread.h index b8b99d3c0..76ec1a63c 100644 --- a/src/runtime/thread.h +++ b/src/runtime/thread.h @@ -229,8 +229,8 @@ static inline struct thread *arch_os_get_current_thread(void) } #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER) -#define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th) -#define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th) +extern kern_return_t mach_lisp_thread_init(struct thread *thread); +extern kern_return_t mach_lisp_thread_destroy(struct thread *thread); #endif extern void create_initial_thread(lispobj); diff --git a/src/runtime/x86-64-bsd-os.c b/src/runtime/x86-64-bsd-os.c index 8c545036f..3d9a9c74e 100644 --- a/src/runtime/x86-64-bsd-os.c +++ b/src/runtime/x86-64-bsd-os.c @@ -12,12 +12,6 @@ #include #endif -#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER -#include - -kern_return_t mach_thread_init(mach_port_t thread_exception_port); -#endif - /* KLUDGE: There is strong family resemblance in the signal context * stuff in FreeBSD and OpenBSD, but in detail they're different in * almost every line of code. It would be nice to find some way to @@ -155,7 +149,7 @@ int arch_os_thread_init(struct thread *thread) { #endif #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER - mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread)); + mach_lisp_thread_init(thread); #endif #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK diff --git a/src/runtime/x86-64-darwin-os.c b/src/runtime/x86-64-darwin-os.c index 1050ade4e..8a25e639a 100644 --- a/src/runtime/x86-64-darwin-os.c +++ b/src/runtime/x86-64-darwin-os.c @@ -65,8 +65,6 @@ pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER; #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER -kern_return_t mach_thread_init(mach_port_t thread_exception_port); - void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context); void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context); void memory_fault_handler(int signal, siginfo_t *siginfo, diff --git a/src/runtime/x86-darwin-os.c b/src/runtime/x86-darwin-os.c index bdc18543c..edcd85cc1 100644 --- a/src/runtime/x86-darwin-os.c +++ b/src/runtime/x86-darwin-os.c @@ -44,10 +44,6 @@ void set_data_desc_addr(data_desc_t* desc, void* addr) #endif -#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER -kern_return_t mach_thread_init(mach_port_t thread_exception_port); -#endif - int arch_os_thread_init(struct thread *thread) { #ifdef LISP_FEATURE_SB_THREAD int n; @@ -79,7 +75,7 @@ int arch_os_thread_init(struct thread *thread) { pthread_setspecific(specials,thread); #endif #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER - mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread)); + mach_lisp_thread_init(thread); #endif #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK @@ -386,7 +382,6 @@ catch_exception_raise(mach_port_t exception_port, exception_data_t code_vector, mach_msg_type_number_t code_count) { - struct thread *th = (struct thread*) exception_port; x86_thread_state32_t thread_state; mach_msg_type_number_t state_count; vm_address_t region_addr; @@ -400,6 +395,10 @@ catch_exception_raise(mach_port_t exception_port, siginfo_t siginfo; kern_return_t ret, dealloc_ret; + struct thread *th = (struct thread*) exception_port; + + FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception)); + /* Get state and info */ state_count = x86_THREAD_STATE32_COUNT; if ((ret = thread_get_state(thread, -- 2.11.4.GIT