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