+
+void
+mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
+{
+ PMonoUnwindInfo unwindinfo;
+ PUNWIND_CODE unwindcode;
+ guchar codeindex;
+ if (!*monoui)
+ mono_arch_unwindinfo_create (monoui);
+
+ unwindinfo = (MonoUnwindInfo*)*monoui;
+
+ if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
+ g_error ("Larger allocation needed for the unwind information.");
+
+ codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
+ unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
+ unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
+ unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
+ unwindcode->OpInfo = reg;
+
+ if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
+ g_error ("Adding unwind info in wrong order.");
+
+ unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
+}
+
+void
+mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
+{
+ PMonoUnwindInfo unwindinfo;
+ PUNWIND_CODE unwindcode;
+ guchar codeindex;
+ if (!*monoui)
+ mono_arch_unwindinfo_create (monoui);
+
+ unwindinfo = (MonoUnwindInfo*)*monoui;
+
+ if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
+ g_error ("Larger allocation needed for the unwind information.");
+
+ codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
+ unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
+ unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
+ unwindcode++;
+ unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
+ unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
+ unwindcode->OpInfo = reg;
+
+ unwindinfo->unwindInfo.FrameRegister = reg;
+
+ if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
+ g_error ("Adding unwind info in wrong order.");
+
+ unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
+}
+
+void
+mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
+{
+ PMonoUnwindInfo unwindinfo;
+ PUNWIND_CODE unwindcode;
+ guchar codeindex;
+ guchar codesneeded;
+ if (!*monoui)
+ mono_arch_unwindinfo_create (monoui);
+
+ unwindinfo = (MonoUnwindInfo*)*monoui;
+
+ if (size < 0x8)
+ g_error ("Stack allocation must be equal to or greater than 0x8.");
+
+ if (size <= 0x80)
+ codesneeded = 1;
+ else if (size <= 0x7FFF8)
+ codesneeded = 2;
+ else
+ codesneeded = 3;
+
+ if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
+ g_error ("Larger allocation needed for the unwind information.");
+
+ codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
+ unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
+
+ if (codesneeded == 1) {
+ /*The size of the allocation is
+ (the number in the OpInfo member) times 8 plus 8*/
+ unwindcode->OpInfo = (size - 8)/8;
+ unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
+ }
+ else {
+ if (codesneeded == 3) {
+ /*the unscaled size of the allocation is recorded
+ in the next two slots in little-endian format*/
+ *((unsigned int*)(&unwindcode->FrameOffset)) = size;
+ unwindcode += 2;
+ unwindcode->OpInfo = 1;
+ }
+ else {
+ /*the size of the allocation divided by 8
+ is recorded in the next slot*/
+ unwindcode->FrameOffset = size/8;
+ unwindcode++;
+ unwindcode->OpInfo = 0;
+
+ }
+ unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
+ }
+
+ unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
+
+ if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
+ g_error ("Adding unwind info in wrong order.");
+
+ unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
+}
+
+guint
+mono_arch_unwindinfo_get_size (gpointer monoui)
+{
+ PMonoUnwindInfo unwindinfo;
+ if (!monoui)
+ return 0;
+
+ unwindinfo = (MonoUnwindInfo*)monoui;
+ return (8 + sizeof (MonoUnwindInfo)) -
+ (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
+}
+
+PRUNTIME_FUNCTION
+MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
+{
+ MonoJitInfo *ji;
+ guint64 pos;
+ PMonoUnwindInfo targetinfo;
+ MonoDomain *domain = mono_domain_get ();
+
+ ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
+ if (!ji)
+ return 0;
+
+ pos = (guint64)(((char*)ji->code_start) + ji->code_size);
+
+ targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
+
+ targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
+
+ return &targetinfo->runtimeFunction;
+}
+
+void
+mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
+{
+ PMonoUnwindInfo unwindinfo, targetinfo;
+ guchar codecount;
+ guint64 targetlocation;
+ if (!*monoui)
+ return;
+
+ unwindinfo = (MonoUnwindInfo*)*monoui;
+ targetlocation = (guint64)&(((guchar*)code)[code_size]);
+ targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
+
+ unwindinfo->runtimeFunction.EndAddress = code_size;
+ unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
+
+ memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
+
+ codecount = unwindinfo->unwindInfo.CountOfCodes;
+ if (codecount) {
+ memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
+ sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
+ }
+
+ g_free (unwindinfo);
+ *monoui = 0;
+
+ RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
+}
+
+#endif
+
+#if MONO_SUPPORT_TASKLETS
+MonoContinuationRestore
+mono_tasklets_arch_restore (void)
+{
+ static guint8* saved = NULL;
+ guint8 *code, *start;
+ int cont_reg = AMD64_R9; /* register usable on both call conventions */
+
+ if (saved)
+ return (MonoContinuationRestore)saved;
+ code = start = mono_global_codeman_reserve (64);
+ /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
+ /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
+ * state is in AMD64_ARG_REG2 ($rdx or $rsi)
+ * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
+ * We move cont to cont_reg since we need both rcx and rdi for the copy
+ * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
+ */
+ amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
+ amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
+ /* setup the copy of the stack */
+ amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
+ amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
+ x86_cld (code);
+ amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
+ amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
+ amd64_prefix (code, X86_REP_PREFIX);
+ amd64_movsl (code);
+
+ /* now restore the registers from the LMF */
+ amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
+ amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
+ amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
+ amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
+ amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
+ amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
+ amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#ifdef TARGET_WIN32
+ amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
+ amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
+#endif
+ amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
+
+ /* restore the lmf chain */
+ /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
+ x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
+
+ /* state is already in rax */
+ amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
+ g_assert ((code - start) <= 64);
+ saved = start;
+ return (MonoContinuationRestore)saved;
+}
+#endif
+