2009-01-07 Zoltan Varga <vargaz@gmail.com>
[mono-project.git] / mono / mini / tramp-s390x.c
blobb382c84a8e0ca6b35b95a58927f2fe23ac5fab54
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 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
20 /* D e f i n e s */
21 /*------------------------------------------------------------------*/
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))
31 /*------------------------------------------------------------------*/
32 /* Method-specific trampoline code fragment sizes */
33 /*------------------------------------------------------------------*/
34 #define SPECIFIC_TRAMPOLINE_SIZE 96
36 /*========================= End of Defines =========================*/
38 /*------------------------------------------------------------------*/
39 /* I n c l u d e s */
40 /*------------------------------------------------------------------*/
42 #include <config.h>
43 #include <glib.h>
44 #include <string.h>
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>
51 #include "mini.h"
52 #include "mini-s390x.h"
54 /*========================= End of Includes ========================*/
56 /*------------------------------------------------------------------*/
57 /* T y p e d e f s */
58 /*------------------------------------------------------------------*/
60 /*========================= End of Typedefs ========================*/
62 /*------------------------------------------------------------------*/
63 /* P r o t o t y p e s */
64 /*------------------------------------------------------------------*/
66 /*========================= End of Prototypes ======================*/
68 /*------------------------------------------------------------------*/
69 /* G l o b a l V a r i a b l e s */
70 /*------------------------------------------------------------------*/
73 /*====================== End of Global Variables ===================*/
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 /*------------------------------------------------------------------*/
91 gpointer
92 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *method, gpointer addr)
94 guint8 *code, *start;
95 int this_pos = s390_r2;
96 MonoDomain *domain = mono_domain_get ();
98 start = addr;
99 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
100 this_pos = s390_r3;
102 mono_domain_lock (domain);
103 start = code = mono_code_manager_reserve (domain->code_mp, 28);
104 mono_domain_unlock (domain);
106 s390_basr (code, s390_r1, 0);
107 s390_j (code, 6);
108 s390_llong(code, addr);
109 s390_lg (code, s390_r1, 0, s390_r1, 4);
110 s390_aghi (code, this_pos, sizeof(MonoObject));
111 s390_br (code, s390_r1);
113 g_assert ((code - start) <= 28);
115 mono_arch_flush_icache (start, code - start);
117 return start;
120 /*========================= End of Function ========================*/
122 /*------------------------------------------------------------------*/
123 /* */
124 /* Name - mono_arch_patch_callsite */
125 /* */
126 /* Function - Patch a non-virtual callsite so it calls @addr. */
127 /* */
128 /*------------------------------------------------------------------*/
130 void
131 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
133 gint32 displace;
134 unsigned short opcode;
136 opcode = *((unsigned short *) (orig_code - 6));
137 if (opcode == 0xc0e5) {
138 /* This is the 'brasl' instruction */
139 orig_code -= 4;
140 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
141 s390_patch_rel (orig_code, displace);
142 mono_arch_flush_icache (orig_code, 4);
143 } else {
144 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
145 g_assert (orig_code [-8] == 0xe3);
146 g_assert (orig_code [-7] == 0xe0);
147 g_assert (orig_code [-6] == 0xd0);
148 g_assert (orig_code [-5] == 0x04);
149 g_assert (orig_code [-4] == 0x00);
150 g_assert (orig_code [-3] == 0x04);
151 opcode = *((unsigned short*) (orig_code - 2));
152 g_assert (opcode == 0x0dee);
154 /* The call address is stored in the 8 bytes preceeding the basr instruction */
155 s390_patch_addr(orig_code - 16, (gssize)addr);
156 mono_arch_flush_icache (orig_code - 16, 8);
160 /*========================= End of Function ========================*/
162 void
163 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
165 g_assert_not_reached ();
168 /*========================= End of Function ========================*/
170 /*------------------------------------------------------------------*/
171 /* */
172 /* Name - mono_arch_nullify_class_init_trampoline */
173 /* */
174 /* Function - Nullify a call which calls a class init trampoline */
175 /* */
176 /*------------------------------------------------------------------*/
178 void
179 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
181 char patch[2] = {0x07, 0x00};
183 code = code - 2;
185 memcpy(code, patch, sizeof(patch));
188 /*========================= End of Function ========================*/
190 void
191 mono_arch_nullify_plt_entry (guint8 *code)
193 g_assert_not_reached ();
196 /*========================= End of Function ========================*/
198 /*------------------------------------------------------------------*/
199 /* */
200 /* Name - mono_arch_get_vcall_slot */
201 /* */
202 /* Function - This method is called by the arch independent */
203 /* trampoline code to determine the vtable slot used by */
204 /* the call which invoked the trampoline. */
205 /* */
206 /* Parameters - code - Pointer into caller code */
207 /* regs - Register state at the point of the call */
208 /* displacement - Out parameter which will receive */
209 /* the displacement of the vtable slot */
210 /* */
211 /*------------------------------------------------------------------*/
213 gpointer
214 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
216 int reg, lkReg;
217 guchar* base;
218 unsigned short opcode;
219 char *sp;
221 // We are passed sp instead of the register array
222 sp = (char*)regs;
224 *displacement = 0;
226 opcode = *((unsigned short *) (code - 6));
227 if (opcode == 0xc0e5)
228 /* This is the 'brasl' instruction */
229 return NULL;
231 /*-----------------------------------*/
232 /* This is a bras r14,Rz instruction */
233 /* If it's preceded by a LG Rx,d(Ry) */
234 /* If Rz == 1 then this is virtual */
235 /* call. */
236 /*-----------------------------------*/
237 code -= 6;
239 /*-----------------------------------*/
240 /* If call is preceded by LGR then */
241 /* there's nothing to patch */
242 /*-----------------------------------*/
243 if ((code[0] == 0xb9) &&
244 (code[1] == 0x04))
245 return NULL;
247 /*-----------------------------------*/
248 /* We back up until we're pointing at*/
249 /* the base/displacement portion of */
250 /* the LG instruction */
251 /*-----------------------------------*/
252 lkReg = code[5] & 0x0f;
254 /*-----------------------------------*/
255 /* The LG instruction has format: */
256 /* E3x0ylllhh04 - where: */
257 /* x = Rx; y = Ry; */
258 /* lll = low 12 bits of displacement */
259 /* hh = high 8 bits of displacement */
260 /*-----------------------------------*/
261 reg = code[0] >> 4;
262 *displacement = (code[2] << 12) +
263 ((code[0] & 0x0f) << 8) +
264 code[1];
266 if (reg > 5)
267 base = *((guchar **) (sp + S390_REG_SAVE_OFFSET +
268 sizeof(long)*(reg-6)));
269 else
270 base = *((guchar **) ((sp - CREATE_STACK_SIZE) +
271 CREATE_GR_OFFSET +
272 sizeof(long)*(reg-2)));
273 if (lkReg != 1)
274 /* Non virtual call */
275 return NULL;
277 return base;
280 /*========================= End of Function ========================*/
282 gpointer*
283 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
285 gpointer vt;
286 int displacement;
287 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
288 if (!vt)
289 return NULL;
290 return (gpointer*)((char*)vt + displacement);
293 /*========================= End of Function ========================*/
295 /*------------------------------------------------------------------*/
296 /* */
297 /* Name - mono_arch_create_trampoline_code */
298 /* */
299 /* Function - Create the designated type of trampoline according*/
300 /* to the 'tramp_type' parameter. */
301 /* */
302 /*------------------------------------------------------------------*/
304 guchar *
305 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
308 guint8 *buf, *tramp, *code;
309 int i, offset, lmfOffset;
311 /* Now we'll create in 'buf' the S/390 trampoline code. This
312 is the trampoline code common to all methods */
314 code = buf = mono_global_codeman_reserve(512);
316 /*-----------------------------------------------------------
317 STEP 0: First create a non-standard function prologue with a
318 stack size big enough to save our registers.
319 -----------------------------------------------------------*/
321 s390_stmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
322 s390_lgr (buf, s390_r11, s390_r15);
323 s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
324 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
325 s390_stg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
326 s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
328 /* Save the FP registers */
329 offset = CREATE_FP_OFFSET;
330 for (i = s390_f0; i <= s390_f15; ++i) {
331 s390_std (buf, i, 0, STK_BASE, offset);
332 offset += 8;
335 /*----------------------------------------------------------
336 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
337 LMF. We'll need to restore it after the call to
338 's390_magic_trampoline' and before the call to the native
339 method.
340 ----------------------------------------------------------*/
342 s390_basr (buf, s390_r13, 0);
343 s390_j (buf, 6);
344 s390_llong(buf, mono_get_lmf_addr);
345 s390_lg (buf, s390_r1, 0, s390_r13, 4);
346 s390_basr (buf, s390_r14, s390_r1);
348 /*---------------------------------------------------------------*/
349 /* we build the MonoLMF structure on the stack - see mini-s390.h */
350 /* Keep in sync with the code in mono_arch_emit_prolog */
351 /*---------------------------------------------------------------*/
352 lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
354 s390_lgr (buf, s390_r13, STK_BASE);
355 s390_aghi (buf, s390_r13, lmfOffset);
357 /*---------------------------------------------------------------*/
358 /* Set lmf.lmf_addr = jit_tls->lmf */
359 /*---------------------------------------------------------------*/
360 s390_stg (buf, s390_r2, 0, s390_r13,
361 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
363 /*---------------------------------------------------------------*/
364 /* Get current lmf */
365 /*---------------------------------------------------------------*/
366 s390_lg (buf, s390_r0, 0, s390_r2, 0);
368 /*---------------------------------------------------------------*/
369 /* Set our lmf as the current lmf */
370 /*---------------------------------------------------------------*/
371 s390_stg (buf, s390_r13, 0, s390_r2, 0);
373 /*---------------------------------------------------------------*/
374 /* Have our lmf.previous_lmf point to the last lmf */
375 /*---------------------------------------------------------------*/
376 s390_stg (buf, s390_r0, 0, s390_r13,
377 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
379 /*---------------------------------------------------------------*/
380 /* save method info */
381 /*---------------------------------------------------------------*/
382 s390_lg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
383 s390_stg (buf, s390_r1, 0, s390_r13,
384 G_STRUCT_OFFSET(MonoLMF, method));
386 /*---------------------------------------------------------------*/
387 /* save the current SP */
388 /*---------------------------------------------------------------*/
389 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
390 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
392 /*---------------------------------------------------------------*/
393 /* save the current IP */
394 /*---------------------------------------------------------------*/
395 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
396 s390_lghi (buf, s390_r1, 0);
397 } else {
398 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
399 // s390_la (buf, s390_r1, 0, s390_r1, 0);
401 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
403 /*---------------------------------------------------------------*/
404 /* Save general and floating point registers */
405 /*---------------------------------------------------------------*/
406 s390_stmg (buf, s390_r2, s390_r12, s390_r13,
407 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
408 for (i = 0; i < 16; i++) {
409 s390_std (buf, i, 0, s390_r13,
410 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
413 /*---------------------------------------------------------------*/
414 /* STEP 2: call the C trampoline function */
415 /*---------------------------------------------------------------*/
417 /* Set arguments */
419 /* Arg 1: gssize *regs. We pass sp instead */
420 s390_lgr (buf, s390_r2, STK_BASE);
421 s390_ahi (buf, s390_r2, CREATE_STACK_SIZE);
423 /* Arg 2: code (next address to the instruction that called us) */
424 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
425 s390_lghi (buf, s390_r3, 0);
426 } else {
427 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
430 /* Arg 3: MonoMethod *method. It was put in r1 by the
431 method-specific trampoline code, and then saved before the call
432 to mono_get_lmf_addr()'. */
433 s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
435 /* Arg 4: trampoline address. Ignore for now */
437 /* Calculate call address and call the C trampoline. Return value will be in r2 */
438 s390_basr (buf, s390_r13, 0);
439 s390_j (buf, 6);
440 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
441 s390_llong (buf, tramp);
442 s390_lg (buf, s390_r1, 0, s390_r13, 4);
443 s390_basr (buf, s390_r14, s390_r1);
445 /* OK, code address is now on r2. Move it to r1, so that we
446 can restore r2 and use it from r1 later */
447 s390_lgr (buf, s390_r1, s390_r2);
449 /*----------------------------------------------------------
450 STEP 3: Restore the LMF
451 ----------------------------------------------------------*/
452 restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
454 /*----------------------------------------------------------
455 STEP 4: call the compiled method
456 ----------------------------------------------------------*/
458 /* Restore registers */
460 s390_lmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
462 /* Restore the FP registers */
463 offset = CREATE_FP_OFFSET;
464 for (i = s390_f0; i <= s390_f15; ++i) {
465 s390_ld (buf, i, 0, STK_BASE, offset);
466 offset += 8;
469 /* Restore stack pointer and jump to the code -
470 R14 contains the return address to our caller */
471 s390_lgr (buf, STK_BASE, s390_r11);
472 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
474 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
475 s390_br (buf, s390_r14);
476 else
477 s390_br (buf, s390_r1);
479 /* Flush instruction cache, since we've generated code */
480 mono_arch_flush_icache (code, buf - code);
482 /* Sanity check */
483 g_assert ((buf - code) <= 512);
485 return code;
488 /*========================= End of Function ========================*/
490 /*------------------------------------------------------------------*/
491 /* */
492 /* Name - mono_arch_create_specific_trampoline */
493 /* */
494 /* Function - Creates the given kind of specific trampoline */
495 /* */
496 /*------------------------------------------------------------------*/
498 gpointer
499 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
501 guint8 *code, *buf, *tramp;
502 gint32 displace;
504 tramp = mono_get_trampoline_code (tramp_type);
506 /*----------------------------------------------------------*/
507 /* This is the method-specific part of the trampoline. Its */
508 /* purpose is to provide the generic part with the */
509 /* MonoMethod *method pointer. We'll use r1 to keep it. */
510 /*----------------------------------------------------------*/
511 mono_domain_lock (domain);
512 code = buf = mono_code_manager_reserve (domain->code_mp, SPECIFIC_TRAMPOLINE_SIZE);
513 mono_domain_unlock (domain);
515 s390_basr (buf, s390_r1, 0);
516 s390_j (buf, 6);
517 s390_llong(buf, arg1);
518 s390_lg (buf, s390_r1, 0, s390_r1, 4);
519 displace = (tramp - buf) / 2;
520 s390_jcl (buf, S390_CC_UN, displace);
522 /* Flush instruction cache, since we've generated code */
523 mono_arch_flush_icache (code, buf - code);
525 /* Sanity check */
526 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
528 if (code_len)
529 *code_len = buf - code;
531 return code;
534 /*========================= End of Function ========================*/
536 gpointer
537 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
539 /* FIXME: implement! */
540 g_assert_not_reached ();
541 return NULL;