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