]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/machine_task.c
xnu-3789.60.24.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@
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
fe8ab488
A
61#if HYPERVISOR
62#include <kern/hv_support.h>
63#endif
64
b0d623f7
A
65extern zone_t ids_zone;
66
67kern_return_t
68machine_task_set_state(
69 task_t task,
70 int flavor,
71 thread_state_t state,
72 mach_msg_type_number_t state_count)
73{
74 switch (flavor) {
75 case x86_DEBUG_STATE32:
76 {
77 x86_debug_state32_t *tstate = (x86_debug_state32_t*) state;
78 if ((task_has_64BitAddr(task)) ||
79 (state_count != x86_DEBUG_STATE32_COUNT) ||
80 (!debug_state_is_valid32(tstate))) {
81 return KERN_INVALID_ARGUMENT;
82 }
83
84 if (task->task_debug == NULL) {
85 task->task_debug = zalloc(ids_zone);
86 }
87
88 copy_debug_state32(tstate, (x86_debug_state32_t*) task->task_debug, FALSE);
89
90 return KERN_SUCCESS;
b0d623f7
A
91 }
92 case x86_DEBUG_STATE64:
93 {
94 x86_debug_state64_t *tstate = (x86_debug_state64_t*) state;
95
96 if ((!task_has_64BitAddr(task)) ||
97 (state_count != x86_DEBUG_STATE64_COUNT) ||
98 (!debug_state_is_valid64(tstate))) {
99 return KERN_INVALID_ARGUMENT;
100 }
101
102 if (task->task_debug == NULL) {
103 task->task_debug = zalloc(ids_zone);
104 }
105
106 copy_debug_state64(tstate, (x86_debug_state64_t*) task->task_debug, FALSE);
107
108 return KERN_SUCCESS;
b0d623f7
A
109 }
110 case x86_DEBUG_STATE:
111 {
112 x86_debug_state_t *tstate = (x86_debug_state_t*) state;
113
114 if (state_count != x86_DEBUG_STATE_COUNT) {
115 return KERN_INVALID_ARGUMENT;
116 }
117
118 if ((tstate->dsh.flavor == x86_DEBUG_STATE32) &&
119 (tstate->dsh.count == x86_DEBUG_STATE32_COUNT) &&
120 (!task_has_64BitAddr(task)) &&
121 debug_state_is_valid32(&tstate->uds.ds32)) {
122
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
130 } else if ((tstate->dsh.flavor == x86_DEBUG_STATE64) &&
131 (tstate->dsh.count == x86_DEBUG_STATE64_COUNT) &&
132 task_has_64BitAddr(task) &&
133 debug_state_is_valid64(&tstate->uds.ds64)) {
134
135 if (task->task_debug == NULL) {
136 task->task_debug = zalloc(ids_zone);
137 }
138
139 copy_debug_state64(&tstate->uds.ds64, (x86_debug_state64_t*) task->task_debug, FALSE);
140 return KERN_SUCCESS;
141 } else {
142 return KERN_INVALID_ARGUMENT;
143 }
b0d623f7
A
144 }
145 default:
146 {
147 return KERN_INVALID_ARGUMENT;
b0d623f7
A
148 }
149 }
150}
151
152kern_return_t
153machine_task_get_state(task_t task,
154 int flavor,
155 thread_state_t state,
156 mach_msg_type_number_t *state_count)
157{
158 switch (flavor) {
159 case x86_DEBUG_STATE32:
160 {
161 x86_debug_state32_t *tstate = (x86_debug_state32_t*) state;
162
163 if ((task_has_64BitAddr(task)) || (*state_count != x86_DEBUG_STATE32_COUNT)) {
164 return KERN_INVALID_ARGUMENT;
165 }
166
167 if (task->task_debug == NULL) {
168 bzero(state, sizeof(*tstate));
169 } else {
170 copy_debug_state32((x86_debug_state32_t*) task->task_debug, tstate, TRUE);
171 }
172
173 return KERN_SUCCESS;
b0d623f7
A
174 }
175 case x86_DEBUG_STATE64:
176 {
177 x86_debug_state64_t *tstate = (x86_debug_state64_t*) state;
178
179 if ((!task_has_64BitAddr(task)) || (*state_count != x86_DEBUG_STATE64_COUNT)) {
180 return KERN_INVALID_ARGUMENT;
181 }
182
183 if (task->task_debug == NULL) {
184 bzero(state, sizeof(*tstate));
185 } else {
186 copy_debug_state64((x86_debug_state64_t*) task->task_debug, tstate, TRUE);
187 }
188
189 return KERN_SUCCESS;
b0d623f7
A
190 }
191 case x86_DEBUG_STATE:
192 {
193 x86_debug_state_t *tstate = (x86_debug_state_t*)state;
194
195 if (*state_count != x86_DEBUG_STATE_COUNT)
196 return(KERN_INVALID_ARGUMENT);
197
198 if (task_has_64BitAddr(task)) {
199 tstate->dsh.flavor = x86_DEBUG_STATE64;
200 tstate->dsh.count = x86_DEBUG_STATE64_COUNT;
201
202 if (task->task_debug == NULL) {
203 bzero(&tstate->uds.ds64, sizeof(tstate->uds.ds64));
204 } else {
205 copy_debug_state64((x86_debug_state64_t*)task->task_debug, &tstate->uds.ds64, TRUE);
206 }
207 } else {
208 tstate->dsh.flavor = x86_DEBUG_STATE32;
209 tstate->dsh.count = x86_DEBUG_STATE32_COUNT;
210
211 if (task->task_debug == NULL) {
212 bzero(&tstate->uds.ds32, sizeof(tstate->uds.ds32));
213 } else {
214 copy_debug_state32((x86_debug_state32_t*)task->task_debug, &tstate->uds.ds32, TRUE);
215 }
216 }
217
218 return KERN_SUCCESS;
b0d623f7
A
219 }
220 default:
221 {
222 return KERN_INVALID_ARGUMENT;
b0d623f7
A
223 }
224 }
225}
226
316670eb
A
227/*
228 * This is called when a task is terminated, and also on exec().
229 * Clear machine-dependent state that is stored on the task.
230 */
231void
232machine_task_terminate(task_t task)
233{
234 if (task) {
235 user_ldt_t user_ldt;
236 void *task_debug;
237
fe8ab488
A
238#if HYPERVISOR
239 if (task->hv_task_target) {
240 hv_callbacks.task_destroy(task->hv_task_target);
241 task->hv_task_target = NULL;
242 }
243#endif
244
316670eb
A
245 user_ldt = task->i386_ldt;
246 if (user_ldt != 0) {
247 task->i386_ldt = 0;
248 user_ldt_free(user_ldt);
249 }
250
251 task_debug = task->task_debug;
252 if (task_debug != NULL) {
253 task->task_debug = NULL;
254 zfree(ids_zone, task_debug);
255 }
256 }
257}
258
b0d623f7
A
259/*
260 * Set initial default state on a thread as stored in the MACHINE_TASK data.
261 * Note: currently only debug state is supported.
262 */
263kern_return_t
264machine_thread_inherit_taskwide(
265 thread_t thread,
266 task_t parent_task)
267{
268 if (parent_task->task_debug) {
269 int flavor;
270 mach_msg_type_number_t count;
271
272 if (task_has_64BitAddr(parent_task)) {
273 flavor = x86_DEBUG_STATE64;
274 count = x86_DEBUG_STATE64_COUNT;
275 } else {
276 flavor = x86_DEBUG_STATE32;
277 count = x86_DEBUG_STATE32_COUNT;
278 }
279
280 return machine_thread_set_state(thread, flavor, parent_task->task_debug, count);
281 }
282
283 return KERN_SUCCESS;
284}