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);
57 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
63 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
68 MonoDomain *domain = mono_domain_get ();
70 buf_len = NACL_SIZE (10, 32);
72 start = code = mono_domain_code_reserve (domain, buf_len);
74 x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
75 x86_jump_code (code, addr);
76 g_assert ((code - start) <= buf_len);
78 nacl_domain_code_validate (domain, &start, buf_len, &code);
79 mono_arch_flush_icache (start, code - start);
80 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
82 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
88 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
96 start = code = mono_domain_code_reserve (domain, buf_len);
98 this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
101 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
103 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
104 /* Load vtable address */
105 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
106 x86_jump_membase (code, X86_EAX, vt_offset);
108 g_assert ((code - start) < buf_len);
110 nacl_domain_code_validate (domain, &start, buf_len, &code);
112 mono_arch_flush_icache (start, code - start);
113 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
119 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
121 #if defined(__default_codegen__)
124 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
128 /* go to the start of the call instruction
130 * address_byte = (m << 6) | (o << 3) | reg
131 * call opcode: 0xff address_byte displacement
137 if (code [1] == 0xe8) {
139 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
141 /* Tell valgrind to recompile the patched code */
142 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
144 } else if (code [1] == 0xe9) {
145 /* A PLT entry: jmp <DISP> */
147 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
149 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
150 code [4], code [5], code [6]);
151 g_assert_not_reached ();
153 #elif defined(__native_client__)
154 /* Target must be bundle-aligned */
155 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
157 /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
158 if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
160 gint32 offset = (gint32)addr - (gint32)orig_code;
161 guint8 buf[sizeof(gint32)];
162 *((gint32*)(buf)) = offset;
163 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
166 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]);
167 g_assert_not_reached ();
173 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
177 /* Patch the jump table entry used by the plt entry */
179 #if defined(__native_client_codegen__) || defined(__native_client__)
180 /* for both compiler and runtime */
182 /* mov <DISP>(%ebx), %ecx */
183 /* and 0xffffffe0, %ecx */
185 g_assert (code [0] == 0x8b);
186 g_assert (code [1] == 0x8b);
188 offset = *(guint32*)(code + 2);
189 #elif defined(__default_codegen__)
190 /* A PLT entry: jmp *<DISP>(%ebx) */
191 g_assert (code [0] == 0xff);
192 g_assert (code [1] == 0xa3);
194 offset = *(guint32*)(code + 2);
195 #endif /* __native_client_codegen__ */
197 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
198 *(guint8**)((guint8*)got + offset) = addr;
202 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
204 const int kBufSize = NACL_SIZE (8, 16);
209 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
214 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
215 reg = code [1] & 0x07;
216 disp = *((gint32*)(code + 2));
217 #if defined(__native_client_codegen__) || defined(__native_client__)
218 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
219 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
220 disp = *((gint32*)(code - 3));
221 reg = code[-4] & 0x07;
222 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
223 reg = code[-1] & 0x07;
224 disp = (signed char)code[0];
227 g_assert_not_reached ();
231 *displacement = disp;
232 return (gpointer)regs [reg];
236 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
240 vt = get_vcall_slot (code, regs, &displacement);
243 return (gpointer*)((char*)vt + displacement);
247 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
250 guint8 *buf, *code, *tramp;
251 GSList *unwind_ops = NULL;
252 MonoJumpInfo *ji = NULL;
253 int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
255 unwind_ops = mono_arch_get_cie_program ();
257 code = buf = mono_global_codeman_reserve (256);
259 /* Note that there is a single argument to the trampoline
260 * and it is stored at: esp + pushed_args * sizeof (gpointer)
261 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
264 // FIXME: Unwind info
266 /* Compute frame offsets relative to the frame pointer %ebp */
267 arg_offset = sizeof (mgreg_t);
268 caller_ip_offset = 2 * sizeof (mgreg_t);
270 offset += sizeof (MonoLMF);
271 lmf_offset = -offset;
272 offset += X86_NREG * sizeof (mgreg_t);
273 regarray_offset = -offset;
275 offset += 4 * sizeof (mgreg_t);
276 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
279 x86_push_reg (code, X86_EBP);
280 x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
281 /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
282 x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
284 /* Save all registers */
285 for (i = X86_EAX; i <= X86_EDI; ++i) {
289 /* Save original ebp */
290 /* EAX is already saved */
291 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
293 } else if (i == X86_ESP) {
294 /* Save original esp */
295 /* EAX is already saved */
296 x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
297 /* Saved ebp + trampoline arg + return addr */
298 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
301 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
306 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
307 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
309 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
310 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
313 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
314 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
315 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
317 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
320 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
321 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
322 /* callee save registers */
323 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
324 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
325 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
326 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
327 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
328 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
329 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
330 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
333 /* get the address of lmf for the current thread */
335 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
336 x86_call_reg (code, X86_EAX);
338 x86_call_code (code, mono_get_lmf_addr);
340 /* lmf->lmf_addr = lmf_addr (%eax) */
341 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
342 /* lmf->previous_lmf = *(lmf_addr) */
343 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
344 /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
345 x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
346 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
347 /* *lmf_addr = lmf */
348 x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
349 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
351 /* Call trampoline function */
352 /* Arg 1 - registers */
353 x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
354 x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
355 /* Arg2 - calling code */
356 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
357 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
359 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
360 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
362 /* Arg3 - trampoline argument */
363 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
364 x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
365 /* Arg4 - trampoline address */
367 x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
370 /* check the stack is aligned after the ret ip is pushed */
372 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
373 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
374 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
375 x86_branch_disp (code, X86_CC_Z, 3, FALSE);
376 x86_breakpoint (code);
381 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
382 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
383 x86_call_reg (code, X86_EAX);
385 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
386 x86_call_code (code, tramp);
390 * Overwrite the trampoline argument with the address we need to jump to,
393 x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
395 /* Check for interruptions */
397 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
398 x86_call_reg (code, X86_EAX);
400 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
404 x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
405 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
406 x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
407 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
409 /* Restore registers */
410 for (i = X86_EAX; i <= X86_EDI; ++i) {
411 if (i == X86_ESP || i == X86_EBP)
413 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
415 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
421 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
422 /* Load the value returned by the trampoline */
423 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
424 /* The trampoline returns normally, pop the trampoline argument */
425 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
428 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
429 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
430 x86_pop_reg (code, X86_EAX);
431 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
432 x86_jump_reg (code, X86_EAX);
438 nacl_global_codeman_validate (&buf, 256, &code);
439 g_assert ((code - buf) <= 256);
440 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
442 tramp_name = mono_get_generic_trampoline_name (tramp_type);
443 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
449 #define TRAMPOLINE_SIZE 10
452 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
454 guint8 *code, *buf, *tramp;
456 tramp = mono_get_trampoline_code (tramp_type);
458 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
460 x86_push_imm (buf, arg1);
461 x86_jump_code (buf, tramp);
462 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
464 nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
466 mono_arch_flush_icache (code, buf - code);
467 mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
470 *code_len = buf - code;
476 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
480 guint8 **rgctx_null_jumps;
485 MonoJumpInfo *ji = NULL;
486 GSList *unwind_ops = NULL;
488 unwind_ops = mono_arch_get_cie_program ();
490 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
491 index = MONO_RGCTX_SLOT_INDEX (slot);
493 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
494 for (depth = 0; ; ++depth) {
495 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
497 if (index < size - 1)
502 #if defined(__default_codegen__)
503 tramp_size = (aot ? 64 : 36) + 6 * depth;
504 #elif defined(__native_client_codegen__)
505 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
506 6 * (depth + kNaClAlignment);
509 code = buf = mono_global_codeman_reserve (tramp_size);
511 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
513 /* load vtable/mrgctx ptr */
514 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
516 /* load rgctx ptr from vtable */
517 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
518 /* is the rgctx ptr null? */
519 x86_test_reg_reg (code, X86_EAX, X86_EAX);
520 /* if yes, jump to actual trampoline */
521 rgctx_null_jumps [0] = code;
522 x86_branch8 (code, X86_CC_Z, -1, 1);
525 for (i = 0; i < depth; ++i) {
526 /* load ptr to next array */
527 if (mrgctx && i == 0)
528 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
530 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
531 /* is the ptr null? */
532 x86_test_reg_reg (code, X86_EAX, X86_EAX);
533 /* if yes, jump to actual trampoline */
534 rgctx_null_jumps [i + 1] = code;
535 x86_branch8 (code, X86_CC_Z, -1, 1);
539 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
540 /* is the slot null? */
541 x86_test_reg_reg (code, X86_EAX, X86_EAX);
542 /* if yes, jump to actual trampoline */
543 rgctx_null_jumps [depth + 1] = code;
544 x86_branch8 (code, X86_CC_Z, -1, 1);
545 /* otherwise return */
548 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
549 x86_patch (rgctx_null_jumps [i], code);
551 g_free (rgctx_null_jumps);
553 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
556 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
557 x86_jump_reg (code, X86_EAX);
559 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
561 /* jump to the actual trampoline */
562 x86_jump_code (code, tramp);
565 nacl_global_codeman_validate (&buf, tramp_size, &code);
566 mono_arch_flush_icache (buf, code - buf);
567 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
569 g_assert (code - buf <= tramp_size);
571 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
572 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
579 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
581 * 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
582 * the trampoline to call if the slot is not filled.
585 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
589 MonoJumpInfo *ji = NULL;
590 GSList *unwind_ops = NULL;
594 unwind_ops = mono_arch_get_cie_program ();
598 code = buf = mono_global_codeman_reserve (tramp_size);
600 // FIXME: Currently, we always go to the slow path.
602 /* Load trampoline addr */
603 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
604 /* Load mrgctx/vtable */
605 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
607 x86_jump_reg (code, X86_EAX);
609 nacl_global_codeman_validate (&buf, tramp_size, &code);
610 mono_arch_flush_icache (buf, code - buf);
611 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
613 g_assert (code - buf <= tramp_size);
615 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
620 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
622 * The code produced by this trampoline is equivalent to this:
625 * if (obj->synchronisation) {
626 * if (obj->synchronisation->owner == 0) {
627 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
630 * if (obj->synchronisation->owner == TID) {
631 * ++obj->synchronisation->nest;
636 * return full_monitor_enter ();
640 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
643 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
644 guint8 *jump_lock_taken_true = NULL;
646 int status_offset, nest_offset;
647 MonoJumpInfo *ji = NULL;
648 GSList *unwind_ops = NULL;
650 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
651 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
652 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == X86_EDX);
657 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
658 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
659 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
660 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
661 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
663 tramp_size = NACL_SIZE (128, 192);
665 code = buf = mono_global_codeman_reserve (tramp_size);
667 x86_push_reg (code, X86_EAX);
668 if (mono_thread_get_tls_offset () != -1) {
670 x86_test_membase_imm (code, X86_EDX, 0, 1);
671 /* if *lock_taken is 1, jump to actual trampoline */
672 jump_lock_taken_true = code;
673 x86_branch8 (code, X86_CC_NZ, -1, 1);
674 x86_push_reg (code, X86_EDX);
676 /* MonoObject* obj is in EAX */
678 x86_test_reg_reg (code, X86_EAX, X86_EAX);
679 /* if yes, jump to actual trampoline */
680 jump_obj_null = code;
681 x86_branch8 (code, X86_CC_Z, -1, 1);
683 /* load obj->synchronization to ECX */
684 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
686 if (mono_gc_is_moving ()) {
687 /*if bit zero is set it's a thin hash*/
688 /*FIXME use testb encoding*/
689 x86_test_reg_imm (code, X86_ECX, 0x01);
690 jump_sync_thin_hash = code;
691 x86_branch8 (code, X86_CC_NE, -1, 1);
693 /*clear bits used by the gc*/
694 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
697 /* is synchronization null? */
698 x86_test_reg_reg (code, X86_ECX, X86_ECX);
700 /* if yes, jump to actual trampoline */
701 jump_sync_null = code;
702 x86_branch8 (code, X86_CC_Z, -1, 1);
704 /* load MonoInternalThread* into EDX */
706 /* load_aotconst () puts the result into EAX */
707 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
708 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
709 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
710 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
712 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
714 /* load TID into EDX */
715 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
717 /* is synchronization->owner free */
718 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
719 x86_test_reg_imm (code, X86_EAX, OWNER_MASK);
720 /* if not, jump to next case */
722 x86_branch8 (code, X86_CC_NZ, -1, 1);
724 /* if yes, try a compare-exchange with the TID */
725 /* Form new status */
726 x86_alu_reg_reg (code, X86_OR, X86_EDX, X86_EAX);
727 /* compare and exchange */
728 x86_prefix (code, X86_LOCK_PREFIX);
729 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
730 /* if not successful, jump to actual trampoline */
731 jump_cmpxchg_failed = code;
732 x86_branch8 (code, X86_CC_NZ, -1, 1);
733 /* if successful, pop and return */
735 x86_pop_reg (code, X86_EDX);
736 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
738 x86_pop_reg (code, X86_EAX);
741 /* next case: synchronization->owner is not null */
742 x86_patch (jump_tid, code);
743 /* is synchronization->owner == TID? */
744 x86_alu_reg_imm (code, X86_AND, X86_EAX, OWNER_MASK);
745 x86_alu_reg_reg (code, X86_CMP, X86_EAX, X86_EDX);
746 /* if not, jump to actual trampoline */
747 jump_other_owner = code;
748 x86_branch8 (code, X86_CC_NZ, -1, 1);
749 /* if yes, increment nest */
750 x86_inc_membase (code, X86_ECX, nest_offset);
752 x86_pop_reg (code, X86_EDX);
753 x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
755 x86_pop_reg (code, X86_EAX);
759 /* obj is pushed, jump to the actual trampoline */
760 x86_patch (jump_obj_null, code);
761 if (jump_sync_thin_hash)
762 x86_patch (jump_sync_thin_hash, code);
763 x86_patch (jump_sync_null, code);
764 x86_patch (jump_other_owner, code);
765 x86_patch (jump_cmpxchg_failed, code);
768 x86_pop_reg (code, X86_EDX);
769 x86_patch (jump_lock_taken_true, code);
774 /* We are calling the generic trampoline directly, the argument is pushed
775 * on the stack just like a specific trampoline.
778 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter_v4");
780 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
781 x86_jump_reg (code, X86_EAX);
784 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER_V4));
786 x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER));
789 mono_arch_flush_icache (buf, code - buf);
790 g_assert (code - buf <= tramp_size);
792 nacl_global_codeman_validate (&buf, tramp_size, &code);
793 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
796 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
798 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
804 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
806 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
808 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
809 guint8 *jump_next, *jump_cmpxchg_failed;
811 int status_offset, nest_offset;
812 MonoJumpInfo *ji = NULL;
813 GSList *unwind_ops = NULL;
815 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
817 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
818 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
819 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
820 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
821 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
823 tramp_size = NACL_SIZE (128, 192);
825 code = buf = mono_global_codeman_reserve (tramp_size);
827 x86_push_reg (code, X86_EAX);
828 if (mono_thread_get_tls_offset () != -1) {
829 /* MonoObject* obj is in EAX */
831 x86_test_reg_reg (code, X86_EAX, X86_EAX);
832 /* if yes, jump to actual trampoline */
833 jump_obj_null = code;
834 x86_branch8 (code, X86_CC_Z, -1, 1);
836 /* load obj->synchronization to ECX */
837 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
839 if (mono_gc_is_moving ()) {
840 /*if bit zero is set it's a thin hash*/
841 /*FIXME use testb encoding*/
842 x86_test_reg_imm (code, X86_ECX, 0x01);
843 jump_sync_thin_hash = code;
844 x86_branch8 (code, X86_CC_NE, -1, 1);
846 /*clear bits used by the gc*/
847 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
850 /* is synchronization null? */
851 x86_test_reg_reg (code, X86_ECX, X86_ECX);
852 /* if yes, jump to actual trampoline */
853 jump_sync_null = code;
854 x86_branch8 (code, X86_CC_Z, -1, 1);
856 /* next case: synchronization is not null */
857 /* load MonoInternalThread* into EDX */
859 /* load_aotconst () puts the result into EAX */
860 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
861 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
862 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
863 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
865 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
867 /* load TID into EDX */
868 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
869 /* is synchronization->owner == TID */
870 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
871 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EAX);
872 x86_test_reg_imm (code, X86_EDX, OWNER_MASK);
873 /* if no, jump to actual trampoline */
874 jump_not_owned = code;
875 x86_branch8 (code, X86_CC_NZ, -1, 1);
877 /* next case: synchronization->owner == TID */
878 /* is synchronization->nest == 1 */
879 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
880 /* if not, jump to next case */
882 x86_branch8 (code, X86_CC_NZ, -1, 1);
883 /* if yes, is synchronization->entry_count greater than zero? */
884 x86_test_reg_imm (code, X86_EAX, ENTRY_COUNT_WAITERS);
885 /* if yes, jump to actual trampoline */
886 jump_have_waiters = code;
887 x86_branch8 (code, X86_CC_NZ, -1 , 1);
888 /* if not, try to set synchronization->owner to null and return */
889 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
890 x86_alu_reg_imm (code, X86_AND, X86_EDX, ENTRY_COUNT_MASK);
891 /* compare and exchange */
892 x86_prefix (code, X86_LOCK_PREFIX);
893 /* EAX contains the previous status */
894 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
895 /* if not successful, jump to actual trampoline */
896 jump_cmpxchg_failed = code;
897 x86_branch8 (code, X86_CC_NZ, -1, 1);
899 x86_pop_reg (code, X86_EAX);
902 /* next case: synchronization->nest is not 1 */
903 x86_patch (jump_next, code);
904 /* decrease synchronization->nest and return */
905 x86_dec_membase (code, X86_ECX, nest_offset);
906 x86_pop_reg (code, X86_EAX);
909 /* push obj and jump to the actual trampoline */
910 x86_patch (jump_obj_null, code);
911 if (jump_sync_thin_hash)
912 x86_patch (jump_sync_thin_hash, code);
913 x86_patch (jump_have_waiters, code);
914 x86_patch (jump_cmpxchg_failed, code);
915 x86_patch (jump_not_owned, code);
916 x86_patch (jump_sync_null, code);
919 /* obj is pushed, jump to the actual trampoline */
921 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
922 x86_jump_reg (code, X86_EAX);
924 x86_jump_code (code, tramp);
927 nacl_global_codeman_validate (&buf, tramp_size, &code);
929 mono_arch_flush_icache (buf, code - buf);
930 g_assert (code - buf <= tramp_size);
931 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
933 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
941 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
943 g_assert_not_reached ();
948 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
950 g_assert_not_reached ();
957 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
959 /* FIXME: This is not thread safe */
960 guint8 *code = ji->code_start;
962 x86_push_imm (code, func_arg);
963 x86_call_code (code, (guint8*)func);
967 handler_block_trampoline_helper (void)
969 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
970 return jit_tls->handler_block_return_address;
974 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
976 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
979 MonoJumpInfo *ji = NULL;
980 GSList *unwind_ops = NULL;
984 code = buf = mono_global_codeman_reserve (tramp_size);
987 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
991 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
994 if (mono_get_jit_tls_offset () != -1) {
995 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
996 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
998 /*Slow path uses a c helper*/
999 x86_call_code (code, handler_block_trampoline_helper);
1001 /* Simulate a call */
1002 /*Fix stack alignment*/
1003 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1004 /* This is the address the trampoline will return to */
1005 x86_push_reg (code, X86_EAX);
1006 /* Dummy trampoline argument, since we call the generic trampoline directly */
1007 x86_push_imm (code, 0);
1008 x86_jump_code (code, tramp);
1010 nacl_global_codeman_validate (&buf, tramp_size, &code);
1012 mono_arch_flush_icache (buf, code - buf);
1013 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
1014 g_assert (code - buf <= tramp_size);
1016 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1022 mono_arch_get_call_target (guint8 *code)
1024 if (code [-5] == 0xe8) {
1025 gint32 disp = *(gint32*)(code - 4);
1026 guint8 *target = code + disp;
1035 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1037 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1041 * mono_arch_get_gsharedvt_arg_trampoline:
1043 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1046 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1048 guint8 *code, *start;
1053 start = code = mono_domain_code_reserve (domain, buf_len);
1055 x86_mov_reg_imm (code, X86_EAX, arg);
1056 x86_jump_code (code, addr);
1057 g_assert ((code - start) <= buf_len);
1059 nacl_domain_code_validate (domain, &start, buf_len, &code);
1060 mono_arch_flush_icache (start, code - start);
1061 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1063 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
1068 #if defined(ENABLE_GSHAREDVT)
1070 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1075 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1081 #endif /* !MONOTOUCH */