2 * tramp-x86.c: JIT trampoline code for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/abi-details.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/metadata-internals.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/mono-debug-debugger.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/arch/x86/x86-codegen.h>
25 #include <mono/utils/memcheck.h>
30 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
33 * mono_arch_get_unbox_trampoline:
35 * @addr: pointer to native code for @m
37 * when value type methods are called through the vtable we need to unbox the
38 * this argument. This method returns a pointer to a trampoline which does
39 * unboxing before calling the method
42 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
45 int this_pos = 4, size = NACL_SIZE(16, 32);
46 MonoDomain *domain = mono_domain_get ();
48 start = code = mono_domain_code_reserve (domain, size);
50 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
51 x86_jump_code (code, addr);
52 g_assert ((code - start) < size);
54 nacl_domain_code_validate (domain, &start, size, &code);
55 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
61 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
66 MonoDomain *domain = mono_domain_get ();
68 buf_len = NACL_SIZE (10, 32);
70 start = code = mono_domain_code_reserve (domain, buf_len);
72 x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
73 x86_jump_code (code, addr);
74 g_assert ((code - start) <= buf_len);
76 nacl_domain_code_validate (domain, &start, buf_len, &code);
77 mono_arch_flush_icache (start, code - start);
78 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
84 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
92 start = code = mono_domain_code_reserve (domain, buf_len);
94 this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
97 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
99 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
100 /* Load vtable address */
101 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
102 x86_jump_membase (code, X86_EAX, vt_offset);
104 g_assert ((code - start) < buf_len);
106 nacl_domain_code_validate (domain, &start, buf_len, &code);
108 mono_arch_flush_icache (start, code - start);
109 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
115 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
117 #if defined(__default_codegen__)
120 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
124 /* go to the start of the call instruction
126 * address_byte = (m << 6) | (o << 3) | reg
127 * call opcode: 0xff address_byte displacement
133 if (code [1] == 0xe8) {
135 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
137 /* Tell valgrind to recompile the patched code */
138 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
140 } else if (code [1] == 0xe9) {
141 /* A PLT entry: jmp <DISP> */
143 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
145 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
146 code [4], code [5], code [6]);
147 g_assert_not_reached ();
149 #elif defined(__native_client__)
150 /* Target must be bundle-aligned */
151 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
153 /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
154 if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
156 gint32 offset = (gint32)addr - (gint32)orig_code;
157 guint8 buf[sizeof(gint32)];
158 *((gint32*)(buf)) = offset;
159 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
162 printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
163 g_assert_not_reached ();
169 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
173 /* Patch the jump table entry used by the plt entry */
175 #if defined(__native_client_codegen__) || defined(__native_client__)
176 /* for both compiler and runtime */
178 /* mov <DISP>(%ebx), %ecx */
179 /* and 0xffffffe0, %ecx */
181 g_assert (code [0] == 0x8b);
182 g_assert (code [1] == 0x8b);
184 offset = *(guint32*)(code + 2);
185 #elif defined(__default_codegen__)
186 /* A PLT entry: jmp *<DISP>(%ebx) */
187 g_assert (code [0] == 0xff);
188 g_assert (code [1] == 0xa3);
190 offset = *(guint32*)(code + 2);
191 #endif /* __native_client_codegen__ */
193 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
194 *(guint8**)((guint8*)got + offset) = addr;
198 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
200 const int kBufSize = NACL_SIZE (8, 16);
205 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
210 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
211 reg = code [1] & 0x07;
212 disp = *((gint32*)(code + 2));
213 #if defined(__native_client_codegen__) || defined(__native_client__)
214 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
215 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
216 disp = *((gint32*)(code - 3));
217 reg = code[-4] & 0x07;
218 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
219 reg = code[-1] & 0x07;
220 disp = (signed char)code[0];
223 g_assert_not_reached ();
227 *displacement = disp;
228 return (gpointer)regs [reg];
232 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
236 vt = get_vcall_slot (code, regs, &displacement);
239 return (gpointer*)((char*)vt + displacement);
243 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
246 guint8 *buf, *code, *tramp;
247 GSList *unwind_ops = NULL;
248 MonoJumpInfo *ji = NULL;
249 int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
251 unwind_ops = mono_arch_get_cie_program ();
253 code = buf = mono_global_codeman_reserve (256);
255 /* Note that there is a single argument to the trampoline
256 * and it is stored at: esp + pushed_args * sizeof (gpointer)
257 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
260 // FIXME: Unwind info
262 /* Compute frame offsets relative to the frame pointer %ebp */
263 arg_offset = sizeof (mgreg_t);
264 caller_ip_offset = 2 * sizeof (mgreg_t);
266 offset += sizeof (MonoLMF);
267 lmf_offset = -offset;
268 offset += X86_NREG * sizeof (mgreg_t);
269 regarray_offset = -offset;
271 offset += 4 * sizeof (mgreg_t);
272 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
275 x86_push_reg (code, X86_EBP);
276 x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
277 /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
278 x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
280 /* Save all registers */
281 for (i = X86_EAX; i <= X86_EDI; ++i) {
285 /* Save original ebp */
286 /* EAX is already saved */
287 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
289 } else if (i == X86_ESP) {
290 /* Save original esp */
291 /* EAX is already saved */
292 x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
293 /* Saved ebp + trampoline arg + return addr */
294 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
297 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
302 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
303 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
305 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
306 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
309 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
310 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
311 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
313 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
316 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
317 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
318 /* callee save registers */
319 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
320 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
321 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
322 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
323 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
324 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
325 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
326 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
329 /* get the address of lmf for the current thread */
331 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
332 x86_call_reg (code, X86_EAX);
334 x86_call_code (code, mono_get_lmf_addr);
336 /* lmf->lmf_addr = lmf_addr (%eax) */
337 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
338 /* lmf->previous_lmf = *(lmf_addr) */
339 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
340 /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
341 x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
342 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
343 /* *lmf_addr = lmf */
344 x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
345 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
347 /* Call trampoline function */
348 /* Arg 1 - registers */
349 x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
350 x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
351 /* Arg2 - calling code */
352 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
353 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
355 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
356 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
358 /* Arg3 - trampoline argument */
359 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
360 x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
361 /* Arg4 - trampoline address */
363 x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
366 /* check the stack is aligned after the ret ip is pushed */
368 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
369 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
370 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
371 x86_branch_disp (code, X86_CC_Z, 3, FALSE);
372 x86_breakpoint (code);
377 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
378 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
379 x86_call_reg (code, X86_EAX);
381 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
382 x86_call_code (code, tramp);
386 * Overwrite the trampoline argument with the address we need to jump to,
389 x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
391 /* Check for interruptions */
393 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
394 x86_call_reg (code, X86_EAX);
396 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
400 x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
401 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
402 x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
403 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
405 /* Restore registers */
406 for (i = X86_EAX; i <= X86_EDI; ++i) {
407 if (i == X86_ESP || i == X86_EBP)
409 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
411 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
417 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
418 /* Load the value returned by the trampoline */
419 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
420 /* The trampoline returns normally, pop the trampoline argument */
421 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
424 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
425 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
426 x86_pop_reg (code, X86_EAX);
427 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
428 x86_jump_reg (code, X86_EAX);
434 nacl_global_codeman_validate (&buf, 256, &code);
435 g_assert ((code - buf) <= 256);
436 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
438 tramp_name = mono_get_generic_trampoline_name (tramp_type);
439 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
445 #define TRAMPOLINE_SIZE 10
448 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
450 guint8 *code, *buf, *tramp;
452 tramp = mono_get_trampoline_code (tramp_type);
454 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
456 x86_push_imm (buf, arg1);
457 x86_jump_code (buf, tramp);
458 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
460 nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
462 mono_arch_flush_icache (code, buf - code);
463 mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
466 *code_len = buf - code;
472 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
476 guint8 **rgctx_null_jumps;
481 MonoJumpInfo *ji = NULL;
482 GSList *unwind_ops = NULL;
484 unwind_ops = mono_arch_get_cie_program ();
486 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
487 index = MONO_RGCTX_SLOT_INDEX (slot);
489 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
490 for (depth = 0; ; ++depth) {
491 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
493 if (index < size - 1)
498 #if defined(__default_codegen__)
499 tramp_size = (aot ? 64 : 36) + 6 * depth;
500 #elif defined(__native_client_codegen__)
501 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
502 6 * (depth + kNaClAlignment);
505 code = buf = mono_global_codeman_reserve (tramp_size);
507 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
509 /* load vtable/mrgctx ptr */
510 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
512 /* load rgctx ptr from vtable */
513 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
514 /* is the rgctx ptr null? */
515 x86_test_reg_reg (code, X86_EAX, X86_EAX);
516 /* if yes, jump to actual trampoline */
517 rgctx_null_jumps [0] = code;
518 x86_branch8 (code, X86_CC_Z, -1, 1);
521 for (i = 0; i < depth; ++i) {
522 /* load ptr to next array */
523 if (mrgctx && i == 0)
524 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
526 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
527 /* is the ptr null? */
528 x86_test_reg_reg (code, X86_EAX, X86_EAX);
529 /* if yes, jump to actual trampoline */
530 rgctx_null_jumps [i + 1] = code;
531 x86_branch8 (code, X86_CC_Z, -1, 1);
535 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
536 /* is the slot null? */
537 x86_test_reg_reg (code, X86_EAX, X86_EAX);
538 /* if yes, jump to actual trampoline */
539 rgctx_null_jumps [depth + 1] = code;
540 x86_branch8 (code, X86_CC_Z, -1, 1);
541 /* otherwise return */
544 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
545 x86_patch (rgctx_null_jumps [i], code);
547 g_free (rgctx_null_jumps);
549 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
552 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
553 x86_jump_reg (code, X86_EAX);
555 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
557 /* jump to the actual trampoline */
558 x86_jump_code (code, tramp);
561 nacl_global_codeman_validate (&buf, tramp_size, &code);
562 mono_arch_flush_icache (buf, code - buf);
563 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
565 g_assert (code - buf <= tramp_size);
567 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
568 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
575 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
577 * This is a general variant of the rgctx fetch trampolines. It receives a pointer to gpointer[2] in the rgctx reg. The first entry contains the slot, the second
578 * the trampoline to call if the slot is not filled.
581 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
585 MonoJumpInfo *ji = NULL;
586 GSList *unwind_ops = NULL;
590 unwind_ops = mono_arch_get_cie_program ();
594 code = buf = mono_global_codeman_reserve (tramp_size);
596 // FIXME: Currently, we always go to the slow path.
598 /* Load trampoline addr */
599 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
600 /* Load mrgctx/vtable */
601 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
603 x86_jump_reg (code, X86_EAX);
605 nacl_global_codeman_validate (&buf, tramp_size, &code);
606 mono_arch_flush_icache (buf, code - buf);
607 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
609 g_assert (code - buf <= tramp_size);
611 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
616 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
618 * The code produced by this trampoline is equivalent to this:
621 * if (obj->synchronisation) {
622 * if (obj->synchronisation->owner == 0) {
623 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
626 * if (obj->synchronisation->owner == TID) {
627 * ++obj->synchronisation->nest;
632 * return full_monitor_enter ();
636 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
639 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
640 guint8 *jump_lock_taken_true = NULL;
642 int status_offset, nest_offset;
643 MonoJumpInfo *ji = NULL;
644 GSList *unwind_ops = NULL;
646 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
647 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
648 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == X86_EDX);
653 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
654 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
655 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
656 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
657 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
659 tramp_size = NACL_SIZE (128, 192);
661 code = buf = mono_global_codeman_reserve (tramp_size);
663 x86_push_reg (code, X86_EAX);
664 if (mono_thread_get_tls_offset () != -1) {
666 x86_test_membase_imm (code, X86_EDX, 0, 1);
667 /* if *lock_taken is 1, jump to actual trampoline */
668 jump_lock_taken_true = code;
669 x86_branch8 (code, X86_CC_NZ, -1, 1);
670 x86_push_reg (code, X86_EDX);
672 /* MonoObject* obj is in EAX */
674 x86_test_reg_reg (code, X86_EAX, X86_EAX);
675 /* if yes, jump to actual trampoline */
676 jump_obj_null = code;
677 x86_branch8 (code, X86_CC_Z, -1, 1);
679 /* load obj->synchronization to ECX */
680 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
682 if (mono_gc_is_moving ()) {
683 /*if bit zero is set it's a thin hash*/
684 /*FIXME use testb encoding*/
685 x86_test_reg_imm (code, X86_ECX, 0x01);
686 jump_sync_thin_hash = code;
687 x86_branch8 (code, X86_CC_NE, -1, 1);
689 /*clear bits used by the gc*/
690 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
693 /* is synchronization null? */
694 x86_test_reg_reg (code, X86_ECX, X86_ECX);
696 /* if yes, jump to actual trampoline */
697 jump_sync_null = code;
698 x86_branch8 (code, X86_CC_Z, -1, 1);
700 /* load MonoInternalThread* into EDX */
702 /* load_aotconst () puts the result into EAX */
703 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
704 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
705 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
706 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
708 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
710 /* load TID into EDX */
711 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
713 /* is synchronization->owner free */
714 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
715 x86_test_reg_imm (code, X86_EAX, OWNER_MASK);
716 /* if not, jump to next case */
718 x86_branch8 (code, X86_CC_NZ, -1, 1);
720 /* if yes, try a compare-exchange with the TID */
721 /* Form new status */
722 x86_alu_reg_reg (code, X86_OR, X86_EDX, X86_EAX);
723 /* compare and exchange */
724 x86_prefix (code, X86_LOCK_PREFIX);
725 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
726 /* if not successful, jump to actual trampoline */
727 jump_cmpxchg_failed = code;
728 x86_branch8 (code, X86_CC_NZ, -1, 1);
729 /* if successful, pop and return */
731 x86_pop_reg (code, X86_EDX);
732 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
734 x86_pop_reg (code, X86_EAX);
737 /* next case: synchronization->owner is not null */
738 x86_patch (jump_tid, code);
739 /* is synchronization->owner == TID? */
740 x86_alu_reg_imm (code, X86_AND, X86_EAX, OWNER_MASK);
741 x86_alu_reg_reg (code, X86_CMP, X86_EAX, X86_EDX);
742 /* if not, jump to actual trampoline */
743 jump_other_owner = code;
744 x86_branch8 (code, X86_CC_NZ, -1, 1);
745 /* if yes, increment nest */
746 x86_inc_membase (code, X86_ECX, nest_offset);
748 x86_pop_reg (code, X86_EDX);
749 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
751 x86_pop_reg (code, X86_EAX);
755 /* obj is pushed, jump to the actual trampoline */
756 x86_patch (jump_obj_null, code);
757 if (jump_sync_thin_hash)
758 x86_patch (jump_sync_thin_hash, code);
759 x86_patch (jump_sync_null, code);
760 x86_patch (jump_other_owner, code);
761 x86_patch (jump_cmpxchg_failed, code);
764 x86_pop_reg (code, X86_EDX);
765 x86_patch (jump_lock_taken_true, code);
770 /* We are calling the generic trampoline directly, the argument is pushed
771 * on the stack just like a specific trampoline.
774 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter_v4");
776 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
777 x86_jump_reg (code, X86_EAX);
780 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER_V4));
782 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER));
785 mono_arch_flush_icache (buf, code - buf);
786 g_assert (code - buf <= tramp_size);
788 nacl_global_codeman_validate (&buf, tramp_size, &code);
789 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
792 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
794 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
800 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
802 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
804 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
805 guint8 *jump_next, *jump_cmpxchg_failed;
807 int status_offset, nest_offset;
808 MonoJumpInfo *ji = NULL;
809 GSList *unwind_ops = NULL;
811 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
813 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
814 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
815 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
816 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
817 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
819 tramp_size = NACL_SIZE (128, 192);
821 code = buf = mono_global_codeman_reserve (tramp_size);
823 x86_push_reg (code, X86_EAX);
824 if (mono_thread_get_tls_offset () != -1) {
825 /* MonoObject* obj is in EAX */
827 x86_test_reg_reg (code, X86_EAX, X86_EAX);
828 /* if yes, jump to actual trampoline */
829 jump_obj_null = code;
830 x86_branch8 (code, X86_CC_Z, -1, 1);
832 /* load obj->synchronization to ECX */
833 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
835 if (mono_gc_is_moving ()) {
836 /*if bit zero is set it's a thin hash*/
837 /*FIXME use testb encoding*/
838 x86_test_reg_imm (code, X86_ECX, 0x01);
839 jump_sync_thin_hash = code;
840 x86_branch8 (code, X86_CC_NE, -1, 1);
842 /*clear bits used by the gc*/
843 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
846 /* is synchronization null? */
847 x86_test_reg_reg (code, X86_ECX, X86_ECX);
848 /* if yes, jump to actual trampoline */
849 jump_sync_null = code;
850 x86_branch8 (code, X86_CC_Z, -1, 1);
852 /* next case: synchronization is not null */
853 /* load MonoInternalThread* into EDX */
855 /* load_aotconst () puts the result into EAX */
856 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
857 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
858 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
859 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
861 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
863 /* load TID into EDX */
864 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
865 /* is synchronization->owner == TID */
866 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
867 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EAX);
868 x86_test_reg_imm (code, X86_EDX, OWNER_MASK);
869 /* if no, jump to actual trampoline */
870 jump_not_owned = code;
871 x86_branch8 (code, X86_CC_NZ, -1, 1);
873 /* next case: synchronization->owner == TID */
874 /* is synchronization->nest == 1 */
875 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
876 /* if not, jump to next case */
878 x86_branch8 (code, X86_CC_NZ, -1, 1);
879 /* if yes, is synchronization->entry_count greater than zero? */
880 x86_test_reg_imm (code, X86_EAX, ENTRY_COUNT_WAITERS);
881 /* if yes, jump to actual trampoline */
882 jump_have_waiters = code;
883 x86_branch8 (code, X86_CC_NZ, -1 , 1);
884 /* if not, try to set synchronization->owner to null and return */
885 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
886 x86_alu_reg_imm (code, X86_AND, X86_EDX, ENTRY_COUNT_MASK);
887 /* compare and exchange */
888 x86_prefix (code, X86_LOCK_PREFIX);
889 /* EAX contains the previous status */
890 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
891 /* if not successful, jump to actual trampoline */
892 jump_cmpxchg_failed = code;
893 x86_branch8 (code, X86_CC_NZ, -1, 1);
895 x86_pop_reg (code, X86_EAX);
898 /* next case: synchronization->nest is not 1 */
899 x86_patch (jump_next, code);
900 /* decrease synchronization->nest and return */
901 x86_dec_membase (code, X86_ECX, nest_offset);
902 x86_pop_reg (code, X86_EAX);
905 /* push obj and jump to the actual trampoline */
906 x86_patch (jump_obj_null, code);
907 if (jump_sync_thin_hash)
908 x86_patch (jump_sync_thin_hash, code);
909 x86_patch (jump_have_waiters, code);
910 x86_patch (jump_cmpxchg_failed, code);
911 x86_patch (jump_not_owned, code);
912 x86_patch (jump_sync_null, code);
915 /* obj is pushed, jump to the actual trampoline */
917 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
918 x86_jump_reg (code, X86_EAX);
920 x86_jump_code (code, tramp);
923 nacl_global_codeman_validate (&buf, tramp_size, &code);
925 mono_arch_flush_icache (buf, code - buf);
926 g_assert (code - buf <= tramp_size);
927 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
929 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
937 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
939 g_assert_not_reached ();
944 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
946 g_assert_not_reached ();
953 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
955 /* FIXME: This is not thread safe */
956 guint8 *code = ji->code_start;
958 x86_push_imm (code, func_arg);
959 x86_call_code (code, (guint8*)func);
963 handler_block_trampoline_helper (void)
965 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
966 return jit_tls->handler_block_return_address;
970 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
972 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
975 MonoJumpInfo *ji = NULL;
976 GSList *unwind_ops = NULL;
980 code = buf = mono_global_codeman_reserve (tramp_size);
983 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
987 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
990 if (mono_get_jit_tls_offset () != -1) {
991 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
992 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
994 /*Slow path uses a c helper*/
995 x86_call_code (code, handler_block_trampoline_helper);
997 /* Simulate a call */
998 /*Fix stack alignment*/
999 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1000 /* This is the address the trampoline will return to */
1001 x86_push_reg (code, X86_EAX);
1002 /* Dummy trampoline argument, since we call the generic trampoline directly */
1003 x86_push_imm (code, 0);
1004 x86_jump_code (code, tramp);
1006 nacl_global_codeman_validate (&buf, tramp_size, &code);
1008 mono_arch_flush_icache (buf, code - buf);
1009 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
1010 g_assert (code - buf <= tramp_size);
1012 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1018 mono_arch_get_call_target (guint8 *code)
1020 if (code [-5] == 0xe8) {
1021 gint32 disp = *(gint32*)(code - 4);
1022 guint8 *target = code + disp;
1031 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1033 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1037 * mono_arch_get_gsharedvt_arg_trampoline:
1039 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1042 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1044 guint8 *code, *start;
1049 start = code = mono_domain_code_reserve (domain, buf_len);
1051 x86_mov_reg_imm (code, X86_EAX, arg);
1052 x86_jump_code (code, addr);
1053 g_assert ((code - start) <= buf_len);
1055 nacl_domain_code_validate (domain, &start, buf_len, &code);
1056 mono_arch_flush_icache (start, code - start);
1057 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1062 #if defined(ENABLE_GSHAREDVT)
1064 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1069 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1075 #endif /* !MONOTOUCH */