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_RAISE (jit_code_buffer, (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)
198 /*-----------------------------------------------------------
199 STEP 0: First create a non-standard function prologue with a
200 stack size big enough to save our registers.
201 -----------------------------------------------------------*/
203 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
204 s390_lgr (buf, s390_r11, s390_r15);
205 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
206 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
208 /*---------------------------------------------------------------*/
209 /* we build the MonoLMF structure on the stack - see mini-s390.h */
210 /* Keep in sync with the code in mono_arch_emit_prolog */
211 /*---------------------------------------------------------------*/
213 s390_lgr (buf, LMFReg, STK_BASE);
214 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
216 /*---------------------------------------------------------------*/
217 /* Save general and floating point registers in LMF */
218 /*---------------------------------------------------------------*/
219 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
220 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
221 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
222 s390_r11, S390_REG_SAVE_OFFSET);
224 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
225 for (i = s390_f0; i <= s390_f15; ++i) {
226 s390_std (buf, i, 0, LMFReg, offset);
227 offset += sizeof(gdouble);
230 /*----------------------------------------------------------
231 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
232 LMF. We'll need to restore it after the call to
233 's390_magic_trampoline' and before the call to the native
235 ----------------------------------------------------------*/
237 S390_SET (buf, s390_r1, mono_get_lmf_addr);
238 s390_basr (buf, s390_r14, s390_r1);
240 /*---------------------------------------------------------------*/
241 /* Set lmf.lmf_addr = jit_tls->lmf */
242 /*---------------------------------------------------------------*/
243 s390_stg (buf, s390_r2, 0, LMFReg,
244 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
246 /*---------------------------------------------------------------*/
247 /* Get current lmf */
248 /*---------------------------------------------------------------*/
249 s390_lg (buf, s390_r0, 0, s390_r2, 0);
251 /*---------------------------------------------------------------*/
252 /* Set our lmf as the current lmf */
253 /*---------------------------------------------------------------*/
254 s390_stg (buf, LMFReg, 0, s390_r2, 0);
256 /*---------------------------------------------------------------*/
257 /* Have our lmf.previous_lmf point to the last lmf */
258 /*---------------------------------------------------------------*/
259 s390_stg (buf, s390_r0, 0, LMFReg,
260 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
262 /*---------------------------------------------------------------*/
263 /* save method info */
264 /*---------------------------------------------------------------*/
265 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
266 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
268 /*---------------------------------------------------------------*/
269 /* save the current SP */
270 /*---------------------------------------------------------------*/
271 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
272 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
274 /*---------------------------------------------------------------*/
275 /* save the current IP */
276 /*---------------------------------------------------------------*/
278 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
280 s390_lghi (buf, s390_r1, 0);
282 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
284 /*---------------------------------------------------------------*/
285 /* STEP 2: call the C trampoline function */
286 /*---------------------------------------------------------------*/
290 /* Arg 1: mgreg_t *regs */
291 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
293 /* Arg 2: code (next address to the instruction that called us) */
295 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
297 s390_lghi (buf, s390_r3, 0);
300 /* Arg 3: Trampoline argument */
301 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
303 /* Arg 4: trampoline address. */
304 S390_SET (buf, s390_r5, buf);
306 /* Calculate call address and call the C trampoline. Return value will be in r2 */
307 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
308 S390_SET (buf, s390_r1, tramp);
309 s390_basr (buf, s390_r14, s390_r1);
311 /* OK, code address is now on r2. Save it, so that we
312 can restore r2 and use it later */
313 s390_stg (buf, s390_r2, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
315 /* Check for thread interruption */
316 S390_SET (buf, s390_r1, (guint8 *)mono_interruption_checkpoint_from_trampoline);
317 s390_basr (buf, s390_r14, s390_r1);
319 /*----------------------------------------------------------
320 STEP 3: Restore the LMF
321 ----------------------------------------------------------*/
322 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
325 s390_lg (buf, s390_r1, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
327 /*----------------------------------------------------------
328 STEP 4: call the compiled method
329 ----------------------------------------------------------*/
331 /* Restore parameter registers */
332 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
334 /* Restore the FP registers */
335 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
336 for (i = s390_f0; i <= s390_f15; ++i) {
337 s390_ld (buf, i, 0, LMFReg, offset);
338 offset += sizeof(gdouble);
341 /* Restore stack pointer and jump to the code -
342 * R14 contains the return address to our caller
344 s390_lgr (buf, STK_BASE, s390_r11);
345 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
347 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
348 s390_lgr (buf, s390_r2, s390_r1);
349 s390_br (buf, s390_r14);
351 s390_br (buf, s390_r1);
354 /* Flush instruction cache, since we've generated code */
355 mono_arch_flush_icache (code, buf - code);
356 MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
359 tramp_name = mono_get_generic_trampoline_name (tramp_type);
360 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
364 g_assert ((buf - code) <= 512);
369 /*========================= End of Function ========================*/
371 /*------------------------------------------------------------------*/
373 /* Name - mono_arch_invalidate_method */
377 /*------------------------------------------------------------------*/
380 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
382 /* FIXME: This is not thread safe */
383 guint8 *code = ji->code_start;
385 S390_SET (code, s390_r1, func);
386 S390_SET (code, s390_r2, func_arg);
387 s390_br (code, s390_r1);
391 /*========================= End of Function ========================*/
393 /*------------------------------------------------------------------*/
395 /* Name - mono_arch_create_specific_trampoline */
397 /* Function - Creates the given kind of specific trampoline */
399 /*------------------------------------------------------------------*/
402 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
404 guint8 *code, *buf, *tramp;
407 tramp = mono_get_trampoline_code (tramp_type);
409 /*----------------------------------------------------------*/
410 /* This is the method-specific part of the trampoline. Its */
411 /* purpose is to provide the generic part with the */
412 /* MonoMethod *method pointer. We'll use r1 to keep it. */
413 /*----------------------------------------------------------*/
414 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
416 S390_SET (buf, s390_r1, arg1);
417 displace = (tramp - buf) / 2;
418 s390_jg (buf, displace);
420 /* Flush instruction cache, since we've generated code */
421 mono_arch_flush_icache (code, buf - code);
422 MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf,
423 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_RAISE (jit_code_buffer, (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_RAISE (jit_code_buffer, (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 ========================*/