[interp] Remove varargs from InterpFrame and recompute it instead (#16598)
[mono-project.git] / mono / mini / tramp-s390x.c
blob56f586a878de9cefddd2b3f5e5578f4a1a580387
1 /**
2 * \file
3 * Function - JIT trampoline code for S/390.
5 * Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com)
7 * Date - January, 2004
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 /*------------------------------------------------------------------*/
19 /* D e f i n e s */
20 /*------------------------------------------------------------------*/
22 #define LMFReg s390_r13
25 * Method-specific trampoline code fragment sizes
27 #define SPECIFIC_TRAMPOLINE_SIZE 96
29 /*========================= End of Defines =========================*/
31 /*------------------------------------------------------------------*/
32 /* I n c l u d e s */
33 /*------------------------------------------------------------------*/
35 #include <config.h>
36 #include <glib.h>
37 #include <string.h>
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>
47 #include "mini.h"
48 #include "mini-s390x.h"
49 #include "mini-runtime.h"
50 #include "support-s390x.h"
51 #include "jit-icalls.h"
53 /*========================= End of Includes ========================*/
55 /*------------------------------------------------------------------*/
56 /* T y p e d e f s */
57 /*------------------------------------------------------------------*/
59 typedef struct {
60 guint8 stk[S390_MINIMAL_STACK_SIZE]; /* Standard s390x stack */
61 guint64 saveFn; /* Call address */
62 struct MonoLMF LMF; /* LMF */
63 } trampStack_t;
65 /*========================= End of Typedefs ========================*/
67 /*------------------------------------------------------------------*/
68 /* P r o t o t y p e s */
69 /*------------------------------------------------------------------*/
71 /*========================= End of Prototypes ======================*/
73 /*------------------------------------------------------------------*/
74 /* G l o b a l V a r i a b l e s */
75 /*------------------------------------------------------------------*/
78 /*====================== End of Global Variables ===================*/
80 /*------------------------------------------------------------------*/
81 /* */
82 /* Name - mono_arch_get_unbox_trampoline */
83 /* */
84 /* Function - Return a pointer to a trampoline which does the */
85 /* unboxing before calling the method. */
86 /* */
87 /* When value type methods are called through the */
88 /* vtable we need to unbox the 'this' argument. */
89 /* */
90 /* Parameters - method - Methd pointer */
91 /* addr - Pointer to native code for method */
92 /* */
93 /*------------------------------------------------------------------*/
95 gpointer
96 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
98 guint8 *code, *start;
99 int this_pos = s390_r2;
100 MonoDomain *domain = mono_domain_get ();
101 char trampName[128];
103 start = code = mono_domain_code_reserve (domain, 28);
105 S390_SET (code, s390_r1, addr);
106 s390_aghi (code, this_pos, MONO_ABI_SIZEOF (MonoObject));
107 s390_br (code, s390_r1);
109 g_assert ((code - start) <= 28);
111 mono_arch_flush_icache (start, code - start);
112 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method));
114 snprintf(trampName, sizeof(trampName), "%s_unbox_trampoline", method->name);
116 mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
118 return start;
121 /*========================= End of Function ========================*/
123 /*------------------------------------------------------------------*/
124 /* */
125 /* Name - mono_arch_patch_callsite */
126 /* */
127 /* Function - Patch a non-virtual callsite so it calls @addr. */
128 /* */
129 /*------------------------------------------------------------------*/
131 void
132 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
134 gint32 displace;
135 unsigned short opcode;
137 opcode = *((unsigned short *) (orig_code - 2));
138 if (opcode == 0x0dee) {
139 /* This should be a 'iihf/iilf' sequence */
140 S390_EMIT_CALL((orig_code - 14), addr);
141 mono_arch_flush_icache (orig_code - 14, 12);
142 } else {
143 /* This is the 'brasl' instruction */
144 orig_code -= 4;
145 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
146 s390_patch_rel (orig_code, displace);
147 mono_arch_flush_icache (orig_code, 4);
151 /*========================= End of Function ========================*/
153 /*------------------------------------------------------------------*/
154 /* */
155 /* Name - mono_arch_patch_plt_entry. */
156 /* */
157 /* Function - Patch a PLT entry - unused as yet. */
158 /* */
159 /*------------------------------------------------------------------*/
161 void
162 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, host_mgreg_t *regs, guint8 *addr)
164 g_assert_not_reached ();
167 /*========================= End of Function ========================*/
169 /*------------------------------------------------------------------*/
170 /* */
171 /* Name - mono_arch_create_trampoline_code */
172 /* */
173 /* Function - Create the designated type of trampoline according*/
174 /* to the 'tramp_type' parameter. */
175 /* */
176 /*------------------------------------------------------------------*/
178 guchar*
179 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
181 const char *tramp_name;
182 guint8 *buf, *tramp, *code;
183 int i, offset, has_caller;
184 short *o[1];
185 GSList *unwind_ops = NULL;
186 MonoJumpInfo *ji = NULL;
188 g_assert (!aot);
190 /* Now we'll create in 'buf' the S/390 trampoline code. This
191 is the trampoline code common to all methods */
193 code = buf = mono_global_codeman_reserve(512);
195 if (tramp_type == MONO_TRAMPOLINE_JUMP)
196 has_caller = 0;
197 else
198 has_caller = 1;
200 /*-----------------------------------------------------------
201 STEP 0: First create a non-standard function prologue with a
202 stack size big enough to save our registers.
203 -----------------------------------------------------------*/
205 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
206 s390_lgr (buf, s390_r11, s390_r15);
207 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
208 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
210 /*---------------------------------------------------------------*/
211 /* we build the MonoLMF structure on the stack - see mini-s390.h */
212 /* Keep in sync with the code in mono_arch_emit_prolog */
213 /*---------------------------------------------------------------*/
215 s390_lgr (buf, LMFReg, STK_BASE);
216 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
218 /*---------------------------------------------------------------*/
219 /* Save general and floating point registers in LMF */
220 /*---------------------------------------------------------------*/
221 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
222 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
223 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
224 s390_r11, S390_REG_SAVE_OFFSET);
226 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
227 for (i = s390_f0; i <= s390_f15; ++i) {
228 s390_std (buf, i, 0, LMFReg, offset);
229 offset += sizeof(gdouble);
232 /*----------------------------------------------------------
233 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
234 LMF. We'll need to restore it after the call to
235 's390_magic_trampoline' and before the call to the native
236 method.
237 ----------------------------------------------------------*/
239 S390_SET (buf, s390_r1, mono_get_lmf_addr);
240 s390_basr (buf, s390_r14, s390_r1);
242 /*---------------------------------------------------------------*/
243 /* Set lmf.lmf_addr = jit_tls->lmf */
244 /*---------------------------------------------------------------*/
245 s390_stg (buf, s390_r2, 0, LMFReg,
246 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
248 /*---------------------------------------------------------------*/
249 /* Get current lmf */
250 /*---------------------------------------------------------------*/
251 s390_lg (buf, s390_r0, 0, s390_r2, 0);
253 /*---------------------------------------------------------------*/
254 /* Set our lmf as the current lmf */
255 /*---------------------------------------------------------------*/
256 s390_stg (buf, LMFReg, 0, s390_r2, 0);
258 /*---------------------------------------------------------------*/
259 /* Have our lmf.previous_lmf point to the last lmf */
260 /*---------------------------------------------------------------*/
261 s390_stg (buf, s390_r0, 0, LMFReg,
262 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
264 /*---------------------------------------------------------------*/
265 /* save method info */
266 /*---------------------------------------------------------------*/
267 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
268 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
270 /*---------------------------------------------------------------*/
271 /* save the current SP */
272 /*---------------------------------------------------------------*/
273 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
274 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
276 /*---------------------------------------------------------------*/
277 /* save the current IP */
278 /*---------------------------------------------------------------*/
279 if (has_caller) {
280 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
281 } else {
282 s390_lghi (buf, s390_r1, 0);
284 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
286 /*---------------------------------------------------------------*/
287 /* STEP 2: call the C trampoline function */
288 /*---------------------------------------------------------------*/
290 /* Set arguments */
292 /* Arg 1: host_mgreg_t *regs */
293 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
295 /* Arg 2: code (next address to the instruction that called us) */
296 if (has_caller) {
297 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
298 } else {
299 s390_lghi (buf, s390_r3, 0);
302 /* Arg 3: Trampoline argument */
303 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
305 /* Arg 4: trampoline address. */
306 S390_SET (buf, s390_r5, buf);
308 /* Calculate call address and call the C trampoline. Return value will be in r2 */
309 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
310 S390_SET (buf, s390_r1, tramp);
311 s390_basr (buf, s390_r14, s390_r1);
313 /* OK, code address is now on r2. Save it, so that we
314 can restore r2 and use it later */
315 s390_stg (buf, s390_r2, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
317 /*----------------------------------------------------------
318 STEP 3: Restore the LMF
319 ----------------------------------------------------------*/
320 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
322 /* Check for thread interruption */
323 S390_SET (buf, s390_r1, (guint8 *)mono_thread_force_interruption_checkpoint_noraise);
324 s390_basr (buf, s390_r14, s390_r1);
325 s390_ltgr (buf, s390_r2, s390_r2);
326 s390_jz (buf, 0); CODEPTR (buf, o[0]);
329 * Exception case:
330 * We have an exception we want to throw in the caller's frame, so pop
331 * the trampoline frame and throw from the caller.
333 S390_SET (buf, s390_r1, (guint *)mono_get_rethrow_preserve_exception_addr ());
334 s390_aghi (buf, STK_BASE, sizeof(trampStack_t));
335 s390_lg (buf, s390_r1, 0, s390_r1, 0);
336 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
337 s390_br (buf, s390_r1);
338 PTRSLOT (buf, o[0]);
340 /* Reload result */
341 s390_lg (buf, s390_r1, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
343 /*----------------------------------------------------------
344 STEP 4: call the compiled method
345 ----------------------------------------------------------*/
347 /* Restore parameter registers */
348 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
350 /* Restore the FP registers */
351 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
352 for (i = s390_f0; i <= s390_f15; ++i) {
353 s390_ld (buf, i, 0, LMFReg, offset);
354 offset += sizeof(gdouble);
357 /* Restore stack pointer and jump to the code -
358 * R14 contains the return address to our caller
360 s390_lgr (buf, STK_BASE, s390_r11);
361 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
363 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
364 s390_lgr (buf, s390_r2, s390_r1);
365 s390_br (buf, s390_r14);
366 } else {
367 s390_br (buf, s390_r1);
370 /* Flush instruction cache, since we've generated code */
371 mono_arch_flush_icache (code, buf - code);
372 MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
374 g_assert (info);
375 tramp_name = mono_get_generic_trampoline_name (tramp_type);
376 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
378 /* Sanity check */
379 g_assert ((buf - code) <= 512);
381 return code;
384 /*========================= End of Function ========================*/
386 /*------------------------------------------------------------------*/
387 /* */
388 /* Name - mono_arch_invalidate_method */
389 /* */
390 /* Function - */
391 /* */
392 /*------------------------------------------------------------------*/
394 void
395 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
397 /* FIXME: This is not thread safe */
398 guint8 *code = ji->code_start;
400 S390_SET (code, s390_r1, func);
401 S390_SET (code, s390_r2, func_arg);
402 s390_br (code, s390_r1);
406 /*========================= End of Function ========================*/
408 /*------------------------------------------------------------------*/
409 /* */
410 /* Name - mono_arch_create_specific_trampoline */
411 /* */
412 /* Function - Creates the given kind of specific trampoline */
413 /* */
414 /*------------------------------------------------------------------*/
416 gpointer
417 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
419 guint8 *code, *buf, *tramp;
420 gint32 displace;
422 tramp = mono_get_trampoline_code (tramp_type);
424 /*----------------------------------------------------------*/
425 /* This is the method-specific part of the trampoline. Its */
426 /* purpose is to provide the generic part with the */
427 /* MonoMethod *method pointer. We'll use r1 to keep it. */
428 /*----------------------------------------------------------*/
429 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
431 S390_SET (buf, s390_r1, arg1);
432 displace = (tramp - buf) / 2;
433 s390_jg (buf, displace);
435 /* Flush instruction cache, since we've generated code */
436 mono_arch_flush_icache (code, buf - code);
437 MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf,
438 MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
439 (void *) mono_get_generic_trampoline_simple_name (tramp_type)));
441 /* Sanity check */
442 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
444 if (code_len)
445 *code_len = buf - code;
447 return code;
450 /*========================= End of Function ========================*/
452 /*------------------------------------------------------------------*/
453 /* */
454 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
455 /* */
456 /* Function - */
457 /* */
458 /*------------------------------------------------------------------*/
460 gpointer
461 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
463 guint8 *tramp;
464 guint8 *code, *buf;
465 guint8 **rgctx_null_jumps;
466 gint32 displace;
467 int tramp_size,
468 depth,
469 index,
470 iPatch = 0,
472 gboolean mrgctx;
473 MonoJumpInfo *ji = NULL;
474 GSList *unwind_ops = NULL;
476 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
477 index = MONO_RGCTX_SLOT_INDEX (slot);
478 if (mrgctx)
479 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (target_mgreg_t);
480 for (depth = 0; ; ++depth) {
481 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
483 if (index < size - 1)
484 break;
485 index -= size - 1;
488 tramp_size = 48 + 16 * depth;
489 if (mrgctx)
490 tramp_size += 4;
491 else
492 tramp_size += 12;
494 code = buf = mono_global_codeman_reserve (tramp_size);
496 unwind_ops = mono_arch_get_cie_program ();
498 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
500 if (mrgctx) {
501 /* get mrgctx ptr */
502 s390_lgr (code, s390_r1, s390_r2);
503 } else {
504 /* load rgctx ptr from vtable */
505 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
506 /* is the rgctx ptr null? */
507 s390_ltgr (code, s390_r1, s390_r1);
508 /* if yes, jump to actual trampoline */
509 rgctx_null_jumps [iPatch++] = code;
510 s390_jge (code, 0);
513 for (i = 0; i < depth; ++i) {
514 /* load ptr to next array */
515 if (mrgctx && i == 0)
516 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
517 else
518 s390_lg (code, s390_r1, 0, s390_r1, 0);
519 s390_ltgr (code, s390_r1, s390_r1);
520 /* if the ptr is null then jump to actual trampoline */
521 rgctx_null_jumps [iPatch++] = code;
522 s390_jge (code, 0);
525 /* fetch slot */
526 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (target_mgreg_t) * (index + 1)));
527 /* is the slot null? */
528 s390_ltgr (code, s390_r1, s390_r1);
529 /* if yes, jump to actual trampoline */
530 rgctx_null_jumps [iPatch++] = code;
531 s390_jge (code, 0);
532 /* otherwise return r1 */
533 s390_lgr (code, s390_r2, s390_r1);
534 s390_br (code, s390_r14);
536 for (i = 0; i < iPatch; i++) {
537 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
538 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
541 g_free (rgctx_null_jumps);
543 /* move the rgctx pointer to the VTABLE register */
544 #if MONO_ARCH_VTABLE_REG != s390_r2
545 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
546 #endif
548 tramp = (guint8*)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
549 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
551 /* jump to the actual trampoline */
552 displace = (tramp - code) / 2;
553 s390_jg (code, displace);
555 mono_arch_flush_icache (buf, code - buf);
556 MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
558 g_assert (code - buf <= tramp_size);
560 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
561 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
562 g_free (name);
564 return(buf);
567 /*========================= End of Function ========================*/
569 /*------------------------------------------------------------------*/
570 /* */
571 /* Name - mono_arch_get_static_rgctx_trampoline */
572 /* */
573 /* Function - Create a trampoline which sets RGCTX_REG to ARG */
574 /* then jumps to ADDR. */
575 /* */
576 /*------------------------------------------------------------------*/
578 gpointer
579 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
581 guint8 *code, *start;
582 gint32 displace;
583 int buf_len;
585 MonoDomain *domain = mono_domain_get ();
587 buf_len = 32;
589 start = code = mono_domain_code_reserve (domain, buf_len);
591 S390_SET (code, MONO_ARCH_RGCTX_REG, arg);
592 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
593 s390_jg (code, displace);
594 g_assert ((code - start) < buf_len);
596 mono_arch_flush_icache (start, code - start);
597 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
599 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL), domain);
601 return(start);
604 /*========================= End of Function ========================*/