1 /*
2 * Copyright (c) 2010-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <mach_ldebug.h>
29
30 #include <mach/kern_return.h>
31 #include <mach/mach_traps.h>
32 #include <mach/thread_status.h>
33 #include <mach/vm_param.h>
34
35 #include <kern/cpu_data.h>
36 #include <kern/mach_param.h>
37 #include <kern/task.h>
38 #include <kern/thread.h>
39 #include <kern/sched_prim.h>
40 #include <kern/misc_protos.h>
41 #include <kern/assert.h>
42 #include <kern/debug.h>
43 #include <kern/spl.h>
44 #include <kern/syscall_sw.h>
45 #include <ipc/ipc_port.h>
46 #include <vm/vm_kern.h>
47 #include <vm/pmap.h>
48
49 #include <i386/cpu_number.h>
50 #include <i386/eflags.h>
51 #include <i386/proc_reg.h>
52 #include <i386/tss.h>
53 #include <i386/user_ldt.h>
54 #include <i386/fpu.h>
55 #include <i386/machdep_call.h>
56 #include <i386/vmparam.h>
57 #include <i386/mp_desc.h>
58 #include <i386/misc_protos.h>
59 #include <i386/thread.h>
60 #include <i386/trap_internal.h>
61 #include <i386/seg.h>
62 #include <mach/i386/syscall_sw.h>
63 #include <sys/syscall.h>
64 #include <sys/kdebug.h>
65 #include <sys/errno.h>
66 #include <../bsd/sys/sysent.h>
67
68
69 /*
70 * Duplicate parent state in child
71 * for U**X fork.
72 */
73 kern_return_t
machine_thread_dup(thread_t parent,thread_t child,__unused boolean_t is_corpse)74 machine_thread_dup(
75 thread_t parent,
76 thread_t child,
77 __unused boolean_t is_corpse
78 )
79 {
80 pcb_t parent_pcb = THREAD_TO_PCB(parent);
81 pcb_t child_pcb = THREAD_TO_PCB(child);
82
83 /*
84 * Copy over the x86_saved_state registers
85 */
86 if (thread_is_64bit_addr(parent)) {
87 bcopy(USER_REGS64(parent), USER_REGS64(child), sizeof(x86_saved_state64_t));
88 } else {
89 bcopy(USER_REGS32(parent), USER_REGS32(child), sizeof(x86_saved_state32_t));
90 }
91
92 /*
93 * Check to see if parent is using floating point
94 * and if so, copy the registers to the child
95 */
96 fpu_dup_fxstate(parent, child);
97
98 #ifdef MACH_BSD
99 /*
100 * Copy the parent's cthread id and USER_CTHREAD descriptor, if 32-bit.
101 */
102 child_pcb->cthread_self = parent_pcb->cthread_self;
103 if (!thread_is_64bit_addr(parent)) {
104 child_pcb->cthread_desc = parent_pcb->cthread_desc;
105 }
106
107 /*
108 * FIXME - should a user specified LDT, TSS and V86 info
109 * be duplicated as well?? - probably not.
110 */
111 // duplicate any use LDT entry that was set I think this is appropriate.
112 if (parent_pcb->uldt_selector != 0) {
113 child_pcb->uldt_selector = parent_pcb->uldt_selector;
114 child_pcb->uldt_desc = parent_pcb->uldt_desc;
115 }
116 #endif
117
118 return KERN_SUCCESS;
119 }
120
121 /*
122 * thread_fast_set_cthread_self: Sets the machine kernel thread ID of the
123 * current thread to the given thread ID; fast version for 32-bit processes
124 *
125 * Parameters: self Thread ID to set
126 *
127 * Returns: 0 Success
128 * !0 Not success
129 */
130 kern_return_t
thread_fast_set_cthread_self(uint32_t self)131 thread_fast_set_cthread_self(uint32_t self)
132 {
133 machine_thread_set_tsd_base(current_thread(), self);
134 return USER_CTHREAD; /* N.B.: not a kern_return_t! */
135 }
136
137 /*
138 * thread_fast_set_cthread_self64: Sets the machine kernel thread ID of the
139 * current thread to the given thread ID; fast version for 64-bit processes
140 *
141 * Parameters: self Thread ID
142 *
143 * Returns: 0 Success
144 * !0 Not success
145 */
146 kern_return_t
thread_fast_set_cthread_self64(uint64_t self)147 thread_fast_set_cthread_self64(uint64_t self)
148 {
149 machine_thread_set_tsd_base(current_thread(), self);
150 return USER_CTHREAD; /* N.B.: not a kern_return_t! */
151 }
152
153 /*
154 * thread_set_user_ldt routine is the interface for the user level
155 * settable ldt entry feature. allowing a user to create arbitrary
156 * ldt entries seems to be too large of a security hole, so instead
157 * this mechanism is in place to allow user level processes to have
158 * an ldt entry that can be used in conjunction with the FS register.
159 *
160 * Swapping occurs inside the pcb.c file along with initialization
161 * when a thread is created. The basic functioning theory is that the
162 * pcb->uldt_selector variable will contain either 0 meaning the
163 * process has not set up any entry, or the selector to be used in
164 * the FS register. pcb->uldt_desc contains the actual descriptor the
165 * user has set up stored in machine usable ldt format.
166 *
167 * Currently one entry is shared by all threads (USER_SETTABLE), but
168 * this could be changed in the future by changing how this routine
169 * allocates the selector. There seems to be no real reason at this
170 * time to have this added feature, but in the future it might be
171 * needed.
172 *
173 * address is the linear address of the start of the data area size
174 * is the size in bytes of the area flags should always be set to 0
175 * for now. in the future it could be used to set R/W permisions or
176 * other functions. Currently the segment is created as a data segment
177 * up to 1 megabyte in size with full read/write permisions only.
178 *
179 * this call returns the segment selector or -1 if any error occurs
180 */
181 kern_return_t
thread_set_user_ldt(uint32_t address,uint32_t size,uint32_t flags)182 thread_set_user_ldt(uint32_t address, uint32_t size, uint32_t flags)
183 {
184 pcb_t pcb;
185 struct fake_descriptor temp;
186
187 if (flags != 0) {
188 return -1; // flags not supported
189 }
190 if (size > 0xFFFFF) {
191 return -1; // size too big, 1 meg is the limit
192 }
193 mp_disable_preemption();
194
195 // create a "fake" descriptor so we can use fix_desc()
196 // to build a real one...
197 // 32 bit default operation size
198 // standard read/write perms for a data segment
199 pcb = THREAD_TO_PCB(current_thread());
200 temp.offset = address;
201 temp.lim_or_seg = size;
202 temp.size_or_wdct = SZ_32;
203 temp.access = ACC_P | ACC_PL_U | ACC_DATA_W;
204
205 // turn this into a real descriptor
206 fix_desc(&temp, 1);
207
208 // set up our data in the pcb
209 pcb->uldt_desc = *(struct real_descriptor*)&temp;
210 pcb->uldt_selector = USER_SETTABLE; // set the selector value
211
212 // now set it up in the current table...
213 *ldt_desc_p(USER_SETTABLE) = *(struct real_descriptor*)&temp;
214
215 mp_enable_preemption();
216
217 return USER_SETTABLE;
218 }
219