Add support for OP_GENERIC_CLASS_INIT
[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         return start;
113 }
114
115 /*========================= End of Function ========================*/
116
117 /*------------------------------------------------------------------*/
118 /*                                                                  */
119 /* Name         - mono_arch_patch_callsite                          */
120 /*                                                                  */
121 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
122 /*                                                                  */
123 /*------------------------------------------------------------------*/
124
125 void
126 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
127 {
128         gint32 displace;
129         unsigned short opcode;
130
131         opcode = *((unsigned short *) (orig_code - 2));
132         if (opcode == 0x0dee) {
133                 /* This should be a 'iihf/iilf' sequence */
134                 S390_EMIT_CALL((orig_code - 14), addr);
135                 mono_arch_flush_icache (orig_code - 14, 12);
136         } else {
137                 /* This is the 'brasl' instruction */
138                 orig_code    -= 4;
139                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
140                 s390_patch_rel (orig_code, displace);
141                 mono_arch_flush_icache (orig_code, 4);
142         }
143 }
144
145 /*========================= End of Function ========================*/
146
147 /*------------------------------------------------------------------*/
148 /*                                                                  */
149 /* Name         - mono_arch_patch_plt_entry.                        */
150 /*                                                                  */
151 /* Function     - Patch a PLT entry - unused as yet.                */
152 /*                                                                  */
153 /*------------------------------------------------------------------*/
154
155 void
156 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
157 {
158         g_assert_not_reached ();
159 }
160
161 /*========================= End of Function ========================*/
162
163 /*------------------------------------------------------------------*/
164 /*                                                                  */
165 /* Name         - mono_arch_nullify_class_init_trampoline           */
166 /*                                                                  */
167 /* Function     - Nullify a call which calls a class init trampoline*/
168 /*                                                                  */
169 /*------------------------------------------------------------------*/
170
171 void
172 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
173 {
174         char patch[2] = {0x07, 0x00};
175
176         code = code - 2;
177
178         memcpy(code, patch, sizeof(patch));
179 }
180
181 /*========================= End of Function ========================*/
182
183 /*------------------------------------------------------------------*/
184 /*                                                                  */
185 /* Name         - mono_arch_get_nullified_class_init                */
186 /*                                                                  */
187 /* Function     - Nullify a PLT entry call.                         */
188 /*                                                                  */
189 /*------------------------------------------------------------------*/
190
191 gpointer
192 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
193 {
194         guint8 *buf, *code;
195
196         code = buf = mono_global_codeman_reserve (16);
197
198         s390_br (code, s390_r14);
199
200         mono_arch_flush_icache (buf, code - buf);
201         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
202
203         *info = mono_tramp_info_create ("nullified_class_init_trampoline", 
204                                                                         buf, code - buf, NULL, NULL);
205
206         return (buf);
207 }
208
209 /*========================= End of Function ========================*/
210
211 /*------------------------------------------------------------------*/
212 /*                                                                  */
213 /* Name         - mono_arch_create_trampoline_code                  */
214 /*                                                                  */
215 /* Function     - Create the designated type of trampoline according*/
216 /*                to the 'tramp_type' parameter.                    */
217 /*                                                                  */
218 /*------------------------------------------------------------------*/
219
220 guchar*
221 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
222 {
223         char *tramp_name;
224         guint8 *buf, *tramp, *code;
225         int i, offset, has_caller;
226         GSList *unwind_ops = NULL;
227         MonoJumpInfo *ji = NULL;
228
229         g_assert (!aot);
230
231         /* Now we'll create in 'buf' the S/390 trampoline code. This
232            is the trampoline code common to all methods  */
233                 
234         code = buf = mono_global_codeman_reserve(512);
235                 
236         if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
237             (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)) 
238                 has_caller = 0;
239         else
240                 has_caller = 1;
241
242         /*-----------------------------------------------------------
243           STEP 0: First create a non-standard function prologue with a
244           stack size big enough to save our registers.
245           -----------------------------------------------------------*/
246                 
247         s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
248         s390_lgr  (buf, s390_r11, s390_r15);
249         s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
250         s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
251
252         /*---------------------------------------------------------------*/
253         /* we build the MonoLMF structure on the stack - see mini-s390.h */
254         /* Keep in sync with the code in mono_arch_emit_prolog           */
255         /*---------------------------------------------------------------*/
256                                                                                         
257         s390_lgr   (buf, LMFReg, STK_BASE);
258         s390_aghi  (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
259                                                                                         
260         /*---------------------------------------------------------------*/     
261         /* Save general and floating point registers in LMF              */     
262         /*---------------------------------------------------------------*/     
263         s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
264         s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
265         s390_mvc  (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
266                    s390_r11, S390_REG_SAVE_OFFSET);
267
268         offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
269         for (i = s390_f0; i <= s390_f15; ++i) {
270                 s390_std  (buf, i, 0, LMFReg, offset);
271                 offset += sizeof(gdouble);
272         }
273
274         /*----------------------------------------------------------
275           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
276           LMF. We'll need to restore it after the call to
277           's390_magic_trampoline' and before the call to the native
278           method.
279           ----------------------------------------------------------*/
280                                 
281         S390_SET  (buf, s390_r1, mono_get_lmf_addr);
282         s390_basr (buf, s390_r14, s390_r1);
283                                                                                         
284         /*---------------------------------------------------------------*/     
285         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
286         /*---------------------------------------------------------------*/     
287         s390_stg   (buf, s390_r2, 0, LMFReg,                            
288                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
289                                                                                         
290         /*---------------------------------------------------------------*/     
291         /* Get current lmf                                               */     
292         /*---------------------------------------------------------------*/     
293         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
294                                                                                         
295         /*---------------------------------------------------------------*/     
296         /* Set our lmf as the current lmf                                */     
297         /*---------------------------------------------------------------*/     
298         s390_stg   (buf, LMFReg, 0, s390_r2, 0);                                
299                                                                                         
300         /*---------------------------------------------------------------*/     
301         /* Have our lmf.previous_lmf point to the last lmf               */     
302         /*---------------------------------------------------------------*/     
303         s390_stg   (buf, s390_r0, 0, LMFReg,                            
304                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
305                                                                                         
306         /*---------------------------------------------------------------*/     
307         /* save method info                                              */     
308         /*---------------------------------------------------------------*/     
309         s390_lg    (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
310         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));                         
311                                                                         
312         /*---------------------------------------------------------------*/     
313         /* save the current SP                                           */     
314         /*---------------------------------------------------------------*/     
315         s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
316         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));    
317                                                                         
318         /*---------------------------------------------------------------*/     
319         /* save the current IP                                           */     
320         /*---------------------------------------------------------------*/     
321         if (has_caller) {
322                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
323         } else {
324                 s390_lghi  (buf, s390_r1, 0);
325         }
326         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));    
327                                                                                         
328         /*---------------------------------------------------------------*/
329         /* STEP 2: call the C trampoline function                        */
330         /*---------------------------------------------------------------*/
331                                 
332         /* Set arguments */
333
334         /* Arg 1: mgreg_t *regs */
335         s390_la  (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
336                 
337         /* Arg 2: code (next address to the instruction that called us) */
338         if (has_caller) {
339                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
340         } else {
341                 s390_lghi (buf, s390_r3, 0);
342         }
343
344         /* Arg 3: Trampoline argument */
345         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
346                 s390_lg (buf, s390_r4, 0, LMFReg,
347                          G_STRUCT_OFFSET(MonoLMF, gregs[MONO_ARCH_VTABLE_REG]));
348         else
349                 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
350
351         /* Arg 4: trampoline address. */
352         S390_SET (buf, s390_r5, buf);
353                 
354         /* Calculate call address and call the C trampoline. Return value will be in r2 */
355         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
356         S390_SET  (buf, s390_r1, tramp);
357         s390_basr (buf, s390_r14, s390_r1);
358                 
359         /* OK, code address is now on r2. Move it to r1, so that we
360            can restore r2 and use it from r1 later */
361         s390_lgr  (buf, s390_r1, s390_r2);
362
363         /*----------------------------------------------------------
364           STEP 3: Restore the LMF
365           ----------------------------------------------------------*/
366         restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
367         
368         /*----------------------------------------------------------
369           STEP 4: call the compiled method
370           ----------------------------------------------------------*/
371                 
372         /* Restore parameter registers */
373         s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
374                 
375         /* Restore the FP registers */
376         offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
377         for (i = s390_f0; i <= s390_f15; ++i) {
378                 s390_ld  (buf, i, 0, LMFReg, offset);
379                 offset += sizeof(gdouble);
380         }
381
382         /* Restore stack pointer and jump to the code -
383          * R14 contains the return address to our caller 
384          */
385         s390_lgr  (buf, STK_BASE, s390_r11);
386         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
387
388         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
389                 s390_lgr (buf, s390_r2, s390_r1);
390                 s390_br  (buf, s390_r14);
391         } else {
392                 s390_br  (buf, s390_r1);
393         }
394
395         /* Flush instruction cache, since we've generated code */
396         mono_arch_flush_icache (code, buf - code);
397         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
398         
399         g_assert (info);
400         tramp_name = mono_get_generic_trampoline_name (tramp_type);
401         *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
402         g_free (tramp_name);
403
404         /* Sanity check */
405         g_assert ((buf - code) <= 512);
406
407         return code;
408 }
409
410 /*========================= End of Function ========================*/
411
412 /*------------------------------------------------------------------*/
413 /*                                                                  */
414 /* Name         - mono_arch_invalidate_method                       */
415 /*                                                                  */
416 /* Function     -                                                   */
417 /*                                                                  */
418 /*------------------------------------------------------------------*/
419
420 void
421 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
422 {
423         /* FIXME: This is not thread safe */
424         guint8 *code = ji->code_start;
425
426         S390_SET  (code, s390_r1, func);
427         S390_SET  (code, s390_r2, func_arg);
428         s390_br   (code, s390_r1);
429
430 }
431
432 /*========================= End of Function ========================*/
433
434 /*------------------------------------------------------------------*/
435 /*                                                                  */
436 /* Name         - mono_arch_create_specific_trampoline              */
437 /*                                                                  */
438 /* Function     - Creates the given kind of specific trampoline     */
439 /*                                                                  */
440 /*------------------------------------------------------------------*/
441
442 gpointer
443 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
444 {
445         guint8 *code, *buf, *tramp;
446         gint32 displace;
447
448         tramp = mono_get_trampoline_code (tramp_type);
449
450         /*----------------------------------------------------------*/
451         /* This is the method-specific part of the trampoline. Its  */
452         /* purpose is to provide the generic part with the          */
453         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
454         /*----------------------------------------------------------*/
455         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
456
457         switch (tramp_type) {
458         /*
459          * Monitor tramps have the object in r2
460          */
461         case MONO_TRAMPOLINE_MONITOR_ENTER:
462         case MONO_TRAMPOLINE_MONITOR_ENTER_V4:
463         case MONO_TRAMPOLINE_MONITOR_EXIT:
464                 s390_lgr (buf, s390_r1, s390_r2);
465                 break;
466         /*
467          * Generic class trampoline arg is in r2
468          */
469         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
470                 s390_lgr (buf, s390_r1, s390_r2);
471                 break;
472         default :
473                 S390_SET  (buf, s390_r1, arg1);
474         }
475         displace = (tramp - buf) / 2;
476         s390_jg   (buf, displace);
477
478         /* Flush instruction cache, since we've generated code */
479         mono_arch_flush_icache (code, buf - code);
480         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, 
481                                        (void *) mono_get_generic_trampoline_simple_name (tramp_type));
482
483         /* Sanity check */
484         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
485
486         if (code_len)
487                 *code_len = buf - code;
488         
489         return code;
490 }       
491
492 /*========================= End of Function ========================*/
493
494 /*------------------------------------------------------------------*/
495 /*                                                                  */
496 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
497 /*                                                                  */
498 /* Function     -                                                   */
499 /*                                                                  */
500 /*------------------------------------------------------------------*/
501
502 gpointer
503 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
504 {
505         guint8 *tramp;
506         guint8 *code, *buf;
507         guint8 **rgctx_null_jumps;
508         gint32 displace;
509         int tramp_size,
510             depth, 
511             index, 
512             iPatch = 0,
513             i;
514         gboolean mrgctx;
515         MonoJumpInfo *ji = NULL;
516         GSList *unwind_ops = NULL;
517
518         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
519         index = MONO_RGCTX_SLOT_INDEX (slot);
520         if (mrgctx)
521                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
522         for (depth = 0; ; ++depth) {
523                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
524
525                 if (index < size - 1)
526                         break;
527                 index -= size - 1;
528         }
529
530         tramp_size = 48 + 16 * depth;
531         if (mrgctx)
532                 tramp_size += 4;
533         else
534                 tramp_size += 12;
535
536         code = buf = mono_global_codeman_reserve (tramp_size);
537
538         unwind_ops = mono_arch_get_cie_program ();
539
540         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
541
542         if (mrgctx) {
543                 /* get mrgctx ptr */
544                 s390_lgr (code, s390_r1, s390_r2);
545         } else {
546                 /* load rgctx ptr from vtable */
547                 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
548                 /* is the rgctx ptr null? */
549                 s390_ltgr (code, s390_r1, s390_r1);
550                 /* if yes, jump to actual trampoline */
551                 rgctx_null_jumps [iPatch++] = code;
552                 s390_jge (code, 0);
553         }
554
555         for (i = 0; i < depth; ++i) {
556                 /* load ptr to next array */
557                 if (mrgctx && i == 0)
558                         s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
559                 else
560                         s390_lg (code, s390_r1, 0, s390_r1, 0);
561                 s390_ltgr (code, s390_r1, s390_r1);
562                 /* if the ptr is null then jump to actual trampoline */
563                 rgctx_null_jumps [iPatch++] = code;
564                 s390_jge (code, 0);
565         }
566
567         /* fetch slot */
568         s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index  + 1)));
569         /* is the slot null? */
570         s390_ltgr (code, s390_r1, s390_r1);
571         /* if yes, jump to actual trampoline */
572         rgctx_null_jumps [iPatch++] = code;
573         s390_jge (code, 0);
574         /* otherwise return r1 */
575         s390_lgr (code, s390_r2, s390_r1);
576         s390_br  (code, s390_r14);
577
578         for (i = 0; i < iPatch; i++) {
579                 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
580                 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
581         }
582
583         g_free (rgctx_null_jumps);
584
585         /* move the rgctx pointer to the VTABLE register */
586         s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
587
588         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
589                 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
590
591         /* jump to the actual trampoline */
592         displace = (tramp - code) / 2;
593         s390_jg (code, displace);
594
595         mono_arch_flush_icache (buf, code - buf);
596         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
597
598         g_assert (code - buf <= tramp_size);
599
600         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
601         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
602         g_free (name);
603
604         return(buf);
605 }       
606
607 /*========================= End of Function ========================*/
608
609 /*------------------------------------------------------------------*/
610 /*                                                                  */
611 /* Name         - mono_arch_get_static_rgctx_trampoline             */
612 /*                                                                  */
613 /* Function     - Create a trampoline which sets RGCTX_REG to MRGCTX*/
614 /*                then jumps to ADDR.                               */
615 /*                                                                  */
616 /*------------------------------------------------------------------*/
617
618 gpointer
619 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, 
620                                         MonoMethodRuntimeGenericContext *mrgctx, 
621                                         gpointer addr)
622 {
623         guint8 *code, *start;
624         gint32 displace;
625         int buf_len;
626
627         MonoDomain *domain = mono_domain_get ();
628
629         buf_len = 32;
630
631         start = code = mono_domain_code_reserve (domain, buf_len);
632
633         S390_SET  (code, MONO_ARCH_RGCTX_REG, mrgctx);
634         displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
635         s390_jg   (code, displace);
636         g_assert ((code - start) < buf_len);
637
638         mono_arch_flush_icache (start, code - start);
639         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
640
641         return(start);
642 }       
643
644 /*========================= End of Function ========================*/
645
646 /*------------------------------------------------------------------*/
647 /*                                                                  */
648 /* Name         - handler_block_trampoline_helper                   */
649 /*                                                                  */
650 /* Function     -                                                   */
651 /*                                                                  */
652 /*------------------------------------------------------------------*/
653
654 static void
655 handler_block_trampoline_helper (gpointer *ptr)
656 {
657         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
658         *ptr = jit_tls->handler_block_return_address;
659 }
660
661 /*========================= End of Function ========================*/
662
663 /*------------------------------------------------------------------*/
664 /*                                                                  */
665 /* Name         - mono_arch_create_handler_block_trampoline         */
666 /*                                                                  */
667 /* Function     -                                                   */
668 /*                                                                  */
669 /*------------------------------------------------------------------*/
670
671 gpointer
672 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
673 {
674         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
675         guint8 *code, *buf;
676         int tramp_size = 64;
677         MonoJumpInfo *ji = NULL;
678         GSList *unwind_ops = NULL;
679
680         g_assert (!aot);
681
682         code = buf = mono_global_codeman_reserve (tramp_size);
683
684         /*
685          * This trampoline restore the call chain of the handler block 
686          * then jumps into the code that deals with it.
687          */
688
689         if (mono_get_jit_tls_offset () != -1) {
690                 s390_ear  (code, s390_r1, 0);
691                 s390_sllg (code, s390_r1, s390_r1, 0, 32);
692                 s390_ear  (code, s390_r1, 1);
693                 S390_SET  (code, s390_r14, mono_get_jit_tls_offset());
694                 s390_lg   (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
695                 /* 
696                  * Simulate a call 
697                  */
698                 S390_SET  (code, s390_r1, tramp);
699                 s390_br   (code, s390_r1);
700         } else {
701                 /*
702                  * Slow path uses a C helper
703                  */
704                 S390_SET  (code, s390_r2, tramp);
705                 S390_SET  (code, s390_r1, handler_block_trampoline_helper);
706                 s390_br   (code, s390_r1);
707         }
708
709         mono_arch_flush_icache (buf, code - buf);
710         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
711         g_assert (code - buf <= tramp_size);
712
713         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
714
715         return buf;
716 }
717
718 /*========================= End of Function ========================*/
719
720 /*------------------------------------------------------------------*/
721 /*                                                                  */
722 /* Name         - mono_arch_create_generic_class_init_trampoline    */
723 /*                                                                  */
724 /* Function     -                                                   */
725 /*                                                                  */
726 /*------------------------------------------------------------------*/
727
728 gpointer
729 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
730 {
731         guint8 *tramp;
732         guint8 *code, *buf;
733         static int byte_offset = -1;
734         static guint8 bitmask;
735         gint32 displace;
736         int tramp_size;
737         GSList *unwind_ops = NULL;
738         MonoJumpInfo *ji = NULL;
739
740         tramp_size = 48;
741
742         code = buf = mono_global_codeman_reserve (tramp_size);
743
744         unwind_ops = mono_arch_get_cie_program ();
745
746         if (byte_offset < 0)
747                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
748
749         s390_llgc(code, s390_r0, 0, MONO_ARCH_VTABLE_REG, byte_offset);
750         s390_nill(code, s390_r0, bitmask);
751         s390_bnzr(code, s390_r14);
752
753         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
754                 mono_get_root_domain (), NULL);
755
756         /* jump to the actual trampoline */
757         displace = (tramp - code) / 2;
758         s390_jg (code, displace);
759
760         mono_arch_flush_icache (buf, code - buf);
761
762         g_assert (code - buf <= tramp_size);
763
764         *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
765
766         return(buf);
767 }
768
769 /*========================= End of Function ========================*/
770
771 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
772 /*------------------------------------------------------------------*/
773 /*                                                                  */
774 /* Name         - mono_arch_create_monitor_enter_trampoline         */
775 /*                                                                  */
776 /* Function     -                                                   */
777 /*                                                                  */
778 /*------------------------------------------------------------------*/
779
780 gpointer
781 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
782 {
783         guint8  *tramp,
784                 *code, *buf;
785         gint16  *jump_obj_null, 
786                 *jump_sync_null, 
787                 *jump_cs_failed, 
788                 *jump_other_owner, 
789                 *jump_tid, 
790                 *jump_sync_thin_hash = NULL,
791                 *jump_lock_taken_true = NULL;
792         int tramp_size,
793             status_reg = s390_r0,
794             lock_taken_reg = s390_r1,
795             obj_reg = s390_r2,
796             sync_reg = s390_r3,
797             tid_reg = s390_r4,
798             status_offset,
799             nest_offset;
800         MonoJumpInfo *ji = NULL;
801         GSList *unwind_ops = NULL;
802
803         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
804 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
805         g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
806 #else
807         g_assert (!is_v4);
808 #endif
809
810         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
811         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
812         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
813         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
814         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
815
816         tramp_size = 160;
817
818         code = buf = mono_global_codeman_reserve (tramp_size);
819
820         unwind_ops = mono_arch_get_cie_program ();
821
822         if (mono_thread_get_tls_offset () != -1) {
823                 /* MonoObject* obj is in obj_reg */
824                 /* is obj null? */
825                 s390_ltgr (code, obj_reg, obj_reg);
826                 /* if yes, jump to actual trampoline */
827                 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
828
829                 if (is_v4) {
830                         s390_cli (code, lock_taken_reg, 0, 1);
831                         /* if *lock_taken is 1, jump to actual trampoline */
832                         s390_je (code, 0); CODEPTR(code, jump_lock_taken_true);
833                 }
834
835                 /* load obj->synchronization to sync_reg */
836                 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
837
838                 if (mono_gc_is_moving ()) {
839                         /*if bit zero is set it's a thin hash*/
840                         s390_tmll (code, sync_reg, 1);
841                         s390_jo  (code, 0); CODEPTR(code, jump_sync_thin_hash);
842
843                         /* Clear bits used by the gc */
844                         s390_nill (code, sync_reg, ~0x3);
845                 }
846
847                 /* is synchronization null? */
848                 s390_ltgr (code, sync_reg, sync_reg);
849                 /* if yes, jump to actual trampoline */
850                 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
851
852                 /* load MonoInternalThread* into tid_reg */
853                 s390_ear (code, s390_r5, 0);
854                 s390_sllg(code, s390_r5, s390_r5, 0, 32);
855                 s390_ear (code, s390_r5, 1);
856                 /* load tid */
857                 s390_lg  (code, tid_reg, 0, s390_r5, mono_thread_get_tls_offset ());
858                 s390_lgf (code, tid_reg, 0, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
859
860                 /* is synchronization->owner free */
861                 s390_lgf  (code, status_reg, 0, sync_reg, status_offset);
862                 s390_nilf (code, status_reg, OWNER_MASK);
863                 /* if not, jump to next case */
864                 s390_jnz  (code, 0); CODEPTR(code, jump_tid);
865
866                 /* if yes, try a compare-exchange with the TID */
867                 /* Form new status in tid_reg */
868                 s390_xr (code, tid_reg, status_reg);
869                 /* compare and exchange */
870                 s390_cs (code, status_reg, tid_reg, sync_reg, status_offset);
871                 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
872                 /* if successful, return */
873                 if (is_v4)
874                         s390_mvi (code, lock_taken_reg, 0, 1);
875                 s390_br (code, s390_r14);
876
877                 /* next case: synchronization->owner is not null */
878                 PTRSLOT(code, jump_tid);
879                 /* is synchronization->owner == TID? */
880                 s390_nilf (code, status_reg, OWNER_MASK);
881                 s390_cr (code, status_reg, tid_reg);
882                 /* if not, jump to actual trampoline */
883                 s390_jnz (code, 0); CODEPTR(code, jump_other_owner);
884                 /* if yes, increment nest */
885                 s390_lgf (code, s390_r5, 0, sync_reg, nest_offset);
886                 s390_ahi (code, s390_r5, 1);
887                 s390_st  (code, s390_r5, 0, sync_reg, nest_offset);
888                 /* return */
889                 if (is_v4)
890                         s390_mvi (code, lock_taken_reg, 0, 1);
891                 s390_br (code, s390_r14);
892
893                 PTRSLOT (code, jump_obj_null);
894                 if (jump_sync_thin_hash)
895                         PTRSLOT (code, jump_sync_thin_hash);
896                 PTRSLOT (code, jump_sync_null);
897                 PTRSLOT (code, jump_cs_failed);
898                 PTRSLOT (code, jump_other_owner);
899                 if (is_v4)
900                         PTRSLOT (code, jump_lock_taken_true);
901         }
902
903         /* jump to the actual trampoline */
904         if (is_v4)
905                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
906         else
907                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
908
909         /* jump to the actual trampoline */
910         S390_SET (code, s390_r1, tramp);
911         s390_br (code, s390_r1);
912
913         mono_arch_flush_icache (code, code - buf);
914         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
915         g_assert (code - buf <= tramp_size);
916
917         if (info) {
918                 if (is_v4)
919                         *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
920                 else
921                         *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
922         }
923
924         return buf;
925 }
926
927 /*========================= End of Function ========================*/
928
929 /*------------------------------------------------------------------*/
930 /*                                                                  */
931 /* Name         - mono_arch_create_monitor_exit_trampoline          */
932 /*                                                                  */
933 /* Function     -                                                   */
934 /*                                                                  */
935 /*------------------------------------------------------------------*/
936
937 gpointer
938 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
939 {
940         guint8  *tramp,
941                 *code, *buf;
942         gint16  *jump_obj_null, 
943                 *jump_have_waiters, 
944                 *jump_sync_null, 
945                 *jump_not_owned, 
946                 *jump_cs_failed,
947                 *jump_next,
948                 *jump_sync_thin_hash = NULL;
949         int     tramp_size,
950                 status_offset, nest_offset;
951         MonoJumpInfo *ji = NULL;
952         GSList *unwind_ops = NULL;
953         int     obj_reg = s390_r2,
954                 sync_reg = s390_r3,
955                 status_reg = s390_r4;
956
957         g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
958
959         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
960         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
961         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
962         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
963         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
964
965         tramp_size = 160;
966
967         code = buf = mono_global_codeman_reserve (tramp_size);
968
969         unwind_ops = mono_arch_get_cie_program ();
970
971         if (mono_thread_get_tls_offset () != -1) {
972                 /* MonoObject* obj is in obj_reg */
973                 /* is obj null? */
974                 s390_ltgr (code, obj_reg, obj_reg);
975                 /* if yes, jump to actual trampoline */
976                 s390_jz (code, 0); CODEPTR(code, jump_obj_null);
977
978                 /* load obj->synchronization to RCX */
979                 s390_lg (code, sync_reg, 0, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
980
981                 if (mono_gc_is_moving ()) {
982                         /*if bit zero is set it's a thin hash*/
983                         s390_tmll (code, sync_reg, 1);
984                         s390_jo   (code, 0); CODEPTR(code, jump_sync_thin_hash);
985
986                         /* Clear bits used by the gc */
987                         s390_nill (code, sync_reg, ~0x3);
988                 }
989
990                 /* is synchronization null? */
991                 s390_ltgr (code, sync_reg, sync_reg);
992                 /* if yes, jump to actual trampoline */
993                 s390_jz (code, 0); CODEPTR(code, jump_sync_null);
994
995                 /* next case: synchronization is not null */
996                 /* load MonoInternalThread* into r5 */
997                 s390_ear (code, s390_r5, 0);
998                 s390_sllg(code, s390_r5, s390_r5, 0, 32);
999                 s390_ear (code, s390_r5, 1);
1000                 /* load TID into r1 */
1001                 s390_lg  (code, s390_r1, 0, s390_r5, mono_thread_get_tls_offset ());
1002                 s390_lgf (code, s390_r1, 0, s390_r1, MONO_STRUCT_OFFSET (MonoInternalThread, small_id));
1003                 /* is synchronization->owner == TID */
1004                 s390_lgf (code, status_reg, 0, sync_reg, status_offset);
1005                 s390_xr  (code, s390_r1, status_reg);
1006                 s390_tmlh (code, s390_r1, OWNER_MASK);
1007                 /* if not, jump to actual trampoline */
1008                 s390_jno (code, 0); CODEPTR(code, jump_not_owned);
1009
1010                 /* next case: synchronization->owner == TID */
1011                 /* is synchronization->nest == 1 */
1012                 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
1013                 s390_chi (code, s390_r0, 1);
1014                 /* if not, jump to next case */
1015                 s390_jne (code, 0); CODEPTR(code, jump_next);
1016                 /* if yes, is synchronization->entry_count greater than zero */
1017                 s390_cfi (code, status_reg, ENTRY_COUNT_WAITERS);
1018                 /* if not, jump to actual trampoline */
1019                 s390_jnz (code, 0); CODEPTR(code, jump_have_waiters);
1020                 /* if yes, try to set synchronization->owner to null and return */
1021                 /* old status in s390_r0 */
1022                 s390_lgfr (code, s390_r0, status_reg);
1023                 /* form new status */
1024                 s390_nilf (code, status_reg, ENTRY_COUNT_MASK);
1025                 /* compare and exchange */
1026                 s390_cs (code, s390_r0, status_reg, sync_reg, status_offset);
1027                 /* if not successful, jump to actual trampoline */
1028                 s390_jnz (code, 0); CODEPTR(code, jump_cs_failed);
1029                 s390_br  (code, s390_r14);
1030
1031                 /* next case: synchronization->nest is not 1 */
1032                 PTRSLOT (code, jump_next);
1033                 /* decrease synchronization->nest and return */
1034                 s390_lgf (code, s390_r0, 0, sync_reg, nest_offset);
1035                 s390_ahi (code, s390_r0, -1);
1036                 s390_st  (code, s390_r0, 0, sync_reg, nest_offset);
1037                 s390_br  (code, s390_r14);
1038
1039                 PTRSLOT (code, jump_obj_null);
1040                 if (jump_sync_thin_hash)
1041                         PTRSLOT (code, jump_sync_thin_hash);
1042                 PTRSLOT (code, jump_have_waiters);
1043                 PTRSLOT (code, jump_not_owned);
1044                 PTRSLOT (code, jump_cs_failed);
1045                 PTRSLOT (code, jump_sync_null);
1046         }
1047
1048         /* jump to the actual trampoline */
1049         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1050
1051         S390_SET (code, s390_r1, tramp);
1052         s390_br (code, s390_r1);
1053
1054         mono_arch_flush_icache (code, code - buf);
1055         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
1056         g_assert (code - buf <= tramp_size);
1057
1058         if (info)
1059                 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
1060
1061         return buf;
1062 }
1063
1064 /*========================= End of Function ========================*/
1065 #endif