[jit] Add support for generating the push/pop the LMF from the LMF stack as IR instea...
[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/appdomain.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/tabledefs.h>
52 #include <mono/arch/s390x/s390x-codegen.h>
53
54 #include "mini.h"
55 #include "mini-s390x.h"
56
57 /*========================= End of Includes ========================*/
58
59 /*------------------------------------------------------------------*/
60 /*                 T y p e d e f s                                  */
61 /*------------------------------------------------------------------*/
62
63 /*========================= End of Typedefs ========================*/
64
65 /*------------------------------------------------------------------*/
66 /*                   P r o t o t y p e s                            */
67 /*------------------------------------------------------------------*/
68
69 /*========================= End of Prototypes ======================*/
70
71 /*------------------------------------------------------------------*/
72 /*                 G l o b a l   V a r i a b l e s                  */
73 /*------------------------------------------------------------------*/
74
75
76 /*====================== End of Global Variables ===================*/
77
78 /*------------------------------------------------------------------*/
79 /*                                                                  */
80 /* Name         - mono_arch_get_unbox_trampoline                    */
81 /*                                                                  */
82 /* Function     - Return a pointer to a trampoline which does the   */
83 /*                unboxing before calling the method.               */
84 /*                                                                  */
85 /*                When value type methods are called through the    */
86 /*                vtable we need to unbox the 'this' argument.      */
87 /*                                                                  */
88 /* Parameters   - method - Methd pointer                            */
89 /*                addr   - Pointer to native code for method        */
90 /*                                                                  */
91 /*------------------------------------------------------------------*/
92
93 gpointer
94 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
95 {
96         guint8 *code, *start;
97         int this_pos = s390_r2;
98         MonoDomain *domain = mono_domain_get ();
99
100         start = code = mono_domain_code_reserve (domain, 28);
101
102         s390_basr (code, s390_r1, 0);
103         s390_j    (code, 6);
104         s390_llong(code, addr);
105         s390_lg   (code, s390_r1, 0, s390_r1, 4);
106         s390_aghi (code, this_pos, sizeof(MonoObject));
107         s390_br   (code, s390_r1);
108
109         g_assert ((code - start) <= 28);
110
111         mono_arch_flush_icache (start, code - start);
112
113         return start;
114 }
115
116 /*========================= End of Function ========================*/
117
118 /*------------------------------------------------------------------*/
119 /*                                                                  */
120 /* Name         - mono_arch_patch_callsite                          */
121 /*                                                                  */
122 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
123 /*                                                                  */
124 /*------------------------------------------------------------------*/
125
126 void
127 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
128 {
129         gint32 displace;
130         unsigned short opcode;
131
132         opcode = *((unsigned short *) (orig_code - 6));
133         if (opcode == 0xc0e5) {
134                 /* This is the 'brasl' instruction */
135                 orig_code    -= 4;
136                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
137                 s390_patch_rel (orig_code, displace);
138                 mono_arch_flush_icache (orig_code, 4);
139         } else {
140                 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
141                 g_assert (orig_code [-8] == 0xe3);
142                 g_assert (orig_code [-7] == 0xe0);
143                 g_assert (orig_code [-6] == 0xd0);
144                 g_assert (orig_code [-5] == 0x04);
145                 g_assert (orig_code [-4] == 0x00);
146                 g_assert (orig_code [-3] == 0x04);
147                 opcode = *((unsigned short*) (orig_code - 2));
148                 g_assert (opcode == 0x0dee);
149
150                 /* The call address is stored in the 8 bytes preceeding the basr instruction */
151                 s390_patch_addr(orig_code - 16, (gssize)addr);
152                 mono_arch_flush_icache (orig_code - 16, 8);
153         }
154 }
155
156 /*========================= End of Function ========================*/
157
158 /*------------------------------------------------------------------*/
159 /*                                                                  */
160 /* Name         - mono_arch_patch_plt_entry.                        */
161 /*                                                                  */
162 /* Function     - Patch a PLT entry - unused as yet.                */
163 /*                                                                  */
164 /*------------------------------------------------------------------*/
165
166 void
167 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
168 {
169         g_assert_not_reached ();
170 }
171
172 /*========================= End of Function ========================*/
173
174 /*------------------------------------------------------------------*/
175 /*                                                                  */
176 /* Name         - mono_arch_nullify_class_init_trampoline           */
177 /*                                                                  */
178 /* Function     - Nullify a call which calls a class init trampoline*/
179 /*                                                                  */
180 /*------------------------------------------------------------------*/
181
182 void
183 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
184 {
185         char patch[2] = {0x07, 0x00};
186
187         code = code - 2;
188
189         memcpy(code, patch, sizeof(patch));
190 }
191
192 /*========================= End of Function ========================*/
193
194 /*------------------------------------------------------------------*/
195 /*                                                                  */
196 /* Name         - mono_arch_get_nullified_class_init                */
197 /*                                                                  */
198 /* Function     - Nullify a PLT entry call.                         */
199 /*                                                                  */
200 /*------------------------------------------------------------------*/
201
202 gpointer
203 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
204 {
205         guint8 *buf, *code;
206
207         code = buf = mono_global_codeman_reserve (16);
208
209         s390_br (code, s390_r14);
210
211         if (info)
212                 *info = mono_tramp_info_create ("nullified_class_init_trampoline", 
213                                                 buf, code - buf, NULL, NULL);
214
215         return (buf);
216 }
217
218 /*========================= End of Function ========================*/
219
220 /*------------------------------------------------------------------*/
221 /*                                                                  */
222 /* Name         - mono_arch_nullify_plt_entry                       */
223 /*                                                                  */
224 /* Function     - Nullify a PLT entry call.                         */
225 /*                                                                  */
226 /*------------------------------------------------------------------*/
227
228 void
229 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
230 {
231         g_assert_not_reached ();
232 }
233
234 /*========================= End of Function ========================*/
235
236 /*------------------------------------------------------------------*/
237 /*                                                                  */
238 /* Name         - mono_arch_create_trampoline_code                  */
239 /*                                                                  */
240 /* Function     - Create the designated type of trampoline according*/
241 /*                to the 'tramp_type' parameter.                    */
242 /*                                                                  */
243 /*------------------------------------------------------------------*/
244
245 guchar*
246 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
247 {
248         char *tramp_name;
249         guint8 *buf, *tramp, *code;
250         int i, offset, lmfOffset;
251         GSList *unwind_ops = NULL;
252         MonoJumpInfo *ji = NULL;
253
254         g_assert (!aot);
255
256         /* Now we'll create in 'buf' the S/390 trampoline code. This
257            is the trampoline code common to all methods  */
258                 
259         code = buf = mono_global_codeman_reserve(512);
260                 
261         /*-----------------------------------------------------------
262           STEP 0: First create a non-standard function prologue with a
263           stack size big enough to save our registers.
264           -----------------------------------------------------------*/
265                 
266         s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
267         s390_lgr  (buf, s390_r11, s390_r15);
268         s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
269         s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
270         s390_stg  (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
271         s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
272
273         /* Save the FP registers */
274         offset = CREATE_FP_OFFSET;
275         for (i = s390_f0; i <= s390_f15; ++i) {
276                 s390_std  (buf, i, 0, STK_BASE, offset);
277                 offset += 8;
278         }
279
280         /*----------------------------------------------------------
281           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
282           LMF. We'll need to restore it after the call to
283           's390_magic_trampoline' and before the call to the native
284           method.
285           ----------------------------------------------------------*/
286                                 
287         s390_basr (buf, s390_r13, 0);
288         s390_j    (buf, 6);
289         s390_llong(buf, mono_get_lmf_addr);
290         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
291         s390_basr (buf, s390_r14, s390_r1);
292
293         /*---------------------------------------------------------------*/
294         /* we build the MonoLMF structure on the stack - see mini-s390.h */
295         /* Keep in sync with the code in mono_arch_emit_prolog           */
296         /*---------------------------------------------------------------*/
297         lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
298                                                                                         
299         s390_lgr   (buf, s390_r13, STK_BASE);
300         s390_aghi  (buf, s390_r13, lmfOffset);  
301                                                                                         
302         /*---------------------------------------------------------------*/     
303         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
304         /*---------------------------------------------------------------*/     
305         s390_stg   (buf, s390_r2, 0, s390_r13,                          
306                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
307                                                                                         
308         /*---------------------------------------------------------------*/     
309         /* Get current lmf                                               */     
310         /*---------------------------------------------------------------*/     
311         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
312                                                                                         
313         /*---------------------------------------------------------------*/     
314         /* Set our lmf as the current lmf                                */     
315         /*---------------------------------------------------------------*/     
316         s390_stg   (buf, s390_r13, 0, s390_r2, 0);                              
317                                                                                         
318         /*---------------------------------------------------------------*/     
319         /* Have our lmf.previous_lmf point to the last lmf               */     
320         /*---------------------------------------------------------------*/     
321         s390_stg   (buf, s390_r0, 0, s390_r13,                          
322                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
323                                                                                         
324         /*---------------------------------------------------------------*/     
325         /* save method info                                              */     
326         /*---------------------------------------------------------------*/     
327         s390_lg    (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
328         s390_stg   (buf, s390_r1, 0, s390_r13,                          
329                             G_STRUCT_OFFSET(MonoLMF, method));                          
330                                                                         
331         /*---------------------------------------------------------------*/     
332         /* save the current SP                                           */     
333         /*---------------------------------------------------------------*/     
334         s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
335         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));  
336                                                                         
337         /*---------------------------------------------------------------*/     
338         /* save the current IP                                           */     
339         /*---------------------------------------------------------------*/     
340         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
341                 s390_lghi  (buf, s390_r1, 0);
342         } else {
343                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
344                 //                      s390_la    (buf, s390_r1, 0, s390_r1, 0);
345         }
346         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));  
347                                                                                         
348         /*---------------------------------------------------------------*/     
349         /* Save general and floating point registers                     */     
350         /*---------------------------------------------------------------*/     
351         s390_mvc   (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]), 
352                     STK_BASE, CREATE_GR_OFFSET);
353         s390_mvc   (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]), 
354                     s390_r11, S390_REG_SAVE_OFFSET);
355
356         /* Simply copy fpregs already saved above                        */
357         s390_mvc   (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
358                     STK_BASE, CREATE_FP_OFFSET);
359
360         /*---------------------------------------------------------------*/
361         /* STEP 2: call the C trampoline function                        */
362         /*---------------------------------------------------------------*/
363                                 
364         /* Set arguments */
365
366         /* Arg 1: mgreg_t *regs. We pass sp instead */
367         s390_la  (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
368                 
369         /* Arg 2: code (next address to the instruction that called us) */
370         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
371                 s390_lghi (buf, s390_r3, 0);
372         } else {
373                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
374         }
375
376         /* Arg 3: Trampoline argument */
377         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
378                 s390_lg (buf, s390_r4, 0, STK_BASE, GENERIC_REG_OFFSET);
379         else
380                 s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
381
382         /* Arg 4: trampoline address. Ignore for now */
383                 
384         /* Calculate call address and call the C trampoline. Return value will be in r2 */
385         s390_basr (buf, s390_r13, 0);
386         s390_j    (buf, 6);
387         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
388         s390_llong (buf, tramp);
389         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
390         s390_basr (buf, s390_r14, s390_r1);
391                 
392         /* OK, code address is now on r2. Move it to r1, so that we
393            can restore r2 and use it from r1 later */
394         s390_lgr  (buf, s390_r1, s390_r2);
395
396         /*----------------------------------------------------------
397           STEP 3: Restore the LMF
398           ----------------------------------------------------------*/
399         restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
400         
401         /*----------------------------------------------------------
402           STEP 4: call the compiled method
403           ----------------------------------------------------------*/
404                 
405         /* Restore registers */
406
407         s390_lmg  (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
408                 
409         /* Restore the FP registers */
410         offset = CREATE_FP_OFFSET;
411         for (i = s390_f0; i <= s390_f15; ++i) {
412                 s390_ld  (buf, i, 0, STK_BASE, offset);
413                 offset += 8;
414         }
415
416         /* Restore stack pointer and jump to the code -
417            R14 contains the return address to our caller */
418         s390_lgr  (buf, STK_BASE, s390_r11);
419         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
420
421         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
422                 s390_lgr (buf, s390_r2, s390_r1);
423                 s390_br  (buf, s390_r14);
424         } else {
425                 s390_br  (buf, s390_r1);
426         }
427
428         /* Flush instruction cache, since we've generated code */
429         mono_arch_flush_icache (code, buf - code);
430         
431         if (info) {
432                 tramp_name = mono_get_generic_trampoline_name (tramp_type);
433                 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
434                 g_free (tramp_name);
435         }
436
437         /* Sanity check */
438         g_assert ((buf - code) <= 512);
439
440         return code;
441 }
442
443 /*========================= End of Function ========================*/
444
445 /*------------------------------------------------------------------*/
446 /*                                                                  */
447 /* Name         - mono_arch_create_specific_trampoline              */
448 /*                                                                  */
449 /* Function     - Creates the given kind of specific trampoline     */
450 /*                                                                  */
451 /*------------------------------------------------------------------*/
452
453 gpointer
454 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
455 {
456         guint8 *code, *buf, *tramp;
457         gint32 displace;
458
459         tramp = mono_get_trampoline_code (tramp_type);
460
461         /*----------------------------------------------------------*/
462         /* This is the method-specific part of the trampoline. Its  */
463         /* purpose is to provide the generic part with the          */
464         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
465         /*----------------------------------------------------------*/
466         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
467
468         s390_basr (buf, s390_r1, 0);
469         s390_j    (buf, 6);
470         s390_llong(buf, arg1);
471         s390_lg   (buf, s390_r1, 0, s390_r1, 4);
472         displace = (tramp - buf) / 2;
473         s390_jg   (buf, displace);
474
475         /* Flush instruction cache, since we've generated code */
476         mono_arch_flush_icache (code, buf - code);
477
478         /* Sanity check */
479         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
480
481         if (code_len)
482                 *code_len = buf - code;
483         
484         return code;
485 }       
486
487 /*========================= End of Function ========================*/
488
489 /*------------------------------------------------------------------*/
490 /*                                                                  */
491 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
492 /*                                                                  */
493 /* Function     -                                                   */
494 /*                                                                  */
495 /*------------------------------------------------------------------*/
496
497 gpointer
498 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
499 {
500 #ifdef MONO_ARCH_VTABLE_REG
501         guint8 *tramp;
502         guint8 *code, *buf;
503         guint8 **rgctx_null_jumps;
504         gint32 displace;
505         int tramp_size,
506             depth, 
507             index, 
508             iPatch = 0,
509             i;
510         gboolean mrgctx;
511         MonoJumpInfo *ji = NULL;
512         GSList *unwind_ops = NULL;
513
514         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
515         index = MONO_RGCTX_SLOT_INDEX (slot);
516         if (mrgctx)
517                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
518         for (depth = 0; ; ++depth) {
519                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
520
521                 if (index < size - 1)
522                         break;
523                 index -= size - 1;
524         }
525
526         tramp_size = 48 + 16 * depth;
527         if (mrgctx)
528                 tramp_size += 4;
529         else
530                 tramp_size += 12;
531
532         code = buf = mono_global_codeman_reserve (tramp_size);
533
534         unwind_ops = mono_arch_get_cie_program ();
535
536         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
537
538         if (mrgctx) {
539                 /* get mrgctx ptr */
540                 s390_lgr (code, s390_r1, s390_r2);
541         } else {
542                 /* load rgctx ptr from vtable */
543                 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
544                 /* is the rgctx ptr null? */
545                 s390_ltgr (code, s390_r1, s390_r1);
546                 /* if yes, jump to actual trampoline */
547                 rgctx_null_jumps [iPatch++] = code;
548                 s390_jge (code, 0);
549         }
550
551         for (i = 0; i < depth; ++i) {
552                 /* load ptr to next array */
553                 if (mrgctx && i == 0)
554                         s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
555                 else
556                         s390_lg (code, s390_r1, 0, s390_r1, 0);
557                 s390_ltgr (code, s390_r1, s390_r1);
558                 /* if the ptr is null then jump to actual trampoline */
559                 rgctx_null_jumps [iPatch++] = code;
560                 s390_jge (code, 0);
561         }
562
563         /* fetch slot */
564         s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index  + 1)));
565         /* is the slot null? */
566         s390_ltgr (code, s390_r1, s390_r1);
567         /* if yes, jump to actual trampoline */
568         rgctx_null_jumps [iPatch++] = code;
569         s390_jge (code, 0);
570         /* otherwise return r1 */
571         s390_lgr (code, s390_r2, s390_r1);
572         s390_br  (code, s390_r14);
573
574         for (i = 0; i < iPatch; i++) {
575                 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
576                 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
577         }
578
579         g_free (rgctx_null_jumps);
580
581         /* move the rgctx pointer to the VTABLE register */
582         s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
583
584         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
585                 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
586
587         /* jump to the actual trampoline */
588         displace = (tramp - code) / 2;
589         s390_jg (code, displace);
590
591         mono_arch_flush_icache (buf, code - buf);
592
593         g_assert (code - buf <= tramp_size);
594
595         if (info) {
596                 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
597                 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
598                 g_free (name);
599         }
600
601         return(buf);
602 #else
603         g_assert_not_reached ();
604 #endif
605         return(NULL);
606 }       
607
608 /*========================= End of Function ========================*/
609
610 /*------------------------------------------------------------------*/
611 /*                                                                  */
612 /* Name         - mono_arch_get_static_rgctx_trampoline             */
613 /*                                                                  */
614 /* Function     - Create a trampoline which sets RGCTX_REG to MRGCTX*/
615 /*                then jumps to ADDR.                               */
616 /*                                                                  */
617 /*------------------------------------------------------------------*/
618
619 gpointer
620 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, 
621                                         MonoMethodRuntimeGenericContext *mrgctx, 
622                                         gpointer addr)
623 {
624         guint8 *code, *start;
625         gint32 displace;
626         int buf_len;
627
628         MonoDomain *domain = mono_domain_get ();
629
630         buf_len = 32;
631
632         start = code = mono_domain_code_reserve (domain, buf_len);
633
634         s390_basr (code, s390_r1, 0);
635         s390_j    (code, 6);
636         s390_llong(code, mrgctx);
637         s390_lg   (code, MONO_ARCH_RGCTX_REG, 0, s390_r1, 4);
638         displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
639         s390_jg   (code, displace);
640         g_assert ((code - start) < buf_len);
641
642         mono_arch_flush_icache (start, code - start);
643
644         return(start);
645 }       
646
647 /*========================= End of Function ========================*/
648
649 /*------------------------------------------------------------------*/
650 /*                                                                  */
651 /* Name         - mono_arch_create_generic_class_init_trampoline    */
652 /*                                                                  */
653 /* Function     -                                                   */
654 /*                                                                  */
655 /*------------------------------------------------------------------*/
656
657 gpointer
658 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
659 {
660         guint8 *tramp;
661         guint8 *code, *buf;
662         static int byte_offset = -1;
663         static guint8 bitmask;
664         guint8 *jump;
665         gint32 displace;
666         int tramp_size;
667         GSList *unwind_ops = NULL;
668         MonoJumpInfo *ji = NULL;
669
670         tramp_size = 48;
671
672         code = buf = mono_global_codeman_reserve (tramp_size);
673
674         unwind_ops = mono_arch_get_cie_program ();
675
676         if (byte_offset < 0)
677                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
678
679         s390_llgc(code, s390_r0, 0, MONO_ARCH_VTABLE_REG, byte_offset);
680         s390_nill(code, s390_r0, bitmask);
681         s390_bnzr(code, s390_r14);
682
683         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
684                 mono_get_root_domain (), NULL);
685
686         /* jump to the actual trampoline */
687         displace = (tramp - code) / 2;
688         s390_jg (code, displace);
689
690         mono_arch_flush_icache (buf, code - buf);
691
692         g_assert (code - buf <= tramp_size);
693
694         if (info)
695                 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
696
697         return(buf);
698 }
699
700 /*========================= End of Function ========================*/