]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/thread.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 2000-2014 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>
316670eb 95#include <machine/limits.h>
91447636
A
96
97#include <kern/kern_types.h>
98#include <kern/kalloc.h>
1c79356b
A
99#include <kern/cpu_data.h>
100#include <kern/counters.h>
6d2010ae 101#include <kern/extmod_statistics.h>
1c79356b
A
102#include <kern/ipc_mig.h>
103#include <kern/ipc_tt.h>
104#include <kern/mach_param.h>
105#include <kern/machine.h>
106#include <kern/misc_protos.h>
107#include <kern/processor.h>
108#include <kern/queue.h>
109#include <kern/sched.h>
110#include <kern/sched_prim.h>
91447636
A
111#include <kern/sync_lock.h>
112#include <kern/syscall_subr.h>
1c79356b
A
113#include <kern/task.h>
114#include <kern/thread.h>
1c79356b
A
115#include <kern/host.h>
116#include <kern/zalloc.h>
1c79356b 117#include <kern/assert.h>
39236c6e
A
118#include <kern/exc_resource.h>
119#include <kern/telemetry.h>
120#if KPC
121#include <kern/kpc.h>
122#endif
91447636
A
123
124#include <ipc/ipc_kmsg.h>
125#include <ipc/ipc_port.h>
fe8ab488 126#include <bank/bank_types.h>
91447636
A
127
128#include <vm/vm_kern.h>
129#include <vm/vm_pageout.h>
130
1c79356b
A
131#include <sys/kdebug.h>
132
2d21ac55
A
133#include <mach/sdt.h>
134
1c79356b
A
135/*
136 * Exported interfaces
137 */
91447636 138#include <mach/task_server.h>
1c79356b
A
139#include <mach/thread_act_server.h>
140#include <mach/mach_host_server.h>
91447636 141#include <mach/host_priv_server.h>
fe8ab488 142#include <mach/mach_voucher_server.h>
1c79356b 143
55e303ae 144static struct zone *thread_zone;
b0d623f7
A
145static lck_grp_attr_t thread_lck_grp_attr;
146lck_attr_t thread_lck_attr;
147lck_grp_t thread_lck_grp;
1c79356b 148
a1c7dba1
A
149struct zone *thread_qos_override_zone;
150
91447636
A
151decl_simple_lock_data(static,thread_stack_lock)
152static queue_head_t thread_stack_queue;
1c79356b 153
91447636
A
154decl_simple_lock_data(static,thread_terminate_lock)
155static queue_head_t thread_terminate_queue;
1c79356b 156
55e303ae 157static struct thread thread_template, init_thread;
1c79356b 158
2d21ac55
A
159static void sched_call_null(
160 int type,
161 thread_t thread);
b0d623f7 162
91447636
A
163#ifdef MACH_BSD
164extern void proc_exit(void *);
b0d623f7 165extern uint64_t get_dispatchqueue_offset_from_proc(void *);
39236c6e
A
166extern int proc_selfpid(void);
167extern char * proc_name_address(void *p);
91447636 168#endif /* MACH_BSD */
b0d623f7 169
39236c6e 170extern int disable_exc_resource;
15129b1c 171extern int audio_active;
2d21ac55 172extern int debug_task;
b0d623f7
A
173int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */
174int task_threadmax = CONFIG_THREAD_MAX;
175
fe8ab488 176static uint64_t thread_unique_id = 100;
55e303ae 177
316670eb
A
178struct _thread_ledger_indices thread_ledgers = { -1 };
179static ledger_template_t thread_ledger_template = NULL;
180void init_thread_ledgers(void);
39236c6e
A
181int task_disable_cpumon(task_t task);
182
fe8ab488
A
183#if CONFIG_JETSAM
184void jetsam_on_ledger_cpulimit_exceeded(void);
185#endif
186
39236c6e
A
187/*
188 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
189 *
190 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
191 * stacktraces, aka micro-stackshots)
192 */
193#define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
194
195int cpumon_ustackshots_trigger_pct; /* Percentage. Level at which we start gathering telemetry. */
196void __attribute__((noinline)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
197
198/*
199 * The smallest interval over which we support limiting CPU consumption is 1ms
200 */
201#define MINIMUM_CPULIMIT_INTERVAL_MS 1
316670eb 202
91447636
A
203void
204thread_bootstrap(void)
205{
206 /*
207 * Fill in a template thread for fast initialization.
208 */
1c79356b 209
2d21ac55 210 thread_template.runq = PROCESSOR_NULL;
1c79356b 211
91447636 212 thread_template.ref_count = 2;
55e303ae 213
91447636
A
214 thread_template.reason = AST_NONE;
215 thread_template.at_safe_point = FALSE;
216 thread_template.wait_event = NO_EVENT64;
217 thread_template.wait_queue = WAIT_QUEUE_NULL;
218 thread_template.wait_result = THREAD_WAITING;
219 thread_template.options = THREAD_ABORTSAFE;
220 thread_template.state = TH_WAIT | TH_UNINT;
221 thread_template.wake_active = FALSE;
222 thread_template.continuation = THREAD_CONTINUE_NULL;
223 thread_template.parameter = NULL;
1c79356b 224
91447636 225 thread_template.importance = 0;
6d2010ae
A
226 thread_template.sched_mode = TH_MODE_NONE;
227 thread_template.sched_flags = 0;
228 thread_template.saved_mode = TH_MODE_NONE;
91447636 229 thread_template.safe_release = 0;
0b4e3aa0 230
fe8ab488
A
231 thread_template.sfi_class = SFI_CLASS_UNSPECIFIED;
232 thread_template.sfi_wait_class = SFI_CLASS_UNSPECIFIED;
233
234 thread_template.active = 0;
235 thread_template.started = 0;
236 thread_template.static_param = 0;
237 thread_template.policy_reset = 0;
238
91447636
A
239 thread_template.priority = 0;
240 thread_template.sched_pri = 0;
241 thread_template.max_priority = 0;
242 thread_template.task_priority = 0;
243 thread_template.promotions = 0;
244 thread_template.pending_promoter_index = 0;
245 thread_template.pending_promoter[0] =
316670eb 246 thread_template.pending_promoter[1] = NULL;
39236c6e 247 thread_template.rwlock_count = 0;
1c79356b 248
fe8ab488
A
249#if MACH_ASSERT
250 thread_template.SHARE_COUNT = 0;
251 thread_template.BG_COUNT = 0;
252#endif /* MACH_ASSERT */
253
91447636 254 thread_template.realtime.deadline = UINT64_MAX;
1c79356b 255
fe8ab488 256 thread_template.quantum_remaining = 0;
6d2010ae 257 thread_template.last_run_time = 0;
1c79356b 258
91447636
A
259 thread_template.computation_metered = 0;
260 thread_template.computation_epoch = 0;
1c79356b 261
fe8ab488 262#if defined(CONFIG_SCHED_TIMESHARE_CORE)
91447636 263 thread_template.sched_stamp = 0;
91447636 264 thread_template.pri_shift = INT8_MAX;
6d2010ae 265 thread_template.sched_usage = 0;
91447636 266 thread_template.cpu_usage = thread_template.cpu_delta = 0;
6d2010ae 267#endif
2d21ac55 268 thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
0b4e3aa0 269
91447636
A
270 thread_template.bound_processor = PROCESSOR_NULL;
271 thread_template.last_processor = PROCESSOR_NULL;
1c79356b 272
2d21ac55
A
273 thread_template.sched_call = sched_call_null;
274
91447636
A
275 timer_init(&thread_template.user_timer);
276 timer_init(&thread_template.system_timer);
277 thread_template.user_timer_save = 0;
278 thread_template.system_timer_save = 0;
2d21ac55
A
279 thread_template.vtimer_user_save = 0;
280 thread_template.vtimer_prof_save = 0;
281 thread_template.vtimer_rlim_save = 0;
1c79356b 282
fe8ab488
A
283 thread_template.wait_sfi_begin_time = 0;
284
91447636
A
285 thread_template.wait_timer_is_set = FALSE;
286 thread_template.wait_timer_active = 0;
1c79356b 287
91447636 288 thread_template.depress_timer_active = 0;
0b4e3aa0 289
91447636 290 thread_template.special_handler.handler = special_handler;
2d21ac55 291 thread_template.special_handler.next = NULL;
55e303ae 292
91447636 293 thread_template.recover = (vm_offset_t)NULL;
2d21ac55
A
294
295 thread_template.map = VM_MAP_NULL;
296
297#if CONFIG_DTRACE
298 thread_template.t_dtrace_predcache = 0;
299 thread_template.t_dtrace_vtime = 0;
300 thread_template.t_dtrace_tracing = 0;
301#endif /* CONFIG_DTRACE */
b0d623f7 302
39236c6e
A
303#if KPC
304 thread_template.kpc_buf = NULL;
305#endif
306
fe8ab488
A
307#if HYPERVISOR
308 thread_template.hv_thread_target = NULL;
309#endif /* HYPERVISOR */
310
2d21ac55 311 thread_template.t_chud = 0;
04b8595b
A
312
313#if (DEVELOPMENT || DEBUG)
314 thread_template.t_page_creation_throttled_hard = 0;
315 thread_template.t_page_creation_throttled_soft = 0;
316#endif /* DEVELOPMENT || DEBUG */
317 thread_template.t_page_creation_throttled = 0;
b0d623f7
A
318 thread_template.t_page_creation_count = 0;
319 thread_template.t_page_creation_time = 0;
55e303ae 320
2d21ac55
A
321 thread_template.affinity_set = NULL;
322
6d2010ae
A
323 thread_template.syscalls_unix = 0;
324 thread_template.syscalls_mach = 0;
325
316670eb
A
326 thread_template.t_ledger = LEDGER_NULL;
327 thread_template.t_threadledger = LEDGER_NULL;
fe8ab488
A
328#ifdef CONFIG_BANK
329 thread_template.t_bankledger = LEDGER_NULL;
330 thread_template.t_deduct_bank_ledger_time = 0;
331#endif
316670eb 332
39236c6e
A
333 thread_template.requested_policy = default_task_requested_policy;
334 thread_template.effective_policy = default_task_effective_policy;
335 thread_template.pended_policy = default_task_pended_policy;
336
a1c7dba1 337 bzero(&thread_template.overrides, sizeof(thread_template.overrides));
39236c6e 338
fe8ab488
A
339 thread_template.iotier_override = THROTTLE_LEVEL_NONE;
340 thread_template.thread_io_stats = NULL;
39236c6e
A
341 thread_template.thread_callout_interrupt_wakeups = thread_template.thread_callout_platform_idle_wakeups = 0;
342
343 thread_template.thread_timer_wakeups_bin_1 = thread_template.thread_timer_wakeups_bin_2 = 0;
344 thread_template.callout_woken_from_icontext = thread_template.callout_woken_from_platform_idle = 0;
345
346 thread_template.thread_tag = 0;
6d2010ae 347
fe8ab488
A
348 thread_template.ith_voucher_name = MACH_PORT_NULL;
349 thread_template.ith_voucher = IPC_VOUCHER_NULL;
350
91447636
A
351 init_thread = thread_template;
352 machine_set_current_thread(&init_thread);
1c79356b
A
353}
354
fe8ab488
A
355extern boolean_t allow_qos_policy_set;
356
55e303ae 357void
91447636 358thread_init(void)
0b4e3aa0 359{
91447636
A
360 thread_zone = zinit(
361 sizeof(struct thread),
b0d623f7 362 thread_max * sizeof(struct thread),
91447636
A
363 THREAD_CHUNK * sizeof(struct thread),
364 "threads");
6d2010ae 365
a1c7dba1
A
366 thread_qos_override_zone = zinit(
367 sizeof(struct thread_qos_override),
368 4 * thread_max * sizeof(struct thread_qos_override),
369 PAGE_SIZE,
370 "thread qos override");
371 zone_change(thread_qos_override_zone, Z_EXPAND, TRUE);
372 zone_change(thread_qos_override_zone, Z_COLLECT, TRUE);
373 zone_change(thread_qos_override_zone, Z_CALLERACCT, FALSE);
374 zone_change(thread_qos_override_zone, Z_NOENCRYPT, TRUE);
375
b0d623f7
A
376 lck_grp_attr_setdefault(&thread_lck_grp_attr);
377 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
378 lck_attr_setdefault(&thread_lck_attr);
379
91447636 380 stack_init();
55e303ae 381
91447636
A
382 /*
383 * Initialize any machine-dependent
384 * per-thread structures necessary.
385 */
386 machine_thread_init();
316670eb 387
39236c6e
A
388 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct,
389 sizeof (cpumon_ustackshots_trigger_pct))) {
390 cpumon_ustackshots_trigger_pct = CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT;
391 }
392
fe8ab488
A
393 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set, sizeof(allow_qos_policy_set));
394
316670eb 395 init_thread_ledgers();
91447636 396}
0b4e3aa0 397
91447636
A
398static void
399thread_terminate_continue(void)
400{
401 panic("thread_terminate_continue");
402 /*NOTREACHED*/
0b4e3aa0
A
403}
404
1c79356b 405/*
91447636 406 * thread_terminate_self:
1c79356b 407 */
1c79356b 408void
91447636 409thread_terminate_self(void)
1c79356b 410{
91447636 411 thread_t thread = current_thread();
6d2010ae 412
91447636
A
413 task_t task;
414 spl_t s;
b0d623f7
A
415 int threadcnt;
416
6d2010ae
A
417 pal_thread_terminate_self(thread);
418
b0d623f7 419 DTRACE_PROC(lwp__exit);
2d21ac55
A
420
421 thread_mtx_lock(thread);
422
2d21ac55
A
423 ipc_thread_disable(thread);
424
425 thread_mtx_unlock(thread);
55e303ae 426
91447636
A
427 s = splsched();
428 thread_lock(thread);
1c79356b 429
fe8ab488
A
430 assert_thread_sched_count(thread);
431
91447636 432 /*
2d21ac55
A
433 * Cancel priority depression, wait for concurrent expirations
434 * on other processors.
91447636 435 */
6d2010ae
A
436 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
437 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
1c79356b 438
fe8ab488
A
439 /* If our priority was low because of a depressed yield, restore it in case we block below */
440 set_sched_pri(thread, thread->priority);
441
91447636
A
442 if (timer_call_cancel(&thread->depress_timer))
443 thread->depress_timer_active--;
1c79356b 444 }
1c79356b 445
91447636
A
446 while (thread->depress_timer_active > 0) {
447 thread_unlock(thread);
448 splx(s);
55e303ae 449
91447636 450 delay(1);
55e303ae 451
91447636
A
452 s = splsched();
453 thread_lock(thread);
55e303ae
A
454 }
455
b0d623f7
A
456 thread_sched_call(thread, NULL);
457
91447636
A
458 thread_unlock(thread);
459 splx(s);
55e303ae 460
fe8ab488
A
461
462 thread_mtx_lock(thread);
463
2d21ac55 464 thread_policy_reset(thread);
55e303ae 465
fe8ab488 466 thread_mtx_unlock(thread);
316670eb 467
b0d623f7
A
468 task = thread->task;
469 uthread_cleanup(task, thread->uthread, task->bsd_info);
470 threadcnt = hw_atomic_sub(&task->active_thread_count, 1);
471
91447636
A
472 /*
473 * If we are the last thread to terminate and the task is
474 * associated with a BSD process, perform BSD process exit.
475 */
b0d623f7 476 if (threadcnt == 0 && task->bsd_info != NULL)
91447636 477 proc_exit(task->bsd_info);
1c79356b 478
2d21ac55
A
479 uthread_cred_free(thread->uthread);
480
91447636
A
481 s = splsched();
482 thread_lock(thread);
1c79356b 483
91447636
A
484 /*
485 * Cancel wait timer, and wait for
486 * concurrent expirations.
487 */
488 if (thread->wait_timer_is_set) {
489 thread->wait_timer_is_set = FALSE;
1c79356b 490
91447636
A
491 if (timer_call_cancel(&thread->wait_timer))
492 thread->wait_timer_active--;
493 }
1c79356b 494
91447636
A
495 while (thread->wait_timer_active > 0) {
496 thread_unlock(thread);
497 splx(s);
0b4e3aa0 498
91447636 499 delay(1);
1c79356b 500
91447636
A
501 s = splsched();
502 thread_lock(thread);
503 }
1c79356b 504
91447636
A
505 /*
506 * If there is a reserved stack, release it.
507 */
508 if (thread->reserved_stack != 0) {
6d2010ae 509 stack_free_reserved(thread);
91447636
A
510 thread->reserved_stack = 0;
511 }
1c79356b 512
91447636
A
513 /*
514 * Mark thread as terminating, and block.
515 */
516 thread->state |= TH_TERMINATE;
517 thread_mark_wait_locked(thread, THREAD_UNINT);
518 assert(thread->promotions == 0);
39236c6e 519 assert(thread->rwlock_count == 0);
91447636
A
520 thread_unlock(thread);
521 /* splsched */
1c79356b 522
91447636
A
523 thread_block((thread_continue_t)thread_terminate_continue);
524 /*NOTREACHED*/
55e303ae
A
525}
526
527void
91447636
A
528thread_deallocate(
529 thread_t thread)
1c79356b 530{
91447636 531 task_t task;
1c79356b 532
91447636
A
533 if (thread == THREAD_NULL)
534 return;
1c79356b 535
91447636
A
536 if (thread_deallocate_internal(thread) > 0)
537 return;
1c79356b 538
39236c6e
A
539 if(!(thread->state & TH_TERMINATE2))
540 panic("thread_deallocate: thread not properly terminated\n");
541
542#if KPC
543 kpc_thread_destroy(thread);
544#endif
545
91447636 546 ipc_thread_terminate(thread);
1c79356b 547
a1c7dba1
A
548 proc_thread_qos_deallocate(thread);
549
91447636 550 task = thread->task;
0b4e3aa0 551
91447636
A
552#ifdef MACH_BSD
553 {
554 void *ut = thread->uthread;
0b4e3aa0 555
91447636 556 thread->uthread = NULL;
2d21ac55 557 uthread_zone_free(ut);
91447636
A
558 }
559#endif /* MACH_BSD */
0b4e3aa0 560
316670eb
A
561 if (thread->t_ledger)
562 ledger_dereference(thread->t_ledger);
563 if (thread->t_threadledger)
564 ledger_dereference(thread->t_threadledger);
565
fe8ab488
A
566 if (IPC_VOUCHER_NULL != thread->ith_voucher)
567 ipc_voucher_release(thread->ith_voucher);
568
569 if (thread->thread_io_stats)
570 kfree(thread->thread_io_stats, sizeof(struct io_stat_info));
571
91447636
A
572 if (thread->kernel_stack != 0)
573 stack_free(thread);
0b4e3aa0 574
b0d623f7 575 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
91447636 576 machine_thread_destroy(thread);
1c79356b 577
6d2010ae
A
578 task_deallocate(task);
579
91447636
A
580 zfree(thread_zone, thread);
581}
0b4e3aa0 582
91447636
A
583/*
584 * thread_terminate_daemon:
585 *
586 * Perform final clean up for terminating threads.
587 */
588static void
589thread_terminate_daemon(void)
590{
6d2010ae
A
591 thread_t self, thread;
592 task_t task;
593
594 self = current_thread();
595 self->options |= TH_OPT_SYSTEM_CRITICAL;
0b4e3aa0 596
91447636
A
597 (void)splsched();
598 simple_lock(&thread_terminate_lock);
0b4e3aa0 599
91447636
A
600 while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
601 simple_unlock(&thread_terminate_lock);
602 (void)spllo();
0b4e3aa0 603
fe8ab488
A
604 assert(thread->SHARE_COUNT == 0);
605 assert(thread->BG_COUNT == 0);
606
91447636 607 task = thread->task;
55e303ae 608
91447636
A
609 task_lock(task);
610 task->total_user_time += timer_grab(&thread->user_timer);
316670eb
A
611 if (thread->precise_user_kernel_time) {
612 task->total_system_time += timer_grab(&thread->system_timer);
613 } else {
614 task->total_user_time += timer_grab(&thread->system_timer);
615 }
55e303ae 616
2d21ac55
A
617 task->c_switch += thread->c_switch;
618 task->p_switch += thread->p_switch;
619 task->ps_switch += thread->ps_switch;
620
6d2010ae
A
621 task->syscalls_unix += thread->syscalls_unix;
622 task->syscalls_mach += thread->syscalls_mach;
623
4b17d6b6
A
624 task->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
625 task->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
fe8ab488
A
626 task->task_gpu_ns += ml_gpu_stat(thread);
627
628 thread_update_qos_cpu_time(thread, FALSE);
91447636
A
629 queue_remove(&task->threads, thread, thread_t, task_threads);
630 task->thread_count--;
b0d623f7
A
631
632 /*
633 * If the task is being halted, and there is only one thread
634 * left in the task after this one, then wakeup that thread.
635 */
636 if (task->thread_count == 1 && task->halting)
637 thread_wakeup((event_t)&task->halting);
638
91447636 639 task_unlock(task);
1c79356b 640
b0d623f7 641 lck_mtx_lock(&tasks_threads_lock);
2d21ac55
A
642 queue_remove(&threads, thread, thread_t, threads);
643 threads_count--;
b0d623f7 644 lck_mtx_unlock(&tasks_threads_lock);
1c79356b 645
91447636 646 thread_deallocate(thread);
1c79356b 647
91447636
A
648 (void)splsched();
649 simple_lock(&thread_terminate_lock);
0b4e3aa0 650 }
1c79356b 651
91447636
A
652 assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT);
653 simple_unlock(&thread_terminate_lock);
654 /* splsched */
655
6d2010ae 656 self->options &= ~TH_OPT_SYSTEM_CRITICAL;
91447636
A
657 thread_block((thread_continue_t)thread_terminate_daemon);
658 /*NOTREACHED*/
1c79356b
A
659}
660
9bccf70c 661/*
91447636
A
662 * thread_terminate_enqueue:
663 *
664 * Enqueue a terminating thread for final disposition.
665 *
666 * Called at splsched.
9bccf70c 667 */
1c79356b 668void
91447636 669thread_terminate_enqueue(
1c79356b
A
670 thread_t thread)
671{
04b8595b 672 KERNEL_DEBUG_CONSTANT(TRACE_DATA_THREAD_TERMINATE | DBG_FUNC_NONE, thread->thread_id, 0, 0, 0, 0);
fe8ab488 673
91447636
A
674 simple_lock(&thread_terminate_lock);
675 enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
676 simple_unlock(&thread_terminate_lock);
1c79356b 677
91447636 678 thread_wakeup((event_t)&thread_terminate_queue);
1c79356b
A
679}
680
91447636
A
681/*
682 * thread_stack_daemon:
683 *
684 * Perform stack allocation as required due to
685 * invoke failures.
686 */
687static void
688thread_stack_daemon(void)
9bccf70c 689{
91447636 690 thread_t thread;
39236c6e 691 spl_t s;
91447636 692
39236c6e 693 s = splsched();
91447636
A
694 simple_lock(&thread_stack_lock);
695
696 while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
697 simple_unlock(&thread_stack_lock);
39236c6e 698 splx(s);
91447636 699
39236c6e 700 /* allocate stack with interrupts enabled so that we can call into VM */
91447636 701 stack_alloc(thread);
2d21ac55 702
39236c6e 703 s = splsched();
91447636
A
704 thread_lock(thread);
705 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
706 thread_unlock(thread);
91447636 707
91447636
A
708 simple_lock(&thread_stack_lock);
709 }
710
711 assert_wait((event_t)&thread_stack_queue, THREAD_UNINT);
712 simple_unlock(&thread_stack_lock);
39236c6e 713 splx(s);
91447636
A
714
715 thread_block((thread_continue_t)thread_stack_daemon);
9bccf70c
A
716 /*NOTREACHED*/
717}
1c79356b
A
718
719/*
91447636 720 * thread_stack_enqueue:
1c79356b 721 *
91447636 722 * Enqueue a thread for stack allocation.
1c79356b 723 *
91447636 724 * Called at splsched.
1c79356b
A
725 */
726void
91447636
A
727thread_stack_enqueue(
728 thread_t thread)
1c79356b 729{
91447636
A
730 simple_lock(&thread_stack_lock);
731 enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
732 simple_unlock(&thread_stack_lock);
1c79356b 733
91447636
A
734 thread_wakeup((event_t)&thread_stack_queue);
735}
9bccf70c 736
91447636
A
737void
738thread_daemon_init(void)
739{
740 kern_return_t result;
6d2010ae 741 thread_t thread = NULL;
0b4e3aa0 742
91447636
A
743 simple_lock_init(&thread_terminate_lock, 0);
744 queue_init(&thread_terminate_queue);
1c79356b 745
91447636
A
746 result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
747 if (result != KERN_SUCCESS)
748 panic("thread_daemon_init: thread_terminate_daemon");
1c79356b 749
91447636 750 thread_deallocate(thread);
1c79356b 751
91447636
A
752 simple_lock_init(&thread_stack_lock, 0);
753 queue_init(&thread_stack_queue);
1c79356b 754
91447636
A
755 result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
756 if (result != KERN_SUCCESS)
757 panic("thread_daemon_init: thread_stack_daemon");
1c79356b 758
91447636 759 thread_deallocate(thread);
1c79356b
A
760}
761
1c79356b
A
762/*
763 * Create a new thread.
55e303ae 764 * Doesn't start the thread running.
fe8ab488
A
765 *
766 * Task and tasks_threads_lock are returned locked on success.
1c79356b 767 */
55e303ae
A
768static kern_return_t
769thread_create_internal(
770 task_t parent_task,
1c79356b 771 integer_t priority,
91447636 772 thread_continue_t continuation,
b0d623f7
A
773 int options,
774#define TH_OPTION_NONE 0x00
775#define TH_OPTION_NOCRED 0x01
776#define TH_OPTION_NOSUSP 0x02
55e303ae 777 thread_t *out_thread)
1c79356b 778{
55e303ae 779 thread_t new_thread;
55e303ae 780 static thread_t first_thread;
1c79356b
A
781
782 /*
783 * Allocate a thread and initialize static fields
784 */
b0d623f7 785 if (first_thread == THREAD_NULL)
91447636 786 new_thread = first_thread = current_thread();
55e303ae
A
787 else
788 new_thread = (thread_t)zalloc(thread_zone);
b0d623f7 789 if (new_thread == THREAD_NULL)
1c79356b
A
790 return (KERN_RESOURCE_SHORTAGE);
791
55e303ae
A
792 if (new_thread != first_thread)
793 *new_thread = thread_template;
794
795#ifdef MACH_BSD
b0d623f7
A
796 new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
797 if (new_thread->uthread == NULL) {
798 zfree(thread_zone, new_thread);
799 return (KERN_RESOURCE_SHORTAGE);
55e303ae
A
800 }
801#endif /* MACH_BSD */
1c79356b 802
55e303ae
A
803 if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
804#ifdef MACH_BSD
b0d623f7 805 void *ut = new_thread->uthread;
1c79356b 806
b0d623f7
A
807 new_thread->uthread = NULL;
808 /* cred free may not be necessary */
809 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
810 uthread_cred_free(ut);
811 uthread_zone_free(ut);
55e303ae 812#endif /* MACH_BSD */
b0d623f7 813
91447636 814 zfree(thread_zone, new_thread);
55e303ae
A
815 return (KERN_FAILURE);
816 }
817
316670eb 818 new_thread->task = parent_task;
55e303ae
A
819
820 thread_lock_init(new_thread);
821 wake_lock_init(new_thread);
822
b0d623f7 823 lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
0b4e3aa0 824
55e303ae 825 ipc_thread_init(new_thread);
55e303ae 826
91447636 827 new_thread->continuation = continuation;
0b4e3aa0 828
fe8ab488
A
829 /* Allocate I/O Statistics structure */
830 new_thread->thread_io_stats = (io_stat_info_t)kalloc(sizeof(struct io_stat_info));
831 assert(new_thread->thread_io_stats != NULL);
832 bzero(new_thread->thread_io_stats, sizeof(struct io_stat_info));
833
834#if CONFIG_IOSCHED
835 /* Clear out the I/O Scheduling info for AppleFSCompression */
836 new_thread->decmp_upl = NULL;
837#endif /* CONFIG_IOSCHED */
838
b0d623f7 839 lck_mtx_lock(&tasks_threads_lock);
1c79356b
A
840 task_lock(parent_task);
841
b0d623f7
A
842 if ( !parent_task->active || parent_task->halting ||
843 ((options & TH_OPTION_NOSUSP) != 0 &&
844 parent_task->suspend_count > 0) ||
845 (parent_task->thread_count >= task_threadmax &&
846 parent_task != kernel_task) ) {
1c79356b 847 task_unlock(parent_task);
b0d623f7 848 lck_mtx_unlock(&tasks_threads_lock);
55e303ae
A
849
850#ifdef MACH_BSD
851 {
55e303ae
A
852 void *ut = new_thread->uthread;
853
854 new_thread->uthread = NULL;
2d21ac55
A
855 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
856 /* cred free may not be necessary */
857 uthread_cred_free(ut);
858 uthread_zone_free(ut);
55e303ae
A
859 }
860#endif /* MACH_BSD */
91447636
A
861 ipc_thread_disable(new_thread);
862 ipc_thread_terminate(new_thread);
fe8ab488 863 kfree(new_thread->thread_io_stats, sizeof(struct io_stat_info));
b0d623f7 864 lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
55e303ae 865 machine_thread_destroy(new_thread);
91447636 866 zfree(thread_zone, new_thread);
1c79356b
A
867 return (KERN_FAILURE);
868 }
869
b0d623f7
A
870 /* New threads inherit any default state on the task */
871 machine_thread_inherit_taskwide(new_thread, parent_task);
872
91447636 873 task_reference_internal(parent_task);
55e303ae 874
316670eb
A
875 if (new_thread->task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
876 /*
877 * This task has a per-thread CPU limit; make sure this new thread
878 * gets its limit set too, before it gets out of the kernel.
879 */
880 set_astledger(new_thread);
881 }
fe8ab488
A
882
883 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
884 if ((new_thread->t_threadledger = ledger_instantiate(thread_ledger_template,
885 LEDGER_CREATE_INACTIVE_ENTRIES)) != LEDGER_NULL) {
886
887 ledger_entry_setactive(new_thread->t_threadledger, thread_ledgers.cpu_time);
888 }
889
890 new_thread->cpu_time_last_qos = 0;
891#ifdef CONFIG_BANK
892 new_thread->t_bankledger = LEDGER_NULL;
893 new_thread->t_deduct_bank_ledger_time = 0;
894#endif
895
316670eb
A
896 new_thread->t_ledger = new_thread->task->ledger;
897 if (new_thread->t_ledger)
898 ledger_reference(new_thread->t_ledger);
899
fe8ab488
A
900#if defined(CONFIG_SCHED_MULTIQ)
901 /* Cache the task's sched_group */
902 new_thread->sched_group = parent_task->sched_group;
903#endif /* defined(CONFIG_SCHED_MULTIQ) */
904
55e303ae
A
905 /* Cache the task's map */
906 new_thread->map = parent_task->map;
1c79356b 907
91447636
A
908 timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
909 timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
1c79356b 910
b0d623f7
A
911#if CONFIG_COUNTERS
912 /*
913 * If parent task has any reservations, they need to be propagated to this
914 * thread.
915 */
916 new_thread->t_chud = (TASK_PMC_FLAG == (parent_task->t_chud & TASK_PMC_FLAG)) ?
917 THREAD_PMC_FLAG : 0U;
918#endif
39236c6e
A
919#if KPC
920 kpc_thread_create(new_thread);
921#endif
922
923 /* Only need to update policies pushed from task to thread */
924 new_thread->requested_policy.bg_iotier = parent_task->effective_policy.bg_iotier;
925 new_thread->requested_policy.terminated = parent_task->effective_policy.terminated;
b0d623f7 926
1c79356b 927 /* Set the thread's scheduling parameters */
6d2010ae
A
928 new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
929 new_thread->sched_flags = 0;
55e303ae
A
930 new_thread->max_priority = parent_task->max_priority;
931 new_thread->task_priority = parent_task->priority;
932 new_thread->priority = (priority < 0)? parent_task->priority: priority;
933 if (new_thread->priority > new_thread->max_priority)
934 new_thread->priority = new_thread->max_priority;
39236c6e 935 new_thread->importance = new_thread->priority - new_thread->task_priority;
316670eb 936 new_thread->saved_importance = new_thread->importance;
316670eb 937
fe8ab488 938#if defined(CONFIG_SCHED_TIMESHARE_CORE)
55e303ae 939 new_thread->sched_stamp = sched_tick;
2d21ac55 940 new_thread->pri_shift = sched_pri_shift;
fe8ab488
A
941#endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
942
943 if (parent_task->max_priority <= MAXPRI_THROTTLE) {
944 sched_set_thread_throttled(new_thread, TRUE);
945 }
946
6d2010ae 947 SCHED(compute_priority)(new_thread, FALSE);
1c79356b 948
fe8ab488
A
949 thread_policy_create(new_thread);
950
951 /* Chain the thread onto the task's list */
952 queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
953 parent_task->thread_count++;
954
955 /* So terminating threads don't need to take the task lock to decrement */
956 hw_atomic_add(&parent_task->active_thread_count, 1);
957
958 /* Protected by the tasks_threads_lock */
959 new_thread->thread_id = ++thread_unique_id;
960
961 queue_enter(&threads, new_thread, thread_t, threads);
962 threads_count++;
963
55e303ae 964 new_thread->active = TRUE;
1c79356b 965
55e303ae 966 *out_thread = new_thread;
1c79356b
A
967
968 {
9bccf70c 969 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
1c79356b 970
55e303ae
A
971 kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
972
316670eb
A
973 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
974 TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
975 (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
1c79356b 976
9bccf70c
A
977 kdbg_trace_string(parent_task->bsd_info,
978 &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
979
316670eb
A
980 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
981 TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE,
982 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
1c79356b
A
983 }
984
2d21ac55
A
985 DTRACE_PROC1(lwp__create, thread_t, *out_thread);
986
1c79356b
A
987 return (KERN_SUCCESS);
988}
989
6d2010ae
A
990static kern_return_t
991thread_create_internal2(
1c79356b 992 task_t task,
6d2010ae
A
993 thread_t *new_thread,
994 boolean_t from_user)
1c79356b 995{
1c79356b 996 kern_return_t result;
9bccf70c 997 thread_t thread;
1c79356b 998
55e303ae
A
999 if (task == TASK_NULL || task == kernel_task)
1000 return (KERN_INVALID_ARGUMENT);
1c79356b 1001
b0d623f7 1002 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
1c79356b
A
1003 if (result != KERN_SUCCESS)
1004 return (result);
1005
55e303ae
A
1006 thread->user_stop_count = 1;
1007 thread_hold(thread);
9bccf70c 1008 if (task->suspend_count > 0)
55e303ae 1009 thread_hold(thread);
1c79356b 1010
6d2010ae
A
1011 if (from_user)
1012 extmod_statistics_incr_thread_create(task);
1013
9bccf70c 1014 task_unlock(task);
b0d623f7 1015 lck_mtx_unlock(&tasks_threads_lock);
1c79356b 1016
55e303ae 1017 *new_thread = thread;
1c79356b
A
1018
1019 return (KERN_SUCCESS);
1020}
1021
6d2010ae 1022/* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1c79356b 1023kern_return_t
6d2010ae
A
1024thread_create(
1025 task_t task,
1026 thread_t *new_thread);
1027
1028kern_return_t
1029thread_create(
1030 task_t task,
1031 thread_t *new_thread)
1032{
1033 return thread_create_internal2(task, new_thread, FALSE);
1034}
1035
1036kern_return_t
1037thread_create_from_user(
1038 task_t task,
1039 thread_t *new_thread)
1040{
1041 return thread_create_internal2(task, new_thread, TRUE);
1042}
1043
1044static kern_return_t
1045thread_create_running_internal2(
9bccf70c 1046 register task_t task,
1c79356b
A
1047 int flavor,
1048 thread_state_t new_state,
1049 mach_msg_type_number_t new_state_count,
6d2010ae
A
1050 thread_t *new_thread,
1051 boolean_t from_user)
1c79356b
A
1052{
1053 register kern_return_t result;
9bccf70c 1054 thread_t thread;
9bccf70c 1055
55e303ae
A
1056 if (task == TASK_NULL || task == kernel_task)
1057 return (KERN_INVALID_ARGUMENT);
1c79356b 1058
b0d623f7 1059 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
1c79356b
A
1060 if (result != KERN_SUCCESS)
1061 return (result);
1062
91447636
A
1063 result = machine_thread_set_state(
1064 thread, flavor, new_state, new_state_count);
1c79356b 1065 if (result != KERN_SUCCESS) {
9bccf70c 1066 task_unlock(task);
b0d623f7 1067 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1068
55e303ae 1069 thread_terminate(thread);
91447636 1070 thread_deallocate(thread);
1c79356b
A
1071 return (result);
1072 }
1073
91447636 1074 thread_mtx_lock(thread);
2d21ac55 1075 thread_start_internal(thread);
91447636 1076 thread_mtx_unlock(thread);
2d21ac55 1077
6d2010ae
A
1078 if (from_user)
1079 extmod_statistics_incr_thread_create(task);
1080
9bccf70c 1081 task_unlock(task);
b0d623f7 1082 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1083
55e303ae 1084 *new_thread = thread;
9bccf70c 1085
1c79356b
A
1086 return (result);
1087}
1088
6d2010ae
A
1089/* Prototype, see justification above */
1090kern_return_t
1091thread_create_running(
1092 register task_t task,
1093 int flavor,
1094 thread_state_t new_state,
1095 mach_msg_type_number_t new_state_count,
1096 thread_t *new_thread);
1097
1098kern_return_t
1099thread_create_running(
1100 register task_t task,
1101 int flavor,
1102 thread_state_t new_state,
1103 mach_msg_type_number_t new_state_count,
1104 thread_t *new_thread)
1105{
1106 return thread_create_running_internal2(
1107 task, flavor, new_state, new_state_count,
1108 new_thread, FALSE);
1109}
1110
1111kern_return_t
1112thread_create_running_from_user(
1113 register task_t task,
1114 int flavor,
1115 thread_state_t new_state,
1116 mach_msg_type_number_t new_state_count,
1117 thread_t *new_thread)
1118{
1119 return thread_create_running_internal2(
1120 task, flavor, new_state, new_state_count,
1121 new_thread, TRUE);
1122}
1123
b0d623f7
A
1124kern_return_t
1125thread_create_workq(
1126 task_t task,
b7266188 1127 thread_continue_t thread_return,
b0d623f7
A
1128 thread_t *new_thread)
1129{
1130 kern_return_t result;
1131 thread_t thread;
1132
1133 if (task == TASK_NULL || task == kernel_task)
1134 return (KERN_INVALID_ARGUMENT);
1135
b7266188 1136 result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
b0d623f7
A
1137 if (result != KERN_SUCCESS)
1138 return (result);
1139
1140 thread->user_stop_count = 1;
1141 thread_hold(thread);
1142 if (task->suspend_count > 0)
1143 thread_hold(thread);
1144
1145 task_unlock(task);
1146 lck_mtx_unlock(&tasks_threads_lock);
1147
1148 *new_thread = thread;
1149
1150 return (KERN_SUCCESS);
1151}
1152
1c79356b 1153/*
91447636 1154 * kernel_thread_create:
1c79356b 1155 *
55e303ae
A
1156 * Create a thread in the kernel task
1157 * to execute in kernel context.
1c79356b 1158 */
91447636 1159kern_return_t
55e303ae 1160kernel_thread_create(
91447636
A
1161 thread_continue_t continuation,
1162 void *parameter,
1163 integer_t priority,
1164 thread_t *new_thread)
1c79356b
A
1165{
1166 kern_return_t result;
1167 thread_t thread;
91447636 1168 task_t task = kernel_task;
1c79356b 1169
b0d623f7 1170 result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
9bccf70c 1171 if (result != KERN_SUCCESS)
91447636 1172 return (result);
1c79356b 1173
9bccf70c 1174 task_unlock(task);
b0d623f7 1175 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1176
91447636 1177 stack_alloc(thread);
55e303ae
A
1178 assert(thread->kernel_stack != 0);
1179 thread->reserved_stack = thread->kernel_stack;
1180
91447636 1181 thread->parameter = parameter;
55e303ae 1182
2d21ac55
A
1183if(debug_task & 1)
1184 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
91447636
A
1185 *new_thread = thread;
1186
1187 return (result);
55e303ae
A
1188}
1189
91447636
A
1190kern_return_t
1191kernel_thread_start_priority(
1192 thread_continue_t continuation,
1193 void *parameter,
1194 integer_t priority,
1195 thread_t *new_thread)
55e303ae 1196{
91447636 1197 kern_return_t result;
55e303ae 1198 thread_t thread;
1c79356b 1199
91447636
A
1200 result = kernel_thread_create(continuation, parameter, priority, &thread);
1201 if (result != KERN_SUCCESS)
1202 return (result);
1c79356b 1203
b0d623f7
A
1204 *new_thread = thread;
1205
91447636 1206 thread_mtx_lock(thread);
2d21ac55 1207 thread_start_internal(thread);
91447636 1208 thread_mtx_unlock(thread);
1c79356b 1209
91447636
A
1210 return (result);
1211}
1212
1213kern_return_t
1214kernel_thread_start(
1215 thread_continue_t continuation,
1216 void *parameter,
1217 thread_t *new_thread)
1218{
1219 return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
1c79356b
A
1220}
1221
b0d623f7 1222
1c79356b 1223kern_return_t
91447636
A
1224thread_info_internal(
1225 register thread_t thread,
1c79356b
A
1226 thread_flavor_t flavor,
1227 thread_info_t thread_info_out, /* ptr to OUT array */
1228 mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
1229{
1c79356b
A
1230 int state, flags;
1231 spl_t s;
1232
1233 if (thread == THREAD_NULL)
1234 return (KERN_INVALID_ARGUMENT);
1235
1236 if (flavor == THREAD_BASIC_INFO) {
1237 register thread_basic_info_t basic_info;
1238
1239 if (*thread_info_count < THREAD_BASIC_INFO_COUNT)
1240 return (KERN_INVALID_ARGUMENT);
1241
1242 basic_info = (thread_basic_info_t) thread_info_out;
1243
1244 s = splsched();
1245 thread_lock(thread);
1246
1247 /* fill in info */
1248
1249 thread_read_times(thread, &basic_info->user_time,
1250 &basic_info->system_time);
1251
0b4e3aa0
A
1252 /*
1253 * Update lazy-evaluated scheduler info because someone wants it.
1254 */
6d2010ae
A
1255 if (SCHED(can_update_priority)(thread))
1256 SCHED(update_priority)(thread);
0b4e3aa0
A
1257
1258 basic_info->sleep_time = 0;
1259
1260 /*
1261 * To calculate cpu_usage, first correct for timer rate,
1262 * then for 5/8 ageing. The correction factor [3/5] is
1263 * (1/(5/8) - 1).
1264 */
6d2010ae 1265 basic_info->cpu_usage = 0;
fe8ab488 1266#if defined(CONFIG_SCHED_TIMESHARE_CORE)
6d2010ae
A
1267 if (sched_tick_interval) {
1268 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1269 * TH_USAGE_SCALE) / sched_tick_interval);
1270 basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
1271 }
1272#endif
1273
91447636
A
1274 if (basic_info->cpu_usage > TH_USAGE_SCALE)
1275 basic_info->cpu_usage = TH_USAGE_SCALE;
1c79356b 1276
6d2010ae 1277 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
0b4e3aa0 1278 POLICY_TIMESHARE: POLICY_RR);
1c79356b
A
1279
1280 flags = 0;
39236c6e 1281 if (thread->options & TH_OPT_IDLE_THREAD)
0b4e3aa0
A
1282 flags |= TH_FLAGS_IDLE;
1283
91447636 1284 if (!thread->kernel_stack)
0b4e3aa0 1285 flags |= TH_FLAGS_SWAPPED;
1c79356b
A
1286
1287 state = 0;
9bccf70c 1288 if (thread->state & TH_TERMINATE)
1c79356b
A
1289 state = TH_STATE_HALTED;
1290 else
1291 if (thread->state & TH_RUN)
1292 state = TH_STATE_RUNNING;
1293 else
1294 if (thread->state & TH_UNINT)
1295 state = TH_STATE_UNINTERRUPTIBLE;
1296 else
1297 if (thread->state & TH_SUSP)
1298 state = TH_STATE_STOPPED;
1299 else
1300 if (thread->state & TH_WAIT)
1301 state = TH_STATE_WAITING;
1302
1303 basic_info->run_state = state;
1304 basic_info->flags = flags;
1305
91447636 1306 basic_info->suspend_count = thread->user_stop_count;
1c79356b
A
1307
1308 thread_unlock(thread);
1309 splx(s);
1310
1311 *thread_info_count = THREAD_BASIC_INFO_COUNT;
1312
1313 return (KERN_SUCCESS);
1314 }
1315 else
b0d623f7
A
1316 if (flavor == THREAD_IDENTIFIER_INFO) {
1317 register thread_identifier_info_t identifier_info;
1318
1319 if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT)
1320 return (KERN_INVALID_ARGUMENT);
1321
1322 identifier_info = (thread_identifier_info_t) thread_info_out;
1323
1324 s = splsched();
1325 thread_lock(thread);
1326
1327 identifier_info->thread_id = thread->thread_id;
b0d623f7 1328 identifier_info->thread_handle = thread->machine.cthread_self;
b0d623f7
A
1329 if(thread->task->bsd_info) {
1330 identifier_info->dispatch_qaddr = identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1331 } else {
1332 thread_unlock(thread);
1333 splx(s);
1334 return KERN_INVALID_ARGUMENT;
1335 }
1336
1337 thread_unlock(thread);
1338 splx(s);
1339 return KERN_SUCCESS;
1340 }
1341 else
1c79356b
A
1342 if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
1343 policy_timeshare_info_t ts_info;
1344
1345 if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT)
1346 return (KERN_INVALID_ARGUMENT);
1347
1348 ts_info = (policy_timeshare_info_t)thread_info_out;
1349
1350 s = splsched();
1351 thread_lock(thread);
1352
6d2010ae 1353 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1c79356b
A
1354 thread_unlock(thread);
1355 splx(s);
1356
1357 return (KERN_INVALID_POLICY);
1358 }
1359
6d2010ae 1360 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
9bccf70c
A
1361 if (ts_info->depressed) {
1362 ts_info->base_priority = DEPRESSPRI;
1363 ts_info->depress_priority = thread->priority;
1364 }
1365 else {
1366 ts_info->base_priority = thread->priority;
1367 ts_info->depress_priority = -1;
1368 }
1c79356b 1369
9bccf70c
A
1370 ts_info->cur_priority = thread->sched_pri;
1371 ts_info->max_priority = thread->max_priority;
1c79356b
A
1372
1373 thread_unlock(thread);
1374 splx(s);
1375
1376 *thread_info_count = POLICY_TIMESHARE_INFO_COUNT;
1377
1378 return (KERN_SUCCESS);
1379 }
1380 else
1381 if (flavor == THREAD_SCHED_FIFO_INFO) {
1c79356b
A
1382 if (*thread_info_count < POLICY_FIFO_INFO_COUNT)
1383 return (KERN_INVALID_ARGUMENT);
1384
0b4e3aa0 1385 return (KERN_INVALID_POLICY);
1c79356b
A
1386 }
1387 else
1388 if (flavor == THREAD_SCHED_RR_INFO) {
1389 policy_rr_info_t rr_info;
6d2010ae
A
1390 uint32_t quantum_time;
1391 uint64_t quantum_ns;
1392
1c79356b
A
1393 if (*thread_info_count < POLICY_RR_INFO_COUNT)
1394 return (KERN_INVALID_ARGUMENT);
1395
1396 rr_info = (policy_rr_info_t) thread_info_out;
1397
1398 s = splsched();
1399 thread_lock(thread);
1400
6d2010ae 1401 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1c79356b
A
1402 thread_unlock(thread);
1403 splx(s);
1404
1405 return (KERN_INVALID_POLICY);
1406 }
1407
6d2010ae 1408 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
9bccf70c
A
1409 if (rr_info->depressed) {
1410 rr_info->base_priority = DEPRESSPRI;
1411 rr_info->depress_priority = thread->priority;
1412 }
1413 else {
1414 rr_info->base_priority = thread->priority;
1415 rr_info->depress_priority = -1;
1416 }
1417
6d2010ae
A
1418 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
1419 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
1420
1c79356b 1421 rr_info->max_priority = thread->max_priority;
6d2010ae 1422 rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1c79356b 1423
1c79356b
A
1424 thread_unlock(thread);
1425 splx(s);
1426
1427 *thread_info_count = POLICY_RR_INFO_COUNT;
1428
1429 return (KERN_SUCCESS);
1430 }
1431
1432 return (KERN_INVALID_ARGUMENT);
1433}
1434
1435void
91447636
A
1436thread_read_times(
1437 thread_t thread,
1438 time_value_t *user_time,
1439 time_value_t *system_time)
1c79356b 1440{
b0d623f7
A
1441 clock_sec_t secs;
1442 clock_usec_t usecs;
316670eb 1443 uint64_t tval_user, tval_system;
b0d623f7 1444
316670eb
A
1445 tval_user = timer_grab(&thread->user_timer);
1446 tval_system = timer_grab(&thread->system_timer);
9bccf70c 1447
316670eb
A
1448 if (thread->precise_user_kernel_time) {
1449 absolutetime_to_microtime(tval_user, &secs, &usecs);
1450 user_time->seconds = (typeof(user_time->seconds))secs;
1451 user_time->microseconds = usecs;
1452
1453 absolutetime_to_microtime(tval_system, &secs, &usecs);
1454 system_time->seconds = (typeof(system_time->seconds))secs;
1455 system_time->microseconds = usecs;
1456 } else {
1457 /* system_timer may represent either sys or user */
1458 tval_user += tval_system;
1459 absolutetime_to_microtime(tval_user, &secs, &usecs);
1460 user_time->seconds = (typeof(user_time->seconds))secs;
1461 user_time->microseconds = usecs;
1462
1463 system_time->seconds = 0;
1464 system_time->microseconds = 0;
1465 }
1c79356b
A
1466}
1467
fe8ab488
A
1468uint64_t thread_get_runtime_self(void)
1469{
1470 boolean_t interrupt_state;
1471 uint64_t runtime;
1472 thread_t thread = NULL;
1473 processor_t processor = NULL;
1474
1475 thread = current_thread();
1476
1477 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
1478 interrupt_state = ml_set_interrupts_enabled(FALSE);
1479 processor = current_processor();
1480 timer_switch(PROCESSOR_DATA(processor, thread_timer), mach_absolute_time(), PROCESSOR_DATA(processor, thread_timer));
1481 runtime = (timer_grab(&thread->user_timer) + timer_grab(&thread->system_timer));
1482 ml_set_interrupts_enabled(interrupt_state);
1483
1484 return runtime;
1485}
1486
1c79356b
A
1487kern_return_t
1488thread_assign(
91447636
A
1489 __unused thread_t thread,
1490 __unused processor_set_t new_pset)
1c79356b 1491{
91447636 1492 return (KERN_FAILURE);
1c79356b
A
1493}
1494
1495/*
1496 * thread_assign_default:
1497 *
1498 * Special version of thread_assign for assigning threads to default
1499 * processor set.
1500 */
1501kern_return_t
1502thread_assign_default(
91447636 1503 thread_t thread)
1c79356b 1504{
2d21ac55 1505 return (thread_assign(thread, &pset0));
1c79356b
A
1506}
1507
1508/*
1509 * thread_get_assignment
1510 *
1511 * Return current assignment for this thread.
1512 */
1513kern_return_t
1514thread_get_assignment(
91447636 1515 thread_t thread,
1c79356b
A
1516 processor_set_t *pset)
1517{
91447636
A
1518 if (thread == NULL)
1519 return (KERN_INVALID_ARGUMENT);
1520
2d21ac55
A
1521 *pset = &pset0;
1522
91447636 1523 return (KERN_SUCCESS);
1c79356b
A
1524}
1525
1526/*
55e303ae 1527 * thread_wire_internal:
1c79356b
A
1528 *
1529 * Specify that the target thread must always be able
1530 * to run and to allocate memory.
1531 */
1532kern_return_t
55e303ae 1533thread_wire_internal(
91447636
A
1534 host_priv_t host_priv,
1535 thread_t thread,
1536 boolean_t wired,
1537 boolean_t *prev_state)
1c79356b 1538{
91447636 1539 if (host_priv == NULL || thread != current_thread())
1c79356b
A
1540 return (KERN_INVALID_ARGUMENT);
1541
1542 assert(host_priv == &realhost);
1543
91447636
A
1544 if (prev_state)
1545 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
55e303ae 1546
1c79356b 1547 if (wired) {
91447636 1548 if (!(thread->options & TH_OPT_VMPRIV))
1c79356b 1549 vm_page_free_reserve(1); /* XXX */
91447636
A
1550 thread->options |= TH_OPT_VMPRIV;
1551 }
1552 else {
1553 if (thread->options & TH_OPT_VMPRIV)
1c79356b 1554 vm_page_free_reserve(-1); /* XXX */
91447636 1555 thread->options &= ~TH_OPT_VMPRIV;
1c79356b
A
1556 }
1557
91447636 1558 return (KERN_SUCCESS);
1c79356b
A
1559}
1560
1c79356b
A
1561
1562/*
55e303ae 1563 * thread_wire:
1c79356b 1564 *
55e303ae 1565 * User-api wrapper for thread_wire_internal()
1c79356b 1566 */
55e303ae
A
1567kern_return_t
1568thread_wire(
1569 host_priv_t host_priv,
91447636 1570 thread_t thread,
55e303ae 1571 boolean_t wired)
1c79356b 1572{
91447636 1573 return (thread_wire_internal(host_priv, thread, wired, NULL));
1c79356b
A
1574}
1575
39236c6e 1576
fe8ab488
A
1577boolean_t
1578set_vm_privilege(boolean_t privileged)
1579{
1580 boolean_t was_vmpriv;
1581
1582 if (current_thread()->options & TH_OPT_VMPRIV)
1583 was_vmpriv = TRUE;
1584 else
1585 was_vmpriv = FALSE;
1586
1587 if (privileged != FALSE)
1588 current_thread()->options |= TH_OPT_VMPRIV;
1589 else
1590 current_thread()->options &= ~TH_OPT_VMPRIV;
1591
1592 return (was_vmpriv);
1593}
1594
1595
39236c6e
A
1596/*
1597 * XXX assuming current thread only, for now...
1598 */
1599void
1600thread_guard_violation(thread_t thread, unsigned type)
1601{
1602 assert(thread == current_thread());
1603
1604 spl_t s = splsched();
1605 /*
1606 * Use the saved state area of the thread structure
1607 * to store all info required to handle the AST when
1608 * returning to userspace
1609 */
1610 thread->guard_exc_info.type = type;
1611 thread_ast_set(thread, AST_GUARD);
1612 ast_propagate(thread->ast);
1613
1614 splx(s);
1615}
1616
1617/*
1618 * guard_ast:
1619 *
1620 * Handle AST_GUARD for a thread. This routine looks at the
1621 * state saved in the thread structure to determine the cause
1622 * of this exception. Based on this value, it invokes the
1623 * appropriate routine which determines other exception related
1624 * info and raises the exception.
1625 */
1626void
1627guard_ast(thread_t thread)
1628{
1629 if (thread->guard_exc_info.type == GUARD_TYPE_MACH_PORT)
1630 mach_port_guard_ast(thread);
1631 else
1632 fd_guard_ast(thread);
1633}
1634
316670eb 1635static void
39236c6e 1636thread_cputime_callback(int warning, __unused const void *arg0, __unused const void *arg1)
316670eb 1637{
39236c6e
A
1638 if (warning == LEDGER_WARNING_ROSE_ABOVE) {
1639#if CONFIG_TELEMETRY
1640 /*
1641 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1642 * on the entire task so there are micro-stackshots available if and when
1643 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1644 * for this thread only; but now that this task is suspect, knowing what all of
1645 * its threads are up to will be useful.
1646 */
1647 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 1);
1648#endif
1649 return;
1650 }
1651
1652#if CONFIG_TELEMETRY
1653 /*
1654 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1655 * exceeded the limit, turn telemetry off for the task.
1656 */
1657 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 0);
1658#endif
1659
1660 if (warning == 0) {
1661 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1662 }
1663}
1664
1665void __attribute__((noinline))
1666THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1667{
1668 int pid = 0;
1669 task_t task = current_task();
1670 thread_t thread = current_thread();
1671 uint64_t tid = thread->thread_id;
1672 char *procname = (char *) "unknown";
1673 time_value_t thread_total_time = {0, 0};
1674 time_value_t thread_system_time;
1675 time_value_t thread_user_time;
1676 int action;
1677 uint8_t percentage;
1678 uint32_t limit_percent;
1679 uint32_t usage_percent;
1680 uint32_t interval_sec;
1681 uint64_t interval_ns;
1682 uint64_t balance_ns;
1683 boolean_t fatal = FALSE;
1684
1685 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
1686 struct ledger_entry_info lei;
1687
316670eb
A
1688 assert(thread->t_threadledger != LEDGER_NULL);
1689
1690 /*
39236c6e 1691 * Now that a thread has tripped the monitor, disable it for the entire task.
316670eb 1692 */
39236c6e
A
1693 task_lock(task);
1694 if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) == 0) {
1695 /*
1696 * The CPU usage monitor has been disabled on our task, so some other
1697 * thread must have gotten here first. We only send one exception per
1698 * task lifetime, so there's nothing left for us to do here.
1699 */
1700 task_unlock(task);
1701 return;
1702 }
1703 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_FATAL_CPUMON) {
1704 fatal = TRUE;
1705 }
1706 task_disable_cpumon(task);
1707 task_unlock(task);
1708
1709#ifdef MACH_BSD
1710 pid = proc_selfpid();
1711 if (task->bsd_info != NULL)
1712 procname = proc_name_address(task->bsd_info);
1713#endif
1714
1715 thread_get_cpulimit(&action, &percentage, &interval_ns);
1716
1717 interval_sec = (uint32_t)(interval_ns / NSEC_PER_SEC);
1718
1719 thread_read_times(thread, &thread_user_time, &thread_system_time);
1720 time_value_add(&thread_total_time, &thread_user_time);
1721 time_value_add(&thread_total_time, &thread_system_time);
1722
1723 ledger_get_entry_info(thread->t_threadledger, thread_ledgers.cpu_time, &lei);
316670eb 1724
39236c6e
A
1725 absolutetime_to_nanoseconds(lei.lei_balance, &balance_ns);
1726 usage_percent = (uint32_t) ((balance_ns * 100ULL) / lei.lei_last_refill);
1727
1728 /* Show refill period in the same units as balance, limit, etc */
1729 nanoseconds_to_absolutetime(lei.lei_refill_period, &lei.lei_refill_period);
1730
1731 limit_percent = (uint32_t) ((lei.lei_limit * 100ULL) / lei.lei_refill_period);
1732
1733 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1734
1735 if (disable_exc_resource) {
1736 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1737 "supressed by a boot-arg\n", procname, pid, tid);
1738 return;
1739 }
1740
15129b1c
A
1741 if (audio_active) {
1742 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1743 "supressed due to audio playback\n", procname, pid, tid);
1744 return;
1745 }
39236c6e
A
1746 printf("process %s[%d] thread %llu caught burning CPU! "
1747 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1748 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1749 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
fe8ab488 1750 "period: %llu time since last refill (ns): %llu %s\n",
39236c6e
A
1751 procname, pid, tid,
1752 percentage, usage_percent, interval_sec,
1753 thread_total_time.seconds, thread_total_time.microseconds,
1754 thread_user_time.seconds, thread_user_time.microseconds,
1755 thread_system_time.seconds, thread_system_time.microseconds,
1756 lei.lei_balance,
1757 lei.lei_credit, lei.lei_debit,
1758 lei.lei_limit, limit_percent,
fe8ab488
A
1759 lei.lei_refill_period, lei.lei_last_refill,
1760 (fatal ? "[fatal violation]" : ""));
39236c6e
A
1761
1762
1763 code[0] = code[1] = 0;
1764 EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_CPU);
fe8ab488
A
1765 if (fatal) {
1766 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR_FATAL);
1767 }else {
1768 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR);
1769 }
39236c6e
A
1770 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code[0], interval_sec);
1771 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[0], limit_percent);
1772 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[1], usage_percent);
1773 exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
1774
1775 if (fatal) {
fe8ab488
A
1776#if CONFIG_JETSAM
1777 jetsam_on_ledger_cpulimit_exceeded();
1778#else
39236c6e 1779 task_terminate_internal(task);
fe8ab488
A
1780#endif
1781 }
1782}
1783
1784#define UPDATE_IO_STATS(info, size) \
1785{ \
1786 info.count++; \
1787 info.size += size; \
1788}
1789
1790#define UPDATE_IO_STATS_ATOMIC(info, size) \
1791{ \
1792 OSIncrementAtomic64((SInt64 *)&(info.count)); \
1793 OSAddAtomic64(size, (SInt64 *)&(info.size)); \
1794}
1795
1796void thread_update_io_stats(thread_t thread, int size, int io_flags)
1797{
1798 int io_tier;
1799
1800 if (thread->thread_io_stats == NULL || thread->task->task_io_stats == NULL)
1801 return;
1802
1803 if (io_flags & DKIO_READ) {
1804 UPDATE_IO_STATS(thread->thread_io_stats->disk_reads, size);
1805 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->disk_reads, size);
1806 }
1807
1808 if (io_flags & DKIO_META) {
1809 UPDATE_IO_STATS(thread->thread_io_stats->metadata, size);
1810 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->metadata, size);
39236c6e 1811 }
fe8ab488
A
1812
1813 if (io_flags & DKIO_PAGING) {
1814 UPDATE_IO_STATS(thread->thread_io_stats->paging, size);
1815 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->paging, size);
1816 }
1817
1818 io_tier = ((io_flags & DKIO_TIER_MASK) >> DKIO_TIER_SHIFT);
1819 assert (io_tier < IO_NUM_PRIORITIES);
1820
1821 UPDATE_IO_STATS(thread->thread_io_stats->io_priority[io_tier], size);
1822 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->io_priority[io_tier], size);
1823
1824 /* Update Total I/O Counts */
1825 UPDATE_IO_STATS(thread->thread_io_stats->total_io, size);
1826 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->total_io, size);
1827
316670eb
A
1828}
1829
1830void
1831init_thread_ledgers(void) {
1832 ledger_template_t t;
1833 int idx;
1834
1835 assert(thread_ledger_template == NULL);
1836
1837 if ((t = ledger_template_create("Per-thread ledger")) == NULL)
1838 panic("couldn't create thread ledger template");
1839
1840 if ((idx = ledger_entry_add(t, "cpu_time", "sched", "ns")) < 0) {
1841 panic("couldn't create cpu_time entry for thread ledger template");
1842 }
1843
39236c6e 1844 if (ledger_set_callback(t, idx, thread_cputime_callback, NULL, NULL) < 0) {
316670eb
A
1845 panic("couldn't set thread ledger callback for cpu_time entry");
1846 }
1847
1848 thread_ledgers.cpu_time = idx;
fe8ab488 1849
316670eb
A
1850 thread_ledger_template = t;
1851}
1852
39236c6e
A
1853/*
1854 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1855 */
1856int
1857thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns)
1858{
1859 int64_t abstime = 0;
1860 uint64_t limittime = 0;
1861 thread_t thread = current_thread();
1862
1863 *percentage = 0;
1864 *interval_ns = 0;
1865 *action = 0;
1866
1867 if (thread->t_threadledger == LEDGER_NULL) {
1868 /*
1869 * This thread has no per-thread ledger, so it can't possibly
1870 * have a CPU limit applied.
1871 */
1872 return (KERN_SUCCESS);
1873 }
1874
1875 ledger_get_period(thread->t_threadledger, thread_ledgers.cpu_time, interval_ns);
1876 ledger_get_limit(thread->t_threadledger, thread_ledgers.cpu_time, &abstime);
1877
1878 if ((abstime == LEDGER_LIMIT_INFINITY) || (*interval_ns == 0)) {
1879 /*
1880 * This thread's CPU time ledger has no period or limit; so it
1881 * doesn't have a CPU limit applied.
1882 */
1883 return (KERN_SUCCESS);
1884 }
1885
1886 /*
1887 * This calculation is the converse to the one in thread_set_cpulimit().
1888 */
1889 absolutetime_to_nanoseconds(abstime, &limittime);
1890 *percentage = (limittime * 100ULL) / *interval_ns;
1891 assert(*percentage <= 100);
1892
1893 if (thread->options & TH_OPT_PROC_CPULIMIT) {
1894 assert((thread->options & TH_OPT_PRVT_CPULIMIT) == 0);
1895
1896 *action = THREAD_CPULIMIT_BLOCK;
1897 } else if (thread->options & TH_OPT_PRVT_CPULIMIT) {
1898 assert((thread->options & TH_OPT_PROC_CPULIMIT) == 0);
1899
1900 *action = THREAD_CPULIMIT_EXCEPTION;
1901 } else {
1902 *action = THREAD_CPULIMIT_DISABLE;
1903 }
1904
1905 return (KERN_SUCCESS);
1906}
1907
316670eb
A
1908/*
1909 * Set CPU usage limit on a thread.
1910 *
1911 * Calling with percentage of 0 will unset the limit for this thread.
1912 */
316670eb
A
1913int
1914thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns)
1915{
1916 thread_t thread = current_thread();
1917 ledger_t l;
1918 uint64_t limittime = 0;
1919 uint64_t abstime = 0;
1920
1921 assert(percentage <= 100);
1922
39236c6e 1923 if (action == THREAD_CPULIMIT_DISABLE) {
316670eb
A
1924 /*
1925 * Remove CPU limit, if any exists.
1926 */
1927 if (thread->t_threadledger != LEDGER_NULL) {
39236c6e 1928 l = thread->t_threadledger;
fe8ab488
A
1929 ledger_set_limit(l, thread_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0);
1930 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_IGNORE);
316670eb
A
1931 thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
1932 }
1933
1934 return (0);
1935 }
1936
39236c6e
A
1937 if (interval_ns < MINIMUM_CPULIMIT_INTERVAL_MS * NSEC_PER_MSEC) {
1938 return (KERN_INVALID_ARGUMENT);
1939 }
1940
316670eb
A
1941 l = thread->t_threadledger;
1942 if (l == LEDGER_NULL) {
1943 /*
1944 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1945 */
1946 if ((l = ledger_instantiate(thread_ledger_template, LEDGER_CREATE_INACTIVE_ENTRIES)) == LEDGER_NULL)
1947 return (KERN_RESOURCE_SHORTAGE);
1948
1949 /*
1950 * We are the first to create this thread's ledger, so only activate our entry.
1951 */
1952 ledger_entry_setactive(l, thread_ledgers.cpu_time);
1953 thread->t_threadledger = l;
1954 }
1955
1956 /*
1957 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1958 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1959 */
1960 limittime = (interval_ns * percentage) / 100;
1961 nanoseconds_to_absolutetime(limittime, &abstime);
39236c6e 1962 ledger_set_limit(l, thread_ledgers.cpu_time, abstime, cpumon_ustackshots_trigger_pct);
316670eb
A
1963 /*
1964 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1965 */
1966 ledger_set_period(l, thread_ledgers.cpu_time, interval_ns);
1967
316670eb 1968 if (action == THREAD_CPULIMIT_EXCEPTION) {
39236c6e
A
1969 /*
1970 * We don't support programming the CPU usage monitor on a task if any of its
1971 * threads have a per-thread blocking CPU limit configured.
1972 */
1973 if (thread->options & TH_OPT_PRVT_CPULIMIT) {
1974 panic("CPU usage monitor activated, but blocking thread limit exists");
1975 }
1976
1977 /*
1978 * Make a note that this thread's CPU limit is being used for the task-wide CPU
1979 * usage monitor. We don't have to arm the callback which will trigger the
1980 * exception, because that was done for us in ledger_instantiate (because the
1981 * ledger template used has a default callback).
1982 */
316670eb 1983 thread->options |= TH_OPT_PROC_CPULIMIT;
39236c6e
A
1984 } else {
1985 /*
1986 * We deliberately override any CPU limit imposed by a task-wide limit (eg
1987 * CPU usage monitor).
1988 */
1989 thread->options &= ~TH_OPT_PROC_CPULIMIT;
316670eb 1990
316670eb
A
1991 thread->options |= TH_OPT_PRVT_CPULIMIT;
1992 /* The per-thread ledger template by default has a callback for CPU time */
1993 ledger_disable_callback(l, thread_ledgers.cpu_time);
1994 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_BLOCK);
1995 }
1996
316670eb
A
1997 return (0);
1998}
1999
2d21ac55
A
2000static void
2001sched_call_null(
2002__unused int type,
2003__unused thread_t thread)
2004{
2005 return;
2006}
2007
2008void
2009thread_sched_call(
2010 thread_t thread,
2011 sched_call_t call)
2012{
2013 thread->sched_call = (call != NULL)? call: sched_call_null;
2014}
2015
2016void
2017thread_static_param(
2018 thread_t thread,
2019 boolean_t state)
2020{
2021 thread_mtx_lock(thread);
2022 thread->static_param = state;
2023 thread_mtx_unlock(thread);
2024}
1c79356b 2025
b0d623f7
A
2026uint64_t
2027thread_tid(
2028 thread_t thread)
2029{
2030 return (thread != THREAD_NULL? thread->thread_id: 0);
2031}
2032
39236c6e 2033uint16_t thread_set_tag(thread_t th, uint16_t tag) {
4b17d6b6
A
2034 return thread_set_tag_internal(th, tag);
2035}
39236c6e 2036uint16_t thread_get_tag(thread_t th) {
4b17d6b6
A
2037 return thread_get_tag_internal(th);
2038}
2039
b0d623f7
A
2040uint64_t
2041thread_dispatchqaddr(
2042 thread_t thread)
2043{
2044 uint64_t dispatchqueue_addr = 0;
2045 uint64_t thread_handle = 0;
2046
2047 if (thread != THREAD_NULL) {
b0d623f7 2048 thread_handle = thread->machine.cthread_self;
b0d623f7
A
2049
2050 if (thread->task->bsd_info)
2051 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
2052 }
2053
2054 return (dispatchqueue_addr);
2055}
2056
91447636
A
2057/*
2058 * Export routines to other components for things that are done as macros
2059 * within the osfmk component.
2060 */
1c79356b 2061
91447636
A
2062#undef thread_reference
2063void thread_reference(thread_t thread);
1c79356b 2064void
91447636
A
2065thread_reference(
2066 thread_t thread)
1c79356b 2067{
91447636
A
2068 if (thread != THREAD_NULL)
2069 thread_reference_internal(thread);
1c79356b
A
2070}
2071
1c79356b 2072#undef thread_should_halt
91447636 2073
1c79356b
A
2074boolean_t
2075thread_should_halt(
55e303ae 2076 thread_t th)
1c79356b 2077{
91447636 2078 return (thread_should_halt_fast(th));
55e303ae 2079}
2d21ac55 2080
fe8ab488
A
2081/*
2082 * thread_set_voucher_name - reset the voucher port name bound to this thread
2083 *
2084 * Conditions: nothing locked
2085 *
2086 * If we already converted the previous name to a cached voucher
2087 * reference, then we discard that reference here. The next lookup
2088 * will cache it again.
2089 */
2090
2091kern_return_t
2092thread_set_voucher_name(mach_port_name_t voucher_name)
2093{
2094 thread_t thread = current_thread();
2095 ipc_voucher_t new_voucher = IPC_VOUCHER_NULL;
2096 ipc_voucher_t voucher;
2097#ifdef CONFIG_BANK
2098 ledger_t bankledger = NULL;
2099#endif
2100
2101 if (MACH_PORT_DEAD == voucher_name)
2102 return KERN_INVALID_RIGHT;
2103
2104 /*
2105 * agressively convert to voucher reference
2106 */
2107 if (MACH_PORT_VALID(voucher_name)) {
2108 new_voucher = convert_port_name_to_voucher(voucher_name);
2109 if (IPC_VOUCHER_NULL == new_voucher)
2110 return KERN_INVALID_ARGUMENT;
2111 }
2112#ifdef CONFIG_BANK
2113 bankledger = bank_get_voucher_ledger(new_voucher);
2114#endif
2115
2116 thread_mtx_lock(thread);
2117 voucher = thread->ith_voucher;
2118 thread->ith_voucher_name = voucher_name;
2119 thread->ith_voucher = new_voucher;
2120#ifdef CONFIG_BANK
2121 bank_swap_thread_bank_ledger(thread, bankledger);
2122#endif
2123 thread_mtx_unlock(thread);
2124
2125 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2126 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2127 (uintptr_t)thread_tid(thread),
2128 (uintptr_t)voucher_name,
2129 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher),
2130 1, 0);
2131
2132 if (IPC_VOUCHER_NULL != voucher)
2133 ipc_voucher_release(voucher);
2134
2135 return KERN_SUCCESS;
2136}
2137
2138/*
2139 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2140 *
2141 * Conditions: nothing locked
2142 *
2143 * A reference to the voucher may be lazily pending, if someone set the voucher name
2144 * but nobody has done a lookup yet. In that case, we'll have to do the equivalent
2145 * lookup here.
2146 *
2147 * NOTE: At the moment, there is no distinction between the current and effective
2148 * vouchers because we only set them at the thread level currently.
2149 */
2150kern_return_t
2151thread_get_mach_voucher(
2152 thread_act_t thread,
2153 mach_voucher_selector_t __unused which,
2154 ipc_voucher_t *voucherp)
2155{
2156 ipc_voucher_t voucher;
2157 mach_port_name_t voucher_name;
2158
2159 if (THREAD_NULL == thread)
2160 return KERN_INVALID_ARGUMENT;
2161
2162 thread_mtx_lock(thread);
2163 voucher = thread->ith_voucher;
2164
2165 /* if already cached, just return a ref */
2166 if (IPC_VOUCHER_NULL != voucher) {
2167 ipc_voucher_reference(voucher);
2168 thread_mtx_unlock(thread);
2169 *voucherp = voucher;
2170 return KERN_SUCCESS;
2171 }
2172
2173 voucher_name = thread->ith_voucher_name;
2174
2175 /* convert the name to a port, then voucher reference */
2176 if (MACH_PORT_VALID(voucher_name)) {
2177 ipc_port_t port;
2178
2179 if (KERN_SUCCESS !=
2180 ipc_object_copyin(thread->task->itk_space, voucher_name,
2181 MACH_MSG_TYPE_COPY_SEND, (ipc_object_t *)&port)) {
2182 thread->ith_voucher_name = MACH_PORT_NULL;
2183 thread_mtx_unlock(thread);
2184 *voucherp = IPC_VOUCHER_NULL;
2185 return KERN_SUCCESS;
2186 }
2187
2188 /* convert to a voucher ref to return, and cache a ref on thread */
2189 voucher = convert_port_to_voucher(port);
2190 ipc_voucher_reference(voucher);
2191 thread->ith_voucher = voucher;
2192 thread_mtx_unlock(thread);
2193
2194 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2195 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2196 (uintptr_t)thread_tid(thread),
2197 (uintptr_t)port,
2198 VM_KERNEL_ADDRPERM((uintptr_t)voucher),
2199 2, 0);
2200
2201
2202 ipc_port_release_send(port);
2203 } else
2204 thread_mtx_unlock(thread);
2205
2206 *voucherp = voucher;
2207 return KERN_SUCCESS;
2208}
2209
2210/*
2211 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2212 *
2213 * Conditions: callers holds a reference on the voucher.
2214 * nothing locked.
2215 *
2216 * We grab another reference to the voucher and bind it to the thread. Any lazy
2217 * binding is erased. The old voucher reference associated with the thread is
2218 * discarded.
2219 */
2220kern_return_t
2221thread_set_mach_voucher(
2222 thread_t thread,
2223 ipc_voucher_t voucher)
2224{
2225 ipc_voucher_t old_voucher;
2226#ifdef CONFIG_BANK
2227 ledger_t bankledger = NULL;
2228#endif
2229
2230 if (THREAD_NULL == thread)
2231 return KERN_INVALID_ARGUMENT;
2232
2233 if (thread != current_thread() || thread->started)
2234 return KERN_INVALID_ARGUMENT;
2235
2236
2237 ipc_voucher_reference(voucher);
2238#ifdef CONFIG_BANK
2239 bankledger = bank_get_voucher_ledger(voucher);
2240#endif
2241 thread_mtx_lock(thread);
2242 old_voucher = thread->ith_voucher;
2243 thread->ith_voucher = voucher;
2244 thread->ith_voucher_name = MACH_PORT_NULL;
2245#ifdef CONFIG_BANK
2246 bank_swap_thread_bank_ledger(thread, bankledger);
2247#endif
2248 thread_mtx_unlock(thread);
2249
2250 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2251 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2252 (uintptr_t)thread_tid(thread),
2253 (uintptr_t)MACH_PORT_NULL,
2254 VM_KERNEL_ADDRPERM((uintptr_t)voucher),
2255 3, 0);
2256
2257 ipc_voucher_release(old_voucher);
2258
2259 return KERN_SUCCESS;
2260}
2261
2262/*
2263 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2264 *
2265 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2266 * nothing locked.
2267 *
2268 * If the old voucher is still the same as passed in, replace it with new voucher
2269 * and discard the old (and the reference passed in). Otherwise, discard the new
2270 * and return an updated old voucher.
2271 */
2272kern_return_t
2273thread_swap_mach_voucher(
2274 thread_t thread,
2275 ipc_voucher_t new_voucher,
2276 ipc_voucher_t *in_out_old_voucher)
2277{
2278 mach_port_name_t old_voucher_name;
2279 ipc_voucher_t old_voucher;
2280#ifdef CONFIG_BANK
2281 ledger_t bankledger = NULL;
2282#endif
2283
2284 if (THREAD_NULL == thread)
2285 return KERN_INVALID_TASK;
2286
2287 if (thread != current_thread() || thread->started)
2288 return KERN_INVALID_ARGUMENT;
2289
2290#ifdef CONFIG_BANK
2291 bankledger = bank_get_voucher_ledger(new_voucher);
2292#endif
2293
2294 thread_mtx_lock(thread);
2295
2296 old_voucher = thread->ith_voucher;
2297
2298 if (IPC_VOUCHER_NULL == old_voucher) {
2299 old_voucher_name = thread->ith_voucher_name;
2300
2301 /* perform lazy binding if needed */
2302 if (MACH_PORT_VALID(old_voucher_name)) {
2303 old_voucher = convert_port_name_to_voucher(old_voucher_name);
2304 thread->ith_voucher_name = MACH_PORT_NULL;
2305 thread->ith_voucher = old_voucher;
2306
2307 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2308 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2309 (uintptr_t)thread_tid(thread),
2310 (uintptr_t)old_voucher_name,
2311 VM_KERNEL_ADDRPERM((uintptr_t)old_voucher),
2312 4, 0);
2313
2314 }
2315 }
2316
2317 /* swap in new voucher, if old voucher matches the one supplied */
2318 if (old_voucher == *in_out_old_voucher) {
2319 ipc_voucher_reference(new_voucher);
2320 thread->ith_voucher = new_voucher;
2321 thread->ith_voucher_name = MACH_PORT_NULL;
2322#ifdef CONFIG_BANK
2323 bank_swap_thread_bank_ledger(thread, bankledger);
2324#endif
2325 thread_mtx_unlock(thread);
2326
2327 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2328 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2329 (uintptr_t)thread_tid(thread),
2330 (uintptr_t)MACH_PORT_NULL,
2331 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher),
2332 5, 0);
2333
2334 ipc_voucher_release(old_voucher);
2335
2336 *in_out_old_voucher = IPC_VOUCHER_NULL;
2337 return KERN_SUCCESS;
2338 }
2339
2340 /* Otherwise, just return old voucher reference */
2341 ipc_voucher_reference(old_voucher);
2342 thread_mtx_unlock(thread);
2343 *in_out_old_voucher = old_voucher;
2344 return KERN_SUCCESS;
2345}
2346
2347/*
2348 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
2349 */
2350kern_return_t
2351thread_get_current_voucher_origin_pid(
2352 int32_t *pid)
2353{
2354 uint32_t buf_size;
2355 kern_return_t kr;
2356 thread_t thread = current_thread();
2357
2358 buf_size = sizeof(*pid);
2359 kr = mach_voucher_attr_command(thread->ith_voucher,
2360 MACH_VOUCHER_ATTR_KEY_BANK,
2361 BANK_ORIGINATOR_PID,
2362 NULL,
2363 0,
2364 (mach_voucher_attr_content_t)pid,
2365 &buf_size);
2366
2367 return kr;
2368}
2369
2d21ac55
A
2370#if CONFIG_DTRACE
2371uint32_t dtrace_get_thread_predcache(thread_t thread)
2372{
2373 if (thread != THREAD_NULL)
2374 return thread->t_dtrace_predcache;
2375 else
2376 return 0;
2377}
2378
2379int64_t dtrace_get_thread_vtime(thread_t thread)
2380{
2381 if (thread != THREAD_NULL)
2382 return thread->t_dtrace_vtime;
2383 else
2384 return 0;
2385}
2386
2387int64_t dtrace_get_thread_tracing(thread_t thread)
2388{
2389 if (thread != THREAD_NULL)
2390 return thread->t_dtrace_tracing;
2391 else
2392 return 0;
2393}
2394
2395boolean_t dtrace_get_thread_reentering(thread_t thread)
2396{
2397 if (thread != THREAD_NULL)
2398 return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
2399 else
2400 return 0;
2401}
2402
2403vm_offset_t dtrace_get_kernel_stack(thread_t thread)
2404{
2405 if (thread != THREAD_NULL)
2406 return thread->kernel_stack;
2407 else
2408 return 0;
2409}
2410
2411int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
2412{
2d21ac55
A
2413 if (thread != THREAD_NULL) {
2414 processor_t processor = current_processor();
2415 uint64_t abstime = mach_absolute_time();
2416 timer_t timer;
2417
2418 timer = PROCESSOR_DATA(processor, thread_timer);
2419
2420 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
2421 (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
2422 } else
2423 return 0;
2d21ac55
A
2424}
2425
2426void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
2427{
2428 if (thread != THREAD_NULL)
2429 thread->t_dtrace_predcache = predcache;
2430}
2431
2432void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
2433{
2434 if (thread != THREAD_NULL)
2435 thread->t_dtrace_vtime = vtime;
2436}
2437
2438void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
2439{
2440 if (thread != THREAD_NULL)
2441 thread->t_dtrace_tracing = accum;
2442}
2443
2444void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
2445{
2446 if (thread != THREAD_NULL) {
2447 if (vbool)
2448 thread->options |= TH_OPT_DTRACE;
2449 else
2450 thread->options &= (~TH_OPT_DTRACE);
2451 }
2452}
2453
2454vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
2455{
2456 vm_offset_t prev = 0;
2457
2458 if (thread != THREAD_NULL) {
2459 prev = thread->recover;
2460 thread->recover = recover;
2461 }
2462 return prev;
2463}
2464
b0d623f7
A
2465void dtrace_thread_bootstrap(void)
2466{
2467 task_t task = current_task();
39236c6e
A
2468
2469 if (task->thread_count == 1) {
2470 thread_t thread = current_thread();
2471 if (thread->t_dtrace_flags & TH_DTRACE_EXECSUCCESS) {
2472 thread->t_dtrace_flags &= ~TH_DTRACE_EXECSUCCESS;
2473 DTRACE_PROC(exec__success);
2474 }
b0d623f7
A
2475 DTRACE_PROC(start);
2476 }
2477 DTRACE_PROC(lwp__start);
2478
2479}
39236c6e
A
2480
2481void
2482dtrace_thread_didexec(thread_t thread)
2483{
2484 thread->t_dtrace_flags |= TH_DTRACE_EXECSUCCESS;
2485}
2d21ac55 2486#endif /* CONFIG_DTRACE */