3 * Function - JIT trampoline code for S/390.
5 * Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com)
9 * Derivation - From exceptions-x86 & exceptions-ppc
10 * Paolo Molaro (lupus@ximian.com)
11 * Dietmar Maurer (dietmar@ximian.com)
13 * Copyright - 2001 Ximian, Inc.
14 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 /*------------------------------------------------------------------*/
20 /*------------------------------------------------------------------*/
22 #define LMFReg s390_r13
25 * Method-specific trampoline code fragment sizes
27 #define SPECIFIC_TRAMPOLINE_SIZE 96
29 /*========================= End of Defines =========================*/
31 /*------------------------------------------------------------------*/
33 /*------------------------------------------------------------------*/
39 #include <mono/metadata/abi-details.h>
40 #include <mono/metadata/appdomain.h>
41 #include <mono/metadata/gc-internals.h>
42 #include <mono/metadata/marshal.h>
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/arch/s390x/s390x-codegen.h>
48 #include "mini-s390x.h"
49 #include "support-s390x.h"
50 #include "jit-icalls.h"
52 /*========================= End of Includes ========================*/
54 /*------------------------------------------------------------------*/
56 /*------------------------------------------------------------------*/
59 guint8 stk[S390_MINIMAL_STACK_SIZE]; /* Standard s390x stack */
60 guint64 saveFn; /* Call address */
61 struct MonoLMF LMF; /* LMF */
64 /*========================= End of Typedefs ========================*/
66 /*------------------------------------------------------------------*/
67 /* P r o t o t y p e s */
68 /*------------------------------------------------------------------*/
70 /*========================= End of Prototypes ======================*/
72 /*------------------------------------------------------------------*/
73 /* G l o b a l V a r i a b l e s */
74 /*------------------------------------------------------------------*/
77 /*====================== End of Global Variables ===================*/
79 /*------------------------------------------------------------------*/
81 /* Name - mono_arch_get_unbox_trampoline */
83 /* Function - Return a pointer to a trampoline which does the */
84 /* unboxing before calling the method. */
86 /* When value type methods are called through the */
87 /* vtable we need to unbox the 'this' argument. */
89 /* Parameters - method - Methd pointer */
90 /* addr - Pointer to native code for method */
92 /*------------------------------------------------------------------*/
95 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
98 int this_pos = s390_r2;
99 MonoDomain *domain = mono_domain_get ();
102 start = code = mono_domain_code_reserve (domain, 28);
104 S390_SET (code, s390_r1, addr);
105 s390_aghi (code, this_pos, sizeof(MonoObject));
106 s390_br (code, s390_r1);
108 g_assert ((code - start) <= 28);
110 mono_arch_flush_icache (start, code - start);
111 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
113 snprintf(trampName, sizeof(trampName), "%s_unbox_trampoline", method->name);
115 mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
120 /*========================= End of Function ========================*/
122 /*------------------------------------------------------------------*/
124 /* Name - mono_arch_patch_callsite */
126 /* Function - Patch a non-virtual callsite so it calls @addr. */
128 /*------------------------------------------------------------------*/
131 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
134 unsigned short opcode;
136 opcode = *((unsigned short *) (orig_code - 2));
137 if (opcode == 0x0dee) {
138 /* This should be a 'iihf/iilf' sequence */
139 S390_EMIT_CALL((orig_code - 14), addr);
140 mono_arch_flush_icache (orig_code - 14, 12);
142 /* This is the 'brasl' instruction */
144 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
145 s390_patch_rel (orig_code, displace);
146 mono_arch_flush_icache (orig_code, 4);
150 /*========================= End of Function ========================*/
152 /*------------------------------------------------------------------*/
154 /* Name - mono_arch_patch_plt_entry. */
156 /* Function - Patch a PLT entry - unused as yet. */
158 /*------------------------------------------------------------------*/
161 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
163 g_assert_not_reached ();
166 /*========================= End of Function ========================*/
168 /*------------------------------------------------------------------*/
170 /* Name - mono_arch_create_trampoline_code */
172 /* Function - Create the designated type of trampoline according*/
173 /* to the 'tramp_type' parameter. */
175 /*------------------------------------------------------------------*/
178 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
181 guint8 *buf, *tramp, *code;
182 int i, offset, has_caller;
183 GSList *unwind_ops = NULL;
184 MonoJumpInfo *ji = NULL;
188 /* Now we'll create in 'buf' the S/390 trampoline code. This
189 is the trampoline code common to all methods */
191 code = buf = mono_global_codeman_reserve(512);
193 if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
194 (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD))
199 /*-----------------------------------------------------------
200 STEP 0: First create a non-standard function prologue with a
201 stack size big enough to save our registers.
202 -----------------------------------------------------------*/
204 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
205 s390_lgr (buf, s390_r11, s390_r15);
206 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
207 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
209 /*---------------------------------------------------------------*/
210 /* we build the MonoLMF structure on the stack - see mini-s390.h */
211 /* Keep in sync with the code in mono_arch_emit_prolog */
212 /*---------------------------------------------------------------*/
214 s390_lgr (buf, LMFReg, STK_BASE);
215 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
217 /*---------------------------------------------------------------*/
218 /* Save general and floating point registers in LMF */
219 /*---------------------------------------------------------------*/
220 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
221 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
222 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
223 s390_r11, S390_REG_SAVE_OFFSET);
225 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
226 for (i = s390_f0; i <= s390_f15; ++i) {
227 s390_std (buf, i, 0, LMFReg, offset);
228 offset += sizeof(gdouble);
231 /*----------------------------------------------------------
232 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
233 LMF. We'll need to restore it after the call to
234 's390_magic_trampoline' and before the call to the native
236 ----------------------------------------------------------*/
238 S390_SET (buf, s390_r1, mono_get_lmf_addr);
239 s390_basr (buf, s390_r14, s390_r1);
241 /*---------------------------------------------------------------*/
242 /* Set lmf.lmf_addr = jit_tls->lmf */
243 /*---------------------------------------------------------------*/
244 s390_stg (buf, s390_r2, 0, LMFReg,
245 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
247 /*---------------------------------------------------------------*/
248 /* Get current lmf */
249 /*---------------------------------------------------------------*/
250 s390_lg (buf, s390_r0, 0, s390_r2, 0);
252 /*---------------------------------------------------------------*/
253 /* Set our lmf as the current lmf */
254 /*---------------------------------------------------------------*/
255 s390_stg (buf, LMFReg, 0, s390_r2, 0);
257 /*---------------------------------------------------------------*/
258 /* Have our lmf.previous_lmf point to the last lmf */
259 /*---------------------------------------------------------------*/
260 s390_stg (buf, s390_r0, 0, LMFReg,
261 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
263 /*---------------------------------------------------------------*/
264 /* save method info */
265 /*---------------------------------------------------------------*/
266 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
267 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
269 /*---------------------------------------------------------------*/
270 /* save the current SP */
271 /*---------------------------------------------------------------*/
272 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
273 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
275 /*---------------------------------------------------------------*/
276 /* save the current IP */
277 /*---------------------------------------------------------------*/
279 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
281 s390_lghi (buf, s390_r1, 0);
283 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
285 /*---------------------------------------------------------------*/
286 /* STEP 2: call the C trampoline function */
287 /*---------------------------------------------------------------*/
291 /* Arg 1: mgreg_t *regs */
292 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
294 /* Arg 2: code (next address to the instruction that called us) */
296 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
298 s390_lghi (buf, s390_r3, 0);
301 /* Arg 3: Trampoline argument */
302 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
304 /* Arg 4: trampoline address. */
305 S390_SET (buf, s390_r5, buf);
307 /* Calculate call address and call the C trampoline. Return value will be in r2 */
308 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
309 S390_SET (buf, s390_r1, tramp);
310 s390_basr (buf, s390_r14, s390_r1);
312 /* OK, code address is now on r2. Save it, so that we
313 can restore r2 and use it later */
314 s390_stg (buf, s390_r2, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
316 /* Check for thread interruption */
317 S390_SET (buf, s390_r1, (guint8 *)mono_interruption_checkpoint_from_trampoline);
318 s390_basr (buf, s390_r14, s390_r1);
320 /*----------------------------------------------------------
321 STEP 3: Restore the LMF
322 ----------------------------------------------------------*/
323 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
326 s390_lg (buf, s390_r1, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
328 /*----------------------------------------------------------
329 STEP 4: call the compiled method
330 ----------------------------------------------------------*/
332 /* Restore parameter registers */
333 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
335 /* Restore the FP registers */
336 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
337 for (i = s390_f0; i <= s390_f15; ++i) {
338 s390_ld (buf, i, 0, LMFReg, offset);
339 offset += sizeof(gdouble);
342 /* Restore stack pointer and jump to the code -
343 * R14 contains the return address to our caller
345 s390_lgr (buf, STK_BASE, s390_r11);
346 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
348 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
349 s390_lgr (buf, s390_r2, s390_r1);
350 s390_br (buf, s390_r14);
352 s390_br (buf, s390_r1);
355 /* Flush instruction cache, since we've generated code */
356 mono_arch_flush_icache (code, buf - code);
357 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
360 tramp_name = mono_get_generic_trampoline_name (tramp_type);
361 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
365 g_assert ((buf - code) <= 512);
370 /*========================= End of Function ========================*/
372 /*------------------------------------------------------------------*/
374 /* Name - mono_arch_invalidate_method */
378 /*------------------------------------------------------------------*/
381 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
383 /* FIXME: This is not thread safe */
384 guint8 *code = ji->code_start;
386 S390_SET (code, s390_r1, func);
387 S390_SET (code, s390_r2, func_arg);
388 s390_br (code, s390_r1);
392 /*========================= End of Function ========================*/
394 /*------------------------------------------------------------------*/
396 /* Name - mono_arch_create_specific_trampoline */
398 /* Function - Creates the given kind of specific trampoline */
400 /*------------------------------------------------------------------*/
403 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
405 guint8 *code, *buf, *tramp;
408 tramp = mono_get_trampoline_code (tramp_type);
410 /*----------------------------------------------------------*/
411 /* This is the method-specific part of the trampoline. Its */
412 /* purpose is to provide the generic part with the */
413 /* MonoMethod *method pointer. We'll use r1 to keep it. */
414 /*----------------------------------------------------------*/
415 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
417 S390_SET (buf, s390_r1, arg1);
418 displace = (tramp - buf) / 2;
419 s390_jg (buf, displace);
421 /* Flush instruction cache, since we've generated code */
422 mono_arch_flush_icache (code, buf - code);
423 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
424 (void *) mono_get_generic_trampoline_simple_name (tramp_type));
427 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
430 *code_len = buf - code;
435 /*========================= End of Function ========================*/
437 /*------------------------------------------------------------------*/
439 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
443 /*------------------------------------------------------------------*/
446 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
450 guint8 **rgctx_null_jumps;
458 MonoJumpInfo *ji = NULL;
459 GSList *unwind_ops = NULL;
461 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
462 index = MONO_RGCTX_SLOT_INDEX (slot);
464 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
465 for (depth = 0; ; ++depth) {
466 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
468 if (index < size - 1)
473 tramp_size = 48 + 16 * depth;
479 code = buf = mono_global_codeman_reserve (tramp_size);
481 unwind_ops = mono_arch_get_cie_program ();
483 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
487 s390_lgr (code, s390_r1, s390_r2);
489 /* load rgctx ptr from vtable */
490 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
491 /* is the rgctx ptr null? */
492 s390_ltgr (code, s390_r1, s390_r1);
493 /* if yes, jump to actual trampoline */
494 rgctx_null_jumps [iPatch++] = code;
498 for (i = 0; i < depth; ++i) {
499 /* load ptr to next array */
500 if (mrgctx && i == 0)
501 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
503 s390_lg (code, s390_r1, 0, s390_r1, 0);
504 s390_ltgr (code, s390_r1, s390_r1);
505 /* if the ptr is null then jump to actual trampoline */
506 rgctx_null_jumps [iPatch++] = code;
511 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
512 /* is the slot null? */
513 s390_ltgr (code, s390_r1, s390_r1);
514 /* if yes, jump to actual trampoline */
515 rgctx_null_jumps [iPatch++] = code;
517 /* otherwise return r1 */
518 s390_lgr (code, s390_r2, s390_r1);
519 s390_br (code, s390_r14);
521 for (i = 0; i < iPatch; i++) {
522 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
523 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
526 g_free (rgctx_null_jumps);
528 /* move the rgctx pointer to the VTABLE register */
529 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
531 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
532 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
534 /* jump to the actual trampoline */
535 displace = (tramp - code) / 2;
536 s390_jg (code, displace);
538 mono_arch_flush_icache (buf, code - buf);
539 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
541 g_assert (code - buf <= tramp_size);
543 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
544 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
550 /*========================= End of Function ========================*/
552 /*------------------------------------------------------------------*/
554 /* Name - mono_arch_get_static_rgctx_trampoline */
556 /* Function - Create a trampoline which sets RGCTX_REG to ARG */
557 /* then jumps to ADDR. */
559 /*------------------------------------------------------------------*/
562 mono_arch_get_static_rgctx_trampoline (gpointer arg,
565 guint8 *code, *start;
569 MonoDomain *domain = mono_domain_get ();
573 start = code = mono_domain_code_reserve (domain, buf_len);
575 S390_SET (code, MONO_ARCH_RGCTX_REG, arg);
576 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
577 s390_jg (code, displace);
578 g_assert ((code - start) < buf_len);
580 mono_arch_flush_icache (start, code - start);
581 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
583 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL), domain);
588 /*========================= End of Function ========================*/
590 /*------------------------------------------------------------------*/
592 /* Name - handler_block_trampoline_helper */
596 /*------------------------------------------------------------------*/
599 handler_block_trampoline_helper (gpointer *ptr)
601 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
602 *ptr = jit_tls->handler_block_return_address;
605 /*========================= End of Function ========================*/
607 /*------------------------------------------------------------------*/
609 /* Name - mono_arch_create_handler_block_trampoline */
613 /*------------------------------------------------------------------*/
616 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
618 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
621 MonoJumpInfo *ji = NULL;
622 GSList *unwind_ops = NULL;
626 code = buf = mono_global_codeman_reserve (tramp_size);
629 * This trampoline restore the call chain of the handler block
630 * then jumps into the code that deals with it.
634 * Slow path uses a C helper
636 S390_SET (code, s390_r2, tramp);
637 S390_SET (code, s390_r1, handler_block_trampoline_helper);
638 s390_br (code, s390_r1);
640 mono_arch_flush_icache (buf, code - buf);
641 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
642 g_assert (code - buf <= tramp_size);
644 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
649 /*========================= End of Function ========================*/