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