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