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