X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/6d2010ae8f7a6078e10b361c6962983bab233e0f..cb3231590a3c94ab4375e2228bd5e86b0cf1ad7e:/osfmk/i386/pcb.c diff --git a/osfmk/i386/pcb.c b/osfmk/i386/pcb.c index caf0c68db..9ece881bd 100644 --- a/osfmk/i386/pcb.c +++ b/osfmk/i386/pcb.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2000-2010 Apple Inc. All rights reserved. + * Copyright (c) 2000-2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -11,10 +11,10 @@ * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. - * + * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -22,39 +22,38 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ -/* +/* * Mach Operating System * Copyright (c) 1991,1990 Carnegie Mellon University * All Rights Reserved. - * + * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. - * + * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. - * + * * Carnegie Mellon requests users of this software to return to - * + * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 - * + * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ -#include #include #include @@ -77,6 +76,7 @@ #include #include #include +#include #include #include #include @@ -91,47 +91,48 @@ #include #include #include -#if defined(__i386__) -#include -#endif #include #include /* LAPIC_PMC_SWI_VECTOR */ +#include -#if CONFIG_COUNTERS -#include -#endif /* CONFIG_COUNTERS */ +#if HYPERVISOR +#include +#endif /* * Maps state flavor to number of words in the state: */ unsigned int _MachineStateCount[] = { - /* FLAVOR_LIST */ - 0, - x86_THREAD_STATE32_COUNT, - x86_FLOAT_STATE32_COUNT, - x86_EXCEPTION_STATE32_COUNT, - x86_THREAD_STATE64_COUNT, - x86_FLOAT_STATE64_COUNT, - x86_EXCEPTION_STATE64_COUNT, - x86_THREAD_STATE_COUNT, - x86_FLOAT_STATE_COUNT, - x86_EXCEPTION_STATE_COUNT, - 0, - x86_SAVED_STATE32_COUNT, - x86_SAVED_STATE64_COUNT, - x86_DEBUG_STATE32_COUNT, - x86_DEBUG_STATE64_COUNT, - x86_DEBUG_STATE_COUNT + [x86_THREAD_STATE32] = x86_THREAD_STATE32_COUNT, + [x86_THREAD_STATE64] = x86_THREAD_STATE64_COUNT, + [x86_THREAD_FULL_STATE64] = x86_THREAD_FULL_STATE64_COUNT, + [x86_THREAD_STATE] = x86_THREAD_STATE_COUNT, + [x86_FLOAT_STATE32] = x86_FLOAT_STATE32_COUNT, + [x86_FLOAT_STATE64] = x86_FLOAT_STATE64_COUNT, + [x86_FLOAT_STATE] = x86_FLOAT_STATE_COUNT, + [x86_EXCEPTION_STATE32] = x86_EXCEPTION_STATE32_COUNT, + [x86_EXCEPTION_STATE64] = x86_EXCEPTION_STATE64_COUNT, + [x86_EXCEPTION_STATE] = x86_EXCEPTION_STATE_COUNT, + [x86_DEBUG_STATE32] = x86_DEBUG_STATE32_COUNT, + [x86_DEBUG_STATE64] = x86_DEBUG_STATE64_COUNT, + [x86_DEBUG_STATE] = x86_DEBUG_STATE_COUNT, + [x86_AVX_STATE32] = x86_AVX_STATE32_COUNT, + [x86_AVX_STATE64] = x86_AVX_STATE64_COUNT, + [x86_AVX_STATE] = x86_AVX_STATE_COUNT, + [x86_AVX512_STATE32] = x86_AVX512_STATE32_COUNT, + [x86_AVX512_STATE64] = x86_AVX512_STATE64_COUNT, + [x86_AVX512_STATE] = x86_AVX512_STATE_COUNT, + [x86_PAGEIN_STATE] = x86_PAGEIN_STATE_COUNT }; -zone_t iss_zone; /* zone for saved_state area */ -zone_t ids_zone; /* zone for debug_state area */ +zone_t iss_zone; /* zone for saved_state area */ +zone_t ids_zone; /* zone for debug_state area */ /* Forward */ -extern void Thread_continue(void); -extern void Load_context( - thread_t thread); +extern void Thread_continue(void); +extern void Load_context( + thread_t thread) __attribute__((noreturn)); static void get_exception_state32(thread_t thread, x86_exception_state32_t *es); @@ -143,65 +144,36 @@ static void get_thread_state32(thread_t thread, x86_thread_state32_t *ts); static void -get_thread_state64(thread_t thread, x86_thread_state64_t *ts); +get_thread_state64(thread_t thread, void *ts, boolean_t full); static int set_thread_state32(thread_t thread, x86_thread_state32_t *ts); static int -set_thread_state64(thread_t thread, x86_thread_state64_t *ts); - -#if CONFIG_COUNTERS -static inline void -machine_pmc_cswitch(thread_t /* old */, thread_t /* new */); - -static inline boolean_t -machine_thread_pmc_eligible(thread_t); +set_thread_state64(thread_t thread, void *ts, boolean_t full); +#if HYPERVISOR static inline void -pmc_swi(thread_t /* old */, thread_t /*new */); - -static inline boolean_t -machine_thread_pmc_eligible(thread_t t) { - /* - * NOTE: Task-level reservations are propagated to child threads via - * thread_create_internal. Any mutation of task reservations forces a - * recalculate of t_chud (for the pmc flag) for all threads in that task. - * Consequently, we can simply check the current thread's flag against - * THREAD_PMC_FLAG. If the result is non-zero, we SWI for a PMC switch. - */ - return (t != NULL) ? ((t->t_chud & THREAD_PMC_FLAG) ? TRUE : FALSE) : FALSE; -} - -static inline void -pmc_swi(thread_t old, thread_t new) { - current_cpu_datap()->csw_old_thread = old; - current_cpu_datap()->csw_new_thread = new; - pal_pmc_swi(); -} - -static inline void -machine_pmc_cswitch(thread_t old, thread_t new) { - if (machine_thread_pmc_eligible(old) || machine_thread_pmc_eligible(new)) { - pmc_swi(old, new); +ml_hv_cswitch(thread_t old, thread_t new) +{ + if (old->hv_thread_target) { + hv_callbacks.preempt(old->hv_thread_target); } -} -void ml_get_csw_threads(thread_t *old, thread_t *new) { - *old = current_cpu_datap()->csw_old_thread; - *new = current_cpu_datap()->csw_new_thread; + if (new->hv_thread_target) { + hv_callbacks.dispatch(new->hv_thread_target); + } } - -#endif /* CONFIG_COUNTERS */ +#endif /* - * Don't let an illegal value for dr7 get set. Specifically, - * check for undefined settings. Setting these bit patterns + * Don't let an illegal value for the lower 32-bits of dr7 get set. + * Specifically, check for undefined settings. Setting these bit patterns * result in undefined behaviour and can lead to an unexpected * TRCTRAP. */ static boolean_t -dr7_is_valid(uint32_t *dr7) +dr7d_is_valid(uint32_t *dr7d) { int i; uint32_t mask1, mask2; @@ -210,114 +182,67 @@ dr7_is_valid(uint32_t *dr7) * If the DE bit is set in CR4, R/W0-3 can be pattern * "10B" to indicate i/o reads and write */ - if (!(get_cr4() & CR4_DE)) - for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 4; - i++, mask1 <<= 4, mask2 <<= 4) - if ((*dr7 & mask1) == mask2) - return (FALSE); - - /* - * len0-3 pattern "10B" is ok for len on Merom and newer processors - * (it signifies an 8-byte wide region). We use the 64bit capability - * of the processor in lieu of the more laborious model/family checks - * as all 64-bit capable processors so far support this. - * Reject an attempt to use this on 64-bit incapable processors. - */ - if (current_cpu_datap()->cpu_is64bit == FALSE) - for (i = 0, mask1 = 0x3<<18, mask2 = 0x2<<18; i < 4; - i++, mask1 <<= 4, mask2 <<= 4) - if ((*dr7 & mask1) == mask2) - return (FALSE); + if (!(get_cr4() & CR4_DE)) { + for (i = 0, mask1 = 0x3 << 16, mask2 = 0x2 << 16; i < 4; + i++, mask1 <<= 4, mask2 <<= 4) { + if ((*dr7d & mask1) == mask2) { + return FALSE; + } + } + } /* * if we are doing an instruction execution break (indicated * by r/w[x] being "00B"), then the len[x] must also be set * to "00B" */ - for (i = 0; i < 4; i++) - if (((((*dr7 >> (16 + i*4))) & 0x3) == 0) && - ((((*dr7 >> (18 + i*4))) & 0x3) != 0)) - return (FALSE); + for (i = 0; i < 4; i++) { + if (((((*dr7d >> (16 + i * 4))) & 0x3) == 0) && + ((((*dr7d >> (18 + i * 4))) & 0x3) != 0)) { + return FALSE; + } + } /* * Intel docs have these bits fixed. */ - *dr7 |= 0x1 << 10; /* set bit 10 to 1 */ - *dr7 &= ~(0x1 << 11); /* set bit 11 to 0 */ - *dr7 &= ~(0x1 << 12); /* set bit 12 to 0 */ - *dr7 &= ~(0x1 << 14); /* set bit 14 to 0 */ - *dr7 &= ~(0x1 << 15); /* set bit 15 to 0 */ + *dr7d |= 0x1 << 10; /* set bit 10 to 1 */ + *dr7d &= ~(0x1 << 11); /* set bit 11 to 0 */ + *dr7d &= ~(0x1 << 12); /* set bit 12 to 0 */ + *dr7d &= ~(0x1 << 14); /* set bit 14 to 0 */ + *dr7d &= ~(0x1 << 15); /* set bit 15 to 0 */ /* * We don't allow anything to set the global breakpoints. */ - if (*dr7 & 0x2) - return (FALSE); - - if (*dr7 & (0x2<<2)) - return (FALSE); + if (*dr7d & 0x2) { + return FALSE; + } - if (*dr7 & (0x2<<4)) - return (FALSE); + if (*dr7d & (0x2 << 2)) { + return FALSE; + } - if (*dr7 & (0x2<<6)) - return (FALSE); + if (*dr7d & (0x2 << 4)) { + return FALSE; + } - return (TRUE); -} + if (*dr7d & (0x2 << 6)) { + return FALSE; + } -static inline void -set_live_debug_state32(cpu_data_t *cdp, x86_debug_state32_t *ds) -{ - __asm__ volatile ("movl %0,%%db0" : :"r" (ds->dr0)); - __asm__ volatile ("movl %0,%%db1" : :"r" (ds->dr1)); - __asm__ volatile ("movl %0,%%db2" : :"r" (ds->dr2)); - __asm__ volatile ("movl %0,%%db3" : :"r" (ds->dr3)); - if (cpu_mode_is64bit()) - cdp->cpu_dr7 = ds->dr7; + return TRUE; } extern void set_64bit_debug_regs(x86_debug_state64_t *ds); -static inline void -set_live_debug_state64(cpu_data_t *cdp, x86_debug_state64_t *ds) -{ - /* - * We need to enter 64-bit mode in order to set the full - * width of these registers - */ - set_64bit_debug_regs(ds); - cdp->cpu_dr7 = ds->dr7; -} - boolean_t -debug_state_is_valid32(x86_debug_state32_t *ds) +debug_state_is_valid32(x86_debug_state32_t *ds) { - if (!dr7_is_valid(&ds->dr7)) + if (!dr7d_is_valid(&ds->dr7)) { return FALSE; - -#if defined(__i386__) - /* - * Only allow local breakpoints and make sure they are not - * in the trampoline code. - */ - if (ds->dr7 & 0x1) - if (ds->dr0 >= (unsigned long)HIGH_MEM_BASE) - return FALSE; - - if (ds->dr7 & (0x1<<2)) - if (ds->dr1 >= (unsigned long)HIGH_MEM_BASE) - return FALSE; - - if (ds->dr7 & (0x1<<4)) - if (ds->dr2 >= (unsigned long)HIGH_MEM_BASE) - return FALSE; - - if (ds->dr7 & (0x1<<6)) - if (ds->dr3 >= (unsigned long)HIGH_MEM_BASE) - return FALSE; -#endif + } return TRUE; } @@ -325,28 +250,40 @@ debug_state_is_valid32(x86_debug_state32_t *ds) boolean_t debug_state_is_valid64(x86_debug_state64_t *ds) { - if (!dr7_is_valid((uint32_t *)&ds->dr7)) + if (!dr7d_is_valid((uint32_t *)&ds->dr7)) { return FALSE; + } /* * Don't allow the user to set debug addresses above their max * value */ - if (ds->dr7 & 0x1) - if (ds->dr0 >= VM_MAX_PAGE_ADDRESS) + if (ds->dr7 & 0x1) { + if (ds->dr0 >= VM_MAX_PAGE_ADDRESS) { return FALSE; + } + } - if (ds->dr7 & (0x1<<2)) - if (ds->dr1 >= VM_MAX_PAGE_ADDRESS) + if (ds->dr7 & (0x1 << 2)) { + if (ds->dr1 >= VM_MAX_PAGE_ADDRESS) { return FALSE; + } + } - if (ds->dr7 & (0x1<<4)) - if (ds->dr2 >= VM_MAX_PAGE_ADDRESS) + if (ds->dr7 & (0x1 << 4)) { + if (ds->dr2 >= VM_MAX_PAGE_ADDRESS) { return FALSE; + } + } - if (ds->dr7 & (0x1<<6)) - if (ds->dr3 >= VM_MAX_PAGE_ADDRESS) + if (ds->dr7 & (0x1 << 6)) { + if (ds->dr3 >= VM_MAX_PAGE_ADDRESS) { return FALSE; + } + } + + /* For x86-64, we must ensure the upper 32-bits of DR7 are clear */ + ds->dr7 &= 0xffffffffULL; return TRUE; } @@ -355,68 +292,73 @@ debug_state_is_valid64(x86_debug_state64_t *ds) static kern_return_t set_debug_state32(thread_t thread, x86_debug_state32_t *ds) { - x86_debug_state32_t *ids; + x86_debug_state32_t *new_ids; pcb_t pcb; pcb = THREAD_TO_PCB(thread); - ids = pcb->ids; if (debug_state_is_valid32(ds) != TRUE) { return KERN_INVALID_ARGUMENT; } - if (ids == NULL) { - ids = zalloc(ids_zone); - bzero(ids, sizeof *ids); + if (pcb->ids == NULL) { + new_ids = zalloc(ids_zone); + bzero(new_ids, sizeof *new_ids); - simple_lock(&pcb->lock); + simple_lock(&pcb->lock, LCK_GRP_NULL); /* make sure it wasn't already alloc()'d elsewhere */ if (pcb->ids == NULL) { - pcb->ids = ids; + pcb->ids = new_ids; simple_unlock(&pcb->lock); } else { simple_unlock(&pcb->lock); - zfree(ids_zone, ids); + zfree(ids_zone, new_ids); } } - copy_debug_state32(ds, ids, FALSE); + copy_debug_state32(ds, pcb->ids, FALSE); - return (KERN_SUCCESS); + return KERN_SUCCESS; } static kern_return_t set_debug_state64(thread_t thread, x86_debug_state64_t *ds) { - x86_debug_state64_t *ids; + x86_debug_state64_t *new_ids; pcb_t pcb; pcb = THREAD_TO_PCB(thread); - ids = pcb->ids; if (debug_state_is_valid64(ds) != TRUE) { return KERN_INVALID_ARGUMENT; } - if (ids == NULL) { - ids = zalloc(ids_zone); - bzero(ids, sizeof *ids); + if (pcb->ids == NULL) { + new_ids = zalloc(ids_zone); + bzero(new_ids, sizeof *new_ids); + +#if HYPERVISOR + if (thread->hv_thread_target) { + hv_callbacks.volatile_state(thread->hv_thread_target, + HV_DEBUG_STATE); + } +#endif - simple_lock(&pcb->lock); + simple_lock(&pcb->lock, LCK_GRP_NULL); /* make sure it wasn't already alloc()'d elsewhere */ if (pcb->ids == NULL) { - pcb->ids = ids; + pcb->ids = new_ids; simple_unlock(&pcb->lock); } else { simple_unlock(&pcb->lock); - zfree(ids_zone, ids); + zfree(ids_zone, new_ids); } } - copy_debug_state64(ds, ids, FALSE); + copy_debug_state64(ds, pcb->ids, FALSE); - return (KERN_SUCCESS); + return KERN_SUCCESS; } static void @@ -428,8 +370,9 @@ get_debug_state32(thread_t thread, x86_debug_state32_t *ds) if (saved_state) { copy_debug_state32(saved_state, ds, TRUE); - } else + } else { bzero(ds, sizeof *ds); + } } static void @@ -441,8 +384,9 @@ get_debug_state64(thread_t thread, x86_debug_state64_t *ds) if (saved_state) { copy_debug_state64(saved_state, ds, TRUE); - } else + } else { bzero(ds, sizeof *ds); + } } /* @@ -465,16 +409,24 @@ consider_machine_adjust(void) */ void machine_load_context( - thread_t new) + thread_t new) { -#if CONFIG_COUNTERS - machine_pmc_cswitch(NULL, new); -#endif new->machine.specFlags |= OnProc; act_machine_switch_pcb(NULL, new); Load_context(new); } +static inline void +pmap_switch_context(thread_t ot, thread_t nt, int cnum) +{ + pmap_assert(ml_get_interrupts_enabled() == FALSE); + vm_map_t nmap = nt->map, omap = ot->map; + if ((omap != nmap) || (nmap->pmap->pagezero_accessible)) { + PMAP_DEACTIVATE_MAP(omap, ot, cnum); + PMAP_ACTIVATE_MAP(nmap, nt, cnum); + } +} + /* * Switch to a new thread. * Save the old thread`s kernel state or continuation, @@ -482,29 +434,29 @@ machine_load_context( */ thread_t machine_switch_context( - thread_t old, - thread_continue_t continuation, - thread_t new) + thread_t old, + thread_continue_t continuation, + thread_t new) { -#if MACH_RT - assert(current_cpu_datap()->cpu_active_stack == old->kernel_stack); -#endif -#if CONFIG_COUNTERS - machine_pmc_cswitch(old, new); -#endif + assert(current_cpu_datap()->cpu_active_stack == old->kernel_stack); + +#if KPC + kpc_off_cpu(old); +#endif /* KPC */ + /* * Save FP registers if in use. */ - fpu_save_context(old); + fpu_switch_context(old, new); old->machine.specFlags &= ~OnProc; new->machine.specFlags |= OnProc; /* - * Monitor the stack depth and report new max, + * Monitor the stack depth and report new max, * not worrying about races. */ - vm_offset_t depth = current_stack_depth(); + vm_offset_t depth = current_stack_depth(); if (depth > kernel_stack_depth_max) { kernel_stack_depth_max = depth; KERNEL_DEBUG_CONSTANT( @@ -516,28 +468,38 @@ machine_switch_context( * Switch address maps if need be, even if not switching tasks. * (A server activation may be "borrowing" a client map.) */ - PMAP_SWITCH_CONTEXT(old, new, cpu_number()); + pmap_switch_context(old, new, cpu_number()); /* * Load the rest of the user state for the new thread */ act_machine_switch_pcb(old, new); - return(Switch_context(old, continuation, new)); +#if HYPERVISOR + ml_hv_cswitch(old, new); +#endif + + return Switch_context(old, continuation, new); } -thread_t +boolean_t +machine_thread_on_core(thread_t thread) +{ + return thread->machine.specFlags & OnProc; +} + +thread_t machine_processor_shutdown( - thread_t thread, - void (*doshutdown)(processor_t), - processor_t processor) + thread_t thread, + void (*doshutdown)(processor_t), + processor_t processor) { #if CONFIG_VMX vmx_suspend(); #endif - fpu_save_context(thread); - PMAP_SWITCH_CONTEXT(thread, processor->idle_thread, cpu_number()); - return(Shutdown_context(thread, doshutdown, processor)); + fpu_switch_context(thread, NULL); + pmap_switch_context(thread, processor->idle_thread, cpu_number()); + return Shutdown_context(thread, doshutdown, processor); } @@ -549,16 +511,17 @@ 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 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()) + if (thread == current_thread()) { clear_fpu(); + } } if (thread->machine.ids) { @@ -566,7 +529,7 @@ machine_thread_state_initialize( thread->machine.ids = NULL; } - return KERN_SUCCESS; + return KERN_SUCCESS; } uint32_t @@ -600,38 +563,38 @@ get_eflags_exportmask(void) * 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; + x86_saved_state64_t *saved_state; - saved_state = USER_REGS64(thread); + 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; + x86_saved_state32_t *saved_state; - saved_state = USER_REGS32(thread); + 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; + x86_saved_state32_t *saved_state; pal_register_cache_state(thread, DIRTY); @@ -641,11 +604,6 @@ set_thread_state32(thread_t thread, x86_thread_state32_t *ts) * Scrub segment selector values: */ ts->cs = USER_CS; -#ifdef __i386__ - if (ts->ss == 0) ts->ss = USER_DS; - if (ts->ds == 0) ts->ds = USER_DS; - if (ts->es == 0) ts->es = USER_DS; -#else /* __x86_64__ */ /* * On a 64 bit kernel, we always override the data segments, * as the actual selector numbers have changed. This also @@ -655,16 +613,19 @@ set_thread_state32(thread_t thread, x86_thread_state32_t *ts) ts->ss = USER_DS; ts->ds = USER_DS; ts->es = USER_DS; -#endif + + /* 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); + ts->ss, + ts->ds, + ts->es, + ts->fs, + ts->gs)) { + return KERN_INVALID_ARGUMENT; + } saved_state->eax = ts->eax; saved_state->ebx = ts->ebx; @@ -688,24 +649,33 @@ set_thread_state32(thread_t thread, x86_thread_state32_t *ts) * ensure that the user returns via iret * - which is signaled thusly: */ - if ((saved_state->efl & EFL_TF) && saved_state->cs == SYSENTER_CS) + if ((saved_state->efl & EFL_TF) && saved_state->cs == SYSENTER_CS) { saved_state->cs = SYSENTER_TF_CS; + } - return(KERN_SUCCESS); + return KERN_SUCCESS; } static int -set_thread_state64(thread_t thread, x86_thread_state64_t *ts) +set_thread_state64(thread_t thread, void *state, int full) { - x86_saved_state64_t *saved_state; + x86_thread_state64_t *ts; + x86_saved_state64_t *saved_state; + + if (full == TRUE) { + ts = &((x86_thread_full_state64_t *)state)->ss64; + } else { + ts = (x86_thread_state64_t *)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); + !IS_USERADDR64_CANONICAL(ts->rip)) { + return KERN_INVALID_ARGUMENT; + } saved_state->r8 = ts->r8; saved_state->r9 = ts->r9; @@ -725,11 +695,22 @@ set_thread_state64(thread_t thread, x86_thread_state64_t *ts) 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; + + if (full == FALSE) { + saved_state->isf.cs = USER64_CS; + } else { + saved_state->isf.cs = ((x86_thread_full_state64_t *)ts)->ss64.cs; + saved_state->isf.ss = ((x86_thread_full_state64_t *)ts)->ss; + saved_state->ds = (uint32_t)((x86_thread_full_state64_t *)ts)->ds; + saved_state->es = (uint32_t)((x86_thread_full_state64_t *)ts)->es; + machine_thread_set_tsd_base(thread, + ((x86_thread_full_state64_t *)ts)->gsbase); + } + saved_state->fs = (uint32_t)ts->fs; saved_state->gs = (uint32_t)ts->gs; - return(KERN_SUCCESS); + return KERN_SUCCESS; } @@ -737,7 +718,7 @@ set_thread_state64(thread_t thread, x86_thread_state64_t *ts) static void get_thread_state32(thread_t thread, x86_thread_state32_t *ts) { - x86_saved_state32_t *saved_state; + x86_saved_state32_t *saved_state; pal_register_cache_state(thread, VALID); @@ -763,9 +744,16 @@ get_thread_state32(thread_t thread, x86_thread_state32_t *ts) static void -get_thread_state64(thread_t thread, x86_thread_state64_t *ts) +get_thread_state64(thread_t thread, void *state, boolean_t full) { - x86_saved_state64_t *saved_state; + x86_thread_state64_t *ts; + x86_saved_state64_t *saved_state; + + if (full == TRUE) { + ts = &((x86_thread_full_state64_t *)state)->ss64; + } else { + ts = (x86_thread_state64_t *)state; + } pal_register_cache_state(thread, VALID); @@ -790,10 +778,59 @@ get_thread_state64(thread_t thread, x86_thread_state64_t *ts) ts->rflags = saved_state->isf.rflags; ts->rip = saved_state->isf.rip; ts->cs = saved_state->isf.cs; + + if (full == TRUE) { + ((x86_thread_full_state64_t *)state)->ds = saved_state->ds; + ((x86_thread_full_state64_t *)state)->es = saved_state->es; + ((x86_thread_full_state64_t *)state)->ss = saved_state->isf.ss; + ((x86_thread_full_state64_t *)state)->gsbase = + thread->machine.cthread_self; + } + ts->fs = saved_state->fs; ts->gs = saved_state->gs; } +kern_return_t +machine_thread_state_convert_to_user( + __unused thread_t thread, + __unused thread_flavor_t flavor, + __unused thread_state_t tstate, + __unused mach_msg_type_number_t *count) +{ + // No conversion to userspace representation on this platform + return KERN_SUCCESS; +} + +kern_return_t +machine_thread_state_convert_from_user( + __unused thread_t thread, + __unused thread_flavor_t flavor, + __unused thread_state_t tstate, + __unused mach_msg_type_number_t count) +{ + // No conversion from userspace representation on this platform + return KERN_SUCCESS; +} + +kern_return_t +machine_thread_siguctx_pointer_convert_to_user( + __unused thread_t thread, + __unused user_addr_t *uctxp) +{ + // No conversion to userspace representation on this platform + return KERN_SUCCESS; +} + +kern_return_t +machine_thread_function_pointers_convert_from_user( + __unused thread_t thread, + __unused user_addr_t *fptrs, + __unused uint32_t count) +{ + // No conversion from userspace representation on this platform + return KERN_SUCCESS; +} /* * act_machine_set_state: @@ -811,25 +848,32 @@ machine_thread_set_state( switch (flavor) { case x86_SAVED_STATE32: { - x86_saved_state32_t *state; - x86_saved_state32_t *saved_state; + x86_saved_state32_t *state; + x86_saved_state32_t *saved_state; - if (count < x86_SAVED_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); - - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (count < x86_SAVED_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_saved_state32_t *) tstate; + /* + * Refuse to allow 64-bit processes to set + * 32-bit state. + */ + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } + /* Check segment selectors are safe */ if (!valid_user_segment_selectors(state->cs, - state->ss, - state->ds, - state->es, - state->fs, - state->gs)) + state->ss, + state->ds, + state->es, + state->fs, + state->gs)) { return KERN_INVALID_ARGUMENT; + } pal_register_cache_state(thr_act, DIRTY); @@ -855,8 +899,9 @@ machine_thread_set_state( * ensure that the user returns via iret * - which is signaled thusly: */ - if ((saved_state->efl & EFL_TF) && state->cs == SYSENTER_CS) + if ((saved_state->efl & EFL_TF) && state->cs == SYSENTER_CS) { state->cs = SYSENTER_TF_CS; + } /* * User setting segment registers. @@ -876,14 +921,16 @@ machine_thread_set_state( case x86_SAVED_STATE64: { - x86_saved_state64_t *state; - x86_saved_state64_t *saved_state; + x86_saved_state64_t *state; + x86_saved_state64_t *saved_state; - if (count < x86_SAVED_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count < x86_SAVED_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } state = (x86_saved_state64_t *) tstate; @@ -894,13 +941,15 @@ machine_thread_set_state( * restore the segment registers--hence they are no * longer relevant for validation. */ - if (!valid_user_code_selector(state->isf.cs)) - return KERN_INVALID_ARGUMENT; - + if (!valid_user_code_selector(state->isf.cs)) { + return KERN_INVALID_ARGUMENT; + } + /* Check pc and stack are canonical addresses */ if (!IS_USERADDR64_CANONICAL(state->isf.rsp) || - !IS_USERADDR64_CANONICAL(state->isf.rip)) + !IS_USERADDR64_CANONICAL(state->isf.rip)) { return KERN_INVALID_ARGUMENT; + } pal_register_cache_state(thr_act, DIRTY); @@ -944,119 +993,163 @@ machine_thread_set_state( } case x86_FLOAT_STATE32: + case x86_AVX_STATE32: + case x86_AVX512_STATE32: { - if (count != x86_FLOAT_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } return fpu_set_fxstate(thr_act, tstate, flavor); } case x86_FLOAT_STATE64: + case x86_AVX_STATE64: + case x86_AVX512_STATE64: { - if (count != x86_FLOAT_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } - if ( !thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } return fpu_set_fxstate(thr_act, tstate, flavor); } case x86_FLOAT_STATE: - { + { x86_float_state_t *state; - if (count != x86_FLOAT_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != x86_FLOAT_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_float_state_t *)tstate; if (state->fsh.flavor == x86_FLOAT_STATE64 && state->fsh.count == x86_FLOAT_STATE64_COUNT && - thread_is_64bit(thr_act)) { + thread_is_64bit_addr(thr_act)) { return fpu_set_fxstate(thr_act, (thread_state_t)&state->ufs.fs64, x86_FLOAT_STATE64); } if (state->fsh.flavor == x86_FLOAT_STATE32 && state->fsh.count == x86_FLOAT_STATE32_COUNT && - !thread_is_64bit(thr_act)) { - return fpu_set_fxstate(thr_act, (thread_state_t)&state->ufs.fs32, x86_FLOAT_STATE32); + !thread_is_64bit_addr(thr_act)) { + return fpu_set_fxstate(thr_act, (thread_state_t)&state->ufs.fs32, x86_FLOAT_STATE32); } - return(KERN_INVALID_ARGUMENT); + return KERN_INVALID_ARGUMENT; } - case x86_AVX_STATE32: + case x86_AVX_STATE: + case x86_AVX512_STATE: { - if (count != x86_AVX_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + x86_avx_state_t *state; - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (count != _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } - return fpu_set_fxstate(thr_act, tstate, flavor); + state = (x86_avx_state_t *)tstate; + /* Flavors are defined to have sequential values: 32-bit, 64-bit, non-specific */ + /* 64-bit flavor? */ + if (state->ash.flavor == (flavor - 1) && + state->ash.count == _MachineStateCount[flavor - 1] && + thread_is_64bit_addr(thr_act)) { + return fpu_set_fxstate(thr_act, + (thread_state_t)&state->ufs.as64, + flavor - 1); + } + /* 32-bit flavor? */ + if (state->ash.flavor == (flavor - 2) && + state->ash.count == _MachineStateCount[flavor - 2] && + !thread_is_64bit_addr(thr_act)) { + return fpu_set_fxstate(thr_act, + (thread_state_t)&state->ufs.as32, + flavor - 2); + } + return KERN_INVALID_ARGUMENT; } - case x86_AVX_STATE64: + case x86_THREAD_STATE32: { - if (count != x86_AVX_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != x86_THREAD_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } - return fpu_set_fxstate(thr_act, tstate, flavor); + return set_thread_state32(thr_act, (x86_thread_state32_t *)tstate); } - case x86_THREAD_STATE32: + case x86_THREAD_STATE64: { - if (count != x86_THREAD_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != x86_THREAD_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } - return set_thread_state32(thr_act, (x86_thread_state32_t *)tstate); + return set_thread_state64(thr_act, tstate, FALSE); } - case x86_THREAD_STATE64: + case x86_THREAD_FULL_STATE64: { - if (count != x86_THREAD_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != x86_THREAD_FULL_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } - return set_thread_state64(thr_act, (x86_thread_state64_t *)tstate); + /* If this process does not have a custom LDT, return failure */ + if (thr_act->task->i386_ldt == 0) { + return KERN_INVALID_ARGUMENT; + } + return set_thread_state64(thr_act, tstate, TRUE); } + case x86_THREAD_STATE: { x86_thread_state_t *state; - if (count != x86_THREAD_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (count != x86_THREAD_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_thread_state_t *)tstate; if (state->tsh.flavor == x86_THREAD_STATE64 && state->tsh.count == x86_THREAD_STATE64_COUNT && - thread_is_64bit(thr_act)) { - return set_thread_state64(thr_act, &state->uts.ts64); + thread_is_64bit_addr(thr_act)) { + return set_thread_state64(thr_act, &state->uts.ts64, FALSE); + } else if (state->tsh.flavor == x86_THREAD_FULL_STATE64 && + state->tsh.count == x86_THREAD_FULL_STATE64_COUNT && + thread_is_64bit_addr(thr_act) && thr_act->task->i386_ldt != 0) { + return set_thread_state64(thr_act, &state->uts.ts64, TRUE); } else if (state->tsh.flavor == x86_THREAD_STATE32 && - state->tsh.count == x86_THREAD_STATE32_COUNT && - !thread_is_64bit(thr_act)) { + state->tsh.count == x86_THREAD_STATE32_COUNT && + !thread_is_64bit_addr(thr_act)) { return set_thread_state32(thr_act, &state->uts.ts32); - } else - return(KERN_INVALID_ARGUMENT); - - break; + } else { + return KERN_INVALID_ARGUMENT; + } } case x86_DEBUG_STATE32: { x86_debug_state32_t *state; kern_return_t ret; - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } state = (x86_debug_state32_t *)tstate; @@ -1069,8 +1162,9 @@ machine_thread_set_state( x86_debug_state64_t *state; kern_return_t ret; - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } state = (x86_debug_state64_t *)tstate; @@ -1083,30 +1177,53 @@ machine_thread_set_state( x86_debug_state_t *state; kern_return_t ret = KERN_INVALID_ARGUMENT; - if (count != x86_DEBUG_STATE_COUNT) - return (KERN_INVALID_ARGUMENT); + if (count != x86_DEBUG_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_debug_state_t *)tstate; if (state->dsh.flavor == x86_DEBUG_STATE64 && - state->dsh.count == x86_DEBUG_STATE64_COUNT && - thread_is_64bit(thr_act)) { + state->dsh.count == x86_DEBUG_STATE64_COUNT && + thread_is_64bit_addr(thr_act)) { ret = set_debug_state64(thr_act, &state->uds.ds64); - } - else - if (state->dsh.flavor == x86_DEBUG_STATE32 && - state->dsh.count == x86_DEBUG_STATE32_COUNT && - !thread_is_64bit(thr_act)) { - ret = set_debug_state32(thr_act, &state->uds.ds32); + } else if (state->dsh.flavor == x86_DEBUG_STATE32 && + state->dsh.count == x86_DEBUG_STATE32_COUNT && + !thread_is_64bit_addr(thr_act)) { + ret = set_debug_state32(thr_act, &state->uds.ds32); } return ret; } default: - return(KERN_INVALID_ARGUMENT); + return KERN_INVALID_ARGUMENT; } - return(KERN_SUCCESS); + return KERN_SUCCESS; } +mach_vm_address_t +machine_thread_pc(thread_t thr_act) +{ + if (thread_is_64bit_addr(thr_act)) { + return (mach_vm_address_t)USER_REGS64(thr_act)->isf.rip; + } else { + return (mach_vm_address_t)USER_REGS32(thr_act)->eip; + } +} + +void +machine_thread_reset_pc(thread_t thr_act, mach_vm_address_t pc) +{ + pal_register_cache_state(thr_act, DIRTY); + + if (thread_is_64bit_addr(thr_act)) { + if (!IS_USERADDR64_CANONICAL(pc)) { + pc = 0; + } + USER_REGS64(thr_act)->isf.rip = (uint64_t)pc; + } else { + USER_REGS32(thr_act)->eip = (uint32_t)pc; + } +} /* @@ -1122,46 +1239,99 @@ machine_thread_get_state( thread_state_t tstate, mach_msg_type_number_t *count) { + switch (flavor) { + case THREAD_STATE_FLAVOR_LIST: + { + if (*count < 3) { + return KERN_INVALID_ARGUMENT; + } - switch (flavor) { - - case THREAD_STATE_FLAVOR_LIST: - { - if (*count < 3) - return (KERN_INVALID_ARGUMENT); - - tstate[0] = i386_THREAD_STATE; + tstate[0] = i386_THREAD_STATE; tstate[1] = i386_FLOAT_STATE; tstate[2] = i386_EXCEPTION_STATE; *count = 3; break; - } + } - case THREAD_STATE_FLAVOR_LIST_NEW: - { - if (*count < 4) - return (KERN_INVALID_ARGUMENT); + case THREAD_STATE_FLAVOR_LIST_NEW: + { + if (*count < 4) { + return KERN_INVALID_ARGUMENT; + } - tstate[0] = x86_THREAD_STATE; + tstate[0] = x86_THREAD_STATE; tstate[1] = x86_FLOAT_STATE; tstate[2] = x86_EXCEPTION_STATE; tstate[3] = x86_DEBUG_STATE; *count = 4; break; - } + } - case x86_SAVED_STATE32: - { - x86_saved_state32_t *state; - x86_saved_state32_t *saved_state; + case THREAD_STATE_FLAVOR_LIST_10_9: + { + if (*count < 5) { + return KERN_INVALID_ARGUMENT; + } + + tstate[0] = x86_THREAD_STATE; + tstate[1] = x86_FLOAT_STATE; + tstate[2] = x86_EXCEPTION_STATE; + tstate[3] = x86_DEBUG_STATE; + tstate[4] = x86_AVX_STATE; + + *count = 5; + break; + } + + case THREAD_STATE_FLAVOR_LIST_10_13: + { + if (*count < 6) { + return KERN_INVALID_ARGUMENT; + } + + tstate[0] = x86_THREAD_STATE; + tstate[1] = x86_FLOAT_STATE; + tstate[2] = x86_EXCEPTION_STATE; + tstate[3] = x86_DEBUG_STATE; + tstate[4] = x86_AVX_STATE; + tstate[5] = x86_AVX512_STATE; + + *count = 6; + break; + } + + case THREAD_STATE_FLAVOR_LIST_10_15: + { + if (*count < 7) { + return KERN_INVALID_ARGUMENT; + } + + tstate[0] = x86_THREAD_STATE; + tstate[1] = x86_FLOAT_STATE; + tstate[2] = x86_EXCEPTION_STATE; + tstate[3] = x86_DEBUG_STATE; + tstate[4] = x86_AVX_STATE; + tstate[5] = x86_AVX512_STATE; + tstate[6] = x86_PAGEIN_STATE; + + *count = 7; + break; + } - if (*count < x86_SAVED_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_SAVED_STATE32: + { + x86_saved_state32_t *state; + x86_saved_state32_t *saved_state; - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_SAVED_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } + + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } state = (x86_saved_state32_t *) tstate; saved_state = USER_REGS32(thr_act); @@ -1177,18 +1347,20 @@ machine_thread_get_state( *count = x86_SAVED_STATE32_COUNT; break; - } + } - case x86_SAVED_STATE64: - { - x86_saved_state64_t *state; - x86_saved_state64_t *saved_state; + case x86_SAVED_STATE64: + { + x86_saved_state64_t *state; + x86_saved_state64_t *saved_state; - if (*count < x86_SAVED_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_SAVED_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } state = (x86_saved_state64_t *)tstate; saved_state = USER_REGS64(thr_act); @@ -1197,158 +1369,227 @@ machine_thread_get_state( * First, copy everything: */ *state = *saved_state; + state->ds = saved_state->ds & 0xffff; + state->es = saved_state->es & 0xffff; state->fs = saved_state->fs & 0xffff; state->gs = saved_state->gs & 0xffff; *count = x86_SAVED_STATE64_COUNT; break; - } + } - case x86_FLOAT_STATE32: - { - if (*count < x86_FLOAT_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_FLOAT_STATE32: + { + if (*count < x86_FLOAT_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_FLOAT_STATE32_COUNT; return fpu_get_fxstate(thr_act, tstate, flavor); - } + } - case x86_FLOAT_STATE64: - { - if (*count < x86_FLOAT_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_FLOAT_STATE64: + { + if (*count < x86_FLOAT_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if ( !thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_FLOAT_STATE64_COUNT; return fpu_get_fxstate(thr_act, tstate, flavor); - } + } - case x86_FLOAT_STATE: - { - x86_float_state_t *state; - kern_return_t kret; + case x86_FLOAT_STATE: + { + x86_float_state_t *state; + kern_return_t kret; - if (*count < x86_FLOAT_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_FLOAT_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_float_state_t *)tstate; /* - * no need to bzero... currently + * no need to bzero... currently * x86_FLOAT_STATE64_COUNT == x86_FLOAT_STATE32_COUNT */ - if (thread_is_64bit(thr_act)) { - state->fsh.flavor = x86_FLOAT_STATE64; - state->fsh.count = x86_FLOAT_STATE64_COUNT; + if (thread_is_64bit_addr(thr_act)) { + state->fsh.flavor = x86_FLOAT_STATE64; + state->fsh.count = x86_FLOAT_STATE64_COUNT; kret = fpu_get_fxstate(thr_act, (thread_state_t)&state->ufs.fs64, x86_FLOAT_STATE64); } else { - state->fsh.flavor = x86_FLOAT_STATE32; + state->fsh.flavor = x86_FLOAT_STATE32; state->fsh.count = x86_FLOAT_STATE32_COUNT; kret = fpu_get_fxstate(thr_act, (thread_state_t)&state->ufs.fs32, x86_FLOAT_STATE32); } *count = x86_FLOAT_STATE_COUNT; - return(kret); - } + return kret; + } case x86_AVX_STATE32: + case x86_AVX512_STATE32: { - if (*count != x86_AVX_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count != _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } - *count = x86_AVX_STATE32_COUNT; + *count = _MachineStateCount[flavor]; return fpu_get_fxstate(thr_act, tstate, flavor); } case x86_AVX_STATE64: + case x86_AVX512_STATE64: { - if (*count != x86_AVX_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count != _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } - if ( !thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } - *count = x86_AVX_STATE64_COUNT; + *count = _MachineStateCount[flavor]; return fpu_get_fxstate(thr_act, tstate, flavor); } - case x86_THREAD_STATE32: - { - if (*count < x86_THREAD_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_AVX_STATE: + case x86_AVX512_STATE: + { + x86_avx_state_t *state; + thread_state_t fstate; + + if (*count < _MachineStateCount[flavor]) { + return KERN_INVALID_ARGUMENT; + } + + *count = _MachineStateCount[flavor]; + state = (x86_avx_state_t *)tstate; + + bzero((char *)state, *count * sizeof(int)); + + if (thread_is_64bit_addr(thr_act)) { + flavor -= 1; /* 64-bit flavor */ + fstate = (thread_state_t) &state->ufs.as64; + } else { + flavor -= 2; /* 32-bit flavor */ + fstate = (thread_state_t) &state->ufs.as32; + } + state->ash.flavor = flavor; + state->ash.count = _MachineStateCount[flavor]; + + return fpu_get_fxstate(thr_act, fstate, flavor); + } + + case x86_THREAD_STATE32: + { + if (*count < x86_THREAD_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_THREAD_STATE32_COUNT; get_thread_state32(thr_act, (x86_thread_state32_t *)tstate); break; - } + } - case x86_THREAD_STATE64: - { - if (*count < x86_THREAD_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_THREAD_STATE64: + { + if (*count < x86_THREAD_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if ( !thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_THREAD_STATE64_COUNT; - get_thread_state64(thr_act, (x86_thread_state64_t *)tstate); + get_thread_state64(thr_act, tstate, FALSE); + break; + } + + case x86_THREAD_FULL_STATE64: + { + if (*count < x86_THREAD_FULL_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } + + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } + + /* If this process does not have a custom LDT, return failure */ + if (thr_act->task->i386_ldt == 0) { + return KERN_INVALID_ARGUMENT; + } + + *count = x86_THREAD_FULL_STATE64_COUNT; + + get_thread_state64(thr_act, tstate, TRUE); break; - } + } - case x86_THREAD_STATE: - { - x86_thread_state_t *state; + case x86_THREAD_STATE: + { + x86_thread_state_t *state; - if (*count < x86_THREAD_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_THREAD_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_thread_state_t *)tstate; bzero((char *)state, sizeof(x86_thread_state_t)); - if (thread_is_64bit(thr_act)) { + if (thread_is_64bit_addr(thr_act)) { state->tsh.flavor = x86_THREAD_STATE64; state->tsh.count = x86_THREAD_STATE64_COUNT; - get_thread_state64(thr_act, &state->uts.ts64); + get_thread_state64(thr_act, &state->uts.ts64, FALSE); } else { state->tsh.flavor = x86_THREAD_STATE32; state->tsh.count = x86_THREAD_STATE32_COUNT; - get_thread_state32(thr_act, &state->uts.ts32); + get_thread_state32(thr_act, &state->uts.ts32); } *count = x86_THREAD_STATE_COUNT; break; - } + } - case x86_EXCEPTION_STATE32: - { - if (*count < x86_EXCEPTION_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_EXCEPTION_STATE32: + { + if (*count < x86_EXCEPTION_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_EXCEPTION_STATE32_COUNT; @@ -1359,15 +1600,17 @@ machine_thread_get_state( */ ((x86_exception_state32_t *)tstate)->cpu = 0; break; - } + } - case x86_EXCEPTION_STATE64: - { - if (*count < x86_EXCEPTION_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); + case x86_EXCEPTION_STATE64: + { + if (*count < x86_EXCEPTION_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if ( !thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } *count = x86_EXCEPTION_STATE64_COUNT; @@ -1378,29 +1621,30 @@ machine_thread_get_state( */ ((x86_exception_state64_t *)tstate)->cpu = 0; break; - } + } - case x86_EXCEPTION_STATE: - { - x86_exception_state_t *state; + case x86_EXCEPTION_STATE: + { + x86_exception_state_t *state; - if (*count < x86_EXCEPTION_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_EXCEPTION_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_exception_state_t *)tstate; bzero((char *)state, sizeof(x86_exception_state_t)); - if (thread_is_64bit(thr_act)) { + if (thread_is_64bit_addr(thr_act)) { state->esh.flavor = x86_EXCEPTION_STATE64; state->esh.count = x86_EXCEPTION_STATE64_COUNT; - get_exception_state64(thr_act, &state->ues.es64); + get_exception_state64(thr_act, &state->ues.es64); } else { state->esh.flavor = x86_EXCEPTION_STATE32; state->esh.count = x86_EXCEPTION_STATE32_COUNT; - get_exception_state32(thr_act, &state->ues.es32); + get_exception_state32(thr_act, &state->ues.es32); } *count = x86_EXCEPTION_STATE_COUNT; @@ -1408,11 +1652,13 @@ machine_thread_get_state( } case x86_DEBUG_STATE32: { - if (*count < x86_DEBUG_STATE32_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_DEBUG_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } get_debug_state32(thr_act, (x86_debug_state32_t *)tstate); @@ -1422,11 +1668,13 @@ machine_thread_get_state( } case x86_DEBUG_STATE64: { - if (*count < x86_DEBUG_STATE64_COUNT) - return(KERN_INVALID_ARGUMENT); - - if (!thread_is_64bit(thr_act)) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_DEBUG_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } + + if (!thread_is_64bit_addr(thr_act)) { + return KERN_INVALID_ARGUMENT; + } get_debug_state64(thr_act, (x86_debug_state64_t *)tstate); @@ -1438,14 +1686,15 @@ machine_thread_get_state( { x86_debug_state_t *state; - if (*count < x86_DEBUG_STATE_COUNT) - return(KERN_INVALID_ARGUMENT); + if (*count < x86_DEBUG_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_debug_state_t *)tstate; bzero(state, sizeof *state); - if (thread_is_64bit(thr_act)) { + if (thread_is_64bit_addr(thr_act)) { state->dsh.flavor = x86_DEBUG_STATE64; state->dsh.count = x86_DEBUG_STATE64_COUNT; @@ -1459,36 +1708,52 @@ machine_thread_get_state( *count = x86_DEBUG_STATE_COUNT; break; } + + case x86_PAGEIN_STATE: + { + if (*count < x86_PAGEIN_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } + + x86_pagein_state_t *state = (void *)tstate; + + state->__pagein_error = thr_act->t_pagein_error; + + *count = x86_PAGEIN_STATE_COUNT; + break; + } default: - return(KERN_INVALID_ARGUMENT); + return KERN_INVALID_ARGUMENT; } - return(KERN_SUCCESS); + return KERN_SUCCESS; } kern_return_t machine_thread_get_kern_state( - thread_t thread, - thread_flavor_t flavor, - thread_state_t tstate, - mach_msg_type_number_t *count) + thread_t thread, + thread_flavor_t flavor, + thread_state_t tstate, + mach_msg_type_number_t *count) { - x86_saved_state_t *int_state = current_cpu_datap()->cpu_int_state; + x86_saved_state_t *int_state = current_cpu_datap()->cpu_int_state; /* * This works only for an interrupted kernel thread */ - if (thread != current_thread() || int_state == NULL) + if (thread != current_thread() || int_state == NULL) { return KERN_FAILURE; + } switch (flavor) { - case x86_THREAD_STATE32: { + case x86_THREAD_STATE32: { x86_thread_state32_t *state; x86_saved_state32_t *saved_state; if (!is_saved_state32(int_state) || - *count < x86_THREAD_STATE32_COUNT) - return (KERN_INVALID_ARGUMENT); + *count < x86_THREAD_STATE32_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_thread_state32_t *) tstate; @@ -1516,15 +1781,16 @@ machine_thread_get_kern_state( *count = x86_THREAD_STATE32_COUNT; return KERN_SUCCESS; - } - - case x86_THREAD_STATE64: { - x86_thread_state64_t *state; - x86_saved_state64_t *saved_state; + } + + case x86_THREAD_STATE64: { + x86_thread_state64_t *state; + x86_saved_state64_t *saved_state; if (!is_saved_state64(int_state) || - *count < x86_THREAD_STATE64_COUNT) - return (KERN_INVALID_ARGUMENT); + *count < x86_THREAD_STATE64_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_thread_state64_t *) tstate; @@ -1557,13 +1823,14 @@ machine_thread_get_kern_state( *count = x86_THREAD_STATE64_COUNT; return KERN_SUCCESS; - } - - case x86_THREAD_STATE: { + } + + case x86_THREAD_STATE: { x86_thread_state_t *state = NULL; - if (*count < x86_THREAD_STATE_COUNT) - return (KERN_INVALID_ARGUMENT); + if (*count < x86_THREAD_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } state = (x86_thread_state_t *) tstate; @@ -1629,7 +1896,7 @@ machine_thread_get_kern_state( *count = x86_THREAD_STATE_COUNT; return KERN_SUCCESS; - } + } } return KERN_FAILURE; } @@ -1650,13 +1917,12 @@ machine_thread_switch_addrmode(thread_t thread) */ machine_thread_create(thread, thread->task); + /* Adjust FPU state */ + fpu_switch_addrmode(thread, task_has_64Bit_addr(thread->task)); + /* If we're switching ourselves, reset the pcb addresses etc. */ if (thread == current_thread()) { boolean_t istate = ml_set_interrupts_enabled(FALSE); -#if defined(__i386__) - if (current_cpu_datap()->cpu_active_cr3 != kernel_pmap->pm_cr3) - pmap_load_kernel_cr3(); -#endif /* defined(__i386) */ act_machine_switch_pcb(NULL, thread); ml_set_interrupts_enabled(istate); } @@ -1675,27 +1941,6 @@ machine_set_current_thread(thread_t thread) current_cpu_datap()->cpu_active_thread = thread; } -/* - * This is called when a task is terminated, and also on exec(). - * Clear machine-dependent state that is stored on the task. - */ -void -machine_thread_terminate_self(void) -{ - task_t self_task = current_task(); - if (self_task) { - user_ldt_t user_ldt = self_task->i386_ldt; - if (user_ldt != 0) { - self_task->i386_ldt = 0; - user_ldt_free(user_ldt); - } - - if (self_task->task_debug != NULL) { - zfree(ids_zone, self_task->task_debug); - self_task->task_debug = NULL; - } - } -} /* * Perform machine-dependent per-thread initializations @@ -1703,139 +1948,38 @@ machine_thread_terminate_self(void) void machine_thread_init(void) { - if (cpu_mode_is64bit()) { - assert(sizeof(x86_sframe_compat32_t) % 16 == 0); - iss_zone = zinit(sizeof(x86_sframe64_t), - thread_max * sizeof(x86_sframe64_t), - THREAD_CHUNK * sizeof(x86_sframe64_t), - "x86_64 saved state"); - - ids_zone = zinit(sizeof(x86_debug_state64_t), - thread_max * sizeof(x86_debug_state64_t), - THREAD_CHUNK * sizeof(x86_debug_state64_t), - "x86_64 debug state"); - - } else { - iss_zone = zinit(sizeof(x86_sframe32_t), - thread_max * sizeof(x86_sframe32_t), - THREAD_CHUNK * sizeof(x86_sframe32_t), - "x86 saved state"); - ids_zone = zinit(sizeof(x86_debug_state32_t), - thread_max * (sizeof(x86_debug_state32_t)), - THREAD_CHUNK * (sizeof(x86_debug_state32_t)), - "x86 debug state"); - } - fpu_module_init(); -} - - -#if defined(__i386__) -/* - * Some routines for debugging activation code - */ -static void dump_handlers(thread_t); -void dump_regs(thread_t); -int dump_act(thread_t thr_act); - -static void -dump_handlers(thread_t thr_act) -{ - ReturnHandler *rhp = thr_act->handlers; - int counter = 0; - - printf("\t"); - while (rhp) { - if (rhp == &thr_act->special_handler){ - if (rhp->next) - printf("[NON-Zero next ptr(%p)]", rhp->next); - printf("special_handler()->"); - break; - } - printf("hdlr_%d(%p)->", counter, rhp->handler); - rhp = rhp->next; - if (++counter > 32) { - printf("Aborting: HUGE handler chain\n"); - break; - } - } - printf("HLDR_NULL\n"); -} - -void -dump_regs(thread_t thr_act) -{ - if (thread_is_64bit(thr_act)) { - x86_saved_state64_t *ssp; - - ssp = USER_REGS64(thr_act); - - panic("dump_regs: 64bit tasks not yet supported"); - - } else { - x86_saved_state32_t *ssp; - - ssp = USER_REGS32(thr_act); + iss_zone = zinit(sizeof(x86_saved_state_t), + thread_max * sizeof(x86_saved_state_t), + THREAD_CHUNK * sizeof(x86_saved_state_t), + "x86_64 saved state"); - /* - * Print out user register state - */ - printf("\tRegs:\tedi=%x esi=%x ebp=%x ebx=%x edx=%x\n", - ssp->edi, ssp->esi, ssp->ebp, ssp->ebx, ssp->edx); + ids_zone = zinit(sizeof(x86_debug_state64_t), + thread_max * sizeof(x86_debug_state64_t), + THREAD_CHUNK * sizeof(x86_debug_state64_t), + "x86_64 debug state"); - printf("\t\tecx=%x eax=%x eip=%x efl=%x uesp=%x\n", - ssp->ecx, ssp->eax, ssp->eip, ssp->efl, ssp->uesp); - - printf("\t\tcs=%x ss=%x\n", ssp->cs, ssp->ss); - } + fpu_module_init(); } -int -dump_act(thread_t thr_act) -{ - if (!thr_act) - return(0); - - printf("thread(%p)(%d): task=%p(%d)\n", - thr_act, thr_act->ref_count, - thr_act->task, - thr_act->task ? thr_act->task->ref_count : 0); - - printf("\tsusp=%d user_stop=%d active=%x ast=%x\n", - thr_act->suspend_count, thr_act->user_stop_count, - thr_act->active, thr_act->ast); - printf("\tpcb=%p\n", &thr_act->machine); - - if (thr_act->kernel_stack) { - vm_offset_t stack = thr_act->kernel_stack; - printf("\tk_stk %lx eip %x ebx %x esp %x iss %p\n", - (long)stack, STACK_IKS(stack)->k_eip, STACK_IKS(stack)->k_ebx, - STACK_IKS(stack)->k_esp, thr_act->machine.iss); - } - - dump_handlers(thr_act); - dump_regs(thr_act); - return((int)thr_act); -} -#endif user_addr_t get_useraddr(void) { - thread_t thr_act = current_thread(); - - if (thread_is_64bit(thr_act)) { - x86_saved_state64_t *iss64; - + thread_t thr_act = current_thread(); + + if (thread_is_64bit_addr(thr_act)) { + x86_saved_state64_t *iss64; + iss64 = USER_REGS64(thr_act); - return(iss64->isf.rip); + return iss64->isf.rip; } else { - x86_saved_state32_t *iss32; + x86_saved_state32_t *iss32; iss32 = USER_REGS32(thr_act); - return(iss32->eip); + return iss32->eip; } } @@ -1849,14 +1993,14 @@ machine_stack_detach(thread_t thread) vm_offset_t stack; KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DETACH), - (uintptr_t)thread_tid(thread), thread->priority, - thread->sched_pri, 0, - 0); + (uintptr_t)thread_tid(thread), thread->priority, + thread->sched_pri, 0, + 0); stack = thread->kernel_stack; thread->kernel_stack = 0; - return (stack); + return stack; } /* @@ -1865,28 +2009,30 @@ machine_stack_detach(thread_t thread) void machine_stack_attach( - thread_t thread, - vm_offset_t stack) + thread_t thread, + vm_offset_t stack) { struct x86_kernel_state *statep; KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_ATTACH), - (uintptr_t)thread_tid(thread), thread->priority, - thread->sched_pri, 0, 0); + (uintptr_t)thread_tid(thread), thread->priority, + thread->sched_pri, 0, 0); assert(stack); thread->kernel_stack = stack; + thread_initialize_kernel_state(thread); statep = STACK_IKS(stack); -#if defined(__x86_64__) - statep->k_rip = (unsigned long) Thread_continue; - statep->k_rbx = (unsigned long) thread_continue; - statep->k_rsp = (unsigned long) (STACK_IKS(stack) - 1); -#else - statep->k_eip = (unsigned long) Thread_continue; - statep->k_ebx = (unsigned long) thread_continue; - statep->k_esp = (unsigned long) (STACK_IKS(stack) - 1); -#endif + + /* + * Reset the state of the thread to resume from a continuation, + * including resetting the stack and frame pointer to avoid backtracers + * seeing this temporary state and attempting to walk the defunct stack. + */ + statep->k_rbp = (uint64_t) 0; + statep->k_rip = (uint64_t) Thread_continue; + statep->k_rbx = (uint64_t) thread_continue; + statep->k_rsp = (uint64_t) STACK_IKS(stack); return; } @@ -1897,16 +2043,14 @@ machine_stack_attach( void machine_stack_handoff(thread_t old, - thread_t new) + thread_t new) { vm_offset_t stack; assert(new); assert(old); -#if CONFIG_COUNTERS - machine_pmc_cswitch(old, new); -#endif + kpc_off_cpu(old); stack = old->kernel_stack; if (stack == old->reserved_stack) { @@ -1921,15 +2065,20 @@ machine_stack_handoff(thread_t old, */ new->kernel_stack = stack; - fpu_save_context(old); - + fpu_switch_context(old, new); + old->machine.specFlags &= ~OnProc; new->machine.specFlags |= OnProc; - PMAP_SWITCH_CONTEXT(old, new, cpu_number()); + pmap_switch_context(old, new, cpu_number()); act_machine_switch_pcb(old, new); +#if HYPERVISOR + ml_hv_cswitch(old, new); +#endif + machine_set_current_thread(new); + thread_initialize_kernel_state(new); return; } @@ -1958,140 +2107,133 @@ act_thread_csave(void) mach_msg_type_number_t val; thread_t thr_act = current_thread(); - if (thread_is_64bit(thr_act)) { + if (thread_is_64bit_addr(thr_act)) { struct x86_act_context64 *ic64; ic64 = (struct x86_act_context64 *)kalloc(sizeof(struct x86_act_context64)); - if (ic64 == (struct x86_act_context64 *)NULL) - return((void *)0); + if (ic64 == (struct x86_act_context64 *)NULL) { + return (void *)0; + } - val = x86_SAVED_STATE64_COUNT; + val = x86_SAVED_STATE64_COUNT; kret = machine_thread_get_state(thr_act, x86_SAVED_STATE64, - (thread_state_t) &ic64->ss, &val); + (thread_state_t) &ic64->ss, &val); if (kret != KERN_SUCCESS) { kfree(ic64, sizeof(struct x86_act_context64)); - return((void *)0); + return (void *)0; } - val = x86_FLOAT_STATE64_COUNT; + val = x86_FLOAT_STATE64_COUNT; kret = machine_thread_get_state(thr_act, x86_FLOAT_STATE64, - (thread_state_t) &ic64->fs, &val); + (thread_state_t) &ic64->fs, &val); if (kret != KERN_SUCCESS) { kfree(ic64, sizeof(struct x86_act_context64)); - return((void *)0); + return (void *)0; } val = x86_DEBUG_STATE64_COUNT; kret = machine_thread_get_state(thr_act, - x86_DEBUG_STATE64, - (thread_state_t)&ic64->ds, - &val); + x86_DEBUG_STATE64, + (thread_state_t)&ic64->ds, + &val); if (kret != KERN_SUCCESS) { - kfree(ic64, sizeof(struct x86_act_context64)); - return((void *)0); + kfree(ic64, sizeof(struct x86_act_context64)); + return (void *)0; } - return(ic64); - + return ic64; } else { struct x86_act_context32 *ic32; ic32 = (struct x86_act_context32 *)kalloc(sizeof(struct x86_act_context32)); - if (ic32 == (struct x86_act_context32 *)NULL) - return((void *)0); + if (ic32 == (struct x86_act_context32 *)NULL) { + return (void *)0; + } - val = x86_SAVED_STATE32_COUNT; + val = x86_SAVED_STATE32_COUNT; kret = machine_thread_get_state(thr_act, x86_SAVED_STATE32, - (thread_state_t) &ic32->ss, &val); + (thread_state_t) &ic32->ss, &val); if (kret != KERN_SUCCESS) { kfree(ic32, sizeof(struct x86_act_context32)); - return((void *)0); + return (void *)0; } - val = x86_FLOAT_STATE32_COUNT; + val = x86_FLOAT_STATE32_COUNT; kret = machine_thread_get_state(thr_act, x86_FLOAT_STATE32, - (thread_state_t) &ic32->fs, &val); + (thread_state_t) &ic32->fs, &val); if (kret != KERN_SUCCESS) { kfree(ic32, sizeof(struct x86_act_context32)); - return((void *)0); + return (void *)0; } val = x86_DEBUG_STATE32_COUNT; kret = machine_thread_get_state(thr_act, - x86_DEBUG_STATE32, - (thread_state_t)&ic32->ds, - &val); + x86_DEBUG_STATE32, + (thread_state_t)&ic32->ds, + &val); if (kret != KERN_SUCCESS) { - kfree(ic32, sizeof(struct x86_act_context32)); - return((void *)0); + kfree(ic32, sizeof(struct x86_act_context32)); + return (void *)0; } - return(ic32); + return ic32; } } -void +void act_thread_catt(void *ctx) { - thread_t thr_act = current_thread(); + thread_t thr_act = current_thread(); kern_return_t kret; - if (ctx == (void *)NULL) - return; + if (ctx == (void *)NULL) { + return; + } - if (thread_is_64bit(thr_act)) { - struct x86_act_context64 *ic64; + if (thread_is_64bit_addr(thr_act)) { + struct x86_act_context64 *ic64; - ic64 = (struct x86_act_context64 *)ctx; + ic64 = (struct x86_act_context64 *)ctx; kret = machine_thread_set_state(thr_act, x86_SAVED_STATE64, - (thread_state_t) &ic64->ss, x86_SAVED_STATE64_COUNT); + (thread_state_t) &ic64->ss, x86_SAVED_STATE64_COUNT); if (kret == KERN_SUCCESS) { - machine_thread_set_state(thr_act, x86_FLOAT_STATE64, - (thread_state_t) &ic64->fs, x86_FLOAT_STATE64_COUNT); + machine_thread_set_state(thr_act, x86_FLOAT_STATE64, + (thread_state_t) &ic64->fs, x86_FLOAT_STATE64_COUNT); } kfree(ic64, sizeof(struct x86_act_context64)); } else { - struct x86_act_context32 *ic32; + struct x86_act_context32 *ic32; - ic32 = (struct x86_act_context32 *)ctx; + ic32 = (struct x86_act_context32 *)ctx; kret = machine_thread_set_state(thr_act, x86_SAVED_STATE32, - (thread_state_t) &ic32->ss, x86_SAVED_STATE32_COUNT); + (thread_state_t) &ic32->ss, x86_SAVED_STATE32_COUNT); if (kret == KERN_SUCCESS) { (void) machine_thread_set_state(thr_act, x86_FLOAT_STATE32, - (thread_state_t) &ic32->fs, x86_FLOAT_STATE32_COUNT); + (thread_state_t) &ic32->fs, x86_FLOAT_STATE32_COUNT); } kfree(ic32, sizeof(struct x86_act_context32)); } } -void act_thread_cfree(__unused void *ctx) +void +act_thread_cfree(__unused void *ctx) { /* XXX - Unused */ } -void x86_toggle_sysenter_arg_store(thread_t thread, boolean_t valid); -void x86_toggle_sysenter_arg_store(thread_t thread, boolean_t valid) { - thread->machine.arg_store_valid = valid; -} - -boolean_t x86_sysenter_arg_store_isvalid(thread_t thread); - -boolean_t x86_sysenter_arg_store_isvalid(thread_t thread) { - return (thread->machine.arg_store_valid); -} /* * Duplicate one x86_debug_state32_t to another. "all" parameter * chooses whether dr4 and dr5 are copied (they are never meant - * to be installed when we do machine_task_set_state() or + * to be installed when we do machine_task_set_state() or * machine_thread_set_state()). */ void copy_debug_state32( - x86_debug_state32_t *src, - x86_debug_state32_t *target, - boolean_t all) + x86_debug_state32_t *src, + x86_debug_state32_t *target, + boolean_t all) { if (all) { target->dr4 = src->dr4; @@ -2109,14 +2251,14 @@ copy_debug_state32( /* * Duplicate one x86_debug_state64_t to another. "all" parameter * chooses whether dr4 and dr5 are copied (they are never meant - * to be installed when we do machine_task_set_state() or + * to be installed when we do machine_task_set_state() or * machine_thread_set_state()). */ void copy_debug_state64( - x86_debug_state64_t *src, - x86_debug_state64_t *target, - boolean_t all) + x86_debug_state64_t *src, + x86_debug_state64_t *target, + boolean_t all) { if (all) { target->dr4 = src->dr4;