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);
80 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
86 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
94 start = code = mono_domain_code_reserve (domain, buf_len);
96 this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
99 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
101 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
102 /* Load vtable address */
103 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
104 x86_jump_membase (code, X86_EAX, vt_offset);
106 g_assert ((code - start) < buf_len);
108 nacl_domain_code_validate (domain, &start, buf_len, &code);
110 mono_arch_flush_icache (start, code - start);
111 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
117 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
119 #if defined(__default_codegen__)
122 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
126 /* go to the start of the call instruction
128 * address_byte = (m << 6) | (o << 3) | reg
129 * call opcode: 0xff address_byte displacement
135 if (code [1] == 0xe8) {
137 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
139 /* Tell valgrind to recompile the patched code */
140 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
142 } else if (code [1] == 0xe9) {
143 /* A PLT entry: jmp <DISP> */
145 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
147 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
148 code [4], code [5], code [6]);
149 g_assert_not_reached ();
151 #elif defined(__native_client__)
152 /* Target must be bundle-aligned */
153 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
155 /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
156 if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
158 gint32 offset = (gint32)addr - (gint32)orig_code;
159 guint8 buf[sizeof(gint32)];
160 *((gint32*)(buf)) = offset;
161 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
164 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]);
165 g_assert_not_reached ();
171 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
175 /* Patch the jump table entry used by the plt entry */
177 #if defined(__native_client_codegen__) || defined(__native_client__)
178 /* for both compiler and runtime */
180 /* mov <DISP>(%ebx), %ecx */
181 /* and 0xffffffe0, %ecx */
183 g_assert (code [0] == 0x8b);
184 g_assert (code [1] == 0x8b);
186 offset = *(guint32*)(code + 2);
187 #elif defined(__default_codegen__)
188 /* A PLT entry: jmp *<DISP>(%ebx) */
189 g_assert (code [0] == 0xff);
190 g_assert (code [1] == 0xa3);
192 offset = *(guint32*)(code + 2);
193 #endif /* __native_client_codegen__ */
195 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
196 *(guint8**)((guint8*)got + offset) = addr;
200 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
202 const int kBufSize = NACL_SIZE (8, 16);
207 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
212 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
213 reg = code [1] & 0x07;
214 disp = *((gint32*)(code + 2));
215 #if defined(__native_client_codegen__) || defined(__native_client__)
216 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
217 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
218 disp = *((gint32*)(code - 3));
219 reg = code[-4] & 0x07;
220 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
221 reg = code[-1] & 0x07;
222 disp = (signed char)code[0];
225 g_assert_not_reached ();
229 *displacement = disp;
230 return (gpointer)regs [reg];
234 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
238 vt = get_vcall_slot (code, regs, &displacement);
241 return (gpointer*)((char*)vt + displacement);
245 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
248 guint8 *buf, *code, *tramp;
249 GSList *unwind_ops = NULL;
250 MonoJumpInfo *ji = NULL;
251 int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
253 unwind_ops = mono_arch_get_cie_program ();
255 code = buf = mono_global_codeman_reserve (256);
257 /* Note that there is a single argument to the trampoline
258 * and it is stored at: esp + pushed_args * sizeof (gpointer)
259 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
262 // FIXME: Unwind info
264 /* Compute frame offsets relative to the frame pointer %ebp */
265 arg_offset = sizeof (mgreg_t);
266 caller_ip_offset = 2 * sizeof (mgreg_t);
268 offset += sizeof (MonoLMF);
269 lmf_offset = -offset;
270 offset += X86_NREG * sizeof (mgreg_t);
271 regarray_offset = -offset;
273 offset += 4 * sizeof (mgreg_t);
274 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
277 x86_push_reg (code, X86_EBP);
278 x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
279 /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
280 x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
282 /* Save all registers */
283 for (i = X86_EAX; i <= X86_EDI; ++i) {
287 /* Save original ebp */
288 /* EAX is already saved */
289 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
291 } else if (i == X86_ESP) {
292 /* Save original esp */
293 /* EAX is already saved */
294 x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
295 /* Saved ebp + trampoline arg + return addr */
296 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
299 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
304 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
305 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
307 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
308 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
311 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
312 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
313 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
315 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
318 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
319 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
320 /* callee save registers */
321 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
322 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
323 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
324 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
325 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
326 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
327 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
328 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
331 /* get the address of lmf for the current thread */
333 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
334 x86_call_reg (code, X86_EAX);
336 x86_call_code (code, mono_get_lmf_addr);
338 /* lmf->lmf_addr = lmf_addr (%eax) */
339 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
340 /* lmf->previous_lmf = *(lmf_addr) */
341 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
342 /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
343 x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
344 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
345 /* *lmf_addr = lmf */
346 x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
347 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
349 /* Call trampoline function */
350 /* Arg 1 - registers */
351 x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
352 x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
353 /* Arg2 - calling code */
354 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
355 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
357 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
358 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
360 /* Arg3 - trampoline argument */
361 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
362 x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
363 /* Arg4 - trampoline address */
365 x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
368 /* check the stack is aligned after the ret ip is pushed */
370 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
371 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
372 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
373 x86_branch_disp (code, X86_CC_Z, 3, FALSE);
374 x86_breakpoint (code);
379 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
380 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
381 x86_call_reg (code, X86_EAX);
383 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
384 x86_call_code (code, tramp);
388 * Overwrite the trampoline argument with the address we need to jump to,
391 x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
393 /* Check for interruptions */
395 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
396 x86_call_reg (code, X86_EAX);
398 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
402 x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
403 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
404 x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
405 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
407 /* Restore registers */
408 for (i = X86_EAX; i <= X86_EDI; ++i) {
409 if (i == X86_ESP || i == X86_EBP)
411 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
413 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
419 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
420 /* Load the value returned by the trampoline */
421 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
422 /* The trampoline returns normally, pop the trampoline argument */
423 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
426 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
427 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
428 x86_pop_reg (code, X86_EAX);
429 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
430 x86_jump_reg (code, X86_EAX);
436 nacl_global_codeman_validate (&buf, 256, &code);
437 g_assert ((code - buf) <= 256);
438 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
440 tramp_name = mono_get_generic_trampoline_name (tramp_type);
441 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
447 #define TRAMPOLINE_SIZE 10
450 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
452 guint8 *code, *buf, *tramp;
454 tramp = mono_get_trampoline_code (tramp_type);
456 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
458 x86_push_imm (buf, arg1);
459 x86_jump_code (buf, tramp);
460 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
462 nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
464 mono_arch_flush_icache (code, buf - code);
465 mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
468 *code_len = buf - code;
474 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
478 guint8 **rgctx_null_jumps;
483 MonoJumpInfo *ji = NULL;
484 GSList *unwind_ops = NULL;
486 unwind_ops = mono_arch_get_cie_program ();
488 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
489 index = MONO_RGCTX_SLOT_INDEX (slot);
491 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
492 for (depth = 0; ; ++depth) {
493 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
495 if (index < size - 1)
500 #if defined(__default_codegen__)
501 tramp_size = (aot ? 64 : 36) + 6 * depth;
502 #elif defined(__native_client_codegen__)
503 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
504 6 * (depth + kNaClAlignment);
507 code = buf = mono_global_codeman_reserve (tramp_size);
509 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
511 /* load vtable/mrgctx ptr */
512 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
514 /* load rgctx ptr from vtable */
515 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
516 /* is the rgctx ptr null? */
517 x86_test_reg_reg (code, X86_EAX, X86_EAX);
518 /* if yes, jump to actual trampoline */
519 rgctx_null_jumps [0] = code;
520 x86_branch8 (code, X86_CC_Z, -1, 1);
523 for (i = 0; i < depth; ++i) {
524 /* load ptr to next array */
525 if (mrgctx && i == 0)
526 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
528 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
529 /* is the ptr null? */
530 x86_test_reg_reg (code, X86_EAX, X86_EAX);
531 /* if yes, jump to actual trampoline */
532 rgctx_null_jumps [i + 1] = code;
533 x86_branch8 (code, X86_CC_Z, -1, 1);
537 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
538 /* is the slot null? */
539 x86_test_reg_reg (code, X86_EAX, X86_EAX);
540 /* if yes, jump to actual trampoline */
541 rgctx_null_jumps [depth + 1] = code;
542 x86_branch8 (code, X86_CC_Z, -1, 1);
543 /* otherwise return */
546 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
547 x86_patch (rgctx_null_jumps [i], code);
549 g_free (rgctx_null_jumps);
551 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
554 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
555 x86_jump_reg (code, X86_EAX);
557 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
559 /* jump to the actual trampoline */
560 x86_jump_code (code, tramp);
563 nacl_global_codeman_validate (&buf, tramp_size, &code);
564 mono_arch_flush_icache (buf, code - buf);
565 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
567 g_assert (code - buf <= tramp_size);
569 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
570 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
577 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
579 * 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
580 * the trampoline to call if the slot is not filled.
583 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
587 MonoJumpInfo *ji = NULL;
588 GSList *unwind_ops = NULL;
592 unwind_ops = mono_arch_get_cie_program ();
596 code = buf = mono_global_codeman_reserve (tramp_size);
598 // FIXME: Currently, we always go to the slow path.
600 /* Load trampoline addr */
601 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
602 /* Load mrgctx/vtable */
603 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
605 x86_jump_reg (code, X86_EAX);
607 nacl_global_codeman_validate (&buf, tramp_size, &code);
608 mono_arch_flush_icache (buf, code - buf);
609 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
611 g_assert (code - buf <= tramp_size);
613 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
618 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
620 * The code produced by this trampoline is equivalent to this:
623 * if (obj->synchronisation) {
624 * if (obj->synchronisation->owner == 0) {
625 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
628 * if (obj->synchronisation->owner == TID) {
629 * ++obj->synchronisation->nest;
634 * return full_monitor_enter ();
638 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
641 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
642 guint8 *jump_lock_taken_true = NULL;
644 int status_offset, nest_offset;
645 MonoJumpInfo *ji = NULL;
646 GSList *unwind_ops = NULL;
648 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
649 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
650 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == X86_EDX);
655 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
656 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
657 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
658 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
659 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
661 tramp_size = NACL_SIZE (128, 192);
663 code = buf = mono_global_codeman_reserve (tramp_size);
665 x86_push_reg (code, X86_EAX);
666 if (mono_thread_get_tls_offset () != -1) {
668 x86_test_membase_imm (code, X86_EDX, 0, 1);
669 /* if *lock_taken is 1, jump to actual trampoline */
670 jump_lock_taken_true = code;
671 x86_branch8 (code, X86_CC_NZ, -1, 1);
672 x86_push_reg (code, X86_EDX);
674 /* MonoObject* obj is in EAX */
676 x86_test_reg_reg (code, X86_EAX, X86_EAX);
677 /* if yes, jump to actual trampoline */
678 jump_obj_null = code;
679 x86_branch8 (code, X86_CC_Z, -1, 1);
681 /* load obj->synchronization to ECX */
682 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
684 if (mono_gc_is_moving ()) {
685 /*if bit zero is set it's a thin hash*/
686 /*FIXME use testb encoding*/
687 x86_test_reg_imm (code, X86_ECX, 0x01);
688 jump_sync_thin_hash = code;
689 x86_branch8 (code, X86_CC_NE, -1, 1);
691 /*clear bits used by the gc*/
692 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
695 /* is synchronization null? */
696 x86_test_reg_reg (code, X86_ECX, X86_ECX);
698 /* if yes, jump to actual trampoline */
699 jump_sync_null = code;
700 x86_branch8 (code, X86_CC_Z, -1, 1);
702 /* load MonoInternalThread* into EDX */
704 /* load_aotconst () puts the result into EAX */
705 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
706 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
707 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
708 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
710 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
712 /* load TID into EDX */
713 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
715 /* is synchronization->owner free */
716 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
717 x86_test_reg_imm (code, X86_EAX, OWNER_MASK);
718 /* if not, jump to next case */
720 x86_branch8 (code, X86_CC_NZ, -1, 1);
722 /* if yes, try a compare-exchange with the TID */
723 /* Form new status */
724 x86_alu_reg_reg (code, X86_OR, X86_EDX, X86_EAX);
725 /* compare and exchange */
726 x86_prefix (code, X86_LOCK_PREFIX);
727 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
728 /* if not successful, jump to actual trampoline */
729 jump_cmpxchg_failed = code;
730 x86_branch8 (code, X86_CC_NZ, -1, 1);
731 /* if successful, pop and return */
733 x86_pop_reg (code, X86_EDX);
734 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
736 x86_pop_reg (code, X86_EAX);
739 /* next case: synchronization->owner is not null */
740 x86_patch (jump_tid, code);
741 /* is synchronization->owner == TID? */
742 x86_alu_reg_imm (code, X86_AND, X86_EAX, OWNER_MASK);
743 x86_alu_reg_reg (code, X86_CMP, X86_EAX, X86_EDX);
744 /* if not, jump to actual trampoline */
745 jump_other_owner = code;
746 x86_branch8 (code, X86_CC_NZ, -1, 1);
747 /* if yes, increment nest */
748 x86_inc_membase (code, X86_ECX, nest_offset);
750 x86_pop_reg (code, X86_EDX);
751 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
753 x86_pop_reg (code, X86_EAX);
757 /* obj is pushed, jump to the actual trampoline */
758 x86_patch (jump_obj_null, code);
759 if (jump_sync_thin_hash)
760 x86_patch (jump_sync_thin_hash, code);
761 x86_patch (jump_sync_null, code);
762 x86_patch (jump_other_owner, code);
763 x86_patch (jump_cmpxchg_failed, code);
766 x86_pop_reg (code, X86_EDX);
767 x86_patch (jump_lock_taken_true, code);
772 /* We are calling the generic trampoline directly, the argument is pushed
773 * on the stack just like a specific trampoline.
776 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter_v4");
778 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
779 x86_jump_reg (code, X86_EAX);
782 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER_V4));
784 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER));
787 mono_arch_flush_icache (buf, code - buf);
788 g_assert (code - buf <= tramp_size);
790 nacl_global_codeman_validate (&buf, tramp_size, &code);
791 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
794 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
796 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
802 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
804 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
806 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
807 guint8 *jump_next, *jump_cmpxchg_failed;
809 int status_offset, nest_offset;
810 MonoJumpInfo *ji = NULL;
811 GSList *unwind_ops = NULL;
813 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
815 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
816 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
817 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
818 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
819 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
821 tramp_size = NACL_SIZE (128, 192);
823 code = buf = mono_global_codeman_reserve (tramp_size);
825 x86_push_reg (code, X86_EAX);
826 if (mono_thread_get_tls_offset () != -1) {
827 /* MonoObject* obj is in EAX */
829 x86_test_reg_reg (code, X86_EAX, X86_EAX);
830 /* if yes, jump to actual trampoline */
831 jump_obj_null = code;
832 x86_branch8 (code, X86_CC_Z, -1, 1);
834 /* load obj->synchronization to ECX */
835 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
837 if (mono_gc_is_moving ()) {
838 /*if bit zero is set it's a thin hash*/
839 /*FIXME use testb encoding*/
840 x86_test_reg_imm (code, X86_ECX, 0x01);
841 jump_sync_thin_hash = code;
842 x86_branch8 (code, X86_CC_NE, -1, 1);
844 /*clear bits used by the gc*/
845 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
848 /* is synchronization null? */
849 x86_test_reg_reg (code, X86_ECX, X86_ECX);
850 /* if yes, jump to actual trampoline */
851 jump_sync_null = code;
852 x86_branch8 (code, X86_CC_Z, -1, 1);
854 /* next case: synchronization is not null */
855 /* load MonoInternalThread* into EDX */
857 /* load_aotconst () puts the result into EAX */
858 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
859 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
860 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
861 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
863 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
865 /* load TID into EDX */
866 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
867 /* is synchronization->owner == TID */
868 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
869 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EAX);
870 x86_test_reg_imm (code, X86_EDX, OWNER_MASK);
871 /* if no, jump to actual trampoline */
872 jump_not_owned = code;
873 x86_branch8 (code, X86_CC_NZ, -1, 1);
875 /* next case: synchronization->owner == TID */
876 /* is synchronization->nest == 1 */
877 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
878 /* if not, jump to next case */
880 x86_branch8 (code, X86_CC_NZ, -1, 1);
881 /* if yes, is synchronization->entry_count greater than zero? */
882 x86_test_reg_imm (code, X86_EAX, ENTRY_COUNT_WAITERS);
883 /* if yes, jump to actual trampoline */
884 jump_have_waiters = code;
885 x86_branch8 (code, X86_CC_NZ, -1 , 1);
886 /* if not, try to set synchronization->owner to null and return */
887 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
888 x86_alu_reg_imm (code, X86_AND, X86_EDX, ENTRY_COUNT_MASK);
889 /* compare and exchange */
890 x86_prefix (code, X86_LOCK_PREFIX);
891 /* EAX contains the previous status */
892 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
893 /* if not successful, jump to actual trampoline */
894 jump_cmpxchg_failed = code;
895 x86_branch8 (code, X86_CC_NZ, -1, 1);
897 x86_pop_reg (code, X86_EAX);
900 /* next case: synchronization->nest is not 1 */
901 x86_patch (jump_next, code);
902 /* decrease synchronization->nest and return */
903 x86_dec_membase (code, X86_ECX, nest_offset);
904 x86_pop_reg (code, X86_EAX);
907 /* push obj and jump to the actual trampoline */
908 x86_patch (jump_obj_null, code);
909 if (jump_sync_thin_hash)
910 x86_patch (jump_sync_thin_hash, code);
911 x86_patch (jump_have_waiters, code);
912 x86_patch (jump_cmpxchg_failed, code);
913 x86_patch (jump_not_owned, code);
914 x86_patch (jump_sync_null, code);
917 /* obj is pushed, jump to the actual trampoline */
919 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
920 x86_jump_reg (code, X86_EAX);
922 x86_jump_code (code, tramp);
925 nacl_global_codeman_validate (&buf, tramp_size, &code);
927 mono_arch_flush_icache (buf, code - buf);
928 g_assert (code - buf <= tramp_size);
929 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
931 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
939 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
941 g_assert_not_reached ();
946 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
948 g_assert_not_reached ();
955 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
957 /* FIXME: This is not thread safe */
958 guint8 *code = ji->code_start;
960 x86_push_imm (code, func_arg);
961 x86_call_code (code, (guint8*)func);
965 handler_block_trampoline_helper (void)
967 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
968 return jit_tls->handler_block_return_address;
972 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
974 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
977 MonoJumpInfo *ji = NULL;
978 GSList *unwind_ops = NULL;
982 code = buf = mono_global_codeman_reserve (tramp_size);
985 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
989 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
992 if (mono_get_jit_tls_offset () != -1) {
993 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
994 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
996 /*Slow path uses a c helper*/
997 x86_call_code (code, handler_block_trampoline_helper);
999 /* Simulate a call */
1000 /*Fix stack alignment*/
1001 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1002 /* This is the address the trampoline will return to */
1003 x86_push_reg (code, X86_EAX);
1004 /* Dummy trampoline argument, since we call the generic trampoline directly */
1005 x86_push_imm (code, 0);
1006 x86_jump_code (code, tramp);
1008 nacl_global_codeman_validate (&buf, tramp_size, &code);
1010 mono_arch_flush_icache (buf, code - buf);
1011 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
1012 g_assert (code - buf <= tramp_size);
1014 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1020 mono_arch_get_call_target (guint8 *code)
1022 if (code [-5] == 0xe8) {
1023 gint32 disp = *(gint32*)(code - 4);
1024 guint8 *target = code + disp;
1033 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1035 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1039 * mono_arch_get_gsharedvt_arg_trampoline:
1041 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1044 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1046 guint8 *code, *start;
1051 start = code = mono_domain_code_reserve (domain, buf_len);
1053 x86_mov_reg_imm (code, X86_EAX, arg);
1054 x86_jump_code (code, addr);
1055 g_assert ((code - start) <= buf_len);
1057 nacl_domain_code_validate (domain, &start, buf_len, &code);
1058 mono_arch_flush_icache (start, code - start);
1059 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1064 #if defined(ENABLE_GSHAREDVT)
1066 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1071 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1077 #endif /* !MONOTOUCH */