3 * JIT trampoline code for x86
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2001 Ximian, Inc.
14 #include <mono/metadata/abi-details.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/metadata-internals.h>
17 #include <mono/metadata/marshal.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/metadata/gc-internals.h>
21 #include <mono/arch/x86/x86-codegen.h>
23 #include <mono/utils/memcheck.h>
27 #include "debugger-agent.h"
28 #include "jit-icalls.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
= 16;
46 MonoDomain
*domain
= mono_domain_get ();
49 start
= code
= mono_domain_code_reserve (domain
, size
);
51 unwind_ops
= mono_arch_get_cie_program ();
53 x86_alu_membase_imm (code
, X86_ADD
, X86_ESP
, this_pos
, sizeof (MonoObject
));
54 x86_jump_code (code
, addr
);
55 g_assert ((code
- start
) < size
);
57 mono_profiler_code_buffer_new (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE
, m
);
59 mono_tramp_info_register (mono_tramp_info_create (NULL
, start
, code
- start
, NULL
, unwind_ops
), domain
);
65 mono_arch_get_static_rgctx_trampoline (gpointer arg
, gpointer addr
)
71 MonoDomain
*domain
= mono_domain_get ();
75 start
= code
= mono_domain_code_reserve (domain
, buf_len
);
77 unwind_ops
= mono_arch_get_cie_program ();
79 x86_mov_reg_imm (code
, MONO_ARCH_RGCTX_REG
, arg
);
80 x86_jump_code (code
, addr
);
81 g_assert ((code
- start
) <= buf_len
);
83 mono_arch_flush_icache (start
, code
- start
);
84 mono_profiler_code_buffer_new (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
);
86 mono_tramp_info_register (mono_tramp_info_create (NULL
, start
, code
- start
, NULL
, unwind_ops
), domain
);
92 mono_arch_patch_callsite (guint8
*method_start
, guint8
*orig_code
, guint8
*addr
)
96 gboolean can_write
= mono_breakpoint_clean_code (method_start
, orig_code
, 8, buf
, sizeof (buf
));
100 /* go to the start of the call instruction
102 * address_byte = (m << 6) | (o << 3) | reg
103 * call opcode: 0xff address_byte displacement
109 if (code
[1] == 0xe8) {
111 InterlockedExchange ((gint32
*)(orig_code
+ 2), (guint
)addr
- ((guint
)orig_code
+ 1) - 5);
113 /* Tell valgrind to recompile the patched code */
114 VALGRIND_DISCARD_TRANSLATIONS (orig_code
+ 2, 4);
116 } else if (code
[1] == 0xe9) {
117 /* A PLT entry: jmp <DISP> */
119 InterlockedExchange ((gint32
*)(orig_code
+ 2), (guint
)addr
- ((guint
)orig_code
+ 1) - 5);
121 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code
[0], code
[1], code
[2], code
[3],
122 code
[4], code
[5], code
[6]);
123 g_assert_not_reached ();
128 mono_arch_patch_plt_entry (guint8
*code
, gpointer
*got
, mgreg_t
*regs
, guint8
*addr
)
132 /* Patch the jump table entry used by the plt entry */
134 /* A PLT entry: jmp *<DISP>(%ebx) */
135 g_assert (code
[0] == 0xff);
136 g_assert (code
[1] == 0xa3);
138 offset
= *(guint32
*)(code
+ 2);
140 got
= (gpointer
*)(gsize
) regs
[MONO_ARCH_GOT_REG
];
141 *(guint8
**)((guint8
*)got
+ offset
) = addr
;
145 get_vcall_slot (guint8
*code
, mgreg_t
*regs
, int *displacement
)
147 const int kBufSize
= 8;
152 mono_breakpoint_clean_code (NULL
, code
, kBufSize
, buf
, kBufSize
);
157 if ((code
[0] == 0xff) && ((code
[1] & 0x18) == 0x10) && ((code
[1] >> 6) == 2)) {
158 reg
= code
[1] & 0x07;
159 disp
= *((gint32
*)(code
+ 2));
161 g_assert_not_reached ();
165 *displacement
= disp
;
166 return (gpointer
)regs
[reg
];
170 get_vcall_slot_addr (guint8
* code
, mgreg_t
*regs
)
174 vt
= get_vcall_slot (code
, regs
, &displacement
);
177 return (gpointer
*)((char*)vt
+ displacement
);
181 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type
, MonoTrampInfo
**info
, gboolean aot
)
184 guint8
*buf
, *code
, *tramp
, *br_ex_check
;
185 GSList
*unwind_ops
= NULL
;
186 MonoJumpInfo
*ji
= NULL
;
187 int i
, offset
, frame_size
, regarray_offset
, lmf_offset
, caller_ip_offset
, arg_offset
;
188 int cfa_offset
; /* cfa = cfa_reg + cfa_offset */
190 code
= buf
= mono_global_codeman_reserve (256);
192 /* Note that there is a single argument to the trampoline
193 * and it is stored at: esp + pushed_args * sizeof (gpointer)
194 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
197 /* Compute frame offsets relative to the frame pointer %ebp */
198 arg_offset
= sizeof (mgreg_t
);
199 caller_ip_offset
= 2 * sizeof (mgreg_t
);
201 offset
+= sizeof (MonoLMF
);
202 lmf_offset
= -offset
;
203 offset
+= X86_NREG
* sizeof (mgreg_t
);
204 regarray_offset
= -offset
;
206 offset
+= 4 * sizeof (mgreg_t
);
207 frame_size
= ALIGN_TO (offset
, MONO_ARCH_FRAME_ALIGNMENT
);
209 /* ret addr and arg are on the stack */
210 cfa_offset
= 2 * sizeof (mgreg_t
);
211 mono_add_unwind_op_def_cfa (unwind_ops
, code
, buf
, X86_ESP
, cfa_offset
);
212 // IP saved at CFA - 4
213 mono_add_unwind_op_offset (unwind_ops
, code
, buf
, X86_NREG
, -4);
216 x86_push_reg (code
, X86_EBP
);
217 cfa_offset
+= sizeof (mgreg_t
);
218 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
219 mono_add_unwind_op_offset (unwind_ops
, code
, buf
, X86_EBP
, -cfa_offset
);
221 x86_mov_reg_reg (code
, X86_EBP
, X86_ESP
, sizeof (mgreg_t
));
222 mono_add_unwind_op_def_cfa_reg (unwind_ops
, code
, buf
, X86_EBP
);
224 /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
225 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, frame_size
+ sizeof (mgreg_t
));
227 /* Save all registers */
228 for (i
= X86_EAX
; i
<= X86_EDI
; ++i
) {
232 /* Save original ebp */
233 /* EAX is already saved */
234 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, 0, sizeof (mgreg_t
));
236 } else if (i
== X86_ESP
) {
237 /* Save original esp */
238 /* EAX is already saved */
239 x86_mov_reg_reg (code
, X86_EAX
, X86_EBP
, sizeof (mgreg_t
));
240 /* Saved ebp + trampoline arg + return addr */
241 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, 3 * sizeof (mgreg_t
));
244 x86_mov_membase_reg (code
, X86_EBP
, regarray_offset
+ (i
* sizeof (mgreg_t
)), reg
, sizeof (mgreg_t
));
249 if (tramp_type
== MONO_TRAMPOLINE_JUMP
) {
250 x86_mov_membase_imm (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, eip
), 0, sizeof (mgreg_t
));
252 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, caller_ip_offset
, sizeof (mgreg_t
));
253 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, eip
), X86_EAX
, sizeof (mgreg_t
));
256 if ((tramp_type
== MONO_TRAMPOLINE_JIT
) || (tramp_type
== MONO_TRAMPOLINE_JUMP
)) {
257 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, arg_offset
, sizeof (mgreg_t
));
258 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, method
), X86_EAX
, sizeof (mgreg_t
));
260 x86_mov_membase_imm (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, method
), 0, sizeof (mgreg_t
));
263 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
+ (X86_ESP
* sizeof (mgreg_t
)), sizeof (mgreg_t
));
264 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, esp
), X86_EAX
, sizeof (mgreg_t
));
265 /* callee save registers */
266 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
+ (X86_EBX
* sizeof (mgreg_t
)), sizeof (mgreg_t
));
267 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, ebx
), X86_EAX
, sizeof (mgreg_t
));
268 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
+ (X86_EDI
* sizeof (mgreg_t
)), sizeof (mgreg_t
));
269 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, edi
), X86_EAX
, sizeof (mgreg_t
));
270 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
+ (X86_ESI
* sizeof (mgreg_t
)), sizeof (mgreg_t
));
271 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, esi
), X86_EAX
, sizeof (mgreg_t
));
272 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
+ (X86_EBP
* sizeof (mgreg_t
)), sizeof (mgreg_t
));
273 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, ebp
), X86_EAX
, sizeof (mgreg_t
));
276 /* get the address of lmf for the current thread */
278 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, "mono_get_lmf_addr");
279 x86_call_reg (code
, X86_EAX
);
281 x86_call_code (code
, mono_get_lmf_addr
);
283 /* lmf->lmf_addr = lmf_addr (%eax) */
284 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, lmf_addr
), X86_EAX
, sizeof (mgreg_t
));
285 /* lmf->previous_lmf = *(lmf_addr) */
286 x86_mov_reg_membase (code
, X86_ECX
, X86_EAX
, 0, sizeof (mgreg_t
));
287 /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
288 x86_alu_reg_imm (code
, X86_ADD
, X86_ECX
, 1);
289 x86_mov_membase_reg (code
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, previous_lmf
), X86_ECX
, sizeof (mgreg_t
));
290 /* *lmf_addr = lmf */
291 x86_lea_membase (code
, X86_ECX
, X86_EBP
, lmf_offset
);
292 x86_mov_membase_reg (code
, X86_EAX
, 0, X86_ECX
, sizeof (mgreg_t
));
294 /* Call trampoline function */
295 /* Arg 1 - registers */
296 x86_lea_membase (code
, X86_EAX
, X86_EBP
, regarray_offset
);
297 x86_mov_membase_reg (code
, X86_ESP
, (0 * sizeof (mgreg_t
)), X86_EAX
, sizeof (mgreg_t
));
298 /* Arg2 - calling code */
299 if (tramp_type
== MONO_TRAMPOLINE_JUMP
) {
300 x86_mov_membase_imm (code
, X86_ESP
, (1 * sizeof (mgreg_t
)), 0, sizeof (mgreg_t
));
302 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, caller_ip_offset
, sizeof (mgreg_t
));
303 x86_mov_membase_reg (code
, X86_ESP
, (1 * sizeof (mgreg_t
)), X86_EAX
, sizeof (mgreg_t
));
305 /* Arg3 - trampoline argument */
306 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, arg_offset
, sizeof (mgreg_t
));
307 x86_mov_membase_reg (code
, X86_ESP
, (2 * sizeof (mgreg_t
)), X86_EAX
, sizeof (mgreg_t
));
308 /* Arg4 - trampoline address */
310 x86_mov_membase_imm (code
, X86_ESP
, (3 * sizeof (mgreg_t
)), 0, sizeof (mgreg_t
));
313 /* check the stack is aligned after the ret ip is pushed */
315 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
316 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
317 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
318 x86_branch_disp (code, X86_CC_Z, 3, FALSE);
319 x86_breakpoint (code);
324 char *icall_name
= g_strdup_printf ("trampoline_func_%d", tramp_type
);
325 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, icall_name
);
326 x86_call_reg (code
, X86_EAX
);
328 tramp
= (guint8
*)mono_get_trampoline_func (tramp_type
);
329 x86_call_code (code
, tramp
);
333 * Overwrite the trampoline argument with the address we need to jump to,
336 x86_mov_membase_reg (code
, X86_EBP
, arg_offset
, X86_EAX
, 4);
339 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, lmf_addr
), sizeof (mgreg_t
));
340 x86_mov_reg_membase (code
, X86_ECX
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, previous_lmf
), sizeof (mgreg_t
));
341 x86_alu_reg_imm (code
, X86_SUB
, X86_ECX
, 1);
342 x86_mov_membase_reg (code
, X86_EAX
, 0, X86_ECX
, sizeof (mgreg_t
));
344 /* Check for interruptions */
346 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, "mono_thread_force_interruption_checkpoint_noraise");
347 x86_call_reg (code
, X86_EAX
);
349 x86_call_code (code
, (guint8
*)mono_thread_force_interruption_checkpoint_noraise
);
352 x86_test_reg_reg (code
, X86_EAX
, X86_EAX
);
354 x86_branch8 (code
, X86_CC_Z
, -1, 1);
358 * We have an exception we want to throw in the caller's frame, so pop
359 * the trampoline frame and throw from the caller.
363 * The exception is in eax.
364 * We are calling the throw trampoline used by OP_THROW, so we have to setup the
365 * stack to look the same.
366 * The stack contains the ret addr, and the trampoline argument, the throw trampoline
367 * expects it to contain the ret addr and the exception. It also needs to be aligned
368 * after the exception is pushed.
371 x86_push_reg (code
, X86_EAX
);
372 /* Push the exception */
373 x86_push_reg (code
, X86_EAX
);
374 //x86_breakpoint (code);
375 /* Push the original return value */
376 x86_push_membase (code
, X86_ESP
, 3 * 4);
378 * EH is initialized after trampolines, so get the address of the variable
379 * which contains throw_exception, and load it from there.
382 /* Not really a jit icall */
383 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, "throw_exception_addr");
385 x86_mov_reg_imm (code
, X86_ECX
, (guint8
*)mono_get_throw_exception_addr ());
387 x86_mov_reg_membase (code
, X86_ECX
, X86_ECX
, 0, sizeof(gpointer
));
388 x86_jump_reg (code
, X86_ECX
);
391 mono_x86_patch (br_ex_check
, code
);
393 /* Restore registers */
394 for (i
= X86_EAX
; i
<= X86_EDI
; ++i
) {
395 if (i
== X86_ESP
|| i
== X86_EBP
)
397 if (i
== X86_EAX
&& !((tramp_type
== MONO_TRAMPOLINE_RESTORE_STACK_PROT
) || (tramp_type
== MONO_TRAMPOLINE_AOT_PLT
)))
399 x86_mov_reg_membase (code
, i
, X86_EBP
, regarray_offset
+ (i
* 4), 4);
404 cfa_offset
-= sizeof (mgreg_t
);
405 mono_add_unwind_op_def_cfa (unwind_ops
, code
, buf
, X86_ESP
, cfa_offset
);
406 mono_add_unwind_op_same_value (unwind_ops
, code
, buf
, X86_EBP
);
408 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type
)) {
409 /* Load the value returned by the trampoline */
410 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, 0, 4);
411 /* The trampoline returns normally, pop the trampoline argument */
412 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
413 cfa_offset
-= sizeof (mgreg_t
);
414 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
417 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
418 if (tramp_type
== MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD
) {
419 x86_pop_reg (code
, X86_EAX
);
420 cfa_offset
-= sizeof (mgreg_t
);
421 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
422 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 0x8);
423 x86_jump_reg (code
, X86_EAX
);
429 g_assert ((code
- buf
) <= 256);
430 mono_profiler_code_buffer_new (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_HELPER
, NULL
);
432 tramp_name
= mono_get_generic_trampoline_name (tramp_type
);
433 *info
= mono_tramp_info_create (tramp_name
, buf
, code
- buf
, ji
, unwind_ops
);
439 #define TRAMPOLINE_SIZE 10
442 mono_arch_create_specific_trampoline (gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
444 guint8
*code
, *buf
, *tramp
;
446 tramp
= mono_get_trampoline_code (tramp_type
);
448 code
= buf
= mono_domain_code_reserve_align (domain
, TRAMPOLINE_SIZE
, 4);
450 x86_push_imm (buf
, arg1
);
451 x86_jump_code (buf
, tramp
);
452 g_assert ((buf
- code
) <= TRAMPOLINE_SIZE
);
454 mono_arch_flush_icache (code
, buf
- code
);
455 mono_profiler_code_buffer_new (code
, buf
- code
, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
, mono_get_generic_trampoline_simple_name (tramp_type
));
458 *code_len
= buf
- code
;
464 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot
, MonoTrampInfo
**info
, gboolean aot
)
468 guint8
**rgctx_null_jumps
;
473 MonoJumpInfo
*ji
= NULL
;
474 GSList
*unwind_ops
= NULL
;
476 unwind_ops
= mono_arch_get_cie_program ();
478 mrgctx
= MONO_RGCTX_SLOT_IS_MRGCTX (slot
);
479 index
= MONO_RGCTX_SLOT_INDEX (slot
);
481 index
+= MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT
/ sizeof (gpointer
);
482 for (depth
= 0; ; ++depth
) {
483 int size
= mono_class_rgctx_get_array_size (depth
, mrgctx
);
485 if (index
< size
- 1)
490 tramp_size
= (aot
? 64 : 36) + 6 * depth
;
492 code
= buf
= mono_global_codeman_reserve (tramp_size
);
494 rgctx_null_jumps
= g_malloc (sizeof (guint8
*) * (depth
+ 2));
496 /* load vtable/mrgctx ptr */
497 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, 4, 4);
499 /* load rgctx ptr from vtable */
500 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoVTable
, runtime_generic_context
), 4);
501 /* is the rgctx ptr null? */
502 x86_test_reg_reg (code
, X86_EAX
, X86_EAX
);
503 /* if yes, jump to actual trampoline */
504 rgctx_null_jumps
[0] = code
;
505 x86_branch8 (code
, X86_CC_Z
, -1, 1);
508 for (i
= 0; i
< depth
; ++i
) {
509 /* load ptr to next array */
510 if (mrgctx
&& i
== 0)
511 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT
, 4);
513 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, 0, 4);
514 /* is the ptr null? */
515 x86_test_reg_reg (code
, X86_EAX
, X86_EAX
);
516 /* if yes, jump to actual trampoline */
517 rgctx_null_jumps
[i
+ 1] = code
;
518 x86_branch8 (code
, X86_CC_Z
, -1, 1);
522 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (gpointer
) * (index
+ 1), 4);
523 /* is the slot null? */
524 x86_test_reg_reg (code
, X86_EAX
, X86_EAX
);
525 /* if yes, jump to actual trampoline */
526 rgctx_null_jumps
[depth
+ 1] = code
;
527 x86_branch8 (code
, X86_CC_Z
, -1, 1);
528 /* otherwise return */
531 for (i
= mrgctx
? 1 : 0; i
<= depth
+ 1; ++i
)
532 x86_patch (rgctx_null_jumps
[i
], code
);
534 g_free (rgctx_null_jumps
);
536 x86_mov_reg_membase (code
, MONO_ARCH_VTABLE_REG
, X86_ESP
, 4, 4);
539 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot
));
540 x86_jump_reg (code
, X86_EAX
);
542 tramp
= mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot
), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mono_get_root_domain (), NULL
);
544 /* jump to the actual trampoline */
545 x86_jump_code (code
, tramp
);
548 mono_arch_flush_icache (buf
, code
- buf
);
549 mono_profiler_code_buffer_new (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
);
551 g_assert (code
- buf
<= tramp_size
);
553 char *name
= mono_get_rgctx_fetch_trampoline_name (slot
);
554 *info
= mono_tramp_info_create (name
, buf
, code
- buf
, ji
, unwind_ops
);
561 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
563 * 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
564 * the trampoline to call if the slot is not filled.
567 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo
**info
, gboolean aot
)
571 MonoJumpInfo
*ji
= NULL
;
572 GSList
*unwind_ops
= NULL
;
576 unwind_ops
= mono_arch_get_cie_program ();
580 code
= buf
= mono_global_codeman_reserve (tramp_size
);
582 // FIXME: Currently, we always go to the slow path.
584 /* Load trampoline addr */
585 x86_mov_reg_membase (code
, X86_EAX
, MONO_ARCH_RGCTX_REG
, 4, 4);
586 /* Load mrgctx/vtable */
587 x86_mov_reg_membase (code
, MONO_ARCH_VTABLE_REG
, X86_ESP
, 4, 4);
589 x86_jump_reg (code
, X86_EAX
);
591 mono_arch_flush_icache (buf
, code
- buf
);
592 mono_profiler_code_buffer_new (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
);
594 g_assert (code
- buf
<= tramp_size
);
596 *info
= mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf
, code
- buf
, ji
, unwind_ops
);
602 mono_arch_invalidate_method (MonoJitInfo
*ji
, void *func
, gpointer func_arg
)
604 /* FIXME: This is not thread safe */
605 guint8
*code
= ji
->code_start
;
607 x86_push_imm (code
, func_arg
);
608 x86_call_code (code
, (guint8
*)func
);
612 handler_block_trampoline_helper (void)
614 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
615 return jit_tls
->handler_block_return_address
;
619 mono_arch_create_handler_block_trampoline (MonoTrampInfo
**info
, gboolean aot
)
621 guint8
*tramp
= mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD
);
624 MonoJumpInfo
*ji
= NULL
;
626 GSList
*unwind_ops
= NULL
;
630 code
= buf
= mono_global_codeman_reserve (tramp_size
);
632 unwind_ops
= mono_arch_get_cie_program ();
633 cfa_offset
= sizeof (mgreg_t
);
635 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
639 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
642 /*Slow path uses a c helper*/
643 x86_call_code (code
, handler_block_trampoline_helper
);
644 /* Simulate a call */
645 /*Fix stack alignment*/
646 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 0x4);
647 cfa_offset
+= sizeof (mgreg_t
);
648 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
650 /* This is the address the trampoline will return to */
651 x86_push_reg (code
, X86_EAX
);
652 cfa_offset
+= sizeof (mgreg_t
);
653 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
655 /* Dummy trampoline argument, since we call the generic trampoline directly */
656 x86_push_imm (code
, 0);
657 cfa_offset
+= sizeof (mgreg_t
);
658 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
659 x86_jump_code (code
, tramp
);
661 mono_arch_flush_icache (buf
, code
- buf
);
662 mono_profiler_code_buffer_new (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_HELPER
, NULL
);
663 g_assert (code
- buf
<= tramp_size
);
665 *info
= mono_tramp_info_create ("handler_block_trampoline", buf
, code
- buf
, ji
, unwind_ops
);
671 mono_arch_get_call_target (guint8
*code
)
673 if (code
[-5] == 0xe8) {
674 gint32 disp
= *(gint32
*)(code
- 4);
675 guint8
*target
= code
+ disp
;
684 mono_arch_get_plt_info_offset (guint8
*plt_entry
, mgreg_t
*regs
, guint8
*code
)
686 return *(guint32
*)(plt_entry
+ 6);
690 * mono_arch_get_gsharedvt_arg_trampoline:
692 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
695 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain
*domain
, gpointer arg
, gpointer addr
)
697 guint8
*code
, *start
;
704 start
= code
= mono_domain_code_reserve (domain
, buf_len
);
706 unwind_ops
= mono_arch_get_cie_program ();
708 x86_mov_reg_imm (code
, X86_EAX
, arg
);
709 x86_jump_code (code
, addr
);
710 g_assert ((code
- start
) <= buf_len
);
712 mono_arch_flush_icache (start
, code
- start
);
713 mono_profiler_code_buffer_new (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
);
715 mono_tramp_info_register (mono_tramp_info_create (NULL
, start
, code
- start
, NULL
, unwind_ops
), domain
);
721 * mono_arch_create_sdb_trampoline:
723 * Return a trampoline which captures the current context, passes it to
724 * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
725 * then restores the (potentially changed) context.
728 mono_arch_create_sdb_trampoline (gboolean single_step
, MonoTrampInfo
**info
, gboolean aot
)
730 int tramp_size
= 256;
731 int framesize
, ctx_offset
, cfa_offset
;
733 GSList
*unwind_ops
= NULL
;
734 MonoJumpInfo
*ji
= NULL
;
736 code
= buf
= mono_global_codeman_reserve (tramp_size
);
741 framesize
+= sizeof (mgreg_t
);
743 framesize
= ALIGN_TO (framesize
, 8);
744 ctx_offset
= framesize
;
745 framesize
+= sizeof (MonoContext
);
747 framesize
= ALIGN_TO (framesize
, MONO_ARCH_FRAME_ALIGNMENT
);
751 mono_add_unwind_op_def_cfa (unwind_ops
, code
, buf
, X86_ESP
, 4);
752 // IP saved at CFA - 4
753 mono_add_unwind_op_offset (unwind_ops
, code
, buf
, X86_NREG
, -cfa_offset
);
755 x86_push_reg (code
, X86_EBP
);
756 cfa_offset
+= sizeof(mgreg_t
);
757 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, buf
, cfa_offset
);
758 mono_add_unwind_op_offset (unwind_ops
, code
, buf
, X86_EBP
, - cfa_offset
);
760 x86_mov_reg_reg (code
, X86_EBP
, X86_ESP
, sizeof(mgreg_t
));
761 mono_add_unwind_op_def_cfa_reg (unwind_ops
, code
, buf
, X86_EBP
);
762 /* The + 8 makes the stack aligned */
763 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, framesize
+ 8);
765 /* Initialize a MonoContext structure on the stack */
766 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, eax
), X86_EAX
, sizeof (mgreg_t
));
767 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ebx
), X86_EBX
, sizeof (mgreg_t
));
768 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ecx
), X86_ECX
, sizeof (mgreg_t
));
769 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, edx
), X86_EDX
, sizeof (mgreg_t
));
770 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, 0, sizeof (mgreg_t
));
771 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ebp
), X86_EAX
, sizeof (mgreg_t
));
772 x86_mov_reg_reg (code
, X86_EAX
, X86_EBP
, sizeof (mgreg_t
));
773 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, cfa_offset
);
774 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, esp
), X86_ESP
, sizeof (mgreg_t
));
775 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, esi
), X86_ESI
, sizeof (mgreg_t
));
776 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, edi
), X86_EDI
, sizeof (mgreg_t
));
777 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, 4, sizeof (mgreg_t
));
778 x86_mov_membase_reg (code
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, eip
), X86_EAX
, sizeof (mgreg_t
));
780 /* Call the single step/breakpoint function in sdb */
781 x86_lea_membase (code
, X86_EAX
, X86_ESP
, ctx_offset
);
782 x86_mov_membase_reg (code
, X86_ESP
, 0, X86_EAX
, sizeof (mgreg_t
));
785 x86_breakpoint (code
);
788 x86_call_code (code
, debugger_agent_single_step_from_context
);
790 x86_call_code (code
, debugger_agent_breakpoint_from_context
);
793 /* Restore registers from ctx */
794 /* Overwrite the saved ebp */
795 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ebp
), sizeof (mgreg_t
));
796 x86_mov_membase_reg (code
, X86_EBP
, 0, X86_EAX
, sizeof (mgreg_t
));
797 /* Overwrite saved eip */
798 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, eip
), sizeof (mgreg_t
));
799 x86_mov_membase_reg (code
, X86_EBP
, 4, X86_EAX
, sizeof (mgreg_t
));
800 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, eax
), sizeof (mgreg_t
));
801 x86_mov_reg_membase (code
, X86_EBX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ebx
), sizeof (mgreg_t
));
802 x86_mov_reg_membase (code
, X86_ECX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, ecx
), sizeof (mgreg_t
));
803 x86_mov_reg_membase (code
, X86_EDX
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, edx
), sizeof (mgreg_t
));
804 x86_mov_reg_membase (code
, X86_ESI
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, esi
), sizeof (mgreg_t
));
805 x86_mov_reg_membase (code
, X86_EDI
, X86_ESP
, ctx_offset
+ G_STRUCT_OFFSET (MonoContext
, edi
), sizeof (mgreg_t
));
808 cfa_offset
-= sizeof (mgreg_t
);
809 mono_add_unwind_op_def_cfa (unwind_ops
, code
, buf
, X86_ESP
, cfa_offset
);
812 mono_arch_flush_icache (code
, code
- buf
);
813 g_assert (code
- buf
<= tramp_size
);
815 const char *tramp_name
= single_step
? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
816 *info
= mono_tramp_info_create (tramp_name
, buf
, code
- buf
, ji
, unwind_ops
);