Merge branch 'cecil-light'
[mono.git] / mono / mini / tramp-s390.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+sizeof(gpointer)
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 /* Specific trampoline code fragment sizes                                  */
33 /*------------------------------------------------------------------*/
34 #define SPECIFIC_TRAMPOLINE_SIZE        64
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/s390/s390-codegen.h>
50
51 #include "mini.h"
52 #include "mini-s390.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   - method - Methd pointer                            */
86 /*                addr   - Pointer to native code for method        */
87 /*                                                                  */
88 /*------------------------------------------------------------------*/
89
90 gpointer
91 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
92 {
93         guint8 *code, *start;
94         int this_pos = s390_r2;
95         MonoDomain *domain = mono_domain_get ();
96
97         start = addr;
98         if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
99                 this_pos = s390_r3;
100
101         start = code = mono_domain_code_reserve (domain, 28);
102     
103         s390_basr (code, s390_r13, 0);
104         s390_j    (code, 4);
105         s390_word (code, addr);
106         s390_l    (code, s390_r1, 0, s390_r13, 4);
107         s390_ahi  (code, this_pos, sizeof(MonoObject));
108         s390_br   (code, s390_r1);
109
110         g_assert ((code - start) <= 28);
111
112         mono_arch_flush_icache (start, code - start);
113
114         return start;
115 }
116
117 /*========================= End of Function ========================*/
118
119 /*------------------------------------------------------------------*/
120 /*                                                                  */
121 /* Name         - mono_arch_patch_callsite                              */
122 /*                                                                  */
123 /* Function     - Patch a non-virtual callsite so it calls @addr.       */
124 /*                                                                  */
125 /*------------------------------------------------------------------*/
126
127 void
128 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
129 {
130         gint32 displace;
131         unsigned short opcode;
132
133         opcode = *((unsigned short *) (orig_code - 6));
134         /* This should be a 'brasl' instruction */
135         g_assert (opcode ==  0xc0e5);
136         orig_code    -= 4;
137         displace = ((gint32) addr - (gint32) (orig_code - 2)) / 2;
138         s390_patch (orig_code, displace);
139         mono_arch_flush_icache (orig_code, 4);
140 }
141
142 /*========================= End of Function ========================*/
143
144 void
145 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
146 {
147         g_assert_not_reached ();
148 }
149
150 /*========================= End of Function ========================*/
151
152 /*------------------------------------------------------------------*/
153 /*                                                                  */
154 /* Name         - mono_arch_nullify_class_init_trampoline               */
155 /*                                                                  */
156 /* Function     - Nullify a call which calls a class init trampoline    */
157 /*                                                                  */
158 /*------------------------------------------------------------------*/
159
160 void
161 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
162 {
163         char patch[6] = {0x47, 0x00, 0x00, 0x00, 0x07, 0x00};
164
165         code = code - 6;
166
167         memcpy(code, patch, sizeof(patch));
168 }
169
170 /*========================= End of Function ========================*/
171
172 void
173 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
174 {
175         g_assert_not_reached ();
176 }
177
178 /*========================= End of Function ========================*/
179
180 /*------------------------------------------------------------------*/
181 /*                                                                  */
182 /* Name         - mono_arch_get_vcall_slot                              */
183 /*                                                                  */
184 /* Function     - This method is called by the arch independent         */
185 /*            trampoline code to determine the vtable slot used by  */
186 /*            the call which invoked the trampoline.                */
187 /*                The calls                                         */
188 /*                generated by mono for S/390 will look like either:*/
189 /*                1. l     %r1,xxx(%rx)                             */
190 /*                   bras  %r14,%r1                                 */
191 /*                2. brasl %r14,xxxxxx                              */
192 /*                                                                  */
193 /* Parameters   - code   - Pointer into caller code                 */
194 /*                regs   - Register state at the point of the call  */
195 /*                displacement - Out parameter which will receive   */
196 /*                the displacement of the vtable slot               */
197 /*                                                                  */
198 /*------------------------------------------------------------------*/
199
200 gpointer
201 mono_arch_get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
202 {
203         int reg;
204         guchar* base;
205         unsigned short opcode;
206         char *sp;
207
208         // We are passed sp instead of the register array
209         sp = (char*)regs;
210
211         *displacement = 0;
212
213         opcode = *((unsigned short *) (code - 6));
214         switch (opcode) {
215         case 0x5810 :
216                 /* This is a bras r14,r1 instruction */
217                 code    -= 4;
218                 reg      = *code >> 4;
219                 *displacement = *((short *)code) & 0x0fff;
220                 if (reg > 5) 
221                         base = *((guchar **) (sp + S390_REG_SAVE_OFFSET+
222                                                                   sizeof(int)*(reg-6)));
223                 else
224                         base = *((guchar **) ((sp - CREATE_STACK_SIZE) + 
225                                                                   CREATE_GR_OFFSET +
226                                                                   sizeof(int)*(reg-2)));
227                 return base;
228         case 0x581d :
229                 /* l %r1,OFFSET(%r13,%r7) */
230                 code    -= 4;
231                 reg      = *code >> 4;
232                 *displacement = *((short *)code) & 0x0fff;
233                 if (reg > 5) 
234                         base = *((guchar **) (sp + S390_REG_SAVE_OFFSET+
235                                                                   sizeof(int)*(reg-6)));
236                 else
237                         base = *((guchar **) ((sp - CREATE_STACK_SIZE) + 
238                                                                   CREATE_GR_OFFSET +
239                                                                   sizeof(int)*(reg-2)));
240                 base += *((guint32*) (sp + S390_REG_SAVE_OFFSET+
241                                                           sizeof(int)*(s390_r13-6)));
242                 return base;
243         case 0xc0e5 :
244                 /* This is the 'brasl' instruction */
245                 return NULL;
246         default :
247                 g_error("Unable to patch instruction prior to %p",code);
248         }
249
250         return NULL;
251 }
252
253 /*========================= End of Function ========================*/
254
255 /*------------------------------------------------------------------*/
256 /*                                                                  */
257 /* Name         - mono_arch_create_trampoline_code                      */
258 /*                                                                  */
259 /* Function     - Create the designated type of trampoline according*/
260 /*                to the 'tramp_type' parameter.                    */
261 /*                                                                  */
262 /*------------------------------------------------------------------*/
263
264 guchar*
265 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
266 {
267         guint8 *buf, *tramp, *code;
268         int i, offset, lmfOffset;
269
270         g_assert (!aot);
271         if (info)
272                 *info = NULL;
273
274         /* Now we'll create in 'buf' the S/390 trampoline code. This
275            is the trampoline code common to all methods  */
276                 
277         code = buf = mono_global_codeman_reserve (512);
278                 
279         /*-----------------------------------------------------------
280           STEP 0: First create a non-standard function prologue with a
281           stack size big enough to save our registers.
282           -----------------------------------------------------------*/
283                 
284         s390_stm  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
285         s390_lr   (buf, s390_r11, s390_r15);
286         s390_ahi  (buf, STK_BASE, -CREATE_STACK_SIZE);
287         s390_st   (buf, s390_r11, 0, STK_BASE, 0);
288         s390_st   (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
289         s390_stm  (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
290
291         /* Save the FP registers */
292         offset = CREATE_FP_OFFSET;
293         for (i = s390_f0; i <= s390_f15; ++i) {
294                 s390_std  (buf, i, 0, STK_BASE, offset);
295                 offset += 8;
296         }
297
298         /*----------------------------------------------------------
299           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
300           LMF. We'll need to restore it after the call to
301           's390_magic_trampoline' and before the call to the native
302           method.
303           ----------------------------------------------------------*/
304                                 
305         s390_basr (buf, s390_r13, 0);
306         s390_j    (buf, 4);
307         s390_word (buf, mono_get_lmf_addr);
308         s390_l    (buf, s390_r1, 0, s390_r13, 4);
309         s390_basr (buf, s390_r14, s390_r1);
310
311         /*---------------------------------------------------------------*/
312         /* we build the MonoLMF structure on the stack - see mini-s390.h */
313         /* Keep in sync with the code in mono_arch_emit_prolog           */
314         /*---------------------------------------------------------------*/
315         lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
316                                                                                         
317         s390_lr    (buf, s390_r13, STK_BASE);
318         s390_ahi   (buf, s390_r13, lmfOffset);  
319                                                                                         
320         /*---------------------------------------------------------------*/     
321         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
322         /*---------------------------------------------------------------*/     
323         s390_st    (buf, s390_r2, 0, s390_r13,                          
324                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
325                                                                                         
326         /*---------------------------------------------------------------*/     
327         /* Get current lmf                                               */     
328         /*---------------------------------------------------------------*/     
329         s390_l     (buf, s390_r0, 0, s390_r2, 0);                               
330                                                                                         
331         /*---------------------------------------------------------------*/     
332         /* Set our lmf as the current lmf                                */     
333         /*---------------------------------------------------------------*/     
334         s390_st    (buf, s390_r13, 0, s390_r2, 0);                              
335                                                                                         
336         /*---------------------------------------------------------------*/     
337         /* Have our lmf.previous_lmf point to the last lmf               */     
338         /*---------------------------------------------------------------*/     
339         s390_st    (buf, s390_r0, 0, s390_r13,                          
340                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
341                                                                                         
342         /*---------------------------------------------------------------*/     
343         /* save method info                                              */     
344         /*---------------------------------------------------------------*/     
345         s390_l     (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
346         s390_st    (buf, s390_r1, 0, s390_r13,                          
347                             G_STRUCT_OFFSET(MonoLMF, method));                          
348                                                                         
349         /*---------------------------------------------------------------*/     
350         /* save the current SP                                           */     
351         /*---------------------------------------------------------------*/     
352         s390_l     (buf, s390_r1, 0, STK_BASE, 0);
353         s390_st    (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));  
354                                                                         
355         /*---------------------------------------------------------------*/     
356         /* save the current IP                                           */     
357         /*---------------------------------------------------------------*/     
358         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
359                 s390_lhi   (buf, s390_r1, 0);
360         } else {
361                 s390_l     (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
362                 s390_la    (buf, s390_r1, 0, s390_r1, 0);
363         }
364         s390_st    (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));  
365                                                                                         
366         /*---------------------------------------------------------------*/     
367         /* Save general and floating point registers                     */     
368         /*---------------------------------------------------------------*/     
369         s390_stm   (buf, s390_r2, s390_r12, s390_r13,                           
370                             G_STRUCT_OFFSET(MonoLMF, gregs[2]));                        
371         for (i = 0; i < 16; i++) {                                              
372                 s390_std  (buf, i, 0, s390_r13,                                 
373                                    G_STRUCT_OFFSET(MonoLMF, fregs[i]));                 
374         }
375
376         /*---------------------------------------------------------------*/
377         /* STEP 2: call the C trampoline function                        */
378         /*---------------------------------------------------------------*/
379                                 
380         /* Set arguments */
381
382         /* Arg 1: mgreg_t *regs. We pass sp instead */
383         s390_lr   (buf, s390_r2, STK_BASE);
384         s390_ahi  (buf, s390_r2, CREATE_STACK_SIZE);
385                 
386         /* Arg 2: code (next address to the instruction that called us) */
387         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
388                 s390_lhi (buf, s390_r3, 0);
389         } else {
390                 s390_l  (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
391
392                 /* Mask out bit 31 */
393                 s390_basr (buf, s390_r13, 0);
394                 s390_j    (buf, 4);
395                 s390_word (buf, (~(1 << 31)));
396                 s390_n (buf, s390_r3, 0, s390_r13, 4);
397         }
398                 
399         /* Arg 3: MonoMethod *method. It was put in r1 by the
400            method-specific trampoline code, and then saved before the call
401            to mono_get_lmf_addr()'. */
402         s390_l  (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
403
404         /* Arg 4: trampoline address. Ignore for now */
405                 
406         /* Calculate call address and call the C trampoline. Return value will be in r2 */
407         s390_basr (buf, s390_r13, 0);
408         s390_j    (buf, 4);
409         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
410         s390_word (buf, tramp);
411         s390_l    (buf, s390_r1, 0, s390_r13, 4);
412         s390_basr (buf, s390_r14, s390_r1);
413                 
414         /* OK, code address is now on r2. Move it to r1, so that we
415            can restore r2 and use it from r1 later */
416         s390_lr   (buf, s390_r1, s390_r2);
417
418         /*----------------------------------------------------------
419           STEP 3: Restore the LMF
420           ----------------------------------------------------------*/
421         restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
422         
423         /*----------------------------------------------------------
424           STEP 4: call the compiled method
425           ----------------------------------------------------------*/
426                 
427         /* Restore registers */
428
429         s390_lm   (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
430                 
431         /* Restore the FP registers */
432         offset = CREATE_FP_OFFSET;
433         for (i = s390_f0; i <= s390_f15; ++i) {
434                 s390_ld  (buf, i, 0, STK_BASE, offset);
435                 offset += 8;
436         }
437
438         /* Restore stack pointer and jump to the code - 
439            R14 contains the return address to our caller */
440         s390_lr   (buf, STK_BASE, s390_r11);
441         s390_lm   (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
442
443         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
444                 s390_br (buf, s390_r14);
445         else
446                 s390_br   (buf, s390_r1);
447
448         /* Flush instruction cache, since we've generated code */
449         mono_arch_flush_icache (code, buf - code);
450         
451         /* Sanity check */
452         g_assert ((buf - code) <= 512);
453
454         return code;
455 }
456
457 /*========================= End of Function ========================*/
458
459 /*------------------------------------------------------------------*/
460 /*                                                                  */
461 /* Name         - mono_arch_create_specific_trampoline                  */
462 /*                                                                  */
463 /* Function     - Creates the given kind of specific trampoline         */
464 /*                                                                  */
465 /*------------------------------------------------------------------*/
466
467 gpointer
468 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
469 {
470         guint8 *code, *buf, *tramp;
471         gint32 displace;
472
473         tramp = mono_get_trampoline_code (tramp_type);
474
475         /*----------------------------------------------------------*/
476         /* This is the method-specific part of the trampoline. Its  */
477         /* purpose is to provide the generic part with the          */
478         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
479         /*----------------------------------------------------------*/
480         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
481
482         s390_basr (buf, s390_r1, 0);
483         s390_j    (buf, 4);
484         s390_word (buf, arg1);
485         s390_l    (buf, s390_r1, 0, s390_r1, 4);
486         displace = (tramp - buf) / 2;
487         s390_jcl  (buf, S390_CC_UN, displace);
488
489         /* Flush instruction cache, since we've generated code */
490         mono_arch_flush_icache (code, buf - code);
491
492         /* Sanity check */
493         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
494
495         if (code_len)
496                 *code_len = buf - code;
497         
498         return code;
499 }       
500
501 /*========================= End of Function ========================*/
502
503 gpointer
504 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
505 {
506         /* FIXME: implement! */
507         g_assert_not_reached ();
508         return NULL;
509 }