1 /**
2  * \file
3  *
4  *   Runtime declarations for the JIT.
5  *
6  * Copyright 2002-2003 Ximian Inc
7  * Copyright 2003-2011 Novell Inc
8  * Copyright 2011 Xamarin Inc
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11 
12 #ifndef __MONO_MINI_RUNTIME_H__
13 #define __MONO_MINI_RUNTIME_H__
14 
15 #include "mini.h"
16 
17 /* Per-domain information maintained by the JIT */
18 typedef struct
19 {
20 	/* Maps MonoMethod's to a GSList of GOT slot addresses pointing to its code */
21 	GHashTable *jump_target_got_slot_hash;
22 	GHashTable *jump_target_hash;
23 	/* Maps methods/klasses to the address of the given type of trampoline */
24 	GHashTable *class_init_trampoline_hash;
25 	GHashTable *jump_trampoline_hash;
26 	GHashTable *jit_trampoline_hash;
27 	GHashTable *delegate_trampoline_hash;
28 	/* Maps ClassMethodPair -> MonoDelegateTrampInfo */
29 	GHashTable *static_rgctx_trampoline_hash;
30 	GHashTable *llvm_vcall_trampoline_hash;
31 	/* maps MonoMethod -> MonoJitDynamicMethodInfo */
32 	GHashTable *dynamic_code_hash;
33 	GHashTable *method_code_hash;
34 	/* Maps methods to a RuntimeInvokeInfo structure, protected by the associated MonoDomain lock */
35 	MonoConcurrentHashTable *runtime_invoke_hash;
36 	/* Maps MonoMethod to a GPtrArray containing sequence point locations */
37 	/* Protected by the domain lock */
38 	GHashTable *seq_points;
39 	/* Debugger agent data */
40 	gpointer agent_info;
41 	/* Maps MonoMethod to an arch-specific structure */
42 	GHashTable *arch_seq_points;
43 	/* Maps a GSharedVtTrampInfo structure to a trampoline address */
44 	GHashTable *gsharedvt_arg_tramp_hash;
45 	/* memcpy/bzero methods specialized for small constant sizes */
46 	gpointer *memcpy_addr [17];
47 	gpointer *bzero_addr [17];
48 	gpointer llvm_module;
49 	/* Maps MonoMethod -> GSlist of addresses */
50 	GHashTable *llvm_jit_callees;
51 	/* Maps MonoMethod -> RuntimeMethod */
52 	MonoInternalHashTable interp_code_hash;
53 } MonoJitDomainInfo;
54 
55 #define domain_jit_info(domain) ((MonoJitDomainInfo*)((domain)->runtime_info))
56 
57 /*
58  * Stores state need to resume exception handling when using LLVM
59  */
60 typedef struct {
61 	MonoJitInfo *ji;
62 	int clause_index;
63 	MonoContext ctx, new_ctx;
64 	/* FIXME: GC */
65 	gpointer        ex_obj;
66 	MonoLMF *lmf;
67 	int first_filter_idx, filter_idx;
68 } ResumeState;
69 
70 struct MonoJitTlsData {
71 	gpointer          end_of_stack;
72 	guint32           stack_size;
73 	MonoLMF          *lmf;
74 	MonoLMF          *first_lmf;
75 	gpointer         restore_stack_prot;
76 	guint32          handling_stack_ovf;
77 	gpointer         signal_stack;
78 	guint32          signal_stack_size;
79 	gpointer         stack_ovf_guard_base;
80 	guint32          stack_ovf_guard_size;
81 	guint            stack_ovf_valloced : 1;
82 	void            (*abort_func) (MonoObject *object);
83 	/* Used to implement --debug=casts */
84 	MonoClass       *class_cast_from, *class_cast_to;
85 
86 	/* Stores state needed by handler block with a guard */
87 	MonoContext     ex_ctx;
88 	ResumeState resume_state;
89 
90 	/* handler block been guarded. It's safe to store this even for dynamic methods since there
91 	is an activation on stack making sure it will remain alive.*/
92 	MonoJitExceptionInfo *handler_block;
93 
94 	/* context to be used by the guard trampoline when resuming interruption.*/
95 	MonoContext handler_block_context;
96 	/*
97 	 * Stores the state at the exception throw site to be used by mono_stack_walk ()
98 	 * when it is called from profiler functions during exception handling.
99 	 */
100 	MonoContext orig_ex_ctx;
101 	gboolean orig_ex_ctx_set;
102 
103 	/*
104 	 * Stores if we need to run a chained exception in Windows.
105 	 */
106 	gboolean mono_win_chained_exception_needs_run;
107 
108 	/*
109 	 * The current exception in flight
110 	 */
111 	guint32 thrown_exc;
112 	/*
113 	 * If the current exception is not a subclass of Exception,
114 	 * the original exception.
115 	 */
116 	guint32 thrown_non_exc;
117 
118 	/*
119 	 * The calling assembly in llvmonly mode.
120 	 */
121 	MonoImage *calling_image;
122 
123 	/*
124 	 * The stack frame "high water mark" for ThreadAbortExceptions.
125 	 * We will rethrow the exception upon exiting a catch clause that's
126 	 * in a function stack frame above the water mark(isn't being called by
127 	 * the catch block that caught the ThreadAbortException).
128 	 */
129 	gpointer abort_exc_stack_threshold;
130 
131 	/*
132 	 * List of methods being JIT'd in the current thread.
133 	 */
134 	int active_jit_methods;
135 
136 	gpointer interp_context;
137 
138 #if defined(TARGET_WIN32)
139 	MonoContext stack_restore_ctx;
140 #endif
141 };
142 
143 /*
144  * This structure is an extension of MonoLMF and contains extra information.
145  */
146 typedef struct {
147 	struct MonoLMF lmf;
148 	gboolean debugger_invoke;
149 	gboolean interp_exit;
150 	MonoContext ctx; /* if debugger_invoke is TRUE */
151 	/* If interp_exit is TRUE */
152 	gpointer interp_exit_data;
153 } MonoLMFExt;
154 
155 /* main function */
156 MONO_API int         mono_main                      (int argc, char* argv[]);
157 MONO_API void        mono_set_defaults              (int verbose_level, guint32 opts);
158 MONO_API void        mono_parse_env_options         (int *ref_argc, char **ref_argv []);
159 MONO_API char       *mono_parse_options_from        (const char *options, int *ref_argc, char **ref_argv []);
160 
161 /* actual definition in interp.h */
162 typedef struct _MonoInterpCallbacks MonoInterpCallbacks;
163 
164 void                   mono_interp_stub_init         (void);
165 void                   mini_install_interp_callbacks (MonoInterpCallbacks *cbs);
166 MonoInterpCallbacks*   mini_get_interp_callbacks     (void);
167 
168 MonoDomain* mini_init                      (const char *filename, const char *runtime_version);
169 void        mini_cleanup                   (MonoDomain *domain);
170 MONO_API MonoDebugOptions *mini_get_debug_options   (void);
171 MONO_API gboolean    mini_parse_debug_option (const char *option);
172 
173 void      mini_jit_init                    (void);
174 void      mini_jit_cleanup                 (void);
175 void      mono_disable_optimizations       (guint32 opts);
176 void      mono_set_optimizations           (guint32 opts);
177 void      mono_precompile_assemblies        (void);
178 MONO_API int       mono_parse_default_optimizations  (const char* p);
179 gboolean          mono_running_on_valgrind (void);
180 
181 MonoLMF * mono_get_lmf                      (void);
182 MonoLMF** mono_get_lmf_addr                 (void);
183 void      mono_set_lmf                      (MonoLMF *lmf);
184 void      mono_push_lmf                     (MonoLMFExt *ext);
185 void      mono_pop_lmf                      (MonoLMF *lmf);
186 MonoJitTlsData* mono_get_jit_tls            (void);
187 MONO_API MonoDomain* mono_jit_thread_attach (MonoDomain *domain);
188 MONO_API void      mono_jit_set_domain      (MonoDomain *domain);
189 
190 #endif /* __MONO_MINI_RUNTIME_H__ */
191 
192