111899812b2f69faa26d13327e0ae512f6dbba0f
[mono.git] / mono / mini / tramp-s390x.c
1 /*------------------------------------------------------------------*/
2 /*                                                                  */
3 /* Name        - tramp-s390x.c                                      */
4 /*                                                                  */
5 /* Function    - JIT trampoline code for S/390.                     */
6 /*                                                                  */
7 /* Name        - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /*                                                                  */
9 /* Date        - January, 2004                                      */
10 /*                                                                  */
11 /* Derivation  - From exceptions-x86 & exceptions-ppc               */
12 /*               Paolo Molaro (lupus@ximian.com)                    */
13 /*               Dietmar Maurer (dietmar@ximian.com)                */
14 /*                                                                  */
15 /* Copyright   - 2001 Ximian, Inc.                                  */
16 /*                                                                  */
17 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
23 #define LMFReg  s390_r13
24
25 /*
26  * Method-specific trampoline code fragment sizes                   
27  */
28 #define SPECIFIC_TRAMPOLINE_SIZE        96
29
30 /*========================= End of Defines =========================*/
31
32 /*------------------------------------------------------------------*/
33 /*                 I n c l u d e s                                  */
34 /*------------------------------------------------------------------*/
35
36 #include <config.h>
37 #include <glib.h>
38 #include <string.h>
39
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>
48
49 #include "mini.h"
50 #include "mini-s390x.h"
51 #include "support-s390x.h"
52
53 /*========================= End of Includes ========================*/
54
55 /*------------------------------------------------------------------*/
56 /*                 T y p e d e f s                                  */
57 /*------------------------------------------------------------------*/
58
59 typedef struct {
60         guint8  stk[S390_MINIMAL_STACK_SIZE];   /* Standard s390x stack */
61         struct MonoLMF  LMF;                    /* LMF                  */
62 } trampStack_t;
63
64 /*========================= End of Typedefs ========================*/
65
66 /*------------------------------------------------------------------*/
67 /*                   P r o t o t y p e s                            */
68 /*------------------------------------------------------------------*/
69
70 /*========================= End of Prototypes ======================*/
71
72 /*------------------------------------------------------------------*/
73 /*                 G l o b a l   V a r i a b l e s                  */
74 /*------------------------------------------------------------------*/
75
76
77 /*====================== End of Global Variables ===================*/
78
79 /*------------------------------------------------------------------*/
80 /*                                                                  */
81 /* Name         - mono_arch_get_unbox_trampoline                    */
82 /*                                                                  */
83 /* Function     - Return a pointer to a trampoline which does the   */
84 /*                unboxing before calling the method.               */
85 /*                                                                  */
86 /*                When value type methods are called through the    */
87 /*                vtable we need to unbox the 'this' argument.      */
88 /*                                                                  */
89 /* Parameters   - method - Methd pointer                            */
90 /*                addr   - Pointer to native code for method        */
91 /*                                                                  */
92 /*------------------------------------------------------------------*/
93
94 gpointer
95 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
96 {
97         guint8 *code, *start;
98         int this_pos = s390_r2;
99         MonoDomain *domain = mono_domain_get ();
100
101         start = code = mono_domain_code_reserve (domain, 28);
102
103         S390_SET  (code, s390_r1, addr);
104         s390_aghi (code, this_pos, sizeof(MonoObject));
105         s390_br   (code, s390_r1);
106
107         g_assert ((code - start) <= 28);
108
109         mono_arch_flush_icache (start, code - start);
110         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
111
112         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
113
114         return start;
115 }
116
117 /*========================= End of Function ========================*/
118
119 /*------------------------------------------------------------------*/
120 /*                                                                  */
121 /* Name         - mono_arch_patch_callsite                          */
122 /*                                                                  */
123 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
124 /*                                                                  */
125 /*------------------------------------------------------------------*/
126
127 void
128 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
129 {
130         gint32 displace;
131         unsigned short opcode;
132
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);
138         } else {
139                 /* This is the 'brasl' instruction */
140                 orig_code    -= 4;
141                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
142                 s390_patch_rel (orig_code, displace);
143                 mono_arch_flush_icache (orig_code, 4);
144         }
145 }
146
147 /*========================= End of Function ========================*/
148
149 /*------------------------------------------------------------------*/
150 /*                                                                  */
151 /* Name         - mono_arch_patch_plt_entry.                        */
152 /*                                                                  */
153 /* Function     - Patch a PLT entry - unused as yet.                */
154 /*                                                                  */
155 /*------------------------------------------------------------------*/
156
157 void
158 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
159 {
160         g_assert_not_reached ();
161 }
162
163 /*========================= End of Function ========================*/
164
165 /*------------------------------------------------------------------*/
166 /*                                                                  */
167 /* Name         - mono_arch_create_trampoline_code                  */
168 /*                                                                  */
169 /* Function     - Create the designated type of trampoline according*/
170 /*                to the 'tramp_type' parameter.                    */
171 /*                                                                  */
172 /*------------------------------------------------------------------*/
173
174 guchar*
175 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
176 {
177         char *tramp_name;
178         guint8 *buf, *tramp, *code;
179         int i, offset, has_caller;
180         GSList *unwind_ops = NULL;
181         MonoJumpInfo *ji = NULL;
182
183         g_assert (!aot);
184
185         /* Now we'll create in 'buf' the S/390 trampoline code. This
186            is the trampoline code common to all methods  */
187                 
188         code = buf = mono_global_codeman_reserve(512);
189                 
190         if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
191             (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)) 
192                 has_caller = 0;
193         else
194                 has_caller = 1;
195
196         /*-----------------------------------------------------------
197           STEP 0: First create a non-standard function prologue with a
198           stack size big enough to save our registers.
199           -----------------------------------------------------------*/
200                 
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);
205
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         /*---------------------------------------------------------------*/
210                                                                                         
211         s390_lgr   (buf, LMFReg, STK_BASE);
212         s390_aghi  (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
213                                                                                         
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);
221
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);
226         }
227
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
232           method.
233           ----------------------------------------------------------*/
234                                 
235         S390_SET  (buf, s390_r1, mono_get_lmf_addr);
236         s390_basr (buf, s390_r14, s390_r1);
237                                                                                         
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));                        
243                                                                                         
244         /*---------------------------------------------------------------*/     
245         /* Get current lmf                                               */     
246         /*---------------------------------------------------------------*/     
247         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
248                                                                                         
249         /*---------------------------------------------------------------*/     
250         /* Set our lmf as the current lmf                                */     
251         /*---------------------------------------------------------------*/     
252         s390_stg   (buf, LMFReg, 0, s390_r2, 0);                                
253                                                                                         
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));                    
259                                                                                         
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));                         
265                                                                         
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));    
271                                                                         
272         /*---------------------------------------------------------------*/     
273         /* save the current IP                                           */     
274         /*---------------------------------------------------------------*/     
275         if (has_caller) {
276                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
277         } else {
278                 s390_lghi  (buf, s390_r1, 0);
279         }
280         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));    
281                                                                                         
282         /*---------------------------------------------------------------*/
283         /* STEP 2: call the C trampoline function                        */
284         /*---------------------------------------------------------------*/
285                                 
286         /* Set arguments */
287
288         /* Arg 1: mgreg_t *regs */
289         s390_la  (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
290                 
291         /* Arg 2: code (next address to the instruction that called us) */
292         if (has_caller) {
293                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
294         } else {
295                 s390_lghi (buf, s390_r3, 0);
296         }
297
298         /* Arg 3: Trampoline argument */
299         s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
300
301         /* Arg 4: trampoline address. */
302         S390_SET (buf, s390_r5, buf);
303                 
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);
308                 
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);
312
313         /*----------------------------------------------------------
314           STEP 3: Restore the LMF
315           ----------------------------------------------------------*/
316         restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
317         
318         /*----------------------------------------------------------
319           STEP 4: call the compiled method
320           ----------------------------------------------------------*/
321                 
322         /* Restore parameter registers */
323         s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
324                 
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);
330         }
331
332         /* Restore stack pointer and jump to the code -
333          * R14 contains the return address to our caller 
334          */
335         s390_lgr  (buf, STK_BASE, s390_r11);
336         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
337
338         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
339                 s390_lgr (buf, s390_r2, s390_r1);
340                 s390_br  (buf, s390_r14);
341         } else {
342                 s390_br  (buf, s390_r1);
343         }
344
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);
348         
349         g_assert (info);
350         tramp_name = mono_get_generic_trampoline_name (tramp_type);
351         *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
352         g_free (tramp_name);
353
354         /* Sanity check */
355         g_assert ((buf - code) <= 512);
356
357         return code;
358 }
359
360 /*========================= End of Function ========================*/
361
362 /*------------------------------------------------------------------*/
363 /*                                                                  */
364 /* Name         - mono_arch_invalidate_method                       */
365 /*                                                                  */
366 /* Function     -                                                   */
367 /*                                                                  */
368 /*------------------------------------------------------------------*/
369
370 void
371 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
372 {
373         /* FIXME: This is not thread safe */
374         guint8 *code = ji->code_start;
375
376         S390_SET  (code, s390_r1, func);
377         S390_SET  (code, s390_r2, func_arg);
378         s390_br   (code, s390_r1);
379
380 }
381
382 /*========================= End of Function ========================*/
383
384 /*------------------------------------------------------------------*/
385 /*                                                                  */
386 /* Name         - mono_arch_create_specific_trampoline              */
387 /*                                                                  */
388 /* Function     - Creates the given kind of specific trampoline     */
389 /*                                                                  */
390 /*------------------------------------------------------------------*/
391
392 gpointer
393 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
394 {
395         guint8 *code, *buf, *tramp;
396         gint32 displace;
397
398         tramp = mono_get_trampoline_code (tramp_type);
399
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);
406
407         switch (tramp_type) {
408         /*
409          * Monitor tramps have the object in r2
410          */
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);
415                 break;
416         default :
417                 S390_SET  (buf, s390_r1, arg1);
418         }
419         displace = (tramp - buf) / 2;
420         s390_jg   (buf, displace);
421
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));
426
427         /* Sanity check */
428         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
429
430         if (code_len)
431                 *code_len = buf - code;
432         
433         return code;
434 }       
435
436 /*========================= End of Function ========================*/
437
438 /*------------------------------------------------------------------*/
439 /*                                                                  */
440 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
441 /*                                                                  */
442 /* Function     -                                                   */
443 /*                                                                  */
444 /*------------------------------------------------------------------*/
445
446 gpointer
447 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
448 {
449         guint8 *tramp;
450         guint8 *code, *buf;
451         guint8 **rgctx_null_jumps;
452         gint32 displace;
453         int tramp_size,
454             depth, 
455             index, 
456             iPatch = 0,
457             i;
458         gboolean mrgctx;
459         MonoJumpInfo *ji = NULL;
460         GSList *unwind_ops = NULL;
461
462         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
463         index = MONO_RGCTX_SLOT_INDEX (slot);
464         if (mrgctx)
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);
468
469                 if (index < size - 1)
470                         break;
471                 index -= size - 1;
472         }
473
474         tramp_size = 48 + 16 * depth;
475         if (mrgctx)
476                 tramp_size += 4;
477         else
478                 tramp_size += 12;
479
480         code = buf = mono_global_codeman_reserve (tramp_size);
481
482         unwind_ops = mono_arch_get_cie_program ();
483
484         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
485
486         if (mrgctx) {
487                 /* get mrgctx ptr */
488                 s390_lgr (code, s390_r1, s390_r2);
489         } else {
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;
496                 s390_jge (code, 0);
497         }
498
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);
503                 else
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;
508                 s390_jge (code, 0);
509         }
510
511         /* fetch slot */
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;
517         s390_jge (code, 0);
518         /* otherwise return r1 */
519         s390_lgr (code, s390_r2, s390_r1);
520         s390_br  (code, s390_r14);
521
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);
525         }
526
527         g_free (rgctx_null_jumps);
528
529         /* move the rgctx pointer to the VTABLE register */
530         s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
531
532         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
533                 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
534
535         /* jump to the actual trampoline */
536         displace = (tramp - code) / 2;
537         s390_jg (code, displace);
538
539         mono_arch_flush_icache (buf, code - buf);
540         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
541
542         g_assert (code - buf <= tramp_size);
543
544         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
545         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
546         g_free (name);
547
548         return(buf);
549 }       
550
551 /*========================= End of Function ========================*/
552
553 /*------------------------------------------------------------------*/
554 /*                                                                  */
555 /* Name         - mono_arch_get_static_rgctx_trampoline             */
556 /*                                                                  */
557 /* Function     - Create a trampoline which sets RGCTX_REG to MRGCTX*/
558 /*                then jumps to ADDR.                               */
559 /*                                                                  */
560 /*------------------------------------------------------------------*/
561
562 gpointer
563 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, 
564                                         MonoMethodRuntimeGenericContext *mrgctx, 
565                                         gpointer addr)
566 {
567         guint8 *code, *start;
568         gint32 displace;
569         int buf_len;
570
571         MonoDomain *domain = mono_domain_get ();
572
573         buf_len = 32;
574
575         start = code = mono_domain_code_reserve (domain, buf_len);
576
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);
581
582         mono_arch_flush_icache (start, code - start);
583         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
584
585         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
586
587         return(start);
588 }       
589
590 /*========================= End of Function ========================*/
591
592 /*------------------------------------------------------------------*/
593 /*                                                                  */
594 /* Name         - handler_block_trampoline_helper                   */
595 /*                                                                  */
596 /* Function     -                                                   */
597 /*                                                                  */
598 /*------------------------------------------------------------------*/
599
600 static void
601 handler_block_trampoline_helper (gpointer *ptr)
602 {
603         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
604         *ptr = jit_tls->handler_block_return_address;
605 }
606
607 /*========================= End of Function ========================*/
608
609 /*------------------------------------------------------------------*/
610 /*                                                                  */
611 /* Name         - mono_arch_create_handler_block_trampoline         */
612 /*                                                                  */
613 /* Function     -                                                   */
614 /*                                                                  */
615 /*------------------------------------------------------------------*/
616
617 gpointer
618 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
619 {
620         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
621         guint8 *code, *buf;
622         int tramp_size = 64;
623         MonoJumpInfo *ji = NULL;
624         GSList *unwind_ops = NULL;
625
626         g_assert (!aot);
627
628         code = buf = mono_global_codeman_reserve (tramp_size);
629
630         /*
631          * This trampoline restore the call chain of the handler block 
632          * then jumps into the code that deals with it.
633          */
634
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));
641                 /* 
642                  * Simulate a call 
643                  */
644                 S390_SET  (code, s390_r1, tramp);
645                 s390_br   (code, s390_r1);
646         } else {
647                 /*
648                  * Slow path uses a C helper
649                  */
650                 S390_SET  (code, s390_r2, tramp);
651                 S390_SET  (code, s390_r1, handler_block_trampoline_helper);
652                 s390_br   (code, s390_r1);
653         }
654
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);
658
659         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
660
661         return buf;
662 }
663
664 /*========================= End of Function ========================*/
665
666 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
667 /*------------------------------------------------------------------*/
668 /*                                                                  */
669 /* Name         - mono_arch_create_monitor_enter_trampoline         */
670 /*                                                                  */
671 /* Function     -                                                   */
672 /*                                                                  */
673 /*------------------------------------------------------------------*/
674
675 gpointer
676 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
677 {
678         guint8  *tramp,
679                 *code, *buf;
680         gint16  *jump_obj_null, 
681                 *jump_sync_null, 
682                 *jump_cs_failed, 
683                 *jump_other_owner, 
684                 *jump_tid, 
685                 *jump_sync_thin_hash = NULL,
686                 *jump_lock_taken_true = NULL;
687         int tramp_size,
688             status_reg = s390_r0,
689             lock_taken_reg = s390_r1,
690             obj_reg = s390_r2,
691             sync_reg = s390_r3,
692             tid_reg = s390_r4,
693             status_offset,
694             nest_offset;
695         MonoJumpInfo *ji = NULL;
696         GSList *unwind_ops = NULL;
697
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);
701 #else
702         g_assert (!is_v4);
703 #endif
704
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);
710
711         tramp_size = 160;
712
713         code = buf = mono_global_codeman_reserve (tramp_size);
714
715         unwind_ops = mono_arch_get_cie_program ();
716
717         if (mono_thread_get_tls_offset () != -1) {
718                 /* MonoObject* obj is in obj_reg */
719                 /* is obj null? */
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);
723
724                 if (is_v4) {
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);
728                 }
729
730                 /* load obj->synchronization to sync_reg */
731                 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
732
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);
737
738                         /* Clear bits used by the gc */
739                         s390_nill (code, sync_reg, ~0x3);
740                 }
741
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);
746
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);
751                 /* load tid */
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));
754
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);
760
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 */
768                 if (is_v4)
769                         s390_mvi (code, lock_taken_reg, 0, 1);
770                 s390_br (code, s390_r14);
771
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);
783                 /* return */
784                 if (is_v4)
785                         s390_mvi (code, lock_taken_reg, 0, 1);
786                 s390_br (code, s390_r14);
787
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);
794                 if (is_v4)
795                         PTRSLOT (code, jump_lock_taken_true);
796         }
797
798         /* jump to the actual trampoline */
799         if (is_v4)
800                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
801         else
802                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
803
804         /* jump to the actual trampoline */
805         S390_SET (code, s390_r1, tramp);
806         s390_br (code, s390_r1);
807
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);
811
812         if (info) {
813                 if (is_v4)
814                         *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
815                 else
816                         *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
817         }
818
819         return buf;
820 }
821
822 /*========================= End of Function ========================*/
823
824 /*------------------------------------------------------------------*/
825 /*                                                                  */
826 /* Name         - mono_arch_create_monitor_exit_trampoline          */
827 /*                                                                  */
828 /* Function     -                                                   */
829 /*                                                                  */
830 /*------------------------------------------------------------------*/
831
832 gpointer
833 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
834 {
835         guint8  *tramp,
836                 *code, *buf;
837         gint16  *jump_obj_null, 
838                 *jump_have_waiters, 
839                 *jump_sync_null, 
840                 *jump_not_owned, 
841                 *jump_cs_failed,
842                 *jump_next,
843                 *jump_sync_thin_hash = NULL;
844         int     tramp_size,
845                 status_offset, nest_offset;
846         MonoJumpInfo *ji = NULL;
847         GSList *unwind_ops = NULL;
848         int     obj_reg = s390_r2,
849                 sync_reg = s390_r3,
850                 status_reg = s390_r4;
851
852         g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
853
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);
859
860         tramp_size = 160;
861
862         code = buf = mono_global_codeman_reserve (tramp_size);
863
864         unwind_ops = mono_arch_get_cie_program ();
865
866         if (mono_thread_get_tls_offset () != -1) {
867                 /* MonoObject* obj is in obj_reg */
868                 /* is obj null? */
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);
872
873                 /* load obj->synchronization to RCX */
874                 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
875
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);
880
881                         /* Clear bits used by the gc */
882                         s390_nill (code, sync_reg, ~0x3);
883                 }
884
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);
889
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);
904
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);
925
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);
933
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);
941         }
942
943         /* jump to the actual trampoline */
944         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
945
946         S390_SET (code, s390_r1, tramp);
947         s390_br (code, s390_r1);
948
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);
952
953         if (info)
954                 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
955
956         return buf;
957 }
958
959 /*========================= End of Function ========================*/
960 #endif