]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/thread.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 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.
0a7de745 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.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_FREE_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
0a7de745 41 *
1c79356b
A
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
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>
5ba3f43e
A
115#include <kern/thread_group.h>
116#include <kern/coalition.h>
1c79356b
A
117#include <kern/host.h>
118#include <kern/zalloc.h>
1c79356b 119#include <kern/assert.h>
39236c6e 120#include <kern/exc_resource.h>
5ba3f43e 121#include <kern/exc_guard.h>
39236c6e 122#include <kern/telemetry.h>
39037602 123#include <kern/policy_internal.h>
d9a64523 124#include <kern/turnstile.h>
cb323159 125#include <kern/sched_clutch.h>
39037602 126
3e170ce0 127#include <corpses/task_corpse.h>
39236c6e
A
128#if KPC
129#include <kern/kpc.h>
130#endif
91447636 131
5ba3f43e
A
132#if MONOTONIC
133#include <kern/monotonic.h>
134#include <machine/monotonic.h>
135#endif /* MONOTONIC */
136
91447636
A
137#include <ipc/ipc_kmsg.h>
138#include <ipc/ipc_port.h>
fe8ab488 139#include <bank/bank_types.h>
91447636
A
140
141#include <vm/vm_kern.h>
142#include <vm/vm_pageout.h>
143
1c79356b 144#include <sys/kdebug.h>
3e170ce0 145#include <sys/bsdtask_info.h>
2d21ac55 146#include <mach/sdt.h>
5ba3f43e 147#include <san/kasan.h>
cb323159
A
148#if CONFIG_KSANCOV
149#include <san/ksancov.h>
150#endif
5ba3f43e
A
151
152#include <stdatomic.h>
2d21ac55 153
cb323159
A
154#if defined(HAS_APPLE_PAC)
155#include <ptrauth.h>
156#include <arm64/proc_reg.h>
157#endif /* defined(HAS_APPLE_PAC) */
0a7de745 158
1c79356b
A
159/*
160 * Exported interfaces
161 */
91447636 162#include <mach/task_server.h>
1c79356b
A
163#include <mach/thread_act_server.h>
164#include <mach/mach_host_server.h>
91447636 165#include <mach/host_priv_server.h>
fe8ab488 166#include <mach/mach_voucher_server.h>
39037602 167#include <kern/policy_internal.h>
1c79356b 168
0a7de745
A
169static struct zone *thread_zone;
170static lck_grp_attr_t thread_lck_grp_attr;
171lck_attr_t thread_lck_attr;
172lck_grp_t thread_lck_grp;
1c79356b 173
0a7de745 174struct zone *thread_qos_override_zone;
a1c7dba1 175
cb323159
A
176static struct mpsc_daemon_queue thread_stack_queue;
177static struct mpsc_daemon_queue thread_terminate_queue;
178static struct mpsc_daemon_queue thread_deallocate_queue;
179static struct mpsc_daemon_queue thread_exception_queue;
d9a64523 180
cb323159 181decl_simple_lock_data(static, crashed_threads_lock);
0a7de745 182static queue_head_t crashed_threads_queue;
3e170ce0 183
39037602 184struct thread_exception_elt {
cb323159 185 struct mpsc_queue_chain link;
0a7de745
A
186 exception_type_t exception_type;
187 task_t exception_task;
188 thread_t exception_thread;
39037602
A
189};
190
0a7de745 191static struct thread thread_template, init_thread;
d9a64523
A
192static void thread_deallocate_enqueue(thread_t thread);
193static void thread_deallocate_complete(thread_t thread);
b0d623f7 194
91447636
A
195#ifdef MACH_BSD
196extern void proc_exit(void *);
39037602 197extern mach_exception_data_type_t proc_encode_exit_exception_code(void *);
b0d623f7 198extern uint64_t get_dispatchqueue_offset_from_proc(void *);
5ba3f43e 199extern uint64_t get_return_to_kernel_offset_from_proc(void *p);
39236c6e 200extern int proc_selfpid(void);
d9a64523 201extern void proc_name(int, char*, int);
39236c6e 202extern char * proc_name_address(void *p);
91447636 203#endif /* MACH_BSD */
b0d623f7 204
39236c6e 205extern int disable_exc_resource;
15129b1c 206extern int audio_active;
2d21ac55 207extern int debug_task;
0a7de745 208int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */
b0d623f7
A
209int task_threadmax = CONFIG_THREAD_MAX;
210
0a7de745 211static uint64_t thread_unique_id = 100;
55e303ae 212
cb323159 213struct _thread_ledger_indices thread_ledgers = { .cpu_time = -1 };
316670eb 214static ledger_template_t thread_ledger_template = NULL;
39037602 215static void init_thread_ledgers(void);
39236c6e 216
fe8ab488
A
217#if CONFIG_JETSAM
218void jetsam_on_ledger_cpulimit_exceeded(void);
219#endif
220
d9a64523
A
221extern int task_thread_soft_limit;
222extern int exc_via_corpse_forking;
223
224#if DEVELOPMENT || DEBUG
225extern int exc_resource_threads_enabled;
226#endif /* DEVELOPMENT || DEBUG */
227
39236c6e
A
228/*
229 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
230 *
231 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
232 * stacktraces, aka micro-stackshots)
233 */
0a7de745 234#define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
39236c6e
A
235
236int cpumon_ustackshots_trigger_pct; /* Percentage. Level at which we start gathering telemetry. */
39037602 237void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU(void);
d9a64523
A
238#if DEVELOPMENT || DEBUG
239void __attribute__((noinline)) SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(task_t, int);
240#endif /* DEVELOPMENT || DEBUG */
39236c6e
A
241
242/*
243 * The smallest interval over which we support limiting CPU consumption is 1ms
244 */
245#define MINIMUM_CPULIMIT_INTERVAL_MS 1
316670eb 246
e8c3f781
A
247os_refgrp_decl(static, thread_refgrp, "thread", NULL);
248
91447636
A
249void
250thread_bootstrap(void)
251{
252 /*
253 * Fill in a template thread for fast initialization.
254 */
1c79356b 255
39037602
A
256#if MACH_ASSERT
257 thread_template.thread_magic = THREAD_MAGIC;
258#endif /* MACH_ASSERT */
259
2d21ac55 260 thread_template.runq = PROCESSOR_NULL;
1c79356b 261
91447636
A
262 thread_template.reason = AST_NONE;
263 thread_template.at_safe_point = FALSE;
264 thread_template.wait_event = NO_EVENT64;
3e170ce0 265 thread_template.waitq = NULL;
91447636
A
266 thread_template.wait_result = THREAD_WAITING;
267 thread_template.options = THREAD_ABORTSAFE;
268 thread_template.state = TH_WAIT | TH_UNINT;
269 thread_template.wake_active = FALSE;
270 thread_template.continuation = THREAD_CONTINUE_NULL;
271 thread_template.parameter = NULL;
1c79356b 272
91447636 273 thread_template.importance = 0;
6d2010ae
A
274 thread_template.sched_mode = TH_MODE_NONE;
275 thread_template.sched_flags = 0;
276 thread_template.saved_mode = TH_MODE_NONE;
91447636 277 thread_template.safe_release = 0;
39037602 278 thread_template.th_sched_bucket = TH_BUCKET_RUN;
0b4e3aa0 279
fe8ab488
A
280 thread_template.sfi_class = SFI_CLASS_UNSPECIFIED;
281 thread_template.sfi_wait_class = SFI_CLASS_UNSPECIFIED;
282
283 thread_template.active = 0;
284 thread_template.started = 0;
285 thread_template.static_param = 0;
286 thread_template.policy_reset = 0;
287
490019cf 288 thread_template.base_pri = BASEPRI_DEFAULT;
91447636
A
289 thread_template.sched_pri = 0;
290 thread_template.max_priority = 0;
291 thread_template.task_priority = 0;
39236c6e 292 thread_template.rwlock_count = 0;
d9a64523 293 thread_template.waiting_for_mutex = NULL;
1c79356b 294
fe8ab488 295
91447636 296 thread_template.realtime.deadline = UINT64_MAX;
1c79356b 297
fe8ab488 298 thread_template.quantum_remaining = 0;
6d2010ae 299 thread_template.last_run_time = 0;
5ba3f43e
A
300 thread_template.last_made_runnable_time = THREAD_NOT_RUNNABLE;
301 thread_template.last_basepri_change_time = THREAD_NOT_RUNNABLE;
302 thread_template.same_pri_latency = 0;
1c79356b 303
91447636
A
304 thread_template.computation_metered = 0;
305 thread_template.computation_epoch = 0;
1c79356b 306
fe8ab488 307#if defined(CONFIG_SCHED_TIMESHARE_CORE)
91447636 308 thread_template.sched_stamp = 0;
91447636 309 thread_template.pri_shift = INT8_MAX;
6d2010ae 310 thread_template.sched_usage = 0;
91447636 311 thread_template.cpu_usage = thread_template.cpu_delta = 0;
6d2010ae 312#endif
2d21ac55 313 thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
0b4e3aa0 314
5ba3f43e
A
315#if MONOTONIC
316 memset(&thread_template.t_monotonic, 0,
0a7de745 317 sizeof(thread_template.t_monotonic));
5ba3f43e
A
318#endif /* MONOTONIC */
319
91447636
A
320 thread_template.bound_processor = PROCESSOR_NULL;
321 thread_template.last_processor = PROCESSOR_NULL;
1c79356b 322
d9a64523 323 thread_template.sched_call = NULL;
2d21ac55 324
91447636
A
325 timer_init(&thread_template.user_timer);
326 timer_init(&thread_template.system_timer);
5ba3f43e 327 timer_init(&thread_template.ptime);
d9a64523 328 timer_init(&thread_template.runnable_timer);
91447636
A
329 thread_template.user_timer_save = 0;
330 thread_template.system_timer_save = 0;
2d21ac55
A
331 thread_template.vtimer_user_save = 0;
332 thread_template.vtimer_prof_save = 0;
333 thread_template.vtimer_rlim_save = 0;
39037602 334 thread_template.vtimer_qos_save = 0;
1c79356b 335
3e170ce0 336#if CONFIG_SCHED_SFI
fe8ab488 337 thread_template.wait_sfi_begin_time = 0;
3e170ce0 338#endif
fe8ab488 339
91447636
A
340 thread_template.wait_timer_is_set = FALSE;
341 thread_template.wait_timer_active = 0;
1c79356b 342
91447636 343 thread_template.depress_timer_active = 0;
0b4e3aa0 344
91447636 345 thread_template.recover = (vm_offset_t)NULL;
0a7de745 346
2d21ac55 347 thread_template.map = VM_MAP_NULL;
d9a64523
A
348#if DEVELOPMENT || DEBUG
349 thread_template.pmap_footprint_suspended = FALSE;
350#endif /* DEVELOPMENT || DEBUG */
2d21ac55
A
351
352#if CONFIG_DTRACE
353 thread_template.t_dtrace_predcache = 0;
354 thread_template.t_dtrace_vtime = 0;
355 thread_template.t_dtrace_tracing = 0;
356#endif /* CONFIG_DTRACE */
b0d623f7 357
39037602
A
358#if KPERF
359 thread_template.kperf_flags = 0;
360 thread_template.kperf_pet_gen = 0;
361 thread_template.kperf_c_switch = 0;
362 thread_template.kperf_pet_cnt = 0;
363#endif
364
39236c6e
A
365#if KPC
366 thread_template.kpc_buf = NULL;
367#endif
368
fe8ab488
A
369#if HYPERVISOR
370 thread_template.hv_thread_target = NULL;
371#endif /* HYPERVISOR */
372
04b8595b
A
373#if (DEVELOPMENT || DEBUG)
374 thread_template.t_page_creation_throttled_hard = 0;
375 thread_template.t_page_creation_throttled_soft = 0;
376#endif /* DEVELOPMENT || DEBUG */
377 thread_template.t_page_creation_throttled = 0;
b0d623f7
A
378 thread_template.t_page_creation_count = 0;
379 thread_template.t_page_creation_time = 0;
55e303ae 380
2d21ac55 381 thread_template.affinity_set = NULL;
0a7de745 382
6d2010ae
A
383 thread_template.syscalls_unix = 0;
384 thread_template.syscalls_mach = 0;
385
316670eb
A
386 thread_template.t_ledger = LEDGER_NULL;
387 thread_template.t_threadledger = LEDGER_NULL;
fe8ab488
A
388 thread_template.t_bankledger = LEDGER_NULL;
389 thread_template.t_deduct_bank_ledger_time = 0;
316670eb 390
39037602
A
391 thread_template.requested_policy = (struct thread_requested_policy) {};
392 thread_template.effective_policy = (struct thread_effective_policy) {};
39236c6e 393
a1c7dba1 394 bzero(&thread_template.overrides, sizeof(thread_template.overrides));
cb323159 395 thread_template.kevent_overrides = 0;
39236c6e 396
fe8ab488
A
397 thread_template.iotier_override = THROTTLE_LEVEL_NONE;
398 thread_template.thread_io_stats = NULL;
5ba3f43e
A
399#if CONFIG_EMBEDDED
400 thread_template.taskwatch = NULL;
401#endif /* CONFIG_EMBEDDED */
39236c6e
A
402 thread_template.thread_callout_interrupt_wakeups = thread_template.thread_callout_platform_idle_wakeups = 0;
403
404 thread_template.thread_timer_wakeups_bin_1 = thread_template.thread_timer_wakeups_bin_2 = 0;
405 thread_template.callout_woken_from_icontext = thread_template.callout_woken_from_platform_idle = 0;
cb323159 406 thread_template.guard_exc_fatal = 0;
39236c6e
A
407
408 thread_template.thread_tag = 0;
6d2010ae 409
fe8ab488
A
410 thread_template.ith_voucher_name = MACH_PORT_NULL;
411 thread_template.ith_voucher = IPC_VOUCHER_NULL;
412
5ba3f43e 413 thread_template.th_work_interval = NULL;
3e170ce0 414
cb323159 415 thread_template.decompressions = 0;
91447636 416 init_thread = thread_template;
e8c3f781 417
0a7de745
A
418 /* fiddle with init thread to skip asserts in set_sched_pri */
419 init_thread.sched_pri = MAXPRI_KERNEL;
420
91447636 421 machine_set_current_thread(&init_thread);
1c79356b
A
422}
423
fe8ab488
A
424extern boolean_t allow_qos_policy_set;
425
55e303ae 426void
91447636 427thread_init(void)
0b4e3aa0 428{
91447636 429 thread_zone = zinit(
0a7de745
A
430 sizeof(struct thread),
431 thread_max * sizeof(struct thread),
432 THREAD_CHUNK * sizeof(struct thread),
433 "threads");
6d2010ae 434
a1c7dba1
A
435 thread_qos_override_zone = zinit(
436 sizeof(struct thread_qos_override),
437 4 * thread_max * sizeof(struct thread_qos_override),
438 PAGE_SIZE,
439 "thread qos override");
440 zone_change(thread_qos_override_zone, Z_EXPAND, TRUE);
441 zone_change(thread_qos_override_zone, Z_COLLECT, TRUE);
442 zone_change(thread_qos_override_zone, Z_CALLERACCT, FALSE);
443 zone_change(thread_qos_override_zone, Z_NOENCRYPT, TRUE);
444
b0d623f7
A
445 lck_grp_attr_setdefault(&thread_lck_grp_attr);
446 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
447 lck_attr_setdefault(&thread_lck_attr);
39037602 448
91447636 449 stack_init();
55e303ae 450
39037602
A
451 thread_policy_init();
452
91447636
A
453 /*
454 * Initialize any machine-dependent
455 * per-thread structures necessary.
456 */
457 machine_thread_init();
316670eb 458
39236c6e 459 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct,
0a7de745 460 sizeof(cpumon_ustackshots_trigger_pct))) {
39236c6e
A
461 cpumon_ustackshots_trigger_pct = CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT;
462 }
463
0a7de745 464 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set, sizeof(allow_qos_policy_set));
fe8ab488 465
316670eb 466 init_thread_ledgers();
91447636 467}
0b4e3aa0 468
743345f9
A
469boolean_t
470thread_is_active(thread_t thread)
471{
0a7de745 472 return thread->active;
743345f9
A
473}
474
39037602
A
475void
476thread_corpse_continue(void)
477{
478 thread_t thread = current_thread();
479
480 thread_terminate_internal(thread);
5ba3f43e
A
481
482 /*
483 * Handle the thread termination directly
484 * here instead of returning to userspace.
485 */
486 assert(thread->active == FALSE);
487 thread_ast_clear(thread, AST_APC);
488 thread_apc_ast(thread);
39037602
A
489
490 panic("thread_corpse_continue");
491 /*NOTREACHED*/
492}
493
cb323159 494__dead2
91447636
A
495static void
496thread_terminate_continue(void)
497{
498 panic("thread_terminate_continue");
499 /*NOTREACHED*/
0b4e3aa0
A
500}
501
1c79356b 502/*
91447636 503 * thread_terminate_self:
1c79356b 504 */
1c79356b 505void
91447636 506thread_terminate_self(void)
1c79356b 507{
0a7de745
A
508 thread_t thread = current_thread();
509 task_t task;
b0d623f7
A
510 int threadcnt;
511
6d2010ae
A
512 pal_thread_terminate_self(thread);
513
b0d623f7 514 DTRACE_PROC(lwp__exit);
2d21ac55
A
515
516 thread_mtx_lock(thread);
517
2d21ac55 518 ipc_thread_disable(thread);
39037602 519
2d21ac55 520 thread_mtx_unlock(thread);
55e303ae 521
d9a64523 522 thread_sched_call(thread, NULL);
55e303ae 523
d9a64523
A
524 spl_t s = splsched();
525 thread_lock(thread);
55e303ae 526
d9a64523 527 thread_depress_abort_locked(thread);
b0d623f7 528
91447636
A
529 thread_unlock(thread);
530 splx(s);
55e303ae 531
5ba3f43e
A
532#if CONFIG_EMBEDDED
533 thead_remove_taskwatch(thread);
534#endif /* CONFIG_EMBEDDED */
535
536 work_interval_thread_terminate(thread);
fe8ab488
A
537
538 thread_mtx_lock(thread);
539
2d21ac55 540 thread_policy_reset(thread);
55e303ae 541
fe8ab488 542 thread_mtx_unlock(thread);
316670eb 543
5ba3f43e
A
544 bank_swap_thread_bank_ledger(thread, NULL);
545
a39ff7e2
A
546 if (kdebug_enable && bsd_hasthreadname(thread->uthread)) {
547 char threadname[MAXTHREADNAMESIZE];
548 bsd_getthreadname(thread->uthread, threadname);
549 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, threadname);
550 }
551
b0d623f7 552 task = thread->task;
39037602 553 uthread_cleanup(task, thread->uthread, task->bsd_info);
b0d623f7 554
a39ff7e2 555 if (kdebug_enable && task->bsd_info && !task_is_exec_copy(task)) {
39037602 556 /* trace out pid before we sign off */
5ba3f43e 557 long dbg_arg1 = 0;
a39ff7e2 558 long dbg_arg2 = 0;
39037602 559
a39ff7e2
A
560 kdbg_trace_data(thread->task->bsd_info, &dbg_arg1, &dbg_arg2);
561 KDBG_RELEASE(TRACE_DATA_THREAD_TERMINATE_PID, dbg_arg1, dbg_arg2);
39037602
A
562 }
563
5ba3f43e
A
564 /*
565 * After this subtraction, this thread should never access
cb323159 566 * task->bsd_info unless it got 0 back from the os_atomic_dec. It
5ba3f43e
A
567 * could be racing with other threads to be the last thread in the
568 * process, and the last thread in the process will tear down the proc
569 * structure and zero-out task->bsd_info.
570 */
cb323159 571 threadcnt = os_atomic_dec(&task->active_thread_count, relaxed);
5ba3f43e 572
91447636
A
573 /*
574 * If we are the last thread to terminate and the task is
575 * associated with a BSD process, perform BSD process exit.
576 */
743345f9 577 if (threadcnt == 0 && task->bsd_info != NULL && !task_is_exec_copy(task)) {
39037602 578 mach_exception_data_type_t subcode = 0;
a39ff7e2 579 if (kdebug_enable) {
39037602 580 /* since we're the last thread in this process, trace out the command name too */
a39ff7e2
A
581 long args[4] = {};
582 kdbg_trace_string(thread->task->bsd_info, &args[0], &args[1], &args[2], &args[3]);
583 KDBG_RELEASE(TRACE_STRING_PROC_EXIT, args[0], args[1], args[2], args[3]);
39037602
A
584 }
585
586 /* Get the exit reason before proc_exit */
587 subcode = proc_encode_exit_exception_code(task->bsd_info);
91447636 588 proc_exit(task->bsd_info);
3e170ce0
A
589 /*
590 * if there is crash info in task
591 * then do the deliver action since this is
592 * last thread for this task.
593 */
594 if (task->corpse_info) {
5ba3f43e 595 task_deliver_crash_notification(task, current_thread(), EXC_RESOURCE, subcode);
39037602
A
596 }
597 }
598
599 if (threadcnt == 0) {
600 task_lock(task);
601 if (task_is_a_corpse_fork(task)) {
602 thread_wakeup((event_t)&task->active_thread_count);
3e170ce0 603 }
39037602 604 task_unlock(task);
3e170ce0 605 }
39037602 606
2d21ac55
A
607 uthread_cred_free(thread->uthread);
608
91447636
A
609 s = splsched();
610 thread_lock(thread);
1c79356b 611
d9a64523
A
612 /*
613 * Ensure that the depress timer is no longer enqueued,
614 * so the timer (stored in the thread) can be safely deallocated
615 *
616 * TODO: build timer_call_cancel_wait
617 */
618
619 assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) == 0);
620
621 uint32_t delay_us = 1;
622
623 while (thread->depress_timer_active > 0) {
624 thread_unlock(thread);
625 splx(s);
626
627 delay(delay_us++);
628
0a7de745 629 if (delay_us > USEC_PER_SEC) {
d9a64523 630 panic("depress timer failed to inactivate!"
0a7de745
A
631 "thread: %p depress_timer_active: %d",
632 thread, thread->depress_timer_active);
633 }
d9a64523
A
634
635 s = splsched();
636 thread_lock(thread);
637 }
638
91447636
A
639 /*
640 * Cancel wait timer, and wait for
641 * concurrent expirations.
642 */
643 if (thread->wait_timer_is_set) {
644 thread->wait_timer_is_set = FALSE;
1c79356b 645
0a7de745 646 if (timer_call_cancel(&thread->wait_timer)) {
91447636 647 thread->wait_timer_active--;
0a7de745 648 }
91447636 649 }
1c79356b 650
d9a64523
A
651 delay_us = 1;
652
91447636
A
653 while (thread->wait_timer_active > 0) {
654 thread_unlock(thread);
655 splx(s);
0b4e3aa0 656
d9a64523
A
657 delay(delay_us++);
658
0a7de745 659 if (delay_us > USEC_PER_SEC) {
d9a64523 660 panic("wait timer failed to inactivate!"
0a7de745
A
661 "thread: %p wait_timer_active: %d",
662 thread, thread->wait_timer_active);
663 }
1c79356b 664
91447636
A
665 s = splsched();
666 thread_lock(thread);
667 }
1c79356b 668
91447636
A
669 /*
670 * If there is a reserved stack, release it.
671 */
672 if (thread->reserved_stack != 0) {
6d2010ae 673 stack_free_reserved(thread);
91447636
A
674 thread->reserved_stack = 0;
675 }
1c79356b 676
91447636
A
677 /*
678 * Mark thread as terminating, and block.
679 */
680 thread->state |= TH_TERMINATE;
681 thread_mark_wait_locked(thread, THREAD_UNINT);
d9a64523
A
682
683 assert((thread->sched_flags & TH_SFLAG_WAITQ_PROMOTED) == 0);
684 assert((thread->sched_flags & TH_SFLAG_RW_PROMOTED) == 0);
685 assert((thread->sched_flags & TH_SFLAG_EXEC_PROMOTED) == 0);
3e170ce0 686 assert((thread->sched_flags & TH_SFLAG_PROMOTED) == 0);
cb323159 687 assert(thread->kern_promotion_schedpri == 0);
d9a64523 688 assert(thread->waiting_for_mutex == NULL);
39236c6e 689 assert(thread->rwlock_count == 0);
d9a64523 690
91447636
A
691 thread_unlock(thread);
692 /* splsched */
1c79356b 693
91447636
A
694 thread_block((thread_continue_t)thread_terminate_continue);
695 /*NOTREACHED*/
55e303ae
A
696}
697
e8c3f781
A
698static bool
699thread_ref_release(thread_t thread)
3e170ce0 700{
e8c3f781
A
701 if (thread == THREAD_NULL) {
702 return false;
703 }
d9a64523 704
39037602
A
705 assert_thread_magic(thread);
706
e8c3f781 707 return os_ref_release(&thread->ref_count) == 0;
3e170ce0
A
708}
709
e8c3f781 710/* Drop a thread refcount safely without triggering a zfree */
55e303ae 711void
e8c3f781 712thread_deallocate_safe(thread_t thread)
1c79356b 713{
e8c3f781
A
714 if (__improbable(thread_ref_release(thread))) {
715 /* enqueue the thread for thread deallocate deamon to call thread_deallocate_complete */
716 thread_deallocate_enqueue(thread);
717 }
718}
5ba3f43e 719
e8c3f781
A
720void
721thread_deallocate(thread_t thread)
722{
723 if (__improbable(thread_ref_release(thread))) {
724 thread_deallocate_complete(thread);
725 }
d9a64523
A
726}
727
728void
729thread_deallocate_complete(
0a7de745 730 thread_t thread)
d9a64523 731{
0a7de745 732 task_t task;
d9a64523
A
733
734 assert_thread_magic(thread);
735
e8c3f781 736 assert(os_ref_get_count(&thread->ref_count) == 0);
d9a64523 737
0a7de745 738 if (!(thread->state & TH_TERMINATE2)) {
39236c6e 739 panic("thread_deallocate: thread not properly terminated\n");
0a7de745 740 }
39236c6e 741
3e170ce0
A
742 assert(thread->runq == PROCESSOR_NULL);
743
39236c6e
A
744#if KPC
745 kpc_thread_destroy(thread);
746#endif
747
91447636 748 ipc_thread_terminate(thread);
1c79356b 749
a1c7dba1
A
750 proc_thread_qos_deallocate(thread);
751
91447636 752 task = thread->task;
0b4e3aa0 753
a39ff7e2 754#ifdef MACH_BSD
91447636
A
755 {
756 void *ut = thread->uthread;
0b4e3aa0 757
91447636 758 thread->uthread = NULL;
2d21ac55 759 uthread_zone_free(ut);
91447636 760 }
a39ff7e2 761#endif /* MACH_BSD */
0b4e3aa0 762
0a7de745 763 if (thread->t_ledger) {
316670eb 764 ledger_dereference(thread->t_ledger);
0a7de745
A
765 }
766 if (thread->t_threadledger) {
316670eb 767 ledger_dereference(thread->t_threadledger);
0a7de745 768 }
316670eb 769
d9a64523 770 assert(thread->turnstile != TURNSTILE_NULL);
0a7de745 771 if (thread->turnstile) {
d9a64523 772 turnstile_deallocate(thread->turnstile);
0a7de745 773 }
d9a64523 774
0a7de745 775 if (IPC_VOUCHER_NULL != thread->ith_voucher) {
fe8ab488 776 ipc_voucher_release(thread->ith_voucher);
0a7de745 777 }
fe8ab488 778
0a7de745 779 if (thread->thread_io_stats) {
fe8ab488 780 kfree(thread->thread_io_stats, sizeof(struct io_stat_info));
0a7de745 781 }
fe8ab488 782
0a7de745 783 if (thread->kernel_stack != 0) {
91447636 784 stack_free(thread);
0a7de745 785 }
0b4e3aa0 786
b0d623f7 787 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
91447636 788 machine_thread_destroy(thread);
1c79356b 789
6d2010ae
A
790 task_deallocate(task);
791
39037602
A
792#if MACH_ASSERT
793 assert_thread_magic(thread);
794 thread->thread_magic = 0;
795#endif /* MACH_ASSERT */
796
91447636
A
797 zfree(thread_zone, thread);
798}
0b4e3aa0 799
813fb2f6
A
800/*
801 * thread_inspect_deallocate:
802 *
803 * Drop a thread inspection reference.
804 */
805void
806thread_inspect_deallocate(
0a7de745 807 thread_inspect_t thread_inspect)
813fb2f6 808{
0a7de745 809 return thread_deallocate((thread_t)thread_inspect);
813fb2f6
A
810}
811
39037602 812/*
cb323159 813 * thread_exception_queue_invoke:
39037602 814 *
5ba3f43e 815 * Deliver EXC_{RESOURCE,GUARD} exception
39037602
A
816 */
817static void
cb323159
A
818thread_exception_queue_invoke(mpsc_queue_chain_t elm,
819 __assert_only mpsc_daemon_queue_t dq)
39037602
A
820{
821 struct thread_exception_elt *elt;
822 task_t task;
823 thread_t thread;
5ba3f43e 824 exception_type_t etype;
39037602 825
cb323159
A
826 assert(dq == &thread_exception_queue);
827 elt = mpsc_queue_element(elm, struct thread_exception_elt, link);
39037602 828
cb323159
A
829 etype = elt->exception_type;
830 task = elt->exception_task;
831 thread = elt->exception_thread;
832 assert_thread_magic(thread);
39037602 833
cb323159 834 kfree(elt, sizeof(*elt));
39037602 835
cb323159
A
836 /* wait for all the threads in the task to terminate */
837 task_lock(task);
838 task_wait_till_threads_terminate_locked(task);
839 task_unlock(task);
39037602 840
cb323159
A
841 /* Consumes the task ref returned by task_generate_corpse_internal */
842 task_deallocate(task);
843 /* Consumes the thread ref returned by task_generate_corpse_internal */
844 thread_deallocate(thread);
39037602 845
cb323159
A
846 /* Deliver the notification, also clears the corpse. */
847 task_deliver_crash_notification(task, thread, etype, 0);
39037602
A
848}
849
850/*
851 * thread_exception_enqueue:
852 *
5ba3f43e 853 * Enqueue a corpse port to be delivered an EXC_{RESOURCE,GUARD}.
39037602
A
854 */
855void
856thread_exception_enqueue(
0a7de745
A
857 task_t task,
858 thread_t thread,
5ba3f43e 859 exception_type_t etype)
39037602 860{
5ba3f43e 861 assert(EXC_RESOURCE == etype || EXC_GUARD == etype);
0a7de745 862 struct thread_exception_elt *elt = kalloc(sizeof(*elt));
5ba3f43e 863 elt->exception_type = etype;
39037602
A
864 elt->exception_task = task;
865 elt->exception_thread = thread;
866
cb323159
A
867 mpsc_daemon_enqueue(&thread_exception_queue, &elt->link,
868 MPSC_QUEUE_DISABLE_PREEMPTION);
39037602
A
869}
870
871/*
872 * thread_copy_resource_info
873 *
874 * Copy the resource info counters from source
875 * thread to destination thread.
876 */
877void
878thread_copy_resource_info(
879 thread_t dst_thread,
880 thread_t src_thread)
881{
39037602
A
882 dst_thread->c_switch = src_thread->c_switch;
883 dst_thread->p_switch = src_thread->p_switch;
884 dst_thread->ps_switch = src_thread->ps_switch;
885 dst_thread->precise_user_kernel_time = src_thread->precise_user_kernel_time;
886 dst_thread->user_timer = src_thread->user_timer;
887 dst_thread->user_timer_save = src_thread->user_timer_save;
5ba3f43e 888 dst_thread->system_timer = src_thread->system_timer;
39037602 889 dst_thread->system_timer_save = src_thread->system_timer_save;
d9a64523 890 dst_thread->runnable_timer = src_thread->runnable_timer;
5ba3f43e
A
891 dst_thread->vtimer_user_save = src_thread->vtimer_user_save;
892 dst_thread->vtimer_prof_save = src_thread->vtimer_prof_save;
893 dst_thread->vtimer_rlim_save = src_thread->vtimer_rlim_save;
894 dst_thread->vtimer_qos_save = src_thread->vtimer_qos_save;
39037602
A
895 dst_thread->syscalls_unix = src_thread->syscalls_unix;
896 dst_thread->syscalls_mach = src_thread->syscalls_mach;
897 ledger_rollup(dst_thread->t_threadledger, src_thread->t_threadledger);
898 *dst_thread->thread_io_stats = *src_thread->thread_io_stats;
39037602
A
899}
900
91447636 901static void
cb323159
A
902thread_terminate_queue_invoke(mpsc_queue_chain_t e,
903 __assert_only mpsc_daemon_queue_t dq)
91447636 904{
cb323159
A
905 thread_t thread = mpsc_queue_element(e, struct thread, mpsc_links);
906 task_t task = thread->task;
5ba3f43e 907
cb323159 908 assert(dq == &thread_terminate_queue);
39037602 909
cb323159 910 task_lock(task);
b0d623f7 911
cb323159
A
912 /*
913 * if marked for crash reporting, skip reaping.
914 * The corpse delivery thread will clear bit and enqueue
915 * for reaping when done
916 *
917 * Note: the inspection field is set under the task lock
918 *
919 * FIXME[mad]: why enqueue for termination before `inspection` is false ?
920 */
921 if (__improbable(thread->inspection)) {
922 simple_lock(&crashed_threads_lock, &thread_lck_grp);
91447636 923 task_unlock(task);
1c79356b 924
cb323159
A
925 enqueue_tail(&crashed_threads_queue, &thread->runq_links);
926 simple_unlock(&crashed_threads_lock);
927 return;
0b4e3aa0 928 }
1c79356b 929
d9a64523 930
cb323159
A
931 task->total_user_time += timer_grab(&thread->user_timer);
932 task->total_ptime += timer_grab(&thread->ptime);
933 task->total_runnable_time += timer_grab(&thread->runnable_timer);
934 if (thread->precise_user_kernel_time) {
935 task->total_system_time += timer_grab(&thread->system_timer);
936 } else {
937 task->total_user_time += timer_grab(&thread->system_timer);
938 }
d9a64523 939
cb323159
A
940 task->c_switch += thread->c_switch;
941 task->p_switch += thread->p_switch;
942 task->ps_switch += thread->ps_switch;
d9a64523 943
cb323159
A
944 task->syscalls_unix += thread->syscalls_unix;
945 task->syscalls_mach += thread->syscalls_mach;
d9a64523 946
cb323159
A
947 task->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
948 task->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
949 task->task_gpu_ns += ml_gpu_stat(thread);
950 task->task_energy += ml_energy_stat(thread);
951 task->decompressions += thread->decompressions;
d9a64523 952
cb323159
A
953#if MONOTONIC
954 mt_terminate_update(task, thread);
955#endif /* MONOTONIC */
d9a64523 956
cb323159 957 thread_update_qos_cpu_time(thread);
d9a64523 958
cb323159
A
959 queue_remove(&task->threads, thread, thread_t, task_threads);
960 task->thread_count--;
d9a64523
A
961
962 /*
cb323159
A
963 * If the task is being halted, and there is only one thread
964 * left in the task after this one, then wakeup that thread.
d9a64523 965 */
cb323159
A
966 if (task->thread_count == 1 && task->halting) {
967 thread_wakeup((event_t)&task->halting);
968 }
969
970 task_unlock(task);
d9a64523 971
cb323159
A
972 lck_mtx_lock(&tasks_threads_lock);
973 queue_remove(&threads, thread, thread_t, threads);
974 threads_count--;
975 lck_mtx_unlock(&tasks_threads_lock);
d9a64523 976
cb323159
A
977 thread_deallocate(thread);
978}
d9a64523 979
cb323159
A
980static void
981thread_deallocate_queue_invoke(mpsc_queue_chain_t e,
982 __assert_only mpsc_daemon_queue_t dq)
983{
984 thread_t thread = mpsc_queue_element(e, struct thread, mpsc_links);
d9a64523 985
cb323159 986 assert(dq == &thread_deallocate_queue);
91447636 987
cb323159 988 thread_deallocate_complete(thread);
1c79356b
A
989}
990
9bccf70c 991/*
91447636
A
992 * thread_terminate_enqueue:
993 *
994 * Enqueue a terminating thread for final disposition.
995 *
996 * Called at splsched.
9bccf70c 997 */
1c79356b 998void
91447636 999thread_terminate_enqueue(
0a7de745 1000 thread_t thread)
1c79356b 1001{
a39ff7e2 1002 KDBG_RELEASE(TRACE_DATA_THREAD_TERMINATE, thread->thread_id);
fe8ab488 1003
cb323159
A
1004 mpsc_daemon_enqueue(&thread_terminate_queue, &thread->mpsc_links,
1005 MPSC_QUEUE_DISABLE_PREEMPTION);
1c79356b
A
1006}
1007
d9a64523
A
1008/*
1009 * thread_deallocate_enqueue:
1010 *
1011 * Enqueue a thread for final deallocation.
1012 */
1013static void
1014thread_deallocate_enqueue(
0a7de745 1015 thread_t thread)
d9a64523 1016{
cb323159
A
1017 mpsc_daemon_enqueue(&thread_deallocate_queue, &thread->mpsc_links,
1018 MPSC_QUEUE_DISABLE_PREEMPTION);
d9a64523
A
1019}
1020
3e170ce0
A
1021/*
1022 * thread_terminate_crashed_threads:
39037602 1023 * walk the list of crashed threads and put back set of threads
3e170ce0
A
1024 * who are no longer being inspected.
1025 */
1026void
cb323159 1027thread_terminate_crashed_threads(void)
3e170ce0 1028{
39037602 1029 thread_t th_remove;
3e170ce0 1030
cb323159 1031 simple_lock(&crashed_threads_lock, &thread_lck_grp);
3e170ce0
A
1032 /*
1033 * loop through the crashed threads queue
1034 * to put any threads that are not being inspected anymore
1035 */
3e170ce0 1036
39037602 1037 qe_foreach_element_safe(th_remove, &crashed_threads_queue, runq_links) {
3e170ce0
A
1038 /* make sure current_thread is never in crashed queue */
1039 assert(th_remove != current_thread());
39037602
A
1040
1041 if (th_remove->inspection == FALSE) {
cb323159
A
1042 remqueue(&th_remove->runq_links);
1043 mpsc_daemon_enqueue(&thread_terminate_queue, &th_remove->mpsc_links,
1044 MPSC_QUEUE_NONE);
3e170ce0
A
1045 }
1046 }
1047
cb323159 1048 simple_unlock(&crashed_threads_lock);
3e170ce0
A
1049}
1050
91447636 1051/*
cb323159 1052 * thread_stack_queue_invoke:
91447636
A
1053 *
1054 * Perform stack allocation as required due to
1055 * invoke failures.
1056 */
1057static void
cb323159
A
1058thread_stack_queue_invoke(mpsc_queue_chain_t elm,
1059 __assert_only mpsc_daemon_queue_t dq)
9bccf70c 1060{
cb323159 1061 thread_t thread = mpsc_queue_element(elm, struct thread, mpsc_links);
3e170ce0 1062
cb323159 1063 assert(dq == &thread_stack_queue);
0a7de745 1064
cb323159
A
1065 /* allocate stack with interrupts enabled so that we can call into VM */
1066 stack_alloc(thread);
91447636 1067
cb323159 1068 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_WAIT) | DBG_FUNC_END, thread_tid(thread), 0, 0, 0, 0);
91447636 1069
cb323159
A
1070 spl_t s = splsched();
1071 thread_lock(thread);
1072 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
1073 thread_unlock(thread);
39236c6e 1074 splx(s);
9bccf70c 1075}
1c79356b
A
1076
1077/*
91447636 1078 * thread_stack_enqueue:
1c79356b 1079 *
91447636 1080 * Enqueue a thread for stack allocation.
1c79356b 1081 *
91447636 1082 * Called at splsched.
1c79356b
A
1083 */
1084void
91447636 1085thread_stack_enqueue(
0a7de745 1086 thread_t thread)
1c79356b 1087{
0a7de745 1088 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_WAIT) | DBG_FUNC_START, thread_tid(thread), 0, 0, 0, 0);
39037602 1089 assert_thread_magic(thread);
3e170ce0 1090
cb323159
A
1091 mpsc_daemon_enqueue(&thread_stack_queue, &thread->mpsc_links,
1092 MPSC_QUEUE_DISABLE_PREEMPTION);
91447636 1093}
9bccf70c 1094
91447636
A
1095void
1096thread_daemon_init(void)
1097{
0a7de745 1098 kern_return_t result;
0b4e3aa0 1099
cb323159 1100 thread_deallocate_daemon_init();
1c79356b 1101
cb323159
A
1102 thread_deallocate_daemon_register_queue(&thread_terminate_queue,
1103 thread_terminate_queue_invoke);
1c79356b 1104
cb323159
A
1105 thread_deallocate_daemon_register_queue(&thread_deallocate_queue,
1106 thread_deallocate_queue_invoke);
1c79356b 1107
cb323159
A
1108 simple_lock_init(&crashed_threads_lock, 0);
1109 queue_init(&crashed_threads_queue);
1c79356b 1110
cb323159
A
1111 result = mpsc_daemon_queue_init_with_thread(&thread_stack_queue,
1112 thread_stack_queue_invoke, BASEPRI_PREEMPT_HIGH,
1113 "daemon.thread-stack");
0a7de745 1114 if (result != KERN_SUCCESS) {
91447636 1115 panic("thread_daemon_init: thread_stack_daemon");
0a7de745 1116 }
1c79356b 1117
cb323159
A
1118 result = mpsc_daemon_queue_init_with_thread(&thread_exception_queue,
1119 thread_exception_queue_invoke, MINPRI_KERNEL,
1120 "daemon.thread-exception");
0a7de745 1121 if (result != KERN_SUCCESS) {
39037602 1122 panic("thread_daemon_init: thread_exception_daemon");
0a7de745 1123 }
1c79356b
A
1124}
1125
0a7de745
A
1126#define TH_OPTION_NONE 0x00
1127#define TH_OPTION_NOCRED 0x01
1128#define TH_OPTION_NOSUSP 0x02
1129#define TH_OPTION_WORKQ 0x04
39037602 1130
1c79356b
A
1131/*
1132 * Create a new thread.
55e303ae 1133 * Doesn't start the thread running.
fe8ab488
A
1134 *
1135 * Task and tasks_threads_lock are returned locked on success.
1c79356b 1136 */
55e303ae
A
1137static kern_return_t
1138thread_create_internal(
0a7de745
A
1139 task_t parent_task,
1140 integer_t priority,
1141 thread_continue_t continuation,
1142 void *parameter,
1143 int options,
1144 thread_t *out_thread)
1c79356b 1145{
0a7de745
A
1146 thread_t new_thread;
1147 static thread_t first_thread;
1c79356b
A
1148
1149 /*
1150 * Allocate a thread and initialize static fields
1151 */
0a7de745 1152 if (first_thread == THREAD_NULL) {
91447636 1153 new_thread = first_thread = current_thread();
0a7de745 1154 } else {
55e303ae 1155 new_thread = (thread_t)zalloc(thread_zone);
0a7de745
A
1156 }
1157 if (new_thread == THREAD_NULL) {
1158 return KERN_RESOURCE_SHORTAGE;
1159 }
1c79356b 1160
0a7de745 1161 if (new_thread != first_thread) {
55e303ae 1162 *new_thread = thread_template;
0a7de745 1163 }
55e303ae 1164
e8c3f781
A
1165 os_ref_init_count(&new_thread->ref_count, &thread_refgrp, 2);
1166
55e303ae 1167#ifdef MACH_BSD
b0d623f7
A
1168 new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
1169 if (new_thread->uthread == NULL) {
39037602
A
1170#if MACH_ASSERT
1171 new_thread->thread_magic = 0;
1172#endif /* MACH_ASSERT */
1173
b0d623f7 1174 zfree(thread_zone, new_thread);
0a7de745 1175 return KERN_RESOURCE_SHORTAGE;
55e303ae
A
1176 }
1177#endif /* MACH_BSD */
1c79356b 1178
55e303ae
A
1179 if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
1180#ifdef MACH_BSD
b0d623f7 1181 void *ut = new_thread->uthread;
1c79356b 1182
b0d623f7
A
1183 new_thread->uthread = NULL;
1184 /* cred free may not be necessary */
39037602 1185 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
b0d623f7
A
1186 uthread_cred_free(ut);
1187 uthread_zone_free(ut);
55e303ae 1188#endif /* MACH_BSD */
b0d623f7 1189
39037602
A
1190#if MACH_ASSERT
1191 new_thread->thread_magic = 0;
1192#endif /* MACH_ASSERT */
1193
91447636 1194 zfree(thread_zone, new_thread);
0a7de745 1195 return KERN_FAILURE;
55e303ae
A
1196 }
1197
316670eb 1198 new_thread->task = parent_task;
55e303ae
A
1199
1200 thread_lock_init(new_thread);
1201 wake_lock_init(new_thread);
1202
b0d623f7 1203 lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
0b4e3aa0 1204
55e303ae 1205 ipc_thread_init(new_thread);
55e303ae 1206
91447636 1207 new_thread->continuation = continuation;
d9a64523
A
1208 new_thread->parameter = parameter;
1209 new_thread->inheritor_flags = TURNSTILE_UPDATE_FLAGS_NONE;
cb323159
A
1210 priority_queue_init(&new_thread->sched_inheritor_queue,
1211 PRIORITY_QUEUE_BUILTIN_MAX_HEAP);
1212 priority_queue_init(&new_thread->base_inheritor_queue,
0a7de745 1213 PRIORITY_QUEUE_BUILTIN_MAX_HEAP);
cb323159
A
1214#if CONFIG_SCHED_CLUTCH
1215 priority_queue_entry_init(&new_thread->sched_clutchpri_link);
1216#endif /* CONFIG_SCHED_CLUTCH */
0b4e3aa0 1217
fe8ab488
A
1218 /* Allocate I/O Statistics structure */
1219 new_thread->thread_io_stats = (io_stat_info_t)kalloc(sizeof(struct io_stat_info));
1220 assert(new_thread->thread_io_stats != NULL);
1221 bzero(new_thread->thread_io_stats, sizeof(struct io_stat_info));
5ba3f43e
A
1222
1223#if KASAN
1224 kasan_init_thread(&new_thread->kasan_data);
1225#endif
fe8ab488 1226
cb323159
A
1227#if CONFIG_KSANCOV
1228 new_thread->ksancov_data = NULL;
1229#endif
1230
fe8ab488
A
1231#if CONFIG_IOSCHED
1232 /* Clear out the I/O Scheduling info for AppleFSCompression */
1233 new_thread->decmp_upl = NULL;
0a7de745 1234#endif /* CONFIG_IOSCHED */
fe8ab488 1235
d9a64523
A
1236#if DEVELOPMENT || DEBUG
1237 task_lock(parent_task);
1238 uint16_t thread_limit = parent_task->task_thread_limit;
1239 if (exc_resource_threads_enabled &&
1240 thread_limit > 0 &&
1241 parent_task->thread_count >= thread_limit &&
1242 !parent_task->task_has_crossed_thread_limit &&
1243 !(parent_task->t_flags & TF_CORPSE)) {
1244 int thread_count = parent_task->thread_count;
1245 parent_task->task_has_crossed_thread_limit = TRUE;
1246 task_unlock(parent_task);
1247 SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(parent_task, thread_count);
0a7de745 1248 } else {
d9a64523
A
1249 task_unlock(parent_task);
1250 }
1251#endif
1252
b0d623f7 1253 lck_mtx_lock(&tasks_threads_lock);
1c79356b
A
1254 task_lock(parent_task);
1255
39037602
A
1256 /*
1257 * Fail thread creation if parent task is being torn down or has too many threads
1258 * If the caller asked for TH_OPTION_NOSUSP, also fail if the parent task is suspended
1259 */
1260 if (parent_task->active == 0 || parent_task->halting ||
1261 (parent_task->suspend_count > 0 && (options & TH_OPTION_NOSUSP) != 0) ||
1262 (parent_task->thread_count >= task_threadmax && parent_task != kernel_task)) {
1c79356b 1263 task_unlock(parent_task);
b0d623f7 1264 lck_mtx_unlock(&tasks_threads_lock);
55e303ae
A
1265
1266#ifdef MACH_BSD
1267 {
55e303ae
A
1268 void *ut = new_thread->uthread;
1269
1270 new_thread->uthread = NULL;
39037602 1271 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
2d21ac55
A
1272 /* cred free may not be necessary */
1273 uthread_cred_free(ut);
1274 uthread_zone_free(ut);
55e303ae
A
1275 }
1276#endif /* MACH_BSD */
91447636
A
1277 ipc_thread_disable(new_thread);
1278 ipc_thread_terminate(new_thread);
fe8ab488 1279 kfree(new_thread->thread_io_stats, sizeof(struct io_stat_info));
b0d623f7 1280 lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
55e303ae 1281 machine_thread_destroy(new_thread);
91447636 1282 zfree(thread_zone, new_thread);
0a7de745 1283 return KERN_FAILURE;
1c79356b
A
1284 }
1285
0a7de745
A
1286 /* Protected by the tasks_threads_lock */
1287 new_thread->thread_id = ++thread_unique_id;
1288
b0d623f7
A
1289 /* New threads inherit any default state on the task */
1290 machine_thread_inherit_taskwide(new_thread, parent_task);
1291
91447636 1292 task_reference_internal(parent_task);
55e303ae 1293
316670eb
A
1294 if (new_thread->task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
1295 /*
1296 * This task has a per-thread CPU limit; make sure this new thread
1297 * gets its limit set too, before it gets out of the kernel.
1298 */
5c9f4661 1299 act_set_astledger(new_thread);
316670eb 1300 }
fe8ab488
A
1301
1302 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
1303 if ((new_thread->t_threadledger = ledger_instantiate(thread_ledger_template,
0a7de745 1304 LEDGER_CREATE_INACTIVE_ENTRIES)) != LEDGER_NULL) {
fe8ab488
A
1305 ledger_entry_setactive(new_thread->t_threadledger, thread_ledgers.cpu_time);
1306 }
1307
fe8ab488
A
1308 new_thread->t_bankledger = LEDGER_NULL;
1309 new_thread->t_deduct_bank_ledger_time = 0;
5ba3f43e 1310 new_thread->t_deduct_bank_ledger_energy = 0;
fe8ab488 1311
316670eb 1312 new_thread->t_ledger = new_thread->task->ledger;
0a7de745 1313 if (new_thread->t_ledger) {
316670eb 1314 ledger_reference(new_thread->t_ledger);
0a7de745 1315 }
316670eb 1316
fe8ab488
A
1317#if defined(CONFIG_SCHED_MULTIQ)
1318 /* Cache the task's sched_group */
1319 new_thread->sched_group = parent_task->sched_group;
1320#endif /* defined(CONFIG_SCHED_MULTIQ) */
1321
55e303ae
A
1322 /* Cache the task's map */
1323 new_thread->map = parent_task->map;
1c79356b 1324
91447636
A
1325 timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
1326 timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
1c79356b 1327
39236c6e
A
1328#if KPC
1329 kpc_thread_create(new_thread);
1330#endif
b0d623f7 1331
1c79356b 1332 /* Set the thread's scheduling parameters */
6d2010ae 1333 new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
55e303ae
A
1334 new_thread->max_priority = parent_task->max_priority;
1335 new_thread->task_priority = parent_task->priority;
316670eb 1336
cb323159 1337
3e170ce0
A
1338 int new_priority = (priority < 0) ? parent_task->priority: priority;
1339 new_priority = (priority < 0)? parent_task->priority: priority;
0a7de745 1340 if (new_priority > new_thread->max_priority) {
3e170ce0 1341 new_priority = new_thread->max_priority;
0a7de745 1342 }
5ba3f43e
A
1343#if CONFIG_EMBEDDED
1344 if (new_priority < MAXPRI_THROTTLE) {
1345 new_priority = MAXPRI_THROTTLE;
1346 }
1347#endif /* CONFIG_EMBEDDED */
3e170ce0
A
1348
1349 new_thread->importance = new_priority - new_thread->task_priority;
fe8ab488 1350
3e170ce0 1351 sched_set_thread_base_priority(new_thread, new_priority);
1c79356b 1352
39037602
A
1353#if defined(CONFIG_SCHED_TIMESHARE_CORE)
1354 new_thread->sched_stamp = sched_tick;
cb323159
A
1355#if CONFIG_SCHED_CLUTCH
1356 new_thread->pri_shift = sched_clutch_thread_pri_shift(new_thread, new_thread->th_sched_bucket);
1357#else /* CONFIG_SCHED_CLUTCH */
39037602 1358 new_thread->pri_shift = sched_pri_shifts[new_thread->th_sched_bucket];
cb323159 1359#endif /* CONFIG_SCHED_CLUTCH */
39037602
A
1360#endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
1361
5ba3f43e 1362#if CONFIG_EMBEDDED
0a7de745 1363 if (parent_task->max_priority <= MAXPRI_THROTTLE) {
5ba3f43e 1364 sched_thread_mode_demote(new_thread, TH_SFLAG_THROTTLED);
0a7de745 1365 }
5ba3f43e 1366#endif /* CONFIG_EMBEDDED */
490019cf 1367
fe8ab488
A
1368 thread_policy_create(new_thread);
1369
1370 /* Chain the thread onto the task's list */
1371 queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
1372 parent_task->thread_count++;
1373
1374 /* So terminating threads don't need to take the task lock to decrement */
cb323159 1375 os_atomic_inc(&parent_task->active_thread_count, relaxed);
5ba3f43e 1376
fe8ab488
A
1377 queue_enter(&threads, new_thread, thread_t, threads);
1378 threads_count++;
1379
55e303ae 1380 new_thread->active = TRUE;
39037602
A
1381 if (task_is_a_corpse_fork(parent_task)) {
1382 /* Set the inspection bit if the task is a corpse fork */
1383 new_thread->inspection = TRUE;
1384 } else {
1385 new_thread->inspection = FALSE;
1386 }
1387 new_thread->corpse_dup = FALSE;
d9a64523 1388 new_thread->turnstile = turnstile_alloc();
55e303ae 1389 *out_thread = new_thread;
1c79356b 1390
a39ff7e2
A
1391 if (kdebug_enable) {
1392 long args[4] = {};
1c79356b 1393
a39ff7e2 1394 kdbg_trace_data(parent_task->bsd_info, &args[1], &args[3]);
55e303ae 1395
743345f9
A
1396 /*
1397 * Starting with 26604425, exec'ing creates a new task/thread.
1398 *
1399 * NEWTHREAD in the current process has two possible meanings:
1400 *
1401 * 1) Create a new thread for this process.
a39ff7e2
A
1402 * 2) Create a new thread for the future process this will become in an
1403 * exec.
743345f9
A
1404 *
1405 * To disambiguate these, arg3 will be set to TRUE for case #2.
1406 *
1407 * The value we need to find (TPF_EXEC_COPY) is stable in the case of a
1408 * task exec'ing. The read of t_procflags does not take the proc_lock.
1409 */
a39ff7e2 1410 args[2] = task_is_exec_copy(parent_task) ? 1 : 0;
743345f9 1411
a39ff7e2 1412 KDBG_RELEASE(TRACE_DATA_NEWTHREAD, (uintptr_t)thread_tid(new_thread),
0a7de745 1413 args[1], args[2], args[3]);
5ba3f43e 1414
a39ff7e2 1415 kdbg_trace_string(parent_task->bsd_info, &args[0], &args[1],
0a7de745 1416 &args[2], &args[3]);
a39ff7e2 1417 KDBG_RELEASE(TRACE_STRING_NEWTHREAD, args[0], args[1], args[2],
0a7de745 1418 args[3]);
1c79356b
A
1419 }
1420
2d21ac55
A
1421 DTRACE_PROC1(lwp__create, thread_t, *out_thread);
1422
0a7de745 1423 return KERN_SUCCESS;
1c79356b
A
1424}
1425
6d2010ae
A
1426static kern_return_t
1427thread_create_internal2(
0a7de745
A
1428 task_t task,
1429 thread_t *new_thread,
1430 boolean_t from_user,
1431 thread_continue_t continuation)
1c79356b 1432{
0a7de745
A
1433 kern_return_t result;
1434 thread_t thread;
1c79356b 1435
0a7de745
A
1436 if (task == TASK_NULL || task == kernel_task) {
1437 return KERN_INVALID_ARGUMENT;
1438 }
1c79356b 1439
d9a64523 1440 result = thread_create_internal(task, -1, continuation, NULL, TH_OPTION_NONE, &thread);
0a7de745
A
1441 if (result != KERN_SUCCESS) {
1442 return result;
1443 }
1c79356b 1444
55e303ae
A
1445 thread->user_stop_count = 1;
1446 thread_hold(thread);
0a7de745 1447 if (task->suspend_count > 0) {
55e303ae 1448 thread_hold(thread);
0a7de745 1449 }
1c79356b 1450
0a7de745 1451 if (from_user) {
6d2010ae 1452 extmod_statistics_incr_thread_create(task);
0a7de745 1453 }
6d2010ae 1454
9bccf70c 1455 task_unlock(task);
b0d623f7 1456 lck_mtx_unlock(&tasks_threads_lock);
0a7de745 1457
55e303ae 1458 *new_thread = thread;
1c79356b 1459
0a7de745 1460 return KERN_SUCCESS;
1c79356b
A
1461}
1462
6d2010ae 1463/* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1c79356b 1464kern_return_t
6d2010ae 1465thread_create(
0a7de745
A
1466 task_t task,
1467 thread_t *new_thread);
6d2010ae
A
1468
1469kern_return_t
1470thread_create(
0a7de745
A
1471 task_t task,
1472 thread_t *new_thread)
6d2010ae 1473{
3e170ce0 1474 return thread_create_internal2(task, new_thread, FALSE, (thread_continue_t)thread_bootstrap_return);
6d2010ae
A
1475}
1476
1477kern_return_t
1478thread_create_from_user(
0a7de745
A
1479 task_t task,
1480 thread_t *new_thread)
6d2010ae 1481{
3e170ce0
A
1482 return thread_create_internal2(task, new_thread, TRUE, (thread_continue_t)thread_bootstrap_return);
1483}
1484
1485kern_return_t
1486thread_create_with_continuation(
0a7de745
A
1487 task_t task,
1488 thread_t *new_thread,
1489 thread_continue_t continuation)
3e170ce0
A
1490{
1491 return thread_create_internal2(task, new_thread, FALSE, continuation);
6d2010ae
A
1492}
1493
743345f9
A
1494/*
1495 * Create a thread that is already started, but is waiting on an event
1496 */
1497static kern_return_t
1498thread_create_waiting_internal(
1499 task_t task,
1500 thread_continue_t continuation,
1501 event_t event,
d9a64523 1502 block_hint_t block_hint,
743345f9
A
1503 int options,
1504 thread_t *new_thread)
1505{
1506 kern_return_t result;
1507 thread_t thread;
1508
0a7de745
A
1509 if (task == TASK_NULL || task == kernel_task) {
1510 return KERN_INVALID_ARGUMENT;
1511 }
743345f9 1512
d9a64523 1513 result = thread_create_internal(task, -1, continuation, NULL,
0a7de745
A
1514 options, &thread);
1515 if (result != KERN_SUCCESS) {
1516 return result;
1517 }
743345f9
A
1518
1519 /* note no user_stop_count or thread_hold here */
1520
0a7de745 1521 if (task->suspend_count > 0) {
743345f9 1522 thread_hold(thread);
0a7de745 1523 }
743345f9
A
1524
1525 thread_mtx_lock(thread);
d9a64523
A
1526 thread_set_pending_block_hint(thread, block_hint);
1527 if (options & TH_OPTION_WORKQ) {
1528 thread->static_param = true;
1529 event = workq_thread_init_and_wq_lock(task, thread);
1530 }
743345f9
A
1531 thread_start_in_assert_wait(thread, event, THREAD_INTERRUPTIBLE);
1532 thread_mtx_unlock(thread);
1533
1534 task_unlock(task);
1535 lck_mtx_unlock(&tasks_threads_lock);
1536
1537 *new_thread = thread;
1538
0a7de745 1539 return KERN_SUCCESS;
743345f9
A
1540}
1541
1542kern_return_t
1543thread_create_waiting(
1544 task_t task,
1545 thread_continue_t continuation,
1546 event_t event,
1547 thread_t *new_thread)
1548{
1549 return thread_create_waiting_internal(task, continuation, event,
0a7de745 1550 kThreadWaitNone, TH_OPTION_NONE, new_thread);
743345f9
A
1551}
1552
1553
6d2010ae
A
1554static kern_return_t
1555thread_create_running_internal2(
39037602 1556 task_t task,
1c79356b
A
1557 int flavor,
1558 thread_state_t new_state,
1559 mach_msg_type_number_t new_state_count,
0a7de745
A
1560 thread_t *new_thread,
1561 boolean_t from_user)
1c79356b 1562{
39037602 1563 kern_return_t result;
0a7de745 1564 thread_t thread;
9bccf70c 1565
0a7de745
A
1566 if (task == TASK_NULL || task == kernel_task) {
1567 return KERN_INVALID_ARGUMENT;
1568 }
1c79356b 1569
d9a64523 1570 result = thread_create_internal(task, -1,
0a7de745
A
1571 (thread_continue_t)thread_bootstrap_return, NULL,
1572 TH_OPTION_NONE, &thread);
1573 if (result != KERN_SUCCESS) {
1574 return result;
1575 }
1c79356b 1576
0a7de745 1577 if (task->suspend_count > 0) {
39037602 1578 thread_hold(thread);
0a7de745 1579 }
39037602 1580
d9a64523
A
1581 if (from_user) {
1582 result = machine_thread_state_convert_from_user(thread, flavor,
0a7de745 1583 new_state, new_state_count);
d9a64523
A
1584 }
1585 if (result == KERN_SUCCESS) {
1586 result = machine_thread_set_state(thread, flavor, new_state,
0a7de745 1587 new_state_count);
d9a64523 1588 }
1c79356b 1589 if (result != KERN_SUCCESS) {
9bccf70c 1590 task_unlock(task);
b0d623f7 1591 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1592
55e303ae 1593 thread_terminate(thread);
91447636 1594 thread_deallocate(thread);
0a7de745 1595 return result;
1c79356b
A
1596 }
1597
91447636 1598 thread_mtx_lock(thread);
39037602 1599 thread_start(thread);
91447636 1600 thread_mtx_unlock(thread);
2d21ac55 1601
0a7de745 1602 if (from_user) {
6d2010ae 1603 extmod_statistics_incr_thread_create(task);
0a7de745 1604 }
6d2010ae 1605
9bccf70c 1606 task_unlock(task);
b0d623f7 1607 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1608
55e303ae 1609 *new_thread = thread;
9bccf70c 1610
0a7de745 1611 return result;
1c79356b
A
1612}
1613
6d2010ae
A
1614/* Prototype, see justification above */
1615kern_return_t
1616thread_create_running(
39037602 1617 task_t task,
6d2010ae
A
1618 int flavor,
1619 thread_state_t new_state,
1620 mach_msg_type_number_t new_state_count,
0a7de745 1621 thread_t *new_thread);
6d2010ae
A
1622
1623kern_return_t
1624thread_create_running(
39037602 1625 task_t task,
6d2010ae
A
1626 int flavor,
1627 thread_state_t new_state,
1628 mach_msg_type_number_t new_state_count,
0a7de745 1629 thread_t *new_thread)
6d2010ae
A
1630{
1631 return thread_create_running_internal2(
1632 task, flavor, new_state, new_state_count,
1633 new_thread, FALSE);
1634}
1635
1636kern_return_t
1637thread_create_running_from_user(
39037602 1638 task_t task,
6d2010ae
A
1639 int flavor,
1640 thread_state_t new_state,
1641 mach_msg_type_number_t new_state_count,
0a7de745 1642 thread_t *new_thread)
6d2010ae
A
1643{
1644 return thread_create_running_internal2(
1645 task, flavor, new_state, new_state_count,
1646 new_thread, TRUE);
1647}
1648
39037602
A
1649kern_return_t
1650thread_create_workq_waiting(
1651 task_t task,
743345f9 1652 thread_continue_t continuation,
39037602
A
1653 thread_t *new_thread)
1654{
d9a64523
A
1655 int options = TH_OPTION_NOCRED | TH_OPTION_NOSUSP | TH_OPTION_WORKQ;
1656 return thread_create_waiting_internal(task, continuation, NULL,
0a7de745 1657 kThreadWaitParkedWorkQueue, options, new_thread);
39037602
A
1658}
1659
1c79356b 1660/*
91447636 1661 * kernel_thread_create:
1c79356b 1662 *
55e303ae
A
1663 * Create a thread in the kernel task
1664 * to execute in kernel context.
1c79356b 1665 */
91447636 1666kern_return_t
55e303ae 1667kernel_thread_create(
0a7de745
A
1668 thread_continue_t continuation,
1669 void *parameter,
1670 integer_t priority,
1671 thread_t *new_thread)
1c79356b 1672{
0a7de745
A
1673 kern_return_t result;
1674 thread_t thread;
1675 task_t task = kernel_task;
1c79356b 1676
d9a64523 1677 result = thread_create_internal(task, priority, continuation, parameter,
0a7de745
A
1678 TH_OPTION_NOCRED | TH_OPTION_NONE, &thread);
1679 if (result != KERN_SUCCESS) {
1680 return result;
1681 }
1c79356b 1682
9bccf70c 1683 task_unlock(task);
b0d623f7 1684 lck_mtx_unlock(&tasks_threads_lock);
9bccf70c 1685
91447636 1686 stack_alloc(thread);
55e303ae 1687 assert(thread->kernel_stack != 0);
5ba3f43e
A
1688#if CONFIG_EMBEDDED
1689 if (priority > BASEPRI_KERNEL)
1690#endif
55e303ae
A
1691 thread->reserved_stack = thread->kernel_stack;
1692
0a7de745
A
1693 if (debug_task & 1) {
1694 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
1695 }
91447636
A
1696 *new_thread = thread;
1697
0a7de745 1698 return result;
55e303ae
A
1699}
1700
91447636
A
1701kern_return_t
1702kernel_thread_start_priority(
0a7de745
A
1703 thread_continue_t continuation,
1704 void *parameter,
1705 integer_t priority,
1706 thread_t *new_thread)
55e303ae 1707{
0a7de745
A
1708 kern_return_t result;
1709 thread_t thread;
1c79356b 1710
91447636 1711 result = kernel_thread_create(continuation, parameter, priority, &thread);
0a7de745
A
1712 if (result != KERN_SUCCESS) {
1713 return result;
1714 }
1c79356b 1715
0a7de745 1716 *new_thread = thread;
b0d623f7 1717
91447636 1718 thread_mtx_lock(thread);
39037602 1719 thread_start(thread);
91447636 1720 thread_mtx_unlock(thread);
1c79356b 1721
0a7de745 1722 return result;
91447636
A
1723}
1724
1725kern_return_t
1726kernel_thread_start(
0a7de745
A
1727 thread_continue_t continuation,
1728 void *parameter,
1729 thread_t *new_thread)
91447636
A
1730{
1731 return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
1c79356b
A
1732}
1733
3e170ce0
A
1734/* Separated into helper function so it can be used by THREAD_BASIC_INFO and THREAD_EXTENDED_INFO */
1735/* it is assumed that the thread is locked by the caller */
1736static void
1737retrieve_thread_basic_info(thread_t thread, thread_basic_info_t basic_info)
1738{
0a7de745 1739 int state, flags;
3e170ce0
A
1740
1741 /* fill in info */
1742
1743 thread_read_times(thread, &basic_info->user_time,
0a7de745 1744 &basic_info->system_time, NULL);
3e170ce0
A
1745
1746 /*
1747 * Update lazy-evaluated scheduler info because someone wants it.
1748 */
0a7de745 1749 if (SCHED(can_update_priority)(thread)) {
3e170ce0 1750 SCHED(update_priority)(thread);
0a7de745 1751 }
3e170ce0
A
1752
1753 basic_info->sleep_time = 0;
1754
1755 /*
1756 * To calculate cpu_usage, first correct for timer rate,
1757 * then for 5/8 ageing. The correction factor [3/5] is
1758 * (1/(5/8) - 1).
1759 */
1760 basic_info->cpu_usage = 0;
1761#if defined(CONFIG_SCHED_TIMESHARE_CORE)
1762 if (sched_tick_interval) {
0a7de745
A
1763 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1764 * TH_USAGE_SCALE) / sched_tick_interval);
3e170ce0
A
1765 basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
1766 }
1767#endif
1768
0a7de745 1769 if (basic_info->cpu_usage > TH_USAGE_SCALE) {
3e170ce0 1770 basic_info->cpu_usage = TH_USAGE_SCALE;
0a7de745 1771 }
3e170ce0
A
1772
1773 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
0a7de745 1774 POLICY_TIMESHARE: POLICY_RR);
3e170ce0
A
1775
1776 flags = 0;
0a7de745 1777 if (thread->options & TH_OPT_IDLE_THREAD) {
3e170ce0 1778 flags |= TH_FLAGS_IDLE;
0a7de745 1779 }
3e170ce0
A
1780
1781 if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) {
1782 flags |= TH_FLAGS_GLOBAL_FORCED_IDLE;
1783 }
1784
0a7de745 1785 if (!thread->kernel_stack) {
3e170ce0 1786 flags |= TH_FLAGS_SWAPPED;
0a7de745 1787 }
3e170ce0
A
1788
1789 state = 0;
0a7de745 1790 if (thread->state & TH_TERMINATE) {
3e170ce0 1791 state = TH_STATE_HALTED;
0a7de745 1792 } else if (thread->state & TH_RUN) {
3e170ce0 1793 state = TH_STATE_RUNNING;
0a7de745 1794 } else if (thread->state & TH_UNINT) {
3e170ce0 1795 state = TH_STATE_UNINTERRUPTIBLE;
0a7de745 1796 } else if (thread->state & TH_SUSP) {
3e170ce0 1797 state = TH_STATE_STOPPED;
0a7de745 1798 } else if (thread->state & TH_WAIT) {
3e170ce0 1799 state = TH_STATE_WAITING;
0a7de745 1800 }
3e170ce0
A
1801
1802 basic_info->run_state = state;
1803 basic_info->flags = flags;
1804
1805 basic_info->suspend_count = thread->user_stop_count;
1806
1807 return;
1808}
b0d623f7 1809
1c79356b 1810kern_return_t
91447636 1811thread_info_internal(
0a7de745
A
1812 thread_t thread,
1813 thread_flavor_t flavor,
1814 thread_info_t thread_info_out, /* ptr to OUT array */
1815 mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
1c79356b 1816{
0a7de745 1817 spl_t s;
1c79356b 1818
0a7de745
A
1819 if (thread == THREAD_NULL) {
1820 return KERN_INVALID_ARGUMENT;
1821 }
1c79356b
A
1822
1823 if (flavor == THREAD_BASIC_INFO) {
0a7de745
A
1824 if (*thread_info_count < THREAD_BASIC_INFO_COUNT) {
1825 return KERN_INVALID_ARGUMENT;
1826 }
1c79356b 1827
3e170ce0
A
1828 s = splsched();
1829 thread_lock(thread);
1c79356b 1830
3e170ce0 1831 retrieve_thread_basic_info(thread, (thread_basic_info_t) thread_info_out);
1c79356b 1832
3e170ce0
A
1833 thread_unlock(thread);
1834 splx(s);
0b4e3aa0 1835
3e170ce0 1836 *thread_info_count = THREAD_BASIC_INFO_COUNT;
0b4e3aa0 1837
0a7de745
A
1838 return KERN_SUCCESS;
1839 } else if (flavor == THREAD_IDENTIFIER_INFO) {
1840 thread_identifier_info_t identifier_info;
b0d623f7 1841
0a7de745
A
1842 if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT) {
1843 return KERN_INVALID_ARGUMENT;
1844 }
b0d623f7 1845
3e170ce0 1846 identifier_info = (thread_identifier_info_t) thread_info_out;
b0d623f7 1847
3e170ce0
A
1848 s = splsched();
1849 thread_lock(thread);
b0d623f7 1850
3e170ce0
A
1851 identifier_info->thread_id = thread->thread_id;
1852 identifier_info->thread_handle = thread->machine.cthread_self;
1853 identifier_info->dispatch_qaddr = thread_dispatchqaddr(thread);
b0d623f7 1854
3e170ce0
A
1855 thread_unlock(thread);
1856 splx(s);
1857 return KERN_SUCCESS;
0a7de745
A
1858 } else if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
1859 policy_timeshare_info_t ts_info;
1c79356b 1860
0a7de745
A
1861 if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT) {
1862 return KERN_INVALID_ARGUMENT;
1863 }
1c79356b
A
1864
1865 ts_info = (policy_timeshare_info_t)thread_info_out;
1866
3e170ce0 1867 s = splsched();
1c79356b
A
1868 thread_lock(thread);
1869
3e170ce0
A
1870 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1871 thread_unlock(thread);
1c79356b 1872 splx(s);
0a7de745 1873 return KERN_INVALID_POLICY;
3e170ce0 1874 }
1c79356b 1875
6d2010ae 1876 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
9bccf70c
A
1877 if (ts_info->depressed) {
1878 ts_info->base_priority = DEPRESSPRI;
3e170ce0 1879 ts_info->depress_priority = thread->base_pri;
0a7de745 1880 } else {
3e170ce0 1881 ts_info->base_priority = thread->base_pri;
9bccf70c
A
1882 ts_info->depress_priority = -1;
1883 }
1c79356b 1884
9bccf70c 1885 ts_info->cur_priority = thread->sched_pri;
0a7de745 1886 ts_info->max_priority = thread->max_priority;
1c79356b
A
1887
1888 thread_unlock(thread);
3e170ce0 1889 splx(s);
1c79356b
A
1890
1891 *thread_info_count = POLICY_TIMESHARE_INFO_COUNT;
1892
0a7de745
A
1893 return KERN_SUCCESS;
1894 } else if (flavor == THREAD_SCHED_FIFO_INFO) {
1895 if (*thread_info_count < POLICY_FIFO_INFO_COUNT) {
1896 return KERN_INVALID_ARGUMENT;
1897 }
1c79356b 1898
0a7de745
A
1899 return KERN_INVALID_POLICY;
1900 } else if (flavor == THREAD_SCHED_RR_INFO) {
1901 policy_rr_info_t rr_info;
6d2010ae
A
1902 uint32_t quantum_time;
1903 uint64_t quantum_ns;
3e170ce0 1904
0a7de745
A
1905 if (*thread_info_count < POLICY_RR_INFO_COUNT) {
1906 return KERN_INVALID_ARGUMENT;
1907 }
1c79356b
A
1908
1909 rr_info = (policy_rr_info_t) thread_info_out;
1910
3e170ce0 1911 s = splsched();
1c79356b
A
1912 thread_lock(thread);
1913
3e170ce0
A
1914 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1915 thread_unlock(thread);
1c79356b
A
1916 splx(s);
1917
0a7de745
A
1918 return KERN_INVALID_POLICY;
1919 }
1c79356b 1920
6d2010ae 1921 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
9bccf70c
A
1922 if (rr_info->depressed) {
1923 rr_info->base_priority = DEPRESSPRI;
3e170ce0 1924 rr_info->depress_priority = thread->base_pri;
0a7de745 1925 } else {
3e170ce0 1926 rr_info->base_priority = thread->base_pri;
9bccf70c
A
1927 rr_info->depress_priority = -1;
1928 }
1929
6d2010ae
A
1930 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
1931 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
3e170ce0 1932
1c79356b 1933 rr_info->max_priority = thread->max_priority;
3e170ce0 1934 rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1c79356b 1935
1c79356b 1936 thread_unlock(thread);
3e170ce0 1937 splx(s);
1c79356b
A
1938
1939 *thread_info_count = POLICY_RR_INFO_COUNT;
1940
0a7de745
A
1941 return KERN_SUCCESS;
1942 } else if (flavor == THREAD_EXTENDED_INFO) {
1943 thread_basic_info_data_t basic_info;
1944 thread_extended_info_t extended_info = (thread_extended_info_t) thread_info_out;
3e170ce0
A
1945
1946 if (*thread_info_count < THREAD_EXTENDED_INFO_COUNT) {
0a7de745 1947 return KERN_INVALID_ARGUMENT;
3e170ce0
A
1948 }
1949
1950 s = splsched();
1951 thread_lock(thread);
1952
1953 /* NOTE: This mimics fill_taskthreadinfo(), which is the function used by proc_pidinfo() for
1954 * the PROC_PIDTHREADINFO flavor (which can't be used on corpses)
1955 */
1956 retrieve_thread_basic_info(thread, &basic_info);
cb323159
A
1957 extended_info->pth_user_time = (((uint64_t)basic_info.user_time.seconds * NSEC_PER_SEC) + ((uint64_t)basic_info.user_time.microseconds * NSEC_PER_USEC));
1958 extended_info->pth_system_time = (((uint64_t)basic_info.system_time.seconds * NSEC_PER_SEC) + ((uint64_t)basic_info.system_time.microseconds * NSEC_PER_USEC));
3e170ce0
A
1959
1960 extended_info->pth_cpu_usage = basic_info.cpu_usage;
1961 extended_info->pth_policy = basic_info.policy;
1962 extended_info->pth_run_state = basic_info.run_state;
1963 extended_info->pth_flags = basic_info.flags;
1964 extended_info->pth_sleep_time = basic_info.sleep_time;
1965 extended_info->pth_curpri = thread->sched_pri;
1966 extended_info->pth_priority = thread->base_pri;
1967 extended_info->pth_maxpriority = thread->max_priority;
1968
0a7de745 1969 bsd_getthreadname(thread->uthread, extended_info->pth_name);
3e170ce0
A
1970
1971 thread_unlock(thread);
1972 splx(s);
1973
1974 *thread_info_count = THREAD_EXTENDED_INFO_COUNT;
1975
0a7de745
A
1976 return KERN_SUCCESS;
1977 } else if (flavor == THREAD_DEBUG_INFO_INTERNAL) {
3e170ce0
A
1978#if DEVELOPMENT || DEBUG
1979 thread_debug_info_internal_t dbg_info;
0a7de745
A
1980 if (*thread_info_count < THREAD_DEBUG_INFO_INTERNAL_COUNT) {
1981 return KERN_NOT_SUPPORTED;
1982 }
3e170ce0 1983
0a7de745
A
1984 if (thread_info_out == NULL) {
1985 return KERN_INVALID_ARGUMENT;
1986 }
3e170ce0
A
1987
1988 dbg_info = (thread_debug_info_internal_t) thread_info_out;
1989 dbg_info->page_creation_count = thread->t_page_creation_count;
1990
1991 *thread_info_count = THREAD_DEBUG_INFO_INTERNAL_COUNT;
0a7de745 1992 return KERN_SUCCESS;
3e170ce0 1993#endif /* DEVELOPMENT || DEBUG */
0a7de745 1994 return KERN_NOT_SUPPORTED;
1c79356b
A
1995 }
1996
0a7de745 1997 return KERN_INVALID_ARGUMENT;
1c79356b
A
1998}
1999
2000void
91447636 2001thread_read_times(
0a7de745
A
2002 thread_t thread,
2003 time_value_t *user_time,
2004 time_value_t *system_time,
2005 time_value_t *runnable_time)
1c79356b 2006{
0a7de745
A
2007 clock_sec_t secs;
2008 clock_usec_t usecs;
2009 uint64_t tval_user, tval_system;
b0d623f7 2010
316670eb
A
2011 tval_user = timer_grab(&thread->user_timer);
2012 tval_system = timer_grab(&thread->system_timer);
9bccf70c 2013
316670eb
A
2014 if (thread->precise_user_kernel_time) {
2015 absolutetime_to_microtime(tval_user, &secs, &usecs);
2016 user_time->seconds = (typeof(user_time->seconds))secs;
2017 user_time->microseconds = usecs;
d9a64523 2018
316670eb
A
2019 absolutetime_to_microtime(tval_system, &secs, &usecs);
2020 system_time->seconds = (typeof(system_time->seconds))secs;
2021 system_time->microseconds = usecs;
2022 } else {
2023 /* system_timer may represent either sys or user */
2024 tval_user += tval_system;
2025 absolutetime_to_microtime(tval_user, &secs, &usecs);
2026 user_time->seconds = (typeof(user_time->seconds))secs;
2027 user_time->microseconds = usecs;
2028
2029 system_time->seconds = 0;
2030 system_time->microseconds = 0;
2031 }
d9a64523
A
2032
2033 if (runnable_time) {
2034 uint64_t tval_runnable = timer_grab(&thread->runnable_timer);
2035 absolutetime_to_microtime(tval_runnable, &secs, &usecs);
2036 runnable_time->seconds = (typeof(runnable_time->seconds))secs;
2037 runnable_time->microseconds = usecs;
2038 }
1c79356b
A
2039}
2040
0a7de745
A
2041uint64_t
2042thread_get_runtime_self(void)
fe8ab488
A
2043{
2044 boolean_t interrupt_state;
2045 uint64_t runtime;
2046 thread_t thread = NULL;
2047 processor_t processor = NULL;
2048
2049 thread = current_thread();
2050
2051 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
2052 interrupt_state = ml_set_interrupts_enabled(FALSE);
2053 processor = current_processor();
d9a64523 2054 timer_update(PROCESSOR_DATA(processor, thread_timer), mach_absolute_time());
fe8ab488
A
2055 runtime = (timer_grab(&thread->user_timer) + timer_grab(&thread->system_timer));
2056 ml_set_interrupts_enabled(interrupt_state);
2057
2058 return runtime;
2059}
2060
1c79356b
A
2061kern_return_t
2062thread_assign(
0a7de745
A
2063 __unused thread_t thread,
2064 __unused processor_set_t new_pset)
1c79356b 2065{
0a7de745 2066 return KERN_FAILURE;
1c79356b
A
2067}
2068
2069/*
2070 * thread_assign_default:
2071 *
2072 * Special version of thread_assign for assigning threads to default
2073 * processor set.
2074 */
2075kern_return_t
2076thread_assign_default(
0a7de745 2077 thread_t thread)
1c79356b 2078{
0a7de745 2079 return thread_assign(thread, &pset0);
1c79356b
A
2080}
2081
2082/*
2083 * thread_get_assignment
2084 *
2085 * Return current assignment for this thread.
0a7de745 2086 */
1c79356b
A
2087kern_return_t
2088thread_get_assignment(
0a7de745
A
2089 thread_t thread,
2090 processor_set_t *pset)
1c79356b 2091{
0a7de745
A
2092 if (thread == NULL) {
2093 return KERN_INVALID_ARGUMENT;
2094 }
91447636 2095
2d21ac55
A
2096 *pset = &pset0;
2097
0a7de745 2098 return KERN_SUCCESS;
1c79356b
A
2099}
2100
2101/*
55e303ae 2102 * thread_wire_internal:
1c79356b
A
2103 *
2104 * Specify that the target thread must always be able
2105 * to run and to allocate memory.
2106 */
2107kern_return_t
55e303ae 2108thread_wire_internal(
0a7de745
A
2109 host_priv_t host_priv,
2110 thread_t thread,
2111 boolean_t wired,
2112 boolean_t *prev_state)
1c79356b 2113{
0a7de745
A
2114 if (host_priv == NULL || thread != current_thread()) {
2115 return KERN_INVALID_ARGUMENT;
2116 }
1c79356b
A
2117
2118 assert(host_priv == &realhost);
2119
0a7de745
A
2120 if (prev_state) {
2121 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
91447636 2122 }
0a7de745
A
2123
2124 if (wired) {
2125 if (!(thread->options & TH_OPT_VMPRIV)) {
2126 vm_page_free_reserve(1); /* XXX */
2127 }
2128 thread->options |= TH_OPT_VMPRIV;
2129 } else {
2130 if (thread->options & TH_OPT_VMPRIV) {
2131 vm_page_free_reserve(-1); /* XXX */
2132 }
2133 thread->options &= ~TH_OPT_VMPRIV;
1c79356b
A
2134 }
2135
0a7de745 2136 return KERN_SUCCESS;
1c79356b
A
2137}
2138
1c79356b
A
2139
2140/*
55e303ae 2141 * thread_wire:
1c79356b 2142 *
55e303ae 2143 * User-api wrapper for thread_wire_internal()
1c79356b 2144 */
55e303ae
A
2145kern_return_t
2146thread_wire(
0a7de745
A
2147 host_priv_t host_priv,
2148 thread_t thread,
2149 boolean_t wired)
1c79356b 2150{
0a7de745 2151 return thread_wire_internal(host_priv, thread, wired, NULL);
1c79356b
A
2152}
2153
39236c6e 2154
39037602
A
2155boolean_t
2156is_vm_privileged(void)
2157{
2158 return current_thread()->options & TH_OPT_VMPRIV ? TRUE : FALSE;
2159}
2160
fe8ab488
A
2161boolean_t
2162set_vm_privilege(boolean_t privileged)
2163{
2164 boolean_t was_vmpriv;
2165
0a7de745 2166 if (current_thread()->options & TH_OPT_VMPRIV) {
fe8ab488 2167 was_vmpriv = TRUE;
0a7de745 2168 } else {
fe8ab488 2169 was_vmpriv = FALSE;
0a7de745 2170 }
fe8ab488 2171
0a7de745 2172 if (privileged != FALSE) {
fe8ab488 2173 current_thread()->options |= TH_OPT_VMPRIV;
0a7de745 2174 } else {
fe8ab488 2175 current_thread()->options &= ~TH_OPT_VMPRIV;
0a7de745 2176 }
fe8ab488 2177
0a7de745 2178 return was_vmpriv;
fe8ab488
A
2179}
2180
ecc0ceb4
A
2181void
2182set_thread_rwlock_boost(void)
2183{
2184 current_thread()->rwlock_count++;
2185}
2186
2187void
2188clear_thread_rwlock_boost(void)
2189{
2190 thread_t thread = current_thread();
2191
2192 if ((thread->rwlock_count-- == 1) && (thread->sched_flags & TH_SFLAG_RW_PROMOTED)) {
d9a64523 2193 lck_rw_clear_promotion(thread, 0);
ecc0ceb4
A
2194 }
2195}
fe8ab488 2196
39236c6e
A
2197/*
2198 * XXX assuming current thread only, for now...
2199 */
2200void
5ba3f43e 2201thread_guard_violation(thread_t thread,
cb323159 2202 mach_exception_data_type_t code, mach_exception_data_type_t subcode, boolean_t fatal)
39236c6e
A
2203{
2204 assert(thread == current_thread());
d9a64523 2205
cb323159
A
2206 /* Don't set up the AST for kernel threads; this check is needed to ensure
2207 * that the guard_exc_* fields in the thread structure are set only by the
2208 * current thread and therefore, don't require a lock.
2209 */
0a7de745 2210 if (thread->task == kernel_task) {
d9a64523 2211 return;
0a7de745 2212 }
39236c6e 2213
cb323159
A
2214 assert(EXC_GUARD_DECODE_GUARD_TYPE(code));
2215
39236c6e
A
2216 /*
2217 * Use the saved state area of the thread structure
2218 * to store all info required to handle the AST when
cb323159
A
2219 * returning to userspace. It's possible that there is
2220 * already a pending guard exception. If it's non-fatal,
2221 * it can only be over-written by a fatal exception code.
39236c6e 2222 */
cb323159
A
2223 if (thread->guard_exc_info.code && (thread->guard_exc_fatal || !fatal)) {
2224 return;
2225 }
2226
5ba3f43e
A
2227 thread->guard_exc_info.code = code;
2228 thread->guard_exc_info.subcode = subcode;
cb323159
A
2229 thread->guard_exc_fatal = fatal ? 1 : 0;
2230
2231 spl_t s = splsched();
39236c6e 2232 thread_ast_set(thread, AST_GUARD);
5ba3f43e 2233 ast_propagate(thread);
39236c6e
A
2234 splx(s);
2235}
2236
2237/*
2238 * guard_ast:
2239 *
2240 * Handle AST_GUARD for a thread. This routine looks at the
2241 * state saved in the thread structure to determine the cause
0a7de745 2242 * of this exception. Based on this value, it invokes the
39236c6e
A
2243 * appropriate routine which determines other exception related
2244 * info and raises the exception.
2245 */
2246void
5ba3f43e
A
2247guard_ast(thread_t t)
2248{
2249 const mach_exception_data_type_t
0a7de745
A
2250 code = t->guard_exc_info.code,
2251 subcode = t->guard_exc_info.subcode;
5ba3f43e 2252
d9a64523
A
2253 t->guard_exc_info.code = 0;
2254 t->guard_exc_info.subcode = 0;
cb323159 2255 t->guard_exc_fatal = 0;
0a7de745 2256
5ba3f43e 2257 switch (EXC_GUARD_DECODE_GUARD_TYPE(code)) {
d9a64523
A
2258 case GUARD_TYPE_NONE:
2259 /* lingering AST_GUARD on the processor? */
2260 break;
5ba3f43e
A
2261 case GUARD_TYPE_MACH_PORT:
2262 mach_port_guard_ast(t, code, subcode);
d9a64523 2263 break;
5ba3f43e
A
2264 case GUARD_TYPE_FD:
2265 fd_guard_ast(t, code, subcode);
2266 break;
2267#if CONFIG_VNGUARD
2268 case GUARD_TYPE_VN:
2269 vn_guard_ast(t, code, subcode);
2270 break;
2271#endif
d9a64523
A
2272 case GUARD_TYPE_VIRT_MEMORY:
2273 virt_memory_guard_ast(t, code, subcode);
2274 break;
5ba3f43e
A
2275 default:
2276 panic("guard_exc_info %llx %llx", code, subcode);
2277 }
39236c6e
A
2278}
2279
316670eb 2280static void
39236c6e 2281thread_cputime_callback(int warning, __unused const void *arg0, __unused const void *arg1)
316670eb 2282{
39236c6e 2283 if (warning == LEDGER_WARNING_ROSE_ABOVE) {
0a7de745 2284#if CONFIG_TELEMETRY
39236c6e
A
2285 /*
2286 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
2287 * on the entire task so there are micro-stackshots available if and when
2288 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
2289 * for this thread only; but now that this task is suspect, knowing what all of
2290 * its threads are up to will be useful.
2291 */
2292 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 1);
2293#endif
2294 return;
2295 }
2296
2297#if CONFIG_TELEMETRY
2298 /*
2299 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
2300 * exceeded the limit, turn telemetry off for the task.
2301 */
2302 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 0);
2303#endif
2304
2305 if (warning == 0) {
39037602 2306 SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU();
39236c6e
A
2307 }
2308}
2309
2310void __attribute__((noinline))
39037602 2311SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU(void)
39236c6e
A
2312{
2313 int pid = 0;
0a7de745 2314 task_t task = current_task();
39236c6e
A
2315 thread_t thread = current_thread();
2316 uint64_t tid = thread->thread_id;
0a7de745 2317 const char *procname = "unknown";
39236c6e
A
2318 time_value_t thread_total_time = {0, 0};
2319 time_value_t thread_system_time;
2320 time_value_t thread_user_time;
2321 int action;
2322 uint8_t percentage;
39037602 2323 uint32_t usage_percent = 0;
39236c6e
A
2324 uint32_t interval_sec;
2325 uint64_t interval_ns;
2326 uint64_t balance_ns;
0a7de745
A
2327 boolean_t fatal = FALSE;
2328 boolean_t send_exc_resource = TRUE; /* in addition to RESOURCE_NOTIFY */
2329 kern_return_t kr;
39236c6e 2330
39037602 2331#ifdef EXC_RESOURCE_MONITORS
0a7de745 2332 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
39037602 2333#endif /* EXC_RESOURCE_MONITORS */
0a7de745 2334 struct ledger_entry_info lei;
39236c6e 2335
316670eb
A
2336 assert(thread->t_threadledger != LEDGER_NULL);
2337
2338 /*
39037602 2339 * Extract the fatal bit and suspend the monitor (which clears the bit).
316670eb 2340 */
39236c6e 2341 task_lock(task);
39236c6e
A
2342 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_FATAL_CPUMON) {
2343 fatal = TRUE;
39037602 2344 send_exc_resource = TRUE;
39236c6e 2345 }
39037602 2346 /* Only one thread can be here at a time. Whichever makes it through
0a7de745
A
2347 * first will successfully suspend the monitor and proceed to send the
2348 * notification. Other threads will get an error trying to suspend the
2349 * monitor and give up on sending the notification. In the first release,
2350 * the monitor won't be resumed for a number of seconds, but we may
2351 * eventually need to handle low-latency resume.
39037602
A
2352 */
2353 kr = task_suspend_cpumon(task);
39236c6e 2354 task_unlock(task);
0a7de745
A
2355 if (kr == KERN_INVALID_ARGUMENT) {
2356 return;
2357 }
39236c6e
A
2358
2359#ifdef MACH_BSD
2360 pid = proc_selfpid();
39037602 2361 if (task->bsd_info != NULL) {
39236c6e 2362 procname = proc_name_address(task->bsd_info);
39037602 2363 }
39236c6e
A
2364#endif
2365
2366 thread_get_cpulimit(&action, &percentage, &interval_ns);
2367
2368 interval_sec = (uint32_t)(interval_ns / NSEC_PER_SEC);
2369
d9a64523 2370 thread_read_times(thread, &thread_user_time, &thread_system_time, NULL);
39236c6e
A
2371 time_value_add(&thread_total_time, &thread_user_time);
2372 time_value_add(&thread_total_time, &thread_system_time);
39236c6e 2373 ledger_get_entry_info(thread->t_threadledger, thread_ledgers.cpu_time, &lei);
316670eb 2374
39037602 2375 /* credit/debit/balance/limit are in absolute time units;
0a7de745 2376 * the refill info is in nanoseconds. */
39236c6e 2377 absolutetime_to_nanoseconds(lei.lei_balance, &balance_ns);
39037602 2378 if (lei.lei_last_refill > 0) {
0a7de745 2379 usage_percent = (uint32_t)((balance_ns * 100ULL) / lei.lei_last_refill);
39037602 2380 }
39236c6e 2381
39037602 2382 /* TODO: show task total runtime (via TASK_ABSOLUTETIME_INFO)? */
cb323159
A
2383 printf("process %s[%d] thread %llu caught burning CPU! It used more than %d%% CPU over %u seconds\n",
2384 procname, pid, tid, percentage, interval_sec);
2385 printf(" (actual recent usage: %d%% over ~%llu seconds)\n",
2386 usage_percent, (lei.lei_last_refill + NSEC_PER_SEC / 2) / NSEC_PER_SEC);
2387 printf(" Thread lifetime cpu usage %d.%06ds, (%d.%06d user, %d.%06d sys)\n",
0a7de745
A
2388 thread_total_time.seconds, thread_total_time.microseconds,
2389 thread_user_time.seconds, thread_user_time.microseconds,
cb323159
A
2390 thread_system_time.seconds, thread_system_time.microseconds);
2391 printf(" Ledger balance: %lld; mabs credit: %lld; mabs debit: %lld\n",
2392 lei.lei_balance, lei.lei_credit, lei.lei_debit);
2393 printf(" mabs limit: %llu; mabs period: %llu ns; last refill: %llu ns%s.\n",
0a7de745
A
2394 lei.lei_limit, lei.lei_refill_period, lei.lei_last_refill,
2395 (fatal ? " [fatal violation]" : ""));
39236c6e 2396
39037602 2397 /*
0a7de745
A
2398 * For now, send RESOURCE_NOTIFY in parallel with EXC_RESOURCE. Once
2399 * we have logging parity, we will stop sending EXC_RESOURCE (24508922).
39037602 2400 */
39236c6e 2401
39037602
A
2402 /* RESOURCE_NOTIFY MIG specifies nanoseconds of CPU time */
2403 lei.lei_balance = balance_ns;
2404 absolutetime_to_nanoseconds(lei.lei_limit, &lei.lei_limit);
2405 trace_resource_violation(RMON_CPUUSAGE_VIOLATED, &lei);
2406 kr = send_resource_violation(send_cpu_usage_violation, task, &lei,
0a7de745 2407 fatal ? kRNFatalLimitFlag : 0);
39037602
A
2408 if (kr) {
2409 printf("send_resource_violation(CPU usage, ...): error %#x\n", kr);
39236c6e
A
2410 }
2411
39037602
A
2412#ifdef EXC_RESOURCE_MONITORS
2413 if (send_exc_resource) {
2414 if (disable_exc_resource) {
2415 printf("process %s[%d] thread %llu caught burning CPU! "
0a7de745
A
2416 "EXC_RESOURCE%s supressed by a boot-arg\n",
2417 procname, pid, tid, fatal ? " (and termination)" : "");
39037602
A
2418 return;
2419 }
2420
2421 if (audio_active) {
2422 printf("process %s[%d] thread %llu caught burning CPU! "
0a7de745
A
2423 "EXC_RESOURCE & termination supressed due to audio playback\n",
2424 procname, pid, tid);
39037602
A
2425 return;
2426 }
15129b1c 2427 }
39037602
A
2428
2429
2430 if (send_exc_resource) {
2431 code[0] = code[1] = 0;
2432 EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_CPU);
2433 if (fatal) {
2434 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR_FATAL);
0a7de745 2435 } else {
39037602
A
2436 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR);
2437 }
2438 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code[0], interval_sec);
2439 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[0], percentage);
2440 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[1], usage_percent);
2441 exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
fe8ab488 2442 }
39037602 2443#endif /* EXC_RESOURCE_MONITORS */
39236c6e
A
2444
2445 if (fatal) {
fe8ab488
A
2446#if CONFIG_JETSAM
2447 jetsam_on_ledger_cpulimit_exceeded();
2448#else
39236c6e 2449 task_terminate_internal(task);
fe8ab488
A
2450#endif
2451 }
2452}
2453
d9a64523 2454#if DEVELOPMENT || DEBUG
0a7de745
A
2455void __attribute__((noinline))
2456SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(task_t task, int thread_count)
d9a64523
A
2457{
2458 mach_exception_data_type_t code[EXCEPTION_CODE_MAX] = {0};
2459 int pid = task_pid(task);
0a7de745 2460 char procname[MAXCOMLEN + 1] = "unknown";
d9a64523
A
2461
2462 if (pid == 1) {
2463 /*
2464 * Cannot suspend launchd
2465 */
2466 return;
2467 }
2468
2469 proc_name(pid, procname, sizeof(procname));
2470
2471 if (disable_exc_resource) {
2472 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
0a7de745 2473 "supressed by a boot-arg. \n", procname, pid, thread_count);
d9a64523
A
2474 return;
2475 }
2476
2477 if (audio_active) {
2478 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
0a7de745 2479 "supressed due to audio playback.\n", procname, pid, thread_count);
d9a64523
A
2480 return;
2481 }
2482
2483 if (exc_via_corpse_forking == 0) {
2484 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
0a7de745
A
2485 "supressed due to corpse forking being disabled.\n", procname, pid,
2486 thread_count);
d9a64523
A
2487 return;
2488 }
2489
2490 printf("process %s[%d] crossed thread count high watermark (%d), sending "
0a7de745 2491 "EXC_RESOURCE\n", procname, pid, thread_count);
d9a64523
A
2492
2493 EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_THREADS);
2494 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_THREADS_HIGH_WATERMARK);
2495 EXC_RESOURCE_THREADS_ENCODE_THREADS(code[0], thread_count);
2496
2497 task_enqueue_exception_with_corpse(task, EXC_RESOURCE, code, EXCEPTION_CODE_MAX, NULL);
2498}
2499#endif /* DEVELOPMENT || DEBUG */
2500
0a7de745
A
2501void
2502thread_update_io_stats(thread_t thread, int size, int io_flags)
fe8ab488
A
2503{
2504 int io_tier;
2505
0a7de745 2506 if (thread->thread_io_stats == NULL || thread->task->task_io_stats == NULL) {
fe8ab488 2507 return;
0a7de745 2508 }
fe8ab488
A
2509
2510 if (io_flags & DKIO_READ) {
2511 UPDATE_IO_STATS(thread->thread_io_stats->disk_reads, size);
2512 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->disk_reads, size);
2513 }
0a7de745 2514
fe8ab488
A
2515 if (io_flags & DKIO_META) {
2516 UPDATE_IO_STATS(thread->thread_io_stats->metadata, size);
2517 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->metadata, size);
39236c6e 2518 }
0a7de745 2519
fe8ab488
A
2520 if (io_flags & DKIO_PAGING) {
2521 UPDATE_IO_STATS(thread->thread_io_stats->paging, size);
2522 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->paging, size);
2523 }
2524
2525 io_tier = ((io_flags & DKIO_TIER_MASK) >> DKIO_TIER_SHIFT);
0a7de745 2526 assert(io_tier < IO_NUM_PRIORITIES);
fe8ab488
A
2527
2528 UPDATE_IO_STATS(thread->thread_io_stats->io_priority[io_tier], size);
2529 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->io_priority[io_tier], size);
2530
2531 /* Update Total I/O Counts */
2532 UPDATE_IO_STATS(thread->thread_io_stats->total_io, size);
2533 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->total_io, size);
2534
39037602
A
2535 if (!(io_flags & DKIO_READ)) {
2536 DTRACE_IO3(physical_writes, struct task *, thread->task, uint32_t, size, int, io_flags);
2537 ledger_credit(thread->task->ledger, task_ledgers.physical_writes, size);
2538 }
316670eb
A
2539}
2540
39037602 2541static void
0a7de745
A
2542init_thread_ledgers(void)
2543{
316670eb
A
2544 ledger_template_t t;
2545 int idx;
0a7de745 2546
316670eb
A
2547 assert(thread_ledger_template == NULL);
2548
0a7de745 2549 if ((t = ledger_template_create("Per-thread ledger")) == NULL) {
316670eb 2550 panic("couldn't create thread ledger template");
0a7de745 2551 }
316670eb
A
2552
2553 if ((idx = ledger_entry_add(t, "cpu_time", "sched", "ns")) < 0) {
2554 panic("couldn't create cpu_time entry for thread ledger template");
2555 }
2556
39236c6e 2557 if (ledger_set_callback(t, idx, thread_cputime_callback, NULL, NULL) < 0) {
0a7de745 2558 panic("couldn't set thread ledger callback for cpu_time entry");
316670eb
A
2559 }
2560
2561 thread_ledgers.cpu_time = idx;
5ba3f43e
A
2562
2563 ledger_template_complete(t);
316670eb
A
2564 thread_ledger_template = t;
2565}
2566
39236c6e
A
2567/*
2568 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
2569 */
2570int
2571thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns)
2572{
0a7de745
A
2573 int64_t abstime = 0;
2574 uint64_t limittime = 0;
2575 thread_t thread = current_thread();
39236c6e
A
2576
2577 *percentage = 0;
2578 *interval_ns = 0;
2579 *action = 0;
2580
2581 if (thread->t_threadledger == LEDGER_NULL) {
2582 /*
2583 * This thread has no per-thread ledger, so it can't possibly
2584 * have a CPU limit applied.
2585 */
0a7de745 2586 return KERN_SUCCESS;
39236c6e
A
2587 }
2588
2589 ledger_get_period(thread->t_threadledger, thread_ledgers.cpu_time, interval_ns);
2590 ledger_get_limit(thread->t_threadledger, thread_ledgers.cpu_time, &abstime);
2591
2592 if ((abstime == LEDGER_LIMIT_INFINITY) || (*interval_ns == 0)) {
2593 /*
2594 * This thread's CPU time ledger has no period or limit; so it
2595 * doesn't have a CPU limit applied.
2596 */
0a7de745 2597 return KERN_SUCCESS;
39236c6e
A
2598 }
2599
2600 /*
2601 * This calculation is the converse to the one in thread_set_cpulimit().
2602 */
2603 absolutetime_to_nanoseconds(abstime, &limittime);
2604 *percentage = (limittime * 100ULL) / *interval_ns;
2605 assert(*percentage <= 100);
2606
2607 if (thread->options & TH_OPT_PROC_CPULIMIT) {
2608 assert((thread->options & TH_OPT_PRVT_CPULIMIT) == 0);
2609
2610 *action = THREAD_CPULIMIT_BLOCK;
2611 } else if (thread->options & TH_OPT_PRVT_CPULIMIT) {
2612 assert((thread->options & TH_OPT_PROC_CPULIMIT) == 0);
2613
2614 *action = THREAD_CPULIMIT_EXCEPTION;
2615 } else {
2616 *action = THREAD_CPULIMIT_DISABLE;
2617 }
2618
0a7de745 2619 return KERN_SUCCESS;
39236c6e
A
2620}
2621
316670eb
A
2622/*
2623 * Set CPU usage limit on a thread.
2624 *
2625 * Calling with percentage of 0 will unset the limit for this thread.
2626 */
316670eb
A
2627int
2628thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns)
2629{
0a7de745
A
2630 thread_t thread = current_thread();
2631 ledger_t l;
2632 uint64_t limittime = 0;
2633 uint64_t abstime = 0;
316670eb
A
2634
2635 assert(percentage <= 100);
2636
39236c6e 2637 if (action == THREAD_CPULIMIT_DISABLE) {
316670eb
A
2638 /*
2639 * Remove CPU limit, if any exists.
2640 */
2641 if (thread->t_threadledger != LEDGER_NULL) {
39236c6e 2642 l = thread->t_threadledger;
fe8ab488
A
2643 ledger_set_limit(l, thread_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0);
2644 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_IGNORE);
316670eb
A
2645 thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
2646 }
2647
0a7de745 2648 return 0;
316670eb
A
2649 }
2650
39236c6e 2651 if (interval_ns < MINIMUM_CPULIMIT_INTERVAL_MS * NSEC_PER_MSEC) {
0a7de745 2652 return KERN_INVALID_ARGUMENT;
39236c6e
A
2653 }
2654
0a7de745 2655 l = thread->t_threadledger;
316670eb
A
2656 if (l == LEDGER_NULL) {
2657 /*
2658 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
2659 */
0a7de745
A
2660 if ((l = ledger_instantiate(thread_ledger_template, LEDGER_CREATE_INACTIVE_ENTRIES)) == LEDGER_NULL) {
2661 return KERN_RESOURCE_SHORTAGE;
2662 }
316670eb
A
2663
2664 /*
2665 * We are the first to create this thread's ledger, so only activate our entry.
2666 */
2667 ledger_entry_setactive(l, thread_ledgers.cpu_time);
2668 thread->t_threadledger = l;
2669 }
2670
2671 /*
2672 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
2673 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
2674 */
2675 limittime = (interval_ns * percentage) / 100;
0a7de745 2676 nanoseconds_to_absolutetime(limittime, &abstime);
39236c6e 2677 ledger_set_limit(l, thread_ledgers.cpu_time, abstime, cpumon_ustackshots_trigger_pct);
316670eb
A
2678 /*
2679 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
2680 */
2681 ledger_set_period(l, thread_ledgers.cpu_time, interval_ns);
2682
316670eb 2683 if (action == THREAD_CPULIMIT_EXCEPTION) {
39236c6e
A
2684 /*
2685 * We don't support programming the CPU usage monitor on a task if any of its
2686 * threads have a per-thread blocking CPU limit configured.
2687 */
2688 if (thread->options & TH_OPT_PRVT_CPULIMIT) {
2689 panic("CPU usage monitor activated, but blocking thread limit exists");
2690 }
2691
2692 /*
2693 * Make a note that this thread's CPU limit is being used for the task-wide CPU
2694 * usage monitor. We don't have to arm the callback which will trigger the
2695 * exception, because that was done for us in ledger_instantiate (because the
2696 * ledger template used has a default callback).
2697 */
316670eb 2698 thread->options |= TH_OPT_PROC_CPULIMIT;
39236c6e
A
2699 } else {
2700 /*
2701 * We deliberately override any CPU limit imposed by a task-wide limit (eg
2702 * CPU usage monitor).
2703 */
0a7de745 2704 thread->options &= ~TH_OPT_PROC_CPULIMIT;
316670eb 2705
316670eb
A
2706 thread->options |= TH_OPT_PRVT_CPULIMIT;
2707 /* The per-thread ledger template by default has a callback for CPU time */
2708 ledger_disable_callback(l, thread_ledgers.cpu_time);
2709 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_BLOCK);
2710 }
2711
0a7de745 2712 return 0;
316670eb
A
2713}
2714
2d21ac55
A
2715void
2716thread_sched_call(
0a7de745
A
2717 thread_t thread,
2718 sched_call_t call)
2d21ac55 2719{
d9a64523
A
2720 assert((thread->state & TH_WAIT_REPORT) == 0);
2721 thread->sched_call = call;
2d21ac55
A
2722}
2723
d9a64523
A
2724uint64_t
2725thread_tid(
0a7de745 2726 thread_t thread)
39037602 2727{
0a7de745 2728 return thread != THREAD_NULL? thread->thread_id: 0;
39037602
A
2729}
2730
d9a64523
A
2731uint16_t
2732thread_set_tag(thread_t th, uint16_t tag)
39037602 2733{
d9a64523 2734 return thread_set_tag_internal(th, tag);
39037602
A
2735}
2736
d9a64523
A
2737uint16_t
2738thread_get_tag(thread_t th)
2d21ac55 2739{
d9a64523 2740 return thread_get_tag_internal(th);
2d21ac55 2741}
1c79356b 2742
b0d623f7 2743uint64_t
d9a64523 2744thread_last_run_time(thread_t th)
b0d623f7 2745{
d9a64523 2746 return th->last_run_time;
4b17d6b6
A
2747}
2748
b0d623f7
A
2749uint64_t
2750thread_dispatchqaddr(
0a7de745 2751 thread_t thread)
b0d623f7 2752{
0a7de745
A
2753 uint64_t dispatchqueue_addr;
2754 uint64_t thread_handle;
b0d623f7 2755
0a7de745 2756 if (thread == THREAD_NULL) {
39037602 2757 return 0;
0a7de745 2758 }
39037602
A
2759
2760 thread_handle = thread->machine.cthread_self;
0a7de745 2761 if (thread_handle == 0) {
39037602 2762 return 0;
0a7de745
A
2763 }
2764
2765 if (thread->inspection == TRUE) {
39037602 2766 dispatchqueue_addr = thread_handle + get_task_dispatchqueue_offset(thread->task);
0a7de745 2767 } else if (thread->task->bsd_info) {
39037602 2768 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
0a7de745 2769 } else {
39037602 2770 dispatchqueue_addr = 0;
0a7de745 2771 }
b0d623f7 2772
39037602 2773 return dispatchqueue_addr;
b0d623f7
A
2774}
2775
5ba3f43e
A
2776uint64_t
2777thread_rettokern_addr(
0a7de745 2778 thread_t thread)
5ba3f43e 2779{
0a7de745
A
2780 uint64_t rettokern_addr;
2781 uint64_t rettokern_offset;
2782 uint64_t thread_handle;
5ba3f43e 2783
0a7de745 2784 if (thread == THREAD_NULL) {
5ba3f43e 2785 return 0;
0a7de745 2786 }
5ba3f43e
A
2787
2788 thread_handle = thread->machine.cthread_self;
0a7de745 2789 if (thread_handle == 0) {
5ba3f43e 2790 return 0;
0a7de745 2791 }
5ba3f43e
A
2792
2793 if (thread->task->bsd_info) {
2794 rettokern_offset = get_return_to_kernel_offset_from_proc(thread->task->bsd_info);
2795
2796 /* Return 0 if return to kernel offset is not initialized. */
2797 if (rettokern_offset == 0) {
2798 rettokern_addr = 0;
2799 } else {
2800 rettokern_addr = thread_handle + rettokern_offset;
2801 }
2802 } else {
2803 rettokern_addr = 0;
2804 }
2805
2806 return rettokern_addr;
2807}
2808
91447636
A
2809/*
2810 * Export routines to other components for things that are done as macros
2811 * within the osfmk component.
2812 */
1c79356b 2813
d9a64523
A
2814#undef thread_mtx_lock
2815void thread_mtx_lock(thread_t thread);
2816void
2817thread_mtx_lock(thread_t thread)
2818{
2819 lck_mtx_lock(&thread->mutex);
2820}
2821
2822#undef thread_mtx_unlock
2823void thread_mtx_unlock(thread_t thread);
2824void
2825thread_mtx_unlock(thread_t thread)
2826{
2827 lck_mtx_unlock(&thread->mutex);
2828}
2829
91447636
A
2830#undef thread_reference
2831void thread_reference(thread_t thread);
1c79356b 2832void
91447636 2833thread_reference(
0a7de745 2834 thread_t thread)
1c79356b 2835{
0a7de745 2836 if (thread != THREAD_NULL) {
91447636 2837 thread_reference_internal(thread);
0a7de745 2838 }
1c79356b
A
2839}
2840
1c79356b 2841#undef thread_should_halt
91447636 2842
1c79356b
A
2843boolean_t
2844thread_should_halt(
0a7de745 2845 thread_t th)
1c79356b 2846{
0a7de745 2847 return thread_should_halt_fast(th);
55e303ae 2848}
2d21ac55 2849
fe8ab488
A
2850/*
2851 * thread_set_voucher_name - reset the voucher port name bound to this thread
2852 *
2853 * Conditions: nothing locked
fe8ab488
A
2854 */
2855
2856kern_return_t
2857thread_set_voucher_name(mach_port_name_t voucher_name)
2858{
2859 thread_t thread = current_thread();
2860 ipc_voucher_t new_voucher = IPC_VOUCHER_NULL;
2861 ipc_voucher_t voucher;
fe8ab488 2862 ledger_t bankledger = NULL;
d9a64523 2863 struct thread_group *banktg = NULL;
cb323159 2864 uint32_t persona_id = 0;
fe8ab488 2865
0a7de745 2866 if (MACH_PORT_DEAD == voucher_name) {
fe8ab488 2867 return KERN_INVALID_RIGHT;
0a7de745 2868 }
fe8ab488
A
2869
2870 /*
2871 * agressively convert to voucher reference
2872 */
2873 if (MACH_PORT_VALID(voucher_name)) {
2874 new_voucher = convert_port_name_to_voucher(voucher_name);
0a7de745 2875 if (IPC_VOUCHER_NULL == new_voucher) {
fe8ab488 2876 return KERN_INVALID_ARGUMENT;
0a7de745 2877 }
fe8ab488 2878 }
cb323159 2879 bank_get_bank_ledger_thread_group_and_persona(new_voucher, &bankledger, &banktg, &persona_id);
fe8ab488
A
2880
2881 thread_mtx_lock(thread);
2882 voucher = thread->ith_voucher;
2883 thread->ith_voucher_name = voucher_name;
2884 thread->ith_voucher = new_voucher;
fe8ab488
A
2885 thread_mtx_unlock(thread);
2886
5ba3f43e
A
2887 bank_swap_thread_bank_ledger(thread, bankledger);
2888
fe8ab488 2889 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
2890 MACHDBG_CODE(DBG_MACH_IPC, MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2891 (uintptr_t)thread_tid(thread),
2892 (uintptr_t)voucher_name,
2893 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher),
cb323159 2894 persona_id, 0);
fe8ab488 2895
0a7de745 2896 if (IPC_VOUCHER_NULL != voucher) {
fe8ab488 2897 ipc_voucher_release(voucher);
0a7de745 2898 }
fe8ab488
A
2899
2900 return KERN_SUCCESS;
2901}
2902
0a7de745 2903/*
fe8ab488
A
2904 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2905 *
2906 * Conditions: nothing locked
2907 *
0a7de745 2908 * NOTE: At the moment, there is no distinction between the current and effective
fe8ab488
A
2909 * vouchers because we only set them at the thread level currently.
2910 */
0a7de745 2911kern_return_t
fe8ab488 2912thread_get_mach_voucher(
0a7de745 2913 thread_act_t thread,
fe8ab488 2914 mach_voucher_selector_t __unused which,
0a7de745 2915 ipc_voucher_t *voucherp)
fe8ab488 2916{
0a7de745 2917 ipc_voucher_t voucher;
fe8ab488 2918
0a7de745 2919 if (THREAD_NULL == thread) {
fe8ab488 2920 return KERN_INVALID_ARGUMENT;
0a7de745 2921 }
fe8ab488
A
2922
2923 thread_mtx_lock(thread);
2924 voucher = thread->ith_voucher;
2925
fe8ab488
A
2926 if (IPC_VOUCHER_NULL != voucher) {
2927 ipc_voucher_reference(voucher);
2928 thread_mtx_unlock(thread);
2929 *voucherp = voucher;
2930 return KERN_SUCCESS;
2931 }
2932
cb323159 2933 thread_mtx_unlock(thread);
fe8ab488 2934
cb323159 2935 *voucherp = IPC_VOUCHER_NULL;
fe8ab488
A
2936 return KERN_SUCCESS;
2937}
2938
0a7de745 2939/*
fe8ab488
A
2940 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2941 *
2942 * Conditions: callers holds a reference on the voucher.
2943 * nothing locked.
2944 *
cb323159
A
2945 * We grab another reference to the voucher and bind it to the thread.
2946 * The old voucher reference associated with the thread is
fe8ab488
A
2947 * discarded.
2948 */
0a7de745 2949kern_return_t
fe8ab488 2950thread_set_mach_voucher(
0a7de745
A
2951 thread_t thread,
2952 ipc_voucher_t voucher)
fe8ab488
A
2953{
2954 ipc_voucher_t old_voucher;
fe8ab488 2955 ledger_t bankledger = NULL;
d9a64523 2956 struct thread_group *banktg = NULL;
cb323159 2957 uint32_t persona_id = 0;
fe8ab488 2958
0a7de745 2959 if (THREAD_NULL == thread) {
fe8ab488 2960 return KERN_INVALID_ARGUMENT;
0a7de745 2961 }
fe8ab488 2962
0a7de745 2963 if (thread != current_thread() && thread->started) {
fe8ab488 2964 return KERN_INVALID_ARGUMENT;
0a7de745 2965 }
fe8ab488 2966
fe8ab488 2967 ipc_voucher_reference(voucher);
cb323159 2968 bank_get_bank_ledger_thread_group_and_persona(voucher, &bankledger, &banktg, &persona_id);
5ba3f43e 2969
fe8ab488
A
2970 thread_mtx_lock(thread);
2971 old_voucher = thread->ith_voucher;
2972 thread->ith_voucher = voucher;
2973 thread->ith_voucher_name = MACH_PORT_NULL;
fe8ab488
A
2974 thread_mtx_unlock(thread);
2975
5ba3f43e
A
2976 bank_swap_thread_bank_ledger(thread, bankledger);
2977
fe8ab488 2978 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
2979 MACHDBG_CODE(DBG_MACH_IPC, MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2980 (uintptr_t)thread_tid(thread),
2981 (uintptr_t)MACH_PORT_NULL,
2982 VM_KERNEL_ADDRPERM((uintptr_t)voucher),
cb323159 2983 persona_id, 0);
fe8ab488
A
2984
2985 ipc_voucher_release(old_voucher);
2986
2987 return KERN_SUCCESS;
2988}
2989
0a7de745 2990/*
fe8ab488
A
2991 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2992 *
2993 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2994 * nothing locked.
2995 *
d9a64523 2996 * This function is no longer supported.
fe8ab488
A
2997 */
2998kern_return_t
2999thread_swap_mach_voucher(
0a7de745
A
3000 __unused thread_t thread,
3001 __unused ipc_voucher_t new_voucher,
3002 ipc_voucher_t *in_out_old_voucher)
fe8ab488 3003{
d9a64523
A
3004 /*
3005 * Currently this function is only called from a MIG generated
3006 * routine which doesn't release the reference on the voucher
3007 * addressed by in_out_old_voucher. To avoid leaking this reference,
3008 * a call to release it has been added here.
3009 */
3010 ipc_voucher_release(*in_out_old_voucher);
3011 return KERN_NOT_SUPPORTED;
fe8ab488
A
3012}
3013
0a7de745 3014/*
fe8ab488
A
3015 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
3016 */
3017kern_return_t
3018thread_get_current_voucher_origin_pid(
3019 int32_t *pid)
3020{
3021 uint32_t buf_size;
3022 kern_return_t kr;
3023 thread_t thread = current_thread();
0a7de745 3024
fe8ab488
A
3025 buf_size = sizeof(*pid);
3026 kr = mach_voucher_attr_command(thread->ith_voucher,
0a7de745
A
3027 MACH_VOUCHER_ATTR_KEY_BANK,
3028 BANK_ORIGINATOR_PID,
3029 NULL,
3030 0,
3031 (mach_voucher_attr_content_t)pid,
3032 &buf_size);
fe8ab488
A
3033
3034 return kr;
3035}
3036
5ba3f43e 3037
39037602
A
3038boolean_t
3039thread_has_thread_name(thread_t th)
3040{
3041 if ((th) && (th->uthread)) {
3042 return bsd_hasthreadname(th->uthread);
3043 }
3044
3045 /*
3046 * This is an odd case; clients may set the thread name based on the lack of
3047 * a name, but in this context there is no uthread to attach the name to.
3048 */
3049 return FALSE;
3050}
3051
3052void
3053thread_set_thread_name(thread_t th, const char* name)
3054{
3055 if ((th) && (th->uthread) && name) {
3056 bsd_setthreadname(th->uthread, name);
3057 }
3058}
3059
a39ff7e2
A
3060void
3061thread_set_honor_qlimit(thread_t thread)
3062{
3063 thread->options |= TH_OPT_HONOR_QLIMIT;
3064}
3065
3066void
3067thread_clear_honor_qlimit(thread_t thread)
3068{
3069 thread->options &= (~TH_OPT_HONOR_QLIMIT);
3070}
3071
490019cf
A
3072/*
3073 * thread_enable_send_importance - set/clear the SEND_IMPORTANCE thread option bit.
3074 */
0a7de745
A
3075void
3076thread_enable_send_importance(thread_t thread, boolean_t enable)
490019cf 3077{
0a7de745 3078 if (enable == TRUE) {
490019cf 3079 thread->options |= TH_OPT_SEND_IMPORTANCE;
0a7de745 3080 } else {
490019cf 3081 thread->options &= ~TH_OPT_SEND_IMPORTANCE;
0a7de745 3082 }
490019cf
A
3083}
3084
5ba3f43e
A
3085/*
3086 * thread_set_allocation_name - .
3087 */
3088
0a7de745
A
3089kern_allocation_name_t
3090thread_set_allocation_name(kern_allocation_name_t new_name)
5ba3f43e
A
3091{
3092 kern_allocation_name_t ret;
3093 thread_kernel_state_t kstate = thread_get_kernel_state(current_thread());
3094 ret = kstate->allocation_name;
3095 // fifo
0a7de745
A
3096 if (!new_name || !kstate->allocation_name) {
3097 kstate->allocation_name = new_name;
3098 }
5ba3f43e
A
3099 return ret;
3100}
3101
cb323159
A
3102void *
3103thread_iokit_tls_get(uint32_t index)
3104{
3105 assert(index < THREAD_SAVE_IOKIT_TLS_COUNT);
3106 return current_thread()->saved.iokit.tls[index];
3107}
3108
3109void
3110thread_iokit_tls_set(uint32_t index, void * data)
3111{
3112 assert(index < THREAD_SAVE_IOKIT_TLS_COUNT);
3113 current_thread()->saved.iokit.tls[index] = data;
3114}
3115
d9a64523
A
3116uint64_t
3117thread_get_last_wait_duration(thread_t thread)
3118{
3119 return thread->last_made_runnable_time - thread->last_run_time;
3120}
3121
cb323159
A
3122integer_t
3123thread_kern_get_pri(thread_t thr)
3124{
3125 return thr->base_pri;
3126}
3127
3128void
3129thread_kern_set_pri(thread_t thr, integer_t pri)
3130{
3131 sched_set_kernel_thread_priority(thr, pri);
3132}
3133
3134integer_t
3135thread_kern_get_kernel_maxpri(void)
3136{
3137 return MAXPRI_KERNEL;
3138}
3139
2d21ac55 3140#if CONFIG_DTRACE
0a7de745
A
3141uint32_t
3142dtrace_get_thread_predcache(thread_t thread)
2d21ac55 3143{
0a7de745 3144 if (thread != THREAD_NULL) {
2d21ac55 3145 return thread->t_dtrace_predcache;
0a7de745 3146 } else {
2d21ac55 3147 return 0;
0a7de745 3148 }
2d21ac55
A
3149}
3150
0a7de745
A
3151int64_t
3152dtrace_get_thread_vtime(thread_t thread)
2d21ac55 3153{
0a7de745 3154 if (thread != THREAD_NULL) {
2d21ac55 3155 return thread->t_dtrace_vtime;
0a7de745 3156 } else {
2d21ac55 3157 return 0;
0a7de745 3158 }
2d21ac55
A
3159}
3160
0a7de745
A
3161int
3162dtrace_get_thread_last_cpu_id(thread_t thread)
3e170ce0
A
3163{
3164 if ((thread != THREAD_NULL) && (thread->last_processor != PROCESSOR_NULL)) {
3165 return thread->last_processor->cpu_id;
3166 } else {
3167 return -1;
3168 }
3169}
3170
0a7de745
A
3171int64_t
3172dtrace_get_thread_tracing(thread_t thread)
2d21ac55 3173{
0a7de745 3174 if (thread != THREAD_NULL) {
2d21ac55 3175 return thread->t_dtrace_tracing;
0a7de745 3176 } else {
2d21ac55 3177 return 0;
0a7de745 3178 }
2d21ac55
A
3179}
3180
cb323159
A
3181uint16_t
3182dtrace_get_thread_inprobe(thread_t thread)
2d21ac55 3183{
0a7de745 3184 if (thread != THREAD_NULL) {
cb323159 3185 return thread->t_dtrace_inprobe;
0a7de745 3186 } else {
2d21ac55 3187 return 0;
0a7de745 3188 }
2d21ac55
A
3189}
3190
0a7de745
A
3191vm_offset_t
3192dtrace_get_kernel_stack(thread_t thread)
2d21ac55 3193{
0a7de745 3194 if (thread != THREAD_NULL) {
2d21ac55 3195 return thread->kernel_stack;
0a7de745 3196 } else {
2d21ac55 3197 return 0;
0a7de745 3198 }
2d21ac55
A
3199}
3200
5ba3f43e
A
3201#if KASAN
3202struct kasan_thread_data *
3203kasan_get_thread_data(thread_t thread)
3204{
3205 return &thread->kasan_data;
3206}
3207#endif
3208
cb323159
A
3209#if CONFIG_KSANCOV
3210void **
3211__sanitizer_get_thread_data(thread_t thread)
3212{
3213 return &thread->ksancov_data;
3214}
3215#endif
3216
0a7de745
A
3217int64_t
3218dtrace_calc_thread_recent_vtime(thread_t thread)
2d21ac55 3219{
2d21ac55
A
3220 if (thread != THREAD_NULL) {
3221 processor_t processor = current_processor();
0a7de745
A
3222 uint64_t abstime = mach_absolute_time();
3223 timer_t timer;
2d21ac55
A
3224
3225 timer = PROCESSOR_DATA(processor, thread_timer);
3226
3227 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
0a7de745
A
3228 (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
3229 } else {
2d21ac55 3230 return 0;
0a7de745 3231 }
2d21ac55
A
3232}
3233
0a7de745
A
3234void
3235dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
2d21ac55 3236{
0a7de745 3237 if (thread != THREAD_NULL) {
2d21ac55 3238 thread->t_dtrace_predcache = predcache;
0a7de745 3239 }
2d21ac55
A
3240}
3241
0a7de745
A
3242void
3243dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
2d21ac55 3244{
0a7de745 3245 if (thread != THREAD_NULL) {
2d21ac55 3246 thread->t_dtrace_vtime = vtime;
0a7de745 3247 }
2d21ac55
A
3248}
3249
0a7de745
A
3250void
3251dtrace_set_thread_tracing(thread_t thread, int64_t accum)
2d21ac55 3252{
0a7de745 3253 if (thread != THREAD_NULL) {
2d21ac55 3254 thread->t_dtrace_tracing = accum;
0a7de745 3255 }
2d21ac55
A
3256}
3257
0a7de745 3258void
cb323159 3259dtrace_set_thread_inprobe(thread_t thread, uint16_t inprobe)
2d21ac55
A
3260{
3261 if (thread != THREAD_NULL) {
cb323159 3262 thread->t_dtrace_inprobe = inprobe;
2d21ac55
A
3263 }
3264}
3265
0a7de745
A
3266vm_offset_t
3267dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
2d21ac55
A
3268{
3269 vm_offset_t prev = 0;
3270
3271 if (thread != THREAD_NULL) {
3272 prev = thread->recover;
3273 thread->recover = recover;
3274 }
3275 return prev;
3276}
3277
0a7de745
A
3278vm_offset_t
3279dtrace_sign_and_set_thread_recover(thread_t thread, vm_offset_t recover)
3280{
cb323159
A
3281#if defined(HAS_APPLE_PAC)
3282 return dtrace_set_thread_recover(thread,
3283 (vm_address_t)ptrauth_sign_unauthenticated((void *)recover,
3284 ptrauth_key_function_pointer,
3285 ptrauth_blend_discriminator(&thread->recover, PAC_DISCRIMINATOR_RECOVER)));
3286#else /* defined(HAS_APPLE_PAC) */
0a7de745 3287 return dtrace_set_thread_recover(thread, recover);
cb323159 3288#endif /* defined(HAS_APPLE_PAC) */
0a7de745
A
3289}
3290
3291void
3292dtrace_thread_bootstrap(void)
b0d623f7
A
3293{
3294 task_t task = current_task();
39236c6e
A
3295
3296 if (task->thread_count == 1) {
3297 thread_t thread = current_thread();
3298 if (thread->t_dtrace_flags & TH_DTRACE_EXECSUCCESS) {
3299 thread->t_dtrace_flags &= ~TH_DTRACE_EXECSUCCESS;
3300 DTRACE_PROC(exec__success);
0a7de745
A
3301 KDBG(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXEC),
3302 task_pid(task));
39236c6e 3303 }
b0d623f7
A
3304 DTRACE_PROC(start);
3305 }
3306 DTRACE_PROC(lwp__start);
b0d623f7 3307}
39236c6e
A
3308
3309void
3310dtrace_thread_didexec(thread_t thread)
3311{
3312 thread->t_dtrace_flags |= TH_DTRACE_EXECSUCCESS;
3313}
2d21ac55 3314#endif /* CONFIG_DTRACE */