+}
+
+
+kern_return_t
+thread_compose_cthread_desc(unsigned int addr, pcb_t pcb)
+{
+ struct real_descriptor desc;
+
+ mp_disable_preemption();
+
+ desc.limit_low = 1;
+ desc.limit_high = 0;
+ desc.base_low = addr & 0xffff;
+ desc.base_med = (addr >> 16) & 0xff;
+ desc.base_high = (addr >> 24) & 0xff;
+ desc.access = ACC_P|ACC_PL_U|ACC_DATA_W;
+ desc.granularity = SZ_32|SZ_G;
+ pcb->cthread_desc = desc;
+ *ldt_desc_p(USER_CTHREAD) = desc;
+
+ mp_enable_preemption();
+
+ return(KERN_SUCCESS);
+}
+
+kern_return_t
+thread_set_cthread_self(uint32_t self)
+{
+ current_thread()->machine.pcb->cthread_self = (uint64_t) self;
+
+ return (KERN_SUCCESS);
+}
+
+kern_return_t
+thread_get_cthread_self(void)
+{
+ return ((kern_return_t)current_thread()->machine.pcb->cthread_self);
+}
+
+kern_return_t
+thread_fast_set_cthread_self(uint32_t self)
+{
+ pcb_t pcb;
+ x86_saved_state32_t *iss;
+
+ pcb = (pcb_t)current_thread()->machine.pcb;
+ thread_compose_cthread_desc(self, pcb);
+ pcb->cthread_self = (uint64_t) self; /* preserve old func too */
+ iss = saved_state32(pcb->iss);
+ iss->gs = USER_CTHREAD;
+
+ return (USER_CTHREAD);
+}
+
+kern_return_t
+thread_fast_set_cthread_self64(uint64_t self)
+{
+ pcb_t pcb;
+ x86_saved_state64_t *iss;
+
+ pcb = current_thread()->machine.pcb;
+
+ /* check for canonical address, set 0 otherwise */
+ if (!IS_USERADDR64_CANONICAL(self))
+ self = 0ULL;
+ pcb->cthread_self = self;
+ current_cpu_datap()->cpu_uber.cu_user_gs_base = self;
+
+ /* XXX for 64-in-32 */
+ iss = saved_state64(pcb->iss);
+ iss->gs = USER_CTHREAD;
+ thread_compose_cthread_desc((uint32_t) self, pcb);
+
+ return (USER_CTHREAD);
+}
+
+/*
+ * thread_set_user_ldt routine is the interface for the user level
+ * settable ldt entry feature. allowing a user to create arbitrary
+ * ldt entries seems to be too large of a security hole, so instead
+ * this mechanism is in place to allow user level processes to have
+ * an ldt entry that can be used in conjunction with the FS register.
+ *
+ * Swapping occurs inside the pcb.c file along with initialization
+ * when a thread is created. The basic functioning theory is that the
+ * pcb->uldt_selector variable will contain either 0 meaning the
+ * process has not set up any entry, or the selector to be used in
+ * the FS register. pcb->uldt_desc contains the actual descriptor the
+ * user has set up stored in machine usable ldt format.
+ *
+ * Currently one entry is shared by all threads (USER_SETTABLE), but
+ * this could be changed in the future by changing how this routine
+ * allocates the selector. There seems to be no real reason at this
+ * time to have this added feature, but in the future it might be
+ * needed.
+ *
+ * address is the linear address of the start of the data area size
+ * is the size in bytes of the area flags should always be set to 0
+ * for now. in the future it could be used to set R/W permisions or
+ * other functions. Currently the segment is created as a data segment
+ * up to 1 megabyte in size with full read/write permisions only.
+ *
+ * this call returns the segment selector or -1 if any error occurs
+ */
+kern_return_t
+thread_set_user_ldt(uint32_t address, uint32_t size, uint32_t flags)
+{
+ pcb_t pcb;
+ struct fake_descriptor temp;
+ int mycpu;
+
+ if (flags != 0)
+ return -1; // flags not supported
+ if (size > 0xFFFFF)
+ return -1; // size too big, 1 meg is the limit
+
+ mp_disable_preemption();
+ mycpu = cpu_number();