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/profiler-private.h>
21 #include <mono/metadata/gc-internals.h>
22 #include <mono/arch/x86/x86-codegen.h>
24 #include <mono/utils/memcheck.h>
28 #include "debugger-agent.h"
29 #include "jit-icalls.h"
31 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
34 * mono_arch_get_unbox_trampoline:
36 * @addr: pointer to native code for @m
38 * when value type methods are called through the vtable we need to unbox the
39 * this argument. This method returns a pointer to a trampoline which does
40 * unboxing before calling the method
43 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
46 int this_pos = 4, size = NACL_SIZE(16, 32);
47 MonoDomain *domain = mono_domain_get ();
50 start = code = mono_domain_code_reserve (domain, size);
52 unwind_ops = mono_arch_get_cie_program ();
54 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
55 x86_jump_code (code, addr);
56 g_assert ((code - start) < size);
58 nacl_domain_code_validate (domain, &start, size, &code);
59 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
61 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
67 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
73 MonoDomain *domain = mono_domain_get ();
75 buf_len = NACL_SIZE (10, 32);
77 start = code = mono_domain_code_reserve (domain, buf_len);
79 unwind_ops = mono_arch_get_cie_program ();
81 x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
82 x86_jump_code (code, addr);
83 g_assert ((code - start) <= buf_len);
85 nacl_domain_code_validate (domain, &start, buf_len, &code);
86 mono_arch_flush_icache (start, code - start);
87 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
89 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
95 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
97 #if defined(__default_codegen__)
100 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
104 /* go to the start of the call instruction
106 * address_byte = (m << 6) | (o << 3) | reg
107 * call opcode: 0xff address_byte displacement
113 if (code [1] == 0xe8) {
115 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
117 /* Tell valgrind to recompile the patched code */
118 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
120 } else if (code [1] == 0xe9) {
121 /* A PLT entry: jmp <DISP> */
123 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
125 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
126 code [4], code [5], code [6]);
127 g_assert_not_reached ();
129 #elif defined(__native_client__)
130 /* Target must be bundle-aligned */
131 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
133 /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
134 if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
136 gint32 offset = (gint32)addr - (gint32)orig_code;
137 guint8 buf[sizeof(gint32)];
138 *((gint32*)(buf)) = offset;
139 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
142 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]);
143 g_assert_not_reached ();
149 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
153 /* Patch the jump table entry used by the plt entry */
155 #if defined(__native_client_codegen__) || defined(__native_client__)
156 /* for both compiler and runtime */
158 /* mov <DISP>(%ebx), %ecx */
159 /* and 0xffffffe0, %ecx */
161 g_assert (code [0] == 0x8b);
162 g_assert (code [1] == 0x8b);
164 offset = *(guint32*)(code + 2);
165 #elif defined(__default_codegen__)
166 /* A PLT entry: jmp *<DISP>(%ebx) */
167 g_assert (code [0] == 0xff);
168 g_assert (code [1] == 0xa3);
170 offset = *(guint32*)(code + 2);
171 #endif /* __native_client_codegen__ */
173 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
174 *(guint8**)((guint8*)got + offset) = addr;
178 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
180 const int kBufSize = NACL_SIZE (8, 16);
185 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
190 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
191 reg = code [1] & 0x07;
192 disp = *((gint32*)(code + 2));
193 #if defined(__native_client_codegen__) || defined(__native_client__)
194 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
195 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
196 disp = *((gint32*)(code - 3));
197 reg = code[-4] & 0x07;
198 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
199 reg = code[-1] & 0x07;
200 disp = (signed char)code[0];
203 g_assert_not_reached ();
207 *displacement = disp;
208 return (gpointer)regs [reg];
212 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
216 vt = get_vcall_slot (code, regs, &displacement);
219 return (gpointer*)((char*)vt + displacement);
223 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
226 guint8 *buf, *code, *tramp, *br_ex_check;
227 GSList *unwind_ops = NULL;
228 MonoJumpInfo *ji = NULL;
229 int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
230 int cfa_offset; /* cfa = cfa_reg + cfa_offset */
232 code = buf = mono_global_codeman_reserve (256);
234 /* Note that there is a single argument to the trampoline
235 * and it is stored at: esp + pushed_args * sizeof (gpointer)
236 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
239 /* Compute frame offsets relative to the frame pointer %ebp */
240 arg_offset = sizeof (mgreg_t);
241 caller_ip_offset = 2 * sizeof (mgreg_t);
243 offset += sizeof (MonoLMF);
244 lmf_offset = -offset;
245 offset += X86_NREG * sizeof (mgreg_t);
246 regarray_offset = -offset;
248 offset += 4 * sizeof (mgreg_t);
249 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
251 /* ret addr and arg are on the stack */
252 cfa_offset = 2 * sizeof (mgreg_t);
253 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
254 // IP saved at CFA - 4
255 mono_add_unwind_op_offset (unwind_ops, code, buf, X86_NREG, -4);
258 x86_push_reg (code, X86_EBP);
259 cfa_offset += sizeof (mgreg_t);
260 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
261 mono_add_unwind_op_offset (unwind_ops, code, buf, X86_EBP, -cfa_offset);
263 x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
264 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, X86_EBP);
266 /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
267 x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
269 /* Save all registers */
270 for (i = X86_EAX; i <= X86_EDI; ++i) {
274 /* Save original ebp */
275 /* EAX is already saved */
276 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
278 } else if (i == X86_ESP) {
279 /* Save original esp */
280 /* EAX is already saved */
281 x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
282 /* Saved ebp + trampoline arg + return addr */
283 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
286 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
291 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
292 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
294 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
295 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
298 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
299 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
300 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
302 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
305 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
306 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
307 /* callee save registers */
308 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
309 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
310 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
311 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
312 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
313 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
314 x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
315 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
318 /* get the address of lmf for the current thread */
320 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
321 x86_call_reg (code, X86_EAX);
323 x86_call_code (code, mono_get_lmf_addr);
325 /* lmf->lmf_addr = lmf_addr (%eax) */
326 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
327 /* lmf->previous_lmf = *(lmf_addr) */
328 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
329 /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
330 x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
331 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
332 /* *lmf_addr = lmf */
333 x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
334 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
336 /* Call trampoline function */
337 /* Arg 1 - registers */
338 x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
339 x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
340 /* Arg2 - calling code */
341 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
342 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
344 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
345 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
347 /* Arg3 - trampoline argument */
348 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
349 x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
350 /* Arg4 - trampoline address */
352 x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
355 /* check the stack is aligned after the ret ip is pushed */
357 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
358 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
359 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
360 x86_branch_disp (code, X86_CC_Z, 3, FALSE);
361 x86_breakpoint (code);
366 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
367 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
368 x86_call_reg (code, X86_EAX);
370 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
371 x86_call_code (code, tramp);
375 * Overwrite the trampoline argument with the address we need to jump to,
378 x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
381 x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
382 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
383 x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
384 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
386 /* Check for interruptions */
388 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
389 x86_call_reg (code, X86_EAX);
391 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
394 x86_test_reg_reg (code, X86_EAX, X86_EAX);
396 x86_branch8 (code, X86_CC_Z, -1, 1);
400 * We have an exception we want to throw in the caller's frame, so pop
401 * the trampoline frame and throw from the caller.
405 * The exception is in eax.
406 * We are calling the throw trampoline used by OP_THROW, so we have to setup the
407 * stack to look the same.
408 * The stack contains the ret addr, and the trampoline argument, the throw trampoline
409 * expects it to contain the ret addr and the exception. It also needs to be aligned
410 * after the exception is pushed.
413 x86_push_reg (code, X86_EAX);
414 /* Push the exception */
415 x86_push_reg (code, X86_EAX);
416 //x86_breakpoint (code);
417 /* Push the original return value */
418 x86_push_membase (code, X86_ESP, 3 * 4);
420 * EH is initialized after trampolines, so get the address of the variable
421 * which contains throw_exception, and load it from there.
424 /* Not really a jit icall */
425 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
427 x86_mov_reg_imm (code, X86_ECX, (guint8*)mono_get_throw_exception_addr ());
429 x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof(gpointer));
430 x86_jump_reg (code, X86_ECX);
433 mono_x86_patch (br_ex_check, code);
435 /* Restore registers */
436 for (i = X86_EAX; i <= X86_EDI; ++i) {
437 if (i == X86_ESP || i == X86_EBP)
439 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
441 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
446 cfa_offset -= sizeof (mgreg_t);
447 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
448 mono_add_unwind_op_same_value (unwind_ops, code, buf, X86_EBP);
450 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
451 /* Load the value returned by the trampoline */
452 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
453 /* The trampoline returns normally, pop the trampoline argument */
454 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
455 cfa_offset -= sizeof (mgreg_t);
456 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
459 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
460 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
461 x86_pop_reg (code, X86_EAX);
462 cfa_offset -= sizeof (mgreg_t);
463 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
464 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
465 x86_jump_reg (code, X86_EAX);
471 nacl_global_codeman_validate (&buf, 256, &code);
472 g_assert ((code - buf) <= 256);
473 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
475 tramp_name = mono_get_generic_trampoline_name (tramp_type);
476 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
482 #define TRAMPOLINE_SIZE 10
485 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
487 guint8 *code, *buf, *tramp;
489 tramp = mono_get_trampoline_code (tramp_type);
491 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
493 x86_push_imm (buf, arg1);
494 x86_jump_code (buf, tramp);
495 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
497 nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
499 mono_arch_flush_icache (code, buf - code);
500 mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
503 *code_len = buf - code;
509 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
513 guint8 **rgctx_null_jumps;
518 MonoJumpInfo *ji = NULL;
519 GSList *unwind_ops = NULL;
521 unwind_ops = mono_arch_get_cie_program ();
523 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
524 index = MONO_RGCTX_SLOT_INDEX (slot);
526 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
527 for (depth = 0; ; ++depth) {
528 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
530 if (index < size - 1)
535 #if defined(__default_codegen__)
536 tramp_size = (aot ? 64 : 36) + 6 * depth;
537 #elif defined(__native_client_codegen__)
538 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
539 6 * (depth + kNaClAlignment);
542 code = buf = mono_global_codeman_reserve (tramp_size);
544 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
546 /* load vtable/mrgctx ptr */
547 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
549 /* load rgctx ptr from vtable */
550 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
551 /* is the rgctx ptr null? */
552 x86_test_reg_reg (code, X86_EAX, X86_EAX);
553 /* if yes, jump to actual trampoline */
554 rgctx_null_jumps [0] = code;
555 x86_branch8 (code, X86_CC_Z, -1, 1);
558 for (i = 0; i < depth; ++i) {
559 /* load ptr to next array */
560 if (mrgctx && i == 0)
561 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
563 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
564 /* is the ptr null? */
565 x86_test_reg_reg (code, X86_EAX, X86_EAX);
566 /* if yes, jump to actual trampoline */
567 rgctx_null_jumps [i + 1] = code;
568 x86_branch8 (code, X86_CC_Z, -1, 1);
572 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
573 /* is the slot null? */
574 x86_test_reg_reg (code, X86_EAX, X86_EAX);
575 /* if yes, jump to actual trampoline */
576 rgctx_null_jumps [depth + 1] = code;
577 x86_branch8 (code, X86_CC_Z, -1, 1);
578 /* otherwise return */
581 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
582 x86_patch (rgctx_null_jumps [i], code);
584 g_free (rgctx_null_jumps);
586 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
589 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
590 x86_jump_reg (code, X86_EAX);
592 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
594 /* jump to the actual trampoline */
595 x86_jump_code (code, tramp);
598 nacl_global_codeman_validate (&buf, tramp_size, &code);
599 mono_arch_flush_icache (buf, code - buf);
600 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
602 g_assert (code - buf <= tramp_size);
604 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
605 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
612 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
614 * 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
615 * the trampoline to call if the slot is not filled.
618 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
622 MonoJumpInfo *ji = NULL;
623 GSList *unwind_ops = NULL;
627 unwind_ops = mono_arch_get_cie_program ();
631 code = buf = mono_global_codeman_reserve (tramp_size);
633 // FIXME: Currently, we always go to the slow path.
635 /* Load trampoline addr */
636 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
637 /* Load mrgctx/vtable */
638 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
640 x86_jump_reg (code, X86_EAX);
642 nacl_global_codeman_validate (&buf, tramp_size, &code);
643 mono_arch_flush_icache (buf, code - buf);
644 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
646 g_assert (code - buf <= tramp_size);
648 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
654 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
656 /* FIXME: This is not thread safe */
657 guint8 *code = ji->code_start;
659 x86_push_imm (code, func_arg);
660 x86_call_code (code, (guint8*)func);
664 handler_block_trampoline_helper (void)
666 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
667 return jit_tls->handler_block_return_address;
671 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
673 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
676 MonoJumpInfo *ji = NULL;
678 GSList *unwind_ops = NULL;
682 code = buf = mono_global_codeman_reserve (tramp_size);
684 unwind_ops = mono_arch_get_cie_program ();
685 cfa_offset = sizeof (mgreg_t);
687 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
691 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
694 if (mono_get_jit_tls_offset () != -1) {
695 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
696 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
698 /*Slow path uses a c helper*/
699 x86_call_code (code, handler_block_trampoline_helper);
701 /* Simulate a call */
702 /*Fix stack alignment*/
703 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
704 cfa_offset += sizeof (mgreg_t);
705 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
707 /* This is the address the trampoline will return to */
708 x86_push_reg (code, X86_EAX);
709 cfa_offset += sizeof (mgreg_t);
710 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
712 /* Dummy trampoline argument, since we call the generic trampoline directly */
713 x86_push_imm (code, 0);
714 cfa_offset += sizeof (mgreg_t);
715 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
716 x86_jump_code (code, tramp);
718 nacl_global_codeman_validate (&buf, tramp_size, &code);
720 mono_arch_flush_icache (buf, code - buf);
721 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
722 g_assert (code - buf <= tramp_size);
724 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
730 mono_arch_get_call_target (guint8 *code)
732 if (code [-5] == 0xe8) {
733 gint32 disp = *(gint32*)(code - 4);
734 guint8 *target = code + disp;
743 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
745 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
749 * mono_arch_get_gsharedvt_arg_trampoline:
751 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
754 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
756 guint8 *code, *start;
763 start = code = mono_domain_code_reserve (domain, buf_len);
765 unwind_ops = mono_arch_get_cie_program ();
767 x86_mov_reg_imm (code, X86_EAX, arg);
768 x86_jump_code (code, addr);
769 g_assert ((code - start) <= buf_len);
771 nacl_domain_code_validate (domain, &start, buf_len, &code);
772 mono_arch_flush_icache (start, code - start);
773 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
775 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
781 * mono_arch_create_sdb_trampoline:
783 * Return a trampoline which captures the current context, passes it to
784 * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
785 * then restores the (potentially changed) context.
788 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
790 int tramp_size = 256;
791 int framesize, ctx_offset, cfa_offset;
793 GSList *unwind_ops = NULL;
794 MonoJumpInfo *ji = NULL;
796 code = buf = mono_global_codeman_reserve (tramp_size);
801 framesize += sizeof (mgreg_t);
803 framesize = ALIGN_TO (framesize, 8);
804 ctx_offset = framesize;
805 framesize += sizeof (MonoContext);
807 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
811 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, 4);
812 // IP saved at CFA - 4
813 mono_add_unwind_op_offset (unwind_ops, code, buf, X86_NREG, -cfa_offset);
815 x86_push_reg (code, X86_EBP);
816 cfa_offset += sizeof(mgreg_t);
817 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
818 mono_add_unwind_op_offset (unwind_ops, code, buf, X86_EBP, - cfa_offset);
820 x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof(mgreg_t));
821 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, X86_EBP);
822 /* The + 8 makes the stack aligned */
823 x86_alu_reg_imm (code, X86_SUB, X86_ESP, framesize + 8);
825 /* Initialize a MonoContext structure on the stack */
826 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), X86_EAX, sizeof (mgreg_t));
827 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), X86_EBX, sizeof (mgreg_t));
828 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), X86_ECX, sizeof (mgreg_t));
829 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), X86_EDX, sizeof (mgreg_t));
830 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
831 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), X86_EAX, sizeof (mgreg_t));
832 x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
833 x86_alu_reg_imm (code, X86_ADD, X86_EAX, cfa_offset);
834 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esp), X86_ESP, sizeof (mgreg_t));
835 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), X86_ESI, sizeof (mgreg_t));
836 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), X86_EDI, sizeof (mgreg_t));
837 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 4, sizeof (mgreg_t));
838 x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), X86_EAX, sizeof (mgreg_t));
840 /* Call the single step/breakpoint function in sdb */
841 x86_lea_membase (code, X86_EAX, X86_ESP, ctx_offset);
842 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, sizeof (mgreg_t));
845 x86_breakpoint (code);
848 x86_call_code (code, debugger_agent_single_step_from_context);
850 x86_call_code (code, debugger_agent_breakpoint_from_context);
853 /* Restore registers from ctx */
854 /* Overwrite the saved ebp */
855 x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), sizeof (mgreg_t));
856 x86_mov_membase_reg (code, X86_EBP, 0, X86_EAX, sizeof (mgreg_t));
857 /* Overwrite saved eip */
858 x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), sizeof (mgreg_t));
859 x86_mov_membase_reg (code, X86_EBP, 4, X86_EAX, sizeof (mgreg_t));
860 x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), sizeof (mgreg_t));
861 x86_mov_reg_membase (code, X86_EBX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), sizeof (mgreg_t));
862 x86_mov_reg_membase (code, X86_ECX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), sizeof (mgreg_t));
863 x86_mov_reg_membase (code, X86_EDX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), sizeof (mgreg_t));
864 x86_mov_reg_membase (code, X86_ESI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), sizeof (mgreg_t));
865 x86_mov_reg_membase (code, X86_EDI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), sizeof (mgreg_t));
868 cfa_offset -= sizeof (mgreg_t);
869 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
872 mono_arch_flush_icache (code, code - buf);
873 g_assert (code - buf <= tramp_size);
875 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
876 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);