Reverse previous changes
[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 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   - 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 = code = mono_domain_code_reserve (domain, 28);
98
99         s390_basr (code, s390_r1, 0);
100         s390_j    (code, 6);
101         s390_llong(code, addr);
102         s390_lg   (code, s390_r1, 0, s390_r1, 4);
103         s390_aghi (code, this_pos, sizeof(MonoObject));
104         s390_br   (code, s390_r1);
105
106         g_assert ((code - start) <= 28);
107
108         mono_arch_flush_icache (start, code - start);
109
110         return start;
111 }
112
113 /*========================= End of Function ========================*/
114
115 /*------------------------------------------------------------------*/
116 /*                                                                  */
117 /* Name         - mono_arch_patch_callsite                          */
118 /*                                                                  */
119 /* Function     - Patch a non-virtual callsite so it calls @addr.   */
120 /*                                                                  */
121 /*------------------------------------------------------------------*/
122
123 void
124 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
125 {
126         gint32 displace;
127         unsigned short opcode;
128
129         opcode = *((unsigned short *) (orig_code - 6));
130         if (opcode == 0xc0e5) {
131                 /* This is the 'brasl' instruction */
132                 orig_code    -= 4;
133                 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
134                 s390_patch_rel (orig_code, displace);
135                 mono_arch_flush_icache (orig_code, 4);
136         } else {
137                 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
138                 g_assert (orig_code [-8] == 0xe3);
139                 g_assert (orig_code [-7] == 0xe0);
140                 g_assert (orig_code [-6] == 0xd0);
141                 g_assert (orig_code [-5] == 0x04);
142                 g_assert (orig_code [-4] == 0x00);
143                 g_assert (orig_code [-3] == 0x04);
144                 opcode = *((unsigned short*) (orig_code - 2));
145                 g_assert (opcode == 0x0dee);
146
147                 /* The call address is stored in the 8 bytes preceeding the basr instruction */
148                 s390_patch_addr(orig_code - 16, (gssize)addr);
149                 mono_arch_flush_icache (orig_code - 16, 8);
150         }
151 }
152
153 /*========================= End of Function ========================*/
154
155 /*------------------------------------------------------------------*/
156 /*                                                                  */
157 /* Name         - mono_arch_patch_plt_entry.                        */
158 /*                                                                  */
159 /* Function     - Patch a PLT entry - unused as yet.                */
160 /*                                                                  */
161 /*------------------------------------------------------------------*/
162
163 void
164 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
165 {
166         g_assert_not_reached ();
167 }
168
169 /*========================= End of Function ========================*/
170
171 /*------------------------------------------------------------------*/
172 /*                                                                  */
173 /* Name         - mono_arch_nullify_class_init_trampoline           */
174 /*                                                                  */
175 /* Function     - Nullify a call which calls a class init trampoline*/
176 /*                                                                  */
177 /*------------------------------------------------------------------*/
178
179 void
180 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
181 {
182         char patch[2] = {0x07, 0x00};
183
184         code = code - 2;
185
186         memcpy(code, patch, sizeof(patch));
187 }
188
189 /*========================= End of Function ========================*/
190
191 /*------------------------------------------------------------------*/
192 /*                                                                  */
193 /* Name         - mono_arch_nullify_plt_entry                       */
194 /*                                                                  */
195 /* Function     - Nullify a PLT entry call.                         */
196 /*                                                                  */
197 /*------------------------------------------------------------------*/
198
199 void
200 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
201 {
202         g_assert_not_reached ();
203 }
204
205 /*========================= End of Function ========================*/
206
207 /*------------------------------------------------------------------*/
208 /*                                                                  */
209 /* Name         - mono_arch_create_trampoline_code                  */
210 /*                                                                  */
211 /* Function     - Create the designated type of trampoline according*/
212 /*                to the 'tramp_type' parameter.                    */
213 /*                                                                  */
214 /*------------------------------------------------------------------*/
215
216 guchar*
217 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
218 {
219         guint8 *buf, *tramp, *code;
220         int i, offset, lmfOffset;
221         GSList *unwind_ops = NULL;
222         MonoJumpInfo *ji = NULL;
223
224         g_assert (!aot);
225
226         /* Now we'll create in 'buf' the S/390 trampoline code. This
227            is the trampoline code common to all methods  */
228                 
229         code = buf = mono_global_codeman_reserve(512);
230                 
231         /*-----------------------------------------------------------
232           STEP 0: First create a non-standard function prologue with a
233           stack size big enough to save our registers.
234           -----------------------------------------------------------*/
235                 
236         s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
237         s390_lgr  (buf, s390_r11, s390_r15);
238         s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
239         s390_stg  (buf, s390_r11, 0, STK_BASE, 0);
240         s390_stg  (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
241         s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
242
243         /* Save the FP registers */
244         offset = CREATE_FP_OFFSET;
245         for (i = s390_f0; i <= s390_f15; ++i) {
246                 s390_std  (buf, i, 0, STK_BASE, offset);
247                 offset += 8;
248         }
249
250         /*----------------------------------------------------------
251           STEP 1: call 'mono_get_lmf_addr()' to get the address of our
252           LMF. We'll need to restore it after the call to
253           's390_magic_trampoline' and before the call to the native
254           method.
255           ----------------------------------------------------------*/
256                                 
257         s390_basr (buf, s390_r13, 0);
258         s390_j    (buf, 6);
259         s390_llong(buf, mono_get_lmf_addr);
260         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
261         s390_basr (buf, s390_r14, s390_r1);
262
263         /*---------------------------------------------------------------*/
264         /* we build the MonoLMF structure on the stack - see mini-s390.h */
265         /* Keep in sync with the code in mono_arch_emit_prolog           */
266         /*---------------------------------------------------------------*/
267         lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
268                                                                                         
269         s390_lgr   (buf, s390_r13, STK_BASE);
270         s390_aghi  (buf, s390_r13, lmfOffset);  
271                                                                                         
272         /*---------------------------------------------------------------*/     
273         /* Set lmf.lmf_addr = jit_tls->lmf                               */     
274         /*---------------------------------------------------------------*/     
275         s390_stg   (buf, s390_r2, 0, s390_r13,                          
276                             G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
277                                                                                         
278         /*---------------------------------------------------------------*/     
279         /* Get current lmf                                               */     
280         /*---------------------------------------------------------------*/     
281         s390_lg    (buf, s390_r0, 0, s390_r2, 0);                               
282                                                                                         
283         /*---------------------------------------------------------------*/     
284         /* Set our lmf as the current lmf                                */     
285         /*---------------------------------------------------------------*/     
286         s390_stg   (buf, s390_r13, 0, s390_r2, 0);                              
287                                                                                         
288         /*---------------------------------------------------------------*/     
289         /* Have our lmf.previous_lmf point to the last lmf               */     
290         /*---------------------------------------------------------------*/     
291         s390_stg   (buf, s390_r0, 0, s390_r13,                          
292                             G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
293                                                                                         
294         /*---------------------------------------------------------------*/     
295         /* save method info                                              */     
296         /*---------------------------------------------------------------*/     
297         s390_lg    (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
298         s390_stg   (buf, s390_r1, 0, s390_r13,                          
299                             G_STRUCT_OFFSET(MonoLMF, method));                          
300                                                                         
301         /*---------------------------------------------------------------*/     
302         /* save the current SP                                           */     
303         /*---------------------------------------------------------------*/     
304         s390_lg    (buf, s390_r1, 0, STK_BASE, 0);
305         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));  
306                                                                         
307         /*---------------------------------------------------------------*/     
308         /* save the current IP                                           */     
309         /*---------------------------------------------------------------*/     
310         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
311                 s390_lghi  (buf, s390_r1, 0);
312         } else {
313                 s390_lg    (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
314                 //                      s390_la    (buf, s390_r1, 0, s390_r1, 0);
315         }
316         s390_stg   (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));  
317                                                                                         
318         /*---------------------------------------------------------------*/     
319         /* Save general and floating point registers                     */     
320         /*---------------------------------------------------------------*/     
321         s390_mvc   (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]), 
322                     STK_BASE, CREATE_GR_OFFSET);
323         s390_mvc   (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]), 
324                     s390_r11, S390_REG_SAVE_OFFSET);
325
326         /* Simply copy fpregs already saved above                        */
327         s390_mvc   (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
328                     STK_BASE, CREATE_FP_OFFSET);
329
330         /*---------------------------------------------------------------*/
331         /* STEP 2: call the C trampoline function                        */
332         /*---------------------------------------------------------------*/
333                                 
334         /* Set arguments */
335
336         /* Arg 1: mgreg_t *regs. We pass sp instead */
337         s390_la  (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
338                 
339         /* Arg 2: code (next address to the instruction that called us) */
340         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
341                 s390_lghi (buf, s390_r3, 0);
342         } else {
343                 s390_lg   (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
344         }
345
346         /* Arg 3: MonoMethod *method. It was put in r1 by the
347            method-specific trampoline code, and then saved before the call
348            to mono_get_lmf_addr()'. */
349         s390_lg  (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
350
351         /* Arg 4: trampoline address. Ignore for now */
352                 
353         /* Calculate call address and call the C trampoline. Return value will be in r2 */
354         s390_basr (buf, s390_r13, 0);
355         s390_j    (buf, 6);
356         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
357         s390_llong (buf, tramp);
358         s390_lg   (buf, s390_r1, 0, s390_r13, 4);
359         s390_basr (buf, s390_r14, s390_r1);
360                 
361         /* OK, code address is now on r2. Move it to r1, so that we
362            can restore r2 and use it from r1 later */
363         s390_lgr  (buf, s390_r1, s390_r2);
364
365         /*----------------------------------------------------------
366           STEP 3: Restore the LMF
367           ----------------------------------------------------------*/
368         restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
369         
370         /*----------------------------------------------------------
371           STEP 4: call the compiled method
372           ----------------------------------------------------------*/
373                 
374         /* Restore registers */
375
376         s390_lmg  (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
377                 
378         /* Restore the FP registers */
379         offset = CREATE_FP_OFFSET;
380         for (i = s390_f0; i <= s390_f15; ++i) {
381                 s390_ld  (buf, i, 0, STK_BASE, offset);
382                 offset += 8;
383         }
384
385         /* Restore stack pointer and jump to the code -
386            R14 contains the return address to our caller */
387         s390_lgr  (buf, STK_BASE, s390_r11);
388         s390_lmg  (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
389
390         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
391                 s390_br (buf, s390_r14);
392         else
393                 s390_br (buf, s390_r1);
394
395         /* Flush instruction cache, since we've generated code */
396         mono_arch_flush_icache (code, buf - code);
397         
398         if (info)
399                 *info = mono_tramp_info_create (mono_get_generic_trampoline_name(tramp_type), 
400                                                 buf, buf - code, ji, unwind_ops);
401
402         /* Sanity check */
403         g_assert ((buf - code) <= 512);
404
405         return code;
406 }
407
408 /*========================= End of Function ========================*/
409
410 /*------------------------------------------------------------------*/
411 /*                                                                  */
412 /* Name         - mono_arch_create_specific_trampoline              */
413 /*                                                                  */
414 /* Function     - Creates the given kind of specific trampoline     */
415 /*                                                                  */
416 /*------------------------------------------------------------------*/
417
418 gpointer
419 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
420 {
421         guint8 *code, *buf, *tramp;
422         gint32 displace;
423
424         tramp = mono_get_trampoline_code (tramp_type);
425
426         /*----------------------------------------------------------*/
427         /* This is the method-specific part of the trampoline. Its  */
428         /* purpose is to provide the generic part with the          */
429         /* MonoMethod *method pointer. We'll use r1 to keep it.     */
430         /*----------------------------------------------------------*/
431         code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
432
433         s390_basr (buf, s390_r1, 0);
434         s390_j    (buf, 6);
435         s390_llong(buf, arg1);
436         s390_lg   (buf, s390_r1, 0, s390_r1, 4);
437         displace = (tramp - buf) / 2;
438         s390_jcl  (buf, S390_CC_UN, displace);
439
440         /* Flush instruction cache, since we've generated code */
441         mono_arch_flush_icache (code, buf - code);
442
443         /* Sanity check */
444         g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
445
446         if (code_len)
447                 *code_len = buf - code;
448         
449         return code;
450 }       
451
452 /*========================= End of Function ========================*/
453
454 /*------------------------------------------------------------------*/
455 /*                                                                  */
456 /* Name         - mono_arch_create_rgctx_lazy_fetch_trampoline      */
457 /*                                                                  */
458 /* Function     -                                                   */
459 /*                                                                  */
460 /*------------------------------------------------------------------*/
461
462 gpointer
463 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
464 {
465         /* FIXME: implement! */
466         g_assert_not_reached ();
467         return NULL;
468 }       
469
470 /*========================= End of Function ========================*/