#include <mach_rt.h>
#include <mach_debug.h>
#include <mach_ldebug.h>
#include <sys/kdebug.h>
#include <mach/kern_return.h>
#include <mach/thread_status.h>
#include <mach/vm_param.h>
#include <kern/counters.h>
#include <kern/kalloc.h>
#include <kern/mach_param.h>
#include <kern/processor.h>
#include <kern/cpu_data.h>
#include <kern/cpu_number.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/sched_prim.h>
#include <kern/misc_protos.h>
#include <kern/assert.h>
#include <kern/spl.h>
#include <kern/machine.h>
#include <ipc/ipc_port.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <vm/pmap.h>
#include <vm/vm_protos.h>
#include <i386/commpage/commpage.h>
#include <i386/cpu_data.h>
#include <i386/cpu_number.h>
#include <i386/eflags.h>
#include <i386/proc_reg.h>
#include <i386/tss.h>
#include <i386/user_ldt.h>
#include <i386/fpu.h>
#include <i386/mp_desc.h>
#include <i386/misc_protos.h>
#include <i386/thread.h>
#include <i386/seg.h>
#include <i386/machine_routines.h>
#if HYPERVISOR
#include <kern/hv_support.h>
#endif
#define ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(_type_) \
extern char assert_is_16byte_multiple_sizeof_ ## _type_ \
[(sizeof(_type_) % 16) == 0 ? 1 : -1]
ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(x86_64_intr_stack_frame_t);
ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(x86_saved_state_t);
#define DIRECTION_FLAG_DEBUG (DEBUG | DEVELOPMENT)
extern zone_t iss_zone;
extern zone_t ids_zone;
void
act_machine_switch_pcb(__unused thread_t old, thread_t new)
{
pcb_t pcb = THREAD_TO_PCB(new);
cpu_data_t *cdp = current_cpu_datap();
struct real_descriptor *ldtp;
mach_vm_offset_t pcb_stack_top;
assert(new->kernel_stack != 0);
assert(ml_get_interrupts_enabled() == FALSE);
#ifdef DIRECTION_FLAG_DEBUG
if (x86_get_flags() & EFL_DF) {
panic("Direction flag detected: 0x%lx", x86_get_flags());
}
#endif
set_ds(NULL_SEG);
set_es(NULL_SEG);
set_fs(NULL_SEG);
if (get_gs() != NULL_SEG) {
swapgs();
set_gs(NULL_SEG);
swapgs();
cdp->cpu_uber.cu_user_gs_base = 0;
}
vm_offset_t isf;
isf = (vm_offset_t) &pcb->iss->ss_64.isf;
cdp->cpu_uber.cu_isf = isf;
pcb_stack_top = (vm_offset_t) (pcb->iss + 1);
assert((pcb_stack_top & 0xF) == 0);
current_ktss64()->rsp0 = pcb_stack_top;
*current_sstk64() = pcb_stack_top;
if (is_saved_state64(pcb->iss)) {
cdp->cpu_task_map = new->map->pmap->pm_task_map;
ldt_desc_p(USER64_CS)->access |= ACC_PL_U;
ldt_desc_p(USER_CS)->access &= ~ACC_PL_U;
if ((pcb->cthread_self != 0) || (new->task != kernel_task)) {
if ((cdp->cpu_uber.cu_user_gs_base != pcb->cthread_self) || (pcb->cthread_self != rdmsr64(MSR_IA32_KERNEL_GS_BASE))) {
cdp->cpu_uber.cu_user_gs_base = pcb->cthread_self;
wrmsr64(MSR_IA32_KERNEL_GS_BASE, pcb->cthread_self);
}
}
} else {
cdp->cpu_task_map = TASK_MAP_32BIT;
ldt_desc_p(USER64_CS)->access &= ~ACC_PL_U;
ldt_desc_p(USER_CS)->access |= ACC_PL_U;
ldtp = (struct real_descriptor *)current_ldt();
ldtp[sel_idx(USER_CTHREAD)] = pcb->cthread_desc;
if (pcb->uldt_selector != 0)
ldtp[sel_idx(pcb->uldt_selector)] = pcb->uldt_desc;
cdp->cpu_uber.cu_user_gs_base = pcb->cthread_self;
if (new->task == TASK_NULL || new->task->i386_ldt == 0) {
ml_cpu_set_ldt(KERNEL_LDT);
} else {
user_ldt_set(new);
}
}
commpage_sched_gen_inc();
}
kern_return_t
thread_set_wq_state32(thread_t thread, thread_state_t tstate)
{
x86_thread_state32_t *state;
x86_saved_state32_t *saved_state;
thread_t curth = current_thread();
spl_t s=0;
pal_register_cache_state(thread, DIRTY);
saved_state = USER_REGS32(thread);
state = (x86_thread_state32_t *)tstate;
if (curth != thread) {
s = splsched();
thread_lock(thread);
}
saved_state->ebp = 0;
saved_state->eip = state->eip;
saved_state->eax = state->eax;
saved_state->ebx = state->ebx;
saved_state->ecx = state->ecx;
saved_state->edx = state->edx;
saved_state->edi = state->edi;
saved_state->esi = state->esi;
saved_state->uesp = state->esp;
saved_state->efl = EFL_USER_SET;
saved_state->cs = USER_CS;
saved_state->ss = USER_DS;
saved_state->ds = USER_DS;
saved_state->es = USER_DS;
if (curth != thread) {
thread_unlock(thread);
splx(s);
}
return KERN_SUCCESS;
}
kern_return_t
thread_set_wq_state64(thread_t thread, thread_state_t tstate)
{
x86_thread_state64_t *state;
x86_saved_state64_t *saved_state;
thread_t curth = current_thread();
spl_t s=0;
saved_state = USER_REGS64(thread);
state = (x86_thread_state64_t *)tstate;
if (!IS_USERADDR64_CANONICAL(state->rsp) ||
!IS_USERADDR64_CANONICAL(state->rip)) {
return KERN_FAILURE;
}
pal_register_cache_state(thread, DIRTY);
if (curth != thread) {
s = splsched();
thread_lock(thread);
}
saved_state->rbp = 0;
saved_state->rdi = state->rdi;
saved_state->rsi = state->rsi;
saved_state->rdx = state->rdx;
saved_state->rcx = state->rcx;
saved_state->r8 = state->r8;
saved_state->r9 = state->r9;
saved_state->isf.rip = state->rip;
saved_state->isf.rsp = state->rsp;
saved_state->isf.cs = USER64_CS;
saved_state->isf.rflags = EFL_USER_SET;
if (curth != thread) {
thread_unlock(thread);
splx(s);
}
return KERN_SUCCESS;
}
kern_return_t
machine_thread_create(
thread_t thread,
task_t task)
{
pcb_t pcb = THREAD_TO_PCB(thread);
#if NCOPY_WINDOWS > 0
inval_copy_windows(thread);
thread->machine.physwindow_pte = 0;
thread->machine.physwindow_busy = 0;
#endif
if (pcb->iss == NULL) {
assert((get_preemption_level() == 0));
pcb->iss = (x86_saved_state_t *) zalloc(iss_zone);
if (pcb->iss == NULL)
panic("iss_zone");
}
assert(sizeof(pcb->iss->ss_32) + sizeof(pcb->iss->ss_64.isf) <=
sizeof(pcb->iss->ss_64));
bzero((char *)pcb->iss, sizeof(x86_saved_state_t));
if (task_has_64BitAddr(task)) {
pcb->iss->flavor = x86_SAVED_STATE64;
pcb->iss->ss_64.isf.cs = USER64_CS;
pcb->iss->ss_64.isf.ss = USER_DS;
pcb->iss->ss_64.fs = USER_DS;
pcb->iss->ss_64.gs = USER_DS;
pcb->iss->ss_64.isf.rflags = EFL_USER_SET;
} else {
pcb->iss->flavor = x86_SAVED_STATE32;
pcb->iss->ss_32.cs = USER_CS;
pcb->iss->ss_32.ss = USER_DS;
pcb->iss->ss_32.ds = USER_DS;
pcb->iss->ss_32.es = USER_DS;
pcb->iss->ss_32.fs = USER_DS;
pcb->iss->ss_32.gs = USER_DS;
pcb->iss->ss_32.efl = EFL_USER_SET;
}
simple_lock_init(&pcb->lock, 0);
pcb->cthread_self = 0;
pcb->uldt_selector = 0;
pcb->thread_gpu_ns = 0;
if ((pcb->cthread_desc.access & ACC_P) == 0) {
struct real_descriptor *ldtp;
ldtp = (struct real_descriptor *)current_ldt();
pcb->cthread_desc = ldtp[sel_idx(USER_DS)];
}
return(KERN_SUCCESS);
}
void
machine_thread_destroy(
thread_t thread)
{
register pcb_t pcb = THREAD_TO_PCB(thread);
#if HYPERVISOR
if (thread->hv_thread_target) {
hv_callbacks.thread_destroy(thread->hv_thread_target);
thread->hv_thread_target = NULL;
}
#endif
if (pcb->ifps != 0)
fpu_free(pcb->ifps);
if (pcb->iss != 0) {
zfree(iss_zone, pcb->iss);
pcb->iss = 0;
}
if (pcb->ids) {
zfree(ids_zone, pcb->ids);
pcb->ids = NULL;
}
}
kern_return_t
machine_thread_set_tsd_base(
thread_t thread,
mach_vm_offset_t tsd_base)
{
if (thread->task == kernel_task) {
return KERN_INVALID_ARGUMENT;
}
if (thread_is_64bit(thread)) {
if (!IS_USERADDR64_CANONICAL(tsd_base))
tsd_base = 0ULL;
} else {
if (tsd_base > UINT32_MAX)
tsd_base = 0ULL;
}
pcb_t pcb = THREAD_TO_PCB(thread);
pcb->cthread_self = tsd_base;
if (!thread_is_64bit(thread)) {
struct real_descriptor desc = {
.limit_low = 1,
.limit_high = 0,
.base_low = tsd_base & 0xffff,
.base_med = (tsd_base >> 16) & 0xff,
.base_high = (tsd_base >> 24) & 0xff,
.access = ACC_P|ACC_PL_U|ACC_DATA_W,
.granularity = SZ_32|SZ_G,
};
pcb->cthread_desc = desc;
saved_state32(pcb->iss)->gs = USER_CTHREAD;
}
if (thread == current_thread()) {
if (thread_is_64bit(thread)) {
cpu_data_t *cdp;
mp_disable_preemption();
cdp = current_cpu_datap();
if ((cdp->cpu_uber.cu_user_gs_base != pcb->cthread_self) ||
(pcb->cthread_self != rdmsr64(MSR_IA32_KERNEL_GS_BASE)))
wrmsr64(MSR_IA32_KERNEL_GS_BASE, tsd_base);
cdp->cpu_uber.cu_user_gs_base = tsd_base;
mp_enable_preemption();
} else {
mp_disable_preemption();
*ldt_desc_p(USER_CTHREAD) = pcb->cthread_desc;
mp_enable_preemption();
}
}
return KERN_SUCCESS;
}