Add support for mono_arch_create_monitor_[enter|exit]_trampoline
authorNeale Ferguson <neale@sinenomine.net>
Thu, 16 Apr 2015 20:14:19 +0000 (16:14 -0400)
committerNeale Ferguson <neale@sinenomine.net>
Thu, 16 Apr 2015 20:14:19 +0000 (16:14 -0400)
mono/arch/s390x/s390x-codegen.h
mono/mini/mini-s390x.c
mono/mini/mini-s390x.h
mono/mini/tramp-s390x.c

index 8c9244d33331cbab54ed00150db8918e6423e4e0..cd3df077ff64502059fea31dc138f0528c5b03c4 100644 (file)
@@ -759,6 +759,7 @@ typedef struct {
 #define s390_clgr(c, r1, r2)           S390_RRE(c, 0xb921, r1, r2)
 #define s390_clgrj(c, r1, r2, m, v)    S390_RIE_2(c, 0xec65, r1, r2, m, v)
 #define s390_clgrb(c, r1, r2, m3, b, d)        S390_RRS(c, 0xece5, r1, r2, m3, b, d)
+#define s390_cli(c, b, d, v)           S390_SI(c, 0x95, b, d, v)
 #define s390_clib(c, r, i, m, b, d)    S390_RIS(c, 0xecff, r, i, m, b, d)
 #define s390_clij(c, r, i, b)          S390_RIE_3(c, 0xec7f, r, i, m, d)
 #define s390_clr(c, r1, r2)            S390_RR(c, 0x15, r1, r2)
@@ -915,6 +916,7 @@ typedef struct {
 #define s390_mvc(c, l, b1, d1, b2, d2) S390_SS_1(c, 0xd2, l, b1, d1, b2, d2)
 #define s390_mvcl(c, r1, r2)           S390_RR(c, 0x0e, r1, r2)
 #define s390_mvcle(c, r1, r3, d2, b2)  S390_RS_1(c, 0xa8, r1, r3, d2, b2)
+#define s390_mvi(c, b, d, v)           S390_SI(c, 0x92, b, d, v)
 #define s390_n(c, r, x, b, d)          S390_RX(c, 0x54, r, x, b, d)
 #define s390_nc(c, l, b1, d1, b2, d2)  S390_SS_1(c, 0xd4, l, b1, d1, b2, d2)
 #define s390_ng(c, r, x, b, d)         S390_RXY(c, 0xe380, r, x, b, d)
@@ -996,6 +998,10 @@ typedef struct {
 #define s390_sty(c, r, x, b, d)                S390_RXY(c, 0xe350, r, x, b, d)
 #define s390_tcdb(c, r, x, b, d)       S390_RXE(c, 0xed11, r, x, b, d)
 #define s390_tceb(c, r, x, b, d)       S390_RXE(c, 0xed10, r, x, b, d)
+#define s390_tmhh(c, r, m)             S390_RI(c, 0xa73, r, m)
+#define s390_tmhl(c, r, m)             S390_RI(c, 0xa72, r, m)
+#define s390_tmlh(c, r, m)             S390_RI(c, 0xa70, r, m)
+#define s390_tmll(c, r, m)             S390_RI(c, 0xa71, r, m)
 #define s390_x(c, r, x, b, d)          S390_RX(c, 0x57, r, x, b, d)
 #define s390_xihf(c, r, v)             S390_RIL_1(c, 0xc06, r, v)
 #define s390_xilf(c, r, v)             S390_RIL_1(c, 0xc07, r, v)
index 004540fb7e2bc7bbf8790e10411aee7e0a852a2d..02d93488e078e7c74faa6ac6af22d261898136cc 100644 (file)
@@ -4669,6 +4669,9 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
                        case MONO_PATCH_INFO_CLASS_INIT:
                        case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
                        case MONO_PATCH_INFO_RGCTX_FETCH:
+                       case MONO_PATCH_INFO_MONITOR_ENTER:
+                       case MONO_PATCH_INFO_MONITOR_ENTER_V4:
+                       case MONO_PATCH_INFO_MONITOR_EXIT:
                        case MONO_PATCH_INFO_ABS: {
                                S390_EMIT_CALL (ip, target);
                                continue;
index 7de88fdf1e2ca5018981c576fb841f1a12df9cf0..26ec96de7cb582a6676c1016b34f22ae377a2e6b 100644 (file)
@@ -62,6 +62,8 @@ typedef struct
 #define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT             1
 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD             1
 #define MONO_ARCH_HAVE_INVALIDATE_METHOD               1
+#define MONO_ARCH_MONITOR_OBJECT_REG                   s390_r2
+#define MONO_ARCH_LOCK_TAKEN_REG                       s390_r1
 
 #define S390_STACK_ALIGNMENT            8
 #define S390_FIRST_ARG_REG             s390_r2
index d30ea460efb3a934a8f7a7b221ffa885f20e02a8..14a96653d67c2f7f1ffcd2398bf287d29589087e 100644 (file)
 
 #include <mono/metadata/abi-details.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/gc-internal.h>
 #include <mono/metadata/marshal.h>
-#include <mono/metadata/tabledefs.h>
+#include <mono/metadata/monitor.h>
 #include <mono/metadata/profiler-private.h>
+#include <mono/metadata/tabledefs.h>
 #include <mono/arch/s390x/s390x-codegen.h>
 
 #include "mini.h"
@@ -358,7 +360,8 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        else
                s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
 
-       /* Arg 4: trampoline address. Ignore for now */
+       /* Arg 4: trampoline address. */
+       S390_SET (buf, s390_r5, buf);
                
        /* Calculate call address and call the C trampoline. Return value will be in r2 */
        tramp = (guint8*)mono_get_trampoline_func (tramp_type);
@@ -463,7 +466,18 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        /*----------------------------------------------------------*/
        code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
 
-       S390_SET  (buf, s390_r1, arg1);
+       switch (tramp_type) {
+       /*
+        * Monitor tramps have the object in r2
+        */
+       case MONO_TRAMPOLINE_MONITOR_ENTER:
+       case MONO_TRAMPOLINE_MONITOR_ENTER_V4:
+       case MONO_TRAMPOLINE_MONITOR_EXIT:
+               s390_lgr (buf, s390_r1, s390_r2);
+               break;
+       default :
+               S390_SET  (buf, s390_r1, arg1);
+       }
        displace = (tramp - buf) / 2;
        s390_jg   (buf, displace);
 
@@ -764,3 +778,299 @@ mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean a
 }
 
 /*========================= End of Function ========================*/
+
+#ifdef MONO_ARCH_MONITOR_OBJECT_REG
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_create_monitor_enter_trampoline         */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
+{
+       guint8  *tramp,
+               *code, *buf;
+       gint16  *jump_obj_null, 
+               *jump_sync_null, 
+               *jump_cs_failed, 
+               *jump_other_owner, 
+               *jump_tid, 
+               *jump_sync_thin_hash = NULL,
+               *jump_lock_taken_true = NULL;
+       int tramp_size,
+           status_reg = s390_r0,
+           lock_taken_reg = s390_r1,
+           obj_reg = s390_r2,
+           sync_reg = s390_r3,
+           tid_reg = s390_r4,
+           status_offset,
+           nest_offset;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
+
+       g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
+#ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
+       g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
+#else
+       g_assert (!is_v4);
+#endif
+
+       mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
+       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
+       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
+       status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
+       nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
+
+       tramp_size = 160;
+
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       unwind_ops = mono_arch_get_cie_program ();
+
+       if (mono_thread_get_tls_offset () != -1) {
+               /* MonoObject* obj is in obj_reg */
+               /* is obj null? */
+               s390_ltgr (code, obj_reg, obj_reg);
+               /* if yes, jump to actual trampoline */
+               s390_jz (code, 0); CODEPTR(code, jump_obj_null);
+
+               if (is_v4) {
+                       s390_cli (code, lock_taken_reg, 0, 1);
+                       /* if *lock_taken is 1, jump to actual trampoline */
+                       s390_je (code, 0); CODEPTR(code, jump_lock_taken_true);
+               }
+
+               /* load obj->synchronization to sync_reg */
+               s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
+
+               if (mono_gc_is_moving ()) {
+                       /*if bit zero is set it's a thin hash*/
+                       s390_tmll (code, sync_reg, 1);
+                       s390_jo  (code, 0); CODEPTR(code, jump_sync_thin_hash);
+
+                       /* Clear bits used by the gc */
+                       s390_nill (code, sync_reg, ~0x3);
+               }
+
+               /* is synchronization null? */
+               s390_ltgr (code, sync_reg, sync_reg);
+               /* if yes, jump to actual trampoline */
+               s390_jz (code, 0); CODEPTR(code, jump_sync_null);
+
+               /* load MonoInternalThread* into tid_reg */
+               s390_ear (code, s390_r5, 0);
+               s390_sllg(code, s390_r5, s390_r5, 0, 32);
+               s390_ear (code, s390_r5, 1);
+               /* load tid */
+               s390_lg  (code, tid_reg, 0, s390_r5, mono_thread_get_tls_offset ());
+               s390_lgf (code, tid_reg, 0, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
+
+               /* is synchronization->owner free */
+               s390_lgf  (code, status_reg, 0, sync_reg, status_offset);
+               s390_nilf (code, status_reg, OWNER_MASK);
+               /* if not, jump to next case */
+               s390_jnz  (code, 0); CODEPTR(code, jump_tid);
+
+               /* if yes, try a compare-exchange with the TID */
+               /* Form new status in tid_reg */
+               s390_xr (code, tid_reg, status_reg);
+               /* compare and exchange */
+               s390_cs (code, status_reg, tid_reg, sync_reg, status_offset);
+               s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
+               /* if successful, return */
+               if (is_v4)
+                       s390_mvi (code, lock_taken_reg, 0, 1);
+               s390_br (code, s390_r14);
+
+               /* next case: synchronization->owner is not null */
+               PTRSLOT(code, jump_tid);
+               /* is synchronization->owner == TID? */
+               s390_nilf (code, status_reg, OWNER_MASK);
+               s390_cr (code, status_reg, tid_reg);
+               /* if not, jump to actual trampoline */
+               s390_jnz (code, 0); CODEPTR(code, jump_other_owner);
+               /* if yes, increment nest */
+               s390_lgf (code, s390_r5, 0, sync_reg, nest_offset);
+               s390_ahi (code, s390_r5, 1);
+               s390_st  (code, s390_r5, 0, sync_reg, nest_offset);
+               /* return */
+               if (is_v4)
+                       s390_mvi (code, lock_taken_reg, 0, 1);
+               s390_br (code, s390_r14);
+
+               PTRSLOT (code, jump_obj_null);
+               if (jump_sync_thin_hash)
+                       PTRSLOT (code, jump_sync_thin_hash);
+               PTRSLOT (code, jump_sync_null);
+               PTRSLOT (code, jump_cs_failed);
+               PTRSLOT (code, jump_other_owner);
+               if (is_v4)
+                       PTRSLOT (code, jump_lock_taken_true);
+       }
+
+       /* jump to the actual trampoline */
+       if (is_v4)
+               tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
+       else
+               tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
+
+       /* jump to the actual trampoline */
+       S390_SET (code, s390_r1, tramp);
+       s390_br (code, s390_r1);
+
+       mono_arch_flush_icache (code, code - buf);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
+       g_assert (code - buf <= tramp_size);
+
+       if (info) {
+               if (is_v4)
+                       *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
+               else
+                       *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
+       }
+
+       return buf;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_create_monitor_exit_trampoline          */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
+{
+       guint8  *tramp,
+               *code, *buf;
+       gint16  *jump_obj_null, 
+               *jump_have_waiters, 
+               *jump_sync_null, 
+               *jump_not_owned, 
+               *jump_cs_failed,
+               *jump_next,
+               *jump_sync_thin_hash = NULL;
+       int     tramp_size,
+               status_offset, nest_offset;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
+       int     obj_reg = s390_r2,
+               sync_reg = s390_r3,
+               status_reg = s390_r4;
+
+       g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
+
+       mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
+       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
+       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
+       status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
+       nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
+
+       tramp_size = 160;
+
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       unwind_ops = mono_arch_get_cie_program ();
+
+       if (mono_thread_get_tls_offset () != -1) {
+               /* MonoObject* obj is in obj_reg */
+               /* is obj null? */
+               s390_ltgr (code, obj_reg, obj_reg);
+               /* if yes, jump to actual trampoline */
+               s390_jz (code, 0); CODEPTR(code, jump_obj_null);
+
+               /* load obj->synchronization to RCX */
+               s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
+
+               if (mono_gc_is_moving ()) {
+                       /*if bit zero is set it's a thin hash*/
+                       s390_tmll (code, sync_reg, 1);
+                       s390_jo   (code, 0); CODEPTR(code, jump_sync_thin_hash);
+
+                       /* Clear bits used by the gc */
+                       s390_nill (code, sync_reg, ~0x3);
+               }
+
+               /* is synchronization null? */
+               s390_ltgr (code, sync_reg, sync_reg);
+               /* if yes, jump to actual trampoline */
+               s390_jz (code, 0); CODEPTR(code, jump_sync_null);
+
+               /* next case: synchronization is not null */
+               /* load MonoInternalThread* into r5 */
+               s390_ear (code, s390_r5, 0);
+               s390_sllg(code, s390_r5, s390_r5, 0, 32);
+               s390_ear (code, s390_r5, 1);
+               /* load TID into r1 */
+               s390_lg  (code, s390_r1, 0, s390_r5, mono_thread_get_tls_offset ());
+               s390_lgf (code, s390_r1, 0, s390_r1, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
+               /* is synchronization->owner == TID */
+               s390_lgf (code, status_reg, 0, sync_reg, status_offset);
+               s390_xr  (code, s390_r1, status_reg);
+               s390_tmlh (code, s390_r1, OWNER_MASK);
+               /* if not, jump to actual trampoline */
+               s390_jno (code, 0); CODEPTR(code, jump_not_owned);
+
+               /* next case: synchronization->owner == TID */
+               /* is synchronization->nest == 1 */
+               s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
+               s390_chi (code, s390_r0, 1);
+               /* if not, jump to next case */
+               s390_jne (code, 0); CODEPTR(code, jump_next);
+               /* if yes, is synchronization->entry_count greater than zero */
+               s390_cfi (code, status_reg, ENTRY_COUNT_WAITERS);
+               /* if not, jump to actual trampoline */
+               s390_jnz (code, 0); CODEPTR(code, jump_have_waiters);
+               /* if yes, try to set synchronization->owner to null and return */
+               /* old status in s390_r0 */
+               s390_lgfr (code, s390_r0, status_reg);
+               /* form new status */
+               s390_nilf (code, status_reg, ENTRY_COUNT_MASK);
+               /* compare and exchange */
+               s390_cs (code, s390_r0, status_reg, sync_reg, status_offset);
+               /* if not successful, jump to actual trampoline */
+               s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
+               s390_br  (code, s390_r14);
+
+               /* next case: synchronization->nest is not 1 */
+               PTRSLOT (code, jump_next);
+               /* decrease synchronization->nest and return */
+               s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
+               s390_ahi (code, s390_r0, -1);
+               s390_st  (code, s390_r0, 0, sync_reg, nest_offset);
+               s390_br  (code, s390_r14);
+
+               PTRSLOT (code, jump_obj_null);
+               if (jump_sync_thin_hash)
+                       PTRSLOT (code, jump_sync_thin_hash);
+               PTRSLOT (code, jump_have_waiters);
+               PTRSLOT (code, jump_not_owned);
+               PTRSLOT (code, jump_cs_failed);
+               PTRSLOT (code, jump_sync_null);
+       }
+
+       /* jump to the actual trampoline */
+       tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
+
+       S390_SET (code, s390_r1, tramp);
+       s390_br (code, s390_r1);
+
+       mono_arch_flush_icache (code, code - buf);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
+       g_assert (code - buf <= tramp_size);
+
+       if (info)
+               *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
+
+       return buf;
+}
+
+/*========================= End of Function ========================*/
+#endif