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