]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/machine_task.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / i386 / machine_task.c
1 /*
2 * Copyright (c) 2000-2016 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 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57 #include <kern/task.h>
58 #include <kern/thread.h>
59 #include <i386/misc_protos.h>
60 #include <i386/fpu.h>
61
62 #if HYPERVISOR
63 #include <kern/hv_support.h>
64 #endif
65
66 extern zone_t ids_zone;
67
68 kern_return_t
69 machine_task_set_state(
70 task_t task,
71 int flavor,
72 thread_state_t state,
73 mach_msg_type_number_t state_count)
74 {
75 switch (flavor) {
76 case x86_DEBUG_STATE32:
77 {
78 x86_debug_state32_t *tstate = (x86_debug_state32_t*) state;
79 if ((task_has_64Bit_addr(task)) ||
80 (state_count != x86_DEBUG_STATE32_COUNT) ||
81 (!debug_state_is_valid32(tstate))) {
82 return KERN_INVALID_ARGUMENT;
83 }
84
85 if (task->task_debug == NULL) {
86 task->task_debug = zalloc(ids_zone);
87 }
88
89 copy_debug_state32(tstate, (x86_debug_state32_t*) task->task_debug, FALSE);
90
91 return KERN_SUCCESS;
92 }
93 case x86_DEBUG_STATE64:
94 {
95 x86_debug_state64_t *tstate = (x86_debug_state64_t*) state;
96
97 if ((!task_has_64Bit_addr(task)) ||
98 (state_count != x86_DEBUG_STATE64_COUNT) ||
99 (!debug_state_is_valid64(tstate))) {
100 return KERN_INVALID_ARGUMENT;
101 }
102
103 if (task->task_debug == NULL) {
104 task->task_debug = zalloc(ids_zone);
105 }
106
107 copy_debug_state64(tstate, (x86_debug_state64_t*) task->task_debug, FALSE);
108
109 return KERN_SUCCESS;
110 }
111 case x86_DEBUG_STATE:
112 {
113 x86_debug_state_t *tstate = (x86_debug_state_t*) state;
114
115 if (state_count != x86_DEBUG_STATE_COUNT) {
116 return KERN_INVALID_ARGUMENT;
117 }
118
119 if ((tstate->dsh.flavor == x86_DEBUG_STATE32) &&
120 (tstate->dsh.count == x86_DEBUG_STATE32_COUNT) &&
121 (!task_has_64Bit_addr(task)) &&
122 debug_state_is_valid32(&tstate->uds.ds32)) {
123 if (task->task_debug == NULL) {
124 task->task_debug = zalloc(ids_zone);
125 }
126
127 copy_debug_state32(&tstate->uds.ds32, (x86_debug_state32_t*) task->task_debug, FALSE);
128 return KERN_SUCCESS;
129 } else if ((tstate->dsh.flavor == x86_DEBUG_STATE64) &&
130 (tstate->dsh.count == x86_DEBUG_STATE64_COUNT) &&
131 task_has_64Bit_addr(task) &&
132 debug_state_is_valid64(&tstate->uds.ds64)) {
133 if (task->task_debug == NULL) {
134 task->task_debug = zalloc(ids_zone);
135 }
136
137 copy_debug_state64(&tstate->uds.ds64, (x86_debug_state64_t*) task->task_debug, FALSE);
138 return KERN_SUCCESS;
139 } else {
140 return KERN_INVALID_ARGUMENT;
141 }
142 }
143 default:
144 {
145 return KERN_INVALID_ARGUMENT;
146 }
147 }
148 }
149
150 kern_return_t
151 machine_task_get_state(task_t task,
152 int flavor,
153 thread_state_t state,
154 mach_msg_type_number_t *state_count)
155 {
156 switch (flavor) {
157 case x86_DEBUG_STATE32:
158 {
159 x86_debug_state32_t *tstate = (x86_debug_state32_t*) state;
160
161 if ((task_has_64Bit_addr(task)) || (*state_count != x86_DEBUG_STATE32_COUNT)) {
162 return KERN_INVALID_ARGUMENT;
163 }
164
165 if (task->task_debug == NULL) {
166 bzero(state, sizeof(*tstate));
167 } else {
168 copy_debug_state32((x86_debug_state32_t*) task->task_debug, tstate, TRUE);
169 }
170
171 return KERN_SUCCESS;
172 }
173 case x86_DEBUG_STATE64:
174 {
175 x86_debug_state64_t *tstate = (x86_debug_state64_t*) state;
176
177 if ((!task_has_64Bit_addr(task)) || (*state_count != x86_DEBUG_STATE64_COUNT)) {
178 return KERN_INVALID_ARGUMENT;
179 }
180
181 if (task->task_debug == NULL) {
182 bzero(state, sizeof(*tstate));
183 } else {
184 copy_debug_state64((x86_debug_state64_t*) task->task_debug, tstate, TRUE);
185 }
186
187 return KERN_SUCCESS;
188 }
189 case x86_DEBUG_STATE:
190 {
191 x86_debug_state_t *tstate = (x86_debug_state_t*)state;
192
193 if (*state_count != x86_DEBUG_STATE_COUNT) {
194 return KERN_INVALID_ARGUMENT;
195 }
196
197 if (task_has_64Bit_addr(task)) {
198 tstate->dsh.flavor = x86_DEBUG_STATE64;
199 tstate->dsh.count = x86_DEBUG_STATE64_COUNT;
200
201 if (task->task_debug == NULL) {
202 bzero(&tstate->uds.ds64, sizeof(tstate->uds.ds64));
203 } else {
204 copy_debug_state64((x86_debug_state64_t*)task->task_debug, &tstate->uds.ds64, TRUE);
205 }
206 } else {
207 tstate->dsh.flavor = x86_DEBUG_STATE32;
208 tstate->dsh.count = x86_DEBUG_STATE32_COUNT;
209
210 if (task->task_debug == NULL) {
211 bzero(&tstate->uds.ds32, sizeof(tstate->uds.ds32));
212 } else {
213 copy_debug_state32((x86_debug_state32_t*)task->task_debug, &tstate->uds.ds32, TRUE);
214 }
215 }
216
217 return KERN_SUCCESS;
218 }
219 default:
220 {
221 return KERN_INVALID_ARGUMENT;
222 }
223 }
224 }
225
226 /*
227 * This is called when a task is terminated, and also on exec().
228 * Clear machine-dependent state that is stored on the task.
229 */
230 void
231 machine_task_terminate(task_t task)
232 {
233 if (task) {
234 user_ldt_t user_ldt;
235 void *task_debug;
236
237 #if HYPERVISOR
238 if (task->hv_task_target) {
239 hv_callbacks.task_destroy(task->hv_task_target);
240 task->hv_task_target = NULL;
241 }
242 #endif
243
244 user_ldt = task->i386_ldt;
245 if (user_ldt != 0) {
246 task->i386_ldt = 0;
247 user_ldt_free(user_ldt);
248 }
249
250 task_debug = task->task_debug;
251 if (task_debug != NULL) {
252 task->task_debug = NULL;
253 zfree(ids_zone, task_debug);
254 }
255 }
256 }
257
258 /*
259 * Set initial default state on a thread as stored in the MACHINE_TASK data.
260 * Note: currently only debug state is supported.
261 */
262 kern_return_t
263 machine_thread_inherit_taskwide(
264 thread_t thread,
265 task_t parent_task)
266 {
267 if (parent_task->task_debug) {
268 int flavor;
269 mach_msg_type_number_t count;
270
271 if (task_has_64Bit_addr(parent_task)) {
272 flavor = x86_DEBUG_STATE64;
273 count = x86_DEBUG_STATE64_COUNT;
274 } else {
275 flavor = x86_DEBUG_STATE32;
276 count = x86_DEBUG_STATE32_COUNT;
277 }
278
279 return machine_thread_set_state(thread, flavor, parent_task->task_debug, count);
280 }
281
282 return KERN_SUCCESS;
283 }
284
285 void
286 machine_task_init(task_t new_task,
287 task_t parent_task,
288 boolean_t inherit_memory)
289 {
290 new_task->uexc_range_start = 0;
291 new_task->uexc_range_size = 0;
292 new_task->uexc_handler = 0;
293
294 new_task->i386_ldt = 0;
295
296 if (parent_task != TASK_NULL) {
297 if (inherit_memory && parent_task->i386_ldt) {
298 new_task->i386_ldt = user_ldt_copy(parent_task->i386_ldt);
299 }
300 new_task->xstate = parent_task->xstate;
301 } else {
302 assert(fpu_default != UNDEFINED);
303 new_task->xstate = fpu_default;
304 }
305 }