+kern_return_t
+machine_thread_state_initialize(
+ thread_t thread)
+{
+ /*
+ * If there's an fpu save area, free it.
+ * The initialized state will then be lazily faulted-in, if required.
+ * And if we're target, re-arm the no-fpu trap.
+ */
+ if (thread->machine.ifps) {
+ (void) fpu_set_fxstate(thread, NULL, x86_FLOAT_STATE64);
+
+ if (thread == current_thread())
+ clear_fpu();
+ }
+
+ if (thread->machine.ids) {
+ zfree(ids_zone, thread->machine.ids);
+ thread->machine.ids = NULL;
+ }
+
+ return KERN_SUCCESS;
+}
+
+uint32_t
+get_eflags_exportmask(void)
+{
+ return EFL_USER_SET;
+}
+
+/*
+ * x86_SAVED_STATE32 - internal save/restore general register state on 32/64 bit processors
+ * for 32bit tasks only
+ * x86_SAVED_STATE64 - internal save/restore general register state on 64 bit processors
+ * for 64bit tasks only
+ * x86_THREAD_STATE32 - external set/get general register state on 32/64 bit processors
+ * for 32bit tasks only
+ * x86_THREAD_STATE64 - external set/get general register state on 64 bit processors
+ * for 64bit tasks only
+ * x86_SAVED_STATE - external set/get general register state on 32/64 bit processors
+ * for either 32bit or 64bit tasks
+ * x86_FLOAT_STATE32 - internal/external save/restore float and xmm state on 32/64 bit processors
+ * for 32bit tasks only
+ * x86_FLOAT_STATE64 - internal/external save/restore float and xmm state on 64 bit processors
+ * for 64bit tasks only
+ * x86_FLOAT_STATE - external save/restore float and xmm state on 32/64 bit processors
+ * for either 32bit or 64bit tasks
+ * x86_EXCEPTION_STATE32 - external get exception state on 32/64 bit processors
+ * for 32bit tasks only
+ * x86_EXCEPTION_STATE64 - external get exception state on 64 bit processors
+ * for 64bit tasks only
+ * x86_EXCEPTION_STATE - external get exception state on 323/64 bit processors
+ * for either 32bit or 64bit tasks
+ */
+
+
+static void
+get_exception_state64(thread_t thread, x86_exception_state64_t *es)
+{
+ x86_saved_state64_t *saved_state;
+
+ saved_state = USER_REGS64(thread);
+
+ es->trapno = saved_state->isf.trapno;
+ es->cpu = saved_state->isf.cpu;
+ es->err = (typeof(es->err))saved_state->isf.err;
+ es->faultvaddr = saved_state->cr2;
+}
+
+static void
+get_exception_state32(thread_t thread, x86_exception_state32_t *es)
+{
+ x86_saved_state32_t *saved_state;
+
+ saved_state = USER_REGS32(thread);
+
+ es->trapno = saved_state->trapno;
+ es->cpu = saved_state->cpu;
+ es->err = saved_state->err;
+ es->faultvaddr = saved_state->cr2;
+}
+
+
+static int
+set_thread_state32(thread_t thread, x86_thread_state32_t *ts)
+{
+ x86_saved_state32_t *saved_state;
+
+ pal_register_cache_state(thread, DIRTY);
+
+ saved_state = USER_REGS32(thread);
+
+ /*
+ * Scrub segment selector values:
+ */
+ ts->cs = USER_CS;
+ /*
+ * On a 64 bit kernel, we always override the data segments,
+ * as the actual selector numbers have changed. This also
+ * means that we don't support setting the data segments
+ * manually any more.
+ */
+ ts->ss = USER_DS;
+ ts->ds = USER_DS;
+ ts->es = USER_DS;
+
+ /* Set GS to CTHREAD only if's been established */
+ ts->gs = thread->machine.cthread_self ? USER_CTHREAD : NULL_SEG;
+
+ /* Check segment selectors are safe */
+ if (!valid_user_segment_selectors(ts->cs,
+ ts->ss,
+ ts->ds,
+ ts->es,
+ ts->fs,
+ ts->gs))
+ return(KERN_INVALID_ARGUMENT);
+
+ saved_state->eax = ts->eax;
+ saved_state->ebx = ts->ebx;
+ saved_state->ecx = ts->ecx;
+ saved_state->edx = ts->edx;
+ saved_state->edi = ts->edi;
+ saved_state->esi = ts->esi;
+ saved_state->ebp = ts->ebp;
+ saved_state->uesp = ts->esp;
+ saved_state->efl = (ts->eflags & ~EFL_USER_CLEAR) | EFL_USER_SET;
+ saved_state->eip = ts->eip;
+ saved_state->cs = ts->cs;
+ saved_state->ss = ts->ss;
+ saved_state->ds = ts->ds;
+ saved_state->es = ts->es;
+ saved_state->fs = ts->fs;
+ saved_state->gs = ts->gs;
+
+ /*
+ * If the trace trap bit is being set,
+ * ensure that the user returns via iret
+ * - which is signaled thusly:
+ */
+ if ((saved_state->efl & EFL_TF) && saved_state->cs == SYSENTER_CS)
+ saved_state->cs = SYSENTER_TF_CS;
+
+ return(KERN_SUCCESS);
+}
+
+static int
+set_thread_state64(thread_t thread, x86_thread_state64_t *ts)
+{
+ x86_saved_state64_t *saved_state;
+
+ pal_register_cache_state(thread, DIRTY);
+
+ saved_state = USER_REGS64(thread);
+
+ if (!IS_USERADDR64_CANONICAL(ts->rsp) ||
+ !IS_USERADDR64_CANONICAL(ts->rip))
+ return(KERN_INVALID_ARGUMENT);
+
+ saved_state->r8 = ts->r8;
+ saved_state->r9 = ts->r9;
+ saved_state->r10 = ts->r10;
+ saved_state->r11 = ts->r11;
+ saved_state->r12 = ts->r12;
+ saved_state->r13 = ts->r13;
+ saved_state->r14 = ts->r14;
+ saved_state->r15 = ts->r15;
+ saved_state->rax = ts->rax;
+ saved_state->rbx = ts->rbx;
+ saved_state->rcx = ts->rcx;
+ saved_state->rdx = ts->rdx;
+ saved_state->rdi = ts->rdi;
+ saved_state->rsi = ts->rsi;
+ saved_state->rbp = ts->rbp;
+ saved_state->isf.rsp = ts->rsp;
+ saved_state->isf.rflags = (ts->rflags & ~EFL_USER_CLEAR) | EFL_USER_SET;
+ saved_state->isf.rip = ts->rip;
+ saved_state->isf.cs = USER64_CS;
+ saved_state->fs = (uint32_t)ts->fs;
+ saved_state->gs = (uint32_t)ts->gs;
+
+ return(KERN_SUCCESS);
+}
+
+
+
+static void
+get_thread_state32(thread_t thread, x86_thread_state32_t *ts)