Make --aot=full able to emit executables on 64 bit Windows
[mono.git] / mono / mini / mini-windows.c
1 /*
2  * mini-posix.c: POSIX signal handling support for Mono.
3  *
4  * Authors:
5  *   Mono Team (mono-list@lists.ximian.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc.
8  * Copyright 2003-2008 Ximian, Inc.
9  *
10  * See LICENSE for licensing information.
11  */
12 #include <config.h>
13 #include <signal.h>
14 #include <math.h>
15
16 #include <mono/metadata/assembly.h>
17 #include <mono/metadata/loader.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/io-layer/io-layer.h>
27 #include "mono/metadata/profiler.h"
28 #include <mono/metadata/profiler-private.h>
29 #include <mono/metadata/mono-config.h>
30 #include <mono/metadata/environment.h>
31 #include <mono/metadata/mono-debug.h>
32 #include <mono/metadata/gc-internals.h>
33 #include <mono/metadata/threads-types.h>
34 #include <mono/metadata/verify.h>
35 #include <mono/metadata/verify-internals.h>
36 #include <mono/metadata/mempool-internals.h>
37 #include <mono/metadata/attach.h>
38 #include <mono/utils/mono-math.h>
39 #include <mono/utils/mono-compiler.h>
40 #include <mono/utils/mono-counters.h>
41 #include <mono/utils/mono-logger-internals.h>
42 #include <mono/utils/mono-mmap.h>
43 #include <mono/utils/dtrace.h>
44
45 #include "mini.h"
46 #include <string.h>
47 #include <ctype.h>
48 #include "trace.h"
49 #include "version.h"
50
51 #include "jit-icalls.h"
52
53 #ifdef _WIN32
54 #include <mmsystem.h>
55 #endif
56
57 void
58 mono_runtime_install_handlers (void)
59 {
60 #ifndef MONO_CROSS_COMPILE
61         if (!mono_aot_only) {
62                 win32_seh_init();
63                 win32_seh_set_handler(SIGFPE, mono_sigfpe_signal_handler);
64                 win32_seh_set_handler(SIGILL, mono_sigill_signal_handler);
65                 win32_seh_set_handler(SIGSEGV, mono_sigsegv_signal_handler);
66                 if (mini_get_debug_options ()->handle_sigint)
67                         win32_seh_set_handler(SIGINT, mono_sigint_signal_handler);
68         }
69 #endif
70 }
71
72 void
73 mono_runtime_cleanup_handlers (void)
74 {
75 #ifndef MONO_CROSS_COMPILE
76         if (!mono_aot_only) {
77                 win32_seh_cleanup();
78         }
79 #endif
80 }
81
82
83 /* mono_chain_signal:
84  *
85  *   Call the original signal handler for the signal given by the arguments, which
86  * should be the same as for a signal handler. Returns TRUE if the original handler
87  * was called, false otherwise.
88  */
89 gboolean
90 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
91 {
92         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
93         jit_tls->mono_win_chained_exception_needs_run = TRUE;
94         return TRUE;
95 }
96
97 static HANDLE win32_main_thread;
98 static MMRESULT win32_timer;
99
100 static void CALLBACK
101 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
102 {
103         CONTEXT context;
104
105         context.ContextFlags = CONTEXT_CONTROL;
106         if (GetThreadContext (win32_main_thread, &context)) {
107 #ifdef _WIN64
108                 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
109 #else
110                 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
111 #endif
112         }
113 }
114
115 void
116 mono_runtime_setup_stat_profiler (void)
117 {
118         static int inited = 0;
119         TIMECAPS timecaps;
120
121         if (inited)
122                 return;
123
124         inited = 1;
125         if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
126                 return;
127
128         if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
129                 return;
130
131         if (timeBeginPeriod (1) != TIMERR_NOERROR)
132                 return;
133
134         if ((win32_timer = timeSetEvent (1, 0, (LPTIMECALLBACK)win32_time_proc, (DWORD_PTR)NULL, TIME_PERIODIC)) == 0) {
135                 timeEndPeriod (1);
136                 return;
137         }
138 }
139
140 void
141 mono_runtime_shutdown_stat_profiler (void)
142 {
143 }
144
145 gboolean
146 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
147 {
148         DWORD id = mono_thread_info_get_tid (info);
149         HANDLE handle;
150         CONTEXT context;
151         DWORD result;
152         MonoContext *ctx;
153         MonoJitTlsData *jit_tls;
154         void *domain;
155         MonoLMF *lmf = NULL;
156         gpointer *addr;
157
158         tctx->valid = FALSE;
159         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
160         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
161         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
162
163         g_assert (id != GetCurrentThreadId ());
164
165         handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
166         g_assert (handle);
167
168         context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
169
170         if (!GetThreadContext (handle, &context)) {
171                 CloseHandle (handle);
172                 return FALSE;
173         }
174
175         g_assert (context.ContextFlags & CONTEXT_INTEGER);
176         g_assert (context.ContextFlags & CONTEXT_CONTROL);
177
178         ctx = &tctx->ctx;
179
180         memset (ctx, 0, sizeof (MonoContext));
181         mono_sigctx_to_monoctx (&context, ctx);
182
183         /* mono_set_jit_tls () sets this */
184         jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
185         /* SET_APPDOMAIN () sets this */
186         domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
187
188         /*Thread already started to cleanup, can no longer capture unwind state*/
189         if (!jit_tls || !domain)
190                 return FALSE;
191
192         /*
193          * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
194          * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
195          * can be accessed through MonoThreadInfo.
196          */
197         /* mono_set_lmf_addr () sets this */
198         addr = mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
199         if (addr)
200                 lmf = *addr;
201
202         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
203         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
204         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
205         tctx->valid = TRUE;
206
207         return TRUE;
208 }
209