From 9cac5e8aa99cc255eac8c32e0a2759076a82e273 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 20:46:34 +0100 Subject: [PATCH] added lookup_vm and removed last usage of vm from windows code --- vm/errors.cpp | 6 +++--- vm/errors.hpp | 7 ------- vm/factor.cpp | 15 +++++++++++++++ vm/mach_signal.cpp | 8 ++++---- vm/os-unix.cpp | 8 ++++---- vm/os-unix.hpp | 1 + vm/os-windows-nt.cpp | 23 ++++++++++++----------- vm/os-windows-nt.hpp | 5 +++-- vm/vm.hpp | 8 ++++++++ 9 files changed, 50 insertions(+), 31 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index 09b397dd02..09e6313f29 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -165,7 +165,7 @@ void factorvm::memory_signal_handler_impl() void memory_signal_handler_impl() { - return vm->memory_signal_handler_impl(); + SIGNAL_VM_PTR->misc_signal_handler_impl(); } void factorvm::misc_signal_handler_impl() @@ -175,7 +175,7 @@ void factorvm::misc_signal_handler_impl() void misc_signal_handler_impl() { - vm->misc_signal_handler_impl(); + SIGNAL_VM_PTR->misc_signal_handler_impl(); } void factorvm::fp_signal_handler_impl() @@ -185,7 +185,7 @@ void factorvm::fp_signal_handler_impl() void fp_signal_handler_impl() { - vm->fp_signal_handler_impl(); + SIGNAL_VM_PTR->fp_signal_handler_impl(); } } diff --git a/vm/errors.hpp b/vm/errors.hpp index cc85cfd0a8..8725941920 100755 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -27,13 +27,6 @@ PRIMITIVE(die); PRIMITIVE(call_clear); PRIMITIVE(unimplemented); -/* Global variables used to pass fault handler state from signal handler to -user-space */ -extern cell signal_number; -extern cell signal_fault_addr; -extern unsigned int signal_fpu_status; -extern stack_frame *signal_callstack_top; - void memory_signal_handler_impl(); void fp_signal_handler_impl(); void misc_signal_handler_impl(); diff --git a/vm/factor.cpp b/vm/factor.cpp index 0c47e2329a..6e31a02cab 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -5,6 +5,19 @@ namespace factor factorvm *vm; +unordered_map thread_vms; + +factorvm *lookup_vm(long threadid) +{ + return thread_vms[threadid]; +} + +void register_vm(long threadid, factorvm *vm) +{ + thread_vms[threadid] = vm; +} + + void factorvm::default_parameters(vm_parameters *p) { p->image_path = NULL; @@ -199,6 +212,8 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { + printf("thread id is %d\n",GetCurrentThreadId());fflush(stdout); + register_vm(GetCurrentThreadId(),this); vm_parameters p; default_parameters(&p); init_parameters_from_args(&p,argc,argv); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index 7b050f72dc..ca3b7aa161 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -42,17 +42,17 @@ static void call_fault_handler( /* Are we in compiled Factor code? Then use the current stack pointer */ if(vm->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) - signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); + vm->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); /* Are we in C? Then use the saved callstack top */ else - signal_callstack_top = NULL; + vm->signal_callstack_top = NULL; MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state)); /* Now we point the program counter at the right handler function. */ if(exception == EXC_BAD_ACCESS) { - signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); + vm->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl; } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) @@ -63,7 +63,7 @@ static void call_fault_handler( } else { - signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); + vm->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl; } } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 3cb7295996..97e1e0c04d 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -120,15 +120,15 @@ static stack_frame *uap_stack_pointer(void *uap) void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - signal_fault_addr = (cell)siginfo->si_addr; - signal_callstack_top = uap_stack_pointer(uap); + vm->signal_fault_addr = (cell)siginfo->si_addr; + vm->signal_callstack_top = uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)memory_signal_handler_impl; } void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - signal_number = signal; - signal_callstack_top = uap_stack_pointer(uap); + vm->signal_number = signal; + vm->signal_callstack_top = uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)misc_signal_handler_impl; } diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 8aff18364e..b7e528a421 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -58,4 +58,5 @@ void sleep_micros(cell usec); void open_console(); +#define SIGNAL_VM_PTR vm } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 49594ed14a..a4cdbc66af 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,8 +3,8 @@ namespace factor { -void start_thread(void *(*start_routine)(void *),void *args){ - CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); +void *start_thread(void *(*start_routine)(void *),void *args){ + return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } @@ -18,18 +18,19 @@ s64 factorvm::current_micros() FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { + printf("exception handler %d\n",GetCurrentThreadId()); + factorvm *myvm = lookup_vm(GetCurrentThreadId()); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; - if(vm->in_code_heap_p(c->EIP)) - signal_callstack_top = (stack_frame *)c->ESP; + if(myvm->in_code_heap_p(c->EIP)) + myvm->signal_callstack_top = (stack_frame *)c->ESP; else - signal_callstack_top = NULL; + myvm->signal_callstack_top = NULL; - switch (e->ExceptionCode) - { - case EXCEPTION_ACCESS_VIOLATION: - signal_fault_addr = e->ExceptionInformation[1]; + switch (e->ExceptionCode) { + case EXCEPTION_ACCESS_VIOLATION: + myvm->signal_fault_addr = e->ExceptionInformation[1]; c->EIP = (cell)memory_signal_handler_impl; break; @@ -42,7 +43,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) case STATUS_FLOAT_UNDERFLOW: case STATUS_FLOAT_MULTIPLE_FAULTS: case STATUS_FLOAT_MULTIPLE_TRAPS: - signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); + myvm->signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); X87SW(c) = 0; MXCSR(c) &= 0xffffffc0; c->EIP = (cell)fp_signal_handler_impl; @@ -56,7 +57,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) enabled. Don't really have any idea what this exception means. */ break; default: - signal_number = e->ExceptionCode; + myvm->signal_number = e->ExceptionCode; c->EIP = (cell)misc_signal_handler_impl; break; } diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index 27923a000a..e5c5adadf1 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -21,12 +21,13 @@ typedef char symbol_char; FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); - // SSE traps raise these exception codes, which are defined in internal NT headers // but not winbase.h #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5 -void start_thread(void *(*start_routine)(void *),void *args); +void *start_thread(void *(*start_routine)(void *),void *args); + +#define SIGNAL_VM_PTR lookup_vm(GetCurrentThreadId()) } diff --git a/vm/vm.hpp b/vm/vm.hpp index 50006d012a..b3e40640b7 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -59,6 +59,12 @@ struct factorvm { inline void vmprim_profiling(); // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + cell signal_number; + cell signal_fault_addr; + unsigned int signal_fpu_status; + stack_frame *signal_callstack_top; void out_of_memory(); void fatal_error(const char* msg, cell tagged); void critical_error(const char* msg, cell tagged); @@ -692,4 +698,6 @@ struct factorvm { extern factorvm *vm; +extern factorvm *lookup_vm(long threadid); +extern void register_vm(long threadid,factorvm *vm); } -- 2.34.1