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_nullify_class_init_trampoline */
167 /* Function - Nullify a call which calls a class init trampoline*/
169 /*------------------------------------------------------------------*/
172 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
174 char patch[2] = {0x07, 0x00};
178 memcpy(code, patch, sizeof(patch));
181 /*========================= End of Function ========================*/
183 /*------------------------------------------------------------------*/
185 /* Name - mono_arch_get_nullified_class_init */
187 /* Function - Nullify a PLT entry call. */
189 /*------------------------------------------------------------------*/
192 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
196 code = buf = mono_global_codeman_reserve (16);
198 s390_br (code, s390_r14);
200 mono_arch_flush_icache (buf, code - buf);
201 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
203 *info = mono_tramp_info_create ("nullified_class_init_trampoline",
204 buf, code - buf, NULL, NULL);
209 /*========================= End of Function ========================*/
211 /*------------------------------------------------------------------*/
213 /* Name - mono_arch_create_trampoline_code */
215 /* Function - Create the designated type of trampoline according*/
216 /* to the 'tramp_type' parameter. */
218 /*------------------------------------------------------------------*/
221 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
224 guint8 *buf, *tramp, *code;
225 int i, offset, has_caller;
226 GSList *unwind_ops = NULL;
227 MonoJumpInfo *ji = NULL;
231 /* Now we'll create in 'buf' the S/390 trampoline code. This
232 is the trampoline code common to all methods */
234 code = buf = mono_global_codeman_reserve(512);
236 if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
237 (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD))
242 /*-----------------------------------------------------------
243 STEP 0: First create a non-standard function prologue with a
244 stack size big enough to save our registers.
245 -----------------------------------------------------------*/
247 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
248 s390_lgr (buf, s390_r11, s390_r15);
249 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
250 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
252 /*---------------------------------------------------------------*/
253 /* we build the MonoLMF structure on the stack - see mini-s390.h */
254 /* Keep in sync with the code in mono_arch_emit_prolog */
255 /*---------------------------------------------------------------*/
257 s390_lgr (buf, LMFReg, STK_BASE);
258 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
260 /*---------------------------------------------------------------*/
261 /* Save general and floating point registers in LMF */
262 /*---------------------------------------------------------------*/
263 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
264 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
265 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
266 s390_r11, S390_REG_SAVE_OFFSET);
268 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
269 for (i = s390_f0; i <= s390_f15; ++i) {
270 s390_std (buf, i, 0, LMFReg, offset);
271 offset += sizeof(gdouble);
274 /*----------------------------------------------------------
275 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
276 LMF. We'll need to restore it after the call to
277 's390_magic_trampoline' and before the call to the native
279 ----------------------------------------------------------*/
281 S390_SET (buf, s390_r1, mono_get_lmf_addr);
282 s390_basr (buf, s390_r14, s390_r1);
284 /*---------------------------------------------------------------*/
285 /* Set lmf.lmf_addr = jit_tls->lmf */
286 /*---------------------------------------------------------------*/
287 s390_stg (buf, s390_r2, 0, LMFReg,
288 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
290 /*---------------------------------------------------------------*/
291 /* Get current lmf */
292 /*---------------------------------------------------------------*/
293 s390_lg (buf, s390_r0, 0, s390_r2, 0);
295 /*---------------------------------------------------------------*/
296 /* Set our lmf as the current lmf */
297 /*---------------------------------------------------------------*/
298 s390_stg (buf, LMFReg, 0, s390_r2, 0);
300 /*---------------------------------------------------------------*/
301 /* Have our lmf.previous_lmf point to the last lmf */
302 /*---------------------------------------------------------------*/
303 s390_stg (buf, s390_r0, 0, LMFReg,
304 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
306 /*---------------------------------------------------------------*/
307 /* save method info */
308 /*---------------------------------------------------------------*/
309 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
310 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
312 /*---------------------------------------------------------------*/
313 /* save the current SP */
314 /*---------------------------------------------------------------*/
315 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
316 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
318 /*---------------------------------------------------------------*/
319 /* save the current IP */
320 /*---------------------------------------------------------------*/
322 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
324 s390_lghi (buf, s390_r1, 0);
326 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
328 /*---------------------------------------------------------------*/
329 /* STEP 2: call the C trampoline function */
330 /*---------------------------------------------------------------*/
334 /* Arg 1: mgreg_t *regs */
335 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
337 /* Arg 2: code (next address to the instruction that called us) */
339 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
341 s390_lghi (buf, s390_r3, 0);
344 /* Arg 3: Trampoline argument */
345 if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
346 s390_lg (buf, s390_r4, 0, LMFReg,
347 G_STRUCT_OFFSET(MonoLMF, gregs[MONO_ARCH_VTABLE_REG]));
349 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
351 /* Arg 4: trampoline address. */
352 S390_SET (buf, s390_r5, buf);
354 /* Calculate call address and call the C trampoline. Return value will be in r2 */
355 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
356 S390_SET (buf, s390_r1, tramp);
357 s390_basr (buf, s390_r14, s390_r1);
359 /* OK, code address is now on r2. Move it to r1, so that we
360 can restore r2 and use it from r1 later */
361 s390_lgr (buf, s390_r1, s390_r2);
363 /*----------------------------------------------------------
364 STEP 3: Restore the LMF
365 ----------------------------------------------------------*/
366 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
368 /*----------------------------------------------------------
369 STEP 4: call the compiled method
370 ----------------------------------------------------------*/
372 /* Restore parameter registers */
373 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
375 /* Restore the FP registers */
376 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
377 for (i = s390_f0; i <= s390_f15; ++i) {
378 s390_ld (buf, i, 0, LMFReg, offset);
379 offset += sizeof(gdouble);
382 /* Restore stack pointer and jump to the code -
383 * R14 contains the return address to our caller
385 s390_lgr (buf, STK_BASE, s390_r11);
386 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
388 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
389 s390_lgr (buf, s390_r2, s390_r1);
390 s390_br (buf, s390_r14);
392 s390_br (buf, s390_r1);
395 /* Flush instruction cache, since we've generated code */
396 mono_arch_flush_icache (code, buf - code);
397 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
400 tramp_name = mono_get_generic_trampoline_name (tramp_type);
401 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
405 g_assert ((buf - code) <= 512);
410 /*========================= End of Function ========================*/
412 /*------------------------------------------------------------------*/
414 /* Name - mono_arch_invalidate_method */
418 /*------------------------------------------------------------------*/
421 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
423 /* FIXME: This is not thread safe */
424 guint8 *code = ji->code_start;
426 S390_SET (code, s390_r1, func);
427 S390_SET (code, s390_r2, func_arg);
428 s390_br (code, s390_r1);
432 /*========================= End of Function ========================*/
434 /*------------------------------------------------------------------*/
436 /* Name - mono_arch_create_specific_trampoline */
438 /* Function - Creates the given kind of specific trampoline */
440 /*------------------------------------------------------------------*/
443 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
445 guint8 *code, *buf, *tramp;
448 tramp = mono_get_trampoline_code (tramp_type);
450 /*----------------------------------------------------------*/
451 /* This is the method-specific part of the trampoline. Its */
452 /* purpose is to provide the generic part with the */
453 /* MonoMethod *method pointer. We'll use r1 to keep it. */
454 /*----------------------------------------------------------*/
455 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
457 switch (tramp_type) {
459 * Monitor tramps have the object in r2
461 case MONO_TRAMPOLINE_MONITOR_ENTER:
462 case MONO_TRAMPOLINE_MONITOR_ENTER_V4:
463 case MONO_TRAMPOLINE_MONITOR_EXIT:
464 s390_lgr (buf, s390_r1, s390_r2);
467 * Generic class trampoline arg is in r2
469 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
470 s390_lgr (buf, s390_r1, s390_r2);
473 S390_SET (buf, s390_r1, arg1);
475 displace = (tramp - buf) / 2;
476 s390_jg (buf, displace);
478 /* Flush instruction cache, since we've generated code */
479 mono_arch_flush_icache (code, buf - code);
480 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
481 (void *) mono_get_generic_trampoline_simple_name (tramp_type));
484 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
487 *code_len = buf - code;
492 /*========================= End of Function ========================*/
494 /*------------------------------------------------------------------*/
496 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
500 /*------------------------------------------------------------------*/
503 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
507 guint8 **rgctx_null_jumps;
515 MonoJumpInfo *ji = NULL;
516 GSList *unwind_ops = NULL;
518 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
519 index = MONO_RGCTX_SLOT_INDEX (slot);
521 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
522 for (depth = 0; ; ++depth) {
523 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
525 if (index < size - 1)
530 tramp_size = 48 + 16 * depth;
536 code = buf = mono_global_codeman_reserve (tramp_size);
538 unwind_ops = mono_arch_get_cie_program ();
540 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
544 s390_lgr (code, s390_r1, s390_r2);
546 /* load rgctx ptr from vtable */
547 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
548 /* is the rgctx ptr null? */
549 s390_ltgr (code, s390_r1, s390_r1);
550 /* if yes, jump to actual trampoline */
551 rgctx_null_jumps [iPatch++] = code;
555 for (i = 0; i < depth; ++i) {
556 /* load ptr to next array */
557 if (mrgctx && i == 0)
558 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
560 s390_lg (code, s390_r1, 0, s390_r1, 0);
561 s390_ltgr (code, s390_r1, s390_r1);
562 /* if the ptr is null then jump to actual trampoline */
563 rgctx_null_jumps [iPatch++] = code;
568 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
569 /* is the slot null? */
570 s390_ltgr (code, s390_r1, s390_r1);
571 /* if yes, jump to actual trampoline */
572 rgctx_null_jumps [iPatch++] = code;
574 /* otherwise return r1 */
575 s390_lgr (code, s390_r2, s390_r1);
576 s390_br (code, s390_r14);
578 for (i = 0; i < iPatch; i++) {
579 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
580 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
583 g_free (rgctx_null_jumps);
585 /* move the rgctx pointer to the VTABLE register */
586 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
588 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
589 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
591 /* jump to the actual trampoline */
592 displace = (tramp - code) / 2;
593 s390_jg (code, displace);
595 mono_arch_flush_icache (buf, code - buf);
596 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
598 g_assert (code - buf <= tramp_size);
600 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
601 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
607 /*========================= End of Function ========================*/
609 /*------------------------------------------------------------------*/
611 /* Name - mono_arch_get_static_rgctx_trampoline */
613 /* Function - Create a trampoline which sets RGCTX_REG to MRGCTX*/
614 /* then jumps to ADDR. */
616 /*------------------------------------------------------------------*/
619 mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
620 MonoMethodRuntimeGenericContext *mrgctx,
623 guint8 *code, *start;
627 MonoDomain *domain = mono_domain_get ();
631 start = code = mono_domain_code_reserve (domain, buf_len);
633 S390_SET (code, MONO_ARCH_RGCTX_REG, mrgctx);
634 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
635 s390_jg (code, displace);
636 g_assert ((code - start) < buf_len);
638 mono_arch_flush_icache (start, code - start);
639 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
644 /*========================= End of Function ========================*/
646 /*------------------------------------------------------------------*/
648 /* Name - handler_block_trampoline_helper */
652 /*------------------------------------------------------------------*/
655 handler_block_trampoline_helper (gpointer *ptr)
657 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
658 *ptr = jit_tls->handler_block_return_address;
661 /*========================= End of Function ========================*/
663 /*------------------------------------------------------------------*/
665 /* Name - mono_arch_create_handler_block_trampoline */
669 /*------------------------------------------------------------------*/
672 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
674 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
677 MonoJumpInfo *ji = NULL;
678 GSList *unwind_ops = NULL;
682 code = buf = mono_global_codeman_reserve (tramp_size);
685 * This trampoline restore the call chain of the handler block
686 * then jumps into the code that deals with it.
689 if (mono_get_jit_tls_offset () != -1) {
690 s390_ear (code, s390_r1, 0);
691 s390_sllg (code, s390_r1, s390_r1, 0, 32);
692 s390_ear (code, s390_r1, 1);
693 S390_SET (code, s390_r14, mono_get_jit_tls_offset());
694 s390_lg (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
698 S390_SET (code, s390_r1, tramp);
699 s390_br (code, s390_r1);
702 * Slow path uses a C helper
704 S390_SET (code, s390_r2, tramp);
705 S390_SET (code, s390_r1, handler_block_trampoline_helper);
706 s390_br (code, s390_r1);
709 mono_arch_flush_icache (buf, code - buf);
710 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
711 g_assert (code - buf <= tramp_size);
713 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
718 /*========================= End of Function ========================*/
720 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
721 /*------------------------------------------------------------------*/
723 /* Name - mono_arch_create_monitor_enter_trampoline */
727 /*------------------------------------------------------------------*/
730 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
734 gint16 *jump_obj_null,
739 *jump_sync_thin_hash = NULL,
740 *jump_lock_taken_true = NULL;
742 status_reg = s390_r0,
743 lock_taken_reg = s390_r1,
749 MonoJumpInfo *ji = NULL;
750 GSList *unwind_ops = NULL;
752 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
753 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
754 g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
759 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
760 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
761 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
762 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
763 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
767 code = buf = mono_global_codeman_reserve (tramp_size);
769 unwind_ops = mono_arch_get_cie_program ();
771 if (mono_thread_get_tls_offset () != -1) {
772 /* MonoObject* obj is in obj_reg */
774 s390_ltgr (code, obj_reg, obj_reg);
775 /* if yes, jump to actual trampoline */
776 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
779 s390_cli (code, lock_taken_reg, 0, 1);
780 /* if *lock_taken is 1, jump to actual trampoline */
781 s390_je (code, 0); CODEPTR(code, jump_lock_taken_true);
784 /* load obj->synchronization to sync_reg */
785 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
787 if (mono_gc_is_moving ()) {
788 /*if bit zero is set it's a thin hash*/
789 s390_tmll (code, sync_reg, 1);
790 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
792 /* Clear bits used by the gc */
793 s390_nill (code, sync_reg, ~0x3);
796 /* is synchronization null? */
797 s390_ltgr (code, sync_reg, sync_reg);
798 /* if yes, jump to actual trampoline */
799 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
801 /* load MonoInternalThread* into tid_reg */
802 s390_ear (code, s390_r5, 0);
803 s390_sllg(code, s390_r5, s390_r5, 0, 32);
804 s390_ear (code, s390_r5, 1);
806 s390_lg (code, tid_reg, 0, s390_r5, mono_thread_get_tls_offset ());
807 s390_lgf (code, tid_reg, 0, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
809 /* is synchronization->owner free */
810 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
811 s390_nilf (code, status_reg, OWNER_MASK);
812 /* if not, jump to next case */
813 s390_jnz (code, 0); CODEPTR(code, jump_tid);
815 /* if yes, try a compare-exchange with the TID */
816 /* Form new status in tid_reg */
817 s390_xr (code, tid_reg, status_reg);
818 /* compare and exchange */
819 s390_cs (code, status_reg, tid_reg, sync_reg, status_offset);
820 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
821 /* if successful, return */
823 s390_mvi (code, lock_taken_reg, 0, 1);
824 s390_br (code, s390_r14);
826 /* next case: synchronization->owner is not null */
827 PTRSLOT(code, jump_tid);
828 /* is synchronization->owner == TID? */
829 s390_nilf (code, status_reg, OWNER_MASK);
830 s390_cr (code, status_reg, tid_reg);
831 /* if not, jump to actual trampoline */
832 s390_jnz (code, 0); CODEPTR(code, jump_other_owner);
833 /* if yes, increment nest */
834 s390_lgf (code, s390_r5, 0, sync_reg, nest_offset);
835 s390_ahi (code, s390_r5, 1);
836 s390_st (code, s390_r5, 0, sync_reg, nest_offset);
839 s390_mvi (code, lock_taken_reg, 0, 1);
840 s390_br (code, s390_r14);
842 PTRSLOT (code, jump_obj_null);
843 if (jump_sync_thin_hash)
844 PTRSLOT (code, jump_sync_thin_hash);
845 PTRSLOT (code, jump_sync_null);
846 PTRSLOT (code, jump_cs_failed);
847 PTRSLOT (code, jump_other_owner);
849 PTRSLOT (code, jump_lock_taken_true);
852 /* jump to the actual trampoline */
854 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
856 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
858 /* jump to the actual trampoline */
859 S390_SET (code, s390_r1, tramp);
860 s390_br (code, s390_r1);
862 mono_arch_flush_icache (code, code - buf);
863 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
864 g_assert (code - buf <= tramp_size);
868 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
870 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
876 /*========================= End of Function ========================*/
878 /*------------------------------------------------------------------*/
880 /* Name - mono_arch_create_monitor_exit_trampoline */
884 /*------------------------------------------------------------------*/
887 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
891 gint16 *jump_obj_null,
897 *jump_sync_thin_hash = NULL;
899 status_offset, nest_offset;
900 MonoJumpInfo *ji = NULL;
901 GSList *unwind_ops = NULL;
902 int obj_reg = s390_r2,
904 status_reg = s390_r4;
906 g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
908 mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
909 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
910 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
911 status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
912 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
916 code = buf = mono_global_codeman_reserve (tramp_size);
918 unwind_ops = mono_arch_get_cie_program ();
920 if (mono_thread_get_tls_offset () != -1) {
921 /* MonoObject* obj is in obj_reg */
923 s390_ltgr (code, obj_reg, obj_reg);
924 /* if yes, jump to actual trampoline */
925 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
927 /* load obj->synchronization to RCX */
928 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
930 if (mono_gc_is_moving ()) {
931 /*if bit zero is set it's a thin hash*/
932 s390_tmll (code, sync_reg, 1);
933 s390_jo (code, 0); CODEPTR(code, jump_sync_thin_hash);
935 /* Clear bits used by the gc */
936 s390_nill (code, sync_reg, ~0x3);
939 /* is synchronization null? */
940 s390_ltgr (code, sync_reg, sync_reg);
941 /* if yes, jump to actual trampoline */
942 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
944 /* next case: synchronization is not null */
945 /* load MonoInternalThread* into r5 */
946 s390_ear (code, s390_r5, 0);
947 s390_sllg(code, s390_r5, s390_r5, 0, 32);
948 s390_ear (code, s390_r5, 1);
949 /* load TID into r1 */
950 s390_lg (code, s390_r1, 0, s390_r5, mono_thread_get_tls_offset ());
951 s390_lgf (code, s390_r1, 0, s390_r1, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
952 /* is synchronization->owner == TID */
953 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
954 s390_xr (code, s390_r1, status_reg);
955 s390_tmlh (code, s390_r1, OWNER_MASK);
956 /* if not, jump to actual trampoline */
957 s390_jno (code, 0); CODEPTR(code, jump_not_owned);
959 /* next case: synchronization->owner == TID */
960 /* is synchronization->nest == 1 */
961 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
962 s390_chi (code, s390_r0, 1);
963 /* if not, jump to next case */
964 s390_jne (code, 0); CODEPTR(code, jump_next);
965 /* if yes, is synchronization->entry_count greater than zero */
966 s390_cfi (code, status_reg, ENTRY_COUNT_WAITERS);
967 /* if not, jump to actual trampoline */
968 s390_jnz (code, 0); CODEPTR(code, jump_have_waiters);
969 /* if yes, try to set synchronization->owner to null and return */
970 /* old status in s390_r0 */
971 s390_lgfr (code, s390_r0, status_reg);
972 /* form new status */
973 s390_nilf (code, status_reg, ENTRY_COUNT_MASK);
974 /* compare and exchange */
975 s390_cs (code, s390_r0, status_reg, sync_reg, status_offset);
976 /* if not successful, jump to actual trampoline */
977 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
978 s390_br (code, s390_r14);
980 /* next case: synchronization->nest is not 1 */
981 PTRSLOT (code, jump_next);
982 /* decrease synchronization->nest and return */
983 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
984 s390_ahi (code, s390_r0, -1);
985 s390_st (code, s390_r0, 0, sync_reg, nest_offset);
986 s390_br (code, s390_r14);
988 PTRSLOT (code, jump_obj_null);
989 if (jump_sync_thin_hash)
990 PTRSLOT (code, jump_sync_thin_hash);
991 PTRSLOT (code, jump_have_waiters);
992 PTRSLOT (code, jump_not_owned);
993 PTRSLOT (code, jump_cs_failed);
994 PTRSLOT (code, jump_sync_null);
997 /* jump to the actual trampoline */
998 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1000 S390_SET (code, s390_r1, tramp);
1001 s390_br (code, s390_r1);
1003 mono_arch_flush_icache (code, code - buf);
1004 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
1005 g_assert (code - buf <= tramp_size);
1008 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
1013 /*========================= End of Function ========================*/