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);
112 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
117 /*========================= End of Function ========================*/
119 /*------------------------------------------------------------------*/
121 /* Name - mono_arch_patch_callsite */
123 /* Function - Patch a non-virtual callsite so it calls @addr. */
125 /*------------------------------------------------------------------*/
128 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
131 unsigned short opcode;
133 opcode = *((unsigned short *) (orig_code - 2));
134 if (opcode == 0x0dee) {
135 /* This should be a 'iihf/iilf' sequence */
136 S390_EMIT_CALL((orig_code - 14), addr);
137 mono_arch_flush_icache (orig_code - 14, 12);
139 /* This is the 'brasl' instruction */
141 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
142 s390_patch_rel (orig_code, displace);
143 mono_arch_flush_icache (orig_code, 4);
147 /*========================= End of Function ========================*/
149 /*------------------------------------------------------------------*/
151 /* Name - mono_arch_patch_plt_entry. */
153 /* Function - Patch a PLT entry - unused as yet. */
155 /*------------------------------------------------------------------*/
158 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
160 g_assert_not_reached ();
163 /*========================= End of Function ========================*/
165 /*------------------------------------------------------------------*/
167 /* Name - mono_arch_create_trampoline_code */
169 /* Function - Create the designated type of trampoline according*/
170 /* to the 'tramp_type' parameter. */
172 /*------------------------------------------------------------------*/
175 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
178 guint8 *buf, *tramp, *code;
179 int i, offset, has_caller;
180 GSList *unwind_ops = NULL;
181 MonoJumpInfo *ji = NULL;
185 /* Now we'll create in 'buf' the S/390 trampoline code. This
186 is the trampoline code common to all methods */
188 code = buf = mono_global_codeman_reserve(512);
190 if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
191 (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD))
196 /*-----------------------------------------------------------
197 STEP 0: First create a non-standard function prologue with a
198 stack size big enough to save our registers.
199 -----------------------------------------------------------*/
201 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
202 s390_lgr (buf, s390_r11, s390_r15);
203 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
204 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
206 /*---------------------------------------------------------------*/
207 /* we build the MonoLMF structure on the stack - see mini-s390.h */
208 /* Keep in sync with the code in mono_arch_emit_prolog */
209 /*---------------------------------------------------------------*/
211 s390_lgr (buf, LMFReg, STK_BASE);
212 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
214 /*---------------------------------------------------------------*/
215 /* Save general and floating point registers in LMF */
216 /*---------------------------------------------------------------*/
217 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
218 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
219 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
220 s390_r11, S390_REG_SAVE_OFFSET);
222 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
223 for (i = s390_f0; i <= s390_f15; ++i) {
224 s390_std (buf, i, 0, LMFReg, offset);
225 offset += sizeof(gdouble);
228 /*----------------------------------------------------------
229 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
230 LMF. We'll need to restore it after the call to
231 's390_magic_trampoline' and before the call to the native
233 ----------------------------------------------------------*/
235 S390_SET (buf, s390_r1, mono_get_lmf_addr);
236 s390_basr (buf, s390_r14, s390_r1);
238 /*---------------------------------------------------------------*/
239 /* Set lmf.lmf_addr = jit_tls->lmf */
240 /*---------------------------------------------------------------*/
241 s390_stg (buf, s390_r2, 0, LMFReg,
242 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
244 /*---------------------------------------------------------------*/
245 /* Get current lmf */
246 /*---------------------------------------------------------------*/
247 s390_lg (buf, s390_r0, 0, s390_r2, 0);
249 /*---------------------------------------------------------------*/
250 /* Set our lmf as the current lmf */
251 /*---------------------------------------------------------------*/
252 s390_stg (buf, LMFReg, 0, s390_r2, 0);
254 /*---------------------------------------------------------------*/
255 /* Have our lmf.previous_lmf point to the last lmf */
256 /*---------------------------------------------------------------*/
257 s390_stg (buf, s390_r0, 0, LMFReg,
258 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
260 /*---------------------------------------------------------------*/
261 /* save method info */
262 /*---------------------------------------------------------------*/
263 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
264 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
266 /*---------------------------------------------------------------*/
267 /* save the current SP */
268 /*---------------------------------------------------------------*/
269 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
270 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
272 /*---------------------------------------------------------------*/
273 /* save the current IP */
274 /*---------------------------------------------------------------*/
276 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
278 s390_lghi (buf, s390_r1, 0);
280 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
282 /*---------------------------------------------------------------*/
283 /* STEP 2: call the C trampoline function */
284 /*---------------------------------------------------------------*/
288 /* Arg 1: mgreg_t *regs */
289 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
291 /* Arg 2: code (next address to the instruction that called us) */
293 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
295 s390_lghi (buf, s390_r3, 0);
298 /* Arg 3: Trampoline argument */
299 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
301 /* Arg 4: trampoline address. */
302 S390_SET (buf, s390_r5, buf);
304 /* Calculate call address and call the C trampoline. Return value will be in r2 */
305 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
306 S390_SET (buf, s390_r1, tramp);
307 s390_basr (buf, s390_r14, s390_r1);
309 /* OK, code address is now on r2. Move it to r1, so that we
310 can restore r2 and use it from r1 later */
311 s390_lgr (buf, s390_r1, s390_r2);
313 /*----------------------------------------------------------
314 STEP 3: Restore the LMF
315 ----------------------------------------------------------*/
316 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
318 /*----------------------------------------------------------
319 STEP 4: call the compiled method
320 ----------------------------------------------------------*/
322 /* Restore parameter registers */
323 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
325 /* Restore the FP registers */
326 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
327 for (i = s390_f0; i <= s390_f15; ++i) {
328 s390_ld (buf, i, 0, LMFReg, offset);
329 offset += sizeof(gdouble);
332 /* Restore stack pointer and jump to the code -
333 * R14 contains the return address to our caller
335 s390_lgr (buf, STK_BASE, s390_r11);
336 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
338 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
339 s390_lgr (buf, s390_r2, s390_r1);
340 s390_br (buf, s390_r14);
342 s390_br (buf, s390_r1);
345 /* Flush instruction cache, since we've generated code */
346 mono_arch_flush_icache (code, buf - code);
347 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
350 tramp_name = mono_get_generic_trampoline_name (tramp_type);
351 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
355 g_assert ((buf - code) <= 512);
360 /*========================= End of Function ========================*/
362 /*------------------------------------------------------------------*/
364 /* Name - mono_arch_invalidate_method */
368 /*------------------------------------------------------------------*/
371 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
373 /* FIXME: This is not thread safe */
374 guint8 *code = ji->code_start;
376 S390_SET (code, s390_r1, func);
377 S390_SET (code, s390_r2, func_arg);
378 s390_br (code, s390_r1);
382 /*========================= End of Function ========================*/
384 /*------------------------------------------------------------------*/
386 /* Name - mono_arch_create_specific_trampoline */
388 /* Function - Creates the given kind of specific trampoline */
390 /*------------------------------------------------------------------*/
393 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
395 guint8 *code, *buf, *tramp;
398 tramp = mono_get_trampoline_code (tramp_type);
400 /*----------------------------------------------------------*/
401 /* This is the method-specific part of the trampoline. Its */
402 /* purpose is to provide the generic part with the */
403 /* MonoMethod *method pointer. We'll use r1 to keep it. */
404 /*----------------------------------------------------------*/
405 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
407 switch (tramp_type) {
409 * Monitor tramps have the object in r2
411 case MONO_TRAMPOLINE_MONITOR_ENTER:
412 case MONO_TRAMPOLINE_MONITOR_ENTER_V4:
413 case MONO_TRAMPOLINE_MONITOR_EXIT:
414 s390_lgr (buf, s390_r1, s390_r2);
417 S390_SET (buf, s390_r1, arg1);
419 displace = (tramp - buf) / 2;
420 s390_jg (buf, displace);
422 /* Flush instruction cache, since we've generated code */
423 mono_arch_flush_icache (code, buf - code);
424 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
425 (void *) mono_get_generic_trampoline_simple_name (tramp_type));
428 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
431 *code_len = buf - code;
436 /*========================= End of Function ========================*/
438 /*------------------------------------------------------------------*/
440 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
444 /*------------------------------------------------------------------*/
447 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
451 guint8 **rgctx_null_jumps;
459 MonoJumpInfo *ji = NULL;
460 GSList *unwind_ops = NULL;
462 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
463 index = MONO_RGCTX_SLOT_INDEX (slot);
465 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
466 for (depth = 0; ; ++depth) {
467 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
469 if (index < size - 1)
474 tramp_size = 48 + 16 * depth;
480 code = buf = mono_global_codeman_reserve (tramp_size);
482 unwind_ops = mono_arch_get_cie_program ();
484 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
488 s390_lgr (code, s390_r1, s390_r2);
490 /* load rgctx ptr from vtable */
491 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
492 /* is the rgctx ptr null? */
493 s390_ltgr (code, s390_r1, s390_r1);
494 /* if yes, jump to actual trampoline */
495 rgctx_null_jumps [iPatch++] = code;
499 for (i = 0; i < depth; ++i) {
500 /* load ptr to next array */
501 if (mrgctx && i == 0)
502 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
504 s390_lg (code, s390_r1, 0, s390_r1, 0);
505 s390_ltgr (code, s390_r1, s390_r1);
506 /* if the ptr is null then jump to actual trampoline */
507 rgctx_null_jumps [iPatch++] = code;
512 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
513 /* is the slot null? */
514 s390_ltgr (code, s390_r1, s390_r1);
515 /* if yes, jump to actual trampoline */
516 rgctx_null_jumps [iPatch++] = code;
518 /* otherwise return r1 */
519 s390_lgr (code, s390_r2, s390_r1);
520 s390_br (code, s390_r14);
522 for (i = 0; i < iPatch; i++) {
523 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
524 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
527 g_free (rgctx_null_jumps);
529 /* move the rgctx pointer to the VTABLE register */
530 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
532 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
533 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
535 /* jump to the actual trampoline */
536 displace = (tramp - code) / 2;
537 s390_jg (code, displace);
539 mono_arch_flush_icache (buf, code - buf);
540 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
542 g_assert (code - buf <= tramp_size);
544 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
545 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
551 /*========================= End of Function ========================*/
553 /*------------------------------------------------------------------*/
555 /* Name - mono_arch_get_static_rgctx_trampoline */
557 /* Function - Create a trampoline which sets RGCTX_REG to MRGCTX*/
558 /* then jumps to ADDR. */
560 /*------------------------------------------------------------------*/
563 mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
564 MonoMethodRuntimeGenericContext *mrgctx,
567 guint8 *code, *start;
571 MonoDomain *domain = mono_domain_get ();
575 start = code = mono_domain_code_reserve (domain, buf_len);
577 S390_SET (code, MONO_ARCH_RGCTX_REG, mrgctx);
578 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
579 s390_jg (code, displace);
580 g_assert ((code - start) < buf_len);
582 mono_arch_flush_icache (start, code - start);
583 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
585 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
590 /*========================= End of Function ========================*/
592 /*------------------------------------------------------------------*/
594 /* Name - handler_block_trampoline_helper */
598 /*------------------------------------------------------------------*/
601 handler_block_trampoline_helper (gpointer *ptr)
603 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
604 *ptr = jit_tls->handler_block_return_address;
607 /*========================= End of Function ========================*/
609 /*------------------------------------------------------------------*/
611 /* Name - mono_arch_create_handler_block_trampoline */
615 /*------------------------------------------------------------------*/
618 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
620 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
623 MonoJumpInfo *ji = NULL;
624 GSList *unwind_ops = NULL;
628 code = buf = mono_global_codeman_reserve (tramp_size);
631 * This trampoline restore the call chain of the handler block
632 * then jumps into the code that deals with it.
635 if (mono_get_jit_tls_offset () != -1) {
636 s390_ear (code, s390_r1, 0);
637 s390_sllg (code, s390_r1, s390_r1, 0, 32);
638 s390_ear (code, s390_r1, 1);
639 S390_SET (code, s390_r14, mono_get_jit_tls_offset());
640 s390_lg (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
644 S390_SET (code, s390_r1, tramp);
645 s390_br (code, s390_r1);
648 * Slow path uses a C helper
650 S390_SET (code, s390_r2, tramp);
651 S390_SET (code, s390_r1, handler_block_trampoline_helper);
652 s390_br (code, s390_r1);
655 mono_arch_flush_icache (buf, code - buf);
656 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
657 g_assert (code - buf <= tramp_size);
659 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
664 /*========================= End of Function ========================*/
666 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
667 /*------------------------------------------------------------------*/
669 /* Name - mono_arch_create_monitor_enter_trampoline */
673 /*------------------------------------------------------------------*/
676 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
680 gint16 *jump_obj_null,
685 *jump_sync_thin_hash = NULL,
686 *jump_lock_taken_true = NULL;
688 status_reg = s390_r0,
689 lock_taken_reg = s390_r1,
695 MonoJumpInfo *ji = NULL;
696 GSList *unwind_ops = NULL;
698 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
699 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
700 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
705 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
706 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
707 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
708 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
709 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
713 code = buf = mono_global_codeman_reserve (tramp_size);
715 unwind_ops = mono_arch_get_cie_program ();
717 if (mono_thread_get_tls_offset () != -1) {
718 /* MonoObject* obj is in obj_reg */
720 s390_ltgr (code, obj_reg, obj_reg);
721 /* if yes, jump to actual trampoline */
722 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
725 s390_cli (code, lock_taken_reg, 0, 1);
726 /* if *lock_taken is 1, jump to actual trampoline */
727 s390_je (code, 0); CODEPTR(code, jump_lock_taken_true);
730 /* load obj->synchronization to sync_reg */
731 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
733 if (mono_gc_is_moving ()) {
734 /*if bit zero is set it's a thin hash*/
735 s390_tmll (code, sync_reg, 1);
736 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
738 /* Clear bits used by the gc */
739 s390_nill (code, sync_reg, ~0x3);
742 /* is synchronization null? */
743 s390_ltgr (code, sync_reg, sync_reg);
744 /* if yes, jump to actual trampoline */
745 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
747 /* load MonoInternalThread* into tid_reg */
748 s390_ear (code, s390_r5, 0);
749 s390_sllg(code, s390_r5, s390_r5, 0, 32);
750 s390_ear (code, s390_r5, 1);
752 s390_lg (code, tid_reg, 0, s390_r5, mono_thread_get_tls_offset ());
753 s390_lgf (code, tid_reg, 0, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
755 /* is synchronization->owner free */
756 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
757 s390_nilf (code, status_reg, OWNER_MASK);
758 /* if not, jump to next case */
759 s390_jnz (code, 0); CODEPTR(code, jump_tid);
761 /* if yes, try a compare-exchange with the TID */
762 /* Form new status in tid_reg */
763 s390_xr (code, tid_reg, status_reg);
764 /* compare and exchange */
765 s390_cs (code, status_reg, tid_reg, sync_reg, status_offset);
766 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
767 /* if successful, return */
769 s390_mvi (code, lock_taken_reg, 0, 1);
770 s390_br (code, s390_r14);
772 /* next case: synchronization->owner is not null */
773 PTRSLOT(code, jump_tid);
774 /* is synchronization->owner == TID? */
775 s390_nilf (code, status_reg, OWNER_MASK);
776 s390_cr (code, status_reg, tid_reg);
777 /* if not, jump to actual trampoline */
778 s390_jnz (code, 0); CODEPTR(code, jump_other_owner);
779 /* if yes, increment nest */
780 s390_lgf (code, s390_r5, 0, sync_reg, nest_offset);
781 s390_ahi (code, s390_r5, 1);
782 s390_st (code, s390_r5, 0, sync_reg, nest_offset);
785 s390_mvi (code, lock_taken_reg, 0, 1);
786 s390_br (code, s390_r14);
788 PTRSLOT (code, jump_obj_null);
789 if (jump_sync_thin_hash)
790 PTRSLOT (code, jump_sync_thin_hash);
791 PTRSLOT (code, jump_sync_null);
792 PTRSLOT (code, jump_cs_failed);
793 PTRSLOT (code, jump_other_owner);
795 PTRSLOT (code, jump_lock_taken_true);
798 /* jump to the actual trampoline */
800 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
802 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
804 /* jump to the actual trampoline */
805 S390_SET (code, s390_r1, tramp);
806 s390_br (code, s390_r1);
808 mono_arch_flush_icache (code, code - buf);
809 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
810 g_assert (code - buf <= tramp_size);
814 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
816 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
822 /*========================= End of Function ========================*/
824 /*------------------------------------------------------------------*/
826 /* Name - mono_arch_create_monitor_exit_trampoline */
830 /*------------------------------------------------------------------*/
833 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
837 gint16 *jump_obj_null,
843 *jump_sync_thin_hash = NULL;
845 status_offset, nest_offset;
846 MonoJumpInfo *ji = NULL;
847 GSList *unwind_ops = NULL;
848 int obj_reg = s390_r2,
850 status_reg = s390_r4;
852 g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
854 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
855 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
856 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
857 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
858 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
862 code = buf = mono_global_codeman_reserve (tramp_size);
864 unwind_ops = mono_arch_get_cie_program ();
866 if (mono_thread_get_tls_offset () != -1) {
867 /* MonoObject* obj is in obj_reg */
869 s390_ltgr (code, obj_reg, obj_reg);
870 /* if yes, jump to actual trampoline */
871 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
873 /* load obj->synchronization to RCX */
874 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
876 if (mono_gc_is_moving ()) {
877 /*if bit zero is set it's a thin hash*/
878 s390_tmll (code, sync_reg, 1);
879 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
881 /* Clear bits used by the gc */
882 s390_nill (code, sync_reg, ~0x3);
885 /* is synchronization null? */
886 s390_ltgr (code, sync_reg, sync_reg);
887 /* if yes, jump to actual trampoline */
888 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
890 /* next case: synchronization is not null */
891 /* load MonoInternalThread* into r5 */
892 s390_ear (code, s390_r5, 0);
893 s390_sllg(code, s390_r5, s390_r5, 0, 32);
894 s390_ear (code, s390_r5, 1);
895 /* load TID into r1 */
896 s390_lg (code, s390_r1, 0, s390_r5, mono_thread_get_tls_offset ());
897 s390_lgf (code, s390_r1, 0, s390_r1, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
898 /* is synchronization->owner == TID */
899 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
900 s390_xr (code, s390_r1, status_reg);
901 s390_tmlh (code, s390_r1, OWNER_MASK);
902 /* if not, jump to actual trampoline */
903 s390_jno (code, 0); CODEPTR(code, jump_not_owned);
905 /* next case: synchronization->owner == TID */
906 /* is synchronization->nest == 1 */
907 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
908 s390_chi (code, s390_r0, 1);
909 /* if not, jump to next case */
910 s390_jne (code, 0); CODEPTR(code, jump_next);
911 /* if yes, is synchronization->entry_count greater than zero */
912 s390_cfi (code, status_reg, ENTRY_COUNT_WAITERS);
913 /* if not, jump to actual trampoline */
914 s390_jnz (code, 0); CODEPTR(code, jump_have_waiters);
915 /* if yes, try to set synchronization->owner to null and return */
916 /* old status in s390_r0 */
917 s390_lgfr (code, s390_r0, status_reg);
918 /* form new status */
919 s390_nilf (code, status_reg, ENTRY_COUNT_MASK);
920 /* compare and exchange */
921 s390_cs (code, s390_r0, status_reg, sync_reg, status_offset);
922 /* if not successful, jump to actual trampoline */
923 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
924 s390_br (code, s390_r14);
926 /* next case: synchronization->nest is not 1 */
927 PTRSLOT (code, jump_next);
928 /* decrease synchronization->nest and return */
929 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
930 s390_ahi (code, s390_r0, -1);
931 s390_st (code, s390_r0, 0, sync_reg, nest_offset);
932 s390_br (code, s390_r14);
934 PTRSLOT (code, jump_obj_null);
935 if (jump_sync_thin_hash)
936 PTRSLOT (code, jump_sync_thin_hash);
937 PTRSLOT (code, jump_have_waiters);
938 PTRSLOT (code, jump_not_owned);
939 PTRSLOT (code, jump_cs_failed);
940 PTRSLOT (code, jump_sync_null);
943 /* jump to the actual trampoline */
944 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
946 S390_SET (code, s390_r1, tramp);
947 s390_br (code, s390_r1);
949 mono_arch_flush_icache (code, code - buf);
950 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
951 g_assert (code - buf <= tramp_size);
954 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
959 /*========================= End of Function ========================*/