Merge pull request #2311 from mlancione/master
[mono.git] / mono / mini / tramp-s390x.c
1 /*------------------------------------------------------------------*/
2 /*                                                                  */
3 /* Name        - tramp-s390x.c                                      */
4 /*                                                                  */
5 /* Function    - JIT trampoline code for S/390.                     */
6 /*                                                                  */
7 /* Name        - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /*                                                                  */
9 /* Date        - January, 2004                                      */
10 /*                                                                  */
11 /* Derivation  - From exceptions-x86 & exceptions-ppc               */
12 /*               Paolo Molaro (lupus@ximian.com)                    */
13 /*               Dietmar Maurer (dietmar@ximian.com)                */
14 /*                                                                  */
15 /* Copyright   - 2001 Ximian, Inc.                                  */
16 /*                                                                  */
17 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
23 #define LMFReg  s390_r13
24
25 /*
26  * Method-specific trampoline code fragment sizes                   
27  */
28 #define SPECIFIC_TRAMPOLINE_SIZE        96
29
30 /*========================= End of Defines =========================*/
31
32 /*------------------------------------------------------------------*/
33 /*                 I n c l u d e s                                  */
34 /*------------------------------------------------------------------*/
35
36 #include <config.h>
37 #include <glib.h>
38 #include <string.h>
39
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/gc-internals.h>
43 #include <mono/metadata/marshal.h>
44 #include <mono/metadata/profiler-private.h>
45 #include <mono/metadata/tabledefs.h>
46 #include <mono/arch/s390x/s390x-codegen.h>
47
48 #include "mini.h"
49 #include "mini-s390x.h"
50 #include "support-s390x.h"
51
52 /*========================= End of Includes ========================*/
53
54 /*------------------------------------------------------------------*/
55 /*                 T y p e d e f s                                  */
56 /*------------------------------------------------------------------*/
57
58 typedef struct {
59         guint8  stk[S390_MINIMAL_STACK_SIZE];   /* Standard s390x stack */
60         struct MonoLMF  LMF;                    /* LMF                  */
61 } trampStack_t;
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         char trampName[128];
100
101         start = code = mono_domain_code_reserve (domain, 28);
102
103         S390_SET  (code, s390_r1, addr);
104         s390_aghi (code, this_pos, sizeof(MonoObject));
105         s390_br   (code, s390_r1);
106
107         g_assert ((code - start) <= 28);
108
109         mono_arch_flush_icache (start, code - start);
110         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
111
112         snprintf(trampName, sizeof(trampName), "%s_unbox_trampoline", method->name);
113
114         mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
115
116         return start;
117 }
118
119 /*========================= End of Function ========================*/
120
121 /*------------------------------------------------------------------*/
122 /*                                                                  */
123 /* Name         - mono_arch_patch_callsite                          */
124 /*                                                                  */
125 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
126 /*                                                                  */
127 /*------------------------------------------------------------------*/
128
129 void
130 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
131 {
132         gint32 displace;
133         unsigned short opcode;
134
135         opcode = *((unsigned short *) (orig_code - 2));
136         if (opcode == 0x0dee) {
137                 /* This should be a 'iihf/iilf' sequence */
138                 S390_EMIT_CALL((orig_code - 14), addr);
139                 mono_arch_flush_icache (orig_code - 14, 12);
140         } else {
141                 /* This is the 'brasl' instruction */
142                 orig_code    -= 4;
143                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
144                 s390_patch_rel (orig_code, displace);
145                 mono_arch_flush_icache (orig_code, 4);
146         }
147 }
148
149 /*========================= End of Function ========================*/
150
151 /*------------------------------------------------------------------*/
152 /*                                                                  */
153 /* Name         - mono_arch_patch_plt_entry.                        */
154 /*                                                                  */
155 /* Function     - Patch a PLT entry - unused as yet.                */
156 /*                                                                  */
157 /*------------------------------------------------------------------*/
158
159 void
160 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
161 {
162         g_assert_not_reached ();
163 }
164
165 /*========================= End of Function ========================*/
166
167 /*------------------------------------------------------------------*/
168 /*                                                                  */
169 /* Name         - mono_arch_create_trampoline_code                  */
170 /*                                                                  */
171 /* Function     - Create the designated type of trampoline according*/
172 /*                to the 'tramp_type' parameter.                    */
173 /*                                                                  */
174 /*------------------------------------------------------------------*/
175
176 guchar*
177 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
178 {
179         char *tramp_name;
180         guint8 *buf, *tramp, *code;
181         int i, offset, has_caller;
182         GSList *unwind_ops = NULL;
183         MonoJumpInfo *ji = NULL;
184
185         g_assert (!aot);
186
187         /* Now we'll create in 'buf' the S/390 trampoline code. This
188            is the trampoline code common to all methods  */
189                 
190         code = buf = mono_global_codeman_reserve(512);
191                 
192         if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
193             (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)) 
194                 has_caller = 0;
195         else
196                 has_caller = 1;
197
198         /*-----------------------------------------------------------
199           STEP 0: First create a non-standard function prologue with a
200           stack size big enough to save our registers.
201           -----------------------------------------------------------*/
202                 
203         s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
204         s390_lgr  (buf, s390_r11, s390_r15);
205         s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
206         s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
207
208         /*---------------------------------------------------------------*/
209         /* we build the MonoLMF structure on the stack - see mini-s390.h */
210         /* Keep in sync with the code in mono_arch_emit_prolog           */
211         /*---------------------------------------------------------------*/
212                                                                                         
213         s390_lgr   (buf, LMFReg, STK_BASE);
214         s390_aghi  (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
215                                                                                         
216         /*---------------------------------------------------------------*/     
217         /* Save general and floating point registers in LMF              */     
218         /*---------------------------------------------------------------*/     
219         s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
220         s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
221         s390_mvc  (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
222                    s390_r11, S390_REG_SAVE_OFFSET);
223
224         offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
225         for (i = s390_f0; i <= s390_f15; ++i) {
226                 s390_std  (buf, i, 0, LMFReg, offset);
227                 offset += sizeof(gdouble);
228         }
229
230         /*----------------------------------------------------------
231           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
232           LMF. We'll need to restore it after the call to
233           's390_magic_trampoline' and before the call to the native
234           method.
235           ----------------------------------------------------------*/
236                                 
237         S390_SET  (buf, s390_r1, mono_get_lmf_addr);
238         s390_basr (buf, s390_r14, s390_r1);
239                                                                                         
240         /*---------------------------------------------------------------*/     
241         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
242         /*---------------------------------------------------------------*/     
243         s390_stg   (buf, s390_r2, 0, LMFReg,                            
244                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
245                                                                                         
246         /*---------------------------------------------------------------*/     
247         /* Get current lmf                                               */     
248         /*---------------------------------------------------------------*/     
249         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
250                                                                                         
251         /*---------------------------------------------------------------*/     
252         /* Set our lmf as the current lmf                                */     
253         /*---------------------------------------------------------------*/     
254         s390_stg   (buf, LMFReg, 0, s390_r2, 0);                                
255                                                                                         
256         /*---------------------------------------------------------------*/     
257         /* Have our lmf.previous_lmf point to the last lmf               */     
258         /*---------------------------------------------------------------*/     
259         s390_stg   (buf, s390_r0, 0, LMFReg,                            
260                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
261                                                                                         
262         /*---------------------------------------------------------------*/     
263         /* save method info                                              */     
264         /*---------------------------------------------------------------*/     
265         s390_lg    (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
266         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));                         
267                                                                         
268         /*---------------------------------------------------------------*/     
269         /* save the current SP                                           */     
270         /*---------------------------------------------------------------*/     
271         s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
272         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));    
273                                                                         
274         /*---------------------------------------------------------------*/     
275         /* save the current IP                                           */     
276         /*---------------------------------------------------------------*/     
277         if (has_caller) {
278                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
279         } else {
280                 s390_lghi  (buf, s390_r1, 0);
281         }
282         s390_stg   (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));    
283                                                                                         
284         /*---------------------------------------------------------------*/
285         /* STEP 2: call the C trampoline function                        */
286         /*---------------------------------------------------------------*/
287                                 
288         /* Set arguments */
289
290         /* Arg 1: mgreg_t *regs */
291         s390_la  (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
292                 
293         /* Arg 2: code (next address to the instruction that called us) */
294         if (has_caller) {
295                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
296         } else {
297                 s390_lghi (buf, s390_r3, 0);
298         }
299
300         /* Arg 3: Trampoline argument */
301         s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
302
303         /* Arg 4: trampoline address. */
304         S390_SET (buf, s390_r5, buf);
305                 
306         /* Calculate call address and call the C trampoline. Return value will be in r2 */
307         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
308         S390_SET  (buf, s390_r1, tramp);
309         s390_basr (buf, s390_r14, s390_r1);
310                 
311         /* OK, code address is now on r2. Move it to r1, so that we
312            can restore r2 and use it from r1 later */
313         s390_lgr  (buf, s390_r1, s390_r2);
314
315         /*----------------------------------------------------------
316           STEP 3: Restore the LMF
317           ----------------------------------------------------------*/
318         restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
319         
320         /*----------------------------------------------------------
321           STEP 4: call the compiled method
322           ----------------------------------------------------------*/
323                 
324         /* Restore parameter registers */
325         s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
326                 
327         /* Restore the FP registers */
328         offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
329         for (i = s390_f0; i <= s390_f15; ++i) {
330                 s390_ld  (buf, i, 0, LMFReg, offset);
331                 offset += sizeof(gdouble);
332         }
333
334         /* Restore stack pointer and jump to the code -
335          * R14 contains the return address to our caller 
336          */
337         s390_lgr  (buf, STK_BASE, s390_r11);
338         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
339
340         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
341                 s390_lgr (buf, s390_r2, s390_r1);
342                 s390_br  (buf, s390_r14);
343         } else {
344                 s390_br  (buf, s390_r1);
345         }
346
347         /* Flush instruction cache, since we've generated code */
348         mono_arch_flush_icache (code, buf - code);
349         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
350         
351         g_assert (info);
352         tramp_name = mono_get_generic_trampoline_name (tramp_type);
353         *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
354         g_free (tramp_name);
355
356         /* Sanity check */
357         g_assert ((buf - code) <= 512);
358
359         return code;
360 }
361
362 /*========================= End of Function ========================*/
363
364 /*------------------------------------------------------------------*/
365 /*                                                                  */
366 /* Name         - mono_arch_invalidate_method                       */
367 /*                                                                  */
368 /* Function     -                                                   */
369 /*                                                                  */
370 /*------------------------------------------------------------------*/
371
372 void
373 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
374 {
375         /* FIXME: This is not thread safe */
376         guint8 *code = ji->code_start;
377
378         S390_SET  (code, s390_r1, func);
379         S390_SET  (code, s390_r2, func_arg);
380         s390_br   (code, s390_r1);
381
382 }
383
384 /*========================= End of Function ========================*/
385
386 /*------------------------------------------------------------------*/
387 /*                                                                  */
388 /* Name         - mono_arch_create_specific_trampoline              */
389 /*                                                                  */
390 /* Function     - Creates the given kind of specific trampoline     */
391 /*                                                                  */
392 /*------------------------------------------------------------------*/
393
394 gpointer
395 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
396 {
397         guint8 *code, *buf, *tramp;
398         gint32 displace;
399
400         tramp = mono_get_trampoline_code (tramp_type);
401
402         /*----------------------------------------------------------*/
403         /* This is the method-specific part of the trampoline. Its  */
404         /* purpose is to provide the generic part with the          */
405         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
406         /*----------------------------------------------------------*/
407         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
408
409         S390_SET  (buf, s390_r1, arg1);
410         displace = (tramp - buf) / 2;
411         s390_jg   (buf, displace);
412
413         /* Flush instruction cache, since we've generated code */
414         mono_arch_flush_icache (code, buf - code);
415         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, 
416                                        (void *) mono_get_generic_trampoline_simple_name (tramp_type));
417
418         /* Sanity check */
419         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
420
421         if (code_len)
422                 *code_len = buf - code;
423         
424         return code;
425 }       
426
427 /*========================= End of Function ========================*/
428
429 /*------------------------------------------------------------------*/
430 /*                                                                  */
431 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
432 /*                                                                  */
433 /* Function     -                                                   */
434 /*                                                                  */
435 /*------------------------------------------------------------------*/
436
437 gpointer
438 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
439 {
440         guint8 *tramp;
441         guint8 *code, *buf;
442         guint8 **rgctx_null_jumps;
443         gint32 displace;
444         int tramp_size,
445             depth, 
446             index, 
447             iPatch = 0,
448             i;
449         gboolean mrgctx;
450         MonoJumpInfo *ji = NULL;
451         GSList *unwind_ops = NULL;
452
453         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
454         index = MONO_RGCTX_SLOT_INDEX (slot);
455         if (mrgctx)
456                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
457         for (depth = 0; ; ++depth) {
458                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
459
460                 if (index < size - 1)
461                         break;
462                 index -= size - 1;
463         }
464
465         tramp_size = 48 + 16 * depth;
466         if (mrgctx)
467                 tramp_size += 4;
468         else
469                 tramp_size += 12;
470
471         code = buf = mono_global_codeman_reserve (tramp_size);
472
473         unwind_ops = mono_arch_get_cie_program ();
474
475         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
476
477         if (mrgctx) {
478                 /* get mrgctx ptr */
479                 s390_lgr (code, s390_r1, s390_r2);
480         } else {
481                 /* load rgctx ptr from vtable */
482                 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
483                 /* is the rgctx ptr null? */
484                 s390_ltgr (code, s390_r1, s390_r1);
485                 /* if yes, jump to actual trampoline */
486                 rgctx_null_jumps [iPatch++] = code;
487                 s390_jge (code, 0);
488         }
489
490         for (i = 0; i < depth; ++i) {
491                 /* load ptr to next array */
492                 if (mrgctx && i == 0)
493                         s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
494                 else
495                         s390_lg (code, s390_r1, 0, s390_r1, 0);
496                 s390_ltgr (code, s390_r1, s390_r1);
497                 /* if the ptr is null then jump to actual trampoline */
498                 rgctx_null_jumps [iPatch++] = code;
499                 s390_jge (code, 0);
500         }
501
502         /* fetch slot */
503         s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index  + 1)));
504         /* is the slot null? */
505         s390_ltgr (code, s390_r1, s390_r1);
506         /* if yes, jump to actual trampoline */
507         rgctx_null_jumps [iPatch++] = code;
508         s390_jge (code, 0);
509         /* otherwise return r1 */
510         s390_lgr (code, s390_r2, s390_r1);
511         s390_br  (code, s390_r14);
512
513         for (i = 0; i < iPatch; i++) {
514                 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
515                 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
516         }
517
518         g_free (rgctx_null_jumps);
519
520         /* move the rgctx pointer to the VTABLE register */
521         s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
522
523         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
524                 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
525
526         /* jump to the actual trampoline */
527         displace = (tramp - code) / 2;
528         s390_jg (code, displace);
529
530         mono_arch_flush_icache (buf, code - buf);
531         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
532
533         g_assert (code - buf <= tramp_size);
534
535         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
536         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
537         g_free (name);
538
539         return(buf);
540 }       
541
542 /*========================= End of Function ========================*/
543
544 /*------------------------------------------------------------------*/
545 /*                                                                  */
546 /* Name         - mono_arch_get_static_rgctx_trampoline             */
547 /*                                                                  */
548 /* Function     - Create a trampoline which sets RGCTX_REG to MRGCTX*/
549 /*                then jumps to ADDR.                               */
550 /*                                                                  */
551 /*------------------------------------------------------------------*/
552
553 gpointer
554 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, 
555                                         MonoMethodRuntimeGenericContext *mrgctx, 
556                                         gpointer addr)
557 {
558         guint8 *code, *start;
559         gint32 displace;
560         int buf_len;
561         char trampName[128];
562
563         MonoDomain *domain = mono_domain_get ();
564
565         buf_len = 32;
566
567         start = code = mono_domain_code_reserve (domain, buf_len);
568
569         S390_SET  (code, MONO_ARCH_RGCTX_REG, mrgctx);
570         displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
571         s390_jg   (code, displace);
572         g_assert ((code - start) < buf_len);
573
574         mono_arch_flush_icache (start, code - start);
575         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
576
577         snprintf(trampName, sizeof(trampName), "%s_rgctx_trampoline", m->name);
578
579         mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
580
581         return(start);
582 }       
583
584 /*========================= End of Function ========================*/
585
586 /*------------------------------------------------------------------*/
587 /*                                                                  */
588 /* Name         - handler_block_trampoline_helper                   */
589 /*                                                                  */
590 /* Function     -                                                   */
591 /*                                                                  */
592 /*------------------------------------------------------------------*/
593
594 static void
595 handler_block_trampoline_helper (gpointer *ptr)
596 {
597         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
598         *ptr = jit_tls->handler_block_return_address;
599 }
600
601 /*========================= End of Function ========================*/
602
603 /*------------------------------------------------------------------*/
604 /*                                                                  */
605 /* Name         - mono_arch_create_handler_block_trampoline         */
606 /*                                                                  */
607 /* Function     -                                                   */
608 /*                                                                  */
609 /*------------------------------------------------------------------*/
610
611 gpointer
612 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
613 {
614         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
615         guint8 *code, *buf;
616         int tramp_size = 64;
617         MonoJumpInfo *ji = NULL;
618         GSList *unwind_ops = NULL;
619
620         g_assert (!aot);
621
622         code = buf = mono_global_codeman_reserve (tramp_size);
623
624         /*
625          * This trampoline restore the call chain of the handler block 
626          * then jumps into the code that deals with it.
627          */
628
629         if (mono_get_jit_tls_offset () != -1) {
630                 s390_ear  (code, s390_r1, 0);
631                 s390_sllg (code, s390_r1, s390_r1, 0, 32);
632                 s390_ear  (code, s390_r1, 1);
633                 S390_SET  (code, s390_r14, mono_get_jit_tls_offset());
634                 s390_lg   (code, s390_r14, s390_r1, 0, G_STRUCT_OFFSET(MonoJitTlsData, handler_block_return_address));
635                 /* 
636                  * Simulate a call 
637                  */
638                 S390_SET  (code, s390_r1, tramp);
639                 s390_br   (code, s390_r1);
640         } else {
641                 /*
642                  * Slow path uses a C helper
643                  */
644                 S390_SET  (code, s390_r2, tramp);
645                 S390_SET  (code, s390_r1, handler_block_trampoline_helper);
646                 s390_br   (code, s390_r1);
647         }
648
649         mono_arch_flush_icache (buf, code - buf);
650         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
651         g_assert (code - buf <= tramp_size);
652
653         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
654
655         return buf;
656 }
657
658 /*========================= End of Function ========================*/