1 /*------------------------------------------------------------------*/
3 /* Name - tramp-s390.c */
5 /* Function - JIT trampoline code for S/390. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /* Copyright - 2001 Ximian, Inc. */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
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 /*------------------------------------------------------------------*/
40 /*------------------------------------------------------------------*/
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>
52 #include "mini-s390x.h"
54 /*========================= End of Includes ========================*/
56 /*------------------------------------------------------------------*/
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 /*------------------------------------------------------------------*/
77 /* Name - mono_arch_get_unbox_trampoline */
79 /* Function - Return a pointer to a trampoline which does the */
80 /* unboxing before calling the method. */
82 /* When value type methods are called through the */
83 /* vtable we need to unbox the 'this' argument. */
85 /* Parameters - method - Methd pointer */
86 /* addr - Pointer to native code for method */
88 /*------------------------------------------------------------------*/
91 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
94 int this_pos = s390_r2;
95 MonoDomain *domain = mono_domain_get ();
98 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
101 mono_domain_lock (domain);
102 start = code = mono_code_manager_reserve (domain->code_mp, 28);
103 mono_domain_unlock (domain);
105 s390_basr (code, s390_r1, 0);
107 s390_llong(code, addr);
108 s390_lg (code, s390_r1, 0, s390_r1, 4);
109 s390_aghi (code, this_pos, sizeof(MonoObject));
110 s390_br (code, s390_r1);
112 g_assert ((code - start) <= 28);
114 mono_arch_flush_icache (start, code - start);
119 /*========================= End of Function ========================*/
121 /*------------------------------------------------------------------*/
123 /* Name - mono_arch_patch_callsite */
125 /* Function - Patch a non-virtual callsite so it calls @addr. */
127 /*------------------------------------------------------------------*/
130 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
133 unsigned short opcode;
135 opcode = *((unsigned short *) (orig_code - 6));
136 if (opcode == 0xc0e5) {
137 /* This is the 'brasl' instruction */
139 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
140 s390_patch_rel (orig_code, displace);
141 mono_arch_flush_icache (orig_code, 4);
143 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
144 g_assert (orig_code [-8] == 0xe3);
145 g_assert (orig_code [-7] == 0xe0);
146 g_assert (orig_code [-6] == 0xd0);
147 g_assert (orig_code [-5] == 0x04);
148 g_assert (orig_code [-4] == 0x00);
149 g_assert (orig_code [-3] == 0x04);
150 opcode = *((unsigned short*) (orig_code - 2));
151 g_assert (opcode == 0x0dee);
153 /* The call address is stored in the 8 bytes preceeding the basr instruction */
154 s390_patch_addr(orig_code - 16, (gssize)addr);
155 mono_arch_flush_icache (orig_code - 16, 8);
159 /*========================= End of Function ========================*/
162 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
164 g_assert_not_reached ();
167 /*========================= End of Function ========================*/
169 /*------------------------------------------------------------------*/
171 /* Name - mono_arch_nullify_class_init_trampoline */
173 /* Function - Nullify a call which calls a class init trampoline */
175 /*------------------------------------------------------------------*/
178 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
180 char patch[2] = {0x07, 0x00};
184 memcpy(code, patch, sizeof(patch));
187 /*========================= End of Function ========================*/
190 mono_arch_nullify_plt_entry (guint8 *code)
192 g_assert_not_reached ();
195 /*========================= End of Function ========================*/
197 /*------------------------------------------------------------------*/
199 /* Name - mono_arch_get_vcall_slot */
201 /* Function - This method is called by the arch independent */
202 /* trampoline code to determine the vtable slot used by */
203 /* the call which invoked the trampoline. */
205 /* Parameters - code - Pointer into caller code */
206 /* regs - Register state at the point of the call */
207 /* displacement - Out parameter which will receive */
208 /* the displacement of the vtable slot */
210 /*------------------------------------------------------------------*/
213 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
217 unsigned short opcode;
220 // We are passed sp instead of the register array
225 opcode = *((unsigned short *) (code - 6));
226 if (opcode == 0xc0e5)
227 /* This is the 'brasl' instruction */
230 /*-----------------------------------*/
231 /* This is a bras r14,Rz instruction */
232 /* If it's preceded by a LG Rx,d(Ry) */
233 /* If Rz == 1 then this is virtual */
235 /*-----------------------------------*/
238 /*-----------------------------------*/
239 /* If call is preceded by LGR then */
240 /* there's nothing to patch */
241 /*-----------------------------------*/
242 if ((code[0] == 0xb9) &&
246 /*-----------------------------------*/
247 /* We back up until we're pointing at*/
248 /* the base/displacement portion of */
249 /* the LG instruction */
250 /*-----------------------------------*/
251 lkReg = code[5] & 0x0f;
253 /*-----------------------------------*/
254 /* The LG instruction has format: */
255 /* E3x0ylllhh04 - where: */
256 /* x = Rx; y = Ry; */
257 /* lll = low 12 bits of displacement */
258 /* hh = high 8 bits of displacement */
259 /*-----------------------------------*/
261 *displacement = (code[2] << 12) +
262 ((code[0] & 0x0f) << 8) +
266 base = *((guchar **) (sp + S390_REG_SAVE_OFFSET +
267 sizeof(long)*(reg-6)));
269 base = *((guchar **) ((sp - CREATE_STACK_SIZE) +
271 sizeof(long)*(reg-2)));
273 /* Non virtual call */
279 /*========================= End of Function ========================*/
282 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
286 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
289 return (gpointer*)((char*)vt + displacement);
292 /*========================= End of Function ========================*/
294 /*------------------------------------------------------------------*/
296 /* Name - mono_arch_create_trampoline_code */
298 /* Function - Create the designated type of trampoline according*/
299 /* to the 'tramp_type' parameter. */
301 /*------------------------------------------------------------------*/
304 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
307 guint8 *buf, *tramp, *code;
308 int i, offset, lmfOffset;
310 /* Now we'll create in 'buf' the S/390 trampoline code. This
311 is the trampoline code common to all methods */
313 code = buf = mono_global_codeman_reserve(512);
315 /*-----------------------------------------------------------
316 STEP 0: First create a non-standard function prologue with a
317 stack size big enough to save our registers.
318 -----------------------------------------------------------*/
320 s390_stmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
321 s390_lgr (buf, s390_r11, s390_r15);
322 s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
323 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
324 s390_stg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
325 s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
327 /* Save the FP registers */
328 offset = CREATE_FP_OFFSET;
329 for (i = s390_f0; i <= s390_f15; ++i) {
330 s390_std (buf, i, 0, STK_BASE, offset);
334 /*----------------------------------------------------------
335 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
336 LMF. We'll need to restore it after the call to
337 's390_magic_trampoline' and before the call to the native
339 ----------------------------------------------------------*/
341 s390_basr (buf, s390_r13, 0);
343 s390_llong(buf, mono_get_lmf_addr);
344 s390_lg (buf, s390_r1, 0, s390_r13, 4);
345 s390_basr (buf, s390_r14, s390_r1);
347 /*---------------------------------------------------------------*/
348 /* we build the MonoLMF structure on the stack - see mini-s390.h */
349 /* Keep in sync with the code in mono_arch_emit_prolog */
350 /*---------------------------------------------------------------*/
351 lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
353 s390_lgr (buf, s390_r13, STK_BASE);
354 s390_aghi (buf, s390_r13, lmfOffset);
356 /*---------------------------------------------------------------*/
357 /* Set lmf.lmf_addr = jit_tls->lmf */
358 /*---------------------------------------------------------------*/
359 s390_stg (buf, s390_r2, 0, s390_r13,
360 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
362 /*---------------------------------------------------------------*/
363 /* Get current lmf */
364 /*---------------------------------------------------------------*/
365 s390_lg (buf, s390_r0, 0, s390_r2, 0);
367 /*---------------------------------------------------------------*/
368 /* Set our lmf as the current lmf */
369 /*---------------------------------------------------------------*/
370 s390_stg (buf, s390_r13, 0, s390_r2, 0);
372 /*---------------------------------------------------------------*/
373 /* Have our lmf.previous_lmf point to the last lmf */
374 /*---------------------------------------------------------------*/
375 s390_stg (buf, s390_r0, 0, s390_r13,
376 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
378 /*---------------------------------------------------------------*/
379 /* save method info */
380 /*---------------------------------------------------------------*/
381 s390_lg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
382 s390_stg (buf, s390_r1, 0, s390_r13,
383 G_STRUCT_OFFSET(MonoLMF, method));
385 /*---------------------------------------------------------------*/
386 /* save the current SP */
387 /*---------------------------------------------------------------*/
388 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
389 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
391 /*---------------------------------------------------------------*/
392 /* save the current IP */
393 /*---------------------------------------------------------------*/
394 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
395 s390_lghi (buf, s390_r1, 0);
397 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
398 // s390_la (buf, s390_r1, 0, s390_r1, 0);
400 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
402 /*---------------------------------------------------------------*/
403 /* Save general and floating point registers */
404 /*---------------------------------------------------------------*/
405 s390_stmg (buf, s390_r2, s390_r12, s390_r13,
406 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
407 for (i = 0; i < 16; i++) {
408 s390_std (buf, i, 0, s390_r13,
409 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
412 /*---------------------------------------------------------------*/
413 /* STEP 2: call the C trampoline function */
414 /*---------------------------------------------------------------*/
418 /* Arg 1: gssize *regs. We pass sp instead */
419 s390_lgr (buf, s390_r2, STK_BASE);
420 s390_ahi (buf, s390_r2, CREATE_STACK_SIZE);
422 /* Arg 2: code (next address to the instruction that called us) */
423 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
424 s390_lghi (buf, s390_r3, 0);
426 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
429 /* Arg 3: MonoMethod *method. It was put in r1 by the
430 method-specific trampoline code, and then saved before the call
431 to mono_get_lmf_addr()'. */
432 s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
434 /* Arg 4: trampoline address. Ignore for now */
436 /* Calculate call address and call the C trampoline. Return value will be in r2 */
437 s390_basr (buf, s390_r13, 0);
439 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
440 s390_llong (buf, tramp);
441 s390_lg (buf, s390_r1, 0, s390_r13, 4);
442 s390_basr (buf, s390_r14, s390_r1);
444 /* OK, code address is now on r2. Move it to r1, so that we
445 can restore r2 and use it from r1 later */
446 s390_lgr (buf, s390_r1, s390_r2);
448 /*----------------------------------------------------------
449 STEP 3: Restore the LMF
450 ----------------------------------------------------------*/
451 restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
453 /*----------------------------------------------------------
454 STEP 4: call the compiled method
455 ----------------------------------------------------------*/
457 /* Restore registers */
459 s390_lmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
461 /* Restore the FP registers */
462 offset = CREATE_FP_OFFSET;
463 for (i = s390_f0; i <= s390_f15; ++i) {
464 s390_ld (buf, i, 0, STK_BASE, offset);
468 /* Restore stack pointer and jump to the code -
469 R14 contains the return address to our caller */
470 s390_lgr (buf, STK_BASE, s390_r11);
471 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
473 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
474 s390_br (buf, s390_r14);
476 s390_br (buf, s390_r1);
478 /* Flush instruction cache, since we've generated code */
479 mono_arch_flush_icache (code, buf - code);
482 g_assert ((buf - code) <= 512);
487 /*========================= End of Function ========================*/
489 /*------------------------------------------------------------------*/
491 /* Name - mono_arch_create_specific_trampoline */
493 /* Function - Creates the given kind of specific trampoline */
495 /*------------------------------------------------------------------*/
498 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
500 guint8 *code, *buf, *tramp;
503 tramp = mono_get_trampoline_code (tramp_type);
505 /*----------------------------------------------------------*/
506 /* This is the method-specific part of the trampoline. Its */
507 /* purpose is to provide the generic part with the */
508 /* MonoMethod *method pointer. We'll use r1 to keep it. */
509 /*----------------------------------------------------------*/
510 mono_domain_lock (domain);
511 code = buf = mono_code_manager_reserve (domain->code_mp, SPECIFIC_TRAMPOLINE_SIZE);
512 mono_domain_unlock (domain);
514 s390_basr (buf, s390_r1, 0);
516 s390_llong(buf, arg1);
517 s390_lg (buf, s390_r1, 0, s390_r1, 4);
518 displace = (tramp - buf) / 2;
519 s390_jcl (buf, S390_CC_UN, displace);
521 /* Flush instruction cache, since we've generated code */
522 mono_arch_flush_icache (code, buf - code);
525 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
528 *code_len = buf - code;
533 /*========================= End of Function ========================*/
536 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
538 /* FIXME: implement! */
539 g_assert_not_reached ();
544 mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
546 /* FIXME: implement! */
547 g_assert_not_reached ();