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 - gsctx - Generic sharing context */
86 /* method - Methd pointer */
87 /* addr - Pointer to native code for method */
89 /*------------------------------------------------------------------*/
92 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *method, gpointer addr)
95 int this_pos = s390_r2;
96 MonoDomain *domain = mono_domain_get ();
98 start = code = mono_domain_code_reserve (domain, 28);
100 s390_basr (code, s390_r1, 0);
102 s390_llong(code, addr);
103 s390_lg (code, s390_r1, 0, s390_r1, 4);
104 s390_aghi (code, this_pos, sizeof(MonoObject));
105 s390_br (code, s390_r1);
107 g_assert ((code - start) <= 28);
109 mono_arch_flush_icache (start, code - start);
114 /*========================= End of Function ========================*/
116 /*------------------------------------------------------------------*/
118 /* Name - mono_arch_patch_callsite */
120 /* Function - Patch a non-virtual callsite so it calls @addr. */
122 /*------------------------------------------------------------------*/
125 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
128 unsigned short opcode;
130 opcode = *((unsigned short *) (orig_code - 6));
131 if (opcode == 0xc0e5) {
132 /* This is the 'brasl' instruction */
134 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
135 s390_patch_rel (orig_code, displace);
136 mono_arch_flush_icache (orig_code, 4);
138 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
139 g_assert (orig_code [-8] == 0xe3);
140 g_assert (orig_code [-7] == 0xe0);
141 g_assert (orig_code [-6] == 0xd0);
142 g_assert (orig_code [-5] == 0x04);
143 g_assert (orig_code [-4] == 0x00);
144 g_assert (orig_code [-3] == 0x04);
145 opcode = *((unsigned short*) (orig_code - 2));
146 g_assert (opcode == 0x0dee);
148 /* The call address is stored in the 8 bytes preceeding the basr instruction */
149 s390_patch_addr(orig_code - 16, (gssize)addr);
150 mono_arch_flush_icache (orig_code - 16, 8);
154 /*========================= End of Function ========================*/
156 /*------------------------------------------------------------------*/
158 /* Name - mono_arch_patch_plt_entry. */
160 /* Function - Patch a PLT entry - unused as yet. */
162 /*------------------------------------------------------------------*/
165 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
167 g_assert_not_reached ();
170 /*========================= End of Function ========================*/
172 /*------------------------------------------------------------------*/
174 /* Name - mono_arch_nullify_class_init_trampoline */
176 /* Function - Nullify a call which calls a class init trampoline*/
178 /*------------------------------------------------------------------*/
181 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
183 char patch[2] = {0x07, 0x00};
187 memcpy(code, patch, sizeof(patch));
190 /*========================= End of Function ========================*/
192 /*------------------------------------------------------------------*/
194 /* Name - mono_arch_nullify_plt_entry */
196 /* Function - Nullify a PLT entry call. */
198 /*------------------------------------------------------------------*/
201 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
203 g_assert_not_reached ();
206 /*========================= End of Function ========================*/
208 /*------------------------------------------------------------------*/
210 /* Name - mono_arch_create_trampoline_code */
212 /* Function - Create the designated type of trampoline according*/
213 /* to the 'tramp_type' parameter. */
215 /*------------------------------------------------------------------*/
218 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
220 guint8 *buf, *tramp, *code;
221 int i, offset, lmfOffset;
227 /* Now we'll create in 'buf' the S/390 trampoline code. This
228 is the trampoline code common to all methods */
230 code = buf = mono_global_codeman_reserve(512);
232 /*-----------------------------------------------------------
233 STEP 0: First create a non-standard function prologue with a
234 stack size big enough to save our registers.
235 -----------------------------------------------------------*/
237 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
238 s390_lgr (buf, s390_r11, s390_r15);
239 s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
240 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
241 s390_stg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
242 s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
244 /* Save the FP registers */
245 offset = CREATE_FP_OFFSET;
246 for (i = s390_f0; i <= s390_f15; ++i) {
247 s390_std (buf, i, 0, STK_BASE, offset);
251 /*----------------------------------------------------------
252 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
253 LMF. We'll need to restore it after the call to
254 's390_magic_trampoline' and before the call to the native
256 ----------------------------------------------------------*/
258 s390_basr (buf, s390_r13, 0);
260 s390_llong(buf, mono_get_lmf_addr);
261 s390_lg (buf, s390_r1, 0, s390_r13, 4);
262 s390_basr (buf, s390_r14, s390_r1);
264 /*---------------------------------------------------------------*/
265 /* we build the MonoLMF structure on the stack - see mini-s390.h */
266 /* Keep in sync with the code in mono_arch_emit_prolog */
267 /*---------------------------------------------------------------*/
268 lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
270 s390_lgr (buf, s390_r13, STK_BASE);
271 s390_aghi (buf, s390_r13, lmfOffset);
273 /*---------------------------------------------------------------*/
274 /* Set lmf.lmf_addr = jit_tls->lmf */
275 /*---------------------------------------------------------------*/
276 s390_stg (buf, s390_r2, 0, s390_r13,
277 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
279 /*---------------------------------------------------------------*/
280 /* Get current lmf */
281 /*---------------------------------------------------------------*/
282 s390_lg (buf, s390_r0, 0, s390_r2, 0);
284 /*---------------------------------------------------------------*/
285 /* Set our lmf as the current lmf */
286 /*---------------------------------------------------------------*/
287 s390_stg (buf, s390_r13, 0, s390_r2, 0);
289 /*---------------------------------------------------------------*/
290 /* Have our lmf.previous_lmf point to the last lmf */
291 /*---------------------------------------------------------------*/
292 s390_stg (buf, s390_r0, 0, s390_r13,
293 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
295 /*---------------------------------------------------------------*/
296 /* save method info */
297 /*---------------------------------------------------------------*/
298 s390_lg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
299 s390_stg (buf, s390_r1, 0, s390_r13,
300 G_STRUCT_OFFSET(MonoLMF, method));
302 /*---------------------------------------------------------------*/
303 /* save the current SP */
304 /*---------------------------------------------------------------*/
305 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
306 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
308 /*---------------------------------------------------------------*/
309 /* save the current IP */
310 /*---------------------------------------------------------------*/
311 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
312 s390_lghi (buf, s390_r1, 0);
314 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
315 // s390_la (buf, s390_r1, 0, s390_r1, 0);
317 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
319 /*---------------------------------------------------------------*/
320 /* Save general and floating point registers */
321 /*---------------------------------------------------------------*/
322 s390_mvc (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]),
323 STK_BASE, CREATE_GR_OFFSET);
324 s390_mvc (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
325 s390_r11, S390_REG_SAVE_OFFSET);
327 /* Simply copy fpregs already saved above */
328 s390_mvc (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
329 STK_BASE, CREATE_FP_OFFSET);
331 /*---------------------------------------------------------------*/
332 /* STEP 2: call the C trampoline function */
333 /*---------------------------------------------------------------*/
337 /* Arg 1: mgreg_t *regs. We pass sp instead */
338 s390_la (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
340 /* Arg 2: code (next address to the instruction that called us) */
341 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
342 s390_lghi (buf, s390_r3, 0);
344 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
347 /* Arg 3: MonoMethod *method. It was put in r1 by the
348 method-specific trampoline code, and then saved before the call
349 to mono_get_lmf_addr()'. */
350 s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
352 /* Arg 4: trampoline address. Ignore for now */
354 /* Calculate call address and call the C trampoline. Return value will be in r2 */
355 s390_basr (buf, s390_r13, 0);
357 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
358 s390_llong (buf, tramp);
359 s390_lg (buf, s390_r1, 0, s390_r13, 4);
360 s390_basr (buf, s390_r14, s390_r1);
362 /* OK, code address is now on r2. Move it to r1, so that we
363 can restore r2 and use it from r1 later */
364 s390_lgr (buf, s390_r1, s390_r2);
366 /*----------------------------------------------------------
367 STEP 3: Restore the LMF
368 ----------------------------------------------------------*/
369 restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
371 /*----------------------------------------------------------
372 STEP 4: call the compiled method
373 ----------------------------------------------------------*/
375 /* Restore registers */
377 s390_lmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
379 /* Restore the FP registers */
380 offset = CREATE_FP_OFFSET;
381 for (i = s390_f0; i <= s390_f15; ++i) {
382 s390_ld (buf, i, 0, STK_BASE, offset);
386 /* Restore stack pointer and jump to the code -
387 R14 contains the return address to our caller */
388 s390_lgr (buf, STK_BASE, s390_r11);
389 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
391 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
392 s390_br (buf, s390_r14);
394 s390_br (buf, s390_r1);
396 /* Flush instruction cache, since we've generated code */
397 mono_arch_flush_icache (code, buf - code);
400 g_assert ((buf - code) <= 512);
405 /*========================= End of Function ========================*/
407 /*------------------------------------------------------------------*/
409 /* Name - mono_arch_create_specific_trampoline */
411 /* Function - Creates the given kind of specific trampoline */
413 /*------------------------------------------------------------------*/
416 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
418 guint8 *code, *buf, *tramp;
421 tramp = mono_get_trampoline_code (tramp_type);
423 /*----------------------------------------------------------*/
424 /* This is the method-specific part of the trampoline. Its */
425 /* purpose is to provide the generic part with the */
426 /* MonoMethod *method pointer. We'll use r1 to keep it. */
427 /*----------------------------------------------------------*/
428 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
430 s390_basr (buf, s390_r1, 0);
432 s390_llong(buf, arg1);
433 s390_lg (buf, s390_r1, 0, s390_r1, 4);
434 displace = (tramp - buf) / 2;
435 s390_jcl (buf, S390_CC_UN, displace);
437 /* Flush instruction cache, since we've generated code */
438 mono_arch_flush_icache (code, buf - code);
441 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
444 *code_len = buf - code;
449 /*========================= End of Function ========================*/
451 /*------------------------------------------------------------------*/
453 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
457 /*------------------------------------------------------------------*/
460 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
462 /* FIXME: implement! */
463 g_assert_not_reached ();
467 /*========================= End of Function ========================*/