2 * mini-amd64-gsharedvt.c: libcorkscrew-based native unwinder
5 * Zoltan Varga <vargaz@gmail.com>
6 * Rodrigo Kumpera <kumpera@gmail.com>
7 * Andi McClure <andi.mcclure@xamarin.com>
8 * Johan Lorensson <johan.lorensson@xamarin.com>
10 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/metadata/abi-details.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/profiler-private.h>
21 #include <mono/metadata/gc-internals.h>
22 #include <mono/arch/amd64/amd64-codegen.h>
24 #include <mono/utils/memcheck.h>
27 #include "mini-amd64.h"
28 #include "mini-amd64-gsharedvt.h"
29 #include "debugger-agent.h"
31 #if defined (MONO_ARCH_GSHAREDVT_SUPPORTED)
33 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
36 mono_arch_gsharedvt_sig_supported (MonoMethodSignature *sig)
42 storage_name (ArgStorage st)
45 case ArgInIReg: return "ArgInIReg";
46 case ArgInFloatSSEReg: return "ArgInFloatSSEReg";
47 case ArgInDoubleSSEReg: return "ArgInDoubleSSEReg";
48 case ArgOnStack: return "ArgOnStack";
49 case ArgValuetypeInReg: return "ArgValuetypeInReg";
50 case ArgValuetypeAddrInIReg: return "ArgValuetypeAddrInIReg";
51 case ArgValuetypeAddrOnStack: return "ArgValuetypeAddrOnStack";
52 case ArgGSharedVtInReg: return "ArgGSharedVtInReg";
53 case ArgGSharedVtOnStack: return "ArgGSharedVtOnStack";
54 case ArgNone: return "ArgNone";
55 default: return "unknown";
60 arg_info_desc (ArgInfo *info)
62 GString *str = g_string_new ("");
64 g_string_append_printf (str, "offset %d reg %s storage %s nregs %d", info->offset, mono_arch_regname (info->reg), storage_name (info->storage), info->nregs);
65 if (info->storage == ArgValuetypeInReg)
66 g_string_append_printf (str, " {(%s %s), (%s %s)",
67 storage_name (info->pair_storage [0]),
68 mono_arch_regname (info->pair_regs [0]),
69 storage_name (info->pair_storage [1]),
70 mono_arch_regname (info->pair_regs [1]));
72 return g_string_free (str, FALSE);
76 add_to_map (GPtrArray *map, int src, int dst)
78 g_ptr_array_add (map, GUINT_TO_POINTER (src));
79 g_ptr_array_add (map, GUINT_TO_POINTER (dst));
86 * 0..5 - rdi, rsi, rdx, rcx, r8, r9
91 * 0..3 - rcx, rdx, r8, r9
100 for (i = 0; i < PARAM_REGS; ++i) {
101 if (param_regs [i] == reg)
104 g_error ("Invalid argument register number %d", reg);
111 return reg + PARAM_REGS;
115 map_stack_slot (int slot)
117 return slot + PARAM_REGS + FLOAT_PARAM_REGS;
121 Format for the source descriptor:
124 Format for the destination descriptor:
125 bits 0:15 - source register
126 bits 16:23 - return marshal
127 bits 24:32 - slot count
129 #define SRC_DESCRIPTOR_MARSHAL_SHIFT 16
130 #define SRC_DESCRIPTOR_MARSHAL_MASK 0x0Ff
132 #define SLOT_COUNT_SHIFT 24
133 #define SLOT_COUNT_MASK 0xff
134 #define SLOT_BYTE_SIZE 8
137 get_arg_slots (ArgInfo *ainfo, int **out_slots, gboolean is_source_argument)
139 int sreg = ainfo->reg;
140 int sslot = ainfo->offset / 8;
144 switch (ainfo->storage) {
147 src = g_malloc (nsrc * sizeof (int));
148 src [0] = map_reg (sreg);
150 case ArgValuetypeInReg:
152 src = g_malloc (nsrc * sizeof (int));
153 for (i = 0; i < ainfo->nregs; ++i)
154 src [i] = map_reg (ainfo->pair_regs [i]);
157 nsrc = ainfo->arg_size / SLOT_BYTE_SIZE;
158 src = g_malloc (nsrc * sizeof (int));
159 // is_source_argument adds 2 because we're skipping over the old BBP and the return address
160 // XXX this is a very fragile setup as changes in alignment for the caller reg array can cause the magic number be 3
161 for (i = 0; i < nsrc; ++i)
162 src [i] = map_stack_slot (sslot + i + (is_source_argument ? 2 : 0));
164 case ArgInDoubleSSEReg:
165 case ArgInFloatSSEReg:
167 src = g_malloc (nsrc * sizeof (int));
168 src [0] = map_freg (sreg);
170 case ArgValuetypeAddrInIReg:
172 src = g_malloc (nsrc * sizeof (int));
173 src [0] = map_reg (ainfo->pair_regs [0]);
175 case ArgValuetypeAddrOnStack:
177 src = g_malloc (nsrc * sizeof (int));
178 // is_source_argument adds 2 because we're skipping over the old BBP and the return address
179 // XXX this is a very fragile setup as changes in alignment for the caller reg array can cause the magic number be 3
180 src [0] = map_stack_slot (sslot + (is_source_argument ? 2 : 0));
191 // Once src is known, operate on the dst
193 handle_marshal_when_src_gsharedvt (ArgInfo *dst_info, int *arg_marshal, int *arg_slots)
195 switch (dst_info->storage) {
197 case ArgInDoubleSSEReg:
198 case ArgInFloatSSEReg:
199 *arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL;
203 *arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL;
204 g_assert (dst_info->arg_size % SLOT_BYTE_SIZE == 0); // Assert quadword aligned
205 *arg_slots = dst_info->arg_size / SLOT_BYTE_SIZE;
207 case ArgValuetypeInReg:
208 *arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL;
209 *arg_slots = dst_info->nregs;
211 case ArgValuetypeAddrInIReg:
212 case ArgValuetypeAddrOnStack:
213 *arg_marshal = GSHAREDVT_ARG_NONE;
214 *arg_slots = dst_info->nregs;
217 NOT_IMPLEMENTED; // Inappropriate value: if dst and src are gsharedvt at once, we shouldn't be here
222 // Once dst is known, operate on the src
224 handle_marshal_when_dst_gsharedvt (ArgInfo *src_info, int *arg_marshal)
226 switch (src_info->storage) {
228 case ArgInDoubleSSEReg:
229 case ArgInFloatSSEReg:
230 case ArgValuetypeInReg:
232 *arg_marshal = GSHAREDVT_ARG_BYVAL_TO_BYREF;
234 case ArgValuetypeAddrInIReg:
235 case ArgValuetypeAddrOnStack:
236 *arg_marshal = GSHAREDVT_ARG_NONE;
239 NOT_IMPLEMENTED; // See above
245 handle_map_when_gsharedvt_in_reg (ArgInfo *reg_info, int *n, int **map)
248 *map = g_new0 (int, 1);
249 (*map) [0] = map_reg (reg_info->reg);
253 handle_map_when_gsharedvt_on_stack (ArgInfo *reg_info, int *n, int **map, gboolean is_source_argument)
256 *map = g_new0 (int, 1);
257 int sslot = reg_info->offset / SLOT_BYTE_SIZE;
258 (*map) [0] = map_stack_slot (sslot + (is_source_argument ? 2 : 0)); // see get_arg_slots
262 mono_arch_get_gsharedvt_call_info (gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig, gboolean gsharedvt_in, gint32 vcall_offset, gboolean calli)
264 GSharedVtCallInfo *info;
265 CallInfo *caller_cinfo, *callee_cinfo;
266 MonoMethodSignature *caller_sig, *callee_sig;
268 gboolean var_ret = FALSE;
269 CallInfo *cinfo, *gcinfo;
270 MonoMethodSignature *sig, *gsig;
274 caller_sig = normal_sig;
275 callee_sig = gsharedvt_sig;
276 caller_cinfo = mono_arch_get_call_info (NULL, caller_sig);
277 callee_cinfo = mono_arch_get_call_info (NULL, callee_sig);
279 callee_sig = normal_sig;
280 caller_sig = gsharedvt_sig;
281 callee_cinfo = mono_arch_get_call_info (NULL, callee_sig);
282 caller_cinfo = mono_arch_get_call_info (NULL, caller_sig);
286 * If GSHAREDVT_IN is true, this means we are transitioning from normal to gsharedvt code. The caller uses the
287 * normal call signature, while the callee uses the gsharedvt signature.
288 * If GSHAREDVT_IN is false, its the other way around.
291 /* sig/cinfo describes the normal call, while gsig/gcinfo describes the gsharedvt call */
295 cinfo = caller_cinfo;
296 gcinfo = callee_cinfo;
300 cinfo = callee_cinfo;
301 gcinfo = caller_cinfo;
304 DEBUG_AMD64_GSHAREDVT_PRINT ("source sig: (%s) return (%s)\n", mono_signature_get_desc (caller_sig, FALSE), mono_type_full_name (mono_signature_get_return_type (caller_sig))); // Leak
305 DEBUG_AMD64_GSHAREDVT_PRINT ("dest sig: (%s) return (%s)\n", mono_signature_get_desc (callee_sig, FALSE), mono_type_full_name (mono_signature_get_return_type (callee_sig)));
307 if (gcinfo->ret.storage == ArgGsharedvtVariableInReg) {
309 * The return type is gsharedvt
315 * The stack looks like this:
319 * We have to map the stack slots in <arguments> to the stack slots in <call area>.
321 map = g_ptr_array_new ();
323 for (aindex = 0; aindex < cinfo->nargs; ++aindex) {
324 ArgInfo *src_info = &caller_cinfo->args [aindex];
325 ArgInfo *dst_info = &callee_cinfo->args [aindex];
326 int *src = NULL, *dst = NULL;
327 int nsrc = -1, ndst = -1, nslots = 0;
329 int arg_marshal = GSHAREDVT_ARG_NONE;
330 int arg_slots = 0; // Size in quadwords
331 DEBUG_AMD64_GSHAREDVT_PRINT ("-- arg %d in (%s) out (%s)\n", aindex, arg_info_desc (src_info), arg_info_desc (dst_info));
333 switch (src_info->storage) {
335 case ArgInDoubleSSEReg:
336 case ArgInFloatSSEReg:
337 case ArgValuetypeInReg:
339 nsrc = get_arg_slots (src_info, &src, TRUE);
341 case ArgGSharedVtInReg:
342 handle_marshal_when_src_gsharedvt (dst_info, &arg_marshal, &arg_slots);
343 handle_map_when_gsharedvt_in_reg (src_info, &nsrc, &src);
345 case ArgGSharedVtOnStack:
346 handle_marshal_when_src_gsharedvt (dst_info, &arg_marshal, &arg_slots);
347 handle_map_when_gsharedvt_on_stack (src_info, &nsrc, &src, TRUE);
349 case ArgValuetypeAddrInIReg:
350 case ArgValuetypeAddrOnStack:
351 nsrc = get_arg_slots (src_info, &src, TRUE);
354 g_error ("Gsharedvt can't handle source arg type %d", (int)src_info->storage); // Inappropriate value: ArgValuetypeAddrInIReg is for returns only
357 switch (dst_info->storage) {
359 case ArgInDoubleSSEReg:
360 case ArgInFloatSSEReg:
362 case ArgValuetypeInReg:
363 ndst = get_arg_slots (dst_info, &dst, FALSE);
365 case ArgGSharedVtInReg:
366 handle_marshal_when_dst_gsharedvt (src_info, &arg_marshal);
367 handle_map_when_gsharedvt_in_reg (dst_info, &ndst, &dst);
369 case ArgGSharedVtOnStack:
370 handle_marshal_when_dst_gsharedvt (src_info, &arg_marshal);
371 handle_map_when_gsharedvt_on_stack (dst_info, &ndst, &dst, FALSE);
373 case ArgValuetypeAddrInIReg:
374 case ArgValuetypeAddrOnStack:
375 ndst = get_arg_slots (dst_info, &dst, FALSE);
378 g_error ("Gsharedvt can't handle dest arg type %d", (int)dst_info->storage); // See above
381 src [0] |= (arg_marshal << SRC_DESCRIPTOR_MARSHAL_SHIFT) | (arg_slots << SLOT_COUNT_SHIFT);
383 /* Merge and add to the global list*/
384 nslots = MIN (nsrc, ndst);
385 DEBUG_AMD64_GSHAREDVT_PRINT ("nsrc %d ndst %d\n", nsrc, ndst);
387 for (i = 0; i < nslots; ++i)
388 add_to_map (map, src [i], dst [i]);
394 DEBUG_AMD64_GSHAREDVT_PRINT ("-- return in (%s) out (%s) var_ret %d\n", arg_info_desc (&caller_cinfo->ret), arg_info_desc (&callee_cinfo->ret), var_ret);
396 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
397 /* Both the caller and the callee pass the vtype ret address in r8 (System V) and RCX or RDX (Windows) */
398 g_assert (gcinfo->ret.storage == ArgValuetypeAddrInIReg || gcinfo->ret.storage == ArgGsharedvtVariableInReg);
399 add_to_map (map, map_reg (cinfo->ret.reg), map_reg (cinfo->ret.reg));
402 info = mono_domain_alloc0 (mono_domain_get (), sizeof (GSharedVtCallInfo) + (map->len * sizeof (int)));
404 info->stack_usage = callee_cinfo->stack_usage;
405 info->ret_marshal = GSHAREDVT_RET_NONE;
406 info->gsharedvt_in = gsharedvt_in ? 1 : 0;
407 info->vret_slot = -1;
411 g_assert (gcinfo->ret.storage == ArgGsharedvtVariableInReg);
412 info->vret_arg_reg = map_reg (gcinfo->ret.reg);
413 DEBUG_AMD64_GSHAREDVT_PRINT ("mapping vreg_arg_reg to %d in reg %s\n", info->vret_arg_reg, mono_arch_regname (gcinfo->ret.reg));
415 info->vret_arg_reg = -1;
418 #ifdef DEBUG_AMD64_GSHAREDVT
419 printf ("final map:\n");
420 for (i = 0; i < map->len; i += 2) {
421 printf ("\t[%d] src %x dst %x\n ",
423 GPOINTER_TO_UINT (g_ptr_array_index (map, i)),
424 GPOINTER_TO_UINT (g_ptr_array_index (map, i + 1)));
428 info->vcall_offset = vcall_offset;
429 info->map_count = map->len / 2;
430 for (i = 0; i < map->len; ++i)
431 info->map [i] = GPOINTER_TO_UINT (g_ptr_array_index (map, i));
432 g_ptr_array_free (map, TRUE);
434 /* Compute return value marshalling */
436 /* Compute return value marshalling */
437 switch (cinfo->ret.storage) {
439 if (!gsharedvt_in || sig->ret->byref) {
440 info->ret_marshal = GSHAREDVT_RET_IREGS_1;
442 MonoType *ret = sig->ret;
445 if (ret->type == MONO_TYPE_VALUETYPE)
446 ret = mini_type_get_underlying_type (ret);
450 info->ret_marshal = GSHAREDVT_RET_I1;
452 case MONO_TYPE_BOOLEAN:
454 info->ret_marshal = GSHAREDVT_RET_U1;
457 info->ret_marshal = GSHAREDVT_RET_I2;
461 info->ret_marshal = GSHAREDVT_RET_U2;
464 info->ret_marshal = GSHAREDVT_RET_I4;
467 info->ret_marshal = GSHAREDVT_RET_U4;
472 case MONO_TYPE_FNPTR:
473 case MONO_TYPE_CLASS:
474 case MONO_TYPE_OBJECT:
475 case MONO_TYPE_SZARRAY:
476 case MONO_TYPE_ARRAY:
477 case MONO_TYPE_STRING:
480 info->ret_marshal = GSHAREDVT_RET_I8;
482 case MONO_TYPE_GENERICINST:
483 g_assert (!mono_type_generic_inst_is_valuetype (ret));
484 info->ret_marshal = GSHAREDVT_RET_I8;
487 g_error ("Gsharedvt can't handle dst type [%d]", (int)sig->ret->type);
491 case ArgValuetypeInReg:
492 info->ret_marshal = GSHAREDVT_RET_IREGS_1 - 1 + cinfo->ret.nregs;
493 g_assert (cinfo->ret.nregs == 1); // ABI supports 2-register return but we do not implement this.
495 case ArgInDoubleSSEReg:
496 case ArgInFloatSSEReg:
497 info->ret_marshal = GSHAREDVT_RET_R8;
499 case ArgValuetypeAddrInIReg:
502 g_error ("Can't marshal return of storage [%d] %s", (int)cinfo->ret.storage, storage_name (cinfo->ret.storage));
505 if (gsharedvt_in && cinfo->ret.storage != ArgValuetypeAddrInIReg) {
506 /* Allocate stack space for the return value */
507 info->vret_slot = map_stack_slot (info->stack_usage / sizeof (gpointer));
508 info->stack_usage += mono_type_stack_size_internal (normal_sig->ret, NULL, FALSE) + sizeof (gpointer);
510 DEBUG_AMD64_GSHAREDVT_PRINT ("RET marshal is %s\n", ret_marshal_name [info->ret_marshal]);
513 info->stack_usage = ALIGN_TO (info->stack_usage, MONO_ARCH_FRAME_ALIGNMENT);
515 g_free (callee_cinfo);
516 g_free (caller_cinfo);
518 DEBUG_AMD64_GSHAREDVT_PRINT ("allocated an info at %p stack usage %d\n", info, info->stack_usage);