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