2008-01-27 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-ia64.c
1 /*
2  * tramp-ia64.c: JIT trampoline code for ia64
3  *
4  * Authors:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/mono-debug-debugger.h>
17 #include <mono/arch/ia64/ia64-codegen.h>
18
19 #include "mini.h"
20 #include "mini-ia64.h"
21
22 #define GP_SCRATCH_REG 31
23 #define GP_SCRATCH_REG2 30
24
25 /*
26  * mono_arch_get_unbox_trampoline:
27  * @m: method pointer
28  * @addr: pointer to native code for @m
29  *
30  * when value type methods are called through the vtable we need to unbox the
31  * this argument. This method returns a pointer to a trampoline which does
32  * unboxing before calling the method
33  */
34 gpointer
35 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
36 {
37         guint8 *buf;
38         gpointer func_addr, func_gp;
39         Ia64CodegenState code;
40         int this_reg = 0;
41         gpointer *desc;
42         MonoDomain *domain = mono_domain_get ();
43
44         /* FIXME: Optimize this */
45
46         if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
47                 this_reg = 1;
48
49         func_addr = ((gpointer*)addr) [0];
50         func_gp = ((gpointer*)addr) [1];
51
52         mono_domain_lock (domain);
53         buf = mono_code_manager_reserve (domain->code_mp, 256);
54         mono_domain_unlock (domain);
55
56         /* Since the this reg is a stacked register, its a bit hard to access it */
57         ia64_codegen_init (code, buf);
58         ia64_alloc (code, 40, 8, 1, 0, 0);
59         ia64_adds_imm (code, 32 + this_reg, sizeof (MonoObject), 32 + this_reg);
60         ia64_mov_to_ar_i (code, IA64_PFS, 40);  
61         ia64_movl (code, GP_SCRATCH_REG, func_addr);
62         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
63         ia64_br_cond_reg (code, IA64_B6);
64         ia64_codegen_close (code);
65
66         g_assert (code.buf - buf < 256);
67
68         mono_arch_flush_icache (buf, code.buf - buf);
69
70         /* FIXME: */
71         desc = g_malloc0 (sizeof (gpointer) * 2);
72         desc [0] = buf;
73         desc [1] = func_gp;
74
75         return desc;
76 }
77
78 void
79 mono_arch_patch_callsite (guint8 *code, guint8 *addr)
80 {
81         guint8 *callsite_begin;
82         guint64 *callsite = (guint64*)(gpointer)(code - 16);
83         guint64 *next_bundle;
84         guint64 ins, instructions [3];
85         guint64 buf [16];
86         Ia64CodegenState gen;
87         gpointer func = ((gpointer*)(gpointer)addr)[0];
88
89         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
90                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
91                 callsite -= 2;
92         callsite_begin = (guint8*)callsite;
93
94         next_bundle = callsite + 2;
95         ins = ia64_bundle_ins1 (next_bundle);
96         if (ia64_ins_opcode (ins) == 5) {
97                 /* ld8_inc_imm -> indirect call through a function pointer */
98                 g_assert (ia64_ins_r1 (ins) == GP_SCRATCH_REG2);
99                 g_assert (ia64_ins_r3 (ins) == GP_SCRATCH_REG);
100                 return;
101         }
102
103         /* Patch the code generated by emit_call */
104
105         instructions [0] = ia64_bundle_ins1 (callsite);
106         instructions [1] = ia64_bundle_ins2 (callsite);
107         instructions [2] = ia64_bundle_ins3 (callsite);
108
109         ia64_codegen_init (gen, (guint8*)buf);
110         ia64_movl (gen, GP_SCRATCH_REG, func);
111         instructions [1] = gen.instructions [0];
112         instructions [2] = gen.instructions [1];
113
114         ia64_codegen_init (gen, (guint8*)buf);
115         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
116         ia64_codegen_close (gen);
117
118         /* This might not be safe, but not all itanium processors support st16 */
119         callsite [0] = buf [0];
120         callsite [1] = buf [1];
121
122         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
123 }
124
125 void
126 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
127 {
128         g_assert_not_reached ();
129 }
130
131 void
132 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
133 {
134         guint8 *callsite_begin;
135         guint64 *callsite = (guint64*)(gpointer)(code - 16);
136         guint64 instructions [3];
137         guint64 buf [16];
138         Ia64CodegenState gen;
139
140         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
141                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
142                 callsite -= 2;
143         callsite_begin = (guint8*)callsite;
144
145         /* Replace the code generated by emit_call with a sets of nops */
146
147         /* The first bundle might have other instructions in it */
148         instructions [0] = ia64_bundle_ins1 (callsite);
149         instructions [1] = IA64_NOP_X;
150         instructions [2] = IA64_NOP_X;
151
152         ia64_codegen_init (gen, (guint8*)buf);
153         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
154         ia64_codegen_close (gen);
155
156         /* This might not be safe, but not all itanium processors support st16 */
157         callsite [0] = buf [0];
158         callsite [1] = buf [1];
159
160         callsite += 2;
161
162         /* The other bundles can be full replaced with nops */
163
164         ia64_codegen_init (gen, (guint8*)buf);
165         ia64_emit_bundle_template (&gen, IA64_TEMPLATE_MII, IA64_NOP_M, IA64_NOP_I, IA64_NOP_I);
166         ia64_codegen_close (gen);
167
168         while ((guint8*)callsite < code) {
169                 callsite [0] = buf [0];
170                 callsite [1] = buf [1];
171                 callsite += 2;
172         }
173
174         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
175 }
176
177 void
178 mono_arch_nullify_plt_entry (guint8 *code)
179 {
180         g_assert_not_reached ();
181 }
182
183 guchar*
184 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
185 {
186         guint8 *buf, *tramp;
187         int i, offset, saved_regs_offset, saved_fpregs_offset, last_offset, framesize;
188         int in0, local0, out0, l0, l1, l2, l3, l4, l5, l6, l7, l8, o0, o1, o2, o3;
189         gboolean has_caller;
190         Ia64CodegenState code;
191         unw_dyn_info_t *di;
192         unw_dyn_region_info_t *r_pro;
193
194         /* 
195          * Since jump trampolines are not patched, this trampoline is executed every
196          * time a call is made to a jump trampoline. So we try to keep things faster
197          * in that case.
198          */
199         if (tramp_type == MONO_TRAMPOLINE_JUMP)
200                 has_caller = FALSE;
201         else
202                 has_caller = TRUE;
203
204         buf = mono_global_codeman_reserve (2048);
205
206         ia64_codegen_init (code, buf);
207
208         /* Stacked Registers */
209         in0 = 32;
210         local0 = in0 + 8;
211         out0 = local0 + 16;
212         l0 = 40;
213         l1 = 41;
214         l2 = 42;
215         l3 = 43;
216         l4 = 44;
217         l5 = 45; /* saved ar.pfs */
218         l6 = 46; /* arg */
219         l7 = 47; /* code */
220         l8 = 48; /* saved sp */
221         o0 = out0 + 0; /* regs */
222         o1 = out0 + 1; /* code */
223         o2 = out0 + 2; /* arg */
224         o3 = out0 + 3; /* tramp */
225
226         framesize = (128 * 8) + 1024;
227         framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
228
229         /*
230          * Allocate a new register+memory stack frame.
231          * 8 input registers (the max used by the ABI)
232          * 16 locals
233          * 4 output (number of parameters passed to trampoline)
234          */
235         ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + l5);
236         ia64_alloc (code, l5, local0 - in0, out0 - local0, 4, 0);
237         ia64_unw_save_reg (code, UNW_IA64_SP, UNW_IA64_GR + l8);
238         ia64_mov (code, l8, IA64_SP);
239         ia64_adds_imm (code, IA64_SP, (-framesize), IA64_SP);
240
241         offset = 16; /* scratch area */
242
243         /* Save the argument received from the specific trampoline */
244         ia64_mov (code, l6, GP_SCRATCH_REG);
245
246         /* Save the calling address */
247         ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 7);
248         ia64_mov_from_br (code, l7, IA64_B0);
249
250         /* Create unwind info for the prolog */
251         ia64_begin_bundle (code);
252         r_pro = mono_ia64_create_unwind_region (&code);
253
254         /* Save registers */
255         /* Not needed for jump trampolines */
256         if (tramp_type != MONO_TRAMPOLINE_JUMP) {
257                 saved_regs_offset = offset;
258                 offset += 128 * 8;
259                 /* 
260                  * Only the registers which are needed for computing vtable slots need
261                  * to be saved.
262                  */
263                 last_offset = -1;
264                 for (i = 0; i < 64; ++i)
265                         if ((1 << i) & MONO_ARCH_CALLEE_REGS) {
266                                 if (last_offset != i * 8)
267                                         ia64_adds_imm (code, l1, saved_regs_offset + (i * 8), IA64_SP);
268                                 ia64_st8_spill_inc_imm_hint (code, l1, i, 8, 0);
269                                 last_offset = (i + 1) * 8;
270                         }
271         }
272
273         /* Save fp registers */
274         saved_fpregs_offset = offset;
275         offset += 8 * 8;
276         ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
277         for (i = 0; i < 8; ++i)
278                 ia64_stfd_inc_imm_hint (code, l1, i + 8, 8, 0);
279
280         g_assert (offset < framesize);
281
282         /* Arg1 is the pointer to the saved registers */
283         ia64_adds_imm (code, o0, saved_regs_offset, IA64_SP);
284
285         /* Arg2 is the address of the calling code */
286         if (has_caller)
287                 ia64_mov (code, o1, l7);
288         else
289                 ia64_mov (code, o1, 0);
290
291         /* Arg3 is the method/vtable ptr */
292         ia64_mov (code, o2, l6);
293
294         /* Arg4 is the trampoline address */
295         /* FIXME: */
296         ia64_mov (code, o3, 0);
297
298         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
299
300         /* Call the trampoline using an indirect call */
301         ia64_movl (code, l0, tramp);
302         ia64_ld8_inc_imm (code, l1, l0, 8);
303         ia64_mov_to_br (code, IA64_B6, l1);
304         ia64_ld8 (code, IA64_GP, l0);
305         ia64_br_call_reg (code, 0, IA64_B6);
306
307         /* Check for thread interruption */
308         /* This is not perf critical code so no need to check the interrupt flag */
309         ia64_mov (code, l2, IA64_R8);
310
311         tramp = (guint8*)mono_thread_interruption_checkpoint;
312         ia64_movl (code, l0, tramp);
313         ia64_ld8_inc_imm (code, l1, l0, 8);
314         ia64_mov_to_br (code, IA64_B6, l1);
315         ia64_ld8 (code, IA64_GP, l0);
316         ia64_br_call_reg (code, 0, IA64_B6);
317
318         ia64_mov (code, IA64_R8, l2);
319
320         /* Restore fp regs */
321         ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
322         for (i = 0; i < 8; ++i)
323                 ia64_ldfd_inc_imm (code, i + 8, l1, 8);
324
325         /* FIXME: Handle NATs in fp regs / scratch regs */
326
327         if (tramp_type != MONO_TRAMPOLINE_CLASS_INIT) {
328                 /* Load method address from function descriptor */
329                 ia64_ld8 (code, l0, IA64_R8);
330                 ia64_mov_to_br (code, IA64_B6, l0);
331         }
332
333         /* Clean up register/memory stack frame */
334         ia64_adds_imm (code, IA64_SP, framesize, IA64_SP);
335         ia64_mov_to_ar_i (code, IA64_PFS, l5);
336
337         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
338                 ia64_mov_ret_to_br (code, IA64_B0, l7);
339                 ia64_br_ret_reg (code, IA64_B0);
340         }
341         else {
342                 /* Call the compiled method */
343                 ia64_mov_to_br (code, IA64_B0, l7);
344                 ia64_br_cond_reg (code, IA64_B6);
345         }
346
347         ia64_codegen_close (code);
348
349         g_assert ((code.buf - buf) <= 2048);
350
351         /* FIXME: emit unwind info for epilog */
352         di = g_malloc0 (sizeof (unw_dyn_info_t));
353         di->start_ip = (unw_word_t) buf;
354         di->end_ip = (unw_word_t) code.buf;
355         di->gp = 0;
356         di->format = UNW_INFO_FORMAT_DYNAMIC;
357         di->u.pi.name_ptr = (unw_word_t)"ia64_generic_trampoline";
358         di->u.pi.regions = r_pro;
359
360         _U_dyn_register (di);
361
362         mono_arch_flush_icache (buf, code.buf - buf);
363
364         return buf;
365 }
366
367 #define TRAMPOLINE_SIZE 128
368
369 gpointer
370 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
371 {
372         guint8 *buf, *tramp;
373         gint64 disp;
374         Ia64CodegenState code;
375
376         tramp = mono_get_trampoline_code (tramp_type);
377
378         mono_domain_lock (domain);
379         buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
380         mono_domain_unlock (domain);
381
382         /* FIXME: Optimize this */
383
384         ia64_codegen_init (code, buf);
385
386         ia64_movl (code, GP_SCRATCH_REG, arg1);
387
388         ia64_begin_bundle (code);
389         disp = (tramp - code.buf) >> 4;
390         if (ia64_is_imm21 (disp)) {
391                 ia64_br_cond (code, disp);
392         }
393         else {
394                 ia64_movl (code, GP_SCRATCH_REG2, tramp);
395                 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
396                 ia64_br_cond_reg (code, IA64_B6);
397         }
398
399         ia64_codegen_close (code);
400
401         g_assert (code.buf - buf <= TRAMPOLINE_SIZE);
402
403         mono_arch_flush_icache (buf, code.buf - buf);
404
405         if (code_len)
406                 *code_len = code.buf - buf;
407
408         return buf;
409 }
410
411 void
412 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
413 {
414         NOT_IMPLEMENTED;
415 }
416
417 gpointer
418 mono_debugger_create_notification_function (void)
419 {
420         NOT_IMPLEMENTED;
421
422         return NULL;
423 }