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/metadata/mono-debug.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/arch/x86/x86-codegen.h>
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
28 static guint8* nullified_class_init_trampoline;
31 * mono_arch_get_unbox_trampoline:
33 * @addr: pointer to native code for @m
35 * when value type methods are called through the vtable we need to unbox the
36 * this argument. This method returns a pointer to a trampoline which does
37 * unboxing before calling the method
40 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
44 MonoDomain *domain = mono_domain_get ();
46 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
49 mono_domain_lock (domain);
50 start = code = mono_code_manager_reserve (domain->code_mp, 16);
51 mono_domain_unlock (domain);
53 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
54 x86_jump_code (code, addr);
55 g_assert ((code - start) < 16);
61 mono_arch_patch_callsite (guint8 *code, guint8 *addr)
63 /* go to the start of the call instruction
65 * address_byte = (m << 6) | (o << 3) | reg
66 * call opcode: 0xff address_byte displacement
71 if ((code [1] == 0xe8)) {
72 if (!mono_running_on_valgrind ()) {
73 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
75 #ifdef HAVE_VALGRIND_MEMCHECK_H
76 /* Tell valgrind to recompile the patched code */
77 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
80 } else if (code [1] == 0xe9) {
81 /* A PLT entry: jmp <DISP> */
82 if (!mono_running_on_valgrind ())
83 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
85 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
86 code [4], code [5], code [6]);
87 g_assert_not_reached ();
92 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
94 /* A PLT entry: jmp <DISP> */
95 g_assert (code [0] == 0xe9);
97 if (!mono_running_on_valgrind ())
98 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
102 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
105 if (code [0] == 0xe8) {
106 if (!mono_running_on_valgrind ()) {
109 * Thread safe code patching using the algorithm from the paper
110 * 'Practicing JUDO: Java Under Dynamic Optimizations'
113 * First atomically change the the first 2 bytes of the call to a
117 InterlockedExchange ((gint32*)code, ops);
119 /* Then change the other bytes to a nop */
124 /* Then atomically change the first 4 bytes to a nop as well */
126 InterlockedExchange ((gint32*)code, ops);
127 #ifdef HAVE_VALGRIND_MEMCHECK_H
128 /* FIXME: the calltree skin trips on the self modifying code above */
130 /* Tell valgrind to recompile the patched code */
131 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
134 } else if (code [0] == 0x90 || code [0] == 0xeb) {
135 /* Already changed by another thread */
137 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
138 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
139 gpointer *vtable_slot;
141 vtable_slot = mono_arch_get_vcall_slot_addr (code + 5, (gpointer*)regs);
142 g_assert (vtable_slot);
144 *vtable_slot = nullified_class_init_trampoline;
146 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
147 code [4], code [5], code [6]);
148 g_assert_not_reached ();
153 mono_arch_nullify_plt_entry (guint8 *code)
155 if (!mono_running_on_valgrind ()) {
159 InterlockedExchange ((gint32*)code, ops);
161 /* Then change the other bytes to a nop */
166 /* Change the first byte to a nop */
168 InterlockedExchange ((gint32*)code, ops);
173 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
178 code = buf = mono_global_codeman_reserve (256);
180 /* Note that there is a single argument to the trampoline
181 * and it is stored at: esp + pushed_args * sizeof (gpointer)
182 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
184 /* Put all registers into an array on the stack */
185 x86_push_reg (buf, X86_EDI);
186 x86_push_reg (buf, X86_ESI);
187 x86_push_reg (buf, X86_EBP);
188 x86_push_reg (buf, X86_ESP);
189 x86_push_reg (buf, X86_EBX);
190 x86_push_reg (buf, X86_EDX);
191 x86_push_reg (buf, X86_ECX);
192 x86_push_reg (buf, X86_EAX);
198 /* save the IP (caller ip) */
199 if (tramp_type == MONO_TRAMPOLINE_JUMP)
200 x86_push_imm (buf, 0);
202 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
206 x86_push_reg (buf, X86_EBP);
207 x86_push_reg (buf, X86_ESI);
208 x86_push_reg (buf, X86_EDI);
209 x86_push_reg (buf, X86_EBX);
213 /* save method info */
214 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
218 /* the stack is correctly aligned to 16 bytes because pushed_args is 14
219 * and there is the extra trampoline arg + the return ip pushed by call
220 * FIXME: Note that if an exception happens while some args are pushed
221 * on the stack, the stack will be misaligned.
224 g_assert (pushed_args == 14);
226 /* get the address of lmf for the current thread */
227 x86_call_code (buf, mono_get_lmf_addr);
229 x86_push_reg (buf, X86_EAX);
230 /* push *lfm (previous_lmf) */
231 x86_push_membase (buf, X86_EAX, 0);
233 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
238 /* starting the call sequence */
240 /* changing esp to keep the stack aligned */
241 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 8);
245 /* FIXME: Push the trampoline address */
246 x86_push_imm (buf, 0);
250 /* push the method info */
251 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
255 /* push the return address onto the stack */
256 if (tramp_type == MONO_TRAMPOLINE_JUMP)
257 x86_push_imm (buf, 0);
259 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
261 /* push the address of the register array */
262 x86_lea_membase (buf, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
263 x86_push_reg (buf, X86_EAX);
268 /* check the stack is aligned after the ret ip is pushed */
269 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
270 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
271 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
272 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
273 x86_breakpoint (buf);*/
276 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
277 x86_call_code (buf, mono_class_init_trampoline);
278 else if (tramp_type == MONO_TRAMPOLINE_AOT)
279 x86_call_code (buf, mono_aot_trampoline);
280 else if (tramp_type == MONO_TRAMPOLINE_AOT_PLT)
281 x86_call_code (buf, mono_aot_plt_trampoline);
282 else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
283 x86_call_code (buf, mono_delegate_trampoline);
285 x86_call_code (buf, mono_magic_trampoline);
288 /* account for the alignment above */
289 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 6*4);
291 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
294 /* restore LMF start */
295 /* ebx = previous_lmf */
296 x86_pop_reg (buf, X86_EBX);
298 x86_pop_reg (buf, X86_EDI);
299 /* *(lmf) = previous_lmf */
300 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
301 /* discard method info */
302 x86_pop_reg (buf, X86_ESI);
303 /* restore caller saved regs */
304 x86_pop_reg (buf, X86_EBX);
305 x86_pop_reg (buf, X86_EDI);
306 x86_pop_reg (buf, X86_ESI);
307 x86_pop_reg (buf, X86_EBP);
309 /* discard save IP */
310 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
311 /* restore LMF end */
313 /* Restore caller saved registers */
314 x86_mov_reg_membase (buf, X86_ECX, X86_ESP, 1 * 4, 4);
315 x86_mov_reg_membase (buf, X86_EDX, X86_ESP, 2 * 4, 4);
317 /* Pop saved reg array + method ptr */
318 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
320 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
323 /* call the compiled method */
324 x86_jump_reg (buf, X86_EAX);
326 g_assert ((buf - code) <= 256);
328 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
329 /* Initialize the nullified class init trampoline used in the AOT case */
330 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
337 #define TRAMPOLINE_SIZE 10
340 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
342 guint8 *code, *buf, *tramp;
344 tramp = mono_get_trampoline_code (tramp_type);
346 mono_domain_lock (domain);
347 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
348 mono_domain_unlock (domain);
350 x86_push_imm (buf, arg1);
351 x86_jump_code (buf, tramp);
352 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
354 mono_arch_flush_icache (code, buf - code);
357 *code_len = buf - code;
363 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
365 /* FIXME: This is not thread safe */
366 guint8 *code = ji->code_start;
368 x86_push_imm (code, func_arg);
369 x86_call_code (code, (guint8*)func);
373 * This method is only called when running in the Mono Debugger.
376 mono_debugger_create_notification_function (void)
380 code = buf = mono_global_codeman_reserve (2);
381 x86_breakpoint (buf);