]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2010 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_FREE_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989,1988,1987 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 | */ | |
58 | /* | |
59 | * File: kern/thread.c | |
60 | * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub | |
61 | * Date: 1986 | |
62 | * | |
91447636 | 63 | * Thread management primitives implementation. |
1c79356b A |
64 | */ |
65 | /* | |
66 | * Copyright (c) 1993 The University of Utah and | |
67 | * the Computer Systems Laboratory (CSL). All rights reserved. | |
68 | * | |
69 | * Permission to use, copy, modify and distribute this software and its | |
70 | * documentation is hereby granted, provided that both the copyright | |
71 | * notice and this permission notice appear in all copies of the | |
72 | * software, derivative works or modified versions, and any portions | |
73 | * thereof, and that both notices appear in supporting documentation. | |
74 | * | |
75 | * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS | |
76 | * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF | |
77 | * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
78 | * | |
79 | * CSL requests users of this software to return to csl-dist@cs.utah.edu any | |
80 | * improvements that they make and grant CSL redistribution rights. | |
81 | * | |
82 | */ | |
83 | ||
91447636 | 84 | #include <mach/mach_types.h> |
1c79356b A |
85 | #include <mach/boolean.h> |
86 | #include <mach/policy.h> | |
87 | #include <mach/thread_info.h> | |
88 | #include <mach/thread_special_ports.h> | |
89 | #include <mach/thread_status.h> | |
90 | #include <mach/time_value.h> | |
91 | #include <mach/vm_param.h> | |
91447636 A |
92 | |
93 | #include <machine/thread.h> | |
6d2010ae | 94 | #include <machine/pal_routines.h> |
91447636 A |
95 | |
96 | #include <kern/kern_types.h> | |
97 | #include <kern/kalloc.h> | |
1c79356b A |
98 | #include <kern/cpu_data.h> |
99 | #include <kern/counters.h> | |
6d2010ae | 100 | #include <kern/extmod_statistics.h> |
1c79356b A |
101 | #include <kern/ipc_mig.h> |
102 | #include <kern/ipc_tt.h> | |
103 | #include <kern/mach_param.h> | |
104 | #include <kern/machine.h> | |
105 | #include <kern/misc_protos.h> | |
106 | #include <kern/processor.h> | |
107 | #include <kern/queue.h> | |
108 | #include <kern/sched.h> | |
109 | #include <kern/sched_prim.h> | |
91447636 A |
110 | #include <kern/sync_lock.h> |
111 | #include <kern/syscall_subr.h> | |
1c79356b A |
112 | #include <kern/task.h> |
113 | #include <kern/thread.h> | |
1c79356b A |
114 | #include <kern/host.h> |
115 | #include <kern/zalloc.h> | |
1c79356b | 116 | #include <kern/assert.h> |
91447636 A |
117 | |
118 | #include <ipc/ipc_kmsg.h> | |
119 | #include <ipc/ipc_port.h> | |
120 | ||
121 | #include <vm/vm_kern.h> | |
122 | #include <vm/vm_pageout.h> | |
123 | ||
1c79356b A |
124 | #include <sys/kdebug.h> |
125 | ||
2d21ac55 A |
126 | #include <mach/sdt.h> |
127 | ||
1c79356b A |
128 | /* |
129 | * Exported interfaces | |
130 | */ | |
91447636 | 131 | #include <mach/task_server.h> |
1c79356b A |
132 | #include <mach/thread_act_server.h> |
133 | #include <mach/mach_host_server.h> | |
91447636 | 134 | #include <mach/host_priv_server.h> |
1c79356b | 135 | |
55e303ae | 136 | static struct zone *thread_zone; |
b0d623f7 A |
137 | static lck_grp_attr_t thread_lck_grp_attr; |
138 | lck_attr_t thread_lck_attr; | |
139 | lck_grp_t thread_lck_grp; | |
1c79356b | 140 | |
91447636 A |
141 | decl_simple_lock_data(static,thread_stack_lock) |
142 | static queue_head_t thread_stack_queue; | |
1c79356b | 143 | |
91447636 A |
144 | decl_simple_lock_data(static,thread_terminate_lock) |
145 | static queue_head_t thread_terminate_queue; | |
1c79356b | 146 | |
55e303ae | 147 | static struct thread thread_template, init_thread; |
1c79356b | 148 | |
2d21ac55 A |
149 | static void sched_call_null( |
150 | int type, | |
151 | thread_t thread); | |
b0d623f7 | 152 | |
91447636 A |
153 | #ifdef MACH_BSD |
154 | extern void proc_exit(void *); | |
b0d623f7 | 155 | extern uint64_t get_dispatchqueue_offset_from_proc(void *); |
91447636 | 156 | #endif /* MACH_BSD */ |
b0d623f7 | 157 | |
2d21ac55 | 158 | extern int debug_task; |
b0d623f7 A |
159 | int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */ |
160 | int task_threadmax = CONFIG_THREAD_MAX; | |
161 | ||
162 | static uint64_t thread_unique_id = 0; | |
55e303ae | 163 | |
91447636 A |
164 | void |
165 | thread_bootstrap(void) | |
166 | { | |
167 | /* | |
168 | * Fill in a template thread for fast initialization. | |
169 | */ | |
1c79356b | 170 | |
2d21ac55 | 171 | thread_template.runq = PROCESSOR_NULL; |
1c79356b | 172 | |
91447636 | 173 | thread_template.ref_count = 2; |
55e303ae | 174 | |
91447636 A |
175 | thread_template.reason = AST_NONE; |
176 | thread_template.at_safe_point = FALSE; | |
177 | thread_template.wait_event = NO_EVENT64; | |
178 | thread_template.wait_queue = WAIT_QUEUE_NULL; | |
179 | thread_template.wait_result = THREAD_WAITING; | |
180 | thread_template.options = THREAD_ABORTSAFE; | |
181 | thread_template.state = TH_WAIT | TH_UNINT; | |
182 | thread_template.wake_active = FALSE; | |
183 | thread_template.continuation = THREAD_CONTINUE_NULL; | |
184 | thread_template.parameter = NULL; | |
1c79356b | 185 | |
91447636 | 186 | thread_template.importance = 0; |
6d2010ae A |
187 | thread_template.sched_mode = TH_MODE_NONE; |
188 | thread_template.sched_flags = 0; | |
189 | thread_template.saved_mode = TH_MODE_NONE; | |
91447636 | 190 | thread_template.safe_release = 0; |
0b4e3aa0 | 191 | |
91447636 A |
192 | thread_template.priority = 0; |
193 | thread_template.sched_pri = 0; | |
194 | thread_template.max_priority = 0; | |
195 | thread_template.task_priority = 0; | |
196 | thread_template.promotions = 0; | |
197 | thread_template.pending_promoter_index = 0; | |
198 | thread_template.pending_promoter[0] = | |
199 | thread_template.pending_promoter[1] = NULL; | |
1c79356b | 200 | |
91447636 | 201 | thread_template.realtime.deadline = UINT64_MAX; |
1c79356b | 202 | |
91447636 | 203 | thread_template.current_quantum = 0; |
6d2010ae A |
204 | thread_template.last_run_time = 0; |
205 | thread_template.last_quantum_refill_time = 0; | |
1c79356b | 206 | |
91447636 A |
207 | thread_template.computation_metered = 0; |
208 | thread_template.computation_epoch = 0; | |
1c79356b | 209 | |
6d2010ae | 210 | #if defined(CONFIG_SCHED_TRADITIONAL) |
91447636 | 211 | thread_template.sched_stamp = 0; |
91447636 | 212 | thread_template.pri_shift = INT8_MAX; |
6d2010ae | 213 | thread_template.sched_usage = 0; |
91447636 | 214 | thread_template.cpu_usage = thread_template.cpu_delta = 0; |
6d2010ae | 215 | #endif |
2d21ac55 | 216 | thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0; |
0b4e3aa0 | 217 | |
91447636 A |
218 | thread_template.bound_processor = PROCESSOR_NULL; |
219 | thread_template.last_processor = PROCESSOR_NULL; | |
1c79356b | 220 | |
2d21ac55 A |
221 | thread_template.sched_call = sched_call_null; |
222 | ||
91447636 A |
223 | timer_init(&thread_template.user_timer); |
224 | timer_init(&thread_template.system_timer); | |
225 | thread_template.user_timer_save = 0; | |
226 | thread_template.system_timer_save = 0; | |
2d21ac55 A |
227 | thread_template.vtimer_user_save = 0; |
228 | thread_template.vtimer_prof_save = 0; | |
229 | thread_template.vtimer_rlim_save = 0; | |
1c79356b | 230 | |
91447636 A |
231 | thread_template.wait_timer_is_set = FALSE; |
232 | thread_template.wait_timer_active = 0; | |
1c79356b | 233 | |
91447636 | 234 | thread_template.depress_timer_active = 0; |
0b4e3aa0 | 235 | |
91447636 | 236 | thread_template.special_handler.handler = special_handler; |
2d21ac55 | 237 | thread_template.special_handler.next = NULL; |
55e303ae | 238 | |
91447636 A |
239 | thread_template.funnel_lock = THR_FUNNEL_NULL; |
240 | thread_template.funnel_state = 0; | |
241 | thread_template.recover = (vm_offset_t)NULL; | |
2d21ac55 A |
242 | |
243 | thread_template.map = VM_MAP_NULL; | |
244 | ||
245 | #if CONFIG_DTRACE | |
246 | thread_template.t_dtrace_predcache = 0; | |
247 | thread_template.t_dtrace_vtime = 0; | |
248 | thread_template.t_dtrace_tracing = 0; | |
249 | #endif /* CONFIG_DTRACE */ | |
b0d623f7 | 250 | |
2d21ac55 | 251 | thread_template.t_chud = 0; |
b0d623f7 A |
252 | thread_template.t_page_creation_count = 0; |
253 | thread_template.t_page_creation_time = 0; | |
55e303ae | 254 | |
2d21ac55 A |
255 | thread_template.affinity_set = NULL; |
256 | ||
6d2010ae A |
257 | thread_template.syscalls_unix = 0; |
258 | thread_template.syscalls_mach = 0; | |
259 | ||
260 | thread_template.tkm_private.alloc = 0; | |
261 | thread_template.tkm_private.free = 0; | |
262 | thread_template.tkm_shared.alloc = 0; | |
263 | thread_template.tkm_shared.free = 0; | |
264 | thread_template.actionstate = default_task_null_policy; | |
265 | thread_template.ext_actionstate = default_task_null_policy; | |
266 | thread_template.policystate = default_task_proc_policy; | |
267 | thread_template.ext_policystate = default_task_proc_policy; | |
268 | ||
91447636 A |
269 | init_thread = thread_template; |
270 | machine_set_current_thread(&init_thread); | |
1c79356b A |
271 | } |
272 | ||
55e303ae | 273 | void |
91447636 | 274 | thread_init(void) |
0b4e3aa0 | 275 | { |
91447636 A |
276 | thread_zone = zinit( |
277 | sizeof(struct thread), | |
b0d623f7 | 278 | thread_max * sizeof(struct thread), |
91447636 A |
279 | THREAD_CHUNK * sizeof(struct thread), |
280 | "threads"); | |
6d2010ae | 281 | |
b0d623f7 A |
282 | lck_grp_attr_setdefault(&thread_lck_grp_attr); |
283 | lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr); | |
284 | lck_attr_setdefault(&thread_lck_attr); | |
285 | ||
91447636 | 286 | stack_init(); |
55e303ae | 287 | |
91447636 A |
288 | /* |
289 | * Initialize any machine-dependent | |
290 | * per-thread structures necessary. | |
291 | */ | |
292 | machine_thread_init(); | |
293 | } | |
0b4e3aa0 | 294 | |
91447636 A |
295 | static void |
296 | thread_terminate_continue(void) | |
297 | { | |
298 | panic("thread_terminate_continue"); | |
299 | /*NOTREACHED*/ | |
0b4e3aa0 A |
300 | } |
301 | ||
1c79356b | 302 | /* |
91447636 | 303 | * thread_terminate_self: |
1c79356b | 304 | */ |
1c79356b | 305 | void |
91447636 | 306 | thread_terminate_self(void) |
1c79356b | 307 | { |
91447636 | 308 | thread_t thread = current_thread(); |
6d2010ae | 309 | |
91447636 A |
310 | task_t task; |
311 | spl_t s; | |
b0d623f7 A |
312 | int threadcnt; |
313 | ||
6d2010ae A |
314 | pal_thread_terminate_self(thread); |
315 | ||
b0d623f7 | 316 | DTRACE_PROC(lwp__exit); |
2d21ac55 A |
317 | |
318 | thread_mtx_lock(thread); | |
319 | ||
320 | ulock_release_all(thread); | |
321 | ||
322 | ipc_thread_disable(thread); | |
323 | ||
324 | thread_mtx_unlock(thread); | |
55e303ae | 325 | |
91447636 A |
326 | s = splsched(); |
327 | thread_lock(thread); | |
1c79356b | 328 | |
91447636 | 329 | /* |
2d21ac55 A |
330 | * Cancel priority depression, wait for concurrent expirations |
331 | * on other processors. | |
91447636 | 332 | */ |
6d2010ae A |
333 | if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) { |
334 | thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK; | |
1c79356b | 335 | |
91447636 A |
336 | if (timer_call_cancel(&thread->depress_timer)) |
337 | thread->depress_timer_active--; | |
1c79356b | 338 | } |
1c79356b | 339 | |
91447636 A |
340 | while (thread->depress_timer_active > 0) { |
341 | thread_unlock(thread); | |
342 | splx(s); | |
55e303ae | 343 | |
91447636 | 344 | delay(1); |
55e303ae | 345 | |
91447636 A |
346 | s = splsched(); |
347 | thread_lock(thread); | |
55e303ae A |
348 | } |
349 | ||
b0d623f7 A |
350 | thread_sched_call(thread, NULL); |
351 | ||
91447636 A |
352 | thread_unlock(thread); |
353 | splx(s); | |
55e303ae | 354 | |
2d21ac55 | 355 | thread_policy_reset(thread); |
55e303ae | 356 | |
b0d623f7 A |
357 | task = thread->task; |
358 | uthread_cleanup(task, thread->uthread, task->bsd_info); | |
359 | threadcnt = hw_atomic_sub(&task->active_thread_count, 1); | |
360 | ||
91447636 A |
361 | /* |
362 | * If we are the last thread to terminate and the task is | |
363 | * associated with a BSD process, perform BSD process exit. | |
364 | */ | |
b0d623f7 | 365 | if (threadcnt == 0 && task->bsd_info != NULL) |
91447636 | 366 | proc_exit(task->bsd_info); |
1c79356b | 367 | |
2d21ac55 A |
368 | uthread_cred_free(thread->uthread); |
369 | ||
91447636 A |
370 | s = splsched(); |
371 | thread_lock(thread); | |
1c79356b | 372 | |
91447636 A |
373 | /* |
374 | * Cancel wait timer, and wait for | |
375 | * concurrent expirations. | |
376 | */ | |
377 | if (thread->wait_timer_is_set) { | |
378 | thread->wait_timer_is_set = FALSE; | |
1c79356b | 379 | |
91447636 A |
380 | if (timer_call_cancel(&thread->wait_timer)) |
381 | thread->wait_timer_active--; | |
382 | } | |
1c79356b | 383 | |
91447636 A |
384 | while (thread->wait_timer_active > 0) { |
385 | thread_unlock(thread); | |
386 | splx(s); | |
0b4e3aa0 | 387 | |
91447636 | 388 | delay(1); |
1c79356b | 389 | |
91447636 A |
390 | s = splsched(); |
391 | thread_lock(thread); | |
392 | } | |
1c79356b | 393 | |
91447636 A |
394 | /* |
395 | * If there is a reserved stack, release it. | |
396 | */ | |
397 | if (thread->reserved_stack != 0) { | |
6d2010ae | 398 | stack_free_reserved(thread); |
91447636 A |
399 | thread->reserved_stack = 0; |
400 | } | |
1c79356b | 401 | |
91447636 A |
402 | /* |
403 | * Mark thread as terminating, and block. | |
404 | */ | |
405 | thread->state |= TH_TERMINATE; | |
406 | thread_mark_wait_locked(thread, THREAD_UNINT); | |
407 | assert(thread->promotions == 0); | |
408 | thread_unlock(thread); | |
409 | /* splsched */ | |
1c79356b | 410 | |
91447636 A |
411 | thread_block((thread_continue_t)thread_terminate_continue); |
412 | /*NOTREACHED*/ | |
55e303ae A |
413 | } |
414 | ||
415 | void | |
91447636 A |
416 | thread_deallocate( |
417 | thread_t thread) | |
1c79356b | 418 | { |
91447636 | 419 | task_t task; |
1c79356b | 420 | |
91447636 A |
421 | if (thread == THREAD_NULL) |
422 | return; | |
1c79356b | 423 | |
91447636 A |
424 | if (thread_deallocate_internal(thread) > 0) |
425 | return; | |
1c79356b | 426 | |
6d2010ae | 427 | |
91447636 | 428 | ipc_thread_terminate(thread); |
1c79356b | 429 | |
91447636 | 430 | task = thread->task; |
0b4e3aa0 | 431 | |
91447636 A |
432 | #ifdef MACH_BSD |
433 | { | |
434 | void *ut = thread->uthread; | |
0b4e3aa0 | 435 | |
91447636 | 436 | thread->uthread = NULL; |
2d21ac55 | 437 | uthread_zone_free(ut); |
91447636 A |
438 | } |
439 | #endif /* MACH_BSD */ | |
0b4e3aa0 | 440 | |
91447636 A |
441 | if (thread->kernel_stack != 0) |
442 | stack_free(thread); | |
0b4e3aa0 | 443 | |
b0d623f7 | 444 | lck_mtx_destroy(&thread->mutex, &thread_lck_grp); |
91447636 | 445 | machine_thread_destroy(thread); |
1c79356b | 446 | |
6d2010ae A |
447 | task_deallocate(task); |
448 | ||
91447636 A |
449 | zfree(thread_zone, thread); |
450 | } | |
0b4e3aa0 | 451 | |
91447636 A |
452 | /* |
453 | * thread_terminate_daemon: | |
454 | * | |
455 | * Perform final clean up for terminating threads. | |
456 | */ | |
457 | static void | |
458 | thread_terminate_daemon(void) | |
459 | { | |
6d2010ae A |
460 | thread_t self, thread; |
461 | task_t task; | |
462 | ||
463 | self = current_thread(); | |
464 | self->options |= TH_OPT_SYSTEM_CRITICAL; | |
0b4e3aa0 | 465 | |
91447636 A |
466 | (void)splsched(); |
467 | simple_lock(&thread_terminate_lock); | |
0b4e3aa0 | 468 | |
91447636 A |
469 | while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) { |
470 | simple_unlock(&thread_terminate_lock); | |
471 | (void)spllo(); | |
0b4e3aa0 | 472 | |
91447636 | 473 | task = thread->task; |
55e303ae | 474 | |
91447636 A |
475 | task_lock(task); |
476 | task->total_user_time += timer_grab(&thread->user_timer); | |
477 | task->total_system_time += timer_grab(&thread->system_timer); | |
55e303ae | 478 | |
2d21ac55 A |
479 | task->c_switch += thread->c_switch; |
480 | task->p_switch += thread->p_switch; | |
481 | task->ps_switch += thread->ps_switch; | |
482 | ||
6d2010ae A |
483 | task->syscalls_unix += thread->syscalls_unix; |
484 | task->syscalls_mach += thread->syscalls_mach; | |
485 | ||
486 | task->tkm_private.alloc += thread->tkm_private.alloc; | |
487 | task->tkm_private.free += thread->tkm_private.free; | |
488 | task->tkm_shared.alloc += thread->tkm_shared.alloc; | |
489 | task->tkm_shared.free += thread->tkm_shared.free; | |
490 | ||
91447636 A |
491 | queue_remove(&task->threads, thread, thread_t, task_threads); |
492 | task->thread_count--; | |
b0d623f7 A |
493 | |
494 | /* | |
495 | * If the task is being halted, and there is only one thread | |
496 | * left in the task after this one, then wakeup that thread. | |
497 | */ | |
498 | if (task->thread_count == 1 && task->halting) | |
499 | thread_wakeup((event_t)&task->halting); | |
500 | ||
91447636 | 501 | task_unlock(task); |
1c79356b | 502 | |
b0d623f7 | 503 | lck_mtx_lock(&tasks_threads_lock); |
2d21ac55 A |
504 | queue_remove(&threads, thread, thread_t, threads); |
505 | threads_count--; | |
b0d623f7 | 506 | lck_mtx_unlock(&tasks_threads_lock); |
1c79356b | 507 | |
91447636 | 508 | thread_deallocate(thread); |
1c79356b | 509 | |
91447636 A |
510 | (void)splsched(); |
511 | simple_lock(&thread_terminate_lock); | |
0b4e3aa0 | 512 | } |
1c79356b | 513 | |
91447636 A |
514 | assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT); |
515 | simple_unlock(&thread_terminate_lock); | |
516 | /* splsched */ | |
517 | ||
6d2010ae | 518 | self->options &= ~TH_OPT_SYSTEM_CRITICAL; |
91447636 A |
519 | thread_block((thread_continue_t)thread_terminate_daemon); |
520 | /*NOTREACHED*/ | |
1c79356b A |
521 | } |
522 | ||
9bccf70c | 523 | /* |
91447636 A |
524 | * thread_terminate_enqueue: |
525 | * | |
526 | * Enqueue a terminating thread for final disposition. | |
527 | * | |
528 | * Called at splsched. | |
9bccf70c | 529 | */ |
1c79356b | 530 | void |
91447636 | 531 | thread_terminate_enqueue( |
1c79356b A |
532 | thread_t thread) |
533 | { | |
91447636 A |
534 | simple_lock(&thread_terminate_lock); |
535 | enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread); | |
536 | simple_unlock(&thread_terminate_lock); | |
1c79356b | 537 | |
91447636 | 538 | thread_wakeup((event_t)&thread_terminate_queue); |
1c79356b A |
539 | } |
540 | ||
91447636 A |
541 | /* |
542 | * thread_stack_daemon: | |
543 | * | |
544 | * Perform stack allocation as required due to | |
545 | * invoke failures. | |
546 | */ | |
547 | static void | |
548 | thread_stack_daemon(void) | |
9bccf70c | 549 | { |
91447636 A |
550 | thread_t thread; |
551 | ||
91447636 A |
552 | simple_lock(&thread_stack_lock); |
553 | ||
554 | while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) { | |
555 | simple_unlock(&thread_stack_lock); | |
91447636 A |
556 | |
557 | stack_alloc(thread); | |
2d21ac55 A |
558 | |
559 | (void)splsched(); | |
91447636 A |
560 | thread_lock(thread); |
561 | thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ); | |
562 | thread_unlock(thread); | |
563 | (void)spllo(); | |
564 | ||
91447636 A |
565 | simple_lock(&thread_stack_lock); |
566 | } | |
567 | ||
568 | assert_wait((event_t)&thread_stack_queue, THREAD_UNINT); | |
569 | simple_unlock(&thread_stack_lock); | |
91447636 A |
570 | |
571 | thread_block((thread_continue_t)thread_stack_daemon); | |
9bccf70c A |
572 | /*NOTREACHED*/ |
573 | } | |
1c79356b A |
574 | |
575 | /* | |
91447636 | 576 | * thread_stack_enqueue: |
1c79356b | 577 | * |
91447636 | 578 | * Enqueue a thread for stack allocation. |
1c79356b | 579 | * |
91447636 | 580 | * Called at splsched. |
1c79356b A |
581 | */ |
582 | void | |
91447636 A |
583 | thread_stack_enqueue( |
584 | thread_t thread) | |
1c79356b | 585 | { |
91447636 A |
586 | simple_lock(&thread_stack_lock); |
587 | enqueue_tail(&thread_stack_queue, (queue_entry_t)thread); | |
588 | simple_unlock(&thread_stack_lock); | |
1c79356b | 589 | |
91447636 A |
590 | thread_wakeup((event_t)&thread_stack_queue); |
591 | } | |
9bccf70c | 592 | |
91447636 A |
593 | void |
594 | thread_daemon_init(void) | |
595 | { | |
596 | kern_return_t result; | |
6d2010ae | 597 | thread_t thread = NULL; |
0b4e3aa0 | 598 | |
91447636 A |
599 | simple_lock_init(&thread_terminate_lock, 0); |
600 | queue_init(&thread_terminate_queue); | |
1c79356b | 601 | |
91447636 A |
602 | result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread); |
603 | if (result != KERN_SUCCESS) | |
604 | panic("thread_daemon_init: thread_terminate_daemon"); | |
1c79356b | 605 | |
91447636 | 606 | thread_deallocate(thread); |
1c79356b | 607 | |
91447636 A |
608 | simple_lock_init(&thread_stack_lock, 0); |
609 | queue_init(&thread_stack_queue); | |
1c79356b | 610 | |
91447636 A |
611 | result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread); |
612 | if (result != KERN_SUCCESS) | |
613 | panic("thread_daemon_init: thread_stack_daemon"); | |
1c79356b | 614 | |
91447636 | 615 | thread_deallocate(thread); |
1c79356b A |
616 | } |
617 | ||
1c79356b A |
618 | /* |
619 | * Create a new thread. | |
55e303ae | 620 | * Doesn't start the thread running. |
1c79356b | 621 | */ |
55e303ae A |
622 | static kern_return_t |
623 | thread_create_internal( | |
624 | task_t parent_task, | |
1c79356b | 625 | integer_t priority, |
91447636 | 626 | thread_continue_t continuation, |
b0d623f7 A |
627 | int options, |
628 | #define TH_OPTION_NONE 0x00 | |
629 | #define TH_OPTION_NOCRED 0x01 | |
630 | #define TH_OPTION_NOSUSP 0x02 | |
55e303ae | 631 | thread_t *out_thread) |
1c79356b | 632 | { |
55e303ae | 633 | thread_t new_thread; |
55e303ae | 634 | static thread_t first_thread; |
1c79356b A |
635 | |
636 | /* | |
637 | * Allocate a thread and initialize static fields | |
638 | */ | |
b0d623f7 | 639 | if (first_thread == THREAD_NULL) |
91447636 | 640 | new_thread = first_thread = current_thread(); |
55e303ae A |
641 | else |
642 | new_thread = (thread_t)zalloc(thread_zone); | |
b0d623f7 | 643 | if (new_thread == THREAD_NULL) |
1c79356b A |
644 | return (KERN_RESOURCE_SHORTAGE); |
645 | ||
55e303ae A |
646 | if (new_thread != first_thread) |
647 | *new_thread = thread_template; | |
648 | ||
649 | #ifdef MACH_BSD | |
b0d623f7 A |
650 | new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0); |
651 | if (new_thread->uthread == NULL) { | |
652 | zfree(thread_zone, new_thread); | |
653 | return (KERN_RESOURCE_SHORTAGE); | |
55e303ae A |
654 | } |
655 | #endif /* MACH_BSD */ | |
1c79356b | 656 | |
55e303ae A |
657 | if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) { |
658 | #ifdef MACH_BSD | |
b0d623f7 | 659 | void *ut = new_thread->uthread; |
1c79356b | 660 | |
b0d623f7 A |
661 | new_thread->uthread = NULL; |
662 | /* cred free may not be necessary */ | |
663 | uthread_cleanup(parent_task, ut, parent_task->bsd_info); | |
664 | uthread_cred_free(ut); | |
665 | uthread_zone_free(ut); | |
55e303ae | 666 | #endif /* MACH_BSD */ |
b0d623f7 | 667 | |
91447636 | 668 | zfree(thread_zone, new_thread); |
55e303ae A |
669 | return (KERN_FAILURE); |
670 | } | |
671 | ||
672 | new_thread->task = parent_task; | |
673 | ||
674 | thread_lock_init(new_thread); | |
675 | wake_lock_init(new_thread); | |
676 | ||
b0d623f7 | 677 | lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr); |
0b4e3aa0 | 678 | |
55e303ae A |
679 | ipc_thread_init(new_thread); |
680 | queue_init(&new_thread->held_ulocks); | |
55e303ae | 681 | |
91447636 | 682 | new_thread->continuation = continuation; |
0b4e3aa0 | 683 | |
b0d623f7 | 684 | lck_mtx_lock(&tasks_threads_lock); |
1c79356b A |
685 | task_lock(parent_task); |
686 | ||
b0d623f7 A |
687 | if ( !parent_task->active || parent_task->halting || |
688 | ((options & TH_OPTION_NOSUSP) != 0 && | |
689 | parent_task->suspend_count > 0) || | |
690 | (parent_task->thread_count >= task_threadmax && | |
691 | parent_task != kernel_task) ) { | |
1c79356b | 692 | task_unlock(parent_task); |
b0d623f7 | 693 | lck_mtx_unlock(&tasks_threads_lock); |
55e303ae A |
694 | |
695 | #ifdef MACH_BSD | |
696 | { | |
55e303ae A |
697 | void *ut = new_thread->uthread; |
698 | ||
699 | new_thread->uthread = NULL; | |
2d21ac55 A |
700 | uthread_cleanup(parent_task, ut, parent_task->bsd_info); |
701 | /* cred free may not be necessary */ | |
702 | uthread_cred_free(ut); | |
703 | uthread_zone_free(ut); | |
55e303ae A |
704 | } |
705 | #endif /* MACH_BSD */ | |
91447636 A |
706 | ipc_thread_disable(new_thread); |
707 | ipc_thread_terminate(new_thread); | |
b0d623f7 | 708 | lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp); |
55e303ae | 709 | machine_thread_destroy(new_thread); |
91447636 | 710 | zfree(thread_zone, new_thread); |
1c79356b A |
711 | return (KERN_FAILURE); |
712 | } | |
713 | ||
b0d623f7 A |
714 | /* New threads inherit any default state on the task */ |
715 | machine_thread_inherit_taskwide(new_thread, parent_task); | |
716 | ||
91447636 | 717 | task_reference_internal(parent_task); |
55e303ae A |
718 | |
719 | /* Cache the task's map */ | |
720 | new_thread->map = parent_task->map; | |
1c79356b | 721 | |
55e303ae | 722 | /* Chain the thread onto the task's list */ |
91447636 | 723 | queue_enter(&parent_task->threads, new_thread, thread_t, task_threads); |
55e303ae | 724 | parent_task->thread_count++; |
9bccf70c A |
725 | |
726 | /* So terminating threads don't need to take the task lock to decrement */ | |
55e303ae | 727 | hw_atomic_add(&parent_task->active_thread_count, 1); |
1c79356b | 728 | |
b0d623f7 A |
729 | /* Protected by the tasks_threads_lock */ |
730 | new_thread->thread_id = ++thread_unique_id; | |
731 | ||
2d21ac55 A |
732 | queue_enter(&threads, new_thread, thread_t, threads); |
733 | threads_count++; | |
55e303ae | 734 | |
91447636 A |
735 | timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread); |
736 | timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread); | |
1c79356b | 737 | |
b0d623f7 A |
738 | #if CONFIG_COUNTERS |
739 | /* | |
740 | * If parent task has any reservations, they need to be propagated to this | |
741 | * thread. | |
742 | */ | |
743 | new_thread->t_chud = (TASK_PMC_FLAG == (parent_task->t_chud & TASK_PMC_FLAG)) ? | |
744 | THREAD_PMC_FLAG : 0U; | |
745 | #endif | |
746 | ||
1c79356b | 747 | /* Set the thread's scheduling parameters */ |
6d2010ae A |
748 | new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task); |
749 | new_thread->sched_flags = 0; | |
55e303ae A |
750 | new_thread->max_priority = parent_task->max_priority; |
751 | new_thread->task_priority = parent_task->priority; | |
752 | new_thread->priority = (priority < 0)? parent_task->priority: priority; | |
753 | if (new_thread->priority > new_thread->max_priority) | |
754 | new_thread->priority = new_thread->max_priority; | |
6d2010ae A |
755 | #if CONFIG_EMBEDDED |
756 | if (new_thread->priority < MAXPRI_THROTTLE) { | |
757 | new_thread->priority = MAXPRI_THROTTLE; | |
758 | } | |
759 | #endif /* CONFIG_EMBEDDED */ | |
55e303ae A |
760 | new_thread->importance = |
761 | new_thread->priority - new_thread->task_priority; | |
6d2010ae | 762 | #if defined(CONFIG_SCHED_TRADITIONAL) |
55e303ae | 763 | new_thread->sched_stamp = sched_tick; |
2d21ac55 | 764 | new_thread->pri_shift = sched_pri_shift; |
6d2010ae A |
765 | #endif |
766 | SCHED(compute_priority)(new_thread, FALSE); | |
1c79356b | 767 | |
55e303ae | 768 | new_thread->active = TRUE; |
1c79356b | 769 | |
55e303ae | 770 | *out_thread = new_thread; |
1c79356b A |
771 | |
772 | { | |
9bccf70c | 773 | long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4; |
1c79356b | 774 | |
55e303ae A |
775 | kdbg_trace_data(parent_task->bsd_info, &dbg_arg2); |
776 | ||
9bccf70c A |
777 | KERNEL_DEBUG_CONSTANT( |
778 | TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE, | |
b0d623f7 | 779 | (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0); |
1c79356b | 780 | |
9bccf70c A |
781 | kdbg_trace_string(parent_task->bsd_info, |
782 | &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4); | |
783 | ||
784 | KERNEL_DEBUG_CONSTANT( | |
785 | TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE, | |
786 | dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0); | |
1c79356b A |
787 | } |
788 | ||
2d21ac55 A |
789 | DTRACE_PROC1(lwp__create, thread_t, *out_thread); |
790 | ||
1c79356b A |
791 | return (KERN_SUCCESS); |
792 | } | |
793 | ||
6d2010ae A |
794 | static kern_return_t |
795 | thread_create_internal2( | |
1c79356b | 796 | task_t task, |
6d2010ae A |
797 | thread_t *new_thread, |
798 | boolean_t from_user) | |
1c79356b | 799 | { |
1c79356b | 800 | kern_return_t result; |
9bccf70c | 801 | thread_t thread; |
1c79356b | 802 | |
55e303ae A |
803 | if (task == TASK_NULL || task == kernel_task) |
804 | return (KERN_INVALID_ARGUMENT); | |
1c79356b | 805 | |
b0d623f7 | 806 | result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread); |
1c79356b A |
807 | if (result != KERN_SUCCESS) |
808 | return (result); | |
809 | ||
55e303ae A |
810 | thread->user_stop_count = 1; |
811 | thread_hold(thread); | |
9bccf70c | 812 | if (task->suspend_count > 0) |
55e303ae | 813 | thread_hold(thread); |
1c79356b | 814 | |
6d2010ae A |
815 | if (from_user) |
816 | extmod_statistics_incr_thread_create(task); | |
817 | ||
9bccf70c | 818 | task_unlock(task); |
b0d623f7 | 819 | lck_mtx_unlock(&tasks_threads_lock); |
1c79356b | 820 | |
55e303ae | 821 | *new_thread = thread; |
1c79356b A |
822 | |
823 | return (KERN_SUCCESS); | |
824 | } | |
825 | ||
6d2010ae | 826 | /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */ |
1c79356b | 827 | kern_return_t |
6d2010ae A |
828 | thread_create( |
829 | task_t task, | |
830 | thread_t *new_thread); | |
831 | ||
832 | kern_return_t | |
833 | thread_create( | |
834 | task_t task, | |
835 | thread_t *new_thread) | |
836 | { | |
837 | return thread_create_internal2(task, new_thread, FALSE); | |
838 | } | |
839 | ||
840 | kern_return_t | |
841 | thread_create_from_user( | |
842 | task_t task, | |
843 | thread_t *new_thread) | |
844 | { | |
845 | return thread_create_internal2(task, new_thread, TRUE); | |
846 | } | |
847 | ||
848 | static kern_return_t | |
849 | thread_create_running_internal2( | |
9bccf70c | 850 | register task_t task, |
1c79356b A |
851 | int flavor, |
852 | thread_state_t new_state, | |
853 | mach_msg_type_number_t new_state_count, | |
6d2010ae A |
854 | thread_t *new_thread, |
855 | boolean_t from_user) | |
1c79356b A |
856 | { |
857 | register kern_return_t result; | |
9bccf70c | 858 | thread_t thread; |
9bccf70c | 859 | |
55e303ae A |
860 | if (task == TASK_NULL || task == kernel_task) |
861 | return (KERN_INVALID_ARGUMENT); | |
1c79356b | 862 | |
b0d623f7 | 863 | result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread); |
1c79356b A |
864 | if (result != KERN_SUCCESS) |
865 | return (result); | |
866 | ||
91447636 A |
867 | result = machine_thread_set_state( |
868 | thread, flavor, new_state, new_state_count); | |
1c79356b | 869 | if (result != KERN_SUCCESS) { |
9bccf70c | 870 | task_unlock(task); |
b0d623f7 | 871 | lck_mtx_unlock(&tasks_threads_lock); |
9bccf70c | 872 | |
55e303ae | 873 | thread_terminate(thread); |
91447636 | 874 | thread_deallocate(thread); |
1c79356b A |
875 | return (result); |
876 | } | |
877 | ||
91447636 | 878 | thread_mtx_lock(thread); |
2d21ac55 | 879 | thread_start_internal(thread); |
91447636 | 880 | thread_mtx_unlock(thread); |
2d21ac55 | 881 | |
6d2010ae A |
882 | if (from_user) |
883 | extmod_statistics_incr_thread_create(task); | |
884 | ||
9bccf70c | 885 | task_unlock(task); |
b0d623f7 | 886 | lck_mtx_unlock(&tasks_threads_lock); |
9bccf70c | 887 | |
55e303ae | 888 | *new_thread = thread; |
9bccf70c | 889 | |
1c79356b A |
890 | return (result); |
891 | } | |
892 | ||
6d2010ae A |
893 | /* Prototype, see justification above */ |
894 | kern_return_t | |
895 | thread_create_running( | |
896 | register task_t task, | |
897 | int flavor, | |
898 | thread_state_t new_state, | |
899 | mach_msg_type_number_t new_state_count, | |
900 | thread_t *new_thread); | |
901 | ||
902 | kern_return_t | |
903 | thread_create_running( | |
904 | register task_t task, | |
905 | int flavor, | |
906 | thread_state_t new_state, | |
907 | mach_msg_type_number_t new_state_count, | |
908 | thread_t *new_thread) | |
909 | { | |
910 | return thread_create_running_internal2( | |
911 | task, flavor, new_state, new_state_count, | |
912 | new_thread, FALSE); | |
913 | } | |
914 | ||
915 | kern_return_t | |
916 | thread_create_running_from_user( | |
917 | register task_t task, | |
918 | int flavor, | |
919 | thread_state_t new_state, | |
920 | mach_msg_type_number_t new_state_count, | |
921 | thread_t *new_thread) | |
922 | { | |
923 | return thread_create_running_internal2( | |
924 | task, flavor, new_state, new_state_count, | |
925 | new_thread, TRUE); | |
926 | } | |
927 | ||
b0d623f7 A |
928 | kern_return_t |
929 | thread_create_workq( | |
930 | task_t task, | |
b7266188 | 931 | thread_continue_t thread_return, |
b0d623f7 A |
932 | thread_t *new_thread) |
933 | { | |
934 | kern_return_t result; | |
935 | thread_t thread; | |
936 | ||
937 | if (task == TASK_NULL || task == kernel_task) | |
938 | return (KERN_INVALID_ARGUMENT); | |
939 | ||
b7266188 | 940 | result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread); |
b0d623f7 A |
941 | if (result != KERN_SUCCESS) |
942 | return (result); | |
943 | ||
944 | thread->user_stop_count = 1; | |
945 | thread_hold(thread); | |
946 | if (task->suspend_count > 0) | |
947 | thread_hold(thread); | |
948 | ||
949 | task_unlock(task); | |
950 | lck_mtx_unlock(&tasks_threads_lock); | |
951 | ||
952 | *new_thread = thread; | |
953 | ||
954 | return (KERN_SUCCESS); | |
955 | } | |
956 | ||
1c79356b | 957 | /* |
91447636 | 958 | * kernel_thread_create: |
1c79356b | 959 | * |
55e303ae A |
960 | * Create a thread in the kernel task |
961 | * to execute in kernel context. | |
1c79356b | 962 | */ |
91447636 | 963 | kern_return_t |
55e303ae | 964 | kernel_thread_create( |
91447636 A |
965 | thread_continue_t continuation, |
966 | void *parameter, | |
967 | integer_t priority, | |
968 | thread_t *new_thread) | |
1c79356b A |
969 | { |
970 | kern_return_t result; | |
971 | thread_t thread; | |
91447636 | 972 | task_t task = kernel_task; |
1c79356b | 973 | |
b0d623f7 | 974 | result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread); |
9bccf70c | 975 | if (result != KERN_SUCCESS) |
91447636 | 976 | return (result); |
1c79356b | 977 | |
9bccf70c | 978 | task_unlock(task); |
b0d623f7 | 979 | lck_mtx_unlock(&tasks_threads_lock); |
9bccf70c | 980 | |
91447636 | 981 | stack_alloc(thread); |
55e303ae | 982 | assert(thread->kernel_stack != 0); |
2d21ac55 A |
983 | #if CONFIG_EMBEDDED |
984 | if (priority > BASEPRI_KERNEL) | |
985 | #endif | |
55e303ae A |
986 | thread->reserved_stack = thread->kernel_stack; |
987 | ||
91447636 | 988 | thread->parameter = parameter; |
55e303ae | 989 | |
2d21ac55 A |
990 | if(debug_task & 1) |
991 | kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation); | |
91447636 A |
992 | *new_thread = thread; |
993 | ||
994 | return (result); | |
55e303ae A |
995 | } |
996 | ||
91447636 A |
997 | kern_return_t |
998 | kernel_thread_start_priority( | |
999 | thread_continue_t continuation, | |
1000 | void *parameter, | |
1001 | integer_t priority, | |
1002 | thread_t *new_thread) | |
55e303ae | 1003 | { |
91447636 | 1004 | kern_return_t result; |
55e303ae | 1005 | thread_t thread; |
1c79356b | 1006 | |
91447636 A |
1007 | result = kernel_thread_create(continuation, parameter, priority, &thread); |
1008 | if (result != KERN_SUCCESS) | |
1009 | return (result); | |
1c79356b | 1010 | |
b0d623f7 A |
1011 | *new_thread = thread; |
1012 | ||
91447636 | 1013 | thread_mtx_lock(thread); |
2d21ac55 | 1014 | thread_start_internal(thread); |
91447636 | 1015 | thread_mtx_unlock(thread); |
1c79356b | 1016 | |
91447636 A |
1017 | return (result); |
1018 | } | |
1019 | ||
1020 | kern_return_t | |
1021 | kernel_thread_start( | |
1022 | thread_continue_t continuation, | |
1023 | void *parameter, | |
1024 | thread_t *new_thread) | |
1025 | { | |
1026 | return kernel_thread_start_priority(continuation, parameter, -1, new_thread); | |
1c79356b A |
1027 | } |
1028 | ||
b0d623f7 A |
1029 | #ifndef __LP64__ |
1030 | ||
1c79356b A |
1031 | thread_t |
1032 | kernel_thread( | |
1033 | task_t task, | |
1034 | void (*start)(void)) | |
1035 | { | |
91447636 A |
1036 | kern_return_t result; |
1037 | thread_t thread; | |
1038 | ||
55e303ae A |
1039 | if (task != kernel_task) |
1040 | panic("kernel_thread"); | |
1041 | ||
91447636 A |
1042 | result = kernel_thread_start_priority((thread_continue_t)start, NULL, -1, &thread); |
1043 | if (result != KERN_SUCCESS) | |
1044 | return (THREAD_NULL); | |
1c79356b | 1045 | |
91447636 | 1046 | thread_deallocate(thread); |
1c79356b | 1047 | |
91447636 | 1048 | return (thread); |
1c79356b A |
1049 | } |
1050 | ||
b0d623f7 A |
1051 | #endif /* __LP64__ */ |
1052 | ||
1c79356b | 1053 | kern_return_t |
91447636 A |
1054 | thread_info_internal( |
1055 | register thread_t thread, | |
1c79356b A |
1056 | thread_flavor_t flavor, |
1057 | thread_info_t thread_info_out, /* ptr to OUT array */ | |
1058 | mach_msg_type_number_t *thread_info_count) /*IN/OUT*/ | |
1059 | { | |
1c79356b A |
1060 | int state, flags; |
1061 | spl_t s; | |
1062 | ||
1063 | if (thread == THREAD_NULL) | |
1064 | return (KERN_INVALID_ARGUMENT); | |
1065 | ||
1066 | if (flavor == THREAD_BASIC_INFO) { | |
1067 | register thread_basic_info_t basic_info; | |
1068 | ||
1069 | if (*thread_info_count < THREAD_BASIC_INFO_COUNT) | |
1070 | return (KERN_INVALID_ARGUMENT); | |
1071 | ||
1072 | basic_info = (thread_basic_info_t) thread_info_out; | |
1073 | ||
1074 | s = splsched(); | |
1075 | thread_lock(thread); | |
1076 | ||
1077 | /* fill in info */ | |
1078 | ||
1079 | thread_read_times(thread, &basic_info->user_time, | |
1080 | &basic_info->system_time); | |
1081 | ||
0b4e3aa0 A |
1082 | /* |
1083 | * Update lazy-evaluated scheduler info because someone wants it. | |
1084 | */ | |
6d2010ae A |
1085 | if (SCHED(can_update_priority)(thread)) |
1086 | SCHED(update_priority)(thread); | |
0b4e3aa0 A |
1087 | |
1088 | basic_info->sleep_time = 0; | |
1089 | ||
1090 | /* | |
1091 | * To calculate cpu_usage, first correct for timer rate, | |
1092 | * then for 5/8 ageing. The correction factor [3/5] is | |
1093 | * (1/(5/8) - 1). | |
1094 | */ | |
6d2010ae A |
1095 | basic_info->cpu_usage = 0; |
1096 | #if defined(CONFIG_SCHED_TRADITIONAL) | |
1097 | if (sched_tick_interval) { | |
1098 | basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage | |
1099 | * TH_USAGE_SCALE) / sched_tick_interval); | |
1100 | basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5; | |
1101 | } | |
1102 | #endif | |
1103 | ||
91447636 A |
1104 | if (basic_info->cpu_usage > TH_USAGE_SCALE) |
1105 | basic_info->cpu_usage = TH_USAGE_SCALE; | |
1c79356b | 1106 | |
6d2010ae | 1107 | basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)? |
0b4e3aa0 | 1108 | POLICY_TIMESHARE: POLICY_RR); |
1c79356b A |
1109 | |
1110 | flags = 0; | |
2d21ac55 | 1111 | if (thread->bound_processor != PROCESSOR_NULL && thread->bound_processor->idle_thread == thread) |
0b4e3aa0 A |
1112 | flags |= TH_FLAGS_IDLE; |
1113 | ||
91447636 | 1114 | if (!thread->kernel_stack) |
0b4e3aa0 | 1115 | flags |= TH_FLAGS_SWAPPED; |
1c79356b A |
1116 | |
1117 | state = 0; | |
9bccf70c | 1118 | if (thread->state & TH_TERMINATE) |
1c79356b A |
1119 | state = TH_STATE_HALTED; |
1120 | else | |
1121 | if (thread->state & TH_RUN) | |
1122 | state = TH_STATE_RUNNING; | |
1123 | else | |
1124 | if (thread->state & TH_UNINT) | |
1125 | state = TH_STATE_UNINTERRUPTIBLE; | |
1126 | else | |
1127 | if (thread->state & TH_SUSP) | |
1128 | state = TH_STATE_STOPPED; | |
1129 | else | |
1130 | if (thread->state & TH_WAIT) | |
1131 | state = TH_STATE_WAITING; | |
1132 | ||
1133 | basic_info->run_state = state; | |
1134 | basic_info->flags = flags; | |
1135 | ||
91447636 | 1136 | basic_info->suspend_count = thread->user_stop_count; |
1c79356b A |
1137 | |
1138 | thread_unlock(thread); | |
1139 | splx(s); | |
1140 | ||
1141 | *thread_info_count = THREAD_BASIC_INFO_COUNT; | |
1142 | ||
1143 | return (KERN_SUCCESS); | |
1144 | } | |
1145 | else | |
b0d623f7 A |
1146 | if (flavor == THREAD_IDENTIFIER_INFO) { |
1147 | register thread_identifier_info_t identifier_info; | |
1148 | ||
1149 | if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT) | |
1150 | return (KERN_INVALID_ARGUMENT); | |
1151 | ||
1152 | identifier_info = (thread_identifier_info_t) thread_info_out; | |
1153 | ||
1154 | s = splsched(); | |
1155 | thread_lock(thread); | |
1156 | ||
1157 | identifier_info->thread_id = thread->thread_id; | |
b0d623f7 | 1158 | identifier_info->thread_handle = thread->machine.cthread_self; |
b0d623f7 A |
1159 | if(thread->task->bsd_info) { |
1160 | identifier_info->dispatch_qaddr = identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info); | |
1161 | } else { | |
1162 | thread_unlock(thread); | |
1163 | splx(s); | |
1164 | return KERN_INVALID_ARGUMENT; | |
1165 | } | |
1166 | ||
1167 | thread_unlock(thread); | |
1168 | splx(s); | |
1169 | return KERN_SUCCESS; | |
1170 | } | |
1171 | else | |
1c79356b A |
1172 | if (flavor == THREAD_SCHED_TIMESHARE_INFO) { |
1173 | policy_timeshare_info_t ts_info; | |
1174 | ||
1175 | if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT) | |
1176 | return (KERN_INVALID_ARGUMENT); | |
1177 | ||
1178 | ts_info = (policy_timeshare_info_t)thread_info_out; | |
1179 | ||
1180 | s = splsched(); | |
1181 | thread_lock(thread); | |
1182 | ||
6d2010ae | 1183 | if (thread->sched_mode != TH_MODE_TIMESHARE) { |
1c79356b A |
1184 | thread_unlock(thread); |
1185 | splx(s); | |
1186 | ||
1187 | return (KERN_INVALID_POLICY); | |
1188 | } | |
1189 | ||
6d2010ae | 1190 | ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0; |
9bccf70c A |
1191 | if (ts_info->depressed) { |
1192 | ts_info->base_priority = DEPRESSPRI; | |
1193 | ts_info->depress_priority = thread->priority; | |
1194 | } | |
1195 | else { | |
1196 | ts_info->base_priority = thread->priority; | |
1197 | ts_info->depress_priority = -1; | |
1198 | } | |
1c79356b | 1199 | |
9bccf70c A |
1200 | ts_info->cur_priority = thread->sched_pri; |
1201 | ts_info->max_priority = thread->max_priority; | |
1c79356b A |
1202 | |
1203 | thread_unlock(thread); | |
1204 | splx(s); | |
1205 | ||
1206 | *thread_info_count = POLICY_TIMESHARE_INFO_COUNT; | |
1207 | ||
1208 | return (KERN_SUCCESS); | |
1209 | } | |
1210 | else | |
1211 | if (flavor == THREAD_SCHED_FIFO_INFO) { | |
1c79356b A |
1212 | if (*thread_info_count < POLICY_FIFO_INFO_COUNT) |
1213 | return (KERN_INVALID_ARGUMENT); | |
1214 | ||
0b4e3aa0 | 1215 | return (KERN_INVALID_POLICY); |
1c79356b A |
1216 | } |
1217 | else | |
1218 | if (flavor == THREAD_SCHED_RR_INFO) { | |
1219 | policy_rr_info_t rr_info; | |
6d2010ae A |
1220 | uint32_t quantum_time; |
1221 | uint64_t quantum_ns; | |
1222 | ||
1c79356b A |
1223 | if (*thread_info_count < POLICY_RR_INFO_COUNT) |
1224 | return (KERN_INVALID_ARGUMENT); | |
1225 | ||
1226 | rr_info = (policy_rr_info_t) thread_info_out; | |
1227 | ||
1228 | s = splsched(); | |
1229 | thread_lock(thread); | |
1230 | ||
6d2010ae | 1231 | if (thread->sched_mode == TH_MODE_TIMESHARE) { |
1c79356b A |
1232 | thread_unlock(thread); |
1233 | splx(s); | |
1234 | ||
1235 | return (KERN_INVALID_POLICY); | |
1236 | } | |
1237 | ||
6d2010ae | 1238 | rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0; |
9bccf70c A |
1239 | if (rr_info->depressed) { |
1240 | rr_info->base_priority = DEPRESSPRI; | |
1241 | rr_info->depress_priority = thread->priority; | |
1242 | } | |
1243 | else { | |
1244 | rr_info->base_priority = thread->priority; | |
1245 | rr_info->depress_priority = -1; | |
1246 | } | |
1247 | ||
6d2010ae A |
1248 | quantum_time = SCHED(initial_quantum_size)(THREAD_NULL); |
1249 | absolutetime_to_nanoseconds(quantum_time, &quantum_ns); | |
1250 | ||
1c79356b | 1251 | rr_info->max_priority = thread->max_priority; |
6d2010ae | 1252 | rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000); |
1c79356b | 1253 | |
1c79356b A |
1254 | thread_unlock(thread); |
1255 | splx(s); | |
1256 | ||
1257 | *thread_info_count = POLICY_RR_INFO_COUNT; | |
1258 | ||
1259 | return (KERN_SUCCESS); | |
1260 | } | |
1261 | ||
1262 | return (KERN_INVALID_ARGUMENT); | |
1263 | } | |
1264 | ||
1265 | void | |
91447636 A |
1266 | thread_read_times( |
1267 | thread_t thread, | |
1268 | time_value_t *user_time, | |
1269 | time_value_t *system_time) | |
1c79356b | 1270 | { |
b0d623f7 A |
1271 | clock_sec_t secs; |
1272 | clock_usec_t usecs; | |
1273 | ||
1274 | absolutetime_to_microtime(timer_grab(&thread->user_timer), &secs, &usecs); | |
1275 | user_time->seconds = (typeof(user_time->seconds))secs; | |
1276 | user_time->microseconds = usecs; | |
9bccf70c | 1277 | |
b0d623f7 A |
1278 | absolutetime_to_microtime(timer_grab(&thread->system_timer), &secs, &usecs); |
1279 | system_time->seconds = (typeof(system_time->seconds))secs; | |
1280 | system_time->microseconds = usecs; | |
1c79356b A |
1281 | } |
1282 | ||
1283 | kern_return_t | |
1284 | thread_assign( | |
91447636 A |
1285 | __unused thread_t thread, |
1286 | __unused processor_set_t new_pset) | |
1c79356b | 1287 | { |
91447636 | 1288 | return (KERN_FAILURE); |
1c79356b A |
1289 | } |
1290 | ||
1291 | /* | |
1292 | * thread_assign_default: | |
1293 | * | |
1294 | * Special version of thread_assign for assigning threads to default | |
1295 | * processor set. | |
1296 | */ | |
1297 | kern_return_t | |
1298 | thread_assign_default( | |
91447636 | 1299 | thread_t thread) |
1c79356b | 1300 | { |
2d21ac55 | 1301 | return (thread_assign(thread, &pset0)); |
1c79356b A |
1302 | } |
1303 | ||
1304 | /* | |
1305 | * thread_get_assignment | |
1306 | * | |
1307 | * Return current assignment for this thread. | |
1308 | */ | |
1309 | kern_return_t | |
1310 | thread_get_assignment( | |
91447636 | 1311 | thread_t thread, |
1c79356b A |
1312 | processor_set_t *pset) |
1313 | { | |
91447636 A |
1314 | if (thread == NULL) |
1315 | return (KERN_INVALID_ARGUMENT); | |
1316 | ||
2d21ac55 A |
1317 | *pset = &pset0; |
1318 | ||
91447636 | 1319 | return (KERN_SUCCESS); |
1c79356b A |
1320 | } |
1321 | ||
1322 | /* | |
55e303ae | 1323 | * thread_wire_internal: |
1c79356b A |
1324 | * |
1325 | * Specify that the target thread must always be able | |
1326 | * to run and to allocate memory. | |
1327 | */ | |
1328 | kern_return_t | |
55e303ae | 1329 | thread_wire_internal( |
91447636 A |
1330 | host_priv_t host_priv, |
1331 | thread_t thread, | |
1332 | boolean_t wired, | |
1333 | boolean_t *prev_state) | |
1c79356b | 1334 | { |
91447636 | 1335 | if (host_priv == NULL || thread != current_thread()) |
1c79356b A |
1336 | return (KERN_INVALID_ARGUMENT); |
1337 | ||
1338 | assert(host_priv == &realhost); | |
1339 | ||
91447636 A |
1340 | if (prev_state) |
1341 | *prev_state = (thread->options & TH_OPT_VMPRIV) != 0; | |
55e303ae | 1342 | |
1c79356b | 1343 | if (wired) { |
91447636 | 1344 | if (!(thread->options & TH_OPT_VMPRIV)) |
1c79356b | 1345 | vm_page_free_reserve(1); /* XXX */ |
91447636 A |
1346 | thread->options |= TH_OPT_VMPRIV; |
1347 | } | |
1348 | else { | |
1349 | if (thread->options & TH_OPT_VMPRIV) | |
1c79356b | 1350 | vm_page_free_reserve(-1); /* XXX */ |
91447636 | 1351 | thread->options &= ~TH_OPT_VMPRIV; |
1c79356b A |
1352 | } |
1353 | ||
91447636 | 1354 | return (KERN_SUCCESS); |
1c79356b A |
1355 | } |
1356 | ||
1c79356b A |
1357 | |
1358 | /* | |
55e303ae | 1359 | * thread_wire: |
1c79356b | 1360 | * |
55e303ae | 1361 | * User-api wrapper for thread_wire_internal() |
1c79356b | 1362 | */ |
55e303ae A |
1363 | kern_return_t |
1364 | thread_wire( | |
1365 | host_priv_t host_priv, | |
91447636 | 1366 | thread_t thread, |
55e303ae | 1367 | boolean_t wired) |
1c79356b | 1368 | { |
91447636 | 1369 | return (thread_wire_internal(host_priv, thread, wired, NULL)); |
1c79356b A |
1370 | } |
1371 | ||
91447636 A |
1372 | int split_funnel_off = 0; |
1373 | lck_grp_t *funnel_lck_grp = LCK_GRP_NULL; | |
1374 | lck_grp_attr_t *funnel_lck_grp_attr; | |
1375 | lck_attr_t *funnel_lck_attr; | |
1c79356b | 1376 | |
91447636 A |
1377 | funnel_t * |
1378 | funnel_alloc( | |
1379 | int type) | |
1c79356b | 1380 | { |
91447636 A |
1381 | lck_mtx_t *m; |
1382 | funnel_t *fnl; | |
1c79356b | 1383 | |
91447636 A |
1384 | if (funnel_lck_grp == LCK_GRP_NULL) { |
1385 | funnel_lck_grp_attr = lck_grp_attr_alloc_init(); | |
1c79356b | 1386 | |
91447636 | 1387 | funnel_lck_grp = lck_grp_alloc_init("Funnel", funnel_lck_grp_attr); |
1c79356b | 1388 | |
91447636 | 1389 | funnel_lck_attr = lck_attr_alloc_init(); |
1c79356b | 1390 | } |
1c79356b | 1391 | if ((fnl = (funnel_t *)kalloc(sizeof(funnel_t))) != 0){ |
0b4e3aa0 | 1392 | bzero((void *)fnl, sizeof(funnel_t)); |
91447636 A |
1393 | if ((m = lck_mtx_alloc_init(funnel_lck_grp, funnel_lck_attr)) == (lck_mtx_t *)NULL) { |
1394 | kfree(fnl, sizeof(funnel_t)); | |
1c79356b A |
1395 | return(THR_FUNNEL_NULL); |
1396 | } | |
1397 | fnl->fnl_mutex = m; | |
1398 | fnl->fnl_type = type; | |
1399 | } | |
1400 | return(fnl); | |
1401 | } | |
1402 | ||
1403 | void | |
1404 | funnel_free( | |
1405 | funnel_t * fnl) | |
1406 | { | |
91447636 | 1407 | lck_mtx_free(fnl->fnl_mutex, funnel_lck_grp); |
1c79356b | 1408 | if (fnl->fnl_oldmutex) |
91447636 A |
1409 | lck_mtx_free(fnl->fnl_oldmutex, funnel_lck_grp); |
1410 | kfree(fnl, sizeof(funnel_t)); | |
1c79356b A |
1411 | } |
1412 | ||
1413 | void | |
1414 | funnel_lock( | |
1415 | funnel_t * fnl) | |
1416 | { | |
91447636 | 1417 | lck_mtx_lock(fnl->fnl_mutex); |
1c79356b | 1418 | fnl->fnl_mtxholder = current_thread(); |
1c79356b A |
1419 | } |
1420 | ||
1421 | void | |
1422 | funnel_unlock( | |
1423 | funnel_t * fnl) | |
1424 | { | |
91447636 | 1425 | lck_mtx_unlock(fnl->fnl_mutex); |
2d21ac55 | 1426 | fnl->fnl_mtxholder = NULL; |
1c79356b A |
1427 | fnl->fnl_mtxrelease = current_thread(); |
1428 | } | |
1429 | ||
1430 | funnel_t * | |
1431 | thread_funnel_get( | |
1432 | void) | |
1433 | { | |
1434 | thread_t th = current_thread(); | |
1435 | ||
1436 | if (th->funnel_state & TH_FN_OWNED) { | |
1437 | return(th->funnel_lock); | |
1438 | } | |
1439 | return(THR_FUNNEL_NULL); | |
1440 | } | |
1441 | ||
1442 | boolean_t | |
1443 | thread_funnel_set( | |
1444 | funnel_t * fnl, | |
1445 | boolean_t funneled) | |
1446 | { | |
1447 | thread_t cur_thread; | |
1448 | boolean_t funnel_state_prev; | |
1449 | boolean_t intr; | |
1450 | ||
1451 | cur_thread = current_thread(); | |
1452 | funnel_state_prev = ((cur_thread->funnel_state & TH_FN_OWNED) == TH_FN_OWNED); | |
1453 | ||
1454 | if (funnel_state_prev != funneled) { | |
1455 | intr = ml_set_interrupts_enabled(FALSE); | |
1456 | ||
1457 | if (funneled == TRUE) { | |
1458 | if (cur_thread->funnel_lock) | |
2d21ac55 | 1459 | panic("Funnel lock called when holding one %p", cur_thread->funnel_lock); |
1c79356b A |
1460 | KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, |
1461 | fnl, 1, 0, 0, 0); | |
1462 | funnel_lock(fnl); | |
1463 | KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE, | |
1464 | fnl, 1, 0, 0, 0); | |
1465 | cur_thread->funnel_state |= TH_FN_OWNED; | |
1466 | cur_thread->funnel_lock = fnl; | |
1467 | } else { | |
1468 | if(cur_thread->funnel_lock->fnl_mutex != fnl->fnl_mutex) | |
1469 | panic("Funnel unlock when not holding funnel"); | |
1470 | cur_thread->funnel_state &= ~TH_FN_OWNED; | |
1471 | KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE, | |
1472 | fnl, 1, 0, 0, 0); | |
1473 | ||
1474 | cur_thread->funnel_lock = THR_FUNNEL_NULL; | |
1475 | funnel_unlock(fnl); | |
1476 | } | |
1477 | (void)ml_set_interrupts_enabled(intr); | |
1478 | } else { | |
1479 | /* if we are trying to acquire funnel recursively | |
1480 | * check for funnel to be held already | |
1481 | */ | |
1482 | if (funneled && (fnl->fnl_mutex != cur_thread->funnel_lock->fnl_mutex)) { | |
1483 | panic("thread_funnel_set: already holding a different funnel"); | |
1484 | } | |
1485 | } | |
1486 | return(funnel_state_prev); | |
1487 | } | |
1488 | ||
2d21ac55 A |
1489 | static void |
1490 | sched_call_null( | |
1491 | __unused int type, | |
1492 | __unused thread_t thread) | |
1493 | { | |
1494 | return; | |
1495 | } | |
1496 | ||
1497 | void | |
1498 | thread_sched_call( | |
1499 | thread_t thread, | |
1500 | sched_call_t call) | |
1501 | { | |
1502 | thread->sched_call = (call != NULL)? call: sched_call_null; | |
1503 | } | |
1504 | ||
1505 | void | |
1506 | thread_static_param( | |
1507 | thread_t thread, | |
1508 | boolean_t state) | |
1509 | { | |
1510 | thread_mtx_lock(thread); | |
1511 | thread->static_param = state; | |
1512 | thread_mtx_unlock(thread); | |
1513 | } | |
1c79356b | 1514 | |
b0d623f7 A |
1515 | uint64_t |
1516 | thread_tid( | |
1517 | thread_t thread) | |
1518 | { | |
1519 | return (thread != THREAD_NULL? thread->thread_id: 0); | |
1520 | } | |
1521 | ||
1522 | uint64_t | |
1523 | thread_dispatchqaddr( | |
1524 | thread_t thread) | |
1525 | { | |
1526 | uint64_t dispatchqueue_addr = 0; | |
1527 | uint64_t thread_handle = 0; | |
1528 | ||
1529 | if (thread != THREAD_NULL) { | |
b0d623f7 | 1530 | thread_handle = thread->machine.cthread_self; |
b0d623f7 A |
1531 | |
1532 | if (thread->task->bsd_info) | |
1533 | dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info); | |
1534 | } | |
1535 | ||
1536 | return (dispatchqueue_addr); | |
1537 | } | |
1538 | ||
91447636 A |
1539 | /* |
1540 | * Export routines to other components for things that are done as macros | |
1541 | * within the osfmk component. | |
1542 | */ | |
1c79356b | 1543 | |
91447636 A |
1544 | #undef thread_reference |
1545 | void thread_reference(thread_t thread); | |
1c79356b | 1546 | void |
91447636 A |
1547 | thread_reference( |
1548 | thread_t thread) | |
1c79356b | 1549 | { |
91447636 A |
1550 | if (thread != THREAD_NULL) |
1551 | thread_reference_internal(thread); | |
1c79356b A |
1552 | } |
1553 | ||
1c79356b | 1554 | #undef thread_should_halt |
91447636 | 1555 | |
1c79356b A |
1556 | boolean_t |
1557 | thread_should_halt( | |
55e303ae | 1558 | thread_t th) |
1c79356b | 1559 | { |
91447636 | 1560 | return (thread_should_halt_fast(th)); |
55e303ae | 1561 | } |
2d21ac55 A |
1562 | |
1563 | #if CONFIG_DTRACE | |
1564 | uint32_t dtrace_get_thread_predcache(thread_t thread) | |
1565 | { | |
1566 | if (thread != THREAD_NULL) | |
1567 | return thread->t_dtrace_predcache; | |
1568 | else | |
1569 | return 0; | |
1570 | } | |
1571 | ||
1572 | int64_t dtrace_get_thread_vtime(thread_t thread) | |
1573 | { | |
1574 | if (thread != THREAD_NULL) | |
1575 | return thread->t_dtrace_vtime; | |
1576 | else | |
1577 | return 0; | |
1578 | } | |
1579 | ||
1580 | int64_t dtrace_get_thread_tracing(thread_t thread) | |
1581 | { | |
1582 | if (thread != THREAD_NULL) | |
1583 | return thread->t_dtrace_tracing; | |
1584 | else | |
1585 | return 0; | |
1586 | } | |
1587 | ||
1588 | boolean_t dtrace_get_thread_reentering(thread_t thread) | |
1589 | { | |
1590 | if (thread != THREAD_NULL) | |
1591 | return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE; | |
1592 | else | |
1593 | return 0; | |
1594 | } | |
1595 | ||
1596 | vm_offset_t dtrace_get_kernel_stack(thread_t thread) | |
1597 | { | |
1598 | if (thread != THREAD_NULL) | |
1599 | return thread->kernel_stack; | |
1600 | else | |
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | int64_t dtrace_calc_thread_recent_vtime(thread_t thread) | |
1605 | { | |
1606 | #if STAT_TIME | |
1607 | if (thread != THREAD_NULL) { | |
1608 | return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)); | |
1609 | } else | |
1610 | return 0; | |
1611 | #else | |
1612 | if (thread != THREAD_NULL) { | |
1613 | processor_t processor = current_processor(); | |
1614 | uint64_t abstime = mach_absolute_time(); | |
1615 | timer_t timer; | |
1616 | ||
1617 | timer = PROCESSOR_DATA(processor, thread_timer); | |
1618 | ||
1619 | return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) + | |
1620 | (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */ | |
1621 | } else | |
1622 | return 0; | |
1623 | #endif | |
1624 | } | |
1625 | ||
1626 | void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache) | |
1627 | { | |
1628 | if (thread != THREAD_NULL) | |
1629 | thread->t_dtrace_predcache = predcache; | |
1630 | } | |
1631 | ||
1632 | void dtrace_set_thread_vtime(thread_t thread, int64_t vtime) | |
1633 | { | |
1634 | if (thread != THREAD_NULL) | |
1635 | thread->t_dtrace_vtime = vtime; | |
1636 | } | |
1637 | ||
1638 | void dtrace_set_thread_tracing(thread_t thread, int64_t accum) | |
1639 | { | |
1640 | if (thread != THREAD_NULL) | |
1641 | thread->t_dtrace_tracing = accum; | |
1642 | } | |
1643 | ||
1644 | void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool) | |
1645 | { | |
1646 | if (thread != THREAD_NULL) { | |
1647 | if (vbool) | |
1648 | thread->options |= TH_OPT_DTRACE; | |
1649 | else | |
1650 | thread->options &= (~TH_OPT_DTRACE); | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover) | |
1655 | { | |
1656 | vm_offset_t prev = 0; | |
1657 | ||
1658 | if (thread != THREAD_NULL) { | |
1659 | prev = thread->recover; | |
1660 | thread->recover = recover; | |
1661 | } | |
1662 | return prev; | |
1663 | } | |
1664 | ||
b0d623f7 A |
1665 | void dtrace_thread_bootstrap(void) |
1666 | { | |
1667 | task_t task = current_task(); | |
1668 | if(task->thread_count == 1) { | |
1669 | DTRACE_PROC(start); | |
1670 | } | |
1671 | DTRACE_PROC(lwp__start); | |
1672 | ||
1673 | } | |
2d21ac55 | 1674 | #endif /* CONFIG_DTRACE */ |