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 *orig_code, guint8 *addr)
65 gboolean can_write = mono_breakpoint_clean_code (orig_code - 8, buf, sizeof (buf));
68 if (mono_running_on_valgrind ())
71 /* go to the start of the call instruction
73 * address_byte = (m << 6) | (o << 3) | reg
74 * call opcode: 0xff address_byte displacement
80 if ((code [1] == 0xe8)) {
82 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
84 #ifdef HAVE_VALGRIND_MEMCHECK_H
85 /* Tell valgrind to recompile the patched code */
86 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
89 } else if (code [1] == 0xe9) {
90 /* A PLT entry: jmp <DISP> */
92 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
94 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
95 code [4], code [5], code [6]);
96 g_assert_not_reached ();
101 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
103 /* A PLT entry: jmp <DISP> */
104 g_assert (code [0] == 0xe9);
106 if (!mono_running_on_valgrind ())
107 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
111 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
114 if (code [0] == 0xe8) {
115 if (!mono_running_on_valgrind ()) {
118 * Thread safe code patching using the algorithm from the paper
119 * 'Practicing JUDO: Java Under Dynamic Optimizations'
122 * First atomically change the the first 2 bytes of the call to a
126 InterlockedExchange ((gint32*)code, ops);
128 /* Then change the other bytes to a nop */
133 /* Then atomically change the first 4 bytes to a nop as well */
135 InterlockedExchange ((gint32*)code, ops);
136 #ifdef HAVE_VALGRIND_MEMCHECK_H
137 /* FIXME: the calltree skin trips on the self modifying code above */
139 /* Tell valgrind to recompile the patched code */
140 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
143 } else if (code [0] == 0x90 || code [0] == 0xeb) {
144 /* Already changed by another thread */
146 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
147 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
148 gpointer *vtable_slot;
150 vtable_slot = mono_arch_get_vcall_slot_addr (code + 5, (gpointer*)regs);
151 g_assert (vtable_slot);
153 *vtable_slot = nullified_class_init_trampoline;
155 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
156 code [4], code [5], code [6]);
157 g_assert_not_reached ();
162 mono_arch_nullify_plt_entry (guint8 *code)
164 if (!mono_running_on_valgrind ()) {
168 InterlockedExchange ((gint32*)code, ops);
170 /* Then change the other bytes to a nop */
175 /* Change the first byte to a nop */
177 InterlockedExchange ((gint32*)code, ops);
182 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
184 guint8 *buf, *code, *tramp;
185 int pushed_args, pushed_args_caller_saved;
187 code = buf = mono_global_codeman_reserve (256);
189 /* Note that there is a single argument to the trampoline
190 * and it is stored at: esp + pushed_args * sizeof (gpointer)
191 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
194 /* If this is a generic class init the argument is not on the
195 * stack yet but in MONO_ARCH_VTABLE_REG. We first check
196 * whether the vtable is already initialized in which case we
197 * just return. Otherwise we push it and continue.
199 if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
200 static int byte_offset = -1;
201 static guint8 bitmask;
206 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
208 x86_test_membase_imm (buf, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
210 x86_branch8 (buf, X86_CC_Z, -1, 1);
214 x86_patch (jump, buf);
215 x86_push_reg (buf, MONO_ARCH_VTABLE_REG);
218 /* Put all registers into an array on the stack
219 * If this code is changed, make sure to update the offset value in
220 * mono_arch_find_this_argument () in mini-x86.c.
222 x86_push_reg (buf, X86_EDI);
223 x86_push_reg (buf, X86_ESI);
224 x86_push_reg (buf, X86_EBP);
225 x86_push_reg (buf, X86_ESP);
226 x86_push_reg (buf, X86_EBX);
227 x86_push_reg (buf, X86_EDX);
228 x86_push_reg (buf, X86_ECX);
229 x86_push_reg (buf, X86_EAX);
231 pushed_args_caller_saved = pushed_args = 8;
233 /* Align stack on apple */
234 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 4);
240 /* save the IP (caller ip) */
241 if (tramp_type == MONO_TRAMPOLINE_JUMP)
242 x86_push_imm (buf, 0);
244 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
248 x86_push_reg (buf, X86_EBP);
249 x86_push_reg (buf, X86_ESI);
250 x86_push_reg (buf, X86_EDI);
251 x86_push_reg (buf, X86_EBX);
256 x86_push_reg (buf, X86_ESP);
257 /* Adjust ESP so it points to the previous frame */
258 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
262 /* save method info */
263 if ((tramp_type == MONO_TRAMPOLINE_GENERIC) || (tramp_type == MONO_TRAMPOLINE_JUMP))
264 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
266 x86_push_imm (buf, 0);
270 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
271 * 16 and there is the extra trampoline arg + the return ip pushed by call
272 * FIXME: Note that if an exception happens while some args are pushed
273 * on the stack, the stack will be misaligned.
275 g_assert (pushed_args == 16);
277 /* get the address of lmf for the current thread */
278 x86_call_code (buf, mono_get_lmf_addr);
280 x86_push_reg (buf, X86_EAX);
281 /* push *lfm (previous_lmf) */
282 x86_push_membase (buf, X86_EAX, 0);
283 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
284 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, 1);
286 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
291 /* starting the call sequence */
293 /* FIXME: Push the trampoline address */
294 x86_push_imm (buf, 0);
298 /* push the method info */
299 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
303 /* push the return address onto the stack */
304 if (tramp_type == MONO_TRAMPOLINE_JUMP)
305 x86_push_imm (buf, 0);
307 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
309 /* push the address of the register array */
310 x86_lea_membase (buf, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
311 x86_push_reg (buf, X86_EAX);
316 /* check the stack is aligned after the ret ip is pushed */
317 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
318 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
319 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
320 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
321 x86_breakpoint (buf);*/
324 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
325 x86_call_code (buf, tramp);
327 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
331 /* Check for thread interruption */
332 /* This is not perf critical code so no need to check the interrupt flag */
333 x86_push_reg (buf, X86_EAX);
334 x86_call_code (buf, (guint8*)mono_thread_interruption_checkpoint);
335 x86_pop_reg (buf, X86_EAX);
339 /* ebx = previous_lmf */
340 x86_pop_reg (buf, X86_EBX);
342 x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
345 x86_pop_reg (buf, X86_EDI);
348 /* *(lmf) = previous_lmf */
349 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
351 /* discard method info */
352 x86_pop_reg (buf, X86_ESI);
356 x86_pop_reg (buf, X86_ESI);
359 /* restore caller saved regs */
360 x86_pop_reg (buf, X86_EBX);
361 x86_pop_reg (buf, X86_EDI);
362 x86_pop_reg (buf, X86_ESI);
363 x86_pop_reg (buf, X86_EBP);
367 /* discard save IP */
368 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
371 /* restore LMF end */
373 /* Restore caller saved registers */
374 x86_mov_reg_membase (buf, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
375 x86_mov_reg_membase (buf, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
377 /* Pop saved reg array + stack align + method ptr */
378 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
382 /* We've popped one more stack item than we've pushed (the
383 method ptr argument), so we must end up at -1. */
384 g_assert (pushed_args == -1);
386 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT ||
387 tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT ||
388 tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
391 /* call the compiled method */
392 x86_jump_reg (buf, X86_EAX);
394 g_assert ((buf - code) <= 256);
396 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
397 /* Initialize the nullified class init trampoline used in the AOT case */
398 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
405 #define TRAMPOLINE_SIZE 10
408 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
410 guint8 *code, *buf, *tramp;
412 tramp = mono_get_trampoline_code (tramp_type);
414 mono_domain_lock (domain);
415 code = buf = mono_code_manager_reserve_align (domain->code_mp, TRAMPOLINE_SIZE, 4);
416 mono_domain_unlock (domain);
418 x86_push_imm (buf, arg1);
419 x86_jump_code (buf, tramp);
420 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
422 mono_arch_flush_icache (code, buf - code);
425 *code_len = buf - code;
431 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
433 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
434 gboolean indirect = MONO_RGCTX_OFFSET_IS_INDIRECT (encoded_offset);
435 int offset = indirect ? MONO_RGCTX_OFFSET_INDIRECT_OFFSET (encoded_offset) :
436 MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
437 guint8 *code, *buf, *jump;
442 g_assert (MONO_RGCTX_OFFSET_INDIRECT_SLOT (encoded_offset) == 0);
444 code = buf = mono_global_codeman_reserve (32);
447 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
449 /* if indirect, load extra_other_infos ptr */
450 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoRuntimeGenericContext, extra_other_infos), 4);
453 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, offset, 4);
458 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
460 /* if yes, jump to actual trampoline */
461 x86_branch8 (buf, X86_CC_Z, -1, 1);
463 /* if no, just return */
467 * our stack looks like this (tos on top):
473 * the trampoline code expects it to look like this:
479 * whereas our caller expects to still have one argument on
480 * the stack when we return, so we transform the stack into
488 * which actually only requires us to push the rgctx ptr, and
489 * the "old" rgctx ptr becomes the dummy.
492 x86_patch (jump, buf);
493 x86_push_membase (buf, X86_ESP, 4);
495 x86_mov_reg_imm (buf, X86_EAX, encoded_offset);
496 x86_jump_code (buf, tramp);
498 mono_arch_flush_icache (code, buf - code);
500 g_assert (buf - code <= 32);
506 mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
508 return (guint32)(regs [X86_EAX]);
512 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
514 /* FIXME: This is not thread safe */
515 guint8 *code = ji->code_start;
517 x86_push_imm (code, func_arg);
518 x86_call_code (code, (guint8*)func);
522 * This method is only called when running in the Mono Debugger.
525 mono_debugger_create_notification_function (void)
529 code = buf = mono_global_codeman_reserve (2);
530 x86_breakpoint (buf);