2 * tramp-x86.c: JIT trampoline code for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/metadata-internals.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/arch/x86/x86-codegen.h>
18 #include <mono/metadata/mono-debug-debugger.h>
20 #ifdef HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
27 static guint8* nullified_class_init_trampoline;
30 * mono_arch_get_unbox_trampoline:
32 * @addr: pointer to native code for @m
34 * when value type methods are called through the vtable we need to unbox the
35 * this argument. This method returns a pointer to a trampoline which does
36 * unboxing before calling the method
39 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
43 MonoDomain *domain = mono_domain_get ();
45 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
48 mono_domain_lock (domain);
49 start = code = mono_code_manager_reserve (domain->code_mp, 16);
50 mono_domain_unlock (domain);
52 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
53 x86_jump_code (code, addr);
54 g_assert ((code - start) < 16);
60 mono_arch_patch_callsite (guint8 *code, guint8 *addr)
62 /* go to the start of the call instruction
64 * address_byte = (m << 6) | (o << 3) | reg
65 * call opcode: 0xff address_byte displacement
70 if ((code [1] == 0xe8)) {
71 if (!mono_running_on_valgrind ()) {
72 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
74 #ifdef HAVE_VALGRIND_MEMCHECK_H
75 /* Tell valgrind to recompile the patched code */
76 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
80 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
81 code [4], code [5], code [6]);
82 g_assert_not_reached ();
87 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
90 if (code [0] == 0xe8) {
91 if (!mono_running_on_valgrind ()) {
94 * Thread safe code patching using the algorithm from the paper
95 * 'Practicing JUDO: Java Under Dynamic Optimizations'
98 * First atomically change the the first 2 bytes of the call to a
102 InterlockedExchange ((gint32*)code, ops);
104 /* Then change the other bytes to a nop */
109 /* Then atomically change the first 4 bytes to a nop as well */
111 InterlockedExchange ((gint32*)code, ops);
112 #ifdef HAVE_VALGRIND_MEMCHECK_H
113 /* FIXME: the calltree skin trips on the self modifying code above */
115 /* Tell valgrind to recompile the patched code */
116 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
119 } else if (code [0] == 0x90 || code [0] == 0xeb) {
120 /* Already changed by another thread */
122 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
123 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
124 gpointer *vtable_slot;
126 vtable_slot = mono_arch_get_vcall_slot_addr (code + 5, (gpointer*)regs);
127 g_assert (vtable_slot);
129 *vtable_slot = nullified_class_init_trampoline;
131 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
132 code [4], code [5], code [6]);
133 g_assert_not_reached ();
138 mono_arch_patch_delegate_trampoline (guint8 *code, guint8 *tramp, gssize *regs, guint8 *addr)
143 if ((code [-3] == 0xff) && (x86_modrm_reg (code [-2]) == 0x2) && (x86_modrm_mod (code [-2]) == 0x1)) {
144 /* call *[reg+disp8] */
145 reg = x86_modrm_rm (code [-2]);
146 disp = *(guint8*)(code - 1);
147 //printf ("B: [%%r%d+0x%x]\n", reg, disp);
152 for (i = -16; i < 0; ++i)
153 printf ("%d ", code [i]);
155 g_assert_not_reached ();
158 *(gpointer*)(((guint32)(regs [reg])) + disp) = addr;
162 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
166 code = buf = mono_global_codeman_reserve (256);
168 /* Put all registers into an array on the stack */
169 x86_push_reg (buf, X86_EDI);
170 x86_push_reg (buf, X86_ESI);
171 x86_push_reg (buf, X86_EBP);
172 x86_push_reg (buf, X86_ESP);
173 x86_push_reg (buf, X86_EBX);
174 x86_push_reg (buf, X86_EDX);
175 x86_push_reg (buf, X86_ECX);
176 x86_push_reg (buf, X86_EAX);
180 /* save the IP (caller ip) */
181 if (tramp_type == MONO_TRAMPOLINE_JUMP)
182 x86_push_imm (buf, 0);
184 x86_push_membase (buf, X86_ESP, 8 * 4 + 4);
186 x86_push_reg (buf, X86_EBP);
187 x86_push_reg (buf, X86_ESI);
188 x86_push_reg (buf, X86_EDI);
189 x86_push_reg (buf, X86_EBX);
191 /* save method info */
192 x86_push_membase (buf, X86_ESP, 13 * 4);
193 /* get the address of lmf for the current thread */
194 x86_call_code (buf, mono_get_lmf_addr);
196 x86_push_reg (buf, X86_EAX);
197 /* push *lfm (previous_lmf) */
198 x86_push_membase (buf, X86_EAX, 0);
200 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
203 /* FIXME: Push the trampoline address */
204 x86_push_imm (buf, 0);
206 /* push the method info */
207 x86_push_membase (buf, X86_ESP, 17 * 4);
208 /* push the return address onto the stack */
209 if (tramp_type == MONO_TRAMPOLINE_JUMP)
210 x86_push_imm (buf, 0);
212 x86_push_membase (buf, X86_ESP, 18 * 4 + 4);
213 /* push the address of the register array */
214 x86_lea_membase (buf, X86_EAX, X86_ESP, 11 * 4);
215 x86_push_reg (buf, X86_EAX);
217 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
218 x86_call_code (buf, mono_class_init_trampoline);
219 else if (tramp_type == MONO_TRAMPOLINE_AOT)
220 x86_call_code (buf, mono_aot_trampoline);
221 else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
222 x86_call_code (buf, mono_delegate_trampoline);
224 x86_call_code (buf, mono_magic_trampoline);
225 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
227 /* restore LMF start */
228 /* ebx = previous_lmf */
229 x86_pop_reg (buf, X86_EBX);
231 x86_pop_reg (buf, X86_EDI);
232 /* *(lmf) = previous_lmf */
233 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
234 /* discard method info */
235 x86_pop_reg (buf, X86_ESI);
236 /* restore caller saved regs */
237 x86_pop_reg (buf, X86_EBX);
238 x86_pop_reg (buf, X86_EDI);
239 x86_pop_reg (buf, X86_ESI);
240 x86_pop_reg (buf, X86_EBP);
242 /* discard save IP */
243 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
244 /* restore LMF end */
246 /* Restore caller saved registers */
247 x86_mov_reg_membase (buf, X86_ECX, X86_ESP, 1 * 4, 4);
248 x86_mov_reg_membase (buf, X86_EDX, X86_ESP, 2 * 4, 4);
250 /* Pop saved reg array + method ptr */
251 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
253 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
256 /* call the compiled method */
257 x86_jump_reg (buf, X86_EAX);
259 g_assert ((buf - code) <= 256);
261 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
262 /* Initialize the nullified class init trampoline used in the AOT case */
263 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
270 #define TRAMPOLINE_SIZE 10
273 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
275 guint8 *code, *buf, *tramp;
277 tramp = mono_get_trampoline_code (tramp_type);
279 mono_domain_lock (domain);
280 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
281 mono_domain_unlock (domain);
283 x86_push_imm (buf, arg1);
284 x86_jump_code (buf, tramp);
285 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
287 mono_arch_flush_icache (code, buf - code);
289 mono_jit_stats.method_trampolines++;
292 *code_len = buf - code;
298 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
300 /* FIXME: This is not thread safe */
301 guint8 *code = ji->code_start;
303 x86_push_imm (code, func_arg);
304 x86_call_code (code, (guint8*)func);
308 * This method is only called when running in the Mono Debugger.
311 mono_debugger_create_notification_function (gpointer *notification_address)
315 ptr = buf = mono_global_codeman_reserve (16);
317 x86_breakpoint (buf);
318 if (notification_address)
319 *notification_address = buf;