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 CREATE_STACK_SIZE (S390_MINIMAL_STACK_SIZE+GR_SAVE_SIZE+FP_SAVE_SIZE+2*sizeof(long))
26 #define CREATE_GR_OFFSET S390_MINIMAL_STACK_SIZE
27 #define CREATE_FP_OFFSET CREATE_GR_OFFSET+GR_SAVE_SIZE
28 #define CREATE_LMF_OFFSET CREATE_FP_OFFSET+FP_SAVE_SIZE
29 #define METHOD_SAVE_OFFSET S390_RET_ADDR_OFFSET-4
31 /*------------------------------------------------------------------*/
32 /* adapt to mini later... */
33 /*------------------------------------------------------------------*/
34 #define mono_jit_share_code (1)
36 /*------------------------------------------------------------------*/
37 /* Method-specific trampoline code fragment sizes */
38 /*------------------------------------------------------------------*/
39 #define METHOD_TRAMPOLINE_SIZE 64
40 #define JUMP_TRAMPOLINE_SIZE 64
42 /*========================= End of Defines =========================*/
44 /*------------------------------------------------------------------*/
46 /*------------------------------------------------------------------*/
52 #include <mono/metadata/appdomain.h>
53 #include <mono/metadata/marshal.h>
54 #include <mono/metadata/tabledefs.h>
55 #include <mono/arch/s390/s390-codegen.h>
56 #include <mono/metadata/mono-debug-debugger.h>
59 #include "mini-s390.h"
61 /*========================= End of Includes ========================*/
63 /*------------------------------------------------------------------*/
65 /*------------------------------------------------------------------*/
68 MONO_TRAMPOLINE_GENERIC,
70 MONO_TRAMPOLINE_CLASS_INIT
73 /*========================= End of Typedefs ========================*/
75 /*------------------------------------------------------------------*/
76 /* P r o t o t y p e s */
77 /*------------------------------------------------------------------*/
79 /*------------------------------------------------------------------*/
80 /* Address of the generic trampoline code. This is used by the */
81 /* debugger to check whether a method is a trampoline. */
82 /*------------------------------------------------------------------*/
83 guint8 *mono_generic_trampoline_code = NULL;
85 /*========================= End of Prototypes ======================*/
87 /*------------------------------------------------------------------*/
88 /* G l o b a l V a r i a b l e s */
89 /*------------------------------------------------------------------*/
92 /*====================== End of Global Variables ===================*/
94 /*------------------------------------------------------------------*/
96 /* Name - get_unbox_trampoline */
98 /* Function - Return a pointer to a trampoline which does the */
99 /* unboxing before calling the method. */
101 /* When value type methods are called through the */
102 /* vtable we need to unbox the 'this' argument. */
104 /* Parameters - method - Methd pointer */
105 /* addr - Pointer to native code for method */
107 /*------------------------------------------------------------------*/
110 get_unbox_trampoline (MonoMethod *method, gpointer addr)
112 guint8 *code, *start;
113 int this_pos = s390_r2;
116 if ((method->klass->valuetype)) {
117 // if ((method->klass->valuetype) &&
118 // (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
119 if ((!method->signature->ret->byref) &&
120 (MONO_TYPE_ISSTRUCT (method->signature->ret)))
123 start = code = g_malloc (28);
125 s390_basr (code, s390_r13, 0);
127 s390_word (code, addr);
128 s390_l (code, s390_r1, 0, s390_r13, 4);
129 s390_ahi (code, this_pos, sizeof(MonoObject));
130 s390_br (code, s390_r1);
132 g_assert ((code - start) <= 28);
138 /*========================= End of Function ========================*/
140 /*------------------------------------------------------------------*/
142 /* Name - s390_magic_trampoline */
144 /* Function - This method is called by the function */
145 /* "arch_create_jit_trampoline", which in turn is */
146 /* called by the trampoline functions for virtual */
147 /* methods. After having called the JIT compiler to */
148 /* compile the method, it inspects the caller code */
149 /* to find the address of the method-specific part */
150 /* of the trampoline vtable slot for this method, */
151 /* updates it with a fragment that calls the newly */
152 /* compiled code and returns this address. The calls */
153 /* generated by mono for S/390 will look like either:*/
154 /* 1. l %r1,xxx(%rx) */
156 /* 2. brasl %r14,xxxxxx */
158 /* Parameters - code - Pointer into caller code */
159 /* method - The method to compile */
160 /* sp - Stack pointer */
162 /*------------------------------------------------------------------*/
165 s390_magic_trampoline (MonoMethod *method, guchar *code, char *sp)
170 unsigned short opcode;
175 addr = mono_compile_method(method);
181 /* The top bit needs to be ignored on S/390 */
182 (guint32) code &= 0x7fffffff;
184 fname = mono_method_full_name (method, TRUE);
185 codeJi = mono_jit_info_table_find (mono_domain_get(), code);
186 addrJi = mono_jit_info_table_find (mono_domain_get(), addr);
188 opcode = *((unsigned short *) (code - 6));
191 /* This is a bras r14,r1 instruction */
194 displace = *((short *)code) & 0x0fff;
196 base = *((int *) (sp + S390_REG_SAVE_OFFSET+
197 sizeof(int)*(reg-6)));
199 base = *((int *) (sp + CREATE_GR_OFFSET+
200 sizeof(int)*(reg-2)));
201 addr = get_unbox_trampoline(method, addr);
202 if (mono_method_same_domain (codeJi, addrJi)) {
203 code = base + displace;
204 s390_patch(code, addr);
208 /* This is the 'brasl' instruction */
210 displace = ((gint32) addr - (gint32) (code - 2)) / 2;
211 if (mono_method_same_domain (codeJi, addrJi)) {
212 s390_patch (code, displace);
213 mono_arch_flush_icache (code, 4);
217 g_error("Unable to patch instruction prior to %p",code);
225 /*========================= End of Function ========================*/
227 /*------------------------------------------------------------------*/
229 /* Name - s390_class_init_trampoline */
231 /* Function - Initialize a class and then no-op the call to */
232 /* the trampoline. */
234 /*------------------------------------------------------------------*/
237 s390_class_init_trampoline (void *vtable, guchar *code, char *sp)
239 char patch[6] = {0x47, 0x00, 0x00, 0x00, 0x07, 0x00};
241 mono_runtime_class_init (vtable);
245 memcpy(code, patch, sizeof(patch));
248 /*========================= End of Function ========================*/
250 /*------------------------------------------------------------------*/
252 /* Name - create_trampoline_code */
254 /* Function - Create the designated type of trampoline according*/
255 /* to the 'tramp_type' parameter. */
257 /*------------------------------------------------------------------*/
260 create_trampoline_code (MonoTrampolineType tramp_type)
263 guint8 *buf, *code = NULL;
264 static guint8* generic_jump_trampoline = NULL;
265 static guint8 *generic_class_init_trampoline = NULL;
268 switch (tramp_type) {
269 case MONO_TRAMPOLINE_GENERIC:
270 if (mono_generic_trampoline_code)
271 return mono_generic_trampoline_code;
273 case MONO_TRAMPOLINE_JUMP:
274 if (generic_jump_trampoline)
275 return generic_jump_trampoline;
277 case MONO_TRAMPOLINE_CLASS_INIT:
278 if (generic_class_init_trampoline)
279 return generic_class_init_trampoline;
284 /* Now we'll create in 'buf' the S/390 trampoline code. This
285 is the trampoline code common to all methods */
287 code = buf = g_malloc(512);
289 /*-----------------------------------------------------------
290 STEP 0: First create a non-standard function prologue with a
291 stack size big enough to save our registers.
292 -----------------------------------------------------------*/
294 s390_stm (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
295 s390_lr (buf, s390_r11, s390_r15);
296 s390_ahi (buf, STK_BASE, -CREATE_STACK_SIZE);
297 s390_st (buf, s390_r11, 0, STK_BASE, 0);
298 s390_stm (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
300 /* Save the FP registers */
301 offset = CREATE_FP_OFFSET;
302 for (i = s390_f0; i <= s390_f15; ++i) {
303 s390_std (buf, i, 0, STK_BASE, offset);
307 /*----------------------------------------------------------
308 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
309 LMF. We'll need to restore it after the call to
310 's390_magic_trampoline' and before the call to the native
312 ----------------------------------------------------------*/
314 s390_basr (buf, s390_r13, 0);
316 s390_word (buf, mono_get_lmf_addr);
317 s390_l (buf, s390_r1, 0, s390_r13, 4);
318 s390_basr (buf, s390_r14, s390_r1);
320 /* XXX Update LMF !!! */
322 /*----------------------------------------------------------
323 STEP 2: call 's390_magic_trampoline()', who will compile the
324 code and fix the method vtable entry for us
325 ----------------------------------------------------------*/
329 /* Arg 1: MonoMethod *method. It was put in r11 by the
330 method-specific trampoline code, and then saved before the call
331 to mono_get_lmf_addr()'. Restore r13, by the way :-) */
332 s390_l (buf, s390_r2, 0, s390_r11, METHOD_SAVE_OFFSET);
334 /* Arg 2: code (next address to the instruction that called us) */
335 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
336 s390_lhi (buf, s390_r3, 0);
338 s390_l (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
341 /* Arg 3: stack pointer */
342 s390_lr (buf, s390_r4, STK_BASE);
344 /* Calculate call address and call
345 's390_magic_trampoline'. Return value will be in r2 */
346 s390_basr (buf, s390_r13, 0);
348 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
349 s390_word (buf, s390_class_init_trampoline);
351 s390_word (buf, s390_magic_trampoline);
353 s390_l (buf, s390_r1, 0, s390_r13, 4);
354 s390_basr (buf, s390_r14, s390_r1);
356 /* OK, code address is now on r2. Move it to r1, so that we
357 can restore r2 and use it from r1 later */
358 s390_lr (buf, s390_r1, s390_r2);
361 /*----------------------------------------------------------
362 STEP 3: Restore the LMF
363 ----------------------------------------------------------*/
367 /*----------------------------------------------------------
368 STEP 4: call the compiled method
369 ----------------------------------------------------------*/
371 /* Restore registers */
373 s390_lm (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
375 /* Restore the FP registers */
376 offset = CREATE_FP_OFFSET;
377 for (i = s390_f0; i <= s390_f15; ++i) {
378 s390_ld (buf, i, 0, STK_BASE, offset);
382 /* Restore stack pointer and jump to the code -
383 R14 contains the return address to our caller */
384 s390_lr (buf, STK_BASE, s390_r11);
385 s390_lm (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
386 s390_br (buf, s390_r1);
388 /* Flush instruction cache, since we've generated code */
389 mono_arch_flush_icache (code, buf - code);
392 g_assert ((buf - code) <= 512);
395 switch (tramp_type) {
396 case MONO_TRAMPOLINE_GENERIC:
397 mono_generic_trampoline_code = code;
399 case MONO_TRAMPOLINE_JUMP:
400 generic_jump_trampoline = code;
402 case MONO_TRAMPOLINE_CLASS_INIT:
403 generic_class_init_trampoline = code;
410 /*========================= End of Function ========================*/
412 /*------------------------------------------------------------------*/
414 /* Name - mono_arch_create_jump_trampoline */
416 /* Function - Create the designated type of trampoline according*/
417 /* to the 'tramp_type' parameter. */
419 /*------------------------------------------------------------------*/
422 mono_arch_create_jump_trampoline (MonoMethod *method)
424 guint8 *code, *buf, *tramp = NULL;
426 MonoDomain *domain = mono_domain_get();
429 tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
431 mono_domain_lock (domain);
432 code = buf = mono_code_manager_reserve (domain->code_mp, METHOD_TRAMPOLINE_SIZE);
433 mono_domain_unlock (domain);
435 s390_basr (buf, s390_r13, 0);
437 s390_word (buf, method);
438 s390_l (buf, s390_r13, 0, s390_r13, 4);
439 displace = (tramp - buf) / 2;
440 s390_jcl (buf, S390_CC_UN, displace);
442 mono_arch_flush_icache (code, buf-code);
444 g_assert ((buf - code) <= JUMP_TRAMPOLINE_SIZE);
446 ji = g_new0 (MonoJitInfo, 1);
448 ji->code_start = code;
449 ji->code_size = buf - code;
451 mono_jit_stats.method_trampolines++;
456 /*========================= End of Function ========================*/
458 /*------------------------------------------------------------------*/
460 /* Name - mono_arch_create_jit_trampoline */
462 /* Function - Creates a trampoline function for virtual methods.*/
463 /* If the created code is called it first starts JIT */
464 /* compilation and then calls the newly created */
465 /* method. It also replaces the corresponding vtable */
466 /* entry (see s390_magic_trampoline). */
468 /* A trampoline consists of two parts: a main */
469 /* fragment, shared by all method trampolines, and */
470 /* and some code specific to each method, which */
471 /* hard-codes a reference to that method and then */
472 /* calls the main fragment. */
474 /* The main fragment contains a call to */
475 /* 's390_magic_trampoline', which performs a call */
476 /* to the JIT compiler and substitutes the method- */
477 /* specific fragment with some code that directly */
478 /* calls the JIT-compiled method. */
480 /* Parameter - method - Pointer to the method information */
482 /* Returns - A pointer to the newly created code */
484 /*------------------------------------------------------------------*/
487 mono_arch_create_jit_trampoline (MonoMethod *method)
490 static guint8 *vc = NULL;
493 vc = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
495 /* This is the method-specific part of the trampoline. Its purpose is
496 to provide the generic part with the MonoMethod *method pointer. We'll
497 use r13 to keep that value, for instance. However, the generic part of
498 the trampoline relies on r11 having the same value it had before coming
499 here, so we must save it before. */
500 code = buf = g_malloc(METHOD_TRAMPOLINE_SIZE);
502 s390_basr (buf, s390_r13, 0);
504 s390_word (buf, method);
505 s390_l (buf, s390_r13, 0, s390_r13, 4);
506 displace = (vc - buf) / 2;
507 s390_jcl (buf, S390_CC_UN, displace);
509 /* Flush instruction cache, since we've generated code */
510 mono_arch_flush_icache (code, buf - code);
513 g_assert ((buf - code) <= METHOD_TRAMPOLINE_SIZE);
515 mono_jit_stats.method_trampolines++;
520 /*========================= End of Function ========================*/
522 /*------------------------------------------------------------------*/
524 /* Name - mono_arch_create_class_init_trampoline */
526 /* Function - Creates a trampoline function to run a type init- */
527 /* ializer. If the trampoline is called, it calls */
528 /* mono_runtime_class_init with the given vtable, */
529 /* then patches the caller code so it does not get */
530 /* called any more. */
532 /* Parameter - vtable - The type to initialize */
534 /* Returns - A pointer to the newly created code */
536 /*------------------------------------------------------------------*/
539 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
541 guint8 *code, *buf, *tramp;
543 tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
545 /* This is the method-specific part of the trampoline. Its purpose is
546 to provide the generic part with the MonoMethod *method pointer. We'll
547 use r11 to keep that value, for instance. However, the generic part of
548 the trampoline relies on r11 having the same value it had before coming
549 here, so we must save it before. */
550 code = buf = g_malloc(METHOD_TRAMPOLINE_SIZE);
552 s390_st (buf, s390_r14, 0, STK_BASE, S390_RET_ADDR_OFFSET);
553 s390_ahi (buf, STK_BASE, -S390_MINIMAL_STACK_SIZE);
555 s390_basr (buf, s390_r1, 0);
557 s390_word (buf, vtable);
558 s390_word (buf, s390_class_init_trampoline);
559 s390_lr (buf, s390_r3, s390_r14);
560 s390_l (buf, s390_r2, 0, s390_r1, 4);
561 s390_lhi (buf, s390_r4, 0);
562 s390_l (buf, s390_r1, 0, s390_r1, 8);
563 s390_basr (buf, s390_r14, s390_r1);
565 s390_ahi (buf, STK_BASE, S390_MINIMAL_STACK_SIZE);
566 s390_l (buf, s390_r14, 0, STK_BASE, S390_RET_ADDR_OFFSET);
567 s390_br (buf, s390_r14);
569 /* Flush instruction cache, since we've generated code */
570 mono_arch_flush_icache (code, buf - code);
573 g_assert ((buf - code) <= METHOD_TRAMPOLINE_SIZE);
575 mono_jit_stats.method_trampolines++;
580 /*========================= End of Function ========================*/
582 /*------------------------------------------------------------------*/
584 /* Name - mono_debuger_create_notification_function */
586 /* Function - This method is only called when running in the */
587 /* Mono debugger. It returns a pointer to the */
588 /* arch specific notification function. */
590 /*------------------------------------------------------------------*/
593 mono_debugger_create_notification_function (gpointer *notification_address)
597 ptr = buf = g_malloc0 (16);
599 if (notification_address)
600 *notification_address = buf;
601 s390_br (buf, s390_r14);
606 /*========================= End of Function ========================*/