+ *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
+}
+
+GSList*
+mono_amd64_get_exception_trampolines (gboolean aot)
+{
+ MonoTrampInfo *info;
+ GSList *tramps = NULL;
+
+ mono_arch_get_throw_pending_exception (&info, aot);
+ tramps = g_slist_prepend (tramps, info);
+
+ /* LLVM needs different throw trampolines */
+ get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
+ tramps = g_slist_prepend (tramps, info);
+
+ get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
+ tramps = g_slist_prepend (tramps, info);
+
+ get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
+ tramps = g_slist_prepend (tramps, info);
+
+ return tramps;
+}
+
+void
+mono_arch_exceptions_init (void)
+{
+ GSList *tramps, *l;
+ gpointer tramp;
+
+ if (mono_aot_only) {
+ throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
+ tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
+ mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
+ tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
+ mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
+ tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
+ mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
+ } else {
+ /* Call this to avoid initialization races */
+ throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
+
+ tramps = mono_amd64_get_exception_trampolines (FALSE);
+ for (l = tramps; l; l = l->next) {
+ MonoTrampInfo *info = l->data;
+
+ mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
+ mono_save_trampoline_xdebug_info (info);
+ mono_tramp_info_free (info);
+ }
+ g_slist_free (tramps);
+ }
+}
+
+#ifdef TARGET_WIN32
+
+/*
+ * The mono_arch_unwindinfo* methods are used to build and add
+ * function table info for each emitted method from mono. On Winx64
+ * the seh handler will not be called if the mono methods are not
+ * added to the function table.
+ *
+ * We should not need to add non-volatile register info to the
+ * table since mono stores that info elsewhere. (Except for the register
+ * used for the fp.)
+ */
+
+#define MONO_MAX_UNWIND_CODES 22
+
+typedef union _UNWIND_CODE {
+ struct {
+ guchar CodeOffset;
+ guchar UnwindOp : 4;
+ guchar OpInfo : 4;
+ };
+ gushort FrameOffset;
+} UNWIND_CODE, *PUNWIND_CODE;
+
+typedef struct _UNWIND_INFO {
+ guchar Version : 3;
+ guchar Flags : 5;
+ guchar SizeOfProlog;
+ guchar CountOfCodes;
+ guchar FrameRegister : 4;
+ guchar FrameOffset : 4;
+ /* custom size for mono allowing for mono allowing for*/
+ /*UWOP_PUSH_NONVOL ebp offset = 21*/
+ /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
+ /*UWOP_SET_FPREG : requires 2 offset = 17*/
+ /*UWOP_PUSH_NONVOL offset = 15-0*/
+ UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
+
+/* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
+ * union {
+ * OPTIONAL ULONG ExceptionHandler;
+ * OPTIONAL ULONG FunctionEntry;
+ * };
+ * OPTIONAL ULONG ExceptionData[]; */
+} UNWIND_INFO, *PUNWIND_INFO;
+
+typedef struct
+{
+ RUNTIME_FUNCTION runtimeFunction;
+ UNWIND_INFO unwindInfo;
+} MonoUnwindInfo, *PMonoUnwindInfo;
+
+static void
+mono_arch_unwindinfo_create (gpointer* monoui)
+{
+ PMonoUnwindInfo newunwindinfo;
+ *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
+ newunwindinfo->unwindInfo.Version = 1;
+}
+
+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 */
+ const guint kMaxCodeSize = NACL_SIZE (64, 128);
+
+
+ if (saved)
+ return (MonoContinuationRestore)saved;
+ code = start = mono_global_codeman_reserve (kMaxCodeSize);
+ /* 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);
+#if !defined(__native_client_codegen__)
+ amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#endif
+#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) <= kMaxCodeSize);
+
+ nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+
+ saved = start;
+ return (MonoContinuationRestore)saved;
+}
+#endif
+
+/*
+ * mono_arch_setup_resume_sighandler_ctx:
+ *
+ * Setup CTX so execution continues at FUNC.
+ */
+void
+mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
+{
+ /*
+ * When resuming from a signal handler, the stack should be misaligned, just like right after
+ * a call.
+ */
+ if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
+ MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
+ MONO_CONTEXT_SET_IP (ctx, func);