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