1 /*------------------------------------------------------------------*/
3 /* Name - tramp-s390x.c */
5 /* Function - JIT trampoline code for S/390. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /* Copyright - 2001 Ximian, Inc. */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 #define LMFReg s390_r13
26 * Method-specific trampoline code fragment sizes
28 #define SPECIFIC_TRAMPOLINE_SIZE 96
30 /*========================= End of Defines =========================*/
32 /*------------------------------------------------------------------*/
34 /*------------------------------------------------------------------*/
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/gc-internal.h>
43 #include <mono/metadata/marshal.h>
44 #include <mono/metadata/monitor.h>
45 #include <mono/metadata/profiler-private.h>
46 #include <mono/metadata/tabledefs.h>
47 #include <mono/arch/s390x/s390x-codegen.h>
50 #include "mini-s390x.h"
51 #include "support-s390x.h"
53 /*========================= End of Includes ========================*/
55 /*------------------------------------------------------------------*/
57 /*------------------------------------------------------------------*/
60 guint8 stk[S390_MINIMAL_STACK_SIZE]; /* Standard s390x stack */
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 ();
101 start = code = mono_domain_code_reserve (domain, 28);
103 S390_SET (code, s390_r1, addr);
104 s390_aghi (code, this_pos, sizeof(MonoObject));
105 s390_br (code, s390_r1);
107 g_assert ((code - start) <= 28);
109 mono_arch_flush_icache (start, code - start);
110 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
115 /*========================= End of Function ========================*/
117 /*------------------------------------------------------------------*/
119 /* Name - mono_arch_patch_callsite */
121 /* Function - Patch a non-virtual callsite so it calls @addr. */
123 /*------------------------------------------------------------------*/
126 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
129 unsigned short opcode;
131 opcode = *((unsigned short *) (orig_code - 2));
132 if (opcode == 0x0dee) {
133 /* This should be a 'iihf/iilf' sequence */
134 S390_EMIT_CALL((orig_code - 14), addr);
135 mono_arch_flush_icache (orig_code - 14, 12);
137 /* This is the 'brasl' instruction */
139 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
140 s390_patch_rel (orig_code, displace);
141 mono_arch_flush_icache (orig_code, 4);
145 /*========================= End of Function ========================*/
147 /*------------------------------------------------------------------*/
149 /* Name - mono_arch_patch_plt_entry. */
151 /* Function - Patch a PLT entry - unused as yet. */
153 /*------------------------------------------------------------------*/
156 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
158 g_assert_not_reached ();
161 /*========================= End of Function ========================*/
163 /*------------------------------------------------------------------*/
165 /* Name - mono_arch_create_trampoline_code */
167 /* Function - Create the designated type of trampoline according*/
168 /* to the 'tramp_type' parameter. */
170 /*------------------------------------------------------------------*/
173 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
176 guint8 *buf, *tramp, *code;
177 int i, offset, has_caller;
178 GSList *unwind_ops = NULL;
179 MonoJumpInfo *ji = NULL;
183 /* Now we'll create in 'buf' the S/390 trampoline code. This
184 is the trampoline code common to all methods */
186 code = buf = mono_global_codeman_reserve(512);
188 if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
189 (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD))
194 /*-----------------------------------------------------------
195 STEP 0: First create a non-standard function prologue with a
196 stack size big enough to save our registers.
197 -----------------------------------------------------------*/
199 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
200 s390_lgr (buf, s390_r11, s390_r15);
201 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
202 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
204 /*---------------------------------------------------------------*/
205 /* we build the MonoLMF structure on the stack - see mini-s390.h */
206 /* Keep in sync with the code in mono_arch_emit_prolog */
207 /*---------------------------------------------------------------*/
209 s390_lgr (buf, LMFReg, STK_BASE);
210 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
212 /*---------------------------------------------------------------*/
213 /* Save general and floating point registers in LMF */
214 /*---------------------------------------------------------------*/
215 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
216 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
217 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
218 s390_r11, S390_REG_SAVE_OFFSET);
220 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
221 for (i = s390_f0; i <= s390_f15; ++i) {
222 s390_std (buf, i, 0, LMFReg, offset);
223 offset += sizeof(gdouble);
226 /*----------------------------------------------------------
227 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
228 LMF. We'll need to restore it after the call to
229 's390_magic_trampoline' and before the call to the native
231 ----------------------------------------------------------*/
233 S390_SET (buf, s390_r1, mono_get_lmf_addr);
234 s390_basr (buf, s390_r14, s390_r1);
236 /*---------------------------------------------------------------*/
237 /* Set lmf.lmf_addr = jit_tls->lmf */
238 /*---------------------------------------------------------------*/
239 s390_stg (buf, s390_r2, 0, LMFReg,
240 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
242 /*---------------------------------------------------------------*/
243 /* Get current lmf */
244 /*---------------------------------------------------------------*/
245 s390_lg (buf, s390_r0, 0, s390_r2, 0);
247 /*---------------------------------------------------------------*/
248 /* Set our lmf as the current lmf */
249 /*---------------------------------------------------------------*/
250 s390_stg (buf, LMFReg, 0, s390_r2, 0);
252 /*---------------------------------------------------------------*/
253 /* Have our lmf.previous_lmf point to the last lmf */
254 /*---------------------------------------------------------------*/
255 s390_stg (buf, s390_r0, 0, LMFReg,
256 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
258 /*---------------------------------------------------------------*/
259 /* save method info */
260 /*---------------------------------------------------------------*/
261 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
262 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
264 /*---------------------------------------------------------------*/
265 /* save the current SP */
266 /*---------------------------------------------------------------*/
267 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
268 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
270 /*---------------------------------------------------------------*/
271 /* save the current IP */
272 /*---------------------------------------------------------------*/
274 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
276 s390_lghi (buf, s390_r1, 0);
278 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
280 /*---------------------------------------------------------------*/
281 /* STEP 2: call the C trampoline function */
282 /*---------------------------------------------------------------*/
286 /* Arg 1: mgreg_t *regs */
287 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
289 /* Arg 2: code (next address to the instruction that called us) */
291 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
293 s390_lghi (buf, s390_r3, 0);
296 /* Arg 3: Trampoline argument */
297 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
299 /* Arg 4: trampoline address. */
300 S390_SET (buf, s390_r5, buf);
302 /* Calculate call address and call the C trampoline. Return value will be in r2 */
303 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
304 S390_SET (buf, s390_r1, tramp);
305 s390_basr (buf, s390_r14, s390_r1);
307 /* OK, code address is now on r2. Move it to r1, so that we
308 can restore r2 and use it from r1 later */
309 s390_lgr (buf, s390_r1, s390_r2);
311 /*----------------------------------------------------------
312 STEP 3: Restore the LMF
313 ----------------------------------------------------------*/
314 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
316 /*----------------------------------------------------------
317 STEP 4: call the compiled method
318 ----------------------------------------------------------*/
320 /* Restore parameter registers */
321 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
323 /* Restore the FP registers */
324 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
325 for (i = s390_f0; i <= s390_f15; ++i) {
326 s390_ld (buf, i, 0, LMFReg, offset);
327 offset += sizeof(gdouble);
330 /* Restore stack pointer and jump to the code -
331 * R14 contains the return address to our caller
333 s390_lgr (buf, STK_BASE, s390_r11);
334 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
336 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
337 s390_lgr (buf, s390_r2, s390_r1);
338 s390_br (buf, s390_r14);
340 s390_br (buf, s390_r1);
343 /* Flush instruction cache, since we've generated code */
344 mono_arch_flush_icache (code, buf - code);
345 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
348 tramp_name = mono_get_generic_trampoline_name (tramp_type);
349 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
353 g_assert ((buf - code) <= 512);
358 /*========================= End of Function ========================*/
360 /*------------------------------------------------------------------*/
362 /* Name - mono_arch_invalidate_method */
366 /*------------------------------------------------------------------*/
369 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
371 /* FIXME: This is not thread safe */
372 guint8 *code = ji->code_start;
374 S390_SET (code, s390_r1, func);
375 S390_SET (code, s390_r2, func_arg);
376 s390_br (code, s390_r1);
380 /*========================= End of Function ========================*/
382 /*------------------------------------------------------------------*/
384 /* Name - mono_arch_create_specific_trampoline */
386 /* Function - Creates the given kind of specific trampoline */
388 /*------------------------------------------------------------------*/
391 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
393 guint8 *code, *buf, *tramp;
396 tramp = mono_get_trampoline_code (tramp_type);
398 /*----------------------------------------------------------*/
399 /* This is the method-specific part of the trampoline. Its */
400 /* purpose is to provide the generic part with the */
401 /* MonoMethod *method pointer. We'll use r1 to keep it. */
402 /*----------------------------------------------------------*/
403 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
405 switch (tramp_type) {
407 * Monitor tramps have the object in r2
409 case MONO_TRAMPOLINE_MONITOR_ENTER:
410 case MONO_TRAMPOLINE_MONITOR_ENTER_V4:
411 case MONO_TRAMPOLINE_MONITOR_EXIT:
412 s390_lgr (buf, s390_r1, s390_r2);
415 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_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
423 (void *) mono_get_generic_trampoline_simple_name (tramp_type));
426 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
429 *code_len = buf - code;
434 /*========================= End of Function ========================*/
436 /*------------------------------------------------------------------*/
438 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
442 /*------------------------------------------------------------------*/
445 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
449 guint8 **rgctx_null_jumps;
457 MonoJumpInfo *ji = NULL;
458 GSList *unwind_ops = NULL;
460 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
461 index = MONO_RGCTX_SLOT_INDEX (slot);
463 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
464 for (depth = 0; ; ++depth) {
465 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
467 if (index < size - 1)
472 tramp_size = 48 + 16 * depth;
478 code = buf = mono_global_codeman_reserve (tramp_size);
480 unwind_ops = mono_arch_get_cie_program ();
482 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
486 s390_lgr (code, s390_r1, s390_r2);
488 /* load rgctx ptr from vtable */
489 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
490 /* is the rgctx ptr null? */
491 s390_ltgr (code, s390_r1, s390_r1);
492 /* if yes, jump to actual trampoline */
493 rgctx_null_jumps [iPatch++] = code;
497 for (i = 0; i < depth; ++i) {
498 /* load ptr to next array */
499 if (mrgctx && i == 0)
500 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
502 s390_lg (code, s390_r1, 0, s390_r1, 0);
503 s390_ltgr (code, s390_r1, s390_r1);
504 /* if the ptr is null then jump to actual trampoline */
505 rgctx_null_jumps [iPatch++] = code;
510 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
511 /* is the slot null? */
512 s390_ltgr (code, s390_r1, s390_r1);
513 /* if yes, jump to actual trampoline */
514 rgctx_null_jumps [iPatch++] = code;
516 /* otherwise return r1 */
517 s390_lgr (code, s390_r2, s390_r1);
518 s390_br (code, s390_r14);
520 for (i = 0; i < iPatch; i++) {
521 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
522 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
525 g_free (rgctx_null_jumps);
527 /* move the rgctx pointer to the VTABLE register */
528 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
530 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
531 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
533 /* jump to the actual trampoline */
534 displace = (tramp - code) / 2;
535 s390_jg (code, displace);
537 mono_arch_flush_icache (buf, code - buf);
538 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
540 g_assert (code - buf <= tramp_size);
542 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
543 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
549 /*========================= End of Function ========================*/
551 /*------------------------------------------------------------------*/
553 /* Name - mono_arch_get_static_rgctx_trampoline */
555 /* Function - Create a trampoline which sets RGCTX_REG to MRGCTX*/
556 /* then jumps to ADDR. */
558 /*------------------------------------------------------------------*/
561 mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
562 MonoMethodRuntimeGenericContext *mrgctx,
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, mrgctx);
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);
586 /*========================= End of Function ========================*/
588 /*------------------------------------------------------------------*/
590 /* Name - handler_block_trampoline_helper */
594 /*------------------------------------------------------------------*/
597 handler_block_trampoline_helper (gpointer *ptr)
599 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
600 *ptr = jit_tls->handler_block_return_address;
603 /*========================= End of Function ========================*/
605 /*------------------------------------------------------------------*/
607 /* Name - mono_arch_create_handler_block_trampoline */
611 /*------------------------------------------------------------------*/
614 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
616 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
619 MonoJumpInfo *ji = NULL;
620 GSList *unwind_ops = NULL;
624 code = buf = mono_global_codeman_reserve (tramp_size);
627 * This trampoline restore the call chain of the handler block
628 * then jumps into the code that deals with it.
631 if (mono_get_jit_tls_offset () != -1) {
632 s390_ear (code, s390_r1, 0);
633 s390_sllg (code, s390_r1, s390_r1, 0, 32);
634 s390_ear (code, s390_r1, 1);
635 S390_SET (code, s390_r14, mono_get_jit_tls_offset());
636 s390_lg (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
640 S390_SET (code, s390_r1, tramp);
641 s390_br (code, s390_r1);
644 * Slow path uses a C helper
646 S390_SET (code, s390_r2, tramp);
647 S390_SET (code, s390_r1, handler_block_trampoline_helper);
648 s390_br (code, s390_r1);
651 mono_arch_flush_icache (buf, code - buf);
652 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
653 g_assert (code - buf <= tramp_size);
655 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
660 /*========================= End of Function ========================*/
662 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
663 /*------------------------------------------------------------------*/
665 /* Name - mono_arch_create_monitor_enter_trampoline */
669 /*------------------------------------------------------------------*/
672 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
676 gint16 *jump_obj_null,
681 *jump_sync_thin_hash = NULL,
682 *jump_lock_taken_true = NULL;
684 status_reg = s390_r0,
685 lock_taken_reg = s390_r1,
691 MonoJumpInfo *ji = NULL;
692 GSList *unwind_ops = NULL;
694 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
695 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
696 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
701 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
702 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
703 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
704 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
705 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
709 code = buf = mono_global_codeman_reserve (tramp_size);
711 unwind_ops = mono_arch_get_cie_program ();
713 if (mono_thread_get_tls_offset () != -1) {
714 /* MonoObject* obj is in obj_reg */
716 s390_ltgr (code, obj_reg, obj_reg);
717 /* if yes, jump to actual trampoline */
718 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
721 s390_cli (code, lock_taken_reg, 0, 1);
722 /* if *lock_taken is 1, jump to actual trampoline */
723 s390_je (code, 0); CODEPTR(code, jump_lock_taken_true);
726 /* load obj->synchronization to sync_reg */
727 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
729 if (mono_gc_is_moving ()) {
730 /*if bit zero is set it's a thin hash*/
731 s390_tmll (code, sync_reg, 1);
732 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
734 /* Clear bits used by the gc */
735 s390_nill (code, sync_reg, ~0x3);
738 /* is synchronization null? */
739 s390_ltgr (code, sync_reg, sync_reg);
740 /* if yes, jump to actual trampoline */
741 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
743 /* load MonoInternalThread* into tid_reg */
744 s390_ear (code, s390_r5, 0);
745 s390_sllg(code, s390_r5, s390_r5, 0, 32);
746 s390_ear (code, s390_r5, 1);
748 s390_lg (code, tid_reg, 0, s390_r5, mono_thread_get_tls_offset ());
749 s390_lgf (code, tid_reg, 0, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
751 /* is synchronization->owner free */
752 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
753 s390_nilf (code, status_reg, OWNER_MASK);
754 /* if not, jump to next case */
755 s390_jnz (code, 0); CODEPTR(code, jump_tid);
757 /* if yes, try a compare-exchange with the TID */
758 /* Form new status in tid_reg */
759 s390_xr (code, tid_reg, status_reg);
760 /* compare and exchange */
761 s390_cs (code, status_reg, tid_reg, sync_reg, status_offset);
762 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
763 /* if successful, return */
765 s390_mvi (code, lock_taken_reg, 0, 1);
766 s390_br (code, s390_r14);
768 /* next case: synchronization->owner is not null */
769 PTRSLOT(code, jump_tid);
770 /* is synchronization->owner == TID? */
771 s390_nilf (code, status_reg, OWNER_MASK);
772 s390_cr (code, status_reg, tid_reg);
773 /* if not, jump to actual trampoline */
774 s390_jnz (code, 0); CODEPTR(code, jump_other_owner);
775 /* if yes, increment nest */
776 s390_lgf (code, s390_r5, 0, sync_reg, nest_offset);
777 s390_ahi (code, s390_r5, 1);
778 s390_st (code, s390_r5, 0, sync_reg, nest_offset);
781 s390_mvi (code, lock_taken_reg, 0, 1);
782 s390_br (code, s390_r14);
784 PTRSLOT (code, jump_obj_null);
785 if (jump_sync_thin_hash)
786 PTRSLOT (code, jump_sync_thin_hash);
787 PTRSLOT (code, jump_sync_null);
788 PTRSLOT (code, jump_cs_failed);
789 PTRSLOT (code, jump_other_owner);
791 PTRSLOT (code, jump_lock_taken_true);
794 /* jump to the actual trampoline */
796 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
798 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
800 /* jump to the actual trampoline */
801 S390_SET (code, s390_r1, tramp);
802 s390_br (code, s390_r1);
804 mono_arch_flush_icache (code, code - buf);
805 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
806 g_assert (code - buf <= tramp_size);
810 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
812 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
818 /*========================= End of Function ========================*/
820 /*------------------------------------------------------------------*/
822 /* Name - mono_arch_create_monitor_exit_trampoline */
826 /*------------------------------------------------------------------*/
829 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
833 gint16 *jump_obj_null,
839 *jump_sync_thin_hash = NULL;
841 status_offset, nest_offset;
842 MonoJumpInfo *ji = NULL;
843 GSList *unwind_ops = NULL;
844 int obj_reg = s390_r2,
846 status_reg = s390_r4;
848 g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
850 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
851 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
852 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
853 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
854 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
858 code = buf = mono_global_codeman_reserve (tramp_size);
860 unwind_ops = mono_arch_get_cie_program ();
862 if (mono_thread_get_tls_offset () != -1) {
863 /* MonoObject* obj is in obj_reg */
865 s390_ltgr (code, obj_reg, obj_reg);
866 /* if yes, jump to actual trampoline */
867 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
869 /* load obj->synchronization to RCX */
870 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
872 if (mono_gc_is_moving ()) {
873 /*if bit zero is set it's a thin hash*/
874 s390_tmll (code, sync_reg, 1);
875 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
877 /* Clear bits used by the gc */
878 s390_nill (code, sync_reg, ~0x3);
881 /* is synchronization null? */
882 s390_ltgr (code, sync_reg, sync_reg);
883 /* if yes, jump to actual trampoline */
884 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
886 /* next case: synchronization is not null */
887 /* load MonoInternalThread* into r5 */
888 s390_ear (code, s390_r5, 0);
889 s390_sllg(code, s390_r5, s390_r5, 0, 32);
890 s390_ear (code, s390_r5, 1);
891 /* load TID into r1 */
892 s390_lg (code, s390_r1, 0, s390_r5, mono_thread_get_tls_offset ());
893 s390_lgf (code, s390_r1, 0, s390_r1, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
894 /* is synchronization->owner == TID */
895 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
896 s390_xr (code, s390_r1, status_reg);
897 s390_tmlh (code, s390_r1, OWNER_MASK);
898 /* if not, jump to actual trampoline */
899 s390_jno (code, 0); CODEPTR(code, jump_not_owned);
901 /* next case: synchronization->owner == TID */
902 /* is synchronization->nest == 1 */
903 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
904 s390_chi (code, s390_r0, 1);
905 /* if not, jump to next case */
906 s390_jne (code, 0); CODEPTR(code, jump_next);
907 /* if yes, is synchronization->entry_count greater than zero */
908 s390_cfi (code, status_reg, ENTRY_COUNT_WAITERS);
909 /* if not, jump to actual trampoline */
910 s390_jnz (code, 0); CODEPTR(code, jump_have_waiters);
911 /* if yes, try to set synchronization->owner to null and return */
912 /* old status in s390_r0 */
913 s390_lgfr (code, s390_r0, status_reg);
914 /* form new status */
915 s390_nilf (code, status_reg, ENTRY_COUNT_MASK);
916 /* compare and exchange */
917 s390_cs (code, s390_r0, status_reg, sync_reg, status_offset);
918 /* if not successful, jump to actual trampoline */
919 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
920 s390_br (code, s390_r14);
922 /* next case: synchronization->nest is not 1 */
923 PTRSLOT (code, jump_next);
924 /* decrease synchronization->nest and return */
925 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
926 s390_ahi (code, s390_r0, -1);
927 s390_st (code, s390_r0, 0, sync_reg, nest_offset);
928 s390_br (code, s390_r14);
930 PTRSLOT (code, jump_obj_null);
931 if (jump_sync_thin_hash)
932 PTRSLOT (code, jump_sync_thin_hash);
933 PTRSLOT (code, jump_have_waiters);
934 PTRSLOT (code, jump_not_owned);
935 PTRSLOT (code, jump_cs_failed);
936 PTRSLOT (code, jump_sync_null);
939 /* jump to the actual trampoline */
940 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
942 S390_SET (code, s390_r1, tramp);
943 s390_br (code, s390_r1);
945 mono_arch_flush_icache (code, code - buf);
946 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
947 g_assert (code - buf <= tramp_size);
950 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
955 /*========================= End of Function ========================*/