2010-05-05 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-s390x.c
1 /*------------------------------------------------------------------*/
2 /*                                                                  */
3 /* Name        - tramp-s390.c                                       */
4 /*                                                                  */
5 /* Function    - JIT trampoline code for S/390.                     */
6 /*                                                                  */
7 /* Name        - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /*                                                                  */
9 /* Date        - January, 2004                                      */
10 /*                                                                  */
11 /* Derivation  - From exceptions-x86 & exceptions-ppc               */
12 /*               Paolo Molaro (lupus@ximian.com)                    */
13 /*               Dietmar Maurer (dietmar@ximian.com)                */
14 /*                                                                  */
15 /* Copyright   - 2001 Ximian, Inc.                                  */
16 /*                                                                  */
17 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
23 #define GR_SAVE_SIZE            4*sizeof(long)
24 #define FP_SAVE_SIZE            16*sizeof(double)
25 #define METHOD_SAVE_OFFSET      S390_MINIMAL_STACK_SIZE
26 #define CREATE_GR_OFFSET        METHOD_SAVE_OFFSET+8
27 #define CREATE_FP_OFFSET        CREATE_GR_OFFSET+GR_SAVE_SIZE
28 #define CREATE_LMF_OFFSET       CREATE_FP_OFFSET+FP_SAVE_SIZE
29 #define CREATE_STACK_SIZE       (CREATE_LMF_OFFSET+2*sizeof(long)+sizeof(MonoLMF))
30
31 /*------------------------------------------------------------------*/
32 /* Method-specific trampoline code fragment sizes                   */
33 /*------------------------------------------------------------------*/
34 #define SPECIFIC_TRAMPOLINE_SIZE        96
35
36 /*========================= End of Defines =========================*/
37
38 /*------------------------------------------------------------------*/
39 /*                 I n c l u d e s                                  */
40 /*------------------------------------------------------------------*/
41
42 #include <config.h>
43 #include <glib.h>
44 #include <string.h>
45
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/marshal.h>
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/arch/s390x/s390x-codegen.h>
50
51 #include "mini.h"
52 #include "mini-s390x.h"
53
54 /*========================= End of Includes ========================*/
55
56 /*------------------------------------------------------------------*/
57 /*                 T y p e d e f s                                  */
58 /*------------------------------------------------------------------*/
59
60 /*========================= End of Typedefs ========================*/
61
62 /*------------------------------------------------------------------*/
63 /*                   P r o t o t y p e s                            */
64 /*------------------------------------------------------------------*/
65
66 /*========================= End of Prototypes ======================*/
67
68 /*------------------------------------------------------------------*/
69 /*                 G l o b a l   V a r i a b l e s                  */
70 /*------------------------------------------------------------------*/
71
72
73 /*====================== End of Global Variables ===================*/
74
75 /*------------------------------------------------------------------*/
76 /*                                                                  */
77 /* Name         - mono_arch_get_unbox_trampoline                    */
78 /*                                                                  */
79 /* Function     - Return a pointer to a trampoline which does the   */
80 /*                unboxing before calling the method.               */
81 /*                                                                  */
82 /*                When value type methods are called through the    */
83 /*                vtable we need to unbox the 'this' argument.      */
84 /*                                                                  */
85 /* Parameters   - gsctx  - Generic sharing context                  */
86 /*                method - Methd pointer                            */
87 /*                addr   - Pointer to native code for method        */
88 /*                                                                  */
89 /*------------------------------------------------------------------*/
90
91 gpointer
92 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *method, gpointer addr)
93 {
94         guint8 *code, *start;
95         int this_pos = s390_r2;
96         MonoDomain *domain = mono_domain_get ();
97
98         start = addr;
99         if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
100                 this_pos = s390_r3;
101
102         start = code = mono_domain_code_reserve (domain, 28);
103
104         s390_basr (code, s390_r1, 0);
105         s390_j    (code, 6);
106         s390_llong(code, addr);
107         s390_lg   (code, s390_r1, 0, s390_r1, 4);
108         s390_aghi (code, this_pos, sizeof(MonoObject));
109         s390_br   (code, s390_r1);
110
111         g_assert ((code - start) <= 28);
112
113         mono_arch_flush_icache (start, code - start);
114
115         return start;
116 }
117
118 /*========================= End of Function ========================*/
119
120 /*------------------------------------------------------------------*/
121 /*                                                                  */
122 /* Name         - mono_arch_patch_callsite                          */
123 /*                                                                  */
124 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
125 /*                                                                  */
126 /*------------------------------------------------------------------*/
127
128 void
129 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
130 {
131         gint32 displace;
132         unsigned short opcode;
133
134         opcode = *((unsigned short *) (orig_code - 6));
135         if (opcode == 0xc0e5) {
136                 /* This is the 'brasl' instruction */
137                 orig_code    -= 4;
138                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
139                 s390_patch_rel (orig_code, displace);
140                 mono_arch_flush_icache (orig_code, 4);
141         } else {
142                 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
143                 g_assert (orig_code [-8] == 0xe3);
144                 g_assert (orig_code [-7] == 0xe0);
145                 g_assert (orig_code [-6] == 0xd0);
146                 g_assert (orig_code [-5] == 0x04);
147                 g_assert (orig_code [-4] == 0x00);
148                 g_assert (orig_code [-3] == 0x04);
149                 opcode = *((unsigned short*) (orig_code - 2));
150                 g_assert (opcode == 0x0dee);
151
152                 /* The call address is stored in the 8 bytes preceeding the basr instruction */
153                 s390_patch_addr(orig_code - 16, (gssize)addr);
154                 mono_arch_flush_icache (orig_code - 16, 8);
155         }
156 }
157
158 /*========================= End of Function ========================*/
159
160 /*------------------------------------------------------------------*/
161 /*                                                                  */
162 /* Name         - mono_arch_patch_plt_entry.                        */
163 /*                                                                  */
164 /* Function     - Patch a PLT entry - unused as yet.                */
165 /*                                                                  */
166 /*------------------------------------------------------------------*/
167
168 void
169 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
170 {
171         g_assert_not_reached ();
172 }
173
174 /*========================= End of Function ========================*/
175
176 /*------------------------------------------------------------------*/
177 /*                                                                  */
178 /* Name         - mono_arch_nullify_class_init_trampoline           */
179 /*                                                                  */
180 /* Function     - Nullify a call which calls a class init trampoline*/
181 /*                                                                  */
182 /*------------------------------------------------------------------*/
183
184 void
185 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
186 {
187         char patch[2] = {0x07, 0x00};
188
189         code = code - 2;
190
191         memcpy(code, patch, sizeof(patch));
192 }
193
194 /*========================= End of Function ========================*/
195
196 /*------------------------------------------------------------------*/
197 /*                                                                  */
198 /* Name         - mono_arch_nullify_plt_entry                       */
199 /*                                                                  */
200 /* Function     - Nullify a PLT entry call.                         */
201 /*                                                                  */
202 /*------------------------------------------------------------------*/
203
204 void
205 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
206 {
207         g_assert_not_reached ();
208 }
209
210 /*========================= End of Function ========================*/
211
212 /*------------------------------------------------------------------*/
213 /*                                                                  */
214 /* Name         - mono_arch_get_vcall_slot                          */
215 /*                                                                  */
216 /* Function     - This method is called by the arch independent     */
217 /*            trampoline code to determine the vtable slot used by  */
218 /*            the call which invoked the trampoline.                */
219 /*                                                                  */
220 /* Parameters   - code   - Pointer into caller code                 */
221 /*                regs   - Register state at the point of the call  */
222 /*                displacement - Out parameter which will receive   */
223 /*                the displacement of the vtable slot               */
224 /*                                                                  */
225 /*------------------------------------------------------------------*/
226
227 gpointer
228 mono_arch_get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
229 {
230         int reg, lkReg;
231         guchar* base;
232         unsigned short opcode;
233         char *sp;
234         MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
235
236         // We are passed sp instead of the register array
237 #if 0
238         sp = (char *) regs;
239 #endif
240         sp = (char *) lmf->gregs[s390_r15];
241
242         *displacement = 0;
243
244         opcode = *((unsigned short *) (code - 6));
245         if (opcode == 0xc0e5)
246                 /* This is the 'brasl' instruction */
247                 return NULL;
248
249         /*-----------------------------------*/
250         /* This is a basr r14,Rz instruction */
251         /* If it's preceded by a LG Rx,d(Ry) */
252         /* If Rz == 1 then this is virtual   */
253         /* call.                             */
254         /*-----------------------------------*/
255         code    -= 6;
256
257         /*-----------------------------------*/
258         /* If call is preceded by LGR then   */
259         /* there's nothing to patch          */
260         /*-----------------------------------*/
261         if ((code[0] == 0xb9) && (code[1] == 0x04))
262                 return NULL;
263
264         /*-----------------------------------*/
265         /* We back up until we're pointing at*/
266         /* the base/displacement portion of  */
267         /* the LG instruction                */
268         /*-----------------------------------*/
269         lkReg    = code[5] & 0x0f;
270
271         /*-----------------------------------*/
272         /* The LG instruction has format:    */
273         /* E3x0ylllhh04 - where:             */
274         /* x = Rx; y = Ry;                   */
275         /* lll = low 12 bits of displacement */
276         /* hh  = high 8 bits of displacement */
277         /*-----------------------------------*/
278         reg      = code[0] >> 4;
279         *displacement = (code[2] << 12) |
280                         ((code[0] & 0x0f) << 8) |
281                         code[1];
282
283         if (code[2] & 0x80)
284                 *displacement |= 0xfff00000;
285
286         base = ((guchar *) lmf->gregs[reg]);
287 #if 0
288         if (reg > 5)
289                 base = *((guchar **) (sp + S390_REG_SAVE_OFFSET +
290                                                           sizeof(long)*(reg-6)));
291         else
292                 base = *((guchar **) ((sp - CREATE_STACK_SIZE) +
293                                                           CREATE_GR_OFFSET +
294                                                           sizeof(long)*(reg-2)));
295 #endif
296         if (lkReg != 1)
297                 /* Non virtual call */
298                 return NULL;
299
300         return base;
301 }
302
303 /*========================= End of Function ========================*/
304
305 /*------------------------------------------------------------------*/
306 /*                                                                  */
307 /* Name         - mono_arch_create_trampoline_code                  */
308 /*                                                                  */
309 /* Function     - Create the designated type of trampoline according*/
310 /*                to the 'tramp_type' parameter.                    */
311 /*                                                                  */
312 /*------------------------------------------------------------------*/
313
314 guchar*
315 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
316 {
317         guint8 *buf, *tramp, *code;
318         int i, offset, lmfOffset;
319
320         g_assert (!aot);
321         if (info)
322                 *info = NULL;
323
324         /* Now we'll create in 'buf' the S/390 trampoline code. This
325            is the trampoline code common to all methods  */
326                 
327         code = buf = mono_global_codeman_reserve(512);
328                 
329         /*-----------------------------------------------------------
330           STEP 0: First create a non-standard function prologue with a
331           stack size big enough to save our registers.
332           -----------------------------------------------------------*/
333                 
334         s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
335         s390_lgr  (buf, s390_r11, s390_r15);
336         s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
337         s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
338         s390_stg  (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
339         s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
340
341         /* Save the FP registers */
342         offset = CREATE_FP_OFFSET;
343         for (i = s390_f0; i <= s390_f15; ++i) {
344                 s390_std  (buf, i, 0, STK_BASE, offset);
345                 offset += 8;
346         }
347
348         /*----------------------------------------------------------
349           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
350           LMF. We'll need to restore it after the call to
351           's390_magic_trampoline' and before the call to the native
352           method.
353           ----------------------------------------------------------*/
354                                 
355         s390_basr (buf, s390_r13, 0);
356         s390_j    (buf, 6);
357         s390_llong(buf, mono_get_lmf_addr);
358         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
359         s390_basr (buf, s390_r14, s390_r1);
360
361         /*---------------------------------------------------------------*/
362         /* we build the MonoLMF structure on the stack - see mini-s390.h */
363         /* Keep in sync with the code in mono_arch_emit_prolog           */
364         /*---------------------------------------------------------------*/
365         lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
366                                                                                         
367         s390_lgr   (buf, s390_r13, STK_BASE);
368         s390_aghi  (buf, s390_r13, lmfOffset);  
369                                                                                         
370         /*---------------------------------------------------------------*/     
371         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
372         /*---------------------------------------------------------------*/     
373         s390_stg   (buf, s390_r2, 0, s390_r13,                          
374                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
375                                                                                         
376         /*---------------------------------------------------------------*/     
377         /* Get current lmf                                               */     
378         /*---------------------------------------------------------------*/     
379         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
380                                                                                         
381         /*---------------------------------------------------------------*/     
382         /* Set our lmf as the current lmf                                */     
383         /*---------------------------------------------------------------*/     
384         s390_stg   (buf, s390_r13, 0, s390_r2, 0);                              
385                                                                                         
386         /*---------------------------------------------------------------*/     
387         /* Have our lmf.previous_lmf point to the last lmf               */     
388         /*---------------------------------------------------------------*/     
389         s390_stg   (buf, s390_r0, 0, s390_r13,                          
390                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
391                                                                                         
392         /*---------------------------------------------------------------*/     
393         /* save method info                                              */     
394         /*---------------------------------------------------------------*/     
395         s390_lg    (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
396         s390_stg   (buf, s390_r1, 0, s390_r13,                          
397                             G_STRUCT_OFFSET(MonoLMF, method));                          
398                                                                         
399         /*---------------------------------------------------------------*/     
400         /* save the current SP                                           */     
401         /*---------------------------------------------------------------*/     
402         s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
403         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));  
404                                                                         
405         /*---------------------------------------------------------------*/     
406         /* save the current IP                                           */     
407         /*---------------------------------------------------------------*/     
408         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
409                 s390_lghi  (buf, s390_r1, 0);
410         } else {
411                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
412                 //                      s390_la    (buf, s390_r1, 0, s390_r1, 0);
413         }
414         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));  
415                                                                                         
416         /*---------------------------------------------------------------*/     
417         /* Save general and floating point registers                     */     
418         /*---------------------------------------------------------------*/     
419         s390_mvc   (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]), 
420                     STK_BASE, CREATE_GR_OFFSET);
421         s390_mvc   (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]), 
422                     s390_r11, S390_REG_SAVE_OFFSET);
423
424         /* Simply copy fpregs already saved above                        */
425         s390_mvc   (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
426                     STK_BASE, CREATE_FP_OFFSET);
427
428         /*---------------------------------------------------------------*/
429         /* STEP 2: call the C trampoline function                        */
430         /*---------------------------------------------------------------*/
431                                 
432         /* Set arguments */
433
434         /* Arg 1: mgreg_t *regs. We pass sp instead */
435         s390_la  (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
436                 
437         /* Arg 2: code (next address to the instruction that called us) */
438         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
439                 s390_lghi (buf, s390_r3, 0);
440         } else {
441                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
442         }
443
444         /* Arg 3: MonoMethod *method. It was put in r1 by the
445            method-specific trampoline code, and then saved before the call
446            to mono_get_lmf_addr()'. */
447         s390_lg  (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
448
449         /* Arg 4: trampoline address. Ignore for now */
450                 
451         /* Calculate call address and call the C trampoline. Return value will be in r2 */
452         s390_basr (buf, s390_r13, 0);
453         s390_j    (buf, 6);
454         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
455         s390_llong (buf, tramp);
456         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
457         s390_basr (buf, s390_r14, s390_r1);
458                 
459         /* OK, code address is now on r2. Move it to r1, so that we
460            can restore r2 and use it from r1 later */
461         s390_lgr  (buf, s390_r1, s390_r2);
462
463         /*----------------------------------------------------------
464           STEP 3: Restore the LMF
465           ----------------------------------------------------------*/
466         restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
467         
468         /*----------------------------------------------------------
469           STEP 4: call the compiled method
470           ----------------------------------------------------------*/
471                 
472         /* Restore registers */
473
474         s390_lmg  (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
475                 
476         /* Restore the FP registers */
477         offset = CREATE_FP_OFFSET;
478         for (i = s390_f0; i <= s390_f15; ++i) {
479                 s390_ld  (buf, i, 0, STK_BASE, offset);
480                 offset += 8;
481         }
482
483         /* Restore stack pointer and jump to the code -
484            R14 contains the return address to our caller */
485         s390_lgr  (buf, STK_BASE, s390_r11);
486         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
487
488         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
489                 s390_br (buf, s390_r14);
490         else
491                 s390_br (buf, s390_r1);
492
493         /* Flush instruction cache, since we've generated code */
494         mono_arch_flush_icache (code, buf - code);
495         
496         /* Sanity check */
497         g_assert ((buf - code) <= 512);
498
499         return code;
500 }
501
502 /*========================= End of Function ========================*/
503
504 /*------------------------------------------------------------------*/
505 /*                                                                  */
506 /* Name         - mono_arch_create_specific_trampoline              */
507 /*                                                                  */
508 /* Function     - Creates the given kind of specific trampoline     */
509 /*                                                                  */
510 /*------------------------------------------------------------------*/
511
512 gpointer
513 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
514 {
515         guint8 *code, *buf, *tramp;
516         gint32 displace;
517
518         tramp = mono_get_trampoline_code (tramp_type);
519
520         /*----------------------------------------------------------*/
521         /* This is the method-specific part of the trampoline. Its  */
522         /* purpose is to provide the generic part with the          */
523         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
524         /*----------------------------------------------------------*/
525         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
526
527         s390_basr (buf, s390_r1, 0);
528         s390_j    (buf, 6);
529         s390_llong(buf, arg1);
530         s390_lg   (buf, s390_r1, 0, s390_r1, 4);
531         displace = (tramp - buf) / 2;
532         s390_jcl  (buf, S390_CC_UN, displace);
533
534         /* Flush instruction cache, since we've generated code */
535         mono_arch_flush_icache (code, buf - code);
536
537         /* Sanity check */
538         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
539
540         if (code_len)
541                 *code_len = buf - code;
542         
543         return code;
544 }       
545
546 /*========================= End of Function ========================*/
547
548 /*------------------------------------------------------------------*/
549 /*                                                                  */
550 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
551 /*                                                                  */
552 /* Function     -                                                   */
553 /*                                                                  */
554 /*------------------------------------------------------------------*/
555
556 gpointer
557 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
558 {
559         /* FIXME: implement! */
560         g_assert_not_reached ();
561         return NULL;
562 }       
563
564 /*========================= End of Function ========================*/