Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / tramp-s390.c
blobc71dd61108f60d11840214675861cc888bbecfe2
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+sizeof(gpointer)
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 /* Specific trampoline code fragment sizes */
33 /*------------------------------------------------------------------*/
34 #define SPECIFIC_TRAMPOLINE_SIZE 64
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/s390/s390-codegen.h>
51 #include "mini.h"
52 #include "mini-s390.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 start = code = mono_domain_code_reserve (domain, 28);
104 s390_basr (code, s390_r13, 0);
105 s390_j (code, 4);
106 s390_word (code, addr);
107 s390_l (code, s390_r1, 0, s390_r13, 4);
108 s390_ahi (code, this_pos, sizeof(MonoObject));
109 s390_br (code, s390_r1);
111 g_assert ((code - start) <= 28);
113 mono_arch_flush_icache (start, code - start);
115 return start;
118 /*========================= End of Function ========================*/
120 /*------------------------------------------------------------------*/
121 /* */
122 /* Name - mono_arch_patch_callsite */
123 /* */
124 /* Function - Patch a non-virtual callsite so it calls @addr. */
125 /* */
126 /*------------------------------------------------------------------*/
128 void
129 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
131 gint32 displace;
132 unsigned short opcode;
134 opcode = *((unsigned short *) (orig_code - 6));
135 /* This should be a 'brasl' instruction */
136 g_assert (opcode == 0xc0e5);
137 orig_code -= 4;
138 displace = ((gint32) addr - (gint32) (orig_code - 2)) / 2;
139 s390_patch (orig_code, displace);
140 mono_arch_flush_icache (orig_code, 4);
143 /*========================= End of Function ========================*/
145 void
146 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
148 g_assert_not_reached ();
151 /*========================= End of Function ========================*/
153 /*------------------------------------------------------------------*/
154 /* */
155 /* Name - mono_arch_nullify_class_init_trampoline */
156 /* */
157 /* Function - Nullify a call which calls a class init trampoline */
158 /* */
159 /*------------------------------------------------------------------*/
161 void
162 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
164 char patch[6] = {0x47, 0x00, 0x00, 0x00, 0x07, 0x00};
166 code = code - 6;
168 memcpy(code, patch, sizeof(patch));
171 /*========================= End of Function ========================*/
173 void
174 mono_arch_nullify_plt_entry (guint8 *code)
176 g_assert_not_reached ();
179 /*========================= End of Function ========================*/
181 /*------------------------------------------------------------------*/
182 /* */
183 /* Name - mono_arch_get_vcall_slot */
184 /* */
185 /* Function - This method is called by the arch independent */
186 /* trampoline code to determine the vtable slot used by */
187 /* the call which invoked the trampoline. */
188 /* The calls */
189 /* generated by mono for S/390 will look like either:*/
190 /* 1. l %r1,xxx(%rx) */
191 /* bras %r14,%r1 */
192 /* 2. brasl %r14,xxxxxx */
193 /* */
194 /* Parameters - code - Pointer into caller code */
195 /* regs - Register state at the point of the call */
196 /* displacement - Out parameter which will receive */
197 /* the displacement of the vtable slot */
198 /* */
199 /*------------------------------------------------------------------*/
201 gpointer
202 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
204 int reg;
205 guchar* base;
206 unsigned short opcode;
207 char *sp;
209 // We are passed sp instead of the register array
210 sp = (char*)regs;
212 *displacement = 0;
214 opcode = *((unsigned short *) (code - 6));
215 switch (opcode) {
216 case 0x5810 :
217 /* This is a bras r14,r1 instruction */
218 code -= 4;
219 reg = *code >> 4;
220 *displacement = *((short *)code) & 0x0fff;
221 if (reg > 5)
222 base = *((guchar **) (sp + S390_REG_SAVE_OFFSET+
223 sizeof(int)*(reg-6)));
224 else
225 base = *((guchar **) ((sp - CREATE_STACK_SIZE) +
226 CREATE_GR_OFFSET +
227 sizeof(int)*(reg-2)));
228 return base;
229 case 0x581d :
230 /* l %r1,OFFSET(%r13,%r7) */
231 code -= 4;
232 reg = *code >> 4;
233 *displacement = *((short *)code) & 0x0fff;
234 if (reg > 5)
235 base = *((guchar **) (sp + S390_REG_SAVE_OFFSET+
236 sizeof(int)*(reg-6)));
237 else
238 base = *((guchar **) ((sp - CREATE_STACK_SIZE) +
239 CREATE_GR_OFFSET +
240 sizeof(int)*(reg-2)));
241 base += *((guint32*) (sp + S390_REG_SAVE_OFFSET+
242 sizeof(int)*(s390_r13-6)));
243 return base;
244 case 0xc0e5 :
245 /* This is the 'brasl' instruction */
246 return NULL;
247 default :
248 g_error("Unable to patch instruction prior to %p",code);
251 return NULL;
254 /*========================= End of Function ========================*/
256 gpointer*
257 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
259 gpointer vt;
260 int displacement;
261 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
262 if (!vt)
263 return NULL;
264 return (gpointer*)((char*)vt + displacement);
267 /*========================= End of Function ========================*/
269 /*------------------------------------------------------------------*/
270 /* */
271 /* Name - mono_arch_create_trampoline_code */
272 /* */
273 /* Function - Create the designated type of trampoline according*/
274 /* to the 'tramp_type' parameter. */
275 /* */
276 /*------------------------------------------------------------------*/
278 guchar*
279 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
282 guint8 *buf, *tramp, *code;
283 int i, offset, lmfOffset;
285 /* Now we'll create in 'buf' the S/390 trampoline code. This
286 is the trampoline code common to all methods */
288 code = buf = mono_global_codeman_reserve (512);
290 /*-----------------------------------------------------------
291 STEP 0: First create a non-standard function prologue with a
292 stack size big enough to save our registers.
293 -----------------------------------------------------------*/
295 s390_stm (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
296 s390_lr (buf, s390_r11, s390_r15);
297 s390_ahi (buf, STK_BASE, -CREATE_STACK_SIZE);
298 s390_st (buf, s390_r11, 0, STK_BASE, 0);
299 s390_st (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
300 s390_stm (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
302 /* Save the FP registers */
303 offset = CREATE_FP_OFFSET;
304 for (i = s390_f0; i <= s390_f15; ++i) {
305 s390_std (buf, i, 0, STK_BASE, offset);
306 offset += 8;
309 /*----------------------------------------------------------
310 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
311 LMF. We'll need to restore it after the call to
312 's390_magic_trampoline' and before the call to the native
313 method.
314 ----------------------------------------------------------*/
316 s390_basr (buf, s390_r13, 0);
317 s390_j (buf, 4);
318 s390_word (buf, mono_get_lmf_addr);
319 s390_l (buf, s390_r1, 0, s390_r13, 4);
320 s390_basr (buf, s390_r14, s390_r1);
322 /*---------------------------------------------------------------*/
323 /* we build the MonoLMF structure on the stack - see mini-s390.h */
324 /* Keep in sync with the code in mono_arch_emit_prolog */
325 /*---------------------------------------------------------------*/
326 lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
328 s390_lr (buf, s390_r13, STK_BASE);
329 s390_ahi (buf, s390_r13, lmfOffset);
331 /*---------------------------------------------------------------*/
332 /* Set lmf.lmf_addr = jit_tls->lmf */
333 /*---------------------------------------------------------------*/
334 s390_st (buf, s390_r2, 0, s390_r13,
335 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
337 /*---------------------------------------------------------------*/
338 /* Get current lmf */
339 /*---------------------------------------------------------------*/
340 s390_l (buf, s390_r0, 0, s390_r2, 0);
342 /*---------------------------------------------------------------*/
343 /* Set our lmf as the current lmf */
344 /*---------------------------------------------------------------*/
345 s390_st (buf, s390_r13, 0, s390_r2, 0);
347 /*---------------------------------------------------------------*/
348 /* Have our lmf.previous_lmf point to the last lmf */
349 /*---------------------------------------------------------------*/
350 s390_st (buf, s390_r0, 0, s390_r13,
351 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
353 /*---------------------------------------------------------------*/
354 /* save method info */
355 /*---------------------------------------------------------------*/
356 s390_l (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
357 s390_st (buf, s390_r1, 0, s390_r13,
358 G_STRUCT_OFFSET(MonoLMF, method));
360 /*---------------------------------------------------------------*/
361 /* save the current SP */
362 /*---------------------------------------------------------------*/
363 s390_l (buf, s390_r1, 0, STK_BASE, 0);
364 s390_st (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
366 /*---------------------------------------------------------------*/
367 /* save the current IP */
368 /*---------------------------------------------------------------*/
369 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
370 s390_lhi (buf, s390_r1, 0);
371 } else {
372 s390_l (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
373 s390_la (buf, s390_r1, 0, s390_r1, 0);
375 s390_st (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
377 /*---------------------------------------------------------------*/
378 /* Save general and floating point registers */
379 /*---------------------------------------------------------------*/
380 s390_stm (buf, s390_r2, s390_r12, s390_r13,
381 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
382 for (i = 0; i < 16; i++) {
383 s390_std (buf, i, 0, s390_r13,
384 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
387 /*---------------------------------------------------------------*/
388 /* STEP 2: call the C trampoline function */
389 /*---------------------------------------------------------------*/
391 /* Set arguments */
393 /* Arg 1: gssize *regs. We pass sp instead */
394 s390_lr (buf, s390_r2, STK_BASE);
395 s390_ahi (buf, s390_r2, CREATE_STACK_SIZE);
397 /* Arg 2: code (next address to the instruction that called us) */
398 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
399 s390_lhi (buf, s390_r3, 0);
400 } else {
401 s390_l (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
403 /* Mask out bit 31 */
404 s390_basr (buf, s390_r13, 0);
405 s390_j (buf, 4);
406 s390_word (buf, (~(1 << 31)));
407 s390_n (buf, s390_r3, 0, s390_r13, 4);
410 /* Arg 3: MonoMethod *method. It was put in r1 by the
411 method-specific trampoline code, and then saved before the call
412 to mono_get_lmf_addr()'. */
413 s390_l (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
415 /* Arg 4: trampoline address. Ignore for now */
417 /* Calculate call address and call the C trampoline. Return value will be in r2 */
418 s390_basr (buf, s390_r13, 0);
419 s390_j (buf, 4);
420 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
421 s390_word (buf, tramp);
422 s390_l (buf, s390_r1, 0, s390_r13, 4);
423 s390_basr (buf, s390_r14, s390_r1);
425 /* OK, code address is now on r2. Move it to r1, so that we
426 can restore r2 and use it from r1 later */
427 s390_lr (buf, s390_r1, s390_r2);
429 /*----------------------------------------------------------
430 STEP 3: Restore the LMF
431 ----------------------------------------------------------*/
432 restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
434 /*----------------------------------------------------------
435 STEP 4: call the compiled method
436 ----------------------------------------------------------*/
438 /* Restore registers */
440 s390_lm (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
442 /* Restore the FP registers */
443 offset = CREATE_FP_OFFSET;
444 for (i = s390_f0; i <= s390_f15; ++i) {
445 s390_ld (buf, i, 0, STK_BASE, offset);
446 offset += 8;
449 /* Restore stack pointer and jump to the code -
450 R14 contains the return address to our caller */
451 s390_lr (buf, STK_BASE, s390_r11);
452 s390_lm (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
454 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
455 s390_br (buf, s390_r14);
456 else
457 s390_br (buf, s390_r1);
459 /* Flush instruction cache, since we've generated code */
460 mono_arch_flush_icache (code, buf - code);
462 /* Sanity check */
463 g_assert ((buf - code) <= 512);
465 return code;
468 /*========================= End of Function ========================*/
470 /*------------------------------------------------------------------*/
471 /* */
472 /* Name - mono_arch_create_specific_trampoline */
473 /* */
474 /* Function - Creates the given kind of specific trampoline */
475 /* */
476 /*------------------------------------------------------------------*/
478 gpointer
479 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
481 guint8 *code, *buf, *tramp;
482 gint32 displace;
484 tramp = mono_get_trampoline_code (tramp_type);
486 /*----------------------------------------------------------*/
487 /* This is the method-specific part of the trampoline. Its */
488 /* purpose is to provide the generic part with the */
489 /* MonoMethod *method pointer. We'll use r1 to keep it. */
490 /*----------------------------------------------------------*/
491 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
493 s390_basr (buf, s390_r1, 0);
494 s390_j (buf, 4);
495 s390_word (buf, arg1);
496 s390_l (buf, s390_r1, 0, s390_r1, 4);
497 displace = (tramp - buf) / 2;
498 s390_jcl (buf, S390_CC_UN, displace);
500 /* Flush instruction cache, since we've generated code */
501 mono_arch_flush_icache (code, buf - code);
503 /* Sanity check */
504 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
506 if (code_len)
507 *code_len = buf - code;
509 return code;
512 /*========================= End of Function ========================*/
514 gpointer
515 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
517 /* FIXME: implement! */
518 g_assert_not_reached ();
519 return NULL;