]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/machine_task.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / i386 / machine_task.c
CommitLineData
b0d623f7 1/*
39037602 2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
b0d623f7
A
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.
0a7de745 14 *
b0d623f7
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
b0d623f7
A
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.
0a7de745 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
b0d623f7
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
b0d623f7
A
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.
0a7de745 41 *
b0d623f7
A
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.
0a7de745 45 *
b0d623f7 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
b0d623f7
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
b0d623f7
A
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>
5ba3f43e 60#include <i386/fpu.h>
b0d623f7 61
fe8ab488
A
62#if HYPERVISOR
63#include <kern/hv_support.h>
64#endif
65
b0d623f7
A
66extern zone_t ids_zone;
67
68kern_return_t
69machine_task_set_state(
0a7de745
A
70 task_t task,
71 int flavor,
72 thread_state_t state,
73 mach_msg_type_number_t state_count)
b0d623f7
A
74{
75 switch (flavor) {
0a7de745
A
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 }
b0d623f7 84
0a7de745
A
85 if (task->task_debug == NULL) {
86 task->task_debug = zalloc(ids_zone);
87 }
b0d623f7 88
0a7de745
A
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;
b0d623f7 101 }
b0d623f7 102
0a7de745
A
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)) {
b0d623f7
A
123 if (task->task_debug == NULL) {
124 task->task_debug = zalloc(ids_zone);
125 }
b0d623f7 126
0a7de745
A
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);
b0d623f7
A
135 }
136
0a7de745
A
137 copy_debug_state64(&tstate->uds.ds64, (x86_debug_state64_t*) task->task_debug, FALSE);
138 return KERN_SUCCESS;
139 } else {
b0d623f7 140 return KERN_INVALID_ARGUMENT;
b0d623f7
A
141 }
142 }
0a7de745
A
143 default:
144 {
145 return KERN_INVALID_ARGUMENT;
146 }
147 }
b0d623f7
A
148}
149
0a7de745
A
150kern_return_t
151machine_task_get_state(task_t task,
152 int flavor,
153 thread_state_t state,
154 mach_msg_type_number_t *state_count)
b0d623f7
A
155{
156 switch (flavor) {
0a7de745
A
157 case x86_DEBUG_STATE32:
158 {
159 x86_debug_state32_t *tstate = (x86_debug_state32_t*) state;
b0d623f7 160
0a7de745
A
161 if ((task_has_64Bit_addr(task)) || (*state_count != x86_DEBUG_STATE32_COUNT)) {
162 return KERN_INVALID_ARGUMENT;
163 }
b0d623f7 164
0a7de745
A
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 }
b0d623f7 170
0a7de745
A
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;
b0d623f7 179 }
b0d623f7 180
0a7de745
A
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;
b0d623f7
A
200
201 if (task->task_debug == NULL) {
0a7de745 202 bzero(&tstate->uds.ds64, sizeof(tstate->uds.ds64));
b0d623f7 203 } else {
0a7de745
A
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;
b0d623f7 209
0a7de745
A
210 if (task->task_debug == NULL) {
211 bzero(&tstate->uds.ds32, sizeof(tstate->uds.ds32));
b0d623f7 212 } else {
0a7de745 213 copy_debug_state32((x86_debug_state32_t*)task->task_debug, &tstate->uds.ds32, TRUE);
b0d623f7 214 }
b0d623f7 215 }
0a7de745
A
216
217 return KERN_SUCCESS;
218 }
219 default:
220 {
221 return KERN_INVALID_ARGUMENT;
222 }
b0d623f7
A
223 }
224}
225
316670eb
A
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 */
230void
231machine_task_terminate(task_t task)
232{
233 if (task) {
234 user_ldt_t user_ldt;
235 void *task_debug;
236
fe8ab488
A
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
316670eb
A
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);
0a7de745 254 }
316670eb
A
255 }
256}
257
b0d623f7
A
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 */
262kern_return_t
263machine_thread_inherit_taskwide(
0a7de745
A
264 thread_t thread,
265 task_t parent_task)
b0d623f7
A
266{
267 if (parent_task->task_debug) {
268 int flavor;
269 mach_msg_type_number_t count;
270
d9a64523 271 if (task_has_64Bit_addr(parent_task)) {
b0d623f7
A
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}
5ba3f43e
A
284
285void
286machine_task_init(task_t new_task,
0a7de745
A
287 task_t parent_task,
288 boolean_t inherit_memory)
5ba3f43e
A
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) {
0a7de745 297 if (inherit_memory && parent_task->i386_ldt) {
5ba3f43e 298 new_task->i386_ldt = user_ldt_copy(parent_task->i386_ldt);
0a7de745 299 }
5ba3f43e
A
300 new_task->xstate = parent_task->xstate;
301 } else {
302 assert(fpu_default != UNDEFINED);
303 new_task->xstate = fpu_default;
304 }
305}