]> gitweb.factorcode.org Git - factor.git/blob - vm/os-windows-nt.cpp
Start windows factor in a spawned thread (for testing)
[factor.git] / vm / os-windows-nt.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void *start_thread(void *(*start_routine)(void *),void *args){
7     return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); 
8 }
9
10
11 s64 factorvm::current_micros()
12 {
13         FILETIME t;
14         GetSystemTimeAsFileTime(&t);
15         return (((s64)t.dwLowDateTime | (s64)t.dwHighDateTime<<32)
16                 - EPOCH_OFFSET) / 10;
17 }
18
19 FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe)
20 {
21         //printf("exception handler %d\n",GetCurrentThreadId());fflush(stdout);
22         factorvm *myvm = lookup_vm(GetCurrentThreadId());
23         PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord;
24         CONTEXT *c = (CONTEXT*)pe->ContextRecord;
25
26         if(myvm->in_code_heap_p(c->EIP))
27                 myvm->signal_callstack_top = (stack_frame *)c->ESP;
28         else
29                 myvm->signal_callstack_top = NULL;
30
31     switch (e->ExceptionCode) {
32     case EXCEPTION_ACCESS_VIOLATION:
33                 myvm->signal_fault_addr = e->ExceptionInformation[1];
34                 c->EIP = (cell)memory_signal_handler_impl;
35         break;
36
37         case STATUS_FLOAT_DENORMAL_OPERAND:
38         case STATUS_FLOAT_DIVIDE_BY_ZERO:
39         case STATUS_FLOAT_INEXACT_RESULT:
40         case STATUS_FLOAT_INVALID_OPERATION:
41         case STATUS_FLOAT_OVERFLOW:
42         case STATUS_FLOAT_STACK_CHECK:
43         case STATUS_FLOAT_UNDERFLOW:
44         case STATUS_FLOAT_MULTIPLE_FAULTS:
45         case STATUS_FLOAT_MULTIPLE_TRAPS:
46                 myvm->signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c));
47                 X87SW(c) = 0;
48                 MXCSR(c) &= 0xffffffc0;
49                 c->EIP = (cell)fp_signal_handler_impl;
50                 break;
51         case 0x40010006:
52                 /* If the Widcomm bluetooth stack is installed, the BTTray.exe
53                 process injects code into running programs. For some reason this
54                 results in random SEH exceptions with this (undocumented)
55                 exception code being raised. The workaround seems to be ignoring
56                 this altogether, since that is what happens if SEH is not
57                 enabled. Don't really have any idea what this exception means. */
58                 break;
59         default:
60                 myvm->signal_number = e->ExceptionCode;
61                 c->EIP = (cell)misc_signal_handler_impl;
62                 break;
63         }
64         return EXCEPTION_CONTINUE_EXECUTION;
65 }
66
67 void factorvm::c_to_factor_toplevel(cell quot)
68 {
69         if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler))
70                 fatal_error("AddVectoredExceptionHandler failed", 0);
71         c_to_factor(quot,this);
72         RemoveVectoredExceptionHandler((void *)exception_handler);
73 }
74
75 void factorvm::open_console()
76 {
77 }
78
79 }