Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mono / mini / tramp-s390x.c
index fe25878f14756bb2b14c4a65d32c84fcfd6e0786..98dbc887fd71199ca4f4627e8e60b7a802429c3a 100644 (file)
 /*                 D e f i n e s                                    */
 /*------------------------------------------------------------------*/
 
-#define GR_SAVE_SIZE           4*sizeof(long)
-#define FP_SAVE_SIZE           16*sizeof(double)
-#define METHOD_SAVE_OFFSET     S390_MINIMAL_STACK_SIZE
-#define CREATE_GR_OFFSET       METHOD_SAVE_OFFSET+8
-#define CREATE_FP_OFFSET       CREATE_GR_OFFSET+GR_SAVE_SIZE
-#define CREATE_LMF_OFFSET      CREATE_FP_OFFSET+FP_SAVE_SIZE
-#define CREATE_STACK_SIZE      (CREATE_LMF_OFFSET+2*sizeof(long)+sizeof(MonoLMF))
-#define GENERIC_REG_OFFSET     CREATE_STACK_SIZE + \
-                               S390_REG_SAVE_OFFSET + \
-                               3*sizeof(long)
+#define LMFReg s390_r13
 
-/*------------------------------------------------------------------*/
-/* Method-specific trampoline code fragment sizes                  */
-/*------------------------------------------------------------------*/
+/*
+ * Method-specific trampoline code fragment sizes                  
+ */
 #define SPECIFIC_TRAMPOLINE_SIZE       96
 
 /*========================= End of Defines =========================*/
 #include <glib.h>
 #include <string.h>
 
+#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/monitor.h>
+#include <mono/metadata/profiler-private.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/arch/s390x/s390x-codegen.h>
 
 #include "mini.h"
 #include "mini-s390x.h"
+#include "support-s390x.h"
 
 /*========================= End of Includes ========================*/
 
 /*                 T y p e d e f s                                  */
 /*------------------------------------------------------------------*/
 
+typedef struct {
+       guint8  stk[S390_MINIMAL_STACK_SIZE];   /* Standard s390x stack */
+       struct MonoLMF  LMF;                    /* LMF                  */
+} trampStack_t;
+
 /*========================= End of Typedefs ========================*/
 
 /*------------------------------------------------------------------*/
@@ -99,16 +100,14 @@ mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
 
        start = code = mono_domain_code_reserve (domain, 28);
 
-       s390_basr (code, s390_r1, 0);
-       s390_j    (code, 6);
-       s390_llong(code, addr);
-       s390_lg   (code, s390_r1, 0, s390_r1, 4);
+       S390_SET  (code, s390_r1, addr);
        s390_aghi (code, this_pos, sizeof(MonoObject));
        s390_br   (code, s390_r1);
 
        g_assert ((code - start) <= 28);
 
        mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
 
        return start;
 }
@@ -129,27 +128,17 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
        gint32 displace;
        unsigned short opcode;
 
-       opcode = *((unsigned short *) (orig_code - 6));
-       if (opcode == 0xc0e5) {
+       opcode = *((unsigned short *) (orig_code - 2));
+       if (opcode == 0x0dee) {
+               /* This should be a 'iihf/iilf' sequence */
+               S390_EMIT_CALL((orig_code - 14), addr);
+               mono_arch_flush_icache (orig_code - 14, 12);
+       } else {
                /* This is the 'brasl' instruction */
                orig_code    -= 4;
                displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
                s390_patch_rel (orig_code, displace);
                mono_arch_flush_icache (orig_code, 4);
-       } else {
-               /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
-               g_assert (orig_code [-8] == 0xe3);
-               g_assert (orig_code [-7] == 0xe0);
-               g_assert (orig_code [-6] == 0xd0);
-               g_assert (orig_code [-5] == 0x04);
-               g_assert (orig_code [-4] == 0x00);
-               g_assert (orig_code [-3] == 0x04);
-               opcode = *((unsigned short*) (orig_code - 2));
-               g_assert (opcode == 0x0dee);
-
-               /* The call address is stored in the 8 bytes preceeding the basr instruction */
-               s390_patch_addr(orig_code - 16, (gssize)addr);
-               mono_arch_flush_icache (orig_code - 16, 8);
        }
 }
 
@@ -193,16 +182,28 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_nullify_plt_entry                       */
+/* Name                - mono_arch_get_nullified_class_init                */
 /*                                                                  */
 /* Function    - Nullify a PLT entry call.                         */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-void
-mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
+gpointer
+mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
 {
-       g_assert_not_reached ();
+       guint8 *buf, *code;
+
+       code = buf = mono_global_codeman_reserve (16);
+
+       s390_br (code, s390_r14);
+
+       mono_arch_flush_icache (buf, code - buf);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+
+       *info = mono_tramp_info_create ("nullified_class_init_trampoline", 
+                                                                       buf, code - buf, NULL, NULL);
+
+       return (buf);
 }
 
 /*========================= End of Function ========================*/
@@ -221,7 +222,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 {
        char *tramp_name;
        guint8 *buf, *tramp, *code;
-       int i, offset, lmfOffset;
+       int i, offset, has_caller;
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
 
@@ -232,6 +233,12 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                
        code = buf = mono_global_codeman_reserve(512);
                
+       if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
+           (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)) 
+               has_caller = 0;
+       else
+               has_caller = 1;
+
        /*-----------------------------------------------------------
          STEP 0: First create a non-standard function prologue with a
          stack size big enough to save our registers.
@@ -239,16 +246,29 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                
        s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
        s390_lgr  (buf, s390_r11, s390_r15);
-       s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
+       s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
        s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
-       s390_stg  (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
-       s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
 
-       /* Save the FP registers */
-       offset = CREATE_FP_OFFSET;
+       /*---------------------------------------------------------------*/
+       /* we build the MonoLMF structure on the stack - see mini-s390.h */
+       /* Keep in sync with the code in mono_arch_emit_prolog           */
+       /*---------------------------------------------------------------*/
+                                                                                       
+       s390_lgr   (buf, LMFReg, STK_BASE);
+       s390_aghi  (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
+                                                                                       
+       /*---------------------------------------------------------------*/     
+       /* Save general and floating point registers in LMF              */     
+       /*---------------------------------------------------------------*/     
+       s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
+       s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
+       s390_mvc  (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
+                  s390_r11, S390_REG_SAVE_OFFSET);
+
+       offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
        for (i = s390_f0; i <= s390_f15; ++i) {
-               s390_std  (buf, i, 0, STK_BASE, offset);
-               offset += 8;
+               s390_std  (buf, i, 0, LMFReg, offset);
+               offset += sizeof(gdouble);
        }
 
        /*----------------------------------------------------------
@@ -258,25 +278,13 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
          method.
          ----------------------------------------------------------*/
                                
-       s390_basr (buf, s390_r13, 0);
-       s390_j    (buf, 6);
-       s390_llong(buf, mono_get_lmf_addr);
-       s390_lg   (buf, s390_r1, 0, s390_r13, 4);
+       S390_SET  (buf, s390_r1, mono_get_lmf_addr);
        s390_basr (buf, s390_r14, s390_r1);
-
-       /*---------------------------------------------------------------*/
-       /* we build the MonoLMF structure on the stack - see mini-s390.h */
-       /* Keep in sync with the code in mono_arch_emit_prolog           */
-       /*---------------------------------------------------------------*/
-       lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
-                                                                                       
-       s390_lgr   (buf, s390_r13, STK_BASE);
-       s390_aghi  (buf, s390_r13, lmfOffset);  
                                                                                        
        /*---------------------------------------------------------------*/     
        /* Set lmf.lmf_addr = jit_tls->lmf                               */     
        /*---------------------------------------------------------------*/     
-       s390_stg   (buf, s390_r2, 0, s390_r13,                          
+       s390_stg   (buf, s390_r2, 0, LMFReg,                            
                            G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
                                                                                        
        /*---------------------------------------------------------------*/     
@@ -287,80 +295,65 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        /*---------------------------------------------------------------*/     
        /* Set our lmf as the current lmf                                */     
        /*---------------------------------------------------------------*/     
-       s390_stg   (buf, s390_r13, 0, s390_r2, 0);                              
+       s390_stg   (buf, LMFReg, 0, s390_r2, 0);                                
                                                                                        
        /*---------------------------------------------------------------*/     
        /* Have our lmf.previous_lmf point to the last lmf               */     
        /*---------------------------------------------------------------*/     
-       s390_stg   (buf, s390_r0, 0, s390_r13,                          
+       s390_stg   (buf, s390_r0, 0, LMFReg,                            
                            G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
                                                                                        
        /*---------------------------------------------------------------*/     
        /* save method info                                              */     
        /*---------------------------------------------------------------*/     
-       s390_lg    (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
-       s390_stg   (buf, s390_r1, 0, s390_r13,                          
-                           G_STRUCT_OFFSET(MonoLMF, method));                          
+       s390_lg    (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
+       s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));                         
                                                                        
        /*---------------------------------------------------------------*/     
        /* save the current SP                                           */     
        /*---------------------------------------------------------------*/     
        s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
-       s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));  
+       s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));    
                                                                        
        /*---------------------------------------------------------------*/     
        /* save the current IP                                           */     
        /*---------------------------------------------------------------*/     
-       if (tramp_type == MONO_TRAMPOLINE_JUMP) {
-               s390_lghi  (buf, s390_r1, 0);
-       } else {
+       if (has_caller) {
                s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
-               //                      s390_la    (buf, s390_r1, 0, s390_r1, 0);
+       } else {
+               s390_lghi  (buf, s390_r1, 0);
        }
-       s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));  
+       s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));    
                                                                                        
-       /*---------------------------------------------------------------*/     
-       /* Save general and floating point registers                     */     
-       /*---------------------------------------------------------------*/     
-       s390_mvc   (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]), 
-                   STK_BASE, CREATE_GR_OFFSET);
-       s390_mvc   (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]), 
-                   s390_r11, S390_REG_SAVE_OFFSET);
-
-       /* Simply copy fpregs already saved above                        */
-       s390_mvc   (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
-                   STK_BASE, CREATE_FP_OFFSET);
-
        /*---------------------------------------------------------------*/
        /* STEP 2: call the C trampoline function                        */
        /*---------------------------------------------------------------*/
                                
        /* Set arguments */
 
-       /* Arg 1: mgreg_t *regs. We pass sp instead */
-       s390_la  (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
+       /* Arg 1: mgreg_t *regs */
+       s390_la  (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
                
        /* Arg 2: code (next address to the instruction that called us) */
-       if (tramp_type == MONO_TRAMPOLINE_JUMP) {
-               s390_lghi (buf, s390_r3, 0);
-       } else {
+       if (has_caller) {
                s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
+       } else {
+               s390_lghi (buf, s390_r3, 0);
        }
 
        /* Arg 3: Trampoline argument */
        if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
-               s390_lg (buf, s390_r4, 0, STK_BASE, GENERIC_REG_OFFSET);
+               s390_lg (buf, s390_r4, 0, LMFReg,
+                        G_STRUCT_OFFSET(MonoLMF, gregs[MONO_ARCH_VTABLE_REG]));
        else
-               s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
+               s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
 
-       /* 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 */
-       s390_basr (buf, s390_r13, 0);
-       s390_j    (buf, 6);
        tramp = (guint8*)mono_get_trampoline_func (tramp_type);
-       s390_llong (buf, tramp);
-       s390_lg   (buf, s390_r1, 0, s390_r13, 4);
+       S390_SET  (buf, s390_r1, tramp);
        s390_basr (buf, s390_r14, s390_r1);
                
        /* OK, code address is now on r2. Move it to r1, so that we
@@ -370,25 +363,25 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        /*----------------------------------------------------------
          STEP 3: Restore the LMF
          ----------------------------------------------------------*/
-       restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
+       restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
        
        /*----------------------------------------------------------
          STEP 4: call the compiled method
          ----------------------------------------------------------*/
                
-       /* Restore registers */
-
-       s390_lmg  (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
+       /* Restore parameter registers */
+       s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
                
        /* Restore the FP registers */
-       offset = CREATE_FP_OFFSET;
+       offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
        for (i = s390_f0; i <= s390_f15; ++i) {
-               s390_ld  (buf, i, 0, STK_BASE, offset);
-               offset += 8;
+               s390_ld  (buf, i, 0, LMFReg, offset);
+               offset += sizeof(gdouble);
        }
 
        /* Restore stack pointer and jump to the code -
-          R14 contains the return address to our caller */
+        * R14 contains the return address to our caller 
+        */
        s390_lgr  (buf, STK_BASE, s390_r11);
        s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
 
@@ -401,12 +394,12 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Flush instruction cache, since we've generated code */
        mono_arch_flush_icache (code, buf - code);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
        
-       if (info) {
-               tramp_name = mono_get_generic_trampoline_name (tramp_type);
-               *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
-               g_free (tramp_name);
-       }
+       g_assert (info);
+       tramp_name = mono_get_generic_trampoline_name (tramp_type);
+       *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
+       g_free (tramp_name);
 
        /* Sanity check */
        g_assert ((buf - code) <= 512);
@@ -416,6 +409,28 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_invalidate_method                       */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
+{
+       /* FIXME: This is not thread safe */
+       guint8 *code = ji->code_start;
+
+       S390_SET  (code, s390_r1, func);
+       S390_SET  (code, s390_r2, func_arg);
+       s390_br   (code, s390_r1);
+
+}
+
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_create_specific_trampoline              */
@@ -439,15 +454,31 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        /*----------------------------------------------------------*/
        code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
 
-       s390_basr (buf, s390_r1, 0);
-       s390_j    (buf, 6);
-       s390_llong(buf, arg1);
-       s390_lg   (buf, s390_r1, 0, s390_r1, 4);
+       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;
+       /*
+        * Generic class trampoline arg is in r2
+        */
+       case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
+               s390_lgr (buf, s390_r1, s390_r2);
+               break;
+       default :
+               S390_SET  (buf, s390_r1, arg1);
+       }
        displace = (tramp - buf) / 2;
        s390_jg   (buf, displace);
 
        /* Flush instruction cache, since we've generated code */
        mono_arch_flush_icache (code, buf - code);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, 
+                                      (void *) mono_get_generic_trampoline_simple_name (tramp_type));
 
        /* Sanity check */
        g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
@@ -471,7 +502,6 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
 gpointer
 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
 {
-#ifdef MONO_ARCH_VTABLE_REG
        guint8 *tramp;
        guint8 *code, *buf;
        guint8 **rgctx_null_jumps;
@@ -514,7 +544,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                s390_lgr (code, s390_r1, s390_r2);
        } else {
                /* load rgctx ptr from vtable */
-               s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
+               s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
                /* is the rgctx ptr null? */
                s390_ltgr (code, s390_r1, s390_r1);
                /* if yes, jump to actual trampoline */
@@ -563,20 +593,15 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        s390_jg (code, displace);
 
        mono_arch_flush_icache (buf, code - buf);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
 
        g_assert (code - buf <= tramp_size);
 
-       if (info) {
-               char *name = mono_get_rgctx_fetch_trampoline_name (slot);
-               *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
-               g_free (name);
-       }
+       char *name = mono_get_rgctx_fetch_trampoline_name (slot);
+       *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
+       g_free (name);
 
        return(buf);
-#else
-       g_assert_not_reached ();
-#endif
-       return(NULL);
 }      
 
 /*========================= End of Function ========================*/
@@ -605,15 +630,13 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
 
        start = code = mono_domain_code_reserve (domain, buf_len);
 
-       s390_basr (code, s390_r1, 0);
-       s390_j    (code, 6);
-       s390_llong(code, mrgctx);
-       s390_lg   (code, MONO_ARCH_RGCTX_REG, 0, s390_r1, 4);
+       S390_SET  (code, MONO_ARCH_RGCTX_REG, mrgctx);
        displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
        s390_jg   (code, displace);
        g_assert ((code - start) < buf_len);
 
        mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
 
        return(start);
 }      
@@ -622,53 +645,370 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_create_generic_class_init_trampoline    */
+/* Name                - handler_block_trampoline_helper                   */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+static void
+handler_block_trampoline_helper (gpointer *ptr)
+{
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       *ptr = jit_tls->handler_block_return_address;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_create_handler_block_trampoline         */
 /*                                                                  */
 /* Function    -                                                   */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
 gpointer
-mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
+mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
 {
-       guint8 *tramp;
+       guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
        guint8 *code, *buf;
-       static int byte_offset = -1;
-       static guint8 bitmask;
-       guint8 *jump;
-       gint32 displace;
-       int tramp_size;
+       int tramp_size = 64;
+       MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
+
+       g_assert (!aot);
+
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       /*
+        * This trampoline restore the call chain of the handler block 
+        * then jumps into the code that deals with it.
+        */
+
+       if (mono_get_jit_tls_offset () != -1) {
+               s390_ear  (code, s390_r1, 0);
+               s390_sllg (code, s390_r1, s390_r1, 0, 32);
+               s390_ear  (code, s390_r1, 1);
+               S390_SET  (code, s390_r14, mono_get_jit_tls_offset());
+               s390_lg   (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
+               /* 
+                * Simulate a call 
+                */
+               S390_SET  (code, s390_r1, tramp);
+               s390_br   (code, s390_r1);
+       } else {
+               /*
+                * Slow path uses a C helper
+                */
+               S390_SET  (code, s390_r2, tramp);
+               S390_SET  (code, s390_r1, handler_block_trampoline_helper);
+               s390_br   (code, s390_r1);
+       }
+
+       mono_arch_flush_icache (buf, code - buf);
+       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       g_assert (code - buf <= tramp_size);
+
+       *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
+
+       return buf;
+}
+
+/*========================= 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;
 
-       tramp_size = 48;
+       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 (byte_offset < 0)
-               mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
+       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);
+               }
 
-       s390_llgc(code, s390_r0, 0, MONO_ARCH_VTABLE_REG, byte_offset);
-       s390_nill(code, s390_r0, bitmask);
-       s390_bnzr(code, s390_r14);
+               /* load obj->synchronization to sync_reg */
+               s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
 
-       tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
-               mono_get_root_domain (), NULL);
+               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 */
-       displace = (tramp - code) / 2;
-       s390_jg (code, displace);
+       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);
 
-       mono_arch_flush_icache (buf, code - buf);
+       /* 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 ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
+               *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
 
-       return(buf);
+       return buf;
 }
 
 /*========================= End of Function ========================*/
+#endif