xref: /xnu-8019.80.24/osfmk/arm64/machine_task.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
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  [email protected]
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 <arm/misc_protos.h>
60 
61 
62 extern zone_t ads_zone;
63 
64 kern_return_t
machine_task_set_state(task_t task,int flavor,thread_state_t state,mach_msg_type_number_t state_count)65 machine_task_set_state(
66 	task_t task,
67 	int flavor,
68 	thread_state_t state,
69 	mach_msg_type_number_t state_count)
70 {
71 	switch (flavor) {
72 	case ARM_DEBUG_STATE:
73 	{
74 		arm_legacy_debug_state_t *tstate = (arm_legacy_debug_state_t *) state;
75 		if (task_has_64Bit_data(task) ||
76 		    (state_count != ARM_LEGACY_DEBUG_STATE_COUNT) ||
77 		    (!debug_legacy_state_is_valid(tstate))) {
78 			return KERN_INVALID_ARGUMENT;
79 		}
80 
81 		if (task->task_debug == NULL) {
82 			task->task_debug = zalloc_flags(ads_zone,
83 			    Z_WAITOK | Z_NOFAIL);
84 		}
85 
86 		copy_legacy_debug_state(tstate, (arm_legacy_debug_state_t *) task->task_debug, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
87 
88 		return KERN_SUCCESS;
89 	}
90 	case ARM_DEBUG_STATE32:
91 	{
92 		arm_debug_state32_t *tstate = (arm_debug_state32_t *) state;
93 		if (task_has_64Bit_data(task) ||
94 		    (state_count != ARM_DEBUG_STATE32_COUNT) ||
95 		    (!debug_state_is_valid32(tstate))) {
96 			return KERN_INVALID_ARGUMENT;
97 		}
98 
99 		if (task->task_debug == NULL) {
100 			task->task_debug = zalloc_flags(ads_zone,
101 			    Z_WAITOK | Z_NOFAIL);
102 		}
103 
104 		copy_debug_state32(tstate, (arm_debug_state32_t *) task->task_debug, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
105 
106 		return KERN_SUCCESS;
107 	}
108 	case ARM_DEBUG_STATE64:
109 	{
110 		arm_debug_state64_t *tstate = (arm_debug_state64_t *) state;
111 
112 		if ((!task_has_64Bit_data(task)) ||
113 		    (state_count != ARM_DEBUG_STATE64_COUNT) ||
114 		    (!debug_state_is_valid64(tstate))) {
115 			return KERN_INVALID_ARGUMENT;
116 		}
117 
118 		if (task->task_debug == NULL) {
119 			task->task_debug = zalloc_flags(ads_zone,
120 			    Z_WAITOK | Z_NOFAIL);
121 		}
122 
123 		copy_debug_state64(tstate, (arm_debug_state64_t *) task->task_debug, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
124 
125 		return KERN_SUCCESS;
126 	}
127 	case THREAD_STATE_NONE:         /* Using this flavor to clear task_debug */
128 	{
129 		if (task->task_debug != NULL) {
130 			zfree(ads_zone, task->task_debug);
131 			task->task_debug = NULL;
132 
133 			return KERN_SUCCESS;
134 		}
135 		return KERN_FAILURE;
136 	}
137 	default:
138 	{
139 		return KERN_INVALID_ARGUMENT;
140 	}
141 	}
142 
143 	return KERN_FAILURE;
144 }
145 
146 kern_return_t
machine_task_get_state(task_t task,int flavor,thread_state_t state,mach_msg_type_number_t * state_count)147 machine_task_get_state(task_t task,
148     int flavor,
149     thread_state_t state,
150     mach_msg_type_number_t *state_count)
151 {
152 	switch (flavor) {
153 	case ARM_DEBUG_STATE:
154 	{
155 		arm_legacy_debug_state_t *tstate = (arm_legacy_debug_state_t *) state;
156 
157 		if (task_has_64Bit_data(task) || (*state_count != ARM_LEGACY_DEBUG_STATE_COUNT)) {
158 			return KERN_INVALID_ARGUMENT;
159 		}
160 
161 		if (task->task_debug == NULL) {
162 			bzero(state, sizeof(*tstate));
163 		} else {
164 			copy_legacy_debug_state((arm_legacy_debug_state_t*) task->task_debug, tstate, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
165 		}
166 
167 		return KERN_SUCCESS;
168 	}
169 	case ARM_DEBUG_STATE32:
170 	{
171 		arm_debug_state32_t *tstate = (arm_debug_state32_t *) state;
172 
173 		if (task_has_64Bit_data(task) || (*state_count != ARM_DEBUG_STATE32_COUNT)) {
174 			return KERN_INVALID_ARGUMENT;
175 		}
176 
177 		if (task->task_debug == NULL) {
178 			bzero(state, sizeof(*tstate));
179 		} else {
180 			copy_debug_state32((arm_debug_state32_t*) task->task_debug, tstate, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
181 		}
182 
183 		return KERN_SUCCESS;
184 	}
185 	case ARM_DEBUG_STATE64:
186 	{
187 		arm_debug_state64_t *tstate = (arm_debug_state64_t *) state;
188 
189 		if ((!task_has_64Bit_data(task)) || (*state_count != ARM_DEBUG_STATE64_COUNT)) {
190 			return KERN_INVALID_ARGUMENT;
191 		}
192 
193 		if (task->task_debug == NULL) {
194 			bzero(state, sizeof(*tstate));
195 		} else {
196 			copy_debug_state64((arm_debug_state64_t*) task->task_debug, tstate, FALSE); /* FALSE OR TRUE doesn't matter since we are ignoring it for arm */
197 		}
198 
199 		return KERN_SUCCESS;
200 	}
201 	default:
202 	{
203 		return KERN_INVALID_ARGUMENT;
204 	}
205 	}
206 	return KERN_FAILURE;
207 }
208 
209 void
machine_task_terminate(task_t task)210 machine_task_terminate(task_t task)
211 {
212 	if (task) {
213 		void *task_debug;
214 		task_debug = task->task_debug;
215 		if (task_debug != NULL) {
216 			task->task_debug = NULL;
217 			zfree(ads_zone, task_debug);
218 		}
219 	}
220 }
221 
222 
223 kern_return_t
machine_thread_inherit_taskwide(thread_t thread,task_t parent_task)224 machine_thread_inherit_taskwide(
225 	thread_t thread,
226 	task_t parent_task)
227 {
228 	if (parent_task->task_debug) {
229 		int flavor;
230 		mach_msg_type_number_t count;
231 
232 		flavor = task_has_64Bit_data(parent_task) ? ARM_DEBUG_STATE64 : ARM_DEBUG_STATE32;
233 		count = task_has_64Bit_data(parent_task) ? ARM_DEBUG_STATE64_COUNT : ARM_DEBUG_STATE32_COUNT;
234 
235 		return machine_thread_set_state(thread, flavor, parent_task->task_debug, count);
236 	}
237 
238 	return KERN_SUCCESS;
239 }
240 
241 
242 void
machine_task_init(__unused task_t new_task,__unused task_t parent_task,__unused boolean_t memory_inherit)243 machine_task_init(__unused task_t new_task,
244     __unused task_t parent_task,
245     __unused boolean_t memory_inherit)
246 {
247 }
248